super.c 70 KB

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