super.c 62 KB

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