include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / fs / nilfs2 / segment.c
index 2c4c088..6a7dbd8 100644 (file)
@@ -32,6 +32,7 @@
 #include <linux/kthread.h>
 #include <linux/crc32.h>
 #include <linux/pagevec.h>
+#include <linux/slab.h>
 #include "nilfs.h"
 #include "btnode.h"
 #include "page.h"
@@ -39,7 +40,6 @@
 #include "sufile.h"
 #include "cpfile.h"
 #include "ifile.h"
-#include "seglist.h"
 #include "segbuf.h"
 
 
@@ -67,7 +67,6 @@ enum {
        NILFS_ST_INIT = 0,
        NILFS_ST_GC,            /* Collecting dirty blocks for GC */
        NILFS_ST_FILE,
-       NILFS_ST_SKETCH,
        NILFS_ST_IFILE,
        NILFS_ST_CPFILE,
        NILFS_ST_SUFILE,
@@ -80,7 +79,8 @@ enum {
 /* State flags of collection */
 #define NILFS_CF_NODE          0x0001  /* Collecting node blocks */
 #define NILFS_CF_IFILE_STARTED 0x0002  /* IFILE stage has started */
-#define NILFS_CF_HISTORY_MASK  (NILFS_CF_IFILE_STARTED)
+#define NILFS_CF_SUFREED       0x0004  /* segment usages has been freed */
+#define NILFS_CF_HISTORY_MASK  (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
 
 /* Operations depending on the construction mode and file type */
 struct nilfs_sc_operations {
@@ -142,7 +142,7 @@ int nilfs_init_transaction_cache(void)
 }
 
 /**
- * nilfs_detroy_transaction_cache - destroy the cache for transaction info
+ * nilfs_destroy_transaction_cache - destroy the cache for transaction info
  *
  * nilfs_destroy_transaction_cache() frees the slab cache for the struct
  * nilfs_transaction_info.
@@ -163,8 +163,8 @@ static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
                else {
                        /*
                         * If journal_info field is occupied by other FS,
-                        * we save it and restore on nilfs_transaction_end().
-                        * But this should never happen.
+                        * it is saved and will be restored on
+                        * nilfs_transaction_commit().
                         */
                        printk(KERN_WARNING
                               "NILFS warning: journal info from a different "
@@ -195,14 +195,14 @@ static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
  *
  * nilfs_transaction_begin() acquires a reader/writer semaphore, called
  * the segment semaphore, to make a segment construction and write tasks
- * exclusive.  The function is used with nilfs_transaction_end() in pairs.
+ * exclusive.  The function is used with nilfs_transaction_commit() in pairs.
  * The region enclosed by these two functions can be nested.  To avoid a
  * deadlock, the semaphore is only acquired or released in the outermost call.
  *
  * This function allocates a nilfs_transaction_info struct to keep context
  * information on it.  It is initialized and hooked onto the current task in
  * the outermost call.  If a pre-allocated struct is given to @ti, it is used
- * instead; othewise a new struct is assigned from a slab.
+ * instead; otherwise a new struct is assigned from a slab.
  *
  * When @vacancy_check flag is set, this function will check the amount of
  * free space, and will wait for the GC to reclaim disk space if low capacity.
@@ -212,8 +212,6 @@ static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
  *
  * %-ENOMEM - Insufficient memory available.
  *
- * %-ERESTARTSYS - Interrupted
- *
  * %-ENOSPC - No space left on device
  */
 int nilfs_transaction_begin(struct super_block *sb,
@@ -248,16 +246,17 @@ int nilfs_transaction_begin(struct super_block *sb,
 }
 
 /**
- * nilfs_transaction_end - end indivisible file operations.
+ * nilfs_transaction_commit - commit indivisible file operations.
  * @sb: super block
- * @commit: commit flag (0 for no change)
  *
- * nilfs_transaction_end() releases the read semaphore which is
- * acquired by nilfs_transaction_begin(). Its releasing is only done
- * in outermost call of this function. If the nilfs_transaction_info
- * was allocated dynamically, it is given back to a slab cache.
+ * nilfs_transaction_commit() releases the read semaphore which is
+ * acquired by nilfs_transaction_begin(). This is only performed
+ * in outermost call of this function.  If a commit flag is set,
+ * nilfs_transaction_commit() sets a timer to start the segment
+ * constructor.  If a sync flag is set, it starts construction
+ * directly.
  */
-int nilfs_transaction_end(struct super_block *sb, int commit)
+int nilfs_transaction_commit(struct super_block *sb)
 {
        struct nilfs_transaction_info *ti = current->journal_info;
        struct nilfs_sb_info *sbi;
@@ -265,9 +264,7 @@ int nilfs_transaction_end(struct super_block *sb, int commit)
        int err = 0;
 
        BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
-
-       if (commit)
-               ti->ti_flags |= NILFS_TI_COMMIT;
+       ti->ti_flags |= NILFS_TI_COMMIT;
        if (ti->ti_count > 0) {
                ti->ti_count--;
                return 0;
@@ -291,6 +288,22 @@ int nilfs_transaction_end(struct super_block *sb, int commit)
        return err;
 }
 
+void nilfs_transaction_abort(struct super_block *sb)
+{
+       struct nilfs_transaction_info *ti = current->journal_info;
+
+       BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
+       if (ti->ti_count > 0) {
+               ti->ti_count--;
+               return;
+       }
+       up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
+
+       current->journal_info = ti->ti_save;
+       if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
+               kmem_cache_free(nilfs_transaction_cachep, ti);
+}
+
 void nilfs_relax_pressure_in_lock(struct super_block *sb)
 {
        struct nilfs_sb_info *sbi = NILFS_SB(sb);
@@ -321,8 +334,7 @@ static void nilfs_transaction_lock(struct nilfs_sb_info *sbi,
 {
        struct nilfs_transaction_info *cur_ti = current->journal_info;
 
-       BUG_ON(cur_ti);
-       BUG_ON(!ti);
+       WARN_ON(cur_ti);
        ti->ti_flags = NILFS_TI_WRITER;
        ti->ti_count = 0;
        ti->ti_save = cur_ti;
@@ -451,6 +463,9 @@ static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
        sci->sc_binfo_ptr = sci->sc_finfo_ptr;
        nilfs_segctor_map_segsum_entry(
                sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
+
+       if (inode->i_sb && !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
+               set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
        /* skip finfo */
 }
 
@@ -533,8 +548,6 @@ static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
 {
        int err;
 
-       /* BUG_ON(!buffer_dirty(bh)); */
-       /* excluded by scan_dirty_data_buffers() */
        err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
        if (unlikely(err < 0))
                return nilfs_handle_bmap_error(err, __func__, inode,
@@ -553,8 +566,6 @@ static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
 {
        int err;
 
-       /* BUG_ON(!buffer_dirty(bh)); */
-       /* excluded by scan_dirty_node_buffers() */
        err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
        if (unlikely(err < 0))
                return nilfs_handle_bmap_error(err, __func__, inode,
@@ -566,7 +577,7 @@ static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
                                   struct buffer_head *bh,
                                   struct inode *inode)
 {
-       BUG_ON(!buffer_dirty(bh));
+       WARN_ON(!buffer_dirty(bh));
        return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
 }
 
@@ -615,7 +626,7 @@ static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
 static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
                                  struct buffer_head *bh, struct inode *inode)
 {
-       BUG_ON(!buffer_dirty(bh));
+       WARN_ON(!buffer_dirty(bh));
        return nilfs_segctor_add_file_block(sci, bh, inode,
                                            sizeof(struct nilfs_binfo_dat));
 }
@@ -654,29 +665,41 @@ struct nilfs_sc_operations nilfs_sc_dsync_ops = {
        .write_node_binfo = NULL,
 };
 
-static int nilfs_lookup_dirty_data_buffers(struct inode *inode,
-                                          struct list_head *listp,
-                                          struct nilfs_sc_info *sci)
+static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
+                                             struct list_head *listp,
+                                             size_t nlimit,
+                                             loff_t start, loff_t end)
 {
-       struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
        struct address_space *mapping = inode->i_mapping;
        struct pagevec pvec;
-       unsigned i, ndirties = 0, nlimit;
-       pgoff_t index = 0;
-       int err = 0;
+       pgoff_t index = 0, last = ULONG_MAX;
+       size_t ndirties = 0;
+       int i;
 
-       nlimit = sci->sc_segbuf_nblocks -
-               (sci->sc_nblk_this_inc + segbuf->sb_sum.nblocks);
+       if (unlikely(start != 0 || end != LLONG_MAX)) {
+               /*
+                * A valid range is given for sync-ing data pages. The
+                * range is rounded to per-page; extra dirty buffers
+                * may be included if blocksize < pagesize.
+                */
+               index = start >> PAGE_SHIFT;
+               last = end >> PAGE_SHIFT;
+       }
        pagevec_init(&pvec, 0);
  repeat:
-       if (!pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
-                               PAGEVEC_SIZE))
-               return 0;
+       if (unlikely(index > last) ||
+           !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
+                               min_t(pgoff_t, last - index,
+                                     PAGEVEC_SIZE - 1) + 1))
+               return ndirties;
 
        for (i = 0; i < pagevec_count(&pvec); i++) {
                struct buffer_head *bh, *head;
                struct page *page = pvec.pages[i];
 
+               if (unlikely(page->index > last))
+                       break;
+
                if (mapping->host) {
                        lock_page(page);
                        if (!page_has_buffers(page))
@@ -687,24 +710,21 @@ static int nilfs_lookup_dirty_data_buffers(struct inode *inode,
 
                bh = head = page_buffers(page);
                do {
-                       if (buffer_dirty(bh)) {
-                               if (ndirties > nlimit) {
-                                       err = -E2BIG;
-                                       break;
-                               }
-                               get_bh(bh);
-                               list_add_tail(&bh->b_assoc_buffers, listp);
-                               ndirties++;
+                       if (!buffer_dirty(bh))
+                               continue;
+                       get_bh(bh);
+                       list_add_tail(&bh->b_assoc_buffers, listp);
+                       ndirties++;
+                       if (unlikely(ndirties >= nlimit)) {
+                               pagevec_release(&pvec);
+                               cond_resched();
+                               return ndirties;
                        }
-                       bh = bh->b_this_page;
-               } while (bh != head);
+               } while (bh = bh->b_this_page, bh != head);
        }
        pagevec_release(&pvec);
        cond_resched();
-
-       if (!err)
-               goto repeat;
-       return err;
+       goto repeat;
 }
 
 static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
@@ -791,7 +811,7 @@ static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
 {
        return list_empty(&sci->sc_dirty_files) &&
                !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
-               list_empty(&sci->sc_cleaning_segments) &&
+               sci->sc_nfreesegs == 0 &&
                (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
 }
 
@@ -840,9 +860,9 @@ static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
                nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
                nilfs_cpfile_put_checkpoint(
                        nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
-       } else {
-               BUG_ON(err == -EINVAL || err == -ENOENT);
-       }
+       } else
+               WARN_ON(err == -EINVAL || err == -ENOENT);
+
        return err;
 }
 
@@ -857,7 +877,7 @@ static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
        err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
                                          &raw_cp, &bh_cp);
        if (unlikely(err)) {
-               BUG_ON(err == -EINVAL || err == -ENOENT);
+               WARN_ON(err == -EINVAL || err == -ENOENT);
                goto failed_ibh;
        }
        raw_cp->cp_snapshot_list.ssl_next = 0;
@@ -870,8 +890,12 @@ static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
                cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
        raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
        raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
-       if (sci->sc_sketch_inode && i_size_read(sci->sc_sketch_inode) > 0)
-               nilfs_checkpoint_set_sketch(raw_cp);
+
+       if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
+               nilfs_checkpoint_clear_minor(raw_cp);
+       else
+               nilfs_checkpoint_set_minor(raw_cp);
+
        nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
        nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
        return 0;
@@ -906,11 +930,6 @@ static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
                nilfs_fill_in_file_bmap(ifile, ii);
                set_bit(NILFS_I_COLLECTED, &ii->i_state);
        }
-       if (sci->sc_sketch_inode) {
-               ii = NILFS_I(sci->sc_sketch_inode);
-               if (test_bit(NILFS_I_DIRTY, &ii->i_state))
-                       nilfs_fill_in_file_bmap(ifile, ii);
-       }
 }
 
 /*
@@ -922,7 +941,6 @@ static void nilfs_fill_in_super_root_crc(struct buffer_head *bh_sr, u32 seed)
                (struct nilfs_super_root *)bh_sr->b_data;
        u32 crc;
 
-       BUG_ON(NILFS_SR_BYTES > bh_sr->b_size);
        crc = crc32_le(seed,
                       (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
                       NILFS_SR_BYTES - sizeof(raw_sr->sr_sum));
@@ -957,12 +975,12 @@ static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
                              nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
        raw_sr->sr_flags = 0;
 
-       nilfs_mdt_write_inode_direct(
-               nilfs_dat_inode(nilfs), bh_sr, NILFS_SR_DAT_OFFSET(isz));
-       nilfs_mdt_write_inode_direct(
-               nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(isz));
-       nilfs_mdt_write_inode_direct(
-               nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(isz));
+       nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
+                                NILFS_SR_DAT_OFFSET(isz), 1);
+       nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
+                                NILFS_SR_CPFILE_OFFSET(isz), 1);
+       nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
+                                NILFS_SR_SUFILE_OFFSET(isz), 1);
 }
 
 static void nilfs_redirty_inodes(struct list_head *head)
@@ -988,45 +1006,6 @@ static void nilfs_drop_collected_inodes(struct list_head *head)
        }
 }
 
-static void nilfs_segctor_cancel_free_segments(struct nilfs_sc_info *sci,
-                                              struct inode *sufile)
-
-{
-       struct list_head *head = &sci->sc_cleaning_segments;
-       struct nilfs_segment_entry *ent;
-       int err;
-
-       list_for_each_entry(ent, head, list) {
-               if (!(ent->flags & NILFS_SLH_FREED))
-                       break;
-               err = nilfs_sufile_cancel_free(sufile, ent->segnum);
-               BUG_ON(err);
-
-               ent->flags &= ~NILFS_SLH_FREED;
-       }
-}
-
-static int nilfs_segctor_prepare_free_segments(struct nilfs_sc_info *sci,
-                                              struct inode *sufile)
-{
-       struct list_head *head = &sci->sc_cleaning_segments;
-       struct nilfs_segment_entry *ent;
-       int err;
-
-       list_for_each_entry(ent, head, list) {
-               err = nilfs_sufile_free(sufile, ent->segnum);
-               if (unlikely(err))
-                       return err;
-               ent->flags |= NILFS_SLH_FREED;
-       }
-       return 0;
-}
-
-static void nilfs_segctor_commit_free_segments(struct nilfs_sc_info *sci)
-{
-       nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
-}
-
 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
                                       struct inode *inode,
                                       struct list_head *listp,
@@ -1058,23 +1037,31 @@ static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
        return err;
 }
 
+static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
+{
+       /* Remaining number of blocks within segment buffer */
+       return sci->sc_segbuf_nblocks -
+               (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
+}
+
 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
                                   struct inode *inode,
                                   struct nilfs_sc_operations *sc_ops)
 {
        LIST_HEAD(data_buffers);
        LIST_HEAD(node_buffers);
-       int err, err2;
+       int err;
 
        if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
-               err = nilfs_lookup_dirty_data_buffers(inode, &data_buffers,
-                                                     sci);
-               if (err) {
-                       err2 = nilfs_segctor_apply_buffers(
+               size_t n, rest = nilfs_segctor_buffer_rest(sci);
+
+               n = nilfs_lookup_dirty_data_buffers(
+                       inode, &data_buffers, rest + 1, 0, LLONG_MAX);
+               if (n > rest) {
+                       err = nilfs_segctor_apply_buffers(
                                sci, inode, &data_buffers,
-                               err == -E2BIG ? sc_ops->collect_data : NULL);
-                       if (err == -E2BIG)
-                               err = err2;
+                               sc_ops->collect_data);
+                       BUG_ON(!err); /* always receive -E2BIG or true error */
                        goto break_or_fail;
                }
        }
@@ -1114,16 +1101,20 @@ static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
                                         struct inode *inode)
 {
        LIST_HEAD(data_buffers);
-       int err, err2;
-
-       err = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, sci);
-       err2 = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
-                                          (!err || err == -E2BIG) ?
-                                          nilfs_collect_file_data : NULL);
-       if (err == -E2BIG)
-               err = err2;
-       if (!err)
+       size_t n, rest = nilfs_segctor_buffer_rest(sci);
+       int err;
+
+       n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
+                                           sci->sc_dsync_start,
+                                           sci->sc_dsync_end);
+
+       err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
+                                         nilfs_collect_file_data);
+       if (!err) {
                nilfs_segctor_end_finfo(sci, inode);
+               BUG_ON(n > rest);
+               /* always receive -E2BIG or true error if n > rest */
+       }
        return err;
 }
 
@@ -1133,6 +1124,7 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
        struct the_nilfs *nilfs = sbi->s_nilfs;
        struct list_head *head;
        struct nilfs_inode_info *ii;
+       size_t ndone;
        int err = 0;
 
        switch (sci->sc_stage.scnt) {
@@ -1201,26 +1193,6 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
                        sci->sc_stage.scnt = NILFS_ST_DONE;
                        return 0;
                }
-               sci->sc_stage.scnt++;  /* Fall through */
-       case NILFS_ST_SKETCH:
-               if (mode == SC_LSEG_SR && sci->sc_sketch_inode) {
-                       ii = NILFS_I(sci->sc_sketch_inode);
-                       if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
-                               sci->sc_sketch_inode->i_ctime.tv_sec
-                                       = sci->sc_seg_ctime;
-                               sci->sc_sketch_inode->i_mtime.tv_sec
-                                       = sci->sc_seg_ctime;
-                               err = nilfs_mark_inode_dirty(
-                                       sci->sc_sketch_inode);
-                               if (unlikely(err))
-                                       goto break_or_fail;
-                       }
-                       err = nilfs_segctor_scan_file(sci,
-                                                     sci->sc_sketch_inode,
-                                                     &nilfs_sc_file_ops);
-                       if (unlikely(err))
-                               goto break_or_fail;
-               }
                sci->sc_stage.scnt++;
                sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
                /* Fall through */
@@ -1242,10 +1214,16 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
                        break;
                sci->sc_stage.scnt++;  /* Fall through */
        case NILFS_ST_SUFILE:
-               err = nilfs_segctor_prepare_free_segments(sci,
-                                                         nilfs->ns_sufile);
-               if (unlikely(err))
+               err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
+                                        sci->sc_nfreesegs, &ndone);
+               if (unlikely(err)) {
+                       nilfs_sufile_cancel_freev(nilfs->ns_sufile,
+                                                 sci->sc_freesegs, ndone,
+                                                 NULL);
                        break;
+               }
+               sci->sc_stage.flags |= NILFS_CF_SUFREED;
+
                err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
                                              &nilfs_sc_file_ops);
                if (unlikely(err))
@@ -1276,14 +1254,13 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
        case NILFS_ST_DSYNC:
  dsync_mode:
                sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
-               ii = sci->sc_stage.dirty_file_ptr;
+               ii = sci->sc_dsync_inode;
                if (!test_bit(NILFS_I_BUSY, &ii->i_state))
                        break;
 
                err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
                if (unlikely(err))
                        break;
-               sci->sc_stage.dirty_file_ptr = NULL;
                sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
                sci->sc_stage.scnt = NILFS_ST_DONE;
                return 0;
@@ -1297,99 +1274,75 @@ static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
        return err;
 }
 
-static int nilfs_segctor_terminate_segment(struct nilfs_sc_info *sci,
-                                          struct nilfs_segment_buffer *segbuf,
-                                          struct inode *sufile)
-{
-       struct nilfs_segment_entry *ent = segbuf->sb_segent;
-       int err;
-
-       err = nilfs_open_segment_entry(ent, sufile);
-       if (unlikely(err))
-               return err;
-       nilfs_mdt_mark_buffer_dirty(ent->bh_su);
-       nilfs_mdt_mark_dirty(sufile);
-       nilfs_close_segment_entry(ent, sufile);
-
-       list_add_tail(&ent->list, &sci->sc_active_segments);
-       segbuf->sb_segent = NULL;
-       return 0;
-}
-
-static int nilfs_touch_segusage(struct inode *sufile, __u64 segnum)
-{
-       struct buffer_head *bh_su;
-       struct nilfs_segment_usage *raw_su;
-       int err;
-
-       err = nilfs_sufile_get_segment_usage(sufile, segnum, &raw_su, &bh_su);
-       if (unlikely(err))
-               return err;
-       nilfs_mdt_mark_buffer_dirty(bh_su);
-       nilfs_mdt_mark_dirty(sufile);
-       nilfs_sufile_put_segment_usage(sufile, segnum, bh_su);
-       return 0;
-}
-
+/**
+ * nilfs_segctor_begin_construction - setup segment buffer to make a new log
+ * @sci: nilfs_sc_info
+ * @nilfs: nilfs object
+ */
 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
                                            struct the_nilfs *nilfs)
 {
-       struct nilfs_segment_buffer *segbuf, *n;
-       struct inode *sufile = nilfs->ns_sufile;
+       struct nilfs_segment_buffer *segbuf, *prev;
        __u64 nextnum;
-       int err;
+       int err, alloc = 0;
 
-       if (list_empty(&sci->sc_segbufs)) {
-               segbuf = nilfs_segbuf_new(sci->sc_super);
-               if (unlikely(!segbuf))
-                       return -ENOMEM;
-               list_add(&segbuf->sb_list, &sci->sc_segbufs);
-       } else
-               segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
+       segbuf = nilfs_segbuf_new(sci->sc_super);
+       if (unlikely(!segbuf))
+               return -ENOMEM;
 
-       err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
-                              nilfs->ns_pseg_offset, nilfs);
-       if (unlikely(err))
-               return err;
+       if (list_empty(&sci->sc_write_logs)) {
+               nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
+                                nilfs->ns_pseg_offset, nilfs);
+               if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
+                       nilfs_shift_to_next_segment(nilfs);
+                       nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
+               }
 
-       if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
-               err = nilfs_segctor_terminate_segment(sci, segbuf, sufile);
-               if (unlikely(err))
-                       return err;
+               segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
+               nextnum = nilfs->ns_nextnum;
 
-               nilfs_shift_to_next_segment(nilfs);
-               err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
+               if (nilfs->ns_segnum == nilfs->ns_nextnum)
+                       /* Start from the head of a new full segment */
+                       alloc++;
+       } else {
+               /* Continue logs */
+               prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
+               nilfs_segbuf_map_cont(segbuf, prev);
+               segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
+               nextnum = prev->sb_nextnum;
+
+               if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
+                       nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
+                       segbuf->sb_sum.seg_seq++;
+                       alloc++;
+               }
        }
-       sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
-
-       err = nilfs_touch_segusage(sufile, segbuf->sb_segnum);
-       if (unlikely(err))
-               return err;
 
-       if (nilfs->ns_segnum == nilfs->ns_nextnum) {
-               /* Start from the head of a new full segment */
-               err = nilfs_sufile_alloc(sufile, &nextnum);
-               if (unlikely(err))
-                       return err;
-       } else
-               nextnum = nilfs->ns_nextnum;
+       err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
+       if (err)
+               goto failed;
 
-       segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
+       if (alloc) {
+               err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
+               if (err)
+                       goto failed;
+       }
        nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
 
-       /* truncating segment buffers */
-       list_for_each_entry_safe_continue(segbuf, n, &sci->sc_segbufs,
-                                         sb_list) {
-               list_del_init(&segbuf->sb_list);
-               nilfs_segbuf_free(segbuf);
-       }
+       BUG_ON(!list_empty(&sci->sc_segbufs));
+       list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
+       sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
+       return 0;
+
+ failed:
+       nilfs_segbuf_free(segbuf);
        return err;
 }
 
 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
                                         struct the_nilfs *nilfs, int nadd)
 {
-       struct nilfs_segment_buffer *segbuf, *prev, *n;
+       struct nilfs_segment_buffer *segbuf, *prev;
        struct inode *sufile = nilfs->ns_sufile;
        __u64 nextnextnum;
        LIST_HEAD(list);
@@ -1402,7 +1355,7 @@ static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
         * not be dirty.  The following call ensures that the buffer is dirty
         * and will pin the buffer on memory until the sufile is written.
         */
-       err = nilfs_touch_segusage(sufile, prev->sb_nextnum);
+       err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
        if (unlikely(err))
                return err;
 
@@ -1414,10 +1367,7 @@ static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
                        goto failed;
 
                /* map this buffer to region of segment on-disk */
-               err = nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
-               if (unlikely(err))
-                       goto failed_segbuf;
-
+               nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
                sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
 
                /* allocate the next next full segment */
@@ -1431,33 +1381,33 @@ static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
                list_add_tail(&segbuf->sb_list, &list);
                prev = segbuf;
        }
-       list_splice(&list, sci->sc_segbufs.prev);
+       list_splice_tail(&list, &sci->sc_segbufs);
        return 0;
 
  failed_segbuf:
        nilfs_segbuf_free(segbuf);
  failed:
-       list_for_each_entry_safe(segbuf, n, &list, sb_list) {
+       list_for_each_entry(segbuf, &list, sb_list) {
                ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
-               BUG_ON(ret);
-               list_del_init(&segbuf->sb_list);
-               nilfs_segbuf_free(segbuf);
+               WARN_ON(ret); /* never fails */
        }
+       nilfs_destroy_logs(&list);
        return err;
 }
 
-static void nilfs_segctor_free_incomplete_segments(struct nilfs_sc_info *sci,
-                                                  struct the_nilfs *nilfs)
+static void nilfs_free_incomplete_logs(struct list_head *logs,
+                                      struct the_nilfs *nilfs)
 {
-       struct nilfs_segment_buffer *segbuf;
-       int ret, done = 0;
+       struct nilfs_segment_buffer *segbuf, *prev;
+       struct inode *sufile = nilfs->ns_sufile;
+       int ret;
 
-       segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
+       segbuf = NILFS_FIRST_SEGBUF(logs);
        if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
-               ret = nilfs_sufile_free(nilfs->ns_sufile, segbuf->sb_nextnum);
-               BUG_ON(ret);
+               ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
+               WARN_ON(ret); /* never fails */
        }
-       if (segbuf->sb_io_error) {
+       if (atomic_read(&segbuf->sb_err)) {
                /* Case 1: The first segment failed */
                if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
                        /* Case 1a:  Partial segment appended into an existing
@@ -1466,98 +1416,54 @@ static void nilfs_segctor_free_incomplete_segments(struct nilfs_sc_info *sci,
                                                segbuf->sb_fseg_end);
                else /* Case 1b:  New full segment */
                        set_nilfs_discontinued(nilfs);
-               done++;
        }
 
-       list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
-               ret = nilfs_sufile_free(nilfs->ns_sufile, segbuf->sb_nextnum);
-               BUG_ON(ret);
-               if (!done && segbuf->sb_io_error) {
-                       if (segbuf->sb_segnum != nilfs->ns_nextnum)
-                               /* Case 2: extended segment (!= next) failed */
-                               nilfs_sufile_set_error(nilfs->ns_sufile,
-                                                      segbuf->sb_segnum);
-                       done++;
+       prev = segbuf;
+       list_for_each_entry_continue(segbuf, logs, sb_list) {
+               if (prev->sb_nextnum != segbuf->sb_nextnum) {
+                       ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
+                       WARN_ON(ret); /* never fails */
                }
+               if (atomic_read(&segbuf->sb_err) &&
+                   segbuf->sb_segnum != nilfs->ns_nextnum)
+                       /* Case 2: extended segment (!= next) failed */
+                       nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
+               prev = segbuf;
        }
 }
 
-static void nilfs_segctor_clear_segment_buffers(struct nilfs_sc_info *sci)
-{
-       struct nilfs_segment_buffer *segbuf;
-
-       list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list)
-               nilfs_segbuf_clear(segbuf);
-       sci->sc_super_root = NULL;
-}
-
-static void nilfs_segctor_destroy_segment_buffers(struct nilfs_sc_info *sci)
-{
-       struct nilfs_segment_buffer *segbuf;
-
-       while (!list_empty(&sci->sc_segbufs)) {
-               segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
-               list_del_init(&segbuf->sb_list);
-               nilfs_segbuf_free(segbuf);
-       }
-       /* sci->sc_curseg = NULL; */
-}
-
-static void nilfs_segctor_end_construction(struct nilfs_sc_info *sci,
-                                          struct the_nilfs *nilfs, int err)
-{
-       if (unlikely(err)) {
-               nilfs_segctor_free_incomplete_segments(sci, nilfs);
-               nilfs_segctor_cancel_free_segments(sci, nilfs->ns_sufile);
-       }
-       nilfs_segctor_clear_segment_buffers(sci);
-}
-
 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
                                          struct inode *sufile)
 {
        struct nilfs_segment_buffer *segbuf;
-       struct buffer_head *bh_su;
-       struct nilfs_segment_usage *raw_su;
        unsigned long live_blocks;
        int ret;
 
        list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
-               ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
-                                                    &raw_su, &bh_su);
-               BUG_ON(ret); /* always succeed because bh_su is dirty */
                live_blocks = segbuf->sb_sum.nblocks +
                        (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
-               raw_su->su_lastmod = cpu_to_le64(sci->sc_seg_ctime);
-               raw_su->su_nblocks = cpu_to_le32(live_blocks);
-               nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum,
-                                              bh_su);
+               ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
+                                                    live_blocks,
+                                                    sci->sc_seg_ctime);
+               WARN_ON(ret); /* always succeed because the segusage is dirty */
        }
 }
 
-static void nilfs_segctor_cancel_segusage(struct nilfs_sc_info *sci,
-                                         struct inode *sufile)
+static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
 {
        struct nilfs_segment_buffer *segbuf;
-       struct buffer_head *bh_su;
-       struct nilfs_segment_usage *raw_su;
        int ret;
 
-       segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
-       ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
-                                            &raw_su, &bh_su);
-       BUG_ON(ret); /* always succeed because bh_su is dirty */
-       raw_su->su_nblocks = cpu_to_le32(segbuf->sb_pseg_start -
-                                        segbuf->sb_fseg_start);
-       nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum, bh_su);
+       segbuf = NILFS_FIRST_SEGBUF(logs);
+       ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
+                                            segbuf->sb_pseg_start -
+                                            segbuf->sb_fseg_start, 0);
+       WARN_ON(ret); /* always succeed because the segusage is dirty */
 
-       list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
-               ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
-                                                    &raw_su, &bh_su);
-               BUG_ON(ret); /* always succeed */
-               raw_su->su_nblocks = 0;
-               nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum,
-                                              bh_su);
+       list_for_each_entry_continue(segbuf, logs, sb_list) {
+               ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
+                                                    0, 0);
+               WARN_ON(ret); /* always succeed */
        }
 }
 
@@ -1565,17 +1471,15 @@ static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
                                            struct nilfs_segment_buffer *last,
                                            struct inode *sufile)
 {
-       struct nilfs_segment_buffer *segbuf = last, *n;
+       struct nilfs_segment_buffer *segbuf = last;
        int ret;
 
-       list_for_each_entry_safe_continue(segbuf, n, &sci->sc_segbufs,
-                                         sb_list) {
-               list_del_init(&segbuf->sb_list);
+       list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
                sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
                ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
-               BUG_ON(ret);
-               nilfs_segbuf_free(segbuf);
+               WARN_ON(ret);
        }
+       nilfs_truncate_logs(&sci->sc_segbufs, last);
 }
 
 
@@ -1607,13 +1511,19 @@ static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
                if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
                        break;
 
-               nilfs_segctor_cancel_free_segments(sci, nilfs->ns_sufile);
-               nilfs_segctor_clear_segment_buffers(sci);
+               nilfs_clear_logs(&sci->sc_segbufs);
 
                err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
                if (unlikely(err))
                        return err;
 
+               if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
+                       err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
+                                                       sci->sc_freesegs,
+                                                       sci->sc_nfreesegs,
+                                                       NULL);
+                       WARN_ON(err); /* do not happen */
+               }
                nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
                sci->sc_stage = prev_stage;
        }
@@ -1853,44 +1763,17 @@ static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
 }
 
 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
-                              struct backing_dev_info *bdi)
-{
-       struct nilfs_segment_buffer *segbuf;
-       struct nilfs_write_info wi;
-       int err, res;
-
-       wi.sb = sci->sc_super;
-       wi.bh_sr = sci->sc_super_root;
-       wi.bdi = bdi;
-
-       list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
-               nilfs_segbuf_prepare_write(segbuf, &wi);
-               err = nilfs_segbuf_write(segbuf, &wi);
-
-               res = nilfs_segbuf_wait(segbuf, &wi);
-               err = unlikely(err) ? : res;
-               if (unlikely(err))
-                       return err;
-       }
-       return 0;
-}
-
-static int nilfs_page_has_uncleared_buffer(struct page *page)
+                              struct the_nilfs *nilfs)
 {
-       struct buffer_head *head, *bh;
+       int ret;
 
-       head = bh = page_buffers(page);
-       do {
-               if (buffer_dirty(bh) && !list_empty(&bh->b_assoc_buffers))
-                       return 1;
-               bh = bh->b_this_page;
-       } while (bh != head);
-       return 0;
+       ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
+       list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
+       return ret;
 }
 
 static void __nilfs_end_page_io(struct page *page, int err)
 {
-       /* BUG_ON(err > 0); */
        if (!err) {
                if (!nilfs_page_buffers_clean(page))
                        __set_page_dirty_nobuffers(page);
@@ -1912,13 +1795,26 @@ static void nilfs_end_page_io(struct page *page, int err)
        if (!page)
                return;
 
-       if (buffer_nilfs_node(page_buffers(page)) &&
-           nilfs_page_has_uncleared_buffer(page))
-               /* For b-tree node pages, this function may be called twice
-                  or more because they might be split in a segment.
-                  This check assures that cleanup has been done for all
-                  buffers in a split btnode page. */
+       if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
+               /*
+                * For b-tree node pages, this function may be called twice
+                * or more because they might be split in a segment.
+                */
+               if (PageDirty(page)) {
+                       /*
+                        * For pages holding split b-tree node buffers, dirty
+                        * flag on the buffers may be cleared discretely.
+                        * In that case, the page is once redirtied for
+                        * remaining buffers, and it must be cancelled if
+                        * all the buffers get cleaned later.
+                        */
+                       lock_page(page);
+                       if (nilfs_page_buffers_clean(page))
+                               __nilfs_clear_page_dirty(page);
+                       unlock_page(page);
+               }
                return;
+       }
 
        __nilfs_end_page_io(page, err);
 }
@@ -1951,15 +1847,17 @@ static void nilfs_clear_copied_buffers(struct list_head *list, int err)
        }
 }
 
-static void nilfs_segctor_abort_write(struct nilfs_sc_info *sci,
-                                     struct page *failed_page, int err)
+static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
+                            struct buffer_head *bh_sr, int err)
 {
        struct nilfs_segment_buffer *segbuf;
        struct page *bd_page = NULL, *fs_page = NULL;
+       struct buffer_head *bh;
 
-       list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
-               struct buffer_head *bh;
+       if (list_empty(logs))
+               return;
 
+       list_for_each_entry(segbuf, logs, sb_list) {
                list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
                                    b_assoc_buffers) {
                        if (bh->b_page != bd_page) {
@@ -1971,7 +1869,7 @@ static void nilfs_segctor_abort_write(struct nilfs_sc_info *sci,
 
                list_for_each_entry(bh, &segbuf->sb_payload_buffers,
                                    b_assoc_buffers) {
-                       if (bh == sci->sc_super_root) {
+                       if (bh == bh_sr) {
                                if (bh->b_page != bd_page) {
                                        end_page_writeback(bd_page);
                                        bd_page = bh->b_page;
@@ -1980,8 +1878,8 @@ static void nilfs_segctor_abort_write(struct nilfs_sc_info *sci,
                        }
                        if (bh->b_page != fs_page) {
                                nilfs_end_page_io(fs_page, err);
-                               if (unlikely(fs_page == failed_page))
-                                       goto done;
+                               if (fs_page && fs_page == failed_page)
+                                       return;
                                fs_page = bh->b_page;
                        }
                }
@@ -1990,8 +1888,33 @@ static void nilfs_segctor_abort_write(struct nilfs_sc_info *sci,
                end_page_writeback(bd_page);
 
        nilfs_end_page_io(fs_page, err);
- done:
+}
+
+static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
+                                            struct the_nilfs *nilfs, int err)
+{
+       LIST_HEAD(logs);
+       int ret;
+
+       list_splice_tail_init(&sci->sc_write_logs, &logs);
+       ret = nilfs_wait_on_logs(&logs);
+       nilfs_abort_logs(&logs, NULL, sci->sc_super_root, ret ? : err);
+
+       list_splice_tail_init(&sci->sc_segbufs, &logs);
+       nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
+       nilfs_free_incomplete_logs(&logs, nilfs);
        nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
+
+       if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
+               ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
+                                               sci->sc_freesegs,
+                                               sci->sc_nfreesegs,
+                                               NULL);
+               WARN_ON(ret); /* do not happen */
+       }
+
+       nilfs_destroy_logs(&logs);
+       sci->sc_super_root = NULL;
 }
 
 static void nilfs_set_next_segment(struct the_nilfs *nilfs,
@@ -2009,11 +1932,10 @@ static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
 {
        struct nilfs_segment_buffer *segbuf;
        struct page *bd_page = NULL, *fs_page = NULL;
-       struct nilfs_sb_info *sbi = sci->sc_sbi;
-       struct the_nilfs *nilfs = sbi->s_nilfs;
+       struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
        int update_sr = (sci->sc_super_root != NULL);
 
-       list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
+       list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
                struct buffer_head *bh;
 
                list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
@@ -2081,27 +2003,39 @@ static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
                nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
                if (update_sr)
                        nilfs_commit_gcdat_inode(nilfs);
-       } else {
+       } else
                nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
-               set_nilfs_cond_nongc_write(nilfs);
-               wake_up(&nilfs->ns_cleanerd_wq);
-       }
 
        sci->sc_nblk_inc += sci->sc_nblk_this_inc;
 
-       segbuf = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
+       segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
        nilfs_set_next_segment(nilfs, segbuf);
 
        if (update_sr) {
                nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
-                                      segbuf->sb_sum.seg_seq, nilfs->ns_cno);
+                                      segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
+               set_nilfs_sb_dirty(nilfs);
 
+               clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
                clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
                set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
+               nilfs_segctor_clear_metadata_dirty(sci);
        } else
                clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
 }
 
+static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
+{
+       int ret;
+
+       ret = nilfs_wait_on_logs(&sci->sc_write_logs);
+       if (!ret) {
+               nilfs_segctor_complete_write(sci);
+               nilfs_destroy_logs(&sci->sc_write_logs);
+       }
+       return ret;
+}
+
 static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
                                        struct nilfs_sb_info *sbi)
 {
@@ -2174,123 +2108,6 @@ static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
 }
 
 /*
- * Nasty routines to manipulate active flags on sufile.
- * These would be removed in a future release.
- */
-static void nilfs_segctor_reactivate_segments(struct nilfs_sc_info *sci,
-                                             struct the_nilfs *nilfs)
-{
-       struct nilfs_segment_buffer *segbuf, *last;
-       struct nilfs_segment_entry *ent, *n;
-       struct inode *sufile = nilfs->ns_sufile;
-       struct list_head *head;
-
-       last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
-       nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
-               ent = segbuf->sb_segent;
-               if (!ent)
-                       break; /* ignore unmapped segments (should check it?)*/
-               nilfs_segment_usage_set_active(ent->raw_su);
-               nilfs_close_segment_entry(ent, sufile);
-       }
-
-       head = &sci->sc_active_segments;
-       list_for_each_entry_safe(ent, n, head, list) {
-               nilfs_segment_usage_set_active(ent->raw_su);
-               nilfs_close_segment_entry(ent, sufile);
-       }
-
-       down_write(&nilfs->ns_sem);
-       head = &nilfs->ns_used_segments;
-       list_for_each_entry(ent, head, list) {
-               nilfs_segment_usage_set_volatile_active(ent->raw_su);
-       }
-       up_write(&nilfs->ns_sem);
-}
-
-static int nilfs_segctor_deactivate_segments(struct nilfs_sc_info *sci,
-                                            struct the_nilfs *nilfs)
-{
-       struct nilfs_segment_buffer *segbuf, *last;
-       struct nilfs_segment_entry *ent;
-       struct inode *sufile = nilfs->ns_sufile;
-       struct list_head *head;
-       int err;
-
-       last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
-       nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
-               /*
-                * Deactivate ongoing full segments.  The last segment is kept
-                * active because it is a start point of recovery, and is not
-                * relocatable until the super block points to a newer
-                * checkpoint.
-                */
-               ent = segbuf->sb_segent;
-               if (!ent)
-                       break; /* ignore unmapped segments (should check it?)*/
-               err = nilfs_open_segment_entry(ent, sufile);
-               if (unlikely(err))
-                       goto failed;
-               nilfs_segment_usage_clear_active(ent->raw_su);
-               BUG_ON(!buffer_dirty(ent->bh_su));
-       }
-
-       head = &sci->sc_active_segments;
-       list_for_each_entry(ent, head, list) {
-               err = nilfs_open_segment_entry(ent, sufile);
-               if (unlikely(err))
-                       goto failed;
-               nilfs_segment_usage_clear_active(ent->raw_su);
-               BUG_ON(!buffer_dirty(ent->bh_su));
-       }
-
-       down_write(&nilfs->ns_sem);
-       head = &nilfs->ns_used_segments;
-       list_for_each_entry(ent, head, list) {
-               /* clear volatile active for segments of older generations */
-               nilfs_segment_usage_clear_volatile_active(ent->raw_su);
-       }
-       up_write(&nilfs->ns_sem);
-       return 0;
-
- failed:
-       nilfs_segctor_reactivate_segments(sci, nilfs);
-       return err;
-}
-
-static void nilfs_segctor_bead_completed_segments(struct nilfs_sc_info *sci)
-{
-       struct nilfs_segment_buffer *segbuf, *last;
-       struct nilfs_segment_entry *ent;
-
-       /* move each segbuf->sb_segent to the list of used active segments */
-       last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
-       nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
-               ent = segbuf->sb_segent;
-               if (!ent)
-                       break; /* ignore unmapped segments (should check it?)*/
-               list_add_tail(&ent->list, &sci->sc_active_segments);
-               segbuf->sb_segent = NULL;
-       }
-}
-
-static void
-__nilfs_segctor_commit_deactivate_segments(struct nilfs_sc_info *sci,
-                                          struct the_nilfs *nilfs)
-
-{
-       struct nilfs_segment_entry *ent;
-
-       list_splice_init(&sci->sc_active_segments,
-                        nilfs->ns_used_segments.prev);
-
-       list_for_each_entry(ent, &nilfs->ns_used_segments, list) {
-               nilfs_segment_usage_set_volatile_active(ent->raw_su);
-               /* These segments are kept open */
-       }
-}
-
-/*
  * Main procedure of segment constructor
  */
 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
@@ -2331,8 +2148,7 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
                /* Avoid empty segment */
                if (sci->sc_stage.scnt == NILFS_ST_DONE &&
                    NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
-                       BUG_ON(mode == SC_LSEG_SR);
-                       nilfs_segctor_end_construction(sci, nilfs, 1);
+                       nilfs_segctor_abort_construction(sci, nilfs, 1);
                        goto out;
                }
 
@@ -2340,18 +2156,13 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
                if (unlikely(err))
                        goto failed;
 
-               if (has_sr) {
-                       err = nilfs_segctor_deactivate_segments(sci, nilfs);
-                       if (unlikely(err))
-                               goto failed;
-               }
                if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
                        nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
 
                if (has_sr) {
                        err = nilfs_segctor_fill_in_checkpoint(sci);
                        if (unlikely(err))
-                               goto failed_to_make_up;
+                               goto failed_to_write;
 
                        nilfs_segctor_fill_in_super_root(sci, nilfs);
                }
@@ -2359,63 +2170,51 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
 
                /* Write partial segments */
                err = nilfs_segctor_prepare_write(sci, &failed_page);
-               if (unlikely(err))
+               if (err) {
+                       nilfs_abort_logs(&sci->sc_segbufs, failed_page,
+                                        sci->sc_super_root, err);
                        goto failed_to_write;
-
+               }
                nilfs_segctor_fill_in_checksums(sci, nilfs->ns_crc_seed);
 
-               err = nilfs_segctor_write(sci, nilfs->ns_bdi);
+               err = nilfs_segctor_write(sci, nilfs);
                if (unlikely(err))
                        goto failed_to_write;
 
-               nilfs_segctor_complete_write(sci);
-
-               /* Commit segments */
-               nilfs_segctor_bead_completed_segments(sci);
-               if (has_sr) {
-                       down_write(&nilfs->ns_sem);
-                       nilfs_update_last_segment(sbi, 1);
-                       __nilfs_segctor_commit_deactivate_segments(sci, nilfs);
-                       up_write(&nilfs->ns_sem);
-                       nilfs_segctor_commit_free_segments(sci);
-                       nilfs_segctor_clear_metadata_dirty(sci);
+               if (sci->sc_stage.scnt == NILFS_ST_DONE ||
+                   nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
+                       /*
+                        * At this point, we avoid double buffering
+                        * for blocksize < pagesize because page dirty
+                        * flag is turned off during write and dirty
+                        * buffers are not properly collected for
+                        * pages crossing over segments.
+                        */
+                       err = nilfs_segctor_wait(sci);
+                       if (err)
+                               goto failed_to_write;
                }
-
-               nilfs_segctor_end_construction(sci, nilfs, 0);
-
        } while (sci->sc_stage.scnt != NILFS_ST_DONE);
 
-       /* Clearing sketch data */
-       if (has_sr && sci->sc_sketch_inode) {
-               if (i_size_read(sci->sc_sketch_inode) == 0)
-                       clear_bit(NILFS_I_DIRTY,
-                                 &NILFS_I(sci->sc_sketch_inode)->i_state);
-               i_size_write(sci->sc_sketch_inode, 0);
-       }
+       sci->sc_super_root = NULL;
+
  out:
-       nilfs_segctor_destroy_segment_buffers(sci);
        nilfs_segctor_check_out_files(sci, sbi);
        return err;
 
  failed_to_write:
-       nilfs_segctor_abort_write(sci, failed_page, err);
-       nilfs_segctor_cancel_segusage(sci, nilfs->ns_sufile);
-
- failed_to_make_up:
        if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
                nilfs_redirty_inodes(&sci->sc_dirty_files);
-       if (has_sr)
-               nilfs_segctor_reactivate_segments(sci, nilfs);
 
  failed:
        if (nilfs_doing_gc())
                nilfs_redirty_inodes(&sci->sc_gc_inodes);
-       nilfs_segctor_end_construction(sci, nilfs, err);
+       nilfs_segctor_abort_construction(sci, nilfs, err);
        goto out;
 }
 
 /**
- * nilfs_secgtor_start_timer - set timer of background write
+ * nilfs_segctor_start_timer - set timer of background write
  * @sci: nilfs_sc_info
  *
  * If the timer has already been set, it ignores the new request.
@@ -2462,69 +2261,6 @@ void nilfs_flush_segment(struct super_block *sb, ino_t ino)
                                        /* assign bit 0 to data files */
 }
 
-int nilfs_segctor_add_segments_to_be_freed(struct nilfs_sc_info *sci,
-                                          __u64 *segnum, size_t nsegs)
-{
-       struct nilfs_segment_entry *ent;
-       struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
-       struct inode *sufile = nilfs->ns_sufile;
-       LIST_HEAD(list);
-       __u64 *pnum;
-       const char *flag_name;
-       size_t i;
-       int err, err2 = 0;
-
-       for (pnum = segnum, i = 0; i < nsegs; pnum++, i++) {
-               ent = nilfs_alloc_segment_entry(*pnum);
-               if (unlikely(!ent)) {
-                       err = -ENOMEM;
-                       goto failed;
-               }
-               list_add_tail(&ent->list, &list);
-
-               err = nilfs_open_segment_entry(ent, sufile);
-               if (unlikely(err))
-                       goto failed;
-
-               if (unlikely(le32_to_cpu(ent->raw_su->su_flags) !=
-                            (1UL << NILFS_SEGMENT_USAGE_DIRTY))) {
-                       if (nilfs_segment_usage_clean(ent->raw_su))
-                               flag_name = "clean";
-                       else if (nilfs_segment_usage_active(ent->raw_su))
-                               flag_name = "active";
-                       else if (nilfs_segment_usage_volatile_active(
-                                        ent->raw_su))
-                               flag_name = "volatile active";
-                       else if (!nilfs_segment_usage_dirty(ent->raw_su))
-                               flag_name = "non-dirty";
-                       else
-                               flag_name = "erroneous";
-
-                       printk(KERN_ERR
-                              "NILFS: %s segment is requested to be cleaned "
-                              "(segnum=%llu)\n",
-                              flag_name, (unsigned long long)ent->segnum);
-                       err2 = -EINVAL;
-               }
-               nilfs_close_segment_entry(ent, sufile);
-       }
-       if (unlikely(err2)) {
-               err = err2;
-               goto failed;
-       }
-       list_splice(&list, sci->sc_cleaning_segments.prev);
-       return 0;
-
- failed:
-       nilfs_dispose_segment_list(&list);
-       return err;
-}
-
-void nilfs_segctor_clear_segments_to_be_freed(struct nilfs_sc_info *sci)
-{
-       nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
-}
-
 struct nilfs_segctor_wait_request {
        wait_queue_t    wq;
        __u32           seq;
@@ -2624,7 +2360,9 @@ int nilfs_construct_segment(struct super_block *sb)
 /**
  * nilfs_construct_dsync_segment - construct a data-only logical segment
  * @sb: super block
- * @inode: the inode whose data blocks should be written out
+ * @inode: inode whose data blocks should be written out
+ * @start: start byte offset
+ * @end: end byte offset (inclusive)
  *
  * Return Value: On success, 0 is retured. On errors, one of the following
  * negative error code is returned.
@@ -2639,8 +2377,8 @@ int nilfs_construct_segment(struct super_block *sb)
  *
  * %-ENOMEM - Insufficient memory available.
  */
-int nilfs_construct_dsync_segment(struct super_block *sb,
-                                 struct inode *inode)
+int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
+                                 loff_t start, loff_t end)
 {
        struct nilfs_sb_info *sbi = NILFS_SB(sb);
        struct nilfs_sc_info *sci = NILFS_SC(sbi);
@@ -2671,7 +2409,9 @@ int nilfs_construct_dsync_segment(struct super_block *sb,
                return 0;
        }
        spin_unlock(&sbi->s_inode_lock);
-       sci->sc_stage.dirty_file_ptr = ii;
+       sci->sc_dsync_inode = ii;
+       sci->sc_dsync_start = start;
+       sci->sc_dsync_end = end;
 
        err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
 
@@ -2679,73 +2419,84 @@ int nilfs_construct_dsync_segment(struct super_block *sb,
        return err;
 }
 
-struct nilfs_segctor_req {
-       int mode;
-       __u32 seq_accepted;
-       int sc_err;  /* construction failure */
-       int sb_err;  /* super block writeback failure */
-};
-
 #define FLUSH_FILE_BIT (0x1) /* data file only */
 #define FLUSH_DAT_BIT  (1 << NILFS_DAT_INO) /* DAT only */
 
-static void nilfs_segctor_accept(struct nilfs_sc_info *sci,
-                                struct nilfs_segctor_req *req)
+/**
+ * nilfs_segctor_accept - record accepted sequence count of log-write requests
+ * @sci: segment constructor object
+ */
+static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
 {
-       BUG_ON(!sci);
-
-       req->sc_err = req->sb_err = 0;
        spin_lock(&sci->sc_state_lock);
-       req->seq_accepted = sci->sc_seq_request;
+       sci->sc_seq_accepted = sci->sc_seq_request;
        spin_unlock(&sci->sc_state_lock);
 
        if (sci->sc_timer)
                del_timer_sync(sci->sc_timer);
 }
 
-static void nilfs_segctor_notify(struct nilfs_sc_info *sci,
-                                struct nilfs_segctor_req *req)
+/**
+ * nilfs_segctor_notify - notify the result of request to caller threads
+ * @sci: segment constructor object
+ * @mode: mode of log forming
+ * @err: error code to be notified
+ */
+static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
 {
        /* Clear requests (even when the construction failed) */
        spin_lock(&sci->sc_state_lock);
 
-       sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
-
-       if (req->mode == SC_LSEG_SR) {
-               sci->sc_seq_done = req->seq_accepted;
-               nilfs_segctor_wakeup(sci, req->sc_err ? : req->sb_err);
+       if (mode == SC_LSEG_SR) {
+               sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
+               sci->sc_seq_done = sci->sc_seq_accepted;
+               nilfs_segctor_wakeup(sci, err);
                sci->sc_flush_request = 0;
-       } else if (req->mode == SC_FLUSH_FILE)
-               sci->sc_flush_request &= ~FLUSH_FILE_BIT;
-       else if (req->mode == SC_FLUSH_DAT)
-               sci->sc_flush_request &= ~FLUSH_DAT_BIT;
+       } else {
+               if (mode == SC_FLUSH_FILE)
+                       sci->sc_flush_request &= ~FLUSH_FILE_BIT;
+               else if (mode == SC_FLUSH_DAT)
+                       sci->sc_flush_request &= ~FLUSH_DAT_BIT;
 
+               /* re-enable timer if checkpoint creation was not done */
+               if (sci->sc_timer && (sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
+                   time_before(jiffies, sci->sc_timer->expires))
+                       add_timer(sci->sc_timer);
+       }
        spin_unlock(&sci->sc_state_lock);
 }
 
-static int nilfs_segctor_construct(struct nilfs_sc_info *sci,
-                                  struct nilfs_segctor_req *req)
+/**
+ * nilfs_segctor_construct - form logs and write them to disk
+ * @sci: segment constructor object
+ * @mode: mode of log forming
+ */
+static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
 {
        struct nilfs_sb_info *sbi = sci->sc_sbi;
        struct the_nilfs *nilfs = sbi->s_nilfs;
        int err = 0;
 
+       nilfs_segctor_accept(sci);
+
        if (nilfs_discontinued(nilfs))
-               req->mode = SC_LSEG_SR;
-       if (!nilfs_segctor_confirm(sci)) {
-               err = nilfs_segctor_do_construct(sci, req->mode);
-               req->sc_err = err;
-       }
+               mode = SC_LSEG_SR;
+       if (!nilfs_segctor_confirm(sci))
+               err = nilfs_segctor_do_construct(sci, mode);
+
        if (likely(!err)) {
-               if (req->mode != SC_FLUSH_DAT)
+               if (mode != SC_FLUSH_DAT)
                        atomic_set(&nilfs->ns_ndirtyblks, 0);
                if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
                    nilfs_discontinued(nilfs)) {
                        down_write(&nilfs->ns_sem);
-                       req->sb_err = nilfs_commit_super(sbi);
+                       err = nilfs_commit_super(
+                               sbi, nilfs_altsb_need_update(nilfs));
                        up_write(&nilfs->ns_sem);
                }
        }
+
+       nilfs_segctor_notify(sci, mode, err);
        return err;
 }
 
@@ -2769,13 +2520,13 @@ nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
        }
 }
 
-int nilfs_clean_segments(struct super_block *sb, void __user *argp)
+int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
+                        void **kbufs)
 {
        struct nilfs_sb_info *sbi = NILFS_SB(sb);
        struct nilfs_sc_info *sci = NILFS_SC(sbi);
        struct the_nilfs *nilfs = sbi->s_nilfs;
        struct nilfs_transaction_info ti;
-       struct nilfs_segctor_req req = { .mode = SC_LSEG_SR };
        int err;
 
        if (unlikely(!sci))
@@ -2786,17 +2537,18 @@ int nilfs_clean_segments(struct super_block *sb, void __user *argp)
        err = nilfs_init_gcdat_inode(nilfs);
        if (unlikely(err))
                goto out_unlock;
-       err = nilfs_ioctl_prepare_clean_segments(nilfs, argp);
+
+       err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
        if (unlikely(err))
                goto out_unlock;
 
-       list_splice_init(&nilfs->ns_gc_inodes, sci->sc_gc_inodes.prev);
+       sci->sc_freesegs = kbufs[4];
+       sci->sc_nfreesegs = argv[4].v_nmembs;
+       list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
 
        for (;;) {
-               nilfs_segctor_accept(sci, &req);
-               err = nilfs_segctor_construct(sci, &req);
+               err = nilfs_segctor_construct(sci, SC_LSEG_SR);
                nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
-               nilfs_segctor_notify(sci, &req);
 
                if (likely(!err))
                        break;
@@ -2806,8 +2558,20 @@ int nilfs_clean_segments(struct super_block *sb, void __user *argp)
                set_current_state(TASK_INTERRUPTIBLE);
                schedule_timeout(sci->sc_interval);
        }
+       if (nilfs_test_opt(sbi, DISCARD)) {
+               int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
+                                                sci->sc_nfreesegs);
+               if (ret) {
+                       printk(KERN_WARNING
+                              "NILFS warning: error %d on discard request, "
+                              "turning discards off for the device\n", ret);
+                       nilfs_clear_opt(sbi, DISCARD);
+               }
+       }
 
  out_unlock:
+       sci->sc_freesegs = NULL;
+       sci->sc_nfreesegs = 0;
        nilfs_clear_gcdat_inode(nilfs);
        nilfs_transaction_unlock(sbi);
        return err;
@@ -2817,13 +2581,9 @@ static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
 {
        struct nilfs_sb_info *sbi = sci->sc_sbi;
        struct nilfs_transaction_info ti;
-       struct nilfs_segctor_req req = { .mode = mode };
 
        nilfs_transaction_lock(sbi, &ti, 0);
-
-       nilfs_segctor_accept(sci, &req);
-       nilfs_segctor_construct(sci, &req);
-       nilfs_segctor_notify(sci, &req);
+       nilfs_segctor_construct(sci, mode);
 
        /*
         * Unclosed segment should be retried.  We do this using sc_timer.
@@ -2879,6 +2639,7 @@ static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
 static int nilfs_segctor_thread(void *arg)
 {
        struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
+       struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
        struct timer_list timer;
        int timeout = 0;
 
@@ -2944,6 +2705,9 @@ static int nilfs_segctor_thread(void *arg)
                finish_wait(&sci->sc_wait_daemon, &wait);
                timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
                           time_after_eq(jiffies, sci->sc_timer->expires));
+
+               if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
+                       set_nilfs_discontinued(nilfs);
        }
        goto loop;
 
@@ -2986,32 +2750,11 @@ static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
        }
 }
 
-static int nilfs_segctor_init(struct nilfs_sc_info *sci,
-                             struct nilfs_recovery_info *ri)
+static int nilfs_segctor_init(struct nilfs_sc_info *sci)
 {
-       int err;
-       struct inode *inode = nilfs_iget(sci->sc_super, NILFS_SKETCH_INO);
-
-       sci->sc_sketch_inode = IS_ERR(inode) ? NULL : inode;
-       if (sci->sc_sketch_inode)
-               i_size_write(sci->sc_sketch_inode, 0);
-
        sci->sc_seq_done = sci->sc_seq_request;
-       if (ri)
-               list_splice_init(&ri->ri_used_segments,
-                                sci->sc_active_segments.prev);
 
-       err = nilfs_segctor_start_thread(sci);
-       if (err) {
-               if (ri)
-                       list_splice_init(&sci->sc_active_segments,
-                                        ri->ri_used_segments.prev);
-               if (sci->sc_sketch_inode) {
-                       iput(sci->sc_sketch_inode);
-                       sci->sc_sketch_inode = NULL;
-               }
-       }
-       return err;
+       return nilfs_segctor_start_thread(sci);
 }
 
 /*
@@ -3034,9 +2777,8 @@ static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
        spin_lock_init(&sci->sc_state_lock);
        INIT_LIST_HEAD(&sci->sc_dirty_files);
        INIT_LIST_HEAD(&sci->sc_segbufs);
+       INIT_LIST_HEAD(&sci->sc_write_logs);
        INIT_LIST_HEAD(&sci->sc_gc_inodes);
-       INIT_LIST_HEAD(&sci->sc_active_segments);
-       INIT_LIST_HEAD(&sci->sc_cleaning_segments);
        INIT_LIST_HEAD(&sci->sc_copied_buffers);
 
        sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
@@ -3059,12 +2801,9 @@ static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
        do {
                struct nilfs_sb_info *sbi = sci->sc_sbi;
                struct nilfs_transaction_info ti;
-               struct nilfs_segctor_req req = { .mode = SC_LSEG_SR };
 
                nilfs_transaction_lock(sbi, &ti, 0);
-               nilfs_segctor_accept(sci, &req);
-               ret = nilfs_segctor_construct(sci, &req);
-               nilfs_segctor_notify(sci, &req);
+               ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
                nilfs_transaction_unlock(sbi);
 
        } while (ret && retrycount-- > 0);
@@ -3091,28 +2830,20 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
                || sci->sc_seq_request != sci->sc_seq_done);
        spin_unlock(&sci->sc_state_lock);
 
-       if (flag || nilfs_segctor_confirm(sci))
+       if (flag || !nilfs_segctor_confirm(sci))
                nilfs_segctor_write_out(sci);
 
-       BUG_ON(!list_empty(&sci->sc_copied_buffers));
+       WARN_ON(!list_empty(&sci->sc_copied_buffers));
 
        if (!list_empty(&sci->sc_dirty_files)) {
                nilfs_warning(sbi->s_super, __func__,
                              "dirty file(s) after the final construction\n");
                nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
        }
-       if (!list_empty(&sci->sc_active_segments))
-               nilfs_dispose_segment_list(&sci->sc_active_segments);
 
-       if (!list_empty(&sci->sc_cleaning_segments))
-               nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
+       WARN_ON(!list_empty(&sci->sc_segbufs));
+       WARN_ON(!list_empty(&sci->sc_write_logs));
 
-       BUG_ON(!list_empty(&sci->sc_segbufs));
-
-       if (sci->sc_sketch_inode) {
-               iput(sci->sc_sketch_inode);
-               sci->sc_sketch_inode = NULL;
-       }
        down_write(&sbi->s_nilfs->ns_segctor_sem);
 
        kfree(sci);
@@ -3121,30 +2852,35 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
 /**
  * nilfs_attach_segment_constructor - attach a segment constructor
  * @sbi: nilfs_sb_info
- * @ri: nilfs_recovery_info
  *
  * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
- * initilizes it, and starts the segment constructor.
+ * initializes it, and starts the segment constructor.
  *
  * Return Value: On success, 0 is returned. On error, one of the following
  * negative error code is returned.
  *
  * %-ENOMEM - Insufficient memory available.
  */
-int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi,
-                                    struct nilfs_recovery_info *ri)
+int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
 {
        struct the_nilfs *nilfs = sbi->s_nilfs;
        int err;
 
-       /* Each field of nilfs_segctor is cleared through the initialization
-          of super-block info */
+       if (NILFS_SC(sbi)) {
+               /*
+                * This happens if the filesystem was remounted
+                * read/write after nilfs_error degenerated it into a
+                * read-only mount.
+                */
+               nilfs_detach_segment_constructor(sbi);
+       }
+
        sbi->s_sc_info = nilfs_segctor_new(sbi);
        if (!sbi->s_sc_info)
                return -ENOMEM;
 
        nilfs_attach_writer(nilfs, sbi);
-       err = nilfs_segctor_init(NILFS_SC(sbi), ri);
+       err = nilfs_segctor_init(NILFS_SC(sbi));
        if (err) {
                nilfs_detach_writer(nilfs, sbi);
                kfree(sbi->s_sc_info);