super.c 50 KB

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