fsnotify: do not set group for a mark before it is on the i_list
[safe/jmp/linux-2.6] / fs / nilfs2 / segment.c
index 9a87410..683df89 100644 (file)
@@ -39,7 +39,6 @@
 #include "sufile.h"
 #include "cpfile.h"
 #include "ifile.h"
-#include "seglist.h"
 #include "segbuf.h"
 
 
@@ -67,7 +66,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 +78,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 {
@@ -463,6 +462,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 */
 }
 
@@ -808,7 +810,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));
 }
 
@@ -887,8 +889,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;
@@ -923,11 +929,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);
-       }
 }
 
 /*
@@ -1004,44 +1005,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);
-               WARN_ON(err); /* do not happen */
-               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,
@@ -1160,6 +1123,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) {
@@ -1228,26 +1192,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 */
@@ -1269,10 +1213,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))
@@ -1323,25 +1273,6 @@ 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;
@@ -1361,7 +1292,6 @@ 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;
        __u64 nextnum;
        int err;
 
@@ -1373,28 +1303,22 @@ static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
        } else
                segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
 
-       err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
-                              nilfs->ns_pseg_offset, nilfs);
-       if (unlikely(err))
-               return err;
+       nilfs_segbuf_map(segbuf, nilfs->ns_segnum, nilfs->ns_pseg_offset,
+                        nilfs);
 
        if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
-               err = nilfs_segctor_terminate_segment(sci, segbuf, sufile);
-               if (unlikely(err))
-                       return err;
-
                nilfs_shift_to_next_segment(nilfs);
-               err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
+               nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
        }
        sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
 
-       err = nilfs_touch_segusage(sufile, segbuf->sb_segnum);
+       err = nilfs_touch_segusage(nilfs->ns_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);
+               err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
                if (unlikely(err))
                        return err;
        } else
@@ -1409,7 +1333,7 @@ static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
                list_del_init(&segbuf->sb_list);
                nilfs_segbuf_free(segbuf);
        }
-       return err;
+       return 0;
 }
 
 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
@@ -1440,10 +1364,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 */
@@ -1534,7 +1455,15 @@ static void nilfs_segctor_end_construction(struct nilfs_sc_info *sci,
 {
        if (unlikely(err)) {
                nilfs_segctor_free_incomplete_segments(sci, nilfs);
-               nilfs_segctor_cancel_free_segments(sci, nilfs->ns_sufile);
+               if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
+                       int ret;
+
+                       ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
+                                                       sci->sc_freesegs,
+                                                       sci->sc_nfreesegs,
+                                                       NULL);
+                       WARN_ON(ret); /* do not happen */
+               }
        }
        nilfs_segctor_clear_segment_buffers(sci);
 }
@@ -1633,7 +1562,13 @@ 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);
+               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 */
+               }
                nilfs_segctor_clear_segment_buffers(sci);
 
                err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
@@ -1894,26 +1829,13 @@ static int nilfs_segctor_write(struct nilfs_sc_info *sci,
                err = nilfs_segbuf_write(segbuf, &wi);
 
                res = nilfs_segbuf_wait(segbuf, &wi);
-               err = unlikely(err) ? : res;
-               if (unlikely(err))
+               err = err ? : res;
+               if (err)
                        return err;
        }
        return 0;
 }
 
-static int nilfs_page_has_uncleared_buffer(struct page *page)
-{
-       struct buffer_head *head, *bh;
-
-       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;
-}
-
 static void __nilfs_end_page_io(struct page *page, int err)
 {
        if (!err) {
@@ -1937,13 +1859,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);
 }
@@ -2005,7 +1940,7 @@ 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))
+                               if (fs_page && fs_page == failed_page)
                                        goto done;
                                fs_page = bh->b_page;
                        }
@@ -2116,8 +2051,10 @@ static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
 
        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++);
+               sbi->s_super->s_dirt = 1;
 
+               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);
        } else
@@ -2196,102 +2133,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);
-       }
-}
-
-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;
-       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));
-       }
-
-       list_for_each_entry(ent, &sci->sc_active_segments, list) {
-               err = nilfs_open_segment_entry(ent, sufile);
-               if (unlikely(err))
-                       goto failed;
-               nilfs_segment_usage_clear_active(ent->raw_su);
-               WARN_ON(!buffer_dirty(ent->bh_su));
-       }
-       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, *n;
-
-       list_for_each_entry_safe(ent, n, &sci->sc_active_segments, list) {
-               list_del(&ent->list);
-               nilfs_close_segment_entry(ent, nilfs->ns_sufile);
-               nilfs_free_segment_entry(ent);
-       }
-}
-
-/*
  * Main procedure of segment constructor
  */
 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
@@ -2340,11 +2181,6 @@ 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);
 
@@ -2371,27 +2207,13 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
                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);
-                       up_write(&nilfs->ns_sem);
-                       nilfs_segctor_commit_deactivate_segments(sci, nilfs);
-                       nilfs_segctor_commit_free_segments(sci);
+               if (has_sr)
                        nilfs_segctor_clear_metadata_dirty(sci);
-               }
 
                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);
-       }
  out:
        nilfs_segctor_destroy_segment_buffers(sci);
        nilfs_segctor_check_out_files(sci, sbi);
@@ -2404,8 +2226,6 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
  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())
@@ -2462,48 +2282,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;
-       size_t i;
-       int err;
-
-       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(!nilfs_segment_usage_dirty(ent->raw_su)))
-                       printk(KERN_WARNING "NILFS: unused segment is "
-                              "requested to be cleaned (segnum=%llu)\n",
-                              (unsigned long long)ent->segnum);
-               nilfs_close_segment_entry(ent, sufile);
-       }
-       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;
@@ -2723,7 +2501,8 @@ static int nilfs_segctor_construct(struct nilfs_sc_info *sci,
                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);
+                       req->sb_err = nilfs_commit_super(sbi,
+                                       nilfs_altsb_need_update(nilfs));
                        up_write(&nilfs->ns_sem);
                }
        }
@@ -2750,7 +2529,8 @@ 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);
@@ -2767,10 +2547,13 @@ 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;
 
+       sci->sc_freesegs = kbufs[4];
+       sci->sc_nfreesegs = argv[4].v_nmembs;
        list_splice_init(&nilfs->ns_gc_inodes, sci->sc_gc_inodes.prev);
 
        for (;;) {
@@ -2789,6 +2572,8 @@ int nilfs_clean_segments(struct super_block *sb, void __user *argp)
        }
 
  out_unlock:
+       sci->sc_freesegs = NULL;
+       sci->sc_nfreesegs = 0;
        nilfs_clear_gcdat_inode(nilfs);
        nilfs_transaction_unlock(sbi);
        return err;
@@ -2905,6 +2690,7 @@ static int nilfs_segctor_thread(void *arg)
        } else {
                DEFINE_WAIT(wait);
                int should_sleep = 1;
+               struct the_nilfs *nilfs;
 
                prepare_to_wait(&sci->sc_wait_daemon, &wait,
                                TASK_INTERRUPTIBLE);
@@ -2925,6 +2711,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));
+               nilfs = sci->sc_sbi->s_nilfs;
+               if (sci->sc_super->s_dirt && nilfs_sb_need_update(nilfs))
+                       set_nilfs_discontinued(nilfs);
        }
        goto loop;
 
@@ -2967,32 +2756,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);
 }
 
 /*
@@ -3016,8 +2784,6 @@ static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
        INIT_LIST_HEAD(&sci->sc_dirty_files);
        INIT_LIST_HEAD(&sci->sc_segbufs);
        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;
@@ -3082,18 +2848,9 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
                              "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));
 
-       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);
@@ -3102,7 +2859,6 @@ 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.
@@ -3112,8 +2868,7 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
  *
  * %-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;
@@ -3125,7 +2880,7 @@ int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi,
                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);