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, &segbuf->sb_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 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
956 if (segbuf->sb_super_root)
957 nilfs_fill_in_super_root_crc(segbuf->sb_super_root,
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;
968 struct nilfs_super_root *raw_sr;
969 unsigned isz = nilfs->ns_inode_size;
971 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
972 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
974 raw_sr->sr_bytes = cpu_to_le16(NILFS_SR_BYTES);
975 raw_sr->sr_nongc_ctime
976 = cpu_to_le64(nilfs_doing_gc() ?
977 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
978 raw_sr->sr_flags = 0;
980 nilfs_write_inode_common(nilfs_dat_inode(nilfs), (void *)raw_sr +
981 NILFS_SR_DAT_OFFSET(isz), 1);
982 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
983 NILFS_SR_CPFILE_OFFSET(isz), 1);
984 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
985 NILFS_SR_SUFILE_OFFSET(isz), 1);
988 static void nilfs_redirty_inodes(struct list_head *head)
990 struct nilfs_inode_info *ii;
992 list_for_each_entry(ii, head, i_dirty) {
993 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
994 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
998 static void nilfs_drop_collected_inodes(struct list_head *head)
1000 struct nilfs_inode_info *ii;
1002 list_for_each_entry(ii, head, i_dirty) {
1003 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
1006 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
1007 set_bit(NILFS_I_UPDATED, &ii->i_state);
1011 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
1012 struct inode *inode,
1013 struct list_head *listp,
1014 int (*collect)(struct nilfs_sc_info *,
1015 struct buffer_head *,
1018 struct buffer_head *bh, *n;
1022 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1023 list_del_init(&bh->b_assoc_buffers);
1024 err = collect(sci, bh, inode);
1027 goto dispose_buffers;
1033 while (!list_empty(listp)) {
1034 bh = list_entry(listp->next, struct buffer_head,
1036 list_del_init(&bh->b_assoc_buffers);
1042 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1044 /* Remaining number of blocks within segment buffer */
1045 return sci->sc_segbuf_nblocks -
1046 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1049 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1050 struct inode *inode,
1051 struct nilfs_sc_operations *sc_ops)
1053 LIST_HEAD(data_buffers);
1054 LIST_HEAD(node_buffers);
1057 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1058 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1060 n = nilfs_lookup_dirty_data_buffers(
1061 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1063 err = nilfs_segctor_apply_buffers(
1064 sci, inode, &data_buffers,
1065 sc_ops->collect_data);
1066 BUG_ON(!err); /* always receive -E2BIG or true error */
1070 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1072 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1073 err = nilfs_segctor_apply_buffers(
1074 sci, inode, &data_buffers, sc_ops->collect_data);
1075 if (unlikely(err)) {
1076 /* dispose node list */
1077 nilfs_segctor_apply_buffers(
1078 sci, inode, &node_buffers, NULL);
1081 sci->sc_stage.flags |= NILFS_CF_NODE;
1084 err = nilfs_segctor_apply_buffers(
1085 sci, inode, &node_buffers, sc_ops->collect_node);
1089 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1090 err = nilfs_segctor_apply_buffers(
1091 sci, inode, &node_buffers, sc_ops->collect_bmap);
1095 nilfs_segctor_end_finfo(sci, inode);
1096 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1102 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1103 struct inode *inode)
1105 LIST_HEAD(data_buffers);
1106 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1109 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1110 sci->sc_dsync_start,
1113 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1114 nilfs_collect_file_data);
1116 nilfs_segctor_end_finfo(sci, inode);
1118 /* always receive -E2BIG or true error if n > rest */
1123 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1125 struct nilfs_sb_info *sbi = sci->sc_sbi;
1126 struct the_nilfs *nilfs = sbi->s_nilfs;
1127 struct list_head *head;
1128 struct nilfs_inode_info *ii;
1132 switch (sci->sc_stage.scnt) {
1135 sci->sc_stage.flags = 0;
1137 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1138 sci->sc_nblk_inc = 0;
1139 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1140 if (mode == SC_LSEG_DSYNC) {
1141 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1146 sci->sc_stage.dirty_file_ptr = NULL;
1147 sci->sc_stage.gc_inode_ptr = NULL;
1148 if (mode == SC_FLUSH_DAT) {
1149 sci->sc_stage.scnt = NILFS_ST_DAT;
1152 sci->sc_stage.scnt++; /* Fall through */
1154 if (nilfs_doing_gc()) {
1155 head = &sci->sc_gc_inodes;
1156 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1158 list_for_each_entry_continue(ii, head, i_dirty) {
1159 err = nilfs_segctor_scan_file(
1160 sci, &ii->vfs_inode,
1161 &nilfs_sc_file_ops);
1162 if (unlikely(err)) {
1163 sci->sc_stage.gc_inode_ptr = list_entry(
1165 struct nilfs_inode_info,
1169 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1171 sci->sc_stage.gc_inode_ptr = NULL;
1173 sci->sc_stage.scnt++; /* Fall through */
1175 head = &sci->sc_dirty_files;
1176 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1178 list_for_each_entry_continue(ii, head, i_dirty) {
1179 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1181 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1182 &nilfs_sc_file_ops);
1183 if (unlikely(err)) {
1184 sci->sc_stage.dirty_file_ptr =
1185 list_entry(ii->i_dirty.prev,
1186 struct nilfs_inode_info,
1190 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1191 /* XXX: required ? */
1193 sci->sc_stage.dirty_file_ptr = NULL;
1194 if (mode == SC_FLUSH_FILE) {
1195 sci->sc_stage.scnt = NILFS_ST_DONE;
1198 sci->sc_stage.scnt++;
1199 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1201 case NILFS_ST_IFILE:
1202 err = nilfs_segctor_scan_file(sci, sbi->s_ifile,
1203 &nilfs_sc_file_ops);
1206 sci->sc_stage.scnt++;
1207 /* Creating a checkpoint */
1208 err = nilfs_segctor_create_checkpoint(sci);
1212 case NILFS_ST_CPFILE:
1213 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1214 &nilfs_sc_file_ops);
1217 sci->sc_stage.scnt++; /* Fall through */
1218 case NILFS_ST_SUFILE:
1219 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1220 sci->sc_nfreesegs, &ndone);
1221 if (unlikely(err)) {
1222 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1223 sci->sc_freesegs, ndone,
1227 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1229 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1230 &nilfs_sc_file_ops);
1233 sci->sc_stage.scnt++; /* Fall through */
1236 err = nilfs_segctor_scan_file(sci, nilfs_dat_inode(nilfs),
1240 if (mode == SC_FLUSH_DAT) {
1241 sci->sc_stage.scnt = NILFS_ST_DONE;
1244 sci->sc_stage.scnt++; /* Fall through */
1246 if (mode == SC_LSEG_SR) {
1247 /* Appending a super root */
1248 err = nilfs_segctor_add_super_root(sci);
1252 /* End of a logical segment */
1253 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1254 sci->sc_stage.scnt = NILFS_ST_DONE;
1256 case NILFS_ST_DSYNC:
1258 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
1259 ii = sci->sc_dsync_inode;
1260 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1263 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1266 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1267 sci->sc_stage.scnt = NILFS_ST_DONE;
1280 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1281 * @sci: nilfs_sc_info
1282 * @nilfs: nilfs object
1284 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1285 struct the_nilfs *nilfs)
1287 struct nilfs_segment_buffer *segbuf, *prev;
1291 segbuf = nilfs_segbuf_new(sci->sc_super);
1292 if (unlikely(!segbuf))
1295 if (list_empty(&sci->sc_write_logs)) {
1296 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1297 nilfs->ns_pseg_offset, nilfs);
1298 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1299 nilfs_shift_to_next_segment(nilfs);
1300 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1303 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1304 nextnum = nilfs->ns_nextnum;
1306 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1307 /* Start from the head of a new full segment */
1311 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1312 nilfs_segbuf_map_cont(segbuf, prev);
1313 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1314 nextnum = prev->sb_nextnum;
1316 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1317 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1318 segbuf->sb_sum.seg_seq++;
1323 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1328 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1332 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1334 BUG_ON(!list_empty(&sci->sc_segbufs));
1335 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1336 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
1340 nilfs_segbuf_free(segbuf);
1344 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1345 struct the_nilfs *nilfs, int nadd)
1347 struct nilfs_segment_buffer *segbuf, *prev;
1348 struct inode *sufile = nilfs->ns_sufile;
1353 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1355 * Since the segment specified with nextnum might be allocated during
1356 * the previous construction, the buffer including its segusage may
1357 * not be dirty. The following call ensures that the buffer is dirty
1358 * and will pin the buffer on memory until the sufile is written.
1360 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
1364 for (i = 0; i < nadd; i++) {
1365 /* extend segment info */
1367 segbuf = nilfs_segbuf_new(sci->sc_super);
1368 if (unlikely(!segbuf))
1371 /* map this buffer to region of segment on-disk */
1372 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1373 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1375 /* allocate the next next full segment */
1376 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1380 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1381 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1383 list_add_tail(&segbuf->sb_list, &list);
1386 list_splice_tail(&list, &sci->sc_segbufs);
1390 nilfs_segbuf_free(segbuf);
1392 list_for_each_entry(segbuf, &list, sb_list) {
1393 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1394 WARN_ON(ret); /* never fails */
1396 nilfs_destroy_logs(&list);
1400 static void nilfs_free_incomplete_logs(struct list_head *logs,
1401 struct the_nilfs *nilfs)
1403 struct nilfs_segment_buffer *segbuf, *prev;
1404 struct inode *sufile = nilfs->ns_sufile;
1407 segbuf = NILFS_FIRST_SEGBUF(logs);
1408 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
1409 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1410 WARN_ON(ret); /* never fails */
1412 if (atomic_read(&segbuf->sb_err)) {
1413 /* Case 1: The first segment failed */
1414 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1415 /* Case 1a: Partial segment appended into an existing
1417 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1418 segbuf->sb_fseg_end);
1419 else /* Case 1b: New full segment */
1420 set_nilfs_discontinued(nilfs);
1424 list_for_each_entry_continue(segbuf, logs, sb_list) {
1425 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1426 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1427 WARN_ON(ret); /* never fails */
1429 if (atomic_read(&segbuf->sb_err) &&
1430 segbuf->sb_segnum != nilfs->ns_nextnum)
1431 /* Case 2: extended segment (!= next) failed */
1432 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1437 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1438 struct inode *sufile)
1440 struct nilfs_segment_buffer *segbuf;
1441 unsigned long live_blocks;
1444 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1445 live_blocks = segbuf->sb_sum.nblocks +
1446 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
1447 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1450 WARN_ON(ret); /* always succeed because the segusage is dirty */
1454 static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
1456 struct nilfs_segment_buffer *segbuf;
1459 segbuf = NILFS_FIRST_SEGBUF(logs);
1460 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1461 segbuf->sb_pseg_start -
1462 segbuf->sb_fseg_start, 0);
1463 WARN_ON(ret); /* always succeed because the segusage is dirty */
1465 list_for_each_entry_continue(segbuf, logs, sb_list) {
1466 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1468 WARN_ON(ret); /* always succeed */
1472 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1473 struct nilfs_segment_buffer *last,
1474 struct inode *sufile)
1476 struct nilfs_segment_buffer *segbuf = last;
1479 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1480 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1481 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1484 nilfs_truncate_logs(&sci->sc_segbufs, last);
1488 static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1489 struct the_nilfs *nilfs, int mode)
1491 struct nilfs_cstage prev_stage = sci->sc_stage;
1494 /* Collection retry loop */
1496 sci->sc_nblk_this_inc = 0;
1497 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1499 err = nilfs_segctor_reset_segment_buffer(sci);
1503 err = nilfs_segctor_collect_blocks(sci, mode);
1504 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1508 if (unlikely(err != -E2BIG))
1511 /* The current segment is filled up */
1512 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1515 nilfs_clear_logs(&sci->sc_segbufs);
1517 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1521 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1522 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1526 WARN_ON(err); /* do not happen */
1528 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1529 sci->sc_stage = prev_stage;
1531 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1538 static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1539 struct buffer_head *new_bh)
1541 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1543 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1544 /* The caller must release old_bh */
1548 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1549 struct nilfs_segment_buffer *segbuf,
1552 struct inode *inode = NULL;
1554 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1555 unsigned long nblocks = 0, ndatablk = 0;
1556 struct nilfs_sc_operations *sc_op = NULL;
1557 struct nilfs_segsum_pointer ssp;
1558 struct nilfs_finfo *finfo = NULL;
1559 union nilfs_binfo binfo;
1560 struct buffer_head *bh, *bh_org;
1567 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1568 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1569 ssp.offset = sizeof(struct nilfs_segment_summary);
1571 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1572 if (bh == segbuf->sb_super_root)
1575 finfo = nilfs_segctor_map_segsum_entry(
1576 sci, &ssp, sizeof(*finfo));
1577 ino = le64_to_cpu(finfo->fi_ino);
1578 nblocks = le32_to_cpu(finfo->fi_nblocks);
1579 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1581 if (buffer_nilfs_node(bh))
1582 inode = NILFS_BTNC_I(bh->b_page->mapping);
1584 inode = NILFS_AS_I(bh->b_page->mapping);
1586 if (mode == SC_LSEG_DSYNC)
1587 sc_op = &nilfs_sc_dsync_ops;
1588 else if (ino == NILFS_DAT_INO)
1589 sc_op = &nilfs_sc_dat_ops;
1590 else /* file blocks */
1591 sc_op = &nilfs_sc_file_ops;
1595 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1598 nilfs_list_replace_buffer(bh_org, bh);
1604 sc_op->write_data_binfo(sci, &ssp, &binfo);
1606 sc_op->write_node_binfo(sci, &ssp, &binfo);
1609 if (--nblocks == 0) {
1613 } else if (ndatablk > 0)
1620 err = nilfs_handle_bmap_error(err, __func__, inode, sci->sc_super);
1624 static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1626 struct nilfs_segment_buffer *segbuf;
1629 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1630 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1633 nilfs_segbuf_fill_in_segsum(segbuf);
1639 nilfs_copy_replace_page_buffers(struct page *page, struct list_head *out)
1641 struct page *clone_page;
1642 struct buffer_head *bh, *head, *bh2;
1645 bh = head = page_buffers(page);
1647 clone_page = nilfs_alloc_private_page(bh->b_bdev, bh->b_size, 0);
1648 if (unlikely(!clone_page))
1651 bh2 = page_buffers(clone_page);
1652 kaddr = kmap_atomic(page, KM_USER0);
1654 if (list_empty(&bh->b_assoc_buffers))
1657 page_cache_get(clone_page); /* for each bh */
1658 memcpy(bh2->b_data, kaddr + bh_offset(bh), bh2->b_size);
1659 bh2->b_blocknr = bh->b_blocknr;
1660 list_replace(&bh->b_assoc_buffers, &bh2->b_assoc_buffers);
1661 list_add_tail(&bh->b_assoc_buffers, out);
1662 } while (bh = bh->b_this_page, bh2 = bh2->b_this_page, bh != head);
1663 kunmap_atomic(kaddr, KM_USER0);
1665 if (!TestSetPageWriteback(clone_page))
1666 inc_zone_page_state(clone_page, NR_WRITEBACK);
1667 unlock_page(clone_page);
1672 static int nilfs_test_page_to_be_frozen(struct page *page)
1674 struct address_space *mapping = page->mapping;
1676 if (!mapping || !mapping->host || S_ISDIR(mapping->host->i_mode))
1679 if (page_mapped(page)) {
1680 ClearPageChecked(page);
1683 return PageChecked(page);
1686 static int nilfs_begin_page_io(struct page *page, struct list_head *out)
1688 if (!page || PageWriteback(page))
1689 /* For split b-tree node pages, this function may be called
1690 twice. We ignore the 2nd or later calls by this check. */
1694 clear_page_dirty_for_io(page);
1695 set_page_writeback(page);
1698 if (nilfs_test_page_to_be_frozen(page)) {
1699 int err = nilfs_copy_replace_page_buffers(page, out);
1706 static int nilfs_segctor_prepare_write(struct nilfs_sc_info *sci,
1707 struct page **failed_page)
1709 struct nilfs_segment_buffer *segbuf;
1710 struct page *bd_page = NULL, *fs_page = NULL;
1711 struct list_head *list = &sci->sc_copied_buffers;
1714 *failed_page = NULL;
1715 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1716 struct buffer_head *bh;
1718 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1720 if (bh->b_page != bd_page) {
1723 clear_page_dirty_for_io(bd_page);
1724 set_page_writeback(bd_page);
1725 unlock_page(bd_page);
1727 bd_page = bh->b_page;
1731 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1733 if (bh == segbuf->sb_super_root) {
1734 if (bh->b_page != bd_page) {
1736 clear_page_dirty_for_io(bd_page);
1737 set_page_writeback(bd_page);
1738 unlock_page(bd_page);
1739 bd_page = bh->b_page;
1743 if (bh->b_page != fs_page) {
1744 err = nilfs_begin_page_io(fs_page, list);
1745 if (unlikely(err)) {
1746 *failed_page = fs_page;
1749 fs_page = bh->b_page;
1755 clear_page_dirty_for_io(bd_page);
1756 set_page_writeback(bd_page);
1757 unlock_page(bd_page);
1759 err = nilfs_begin_page_io(fs_page, list);
1761 *failed_page = fs_page;
1766 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
1767 struct the_nilfs *nilfs)
1771 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
1772 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1776 static void __nilfs_end_page_io(struct page *page, int err)
1779 if (!nilfs_page_buffers_clean(page))
1780 __set_page_dirty_nobuffers(page);
1781 ClearPageError(page);
1783 __set_page_dirty_nobuffers(page);
1787 if (buffer_nilfs_allocated(page_buffers(page))) {
1788 if (TestClearPageWriteback(page))
1789 dec_zone_page_state(page, NR_WRITEBACK);
1791 end_page_writeback(page);
1794 static void nilfs_end_page_io(struct page *page, int err)
1799 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
1801 * For b-tree node pages, this function may be called twice
1802 * or more because they might be split in a segment.
1804 if (PageDirty(page)) {
1806 * For pages holding split b-tree node buffers, dirty
1807 * flag on the buffers may be cleared discretely.
1808 * In that case, the page is once redirtied for
1809 * remaining buffers, and it must be cancelled if
1810 * all the buffers get cleaned later.
1813 if (nilfs_page_buffers_clean(page))
1814 __nilfs_clear_page_dirty(page);
1820 __nilfs_end_page_io(page, err);
1823 static void nilfs_clear_copied_buffers(struct list_head *list, int err)
1825 struct buffer_head *bh, *head;
1828 while (!list_empty(list)) {
1829 bh = list_entry(list->next, struct buffer_head,
1832 page_cache_get(page);
1833 head = bh = page_buffers(page);
1835 if (!list_empty(&bh->b_assoc_buffers)) {
1836 list_del_init(&bh->b_assoc_buffers);
1838 set_buffer_uptodate(bh);
1839 clear_buffer_dirty(bh);
1840 clear_buffer_nilfs_volatile(bh);
1842 brelse(bh); /* for b_assoc_buffers */
1844 } while ((bh = bh->b_this_page) != head);
1846 __nilfs_end_page_io(page, err);
1847 page_cache_release(page);
1851 static void nilfs_abort_logs(struct list_head *logs, struct page *failed_page,
1854 struct nilfs_segment_buffer *segbuf;
1855 struct page *bd_page = NULL, *fs_page = NULL;
1856 struct buffer_head *bh;
1858 if (list_empty(logs))
1861 list_for_each_entry(segbuf, logs, sb_list) {
1862 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1864 if (bh->b_page != bd_page) {
1866 end_page_writeback(bd_page);
1867 bd_page = bh->b_page;
1871 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1873 if (bh == segbuf->sb_super_root) {
1874 if (bh->b_page != bd_page) {
1875 end_page_writeback(bd_page);
1876 bd_page = bh->b_page;
1880 if (bh->b_page != fs_page) {
1881 nilfs_end_page_io(fs_page, err);
1882 if (fs_page && fs_page == failed_page)
1884 fs_page = bh->b_page;
1889 end_page_writeback(bd_page);
1891 nilfs_end_page_io(fs_page, err);
1894 static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1895 struct the_nilfs *nilfs, int err)
1900 list_splice_tail_init(&sci->sc_write_logs, &logs);
1901 ret = nilfs_wait_on_logs(&logs);
1902 nilfs_abort_logs(&logs, NULL, ret ? : err);
1904 list_splice_tail_init(&sci->sc_segbufs, &logs);
1905 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1906 nilfs_free_incomplete_logs(&logs, nilfs);
1907 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, err);
1909 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1910 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1914 WARN_ON(ret); /* do not happen */
1917 nilfs_destroy_logs(&logs);
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 = false;
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 == segbuf->sb_super_root) {
1968 if (bh->b_page != bd_page) {
1969 end_page_writeback(bd_page);
1970 bd_page = bh->b_page;
1975 if (bh->b_page != fs_page) {
1976 nilfs_end_page_io(fs_page, 0);
1977 fs_page = bh->b_page;
1981 if (!NILFS_SEG_SIMPLEX(&segbuf->sb_sum)) {
1982 if (NILFS_SEG_LOGBGN(&segbuf->sb_sum)) {
1983 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1984 sci->sc_lseg_stime = jiffies;
1986 if (NILFS_SEG_LOGEND(&segbuf->sb_sum))
1987 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1991 * Since pages may continue over multiple segment buffers,
1992 * end of the last page must be checked outside of the loop.
1995 end_page_writeback(bd_page);
1997 nilfs_end_page_io(fs_page, 0);
1999 nilfs_clear_copied_buffers(&sci->sc_copied_buffers, 0);
2001 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
2003 if (nilfs_doing_gc()) {
2004 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
2006 nilfs_commit_gcdat_inode(nilfs);
2008 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
2010 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
2012 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
2013 nilfs_set_next_segment(nilfs, segbuf);
2016 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
2017 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
2018 set_nilfs_sb_dirty(nilfs);
2020 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
2021 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2022 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2023 nilfs_segctor_clear_metadata_dirty(sci);
2025 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
2028 static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
2032 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
2034 nilfs_segctor_complete_write(sci);
2035 nilfs_destroy_logs(&sci->sc_write_logs);
2040 static int nilfs_segctor_check_in_files(struct nilfs_sc_info *sci,
2041 struct nilfs_sb_info *sbi)
2043 struct nilfs_inode_info *ii, *n;
2044 __u64 cno = sbi->s_nilfs->ns_cno;
2046 spin_lock(&sbi->s_inode_lock);
2048 list_for_each_entry_safe(ii, n, &sbi->s_dirty_files, i_dirty) {
2050 struct buffer_head *ibh;
2053 spin_unlock(&sbi->s_inode_lock);
2054 err = nilfs_ifile_get_inode_block(
2055 sbi->s_ifile, ii->vfs_inode.i_ino, &ibh);
2056 if (unlikely(err)) {
2057 nilfs_warning(sbi->s_super, __func__,
2058 "failed to get inode block.\n");
2061 nilfs_mdt_mark_buffer_dirty(ibh);
2062 nilfs_mdt_mark_dirty(sbi->s_ifile);
2063 spin_lock(&sbi->s_inode_lock);
2064 if (likely(!ii->i_bh))
2072 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2073 set_bit(NILFS_I_BUSY, &ii->i_state);
2074 list_del(&ii->i_dirty);
2075 list_add_tail(&ii->i_dirty, &sci->sc_dirty_files);
2077 spin_unlock(&sbi->s_inode_lock);
2079 NILFS_I(sbi->s_ifile)->i_cno = cno;
2084 static void nilfs_segctor_check_out_files(struct nilfs_sc_info *sci,
2085 struct nilfs_sb_info *sbi)
2087 struct nilfs_transaction_info *ti = current->journal_info;
2088 struct nilfs_inode_info *ii, *n;
2089 __u64 cno = sbi->s_nilfs->ns_cno;
2091 spin_lock(&sbi->s_inode_lock);
2092 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2093 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2094 test_bit(NILFS_I_DIRTY, &ii->i_state)) {
2095 /* The current checkpoint number (=nilfs->ns_cno) is
2096 changed between check-in and check-out only if the
2097 super root is written out. So, we can update i_cno
2098 for the inodes that remain in the dirty list. */
2102 clear_bit(NILFS_I_BUSY, &ii->i_state);
2105 list_del(&ii->i_dirty);
2106 list_add_tail(&ii->i_dirty, &ti->ti_garbage);
2108 spin_unlock(&sbi->s_inode_lock);
2112 * Main procedure of segment constructor
2114 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2116 struct nilfs_sb_info *sbi = sci->sc_sbi;
2117 struct the_nilfs *nilfs = sbi->s_nilfs;
2118 struct page *failed_page;
2121 sci->sc_stage.scnt = NILFS_ST_INIT;
2123 err = nilfs_segctor_check_in_files(sci, sbi);
2127 if (nilfs_test_metadata_dirty(sbi))
2128 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2130 if (nilfs_segctor_clean(sci))
2134 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2136 err = nilfs_segctor_begin_construction(sci, nilfs);
2140 /* Update time stamp */
2141 sci->sc_seg_ctime = get_seconds();
2143 err = nilfs_segctor_collect(sci, nilfs, mode);
2147 /* Avoid empty segment */
2148 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
2149 NILFS_SEG_EMPTY(&sci->sc_curseg->sb_sum)) {
2150 nilfs_segctor_abort_construction(sci, nilfs, 1);
2154 err = nilfs_segctor_assign(sci, mode);
2158 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2159 nilfs_segctor_fill_in_file_bmap(sci, sbi->s_ifile);
2161 if (mode == SC_LSEG_SR &&
2162 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
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, err);
2175 goto failed_to_write;
2177 nilfs_segctor_fill_in_checksums(sci, nilfs->ns_crc_seed);
2179 err = nilfs_segctor_write(sci, nilfs);
2181 goto failed_to_write;
2183 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2184 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2186 * At this point, we avoid double buffering
2187 * for blocksize < pagesize because page dirty
2188 * flag is turned off during write and dirty
2189 * buffers are not properly collected for
2190 * pages crossing over segments.
2192 err = nilfs_segctor_wait(sci);
2194 goto failed_to_write;
2196 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2199 nilfs_segctor_check_out_files(sci, sbi);
2203 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2204 nilfs_redirty_inodes(&sci->sc_dirty_files);
2207 if (nilfs_doing_gc())
2208 nilfs_redirty_inodes(&sci->sc_gc_inodes);
2209 nilfs_segctor_abort_construction(sci, nilfs, err);
2214 * nilfs_segctor_start_timer - set timer of background write
2215 * @sci: nilfs_sc_info
2217 * If the timer has already been set, it ignores the new request.
2218 * This function MUST be called within a section locking the segment
2221 static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2223 spin_lock(&sci->sc_state_lock);
2224 if (sci->sc_timer && !(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2225 sci->sc_timer->expires = jiffies + sci->sc_interval;
2226 add_timer(sci->sc_timer);
2227 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2229 spin_unlock(&sci->sc_state_lock);
2232 static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2234 spin_lock(&sci->sc_state_lock);
2235 if (!(sci->sc_flush_request & (1 << bn))) {
2236 unsigned long prev_req = sci->sc_flush_request;
2238 sci->sc_flush_request |= (1 << bn);
2240 wake_up(&sci->sc_wait_daemon);
2242 spin_unlock(&sci->sc_state_lock);
2246 * nilfs_flush_segment - trigger a segment construction for resource control
2248 * @ino: inode number of the file to be flushed out.
2250 void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2252 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2253 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2255 if (!sci || nilfs_doing_construction())
2257 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2258 /* assign bit 0 to data files */
2261 struct nilfs_segctor_wait_request {
2268 static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2270 struct nilfs_segctor_wait_request wait_req;
2273 spin_lock(&sci->sc_state_lock);
2274 init_wait(&wait_req.wq);
2276 atomic_set(&wait_req.done, 0);
2277 wait_req.seq = ++sci->sc_seq_request;
2278 spin_unlock(&sci->sc_state_lock);
2280 init_waitqueue_entry(&wait_req.wq, current);
2281 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2282 set_current_state(TASK_INTERRUPTIBLE);
2283 wake_up(&sci->sc_wait_daemon);
2286 if (atomic_read(&wait_req.done)) {
2290 if (!signal_pending(current)) {
2297 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2301 static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2303 struct nilfs_segctor_wait_request *wrq, *n;
2304 unsigned long flags;
2306 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2307 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2309 if (!atomic_read(&wrq->done) &&
2310 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2312 atomic_set(&wrq->done, 1);
2314 if (atomic_read(&wrq->done)) {
2315 wrq->wq.func(&wrq->wq,
2316 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2320 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2324 * nilfs_construct_segment - construct a logical segment
2327 * Return Value: On success, 0 is retured. On errors, one of the following
2328 * negative error code is returned.
2330 * %-EROFS - Read only filesystem.
2334 * %-ENOSPC - No space left on device (only in a panic state).
2336 * %-ERESTARTSYS - Interrupted.
2338 * %-ENOMEM - Insufficient memory available.
2340 int nilfs_construct_segment(struct super_block *sb)
2342 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2343 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2344 struct nilfs_transaction_info *ti;
2350 /* A call inside transactions causes a deadlock. */
2351 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2353 err = nilfs_segctor_sync(sci);
2358 * nilfs_construct_dsync_segment - construct a data-only logical segment
2360 * @inode: inode whose data blocks should be written out
2361 * @start: start byte offset
2362 * @end: end byte offset (inclusive)
2364 * Return Value: On success, 0 is retured. On errors, one of the following
2365 * negative error code is returned.
2367 * %-EROFS - Read only filesystem.
2371 * %-ENOSPC - No space left on device (only in a panic state).
2373 * %-ERESTARTSYS - Interrupted.
2375 * %-ENOMEM - Insufficient memory available.
2377 int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2378 loff_t start, loff_t end)
2380 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2381 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2382 struct nilfs_inode_info *ii;
2383 struct nilfs_transaction_info ti;
2389 nilfs_transaction_lock(sbi, &ti, 0);
2391 ii = NILFS_I(inode);
2392 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2393 nilfs_test_opt(sbi, STRICT_ORDER) ||
2394 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2395 nilfs_discontinued(sbi->s_nilfs)) {
2396 nilfs_transaction_unlock(sbi);
2397 err = nilfs_segctor_sync(sci);
2401 spin_lock(&sbi->s_inode_lock);
2402 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2403 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2404 spin_unlock(&sbi->s_inode_lock);
2405 nilfs_transaction_unlock(sbi);
2408 spin_unlock(&sbi->s_inode_lock);
2409 sci->sc_dsync_inode = ii;
2410 sci->sc_dsync_start = start;
2411 sci->sc_dsync_end = end;
2413 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2415 nilfs_transaction_unlock(sbi);
2419 #define FLUSH_FILE_BIT (0x1) /* data file only */
2420 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2423 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2424 * @sci: segment constructor object
2426 static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
2428 spin_lock(&sci->sc_state_lock);
2429 sci->sc_seq_accepted = sci->sc_seq_request;
2430 spin_unlock(&sci->sc_state_lock);
2433 del_timer_sync(sci->sc_timer);
2437 * nilfs_segctor_notify - notify the result of request to caller threads
2438 * @sci: segment constructor object
2439 * @mode: mode of log forming
2440 * @err: error code to be notified
2442 static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
2444 /* Clear requests (even when the construction failed) */
2445 spin_lock(&sci->sc_state_lock);
2447 if (mode == SC_LSEG_SR) {
2448 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
2449 sci->sc_seq_done = sci->sc_seq_accepted;
2450 nilfs_segctor_wakeup(sci, err);
2451 sci->sc_flush_request = 0;
2453 if (mode == SC_FLUSH_FILE)
2454 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
2455 else if (mode == SC_FLUSH_DAT)
2456 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2458 /* re-enable timer if checkpoint creation was not done */
2459 if (sci->sc_timer && (sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2460 time_before(jiffies, sci->sc_timer->expires))
2461 add_timer(sci->sc_timer);
2463 spin_unlock(&sci->sc_state_lock);
2467 * nilfs_segctor_construct - form logs and write them to disk
2468 * @sci: segment constructor object
2469 * @mode: mode of log forming
2471 static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
2473 struct nilfs_sb_info *sbi = sci->sc_sbi;
2474 struct the_nilfs *nilfs = sbi->s_nilfs;
2477 nilfs_segctor_accept(sci);
2479 if (nilfs_discontinued(nilfs))
2481 if (!nilfs_segctor_confirm(sci))
2482 err = nilfs_segctor_do_construct(sci, mode);
2485 if (mode != SC_FLUSH_DAT)
2486 atomic_set(&nilfs->ns_ndirtyblks, 0);
2487 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2488 nilfs_discontinued(nilfs)) {
2489 down_write(&nilfs->ns_sem);
2490 err = nilfs_commit_super(
2491 sbi, nilfs_altsb_need_update(nilfs));
2492 up_write(&nilfs->ns_sem);
2496 nilfs_segctor_notify(sci, mode, err);
2500 static void nilfs_construction_timeout(unsigned long data)
2502 struct task_struct *p = (struct task_struct *)data;
2507 nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2509 struct nilfs_inode_info *ii, *n;
2511 list_for_each_entry_safe(ii, n, head, i_dirty) {
2512 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2514 hlist_del_init(&ii->vfs_inode.i_hash);
2515 list_del_init(&ii->i_dirty);
2516 nilfs_clear_gcinode(&ii->vfs_inode);
2520 int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2523 struct nilfs_sb_info *sbi = NILFS_SB(sb);
2524 struct nilfs_sc_info *sci = NILFS_SC(sbi);
2525 struct the_nilfs *nilfs = sbi->s_nilfs;
2526 struct nilfs_transaction_info ti;
2532 nilfs_transaction_lock(sbi, &ti, 1);
2534 err = nilfs_init_gcdat_inode(nilfs);
2538 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
2542 sci->sc_freesegs = kbufs[4];
2543 sci->sc_nfreesegs = argv[4].v_nmembs;
2544 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
2547 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
2548 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
2553 nilfs_warning(sb, __func__,
2554 "segment construction failed. (err=%d)", err);
2555 set_current_state(TASK_INTERRUPTIBLE);
2556 schedule_timeout(sci->sc_interval);
2558 if (nilfs_test_opt(sbi, DISCARD)) {
2559 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2563 "NILFS warning: error %d on discard request, "
2564 "turning discards off for the device\n", ret);
2565 nilfs_clear_opt(sbi, DISCARD);
2570 sci->sc_freesegs = NULL;
2571 sci->sc_nfreesegs = 0;
2572 nilfs_clear_gcdat_inode(nilfs);
2573 nilfs_transaction_unlock(sbi);
2577 static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2579 struct nilfs_sb_info *sbi = sci->sc_sbi;
2580 struct nilfs_transaction_info ti;
2582 nilfs_transaction_lock(sbi, &ti, 0);
2583 nilfs_segctor_construct(sci, mode);
2586 * Unclosed segment should be retried. We do this using sc_timer.
2587 * Timeout of sc_timer will invoke complete construction which leads
2588 * to close the current logical segment.
2590 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2591 nilfs_segctor_start_timer(sci);
2593 nilfs_transaction_unlock(sbi);
2596 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2601 spin_lock(&sci->sc_state_lock);
2602 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2603 SC_FLUSH_DAT : SC_FLUSH_FILE;
2604 spin_unlock(&sci->sc_state_lock);
2607 err = nilfs_segctor_do_construct(sci, mode);
2609 spin_lock(&sci->sc_state_lock);
2610 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2611 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2612 spin_unlock(&sci->sc_state_lock);
2614 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2617 static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2619 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2620 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2621 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2622 return SC_FLUSH_FILE;
2623 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2624 return SC_FLUSH_DAT;
2630 * nilfs_segctor_thread - main loop of the segment constructor thread.
2631 * @arg: pointer to a struct nilfs_sc_info.
2633 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2634 * to execute segment constructions.
2636 static int nilfs_segctor_thread(void *arg)
2638 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
2639 struct the_nilfs *nilfs = sci->sc_sbi->s_nilfs;
2640 struct timer_list timer;
2644 timer.data = (unsigned long)current;
2645 timer.function = nilfs_construction_timeout;
2646 sci->sc_timer = &timer;
2649 sci->sc_task = current;
2650 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2652 "segctord starting. Construction interval = %lu seconds, "
2653 "CP frequency < %lu seconds\n",
2654 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2656 spin_lock(&sci->sc_state_lock);
2661 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2664 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2666 else if (!sci->sc_flush_request)
2669 mode = nilfs_segctor_flush_mode(sci);
2671 spin_unlock(&sci->sc_state_lock);
2672 nilfs_segctor_thread_construct(sci, mode);
2673 spin_lock(&sci->sc_state_lock);
2678 if (freezing(current)) {
2679 spin_unlock(&sci->sc_state_lock);
2681 spin_lock(&sci->sc_state_lock);
2684 int should_sleep = 1;
2686 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2687 TASK_INTERRUPTIBLE);
2689 if (sci->sc_seq_request != sci->sc_seq_done)
2691 else if (sci->sc_flush_request)
2693 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2694 should_sleep = time_before(jiffies,
2695 sci->sc_timer->expires);
2698 spin_unlock(&sci->sc_state_lock);
2700 spin_lock(&sci->sc_state_lock);
2702 finish_wait(&sci->sc_wait_daemon, &wait);
2703 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2704 time_after_eq(jiffies, sci->sc_timer->expires));
2706 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
2707 set_nilfs_discontinued(nilfs);
2712 spin_unlock(&sci->sc_state_lock);
2713 del_timer_sync(sci->sc_timer);
2714 sci->sc_timer = NULL;
2717 sci->sc_task = NULL;
2718 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2722 static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2724 struct task_struct *t;
2726 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2728 int err = PTR_ERR(t);
2730 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2734 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2738 static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2740 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2742 while (sci->sc_task) {
2743 wake_up(&sci->sc_wait_daemon);
2744 spin_unlock(&sci->sc_state_lock);
2745 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2746 spin_lock(&sci->sc_state_lock);
2750 static int nilfs_segctor_init(struct nilfs_sc_info *sci)
2752 sci->sc_seq_done = sci->sc_seq_request;
2754 return nilfs_segctor_start_thread(sci);
2758 * Setup & clean-up functions
2760 static struct nilfs_sc_info *nilfs_segctor_new(struct nilfs_sb_info *sbi)
2762 struct nilfs_sc_info *sci;
2764 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2769 sci->sc_super = sbi->s_super;
2771 init_waitqueue_head(&sci->sc_wait_request);
2772 init_waitqueue_head(&sci->sc_wait_daemon);
2773 init_waitqueue_head(&sci->sc_wait_task);
2774 spin_lock_init(&sci->sc_state_lock);
2775 INIT_LIST_HEAD(&sci->sc_dirty_files);
2776 INIT_LIST_HEAD(&sci->sc_segbufs);
2777 INIT_LIST_HEAD(&sci->sc_write_logs);
2778 INIT_LIST_HEAD(&sci->sc_gc_inodes);
2779 INIT_LIST_HEAD(&sci->sc_copied_buffers);
2781 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2782 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2783 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2785 if (sbi->s_interval)
2786 sci->sc_interval = sbi->s_interval;
2787 if (sbi->s_watermark)
2788 sci->sc_watermark = sbi->s_watermark;
2792 static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2794 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2796 /* The segctord thread was stopped and its timer was removed.
2797 But some tasks remain. */
2799 struct nilfs_sb_info *sbi = sci->sc_sbi;
2800 struct nilfs_transaction_info ti;
2802 nilfs_transaction_lock(sbi, &ti, 0);
2803 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
2804 nilfs_transaction_unlock(sbi);
2806 } while (ret && retrycount-- > 0);
2810 * nilfs_segctor_destroy - destroy the segment constructor.
2811 * @sci: nilfs_sc_info
2813 * nilfs_segctor_destroy() kills the segctord thread and frees
2814 * the nilfs_sc_info struct.
2815 * Caller must hold the segment semaphore.
2817 static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2819 struct nilfs_sb_info *sbi = sci->sc_sbi;
2822 up_write(&sbi->s_nilfs->ns_segctor_sem);
2824 spin_lock(&sci->sc_state_lock);
2825 nilfs_segctor_kill_thread(sci);
2826 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2827 || sci->sc_seq_request != sci->sc_seq_done);
2828 spin_unlock(&sci->sc_state_lock);
2830 if (flag || !nilfs_segctor_confirm(sci))
2831 nilfs_segctor_write_out(sci);
2833 WARN_ON(!list_empty(&sci->sc_copied_buffers));
2835 if (!list_empty(&sci->sc_dirty_files)) {
2836 nilfs_warning(sbi->s_super, __func__,
2837 "dirty file(s) after the final construction\n");
2838 nilfs_dispose_list(sbi, &sci->sc_dirty_files, 1);
2841 WARN_ON(!list_empty(&sci->sc_segbufs));
2842 WARN_ON(!list_empty(&sci->sc_write_logs));
2844 down_write(&sbi->s_nilfs->ns_segctor_sem);
2850 * nilfs_attach_segment_constructor - attach a segment constructor
2851 * @sbi: nilfs_sb_info
2853 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
2854 * initializes it, and starts the segment constructor.
2856 * Return Value: On success, 0 is returned. On error, one of the following
2857 * negative error code is returned.
2859 * %-ENOMEM - Insufficient memory available.
2861 int nilfs_attach_segment_constructor(struct nilfs_sb_info *sbi)
2863 struct the_nilfs *nilfs = sbi->s_nilfs;
2866 if (NILFS_SC(sbi)) {
2868 * This happens if the filesystem was remounted
2869 * read/write after nilfs_error degenerated it into a
2872 nilfs_detach_segment_constructor(sbi);
2875 sbi->s_sc_info = nilfs_segctor_new(sbi);
2876 if (!sbi->s_sc_info)
2879 nilfs_attach_writer(nilfs, sbi);
2880 err = nilfs_segctor_init(NILFS_SC(sbi));
2882 nilfs_detach_writer(nilfs, sbi);
2883 kfree(sbi->s_sc_info);
2884 sbi->s_sc_info = NULL;
2890 * nilfs_detach_segment_constructor - destroy the segment constructor
2891 * @sbi: nilfs_sb_info
2893 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2894 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2896 void nilfs_detach_segment_constructor(struct nilfs_sb_info *sbi)
2898 struct the_nilfs *nilfs = sbi->s_nilfs;
2899 LIST_HEAD(garbage_list);
2901 down_write(&nilfs->ns_segctor_sem);
2902 if (NILFS_SC(sbi)) {
2903 nilfs_segctor_destroy(NILFS_SC(sbi));
2904 sbi->s_sc_info = NULL;
2907 /* Force to free the list of dirty files */
2908 spin_lock(&sbi->s_inode_lock);
2909 if (!list_empty(&sbi->s_dirty_files)) {
2910 list_splice_init(&sbi->s_dirty_files, &garbage_list);
2911 nilfs_warning(sbi->s_super, __func__,
2912 "Non empty dirty list after the last "
2913 "segment construction\n");
2915 spin_unlock(&sbi->s_inode_lock);
2916 up_write(&nilfs->ns_segctor_sem);
2918 nilfs_dispose_list(sbi, &garbage_list, 1);
2919 nilfs_detach_writer(nilfs, sbi);