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)
119 static int nilfs_prepare_segment_lock(struct nilfs_transaction_info
*ti
)
121 struct nilfs_transaction_info
*cur_ti
= current
->journal_info
;
125 if (cur_ti
->ti_magic
== NILFS_TI_MAGIC
)
126 return ++cur_ti
->ti_count
;
129 * If journal_info field is occupied by other FS,
130 * it is saved and will be restored on
131 * nilfs_transaction_commit().
134 "NILFS warning: journal info from a different "
136 save
= current
->journal_info
;
140 ti
= kmem_cache_alloc(nilfs_transaction_cachep
, GFP_NOFS
);
143 ti
->ti_flags
= NILFS_TI_DYNAMIC_ALLOC
;
149 ti
->ti_magic
= NILFS_TI_MAGIC
;
150 current
->journal_info
= ti
;
155 * nilfs_transaction_begin - start indivisible file operations.
157 * @ti: nilfs_transaction_info
158 * @vacancy_check: flags for vacancy rate checks
160 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
161 * the segment semaphore, to make a segment construction and write tasks
162 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
163 * The region enclosed by these two functions can be nested. To avoid a
164 * deadlock, the semaphore is only acquired or released in the outermost call.
166 * This function allocates a nilfs_transaction_info struct to keep context
167 * information on it. It is initialized and hooked onto the current task in
168 * the outermost call. If a pre-allocated struct is given to @ti, it is used
169 * instead; otherwise a new struct is assigned from a slab.
171 * When @vacancy_check flag is set, this function will check the amount of
172 * free space, and will wait for the GC to reclaim disk space if low capacity.
174 * Return Value: On success, 0 is returned. On error, one of the following
175 * negative error code is returned.
177 * %-ENOMEM - Insufficient memory available.
179 * %-ENOSPC - No space left on device
181 int nilfs_transaction_begin(struct super_block
*sb
,
182 struct nilfs_transaction_info
*ti
,
185 struct nilfs_sb_info
*sbi
;
186 struct the_nilfs
*nilfs
;
187 int ret
= nilfs_prepare_segment_lock(ti
);
189 if (unlikely(ret
< 0))
195 nilfs
= sbi
->s_nilfs
;
196 down_read(&nilfs
->ns_segctor_sem
);
197 if (vacancy_check
&& nilfs_near_disk_full(nilfs
)) {
198 up_read(&nilfs
->ns_segctor_sem
);
205 ti
= current
->journal_info
;
206 current
->journal_info
= ti
->ti_save
;
207 if (ti
->ti_flags
& NILFS_TI_DYNAMIC_ALLOC
)
208 kmem_cache_free(nilfs_transaction_cachep
, ti
);
213 * nilfs_transaction_commit - commit indivisible file operations.
216 * nilfs_transaction_commit() releases the read semaphore which is
217 * acquired by nilfs_transaction_begin(). This is only performed
218 * in outermost call of this function. If a commit flag is set,
219 * nilfs_transaction_commit() sets a timer to start the segment
220 * constructor. If a sync flag is set, it starts construction
223 int nilfs_transaction_commit(struct super_block
*sb
)
225 struct nilfs_transaction_info
*ti
= current
->journal_info
;
226 struct nilfs_sb_info
*sbi
;
227 struct nilfs_sc_info
*sci
;
230 BUG_ON(ti
== NULL
|| ti
->ti_magic
!= NILFS_TI_MAGIC
);
231 ti
->ti_flags
|= NILFS_TI_COMMIT
;
232 if (ti
->ti_count
> 0) {
239 if (ti
->ti_flags
& NILFS_TI_COMMIT
)
240 nilfs_segctor_start_timer(sci
);
241 if (atomic_read(&sbi
->s_nilfs
->ns_ndirtyblks
) >
243 nilfs_segctor_do_flush(sci
, 0);
245 up_read(&sbi
->s_nilfs
->ns_segctor_sem
);
246 current
->journal_info
= ti
->ti_save
;
248 if (ti
->ti_flags
& NILFS_TI_SYNC
)
249 err
= nilfs_construct_segment(sb
);
250 if (ti
->ti_flags
& NILFS_TI_DYNAMIC_ALLOC
)
251 kmem_cache_free(nilfs_transaction_cachep
, ti
);
255 void nilfs_transaction_abort(struct super_block
*sb
)
257 struct nilfs_transaction_info
*ti
= current
->journal_info
;
259 BUG_ON(ti
== NULL
|| ti
->ti_magic
!= NILFS_TI_MAGIC
);
260 if (ti
->ti_count
> 0) {
264 up_read(&NILFS_SB(sb
)->s_nilfs
->ns_segctor_sem
);
266 current
->journal_info
= ti
->ti_save
;
267 if (ti
->ti_flags
& NILFS_TI_DYNAMIC_ALLOC
)
268 kmem_cache_free(nilfs_transaction_cachep
, ti
);
271 void nilfs_relax_pressure_in_lock(struct super_block
*sb
)
273 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
274 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
275 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
277 if (!sci
|| !sci
->sc_flush_request
)
280 set_bit(NILFS_SC_PRIOR_FLUSH
, &sci
->sc_flags
);
281 up_read(&nilfs
->ns_segctor_sem
);
283 down_write(&nilfs
->ns_segctor_sem
);
284 if (sci
->sc_flush_request
&&
285 test_bit(NILFS_SC_PRIOR_FLUSH
, &sci
->sc_flags
)) {
286 struct nilfs_transaction_info
*ti
= current
->journal_info
;
288 ti
->ti_flags
|= NILFS_TI_WRITER
;
289 nilfs_segctor_do_immediate_flush(sci
);
290 ti
->ti_flags
&= ~NILFS_TI_WRITER
;
292 downgrade_write(&nilfs
->ns_segctor_sem
);
295 static void nilfs_transaction_lock(struct nilfs_sb_info
*sbi
,
296 struct nilfs_transaction_info
*ti
,
299 struct nilfs_transaction_info
*cur_ti
= current
->journal_info
;
302 ti
->ti_flags
= NILFS_TI_WRITER
;
304 ti
->ti_save
= cur_ti
;
305 ti
->ti_magic
= NILFS_TI_MAGIC
;
306 INIT_LIST_HEAD(&ti
->ti_garbage
);
307 current
->journal_info
= ti
;
310 down_write(&sbi
->s_nilfs
->ns_segctor_sem
);
311 if (!test_bit(NILFS_SC_PRIOR_FLUSH
, &NILFS_SC(sbi
)->sc_flags
))
314 nilfs_segctor_do_immediate_flush(NILFS_SC(sbi
));
316 up_write(&sbi
->s_nilfs
->ns_segctor_sem
);
320 ti
->ti_flags
|= NILFS_TI_GC
;
323 static void nilfs_transaction_unlock(struct nilfs_sb_info
*sbi
)
325 struct nilfs_transaction_info
*ti
= current
->journal_info
;
327 BUG_ON(ti
== NULL
|| ti
->ti_magic
!= NILFS_TI_MAGIC
);
328 BUG_ON(ti
->ti_count
> 0);
330 up_write(&sbi
->s_nilfs
->ns_segctor_sem
);
331 current
->journal_info
= ti
->ti_save
;
332 if (!list_empty(&ti
->ti_garbage
))
333 nilfs_dispose_list(sbi
, &ti
->ti_garbage
, 0);
336 static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info
*sci
,
337 struct nilfs_segsum_pointer
*ssp
,
340 struct nilfs_segment_buffer
*segbuf
= sci
->sc_curseg
;
341 unsigned blocksize
= sci
->sc_super
->s_blocksize
;
344 if (unlikely(ssp
->offset
+ bytes
> blocksize
)) {
346 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp
->bh
,
347 &segbuf
->sb_segsum_buffers
));
348 ssp
->bh
= NILFS_SEGBUF_NEXT_BH(ssp
->bh
);
350 p
= ssp
->bh
->b_data
+ ssp
->offset
;
351 ssp
->offset
+= bytes
;
356 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
357 * @sci: nilfs_sc_info
359 static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info
*sci
)
361 struct nilfs_segment_buffer
*segbuf
= sci
->sc_curseg
;
362 struct buffer_head
*sumbh
;
367 if (nilfs_doing_gc())
369 err
= nilfs_segbuf_reset(segbuf
, flags
, sci
->sc_seg_ctime
,
370 sci
->sc_sbi
->s_nilfs
->ns_cno
);
374 sumbh
= NILFS_SEGBUF_FIRST_BH(&segbuf
->sb_segsum_buffers
);
375 sumbytes
= segbuf
->sb_sum
.sumbytes
;
376 sci
->sc_finfo_ptr
.bh
= sumbh
; sci
->sc_finfo_ptr
.offset
= sumbytes
;
377 sci
->sc_binfo_ptr
.bh
= sumbh
; sci
->sc_binfo_ptr
.offset
= sumbytes
;
378 sci
->sc_blk_cnt
= sci
->sc_datablk_cnt
= 0;
382 static int nilfs_segctor_feed_segment(struct nilfs_sc_info
*sci
)
384 sci
->sc_nblk_this_inc
+= sci
->sc_curseg
->sb_sum
.nblocks
;
385 if (NILFS_SEGBUF_IS_LAST(sci
->sc_curseg
, &sci
->sc_segbufs
))
386 return -E2BIG
; /* The current segment is filled up
388 sci
->sc_curseg
= NILFS_NEXT_SEGBUF(sci
->sc_curseg
);
389 return nilfs_segctor_reset_segment_buffer(sci
);
392 static int nilfs_segctor_add_super_root(struct nilfs_sc_info
*sci
)
394 struct nilfs_segment_buffer
*segbuf
= sci
->sc_curseg
;
397 if (segbuf
->sb_sum
.nblocks
>= segbuf
->sb_rest_blocks
) {
398 err
= nilfs_segctor_feed_segment(sci
);
401 segbuf
= sci
->sc_curseg
;
403 err
= nilfs_segbuf_extend_payload(segbuf
, &segbuf
->sb_super_root
);
405 segbuf
->sb_sum
.flags
|= NILFS_SS_SR
;
410 * Functions for making segment summary and payloads
412 static int nilfs_segctor_segsum_block_required(
413 struct nilfs_sc_info
*sci
, const struct nilfs_segsum_pointer
*ssp
,
416 unsigned blocksize
= sci
->sc_super
->s_blocksize
;
417 /* Size of finfo and binfo is enough small against blocksize */
419 return ssp
->offset
+ binfo_size
+
420 (!sci
->sc_blk_cnt
? sizeof(struct nilfs_finfo
) : 0) >
424 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info
*sci
,
427 sci
->sc_curseg
->sb_sum
.nfinfo
++;
428 sci
->sc_binfo_ptr
= sci
->sc_finfo_ptr
;
429 nilfs_segctor_map_segsum_entry(
430 sci
, &sci
->sc_binfo_ptr
, sizeof(struct nilfs_finfo
));
432 if (inode
->i_sb
&& !test_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
))
433 set_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
);
437 static void nilfs_segctor_end_finfo(struct nilfs_sc_info
*sci
,
440 struct nilfs_finfo
*finfo
;
441 struct nilfs_inode_info
*ii
;
442 struct nilfs_segment_buffer
*segbuf
;
444 if (sci
->sc_blk_cnt
== 0)
448 finfo
= nilfs_segctor_map_segsum_entry(sci
, &sci
->sc_finfo_ptr
,
450 finfo
->fi_ino
= cpu_to_le64(inode
->i_ino
);
451 finfo
->fi_nblocks
= cpu_to_le32(sci
->sc_blk_cnt
);
452 finfo
->fi_ndatablk
= cpu_to_le32(sci
->sc_datablk_cnt
);
453 finfo
->fi_cno
= cpu_to_le64(ii
->i_cno
);
455 segbuf
= sci
->sc_curseg
;
456 segbuf
->sb_sum
.sumbytes
= sci
->sc_binfo_ptr
.offset
+
457 sci
->sc_super
->s_blocksize
* (segbuf
->sb_sum
.nsumblk
- 1);
458 sci
->sc_finfo_ptr
= sci
->sc_binfo_ptr
;
459 sci
->sc_blk_cnt
= sci
->sc_datablk_cnt
= 0;
462 static int nilfs_segctor_add_file_block(struct nilfs_sc_info
*sci
,
463 struct buffer_head
*bh
,
467 struct nilfs_segment_buffer
*segbuf
;
468 int required
, err
= 0;
471 segbuf
= sci
->sc_curseg
;
472 required
= nilfs_segctor_segsum_block_required(
473 sci
, &sci
->sc_binfo_ptr
, binfo_size
);
474 if (segbuf
->sb_sum
.nblocks
+ required
+ 1 > segbuf
->sb_rest_blocks
) {
475 nilfs_segctor_end_finfo(sci
, inode
);
476 err
= nilfs_segctor_feed_segment(sci
);
481 if (unlikely(required
)) {
482 err
= nilfs_segbuf_extend_segsum(segbuf
);
486 if (sci
->sc_blk_cnt
== 0)
487 nilfs_segctor_begin_finfo(sci
, inode
);
489 nilfs_segctor_map_segsum_entry(sci
, &sci
->sc_binfo_ptr
, binfo_size
);
490 /* Substitution to vblocknr is delayed until update_blocknr() */
491 nilfs_segbuf_add_file_buffer(segbuf
, bh
);
497 static int nilfs_handle_bmap_error(int err
, const char *fname
,
498 struct inode
*inode
, struct super_block
*sb
)
500 if (err
== -EINVAL
) {
501 nilfs_error(sb
, fname
, "broken bmap (inode=%lu)\n",
509 * Callback functions that enumerate, mark, and collect dirty blocks
511 static int nilfs_collect_file_data(struct nilfs_sc_info
*sci
,
512 struct buffer_head
*bh
, struct inode
*inode
)
516 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
517 if (unlikely(err
< 0))
518 return nilfs_handle_bmap_error(err
, __func__
, inode
,
521 err
= nilfs_segctor_add_file_block(sci
, bh
, inode
,
522 sizeof(struct nilfs_binfo_v
));
524 sci
->sc_datablk_cnt
++;
528 static int nilfs_collect_file_node(struct nilfs_sc_info
*sci
,
529 struct buffer_head
*bh
,
534 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
535 if (unlikely(err
< 0))
536 return nilfs_handle_bmap_error(err
, __func__
, inode
,
541 static int nilfs_collect_file_bmap(struct nilfs_sc_info
*sci
,
542 struct buffer_head
*bh
,
545 WARN_ON(!buffer_dirty(bh
));
546 return nilfs_segctor_add_file_block(sci
, bh
, inode
, sizeof(__le64
));
549 static void nilfs_write_file_data_binfo(struct nilfs_sc_info
*sci
,
550 struct nilfs_segsum_pointer
*ssp
,
551 union nilfs_binfo
*binfo
)
553 struct nilfs_binfo_v
*binfo_v
= nilfs_segctor_map_segsum_entry(
554 sci
, ssp
, sizeof(*binfo_v
));
555 *binfo_v
= binfo
->bi_v
;
558 static void nilfs_write_file_node_binfo(struct nilfs_sc_info
*sci
,
559 struct nilfs_segsum_pointer
*ssp
,
560 union nilfs_binfo
*binfo
)
562 __le64
*vblocknr
= nilfs_segctor_map_segsum_entry(
563 sci
, ssp
, sizeof(*vblocknr
));
564 *vblocknr
= binfo
->bi_v
.bi_vblocknr
;
567 static struct nilfs_sc_operations nilfs_sc_file_ops
= {
568 .collect_data
= nilfs_collect_file_data
,
569 .collect_node
= nilfs_collect_file_node
,
570 .collect_bmap
= nilfs_collect_file_bmap
,
571 .write_data_binfo
= nilfs_write_file_data_binfo
,
572 .write_node_binfo
= nilfs_write_file_node_binfo
,
575 static int nilfs_collect_dat_data(struct nilfs_sc_info
*sci
,
576 struct buffer_head
*bh
, struct inode
*inode
)
580 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
581 if (unlikely(err
< 0))
582 return nilfs_handle_bmap_error(err
, __func__
, inode
,
585 err
= nilfs_segctor_add_file_block(sci
, bh
, inode
, sizeof(__le64
));
587 sci
->sc_datablk_cnt
++;
591 static int nilfs_collect_dat_bmap(struct nilfs_sc_info
*sci
,
592 struct buffer_head
*bh
, struct inode
*inode
)
594 WARN_ON(!buffer_dirty(bh
));
595 return nilfs_segctor_add_file_block(sci
, bh
, inode
,
596 sizeof(struct nilfs_binfo_dat
));
599 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info
*sci
,
600 struct nilfs_segsum_pointer
*ssp
,
601 union nilfs_binfo
*binfo
)
603 __le64
*blkoff
= nilfs_segctor_map_segsum_entry(sci
, ssp
,
605 *blkoff
= binfo
->bi_dat
.bi_blkoff
;
608 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info
*sci
,
609 struct nilfs_segsum_pointer
*ssp
,
610 union nilfs_binfo
*binfo
)
612 struct nilfs_binfo_dat
*binfo_dat
=
613 nilfs_segctor_map_segsum_entry(sci
, ssp
, sizeof(*binfo_dat
));
614 *binfo_dat
= binfo
->bi_dat
;
617 static struct nilfs_sc_operations nilfs_sc_dat_ops
= {
618 .collect_data
= nilfs_collect_dat_data
,
619 .collect_node
= nilfs_collect_file_node
,
620 .collect_bmap
= nilfs_collect_dat_bmap
,
621 .write_data_binfo
= nilfs_write_dat_data_binfo
,
622 .write_node_binfo
= nilfs_write_dat_node_binfo
,
625 static struct nilfs_sc_operations nilfs_sc_dsync_ops
= {
626 .collect_data
= nilfs_collect_file_data
,
627 .collect_node
= NULL
,
628 .collect_bmap
= NULL
,
629 .write_data_binfo
= nilfs_write_file_data_binfo
,
630 .write_node_binfo
= NULL
,
633 static size_t nilfs_lookup_dirty_data_buffers(struct inode
*inode
,
634 struct list_head
*listp
,
636 loff_t start
, loff_t end
)
638 struct address_space
*mapping
= inode
->i_mapping
;
640 pgoff_t index
= 0, last
= ULONG_MAX
;
644 if (unlikely(start
!= 0 || end
!= LLONG_MAX
)) {
646 * A valid range is given for sync-ing data pages. The
647 * range is rounded to per-page; extra dirty buffers
648 * may be included if blocksize < pagesize.
650 index
= start
>> PAGE_SHIFT
;
651 last
= end
>> PAGE_SHIFT
;
653 pagevec_init(&pvec
, 0);
655 if (unlikely(index
> last
) ||
656 !pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
657 min_t(pgoff_t
, last
- index
,
658 PAGEVEC_SIZE
- 1) + 1))
661 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
662 struct buffer_head
*bh
, *head
;
663 struct page
*page
= pvec
.pages
[i
];
665 if (unlikely(page
->index
> last
))
670 if (!page_has_buffers(page
))
671 create_empty_buffers(page
,
672 1 << inode
->i_blkbits
, 0);
676 bh
= head
= page_buffers(page
);
678 if (!buffer_dirty(bh
))
681 list_add_tail(&bh
->b_assoc_buffers
, listp
);
683 if (unlikely(ndirties
>= nlimit
)) {
684 pagevec_release(&pvec
);
688 } while (bh
= bh
->b_this_page
, bh
!= head
);
690 pagevec_release(&pvec
);
695 static void nilfs_lookup_dirty_node_buffers(struct inode
*inode
,
696 struct list_head
*listp
)
698 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
699 struct address_space
*mapping
= &ii
->i_btnode_cache
;
701 struct buffer_head
*bh
, *head
;
705 pagevec_init(&pvec
, 0);
707 while (pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
709 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
710 bh
= head
= page_buffers(pvec
.pages
[i
]);
712 if (buffer_dirty(bh
)) {
714 list_add_tail(&bh
->b_assoc_buffers
,
717 bh
= bh
->b_this_page
;
718 } while (bh
!= head
);
720 pagevec_release(&pvec
);
725 static void nilfs_dispose_list(struct nilfs_sb_info
*sbi
,
726 struct list_head
*head
, int force
)
728 struct nilfs_inode_info
*ii
, *n
;
729 struct nilfs_inode_info
*ivec
[SC_N_INODEVEC
], **pii
;
732 while (!list_empty(head
)) {
733 spin_lock(&sbi
->s_inode_lock
);
734 list_for_each_entry_safe(ii
, n
, head
, i_dirty
) {
735 list_del_init(&ii
->i_dirty
);
737 if (unlikely(ii
->i_bh
)) {
741 } else if (test_bit(NILFS_I_DIRTY
, &ii
->i_state
)) {
742 set_bit(NILFS_I_QUEUED
, &ii
->i_state
);
743 list_add_tail(&ii
->i_dirty
,
744 &sbi
->s_dirty_files
);
748 if (nv
== SC_N_INODEVEC
)
751 spin_unlock(&sbi
->s_inode_lock
);
753 for (pii
= ivec
; nv
> 0; pii
++, nv
--)
754 iput(&(*pii
)->vfs_inode
);
758 static int nilfs_test_metadata_dirty(struct nilfs_sb_info
*sbi
)
760 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
763 if (nilfs_mdt_fetch_dirty(sbi
->s_ifile
))
765 if (nilfs_mdt_fetch_dirty(nilfs
->ns_cpfile
))
767 if (nilfs_mdt_fetch_dirty(nilfs
->ns_sufile
))
769 if (ret
|| nilfs_doing_gc())
770 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs
)))
775 static int nilfs_segctor_clean(struct nilfs_sc_info
*sci
)
777 return list_empty(&sci
->sc_dirty_files
) &&
778 !test_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
) &&
779 sci
->sc_nfreesegs
== 0 &&
780 (!nilfs_doing_gc() || list_empty(&sci
->sc_gc_inodes
));
783 static int nilfs_segctor_confirm(struct nilfs_sc_info
*sci
)
785 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
788 if (nilfs_test_metadata_dirty(sbi
))
789 set_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
791 spin_lock(&sbi
->s_inode_lock
);
792 if (list_empty(&sbi
->s_dirty_files
) && nilfs_segctor_clean(sci
))
795 spin_unlock(&sbi
->s_inode_lock
);
799 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info
*sci
)
801 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
802 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
804 nilfs_mdt_clear_dirty(sbi
->s_ifile
);
805 nilfs_mdt_clear_dirty(nilfs
->ns_cpfile
);
806 nilfs_mdt_clear_dirty(nilfs
->ns_sufile
);
807 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs
));
810 static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info
*sci
)
812 struct the_nilfs
*nilfs
= sci
->sc_sbi
->s_nilfs
;
813 struct buffer_head
*bh_cp
;
814 struct nilfs_checkpoint
*raw_cp
;
817 err
= nilfs_cpfile_get_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, 1,
820 /* The following code is duplicated with cpfile. But, it is
821 needed to collect the checkpoint even if it was not newly
823 nilfs_mdt_mark_buffer_dirty(bh_cp
);
824 nilfs_mdt_mark_dirty(nilfs
->ns_cpfile
);
825 nilfs_cpfile_put_checkpoint(
826 nilfs
->ns_cpfile
, nilfs
->ns_cno
, bh_cp
);
828 WARN_ON(err
== -EINVAL
|| err
== -ENOENT
);
833 static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info
*sci
)
835 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
836 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
837 struct buffer_head
*bh_cp
;
838 struct nilfs_checkpoint
*raw_cp
;
841 err
= nilfs_cpfile_get_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, 0,
844 WARN_ON(err
== -EINVAL
|| err
== -ENOENT
);
847 raw_cp
->cp_snapshot_list
.ssl_next
= 0;
848 raw_cp
->cp_snapshot_list
.ssl_prev
= 0;
849 raw_cp
->cp_inodes_count
=
850 cpu_to_le64(atomic_read(&sbi
->s_inodes_count
));
851 raw_cp
->cp_blocks_count
=
852 cpu_to_le64(atomic_read(&sbi
->s_blocks_count
));
853 raw_cp
->cp_nblk_inc
=
854 cpu_to_le64(sci
->sc_nblk_inc
+ sci
->sc_nblk_this_inc
);
855 raw_cp
->cp_create
= cpu_to_le64(sci
->sc_seg_ctime
);
856 raw_cp
->cp_cno
= cpu_to_le64(nilfs
->ns_cno
);
858 if (test_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
))
859 nilfs_checkpoint_clear_minor(raw_cp
);
861 nilfs_checkpoint_set_minor(raw_cp
);
863 nilfs_write_inode_common(sbi
->s_ifile
, &raw_cp
->cp_ifile_inode
, 1);
864 nilfs_cpfile_put_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, bh_cp
);
871 static void nilfs_fill_in_file_bmap(struct inode
*ifile
,
872 struct nilfs_inode_info
*ii
)
875 struct buffer_head
*ibh
;
876 struct nilfs_inode
*raw_inode
;
878 if (test_bit(NILFS_I_BMAP
, &ii
->i_state
)) {
881 raw_inode
= nilfs_ifile_map_inode(ifile
, ii
->vfs_inode
.i_ino
,
883 nilfs_bmap_write(ii
->i_bmap
, raw_inode
);
884 nilfs_ifile_unmap_inode(ifile
, ii
->vfs_inode
.i_ino
, ibh
);
888 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info
*sci
,
891 struct nilfs_inode_info
*ii
;
893 list_for_each_entry(ii
, &sci
->sc_dirty_files
, i_dirty
) {
894 nilfs_fill_in_file_bmap(ifile
, ii
);
895 set_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
899 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info
*sci
,
900 struct the_nilfs
*nilfs
)
902 struct buffer_head
*bh_sr
;
903 struct nilfs_super_root
*raw_sr
;
904 unsigned isz
= nilfs
->ns_inode_size
;
906 bh_sr
= NILFS_LAST_SEGBUF(&sci
->sc_segbufs
)->sb_super_root
;
907 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
909 raw_sr
->sr_bytes
= cpu_to_le16(NILFS_SR_BYTES
);
910 raw_sr
->sr_nongc_ctime
911 = cpu_to_le64(nilfs_doing_gc() ?
912 nilfs
->ns_nongc_ctime
: sci
->sc_seg_ctime
);
913 raw_sr
->sr_flags
= 0;
915 nilfs_write_inode_common(nilfs_dat_inode(nilfs
), (void *)raw_sr
+
916 NILFS_SR_DAT_OFFSET(isz
), 1);
917 nilfs_write_inode_common(nilfs
->ns_cpfile
, (void *)raw_sr
+
918 NILFS_SR_CPFILE_OFFSET(isz
), 1);
919 nilfs_write_inode_common(nilfs
->ns_sufile
, (void *)raw_sr
+
920 NILFS_SR_SUFILE_OFFSET(isz
), 1);
923 static void nilfs_redirty_inodes(struct list_head
*head
)
925 struct nilfs_inode_info
*ii
;
927 list_for_each_entry(ii
, head
, i_dirty
) {
928 if (test_bit(NILFS_I_COLLECTED
, &ii
->i_state
))
929 clear_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
933 static void nilfs_drop_collected_inodes(struct list_head
*head
)
935 struct nilfs_inode_info
*ii
;
937 list_for_each_entry(ii
, head
, i_dirty
) {
938 if (!test_and_clear_bit(NILFS_I_COLLECTED
, &ii
->i_state
))
941 clear_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
);
942 set_bit(NILFS_I_UPDATED
, &ii
->i_state
);
946 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info
*sci
,
948 struct list_head
*listp
,
949 int (*collect
)(struct nilfs_sc_info
*,
950 struct buffer_head
*,
953 struct buffer_head
*bh
, *n
;
957 list_for_each_entry_safe(bh
, n
, listp
, b_assoc_buffers
) {
958 list_del_init(&bh
->b_assoc_buffers
);
959 err
= collect(sci
, bh
, inode
);
962 goto dispose_buffers
;
968 while (!list_empty(listp
)) {
969 bh
= list_entry(listp
->next
, struct buffer_head
,
971 list_del_init(&bh
->b_assoc_buffers
);
977 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info
*sci
)
979 /* Remaining number of blocks within segment buffer */
980 return sci
->sc_segbuf_nblocks
-
981 (sci
->sc_nblk_this_inc
+ sci
->sc_curseg
->sb_sum
.nblocks
);
984 static int nilfs_segctor_scan_file(struct nilfs_sc_info
*sci
,
986 struct nilfs_sc_operations
*sc_ops
)
988 LIST_HEAD(data_buffers
);
989 LIST_HEAD(node_buffers
);
992 if (!(sci
->sc_stage
.flags
& NILFS_CF_NODE
)) {
993 size_t n
, rest
= nilfs_segctor_buffer_rest(sci
);
995 n
= nilfs_lookup_dirty_data_buffers(
996 inode
, &data_buffers
, rest
+ 1, 0, LLONG_MAX
);
998 err
= nilfs_segctor_apply_buffers(
999 sci
, inode
, &data_buffers
,
1000 sc_ops
->collect_data
);
1001 BUG_ON(!err
); /* always receive -E2BIG or true error */
1005 nilfs_lookup_dirty_node_buffers(inode
, &node_buffers
);
1007 if (!(sci
->sc_stage
.flags
& NILFS_CF_NODE
)) {
1008 err
= nilfs_segctor_apply_buffers(
1009 sci
, inode
, &data_buffers
, sc_ops
->collect_data
);
1010 if (unlikely(err
)) {
1011 /* dispose node list */
1012 nilfs_segctor_apply_buffers(
1013 sci
, inode
, &node_buffers
, NULL
);
1016 sci
->sc_stage
.flags
|= NILFS_CF_NODE
;
1019 err
= nilfs_segctor_apply_buffers(
1020 sci
, inode
, &node_buffers
, sc_ops
->collect_node
);
1024 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode
)->i_bmap
, &node_buffers
);
1025 err
= nilfs_segctor_apply_buffers(
1026 sci
, inode
, &node_buffers
, sc_ops
->collect_bmap
);
1030 nilfs_segctor_end_finfo(sci
, inode
);
1031 sci
->sc_stage
.flags
&= ~NILFS_CF_NODE
;
1037 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info
*sci
,
1038 struct inode
*inode
)
1040 LIST_HEAD(data_buffers
);
1041 size_t n
, rest
= nilfs_segctor_buffer_rest(sci
);
1044 n
= nilfs_lookup_dirty_data_buffers(inode
, &data_buffers
, rest
+ 1,
1045 sci
->sc_dsync_start
,
1048 err
= nilfs_segctor_apply_buffers(sci
, inode
, &data_buffers
,
1049 nilfs_collect_file_data
);
1051 nilfs_segctor_end_finfo(sci
, inode
);
1053 /* always receive -E2BIG or true error if n > rest */
1058 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info
*sci
, int mode
)
1060 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
1061 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
1062 struct list_head
*head
;
1063 struct nilfs_inode_info
*ii
;
1067 switch (sci
->sc_stage
.scnt
) {
1070 sci
->sc_stage
.flags
= 0;
1072 if (!test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
)) {
1073 sci
->sc_nblk_inc
= 0;
1074 sci
->sc_curseg
->sb_sum
.flags
= NILFS_SS_LOGBGN
;
1075 if (mode
== SC_LSEG_DSYNC
) {
1076 sci
->sc_stage
.scnt
= NILFS_ST_DSYNC
;
1081 sci
->sc_stage
.dirty_file_ptr
= NULL
;
1082 sci
->sc_stage
.gc_inode_ptr
= NULL
;
1083 if (mode
== SC_FLUSH_DAT
) {
1084 sci
->sc_stage
.scnt
= NILFS_ST_DAT
;
1087 sci
->sc_stage
.scnt
++; /* Fall through */
1089 if (nilfs_doing_gc()) {
1090 head
= &sci
->sc_gc_inodes
;
1091 ii
= list_prepare_entry(sci
->sc_stage
.gc_inode_ptr
,
1093 list_for_each_entry_continue(ii
, head
, i_dirty
) {
1094 err
= nilfs_segctor_scan_file(
1095 sci
, &ii
->vfs_inode
,
1096 &nilfs_sc_file_ops
);
1097 if (unlikely(err
)) {
1098 sci
->sc_stage
.gc_inode_ptr
= list_entry(
1100 struct nilfs_inode_info
,
1104 set_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
1106 sci
->sc_stage
.gc_inode_ptr
= NULL
;
1108 sci
->sc_stage
.scnt
++; /* Fall through */
1110 head
= &sci
->sc_dirty_files
;
1111 ii
= list_prepare_entry(sci
->sc_stage
.dirty_file_ptr
, head
,
1113 list_for_each_entry_continue(ii
, head
, i_dirty
) {
1114 clear_bit(NILFS_I_DIRTY
, &ii
->i_state
);
1116 err
= nilfs_segctor_scan_file(sci
, &ii
->vfs_inode
,
1117 &nilfs_sc_file_ops
);
1118 if (unlikely(err
)) {
1119 sci
->sc_stage
.dirty_file_ptr
=
1120 list_entry(ii
->i_dirty
.prev
,
1121 struct nilfs_inode_info
,
1125 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1127 sci
->sc_stage
.dirty_file_ptr
= NULL
;
1128 if (mode
== SC_FLUSH_FILE
) {
1129 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1132 sci
->sc_stage
.scnt
++;
1133 sci
->sc_stage
.flags
|= NILFS_CF_IFILE_STARTED
;
1135 case NILFS_ST_IFILE
:
1136 err
= nilfs_segctor_scan_file(sci
, sbi
->s_ifile
,
1137 &nilfs_sc_file_ops
);
1140 sci
->sc_stage
.scnt
++;
1141 /* Creating a checkpoint */
1142 err
= nilfs_segctor_create_checkpoint(sci
);
1146 case NILFS_ST_CPFILE
:
1147 err
= nilfs_segctor_scan_file(sci
, nilfs
->ns_cpfile
,
1148 &nilfs_sc_file_ops
);
1151 sci
->sc_stage
.scnt
++; /* Fall through */
1152 case NILFS_ST_SUFILE
:
1153 err
= nilfs_sufile_freev(nilfs
->ns_sufile
, sci
->sc_freesegs
,
1154 sci
->sc_nfreesegs
, &ndone
);
1155 if (unlikely(err
)) {
1156 nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1157 sci
->sc_freesegs
, ndone
,
1161 sci
->sc_stage
.flags
|= NILFS_CF_SUFREED
;
1163 err
= nilfs_segctor_scan_file(sci
, nilfs
->ns_sufile
,
1164 &nilfs_sc_file_ops
);
1167 sci
->sc_stage
.scnt
++; /* Fall through */
1170 err
= nilfs_segctor_scan_file(sci
, nilfs_dat_inode(nilfs
),
1174 if (mode
== SC_FLUSH_DAT
) {
1175 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1178 sci
->sc_stage
.scnt
++; /* Fall through */
1180 if (mode
== SC_LSEG_SR
) {
1181 /* Appending a super root */
1182 err
= nilfs_segctor_add_super_root(sci
);
1186 /* End of a logical segment */
1187 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_LOGEND
;
1188 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1190 case NILFS_ST_DSYNC
:
1192 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_SYNDT
;
1193 ii
= sci
->sc_dsync_inode
;
1194 if (!test_bit(NILFS_I_BUSY
, &ii
->i_state
))
1197 err
= nilfs_segctor_scan_file_dsync(sci
, &ii
->vfs_inode
);
1200 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_LOGEND
;
1201 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1214 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1215 * @sci: nilfs_sc_info
1216 * @nilfs: nilfs object
1218 static int nilfs_segctor_begin_construction(struct nilfs_sc_info
*sci
,
1219 struct the_nilfs
*nilfs
)
1221 struct nilfs_segment_buffer
*segbuf
, *prev
;
1225 segbuf
= nilfs_segbuf_new(sci
->sc_super
);
1226 if (unlikely(!segbuf
))
1229 if (list_empty(&sci
->sc_write_logs
)) {
1230 nilfs_segbuf_map(segbuf
, nilfs
->ns_segnum
,
1231 nilfs
->ns_pseg_offset
, nilfs
);
1232 if (segbuf
->sb_rest_blocks
< NILFS_PSEG_MIN_BLOCKS
) {
1233 nilfs_shift_to_next_segment(nilfs
);
1234 nilfs_segbuf_map(segbuf
, nilfs
->ns_segnum
, 0, nilfs
);
1237 segbuf
->sb_sum
.seg_seq
= nilfs
->ns_seg_seq
;
1238 nextnum
= nilfs
->ns_nextnum
;
1240 if (nilfs
->ns_segnum
== nilfs
->ns_nextnum
)
1241 /* Start from the head of a new full segment */
1245 prev
= NILFS_LAST_SEGBUF(&sci
->sc_write_logs
);
1246 nilfs_segbuf_map_cont(segbuf
, prev
);
1247 segbuf
->sb_sum
.seg_seq
= prev
->sb_sum
.seg_seq
;
1248 nextnum
= prev
->sb_nextnum
;
1250 if (segbuf
->sb_rest_blocks
< NILFS_PSEG_MIN_BLOCKS
) {
1251 nilfs_segbuf_map(segbuf
, prev
->sb_nextnum
, 0, nilfs
);
1252 segbuf
->sb_sum
.seg_seq
++;
1257 err
= nilfs_sufile_mark_dirty(nilfs
->ns_sufile
, segbuf
->sb_segnum
);
1262 err
= nilfs_sufile_alloc(nilfs
->ns_sufile
, &nextnum
);
1266 nilfs_segbuf_set_next_segnum(segbuf
, nextnum
, nilfs
);
1268 BUG_ON(!list_empty(&sci
->sc_segbufs
));
1269 list_add_tail(&segbuf
->sb_list
, &sci
->sc_segbufs
);
1270 sci
->sc_segbuf_nblocks
= segbuf
->sb_rest_blocks
;
1274 nilfs_segbuf_free(segbuf
);
1278 static int nilfs_segctor_extend_segments(struct nilfs_sc_info
*sci
,
1279 struct the_nilfs
*nilfs
, int nadd
)
1281 struct nilfs_segment_buffer
*segbuf
, *prev
;
1282 struct inode
*sufile
= nilfs
->ns_sufile
;
1287 prev
= NILFS_LAST_SEGBUF(&sci
->sc_segbufs
);
1289 * Since the segment specified with nextnum might be allocated during
1290 * the previous construction, the buffer including its segusage may
1291 * not be dirty. The following call ensures that the buffer is dirty
1292 * and will pin the buffer on memory until the sufile is written.
1294 err
= nilfs_sufile_mark_dirty(sufile
, prev
->sb_nextnum
);
1298 for (i
= 0; i
< nadd
; i
++) {
1299 /* extend segment info */
1301 segbuf
= nilfs_segbuf_new(sci
->sc_super
);
1302 if (unlikely(!segbuf
))
1305 /* map this buffer to region of segment on-disk */
1306 nilfs_segbuf_map(segbuf
, prev
->sb_nextnum
, 0, nilfs
);
1307 sci
->sc_segbuf_nblocks
+= segbuf
->sb_rest_blocks
;
1309 /* allocate the next next full segment */
1310 err
= nilfs_sufile_alloc(sufile
, &nextnextnum
);
1314 segbuf
->sb_sum
.seg_seq
= prev
->sb_sum
.seg_seq
+ 1;
1315 nilfs_segbuf_set_next_segnum(segbuf
, nextnextnum
, nilfs
);
1317 list_add_tail(&segbuf
->sb_list
, &list
);
1320 list_splice_tail(&list
, &sci
->sc_segbufs
);
1324 nilfs_segbuf_free(segbuf
);
1326 list_for_each_entry(segbuf
, &list
, sb_list
) {
1327 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1328 WARN_ON(ret
); /* never fails */
1330 nilfs_destroy_logs(&list
);
1334 static void nilfs_free_incomplete_logs(struct list_head
*logs
,
1335 struct the_nilfs
*nilfs
)
1337 struct nilfs_segment_buffer
*segbuf
, *prev
;
1338 struct inode
*sufile
= nilfs
->ns_sufile
;
1341 segbuf
= NILFS_FIRST_SEGBUF(logs
);
1342 if (nilfs
->ns_nextnum
!= segbuf
->sb_nextnum
) {
1343 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1344 WARN_ON(ret
); /* never fails */
1346 if (atomic_read(&segbuf
->sb_err
)) {
1347 /* Case 1: The first segment failed */
1348 if (segbuf
->sb_pseg_start
!= segbuf
->sb_fseg_start
)
1349 /* Case 1a: Partial segment appended into an existing
1351 nilfs_terminate_segment(nilfs
, segbuf
->sb_fseg_start
,
1352 segbuf
->sb_fseg_end
);
1353 else /* Case 1b: New full segment */
1354 set_nilfs_discontinued(nilfs
);
1358 list_for_each_entry_continue(segbuf
, logs
, sb_list
) {
1359 if (prev
->sb_nextnum
!= segbuf
->sb_nextnum
) {
1360 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1361 WARN_ON(ret
); /* never fails */
1363 if (atomic_read(&segbuf
->sb_err
) &&
1364 segbuf
->sb_segnum
!= nilfs
->ns_nextnum
)
1365 /* Case 2: extended segment (!= next) failed */
1366 nilfs_sufile_set_error(sufile
, segbuf
->sb_segnum
);
1371 static void nilfs_segctor_update_segusage(struct nilfs_sc_info
*sci
,
1372 struct inode
*sufile
)
1374 struct nilfs_segment_buffer
*segbuf
;
1375 unsigned long live_blocks
;
1378 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1379 live_blocks
= segbuf
->sb_sum
.nblocks
+
1380 (segbuf
->sb_pseg_start
- segbuf
->sb_fseg_start
);
1381 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1384 WARN_ON(ret
); /* always succeed because the segusage is dirty */
1388 static void nilfs_cancel_segusage(struct list_head
*logs
, struct inode
*sufile
)
1390 struct nilfs_segment_buffer
*segbuf
;
1393 segbuf
= NILFS_FIRST_SEGBUF(logs
);
1394 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1395 segbuf
->sb_pseg_start
-
1396 segbuf
->sb_fseg_start
, 0);
1397 WARN_ON(ret
); /* always succeed because the segusage is dirty */
1399 list_for_each_entry_continue(segbuf
, logs
, sb_list
) {
1400 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1402 WARN_ON(ret
); /* always succeed */
1406 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info
*sci
,
1407 struct nilfs_segment_buffer
*last
,
1408 struct inode
*sufile
)
1410 struct nilfs_segment_buffer
*segbuf
= last
;
1413 list_for_each_entry_continue(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1414 sci
->sc_segbuf_nblocks
-= segbuf
->sb_rest_blocks
;
1415 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1418 nilfs_truncate_logs(&sci
->sc_segbufs
, last
);
1422 static int nilfs_segctor_collect(struct nilfs_sc_info
*sci
,
1423 struct the_nilfs
*nilfs
, int mode
)
1425 struct nilfs_cstage prev_stage
= sci
->sc_stage
;
1428 /* Collection retry loop */
1430 sci
->sc_nblk_this_inc
= 0;
1431 sci
->sc_curseg
= NILFS_FIRST_SEGBUF(&sci
->sc_segbufs
);
1433 err
= nilfs_segctor_reset_segment_buffer(sci
);
1437 err
= nilfs_segctor_collect_blocks(sci
, mode
);
1438 sci
->sc_nblk_this_inc
+= sci
->sc_curseg
->sb_sum
.nblocks
;
1442 if (unlikely(err
!= -E2BIG
))
1445 /* The current segment is filled up */
1446 if (mode
!= SC_LSEG_SR
|| sci
->sc_stage
.scnt
< NILFS_ST_CPFILE
)
1449 nilfs_clear_logs(&sci
->sc_segbufs
);
1451 err
= nilfs_segctor_extend_segments(sci
, nilfs
, nadd
);
1455 if (sci
->sc_stage
.flags
& NILFS_CF_SUFREED
) {
1456 err
= nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1460 WARN_ON(err
); /* do not happen */
1462 nadd
= min_t(int, nadd
<< 1, SC_MAX_SEGDELTA
);
1463 sci
->sc_stage
= prev_stage
;
1465 nilfs_segctor_truncate_segments(sci
, sci
->sc_curseg
, nilfs
->ns_sufile
);
1472 static void nilfs_list_replace_buffer(struct buffer_head
*old_bh
,
1473 struct buffer_head
*new_bh
)
1475 BUG_ON(!list_empty(&new_bh
->b_assoc_buffers
));
1477 list_replace_init(&old_bh
->b_assoc_buffers
, &new_bh
->b_assoc_buffers
);
1478 /* The caller must release old_bh */
1482 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info
*sci
,
1483 struct nilfs_segment_buffer
*segbuf
,
1486 struct inode
*inode
= NULL
;
1488 unsigned long nfinfo
= segbuf
->sb_sum
.nfinfo
;
1489 unsigned long nblocks
= 0, ndatablk
= 0;
1490 struct nilfs_sc_operations
*sc_op
= NULL
;
1491 struct nilfs_segsum_pointer ssp
;
1492 struct nilfs_finfo
*finfo
= NULL
;
1493 union nilfs_binfo binfo
;
1494 struct buffer_head
*bh
, *bh_org
;
1501 blocknr
= segbuf
->sb_pseg_start
+ segbuf
->sb_sum
.nsumblk
;
1502 ssp
.bh
= NILFS_SEGBUF_FIRST_BH(&segbuf
->sb_segsum_buffers
);
1503 ssp
.offset
= sizeof(struct nilfs_segment_summary
);
1505 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
, b_assoc_buffers
) {
1506 if (bh
== segbuf
->sb_super_root
)
1509 finfo
= nilfs_segctor_map_segsum_entry(
1510 sci
, &ssp
, sizeof(*finfo
));
1511 ino
= le64_to_cpu(finfo
->fi_ino
);
1512 nblocks
= le32_to_cpu(finfo
->fi_nblocks
);
1513 ndatablk
= le32_to_cpu(finfo
->fi_ndatablk
);
1515 if (buffer_nilfs_node(bh
))
1516 inode
= NILFS_BTNC_I(bh
->b_page
->mapping
);
1518 inode
= NILFS_AS_I(bh
->b_page
->mapping
);
1520 if (mode
== SC_LSEG_DSYNC
)
1521 sc_op
= &nilfs_sc_dsync_ops
;
1522 else if (ino
== NILFS_DAT_INO
)
1523 sc_op
= &nilfs_sc_dat_ops
;
1524 else /* file blocks */
1525 sc_op
= &nilfs_sc_file_ops
;
1529 err
= nilfs_bmap_assign(NILFS_I(inode
)->i_bmap
, &bh
, blocknr
,
1532 nilfs_list_replace_buffer(bh_org
, bh
);
1538 sc_op
->write_data_binfo(sci
, &ssp
, &binfo
);
1540 sc_op
->write_node_binfo(sci
, &ssp
, &binfo
);
1543 if (--nblocks
== 0) {
1547 } else if (ndatablk
> 0)
1554 err
= nilfs_handle_bmap_error(err
, __func__
, inode
, sci
->sc_super
);
1558 static int nilfs_segctor_assign(struct nilfs_sc_info
*sci
, int mode
)
1560 struct nilfs_segment_buffer
*segbuf
;
1563 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1564 err
= nilfs_segctor_update_payload_blocknr(sci
, segbuf
, mode
);
1567 nilfs_segbuf_fill_in_segsum(segbuf
);
1573 nilfs_copy_replace_page_buffers(struct page
*page
, struct list_head
*out
)
1575 struct page
*clone_page
;
1576 struct buffer_head
*bh
, *head
, *bh2
;
1579 bh
= head
= page_buffers(page
);
1581 clone_page
= nilfs_alloc_private_page(bh
->b_bdev
, bh
->b_size
, 0);
1582 if (unlikely(!clone_page
))
1585 bh2
= page_buffers(clone_page
);
1586 kaddr
= kmap_atomic(page
, KM_USER0
);
1588 if (list_empty(&bh
->b_assoc_buffers
))
1591 page_cache_get(clone_page
); /* for each bh */
1592 memcpy(bh2
->b_data
, kaddr
+ bh_offset(bh
), bh2
->b_size
);
1593 bh2
->b_blocknr
= bh
->b_blocknr
;
1594 list_replace(&bh
->b_assoc_buffers
, &bh2
->b_assoc_buffers
);
1595 list_add_tail(&bh
->b_assoc_buffers
, out
);
1596 } while (bh
= bh
->b_this_page
, bh2
= bh2
->b_this_page
, bh
!= head
);
1597 kunmap_atomic(kaddr
, KM_USER0
);
1599 if (!TestSetPageWriteback(clone_page
))
1600 inc_zone_page_state(clone_page
, NR_WRITEBACK
);
1601 unlock_page(clone_page
);
1606 static int nilfs_test_page_to_be_frozen(struct page
*page
)
1608 struct address_space
*mapping
= page
->mapping
;
1610 if (!mapping
|| !mapping
->host
|| S_ISDIR(mapping
->host
->i_mode
))
1613 if (page_mapped(page
)) {
1614 ClearPageChecked(page
);
1617 return PageChecked(page
);
1620 static int nilfs_begin_page_io(struct page
*page
, struct list_head
*out
)
1622 if (!page
|| PageWriteback(page
))
1623 /* For split b-tree node pages, this function may be called
1624 twice. We ignore the 2nd or later calls by this check. */
1628 clear_page_dirty_for_io(page
);
1629 set_page_writeback(page
);
1632 if (nilfs_test_page_to_be_frozen(page
)) {
1633 int err
= nilfs_copy_replace_page_buffers(page
, out
);
1640 static int nilfs_segctor_prepare_write(struct nilfs_sc_info
*sci
,
1641 struct page
**failed_page
)
1643 struct nilfs_segment_buffer
*segbuf
;
1644 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1645 struct list_head
*list
= &sci
->sc_copied_buffers
;
1648 *failed_page
= NULL
;
1649 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1650 struct buffer_head
*bh
;
1652 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1654 if (bh
->b_page
!= bd_page
) {
1657 clear_page_dirty_for_io(bd_page
);
1658 set_page_writeback(bd_page
);
1659 unlock_page(bd_page
);
1661 bd_page
= bh
->b_page
;
1665 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1667 if (bh
== segbuf
->sb_super_root
) {
1668 if (bh
->b_page
!= bd_page
) {
1670 clear_page_dirty_for_io(bd_page
);
1671 set_page_writeback(bd_page
);
1672 unlock_page(bd_page
);
1673 bd_page
= bh
->b_page
;
1677 if (bh
->b_page
!= fs_page
) {
1678 err
= nilfs_begin_page_io(fs_page
, list
);
1679 if (unlikely(err
)) {
1680 *failed_page
= fs_page
;
1683 fs_page
= bh
->b_page
;
1689 clear_page_dirty_for_io(bd_page
);
1690 set_page_writeback(bd_page
);
1691 unlock_page(bd_page
);
1693 err
= nilfs_begin_page_io(fs_page
, list
);
1695 *failed_page
= fs_page
;
1700 static int nilfs_segctor_write(struct nilfs_sc_info
*sci
,
1701 struct the_nilfs
*nilfs
)
1705 ret
= nilfs_write_logs(&sci
->sc_segbufs
, nilfs
);
1706 list_splice_tail_init(&sci
->sc_segbufs
, &sci
->sc_write_logs
);
1710 static void __nilfs_end_page_io(struct page
*page
, int err
)
1713 if (!nilfs_page_buffers_clean(page
))
1714 __set_page_dirty_nobuffers(page
);
1715 ClearPageError(page
);
1717 __set_page_dirty_nobuffers(page
);
1721 if (buffer_nilfs_allocated(page_buffers(page
))) {
1722 if (TestClearPageWriteback(page
))
1723 dec_zone_page_state(page
, NR_WRITEBACK
);
1725 end_page_writeback(page
);
1728 static void nilfs_end_page_io(struct page
*page
, int err
)
1733 if (buffer_nilfs_node(page_buffers(page
)) && !PageWriteback(page
)) {
1735 * For b-tree node pages, this function may be called twice
1736 * or more because they might be split in a segment.
1738 if (PageDirty(page
)) {
1740 * For pages holding split b-tree node buffers, dirty
1741 * flag on the buffers may be cleared discretely.
1742 * In that case, the page is once redirtied for
1743 * remaining buffers, and it must be cancelled if
1744 * all the buffers get cleaned later.
1747 if (nilfs_page_buffers_clean(page
))
1748 __nilfs_clear_page_dirty(page
);
1754 __nilfs_end_page_io(page
, err
);
1757 static void nilfs_clear_copied_buffers(struct list_head
*list
, int err
)
1759 struct buffer_head
*bh
, *head
;
1762 while (!list_empty(list
)) {
1763 bh
= list_entry(list
->next
, struct buffer_head
,
1766 page_cache_get(page
);
1767 head
= bh
= page_buffers(page
);
1769 if (!list_empty(&bh
->b_assoc_buffers
)) {
1770 list_del_init(&bh
->b_assoc_buffers
);
1772 set_buffer_uptodate(bh
);
1773 clear_buffer_dirty(bh
);
1774 clear_buffer_nilfs_volatile(bh
);
1776 brelse(bh
); /* for b_assoc_buffers */
1778 } while ((bh
= bh
->b_this_page
) != head
);
1780 __nilfs_end_page_io(page
, err
);
1781 page_cache_release(page
);
1785 static void nilfs_abort_logs(struct list_head
*logs
, struct page
*failed_page
,
1788 struct nilfs_segment_buffer
*segbuf
;
1789 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1790 struct buffer_head
*bh
;
1792 if (list_empty(logs
))
1795 list_for_each_entry(segbuf
, logs
, sb_list
) {
1796 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1798 if (bh
->b_page
!= bd_page
) {
1800 end_page_writeback(bd_page
);
1801 bd_page
= bh
->b_page
;
1805 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1807 if (bh
== segbuf
->sb_super_root
) {
1808 if (bh
->b_page
!= bd_page
) {
1809 end_page_writeback(bd_page
);
1810 bd_page
= bh
->b_page
;
1814 if (bh
->b_page
!= fs_page
) {
1815 nilfs_end_page_io(fs_page
, err
);
1816 if (fs_page
&& fs_page
== failed_page
)
1818 fs_page
= bh
->b_page
;
1823 end_page_writeback(bd_page
);
1825 nilfs_end_page_io(fs_page
, err
);
1828 static void nilfs_segctor_abort_construction(struct nilfs_sc_info
*sci
,
1829 struct the_nilfs
*nilfs
, int err
)
1834 list_splice_tail_init(&sci
->sc_write_logs
, &logs
);
1835 ret
= nilfs_wait_on_logs(&logs
);
1836 nilfs_abort_logs(&logs
, NULL
, ret
? : err
);
1838 list_splice_tail_init(&sci
->sc_segbufs
, &logs
);
1839 nilfs_cancel_segusage(&logs
, nilfs
->ns_sufile
);
1840 nilfs_free_incomplete_logs(&logs
, nilfs
);
1841 nilfs_clear_copied_buffers(&sci
->sc_copied_buffers
, err
);
1843 if (sci
->sc_stage
.flags
& NILFS_CF_SUFREED
) {
1844 ret
= nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1848 WARN_ON(ret
); /* do not happen */
1851 nilfs_destroy_logs(&logs
);
1854 static void nilfs_set_next_segment(struct the_nilfs
*nilfs
,
1855 struct nilfs_segment_buffer
*segbuf
)
1857 nilfs
->ns_segnum
= segbuf
->sb_segnum
;
1858 nilfs
->ns_nextnum
= segbuf
->sb_nextnum
;
1859 nilfs
->ns_pseg_offset
= segbuf
->sb_pseg_start
- segbuf
->sb_fseg_start
1860 + segbuf
->sb_sum
.nblocks
;
1861 nilfs
->ns_seg_seq
= segbuf
->sb_sum
.seg_seq
;
1862 nilfs
->ns_ctime
= segbuf
->sb_sum
.ctime
;
1865 static void nilfs_segctor_complete_write(struct nilfs_sc_info
*sci
)
1867 struct nilfs_segment_buffer
*segbuf
;
1868 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1869 struct the_nilfs
*nilfs
= sci
->sc_sbi
->s_nilfs
;
1870 int update_sr
= false;
1872 list_for_each_entry(segbuf
, &sci
->sc_write_logs
, sb_list
) {
1873 struct buffer_head
*bh
;
1875 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1877 set_buffer_uptodate(bh
);
1878 clear_buffer_dirty(bh
);
1879 if (bh
->b_page
!= bd_page
) {
1881 end_page_writeback(bd_page
);
1882 bd_page
= bh
->b_page
;
1886 * We assume that the buffers which belong to the same page
1887 * continue over the buffer list.
1888 * Under this assumption, the last BHs of pages is
1889 * identifiable by the discontinuity of bh->b_page
1890 * (page != fs_page).
1892 * For B-tree node blocks, however, this assumption is not
1893 * guaranteed. The cleanup code of B-tree node pages needs
1896 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1898 set_buffer_uptodate(bh
);
1899 clear_buffer_dirty(bh
);
1900 clear_buffer_nilfs_volatile(bh
);
1901 if (bh
== segbuf
->sb_super_root
) {
1902 if (bh
->b_page
!= bd_page
) {
1903 end_page_writeback(bd_page
);
1904 bd_page
= bh
->b_page
;
1909 if (bh
->b_page
!= fs_page
) {
1910 nilfs_end_page_io(fs_page
, 0);
1911 fs_page
= bh
->b_page
;
1915 if (!nilfs_segbuf_simplex(segbuf
)) {
1916 if (segbuf
->sb_sum
.flags
& NILFS_SS_LOGBGN
) {
1917 set_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
);
1918 sci
->sc_lseg_stime
= jiffies
;
1920 if (segbuf
->sb_sum
.flags
& NILFS_SS_LOGEND
)
1921 clear_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
);
1925 * Since pages may continue over multiple segment buffers,
1926 * end of the last page must be checked outside of the loop.
1929 end_page_writeback(bd_page
);
1931 nilfs_end_page_io(fs_page
, 0);
1933 nilfs_clear_copied_buffers(&sci
->sc_copied_buffers
, 0);
1935 nilfs_drop_collected_inodes(&sci
->sc_dirty_files
);
1937 if (nilfs_doing_gc()) {
1938 nilfs_drop_collected_inodes(&sci
->sc_gc_inodes
);
1940 nilfs_commit_gcdat_inode(nilfs
);
1942 nilfs
->ns_nongc_ctime
= sci
->sc_seg_ctime
;
1944 sci
->sc_nblk_inc
+= sci
->sc_nblk_this_inc
;
1946 segbuf
= NILFS_LAST_SEGBUF(&sci
->sc_write_logs
);
1947 nilfs_set_next_segment(nilfs
, segbuf
);
1950 nilfs_set_last_segment(nilfs
, segbuf
->sb_pseg_start
,
1951 segbuf
->sb_sum
.seg_seq
, nilfs
->ns_cno
++);
1953 clear_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
);
1954 clear_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
1955 set_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
);
1956 nilfs_segctor_clear_metadata_dirty(sci
);
1958 clear_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
);
1961 static int nilfs_segctor_wait(struct nilfs_sc_info
*sci
)
1965 ret
= nilfs_wait_on_logs(&sci
->sc_write_logs
);
1967 nilfs_segctor_complete_write(sci
);
1968 nilfs_destroy_logs(&sci
->sc_write_logs
);
1973 static int nilfs_segctor_check_in_files(struct nilfs_sc_info
*sci
,
1974 struct nilfs_sb_info
*sbi
)
1976 struct nilfs_inode_info
*ii
, *n
;
1977 __u64 cno
= sbi
->s_nilfs
->ns_cno
;
1979 spin_lock(&sbi
->s_inode_lock
);
1981 list_for_each_entry_safe(ii
, n
, &sbi
->s_dirty_files
, i_dirty
) {
1983 struct buffer_head
*ibh
;
1986 spin_unlock(&sbi
->s_inode_lock
);
1987 err
= nilfs_ifile_get_inode_block(
1988 sbi
->s_ifile
, ii
->vfs_inode
.i_ino
, &ibh
);
1989 if (unlikely(err
)) {
1990 nilfs_warning(sbi
->s_super
, __func__
,
1991 "failed to get inode block.\n");
1994 nilfs_mdt_mark_buffer_dirty(ibh
);
1995 nilfs_mdt_mark_dirty(sbi
->s_ifile
);
1996 spin_lock(&sbi
->s_inode_lock
);
1997 if (likely(!ii
->i_bh
))
2005 clear_bit(NILFS_I_QUEUED
, &ii
->i_state
);
2006 set_bit(NILFS_I_BUSY
, &ii
->i_state
);
2007 list_del(&ii
->i_dirty
);
2008 list_add_tail(&ii
->i_dirty
, &sci
->sc_dirty_files
);
2010 spin_unlock(&sbi
->s_inode_lock
);
2012 NILFS_I(sbi
->s_ifile
)->i_cno
= cno
;
2017 static void nilfs_segctor_check_out_files(struct nilfs_sc_info
*sci
,
2018 struct nilfs_sb_info
*sbi
)
2020 struct nilfs_transaction_info
*ti
= current
->journal_info
;
2021 struct nilfs_inode_info
*ii
, *n
;
2022 __u64 cno
= sbi
->s_nilfs
->ns_cno
;
2024 spin_lock(&sbi
->s_inode_lock
);
2025 list_for_each_entry_safe(ii
, n
, &sci
->sc_dirty_files
, i_dirty
) {
2026 if (!test_and_clear_bit(NILFS_I_UPDATED
, &ii
->i_state
) ||
2027 test_bit(NILFS_I_DIRTY
, &ii
->i_state
)) {
2028 /* The current checkpoint number (=nilfs->ns_cno) is
2029 changed between check-in and check-out only if the
2030 super root is written out. So, we can update i_cno
2031 for the inodes that remain in the dirty list. */
2035 clear_bit(NILFS_I_BUSY
, &ii
->i_state
);
2038 list_del(&ii
->i_dirty
);
2039 list_add_tail(&ii
->i_dirty
, &ti
->ti_garbage
);
2041 spin_unlock(&sbi
->s_inode_lock
);
2045 * Main procedure of segment constructor
2047 static int nilfs_segctor_do_construct(struct nilfs_sc_info
*sci
, int mode
)
2049 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2050 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2051 struct page
*failed_page
;
2054 sci
->sc_stage
.scnt
= NILFS_ST_INIT
;
2056 err
= nilfs_segctor_check_in_files(sci
, sbi
);
2060 if (nilfs_test_metadata_dirty(sbi
))
2061 set_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
2063 if (nilfs_segctor_clean(sci
))
2067 sci
->sc_stage
.flags
&= ~NILFS_CF_HISTORY_MASK
;
2069 err
= nilfs_segctor_begin_construction(sci
, nilfs
);
2073 /* Update time stamp */
2074 sci
->sc_seg_ctime
= get_seconds();
2076 err
= nilfs_segctor_collect(sci
, nilfs
, mode
);
2080 /* Avoid empty segment */
2081 if (sci
->sc_stage
.scnt
== NILFS_ST_DONE
&&
2082 nilfs_segbuf_empty(sci
->sc_curseg
)) {
2083 nilfs_segctor_abort_construction(sci
, nilfs
, 1);
2087 err
= nilfs_segctor_assign(sci
, mode
);
2091 if (sci
->sc_stage
.flags
& NILFS_CF_IFILE_STARTED
)
2092 nilfs_segctor_fill_in_file_bmap(sci
, sbi
->s_ifile
);
2094 if (mode
== SC_LSEG_SR
&&
2095 sci
->sc_stage
.scnt
>= NILFS_ST_CPFILE
) {
2096 err
= nilfs_segctor_fill_in_checkpoint(sci
);
2098 goto failed_to_write
;
2100 nilfs_segctor_fill_in_super_root(sci
, nilfs
);
2102 nilfs_segctor_update_segusage(sci
, nilfs
->ns_sufile
);
2104 /* Write partial segments */
2105 err
= nilfs_segctor_prepare_write(sci
, &failed_page
);
2107 nilfs_abort_logs(&sci
->sc_segbufs
, failed_page
, err
);
2108 goto failed_to_write
;
2111 nilfs_add_checksums_on_logs(&sci
->sc_segbufs
,
2112 nilfs
->ns_crc_seed
);
2114 err
= nilfs_segctor_write(sci
, nilfs
);
2116 goto failed_to_write
;
2118 if (sci
->sc_stage
.scnt
== NILFS_ST_DONE
||
2119 nilfs
->ns_blocksize_bits
!= PAGE_CACHE_SHIFT
) {
2121 * At this point, we avoid double buffering
2122 * for blocksize < pagesize because page dirty
2123 * flag is turned off during write and dirty
2124 * buffers are not properly collected for
2125 * pages crossing over segments.
2127 err
= nilfs_segctor_wait(sci
);
2129 goto failed_to_write
;
2131 } while (sci
->sc_stage
.scnt
!= NILFS_ST_DONE
);
2134 nilfs_segctor_check_out_files(sci
, sbi
);
2138 if (sci
->sc_stage
.flags
& NILFS_CF_IFILE_STARTED
)
2139 nilfs_redirty_inodes(&sci
->sc_dirty_files
);
2142 if (nilfs_doing_gc())
2143 nilfs_redirty_inodes(&sci
->sc_gc_inodes
);
2144 nilfs_segctor_abort_construction(sci
, nilfs
, err
);
2149 * nilfs_segctor_start_timer - set timer of background write
2150 * @sci: nilfs_sc_info
2152 * If the timer has already been set, it ignores the new request.
2153 * This function MUST be called within a section locking the segment
2156 static void nilfs_segctor_start_timer(struct nilfs_sc_info
*sci
)
2158 spin_lock(&sci
->sc_state_lock
);
2159 if (!(sci
->sc_state
& NILFS_SEGCTOR_COMMIT
)) {
2160 sci
->sc_timer
.expires
= jiffies
+ sci
->sc_interval
;
2161 add_timer(&sci
->sc_timer
);
2162 sci
->sc_state
|= NILFS_SEGCTOR_COMMIT
;
2164 spin_unlock(&sci
->sc_state_lock
);
2167 static void nilfs_segctor_do_flush(struct nilfs_sc_info
*sci
, int bn
)
2169 spin_lock(&sci
->sc_state_lock
);
2170 if (!(sci
->sc_flush_request
& (1 << bn
))) {
2171 unsigned long prev_req
= sci
->sc_flush_request
;
2173 sci
->sc_flush_request
|= (1 << bn
);
2175 wake_up(&sci
->sc_wait_daemon
);
2177 spin_unlock(&sci
->sc_state_lock
);
2181 * nilfs_flush_segment - trigger a segment construction for resource control
2183 * @ino: inode number of the file to be flushed out.
2185 void nilfs_flush_segment(struct super_block
*sb
, ino_t ino
)
2187 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2188 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2190 if (!sci
|| nilfs_doing_construction())
2192 nilfs_segctor_do_flush(sci
, NILFS_MDT_INODE(sb
, ino
) ? ino
: 0);
2193 /* assign bit 0 to data files */
2196 struct nilfs_segctor_wait_request
{
2203 static int nilfs_segctor_sync(struct nilfs_sc_info
*sci
)
2205 struct nilfs_segctor_wait_request wait_req
;
2208 spin_lock(&sci
->sc_state_lock
);
2209 init_wait(&wait_req
.wq
);
2211 atomic_set(&wait_req
.done
, 0);
2212 wait_req
.seq
= ++sci
->sc_seq_request
;
2213 spin_unlock(&sci
->sc_state_lock
);
2215 init_waitqueue_entry(&wait_req
.wq
, current
);
2216 add_wait_queue(&sci
->sc_wait_request
, &wait_req
.wq
);
2217 set_current_state(TASK_INTERRUPTIBLE
);
2218 wake_up(&sci
->sc_wait_daemon
);
2221 if (atomic_read(&wait_req
.done
)) {
2225 if (!signal_pending(current
)) {
2232 finish_wait(&sci
->sc_wait_request
, &wait_req
.wq
);
2236 static void nilfs_segctor_wakeup(struct nilfs_sc_info
*sci
, int err
)
2238 struct nilfs_segctor_wait_request
*wrq
, *n
;
2239 unsigned long flags
;
2241 spin_lock_irqsave(&sci
->sc_wait_request
.lock
, flags
);
2242 list_for_each_entry_safe(wrq
, n
, &sci
->sc_wait_request
.task_list
,
2244 if (!atomic_read(&wrq
->done
) &&
2245 nilfs_cnt32_ge(sci
->sc_seq_done
, wrq
->seq
)) {
2247 atomic_set(&wrq
->done
, 1);
2249 if (atomic_read(&wrq
->done
)) {
2250 wrq
->wq
.func(&wrq
->wq
,
2251 TASK_UNINTERRUPTIBLE
| TASK_INTERRUPTIBLE
,
2255 spin_unlock_irqrestore(&sci
->sc_wait_request
.lock
, flags
);
2259 * nilfs_construct_segment - construct a logical segment
2262 * Return Value: On success, 0 is retured. On errors, one of the following
2263 * negative error code is returned.
2265 * %-EROFS - Read only filesystem.
2269 * %-ENOSPC - No space left on device (only in a panic state).
2271 * %-ERESTARTSYS - Interrupted.
2273 * %-ENOMEM - Insufficient memory available.
2275 int nilfs_construct_segment(struct super_block
*sb
)
2277 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2278 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2279 struct nilfs_transaction_info
*ti
;
2285 /* A call inside transactions causes a deadlock. */
2286 BUG_ON((ti
= current
->journal_info
) && ti
->ti_magic
== NILFS_TI_MAGIC
);
2288 err
= nilfs_segctor_sync(sci
);
2293 * nilfs_construct_dsync_segment - construct a data-only logical segment
2295 * @inode: inode whose data blocks should be written out
2296 * @start: start byte offset
2297 * @end: end byte offset (inclusive)
2299 * Return Value: On success, 0 is retured. On errors, one of the following
2300 * negative error code is returned.
2302 * %-EROFS - Read only filesystem.
2306 * %-ENOSPC - No space left on device (only in a panic state).
2308 * %-ERESTARTSYS - Interrupted.
2310 * %-ENOMEM - Insufficient memory available.
2312 int nilfs_construct_dsync_segment(struct super_block
*sb
, struct inode
*inode
,
2313 loff_t start
, loff_t end
)
2315 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2316 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2317 struct nilfs_inode_info
*ii
;
2318 struct nilfs_transaction_info ti
;
2324 nilfs_transaction_lock(sbi
, &ti
, 0);
2326 ii
= NILFS_I(inode
);
2327 if (test_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
) ||
2328 nilfs_test_opt(sbi
, STRICT_ORDER
) ||
2329 test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
) ||
2330 nilfs_discontinued(sbi
->s_nilfs
)) {
2331 nilfs_transaction_unlock(sbi
);
2332 err
= nilfs_segctor_sync(sci
);
2336 spin_lock(&sbi
->s_inode_lock
);
2337 if (!test_bit(NILFS_I_QUEUED
, &ii
->i_state
) &&
2338 !test_bit(NILFS_I_BUSY
, &ii
->i_state
)) {
2339 spin_unlock(&sbi
->s_inode_lock
);
2340 nilfs_transaction_unlock(sbi
);
2343 spin_unlock(&sbi
->s_inode_lock
);
2344 sci
->sc_dsync_inode
= ii
;
2345 sci
->sc_dsync_start
= start
;
2346 sci
->sc_dsync_end
= end
;
2348 err
= nilfs_segctor_do_construct(sci
, SC_LSEG_DSYNC
);
2350 nilfs_transaction_unlock(sbi
);
2354 #define FLUSH_FILE_BIT (0x1) /* data file only */
2355 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2358 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2359 * @sci: segment constructor object
2361 static void nilfs_segctor_accept(struct nilfs_sc_info
*sci
)
2363 spin_lock(&sci
->sc_state_lock
);
2364 sci
->sc_seq_accepted
= sci
->sc_seq_request
;
2365 spin_unlock(&sci
->sc_state_lock
);
2366 del_timer_sync(&sci
->sc_timer
);
2370 * nilfs_segctor_notify - notify the result of request to caller threads
2371 * @sci: segment constructor object
2372 * @mode: mode of log forming
2373 * @err: error code to be notified
2375 static void nilfs_segctor_notify(struct nilfs_sc_info
*sci
, int mode
, int err
)
2377 /* Clear requests (even when the construction failed) */
2378 spin_lock(&sci
->sc_state_lock
);
2380 if (mode
== SC_LSEG_SR
) {
2381 sci
->sc_state
&= ~NILFS_SEGCTOR_COMMIT
;
2382 sci
->sc_seq_done
= sci
->sc_seq_accepted
;
2383 nilfs_segctor_wakeup(sci
, err
);
2384 sci
->sc_flush_request
= 0;
2386 if (mode
== SC_FLUSH_FILE
)
2387 sci
->sc_flush_request
&= ~FLUSH_FILE_BIT
;
2388 else if (mode
== SC_FLUSH_DAT
)
2389 sci
->sc_flush_request
&= ~FLUSH_DAT_BIT
;
2391 /* re-enable timer if checkpoint creation was not done */
2392 if ((sci
->sc_state
& NILFS_SEGCTOR_COMMIT
) &&
2393 time_before(jiffies
, sci
->sc_timer
.expires
))
2394 add_timer(&sci
->sc_timer
);
2396 spin_unlock(&sci
->sc_state_lock
);
2400 * nilfs_segctor_construct - form logs and write them to disk
2401 * @sci: segment constructor object
2402 * @mode: mode of log forming
2404 static int nilfs_segctor_construct(struct nilfs_sc_info
*sci
, int mode
)
2406 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2407 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2408 struct nilfs_super_block
**sbp
;
2411 nilfs_segctor_accept(sci
);
2413 if (nilfs_discontinued(nilfs
))
2415 if (!nilfs_segctor_confirm(sci
))
2416 err
= nilfs_segctor_do_construct(sci
, mode
);
2419 if (mode
!= SC_FLUSH_DAT
)
2420 atomic_set(&nilfs
->ns_ndirtyblks
, 0);
2421 if (test_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
) &&
2422 nilfs_discontinued(nilfs
)) {
2423 down_write(&nilfs
->ns_sem
);
2425 sbp
= nilfs_prepare_super(sbi
,
2426 nilfs_sb_will_flip(nilfs
));
2428 nilfs_set_log_cursor(sbp
[0], nilfs
);
2429 err
= nilfs_commit_super(sbi
, NILFS_SB_COMMIT
);
2431 up_write(&nilfs
->ns_sem
);
2435 nilfs_segctor_notify(sci
, mode
, err
);
2439 static void nilfs_construction_timeout(unsigned long data
)
2441 struct task_struct
*p
= (struct task_struct
*)data
;
2446 nilfs_remove_written_gcinodes(struct the_nilfs
*nilfs
, struct list_head
*head
)
2448 struct nilfs_inode_info
*ii
, *n
;
2450 list_for_each_entry_safe(ii
, n
, head
, i_dirty
) {
2451 if (!test_bit(NILFS_I_UPDATED
, &ii
->i_state
))
2453 hlist_del_init(&ii
->vfs_inode
.i_hash
);
2454 list_del_init(&ii
->i_dirty
);
2455 nilfs_clear_gcinode(&ii
->vfs_inode
);
2459 int nilfs_clean_segments(struct super_block
*sb
, struct nilfs_argv
*argv
,
2462 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2463 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2464 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2465 struct nilfs_transaction_info ti
;
2471 nilfs_transaction_lock(sbi
, &ti
, 1);
2473 err
= nilfs_init_gcdat_inode(nilfs
);
2477 err
= nilfs_ioctl_prepare_clean_segments(nilfs
, argv
, kbufs
);
2481 sci
->sc_freesegs
= kbufs
[4];
2482 sci
->sc_nfreesegs
= argv
[4].v_nmembs
;
2483 list_splice_tail_init(&nilfs
->ns_gc_inodes
, &sci
->sc_gc_inodes
);
2486 err
= nilfs_segctor_construct(sci
, SC_LSEG_SR
);
2487 nilfs_remove_written_gcinodes(nilfs
, &sci
->sc_gc_inodes
);
2492 nilfs_warning(sb
, __func__
,
2493 "segment construction failed. (err=%d)", err
);
2494 set_current_state(TASK_INTERRUPTIBLE
);
2495 schedule_timeout(sci
->sc_interval
);
2497 if (nilfs_test_opt(sbi
, DISCARD
)) {
2498 int ret
= nilfs_discard_segments(nilfs
, sci
->sc_freesegs
,
2502 "NILFS warning: error %d on discard request, "
2503 "turning discards off for the device\n", ret
);
2504 nilfs_clear_opt(sbi
, DISCARD
);
2509 sci
->sc_freesegs
= NULL
;
2510 sci
->sc_nfreesegs
= 0;
2511 nilfs_clear_gcdat_inode(nilfs
);
2512 nilfs_transaction_unlock(sbi
);
2516 static void nilfs_segctor_thread_construct(struct nilfs_sc_info
*sci
, int mode
)
2518 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2519 struct nilfs_transaction_info ti
;
2521 nilfs_transaction_lock(sbi
, &ti
, 0);
2522 nilfs_segctor_construct(sci
, mode
);
2525 * Unclosed segment should be retried. We do this using sc_timer.
2526 * Timeout of sc_timer will invoke complete construction which leads
2527 * to close the current logical segment.
2529 if (test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
))
2530 nilfs_segctor_start_timer(sci
);
2532 nilfs_transaction_unlock(sbi
);
2535 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info
*sci
)
2540 spin_lock(&sci
->sc_state_lock
);
2541 mode
= (sci
->sc_flush_request
& FLUSH_DAT_BIT
) ?
2542 SC_FLUSH_DAT
: SC_FLUSH_FILE
;
2543 spin_unlock(&sci
->sc_state_lock
);
2546 err
= nilfs_segctor_do_construct(sci
, mode
);
2548 spin_lock(&sci
->sc_state_lock
);
2549 sci
->sc_flush_request
&= (mode
== SC_FLUSH_FILE
) ?
2550 ~FLUSH_FILE_BIT
: ~FLUSH_DAT_BIT
;
2551 spin_unlock(&sci
->sc_state_lock
);
2553 clear_bit(NILFS_SC_PRIOR_FLUSH
, &sci
->sc_flags
);
2556 static int nilfs_segctor_flush_mode(struct nilfs_sc_info
*sci
)
2558 if (!test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
) ||
2559 time_before(jiffies
, sci
->sc_lseg_stime
+ sci
->sc_mjcp_freq
)) {
2560 if (!(sci
->sc_flush_request
& ~FLUSH_FILE_BIT
))
2561 return SC_FLUSH_FILE
;
2562 else if (!(sci
->sc_flush_request
& ~FLUSH_DAT_BIT
))
2563 return SC_FLUSH_DAT
;
2569 * nilfs_segctor_thread - main loop of the segment constructor thread.
2570 * @arg: pointer to a struct nilfs_sc_info.
2572 * nilfs_segctor_thread() initializes a timer and serves as a daemon
2573 * to execute segment constructions.
2575 static int nilfs_segctor_thread(void *arg
)
2577 struct nilfs_sc_info
*sci
= (struct nilfs_sc_info
*)arg
;
2578 struct the_nilfs
*nilfs
= sci
->sc_sbi
->s_nilfs
;
2581 sci
->sc_timer
.data
= (unsigned long)current
;
2582 sci
->sc_timer
.function
= nilfs_construction_timeout
;
2585 sci
->sc_task
= current
;
2586 wake_up(&sci
->sc_wait_task
); /* for nilfs_segctor_start_thread() */
2588 "segctord starting. Construction interval = %lu seconds, "
2589 "CP frequency < %lu seconds\n",
2590 sci
->sc_interval
/ HZ
, sci
->sc_mjcp_freq
/ HZ
);
2592 spin_lock(&sci
->sc_state_lock
);
2597 if (sci
->sc_state
& NILFS_SEGCTOR_QUIT
)
2600 if (timeout
|| sci
->sc_seq_request
!= sci
->sc_seq_done
)
2602 else if (!sci
->sc_flush_request
)
2605 mode
= nilfs_segctor_flush_mode(sci
);
2607 spin_unlock(&sci
->sc_state_lock
);
2608 nilfs_segctor_thread_construct(sci
, mode
);
2609 spin_lock(&sci
->sc_state_lock
);
2614 if (freezing(current
)) {
2615 spin_unlock(&sci
->sc_state_lock
);
2617 spin_lock(&sci
->sc_state_lock
);
2620 int should_sleep
= 1;
2622 prepare_to_wait(&sci
->sc_wait_daemon
, &wait
,
2623 TASK_INTERRUPTIBLE
);
2625 if (sci
->sc_seq_request
!= sci
->sc_seq_done
)
2627 else if (sci
->sc_flush_request
)
2629 else if (sci
->sc_state
& NILFS_SEGCTOR_COMMIT
)
2630 should_sleep
= time_before(jiffies
,
2631 sci
->sc_timer
.expires
);
2634 spin_unlock(&sci
->sc_state_lock
);
2636 spin_lock(&sci
->sc_state_lock
);
2638 finish_wait(&sci
->sc_wait_daemon
, &wait
);
2639 timeout
= ((sci
->sc_state
& NILFS_SEGCTOR_COMMIT
) &&
2640 time_after_eq(jiffies
, sci
->sc_timer
.expires
));
2642 if (nilfs_sb_dirty(nilfs
) && nilfs_sb_need_update(nilfs
))
2643 set_nilfs_discontinued(nilfs
);
2648 spin_unlock(&sci
->sc_state_lock
);
2651 sci
->sc_task
= NULL
;
2652 wake_up(&sci
->sc_wait_task
); /* for nilfs_segctor_kill_thread() */
2656 static int nilfs_segctor_start_thread(struct nilfs_sc_info
*sci
)
2658 struct task_struct
*t
;
2660 t
= kthread_run(nilfs_segctor_thread
, sci
, "segctord");
2662 int err
= PTR_ERR(t
);
2664 printk(KERN_ERR
"NILFS: error %d creating segctord thread\n",
2668 wait_event(sci
->sc_wait_task
, sci
->sc_task
!= NULL
);
2672 static void nilfs_segctor_kill_thread(struct nilfs_sc_info
*sci
)
2674 sci
->sc_state
|= NILFS_SEGCTOR_QUIT
;
2676 while (sci
->sc_task
) {
2677 wake_up(&sci
->sc_wait_daemon
);
2678 spin_unlock(&sci
->sc_state_lock
);
2679 wait_event(sci
->sc_wait_task
, sci
->sc_task
== NULL
);
2680 spin_lock(&sci
->sc_state_lock
);
2685 * Setup & clean-up functions
2687 static struct nilfs_sc_info
*nilfs_segctor_new(struct nilfs_sb_info
*sbi
)
2689 struct nilfs_sc_info
*sci
;
2691 sci
= kzalloc(sizeof(*sci
), GFP_KERNEL
);
2696 sci
->sc_super
= sbi
->s_super
;
2698 init_waitqueue_head(&sci
->sc_wait_request
);
2699 init_waitqueue_head(&sci
->sc_wait_daemon
);
2700 init_waitqueue_head(&sci
->sc_wait_task
);
2701 spin_lock_init(&sci
->sc_state_lock
);
2702 INIT_LIST_HEAD(&sci
->sc_dirty_files
);
2703 INIT_LIST_HEAD(&sci
->sc_segbufs
);
2704 INIT_LIST_HEAD(&sci
->sc_write_logs
);
2705 INIT_LIST_HEAD(&sci
->sc_gc_inodes
);
2706 INIT_LIST_HEAD(&sci
->sc_copied_buffers
);
2707 init_timer(&sci
->sc_timer
);
2709 sci
->sc_interval
= HZ
* NILFS_SC_DEFAULT_TIMEOUT
;
2710 sci
->sc_mjcp_freq
= HZ
* NILFS_SC_DEFAULT_SR_FREQ
;
2711 sci
->sc_watermark
= NILFS_SC_DEFAULT_WATERMARK
;
2713 if (sbi
->s_interval
)
2714 sci
->sc_interval
= sbi
->s_interval
;
2715 if (sbi
->s_watermark
)
2716 sci
->sc_watermark
= sbi
->s_watermark
;
2720 static void nilfs_segctor_write_out(struct nilfs_sc_info
*sci
)
2722 int ret
, retrycount
= NILFS_SC_CLEANUP_RETRY
;
2724 /* The segctord thread was stopped and its timer was removed.
2725 But some tasks remain. */
2727 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2728 struct nilfs_transaction_info ti
;
2730 nilfs_transaction_lock(sbi
, &ti
, 0);
2731 ret
= nilfs_segctor_construct(sci
, SC_LSEG_SR
);
2732 nilfs_transaction_unlock(sbi
);
2734 } while (ret
&& retrycount
-- > 0);
2738 * nilfs_segctor_destroy - destroy the segment constructor.
2739 * @sci: nilfs_sc_info
2741 * nilfs_segctor_destroy() kills the segctord thread and frees
2742 * the nilfs_sc_info struct.
2743 * Caller must hold the segment semaphore.
2745 static void nilfs_segctor_destroy(struct nilfs_sc_info
*sci
)
2747 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2750 up_write(&sbi
->s_nilfs
->ns_segctor_sem
);
2752 spin_lock(&sci
->sc_state_lock
);
2753 nilfs_segctor_kill_thread(sci
);
2754 flag
= ((sci
->sc_state
& NILFS_SEGCTOR_COMMIT
) || sci
->sc_flush_request
2755 || sci
->sc_seq_request
!= sci
->sc_seq_done
);
2756 spin_unlock(&sci
->sc_state_lock
);
2758 if (flag
|| !nilfs_segctor_confirm(sci
))
2759 nilfs_segctor_write_out(sci
);
2761 WARN_ON(!list_empty(&sci
->sc_copied_buffers
));
2763 if (!list_empty(&sci
->sc_dirty_files
)) {
2764 nilfs_warning(sbi
->s_super
, __func__
,
2765 "dirty file(s) after the final construction\n");
2766 nilfs_dispose_list(sbi
, &sci
->sc_dirty_files
, 1);
2769 WARN_ON(!list_empty(&sci
->sc_segbufs
));
2770 WARN_ON(!list_empty(&sci
->sc_write_logs
));
2772 down_write(&sbi
->s_nilfs
->ns_segctor_sem
);
2774 del_timer_sync(&sci
->sc_timer
);
2779 * nilfs_attach_segment_constructor - attach a segment constructor
2780 * @sbi: nilfs_sb_info
2782 * nilfs_attach_segment_constructor() allocates a struct nilfs_sc_info,
2783 * initializes it, and starts the segment constructor.
2785 * Return Value: On success, 0 is returned. On error, one of the following
2786 * negative error code is returned.
2788 * %-ENOMEM - Insufficient memory available.
2790 int nilfs_attach_segment_constructor(struct nilfs_sb_info
*sbi
)
2792 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2795 if (NILFS_SC(sbi
)) {
2797 * This happens if the filesystem was remounted
2798 * read/write after nilfs_error degenerated it into a
2801 nilfs_detach_segment_constructor(sbi
);
2804 sbi
->s_sc_info
= nilfs_segctor_new(sbi
);
2805 if (!sbi
->s_sc_info
)
2808 nilfs_attach_writer(nilfs
, sbi
);
2809 err
= nilfs_segctor_start_thread(NILFS_SC(sbi
));
2811 nilfs_detach_writer(nilfs
, sbi
);
2812 kfree(sbi
->s_sc_info
);
2813 sbi
->s_sc_info
= NULL
;
2819 * nilfs_detach_segment_constructor - destroy the segment constructor
2820 * @sbi: nilfs_sb_info
2822 * nilfs_detach_segment_constructor() kills the segment constructor daemon,
2823 * frees the struct nilfs_sc_info, and destroy the dirty file list.
2825 void nilfs_detach_segment_constructor(struct nilfs_sb_info
*sbi
)
2827 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2828 LIST_HEAD(garbage_list
);
2830 down_write(&nilfs
->ns_segctor_sem
);
2831 if (NILFS_SC(sbi
)) {
2832 nilfs_segctor_destroy(NILFS_SC(sbi
));
2833 sbi
->s_sc_info
= NULL
;
2836 /* Force to free the list of dirty files */
2837 spin_lock(&sbi
->s_inode_lock
);
2838 if (!list_empty(&sbi
->s_dirty_files
)) {
2839 list_splice_init(&sbi
->s_dirty_files
, &garbage_list
);
2840 nilfs_warning(sbi
->s_super
, __func__
,
2841 "Non empty dirty list after the last "
2842 "segment construction\n");
2844 spin_unlock(&sbi
->s_inode_lock
);
2845 up_write(&nilfs
->ns_segctor_sem
);
2847 nilfs_dispose_list(sbi
, &garbage_list
, 1);
2848 nilfs_detach_writer(nilfs
, sbi
);