super.c 58 KB

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