super.c 57 KB

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