super.c 60 KB

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