2 * segment.c - NILFS segment constructor.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
24 #include <linux/pagemap.h>
25 #include <linux/buffer_head.h>
26 #include <linux/writeback.h>
27 #include <linux/bio.h>
28 #include <linux/completion.h>
29 #include <linux/blkdev.h>
30 #include <linux/backing-dev.h>
31 #include <linux/freezer.h>
32 #include <linux/kthread.h>
33 #include <linux/crc32.h>
34 #include <linux/pagevec.h>
49 #define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
51 #define SC_MAX_SEGDELTA 64 /* Upper limit of the number of segments
52 appended in collection retry loop */
54 /* Construction mode */
56 SC_LSEG_SR = 1, /* Make a logical segment having a super root */
57 SC_LSEG_DSYNC, /* Flush data blocks of a given file and make
58 a logical segment without a super root */
59 SC_FLUSH_FILE, /* Flush data files, leads to segment writes without
60 creating a checkpoint */
61 SC_FLUSH_DAT, /* Flush DAT file. This also creates segments without
65 /* Stage numbers of dirty block collection */
68 NILFS_ST_GC, /* Collecting dirty blocks for GC */
75 NILFS_ST_SR, /* Super root */
76 NILFS_ST_DSYNC, /* Data sync blocks */
80 /* State flags of collection */
81 #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
82 #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
83 #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED)
85 /* Operations depending on the construction mode and file type */
86 struct nilfs_sc_operations {
87 int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
89 int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
91 int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
93 void (*write_data_binfo)(struct nilfs_sc_info *,
94 struct nilfs_segsum_pointer *,
96 void (*write_node_binfo)(struct nilfs_sc_info *,
97 struct nilfs_segsum_pointer *,
104 static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
105 static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
106 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
107 static void nilfs_dispose_list(struct nilfs_sb_info *, struct list_head *,
110 #define nilfs_cnt32_gt(a, b) \
111 (typecheck(__u32, a) && typecheck(__u32, b) && \
112 ((__s32)(b) - (__s32)(a) < 0))
113 #define nilfs_cnt32_ge(a, b) \
114 (typecheck(__u32, a) && typecheck(__u32, b) && \
115 ((__s32)(a) - (__s32)(b) >= 0))
116 #define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a)
117 #define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a)
122 static struct kmem_cache *nilfs_transaction_cachep;
125 * nilfs_init_transaction_cache - create a cache for nilfs_transaction_info
127 * nilfs_init_transaction_cache() creates a slab cache for the struct
128 * nilfs_transaction_info.
130 * Return Value: On success, it returns 0. On error, one of the following
131 * negative error code is returned.
133 * %-ENOMEM - Insufficient memory available.
135 int nilfs_init_transaction_cache(void)
137 nilfs_transaction_cachep =
138 kmem_cache_create("nilfs2_transaction_cache",
139 sizeof(struct nilfs_transaction_info),
140 0, SLAB_RECLAIM_ACCOUNT, NULL);
141 return (nilfs_transaction_cachep == NULL) ? -ENOMEM : 0;
145 * nilfs_detroy_transaction_cache - destroy the cache for transaction info
147 * nilfs_destroy_transaction_cache() frees the slab cache for the struct
148 * nilfs_transaction_info.
150 void nilfs_destroy_transaction_cache(void)
152 kmem_cache_destroy(nilfs_transaction_cachep);
155 static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
157 struct nilfs_transaction_info *cur_ti = current->journal_info;
161 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
162 return ++cur_ti->ti_count;
165 * If journal_info field is occupied by other FS,
166 * it is saved and will be restored on
167 * nilfs_transaction_commit().
170 "NILFS warning: journal info from a different "
172 save = current->journal_info;
176 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
179 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
185 ti->ti_magic = NILFS_TI_MAGIC;
186 current->journal_info = ti;
191 * nilfs_transaction_begin - start indivisible file operations.
193 * @ti: nilfs_transaction_info
194 * @vacancy_check: flags for vacancy rate checks
196 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
197 * the segment semaphore, to make a segment construction and write tasks
198 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
199 * The region enclosed by these two functions can be nested. To avoid a
200 * deadlock, the semaphore is only acquired or released in the outermost call.
202 * This function allocates a nilfs_transaction_info struct to keep context
203 * information on it. It is initialized and hooked onto the current task in
204 * the outermost call. If a pre-allocated struct is given to @ti, it is used
205 * instead; othewise a new struct is assigned from a slab.
207 * When @vacancy_check flag is set, this function will check the amount of
208 * free space, and will wait for the GC to reclaim disk space if low capacity.
210 * Return Value: On success, 0 is returned. On error, one of the following
211 * negative error code is returned.
213 * %-ENOMEM - Insufficient memory available.
215 * %-ENOSPC - No space left on device
217 int nilfs_transaction_begin(struct super_block *sb,
218 struct nilfs_transaction_info *ti,
221 struct nilfs_sb_info *sbi;
222 struct the_nilfs *nilfs;
223 int ret = nilfs_prepare_segment_lock(ti);
225 if (unlikely(ret < 0))
231 nilfs = sbi->s_nilfs;
232 down_read(&nilfs->ns_segctor_sem);
233 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
234 up_read(&nilfs->ns_segctor_sem);
241 ti = current->journal_info;
242 current->journal_info = ti->ti_save;
243 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
244 kmem_cache_free(nilfs_transaction_cachep, ti);
249 * nilfs_transaction_commit - commit indivisible file operations.
252 * nilfs_transaction_commit() releases the read semaphore which is
253 * acquired by nilfs_transaction_begin(). This is only performed
254 * in outermost call of this function. If a commit flag is set,
255 * nilfs_transaction_commit() sets a timer to start the segment
256 * constructor. If a sync flag is set, it starts construction
259 int nilfs_transaction_commit(struct super_block *sb)
261 struct nilfs_transaction_info *ti = current->journal_info;
262 struct nilfs_sb_info *sbi;
263 struct nilfs_sc_info *sci;
266 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
267 ti->ti_flags |= NILFS_TI_COMMIT;
268 if (ti->ti_count > 0) {
275 if (ti->ti_flags & NILFS_TI_COMMIT)
276 nilfs_segctor_start_timer(sci);
277 if (atomic_read(&sbi->s_nilfs->ns_ndirtyblks) >
279 nilfs_segctor_do_flush(sci, 0);
281 up_read(&sbi->s_nilfs->ns_segctor_sem);
282 current->journal_info = ti->ti_save;
284 if (ti->ti_flags & NILFS_TI_SYNC)
285 err = nilfs_construct_segment(sb);
286 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
287 kmem_cache_free(nilfs_transaction_cachep, ti);
291 void nilfs_transaction_abort(struct super_block *sb)
293 struct nilfs_transaction_info *ti = current->journal_info;
295 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
296 if (ti->ti_count > 0) {
300 up_read(&NILFS_SB(sb)->s_nilfs->ns_segctor_sem);
302 current->journal_info = ti->ti_save;
303 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
304 kmem_cache_free(nilfs_transaction_cachep, ti);
307 void nilfs_relax_pressure_in_lock(struct super_block *sb)
309 struct nilfs_sb_info *sbi = NILFS_SB(sb);
310 struct nilfs_sc_info *sci = NILFS_SC(sbi);
311 struct the_nilfs *nilfs = sbi->s_nilfs;
313 if (!sci || !sci->sc_flush_request)
316 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
317 up_read(&nilfs->ns_segctor_sem);
319 down_write(&nilfs->ns_segctor_sem);
320 if (sci->sc_flush_request &&
321 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
322 struct nilfs_transaction_info *ti = current->journal_info;
324 ti->ti_flags |= NILFS_TI_WRITER;
325 nilfs_segctor_do_immediate_flush(sci);
326 ti->ti_flags &= ~NILFS_TI_WRITER;
328 downgrade_write(&nilfs->ns_segctor_sem);
331 static void nilfs_transaction_lock(struct nilfs_sb_info *sbi,
332 struct nilfs_transaction_info *ti,
335 struct nilfs_transaction_info *cur_ti = current->journal_info;
339 ti->ti_flags = NILFS_TI_WRITER;
341 ti->ti_save = cur_ti;
342 ti->ti_magic = NILFS_TI_MAGIC;
343 INIT_LIST_HEAD(&ti->ti_garbage);
344 current->journal_info = ti;
347 down_write(&sbi->s_nilfs->ns_segctor_sem);
348 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &NILFS_SC(sbi)->sc_flags))
351 nilfs_segctor_do_immediate_flush(NILFS_SC(sbi));
353 up_write(&sbi->s_nilfs->ns_segctor_sem);
357 ti->ti_flags |= NILFS_TI_GC;
360 static void nilfs_transaction_unlock(struct nilfs_sb_info *sbi)
362 struct nilfs_transaction_info *ti = current->journal_info;
364 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
365 BUG_ON(ti->ti_count > 0);
367 up_write(&sbi->s_nilfs->ns_segctor_sem);
368 current->journal_info = ti->ti_save;
369 if (!list_empty(&ti->ti_garbage))
370 nilfs_dispose_list(sbi, &ti->ti_garbage, 0);
373 static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
374 struct nilfs_segsum_pointer *ssp,
377 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
378 unsigned blocksize = sci->sc_super->s_blocksize;
381 if (unlikely(ssp->offset + bytes > blocksize)) {
383 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
384 &segbuf->sb_segsum_buffers));
385 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
387 p = ssp->bh->b_data + ssp->offset;
388 ssp->offset += bytes;
393 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
394 * @sci: nilfs_sc_info
396 static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
398 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
399 struct buffer_head *sumbh;
404 if (nilfs_doing_gc())
406 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime);
410 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
411 sumbytes = segbuf->sb_sum.sumbytes;
412 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
413 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
414 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
418 static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
420 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
421 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
422 return -E2BIG; /* The current segment is filled up
424 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
425 return nilfs_segctor_reset_segment_buffer(sci);
428 static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
430 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
433 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
434 err = nilfs_segctor_feed_segment(sci);
437 segbuf = sci->sc_curseg;
439 err = nilfs_segbuf_extend_payload(segbuf, &sci->sc_super_root);
441 segbuf->sb_sum.flags |= NILFS_SS_SR;
446 * Functions for making segment summary and payloads
448 static int nilfs_segctor_segsum_block_required(
449 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
452 unsigned blocksize = sci->sc_super->s_blocksize;
453 /* Size of finfo and binfo is enough small against blocksize */
455 return ssp->offset + binfo_size +
456 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
460 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
463 sci->sc_curseg->sb_sum.nfinfo++;
464 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
465 nilfs_segctor_map_segsum_entry(
466 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
470 static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
473 struct nilfs_finfo *finfo;
474 struct nilfs_inode_info *ii;
475 struct nilfs_segment_buffer *segbuf;
477 if (sci->sc_blk_cnt == 0)
481 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
483 finfo->fi_ino = cpu_to_le64(inode->i_ino);
484 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
485 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
486 finfo->fi_cno = cpu_to_le64(ii->i_cno);
488 segbuf = sci->sc_curseg;
489 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
490 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
491 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
492 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
495 static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
496 struct buffer_head *bh,
500 struct nilfs_segment_buffer *segbuf;
501 int required, err = 0;
504 segbuf = sci->sc_curseg;
505 required = nilfs_segctor_segsum_block_required(
506 sci, &sci->sc_binfo_ptr, binfo_size);
507 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
508 nilfs_segctor_end_finfo(sci, inode);
509 err = nilfs_segctor_feed_segment(sci);
514 if (unlikely(required)) {
515 err = nilfs_segbuf_extend_segsum(segbuf);
519 if (sci->sc_blk_cnt == 0)
520 nilfs_segctor_begin_finfo(sci, inode);
522 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
523 /* Substitution to vblocknr is delayed until update_blocknr() */
524 nilfs_segbuf_add_file_buffer(segbuf, bh);
530 static int nilfs_handle_bmap_error(int err, const char *fname,
531 struct inode *inode, struct super_block *sb)
533 if (err == -EINVAL) {
534 nilfs_error(sb, fname, "broken bmap (inode=%lu)\n",
542 * Callback functions that enumerate, mark, and collect dirty blocks
544 static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
545 struct buffer_head *bh, struct inode *inode)
549 /* BUG_ON(!buffer_dirty(bh)); */
550 /* excluded by scan_dirty_data_buffers() */
551 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
552 if (unlikely(err < 0))
553 return nilfs_handle_bmap_error(err, __func__, inode,
556 err = nilfs_segctor_add_file_block(sci, bh, inode,
557 sizeof(struct nilfs_binfo_v));
559 sci->sc_datablk_cnt++;
563 static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
564 struct buffer_head *bh,
569 /* BUG_ON(!buffer_dirty(bh)); */
570 /* excluded by scan_dirty_node_buffers() */
571 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
572 if (unlikely(err < 0))
573 return nilfs_handle_bmap_error(err, __func__, inode,
578 static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
579 struct buffer_head *bh,
582 BUG_ON(!buffer_dirty(bh));
583 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
586 static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
587 struct nilfs_segsum_pointer *ssp,
588 union nilfs_binfo *binfo)
590 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
591 sci, ssp, sizeof(*binfo_v));
592 *binfo_v = binfo->bi_v;
595 static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
596 struct nilfs_segsum_pointer *ssp,
597 union nilfs_binfo *binfo)
599 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
600 sci, ssp, sizeof(*vblocknr));
601 *vblocknr = binfo->bi_v.bi_vblocknr;
604 struct nilfs_sc_operations nilfs_sc_file_ops = {
605 .collect_data = nilfs_collect_file_data,
606 .collect_node = nilfs_collect_file_node,
607 .collect_bmap = nilfs_collect_file_bmap,
608 .write_data_binfo = nilfs_write_file_data_binfo,
609 .write_node_binfo = nilfs_write_file_node_binfo,
612 static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
613 struct buffer_head *bh, struct inode *inode)
617 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
618 if (unlikely(err < 0))
619 return nilfs_handle_bmap_error(err, __func__, inode,
622 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
624 sci->sc_datablk_cnt++;
628 static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
629 struct buffer_head *bh, struct inode *inode)
631 BUG_ON(!buffer_dirty(bh));
632 return nilfs_segctor_add_file_block(sci, bh, inode,
633 sizeof(struct nilfs_binfo_dat));
636 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
637 struct nilfs_segsum_pointer *ssp,
638 union nilfs_binfo *binfo)
640 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
642 *blkoff = binfo->bi_dat.bi_blkoff;
645 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
646 struct nilfs_segsum_pointer *ssp,
647 union nilfs_binfo *binfo)
649 struct nilfs_binfo_dat *binfo_dat =
650 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
651 *binfo_dat = binfo->bi_dat;
654 struct nilfs_sc_operations nilfs_sc_dat_ops = {
655 .collect_data = nilfs_collect_dat_data,
656 .collect_node = nilfs_collect_file_node,
657 .collect_bmap = nilfs_collect_dat_bmap,
658 .write_data_binfo = nilfs_write_dat_data_binfo,
659 .write_node_binfo = nilfs_write_dat_node_binfo,
662 struct nilfs_sc_operations nilfs_sc_dsync_ops = {
663 .collect_data = nilfs_collect_file_data,
664 .collect_node = NULL,
665 .collect_bmap = NULL,
666 .write_data_binfo = nilfs_write_file_data_binfo,
667 .write_node_binfo = NULL,
670 static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
671 struct list_head *listp,
673 loff_t start, loff_t end)
675 struct address_space *mapping = inode->i_mapping;
677 pgoff_t index = 0, last = ULONG_MAX;
681 if (unlikely(start != 0 || end != LLONG_MAX)) {
683 * A valid range is given for sync-ing data pages. The
684 * range is rounded to per-page; extra dirty buffers
685 * may be included if blocksize < pagesize.
687 index = start >> PAGE_SHIFT;
688 last = end >> PAGE_SHIFT;
690 pagevec_init(&pvec, 0);
692 if (unlikely(index > last) ||
693 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
694 min_t(pgoff_t, last - index,
695 PAGEVEC_SIZE - 1) + 1))
698 for (i = 0; i < pagevec_count(&pvec); i++) {
699 struct buffer_head *bh, *head;
700 struct page *page = pvec.pages[i];
702 if (unlikely(page->index > last))
707 if (!page_has_buffers(page))
708 create_empty_buffers(page,
709 1 << inode->i_blkbits, 0);
713 bh = head = page_buffers(page);
715 if (!buffer_dirty(bh))
718 list_add_tail(&bh->b_assoc_buffers, listp);
720 if (unlikely(ndirties >= nlimit)) {
721 pagevec_release(&pvec);
725 } while (bh = bh->b_this_page, bh != head);
727 pagevec_release(&pvec);
732 static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
733 struct list_head *listp)
735 struct nilfs_inode_info *ii = NILFS_I(inode);
736 struct address_space *mapping = &ii->i_btnode_cache;
738 struct buffer_head *bh, *head;
742 pagevec_init(&pvec, 0);
744 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
746 for (i = 0; i < pagevec_count(&pvec); i++) {
747 bh = head = page_buffers(pvec.pages[i]);
749 if (buffer_dirty(bh)) {
751 list_add_tail(&bh->b_assoc_buffers,
754 bh = bh->b_this_page;
755 } while (bh != head);
757 pagevec_release(&pvec);
762 static void nilfs_dispose_list(struct nilfs_sb_info *sbi,
763 struct list_head *head, int force)
765 struct nilfs_inode_info *ii, *n;
766 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
769 while (!list_empty(head)) {
770 spin_lock(&sbi->s_inode_lock);
771 list_for_each_entry_safe(ii, n, head, i_dirty) {
772 list_del_init(&ii->i_dirty);
774 if (unlikely(ii->i_bh)) {
778 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
779 set_bit(NILFS_I_QUEUED, &ii->i_state);
780 list_add_tail(&ii->i_dirty,
781 &sbi->s_dirty_files);
785 if (nv == SC_N_INODEVEC)
788 spin_unlock(&sbi->s_inode_lock);
790 for (pii = ivec; nv > 0; pii++, nv--)
791 iput(&(*pii)->vfs_inode);
795 static int nilfs_test_metadata_dirty(struct nilfs_sb_info *sbi)
797 struct the_nilfs *nilfs = sbi->s_nilfs;
800 if (nilfs_mdt_fetch_dirty(sbi->s_ifile))
802 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
804 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
806 if (ret || nilfs_doing_gc())
807 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs)))
812 static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
814 return list_empty(&sci->sc_dirty_files) &&
815 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
816 list_empty(&sci->sc_cleaning_segments) &&
817 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
820 static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
822 struct nilfs_sb_info *sbi = sci->sc_sbi;
825 if (nilfs_test_metadata_dirty(sbi))
826 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
828 spin_lock(&sbi->s_inode_lock);
829 if (list_empty(&sbi->s_dirty_files) && nilfs_segctor_clean(sci))
832 spin_unlock(&sbi->s_inode_lock);
836 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
838 struct nilfs_sb_info *sbi = sci->sc_sbi;
839 struct the_nilfs *nilfs = sbi->s_nilfs;
841 nilfs_mdt_clear_dirty(sbi->s_ifile);
842 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
843 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
844 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs));
847 static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
849 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
850 struct buffer_head *bh_cp;
851 struct nilfs_checkpoint *raw_cp;
854 /* XXX: this interface will be changed */
855 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
858 /* The following code is duplicated with cpfile. But, it is
859 needed to collect the checkpoint even if it was not newly
861 nilfs_mdt_mark_buffer_dirty(bh_cp);
862 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
863 nilfs_cpfile_put_checkpoint(
864 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
866 BUG_ON(err == -EINVAL || err == -ENOENT);
871 static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
873 struct nilfs_sb_info *sbi = sci->sc_sbi;
874 struct the_nilfs *nilfs = sbi->s_nilfs;
875 struct buffer_head *bh_cp;
876 struct nilfs_checkpoint *raw_cp;
879 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
882 BUG_ON(err == -EINVAL || err == -ENOENT);
885 raw_cp->cp_snapshot_list.ssl_next = 0;
886 raw_cp->cp_snapshot_list.ssl_prev = 0;
887 raw_cp->cp_inodes_count =
888 cpu_to_le64(atomic_read(&sbi->s_inodes_count));
889 raw_cp->cp_blocks_count =
890 cpu_to_le64(atomic_read(&sbi->s_blocks_count));
891 raw_cp->cp_nblk_inc =
892 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
893 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
894 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
895 if (sci->sc_sketch_inode && i_size_read(sci->sc_sketch_inode) > 0)
896 nilfs_checkpoint_set_sketch(raw_cp);
897 nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
898 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
905 static void nilfs_fill_in_file_bmap(struct inode *ifile,
906 struct nilfs_inode_info *ii)
909 struct buffer_head *ibh;
910 struct nilfs_inode *raw_inode;
912 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
915 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
917 nilfs_bmap_write(ii->i_bmap, raw_inode);
918 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
922 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
925 struct nilfs_inode_info *ii;
927 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
928 nilfs_fill_in_file_bmap(ifile, ii);
929 set_bit(NILFS_I_COLLECTED, &ii->i_state);
931 if (sci->sc_sketch_inode) {
932 ii = NILFS_I(sci->sc_sketch_inode);
933 if (test_bit(NILFS_I_DIRTY, &ii->i_state))
934 nilfs_fill_in_file_bmap(ifile, ii);
939 * CRC calculation routines
941 static void nilfs_fill_in_super_root_crc(struct buffer_head *bh_sr, u32 seed)
943 struct nilfs_super_root *raw_sr =
944 (struct nilfs_super_root *)bh_sr->b_data;
947 BUG_ON(NILFS_SR_BYTES > bh_sr->b_size);
949 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
950 NILFS_SR_BYTES - sizeof(raw_sr->sr_sum));
951 raw_sr->sr_sum = cpu_to_le32(crc);
954 static void nilfs_segctor_fill_in_checksums(struct nilfs_sc_info *sci,
957 struct nilfs_segment_buffer *segbuf;
959 if (sci->sc_super_root)
960 nilfs_fill_in_super_root_crc(sci->sc_super_root, seed);
962 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
963 nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
964 nilfs_segbuf_fill_in_data_crc(segbuf, seed);
968 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
969 struct the_nilfs *nilfs)
971 struct buffer_head *bh_sr = sci->sc_super_root;
972 struct nilfs_super_root *raw_sr =
973 (struct nilfs_super_root *)bh_sr->b_data;
974 unsigned isz = nilfs->ns_inode_size;
976 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
977 raw_sr->sr_nongc_ctime
978 = cpu_to_le64(nilfs_doing_gc() ?
979 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
980 raw_sr->sr_flags = 0;
982 nilfs_mdt_write_inode_direct(
983 nilfs_dat_inode(nilfs), bh_sr, NILFS_SR_DAT_OFFSET(isz));
984 nilfs_mdt_write_inode_direct(
985 nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(isz));
986 nilfs_mdt_write_inode_direct(
987 nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(isz));
990 static void nilfs_redirty_inodes(struct list_head *head)
992 struct nilfs_inode_info *ii;
994 list_for_each_entry(ii, head, i_dirty) {
995 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
996 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
1000 static void nilfs_drop_collected_inodes(struct list_head *head)
1002 struct nilfs_inode_info *ii;
1004 list_for_each_entry(ii, head, i_dirty) {
1005 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
1008 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
1009 set_bit(NILFS_I_UPDATED, &ii->i_state);
1013 static void nilfs_segctor_cancel_free_segments(struct nilfs_sc_info *sci,
1014 struct inode *sufile)
1017 struct list_head *head = &sci->sc_cleaning_segments;
1018 struct nilfs_segment_entry *ent;
1021 list_for_each_entry(ent, head, list) {
1022 if (!(ent->flags & NILFS_SLH_FREED))
1024 err = nilfs_sufile_cancel_free(sufile, ent->segnum);
1027 ent->flags &= ~NILFS_SLH_FREED;
1031 static int nilfs_segctor_prepare_free_segments(struct nilfs_sc_info *sci,
1032 struct inode *sufile)
1034 struct list_head *head = &sci->sc_cleaning_segments;
1035 struct nilfs_segment_entry *ent;
1038 list_for_each_entry(ent, head, list) {
1039 err = nilfs_sufile_free(sufile, ent->segnum);
1042 ent->flags |= NILFS_SLH_FREED;
1047 static void nilfs_segctor_commit_free_segments(struct nilfs_sc_info *sci)
1049 nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
1052 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1053 struct inode *inode,
1054 struct list_head *listp,
1055 int (*collect)(struct nilfs_sc_info *,
1056 struct buffer_head *,
1059 struct buffer_head *bh, *n;
1063 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1064 list_del_init(&bh->b_assoc_buffers);
1065 err = collect(sci, bh, inode);
1068 goto dispose_buffers;
1074 while (!list_empty(listp)) {
1075 bh = list_entry(listp->next, struct buffer_head,
1077 list_del_init(&bh->b_assoc_buffers);
1083 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1085 /* Remaining number of blocks within segment buffer */
1086 return sci->sc_segbuf_nblocks -
1087 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1090 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1091 struct inode *inode,
1092 struct nilfs_sc_operations *sc_ops)
1094 LIST_HEAD(data_buffers);
1095 LIST_HEAD(node_buffers);
1098 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1099 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1101 n = nilfs_lookup_dirty_data_buffers(
1102 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1104 err = nilfs_segctor_apply_buffers(
1105 sci, inode, &data_buffers,
1106 sc_ops->collect_data);
1107 BUG_ON(!err); /* always receive -E2BIG or true error */
1111 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1113 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1114 err = nilfs_segctor_apply_buffers(
1115 sci, inode, &data_buffers, sc_ops->collect_data);
1116 if (unlikely(err)) {
1117 /* dispose node list */
1118 nilfs_segctor_apply_buffers(
1119 sci, inode, &node_buffers, NULL);
1122 sci->sc_stage.flags |= NILFS_CF_NODE;
1125 err = nilfs_segctor_apply_buffers(
1126 sci, inode, &node_buffers, sc_ops->collect_node);
1130 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1131 err = nilfs_segctor_apply_buffers(
1132 sci, inode, &node_buffers, sc_ops->collect_bmap);
1136 nilfs_segctor_end_finfo(sci, inode);
1137 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1143 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1144 struct inode *inode)
1146 LIST_HEAD(data_buffers);
1147 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1150 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1151 sci->sc_dsync_start,
1154 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1155 nilfs_collect_file_data);
1157 nilfs_segctor_end_finfo(sci, inode);
1159 /* always receive -E2BIG or true error if n > rest */
1164 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1166 struct nilfs_sb_info *sbi = sci->sc_sbi;
1167 struct the_nilfs *nilfs = sbi->s_nilfs;
1168 struct list_head *head;
1169 struct nilfs_inode_info *ii;
1172 switch (sci->sc_stage.scnt) {
1175 sci->sc_stage.flags = 0;
1177 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1178 sci->sc_nblk_inc = 0;
1179 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1180 if (mode == SC_LSEG_DSYNC) {
1181 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1186 sci->sc_stage.dirty_file_ptr = NULL;
1187 sci->sc_stage.gc_inode_ptr = NULL;
1188 if (mode == SC_FLUSH_DAT) {
1189 sci->sc_stage.scnt = NILFS_ST_DAT;
1192 sci->sc_stage.scnt++; /* Fall through */
1194 if (nilfs_doing_gc()) {
1195 head = &sci->sc_gc_inodes;
1196 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1198 list_for_each_entry_continue(ii, head, i_dirty) {
1199 err = nilfs_segctor_scan_file(
1200 sci, &ii->vfs_inode,
1201 &nilfs_sc_file_ops);
1202 if (unlikely(err)) {
1203 sci->sc_stage.gc_inode_ptr = list_entry(
1205 struct nilfs_inode_info,
1209 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1211 sci->sc_stage.gc_inode_ptr = NULL;
1213 sci->sc_stage.scnt++; /* Fall through */
1215 head = &sci->sc_dirty_files;
1216 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1218 list_for_each_entry_continue(ii, head, i_dirty) {
1219 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1221 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1222 &nilfs_sc_file_ops);
1223 if (unlikely(err)) {
1224 sci->sc_stage.dirty_file_ptr =
1225 list_entry(ii->i_dirty.prev,
1226 struct nilfs_inode_info,
1230 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1231 /* XXX: required ? */
1233 sci->sc_stage.dirty_file_ptr = NULL;
1234 if (mode == SC_FLUSH_FILE) {
1235 sci->sc_stage.scnt = NILFS_ST_DONE;
1238 sci->sc_stage.scnt++; /* Fall through */
1239 case NILFS_ST_SKETCH:
1240 if (mode == SC_LSEG_SR && sci->sc_sketch_inode) {
1241 ii = NILFS_I(sci->sc_sketch_inode);
1242 if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
1243 sci->sc_sketch_inode->i_ctime.tv_sec
1244 = sci->sc_seg_ctime;
1245 sci->sc_sketch_inode->i_mtime.tv_sec
1246 = sci->sc_seg_ctime;
1247 err = nilfs_mark_inode_dirty(
1248 sci->sc_sketch_inode);
1252 err = nilfs_segctor_scan_file(sci,
1253 sci->sc_sketch_inode,
1254 &nilfs_sc_file_ops);
1258 sci->sc_stage.scnt++;
1259 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1261 case NILFS_ST_IFILE:
1262 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1263 &nilfs_sc_file_ops);
1266 sci->sc_stage.scnt++;
1267 /* Creating a checkpoint */
1268 err = nilfs_segctor_create_checkpoint(sci);
1272 case NILFS_ST_CPFILE:
1273 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1274 &nilfs_sc_file_ops);
1277 sci->sc_stage.scnt++; /* Fall through */
1278 case NILFS_ST_SUFILE:
1279 err = nilfs_segctor_prepare_free_segments(sci,
1283 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1284 &nilfs_sc_file_ops);
1287 sci->sc_stage.scnt++; /* Fall through */
1290 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1294 if (mode == SC_FLUSH_DAT) {
1295 sci->sc_stage.scnt = NILFS_ST_DONE;
1298 sci->sc_stage.scnt++; /* Fall through */
1300 if (mode == SC_LSEG_SR) {
1301 /* Appending a super root */
1302 err = nilfs_segctor_add_super_root(sci);
1306 /* End of a logical segment */
1307 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1308 sci->sc_stage.scnt = NILFS_ST_DONE;
1310 case NILFS_ST_DSYNC:
1312 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
1313 ii = sci->sc_dsync_inode;
1314 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1317 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1320 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1321 sci->sc_stage.scnt = NILFS_ST_DONE;
1333 static int nilfs_segctor_terminate_segment(struct nilfs_sc_info *sci,
1334 struct nilfs_segment_buffer *segbuf,
1335 struct inode *sufile)
1337 struct nilfs_segment_entry *ent = segbuf->sb_segent;
1340 err = nilfs_open_segment_entry(ent, sufile);
1343 nilfs_mdt_mark_buffer_dirty(ent->bh_su);
1344 nilfs_mdt_mark_dirty(sufile);
1345 nilfs_close_segment_entry(ent, sufile);
1347 list_add_tail(&ent->list, &sci->sc_active_segments);
1348 segbuf->sb_segent = NULL;
1352 static int nilfs_touch_segusage(struct inode *sufile, __u64 segnum)
1354 struct buffer_head *bh_su;
1355 struct nilfs_segment_usage *raw_su;
1358 err = nilfs_sufile_get_segment_usage(sufile, segnum, &raw_su, &bh_su);
1361 nilfs_mdt_mark_buffer_dirty(bh_su);
1362 nilfs_mdt_mark_dirty(sufile);
1363 nilfs_sufile_put_segment_usage(sufile, segnum, bh_su);
1367 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1368 struct the_nilfs *nilfs)
1370 struct nilfs_segment_buffer *segbuf, *n;
1371 struct inode *sufile = nilfs->ns_sufile;
1375 if (list_empty(&sci->sc_segbufs)) {
1376 segbuf = nilfs_segbuf_new(sci->sc_super);
1377 if (unlikely(!segbuf))
1379 list_add(&segbuf->sb_list, &sci->sc_segbufs);
1381 segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1383 err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1384 nilfs->ns_pseg_offset, nilfs);
1388 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1389 err = nilfs_segctor_terminate_segment(sci, segbuf, sufile);
1393 nilfs_shift_to_next_segment(nilfs);
1394 err = nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1396 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
1398 err = nilfs_touch_segusage(sufile, segbuf->sb_segnum);
1402 if (nilfs->ns_segnum == nilfs->ns_nextnum) {
1403 /* Start from the head of a new full segment */
1404 err = nilfs_sufile_alloc(sufile, &nextnum);
1408 nextnum = nilfs->ns_nextnum;
1410 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1411 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1413 /* truncating segment buffers */
1414 list_for_each_entry_safe_continue(segbuf, n, &sci->sc_segbufs,
1416 list_del_init(&segbuf->sb_list);
1417 nilfs_segbuf_free(segbuf);
1422 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1423 struct the_nilfs *nilfs, int nadd)
1425 struct nilfs_segment_buffer *segbuf, *prev, *n;
1426 struct inode *sufile = nilfs->ns_sufile;
1431 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1433 * Since the segment specified with nextnum might be allocated during
1434 * the previous construction, the buffer including its segusage may
1435 * not be dirty. The following call ensures that the buffer is dirty
1436 * and will pin the buffer on memory until the sufile is written.
1438 err = nilfs_touch_segusage(sufile, prev->sb_nextnum);
1442 for (i = 0; i < nadd; i++) {
1443 /* extend segment info */
1445 segbuf = nilfs_segbuf_new(sci->sc_super);
1446 if (unlikely(!segbuf))
1449 /* map this buffer to region of segment on-disk */
1450 err = nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1454 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1456 /* allocate the next next full segment */
1457 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1461 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1462 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1464 list_add_tail(&segbuf->sb_list, &list);
1467 list_splice(&list, sci->sc_segbufs.prev);
1471 nilfs_segbuf_free(segbuf);
1473 list_for_each_entry_safe(segbuf, n, &list, sb_list) {
1474 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1476 list_del_init(&segbuf->sb_list);
1477 nilfs_segbuf_free(segbuf);
1482 static void nilfs_segctor_free_incomplete_segments(struct nilfs_sc_info *sci,
1483 struct the_nilfs *nilfs)
1485 struct nilfs_segment_buffer *segbuf;
1488 segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1489 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
1490 ret = nilfs_sufile_free(nilfs->ns_sufile, segbuf->sb_nextnum);
1493 if (segbuf->sb_io_error) {
1494 /* Case 1: The first segment failed */
1495 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1496 /* Case 1a: Partial segment appended into an existing
1498 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1499 segbuf->sb_fseg_end);
1500 else /* Case 1b: New full segment */
1501 set_nilfs_discontinued(nilfs);
1505 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1506 ret = nilfs_sufile_free(nilfs->ns_sufile, segbuf->sb_nextnum);
1508 if (!done && segbuf->sb_io_error) {
1509 if (segbuf->sb_segnum != nilfs->ns_nextnum)
1510 /* Case 2: extended segment (!= next) failed */
1511 nilfs_sufile_set_error(nilfs->ns_sufile,
1518 static void nilfs_segctor_clear_segment_buffers(struct nilfs_sc_info *sci)
1520 struct nilfs_segment_buffer *segbuf;
1522 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list)
1523 nilfs_segbuf_clear(segbuf);
1524 sci->sc_super_root = NULL;
1527 static void nilfs_segctor_destroy_segment_buffers(struct nilfs_sc_info *sci)
1529 struct nilfs_segment_buffer *segbuf;
1531 while (!list_empty(&sci->sc_segbufs)) {
1532 segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1533 list_del_init(&segbuf->sb_list);
1534 nilfs_segbuf_free(segbuf);
1536 /* sci->sc_curseg = NULL; */
1539 static void nilfs_segctor_end_construction(struct nilfs_sc_info *sci,
1540 struct the_nilfs *nilfs, int err)
1542 if (unlikely(err)) {
1543 nilfs_segctor_free_incomplete_segments(sci, nilfs);
1544 nilfs_segctor_cancel_free_segments(sci, nilfs->ns_sufile);
1546 nilfs_segctor_clear_segment_buffers(sci);
1549 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1550 struct inode *sufile)
1552 struct nilfs_segment_buffer *segbuf;
1553 struct buffer_head *bh_su;
1554 struct nilfs_segment_usage *raw_su;
1555 unsigned long live_blocks;
1558 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1559 ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
1561 BUG_ON(ret); /* always succeed because bh_su is dirty */
1562 live_blocks = segbuf->sb_sum.nblocks +
1563 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
1564 raw_su->su_lastmod = cpu_to_le64(sci->sc_seg_ctime);
1565 raw_su->su_nblocks = cpu_to_le32(live_blocks);
1566 nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum,
1571 static void nilfs_segctor_cancel_segusage(struct nilfs_sc_info *sci,
1572 struct inode *sufile)
1574 struct nilfs_segment_buffer *segbuf;
1575 struct buffer_head *bh_su;
1576 struct nilfs_segment_usage *raw_su;
1579 segbuf = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1580 ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
1582 BUG_ON(ret); /* always succeed because bh_su is dirty */
1583 raw_su->su_nblocks = cpu_to_le32(segbuf->sb_pseg_start -
1584 segbuf->sb_fseg_start);
1585 nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum, bh_su);
1587 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1588 ret = nilfs_sufile_get_segment_usage(sufile, segbuf->sb_segnum,
1590 BUG_ON(ret); /* always succeed */
1591 raw_su->su_nblocks = 0;
1592 nilfs_sufile_put_segment_usage(sufile, segbuf->sb_segnum,
1597 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1598 struct nilfs_segment_buffer *last,
1599 struct inode *sufile)
1601 struct nilfs_segment_buffer *segbuf = last, *n;
1604 list_for_each_entry_safe_continue(segbuf, n, &sci->sc_segbufs,
1606 list_del_init(&segbuf->sb_list);
1607 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1608 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1610 nilfs_segbuf_free(segbuf);
1615 static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1616 struct the_nilfs *nilfs, int mode)
1618 struct nilfs_cstage prev_stage = sci->sc_stage;
1621 /* Collection retry loop */
1623 sci->sc_super_root = NULL;
1624 sci->sc_nblk_this_inc = 0;
1625 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1627 err = nilfs_segctor_reset_segment_buffer(sci);
1631 err = nilfs_segctor_collect_blocks(sci, mode);
1632 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1636 if (unlikely(err != -E2BIG))
1639 /* The current segment is filled up */
1640 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1643 nilfs_segctor_cancel_free_segments(sci, nilfs->ns_sufile);
1644 nilfs_segctor_clear_segment_buffers(sci);
1646 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1650 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1651 sci->sc_stage = prev_stage;
1653 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1660 static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1661 struct buffer_head *new_bh)
1663 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1665 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1666 /* The caller must release old_bh */
1670 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1671 struct nilfs_segment_buffer *segbuf,
1674 struct inode *inode = NULL;
1676 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1677 unsigned long nblocks = 0, ndatablk = 0;
1678 struct nilfs_sc_operations *sc_op = NULL;
1679 struct nilfs_segsum_pointer ssp;
1680 struct nilfs_finfo *finfo = NULL;
1681 union nilfs_binfo binfo;
1682 struct buffer_head *bh, *bh_org;
1689 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1690 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1691 ssp.offset = sizeof(struct nilfs_segment_summary);
1693 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1694 if (bh == sci->sc_super_root)
1697 finfo = nilfs_segctor_map_segsum_entry(
1698 sci, &ssp, sizeof(*finfo));
1699 ino = le64_to_cpu(finfo->fi_ino);
1700 nblocks = le32_to_cpu(finfo->fi_nblocks);
1701 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1703 if (buffer_nilfs_node(bh))
1704 inode = NILFS_BTNC_I(bh->b_page->mapping);
1706 inode = NILFS_AS_I(bh->b_page->mapping);
1708 if (mode == SC_LSEG_DSYNC)
1709 sc_op = &nilfs_sc_dsync_ops;
1710 else if (ino == NILFS_DAT_INO)
1711 sc_op = &nilfs_sc_dat_ops;
1712 else /* file blocks */
1713 sc_op = &nilfs_sc_file_ops;
1717 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1720 nilfs_list_replace_buffer(bh_org, bh);
1726 sc_op->write_data_binfo(sci, &ssp, &binfo);
1728 sc_op->write_node_binfo(sci, &ssp, &binfo);
1731 if (--nblocks == 0) {
1735 } else if (ndatablk > 0)
1742 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1746 static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1748 struct nilfs_segment_buffer *segbuf;
1751 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1752 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1755 nilfs_segbuf_fill_in_segsum(segbuf);
1761 nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1763 struct page *clone_page;
1764 struct buffer_head *bh, *head, *bh2;
1767 bh = head = page_buffers(page);
1769 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1770 if (unlikely(!clone_page))
1773 bh2 = page_buffers(clone_page);
1774 kaddr = kmap_atomic(page, KM_USER0);
1776 if (list_empty(&bh->b_assoc_buffers))
1779 page_cache_get(clone_page); /* for each bh */
1780 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1781 bh2->b_blocknr = bh->b_blocknr;
1782 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1783 list_add_tail(&bh->b_assoc_buffers, out);
1784 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1785 kunmap_atomic(kaddr, KM_USER0);
1787 if (!TestSetPageWriteback(clone_page))
1788 inc_zone_page_state(clone_page, NR_WRITEBACK);
1789 unlock_page(clone_page);
1794 static int nilfs_test_page_to_be_frozen(struct page *page)
1796 struct address_space *mapping = page->mapping;
1798 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1801 if (page_mapped(page)) {
1802 ClearPageChecked(page);
1805 return PageChecked(page);
1808 static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1810 if (!page || PageWriteback(page))
1811 /* For split b-tree node pages, this function may be called
1812 twice. We ignore the 2nd or later calls by this check. */
1816 clear_page_dirty_for_io(page);
1817 set_page_writeback(page);
1820 if (nilfs_test_page_to_be_frozen(page)) {
1821 int err = nilfs_copy_replace_page_buffers(page, out);
1828 static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1829 struct page **failed_page)
1831 struct nilfs_segment_buffer *segbuf;
1832 struct page *bd_page = NULL, *fs_page = NULL;
1833 struct list_head *list = &sci->sc_copied_buffers;
1836 *failed_page = NULL;
1837 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1838 struct buffer_head *bh;
1840 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1842 if (bh->b_page != bd_page) {
1845 clear_page_dirty_for_io(bd_page);
1846 set_page_writeback(bd_page);
1847 unlock_page(bd_page);
1849 bd_page = bh->b_page;
1853 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1855 if (bh == sci->sc_super_root) {
1856 if (bh->b_page != bd_page) {
1858 clear_page_dirty_for_io(bd_page);
1859 set_page_writeback(bd_page);
1860 unlock_page(bd_page);
1861 bd_page = bh->b_page;
1865 if (bh->b_page != fs_page) {
1866 err = nilfs_begin_page_io(fs_page, list);
1867 if (unlikely(err)) {
1868 *failed_page = fs_page;
1871 fs_page = bh->b_page;
1877 clear_page_dirty_for_io(bd_page);
1878 set_page_writeback(bd_page);
1879 unlock_page(bd_page);
1881 err = nilfs_begin_page_io(fs_page, list);
1883 *failed_page = fs_page;
1888 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
1889 struct backing_dev_info *bdi)
1891 struct nilfs_segment_buffer *segbuf;
1892 struct nilfs_write_info wi;
1895 wi.sb = sci->sc_super;
1896 wi.bh_sr = sci->sc_super_root;
1899 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1900 nilfs_segbuf_prepare_write(segbuf, &wi);
1901 err = nilfs_segbuf_write(segbuf, &wi);
1903 res = nilfs_segbuf_wait(segbuf, &wi);
1904 err = unlikely(err) ? : res;
1911 static int nilfs_page_has_uncleared_buffer(struct page *page)
1913 struct buffer_head *head, *bh;
1915 head = bh = page_buffers(page);
1917 if (buffer_dirty(bh) && !list_empty(&bh->b_assoc_buffers))
1919 bh = bh->b_this_page;
1920 } while (bh != head);
1924 static void __nilfs_end_page_io(struct page *page, int err)
1926 /* BUG_ON(err > 0); */
1928 if (!nilfs_page_buffers_clean(page))
1929 __set_page_dirty_nobuffers(page);
1930 ClearPageError(page);
1932 __set_page_dirty_nobuffers(page);
1936 if (buffer_nilfs_allocated(page_buffers(page))) {
1937 if (TestClearPageWriteback(page))
1938 dec_zone_page_state(page, NR_WRITEBACK);
1940 end_page_writeback(page);
1943 static void nilfs_end_page_io(struct page *page, int err)
1948 if (buffer_nilfs_node(page_buffers(page)) &&
1949 nilfs_page_has_uncleared_buffer(page))
1950 /* For b-tree node pages, this function may be called twice
1951 or more because they might be split in a segment.
1952 This check assures that cleanup has been done for all
1953 buffers in a split btnode page. */
1956 __nilfs_end_page_io(page, err);
1959 static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1961 struct buffer_head *bh, *head;
1964 while (!list_empty(list)) {
1965 bh = list_entry(list->next, struct buffer_head,
1968 page_cache_get(page);
1969 head = bh = page_buffers(page);
1971 if (!list_empty(&bh->b_assoc_buffers)) {
1972 list_del_init(&bh->b_assoc_buffers);
1974 set_buffer_uptodate(bh);
1975 clear_buffer_dirty(bh);
1976 clear_buffer_nilfs_volatile(bh);
1978 brelse(bh); /* for b_assoc_buffers */
1980 } while ((bh = bh->b_this_page) != head);
1982 __nilfs_end_page_io(page, err);
1983 page_cache_release(page);
1987 static void nilfs_segctor_abort_write(struct nilfs_sc_info *sci,
1988 struct page *failed_page, int err)
1990 struct nilfs_segment_buffer *segbuf;
1991 struct page *bd_page = NULL, *fs_page = NULL;
1993 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1994 struct buffer_head *bh;
1996 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1998 if (bh->b_page != bd_page) {
2000 end_page_writeback(bd_page);
2001 bd_page = bh->b_page;
2005 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
2007 if (bh == sci->sc_super_root) {
2008 if (bh->b_page != bd_page) {
2009 end_page_writeback(bd_page);
2010 bd_page = bh->b_page;
2014 if (bh->b_page != fs_page) {
2015 nilfs_end_page_io(fs_page, err);
2016 if (unlikely(fs_page == failed_page))
2018 fs_page = bh->b_page;
2023 end_page_writeback(bd_page);
2025 nilfs_end_page_io(fs_page, err);
2027 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
2030 static void nilfs_set_next_segment(struct the_nilfs *nilfs,
2031 struct nilfs_segment_buffer *segbuf)
2033 nilfs->ns_segnum = segbuf->sb_segnum;
2034 nilfs->ns_nextnum = segbuf->sb_nextnum;
2035 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
2036 + segbuf->sb_sum.nblocks;
2037 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
2038 nilfs->ns_ctime = segbuf->sb_sum.ctime;
2041 static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
2043 struct nilfs_segment_buffer *segbuf;
2044 struct page *bd_page = NULL, *fs_page = NULL;
2045 struct nilfs_sb_info *sbi = sci->sc_sbi;
2046 struct the_nilfs *nilfs = sbi->s_nilfs;
2047 int update_sr = (sci->sc_super_root != NULL);
2049 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
2050 struct buffer_head *bh;
2052 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
2054 set_buffer_uptodate(bh);
2055 clear_buffer_dirty(bh);
2056 if (bh->b_page != bd_page) {
2058 end_page_writeback(bd_page);
2059 bd_page = bh->b_page;
2063 * We assume that the buffers which belong to the same page
2064 * continue over the buffer list.
2065 * Under this assumption, the last BHs of pages is
2066 * identifiable by the discontinuity of bh->b_page
2067 * (page != fs_page).
2069 * For B-tree node blocks, however, this assumption is not
2070 * guaranteed. The cleanup code of B-tree node pages needs
2073 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
2075 set_buffer_uptodate(bh);
2076 clear_buffer_dirty(bh);
2077 clear_buffer_nilfs_volatile(bh);
2078 if (bh == sci->sc_super_root) {
2079 if (bh->b_page != bd_page) {
2080 end_page_writeback(bd_page);
2081 bd_page = bh->b_page;
2085 if (bh->b_page != fs_page) {
2086 nilfs_end_page_io(fs_page, 0);
2087 fs_page = bh->b_page;
2091 if (!NILFS_SEG_SIMPLEX(&segbuf->sb_sum)) {
2092 if (NILFS_SEG_LOGBGN(&segbuf->sb_sum)) {
2093 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
2094 sci->sc_lseg_stime = jiffies;
2096 if (NILFS_SEG_LOGEND(&segbuf->sb_sum))
2097 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
2101 * Since pages may continue over multiple segment buffers,
2102 * end of the last page must be checked outside of the loop.
2105 end_page_writeback(bd_page);
2107 nilfs_end_page_io(fs_page, 0);
2109 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
2111 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
2113 if (nilfs_doing_gc()) {
2114 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
2116 nilfs_commit_gcdat_inode(nilfs);
2118 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
2120 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
2122 segbuf = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
2123 nilfs_set_next_segment(nilfs, segbuf);
2126 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
2127 segbuf->sb_sum.seg_seq, nilfs->ns_cno);
2129 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2130 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2132 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2135 static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
2136 struct nilfs_sb_info *sbi)
2138 struct nilfs_inode_info *ii, *n;
2139 __u64 cno = sbi->s_nilfs->ns_cno;
2141 spin_lock(&sbi->s_inode_lock);
2143 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
2145 struct buffer_head *ibh;
2148 spin_unlock(&sbi->s_inode_lock);
2149 err = nilfs_ifile_get_inode_block(
2150 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
2151 if (unlikely(err)) {
2152 nilfs_warning(sbi->s_super, __func__,
2153 "failed to get inode block.\n");
2156 nilfs_mdt_mark_buffer_dirty(ibh);
2157 nilfs_mdt_mark_dirty(sbi->s_ifile);
2158 spin_lock(&sbi->s_inode_lock);
2159 if (likely(!ii->i_bh))
2167 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2168 set_bit(NILFS_I_BUSY, &ii->i_state);
2169 list_del(&ii->i_dirty);
2170 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2172 spin_unlock(&sbi->s_inode_lock);
2174 NILFS_I(sbi->s_ifile)->i_cno = cno;
2179 static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2180 struct nilfs_sb_info *sbi)
2182 struct nilfs_transaction_info *ti = current->journal_info;
2183 struct nilfs_inode_info *ii, *n;
2184 __u64 cno = sbi->s_nilfs->ns_cno;
2186 spin_lock(&sbi->s_inode_lock);
2187 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2188 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2189 test_bit(NILFS_I_DIRTY, &ii->i_state)) {
2190 /* The current checkpoint number (=nilfs->ns_cno) is
2191 changed between check-in and check-out only if the
2192 super root is written out. So, we can update i_cno
2193 for the inodes that remain in the dirty list. */
2197 clear_bit(NILFS_I_BUSY, &ii->i_state);
2200 list_del(&ii->i_dirty);
2201 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2203 spin_unlock(&sbi->s_inode_lock);
2207 * Nasty routines to manipulate active flags on sufile.
2208 * These would be removed in a future release.
2210 static void nilfs_segctor_reactivate_segments(struct nilfs_sc_info *sci,
2211 struct the_nilfs *nilfs)
2213 struct nilfs_segment_buffer *segbuf, *last;
2214 struct nilfs_segment_entry *ent, *n;
2215 struct inode *sufile = nilfs->ns_sufile;
2216 struct list_head *head;
2218 last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
2219 nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
2220 ent = segbuf->sb_segent;
2222 break; /* ignore unmapped segments (should check it?)*/
2223 nilfs_segment_usage_set_active(ent->raw_su);
2224 nilfs_close_segment_entry(ent, sufile);
2227 head = &sci->sc_active_segments;
2228 list_for_each_entry_safe(ent, n, head, list) {
2229 nilfs_segment_usage_set_active(ent->raw_su);
2230 nilfs_close_segment_entry(ent, sufile);
2233 down_write(&nilfs->ns_sem);
2234 head = &nilfs->ns_used_segments;
2235 list_for_each_entry(ent, head, list) {
2236 nilfs_segment_usage_set_volatile_active(ent->raw_su);
2238 up_write(&nilfs->ns_sem);
2241 static int nilfs_segctor_deactivate_segments(struct nilfs_sc_info *sci,
2242 struct the_nilfs *nilfs)
2244 struct nilfs_segment_buffer *segbuf, *last;
2245 struct nilfs_segment_entry *ent;
2246 struct inode *sufile = nilfs->ns_sufile;
2247 struct list_head *head;
2250 last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
2251 nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
2253 * Deactivate ongoing full segments. The last segment is kept
2254 * active because it is a start point of recovery, and is not
2255 * relocatable until the super block points to a newer
2258 ent = segbuf->sb_segent;
2260 break; /* ignore unmapped segments (should check it?)*/
2261 err = nilfs_open_segment_entry(ent, sufile);
2264 nilfs_segment_usage_clear_active(ent->raw_su);
2265 BUG_ON(!buffer_dirty(ent->bh_su));
2268 head = &sci->sc_active_segments;
2269 list_for_each_entry(ent, head, list) {
2270 err = nilfs_open_segment_entry(ent, sufile);
2273 nilfs_segment_usage_clear_active(ent->raw_su);
2274 BUG_ON(!buffer_dirty(ent->bh_su));
2277 down_write(&nilfs->ns_sem);
2278 head = &nilfs->ns_used_segments;
2279 list_for_each_entry(ent, head, list) {
2280 /* clear volatile active for segments of older generations */
2281 nilfs_segment_usage_clear_volatile_active(ent->raw_su);
2283 up_write(&nilfs->ns_sem);
2287 nilfs_segctor_reactivate_segments(sci, nilfs);
2291 static void nilfs_segctor_bead_completed_segments(struct nilfs_sc_info *sci)
2293 struct nilfs_segment_buffer *segbuf, *last;
2294 struct nilfs_segment_entry *ent;
2296 /* move each segbuf->sb_segent to the list of used active segments */
2297 last = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
2298 nilfs_for_each_segbuf_before(segbuf, last, &sci->sc_segbufs) {
2299 ent = segbuf->sb_segent;
2301 break; /* ignore unmapped segments (should check it?)*/
2302 list_add_tail(&ent->list, &sci->sc_active_segments);
2303 segbuf->sb_segent = NULL;
2308 __nilfs_segctor_commit_deactivate_segments(struct nilfs_sc_info *sci,
2309 struct the_nilfs *nilfs)
2312 struct nilfs_segment_entry *ent;
2314 list_splice_init(&sci->sc_active_segments,
2315 nilfs->ns_used_segments.prev);
2317 list_for_each_entry(ent, &nilfs->ns_used_segments, list) {
2318 nilfs_segment_usage_set_volatile_active(ent->raw_su);
2319 /* These segments are kept open */
2324 * Main procedure of segment constructor
2326 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2328 struct nilfs_sb_info *sbi = sci->sc_sbi;
2329 struct the_nilfs *nilfs = sbi->s_nilfs;
2330 struct page *failed_page;
2331 int err, has_sr = 0;
2333 sci->sc_stage.scnt = NILFS_ST_INIT;
2335 err = nilfs_segctor_check_in_files(sci, sbi);
2339 if (nilfs_test_metadata_dirty(sbi))
2340 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2342 if (nilfs_segctor_clean(sci))
2346 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2348 err = nilfs_segctor_begin_construction(sci, nilfs);
2352 /* Update time stamp */
2353 sci->sc_seg_ctime = get_seconds();
2355 err = nilfs_segctor_collect(sci, nilfs, mode);
2359 has_sr = (sci->sc_super_root != NULL);
2361 /* Avoid empty segment */
2362 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
2363 NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
2364 BUG_ON(mode == SC_LSEG_SR);
2365 nilfs_segctor_end_construction(sci, nilfs, 1);
2369 err = nilfs_segctor_assign(sci, mode);
2374 err = nilfs_segctor_deactivate_segments(sci, nilfs);
2378 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2379 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2382 err = nilfs_segctor_fill_in_checkpoint(sci);
2384 goto failed_to_make_up;
2386 nilfs_segctor_fill_in_super_root(sci, nilfs);
2388 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2390 /* Write partial segments */
2391 err = nilfs_segctor_prepare_write(sci, &failed_page);
2393 goto failed_to_write;
2395 nilfs_segctor_fill_in_checksums(sci, nilfs->ns_crc_seed);
2397 err = nilfs_segctor_write(sci, nilfs->ns_bdi);
2399 goto failed_to_write;
2401 nilfs_segctor_complete_write(sci);
2403 /* Commit segments */
2404 nilfs_segctor_bead_completed_segments(sci);
2406 down_write(&nilfs->ns_sem);
2407 nilfs_update_last_segment(sbi, 1);
2408 __nilfs_segctor_commit_deactivate_segments(sci, nilfs);
2409 up_write(&nilfs->ns_sem);
2410 nilfs_segctor_commit_free_segments(sci);
2411 nilfs_segctor_clear_metadata_dirty(sci);
2414 nilfs_segctor_end_construction(sci, nilfs, 0);
2416 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2418 /* Clearing sketch data */
2419 if (has_sr && sci->sc_sketch_inode) {
2420 if (i_size_read(sci->sc_sketch_inode) == 0)
2421 clear_bit(NILFS_I_DIRTY,
2422 &NILFS_I(sci->sc_sketch_inode)->i_state);
2423 i_size_write(sci->sc_sketch_inode, 0);
2426 nilfs_segctor_destroy_segment_buffers(sci);
2427 nilfs_segctor_check_out_files(sci, sbi);
2431 nilfs_segctor_abort_write(sci, failed_page, err);
2432 nilfs_segctor_cancel_segusage(sci, nilfs->ns_sufile);
2435 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2436 nilfs_redirty_inodes(&sci->sc_dirty_files);
2438 nilfs_segctor_reactivate_segments(sci, nilfs);
2441 if (nilfs_doing_gc())
2442 nilfs_redirty_inodes(&sci->sc_gc_inodes);
2443 nilfs_segctor_end_construction(sci, nilfs, err);
2448 * nilfs_secgtor_start_timer - set timer of background write
2449 * @sci: nilfs_sc_info
2451 * If the timer has already been set, it ignores the new request.
2452 * This function MUST be called within a section locking the segment
2455 static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2457 spin_lock(&sci->sc_state_lock);
2458 if (sci->sc_timer && !(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2459 sci->sc_timer->expires = jiffies + sci->sc_interval;
2460 add_timer(sci->sc_timer);
2461 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2463 spin_unlock(&sci->sc_state_lock);
2466 static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2468 spin_lock(&sci->sc_state_lock);
2469 if (!(sci->sc_flush_request & (1 << bn))) {
2470 unsigned long prev_req = sci->sc_flush_request;
2472 sci->sc_flush_request |= (1 << bn);
2474 wake_up(&sci->sc_wait_daemon);
2476 spin_unlock(&sci->sc_state_lock);
2480 * nilfs_flush_segment - trigger a segment construction for resource control
2482 * @ino: inode number of the file to be flushed out.
2484 void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2486 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2487 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2489 if (!sci || nilfs_doing_construction())
2491 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2492 /* assign bit 0 to data files */
2495 int nilfs_segctor_add_segments_to_be_freed(struct nilfs_sc_info *sci,
2496 __u64 *segnum, size_t nsegs)
2498 struct nilfs_segment_entry *ent;
2499 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
2500 struct inode *sufile = nilfs->ns_sufile;
2503 const char *flag_name;
2507 for (pnum = segnum, i = 0; i < nsegs; pnum++, i++) {
2508 ent = nilfs_alloc_segment_entry(*pnum);
2509 if (unlikely(!ent)) {
2513 list_add_tail(&ent->list, &list);
2515 err = nilfs_open_segment_entry(ent, sufile);
2519 if (unlikely(le32_to_cpu(ent->raw_su->su_flags) !=
2520 (1UL << NILFS_SEGMENT_USAGE_DIRTY))) {
2521 if (nilfs_segment_usage_clean(ent->raw_su))
2522 flag_name = "clean";
2523 else if (nilfs_segment_usage_active(ent->raw_su))
2524 flag_name = "active";
2525 else if (nilfs_segment_usage_volatile_active(
2527 flag_name = "volatile active";
2528 else if (!nilfs_segment_usage_dirty(ent->raw_su))
2529 flag_name = "non-dirty";
2531 flag_name = "erroneous";
2534 "NILFS: %s segment is requested to be cleaned "
2536 flag_name, (unsigned long long)ent->segnum);
2539 nilfs_close_segment_entry(ent, sufile);
2541 if (unlikely(err2)) {
2545 list_splice(&list, sci->sc_cleaning_segments.prev);
2549 nilfs_dispose_segment_list(&list);
2553 void nilfs_segctor_clear_segments_to_be_freed(struct nilfs_sc_info *sci)
2555 nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
2558 struct nilfs_segctor_wait_request {
2565 static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2567 struct nilfs_segctor_wait_request wait_req;
2570 spin_lock(&sci->sc_state_lock);
2571 init_wait(&wait_req.wq);
2573 atomic_set(&wait_req.done, 0);
2574 wait_req.seq = ++sci->sc_seq_request;
2575 spin_unlock(&sci->sc_state_lock);
2577 init_waitqueue_entry(&wait_req.wq, current);
2578 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2579 set_current_state(TASK_INTERRUPTIBLE);
2580 wake_up(&sci->sc_wait_daemon);
2583 if (atomic_read(&wait_req.done)) {
2587 if (!signal_pending(current)) {
2594 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2598 static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2600 struct nilfs_segctor_wait_request *wrq, *n;
2601 unsigned long flags;
2603 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2604 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2606 if (!atomic_read(&wrq->done) &&
2607 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2609 atomic_set(&wrq->done, 1);
2611 if (atomic_read(&wrq->done)) {
2612 wrq->wq.func(&wrq->wq,
2613 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2617 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2621 * nilfs_construct_segment - construct a logical segment
2624 * Return Value: On success, 0 is retured. On errors, one of the following
2625 * negative error code is returned.
2627 * %-EROFS - Read only filesystem.
2631 * %-ENOSPC - No space left on device (only in a panic state).
2633 * %-ERESTARTSYS - Interrupted.
2635 * %-ENOMEM - Insufficient memory available.
2637 int nilfs_construct_segment(struct super_block *sb)
2639 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2640 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2641 struct nilfs_transaction_info *ti;
2647 /* A call inside transactions causes a deadlock. */
2648 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2650 err = nilfs_segctor_sync(sci);
2655 * nilfs_construct_dsync_segment - construct a data-only logical segment
2657 * @inode: inode whose data blocks should be written out
2658 * @start: start byte offset
2659 * @end: end byte offset (inclusive)
2661 * Return Value: On success, 0 is retured. On errors, one of the following
2662 * negative error code is returned.
2664 * %-EROFS - Read only filesystem.
2668 * %-ENOSPC - No space left on device (only in a panic state).
2670 * %-ERESTARTSYS - Interrupted.
2672 * %-ENOMEM - Insufficient memory available.
2674 int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2675 loff_t start, loff_t end)
2677 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2678 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2679 struct nilfs_inode_info *ii;
2680 struct nilfs_transaction_info ti;
2686 nilfs_transaction_lock(sbi, &ti, 0);
2688 ii = NILFS_I(inode);
2689 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2690 nilfs_test_opt(sbi, STRICT_ORDER) ||
2691 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2692 nilfs_discontinued(sbi->s_nilfs)) {
2693 nilfs_transaction_unlock(sbi);
2694 err = nilfs_segctor_sync(sci);
2698 spin_lock(&sbi->s_inode_lock);
2699 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2700 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2701 spin_unlock(&sbi->s_inode_lock);
2702 nilfs_transaction_unlock(sbi);
2705 spin_unlock(&sbi->s_inode_lock);
2706 sci->sc_dsync_inode = ii;
2707 sci->sc_dsync_start = start;
2708 sci->sc_dsync_end = end;
2710 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2712 nilfs_transaction_unlock(sbi);
2716 struct nilfs_segctor_req {
2719 int sc_err; /* construction failure */
2720 int sb_err; /* super block writeback failure */
2723 #define FLUSH_FILE_BIT (0x1) /* data file only */
2724 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2726 static void nilfs_segctor_accept(struct nilfs_sc_info *sci,
2727 struct nilfs_segctor_req *req)
2731 req->sc_err = req->sb_err = 0;
2732 spin_lock(&sci->sc_state_lock);
2733 req->seq_accepted = sci->sc_seq_request;
2734 spin_unlock(&sci->sc_state_lock);
2737 del_timer_sync(sci->sc_timer);
2740 static void nilfs_segctor_notify(struct nilfs_sc_info *sci,
2741 struct nilfs_segctor_req *req)
2743 /* Clear requests (even when the construction failed) */
2744 spin_lock(&sci->sc_state_lock);
2746 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
2748 if (req->mode == SC_LSEG_SR) {
2749 sci->sc_seq_done = req->seq_accepted;
2750 nilfs_segctor_wakeup(sci, req->sc_err ? : req->sb_err);
2751 sci->sc_flush_request = 0;
2752 } else if (req->mode == SC_FLUSH_FILE)
2753 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
2754 else if (req->mode == SC_FLUSH_DAT)
2755 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2757 spin_unlock(&sci->sc_state_lock);
2760 static int nilfs_segctor_construct(struct nilfs_sc_info *sci,
2761 struct nilfs_segctor_req *req)
2763 struct nilfs_sb_info *sbi = sci->sc_sbi;
2764 struct the_nilfs *nilfs = sbi->s_nilfs;
2767 if (nilfs_discontinued(nilfs))
2768 req->mode = SC_LSEG_SR;
2769 if (!nilfs_segctor_confirm(sci)) {
2770 err = nilfs_segctor_do_construct(sci, req->mode);
2774 if (req->mode != SC_FLUSH_DAT)
2775 atomic_set(&nilfs->ns_ndirtyblks, 0);
2776 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2777 nilfs_discontinued(nilfs)) {
2778 down_write(&nilfs->ns_sem);
2779 req->sb_err = nilfs_commit_super(sbi);
2780 up_write(&nilfs->ns_sem);
2786 static void nilfs_construction_timeout(unsigned long data)
2788 struct task_struct *p = (struct task_struct *)data;
2793 nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2795 struct nilfs_inode_info *ii, *n;
2797 list_for_each_entry_safe(ii, n, head, i_dirty) {
2798 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2800 hlist_del_init(&ii->vfs_inode.i_hash);
2801 list_del_init(&ii->i_dirty);
2802 nilfs_clear_gcinode(&ii->vfs_inode);
2806 int nilfs_clean_segments(struct super_block *sb, void __user *argp)
2808 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2809 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2810 struct the_nilfs *nilfs = sbi->s_nilfs;
2811 struct nilfs_transaction_info ti;
2812 struct nilfs_segctor_req req = { .mode = SC_LSEG_SR };
2818 nilfs_transaction_lock(sbi, &ti, 1);
2820 err = nilfs_init_gcdat_inode(nilfs);
2823 err = nilfs_ioctl_prepare_clean_segments(nilfs, argp);
2827 list_splice_init(&nilfs->ns_gc_inodes, sci->sc_gc_inodes.prev);
2830 nilfs_segctor_accept(sci, &req);
2831 err = nilfs_segctor_construct(sci, &req);
2832 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
2833 nilfs_segctor_notify(sci, &req);
2838 nilfs_warning(sb, __func__,
2839 "segment construction failed. (err=%d)", err);
2840 set_current_state(TASK_INTERRUPTIBLE);
2841 schedule_timeout(sci->sc_interval);
2845 nilfs_clear_gcdat_inode(nilfs);
2846 nilfs_transaction_unlock(sbi);
2850 static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2852 struct nilfs_sb_info *sbi = sci->sc_sbi;
2853 struct nilfs_transaction_info ti;
2854 struct nilfs_segctor_req req = { .mode = mode };
2856 nilfs_transaction_lock(sbi, &ti, 0);
2858 nilfs_segctor_accept(sci, &req);
2859 nilfs_segctor_construct(sci, &req);
2860 nilfs_segctor_notify(sci, &req);
2863 * Unclosed segment should be retried. We do this using sc_timer.
2864 * Timeout of sc_timer will invoke complete construction which leads
2865 * to close the current logical segment.
2867 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2868 nilfs_segctor_start_timer(sci);
2870 nilfs_transaction_unlock(sbi);
2873 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2878 spin_lock(&sci->sc_state_lock);
2879 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2880 SC_FLUSH_DAT : SC_FLUSH_FILE;
2881 spin_unlock(&sci->sc_state_lock);
2884 err = nilfs_segctor_do_construct(sci, mode);
2886 spin_lock(&sci->sc_state_lock);
2887 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2888 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2889 spin_unlock(&sci->sc_state_lock);
2891 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2894 static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2896 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2897 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2898 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2899 return SC_FLUSH_FILE;
2900 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2901 return SC_FLUSH_DAT;
2907 * nilfs_segctor_thread - main loop of the segment constructor thread.
2908 * @arg: pointer to a struct nilfs_sc_info.
2910 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2911 * to execute segment constructions.
2913 static int nilfs_segctor_thread(void *arg)
2915 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
2916 struct timer_list timer;
2920 timer.data = (unsigned long)current;
2921 timer.function = nilfs_construction_timeout;
2922 sci->sc_timer = &timer;
2925 sci->sc_task = current;
2926 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2928 "segctord starting. Construction interval = %lu seconds, "
2929 "CP frequency < %lu seconds\n",
2930 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2932 spin_lock(&sci->sc_state_lock);
2937 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2940 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2942 else if (!sci->sc_flush_request)
2945 mode = nilfs_segctor_flush_mode(sci);
2947 spin_unlock(&sci->sc_state_lock);
2948 nilfs_segctor_thread_construct(sci, mode);
2949 spin_lock(&sci->sc_state_lock);
2954 if (freezing(current)) {
2955 spin_unlock(&sci->sc_state_lock);
2957 spin_lock(&sci->sc_state_lock);
2960 int should_sleep = 1;
2962 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2963 TASK_INTERRUPTIBLE);
2965 if (sci->sc_seq_request != sci->sc_seq_done)
2967 else if (sci->sc_flush_request)
2969 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2970 should_sleep = time_before(jiffies,
2971 sci->sc_timer->expires);
2974 spin_unlock(&sci->sc_state_lock);
2976 spin_lock(&sci->sc_state_lock);
2978 finish_wait(&sci->sc_wait_daemon, &wait);
2979 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2980 time_after_eq(jiffies, sci->sc_timer->expires));
2985 spin_unlock(&sci->sc_state_lock);
2986 del_timer_sync(sci->sc_timer);
2987 sci->sc_timer = NULL;
2990 sci->sc_task = NULL;
2991 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2995 static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2997 struct task_struct *t;
2999 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
3001 int err = PTR_ERR(t);
3003 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
3007 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
3011 static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
3013 sci->sc_state |= NILFS_SEGCTOR_QUIT;
3015 while (sci->sc_task) {
3016 wake_up(&sci->sc_wait_daemon);
3017 spin_unlock(&sci->sc_state_lock);
3018 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
3019 spin_lock(&sci->sc_state_lock);
3023 static int nilfs_segctor_init(struct nilfs_sc_info *sci,
3024 struct nilfs_recovery_info *ri)
3027 struct inode *inode = nilfs_iget(sci->sc_super, NILFS_SKETCH_INO);
3029 sci->sc_sketch_inode = IS_ERR(inode) ? NULL : inode;
3030 if (sci->sc_sketch_inode)
3031 i_size_write(sci->sc_sketch_inode, 0);
3033 sci->sc_seq_done = sci->sc_seq_request;
3035 list_splice_init(&ri->ri_used_segments,
3036 sci->sc_active_segments.prev);
3038 err = nilfs_segctor_start_thread(sci);
3041 list_splice_init(&sci->sc_active_segments,
3042 ri->ri_used_segments.prev);
3043 if (sci->sc_sketch_inode) {
3044 iput(sci->sc_sketch_inode);
3045 sci->sc_sketch_inode = NULL;
3052 * Setup & clean-up functions
3054 static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
3056 struct nilfs_sc_info *sci;
3058 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
3063 sci->sc_super = sbi->s_super;
3065 init_waitqueue_head(&sci->sc_wait_request);
3066 init_waitqueue_head(&sci->sc_wait_daemon);
3067 init_waitqueue_head(&sci->sc_wait_task);
3068 spin_lock_init(&sci->sc_state_lock);
3069 INIT_LIST_HEAD(&sci->sc_dirty_files);
3070 INIT_LIST_HEAD(&sci->sc_segbufs);
3071 INIT_LIST_HEAD(&sci->sc_gc_inodes);
3072 INIT_LIST_HEAD(&sci->sc_active_segments);
3073 INIT_LIST_HEAD(&sci->sc_cleaning_segments);
3074 INIT_LIST_HEAD(&sci->sc_copied_buffers);
3076 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
3077 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
3078 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
3080 if (sbi->s_interval)
3081 sci->sc_interval = sbi->s_interval;
3082 if (sbi->s_watermark)
3083 sci->sc_watermark = sbi->s_watermark;
3087 static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
3089 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
3091 /* The segctord thread was stopped and its timer was removed.
3092 But some tasks remain. */
3094 struct nilfs_sb_info *sbi = sci->sc_sbi;
3095 struct nilfs_transaction_info ti;
3096 struct nilfs_segctor_req req = { .mode = SC_LSEG_SR };
3098 nilfs_transaction_lock(sbi, &ti, 0);
3099 nilfs_segctor_accept(sci, &req);
3100 ret = nilfs_segctor_construct(sci, &req);
3101 nilfs_segctor_notify(sci, &req);
3102 nilfs_transaction_unlock(sbi);
3104 } while (ret && retrycount-- > 0);
3108 * nilfs_segctor_destroy - destroy the segment constructor.
3109 * @sci: nilfs_sc_info
3111 * nilfs_segctor_destroy() kills the segctord thread and frees
3112 * the nilfs_sc_info struct.
3113 * Caller must hold the segment semaphore.
3115 static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
3117 struct nilfs_sb_info *sbi = sci->sc_sbi;
3120 up_write(&sbi->s_nilfs->ns_segctor_sem);
3122 spin_lock(&sci->sc_state_lock);
3123 nilfs_segctor_kill_thread(sci);
3124 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
3125 || sci->sc_seq_request != sci->sc_seq_done);
3126 spin_unlock(&sci->sc_state_lock);
3128 if (flag || nilfs_segctor_confirm(sci))
3129 nilfs_segctor_write_out(sci);
3131 BUG_ON(!list_empty(&sci->sc_copied_buffers));
3133 if (!list_empty(&sci->sc_dirty_files)) {
3134 nilfs_warning(sbi->s_super, __func__,
3135 "dirty file(s) after the final construction\n");
3136 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
3138 if (!list_empty(&sci->sc_active_segments))
3139 nilfs_dispose_segment_list(&sci->sc_active_segments);
3141 if (!list_empty(&sci->sc_cleaning_segments))
3142 nilfs_dispose_segment_list(&sci->sc_cleaning_segments);
3144 BUG_ON(!list_empty(&sci->sc_segbufs));
3146 if (sci->sc_sketch_inode) {
3147 iput(sci->sc_sketch_inode);
3148 sci->sc_sketch_inode = NULL;
3150 down_write(&sbi->s_nilfs->ns_segctor_sem);
3156 * nilfs_attach_segment_constructor - attach a segment constructor
3157 * @sbi: nilfs_sb_info
3158 * @ri: nilfs_recovery_info
3160 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
3161 * initilizes it, and starts the segment constructor.
3163 * Return Value: On success, 0 is returned. On error, one of the following
3164 * negative error code is returned.
3166 * %-ENOMEM - Insufficient memory available.
3168 int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi,
3169 struct nilfs_recovery_info *ri)
3171 struct the_nilfs *nilfs = sbi->s_nilfs;
3174 /* Each field of nilfs_segctor is cleared through the initialization
3175 of super-block info */
3176 sbi->s_sc_info = nilfs_segctor_new(sbi);
3177 if (!sbi->s_sc_info)
3180 nilfs_attach_writer(nilfs, sbi);
3181 err = nilfs_segctor_init(NILFS_SC(sbi), ri);
3183 nilfs_detach_writer(nilfs, sbi);
3184 kfree(sbi->s_sc_info);
3185 sbi->s_sc_info = NULL;
3191 * nilfs_detach_segment_constructor - destroy the segment constructor
3192 * @sbi: nilfs_sb_info
3194 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
3195 * frees the struct nilfs_sc_info, and destroy the dirty file list.
3197 void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
3199 struct the_nilfs *nilfs = sbi->s_nilfs;
3200 LIST_HEAD(garbage_list);
3202 down_write(&nilfs->ns_segctor_sem);
3203 if (NILFS_SC(sbi)) {
3204 nilfs_segctor_destroy(NILFS_SC(sbi));
3205 sbi->s_sc_info = NULL;
3208 /* Force to free the list of dirty files */
3209 spin_lock(&sbi->s_inode_lock);
3210 if (!list_empty(&sbi->s_dirty_files)) {
3211 list_splice_init(&sbi->s_dirty_files, &garbage_list);
3212 nilfs_warning(sbi->s_super, __func__,
3213 "Non empty dirty list after the last "
3214 "segment construction\n");
3216 spin_unlock(&sbi->s_inode_lock);
3217 up_write(&nilfs->ns_segctor_sem);
3219 nilfs_dispose_list(sbi, &garbage_list, 1);
3220 nilfs_detach_writer(nilfs, sbi);