super.c 67 KB

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