super.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968
  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. sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
  899. if (!mounted_read_only) {
  900. err = alloc_wbufs(c);
  901. if (err)
  902. goto out_cbuf;
  903. /* Create background thread */
  904. c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
  905. if (!c->bgt)
  906. c->bgt = ERR_PTR(-EINVAL);
  907. if (IS_ERR(c->bgt)) {
  908. err = PTR_ERR(c->bgt);
  909. c->bgt = NULL;
  910. ubifs_err("cannot spawn \"%s\", error %d",
  911. c->bgt_name, err);
  912. goto out_wbufs;
  913. }
  914. wake_up_process(c->bgt);
  915. }
  916. err = ubifs_read_master(c);
  917. if (err)
  918. goto out_master;
  919. if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
  920. ubifs_msg("recovery needed");
  921. c->need_recovery = 1;
  922. if (!mounted_read_only) {
  923. err = ubifs_recover_inl_heads(c, c->sbuf);
  924. if (err)
  925. goto out_master;
  926. }
  927. } else if (!mounted_read_only) {
  928. /*
  929. * Set the "dirty" flag so that if we reboot uncleanly we
  930. * will notice this immediately on the next mount.
  931. */
  932. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  933. err = ubifs_write_master(c);
  934. if (err)
  935. goto out_master;
  936. }
  937. err = ubifs_lpt_init(c, 1, !mounted_read_only);
  938. if (err)
  939. goto out_lpt;
  940. err = dbg_check_idx_size(c, c->old_idx_sz);
  941. if (err)
  942. goto out_lpt;
  943. err = ubifs_replay_journal(c);
  944. if (err)
  945. goto out_journal;
  946. err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
  947. if (err)
  948. goto out_orphans;
  949. if (!mounted_read_only) {
  950. int lnum;
  951. /* Check for enough free space */
  952. if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
  953. ubifs_err("insufficient available space");
  954. err = -EINVAL;
  955. goto out_orphans;
  956. }
  957. /* Check for enough log space */
  958. lnum = c->lhead_lnum + 1;
  959. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  960. lnum = UBIFS_LOG_LNUM;
  961. if (lnum == c->ltail_lnum) {
  962. err = ubifs_consolidate_log(c);
  963. if (err)
  964. goto out_orphans;
  965. }
  966. if (c->need_recovery) {
  967. err = ubifs_recover_size(c);
  968. if (err)
  969. goto out_orphans;
  970. err = ubifs_rcvry_gc_commit(c);
  971. } else
  972. err = take_gc_lnum(c);
  973. if (err)
  974. goto out_orphans;
  975. err = dbg_check_lprops(c);
  976. if (err)
  977. goto out_orphans;
  978. } else if (c->need_recovery) {
  979. err = ubifs_recover_size(c);
  980. if (err)
  981. goto out_orphans;
  982. }
  983. spin_lock(&ubifs_infos_lock);
  984. list_add_tail(&c->infos_list, &ubifs_infos);
  985. spin_unlock(&ubifs_infos_lock);
  986. if (c->need_recovery) {
  987. if (mounted_read_only)
  988. ubifs_msg("recovery deferred");
  989. else {
  990. c->need_recovery = 0;
  991. ubifs_msg("recovery completed");
  992. }
  993. }
  994. err = dbg_check_filesystem(c);
  995. if (err)
  996. goto out_infos;
  997. ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
  998. c->vi.ubi_num, c->vi.vol_id, c->vi.name);
  999. if (mounted_read_only)
  1000. ubifs_msg("mounted read-only");
  1001. x = (long long)c->main_lebs * c->leb_size;
  1002. ubifs_msg("file system size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
  1003. x, x >> 10, x >> 20, c->main_lebs);
  1004. x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
  1005. ubifs_msg("journal size: %lld bytes (%lld KiB, %lld MiB, %d LEBs)",
  1006. x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
  1007. ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
  1008. ubifs_msg("media format %d, latest format %d",
  1009. c->fmt_version, UBIFS_FORMAT_VERSION);
  1010. dbg_msg("compiled on: " __DATE__ " at " __TIME__);
  1011. dbg_msg("min. I/O unit size: %d bytes", c->min_io_size);
  1012. dbg_msg("LEB size: %d bytes (%d KiB)",
  1013. c->leb_size, c->leb_size / 1024);
  1014. dbg_msg("data journal heads: %d",
  1015. c->jhead_cnt - NONDATA_JHEADS_CNT);
  1016. dbg_msg("UUID: %02X%02X%02X%02X-%02X%02X"
  1017. "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
  1018. c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
  1019. c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
  1020. c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
  1021. c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
  1022. dbg_msg("fast unmount: %d", c->fast_unmount);
  1023. dbg_msg("big_lpt %d", c->big_lpt);
  1024. dbg_msg("log LEBs: %d (%d - %d)",
  1025. c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
  1026. dbg_msg("LPT area LEBs: %d (%d - %d)",
  1027. c->lpt_lebs, c->lpt_first, c->lpt_last);
  1028. dbg_msg("orphan area LEBs: %d (%d - %d)",
  1029. c->orph_lebs, c->orph_first, c->orph_last);
  1030. dbg_msg("main area LEBs: %d (%d - %d)",
  1031. c->main_lebs, c->main_first, c->leb_cnt - 1);
  1032. dbg_msg("index LEBs: %d", c->lst.idx_lebs);
  1033. dbg_msg("total index bytes: %lld (%lld KiB, %lld MiB)",
  1034. c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
  1035. dbg_msg("key hash type: %d", c->key_hash_type);
  1036. dbg_msg("tree fanout: %d", c->fanout);
  1037. dbg_msg("reserved GC LEB: %d", c->gc_lnum);
  1038. dbg_msg("first main LEB: %d", c->main_first);
  1039. dbg_msg("dead watermark: %d", c->dead_wm);
  1040. dbg_msg("dark watermark: %d", c->dark_wm);
  1041. x = (long long)c->main_lebs * c->dark_wm;
  1042. dbg_msg("max. dark space: %lld (%lld KiB, %lld MiB)",
  1043. x, x >> 10, x >> 20);
  1044. dbg_msg("maximum bud bytes: %lld (%lld KiB, %lld MiB)",
  1045. c->max_bud_bytes, c->max_bud_bytes >> 10,
  1046. c->max_bud_bytes >> 20);
  1047. dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
  1048. c->bg_bud_bytes, c->bg_bud_bytes >> 10,
  1049. c->bg_bud_bytes >> 20);
  1050. dbg_msg("current bud bytes %lld (%lld KiB, %lld MiB)",
  1051. c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
  1052. dbg_msg("max. seq. number: %llu", c->max_sqnum);
  1053. dbg_msg("commit number: %llu", c->cmt_no);
  1054. return 0;
  1055. out_infos:
  1056. spin_lock(&ubifs_infos_lock);
  1057. list_del(&c->infos_list);
  1058. spin_unlock(&ubifs_infos_lock);
  1059. out_orphans:
  1060. free_orphans(c);
  1061. out_journal:
  1062. destroy_journal(c);
  1063. out_lpt:
  1064. ubifs_lpt_free(c, 0);
  1065. out_master:
  1066. kfree(c->mst_node);
  1067. kfree(c->rcvrd_mst_node);
  1068. if (c->bgt)
  1069. kthread_stop(c->bgt);
  1070. out_wbufs:
  1071. free_wbufs(c);
  1072. out_cbuf:
  1073. kfree(c->cbuf);
  1074. out_dereg:
  1075. dbg_failure_mode_deregistration(c);
  1076. out_free:
  1077. vfree(c->ileb_buf);
  1078. vfree(c->sbuf);
  1079. kfree(c->bottom_up_buf);
  1080. UBIFS_DBG(vfree(c->dbg_buf));
  1081. return err;
  1082. }
  1083. /**
  1084. * ubifs_umount - un-mount UBIFS file-system.
  1085. * @c: UBIFS file-system description object
  1086. *
  1087. * Note, this function is called to free allocated resourced when un-mounting,
  1088. * as well as free resources when an error occurred while we were half way
  1089. * through mounting (error path cleanup function). So it has to make sure the
  1090. * resource was actually allocated before freeing it.
  1091. */
  1092. static void ubifs_umount(struct ubifs_info *c)
  1093. {
  1094. dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
  1095. c->vi.vol_id);
  1096. spin_lock(&ubifs_infos_lock);
  1097. list_del(&c->infos_list);
  1098. spin_unlock(&ubifs_infos_lock);
  1099. if (c->bgt)
  1100. kthread_stop(c->bgt);
  1101. destroy_journal(c);
  1102. free_wbufs(c);
  1103. free_orphans(c);
  1104. ubifs_lpt_free(c, 0);
  1105. kfree(c->cbuf);
  1106. kfree(c->rcvrd_mst_node);
  1107. kfree(c->mst_node);
  1108. vfree(c->sbuf);
  1109. kfree(c->bottom_up_buf);
  1110. UBIFS_DBG(vfree(c->dbg_buf));
  1111. vfree(c->ileb_buf);
  1112. dbg_failure_mode_deregistration(c);
  1113. }
  1114. /**
  1115. * ubifs_remount_rw - re-mount in read-write mode.
  1116. * @c: UBIFS file-system description object
  1117. *
  1118. * UBIFS avoids allocating many unnecessary resources when mounted in read-only
  1119. * mode. This function allocates the needed resources and re-mounts UBIFS in
  1120. * read-write mode.
  1121. */
  1122. static int ubifs_remount_rw(struct ubifs_info *c)
  1123. {
  1124. int err, lnum;
  1125. if (c->ro_media)
  1126. return -EINVAL;
  1127. mutex_lock(&c->umount_mutex);
  1128. c->remounting_rw = 1;
  1129. /* Check for enough free space */
  1130. if (ubifs_calc_available(c, c->min_idx_lebs) <= 0) {
  1131. ubifs_err("insufficient available space");
  1132. err = -EINVAL;
  1133. goto out;
  1134. }
  1135. if (c->old_leb_cnt != c->leb_cnt) {
  1136. struct ubifs_sb_node *sup;
  1137. sup = ubifs_read_sb_node(c);
  1138. if (IS_ERR(sup)) {
  1139. err = PTR_ERR(sup);
  1140. goto out;
  1141. }
  1142. sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  1143. err = ubifs_write_sb_node(c, sup);
  1144. if (err)
  1145. goto out;
  1146. }
  1147. if (c->need_recovery) {
  1148. ubifs_msg("completing deferred recovery");
  1149. err = ubifs_write_rcvrd_mst_node(c);
  1150. if (err)
  1151. goto out;
  1152. err = ubifs_recover_size(c);
  1153. if (err)
  1154. goto out;
  1155. err = ubifs_clean_lebs(c, c->sbuf);
  1156. if (err)
  1157. goto out;
  1158. err = ubifs_recover_inl_heads(c, c->sbuf);
  1159. if (err)
  1160. goto out;
  1161. }
  1162. if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
  1163. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  1164. err = ubifs_write_master(c);
  1165. if (err)
  1166. goto out;
  1167. }
  1168. c->ileb_buf = vmalloc(c->leb_size);
  1169. if (!c->ileb_buf) {
  1170. err = -ENOMEM;
  1171. goto out;
  1172. }
  1173. err = ubifs_lpt_init(c, 0, 1);
  1174. if (err)
  1175. goto out;
  1176. err = alloc_wbufs(c);
  1177. if (err)
  1178. goto out;
  1179. ubifs_create_buds_lists(c);
  1180. /* Create background thread */
  1181. c->bgt = kthread_create(ubifs_bg_thread, c, c->bgt_name);
  1182. if (!c->bgt)
  1183. c->bgt = ERR_PTR(-EINVAL);
  1184. if (IS_ERR(c->bgt)) {
  1185. err = PTR_ERR(c->bgt);
  1186. c->bgt = NULL;
  1187. ubifs_err("cannot spawn \"%s\", error %d",
  1188. c->bgt_name, err);
  1189. return err;
  1190. }
  1191. wake_up_process(c->bgt);
  1192. c->orph_buf = vmalloc(c->leb_size);
  1193. if (!c->orph_buf)
  1194. return -ENOMEM;
  1195. /* Check for enough log space */
  1196. lnum = c->lhead_lnum + 1;
  1197. if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  1198. lnum = UBIFS_LOG_LNUM;
  1199. if (lnum == c->ltail_lnum) {
  1200. err = ubifs_consolidate_log(c);
  1201. if (err)
  1202. goto out;
  1203. }
  1204. if (c->need_recovery)
  1205. err = ubifs_rcvry_gc_commit(c);
  1206. else
  1207. err = take_gc_lnum(c);
  1208. if (err)
  1209. goto out;
  1210. if (c->need_recovery) {
  1211. c->need_recovery = 0;
  1212. ubifs_msg("deferred recovery completed");
  1213. }
  1214. dbg_gen("re-mounted read-write");
  1215. c->vfs_sb->s_flags &= ~MS_RDONLY;
  1216. c->remounting_rw = 0;
  1217. mutex_unlock(&c->umount_mutex);
  1218. return 0;
  1219. out:
  1220. vfree(c->orph_buf);
  1221. c->orph_buf = NULL;
  1222. if (c->bgt) {
  1223. kthread_stop(c->bgt);
  1224. c->bgt = NULL;
  1225. }
  1226. free_wbufs(c);
  1227. vfree(c->ileb_buf);
  1228. c->ileb_buf = NULL;
  1229. ubifs_lpt_free(c, 1);
  1230. c->remounting_rw = 0;
  1231. mutex_unlock(&c->umount_mutex);
  1232. return err;
  1233. }
  1234. /**
  1235. * commit_on_unmount - commit the journal when un-mounting.
  1236. * @c: UBIFS file-system description object
  1237. *
  1238. * This function is called during un-mounting and it commits the journal unless
  1239. * the "fast unmount" mode is enabled. It also avoids committing the journal if
  1240. * it contains too few data.
  1241. *
  1242. * Sometimes recovery requires the journal to be committed at least once, and
  1243. * this function takes care about this.
  1244. */
  1245. static void commit_on_unmount(struct ubifs_info *c)
  1246. {
  1247. if (!c->fast_unmount) {
  1248. long long bud_bytes;
  1249. spin_lock(&c->buds_lock);
  1250. bud_bytes = c->bud_bytes;
  1251. spin_unlock(&c->buds_lock);
  1252. if (bud_bytes > c->leb_size)
  1253. ubifs_run_commit(c);
  1254. }
  1255. }
  1256. /**
  1257. * ubifs_remount_ro - re-mount in read-only mode.
  1258. * @c: UBIFS file-system description object
  1259. *
  1260. * We rely on VFS to have stopped writing. Possibly the background thread could
  1261. * be running a commit, however kthread_stop will wait in that case.
  1262. */
  1263. static void ubifs_remount_ro(struct ubifs_info *c)
  1264. {
  1265. int i, err;
  1266. ubifs_assert(!c->need_recovery);
  1267. commit_on_unmount(c);
  1268. mutex_lock(&c->umount_mutex);
  1269. if (c->bgt) {
  1270. kthread_stop(c->bgt);
  1271. c->bgt = NULL;
  1272. }
  1273. for (i = 0; i < c->jhead_cnt; i++) {
  1274. ubifs_wbuf_sync(&c->jheads[i].wbuf);
  1275. del_timer_sync(&c->jheads[i].wbuf.timer);
  1276. }
  1277. if (!c->ro_media) {
  1278. c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  1279. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  1280. c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  1281. err = ubifs_write_master(c);
  1282. if (err)
  1283. ubifs_ro_mode(c, err);
  1284. }
  1285. ubifs_destroy_idx_gc(c);
  1286. free_wbufs(c);
  1287. vfree(c->orph_buf);
  1288. c->orph_buf = NULL;
  1289. vfree(c->ileb_buf);
  1290. c->ileb_buf = NULL;
  1291. ubifs_lpt_free(c, 1);
  1292. mutex_unlock(&c->umount_mutex);
  1293. }
  1294. static void ubifs_put_super(struct super_block *sb)
  1295. {
  1296. int i;
  1297. struct ubifs_info *c = sb->s_fs_info;
  1298. ubifs_msg("un-mount UBI device %d, volume %d", c->vi.ubi_num,
  1299. c->vi.vol_id);
  1300. /*
  1301. * The following asserts are only valid if there has not been a failure
  1302. * of the media. For example, there will be dirty inodes if we failed
  1303. * to write them back because of I/O errors.
  1304. */
  1305. ubifs_assert(atomic_long_read(&c->dirty_pg_cnt) == 0);
  1306. ubifs_assert(c->budg_idx_growth == 0);
  1307. ubifs_assert(c->budg_dd_growth == 0);
  1308. ubifs_assert(c->budg_data_growth == 0);
  1309. /*
  1310. * The 'c->umount_lock' prevents races between UBIFS memory shrinker
  1311. * and file system un-mount. Namely, it prevents the shrinker from
  1312. * picking this superblock for shrinking - it will be just skipped if
  1313. * the mutex is locked.
  1314. */
  1315. mutex_lock(&c->umount_mutex);
  1316. if (!(c->vfs_sb->s_flags & MS_RDONLY)) {
  1317. /*
  1318. * First of all kill the background thread to make sure it does
  1319. * not interfere with un-mounting and freeing resources.
  1320. */
  1321. if (c->bgt) {
  1322. kthread_stop(c->bgt);
  1323. c->bgt = NULL;
  1324. }
  1325. /* Synchronize write-buffers */
  1326. if (c->jheads)
  1327. for (i = 0; i < c->jhead_cnt; i++) {
  1328. ubifs_wbuf_sync(&c->jheads[i].wbuf);
  1329. del_timer_sync(&c->jheads[i].wbuf.timer);
  1330. }
  1331. /*
  1332. * On fatal errors c->ro_media is set to 1, in which case we do
  1333. * not write the master node.
  1334. */
  1335. if (!c->ro_media) {
  1336. /*
  1337. * We are being cleanly unmounted which means the
  1338. * orphans were killed - indicate this in the master
  1339. * node. Also save the reserved GC LEB number.
  1340. */
  1341. int err;
  1342. c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  1343. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  1344. c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  1345. err = ubifs_write_master(c);
  1346. if (err)
  1347. /*
  1348. * Recovery will attempt to fix the master area
  1349. * next mount, so we just print a message and
  1350. * continue to unmount normally.
  1351. */
  1352. ubifs_err("failed to write master node, "
  1353. "error %d", err);
  1354. }
  1355. }
  1356. ubifs_umount(c);
  1357. bdi_destroy(&c->bdi);
  1358. ubi_close_volume(c->ubi);
  1359. mutex_unlock(&c->umount_mutex);
  1360. kfree(c);
  1361. }
  1362. static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
  1363. {
  1364. int err;
  1365. struct ubifs_info *c = sb->s_fs_info;
  1366. dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
  1367. err = ubifs_parse_options(c, data, 1);
  1368. if (err) {
  1369. ubifs_err("invalid or unknown remount parameter");
  1370. return err;
  1371. }
  1372. if ((sb->s_flags & MS_RDONLY) && !(*flags & MS_RDONLY)) {
  1373. err = ubifs_remount_rw(c);
  1374. if (err)
  1375. return err;
  1376. } else if (!(sb->s_flags & MS_RDONLY) && (*flags & MS_RDONLY))
  1377. ubifs_remount_ro(c);
  1378. return 0;
  1379. }
  1380. struct super_operations ubifs_super_operations = {
  1381. .alloc_inode = ubifs_alloc_inode,
  1382. .destroy_inode = ubifs_destroy_inode,
  1383. .put_super = ubifs_put_super,
  1384. .write_inode = ubifs_write_inode,
  1385. .delete_inode = ubifs_delete_inode,
  1386. .statfs = ubifs_statfs,
  1387. .dirty_inode = ubifs_dirty_inode,
  1388. .remount_fs = ubifs_remount_fs,
  1389. .show_options = ubifs_show_options,
  1390. .sync_fs = ubifs_sync_fs,
  1391. };
  1392. /**
  1393. * open_ubi - parse UBI device name string and open the UBI device.
  1394. * @name: UBI volume name
  1395. * @mode: UBI volume open mode
  1396. *
  1397. * There are several ways to specify UBI volumes when mounting UBIFS:
  1398. * o ubiX_Y - UBI device number X, volume Y;
  1399. * o ubiY - UBI device number 0, volume Y;
  1400. * o ubiX:NAME - mount UBI device X, volume with name NAME;
  1401. * o ubi:NAME - mount UBI device 0, volume with name NAME.
  1402. *
  1403. * Alternative '!' separator may be used instead of ':' (because some shells
  1404. * like busybox may interpret ':' as an NFS host name separator). This function
  1405. * returns ubi volume object in case of success and a negative error code in
  1406. * case of failure.
  1407. */
  1408. static struct ubi_volume_desc *open_ubi(const char *name, int mode)
  1409. {
  1410. int dev, vol;
  1411. char *endptr;
  1412. if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
  1413. return ERR_PTR(-EINVAL);
  1414. /* ubi:NAME method */
  1415. if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
  1416. return ubi_open_volume_nm(0, name + 4, mode);
  1417. if (!isdigit(name[3]))
  1418. return ERR_PTR(-EINVAL);
  1419. dev = simple_strtoul(name + 3, &endptr, 0);
  1420. /* ubiY method */
  1421. if (*endptr == '\0')
  1422. return ubi_open_volume(0, dev, mode);
  1423. /* ubiX_Y method */
  1424. if (*endptr == '_' && isdigit(endptr[1])) {
  1425. vol = simple_strtoul(endptr + 1, &endptr, 0);
  1426. if (*endptr != '\0')
  1427. return ERR_PTR(-EINVAL);
  1428. return ubi_open_volume(dev, vol, mode);
  1429. }
  1430. /* ubiX:NAME method */
  1431. if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
  1432. return ubi_open_volume_nm(dev, ++endptr, mode);
  1433. return ERR_PTR(-EINVAL);
  1434. }
  1435. static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
  1436. {
  1437. struct ubi_volume_desc *ubi = sb->s_fs_info;
  1438. struct ubifs_info *c;
  1439. struct inode *root;
  1440. int err;
  1441. c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
  1442. if (!c)
  1443. return -ENOMEM;
  1444. spin_lock_init(&c->cnt_lock);
  1445. spin_lock_init(&c->cs_lock);
  1446. spin_lock_init(&c->buds_lock);
  1447. spin_lock_init(&c->space_lock);
  1448. spin_lock_init(&c->orphan_lock);
  1449. init_rwsem(&c->commit_sem);
  1450. mutex_init(&c->lp_mutex);
  1451. mutex_init(&c->tnc_mutex);
  1452. mutex_init(&c->log_mutex);
  1453. mutex_init(&c->mst_mutex);
  1454. mutex_init(&c->umount_mutex);
  1455. init_waitqueue_head(&c->cmt_wq);
  1456. c->buds = RB_ROOT;
  1457. c->old_idx = RB_ROOT;
  1458. c->size_tree = RB_ROOT;
  1459. c->orph_tree = RB_ROOT;
  1460. INIT_LIST_HEAD(&c->infos_list);
  1461. INIT_LIST_HEAD(&c->idx_gc);
  1462. INIT_LIST_HEAD(&c->replay_list);
  1463. INIT_LIST_HEAD(&c->replay_buds);
  1464. INIT_LIST_HEAD(&c->uncat_list);
  1465. INIT_LIST_HEAD(&c->empty_list);
  1466. INIT_LIST_HEAD(&c->freeable_list);
  1467. INIT_LIST_HEAD(&c->frdi_idx_list);
  1468. INIT_LIST_HEAD(&c->unclean_leb_list);
  1469. INIT_LIST_HEAD(&c->old_buds);
  1470. INIT_LIST_HEAD(&c->orph_list);
  1471. INIT_LIST_HEAD(&c->orph_new);
  1472. c->highest_inum = UBIFS_FIRST_INO;
  1473. c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
  1474. ubi_get_volume_info(ubi, &c->vi);
  1475. ubi_get_device_info(c->vi.ubi_num, &c->di);
  1476. /* Re-open the UBI device in read-write mode */
  1477. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
  1478. if (IS_ERR(c->ubi)) {
  1479. err = PTR_ERR(c->ubi);
  1480. goto out_free;
  1481. }
  1482. /*
  1483. * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
  1484. * UBIFS, I/O is not deferred, it is done immediately in readpage,
  1485. * which means the user would have to wait not just for their own I/O
  1486. * but the read-ahead I/O as well i.e. completely pointless.
  1487. *
  1488. * Read-ahead will be disabled because @c->bdi.ra_pages is 0.
  1489. */
  1490. c->bdi.capabilities = BDI_CAP_MAP_COPY;
  1491. c->bdi.unplug_io_fn = default_unplug_io_fn;
  1492. err = bdi_init(&c->bdi);
  1493. if (err)
  1494. goto out_close;
  1495. err = ubifs_parse_options(c, data, 0);
  1496. if (err)
  1497. goto out_bdi;
  1498. c->vfs_sb = sb;
  1499. sb->s_fs_info = c;
  1500. sb->s_magic = UBIFS_SUPER_MAGIC;
  1501. sb->s_blocksize = UBIFS_BLOCK_SIZE;
  1502. sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
  1503. sb->s_dev = c->vi.cdev;
  1504. sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
  1505. if (c->max_inode_sz > MAX_LFS_FILESIZE)
  1506. sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
  1507. sb->s_op = &ubifs_super_operations;
  1508. mutex_lock(&c->umount_mutex);
  1509. err = mount_ubifs(c);
  1510. if (err) {
  1511. ubifs_assert(err < 0);
  1512. goto out_unlock;
  1513. }
  1514. /* Read the root inode */
  1515. root = ubifs_iget(sb, UBIFS_ROOT_INO);
  1516. if (IS_ERR(root)) {
  1517. err = PTR_ERR(root);
  1518. goto out_umount;
  1519. }
  1520. sb->s_root = d_alloc_root(root);
  1521. if (!sb->s_root)
  1522. goto out_iput;
  1523. mutex_unlock(&c->umount_mutex);
  1524. return 0;
  1525. out_iput:
  1526. iput(root);
  1527. out_umount:
  1528. ubifs_umount(c);
  1529. out_unlock:
  1530. mutex_unlock(&c->umount_mutex);
  1531. out_bdi:
  1532. bdi_destroy(&c->bdi);
  1533. out_close:
  1534. ubi_close_volume(c->ubi);
  1535. out_free:
  1536. kfree(c);
  1537. return err;
  1538. }
  1539. static int sb_test(struct super_block *sb, void *data)
  1540. {
  1541. dev_t *dev = data;
  1542. return sb->s_dev == *dev;
  1543. }
  1544. static int sb_set(struct super_block *sb, void *data)
  1545. {
  1546. dev_t *dev = data;
  1547. sb->s_dev = *dev;
  1548. return 0;
  1549. }
  1550. static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
  1551. const char *name, void *data, struct vfsmount *mnt)
  1552. {
  1553. struct ubi_volume_desc *ubi;
  1554. struct ubi_volume_info vi;
  1555. struct super_block *sb;
  1556. int err;
  1557. dbg_gen("name %s, flags %#x", name, flags);
  1558. /*
  1559. * Get UBI device number and volume ID. Mount it read-only so far
  1560. * because this might be a new mount point, and UBI allows only one
  1561. * read-write user at a time.
  1562. */
  1563. ubi = open_ubi(name, UBI_READONLY);
  1564. if (IS_ERR(ubi)) {
  1565. ubifs_err("cannot open \"%s\", error %d",
  1566. name, (int)PTR_ERR(ubi));
  1567. return PTR_ERR(ubi);
  1568. }
  1569. ubi_get_volume_info(ubi, &vi);
  1570. dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
  1571. sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
  1572. if (IS_ERR(sb)) {
  1573. err = PTR_ERR(sb);
  1574. goto out_close;
  1575. }
  1576. if (sb->s_root) {
  1577. /* A new mount point for already mounted UBIFS */
  1578. dbg_gen("this ubi volume is already mounted");
  1579. if ((flags ^ sb->s_flags) & MS_RDONLY) {
  1580. err = -EBUSY;
  1581. goto out_deact;
  1582. }
  1583. } else {
  1584. sb->s_flags = flags;
  1585. /*
  1586. * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
  1587. * replaced by 'c'.
  1588. */
  1589. sb->s_fs_info = ubi;
  1590. err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  1591. if (err)
  1592. goto out_deact;
  1593. /* We do not support atime */
  1594. sb->s_flags |= MS_ACTIVE | MS_NOATIME;
  1595. }
  1596. /* 'fill_super()' opens ubi again so we must close it here */
  1597. ubi_close_volume(ubi);
  1598. return simple_set_mnt(mnt, sb);
  1599. out_deact:
  1600. up_write(&sb->s_umount);
  1601. deactivate_super(sb);
  1602. out_close:
  1603. ubi_close_volume(ubi);
  1604. return err;
  1605. }
  1606. static void ubifs_kill_sb(struct super_block *sb)
  1607. {
  1608. struct ubifs_info *c = sb->s_fs_info;
  1609. /*
  1610. * We do 'commit_on_unmount()' here instead of 'ubifs_put_super()'
  1611. * in order to be outside BKL.
  1612. */
  1613. if (sb->s_root && !(sb->s_flags & MS_RDONLY))
  1614. commit_on_unmount(c);
  1615. /* The un-mount routine is actually done in put_super() */
  1616. generic_shutdown_super(sb);
  1617. }
  1618. static struct file_system_type ubifs_fs_type = {
  1619. .name = "ubifs",
  1620. .owner = THIS_MODULE,
  1621. .get_sb = ubifs_get_sb,
  1622. .kill_sb = ubifs_kill_sb
  1623. };
  1624. /*
  1625. * Inode slab cache constructor.
  1626. */
  1627. static void inode_slab_ctor(void *obj)
  1628. {
  1629. struct ubifs_inode *ui = obj;
  1630. inode_init_once(&ui->vfs_inode);
  1631. }
  1632. static int __init ubifs_init(void)
  1633. {
  1634. int err;
  1635. BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
  1636. /* Make sure node sizes are 8-byte aligned */
  1637. BUILD_BUG_ON(UBIFS_CH_SZ & 7);
  1638. BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7);
  1639. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
  1640. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
  1641. BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
  1642. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
  1643. BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7);
  1644. BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7);
  1645. BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7);
  1646. BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7);
  1647. BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
  1648. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
  1649. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
  1650. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
  1651. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7);
  1652. BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7);
  1653. BUILD_BUG_ON(MIN_WRITE_SZ & 7);
  1654. /* Check min. node size */
  1655. BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ);
  1656. BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
  1657. BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
  1658. BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
  1659. BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1660. BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1661. BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1662. BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ);
  1663. /* Defined node sizes */
  1664. BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096);
  1665. BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
  1666. BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
  1667. BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
  1668. /*
  1669. * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
  1670. * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
  1671. */
  1672. if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
  1673. ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
  1674. " at least 4096 bytes",
  1675. (unsigned int)PAGE_CACHE_SIZE);
  1676. return -EINVAL;
  1677. }
  1678. err = register_filesystem(&ubifs_fs_type);
  1679. if (err) {
  1680. ubifs_err("cannot register file system, error %d", err);
  1681. return err;
  1682. }
  1683. err = -ENOMEM;
  1684. ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
  1685. sizeof(struct ubifs_inode), 0,
  1686. SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT,
  1687. &inode_slab_ctor);
  1688. if (!ubifs_inode_slab)
  1689. goto out_reg;
  1690. register_shrinker(&ubifs_shrinker_info);
  1691. err = ubifs_compressors_init();
  1692. if (err)
  1693. goto out_compr;
  1694. return 0;
  1695. out_compr:
  1696. unregister_shrinker(&ubifs_shrinker_info);
  1697. kmem_cache_destroy(ubifs_inode_slab);
  1698. out_reg:
  1699. unregister_filesystem(&ubifs_fs_type);
  1700. return err;
  1701. }
  1702. /* late_initcall to let compressors initialize first */
  1703. late_initcall(ubifs_init);
  1704. static void __exit ubifs_exit(void)
  1705. {
  1706. ubifs_assert(list_empty(&ubifs_infos));
  1707. ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
  1708. ubifs_compressors_exit();
  1709. unregister_shrinker(&ubifs_shrinker_info);
  1710. kmem_cache_destroy(ubifs_inode_slab);
  1711. unregister_filesystem(&ubifs_fs_type);
  1712. }
  1713. module_exit(ubifs_exit);
  1714. MODULE_LICENSE("GPL");
  1715. MODULE_VERSION(__stringify(UBIFS_VERSION));
  1716. MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
  1717. MODULE_DESCRIPTION("UBIFS - UBI File System");