super.c 66 KB

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