adt74{62, 70, 73}: Use DIV_ROUND_CLOSEST for rounded division
[linux-2.6/mini2440.git] / fs / ext4 / super.c
blob9494bb2493901b353ad2f3be6f93535600896624
1 /*
2 * linux/fs/ext4/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/jbd2.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/blkdev.h>
27 #include <linux/parser.h>
28 #include <linux/smp_lock.h>
29 #include <linux/buffer_head.h>
30 #include <linux/exportfs.h>
31 #include <linux/vfs.h>
32 #include <linux/random.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/quotaops.h>
36 #include <linux/seq_file.h>
37 #include <linux/proc_fs.h>
38 #include <linux/marker.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <asm/uaccess.h>
43 #include "ext4.h"
44 #include "ext4_jbd2.h"
45 #include "xattr.h"
46 #include "acl.h"
47 #include "namei.h"
48 #include "group.h"
50 struct proc_dir_entry *ext4_proc_root;
52 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
53 unsigned long journal_devnum);
54 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
55 unsigned int);
56 static void ext4_commit_super(struct super_block *sb,
57 struct ext4_super_block *es, int sync);
58 static void ext4_mark_recovery_complete(struct super_block *sb,
59 struct ext4_super_block *es);
60 static void ext4_clear_journal_err(struct super_block *sb,
61 struct ext4_super_block *es);
62 static int ext4_sync_fs(struct super_block *sb, int wait);
63 static const char *ext4_decode_error(struct super_block *sb, int errno,
64 char nbuf[16]);
65 static int ext4_remount(struct super_block *sb, int *flags, char *data);
66 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
67 static void ext4_unlockfs(struct super_block *sb);
68 static void ext4_write_super(struct super_block *sb);
69 static void ext4_write_super_lockfs(struct super_block *sb);
72 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73 struct ext4_group_desc *bg)
75 return le32_to_cpu(bg->bg_block_bitmap_lo) |
76 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
77 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
80 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81 struct ext4_group_desc *bg)
83 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
84 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
85 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
88 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89 struct ext4_group_desc *bg)
91 return le32_to_cpu(bg->bg_inode_table_lo) |
92 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
93 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
96 void ext4_block_bitmap_set(struct super_block *sb,
97 struct ext4_group_desc *bg, ext4_fsblk_t blk)
99 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
100 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
104 void ext4_inode_bitmap_set(struct super_block *sb,
105 struct ext4_group_desc *bg, ext4_fsblk_t blk)
107 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
108 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
112 void ext4_inode_table_set(struct super_block *sb,
113 struct ext4_group_desc *bg, ext4_fsblk_t blk)
115 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
116 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
121 * Wrappers for jbd2_journal_start/end.
123 * The only special thing we need to do here is to make sure that all
124 * journal_end calls result in the superblock being marked dirty, so
125 * that sync() will call the filesystem's write_super callback if
126 * appropriate.
128 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
130 journal_t *journal;
132 if (sb->s_flags & MS_RDONLY)
133 return ERR_PTR(-EROFS);
135 /* Special case here: if the journal has aborted behind our
136 * backs (eg. EIO in the commit thread), then we still need to
137 * take the FS itself readonly cleanly. */
138 journal = EXT4_SB(sb)->s_journal;
139 if (is_journal_aborted(journal)) {
140 ext4_abort(sb, __func__,
141 "Detected aborted journal");
142 return ERR_PTR(-EROFS);
145 return jbd2_journal_start(journal, nblocks);
149 * The only special thing we need to do here is to make sure that all
150 * jbd2_journal_stop calls result in the superblock being marked dirty, so
151 * that sync() will call the filesystem's write_super callback if
152 * appropriate.
154 int __ext4_journal_stop(const char *where, handle_t *handle)
156 struct super_block *sb;
157 int err;
158 int rc;
160 sb = handle->h_transaction->t_journal->j_private;
161 err = handle->h_err;
162 rc = jbd2_journal_stop(handle);
164 if (!err)
165 err = rc;
166 if (err)
167 __ext4_std_error(sb, where, err);
168 return err;
171 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
172 struct buffer_head *bh, handle_t *handle, int err)
174 char nbuf[16];
175 const char *errstr = ext4_decode_error(NULL, err, nbuf);
177 if (bh)
178 BUFFER_TRACE(bh, "abort");
180 if (!handle->h_err)
181 handle->h_err = err;
183 if (is_handle_aborted(handle))
184 return;
186 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
187 caller, errstr, err_fn);
189 jbd2_journal_abort_handle(handle);
192 /* Deal with the reporting of failure conditions on a filesystem such as
193 * inconsistencies detected or read IO failures.
195 * On ext2, we can store the error state of the filesystem in the
196 * superblock. That is not possible on ext4, because we may have other
197 * write ordering constraints on the superblock which prevent us from
198 * writing it out straight away; and given that the journal is about to
199 * be aborted, we can't rely on the current, or future, transactions to
200 * write out the superblock safely.
202 * We'll just use the jbd2_journal_abort() error code to record an error in
203 * the journal instead. On recovery, the journal will compain about
204 * that error until we've noted it down and cleared it.
207 static void ext4_handle_error(struct super_block *sb)
209 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
211 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
212 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
214 if (sb->s_flags & MS_RDONLY)
215 return;
217 if (!test_opt(sb, ERRORS_CONT)) {
218 journal_t *journal = EXT4_SB(sb)->s_journal;
220 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
221 if (journal)
222 jbd2_journal_abort(journal, -EIO);
224 if (test_opt(sb, ERRORS_RO)) {
225 printk(KERN_CRIT "Remounting filesystem read-only\n");
226 sb->s_flags |= MS_RDONLY;
228 ext4_commit_super(sb, es, 1);
229 if (test_opt(sb, ERRORS_PANIC))
230 panic("EXT4-fs (device %s): panic forced after error\n",
231 sb->s_id);
234 void ext4_error(struct super_block *sb, const char *function,
235 const char *fmt, ...)
237 va_list args;
239 va_start(args, fmt);
240 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
241 vprintk(fmt, args);
242 printk("\n");
243 va_end(args);
245 ext4_handle_error(sb);
248 static const char *ext4_decode_error(struct super_block *sb, int errno,
249 char nbuf[16])
251 char *errstr = NULL;
253 switch (errno) {
254 case -EIO:
255 errstr = "IO failure";
256 break;
257 case -ENOMEM:
258 errstr = "Out of memory";
259 break;
260 case -EROFS:
261 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
262 errstr = "Journal has aborted";
263 else
264 errstr = "Readonly filesystem";
265 break;
266 default:
267 /* If the caller passed in an extra buffer for unknown
268 * errors, textualise them now. Else we just return
269 * NULL. */
270 if (nbuf) {
271 /* Check for truncated error codes... */
272 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
273 errstr = nbuf;
275 break;
278 return errstr;
281 /* __ext4_std_error decodes expected errors from journaling functions
282 * automatically and invokes the appropriate error response. */
284 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
286 char nbuf[16];
287 const char *errstr;
289 /* Special case: if the error is EROFS, and we're not already
290 * inside a transaction, then there's really no point in logging
291 * an error. */
292 if (errno == -EROFS && journal_current_handle() == NULL &&
293 (sb->s_flags & MS_RDONLY))
294 return;
296 errstr = ext4_decode_error(sb, errno, nbuf);
297 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
298 sb->s_id, function, errstr);
300 ext4_handle_error(sb);
304 * ext4_abort is a much stronger failure handler than ext4_error. The
305 * abort function may be used to deal with unrecoverable failures such
306 * as journal IO errors or ENOMEM at a critical moment in log management.
308 * We unconditionally force the filesystem into an ABORT|READONLY state,
309 * unless the error response on the fs has been set to panic in which
310 * case we take the easy way out and panic immediately.
313 void ext4_abort(struct super_block *sb, const char *function,
314 const char *fmt, ...)
316 va_list args;
318 printk(KERN_CRIT "ext4_abort called.\n");
320 va_start(args, fmt);
321 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
322 vprintk(fmt, args);
323 printk("\n");
324 va_end(args);
326 if (test_opt(sb, ERRORS_PANIC))
327 panic("EXT4-fs panic from previous error\n");
329 if (sb->s_flags & MS_RDONLY)
330 return;
332 printk(KERN_CRIT "Remounting filesystem read-only\n");
333 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
334 sb->s_flags |= MS_RDONLY;
335 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
336 if (EXT4_SB(sb)->s_journal)
337 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
340 void ext4_warning(struct super_block *sb, const char *function,
341 const char *fmt, ...)
343 va_list args;
345 va_start(args, fmt);
346 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
347 sb->s_id, function);
348 vprintk(fmt, args);
349 printk("\n");
350 va_end(args);
353 void ext4_update_dynamic_rev(struct super_block *sb)
355 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
357 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
358 return;
360 ext4_warning(sb, __func__,
361 "updating to rev %d because of new feature flag, "
362 "running e2fsck is recommended",
363 EXT4_DYNAMIC_REV);
365 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
366 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
367 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
368 /* leave es->s_feature_*compat flags alone */
369 /* es->s_uuid will be set by e2fsck if empty */
372 * The rest of the superblock fields should be zero, and if not it
373 * means they are likely already in use, so leave them alone. We
374 * can leave it up to e2fsck to clean up any inconsistencies there.
379 * Open the external journal device
381 static struct block_device *ext4_blkdev_get(dev_t dev)
383 struct block_device *bdev;
384 char b[BDEVNAME_SIZE];
386 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
387 if (IS_ERR(bdev))
388 goto fail;
389 return bdev;
391 fail:
392 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
393 __bdevname(dev, b), PTR_ERR(bdev));
394 return NULL;
398 * Release the journal device
400 static int ext4_blkdev_put(struct block_device *bdev)
402 bd_release(bdev);
403 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
406 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
408 struct block_device *bdev;
409 int ret = -ENODEV;
411 bdev = sbi->journal_bdev;
412 if (bdev) {
413 ret = ext4_blkdev_put(bdev);
414 sbi->journal_bdev = NULL;
416 return ret;
419 static inline struct inode *orphan_list_entry(struct list_head *l)
421 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
424 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
426 struct list_head *l;
428 printk(KERN_ERR "sb orphan head is %d\n",
429 le32_to_cpu(sbi->s_es->s_last_orphan));
431 printk(KERN_ERR "sb_info orphan list:\n");
432 list_for_each(l, &sbi->s_orphan) {
433 struct inode *inode = orphan_list_entry(l);
434 printk(KERN_ERR " "
435 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
436 inode->i_sb->s_id, inode->i_ino, inode,
437 inode->i_mode, inode->i_nlink,
438 NEXT_ORPHAN(inode));
442 static void ext4_put_super(struct super_block *sb)
444 struct ext4_sb_info *sbi = EXT4_SB(sb);
445 struct ext4_super_block *es = sbi->s_es;
446 int i, err;
448 ext4_mb_release(sb);
449 ext4_ext_release(sb);
450 ext4_xattr_put_super(sb);
451 err = jbd2_journal_destroy(sbi->s_journal);
452 sbi->s_journal = NULL;
453 if (err < 0)
454 ext4_abort(sb, __func__, "Couldn't clean up the journal");
456 if (!(sb->s_flags & MS_RDONLY)) {
457 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
458 es->s_state = cpu_to_le16(sbi->s_mount_state);
459 ext4_commit_super(sb, es, 1);
461 if (sbi->s_proc) {
462 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
463 remove_proc_entry(sb->s_id, ext4_proc_root);
466 for (i = 0; i < sbi->s_gdb_count; i++)
467 brelse(sbi->s_group_desc[i]);
468 kfree(sbi->s_group_desc);
469 kfree(sbi->s_flex_groups);
470 percpu_counter_destroy(&sbi->s_freeblocks_counter);
471 percpu_counter_destroy(&sbi->s_freeinodes_counter);
472 percpu_counter_destroy(&sbi->s_dirs_counter);
473 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
474 brelse(sbi->s_sbh);
475 #ifdef CONFIG_QUOTA
476 for (i = 0; i < MAXQUOTAS; i++)
477 kfree(sbi->s_qf_names[i]);
478 #endif
480 /* Debugging code just in case the in-memory inode orphan list
481 * isn't empty. The on-disk one can be non-empty if we've
482 * detected an error and taken the fs readonly, but the
483 * in-memory list had better be clean by this point. */
484 if (!list_empty(&sbi->s_orphan))
485 dump_orphan_list(sb, sbi);
486 J_ASSERT(list_empty(&sbi->s_orphan));
488 invalidate_bdev(sb->s_bdev);
489 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
491 * Invalidate the journal device's buffers. We don't want them
492 * floating about in memory - the physical journal device may
493 * hotswapped, and it breaks the `ro-after' testing code.
495 sync_blockdev(sbi->journal_bdev);
496 invalidate_bdev(sbi->journal_bdev);
497 ext4_blkdev_remove(sbi);
499 sb->s_fs_info = NULL;
500 kfree(sbi);
501 return;
504 static struct kmem_cache *ext4_inode_cachep;
507 * Called inside transaction, so use GFP_NOFS
509 static struct inode *ext4_alloc_inode(struct super_block *sb)
511 struct ext4_inode_info *ei;
513 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
514 if (!ei)
515 return NULL;
516 #ifdef CONFIG_EXT4_FS_POSIX_ACL
517 ei->i_acl = EXT4_ACL_NOT_CACHED;
518 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
519 #endif
520 ei->vfs_inode.i_version = 1;
521 ei->vfs_inode.i_data.writeback_index = 0;
522 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
523 INIT_LIST_HEAD(&ei->i_prealloc_list);
524 spin_lock_init(&ei->i_prealloc_lock);
525 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
526 ei->i_reserved_data_blocks = 0;
527 ei->i_reserved_meta_blocks = 0;
528 ei->i_allocated_meta_blocks = 0;
529 ei->i_delalloc_reserved_flag = 0;
530 spin_lock_init(&(ei->i_block_reservation_lock));
531 return &ei->vfs_inode;
534 static void ext4_destroy_inode(struct inode *inode)
536 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
537 printk("EXT4 Inode %p: orphan list check failed!\n",
538 EXT4_I(inode));
539 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
540 EXT4_I(inode), sizeof(struct ext4_inode_info),
541 true);
542 dump_stack();
544 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
547 static void init_once(void *foo)
549 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
551 INIT_LIST_HEAD(&ei->i_orphan);
552 #ifdef CONFIG_EXT4_FS_XATTR
553 init_rwsem(&ei->xattr_sem);
554 #endif
555 init_rwsem(&ei->i_data_sem);
556 inode_init_once(&ei->vfs_inode);
559 static int init_inodecache(void)
561 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
562 sizeof(struct ext4_inode_info),
563 0, (SLAB_RECLAIM_ACCOUNT|
564 SLAB_MEM_SPREAD),
565 init_once);
566 if (ext4_inode_cachep == NULL)
567 return -ENOMEM;
568 return 0;
571 static void destroy_inodecache(void)
573 kmem_cache_destroy(ext4_inode_cachep);
576 static void ext4_clear_inode(struct inode *inode)
578 #ifdef CONFIG_EXT4_FS_POSIX_ACL
579 if (EXT4_I(inode)->i_acl &&
580 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
581 posix_acl_release(EXT4_I(inode)->i_acl);
582 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
584 if (EXT4_I(inode)->i_default_acl &&
585 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
586 posix_acl_release(EXT4_I(inode)->i_default_acl);
587 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
589 #endif
590 ext4_discard_preallocations(inode);
591 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
592 &EXT4_I(inode)->jinode);
595 static inline void ext4_show_quota_options(struct seq_file *seq,
596 struct super_block *sb)
598 #if defined(CONFIG_QUOTA)
599 struct ext4_sb_info *sbi = EXT4_SB(sb);
601 if (sbi->s_jquota_fmt)
602 seq_printf(seq, ",jqfmt=%s",
603 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
605 if (sbi->s_qf_names[USRQUOTA])
606 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
608 if (sbi->s_qf_names[GRPQUOTA])
609 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
611 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
612 seq_puts(seq, ",usrquota");
614 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
615 seq_puts(seq, ",grpquota");
616 #endif
620 * Show an option if
621 * - it's set to a non-default value OR
622 * - if the per-sb default is different from the global default
624 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
626 int def_errors;
627 unsigned long def_mount_opts;
628 struct super_block *sb = vfs->mnt_sb;
629 struct ext4_sb_info *sbi = EXT4_SB(sb);
630 struct ext4_super_block *es = sbi->s_es;
632 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
633 def_errors = le16_to_cpu(es->s_errors);
635 if (sbi->s_sb_block != 1)
636 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
637 if (test_opt(sb, MINIX_DF))
638 seq_puts(seq, ",minixdf");
639 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
640 seq_puts(seq, ",grpid");
641 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
642 seq_puts(seq, ",nogrpid");
643 if (sbi->s_resuid != EXT4_DEF_RESUID ||
644 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
645 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
647 if (sbi->s_resgid != EXT4_DEF_RESGID ||
648 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
649 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
651 if (test_opt(sb, ERRORS_RO)) {
652 if (def_errors == EXT4_ERRORS_PANIC ||
653 def_errors == EXT4_ERRORS_CONTINUE) {
654 seq_puts(seq, ",errors=remount-ro");
657 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
658 seq_puts(seq, ",errors=continue");
659 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
660 seq_puts(seq, ",errors=panic");
661 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
662 seq_puts(seq, ",nouid32");
663 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
664 seq_puts(seq, ",debug");
665 if (test_opt(sb, OLDALLOC))
666 seq_puts(seq, ",oldalloc");
667 #ifdef CONFIG_EXT4_FS_XATTR
668 if (test_opt(sb, XATTR_USER) &&
669 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
670 seq_puts(seq, ",user_xattr");
671 if (!test_opt(sb, XATTR_USER) &&
672 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
673 seq_puts(seq, ",nouser_xattr");
675 #endif
676 #ifdef CONFIG_EXT4_FS_POSIX_ACL
677 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
678 seq_puts(seq, ",acl");
679 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
680 seq_puts(seq, ",noacl");
681 #endif
682 if (!test_opt(sb, RESERVATION))
683 seq_puts(seq, ",noreservation");
684 if (sbi->s_commit_interval) {
685 seq_printf(seq, ",commit=%u",
686 (unsigned) (sbi->s_commit_interval / HZ));
689 * We're changing the default of barrier mount option, so
690 * let's always display its mount state so it's clear what its
691 * status is.
693 seq_puts(seq, ",barrier=");
694 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
695 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
696 seq_puts(seq, ",journal_async_commit");
697 if (test_opt(sb, NOBH))
698 seq_puts(seq, ",nobh");
699 if (!test_opt(sb, EXTENTS))
700 seq_puts(seq, ",noextents");
701 if (test_opt(sb, I_VERSION))
702 seq_puts(seq, ",i_version");
703 if (!test_opt(sb, DELALLOC))
704 seq_puts(seq, ",nodelalloc");
707 if (sbi->s_stripe)
708 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
710 * journal mode get enabled in different ways
711 * So just print the value even if we didn't specify it
713 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
714 seq_puts(seq, ",data=journal");
715 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
716 seq_puts(seq, ",data=ordered");
717 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
718 seq_puts(seq, ",data=writeback");
720 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
721 seq_printf(seq, ",inode_readahead_blks=%u",
722 sbi->s_inode_readahead_blks);
724 if (test_opt(sb, DATA_ERR_ABORT))
725 seq_puts(seq, ",data_err=abort");
727 ext4_show_quota_options(seq, sb);
728 return 0;
732 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
733 u64 ino, u32 generation)
735 struct inode *inode;
737 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
738 return ERR_PTR(-ESTALE);
739 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
740 return ERR_PTR(-ESTALE);
742 /* iget isn't really right if the inode is currently unallocated!!
744 * ext4_read_inode will return a bad_inode if the inode had been
745 * deleted, so we should be safe.
747 * Currently we don't know the generation for parent directory, so
748 * a generation of 0 means "accept any"
750 inode = ext4_iget(sb, ino);
751 if (IS_ERR(inode))
752 return ERR_CAST(inode);
753 if (generation && inode->i_generation != generation) {
754 iput(inode);
755 return ERR_PTR(-ESTALE);
758 return inode;
761 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
762 int fh_len, int fh_type)
764 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
765 ext4_nfs_get_inode);
768 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
769 int fh_len, int fh_type)
771 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
772 ext4_nfs_get_inode);
775 #ifdef CONFIG_QUOTA
776 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
777 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
779 static int ext4_dquot_initialize(struct inode *inode, int type);
780 static int ext4_dquot_drop(struct inode *inode);
781 static int ext4_write_dquot(struct dquot *dquot);
782 static int ext4_acquire_dquot(struct dquot *dquot);
783 static int ext4_release_dquot(struct dquot *dquot);
784 static int ext4_mark_dquot_dirty(struct dquot *dquot);
785 static int ext4_write_info(struct super_block *sb, int type);
786 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
787 char *path, int remount);
788 static int ext4_quota_on_mount(struct super_block *sb, int type);
789 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
790 size_t len, loff_t off);
791 static ssize_t ext4_quota_write(struct super_block *sb, int type,
792 const char *data, size_t len, loff_t off);
794 static struct dquot_operations ext4_quota_operations = {
795 .initialize = ext4_dquot_initialize,
796 .drop = ext4_dquot_drop,
797 .alloc_space = dquot_alloc_space,
798 .alloc_inode = dquot_alloc_inode,
799 .free_space = dquot_free_space,
800 .free_inode = dquot_free_inode,
801 .transfer = dquot_transfer,
802 .write_dquot = ext4_write_dquot,
803 .acquire_dquot = ext4_acquire_dquot,
804 .release_dquot = ext4_release_dquot,
805 .mark_dirty = ext4_mark_dquot_dirty,
806 .write_info = ext4_write_info,
807 .alloc_dquot = dquot_alloc,
808 .destroy_dquot = dquot_destroy,
811 static struct quotactl_ops ext4_qctl_operations = {
812 .quota_on = ext4_quota_on,
813 .quota_off = vfs_quota_off,
814 .quota_sync = vfs_quota_sync,
815 .get_info = vfs_get_dqinfo,
816 .set_info = vfs_set_dqinfo,
817 .get_dqblk = vfs_get_dqblk,
818 .set_dqblk = vfs_set_dqblk
820 #endif
822 static const struct super_operations ext4_sops = {
823 .alloc_inode = ext4_alloc_inode,
824 .destroy_inode = ext4_destroy_inode,
825 .write_inode = ext4_write_inode,
826 .dirty_inode = ext4_dirty_inode,
827 .delete_inode = ext4_delete_inode,
828 .put_super = ext4_put_super,
829 .write_super = ext4_write_super,
830 .sync_fs = ext4_sync_fs,
831 .write_super_lockfs = ext4_write_super_lockfs,
832 .unlockfs = ext4_unlockfs,
833 .statfs = ext4_statfs,
834 .remount_fs = ext4_remount,
835 .clear_inode = ext4_clear_inode,
836 .show_options = ext4_show_options,
837 #ifdef CONFIG_QUOTA
838 .quota_read = ext4_quota_read,
839 .quota_write = ext4_quota_write,
840 #endif
843 static const struct export_operations ext4_export_ops = {
844 .fh_to_dentry = ext4_fh_to_dentry,
845 .fh_to_parent = ext4_fh_to_parent,
846 .get_parent = ext4_get_parent,
849 enum {
850 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
851 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
852 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
853 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
854 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
855 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
856 Opt_journal_checksum, Opt_journal_async_commit,
857 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
858 Opt_data_err_abort, Opt_data_err_ignore,
859 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
860 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
861 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
862 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
863 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
864 Opt_inode_readahead_blks
867 static const match_table_t tokens = {
868 {Opt_bsd_df, "bsddf"},
869 {Opt_minix_df, "minixdf"},
870 {Opt_grpid, "grpid"},
871 {Opt_grpid, "bsdgroups"},
872 {Opt_nogrpid, "nogrpid"},
873 {Opt_nogrpid, "sysvgroups"},
874 {Opt_resgid, "resgid=%u"},
875 {Opt_resuid, "resuid=%u"},
876 {Opt_sb, "sb=%u"},
877 {Opt_err_cont, "errors=continue"},
878 {Opt_err_panic, "errors=panic"},
879 {Opt_err_ro, "errors=remount-ro"},
880 {Opt_nouid32, "nouid32"},
881 {Opt_debug, "debug"},
882 {Opt_oldalloc, "oldalloc"},
883 {Opt_orlov, "orlov"},
884 {Opt_user_xattr, "user_xattr"},
885 {Opt_nouser_xattr, "nouser_xattr"},
886 {Opt_acl, "acl"},
887 {Opt_noacl, "noacl"},
888 {Opt_reservation, "reservation"},
889 {Opt_noreservation, "noreservation"},
890 {Opt_noload, "noload"},
891 {Opt_nobh, "nobh"},
892 {Opt_bh, "bh"},
893 {Opt_commit, "commit=%u"},
894 {Opt_journal_update, "journal=update"},
895 {Opt_journal_inum, "journal=%u"},
896 {Opt_journal_dev, "journal_dev=%u"},
897 {Opt_journal_checksum, "journal_checksum"},
898 {Opt_journal_async_commit, "journal_async_commit"},
899 {Opt_abort, "abort"},
900 {Opt_data_journal, "data=journal"},
901 {Opt_data_ordered, "data=ordered"},
902 {Opt_data_writeback, "data=writeback"},
903 {Opt_data_err_abort, "data_err=abort"},
904 {Opt_data_err_ignore, "data_err=ignore"},
905 {Opt_offusrjquota, "usrjquota="},
906 {Opt_usrjquota, "usrjquota=%s"},
907 {Opt_offgrpjquota, "grpjquota="},
908 {Opt_grpjquota, "grpjquota=%s"},
909 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
910 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
911 {Opt_grpquota, "grpquota"},
912 {Opt_noquota, "noquota"},
913 {Opt_quota, "quota"},
914 {Opt_usrquota, "usrquota"},
915 {Opt_barrier, "barrier=%u"},
916 {Opt_extents, "extents"},
917 {Opt_noextents, "noextents"},
918 {Opt_i_version, "i_version"},
919 {Opt_stripe, "stripe=%u"},
920 {Opt_resize, "resize"},
921 {Opt_delalloc, "delalloc"},
922 {Opt_nodelalloc, "nodelalloc"},
923 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
924 {Opt_err, NULL},
927 static ext4_fsblk_t get_sb_block(void **data)
929 ext4_fsblk_t sb_block;
930 char *options = (char *) *data;
932 if (!options || strncmp(options, "sb=", 3) != 0)
933 return 1; /* Default location */
934 options += 3;
935 /*todo: use simple_strtoll with >32bit ext4 */
936 sb_block = simple_strtoul(options, &options, 0);
937 if (*options && *options != ',') {
938 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
939 (char *) *data);
940 return 1;
942 if (*options == ',')
943 options++;
944 *data = (void *) options;
945 return sb_block;
948 static int parse_options(char *options, struct super_block *sb,
949 unsigned int *inum, unsigned long *journal_devnum,
950 ext4_fsblk_t *n_blocks_count, int is_remount)
952 struct ext4_sb_info *sbi = EXT4_SB(sb);
953 char *p;
954 substring_t args[MAX_OPT_ARGS];
955 int data_opt = 0;
956 int option;
957 #ifdef CONFIG_QUOTA
958 int qtype, qfmt;
959 char *qname;
960 #endif
961 ext4_fsblk_t last_block;
963 if (!options)
964 return 1;
966 while ((p = strsep(&options, ",")) != NULL) {
967 int token;
968 if (!*p)
969 continue;
971 token = match_token(p, tokens, args);
972 switch (token) {
973 case Opt_bsd_df:
974 clear_opt(sbi->s_mount_opt, MINIX_DF);
975 break;
976 case Opt_minix_df:
977 set_opt(sbi->s_mount_opt, MINIX_DF);
978 break;
979 case Opt_grpid:
980 set_opt(sbi->s_mount_opt, GRPID);
981 break;
982 case Opt_nogrpid:
983 clear_opt(sbi->s_mount_opt, GRPID);
984 break;
985 case Opt_resuid:
986 if (match_int(&args[0], &option))
987 return 0;
988 sbi->s_resuid = option;
989 break;
990 case Opt_resgid:
991 if (match_int(&args[0], &option))
992 return 0;
993 sbi->s_resgid = option;
994 break;
995 case Opt_sb:
996 /* handled by get_sb_block() instead of here */
997 /* *sb_block = match_int(&args[0]); */
998 break;
999 case Opt_err_panic:
1000 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1001 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1002 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1003 break;
1004 case Opt_err_ro:
1005 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1006 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1007 set_opt(sbi->s_mount_opt, ERRORS_RO);
1008 break;
1009 case Opt_err_cont:
1010 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1011 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1012 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1013 break;
1014 case Opt_nouid32:
1015 set_opt(sbi->s_mount_opt, NO_UID32);
1016 break;
1017 case Opt_debug:
1018 set_opt(sbi->s_mount_opt, DEBUG);
1019 break;
1020 case Opt_oldalloc:
1021 set_opt(sbi->s_mount_opt, OLDALLOC);
1022 break;
1023 case Opt_orlov:
1024 clear_opt(sbi->s_mount_opt, OLDALLOC);
1025 break;
1026 #ifdef CONFIG_EXT4_FS_XATTR
1027 case Opt_user_xattr:
1028 set_opt(sbi->s_mount_opt, XATTR_USER);
1029 break;
1030 case Opt_nouser_xattr:
1031 clear_opt(sbi->s_mount_opt, XATTR_USER);
1032 break;
1033 #else
1034 case Opt_user_xattr:
1035 case Opt_nouser_xattr:
1036 printk(KERN_ERR "EXT4 (no)user_xattr options "
1037 "not supported\n");
1038 break;
1039 #endif
1040 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1041 case Opt_acl:
1042 set_opt(sbi->s_mount_opt, POSIX_ACL);
1043 break;
1044 case Opt_noacl:
1045 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1046 break;
1047 #else
1048 case Opt_acl:
1049 case Opt_noacl:
1050 printk(KERN_ERR "EXT4 (no)acl options "
1051 "not supported\n");
1052 break;
1053 #endif
1054 case Opt_reservation:
1055 set_opt(sbi->s_mount_opt, RESERVATION);
1056 break;
1057 case Opt_noreservation:
1058 clear_opt(sbi->s_mount_opt, RESERVATION);
1059 break;
1060 case Opt_journal_update:
1061 /* @@@ FIXME */
1062 /* Eventually we will want to be able to create
1063 a journal file here. For now, only allow the
1064 user to specify an existing inode to be the
1065 journal file. */
1066 if (is_remount) {
1067 printk(KERN_ERR "EXT4-fs: cannot specify "
1068 "journal on remount\n");
1069 return 0;
1071 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1072 break;
1073 case Opt_journal_inum:
1074 if (is_remount) {
1075 printk(KERN_ERR "EXT4-fs: cannot specify "
1076 "journal on remount\n");
1077 return 0;
1079 if (match_int(&args[0], &option))
1080 return 0;
1081 *inum = option;
1082 break;
1083 case Opt_journal_dev:
1084 if (is_remount) {
1085 printk(KERN_ERR "EXT4-fs: cannot specify "
1086 "journal on remount\n");
1087 return 0;
1089 if (match_int(&args[0], &option))
1090 return 0;
1091 *journal_devnum = option;
1092 break;
1093 case Opt_journal_checksum:
1094 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1095 break;
1096 case Opt_journal_async_commit:
1097 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1098 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1099 break;
1100 case Opt_noload:
1101 set_opt(sbi->s_mount_opt, NOLOAD);
1102 break;
1103 case Opt_commit:
1104 if (match_int(&args[0], &option))
1105 return 0;
1106 if (option < 0)
1107 return 0;
1108 if (option == 0)
1109 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1110 sbi->s_commit_interval = HZ * option;
1111 break;
1112 case Opt_data_journal:
1113 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1114 goto datacheck;
1115 case Opt_data_ordered:
1116 data_opt = EXT4_MOUNT_ORDERED_DATA;
1117 goto datacheck;
1118 case Opt_data_writeback:
1119 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1120 datacheck:
1121 if (is_remount) {
1122 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1123 != data_opt) {
1124 printk(KERN_ERR
1125 "EXT4-fs: cannot change data "
1126 "mode on remount\n");
1127 return 0;
1129 } else {
1130 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1131 sbi->s_mount_opt |= data_opt;
1133 break;
1134 case Opt_data_err_abort:
1135 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1136 break;
1137 case Opt_data_err_ignore:
1138 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1139 break;
1140 #ifdef CONFIG_QUOTA
1141 case Opt_usrjquota:
1142 qtype = USRQUOTA;
1143 goto set_qf_name;
1144 case Opt_grpjquota:
1145 qtype = GRPQUOTA;
1146 set_qf_name:
1147 if (sb_any_quota_loaded(sb) &&
1148 !sbi->s_qf_names[qtype]) {
1149 printk(KERN_ERR
1150 "EXT4-fs: Cannot change journaled "
1151 "quota options when quota turned on.\n");
1152 return 0;
1154 qname = match_strdup(&args[0]);
1155 if (!qname) {
1156 printk(KERN_ERR
1157 "EXT4-fs: not enough memory for "
1158 "storing quotafile name.\n");
1159 return 0;
1161 if (sbi->s_qf_names[qtype] &&
1162 strcmp(sbi->s_qf_names[qtype], qname)) {
1163 printk(KERN_ERR
1164 "EXT4-fs: %s quota file already "
1165 "specified.\n", QTYPE2NAME(qtype));
1166 kfree(qname);
1167 return 0;
1169 sbi->s_qf_names[qtype] = qname;
1170 if (strchr(sbi->s_qf_names[qtype], '/')) {
1171 printk(KERN_ERR
1172 "EXT4-fs: quotafile must be on "
1173 "filesystem root.\n");
1174 kfree(sbi->s_qf_names[qtype]);
1175 sbi->s_qf_names[qtype] = NULL;
1176 return 0;
1178 set_opt(sbi->s_mount_opt, QUOTA);
1179 break;
1180 case Opt_offusrjquota:
1181 qtype = USRQUOTA;
1182 goto clear_qf_name;
1183 case Opt_offgrpjquota:
1184 qtype = GRPQUOTA;
1185 clear_qf_name:
1186 if (sb_any_quota_loaded(sb) &&
1187 sbi->s_qf_names[qtype]) {
1188 printk(KERN_ERR "EXT4-fs: Cannot change "
1189 "journaled quota options when "
1190 "quota turned on.\n");
1191 return 0;
1194 * The space will be released later when all options
1195 * are confirmed to be correct
1197 sbi->s_qf_names[qtype] = NULL;
1198 break;
1199 case Opt_jqfmt_vfsold:
1200 qfmt = QFMT_VFS_OLD;
1201 goto set_qf_format;
1202 case Opt_jqfmt_vfsv0:
1203 qfmt = QFMT_VFS_V0;
1204 set_qf_format:
1205 if (sb_any_quota_loaded(sb) &&
1206 sbi->s_jquota_fmt != qfmt) {
1207 printk(KERN_ERR "EXT4-fs: Cannot change "
1208 "journaled quota options when "
1209 "quota turned on.\n");
1210 return 0;
1212 sbi->s_jquota_fmt = qfmt;
1213 break;
1214 case Opt_quota:
1215 case Opt_usrquota:
1216 set_opt(sbi->s_mount_opt, QUOTA);
1217 set_opt(sbi->s_mount_opt, USRQUOTA);
1218 break;
1219 case Opt_grpquota:
1220 set_opt(sbi->s_mount_opt, QUOTA);
1221 set_opt(sbi->s_mount_opt, GRPQUOTA);
1222 break;
1223 case Opt_noquota:
1224 if (sb_any_quota_loaded(sb)) {
1225 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1226 "options when quota turned on.\n");
1227 return 0;
1229 clear_opt(sbi->s_mount_opt, QUOTA);
1230 clear_opt(sbi->s_mount_opt, USRQUOTA);
1231 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1232 break;
1233 #else
1234 case Opt_quota:
1235 case Opt_usrquota:
1236 case Opt_grpquota:
1237 printk(KERN_ERR
1238 "EXT4-fs: quota options not supported.\n");
1239 break;
1240 case Opt_usrjquota:
1241 case Opt_grpjquota:
1242 case Opt_offusrjquota:
1243 case Opt_offgrpjquota:
1244 case Opt_jqfmt_vfsold:
1245 case Opt_jqfmt_vfsv0:
1246 printk(KERN_ERR
1247 "EXT4-fs: journaled quota options not "
1248 "supported.\n");
1249 break;
1250 case Opt_noquota:
1251 break;
1252 #endif
1253 case Opt_abort:
1254 set_opt(sbi->s_mount_opt, ABORT);
1255 break;
1256 case Opt_barrier:
1257 if (match_int(&args[0], &option))
1258 return 0;
1259 if (option)
1260 set_opt(sbi->s_mount_opt, BARRIER);
1261 else
1262 clear_opt(sbi->s_mount_opt, BARRIER);
1263 break;
1264 case Opt_ignore:
1265 break;
1266 case Opt_resize:
1267 if (!is_remount) {
1268 printk("EXT4-fs: resize option only available "
1269 "for remount\n");
1270 return 0;
1272 if (match_int(&args[0], &option) != 0)
1273 return 0;
1274 *n_blocks_count = option;
1275 break;
1276 case Opt_nobh:
1277 set_opt(sbi->s_mount_opt, NOBH);
1278 break;
1279 case Opt_bh:
1280 clear_opt(sbi->s_mount_opt, NOBH);
1281 break;
1282 case Opt_extents:
1283 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1284 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1285 ext4_warning(sb, __func__,
1286 "extents feature not enabled "
1287 "on this filesystem, use tune2fs\n");
1288 return 0;
1290 set_opt(sbi->s_mount_opt, EXTENTS);
1291 break;
1292 case Opt_noextents:
1294 * When e2fsprogs support resizing an already existing
1295 * ext3 file system to greater than 2**32 we need to
1296 * add support to block allocator to handle growing
1297 * already existing block mapped inode so that blocks
1298 * allocated for them fall within 2**32
1300 last_block = ext4_blocks_count(sbi->s_es) - 1;
1301 if (last_block > 0xffffffffULL) {
1302 printk(KERN_ERR "EXT4-fs: Filesystem too "
1303 "large to mount with "
1304 "-o noextents options\n");
1305 return 0;
1307 clear_opt(sbi->s_mount_opt, EXTENTS);
1308 break;
1309 case Opt_i_version:
1310 set_opt(sbi->s_mount_opt, I_VERSION);
1311 sb->s_flags |= MS_I_VERSION;
1312 break;
1313 case Opt_nodelalloc:
1314 clear_opt(sbi->s_mount_opt, DELALLOC);
1315 break;
1316 case Opt_stripe:
1317 if (match_int(&args[0], &option))
1318 return 0;
1319 if (option < 0)
1320 return 0;
1321 sbi->s_stripe = option;
1322 break;
1323 case Opt_delalloc:
1324 set_opt(sbi->s_mount_opt, DELALLOC);
1325 break;
1326 case Opt_inode_readahead_blks:
1327 if (match_int(&args[0], &option))
1328 return 0;
1329 if (option < 0 || option > (1 << 30))
1330 return 0;
1331 sbi->s_inode_readahead_blks = option;
1332 break;
1333 default:
1334 printk(KERN_ERR
1335 "EXT4-fs: Unrecognized mount option \"%s\" "
1336 "or missing value\n", p);
1337 return 0;
1340 #ifdef CONFIG_QUOTA
1341 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1342 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1343 sbi->s_qf_names[USRQUOTA])
1344 clear_opt(sbi->s_mount_opt, USRQUOTA);
1346 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1347 sbi->s_qf_names[GRPQUOTA])
1348 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1350 if ((sbi->s_qf_names[USRQUOTA] &&
1351 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1352 (sbi->s_qf_names[GRPQUOTA] &&
1353 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1354 printk(KERN_ERR "EXT4-fs: old and new quota "
1355 "format mixing.\n");
1356 return 0;
1359 if (!sbi->s_jquota_fmt) {
1360 printk(KERN_ERR "EXT4-fs: journaled quota format "
1361 "not specified.\n");
1362 return 0;
1364 } else {
1365 if (sbi->s_jquota_fmt) {
1366 printk(KERN_ERR "EXT4-fs: journaled quota format "
1367 "specified with no journaling "
1368 "enabled.\n");
1369 return 0;
1372 #endif
1373 return 1;
1376 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1377 int read_only)
1379 struct ext4_sb_info *sbi = EXT4_SB(sb);
1380 int res = 0;
1382 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1383 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1384 "forcing read-only mode\n");
1385 res = MS_RDONLY;
1387 if (read_only)
1388 return res;
1389 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1390 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1391 "running e2fsck is recommended\n");
1392 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1393 printk(KERN_WARNING
1394 "EXT4-fs warning: mounting fs with errors, "
1395 "running e2fsck is recommended\n");
1396 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1397 le16_to_cpu(es->s_mnt_count) >=
1398 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1399 printk(KERN_WARNING
1400 "EXT4-fs warning: maximal mount count reached, "
1401 "running e2fsck is recommended\n");
1402 else if (le32_to_cpu(es->s_checkinterval) &&
1403 (le32_to_cpu(es->s_lastcheck) +
1404 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1405 printk(KERN_WARNING
1406 "EXT4-fs warning: checktime reached, "
1407 "running e2fsck is recommended\n");
1408 #if 0
1409 /* @@@ We _will_ want to clear the valid bit if we find
1410 * inconsistencies, to force a fsck at reboot. But for
1411 * a plain journaled filesystem we can keep it set as
1412 * valid forever! :)
1414 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1415 #endif
1416 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1417 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1418 le16_add_cpu(&es->s_mnt_count, 1);
1419 es->s_mtime = cpu_to_le32(get_seconds());
1420 ext4_update_dynamic_rev(sb);
1421 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1423 ext4_commit_super(sb, es, 1);
1424 if (test_opt(sb, DEBUG))
1425 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1426 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1427 sb->s_blocksize,
1428 sbi->s_groups_count,
1429 EXT4_BLOCKS_PER_GROUP(sb),
1430 EXT4_INODES_PER_GROUP(sb),
1431 sbi->s_mount_opt);
1433 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1434 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1435 "external", EXT4_SB(sb)->s_journal->j_devname);
1436 return res;
1439 static int ext4_fill_flex_info(struct super_block *sb)
1441 struct ext4_sb_info *sbi = EXT4_SB(sb);
1442 struct ext4_group_desc *gdp = NULL;
1443 struct buffer_head *bh;
1444 ext4_group_t flex_group_count;
1445 ext4_group_t flex_group;
1446 int groups_per_flex = 0;
1447 __u64 block_bitmap = 0;
1448 int i;
1450 if (!sbi->s_es->s_log_groups_per_flex) {
1451 sbi->s_log_groups_per_flex = 0;
1452 return 1;
1455 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1456 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1458 /* We allocate both existing and potentially added groups */
1459 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1460 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1461 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
1462 sbi->s_flex_groups = kzalloc(flex_group_count *
1463 sizeof(struct flex_groups), GFP_KERNEL);
1464 if (sbi->s_flex_groups == NULL) {
1465 printk(KERN_ERR "EXT4-fs: not enough memory for "
1466 "%lu flex groups\n", flex_group_count);
1467 goto failed;
1470 gdp = ext4_get_group_desc(sb, 1, &bh);
1471 block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1473 for (i = 0; i < sbi->s_groups_count; i++) {
1474 gdp = ext4_get_group_desc(sb, i, &bh);
1476 flex_group = ext4_flex_group(sbi, i);
1477 sbi->s_flex_groups[flex_group].free_inodes +=
1478 le16_to_cpu(gdp->bg_free_inodes_count);
1479 sbi->s_flex_groups[flex_group].free_blocks +=
1480 le16_to_cpu(gdp->bg_free_blocks_count);
1483 return 1;
1484 failed:
1485 return 0;
1488 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1489 struct ext4_group_desc *gdp)
1491 __u16 crc = 0;
1493 if (sbi->s_es->s_feature_ro_compat &
1494 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1495 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1496 __le32 le_group = cpu_to_le32(block_group);
1498 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1499 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1500 crc = crc16(crc, (__u8 *)gdp, offset);
1501 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1502 /* for checksum of struct ext4_group_desc do the rest...*/
1503 if ((sbi->s_es->s_feature_incompat &
1504 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1505 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1506 crc = crc16(crc, (__u8 *)gdp + offset,
1507 le16_to_cpu(sbi->s_es->s_desc_size) -
1508 offset);
1511 return cpu_to_le16(crc);
1514 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1515 struct ext4_group_desc *gdp)
1517 if ((sbi->s_es->s_feature_ro_compat &
1518 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1519 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1520 return 0;
1522 return 1;
1525 /* Called at mount-time, super-block is locked */
1526 static int ext4_check_descriptors(struct super_block *sb)
1528 struct ext4_sb_info *sbi = EXT4_SB(sb);
1529 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1530 ext4_fsblk_t last_block;
1531 ext4_fsblk_t block_bitmap;
1532 ext4_fsblk_t inode_bitmap;
1533 ext4_fsblk_t inode_table;
1534 int flexbg_flag = 0;
1535 ext4_group_t i;
1537 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1538 flexbg_flag = 1;
1540 ext4_debug("Checking group descriptors");
1542 for (i = 0; i < sbi->s_groups_count; i++) {
1543 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1545 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1546 last_block = ext4_blocks_count(sbi->s_es) - 1;
1547 else
1548 last_block = first_block +
1549 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1551 block_bitmap = ext4_block_bitmap(sb, gdp);
1552 if (block_bitmap < first_block || block_bitmap > last_block) {
1553 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1554 "Block bitmap for group %lu not in group "
1555 "(block %llu)!\n", i, block_bitmap);
1556 return 0;
1558 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1559 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1560 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1561 "Inode bitmap for group %lu not in group "
1562 "(block %llu)!\n", i, inode_bitmap);
1563 return 0;
1565 inode_table = ext4_inode_table(sb, gdp);
1566 if (inode_table < first_block ||
1567 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1568 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1569 "Inode table for group %lu not in group "
1570 "(block %llu)!\n", i, inode_table);
1571 return 0;
1573 spin_lock(sb_bgl_lock(sbi, i));
1574 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1575 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1576 "Checksum for group %lu failed (%u!=%u)\n",
1577 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1578 gdp)), le16_to_cpu(gdp->bg_checksum));
1579 if (!(sb->s_flags & MS_RDONLY)) {
1580 spin_unlock(sb_bgl_lock(sbi, i));
1581 return 0;
1584 spin_unlock(sb_bgl_lock(sbi, i));
1585 if (!flexbg_flag)
1586 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1589 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1590 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1591 return 1;
1594 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1595 * the superblock) which were deleted from all directories, but held open by
1596 * a process at the time of a crash. We walk the list and try to delete these
1597 * inodes at recovery time (only with a read-write filesystem).
1599 * In order to keep the orphan inode chain consistent during traversal (in
1600 * case of crash during recovery), we link each inode into the superblock
1601 * orphan list_head and handle it the same way as an inode deletion during
1602 * normal operation (which journals the operations for us).
1604 * We only do an iget() and an iput() on each inode, which is very safe if we
1605 * accidentally point at an in-use or already deleted inode. The worst that
1606 * can happen in this case is that we get a "bit already cleared" message from
1607 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1608 * e2fsck was run on this filesystem, and it must have already done the orphan
1609 * inode cleanup for us, so we can safely abort without any further action.
1611 static void ext4_orphan_cleanup(struct super_block *sb,
1612 struct ext4_super_block *es)
1614 unsigned int s_flags = sb->s_flags;
1615 int nr_orphans = 0, nr_truncates = 0;
1616 #ifdef CONFIG_QUOTA
1617 int i;
1618 #endif
1619 if (!es->s_last_orphan) {
1620 jbd_debug(4, "no orphan inodes to clean up\n");
1621 return;
1624 if (bdev_read_only(sb->s_bdev)) {
1625 printk(KERN_ERR "EXT4-fs: write access "
1626 "unavailable, skipping orphan cleanup.\n");
1627 return;
1630 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1631 if (es->s_last_orphan)
1632 jbd_debug(1, "Errors on filesystem, "
1633 "clearing orphan list.\n");
1634 es->s_last_orphan = 0;
1635 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1636 return;
1639 if (s_flags & MS_RDONLY) {
1640 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1641 sb->s_id);
1642 sb->s_flags &= ~MS_RDONLY;
1644 #ifdef CONFIG_QUOTA
1645 /* Needed for iput() to work correctly and not trash data */
1646 sb->s_flags |= MS_ACTIVE;
1647 /* Turn on quotas so that they are updated correctly */
1648 for (i = 0; i < MAXQUOTAS; i++) {
1649 if (EXT4_SB(sb)->s_qf_names[i]) {
1650 int ret = ext4_quota_on_mount(sb, i);
1651 if (ret < 0)
1652 printk(KERN_ERR
1653 "EXT4-fs: Cannot turn on journaled "
1654 "quota: error %d\n", ret);
1657 #endif
1659 while (es->s_last_orphan) {
1660 struct inode *inode;
1662 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1663 if (IS_ERR(inode)) {
1664 es->s_last_orphan = 0;
1665 break;
1668 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1669 DQUOT_INIT(inode);
1670 if (inode->i_nlink) {
1671 printk(KERN_DEBUG
1672 "%s: truncating inode %lu to %lld bytes\n",
1673 __func__, inode->i_ino, inode->i_size);
1674 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
1675 inode->i_ino, inode->i_size);
1676 ext4_truncate(inode);
1677 nr_truncates++;
1678 } else {
1679 printk(KERN_DEBUG
1680 "%s: deleting unreferenced inode %lu\n",
1681 __func__, inode->i_ino);
1682 jbd_debug(2, "deleting unreferenced inode %lu\n",
1683 inode->i_ino);
1684 nr_orphans++;
1686 iput(inode); /* The delete magic happens here! */
1689 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1691 if (nr_orphans)
1692 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1693 sb->s_id, PLURAL(nr_orphans));
1694 if (nr_truncates)
1695 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1696 sb->s_id, PLURAL(nr_truncates));
1697 #ifdef CONFIG_QUOTA
1698 /* Turn quotas off */
1699 for (i = 0; i < MAXQUOTAS; i++) {
1700 if (sb_dqopt(sb)->files[i])
1701 vfs_quota_off(sb, i, 0);
1703 #endif
1704 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1707 * Maximal extent format file size.
1708 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1709 * extent format containers, within a sector_t, and within i_blocks
1710 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1711 * so that won't be a limiting factor.
1713 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1715 static loff_t ext4_max_size(int blkbits, int has_huge_files)
1717 loff_t res;
1718 loff_t upper_limit = MAX_LFS_FILESIZE;
1720 /* small i_blocks in vfs inode? */
1721 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1723 * CONFIG_LBD is not enabled implies the inode
1724 * i_block represent total blocks in 512 bytes
1725 * 32 == size of vfs inode i_blocks * 8
1727 upper_limit = (1LL << 32) - 1;
1729 /* total blocks in file system block size */
1730 upper_limit >>= (blkbits - 9);
1731 upper_limit <<= blkbits;
1734 /* 32-bit extent-start container, ee_block */
1735 res = 1LL << 32;
1736 res <<= blkbits;
1737 res -= 1;
1739 /* Sanity check against vm- & vfs- imposed limits */
1740 if (res > upper_limit)
1741 res = upper_limit;
1743 return res;
1747 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1748 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1749 * We need to be 1 filesystem block less than the 2^48 sector limit.
1751 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
1753 loff_t res = EXT4_NDIR_BLOCKS;
1754 int meta_blocks;
1755 loff_t upper_limit;
1756 /* This is calculated to be the largest file size for a
1757 * dense, bitmapped file such that the total number of
1758 * sectors in the file, including data and all indirect blocks,
1759 * does not exceed 2^48 -1
1760 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1761 * total number of 512 bytes blocks of the file
1764 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
1766 * !has_huge_files or CONFIG_LBD is not enabled
1767 * implies the inode i_block represent total blocks in
1768 * 512 bytes 32 == size of vfs inode i_blocks * 8
1770 upper_limit = (1LL << 32) - 1;
1772 /* total blocks in file system block size */
1773 upper_limit >>= (bits - 9);
1775 } else {
1777 * We use 48 bit ext4_inode i_blocks
1778 * With EXT4_HUGE_FILE_FL set the i_blocks
1779 * represent total number of blocks in
1780 * file system block size
1782 upper_limit = (1LL << 48) - 1;
1786 /* indirect blocks */
1787 meta_blocks = 1;
1788 /* double indirect blocks */
1789 meta_blocks += 1 + (1LL << (bits-2));
1790 /* tripple indirect blocks */
1791 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1793 upper_limit -= meta_blocks;
1794 upper_limit <<= bits;
1796 res += 1LL << (bits-2);
1797 res += 1LL << (2*(bits-2));
1798 res += 1LL << (3*(bits-2));
1799 res <<= bits;
1800 if (res > upper_limit)
1801 res = upper_limit;
1803 if (res > MAX_LFS_FILESIZE)
1804 res = MAX_LFS_FILESIZE;
1806 return res;
1809 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1810 ext4_fsblk_t logical_sb_block, int nr)
1812 struct ext4_sb_info *sbi = EXT4_SB(sb);
1813 ext4_group_t bg, first_meta_bg;
1814 int has_super = 0;
1816 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1818 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1819 nr < first_meta_bg)
1820 return logical_sb_block + nr + 1;
1821 bg = sbi->s_desc_per_block * nr;
1822 if (ext4_bg_has_super(sb, bg))
1823 has_super = 1;
1824 return (has_super + ext4_group_first_block_no(sb, bg));
1828 * ext4_get_stripe_size: Get the stripe size.
1829 * @sbi: In memory super block info
1831 * If we have specified it via mount option, then
1832 * use the mount option value. If the value specified at mount time is
1833 * greater than the blocks per group use the super block value.
1834 * If the super block value is greater than blocks per group return 0.
1835 * Allocator needs it be less than blocks per group.
1838 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1840 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1841 unsigned long stripe_width =
1842 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1844 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1845 return sbi->s_stripe;
1847 if (stripe_width <= sbi->s_blocks_per_group)
1848 return stripe_width;
1850 if (stride <= sbi->s_blocks_per_group)
1851 return stride;
1853 return 0;
1856 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
1857 __releases(kernel_lock)
1858 __acquires(kernel_lock)
1861 struct buffer_head *bh;
1862 struct ext4_super_block *es = NULL;
1863 struct ext4_sb_info *sbi;
1864 ext4_fsblk_t block;
1865 ext4_fsblk_t sb_block = get_sb_block(&data);
1866 ext4_fsblk_t logical_sb_block;
1867 unsigned long offset = 0;
1868 unsigned int journal_inum = 0;
1869 unsigned long journal_devnum = 0;
1870 unsigned long def_mount_opts;
1871 struct inode *root;
1872 char *cp;
1873 int ret = -EINVAL;
1874 int blocksize;
1875 int db_count;
1876 int i;
1877 int needs_recovery, has_huge_files;
1878 __le32 features;
1879 __u64 blocks_count;
1880 int err;
1882 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1883 if (!sbi)
1884 return -ENOMEM;
1885 sb->s_fs_info = sbi;
1886 sbi->s_mount_opt = 0;
1887 sbi->s_resuid = EXT4_DEF_RESUID;
1888 sbi->s_resgid = EXT4_DEF_RESGID;
1889 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
1890 sbi->s_sb_block = sb_block;
1892 unlock_kernel();
1894 /* Cleanup superblock name */
1895 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1896 *cp = '!';
1898 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1899 if (!blocksize) {
1900 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1901 goto out_fail;
1905 * The ext4 superblock will not be buffer aligned for other than 1kB
1906 * block sizes. We need to calculate the offset from buffer start.
1908 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1909 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1910 offset = do_div(logical_sb_block, blocksize);
1911 } else {
1912 logical_sb_block = sb_block;
1915 if (!(bh = sb_bread(sb, logical_sb_block))) {
1916 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
1917 goto out_fail;
1920 * Note: s_es must be initialized as soon as possible because
1921 * some ext4 macro-instructions depend on its value
1923 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1924 sbi->s_es = es;
1925 sb->s_magic = le16_to_cpu(es->s_magic);
1926 if (sb->s_magic != EXT4_SUPER_MAGIC)
1927 goto cantfind_ext4;
1929 /* Set defaults before we parse the mount options */
1930 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1931 if (def_mount_opts & EXT4_DEFM_DEBUG)
1932 set_opt(sbi->s_mount_opt, DEBUG);
1933 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1934 set_opt(sbi->s_mount_opt, GRPID);
1935 if (def_mount_opts & EXT4_DEFM_UID16)
1936 set_opt(sbi->s_mount_opt, NO_UID32);
1937 #ifdef CONFIG_EXT4_FS_XATTR
1938 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1939 set_opt(sbi->s_mount_opt, XATTR_USER);
1940 #endif
1941 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1942 if (def_mount_opts & EXT4_DEFM_ACL)
1943 set_opt(sbi->s_mount_opt, POSIX_ACL);
1944 #endif
1945 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1946 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1947 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1948 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1949 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1950 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1952 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1953 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1954 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
1955 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1956 else
1957 set_opt(sbi->s_mount_opt, ERRORS_RO);
1959 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1960 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1962 set_opt(sbi->s_mount_opt, RESERVATION);
1963 set_opt(sbi->s_mount_opt, BARRIER);
1966 * turn on extents feature by default in ext4 filesystem
1967 * only if feature flag already set by mkfs or tune2fs.
1968 * Use -o noextents to turn it off
1970 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
1971 set_opt(sbi->s_mount_opt, EXTENTS);
1972 else
1973 ext4_warning(sb, __func__,
1974 "extents feature not enabled on this filesystem, "
1975 "use tune2fs.\n");
1978 * enable delayed allocation by default
1979 * Use -o nodelalloc to turn it off
1981 set_opt(sbi->s_mount_opt, DELALLOC);
1984 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
1985 NULL, 0))
1986 goto failed_mount;
1988 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
1989 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
1991 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1992 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1993 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1994 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
1995 printk(KERN_WARNING
1996 "EXT4-fs warning: feature flags set on rev 0 fs, "
1997 "running e2fsck is recommended\n");
2000 * Check feature flags regardless of the revision level, since we
2001 * previously didn't change the revision level when setting the flags,
2002 * so there is a chance incompat flags are set on a rev 0 filesystem.
2004 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2005 if (features) {
2006 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2007 "unsupported optional features (%x).\n",
2008 sb->s_id, le32_to_cpu(features));
2009 goto failed_mount;
2011 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2012 if (!(sb->s_flags & MS_RDONLY) && features) {
2013 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2014 "unsupported optional features (%x).\n",
2015 sb->s_id, le32_to_cpu(features));
2016 goto failed_mount;
2018 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2019 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2020 if (has_huge_files) {
2022 * Large file size enabled file system can only be
2023 * mount if kernel is build with CONFIG_LBD
2025 if (sizeof(root->i_blocks) < sizeof(u64) &&
2026 !(sb->s_flags & MS_RDONLY)) {
2027 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2028 "files cannot be mounted read-write "
2029 "without CONFIG_LBD.\n", sb->s_id);
2030 goto failed_mount;
2033 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2035 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2036 blocksize > EXT4_MAX_BLOCK_SIZE) {
2037 printk(KERN_ERR
2038 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2039 blocksize, sb->s_id);
2040 goto failed_mount;
2043 if (sb->s_blocksize != blocksize) {
2045 /* Validate the filesystem blocksize */
2046 if (!sb_set_blocksize(sb, blocksize)) {
2047 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2048 blocksize);
2049 goto failed_mount;
2052 brelse(bh);
2053 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2054 offset = do_div(logical_sb_block, blocksize);
2055 bh = sb_bread(sb, logical_sb_block);
2056 if (!bh) {
2057 printk(KERN_ERR
2058 "EXT4-fs: Can't read superblock on 2nd try.\n");
2059 goto failed_mount;
2061 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2062 sbi->s_es = es;
2063 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2064 printk(KERN_ERR
2065 "EXT4-fs: Magic mismatch, very weird !\n");
2066 goto failed_mount;
2070 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2071 has_huge_files);
2072 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
2074 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2075 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2076 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2077 } else {
2078 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2079 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2080 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2081 (!is_power_of_2(sbi->s_inode_size)) ||
2082 (sbi->s_inode_size > blocksize)) {
2083 printk(KERN_ERR
2084 "EXT4-fs: unsupported inode size: %d\n",
2085 sbi->s_inode_size);
2086 goto failed_mount;
2088 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2089 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2091 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2092 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2093 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2094 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2095 !is_power_of_2(sbi->s_desc_size)) {
2096 printk(KERN_ERR
2097 "EXT4-fs: unsupported descriptor size %lu\n",
2098 sbi->s_desc_size);
2099 goto failed_mount;
2101 } else
2102 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2103 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2104 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2105 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2106 goto cantfind_ext4;
2107 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2108 if (sbi->s_inodes_per_block == 0)
2109 goto cantfind_ext4;
2110 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2111 sbi->s_inodes_per_block;
2112 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2113 sbi->s_sbh = bh;
2114 sbi->s_mount_state = le16_to_cpu(es->s_state);
2115 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2116 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2117 for (i = 0; i < 4; i++)
2118 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2119 sbi->s_def_hash_version = es->s_def_hash_version;
2121 if (sbi->s_blocks_per_group > blocksize * 8) {
2122 printk(KERN_ERR
2123 "EXT4-fs: #blocks per group too big: %lu\n",
2124 sbi->s_blocks_per_group);
2125 goto failed_mount;
2127 if (sbi->s_inodes_per_group > blocksize * 8) {
2128 printk(KERN_ERR
2129 "EXT4-fs: #inodes per group too big: %lu\n",
2130 sbi->s_inodes_per_group);
2131 goto failed_mount;
2134 if (ext4_blocks_count(es) >
2135 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2136 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2137 " too large to mount safely\n", sb->s_id);
2138 if (sizeof(sector_t) < 8)
2139 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2140 "enabled\n");
2141 goto failed_mount;
2144 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2145 goto cantfind_ext4;
2147 /* ensure blocks_count calculation below doesn't sign-extend */
2148 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2149 le32_to_cpu(es->s_first_data_block) + 1) {
2150 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2151 "first data block %u, blocks per group %lu\n",
2152 ext4_blocks_count(es),
2153 le32_to_cpu(es->s_first_data_block),
2154 EXT4_BLOCKS_PER_GROUP(sb));
2155 goto failed_mount;
2157 blocks_count = (ext4_blocks_count(es) -
2158 le32_to_cpu(es->s_first_data_block) +
2159 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2160 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2161 sbi->s_groups_count = blocks_count;
2162 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2163 EXT4_DESC_PER_BLOCK(sb);
2164 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2165 GFP_KERNEL);
2166 if (sbi->s_group_desc == NULL) {
2167 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2168 goto failed_mount;
2171 #ifdef CONFIG_PROC_FS
2172 if (ext4_proc_root)
2173 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2175 if (sbi->s_proc)
2176 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2177 &ext4_ui_proc_fops,
2178 &sbi->s_inode_readahead_blks);
2179 #endif
2181 bgl_lock_init(&sbi->s_blockgroup_lock);
2183 for (i = 0; i < db_count; i++) {
2184 block = descriptor_loc(sb, logical_sb_block, i);
2185 sbi->s_group_desc[i] = sb_bread(sb, block);
2186 if (!sbi->s_group_desc[i]) {
2187 printk(KERN_ERR "EXT4-fs: "
2188 "can't read group descriptor %d\n", i);
2189 db_count = i;
2190 goto failed_mount2;
2193 if (!ext4_check_descriptors(sb)) {
2194 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2195 goto failed_mount2;
2197 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2198 if (!ext4_fill_flex_info(sb)) {
2199 printk(KERN_ERR
2200 "EXT4-fs: unable to initialize "
2201 "flex_bg meta info!\n");
2202 goto failed_mount2;
2205 sbi->s_gdb_count = db_count;
2206 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2207 spin_lock_init(&sbi->s_next_gen_lock);
2209 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2210 ext4_count_free_blocks(sb));
2211 if (!err) {
2212 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2213 ext4_count_free_inodes(sb));
2215 if (!err) {
2216 err = percpu_counter_init(&sbi->s_dirs_counter,
2217 ext4_count_dirs(sb));
2219 if (!err) {
2220 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2222 if (err) {
2223 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2224 goto failed_mount3;
2227 sbi->s_stripe = ext4_get_stripe_size(sbi);
2230 * set up enough so that it can read an inode
2232 sb->s_op = &ext4_sops;
2233 sb->s_export_op = &ext4_export_ops;
2234 sb->s_xattr = ext4_xattr_handlers;
2235 #ifdef CONFIG_QUOTA
2236 sb->s_qcop = &ext4_qctl_operations;
2237 sb->dq_op = &ext4_quota_operations;
2238 #endif
2239 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2241 sb->s_root = NULL;
2243 needs_recovery = (es->s_last_orphan != 0 ||
2244 EXT4_HAS_INCOMPAT_FEATURE(sb,
2245 EXT4_FEATURE_INCOMPAT_RECOVER));
2248 * The first inode we look at is the journal inode. Don't try
2249 * root first: it may be modified in the journal!
2251 if (!test_opt(sb, NOLOAD) &&
2252 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2253 if (ext4_load_journal(sb, es, journal_devnum))
2254 goto failed_mount3;
2255 if (!(sb->s_flags & MS_RDONLY) &&
2256 EXT4_SB(sb)->s_journal->j_failed_commit) {
2257 printk(KERN_CRIT "EXT4-fs error (device %s): "
2258 "ext4_fill_super: Journal transaction "
2259 "%u is corrupt\n", sb->s_id,
2260 EXT4_SB(sb)->s_journal->j_failed_commit);
2261 if (test_opt(sb, ERRORS_RO)) {
2262 printk(KERN_CRIT
2263 "Mounting filesystem read-only\n");
2264 sb->s_flags |= MS_RDONLY;
2265 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2266 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2268 if (test_opt(sb, ERRORS_PANIC)) {
2269 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2270 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2271 ext4_commit_super(sb, es, 1);
2272 printk(KERN_CRIT
2273 "EXT4-fs (device %s): mount failed\n",
2274 sb->s_id);
2275 goto failed_mount4;
2278 } else if (journal_inum) {
2279 if (ext4_create_journal(sb, es, journal_inum))
2280 goto failed_mount3;
2281 } else {
2282 if (!silent)
2283 printk(KERN_ERR
2284 "ext4: No journal on filesystem on %s\n",
2285 sb->s_id);
2286 goto failed_mount3;
2289 if (ext4_blocks_count(es) > 0xffffffffULL &&
2290 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2291 JBD2_FEATURE_INCOMPAT_64BIT)) {
2292 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2293 goto failed_mount4;
2296 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2297 jbd2_journal_set_features(sbi->s_journal,
2298 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2299 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2300 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2301 jbd2_journal_set_features(sbi->s_journal,
2302 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2303 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2304 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2305 } else {
2306 jbd2_journal_clear_features(sbi->s_journal,
2307 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2308 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2311 /* We have now updated the journal if required, so we can
2312 * validate the data journaling mode. */
2313 switch (test_opt(sb, DATA_FLAGS)) {
2314 case 0:
2315 /* No mode set, assume a default based on the journal
2316 * capabilities: ORDERED_DATA if the journal can
2317 * cope, else JOURNAL_DATA
2319 if (jbd2_journal_check_available_features
2320 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2321 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2322 else
2323 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2324 break;
2326 case EXT4_MOUNT_ORDERED_DATA:
2327 case EXT4_MOUNT_WRITEBACK_DATA:
2328 if (!jbd2_journal_check_available_features
2329 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2330 printk(KERN_ERR "EXT4-fs: Journal does not support "
2331 "requested data journaling mode\n");
2332 goto failed_mount4;
2334 default:
2335 break;
2338 if (test_opt(sb, NOBH)) {
2339 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2340 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2341 "its supported only with writeback mode\n");
2342 clear_opt(sbi->s_mount_opt, NOBH);
2346 * The jbd2_journal_load will have done any necessary log recovery,
2347 * so we can safely mount the rest of the filesystem now.
2350 root = ext4_iget(sb, EXT4_ROOT_INO);
2351 if (IS_ERR(root)) {
2352 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2353 ret = PTR_ERR(root);
2354 goto failed_mount4;
2356 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2357 iput(root);
2358 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2359 goto failed_mount4;
2361 sb->s_root = d_alloc_root(root);
2362 if (!sb->s_root) {
2363 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2364 iput(root);
2365 ret = -ENOMEM;
2366 goto failed_mount4;
2369 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2371 /* determine the minimum size of new large inodes, if present */
2372 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2373 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2374 EXT4_GOOD_OLD_INODE_SIZE;
2375 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2376 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2377 if (sbi->s_want_extra_isize <
2378 le16_to_cpu(es->s_want_extra_isize))
2379 sbi->s_want_extra_isize =
2380 le16_to_cpu(es->s_want_extra_isize);
2381 if (sbi->s_want_extra_isize <
2382 le16_to_cpu(es->s_min_extra_isize))
2383 sbi->s_want_extra_isize =
2384 le16_to_cpu(es->s_min_extra_isize);
2387 /* Check if enough inode space is available */
2388 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2389 sbi->s_inode_size) {
2390 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2391 EXT4_GOOD_OLD_INODE_SIZE;
2392 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2393 "available.\n");
2396 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2397 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2398 "requested data journaling mode\n");
2399 clear_opt(sbi->s_mount_opt, DELALLOC);
2400 } else if (test_opt(sb, DELALLOC))
2401 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2403 ext4_ext_init(sb);
2404 err = ext4_mb_init(sb, needs_recovery);
2405 if (err) {
2406 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2407 err);
2408 goto failed_mount4;
2412 * akpm: core read_super() calls in here with the superblock locked.
2413 * That deadlocks, because orphan cleanup needs to lock the superblock
2414 * in numerous places. Here we just pop the lock - it's relatively
2415 * harmless, because we are now ready to accept write_super() requests,
2416 * and aviro says that's the only reason for hanging onto the
2417 * superblock lock.
2419 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2420 ext4_orphan_cleanup(sb, es);
2421 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2422 if (needs_recovery)
2423 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2424 ext4_mark_recovery_complete(sb, es);
2425 printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2426 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2427 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2428 "writeback");
2430 lock_kernel();
2431 return 0;
2433 cantfind_ext4:
2434 if (!silent)
2435 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2436 sb->s_id);
2437 goto failed_mount;
2439 failed_mount4:
2440 jbd2_journal_destroy(sbi->s_journal);
2441 sbi->s_journal = NULL;
2442 failed_mount3:
2443 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2444 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2445 percpu_counter_destroy(&sbi->s_dirs_counter);
2446 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
2447 failed_mount2:
2448 for (i = 0; i < db_count; i++)
2449 brelse(sbi->s_group_desc[i]);
2450 kfree(sbi->s_group_desc);
2451 failed_mount:
2452 if (sbi->s_proc) {
2453 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
2454 remove_proc_entry(sb->s_id, ext4_proc_root);
2456 #ifdef CONFIG_QUOTA
2457 for (i = 0; i < MAXQUOTAS; i++)
2458 kfree(sbi->s_qf_names[i]);
2459 #endif
2460 ext4_blkdev_remove(sbi);
2461 brelse(bh);
2462 out_fail:
2463 sb->s_fs_info = NULL;
2464 kfree(sbi);
2465 lock_kernel();
2466 return ret;
2470 * Setup any per-fs journal parameters now. We'll do this both on
2471 * initial mount, once the journal has been initialised but before we've
2472 * done any recovery; and again on any subsequent remount.
2474 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2476 struct ext4_sb_info *sbi = EXT4_SB(sb);
2478 if (sbi->s_commit_interval)
2479 journal->j_commit_interval = sbi->s_commit_interval;
2480 /* We could also set up an ext4-specific default for the commit
2481 * interval here, but for now we'll just fall back to the jbd
2482 * default. */
2484 spin_lock(&journal->j_state_lock);
2485 if (test_opt(sb, BARRIER))
2486 journal->j_flags |= JBD2_BARRIER;
2487 else
2488 journal->j_flags &= ~JBD2_BARRIER;
2489 if (test_opt(sb, DATA_ERR_ABORT))
2490 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2491 else
2492 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
2493 spin_unlock(&journal->j_state_lock);
2496 static journal_t *ext4_get_journal(struct super_block *sb,
2497 unsigned int journal_inum)
2499 struct inode *journal_inode;
2500 journal_t *journal;
2502 /* First, test for the existence of a valid inode on disk. Bad
2503 * things happen if we iget() an unused inode, as the subsequent
2504 * iput() will try to delete it. */
2506 journal_inode = ext4_iget(sb, journal_inum);
2507 if (IS_ERR(journal_inode)) {
2508 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2509 return NULL;
2511 if (!journal_inode->i_nlink) {
2512 make_bad_inode(journal_inode);
2513 iput(journal_inode);
2514 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2515 return NULL;
2518 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
2519 journal_inode, journal_inode->i_size);
2520 if (!S_ISREG(journal_inode->i_mode)) {
2521 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2522 iput(journal_inode);
2523 return NULL;
2526 journal = jbd2_journal_init_inode(journal_inode);
2527 if (!journal) {
2528 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2529 iput(journal_inode);
2530 return NULL;
2532 journal->j_private = sb;
2533 ext4_init_journal_params(sb, journal);
2534 return journal;
2537 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2538 dev_t j_dev)
2540 struct buffer_head *bh;
2541 journal_t *journal;
2542 ext4_fsblk_t start;
2543 ext4_fsblk_t len;
2544 int hblock, blocksize;
2545 ext4_fsblk_t sb_block;
2546 unsigned long offset;
2547 struct ext4_super_block *es;
2548 struct block_device *bdev;
2550 bdev = ext4_blkdev_get(j_dev);
2551 if (bdev == NULL)
2552 return NULL;
2554 if (bd_claim(bdev, sb)) {
2555 printk(KERN_ERR
2556 "EXT4: failed to claim external journal device.\n");
2557 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2558 return NULL;
2561 blocksize = sb->s_blocksize;
2562 hblock = bdev_hardsect_size(bdev);
2563 if (blocksize < hblock) {
2564 printk(KERN_ERR
2565 "EXT4-fs: blocksize too small for journal device.\n");
2566 goto out_bdev;
2569 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2570 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2571 set_blocksize(bdev, blocksize);
2572 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2573 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2574 "external journal\n");
2575 goto out_bdev;
2578 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2579 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2580 !(le32_to_cpu(es->s_feature_incompat) &
2581 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2582 printk(KERN_ERR "EXT4-fs: external journal has "
2583 "bad superblock\n");
2584 brelse(bh);
2585 goto out_bdev;
2588 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2589 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2590 brelse(bh);
2591 goto out_bdev;
2594 len = ext4_blocks_count(es);
2595 start = sb_block + 1;
2596 brelse(bh); /* we're done with the superblock */
2598 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2599 start, len, blocksize);
2600 if (!journal) {
2601 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2602 goto out_bdev;
2604 journal->j_private = sb;
2605 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2606 wait_on_buffer(journal->j_sb_buffer);
2607 if (!buffer_uptodate(journal->j_sb_buffer)) {
2608 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2609 goto out_journal;
2611 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2612 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2613 "user (unsupported) - %d\n",
2614 be32_to_cpu(journal->j_superblock->s_nr_users));
2615 goto out_journal;
2617 EXT4_SB(sb)->journal_bdev = bdev;
2618 ext4_init_journal_params(sb, journal);
2619 return journal;
2620 out_journal:
2621 jbd2_journal_destroy(journal);
2622 out_bdev:
2623 ext4_blkdev_put(bdev);
2624 return NULL;
2627 static int ext4_load_journal(struct super_block *sb,
2628 struct ext4_super_block *es,
2629 unsigned long journal_devnum)
2631 journal_t *journal;
2632 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2633 dev_t journal_dev;
2634 int err = 0;
2635 int really_read_only;
2637 if (journal_devnum &&
2638 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2639 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2640 "numbers have changed\n");
2641 journal_dev = new_decode_dev(journal_devnum);
2642 } else
2643 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2645 really_read_only = bdev_read_only(sb->s_bdev);
2648 * Are we loading a blank journal or performing recovery after a
2649 * crash? For recovery, we need to check in advance whether we
2650 * can get read-write access to the device.
2653 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2654 if (sb->s_flags & MS_RDONLY) {
2655 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2656 "required on readonly filesystem.\n");
2657 if (really_read_only) {
2658 printk(KERN_ERR "EXT4-fs: write access "
2659 "unavailable, cannot proceed.\n");
2660 return -EROFS;
2662 printk(KERN_INFO "EXT4-fs: write access will "
2663 "be enabled during recovery.\n");
2667 if (journal_inum && journal_dev) {
2668 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2669 "and inode journals!\n");
2670 return -EINVAL;
2673 if (journal_inum) {
2674 if (!(journal = ext4_get_journal(sb, journal_inum)))
2675 return -EINVAL;
2676 } else {
2677 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2678 return -EINVAL;
2681 if (journal->j_flags & JBD2_BARRIER)
2682 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2683 else
2684 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2686 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2687 err = jbd2_journal_update_format(journal);
2688 if (err) {
2689 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2690 jbd2_journal_destroy(journal);
2691 return err;
2695 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2696 err = jbd2_journal_wipe(journal, !really_read_only);
2697 if (!err)
2698 err = jbd2_journal_load(journal);
2700 if (err) {
2701 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2702 jbd2_journal_destroy(journal);
2703 return err;
2706 EXT4_SB(sb)->s_journal = journal;
2707 ext4_clear_journal_err(sb, es);
2709 if (journal_devnum &&
2710 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2711 es->s_journal_dev = cpu_to_le32(journal_devnum);
2712 sb->s_dirt = 1;
2714 /* Make sure we flush the recovery flag to disk. */
2715 ext4_commit_super(sb, es, 1);
2718 return 0;
2721 static int ext4_create_journal(struct super_block *sb,
2722 struct ext4_super_block *es,
2723 unsigned int journal_inum)
2725 journal_t *journal;
2726 int err;
2728 if (sb->s_flags & MS_RDONLY) {
2729 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2730 "create journal.\n");
2731 return -EROFS;
2734 journal = ext4_get_journal(sb, journal_inum);
2735 if (!journal)
2736 return -EINVAL;
2738 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2739 journal_inum);
2741 err = jbd2_journal_create(journal);
2742 if (err) {
2743 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2744 jbd2_journal_destroy(journal);
2745 return -EIO;
2748 EXT4_SB(sb)->s_journal = journal;
2750 ext4_update_dynamic_rev(sb);
2751 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2752 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2754 es->s_journal_inum = cpu_to_le32(journal_inum);
2755 sb->s_dirt = 1;
2757 /* Make sure we flush the recovery flag to disk. */
2758 ext4_commit_super(sb, es, 1);
2760 return 0;
2763 static void ext4_commit_super(struct super_block *sb,
2764 struct ext4_super_block *es, int sync)
2766 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2768 if (!sbh)
2769 return;
2770 if (buffer_write_io_error(sbh)) {
2772 * Oh, dear. A previous attempt to write the
2773 * superblock failed. This could happen because the
2774 * USB device was yanked out. Or it could happen to
2775 * be a transient write error and maybe the block will
2776 * be remapped. Nothing we can do but to retry the
2777 * write and hope for the best.
2779 printk(KERN_ERR "ext4: previous I/O error to "
2780 "superblock detected for %s.\n", sb->s_id);
2781 clear_buffer_write_io_error(sbh);
2782 set_buffer_uptodate(sbh);
2784 es->s_wtime = cpu_to_le32(get_seconds());
2785 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2786 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2787 BUFFER_TRACE(sbh, "marking dirty");
2788 mark_buffer_dirty(sbh);
2789 if (sync) {
2790 sync_dirty_buffer(sbh);
2791 if (buffer_write_io_error(sbh)) {
2792 printk(KERN_ERR "ext4: I/O error while writing "
2793 "superblock for %s.\n", sb->s_id);
2794 clear_buffer_write_io_error(sbh);
2795 set_buffer_uptodate(sbh);
2802 * Have we just finished recovery? If so, and if we are mounting (or
2803 * remounting) the filesystem readonly, then we will end up with a
2804 * consistent fs on disk. Record that fact.
2806 static void ext4_mark_recovery_complete(struct super_block *sb,
2807 struct ext4_super_block *es)
2809 journal_t *journal = EXT4_SB(sb)->s_journal;
2811 jbd2_journal_lock_updates(journal);
2812 if (jbd2_journal_flush(journal) < 0)
2813 goto out;
2815 lock_super(sb);
2816 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2817 sb->s_flags & MS_RDONLY) {
2818 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2819 sb->s_dirt = 0;
2820 ext4_commit_super(sb, es, 1);
2822 unlock_super(sb);
2824 out:
2825 jbd2_journal_unlock_updates(journal);
2829 * If we are mounting (or read-write remounting) a filesystem whose journal
2830 * has recorded an error from a previous lifetime, move that error to the
2831 * main filesystem now.
2833 static void ext4_clear_journal_err(struct super_block *sb,
2834 struct ext4_super_block *es)
2836 journal_t *journal;
2837 int j_errno;
2838 const char *errstr;
2840 journal = EXT4_SB(sb)->s_journal;
2843 * Now check for any error status which may have been recorded in the
2844 * journal by a prior ext4_error() or ext4_abort()
2847 j_errno = jbd2_journal_errno(journal);
2848 if (j_errno) {
2849 char nbuf[16];
2851 errstr = ext4_decode_error(sb, j_errno, nbuf);
2852 ext4_warning(sb, __func__, "Filesystem error recorded "
2853 "from previous mount: %s", errstr);
2854 ext4_warning(sb, __func__, "Marking fs in need of "
2855 "filesystem check.");
2857 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2858 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2859 ext4_commit_super(sb, es, 1);
2861 jbd2_journal_clear_err(journal);
2866 * Force the running and committing transactions to commit,
2867 * and wait on the commit.
2869 int ext4_force_commit(struct super_block *sb)
2871 journal_t *journal;
2872 int ret;
2874 if (sb->s_flags & MS_RDONLY)
2875 return 0;
2877 journal = EXT4_SB(sb)->s_journal;
2878 sb->s_dirt = 0;
2879 ret = ext4_journal_force_commit(journal);
2880 return ret;
2884 * Ext4 always journals updates to the superblock itself, so we don't
2885 * have to propagate any other updates to the superblock on disk at this
2886 * point. (We can probably nuke this function altogether, and remove
2887 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
2889 static void ext4_write_super(struct super_block *sb)
2891 if (mutex_trylock(&sb->s_lock) != 0)
2892 BUG();
2893 sb->s_dirt = 0;
2896 static int ext4_sync_fs(struct super_block *sb, int wait)
2898 int ret = 0;
2900 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
2901 sb->s_dirt = 0;
2902 if (wait)
2903 ret = ext4_force_commit(sb);
2904 else
2905 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
2906 return ret;
2910 * LVM calls this function before a (read-only) snapshot is created. This
2911 * gives us a chance to flush the journal completely and mark the fs clean.
2913 static void ext4_write_super_lockfs(struct super_block *sb)
2915 sb->s_dirt = 0;
2917 if (!(sb->s_flags & MS_RDONLY)) {
2918 journal_t *journal = EXT4_SB(sb)->s_journal;
2920 /* Now we set up the journal barrier. */
2921 jbd2_journal_lock_updates(journal);
2924 * We don't want to clear needs_recovery flag when we failed
2925 * to flush the journal.
2927 if (jbd2_journal_flush(journal) < 0)
2928 return;
2930 /* Journal blocked and flushed, clear needs_recovery flag. */
2931 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2932 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2937 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2938 * flag here, even though the filesystem is not technically dirty yet.
2940 static void ext4_unlockfs(struct super_block *sb)
2942 if (!(sb->s_flags & MS_RDONLY)) {
2943 lock_super(sb);
2944 /* Reser the needs_recovery flag before the fs is unlocked. */
2945 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2946 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2947 unlock_super(sb);
2948 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2952 static int ext4_remount(struct super_block *sb, int *flags, char *data)
2954 struct ext4_super_block *es;
2955 struct ext4_sb_info *sbi = EXT4_SB(sb);
2956 ext4_fsblk_t n_blocks_count = 0;
2957 unsigned long old_sb_flags;
2958 struct ext4_mount_options old_opts;
2959 ext4_group_t g;
2960 int err;
2961 #ifdef CONFIG_QUOTA
2962 int i;
2963 #endif
2965 /* Store the original options */
2966 old_sb_flags = sb->s_flags;
2967 old_opts.s_mount_opt = sbi->s_mount_opt;
2968 old_opts.s_resuid = sbi->s_resuid;
2969 old_opts.s_resgid = sbi->s_resgid;
2970 old_opts.s_commit_interval = sbi->s_commit_interval;
2971 #ifdef CONFIG_QUOTA
2972 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2973 for (i = 0; i < MAXQUOTAS; i++)
2974 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2975 #endif
2978 * Allow the "check" option to be passed as a remount option.
2980 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2981 err = -EINVAL;
2982 goto restore_opts;
2985 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2986 ext4_abort(sb, __func__, "Abort forced by user");
2988 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2989 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2991 es = sbi->s_es;
2993 ext4_init_journal_params(sb, sbi->s_journal);
2995 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2996 n_blocks_count > ext4_blocks_count(es)) {
2997 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
2998 err = -EROFS;
2999 goto restore_opts;
3002 if (*flags & MS_RDONLY) {
3004 * First of all, the unconditional stuff we have to do
3005 * to disable replay of the journal when we next remount
3007 sb->s_flags |= MS_RDONLY;
3010 * OK, test if we are remounting a valid rw partition
3011 * readonly, and if so set the rdonly flag and then
3012 * mark the partition as valid again.
3014 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3015 (sbi->s_mount_state & EXT4_VALID_FS))
3016 es->s_state = cpu_to_le16(sbi->s_mount_state);
3019 * We have to unlock super so that we can wait for
3020 * transactions.
3022 unlock_super(sb);
3023 ext4_mark_recovery_complete(sb, es);
3024 lock_super(sb);
3025 } else {
3026 __le32 ret;
3027 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3028 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3029 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3030 "remount RDWR because of unsupported "
3031 "optional features (%x).\n",
3032 sb->s_id, le32_to_cpu(ret));
3033 err = -EROFS;
3034 goto restore_opts;
3038 * Make sure the group descriptor checksums
3039 * are sane. If they aren't, refuse to
3040 * remount r/w.
3042 for (g = 0; g < sbi->s_groups_count; g++) {
3043 struct ext4_group_desc *gdp =
3044 ext4_get_group_desc(sb, g, NULL);
3046 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3047 printk(KERN_ERR
3048 "EXT4-fs: ext4_remount: "
3049 "Checksum for group %lu failed (%u!=%u)\n",
3050 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3051 le16_to_cpu(gdp->bg_checksum));
3052 err = -EINVAL;
3053 goto restore_opts;
3058 * If we have an unprocessed orphan list hanging
3059 * around from a previously readonly bdev mount,
3060 * require a full umount/remount for now.
3062 if (es->s_last_orphan) {
3063 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3064 "remount RDWR because of unprocessed "
3065 "orphan inode list. Please "
3066 "umount/remount instead.\n",
3067 sb->s_id);
3068 err = -EINVAL;
3069 goto restore_opts;
3073 * Mounting a RDONLY partition read-write, so reread
3074 * and store the current valid flag. (It may have
3075 * been changed by e2fsck since we originally mounted
3076 * the partition.)
3078 ext4_clear_journal_err(sb, es);
3079 sbi->s_mount_state = le16_to_cpu(es->s_state);
3080 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3081 goto restore_opts;
3082 if (!ext4_setup_super(sb, es, 0))
3083 sb->s_flags &= ~MS_RDONLY;
3086 #ifdef CONFIG_QUOTA
3087 /* Release old quota file names */
3088 for (i = 0; i < MAXQUOTAS; i++)
3089 if (old_opts.s_qf_names[i] &&
3090 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3091 kfree(old_opts.s_qf_names[i]);
3092 #endif
3093 return 0;
3094 restore_opts:
3095 sb->s_flags = old_sb_flags;
3096 sbi->s_mount_opt = old_opts.s_mount_opt;
3097 sbi->s_resuid = old_opts.s_resuid;
3098 sbi->s_resgid = old_opts.s_resgid;
3099 sbi->s_commit_interval = old_opts.s_commit_interval;
3100 #ifdef CONFIG_QUOTA
3101 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3102 for (i = 0; i < MAXQUOTAS; i++) {
3103 if (sbi->s_qf_names[i] &&
3104 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3105 kfree(sbi->s_qf_names[i]);
3106 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3108 #endif
3109 return err;
3112 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3114 struct super_block *sb = dentry->d_sb;
3115 struct ext4_sb_info *sbi = EXT4_SB(sb);
3116 struct ext4_super_block *es = sbi->s_es;
3117 u64 fsid;
3119 if (test_opt(sb, MINIX_DF)) {
3120 sbi->s_overhead_last = 0;
3121 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3122 ext4_group_t ngroups = sbi->s_groups_count, i;
3123 ext4_fsblk_t overhead = 0;
3124 smp_rmb();
3127 * Compute the overhead (FS structures). This is constant
3128 * for a given filesystem unless the number of block groups
3129 * changes so we cache the previous value until it does.
3133 * All of the blocks before first_data_block are
3134 * overhead
3136 overhead = le32_to_cpu(es->s_first_data_block);
3139 * Add the overhead attributed to the superblock and
3140 * block group descriptors. If the sparse superblocks
3141 * feature is turned on, then not all groups have this.
3143 for (i = 0; i < ngroups; i++) {
3144 overhead += ext4_bg_has_super(sb, i) +
3145 ext4_bg_num_gdb(sb, i);
3146 cond_resched();
3150 * Every block group has an inode bitmap, a block
3151 * bitmap, and an inode table.
3153 overhead += ngroups * (2 + sbi->s_itb_per_group);
3154 sbi->s_overhead_last = overhead;
3155 smp_wmb();
3156 sbi->s_blocks_last = ext4_blocks_count(es);
3159 buf->f_type = EXT4_SUPER_MAGIC;
3160 buf->f_bsize = sb->s_blocksize;
3161 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3162 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3163 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
3164 ext4_free_blocks_count_set(es, buf->f_bfree);
3165 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3166 if (buf->f_bfree < ext4_r_blocks_count(es))
3167 buf->f_bavail = 0;
3168 buf->f_files = le32_to_cpu(es->s_inodes_count);
3169 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3170 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3171 buf->f_namelen = EXT4_NAME_LEN;
3172 fsid = le64_to_cpup((void *)es->s_uuid) ^
3173 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3174 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3175 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3176 return 0;
3179 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3180 * is locked for write. Otherwise the are possible deadlocks:
3181 * Process 1 Process 2
3182 * ext4_create() quota_sync()
3183 * jbd2_journal_start() write_dquot()
3184 * DQUOT_INIT() down(dqio_mutex)
3185 * down(dqio_mutex) jbd2_journal_start()
3189 #ifdef CONFIG_QUOTA
3191 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3193 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3196 static int ext4_dquot_initialize(struct inode *inode, int type)
3198 handle_t *handle;
3199 int ret, err;
3201 /* We may create quota structure so we need to reserve enough blocks */
3202 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3203 if (IS_ERR(handle))
3204 return PTR_ERR(handle);
3205 ret = dquot_initialize(inode, type);
3206 err = ext4_journal_stop(handle);
3207 if (!ret)
3208 ret = err;
3209 return ret;
3212 static int ext4_dquot_drop(struct inode *inode)
3214 handle_t *handle;
3215 int ret, err;
3217 /* We may delete quota structure so we need to reserve enough blocks */
3218 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3219 if (IS_ERR(handle)) {
3221 * We call dquot_drop() anyway to at least release references
3222 * to quota structures so that umount does not hang.
3224 dquot_drop(inode);
3225 return PTR_ERR(handle);
3227 ret = dquot_drop(inode);
3228 err = ext4_journal_stop(handle);
3229 if (!ret)
3230 ret = err;
3231 return ret;
3234 static int ext4_write_dquot(struct dquot *dquot)
3236 int ret, err;
3237 handle_t *handle;
3238 struct inode *inode;
3240 inode = dquot_to_inode(dquot);
3241 handle = ext4_journal_start(inode,
3242 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3243 if (IS_ERR(handle))
3244 return PTR_ERR(handle);
3245 ret = dquot_commit(dquot);
3246 err = ext4_journal_stop(handle);
3247 if (!ret)
3248 ret = err;
3249 return ret;
3252 static int ext4_acquire_dquot(struct dquot *dquot)
3254 int ret, err;
3255 handle_t *handle;
3257 handle = ext4_journal_start(dquot_to_inode(dquot),
3258 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3259 if (IS_ERR(handle))
3260 return PTR_ERR(handle);
3261 ret = dquot_acquire(dquot);
3262 err = ext4_journal_stop(handle);
3263 if (!ret)
3264 ret = err;
3265 return ret;
3268 static int ext4_release_dquot(struct dquot *dquot)
3270 int ret, err;
3271 handle_t *handle;
3273 handle = ext4_journal_start(dquot_to_inode(dquot),
3274 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3275 if (IS_ERR(handle)) {
3276 /* Release dquot anyway to avoid endless cycle in dqput() */
3277 dquot_release(dquot);
3278 return PTR_ERR(handle);
3280 ret = dquot_release(dquot);
3281 err = ext4_journal_stop(handle);
3282 if (!ret)
3283 ret = err;
3284 return ret;
3287 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3289 /* Are we journaling quotas? */
3290 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3291 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3292 dquot_mark_dquot_dirty(dquot);
3293 return ext4_write_dquot(dquot);
3294 } else {
3295 return dquot_mark_dquot_dirty(dquot);
3299 static int ext4_write_info(struct super_block *sb, int type)
3301 int ret, err;
3302 handle_t *handle;
3304 /* Data block + inode block */
3305 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3306 if (IS_ERR(handle))
3307 return PTR_ERR(handle);
3308 ret = dquot_commit_info(sb, type);
3309 err = ext4_journal_stop(handle);
3310 if (!ret)
3311 ret = err;
3312 return ret;
3316 * Turn on quotas during mount time - we need to find
3317 * the quota file and such...
3319 static int ext4_quota_on_mount(struct super_block *sb, int type)
3321 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3322 EXT4_SB(sb)->s_jquota_fmt, type);
3326 * Standard function to be called on quota_on
3328 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3329 char *name, int remount)
3331 int err;
3332 struct path path;
3334 if (!test_opt(sb, QUOTA))
3335 return -EINVAL;
3336 /* When remounting, no checks are needed and in fact, name is NULL */
3337 if (remount)
3338 return vfs_quota_on(sb, type, format_id, name, remount);
3340 err = kern_path(name, LOOKUP_FOLLOW, &path);
3341 if (err)
3342 return err;
3344 /* Quotafile not on the same filesystem? */
3345 if (path.mnt->mnt_sb != sb) {
3346 path_put(&path);
3347 return -EXDEV;
3349 /* Journaling quota? */
3350 if (EXT4_SB(sb)->s_qf_names[type]) {
3351 /* Quotafile not in fs root? */
3352 if (path.dentry->d_parent != sb->s_root)
3353 printk(KERN_WARNING
3354 "EXT4-fs: Quota file not on filesystem root. "
3355 "Journaled quota will not work.\n");
3359 * When we journal data on quota file, we have to flush journal to see
3360 * all updates to the file when we bypass pagecache...
3362 if (ext4_should_journal_data(path.dentry->d_inode)) {
3364 * We don't need to lock updates but journal_flush() could
3365 * otherwise be livelocked...
3367 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3368 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3369 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3370 if (err) {
3371 path_put(&path);
3372 return err;
3376 err = vfs_quota_on_path(sb, type, format_id, &path);
3377 path_put(&path);
3378 return err;
3381 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3382 * acquiring the locks... As quota files are never truncated and quota code
3383 * itself serializes the operations (and noone else should touch the files)
3384 * we don't have to be afraid of races */
3385 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3386 size_t len, loff_t off)
3388 struct inode *inode = sb_dqopt(sb)->files[type];
3389 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3390 int err = 0;
3391 int offset = off & (sb->s_blocksize - 1);
3392 int tocopy;
3393 size_t toread;
3394 struct buffer_head *bh;
3395 loff_t i_size = i_size_read(inode);
3397 if (off > i_size)
3398 return 0;
3399 if (off+len > i_size)
3400 len = i_size-off;
3401 toread = len;
3402 while (toread > 0) {
3403 tocopy = sb->s_blocksize - offset < toread ?
3404 sb->s_blocksize - offset : toread;
3405 bh = ext4_bread(NULL, inode, blk, 0, &err);
3406 if (err)
3407 return err;
3408 if (!bh) /* A hole? */
3409 memset(data, 0, tocopy);
3410 else
3411 memcpy(data, bh->b_data+offset, tocopy);
3412 brelse(bh);
3413 offset = 0;
3414 toread -= tocopy;
3415 data += tocopy;
3416 blk++;
3418 return len;
3421 /* Write to quotafile (we know the transaction is already started and has
3422 * enough credits) */
3423 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3424 const char *data, size_t len, loff_t off)
3426 struct inode *inode = sb_dqopt(sb)->files[type];
3427 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3428 int err = 0;
3429 int offset = off & (sb->s_blocksize - 1);
3430 int tocopy;
3431 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3432 size_t towrite = len;
3433 struct buffer_head *bh;
3434 handle_t *handle = journal_current_handle();
3436 if (!handle) {
3437 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
3438 " cancelled because transaction is not started.\n",
3439 (unsigned long long)off, (unsigned long long)len);
3440 return -EIO;
3442 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3443 while (towrite > 0) {
3444 tocopy = sb->s_blocksize - offset < towrite ?
3445 sb->s_blocksize - offset : towrite;
3446 bh = ext4_bread(handle, inode, blk, 1, &err);
3447 if (!bh)
3448 goto out;
3449 if (journal_quota) {
3450 err = ext4_journal_get_write_access(handle, bh);
3451 if (err) {
3452 brelse(bh);
3453 goto out;
3456 lock_buffer(bh);
3457 memcpy(bh->b_data+offset, data, tocopy);
3458 flush_dcache_page(bh->b_page);
3459 unlock_buffer(bh);
3460 if (journal_quota)
3461 err = ext4_journal_dirty_metadata(handle, bh);
3462 else {
3463 /* Always do at least ordered writes for quotas */
3464 err = ext4_jbd2_file_inode(handle, inode);
3465 mark_buffer_dirty(bh);
3467 brelse(bh);
3468 if (err)
3469 goto out;
3470 offset = 0;
3471 towrite -= tocopy;
3472 data += tocopy;
3473 blk++;
3475 out:
3476 if (len == towrite) {
3477 mutex_unlock(&inode->i_mutex);
3478 return err;
3480 if (inode->i_size < off+len-towrite) {
3481 i_size_write(inode, off+len-towrite);
3482 EXT4_I(inode)->i_disksize = inode->i_size;
3484 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3485 ext4_mark_inode_dirty(handle, inode);
3486 mutex_unlock(&inode->i_mutex);
3487 return len - towrite;
3490 #endif
3492 static int ext4_get_sb(struct file_system_type *fs_type,
3493 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3495 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3498 #ifdef CONFIG_PROC_FS
3499 static int ext4_ui_proc_show(struct seq_file *m, void *v)
3501 unsigned int *p = m->private;
3503 seq_printf(m, "%u\n", *p);
3504 return 0;
3507 static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3509 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3512 static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3513 size_t cnt, loff_t *ppos)
3515 unsigned int *p = PDE(file->f_path.dentry->d_inode)->data;
3516 char str[32];
3517 unsigned long value;
3519 if (cnt >= sizeof(str))
3520 return -EINVAL;
3521 if (copy_from_user(str, buf, cnt))
3522 return -EFAULT;
3523 value = simple_strtol(str, NULL, 0);
3524 if (value < 0)
3525 return -ERANGE;
3526 *p = value;
3527 return cnt;
3530 const struct file_operations ext4_ui_proc_fops = {
3531 .owner = THIS_MODULE,
3532 .open = ext4_ui_proc_open,
3533 .read = seq_read,
3534 .llseek = seq_lseek,
3535 .release = single_release,
3536 .write = ext4_ui_proc_write,
3538 #endif
3540 static struct file_system_type ext4_fs_type = {
3541 .owner = THIS_MODULE,
3542 .name = "ext4",
3543 .get_sb = ext4_get_sb,
3544 .kill_sb = kill_block_super,
3545 .fs_flags = FS_REQUIRES_DEV,
3548 #ifdef CONFIG_EXT4DEV_COMPAT
3549 static int ext4dev_get_sb(struct file_system_type *fs_type,
3550 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3552 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3553 "to mount using ext4\n");
3554 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3555 "will go away by 2.6.31\n");
3556 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3559 static struct file_system_type ext4dev_fs_type = {
3560 .owner = THIS_MODULE,
3561 .name = "ext4dev",
3562 .get_sb = ext4dev_get_sb,
3563 .kill_sb = kill_block_super,
3564 .fs_flags = FS_REQUIRES_DEV,
3566 MODULE_ALIAS("ext4dev");
3567 #endif
3569 static int __init init_ext4_fs(void)
3571 int err;
3573 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
3574 err = init_ext4_mballoc();
3575 if (err)
3576 return err;
3578 err = init_ext4_xattr();
3579 if (err)
3580 goto out2;
3581 err = init_inodecache();
3582 if (err)
3583 goto out1;
3584 err = register_filesystem(&ext4_fs_type);
3585 if (err)
3586 goto out;
3587 #ifdef CONFIG_EXT4DEV_COMPAT
3588 err = register_filesystem(&ext4dev_fs_type);
3589 if (err) {
3590 unregister_filesystem(&ext4_fs_type);
3591 goto out;
3593 #endif
3594 return 0;
3595 out:
3596 destroy_inodecache();
3597 out1:
3598 exit_ext4_xattr();
3599 out2:
3600 exit_ext4_mballoc();
3601 return err;
3604 static void __exit exit_ext4_fs(void)
3606 unregister_filesystem(&ext4_fs_type);
3607 #ifdef CONFIG_EXT4DEV_COMPAT
3608 unregister_filesystem(&ext4dev_fs_type);
3609 #endif
3610 destroy_inodecache();
3611 exit_ext4_xattr();
3612 exit_ext4_mballoc();
3613 remove_proc_entry("fs/ext4", NULL);
3616 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3617 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3618 MODULE_LICENSE("GPL");
3619 module_init(init_ext4_fs)
3620 module_exit(exit_ext4_fs)