super.c 60 KB

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