super.c 57 KB

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