super.c 55 KB

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