dquot: move dquot drop responsibility into the filesystem
[linux-2.6/kvm.git] / fs / ext3 / super.c
blob2277b1a98e622edb44d394b1805d1416d366eb47
1 /*
2 * linux/fs/ext3/super.c
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)
9 * from
11 * linux/fs/minix/inode.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * Big-endian to little-endian byte-swapping/bitmaps by
16 * David S. Miller (davem@caip.rutgers.edu), 1995
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/exportfs.h>
33 #include <linux/vfs.h>
34 #include <linux/random.h>
35 #include <linux/mount.h>
36 #include <linux/namei.h>
37 #include <linux/quotaops.h>
38 #include <linux/seq_file.h>
39 #include <linux/log2.h>
41 #include <asm/uaccess.h>
43 #include "xattr.h"
44 #include "acl.h"
45 #include "namei.h"
47 #ifdef CONFIG_EXT3_DEFAULTS_TO_ORDERED
48 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_ORDERED_DATA
49 #else
50 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_WRITEBACK_DATA
51 #endif
53 static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
54 unsigned long journal_devnum);
55 static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
56 unsigned int);
57 static int ext3_commit_super(struct super_block *sb,
58 struct ext3_super_block *es,
59 int sync);
60 static void ext3_mark_recovery_complete(struct super_block * sb,
61 struct ext3_super_block * es);
62 static void ext3_clear_journal_err(struct super_block * sb,
63 struct ext3_super_block * es);
64 static int ext3_sync_fs(struct super_block *sb, int wait);
65 static const char *ext3_decode_error(struct super_block * sb, int errno,
66 char nbuf[16]);
67 static int ext3_remount (struct super_block * sb, int * flags, char * data);
68 static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
69 static int ext3_unfreeze(struct super_block *sb);
70 static int ext3_freeze(struct super_block *sb);
73 * Wrappers for journal_start/end.
75 * The only special thing we need to do here is to make sure that all
76 * journal_end calls result in the superblock being marked dirty, so
77 * that sync() will call the filesystem's write_super callback if
78 * appropriate.
80 handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
82 journal_t *journal;
84 if (sb->s_flags & MS_RDONLY)
85 return ERR_PTR(-EROFS);
87 /* Special case here: if the journal has aborted behind our
88 * backs (eg. EIO in the commit thread), then we still need to
89 * take the FS itself readonly cleanly. */
90 journal = EXT3_SB(sb)->s_journal;
91 if (is_journal_aborted(journal)) {
92 ext3_abort(sb, __func__,
93 "Detected aborted journal");
94 return ERR_PTR(-EROFS);
97 return journal_start(journal, nblocks);
101 * The only special thing we need to do here is to make sure that all
102 * journal_stop calls result in the superblock being marked dirty, so
103 * that sync() will call the filesystem's write_super callback if
104 * appropriate.
106 int __ext3_journal_stop(const char *where, handle_t *handle)
108 struct super_block *sb;
109 int err;
110 int rc;
112 sb = handle->h_transaction->t_journal->j_private;
113 err = handle->h_err;
114 rc = journal_stop(handle);
116 if (!err)
117 err = rc;
118 if (err)
119 __ext3_std_error(sb, where, err);
120 return err;
123 void ext3_journal_abort_handle(const char *caller, const char *err_fn,
124 struct buffer_head *bh, handle_t *handle, int err)
126 char nbuf[16];
127 const char *errstr = ext3_decode_error(NULL, err, nbuf);
129 if (bh)
130 BUFFER_TRACE(bh, "abort");
132 if (!handle->h_err)
133 handle->h_err = err;
135 if (is_handle_aborted(handle))
136 return;
138 printk(KERN_ERR "EXT3-fs: %s: aborting transaction: %s in %s\n",
139 caller, errstr, err_fn);
141 journal_abort_handle(handle);
144 void ext3_msg(struct super_block *sb, const char *prefix,
145 const char *fmt, ...)
147 va_list args;
149 va_start(args, fmt);
150 printk("%sEXT3-fs (%s): ", prefix, sb->s_id);
151 vprintk(fmt, args);
152 printk("\n");
153 va_end(args);
156 /* Deal with the reporting of failure conditions on a filesystem such as
157 * inconsistencies detected or read IO failures.
159 * On ext2, we can store the error state of the filesystem in the
160 * superblock. That is not possible on ext3, because we may have other
161 * write ordering constraints on the superblock which prevent us from
162 * writing it out straight away; and given that the journal is about to
163 * be aborted, we can't rely on the current, or future, transactions to
164 * write out the superblock safely.
166 * We'll just use the journal_abort() error code to record an error in
167 * the journal instead. On recovery, the journal will compain about
168 * that error until we've noted it down and cleared it.
171 static void ext3_handle_error(struct super_block *sb)
173 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
175 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
176 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
178 if (sb->s_flags & MS_RDONLY)
179 return;
181 if (!test_opt (sb, ERRORS_CONT)) {
182 journal_t *journal = EXT3_SB(sb)->s_journal;
184 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
185 if (journal)
186 journal_abort(journal, -EIO);
188 if (test_opt (sb, ERRORS_RO)) {
189 ext3_msg(sb, KERN_CRIT,
190 "error: remounting filesystem read-only");
191 sb->s_flags |= MS_RDONLY;
193 ext3_commit_super(sb, es, 1);
194 if (test_opt(sb, ERRORS_PANIC))
195 panic("EXT3-fs (%s): panic forced after error\n",
196 sb->s_id);
199 void ext3_error (struct super_block * sb, const char * function,
200 const char * fmt, ...)
202 va_list args;
204 va_start(args, fmt);
205 printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
206 vprintk(fmt, args);
207 printk("\n");
208 va_end(args);
210 ext3_handle_error(sb);
213 static const char *ext3_decode_error(struct super_block * sb, int errno,
214 char nbuf[16])
216 char *errstr = NULL;
218 switch (errno) {
219 case -EIO:
220 errstr = "IO failure";
221 break;
222 case -ENOMEM:
223 errstr = "Out of memory";
224 break;
225 case -EROFS:
226 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
227 errstr = "Journal has aborted";
228 else
229 errstr = "Readonly filesystem";
230 break;
231 default:
232 /* If the caller passed in an extra buffer for unknown
233 * errors, textualise them now. Else we just return
234 * NULL. */
235 if (nbuf) {
236 /* Check for truncated error codes... */
237 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
238 errstr = nbuf;
240 break;
243 return errstr;
246 /* __ext3_std_error decodes expected errors from journaling functions
247 * automatically and invokes the appropriate error response. */
249 void __ext3_std_error (struct super_block * sb, const char * function,
250 int errno)
252 char nbuf[16];
253 const char *errstr;
255 /* Special case: if the error is EROFS, and we're not already
256 * inside a transaction, then there's really no point in logging
257 * an error. */
258 if (errno == -EROFS && journal_current_handle() == NULL &&
259 (sb->s_flags & MS_RDONLY))
260 return;
262 errstr = ext3_decode_error(sb, errno, nbuf);
263 ext3_msg(sb, KERN_CRIT, "error in %s: %s", function, errstr);
265 ext3_handle_error(sb);
269 * ext3_abort is a much stronger failure handler than ext3_error. The
270 * abort function may be used to deal with unrecoverable failures such
271 * as journal IO errors or ENOMEM at a critical moment in log management.
273 * We unconditionally force the filesystem into an ABORT|READONLY state,
274 * unless the error response on the fs has been set to panic in which
275 * case we take the easy way out and panic immediately.
278 void ext3_abort (struct super_block * sb, const char * function,
279 const char * fmt, ...)
281 va_list args;
283 va_start(args, fmt);
284 printk(KERN_CRIT "EXT3-fs (%s): error: %s: ", sb->s_id, function);
285 vprintk(fmt, args);
286 printk("\n");
287 va_end(args);
289 if (test_opt(sb, ERRORS_PANIC))
290 panic("EXT3-fs: panic from previous error\n");
292 if (sb->s_flags & MS_RDONLY)
293 return;
295 ext3_msg(sb, KERN_CRIT,
296 "error: remounting filesystem read-only");
297 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
298 sb->s_flags |= MS_RDONLY;
299 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
300 if (EXT3_SB(sb)->s_journal)
301 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
304 void ext3_warning (struct super_block * sb, const char * function,
305 const char * fmt, ...)
307 va_list args;
309 va_start(args, fmt);
310 printk(KERN_WARNING "EXT3-fs (%s): warning: %s: ",
311 sb->s_id, function);
312 vprintk(fmt, args);
313 printk("\n");
314 va_end(args);
317 void ext3_update_dynamic_rev(struct super_block *sb)
319 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
321 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
322 return;
324 ext3_msg(sb, KERN_WARNING,
325 "warning: updating to rev %d because of "
326 "new feature flag, running e2fsck is recommended",
327 EXT3_DYNAMIC_REV);
329 es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
330 es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
331 es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
332 /* leave es->s_feature_*compat flags alone */
333 /* es->s_uuid will be set by e2fsck if empty */
336 * The rest of the superblock fields should be zero, and if not it
337 * means they are likely already in use, so leave them alone. We
338 * can leave it up to e2fsck to clean up any inconsistencies there.
343 * Open the external journal device
345 static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
347 struct block_device *bdev;
348 char b[BDEVNAME_SIZE];
350 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
351 if (IS_ERR(bdev))
352 goto fail;
353 return bdev;
355 fail:
356 ext3_msg(sb, "error: failed to open journal device %s: %ld",
357 __bdevname(dev, b), PTR_ERR(bdev));
359 return NULL;
363 * Release the journal device
365 static int ext3_blkdev_put(struct block_device *bdev)
367 bd_release(bdev);
368 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
371 static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
373 struct block_device *bdev;
374 int ret = -ENODEV;
376 bdev = sbi->journal_bdev;
377 if (bdev) {
378 ret = ext3_blkdev_put(bdev);
379 sbi->journal_bdev = NULL;
381 return ret;
384 static inline struct inode *orphan_list_entry(struct list_head *l)
386 return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
389 static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
391 struct list_head *l;
393 ext3_msg(sb, KERN_ERR, "error: sb orphan head is %d",
394 le32_to_cpu(sbi->s_es->s_last_orphan));
396 ext3_msg(sb, KERN_ERR, "sb_info orphan list:");
397 list_for_each(l, &sbi->s_orphan) {
398 struct inode *inode = orphan_list_entry(l);
399 ext3_msg(sb, KERN_ERR, " "
400 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
401 inode->i_sb->s_id, inode->i_ino, inode,
402 inode->i_mode, inode->i_nlink,
403 NEXT_ORPHAN(inode));
407 static void ext3_put_super (struct super_block * sb)
409 struct ext3_sb_info *sbi = EXT3_SB(sb);
410 struct ext3_super_block *es = sbi->s_es;
411 int i, err;
413 lock_kernel();
415 ext3_xattr_put_super(sb);
416 err = journal_destroy(sbi->s_journal);
417 sbi->s_journal = NULL;
418 if (err < 0)
419 ext3_abort(sb, __func__, "Couldn't clean up the journal");
421 if (!(sb->s_flags & MS_RDONLY)) {
422 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
423 es->s_state = cpu_to_le16(sbi->s_mount_state);
424 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
425 mark_buffer_dirty(sbi->s_sbh);
426 ext3_commit_super(sb, es, 1);
429 for (i = 0; i < sbi->s_gdb_count; i++)
430 brelse(sbi->s_group_desc[i]);
431 kfree(sbi->s_group_desc);
432 percpu_counter_destroy(&sbi->s_freeblocks_counter);
433 percpu_counter_destroy(&sbi->s_freeinodes_counter);
434 percpu_counter_destroy(&sbi->s_dirs_counter);
435 brelse(sbi->s_sbh);
436 #ifdef CONFIG_QUOTA
437 for (i = 0; i < MAXQUOTAS; i++)
438 kfree(sbi->s_qf_names[i]);
439 #endif
441 /* Debugging code just in case the in-memory inode orphan list
442 * isn't empty. The on-disk one can be non-empty if we've
443 * detected an error and taken the fs readonly, but the
444 * in-memory list had better be clean by this point. */
445 if (!list_empty(&sbi->s_orphan))
446 dump_orphan_list(sb, sbi);
447 J_ASSERT(list_empty(&sbi->s_orphan));
449 invalidate_bdev(sb->s_bdev);
450 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
452 * Invalidate the journal device's buffers. We don't want them
453 * floating about in memory - the physical journal device may
454 * hotswapped, and it breaks the `ro-after' testing code.
456 sync_blockdev(sbi->journal_bdev);
457 invalidate_bdev(sbi->journal_bdev);
458 ext3_blkdev_remove(sbi);
460 sb->s_fs_info = NULL;
461 kfree(sbi->s_blockgroup_lock);
462 kfree(sbi);
464 unlock_kernel();
467 static struct kmem_cache *ext3_inode_cachep;
470 * Called inside transaction, so use GFP_NOFS
472 static struct inode *ext3_alloc_inode(struct super_block *sb)
474 struct ext3_inode_info *ei;
476 ei = kmem_cache_alloc(ext3_inode_cachep, GFP_NOFS);
477 if (!ei)
478 return NULL;
479 ei->i_block_alloc_info = NULL;
480 ei->vfs_inode.i_version = 1;
481 atomic_set(&ei->i_datasync_tid, 0);
482 atomic_set(&ei->i_sync_tid, 0);
483 return &ei->vfs_inode;
486 static void ext3_destroy_inode(struct inode *inode)
488 if (!list_empty(&(EXT3_I(inode)->i_orphan))) {
489 printk("EXT3 Inode %p: orphan list check failed!\n",
490 EXT3_I(inode));
491 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
492 EXT3_I(inode), sizeof(struct ext3_inode_info),
493 false);
494 dump_stack();
496 kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
499 static void init_once(void *foo)
501 struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
503 INIT_LIST_HEAD(&ei->i_orphan);
504 #ifdef CONFIG_EXT3_FS_XATTR
505 init_rwsem(&ei->xattr_sem);
506 #endif
507 mutex_init(&ei->truncate_mutex);
508 inode_init_once(&ei->vfs_inode);
511 static int init_inodecache(void)
513 ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
514 sizeof(struct ext3_inode_info),
515 0, (SLAB_RECLAIM_ACCOUNT|
516 SLAB_MEM_SPREAD),
517 init_once);
518 if (ext3_inode_cachep == NULL)
519 return -ENOMEM;
520 return 0;
523 static void destroy_inodecache(void)
525 kmem_cache_destroy(ext3_inode_cachep);
528 static void ext3_clear_inode(struct inode *inode)
530 struct ext3_block_alloc_info *rsv = EXT3_I(inode)->i_block_alloc_info;
532 vfs_dq_drop(inode);
533 ext3_discard_reservation(inode);
534 EXT3_I(inode)->i_block_alloc_info = NULL;
535 if (unlikely(rsv))
536 kfree(rsv);
539 static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
541 #if defined(CONFIG_QUOTA)
542 struct ext3_sb_info *sbi = EXT3_SB(sb);
544 if (sbi->s_jquota_fmt) {
545 char *fmtname = "";
547 switch (sbi->s_jquota_fmt) {
548 case QFMT_VFS_OLD:
549 fmtname = "vfsold";
550 break;
551 case QFMT_VFS_V0:
552 fmtname = "vfsv0";
553 break;
554 case QFMT_VFS_V1:
555 fmtname = "vfsv1";
556 break;
558 seq_printf(seq, ",jqfmt=%s", fmtname);
561 if (sbi->s_qf_names[USRQUOTA])
562 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
564 if (sbi->s_qf_names[GRPQUOTA])
565 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
567 if (test_opt(sb, USRQUOTA))
568 seq_puts(seq, ",usrquota");
570 if (test_opt(sb, GRPQUOTA))
571 seq_puts(seq, ",grpquota");
572 #endif
575 static char *data_mode_string(unsigned long mode)
577 switch (mode) {
578 case EXT3_MOUNT_JOURNAL_DATA:
579 return "journal";
580 case EXT3_MOUNT_ORDERED_DATA:
581 return "ordered";
582 case EXT3_MOUNT_WRITEBACK_DATA:
583 return "writeback";
585 return "unknown";
589 * Show an option if
590 * - it's set to a non-default value OR
591 * - if the per-sb default is different from the global default
593 static int ext3_show_options(struct seq_file *seq, struct vfsmount *vfs)
595 struct super_block *sb = vfs->mnt_sb;
596 struct ext3_sb_info *sbi = EXT3_SB(sb);
597 struct ext3_super_block *es = sbi->s_es;
598 unsigned long def_mount_opts;
600 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
602 if (sbi->s_sb_block != 1)
603 seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
604 if (test_opt(sb, MINIX_DF))
605 seq_puts(seq, ",minixdf");
606 if (test_opt(sb, GRPID))
607 seq_puts(seq, ",grpid");
608 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT3_DEFM_BSDGROUPS))
609 seq_puts(seq, ",nogrpid");
610 if (sbi->s_resuid != EXT3_DEF_RESUID ||
611 le16_to_cpu(es->s_def_resuid) != EXT3_DEF_RESUID) {
612 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
614 if (sbi->s_resgid != EXT3_DEF_RESGID ||
615 le16_to_cpu(es->s_def_resgid) != EXT3_DEF_RESGID) {
616 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
618 if (test_opt(sb, ERRORS_RO)) {
619 int def_errors = le16_to_cpu(es->s_errors);
621 if (def_errors == EXT3_ERRORS_PANIC ||
622 def_errors == EXT3_ERRORS_CONTINUE) {
623 seq_puts(seq, ",errors=remount-ro");
626 if (test_opt(sb, ERRORS_CONT))
627 seq_puts(seq, ",errors=continue");
628 if (test_opt(sb, ERRORS_PANIC))
629 seq_puts(seq, ",errors=panic");
630 if (test_opt(sb, NO_UID32))
631 seq_puts(seq, ",nouid32");
632 if (test_opt(sb, DEBUG))
633 seq_puts(seq, ",debug");
634 if (test_opt(sb, OLDALLOC))
635 seq_puts(seq, ",oldalloc");
636 #ifdef CONFIG_EXT3_FS_XATTR
637 if (test_opt(sb, XATTR_USER))
638 seq_puts(seq, ",user_xattr");
639 if (!test_opt(sb, XATTR_USER) &&
640 (def_mount_opts & EXT3_DEFM_XATTR_USER)) {
641 seq_puts(seq, ",nouser_xattr");
643 #endif
644 #ifdef CONFIG_EXT3_FS_POSIX_ACL
645 if (test_opt(sb, POSIX_ACL))
646 seq_puts(seq, ",acl");
647 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT3_DEFM_ACL))
648 seq_puts(seq, ",noacl");
649 #endif
650 if (!test_opt(sb, RESERVATION))
651 seq_puts(seq, ",noreservation");
652 if (sbi->s_commit_interval) {
653 seq_printf(seq, ",commit=%u",
654 (unsigned) (sbi->s_commit_interval / HZ));
656 if (test_opt(sb, BARRIER))
657 seq_puts(seq, ",barrier=1");
658 if (test_opt(sb, NOBH))
659 seq_puts(seq, ",nobh");
661 seq_printf(seq, ",data=%s", data_mode_string(test_opt(sb, DATA_FLAGS)));
662 if (test_opt(sb, DATA_ERR_ABORT))
663 seq_puts(seq, ",data_err=abort");
665 if (test_opt(sb, NOLOAD))
666 seq_puts(seq, ",norecovery");
668 ext3_show_quota_options(seq, sb);
670 return 0;
674 static struct inode *ext3_nfs_get_inode(struct super_block *sb,
675 u64 ino, u32 generation)
677 struct inode *inode;
679 if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
680 return ERR_PTR(-ESTALE);
681 if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
682 return ERR_PTR(-ESTALE);
684 /* iget isn't really right if the inode is currently unallocated!!
686 * ext3_read_inode will return a bad_inode if the inode had been
687 * deleted, so we should be safe.
689 * Currently we don't know the generation for parent directory, so
690 * a generation of 0 means "accept any"
692 inode = ext3_iget(sb, ino);
693 if (IS_ERR(inode))
694 return ERR_CAST(inode);
695 if (generation && inode->i_generation != generation) {
696 iput(inode);
697 return ERR_PTR(-ESTALE);
700 return inode;
703 static struct dentry *ext3_fh_to_dentry(struct super_block *sb, struct fid *fid,
704 int fh_len, int fh_type)
706 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
707 ext3_nfs_get_inode);
710 static struct dentry *ext3_fh_to_parent(struct super_block *sb, struct fid *fid,
711 int fh_len, int fh_type)
713 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
714 ext3_nfs_get_inode);
718 * Try to release metadata pages (indirect blocks, directories) which are
719 * mapped via the block device. Since these pages could have journal heads
720 * which would prevent try_to_free_buffers() from freeing them, we must use
721 * jbd layer's try_to_free_buffers() function to release them.
723 static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
724 gfp_t wait)
726 journal_t *journal = EXT3_SB(sb)->s_journal;
728 WARN_ON(PageChecked(page));
729 if (!page_has_buffers(page))
730 return 0;
731 if (journal)
732 return journal_try_to_free_buffers(journal, page,
733 wait & ~__GFP_WAIT);
734 return try_to_free_buffers(page);
737 #ifdef CONFIG_QUOTA
738 #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
739 #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
741 static int ext3_write_dquot(struct dquot *dquot);
742 static int ext3_acquire_dquot(struct dquot *dquot);
743 static int ext3_release_dquot(struct dquot *dquot);
744 static int ext3_mark_dquot_dirty(struct dquot *dquot);
745 static int ext3_write_info(struct super_block *sb, int type);
746 static int ext3_quota_on(struct super_block *sb, int type, int format_id,
747 char *path, int remount);
748 static int ext3_quota_on_mount(struct super_block *sb, int type);
749 static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
750 size_t len, loff_t off);
751 static ssize_t ext3_quota_write(struct super_block *sb, int type,
752 const char *data, size_t len, loff_t off);
754 static const struct dquot_operations ext3_quota_operations = {
755 .initialize = dquot_initialize,
756 .drop = dquot_drop,
757 .write_dquot = ext3_write_dquot,
758 .acquire_dquot = ext3_acquire_dquot,
759 .release_dquot = ext3_release_dquot,
760 .mark_dirty = ext3_mark_dquot_dirty,
761 .write_info = ext3_write_info,
762 .alloc_dquot = dquot_alloc,
763 .destroy_dquot = dquot_destroy,
766 static const struct quotactl_ops ext3_qctl_operations = {
767 .quota_on = ext3_quota_on,
768 .quota_off = vfs_quota_off,
769 .quota_sync = vfs_quota_sync,
770 .get_info = vfs_get_dqinfo,
771 .set_info = vfs_set_dqinfo,
772 .get_dqblk = vfs_get_dqblk,
773 .set_dqblk = vfs_set_dqblk
775 #endif
777 static const struct super_operations ext3_sops = {
778 .alloc_inode = ext3_alloc_inode,
779 .destroy_inode = ext3_destroy_inode,
780 .write_inode = ext3_write_inode,
781 .dirty_inode = ext3_dirty_inode,
782 .delete_inode = ext3_delete_inode,
783 .put_super = ext3_put_super,
784 .sync_fs = ext3_sync_fs,
785 .freeze_fs = ext3_freeze,
786 .unfreeze_fs = ext3_unfreeze,
787 .statfs = ext3_statfs,
788 .remount_fs = ext3_remount,
789 .clear_inode = ext3_clear_inode,
790 .show_options = ext3_show_options,
791 #ifdef CONFIG_QUOTA
792 .quota_read = ext3_quota_read,
793 .quota_write = ext3_quota_write,
794 #endif
795 .bdev_try_to_free_page = bdev_try_to_free_page,
798 static const struct export_operations ext3_export_ops = {
799 .fh_to_dentry = ext3_fh_to_dentry,
800 .fh_to_parent = ext3_fh_to_parent,
801 .get_parent = ext3_get_parent,
804 enum {
805 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
806 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
807 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
808 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
809 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
810 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
811 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
812 Opt_data_err_abort, Opt_data_err_ignore,
813 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
814 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
815 Opt_noquota, Opt_ignore, Opt_barrier, Opt_err, Opt_resize,
816 Opt_usrquota, Opt_grpquota
819 static const match_table_t tokens = {
820 {Opt_bsd_df, "bsddf"},
821 {Opt_minix_df, "minixdf"},
822 {Opt_grpid, "grpid"},
823 {Opt_grpid, "bsdgroups"},
824 {Opt_nogrpid, "nogrpid"},
825 {Opt_nogrpid, "sysvgroups"},
826 {Opt_resgid, "resgid=%u"},
827 {Opt_resuid, "resuid=%u"},
828 {Opt_sb, "sb=%u"},
829 {Opt_err_cont, "errors=continue"},
830 {Opt_err_panic, "errors=panic"},
831 {Opt_err_ro, "errors=remount-ro"},
832 {Opt_nouid32, "nouid32"},
833 {Opt_nocheck, "nocheck"},
834 {Opt_nocheck, "check=none"},
835 {Opt_debug, "debug"},
836 {Opt_oldalloc, "oldalloc"},
837 {Opt_orlov, "orlov"},
838 {Opt_user_xattr, "user_xattr"},
839 {Opt_nouser_xattr, "nouser_xattr"},
840 {Opt_acl, "acl"},
841 {Opt_noacl, "noacl"},
842 {Opt_reservation, "reservation"},
843 {Opt_noreservation, "noreservation"},
844 {Opt_noload, "noload"},
845 {Opt_noload, "norecovery"},
846 {Opt_nobh, "nobh"},
847 {Opt_bh, "bh"},
848 {Opt_commit, "commit=%u"},
849 {Opt_journal_update, "journal=update"},
850 {Opt_journal_inum, "journal=%u"},
851 {Opt_journal_dev, "journal_dev=%u"},
852 {Opt_abort, "abort"},
853 {Opt_data_journal, "data=journal"},
854 {Opt_data_ordered, "data=ordered"},
855 {Opt_data_writeback, "data=writeback"},
856 {Opt_data_err_abort, "data_err=abort"},
857 {Opt_data_err_ignore, "data_err=ignore"},
858 {Opt_offusrjquota, "usrjquota="},
859 {Opt_usrjquota, "usrjquota=%s"},
860 {Opt_offgrpjquota, "grpjquota="},
861 {Opt_grpjquota, "grpjquota=%s"},
862 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
863 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
864 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
865 {Opt_grpquota, "grpquota"},
866 {Opt_noquota, "noquota"},
867 {Opt_quota, "quota"},
868 {Opt_usrquota, "usrquota"},
869 {Opt_barrier, "barrier=%u"},
870 {Opt_resize, "resize"},
871 {Opt_err, NULL},
874 static ext3_fsblk_t get_sb_block(void **data, struct super_block *sb)
876 ext3_fsblk_t sb_block;
877 char *options = (char *) *data;
879 if (!options || strncmp(options, "sb=", 3) != 0)
880 return 1; /* Default location */
881 options += 3;
882 /*todo: use simple_strtoll with >32bit ext3 */
883 sb_block = simple_strtoul(options, &options, 0);
884 if (*options && *options != ',') {
885 ext3_msg(sb, "error: invalid sb specification: %s",
886 (char *) *data);
887 return 1;
889 if (*options == ',')
890 options++;
891 *data = (void *) options;
892 return sb_block;
895 #ifdef CONFIG_QUOTA
896 static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
898 struct ext3_sb_info *sbi = EXT3_SB(sb);
899 char *qname;
901 if (sb_any_quota_loaded(sb) &&
902 !sbi->s_qf_names[qtype]) {
903 ext3_msg(sb, KERN_ERR,
904 "Cannot change journaled "
905 "quota options when quota turned on");
906 return 0;
908 qname = match_strdup(args);
909 if (!qname) {
910 ext3_msg(sb, KERN_ERR,
911 "Not enough memory for storing quotafile name");
912 return 0;
914 if (sbi->s_qf_names[qtype] &&
915 strcmp(sbi->s_qf_names[qtype], qname)) {
916 ext3_msg(sb, KERN_ERR,
917 "%s quota file already specified", QTYPE2NAME(qtype));
918 kfree(qname);
919 return 0;
921 sbi->s_qf_names[qtype] = qname;
922 if (strchr(sbi->s_qf_names[qtype], '/')) {
923 ext3_msg(sb, KERN_ERR,
924 "quotafile must be on filesystem root");
925 kfree(sbi->s_qf_names[qtype]);
926 sbi->s_qf_names[qtype] = NULL;
927 return 0;
929 set_opt(sbi->s_mount_opt, QUOTA);
930 return 1;
933 static int clear_qf_name(struct super_block *sb, int qtype) {
935 struct ext3_sb_info *sbi = EXT3_SB(sb);
937 if (sb_any_quota_loaded(sb) &&
938 sbi->s_qf_names[qtype]) {
939 ext3_msg(sb, KERN_ERR, "Cannot change journaled quota options"
940 " when quota turned on");
941 return 0;
944 * The space will be released later when all options are confirmed
945 * to be correct
947 sbi->s_qf_names[qtype] = NULL;
948 return 1;
950 #endif
952 static int parse_options (char *options, struct super_block *sb,
953 unsigned int *inum, unsigned long *journal_devnum,
954 ext3_fsblk_t *n_blocks_count, int is_remount)
956 struct ext3_sb_info *sbi = EXT3_SB(sb);
957 char * p;
958 substring_t args[MAX_OPT_ARGS];
959 int data_opt = 0;
960 int option;
961 #ifdef CONFIG_QUOTA
962 int qfmt;
963 #endif
965 if (!options)
966 return 1;
968 while ((p = strsep (&options, ",")) != NULL) {
969 int token;
970 if (!*p)
971 continue;
973 token = match_token(p, tokens, args);
974 switch (token) {
975 case Opt_bsd_df:
976 clear_opt (sbi->s_mount_opt, MINIX_DF);
977 break;
978 case Opt_minix_df:
979 set_opt (sbi->s_mount_opt, MINIX_DF);
980 break;
981 case Opt_grpid:
982 set_opt (sbi->s_mount_opt, GRPID);
983 break;
984 case Opt_nogrpid:
985 clear_opt (sbi->s_mount_opt, GRPID);
986 break;
987 case Opt_resuid:
988 if (match_int(&args[0], &option))
989 return 0;
990 sbi->s_resuid = option;
991 break;
992 case Opt_resgid:
993 if (match_int(&args[0], &option))
994 return 0;
995 sbi->s_resgid = option;
996 break;
997 case Opt_sb:
998 /* handled by get_sb_block() instead of here */
999 /* *sb_block = match_int(&args[0]); */
1000 break;
1001 case Opt_err_panic:
1002 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1003 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1004 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
1005 break;
1006 case Opt_err_ro:
1007 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1008 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1009 set_opt (sbi->s_mount_opt, ERRORS_RO);
1010 break;
1011 case Opt_err_cont:
1012 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1013 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1014 set_opt (sbi->s_mount_opt, ERRORS_CONT);
1015 break;
1016 case Opt_nouid32:
1017 set_opt (sbi->s_mount_opt, NO_UID32);
1018 break;
1019 case Opt_nocheck:
1020 clear_opt (sbi->s_mount_opt, CHECK);
1021 break;
1022 case Opt_debug:
1023 set_opt (sbi->s_mount_opt, DEBUG);
1024 break;
1025 case Opt_oldalloc:
1026 set_opt (sbi->s_mount_opt, OLDALLOC);
1027 break;
1028 case Opt_orlov:
1029 clear_opt (sbi->s_mount_opt, OLDALLOC);
1030 break;
1031 #ifdef CONFIG_EXT3_FS_XATTR
1032 case Opt_user_xattr:
1033 set_opt (sbi->s_mount_opt, XATTR_USER);
1034 break;
1035 case Opt_nouser_xattr:
1036 clear_opt (sbi->s_mount_opt, XATTR_USER);
1037 break;
1038 #else
1039 case Opt_user_xattr:
1040 case Opt_nouser_xattr:
1041 ext3_msg(sb, KERN_INFO,
1042 "(no)user_xattr options not supported");
1043 break;
1044 #endif
1045 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1046 case Opt_acl:
1047 set_opt(sbi->s_mount_opt, POSIX_ACL);
1048 break;
1049 case Opt_noacl:
1050 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1051 break;
1052 #else
1053 case Opt_acl:
1054 case Opt_noacl:
1055 ext3_msg(sb, KERN_INFO,
1056 "(no)acl options not supported");
1057 break;
1058 #endif
1059 case Opt_reservation:
1060 set_opt(sbi->s_mount_opt, RESERVATION);
1061 break;
1062 case Opt_noreservation:
1063 clear_opt(sbi->s_mount_opt, RESERVATION);
1064 break;
1065 case Opt_journal_update:
1066 /* @@@ FIXME */
1067 /* Eventually we will want to be able to create
1068 a journal file here. For now, only allow the
1069 user to specify an existing inode to be the
1070 journal file. */
1071 if (is_remount) {
1072 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1073 "journal on remount");
1074 return 0;
1076 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1077 break;
1078 case Opt_journal_inum:
1079 if (is_remount) {
1080 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1081 "journal on remount");
1082 return 0;
1084 if (match_int(&args[0], &option))
1085 return 0;
1086 *inum = option;
1087 break;
1088 case Opt_journal_dev:
1089 if (is_remount) {
1090 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1091 "journal on remount");
1092 return 0;
1094 if (match_int(&args[0], &option))
1095 return 0;
1096 *journal_devnum = option;
1097 break;
1098 case Opt_noload:
1099 set_opt (sbi->s_mount_opt, NOLOAD);
1100 break;
1101 case Opt_commit:
1102 if (match_int(&args[0], &option))
1103 return 0;
1104 if (option < 0)
1105 return 0;
1106 if (option == 0)
1107 option = JBD_DEFAULT_MAX_COMMIT_AGE;
1108 sbi->s_commit_interval = HZ * option;
1109 break;
1110 case Opt_data_journal:
1111 data_opt = EXT3_MOUNT_JOURNAL_DATA;
1112 goto datacheck;
1113 case Opt_data_ordered:
1114 data_opt = EXT3_MOUNT_ORDERED_DATA;
1115 goto datacheck;
1116 case Opt_data_writeback:
1117 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
1118 datacheck:
1119 if (is_remount) {
1120 if (test_opt(sb, DATA_FLAGS) == data_opt)
1121 break;
1122 ext3_msg(sb, KERN_ERR,
1123 "error: cannot change "
1124 "data mode on remount. The filesystem "
1125 "is mounted in data=%s mode and you "
1126 "try to remount it in data=%s mode.",
1127 data_mode_string(test_opt(sb,
1128 DATA_FLAGS)),
1129 data_mode_string(data_opt));
1130 return 0;
1131 } else {
1132 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
1133 sbi->s_mount_opt |= data_opt;
1135 break;
1136 case Opt_data_err_abort:
1137 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1138 break;
1139 case Opt_data_err_ignore:
1140 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1141 break;
1142 #ifdef CONFIG_QUOTA
1143 case Opt_usrjquota:
1144 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1145 return 0;
1146 break;
1147 case Opt_grpjquota:
1148 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
1149 return 0;
1150 break;
1151 case Opt_offusrjquota:
1152 if (!clear_qf_name(sb, USRQUOTA))
1153 return 0;
1154 break;
1155 case Opt_offgrpjquota:
1156 if (!clear_qf_name(sb, GRPQUOTA))
1157 return 0;
1158 break;
1159 case Opt_jqfmt_vfsold:
1160 qfmt = QFMT_VFS_OLD;
1161 goto set_qf_format;
1162 case Opt_jqfmt_vfsv0:
1163 qfmt = QFMT_VFS_V0;
1164 goto set_qf_format;
1165 case Opt_jqfmt_vfsv1:
1166 qfmt = QFMT_VFS_V1;
1167 set_qf_format:
1168 if (sb_any_quota_loaded(sb) &&
1169 sbi->s_jquota_fmt != qfmt) {
1170 ext3_msg(sb, KERN_ERR, "error: cannot change "
1171 "journaled quota options when "
1172 "quota turned on.");
1173 return 0;
1175 sbi->s_jquota_fmt = qfmt;
1176 break;
1177 case Opt_quota:
1178 case Opt_usrquota:
1179 set_opt(sbi->s_mount_opt, QUOTA);
1180 set_opt(sbi->s_mount_opt, USRQUOTA);
1181 break;
1182 case Opt_grpquota:
1183 set_opt(sbi->s_mount_opt, QUOTA);
1184 set_opt(sbi->s_mount_opt, GRPQUOTA);
1185 break;
1186 case Opt_noquota:
1187 if (sb_any_quota_loaded(sb)) {
1188 ext3_msg(sb, KERN_ERR, "error: cannot change "
1189 "quota options when quota turned on.");
1190 return 0;
1192 clear_opt(sbi->s_mount_opt, QUOTA);
1193 clear_opt(sbi->s_mount_opt, USRQUOTA);
1194 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1195 break;
1196 #else
1197 case Opt_quota:
1198 case Opt_usrquota:
1199 case Opt_grpquota:
1200 ext3_msg(sb, KERN_ERR,
1201 "error: quota options not supported.");
1202 break;
1203 case Opt_usrjquota:
1204 case Opt_grpjquota:
1205 case Opt_offusrjquota:
1206 case Opt_offgrpjquota:
1207 case Opt_jqfmt_vfsold:
1208 case Opt_jqfmt_vfsv0:
1209 case Opt_jqfmt_vfsv1:
1210 ext3_msg(sb, KERN_ERR,
1211 "error: journaled quota options not "
1212 "supported.");
1213 break;
1214 case Opt_noquota:
1215 break;
1216 #endif
1217 case Opt_abort:
1218 set_opt(sbi->s_mount_opt, ABORT);
1219 break;
1220 case Opt_barrier:
1221 if (match_int(&args[0], &option))
1222 return 0;
1223 if (option)
1224 set_opt(sbi->s_mount_opt, BARRIER);
1225 else
1226 clear_opt(sbi->s_mount_opt, BARRIER);
1227 break;
1228 case Opt_ignore:
1229 break;
1230 case Opt_resize:
1231 if (!is_remount) {
1232 ext3_msg(sb, KERN_ERR,
1233 "error: resize option only available "
1234 "for remount");
1235 return 0;
1237 if (match_int(&args[0], &option) != 0)
1238 return 0;
1239 *n_blocks_count = option;
1240 break;
1241 case Opt_nobh:
1242 set_opt(sbi->s_mount_opt, NOBH);
1243 break;
1244 case Opt_bh:
1245 clear_opt(sbi->s_mount_opt, NOBH);
1246 break;
1247 default:
1248 ext3_msg(sb, KERN_ERR,
1249 "error: unrecognized mount option \"%s\" "
1250 "or missing value", p);
1251 return 0;
1254 #ifdef CONFIG_QUOTA
1255 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1256 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1257 clear_opt(sbi->s_mount_opt, USRQUOTA);
1258 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1259 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1261 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1262 ext3_msg(sb, KERN_ERR, "error: old and new quota "
1263 "format mixing.");
1264 return 0;
1267 if (!sbi->s_jquota_fmt) {
1268 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
1269 "not specified.");
1270 return 0;
1272 } else {
1273 if (sbi->s_jquota_fmt) {
1274 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
1275 "specified with no journaling "
1276 "enabled.");
1277 return 0;
1280 #endif
1281 return 1;
1284 static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
1285 int read_only)
1287 struct ext3_sb_info *sbi = EXT3_SB(sb);
1288 int res = 0;
1290 if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
1291 ext3_msg(sb, KERN_ERR,
1292 "error: revision level too high, "
1293 "forcing read-only mode");
1294 res = MS_RDONLY;
1296 if (read_only)
1297 return res;
1298 if (!(sbi->s_mount_state & EXT3_VALID_FS))
1299 ext3_msg(sb, KERN_WARNING,
1300 "warning: mounting unchecked fs, "
1301 "running e2fsck is recommended");
1302 else if ((sbi->s_mount_state & EXT3_ERROR_FS))
1303 ext3_msg(sb, KERN_WARNING,
1304 "warning: mounting fs with errors, "
1305 "running e2fsck is recommended");
1306 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1307 le16_to_cpu(es->s_mnt_count) >=
1308 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1309 ext3_msg(sb, KERN_WARNING,
1310 "warning: maximal mount count reached, "
1311 "running e2fsck is recommended");
1312 else if (le32_to_cpu(es->s_checkinterval) &&
1313 (le32_to_cpu(es->s_lastcheck) +
1314 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1315 ext3_msg(sb, KERN_WARNING,
1316 "warning: checktime reached, "
1317 "running e2fsck is recommended");
1318 #if 0
1319 /* @@@ We _will_ want to clear the valid bit if we find
1320 inconsistencies, to force a fsck at reboot. But for
1321 a plain journaled filesystem we can keep it set as
1322 valid forever! :) */
1323 es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
1324 #endif
1325 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1326 es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
1327 le16_add_cpu(&es->s_mnt_count, 1);
1328 es->s_mtime = cpu_to_le32(get_seconds());
1329 ext3_update_dynamic_rev(sb);
1330 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1332 ext3_commit_super(sb, es, 1);
1333 if (test_opt(sb, DEBUG))
1334 ext3_msg(sb, KERN_INFO, "[bs=%lu, gc=%lu, "
1335 "bpg=%lu, ipg=%lu, mo=%04lx]",
1336 sb->s_blocksize,
1337 sbi->s_groups_count,
1338 EXT3_BLOCKS_PER_GROUP(sb),
1339 EXT3_INODES_PER_GROUP(sb),
1340 sbi->s_mount_opt);
1342 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
1343 char b[BDEVNAME_SIZE];
1344 ext3_msg(sb, KERN_INFO, "using external journal on %s",
1345 bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
1346 } else {
1347 ext3_msg(sb, KERN_INFO, "using internal journal");
1349 return res;
1352 /* Called at mount-time, super-block is locked */
1353 static int ext3_check_descriptors(struct super_block *sb)
1355 struct ext3_sb_info *sbi = EXT3_SB(sb);
1356 int i;
1358 ext3_debug ("Checking group descriptors");
1360 for (i = 0; i < sbi->s_groups_count; i++) {
1361 struct ext3_group_desc *gdp = ext3_get_group_desc(sb, i, NULL);
1362 ext3_fsblk_t first_block = ext3_group_first_block_no(sb, i);
1363 ext3_fsblk_t last_block;
1365 if (i == sbi->s_groups_count - 1)
1366 last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
1367 else
1368 last_block = first_block +
1369 (EXT3_BLOCKS_PER_GROUP(sb) - 1);
1371 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
1372 le32_to_cpu(gdp->bg_block_bitmap) > last_block)
1374 ext3_error (sb, "ext3_check_descriptors",
1375 "Block bitmap for group %d"
1376 " not in group (block %lu)!",
1377 i, (unsigned long)
1378 le32_to_cpu(gdp->bg_block_bitmap));
1379 return 0;
1381 if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
1382 le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
1384 ext3_error (sb, "ext3_check_descriptors",
1385 "Inode bitmap for group %d"
1386 " not in group (block %lu)!",
1387 i, (unsigned long)
1388 le32_to_cpu(gdp->bg_inode_bitmap));
1389 return 0;
1391 if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
1392 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
1393 last_block)
1395 ext3_error (sb, "ext3_check_descriptors",
1396 "Inode table for group %d"
1397 " not in group (block %lu)!",
1398 i, (unsigned long)
1399 le32_to_cpu(gdp->bg_inode_table));
1400 return 0;
1404 sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
1405 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
1406 return 1;
1410 /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
1411 * the superblock) which were deleted from all directories, but held open by
1412 * a process at the time of a crash. We walk the list and try to delete these
1413 * inodes at recovery time (only with a read-write filesystem).
1415 * In order to keep the orphan inode chain consistent during traversal (in
1416 * case of crash during recovery), we link each inode into the superblock
1417 * orphan list_head and handle it the same way as an inode deletion during
1418 * normal operation (which journals the operations for us).
1420 * We only do an iget() and an iput() on each inode, which is very safe if we
1421 * accidentally point at an in-use or already deleted inode. The worst that
1422 * can happen in this case is that we get a "bit already cleared" message from
1423 * ext3_free_inode(). The only reason we would point at a wrong inode is if
1424 * e2fsck was run on this filesystem, and it must have already done the orphan
1425 * inode cleanup for us, so we can safely abort without any further action.
1427 static void ext3_orphan_cleanup (struct super_block * sb,
1428 struct ext3_super_block * es)
1430 unsigned int s_flags = sb->s_flags;
1431 int nr_orphans = 0, nr_truncates = 0;
1432 #ifdef CONFIG_QUOTA
1433 int i;
1434 #endif
1435 if (!es->s_last_orphan) {
1436 jbd_debug(4, "no orphan inodes to clean up\n");
1437 return;
1440 if (bdev_read_only(sb->s_bdev)) {
1441 ext3_msg(sb, KERN_ERR, "error: write access "
1442 "unavailable, skipping orphan cleanup.");
1443 return;
1446 if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
1447 if (es->s_last_orphan)
1448 jbd_debug(1, "Errors on filesystem, "
1449 "clearing orphan list.\n");
1450 es->s_last_orphan = 0;
1451 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1452 return;
1455 if (s_flags & MS_RDONLY) {
1456 ext3_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
1457 sb->s_flags &= ~MS_RDONLY;
1459 #ifdef CONFIG_QUOTA
1460 /* Needed for iput() to work correctly and not trash data */
1461 sb->s_flags |= MS_ACTIVE;
1462 /* Turn on quotas so that they are updated correctly */
1463 for (i = 0; i < MAXQUOTAS; i++) {
1464 if (EXT3_SB(sb)->s_qf_names[i]) {
1465 int ret = ext3_quota_on_mount(sb, i);
1466 if (ret < 0)
1467 ext3_msg(sb, KERN_ERR,
1468 "error: cannot turn on journaled "
1469 "quota: %d", ret);
1472 #endif
1474 while (es->s_last_orphan) {
1475 struct inode *inode;
1477 inode = ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1478 if (IS_ERR(inode)) {
1479 es->s_last_orphan = 0;
1480 break;
1483 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
1484 vfs_dq_init(inode);
1485 if (inode->i_nlink) {
1486 printk(KERN_DEBUG
1487 "%s: truncating inode %lu to %Ld bytes\n",
1488 __func__, inode->i_ino, inode->i_size);
1489 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1490 inode->i_ino, inode->i_size);
1491 ext3_truncate(inode);
1492 nr_truncates++;
1493 } else {
1494 printk(KERN_DEBUG
1495 "%s: deleting unreferenced inode %lu\n",
1496 __func__, inode->i_ino);
1497 jbd_debug(2, "deleting unreferenced inode %lu\n",
1498 inode->i_ino);
1499 nr_orphans++;
1501 iput(inode); /* The delete magic happens here! */
1504 #define PLURAL(x) (x), ((x)==1) ? "" : "s"
1506 if (nr_orphans)
1507 ext3_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1508 PLURAL(nr_orphans));
1509 if (nr_truncates)
1510 ext3_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1511 PLURAL(nr_truncates));
1512 #ifdef CONFIG_QUOTA
1513 /* Turn quotas off */
1514 for (i = 0; i < MAXQUOTAS; i++) {
1515 if (sb_dqopt(sb)->files[i])
1516 vfs_quota_off(sb, i, 0);
1518 #endif
1519 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1523 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1524 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1525 * We need to be 1 filesystem block less than the 2^32 sector limit.
1527 static loff_t ext3_max_size(int bits)
1529 loff_t res = EXT3_NDIR_BLOCKS;
1530 int meta_blocks;
1531 loff_t upper_limit;
1533 /* This is calculated to be the largest file size for a
1534 * dense, file such that the total number of
1535 * sectors in the file, including data and all indirect blocks,
1536 * does not exceed 2^32 -1
1537 * __u32 i_blocks representing the total number of
1538 * 512 bytes blocks of the file
1540 upper_limit = (1LL << 32) - 1;
1542 /* total blocks in file system block size */
1543 upper_limit >>= (bits - 9);
1546 /* indirect blocks */
1547 meta_blocks = 1;
1548 /* double indirect blocks */
1549 meta_blocks += 1 + (1LL << (bits-2));
1550 /* tripple indirect blocks */
1551 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1553 upper_limit -= meta_blocks;
1554 upper_limit <<= bits;
1556 res += 1LL << (bits-2);
1557 res += 1LL << (2*(bits-2));
1558 res += 1LL << (3*(bits-2));
1559 res <<= bits;
1560 if (res > upper_limit)
1561 res = upper_limit;
1563 if (res > MAX_LFS_FILESIZE)
1564 res = MAX_LFS_FILESIZE;
1566 return res;
1569 static ext3_fsblk_t descriptor_loc(struct super_block *sb,
1570 ext3_fsblk_t logic_sb_block,
1571 int nr)
1573 struct ext3_sb_info *sbi = EXT3_SB(sb);
1574 unsigned long bg, first_meta_bg;
1575 int has_super = 0;
1577 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1579 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
1580 nr < first_meta_bg)
1581 return (logic_sb_block + nr + 1);
1582 bg = sbi->s_desc_per_block * nr;
1583 if (ext3_bg_has_super(sb, bg))
1584 has_super = 1;
1585 return (has_super + ext3_group_first_block_no(sb, bg));
1589 static int ext3_fill_super (struct super_block *sb, void *data, int silent)
1591 struct buffer_head * bh;
1592 struct ext3_super_block *es = NULL;
1593 struct ext3_sb_info *sbi;
1594 ext3_fsblk_t block;
1595 ext3_fsblk_t sb_block = get_sb_block(&data, sb);
1596 ext3_fsblk_t logic_sb_block;
1597 unsigned long offset = 0;
1598 unsigned int journal_inum = 0;
1599 unsigned long journal_devnum = 0;
1600 unsigned long def_mount_opts;
1601 struct inode *root;
1602 int blocksize;
1603 int hblock;
1604 int db_count;
1605 int i;
1606 int needs_recovery;
1607 int ret = -EINVAL;
1608 __le32 features;
1609 int err;
1611 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1612 if (!sbi)
1613 return -ENOMEM;
1615 sbi->s_blockgroup_lock =
1616 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
1617 if (!sbi->s_blockgroup_lock) {
1618 kfree(sbi);
1619 return -ENOMEM;
1621 sb->s_fs_info = sbi;
1622 sbi->s_mount_opt = 0;
1623 sbi->s_resuid = EXT3_DEF_RESUID;
1624 sbi->s_resgid = EXT3_DEF_RESGID;
1625 sbi->s_sb_block = sb_block;
1627 unlock_kernel();
1629 blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
1630 if (!blocksize) {
1631 ext3_msg(sb, KERN_ERR, "error: unable to set blocksize");
1632 goto out_fail;
1636 * The ext3 superblock will not be buffer aligned for other than 1kB
1637 * block sizes. We need to calculate the offset from buffer start.
1639 if (blocksize != EXT3_MIN_BLOCK_SIZE) {
1640 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1641 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1642 } else {
1643 logic_sb_block = sb_block;
1646 if (!(bh = sb_bread(sb, logic_sb_block))) {
1647 ext3_msg(sb, KERN_ERR, "error: unable to read superblock");
1648 goto out_fail;
1651 * Note: s_es must be initialized as soon as possible because
1652 * some ext3 macro-instructions depend on its value
1654 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
1655 sbi->s_es = es;
1656 sb->s_magic = le16_to_cpu(es->s_magic);
1657 if (sb->s_magic != EXT3_SUPER_MAGIC)
1658 goto cantfind_ext3;
1660 /* Set defaults before we parse the mount options */
1661 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1662 if (def_mount_opts & EXT3_DEFM_DEBUG)
1663 set_opt(sbi->s_mount_opt, DEBUG);
1664 if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
1665 set_opt(sbi->s_mount_opt, GRPID);
1666 if (def_mount_opts & EXT3_DEFM_UID16)
1667 set_opt(sbi->s_mount_opt, NO_UID32);
1668 #ifdef CONFIG_EXT3_FS_XATTR
1669 if (def_mount_opts & EXT3_DEFM_XATTR_USER)
1670 set_opt(sbi->s_mount_opt, XATTR_USER);
1671 #endif
1672 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1673 if (def_mount_opts & EXT3_DEFM_ACL)
1674 set_opt(sbi->s_mount_opt, POSIX_ACL);
1675 #endif
1676 if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
1677 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1678 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
1679 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1680 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
1681 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
1683 if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
1684 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1685 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_CONTINUE)
1686 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1687 else
1688 set_opt(sbi->s_mount_opt, ERRORS_RO);
1690 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1691 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1693 set_opt(sbi->s_mount_opt, RESERVATION);
1695 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1696 NULL, 0))
1697 goto failed_mount;
1699 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1700 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
1702 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
1703 (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
1704 EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1705 EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1706 ext3_msg(sb, KERN_WARNING,
1707 "warning: feature flags set on rev 0 fs, "
1708 "running e2fsck is recommended");
1710 * Check feature flags regardless of the revision level, since we
1711 * previously didn't change the revision level when setting the flags,
1712 * so there is a chance incompat flags are set on a rev 0 filesystem.
1714 features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
1715 if (features) {
1716 ext3_msg(sb, KERN_ERR,
1717 "error: couldn't mount because of unsupported "
1718 "optional features (%x)", le32_to_cpu(features));
1719 goto failed_mount;
1721 features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
1722 if (!(sb->s_flags & MS_RDONLY) && features) {
1723 ext3_msg(sb, KERN_ERR,
1724 "error: couldn't mount RDWR because of unsupported "
1725 "optional features (%x)", le32_to_cpu(features));
1726 goto failed_mount;
1728 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1730 if (blocksize < EXT3_MIN_BLOCK_SIZE ||
1731 blocksize > EXT3_MAX_BLOCK_SIZE) {
1732 ext3_msg(sb, KERN_ERR,
1733 "error: couldn't mount because of unsupported "
1734 "filesystem blocksize %d", blocksize);
1735 goto failed_mount;
1738 hblock = bdev_logical_block_size(sb->s_bdev);
1739 if (sb->s_blocksize != blocksize) {
1741 * Make sure the blocksize for the filesystem is larger
1742 * than the hardware sectorsize for the machine.
1744 if (blocksize < hblock) {
1745 ext3_msg(sb, KERN_ERR,
1746 "error: fsblocksize %d too small for "
1747 "hardware sectorsize %d", blocksize, hblock);
1748 goto failed_mount;
1751 brelse (bh);
1752 if (!sb_set_blocksize(sb, blocksize)) {
1753 ext3_msg(sb, KERN_ERR,
1754 "error: bad blocksize %d", blocksize);
1755 goto out_fail;
1757 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1758 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1759 bh = sb_bread(sb, logic_sb_block);
1760 if (!bh) {
1761 ext3_msg(sb, KERN_ERR,
1762 "error: can't read superblock on 2nd try");
1763 goto failed_mount;
1765 es = (struct ext3_super_block *)(((char *)bh->b_data) + offset);
1766 sbi->s_es = es;
1767 if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
1768 ext3_msg(sb, KERN_ERR,
1769 "error: magic mismatch");
1770 goto failed_mount;
1774 sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1776 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1777 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1778 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1779 } else {
1780 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1781 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1782 if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
1783 (!is_power_of_2(sbi->s_inode_size)) ||
1784 (sbi->s_inode_size > blocksize)) {
1785 ext3_msg(sb, KERN_ERR,
1786 "error: unsupported inode size: %d",
1787 sbi->s_inode_size);
1788 goto failed_mount;
1791 sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1792 le32_to_cpu(es->s_log_frag_size);
1793 if (blocksize != sbi->s_frag_size) {
1794 ext3_msg(sb, KERN_ERR,
1795 "error: fragsize %lu != blocksize %u (unsupported)",
1796 sbi->s_frag_size, blocksize);
1797 goto failed_mount;
1799 sbi->s_frags_per_block = 1;
1800 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1801 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1802 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
1803 if (EXT3_INODE_SIZE(sb) == 0 || EXT3_INODES_PER_GROUP(sb) == 0)
1804 goto cantfind_ext3;
1805 sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1806 if (sbi->s_inodes_per_block == 0)
1807 goto cantfind_ext3;
1808 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1809 sbi->s_inodes_per_block;
1810 sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1811 sbi->s_sbh = bh;
1812 sbi->s_mount_state = le16_to_cpu(es->s_state);
1813 sbi->s_addr_per_block_bits = ilog2(EXT3_ADDR_PER_BLOCK(sb));
1814 sbi->s_desc_per_block_bits = ilog2(EXT3_DESC_PER_BLOCK(sb));
1815 for (i=0; i < 4; i++)
1816 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1817 sbi->s_def_hash_version = es->s_def_hash_version;
1818 i = le32_to_cpu(es->s_flags);
1819 if (i & EXT2_FLAGS_UNSIGNED_HASH)
1820 sbi->s_hash_unsigned = 3;
1821 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
1822 #ifdef __CHAR_UNSIGNED__
1823 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
1824 sbi->s_hash_unsigned = 3;
1825 #else
1826 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
1827 #endif
1830 if (sbi->s_blocks_per_group > blocksize * 8) {
1831 ext3_msg(sb, KERN_ERR,
1832 "#blocks per group too big: %lu",
1833 sbi->s_blocks_per_group);
1834 goto failed_mount;
1836 if (sbi->s_frags_per_group > blocksize * 8) {
1837 ext3_msg(sb, KERN_ERR,
1838 "error: #fragments per group too big: %lu",
1839 sbi->s_frags_per_group);
1840 goto failed_mount;
1842 if (sbi->s_inodes_per_group > blocksize * 8) {
1843 ext3_msg(sb, KERN_ERR,
1844 "error: #inodes per group too big: %lu",
1845 sbi->s_inodes_per_group);
1846 goto failed_mount;
1849 if (le32_to_cpu(es->s_blocks_count) >
1850 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
1851 ext3_msg(sb, KERN_ERR,
1852 "error: filesystem is too large to mount safely");
1853 if (sizeof(sector_t) < 8)
1854 ext3_msg(sb, KERN_ERR,
1855 "error: CONFIG_LBDAF not enabled");
1856 goto failed_mount;
1859 if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
1860 goto cantfind_ext3;
1861 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
1862 le32_to_cpu(es->s_first_data_block) - 1)
1863 / EXT3_BLOCKS_PER_GROUP(sb)) + 1;
1864 db_count = (sbi->s_groups_count + EXT3_DESC_PER_BLOCK(sb) - 1) /
1865 EXT3_DESC_PER_BLOCK(sb);
1866 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1867 GFP_KERNEL);
1868 if (sbi->s_group_desc == NULL) {
1869 ext3_msg(sb, KERN_ERR,
1870 "error: not enough memory");
1871 goto failed_mount;
1874 bgl_lock_init(sbi->s_blockgroup_lock);
1876 for (i = 0; i < db_count; i++) {
1877 block = descriptor_loc(sb, logic_sb_block, i);
1878 sbi->s_group_desc[i] = sb_bread(sb, block);
1879 if (!sbi->s_group_desc[i]) {
1880 ext3_msg(sb, KERN_ERR,
1881 "error: can't read group descriptor %d", i);
1882 db_count = i;
1883 goto failed_mount2;
1886 if (!ext3_check_descriptors (sb)) {
1887 ext3_msg(sb, KERN_ERR,
1888 "error: group descriptors corrupted");
1889 goto failed_mount2;
1891 sbi->s_gdb_count = db_count;
1892 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1893 spin_lock_init(&sbi->s_next_gen_lock);
1895 err = percpu_counter_init(&sbi->s_freeblocks_counter,
1896 ext3_count_free_blocks(sb));
1897 if (!err) {
1898 err = percpu_counter_init(&sbi->s_freeinodes_counter,
1899 ext3_count_free_inodes(sb));
1901 if (!err) {
1902 err = percpu_counter_init(&sbi->s_dirs_counter,
1903 ext3_count_dirs(sb));
1905 if (err) {
1906 ext3_msg(sb, KERN_ERR, "error: insufficient memory");
1907 goto failed_mount3;
1910 /* per fileystem reservation list head & lock */
1911 spin_lock_init(&sbi->s_rsv_window_lock);
1912 sbi->s_rsv_window_root = RB_ROOT;
1913 /* Add a single, static dummy reservation to the start of the
1914 * reservation window list --- it gives us a placeholder for
1915 * append-at-start-of-list which makes the allocation logic
1916 * _much_ simpler. */
1917 sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1918 sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1919 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1920 sbi->s_rsv_window_head.rsv_goal_size = 0;
1921 ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
1924 * set up enough so that it can read an inode
1926 sb->s_op = &ext3_sops;
1927 sb->s_export_op = &ext3_export_ops;
1928 sb->s_xattr = ext3_xattr_handlers;
1929 #ifdef CONFIG_QUOTA
1930 sb->s_qcop = &ext3_qctl_operations;
1931 sb->dq_op = &ext3_quota_operations;
1932 #endif
1933 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1934 mutex_init(&sbi->s_orphan_lock);
1935 mutex_init(&sbi->s_resize_lock);
1937 sb->s_root = NULL;
1939 needs_recovery = (es->s_last_orphan != 0 ||
1940 EXT3_HAS_INCOMPAT_FEATURE(sb,
1941 EXT3_FEATURE_INCOMPAT_RECOVER));
1944 * The first inode we look at is the journal inode. Don't try
1945 * root first: it may be modified in the journal!
1947 if (!test_opt(sb, NOLOAD) &&
1948 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
1949 if (ext3_load_journal(sb, es, journal_devnum))
1950 goto failed_mount3;
1951 } else if (journal_inum) {
1952 if (ext3_create_journal(sb, es, journal_inum))
1953 goto failed_mount3;
1954 } else {
1955 if (!silent)
1956 ext3_msg(sb, KERN_ERR,
1957 "error: no journal found. "
1958 "mounting ext3 over ext2?");
1959 goto failed_mount3;
1962 /* We have now updated the journal if required, so we can
1963 * validate the data journaling mode. */
1964 switch (test_opt(sb, DATA_FLAGS)) {
1965 case 0:
1966 /* No mode set, assume a default based on the journal
1967 capabilities: ORDERED_DATA if the journal can
1968 cope, else JOURNAL_DATA */
1969 if (journal_check_available_features
1970 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
1971 set_opt(sbi->s_mount_opt, DEFAULT_DATA_MODE);
1972 else
1973 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1974 break;
1976 case EXT3_MOUNT_ORDERED_DATA:
1977 case EXT3_MOUNT_WRITEBACK_DATA:
1978 if (!journal_check_available_features
1979 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
1980 ext3_msg(sb, KERN_ERR,
1981 "error: journal does not support "
1982 "requested data journaling mode");
1983 goto failed_mount4;
1985 default:
1986 break;
1989 if (test_opt(sb, NOBH)) {
1990 if (!(test_opt(sb, DATA_FLAGS) == EXT3_MOUNT_WRITEBACK_DATA)) {
1991 ext3_msg(sb, KERN_WARNING,
1992 "warning: ignoring nobh option - "
1993 "it is supported only with writeback mode");
1994 clear_opt(sbi->s_mount_opt, NOBH);
1998 * The journal_load will have done any necessary log recovery,
1999 * so we can safely mount the rest of the filesystem now.
2002 root = ext3_iget(sb, EXT3_ROOT_INO);
2003 if (IS_ERR(root)) {
2004 ext3_msg(sb, KERN_ERR, "error: get root inode failed");
2005 ret = PTR_ERR(root);
2006 goto failed_mount4;
2008 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2009 iput(root);
2010 ext3_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
2011 goto failed_mount4;
2013 sb->s_root = d_alloc_root(root);
2014 if (!sb->s_root) {
2015 ext3_msg(sb, KERN_ERR, "error: get root dentry failed");
2016 iput(root);
2017 ret = -ENOMEM;
2018 goto failed_mount4;
2021 ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
2023 EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
2024 ext3_orphan_cleanup(sb, es);
2025 EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
2026 if (needs_recovery)
2027 ext3_msg(sb, KERN_INFO, "recovery complete");
2028 ext3_mark_recovery_complete(sb, es);
2029 ext3_msg(sb, KERN_INFO, "mounted filesystem with %s data mode",
2030 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
2031 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2032 "writeback");
2034 lock_kernel();
2035 return 0;
2037 cantfind_ext3:
2038 if (!silent)
2039 ext3_msg(sb, KERN_INFO,
2040 "error: can't find ext3 filesystem on dev %s.",
2041 sb->s_id);
2042 goto failed_mount;
2044 failed_mount4:
2045 journal_destroy(sbi->s_journal);
2046 failed_mount3:
2047 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2048 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2049 percpu_counter_destroy(&sbi->s_dirs_counter);
2050 failed_mount2:
2051 for (i = 0; i < db_count; i++)
2052 brelse(sbi->s_group_desc[i]);
2053 kfree(sbi->s_group_desc);
2054 failed_mount:
2055 #ifdef CONFIG_QUOTA
2056 for (i = 0; i < MAXQUOTAS; i++)
2057 kfree(sbi->s_qf_names[i]);
2058 #endif
2059 ext3_blkdev_remove(sbi);
2060 brelse(bh);
2061 out_fail:
2062 sb->s_fs_info = NULL;
2063 kfree(sbi->s_blockgroup_lock);
2064 kfree(sbi);
2065 lock_kernel();
2066 return ret;
2070 * Setup any per-fs journal parameters now. We'll do this both on
2071 * initial mount, once the journal has been initialised but before we've
2072 * done any recovery; and again on any subsequent remount.
2074 static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
2076 struct ext3_sb_info *sbi = EXT3_SB(sb);
2078 if (sbi->s_commit_interval)
2079 journal->j_commit_interval = sbi->s_commit_interval;
2080 /* We could also set up an ext3-specific default for the commit
2081 * interval here, but for now we'll just fall back to the jbd
2082 * default. */
2084 spin_lock(&journal->j_state_lock);
2085 if (test_opt(sb, BARRIER))
2086 journal->j_flags |= JFS_BARRIER;
2087 else
2088 journal->j_flags &= ~JFS_BARRIER;
2089 if (test_opt(sb, DATA_ERR_ABORT))
2090 journal->j_flags |= JFS_ABORT_ON_SYNCDATA_ERR;
2091 else
2092 journal->j_flags &= ~JFS_ABORT_ON_SYNCDATA_ERR;
2093 spin_unlock(&journal->j_state_lock);
2096 static journal_t *ext3_get_journal(struct super_block *sb,
2097 unsigned int journal_inum)
2099 struct inode *journal_inode;
2100 journal_t *journal;
2102 /* First, test for the existence of a valid inode on disk. Bad
2103 * things happen if we iget() an unused inode, as the subsequent
2104 * iput() will try to delete it. */
2106 journal_inode = ext3_iget(sb, journal_inum);
2107 if (IS_ERR(journal_inode)) {
2108 ext3_msg(sb, KERN_ERR, "error: no journal found");
2109 return NULL;
2111 if (!journal_inode->i_nlink) {
2112 make_bad_inode(journal_inode);
2113 iput(journal_inode);
2114 ext3_msg(sb, KERN_ERR, "error: journal inode is deleted");
2115 return NULL;
2118 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2119 journal_inode, journal_inode->i_size);
2120 if (!S_ISREG(journal_inode->i_mode)) {
2121 ext3_msg(sb, KERN_ERR, "error: invalid journal inode");
2122 iput(journal_inode);
2123 return NULL;
2126 journal = journal_init_inode(journal_inode);
2127 if (!journal) {
2128 ext3_msg(sb, KERN_ERR, "error: could not load journal inode");
2129 iput(journal_inode);
2130 return NULL;
2132 journal->j_private = sb;
2133 ext3_init_journal_params(sb, journal);
2134 return journal;
2137 static journal_t *ext3_get_dev_journal(struct super_block *sb,
2138 dev_t j_dev)
2140 struct buffer_head * bh;
2141 journal_t *journal;
2142 ext3_fsblk_t start;
2143 ext3_fsblk_t len;
2144 int hblock, blocksize;
2145 ext3_fsblk_t sb_block;
2146 unsigned long offset;
2147 struct ext3_super_block * es;
2148 struct block_device *bdev;
2150 bdev = ext3_blkdev_get(j_dev, sb);
2151 if (bdev == NULL)
2152 return NULL;
2154 if (bd_claim(bdev, sb)) {
2155 ext3_msg(sb, KERN_ERR,
2156 "error: failed to claim external journal device");
2157 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2158 return NULL;
2161 blocksize = sb->s_blocksize;
2162 hblock = bdev_logical_block_size(bdev);
2163 if (blocksize < hblock) {
2164 ext3_msg(sb, KERN_ERR,
2165 "error: blocksize too small for journal device");
2166 goto out_bdev;
2169 sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
2170 offset = EXT3_MIN_BLOCK_SIZE % blocksize;
2171 set_blocksize(bdev, blocksize);
2172 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2173 ext3_msg(sb, KERN_ERR, "error: couldn't read superblock of "
2174 "external journal");
2175 goto out_bdev;
2178 es = (struct ext3_super_block *) (((char *)bh->b_data) + offset);
2179 if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
2180 !(le32_to_cpu(es->s_feature_incompat) &
2181 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2182 ext3_msg(sb, KERN_ERR, "error: external journal has "
2183 "bad superblock");
2184 brelse(bh);
2185 goto out_bdev;
2188 if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2189 ext3_msg(sb, KERN_ERR, "error: journal UUID does not match");
2190 brelse(bh);
2191 goto out_bdev;
2194 len = le32_to_cpu(es->s_blocks_count);
2195 start = sb_block + 1;
2196 brelse(bh); /* we're done with the superblock */
2198 journal = journal_init_dev(bdev, sb->s_bdev,
2199 start, len, blocksize);
2200 if (!journal) {
2201 ext3_msg(sb, KERN_ERR,
2202 "error: failed to create device journal");
2203 goto out_bdev;
2205 journal->j_private = sb;
2206 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2207 wait_on_buffer(journal->j_sb_buffer);
2208 if (!buffer_uptodate(journal->j_sb_buffer)) {
2209 ext3_msg(sb, KERN_ERR, "I/O error on journal device");
2210 goto out_journal;
2212 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2213 ext3_msg(sb, KERN_ERR,
2214 "error: external journal has more than one "
2215 "user (unsupported) - %d",
2216 be32_to_cpu(journal->j_superblock->s_nr_users));
2217 goto out_journal;
2219 EXT3_SB(sb)->journal_bdev = bdev;
2220 ext3_init_journal_params(sb, journal);
2221 return journal;
2222 out_journal:
2223 journal_destroy(journal);
2224 out_bdev:
2225 ext3_blkdev_put(bdev);
2226 return NULL;
2229 static int ext3_load_journal(struct super_block *sb,
2230 struct ext3_super_block *es,
2231 unsigned long journal_devnum)
2233 journal_t *journal;
2234 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2235 dev_t journal_dev;
2236 int err = 0;
2237 int really_read_only;
2239 if (journal_devnum &&
2240 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2241 ext3_msg(sb, KERN_INFO, "external journal device major/minor "
2242 "numbers have changed");
2243 journal_dev = new_decode_dev(journal_devnum);
2244 } else
2245 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2247 really_read_only = bdev_read_only(sb->s_bdev);
2250 * Are we loading a blank journal or performing recovery after a
2251 * crash? For recovery, we need to check in advance whether we
2252 * can get read-write access to the device.
2255 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
2256 if (sb->s_flags & MS_RDONLY) {
2257 ext3_msg(sb, KERN_INFO,
2258 "recovery required on readonly filesystem");
2259 if (really_read_only) {
2260 ext3_msg(sb, KERN_ERR, "error: write access "
2261 "unavailable, cannot proceed");
2262 return -EROFS;
2264 ext3_msg(sb, KERN_INFO,
2265 "write access will be enabled during recovery");
2269 if (journal_inum && journal_dev) {
2270 ext3_msg(sb, KERN_ERR, "error: filesystem has both journal "
2271 "and inode journals");
2272 return -EINVAL;
2275 if (journal_inum) {
2276 if (!(journal = ext3_get_journal(sb, journal_inum)))
2277 return -EINVAL;
2278 } else {
2279 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
2280 return -EINVAL;
2283 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2284 err = journal_update_format(journal);
2285 if (err) {
2286 ext3_msg(sb, KERN_ERR, "error updating journal");
2287 journal_destroy(journal);
2288 return err;
2292 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
2293 err = journal_wipe(journal, !really_read_only);
2294 if (!err)
2295 err = journal_load(journal);
2297 if (err) {
2298 ext3_msg(sb, KERN_ERR, "error loading journal");
2299 journal_destroy(journal);
2300 return err;
2303 EXT3_SB(sb)->s_journal = journal;
2304 ext3_clear_journal_err(sb, es);
2306 if (journal_devnum &&
2307 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2308 es->s_journal_dev = cpu_to_le32(journal_devnum);
2310 /* Make sure we flush the recovery flag to disk. */
2311 ext3_commit_super(sb, es, 1);
2314 return 0;
2317 static int ext3_create_journal(struct super_block *sb,
2318 struct ext3_super_block *es,
2319 unsigned int journal_inum)
2321 journal_t *journal;
2322 int err;
2324 if (sb->s_flags & MS_RDONLY) {
2325 ext3_msg(sb, KERN_ERR,
2326 "error: readonly filesystem when trying to "
2327 "create journal");
2328 return -EROFS;
2331 journal = ext3_get_journal(sb, journal_inum);
2332 if (!journal)
2333 return -EINVAL;
2335 ext3_msg(sb, KERN_INFO, "creating new journal on inode %u",
2336 journal_inum);
2338 err = journal_create(journal);
2339 if (err) {
2340 ext3_msg(sb, KERN_ERR, "error creating journal");
2341 journal_destroy(journal);
2342 return -EIO;
2345 EXT3_SB(sb)->s_journal = journal;
2347 ext3_update_dynamic_rev(sb);
2348 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2349 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
2351 es->s_journal_inum = cpu_to_le32(journal_inum);
2353 /* Make sure we flush the recovery flag to disk. */
2354 ext3_commit_super(sb, es, 1);
2356 return 0;
2359 static int ext3_commit_super(struct super_block *sb,
2360 struct ext3_super_block *es,
2361 int sync)
2363 struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
2364 int error = 0;
2366 if (!sbh)
2367 return error;
2369 * If the file system is mounted read-only, don't update the
2370 * superblock write time. This avoids updating the superblock
2371 * write time when we are mounting the root file system
2372 * read/only but we need to replay the journal; at that point,
2373 * for people who are east of GMT and who make their clock
2374 * tick in localtime for Windows bug-for-bug compatibility,
2375 * the clock is set in the future, and this will cause e2fsck
2376 * to complain and force a full file system check.
2378 if (!(sb->s_flags & MS_RDONLY))
2379 es->s_wtime = cpu_to_le32(get_seconds());
2380 es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
2381 es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
2382 BUFFER_TRACE(sbh, "marking dirty");
2383 mark_buffer_dirty(sbh);
2384 if (sync)
2385 error = sync_dirty_buffer(sbh);
2386 return error;
2391 * Have we just finished recovery? If so, and if we are mounting (or
2392 * remounting) the filesystem readonly, then we will end up with a
2393 * consistent fs on disk. Record that fact.
2395 static void ext3_mark_recovery_complete(struct super_block * sb,
2396 struct ext3_super_block * es)
2398 journal_t *journal = EXT3_SB(sb)->s_journal;
2400 journal_lock_updates(journal);
2401 if (journal_flush(journal) < 0)
2402 goto out;
2404 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
2405 sb->s_flags & MS_RDONLY) {
2406 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2407 ext3_commit_super(sb, es, 1);
2410 out:
2411 journal_unlock_updates(journal);
2415 * If we are mounting (or read-write remounting) a filesystem whose journal
2416 * has recorded an error from a previous lifetime, move that error to the
2417 * main filesystem now.
2419 static void ext3_clear_journal_err(struct super_block *sb,
2420 struct ext3_super_block *es)
2422 journal_t *journal;
2423 int j_errno;
2424 const char *errstr;
2426 journal = EXT3_SB(sb)->s_journal;
2429 * Now check for any error status which may have been recorded in the
2430 * journal by a prior ext3_error() or ext3_abort()
2433 j_errno = journal_errno(journal);
2434 if (j_errno) {
2435 char nbuf[16];
2437 errstr = ext3_decode_error(sb, j_errno, nbuf);
2438 ext3_warning(sb, __func__, "Filesystem error recorded "
2439 "from previous mount: %s", errstr);
2440 ext3_warning(sb, __func__, "Marking fs in need of "
2441 "filesystem check.");
2443 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
2444 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
2445 ext3_commit_super (sb, es, 1);
2447 journal_clear_err(journal);
2452 * Force the running and committing transactions to commit,
2453 * and wait on the commit.
2455 int ext3_force_commit(struct super_block *sb)
2457 journal_t *journal;
2458 int ret;
2460 if (sb->s_flags & MS_RDONLY)
2461 return 0;
2463 journal = EXT3_SB(sb)->s_journal;
2464 ret = ext3_journal_force_commit(journal);
2465 return ret;
2468 static int ext3_sync_fs(struct super_block *sb, int wait)
2470 tid_t target;
2472 if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
2473 if (wait)
2474 log_wait_commit(EXT3_SB(sb)->s_journal, target);
2476 return 0;
2480 * LVM calls this function before a (read-only) snapshot is created. This
2481 * gives us a chance to flush the journal completely and mark the fs clean.
2483 static int ext3_freeze(struct super_block *sb)
2485 int error = 0;
2486 journal_t *journal;
2488 if (!(sb->s_flags & MS_RDONLY)) {
2489 journal = EXT3_SB(sb)->s_journal;
2491 /* Now we set up the journal barrier. */
2492 journal_lock_updates(journal);
2495 * We don't want to clear needs_recovery flag when we failed
2496 * to flush the journal.
2498 error = journal_flush(journal);
2499 if (error < 0)
2500 goto out;
2502 /* Journal blocked and flushed, clear needs_recovery flag. */
2503 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2504 error = ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2505 if (error)
2506 goto out;
2508 return 0;
2510 out:
2511 journal_unlock_updates(journal);
2512 return error;
2516 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2517 * flag here, even though the filesystem is not technically dirty yet.
2519 static int ext3_unfreeze(struct super_block *sb)
2521 if (!(sb->s_flags & MS_RDONLY)) {
2522 lock_super(sb);
2523 /* Reser the needs_recovery flag before the fs is unlocked. */
2524 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2525 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2526 unlock_super(sb);
2527 journal_unlock_updates(EXT3_SB(sb)->s_journal);
2529 return 0;
2532 static int ext3_remount (struct super_block * sb, int * flags, char * data)
2534 struct ext3_super_block * es;
2535 struct ext3_sb_info *sbi = EXT3_SB(sb);
2536 ext3_fsblk_t n_blocks_count = 0;
2537 unsigned long old_sb_flags;
2538 struct ext3_mount_options old_opts;
2539 int err;
2540 #ifdef CONFIG_QUOTA
2541 int i;
2542 #endif
2544 lock_kernel();
2546 /* Store the original options */
2547 lock_super(sb);
2548 old_sb_flags = sb->s_flags;
2549 old_opts.s_mount_opt = sbi->s_mount_opt;
2550 old_opts.s_resuid = sbi->s_resuid;
2551 old_opts.s_resgid = sbi->s_resgid;
2552 old_opts.s_commit_interval = sbi->s_commit_interval;
2553 #ifdef CONFIG_QUOTA
2554 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2555 for (i = 0; i < MAXQUOTAS; i++)
2556 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2557 #endif
2560 * Allow the "check" option to be passed as a remount option.
2562 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2563 err = -EINVAL;
2564 goto restore_opts;
2567 if (test_opt(sb, ABORT))
2568 ext3_abort(sb, __func__, "Abort forced by user");
2570 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2571 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
2573 es = sbi->s_es;
2575 ext3_init_journal_params(sb, sbi->s_journal);
2577 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2578 n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
2579 if (test_opt(sb, ABORT)) {
2580 err = -EROFS;
2581 goto restore_opts;
2584 if (*flags & MS_RDONLY) {
2586 * First of all, the unconditional stuff we have to do
2587 * to disable replay of the journal when we next remount
2589 sb->s_flags |= MS_RDONLY;
2592 * OK, test if we are remounting a valid rw partition
2593 * readonly, and if so set the rdonly flag and then
2594 * mark the partition as valid again.
2596 if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
2597 (sbi->s_mount_state & EXT3_VALID_FS))
2598 es->s_state = cpu_to_le16(sbi->s_mount_state);
2600 ext3_mark_recovery_complete(sb, es);
2601 } else {
2602 __le32 ret;
2603 if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
2604 ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
2605 ext3_msg(sb, KERN_WARNING,
2606 "warning: couldn't remount RDWR "
2607 "because of unsupported optional "
2608 "features (%x)", le32_to_cpu(ret));
2609 err = -EROFS;
2610 goto restore_opts;
2614 * If we have an unprocessed orphan list hanging
2615 * around from a previously readonly bdev mount,
2616 * require a full umount/remount for now.
2618 if (es->s_last_orphan) {
2619 ext3_msg(sb, KERN_WARNING, "warning: couldn't "
2620 "remount RDWR because of unprocessed "
2621 "orphan inode list. Please "
2622 "umount/remount instead.");
2623 err = -EINVAL;
2624 goto restore_opts;
2628 * Mounting a RDONLY partition read-write, so reread
2629 * and store the current valid flag. (It may have
2630 * been changed by e2fsck since we originally mounted
2631 * the partition.)
2633 ext3_clear_journal_err(sb, es);
2634 sbi->s_mount_state = le16_to_cpu(es->s_state);
2635 if ((err = ext3_group_extend(sb, es, n_blocks_count)))
2636 goto restore_opts;
2637 if (!ext3_setup_super (sb, es, 0))
2638 sb->s_flags &= ~MS_RDONLY;
2641 #ifdef CONFIG_QUOTA
2642 /* Release old quota file names */
2643 for (i = 0; i < MAXQUOTAS; i++)
2644 if (old_opts.s_qf_names[i] &&
2645 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2646 kfree(old_opts.s_qf_names[i]);
2647 #endif
2648 unlock_super(sb);
2649 unlock_kernel();
2650 return 0;
2651 restore_opts:
2652 sb->s_flags = old_sb_flags;
2653 sbi->s_mount_opt = old_opts.s_mount_opt;
2654 sbi->s_resuid = old_opts.s_resuid;
2655 sbi->s_resgid = old_opts.s_resgid;
2656 sbi->s_commit_interval = old_opts.s_commit_interval;
2657 #ifdef CONFIG_QUOTA
2658 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2659 for (i = 0; i < MAXQUOTAS; i++) {
2660 if (sbi->s_qf_names[i] &&
2661 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2662 kfree(sbi->s_qf_names[i]);
2663 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2665 #endif
2666 unlock_super(sb);
2667 unlock_kernel();
2668 return err;
2671 static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
2673 struct super_block *sb = dentry->d_sb;
2674 struct ext3_sb_info *sbi = EXT3_SB(sb);
2675 struct ext3_super_block *es = sbi->s_es;
2676 u64 fsid;
2678 if (test_opt(sb, MINIX_DF)) {
2679 sbi->s_overhead_last = 0;
2680 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2681 unsigned long ngroups = sbi->s_groups_count, i;
2682 ext3_fsblk_t overhead = 0;
2683 smp_rmb();
2686 * Compute the overhead (FS structures). This is constant
2687 * for a given filesystem unless the number of block groups
2688 * changes so we cache the previous value until it does.
2692 * All of the blocks before first_data_block are
2693 * overhead
2695 overhead = le32_to_cpu(es->s_first_data_block);
2698 * Add the overhead attributed to the superblock and
2699 * block group descriptors. If the sparse superblocks
2700 * feature is turned on, then not all groups have this.
2702 for (i = 0; i < ngroups; i++) {
2703 overhead += ext3_bg_has_super(sb, i) +
2704 ext3_bg_num_gdb(sb, i);
2705 cond_resched();
2709 * Every block group has an inode bitmap, a block
2710 * bitmap, and an inode table.
2712 overhead += ngroups * (2 + sbi->s_itb_per_group);
2713 sbi->s_overhead_last = overhead;
2714 smp_wmb();
2715 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
2718 buf->f_type = EXT3_SUPER_MAGIC;
2719 buf->f_bsize = sb->s_blocksize;
2720 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
2721 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
2722 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
2723 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
2724 buf->f_bavail = 0;
2725 buf->f_files = le32_to_cpu(es->s_inodes_count);
2726 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
2727 buf->f_namelen = EXT3_NAME_LEN;
2728 fsid = le64_to_cpup((void *)es->s_uuid) ^
2729 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2730 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2731 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
2732 return 0;
2735 /* Helper function for writing quotas on sync - we need to start transaction before quota file
2736 * is locked for write. Otherwise the are possible deadlocks:
2737 * Process 1 Process 2
2738 * ext3_create() quota_sync()
2739 * journal_start() write_dquot()
2740 * vfs_dq_init() down(dqio_mutex)
2741 * down(dqio_mutex) journal_start()
2745 #ifdef CONFIG_QUOTA
2747 static inline struct inode *dquot_to_inode(struct dquot *dquot)
2749 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2752 static int ext3_write_dquot(struct dquot *dquot)
2754 int ret, err;
2755 handle_t *handle;
2756 struct inode *inode;
2758 inode = dquot_to_inode(dquot);
2759 handle = ext3_journal_start(inode,
2760 EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
2761 if (IS_ERR(handle))
2762 return PTR_ERR(handle);
2763 ret = dquot_commit(dquot);
2764 err = ext3_journal_stop(handle);
2765 if (!ret)
2766 ret = err;
2767 return ret;
2770 static int ext3_acquire_dquot(struct dquot *dquot)
2772 int ret, err;
2773 handle_t *handle;
2775 handle = ext3_journal_start(dquot_to_inode(dquot),
2776 EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
2777 if (IS_ERR(handle))
2778 return PTR_ERR(handle);
2779 ret = dquot_acquire(dquot);
2780 err = ext3_journal_stop(handle);
2781 if (!ret)
2782 ret = err;
2783 return ret;
2786 static int ext3_release_dquot(struct dquot *dquot)
2788 int ret, err;
2789 handle_t *handle;
2791 handle = ext3_journal_start(dquot_to_inode(dquot),
2792 EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
2793 if (IS_ERR(handle)) {
2794 /* Release dquot anyway to avoid endless cycle in dqput() */
2795 dquot_release(dquot);
2796 return PTR_ERR(handle);
2798 ret = dquot_release(dquot);
2799 err = ext3_journal_stop(handle);
2800 if (!ret)
2801 ret = err;
2802 return ret;
2805 static int ext3_mark_dquot_dirty(struct dquot *dquot)
2807 /* Are we journaling quotas? */
2808 if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2809 EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2810 dquot_mark_dquot_dirty(dquot);
2811 return ext3_write_dquot(dquot);
2812 } else {
2813 return dquot_mark_dquot_dirty(dquot);
2817 static int ext3_write_info(struct super_block *sb, int type)
2819 int ret, err;
2820 handle_t *handle;
2822 /* Data block + inode block */
2823 handle = ext3_journal_start(sb->s_root->d_inode, 2);
2824 if (IS_ERR(handle))
2825 return PTR_ERR(handle);
2826 ret = dquot_commit_info(sb, type);
2827 err = ext3_journal_stop(handle);
2828 if (!ret)
2829 ret = err;
2830 return ret;
2834 * Turn on quotas during mount time - we need to find
2835 * the quota file and such...
2837 static int ext3_quota_on_mount(struct super_block *sb, int type)
2839 return vfs_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
2840 EXT3_SB(sb)->s_jquota_fmt, type);
2844 * Standard function to be called on quota_on
2846 static int ext3_quota_on(struct super_block *sb, int type, int format_id,
2847 char *name, int remount)
2849 int err;
2850 struct path path;
2852 if (!test_opt(sb, QUOTA))
2853 return -EINVAL;
2854 /* When remounting, no checks are needed and in fact, name is NULL */
2855 if (remount)
2856 return vfs_quota_on(sb, type, format_id, name, remount);
2858 err = kern_path(name, LOOKUP_FOLLOW, &path);
2859 if (err)
2860 return err;
2862 /* Quotafile not on the same filesystem? */
2863 if (path.mnt->mnt_sb != sb) {
2864 path_put(&path);
2865 return -EXDEV;
2867 /* Journaling quota? */
2868 if (EXT3_SB(sb)->s_qf_names[type]) {
2869 /* Quotafile not of fs root? */
2870 if (path.dentry->d_parent != sb->s_root)
2871 ext3_msg(sb, KERN_WARNING,
2872 "warning: Quota file not on filesystem root. "
2873 "Journaled quota will not work.");
2877 * When we journal data on quota file, we have to flush journal to see
2878 * all updates to the file when we bypass pagecache...
2880 if (ext3_should_journal_data(path.dentry->d_inode)) {
2882 * We don't need to lock updates but journal_flush() could
2883 * otherwise be livelocked...
2885 journal_lock_updates(EXT3_SB(sb)->s_journal);
2886 err = journal_flush(EXT3_SB(sb)->s_journal);
2887 journal_unlock_updates(EXT3_SB(sb)->s_journal);
2888 if (err) {
2889 path_put(&path);
2890 return err;
2894 err = vfs_quota_on_path(sb, type, format_id, &path);
2895 path_put(&path);
2896 return err;
2899 /* Read data from quotafile - avoid pagecache and such because we cannot afford
2900 * acquiring the locks... As quota files are never truncated and quota code
2901 * itself serializes the operations (and noone else should touch the files)
2902 * we don't have to be afraid of races */
2903 static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
2904 size_t len, loff_t off)
2906 struct inode *inode = sb_dqopt(sb)->files[type];
2907 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2908 int err = 0;
2909 int offset = off & (sb->s_blocksize - 1);
2910 int tocopy;
2911 size_t toread;
2912 struct buffer_head *bh;
2913 loff_t i_size = i_size_read(inode);
2915 if (off > i_size)
2916 return 0;
2917 if (off+len > i_size)
2918 len = i_size-off;
2919 toread = len;
2920 while (toread > 0) {
2921 tocopy = sb->s_blocksize - offset < toread ?
2922 sb->s_blocksize - offset : toread;
2923 bh = ext3_bread(NULL, inode, blk, 0, &err);
2924 if (err)
2925 return err;
2926 if (!bh) /* A hole? */
2927 memset(data, 0, tocopy);
2928 else
2929 memcpy(data, bh->b_data+offset, tocopy);
2930 brelse(bh);
2931 offset = 0;
2932 toread -= tocopy;
2933 data += tocopy;
2934 blk++;
2936 return len;
2939 /* Write to quotafile (we know the transaction is already started and has
2940 * enough credits) */
2941 static ssize_t ext3_quota_write(struct super_block *sb, int type,
2942 const char *data, size_t len, loff_t off)
2944 struct inode *inode = sb_dqopt(sb)->files[type];
2945 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2946 int err = 0;
2947 int offset = off & (sb->s_blocksize - 1);
2948 int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
2949 struct buffer_head *bh;
2950 handle_t *handle = journal_current_handle();
2952 if (!handle) {
2953 ext3_msg(sb, KERN_WARNING,
2954 "warning: quota write (off=%llu, len=%llu)"
2955 " cancelled because transaction is not started.",
2956 (unsigned long long)off, (unsigned long long)len);
2957 return -EIO;
2961 * Since we account only one data block in transaction credits,
2962 * then it is impossible to cross a block boundary.
2964 if (sb->s_blocksize - offset < len) {
2965 ext3_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
2966 " cancelled because not block aligned",
2967 (unsigned long long)off, (unsigned long long)len);
2968 return -EIO;
2970 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2971 bh = ext3_bread(handle, inode, blk, 1, &err);
2972 if (!bh)
2973 goto out;
2974 if (journal_quota) {
2975 err = ext3_journal_get_write_access(handle, bh);
2976 if (err) {
2977 brelse(bh);
2978 goto out;
2981 lock_buffer(bh);
2982 memcpy(bh->b_data+offset, data, len);
2983 flush_dcache_page(bh->b_page);
2984 unlock_buffer(bh);
2985 if (journal_quota)
2986 err = ext3_journal_dirty_metadata(handle, bh);
2987 else {
2988 /* Always do at least ordered writes for quotas */
2989 err = ext3_journal_dirty_data(handle, bh);
2990 mark_buffer_dirty(bh);
2992 brelse(bh);
2993 out:
2994 if (err) {
2995 mutex_unlock(&inode->i_mutex);
2996 return err;
2998 if (inode->i_size < off + len) {
2999 i_size_write(inode, off + len);
3000 EXT3_I(inode)->i_disksize = inode->i_size;
3002 inode->i_version++;
3003 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3004 ext3_mark_inode_dirty(handle, inode);
3005 mutex_unlock(&inode->i_mutex);
3006 return len;
3009 #endif
3011 static int ext3_get_sb(struct file_system_type *fs_type,
3012 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3014 return get_sb_bdev(fs_type, flags, dev_name, data, ext3_fill_super, mnt);
3017 static struct file_system_type ext3_fs_type = {
3018 .owner = THIS_MODULE,
3019 .name = "ext3",
3020 .get_sb = ext3_get_sb,
3021 .kill_sb = kill_block_super,
3022 .fs_flags = FS_REQUIRES_DEV,
3025 static int __init init_ext3_fs(void)
3027 int err = init_ext3_xattr();
3028 if (err)
3029 return err;
3030 err = init_inodecache();
3031 if (err)
3032 goto out1;
3033 err = register_filesystem(&ext3_fs_type);
3034 if (err)
3035 goto out;
3036 return 0;
3037 out:
3038 destroy_inodecache();
3039 out1:
3040 exit_ext3_xattr();
3041 return err;
3044 static void __exit exit_ext3_fs(void)
3046 unregister_filesystem(&ext3_fs_type);
3047 destroy_inodecache();
3048 exit_ext3_xattr();
3051 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3052 MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
3053 MODULE_LICENSE("GPL");
3054 module_init(init_ext3_fs)
3055 module_exit(exit_ext3_fs)