super.c 65 KB

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