super.c 70 KB

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