super.c 65 KB

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