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 the_nilfs *, struct list_head *, int);
109 #define nilfs_cnt32_gt(a, b) \
110 (typecheck(__u32, a) && typecheck(__u32, b) && \
111 ((__s32)(b) - (__s32)(a) < 0))
112 #define nilfs_cnt32_ge(a, b) \
113 (typecheck(__u32, a) && typecheck(__u32, b) && \
114 ((__s32)(a) - (__s32)(b) >= 0))
115 #define nilfs_cnt32_lt(a, b) nilfs_cnt32_gt(b, a)
116 #define nilfs_cnt32_le(a, b) nilfs_cnt32_ge(b, a)
118 static int nilfs_prepare_segment_lock(struct nilfs_transaction_info *ti)
120 struct nilfs_transaction_info *cur_ti = current->journal_info;
124 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
125 return ++cur_ti->ti_count;
128 * If journal_info field is occupied by other FS,
129 * it is saved and will be restored on
130 * nilfs_transaction_commit().
133 "NILFS warning: journal info from a different "
135 save = current->journal_info;
139 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
142 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
148 ti->ti_magic = NILFS_TI_MAGIC;
149 current->journal_info = ti;
154 * nilfs_transaction_begin - start indivisible file operations.
156 * @ti: nilfs_transaction_info
157 * @vacancy_check: flags for vacancy rate checks
159 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
160 * the segment semaphore, to make a segment construction and write tasks
161 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
162 * The region enclosed by these two functions can be nested. To avoid a
163 * deadlock, the semaphore is only acquired or released in the outermost call.
165 * This function allocates a nilfs_transaction_info struct to keep context
166 * information on it. It is initialized and hooked onto the current task in
167 * the outermost call. If a pre-allocated struct is given to @ti, it is used
168 * instead; otherwise a new struct is assigned from a slab.
170 * When @vacancy_check flag is set, this function will check the amount of
171 * free space, and will wait for the GC to reclaim disk space if low capacity.
173 * Return Value: On success, 0 is returned. On error, one of the following
174 * negative error code is returned.
176 * %-ENOMEM - Insufficient memory available.
178 * %-ENOSPC - No space left on device
180 int nilfs_transaction_begin(struct super_block *sb,
181 struct nilfs_transaction_info *ti,
184 struct the_nilfs *nilfs;
185 int ret = nilfs_prepare_segment_lock(ti);
187 if (unlikely(ret < 0))
192 vfs_check_frozen(sb, SB_FREEZE_WRITE);
194 nilfs = sb->s_fs_info;
195 down_read(&nilfs->ns_segctor_sem);
196 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
197 up_read(&nilfs->ns_segctor_sem);
204 ti = current->journal_info;
205 current->journal_info = ti->ti_save;
206 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
207 kmem_cache_free(nilfs_transaction_cachep, ti);
212 * nilfs_transaction_commit - commit indivisible file operations.
215 * nilfs_transaction_commit() releases the read semaphore which is
216 * acquired by nilfs_transaction_begin(). This is only performed
217 * in outermost call of this function. If a commit flag is set,
218 * nilfs_transaction_commit() sets a timer to start the segment
219 * constructor. If a sync flag is set, it starts construction
222 int nilfs_transaction_commit(struct super_block *sb)
224 struct nilfs_transaction_info *ti = current->journal_info;
225 struct the_nilfs *nilfs = sb->s_fs_info;
228 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
229 ti->ti_flags |= NILFS_TI_COMMIT;
230 if (ti->ti_count > 0) {
234 if (nilfs->ns_writer) {
235 struct nilfs_sc_info *sci = nilfs->ns_writer;
237 if (ti->ti_flags & NILFS_TI_COMMIT)
238 nilfs_segctor_start_timer(sci);
239 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
240 nilfs_segctor_do_flush(sci, 0);
242 up_read(&nilfs->ns_segctor_sem);
243 current->journal_info = ti->ti_save;
245 if (ti->ti_flags & NILFS_TI_SYNC)
246 err = nilfs_construct_segment(sb);
247 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
248 kmem_cache_free(nilfs_transaction_cachep, ti);
252 void nilfs_transaction_abort(struct super_block *sb)
254 struct nilfs_transaction_info *ti = current->journal_info;
255 struct the_nilfs *nilfs = sb->s_fs_info;
257 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
258 if (ti->ti_count > 0) {
262 up_read(&nilfs->ns_segctor_sem);
264 current->journal_info = ti->ti_save;
265 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
266 kmem_cache_free(nilfs_transaction_cachep, ti);
269 void nilfs_relax_pressure_in_lock(struct super_block *sb)
271 struct the_nilfs *nilfs = sb->s_fs_info;
272 struct nilfs_sc_info *sci = nilfs->ns_writer;
274 if (!sci || !sci->sc_flush_request)
277 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
278 up_read(&nilfs->ns_segctor_sem);
280 down_write(&nilfs->ns_segctor_sem);
281 if (sci->sc_flush_request &&
282 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
283 struct nilfs_transaction_info *ti = current->journal_info;
285 ti->ti_flags |= NILFS_TI_WRITER;
286 nilfs_segctor_do_immediate_flush(sci);
287 ti->ti_flags &= ~NILFS_TI_WRITER;
289 downgrade_write(&nilfs->ns_segctor_sem);
292 static void nilfs_transaction_lock(struct super_block *sb,
293 struct nilfs_transaction_info *ti,
296 struct nilfs_transaction_info *cur_ti = current->journal_info;
297 struct the_nilfs *nilfs = sb->s_fs_info;
298 struct nilfs_sc_info *sci = nilfs->ns_writer;
301 ti->ti_flags = NILFS_TI_WRITER;
303 ti->ti_save = cur_ti;
304 ti->ti_magic = NILFS_TI_MAGIC;
305 current->journal_info = ti;
308 down_write(&nilfs->ns_segctor_sem);
309 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
312 nilfs_segctor_do_immediate_flush(sci);
314 up_write(&nilfs->ns_segctor_sem);
318 ti->ti_flags |= NILFS_TI_GC;
321 static void nilfs_transaction_unlock(struct super_block *sb)
323 struct nilfs_transaction_info *ti = current->journal_info;
324 struct the_nilfs *nilfs = sb->s_fs_info;
326 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
327 BUG_ON(ti->ti_count > 0);
329 up_write(&nilfs->ns_segctor_sem);
330 current->journal_info = ti->ti_save;
333 static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
334 struct nilfs_segsum_pointer *ssp,
337 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
338 unsigned blocksize = sci->sc_super->s_blocksize;
341 if (unlikely(ssp->offset + bytes > blocksize)) {
343 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
344 &segbuf->sb_segsum_buffers));
345 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
347 p = ssp->bh->b_data + ssp->offset;
348 ssp->offset += bytes;
353 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
354 * @sci: nilfs_sc_info
356 static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
358 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
359 struct buffer_head *sumbh;
364 if (nilfs_doing_gc())
366 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
370 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
371 sumbytes = segbuf->sb_sum.sumbytes;
372 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
373 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
374 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
378 static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
380 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
381 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
382 return -E2BIG; /* The current segment is filled up
384 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
385 return nilfs_segctor_reset_segment_buffer(sci);
388 static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
390 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
393 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
394 err = nilfs_segctor_feed_segment(sci);
397 segbuf = sci->sc_curseg;
399 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
401 segbuf->sb_sum.flags |= NILFS_SS_SR;
406 * Functions for making segment summary and payloads
408 static int nilfs_segctor_segsum_block_required(
409 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
412 unsigned blocksize = sci->sc_super->s_blocksize;
413 /* Size of finfo and binfo is enough small against blocksize */
415 return ssp->offset + binfo_size +
416 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
420 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
423 sci->sc_curseg->sb_sum.nfinfo++;
424 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
425 nilfs_segctor_map_segsum_entry(
426 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
428 if (NILFS_I(inode)->i_root &&
429 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
430 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
434 static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
437 struct nilfs_finfo *finfo;
438 struct nilfs_inode_info *ii;
439 struct nilfs_segment_buffer *segbuf;
442 if (sci->sc_blk_cnt == 0)
447 if (test_bit(NILFS_I_GCINODE, &ii->i_state))
449 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
454 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
456 finfo->fi_ino = cpu_to_le64(inode->i_ino);
457 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
458 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
459 finfo->fi_cno = cpu_to_le64(cno);
461 segbuf = sci->sc_curseg;
462 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
463 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
464 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
465 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
468 static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
469 struct buffer_head *bh,
473 struct nilfs_segment_buffer *segbuf;
474 int required, err = 0;
477 segbuf = sci->sc_curseg;
478 required = nilfs_segctor_segsum_block_required(
479 sci, &sci->sc_binfo_ptr, binfo_size);
480 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
481 nilfs_segctor_end_finfo(sci, inode);
482 err = nilfs_segctor_feed_segment(sci);
487 if (unlikely(required)) {
488 err = nilfs_segbuf_extend_segsum(segbuf);
492 if (sci->sc_blk_cnt == 0)
493 nilfs_segctor_begin_finfo(sci, inode);
495 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
496 /* Substitution to vblocknr is delayed until update_blocknr() */
497 nilfs_segbuf_add_file_buffer(segbuf, bh);
504 * Callback functions that enumerate, mark, and collect dirty blocks
506 static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
507 struct buffer_head *bh, struct inode *inode)
511 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
515 err = nilfs_segctor_add_file_block(sci, bh, inode,
516 sizeof(struct nilfs_binfo_v));
518 sci->sc_datablk_cnt++;
522 static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
523 struct buffer_head *bh,
526 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
529 static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
530 struct buffer_head *bh,
533 WARN_ON(!buffer_dirty(bh));
534 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
537 static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
538 struct nilfs_segsum_pointer *ssp,
539 union nilfs_binfo *binfo)
541 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
542 sci, ssp, sizeof(*binfo_v));
543 *binfo_v = binfo->bi_v;
546 static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
547 struct nilfs_segsum_pointer *ssp,
548 union nilfs_binfo *binfo)
550 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
551 sci, ssp, sizeof(*vblocknr));
552 *vblocknr = binfo->bi_v.bi_vblocknr;
555 static struct nilfs_sc_operations nilfs_sc_file_ops = {
556 .collect_data = nilfs_collect_file_data,
557 .collect_node = nilfs_collect_file_node,
558 .collect_bmap = nilfs_collect_file_bmap,
559 .write_data_binfo = nilfs_write_file_data_binfo,
560 .write_node_binfo = nilfs_write_file_node_binfo,
563 static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
564 struct buffer_head *bh, struct inode *inode)
568 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
572 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
574 sci->sc_datablk_cnt++;
578 static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
579 struct buffer_head *bh, struct inode *inode)
581 WARN_ON(!buffer_dirty(bh));
582 return nilfs_segctor_add_file_block(sci, bh, inode,
583 sizeof(struct nilfs_binfo_dat));
586 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
587 struct nilfs_segsum_pointer *ssp,
588 union nilfs_binfo *binfo)
590 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
592 *blkoff = binfo->bi_dat.bi_blkoff;
595 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
596 struct nilfs_segsum_pointer *ssp,
597 union nilfs_binfo *binfo)
599 struct nilfs_binfo_dat *binfo_dat =
600 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
601 *binfo_dat = binfo->bi_dat;
604 static struct nilfs_sc_operations nilfs_sc_dat_ops = {
605 .collect_data = nilfs_collect_dat_data,
606 .collect_node = nilfs_collect_file_node,
607 .collect_bmap = nilfs_collect_dat_bmap,
608 .write_data_binfo = nilfs_write_dat_data_binfo,
609 .write_node_binfo = nilfs_write_dat_node_binfo,
612 static struct nilfs_sc_operations nilfs_sc_dsync_ops = {
613 .collect_data = nilfs_collect_file_data,
614 .collect_node = NULL,
615 .collect_bmap = NULL,
616 .write_data_binfo = nilfs_write_file_data_binfo,
617 .write_node_binfo = NULL,
620 static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
621 struct list_head *listp,
623 loff_t start, loff_t end)
625 struct address_space *mapping = inode->i_mapping;
627 pgoff_t index = 0, last = ULONG_MAX;
631 if (unlikely(start != 0 || end != LLONG_MAX)) {
633 * A valid range is given for sync-ing data pages. The
634 * range is rounded to per-page; extra dirty buffers
635 * may be included if blocksize < pagesize.
637 index = start >> PAGE_SHIFT;
638 last = end >> PAGE_SHIFT;
640 pagevec_init(&pvec, 0);
642 if (unlikely(index > last) ||
643 !pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
644 min_t(pgoff_t, last - index,
645 PAGEVEC_SIZE - 1) + 1))
648 for (i = 0; i < pagevec_count(&pvec); i++) {
649 struct buffer_head *bh, *head;
650 struct page *page = pvec.pages[i];
652 if (unlikely(page->index > last))
656 if (!page_has_buffers(page))
657 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
660 bh = head = page_buffers(page);
662 if (!buffer_dirty(bh) || buffer_async_write(bh))
665 list_add_tail(&bh->b_assoc_buffers, listp);
667 if (unlikely(ndirties >= nlimit)) {
668 pagevec_release(&pvec);
672 } while (bh = bh->b_this_page, bh != head);
674 pagevec_release(&pvec);
679 static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
680 struct list_head *listp)
682 struct nilfs_inode_info *ii = NILFS_I(inode);
683 struct address_space *mapping = &ii->i_btnode_cache;
685 struct buffer_head *bh, *head;
689 pagevec_init(&pvec, 0);
691 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
693 for (i = 0; i < pagevec_count(&pvec); i++) {
694 bh = head = page_buffers(pvec.pages[i]);
696 if (buffer_dirty(bh) &&
697 !buffer_async_write(bh)) {
699 list_add_tail(&bh->b_assoc_buffers,
702 bh = bh->b_this_page;
703 } while (bh != head);
705 pagevec_release(&pvec);
710 static void nilfs_dispose_list(struct the_nilfs *nilfs,
711 struct list_head *head, int force)
713 struct nilfs_inode_info *ii, *n;
714 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
717 while (!list_empty(head)) {
718 spin_lock(&nilfs->ns_inode_lock);
719 list_for_each_entry_safe(ii, n, head, i_dirty) {
720 list_del_init(&ii->i_dirty);
722 if (unlikely(ii->i_bh)) {
726 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
727 set_bit(NILFS_I_QUEUED, &ii->i_state);
728 list_add_tail(&ii->i_dirty,
729 &nilfs->ns_dirty_files);
733 if (nv == SC_N_INODEVEC)
736 spin_unlock(&nilfs->ns_inode_lock);
738 for (pii = ivec; nv > 0; pii++, nv--)
739 iput(&(*pii)->vfs_inode);
743 static void nilfs_iput_work_func(struct work_struct *work)
745 struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
747 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
749 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
752 static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
753 struct nilfs_root *root)
757 if (nilfs_mdt_fetch_dirty(root->ifile))
759 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
761 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
763 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
768 static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
770 return list_empty(&sci->sc_dirty_files) &&
771 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
772 sci->sc_nfreesegs == 0 &&
773 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
776 static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
778 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
781 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
782 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
784 spin_lock(&nilfs->ns_inode_lock);
785 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
788 spin_unlock(&nilfs->ns_inode_lock);
792 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
794 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
796 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
797 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
798 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
799 nilfs_mdt_clear_dirty(nilfs->ns_dat);
802 static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info *sci)
804 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
805 struct buffer_head *bh_cp;
806 struct nilfs_checkpoint *raw_cp;
809 /* XXX: this interface will be changed */
810 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 1,
813 /* The following code is duplicated with cpfile. But, it is
814 needed to collect the checkpoint even if it was not newly
816 mark_buffer_dirty(bh_cp);
817 nilfs_mdt_mark_dirty(nilfs->ns_cpfile);
818 nilfs_cpfile_put_checkpoint(
819 nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
821 WARN_ON(err == -EINVAL || err == -ENOENT);
826 static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info *sci)
828 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
829 struct buffer_head *bh_cp;
830 struct nilfs_checkpoint *raw_cp;
833 err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, 0,
836 WARN_ON(err == -EINVAL || err == -ENOENT);
839 raw_cp->cp_snapshot_list.ssl_next = 0;
840 raw_cp->cp_snapshot_list.ssl_prev = 0;
841 raw_cp->cp_inodes_count =
842 cpu_to_le64(atomic_read(&sci->sc_root->inodes_count));
843 raw_cp->cp_blocks_count =
844 cpu_to_le64(atomic_read(&sci->sc_root->blocks_count));
845 raw_cp->cp_nblk_inc =
846 cpu_to_le64(sci->sc_nblk_inc + sci->sc_nblk_this_inc);
847 raw_cp->cp_create = cpu_to_le64(sci->sc_seg_ctime);
848 raw_cp->cp_cno = cpu_to_le64(nilfs->ns_cno);
850 if (test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
851 nilfs_checkpoint_clear_minor(raw_cp);
853 nilfs_checkpoint_set_minor(raw_cp);
855 nilfs_write_inode_common(sci->sc_root->ifile,
856 &raw_cp->cp_ifile_inode, 1);
857 nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, nilfs->ns_cno, bh_cp);
864 static void nilfs_fill_in_file_bmap(struct inode *ifile,
865 struct nilfs_inode_info *ii)
868 struct buffer_head *ibh;
869 struct nilfs_inode *raw_inode;
871 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
874 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
876 nilfs_bmap_write(ii->i_bmap, raw_inode);
877 nilfs_ifile_unmap_inode(ifile, ii->vfs_inode.i_ino, ibh);
881 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
883 struct nilfs_inode_info *ii;
885 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
886 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
887 set_bit(NILFS_I_COLLECTED, &ii->i_state);
891 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
892 struct the_nilfs *nilfs)
894 struct buffer_head *bh_sr;
895 struct nilfs_super_root *raw_sr;
898 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
899 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
900 isz = nilfs->ns_inode_size;
901 srsz = NILFS_SR_BYTES(isz);
903 raw_sr->sr_bytes = cpu_to_le16(srsz);
904 raw_sr->sr_nongc_ctime
905 = cpu_to_le64(nilfs_doing_gc() ?
906 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
907 raw_sr->sr_flags = 0;
909 nilfs_write_inode_common(nilfs->ns_dat, (void *)raw_sr +
910 NILFS_SR_DAT_OFFSET(isz), 1);
911 nilfs_write_inode_common(nilfs->ns_cpfile, (void *)raw_sr +
912 NILFS_SR_CPFILE_OFFSET(isz), 1);
913 nilfs_write_inode_common(nilfs->ns_sufile, (void *)raw_sr +
914 NILFS_SR_SUFILE_OFFSET(isz), 1);
915 memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
918 static void nilfs_redirty_inodes(struct list_head *head)
920 struct nilfs_inode_info *ii;
922 list_for_each_entry(ii, head, i_dirty) {
923 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
924 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
928 static void nilfs_drop_collected_inodes(struct list_head *head)
930 struct nilfs_inode_info *ii;
932 list_for_each_entry(ii, head, i_dirty) {
933 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
936 clear_bit(NILFS_I_INODE_DIRTY, &ii->i_state);
937 set_bit(NILFS_I_UPDATED, &ii->i_state);
941 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
943 struct list_head *listp,
944 int (*collect)(struct nilfs_sc_info *,
945 struct buffer_head *,
948 struct buffer_head *bh, *n;
952 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
953 list_del_init(&bh->b_assoc_buffers);
954 err = collect(sci, bh, inode);
957 goto dispose_buffers;
963 while (!list_empty(listp)) {
964 bh = list_first_entry(listp, struct buffer_head,
966 list_del_init(&bh->b_assoc_buffers);
972 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
974 /* Remaining number of blocks within segment buffer */
975 return sci->sc_segbuf_nblocks -
976 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
979 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
981 struct nilfs_sc_operations *sc_ops)
983 LIST_HEAD(data_buffers);
984 LIST_HEAD(node_buffers);
987 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
988 size_t n, rest = nilfs_segctor_buffer_rest(sci);
990 n = nilfs_lookup_dirty_data_buffers(
991 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
993 err = nilfs_segctor_apply_buffers(
994 sci, inode, &data_buffers,
995 sc_ops->collect_data);
996 BUG_ON(!err); /* always receive -E2BIG or true error */
1000 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1002 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1003 err = nilfs_segctor_apply_buffers(
1004 sci, inode, &data_buffers, sc_ops->collect_data);
1005 if (unlikely(err)) {
1006 /* dispose node list */
1007 nilfs_segctor_apply_buffers(
1008 sci, inode, &node_buffers, NULL);
1011 sci->sc_stage.flags |= NILFS_CF_NODE;
1014 err = nilfs_segctor_apply_buffers(
1015 sci, inode, &node_buffers, sc_ops->collect_node);
1019 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1020 err = nilfs_segctor_apply_buffers(
1021 sci, inode, &node_buffers, sc_ops->collect_bmap);
1025 nilfs_segctor_end_finfo(sci, inode);
1026 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1032 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1033 struct inode *inode)
1035 LIST_HEAD(data_buffers);
1036 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1039 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1040 sci->sc_dsync_start,
1043 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1044 nilfs_collect_file_data);
1046 nilfs_segctor_end_finfo(sci, inode);
1048 /* always receive -E2BIG or true error if n > rest */
1053 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1055 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1056 struct list_head *head;
1057 struct nilfs_inode_info *ii;
1061 switch (sci->sc_stage.scnt) {
1064 sci->sc_stage.flags = 0;
1066 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1067 sci->sc_nblk_inc = 0;
1068 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1069 if (mode == SC_LSEG_DSYNC) {
1070 sci->sc_stage.scnt = NILFS_ST_DSYNC;
1075 sci->sc_stage.dirty_file_ptr = NULL;
1076 sci->sc_stage.gc_inode_ptr = NULL;
1077 if (mode == SC_FLUSH_DAT) {
1078 sci->sc_stage.scnt = NILFS_ST_DAT;
1081 sci->sc_stage.scnt++; /* Fall through */
1083 if (nilfs_doing_gc()) {
1084 head = &sci->sc_gc_inodes;
1085 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1087 list_for_each_entry_continue(ii, head, i_dirty) {
1088 err = nilfs_segctor_scan_file(
1089 sci, &ii->vfs_inode,
1090 &nilfs_sc_file_ops);
1091 if (unlikely(err)) {
1092 sci->sc_stage.gc_inode_ptr = list_entry(
1094 struct nilfs_inode_info,
1098 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1100 sci->sc_stage.gc_inode_ptr = NULL;
1102 sci->sc_stage.scnt++; /* Fall through */
1104 head = &sci->sc_dirty_files;
1105 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1107 list_for_each_entry_continue(ii, head, i_dirty) {
1108 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1110 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1111 &nilfs_sc_file_ops);
1112 if (unlikely(err)) {
1113 sci->sc_stage.dirty_file_ptr =
1114 list_entry(ii->i_dirty.prev,
1115 struct nilfs_inode_info,
1119 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1120 /* XXX: required ? */
1122 sci->sc_stage.dirty_file_ptr = NULL;
1123 if (mode == SC_FLUSH_FILE) {
1124 sci->sc_stage.scnt = NILFS_ST_DONE;
1127 sci->sc_stage.scnt++;
1128 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1130 case NILFS_ST_IFILE:
1131 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
1132 &nilfs_sc_file_ops);
1135 sci->sc_stage.scnt++;
1136 /* Creating a checkpoint */
1137 err = nilfs_segctor_create_checkpoint(sci);
1141 case NILFS_ST_CPFILE:
1142 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1143 &nilfs_sc_file_ops);
1146 sci->sc_stage.scnt++; /* Fall through */
1147 case NILFS_ST_SUFILE:
1148 err = nilfs_sufile_freev(nilfs->ns_sufile, sci->sc_freesegs,
1149 sci->sc_nfreesegs, &ndone);
1150 if (unlikely(err)) {
1151 nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1152 sci->sc_freesegs, ndone,
1156 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1158 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1159 &nilfs_sc_file_ops);
1162 sci->sc_stage.scnt++; /* Fall through */
1165 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
1169 if (mode == SC_FLUSH_DAT) {
1170 sci->sc_stage.scnt = NILFS_ST_DONE;
1173 sci->sc_stage.scnt++; /* Fall through */
1175 if (mode == SC_LSEG_SR) {
1176 /* Appending a super root */
1177 err = nilfs_segctor_add_super_root(sci);
1181 /* End of a logical segment */
1182 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1183 sci->sc_stage.scnt = NILFS_ST_DONE;
1185 case NILFS_ST_DSYNC:
1187 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
1188 ii = sci->sc_dsync_inode;
1189 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1192 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1195 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1196 sci->sc_stage.scnt = NILFS_ST_DONE;
1209 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1210 * @sci: nilfs_sc_info
1211 * @nilfs: nilfs object
1213 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1214 struct the_nilfs *nilfs)
1216 struct nilfs_segment_buffer *segbuf, *prev;
1220 segbuf = nilfs_segbuf_new(sci->sc_super);
1221 if (unlikely(!segbuf))
1224 if (list_empty(&sci->sc_write_logs)) {
1225 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1226 nilfs->ns_pseg_offset, nilfs);
1227 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1228 nilfs_shift_to_next_segment(nilfs);
1229 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1232 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1233 nextnum = nilfs->ns_nextnum;
1235 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1236 /* Start from the head of a new full segment */
1240 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1241 nilfs_segbuf_map_cont(segbuf, prev);
1242 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1243 nextnum = prev->sb_nextnum;
1245 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1246 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1247 segbuf->sb_sum.seg_seq++;
1252 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1257 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1261 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1263 BUG_ON(!list_empty(&sci->sc_segbufs));
1264 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1265 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
1269 nilfs_segbuf_free(segbuf);
1273 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1274 struct the_nilfs *nilfs, int nadd)
1276 struct nilfs_segment_buffer *segbuf, *prev;
1277 struct inode *sufile = nilfs->ns_sufile;
1282 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1284 * Since the segment specified with nextnum might be allocated during
1285 * the previous construction, the buffer including its segusage may
1286 * not be dirty. The following call ensures that the buffer is dirty
1287 * and will pin the buffer on memory until the sufile is written.
1289 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
1293 for (i = 0; i < nadd; i++) {
1294 /* extend segment info */
1296 segbuf = nilfs_segbuf_new(sci->sc_super);
1297 if (unlikely(!segbuf))
1300 /* map this buffer to region of segment on-disk */
1301 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1302 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1304 /* allocate the next next full segment */
1305 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1309 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1310 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1312 list_add_tail(&segbuf->sb_list, &list);
1315 list_splice_tail(&list, &sci->sc_segbufs);
1319 nilfs_segbuf_free(segbuf);
1321 list_for_each_entry(segbuf, &list, sb_list) {
1322 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1323 WARN_ON(ret); /* never fails */
1325 nilfs_destroy_logs(&list);
1329 static void nilfs_free_incomplete_logs(struct list_head *logs,
1330 struct the_nilfs *nilfs)
1332 struct nilfs_segment_buffer *segbuf, *prev;
1333 struct inode *sufile = nilfs->ns_sufile;
1336 segbuf = NILFS_FIRST_SEGBUF(logs);
1337 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
1338 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1339 WARN_ON(ret); /* never fails */
1341 if (atomic_read(&segbuf->sb_err)) {
1342 /* Case 1: The first segment failed */
1343 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1344 /* Case 1a: Partial segment appended into an existing
1346 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1347 segbuf->sb_fseg_end);
1348 else /* Case 1b: New full segment */
1349 set_nilfs_discontinued(nilfs);
1353 list_for_each_entry_continue(segbuf, logs, sb_list) {
1354 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1355 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1356 WARN_ON(ret); /* never fails */
1358 if (atomic_read(&segbuf->sb_err) &&
1359 segbuf->sb_segnum != nilfs->ns_nextnum)
1360 /* Case 2: extended segment (!= next) failed */
1361 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1366 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1367 struct inode *sufile)
1369 struct nilfs_segment_buffer *segbuf;
1370 unsigned long live_blocks;
1373 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1374 live_blocks = segbuf->sb_sum.nblocks +
1375 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
1376 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1379 WARN_ON(ret); /* always succeed because the segusage is dirty */
1383 static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
1385 struct nilfs_segment_buffer *segbuf;
1388 segbuf = NILFS_FIRST_SEGBUF(logs);
1389 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1390 segbuf->sb_pseg_start -
1391 segbuf->sb_fseg_start, 0);
1392 WARN_ON(ret); /* always succeed because the segusage is dirty */
1394 list_for_each_entry_continue(segbuf, logs, sb_list) {
1395 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1397 WARN_ON(ret); /* always succeed */
1401 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1402 struct nilfs_segment_buffer *last,
1403 struct inode *sufile)
1405 struct nilfs_segment_buffer *segbuf = last;
1408 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1409 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1410 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1413 nilfs_truncate_logs(&sci->sc_segbufs, last);
1417 static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1418 struct the_nilfs *nilfs, int mode)
1420 struct nilfs_cstage prev_stage = sci->sc_stage;
1423 /* Collection retry loop */
1425 sci->sc_nblk_this_inc = 0;
1426 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1428 err = nilfs_segctor_reset_segment_buffer(sci);
1432 err = nilfs_segctor_collect_blocks(sci, mode);
1433 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1437 if (unlikely(err != -E2BIG))
1440 /* The current segment is filled up */
1441 if (mode != SC_LSEG_SR || sci->sc_stage.scnt < NILFS_ST_CPFILE)
1444 nilfs_clear_logs(&sci->sc_segbufs);
1446 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1447 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1451 WARN_ON(err); /* do not happen */
1452 sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
1455 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1459 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1460 sci->sc_stage = prev_stage;
1462 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1469 static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1470 struct buffer_head *new_bh)
1472 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1474 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1475 /* The caller must release old_bh */
1479 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1480 struct nilfs_segment_buffer *segbuf,
1483 struct inode *inode = NULL;
1485 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1486 unsigned long nblocks = 0, ndatablk = 0;
1487 struct nilfs_sc_operations *sc_op = NULL;
1488 struct nilfs_segsum_pointer ssp;
1489 struct nilfs_finfo *finfo = NULL;
1490 union nilfs_binfo binfo;
1491 struct buffer_head *bh, *bh_org;
1498 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1499 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1500 ssp.offset = sizeof(struct nilfs_segment_summary);
1502 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1503 if (bh == segbuf->sb_super_root)
1506 finfo = nilfs_segctor_map_segsum_entry(
1507 sci, &ssp, sizeof(*finfo));
1508 ino = le64_to_cpu(finfo->fi_ino);
1509 nblocks = le32_to_cpu(finfo->fi_nblocks);
1510 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1512 inode = bh->b_page->mapping->host;
1514 if (mode == SC_LSEG_DSYNC)
1515 sc_op = &nilfs_sc_dsync_ops;
1516 else if (ino == NILFS_DAT_INO)
1517 sc_op = &nilfs_sc_dat_ops;
1518 else /* file blocks */
1519 sc_op = &nilfs_sc_file_ops;
1523 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1526 nilfs_list_replace_buffer(bh_org, bh);
1532 sc_op->write_data_binfo(sci, &ssp, &binfo);
1534 sc_op->write_node_binfo(sci, &ssp, &binfo);
1537 if (--nblocks == 0) {
1541 } else if (ndatablk > 0)
1551 static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1553 struct nilfs_segment_buffer *segbuf;
1556 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1557 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1560 nilfs_segbuf_fill_in_segsum(segbuf);
1565 static void nilfs_begin_page_io(struct page *page)
1567 if (!page || PageWriteback(page))
1568 /* For split b-tree node pages, this function may be called
1569 twice. We ignore the 2nd or later calls by this check. */
1573 clear_page_dirty_for_io(page);
1574 set_page_writeback(page);
1578 static void nilfs_segctor_prepare_write(struct nilfs_sc_info *sci)
1580 struct nilfs_segment_buffer *segbuf;
1581 struct page *bd_page = NULL, *fs_page = NULL;
1583 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1584 struct buffer_head *bh;
1586 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1588 set_buffer_async_write(bh);
1589 if (bh->b_page != bd_page) {
1592 clear_page_dirty_for_io(bd_page);
1593 set_page_writeback(bd_page);
1594 unlock_page(bd_page);
1596 bd_page = bh->b_page;
1600 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1602 set_buffer_async_write(bh);
1603 if (bh == segbuf->sb_super_root) {
1604 if (bh->b_page != bd_page) {
1606 clear_page_dirty_for_io(bd_page);
1607 set_page_writeback(bd_page);
1608 unlock_page(bd_page);
1609 bd_page = bh->b_page;
1613 if (bh->b_page != fs_page) {
1614 nilfs_begin_page_io(fs_page);
1615 fs_page = bh->b_page;
1621 clear_page_dirty_for_io(bd_page);
1622 set_page_writeback(bd_page);
1623 unlock_page(bd_page);
1625 nilfs_begin_page_io(fs_page);
1628 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
1629 struct the_nilfs *nilfs)
1633 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
1634 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1638 static void nilfs_end_page_io(struct page *page, int err)
1643 if (buffer_nilfs_node(page_buffers(page)) && !PageWriteback(page)) {
1645 * For b-tree node pages, this function may be called twice
1646 * or more because they might be split in a segment.
1648 if (PageDirty(page)) {
1650 * For pages holding split b-tree node buffers, dirty
1651 * flag on the buffers may be cleared discretely.
1652 * In that case, the page is once redirtied for
1653 * remaining buffers, and it must be cancelled if
1654 * all the buffers get cleaned later.
1657 if (nilfs_page_buffers_clean(page))
1658 __nilfs_clear_page_dirty(page);
1665 if (!nilfs_page_buffers_clean(page))
1666 __set_page_dirty_nobuffers(page);
1667 ClearPageError(page);
1669 __set_page_dirty_nobuffers(page);
1673 end_page_writeback(page);
1676 static void nilfs_abort_logs(struct list_head *logs, int err)
1678 struct nilfs_segment_buffer *segbuf;
1679 struct page *bd_page = NULL, *fs_page = NULL;
1680 struct buffer_head *bh;
1682 if (list_empty(logs))
1685 list_for_each_entry(segbuf, logs, sb_list) {
1686 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1688 clear_buffer_async_write(bh);
1689 if (bh->b_page != bd_page) {
1691 end_page_writeback(bd_page);
1692 bd_page = bh->b_page;
1696 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1698 clear_buffer_async_write(bh);
1699 if (bh == segbuf->sb_super_root) {
1700 if (bh->b_page != bd_page) {
1701 end_page_writeback(bd_page);
1702 bd_page = bh->b_page;
1706 if (bh->b_page != fs_page) {
1707 nilfs_end_page_io(fs_page, err);
1708 fs_page = bh->b_page;
1713 end_page_writeback(bd_page);
1715 nilfs_end_page_io(fs_page, err);
1718 static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1719 struct the_nilfs *nilfs, int err)
1724 list_splice_tail_init(&sci->sc_write_logs, &logs);
1725 ret = nilfs_wait_on_logs(&logs);
1726 nilfs_abort_logs(&logs, ret ? : err);
1728 list_splice_tail_init(&sci->sc_segbufs, &logs);
1729 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1730 nilfs_free_incomplete_logs(&logs, nilfs);
1732 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1733 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1737 WARN_ON(ret); /* do not happen */
1740 nilfs_destroy_logs(&logs);
1743 static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1744 struct nilfs_segment_buffer *segbuf)
1746 nilfs->ns_segnum = segbuf->sb_segnum;
1747 nilfs->ns_nextnum = segbuf->sb_nextnum;
1748 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1749 + segbuf->sb_sum.nblocks;
1750 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1751 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1754 static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1756 struct nilfs_segment_buffer *segbuf;
1757 struct page *bd_page = NULL, *fs_page = NULL;
1758 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1759 int update_sr = false;
1761 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
1762 struct buffer_head *bh;
1764 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1766 set_buffer_uptodate(bh);
1767 clear_buffer_dirty(bh);
1768 clear_buffer_async_write(bh);
1769 if (bh->b_page != bd_page) {
1771 end_page_writeback(bd_page);
1772 bd_page = bh->b_page;
1776 * We assume that the buffers which belong to the same page
1777 * continue over the buffer list.
1778 * Under this assumption, the last BHs of pages is
1779 * identifiable by the discontinuity of bh->b_page
1780 * (page != fs_page).
1782 * For B-tree node blocks, however, this assumption is not
1783 * guaranteed. The cleanup code of B-tree node pages needs
1786 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1788 set_buffer_uptodate(bh);
1789 clear_buffer_dirty(bh);
1790 clear_buffer_async_write(bh);
1791 clear_buffer_delay(bh);
1792 clear_buffer_nilfs_volatile(bh);
1793 clear_buffer_nilfs_redirected(bh);
1794 if (bh == segbuf->sb_super_root) {
1795 if (bh->b_page != bd_page) {
1796 end_page_writeback(bd_page);
1797 bd_page = bh->b_page;
1802 if (bh->b_page != fs_page) {
1803 nilfs_end_page_io(fs_page, 0);
1804 fs_page = bh->b_page;
1808 if (!nilfs_segbuf_simplex(segbuf)) {
1809 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
1810 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1811 sci->sc_lseg_stime = jiffies;
1813 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
1814 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1818 * Since pages may continue over multiple segment buffers,
1819 * end of the last page must be checked outside of the loop.
1822 end_page_writeback(bd_page);
1824 nilfs_end_page_io(fs_page, 0);
1826 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1828 if (nilfs_doing_gc())
1829 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
1831 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
1833 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1835 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1836 nilfs_set_next_segment(nilfs, segbuf);
1839 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
1840 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
1842 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
1843 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1844 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1845 nilfs_segctor_clear_metadata_dirty(sci);
1847 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1850 static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
1854 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
1856 nilfs_segctor_complete_write(sci);
1857 nilfs_destroy_logs(&sci->sc_write_logs);
1862 static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
1863 struct the_nilfs *nilfs)
1865 struct nilfs_inode_info *ii, *n;
1866 struct inode *ifile = sci->sc_root->ifile;
1868 spin_lock(&nilfs->ns_inode_lock);
1870 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
1872 struct buffer_head *ibh;
1875 spin_unlock(&nilfs->ns_inode_lock);
1876 err = nilfs_ifile_get_inode_block(
1877 ifile, ii->vfs_inode.i_ino, &ibh);
1878 if (unlikely(err)) {
1879 nilfs_warning(sci->sc_super, __func__,
1880 "failed to get inode block.\n");
1883 mark_buffer_dirty(ibh);
1884 nilfs_mdt_mark_dirty(ifile);
1885 spin_lock(&nilfs->ns_inode_lock);
1886 if (likely(!ii->i_bh))
1893 clear_bit(NILFS_I_QUEUED, &ii->i_state);
1894 set_bit(NILFS_I_BUSY, &ii->i_state);
1895 list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
1897 spin_unlock(&nilfs->ns_inode_lock);
1902 static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
1903 struct the_nilfs *nilfs)
1905 struct nilfs_inode_info *ii, *n;
1906 int during_mount = !(sci->sc_super->s_flags & MS_ACTIVE);
1907 int defer_iput = false;
1909 spin_lock(&nilfs->ns_inode_lock);
1910 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
1911 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
1912 test_bit(NILFS_I_DIRTY, &ii->i_state))
1915 clear_bit(NILFS_I_BUSY, &ii->i_state);
1918 list_del_init(&ii->i_dirty);
1919 if (!ii->vfs_inode.i_nlink || during_mount) {
1921 * Defer calling iput() to avoid deadlocks if
1922 * i_nlink == 0 or mount is not yet finished.
1924 list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
1927 spin_unlock(&nilfs->ns_inode_lock);
1928 iput(&ii->vfs_inode);
1929 spin_lock(&nilfs->ns_inode_lock);
1932 spin_unlock(&nilfs->ns_inode_lock);
1935 schedule_work(&sci->sc_iput_work);
1939 * Main procedure of segment constructor
1941 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
1943 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1946 sci->sc_stage.scnt = NILFS_ST_INIT;
1947 sci->sc_cno = nilfs->ns_cno;
1949 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
1953 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
1954 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1956 if (nilfs_segctor_clean(sci))
1960 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
1962 err = nilfs_segctor_begin_construction(sci, nilfs);
1966 /* Update time stamp */
1967 sci->sc_seg_ctime = get_seconds();
1969 err = nilfs_segctor_collect(sci, nilfs, mode);
1973 /* Avoid empty segment */
1974 if (sci->sc_stage.scnt == NILFS_ST_DONE &&
1975 nilfs_segbuf_empty(sci->sc_curseg)) {
1976 nilfs_segctor_abort_construction(sci, nilfs, 1);
1980 err = nilfs_segctor_assign(sci, mode);
1984 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
1985 nilfs_segctor_fill_in_file_bmap(sci);
1987 if (mode == SC_LSEG_SR &&
1988 sci->sc_stage.scnt >= NILFS_ST_CPFILE) {
1989 err = nilfs_segctor_fill_in_checkpoint(sci);
1991 goto failed_to_write;
1993 nilfs_segctor_fill_in_super_root(sci, nilfs);
1995 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
1997 /* Write partial segments */
1998 nilfs_segctor_prepare_write(sci);
2000 nilfs_add_checksums_on_logs(&sci->sc_segbufs,
2001 nilfs->ns_crc_seed);
2003 err = nilfs_segctor_write(sci, nilfs);
2005 goto failed_to_write;
2007 if (sci->sc_stage.scnt == NILFS_ST_DONE ||
2008 nilfs->ns_blocksize_bits != PAGE_CACHE_SHIFT) {
2010 * At this point, we avoid double buffering
2011 * for blocksize < pagesize because page dirty
2012 * flag is turned off during write and dirty
2013 * buffers are not properly collected for
2014 * pages crossing over segments.
2016 err = nilfs_segctor_wait(sci);
2018 goto failed_to_write;
2020 } while (sci->sc_stage.scnt != NILFS_ST_DONE);
2023 nilfs_segctor_drop_written_files(sci, nilfs);
2027 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2028 nilfs_redirty_inodes(&sci->sc_dirty_files);
2031 if (nilfs_doing_gc())
2032 nilfs_redirty_inodes(&sci->sc_gc_inodes);
2033 nilfs_segctor_abort_construction(sci, nilfs, err);
2038 * nilfs_segctor_start_timer - set timer of background write
2039 * @sci: nilfs_sc_info
2041 * If the timer has already been set, it ignores the new request.
2042 * This function MUST be called within a section locking the segment
2045 static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2047 spin_lock(&sci->sc_state_lock);
2048 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2049 sci->sc_timer.expires = jiffies + sci->sc_interval;
2050 add_timer(&sci->sc_timer);
2051 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2053 spin_unlock(&sci->sc_state_lock);
2056 static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2058 spin_lock(&sci->sc_state_lock);
2059 if (!(sci->sc_flush_request & (1 << bn))) {
2060 unsigned long prev_req = sci->sc_flush_request;
2062 sci->sc_flush_request |= (1 << bn);
2064 wake_up(&sci->sc_wait_daemon);
2066 spin_unlock(&sci->sc_state_lock);
2070 * nilfs_flush_segment - trigger a segment construction for resource control
2072 * @ino: inode number of the file to be flushed out.
2074 void nilfs_flush_segment(struct super_block *sb, ino_t ino)
2076 struct the_nilfs *nilfs = sb->s_fs_info;
2077 struct nilfs_sc_info *sci = nilfs->ns_writer;
2079 if (!sci || nilfs_doing_construction())
2081 nilfs_segctor_do_flush(sci, NILFS_MDT_INODE(sb, ino) ? ino : 0);
2082 /* assign bit 0 to data files */
2085 struct nilfs_segctor_wait_request {
2092 static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2094 struct nilfs_segctor_wait_request wait_req;
2097 spin_lock(&sci->sc_state_lock);
2098 init_wait(&wait_req.wq);
2100 atomic_set(&wait_req.done, 0);
2101 wait_req.seq = ++sci->sc_seq_request;
2102 spin_unlock(&sci->sc_state_lock);
2104 init_waitqueue_entry(&wait_req.wq, current);
2105 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2106 set_current_state(TASK_INTERRUPTIBLE);
2107 wake_up(&sci->sc_wait_daemon);
2110 if (atomic_read(&wait_req.done)) {
2114 if (!signal_pending(current)) {
2121 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2125 static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err)
2127 struct nilfs_segctor_wait_request *wrq, *n;
2128 unsigned long flags;
2130 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2131 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.task_list,
2133 if (!atomic_read(&wrq->done) &&
2134 nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq)) {
2136 atomic_set(&wrq->done, 1);
2138 if (atomic_read(&wrq->done)) {
2139 wrq->wq.func(&wrq->wq,
2140 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2144 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2148 * nilfs_construct_segment - construct a logical segment
2151 * Return Value: On success, 0 is retured. On errors, one of the following
2152 * negative error code is returned.
2154 * %-EROFS - Read only filesystem.
2158 * %-ENOSPC - No space left on device (only in a panic state).
2160 * %-ERESTARTSYS - Interrupted.
2162 * %-ENOMEM - Insufficient memory available.
2164 int nilfs_construct_segment(struct super_block *sb)
2166 struct the_nilfs *nilfs = sb->s_fs_info;
2167 struct nilfs_sc_info *sci = nilfs->ns_writer;
2168 struct nilfs_transaction_info *ti;
2174 /* A call inside transactions causes a deadlock. */
2175 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2177 err = nilfs_segctor_sync(sci);
2182 * nilfs_construct_dsync_segment - construct a data-only logical segment
2184 * @inode: inode whose data blocks should be written out
2185 * @start: start byte offset
2186 * @end: end byte offset (inclusive)
2188 * Return Value: On success, 0 is retured. On errors, one of the following
2189 * negative error code is returned.
2191 * %-EROFS - Read only filesystem.
2195 * %-ENOSPC - No space left on device (only in a panic state).
2197 * %-ERESTARTSYS - Interrupted.
2199 * %-ENOMEM - Insufficient memory available.
2201 int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2202 loff_t start, loff_t end)
2204 struct the_nilfs *nilfs = sb->s_fs_info;
2205 struct nilfs_sc_info *sci = nilfs->ns_writer;
2206 struct nilfs_inode_info *ii;
2207 struct nilfs_transaction_info ti;
2213 nilfs_transaction_lock(sb, &ti, 0);
2215 ii = NILFS_I(inode);
2216 if (test_bit(NILFS_I_INODE_DIRTY, &ii->i_state) ||
2217 nilfs_test_opt(nilfs, STRICT_ORDER) ||
2218 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2219 nilfs_discontinued(nilfs)) {
2220 nilfs_transaction_unlock(sb);
2221 err = nilfs_segctor_sync(sci);
2225 spin_lock(&nilfs->ns_inode_lock);
2226 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2227 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2228 spin_unlock(&nilfs->ns_inode_lock);
2229 nilfs_transaction_unlock(sb);
2232 spin_unlock(&nilfs->ns_inode_lock);
2233 sci->sc_dsync_inode = ii;
2234 sci->sc_dsync_start = start;
2235 sci->sc_dsync_end = end;
2237 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2239 nilfs_transaction_unlock(sb);
2243 #define FLUSH_FILE_BIT (0x1) /* data file only */
2244 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2247 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2248 * @sci: segment constructor object
2250 static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
2252 spin_lock(&sci->sc_state_lock);
2253 sci->sc_seq_accepted = sci->sc_seq_request;
2254 spin_unlock(&sci->sc_state_lock);
2255 del_timer_sync(&sci->sc_timer);
2259 * nilfs_segctor_notify - notify the result of request to caller threads
2260 * @sci: segment constructor object
2261 * @mode: mode of log forming
2262 * @err: error code to be notified
2264 static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
2266 /* Clear requests (even when the construction failed) */
2267 spin_lock(&sci->sc_state_lock);
2269 if (mode == SC_LSEG_SR) {
2270 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
2271 sci->sc_seq_done = sci->sc_seq_accepted;
2272 nilfs_segctor_wakeup(sci, err);
2273 sci->sc_flush_request = 0;
2275 if (mode == SC_FLUSH_FILE)
2276 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
2277 else if (mode == SC_FLUSH_DAT)
2278 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2280 /* re-enable timer if checkpoint creation was not done */
2281 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2282 time_before(jiffies, sci->sc_timer.expires))
2283 add_timer(&sci->sc_timer);
2285 spin_unlock(&sci->sc_state_lock);
2289 * nilfs_segctor_construct - form logs and write them to disk
2290 * @sci: segment constructor object
2291 * @mode: mode of log forming
2293 static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
2295 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2296 struct nilfs_super_block **sbp;
2299 nilfs_segctor_accept(sci);
2301 if (nilfs_discontinued(nilfs))
2303 if (!nilfs_segctor_confirm(sci))
2304 err = nilfs_segctor_do_construct(sci, mode);
2307 if (mode != SC_FLUSH_DAT)
2308 atomic_set(&nilfs->ns_ndirtyblks, 0);
2309 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2310 nilfs_discontinued(nilfs)) {
2311 down_write(&nilfs->ns_sem);
2313 sbp = nilfs_prepare_super(sci->sc_super,
2314 nilfs_sb_will_flip(nilfs));
2316 nilfs_set_log_cursor(sbp[0], nilfs);
2317 err = nilfs_commit_super(sci->sc_super,
2320 up_write(&nilfs->ns_sem);
2324 nilfs_segctor_notify(sci, mode, err);
2328 static void nilfs_construction_timeout(unsigned long data)
2330 struct task_struct *p = (struct task_struct *)data;
2335 nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2337 struct nilfs_inode_info *ii, *n;
2339 list_for_each_entry_safe(ii, n, head, i_dirty) {
2340 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2342 list_del_init(&ii->i_dirty);
2343 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
2344 nilfs_btnode_cache_clear(&ii->i_btnode_cache);
2345 iput(&ii->vfs_inode);
2349 int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2352 struct the_nilfs *nilfs = sb->s_fs_info;
2353 struct nilfs_sc_info *sci = nilfs->ns_writer;
2354 struct nilfs_transaction_info ti;
2360 nilfs_transaction_lock(sb, &ti, 1);
2362 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
2366 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
2367 if (unlikely(err)) {
2368 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
2372 sci->sc_freesegs = kbufs[4];
2373 sci->sc_nfreesegs = argv[4].v_nmembs;
2374 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
2377 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
2378 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
2383 nilfs_warning(sb, __func__,
2384 "segment construction failed. (err=%d)", err);
2385 set_current_state(TASK_INTERRUPTIBLE);
2386 schedule_timeout(sci->sc_interval);
2388 if (nilfs_test_opt(nilfs, DISCARD)) {
2389 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2393 "NILFS warning: error %d on discard request, "
2394 "turning discards off for the device\n", ret);
2395 nilfs_clear_opt(nilfs, DISCARD);
2400 sci->sc_freesegs = NULL;
2401 sci->sc_nfreesegs = 0;
2402 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
2403 nilfs_transaction_unlock(sb);
2407 static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2409 struct nilfs_transaction_info ti;
2411 nilfs_transaction_lock(sci->sc_super, &ti, 0);
2412 nilfs_segctor_construct(sci, mode);
2415 * Unclosed segment should be retried. We do this using sc_timer.
2416 * Timeout of sc_timer will invoke complete construction which leads
2417 * to close the current logical segment.
2419 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2420 nilfs_segctor_start_timer(sci);
2422 nilfs_transaction_unlock(sci->sc_super);
2425 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2430 spin_lock(&sci->sc_state_lock);
2431 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2432 SC_FLUSH_DAT : SC_FLUSH_FILE;
2433 spin_unlock(&sci->sc_state_lock);
2436 err = nilfs_segctor_do_construct(sci, mode);
2438 spin_lock(&sci->sc_state_lock);
2439 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2440 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2441 spin_unlock(&sci->sc_state_lock);
2443 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2446 static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2448 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2449 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2450 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2451 return SC_FLUSH_FILE;
2452 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2453 return SC_FLUSH_DAT;
2459 * nilfs_segctor_thread - main loop of the segment constructor thread.
2460 * @arg: pointer to a struct nilfs_sc_info.
2462 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2463 * to execute segment constructions.
2465 static int nilfs_segctor_thread(void *arg)
2467 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
2468 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2471 sci->sc_timer.data = (unsigned long)current;
2472 sci->sc_timer.function = nilfs_construction_timeout;
2475 sci->sc_task = current;
2476 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_start_thread() */
2478 "segctord starting. Construction interval = %lu seconds, "
2479 "CP frequency < %lu seconds\n",
2480 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2482 spin_lock(&sci->sc_state_lock);
2487 if (sci->sc_state & NILFS_SEGCTOR_QUIT)
2490 if (timeout || sci->sc_seq_request != sci->sc_seq_done)
2492 else if (!sci->sc_flush_request)
2495 mode = nilfs_segctor_flush_mode(sci);
2497 spin_unlock(&sci->sc_state_lock);
2498 nilfs_segctor_thread_construct(sci, mode);
2499 spin_lock(&sci->sc_state_lock);
2504 if (freezing(current)) {
2505 spin_unlock(&sci->sc_state_lock);
2507 spin_lock(&sci->sc_state_lock);
2510 int should_sleep = 1;
2512 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2513 TASK_INTERRUPTIBLE);
2515 if (sci->sc_seq_request != sci->sc_seq_done)
2517 else if (sci->sc_flush_request)
2519 else if (sci->sc_state & NILFS_SEGCTOR_COMMIT)
2520 should_sleep = time_before(jiffies,
2521 sci->sc_timer.expires);
2524 spin_unlock(&sci->sc_state_lock);
2526 spin_lock(&sci->sc_state_lock);
2528 finish_wait(&sci->sc_wait_daemon, &wait);
2529 timeout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2530 time_after_eq(jiffies, sci->sc_timer.expires));
2532 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
2533 set_nilfs_discontinued(nilfs);
2538 spin_unlock(&sci->sc_state_lock);
2541 sci->sc_task = NULL;
2542 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2546 static int nilfs_segctor_start_thread(struct nilfs_sc_info *sci)
2548 struct task_struct *t;
2550 t = kthread_run(nilfs_segctor_thread, sci, "segctord");
2552 int err = PTR_ERR(t);
2554 printk(KERN_ERR "NILFS: error %d creating segctord thread\n",
2558 wait_event(sci->sc_wait_task, sci->sc_task != NULL);
2562 static void nilfs_segctor_kill_thread(struct nilfs_sc_info *sci)
2563 __acquires(&sci->sc_state_lock)
2564 __releases(&sci->sc_state_lock)
2566 sci->sc_state |= NILFS_SEGCTOR_QUIT;
2568 while (sci->sc_task) {
2569 wake_up(&sci->sc_wait_daemon);
2570 spin_unlock(&sci->sc_state_lock);
2571 wait_event(sci->sc_wait_task, sci->sc_task == NULL);
2572 spin_lock(&sci->sc_state_lock);
2577 * Setup & clean-up functions
2579 static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
2580 struct nilfs_root *root)
2582 struct the_nilfs *nilfs = sb->s_fs_info;
2583 struct nilfs_sc_info *sci;
2585 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2591 nilfs_get_root(root);
2592 sci->sc_root = root;
2594 init_waitqueue_head(&sci->sc_wait_request);
2595 init_waitqueue_head(&sci->sc_wait_daemon);
2596 init_waitqueue_head(&sci->sc_wait_task);
2597 spin_lock_init(&sci->sc_state_lock);
2598 INIT_LIST_HEAD(&sci->sc_dirty_files);
2599 INIT_LIST_HEAD(&sci->sc_segbufs);
2600 INIT_LIST_HEAD(&sci->sc_write_logs);
2601 INIT_LIST_HEAD(&sci->sc_gc_inodes);
2602 INIT_LIST_HEAD(&sci->sc_iput_queue);
2603 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
2604 init_timer(&sci->sc_timer);
2606 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2607 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2608 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2610 if (nilfs->ns_interval)
2611 sci->sc_interval = HZ * nilfs->ns_interval;
2612 if (nilfs->ns_watermark)
2613 sci->sc_watermark = nilfs->ns_watermark;
2617 static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2619 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2621 /* The segctord thread was stopped and its timer was removed.
2622 But some tasks remain. */
2624 struct nilfs_transaction_info ti;
2626 nilfs_transaction_lock(sci->sc_super, &ti, 0);
2627 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
2628 nilfs_transaction_unlock(sci->sc_super);
2630 flush_work(&sci->sc_iput_work);
2632 } while (ret && retrycount-- > 0);
2636 * nilfs_segctor_destroy - destroy the segment constructor.
2637 * @sci: nilfs_sc_info
2639 * nilfs_segctor_destroy() kills the segctord thread and frees
2640 * the nilfs_sc_info struct.
2641 * Caller must hold the segment semaphore.
2643 static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2645 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2648 up_write(&nilfs->ns_segctor_sem);
2650 spin_lock(&sci->sc_state_lock);
2651 nilfs_segctor_kill_thread(sci);
2652 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2653 || sci->sc_seq_request != sci->sc_seq_done);
2654 spin_unlock(&sci->sc_state_lock);
2656 if (flush_work(&sci->sc_iput_work))
2659 if (flag || !nilfs_segctor_confirm(sci))
2660 nilfs_segctor_write_out(sci);
2662 if (!list_empty(&sci->sc_dirty_files)) {
2663 nilfs_warning(sci->sc_super, __func__,
2664 "dirty file(s) after the final construction\n");
2665 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
2668 if (!list_empty(&sci->sc_iput_queue)) {
2669 nilfs_warning(sci->sc_super, __func__,
2670 "iput queue is not empty\n");
2671 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2674 WARN_ON(!list_empty(&sci->sc_segbufs));
2675 WARN_ON(!list_empty(&sci->sc_write_logs));
2677 nilfs_put_root(sci->sc_root);
2679 down_write(&nilfs->ns_segctor_sem);
2681 del_timer_sync(&sci->sc_timer);
2686 * nilfs_attach_log_writer - attach log writer
2687 * @sb: super block instance
2688 * @root: root object of the current filesystem tree
2690 * This allocates a log writer object, initializes it, and starts the
2693 * Return Value: On success, 0 is returned. On error, one of the following
2694 * negative error code is returned.
2696 * %-ENOMEM - Insufficient memory available.
2698 int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
2700 struct the_nilfs *nilfs = sb->s_fs_info;
2703 if (nilfs->ns_writer) {
2705 * This happens if the filesystem was remounted
2706 * read/write after nilfs_error degenerated it into a
2709 nilfs_detach_log_writer(sb);
2712 nilfs->ns_writer = nilfs_segctor_new(sb, root);
2713 if (!nilfs->ns_writer)
2716 err = nilfs_segctor_start_thread(nilfs->ns_writer);
2718 kfree(nilfs->ns_writer);
2719 nilfs->ns_writer = NULL;
2725 * nilfs_detach_log_writer - destroy log writer
2726 * @sb: super block instance
2728 * This kills log writer daemon, frees the log writer object, and
2729 * destroys list of dirty files.
2731 void nilfs_detach_log_writer(struct super_block *sb)
2733 struct the_nilfs *nilfs = sb->s_fs_info;
2734 LIST_HEAD(garbage_list);
2736 down_write(&nilfs->ns_segctor_sem);
2737 if (nilfs->ns_writer) {
2738 nilfs_segctor_destroy(nilfs->ns_writer);
2739 nilfs->ns_writer = NULL;
2742 /* Force to free the list of dirty files */
2743 spin_lock(&nilfs->ns_inode_lock);
2744 if (!list_empty(&nilfs->ns_dirty_files)) {
2745 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
2746 nilfs_warning(sb, __func__,
2747 "Hit dirty file after stopped log writer\n");
2749 spin_unlock(&nilfs->ns_inode_lock);
2750 up_write(&nilfs->ns_segctor_sem);
2752 nilfs_dispose_list(nilfs, &garbage_list, 1);