ACPI: Make Len Brown the ACPI maintainer again
[linux-2.6/next.git] / fs / ext4 / super.c
blobd5d77958b861b9fc29114965a566a7e2dfdd4a84
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/log2.h>
38 #include <linux/crc16.h>
39 #include <asm/uaccess.h>
41 #include "ext4.h"
42 #include "ext4_jbd2.h"
43 #include "xattr.h"
44 #include "acl.h"
45 #include "namei.h"
46 #include "group.h"
48 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
49 unsigned long journal_devnum);
50 static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
51 unsigned int);
52 static void ext4_commit_super(struct super_block *sb,
53 struct ext4_super_block *es, int sync);
54 static void ext4_mark_recovery_complete(struct super_block *sb,
55 struct ext4_super_block *es);
56 static void ext4_clear_journal_err(struct super_block *sb,
57 struct ext4_super_block *es);
58 static int ext4_sync_fs(struct super_block *sb, int wait);
59 static const char *ext4_decode_error(struct super_block *sb, int errno,
60 char nbuf[16]);
61 static int ext4_remount(struct super_block *sb, int *flags, char *data);
62 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
63 static void ext4_unlockfs(struct super_block *sb);
64 static void ext4_write_super(struct super_block *sb);
65 static void ext4_write_super_lockfs(struct super_block *sb);
68 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
69 struct ext4_group_desc *bg)
71 return le32_to_cpu(bg->bg_block_bitmap_lo) |
72 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
73 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
76 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
77 struct ext4_group_desc *bg)
79 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
80 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
81 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
84 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
85 struct ext4_group_desc *bg)
87 return le32_to_cpu(bg->bg_inode_table_lo) |
88 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
89 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
92 void ext4_block_bitmap_set(struct super_block *sb,
93 struct ext4_group_desc *bg, ext4_fsblk_t blk)
95 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
96 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
97 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
100 void ext4_inode_bitmap_set(struct super_block *sb,
101 struct ext4_group_desc *bg, ext4_fsblk_t blk)
103 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
104 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
105 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
108 void ext4_inode_table_set(struct super_block *sb,
109 struct ext4_group_desc *bg, ext4_fsblk_t blk)
111 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
112 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
113 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
117 * Wrappers for jbd2_journal_start/end.
119 * The only special thing we need to do here is to make sure that all
120 * journal_end calls result in the superblock being marked dirty, so
121 * that sync() will call the filesystem's write_super callback if
122 * appropriate.
124 handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
126 journal_t *journal;
128 if (sb->s_flags & MS_RDONLY)
129 return ERR_PTR(-EROFS);
131 /* Special case here: if the journal has aborted behind our
132 * backs (eg. EIO in the commit thread), then we still need to
133 * take the FS itself readonly cleanly. */
134 journal = EXT4_SB(sb)->s_journal;
135 if (is_journal_aborted(journal)) {
136 ext4_abort(sb, __func__,
137 "Detected aborted journal");
138 return ERR_PTR(-EROFS);
141 return jbd2_journal_start(journal, nblocks);
145 * The only special thing we need to do here is to make sure that all
146 * jbd2_journal_stop calls result in the superblock being marked dirty, so
147 * that sync() will call the filesystem's write_super callback if
148 * appropriate.
150 int __ext4_journal_stop(const char *where, handle_t *handle)
152 struct super_block *sb;
153 int err;
154 int rc;
156 sb = handle->h_transaction->t_journal->j_private;
157 err = handle->h_err;
158 rc = jbd2_journal_stop(handle);
160 if (!err)
161 err = rc;
162 if (err)
163 __ext4_std_error(sb, where, err);
164 return err;
167 void ext4_journal_abort_handle(const char *caller, const char *err_fn,
168 struct buffer_head *bh, handle_t *handle, int err)
170 char nbuf[16];
171 const char *errstr = ext4_decode_error(NULL, err, nbuf);
173 if (bh)
174 BUFFER_TRACE(bh, "abort");
176 if (!handle->h_err)
177 handle->h_err = err;
179 if (is_handle_aborted(handle))
180 return;
182 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
183 caller, errstr, err_fn);
185 jbd2_journal_abort_handle(handle);
188 /* Deal with the reporting of failure conditions on a filesystem such as
189 * inconsistencies detected or read IO failures.
191 * On ext2, we can store the error state of the filesystem in the
192 * superblock. That is not possible on ext4, because we may have other
193 * write ordering constraints on the superblock which prevent us from
194 * writing it out straight away; and given that the journal is about to
195 * be aborted, we can't rely on the current, or future, transactions to
196 * write out the superblock safely.
198 * We'll just use the jbd2_journal_abort() error code to record an error in
199 * the journal instead. On recovery, the journal will compain about
200 * that error until we've noted it down and cleared it.
203 static void ext4_handle_error(struct super_block *sb)
205 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
207 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
208 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
210 if (sb->s_flags & MS_RDONLY)
211 return;
213 if (!test_opt(sb, ERRORS_CONT)) {
214 journal_t *journal = EXT4_SB(sb)->s_journal;
216 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
217 if (journal)
218 jbd2_journal_abort(journal, -EIO);
220 if (test_opt(sb, ERRORS_RO)) {
221 printk(KERN_CRIT "Remounting filesystem read-only\n");
222 sb->s_flags |= MS_RDONLY;
224 ext4_commit_super(sb, es, 1);
225 if (test_opt(sb, ERRORS_PANIC))
226 panic("EXT4-fs (device %s): panic forced after error\n",
227 sb->s_id);
230 void ext4_error(struct super_block *sb, const char *function,
231 const char *fmt, ...)
233 va_list args;
235 va_start(args, fmt);
236 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
237 vprintk(fmt, args);
238 printk("\n");
239 va_end(args);
241 ext4_handle_error(sb);
244 static const char *ext4_decode_error(struct super_block *sb, int errno,
245 char nbuf[16])
247 char *errstr = NULL;
249 switch (errno) {
250 case -EIO:
251 errstr = "IO failure";
252 break;
253 case -ENOMEM:
254 errstr = "Out of memory";
255 break;
256 case -EROFS:
257 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
258 errstr = "Journal has aborted";
259 else
260 errstr = "Readonly filesystem";
261 break;
262 default:
263 /* If the caller passed in an extra buffer for unknown
264 * errors, textualise them now. Else we just return
265 * NULL. */
266 if (nbuf) {
267 /* Check for truncated error codes... */
268 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
269 errstr = nbuf;
271 break;
274 return errstr;
277 /* __ext4_std_error decodes expected errors from journaling functions
278 * automatically and invokes the appropriate error response. */
280 void __ext4_std_error(struct super_block *sb, const char *function, int errno)
282 char nbuf[16];
283 const char *errstr;
285 /* Special case: if the error is EROFS, and we're not already
286 * inside a transaction, then there's really no point in logging
287 * an error. */
288 if (errno == -EROFS && journal_current_handle() == NULL &&
289 (sb->s_flags & MS_RDONLY))
290 return;
292 errstr = ext4_decode_error(sb, errno, nbuf);
293 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
294 sb->s_id, function, errstr);
296 ext4_handle_error(sb);
300 * ext4_abort is a much stronger failure handler than ext4_error. The
301 * abort function may be used to deal with unrecoverable failures such
302 * as journal IO errors or ENOMEM at a critical moment in log management.
304 * We unconditionally force the filesystem into an ABORT|READONLY state,
305 * unless the error response on the fs has been set to panic in which
306 * case we take the easy way out and panic immediately.
309 void ext4_abort(struct super_block *sb, const char *function,
310 const char *fmt, ...)
312 va_list args;
314 printk(KERN_CRIT "ext4_abort called.\n");
316 va_start(args, fmt);
317 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
318 vprintk(fmt, args);
319 printk("\n");
320 va_end(args);
322 if (test_opt(sb, ERRORS_PANIC))
323 panic("EXT4-fs panic from previous error\n");
325 if (sb->s_flags & MS_RDONLY)
326 return;
328 printk(KERN_CRIT "Remounting filesystem read-only\n");
329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
330 sb->s_flags |= MS_RDONLY;
331 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
332 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
335 void ext4_warning(struct super_block *sb, const char *function,
336 const char *fmt, ...)
338 va_list args;
340 va_start(args, fmt);
341 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
342 sb->s_id, function);
343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
348 void ext4_update_dynamic_rev(struct super_block *sb)
350 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
352 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
353 return;
355 ext4_warning(sb, __func__,
356 "updating to rev %d because of new feature flag, "
357 "running e2fsck is recommended",
358 EXT4_DYNAMIC_REV);
360 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
361 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
362 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
363 /* leave es->s_feature_*compat flags alone */
364 /* es->s_uuid will be set by e2fsck if empty */
367 * The rest of the superblock fields should be zero, and if not it
368 * means they are likely already in use, so leave them alone. We
369 * can leave it up to e2fsck to clean up any inconsistencies there.
373 int ext4_update_compat_feature(handle_t *handle,
374 struct super_block *sb, __u32 compat)
376 int err = 0;
377 if (!EXT4_HAS_COMPAT_FEATURE(sb, compat)) {
378 err = ext4_journal_get_write_access(handle,
379 EXT4_SB(sb)->s_sbh);
380 if (err)
381 return err;
382 EXT4_SET_COMPAT_FEATURE(sb, compat);
383 sb->s_dirt = 1;
384 handle->h_sync = 1;
385 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
386 "call ext4_journal_dirty_met adata");
387 err = ext4_journal_dirty_metadata(handle,
388 EXT4_SB(sb)->s_sbh);
390 return err;
393 int ext4_update_rocompat_feature(handle_t *handle,
394 struct super_block *sb, __u32 rocompat)
396 int err = 0;
397 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, rocompat)) {
398 err = ext4_journal_get_write_access(handle,
399 EXT4_SB(sb)->s_sbh);
400 if (err)
401 return err;
402 EXT4_SET_RO_COMPAT_FEATURE(sb, rocompat);
403 sb->s_dirt = 1;
404 handle->h_sync = 1;
405 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
406 "call ext4_journal_dirty_met adata");
407 err = ext4_journal_dirty_metadata(handle,
408 EXT4_SB(sb)->s_sbh);
410 return err;
413 int ext4_update_incompat_feature(handle_t *handle,
414 struct super_block *sb, __u32 incompat)
416 int err = 0;
417 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, incompat)) {
418 err = ext4_journal_get_write_access(handle,
419 EXT4_SB(sb)->s_sbh);
420 if (err)
421 return err;
422 EXT4_SET_INCOMPAT_FEATURE(sb, incompat);
423 sb->s_dirt = 1;
424 handle->h_sync = 1;
425 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
426 "call ext4_journal_dirty_met adata");
427 err = ext4_journal_dirty_metadata(handle,
428 EXT4_SB(sb)->s_sbh);
430 return err;
434 * Open the external journal device
436 static struct block_device *ext4_blkdev_get(dev_t dev)
438 struct block_device *bdev;
439 char b[BDEVNAME_SIZE];
441 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
442 if (IS_ERR(bdev))
443 goto fail;
444 return bdev;
446 fail:
447 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
448 __bdevname(dev, b), PTR_ERR(bdev));
449 return NULL;
453 * Release the journal device
455 static int ext4_blkdev_put(struct block_device *bdev)
457 bd_release(bdev);
458 return blkdev_put(bdev);
461 static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
463 struct block_device *bdev;
464 int ret = -ENODEV;
466 bdev = sbi->journal_bdev;
467 if (bdev) {
468 ret = ext4_blkdev_put(bdev);
469 sbi->journal_bdev = NULL;
471 return ret;
474 static inline struct inode *orphan_list_entry(struct list_head *l)
476 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
479 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
481 struct list_head *l;
483 printk(KERN_ERR "sb orphan head is %d\n",
484 le32_to_cpu(sbi->s_es->s_last_orphan));
486 printk(KERN_ERR "sb_info orphan list:\n");
487 list_for_each(l, &sbi->s_orphan) {
488 struct inode *inode = orphan_list_entry(l);
489 printk(KERN_ERR " "
490 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
491 inode->i_sb->s_id, inode->i_ino, inode,
492 inode->i_mode, inode->i_nlink,
493 NEXT_ORPHAN(inode));
497 static void ext4_put_super(struct super_block *sb)
499 struct ext4_sb_info *sbi = EXT4_SB(sb);
500 struct ext4_super_block *es = sbi->s_es;
501 int i;
503 ext4_mb_release(sb);
504 ext4_ext_release(sb);
505 ext4_xattr_put_super(sb);
506 jbd2_journal_destroy(sbi->s_journal);
507 sbi->s_journal = NULL;
508 if (!(sb->s_flags & MS_RDONLY)) {
509 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
510 es->s_state = cpu_to_le16(sbi->s_mount_state);
511 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
512 mark_buffer_dirty(sbi->s_sbh);
513 ext4_commit_super(sb, es, 1);
516 for (i = 0; i < sbi->s_gdb_count; i++)
517 brelse(sbi->s_group_desc[i]);
518 kfree(sbi->s_group_desc);
519 kfree(sbi->s_flex_groups);
520 percpu_counter_destroy(&sbi->s_freeblocks_counter);
521 percpu_counter_destroy(&sbi->s_freeinodes_counter);
522 percpu_counter_destroy(&sbi->s_dirs_counter);
523 brelse(sbi->s_sbh);
524 #ifdef CONFIG_QUOTA
525 for (i = 0; i < MAXQUOTAS; i++)
526 kfree(sbi->s_qf_names[i]);
527 #endif
529 /* Debugging code just in case the in-memory inode orphan list
530 * isn't empty. The on-disk one can be non-empty if we've
531 * detected an error and taken the fs readonly, but the
532 * in-memory list had better be clean by this point. */
533 if (!list_empty(&sbi->s_orphan))
534 dump_orphan_list(sb, sbi);
535 J_ASSERT(list_empty(&sbi->s_orphan));
537 invalidate_bdev(sb->s_bdev);
538 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
540 * Invalidate the journal device's buffers. We don't want them
541 * floating about in memory - the physical journal device may
542 * hotswapped, and it breaks the `ro-after' testing code.
544 sync_blockdev(sbi->journal_bdev);
545 invalidate_bdev(sbi->journal_bdev);
546 ext4_blkdev_remove(sbi);
548 sb->s_fs_info = NULL;
549 kfree(sbi);
550 return;
553 static struct kmem_cache *ext4_inode_cachep;
556 * Called inside transaction, so use GFP_NOFS
558 static struct inode *ext4_alloc_inode(struct super_block *sb)
560 struct ext4_inode_info *ei;
562 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
563 if (!ei)
564 return NULL;
565 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
566 ei->i_acl = EXT4_ACL_NOT_CACHED;
567 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
568 #endif
569 ei->i_block_alloc_info = NULL;
570 ei->vfs_inode.i_version = 1;
571 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
572 INIT_LIST_HEAD(&ei->i_prealloc_list);
573 spin_lock_init(&ei->i_prealloc_lock);
574 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
575 ei->i_reserved_data_blocks = 0;
576 ei->i_reserved_meta_blocks = 0;
577 ei->i_allocated_meta_blocks = 0;
578 ei->i_delalloc_reserved_flag = 0;
579 spin_lock_init(&(ei->i_block_reservation_lock));
580 return &ei->vfs_inode;
583 static void ext4_destroy_inode(struct inode *inode)
585 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
586 printk("EXT4 Inode %p: orphan list check failed!\n",
587 EXT4_I(inode));
588 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
589 EXT4_I(inode), sizeof(struct ext4_inode_info),
590 true);
591 dump_stack();
593 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
596 static void init_once(void *foo)
598 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
600 INIT_LIST_HEAD(&ei->i_orphan);
601 #ifdef CONFIG_EXT4DEV_FS_XATTR
602 init_rwsem(&ei->xattr_sem);
603 #endif
604 init_rwsem(&ei->i_data_sem);
605 inode_init_once(&ei->vfs_inode);
608 static int init_inodecache(void)
610 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
611 sizeof(struct ext4_inode_info),
612 0, (SLAB_RECLAIM_ACCOUNT|
613 SLAB_MEM_SPREAD),
614 init_once);
615 if (ext4_inode_cachep == NULL)
616 return -ENOMEM;
617 return 0;
620 static void destroy_inodecache(void)
622 kmem_cache_destroy(ext4_inode_cachep);
625 static void ext4_clear_inode(struct inode *inode)
627 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
628 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
629 if (EXT4_I(inode)->i_acl &&
630 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
631 posix_acl_release(EXT4_I(inode)->i_acl);
632 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
634 if (EXT4_I(inode)->i_default_acl &&
635 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
636 posix_acl_release(EXT4_I(inode)->i_default_acl);
637 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
639 #endif
640 ext4_discard_reservation(inode);
641 EXT4_I(inode)->i_block_alloc_info = NULL;
642 if (unlikely(rsv))
643 kfree(rsv);
644 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
645 &EXT4_I(inode)->jinode);
648 static inline void ext4_show_quota_options(struct seq_file *seq,
649 struct super_block *sb)
651 #if defined(CONFIG_QUOTA)
652 struct ext4_sb_info *sbi = EXT4_SB(sb);
654 if (sbi->s_jquota_fmt)
655 seq_printf(seq, ",jqfmt=%s",
656 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
658 if (sbi->s_qf_names[USRQUOTA])
659 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
661 if (sbi->s_qf_names[GRPQUOTA])
662 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
664 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
665 seq_puts(seq, ",usrquota");
667 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
668 seq_puts(seq, ",grpquota");
669 #endif
673 * Show an option if
674 * - it's set to a non-default value OR
675 * - if the per-sb default is different from the global default
677 static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
679 int def_errors;
680 unsigned long def_mount_opts;
681 struct super_block *sb = vfs->mnt_sb;
682 struct ext4_sb_info *sbi = EXT4_SB(sb);
683 struct ext4_super_block *es = sbi->s_es;
685 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
686 def_errors = le16_to_cpu(es->s_errors);
688 if (sbi->s_sb_block != 1)
689 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
690 if (test_opt(sb, MINIX_DF))
691 seq_puts(seq, ",minixdf");
692 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
693 seq_puts(seq, ",grpid");
694 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
695 seq_puts(seq, ",nogrpid");
696 if (sbi->s_resuid != EXT4_DEF_RESUID ||
697 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
698 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
700 if (sbi->s_resgid != EXT4_DEF_RESGID ||
701 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
702 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
704 if (test_opt(sb, ERRORS_RO)) {
705 if (def_errors == EXT4_ERRORS_PANIC ||
706 def_errors == EXT4_ERRORS_CONTINUE) {
707 seq_puts(seq, ",errors=remount-ro");
710 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
711 seq_puts(seq, ",errors=continue");
712 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
713 seq_puts(seq, ",errors=panic");
714 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
715 seq_puts(seq, ",nouid32");
716 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
717 seq_puts(seq, ",debug");
718 if (test_opt(sb, OLDALLOC))
719 seq_puts(seq, ",oldalloc");
720 #ifdef CONFIG_EXT4DEV_FS_XATTR
721 if (test_opt(sb, XATTR_USER) &&
722 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
723 seq_puts(seq, ",user_xattr");
724 if (!test_opt(sb, XATTR_USER) &&
725 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
726 seq_puts(seq, ",nouser_xattr");
728 #endif
729 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
730 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
731 seq_puts(seq, ",acl");
732 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
733 seq_puts(seq, ",noacl");
734 #endif
735 if (!test_opt(sb, RESERVATION))
736 seq_puts(seq, ",noreservation");
737 if (sbi->s_commit_interval) {
738 seq_printf(seq, ",commit=%u",
739 (unsigned) (sbi->s_commit_interval / HZ));
742 * We're changing the default of barrier mount option, so
743 * let's always display its mount state so it's clear what its
744 * status is.
746 seq_puts(seq, ",barrier=");
747 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
748 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
749 seq_puts(seq, ",journal_async_commit");
750 if (test_opt(sb, NOBH))
751 seq_puts(seq, ",nobh");
752 if (!test_opt(sb, EXTENTS))
753 seq_puts(seq, ",noextents");
754 if (!test_opt(sb, MBALLOC))
755 seq_puts(seq, ",nomballoc");
756 if (test_opt(sb, I_VERSION))
757 seq_puts(seq, ",i_version");
758 if (!test_opt(sb, DELALLOC))
759 seq_puts(seq, ",nodelalloc");
762 if (sbi->s_stripe)
763 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
765 * journal mode get enabled in different ways
766 * So just print the value even if we didn't specify it
768 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
769 seq_puts(seq, ",data=journal");
770 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
771 seq_puts(seq, ",data=ordered");
772 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
773 seq_puts(seq, ",data=writeback");
775 ext4_show_quota_options(seq, sb);
776 return 0;
780 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
781 u64 ino, u32 generation)
783 struct inode *inode;
785 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
786 return ERR_PTR(-ESTALE);
787 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
788 return ERR_PTR(-ESTALE);
790 /* iget isn't really right if the inode is currently unallocated!!
792 * ext4_read_inode will return a bad_inode if the inode had been
793 * deleted, so we should be safe.
795 * Currently we don't know the generation for parent directory, so
796 * a generation of 0 means "accept any"
798 inode = ext4_iget(sb, ino);
799 if (IS_ERR(inode))
800 return ERR_CAST(inode);
801 if (generation && inode->i_generation != generation) {
802 iput(inode);
803 return ERR_PTR(-ESTALE);
806 return inode;
809 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
810 int fh_len, int fh_type)
812 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
813 ext4_nfs_get_inode);
816 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
817 int fh_len, int fh_type)
819 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
820 ext4_nfs_get_inode);
823 #ifdef CONFIG_QUOTA
824 #define QTYPE2NAME(t) ((t) == USRQUOTA?"user":"group")
825 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
827 static int ext4_dquot_initialize(struct inode *inode, int type);
828 static int ext4_dquot_drop(struct inode *inode);
829 static int ext4_write_dquot(struct dquot *dquot);
830 static int ext4_acquire_dquot(struct dquot *dquot);
831 static int ext4_release_dquot(struct dquot *dquot);
832 static int ext4_mark_dquot_dirty(struct dquot *dquot);
833 static int ext4_write_info(struct super_block *sb, int type);
834 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
835 char *path, int remount);
836 static int ext4_quota_on_mount(struct super_block *sb, int type);
837 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
838 size_t len, loff_t off);
839 static ssize_t ext4_quota_write(struct super_block *sb, int type,
840 const char *data, size_t len, loff_t off);
842 static struct dquot_operations ext4_quota_operations = {
843 .initialize = ext4_dquot_initialize,
844 .drop = ext4_dquot_drop,
845 .alloc_space = dquot_alloc_space,
846 .alloc_inode = dquot_alloc_inode,
847 .free_space = dquot_free_space,
848 .free_inode = dquot_free_inode,
849 .transfer = dquot_transfer,
850 .write_dquot = ext4_write_dquot,
851 .acquire_dquot = ext4_acquire_dquot,
852 .release_dquot = ext4_release_dquot,
853 .mark_dirty = ext4_mark_dquot_dirty,
854 .write_info = ext4_write_info
857 static struct quotactl_ops ext4_qctl_operations = {
858 .quota_on = ext4_quota_on,
859 .quota_off = vfs_quota_off,
860 .quota_sync = vfs_quota_sync,
861 .get_info = vfs_get_dqinfo,
862 .set_info = vfs_set_dqinfo,
863 .get_dqblk = vfs_get_dqblk,
864 .set_dqblk = vfs_set_dqblk
866 #endif
868 static const struct super_operations ext4_sops = {
869 .alloc_inode = ext4_alloc_inode,
870 .destroy_inode = ext4_destroy_inode,
871 .write_inode = ext4_write_inode,
872 .dirty_inode = ext4_dirty_inode,
873 .delete_inode = ext4_delete_inode,
874 .put_super = ext4_put_super,
875 .write_super = ext4_write_super,
876 .sync_fs = ext4_sync_fs,
877 .write_super_lockfs = ext4_write_super_lockfs,
878 .unlockfs = ext4_unlockfs,
879 .statfs = ext4_statfs,
880 .remount_fs = ext4_remount,
881 .clear_inode = ext4_clear_inode,
882 .show_options = ext4_show_options,
883 #ifdef CONFIG_QUOTA
884 .quota_read = ext4_quota_read,
885 .quota_write = ext4_quota_write,
886 #endif
889 static const struct export_operations ext4_export_ops = {
890 .fh_to_dentry = ext4_fh_to_dentry,
891 .fh_to_parent = ext4_fh_to_parent,
892 .get_parent = ext4_get_parent,
895 enum {
896 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
897 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
898 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
899 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
900 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
901 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
902 Opt_journal_checksum, Opt_journal_async_commit,
903 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
904 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
905 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
906 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
907 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
908 Opt_mballoc, Opt_nomballoc, Opt_stripe, Opt_delalloc, Opt_nodelalloc,
911 static match_table_t tokens = {
912 {Opt_bsd_df, "bsddf"},
913 {Opt_minix_df, "minixdf"},
914 {Opt_grpid, "grpid"},
915 {Opt_grpid, "bsdgroups"},
916 {Opt_nogrpid, "nogrpid"},
917 {Opt_nogrpid, "sysvgroups"},
918 {Opt_resgid, "resgid=%u"},
919 {Opt_resuid, "resuid=%u"},
920 {Opt_sb, "sb=%u"},
921 {Opt_err_cont, "errors=continue"},
922 {Opt_err_panic, "errors=panic"},
923 {Opt_err_ro, "errors=remount-ro"},
924 {Opt_nouid32, "nouid32"},
925 {Opt_nocheck, "nocheck"},
926 {Opt_nocheck, "check=none"},
927 {Opt_debug, "debug"},
928 {Opt_oldalloc, "oldalloc"},
929 {Opt_orlov, "orlov"},
930 {Opt_user_xattr, "user_xattr"},
931 {Opt_nouser_xattr, "nouser_xattr"},
932 {Opt_acl, "acl"},
933 {Opt_noacl, "noacl"},
934 {Opt_reservation, "reservation"},
935 {Opt_noreservation, "noreservation"},
936 {Opt_noload, "noload"},
937 {Opt_nobh, "nobh"},
938 {Opt_bh, "bh"},
939 {Opt_commit, "commit=%u"},
940 {Opt_journal_update, "journal=update"},
941 {Opt_journal_inum, "journal=%u"},
942 {Opt_journal_dev, "journal_dev=%u"},
943 {Opt_journal_checksum, "journal_checksum"},
944 {Opt_journal_async_commit, "journal_async_commit"},
945 {Opt_abort, "abort"},
946 {Opt_data_journal, "data=journal"},
947 {Opt_data_ordered, "data=ordered"},
948 {Opt_data_writeback, "data=writeback"},
949 {Opt_offusrjquota, "usrjquota="},
950 {Opt_usrjquota, "usrjquota=%s"},
951 {Opt_offgrpjquota, "grpjquota="},
952 {Opt_grpjquota, "grpjquota=%s"},
953 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
954 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
955 {Opt_grpquota, "grpquota"},
956 {Opt_noquota, "noquota"},
957 {Opt_quota, "quota"},
958 {Opt_usrquota, "usrquota"},
959 {Opt_barrier, "barrier=%u"},
960 {Opt_extents, "extents"},
961 {Opt_noextents, "noextents"},
962 {Opt_i_version, "i_version"},
963 {Opt_mballoc, "mballoc"},
964 {Opt_nomballoc, "nomballoc"},
965 {Opt_stripe, "stripe=%u"},
966 {Opt_resize, "resize"},
967 {Opt_delalloc, "delalloc"},
968 {Opt_nodelalloc, "nodelalloc"},
969 {Opt_err, NULL},
972 static ext4_fsblk_t get_sb_block(void **data)
974 ext4_fsblk_t sb_block;
975 char *options = (char *) *data;
977 if (!options || strncmp(options, "sb=", 3) != 0)
978 return 1; /* Default location */
979 options += 3;
980 /*todo: use simple_strtoll with >32bit ext4 */
981 sb_block = simple_strtoul(options, &options, 0);
982 if (*options && *options != ',') {
983 printk("EXT4-fs: Invalid sb specification: %s\n",
984 (char *) *data);
985 return 1;
987 if (*options == ',')
988 options++;
989 *data = (void *) options;
990 return sb_block;
993 static int parse_options(char *options, struct super_block *sb,
994 unsigned int *inum, unsigned long *journal_devnum,
995 ext4_fsblk_t *n_blocks_count, int is_remount)
997 struct ext4_sb_info *sbi = EXT4_SB(sb);
998 char *p;
999 substring_t args[MAX_OPT_ARGS];
1000 int data_opt = 0;
1001 int option;
1002 #ifdef CONFIG_QUOTA
1003 int qtype, qfmt;
1004 char *qname;
1005 #endif
1006 ext4_fsblk_t last_block;
1008 if (!options)
1009 return 1;
1011 while ((p = strsep(&options, ",")) != NULL) {
1012 int token;
1013 if (!*p)
1014 continue;
1016 token = match_token(p, tokens, args);
1017 switch (token) {
1018 case Opt_bsd_df:
1019 clear_opt(sbi->s_mount_opt, MINIX_DF);
1020 break;
1021 case Opt_minix_df:
1022 set_opt(sbi->s_mount_opt, MINIX_DF);
1023 break;
1024 case Opt_grpid:
1025 set_opt(sbi->s_mount_opt, GRPID);
1026 break;
1027 case Opt_nogrpid:
1028 clear_opt(sbi->s_mount_opt, GRPID);
1029 break;
1030 case Opt_resuid:
1031 if (match_int(&args[0], &option))
1032 return 0;
1033 sbi->s_resuid = option;
1034 break;
1035 case Opt_resgid:
1036 if (match_int(&args[0], &option))
1037 return 0;
1038 sbi->s_resgid = option;
1039 break;
1040 case Opt_sb:
1041 /* handled by get_sb_block() instead of here */
1042 /* *sb_block = match_int(&args[0]); */
1043 break;
1044 case Opt_err_panic:
1045 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1046 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1047 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1048 break;
1049 case Opt_err_ro:
1050 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1051 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1052 set_opt(sbi->s_mount_opt, ERRORS_RO);
1053 break;
1054 case Opt_err_cont:
1055 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1056 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1057 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1058 break;
1059 case Opt_nouid32:
1060 set_opt(sbi->s_mount_opt, NO_UID32);
1061 break;
1062 case Opt_nocheck:
1063 clear_opt(sbi->s_mount_opt, CHECK);
1064 break;
1065 case Opt_debug:
1066 set_opt(sbi->s_mount_opt, DEBUG);
1067 break;
1068 case Opt_oldalloc:
1069 set_opt(sbi->s_mount_opt, OLDALLOC);
1070 break;
1071 case Opt_orlov:
1072 clear_opt(sbi->s_mount_opt, OLDALLOC);
1073 break;
1074 #ifdef CONFIG_EXT4DEV_FS_XATTR
1075 case Opt_user_xattr:
1076 set_opt(sbi->s_mount_opt, XATTR_USER);
1077 break;
1078 case Opt_nouser_xattr:
1079 clear_opt(sbi->s_mount_opt, XATTR_USER);
1080 break;
1081 #else
1082 case Opt_user_xattr:
1083 case Opt_nouser_xattr:
1084 printk("EXT4 (no)user_xattr options not supported\n");
1085 break;
1086 #endif
1087 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1088 case Opt_acl:
1089 set_opt(sbi->s_mount_opt, POSIX_ACL);
1090 break;
1091 case Opt_noacl:
1092 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1093 break;
1094 #else
1095 case Opt_acl:
1096 case Opt_noacl:
1097 printk("EXT4 (no)acl options not supported\n");
1098 break;
1099 #endif
1100 case Opt_reservation:
1101 set_opt(sbi->s_mount_opt, RESERVATION);
1102 break;
1103 case Opt_noreservation:
1104 clear_opt(sbi->s_mount_opt, RESERVATION);
1105 break;
1106 case Opt_journal_update:
1107 /* @@@ FIXME */
1108 /* Eventually we will want to be able to create
1109 a journal file here. For now, only allow the
1110 user to specify an existing inode to be the
1111 journal file. */
1112 if (is_remount) {
1113 printk(KERN_ERR "EXT4-fs: cannot specify "
1114 "journal on remount\n");
1115 return 0;
1117 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
1118 break;
1119 case Opt_journal_inum:
1120 if (is_remount) {
1121 printk(KERN_ERR "EXT4-fs: cannot specify "
1122 "journal on remount\n");
1123 return 0;
1125 if (match_int(&args[0], &option))
1126 return 0;
1127 *inum = option;
1128 break;
1129 case Opt_journal_dev:
1130 if (is_remount) {
1131 printk(KERN_ERR "EXT4-fs: cannot specify "
1132 "journal on remount\n");
1133 return 0;
1135 if (match_int(&args[0], &option))
1136 return 0;
1137 *journal_devnum = option;
1138 break;
1139 case Opt_journal_checksum:
1140 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1141 break;
1142 case Opt_journal_async_commit:
1143 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1144 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1145 break;
1146 case Opt_noload:
1147 set_opt(sbi->s_mount_opt, NOLOAD);
1148 break;
1149 case Opt_commit:
1150 if (match_int(&args[0], &option))
1151 return 0;
1152 if (option < 0)
1153 return 0;
1154 if (option == 0)
1155 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
1156 sbi->s_commit_interval = HZ * option;
1157 break;
1158 case Opt_data_journal:
1159 data_opt = EXT4_MOUNT_JOURNAL_DATA;
1160 goto datacheck;
1161 case Opt_data_ordered:
1162 data_opt = EXT4_MOUNT_ORDERED_DATA;
1163 goto datacheck;
1164 case Opt_data_writeback:
1165 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
1166 datacheck:
1167 if (is_remount) {
1168 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
1169 != data_opt) {
1170 printk(KERN_ERR
1171 "EXT4-fs: cannot change data "
1172 "mode on remount\n");
1173 return 0;
1175 } else {
1176 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
1177 sbi->s_mount_opt |= data_opt;
1179 break;
1180 #ifdef CONFIG_QUOTA
1181 case Opt_usrjquota:
1182 qtype = USRQUOTA;
1183 goto set_qf_name;
1184 case Opt_grpjquota:
1185 qtype = GRPQUOTA;
1186 set_qf_name:
1187 if ((sb_any_quota_enabled(sb) ||
1188 sb_any_quota_suspended(sb)) &&
1189 !sbi->s_qf_names[qtype]) {
1190 printk(KERN_ERR
1191 "EXT4-fs: Cannot change journaled "
1192 "quota options when quota turned on.\n");
1193 return 0;
1195 qname = match_strdup(&args[0]);
1196 if (!qname) {
1197 printk(KERN_ERR
1198 "EXT4-fs: not enough memory for "
1199 "storing quotafile name.\n");
1200 return 0;
1202 if (sbi->s_qf_names[qtype] &&
1203 strcmp(sbi->s_qf_names[qtype], qname)) {
1204 printk(KERN_ERR
1205 "EXT4-fs: %s quota file already "
1206 "specified.\n", QTYPE2NAME(qtype));
1207 kfree(qname);
1208 return 0;
1210 sbi->s_qf_names[qtype] = qname;
1211 if (strchr(sbi->s_qf_names[qtype], '/')) {
1212 printk(KERN_ERR
1213 "EXT4-fs: quotafile must be on "
1214 "filesystem root.\n");
1215 kfree(sbi->s_qf_names[qtype]);
1216 sbi->s_qf_names[qtype] = NULL;
1217 return 0;
1219 set_opt(sbi->s_mount_opt, QUOTA);
1220 break;
1221 case Opt_offusrjquota:
1222 qtype = USRQUOTA;
1223 goto clear_qf_name;
1224 case Opt_offgrpjquota:
1225 qtype = GRPQUOTA;
1226 clear_qf_name:
1227 if ((sb_any_quota_enabled(sb) ||
1228 sb_any_quota_suspended(sb)) &&
1229 sbi->s_qf_names[qtype]) {
1230 printk(KERN_ERR "EXT4-fs: Cannot change "
1231 "journaled quota options when "
1232 "quota turned on.\n");
1233 return 0;
1236 * The space will be released later when all options
1237 * are confirmed to be correct
1239 sbi->s_qf_names[qtype] = NULL;
1240 break;
1241 case Opt_jqfmt_vfsold:
1242 qfmt = QFMT_VFS_OLD;
1243 goto set_qf_format;
1244 case Opt_jqfmt_vfsv0:
1245 qfmt = QFMT_VFS_V0;
1246 set_qf_format:
1247 if ((sb_any_quota_enabled(sb) ||
1248 sb_any_quota_suspended(sb)) &&
1249 sbi->s_jquota_fmt != qfmt) {
1250 printk(KERN_ERR "EXT4-fs: Cannot change "
1251 "journaled quota options when "
1252 "quota turned on.\n");
1253 return 0;
1255 sbi->s_jquota_fmt = qfmt;
1256 break;
1257 case Opt_quota:
1258 case Opt_usrquota:
1259 set_opt(sbi->s_mount_opt, QUOTA);
1260 set_opt(sbi->s_mount_opt, USRQUOTA);
1261 break;
1262 case Opt_grpquota:
1263 set_opt(sbi->s_mount_opt, QUOTA);
1264 set_opt(sbi->s_mount_opt, GRPQUOTA);
1265 break;
1266 case Opt_noquota:
1267 if (sb_any_quota_enabled(sb)) {
1268 printk(KERN_ERR "EXT4-fs: Cannot change quota "
1269 "options when quota turned on.\n");
1270 return 0;
1272 clear_opt(sbi->s_mount_opt, QUOTA);
1273 clear_opt(sbi->s_mount_opt, USRQUOTA);
1274 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1275 break;
1276 #else
1277 case Opt_quota:
1278 case Opt_usrquota:
1279 case Opt_grpquota:
1280 printk(KERN_ERR
1281 "EXT4-fs: quota options not supported.\n");
1282 break;
1283 case Opt_usrjquota:
1284 case Opt_grpjquota:
1285 case Opt_offusrjquota:
1286 case Opt_offgrpjquota:
1287 case Opt_jqfmt_vfsold:
1288 case Opt_jqfmt_vfsv0:
1289 printk(KERN_ERR
1290 "EXT4-fs: journaled quota options not "
1291 "supported.\n");
1292 break;
1293 case Opt_noquota:
1294 break;
1295 #endif
1296 case Opt_abort:
1297 set_opt(sbi->s_mount_opt, ABORT);
1298 break;
1299 case Opt_barrier:
1300 if (match_int(&args[0], &option))
1301 return 0;
1302 if (option)
1303 set_opt(sbi->s_mount_opt, BARRIER);
1304 else
1305 clear_opt(sbi->s_mount_opt, BARRIER);
1306 break;
1307 case Opt_ignore:
1308 break;
1309 case Opt_resize:
1310 if (!is_remount) {
1311 printk("EXT4-fs: resize option only available "
1312 "for remount\n");
1313 return 0;
1315 if (match_int(&args[0], &option) != 0)
1316 return 0;
1317 *n_blocks_count = option;
1318 break;
1319 case Opt_nobh:
1320 set_opt(sbi->s_mount_opt, NOBH);
1321 break;
1322 case Opt_bh:
1323 clear_opt(sbi->s_mount_opt, NOBH);
1324 break;
1325 case Opt_extents:
1326 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1327 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1328 ext4_warning(sb, __func__,
1329 "extents feature not enabled "
1330 "on this filesystem, use tune2fs\n");
1331 return 0;
1333 set_opt(sbi->s_mount_opt, EXTENTS);
1334 break;
1335 case Opt_noextents:
1337 * When e2fsprogs support resizing an already existing
1338 * ext3 file system to greater than 2**32 we need to
1339 * add support to block allocator to handle growing
1340 * already existing block mapped inode so that blocks
1341 * allocated for them fall within 2**32
1343 last_block = ext4_blocks_count(sbi->s_es) - 1;
1344 if (last_block > 0xffffffffULL) {
1345 printk(KERN_ERR "EXT4-fs: Filesystem too "
1346 "large to mount with "
1347 "-o noextents options\n");
1348 return 0;
1350 clear_opt(sbi->s_mount_opt, EXTENTS);
1351 break;
1352 case Opt_i_version:
1353 set_opt(sbi->s_mount_opt, I_VERSION);
1354 sb->s_flags |= MS_I_VERSION;
1355 break;
1356 case Opt_nodelalloc:
1357 clear_opt(sbi->s_mount_opt, DELALLOC);
1358 break;
1359 case Opt_mballoc:
1360 set_opt(sbi->s_mount_opt, MBALLOC);
1361 break;
1362 case Opt_nomballoc:
1363 clear_opt(sbi->s_mount_opt, MBALLOC);
1364 break;
1365 case Opt_stripe:
1366 if (match_int(&args[0], &option))
1367 return 0;
1368 if (option < 0)
1369 return 0;
1370 sbi->s_stripe = option;
1371 break;
1372 case Opt_delalloc:
1373 set_opt(sbi->s_mount_opt, DELALLOC);
1374 break;
1375 default:
1376 printk(KERN_ERR
1377 "EXT4-fs: Unrecognized mount option \"%s\" "
1378 "or missing value\n", p);
1379 return 0;
1382 #ifdef CONFIG_QUOTA
1383 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1384 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
1385 sbi->s_qf_names[USRQUOTA])
1386 clear_opt(sbi->s_mount_opt, USRQUOTA);
1388 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
1389 sbi->s_qf_names[GRPQUOTA])
1390 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1392 if ((sbi->s_qf_names[USRQUOTA] &&
1393 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
1394 (sbi->s_qf_names[GRPQUOTA] &&
1395 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1396 printk(KERN_ERR "EXT4-fs: old and new quota "
1397 "format mixing.\n");
1398 return 0;
1401 if (!sbi->s_jquota_fmt) {
1402 printk(KERN_ERR "EXT4-fs: journaled quota format "
1403 "not specified.\n");
1404 return 0;
1406 } else {
1407 if (sbi->s_jquota_fmt) {
1408 printk(KERN_ERR "EXT4-fs: journaled quota format "
1409 "specified with no journaling "
1410 "enabled.\n");
1411 return 0;
1414 #endif
1415 return 1;
1418 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1419 int read_only)
1421 struct ext4_sb_info *sbi = EXT4_SB(sb);
1422 int res = 0;
1424 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1425 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1426 "forcing read-only mode\n");
1427 res = MS_RDONLY;
1429 if (read_only)
1430 return res;
1431 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1432 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1433 "running e2fsck is recommended\n");
1434 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
1435 printk(KERN_WARNING
1436 "EXT4-fs warning: mounting fs with errors, "
1437 "running e2fsck is recommended\n");
1438 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1439 le16_to_cpu(es->s_mnt_count) >=
1440 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1441 printk(KERN_WARNING
1442 "EXT4-fs warning: maximal mount count reached, "
1443 "running e2fsck is recommended\n");
1444 else if (le32_to_cpu(es->s_checkinterval) &&
1445 (le32_to_cpu(es->s_lastcheck) +
1446 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1447 printk(KERN_WARNING
1448 "EXT4-fs warning: checktime reached, "
1449 "running e2fsck is recommended\n");
1450 #if 0
1451 /* @@@ We _will_ want to clear the valid bit if we find
1452 * inconsistencies, to force a fsck at reboot. But for
1453 * a plain journaled filesystem we can keep it set as
1454 * valid forever! :)
1456 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1457 #endif
1458 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1459 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1460 le16_add_cpu(&es->s_mnt_count, 1);
1461 es->s_mtime = cpu_to_le32(get_seconds());
1462 ext4_update_dynamic_rev(sb);
1463 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1465 ext4_commit_super(sb, es, 1);
1466 if (test_opt(sb, DEBUG))
1467 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
1468 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1469 sb->s_blocksize,
1470 sbi->s_groups_count,
1471 EXT4_BLOCKS_PER_GROUP(sb),
1472 EXT4_INODES_PER_GROUP(sb),
1473 sbi->s_mount_opt);
1475 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1476 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
1477 char b[BDEVNAME_SIZE];
1479 printk("external journal on %s\n",
1480 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
1481 } else {
1482 printk("internal journal\n");
1484 return res;
1487 static int ext4_fill_flex_info(struct super_block *sb)
1489 struct ext4_sb_info *sbi = EXT4_SB(sb);
1490 struct ext4_group_desc *gdp = NULL;
1491 struct buffer_head *bh;
1492 ext4_group_t flex_group_count;
1493 ext4_group_t flex_group;
1494 int groups_per_flex = 0;
1495 __u64 block_bitmap = 0;
1496 int i;
1498 if (!sbi->s_es->s_log_groups_per_flex) {
1499 sbi->s_log_groups_per_flex = 0;
1500 return 1;
1503 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1504 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1506 flex_group_count = (sbi->s_groups_count + groups_per_flex - 1) /
1507 groups_per_flex;
1508 sbi->s_flex_groups = kzalloc(flex_group_count *
1509 sizeof(struct flex_groups), GFP_KERNEL);
1510 if (sbi->s_flex_groups == NULL) {
1511 printk(KERN_ERR "EXT4-fs: not enough memory for "
1512 "%lu flex groups\n", flex_group_count);
1513 goto failed;
1516 gdp = ext4_get_group_desc(sb, 1, &bh);
1517 block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1519 for (i = 0; i < sbi->s_groups_count; i++) {
1520 gdp = ext4_get_group_desc(sb, i, &bh);
1522 flex_group = ext4_flex_group(sbi, i);
1523 sbi->s_flex_groups[flex_group].free_inodes +=
1524 le16_to_cpu(gdp->bg_free_inodes_count);
1525 sbi->s_flex_groups[flex_group].free_blocks +=
1526 le16_to_cpu(gdp->bg_free_blocks_count);
1529 return 1;
1530 failed:
1531 return 0;
1534 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1535 struct ext4_group_desc *gdp)
1537 __u16 crc = 0;
1539 if (sbi->s_es->s_feature_ro_compat &
1540 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1541 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1542 __le32 le_group = cpu_to_le32(block_group);
1544 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1545 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1546 crc = crc16(crc, (__u8 *)gdp, offset);
1547 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1548 /* for checksum of struct ext4_group_desc do the rest...*/
1549 if ((sbi->s_es->s_feature_incompat &
1550 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1551 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1552 crc = crc16(crc, (__u8 *)gdp + offset,
1553 le16_to_cpu(sbi->s_es->s_desc_size) -
1554 offset);
1557 return cpu_to_le16(crc);
1560 int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1561 struct ext4_group_desc *gdp)
1563 if ((sbi->s_es->s_feature_ro_compat &
1564 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1565 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1566 return 0;
1568 return 1;
1571 /* Called at mount-time, super-block is locked */
1572 static int ext4_check_descriptors(struct super_block *sb)
1574 struct ext4_sb_info *sbi = EXT4_SB(sb);
1575 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1576 ext4_fsblk_t last_block;
1577 ext4_fsblk_t block_bitmap;
1578 ext4_fsblk_t inode_bitmap;
1579 ext4_fsblk_t inode_table;
1580 int flexbg_flag = 0;
1581 ext4_group_t i;
1583 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1584 flexbg_flag = 1;
1586 ext4_debug ("Checking group descriptors");
1588 for (i = 0; i < sbi->s_groups_count; i++) {
1589 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1591 if (i == sbi->s_groups_count - 1 || flexbg_flag)
1592 last_block = ext4_blocks_count(sbi->s_es) - 1;
1593 else
1594 last_block = first_block +
1595 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
1597 block_bitmap = ext4_block_bitmap(sb, gdp);
1598 if (block_bitmap < first_block || block_bitmap > last_block) {
1599 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1600 "Block bitmap for group %lu not in group "
1601 "(block %llu)!", i, block_bitmap);
1602 return 0;
1604 inode_bitmap = ext4_inode_bitmap(sb, gdp);
1605 if (inode_bitmap < first_block || inode_bitmap > last_block) {
1606 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1607 "Inode bitmap for group %lu not in group "
1608 "(block %llu)!", i, inode_bitmap);
1609 return 0;
1611 inode_table = ext4_inode_table(sb, gdp);
1612 if (inode_table < first_block ||
1613 inode_table + sbi->s_itb_per_group - 1 > last_block) {
1614 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1615 "Inode table for group %lu not in group "
1616 "(block %llu)!", i, inode_table);
1617 return 0;
1619 spin_lock(sb_bgl_lock(sbi, i));
1620 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
1621 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1622 "Checksum for group %lu failed (%u!=%u)\n",
1623 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1624 gdp)), le16_to_cpu(gdp->bg_checksum));
1625 if (!(sb->s_flags & MS_RDONLY))
1626 return 0;
1628 spin_unlock(sb_bgl_lock(sbi, i));
1629 if (!flexbg_flag)
1630 first_block += EXT4_BLOCKS_PER_GROUP(sb);
1633 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
1634 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
1635 return 1;
1638 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
1639 * the superblock) which were deleted from all directories, but held open by
1640 * a process at the time of a crash. We walk the list and try to delete these
1641 * inodes at recovery time (only with a read-write filesystem).
1643 * In order to keep the orphan inode chain consistent during traversal (in
1644 * case of crash during recovery), we link each inode into the superblock
1645 * orphan list_head and handle it the same way as an inode deletion during
1646 * normal operation (which journals the operations for us).
1648 * We only do an iget() and an iput() on each inode, which is very safe if we
1649 * accidentally point at an in-use or already deleted inode. The worst that
1650 * can happen in this case is that we get a "bit already cleared" message from
1651 * ext4_free_inode(). The only reason we would point at a wrong inode is if
1652 * e2fsck was run on this filesystem, and it must have already done the orphan
1653 * inode cleanup for us, so we can safely abort without any further action.
1655 static void ext4_orphan_cleanup(struct super_block *sb,
1656 struct ext4_super_block *es)
1658 unsigned int s_flags = sb->s_flags;
1659 int nr_orphans = 0, nr_truncates = 0;
1660 #ifdef CONFIG_QUOTA
1661 int i;
1662 #endif
1663 if (!es->s_last_orphan) {
1664 jbd_debug(4, "no orphan inodes to clean up\n");
1665 return;
1668 if (bdev_read_only(sb->s_bdev)) {
1669 printk(KERN_ERR "EXT4-fs: write access "
1670 "unavailable, skipping orphan cleanup.\n");
1671 return;
1674 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
1675 if (es->s_last_orphan)
1676 jbd_debug(1, "Errors on filesystem, "
1677 "clearing orphan list.\n");
1678 es->s_last_orphan = 0;
1679 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1680 return;
1683 if (s_flags & MS_RDONLY) {
1684 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
1685 sb->s_id);
1686 sb->s_flags &= ~MS_RDONLY;
1688 #ifdef CONFIG_QUOTA
1689 /* Needed for iput() to work correctly and not trash data */
1690 sb->s_flags |= MS_ACTIVE;
1691 /* Turn on quotas so that they are updated correctly */
1692 for (i = 0; i < MAXQUOTAS; i++) {
1693 if (EXT4_SB(sb)->s_qf_names[i]) {
1694 int ret = ext4_quota_on_mount(sb, i);
1695 if (ret < 0)
1696 printk(KERN_ERR
1697 "EXT4-fs: Cannot turn on journaled "
1698 "quota: error %d\n", ret);
1701 #endif
1703 while (es->s_last_orphan) {
1704 struct inode *inode;
1706 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1707 if (IS_ERR(inode)) {
1708 es->s_last_orphan = 0;
1709 break;
1712 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
1713 DQUOT_INIT(inode);
1714 if (inode->i_nlink) {
1715 printk(KERN_DEBUG
1716 "%s: truncating inode %lu to %Ld bytes\n",
1717 __func__, inode->i_ino, inode->i_size);
1718 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1719 inode->i_ino, inode->i_size);
1720 ext4_truncate(inode);
1721 nr_truncates++;
1722 } else {
1723 printk(KERN_DEBUG
1724 "%s: deleting unreferenced inode %lu\n",
1725 __func__, inode->i_ino);
1726 jbd_debug(2, "deleting unreferenced inode %lu\n",
1727 inode->i_ino);
1728 nr_orphans++;
1730 iput(inode); /* The delete magic happens here! */
1733 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
1735 if (nr_orphans)
1736 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
1737 sb->s_id, PLURAL(nr_orphans));
1738 if (nr_truncates)
1739 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
1740 sb->s_id, PLURAL(nr_truncates));
1741 #ifdef CONFIG_QUOTA
1742 /* Turn quotas off */
1743 for (i = 0; i < MAXQUOTAS; i++) {
1744 if (sb_dqopt(sb)->files[i])
1745 vfs_quota_off(sb, i, 0);
1747 #endif
1748 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1751 * Maximal extent format file size.
1752 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1753 * extent format containers, within a sector_t, and within i_blocks
1754 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1755 * so that won't be a limiting factor.
1757 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1759 static loff_t ext4_max_size(int blkbits)
1761 loff_t res;
1762 loff_t upper_limit = MAX_LFS_FILESIZE;
1764 /* small i_blocks in vfs inode? */
1765 if (sizeof(blkcnt_t) < sizeof(u64)) {
1767 * CONFIG_LSF is not enabled implies the inode
1768 * i_block represent total blocks in 512 bytes
1769 * 32 == size of vfs inode i_blocks * 8
1771 upper_limit = (1LL << 32) - 1;
1773 /* total blocks in file system block size */
1774 upper_limit >>= (blkbits - 9);
1775 upper_limit <<= blkbits;
1778 /* 32-bit extent-start container, ee_block */
1779 res = 1LL << 32;
1780 res <<= blkbits;
1781 res -= 1;
1783 /* Sanity check against vm- & vfs- imposed limits */
1784 if (res > upper_limit)
1785 res = upper_limit;
1787 return res;
1791 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
1792 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1793 * We need to be 1 filesystem block less than the 2^48 sector limit.
1795 static loff_t ext4_max_bitmap_size(int bits)
1797 loff_t res = EXT4_NDIR_BLOCKS;
1798 int meta_blocks;
1799 loff_t upper_limit;
1800 /* This is calculated to be the largest file size for a
1801 * dense, bitmapped file such that the total number of
1802 * sectors in the file, including data and all indirect blocks,
1803 * does not exceed 2^48 -1
1804 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1805 * total number of 512 bytes blocks of the file
1808 if (sizeof(blkcnt_t) < sizeof(u64)) {
1810 * CONFIG_LSF is not enabled implies the inode
1811 * i_block represent total blocks in 512 bytes
1812 * 32 == size of vfs inode i_blocks * 8
1814 upper_limit = (1LL << 32) - 1;
1816 /* total blocks in file system block size */
1817 upper_limit >>= (bits - 9);
1819 } else {
1821 * We use 48 bit ext4_inode i_blocks
1822 * With EXT4_HUGE_FILE_FL set the i_blocks
1823 * represent total number of blocks in
1824 * file system block size
1826 upper_limit = (1LL << 48) - 1;
1830 /* indirect blocks */
1831 meta_blocks = 1;
1832 /* double indirect blocks */
1833 meta_blocks += 1 + (1LL << (bits-2));
1834 /* tripple indirect blocks */
1835 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1837 upper_limit -= meta_blocks;
1838 upper_limit <<= bits;
1840 res += 1LL << (bits-2);
1841 res += 1LL << (2*(bits-2));
1842 res += 1LL << (3*(bits-2));
1843 res <<= bits;
1844 if (res > upper_limit)
1845 res = upper_limit;
1847 if (res > MAX_LFS_FILESIZE)
1848 res = MAX_LFS_FILESIZE;
1850 return res;
1853 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1854 ext4_fsblk_t logical_sb_block, int nr)
1856 struct ext4_sb_info *sbi = EXT4_SB(sb);
1857 ext4_group_t bg, first_meta_bg;
1858 int has_super = 0;
1860 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1862 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
1863 nr < first_meta_bg)
1864 return logical_sb_block + nr + 1;
1865 bg = sbi->s_desc_per_block * nr;
1866 if (ext4_bg_has_super(sb, bg))
1867 has_super = 1;
1868 return (has_super + ext4_group_first_block_no(sb, bg));
1872 * ext4_get_stripe_size: Get the stripe size.
1873 * @sbi: In memory super block info
1875 * If we have specified it via mount option, then
1876 * use the mount option value. If the value specified at mount time is
1877 * greater than the blocks per group use the super block value.
1878 * If the super block value is greater than blocks per group return 0.
1879 * Allocator needs it be less than blocks per group.
1882 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1884 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1885 unsigned long stripe_width =
1886 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1888 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1889 return sbi->s_stripe;
1891 if (stripe_width <= sbi->s_blocks_per_group)
1892 return stripe_width;
1894 if (stride <= sbi->s_blocks_per_group)
1895 return stride;
1897 return 0;
1900 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
1901 __releases(kernel_lock)
1902 __acquires(kernel_lock)
1905 struct buffer_head *bh;
1906 struct ext4_super_block *es = NULL;
1907 struct ext4_sb_info *sbi;
1908 ext4_fsblk_t block;
1909 ext4_fsblk_t sb_block = get_sb_block(&data);
1910 ext4_fsblk_t logical_sb_block;
1911 unsigned long offset = 0;
1912 unsigned int journal_inum = 0;
1913 unsigned long journal_devnum = 0;
1914 unsigned long def_mount_opts;
1915 struct inode *root;
1916 int ret = -EINVAL;
1917 int blocksize;
1918 int db_count;
1919 int i;
1920 int needs_recovery;
1921 __le32 features;
1922 __u64 blocks_count;
1923 int err;
1925 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1926 if (!sbi)
1927 return -ENOMEM;
1928 sb->s_fs_info = sbi;
1929 sbi->s_mount_opt = 0;
1930 sbi->s_resuid = EXT4_DEF_RESUID;
1931 sbi->s_resgid = EXT4_DEF_RESGID;
1932 sbi->s_sb_block = sb_block;
1934 unlock_kernel();
1936 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
1937 if (!blocksize) {
1938 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
1939 goto out_fail;
1943 * The ext4 superblock will not be buffer aligned for other than 1kB
1944 * block sizes. We need to calculate the offset from buffer start.
1946 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
1947 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1948 offset = do_div(logical_sb_block, blocksize);
1949 } else {
1950 logical_sb_block = sb_block;
1953 if (!(bh = sb_bread(sb, logical_sb_block))) {
1954 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
1955 goto out_fail;
1958 * Note: s_es must be initialized as soon as possible because
1959 * some ext4 macro-instructions depend on its value
1961 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1962 sbi->s_es = es;
1963 sb->s_magic = le16_to_cpu(es->s_magic);
1964 if (sb->s_magic != EXT4_SUPER_MAGIC)
1965 goto cantfind_ext4;
1967 /* Set defaults before we parse the mount options */
1968 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1969 if (def_mount_opts & EXT4_DEFM_DEBUG)
1970 set_opt(sbi->s_mount_opt, DEBUG);
1971 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
1972 set_opt(sbi->s_mount_opt, GRPID);
1973 if (def_mount_opts & EXT4_DEFM_UID16)
1974 set_opt(sbi->s_mount_opt, NO_UID32);
1975 #ifdef CONFIG_EXT4DEV_FS_XATTR
1976 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
1977 set_opt(sbi->s_mount_opt, XATTR_USER);
1978 #endif
1979 #ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
1980 if (def_mount_opts & EXT4_DEFM_ACL)
1981 set_opt(sbi->s_mount_opt, POSIX_ACL);
1982 #endif
1983 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1984 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1985 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1986 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1987 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1988 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
1990 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
1991 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
1992 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
1993 set_opt(sbi->s_mount_opt, ERRORS_CONT);
1994 else
1995 set_opt(sbi->s_mount_opt, ERRORS_RO);
1997 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1998 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2000 set_opt(sbi->s_mount_opt, RESERVATION);
2001 set_opt(sbi->s_mount_opt, BARRIER);
2004 * turn on extents feature by default in ext4 filesystem
2005 * only if feature flag already set by mkfs or tune2fs.
2006 * Use -o noextents to turn it off
2008 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2009 set_opt(sbi->s_mount_opt, EXTENTS);
2010 else
2011 ext4_warning(sb, __func__,
2012 "extents feature not enabled on this filesystem, "
2013 "use tune2fs.\n");
2015 * turn on mballoc code by default in ext4 filesystem
2016 * Use -o nomballoc to turn it off
2018 set_opt(sbi->s_mount_opt, MBALLOC);
2021 * enable delayed allocation by default
2022 * Use -o nodelalloc to turn it off
2024 set_opt(sbi->s_mount_opt, DELALLOC);
2027 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2028 NULL, 0))
2029 goto failed_mount;
2031 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2032 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2034 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2035 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2036 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2037 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
2038 printk(KERN_WARNING
2039 "EXT4-fs warning: feature flags set on rev 0 fs, "
2040 "running e2fsck is recommended\n");
2043 * Since ext4 is still considered development code, we require
2044 * that the TEST_FILESYS flag in s->flags be set.
2046 if (!(le32_to_cpu(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)) {
2047 printk(KERN_WARNING "EXT4-fs: %s: not marked "
2048 "OK to use with test code.\n", sb->s_id);
2049 goto failed_mount;
2053 * Check feature flags regardless of the revision level, since we
2054 * previously didn't change the revision level when setting the flags,
2055 * so there is a chance incompat flags are set on a rev 0 filesystem.
2057 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
2058 if (features) {
2059 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
2060 "unsupported optional features (%x).\n",
2061 sb->s_id, le32_to_cpu(features));
2062 goto failed_mount;
2064 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
2065 if (!(sb->s_flags & MS_RDONLY) && features) {
2066 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
2067 "unsupported optional features (%x).\n",
2068 sb->s_id, le32_to_cpu(features));
2069 goto failed_mount;
2071 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2073 * Large file size enabled file system can only be
2074 * mount if kernel is build with CONFIG_LSF
2076 if (sizeof(root->i_blocks) < sizeof(u64) &&
2077 !(sb->s_flags & MS_RDONLY)) {
2078 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2079 "files cannot be mounted read-write "
2080 "without CONFIG_LSF.\n", sb->s_id);
2081 goto failed_mount;
2084 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2086 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2087 blocksize > EXT4_MAX_BLOCK_SIZE) {
2088 printk(KERN_ERR
2089 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
2090 blocksize, sb->s_id);
2091 goto failed_mount;
2094 if (sb->s_blocksize != blocksize) {
2096 /* Validate the filesystem blocksize */
2097 if (!sb_set_blocksize(sb, blocksize)) {
2098 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2099 blocksize);
2100 goto failed_mount;
2103 brelse(bh);
2104 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2105 offset = do_div(logical_sb_block, blocksize);
2106 bh = sb_bread(sb, logical_sb_block);
2107 if (!bh) {
2108 printk(KERN_ERR
2109 "EXT4-fs: Can't read superblock on 2nd try.\n");
2110 goto failed_mount;
2112 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
2113 sbi->s_es = es;
2114 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
2115 printk(KERN_ERR
2116 "EXT4-fs: Magic mismatch, very weird !\n");
2117 goto failed_mount;
2121 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits);
2122 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
2124 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2125 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2126 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
2127 } else {
2128 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2129 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
2130 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
2131 (!is_power_of_2(sbi->s_inode_size)) ||
2132 (sbi->s_inode_size > blocksize)) {
2133 printk(KERN_ERR
2134 "EXT4-fs: unsupported inode size: %d\n",
2135 sbi->s_inode_size);
2136 goto failed_mount;
2138 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2139 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
2141 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2142 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
2143 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
2144 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
2145 !is_power_of_2(sbi->s_desc_size)) {
2146 printk(KERN_ERR
2147 "EXT4-fs: unsupported descriptor size %lu\n",
2148 sbi->s_desc_size);
2149 goto failed_mount;
2151 } else
2152 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
2153 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
2154 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
2155 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
2156 goto cantfind_ext4;
2157 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
2158 if (sbi->s_inodes_per_block == 0)
2159 goto cantfind_ext4;
2160 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2161 sbi->s_inodes_per_block;
2162 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
2163 sbi->s_sbh = bh;
2164 sbi->s_mount_state = le16_to_cpu(es->s_state);
2165 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2166 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
2167 for (i = 0; i < 4; i++)
2168 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2169 sbi->s_def_hash_version = es->s_def_hash_version;
2171 if (sbi->s_blocks_per_group > blocksize * 8) {
2172 printk(KERN_ERR
2173 "EXT4-fs: #blocks per group too big: %lu\n",
2174 sbi->s_blocks_per_group);
2175 goto failed_mount;
2177 if (sbi->s_inodes_per_group > blocksize * 8) {
2178 printk(KERN_ERR
2179 "EXT4-fs: #inodes per group too big: %lu\n",
2180 sbi->s_inodes_per_group);
2181 goto failed_mount;
2184 if (ext4_blocks_count(es) >
2185 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
2186 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
2187 " too large to mount safely\n", sb->s_id);
2188 if (sizeof(sector_t) < 8)
2189 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
2190 "enabled\n");
2191 goto failed_mount;
2194 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2195 goto cantfind_ext4;
2197 /* ensure blocks_count calculation below doesn't sign-extend */
2198 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2199 le32_to_cpu(es->s_first_data_block) + 1) {
2200 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2201 "first data block %u, blocks per group %lu\n",
2202 ext4_blocks_count(es),
2203 le32_to_cpu(es->s_first_data_block),
2204 EXT4_BLOCKS_PER_GROUP(sb));
2205 goto failed_mount;
2207 blocks_count = (ext4_blocks_count(es) -
2208 le32_to_cpu(es->s_first_data_block) +
2209 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2210 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2211 sbi->s_groups_count = blocks_count;
2212 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2213 EXT4_DESC_PER_BLOCK(sb);
2214 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
2215 GFP_KERNEL);
2216 if (sbi->s_group_desc == NULL) {
2217 printk(KERN_ERR "EXT4-fs: not enough memory\n");
2218 goto failed_mount;
2221 bgl_lock_init(&sbi->s_blockgroup_lock);
2223 for (i = 0; i < db_count; i++) {
2224 block = descriptor_loc(sb, logical_sb_block, i);
2225 sbi->s_group_desc[i] = sb_bread(sb, block);
2226 if (!sbi->s_group_desc[i]) {
2227 printk(KERN_ERR "EXT4-fs: "
2228 "can't read group descriptor %d\n", i);
2229 db_count = i;
2230 goto failed_mount2;
2233 if (!ext4_check_descriptors(sb)) {
2234 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
2235 goto failed_mount2;
2237 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2238 if (!ext4_fill_flex_info(sb)) {
2239 printk(KERN_ERR
2240 "EXT4-fs: unable to initialize "
2241 "flex_bg meta info!\n");
2242 goto failed_mount2;
2245 sbi->s_gdb_count = db_count;
2246 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2247 spin_lock_init(&sbi->s_next_gen_lock);
2249 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2250 ext4_count_free_blocks(sb));
2251 if (!err) {
2252 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2253 ext4_count_free_inodes(sb));
2255 if (!err) {
2256 err = percpu_counter_init(&sbi->s_dirs_counter,
2257 ext4_count_dirs(sb));
2259 if (err) {
2260 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2261 goto failed_mount3;
2264 /* per fileystem reservation list head & lock */
2265 spin_lock_init(&sbi->s_rsv_window_lock);
2266 sbi->s_rsv_window_root = RB_ROOT;
2267 /* Add a single, static dummy reservation to the start of the
2268 * reservation window list --- it gives us a placeholder for
2269 * append-at-start-of-list which makes the allocation logic
2270 * _much_ simpler. */
2271 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2272 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
2273 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
2274 sbi->s_rsv_window_head.rsv_goal_size = 0;
2275 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
2277 sbi->s_stripe = ext4_get_stripe_size(sbi);
2280 * set up enough so that it can read an inode
2282 sb->s_op = &ext4_sops;
2283 sb->s_export_op = &ext4_export_ops;
2284 sb->s_xattr = ext4_xattr_handlers;
2285 #ifdef CONFIG_QUOTA
2286 sb->s_qcop = &ext4_qctl_operations;
2287 sb->dq_op = &ext4_quota_operations;
2288 #endif
2289 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2291 sb->s_root = NULL;
2293 needs_recovery = (es->s_last_orphan != 0 ||
2294 EXT4_HAS_INCOMPAT_FEATURE(sb,
2295 EXT4_FEATURE_INCOMPAT_RECOVER));
2298 * The first inode we look at is the journal inode. Don't try
2299 * root first: it may be modified in the journal!
2301 if (!test_opt(sb, NOLOAD) &&
2302 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2303 if (ext4_load_journal(sb, es, journal_devnum))
2304 goto failed_mount3;
2305 if (!(sb->s_flags & MS_RDONLY) &&
2306 EXT4_SB(sb)->s_journal->j_failed_commit) {
2307 printk(KERN_CRIT "EXT4-fs error (device %s): "
2308 "ext4_fill_super: Journal transaction "
2309 "%u is corrupt\n", sb->s_id,
2310 EXT4_SB(sb)->s_journal->j_failed_commit);
2311 if (test_opt(sb, ERRORS_RO)) {
2312 printk(KERN_CRIT
2313 "Mounting filesystem read-only\n");
2314 sb->s_flags |= MS_RDONLY;
2315 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2316 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2318 if (test_opt(sb, ERRORS_PANIC)) {
2319 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2320 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2321 ext4_commit_super(sb, es, 1);
2322 printk(KERN_CRIT
2323 "EXT4-fs (device %s): mount failed\n",
2324 sb->s_id);
2325 goto failed_mount4;
2328 } else if (journal_inum) {
2329 if (ext4_create_journal(sb, es, journal_inum))
2330 goto failed_mount3;
2331 } else {
2332 if (!silent)
2333 printk(KERN_ERR
2334 "ext4: No journal on filesystem on %s\n",
2335 sb->s_id);
2336 goto failed_mount3;
2339 if (ext4_blocks_count(es) > 0xffffffffULL &&
2340 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2341 JBD2_FEATURE_INCOMPAT_64BIT)) {
2342 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2343 goto failed_mount4;
2346 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2347 jbd2_journal_set_features(sbi->s_journal,
2348 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2349 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2350 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2351 jbd2_journal_set_features(sbi->s_journal,
2352 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2353 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2354 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2355 } else {
2356 jbd2_journal_clear_features(sbi->s_journal,
2357 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2358 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2361 /* We have now updated the journal if required, so we can
2362 * validate the data journaling mode. */
2363 switch (test_opt(sb, DATA_FLAGS)) {
2364 case 0:
2365 /* No mode set, assume a default based on the journal
2366 * capabilities: ORDERED_DATA if the journal can
2367 * cope, else JOURNAL_DATA
2369 if (jbd2_journal_check_available_features
2370 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
2371 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2372 else
2373 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2374 break;
2376 case EXT4_MOUNT_ORDERED_DATA:
2377 case EXT4_MOUNT_WRITEBACK_DATA:
2378 if (!jbd2_journal_check_available_features
2379 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
2380 printk(KERN_ERR "EXT4-fs: Journal does not support "
2381 "requested data journaling mode\n");
2382 goto failed_mount4;
2384 default:
2385 break;
2388 if (test_opt(sb, NOBH)) {
2389 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2390 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
2391 "its supported only with writeback mode\n");
2392 clear_opt(sbi->s_mount_opt, NOBH);
2396 * The jbd2_journal_load will have done any necessary log recovery,
2397 * so we can safely mount the rest of the filesystem now.
2400 root = ext4_iget(sb, EXT4_ROOT_INO);
2401 if (IS_ERR(root)) {
2402 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2403 ret = PTR_ERR(root);
2404 goto failed_mount4;
2406 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2407 iput(root);
2408 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
2409 goto failed_mount4;
2411 sb->s_root = d_alloc_root(root);
2412 if (!sb->s_root) {
2413 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2414 iput(root);
2415 ret = -ENOMEM;
2416 goto failed_mount4;
2419 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
2421 /* determine the minimum size of new large inodes, if present */
2422 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2423 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2424 EXT4_GOOD_OLD_INODE_SIZE;
2425 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2426 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2427 if (sbi->s_want_extra_isize <
2428 le16_to_cpu(es->s_want_extra_isize))
2429 sbi->s_want_extra_isize =
2430 le16_to_cpu(es->s_want_extra_isize);
2431 if (sbi->s_want_extra_isize <
2432 le16_to_cpu(es->s_min_extra_isize))
2433 sbi->s_want_extra_isize =
2434 le16_to_cpu(es->s_min_extra_isize);
2437 /* Check if enough inode space is available */
2438 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2439 sbi->s_inode_size) {
2440 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2441 EXT4_GOOD_OLD_INODE_SIZE;
2442 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2443 "available.\n");
2447 * akpm: core read_super() calls in here with the superblock locked.
2448 * That deadlocks, because orphan cleanup needs to lock the superblock
2449 * in numerous places. Here we just pop the lock - it's relatively
2450 * harmless, because we are now ready to accept write_super() requests,
2451 * and aviro says that's the only reason for hanging onto the
2452 * superblock lock.
2454 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2455 ext4_orphan_cleanup(sb, es);
2456 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
2457 if (needs_recovery)
2458 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
2459 ext4_mark_recovery_complete(sb, es);
2460 printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2461 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2462 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2463 "writeback");
2465 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2466 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2467 "requested data journaling mode\n");
2468 clear_opt(sbi->s_mount_opt, DELALLOC);
2469 } else if (test_opt(sb, DELALLOC))
2470 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2472 ext4_ext_init(sb);
2473 ext4_mb_init(sb, needs_recovery);
2475 lock_kernel();
2476 return 0;
2478 cantfind_ext4:
2479 if (!silent)
2480 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
2481 sb->s_id);
2482 goto failed_mount;
2484 failed_mount4:
2485 jbd2_journal_destroy(sbi->s_journal);
2486 sbi->s_journal = NULL;
2487 failed_mount3:
2488 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2489 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2490 percpu_counter_destroy(&sbi->s_dirs_counter);
2491 failed_mount2:
2492 for (i = 0; i < db_count; i++)
2493 brelse(sbi->s_group_desc[i]);
2494 kfree(sbi->s_group_desc);
2495 failed_mount:
2496 #ifdef CONFIG_QUOTA
2497 for (i = 0; i < MAXQUOTAS; i++)
2498 kfree(sbi->s_qf_names[i]);
2499 #endif
2500 ext4_blkdev_remove(sbi);
2501 brelse(bh);
2502 out_fail:
2503 sb->s_fs_info = NULL;
2504 kfree(sbi);
2505 lock_kernel();
2506 return ret;
2510 * Setup any per-fs journal parameters now. We'll do this both on
2511 * initial mount, once the journal has been initialised but before we've
2512 * done any recovery; and again on any subsequent remount.
2514 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
2516 struct ext4_sb_info *sbi = EXT4_SB(sb);
2518 if (sbi->s_commit_interval)
2519 journal->j_commit_interval = sbi->s_commit_interval;
2520 /* We could also set up an ext4-specific default for the commit
2521 * interval here, but for now we'll just fall back to the jbd
2522 * default. */
2524 spin_lock(&journal->j_state_lock);
2525 if (test_opt(sb, BARRIER))
2526 journal->j_flags |= JBD2_BARRIER;
2527 else
2528 journal->j_flags &= ~JBD2_BARRIER;
2529 spin_unlock(&journal->j_state_lock);
2532 static journal_t *ext4_get_journal(struct super_block *sb,
2533 unsigned int journal_inum)
2535 struct inode *journal_inode;
2536 journal_t *journal;
2538 /* First, test for the existence of a valid inode on disk. Bad
2539 * things happen if we iget() an unused inode, as the subsequent
2540 * iput() will try to delete it. */
2542 journal_inode = ext4_iget(sb, journal_inum);
2543 if (IS_ERR(journal_inode)) {
2544 printk(KERN_ERR "EXT4-fs: no journal found.\n");
2545 return NULL;
2547 if (!journal_inode->i_nlink) {
2548 make_bad_inode(journal_inode);
2549 iput(journal_inode);
2550 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
2551 return NULL;
2554 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2555 journal_inode, journal_inode->i_size);
2556 if (!S_ISREG(journal_inode->i_mode)) {
2557 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
2558 iput(journal_inode);
2559 return NULL;
2562 journal = jbd2_journal_init_inode(journal_inode);
2563 if (!journal) {
2564 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
2565 iput(journal_inode);
2566 return NULL;
2568 journal->j_private = sb;
2569 ext4_init_journal_params(sb, journal);
2570 return journal;
2573 static journal_t *ext4_get_dev_journal(struct super_block *sb,
2574 dev_t j_dev)
2576 struct buffer_head *bh;
2577 journal_t *journal;
2578 ext4_fsblk_t start;
2579 ext4_fsblk_t len;
2580 int hblock, blocksize;
2581 ext4_fsblk_t sb_block;
2582 unsigned long offset;
2583 struct ext4_super_block *es;
2584 struct block_device *bdev;
2586 bdev = ext4_blkdev_get(j_dev);
2587 if (bdev == NULL)
2588 return NULL;
2590 if (bd_claim(bdev, sb)) {
2591 printk(KERN_ERR
2592 "EXT4: failed to claim external journal device.\n");
2593 blkdev_put(bdev);
2594 return NULL;
2597 blocksize = sb->s_blocksize;
2598 hblock = bdev_hardsect_size(bdev);
2599 if (blocksize < hblock) {
2600 printk(KERN_ERR
2601 "EXT4-fs: blocksize too small for journal device.\n");
2602 goto out_bdev;
2605 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2606 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
2607 set_blocksize(bdev, blocksize);
2608 if (!(bh = __bread(bdev, sb_block, blocksize))) {
2609 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
2610 "external journal\n");
2611 goto out_bdev;
2614 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2615 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
2616 !(le32_to_cpu(es->s_feature_incompat) &
2617 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2618 printk(KERN_ERR "EXT4-fs: external journal has "
2619 "bad superblock\n");
2620 brelse(bh);
2621 goto out_bdev;
2624 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2625 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
2626 brelse(bh);
2627 goto out_bdev;
2630 len = ext4_blocks_count(es);
2631 start = sb_block + 1;
2632 brelse(bh); /* we're done with the superblock */
2634 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
2635 start, len, blocksize);
2636 if (!journal) {
2637 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
2638 goto out_bdev;
2640 journal->j_private = sb;
2641 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2642 wait_on_buffer(journal->j_sb_buffer);
2643 if (!buffer_uptodate(journal->j_sb_buffer)) {
2644 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
2645 goto out_journal;
2647 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
2648 printk(KERN_ERR "EXT4-fs: External journal has more than one "
2649 "user (unsupported) - %d\n",
2650 be32_to_cpu(journal->j_superblock->s_nr_users));
2651 goto out_journal;
2653 EXT4_SB(sb)->journal_bdev = bdev;
2654 ext4_init_journal_params(sb, journal);
2655 return journal;
2656 out_journal:
2657 jbd2_journal_destroy(journal);
2658 out_bdev:
2659 ext4_blkdev_put(bdev);
2660 return NULL;
2663 static int ext4_load_journal(struct super_block *sb,
2664 struct ext4_super_block *es,
2665 unsigned long journal_devnum)
2667 journal_t *journal;
2668 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2669 dev_t journal_dev;
2670 int err = 0;
2671 int really_read_only;
2673 if (journal_devnum &&
2674 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2675 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
2676 "numbers have changed\n");
2677 journal_dev = new_decode_dev(journal_devnum);
2678 } else
2679 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2681 really_read_only = bdev_read_only(sb->s_bdev);
2684 * Are we loading a blank journal or performing recovery after a
2685 * crash? For recovery, we need to check in advance whether we
2686 * can get read-write access to the device.
2689 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2690 if (sb->s_flags & MS_RDONLY) {
2691 printk(KERN_INFO "EXT4-fs: INFO: recovery "
2692 "required on readonly filesystem.\n");
2693 if (really_read_only) {
2694 printk(KERN_ERR "EXT4-fs: write access "
2695 "unavailable, cannot proceed.\n");
2696 return -EROFS;
2698 printk(KERN_INFO "EXT4-fs: write access will "
2699 "be enabled during recovery.\n");
2703 if (journal_inum && journal_dev) {
2704 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
2705 "and inode journals!\n");
2706 return -EINVAL;
2709 if (journal_inum) {
2710 if (!(journal = ext4_get_journal(sb, journal_inum)))
2711 return -EINVAL;
2712 } else {
2713 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
2714 return -EINVAL;
2717 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2718 err = jbd2_journal_update_format(journal);
2719 if (err) {
2720 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
2721 jbd2_journal_destroy(journal);
2722 return err;
2726 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
2727 err = jbd2_journal_wipe(journal, !really_read_only);
2728 if (!err)
2729 err = jbd2_journal_load(journal);
2731 if (err) {
2732 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
2733 jbd2_journal_destroy(journal);
2734 return err;
2737 EXT4_SB(sb)->s_journal = journal;
2738 ext4_clear_journal_err(sb, es);
2740 if (journal_devnum &&
2741 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2742 es->s_journal_dev = cpu_to_le32(journal_devnum);
2743 sb->s_dirt = 1;
2745 /* Make sure we flush the recovery flag to disk. */
2746 ext4_commit_super(sb, es, 1);
2749 return 0;
2752 static int ext4_create_journal(struct super_block *sb,
2753 struct ext4_super_block *es,
2754 unsigned int journal_inum)
2756 journal_t *journal;
2757 int err;
2759 if (sb->s_flags & MS_RDONLY) {
2760 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
2761 "create journal.\n");
2762 return -EROFS;
2765 journal = ext4_get_journal(sb, journal_inum);
2766 if (!journal)
2767 return -EINVAL;
2769 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
2770 journal_inum);
2772 err = jbd2_journal_create(journal);
2773 if (err) {
2774 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
2775 jbd2_journal_destroy(journal);
2776 return -EIO;
2779 EXT4_SB(sb)->s_journal = journal;
2781 ext4_update_dynamic_rev(sb);
2782 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2783 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
2785 es->s_journal_inum = cpu_to_le32(journal_inum);
2786 sb->s_dirt = 1;
2788 /* Make sure we flush the recovery flag to disk. */
2789 ext4_commit_super(sb, es, 1);
2791 return 0;
2794 static void ext4_commit_super(struct super_block *sb,
2795 struct ext4_super_block *es, int sync)
2797 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2799 if (!sbh)
2800 return;
2801 es->s_wtime = cpu_to_le32(get_seconds());
2802 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
2803 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
2804 BUFFER_TRACE(sbh, "marking dirty");
2805 mark_buffer_dirty(sbh);
2806 if (sync)
2807 sync_dirty_buffer(sbh);
2812 * Have we just finished recovery? If so, and if we are mounting (or
2813 * remounting) the filesystem readonly, then we will end up with a
2814 * consistent fs on disk. Record that fact.
2816 static void ext4_mark_recovery_complete(struct super_block *sb,
2817 struct ext4_super_block *es)
2819 journal_t *journal = EXT4_SB(sb)->s_journal;
2821 jbd2_journal_lock_updates(journal);
2822 jbd2_journal_flush(journal);
2823 lock_super(sb);
2824 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
2825 sb->s_flags & MS_RDONLY) {
2826 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2827 sb->s_dirt = 0;
2828 ext4_commit_super(sb, es, 1);
2830 unlock_super(sb);
2831 jbd2_journal_unlock_updates(journal);
2835 * If we are mounting (or read-write remounting) a filesystem whose journal
2836 * has recorded an error from a previous lifetime, move that error to the
2837 * main filesystem now.
2839 static void ext4_clear_journal_err(struct super_block *sb,
2840 struct ext4_super_block *es)
2842 journal_t *journal;
2843 int j_errno;
2844 const char *errstr;
2846 journal = EXT4_SB(sb)->s_journal;
2849 * Now check for any error status which may have been recorded in the
2850 * journal by a prior ext4_error() or ext4_abort()
2853 j_errno = jbd2_journal_errno(journal);
2854 if (j_errno) {
2855 char nbuf[16];
2857 errstr = ext4_decode_error(sb, j_errno, nbuf);
2858 ext4_warning(sb, __func__, "Filesystem error recorded "
2859 "from previous mount: %s", errstr);
2860 ext4_warning(sb, __func__, "Marking fs in need of "
2861 "filesystem check.");
2863 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2864 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2865 ext4_commit_super(sb, es, 1);
2867 jbd2_journal_clear_err(journal);
2872 * Force the running and committing transactions to commit,
2873 * and wait on the commit.
2875 int ext4_force_commit(struct super_block *sb)
2877 journal_t *journal;
2878 int ret;
2880 if (sb->s_flags & MS_RDONLY)
2881 return 0;
2883 journal = EXT4_SB(sb)->s_journal;
2884 sb->s_dirt = 0;
2885 ret = ext4_journal_force_commit(journal);
2886 return ret;
2890 * Ext4 always journals updates to the superblock itself, so we don't
2891 * have to propagate any other updates to the superblock on disk at this
2892 * point. Just start an async writeback to get the buffers on their way
2893 * to the disk.
2895 * This implicitly triggers the writebehind on sync().
2898 static void ext4_write_super(struct super_block *sb)
2900 if (mutex_trylock(&sb->s_lock) != 0)
2901 BUG();
2902 sb->s_dirt = 0;
2905 static int ext4_sync_fs(struct super_block *sb, int wait)
2907 tid_t target;
2909 sb->s_dirt = 0;
2910 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
2911 if (wait)
2912 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
2914 return 0;
2918 * LVM calls this function before a (read-only) snapshot is created. This
2919 * gives us a chance to flush the journal completely and mark the fs clean.
2921 static void ext4_write_super_lockfs(struct super_block *sb)
2923 sb->s_dirt = 0;
2925 if (!(sb->s_flags & MS_RDONLY)) {
2926 journal_t *journal = EXT4_SB(sb)->s_journal;
2928 /* Now we set up the journal barrier. */
2929 jbd2_journal_lock_updates(journal);
2930 jbd2_journal_flush(journal);
2932 /* Journal blocked and flushed, clear needs_recovery flag. */
2933 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2934 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2939 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2940 * flag here, even though the filesystem is not technically dirty yet.
2942 static void ext4_unlockfs(struct super_block *sb)
2944 if (!(sb->s_flags & MS_RDONLY)) {
2945 lock_super(sb);
2946 /* Reser the needs_recovery flag before the fs is unlocked. */
2947 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2948 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
2949 unlock_super(sb);
2950 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
2954 static int ext4_remount(struct super_block *sb, int *flags, char *data)
2956 struct ext4_super_block *es;
2957 struct ext4_sb_info *sbi = EXT4_SB(sb);
2958 ext4_fsblk_t n_blocks_count = 0;
2959 unsigned long old_sb_flags;
2960 struct ext4_mount_options old_opts;
2961 ext4_group_t g;
2962 int err;
2963 #ifdef CONFIG_QUOTA
2964 int i;
2965 #endif
2967 /* Store the original options */
2968 old_sb_flags = sb->s_flags;
2969 old_opts.s_mount_opt = sbi->s_mount_opt;
2970 old_opts.s_resuid = sbi->s_resuid;
2971 old_opts.s_resgid = sbi->s_resgid;
2972 old_opts.s_commit_interval = sbi->s_commit_interval;
2973 #ifdef CONFIG_QUOTA
2974 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2975 for (i = 0; i < MAXQUOTAS; i++)
2976 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2977 #endif
2980 * Allow the "check" option to be passed as a remount option.
2982 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2983 err = -EINVAL;
2984 goto restore_opts;
2987 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2988 ext4_abort(sb, __func__, "Abort forced by user");
2990 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
2991 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
2993 es = sbi->s_es;
2995 ext4_init_journal_params(sb, sbi->s_journal);
2997 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2998 n_blocks_count > ext4_blocks_count(es)) {
2999 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
3000 err = -EROFS;
3001 goto restore_opts;
3004 if (*flags & MS_RDONLY) {
3006 * First of all, the unconditional stuff we have to do
3007 * to disable replay of the journal when we next remount
3009 sb->s_flags |= MS_RDONLY;
3012 * OK, test if we are remounting a valid rw partition
3013 * readonly, and if so set the rdonly flag and then
3014 * mark the partition as valid again.
3016 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3017 (sbi->s_mount_state & EXT4_VALID_FS))
3018 es->s_state = cpu_to_le16(sbi->s_mount_state);
3021 * We have to unlock super so that we can wait for
3022 * transactions.
3024 unlock_super(sb);
3025 ext4_mark_recovery_complete(sb, es);
3026 lock_super(sb);
3027 } else {
3028 __le32 ret;
3029 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3030 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3031 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3032 "remount RDWR because of unsupported "
3033 "optional features (%x).\n",
3034 sb->s_id, le32_to_cpu(ret));
3035 err = -EROFS;
3036 goto restore_opts;
3040 * Make sure the group descriptor checksums
3041 * are sane. If they aren't, refuse to
3042 * remount r/w.
3044 for (g = 0; g < sbi->s_groups_count; g++) {
3045 struct ext4_group_desc *gdp =
3046 ext4_get_group_desc(sb, g, NULL);
3048 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3049 printk(KERN_ERR
3050 "EXT4-fs: ext4_remount: "
3051 "Checksum for group %lu failed (%u!=%u)\n",
3052 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3053 le16_to_cpu(gdp->bg_checksum));
3054 err = -EINVAL;
3055 goto restore_opts;
3060 * If we have an unprocessed orphan list hanging
3061 * around from a previously readonly bdev mount,
3062 * require a full umount/remount for now.
3064 if (es->s_last_orphan) {
3065 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3066 "remount RDWR because of unprocessed "
3067 "orphan inode list. Please "
3068 "umount/remount instead.\n",
3069 sb->s_id);
3070 err = -EINVAL;
3071 goto restore_opts;
3075 * Mounting a RDONLY partition read-write, so reread
3076 * and store the current valid flag. (It may have
3077 * been changed by e2fsck since we originally mounted
3078 * the partition.)
3080 ext4_clear_journal_err(sb, es);
3081 sbi->s_mount_state = le16_to_cpu(es->s_state);
3082 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
3083 goto restore_opts;
3084 if (!ext4_setup_super(sb, es, 0))
3085 sb->s_flags &= ~MS_RDONLY;
3088 #ifdef CONFIG_QUOTA
3089 /* Release old quota file names */
3090 for (i = 0; i < MAXQUOTAS; i++)
3091 if (old_opts.s_qf_names[i] &&
3092 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3093 kfree(old_opts.s_qf_names[i]);
3094 #endif
3095 return 0;
3096 restore_opts:
3097 sb->s_flags = old_sb_flags;
3098 sbi->s_mount_opt = old_opts.s_mount_opt;
3099 sbi->s_resuid = old_opts.s_resuid;
3100 sbi->s_resgid = old_opts.s_resgid;
3101 sbi->s_commit_interval = old_opts.s_commit_interval;
3102 #ifdef CONFIG_QUOTA
3103 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3104 for (i = 0; i < MAXQUOTAS; i++) {
3105 if (sbi->s_qf_names[i] &&
3106 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3107 kfree(sbi->s_qf_names[i]);
3108 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3110 #endif
3111 return err;
3114 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
3116 struct super_block *sb = dentry->d_sb;
3117 struct ext4_sb_info *sbi = EXT4_SB(sb);
3118 struct ext4_super_block *es = sbi->s_es;
3119 u64 fsid;
3121 if (test_opt(sb, MINIX_DF)) {
3122 sbi->s_overhead_last = 0;
3123 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
3124 ext4_group_t ngroups = sbi->s_groups_count, i;
3125 ext4_fsblk_t overhead = 0;
3126 smp_rmb();
3129 * Compute the overhead (FS structures). This is constant
3130 * for a given filesystem unless the number of block groups
3131 * changes so we cache the previous value until it does.
3135 * All of the blocks before first_data_block are
3136 * overhead
3138 overhead = le32_to_cpu(es->s_first_data_block);
3141 * Add the overhead attributed to the superblock and
3142 * block group descriptors. If the sparse superblocks
3143 * feature is turned on, then not all groups have this.
3145 for (i = 0; i < ngroups; i++) {
3146 overhead += ext4_bg_has_super(sb, i) +
3147 ext4_bg_num_gdb(sb, i);
3148 cond_resched();
3152 * Every block group has an inode bitmap, a block
3153 * bitmap, and an inode table.
3155 overhead += ngroups * (2 + sbi->s_itb_per_group);
3156 sbi->s_overhead_last = overhead;
3157 smp_wmb();
3158 sbi->s_blocks_last = ext4_blocks_count(es);
3161 buf->f_type = EXT4_SUPER_MAGIC;
3162 buf->f_bsize = sb->s_blocksize;
3163 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
3164 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
3165 ext4_free_blocks_count_set(es, buf->f_bfree);
3166 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3167 if (buf->f_bfree < ext4_r_blocks_count(es))
3168 buf->f_bavail = 0;
3169 buf->f_files = le32_to_cpu(es->s_inodes_count);
3170 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
3171 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
3172 buf->f_namelen = EXT4_NAME_LEN;
3173 fsid = le64_to_cpup((void *)es->s_uuid) ^
3174 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3175 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3176 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
3177 return 0;
3180 /* Helper function for writing quotas on sync - we need to start transaction before quota file
3181 * is locked for write. Otherwise the are possible deadlocks:
3182 * Process 1 Process 2
3183 * ext4_create() quota_sync()
3184 * jbd2_journal_start() write_dquot()
3185 * DQUOT_INIT() down(dqio_mutex)
3186 * down(dqio_mutex) jbd2_journal_start()
3190 #ifdef CONFIG_QUOTA
3192 static inline struct inode *dquot_to_inode(struct dquot *dquot)
3194 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3197 static int ext4_dquot_initialize(struct inode *inode, int type)
3199 handle_t *handle;
3200 int ret, err;
3202 /* We may create quota structure so we need to reserve enough blocks */
3203 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
3204 if (IS_ERR(handle))
3205 return PTR_ERR(handle);
3206 ret = dquot_initialize(inode, type);
3207 err = ext4_journal_stop(handle);
3208 if (!ret)
3209 ret = err;
3210 return ret;
3213 static int ext4_dquot_drop(struct inode *inode)
3215 handle_t *handle;
3216 int ret, err;
3218 /* We may delete quota structure so we need to reserve enough blocks */
3219 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
3220 if (IS_ERR(handle)) {
3222 * We call dquot_drop() anyway to at least release references
3223 * to quota structures so that umount does not hang.
3225 dquot_drop(inode);
3226 return PTR_ERR(handle);
3228 ret = dquot_drop(inode);
3229 err = ext4_journal_stop(handle);
3230 if (!ret)
3231 ret = err;
3232 return ret;
3235 static int ext4_write_dquot(struct dquot *dquot)
3237 int ret, err;
3238 handle_t *handle;
3239 struct inode *inode;
3241 inode = dquot_to_inode(dquot);
3242 handle = ext4_journal_start(inode,
3243 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
3244 if (IS_ERR(handle))
3245 return PTR_ERR(handle);
3246 ret = dquot_commit(dquot);
3247 err = ext4_journal_stop(handle);
3248 if (!ret)
3249 ret = err;
3250 return ret;
3253 static int ext4_acquire_dquot(struct dquot *dquot)
3255 int ret, err;
3256 handle_t *handle;
3258 handle = ext4_journal_start(dquot_to_inode(dquot),
3259 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
3260 if (IS_ERR(handle))
3261 return PTR_ERR(handle);
3262 ret = dquot_acquire(dquot);
3263 err = ext4_journal_stop(handle);
3264 if (!ret)
3265 ret = err;
3266 return ret;
3269 static int ext4_release_dquot(struct dquot *dquot)
3271 int ret, err;
3272 handle_t *handle;
3274 handle = ext4_journal_start(dquot_to_inode(dquot),
3275 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
3276 if (IS_ERR(handle)) {
3277 /* Release dquot anyway to avoid endless cycle in dqput() */
3278 dquot_release(dquot);
3279 return PTR_ERR(handle);
3281 ret = dquot_release(dquot);
3282 err = ext4_journal_stop(handle);
3283 if (!ret)
3284 ret = err;
3285 return ret;
3288 static int ext4_mark_dquot_dirty(struct dquot *dquot)
3290 /* Are we journaling quotas? */
3291 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3292 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
3293 dquot_mark_dquot_dirty(dquot);
3294 return ext4_write_dquot(dquot);
3295 } else {
3296 return dquot_mark_dquot_dirty(dquot);
3300 static int ext4_write_info(struct super_block *sb, int type)
3302 int ret, err;
3303 handle_t *handle;
3305 /* Data block + inode block */
3306 handle = ext4_journal_start(sb->s_root->d_inode, 2);
3307 if (IS_ERR(handle))
3308 return PTR_ERR(handle);
3309 ret = dquot_commit_info(sb, type);
3310 err = ext4_journal_stop(handle);
3311 if (!ret)
3312 ret = err;
3313 return ret;
3317 * Turn on quotas during mount time - we need to find
3318 * the quota file and such...
3320 static int ext4_quota_on_mount(struct super_block *sb, int type)
3322 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3323 EXT4_SB(sb)->s_jquota_fmt, type);
3327 * Standard function to be called on quota_on
3329 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
3330 char *path, int remount)
3332 int err;
3333 struct nameidata nd;
3335 if (!test_opt(sb, QUOTA))
3336 return -EINVAL;
3337 /* When remounting, no checks are needed and in fact, path is NULL */
3338 if (remount)
3339 return vfs_quota_on(sb, type, format_id, path, remount);
3341 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3342 if (err)
3343 return err;
3345 /* Quotafile not on the same filesystem? */
3346 if (nd.path.mnt->mnt_sb != sb) {
3347 path_put(&nd.path);
3348 return -EXDEV;
3350 /* Journaling quota? */
3351 if (EXT4_SB(sb)->s_qf_names[type]) {
3352 /* Quotafile not in fs root? */
3353 if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
3354 printk(KERN_WARNING
3355 "EXT4-fs: Quota file not on filesystem root. "
3356 "Journaled quota will not work.\n");
3360 * When we journal data on quota file, we have to flush journal to see
3361 * all updates to the file when we bypass pagecache...
3363 if (ext4_should_journal_data(nd.path.dentry->d_inode)) {
3365 * We don't need to lock updates but journal_flush() could
3366 * otherwise be livelocked...
3368 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3369 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3370 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3373 err = vfs_quota_on_path(sb, type, format_id, &nd.path);
3374 path_put(&nd.path);
3375 return err;
3378 /* Read data from quotafile - avoid pagecache and such because we cannot afford
3379 * acquiring the locks... As quota files are never truncated and quota code
3380 * itself serializes the operations (and noone else should touch the files)
3381 * we don't have to be afraid of races */
3382 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
3383 size_t len, loff_t off)
3385 struct inode *inode = sb_dqopt(sb)->files[type];
3386 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3387 int err = 0;
3388 int offset = off & (sb->s_blocksize - 1);
3389 int tocopy;
3390 size_t toread;
3391 struct buffer_head *bh;
3392 loff_t i_size = i_size_read(inode);
3394 if (off > i_size)
3395 return 0;
3396 if (off+len > i_size)
3397 len = i_size-off;
3398 toread = len;
3399 while (toread > 0) {
3400 tocopy = sb->s_blocksize - offset < toread ?
3401 sb->s_blocksize - offset : toread;
3402 bh = ext4_bread(NULL, inode, blk, 0, &err);
3403 if (err)
3404 return err;
3405 if (!bh) /* A hole? */
3406 memset(data, 0, tocopy);
3407 else
3408 memcpy(data, bh->b_data+offset, tocopy);
3409 brelse(bh);
3410 offset = 0;
3411 toread -= tocopy;
3412 data += tocopy;
3413 blk++;
3415 return len;
3418 /* Write to quotafile (we know the transaction is already started and has
3419 * enough credits) */
3420 static ssize_t ext4_quota_write(struct super_block *sb, int type,
3421 const char *data, size_t len, loff_t off)
3423 struct inode *inode = sb_dqopt(sb)->files[type];
3424 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
3425 int err = 0;
3426 int offset = off & (sb->s_blocksize - 1);
3427 int tocopy;
3428 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
3429 size_t towrite = len;
3430 struct buffer_head *bh;
3431 handle_t *handle = journal_current_handle();
3433 if (!handle) {
3434 printk(KERN_WARNING "EXT4-fs: Quota write (off=%Lu, len=%Lu)"
3435 " cancelled because transaction is not started.\n",
3436 (unsigned long long)off, (unsigned long long)len);
3437 return -EIO;
3439 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3440 while (towrite > 0) {
3441 tocopy = sb->s_blocksize - offset < towrite ?
3442 sb->s_blocksize - offset : towrite;
3443 bh = ext4_bread(handle, inode, blk, 1, &err);
3444 if (!bh)
3445 goto out;
3446 if (journal_quota) {
3447 err = ext4_journal_get_write_access(handle, bh);
3448 if (err) {
3449 brelse(bh);
3450 goto out;
3453 lock_buffer(bh);
3454 memcpy(bh->b_data+offset, data, tocopy);
3455 flush_dcache_page(bh->b_page);
3456 unlock_buffer(bh);
3457 if (journal_quota)
3458 err = ext4_journal_dirty_metadata(handle, bh);
3459 else {
3460 /* Always do at least ordered writes for quotas */
3461 err = ext4_jbd2_file_inode(handle, inode);
3462 mark_buffer_dirty(bh);
3464 brelse(bh);
3465 if (err)
3466 goto out;
3467 offset = 0;
3468 towrite -= tocopy;
3469 data += tocopy;
3470 blk++;
3472 out:
3473 if (len == towrite) {
3474 mutex_unlock(&inode->i_mutex);
3475 return err;
3477 if (inode->i_size < off+len-towrite) {
3478 i_size_write(inode, off+len-towrite);
3479 EXT4_I(inode)->i_disksize = inode->i_size;
3481 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3482 ext4_mark_inode_dirty(handle, inode);
3483 mutex_unlock(&inode->i_mutex);
3484 return len - towrite;
3487 #endif
3489 static int ext4_get_sb(struct file_system_type *fs_type,
3490 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3492 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3495 static struct file_system_type ext4dev_fs_type = {
3496 .owner = THIS_MODULE,
3497 .name = "ext4dev",
3498 .get_sb = ext4_get_sb,
3499 .kill_sb = kill_block_super,
3500 .fs_flags = FS_REQUIRES_DEV,
3503 static int __init init_ext4_fs(void)
3505 int err;
3507 err = init_ext4_mballoc();
3508 if (err)
3509 return err;
3511 err = init_ext4_xattr();
3512 if (err)
3513 goto out2;
3514 err = init_inodecache();
3515 if (err)
3516 goto out1;
3517 err = register_filesystem(&ext4dev_fs_type);
3518 if (err)
3519 goto out;
3520 return 0;
3521 out:
3522 destroy_inodecache();
3523 out1:
3524 exit_ext4_xattr();
3525 out2:
3526 exit_ext4_mballoc();
3527 return err;
3530 static void __exit exit_ext4_fs(void)
3532 unregister_filesystem(&ext4dev_fs_type);
3533 destroy_inodecache();
3534 exit_ext4_xattr();
3535 exit_ext4_mballoc();
3538 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3539 MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
3540 MODULE_LICENSE("GPL");
3541 module_init(init_ext4_fs)
3542 module_exit(exit_ext4_fs)