super.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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: %d (latest is %d)",
  624. c->fmt_version, UBIFS_FORMAT_VERSION);
  625. ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
  626. ubifs_msg("reserved for root: %llu bytes (%llu KiB)",
  627. c->report_rp_size, c->report_rp_size >> 10);
  628. dbg_msg("compiled on: " __DATE__ " at " __TIME__);
  629. dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
  630. dbg_msg("LEB size: %d bytes (%d KiB)",
  631. c->leb_size, c->leb_size >> 10);
  632. dbg_msg("data journal heads: %d",
  633. c->jhead_cnt - NONDATA_JHEADS_CNT);
  634. dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
  635. "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  636. c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
  637. c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
  638. c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
  639. c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
  640. dbg_msg("big_lpt %d", c->big_lpt);
  641. dbg_msg("log LEBs: %d (%d - %d)",
  642. c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
  643. dbg_msg("LPT area LEBs: %d (%d - %d)",
  644. c->lpt_lebs, c->lpt_first, c->lpt_last);
  645. dbg_msg("orphan area LEBs: %d (%d - %d)",
  646. c->orph_lebs, c->orph_first, c->orph_last);
  647. dbg_msg("main area LEBs: %d (%d - %d)",
  648. c->main_lebs, c->main_first, c->leb_cnt - 1);
  649. dbg_msg("index LEBs: %d", c->lst.idx_lebs);
  650. dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
  651. c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
  652. dbg_msg("key hash type: %d", c->key_hash_type);
  653. dbg_msg("tree fanout: %d", c->fanout);
  654. dbg_msg("reserved GC LEB: %d", c->gc_lnum);
  655. dbg_msg("first main LEB: %d", c->main_first);
  656. dbg_msg("max. znode size %d", c->max_znode_sz);
  657. dbg_msg("max. index node size %d", c->max_idx_node_sz);
  658. dbg_msg("node sizes: data %zu, inode %zu, dentry %zu",
  659. UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
  660. dbg_msg("node sizes: trun %zu, sb %zu, master %zu",
  661. UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
  662. dbg_msg("node sizes: ref %zu, cmt. start %zu, orph %zu",
  663. UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
  664. dbg_msg("max. node sizes: data %zu, inode %zu dentry %zu",
  665. UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
  666. UBIFS_MAX_DENT_NODE_SZ);
  667. dbg_msg("dead watermark: %d", c->dead_wm);
  668. dbg_msg("dark watermark: %d", c->dark_wm);
  669. dbg_msg("LEB overhead: %d", c->leb_overhead);
  670. x = (long long)c->main_lebs * c->dark_wm;
  671. dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
  672. x, x >> 10, x >> 20);
  673. dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
  674. c->max_bud_bytes, c->max_bud_bytes >> 10,
  675. c->max_bud_bytes >> 20);
  676. dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
  677. c->bg_bud_bytes, c->bg_bud_bytes >> 10,
  678. c->bg_bud_bytes >> 20);
  679. dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
  680. c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
  681. dbg_msg("max. seq. number: %llu", c->max_sqnum);
  682. dbg_msg("commit number: %llu", c->cmt_no);
  683. return 0;
  684. out_infos:
  685. spin_lock(&ubifs_infos_lock);
  686. list_del(&c->infos_list);
  687. spin_unlock(&ubifs_infos_lock);
  688. out_orphans:
  689. free_orphans(c);
  690. out_journal:
  691. out_lpt:
  692. ubifs_lpt_free(c, 0);
  693. out_master:
  694. kfree(c->mst_node);
  695. kfree(c->rcvrd_mst_node);
  696. if (c->bgt)
  697. kthread_stop(c->bgt);
  698. kfree(c->cbuf);
  699. out_free:
  700. vfree(c->ileb_buf);
  701. vfree(c->sbuf);
  702. kfree(c->bottom_up_buf);
  703. ubifs_debugging_exit(c);
  704. return err;
  705. }
  706. /**
  707. * ubifs_umount - un-mount UBIFS file-system.
  708. * @c: UBIFS file-system description object
  709. *
  710. * Note, this function is called to free allocated resourced when un-mounting,
  711. * as well as free resources when an error occurred while we were half way
  712. * through mounting (error path cleanup function). So it has to make sure the
  713. * resource was actually allocated before freeing it.
  714. */
  715. static void ubifs_umount(struct ubifs_info *c)
  716. {
  717. dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
  718. c->vi.vol_id);
  719. spin_lock(&ubifs_infos_lock);
  720. list_del(&c->infos_list);
  721. spin_unlock(&ubifs_infos_lock);
  722. if (c->bgt)
  723. kthread_stop(c->bgt);
  724. free_orphans(c);
  725. ubifs_lpt_free(c, 0);
  726. kfree(c->cbuf);
  727. kfree(c->rcvrd_mst_node);
  728. kfree(c->mst_node);
  729. vfree(c->ileb_buf);
  730. vfree(c->sbuf);
  731. kfree(c->bottom_up_buf);
  732. ubifs_debugging_exit(c);
  733. /* Finally free U-Boot's global copy of superblock */
  734. free(ubifs_sb->s_fs_info);
  735. free(ubifs_sb);
  736. }
  737. /**
  738. * open_ubi - parse UBI device name string and open the UBI device.
  739. * @name: UBI volume name
  740. * @mode: UBI volume open mode
  741. *
  742. * There are several ways to specify UBI volumes when mounting UBIFS:
  743. * o ubiX_Y - UBI device number X, volume Y;
  744. * o ubiY - UBI device number 0, volume Y;
  745. * o ubiX:NAME - mount UBI device X, volume with name NAME;
  746. * o ubi:NAME - mount UBI device 0, volume with name NAME.
  747. *
  748. * Alternative '!' separator may be used instead of ':' (because some shells
  749. * like busybox may interpret ':' as an NFS host name separator). This function
  750. * returns ubi volume object in case of success and a negative error code in
  751. * case of failure.
  752. */
  753. static struct ubi_volume_desc *open_ubi(const char *name, int mode)
  754. {
  755. int dev, vol;
  756. char *endptr;
  757. if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
  758. return ERR_PTR(-EINVAL);
  759. /* ubi:NAME method */
  760. if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
  761. return ubi_open_volume_nm(0, name + 4, mode);
  762. if (!isdigit(name[3]))
  763. return ERR_PTR(-EINVAL);
  764. dev = simple_strtoul(name + 3, &endptr, 0);
  765. /* ubiY method */
  766. if (*endptr == '\0')
  767. return ubi_open_volume(0, dev, mode);
  768. /* ubiX_Y method */
  769. if (*endptr == '_' && isdigit(endptr[1])) {
  770. vol = simple_strtoul(endptr + 1, &endptr, 0);
  771. if (*endptr != '\0')
  772. return ERR_PTR(-EINVAL);
  773. return ubi_open_volume(dev, vol, mode);
  774. }
  775. /* ubiX:NAME method */
  776. if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
  777. return ubi_open_volume_nm(dev, ++endptr, mode);
  778. return ERR_PTR(-EINVAL);
  779. }
  780. static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
  781. {
  782. struct ubi_volume_desc *ubi = sb->s_fs_info;
  783. struct ubifs_info *c;
  784. struct inode *root;
  785. int err;
  786. c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
  787. if (!c)
  788. return -ENOMEM;
  789. spin_lock_init(&c->cnt_lock);
  790. spin_lock_init(&c->cs_lock);
  791. spin_lock_init(&c->buds_lock);
  792. spin_lock_init(&c->space_lock);
  793. spin_lock_init(&c->orphan_lock);
  794. init_rwsem(&c->commit_sem);
  795. mutex_init(&c->lp_mutex);
  796. mutex_init(&c->tnc_mutex);
  797. mutex_init(&c->log_mutex);
  798. mutex_init(&c->mst_mutex);
  799. mutex_init(&c->umount_mutex);
  800. init_waitqueue_head(&c->cmt_wq);
  801. c->buds = RB_ROOT;
  802. c->old_idx = RB_ROOT;
  803. c->size_tree = RB_ROOT;
  804. c->orph_tree = RB_ROOT;
  805. INIT_LIST_HEAD(&c->infos_list);
  806. INIT_LIST_HEAD(&c->idx_gc);
  807. INIT_LIST_HEAD(&c->replay_list);
  808. INIT_LIST_HEAD(&c->replay_buds);
  809. INIT_LIST_HEAD(&c->uncat_list);
  810. INIT_LIST_HEAD(&c->empty_list);
  811. INIT_LIST_HEAD(&c->freeable_list);
  812. INIT_LIST_HEAD(&c->frdi_idx_list);
  813. INIT_LIST_HEAD(&c->unclean_leb_list);
  814. INIT_LIST_HEAD(&c->old_buds);
  815. INIT_LIST_HEAD(&c->orph_list);
  816. INIT_LIST_HEAD(&c->orph_new);
  817. c->highest_inum = UBIFS_FIRST_INO;
  818. c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
  819. ubi_get_volume_info(ubi, &c->vi);
  820. ubi_get_device_info(c->vi.ubi_num, &c->di);
  821. /* Re-open the UBI device in read-write mode */
  822. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  823. if (IS_ERR(c->ubi)) {
  824. err = PTR_ERR(c->ubi);
  825. goto out_free;
  826. }
  827. c->vfs_sb = sb;
  828. sb->s_fs_info = c;
  829. sb->s_magic = UBIFS_SUPER_MAGIC;
  830. sb->s_blocksize = UBIFS_BLOCK_SIZE;
  831. sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
  832. sb->s_dev = c->vi.cdev;
  833. sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
  834. if (c->max_inode_sz > MAX_LFS_FILESIZE)
  835. sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
  836. mutex_lock(&c->umount_mutex);
  837. err = mount_ubifs(c);
  838. if (err) {
  839. ubifs_assert(err < 0);
  840. goto out_unlock;
  841. }
  842. /* Read the root inode */
  843. root = ubifs_iget(sb, UBIFS_ROOT_INO);
  844. if (IS_ERR(root)) {
  845. err = PTR_ERR(root);
  846. goto out_umount;
  847. }
  848. sb->s_root = NULL;
  849. mutex_unlock(&c->umount_mutex);
  850. return 0;
  851. out_umount:
  852. ubifs_umount(c);
  853. out_unlock:
  854. mutex_unlock(&c->umount_mutex);
  855. ubi_close_volume(c->ubi);
  856. out_free:
  857. kfree(c);
  858. return err;
  859. }
  860. static int sb_test(struct super_block *sb, void *data)
  861. {
  862. dev_t *dev = data;
  863. return sb->s_dev == *dev;
  864. }
  865. static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
  866. const char *name, void *data, struct vfsmount *mnt)
  867. {
  868. struct ubi_volume_desc *ubi;
  869. struct ubi_volume_info vi;
  870. struct super_block *sb;
  871. int err;
  872. dbg_gen("name %s, flags %#x", name, flags);
  873. /*
  874. * Get UBI device number and volume ID. Mount it read-only so far
  875. * because this might be a new mount point, and UBI allows only one
  876. * read-write user at a time.
  877. */
  878. ubi = open_ubi(name, UBI_READONLY);
  879. if (IS_ERR(ubi)) {
  880. ubifs_err("cannot open \"%s\", error %d",
  881. name, (int)PTR_ERR(ubi));
  882. return PTR_ERR(ubi);
  883. }
  884. ubi_get_volume_info(ubi, &vi);
  885. dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
  886. sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
  887. if (IS_ERR(sb)) {
  888. err = PTR_ERR(sb);
  889. goto out_close;
  890. }
  891. if (sb->s_root) {
  892. /* A new mount point for already mounted UBIFS */
  893. dbg_gen("this ubi volume is already mounted");
  894. if ((flags ^ sb->s_flags) & MS_RDONLY) {
  895. err = -EBUSY;
  896. goto out_deact;
  897. }
  898. } else {
  899. sb->s_flags = flags;
  900. /*
  901. * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
  902. * replaced by 'c'.
  903. */
  904. sb->s_fs_info = ubi;
  905. err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  906. if (err)
  907. goto out_deact;
  908. /* We do not support atime */
  909. sb->s_flags |= MS_ACTIVE | MS_NOATIME;
  910. }
  911. /* 'fill_super()' opens ubi again so we must close it here */
  912. ubi_close_volume(ubi);
  913. ubifs_sb = sb;
  914. return 0;
  915. out_deact:
  916. up_write(&sb->s_umount);
  917. out_close:
  918. ubi_close_volume(ubi);
  919. return err;
  920. }
  921. int __init ubifs_init(void)
  922. {
  923. int err;
  924. BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
  925. /* Make sure node sizes are 8-byte aligned */
  926. BUILD_BUG_ON(UBIFS_CH_SZ & 7);
  927. BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
  928. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
  929. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
  930. BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
  931. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
  932. BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
  933. BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
  934. BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
  935. BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
  936. BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
  937. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
  938. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
  939. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
  940. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
  941. BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
  942. BUILD_BUG_ON(MIN_WRITE_SZ & 7);
  943. /* Check min. node size */
  944. BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
  945. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
  946. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
  947. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
  948. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  949. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  950. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
  951. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
  952. /* Defined node sizes */
  953. BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
  954. BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
  955. BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
  956. BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
  957. /*
  958. * We use 2 bit wide bit-fields to store compression type, which should
  959. * be amended if more compressors are added. The bit-fields are:
  960. * @compr_type in 'struct ubifs_inode', @default_compr in
  961. * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
  962. */
  963. BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
  964. /*
  965. * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
  966. * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
  967. */
  968. if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
  969. ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
  970. " at least 4096 bytes",
  971. (unsigned int)PAGE_CACHE_SIZE);
  972. return -EINVAL;
  973. }
  974. err = -ENOMEM;
  975. err = ubifs_compressors_init();
  976. if (err)
  977. goto out_shrinker;
  978. return 0;
  979. out_shrinker:
  980. return err;
  981. }
  982. /*
  983. * ubifsmount...
  984. */
  985. static struct file_system_type ubifs_fs_type = {
  986. .name = "ubifs",
  987. .owner = THIS_MODULE,
  988. .get_sb = ubifs_get_sb,
  989. };
  990. int ubifs_mount(char *vol_name)
  991. {
  992. int flags;
  993. char name[80] = "ubi:";
  994. void *data;
  995. struct vfsmount *mnt;
  996. int ret;
  997. struct ubifs_info *c;
  998. /*
  999. * First unmount if allready mounted
  1000. */
  1001. if (ubifs_sb)
  1002. ubifs_umount(ubifs_sb->s_fs_info);
  1003. INIT_LIST_HEAD(&ubifs_infos);
  1004. /*
  1005. * Mount in read-only mode
  1006. */
  1007. flags = MS_RDONLY;
  1008. strcat(name, vol_name);
  1009. data = NULL;
  1010. mnt = NULL;
  1011. ret = ubifs_get_sb(&ubifs_fs_type, flags, name, data, mnt);
  1012. if (ret) {
  1013. printf("Error reading superblock on volume '%s'!\n", name);
  1014. return -1;
  1015. }
  1016. c = ubifs_sb->s_fs_info;
  1017. ubi_close_volume(c->ubi);
  1018. return 0;
  1019. }