nilfs2: remove individual gfp constants for each metadata file
[safe/jmp/linux-2.6] / fs / nilfs2 / the_nilfs.c
index 69b6255..d4168e2 100644 (file)
 #include <linux/slab.h>
 #include <linux/blkdev.h>
 #include <linux/backing-dev.h>
+#include <linux/crc32.h>
 #include "nilfs.h"
 #include "segment.h"
 #include "alloc.h"
 #include "cpfile.h"
 #include "sufile.h"
 #include "dat.h"
-#include "seglist.h"
 #include "segbuf.h"
 
+
+static LIST_HEAD(nilfs_objects);
+static DEFINE_SPINLOCK(nilfs_lock);
+
 void nilfs_set_last_segment(struct the_nilfs *nilfs,
                            sector_t start_blocknr, u64 seq, __u64 cno)
 {
@@ -54,7 +58,7 @@ void nilfs_set_last_segment(struct the_nilfs *nilfs,
  * Return Value: On success, pointer to the_nilfs is returned.
  * On error, NULL is returned.
  */
-struct the_nilfs *alloc_nilfs(struct block_device *bdev)
+static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
 {
        struct the_nilfs *nilfs;
 
@@ -64,20 +68,60 @@ struct the_nilfs *alloc_nilfs(struct block_device *bdev)
 
        nilfs->ns_bdev = bdev;
        atomic_set(&nilfs->ns_count, 1);
-       atomic_set(&nilfs->ns_writer_refcount, -1);
        atomic_set(&nilfs->ns_ndirtyblks, 0);
        init_rwsem(&nilfs->ns_sem);
-       mutex_init(&nilfs->ns_writer_mutex);
+       init_rwsem(&nilfs->ns_super_sem);
+       mutex_init(&nilfs->ns_mount_mutex);
+       init_rwsem(&nilfs->ns_writer_sem);
+       INIT_LIST_HEAD(&nilfs->ns_list);
        INIT_LIST_HEAD(&nilfs->ns_supers);
        spin_lock_init(&nilfs->ns_last_segment_lock);
        nilfs->ns_gc_inodes_h = NULL;
-       INIT_LIST_HEAD(&nilfs->ns_used_segments);
        init_rwsem(&nilfs->ns_segctor_sem);
 
        return nilfs;
 }
 
 /**
+ * find_or_create_nilfs - find or create nilfs object
+ * @bdev: block device to which the_nilfs is related
+ *
+ * find_nilfs() looks up an existent nilfs object created on the
+ * device and gets the reference count of the object.  If no nilfs object
+ * is found on the device, a new nilfs object is allocated.
+ *
+ * Return Value: On success, pointer to the nilfs object is returned.
+ * On error, NULL is returned.
+ */
+struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
+{
+       struct the_nilfs *nilfs, *new = NULL;
+
+ retry:
+       spin_lock(&nilfs_lock);
+       list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
+               if (nilfs->ns_bdev == bdev) {
+                       get_nilfs(nilfs);
+                       spin_unlock(&nilfs_lock);
+                       if (new)
+                               put_nilfs(new);
+                       return nilfs; /* existing object */
+               }
+       }
+       if (new) {
+               list_add_tail(&new->ns_list, &nilfs_objects);
+               spin_unlock(&nilfs_lock);
+               return new; /* new object */
+       }
+       spin_unlock(&nilfs_lock);
+
+       new = alloc_nilfs(bdev);
+       if (new)
+               goto retry;
+       return NULL; /* insufficient memory */
+}
+
+/**
  * put_nilfs - release a reference to the_nilfs
  * @nilfs: the_nilfs structure to be released
  *
@@ -86,16 +130,22 @@ struct the_nilfs *alloc_nilfs(struct block_device *bdev)
  */
 void put_nilfs(struct the_nilfs *nilfs)
 {
-       if (!atomic_dec_and_test(&nilfs->ns_count))
+       spin_lock(&nilfs_lock);
+       if (!atomic_dec_and_test(&nilfs->ns_count)) {
+               spin_unlock(&nilfs_lock);
                return;
+       }
+       list_del_init(&nilfs->ns_list);
+       spin_unlock(&nilfs_lock);
+
        /*
-        * Increment of ns_count never occur below because the caller
+        * Increment of ns_count never occurs below because the caller
         * of get_nilfs() holds at least one reference to the_nilfs.
         * Thus its exclusion control is not required here.
         */
+
        might_sleep();
        if (nilfs_loaded(nilfs)) {
-               nilfs_dispose_used_segments(nilfs);
                nilfs_mdt_clear(nilfs->ns_sufile);
                nilfs_mdt_destroy(nilfs->ns_sufile);
                nilfs_mdt_clear(nilfs->ns_cpfile);
@@ -107,7 +157,8 @@ void put_nilfs(struct the_nilfs *nilfs)
        }
        if (nilfs_init(nilfs)) {
                nilfs_destroy_gccache(nilfs);
-               brelse(nilfs->ns_sbh);
+               brelse(nilfs->ns_sbh[0]);
+               brelse(nilfs->ns_sbh[1]);
        }
        kfree(nilfs);
 }
@@ -115,8 +166,10 @@ void put_nilfs(struct the_nilfs *nilfs)
 static int nilfs_load_super_root(struct the_nilfs *nilfs,
                                 struct nilfs_sb_info *sbi, sector_t sr_block)
 {
+       static struct lock_class_key dat_lock_key;
        struct buffer_head *bh_sr;
        struct nilfs_super_root *raw_sr;
+       struct nilfs_super_block **sbp = nilfs->ns_sbp;
        unsigned dat_entry_size, segment_usage_size, checkpoint_size;
        unsigned inode_size;
        int err;
@@ -126,31 +179,27 @@ static int nilfs_load_super_root(struct the_nilfs *nilfs,
                return err;
 
        down_read(&nilfs->ns_sem);
-       dat_entry_size = le16_to_cpu(nilfs->ns_sbp->s_dat_entry_size);
-       checkpoint_size = le16_to_cpu(nilfs->ns_sbp->s_checkpoint_size);
-       segment_usage_size = le16_to_cpu(nilfs->ns_sbp->s_segment_usage_size);
+       dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
+       checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
+       segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
        up_read(&nilfs->ns_sem);
 
        inode_size = nilfs->ns_inode_size;
 
        err = -ENOMEM;
-       nilfs->ns_dat = nilfs_mdt_new(
-               nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
+       nilfs->ns_dat = nilfs_mdt_new(nilfs, NULL, NILFS_DAT_INO);
        if (unlikely(!nilfs->ns_dat))
                goto failed;
 
-       nilfs->ns_gc_dat = nilfs_mdt_new(
-               nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
+       nilfs->ns_gc_dat = nilfs_mdt_new(nilfs, NULL, NILFS_DAT_INO);
        if (unlikely(!nilfs->ns_gc_dat))
                goto failed_dat;
 
-       nilfs->ns_cpfile = nilfs_mdt_new(
-               nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
+       nilfs->ns_cpfile = nilfs_mdt_new(nilfs, NULL, NILFS_CPFILE_INO);
        if (unlikely(!nilfs->ns_cpfile))
                goto failed_gc_dat;
 
-       nilfs->ns_sufile = nilfs_mdt_new(
-               nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
+       nilfs->ns_sufile = nilfs_mdt_new(nilfs, NULL, NILFS_SUFILE_INO);
        if (unlikely(!nilfs->ns_sufile))
                goto failed_cpfile;
 
@@ -162,6 +211,9 @@ static int nilfs_load_super_root(struct the_nilfs *nilfs,
        if (unlikely(err))
                goto failed_sufile;
 
+       lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key);
+       lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key);
+
        nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
        nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
                                 sizeof(struct nilfs_cpfile_header));
@@ -272,11 +324,8 @@ int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
                        nilfs_mdt_destroy(nilfs->ns_dat);
                        goto failed;
                }
-               if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED) {
-                       down_write(&nilfs->ns_sem);
-                       nilfs_update_last_segment(sbi, 0);
-                       up_write(&nilfs->ns_sem);
-               }
+               if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
+                       sbi->s_super->s_dirt = 1;
        }
 
        set_nilfs_loaded(nilfs);
@@ -298,9 +347,8 @@ static unsigned long long nilfs_max_size(unsigned int blkbits)
        return res;
 }
 
-static int
-nilfs_store_disk_layout(struct the_nilfs *nilfs, struct super_block *sb,
-                       struct nilfs_super_block *sbp)
+static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
+                                  struct nilfs_super_block *sbp)
 {
        if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
                printk(KERN_ERR "NILFS: revision mismatch "
@@ -311,6 +359,10 @@ nilfs_store_disk_layout(struct the_nilfs *nilfs, struct super_block *sb,
                       NILFS_CURRENT_REV, NILFS_MINOR_REV);
                return -EINVAL;
        }
+       nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
+       if (nilfs->ns_sbsize > BLOCK_SIZE)
+               return -EINVAL;
+
        nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
        nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
 
@@ -332,6 +384,122 @@ nilfs_store_disk_layout(struct the_nilfs *nilfs, struct super_block *sb,
        return 0;
 }
 
+static int nilfs_valid_sb(struct nilfs_super_block *sbp)
+{
+       static unsigned char sum[4];
+       const int sumoff = offsetof(struct nilfs_super_block, s_sum);
+       size_t bytes;
+       u32 crc;
+
+       if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
+               return 0;
+       bytes = le16_to_cpu(sbp->s_bytes);
+       if (bytes > BLOCK_SIZE)
+               return 0;
+       crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
+                      sumoff);
+       crc = crc32_le(crc, sum, 4);
+       crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
+                      bytes - sumoff - 4);
+       return crc == le32_to_cpu(sbp->s_sum);
+}
+
+static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
+{
+       return offset < ((le64_to_cpu(sbp->s_nsegments) *
+                         le32_to_cpu(sbp->s_blocks_per_segment)) <<
+                        (le32_to_cpu(sbp->s_log_block_size) + 10));
+}
+
+static void nilfs_release_super_block(struct the_nilfs *nilfs)
+{
+       int i;
+
+       for (i = 0; i < 2; i++) {
+               if (nilfs->ns_sbp[i]) {
+                       brelse(nilfs->ns_sbh[i]);
+                       nilfs->ns_sbh[i] = NULL;
+                       nilfs->ns_sbp[i] = NULL;
+               }
+       }
+}
+
+void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
+{
+       brelse(nilfs->ns_sbh[0]);
+       nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
+       nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
+       nilfs->ns_sbh[1] = NULL;
+       nilfs->ns_sbp[1] = NULL;
+}
+
+void nilfs_swap_super_block(struct the_nilfs *nilfs)
+{
+       struct buffer_head *tsbh = nilfs->ns_sbh[0];
+       struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
+
+       nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
+       nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
+       nilfs->ns_sbh[1] = tsbh;
+       nilfs->ns_sbp[1] = tsbp;
+}
+
+static int nilfs_load_super_block(struct the_nilfs *nilfs,
+                                 struct super_block *sb, int blocksize,
+                                 struct nilfs_super_block **sbpp)
+{
+       struct nilfs_super_block **sbp = nilfs->ns_sbp;
+       struct buffer_head **sbh = nilfs->ns_sbh;
+       u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
+       int valid[2], swp = 0;
+
+       sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
+                                       &sbh[0]);
+       sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
+
+       if (!sbp[0]) {
+               if (!sbp[1]) {
+                       printk(KERN_ERR "NILFS: unable to read superblock\n");
+                       return -EIO;
+               }
+               printk(KERN_WARNING
+                      "NILFS warning: unable to read primary superblock\n");
+       } else if (!sbp[1])
+               printk(KERN_WARNING
+                      "NILFS warning: unable to read secondary superblock\n");
+
+       valid[0] = nilfs_valid_sb(sbp[0]);
+       valid[1] = nilfs_valid_sb(sbp[1]);
+       swp = valid[1] &&
+               (!valid[0] ||
+                le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
+
+       if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
+               brelse(sbh[1]);
+               sbh[1] = NULL;
+               sbp[1] = NULL;
+               swp = 0;
+       }
+       if (!valid[swp]) {
+               nilfs_release_super_block(nilfs);
+               printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
+                      sb->s_id);
+               return -EINVAL;
+       }
+
+       if (swp) {
+               printk(KERN_WARNING "NILFS warning: broken superblock. "
+                      "using spare superblock.\n");
+               nilfs_swap_super_block(nilfs);
+       }
+
+       nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
+       nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
+       nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
+       *sbpp = sbp[0];
+       return 0;
+}
+
 /**
  * init_nilfs - initialize a NILFS instance.
  * @nilfs: the_nilfs structure
@@ -354,16 +522,15 @@ nilfs_store_disk_layout(struct the_nilfs *nilfs, struct super_block *sb,
 int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
 {
        struct super_block *sb = sbi->s_super;
-       struct buffer_head *sbh;
        struct nilfs_super_block *sbp;
        struct backing_dev_info *bdi;
        int blocksize;
-       int err = 0;
+       int err;
 
        down_write(&nilfs->ns_sem);
        if (nilfs_init(nilfs)) {
                /* Load values from existing the_nilfs */
-               sbp = nilfs->ns_sbp;
+               sbp = nilfs->ns_sbp[0];
                err = nilfs_store_magic_and_option(sb, sbp, data);
                if (err)
                        goto out;
@@ -379,36 +546,50 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
                goto out;
        }
 
-       sbp = nilfs_load_super_block(sb, &sbh);
-       if (!sbp) {
+       blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
+       if (!blocksize) {
+               printk(KERN_ERR "NILFS: unable to set blocksize\n");
                err = -EINVAL;
                goto out;
        }
+       err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
+       if (err)
+               goto out;
+
        err = nilfs_store_magic_and_option(sb, sbp, data);
        if (err)
                goto failed_sbh;
 
        blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
        if (sb->s_blocksize != blocksize) {
-               sbp = nilfs_reload_super_block(sb, &sbh, blocksize);
-               if (!sbp) {
+               int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
+
+               if (blocksize < hw_blocksize) {
+                       printk(KERN_ERR
+                              "NILFS: blocksize %d too small for device "
+                              "(sector-size = %d).\n",
+                              blocksize, hw_blocksize);
                        err = -EINVAL;
+                       goto failed_sbh;
+               }
+               nilfs_release_super_block(nilfs);
+               sb_set_blocksize(sb, blocksize);
+
+               err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
+               if (err)
                        goto out;
                        /* not failed_sbh; sbh is released automatically
                           when reloading fails. */
-               }
        }
        nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
 
-       err = nilfs_store_disk_layout(nilfs, sb, sbp);
+       err = nilfs_store_disk_layout(nilfs, sbp);
        if (err)
                goto failed_sbh;
 
        sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
 
        nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
-       nilfs->ns_sbh = sbh;
-       nilfs->ns_sbp = sbp;
 
        bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
        if (!bdi)
@@ -445,7 +626,7 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
        return err;
 
  failed_sbh:
-       brelse(sbh);
+       nilfs_release_super_block(nilfs);
        goto out;
 }
 
@@ -463,22 +644,6 @@ int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
        return err;
 }
 
-void nilfs_dispose_used_segments(struct the_nilfs *nilfs)
-{
-       struct nilfs_segment_entry *ent, *n;
-
-       /* nilfs->sem must be locked by the caller. */
-       if (!nilfs_loaded(nilfs))
-               return;
-
-       list_for_each_entry_safe(ent, n, &nilfs->ns_used_segments, list) {
-               list_del_init(&ent->list);
-               nilfs_segment_usage_clear_volatile_active(ent->raw_su);
-               nilfs_close_segment_entry(ent, nilfs->ns_sufile);
-               nilfs_free_segment_entry(ent);
-       }
-}
-
 int nilfs_near_disk_full(struct the_nilfs *nilfs)
 {
        struct inode *sufile = nilfs->ns_sufile;
@@ -495,13 +660,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;
 
@@ -518,6 +733,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;
 }