xfs: remove duplicated #include
[safe/jmp/linux-2.6] / fs / ext2 / super.c
index a6c4d6e..42e4a30 100644 (file)
@@ -16,7 +16,6 @@
  *        David S. Miller (davem@caip.rutgers.edu), 1995
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/string.h>
 #include <linux/fs.h>
 #include <linux/parser.h>
 #include <linux/random.h>
 #include <linux/buffer_head.h>
+#include <linux/exportfs.h>
 #include <linux/smp_lock.h>
 #include <linux/vfs.h>
 #include <linux/seq_file.h>
 #include <linux/mount.h>
+#include <linux/log2.h>
+#include <linux/quotaops.h>
 #include <asm/uaccess.h>
 #include "ext2.h"
 #include "xattr.h"
@@ -40,6 +42,7 @@ static void ext2_sync_super(struct super_block *sb,
                            struct ext2_super_block *es);
 static int ext2_remount (struct super_block * sb, int * flags, char * data);
 static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf);
+static int ext2_sync_fs(struct super_block *sb, int wait);
 
 void ext2_error (struct super_block * sb, const char * function,
                 const char * fmt, ...)
@@ -50,33 +53,32 @@ void ext2_error (struct super_block * sb, const char * function,
 
        if (!(sb->s_flags & MS_RDONLY)) {
                sbi->s_mount_state |= EXT2_ERROR_FS;
-               es->s_state =
-                       cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
+               es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
                ext2_sync_super(sb, es);
        }
 
        va_start(args, fmt);
-       printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
+       printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
        vprintk(fmt, args);
        printk("\n");
        va_end(args);
 
        if (test_opt(sb, ERRORS_PANIC))
-               panic("EXT2-fs panic from previous error\n");
+               panic("EXT2-fs: panic from previous error\n");
        if (test_opt(sb, ERRORS_RO)) {
-               printk("Remounting filesystem read-only\n");
+               ext2_msg(sb, KERN_CRIT,
+                            "error: remounting filesystem read-only");
                sb->s_flags |= MS_RDONLY;
        }
 }
 
-void ext2_warning (struct super_block * sb, const char * function,
-                  const char * fmt, ...)
+void ext2_msg(struct super_block *sb, const char *prefix,
+               const char *fmt, ...)
 {
        va_list args;
 
        va_start(args, fmt);
-       printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
-              sb->s_id, function);
+       printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
        vprintk(fmt, args);
        printk("\n");
        va_end(args);
@@ -89,9 +91,9 @@ void ext2_update_dynamic_rev(struct super_block *sb)
        if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
                return;
 
-       ext2_warning(sb, __FUNCTION__,
-                    "updating to rev %d because of new feature flag, "
-                    "running e2fsck is recommended",
+       ext2_msg(sb, KERN_WARNING,
+                    "warning: updating to rev %d because of "
+                    "new feature flag, running e2fsck is recommended",
                     EXT2_DYNAMIC_REV);
 
        es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
@@ -113,6 +115,11 @@ static void ext2_put_super (struct super_block * sb)
        int i;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
 
+       lock_kernel();
+
+       if (sb->s_dirt)
+               ext2_write_super(sb);
+
        ext2_xattr_put_super(sb);
        if (!(sb->s_flags & MS_RDONLY)) {
                struct ext2_super_block *es = sbi->s_es;
@@ -131,23 +138,21 @@ static void ext2_put_super (struct super_block * sb)
        percpu_counter_destroy(&sbi->s_dirs_counter);
        brelse (sbi->s_sbh);
        sb->s_fs_info = NULL;
+       kfree(sbi->s_blockgroup_lock);
        kfree(sbi);
 
-       return;
+       unlock_kernel();
 }
 
-static kmem_cache_t * ext2_inode_cachep;
+static struct kmem_cache * ext2_inode_cachep;
 
 static struct inode *ext2_alloc_inode(struct super_block *sb)
 {
        struct ext2_inode_info *ei;
-       ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
+       ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, GFP_KERNEL);
        if (!ei)
                return NULL;
-#ifdef CONFIG_EXT2_FS_POSIX_ACL
-       ei->i_acl = EXT2_ACL_NOT_CACHED;
-       ei->i_default_acl = EXT2_ACL_NOT_CACHED;
-#endif
+       ei->i_block_alloc_info = NULL;
        ei->vfs_inode.i_version = 1;
        return &ei->vfs_inode;
 }
@@ -157,27 +162,25 @@ static void ext2_destroy_inode(struct inode *inode)
        kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
 }
 
-static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
+static void init_once(void *foo)
 {
        struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
 
-       if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
-           SLAB_CTOR_CONSTRUCTOR) {
-               rwlock_init(&ei->i_meta_lock);
+       rwlock_init(&ei->i_meta_lock);
 #ifdef CONFIG_EXT2_FS_XATTR
-               init_rwsem(&ei->xattr_sem);
+       init_rwsem(&ei->xattr_sem);
 #endif
-               inode_init_once(&ei->vfs_inode);
-       }
+       mutex_init(&ei->truncate_mutex);
+       inode_init_once(&ei->vfs_inode);
 }
+
 static int init_inodecache(void)
 {
        ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
                                             sizeof(struct ext2_inode_info),
                                             0, (SLAB_RECLAIM_ACCOUNT|
                                                SLAB_MEM_SPREAD),
-                                            init_once, NULL);
+                                            init_once);
        if (ext2_inode_cachep == NULL)
                return -ENOMEM;
        return 0;
@@ -185,32 +188,82 @@ static int init_inodecache(void)
 
 static void destroy_inodecache(void)
 {
-       if (kmem_cache_destroy(ext2_inode_cachep))
-               printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
+       kmem_cache_destroy(ext2_inode_cachep);
 }
 
 static void ext2_clear_inode(struct inode *inode)
 {
-#ifdef CONFIG_EXT2_FS_POSIX_ACL
-       struct ext2_inode_info *ei = EXT2_I(inode);
+       struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
 
-       if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
-               posix_acl_release(ei->i_acl);
-               ei->i_acl = EXT2_ACL_NOT_CACHED;
-       }
-       if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
-               posix_acl_release(ei->i_default_acl);
-               ei->i_default_acl = EXT2_ACL_NOT_CACHED;
-       }
-#endif
+       dquot_drop(inode);
+       ext2_discard_reservation(inode);
+       EXT2_I(inode)->i_block_alloc_info = NULL;
+       if (unlikely(rsv))
+               kfree(rsv);
 }
 
 static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
 {
-       struct ext2_sb_info *sbi = EXT2_SB(vfs->mnt_sb);
+       struct super_block *sb = vfs->mnt_sb;
+       struct ext2_sb_info *sbi = EXT2_SB(sb);
+       struct ext2_super_block *es = sbi->s_es;
+       unsigned long def_mount_opts;
+
+       def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
 
-       if (sbi->s_mount_opt & EXT2_MOUNT_GRPID)
+       if (sbi->s_sb_block != 1)
+               seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
+       if (test_opt(sb, MINIX_DF))
+               seq_puts(seq, ",minixdf");
+       if (test_opt(sb, GRPID))
                seq_puts(seq, ",grpid");
+       if (!test_opt(sb, GRPID) && (def_mount_opts & EXT2_DEFM_BSDGROUPS))
+               seq_puts(seq, ",nogrpid");
+       if (sbi->s_resuid != EXT2_DEF_RESUID ||
+           le16_to_cpu(es->s_def_resuid) != EXT2_DEF_RESUID) {
+               seq_printf(seq, ",resuid=%u", sbi->s_resuid);
+       }
+       if (sbi->s_resgid != EXT2_DEF_RESGID ||
+           le16_to_cpu(es->s_def_resgid) != EXT2_DEF_RESGID) {
+               seq_printf(seq, ",resgid=%u", sbi->s_resgid);
+       }
+       if (test_opt(sb, ERRORS_RO)) {
+               int def_errors = le16_to_cpu(es->s_errors);
+
+               if (def_errors == EXT2_ERRORS_PANIC ||
+                   def_errors == EXT2_ERRORS_CONTINUE) {
+                       seq_puts(seq, ",errors=remount-ro");
+               }
+       }
+       if (test_opt(sb, ERRORS_CONT))
+               seq_puts(seq, ",errors=continue");
+       if (test_opt(sb, ERRORS_PANIC))
+               seq_puts(seq, ",errors=panic");
+       if (test_opt(sb, NO_UID32))
+               seq_puts(seq, ",nouid32");
+       if (test_opt(sb, DEBUG))
+               seq_puts(seq, ",debug");
+       if (test_opt(sb, OLDALLOC))
+               seq_puts(seq, ",oldalloc");
+
+#ifdef CONFIG_EXT2_FS_XATTR
+       if (test_opt(sb, XATTR_USER))
+               seq_puts(seq, ",user_xattr");
+       if (!test_opt(sb, XATTR_USER) &&
+           (def_mount_opts & EXT2_DEFM_XATTR_USER)) {
+               seq_puts(seq, ",nouser_xattr");
+       }
+#endif
+
+#ifdef CONFIG_EXT2_FS_POSIX_ACL
+       if (test_opt(sb, POSIX_ACL))
+               seq_puts(seq, ",acl");
+       if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT2_DEFM_ACL))
+               seq_puts(seq, ",noacl");
+#endif
+
+       if (test_opt(sb, NOBH))
+               seq_puts(seq, ",nobh");
 
 #if defined(CONFIG_QUOTA)
        if (sbi->s_mount_opt & EXT2_MOUNT_USRQUOTA)
@@ -225,6 +278,9 @@ static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
                seq_puts(seq, ",xip");
 #endif
 
+       if (!test_opt(sb, RESERVATION))
+               seq_puts(seq, ",noreservation");
+
        return 0;
 }
 
@@ -233,15 +289,14 @@ static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, siz
 static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
 #endif
 
-static struct super_operations ext2_sops = {
+static const struct super_operations ext2_sops = {
        .alloc_inode    = ext2_alloc_inode,
        .destroy_inode  = ext2_destroy_inode,
-       .read_inode     = ext2_read_inode,
        .write_inode    = ext2_write_inode,
-       .put_inode      = ext2_put_inode,
        .delete_inode   = ext2_delete_inode,
        .put_super      = ext2_put_super,
        .write_super    = ext2_write_super,
+       .sync_fs        = ext2_sync_fs,
        .statfs         = ext2_statfs,
        .remount_fs     = ext2_remount,
        .clear_inode    = ext2_clear_inode,
@@ -252,12 +307,54 @@ static struct super_operations ext2_sops = {
 #endif
 };
 
+static struct inode *ext2_nfs_get_inode(struct super_block *sb,
+               u64 ino, u32 generation)
+{
+       struct inode *inode;
+
+       if (ino < EXT2_FIRST_INO(sb) && ino != EXT2_ROOT_INO)
+               return ERR_PTR(-ESTALE);
+       if (ino > le32_to_cpu(EXT2_SB(sb)->s_es->s_inodes_count))
+               return ERR_PTR(-ESTALE);
+
+       /* iget isn't really right if the inode is currently unallocated!!
+        * ext2_read_inode currently does appropriate checks, but
+        * it might be "neater" to call ext2_get_inode first and check
+        * if the inode is valid.....
+        */
+       inode = ext2_iget(sb, ino);
+       if (IS_ERR(inode))
+               return ERR_CAST(inode);
+       if (generation && inode->i_generation != generation) {
+               /* we didn't find the right inode.. */
+               iput(inode);
+               return ERR_PTR(-ESTALE);
+       }
+       return inode;
+}
+
+static struct dentry *ext2_fh_to_dentry(struct super_block *sb, struct fid *fid,
+               int fh_len, int fh_type)
+{
+       return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
+                                   ext2_nfs_get_inode);
+}
+
+static struct dentry *ext2_fh_to_parent(struct super_block *sb, struct fid *fid,
+               int fh_len, int fh_type)
+{
+       return generic_fh_to_parent(sb, fid, fh_len, fh_type,
+                                   ext2_nfs_get_inode);
+}
+
 /* Yes, most of these are left as NULL!!
  * A NULL value implies the default, which works with ext2-like file
  * systems, but can be improved upon.
  * Currently only get_parent is required.
  */
-static struct export_operations ext2_export_ops = {
+static const struct export_operations ext2_export_ops = {
+       .fh_to_dentry = ext2_fh_to_dentry,
+       .fh_to_parent = ext2_fh_to_parent,
        .get_parent = ext2_get_parent,
 };
 
@@ -287,10 +384,10 @@ enum {
        Opt_err_ro, Opt_nouid32, Opt_nocheck, Opt_debug,
        Opt_oldalloc, Opt_orlov, Opt_nobh, Opt_user_xattr, Opt_nouser_xattr,
        Opt_acl, Opt_noacl, Opt_xip, Opt_ignore, Opt_err, Opt_quota,
-       Opt_usrquota, Opt_grpquota
+       Opt_usrquota, Opt_grpquota, Opt_reservation, Opt_noreservation
 };
 
-static match_table_t tokens = {
+static const match_table_t tokens = {
        {Opt_bsd_df, "bsddf"},
        {Opt_minix_df, "minixdf"},
        {Opt_grpid, "grpid"},
@@ -319,15 +416,16 @@ static match_table_t tokens = {
        {Opt_ignore, "noquota"},
        {Opt_quota, "quota"},
        {Opt_usrquota, "usrquota"},
+       {Opt_reservation, "reservation"},
+       {Opt_noreservation, "noreservation"},
        {Opt_err, NULL}
 };
 
-static int parse_options (char * options,
-                         struct ext2_sb_info *sbi)
+static int parse_options(char *options, struct super_block *sb)
 {
-       char * p;
+       char *p;
+       struct ext2_sb_info *sbi = EXT2_SB(sb);
        substring_t args[MAX_OPT_ARGS];
-       unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
        int option;
 
        if (!options)
@@ -367,13 +465,19 @@ static int parse_options (char * options,
                        /* *sb_block = match_int(&args[0]); */
                        break;
                case Opt_err_panic:
-                       kind = EXT2_MOUNT_ERRORS_PANIC;
+                       clear_opt (sbi->s_mount_opt, ERRORS_CONT);
+                       clear_opt (sbi->s_mount_opt, ERRORS_RO);
+                       set_opt (sbi->s_mount_opt, ERRORS_PANIC);
                        break;
                case Opt_err_ro:
-                       kind = EXT2_MOUNT_ERRORS_RO;
+                       clear_opt (sbi->s_mount_opt, ERRORS_CONT);
+                       clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
+                       set_opt (sbi->s_mount_opt, ERRORS_RO);
                        break;
                case Opt_err_cont:
-                       kind = EXT2_MOUNT_ERRORS_CONT;
+                       clear_opt (sbi->s_mount_opt, ERRORS_RO);
+                       clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
+                       set_opt (sbi->s_mount_opt, ERRORS_CONT);
                        break;
                case Opt_nouid32:
                        set_opt (sbi->s_mount_opt, NO_UID32);
@@ -403,7 +507,8 @@ static int parse_options (char * options,
 #else
                case Opt_user_xattr:
                case Opt_nouser_xattr:
-                       printk("EXT2 (no)user_xattr options not supported\n");
+                       ext2_msg(sb, KERN_INFO, "(no)user_xattr options"
+                               "not supported");
                        break;
 #endif
 #ifdef CONFIG_EXT2_FS_POSIX_ACL
@@ -416,14 +521,15 @@ static int parse_options (char * options,
 #else
                case Opt_acl:
                case Opt_noacl:
-                       printk("EXT2 (no)acl options not supported\n");
+                       ext2_msg(sb, KERN_INFO,
+                               "(no)acl options not supported");
                        break;
 #endif
                case Opt_xip:
 #ifdef CONFIG_EXT2_FS_XIP
                        set_opt (sbi->s_mount_opt, XIP);
 #else
-                       printk("EXT2 xip option not supported\n");
+                       ext2_msg(sb, KERN_INFO, "xip option not supported");
 #endif
                        break;
 
@@ -440,19 +546,25 @@ static int parse_options (char * options,
                case Opt_quota:
                case Opt_usrquota:
                case Opt_grpquota:
-                       printk(KERN_ERR
-                               "EXT2-fs: quota operations not supported.\n");
-
+                       ext2_msg(sb, KERN_INFO,
+                               "quota operations not supported");
                        break;
 #endif
 
+               case Opt_reservation:
+                       set_opt(sbi->s_mount_opt, RESERVATION);
+                       ext2_msg(sb, KERN_INFO, "reservations ON");
+                       break;
+               case Opt_noreservation:
+                       clear_opt(sbi->s_mount_opt, RESERVATION);
+                       ext2_msg(sb, KERN_INFO, "reservations OFF");
+                       break;
                case Opt_ignore:
                        break;
                default:
                        return 0;
                }
        }
-       sbi->s_mount_opt |= kind;
        return 1;
 }
 
@@ -464,34 +576,40 @@ static int ext2_setup_super (struct super_block * sb,
        struct ext2_sb_info *sbi = EXT2_SB(sb);
 
        if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
-               printk ("EXT2-fs warning: revision level too high, "
-                       "forcing read-only mode\n");
+               ext2_msg(sb, KERN_ERR,
+                       "error: revision level too high, "
+                       "forcing read-only mode");
                res = MS_RDONLY;
        }
        if (read_only)
                return res;
        if (!(sbi->s_mount_state & EXT2_VALID_FS))
-               printk ("EXT2-fs warning: mounting unchecked fs, "
-                       "running e2fsck is recommended\n");
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: mounting unchecked fs, "
+                       "running e2fsck is recommended");
        else if ((sbi->s_mount_state & EXT2_ERROR_FS))
-               printk ("EXT2-fs warning: mounting fs with errors, "
-                       "running e2fsck is recommended\n");
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: mounting fs with errors, "
+                       "running e2fsck is recommended");
        else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
                 le16_to_cpu(es->s_mnt_count) >=
                 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
-               printk ("EXT2-fs warning: maximal mount count reached, "
-                       "running e2fsck is recommended\n");
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: maximal mount count reached, "
+                       "running e2fsck is recommended");
        else if (le32_to_cpu(es->s_checkinterval) &&
-               (le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
-               printk ("EXT2-fs warning: checktime reached, "
-                       "running e2fsck is recommended\n");
+               (le32_to_cpu(es->s_lastcheck) +
+                       le32_to_cpu(es->s_checkinterval) <= get_seconds()))
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: checktime reached, "
+                       "running e2fsck is recommended");
        if (!le16_to_cpu(es->s_max_mnt_count))
                es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
-       es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
+       le16_add_cpu(&es->s_mnt_count, 1);
        ext2_write_super(sb);
        if (test_opt (sb, DEBUG))
-               printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
-                       "bpg=%lu, ipg=%lu, mo=%04lx]\n",
+               ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
+                       "bpg=%lu, ipg=%lu, mo=%04lx]",
                        EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
                        sbi->s_frag_size,
                        sbi->s_groups_count,
@@ -501,22 +619,26 @@ static int ext2_setup_super (struct super_block * sb,
        return res;
 }
 
-static int ext2_check_descriptors (struct super_block * sb)
+static int ext2_check_descriptors(struct super_block *sb)
 {
        int i;
-       int desc_block = 0;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
-       unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
-       struct ext2_group_desc * gdp = NULL;
 
        ext2_debug ("Checking group descriptors");
 
-       for (i = 0; i < sbi->s_groups_count; i++)
-       {
-               if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
-                       gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
-               if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
-                   le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
+       for (i = 0; i < sbi->s_groups_count; i++) {
+               struct ext2_group_desc *gdp = ext2_get_group_desc(sb, i, NULL);
+               ext2_fsblk_t first_block = ext2_group_first_block_no(sb, i);
+               ext2_fsblk_t last_block;
+
+               if (i == sbi->s_groups_count - 1)
+                       last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
+               else
+                       last_block = first_block +
+                               (EXT2_BLOCKS_PER_GROUP(sb) - 1);
+
+               if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
+                   le32_to_cpu(gdp->bg_block_bitmap) > last_block)
                {
                        ext2_error (sb, "ext2_check_descriptors",
                                    "Block bitmap for group %d"
@@ -524,8 +646,8 @@ static int ext2_check_descriptors (struct super_block * sb)
                                    i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
                        return 0;
                }
-               if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
-                   le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
+               if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
+                   le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
                {
                        ext2_error (sb, "ext2_check_descriptors",
                                    "Inode bitmap for group %d"
@@ -533,9 +655,9 @@ static int ext2_check_descriptors (struct super_block * sb)
                                    i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
                        return 0;
                }
-               if (le32_to_cpu(gdp->bg_inode_table) < block ||
-                   le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
-                   block + EXT2_BLOCKS_PER_GROUP(sb))
+               if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
+                   le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
+                   last_block)
                {
                        ext2_error (sb, "ext2_check_descriptors",
                                    "Inode table for group %d"
@@ -543,14 +665,10 @@ static int ext2_check_descriptors (struct super_block * sb)
                                    i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
                        return 0;
                }
-               block += EXT2_BLOCKS_PER_GROUP(sb);
-               gdp++;
        }
        return 1;
 }
 
-#define log2(n) ffz(~(n))
 /*
  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
@@ -559,11 +677,31 @@ static int ext2_check_descriptors (struct super_block * sb)
 static loff_t ext2_max_size(int bits)
 {
        loff_t res = EXT2_NDIR_BLOCKS;
-       /* This constant is calculated to be the largest file size for a
-        * dense, 4k-blocksize file such that the total number of
+       int meta_blocks;
+       loff_t upper_limit;
+
+       /* This is calculated to be the largest file size for a
+        * dense, file such that the total number of
         * sectors in the file, including data and all indirect blocks,
-        * does not exceed 2^32. */
-       const loff_t upper_limit = 0x1ff7fffd000LL;
+        * does not exceed 2^32 -1
+        * __u32 i_blocks representing the total number of
+        * 512 bytes blocks of the file
+        */
+       upper_limit = (1LL << 32) - 1;
+
+       /* total blocks in file system block size */
+       upper_limit >>= (bits - 9);
+
+
+       /* indirect blocks */
+       meta_blocks = 1;
+       /* double indirect blocks */
+       meta_blocks += 1 + (1LL << (bits-2));
+       /* tripple indirect blocks */
+       meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
+
+       upper_limit -= meta_blocks;
+       upper_limit <<= bits;
 
        res += 1LL << (bits-2);
        res += 1LL << (2*(bits-2));
@@ -571,6 +709,10 @@ static loff_t ext2_max_size(int bits)
        res <<= bits;
        if (res > upper_limit)
                res = upper_limit;
+
+       if (res > MAX_LFS_FILESIZE)
+               res = MAX_LFS_FILESIZE;
+
        return res;
 }
 
@@ -579,10 +721,9 @@ static unsigned long descriptor_loc(struct super_block *sb,
                                    int nr)
 {
        struct ext2_sb_info *sbi = EXT2_SB(sb);
-       unsigned long bg, first_data_block, first_meta_bg;
+       unsigned long bg, first_meta_bg;
        int has_super = 0;
        
-       first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
        first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
 
        if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
@@ -591,7 +732,8 @@ static unsigned long descriptor_loc(struct super_block *sb,
        bg = sbi->s_desc_per_block * nr;
        if (ext2_bg_has_super(sb, bg))
                has_super = 1;
-       return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
+
+       return ext2_group_first_block_no(sb, bg) + has_super;
 }
 
 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
@@ -605,16 +747,25 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        unsigned long logic_sb_block;
        unsigned long offset = 0;
        unsigned long def_mount_opts;
+       long ret = -EINVAL;
        int blocksize = BLOCK_SIZE;
        int db_count;
        int i, j;
        __le32 features;
+       int err;
 
-       sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
+       sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
        if (!sbi)
                return -ENOMEM;
+
+       sbi->s_blockgroup_lock =
+               kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
+       if (!sbi->s_blockgroup_lock) {
+               kfree(sbi);
+               return -ENOMEM;
+       }
        sb->s_fs_info = sbi;
-       memset(sbi, 0, sizeof(*sbi));
+       sbi->s_sb_block = sb_block;
 
        /*
         * See what the current blocksize for the device is, and
@@ -625,7 +776,7 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
         */
        blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
        if (!blocksize) {
-               printk ("EXT2-fs: unable to set blocksize\n");
+               ext2_msg(sb, KERN_ERR, "error: unable to set blocksize");
                goto failed_sbi;
        }
 
@@ -641,7 +792,7 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        }
 
        if (!(bh = sb_bread(sb, logic_sb_block))) {
-               printk ("EXT2-fs: unable to read superblock\n");
+               ext2_msg(sb, KERN_ERR, "error: unable to read superblock");
                goto failed_sbi;
        }
        /*
@@ -663,20 +814,28 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                set_opt(sbi->s_mount_opt, GRPID);
        if (def_mount_opts & EXT2_DEFM_UID16)
                set_opt(sbi->s_mount_opt, NO_UID32);
+#ifdef CONFIG_EXT2_FS_XATTR
        if (def_mount_opts & EXT2_DEFM_XATTR_USER)
                set_opt(sbi->s_mount_opt, XATTR_USER);
+#endif
+#ifdef CONFIG_EXT2_FS_POSIX_ACL
        if (def_mount_opts & EXT2_DEFM_ACL)
                set_opt(sbi->s_mount_opt, POSIX_ACL);
+#endif
        
        if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
                set_opt(sbi->s_mount_opt, ERRORS_PANIC);
-       else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
+       else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_CONTINUE)
+               set_opt(sbi->s_mount_opt, ERRORS_CONT);
+       else
                set_opt(sbi->s_mount_opt, ERRORS_RO);
 
        sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
        sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
        
-       if (!parse_options ((char *) data, sbi))
+       set_opt(sbi->s_mount_opt, RESERVATION);
+
+       if (!parse_options((char *) data, sb))
                goto failed_mount;
 
        sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
@@ -690,8 +849,9 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
            (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
             EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
             EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
-               printk("EXT2-fs warning: feature flags set on rev 0 fs, "
-                      "running e2fsck is recommended\n");
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: feature flags set on rev 0 fs, "
+                       "running e2fsck is recommended");
        /*
         * Check feature flags regardless of the revision level, since we
         * previously didn't change the revision level when setting the flags,
@@ -699,25 +859,25 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
         */
        features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
        if (features) {
-               printk("EXT2-fs: %s: couldn't mount because of "
-                      "unsupported optional features (%x).\n",
-                      sb->s_id, le32_to_cpu(features));
+               ext2_msg(sb, KERN_ERR,  "error: couldn't mount because of "
+                      "unsupported optional features (%x)",
+                       le32_to_cpu(features));
                goto failed_mount;
        }
        if (!(sb->s_flags & MS_RDONLY) &&
            (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
-               printk("EXT2-fs: %s: couldn't mount RDWR because of "
-                      "unsupported optional features (%x).\n",
-                      sb->s_id, le32_to_cpu(features));
+               ext2_msg(sb, KERN_ERR, "error: couldn't mount RDWR because of "
+                      "unsupported optional features (%x)",
+                      le32_to_cpu(features));
                goto failed_mount;
        }
 
        blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
 
-       if ((ext2_use_xip(sb)) && ((blocksize != PAGE_SIZE) ||
-                                 (sb->s_blocksize != blocksize))) {
+       if (ext2_use_xip(sb) && blocksize != PAGE_SIZE) {
                if (!silent)
-                       printk("XIP: Unsupported blocksize\n");
+                       ext2_msg(sb, KERN_ERR,
+                               "error: unsupported blocksize for xip");
                goto failed_mount;
        }
 
@@ -726,7 +886,7 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                brelse(bh);
 
                if (!sb_set_blocksize(sb, blocksize)) {
-                       printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
+                       ext2_msg(sb, KERN_ERR, "error: blocksize is too small");
                        goto failed_sbi;
                }
 
@@ -734,14 +894,14 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                offset = (sb_block*BLOCK_SIZE) % blocksize;
                bh = sb_bread(sb, logic_sb_block);
                if(!bh) {
-                       printk("EXT2-fs: Couldn't read superblock on "
-                              "2nd try.\n");
+                       ext2_msg(sb, KERN_ERR, "error: couldn't read"
+                               "superblock on 2nd try");
                        goto failed_sbi;
                }
                es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
                sbi->s_es = es;
                if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
-                       printk ("EXT2-fs: Magic mismatch, very weird !\n");
+                       ext2_msg(sb, KERN_ERR, "error: magic mismatch");
                        goto failed_mount;
                }
        }
@@ -755,9 +915,10 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
                sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
                if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
-                   (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
+                   !is_power_of_2(sbi->s_inode_size) ||
                    (sbi->s_inode_size > blocksize)) {
-                       printk ("EXT2-fs: unsupported inode size: %d\n",
+                       ext2_msg(sb, KERN_ERR,
+                               "error: unsupported inode size: %d",
                                sbi->s_inode_size);
                        goto failed_mount;
                }
@@ -776,7 +937,7 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        if (EXT2_INODE_SIZE(sb) == 0)
                goto cantfind_ext2;
        sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
-       if (sbi->s_inodes_per_block == 0)
+       if (sbi->s_inodes_per_block == 0 || sbi->s_inodes_per_group == 0)
                goto cantfind_ext2;
        sbi->s_itb_per_group = sbi->s_inodes_per_group /
                                        sbi->s_inodes_per_block;
@@ -785,121 +946,152 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        sbi->s_sbh = bh;
        sbi->s_mount_state = le16_to_cpu(es->s_state);
        sbi->s_addr_per_block_bits =
-               log2 (EXT2_ADDR_PER_BLOCK(sb));
+               ilog2 (EXT2_ADDR_PER_BLOCK(sb));
        sbi->s_desc_per_block_bits =
-               log2 (EXT2_DESC_PER_BLOCK(sb));
+               ilog2 (EXT2_DESC_PER_BLOCK(sb));
 
        if (sb->s_magic != EXT2_SUPER_MAGIC)
                goto cantfind_ext2;
 
        if (sb->s_blocksize != bh->b_size) {
                if (!silent)
-                       printk ("VFS: Unsupported blocksize on dev "
-                               "%s.\n", sb->s_id);
+                       ext2_msg(sb, KERN_ERR, "error: unsupported blocksize");
                goto failed_mount;
        }
 
        if (sb->s_blocksize != sbi->s_frag_size) {
-               printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
+               ext2_msg(sb, KERN_ERR,
+                       "error: fragsize %lu != blocksize %lu"
+                       "(not supported yet)",
                        sbi->s_frag_size, sb->s_blocksize);
                goto failed_mount;
        }
 
        if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
-               printk ("EXT2-fs: #blocks per group too big: %lu\n",
+               ext2_msg(sb, KERN_ERR,
+                       "error: #blocks per group too big: %lu",
                        sbi->s_blocks_per_group);
                goto failed_mount;
        }
        if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
-               printk ("EXT2-fs: #fragments per group too big: %lu\n",
+               ext2_msg(sb, KERN_ERR,
+                       "error: #fragments per group too big: %lu",
                        sbi->s_frags_per_group);
                goto failed_mount;
        }
        if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
-               printk ("EXT2-fs: #inodes per group too big: %lu\n",
+               ext2_msg(sb, KERN_ERR,
+                       "error: #inodes per group too big: %lu",
                        sbi->s_inodes_per_group);
                goto failed_mount;
        }
 
        if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
                goto cantfind_ext2;
-       sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
-                                       le32_to_cpu(es->s_first_data_block) +
-                                      EXT2_BLOCKS_PER_GROUP(sb) - 1) /
-                                      EXT2_BLOCKS_PER_GROUP(sb);
+       sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
+                               le32_to_cpu(es->s_first_data_block) - 1)
+                                       / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
        db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
                   EXT2_DESC_PER_BLOCK(sb);
        sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
        if (sbi->s_group_desc == NULL) {
-               printk ("EXT2-fs: not enough memory\n");
+               ext2_msg(sb, KERN_ERR, "error: not enough memory");
                goto failed_mount;
        }
-       percpu_counter_init(&sbi->s_freeblocks_counter);
-       percpu_counter_init(&sbi->s_freeinodes_counter);
-       percpu_counter_init(&sbi->s_dirs_counter);
-       bgl_lock_init(&sbi->s_blockgroup_lock);
-       sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
-                              GFP_KERNEL);
+       bgl_lock_init(sbi->s_blockgroup_lock);
+       sbi->s_debts = kcalloc(sbi->s_groups_count, sizeof(*sbi->s_debts), GFP_KERNEL);
        if (!sbi->s_debts) {
-               printk ("EXT2-fs: not enough memory\n");
+               ext2_msg(sb, KERN_ERR, "error: not enough memory");
                goto failed_mount_group_desc;
        }
-       memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
        for (i = 0; i < db_count; i++) {
                block = descriptor_loc(sb, logic_sb_block, i);
                sbi->s_group_desc[i] = sb_bread(sb, block);
                if (!sbi->s_group_desc[i]) {
                        for (j = 0; j < i; j++)
                                brelse (sbi->s_group_desc[j]);
-                       printk ("EXT2-fs: unable to read group descriptors\n");
+                       ext2_msg(sb, KERN_ERR,
+                               "error: unable to read group descriptors");
                        goto failed_mount_group_desc;
                }
        }
        if (!ext2_check_descriptors (sb)) {
-               printk ("EXT2-fs: group descriptors corrupted!\n");
-               db_count = i;
+               ext2_msg(sb, KERN_ERR, "group descriptors corrupted");
                goto failed_mount2;
        }
        sbi->s_gdb_count = db_count;
        get_random_bytes(&sbi->s_next_generation, sizeof(u32));
        spin_lock_init(&sbi->s_next_gen_lock);
+
+       /* per fileystem reservation list head & lock */
+       spin_lock_init(&sbi->s_rsv_window_lock);
+       sbi->s_rsv_window_root = RB_ROOT;
+       /*
+        * Add a single, static dummy reservation to the start of the
+        * reservation window list --- it gives us a placeholder for
+        * append-at-start-of-list which makes the allocation logic
+        * _much_ simpler.
+        */
+       sbi->s_rsv_window_head.rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
+       sbi->s_rsv_window_head.rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
+       sbi->s_rsv_window_head.rsv_alloc_hit = 0;
+       sbi->s_rsv_window_head.rsv_goal_size = 0;
+       ext2_rsv_window_add(sb, &sbi->s_rsv_window_head);
+
+       err = percpu_counter_init(&sbi->s_freeblocks_counter,
+                               ext2_count_free_blocks(sb));
+       if (!err) {
+               err = percpu_counter_init(&sbi->s_freeinodes_counter,
+                               ext2_count_free_inodes(sb));
+       }
+       if (!err) {
+               err = percpu_counter_init(&sbi->s_dirs_counter,
+                               ext2_count_dirs(sb));
+       }
+       if (err) {
+               ext2_msg(sb, KERN_ERR, "error: insufficient memory");
+               goto failed_mount3;
+       }
        /*
         * set up enough so that it can read an inode
         */
        sb->s_op = &ext2_sops;
        sb->s_export_op = &ext2_export_ops;
        sb->s_xattr = ext2_xattr_handlers;
-       root = iget(sb, EXT2_ROOT_INO);
+       root = ext2_iget(sb, EXT2_ROOT_INO);
+       if (IS_ERR(root)) {
+               ret = PTR_ERR(root);
+               goto failed_mount3;
+       }
+       if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
+               iput(root);
+               ext2_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
+               goto failed_mount3;
+       }
+
        sb->s_root = d_alloc_root(root);
        if (!sb->s_root) {
                iput(root);
-               printk(KERN_ERR "EXT2-fs: get root inode failed\n");
-               goto failed_mount2;
-       }
-       if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
-               dput(sb->s_root);
-               sb->s_root = NULL;
-               printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
-               goto failed_mount2;
+               ext2_msg(sb, KERN_ERR, "error: get root inode failed");
+               ret = -ENOMEM;
+               goto failed_mount3;
        }
        if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
-               ext2_warning(sb, __FUNCTION__,
-                       "mounting ext3 filesystem as ext2");
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: mounting ext3 filesystem as ext2");
        ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
-       percpu_counter_mod(&sbi->s_freeblocks_counter,
-                               ext2_count_free_blocks(sb));
-       percpu_counter_mod(&sbi->s_freeinodes_counter,
-                               ext2_count_free_inodes(sb));
-       percpu_counter_mod(&sbi->s_dirs_counter,
-                               ext2_count_dirs(sb));
        return 0;
 
 cantfind_ext2:
        if (!silent)
-               printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
-                      sb->s_id);
+               ext2_msg(sb, KERN_ERR,
+                       "error: can't find an ext2 filesystem on dev %s.",
+                       sb->s_id);
        goto failed_mount;
-
+failed_mount3:
+       percpu_counter_destroy(&sbi->s_freeblocks_counter);
+       percpu_counter_destroy(&sbi->s_freeinodes_counter);
+       percpu_counter_destroy(&sbi->s_dirs_counter);
 failed_mount2:
        for (i = 0; i < db_count; i++)
                brelse(sbi->s_group_desc[i]);
@@ -910,13 +1102,35 @@ failed_mount:
        brelse(bh);
 failed_sbi:
        sb->s_fs_info = NULL;
+       kfree(sbi->s_blockgroup_lock);
        kfree(sbi);
-       return -EINVAL;
+       return ret;
+}
+
+static void ext2_clear_super_error(struct super_block *sb)
+{
+       struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
+
+       if (buffer_write_io_error(sbh)) {
+               /*
+                * Oh, dear.  A previous attempt to write the
+                * superblock failed.  This could happen because the
+                * USB device was yanked out.  Or it could happen to
+                * be a transient write error and maybe the block will
+                * be remapped.  Nothing we can do but to retry the
+                * write and hope for the best.
+                */
+               printk(KERN_ERR "EXT2-fs: %s previous I/O error to "
+                      "superblock detected", sb->s_id);
+               clear_buffer_write_io_error(sbh);
+               set_buffer_uptodate(sbh);
+       }
 }
 
 static void ext2_commit_super (struct super_block * sb,
                               struct ext2_super_block * es)
 {
+       ext2_clear_super_error(sb);
        es->s_wtime = cpu_to_le32(get_seconds());
        mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
        sb->s_dirt = 0;
@@ -924,6 +1138,7 @@ static void ext2_commit_super (struct super_block * sb,
 
 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
 {
+       ext2_clear_super_error(sb);
        es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
        es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
        es->s_wtime = cpu_to_le32(get_seconds());
@@ -943,26 +1158,52 @@ static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
  * set s_state to EXT2_VALID_FS after some corrections.
  */
 
-void ext2_write_super (struct super_block * sb)
+static int ext2_sync_fs(struct super_block *sb, int wait)
 {
-       struct ext2_super_block * es;
+       struct ext2_super_block *es = EXT2_SB(sb)->s_es;
+       struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
+
        lock_kernel();
-       if (!(sb->s_flags & MS_RDONLY)) {
-               es = EXT2_SB(sb)->s_es;
-
-               if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
-                       ext2_debug ("setting valid to 0\n");
-                       es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
-                                                 ~EXT2_VALID_FS);
-                       es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
-                       es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
-                       es->s_mtime = cpu_to_le32(get_seconds());
-                       ext2_sync_super(sb, es);
-               } else
-                       ext2_commit_super (sb, es);
+       if (buffer_write_io_error(sbh)) {
+               /*
+                * Oh, dear.  A previous attempt to write the
+                * superblock failed.  This could happen because the
+                * USB device was yanked out.  Or it could happen to
+                * be a transient write error and maybe the block will
+                * be remapped.  Nothing we can do but to retry the
+                * write and hope for the best.
+                */
+               ext2_msg(sb, KERN_ERR,
+                      "previous I/O error to superblock detected\n");
+               clear_buffer_write_io_error(sbh);
+               set_buffer_uptodate(sbh);
+       }
+
+       if (es->s_state & cpu_to_le16(EXT2_VALID_FS)) {
+               ext2_debug("setting valid to 0\n");
+               es->s_state &= cpu_to_le16(~EXT2_VALID_FS);
+               es->s_free_blocks_count =
+                       cpu_to_le32(ext2_count_free_blocks(sb));
+               es->s_free_inodes_count =
+                       cpu_to_le32(ext2_count_free_inodes(sb));
+               es->s_mtime = cpu_to_le32(get_seconds());
+               ext2_sync_super(sb, es);
+       } else {
+               ext2_commit_super(sb, es);
        }
        sb->s_dirt = 0;
        unlock_kernel();
+
+       return 0;
+}
+
+
+void ext2_write_super(struct super_block *sb)
+{
+       if (!(sb->s_flags & MS_RDONLY))
+               ext2_sync_fs(sb, 1);
+       else
+               sb->s_dirt = 0;
 }
 
 static int ext2_remount (struct super_block * sb, int * flags, char * data)
@@ -974,6 +1215,8 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
        unsigned long old_sb_flags;
        int err;
 
+       lock_kernel();
+
        /* Store the old options */
        old_sb_flags = sb->s_flags;
        old_opts.s_mount_opt = sbi->s_mount_opt;
@@ -983,7 +1226,7 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
        /*
         * Allow the "check" option to be passed as a remount option.
         */
-       if (!parse_options (data, sbi)) {
+       if (!parse_options(data, sb)) {
                err = -EINVAL;
                goto restore_opts;
        }
@@ -991,18 +1234,35 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
        sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
                ((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
 
+       ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
+                                   EXT2_MOUNT_XIP if not */
+
+       if ((ext2_use_xip(sb)) && (sb->s_blocksize != PAGE_SIZE)) {
+               ext2_msg(sb, KERN_WARNING,
+                       "warning: unsupported blocksize for xip");
+               err = -EINVAL;
+               goto restore_opts;
+       }
+
        es = sbi->s_es;
        if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
            (old_mount_opt & EXT2_MOUNT_XIP)) &&
-           invalidate_inodes(sb))
-               ext2_warning(sb, __FUNCTION__, "busy inodes while remounting "\
-                            "xip remain in cache (no functional problem)");
-       if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
+           invalidate_inodes(sb)) {
+               ext2_msg(sb, KERN_WARNING, "warning: refusing change of "
+                        "xip flag with busy inodes while remounting");
+               sbi->s_mount_opt &= ~EXT2_MOUNT_XIP;
+               sbi->s_mount_opt |= old_mount_opt & EXT2_MOUNT_XIP;
+       }
+       if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) {
+               unlock_kernel();
                return 0;
+       }
        if (*flags & MS_RDONLY) {
                if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
-                   !(sbi->s_mount_state & EXT2_VALID_FS))
+                   !(sbi->s_mount_state & EXT2_VALID_FS)) {
+                       unlock_kernel();
                        return 0;
+               }
                /*
                 * OK, we are remounting a valid rw partition rdonly, so set
                 * the rdonly flag and then mark the partition as valid again.
@@ -1013,9 +1273,10 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
                __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
                                               ~EXT2_FEATURE_RO_COMPAT_SUPP);
                if (ret) {
-                       printk("EXT2-fs: %s: couldn't remount RDWR because of "
-                              "unsupported optional features (%x).\n",
-                              sb->s_id, le32_to_cpu(ret));
+                       ext2_msg(sb, KERN_WARNING,
+                               "warning: couldn't remount RDWR because of "
+                               "unsupported optional features (%x).",
+                               le32_to_cpu(ret));
                        err = -EROFS;
                        goto restore_opts;
                }
@@ -1029,12 +1290,14 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
                        sb->s_flags &= ~MS_RDONLY;
        }
        ext2_sync_super(sb, es);
+       unlock_kernel();
        return 0;
 restore_opts:
        sbi->s_mount_opt = old_opts.s_mount_opt;
        sbi->s_resuid = old_opts.s_resuid;
        sbi->s_resgid = old_opts.s_resgid;
        sb->s_flags = old_sb_flags;
+       unlock_kernel();
        return err;
 }
 
@@ -1042,21 +1305,26 @@ static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
 {
        struct super_block *sb = dentry->d_sb;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
-       unsigned long overhead;
-       int i;
+       struct ext2_super_block *es = sbi->s_es;
+       u64 fsid;
 
        if (test_opt (sb, MINIX_DF))
-               overhead = 0;
-       else {
+               sbi->s_overhead_last = 0;
+       else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
+               unsigned long i, overhead = 0;
+               smp_rmb();
+
                /*
-                * Compute the overhead (FS structures)
+                * Compute the overhead (FS structures). This is constant
+                * for a given filesystem unless the number of block groups
+                * changes so we cache the previous value until it does.
                 */
 
                /*
                 * All of the blocks before first_data_block are
                 * overhead
                 */
-               overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
+               overhead = le32_to_cpu(es->s_first_data_block);
 
                /*
                 * Add the overhead attributed to the superblock and
@@ -1073,18 +1341,27 @@ static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
                 */
                overhead += (sbi->s_groups_count *
                             (2 + sbi->s_itb_per_group));
+               sbi->s_overhead_last = overhead;
+               smp_wmb();
+               sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
        }
 
        buf->f_type = EXT2_SUPER_MAGIC;
        buf->f_bsize = sb->s_blocksize;
-       buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
+       buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
        buf->f_bfree = ext2_count_free_blocks(sb);
-       buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
-       if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
+       es->s_free_blocks_count = cpu_to_le32(buf->f_bfree);
+       buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
+       if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
                buf->f_bavail = 0;
-       buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
-       buf->f_ffree = ext2_count_free_inodes (sb);
+       buf->f_files = le32_to_cpu(es->s_inodes_count);
+       buf->f_ffree = ext2_count_free_inodes(sb);
+       es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
        buf->f_namelen = EXT2_NAME_LEN;
+       fsid = le64_to_cpup((void *)es->s_uuid) ^
+              le64_to_cpup((void *)es->s_uuid + sizeof(u64));
+       buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
+       buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
        return 0;
 }
 
@@ -1123,8 +1400,9 @@ static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
                                sb->s_blocksize - offset : toread;
 
                tmp_bh.b_state = 0;
+               tmp_bh.b_size = sb->s_blocksize;
                err = ext2_get_block(inode, blk, &tmp_bh, 0);
-               if (err)
+               if (err < 0)
                        return err;
                if (!buffer_mapped(&tmp_bh))    /* A hole? */
                        memset(data, 0, tocopy);
@@ -1156,14 +1434,14 @@ static ssize_t ext2_quota_write(struct super_block *sb, int type,
        struct buffer_head tmp_bh;
        struct buffer_head *bh;
 
-       mutex_lock(&inode->i_mutex);
+       mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
        while (towrite > 0) {
                tocopy = sb->s_blocksize - offset < towrite ?
                                sb->s_blocksize - offset : towrite;
 
                tmp_bh.b_state = 0;
                err = ext2_get_block(inode, blk, &tmp_bh, 1);
-               if (err)
+               if (err < 0)
                        goto out;
                if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
                        bh = sb_bread(sb, tmp_bh.b_blocknr);
@@ -1186,8 +1464,10 @@ static ssize_t ext2_quota_write(struct super_block *sb, int type,
                blk++;
        }
 out:
-       if (len == towrite)
+       if (len == towrite) {
+               mutex_unlock(&inode->i_mutex);
                return err;
+       }
        if (inode->i_size < off+len-towrite)
                i_size_write(inode, off+len-towrite);
        inode->i_version++;