super.c 60 KB

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