super.c 61 KB

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