super.c 51 KB

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