super.c 51 KB

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