nilfs2: move mount options to nilfs object
[linux-2.6.git] / fs / nilfs2 / the_nilfs.c
index ad391a8..d377533 100644 (file)
@@ -35,8 +35,7 @@
 #include "segbuf.h"
 
 
-static LIST_HEAD(nilfs_objects);
-static DEFINE_SPINLOCK(nilfs_lock);
+static int nilfs_valid_sb(struct nilfs_super_block *sbp);
 
 void nilfs_set_last_segment(struct the_nilfs *nilfs,
                            sector_t start_blocknr, u64 seq, __u64 cno)
@@ -45,20 +44,27 @@ void nilfs_set_last_segment(struct the_nilfs *nilfs,
        nilfs->ns_last_pseg = start_blocknr;
        nilfs->ns_last_seq = seq;
        nilfs->ns_last_cno = cno;
+
+       if (!nilfs_sb_dirty(nilfs)) {
+               if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
+                       goto stay_cursor;
+
+               set_nilfs_sb_dirty(nilfs);
+       }
+       nilfs->ns_prev_seq = nilfs->ns_last_seq;
+
+ stay_cursor:
        spin_unlock(&nilfs->ns_last_segment_lock);
 }
 
 /**
- * alloc_nilfs - allocate the_nilfs structure
+ * alloc_nilfs - allocate a nilfs object
  * @bdev: block device to which the_nilfs is related
  *
- * alloc_nilfs() allocates memory for the_nilfs and
- * initializes its reference count and locks.
- *
  * Return Value: On success, pointer to the_nilfs is returned.
  * On error, NULL is returned.
  */
-static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
+struct the_nilfs *alloc_nilfs(struct block_device *bdev)
 {
        struct the_nilfs *nilfs;
 
@@ -67,96 +73,25 @@ static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
                return NULL;
 
        nilfs->ns_bdev = bdev;
-       atomic_set(&nilfs->ns_count, 1);
        atomic_set(&nilfs->ns_ndirtyblks, 0);
        init_rwsem(&nilfs->ns_sem);
-       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);
+       INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
        spin_lock_init(&nilfs->ns_last_segment_lock);
-       nilfs->ns_gc_inodes_h = NULL;
+       nilfs->ns_cptree = RB_ROOT;
+       spin_lock_init(&nilfs->ns_cptree_lock);
        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
- *
- * put_nilfs() decrements a reference counter of the_nilfs.
- * If the reference count reaches zero, the_nilfs is freed.
+ * destroy_nilfs - destroy nilfs object
+ * @nilfs: nilfs object to be released
  */
-void put_nilfs(struct the_nilfs *nilfs)
+void destroy_nilfs(struct the_nilfs *nilfs)
 {
-       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 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_mdt_clear(nilfs->ns_sufile);
-               nilfs_mdt_destroy(nilfs->ns_sufile);
-               nilfs_mdt_clear(nilfs->ns_cpfile);
-               nilfs_mdt_destroy(nilfs->ns_cpfile);
-               nilfs_mdt_clear(nilfs->ns_dat);
-               nilfs_mdt_destroy(nilfs->ns_dat);
-               /* XXX: how and when to clear nilfs->ns_gc_dat? */
-               nilfs_mdt_destroy(nilfs->ns_gc_dat);
-       }
        if (nilfs_init(nilfs)) {
-               nilfs_destroy_gccache(nilfs);
                brelse(nilfs->ns_sbh[0]);
                brelse(nilfs->ns_sbh[1]);
        }
@@ -164,17 +99,17 @@ 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)
+                                struct super_block *sb, 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;
+       struct nilfs_inode *rawi;
        unsigned dat_entry_size, segment_usage_size, checkpoint_size;
        unsigned inode_size;
        int err;
 
-       err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
+       err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
        if (unlikely(err))
                return err;
 
@@ -186,55 +121,22 @@ static int nilfs_load_super_root(struct the_nilfs *nilfs,
 
        inode_size = nilfs->ns_inode_size;
 
-       err = -ENOMEM;
-       nilfs->ns_dat = nilfs_mdt_new(nilfs, NULL, NILFS_DAT_INO);
-       if (unlikely(!nilfs->ns_dat))
+       rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
+       err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
+       if (err)
                goto failed;
 
-       nilfs->ns_gc_dat = nilfs_mdt_new(nilfs, NULL, NILFS_DAT_INO);
-       if (unlikely(!nilfs->ns_gc_dat))
+       rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
+       err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
+       if (err)
                goto failed_dat;
 
-       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);
-       if (unlikely(!nilfs->ns_sufile))
+       rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
+       err = nilfs_sufile_read(sb, segment_usage_size, rawi,
+                               &nilfs->ns_sufile);
+       if (err)
                goto failed_cpfile;
 
-       err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
-       if (unlikely(err))
-               goto failed_sufile;
-
-       err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
-       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));
-       nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
-                                sizeof(struct nilfs_sufile_header));
-
-       err = nilfs_mdt_read_inode_direct(
-               nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
-       if (unlikely(err))
-               goto failed_sufile;
-
-       err = nilfs_mdt_read_inode_direct(
-               nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
-       if (unlikely(err))
-               goto failed_sufile;
-
-       err = nilfs_mdt_read_inode_direct(
-               nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
-       if (unlikely(err))
-               goto failed_sufile;
-
        raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
        nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
 
@@ -242,17 +144,11 @@ static int nilfs_load_super_root(struct the_nilfs *nilfs,
        brelse(bh_sr);
        return err;
 
- failed_sufile:
-       nilfs_mdt_destroy(nilfs->ns_sufile);
-
  failed_cpfile:
-       nilfs_mdt_destroy(nilfs->ns_cpfile);
-
- failed_gc_dat:
-       nilfs_mdt_destroy(nilfs->ns_gc_dat);
+       iput(nilfs->ns_cpfile);
 
  failed_dat:
-       nilfs_mdt_destroy(nilfs->ns_dat);
+       iput(nilfs->ns_dat);
        goto failed;
 }
 
@@ -268,6 +164,37 @@ static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
 }
 
 /**
+ * nilfs_store_log_cursor - load log cursor from a super block
+ * @nilfs: nilfs object
+ * @sbp: buffer storing super block to be read
+ *
+ * nilfs_store_log_cursor() reads the last position of the log
+ * containing a super root from a given super block, and initializes
+ * relevant information on the nilfs object preparatory for log
+ * scanning and recovery.
+ */
+static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
+                                 struct nilfs_super_block *sbp)
+{
+       int ret = 0;
+
+       nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
+       nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
+       nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
+
+       nilfs->ns_prev_seq = nilfs->ns_last_seq;
+       nilfs->ns_seg_seq = nilfs->ns_last_seq;
+       nilfs->ns_segnum =
+               nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
+       nilfs->ns_cno = nilfs->ns_last_cno + 1;
+       if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
+               printk(KERN_ERR "NILFS invalid last segment number.\n");
+               ret = -EINVAL;
+       }
+       return ret;
+}
+
+/**
  * load_nilfs - load and recover the nilfs
  * @nilfs: the_nilfs structure to be released
  * @sbi: nilfs_sb_info used to recover past segment
@@ -281,54 +208,139 @@ int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
        struct nilfs_recovery_info ri;
        unsigned int s_flags = sbi->s_super->s_flags;
        int really_read_only = bdev_read_only(nilfs->ns_bdev);
-       unsigned valid_fs;
-       int err = 0;
+       int valid_fs = nilfs_valid_fs(nilfs);
+       int err;
+
+       if (!valid_fs) {
+               printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
+               if (s_flags & MS_RDONLY) {
+                       printk(KERN_INFO "NILFS: INFO: recovery "
+                              "required for readonly filesystem.\n");
+                       printk(KERN_INFO "NILFS: write access will "
+                              "be enabled during recovery.\n");
+               }
+       }
 
        nilfs_init_recovery_info(&ri);
 
-       down_write(&nilfs->ns_sem);
-       valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
-       up_write(&nilfs->ns_sem);
+       err = nilfs_search_super_root(nilfs, &ri);
+       if (unlikely(err)) {
+               struct nilfs_super_block **sbp = nilfs->ns_sbp;
+               int blocksize;
 
-       if (!valid_fs && (s_flags & MS_RDONLY)) {
-               printk(KERN_INFO "NILFS: INFO: recovery "
-                      "required for readonly filesystem.\n");
-               if (really_read_only) {
-                       printk(KERN_ERR "NILFS: write access "
-                              "unavailable, cannot proceed.\n");
-                       err = -EROFS;
-                       goto failed;
+               if (err != -EINVAL)
+                       goto scan_error;
+
+               if (!nilfs_valid_sb(sbp[1])) {
+                       printk(KERN_WARNING
+                              "NILFS warning: unable to fall back to spare"
+                              "super block\n");
+                       goto scan_error;
+               }
+               printk(KERN_INFO
+                      "NILFS: try rollback from an earlier position\n");
+
+               /*
+                * restore super block with its spare and reconfigure
+                * relevant states of the nilfs object.
+                */
+               memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
+               nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
+               nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
+
+               /* verify consistency between two super blocks */
+               blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
+               if (blocksize != nilfs->ns_blocksize) {
+                       printk(KERN_WARNING
+                              "NILFS warning: blocksize differs between "
+                              "two super blocks (%d != %d)\n",
+                              blocksize, nilfs->ns_blocksize);
+                       goto scan_error;
                }
-               printk(KERN_INFO "NILFS: write access will "
-                      "be enabled during recovery.\n");
-               sbi->s_super->s_flags &= ~MS_RDONLY;
-       }
 
-       err = nilfs_search_super_root(nilfs, sbi, &ri);
-       if (unlikely(err)) {
-               printk(KERN_ERR "NILFS: error searching super root.\n");
-               goto failed;
+               err = nilfs_store_log_cursor(nilfs, sbp[0]);
+               if (err)
+                       goto scan_error;
+
+               /* drop clean flag to allow roll-forward and recovery */
+               nilfs->ns_mount_state &= ~NILFS_VALID_FS;
+               valid_fs = 0;
+
+               err = nilfs_search_super_root(nilfs, &ri);
+               if (err)
+                       goto scan_error;
        }
 
-       err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
+       err = nilfs_load_super_root(nilfs, sbi->s_super, ri.ri_super_root);
        if (unlikely(err)) {
                printk(KERN_ERR "NILFS: error loading super root.\n");
                goto failed;
        }
 
-       if (!valid_fs) {
-               err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
-               if (unlikely(err)) {
-                       nilfs_mdt_destroy(nilfs->ns_cpfile);
-                       nilfs_mdt_destroy(nilfs->ns_sufile);
-                       nilfs_mdt_destroy(nilfs->ns_dat);
-                       goto failed;
+       if (valid_fs)
+               goto skip_recovery;
+
+       if (s_flags & MS_RDONLY) {
+               __u64 features;
+
+               if (nilfs_test_opt(nilfs, NORECOVERY)) {
+                       printk(KERN_INFO "NILFS: norecovery option specified. "
+                              "skipping roll-forward recovery\n");
+                       goto skip_recovery;
                }
-               if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
-                       sbi->s_super->s_dirt = 1;
+               features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
+                       ~NILFS_FEATURE_COMPAT_RO_SUPP;
+               if (features) {
+                       printk(KERN_ERR "NILFS: couldn't proceed with "
+                              "recovery because of unsupported optional "
+                              "features (%llx)\n",
+                              (unsigned long long)features);
+                       err = -EROFS;
+                       goto failed_unload;
+               }
+               if (really_read_only) {
+                       printk(KERN_ERR "NILFS: write access "
+                              "unavailable, cannot proceed.\n");
+                       err = -EROFS;
+                       goto failed_unload;
+               }
+               sbi->s_super->s_flags &= ~MS_RDONLY;
+       } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
+               printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
+                      "option was specified for a read/write mount\n");
+               err = -EINVAL;
+               goto failed_unload;
+       }
+
+       err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
+       if (err)
+               goto failed_unload;
+
+       down_write(&nilfs->ns_sem);
+       nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
+       err = nilfs_cleanup_super(sbi);
+       up_write(&nilfs->ns_sem);
+
+       if (err) {
+               printk(KERN_ERR "NILFS: failed to update super block. "
+                      "recovery unfinished.\n");
+               goto failed_unload;
        }
+       printk(KERN_INFO "NILFS: recovery complete.\n");
+
+ skip_recovery:
+       nilfs_clear_recovery_info(&ri);
+       sbi->s_super->s_flags = s_flags;
+       return 0;
 
-       set_nilfs_loaded(nilfs);
+ scan_error:
+       printk(KERN_ERR "NILFS: error searching super root.\n");
+       goto failed;
+
+ failed_unload:
+       iput(nilfs->ns_cpfile);
+       iput(nilfs->ns_sufile);
+       iput(nilfs->ns_dat);
 
  failed:
        nilfs_clear_recovery_info(&ri);
@@ -350,8 +362,8 @@ static unsigned long long nilfs_max_size(unsigned int blkbits)
 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 "
+       if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
+               printk(KERN_ERR "NILFS: unsupported revision "
                       "(superblock rev.=%d.%d, current rev.=%d.%d). "
                       "Please check the version of mkfs.nilfs.\n",
                       le32_to_cpu(sbp->s_rev_level),
@@ -368,7 +380,7 @@ static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
 
        nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
        if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
-               printk(KERN_ERR "NILFS: too short segment. \n");
+               printk(KERN_ERR "NILFS: too short segment.\n");
                return -EINVAL;
        }
 
@@ -463,16 +475,23 @@ static int nilfs_load_super_block(struct the_nilfs *nilfs,
                        return -EIO;
                }
                printk(KERN_WARNING
-                      "NILFS warning: unable to read primary superblock\n");
-       } else if (!sbp[1])
+                      "NILFS warning: unable to read primary superblock "
+                      "(blocksize = %d)\n", blocksize);
+       } else if (!sbp[1]) {
                printk(KERN_WARNING
-                      "NILFS warning: unable to read secondary superblock\n");
+                      "NILFS warning: unable to read secondary superblock "
+                      "(blocksize = %d)\n", blocksize);
+       }
 
+       /*
+        * Compare two super blocks and set 1 in swp if the secondary
+        * super block is valid and newer.  Otherwise, set 0 in swp.
+        */
        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));
+       swp = valid[1] && (!valid[0] ||
+                          le64_to_cpu(sbp[1]->s_last_cno) >
+                          le64_to_cpu(sbp[0]->s_last_cno));
 
        if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
                brelse(sbh[1]);
@@ -487,14 +506,14 @@ static int nilfs_load_super_block(struct the_nilfs *nilfs,
                return -EINVAL;
        }
 
-       if (swp) {
+       if (!valid[!swp])
                printk(KERN_WARNING "NILFS warning: broken superblock. "
-                      "using spare superblock.\n");
+                      "using spare superblock (blocksize = %d).\n", blocksize);
+       if (swp)
                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_sbwcount = 0;
+       nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
        nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
        *sbpp = sbp[0];
        return 0;
@@ -509,12 +528,7 @@ static int nilfs_load_super_block(struct the_nilfs *nilfs,
  *
  * init_nilfs() performs common initialization per block device (e.g.
  * reading the super block, getting disk layout information, initializing
- * shared fields in the_nilfs). It takes on some portion of the jobs
- * typically done by a fill_super() routine. This division arises from
- * the nature that multiple NILFS instances may be simultaneously
- * mounted on a device.
- * For multiple mounts on the same device, only the first mount
- * invokes these tasks.
+ * shared fields in the_nilfs).
  *
  * Return Value: On success, 0 is returned. On error, a negative error
  * code is returned.
@@ -523,30 +537,12 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
 {
        struct super_block *sb = sbi->s_super;
        struct nilfs_super_block *sbp;
-       struct backing_dev_info *bdi;
        int blocksize;
        int err;
 
        down_write(&nilfs->ns_sem);
-       if (nilfs_init(nilfs)) {
-               /* Load values from existing the_nilfs */
-               sbp = nilfs->ns_sbp[0];
-               err = nilfs_store_magic_and_option(sb, sbp, data);
-               if (err)
-                       goto out;
 
-               blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
-               if (sb->s_blocksize != blocksize &&
-                   !sb_set_blocksize(sb, blocksize)) {
-                       printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
-                              blocksize);
-                       err = -EINVAL;
-               }
-               sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
-               goto out;
-       }
-
-       blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
+       blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
        if (!blocksize) {
                printk(KERN_ERR "NILFS: unable to set blocksize\n");
                err = -EINVAL;
@@ -560,7 +556,18 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
        if (err)
                goto failed_sbh;
 
+       err = nilfs_check_feature_compatibility(sb, sbp);
+       if (err)
+               goto failed_sbh;
+
        blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
+       if (blocksize < NILFS_MIN_BLOCK_SIZE ||
+           blocksize > NILFS_MAX_BLOCK_SIZE) {
+               printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
+                      "filesystem blocksize %d\n", blocksize);
+               err = -EINVAL;
+               goto failed_sbh;
+       }
        if (sb->s_blocksize != blocksize) {
                int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
 
@@ -582,6 +589,7 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
                           when reloading fails. */
        }
        nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
+       nilfs->ns_blocksize = blocksize;
 
        err = nilfs_store_disk_layout(nilfs, sbp);
        if (err)
@@ -591,29 +599,7 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
 
        nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
 
-       bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
-       nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
-
-       /* Finding last segment */
-       nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
-       nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
-       nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
-
-       nilfs->ns_seg_seq = nilfs->ns_last_seq;
-       nilfs->ns_segnum =
-               nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
-       nilfs->ns_cno = nilfs->ns_last_cno + 1;
-       if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
-               printk(KERN_ERR "NILFS invalid last segment number.\n");
-               err = -EINVAL;
-               goto failed_sbh;
-       }
-       /* Dummy values  */
-       nilfs->ns_free_segments_count =
-               nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
-
-       /* Initialize gcinode cache */
-       err = nilfs_init_gccache(nilfs);
+       err = nilfs_store_log_cursor(nilfs, sbp);
        if (err)
                goto failed_sbh;
 
@@ -628,109 +614,151 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
        goto out;
 }
 
+int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
+                           size_t nsegs)
+{
+       sector_t seg_start, seg_end;
+       sector_t start = 0, nblocks = 0;
+       unsigned int sects_per_block;
+       __u64 *sn;
+       int ret = 0;
+
+       sects_per_block = (1 << nilfs->ns_blocksize_bits) /
+               bdev_logical_block_size(nilfs->ns_bdev);
+       for (sn = segnump; sn < segnump + nsegs; sn++) {
+               nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
+
+               if (!nblocks) {
+                       start = seg_start;
+                       nblocks = seg_end - seg_start + 1;
+               } else if (start + nblocks == seg_start) {
+                       nblocks += seg_end - seg_start + 1;
+               } else {
+                       ret = blkdev_issue_discard(nilfs->ns_bdev,
+                                                  start * sects_per_block,
+                                                  nblocks * sects_per_block,
+                                                  GFP_NOFS, 0);
+                       if (ret < 0)
+                               return ret;
+                       nblocks = 0;
+               }
+       }
+       if (nblocks)
+               ret = blkdev_issue_discard(nilfs->ns_bdev,
+                                          start * sects_per_block,
+                                          nblocks * sects_per_block,
+                                          GFP_NOFS, 0);
+       return ret;
+}
+
 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
 {
-       struct inode *dat = nilfs_dat_inode(nilfs);
        unsigned long ncleansegs;
-       int err;
 
-       down_read(&NILFS_MDT(dat)->mi_sem);     /* XXX */
-       err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
-       up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
-       if (likely(!err))
-               *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
-       return err;
+       down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
+       ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
+       up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
+       *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
+       return 0;
 }
 
 int nilfs_near_disk_full(struct the_nilfs *nilfs)
 {
-       struct inode *sufile = nilfs->ns_sufile;
        unsigned long ncleansegs, nincsegs;
-       int ret;
-
-       ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
-       if (likely(!ret)) {
-               nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
-                       nilfs->ns_blocks_per_segment + 1;
-               if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
-                       ret++;
-       }
-       return ret;
+
+       ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
+       nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
+               nilfs->ns_blocks_per_segment + 1;
+
+       return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
 }
 
-/**
- * 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_root *nilfs_lookup_root(struct the_nilfs *nilfs, __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;
+       struct rb_node *n;
+       struct nilfs_root *root;
+
+       spin_lock(&nilfs->ns_cptree_lock);
+       n = nilfs->ns_cptree.rb_node;
+       while (n) {
+               root = rb_entry(n, struct nilfs_root, rb_node);
+
+               if (cno < root->cno) {
+                       n = n->rb_left;
+               } else if (cno > root->cno) {
+                       n = n->rb_right;
+               } else {
+                       atomic_inc(&root->count);
+                       spin_unlock(&nilfs->ns_cptree_lock);
+                       return root;
+               }
        }
+       spin_unlock(&nilfs->ns_cptree_lock);
 
-       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_root *
+nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
 {
-       struct nilfs_sb_info *sbi;
-       int ret = 0;
+       struct rb_node **p, *parent;
+       struct nilfs_root *root, *new;
+
+       root = nilfs_lookup_root(nilfs, cno);
+       if (root)
+               return root;
+
+       new = kmalloc(sizeof(*root), GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       spin_lock(&nilfs->ns_cptree_lock);
 
-       down_read(&nilfs->ns_super_sem);
-       if (cno == 0 || cno > nilfs->ns_cno)
-               goto out_unlock;
+       p = &nilfs->ns_cptree.rb_node;
+       parent = NULL;
 
-       list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
-               if (sbi->s_snapshot_cno == cno &&
-                   (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
-                                       /* exclude read-only mounts */
-                       ret++;
-                       break;
+       while (*p) {
+               parent = *p;
+               root = rb_entry(parent, struct nilfs_root, rb_node);
+
+               if (cno < root->cno) {
+                       p = &(*p)->rb_left;
+               } else if (cno > root->cno) {
+                       p = &(*p)->rb_right;
+               } else {
+                       atomic_inc(&root->count);
+                       spin_unlock(&nilfs->ns_cptree_lock);
+                       kfree(new);
+                       return root;
                }
        }
-       /* for protecting recent checkpoints */
-       if (cno >= nilfs_last_cno(nilfs))
-               ret++;
 
- out_unlock:
-       up_read(&nilfs->ns_super_sem);
-       return ret;
+       new->cno = cno;
+       new->ifile = NULL;
+       new->nilfs = nilfs;
+       atomic_set(&new->count, 1);
+       atomic_set(&new->inodes_count, 0);
+       atomic_set(&new->blocks_count, 0);
+
+       rb_link_node(&new->rb_node, parent, p);
+       rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
+
+       spin_unlock(&nilfs->ns_cptree_lock);
+
+       return new;
+}
+
+void nilfs_put_root(struct nilfs_root *root)
+{
+       if (atomic_dec_and_test(&root->count)) {
+               struct the_nilfs *nilfs = root->nilfs;
+
+               spin_lock(&nilfs->ns_cptree_lock);
+               rb_erase(&root->rb_node, &nilfs->ns_cptree);
+               spin_unlock(&nilfs->ns_cptree_lock);
+               if (root->ifile)
+                       iput(root->ifile);
+
+               kfree(root);
+       }
 }