super.c 52 KB

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