super.c 61 KB

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