super.c 53 KB

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