]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - fs/nilfs2/the_nilfs.c
nilfs2: get rid of nilfs_bmap_union
[karo-tx-linux.git] / fs / nilfs2 / the_nilfs.c
index 4a9e8a059638b8b7ac5a0e2cab65a50d6a54c32d..f2efc8c5be7fbd6a75f7f4773b095ec9c63037d3 100644 (file)
@@ -38,6 +38,8 @@
 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,6 +47,16 @@ 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);
 }
 
@@ -246,6 +258,37 @@ static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
        nilfs_dispose_segment_list(&ri->ri_used_segments);
 }
 
+/**
+ * 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
@@ -286,8 +329,50 @@ int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
 
        err = nilfs_search_super_root(nilfs, &ri);
        if (unlikely(err)) {
-               printk(KERN_ERR "NILFS: error searching super root.\n");
-               goto failed;
+               struct nilfs_super_block **sbp = nilfs->ns_sbp;
+               int blocksize;
+
+               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;
+               }
+
+               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, ri.ri_super_root);
@@ -324,9 +409,8 @@ int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
                goto failed_unload;
 
        down_write(&nilfs->ns_sem);
-       nilfs->ns_mount_state |= NILFS_VALID_FS;
-       nilfs->ns_sbp[0]->s_state = cpu_to_le16(nilfs->ns_mount_state);
-       err = nilfs_commit_super(sbi, 1);
+       nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
+       err = nilfs_cleanup_super(sbi);
        up_write(&nilfs->ns_sem);
 
        if (err) {
@@ -342,6 +426,10 @@ int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
        sbi->s_super->s_flags = s_flags;
        return 0;
 
+ scan_error:
+       printk(KERN_ERR "NILFS: error searching super root.\n");
+       goto failed;
+
  failed_unload:
        nilfs_mdt_destroy(nilfs->ns_cpfile);
        nilfs_mdt_destroy(nilfs->ns_sufile);
@@ -514,8 +602,8 @@ static int nilfs_load_super_block(struct the_nilfs *nilfs,
                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;
@@ -616,20 +704,9 @@ int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
        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;
+       err = nilfs_store_log_cursor(nilfs, sbp);
+       if (err)
                goto failed_sbh;
-       }
 
        /* Initialize gcinode cache */
        err = nilfs_init_gccache(nilfs);