super.c 59 KB

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