super.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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 <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <linux/ctype.h>
  31. #include <linux/random.h>
  32. #include <linux/kthread.h>
  33. #include <linux/parser.h>
  34. #include <linux/seq_file.h>
  35. #include <linux/mount.h>
  36. #include "ubifs.h"
  37. /* Slab cache for UBIFS inodes */
  38. struct kmem_cache *ubifs_inode_slab;
  39. /* UBIFS TNC shrinker description */
  40. static struct shrinker ubifs_shrinker_info = {
  41. .shrink = ubifs_shrinker,
  42. .seeks = DEFAULT_SEEKS,
  43. };
  44. /**
  45. * validate_inode - validate inode.
  46. * @c: UBIFS file-system description object
  47. * @inode: the inode to validate
  48. *
  49. * This is a helper function for 'ubifs_iget()' which validates various fields
  50. * of a newly built inode to make sure they contain sane values and prevent
  51. * possible vulnerabilities. Returns zero if the inode is all right and
  52. * a non-zero error code if not.
  53. */
  54. static int validate_inode(struct ubifs_info *c, const struct inode *inode)
  55. {
  56. int err;
  57. const struct ubifs_inode *ui = ubifs_inode(inode);
  58. if (inode->i_size > c->max_inode_sz) {
  59. ubifs_err("inode is too large (%lld)",
  60. (long long)inode->i_size);
  61. return 1;
  62. }
  63. if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
  64. ubifs_err("unknown compression type %d", ui->compr_type);
  65. return 2;
  66. }
  67. if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
  68. return 3;
  69. if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
  70. return 4;
  71. if (ui->xattr && (inode->i_mode & S_IFMT) != S_IFREG)
  72. return 5;
  73. if (!ubifs_compr_present(ui->compr_type)) {
  74. ubifs_warn("inode %lu uses '%s' compression, but it was not "
  75. "compiled in", inode->i_ino,
  76. ubifs_compr_name(ui->compr_type));
  77. }
  78. err = dbg_check_dir_size(c, inode);
  79. return err;
  80. }
  81. struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
  82. {
  83. int err;
  84. union ubifs_key key;
  85. struct ubifs_ino_node *ino;
  86. struct ubifs_info *c = sb->s_fs_info;
  87. struct inode *inode;
  88. struct ubifs_inode *ui;
  89. dbg_gen("inode %lu", inum);
  90. inode = iget_locked(sb, inum);
  91. if (!inode)
  92. return ERR_PTR(-ENOMEM);
  93. if (!(inode->i_state & I_NEW))
  94. return inode;
  95. ui = ubifs_inode(inode);
  96. ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  97. if (!ino) {
  98. err = -ENOMEM;
  99. goto out;
  100. }
  101. ino_key_init(c, &key, inode->i_ino);
  102. err = ubifs_tnc_lookup(c, &key, ino);
  103. if (err)
  104. goto out_ino;
  105. inode->i_flags |= (S_NOCMTIME | S_NOATIME);
  106. inode->i_nlink = le32_to_cpu(ino->nlink);
  107. inode->i_uid = le32_to_cpu(ino->uid);
  108. inode->i_gid = le32_to_cpu(ino->gid);
  109. inode->i_atime.tv_sec = (int64_t)le64_to_cpu(ino->atime_sec);
  110. inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
  111. inode->i_mtime.tv_sec = (int64_t)le64_to_cpu(ino->mtime_sec);
  112. inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
  113. inode->i_ctime.tv_sec = (int64_t)le64_to_cpu(ino->ctime_sec);
  114. inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
  115. inode->i_mode = le32_to_cpu(ino->mode);
  116. inode->i_size = le64_to_cpu(ino->size);
  117. ui->data_len = le32_to_cpu(ino->data_len);
  118. ui->flags = le32_to_cpu(ino->flags);
  119. ui->compr_type = le16_to_cpu(ino->compr_type);
  120. ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
  121. ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt);
  122. ui->xattr_size = le32_to_cpu(ino->xattr_size);
  123. ui->xattr_names = le32_to_cpu(ino->xattr_names);
  124. ui->synced_i_size = ui->ui_size = inode->i_size;
  125. ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
  126. err = validate_inode(c, inode);
  127. if (err)
  128. goto out_invalid;
  129. /* Disable readahead */
  130. inode->i_mapping->backing_dev_info = &c->bdi;
  131. switch (inode->i_mode & S_IFMT) {
  132. case S_IFREG:
  133. inode->i_mapping->a_ops = &ubifs_file_address_operations;
  134. inode->i_op = &ubifs_file_inode_operations;
  135. inode->i_fop = &ubifs_file_operations;
  136. if (ui->xattr) {
  137. ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
  138. if (!ui->data) {
  139. err = -ENOMEM;
  140. goto out_ino;
  141. }
  142. memcpy(ui->data, ino->data, ui->data_len);
  143. ((char *)ui->data)[ui->data_len] = '\0';
  144. } else if (ui->data_len != 0) {
  145. err = 10;
  146. goto out_invalid;
  147. }
  148. break;
  149. case S_IFDIR:
  150. inode->i_op = &ubifs_dir_inode_operations;
  151. inode->i_fop = &ubifs_dir_operations;
  152. if (ui->data_len != 0) {
  153. err = 11;
  154. goto out_invalid;
  155. }
  156. break;
  157. case S_IFLNK:
  158. inode->i_op = &ubifs_symlink_inode_operations;
  159. if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
  160. err = 12;
  161. goto out_invalid;
  162. }
  163. ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
  164. if (!ui->data) {
  165. err = -ENOMEM;
  166. goto out_ino;
  167. }
  168. memcpy(ui->data, ino->data, ui->data_len);
  169. ((char *)ui->data)[ui->data_len] = '\0';
  170. break;
  171. case S_IFBLK:
  172. case S_IFCHR:
  173. {
  174. dev_t rdev;
  175. union ubifs_dev_desc *dev;
  176. ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  177. if (!ui->data) {
  178. err = -ENOMEM;
  179. goto out_ino;
  180. }
  181. dev = (union ubifs_dev_desc *)ino->data;
  182. if (ui->data_len == sizeof(dev->new))
  183. rdev = new_decode_dev(le32_to_cpu(dev->new));
  184. else if (ui->data_len == sizeof(dev->huge))
  185. rdev = huge_decode_dev(le64_to_cpu(dev->huge));
  186. else {
  187. err = 13;
  188. goto out_invalid;
  189. }
  190. memcpy(ui->data, ino->data, ui->data_len);
  191. inode->i_op = &ubifs_file_inode_operations;
  192. init_special_inode(inode, inode->i_mode, rdev);
  193. break;
  194. }
  195. case S_IFSOCK:
  196. case S_IFIFO:
  197. inode->i_op = &ubifs_file_inode_operations;
  198. init_special_inode(inode, inode->i_mode, 0);
  199. if (ui->data_len != 0) {
  200. err = 14;
  201. goto out_invalid;
  202. }
  203. break;
  204. default:
  205. err = 15;
  206. goto out_invalid;
  207. }
  208. kfree(ino);
  209. ubifs_set_inode_flags(inode);
  210. unlock_new_inode(inode);
  211. return inode;
  212. out_invalid:
  213. ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
  214. dbg_dump_node(c, ino);
  215. dbg_dump_inode(c, inode);
  216. err = -EINVAL;
  217. out_ino:
  218. kfree(ino);
  219. out:
  220. ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
  221. iget_failed(inode);
  222. return ERR_PTR(err);
  223. }
  224. static struct inode *ubifs_alloc_inode(struct super_block *sb)
  225. {
  226. struct ubifs_inode *ui;
  227. ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
  228. if (!ui)
  229. return NULL;
  230. memset((void *)ui + sizeof(struct inode), 0,
  231. sizeof(struct ubifs_inode) - sizeof(struct inode));
  232. mutex_init(&ui->ui_mutex);
  233. spin_lock_init(&ui->ui_lock);
  234. return &ui->vfs_inode;
  235. };
  236. static void ubifs_destroy_inode(struct inode *inode)
  237. {
  238. struct ubifs_inode *ui = ubifs_inode(inode);
  239. kfree(ui->data);
  240. kmem_cache_free(ubifs_inode_slab, inode);
  241. }
  242. /*
  243. * Note, Linux write-back code calls this without 'i_mutex'.
  244. */
  245. static int ubifs_write_inode(struct inode *inode, int wait)
  246. {
  247. int err;
  248. struct ubifs_info *c = inode->i_sb->s_fs_info;
  249. struct ubifs_inode *ui = ubifs_inode(inode);
  250. ubifs_assert(!ui->xattr);
  251. if (is_bad_inode(inode))
  252. return 0;
  253. mutex_lock(&ui->ui_mutex);
  254. /*
  255. * Due to races between write-back forced by budgeting
  256. * (see 'sync_some_inodes()') and pdflush write-back, the inode may
  257. * have already been synchronized, do not do this again. This might
  258. * also happen if it was synchronized in an VFS operation, e.g.
  259. * 'ubifs_link()'.
  260. */
  261. if (!ui->dirty) {
  262. mutex_unlock(&ui->ui_mutex);
  263. return 0;
  264. }
  265. dbg_gen("inode %lu", inode->i_ino);
  266. err = ubifs_jnl_write_inode(c, inode, 0);
  267. if (err)
  268. ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
  269. ui->dirty = 0;
  270. mutex_unlock(&ui->ui_mutex);
  271. ubifs_release_dirty_inode_budget(c, ui);
  272. return err;
  273. }
  274. static void ubifs_delete_inode(struct inode *inode)
  275. {
  276. int err;
  277. struct ubifs_info *c = inode->i_sb->s_fs_info;
  278. if (ubifs_inode(inode)->xattr)
  279. /*
  280. * Extended attribute inode deletions are fully handled in
  281. * 'ubifs_removexattr()'. These inodes are special and have
  282. * limited usage, so there is nothing to do here.
  283. */
  284. goto out;
  285. dbg_gen("inode %lu", inode->i_ino);
  286. ubifs_assert(!atomic_read(&inode->i_count));
  287. ubifs_assert(inode->i_nlink == 0);
  288. truncate_inode_pages(&inode->i_data, 0);
  289. if (is_bad_inode(inode))
  290. goto out;
  291. ubifs_inode(inode)->ui_size = inode->i_size = 0;
  292. err = ubifs_jnl_write_inode(c, inode, 1);
  293. if (err)
  294. /*
  295. * Worst case we have a lost orphan inode wasting space, so a
  296. * simple error message is ok here.
  297. */
  298. ubifs_err("can't write inode %lu, error %d", inode->i_ino, err);
  299. out:
  300. clear_inode(inode);
  301. }
  302. static void ubifs_dirty_inode(struct inode *inode)
  303. {
  304. struct ubifs_inode *ui = ubifs_inode(inode);
  305. ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  306. if (!ui->dirty) {
  307. ui->dirty = 1;
  308. dbg_gen("inode %lu", inode->i_ino);
  309. }
  310. }
  311. static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  312. {
  313. struct ubifs_info *c = dentry->d_sb->s_fs_info;
  314. unsigned long long free;
  315. free = ubifs_budg_get_free_space(c);
  316. dbg_gen("free space %lld bytes (%lld blocks)",
  317. free, free >> UBIFS_BLOCK_SHIFT);
  318. buf->f_type = UBIFS_SUPER_MAGIC;
  319. buf->f_bsize = UBIFS_BLOCK_SIZE;
  320. buf->f_blocks = c->block_cnt;
  321. buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
  322. if (free > c->report_rp_size)
  323. buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
  324. else
  325. buf->f_bavail = 0;
  326. buf->f_files = 0;
  327. buf->f_ffree = 0;
  328. buf->f_namelen = UBIFS_MAX_NLEN;
  329. return 0;
  330. }
  331. static int ubifs_show_options(struct seq_file *s, struct vfsmount *mnt)
  332. {
  333. struct ubifs_info *c = mnt->mnt_sb->s_fs_info;
  334. if (c->mount_opts.unmount_mode == 2)
  335. seq_printf(s, ",fast_unmount");
  336. else if (c->mount_opts.unmount_mode == 1)
  337. seq_printf(s, ",norm_unmount");
  338. return 0;
  339. }
  340. static int ubifs_sync_fs(struct super_block *sb, int wait)
  341. {
  342. struct ubifs_info *c = sb->s_fs_info;
  343. int i, ret = 0, err;
  344. if (c->jheads)
  345. for (i = 0; i < c->jhead_cnt; i++) {
  346. err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  347. if (err && !ret)
  348. ret = err;
  349. }
  350. /*
  351. * We ought to call sync for c->ubi but it does not have one. If it had
  352. * it would in turn call mtd->sync, however mtd operations are
  353. * synchronous anyway, so we don't lose any sleep here.
  354. */
  355. return ret;
  356. }
  357. /**
  358. * init_constants_early - initialize UBIFS constants.
  359. * @c: UBIFS file-system description object
  360. *
  361. * This function initialize UBIFS constants which do not need the superblock to
  362. * be read. It also checks that the UBI volume satisfies basic UBIFS
  363. * requirements. Returns zero in case of success and a negative error code in
  364. * case of failure.
  365. */
  366. static int init_constants_early(struct ubifs_info *c)
  367. {
  368. if (c->vi.corrupted) {
  369. ubifs_warn("UBI volume is corrupted - read-only mode");
  370. c->ro_media = 1;
  371. }
  372. if (c->di.ro_mode) {
  373. ubifs_msg("read-only UBI device");
  374. c->ro_media = 1;
  375. }
  376. if (c->vi.vol_type == UBI_STATIC_VOLUME) {
  377. ubifs_msg("static UBI volume - read-only mode");
  378. c->ro_media = 1;
  379. }
  380. c->leb_cnt = c->vi.size;
  381. c->leb_size = c->vi.usable_leb_size;
  382. c->half_leb_size = c->leb_size / 2;
  383. c->min_io_size = c->di.min_io_size;
  384. c->min_io_shift = fls(c->min_io_size) - 1;
  385. if (c->leb_size < UBIFS_MIN_LEB_SZ) {
  386. ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
  387. c->leb_size, UBIFS_MIN_LEB_SZ);
  388. return -EINVAL;
  389. }
  390. if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
  391. ubifs_err("too few LEBs (%d), min. is %d",
  392. c->leb_cnt, UBIFS_MIN_LEB_CNT);
  393. return -EINVAL;
  394. }
  395. if (!is_power_of_2(c->min_io_size)) {
  396. ubifs_err("bad min. I/O size %d", c->min_io_size);
  397. return -EINVAL;
  398. }
  399. /*
  400. * UBIFS aligns all node to 8-byte boundary, so to make function in
  401. * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
  402. * less than 8.
  403. */
  404. if (c->min_io_size < 8) {
  405. c->min_io_size = 8;
  406. c->min_io_shift = 3;
  407. }
  408. c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
  409. c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
  410. /*
  411. * Initialize node length ranges which are mostly needed for node
  412. * length validation.
  413. */
  414. c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ;
  415. c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ;
  416. c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ;
  417. c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ;
  418. c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
  419. c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ;
  420. c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ;
  421. c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ;
  422. c->ranges[UBIFS_ORPH_NODE].min_len =
  423. UBIFS_ORPH_NODE_SZ + sizeof(__le64);
  424. c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
  425. c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
  426. c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
  427. c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
  428. c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
  429. c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
  430. c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
  431. /*
  432. * Minimum indexing node size is amended later when superblock is
  433. * read and the key length is known.
  434. */
  435. c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
  436. /*
  437. * Maximum indexing node size is amended later when superblock is
  438. * read and the fanout is known.
  439. */
  440. c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
  441. /*
  442. * Initialize dead and dark LEB space watermarks.
  443. *
  444. * Dead space is the space which cannot be used. Its watermark is
  445. * equivalent to min. I/O unit or minimum node size if it is greater
  446. * then min. I/O unit.
  447. *
  448. * Dark space is the space which might be used, or might not, depending
  449. * on which node should be written to the LEB. Its watermark is
  450. * equivalent to maximum UBIFS node size.
  451. */
  452. c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
  453. c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
  454. return 0;
  455. }
  456. /**
  457. * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
  458. * @c: UBIFS file-system description object
  459. * @lnum: LEB the write-buffer was synchronized to
  460. * @free: how many free bytes left in this LEB
  461. * @pad: how many bytes were padded
  462. *
  463. * This is a callback function which is called by the I/O unit when the
  464. * write-buffer is synchronized. We need this to correctly maintain space
  465. * accounting in bud logical eraseblocks. This function returns zero in case of
  466. * success and a negative error code in case of failure.
  467. *
  468. * This function actually belongs to the journal, but we keep it here because
  469. * we want to keep it static.
  470. */
  471. static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
  472. {
  473. return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
  474. }
  475. /*
  476. * init_constants_late - initialize UBIFS constants.
  477. * @c: UBIFS file-system description object
  478. *
  479. * This is a helper function which initializes various UBIFS constants after
  480. * the superblock has been read. It also checks various UBIFS parameters and
  481. * makes sure they are all right. Returns zero in case of success and a
  482. * negative error code in case of failure.
  483. */
  484. static int init_constants_late(struct ubifs_info *c)
  485. {
  486. int tmp, err;
  487. uint64_t tmp64;
  488. c->main_bytes = (long long)c->main_lebs * c->leb_size;
  489. c->max_znode_sz = sizeof(struct ubifs_znode) +
  490. c->fanout * sizeof(struct ubifs_zbranch);
  491. tmp = ubifs_idx_node_sz(c, 1);
  492. c->ranges[UBIFS_IDX_NODE].min_len = tmp;
  493. c->min_idx_node_sz = ALIGN(tmp, 8);
  494. tmp = ubifs_idx_node_sz(c, c->fanout);
  495. c->ranges[UBIFS_IDX_NODE].max_len = tmp;
  496. c->max_idx_node_sz = ALIGN(tmp, 8);
  497. /* Make sure LEB size is large enough to fit full commit */
  498. tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
  499. tmp = ALIGN(tmp, c->min_io_size);
  500. if (tmp > c->leb_size) {
  501. dbg_err("too small LEB size %d, at least %d needed",
  502. c->leb_size, tmp);
  503. return -EINVAL;
  504. }
  505. /*
  506. * Make sure that the log is large enough to fit reference nodes for
  507. * all buds plus one reserved LEB.
  508. */
  509. tmp64 = c->max_bud_bytes;
  510. tmp = do_div(tmp64, c->leb_size);
  511. c->max_bud_cnt = tmp64 + !!tmp;
  512. tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
  513. tmp /= c->leb_size;
  514. tmp += 1;
  515. if (c->log_lebs < tmp) {
  516. dbg_err("too small log %d LEBs, required min. %d LEBs",
  517. c->log_lebs, tmp);
  518. return -EINVAL;
  519. }
  520. /*
  521. * When budgeting we assume worst-case scenarios when the pages are not
  522. * be compressed and direntries are of the maximum size.
  523. *
  524. * Note, data, which may be stored in inodes is budgeted separately, so
  525. * it is not included into 'c->inode_budget'.
  526. */
  527. c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
  528. c->inode_budget = UBIFS_INO_NODE_SZ;
  529. c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
  530. /*
  531. * When the amount of flash space used by buds becomes
  532. * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
  533. * The writers are unblocked when the commit is finished. To avoid
  534. * writers to be blocked UBIFS initiates background commit in advance,
  535. * when number of bud bytes becomes above the limit defined below.
  536. */
  537. c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
  538. /*
  539. * Ensure minimum journal size. All the bytes in the journal heads are
  540. * considered to be used, when calculating the current journal usage.
  541. * Consequently, if the journal is too small, UBIFS will treat it as
  542. * always full.
  543. */
  544. tmp64 = (uint64_t)(c->jhead_cnt + 1) * c->leb_size + 1;
  545. if (c->bg_bud_bytes < tmp64)
  546. c->bg_bud_bytes = tmp64;
  547. if (c->max_bud_bytes < tmp64 + c->leb_size)
  548. c->max_bud_bytes = tmp64 + c->leb_size;
  549. err = ubifs_calc_lpt_geom(c);
  550. if (err)
  551. return err;
  552. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  553. /*
  554. * Calculate total amount of FS blocks. This number is not used
  555. * internally because it does not make much sense for UBIFS, but it is
  556. * necessary to report something for the 'statfs()' call.
  557. *
  558. * Subtract the LEB reserved for GC and the LEB which is reserved for
  559. * deletions.
  560. *
  561. * Review 'ubifs_calc_available()' if changing this calculation.
  562. */
  563. tmp64 = c->main_lebs - 2;
  564. tmp64 *= (uint64_t)c->leb_size - c->dark_wm;
  565. tmp64 = ubifs_reported_space(c, tmp64);
  566. c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
  567. return 0;
  568. }
  569. /**
  570. * take_gc_lnum - reserve GC LEB.
  571. * @c: UBIFS file-system description object
  572. *
  573. * This function ensures that the LEB reserved for garbage collection is
  574. * unmapped and is marked as "taken" in lprops. We also have to set free space
  575. * to LEB size and dirty space to zero, because lprops may contain out-of-date
  576. * information if the file-system was un-mounted before it has been committed.
  577. * This function returns zero in case of success and a negative error code in
  578. * case of failure.
  579. */
  580. static int take_gc_lnum(struct ubifs_info *c)
  581. {
  582. int err;
  583. if (c->gc_lnum == -1) {
  584. ubifs_err("no LEB for GC");
  585. return -EINVAL;
  586. }
  587. err = ubifs_leb_unmap(c, c->gc_lnum);
  588. if (err)
  589. return err;
  590. /* And we have to tell lprops that this LEB is taken */
  591. err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
  592. LPROPS_TAKEN, 0, 0);
  593. return err;
  594. }
  595. /**
  596. * alloc_wbufs - allocate write-buffers.
  597. * @c: UBIFS file-system description object
  598. *
  599. * This helper function allocates and initializes UBIFS write-buffers. Returns
  600. * zero in case of success and %-ENOMEM in case of failure.
  601. */
  602. static int alloc_wbufs(struct ubifs_info *c)
  603. {
  604. int i, err;
  605. c->jheads = kzalloc(c->jhead_cnt * sizeof(struct ubifs_jhead),
  606. GFP_KERNEL);
  607. if (!c->jheads)
  608. return -ENOMEM;
  609. /* Initialize journal heads */
  610. for (i = 0; i < c->jhead_cnt; i++) {
  611. INIT_LIST_HEAD(&c->jheads[i].buds_list);
  612. err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
  613. if (err)
  614. return err;
  615. c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
  616. c->jheads[i].wbuf.jhead = i;
  617. }
  618. c->jheads[BASEHD].wbuf.dtype = UBI_SHORTTERM;
  619. /*
  620. * Garbage Collector head likely contains long-term data and
  621. * does not need to be synchronized by timer.
  622. */
  623. c->jheads[GCHD].wbuf.dtype = UBI_LONGTERM;
  624. c->jheads[GCHD].wbuf.timeout = 0;
  625. return 0;
  626. }
  627. /**
  628. * free_wbufs - free write-buffers.
  629. * @c: UBIFS file-system description object
  630. */
  631. static void free_wbufs(struct ubifs_info *c)
  632. {
  633. int i;
  634. if (c->jheads) {
  635. for (i = 0; i < c->jhead_cnt; i++) {
  636. kfree(c->jheads[i].wbuf.buf);
  637. kfree(c->jheads[i].wbuf.inodes);
  638. }
  639. kfree(c->jheads);
  640. c->jheads = NULL;
  641. }
  642. }
  643. /**
  644. * free_orphans - free orphans.
  645. * @c: UBIFS file-system description object
  646. */
  647. static void free_orphans(struct ubifs_info *c)
  648. {
  649. struct ubifs_orphan *orph;
  650. while (c->orph_dnext) {
  651. orph = c->orph_dnext;
  652. c->orph_dnext = orph->dnext;
  653. list_del(&orph->list);
  654. kfree(orph);
  655. }
  656. while (!list_empty(&c->orph_list)) {
  657. orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
  658. list_del(&orph->list);
  659. kfree(orph);
  660. dbg_err("orphan list not empty at unmount");
  661. }
  662. vfree(c->orph_buf);
  663. c->orph_buf = NULL;
  664. }
  665. /**
  666. * free_buds - free per-bud objects.
  667. * @c: UBIFS file-system description object
  668. */
  669. static void free_buds(struct ubifs_info *c)
  670. {
  671. struct rb_node *this = c->buds.rb_node;
  672. struct ubifs_bud *bud;
  673. while (this) {
  674. if (this->rb_left)
  675. this = this->rb_left;
  676. else if (this->rb_right)
  677. this = this->rb_right;
  678. else {
  679. bud = rb_entry(this, struct ubifs_bud, rb);
  680. this = rb_parent(this);
  681. if (this) {
  682. if (this->rb_left == &bud->rb)
  683. this->rb_left = NULL;
  684. else
  685. this->rb_right = NULL;
  686. }
  687. kfree(bud);
  688. }
  689. }
  690. }
  691. /**
  692. * check_volume_empty - check if the UBI volume is empty.
  693. * @c: UBIFS file-system description object
  694. *
  695. * This function checks if the UBIFS volume is empty by looking if its LEBs are
  696. * mapped or not. The result of checking is stored in the @c->empty variable.
  697. * Returns zero in case of success and a negative error code in case of
  698. * failure.
  699. */
  700. static int check_volume_empty(struct ubifs_info *c)
  701. {
  702. int lnum, err;
  703. c->empty = 1;
  704. for (lnum = 0; lnum < c->leb_cnt; lnum++) {
  705. err = ubi_is_mapped(c->ubi, lnum);
  706. if (unlikely(err < 0))
  707. return err;
  708. if (err == 1) {
  709. c->empty = 0;
  710. break;
  711. }
  712. cond_resched();
  713. }
  714. return 0;
  715. }
  716. /*
  717. * UBIFS mount options.
  718. *
  719. * Opt_fast_unmount: do not run a journal commit before un-mounting
  720. * Opt_norm_unmount: run a journal commit before un-mounting
  721. * Opt_err: just end of array marker
  722. */
  723. enum {
  724. Opt_fast_unmount,
  725. Opt_norm_unmount,
  726. Opt_err,
  727. };
  728. static match_table_t tokens = {
  729. {Opt_fast_unmount, "fast_unmount"},
  730. {Opt_norm_unmount, "norm_unmount"},
  731. {Opt_err, NULL},
  732. };
  733. /**
  734. * ubifs_parse_options - parse mount parameters.
  735. * @c: UBIFS file-system description object
  736. * @options: parameters to parse
  737. * @is_remount: non-zero if this is FS re-mount
  738. *
  739. * This function parses UBIFS mount options and returns zero in case success
  740. * and a negative error code in case of failure.
  741. */
  742. static int ubifs_parse_options(struct ubifs_info *c, char *options,
  743. int is_remount)
  744. {
  745. char *p;
  746. substring_t args[MAX_OPT_ARGS];
  747. if (!options)
  748. return 0;
  749. while ((p = strsep(&options, ","))) {
  750. int token;
  751. if (!*p)
  752. continue;
  753. token = match_token(p, tokens, args);
  754. switch (token) {
  755. case Opt_fast_unmount:
  756. c->mount_opts.unmount_mode = 2;
  757. c->fast_unmount = 1;
  758. break;
  759. case Opt_norm_unmount:
  760. c->mount_opts.unmount_mode = 1;
  761. c->fast_unmount = 0;
  762. break;
  763. default:
  764. ubifs_err("unrecognized mount option \"%s\" "
  765. "or missing value", p);
  766. return -EINVAL;
  767. }
  768. }
  769. return 0;
  770. }
  771. /**
  772. * destroy_journal - destroy journal data structures.
  773. * @c: UBIFS file-system description object
  774. *
  775. * This function destroys journal data structures including those that may have
  776. * been created by recovery functions.
  777. */
  778. static void destroy_journal(struct ubifs_info *c)
  779. {
  780. while (!list_empty(&c->unclean_leb_list)) {
  781. struct ubifs_unclean_leb *ucleb;
  782. ucleb = list_entry(c->unclean_leb_list.next,
  783. struct ubifs_unclean_leb, list);
  784. list_del(&ucleb->list);
  785. kfree(ucleb);
  786. }
  787. while (!list_empty(&c->old_buds)) {
  788. struct ubifs_bud *bud;
  789. bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
  790. list_del(&bud->list);
  791. kfree(bud);
  792. }
  793. ubifs_destroy_idx_gc(c);
  794. ubifs_destroy_size_tree(c);
  795. ubifs_tnc_close(c);
  796. free_buds(c);
  797. }
  798. /**
  799. * mount_ubifs - mount UBIFS file-system.
  800. * @c: UBIFS file-system description object
  801. *
  802. * This function mounts UBIFS file system. Returns zero in case of success and
  803. * a negative error code in case of failure.
  804. *
  805. * Note, the function does not de-allocate resources it it fails half way
  806. * through, and the caller has to do this instead.
  807. */
  808. static int mount_ubifs(struct ubifs_info *c)
  809. {
  810. struct super_block *sb = c->vfs_sb;
  811. int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
  812. long long x;
  813. size_t sz;
  814. err = init_constants_early(c);
  815. if (err)
  816. return err;
  817. #ifdef CONFIG_UBIFS_FS_DEBUG
  818. c->dbg_buf = vmalloc(c->leb_size);
  819. if (!c->dbg_buf)
  820. return -ENOMEM;
  821. #endif
  822. err = check_volume_empty(c);
  823. if (err)
  824. goto out_free;
  825. if (c->empty && (mounted_read_only || c->ro_media)) {
  826. /*
  827. * This UBI volume is empty, and read-only, or the file system
  828. * is mounted read-only - we cannot format it.
  829. */
  830. ubifs_err("can't format empty UBI volume: read-only %s",
  831. c->ro_media ? "UBI volume" : "mount");
  832. err = -EROFS;
  833. goto out_free;
  834. }
  835. if (c->ro_media && !mounted_read_only) {
  836. ubifs_err("cannot mount read-write - read-only media");
  837. err = -EROFS;
  838. goto out_free;
  839. }
  840. /*
  841. * The requirement for the buffer is that it should fit indexing B-tree
  842. * height amount of integers. We assume the height if the TNC tree will
  843. * never exceed 64.
  844. */
  845. err = -ENOMEM;
  846. c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
  847. if (!c->bottom_up_buf)
  848. goto out_free;
  849. c->sbuf = vmalloc(c->leb_size);
  850. if (!c->sbuf)
  851. goto out_free;
  852. if (!mounted_read_only) {
  853. c->ileb_buf = vmalloc(c->leb_size);
  854. if (!c->ileb_buf)
  855. goto out_free;
  856. }
  857. err = ubifs_read_superblock(c);
  858. if (err)
  859. goto out_free;
  860. /*
  861. * Make sure the compressor which is set as the default on in the
  862. * superblock was actually compiled in.
  863. */
  864. if (!ubifs_compr_present(c->default_compr)) {
  865. ubifs_warn("'%s' compressor is set by superblock, but not "
  866. "compiled in", ubifs_compr_name(c->default_compr));
  867. c->default_compr = UBIFS_COMPR_NONE;
  868. }
  869. dbg_failure_mode_registration(c);
  870. err = init_constants_late(c);
  871. if (err)
  872. goto out_dereg;
  873. sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
  874. sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
  875. c->cbuf = kmalloc(sz, GFP_NOFS);
  876. if (!c->cbuf) {
  877. err = -ENOMEM;
  878. goto out_dereg;
  879. }
  880. if (!mounted_read_only) {
  881. err = alloc_wbufs(c);
  882. if (err)
  883. goto out_cbuf;
  884. /* Create background thread */
  885. sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num,
  886. c->vi.vol_id);
  887. c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
  888. if (!c->bgt)
  889. c->bgt = ERR_PTR(-EINVAL);
  890. if (IS_ERR(c->bgt)) {
  891. err = PTR_ERR(c->bgt);
  892. c->bgt = NULL;
  893. ubifs_err("cannot spawn \"%s\", error %d",
  894. c->bgt_name, err);
  895. goto out_wbufs;
  896. }
  897. wake_up_process(c->bgt);
  898. }
  899. err = ubifs_read_master(c);
  900. if (err)
  901. goto out_master;
  902. if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
  903. ubifs_msg("recovery needed");
  904. c->need_recovery = 1;
  905. if (!mounted_read_only) {
  906. err = ubifs_recover_inl_heads(c, c->sbuf);
  907. if (err)
  908. goto out_master;
  909. }
  910. } else if (!mounted_read_only) {
  911. /*
  912. * Set the "dirty" flag so that if we reboot uncleanly we
  913. * will notice this immediately on the next mount.
  914. */
  915. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  916. err = ubifs_write_master(c);
  917. if (err)
  918. goto out_master;
  919. }
  920. err = ubifs_lpt_init(c, 1, !mounted_read_only);
  921. if (err)
  922. goto out_lpt;
  923. err = dbg_check_idx_size(c, c->old_idx_sz);
  924. if (err)
  925. goto out_lpt;
  926. err = ubifs_replay_journal(c);
  927. if (err)
  928. goto out_journal;
  929. err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
  930. if (err)
  931. goto out_orphans;
  932. if (!mounted_read_only) {
  933. int lnum;
  934. /* Check for enough free space */
  935. if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
  936. ubifs_err("insufficient available space");
  937. err = -EINVAL;
  938. goto out_orphans;
  939. }
  940. /* Check for enough log space */
  941. lnum = c->lhead_lnum + 1;
  942. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  943. lnum = UBIFS_LOG_LNUM;
  944. if (lnum == c->ltail_lnum) {
  945. err = ubifs_consolidate_log(c);
  946. if (err)
  947. goto out_orphans;
  948. }
  949. if (c->need_recovery) {
  950. err = ubifs_recover_size(c);
  951. if (err)
  952. goto out_orphans;
  953. err = ubifs_rcvry_gc_commit(c);
  954. } else
  955. err = take_gc_lnum(c);
  956. if (err)
  957. goto out_orphans;
  958. err = dbg_check_lprops(c);
  959. if (err)
  960. goto out_orphans;
  961. } else if (c->need_recovery) {
  962. err = ubifs_recover_size(c);
  963. if (err)
  964. goto out_orphans;
  965. }
  966. spin_lock(&ubifs_infos_lock);
  967. list_add_tail(&c->infos_list, &ubifs_infos);
  968. spin_unlock(&ubifs_infos_lock);
  969. if (c->need_recovery) {
  970. if (mounted_read_only)
  971. ubifs_msg("recovery deferred");
  972. else {
  973. c->need_recovery = 0;
  974. ubifs_msg("recovery completed");
  975. }
  976. }
  977. err = dbg_check_filesystem(c);
  978. if (err)
  979. goto out_infos;
  980. ubifs_msg("mounted UBI device %d, volume %d", c->vi.ubi_num,
  981. c->vi.vol_id);
  982. if (mounted_read_only)
  983. ubifs_msg("mounted read-only");
  984. x = (long long)c->main_lebs * c->leb_size;
  985. ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
  986. x, x >> 10, x >> 20, c->main_lebs);
  987. x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
  988. ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
  989. x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
  990. ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
  991. ubifs_msg("media format %d, latest format %d",
  992. c->fmt_version, UBIFS_FORMAT_VERSION);
  993. dbg_msg("compiled on: " __DATE__ " at " __TIME__);
  994. dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
  995. dbg_msg("LEB size: %d bytes (%d KiB)",
  996. c->leb_size, c->leb_size / 1024);
  997. dbg_msg("data journal heads: %d",
  998. c->jhead_cnt - NONDATA_JHEADS_CNT);
  999. dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
  1000. "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  1001. c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
  1002. c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
  1003. c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
  1004. c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
  1005. dbg_msg("fast unmount: %d", c->fast_unmount);
  1006. dbg_msg("big_lpt %d", c->big_lpt);
  1007. dbg_msg("log LEBs: %d (%d - %d)",
  1008. c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
  1009. dbg_msg("LPT area LEBs: %d (%d - %d)",
  1010. c->lpt_lebs, c->lpt_first, c->lpt_last);
  1011. dbg_msg("orphan area LEBs: %d (%d - %d)",
  1012. c->orph_lebs, c->orph_first, c->orph_last);
  1013. dbg_msg("main area LEBs: %d (%d - %d)",
  1014. c->main_lebs, c->main_first, c->leb_cnt - 1);
  1015. dbg_msg("index LEBs: %d", c->lst.idx_lebs);
  1016. dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
  1017. c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
  1018. dbg_msg("key hash type: %d", c->key_hash_type);
  1019. dbg_msg("tree fanout: %d", c->fanout);
  1020. dbg_msg("reserved GC LEB: %d", c->gc_lnum);
  1021. dbg_msg("first main LEB: %d", c->main_first);
  1022. dbg_msg("dead watermark: %d", c->dead_wm);
  1023. dbg_msg("dark watermark: %d", c->dark_wm);
  1024. x = (long long)c->main_lebs * c->dark_wm;
  1025. dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
  1026. x, x >> 10, x >> 20);
  1027. dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
  1028. c->max_bud_bytes, c->max_bud_bytes >> 10,
  1029. c->max_bud_bytes >> 20);
  1030. dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
  1031. c->bg_bud_bytes, c->bg_bud_bytes >> 10,
  1032. c->bg_bud_bytes >> 20);
  1033. dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
  1034. c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
  1035. dbg_msg("max. seq. number: %llu", c->max_sqnum);
  1036. dbg_msg("commit number: %llu", c->cmt_no);
  1037. return 0;
  1038. out_infos:
  1039. spin_lock(&ubifs_infos_lock);
  1040. list_del(&c->infos_list);
  1041. spin_unlock(&ubifs_infos_lock);
  1042. out_orphans:
  1043. free_orphans(c);
  1044. out_journal:
  1045. destroy_journal(c);
  1046. out_lpt:
  1047. ubifs_lpt_free(c, 0);
  1048. out_master:
  1049. kfree(c->mst_node);
  1050. kfree(c->rcvrd_mst_node);
  1051. if (c->bgt)
  1052. kthread_stop(c->bgt);
  1053. out_wbufs:
  1054. free_wbufs(c);
  1055. out_cbuf:
  1056. kfree(c->cbuf);
  1057. out_dereg:
  1058. dbg_failure_mode_deregistration(c);
  1059. out_free:
  1060. vfree(c->ileb_buf);
  1061. vfree(c->sbuf);
  1062. kfree(c->bottom_up_buf);
  1063. UBIFS_DBG(vfree(c->dbg_buf));
  1064. return err;
  1065. }
  1066. /**
  1067. * ubifs_umount - un-mount UBIFS file-system.
  1068. * @c: UBIFS file-system description object
  1069. *
  1070. * Note, this function is called to free allocated resourced when un-mounting,
  1071. * as well as free resources when an error occurred while we were half way
  1072. * through mounting (error path cleanup function). So it has to make sure the
  1073. * resource was actually allocated before freeing it.
  1074. */
  1075. static void ubifs_umount(struct ubifs_info *c)
  1076. {
  1077. dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
  1078. c->vi.vol_id);
  1079. spin_lock(&ubifs_infos_lock);
  1080. list_del(&c->infos_list);
  1081. spin_unlock(&ubifs_infos_lock);
  1082. if (c->bgt)
  1083. kthread_stop(c->bgt);
  1084. destroy_journal(c);
  1085. free_wbufs(c);
  1086. free_orphans(c);
  1087. ubifs_lpt_free(c, 0);
  1088. kfree(c->cbuf);
  1089. kfree(c->rcvrd_mst_node);
  1090. kfree(c->mst_node);
  1091. vfree(c->sbuf);
  1092. kfree(c->bottom_up_buf);
  1093. UBIFS_DBG(vfree(c->dbg_buf));
  1094. vfree(c->ileb_buf);
  1095. dbg_failure_mode_deregistration(c);
  1096. }
  1097. /**
  1098. * ubifs_remount_rw - re-mount in read-write mode.
  1099. * @c: UBIFS file-system description object
  1100. *
  1101. * UBIFS avoids allocating many unnecessary resources when mounted in read-only
  1102. * mode. This function allocates the needed resources and re-mounts UBIFS in
  1103. * read-write mode.
  1104. */
  1105. static int ubifs_remount_rw(struct ubifs_info *c)
  1106. {
  1107. int err, lnum;
  1108. if (c->ro_media)
  1109. return -EINVAL;
  1110. mutex_lock(&c->umount_mutex);
  1111. c->remounting_rw = 1;
  1112. /* Check for enough free space */
  1113. if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
  1114. ubifs_err("insufficient available space");
  1115. err = -EINVAL;
  1116. goto out;
  1117. }
  1118. if (c->old_leb_cnt != c->leb_cnt) {
  1119. struct ubifs_sb_node *sup;
  1120. sup = ubifs_read_sb_node(c);
  1121. if (IS_ERR(sup)) {
  1122. err = PTR_ERR(sup);
  1123. goto out;
  1124. }
  1125. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  1126. err = ubifs_write_sb_node(c, sup);
  1127. if (err)
  1128. goto out;
  1129. }
  1130. if (c->need_recovery) {
  1131. ubifs_msg("completing deferred recovery");
  1132. err = ubifs_write_rcvrd_mst_node(c);
  1133. if (err)
  1134. goto out;
  1135. err = ubifs_recover_size(c);
  1136. if (err)
  1137. goto out;
  1138. err = ubifs_clean_lebs(c, c->sbuf);
  1139. if (err)
  1140. goto out;
  1141. err = ubifs_recover_inl_heads(c, c->sbuf);
  1142. if (err)
  1143. goto out;
  1144. }
  1145. if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
  1146. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  1147. err = ubifs_write_master(c);
  1148. if (err)
  1149. goto out;
  1150. }
  1151. c->ileb_buf = vmalloc(c->leb_size);
  1152. if (!c->ileb_buf) {
  1153. err = -ENOMEM;
  1154. goto out;
  1155. }
  1156. err = ubifs_lpt_init(c, 0, 1);
  1157. if (err)
  1158. goto out;
  1159. err = alloc_wbufs(c);
  1160. if (err)
  1161. goto out;
  1162. ubifs_create_buds_lists(c);
  1163. /* Create background thread */
  1164. c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
  1165. if (!c->bgt)
  1166. c->bgt = ERR_PTR(-EINVAL);
  1167. if (IS_ERR(c->bgt)) {
  1168. err = PTR_ERR(c->bgt);
  1169. c->bgt = NULL;
  1170. ubifs_err("cannot spawn \"%s\", error %d",
  1171. c->bgt_name, err);
  1172. return err;
  1173. }
  1174. wake_up_process(c->bgt);
  1175. c->orph_buf = vmalloc(c->leb_size);
  1176. if (!c->orph_buf)
  1177. return -ENOMEM;
  1178. /* Check for enough log space */
  1179. lnum = c->lhead_lnum + 1;
  1180. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  1181. lnum = UBIFS_LOG_LNUM;
  1182. if (lnum == c->ltail_lnum) {
  1183. err = ubifs_consolidate_log(c);
  1184. if (err)
  1185. goto out;
  1186. }
  1187. if (c->need_recovery)
  1188. err = ubifs_rcvry_gc_commit(c);
  1189. else
  1190. err = take_gc_lnum(c);
  1191. if (err)
  1192. goto out;
  1193. if (c->need_recovery) {
  1194. c->need_recovery = 0;
  1195. ubifs_msg("deferred recovery completed");
  1196. }
  1197. dbg_gen("re-mounted read-write");
  1198. c->vfs_sb->s_flags &= ~MS_RDONLY;
  1199. c->remounting_rw = 0;
  1200. mutex_unlock(&c->umount_mutex);
  1201. return 0;
  1202. out:
  1203. vfree(c->orph_buf);
  1204. c->orph_buf = NULL;
  1205. if (c->bgt) {
  1206. kthread_stop(c->bgt);
  1207. c->bgt = NULL;
  1208. }
  1209. free_wbufs(c);
  1210. vfree(c->ileb_buf);
  1211. c->ileb_buf = NULL;
  1212. ubifs_lpt_free(c, 1);
  1213. c->remounting_rw = 0;
  1214. mutex_unlock(&c->umount_mutex);
  1215. return err;
  1216. }
  1217. /**
  1218. * commit_on_unmount - commit the journal when un-mounting.
  1219. * @c: UBIFS file-system description object
  1220. *
  1221. * This function is called during un-mounting and it commits the journal unless
  1222. * the "fast unmount" mode is enabled. It also avoids committing the journal if
  1223. * it contains too few data.
  1224. *
  1225. * Sometimes recovery requires the journal to be committed at least once, and
  1226. * this function takes care about this.
  1227. */
  1228. static void commit_on_unmount(struct ubifs_info *c)
  1229. {
  1230. if (!c->fast_unmount) {
  1231. long long bud_bytes;
  1232. spin_lock(&c->buds_lock);
  1233. bud_bytes = c->bud_bytes;
  1234. spin_unlock(&c->buds_lock);
  1235. if (bud_bytes > c->leb_size)
  1236. ubifs_run_commit(c);
  1237. }
  1238. }
  1239. /**
  1240. * ubifs_remount_ro - re-mount in read-only mode.
  1241. * @c: UBIFS file-system description object
  1242. *
  1243. * We rely on VFS to have stopped writing. Possibly the background thread could
  1244. * be running a commit, however kthread_stop will wait in that case.
  1245. */
  1246. static void ubifs_remount_ro(struct ubifs_info *c)
  1247. {
  1248. int i, err;
  1249. ubifs_assert(!c->need_recovery);
  1250. commit_on_unmount(c);
  1251. mutex_lock(&c->umount_mutex);
  1252. if (c->bgt) {
  1253. kthread_stop(c->bgt);
  1254. c->bgt = NULL;
  1255. }
  1256. for (i = 0; i < c->jhead_cnt; i++) {
  1257. ubifs_wbuf_sync(&c->jheads[i].wbuf);
  1258. del_timer_sync(&c->jheads[i].wbuf.timer);
  1259. }
  1260. if (!c->ro_media) {
  1261. c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  1262. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  1263. c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  1264. err = ubifs_write_master(c);
  1265. if (err)
  1266. ubifs_ro_mode(c, err);
  1267. }
  1268. ubifs_destroy_idx_gc(c);
  1269. free_wbufs(c);
  1270. vfree(c->orph_buf);
  1271. c->orph_buf = NULL;
  1272. vfree(c->ileb_buf);
  1273. c->ileb_buf = NULL;
  1274. ubifs_lpt_free(c, 1);
  1275. mutex_unlock(&c->umount_mutex);
  1276. }
  1277. static void ubifs_put_super(struct super_block *sb)
  1278. {
  1279. int i;
  1280. struct ubifs_info *c = sb->s_fs_info;
  1281. ubifs_msg("un-mount UBI device %d, volume %d", c->vi.ubi_num,
  1282. c->vi.vol_id);
  1283. /*
  1284. * The following asserts are only valid if there has not been a failure
  1285. * of the media. For example, there will be dirty inodes if we failed
  1286. * to write them back because of I/O errors.
  1287. */
  1288. ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0);
  1289. ubifs_assert(c->budg_idx_growth == 0);
  1290. ubifs_assert(c->budg_data_growth == 0);
  1291. /*
  1292. * The 'c->umount_lock' prevents races between UBIFS memory shrinker
  1293. * and file system un-mount. Namely, it prevents the shrinker from
  1294. * picking this superblock for shrinking - it will be just skipped if
  1295. * the mutex is locked.
  1296. */
  1297. mutex_lock(&c->umount_mutex);
  1298. if (!(c->vfs_sb->s_flags & MS_RDONLY)) {
  1299. /*
  1300. * First of all kill the background thread to make sure it does
  1301. * not interfere with un-mounting and freeing resources.
  1302. */
  1303. if (c->bgt) {
  1304. kthread_stop(c->bgt);
  1305. c->bgt = NULL;
  1306. }
  1307. /* Synchronize write-buffers */
  1308. if (c->jheads)
  1309. for (i = 0; i < c->jhead_cnt; i++) {
  1310. ubifs_wbuf_sync(&c->jheads[i].wbuf);
  1311. del_timer_sync(&c->jheads[i].wbuf.timer);
  1312. }
  1313. /*
  1314. * On fatal errors c->ro_media is set to 1, in which case we do
  1315. * not write the master node.
  1316. */
  1317. if (!c->ro_media) {
  1318. /*
  1319. * We are being cleanly unmounted which means the
  1320. * orphans were killed - indicate this in the master
  1321. * node. Also save the reserved GC LEB number.
  1322. */
  1323. int err;
  1324. c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  1325. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  1326. c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  1327. err = ubifs_write_master(c);
  1328. if (err)
  1329. /*
  1330. * Recovery will attempt to fix the master area
  1331. * next mount, so we just print a message and
  1332. * continue to unmount normally.
  1333. */
  1334. ubifs_err("failed to write master node, "
  1335. "error %d", err);
  1336. }
  1337. }
  1338. ubifs_umount(c);
  1339. bdi_destroy(&c->bdi);
  1340. ubi_close_volume(c->ubi);
  1341. mutex_unlock(&c->umount_mutex);
  1342. kfree(c);
  1343. }
  1344. static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
  1345. {
  1346. int err;
  1347. struct ubifs_info *c = sb->s_fs_info;
  1348. dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
  1349. err = ubifs_parse_options(c, data, 1);
  1350. if (err) {
  1351. ubifs_err("invalid or unknown remount parameter");
  1352. return err;
  1353. }
  1354. if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
  1355. err = ubifs_remount_rw(c);
  1356. if (err)
  1357. return err;
  1358. } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
  1359. ubifs_remount_ro(c);
  1360. return 0;
  1361. }
  1362. struct super_operations ubifs_super_operations = {
  1363. .alloc_inode = ubifs_alloc_inode,
  1364. .destroy_inode = ubifs_destroy_inode,
  1365. .put_super = ubifs_put_super,
  1366. .write_inode = ubifs_write_inode,
  1367. .delete_inode = ubifs_delete_inode,
  1368. .statfs = ubifs_statfs,
  1369. .dirty_inode = ubifs_dirty_inode,
  1370. .remount_fs = ubifs_remount_fs,
  1371. .show_options = ubifs_show_options,
  1372. .sync_fs = ubifs_sync_fs,
  1373. };
  1374. /**
  1375. * open_ubi - parse UBI device name string and open the UBI device.
  1376. * @name: UBI volume name
  1377. * @mode: UBI volume open mode
  1378. *
  1379. * There are several ways to specify UBI volumes when mounting UBIFS:
  1380. * o ubiX_Y - UBI device number X, volume Y;
  1381. * o ubiY - UBI device number 0, volume Y;
  1382. * o ubiX:NAME - mount UBI device X, volume with name NAME;
  1383. * o ubi:NAME - mount UBI device 0, volume with name NAME.
  1384. *
  1385. * Alternative '!' separator may be used instead of ':' (because some shells
  1386. * like busybox may interpret ':' as an NFS host name separator). This function
  1387. * returns ubi volume object in case of success and a negative error code in
  1388. * case of failure.
  1389. */
  1390. static struct ubi_volume_desc *open_ubi(const char *name, int mode)
  1391. {
  1392. int dev, vol;
  1393. char *endptr;
  1394. if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
  1395. return ERR_PTR(-EINVAL);
  1396. /* ubi:NAME method */
  1397. if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
  1398. return ubi_open_volume_nm(0, name + 4, mode);
  1399. if (!isdigit(name[3]))
  1400. return ERR_PTR(-EINVAL);
  1401. dev = simple_strtoul(name + 3, &endptr, 0);
  1402. /* ubiY method */
  1403. if (*endptr == '\0')
  1404. return ubi_open_volume(0, dev, mode);
  1405. /* ubiX_Y method */
  1406. if (*endptr == '_' && isdigit(endptr[1])) {
  1407. vol = simple_strtoul(endptr + 1, &endptr, 0);
  1408. if (*endptr != '\0')
  1409. return ERR_PTR(-EINVAL);
  1410. return ubi_open_volume(dev, vol, mode);
  1411. }
  1412. /* ubiX:NAME method */
  1413. if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
  1414. return ubi_open_volume_nm(dev, ++endptr, mode);
  1415. return ERR_PTR(-EINVAL);
  1416. }
  1417. static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
  1418. {
  1419. struct ubi_volume_desc *ubi = sb->s_fs_info;
  1420. struct ubifs_info *c;
  1421. struct inode *root;
  1422. int err;
  1423. c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
  1424. if (!c)
  1425. return -ENOMEM;
  1426. spin_lock_init(&c->cnt_lock);
  1427. spin_lock_init(&c->cs_lock);
  1428. spin_lock_init(&c->buds_lock);
  1429. spin_lock_init(&c->space_lock);
  1430. spin_lock_init(&c->orphan_lock);
  1431. init_rwsem(&c->commit_sem);
  1432. mutex_init(&c->lp_mutex);
  1433. mutex_init(&c->tnc_mutex);
  1434. mutex_init(&c->log_mutex);
  1435. mutex_init(&c->mst_mutex);
  1436. mutex_init(&c->umount_mutex);
  1437. init_waitqueue_head(&c->cmt_wq);
  1438. c->buds = RB_ROOT;
  1439. c->old_idx = RB_ROOT;
  1440. c->size_tree = RB_ROOT;
  1441. c->orph_tree = RB_ROOT;
  1442. INIT_LIST_HEAD(&c->infos_list);
  1443. INIT_LIST_HEAD(&c->idx_gc);
  1444. INIT_LIST_HEAD(&c->replay_list);
  1445. INIT_LIST_HEAD(&c->replay_buds);
  1446. INIT_LIST_HEAD(&c->uncat_list);
  1447. INIT_LIST_HEAD(&c->empty_list);
  1448. INIT_LIST_HEAD(&c->freeable_list);
  1449. INIT_LIST_HEAD(&c->frdi_idx_list);
  1450. INIT_LIST_HEAD(&c->unclean_leb_list);
  1451. INIT_LIST_HEAD(&c->old_buds);
  1452. INIT_LIST_HEAD(&c->orph_list);
  1453. INIT_LIST_HEAD(&c->orph_new);
  1454. c->highest_inum = UBIFS_FIRST_INO;
  1455. get_random_bytes(&c->vfs_gen, sizeof(int));
  1456. c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
  1457. ubi_get_volume_info(ubi, &c->vi);
  1458. ubi_get_device_info(c->vi.ubi_num, &c->di);
  1459. /* Re-open the UBI device in read-write mode */
  1460. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
  1461. if (IS_ERR(c->ubi)) {
  1462. err = PTR_ERR(c->ubi);
  1463. goto out_free;
  1464. }
  1465. /*
  1466. * UBIFS provids 'backing_dev_info' in order to disable readahead. For
  1467. * UBIFS, I/O is not deferred, it is done immediately in readpage,
  1468. * which means the user would have to wait not just for their own I/O
  1469. * but the readahead I/O as well i.e. completely pointless.
  1470. *
  1471. * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
  1472. */
  1473. c->bdi.capabilities = BDI_CAP_MAP_COPY;
  1474. c->bdi.unplug_io_fn = default_unplug_io_fn;
  1475. err = bdi_init(&c->bdi);
  1476. if (err)
  1477. goto out_close;
  1478. err = ubifs_parse_options(c, data, 0);
  1479. if (err)
  1480. goto out_bdi;
  1481. c->vfs_sb = sb;
  1482. sb->s_fs_info = c;
  1483. sb->s_magic = UBIFS_SUPER_MAGIC;
  1484. sb->s_blocksize = UBIFS_BLOCK_SIZE;
  1485. sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
  1486. sb->s_dev = c->vi.cdev;
  1487. sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
  1488. if (c->max_inode_sz > MAX_LFS_FILESIZE)
  1489. sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
  1490. sb->s_op = &ubifs_super_operations;
  1491. mutex_lock(&c->umount_mutex);
  1492. err = mount_ubifs(c);
  1493. if (err) {
  1494. ubifs_assert(err < 0);
  1495. goto out_unlock;
  1496. }
  1497. /* Read the root inode */
  1498. root = ubifs_iget(sb, UBIFS_ROOT_INO);
  1499. if (IS_ERR(root)) {
  1500. err = PTR_ERR(root);
  1501. goto out_umount;
  1502. }
  1503. sb->s_root = d_alloc_root(root);
  1504. if (!sb->s_root)
  1505. goto out_iput;
  1506. mutex_unlock(&c->umount_mutex);
  1507. return 0;
  1508. out_iput:
  1509. iput(root);
  1510. out_umount:
  1511. ubifs_umount(c);
  1512. out_unlock:
  1513. mutex_unlock(&c->umount_mutex);
  1514. out_bdi:
  1515. bdi_destroy(&c->bdi);
  1516. out_close:
  1517. ubi_close_volume(c->ubi);
  1518. out_free:
  1519. kfree(c);
  1520. return err;
  1521. }
  1522. static int sb_test(struct super_block *sb, void *data)
  1523. {
  1524. dev_t *dev = data;
  1525. return sb->s_dev == *dev;
  1526. }
  1527. static int sb_set(struct super_block *sb, void *data)
  1528. {
  1529. dev_t *dev = data;
  1530. sb->s_dev = *dev;
  1531. return 0;
  1532. }
  1533. static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
  1534. const char *name, void *data, struct vfsmount *mnt)
  1535. {
  1536. struct ubi_volume_desc *ubi;
  1537. struct ubi_volume_info vi;
  1538. struct super_block *sb;
  1539. int err;
  1540. dbg_gen("name %s, flags %#x", name, flags);
  1541. /*
  1542. * Get UBI device number and volume ID. Mount it read-only so far
  1543. * because this might be a new mount point, and UBI allows only one
  1544. * read-write user at a time.
  1545. */
  1546. ubi = open_ubi(name, UBI_READONLY);
  1547. if (IS_ERR(ubi)) {
  1548. ubifs_err("cannot open \"%s\", error %d",
  1549. name, (int)PTR_ERR(ubi));
  1550. return PTR_ERR(ubi);
  1551. }
  1552. ubi_get_volume_info(ubi, &vi);
  1553. dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
  1554. sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
  1555. if (IS_ERR(sb)) {
  1556. err = PTR_ERR(sb);
  1557. goto out_close;
  1558. }
  1559. if (sb->s_root) {
  1560. /* A new mount point for already mounted UBIFS */
  1561. dbg_gen("this ubi volume is already mounted");
  1562. if ((flags ^ sb->s_flags) & MS_RDONLY) {
  1563. err = -EBUSY;
  1564. goto out_deact;
  1565. }
  1566. } else {
  1567. sb->s_flags = flags;
  1568. /*
  1569. * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
  1570. * replaced by 'c'.
  1571. */
  1572. sb->s_fs_info = ubi;
  1573. err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  1574. if (err)
  1575. goto out_deact;
  1576. /* We do not support atime */
  1577. sb->s_flags |= MS_ACTIVE | MS_NOATIME;
  1578. }
  1579. /* 'fill_super()' opens ubi again so we must close it here */
  1580. ubi_close_volume(ubi);
  1581. return simple_set_mnt(mnt, sb);
  1582. out_deact:
  1583. up_write(&sb->s_umount);
  1584. deactivate_super(sb);
  1585. out_close:
  1586. ubi_close_volume(ubi);
  1587. return err;
  1588. }
  1589. static void ubifs_kill_sb(struct super_block *sb)
  1590. {
  1591. struct ubifs_info *c = sb->s_fs_info;
  1592. /*
  1593. * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
  1594. * in order to be outside BKL.
  1595. */
  1596. if (sb->s_root && !(sb->s_flags & MS_RDONLY))
  1597. commit_on_unmount(c);
  1598. /* The un-mount routine is actually done in put_super() */
  1599. generic_shutdown_super(sb);
  1600. }
  1601. static struct file_system_type ubifs_fs_type = {
  1602. .name = "ubifs",
  1603. .owner = THIS_MODULE,
  1604. .get_sb = ubifs_get_sb,
  1605. .kill_sb = ubifs_kill_sb
  1606. };
  1607. /*
  1608. * Inode slab cache constructor.
  1609. */
  1610. static void inode_slab_ctor(struct kmem_cache *cachep, void *obj)
  1611. {
  1612. struct ubifs_inode *ui = obj;
  1613. inode_init_once(&ui->vfs_inode);
  1614. }
  1615. static int __init ubifs_init(void)
  1616. {
  1617. int err;
  1618. BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
  1619. /* Make sure node sizes are 8-byte aligned */
  1620. BUILD_BUG_ON(UBIFS_CH_SZ & 7);
  1621. BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
  1622. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
  1623. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
  1624. BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
  1625. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
  1626. BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
  1627. BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
  1628. BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
  1629. BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
  1630. BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
  1631. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
  1632. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
  1633. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
  1634. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
  1635. BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
  1636. BUILD_BUG_ON(MIN_WRITE_SZ & 7);
  1637. /* Check min. node size */
  1638. BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
  1639. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
  1640. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
  1641. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
  1642. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1643. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1644. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1645. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1646. /* Defined node sizes */
  1647. BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
  1648. BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
  1649. BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
  1650. BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
  1651. /*
  1652. * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
  1653. * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
  1654. */
  1655. if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
  1656. ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
  1657. " at least 4096 bytes",
  1658. (unsigned int)PAGE_CACHE_SIZE);
  1659. return -EINVAL;
  1660. }
  1661. err = register_filesystem(&ubifs_fs_type);
  1662. if (err) {
  1663. ubifs_err("cannot register file system, error %d", err);
  1664. return err;
  1665. }
  1666. err = -ENOMEM;
  1667. ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
  1668. sizeof(struct ubifs_inode), 0,
  1669. SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT,
  1670. &inode_slab_ctor);
  1671. if (!ubifs_inode_slab)
  1672. goto out_reg;
  1673. register_shrinker(&ubifs_shrinker_info);
  1674. err = ubifs_compressors_init();
  1675. if (err)
  1676. goto out_compr;
  1677. return 0;
  1678. out_compr:
  1679. unregister_shrinker(&ubifs_shrinker_info);
  1680. kmem_cache_destroy(ubifs_inode_slab);
  1681. out_reg:
  1682. unregister_filesystem(&ubifs_fs_type);
  1683. return err;
  1684. }
  1685. /* late_initcall to let compressors initialize first */
  1686. late_initcall(ubifs_init);
  1687. static void __exit ubifs_exit(void)
  1688. {
  1689. ubifs_assert(list_empty(&ubifs_infos));
  1690. ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
  1691. ubifs_compressors_exit();
  1692. unregister_shrinker(&ubifs_shrinker_info);
  1693. kmem_cache_destroy(ubifs_inode_slab);
  1694. unregister_filesystem(&ubifs_fs_type);
  1695. }
  1696. module_exit(ubifs_exit);
  1697. MODULE_LICENSE("GPL");
  1698. MODULE_VERSION(__stringify(UBIFS_VERSION));
  1699. MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
  1700. MODULE_DESCRIPTION("UBIFS - UBI File System");