super.c 60 KB

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