reiserfs: Don't call reiserfs_get_acl() with the reiserfs lock
[safe/jmp/linux-2.6] / fs / nilfs2 / super.c
index 5a8c5e4..644e667 100644 (file)
@@ -50,6 +50,8 @@
 #include <linux/writeback.h>
 #include <linux/kobject.h>
 #include <linux/exportfs.h>
+#include <linux/seq_file.h>
+#include <linux/mount.h>
 #include "nilfs.h"
 #include "mdt.h"
 #include "alloc.h"
@@ -65,7 +67,6 @@ MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem "
                   "(NILFS)");
 MODULE_LICENSE("GPL");
 
-static void nilfs_write_super(struct super_block *sb);
 static int nilfs_remount(struct super_block *sb, int *flags, char *data);
 
 /**
@@ -133,7 +134,7 @@ void nilfs_warning(struct super_block *sb, const char *function,
 
 static struct kmem_cache *nilfs_inode_cachep;
 
-struct inode *nilfs_alloc_inode(struct super_block *sb)
+struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs)
 {
        struct nilfs_inode_info *ii;
 
@@ -143,10 +144,15 @@ struct inode *nilfs_alloc_inode(struct super_block *sb)
        ii->i_bh = NULL;
        ii->i_state = 0;
        ii->vfs_inode.i_version = 1;
-       nilfs_btnode_cache_init(&ii->i_btnode_cache);
+       nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi);
        return &ii->vfs_inode;
 }
 
+struct inode *nilfs_alloc_inode(struct super_block *sb)
+{
+       return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs);
+}
+
 void nilfs_destroy_inode(struct inode *inode)
 {
        kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode));
@@ -184,16 +190,6 @@ static void nilfs_clear_inode(struct inode *inode)
 {
        struct nilfs_inode_info *ii = NILFS_I(inode);
 
-#ifdef CONFIG_NILFS_POSIX_ACL
-       if (ii->i_acl && ii->i_acl != NILFS_ACL_NOT_CACHED) {
-               posix_acl_release(ii->i_acl);
-               ii->i_acl = NILFS_ACL_NOT_CACHED;
-       }
-       if (ii->i_default_acl && ii->i_default_acl != NILFS_ACL_NOT_CACHED) {
-               posix_acl_release(ii->i_default_acl);
-               ii->i_default_acl = NILFS_ACL_NOT_CACHED;
-       }
-#endif
        /*
         * Free resources allocated in nilfs_read_inode(), here.
         */
@@ -316,9 +312,6 @@ static void nilfs_put_super(struct super_block *sb)
 
        lock_kernel();
 
-       if (sb->s_dirt)
-               nilfs_write_super(sb);
-
        nilfs_detach_segment_constructor(sbi);
 
        if (!(sb->s_flags & MS_RDONLY)) {
@@ -327,77 +320,35 @@ static void nilfs_put_super(struct super_block *sb)
                nilfs_commit_super(sbi, 1);
                up_write(&nilfs->ns_sem);
        }
-       down_write(&nilfs->ns_sem);
+       down_write(&nilfs->ns_super_sem);
        if (nilfs->ns_current == sbi)
                nilfs->ns_current = NULL;
-       up_write(&nilfs->ns_sem);
+       up_write(&nilfs->ns_super_sem);
 
        nilfs_detach_checkpoint(sbi);
        put_nilfs(sbi->s_nilfs);
        sbi->s_super = NULL;
        sb->s_fs_info = NULL;
-       kfree(sbi);
+       nilfs_put_sbinfo(sbi);
 
        unlock_kernel();
 }
 
-/**
- * nilfs_write_super - write super block(s) of NILFS
- * @sb: super_block
- *
- * nilfs_write_super() gets a fs-dependent lock, writes super block(s), and
- * clears s_dirt.  This function is called in the section protected by
- * lock_super().
- *
- * The s_dirt flag is managed by each filesystem and we protect it by ns_sem
- * of the struct the_nilfs.  Lock order must be as follows:
- *
- *   1. lock_super()
- *   2.    down_write(&nilfs->ns_sem)
- *
- * Inside NILFS, locking ns_sem is enough to protect s_dirt and the buffer
- * of the super block (nilfs->ns_sbp[]).
- *
- * In most cases, VFS functions call lock_super() before calling these
- * methods.  So we must be careful not to bring on deadlocks when using
- * lock_super();  see generic_shutdown_super(), write_super(), and so on.
- *
- * Note that order of lock_kernel() and lock_super() depends on contexts
- * of VFS.  We should also note that lock_kernel() can be used in its
- * protective section and only the outermost one has an effect.
- */
-static void nilfs_write_super(struct super_block *sb)
+static int nilfs_sync_fs(struct super_block *sb, int wait)
 {
        struct nilfs_sb_info *sbi = NILFS_SB(sb);
        struct the_nilfs *nilfs = sbi->s_nilfs;
-
-       down_write(&nilfs->ns_sem);
-       if (!(sb->s_flags & MS_RDONLY)) {
-               struct nilfs_super_block **sbp = nilfs->ns_sbp;
-               u64 t = get_seconds();
-               int dupsb;
-
-               if (!nilfs_discontinued(nilfs) && t >= nilfs->ns_sbwtime[0] &&
-                   t < nilfs->ns_sbwtime[0] + NILFS_SB_FREQ) {
-                       up_write(&nilfs->ns_sem);
-                       return;
-               }
-               dupsb = sbp[1] && t > nilfs->ns_sbwtime[1] + NILFS_ALTSB_FREQ;
-               nilfs_commit_super(sbi, dupsb);
-       }
-       sb->s_dirt = 0;
-       up_write(&nilfs->ns_sem);
-}
-
-static int nilfs_sync_fs(struct super_block *sb, int wait)
-{
        int err = 0;
 
-       nilfs_write_super(sb);
-
        /* This function is called when super block should be written back */
        if (wait)
                err = nilfs_construct_segment(sb);
+
+       down_write(&nilfs->ns_sem);
+       if (sb->s_dirt)
+               nilfs_commit_super(sbi, 1);
+       up_write(&nilfs->ns_sem);
+
        return err;
 }
 
@@ -408,12 +359,11 @@ int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
        struct buffer_head *bh_cp;
        int err;
 
-       down_write(&nilfs->ns_sem);
+       down_write(&nilfs->ns_super_sem);
        list_add(&sbi->s_list, &nilfs->ns_supers);
-       up_write(&nilfs->ns_sem);
+       up_write(&nilfs->ns_super_sem);
 
-       sbi->s_ifile = nilfs_mdt_new(
-               nilfs, sbi->s_super, NILFS_IFILE_INO, NILFS_IFILE_GFP);
+       sbi->s_ifile = nilfs_mdt_new(nilfs, sbi->s_super, NILFS_IFILE_INO);
        if (!sbi->s_ifile)
                return -ENOMEM;
 
@@ -421,8 +371,10 @@ int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
        if (unlikely(err))
                goto failed;
 
+       down_read(&nilfs->ns_segctor_sem);
        err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp,
                                          &bh_cp);
+       up_read(&nilfs->ns_segctor_sem);
        if (unlikely(err)) {
                if (err == -ENOENT || err == -EINVAL) {
                        printk(KERN_ERR
@@ -448,9 +400,9 @@ int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno)
        nilfs_mdt_destroy(sbi->s_ifile);
        sbi->s_ifile = NULL;
 
-       down_write(&nilfs->ns_sem);
+       down_write(&nilfs->ns_super_sem);
        list_del_init(&sbi->s_list);
-       up_write(&nilfs->ns_sem);
+       up_write(&nilfs->ns_super_sem);
 
        return err;
 }
@@ -462,9 +414,9 @@ void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi)
        nilfs_mdt_clear(sbi->s_ifile);
        nilfs_mdt_destroy(sbi->s_ifile);
        sbi->s_ifile = NULL;
-       down_write(&nilfs->ns_sem);
+       down_write(&nilfs->ns_super_sem);
        list_del_init(&sbi->s_list);
-       up_write(&nilfs->ns_sem);
+       up_write(&nilfs->ns_super_sem);
 }
 
 static int nilfs_mark_recovery_complete(struct nilfs_sb_info *sbi)
@@ -532,7 +484,27 @@ static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf)
        return 0;
 }
 
-static struct super_operations nilfs_sops = {
+static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
+{
+       struct super_block *sb = vfs->mnt_sb;
+       struct nilfs_sb_info *sbi = NILFS_SB(sb);
+
+       if (!nilfs_test_opt(sbi, BARRIER))
+               seq_printf(seq, ",barrier=off");
+       if (nilfs_test_opt(sbi, SNAPSHOT))
+               seq_printf(seq, ",cp=%llu",
+                          (unsigned long long int)sbi->s_snapshot_cno);
+       if (nilfs_test_opt(sbi, ERRORS_RO))
+               seq_printf(seq, ",errors=remount-ro");
+       if (nilfs_test_opt(sbi, ERRORS_PANIC))
+               seq_printf(seq, ",errors=panic");
+       if (nilfs_test_opt(sbi, STRICT_ORDER))
+               seq_printf(seq, ",order=strict");
+
+       return 0;
+}
+
+static const struct super_operations nilfs_sops = {
        .alloc_inode    = nilfs_alloc_inode,
        .destroy_inode  = nilfs_destroy_inode,
        .dirty_inode    = nilfs_dirty_inode,
@@ -541,7 +513,7 @@ static struct super_operations nilfs_sops = {
        /* .drop_inode    = nilfs_drop_inode, */
        .delete_inode   = nilfs_delete_inode,
        .put_super      = nilfs_put_super,
-       .write_super    = nilfs_write_super,
+       /* .write_super    = nilfs_write_super, */
        .sync_fs        = nilfs_sync_fs,
        /* .write_super_lockfs */
        /* .unlockfs */
@@ -549,7 +521,7 @@ static struct super_operations nilfs_sops = {
        .remount_fs     = nilfs_remount,
        .clear_inode    = nilfs_clear_inode,
        /* .umount_begin */
-       /* .show_options */
+       .show_options = nilfs_show_options
 };
 
 static struct inode *
@@ -588,7 +560,7 @@ nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len,
                                    nilfs_nfs_get_inode);
 }
 
-static struct export_operations nilfs_export_ops = {
+static const struct export_operations nilfs_export_ops = {
        .fh_to_dentry = nilfs_fh_to_dentry,
        .fh_to_parent = nilfs_fh_to_parent,
        .get_parent = nilfs_get_parent,
@@ -764,7 +736,7 @@ int nilfs_store_magic_and_option(struct super_block *sb,
  * @silent: silent mode flag
  * @nilfs: the_nilfs struct
  *
- * This function is called exclusively by bd_mount_mutex.
+ * This function is called exclusively by nilfs->ns_mount_mutex.
  * So, the recovery process is protected from other simultaneous mounts.
  */
 static int
@@ -785,6 +757,7 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
        get_nilfs(nilfs);
        sbi->s_nilfs = nilfs;
        sbi->s_super = sb;
+       atomic_set(&sbi->s_count, 1);
 
        err = init_nilfs(nilfs, sbi, (char *)data);
        if (err)
@@ -818,10 +791,15 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
 
        if (sb->s_flags & MS_RDONLY) {
                if (nilfs_test_opt(sbi, SNAPSHOT)) {
+                       down_read(&nilfs->ns_segctor_sem);
                        err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile,
                                                       sbi->s_snapshot_cno);
-                       if (err < 0)
+                       up_read(&nilfs->ns_segctor_sem);
+                       if (err < 0) {
+                               if (err == -ENOENT)
+                                       err = -EINVAL;
                                goto failed_sbi;
+                       }
                        if (!err) {
                                printk(KERN_ERR
                                       "NILFS: The specified checkpoint is "
@@ -882,10 +860,10 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
                goto failed_root;
        }
 
-       down_write(&nilfs->ns_sem);
+       down_write(&nilfs->ns_super_sem);
        if (!nilfs_test_opt(sbi, SNAPSHOT))
                nilfs->ns_current = sbi;
-       up_write(&nilfs->ns_sem);
+       up_write(&nilfs->ns_super_sem);
 
        return 0;
 
@@ -902,7 +880,7 @@ nilfs_fill_super(struct super_block *sb, void *data, int silent,
  failed_sbi:
        put_nilfs(nilfs);
        sb->s_fs_info = NULL;
-       kfree(sbi);
+       nilfs_put_sbinfo(sbi);
        return err;
 }
 
@@ -917,6 +895,7 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
 
        lock_kernel();
 
+       down_write(&nilfs->ns_super_sem);
        old_sb_flags = sb->s_flags;
        old_opts.mount_opt = sbi->s_mount_opt;
        old_opts.snapshot_cno = sbi->s_snapshot_cno;
@@ -964,24 +943,20 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
                 * store the current valid flag.  (It may have been changed
                 * by fsck since we originally mounted the partition.)
                 */
-               down(&sb->s_bdev->bd_mount_sem);
-               down_read(&nilfs->ns_sem);
                if (nilfs->ns_current && nilfs->ns_current != sbi) {
                        printk(KERN_WARNING "NILFS (device %s): couldn't "
                               "remount because an RW-mount exists.\n",
                               sb->s_id);
-                       up_read(&nilfs->ns_sem);
                        err = -EBUSY;
-                       goto rw_remount_failed;
+                       goto restore_opts;
                }
-               up_read(&nilfs->ns_sem);
                if (sbi->s_snapshot_cno != nilfs_last_cno(nilfs)) {
                        printk(KERN_WARNING "NILFS (device %s): couldn't "
                               "remount because the current RO-mount is not "
                               "the latest one.\n",
                               sb->s_id);
                        err = -EINVAL;
-                       goto rw_remount_failed;
+                       goto restore_opts;
                }
                sb->s_flags &= ~MS_RDONLY;
                nilfs_clear_opt(sbi, SNAPSHOT);
@@ -989,31 +964,31 @@ static int nilfs_remount(struct super_block *sb, int *flags, char *data)
 
                err = nilfs_attach_segment_constructor(sbi);
                if (err)
-                       goto rw_remount_failed;
+                       goto restore_opts;
 
                down_write(&nilfs->ns_sem);
                nilfs_setup_super(sbi);
-               nilfs->ns_current = sbi;
                up_write(&nilfs->ns_sem);
 
-               up(&sb->s_bdev->bd_mount_sem);
+               nilfs->ns_current = sbi;
        }
  out:
+       up_write(&nilfs->ns_super_sem);
        unlock_kernel();
        return 0;
 
- rw_remount_failed:
-       up(&sb->s_bdev->bd_mount_sem);
  restore_opts:
        sb->s_flags = old_sb_flags;
        sbi->s_mount_opt = old_opts.mount_opt;
        sbi->s_snapshot_cno = old_opts.snapshot_cno;
+       up_write(&nilfs->ns_super_sem);
        unlock_kernel();
        return err;
 }
 
 struct nilfs_super_data {
        struct block_device *bdev;
+       struct nilfs_sb_info *sbi;
        __u64 cno;
        int flags;
 };
@@ -1071,27 +1046,8 @@ static int nilfs_set_bdev_super(struct super_block *s, void *data)
 static int nilfs_test_bdev_super(struct super_block *s, void *data)
 {
        struct nilfs_super_data *sd = data;
-       int ret;
-
-       if (s->s_bdev != sd->bdev)
-               return 0;
-
-       if (!((s->s_flags | sd->flags) & MS_RDONLY))
-               return 1; /* Reuse an old R/W-mode super_block */
-
-       if (s->s_flags & sd->flags & MS_RDONLY) {
-               if (down_read_trylock(&s->s_umount)) {
-                       ret = s->s_root &&
-                               (sd->cno == NILFS_SB(s)->s_snapshot_cno);
-                       up_read(&s->s_umount);
-                       /*
-                        * This path is locked with sb_lock by sget().
-                        * So, drop_super() causes deadlock.
-                        */
-                       return ret;
-               }
-       }
-       return 0;
+
+       return sd->sbi && s->s_fs_info == (void *)sd->sbi;
 }
 
 static int
@@ -1112,7 +1068,6 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
         * much more information than normal filesystems to identify mount
         * instance.  For snapshot mounts, not only a mount type (ro-mount
         * or rw-mount) but also a checkpoint number is required.
-        * The results are passed in sget() using nilfs_super_data.
         */
        sd.cno = 0;
        sd.flags = flags;
@@ -1127,7 +1082,7 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
                goto failed;
        }
 
-       down(&sd.bdev->bd_mount_sem);
+       mutex_lock(&nilfs->ns_mount_mutex);
 
        if (!sd.cno) {
                /*
@@ -1136,25 +1091,31 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
                 * (i.e. rw-mount or ro-mount), whereas rw-mount and
                 * ro-mount are mutually exclusive.
                 */
-               down_read(&nilfs->ns_sem);
+               down_read(&nilfs->ns_super_sem);
                if (nilfs->ns_current &&
                    ((nilfs->ns_current->s_super->s_flags ^ flags)
                     & MS_RDONLY)) {
-                       up_read(&nilfs->ns_sem);
+                       up_read(&nilfs->ns_super_sem);
                        err = -EBUSY;
                        goto failed_unlock;
                }
-               up_read(&nilfs->ns_sem);
+               up_read(&nilfs->ns_super_sem);
        }
 
        /*
-        * Search specified snapshot or R/W mode super_block
+        * Find existing nilfs_sb_info struct
         */
-       if (!sd.cno)
-               /* trying to get the latest checkpoint.  */
-               sd.cno = nilfs_last_cno(nilfs);
+       sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno);
 
+       /*
+        * Get super block instance holding the nilfs_sb_info struct.
+        * A new instance is allocated if no existing mount is present or
+        * existing instance has been unmounted.
+        */
        s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd);
+       if (sd.sbi)
+               nilfs_put_sbinfo(sd.sbi);
+
        if (IS_ERR(s)) {
                err = PTR_ERR(s);
                goto failed_unlock;
@@ -1176,7 +1137,7 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
                need_to_close = 0;
        }
 
-       up(&sd.bdev->bd_mount_sem);
+       mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
        if (need_to_close)
                close_bdev_exclusive(sd.bdev, flags);
@@ -1184,7 +1145,7 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
        return 0;
 
  failed_unlock:
-       up(&sd.bdev->bd_mount_sem);
+       mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
  failed:
        close_bdev_exclusive(sd.bdev, flags);
@@ -1193,14 +1154,14 @@ nilfs_get_sb(struct file_system_type *fs_type, int flags,
 
  cancel_new:
        /* Abandoning the newly allocated superblock */
-       up(&sd.bdev->bd_mount_sem);
+       mutex_unlock(&nilfs->ns_mount_mutex);
        put_nilfs(nilfs);
        up_write(&s->s_umount);
        deactivate_super(s);
        /*
         * deactivate_super() invokes close_bdev_exclusive().
         * We must finish all post-cleaning before this call;
-        * put_nilfs() and unlocking bd_mount_sem need the block device.
+        * put_nilfs() needs the block device.
         */
        return err;
 }