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>
35 #include <linux/slab.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 */
74 NILFS_ST_SR, /* Super root */
75 NILFS_ST_DSYNC, /* Data sync blocks */
79 /* State flags of collection */
80 #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
81 #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
82 #define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
83 #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
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_destroy_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; otherwise 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;
338 ti->ti_flags = NILFS_TI_WRITER;
340 ti->ti_save = cur_ti;
341 ti->ti_magic = NILFS_TI_MAGIC;
342 INIT_LIST_HEAD(&ti->ti_garbage);
343 current->journal_info = ti;
346 down_write(&sbi->s_nilfs->ns_segctor_sem);
347 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &NILFS_SC(sbi)->sc_flags))
350 nilfs_segctor_do_immediate_flush(NILFS_SC(sbi));
352 up_write(&sbi->s_nilfs->ns_segctor_sem);
356 ti->ti_flags |= NILFS_TI_GC;
359 static void nilfs_transaction_unlock(struct nilfs_sb_info *sbi)
361 struct nilfs_transaction_info *ti = current->journal_info;
363 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
364 BUG_ON(ti->ti_count > 0);
366 up_write(&sbi->s_nilfs->ns_segctor_sem);
367 current->journal_info = ti->ti_save;
368 if (!list_empty(&ti->ti_garbage))
369 nilfs_dispose_list(sbi, &ti->ti_garbage, 0);
372 static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
373 struct nilfs_segsum_pointer *ssp,
376 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
377 unsigned blocksize = sci->sc_super->s_blocksize;
380 if (unlikely(ssp->offset + bytes > blocksize)) {
382 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
383 &segbuf->sb_segsum_buffers));
384 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
386 p = ssp->bh->b_data + ssp->offset;
387 ssp->offset += bytes;
392 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
393 * @sci: nilfs_sc_info
395 static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
397 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
398 struct buffer_head *sumbh;
403 if (nilfs_doing_gc())
405 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime);
409 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
410 sumbytes = segbuf->sb_sum.sumbytes;
411 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
412 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
413 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
417 static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
419 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
420 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
421 return -E2BIG; /* The current segment is filled up
423 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
424 return nilfs_segctor_reset_segment_buffer(sci);
427 static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
429 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
432 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
433 err = nilfs_segctor_feed_segment(sci);
436 segbuf = sci->sc_curseg;
438 err = nilfs_segbuf_extend_payload(segbuf, &sci->sc_super_root);
440 segbuf->sb_sum.flags |= NILFS_SS_SR;
445 * Functions for making segment summary and payloads
447 static int nilfs_segctor_segsum_block_required(
448 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
451 unsigned blocksize = sci->sc_super->s_blocksize;
452 /* Size of finfo and binfo is enough small against blocksize */
454 return ssp->offset + binfo_size +
455 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
459 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
462 sci->sc_curseg->sb_sum.nfinfo++;
463 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
464 nilfs_segctor_map_segsum_entry(
465 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
467 if (inode->i_sb && !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
468 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
472 static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
475 struct nilfs_finfo *finfo;
476 struct nilfs_inode_info *ii;
477 struct nilfs_segment_buffer *segbuf;
479 if (sci->sc_blk_cnt == 0)
483 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
485 finfo->fi_ino = cpu_to_le64(inode->i_ino);
486 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
487 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
488 finfo->fi_cno = cpu_to_le64(ii->i_cno);
490 segbuf = sci->sc_curseg;
491 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
492 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
493 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
494 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
497 static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
498 struct buffer_head *bh,
502 struct nilfs_segment_buffer *segbuf;
503 int required, err = 0;
506 segbuf = sci->sc_curseg;
507 required = nilfs_segctor_segsum_block_required(
508 sci, &sci->sc_binfo_ptr, binfo_size);
509 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
510 nilfs_segctor_end_finfo(sci, inode);
511 err = nilfs_segctor_feed_segment(sci);
516 if (unlikely(required)) {
517 err = nilfs_segbuf_extend_segsum(segbuf);
521 if (sci->sc_blk_cnt == 0)
522 nilfs_segctor_begin_finfo(sci, inode);
524 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
525 /* Substitution to vblocknr is delayed until update_blocknr() */
526 nilfs_segbuf_add_file_buffer(segbuf, bh);
532 static int nilfs_handle_bmap_error(int err, const char *fname,
533 struct inode *inode, struct super_block *sb)
535 if (err == -EINVAL) {
536 nilfs_error(sb, fname, "broken bmap (inode=%lu)\n",
544 * Callback functions that enumerate, mark, and collect dirty blocks
546 static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
547 struct buffer_head *bh, struct inode *inode)
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 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
570 if (unlikely(err < 0))
571 return nilfs_handle_bmap_error(err, __func__, inode,
576 static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
577 struct buffer_head *bh,
580 WARN_ON(!buffer_dirty(bh));
581 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
584 static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
585 struct nilfs_segsum_pointer *ssp,
586 union nilfs_binfo *binfo)
588 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
589 sci, ssp, sizeof(*binfo_v));
590 *binfo_v = binfo->bi_v;
593 static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
594 struct nilfs_segsum_pointer *ssp,
595 union nilfs_binfo *binfo)
597 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
598 sci, ssp, sizeof(*vblocknr));
599 *vblocknr = binfo->bi_v.bi_vblocknr;
602 struct nilfs_sc_operations nilfs_sc_file_ops = {
603 .collect_data = nilfs_collect_file_data,
604 .collect_node = nilfs_collect_file_node,
605 .collect_bmap = nilfs_collect_file_bmap,
606 .write_data_binfo = nilfs_write_file_data_binfo,
607 .write_node_binfo = nilfs_write_file_node_binfo,
610 static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
611 struct buffer_head *bh, struct inode *inode)
615 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
616 if (unlikely(err < 0))
617 return nilfs_handle_bmap_error(err, __func__, inode,
620 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
622 sci->sc_datablk_cnt++;
626 static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
627 struct buffer_head *bh, struct inode *inode)
629 WARN_ON(!buffer_dirty(bh));
630 return nilfs_segctor_add_file_block(sci, bh, inode,
631 sizeof(struct nilfs_binfo_dat));
634 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
635 struct nilfs_segsum_pointer *ssp,
636 union nilfs_binfo *binfo)
638 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
640 *blkoff = binfo->bi_dat.bi_blkoff;
643 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
644 struct nilfs_segsum_pointer *ssp,
645 union nilfs_binfo *binfo)
647 struct nilfs_binfo_dat *binfo_dat =
648 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
649 *binfo_dat = binfo->bi_dat;
652 struct nilfs_sc_operations nilfs_sc_dat_ops = {
653 .collect_data = nilfs_collect_dat_data,
654 .collect_node = nilfs_collect_file_node,
655 .collect_bmap = nilfs_collect_dat_bmap,
656 .write_data_binfo = nilfs_write_dat_data_binfo,
657 .write_node_binfo = nilfs_write_dat_node_binfo,
660 struct nilfs_sc_operations nilfs_sc_dsync_ops = {
661 .collect_data = nilfs_collect_file_data,
662 .collect_node = NULL,
663 .collect_bmap = NULL,
664 .write_data_binfo = nilfs_write_file_data_binfo,
665 .write_node_binfo = NULL,
668 static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
669 struct list_head *listp,
671 loff_t start, loff_t end)
673 struct address_space *mapping = inode->i_mapping;
675 pgoff_t index = 0, last = ULONG_MAX;
679 if (unlikely(start != 0 || end != LLONG_MAX)) {
681 * A valid range is given for sync-ing data pages. The
682 * range is rounded to per-page; extra dirty buffers
683 * may be included if blocksize < pagesize.
685 index = start >> PAGE_SHIFT;
686 last = end >> PAGE_SHIFT;
688 pagevec_init(&pvec, 0);
690 if (unlikely(index > last) ||
691 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
692 min_t(pgoff_t, last - index,
693 PAGEVEC_SIZE - 1) + 1))
696 for (i = 0; i < pagevec_count(&pvec); i++) {
697 struct buffer_head *bh, *head;
698 struct page *page = pvec.pages[i];
700 if (unlikely(page->index > last))
705 if (!page_has_buffers(page))
706 create_empty_buffers(page,
707 1 << inode->i_blkbits, 0);
711 bh = head = page_buffers(page);
713 if (!buffer_dirty(bh))
716 list_add_tail(&bh->b_assoc_buffers, listp);
718 if (unlikely(ndirties >= nlimit)) {
719 pagevec_release(&pvec);
723 } while (bh = bh->b_this_page, bh != head);
725 pagevec_release(&pvec);
730 static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
731 struct list_head *listp)
733 struct nilfs_inode_info *ii = NILFS_I(inode);
734 struct address_space *mapping = &ii->i_btnode_cache;
736 struct buffer_head *bh, *head;
740 pagevec_init(&pvec, 0);
742 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
744 for (i = 0; i < pagevec_count(&pvec); i++) {
745 bh = head = page_buffers(pvec.pages[i]);
747 if (buffer_dirty(bh)) {
749 list_add_tail(&bh->b_assoc_buffers,
752 bh = bh->b_this_page;
753 } while (bh != head);
755 pagevec_release(&pvec);
760 static void nilfs_dispose_list(struct nilfs_sb_info *sbi,
761 struct list_head *head, int force)
763 struct nilfs_inode_info *ii, *n;
764 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
767 while (!list_empty(head)) {
768 spin_lock(&sbi->s_inode_lock);
769 list_for_each_entry_safe(ii, n, head, i_dirty) {
770 list_del_init(&ii->i_dirty);
772 if (unlikely(ii->i_bh)) {
776 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
777 set_bit(NILFS_I_QUEUED, &ii->i_state);
778 list_add_tail(&ii->i_dirty,
779 &sbi->s_dirty_files);
783 if (nv == SC_N_INODEVEC)
786 spin_unlock(&sbi->s_inode_lock);
788 for (pii = ivec; nv > 0; pii++, nv--)
789 iput(&(*pii)->vfs_inode);
793 static int nilfs_test_metadata_dirty(struct nilfs_sb_info *sbi)
795 struct the_nilfs *nilfs = sbi->s_nilfs;
798 if (nilfs_mdt_fetch_dirty(sbi->s_ifile))
800 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
802 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
804 if (ret || nilfs_doing_gc())
805 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs)))
810 static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
812 return list_empty(&sci->sc_dirty_files) &&
813 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
814 sci->sc_nfreesegs == 0 &&
815 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
818 static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
820 struct nilfs_sb_info *sbi = sci->sc_sbi;
823 if (nilfs_test_metadata_dirty(sbi))
824 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
826 spin_lock(&sbi->s_inode_lock);
827 if (list_empty(&sbi->s_dirty_files) && nilfs_segctor_clean(sci))
830 spin_unlock(&sbi->s_inode_lock);
834 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
836 struct nilfs_sb_info *sbi = sci->sc_sbi;
837 struct the_nilfs *nilfs = sbi->s_nilfs;
839 nilfs_mdt_clear_dirty(sbi->s_ifile);
840 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
841 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
842 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs));
845 static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
847 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
848 struct buffer_head *bh_cp;
849 struct nilfs_checkpoint *raw_cp;
852 /* XXX: this interface will be changed */
853 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
856 /* The following code is duplicated with cpfile. But, it is
857 needed to collect the checkpoint even if it was not newly
859 nilfs_mdt_mark_buffer_dirty(bh_cp);
860 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
861 nilfs_cpfile_put_checkpoint(
862 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
864 WARN_ON(err == -EINVAL || err == -ENOENT);
869 static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
871 struct nilfs_sb_info *sbi = sci->sc_sbi;
872 struct the_nilfs *nilfs = sbi->s_nilfs;
873 struct buffer_head *bh_cp;
874 struct nilfs_checkpoint *raw_cp;
877 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
880 WARN_ON(err == -EINVAL || err == -ENOENT);
883 raw_cp->cp_snapshot_list.ssl_next = 0;
884 raw_cp->cp_snapshot_list.ssl_prev = 0;
885 raw_cp->cp_inodes_count =
886 cpu_to_le64(atomic_read(&sbi->s_inodes_count));
887 raw_cp->cp_blocks_count =
888 cpu_to_le64(atomic_read(&sbi->s_blocks_count));
889 raw_cp->cp_nblk_inc =
890 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
891 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
892 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
894 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
895 nilfs_checkpoint_clear_minor(raw_cp);
897 nilfs_checkpoint_set_minor(raw_cp);
899 nilfs_write_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode, 1);
900 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
907 static void nilfs_fill_in_file_bmap(struct inode *ifile,
908 struct nilfs_inode_info *ii)
911 struct buffer_head *ibh;
912 struct nilfs_inode *raw_inode;
914 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
917 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
919 nilfs_bmap_write(ii->i_bmap, raw_inode);
920 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
924 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci,
927 struct nilfs_inode_info *ii;
929 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
930 nilfs_fill_in_file_bmap(ifile, ii);
931 set_bit(NILFS_I_COLLECTED, &ii->i_state);
936 * CRC calculation routines
938 static void nilfs_fill_in_super_root_crc(struct buffer_head *bh_sr, u32 seed)
940 struct nilfs_super_root *raw_sr =
941 (struct nilfs_super_root *)bh_sr->b_data;
945 (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
946 NILFS_SR_BYTES - sizeof(raw_sr->sr_sum));
947 raw_sr->sr_sum = cpu_to_le32(crc);
950 static void nilfs_segctor_fill_in_checksums(struct nilfs_sc_info *sci,
953 struct nilfs_segment_buffer *segbuf;
955 if (sci->sc_super_root)
956 nilfs_fill_in_super_root_crc(sci->sc_super_root, seed);
958 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
959 nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
960 nilfs_segbuf_fill_in_data_crc(segbuf, seed);
964 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
965 struct the_nilfs *nilfs)
967 struct buffer_head *bh_sr = sci->sc_super_root;
968 struct nilfs_super_root *raw_sr =
969 (struct nilfs_super_root *)bh_sr->b_data;
970 unsigned isz = nilfs->ns_inode_size;
972 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
973 raw_sr->sr_nongc_ctime
974 = cpu_to_le64(nilfs_doing_gc() ?
975 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
976 raw_sr->sr_flags = 0;
978 nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
979 NILFS_SR_DAT_OFFSET(isz), 1);
980 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
981 NILFS_SR_CPFILE_OFFSET(isz), 1);
982 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
983 NILFS_SR_SUFILE_OFFSET(isz), 1);
986 static void nilfs_redirty_inodes(struct list_head *head)
988 struct nilfs_inode_info *ii;
990 list_for_each_entry(ii, head, i_dirty) {
991 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
992 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
996 static void nilfs_drop_collected_inodes(struct list_head *head)
998 struct nilfs_inode_info *ii;
1000 list_for_each_entry(ii, head, i_dirty) {
1001 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
1004 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
1005 set_bit(NILFS_I_UPDATED, &ii->i_state);
1009 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1010 struct inode *inode,
1011 struct list_head *listp,
1012 int (*collect)(struct nilfs_sc_info *,
1013 struct buffer_head *,
1016 struct buffer_head *bh, *n;
1020 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1021 list_del_init(&bh->b_assoc_buffers);
1022 err = collect(sci, bh, inode);
1025 goto dispose_buffers;
1031 while (!list_empty(listp)) {
1032 bh = list_entry(listp->next, struct buffer_head,
1034 list_del_init(&bh->b_assoc_buffers);
1040 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1042 /* Remaining number of blocks within segment buffer */
1043 return sci->sc_segbuf_nblocks -
1044 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1047 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1048 struct inode *inode,
1049 struct nilfs_sc_operations *sc_ops)
1051 LIST_HEAD(data_buffers);
1052 LIST_HEAD(node_buffers);
1055 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1056 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1058 n = nilfs_lookup_dirty_data_buffers(
1059 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1061 err = nilfs_segctor_apply_buffers(
1062 sci, inode, &data_buffers,
1063 sc_ops->collect_data);
1064 BUG_ON(!err); /* always receive -E2BIG or true error */
1068 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1070 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1071 err = nilfs_segctor_apply_buffers(
1072 sci, inode, &data_buffers, sc_ops->collect_data);
1073 if (unlikely(err)) {
1074 /* dispose node list */
1075 nilfs_segctor_apply_buffers(
1076 sci, inode, &node_buffers, NULL);
1079 sci->sc_stage.flags |= NILFS_CF_NODE;
1082 err = nilfs_segctor_apply_buffers(
1083 sci, inode, &node_buffers, sc_ops->collect_node);
1087 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1088 err = nilfs_segctor_apply_buffers(
1089 sci, inode, &node_buffers, sc_ops->collect_bmap);
1093 nilfs_segctor_end_finfo(sci, inode);
1094 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1100 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1101 struct inode *inode)
1103 LIST_HEAD(data_buffers);
1104 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1107 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1108 sci->sc_dsync_start,
1111 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1112 nilfs_collect_file_data);
1114 nilfs_segctor_end_finfo(sci, inode);
1116 /* always receive -E2BIG or true error if n > rest */
1121 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1123 struct nilfs_sb_info *sbi = sci->sc_sbi;
1124 struct the_nilfs *nilfs = sbi->s_nilfs;
1125 struct list_head *head;
1126 struct nilfs_inode_info *ii;
1130 switch (sci->sc_stage.scnt) {
1133 sci->sc_stage.flags = 0;
1135 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1136 sci->sc_nblk_inc = 0;
1137 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1138 if (mode == SC_LSEG_DSYNC) {
1139 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1144 sci->sc_stage.dirty_file_ptr = NULL;
1145 sci->sc_stage.gc_inode_ptr = NULL;
1146 if (mode == SC_FLUSH_DAT) {
1147 sci->sc_stage.scnt = NILFS_ST_DAT;
1150 sci->sc_stage.scnt++; /* Fall through */
1152 if (nilfs_doing_gc()) {
1153 head = &sci->sc_gc_inodes;
1154 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1156 list_for_each_entry_continue(ii, head, i_dirty) {
1157 err = nilfs_segctor_scan_file(
1158 sci, &ii->vfs_inode,
1159 &nilfs_sc_file_ops);
1160 if (unlikely(err)) {
1161 sci->sc_stage.gc_inode_ptr = list_entry(
1163 struct nilfs_inode_info,
1167 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1169 sci->sc_stage.gc_inode_ptr = NULL;
1171 sci->sc_stage.scnt++; /* Fall through */
1173 head = &sci->sc_dirty_files;
1174 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1176 list_for_each_entry_continue(ii, head, i_dirty) {
1177 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1179 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1180 &nilfs_sc_file_ops);
1181 if (unlikely(err)) {
1182 sci->sc_stage.dirty_file_ptr =
1183 list_entry(ii->i_dirty.prev,
1184 struct nilfs_inode_info,
1188 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1189 /* XXX: required ? */
1191 sci->sc_stage.dirty_file_ptr = NULL;
1192 if (mode == SC_FLUSH_FILE) {
1193 sci->sc_stage.scnt = NILFS_ST_DONE;
1196 sci->sc_stage.scnt++;
1197 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1199 case NILFS_ST_IFILE:
1200 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1201 &nilfs_sc_file_ops);
1204 sci->sc_stage.scnt++;
1205 /* Creating a checkpoint */
1206 err = nilfs_segctor_create_checkpoint(sci);
1210 case NILFS_ST_CPFILE:
1211 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1212 &nilfs_sc_file_ops);
1215 sci->sc_stage.scnt++; /* Fall through */
1216 case NILFS_ST_SUFILE:
1217 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1218 sci->sc_nfreesegs, &ndone);
1219 if (unlikely(err)) {
1220 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1221 sci->sc_freesegs, ndone,
1225 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1227 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1228 &nilfs_sc_file_ops);
1231 sci->sc_stage.scnt++; /* Fall through */
1234 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1238 if (mode == SC_FLUSH_DAT) {
1239 sci->sc_stage.scnt = NILFS_ST_DONE;
1242 sci->sc_stage.scnt++; /* Fall through */
1244 if (mode == SC_LSEG_SR) {
1245 /* Appending a super root */
1246 err = nilfs_segctor_add_super_root(sci);
1250 /* End of a logical segment */
1251 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1252 sci->sc_stage.scnt = NILFS_ST_DONE;
1254 case NILFS_ST_DSYNC:
1256 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
1257 ii = sci->sc_dsync_inode;
1258 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1261 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1264 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1265 sci->sc_stage.scnt = NILFS_ST_DONE;
1278 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1279 * @sci: nilfs_sc_info
1280 * @nilfs: nilfs object
1282 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1283 struct the_nilfs *nilfs)
1285 struct nilfs_segment_buffer *segbuf, *prev;
1289 segbuf = nilfs_segbuf_new(sci->sc_super);
1290 if (unlikely(!segbuf))
1293 if (list_empty(&sci->sc_write_logs)) {
1294 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1295 nilfs->ns_pseg_offset, nilfs);
1296 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1297 nilfs_shift_to_next_segment(nilfs);
1298 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1301 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1302 nextnum = nilfs->ns_nextnum;
1304 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1305 /* Start from the head of a new full segment */
1309 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1310 nilfs_segbuf_map_cont(segbuf, prev);
1311 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1312 nextnum = prev->sb_nextnum;
1314 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1315 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1316 segbuf->sb_sum.seg_seq++;
1321 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1326 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1330 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1332 BUG_ON(!list_empty(&sci->sc_segbufs));
1333 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1334 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
1338 nilfs_segbuf_free(segbuf);
1342 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1343 struct the_nilfs *nilfs, int nadd)
1345 struct nilfs_segment_buffer *segbuf, *prev;
1346 struct inode *sufile = nilfs->ns_sufile;
1351 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1353 * Since the segment specified with nextnum might be allocated during
1354 * the previous construction, the buffer including its segusage may
1355 * not be dirty. The following call ensures that the buffer is dirty
1356 * and will pin the buffer on memory until the sufile is written.
1358 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
1362 for (i = 0; i < nadd; i++) {
1363 /* extend segment info */
1365 segbuf = nilfs_segbuf_new(sci->sc_super);
1366 if (unlikely(!segbuf))
1369 /* map this buffer to region of segment on-disk */
1370 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1371 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1373 /* allocate the next next full segment */
1374 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1378 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1379 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1381 list_add_tail(&segbuf->sb_list, &list);
1384 list_splice_tail(&list, &sci->sc_segbufs);
1388 nilfs_segbuf_free(segbuf);
1390 list_for_each_entry(segbuf, &list, sb_list) {
1391 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1392 WARN_ON(ret); /* never fails */
1394 nilfs_destroy_logs(&list);
1398 static void nilfs_free_incomplete_logs(struct list_head *logs,
1399 struct the_nilfs *nilfs)
1401 struct nilfs_segment_buffer *segbuf, *prev;
1402 struct inode *sufile = nilfs->ns_sufile;
1405 segbuf = NILFS_FIRST_SEGBUF(logs);
1406 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
1407 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1408 WARN_ON(ret); /* never fails */
1410 if (atomic_read(&segbuf->sb_err)) {
1411 /* Case 1: The first segment failed */
1412 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1413 /* Case 1a: Partial segment appended into an existing
1415 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1416 segbuf->sb_fseg_end);
1417 else /* Case 1b: New full segment */
1418 set_nilfs_discontinued(nilfs);
1422 list_for_each_entry_continue(segbuf, logs, sb_list) {
1423 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1424 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1425 WARN_ON(ret); /* never fails */
1427 if (atomic_read(&segbuf->sb_err) &&
1428 segbuf->sb_segnum != nilfs->ns_nextnum)
1429 /* Case 2: extended segment (!= next) failed */
1430 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1435 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1436 struct inode *sufile)
1438 struct nilfs_segment_buffer *segbuf;
1439 unsigned long live_blocks;
1442 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1443 live_blocks = segbuf->sb_sum.nblocks +
1444 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
1445 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1448 WARN_ON(ret); /* always succeed because the segusage is dirty */
1452 static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
1454 struct nilfs_segment_buffer *segbuf;
1457 segbuf = NILFS_FIRST_SEGBUF(logs);
1458 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1459 segbuf->sb_pseg_start -
1460 segbuf->sb_fseg_start, 0);
1461 WARN_ON(ret); /* always succeed because the segusage is dirty */
1463 list_for_each_entry_continue(segbuf, logs, sb_list) {
1464 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1466 WARN_ON(ret); /* always succeed */
1470 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1471 struct nilfs_segment_buffer *last,
1472 struct inode *sufile)
1474 struct nilfs_segment_buffer *segbuf = last;
1477 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1478 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1479 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1482 nilfs_truncate_logs(&sci->sc_segbufs, last);
1486 static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1487 struct the_nilfs *nilfs, int mode)
1489 struct nilfs_cstage prev_stage = sci->sc_stage;
1492 /* Collection retry loop */
1494 sci->sc_super_root = NULL;
1495 sci->sc_nblk_this_inc = 0;
1496 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1498 err = nilfs_segctor_reset_segment_buffer(sci);
1502 err = nilfs_segctor_collect_blocks(sci, mode);
1503 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1507 if (unlikely(err != -E2BIG))
1510 /* The current segment is filled up */
1511 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1514 nilfs_clear_logs(&sci->sc_segbufs);
1516 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1520 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1521 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1525 WARN_ON(err); /* do not happen */
1527 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1528 sci->sc_stage = prev_stage;
1530 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1537 static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1538 struct buffer_head *new_bh)
1540 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1542 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1543 /* The caller must release old_bh */
1547 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1548 struct nilfs_segment_buffer *segbuf,
1551 struct inode *inode = NULL;
1553 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1554 unsigned long nblocks = 0, ndatablk = 0;
1555 struct nilfs_sc_operations *sc_op = NULL;
1556 struct nilfs_segsum_pointer ssp;
1557 struct nilfs_finfo *finfo = NULL;
1558 union nilfs_binfo binfo;
1559 struct buffer_head *bh, *bh_org;
1566 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1567 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1568 ssp.offset = sizeof(struct nilfs_segment_summary);
1570 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1571 if (bh == sci->sc_super_root)
1574 finfo = nilfs_segctor_map_segsum_entry(
1575 sci, &ssp, sizeof(*finfo));
1576 ino = le64_to_cpu(finfo->fi_ino);
1577 nblocks = le32_to_cpu(finfo->fi_nblocks);
1578 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1580 if (buffer_nilfs_node(bh))
1581 inode = NILFS_BTNC_I(bh->b_page->mapping);
1583 inode = NILFS_AS_I(bh->b_page->mapping);
1585 if (mode == SC_LSEG_DSYNC)
1586 sc_op = &nilfs_sc_dsync_ops;
1587 else if (ino == NILFS_DAT_INO)
1588 sc_op = &nilfs_sc_dat_ops;
1589 else /* file blocks */
1590 sc_op = &nilfs_sc_file_ops;
1594 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1597 nilfs_list_replace_buffer(bh_org, bh);
1603 sc_op->write_data_binfo(sci, &ssp, &binfo);
1605 sc_op->write_node_binfo(sci, &ssp, &binfo);
1608 if (--nblocks == 0) {
1612 } else if (ndatablk > 0)
1619 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1623 static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1625 struct nilfs_segment_buffer *segbuf;
1628 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1629 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1632 nilfs_segbuf_fill_in_segsum(segbuf);
1638 nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1640 struct page *clone_page;
1641 struct buffer_head *bh, *head, *bh2;
1644 bh = head = page_buffers(page);
1646 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1647 if (unlikely(!clone_page))
1650 bh2 = page_buffers(clone_page);
1651 kaddr = kmap_atomic(page, KM_USER0);
1653 if (list_empty(&bh->b_assoc_buffers))
1656 page_cache_get(clone_page); /* for each bh */
1657 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1658 bh2->b_blocknr = bh->b_blocknr;
1659 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1660 list_add_tail(&bh->b_assoc_buffers, out);
1661 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1662 kunmap_atomic(kaddr, KM_USER0);
1664 if (!TestSetPageWriteback(clone_page))
1665 inc_zone_page_state(clone_page, NR_WRITEBACK);
1666 unlock_page(clone_page);
1671 static int nilfs_test_page_to_be_frozen(struct page *page)
1673 struct address_space *mapping = page->mapping;
1675 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1678 if (page_mapped(page)) {
1679 ClearPageChecked(page);
1682 return PageChecked(page);
1685 static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1687 if (!page || PageWriteback(page))
1688 /* For split b-tree node pages, this function may be called
1689 twice. We ignore the 2nd or later calls by this check. */
1693 clear_page_dirty_for_io(page);
1694 set_page_writeback(page);
1697 if (nilfs_test_page_to_be_frozen(page)) {
1698 int err = nilfs_copy_replace_page_buffers(page, out);
1705 static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1706 struct page **failed_page)
1708 struct nilfs_segment_buffer *segbuf;
1709 struct page *bd_page = NULL, *fs_page = NULL;
1710 struct list_head *list = &sci->sc_copied_buffers;
1713 *failed_page = NULL;
1714 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1715 struct buffer_head *bh;
1717 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1719 if (bh->b_page != bd_page) {
1722 clear_page_dirty_for_io(bd_page);
1723 set_page_writeback(bd_page);
1724 unlock_page(bd_page);
1726 bd_page = bh->b_page;
1730 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1732 if (bh == sci->sc_super_root) {
1733 if (bh->b_page != bd_page) {
1735 clear_page_dirty_for_io(bd_page);
1736 set_page_writeback(bd_page);
1737 unlock_page(bd_page);
1738 bd_page = bh->b_page;
1742 if (bh->b_page != fs_page) {
1743 err = nilfs_begin_page_io(fs_page, list);
1744 if (unlikely(err)) {
1745 *failed_page = fs_page;
1748 fs_page = bh->b_page;
1754 clear_page_dirty_for_io(bd_page);
1755 set_page_writeback(bd_page);
1756 unlock_page(bd_page);
1758 err = nilfs_begin_page_io(fs_page, list);
1760 *failed_page = fs_page;
1765 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
1766 struct the_nilfs *nilfs)
1770 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
1771 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1775 static void __nilfs_end_page_io(struct page *page, int err)
1778 if (!nilfs_page_buffers_clean(page))
1779 __set_page_dirty_nobuffers(page);
1780 ClearPageError(page);
1782 __set_page_dirty_nobuffers(page);
1786 if (buffer_nilfs_allocated(page_buffers(page))) {
1787 if (TestClearPageWriteback(page))
1788 dec_zone_page_state(page, NR_WRITEBACK);
1790 end_page_writeback(page);
1793 static void nilfs_end_page_io(struct page *page, int err)
1798 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
1800 * For b-tree node pages, this function may be called twice
1801 * or more because they might be split in a segment.
1803 if (PageDirty(page)) {
1805 * For pages holding split b-tree node buffers, dirty
1806 * flag on the buffers may be cleared discretely.
1807 * In that case, the page is once redirtied for
1808 * remaining buffers, and it must be cancelled if
1809 * all the buffers get cleaned later.
1812 if (nilfs_page_buffers_clean(page))
1813 __nilfs_clear_page_dirty(page);
1819 __nilfs_end_page_io(page, err);
1822 static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1824 struct buffer_head *bh, *head;
1827 while (!list_empty(list)) {
1828 bh = list_entry(list->next, struct buffer_head,
1831 page_cache_get(page);
1832 head = bh = page_buffers(page);
1834 if (!list_empty(&bh->b_assoc_buffers)) {
1835 list_del_init(&bh->b_assoc_buffers);
1837 set_buffer_uptodate(bh);
1838 clear_buffer_dirty(bh);
1839 clear_buffer_nilfs_volatile(bh);
1841 brelse(bh); /* for b_assoc_buffers */
1843 } while ((bh = bh->b_this_page) != head);
1845 __nilfs_end_page_io(page, err);
1846 page_cache_release(page);
1850 static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
1851 struct buffer_head *bh_sr, int err)
1853 struct nilfs_segment_buffer *segbuf;
1854 struct page *bd_page = NULL, *fs_page = NULL;
1855 struct buffer_head *bh;
1857 if (list_empty(logs))
1860 list_for_each_entry(segbuf, logs, sb_list) {
1861 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1863 if (bh->b_page != bd_page) {
1865 end_page_writeback(bd_page);
1866 bd_page = bh->b_page;
1870 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1873 if (bh->b_page != bd_page) {
1874 end_page_writeback(bd_page);
1875 bd_page = bh->b_page;
1879 if (bh->b_page != fs_page) {
1880 nilfs_end_page_io(fs_page, err);
1881 if (fs_page && fs_page == failed_page)
1883 fs_page = bh->b_page;
1888 end_page_writeback(bd_page);
1890 nilfs_end_page_io(fs_page, err);
1893 static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1894 struct the_nilfs *nilfs, int err)
1899 list_splice_tail_init(&sci->sc_write_logs, &logs);
1900 ret = nilfs_wait_on_logs(&logs);
1901 nilfs_abort_logs(&logs, NULL, sci->sc_super_root, ret ? : err);
1903 list_splice_tail_init(&sci->sc_segbufs, &logs);
1904 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1905 nilfs_free_incomplete_logs(&logs, nilfs);
1906 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
1908 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1909 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1913 WARN_ON(ret); /* do not happen */
1916 nilfs_destroy_logs(&logs);
1917 sci->sc_super_root = NULL;
1920 static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1921 struct nilfs_segment_buffer *segbuf)
1923 nilfs->ns_segnum = segbuf->sb_segnum;
1924 nilfs->ns_nextnum = segbuf->sb_nextnum;
1925 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1926 + segbuf->sb_sum.nblocks;
1927 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1928 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1931 static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1933 struct nilfs_segment_buffer *segbuf;
1934 struct page *bd_page = NULL, *fs_page = NULL;
1935 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
1936 int update_sr = (sci->sc_super_root != NULL);
1938 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
1939 struct buffer_head *bh;
1941 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1943 set_buffer_uptodate(bh);
1944 clear_buffer_dirty(bh);
1945 if (bh->b_page != bd_page) {
1947 end_page_writeback(bd_page);
1948 bd_page = bh->b_page;
1952 * We assume that the buffers which belong to the same page
1953 * continue over the buffer list.
1954 * Under this assumption, the last BHs of pages is
1955 * identifiable by the discontinuity of bh->b_page
1956 * (page != fs_page).
1958 * For B-tree node blocks, however, this assumption is not
1959 * guaranteed. The cleanup code of B-tree node pages needs
1962 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1964 set_buffer_uptodate(bh);
1965 clear_buffer_dirty(bh);
1966 clear_buffer_nilfs_volatile(bh);
1967 if (bh == sci->sc_super_root) {
1968 if (bh->b_page != bd_page) {
1969 end_page_writeback(bd_page);
1970 bd_page = bh->b_page;
1974 if (bh->b_page != fs_page) {
1975 nilfs_end_page_io(fs_page, 0);
1976 fs_page = bh->b_page;
1980 if (!NILFS_SEG_SIMPLEX(&segbuf->sb_sum)) {
1981 if (NILFS_SEG_LOGBGN(&segbuf->sb_sum)) {
1982 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1983 sci->sc_lseg_stime = jiffies;
1985 if (NILFS_SEG_LOGEND(&segbuf->sb_sum))
1986 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1990 * Since pages may continue over multiple segment buffers,
1991 * end of the last page must be checked outside of the loop.
1994 end_page_writeback(bd_page);
1996 nilfs_end_page_io(fs_page, 0);
1998 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
2000 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
2002 if (nilfs_doing_gc()) {
2003 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
2005 nilfs_commit_gcdat_inode(nilfs);
2007 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
2009 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
2011 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
2012 nilfs_set_next_segment(nilfs, segbuf);
2015 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
2016 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
2017 set_nilfs_sb_dirty(nilfs);
2019 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
2020 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2021 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2022 nilfs_segctor_clear_metadata_dirty(sci);
2024 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2027 static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
2031 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
2033 nilfs_segctor_complete_write(sci);
2034 nilfs_destroy_logs(&sci->sc_write_logs);
2039 static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
2040 struct nilfs_sb_info *sbi)
2042 struct nilfs_inode_info *ii, *n;
2043 __u64 cno = sbi->s_nilfs->ns_cno;
2045 spin_lock(&sbi->s_inode_lock);
2047 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
2049 struct buffer_head *ibh;
2052 spin_unlock(&sbi->s_inode_lock);
2053 err = nilfs_ifile_get_inode_block(
2054 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
2055 if (unlikely(err)) {
2056 nilfs_warning(sbi->s_super, __func__,
2057 "failed to get inode block.\n");
2060 nilfs_mdt_mark_buffer_dirty(ibh);
2061 nilfs_mdt_mark_dirty(sbi->s_ifile);
2062 spin_lock(&sbi->s_inode_lock);
2063 if (likely(!ii->i_bh))
2071 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2072 set_bit(NILFS_I_BUSY, &ii->i_state);
2073 list_del(&ii->i_dirty);
2074 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2076 spin_unlock(&sbi->s_inode_lock);
2078 NILFS_I(sbi->s_ifile)->i_cno = cno;
2083 static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2084 struct nilfs_sb_info *sbi)
2086 struct nilfs_transaction_info *ti = current->journal_info;
2087 struct nilfs_inode_info *ii, *n;
2088 __u64 cno = sbi->s_nilfs->ns_cno;
2090 spin_lock(&sbi->s_inode_lock);
2091 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2092 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2093 test_bit(NILFS_I_DIRTY, &ii->i_state)) {
2094 /* The current checkpoint number (=nilfs->ns_cno) is
2095 changed between check-in and check-out only if the
2096 super root is written out. So, we can update i_cno
2097 for the inodes that remain in the dirty list. */
2101 clear_bit(NILFS_I_BUSY, &ii->i_state);
2104 list_del(&ii->i_dirty);
2105 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2107 spin_unlock(&sbi->s_inode_lock);
2111 * Main procedure of segment constructor
2113 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2115 struct nilfs_sb_info *sbi = sci->sc_sbi;
2116 struct the_nilfs *nilfs = sbi->s_nilfs;
2117 struct page *failed_page;
2118 int err, has_sr = 0;
2120 sci->sc_stage.scnt = NILFS_ST_INIT;
2122 err = nilfs_segctor_check_in_files(sci, sbi);
2126 if (nilfs_test_metadata_dirty(sbi))
2127 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2129 if (nilfs_segctor_clean(sci))
2133 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2135 err = nilfs_segctor_begin_construction(sci, nilfs);
2139 /* Update time stamp */
2140 sci->sc_seg_ctime = get_seconds();
2142 err = nilfs_segctor_collect(sci, nilfs, mode);
2146 has_sr = (sci->sc_super_root != NULL);
2148 /* Avoid empty segment */
2149 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
2150 NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
2151 nilfs_segctor_abort_construction(sci, nilfs, 1);
2155 err = nilfs_segctor_assign(sci, mode);
2159 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2160 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2163 err = nilfs_segctor_fill_in_checkpoint(sci);
2165 goto failed_to_write;
2167 nilfs_segctor_fill_in_super_root(sci, nilfs);
2169 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2171 /* Write partial segments */
2172 err = nilfs_segctor_prepare_write(sci, &failed_page);
2174 nilfs_abort_logs(&sci->sc_segbufs, failed_page,
2175 sci->sc_super_root, err);
2176 goto failed_to_write;
2178 nilfs_segctor_fill_in_checksums(sci, nilfs->ns_crc_seed);
2180 err = nilfs_segctor_write(sci, nilfs);
2182 goto failed_to_write;
2184 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2185 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2187 * At this point, we avoid double buffering
2188 * for blocksize < pagesize because page dirty
2189 * flag is turned off during write and dirty
2190 * buffers are not properly collected for
2191 * pages crossing over segments.
2193 err = nilfs_segctor_wait(sci);
2195 goto failed_to_write;
2197 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2199 sci->sc_super_root = NULL;
2202 nilfs_segctor_check_out_files(sci, sbi);
2206 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2207 nilfs_redirty_inodes(&sci->sc_dirty_files);
2210 if (nilfs_doing_gc())
2211 nilfs_redirty_inodes(&sci->sc_gc_inodes);
2212 nilfs_segctor_abort_construction(sci, nilfs, err);
2217 * nilfs_segctor_start_timer - set timer of background write
2218 * @sci: nilfs_sc_info
2220 * If the timer has already been set, it ignores the new request.
2221 * This function MUST be called within a section locking the segment
2224 static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2226 spin_lock(&sci->sc_state_lock);
2227 if (sci->sc_timer && !(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2228 sci->sc_timer->expires = jiffies + sci->sc_interval;
2229 add_timer(sci->sc_timer);
2230 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2232 spin_unlock(&sci->sc_state_lock);
2235 static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2237 spin_lock(&sci->sc_state_lock);
2238 if (!(sci->sc_flush_request & (1 << bn))) {
2239 unsigned long prev_req = sci->sc_flush_request;
2241 sci->sc_flush_request |= (1 << bn);
2243 wake_up(&sci->sc_wait_daemon);
2245 spin_unlock(&sci->sc_state_lock);
2249 * nilfs_flush_segment - trigger a segment construction for resource control
2251 * @ino: inode number of the file to be flushed out.
2253 void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2255 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2256 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2258 if (!sci || nilfs_doing_construction())
2260 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2261 /* assign bit 0 to data files */
2264 struct nilfs_segctor_wait_request {
2271 static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2273 struct nilfs_segctor_wait_request wait_req;
2276 spin_lock(&sci->sc_state_lock);
2277 init_wait(&wait_req.wq);
2279 atomic_set(&wait_req.done, 0);
2280 wait_req.seq = ++sci->sc_seq_request;
2281 spin_unlock(&sci->sc_state_lock);
2283 init_waitqueue_entry(&wait_req.wq, current);
2284 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2285 set_current_state(TASK_INTERRUPTIBLE);
2286 wake_up(&sci->sc_wait_daemon);
2289 if (atomic_read(&wait_req.done)) {
2293 if (!signal_pending(current)) {
2300 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2304 static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2306 struct nilfs_segctor_wait_request *wrq, *n;
2307 unsigned long flags;
2309 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2310 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2312 if (!atomic_read(&wrq->done) &&
2313 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2315 atomic_set(&wrq->done, 1);
2317 if (atomic_read(&wrq->done)) {
2318 wrq->wq.func(&wrq->wq,
2319 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2323 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2327 * nilfs_construct_segment - construct a logical segment
2330 * Return Value: On success, 0 is retured. On errors, one of the following
2331 * negative error code is returned.
2333 * %-EROFS - Read only filesystem.
2337 * %-ENOSPC - No space left on device (only in a panic state).
2339 * %-ERESTARTSYS - Interrupted.
2341 * %-ENOMEM - Insufficient memory available.
2343 int nilfs_construct_segment(struct super_block *sb)
2345 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2346 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2347 struct nilfs_transaction_info *ti;
2353 /* A call inside transactions causes a deadlock. */
2354 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2356 err = nilfs_segctor_sync(sci);
2361 * nilfs_construct_dsync_segment - construct a data-only logical segment
2363 * @inode: inode whose data blocks should be written out
2364 * @start: start byte offset
2365 * @end: end byte offset (inclusive)
2367 * Return Value: On success, 0 is retured. On errors, one of the following
2368 * negative error code is returned.
2370 * %-EROFS - Read only filesystem.
2374 * %-ENOSPC - No space left on device (only in a panic state).
2376 * %-ERESTARTSYS - Interrupted.
2378 * %-ENOMEM - Insufficient memory available.
2380 int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2381 loff_t start, loff_t end)
2383 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2384 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2385 struct nilfs_inode_info *ii;
2386 struct nilfs_transaction_info ti;
2392 nilfs_transaction_lock(sbi, &ti, 0);
2394 ii = NILFS_I(inode);
2395 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2396 nilfs_test_opt(sbi, STRICT_ORDER) ||
2397 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2398 nilfs_discontinued(sbi->s_nilfs)) {
2399 nilfs_transaction_unlock(sbi);
2400 err = nilfs_segctor_sync(sci);
2404 spin_lock(&sbi->s_inode_lock);
2405 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2406 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2407 spin_unlock(&sbi->s_inode_lock);
2408 nilfs_transaction_unlock(sbi);
2411 spin_unlock(&sbi->s_inode_lock);
2412 sci->sc_dsync_inode = ii;
2413 sci->sc_dsync_start = start;
2414 sci->sc_dsync_end = end;
2416 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2418 nilfs_transaction_unlock(sbi);
2422 #define FLUSH_FILE_BIT (0x1) /* data file only */
2423 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2426 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2427 * @sci: segment constructor object
2429 static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
2431 spin_lock(&sci->sc_state_lock);
2432 sci->sc_seq_accepted = sci->sc_seq_request;
2433 spin_unlock(&sci->sc_state_lock);
2436 del_timer_sync(sci->sc_timer);
2440 * nilfs_segctor_notify - notify the result of request to caller threads
2441 * @sci: segment constructor object
2442 * @mode: mode of log forming
2443 * @err: error code to be notified
2445 static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
2447 /* Clear requests (even when the construction failed) */
2448 spin_lock(&sci->sc_state_lock);
2450 if (mode == SC_LSEG_SR) {
2451 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
2452 sci->sc_seq_done = sci->sc_seq_accepted;
2453 nilfs_segctor_wakeup(sci, err);
2454 sci->sc_flush_request = 0;
2456 if (mode == SC_FLUSH_FILE)
2457 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
2458 else if (mode == SC_FLUSH_DAT)
2459 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2461 /* re-enable timer if checkpoint creation was not done */
2462 if (sci->sc_timer && (sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2463 time_before(jiffies, sci->sc_timer->expires))
2464 add_timer(sci->sc_timer);
2466 spin_unlock(&sci->sc_state_lock);
2470 * nilfs_segctor_construct - form logs and write them to disk
2471 * @sci: segment constructor object
2472 * @mode: mode of log forming
2474 static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
2476 struct nilfs_sb_info *sbi = sci->sc_sbi;
2477 struct the_nilfs *nilfs = sbi->s_nilfs;
2480 nilfs_segctor_accept(sci);
2482 if (nilfs_discontinued(nilfs))
2484 if (!nilfs_segctor_confirm(sci))
2485 err = nilfs_segctor_do_construct(sci, mode);
2488 if (mode != SC_FLUSH_DAT)
2489 atomic_set(&nilfs->ns_ndirtyblks, 0);
2490 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2491 nilfs_discontinued(nilfs)) {
2492 down_write(&nilfs->ns_sem);
2493 err = nilfs_commit_super(
2494 sbi, nilfs_altsb_need_update(nilfs));
2495 up_write(&nilfs->ns_sem);
2499 nilfs_segctor_notify(sci, mode, err);
2503 static void nilfs_construction_timeout(unsigned long data)
2505 struct task_struct *p = (struct task_struct *)data;
2510 nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2512 struct nilfs_inode_info *ii, *n;
2514 list_for_each_entry_safe(ii, n, head, i_dirty) {
2515 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2517 hlist_del_init(&ii->vfs_inode.i_hash);
2518 list_del_init(&ii->i_dirty);
2519 nilfs_clear_gcinode(&ii->vfs_inode);
2523 int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2526 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2527 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2528 struct the_nilfs *nilfs = sbi->s_nilfs;
2529 struct nilfs_transaction_info ti;
2535 nilfs_transaction_lock(sbi, &ti, 1);
2537 err = nilfs_init_gcdat_inode(nilfs);
2541 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
2545 sci->sc_freesegs = kbufs[4];
2546 sci->sc_nfreesegs = argv[4].v_nmembs;
2547 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
2550 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
2551 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
2556 nilfs_warning(sb, __func__,
2557 "segment construction failed. (err=%d)", err);
2558 set_current_state(TASK_INTERRUPTIBLE);
2559 schedule_timeout(sci->sc_interval);
2561 if (nilfs_test_opt(sbi, DISCARD)) {
2562 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2566 "NILFS warning: error %d on discard request, "
2567 "turning discards off for the device\n", ret);
2568 nilfs_clear_opt(sbi, DISCARD);
2573 sci->sc_freesegs = NULL;
2574 sci->sc_nfreesegs = 0;
2575 nilfs_clear_gcdat_inode(nilfs);
2576 nilfs_transaction_unlock(sbi);
2580 static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2582 struct nilfs_sb_info *sbi = sci->sc_sbi;
2583 struct nilfs_transaction_info ti;
2585 nilfs_transaction_lock(sbi, &ti, 0);
2586 nilfs_segctor_construct(sci, mode);
2589 * Unclosed segment should be retried. We do this using sc_timer.
2590 * Timeout of sc_timer will invoke complete construction which leads
2591 * to close the current logical segment.
2593 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2594 nilfs_segctor_start_timer(sci);
2596 nilfs_transaction_unlock(sbi);
2599 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2604 spin_lock(&sci->sc_state_lock);
2605 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2606 SC_FLUSH_DAT : SC_FLUSH_FILE;
2607 spin_unlock(&sci->sc_state_lock);
2610 err = nilfs_segctor_do_construct(sci, mode);
2612 spin_lock(&sci->sc_state_lock);
2613 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2614 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2615 spin_unlock(&sci->sc_state_lock);
2617 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2620 static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2622 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2623 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2624 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2625 return SC_FLUSH_FILE;
2626 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2627 return SC_FLUSH_DAT;
2633 * nilfs_segctor_thread - main loop of the segment constructor thread.
2634 * @arg: pointer to a struct nilfs_sc_info.
2636 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2637 * to execute segment constructions.
2639 static int nilfs_segctor_thread(void *arg)
2641 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
2642 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
2643 struct timer_list timer;
2647 timer.data = (unsigned long)current;
2648 timer.function = nilfs_construction_timeout;
2649 sci->sc_timer = &timer;
2652 sci->sc_task = current;
2653 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2655 "segctord starting. Construction interval = %lu seconds, "
2656 "CP frequency < %lu seconds\n",
2657 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2659 spin_lock(&sci->sc_state_lock);
2664 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2667 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2669 else if (!sci->sc_flush_request)
2672 mode = nilfs_segctor_flush_mode(sci);
2674 spin_unlock(&sci->sc_state_lock);
2675 nilfs_segctor_thread_construct(sci, mode);
2676 spin_lock(&sci->sc_state_lock);
2681 if (freezing(current)) {
2682 spin_unlock(&sci->sc_state_lock);
2684 spin_lock(&sci->sc_state_lock);
2687 int should_sleep = 1;
2689 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2690 TASK_INTERRUPTIBLE);
2692 if (sci->sc_seq_request != sci->sc_seq_done)
2694 else if (sci->sc_flush_request)
2696 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2697 should_sleep = time_before(jiffies,
2698 sci->sc_timer->expires);
2701 spin_unlock(&sci->sc_state_lock);
2703 spin_lock(&sci->sc_state_lock);
2705 finish_wait(&sci->sc_wait_daemon, &wait);
2706 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2707 time_after_eq(jiffies, sci->sc_timer->expires));
2709 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
2710 set_nilfs_discontinued(nilfs);
2715 spin_unlock(&sci->sc_state_lock);
2716 del_timer_sync(sci->sc_timer);
2717 sci->sc_timer = NULL;
2720 sci->sc_task = NULL;
2721 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2725 static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2727 struct task_struct *t;
2729 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2731 int err = PTR_ERR(t);
2733 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2737 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2741 static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2743 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2745 while (sci->sc_task) {
2746 wake_up(&sci->sc_wait_daemon);
2747 spin_unlock(&sci->sc_state_lock);
2748 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2749 spin_lock(&sci->sc_state_lock);
2753 static int nilfs_segctor_init(struct nilfs_sc_info *sci)
2755 sci->sc_seq_done = sci->sc_seq_request;
2757 return nilfs_segctor_start_thread(sci);
2761 * Setup & clean-up functions
2763 static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2765 struct nilfs_sc_info *sci;
2767 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2772 sci->sc_super = sbi->s_super;
2774 init_waitqueue_head(&sci->sc_wait_request);
2775 init_waitqueue_head(&sci->sc_wait_daemon);
2776 init_waitqueue_head(&sci->sc_wait_task);
2777 spin_lock_init(&sci->sc_state_lock);
2778 INIT_LIST_HEAD(&sci->sc_dirty_files);
2779 INIT_LIST_HEAD(&sci->sc_segbufs);
2780 INIT_LIST_HEAD(&sci->sc_write_logs);
2781 INIT_LIST_HEAD(&sci->sc_gc_inodes);
2782 INIT_LIST_HEAD(&sci->sc_copied_buffers);
2784 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2785 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2786 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2788 if (sbi->s_interval)
2789 sci->sc_interval = sbi->s_interval;
2790 if (sbi->s_watermark)
2791 sci->sc_watermark = sbi->s_watermark;
2795 static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2797 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2799 /* The segctord thread was stopped and its timer was removed.
2800 But some tasks remain. */
2802 struct nilfs_sb_info *sbi = sci->sc_sbi;
2803 struct nilfs_transaction_info ti;
2805 nilfs_transaction_lock(sbi, &ti, 0);
2806 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
2807 nilfs_transaction_unlock(sbi);
2809 } while (ret && retrycount-- > 0);
2813 * nilfs_segctor_destroy - destroy the segment constructor.
2814 * @sci: nilfs_sc_info
2816 * nilfs_segctor_destroy() kills the segctord thread and frees
2817 * the nilfs_sc_info struct.
2818 * Caller must hold the segment semaphore.
2820 static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2822 struct nilfs_sb_info *sbi = sci->sc_sbi;
2825 up_write(&sbi->s_nilfs->ns_segctor_sem);
2827 spin_lock(&sci->sc_state_lock);
2828 nilfs_segctor_kill_thread(sci);
2829 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2830 || sci->sc_seq_request != sci->sc_seq_done);
2831 spin_unlock(&sci->sc_state_lock);
2833 if (flag || !nilfs_segctor_confirm(sci))
2834 nilfs_segctor_write_out(sci);
2836 WARN_ON(!list_empty(&sci->sc_copied_buffers));
2838 if (!list_empty(&sci->sc_dirty_files)) {
2839 nilfs_warning(sbi->s_super, __func__,
2840 "dirty file(s) after the final construction\n");
2841 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2844 WARN_ON(!list_empty(&sci->sc_segbufs));
2845 WARN_ON(!list_empty(&sci->sc_write_logs));
2847 down_write(&sbi->s_nilfs->ns_segctor_sem);
2853 * nilfs_attach_segment_constructor - attach a segment constructor
2854 * @sbi: nilfs_sb_info
2856 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
2857 * initializes it, and starts the segment constructor.
2859 * Return Value: On success, 0 is returned. On error, one of the following
2860 * negative error code is returned.
2862 * %-ENOMEM - Insufficient memory available.
2864 int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
2866 struct the_nilfs *nilfs = sbi->s_nilfs;
2869 if (NILFS_SC(sbi)) {
2871 * This happens if the filesystem was remounted
2872 * read/write after nilfs_error degenerated it into a
2875 nilfs_detach_segment_constructor(sbi);
2878 sbi->s_sc_info = nilfs_segctor_new(sbi);
2879 if (!sbi->s_sc_info)
2882 nilfs_attach_writer(nilfs, sbi);
2883 err = nilfs_segctor_init(NILFS_SC(sbi));
2885 nilfs_detach_writer(nilfs, sbi);
2886 kfree(sbi->s_sc_info);
2887 sbi->s_sc_info = NULL;
2893 * nilfs_detach_segment_constructor - destroy the segment constructor
2894 * @sbi: nilfs_sb_info
2896 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2897 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2899 void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2901 struct the_nilfs *nilfs = sbi->s_nilfs;
2902 LIST_HEAD(garbage_list);
2904 down_write(&nilfs->ns_segctor_sem);
2905 if (NILFS_SC(sbi)) {
2906 nilfs_segctor_destroy(NILFS_SC(sbi));
2907 sbi->s_sc_info = NULL;
2910 /* Force to free the list of dirty files */
2911 spin_lock(&sbi->s_inode_lock);
2912 if (!list_empty(&sbi->s_dirty_files)) {
2913 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2914 nilfs_warning(sbi->s_super, __func__,
2915 "Non empty dirty list after the last "
2916 "segment construction\n");
2918 spin_unlock(&sbi->s_inode_lock);
2919 up_write(&nilfs->ns_segctor_sem);
2921 nilfs_dispose_list(sbi, &garbage_list, 1);
2922 nilfs_detach_writer(nilfs, sbi);