super.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547
  1. /*
  2. * linux/fs/ext3/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/config.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/fs.h>
  22. #include <linux/time.h>
  23. #include <linux/jbd.h>
  24. #include <linux/ext3_fs.h>
  25. #include <linux/ext3_jbd.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/parser.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/buffer_head.h>
  32. #include <linux/vfs.h>
  33. #include <linux/random.h>
  34. #include <linux/mount.h>
  35. #include <linux/namei.h>
  36. #include <linux/quotaops.h>
  37. #include <asm/uaccess.h>
  38. #include "xattr.h"
  39. #include "acl.h"
  40. static int ext3_load_journal(struct super_block *, struct ext3_super_block *);
  41. static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
  42. int);
  43. static void ext3_commit_super (struct super_block * sb,
  44. struct ext3_super_block * es,
  45. int sync);
  46. static void ext3_mark_recovery_complete(struct super_block * sb,
  47. struct ext3_super_block * es);
  48. static void ext3_clear_journal_err(struct super_block * sb,
  49. struct ext3_super_block * es);
  50. static int ext3_sync_fs(struct super_block *sb, int wait);
  51. static const char *ext3_decode_error(struct super_block * sb, int errno,
  52. char nbuf[16]);
  53. static int ext3_remount (struct super_block * sb, int * flags, char * data);
  54. static int ext3_statfs (struct super_block * sb, struct kstatfs * buf);
  55. static void ext3_unlockfs(struct super_block *sb);
  56. static void ext3_write_super (struct super_block * sb);
  57. static void ext3_write_super_lockfs(struct super_block *sb);
  58. /*
  59. * Wrappers for journal_start/end.
  60. *
  61. * The only special thing we need to do here is to make sure that all
  62. * journal_end calls result in the superblock being marked dirty, so
  63. * that sync() will call the filesystem's write_super callback if
  64. * appropriate.
  65. */
  66. handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
  67. {
  68. journal_t *journal;
  69. if (sb->s_flags & MS_RDONLY)
  70. return ERR_PTR(-EROFS);
  71. /* Special case here: if the journal has aborted behind our
  72. * backs (eg. EIO in the commit thread), then we still need to
  73. * take the FS itself readonly cleanly. */
  74. journal = EXT3_SB(sb)->s_journal;
  75. if (is_journal_aborted(journal)) {
  76. ext3_abort(sb, __FUNCTION__,
  77. "Detected aborted journal");
  78. return ERR_PTR(-EROFS);
  79. }
  80. return journal_start(journal, nblocks);
  81. }
  82. /*
  83. * The only special thing we need to do here is to make sure that all
  84. * journal_stop calls result in the superblock being marked dirty, so
  85. * that sync() will call the filesystem's write_super callback if
  86. * appropriate.
  87. */
  88. int __ext3_journal_stop(const char *where, handle_t *handle)
  89. {
  90. struct super_block *sb;
  91. int err;
  92. int rc;
  93. sb = handle->h_transaction->t_journal->j_private;
  94. err = handle->h_err;
  95. rc = journal_stop(handle);
  96. if (!err)
  97. err = rc;
  98. if (err)
  99. __ext3_std_error(sb, where, err);
  100. return err;
  101. }
  102. void ext3_journal_abort_handle(const char *caller, const char *err_fn,
  103. struct buffer_head *bh, handle_t *handle, int err)
  104. {
  105. char nbuf[16];
  106. const char *errstr = ext3_decode_error(NULL, err, nbuf);
  107. if (bh)
  108. BUFFER_TRACE(bh, "abort");
  109. if (!handle->h_err)
  110. handle->h_err = err;
  111. if (is_handle_aborted(handle))
  112. return;
  113. printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
  114. caller, errstr, err_fn);
  115. journal_abort_handle(handle);
  116. }
  117. /* Deal with the reporting of failure conditions on a filesystem such as
  118. * inconsistencies detected or read IO failures.
  119. *
  120. * On ext2, we can store the error state of the filesystem in the
  121. * superblock. That is not possible on ext3, because we may have other
  122. * write ordering constraints on the superblock which prevent us from
  123. * writing it out straight away; and given that the journal is about to
  124. * be aborted, we can't rely on the current, or future, transactions to
  125. * write out the superblock safely.
  126. *
  127. * We'll just use the journal_abort() error code to record an error in
  128. * the journal instead. On recovery, the journal will compain about
  129. * that error until we've noted it down and cleared it.
  130. */
  131. static void ext3_handle_error(struct super_block *sb)
  132. {
  133. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  134. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  135. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  136. if (sb->s_flags & MS_RDONLY)
  137. return;
  138. if (test_opt (sb, ERRORS_RO)) {
  139. printk (KERN_CRIT "Remounting filesystem read-only\n");
  140. sb->s_flags |= MS_RDONLY;
  141. } else {
  142. journal_t *journal = EXT3_SB(sb)->s_journal;
  143. EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
  144. if (journal)
  145. journal_abort(journal, -EIO);
  146. }
  147. if (test_opt(sb, ERRORS_PANIC))
  148. panic("EXT3-fs (device %s): panic forced after error\n",
  149. sb->s_id);
  150. ext3_commit_super(sb, es, 1);
  151. }
  152. void ext3_error (struct super_block * sb, const char * function,
  153. const char * fmt, ...)
  154. {
  155. va_list args;
  156. va_start(args, fmt);
  157. printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
  158. vprintk(fmt, args);
  159. printk("\n");
  160. va_end(args);
  161. ext3_handle_error(sb);
  162. }
  163. static const char *ext3_decode_error(struct super_block * sb, int errno,
  164. char nbuf[16])
  165. {
  166. char *errstr = NULL;
  167. switch (errno) {
  168. case -EIO:
  169. errstr = "IO failure";
  170. break;
  171. case -ENOMEM:
  172. errstr = "Out of memory";
  173. break;
  174. case -EROFS:
  175. if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
  176. errstr = "Journal has aborted";
  177. else
  178. errstr = "Readonly filesystem";
  179. break;
  180. default:
  181. /* If the caller passed in an extra buffer for unknown
  182. * errors, textualise them now. Else we just return
  183. * NULL. */
  184. if (nbuf) {
  185. /* Check for truncated error codes... */
  186. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  187. errstr = nbuf;
  188. }
  189. break;
  190. }
  191. return errstr;
  192. }
  193. /* __ext3_std_error decodes expected errors from journaling functions
  194. * automatically and invokes the appropriate error response. */
  195. void __ext3_std_error (struct super_block * sb, const char * function,
  196. int errno)
  197. {
  198. char nbuf[16];
  199. const char *errstr;
  200. /* Special case: if the error is EROFS, and we're not already
  201. * inside a transaction, then there's really no point in logging
  202. * an error. */
  203. if (errno == -EROFS && journal_current_handle() == NULL &&
  204. (sb->s_flags & MS_RDONLY))
  205. return;
  206. errstr = ext3_decode_error(sb, errno, nbuf);
  207. printk (KERN_CRIT "EXT3-fs error (device %s) in %s: %s\n",
  208. sb->s_id, function, errstr);
  209. ext3_handle_error(sb);
  210. }
  211. /*
  212. * ext3_abort is a much stronger failure handler than ext3_error. The
  213. * abort function may be used to deal with unrecoverable failures such
  214. * as journal IO errors or ENOMEM at a critical moment in log management.
  215. *
  216. * We unconditionally force the filesystem into an ABORT|READONLY state,
  217. * unless the error response on the fs has been set to panic in which
  218. * case we take the easy way out and panic immediately.
  219. */
  220. void ext3_abort (struct super_block * sb, const char * function,
  221. const char * fmt, ...)
  222. {
  223. va_list args;
  224. printk (KERN_CRIT "ext3_abort called.\n");
  225. va_start(args, fmt);
  226. printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
  227. vprintk(fmt, args);
  228. printk("\n");
  229. va_end(args);
  230. if (test_opt(sb, ERRORS_PANIC))
  231. panic("EXT3-fs panic from previous error\n");
  232. if (sb->s_flags & MS_RDONLY)
  233. return;
  234. printk(KERN_CRIT "Remounting filesystem read-only\n");
  235. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  236. sb->s_flags |= MS_RDONLY;
  237. EXT3_SB(sb)->s_mount_opt |= EXT3_MOUNT_ABORT;
  238. journal_abort(EXT3_SB(sb)->s_journal, -EIO);
  239. }
  240. void ext3_warning (struct super_block * sb, const char * function,
  241. const char * fmt, ...)
  242. {
  243. va_list args;
  244. va_start(args, fmt);
  245. printk(KERN_WARNING "EXT3-fs warning (device %s): %s: ",
  246. sb->s_id, function);
  247. vprintk(fmt, args);
  248. printk("\n");
  249. va_end(args);
  250. }
  251. void ext3_update_dynamic_rev(struct super_block *sb)
  252. {
  253. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  254. if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
  255. return;
  256. ext3_warning(sb, __FUNCTION__,
  257. "updating to rev %d because of new feature flag, "
  258. "running e2fsck is recommended",
  259. EXT3_DYNAMIC_REV);
  260. es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
  261. es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
  262. es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
  263. /* leave es->s_feature_*compat flags alone */
  264. /* es->s_uuid will be set by e2fsck if empty */
  265. /*
  266. * The rest of the superblock fields should be zero, and if not it
  267. * means they are likely already in use, so leave them alone. We
  268. * can leave it up to e2fsck to clean up any inconsistencies there.
  269. */
  270. }
  271. /*
  272. * Open the external journal device
  273. */
  274. static struct block_device *ext3_blkdev_get(dev_t dev)
  275. {
  276. struct block_device *bdev;
  277. char b[BDEVNAME_SIZE];
  278. bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
  279. if (IS_ERR(bdev))
  280. goto fail;
  281. return bdev;
  282. fail:
  283. printk(KERN_ERR "EXT3: failed to open journal device %s: %ld\n",
  284. __bdevname(dev, b), PTR_ERR(bdev));
  285. return NULL;
  286. }
  287. /*
  288. * Release the journal device
  289. */
  290. static int ext3_blkdev_put(struct block_device *bdev)
  291. {
  292. bd_release(bdev);
  293. return blkdev_put(bdev);
  294. }
  295. static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
  296. {
  297. struct block_device *bdev;
  298. int ret = -ENODEV;
  299. bdev = sbi->journal_bdev;
  300. if (bdev) {
  301. ret = ext3_blkdev_put(bdev);
  302. sbi->journal_bdev = NULL;
  303. }
  304. return ret;
  305. }
  306. static inline struct inode *orphan_list_entry(struct list_head *l)
  307. {
  308. return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
  309. }
  310. static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
  311. {
  312. struct list_head *l;
  313. printk(KERN_ERR "sb orphan head is %d\n",
  314. le32_to_cpu(sbi->s_es->s_last_orphan));
  315. printk(KERN_ERR "sb_info orphan list:\n");
  316. list_for_each(l, &sbi->s_orphan) {
  317. struct inode *inode = orphan_list_entry(l);
  318. printk(KERN_ERR " "
  319. "inode %s:%ld at %p: mode %o, nlink %d, next %d\n",
  320. inode->i_sb->s_id, inode->i_ino, inode,
  321. inode->i_mode, inode->i_nlink,
  322. NEXT_ORPHAN(inode));
  323. }
  324. }
  325. static void ext3_put_super (struct super_block * sb)
  326. {
  327. struct ext3_sb_info *sbi = EXT3_SB(sb);
  328. struct ext3_super_block *es = sbi->s_es;
  329. int i;
  330. ext3_xattr_put_super(sb);
  331. journal_destroy(sbi->s_journal);
  332. if (!(sb->s_flags & MS_RDONLY)) {
  333. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  334. es->s_state = cpu_to_le16(sbi->s_mount_state);
  335. BUFFER_TRACE(sbi->s_sbh, "marking dirty");
  336. mark_buffer_dirty(sbi->s_sbh);
  337. ext3_commit_super(sb, es, 1);
  338. }
  339. for (i = 0; i < sbi->s_gdb_count; i++)
  340. brelse(sbi->s_group_desc[i]);
  341. kfree(sbi->s_group_desc);
  342. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  343. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  344. percpu_counter_destroy(&sbi->s_dirs_counter);
  345. brelse(sbi->s_sbh);
  346. #ifdef CONFIG_QUOTA
  347. for (i = 0; i < MAXQUOTAS; i++)
  348. kfree(sbi->s_qf_names[i]);
  349. #endif
  350. /* Debugging code just in case the in-memory inode orphan list
  351. * isn't empty. The on-disk one can be non-empty if we've
  352. * detected an error and taken the fs readonly, but the
  353. * in-memory list had better be clean by this point. */
  354. if (!list_empty(&sbi->s_orphan))
  355. dump_orphan_list(sb, sbi);
  356. J_ASSERT(list_empty(&sbi->s_orphan));
  357. invalidate_bdev(sb->s_bdev, 0);
  358. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  359. /*
  360. * Invalidate the journal device's buffers. We don't want them
  361. * floating about in memory - the physical journal device may
  362. * hotswapped, and it breaks the `ro-after' testing code.
  363. */
  364. sync_blockdev(sbi->journal_bdev);
  365. invalidate_bdev(sbi->journal_bdev, 0);
  366. ext3_blkdev_remove(sbi);
  367. }
  368. sb->s_fs_info = NULL;
  369. kfree(sbi);
  370. return;
  371. }
  372. static kmem_cache_t *ext3_inode_cachep;
  373. /*
  374. * Called inside transaction, so use GFP_NOFS
  375. */
  376. static struct inode *ext3_alloc_inode(struct super_block *sb)
  377. {
  378. struct ext3_inode_info *ei;
  379. ei = kmem_cache_alloc(ext3_inode_cachep, SLAB_NOFS);
  380. if (!ei)
  381. return NULL;
  382. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  383. ei->i_acl = EXT3_ACL_NOT_CACHED;
  384. ei->i_default_acl = EXT3_ACL_NOT_CACHED;
  385. #endif
  386. ei->i_block_alloc_info = NULL;
  387. ei->vfs_inode.i_version = 1;
  388. return &ei->vfs_inode;
  389. }
  390. static void ext3_destroy_inode(struct inode *inode)
  391. {
  392. kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
  393. }
  394. static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
  395. {
  396. struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
  397. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  398. SLAB_CTOR_CONSTRUCTOR) {
  399. INIT_LIST_HEAD(&ei->i_orphan);
  400. #ifdef CONFIG_EXT3_FS_XATTR
  401. init_rwsem(&ei->xattr_sem);
  402. #endif
  403. init_MUTEX(&ei->truncate_sem);
  404. inode_init_once(&ei->vfs_inode);
  405. }
  406. }
  407. static int init_inodecache(void)
  408. {
  409. ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
  410. sizeof(struct ext3_inode_info),
  411. 0, SLAB_RECLAIM_ACCOUNT,
  412. init_once, NULL);
  413. if (ext3_inode_cachep == NULL)
  414. return -ENOMEM;
  415. return 0;
  416. }
  417. static void destroy_inodecache(void)
  418. {
  419. if (kmem_cache_destroy(ext3_inode_cachep))
  420. printk(KERN_INFO "ext3_inode_cache: not all structures were freed\n");
  421. }
  422. static void ext3_clear_inode(struct inode *inode)
  423. {
  424. struct ext3_block_alloc_info *rsv = EXT3_I(inode)->i_block_alloc_info;
  425. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  426. if (EXT3_I(inode)->i_acl &&
  427. EXT3_I(inode)->i_acl != EXT3_ACL_NOT_CACHED) {
  428. posix_acl_release(EXT3_I(inode)->i_acl);
  429. EXT3_I(inode)->i_acl = EXT3_ACL_NOT_CACHED;
  430. }
  431. if (EXT3_I(inode)->i_default_acl &&
  432. EXT3_I(inode)->i_default_acl != EXT3_ACL_NOT_CACHED) {
  433. posix_acl_release(EXT3_I(inode)->i_default_acl);
  434. EXT3_I(inode)->i_default_acl = EXT3_ACL_NOT_CACHED;
  435. }
  436. #endif
  437. ext3_discard_reservation(inode);
  438. EXT3_I(inode)->i_block_alloc_info = NULL;
  439. kfree(rsv);
  440. }
  441. #ifdef CONFIG_QUOTA
  442. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  443. #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  444. static int ext3_dquot_initialize(struct inode *inode, int type);
  445. static int ext3_dquot_drop(struct inode *inode);
  446. static int ext3_write_dquot(struct dquot *dquot);
  447. static int ext3_acquire_dquot(struct dquot *dquot);
  448. static int ext3_release_dquot(struct dquot *dquot);
  449. static int ext3_mark_dquot_dirty(struct dquot *dquot);
  450. static int ext3_write_info(struct super_block *sb, int type);
  451. static int ext3_quota_on(struct super_block *sb, int type, int format_id, char *path);
  452. static int ext3_quota_on_mount(struct super_block *sb, int type);
  453. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  454. size_t len, loff_t off);
  455. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  456. const char *data, size_t len, loff_t off);
  457. static struct dquot_operations ext3_quota_operations = {
  458. .initialize = ext3_dquot_initialize,
  459. .drop = ext3_dquot_drop,
  460. .alloc_space = dquot_alloc_space,
  461. .alloc_inode = dquot_alloc_inode,
  462. .free_space = dquot_free_space,
  463. .free_inode = dquot_free_inode,
  464. .transfer = dquot_transfer,
  465. .write_dquot = ext3_write_dquot,
  466. .acquire_dquot = ext3_acquire_dquot,
  467. .release_dquot = ext3_release_dquot,
  468. .mark_dirty = ext3_mark_dquot_dirty,
  469. .write_info = ext3_write_info
  470. };
  471. static struct quotactl_ops ext3_qctl_operations = {
  472. .quota_on = ext3_quota_on,
  473. .quota_off = vfs_quota_off,
  474. .quota_sync = vfs_quota_sync,
  475. .get_info = vfs_get_dqinfo,
  476. .set_info = vfs_set_dqinfo,
  477. .get_dqblk = vfs_get_dqblk,
  478. .set_dqblk = vfs_set_dqblk
  479. };
  480. #endif
  481. static struct super_operations ext3_sops = {
  482. .alloc_inode = ext3_alloc_inode,
  483. .destroy_inode = ext3_destroy_inode,
  484. .read_inode = ext3_read_inode,
  485. .write_inode = ext3_write_inode,
  486. .dirty_inode = ext3_dirty_inode,
  487. .delete_inode = ext3_delete_inode,
  488. .put_super = ext3_put_super,
  489. .write_super = ext3_write_super,
  490. .sync_fs = ext3_sync_fs,
  491. .write_super_lockfs = ext3_write_super_lockfs,
  492. .unlockfs = ext3_unlockfs,
  493. .statfs = ext3_statfs,
  494. .remount_fs = ext3_remount,
  495. .clear_inode = ext3_clear_inode,
  496. #ifdef CONFIG_QUOTA
  497. .quota_read = ext3_quota_read,
  498. .quota_write = ext3_quota_write,
  499. #endif
  500. };
  501. struct dentry *ext3_get_parent(struct dentry *child);
  502. static struct export_operations ext3_export_ops = {
  503. .get_parent = ext3_get_parent,
  504. };
  505. enum {
  506. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  507. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  508. Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
  509. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  510. Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh,
  511. Opt_commit, Opt_journal_update, Opt_journal_inum,
  512. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  513. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  514. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0,
  515. Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
  516. };
  517. static match_table_t tokens = {
  518. {Opt_bsd_df, "bsddf"},
  519. {Opt_minix_df, "minixdf"},
  520. {Opt_grpid, "grpid"},
  521. {Opt_grpid, "bsdgroups"},
  522. {Opt_nogrpid, "nogrpid"},
  523. {Opt_nogrpid, "sysvgroups"},
  524. {Opt_resgid, "resgid=%u"},
  525. {Opt_resuid, "resuid=%u"},
  526. {Opt_sb, "sb=%u"},
  527. {Opt_err_cont, "errors=continue"},
  528. {Opt_err_panic, "errors=panic"},
  529. {Opt_err_ro, "errors=remount-ro"},
  530. {Opt_nouid32, "nouid32"},
  531. {Opt_nocheck, "nocheck"},
  532. {Opt_nocheck, "check=none"},
  533. {Opt_check, "check"},
  534. {Opt_debug, "debug"},
  535. {Opt_oldalloc, "oldalloc"},
  536. {Opt_orlov, "orlov"},
  537. {Opt_user_xattr, "user_xattr"},
  538. {Opt_nouser_xattr, "nouser_xattr"},
  539. {Opt_acl, "acl"},
  540. {Opt_noacl, "noacl"},
  541. {Opt_reservation, "reservation"},
  542. {Opt_noreservation, "noreservation"},
  543. {Opt_noload, "noload"},
  544. {Opt_nobh, "nobh"},
  545. {Opt_commit, "commit=%u"},
  546. {Opt_journal_update, "journal=update"},
  547. {Opt_journal_inum, "journal=%u"},
  548. {Opt_abort, "abort"},
  549. {Opt_data_journal, "data=journal"},
  550. {Opt_data_ordered, "data=ordered"},
  551. {Opt_data_writeback, "data=writeback"},
  552. {Opt_offusrjquota, "usrjquota="},
  553. {Opt_usrjquota, "usrjquota=%s"},
  554. {Opt_offgrpjquota, "grpjquota="},
  555. {Opt_grpjquota, "grpjquota=%s"},
  556. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  557. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  558. {Opt_ignore, "grpquota"},
  559. {Opt_ignore, "noquota"},
  560. {Opt_ignore, "quota"},
  561. {Opt_ignore, "usrquota"},
  562. {Opt_barrier, "barrier=%u"},
  563. {Opt_err, NULL},
  564. {Opt_resize, "resize"},
  565. };
  566. static unsigned long get_sb_block(void **data)
  567. {
  568. unsigned long sb_block;
  569. char *options = (char *) *data;
  570. if (!options || strncmp(options, "sb=", 3) != 0)
  571. return 1; /* Default location */
  572. options += 3;
  573. sb_block = simple_strtoul(options, &options, 0);
  574. if (*options && *options != ',') {
  575. printk("EXT3-fs: Invalid sb specification: %s\n",
  576. (char *) *data);
  577. return 1;
  578. }
  579. if (*options == ',')
  580. options++;
  581. *data = (void *) options;
  582. return sb_block;
  583. }
  584. static int parse_options (char * options, struct super_block *sb,
  585. unsigned long * inum, unsigned long *n_blocks_count, int is_remount)
  586. {
  587. struct ext3_sb_info *sbi = EXT3_SB(sb);
  588. char * p;
  589. substring_t args[MAX_OPT_ARGS];
  590. int data_opt = 0;
  591. int option;
  592. #ifdef CONFIG_QUOTA
  593. int qtype;
  594. char *qname;
  595. #endif
  596. if (!options)
  597. return 1;
  598. while ((p = strsep (&options, ",")) != NULL) {
  599. int token;
  600. if (!*p)
  601. continue;
  602. token = match_token(p, tokens, args);
  603. switch (token) {
  604. case Opt_bsd_df:
  605. clear_opt (sbi->s_mount_opt, MINIX_DF);
  606. break;
  607. case Opt_minix_df:
  608. set_opt (sbi->s_mount_opt, MINIX_DF);
  609. break;
  610. case Opt_grpid:
  611. set_opt (sbi->s_mount_opt, GRPID);
  612. break;
  613. case Opt_nogrpid:
  614. clear_opt (sbi->s_mount_opt, GRPID);
  615. break;
  616. case Opt_resuid:
  617. if (match_int(&args[0], &option))
  618. return 0;
  619. sbi->s_resuid = option;
  620. break;
  621. case Opt_resgid:
  622. if (match_int(&args[0], &option))
  623. return 0;
  624. sbi->s_resgid = option;
  625. break;
  626. case Opt_sb:
  627. /* handled by get_sb_block() instead of here */
  628. /* *sb_block = match_int(&args[0]); */
  629. break;
  630. case Opt_err_panic:
  631. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  632. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  633. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  634. break;
  635. case Opt_err_ro:
  636. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  637. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  638. set_opt (sbi->s_mount_opt, ERRORS_RO);
  639. break;
  640. case Opt_err_cont:
  641. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  642. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  643. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  644. break;
  645. case Opt_nouid32:
  646. set_opt (sbi->s_mount_opt, NO_UID32);
  647. break;
  648. case Opt_check:
  649. #ifdef CONFIG_EXT3_CHECK
  650. set_opt (sbi->s_mount_opt, CHECK);
  651. #else
  652. printk(KERN_ERR
  653. "EXT3 Check option not supported\n");
  654. #endif
  655. break;
  656. case Opt_nocheck:
  657. clear_opt (sbi->s_mount_opt, CHECK);
  658. break;
  659. case Opt_debug:
  660. set_opt (sbi->s_mount_opt, DEBUG);
  661. break;
  662. case Opt_oldalloc:
  663. set_opt (sbi->s_mount_opt, OLDALLOC);
  664. break;
  665. case Opt_orlov:
  666. clear_opt (sbi->s_mount_opt, OLDALLOC);
  667. break;
  668. #ifdef CONFIG_EXT3_FS_XATTR
  669. case Opt_user_xattr:
  670. set_opt (sbi->s_mount_opt, XATTR_USER);
  671. break;
  672. case Opt_nouser_xattr:
  673. clear_opt (sbi->s_mount_opt, XATTR_USER);
  674. break;
  675. #else
  676. case Opt_user_xattr:
  677. case Opt_nouser_xattr:
  678. printk("EXT3 (no)user_xattr options not supported\n");
  679. break;
  680. #endif
  681. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  682. case Opt_acl:
  683. set_opt(sbi->s_mount_opt, POSIX_ACL);
  684. break;
  685. case Opt_noacl:
  686. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  687. break;
  688. #else
  689. case Opt_acl:
  690. case Opt_noacl:
  691. printk("EXT3 (no)acl options not supported\n");
  692. break;
  693. #endif
  694. case Opt_reservation:
  695. set_opt(sbi->s_mount_opt, RESERVATION);
  696. break;
  697. case Opt_noreservation:
  698. clear_opt(sbi->s_mount_opt, RESERVATION);
  699. break;
  700. case Opt_journal_update:
  701. /* @@@ FIXME */
  702. /* Eventually we will want to be able to create
  703. a journal file here. For now, only allow the
  704. user to specify an existing inode to be the
  705. journal file. */
  706. if (is_remount) {
  707. printk(KERN_ERR "EXT3-fs: cannot specify "
  708. "journal on remount\n");
  709. return 0;
  710. }
  711. set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
  712. break;
  713. case Opt_journal_inum:
  714. if (is_remount) {
  715. printk(KERN_ERR "EXT3-fs: cannot specify "
  716. "journal on remount\n");
  717. return 0;
  718. }
  719. if (match_int(&args[0], &option))
  720. return 0;
  721. *inum = option;
  722. break;
  723. case Opt_noload:
  724. set_opt (sbi->s_mount_opt, NOLOAD);
  725. break;
  726. case Opt_commit:
  727. if (match_int(&args[0], &option))
  728. return 0;
  729. if (option < 0)
  730. return 0;
  731. if (option == 0)
  732. option = JBD_DEFAULT_MAX_COMMIT_AGE;
  733. sbi->s_commit_interval = HZ * option;
  734. break;
  735. case Opt_data_journal:
  736. data_opt = EXT3_MOUNT_JOURNAL_DATA;
  737. goto datacheck;
  738. case Opt_data_ordered:
  739. data_opt = EXT3_MOUNT_ORDERED_DATA;
  740. goto datacheck;
  741. case Opt_data_writeback:
  742. data_opt = EXT3_MOUNT_WRITEBACK_DATA;
  743. datacheck:
  744. if (is_remount) {
  745. if ((sbi->s_mount_opt & EXT3_MOUNT_DATA_FLAGS)
  746. != data_opt) {
  747. printk(KERN_ERR
  748. "EXT3-fs: cannot change data "
  749. "mode on remount\n");
  750. return 0;
  751. }
  752. } else {
  753. sbi->s_mount_opt &= ~EXT3_MOUNT_DATA_FLAGS;
  754. sbi->s_mount_opt |= data_opt;
  755. }
  756. break;
  757. #ifdef CONFIG_QUOTA
  758. case Opt_usrjquota:
  759. qtype = USRQUOTA;
  760. goto set_qf_name;
  761. case Opt_grpjquota:
  762. qtype = GRPQUOTA;
  763. set_qf_name:
  764. if (sb_any_quota_enabled(sb)) {
  765. printk(KERN_ERR
  766. "EXT3-fs: Cannot change journalled "
  767. "quota options when quota turned on.\n");
  768. return 0;
  769. }
  770. qname = match_strdup(&args[0]);
  771. if (!qname) {
  772. printk(KERN_ERR
  773. "EXT3-fs: not enough memory for "
  774. "storing quotafile name.\n");
  775. return 0;
  776. }
  777. if (sbi->s_qf_names[qtype] &&
  778. strcmp(sbi->s_qf_names[qtype], qname)) {
  779. printk(KERN_ERR
  780. "EXT3-fs: %s quota file already "
  781. "specified.\n", QTYPE2NAME(qtype));
  782. kfree(qname);
  783. return 0;
  784. }
  785. sbi->s_qf_names[qtype] = qname;
  786. if (strchr(sbi->s_qf_names[qtype], '/')) {
  787. printk(KERN_ERR
  788. "EXT3-fs: quotafile must be on "
  789. "filesystem root.\n");
  790. kfree(sbi->s_qf_names[qtype]);
  791. sbi->s_qf_names[qtype] = NULL;
  792. return 0;
  793. }
  794. break;
  795. case Opt_offusrjquota:
  796. qtype = USRQUOTA;
  797. goto clear_qf_name;
  798. case Opt_offgrpjquota:
  799. qtype = GRPQUOTA;
  800. clear_qf_name:
  801. if (sb_any_quota_enabled(sb)) {
  802. printk(KERN_ERR "EXT3-fs: Cannot change "
  803. "journalled quota options when "
  804. "quota turned on.\n");
  805. return 0;
  806. }
  807. kfree(sbi->s_qf_names[qtype]);
  808. sbi->s_qf_names[qtype] = NULL;
  809. break;
  810. case Opt_jqfmt_vfsold:
  811. sbi->s_jquota_fmt = QFMT_VFS_OLD;
  812. break;
  813. case Opt_jqfmt_vfsv0:
  814. sbi->s_jquota_fmt = QFMT_VFS_V0;
  815. break;
  816. #else
  817. case Opt_usrjquota:
  818. case Opt_grpjquota:
  819. case Opt_offusrjquota:
  820. case Opt_offgrpjquota:
  821. case Opt_jqfmt_vfsold:
  822. case Opt_jqfmt_vfsv0:
  823. printk(KERN_ERR
  824. "EXT3-fs: journalled quota options not "
  825. "supported.\n");
  826. break;
  827. #endif
  828. case Opt_abort:
  829. set_opt(sbi->s_mount_opt, ABORT);
  830. break;
  831. case Opt_barrier:
  832. if (match_int(&args[0], &option))
  833. return 0;
  834. if (option)
  835. set_opt(sbi->s_mount_opt, BARRIER);
  836. else
  837. clear_opt(sbi->s_mount_opt, BARRIER);
  838. break;
  839. case Opt_ignore:
  840. break;
  841. case Opt_resize:
  842. if (!n_blocks_count) {
  843. printk("EXT3-fs: resize option only available "
  844. "for remount\n");
  845. return 0;
  846. }
  847. match_int(&args[0], &option);
  848. *n_blocks_count = option;
  849. break;
  850. case Opt_nobh:
  851. set_opt(sbi->s_mount_opt, NOBH);
  852. break;
  853. default:
  854. printk (KERN_ERR
  855. "EXT3-fs: Unrecognized mount option \"%s\" "
  856. "or missing value\n", p);
  857. return 0;
  858. }
  859. }
  860. #ifdef CONFIG_QUOTA
  861. if (!sbi->s_jquota_fmt && (sbi->s_qf_names[USRQUOTA] ||
  862. sbi->s_qf_names[GRPQUOTA])) {
  863. printk(KERN_ERR
  864. "EXT3-fs: journalled quota format not specified.\n");
  865. return 0;
  866. }
  867. #endif
  868. return 1;
  869. }
  870. static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
  871. int read_only)
  872. {
  873. struct ext3_sb_info *sbi = EXT3_SB(sb);
  874. int res = 0;
  875. if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
  876. printk (KERN_ERR "EXT3-fs warning: revision level too high, "
  877. "forcing read-only mode\n");
  878. res = MS_RDONLY;
  879. }
  880. if (read_only)
  881. return res;
  882. if (!(sbi->s_mount_state & EXT3_VALID_FS))
  883. printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
  884. "running e2fsck is recommended\n");
  885. else if ((sbi->s_mount_state & EXT3_ERROR_FS))
  886. printk (KERN_WARNING
  887. "EXT3-fs warning: mounting fs with errors, "
  888. "running e2fsck is recommended\n");
  889. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  890. le16_to_cpu(es->s_mnt_count) >=
  891. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  892. printk (KERN_WARNING
  893. "EXT3-fs warning: maximal mount count reached, "
  894. "running e2fsck is recommended\n");
  895. else if (le32_to_cpu(es->s_checkinterval) &&
  896. (le32_to_cpu(es->s_lastcheck) +
  897. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  898. printk (KERN_WARNING
  899. "EXT3-fs warning: checktime reached, "
  900. "running e2fsck is recommended\n");
  901. #if 0
  902. /* @@@ We _will_ want to clear the valid bit if we find
  903. inconsistencies, to force a fsck at reboot. But for
  904. a plain journaled filesystem we can keep it set as
  905. valid forever! :) */
  906. es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
  907. #endif
  908. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  909. es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
  910. es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
  911. es->s_mtime = cpu_to_le32(get_seconds());
  912. ext3_update_dynamic_rev(sb);
  913. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  914. ext3_commit_super(sb, es, 1);
  915. if (test_opt(sb, DEBUG))
  916. printk(KERN_INFO "[EXT3 FS bs=%lu, gc=%lu, "
  917. "bpg=%lu, ipg=%lu, mo=%04lx]\n",
  918. sb->s_blocksize,
  919. sbi->s_groups_count,
  920. EXT3_BLOCKS_PER_GROUP(sb),
  921. EXT3_INODES_PER_GROUP(sb),
  922. sbi->s_mount_opt);
  923. printk(KERN_INFO "EXT3 FS on %s, ", sb->s_id);
  924. if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
  925. char b[BDEVNAME_SIZE];
  926. printk("external journal on %s\n",
  927. bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
  928. } else {
  929. printk("internal journal\n");
  930. }
  931. #ifdef CONFIG_EXT3_CHECK
  932. if (test_opt (sb, CHECK)) {
  933. ext3_check_blocks_bitmap (sb);
  934. ext3_check_inodes_bitmap (sb);
  935. }
  936. #endif
  937. return res;
  938. }
  939. /* Called at mount-time, super-block is locked */
  940. static int ext3_check_descriptors (struct super_block * sb)
  941. {
  942. struct ext3_sb_info *sbi = EXT3_SB(sb);
  943. unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
  944. struct ext3_group_desc * gdp = NULL;
  945. int desc_block = 0;
  946. int i;
  947. ext3_debug ("Checking group descriptors");
  948. for (i = 0; i < sbi->s_groups_count; i++)
  949. {
  950. if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
  951. gdp = (struct ext3_group_desc *)
  952. sbi->s_group_desc[desc_block++]->b_data;
  953. if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
  954. le32_to_cpu(gdp->bg_block_bitmap) >=
  955. block + EXT3_BLOCKS_PER_GROUP(sb))
  956. {
  957. ext3_error (sb, "ext3_check_descriptors",
  958. "Block bitmap for group %d"
  959. " not in group (block %lu)!",
  960. i, (unsigned long)
  961. le32_to_cpu(gdp->bg_block_bitmap));
  962. return 0;
  963. }
  964. if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
  965. le32_to_cpu(gdp->bg_inode_bitmap) >=
  966. block + EXT3_BLOCKS_PER_GROUP(sb))
  967. {
  968. ext3_error (sb, "ext3_check_descriptors",
  969. "Inode bitmap for group %d"
  970. " not in group (block %lu)!",
  971. i, (unsigned long)
  972. le32_to_cpu(gdp->bg_inode_bitmap));
  973. return 0;
  974. }
  975. if (le32_to_cpu(gdp->bg_inode_table) < block ||
  976. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
  977. block + EXT3_BLOCKS_PER_GROUP(sb))
  978. {
  979. ext3_error (sb, "ext3_check_descriptors",
  980. "Inode table for group %d"
  981. " not in group (block %lu)!",
  982. i, (unsigned long)
  983. le32_to_cpu(gdp->bg_inode_table));
  984. return 0;
  985. }
  986. block += EXT3_BLOCKS_PER_GROUP(sb);
  987. gdp++;
  988. }
  989. sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
  990. sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
  991. return 1;
  992. }
  993. /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
  994. * the superblock) which were deleted from all directories, but held open by
  995. * a process at the time of a crash. We walk the list and try to delete these
  996. * inodes at recovery time (only with a read-write filesystem).
  997. *
  998. * In order to keep the orphan inode chain consistent during traversal (in
  999. * case of crash during recovery), we link each inode into the superblock
  1000. * orphan list_head and handle it the same way as an inode deletion during
  1001. * normal operation (which journals the operations for us).
  1002. *
  1003. * We only do an iget() and an iput() on each inode, which is very safe if we
  1004. * accidentally point at an in-use or already deleted inode. The worst that
  1005. * can happen in this case is that we get a "bit already cleared" message from
  1006. * ext3_free_inode(). The only reason we would point at a wrong inode is if
  1007. * e2fsck was run on this filesystem, and it must have already done the orphan
  1008. * inode cleanup for us, so we can safely abort without any further action.
  1009. */
  1010. static void ext3_orphan_cleanup (struct super_block * sb,
  1011. struct ext3_super_block * es)
  1012. {
  1013. unsigned int s_flags = sb->s_flags;
  1014. int nr_orphans = 0, nr_truncates = 0;
  1015. #ifdef CONFIG_QUOTA
  1016. int i;
  1017. #endif
  1018. if (!es->s_last_orphan) {
  1019. jbd_debug(4, "no orphan inodes to clean up\n");
  1020. return;
  1021. }
  1022. if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
  1023. if (es->s_last_orphan)
  1024. jbd_debug(1, "Errors on filesystem, "
  1025. "clearing orphan list.\n");
  1026. es->s_last_orphan = 0;
  1027. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1028. return;
  1029. }
  1030. if (s_flags & MS_RDONLY) {
  1031. printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
  1032. sb->s_id);
  1033. sb->s_flags &= ~MS_RDONLY;
  1034. }
  1035. #ifdef CONFIG_QUOTA
  1036. /* Needed for iput() to work correctly and not trash data */
  1037. sb->s_flags |= MS_ACTIVE;
  1038. /* Turn on quotas so that they are updated correctly */
  1039. for (i = 0; i < MAXQUOTAS; i++) {
  1040. if (EXT3_SB(sb)->s_qf_names[i]) {
  1041. int ret = ext3_quota_on_mount(sb, i);
  1042. if (ret < 0)
  1043. printk(KERN_ERR
  1044. "EXT3-fs: Cannot turn on journalled "
  1045. "quota: error %d\n", ret);
  1046. }
  1047. }
  1048. #endif
  1049. while (es->s_last_orphan) {
  1050. struct inode *inode;
  1051. if (!(inode =
  1052. ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
  1053. es->s_last_orphan = 0;
  1054. break;
  1055. }
  1056. list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
  1057. DQUOT_INIT(inode);
  1058. if (inode->i_nlink) {
  1059. printk(KERN_DEBUG
  1060. "%s: truncating inode %ld to %Ld bytes\n",
  1061. __FUNCTION__, inode->i_ino, inode->i_size);
  1062. jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
  1063. inode->i_ino, inode->i_size);
  1064. ext3_truncate(inode);
  1065. nr_truncates++;
  1066. } else {
  1067. printk(KERN_DEBUG
  1068. "%s: deleting unreferenced inode %ld\n",
  1069. __FUNCTION__, inode->i_ino);
  1070. jbd_debug(2, "deleting unreferenced inode %ld\n",
  1071. inode->i_ino);
  1072. nr_orphans++;
  1073. }
  1074. iput(inode); /* The delete magic happens here! */
  1075. }
  1076. #define PLURAL(x) (x), ((x)==1) ? "" : "s"
  1077. if (nr_orphans)
  1078. printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
  1079. sb->s_id, PLURAL(nr_orphans));
  1080. if (nr_truncates)
  1081. printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
  1082. sb->s_id, PLURAL(nr_truncates));
  1083. #ifdef CONFIG_QUOTA
  1084. /* Turn quotas off */
  1085. for (i = 0; i < MAXQUOTAS; i++) {
  1086. if (sb_dqopt(sb)->files[i])
  1087. vfs_quota_off(sb, i);
  1088. }
  1089. #endif
  1090. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1091. }
  1092. #define log2(n) ffz(~(n))
  1093. /*
  1094. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  1095. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  1096. * We need to be 1 filesystem block less than the 2^32 sector limit.
  1097. */
  1098. static loff_t ext3_max_size(int bits)
  1099. {
  1100. loff_t res = EXT3_NDIR_BLOCKS;
  1101. /* This constant is calculated to be the largest file size for a
  1102. * dense, 4k-blocksize file such that the total number of
  1103. * sectors in the file, including data and all indirect blocks,
  1104. * does not exceed 2^32. */
  1105. const loff_t upper_limit = 0x1ff7fffd000LL;
  1106. res += 1LL << (bits-2);
  1107. res += 1LL << (2*(bits-2));
  1108. res += 1LL << (3*(bits-2));
  1109. res <<= bits;
  1110. if (res > upper_limit)
  1111. res = upper_limit;
  1112. return res;
  1113. }
  1114. static unsigned long descriptor_loc(struct super_block *sb,
  1115. unsigned long logic_sb_block,
  1116. int nr)
  1117. {
  1118. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1119. unsigned long bg, first_data_block, first_meta_bg;
  1120. int has_super = 0;
  1121. first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1122. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1123. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
  1124. nr < first_meta_bg)
  1125. return (logic_sb_block + nr + 1);
  1126. bg = sbi->s_desc_per_block * nr;
  1127. if (ext3_bg_has_super(sb, bg))
  1128. has_super = 1;
  1129. return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
  1130. }
  1131. static int ext3_fill_super (struct super_block *sb, void *data, int silent)
  1132. {
  1133. struct buffer_head * bh;
  1134. struct ext3_super_block *es = NULL;
  1135. struct ext3_sb_info *sbi;
  1136. unsigned long block;
  1137. unsigned long sb_block = get_sb_block(&data);
  1138. unsigned long logic_sb_block;
  1139. unsigned long offset = 0;
  1140. unsigned long journal_inum = 0;
  1141. unsigned long def_mount_opts;
  1142. struct inode *root;
  1143. int blocksize;
  1144. int hblock;
  1145. int db_count;
  1146. int i;
  1147. int needs_recovery;
  1148. __le32 features;
  1149. sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
  1150. if (!sbi)
  1151. return -ENOMEM;
  1152. sb->s_fs_info = sbi;
  1153. memset(sbi, 0, sizeof(*sbi));
  1154. sbi->s_mount_opt = 0;
  1155. sbi->s_resuid = EXT3_DEF_RESUID;
  1156. sbi->s_resgid = EXT3_DEF_RESGID;
  1157. unlock_kernel();
  1158. blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
  1159. if (!blocksize) {
  1160. printk(KERN_ERR "EXT3-fs: unable to set blocksize\n");
  1161. goto out_fail;
  1162. }
  1163. /*
  1164. * The ext3 superblock will not be buffer aligned for other than 1kB
  1165. * block sizes. We need to calculate the offset from buffer start.
  1166. */
  1167. if (blocksize != EXT3_MIN_BLOCK_SIZE) {
  1168. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1169. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1170. } else {
  1171. logic_sb_block = sb_block;
  1172. }
  1173. if (!(bh = sb_bread(sb, logic_sb_block))) {
  1174. printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
  1175. goto out_fail;
  1176. }
  1177. /*
  1178. * Note: s_es must be initialized as soon as possible because
  1179. * some ext3 macro-instructions depend on its value
  1180. */
  1181. es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
  1182. sbi->s_es = es;
  1183. sb->s_magic = le16_to_cpu(es->s_magic);
  1184. if (sb->s_magic != EXT3_SUPER_MAGIC)
  1185. goto cantfind_ext3;
  1186. /* Set defaults before we parse the mount options */
  1187. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1188. if (def_mount_opts & EXT3_DEFM_DEBUG)
  1189. set_opt(sbi->s_mount_opt, DEBUG);
  1190. if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
  1191. set_opt(sbi->s_mount_opt, GRPID);
  1192. if (def_mount_opts & EXT3_DEFM_UID16)
  1193. set_opt(sbi->s_mount_opt, NO_UID32);
  1194. if (def_mount_opts & EXT3_DEFM_XATTR_USER)
  1195. set_opt(sbi->s_mount_opt, XATTR_USER);
  1196. if (def_mount_opts & EXT3_DEFM_ACL)
  1197. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1198. if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
  1199. sbi->s_mount_opt |= EXT3_MOUNT_JOURNAL_DATA;
  1200. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
  1201. sbi->s_mount_opt |= EXT3_MOUNT_ORDERED_DATA;
  1202. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
  1203. sbi->s_mount_opt |= EXT3_MOUNT_WRITEBACK_DATA;
  1204. if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
  1205. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1206. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_RO)
  1207. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1208. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  1209. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  1210. set_opt(sbi->s_mount_opt, RESERVATION);
  1211. if (!parse_options ((char *) data, sb, &journal_inum, NULL, 0))
  1212. goto failed_mount;
  1213. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1214. ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1215. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
  1216. (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1217. EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1218. EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1219. printk(KERN_WARNING
  1220. "EXT3-fs warning: feature flags set on rev 0 fs, "
  1221. "running e2fsck is recommended\n");
  1222. /*
  1223. * Check feature flags regardless of the revision level, since we
  1224. * previously didn't change the revision level when setting the flags,
  1225. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1226. */
  1227. features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
  1228. if (features) {
  1229. printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
  1230. "unsupported optional features (%x).\n",
  1231. sb->s_id, le32_to_cpu(features));
  1232. goto failed_mount;
  1233. }
  1234. features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
  1235. if (!(sb->s_flags & MS_RDONLY) && features) {
  1236. printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
  1237. "unsupported optional features (%x).\n",
  1238. sb->s_id, le32_to_cpu(features));
  1239. goto failed_mount;
  1240. }
  1241. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1242. if (blocksize < EXT3_MIN_BLOCK_SIZE ||
  1243. blocksize > EXT3_MAX_BLOCK_SIZE) {
  1244. printk(KERN_ERR
  1245. "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
  1246. blocksize, sb->s_id);
  1247. goto failed_mount;
  1248. }
  1249. hblock = bdev_hardsect_size(sb->s_bdev);
  1250. if (sb->s_blocksize != blocksize) {
  1251. /*
  1252. * Make sure the blocksize for the filesystem is larger
  1253. * than the hardware sectorsize for the machine.
  1254. */
  1255. if (blocksize < hblock) {
  1256. printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
  1257. "device blocksize %d.\n", blocksize, hblock);
  1258. goto failed_mount;
  1259. }
  1260. brelse (bh);
  1261. sb_set_blocksize(sb, blocksize);
  1262. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1263. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1264. bh = sb_bread(sb, logic_sb_block);
  1265. if (!bh) {
  1266. printk(KERN_ERR
  1267. "EXT3-fs: Can't read superblock on 2nd try.\n");
  1268. goto failed_mount;
  1269. }
  1270. es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
  1271. sbi->s_es = es;
  1272. if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
  1273. printk (KERN_ERR
  1274. "EXT3-fs: Magic mismatch, very weird !\n");
  1275. goto failed_mount;
  1276. }
  1277. }
  1278. sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
  1279. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
  1280. sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
  1281. sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
  1282. } else {
  1283. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1284. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1285. if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
  1286. (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
  1287. (sbi->s_inode_size > blocksize)) {
  1288. printk (KERN_ERR
  1289. "EXT3-fs: unsupported inode size: %d\n",
  1290. sbi->s_inode_size);
  1291. goto failed_mount;
  1292. }
  1293. }
  1294. sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
  1295. le32_to_cpu(es->s_log_frag_size);
  1296. if (blocksize != sbi->s_frag_size) {
  1297. printk(KERN_ERR
  1298. "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
  1299. sbi->s_frag_size, blocksize);
  1300. goto failed_mount;
  1301. }
  1302. sbi->s_frags_per_block = 1;
  1303. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1304. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1305. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1306. if (EXT3_INODE_SIZE(sb) == 0)
  1307. goto cantfind_ext3;
  1308. sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
  1309. if (sbi->s_inodes_per_block == 0)
  1310. goto cantfind_ext3;
  1311. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1312. sbi->s_inodes_per_block;
  1313. sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
  1314. sbi->s_sbh = bh;
  1315. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1316. sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
  1317. sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
  1318. for (i=0; i < 4; i++)
  1319. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1320. sbi->s_def_hash_version = es->s_def_hash_version;
  1321. if (sbi->s_blocks_per_group > blocksize * 8) {
  1322. printk (KERN_ERR
  1323. "EXT3-fs: #blocks per group too big: %lu\n",
  1324. sbi->s_blocks_per_group);
  1325. goto failed_mount;
  1326. }
  1327. if (sbi->s_frags_per_group > blocksize * 8) {
  1328. printk (KERN_ERR
  1329. "EXT3-fs: #fragments per group too big: %lu\n",
  1330. sbi->s_frags_per_group);
  1331. goto failed_mount;
  1332. }
  1333. if (sbi->s_inodes_per_group > blocksize * 8) {
  1334. printk (KERN_ERR
  1335. "EXT3-fs: #inodes per group too big: %lu\n",
  1336. sbi->s_inodes_per_group);
  1337. goto failed_mount;
  1338. }
  1339. if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
  1340. goto cantfind_ext3;
  1341. sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
  1342. le32_to_cpu(es->s_first_data_block) +
  1343. EXT3_BLOCKS_PER_GROUP(sb) - 1) /
  1344. EXT3_BLOCKS_PER_GROUP(sb);
  1345. db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
  1346. EXT3_DESC_PER_BLOCK(sb);
  1347. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1348. GFP_KERNEL);
  1349. if (sbi->s_group_desc == NULL) {
  1350. printk (KERN_ERR "EXT3-fs: not enough memory\n");
  1351. goto failed_mount;
  1352. }
  1353. percpu_counter_init(&sbi->s_freeblocks_counter);
  1354. percpu_counter_init(&sbi->s_freeinodes_counter);
  1355. percpu_counter_init(&sbi->s_dirs_counter);
  1356. bgl_lock_init(&sbi->s_blockgroup_lock);
  1357. for (i = 0; i < db_count; i++) {
  1358. block = descriptor_loc(sb, logic_sb_block, i);
  1359. sbi->s_group_desc[i] = sb_bread(sb, block);
  1360. if (!sbi->s_group_desc[i]) {
  1361. printk (KERN_ERR "EXT3-fs: "
  1362. "can't read group descriptor %d\n", i);
  1363. db_count = i;
  1364. goto failed_mount2;
  1365. }
  1366. }
  1367. if (!ext3_check_descriptors (sb)) {
  1368. printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
  1369. goto failed_mount2;
  1370. }
  1371. sbi->s_gdb_count = db_count;
  1372. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1373. spin_lock_init(&sbi->s_next_gen_lock);
  1374. /* per fileystem reservation list head & lock */
  1375. spin_lock_init(&sbi->s_rsv_window_lock);
  1376. sbi->s_rsv_window_root = RB_ROOT;
  1377. /* Add a single, static dummy reservation to the start of the
  1378. * reservation window list --- it gives us a placeholder for
  1379. * append-at-start-of-list which makes the allocation logic
  1380. * _much_ simpler. */
  1381. sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1382. sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1383. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1384. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1385. ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1386. /*
  1387. * set up enough so that it can read an inode
  1388. */
  1389. sb->s_op = &ext3_sops;
  1390. sb->s_export_op = &ext3_export_ops;
  1391. sb->s_xattr = ext3_xattr_handlers;
  1392. #ifdef CONFIG_QUOTA
  1393. sb->s_qcop = &ext3_qctl_operations;
  1394. sb->dq_op = &ext3_quota_operations;
  1395. #endif
  1396. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1397. sb->s_root = NULL;
  1398. needs_recovery = (es->s_last_orphan != 0 ||
  1399. EXT3_HAS_INCOMPAT_FEATURE(sb,
  1400. EXT3_FEATURE_INCOMPAT_RECOVER));
  1401. /*
  1402. * The first inode we look at is the journal inode. Don't try
  1403. * root first: it may be modified in the journal!
  1404. */
  1405. if (!test_opt(sb, NOLOAD) &&
  1406. EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
  1407. if (ext3_load_journal(sb, es))
  1408. goto failed_mount2;
  1409. } else if (journal_inum) {
  1410. if (ext3_create_journal(sb, es, journal_inum))
  1411. goto failed_mount2;
  1412. } else {
  1413. if (!silent)
  1414. printk (KERN_ERR
  1415. "ext3: No journal on filesystem on %s\n",
  1416. sb->s_id);
  1417. goto failed_mount2;
  1418. }
  1419. /* We have now updated the journal if required, so we can
  1420. * validate the data journaling mode. */
  1421. switch (test_opt(sb, DATA_FLAGS)) {
  1422. case 0:
  1423. /* No mode set, assume a default based on the journal
  1424. capabilities: ORDERED_DATA if the journal can
  1425. cope, else JOURNAL_DATA */
  1426. if (journal_check_available_features
  1427. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
  1428. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1429. else
  1430. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1431. break;
  1432. case EXT3_MOUNT_ORDERED_DATA:
  1433. case EXT3_MOUNT_WRITEBACK_DATA:
  1434. if (!journal_check_available_features
  1435. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
  1436. printk(KERN_ERR "EXT3-fs: Journal does not support "
  1437. "requested data journaling mode\n");
  1438. goto failed_mount3;
  1439. }
  1440. default:
  1441. break;
  1442. }
  1443. if (test_opt(sb, NOBH)) {
  1444. if (sb->s_blocksize_bits != PAGE_CACHE_SHIFT) {
  1445. printk(KERN_WARNING "EXT3-fs: Ignoring nobh option "
  1446. "since filesystem blocksize doesn't match "
  1447. "pagesize\n");
  1448. clear_opt(sbi->s_mount_opt, NOBH);
  1449. }
  1450. if (!(test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_WRITEBACK_DATA)) {
  1451. printk(KERN_WARNING "EXT3-fs: Ignoring nobh option - "
  1452. "its supported only with writeback mode\n");
  1453. clear_opt(sbi->s_mount_opt, NOBH);
  1454. }
  1455. }
  1456. /*
  1457. * The journal_load will have done any necessary log recovery,
  1458. * so we can safely mount the rest of the filesystem now.
  1459. */
  1460. root = iget(sb, EXT3_ROOT_INO);
  1461. sb->s_root = d_alloc_root(root);
  1462. if (!sb->s_root) {
  1463. printk(KERN_ERR "EXT3-fs: get root inode failed\n");
  1464. iput(root);
  1465. goto failed_mount3;
  1466. }
  1467. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1468. dput(sb->s_root);
  1469. sb->s_root = NULL;
  1470. printk(KERN_ERR "EXT3-fs: corrupt root inode, run e2fsck\n");
  1471. goto failed_mount3;
  1472. }
  1473. ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  1474. /*
  1475. * akpm: core read_super() calls in here with the superblock locked.
  1476. * That deadlocks, because orphan cleanup needs to lock the superblock
  1477. * in numerous places. Here we just pop the lock - it's relatively
  1478. * harmless, because we are now ready to accept write_super() requests,
  1479. * and aviro says that's the only reason for hanging onto the
  1480. * superblock lock.
  1481. */
  1482. EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
  1483. ext3_orphan_cleanup(sb, es);
  1484. EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
  1485. if (needs_recovery)
  1486. printk (KERN_INFO "EXT3-fs: recovery complete.\n");
  1487. ext3_mark_recovery_complete(sb, es);
  1488. printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
  1489. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
  1490. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
  1491. "writeback");
  1492. percpu_counter_mod(&sbi->s_freeblocks_counter,
  1493. ext3_count_free_blocks(sb));
  1494. percpu_counter_mod(&sbi->s_freeinodes_counter,
  1495. ext3_count_free_inodes(sb));
  1496. percpu_counter_mod(&sbi->s_dirs_counter,
  1497. ext3_count_dirs(sb));
  1498. lock_kernel();
  1499. return 0;
  1500. cantfind_ext3:
  1501. if (!silent)
  1502. printk(KERN_ERR "VFS: Can't find ext3 filesystem on dev %s.\n",
  1503. sb->s_id);
  1504. goto failed_mount;
  1505. failed_mount3:
  1506. journal_destroy(sbi->s_journal);
  1507. failed_mount2:
  1508. for (i = 0; i < db_count; i++)
  1509. brelse(sbi->s_group_desc[i]);
  1510. kfree(sbi->s_group_desc);
  1511. failed_mount:
  1512. #ifdef CONFIG_QUOTA
  1513. for (i = 0; i < MAXQUOTAS; i++)
  1514. kfree(sbi->s_qf_names[i]);
  1515. #endif
  1516. ext3_blkdev_remove(sbi);
  1517. brelse(bh);
  1518. out_fail:
  1519. sb->s_fs_info = NULL;
  1520. kfree(sbi);
  1521. lock_kernel();
  1522. return -EINVAL;
  1523. }
  1524. /*
  1525. * Setup any per-fs journal parameters now. We'll do this both on
  1526. * initial mount, once the journal has been initialised but before we've
  1527. * done any recovery; and again on any subsequent remount.
  1528. */
  1529. static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
  1530. {
  1531. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1532. if (sbi->s_commit_interval)
  1533. journal->j_commit_interval = sbi->s_commit_interval;
  1534. /* We could also set up an ext3-specific default for the commit
  1535. * interval here, but for now we'll just fall back to the jbd
  1536. * default. */
  1537. spin_lock(&journal->j_state_lock);
  1538. if (test_opt(sb, BARRIER))
  1539. journal->j_flags |= JFS_BARRIER;
  1540. else
  1541. journal->j_flags &= ~JFS_BARRIER;
  1542. spin_unlock(&journal->j_state_lock);
  1543. }
  1544. static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
  1545. {
  1546. struct inode *journal_inode;
  1547. journal_t *journal;
  1548. /* First, test for the existence of a valid inode on disk. Bad
  1549. * things happen if we iget() an unused inode, as the subsequent
  1550. * iput() will try to delete it. */
  1551. journal_inode = iget(sb, journal_inum);
  1552. if (!journal_inode) {
  1553. printk(KERN_ERR "EXT3-fs: no journal found.\n");
  1554. return NULL;
  1555. }
  1556. if (!journal_inode->i_nlink) {
  1557. make_bad_inode(journal_inode);
  1558. iput(journal_inode);
  1559. printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
  1560. return NULL;
  1561. }
  1562. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1563. journal_inode, journal_inode->i_size);
  1564. if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
  1565. printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
  1566. iput(journal_inode);
  1567. return NULL;
  1568. }
  1569. journal = journal_init_inode(journal_inode);
  1570. if (!journal) {
  1571. printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
  1572. iput(journal_inode);
  1573. return NULL;
  1574. }
  1575. journal->j_private = sb;
  1576. ext3_init_journal_params(sb, journal);
  1577. return journal;
  1578. }
  1579. static journal_t *ext3_get_dev_journal(struct super_block *sb,
  1580. dev_t j_dev)
  1581. {
  1582. struct buffer_head * bh;
  1583. journal_t *journal;
  1584. int start;
  1585. int len;
  1586. int hblock, blocksize;
  1587. unsigned long sb_block;
  1588. unsigned long offset;
  1589. struct ext3_super_block * es;
  1590. struct block_device *bdev;
  1591. bdev = ext3_blkdev_get(j_dev);
  1592. if (bdev == NULL)
  1593. return NULL;
  1594. if (bd_claim(bdev, sb)) {
  1595. printk(KERN_ERR
  1596. "EXT3: failed to claim external journal device.\n");
  1597. blkdev_put(bdev);
  1598. return NULL;
  1599. }
  1600. blocksize = sb->s_blocksize;
  1601. hblock = bdev_hardsect_size(bdev);
  1602. if (blocksize < hblock) {
  1603. printk(KERN_ERR
  1604. "EXT3-fs: blocksize too small for journal device.\n");
  1605. goto out_bdev;
  1606. }
  1607. sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
  1608. offset = EXT3_MIN_BLOCK_SIZE % blocksize;
  1609. set_blocksize(bdev, blocksize);
  1610. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  1611. printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
  1612. "external journal\n");
  1613. goto out_bdev;
  1614. }
  1615. es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
  1616. if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
  1617. !(le32_to_cpu(es->s_feature_incompat) &
  1618. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  1619. printk(KERN_ERR "EXT3-fs: external journal has "
  1620. "bad superblock\n");
  1621. brelse(bh);
  1622. goto out_bdev;
  1623. }
  1624. if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  1625. printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
  1626. brelse(bh);
  1627. goto out_bdev;
  1628. }
  1629. len = le32_to_cpu(es->s_blocks_count);
  1630. start = sb_block + 1;
  1631. brelse(bh); /* we're done with the superblock */
  1632. journal = journal_init_dev(bdev, sb->s_bdev,
  1633. start, len, blocksize);
  1634. if (!journal) {
  1635. printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
  1636. goto out_bdev;
  1637. }
  1638. journal->j_private = sb;
  1639. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  1640. wait_on_buffer(journal->j_sb_buffer);
  1641. if (!buffer_uptodate(journal->j_sb_buffer)) {
  1642. printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
  1643. goto out_journal;
  1644. }
  1645. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  1646. printk(KERN_ERR "EXT3-fs: External journal has more than one "
  1647. "user (unsupported) - %d\n",
  1648. be32_to_cpu(journal->j_superblock->s_nr_users));
  1649. goto out_journal;
  1650. }
  1651. EXT3_SB(sb)->journal_bdev = bdev;
  1652. ext3_init_journal_params(sb, journal);
  1653. return journal;
  1654. out_journal:
  1655. journal_destroy(journal);
  1656. out_bdev:
  1657. ext3_blkdev_put(bdev);
  1658. return NULL;
  1659. }
  1660. static int ext3_load_journal(struct super_block * sb,
  1661. struct ext3_super_block * es)
  1662. {
  1663. journal_t *journal;
  1664. int journal_inum = le32_to_cpu(es->s_journal_inum);
  1665. dev_t journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  1666. int err = 0;
  1667. int really_read_only;
  1668. really_read_only = bdev_read_only(sb->s_bdev);
  1669. /*
  1670. * Are we loading a blank journal or performing recovery after a
  1671. * crash? For recovery, we need to check in advance whether we
  1672. * can get read-write access to the device.
  1673. */
  1674. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
  1675. if (sb->s_flags & MS_RDONLY) {
  1676. printk(KERN_INFO "EXT3-fs: INFO: recovery "
  1677. "required on readonly filesystem.\n");
  1678. if (really_read_only) {
  1679. printk(KERN_ERR "EXT3-fs: write access "
  1680. "unavailable, cannot proceed.\n");
  1681. return -EROFS;
  1682. }
  1683. printk (KERN_INFO "EXT3-fs: write access will "
  1684. "be enabled during recovery.\n");
  1685. }
  1686. }
  1687. if (journal_inum && journal_dev) {
  1688. printk(KERN_ERR "EXT3-fs: filesystem has both journal "
  1689. "and inode journals!\n");
  1690. return -EINVAL;
  1691. }
  1692. if (journal_inum) {
  1693. if (!(journal = ext3_get_journal(sb, journal_inum)))
  1694. return -EINVAL;
  1695. } else {
  1696. if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
  1697. return -EINVAL;
  1698. }
  1699. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  1700. err = journal_update_format(journal);
  1701. if (err) {
  1702. printk(KERN_ERR "EXT3-fs: error updating journal.\n");
  1703. journal_destroy(journal);
  1704. return err;
  1705. }
  1706. }
  1707. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
  1708. err = journal_wipe(journal, !really_read_only);
  1709. if (!err)
  1710. err = journal_load(journal);
  1711. if (err) {
  1712. printk(KERN_ERR "EXT3-fs: error loading journal.\n");
  1713. journal_destroy(journal);
  1714. return err;
  1715. }
  1716. EXT3_SB(sb)->s_journal = journal;
  1717. ext3_clear_journal_err(sb, es);
  1718. return 0;
  1719. }
  1720. static int ext3_create_journal(struct super_block * sb,
  1721. struct ext3_super_block * es,
  1722. int journal_inum)
  1723. {
  1724. journal_t *journal;
  1725. if (sb->s_flags & MS_RDONLY) {
  1726. printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
  1727. "create journal.\n");
  1728. return -EROFS;
  1729. }
  1730. if (!(journal = ext3_get_journal(sb, journal_inum)))
  1731. return -EINVAL;
  1732. printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
  1733. journal_inum);
  1734. if (journal_create(journal)) {
  1735. printk(KERN_ERR "EXT3-fs: error creating journal.\n");
  1736. journal_destroy(journal);
  1737. return -EIO;
  1738. }
  1739. EXT3_SB(sb)->s_journal = journal;
  1740. ext3_update_dynamic_rev(sb);
  1741. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1742. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
  1743. es->s_journal_inum = cpu_to_le32(journal_inum);
  1744. sb->s_dirt = 1;
  1745. /* Make sure we flush the recovery flag to disk. */
  1746. ext3_commit_super(sb, es, 1);
  1747. return 0;
  1748. }
  1749. static void ext3_commit_super (struct super_block * sb,
  1750. struct ext3_super_block * es,
  1751. int sync)
  1752. {
  1753. struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
  1754. if (!sbh)
  1755. return;
  1756. es->s_wtime = cpu_to_le32(get_seconds());
  1757. es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
  1758. es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
  1759. BUFFER_TRACE(sbh, "marking dirty");
  1760. mark_buffer_dirty(sbh);
  1761. if (sync)
  1762. sync_dirty_buffer(sbh);
  1763. }
  1764. /*
  1765. * Have we just finished recovery? If so, and if we are mounting (or
  1766. * remounting) the filesystem readonly, then we will end up with a
  1767. * consistent fs on disk. Record that fact.
  1768. */
  1769. static void ext3_mark_recovery_complete(struct super_block * sb,
  1770. struct ext3_super_block * es)
  1771. {
  1772. journal_t *journal = EXT3_SB(sb)->s_journal;
  1773. journal_lock_updates(journal);
  1774. journal_flush(journal);
  1775. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
  1776. sb->s_flags & MS_RDONLY) {
  1777. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1778. sb->s_dirt = 0;
  1779. ext3_commit_super(sb, es, 1);
  1780. }
  1781. journal_unlock_updates(journal);
  1782. }
  1783. /*
  1784. * If we are mounting (or read-write remounting) a filesystem whose journal
  1785. * has recorded an error from a previous lifetime, move that error to the
  1786. * main filesystem now.
  1787. */
  1788. static void ext3_clear_journal_err(struct super_block * sb,
  1789. struct ext3_super_block * es)
  1790. {
  1791. journal_t *journal;
  1792. int j_errno;
  1793. const char *errstr;
  1794. journal = EXT3_SB(sb)->s_journal;
  1795. /*
  1796. * Now check for any error status which may have been recorded in the
  1797. * journal by a prior ext3_error() or ext3_abort()
  1798. */
  1799. j_errno = journal_errno(journal);
  1800. if (j_errno) {
  1801. char nbuf[16];
  1802. errstr = ext3_decode_error(sb, j_errno, nbuf);
  1803. ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
  1804. "from previous mount: %s", errstr);
  1805. ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
  1806. "filesystem check.");
  1807. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  1808. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  1809. ext3_commit_super (sb, es, 1);
  1810. journal_clear_err(journal);
  1811. }
  1812. }
  1813. /*
  1814. * Force the running and committing transactions to commit,
  1815. * and wait on the commit.
  1816. */
  1817. int ext3_force_commit(struct super_block *sb)
  1818. {
  1819. journal_t *journal;
  1820. int ret;
  1821. if (sb->s_flags & MS_RDONLY)
  1822. return 0;
  1823. journal = EXT3_SB(sb)->s_journal;
  1824. sb->s_dirt = 0;
  1825. ret = ext3_journal_force_commit(journal);
  1826. return ret;
  1827. }
  1828. /*
  1829. * Ext3 always journals updates to the superblock itself, so we don't
  1830. * have to propagate any other updates to the superblock on disk at this
  1831. * point. Just start an async writeback to get the buffers on their way
  1832. * to the disk.
  1833. *
  1834. * This implicitly triggers the writebehind on sync().
  1835. */
  1836. static void ext3_write_super (struct super_block * sb)
  1837. {
  1838. if (down_trylock(&sb->s_lock) == 0)
  1839. BUG();
  1840. sb->s_dirt = 0;
  1841. }
  1842. static int ext3_sync_fs(struct super_block *sb, int wait)
  1843. {
  1844. tid_t target;
  1845. sb->s_dirt = 0;
  1846. if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
  1847. if (wait)
  1848. log_wait_commit(EXT3_SB(sb)->s_journal, target);
  1849. }
  1850. return 0;
  1851. }
  1852. /*
  1853. * LVM calls this function before a (read-only) snapshot is created. This
  1854. * gives us a chance to flush the journal completely and mark the fs clean.
  1855. */
  1856. static void ext3_write_super_lockfs(struct super_block *sb)
  1857. {
  1858. sb->s_dirt = 0;
  1859. if (!(sb->s_flags & MS_RDONLY)) {
  1860. journal_t *journal = EXT3_SB(sb)->s_journal;
  1861. /* Now we set up the journal barrier. */
  1862. journal_lock_updates(journal);
  1863. journal_flush(journal);
  1864. /* Journal blocked and flushed, clear needs_recovery flag. */
  1865. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1866. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  1867. }
  1868. }
  1869. /*
  1870. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  1871. * flag here, even though the filesystem is not technically dirty yet.
  1872. */
  1873. static void ext3_unlockfs(struct super_block *sb)
  1874. {
  1875. if (!(sb->s_flags & MS_RDONLY)) {
  1876. lock_super(sb);
  1877. /* Reser the needs_recovery flag before the fs is unlocked. */
  1878. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1879. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  1880. unlock_super(sb);
  1881. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  1882. }
  1883. }
  1884. static int ext3_remount (struct super_block * sb, int * flags, char * data)
  1885. {
  1886. struct ext3_super_block * es;
  1887. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1888. unsigned long tmp;
  1889. unsigned long n_blocks_count = 0;
  1890. /*
  1891. * Allow the "check" option to be passed as a remount option.
  1892. */
  1893. if (!parse_options(data, sb, &tmp, &n_blocks_count, 1))
  1894. return -EINVAL;
  1895. if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
  1896. ext3_abort(sb, __FUNCTION__, "Abort forced by user");
  1897. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1898. ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1899. es = sbi->s_es;
  1900. ext3_init_journal_params(sb, sbi->s_journal);
  1901. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  1902. n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
  1903. if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
  1904. return -EROFS;
  1905. if (*flags & MS_RDONLY) {
  1906. /*
  1907. * First of all, the unconditional stuff we have to do
  1908. * to disable replay of the journal when we next remount
  1909. */
  1910. sb->s_flags |= MS_RDONLY;
  1911. /*
  1912. * OK, test if we are remounting a valid rw partition
  1913. * readonly, and if so set the rdonly flag and then
  1914. * mark the partition as valid again.
  1915. */
  1916. if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
  1917. (sbi->s_mount_state & EXT3_VALID_FS))
  1918. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1919. ext3_mark_recovery_complete(sb, es);
  1920. } else {
  1921. __le32 ret;
  1922. if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1923. ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
  1924. printk(KERN_WARNING "EXT3-fs: %s: couldn't "
  1925. "remount RDWR because of unsupported "
  1926. "optional features (%x).\n",
  1927. sb->s_id, le32_to_cpu(ret));
  1928. return -EROFS;
  1929. }
  1930. /*
  1931. * Mounting a RDONLY partition read-write, so reread
  1932. * and store the current valid flag. (It may have
  1933. * been changed by e2fsck since we originally mounted
  1934. * the partition.)
  1935. */
  1936. ext3_clear_journal_err(sb, es);
  1937. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1938. if ((ret = ext3_group_extend(sb, es, n_blocks_count)))
  1939. return ret;
  1940. if (!ext3_setup_super (sb, es, 0))
  1941. sb->s_flags &= ~MS_RDONLY;
  1942. }
  1943. }
  1944. return 0;
  1945. }
  1946. static int ext3_statfs (struct super_block * sb, struct kstatfs * buf)
  1947. {
  1948. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  1949. unsigned long overhead;
  1950. int i;
  1951. if (test_opt (sb, MINIX_DF))
  1952. overhead = 0;
  1953. else {
  1954. unsigned long ngroups;
  1955. ngroups = EXT3_SB(sb)->s_groups_count;
  1956. smp_rmb();
  1957. /*
  1958. * Compute the overhead (FS structures)
  1959. */
  1960. /*
  1961. * All of the blocks before first_data_block are
  1962. * overhead
  1963. */
  1964. overhead = le32_to_cpu(es->s_first_data_block);
  1965. /*
  1966. * Add the overhead attributed to the superblock and
  1967. * block group descriptors. If the sparse superblocks
  1968. * feature is turned on, then not all groups have this.
  1969. */
  1970. for (i = 0; i < ngroups; i++) {
  1971. overhead += ext3_bg_has_super(sb, i) +
  1972. ext3_bg_num_gdb(sb, i);
  1973. cond_resched();
  1974. }
  1975. /*
  1976. * Every block group has an inode bitmap, a block
  1977. * bitmap, and an inode table.
  1978. */
  1979. overhead += (ngroups * (2 + EXT3_SB(sb)->s_itb_per_group));
  1980. }
  1981. buf->f_type = EXT3_SUPER_MAGIC;
  1982. buf->f_bsize = sb->s_blocksize;
  1983. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
  1984. buf->f_bfree = ext3_count_free_blocks (sb);
  1985. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  1986. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  1987. buf->f_bavail = 0;
  1988. buf->f_files = le32_to_cpu(es->s_inodes_count);
  1989. buf->f_ffree = ext3_count_free_inodes (sb);
  1990. buf->f_namelen = EXT3_NAME_LEN;
  1991. return 0;
  1992. }
  1993. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  1994. * is locked for write. Otherwise the are possible deadlocks:
  1995. * Process 1 Process 2
  1996. * ext3_create() quota_sync()
  1997. * journal_start() write_dquot()
  1998. * DQUOT_INIT() down(dqio_sem)
  1999. * down(dqio_sem) journal_start()
  2000. *
  2001. */
  2002. #ifdef CONFIG_QUOTA
  2003. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2004. {
  2005. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  2006. }
  2007. static int ext3_dquot_initialize(struct inode *inode, int type)
  2008. {
  2009. handle_t *handle;
  2010. int ret, err;
  2011. /* We may create quota structure so we need to reserve enough blocks */
  2012. handle = ext3_journal_start(inode, 2*EXT3_QUOTA_INIT_BLOCKS);
  2013. if (IS_ERR(handle))
  2014. return PTR_ERR(handle);
  2015. ret = dquot_initialize(inode, type);
  2016. err = ext3_journal_stop(handle);
  2017. if (!ret)
  2018. ret = err;
  2019. return ret;
  2020. }
  2021. static int ext3_dquot_drop(struct inode *inode)
  2022. {
  2023. handle_t *handle;
  2024. int ret, err;
  2025. /* We may delete quota structure so we need to reserve enough blocks */
  2026. handle = ext3_journal_start(inode, 2*EXT3_QUOTA_INIT_BLOCKS);
  2027. if (IS_ERR(handle))
  2028. return PTR_ERR(handle);
  2029. ret = dquot_drop(inode);
  2030. err = ext3_journal_stop(handle);
  2031. if (!ret)
  2032. ret = err;
  2033. return ret;
  2034. }
  2035. static int ext3_write_dquot(struct dquot *dquot)
  2036. {
  2037. int ret, err;
  2038. handle_t *handle;
  2039. struct inode *inode;
  2040. inode = dquot_to_inode(dquot);
  2041. handle = ext3_journal_start(inode,
  2042. EXT3_QUOTA_TRANS_BLOCKS);
  2043. if (IS_ERR(handle))
  2044. return PTR_ERR(handle);
  2045. ret = dquot_commit(dquot);
  2046. err = ext3_journal_stop(handle);
  2047. if (!ret)
  2048. ret = err;
  2049. return ret;
  2050. }
  2051. static int ext3_acquire_dquot(struct dquot *dquot)
  2052. {
  2053. int ret, err;
  2054. handle_t *handle;
  2055. handle = ext3_journal_start(dquot_to_inode(dquot),
  2056. EXT3_QUOTA_INIT_BLOCKS);
  2057. if (IS_ERR(handle))
  2058. return PTR_ERR(handle);
  2059. ret = dquot_acquire(dquot);
  2060. err = ext3_journal_stop(handle);
  2061. if (!ret)
  2062. ret = err;
  2063. return ret;
  2064. }
  2065. static int ext3_release_dquot(struct dquot *dquot)
  2066. {
  2067. int ret, err;
  2068. handle_t *handle;
  2069. handle = ext3_journal_start(dquot_to_inode(dquot),
  2070. EXT3_QUOTA_INIT_BLOCKS);
  2071. if (IS_ERR(handle))
  2072. return PTR_ERR(handle);
  2073. ret = dquot_release(dquot);
  2074. err = ext3_journal_stop(handle);
  2075. if (!ret)
  2076. ret = err;
  2077. return ret;
  2078. }
  2079. static int ext3_mark_dquot_dirty(struct dquot *dquot)
  2080. {
  2081. /* Are we journalling quotas? */
  2082. if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2083. EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2084. dquot_mark_dquot_dirty(dquot);
  2085. return ext3_write_dquot(dquot);
  2086. } else {
  2087. return dquot_mark_dquot_dirty(dquot);
  2088. }
  2089. }
  2090. static int ext3_write_info(struct super_block *sb, int type)
  2091. {
  2092. int ret, err;
  2093. handle_t *handle;
  2094. /* Data block + inode block */
  2095. handle = ext3_journal_start(sb->s_root->d_inode, 2);
  2096. if (IS_ERR(handle))
  2097. return PTR_ERR(handle);
  2098. ret = dquot_commit_info(sb, type);
  2099. err = ext3_journal_stop(handle);
  2100. if (!ret)
  2101. ret = err;
  2102. return ret;
  2103. }
  2104. /*
  2105. * Turn on quotas during mount time - we need to find
  2106. * the quota file and such...
  2107. */
  2108. static int ext3_quota_on_mount(struct super_block *sb, int type)
  2109. {
  2110. int err;
  2111. struct dentry *dentry;
  2112. struct qstr name = { .name = EXT3_SB(sb)->s_qf_names[type],
  2113. .hash = 0,
  2114. .len = strlen(EXT3_SB(sb)->s_qf_names[type])};
  2115. dentry = lookup_hash(&name, sb->s_root);
  2116. if (IS_ERR(dentry))
  2117. return PTR_ERR(dentry);
  2118. err = vfs_quota_on_mount(type, EXT3_SB(sb)->s_jquota_fmt, dentry);
  2119. /* Now invalidate and put the dentry - quota got its own reference
  2120. * to inode and dentry has at least wrong hash so we had better
  2121. * throw it away */
  2122. d_invalidate(dentry);
  2123. dput(dentry);
  2124. return err;
  2125. }
  2126. /*
  2127. * Standard function to be called on quota_on
  2128. */
  2129. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  2130. char *path)
  2131. {
  2132. int err;
  2133. struct nameidata nd;
  2134. /* Not journalling quota? */
  2135. if (!EXT3_SB(sb)->s_qf_names[USRQUOTA] &&
  2136. !EXT3_SB(sb)->s_qf_names[GRPQUOTA])
  2137. return vfs_quota_on(sb, type, format_id, path);
  2138. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  2139. if (err)
  2140. return err;
  2141. /* Quotafile not on the same filesystem? */
  2142. if (nd.mnt->mnt_sb != sb) {
  2143. path_release(&nd);
  2144. return -EXDEV;
  2145. }
  2146. /* Quotafile not of fs root? */
  2147. if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
  2148. printk(KERN_WARNING
  2149. "EXT3-fs: Quota file not on filesystem root. "
  2150. "Journalled quota will not work.\n");
  2151. path_release(&nd);
  2152. return vfs_quota_on(sb, type, format_id, path);
  2153. }
  2154. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2155. * acquiring the locks... As quota files are never truncated and quota code
  2156. * itself serializes the operations (and noone else should touch the files)
  2157. * we don't have to be afraid of races */
  2158. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  2159. size_t len, loff_t off)
  2160. {
  2161. struct inode *inode = sb_dqopt(sb)->files[type];
  2162. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2163. int err = 0;
  2164. int offset = off & (sb->s_blocksize - 1);
  2165. int tocopy;
  2166. size_t toread;
  2167. struct buffer_head *bh;
  2168. loff_t i_size = i_size_read(inode);
  2169. if (off > i_size)
  2170. return 0;
  2171. if (off+len > i_size)
  2172. len = i_size-off;
  2173. toread = len;
  2174. while (toread > 0) {
  2175. tocopy = sb->s_blocksize - offset < toread ?
  2176. sb->s_blocksize - offset : toread;
  2177. bh = ext3_bread(NULL, inode, blk, 0, &err);
  2178. if (err)
  2179. return err;
  2180. if (!bh) /* A hole? */
  2181. memset(data, 0, tocopy);
  2182. else
  2183. memcpy(data, bh->b_data+offset, tocopy);
  2184. brelse(bh);
  2185. offset = 0;
  2186. toread -= tocopy;
  2187. data += tocopy;
  2188. blk++;
  2189. }
  2190. return len;
  2191. }
  2192. /* Write to quotafile (we know the transaction is already started and has
  2193. * enough credits) */
  2194. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  2195. const char *data, size_t len, loff_t off)
  2196. {
  2197. struct inode *inode = sb_dqopt(sb)->files[type];
  2198. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2199. int err = 0;
  2200. int offset = off & (sb->s_blocksize - 1);
  2201. int tocopy;
  2202. int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
  2203. size_t towrite = len;
  2204. struct buffer_head *bh;
  2205. handle_t *handle = journal_current_handle();
  2206. down(&inode->i_sem);
  2207. while (towrite > 0) {
  2208. tocopy = sb->s_blocksize - offset < towrite ?
  2209. sb->s_blocksize - offset : towrite;
  2210. bh = ext3_bread(handle, inode, blk, 1, &err);
  2211. if (!bh)
  2212. goto out;
  2213. if (journal_quota) {
  2214. err = ext3_journal_get_write_access(handle, bh);
  2215. if (err) {
  2216. brelse(bh);
  2217. goto out;
  2218. }
  2219. }
  2220. lock_buffer(bh);
  2221. memcpy(bh->b_data+offset, data, tocopy);
  2222. flush_dcache_page(bh->b_page);
  2223. unlock_buffer(bh);
  2224. if (journal_quota)
  2225. err = ext3_journal_dirty_metadata(handle, bh);
  2226. else {
  2227. /* Always do at least ordered writes for quotas */
  2228. err = ext3_journal_dirty_data(handle, bh);
  2229. mark_buffer_dirty(bh);
  2230. }
  2231. brelse(bh);
  2232. if (err)
  2233. goto out;
  2234. offset = 0;
  2235. towrite -= tocopy;
  2236. data += tocopy;
  2237. blk++;
  2238. }
  2239. out:
  2240. if (len == towrite)
  2241. return err;
  2242. if (inode->i_size < off+len-towrite) {
  2243. i_size_write(inode, off+len-towrite);
  2244. EXT3_I(inode)->i_disksize = inode->i_size;
  2245. }
  2246. inode->i_version++;
  2247. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2248. ext3_mark_inode_dirty(handle, inode);
  2249. up(&inode->i_sem);
  2250. return len - towrite;
  2251. }
  2252. #endif
  2253. static struct super_block *ext3_get_sb(struct file_system_type *fs_type,
  2254. int flags, const char *dev_name, void *data)
  2255. {
  2256. return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
  2257. }
  2258. static struct file_system_type ext3_fs_type = {
  2259. .owner = THIS_MODULE,
  2260. .name = "ext3",
  2261. .get_sb = ext3_get_sb,
  2262. .kill_sb = kill_block_super,
  2263. .fs_flags = FS_REQUIRES_DEV,
  2264. };
  2265. static int __init init_ext3_fs(void)
  2266. {
  2267. int err = init_ext3_xattr();
  2268. if (err)
  2269. return err;
  2270. err = init_inodecache();
  2271. if (err)
  2272. goto out1;
  2273. err = register_filesystem(&ext3_fs_type);
  2274. if (err)
  2275. goto out;
  2276. return 0;
  2277. out:
  2278. destroy_inodecache();
  2279. out1:
  2280. exit_ext3_xattr();
  2281. return err;
  2282. }
  2283. static void __exit exit_ext3_fs(void)
  2284. {
  2285. unregister_filesystem(&ext3_fs_type);
  2286. destroy_inodecache();
  2287. exit_ext3_xattr();
  2288. }
  2289. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2290. MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
  2291. MODULE_LICENSE("GPL");
  2292. module_init(init_ext3_fs)
  2293. module_exit(exit_ext3_fs)