switch open-coded instances of d_make_root() to new helper
[linux-2.6.git] / fs / ext2 / super.c
index 42e4a30..e1025c7 100644 (file)
@@ -26,7 +26,6 @@
 #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 "xip.h"
 
 static void ext2_sync_super(struct super_block *sb,
-                           struct ext2_super_block *es);
+                           struct ext2_super_block *es, int wait);
 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, ...)
+void ext2_error(struct super_block *sb, const char *function,
+               const char *fmt, ...)
 {
+       struct va_format vaf;
        va_list args;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
        struct ext2_super_block *es = sbi->s_es;
 
        if (!(sb->s_flags & MS_RDONLY)) {
+               spin_lock(&sbi->s_lock);
                sbi->s_mount_state |= EXT2_ERROR_FS;
                es->s_state |= cpu_to_le16(EXT2_ERROR_FS);
-               ext2_sync_super(sb, es);
+               spin_unlock(&sbi->s_lock);
+               ext2_sync_super(sb, es, 1);
        }
 
        va_start(args, fmt);
-       printk(KERN_CRIT "EXT2-fs (%s): error: %s: ", sb->s_id, function);
-       vprintk(fmt, args);
-       printk("\n");
+
+       vaf.fmt = fmt;
+       vaf.va = &args;
+
+       printk(KERN_CRIT "EXT2-fs (%s): error: %s: %pV\n",
+              sb->s_id, function, &vaf);
+
        va_end(args);
 
        if (test_opt(sb, ERRORS_PANIC))
@@ -75,15 +81,22 @@ void ext2_error (struct super_block * sb, const char * function,
 void ext2_msg(struct super_block *sb, const char *prefix,
                const char *fmt, ...)
 {
+       struct va_format vaf;
        va_list args;
 
        va_start(args, fmt);
-       printk("%sEXT2-fs (%s): ", prefix, sb->s_id);
-       vprintk(fmt, args);
-       printk("\n");
+
+       vaf.fmt = fmt;
+       vaf.va = &args;
+
+       printk("%sEXT2-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
+
        va_end(args);
 }
 
+/*
+ * This must be called with sbi->s_lock held.
+ */
 void ext2_update_dynamic_rev(struct super_block *sb)
 {
        struct ext2_super_block *es = EXT2_SB(sb)->s_es;
@@ -115,7 +128,7 @@ static void ext2_put_super (struct super_block * sb)
        int i;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
 
-       lock_kernel();
+       dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
 
        if (sb->s_dirt)
                ext2_write_super(sb);
@@ -124,8 +137,10 @@ static void ext2_put_super (struct super_block * sb)
        if (!(sb->s_flags & MS_RDONLY)) {
                struct ext2_super_block *es = sbi->s_es;
 
+               spin_lock(&sbi->s_lock);
                es->s_state = cpu_to_le16(sbi->s_mount_state);
-               ext2_sync_super(sb, es);
+               spin_unlock(&sbi->s_lock);
+               ext2_sync_super(sb, es, 1);
        }
        db_count = sbi->s_gdb_count;
        for (i = 0; i < db_count; i++)
@@ -140,8 +155,6 @@ static void ext2_put_super (struct super_block * sb)
        sb->s_fs_info = NULL;
        kfree(sbi->s_blockgroup_lock);
        kfree(sbi);
-
-       unlock_kernel();
 }
 
 static struct kmem_cache * ext2_inode_cachep;
@@ -157,11 +170,17 @@ static struct inode *ext2_alloc_inode(struct super_block *sb)
        return &ei->vfs_inode;
 }
 
-static void ext2_destroy_inode(struct inode *inode)
+static void ext2_i_callback(struct rcu_head *head)
 {
+       struct inode *inode = container_of(head, struct inode, i_rcu);
        kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
 }
 
+static void ext2_destroy_inode(struct inode *inode)
+{
+       call_rcu(&inode->i_rcu, ext2_i_callback);
+}
+
 static void init_once(void *foo)
 {
        struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
@@ -191,24 +210,14 @@ static void destroy_inodecache(void)
        kmem_cache_destroy(ext2_inode_cachep);
 }
 
-static void ext2_clear_inode(struct inode *inode)
-{
-       struct ext2_block_alloc_info *rsv = EXT2_I(inode)->i_block_alloc_info;
-
-       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)
+static int ext2_show_options(struct seq_file *seq, struct dentry *root)
 {
-       struct super_block *sb = vfs->mnt_sb;
+       struct super_block *sb = root->d_sb;
        struct ext2_sb_info *sbi = EXT2_SB(sb);
        struct ext2_super_block *es = sbi->s_es;
        unsigned long def_mount_opts;
 
+       spin_lock(&sbi->s_lock);
        def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
 
        if (sbi->s_sb_block != 1)
@@ -281,6 +290,7 @@ static int ext2_show_options(struct seq_file *seq, struct vfsmount *vfs)
        if (!test_opt(sb, RESERVATION))
                seq_puts(seq, ",noreservation");
 
+       spin_unlock(&sbi->s_lock);
        return 0;
 }
 
@@ -293,13 +303,12 @@ static const struct super_operations ext2_sops = {
        .alloc_inode    = ext2_alloc_inode,
        .destroy_inode  = ext2_destroy_inode,
        .write_inode    = ext2_write_inode,
-       .delete_inode   = ext2_delete_inode,
+       .evict_inode    = ext2_evict_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,
        .show_options   = ext2_show_options,
 #ifdef CONFIG_QUOTA
        .quota_read     = ext2_quota_read,
@@ -317,10 +326,10 @@ static struct inode *ext2_nfs_get_inode(struct super_block *sb,
        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.....
+       /*
+        * ext2_iget isn't quite right if the inode is currently unallocated!
+        * However ext2_iget currently does appropriate checks to handle stale
+        * inodes so everything is OK.
         */
        inode = ext2_iget(sb, ino);
        if (IS_ERR(inode))
@@ -606,7 +615,6 @@ static int ext2_setup_super (struct super_block * sb,
        if (!le16_to_cpu(es->s_max_mnt_count))
                es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
        le16_add_cpu(&es->s_mnt_count, 1);
-       ext2_write_super(sb);
        if (test_opt (sb, DEBUG))
                ext2_msg(sb, KERN_INFO, "%s, %s, bs=%lu, fs=%lu, gc=%lu, "
                        "bpg=%lu, ipg=%lu, mo=%04lx]",
@@ -754,19 +762,22 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        __le32 features;
        int err;
 
+       err = -ENOMEM;
        sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
        if (!sbi)
-               return -ENOMEM;
+               goto failed_unlock;
 
        sbi->s_blockgroup_lock =
                kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
        if (!sbi->s_blockgroup_lock) {
                kfree(sbi);
-               return -ENOMEM;
+               goto failed_unlock;
        }
        sb->s_fs_info = sbi;
        sbi->s_sb_block = sb_block;
 
+       spin_lock_init(&sbi->s_lock);
+
        /*
         * See what the current blocksize for the device is, and
         * use that as the blocksize.  Otherwise (or if the blocksize
@@ -886,7 +897,8 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                brelse(bh);
 
                if (!sb_set_blocksize(sb, blocksize)) {
-                       ext2_msg(sb, KERN_ERR, "error: blocksize is too small");
+                       ext2_msg(sb, KERN_ERR,
+                               "error: bad blocksize %d", blocksize);
                        goto failed_sbi;
                }
 
@@ -907,6 +919,7 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        }
 
        sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
+       sb->s_max_links = EXT2_LINK_MAX;
 
        if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
                sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
@@ -1058,6 +1071,12 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        sb->s_op = &ext2_sops;
        sb->s_export_op = &ext2_export_ops;
        sb->s_xattr = ext2_xattr_handlers;
+
+#ifdef CONFIG_QUOTA
+       sb->dq_op = &dquot_operations;
+       sb->s_qcop = &dquot_quotactl_ops;
+#endif
+
        root = ext2_iget(sb, EXT2_ROOT_INO);
        if (IS_ERR(root)) {
                ret = PTR_ERR(root);
@@ -1069,9 +1088,8 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
                goto failed_mount3;
        }
 
-       sb->s_root = d_alloc_root(root);
+       sb->s_root = d_make_root(root);
        if (!sb->s_root) {
-               iput(root);
                ext2_msg(sb, KERN_ERR, "error: get root inode failed");
                ret = -ENOMEM;
                goto failed_mount3;
@@ -1079,7 +1097,9 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
        if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
                ext2_msg(sb, KERN_WARNING,
                        "warning: mounting ext3 filesystem as ext2");
-       ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
+       if (ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY))
+               sb->s_flags |= MS_RDONLY;
+       ext2_write_super(sb);
        return 0;
 
 cantfind_ext2:
@@ -1104,6 +1124,7 @@ failed_sbi:
        sb->s_fs_info = NULL;
        kfree(sbi->s_blockgroup_lock);
        kfree(sbi);
+failed_unlock:
        return ret;
 }
 
@@ -1120,30 +1141,26 @@ static void ext2_clear_super_error(struct super_block *sb)
                 * 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);
+               ext2_msg(sb, KERN_ERR,
+                      "previous I/O error to superblock detected\n");
                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;
-}
-
-static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
+static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es,
+                           int wait)
 {
        ext2_clear_super_error(sb);
+       spin_lock(&EXT2_SB(sb)->s_lock);
        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());
+       /* unlock before we do IO */
+       spin_unlock(&EXT2_SB(sb)->s_lock);
        mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
-       sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
+       if (wait)
+               sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
        sb->s_dirt = 0;
 }
 
@@ -1157,43 +1174,18 @@ static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
  * may have been checked while mounted and e2fsck may have
  * set s_state to EXT2_VALID_FS after some corrections.
  */
-
 static int ext2_sync_fs(struct super_block *sb, int wait)
 {
+       struct ext2_sb_info *sbi = EXT2_SB(sb);
        struct ext2_super_block *es = EXT2_SB(sb)->s_es;
-       struct buffer_head *sbh = EXT2_SB(sb)->s_sbh;
-
-       lock_kernel();
-       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);
-       }
 
+       spin_lock(&sbi->s_lock);
        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();
-
+       spin_unlock(&sbi->s_lock);
+       ext2_sync_super(sb, es, wait);
        return 0;
 }
 
@@ -1215,7 +1207,7 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
        unsigned long old_sb_flags;
        int err;
 
-       lock_kernel();
+       spin_lock(&sbi->s_lock);
 
        /* Store the old options */
        old_sb_flags = sb->s_flags;
@@ -1245,30 +1237,38 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
        }
 
        es = sbi->s_es;
-       if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
-           (old_mount_opt & EXT2_MOUNT_XIP)) &&
-           invalidate_inodes(sb)) {
+       if ((sbi->s_mount_opt ^ old_mount_opt) & EXT2_MOUNT_XIP) {
                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();
+               spin_unlock(&sbi->s_lock);
                return 0;
        }
        if (*flags & MS_RDONLY) {
                if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
                    !(sbi->s_mount_state & EXT2_VALID_FS)) {
-                       unlock_kernel();
+                       spin_unlock(&sbi->s_lock);
                        return 0;
                }
+
                /*
                 * OK, we are remounting a valid rw partition rdonly, so set
                 * the rdonly flag and then mark the partition as valid again.
                 */
                es->s_state = cpu_to_le16(sbi->s_mount_state);
                es->s_mtime = cpu_to_le32(get_seconds());
+               spin_unlock(&sbi->s_lock);
+
+               err = dquot_suspend(sb, -1);
+               if (err < 0) {
+                       spin_lock(&sbi->s_lock);
+                       goto restore_opts;
+               }
+
+               ext2_sync_super(sb, es, 1);
        } else {
                __le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
                                               ~EXT2_FEATURE_RO_COMPAT_SUPP);
@@ -1288,16 +1288,20 @@ static int ext2_remount (struct super_block * sb, int * flags, char * data)
                sbi->s_mount_state = le16_to_cpu(es->s_state);
                if (!ext2_setup_super (sb, es, 0))
                        sb->s_flags &= ~MS_RDONLY;
+               spin_unlock(&sbi->s_lock);
+
+               ext2_write_super(sb);
+
+               dquot_resume(sb, -1);
        }
-       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();
+       spin_unlock(&sbi->s_lock);
        return err;
 }
 
@@ -1308,6 +1312,8 @@ static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
        struct ext2_super_block *es = sbi->s_es;
        u64 fsid;
 
+       spin_lock(&sbi->s_lock);
+
        if (test_opt (sb, MINIX_DF))
                sbi->s_overhead_last = 0;
        else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
@@ -1362,20 +1368,21 @@ static int ext2_statfs (struct dentry * dentry, struct kstatfs * buf)
               le64_to_cpup((void *)es->s_uuid + sizeof(u64));
        buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
        buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
+       spin_unlock(&sbi->s_lock);
        return 0;
 }
 
-static int ext2_get_sb(struct file_system_type *fs_type,
-       int flags, const char *dev_name, void *data, struct vfsmount *mnt)
+static struct dentry *ext2_mount(struct file_system_type *fs_type,
+       int flags, const char *dev_name, void *data)
 {
-       return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super, mnt);
+       return mount_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
 }
 
 #ifdef CONFIG_QUOTA
 
 /* Read data from quotafile - avoid pagecache and such because we cannot afford
  * acquiring the locks... As quota files are never truncated and quota code
- * itself serializes the operations (and noone else should touch the files)
+ * itself serializes the operations (and no one else should touch the files)
  * we don't have to be afraid of races */
 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
                               size_t len, loff_t off)
@@ -1482,7 +1489,7 @@ out:
 static struct file_system_type ext2_fs_type = {
        .owner          = THIS_MODULE,
        .name           = "ext2",
-       .get_sb         = ext2_get_sb,
+       .mount          = ext2_mount,
        .kill_sb        = kill_block_super,
        .fs_flags       = FS_REQUIRES_DEV,
 };
@@ -1513,5 +1520,8 @@ static void __exit exit_ext2_fs(void)
        exit_ext2_xattr();
 }
 
+MODULE_AUTHOR("Remy Card and others");
+MODULE_DESCRIPTION("Second Extended Filesystem");
+MODULE_LICENSE("GPL");
 module_init(init_ext2_fs)
 module_exit(exit_ext2_fs)