super.c 58 KB

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