nilfs2: fix lockdep warning between regular file and inode file
[safe/jmp/linux-2.6] / fs / nilfs2 / the_nilfs.c
index 45dbf6a..8b88898 100644 (file)
@@ -32,7 +32,6 @@
 #include "cpfile.h"
 #include "sufile.h"
 #include "dat.h"
-#include "seglist.h"
 #include "segbuf.h"
 
 
@@ -72,6 +71,8 @@ static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
        atomic_set(&nilfs->ns_writer_refcount, -1);
        atomic_set(&nilfs->ns_ndirtyblks, 0);
        init_rwsem(&nilfs->ns_sem);
+       init_rwsem(&nilfs->ns_super_sem);
+       mutex_init(&nilfs->ns_mount_mutex);
        mutex_init(&nilfs->ns_writer_mutex);
        INIT_LIST_HEAD(&nilfs->ns_list);
        INIT_LIST_HEAD(&nilfs->ns_supers);
@@ -664,13 +665,63 @@ int nilfs_near_disk_full(struct the_nilfs *nilfs)
        return ret;
 }
 
+/**
+ * nilfs_find_sbinfo - find existing nilfs_sb_info structure
+ * @nilfs: nilfs object
+ * @rw_mount: mount type (non-zero value for read/write mount)
+ * @cno: checkpoint number (zero for read-only mount)
+ *
+ * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
+ * @rw_mount and @cno (in case of snapshots) matched.  If no instance
+ * was found, NULL is returned.  Although the super block instance can
+ * be unmounted after this function returns, the nilfs_sb_info struct
+ * is kept on memory until nilfs_put_sbinfo() is called.
+ */
+struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
+                                       int rw_mount, __u64 cno)
+{
+       struct nilfs_sb_info *sbi;
+
+       down_read(&nilfs->ns_super_sem);
+       /*
+        * The SNAPSHOT flag and sb->s_flags are supposed to be
+        * protected with nilfs->ns_super_sem.
+        */
+       sbi = nilfs->ns_current;
+       if (rw_mount) {
+               if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
+                       goto found; /* read/write mount */
+               else
+                       goto out;
+       } else if (cno == 0) {
+               if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
+                       goto found; /* read-only mount */
+               else
+                       goto out;
+       }
+
+       list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
+               if (nilfs_test_opt(sbi, SNAPSHOT) &&
+                   sbi->s_snapshot_cno == cno)
+                       goto found; /* snapshot mount */
+       }
+ out:
+       up_read(&nilfs->ns_super_sem);
+       return NULL;
+
+ found:
+       atomic_inc(&sbi->s_count);
+       up_read(&nilfs->ns_super_sem);
+       return sbi;
+}
+
 int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
                                int snapshot_mount)
 {
        struct nilfs_sb_info *sbi;
        int ret = 0;
 
-       down_read(&nilfs->ns_sem);
+       down_read(&nilfs->ns_super_sem);
        if (cno == 0 || cno > nilfs->ns_cno)
                goto out_unlock;
 
@@ -687,6 +738,6 @@ int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
                ret++;
 
  out_unlock:
-       up_read(&nilfs->ns_sem);
+       up_read(&nilfs->ns_super_sem);
        return ret;
 }