super.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Artem Bityutskiy (Битюцкий Артём)
  20. * Adrian Hunter
  21. */
  22. /*
  23. * This file implements UBIFS initialization and VFS superblock operations. Some
  24. * initialization stuff which is rather large and complex is placed at
  25. * corresponding subsystems, but most of it is here.
  26. */
  27. #include "ubifs.h"
  28. #include <linux/math64.h>
  29. #define INODE_LOCKED_MAX 64
  30. struct super_block *ubifs_sb;
  31. static struct inode *inodes_locked_down[INODE_LOCKED_MAX];
  32. /* shrinker.c */
  33. /* List of all UBIFS file-system instances */
  34. struct list_head ubifs_infos;
  35. /* linux/fs/super.c */
  36. static int sb_set(struct super_block *sb, void *data)
  37. {
  38. dev_t *dev = data;
  39. sb->s_dev = *dev;
  40. return 0;
  41. }
  42. /**
  43. * sget - find or create a superblock
  44. * @type: filesystem type superblock should belong to
  45. * @test: comparison callback
  46. * @set: setup callback
  47. * @data: argument to each of them
  48. */
  49. struct super_block *sget(struct file_system_type *type,
  50. int (*test)(struct super_block *,void *),
  51. int (*set)(struct super_block *,void *),
  52. void *data)
  53. {
  54. struct super_block *s = NULL;
  55. int err;
  56. s = kzalloc(sizeof(struct super_block), GFP_USER);
  57. if (!s) {
  58. err = -ENOMEM;
  59. return ERR_PTR(err);
  60. }
  61. INIT_LIST_HEAD(&s->s_instances);
  62. INIT_LIST_HEAD(&s->s_inodes);
  63. s->s_time_gran = 1000000000;
  64. err = set(s, data);
  65. if (err) {
  66. return ERR_PTR(err);
  67. }
  68. s->s_type = type;
  69. strncpy(s->s_id, type->name, sizeof(s->s_id));
  70. list_add(&s->s_instances, &type->fs_supers);
  71. return s;
  72. }
  73. /**
  74. * validate_inode - validate inode.
  75. * @c: UBIFS file-system description object
  76. * @inode: the inode to validate
  77. *
  78. * This is a helper function for 'ubifs_iget()' which validates various fields
  79. * of a newly built inode to make sure they contain sane values and prevent
  80. * possible vulnerabilities. Returns zero if the inode is all right and
  81. * a non-zero error code if not.
  82. */
  83. static int validate_inode(struct ubifs_info *c, const struct inode *inode)
  84. {
  85. int err;
  86. const struct ubifs_inode *ui = ubifs_inode(inode);
  87. if (inode->i_size > c->max_inode_sz) {
  88. ubifs_err("inode is too large (%lld)",
  89. (long long)inode->i_size);
  90. return 1;
  91. }
  92. if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
  93. ubifs_err("unknown compression type %d", ui->compr_type);
  94. return 2;
  95. }
  96. if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
  97. return 4;
  98. if (!ubifs_compr_present(ui->compr_type)) {
  99. ubifs_warn("inode %lu uses '%s' compression, but it was not "
  100. "compiled in", inode->i_ino,
  101. ubifs_compr_name(ui->compr_type));
  102. }
  103. err = dbg_check_dir_size(c, inode);
  104. return err;
  105. }
  106. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  107. {
  108. struct inode *inode;
  109. inode = (struct inode *)malloc(sizeof(struct ubifs_inode));
  110. if (inode) {
  111. inode->i_ino = ino;
  112. inode->i_sb = sb;
  113. list_add(&inode->i_sb_list, &sb->s_inodes);
  114. inode->i_state = I_LOCK | I_NEW;
  115. }
  116. return inode;
  117. }
  118. int ubifs_iput(struct inode *inode)
  119. {
  120. list_del_init(&inode->i_sb_list);
  121. free(inode);
  122. return 0;
  123. }
  124. /*
  125. * Lock (save) inode in inode array for readback after recovery
  126. */
  127. void iput(struct inode *inode)
  128. {
  129. int i;
  130. struct inode *ino;
  131. /*
  132. * Search end of list
  133. */
  134. for (i = 0; i < INODE_LOCKED_MAX; i++) {
  135. if (inodes_locked_down[i] == NULL)
  136. break;
  137. }
  138. if (i >= INODE_LOCKED_MAX) {
  139. ubifs_err("Error, can't lock (save) more inodes while recovery!!!");
  140. return;
  141. }
  142. /*
  143. * Allocate and use new inode
  144. */
  145. ino = (struct inode *)malloc(sizeof(struct ubifs_inode));
  146. memcpy(ino, inode, sizeof(struct ubifs_inode));
  147. /*
  148. * Finally save inode in array
  149. */
  150. inodes_locked_down[i] = ino;
  151. }
  152. struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
  153. {
  154. int err;
  155. union ubifs_key key;
  156. struct ubifs_ino_node *ino;
  157. struct ubifs_info *c = sb->s_fs_info;
  158. struct inode *inode;
  159. struct ubifs_inode *ui;
  160. int i;
  161. dbg_gen("inode %lu", inum);
  162. /*
  163. * U-Boot special handling of locked down inodes via recovery
  164. * e.g. ubifs_recover_size()
  165. */
  166. for (i = 0; i < INODE_LOCKED_MAX; i++) {
  167. /*
  168. * Exit on last entry (NULL), inode not found in list
  169. */
  170. if (inodes_locked_down[i] == NULL)
  171. break;
  172. if (inodes_locked_down[i]->i_ino == inum) {
  173. /*
  174. * We found the locked down inode in our array,
  175. * so just return this pointer instead of creating
  176. * a new one.
  177. */
  178. return inodes_locked_down[i];
  179. }
  180. }
  181. inode = iget_locked(sb, inum);
  182. if (!inode)
  183. return ERR_PTR(-ENOMEM);
  184. if (!(inode->i_state & I_NEW))
  185. return inode;
  186. ui = ubifs_inode(inode);
  187. ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  188. if (!ino) {
  189. err = -ENOMEM;
  190. goto out;
  191. }
  192. ino_key_init(c, &key, inode->i_ino);
  193. err = ubifs_tnc_lookup(c, &key, ino);
  194. if (err)
  195. goto out_ino;
  196. inode->i_flags |= (S_NOCMTIME | S_NOATIME);
  197. inode->i_nlink = le32_to_cpu(ino->nlink);
  198. inode->i_uid = le32_to_cpu(ino->uid);
  199. inode->i_gid = le32_to_cpu(ino->gid);
  200. inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
  201. inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
  202. inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
  203. inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
  204. inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
  205. inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
  206. inode->i_mode = le32_to_cpu(ino->mode);
  207. inode->i_size = le64_to_cpu(ino->size);
  208. ui->data_len = le32_to_cpu(ino->data_len);
  209. ui->flags = le32_to_cpu(ino->flags);
  210. ui->compr_type = le16_to_cpu(ino->compr_type);
  211. ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
  212. ui->synced_i_size = ui->ui_size = inode->i_size;
  213. err = validate_inode(c, inode);
  214. if (err)
  215. goto out_invalid;
  216. if ((inode->i_mode & S_IFMT) == S_IFLNK) {
  217. if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
  218. err = 12;
  219. goto out_invalid;
  220. }
  221. ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
  222. if (!ui->data) {
  223. err = -ENOMEM;
  224. goto out_ino;
  225. }
  226. memcpy(ui->data, ino->data, ui->data_len);
  227. ((char *)ui->data)[ui->data_len] = '\0';
  228. }
  229. kfree(ino);
  230. inode->i_state &= ~(I_LOCK | I_NEW);
  231. return inode;
  232. out_invalid:
  233. ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
  234. dbg_dump_node(c, ino);
  235. dbg_dump_inode(c, inode);
  236. err = -EINVAL;
  237. out_ino:
  238. kfree(ino);
  239. out:
  240. ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
  241. return ERR_PTR(err);
  242. }
  243. /**
  244. * init_constants_early - initialize UBIFS constants.
  245. * @c: UBIFS file-system description object
  246. *
  247. * This function initialize UBIFS constants which do not need the superblock to
  248. * be read. It also checks that the UBI volume satisfies basic UBIFS
  249. * requirements. Returns zero in case of success and a negative error code in
  250. * case of failure.
  251. */
  252. static int init_constants_early(struct ubifs_info *c)
  253. {
  254. if (c->vi.corrupted) {
  255. ubifs_warn("UBI volume is corrupted - read-only mode");
  256. c->ro_media = 1;
  257. }
  258. if (c->di.ro_mode) {
  259. ubifs_msg("read-only UBI device");
  260. c->ro_media = 1;
  261. }
  262. if (c->vi.vol_type == UBI_STATIC_VOLUME) {
  263. ubifs_msg("static UBI volume - read-only mode");
  264. c->ro_media = 1;
  265. }
  266. c->leb_cnt = c->vi.size;
  267. c->leb_size = c->vi.usable_leb_size;
  268. c->half_leb_size = c->leb_size / 2;
  269. c->min_io_size = c->di.min_io_size;
  270. c->min_io_shift = fls(c->min_io_size) - 1;
  271. if (c->leb_size < UBIFS_MIN_LEB_SZ) {
  272. ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
  273. c->leb_size, UBIFS_MIN_LEB_SZ);
  274. return -EINVAL;
  275. }
  276. if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  277. ubifs_err("too few LEBs (%d), min. is %d",
  278. c->leb_cnt, UBIFS_MIN_LEB_CNT);
  279. return -EINVAL;
  280. }
  281. if (!is_power_of_2(c->min_io_size)) {
  282. ubifs_err("bad min. I/O size %d", c->min_io_size);
  283. return -EINVAL;
  284. }
  285. /*
  286. * UBIFS aligns all node to 8-byte boundary, so to make function in
  287. * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
  288. * less than 8.
  289. */
  290. if (c->min_io_size < 8) {
  291. c->min_io_size = 8;
  292. c->min_io_shift = 3;
  293. }
  294. c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
  295. c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
  296. /*
  297. * Initialize node length ranges which are mostly needed for node
  298. * length validation.
  299. */
  300. c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
  301. c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
  302. c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
  303. c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
  304. c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
  305. c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
  306. c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
  307. c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
  308. c->ranges[UBIFS_ORPH_NODE].min_len =
  309. UBIFS_ORPH_NODE_SZ + sizeof(__le64);
  310. c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
  311. c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
  312. c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
  313. c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
  314. c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
  315. c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
  316. c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
  317. /*
  318. * Minimum indexing node size is amended later when superblock is
  319. * read and the key length is known.
  320. */
  321. c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
  322. /*
  323. * Maximum indexing node size is amended later when superblock is
  324. * read and the fanout is known.
  325. */
  326. c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
  327. /*
  328. * Initialize dead and dark LEB space watermarks. See gc.c for comments
  329. * about these values.
  330. */
  331. c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
  332. c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
  333. /*
  334. * Calculate how many bytes would be wasted at the end of LEB if it was
  335. * fully filled with data nodes of maximum size. This is used in
  336. * calculations when reporting free space.
  337. */
  338. c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
  339. return 0;
  340. }
  341. /*
  342. * init_constants_sb - initialize UBIFS constants.
  343. * @c: UBIFS file-system description object
  344. *
  345. * This is a helper function which initializes various UBIFS constants after
  346. * the superblock has been read. It also checks various UBIFS parameters and
  347. * makes sure they are all right. Returns zero in case of success and a
  348. * negative error code in case of failure.
  349. */
  350. static int init_constants_sb(struct ubifs_info *c)
  351. {
  352. int tmp, err;
  353. long long tmp64;
  354. c->main_bytes = (long long)c->main_lebs * c->leb_size;
  355. c->max_znode_sz = sizeof(struct ubifs_znode) +
  356. c->fanout * sizeof(struct ubifs_zbranch);
  357. tmp = ubifs_idx_node_sz(c, 1);
  358. c->ranges[UBIFS_IDX_NODE].min_len = tmp;
  359. c->min_idx_node_sz = ALIGN(tmp, 8);
  360. tmp = ubifs_idx_node_sz(c, c->fanout);
  361. c->ranges[UBIFS_IDX_NODE].max_len = tmp;
  362. c->max_idx_node_sz = ALIGN(tmp, 8);
  363. /* Make sure LEB size is large enough to fit full commit */
  364. tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
  365. tmp = ALIGN(tmp, c->min_io_size);
  366. if (tmp > c->leb_size) {
  367. dbg_err("too small LEB size %d, at least %d needed",
  368. c->leb_size, tmp);
  369. return -EINVAL;
  370. }
  371. /*
  372. * Make sure that the log is large enough to fit reference nodes for
  373. * all buds plus one reserved LEB.
  374. */
  375. tmp64 = c->max_bud_bytes + c->leb_size - 1;
  376. c->max_bud_cnt = div_u64(tmp64, c->leb_size);
  377. tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
  378. tmp /= c->leb_size;
  379. tmp += 1;
  380. if (c->log_lebs < tmp) {
  381. dbg_err("too small log %d LEBs, required min. %d LEBs",
  382. c->log_lebs, tmp);
  383. return -EINVAL;
  384. }
  385. /*
  386. * When budgeting we assume worst-case scenarios when the pages are not
  387. * be compressed and direntries are of the maximum size.
  388. *
  389. * Note, data, which may be stored in inodes is budgeted separately, so
  390. * it is not included into 'c->inode_budget'.
  391. */
  392. c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
  393. c->inode_budget = UBIFS_INO_NODE_SZ;
  394. c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
  395. /*
  396. * When the amount of flash space used by buds becomes
  397. * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
  398. * The writers are unblocked when the commit is finished. To avoid
  399. * writers to be blocked UBIFS initiates background commit in advance,
  400. * when number of bud bytes becomes above the limit defined below.
  401. */
  402. c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
  403. /*
  404. * Ensure minimum journal size. All the bytes in the journal heads are
  405. * considered to be used, when calculating the current journal usage.
  406. * Consequently, if the journal is too small, UBIFS will treat it as
  407. * always full.
  408. */
  409. tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
  410. if (c->bg_bud_bytes < tmp64)
  411. c->bg_bud_bytes = tmp64;
  412. if (c->max_bud_bytes < tmp64 + c->leb_size)
  413. c->max_bud_bytes = tmp64 + c->leb_size;
  414. err = ubifs_calc_lpt_geom(c);
  415. if (err)
  416. return err;
  417. return 0;
  418. }
  419. /*
  420. * init_constants_master - initialize UBIFS constants.
  421. * @c: UBIFS file-system description object
  422. *
  423. * This is a helper function which initializes various UBIFS constants after
  424. * the master node has been read. It also checks various UBIFS parameters and
  425. * makes sure they are all right.
  426. */
  427. static void init_constants_master(struct ubifs_info *c)
  428. {
  429. long long tmp64;
  430. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  431. /*
  432. * Calculate total amount of FS blocks. This number is not used
  433. * internally because it does not make much sense for UBIFS, but it is
  434. * necessary to report something for the 'statfs()' call.
  435. *
  436. * Subtract the LEB reserved for GC, the LEB which is reserved for
  437. * deletions, minimum LEBs for the index, and assume only one journal
  438. * head is available.
  439. */
  440. tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
  441. tmp64 *= (long long)c->leb_size - c->leb_overhead;
  442. tmp64 = ubifs_reported_space(c, tmp64);
  443. c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
  444. }
  445. /**
  446. * free_orphans - free orphans.
  447. * @c: UBIFS file-system description object
  448. */
  449. static void free_orphans(struct ubifs_info *c)
  450. {
  451. struct ubifs_orphan *orph;
  452. while (c->orph_dnext) {
  453. orph = c->orph_dnext;
  454. c->orph_dnext = orph->dnext;
  455. list_del(&orph->list);
  456. kfree(orph);
  457. }
  458. while (!list_empty(&c->orph_list)) {
  459. orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
  460. list_del(&orph->list);
  461. kfree(orph);
  462. dbg_err("orphan list not empty at unmount");
  463. }
  464. vfree(c->orph_buf);
  465. c->orph_buf = NULL;
  466. }
  467. /**
  468. * check_volume_empty - check if the UBI volume is empty.
  469. * @c: UBIFS file-system description object
  470. *
  471. * This function checks if the UBIFS volume is empty by looking if its LEBs are
  472. * mapped or not. The result of checking is stored in the @c->empty variable.
  473. * Returns zero in case of success and a negative error code in case of
  474. * failure.
  475. */
  476. static int check_volume_empty(struct ubifs_info *c)
  477. {
  478. int lnum, err;
  479. c->empty = 1;
  480. for (lnum = 0; lnum < c->leb_cnt; lnum++) {
  481. err = ubi_is_mapped(c->ubi, lnum);
  482. if (unlikely(err < 0))
  483. return err;
  484. if (err == 1) {
  485. c->empty = 0;
  486. break;
  487. }
  488. cond_resched();
  489. }
  490. return 0;
  491. }
  492. /**
  493. * mount_ubifs - mount UBIFS file-system.
  494. * @c: UBIFS file-system description object
  495. *
  496. * This function mounts UBIFS file system. Returns zero in case of success and
  497. * a negative error code in case of failure.
  498. *
  499. * Note, the function does not de-allocate resources it it fails half way
  500. * through, and the caller has to do this instead.
  501. */
  502. static int mount_ubifs(struct ubifs_info *c)
  503. {
  504. struct super_block *sb = c->vfs_sb;
  505. int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
  506. long long x;
  507. size_t sz;
  508. err = init_constants_early(c);
  509. if (err)
  510. return err;
  511. err = ubifs_debugging_init(c);
  512. if (err)
  513. return err;
  514. err = check_volume_empty(c);
  515. if (err)
  516. goto out_free;
  517. if (c->empty && (mounted_read_only || c->ro_media)) {
  518. /*
  519. * This UBI volume is empty, and read-only, or the file system
  520. * is mounted read-only - we cannot format it.
  521. */
  522. ubifs_err("can't format empty UBI volume: read-only %s",
  523. c->ro_media ? "UBI volume" : "mount");
  524. err = -EROFS;
  525. goto out_free;
  526. }
  527. if (c->ro_media && !mounted_read_only) {
  528. ubifs_err("cannot mount read-write - read-only media");
  529. err = -EROFS;
  530. goto out_free;
  531. }
  532. /*
  533. * The requirement for the buffer is that it should fit indexing B-tree
  534. * height amount of integers. We assume the height if the TNC tree will
  535. * never exceed 64.
  536. */
  537. err = -ENOMEM;
  538. c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
  539. if (!c->bottom_up_buf)
  540. goto out_free;
  541. c->sbuf = vmalloc(c->leb_size);
  542. if (!c->sbuf)
  543. goto out_free;
  544. /*
  545. * We have to check all CRCs, even for data nodes, when we mount the FS
  546. * (specifically, when we are replaying).
  547. */
  548. c->always_chk_crc = 1;
  549. err = ubifs_read_superblock(c);
  550. if (err)
  551. goto out_free;
  552. /*
  553. * Make sure the compressor which is set as default in the superblock
  554. * or overridden by mount options is actually compiled in.
  555. */
  556. if (!ubifs_compr_present(c->default_compr)) {
  557. ubifs_err("'compressor \"%s\" is not compiled in",
  558. ubifs_compr_name(c->default_compr));
  559. goto out_free;
  560. }
  561. dbg_failure_mode_registration(c);
  562. err = init_constants_sb(c);
  563. if (err)
  564. goto out_free;
  565. sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
  566. sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
  567. c->cbuf = kmalloc(sz, GFP_NOFS);
  568. if (!c->cbuf) {
  569. err = -ENOMEM;
  570. goto out_free;
  571. }
  572. sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
  573. err = ubifs_read_master(c);
  574. if (err)
  575. goto out_master;
  576. init_constants_master(c);
  577. if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
  578. ubifs_msg("recovery needed");
  579. c->need_recovery = 1;
  580. }
  581. err = ubifs_lpt_init(c, 1, !mounted_read_only);
  582. if (err)
  583. goto out_lpt;
  584. err = dbg_check_idx_size(c, c->old_idx_sz);
  585. if (err)
  586. goto out_lpt;
  587. err = ubifs_replay_journal(c);
  588. if (err)
  589. goto out_journal;
  590. err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
  591. if (err)
  592. goto out_orphans;
  593. if (c->need_recovery) {
  594. err = ubifs_recover_size(c);
  595. if (err)
  596. goto out_orphans;
  597. }
  598. spin_lock(&ubifs_infos_lock);
  599. list_add_tail(&c->infos_list, &ubifs_infos);
  600. spin_unlock(&ubifs_infos_lock);
  601. if (c->need_recovery) {
  602. if (mounted_read_only)
  603. ubifs_msg("recovery deferred");
  604. else {
  605. c->need_recovery = 0;
  606. ubifs_msg("recovery completed");
  607. }
  608. }
  609. err = dbg_check_filesystem(c);
  610. if (err)
  611. goto out_infos;
  612. c->always_chk_crc = 0;
  613. ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
  614. c->vi.ubi_num, c->vi.vol_id, c->vi.name);
  615. if (mounted_read_only)
  616. ubifs_msg("mounted read-only");
  617. x = (long long)c->main_lebs * c->leb_size;
  618. ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d "
  619. "LEBs)", x, x >> 10, x >> 20, c->main_lebs);
  620. x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
  621. ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d "
  622. "LEBs)", x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
  623. ubifs_msg("media format: w%d/r%d (latest is w%d/r%d)",
  624. c->fmt_version, c->ro_compat_version,
  625. UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
  626. ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
  627. ubifs_msg("reserved for root: %llu bytes (%llu KiB)",
  628. c->report_rp_size, c->report_rp_size >> 10);
  629. dbg_msg("compiled on: " __DATE__ " at " __TIME__);
  630. dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
  631. dbg_msg("LEB size: %d bytes (%d KiB)",
  632. c->leb_size, c->leb_size >> 10);
  633. dbg_msg("data journal heads: %d",
  634. c->jhead_cnt - NONDATA_JHEADS_CNT);
  635. dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
  636. "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  637. c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
  638. c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
  639. c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
  640. c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
  641. dbg_msg("big_lpt %d", c->big_lpt);
  642. dbg_msg("log LEBs: %d (%d - %d)",
  643. c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
  644. dbg_msg("LPT area LEBs: %d (%d - %d)",
  645. c->lpt_lebs, c->lpt_first, c->lpt_last);
  646. dbg_msg("orphan area LEBs: %d (%d - %d)",
  647. c->orph_lebs, c->orph_first, c->orph_last);
  648. dbg_msg("main area LEBs: %d (%d - %d)",
  649. c->main_lebs, c->main_first, c->leb_cnt - 1);
  650. dbg_msg("index LEBs: %d", c->lst.idx_lebs);
  651. dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
  652. c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
  653. dbg_msg("key hash type: %d", c->key_hash_type);
  654. dbg_msg("tree fanout: %d", c->fanout);
  655. dbg_msg("reserved GC LEB: %d", c->gc_lnum);
  656. dbg_msg("first main LEB: %d", c->main_first);
  657. dbg_msg("max. znode size %d", c->max_znode_sz);
  658. dbg_msg("max. index node size %d", c->max_idx_node_sz);
  659. dbg_msg("node sizes: data %zu, inode %zu, dentry %zu",
  660. UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
  661. dbg_msg("node sizes: trun %zu, sb %zu, master %zu",
  662. UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
  663. dbg_msg("node sizes: ref %zu, cmt. start %zu, orph %zu",
  664. UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
  665. dbg_msg("max. node sizes: data %zu, inode %zu dentry %zu",
  666. UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
  667. UBIFS_MAX_DENT_NODE_SZ);
  668. dbg_msg("dead watermark: %d", c->dead_wm);
  669. dbg_msg("dark watermark: %d", c->dark_wm);
  670. dbg_msg("LEB overhead: %d", c->leb_overhead);
  671. x = (long long)c->main_lebs * c->dark_wm;
  672. dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
  673. x, x >> 10, x >> 20);
  674. dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
  675. c->max_bud_bytes, c->max_bud_bytes >> 10,
  676. c->max_bud_bytes >> 20);
  677. dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
  678. c->bg_bud_bytes, c->bg_bud_bytes >> 10,
  679. c->bg_bud_bytes >> 20);
  680. dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
  681. c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
  682. dbg_msg("max. seq. number: %llu", c->max_sqnum);
  683. dbg_msg("commit number: %llu", c->cmt_no);
  684. return 0;
  685. out_infos:
  686. spin_lock(&ubifs_infos_lock);
  687. list_del(&c->infos_list);
  688. spin_unlock(&ubifs_infos_lock);
  689. out_orphans:
  690. free_orphans(c);
  691. out_journal:
  692. out_lpt:
  693. ubifs_lpt_free(c, 0);
  694. out_master:
  695. kfree(c->mst_node);
  696. kfree(c->rcvrd_mst_node);
  697. if (c->bgt)
  698. kthread_stop(c->bgt);
  699. kfree(c->cbuf);
  700. out_free:
  701. vfree(c->ileb_buf);
  702. vfree(c->sbuf);
  703. kfree(c->bottom_up_buf);
  704. ubifs_debugging_exit(c);
  705. return err;
  706. }
  707. /**
  708. * ubifs_umount - un-mount UBIFS file-system.
  709. * @c: UBIFS file-system description object
  710. *
  711. * Note, this function is called to free allocated resourced when un-mounting,
  712. * as well as free resources when an error occurred while we were half way
  713. * through mounting (error path cleanup function). So it has to make sure the
  714. * resource was actually allocated before freeing it.
  715. */
  716. static void ubifs_umount(struct ubifs_info *c)
  717. {
  718. dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
  719. c->vi.vol_id);
  720. spin_lock(&ubifs_infos_lock);
  721. list_del(&c->infos_list);
  722. spin_unlock(&ubifs_infos_lock);
  723. if (c->bgt)
  724. kthread_stop(c->bgt);
  725. free_orphans(c);
  726. ubifs_lpt_free(c, 0);
  727. kfree(c->cbuf);
  728. kfree(c->rcvrd_mst_node);
  729. kfree(c->mst_node);
  730. vfree(c->ileb_buf);
  731. vfree(c->sbuf);
  732. kfree(c->bottom_up_buf);
  733. ubifs_debugging_exit(c);
  734. /* Finally free U-Boot's global copy of superblock */
  735. free(ubifs_sb->s_fs_info);
  736. free(ubifs_sb);
  737. }
  738. /**
  739. * open_ubi - parse UBI device name string and open the UBI device.
  740. * @name: UBI volume name
  741. * @mode: UBI volume open mode
  742. *
  743. * There are several ways to specify UBI volumes when mounting UBIFS:
  744. * o ubiX_Y - UBI device number X, volume Y;
  745. * o ubiY - UBI device number 0, volume Y;
  746. * o ubiX:NAME - mount UBI device X, volume with name NAME;
  747. * o ubi:NAME - mount UBI device 0, volume with name NAME.
  748. *
  749. * Alternative '!' separator may be used instead of ':' (because some shells
  750. * like busybox may interpret ':' as an NFS host name separator). This function
  751. * returns ubi volume object in case of success and a negative error code in
  752. * case of failure.
  753. */
  754. static struct ubi_volume_desc *open_ubi(const char *name, int mode)
  755. {
  756. int dev, vol;
  757. char *endptr;
  758. if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
  759. return ERR_PTR(-EINVAL);
  760. /* ubi:NAME method */
  761. if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
  762. return ubi_open_volume_nm(0, name + 4, mode);
  763. if (!isdigit(name[3]))
  764. return ERR_PTR(-EINVAL);
  765. dev = simple_strtoul(name + 3, &endptr, 0);
  766. /* ubiY method */
  767. if (*endptr == '\0')
  768. return ubi_open_volume(0, dev, mode);
  769. /* ubiX_Y method */
  770. if (*endptr == '_' && isdigit(endptr[1])) {
  771. vol = simple_strtoul(endptr + 1, &endptr, 0);
  772. if (*endptr != '\0')
  773. return ERR_PTR(-EINVAL);
  774. return ubi_open_volume(dev, vol, mode);
  775. }
  776. /* ubiX:NAME method */
  777. if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
  778. return ubi_open_volume_nm(dev, ++endptr, mode);
  779. return ERR_PTR(-EINVAL);
  780. }
  781. static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
  782. {
  783. struct ubi_volume_desc *ubi = sb->s_fs_info;
  784. struct ubifs_info *c;
  785. struct inode *root;
  786. int err;
  787. c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
  788. if (!c)
  789. return -ENOMEM;
  790. spin_lock_init(&c->cnt_lock);
  791. spin_lock_init(&c->cs_lock);
  792. spin_lock_init(&c->buds_lock);
  793. spin_lock_init(&c->space_lock);
  794. spin_lock_init(&c->orphan_lock);
  795. init_rwsem(&c->commit_sem);
  796. mutex_init(&c->lp_mutex);
  797. mutex_init(&c->tnc_mutex);
  798. mutex_init(&c->log_mutex);
  799. mutex_init(&c->mst_mutex);
  800. mutex_init(&c->umount_mutex);
  801. init_waitqueue_head(&c->cmt_wq);
  802. c->buds = RB_ROOT;
  803. c->old_idx = RB_ROOT;
  804. c->size_tree = RB_ROOT;
  805. c->orph_tree = RB_ROOT;
  806. INIT_LIST_HEAD(&c->infos_list);
  807. INIT_LIST_HEAD(&c->idx_gc);
  808. INIT_LIST_HEAD(&c->replay_list);
  809. INIT_LIST_HEAD(&c->replay_buds);
  810. INIT_LIST_HEAD(&c->uncat_list);
  811. INIT_LIST_HEAD(&c->empty_list);
  812. INIT_LIST_HEAD(&c->freeable_list);
  813. INIT_LIST_HEAD(&c->frdi_idx_list);
  814. INIT_LIST_HEAD(&c->unclean_leb_list);
  815. INIT_LIST_HEAD(&c->old_buds);
  816. INIT_LIST_HEAD(&c->orph_list);
  817. INIT_LIST_HEAD(&c->orph_new);
  818. c->highest_inum = UBIFS_FIRST_INO;
  819. c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
  820. ubi_get_volume_info(ubi, &c->vi);
  821. ubi_get_device_info(c->vi.ubi_num, &c->di);
  822. /* Re-open the UBI device in read-write mode */
  823. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  824. if (IS_ERR(c->ubi)) {
  825. err = PTR_ERR(c->ubi);
  826. goto out_free;
  827. }
  828. c->vfs_sb = sb;
  829. sb->s_fs_info = c;
  830. sb->s_magic = UBIFS_SUPER_MAGIC;
  831. sb->s_blocksize = UBIFS_BLOCK_SIZE;
  832. sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
  833. sb->s_dev = c->vi.cdev;
  834. sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
  835. if (c->max_inode_sz > MAX_LFS_FILESIZE)
  836. sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
  837. if (c->rw_incompat) {
  838. ubifs_err("the file-system is not R/W-compatible");
  839. ubifs_msg("on-flash format version is w%d/r%d, but software "
  840. "only supports up to version w%d/r%d", c->fmt_version,
  841. c->ro_compat_version, UBIFS_FORMAT_VERSION,
  842. UBIFS_RO_COMPAT_VERSION);
  843. return -EROFS;
  844. }
  845. mutex_lock(&c->umount_mutex);
  846. err = mount_ubifs(c);
  847. if (err) {
  848. ubifs_assert(err < 0);
  849. goto out_unlock;
  850. }
  851. /* Read the root inode */
  852. root = ubifs_iget(sb, UBIFS_ROOT_INO);
  853. if (IS_ERR(root)) {
  854. err = PTR_ERR(root);
  855. goto out_umount;
  856. }
  857. sb->s_root = NULL;
  858. mutex_unlock(&c->umount_mutex);
  859. return 0;
  860. out_umount:
  861. ubifs_umount(c);
  862. out_unlock:
  863. mutex_unlock(&c->umount_mutex);
  864. ubi_close_volume(c->ubi);
  865. out_free:
  866. kfree(c);
  867. return err;
  868. }
  869. static int sb_test(struct super_block *sb, void *data)
  870. {
  871. dev_t *dev = data;
  872. return sb->s_dev == *dev;
  873. }
  874. static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
  875. const char *name, void *data, struct vfsmount *mnt)
  876. {
  877. struct ubi_volume_desc *ubi;
  878. struct ubi_volume_info vi;
  879. struct super_block *sb;
  880. int err;
  881. dbg_gen("name %s, flags %#x", name, flags);
  882. /*
  883. * Get UBI device number and volume ID. Mount it read-only so far
  884. * because this might be a new mount point, and UBI allows only one
  885. * read-write user at a time.
  886. */
  887. ubi = open_ubi(name, UBI_READONLY);
  888. if (IS_ERR(ubi)) {
  889. ubifs_err("cannot open \"%s\", error %d",
  890. name, (int)PTR_ERR(ubi));
  891. return PTR_ERR(ubi);
  892. }
  893. ubi_get_volume_info(ubi, &vi);
  894. dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
  895. sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
  896. if (IS_ERR(sb)) {
  897. err = PTR_ERR(sb);
  898. goto out_close;
  899. }
  900. if (sb->s_root) {
  901. /* A new mount point for already mounted UBIFS */
  902. dbg_gen("this ubi volume is already mounted");
  903. if ((flags ^ sb->s_flags) & MS_RDONLY) {
  904. err = -EBUSY;
  905. goto out_deact;
  906. }
  907. } else {
  908. sb->s_flags = flags;
  909. /*
  910. * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
  911. * replaced by 'c'.
  912. */
  913. sb->s_fs_info = ubi;
  914. err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  915. if (err)
  916. goto out_deact;
  917. /* We do not support atime */
  918. sb->s_flags |= MS_ACTIVE | MS_NOATIME;
  919. }
  920. /* 'fill_super()' opens ubi again so we must close it here */
  921. ubi_close_volume(ubi);
  922. ubifs_sb = sb;
  923. return 0;
  924. out_deact:
  925. up_write(&sb->s_umount);
  926. out_close:
  927. ubi_close_volume(ubi);
  928. return err;
  929. }
  930. int __init ubifs_init(void)
  931. {
  932. int err;
  933. BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
  934. /* Make sure node sizes are 8-byte aligned */
  935. BUILD_BUG_ON(UBIFS_CH_SZ & 7);
  936. BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
  937. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
  938. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
  939. BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
  940. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
  941. BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
  942. BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
  943. BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
  944. BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
  945. BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
  946. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
  947. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
  948. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
  949. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
  950. BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
  951. BUILD_BUG_ON(MIN_WRITE_SZ & 7);
  952. /* Check min. node size */
  953. BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
  954. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
  955. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
  956. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
  957. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  958. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  959. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
  960. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
  961. /* Defined node sizes */
  962. BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
  963. BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
  964. BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
  965. BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
  966. /*
  967. * We use 2 bit wide bit-fields to store compression type, which should
  968. * be amended if more compressors are added. The bit-fields are:
  969. * @compr_type in 'struct ubifs_inode', @default_compr in
  970. * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
  971. */
  972. BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
  973. /*
  974. * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
  975. * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
  976. */
  977. if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
  978. ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
  979. " at least 4096 bytes",
  980. (unsigned int)PAGE_CACHE_SIZE);
  981. return -EINVAL;
  982. }
  983. err = -ENOMEM;
  984. err = ubifs_compressors_init();
  985. if (err)
  986. goto out_shrinker;
  987. return 0;
  988. out_shrinker:
  989. return err;
  990. }
  991. /*
  992. * ubifsmount...
  993. */
  994. static struct file_system_type ubifs_fs_type = {
  995. .name = "ubifs",
  996. .owner = THIS_MODULE,
  997. .get_sb = ubifs_get_sb,
  998. };
  999. int ubifs_mount(char *vol_name)
  1000. {
  1001. int flags;
  1002. char name[80] = "ubi:";
  1003. void *data;
  1004. struct vfsmount *mnt;
  1005. int ret;
  1006. struct ubifs_info *c;
  1007. /*
  1008. * First unmount if allready mounted
  1009. */
  1010. if (ubifs_sb)
  1011. ubifs_umount(ubifs_sb->s_fs_info);
  1012. INIT_LIST_HEAD(&ubifs_infos);
  1013. INIT_LIST_HEAD(&ubifs_fs_type.fs_supers);
  1014. /*
  1015. * Mount in read-only mode
  1016. */
  1017. flags = MS_RDONLY;
  1018. strcat(name, vol_name);
  1019. data = NULL;
  1020. mnt = NULL;
  1021. ret = ubifs_get_sb(&ubifs_fs_type, flags, name, data, mnt);
  1022. if (ret) {
  1023. printf("Error reading superblock on volume '%s'!\n", name);
  1024. return -1;
  1025. }
  1026. c = ubifs_sb->s_fs_info;
  1027. ubi_close_volume(c->ubi);
  1028. return 0;
  1029. }