super.c 57 KB

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