cifs: don't instantiate new dentries in readdir for inodes that need to be revalidate...
[linux-3.10.git] / fs / nilfs2 / recovery.c
index 017bedc..ff00a0b 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/buffer_head.h>
 #include <linux/blkdev.h>
 #include <linux/swap.h>
+#include <linux/slab.h>
 #include <linux/crc32.h>
 #include "nilfs.h"
 #include "segment.h"
@@ -90,25 +91,9 @@ static int nilfs_warn_segment_error(int err)
        return -EINVAL;
 }
 
-static void store_segsum_info(struct nilfs_segsum_info *ssi,
-                             struct nilfs_segment_summary *sum,
-                             unsigned int blocksize)
-{
-       ssi->flags = le16_to_cpu(sum->ss_flags);
-       ssi->seg_seq = le64_to_cpu(sum->ss_seq);
-       ssi->ctime = le64_to_cpu(sum->ss_create);
-       ssi->next = le64_to_cpu(sum->ss_next);
-       ssi->nblocks = le32_to_cpu(sum->ss_nblocks);
-       ssi->nfinfo = le32_to_cpu(sum->ss_nfinfo);
-       ssi->sumbytes = le32_to_cpu(sum->ss_sumbytes);
-
-       ssi->nsumblk = DIV_ROUND_UP(ssi->sumbytes, blocksize);
-       ssi->nfileblk = ssi->nblocks - ssi->nsumblk - !!NILFS_SEG_HAS_SR(ssi);
-}
-
 /**
- * calc_crc_cont - check CRC of blocks continuously
- * @sbi: nilfs_sb_info
+ * nilfs_compute_checksum - compute checksum of blocks continuously
+ * @nilfs: nilfs object
  * @bhs: buffer head of start block
  * @sum: place to store result
  * @offset: offset bytes in the first block
@@ -116,23 +101,25 @@ static void store_segsum_info(struct nilfs_segsum_info *ssi,
  * @start: DBN of start block
  * @nblock: number of blocks to be checked
  */
-static int calc_crc_cont(struct nilfs_sb_info *sbi, struct buffer_head *bhs,
-                        u32 *sum, unsigned long offset, u64 check_bytes,
-                        sector_t start, unsigned long nblock)
+static int nilfs_compute_checksum(struct the_nilfs *nilfs,
+                                 struct buffer_head *bhs, u32 *sum,
+                                 unsigned long offset, u64 check_bytes,
+                                 sector_t start, unsigned long nblock)
 {
-       unsigned long blocksize = sbi->s_super->s_blocksize;
+       unsigned int blocksize = nilfs->ns_blocksize;
        unsigned long size;
        u32 crc;
 
        BUG_ON(offset >= blocksize);
        check_bytes -= offset;
        size = min_t(u64, check_bytes, blocksize - offset);
-       crc = crc32_le(sbi->s_nilfs->ns_crc_seed,
+       crc = crc32_le(nilfs->ns_crc_seed,
                       (unsigned char *)bhs->b_data + offset, size);
        if (--nblock > 0) {
                do {
-                       struct buffer_head *bh
-                               = sb_bread(sbi->s_super, ++start);
+                       struct buffer_head *bh;
+
+                       bh = __bread(nilfs->ns_bdev, ++start, blocksize);
                        if (!bh)
                                return -EIO;
                        check_bytes -= size;
@@ -147,12 +134,12 @@ static int calc_crc_cont(struct nilfs_sb_info *sbi, struct buffer_head *bhs,
 
 /**
  * nilfs_read_super_root_block - read super root block
- * @sb: super_block
+ * @nilfs: nilfs object
  * @sr_block: disk block number of the super root block
  * @pbh: address of a buffer_head pointer to return super root buffer
  * @check: CRC check flag
  */
-int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
+int nilfs_read_super_root_block(struct the_nilfs *nilfs, sector_t sr_block,
                                struct buffer_head **pbh, int check)
 {
        struct buffer_head *bh_sr;
@@ -161,7 +148,7 @@ int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
        int ret;
 
        *pbh = NULL;
-       bh_sr = sb_bread(sb, sr_block);
+       bh_sr = __bread(nilfs->ns_bdev, sr_block, nilfs->ns_blocksize);
        if (unlikely(!bh_sr)) {
                ret = NILFS_SEG_FAIL_IO;
                goto failed;
@@ -171,12 +158,13 @@ int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
        if (check) {
                unsigned bytes = le16_to_cpu(sr->sr_bytes);
 
-               if (bytes == 0 || bytes > sb->s_blocksize) {
+               if (bytes == 0 || bytes > nilfs->ns_blocksize) {
                        ret = NILFS_SEG_FAIL_CHECKSUM_SUPER_ROOT;
                        goto failed_bh;
                }
-               if (calc_crc_cont(NILFS_SB(sb), bh_sr, &crc,
-                                 sizeof(sr->sr_sum), bytes, sr_block, 1)) {
+               if (nilfs_compute_checksum(
+                           nilfs, bh_sr, &crc, sizeof(sr->sr_sum), bytes,
+                           sr_block, 1)) {
                        ret = NILFS_SEG_FAIL_IO;
                        goto failed_bh;
                }
@@ -196,64 +184,76 @@ int nilfs_read_super_root_block(struct super_block *sb, sector_t sr_block,
 }
 
 /**
- * load_segment_summary - read segment summary of the specified partial segment
- * @sbi: nilfs_sb_info
- * @pseg_start: start disk block number of partial segment
- * @seg_seq: sequence number requested
- * @ssi: pointer to nilfs_segsum_info struct to store information
+ * nilfs_read_log_header - read summary header of the specified log
+ * @nilfs: nilfs object
+ * @start_blocknr: start block number of the log
+ * @sum: pointer to return segment summary structure
  */
-static int
-load_segment_summary(struct nilfs_sb_info *sbi, sector_t pseg_start,
-                    u64 seg_seq, struct nilfs_segsum_info *ssi)
+static struct buffer_head *
+nilfs_read_log_header(struct the_nilfs *nilfs, sector_t start_blocknr,
+                     struct nilfs_segment_summary **sum)
 {
        struct buffer_head *bh_sum;
-       struct nilfs_segment_summary *sum;
+
+       bh_sum = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
+       if (bh_sum)
+               *sum = (struct nilfs_segment_summary *)bh_sum->b_data;
+       return bh_sum;
+}
+
+/**
+ * nilfs_validate_log - verify consistency of log
+ * @nilfs: nilfs object
+ * @seg_seq: sequence number of segment
+ * @bh_sum: buffer head of summary block
+ * @sum: segment summary struct
+ */
+static int nilfs_validate_log(struct the_nilfs *nilfs, u64 seg_seq,
+                             struct buffer_head *bh_sum,
+                             struct nilfs_segment_summary *sum)
+{
        unsigned long nblock;
        u32 crc;
-       int ret = NILFS_SEG_FAIL_IO;
+       int ret;
 
-       bh_sum = sb_bread(sbi->s_super, pseg_start);
-       if (!bh_sum)
+       ret = NILFS_SEG_FAIL_MAGIC;
+       if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC)
                goto out;
 
-       sum = (struct nilfs_segment_summary *)bh_sum->b_data;
-
-       /* Check consistency of segment summary */
-       if (le32_to_cpu(sum->ss_magic) != NILFS_SEGSUM_MAGIC) {
-               ret = NILFS_SEG_FAIL_MAGIC;
-               goto failed;
-       }
-       store_segsum_info(ssi, sum, sbi->s_super->s_blocksize);
-       if (seg_seq != ssi->seg_seq) {
-               ret = NILFS_SEG_FAIL_SEQ;
-               goto failed;
-       }
+       ret = NILFS_SEG_FAIL_SEQ;
+       if (le64_to_cpu(sum->ss_seq) != seg_seq)
+               goto out;
 
-       nblock = ssi->nblocks;
-       if (unlikely(nblock == 0 ||
-                    nblock > sbi->s_nilfs->ns_blocks_per_segment)) {
+       nblock = le32_to_cpu(sum->ss_nblocks);
+       ret = NILFS_SEG_FAIL_CONSISTENCY;
+       if (unlikely(nblock == 0 || nblock > nilfs->ns_blocks_per_segment))
                /* This limits the number of blocks read in the CRC check */
-               ret = NILFS_SEG_FAIL_CONSISTENCY;
-               goto failed;
-       }
-       if (calc_crc_cont(sbi, bh_sum, &crc, sizeof(sum->ss_datasum),
-                         ((u64)nblock << sbi->s_super->s_blocksize_bits),
-                         pseg_start, nblock)) {
-               ret = NILFS_SEG_FAIL_IO;
-               goto failed;
-       }
-       if (crc == le32_to_cpu(sum->ss_datasum))
-               ret = 0;
-       else
-               ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
- failed:
-       brelse(bh_sum);
- out:
+               goto out;
+
+       ret = NILFS_SEG_FAIL_IO;
+       if (nilfs_compute_checksum(nilfs, bh_sum, &crc, sizeof(sum->ss_datasum),
+                                  ((u64)nblock << nilfs->ns_blocksize_bits),
+                                  bh_sum->b_blocknr, nblock))
+               goto out;
+
+       ret = NILFS_SEG_FAIL_CHECKSUM_FULL;
+       if (crc != le32_to_cpu(sum->ss_datasum))
+               goto out;
+       ret = 0;
+out:
        return ret;
 }
 
-static void *segsum_get(struct super_block *sb, struct buffer_head **pbh,
-                       unsigned int *offset, unsigned int bytes)
+/**
+ * nilfs_read_summary_info - read an item on summary blocks of a log
+ * @nilfs: nilfs object
+ * @pbh: the current buffer head on summary blocks [in, out]
+ * @offset: the current byte offset on summary blocks [in, out]
+ * @bytes: byte size of the item to be read
+ */
+static void *nilfs_read_summary_info(struct the_nilfs *nilfs,
+                                    struct buffer_head **pbh,
+                                    unsigned int *offset, unsigned int bytes)
 {
        void *ptr;
        sector_t blocknr;
@@ -262,7 +262,8 @@ static void *segsum_get(struct super_block *sb, struct buffer_head **pbh,
        if (bytes > (*pbh)->b_size - *offset) {
                blocknr = (*pbh)->b_blocknr;
                brelse(*pbh);
-               *pbh = sb_bread(sb, blocknr + 1);
+               *pbh = __bread(nilfs->ns_bdev, blocknr + 1,
+                              nilfs->ns_blocksize);
                if (unlikely(!*pbh))
                        return NULL;
                *offset = 0;
@@ -272,9 +273,18 @@ static void *segsum_get(struct super_block *sb, struct buffer_head **pbh,
        return ptr;
 }
 
-static void segsum_skip(struct super_block *sb, struct buffer_head **pbh,
-                       unsigned int *offset, unsigned int bytes,
-                       unsigned long count)
+/**
+ * nilfs_skip_summary_info - skip items on summary blocks of a log
+ * @nilfs: nilfs object
+ * @pbh: the current buffer head on summary blocks [in, out]
+ * @offset: the current byte offset on summary blocks [in, out]
+ * @bytes: byte size of the item to be skipped
+ * @count: number of items to be skipped
+ */
+static void nilfs_skip_summary_info(struct the_nilfs *nilfs,
+                                   struct buffer_head **pbh,
+                                   unsigned int *offset, unsigned int bytes,
+                                   unsigned long count)
 {
        unsigned int rest_item_in_current_block
                = ((*pbh)->b_size - *offset) / bytes;
@@ -291,36 +301,46 @@ static void segsum_skip(struct super_block *sb, struct buffer_head **pbh,
                *offset = bytes * (count - (bcnt - 1) * nitem_per_block);
 
                brelse(*pbh);
-               *pbh = sb_bread(sb, blocknr + bcnt);
+               *pbh = __bread(nilfs->ns_bdev, blocknr + bcnt,
+                              nilfs->ns_blocksize);
        }
 }
 
-static int
-collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
-                          struct nilfs_segsum_info *ssi,
-                          struct list_head *head)
+/**
+ * nilfs_scan_dsync_log - get block information of a log written for data sync
+ * @nilfs: nilfs object
+ * @start_blocknr: start block number of the log
+ * @sum: log summary information
+ * @head: list head to add nilfs_recovery_block struct
+ */
+static int nilfs_scan_dsync_log(struct the_nilfs *nilfs, sector_t start_blocknr,
+                               struct nilfs_segment_summary *sum,
+                               struct list_head *head)
 {
        struct buffer_head *bh;
        unsigned int offset;
-       unsigned long nfinfo = ssi->nfinfo;
-       sector_t blocknr = sum_blocknr + ssi->nsumblk;
+       u32 nfinfo, sumbytes;
+       sector_t blocknr;
        ino_t ino;
        int err = -EIO;
 
+       nfinfo = le32_to_cpu(sum->ss_nfinfo);
        if (!nfinfo)
                return 0;
 
-       bh = sb_bread(sbi->s_super, sum_blocknr);
+       sumbytes = le32_to_cpu(sum->ss_sumbytes);
+       blocknr = start_blocknr + DIV_ROUND_UP(sumbytes, nilfs->ns_blocksize);
+       bh = __bread(nilfs->ns_bdev, start_blocknr, nilfs->ns_blocksize);
        if (unlikely(!bh))
                goto out;
 
-       offset = le16_to_cpu(
-               ((struct nilfs_segment_summary *)bh->b_data)->ss_bytes);
+       offset = le16_to_cpu(sum->ss_bytes);
        for (;;) {
                unsigned long nblocks, ndatablk, nnodeblk;
                struct nilfs_finfo *finfo;
 
-               finfo = segsum_get(sbi->s_super, &bh, &offset, sizeof(*finfo));
+               finfo = nilfs_read_summary_info(nilfs, &bh, &offset,
+                                               sizeof(*finfo));
                if (unlikely(!finfo))
                        goto out;
 
@@ -333,8 +353,8 @@ collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
                        struct nilfs_recovery_block *rb;
                        struct nilfs_binfo_v *binfo;
 
-                       binfo = segsum_get(sbi->s_super, &bh, &offset,
-                                          sizeof(*binfo));
+                       binfo = nilfs_read_summary_info(nilfs, &bh, &offset,
+                                                       sizeof(*binfo));
                        if (unlikely(!binfo))
                                goto out;
 
@@ -352,9 +372,9 @@ collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
                }
                if (--nfinfo == 0)
                        break;
-               blocknr += nnodeblk; /* always 0 for the data sync segments */
-               segsum_skip(sbi->s_super, &bh, &offset, sizeof(__le64),
-                           nnodeblk);
+               blocknr += nnodeblk; /* always 0 for data sync logs */
+               nilfs_skip_summary_info(nilfs, &bh, &offset, sizeof(__le64),
+                                       nnodeblk);
                if (unlikely(!bh))
                        goto out;
        }
@@ -367,9 +387,9 @@ collect_blocks_from_segsum(struct nilfs_sb_info *sbi, sector_t sum_blocknr,
 static void dispose_recovery_list(struct list_head *head)
 {
        while (!list_empty(head)) {
-               struct nilfs_recovery_block *rb
-                       = list_entry(head->next,
-                                    struct nilfs_recovery_block, list);
+               struct nilfs_recovery_block *rb;
+
+               rb = list_first_entry(head, struct nilfs_recovery_block, list);
                list_del(&rb->list);
                kfree(rb);
        }
@@ -396,16 +416,16 @@ static int nilfs_segment_list_add(struct list_head *head, __u64 segnum)
 void nilfs_dispose_segment_list(struct list_head *head)
 {
        while (!list_empty(head)) {
-               struct nilfs_segment_entry *ent
-                       = list_entry(head->next,
-                                    struct nilfs_segment_entry, list);
+               struct nilfs_segment_entry *ent;
+
+               ent = list_first_entry(head, struct nilfs_segment_entry, list);
                list_del(&ent->list);
                kfree(ent);
        }
 }
 
 static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
-                                             struct nilfs_sb_info *sbi,
+                                             struct super_block *sb,
                                              struct nilfs_recovery_info *ri)
 {
        struct list_head *head = &ri->ri_used_segments;
@@ -420,7 +440,6 @@ static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
        segnum[2] = ri->ri_segnum;
        segnum[3] = ri->ri_nextnum;
 
-       nilfs_attach_writer(nilfs, sbi);
        /*
         * Releasing the next segment of the latest super root.
         * The next segment is invalidated by this recovery.
@@ -460,41 +479,42 @@ static int nilfs_prepare_segment_for_recovery(struct the_nilfs *nilfs,
 
  failed:
        /* No need to recover sufile because it will be destroyed on error */
-       nilfs_detach_writer(nilfs, sbi);
        return err;
 }
 
-static int nilfs_recovery_copy_block(struct nilfs_sb_info *sbi,
+static int nilfs_recovery_copy_block(struct the_nilfs *nilfs,
                                     struct nilfs_recovery_block *rb,
                                     struct page *page)
 {
        struct buffer_head *bh_org;
        void *kaddr;
 
-       bh_org = sb_bread(sbi->s_super, rb->blocknr);
+       bh_org = __bread(nilfs->ns_bdev, rb->blocknr, nilfs->ns_blocksize);
        if (unlikely(!bh_org))
                return -EIO;
 
-       kaddr = kmap_atomic(page, KM_USER0);
+       kaddr = kmap_atomic(page);
        memcpy(kaddr + bh_offset(bh_org), bh_org->b_data, bh_org->b_size);
-       kunmap_atomic(kaddr, KM_USER0);
+       kunmap_atomic(kaddr);
        brelse(bh_org);
        return 0;
 }
 
-static int recover_dsync_blocks(struct nilfs_sb_info *sbi,
-                               struct list_head *head,
-                               unsigned long *nr_salvaged_blocks)
+static int nilfs_recover_dsync_blocks(struct the_nilfs *nilfs,
+                                     struct super_block *sb,
+                                     struct nilfs_root *root,
+                                     struct list_head *head,
+                                     unsigned long *nr_salvaged_blocks)
 {
        struct inode *inode;
        struct nilfs_recovery_block *rb, *n;
-       unsigned blocksize = sbi->s_super->s_blocksize;
+       unsigned blocksize = nilfs->ns_blocksize;
        struct page *page;
        loff_t pos;
        int err = 0, err2 = 0;
 
        list_for_each_entry_safe(rb, n, head, list) {
-               inode = nilfs_iget(sbi->s_super, rb->ino);
+               inode = nilfs_iget(sb, root, rb->ino);
                if (IS_ERR(inode)) {
                        err = PTR_ERR(inode);
                        inode = NULL;
@@ -502,17 +522,21 @@ static int recover_dsync_blocks(struct nilfs_sb_info *sbi,
                }
 
                pos = rb->blkoff << inode->i_blkbits;
-               page = NULL;
-               err = block_write_begin(NULL, inode->i_mapping, pos, blocksize,
-                                       0, &page, NULL, nilfs_get_block);
-               if (unlikely(err))
+               err = block_write_begin(inode->i_mapping, pos, blocksize,
+                                       0, &page, nilfs_get_block);
+               if (unlikely(err)) {
+                       loff_t isize = inode->i_size;
+                       if (pos + blocksize > isize)
+                               nilfs_write_failed(inode->i_mapping,
+                                                       pos + blocksize);
                        goto failed_inode;
+               }
 
-               err = nilfs_recovery_copy_block(sbi, rb, page);
+               err = nilfs_recovery_copy_block(nilfs, rb, page);
                if (unlikely(err))
                        goto failed_page;
 
-               err = nilfs_set_file_dirty(sbi, inode, 1);
+               err = nilfs_set_file_dirty(inode, 1);
                if (unlikely(err))
                        goto failed_page;
 
@@ -548,18 +572,21 @@ static int recover_dsync_blocks(struct nilfs_sb_info *sbi,
 /**
  * nilfs_do_roll_forward - salvage logical segments newer than the latest
  * checkpoint
- * @sbi: nilfs_sb_info
- * @nilfs: the_nilfs
+ * @nilfs: nilfs object
+ * @sb: super block instance
  * @ri: pointer to a nilfs_recovery_info
  */
 static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
-                                struct nilfs_sb_info *sbi,
+                                struct super_block *sb,
+                                struct nilfs_root *root,
                                 struct nilfs_recovery_info *ri)
 {
-       struct nilfs_segsum_info ssi;
+       struct buffer_head *bh_sum = NULL;
+       struct nilfs_segment_summary *sum;
        sector_t pseg_start;
        sector_t seg_start, seg_end;  /* Starting/ending DBN of full segment */
        unsigned long nsalvaged_blocks = 0;
+       unsigned int flags;
        u64 seg_seq;
        __u64 segnum, nextnum = 0;
        int empty_seg = 0;
@@ -571,15 +598,20 @@ static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
        };
        int state = RF_INIT_ST;
 
-       nilfs_attach_writer(nilfs, sbi);
        pseg_start = ri->ri_lsegs_start;
        seg_seq = ri->ri_lsegs_start_seq;
        segnum = nilfs_get_segnum_of_block(nilfs, pseg_start);
        nilfs_get_segment_range(nilfs, segnum, &seg_start, &seg_end);
 
        while (segnum != ri->ri_segnum || pseg_start <= ri->ri_pseg_start) {
+               brelse(bh_sum);
+               bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
+               if (!bh_sum) {
+                       err = -EIO;
+                       goto failed;
+               }
 
-               ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi);
+               ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
                if (ret) {
                        if (ret == NILFS_SEG_FAIL_IO) {
                                err = -EIO;
@@ -587,33 +619,38 @@ static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
                        }
                        goto strayed;
                }
-               if (unlikely(NILFS_SEG_HAS_SR(&ssi)))
+
+               flags = le16_to_cpu(sum->ss_flags);
+               if (flags & NILFS_SS_SR)
                        goto confused;
 
                /* Found a valid partial segment; do recovery actions */
-               nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
+               nextnum = nilfs_get_segnum_of_block(nilfs,
+                                                   le64_to_cpu(sum->ss_next));
                empty_seg = 0;
-               nilfs->ns_ctime = ssi.ctime;
-               if (!(ssi.flags & NILFS_SS_GC))
-                       nilfs->ns_nongc_ctime = ssi.ctime;
+               nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
+               if (!(flags & NILFS_SS_GC))
+                       nilfs->ns_nongc_ctime = nilfs->ns_ctime;
 
                switch (state) {
                case RF_INIT_ST:
-                       if (!NILFS_SEG_LOGBGN(&ssi) || !NILFS_SEG_DSYNC(&ssi))
+                       if (!(flags & NILFS_SS_LOGBGN) ||
+                           !(flags & NILFS_SS_SYNDT))
                                goto try_next_pseg;
                        state = RF_DSYNC_ST;
                        /* Fall through */
                case RF_DSYNC_ST:
-                       if (!NILFS_SEG_DSYNC(&ssi))
+                       if (!(flags & NILFS_SS_SYNDT))
                                goto confused;
 
-                       err = collect_blocks_from_segsum(
-                               sbi, pseg_start, &ssi, &dsync_blocks);
+                       err = nilfs_scan_dsync_log(nilfs, pseg_start, sum,
+                                                  &dsync_blocks);
                        if (unlikely(err))
                                goto failed;
-                       if (NILFS_SEG_LOGEND(&ssi)) {
-                               err = recover_dsync_blocks(
-                                       sbi, &dsync_blocks, &nsalvaged_blocks);
+                       if (flags & NILFS_SS_LOGEND) {
+                               err = nilfs_recover_dsync_blocks(
+                                       nilfs, sb, root, &dsync_blocks,
+                                       &nsalvaged_blocks);
                                if (unlikely(err))
                                        goto failed;
                                state = RF_INIT_ST;
@@ -624,7 +661,7 @@ static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
  try_next_pseg:
                if (pseg_start == ri->ri_lsegs_end)
                        break;
-               pseg_start += ssi.nblocks;
+               pseg_start += le32_to_cpu(sum->ss_nblocks);
                if (pseg_start < seg_end)
                        continue;
                goto feed_segment;
@@ -645,12 +682,12 @@ static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
 
        if (nsalvaged_blocks) {
                printk(KERN_INFO "NILFS (device %s): salvaged %lu blocks\n",
-                      sbi->s_super->s_id, nsalvaged_blocks);
+                      sb->s_id, nsalvaged_blocks);
                ri->ri_need_recovery = NILFS_RECOVERY_ROLLFORWARD_DONE;
        }
  out:
+       brelse(bh_sum);
        dispose_recovery_list(&dsync_blocks);
-       nilfs_detach_writer(sbi->s_nilfs, sbi);
        return err;
 
  confused:
@@ -659,12 +696,11 @@ static int nilfs_do_roll_forward(struct the_nilfs *nilfs,
        printk(KERN_ERR
               "NILFS (device %s): Error roll-forwarding "
               "(err=%d, pseg block=%llu). ",
-              sbi->s_super->s_id, err, (unsigned long long)pseg_start);
+              sb->s_id, err, (unsigned long long)pseg_start);
        goto out;
 }
 
 static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
-                                     struct nilfs_sb_info *sbi,
                                      struct nilfs_recovery_info *ri)
 {
        struct buffer_head *bh;
@@ -674,7 +710,7 @@ static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
            nilfs_get_segnum_of_block(nilfs, ri->ri_super_root))
                return;
 
-       bh = sb_getblk(sbi->s_super, ri->ri_lsegs_start);
+       bh = __getblk(nilfs->ns_bdev, ri->ri_lsegs_start, nilfs->ns_blocksize);
        BUG_ON(!bh);
        memset(bh->b_data, 0, bh->b_size);
        set_buffer_dirty(bh);
@@ -687,10 +723,9 @@ static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
 }
 
 /**
- * nilfs_recover_logical_segments - salvage logical segments written after
- * the latest super root
- * @nilfs: the_nilfs
- * @sbi: nilfs_sb_info
+ * nilfs_salvage_orphan_logs - salvage logs written after the latest checkpoint
+ * @nilfs: nilfs object
+ * @sb: super block instance
  * @ri: pointer to a nilfs_recovery_info struct to store search results.
  *
  * Return Value: On success, 0 is returned.  On error, one of the following
@@ -706,41 +741,42 @@ static void nilfs_finish_roll_forward(struct the_nilfs *nilfs,
  *
  * %-ENOMEM - Insufficient memory available.
  */
-int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
-                                  struct nilfs_sb_info *sbi,
-                                  struct nilfs_recovery_info *ri)
+int nilfs_salvage_orphan_logs(struct the_nilfs *nilfs,
+                             struct super_block *sb,
+                             struct nilfs_recovery_info *ri)
 {
+       struct nilfs_root *root;
        int err;
 
        if (ri->ri_lsegs_start == 0 || ri->ri_lsegs_end == 0)
                return 0;
 
-       err = nilfs_attach_checkpoint(sbi, ri->ri_cno);
+       err = nilfs_attach_checkpoint(sb, ri->ri_cno, true, &root);
        if (unlikely(err)) {
                printk(KERN_ERR
                       "NILFS: error loading the latest checkpoint.\n");
                return err;
        }
 
-       err = nilfs_do_roll_forward(nilfs, sbi, ri);
+       err = nilfs_do_roll_forward(nilfs, sb, root, ri);
        if (unlikely(err))
                goto failed;
 
        if (ri->ri_need_recovery == NILFS_RECOVERY_ROLLFORWARD_DONE) {
-               err = nilfs_prepare_segment_for_recovery(nilfs, sbi, ri);
+               err = nilfs_prepare_segment_for_recovery(nilfs, sb, ri);
                if (unlikely(err)) {
                        printk(KERN_ERR "NILFS: Error preparing segments for "
                               "recovery.\n");
                        goto failed;
                }
 
-               err = nilfs_attach_segment_constructor(sbi);
+               err = nilfs_attach_log_writer(sb, root);
                if (unlikely(err))
                        goto failed;
 
                set_nilfs_discontinued(nilfs);
-               err = nilfs_construct_segment(sbi->s_super);
-               nilfs_detach_segment_constructor(sbi);
+               err = nilfs_construct_segment(sb);
+               nilfs_detach_log_writer(sb);
 
                if (unlikely(err)) {
                        printk(KERN_ERR "NILFS: Oops! recovery failed. "
@@ -748,18 +784,17 @@ int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
                        goto failed;
                }
 
-               nilfs_finish_roll_forward(nilfs, sbi, ri);
+               nilfs_finish_roll_forward(nilfs, ri);
        }
 
  failed:
-       nilfs_detach_checkpoint(sbi);
+       nilfs_put_root(root);
        return err;
 }
 
 /**
  * nilfs_search_super_root - search the latest valid super root
  * @nilfs: the_nilfs
- * @sbi: nilfs_sb_info
  * @ri: pointer to a nilfs_recovery_info struct to store search results.
  *
  * nilfs_search_super_root() looks for the latest super-root from a partial
@@ -772,14 +807,19 @@ int nilfs_recover_logical_segments(struct the_nilfs *nilfs,
  * %-EINVAL - No valid segment found
  *
  * %-EIO - I/O error
+ *
+ * %-ENOMEM - Insufficient memory available.
  */
-int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
+int nilfs_search_super_root(struct the_nilfs *nilfs,
                            struct nilfs_recovery_info *ri)
 {
-       struct nilfs_segsum_info ssi;
+       struct buffer_head *bh_sum = NULL;
+       struct nilfs_segment_summary *sum;
        sector_t pseg_start, pseg_end, sr_pseg_start = 0;
        sector_t seg_start, seg_end; /* range of full segment (block number) */
        sector_t b, end;
+       unsigned long nblocks;
+       unsigned int flags;
        u64 seg_seq;
        __u64 segnum, nextnum = 0;
        __u64 cno;
@@ -798,17 +838,24 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
        /* Read ahead segment */
        b = seg_start;
        while (b <= seg_end)
-               sb_breadahead(sbi->s_super, b++);
+               __breadahead(nilfs->ns_bdev, b++, nilfs->ns_blocksize);
 
        for (;;) {
-               /* Load segment summary */
-               ret = load_segment_summary(sbi, pseg_start, seg_seq, &ssi);
+               brelse(bh_sum);
+               ret = NILFS_SEG_FAIL_IO;
+               bh_sum = nilfs_read_log_header(nilfs, pseg_start, &sum);
+               if (!bh_sum)
+                       goto failed;
+
+               ret = nilfs_validate_log(nilfs, seg_seq, bh_sum, sum);
                if (ret) {
                        if (ret == NILFS_SEG_FAIL_IO)
                                goto failed;
                        goto strayed;
                }
-               pseg_end = pseg_start + ssi.nblocks - 1;
+
+               nblocks = le32_to_cpu(sum->ss_nblocks);
+               pseg_end = pseg_start + nblocks - 1;
                if (unlikely(pseg_end > seg_end)) {
                        ret = NILFS_SEG_FAIL_CONSISTENCY;
                        goto strayed;
@@ -818,11 +865,13 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
                ri->ri_pseg_start = pseg_start;
                ri->ri_seq = seg_seq;
                ri->ri_segnum = segnum;
-               nextnum = nilfs_get_segnum_of_block(nilfs, ssi.next);
+               nextnum = nilfs_get_segnum_of_block(nilfs,
+                                                   le64_to_cpu(sum->ss_next));
                ri->ri_nextnum = nextnum;
                empty_seg = 0;
 
-               if (!NILFS_SEG_HAS_SR(&ssi) && !scan_newer) {
+               flags = le16_to_cpu(sum->ss_flags);
+               if (!(flags & NILFS_SS_SR) && !scan_newer) {
                        /* This will never happen because a superblock
                           (last_segment) always points to a pseg
                           having a super root. */
@@ -833,14 +882,15 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
                if (pseg_start == seg_start) {
                        nilfs_get_segment_range(nilfs, nextnum, &b, &end);
                        while (b <= end)
-                               sb_breadahead(sbi->s_super, b++);
+                               __breadahead(nilfs->ns_bdev, b++,
+                                            nilfs->ns_blocksize);
                }
-               if (!NILFS_SEG_HAS_SR(&ssi)) {
-                       if (!ri->ri_lsegs_start && NILFS_SEG_LOGBGN(&ssi)) {
+               if (!(flags & NILFS_SS_SR)) {
+                       if (!ri->ri_lsegs_start && (flags & NILFS_SS_LOGBGN)) {
                                ri->ri_lsegs_start = pseg_start;
                                ri->ri_lsegs_start_seq = seg_seq;
                        }
-                       if (NILFS_SEG_LOGEND(&ssi))
+                       if (flags & NILFS_SS_LOGEND)
                                ri->ri_lsegs_end = pseg_start;
                        goto try_next_pseg;
                }
@@ -851,12 +901,12 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
                ri->ri_lsegs_start = ri->ri_lsegs_end = 0;
 
                nilfs_dispose_segment_list(&segments);
-               nilfs->ns_pseg_offset = (sr_pseg_start = pseg_start)
-                       + ssi.nblocks - seg_start;
+               sr_pseg_start = pseg_start;
+               nilfs->ns_pseg_offset = pseg_start + nblocks - seg_start;
                nilfs->ns_seg_seq = seg_seq;
                nilfs->ns_segnum = segnum;
                nilfs->ns_cno = cno;  /* nilfs->ns_cno = ri->ri_cno + 1 */
-               nilfs->ns_ctime = ssi.ctime;
+               nilfs->ns_ctime = le64_to_cpu(sum->ss_create);
                nilfs->ns_nextnum = nextnum;
 
                if (scan_newer)
@@ -867,15 +917,9 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
                        scan_newer = 1;
                }
 
-               /* reset region for roll-forward */
-               pseg_start += ssi.nblocks;
-               if (pseg_start < seg_end)
-                       continue;
-               goto feed_segment;
-
  try_next_pseg:
                /* Standing on a course, or met an inconsistent state */
-               pseg_start += ssi.nblocks;
+               pseg_start += nblocks;
                if (pseg_start < seg_end)
                        continue;
                goto feed_segment;
@@ -906,6 +950,7 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
 
  super_root_found:
        /* Updating pointers relating to the latest checkpoint */
+       brelse(bh_sum);
        list_splice_tail(&segments, &ri->ri_used_segments);
        nilfs->ns_last_pseg = sr_pseg_start;
        nilfs->ns_last_seq = nilfs->ns_seg_seq;
@@ -913,6 +958,7 @@ int nilfs_search_super_root(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi,
        return 0;
 
  failed:
+       brelse(bh_sum);
        nilfs_dispose_segment_list(&segments);
        return (ret < 0) ? ret : nilfs_warn_segment_error(ret);
 }