super.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601
  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, Opt_quota, Opt_noquota,
  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_quota, "grpquota"},
  559. {Opt_noquota, "noquota"},
  560. {Opt_quota, "quota"},
  561. {Opt_quota, "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. set_opt(sbi->s_mount_opt, QUOTA);
  795. break;
  796. case Opt_offusrjquota:
  797. qtype = USRQUOTA;
  798. goto clear_qf_name;
  799. case Opt_offgrpjquota:
  800. qtype = GRPQUOTA;
  801. clear_qf_name:
  802. if (sb_any_quota_enabled(sb)) {
  803. printk(KERN_ERR "EXT3-fs: Cannot change "
  804. "journalled quota options when "
  805. "quota turned on.\n");
  806. return 0;
  807. }
  808. /*
  809. * The space will be released later when all options
  810. * are confirmed to be correct
  811. */
  812. sbi->s_qf_names[qtype] = NULL;
  813. break;
  814. case Opt_jqfmt_vfsold:
  815. sbi->s_jquota_fmt = QFMT_VFS_OLD;
  816. break;
  817. case Opt_jqfmt_vfsv0:
  818. sbi->s_jquota_fmt = QFMT_VFS_V0;
  819. break;
  820. case Opt_quota:
  821. set_opt(sbi->s_mount_opt, QUOTA);
  822. break;
  823. case Opt_noquota:
  824. if (sb_any_quota_enabled(sb)) {
  825. printk(KERN_ERR "EXT3-fs: Cannot change quota "
  826. "options when quota turned on.\n");
  827. return 0;
  828. }
  829. clear_opt(sbi->s_mount_opt, QUOTA);
  830. break;
  831. #else
  832. case Opt_usrjquota:
  833. case Opt_grpjquota:
  834. case Opt_offusrjquota:
  835. case Opt_offgrpjquota:
  836. case Opt_jqfmt_vfsold:
  837. case Opt_jqfmt_vfsv0:
  838. printk(KERN_ERR
  839. "EXT3-fs: journalled quota options not "
  840. "supported.\n");
  841. break;
  842. case Opt_quota:
  843. case Opt_noquota:
  844. break;
  845. #endif
  846. case Opt_abort:
  847. set_opt(sbi->s_mount_opt, ABORT);
  848. break;
  849. case Opt_barrier:
  850. if (match_int(&args[0], &option))
  851. return 0;
  852. if (option)
  853. set_opt(sbi->s_mount_opt, BARRIER);
  854. else
  855. clear_opt(sbi->s_mount_opt, BARRIER);
  856. break;
  857. case Opt_ignore:
  858. break;
  859. case Opt_resize:
  860. if (!is_remount) {
  861. printk("EXT3-fs: resize option only available "
  862. "for remount\n");
  863. return 0;
  864. }
  865. if (match_int(&args[0], &option) != 0)
  866. return 0;
  867. *n_blocks_count = option;
  868. break;
  869. case Opt_nobh:
  870. set_opt(sbi->s_mount_opt, NOBH);
  871. break;
  872. default:
  873. printk (KERN_ERR
  874. "EXT3-fs: Unrecognized mount option \"%s\" "
  875. "or missing value\n", p);
  876. return 0;
  877. }
  878. }
  879. #ifdef CONFIG_QUOTA
  880. if (!sbi->s_jquota_fmt && (sbi->s_qf_names[USRQUOTA] ||
  881. sbi->s_qf_names[GRPQUOTA])) {
  882. printk(KERN_ERR
  883. "EXT3-fs: journalled quota format not specified.\n");
  884. return 0;
  885. }
  886. #endif
  887. return 1;
  888. }
  889. static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
  890. int read_only)
  891. {
  892. struct ext3_sb_info *sbi = EXT3_SB(sb);
  893. int res = 0;
  894. if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
  895. printk (KERN_ERR "EXT3-fs warning: revision level too high, "
  896. "forcing read-only mode\n");
  897. res = MS_RDONLY;
  898. }
  899. if (read_only)
  900. return res;
  901. if (!(sbi->s_mount_state & EXT3_VALID_FS))
  902. printk (KERN_WARNING "EXT3-fs warning: mounting unchecked fs, "
  903. "running e2fsck is recommended\n");
  904. else if ((sbi->s_mount_state & EXT3_ERROR_FS))
  905. printk (KERN_WARNING
  906. "EXT3-fs warning: mounting fs with errors, "
  907. "running e2fsck is recommended\n");
  908. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
  909. le16_to_cpu(es->s_mnt_count) >=
  910. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  911. printk (KERN_WARNING
  912. "EXT3-fs warning: maximal mount count reached, "
  913. "running e2fsck is recommended\n");
  914. else if (le32_to_cpu(es->s_checkinterval) &&
  915. (le32_to_cpu(es->s_lastcheck) +
  916. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  917. printk (KERN_WARNING
  918. "EXT3-fs warning: checktime reached, "
  919. "running e2fsck is recommended\n");
  920. #if 0
  921. /* @@@ We _will_ want to clear the valid bit if we find
  922. inconsistencies, to force a fsck at reboot. But for
  923. a plain journaled filesystem we can keep it set as
  924. valid forever! :) */
  925. es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT3_VALID_FS);
  926. #endif
  927. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  928. es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
  929. es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
  930. es->s_mtime = cpu_to_le32(get_seconds());
  931. ext3_update_dynamic_rev(sb);
  932. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  933. ext3_commit_super(sb, es, 1);
  934. if (test_opt(sb, DEBUG))
  935. printk(KERN_INFO "[EXT3 FS bs=%lu, gc=%lu, "
  936. "bpg=%lu, ipg=%lu, mo=%04lx]\n",
  937. sb->s_blocksize,
  938. sbi->s_groups_count,
  939. EXT3_BLOCKS_PER_GROUP(sb),
  940. EXT3_INODES_PER_GROUP(sb),
  941. sbi->s_mount_opt);
  942. printk(KERN_INFO "EXT3 FS on %s, ", sb->s_id);
  943. if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
  944. char b[BDEVNAME_SIZE];
  945. printk("external journal on %s\n",
  946. bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
  947. } else {
  948. printk("internal journal\n");
  949. }
  950. #ifdef CONFIG_EXT3_CHECK
  951. if (test_opt (sb, CHECK)) {
  952. ext3_check_blocks_bitmap (sb);
  953. ext3_check_inodes_bitmap (sb);
  954. }
  955. #endif
  956. return res;
  957. }
  958. /* Called at mount-time, super-block is locked */
  959. static int ext3_check_descriptors (struct super_block * sb)
  960. {
  961. struct ext3_sb_info *sbi = EXT3_SB(sb);
  962. unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
  963. struct ext3_group_desc * gdp = NULL;
  964. int desc_block = 0;
  965. int i;
  966. ext3_debug ("Checking group descriptors");
  967. for (i = 0; i < sbi->s_groups_count; i++)
  968. {
  969. if ((i % EXT3_DESC_PER_BLOCK(sb)) == 0)
  970. gdp = (struct ext3_group_desc *)
  971. sbi->s_group_desc[desc_block++]->b_data;
  972. if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
  973. le32_to_cpu(gdp->bg_block_bitmap) >=
  974. block + EXT3_BLOCKS_PER_GROUP(sb))
  975. {
  976. ext3_error (sb, "ext3_check_descriptors",
  977. "Block bitmap for group %d"
  978. " not in group (block %lu)!",
  979. i, (unsigned long)
  980. le32_to_cpu(gdp->bg_block_bitmap));
  981. return 0;
  982. }
  983. if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
  984. le32_to_cpu(gdp->bg_inode_bitmap) >=
  985. block + EXT3_BLOCKS_PER_GROUP(sb))
  986. {
  987. ext3_error (sb, "ext3_check_descriptors",
  988. "Inode bitmap for group %d"
  989. " not in group (block %lu)!",
  990. i, (unsigned long)
  991. le32_to_cpu(gdp->bg_inode_bitmap));
  992. return 0;
  993. }
  994. if (le32_to_cpu(gdp->bg_inode_table) < block ||
  995. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
  996. block + EXT3_BLOCKS_PER_GROUP(sb))
  997. {
  998. ext3_error (sb, "ext3_check_descriptors",
  999. "Inode table for group %d"
  1000. " not in group (block %lu)!",
  1001. i, (unsigned long)
  1002. le32_to_cpu(gdp->bg_inode_table));
  1003. return 0;
  1004. }
  1005. block += EXT3_BLOCKS_PER_GROUP(sb);
  1006. gdp++;
  1007. }
  1008. sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
  1009. sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
  1010. return 1;
  1011. }
  1012. /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1013. * the superblock) which were deleted from all directories, but held open by
  1014. * a process at the time of a crash. We walk the list and try to delete these
  1015. * inodes at recovery time (only with a read-write filesystem).
  1016. *
  1017. * In order to keep the orphan inode chain consistent during traversal (in
  1018. * case of crash during recovery), we link each inode into the superblock
  1019. * orphan list_head and handle it the same way as an inode deletion during
  1020. * normal operation (which journals the operations for us).
  1021. *
  1022. * We only do an iget() and an iput() on each inode, which is very safe if we
  1023. * accidentally point at an in-use or already deleted inode. The worst that
  1024. * can happen in this case is that we get a "bit already cleared" message from
  1025. * ext3_free_inode(). The only reason we would point at a wrong inode is if
  1026. * e2fsck was run on this filesystem, and it must have already done the orphan
  1027. * inode cleanup for us, so we can safely abort without any further action.
  1028. */
  1029. static void ext3_orphan_cleanup (struct super_block * sb,
  1030. struct ext3_super_block * es)
  1031. {
  1032. unsigned int s_flags = sb->s_flags;
  1033. int nr_orphans = 0, nr_truncates = 0;
  1034. #ifdef CONFIG_QUOTA
  1035. int i;
  1036. #endif
  1037. if (!es->s_last_orphan) {
  1038. jbd_debug(4, "no orphan inodes to clean up\n");
  1039. return;
  1040. }
  1041. if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
  1042. if (es->s_last_orphan)
  1043. jbd_debug(1, "Errors on filesystem, "
  1044. "clearing orphan list.\n");
  1045. es->s_last_orphan = 0;
  1046. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1047. return;
  1048. }
  1049. if (s_flags & MS_RDONLY) {
  1050. printk(KERN_INFO "EXT3-fs: %s: orphan cleanup on readonly fs\n",
  1051. sb->s_id);
  1052. sb->s_flags &= ~MS_RDONLY;
  1053. }
  1054. #ifdef CONFIG_QUOTA
  1055. /* Needed for iput() to work correctly and not trash data */
  1056. sb->s_flags |= MS_ACTIVE;
  1057. /* Turn on quotas so that they are updated correctly */
  1058. for (i = 0; i < MAXQUOTAS; i++) {
  1059. if (EXT3_SB(sb)->s_qf_names[i]) {
  1060. int ret = ext3_quota_on_mount(sb, i);
  1061. if (ret < 0)
  1062. printk(KERN_ERR
  1063. "EXT3-fs: Cannot turn on journalled "
  1064. "quota: error %d\n", ret);
  1065. }
  1066. }
  1067. #endif
  1068. while (es->s_last_orphan) {
  1069. struct inode *inode;
  1070. if (!(inode =
  1071. ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
  1072. es->s_last_orphan = 0;
  1073. break;
  1074. }
  1075. list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
  1076. DQUOT_INIT(inode);
  1077. if (inode->i_nlink) {
  1078. printk(KERN_DEBUG
  1079. "%s: truncating inode %ld to %Ld bytes\n",
  1080. __FUNCTION__, inode->i_ino, inode->i_size);
  1081. jbd_debug(2, "truncating inode %ld to %Ld bytes\n",
  1082. inode->i_ino, inode->i_size);
  1083. ext3_truncate(inode);
  1084. nr_truncates++;
  1085. } else {
  1086. printk(KERN_DEBUG
  1087. "%s: deleting unreferenced inode %ld\n",
  1088. __FUNCTION__, inode->i_ino);
  1089. jbd_debug(2, "deleting unreferenced inode %ld\n",
  1090. inode->i_ino);
  1091. nr_orphans++;
  1092. }
  1093. iput(inode); /* The delete magic happens here! */
  1094. }
  1095. #define PLURAL(x) (x), ((x)==1) ? "" : "s"
  1096. if (nr_orphans)
  1097. printk(KERN_INFO "EXT3-fs: %s: %d orphan inode%s deleted\n",
  1098. sb->s_id, PLURAL(nr_orphans));
  1099. if (nr_truncates)
  1100. printk(KERN_INFO "EXT3-fs: %s: %d truncate%s cleaned up\n",
  1101. sb->s_id, PLURAL(nr_truncates));
  1102. #ifdef CONFIG_QUOTA
  1103. /* Turn quotas off */
  1104. for (i = 0; i < MAXQUOTAS; i++) {
  1105. if (sb_dqopt(sb)->files[i])
  1106. vfs_quota_off(sb, i);
  1107. }
  1108. #endif
  1109. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1110. }
  1111. #define log2(n) ffz(~(n))
  1112. /*
  1113. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  1114. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  1115. * We need to be 1 filesystem block less than the 2^32 sector limit.
  1116. */
  1117. static loff_t ext3_max_size(int bits)
  1118. {
  1119. loff_t res = EXT3_NDIR_BLOCKS;
  1120. /* This constant is calculated to be the largest file size for a
  1121. * dense, 4k-blocksize file such that the total number of
  1122. * sectors in the file, including data and all indirect blocks,
  1123. * does not exceed 2^32. */
  1124. const loff_t upper_limit = 0x1ff7fffd000LL;
  1125. res += 1LL << (bits-2);
  1126. res += 1LL << (2*(bits-2));
  1127. res += 1LL << (3*(bits-2));
  1128. res <<= bits;
  1129. if (res > upper_limit)
  1130. res = upper_limit;
  1131. return res;
  1132. }
  1133. static unsigned long descriptor_loc(struct super_block *sb,
  1134. unsigned long logic_sb_block,
  1135. int nr)
  1136. {
  1137. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1138. unsigned long bg, first_data_block, first_meta_bg;
  1139. int has_super = 0;
  1140. first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1141. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1142. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
  1143. nr < first_meta_bg)
  1144. return (logic_sb_block + nr + 1);
  1145. bg = sbi->s_desc_per_block * nr;
  1146. if (ext3_bg_has_super(sb, bg))
  1147. has_super = 1;
  1148. return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
  1149. }
  1150. static int ext3_fill_super (struct super_block *sb, void *data, int silent)
  1151. {
  1152. struct buffer_head * bh;
  1153. struct ext3_super_block *es = NULL;
  1154. struct ext3_sb_info *sbi;
  1155. unsigned long block;
  1156. unsigned long sb_block = get_sb_block(&data);
  1157. unsigned long logic_sb_block;
  1158. unsigned long offset = 0;
  1159. unsigned long journal_inum = 0;
  1160. unsigned long def_mount_opts;
  1161. struct inode *root;
  1162. int blocksize;
  1163. int hblock;
  1164. int db_count;
  1165. int i;
  1166. int needs_recovery;
  1167. __le32 features;
  1168. sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
  1169. if (!sbi)
  1170. return -ENOMEM;
  1171. sb->s_fs_info = sbi;
  1172. memset(sbi, 0, sizeof(*sbi));
  1173. sbi->s_mount_opt = 0;
  1174. sbi->s_resuid = EXT3_DEF_RESUID;
  1175. sbi->s_resgid = EXT3_DEF_RESGID;
  1176. unlock_kernel();
  1177. blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
  1178. if (!blocksize) {
  1179. printk(KERN_ERR "EXT3-fs: unable to set blocksize\n");
  1180. goto out_fail;
  1181. }
  1182. /*
  1183. * The ext3 superblock will not be buffer aligned for other than 1kB
  1184. * block sizes. We need to calculate the offset from buffer start.
  1185. */
  1186. if (blocksize != EXT3_MIN_BLOCK_SIZE) {
  1187. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1188. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1189. } else {
  1190. logic_sb_block = sb_block;
  1191. }
  1192. if (!(bh = sb_bread(sb, logic_sb_block))) {
  1193. printk (KERN_ERR "EXT3-fs: unable to read superblock\n");
  1194. goto out_fail;
  1195. }
  1196. /*
  1197. * Note: s_es must be initialized as soon as possible because
  1198. * some ext3 macro-instructions depend on its value
  1199. */
  1200. es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
  1201. sbi->s_es = es;
  1202. sb->s_magic = le16_to_cpu(es->s_magic);
  1203. if (sb->s_magic != EXT3_SUPER_MAGIC)
  1204. goto cantfind_ext3;
  1205. /* Set defaults before we parse the mount options */
  1206. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1207. if (def_mount_opts & EXT3_DEFM_DEBUG)
  1208. set_opt(sbi->s_mount_opt, DEBUG);
  1209. if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
  1210. set_opt(sbi->s_mount_opt, GRPID);
  1211. if (def_mount_opts & EXT3_DEFM_UID16)
  1212. set_opt(sbi->s_mount_opt, NO_UID32);
  1213. if (def_mount_opts & EXT3_DEFM_XATTR_USER)
  1214. set_opt(sbi->s_mount_opt, XATTR_USER);
  1215. if (def_mount_opts & EXT3_DEFM_ACL)
  1216. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1217. if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
  1218. sbi->s_mount_opt |= EXT3_MOUNT_JOURNAL_DATA;
  1219. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
  1220. sbi->s_mount_opt |= EXT3_MOUNT_ORDERED_DATA;
  1221. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
  1222. sbi->s_mount_opt |= EXT3_MOUNT_WRITEBACK_DATA;
  1223. if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
  1224. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1225. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_RO)
  1226. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1227. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  1228. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  1229. set_opt(sbi->s_mount_opt, RESERVATION);
  1230. if (!parse_options ((char *) data, sb, &journal_inum, NULL, 0))
  1231. goto failed_mount;
  1232. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1233. ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1234. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
  1235. (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1236. EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1237. EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1238. printk(KERN_WARNING
  1239. "EXT3-fs warning: feature flags set on rev 0 fs, "
  1240. "running e2fsck is recommended\n");
  1241. /*
  1242. * Check feature flags regardless of the revision level, since we
  1243. * previously didn't change the revision level when setting the flags,
  1244. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1245. */
  1246. features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
  1247. if (features) {
  1248. printk(KERN_ERR "EXT3-fs: %s: couldn't mount because of "
  1249. "unsupported optional features (%x).\n",
  1250. sb->s_id, le32_to_cpu(features));
  1251. goto failed_mount;
  1252. }
  1253. features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
  1254. if (!(sb->s_flags & MS_RDONLY) && features) {
  1255. printk(KERN_ERR "EXT3-fs: %s: couldn't mount RDWR because of "
  1256. "unsupported optional features (%x).\n",
  1257. sb->s_id, le32_to_cpu(features));
  1258. goto failed_mount;
  1259. }
  1260. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1261. if (blocksize < EXT3_MIN_BLOCK_SIZE ||
  1262. blocksize > EXT3_MAX_BLOCK_SIZE) {
  1263. printk(KERN_ERR
  1264. "EXT3-fs: Unsupported filesystem blocksize %d on %s.\n",
  1265. blocksize, sb->s_id);
  1266. goto failed_mount;
  1267. }
  1268. hblock = bdev_hardsect_size(sb->s_bdev);
  1269. if (sb->s_blocksize != blocksize) {
  1270. /*
  1271. * Make sure the blocksize for the filesystem is larger
  1272. * than the hardware sectorsize for the machine.
  1273. */
  1274. if (blocksize < hblock) {
  1275. printk(KERN_ERR "EXT3-fs: blocksize %d too small for "
  1276. "device blocksize %d.\n", blocksize, hblock);
  1277. goto failed_mount;
  1278. }
  1279. brelse (bh);
  1280. sb_set_blocksize(sb, blocksize);
  1281. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1282. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1283. bh = sb_bread(sb, logic_sb_block);
  1284. if (!bh) {
  1285. printk(KERN_ERR
  1286. "EXT3-fs: Can't read superblock on 2nd try.\n");
  1287. goto failed_mount;
  1288. }
  1289. es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
  1290. sbi->s_es = es;
  1291. if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
  1292. printk (KERN_ERR
  1293. "EXT3-fs: Magic mismatch, very weird !\n");
  1294. goto failed_mount;
  1295. }
  1296. }
  1297. sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
  1298. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
  1299. sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
  1300. sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
  1301. } else {
  1302. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1303. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1304. if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
  1305. (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
  1306. (sbi->s_inode_size > blocksize)) {
  1307. printk (KERN_ERR
  1308. "EXT3-fs: unsupported inode size: %d\n",
  1309. sbi->s_inode_size);
  1310. goto failed_mount;
  1311. }
  1312. }
  1313. sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
  1314. le32_to_cpu(es->s_log_frag_size);
  1315. if (blocksize != sbi->s_frag_size) {
  1316. printk(KERN_ERR
  1317. "EXT3-fs: fragsize %lu != blocksize %u (unsupported)\n",
  1318. sbi->s_frag_size, blocksize);
  1319. goto failed_mount;
  1320. }
  1321. sbi->s_frags_per_block = 1;
  1322. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1323. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1324. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1325. if (EXT3_INODE_SIZE(sb) == 0)
  1326. goto cantfind_ext3;
  1327. sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
  1328. if (sbi->s_inodes_per_block == 0)
  1329. goto cantfind_ext3;
  1330. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1331. sbi->s_inodes_per_block;
  1332. sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
  1333. sbi->s_sbh = bh;
  1334. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1335. sbi->s_addr_per_block_bits = log2(EXT3_ADDR_PER_BLOCK(sb));
  1336. sbi->s_desc_per_block_bits = log2(EXT3_DESC_PER_BLOCK(sb));
  1337. for (i=0; i < 4; i++)
  1338. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1339. sbi->s_def_hash_version = es->s_def_hash_version;
  1340. if (sbi->s_blocks_per_group > blocksize * 8) {
  1341. printk (KERN_ERR
  1342. "EXT3-fs: #blocks per group too big: %lu\n",
  1343. sbi->s_blocks_per_group);
  1344. goto failed_mount;
  1345. }
  1346. if (sbi->s_frags_per_group > blocksize * 8) {
  1347. printk (KERN_ERR
  1348. "EXT3-fs: #fragments per group too big: %lu\n",
  1349. sbi->s_frags_per_group);
  1350. goto failed_mount;
  1351. }
  1352. if (sbi->s_inodes_per_group > blocksize * 8) {
  1353. printk (KERN_ERR
  1354. "EXT3-fs: #inodes per group too big: %lu\n",
  1355. sbi->s_inodes_per_group);
  1356. goto failed_mount;
  1357. }
  1358. if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
  1359. goto cantfind_ext3;
  1360. sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
  1361. le32_to_cpu(es->s_first_data_block) +
  1362. EXT3_BLOCKS_PER_GROUP(sb) - 1) /
  1363. EXT3_BLOCKS_PER_GROUP(sb);
  1364. db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
  1365. EXT3_DESC_PER_BLOCK(sb);
  1366. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1367. GFP_KERNEL);
  1368. if (sbi->s_group_desc == NULL) {
  1369. printk (KERN_ERR "EXT3-fs: not enough memory\n");
  1370. goto failed_mount;
  1371. }
  1372. percpu_counter_init(&sbi->s_freeblocks_counter);
  1373. percpu_counter_init(&sbi->s_freeinodes_counter);
  1374. percpu_counter_init(&sbi->s_dirs_counter);
  1375. bgl_lock_init(&sbi->s_blockgroup_lock);
  1376. for (i = 0; i < db_count; i++) {
  1377. block = descriptor_loc(sb, logic_sb_block, i);
  1378. sbi->s_group_desc[i] = sb_bread(sb, block);
  1379. if (!sbi->s_group_desc[i]) {
  1380. printk (KERN_ERR "EXT3-fs: "
  1381. "can't read group descriptor %d\n", i);
  1382. db_count = i;
  1383. goto failed_mount2;
  1384. }
  1385. }
  1386. if (!ext3_check_descriptors (sb)) {
  1387. printk (KERN_ERR "EXT3-fs: group descriptors corrupted !\n");
  1388. goto failed_mount2;
  1389. }
  1390. sbi->s_gdb_count = db_count;
  1391. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1392. spin_lock_init(&sbi->s_next_gen_lock);
  1393. /* per fileystem reservation list head & lock */
  1394. spin_lock_init(&sbi->s_rsv_window_lock);
  1395. sbi->s_rsv_window_root = RB_ROOT;
  1396. /* Add a single, static dummy reservation to the start of the
  1397. * reservation window list --- it gives us a placeholder for
  1398. * append-at-start-of-list which makes the allocation logic
  1399. * _much_ simpler. */
  1400. sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1401. sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1402. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1403. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1404. ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1405. /*
  1406. * set up enough so that it can read an inode
  1407. */
  1408. sb->s_op = &ext3_sops;
  1409. sb->s_export_op = &ext3_export_ops;
  1410. sb->s_xattr = ext3_xattr_handlers;
  1411. #ifdef CONFIG_QUOTA
  1412. sb->s_qcop = &ext3_qctl_operations;
  1413. sb->dq_op = &ext3_quota_operations;
  1414. #endif
  1415. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1416. sb->s_root = NULL;
  1417. needs_recovery = (es->s_last_orphan != 0 ||
  1418. EXT3_HAS_INCOMPAT_FEATURE(sb,
  1419. EXT3_FEATURE_INCOMPAT_RECOVER));
  1420. /*
  1421. * The first inode we look at is the journal inode. Don't try
  1422. * root first: it may be modified in the journal!
  1423. */
  1424. if (!test_opt(sb, NOLOAD) &&
  1425. EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
  1426. if (ext3_load_journal(sb, es))
  1427. goto failed_mount2;
  1428. } else if (journal_inum) {
  1429. if (ext3_create_journal(sb, es, journal_inum))
  1430. goto failed_mount2;
  1431. } else {
  1432. if (!silent)
  1433. printk (KERN_ERR
  1434. "ext3: No journal on filesystem on %s\n",
  1435. sb->s_id);
  1436. goto failed_mount2;
  1437. }
  1438. /* We have now updated the journal if required, so we can
  1439. * validate the data journaling mode. */
  1440. switch (test_opt(sb, DATA_FLAGS)) {
  1441. case 0:
  1442. /* No mode set, assume a default based on the journal
  1443. capabilities: ORDERED_DATA if the journal can
  1444. cope, else JOURNAL_DATA */
  1445. if (journal_check_available_features
  1446. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
  1447. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1448. else
  1449. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1450. break;
  1451. case EXT3_MOUNT_ORDERED_DATA:
  1452. case EXT3_MOUNT_WRITEBACK_DATA:
  1453. if (!journal_check_available_features
  1454. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
  1455. printk(KERN_ERR "EXT3-fs: Journal does not support "
  1456. "requested data journaling mode\n");
  1457. goto failed_mount3;
  1458. }
  1459. default:
  1460. break;
  1461. }
  1462. if (test_opt(sb, NOBH)) {
  1463. if (sb->s_blocksize_bits != PAGE_CACHE_SHIFT) {
  1464. printk(KERN_WARNING "EXT3-fs: Ignoring nobh option "
  1465. "since filesystem blocksize doesn't match "
  1466. "pagesize\n");
  1467. clear_opt(sbi->s_mount_opt, NOBH);
  1468. }
  1469. if (!(test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_WRITEBACK_DATA)) {
  1470. printk(KERN_WARNING "EXT3-fs: Ignoring nobh option - "
  1471. "its supported only with writeback mode\n");
  1472. clear_opt(sbi->s_mount_opt, NOBH);
  1473. }
  1474. }
  1475. /*
  1476. * The journal_load will have done any necessary log recovery,
  1477. * so we can safely mount the rest of the filesystem now.
  1478. */
  1479. root = iget(sb, EXT3_ROOT_INO);
  1480. sb->s_root = d_alloc_root(root);
  1481. if (!sb->s_root) {
  1482. printk(KERN_ERR "EXT3-fs: get root inode failed\n");
  1483. iput(root);
  1484. goto failed_mount3;
  1485. }
  1486. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1487. dput(sb->s_root);
  1488. sb->s_root = NULL;
  1489. printk(KERN_ERR "EXT3-fs: corrupt root inode, run e2fsck\n");
  1490. goto failed_mount3;
  1491. }
  1492. ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  1493. /*
  1494. * akpm: core read_super() calls in here with the superblock locked.
  1495. * That deadlocks, because orphan cleanup needs to lock the superblock
  1496. * in numerous places. Here we just pop the lock - it's relatively
  1497. * harmless, because we are now ready to accept write_super() requests,
  1498. * and aviro says that's the only reason for hanging onto the
  1499. * superblock lock.
  1500. */
  1501. EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
  1502. ext3_orphan_cleanup(sb, es);
  1503. EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
  1504. if (needs_recovery)
  1505. printk (KERN_INFO "EXT3-fs: recovery complete.\n");
  1506. ext3_mark_recovery_complete(sb, es);
  1507. printk (KERN_INFO "EXT3-fs: mounted filesystem with %s data mode.\n",
  1508. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
  1509. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
  1510. "writeback");
  1511. percpu_counter_mod(&sbi->s_freeblocks_counter,
  1512. ext3_count_free_blocks(sb));
  1513. percpu_counter_mod(&sbi->s_freeinodes_counter,
  1514. ext3_count_free_inodes(sb));
  1515. percpu_counter_mod(&sbi->s_dirs_counter,
  1516. ext3_count_dirs(sb));
  1517. lock_kernel();
  1518. return 0;
  1519. cantfind_ext3:
  1520. if (!silent)
  1521. printk(KERN_ERR "VFS: Can't find ext3 filesystem on dev %s.\n",
  1522. sb->s_id);
  1523. goto failed_mount;
  1524. failed_mount3:
  1525. journal_destroy(sbi->s_journal);
  1526. failed_mount2:
  1527. for (i = 0; i < db_count; i++)
  1528. brelse(sbi->s_group_desc[i]);
  1529. kfree(sbi->s_group_desc);
  1530. failed_mount:
  1531. #ifdef CONFIG_QUOTA
  1532. for (i = 0; i < MAXQUOTAS; i++)
  1533. kfree(sbi->s_qf_names[i]);
  1534. #endif
  1535. ext3_blkdev_remove(sbi);
  1536. brelse(bh);
  1537. out_fail:
  1538. sb->s_fs_info = NULL;
  1539. kfree(sbi);
  1540. lock_kernel();
  1541. return -EINVAL;
  1542. }
  1543. /*
  1544. * Setup any per-fs journal parameters now. We'll do this both on
  1545. * initial mount, once the journal has been initialised but before we've
  1546. * done any recovery; and again on any subsequent remount.
  1547. */
  1548. static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
  1549. {
  1550. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1551. if (sbi->s_commit_interval)
  1552. journal->j_commit_interval = sbi->s_commit_interval;
  1553. /* We could also set up an ext3-specific default for the commit
  1554. * interval here, but for now we'll just fall back to the jbd
  1555. * default. */
  1556. spin_lock(&journal->j_state_lock);
  1557. if (test_opt(sb, BARRIER))
  1558. journal->j_flags |= JFS_BARRIER;
  1559. else
  1560. journal->j_flags &= ~JFS_BARRIER;
  1561. spin_unlock(&journal->j_state_lock);
  1562. }
  1563. static journal_t *ext3_get_journal(struct super_block *sb, int journal_inum)
  1564. {
  1565. struct inode *journal_inode;
  1566. journal_t *journal;
  1567. /* First, test for the existence of a valid inode on disk. Bad
  1568. * things happen if we iget() an unused inode, as the subsequent
  1569. * iput() will try to delete it. */
  1570. journal_inode = iget(sb, journal_inum);
  1571. if (!journal_inode) {
  1572. printk(KERN_ERR "EXT3-fs: no journal found.\n");
  1573. return NULL;
  1574. }
  1575. if (!journal_inode->i_nlink) {
  1576. make_bad_inode(journal_inode);
  1577. iput(journal_inode);
  1578. printk(KERN_ERR "EXT3-fs: journal inode is deleted.\n");
  1579. return NULL;
  1580. }
  1581. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1582. journal_inode, journal_inode->i_size);
  1583. if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
  1584. printk(KERN_ERR "EXT3-fs: invalid journal inode.\n");
  1585. iput(journal_inode);
  1586. return NULL;
  1587. }
  1588. journal = journal_init_inode(journal_inode);
  1589. if (!journal) {
  1590. printk(KERN_ERR "EXT3-fs: Could not load journal inode\n");
  1591. iput(journal_inode);
  1592. return NULL;
  1593. }
  1594. journal->j_private = sb;
  1595. ext3_init_journal_params(sb, journal);
  1596. return journal;
  1597. }
  1598. static journal_t *ext3_get_dev_journal(struct super_block *sb,
  1599. dev_t j_dev)
  1600. {
  1601. struct buffer_head * bh;
  1602. journal_t *journal;
  1603. int start;
  1604. int len;
  1605. int hblock, blocksize;
  1606. unsigned long sb_block;
  1607. unsigned long offset;
  1608. struct ext3_super_block * es;
  1609. struct block_device *bdev;
  1610. bdev = ext3_blkdev_get(j_dev);
  1611. if (bdev == NULL)
  1612. return NULL;
  1613. if (bd_claim(bdev, sb)) {
  1614. printk(KERN_ERR
  1615. "EXT3: failed to claim external journal device.\n");
  1616. blkdev_put(bdev);
  1617. return NULL;
  1618. }
  1619. blocksize = sb->s_blocksize;
  1620. hblock = bdev_hardsect_size(bdev);
  1621. if (blocksize < hblock) {
  1622. printk(KERN_ERR
  1623. "EXT3-fs: blocksize too small for journal device.\n");
  1624. goto out_bdev;
  1625. }
  1626. sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
  1627. offset = EXT3_MIN_BLOCK_SIZE % blocksize;
  1628. set_blocksize(bdev, blocksize);
  1629. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  1630. printk(KERN_ERR "EXT3-fs: couldn't read superblock of "
  1631. "external journal\n");
  1632. goto out_bdev;
  1633. }
  1634. es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
  1635. if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
  1636. !(le32_to_cpu(es->s_feature_incompat) &
  1637. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  1638. printk(KERN_ERR "EXT3-fs: external journal has "
  1639. "bad superblock\n");
  1640. brelse(bh);
  1641. goto out_bdev;
  1642. }
  1643. if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  1644. printk(KERN_ERR "EXT3-fs: journal UUID does not match\n");
  1645. brelse(bh);
  1646. goto out_bdev;
  1647. }
  1648. len = le32_to_cpu(es->s_blocks_count);
  1649. start = sb_block + 1;
  1650. brelse(bh); /* we're done with the superblock */
  1651. journal = journal_init_dev(bdev, sb->s_bdev,
  1652. start, len, blocksize);
  1653. if (!journal) {
  1654. printk(KERN_ERR "EXT3-fs: failed to create device journal\n");
  1655. goto out_bdev;
  1656. }
  1657. journal->j_private = sb;
  1658. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  1659. wait_on_buffer(journal->j_sb_buffer);
  1660. if (!buffer_uptodate(journal->j_sb_buffer)) {
  1661. printk(KERN_ERR "EXT3-fs: I/O error on journal device\n");
  1662. goto out_journal;
  1663. }
  1664. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  1665. printk(KERN_ERR "EXT3-fs: External journal has more than one "
  1666. "user (unsupported) - %d\n",
  1667. be32_to_cpu(journal->j_superblock->s_nr_users));
  1668. goto out_journal;
  1669. }
  1670. EXT3_SB(sb)->journal_bdev = bdev;
  1671. ext3_init_journal_params(sb, journal);
  1672. return journal;
  1673. out_journal:
  1674. journal_destroy(journal);
  1675. out_bdev:
  1676. ext3_blkdev_put(bdev);
  1677. return NULL;
  1678. }
  1679. static int ext3_load_journal(struct super_block * sb,
  1680. struct ext3_super_block * es)
  1681. {
  1682. journal_t *journal;
  1683. int journal_inum = le32_to_cpu(es->s_journal_inum);
  1684. dev_t journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  1685. int err = 0;
  1686. int really_read_only;
  1687. really_read_only = bdev_read_only(sb->s_bdev);
  1688. /*
  1689. * Are we loading a blank journal or performing recovery after a
  1690. * crash? For recovery, we need to check in advance whether we
  1691. * can get read-write access to the device.
  1692. */
  1693. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
  1694. if (sb->s_flags & MS_RDONLY) {
  1695. printk(KERN_INFO "EXT3-fs: INFO: recovery "
  1696. "required on readonly filesystem.\n");
  1697. if (really_read_only) {
  1698. printk(KERN_ERR "EXT3-fs: write access "
  1699. "unavailable, cannot proceed.\n");
  1700. return -EROFS;
  1701. }
  1702. printk (KERN_INFO "EXT3-fs: write access will "
  1703. "be enabled during recovery.\n");
  1704. }
  1705. }
  1706. if (journal_inum && journal_dev) {
  1707. printk(KERN_ERR "EXT3-fs: filesystem has both journal "
  1708. "and inode journals!\n");
  1709. return -EINVAL;
  1710. }
  1711. if (journal_inum) {
  1712. if (!(journal = ext3_get_journal(sb, journal_inum)))
  1713. return -EINVAL;
  1714. } else {
  1715. if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
  1716. return -EINVAL;
  1717. }
  1718. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  1719. err = journal_update_format(journal);
  1720. if (err) {
  1721. printk(KERN_ERR "EXT3-fs: error updating journal.\n");
  1722. journal_destroy(journal);
  1723. return err;
  1724. }
  1725. }
  1726. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
  1727. err = journal_wipe(journal, !really_read_only);
  1728. if (!err)
  1729. err = journal_load(journal);
  1730. if (err) {
  1731. printk(KERN_ERR "EXT3-fs: error loading journal.\n");
  1732. journal_destroy(journal);
  1733. return err;
  1734. }
  1735. EXT3_SB(sb)->s_journal = journal;
  1736. ext3_clear_journal_err(sb, es);
  1737. return 0;
  1738. }
  1739. static int ext3_create_journal(struct super_block * sb,
  1740. struct ext3_super_block * es,
  1741. int journal_inum)
  1742. {
  1743. journal_t *journal;
  1744. if (sb->s_flags & MS_RDONLY) {
  1745. printk(KERN_ERR "EXT3-fs: readonly filesystem when trying to "
  1746. "create journal.\n");
  1747. return -EROFS;
  1748. }
  1749. if (!(journal = ext3_get_journal(sb, journal_inum)))
  1750. return -EINVAL;
  1751. printk(KERN_INFO "EXT3-fs: creating new journal on inode %d\n",
  1752. journal_inum);
  1753. if (journal_create(journal)) {
  1754. printk(KERN_ERR "EXT3-fs: error creating journal.\n");
  1755. journal_destroy(journal);
  1756. return -EIO;
  1757. }
  1758. EXT3_SB(sb)->s_journal = journal;
  1759. ext3_update_dynamic_rev(sb);
  1760. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1761. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
  1762. es->s_journal_inum = cpu_to_le32(journal_inum);
  1763. sb->s_dirt = 1;
  1764. /* Make sure we flush the recovery flag to disk. */
  1765. ext3_commit_super(sb, es, 1);
  1766. return 0;
  1767. }
  1768. static void ext3_commit_super (struct super_block * sb,
  1769. struct ext3_super_block * es,
  1770. int sync)
  1771. {
  1772. struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
  1773. if (!sbh)
  1774. return;
  1775. es->s_wtime = cpu_to_le32(get_seconds());
  1776. es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
  1777. es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
  1778. BUFFER_TRACE(sbh, "marking dirty");
  1779. mark_buffer_dirty(sbh);
  1780. if (sync)
  1781. sync_dirty_buffer(sbh);
  1782. }
  1783. /*
  1784. * Have we just finished recovery? If so, and if we are mounting (or
  1785. * remounting) the filesystem readonly, then we will end up with a
  1786. * consistent fs on disk. Record that fact.
  1787. */
  1788. static void ext3_mark_recovery_complete(struct super_block * sb,
  1789. struct ext3_super_block * es)
  1790. {
  1791. journal_t *journal = EXT3_SB(sb)->s_journal;
  1792. journal_lock_updates(journal);
  1793. journal_flush(journal);
  1794. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
  1795. sb->s_flags & MS_RDONLY) {
  1796. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1797. sb->s_dirt = 0;
  1798. ext3_commit_super(sb, es, 1);
  1799. }
  1800. journal_unlock_updates(journal);
  1801. }
  1802. /*
  1803. * If we are mounting (or read-write remounting) a filesystem whose journal
  1804. * has recorded an error from a previous lifetime, move that error to the
  1805. * main filesystem now.
  1806. */
  1807. static void ext3_clear_journal_err(struct super_block * sb,
  1808. struct ext3_super_block * es)
  1809. {
  1810. journal_t *journal;
  1811. int j_errno;
  1812. const char *errstr;
  1813. journal = EXT3_SB(sb)->s_journal;
  1814. /*
  1815. * Now check for any error status which may have been recorded in the
  1816. * journal by a prior ext3_error() or ext3_abort()
  1817. */
  1818. j_errno = journal_errno(journal);
  1819. if (j_errno) {
  1820. char nbuf[16];
  1821. errstr = ext3_decode_error(sb, j_errno, nbuf);
  1822. ext3_warning(sb, __FUNCTION__, "Filesystem error recorded "
  1823. "from previous mount: %s", errstr);
  1824. ext3_warning(sb, __FUNCTION__, "Marking fs in need of "
  1825. "filesystem check.");
  1826. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  1827. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  1828. ext3_commit_super (sb, es, 1);
  1829. journal_clear_err(journal);
  1830. }
  1831. }
  1832. /*
  1833. * Force the running and committing transactions to commit,
  1834. * and wait on the commit.
  1835. */
  1836. int ext3_force_commit(struct super_block *sb)
  1837. {
  1838. journal_t *journal;
  1839. int ret;
  1840. if (sb->s_flags & MS_RDONLY)
  1841. return 0;
  1842. journal = EXT3_SB(sb)->s_journal;
  1843. sb->s_dirt = 0;
  1844. ret = ext3_journal_force_commit(journal);
  1845. return ret;
  1846. }
  1847. /*
  1848. * Ext3 always journals updates to the superblock itself, so we don't
  1849. * have to propagate any other updates to the superblock on disk at this
  1850. * point. Just start an async writeback to get the buffers on their way
  1851. * to the disk.
  1852. *
  1853. * This implicitly triggers the writebehind on sync().
  1854. */
  1855. static void ext3_write_super (struct super_block * sb)
  1856. {
  1857. if (down_trylock(&sb->s_lock) == 0)
  1858. BUG();
  1859. sb->s_dirt = 0;
  1860. }
  1861. static int ext3_sync_fs(struct super_block *sb, int wait)
  1862. {
  1863. tid_t target;
  1864. sb->s_dirt = 0;
  1865. if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
  1866. if (wait)
  1867. log_wait_commit(EXT3_SB(sb)->s_journal, target);
  1868. }
  1869. return 0;
  1870. }
  1871. /*
  1872. * LVM calls this function before a (read-only) snapshot is created. This
  1873. * gives us a chance to flush the journal completely and mark the fs clean.
  1874. */
  1875. static void ext3_write_super_lockfs(struct super_block *sb)
  1876. {
  1877. sb->s_dirt = 0;
  1878. if (!(sb->s_flags & MS_RDONLY)) {
  1879. journal_t *journal = EXT3_SB(sb)->s_journal;
  1880. /* Now we set up the journal barrier. */
  1881. journal_lock_updates(journal);
  1882. journal_flush(journal);
  1883. /* Journal blocked and flushed, clear needs_recovery flag. */
  1884. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1885. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  1886. }
  1887. }
  1888. /*
  1889. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  1890. * flag here, even though the filesystem is not technically dirty yet.
  1891. */
  1892. static void ext3_unlockfs(struct super_block *sb)
  1893. {
  1894. if (!(sb->s_flags & MS_RDONLY)) {
  1895. lock_super(sb);
  1896. /* Reser the needs_recovery flag before the fs is unlocked. */
  1897. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1898. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  1899. unlock_super(sb);
  1900. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  1901. }
  1902. }
  1903. static int ext3_remount (struct super_block * sb, int * flags, char * data)
  1904. {
  1905. struct ext3_super_block * es;
  1906. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1907. unsigned long n_blocks_count = 0;
  1908. unsigned long old_sb_flags;
  1909. struct ext3_mount_options old_opts;
  1910. int err;
  1911. #ifdef CONFIG_QUOTA
  1912. int i;
  1913. #endif
  1914. /* Store the original options */
  1915. old_sb_flags = sb->s_flags;
  1916. old_opts.s_mount_opt = sbi->s_mount_opt;
  1917. old_opts.s_resuid = sbi->s_resuid;
  1918. old_opts.s_resgid = sbi->s_resgid;
  1919. old_opts.s_commit_interval = sbi->s_commit_interval;
  1920. #ifdef CONFIG_QUOTA
  1921. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  1922. for (i = 0; i < MAXQUOTAS; i++)
  1923. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  1924. #endif
  1925. /*
  1926. * Allow the "check" option to be passed as a remount option.
  1927. */
  1928. if (!parse_options(data, sb, NULL, &n_blocks_count, 1)) {
  1929. err = -EINVAL;
  1930. goto restore_opts;
  1931. }
  1932. if (sbi->s_mount_opt & EXT3_MOUNT_ABORT)
  1933. ext3_abort(sb, __FUNCTION__, "Abort forced by user");
  1934. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1935. ((sbi->s_mount_opt & EXT3_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
  1936. es = sbi->s_es;
  1937. ext3_init_journal_params(sb, sbi->s_journal);
  1938. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  1939. n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
  1940. if (sbi->s_mount_opt & EXT3_MOUNT_ABORT) {
  1941. err = -EROFS;
  1942. goto restore_opts;
  1943. }
  1944. if (*flags & MS_RDONLY) {
  1945. /*
  1946. * First of all, the unconditional stuff we have to do
  1947. * to disable replay of the journal when we next remount
  1948. */
  1949. sb->s_flags |= MS_RDONLY;
  1950. /*
  1951. * OK, test if we are remounting a valid rw partition
  1952. * readonly, and if so set the rdonly flag and then
  1953. * mark the partition as valid again.
  1954. */
  1955. if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
  1956. (sbi->s_mount_state & EXT3_VALID_FS))
  1957. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1958. ext3_mark_recovery_complete(sb, es);
  1959. } else {
  1960. __le32 ret;
  1961. if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1962. ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
  1963. printk(KERN_WARNING "EXT3-fs: %s: couldn't "
  1964. "remount RDWR because of unsupported "
  1965. "optional features (%x).\n",
  1966. sb->s_id, le32_to_cpu(ret));
  1967. err = -EROFS;
  1968. goto restore_opts;
  1969. }
  1970. /*
  1971. * Mounting a RDONLY partition read-write, so reread
  1972. * and store the current valid flag. (It may have
  1973. * been changed by e2fsck since we originally mounted
  1974. * the partition.)
  1975. */
  1976. ext3_clear_journal_err(sb, es);
  1977. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1978. if ((ret = ext3_group_extend(sb, es, n_blocks_count))) {
  1979. err = ret;
  1980. goto restore_opts;
  1981. }
  1982. if (!ext3_setup_super (sb, es, 0))
  1983. sb->s_flags &= ~MS_RDONLY;
  1984. }
  1985. }
  1986. #ifdef CONFIG_QUOTA
  1987. /* Release old quota file names */
  1988. for (i = 0; i < MAXQUOTAS; i++)
  1989. if (old_opts.s_qf_names[i] &&
  1990. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  1991. kfree(old_opts.s_qf_names[i]);
  1992. #endif
  1993. return 0;
  1994. restore_opts:
  1995. sb->s_flags = old_sb_flags;
  1996. sbi->s_mount_opt = old_opts.s_mount_opt;
  1997. sbi->s_resuid = old_opts.s_resuid;
  1998. sbi->s_resgid = old_opts.s_resgid;
  1999. sbi->s_commit_interval = old_opts.s_commit_interval;
  2000. #ifdef CONFIG_QUOTA
  2001. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  2002. for (i = 0; i < MAXQUOTAS; i++) {
  2003. if (sbi->s_qf_names[i] &&
  2004. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2005. kfree(sbi->s_qf_names[i]);
  2006. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  2007. }
  2008. #endif
  2009. return err;
  2010. }
  2011. static int ext3_statfs (struct super_block * sb, struct kstatfs * buf)
  2012. {
  2013. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  2014. unsigned long overhead;
  2015. int i;
  2016. if (test_opt (sb, MINIX_DF))
  2017. overhead = 0;
  2018. else {
  2019. unsigned long ngroups;
  2020. ngroups = EXT3_SB(sb)->s_groups_count;
  2021. smp_rmb();
  2022. /*
  2023. * Compute the overhead (FS structures)
  2024. */
  2025. /*
  2026. * All of the blocks before first_data_block are
  2027. * overhead
  2028. */
  2029. overhead = le32_to_cpu(es->s_first_data_block);
  2030. /*
  2031. * Add the overhead attributed to the superblock and
  2032. * block group descriptors. If the sparse superblocks
  2033. * feature is turned on, then not all groups have this.
  2034. */
  2035. for (i = 0; i < ngroups; i++) {
  2036. overhead += ext3_bg_has_super(sb, i) +
  2037. ext3_bg_num_gdb(sb, i);
  2038. cond_resched();
  2039. }
  2040. /*
  2041. * Every block group has an inode bitmap, a block
  2042. * bitmap, and an inode table.
  2043. */
  2044. overhead += (ngroups * (2 + EXT3_SB(sb)->s_itb_per_group));
  2045. }
  2046. buf->f_type = EXT3_SUPER_MAGIC;
  2047. buf->f_bsize = sb->s_blocksize;
  2048. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - overhead;
  2049. buf->f_bfree = ext3_count_free_blocks (sb);
  2050. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  2051. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  2052. buf->f_bavail = 0;
  2053. buf->f_files = le32_to_cpu(es->s_inodes_count);
  2054. buf->f_ffree = ext3_count_free_inodes (sb);
  2055. buf->f_namelen = EXT3_NAME_LEN;
  2056. return 0;
  2057. }
  2058. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  2059. * is locked for write. Otherwise the are possible deadlocks:
  2060. * Process 1 Process 2
  2061. * ext3_create() quota_sync()
  2062. * journal_start() write_dquot()
  2063. * DQUOT_INIT() down(dqio_sem)
  2064. * down(dqio_sem) journal_start()
  2065. *
  2066. */
  2067. #ifdef CONFIG_QUOTA
  2068. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2069. {
  2070. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  2071. }
  2072. static int ext3_dquot_initialize(struct inode *inode, int type)
  2073. {
  2074. handle_t *handle;
  2075. int ret, err;
  2076. /* We may create quota structure so we need to reserve enough blocks */
  2077. handle = ext3_journal_start(inode, 2*EXT3_QUOTA_INIT_BLOCKS(inode->i_sb));
  2078. if (IS_ERR(handle))
  2079. return PTR_ERR(handle);
  2080. ret = dquot_initialize(inode, type);
  2081. err = ext3_journal_stop(handle);
  2082. if (!ret)
  2083. ret = err;
  2084. return ret;
  2085. }
  2086. static int ext3_dquot_drop(struct inode *inode)
  2087. {
  2088. handle_t *handle;
  2089. int ret, err;
  2090. /* We may delete quota structure so we need to reserve enough blocks */
  2091. handle = ext3_journal_start(inode, 2*EXT3_QUOTA_DEL_BLOCKS(inode->i_sb));
  2092. if (IS_ERR(handle))
  2093. return PTR_ERR(handle);
  2094. ret = dquot_drop(inode);
  2095. err = ext3_journal_stop(handle);
  2096. if (!ret)
  2097. ret = err;
  2098. return ret;
  2099. }
  2100. static int ext3_write_dquot(struct dquot *dquot)
  2101. {
  2102. int ret, err;
  2103. handle_t *handle;
  2104. struct inode *inode;
  2105. inode = dquot_to_inode(dquot);
  2106. handle = ext3_journal_start(inode,
  2107. EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  2108. if (IS_ERR(handle))
  2109. return PTR_ERR(handle);
  2110. ret = dquot_commit(dquot);
  2111. err = ext3_journal_stop(handle);
  2112. if (!ret)
  2113. ret = err;
  2114. return ret;
  2115. }
  2116. static int ext3_acquire_dquot(struct dquot *dquot)
  2117. {
  2118. int ret, err;
  2119. handle_t *handle;
  2120. handle = ext3_journal_start(dquot_to_inode(dquot),
  2121. EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  2122. if (IS_ERR(handle))
  2123. return PTR_ERR(handle);
  2124. ret = dquot_acquire(dquot);
  2125. err = ext3_journal_stop(handle);
  2126. if (!ret)
  2127. ret = err;
  2128. return ret;
  2129. }
  2130. static int ext3_release_dquot(struct dquot *dquot)
  2131. {
  2132. int ret, err;
  2133. handle_t *handle;
  2134. handle = ext3_journal_start(dquot_to_inode(dquot),
  2135. EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2136. if (IS_ERR(handle))
  2137. return PTR_ERR(handle);
  2138. ret = dquot_release(dquot);
  2139. err = ext3_journal_stop(handle);
  2140. if (!ret)
  2141. ret = err;
  2142. return ret;
  2143. }
  2144. static int ext3_mark_dquot_dirty(struct dquot *dquot)
  2145. {
  2146. /* Are we journalling quotas? */
  2147. if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2148. EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2149. dquot_mark_dquot_dirty(dquot);
  2150. return ext3_write_dquot(dquot);
  2151. } else {
  2152. return dquot_mark_dquot_dirty(dquot);
  2153. }
  2154. }
  2155. static int ext3_write_info(struct super_block *sb, int type)
  2156. {
  2157. int ret, err;
  2158. handle_t *handle;
  2159. /* Data block + inode block */
  2160. handle = ext3_journal_start(sb->s_root->d_inode, 2);
  2161. if (IS_ERR(handle))
  2162. return PTR_ERR(handle);
  2163. ret = dquot_commit_info(sb, type);
  2164. err = ext3_journal_stop(handle);
  2165. if (!ret)
  2166. ret = err;
  2167. return ret;
  2168. }
  2169. /*
  2170. * Turn on quotas during mount time - we need to find
  2171. * the quota file and such...
  2172. */
  2173. static int ext3_quota_on_mount(struct super_block *sb, int type)
  2174. {
  2175. return vfs_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
  2176. EXT3_SB(sb)->s_jquota_fmt, type);
  2177. }
  2178. /*
  2179. * Standard function to be called on quota_on
  2180. */
  2181. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  2182. char *path)
  2183. {
  2184. int err;
  2185. struct nameidata nd;
  2186. if (!test_opt(sb, QUOTA))
  2187. return -EINVAL;
  2188. /* Not journalling quota? */
  2189. if (!EXT3_SB(sb)->s_qf_names[USRQUOTA] &&
  2190. !EXT3_SB(sb)->s_qf_names[GRPQUOTA])
  2191. return vfs_quota_on(sb, type, format_id, path);
  2192. err = path_lookup(path, LOOKUP_FOLLOW, &nd);
  2193. if (err)
  2194. return err;
  2195. /* Quotafile not on the same filesystem? */
  2196. if (nd.mnt->mnt_sb != sb) {
  2197. path_release(&nd);
  2198. return -EXDEV;
  2199. }
  2200. /* Quotafile not of fs root? */
  2201. if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
  2202. printk(KERN_WARNING
  2203. "EXT3-fs: Quota file not on filesystem root. "
  2204. "Journalled quota will not work.\n");
  2205. path_release(&nd);
  2206. return vfs_quota_on(sb, type, format_id, path);
  2207. }
  2208. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2209. * acquiring the locks... As quota files are never truncated and quota code
  2210. * itself serializes the operations (and noone else should touch the files)
  2211. * we don't have to be afraid of races */
  2212. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  2213. size_t len, loff_t off)
  2214. {
  2215. struct inode *inode = sb_dqopt(sb)->files[type];
  2216. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2217. int err = 0;
  2218. int offset = off & (sb->s_blocksize - 1);
  2219. int tocopy;
  2220. size_t toread;
  2221. struct buffer_head *bh;
  2222. loff_t i_size = i_size_read(inode);
  2223. if (off > i_size)
  2224. return 0;
  2225. if (off+len > i_size)
  2226. len = i_size-off;
  2227. toread = len;
  2228. while (toread > 0) {
  2229. tocopy = sb->s_blocksize - offset < toread ?
  2230. sb->s_blocksize - offset : toread;
  2231. bh = ext3_bread(NULL, inode, blk, 0, &err);
  2232. if (err)
  2233. return err;
  2234. if (!bh) /* A hole? */
  2235. memset(data, 0, tocopy);
  2236. else
  2237. memcpy(data, bh->b_data+offset, tocopy);
  2238. brelse(bh);
  2239. offset = 0;
  2240. toread -= tocopy;
  2241. data += tocopy;
  2242. blk++;
  2243. }
  2244. return len;
  2245. }
  2246. /* Write to quotafile (we know the transaction is already started and has
  2247. * enough credits) */
  2248. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  2249. const char *data, size_t len, loff_t off)
  2250. {
  2251. struct inode *inode = sb_dqopt(sb)->files[type];
  2252. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2253. int err = 0;
  2254. int offset = off & (sb->s_blocksize - 1);
  2255. int tocopy;
  2256. int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
  2257. size_t towrite = len;
  2258. struct buffer_head *bh;
  2259. handle_t *handle = journal_current_handle();
  2260. down(&inode->i_sem);
  2261. while (towrite > 0) {
  2262. tocopy = sb->s_blocksize - offset < towrite ?
  2263. sb->s_blocksize - offset : towrite;
  2264. bh = ext3_bread(handle, inode, blk, 1, &err);
  2265. if (!bh)
  2266. goto out;
  2267. if (journal_quota) {
  2268. err = ext3_journal_get_write_access(handle, bh);
  2269. if (err) {
  2270. brelse(bh);
  2271. goto out;
  2272. }
  2273. }
  2274. lock_buffer(bh);
  2275. memcpy(bh->b_data+offset, data, tocopy);
  2276. flush_dcache_page(bh->b_page);
  2277. unlock_buffer(bh);
  2278. if (journal_quota)
  2279. err = ext3_journal_dirty_metadata(handle, bh);
  2280. else {
  2281. /* Always do at least ordered writes for quotas */
  2282. err = ext3_journal_dirty_data(handle, bh);
  2283. mark_buffer_dirty(bh);
  2284. }
  2285. brelse(bh);
  2286. if (err)
  2287. goto out;
  2288. offset = 0;
  2289. towrite -= tocopy;
  2290. data += tocopy;
  2291. blk++;
  2292. }
  2293. out:
  2294. if (len == towrite)
  2295. return err;
  2296. if (inode->i_size < off+len-towrite) {
  2297. i_size_write(inode, off+len-towrite);
  2298. EXT3_I(inode)->i_disksize = inode->i_size;
  2299. }
  2300. inode->i_version++;
  2301. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2302. ext3_mark_inode_dirty(handle, inode);
  2303. up(&inode->i_sem);
  2304. return len - towrite;
  2305. }
  2306. #endif
  2307. static struct super_block *ext3_get_sb(struct file_system_type *fs_type,
  2308. int flags, const char *dev_name, void *data)
  2309. {
  2310. return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
  2311. }
  2312. static struct file_system_type ext3_fs_type = {
  2313. .owner = THIS_MODULE,
  2314. .name = "ext3",
  2315. .get_sb = ext3_get_sb,
  2316. .kill_sb = kill_block_super,
  2317. .fs_flags = FS_REQUIRES_DEV,
  2318. };
  2319. static int __init init_ext3_fs(void)
  2320. {
  2321. int err = init_ext3_xattr();
  2322. if (err)
  2323. return err;
  2324. err = init_inodecache();
  2325. if (err)
  2326. goto out1;
  2327. err = register_filesystem(&ext3_fs_type);
  2328. if (err)
  2329. goto out;
  2330. return 0;
  2331. out:
  2332. destroy_inodecache();
  2333. out1:
  2334. exit_ext3_xattr();
  2335. return err;
  2336. }
  2337. static void __exit exit_ext3_fs(void)
  2338. {
  2339. unregister_filesystem(&ext3_fs_type);
  2340. destroy_inodecache();
  2341. exit_ext3_xattr();
  2342. }
  2343. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2344. MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
  2345. MODULE_LICENSE("GPL");
  2346. module_init(init_ext3_fs)
  2347. module_exit(exit_ext3_fs)