super.c 67 KB

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