super.c 50 KB

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