super.c 54 KB

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