super.c 53 KB

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