super.c 50 KB

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