super.c 52 KB

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