super.c 61 KB

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