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
, sci
->sc_cno
);
373 sumbh
= NILFS_SEGBUF_FIRST_BH(&segbuf
->sb_segsum_buffers
);
374 sumbytes
= segbuf
->sb_sum
.sumbytes
;
375 sci
->sc_finfo_ptr
.bh
= sumbh
; sci
->sc_finfo_ptr
.offset
= sumbytes
;
376 sci
->sc_binfo_ptr
.bh
= sumbh
; sci
->sc_binfo_ptr
.offset
= sumbytes
;
377 sci
->sc_blk_cnt
= sci
->sc_datablk_cnt
= 0;
381 static int nilfs_segctor_feed_segment(struct nilfs_sc_info
*sci
)
383 sci
->sc_nblk_this_inc
+= sci
->sc_curseg
->sb_sum
.nblocks
;
384 if (NILFS_SEGBUF_IS_LAST(sci
->sc_curseg
, &sci
->sc_segbufs
))
385 return -E2BIG
; /* The current segment is filled up
387 sci
->sc_curseg
= NILFS_NEXT_SEGBUF(sci
->sc_curseg
);
388 return nilfs_segctor_reset_segment_buffer(sci
);
391 static int nilfs_segctor_add_super_root(struct nilfs_sc_info
*sci
)
393 struct nilfs_segment_buffer
*segbuf
= sci
->sc_curseg
;
396 if (segbuf
->sb_sum
.nblocks
>= segbuf
->sb_rest_blocks
) {
397 err
= nilfs_segctor_feed_segment(sci
);
400 segbuf
= sci
->sc_curseg
;
402 err
= nilfs_segbuf_extend_payload(segbuf
, &segbuf
->sb_super_root
);
404 segbuf
->sb_sum
.flags
|= NILFS_SS_SR
;
409 * Functions for making segment summary and payloads
411 static int nilfs_segctor_segsum_block_required(
412 struct nilfs_sc_info
*sci
, const struct nilfs_segsum_pointer
*ssp
,
415 unsigned blocksize
= sci
->sc_super
->s_blocksize
;
416 /* Size of finfo and binfo is enough small against blocksize */
418 return ssp
->offset
+ binfo_size
+
419 (!sci
->sc_blk_cnt
? sizeof(struct nilfs_finfo
) : 0) >
423 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info
*sci
,
426 sci
->sc_curseg
->sb_sum
.nfinfo
++;
427 sci
->sc_binfo_ptr
= sci
->sc_finfo_ptr
;
428 nilfs_segctor_map_segsum_entry(
429 sci
, &sci
->sc_binfo_ptr
, sizeof(struct nilfs_finfo
));
431 if (inode
->i_sb
&& !test_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
))
432 set_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
);
436 static void nilfs_segctor_end_finfo(struct nilfs_sc_info
*sci
,
439 struct nilfs_finfo
*finfo
;
440 struct nilfs_inode_info
*ii
;
441 struct nilfs_segment_buffer
*segbuf
;
444 if (sci
->sc_blk_cnt
== 0)
449 if (test_bit(NILFS_I_GCINODE
, &ii
->i_state
))
451 else if (NILFS_ROOT_METADATA_FILE(inode
->i_ino
))
456 finfo
= nilfs_segctor_map_segsum_entry(sci
, &sci
->sc_finfo_ptr
,
458 finfo
->fi_ino
= cpu_to_le64(inode
->i_ino
);
459 finfo
->fi_nblocks
= cpu_to_le32(sci
->sc_blk_cnt
);
460 finfo
->fi_ndatablk
= cpu_to_le32(sci
->sc_datablk_cnt
);
461 finfo
->fi_cno
= cpu_to_le64(cno
);
463 segbuf
= sci
->sc_curseg
;
464 segbuf
->sb_sum
.sumbytes
= sci
->sc_binfo_ptr
.offset
+
465 sci
->sc_super
->s_blocksize
* (segbuf
->sb_sum
.nsumblk
- 1);
466 sci
->sc_finfo_ptr
= sci
->sc_binfo_ptr
;
467 sci
->sc_blk_cnt
= sci
->sc_datablk_cnt
= 0;
470 static int nilfs_segctor_add_file_block(struct nilfs_sc_info
*sci
,
471 struct buffer_head
*bh
,
475 struct nilfs_segment_buffer
*segbuf
;
476 int required
, err
= 0;
479 segbuf
= sci
->sc_curseg
;
480 required
= nilfs_segctor_segsum_block_required(
481 sci
, &sci
->sc_binfo_ptr
, binfo_size
);
482 if (segbuf
->sb_sum
.nblocks
+ required
+ 1 > segbuf
->sb_rest_blocks
) {
483 nilfs_segctor_end_finfo(sci
, inode
);
484 err
= nilfs_segctor_feed_segment(sci
);
489 if (unlikely(required
)) {
490 err
= nilfs_segbuf_extend_segsum(segbuf
);
494 if (sci
->sc_blk_cnt
== 0)
495 nilfs_segctor_begin_finfo(sci
, inode
);
497 nilfs_segctor_map_segsum_entry(sci
, &sci
->sc_binfo_ptr
, binfo_size
);
498 /* Substitution to vblocknr is delayed until update_blocknr() */
499 nilfs_segbuf_add_file_buffer(segbuf
, bh
);
505 static int nilfs_handle_bmap_error(int err
, const char *fname
,
506 struct inode
*inode
, struct super_block
*sb
)
508 if (err
== -EINVAL
) {
509 nilfs_error(sb
, fname
, "broken bmap (inode=%lu)\n",
517 * Callback functions that enumerate, mark, and collect dirty blocks
519 static int nilfs_collect_file_data(struct nilfs_sc_info
*sci
,
520 struct buffer_head
*bh
, struct inode
*inode
)
524 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
525 if (unlikely(err
< 0))
526 return nilfs_handle_bmap_error(err
, __func__
, inode
,
529 err
= nilfs_segctor_add_file_block(sci
, bh
, inode
,
530 sizeof(struct nilfs_binfo_v
));
532 sci
->sc_datablk_cnt
++;
536 static int nilfs_collect_file_node(struct nilfs_sc_info
*sci
,
537 struct buffer_head
*bh
,
542 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
543 if (unlikely(err
< 0))
544 return nilfs_handle_bmap_error(err
, __func__
, inode
,
549 static int nilfs_collect_file_bmap(struct nilfs_sc_info
*sci
,
550 struct buffer_head
*bh
,
553 WARN_ON(!buffer_dirty(bh
));
554 return nilfs_segctor_add_file_block(sci
, bh
, inode
, sizeof(__le64
));
557 static void nilfs_write_file_data_binfo(struct nilfs_sc_info
*sci
,
558 struct nilfs_segsum_pointer
*ssp
,
559 union nilfs_binfo
*binfo
)
561 struct nilfs_binfo_v
*binfo_v
= nilfs_segctor_map_segsum_entry(
562 sci
, ssp
, sizeof(*binfo_v
));
563 *binfo_v
= binfo
->bi_v
;
566 static void nilfs_write_file_node_binfo(struct nilfs_sc_info
*sci
,
567 struct nilfs_segsum_pointer
*ssp
,
568 union nilfs_binfo
*binfo
)
570 __le64
*vblocknr
= nilfs_segctor_map_segsum_entry(
571 sci
, ssp
, sizeof(*vblocknr
));
572 *vblocknr
= binfo
->bi_v
.bi_vblocknr
;
575 static struct nilfs_sc_operations nilfs_sc_file_ops
= {
576 .collect_data
= nilfs_collect_file_data
,
577 .collect_node
= nilfs_collect_file_node
,
578 .collect_bmap
= nilfs_collect_file_bmap
,
579 .write_data_binfo
= nilfs_write_file_data_binfo
,
580 .write_node_binfo
= nilfs_write_file_node_binfo
,
583 static int nilfs_collect_dat_data(struct nilfs_sc_info
*sci
,
584 struct buffer_head
*bh
, struct inode
*inode
)
588 err
= nilfs_bmap_propagate(NILFS_I(inode
)->i_bmap
, bh
);
589 if (unlikely(err
< 0))
590 return nilfs_handle_bmap_error(err
, __func__
, inode
,
593 err
= nilfs_segctor_add_file_block(sci
, bh
, inode
, sizeof(__le64
));
595 sci
->sc_datablk_cnt
++;
599 static int nilfs_collect_dat_bmap(struct nilfs_sc_info
*sci
,
600 struct buffer_head
*bh
, struct inode
*inode
)
602 WARN_ON(!buffer_dirty(bh
));
603 return nilfs_segctor_add_file_block(sci
, bh
, inode
,
604 sizeof(struct nilfs_binfo_dat
));
607 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info
*sci
,
608 struct nilfs_segsum_pointer
*ssp
,
609 union nilfs_binfo
*binfo
)
611 __le64
*blkoff
= nilfs_segctor_map_segsum_entry(sci
, ssp
,
613 *blkoff
= binfo
->bi_dat
.bi_blkoff
;
616 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info
*sci
,
617 struct nilfs_segsum_pointer
*ssp
,
618 union nilfs_binfo
*binfo
)
620 struct nilfs_binfo_dat
*binfo_dat
=
621 nilfs_segctor_map_segsum_entry(sci
, ssp
, sizeof(*binfo_dat
));
622 *binfo_dat
= binfo
->bi_dat
;
625 static struct nilfs_sc_operations nilfs_sc_dat_ops
= {
626 .collect_data
= nilfs_collect_dat_data
,
627 .collect_node
= nilfs_collect_file_node
,
628 .collect_bmap
= nilfs_collect_dat_bmap
,
629 .write_data_binfo
= nilfs_write_dat_data_binfo
,
630 .write_node_binfo
= nilfs_write_dat_node_binfo
,
633 static struct nilfs_sc_operations nilfs_sc_dsync_ops
= {
634 .collect_data
= nilfs_collect_file_data
,
635 .collect_node
= NULL
,
636 .collect_bmap
= NULL
,
637 .write_data_binfo
= nilfs_write_file_data_binfo
,
638 .write_node_binfo
= NULL
,
641 static size_t nilfs_lookup_dirty_data_buffers(struct inode
*inode
,
642 struct list_head
*listp
,
644 loff_t start
, loff_t end
)
646 struct address_space
*mapping
= inode
->i_mapping
;
648 pgoff_t index
= 0, last
= ULONG_MAX
;
652 if (unlikely(start
!= 0 || end
!= LLONG_MAX
)) {
654 * A valid range is given for sync-ing data pages. The
655 * range is rounded to per-page; extra dirty buffers
656 * may be included if blocksize < pagesize.
658 index
= start
>> PAGE_SHIFT
;
659 last
= end
>> PAGE_SHIFT
;
661 pagevec_init(&pvec
, 0);
663 if (unlikely(index
> last
) ||
664 !pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
665 min_t(pgoff_t
, last
- index
,
666 PAGEVEC_SIZE
- 1) + 1))
669 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
670 struct buffer_head
*bh
, *head
;
671 struct page
*page
= pvec
.pages
[i
];
673 if (unlikely(page
->index
> last
))
678 if (!page_has_buffers(page
))
679 create_empty_buffers(page
,
680 1 << inode
->i_blkbits
, 0);
684 bh
= head
= page_buffers(page
);
686 if (!buffer_dirty(bh
))
689 list_add_tail(&bh
->b_assoc_buffers
, listp
);
691 if (unlikely(ndirties
>= nlimit
)) {
692 pagevec_release(&pvec
);
696 } while (bh
= bh
->b_this_page
, bh
!= head
);
698 pagevec_release(&pvec
);
703 static void nilfs_lookup_dirty_node_buffers(struct inode
*inode
,
704 struct list_head
*listp
)
706 struct nilfs_inode_info
*ii
= NILFS_I(inode
);
707 struct address_space
*mapping
= &ii
->i_btnode_cache
;
709 struct buffer_head
*bh
, *head
;
713 pagevec_init(&pvec
, 0);
715 while (pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
717 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
718 bh
= head
= page_buffers(pvec
.pages
[i
]);
720 if (buffer_dirty(bh
)) {
722 list_add_tail(&bh
->b_assoc_buffers
,
725 bh
= bh
->b_this_page
;
726 } while (bh
!= head
);
728 pagevec_release(&pvec
);
733 static void nilfs_dispose_list(struct nilfs_sb_info
*sbi
,
734 struct list_head
*head
, int force
)
736 struct nilfs_inode_info
*ii
, *n
;
737 struct nilfs_inode_info
*ivec
[SC_N_INODEVEC
], **pii
;
740 while (!list_empty(head
)) {
741 spin_lock(&sbi
->s_inode_lock
);
742 list_for_each_entry_safe(ii
, n
, head
, i_dirty
) {
743 list_del_init(&ii
->i_dirty
);
745 if (unlikely(ii
->i_bh
)) {
749 } else if (test_bit(NILFS_I_DIRTY
, &ii
->i_state
)) {
750 set_bit(NILFS_I_QUEUED
, &ii
->i_state
);
751 list_add_tail(&ii
->i_dirty
,
752 &sbi
->s_dirty_files
);
756 if (nv
== SC_N_INODEVEC
)
759 spin_unlock(&sbi
->s_inode_lock
);
761 for (pii
= ivec
; nv
> 0; pii
++, nv
--)
762 iput(&(*pii
)->vfs_inode
);
766 static int nilfs_test_metadata_dirty(struct nilfs_sb_info
*sbi
)
768 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
771 if (nilfs_mdt_fetch_dirty(sbi
->s_ifile
))
773 if (nilfs_mdt_fetch_dirty(nilfs
->ns_cpfile
))
775 if (nilfs_mdt_fetch_dirty(nilfs
->ns_sufile
))
777 if (ret
|| nilfs_doing_gc())
778 if (nilfs_mdt_fetch_dirty(nilfs_dat_inode(nilfs
)))
783 static int nilfs_segctor_clean(struct nilfs_sc_info
*sci
)
785 return list_empty(&sci
->sc_dirty_files
) &&
786 !test_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
) &&
787 sci
->sc_nfreesegs
== 0 &&
788 (!nilfs_doing_gc() || list_empty(&sci
->sc_gc_inodes
));
791 static int nilfs_segctor_confirm(struct nilfs_sc_info
*sci
)
793 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
796 if (nilfs_test_metadata_dirty(sbi
))
797 set_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
799 spin_lock(&sbi
->s_inode_lock
);
800 if (list_empty(&sbi
->s_dirty_files
) && nilfs_segctor_clean(sci
))
803 spin_unlock(&sbi
->s_inode_lock
);
807 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info
*sci
)
809 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
810 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
812 nilfs_mdt_clear_dirty(sbi
->s_ifile
);
813 nilfs_mdt_clear_dirty(nilfs
->ns_cpfile
);
814 nilfs_mdt_clear_dirty(nilfs
->ns_sufile
);
815 nilfs_mdt_clear_dirty(nilfs_dat_inode(nilfs
));
818 static int nilfs_segctor_create_checkpoint(struct nilfs_sc_info
*sci
)
820 struct the_nilfs
*nilfs
= sci
->sc_sbi
->s_nilfs
;
821 struct buffer_head
*bh_cp
;
822 struct nilfs_checkpoint
*raw_cp
;
825 /* XXX: this interface will be changed */
826 err
= nilfs_cpfile_get_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, 1,
829 /* The following code is duplicated with cpfile. But, it is
830 needed to collect the checkpoint even if it was not newly
832 nilfs_mdt_mark_buffer_dirty(bh_cp
);
833 nilfs_mdt_mark_dirty(nilfs
->ns_cpfile
);
834 nilfs_cpfile_put_checkpoint(
835 nilfs
->ns_cpfile
, nilfs
->ns_cno
, bh_cp
);
837 WARN_ON(err
== -EINVAL
|| err
== -ENOENT
);
842 static int nilfs_segctor_fill_in_checkpoint(struct nilfs_sc_info
*sci
)
844 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
845 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
846 struct buffer_head
*bh_cp
;
847 struct nilfs_checkpoint
*raw_cp
;
850 err
= nilfs_cpfile_get_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, 0,
853 WARN_ON(err
== -EINVAL
|| err
== -ENOENT
);
856 raw_cp
->cp_snapshot_list
.ssl_next
= 0;
857 raw_cp
->cp_snapshot_list
.ssl_prev
= 0;
858 raw_cp
->cp_inodes_count
=
859 cpu_to_le64(atomic_read(&sbi
->s_inodes_count
));
860 raw_cp
->cp_blocks_count
=
861 cpu_to_le64(atomic_read(&sbi
->s_blocks_count
));
862 raw_cp
->cp_nblk_inc
=
863 cpu_to_le64(sci
->sc_nblk_inc
+ sci
->sc_nblk_this_inc
);
864 raw_cp
->cp_create
= cpu_to_le64(sci
->sc_seg_ctime
);
865 raw_cp
->cp_cno
= cpu_to_le64(nilfs
->ns_cno
);
867 if (test_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
))
868 nilfs_checkpoint_clear_minor(raw_cp
);
870 nilfs_checkpoint_set_minor(raw_cp
);
872 nilfs_write_inode_common(sbi
->s_ifile
, &raw_cp
->cp_ifile_inode
, 1);
873 nilfs_cpfile_put_checkpoint(nilfs
->ns_cpfile
, nilfs
->ns_cno
, bh_cp
);
880 static void nilfs_fill_in_file_bmap(struct inode
*ifile
,
881 struct nilfs_inode_info
*ii
)
884 struct buffer_head
*ibh
;
885 struct nilfs_inode
*raw_inode
;
887 if (test_bit(NILFS_I_BMAP
, &ii
->i_state
)) {
890 raw_inode
= nilfs_ifile_map_inode(ifile
, ii
->vfs_inode
.i_ino
,
892 nilfs_bmap_write(ii
->i_bmap
, raw_inode
);
893 nilfs_ifile_unmap_inode(ifile
, ii
->vfs_inode
.i_ino
, ibh
);
897 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info
*sci
,
900 struct nilfs_inode_info
*ii
;
902 list_for_each_entry(ii
, &sci
->sc_dirty_files
, i_dirty
) {
903 nilfs_fill_in_file_bmap(ifile
, ii
);
904 set_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
908 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info
*sci
,
909 struct the_nilfs
*nilfs
)
911 struct buffer_head
*bh_sr
;
912 struct nilfs_super_root
*raw_sr
;
913 unsigned isz
= nilfs
->ns_inode_size
;
915 bh_sr
= NILFS_LAST_SEGBUF(&sci
->sc_segbufs
)->sb_super_root
;
916 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
918 raw_sr
->sr_bytes
= cpu_to_le16(NILFS_SR_BYTES
);
919 raw_sr
->sr_nongc_ctime
920 = cpu_to_le64(nilfs_doing_gc() ?
921 nilfs
->ns_nongc_ctime
: sci
->sc_seg_ctime
);
922 raw_sr
->sr_flags
= 0;
924 nilfs_write_inode_common(nilfs_dat_inode(nilfs
), (void *)raw_sr
+
925 NILFS_SR_DAT_OFFSET(isz
), 1);
926 nilfs_write_inode_common(nilfs
->ns_cpfile
, (void *)raw_sr
+
927 NILFS_SR_CPFILE_OFFSET(isz
), 1);
928 nilfs_write_inode_common(nilfs
->ns_sufile
, (void *)raw_sr
+
929 NILFS_SR_SUFILE_OFFSET(isz
), 1);
932 static void nilfs_redirty_inodes(struct list_head
*head
)
934 struct nilfs_inode_info
*ii
;
936 list_for_each_entry(ii
, head
, i_dirty
) {
937 if (test_bit(NILFS_I_COLLECTED
, &ii
->i_state
))
938 clear_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
942 static void nilfs_drop_collected_inodes(struct list_head
*head
)
944 struct nilfs_inode_info
*ii
;
946 list_for_each_entry(ii
, head
, i_dirty
) {
947 if (!test_and_clear_bit(NILFS_I_COLLECTED
, &ii
->i_state
))
950 clear_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
);
951 set_bit(NILFS_I_UPDATED
, &ii
->i_state
);
955 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info
*sci
,
957 struct list_head
*listp
,
958 int (*collect
)(struct nilfs_sc_info
*,
959 struct buffer_head
*,
962 struct buffer_head
*bh
, *n
;
966 list_for_each_entry_safe(bh
, n
, listp
, b_assoc_buffers
) {
967 list_del_init(&bh
->b_assoc_buffers
);
968 err
= collect(sci
, bh
, inode
);
971 goto dispose_buffers
;
977 while (!list_empty(listp
)) {
978 bh
= list_entry(listp
->next
, struct buffer_head
,
980 list_del_init(&bh
->b_assoc_buffers
);
986 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info
*sci
)
988 /* Remaining number of blocks within segment buffer */
989 return sci
->sc_segbuf_nblocks
-
990 (sci
->sc_nblk_this_inc
+ sci
->sc_curseg
->sb_sum
.nblocks
);
993 static int nilfs_segctor_scan_file(struct nilfs_sc_info
*sci
,
995 struct nilfs_sc_operations
*sc_ops
)
997 LIST_HEAD(data_buffers
);
998 LIST_HEAD(node_buffers
);
1001 if (!(sci
->sc_stage
.flags
& NILFS_CF_NODE
)) {
1002 size_t n
, rest
= nilfs_segctor_buffer_rest(sci
);
1004 n
= nilfs_lookup_dirty_data_buffers(
1005 inode
, &data_buffers
, rest
+ 1, 0, LLONG_MAX
);
1007 err
= nilfs_segctor_apply_buffers(
1008 sci
, inode
, &data_buffers
,
1009 sc_ops
->collect_data
);
1010 BUG_ON(!err
); /* always receive -E2BIG or true error */
1014 nilfs_lookup_dirty_node_buffers(inode
, &node_buffers
);
1016 if (!(sci
->sc_stage
.flags
& NILFS_CF_NODE
)) {
1017 err
= nilfs_segctor_apply_buffers(
1018 sci
, inode
, &data_buffers
, sc_ops
->collect_data
);
1019 if (unlikely(err
)) {
1020 /* dispose node list */
1021 nilfs_segctor_apply_buffers(
1022 sci
, inode
, &node_buffers
, NULL
);
1025 sci
->sc_stage
.flags
|= NILFS_CF_NODE
;
1028 err
= nilfs_segctor_apply_buffers(
1029 sci
, inode
, &node_buffers
, sc_ops
->collect_node
);
1033 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode
)->i_bmap
, &node_buffers
);
1034 err
= nilfs_segctor_apply_buffers(
1035 sci
, inode
, &node_buffers
, sc_ops
->collect_bmap
);
1039 nilfs_segctor_end_finfo(sci
, inode
);
1040 sci
->sc_stage
.flags
&= ~NILFS_CF_NODE
;
1046 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info
*sci
,
1047 struct inode
*inode
)
1049 LIST_HEAD(data_buffers
);
1050 size_t n
, rest
= nilfs_segctor_buffer_rest(sci
);
1053 n
= nilfs_lookup_dirty_data_buffers(inode
, &data_buffers
, rest
+ 1,
1054 sci
->sc_dsync_start
,
1057 err
= nilfs_segctor_apply_buffers(sci
, inode
, &data_buffers
,
1058 nilfs_collect_file_data
);
1060 nilfs_segctor_end_finfo(sci
, inode
);
1062 /* always receive -E2BIG or true error if n > rest */
1067 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info
*sci
, int mode
)
1069 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
1070 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
1071 struct list_head
*head
;
1072 struct nilfs_inode_info
*ii
;
1076 switch (sci
->sc_stage
.scnt
) {
1079 sci
->sc_stage
.flags
= 0;
1081 if (!test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
)) {
1082 sci
->sc_nblk_inc
= 0;
1083 sci
->sc_curseg
->sb_sum
.flags
= NILFS_SS_LOGBGN
;
1084 if (mode
== SC_LSEG_DSYNC
) {
1085 sci
->sc_stage
.scnt
= NILFS_ST_DSYNC
;
1090 sci
->sc_stage
.dirty_file_ptr
= NULL
;
1091 sci
->sc_stage
.gc_inode_ptr
= NULL
;
1092 if (mode
== SC_FLUSH_DAT
) {
1093 sci
->sc_stage
.scnt
= NILFS_ST_DAT
;
1096 sci
->sc_stage
.scnt
++; /* Fall through */
1098 if (nilfs_doing_gc()) {
1099 head
= &sci
->sc_gc_inodes
;
1100 ii
= list_prepare_entry(sci
->sc_stage
.gc_inode_ptr
,
1102 list_for_each_entry_continue(ii
, head
, i_dirty
) {
1103 err
= nilfs_segctor_scan_file(
1104 sci
, &ii
->vfs_inode
,
1105 &nilfs_sc_file_ops
);
1106 if (unlikely(err
)) {
1107 sci
->sc_stage
.gc_inode_ptr
= list_entry(
1109 struct nilfs_inode_info
,
1113 set_bit(NILFS_I_COLLECTED
, &ii
->i_state
);
1115 sci
->sc_stage
.gc_inode_ptr
= NULL
;
1117 sci
->sc_stage
.scnt
++; /* Fall through */
1119 head
= &sci
->sc_dirty_files
;
1120 ii
= list_prepare_entry(sci
->sc_stage
.dirty_file_ptr
, head
,
1122 list_for_each_entry_continue(ii
, head
, i_dirty
) {
1123 clear_bit(NILFS_I_DIRTY
, &ii
->i_state
);
1125 err
= nilfs_segctor_scan_file(sci
, &ii
->vfs_inode
,
1126 &nilfs_sc_file_ops
);
1127 if (unlikely(err
)) {
1128 sci
->sc_stage
.dirty_file_ptr
=
1129 list_entry(ii
->i_dirty
.prev
,
1130 struct nilfs_inode_info
,
1134 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1135 /* XXX: required ? */
1137 sci
->sc_stage
.dirty_file_ptr
= NULL
;
1138 if (mode
== SC_FLUSH_FILE
) {
1139 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1142 sci
->sc_stage
.scnt
++;
1143 sci
->sc_stage
.flags
|= NILFS_CF_IFILE_STARTED
;
1145 case NILFS_ST_IFILE
:
1146 err
= nilfs_segctor_scan_file(sci
, sbi
->s_ifile
,
1147 &nilfs_sc_file_ops
);
1150 sci
->sc_stage
.scnt
++;
1151 /* Creating a checkpoint */
1152 err
= nilfs_segctor_create_checkpoint(sci
);
1156 case NILFS_ST_CPFILE
:
1157 err
= nilfs_segctor_scan_file(sci
, nilfs
->ns_cpfile
,
1158 &nilfs_sc_file_ops
);
1161 sci
->sc_stage
.scnt
++; /* Fall through */
1162 case NILFS_ST_SUFILE
:
1163 err
= nilfs_sufile_freev(nilfs
->ns_sufile
, sci
->sc_freesegs
,
1164 sci
->sc_nfreesegs
, &ndone
);
1165 if (unlikely(err
)) {
1166 nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1167 sci
->sc_freesegs
, ndone
,
1171 sci
->sc_stage
.flags
|= NILFS_CF_SUFREED
;
1173 err
= nilfs_segctor_scan_file(sci
, nilfs
->ns_sufile
,
1174 &nilfs_sc_file_ops
);
1177 sci
->sc_stage
.scnt
++; /* Fall through */
1180 err
= nilfs_segctor_scan_file(sci
, nilfs_dat_inode(nilfs
),
1184 if (mode
== SC_FLUSH_DAT
) {
1185 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1188 sci
->sc_stage
.scnt
++; /* Fall through */
1190 if (mode
== SC_LSEG_SR
) {
1191 /* Appending a super root */
1192 err
= nilfs_segctor_add_super_root(sci
);
1196 /* End of a logical segment */
1197 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_LOGEND
;
1198 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1200 case NILFS_ST_DSYNC
:
1202 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_SYNDT
;
1203 ii
= sci
->sc_dsync_inode
;
1204 if (!test_bit(NILFS_I_BUSY
, &ii
->i_state
))
1207 err
= nilfs_segctor_scan_file_dsync(sci
, &ii
->vfs_inode
);
1210 sci
->sc_curseg
->sb_sum
.flags
|= NILFS_SS_LOGEND
;
1211 sci
->sc_stage
.scnt
= NILFS_ST_DONE
;
1224 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1225 * @sci: nilfs_sc_info
1226 * @nilfs: nilfs object
1228 static int nilfs_segctor_begin_construction(struct nilfs_sc_info
*sci
,
1229 struct the_nilfs
*nilfs
)
1231 struct nilfs_segment_buffer
*segbuf
, *prev
;
1235 segbuf
= nilfs_segbuf_new(sci
->sc_super
);
1236 if (unlikely(!segbuf
))
1239 if (list_empty(&sci
->sc_write_logs
)) {
1240 nilfs_segbuf_map(segbuf
, nilfs
->ns_segnum
,
1241 nilfs
->ns_pseg_offset
, nilfs
);
1242 if (segbuf
->sb_rest_blocks
< NILFS_PSEG_MIN_BLOCKS
) {
1243 nilfs_shift_to_next_segment(nilfs
);
1244 nilfs_segbuf_map(segbuf
, nilfs
->ns_segnum
, 0, nilfs
);
1247 segbuf
->sb_sum
.seg_seq
= nilfs
->ns_seg_seq
;
1248 nextnum
= nilfs
->ns_nextnum
;
1250 if (nilfs
->ns_segnum
== nilfs
->ns_nextnum
)
1251 /* Start from the head of a new full segment */
1255 prev
= NILFS_LAST_SEGBUF(&sci
->sc_write_logs
);
1256 nilfs_segbuf_map_cont(segbuf
, prev
);
1257 segbuf
->sb_sum
.seg_seq
= prev
->sb_sum
.seg_seq
;
1258 nextnum
= prev
->sb_nextnum
;
1260 if (segbuf
->sb_rest_blocks
< NILFS_PSEG_MIN_BLOCKS
) {
1261 nilfs_segbuf_map(segbuf
, prev
->sb_nextnum
, 0, nilfs
);
1262 segbuf
->sb_sum
.seg_seq
++;
1267 err
= nilfs_sufile_mark_dirty(nilfs
->ns_sufile
, segbuf
->sb_segnum
);
1272 err
= nilfs_sufile_alloc(nilfs
->ns_sufile
, &nextnum
);
1276 nilfs_segbuf_set_next_segnum(segbuf
, nextnum
, nilfs
);
1278 BUG_ON(!list_empty(&sci
->sc_segbufs
));
1279 list_add_tail(&segbuf
->sb_list
, &sci
->sc_segbufs
);
1280 sci
->sc_segbuf_nblocks
= segbuf
->sb_rest_blocks
;
1284 nilfs_segbuf_free(segbuf
);
1288 static int nilfs_segctor_extend_segments(struct nilfs_sc_info
*sci
,
1289 struct the_nilfs
*nilfs
, int nadd
)
1291 struct nilfs_segment_buffer
*segbuf
, *prev
;
1292 struct inode
*sufile
= nilfs
->ns_sufile
;
1297 prev
= NILFS_LAST_SEGBUF(&sci
->sc_segbufs
);
1299 * Since the segment specified with nextnum might be allocated during
1300 * the previous construction, the buffer including its segusage may
1301 * not be dirty. The following call ensures that the buffer is dirty
1302 * and will pin the buffer on memory until the sufile is written.
1304 err
= nilfs_sufile_mark_dirty(sufile
, prev
->sb_nextnum
);
1308 for (i
= 0; i
< nadd
; i
++) {
1309 /* extend segment info */
1311 segbuf
= nilfs_segbuf_new(sci
->sc_super
);
1312 if (unlikely(!segbuf
))
1315 /* map this buffer to region of segment on-disk */
1316 nilfs_segbuf_map(segbuf
, prev
->sb_nextnum
, 0, nilfs
);
1317 sci
->sc_segbuf_nblocks
+= segbuf
->sb_rest_blocks
;
1319 /* allocate the next next full segment */
1320 err
= nilfs_sufile_alloc(sufile
, &nextnextnum
);
1324 segbuf
->sb_sum
.seg_seq
= prev
->sb_sum
.seg_seq
+ 1;
1325 nilfs_segbuf_set_next_segnum(segbuf
, nextnextnum
, nilfs
);
1327 list_add_tail(&segbuf
->sb_list
, &list
);
1330 list_splice_tail(&list
, &sci
->sc_segbufs
);
1334 nilfs_segbuf_free(segbuf
);
1336 list_for_each_entry(segbuf
, &list
, sb_list
) {
1337 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1338 WARN_ON(ret
); /* never fails */
1340 nilfs_destroy_logs(&list
);
1344 static void nilfs_free_incomplete_logs(struct list_head
*logs
,
1345 struct the_nilfs
*nilfs
)
1347 struct nilfs_segment_buffer
*segbuf
, *prev
;
1348 struct inode
*sufile
= nilfs
->ns_sufile
;
1351 segbuf
= NILFS_FIRST_SEGBUF(logs
);
1352 if (nilfs
->ns_nextnum
!= segbuf
->sb_nextnum
) {
1353 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1354 WARN_ON(ret
); /* never fails */
1356 if (atomic_read(&segbuf
->sb_err
)) {
1357 /* Case 1: The first segment failed */
1358 if (segbuf
->sb_pseg_start
!= segbuf
->sb_fseg_start
)
1359 /* Case 1a: Partial segment appended into an existing
1361 nilfs_terminate_segment(nilfs
, segbuf
->sb_fseg_start
,
1362 segbuf
->sb_fseg_end
);
1363 else /* Case 1b: New full segment */
1364 set_nilfs_discontinued(nilfs
);
1368 list_for_each_entry_continue(segbuf
, logs
, sb_list
) {
1369 if (prev
->sb_nextnum
!= segbuf
->sb_nextnum
) {
1370 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1371 WARN_ON(ret
); /* never fails */
1373 if (atomic_read(&segbuf
->sb_err
) &&
1374 segbuf
->sb_segnum
!= nilfs
->ns_nextnum
)
1375 /* Case 2: extended segment (!= next) failed */
1376 nilfs_sufile_set_error(sufile
, segbuf
->sb_segnum
);
1381 static void nilfs_segctor_update_segusage(struct nilfs_sc_info
*sci
,
1382 struct inode
*sufile
)
1384 struct nilfs_segment_buffer
*segbuf
;
1385 unsigned long live_blocks
;
1388 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1389 live_blocks
= segbuf
->sb_sum
.nblocks
+
1390 (segbuf
->sb_pseg_start
- segbuf
->sb_fseg_start
);
1391 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1394 WARN_ON(ret
); /* always succeed because the segusage is dirty */
1398 static void nilfs_cancel_segusage(struct list_head
*logs
, struct inode
*sufile
)
1400 struct nilfs_segment_buffer
*segbuf
;
1403 segbuf
= NILFS_FIRST_SEGBUF(logs
);
1404 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1405 segbuf
->sb_pseg_start
-
1406 segbuf
->sb_fseg_start
, 0);
1407 WARN_ON(ret
); /* always succeed because the segusage is dirty */
1409 list_for_each_entry_continue(segbuf
, logs
, sb_list
) {
1410 ret
= nilfs_sufile_set_segment_usage(sufile
, segbuf
->sb_segnum
,
1412 WARN_ON(ret
); /* always succeed */
1416 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info
*sci
,
1417 struct nilfs_segment_buffer
*last
,
1418 struct inode
*sufile
)
1420 struct nilfs_segment_buffer
*segbuf
= last
;
1423 list_for_each_entry_continue(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1424 sci
->sc_segbuf_nblocks
-= segbuf
->sb_rest_blocks
;
1425 ret
= nilfs_sufile_free(sufile
, segbuf
->sb_nextnum
);
1428 nilfs_truncate_logs(&sci
->sc_segbufs
, last
);
1432 static int nilfs_segctor_collect(struct nilfs_sc_info
*sci
,
1433 struct the_nilfs
*nilfs
, int mode
)
1435 struct nilfs_cstage prev_stage
= sci
->sc_stage
;
1438 /* Collection retry loop */
1440 sci
->sc_nblk_this_inc
= 0;
1441 sci
->sc_curseg
= NILFS_FIRST_SEGBUF(&sci
->sc_segbufs
);
1443 err
= nilfs_segctor_reset_segment_buffer(sci
);
1447 err
= nilfs_segctor_collect_blocks(sci
, mode
);
1448 sci
->sc_nblk_this_inc
+= sci
->sc_curseg
->sb_sum
.nblocks
;
1452 if (unlikely(err
!= -E2BIG
))
1455 /* The current segment is filled up */
1456 if (mode
!= SC_LSEG_SR
|| sci
->sc_stage
.scnt
< NILFS_ST_CPFILE
)
1459 nilfs_clear_logs(&sci
->sc_segbufs
);
1461 err
= nilfs_segctor_extend_segments(sci
, nilfs
, nadd
);
1465 if (sci
->sc_stage
.flags
& NILFS_CF_SUFREED
) {
1466 err
= nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1470 WARN_ON(err
); /* do not happen */
1472 nadd
= min_t(int, nadd
<< 1, SC_MAX_SEGDELTA
);
1473 sci
->sc_stage
= prev_stage
;
1475 nilfs_segctor_truncate_segments(sci
, sci
->sc_curseg
, nilfs
->ns_sufile
);
1482 static void nilfs_list_replace_buffer(struct buffer_head
*old_bh
,
1483 struct buffer_head
*new_bh
)
1485 BUG_ON(!list_empty(&new_bh
->b_assoc_buffers
));
1487 list_replace_init(&old_bh
->b_assoc_buffers
, &new_bh
->b_assoc_buffers
);
1488 /* The caller must release old_bh */
1492 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info
*sci
,
1493 struct nilfs_segment_buffer
*segbuf
,
1496 struct inode
*inode
= NULL
;
1498 unsigned long nfinfo
= segbuf
->sb_sum
.nfinfo
;
1499 unsigned long nblocks
= 0, ndatablk
= 0;
1500 struct nilfs_sc_operations
*sc_op
= NULL
;
1501 struct nilfs_segsum_pointer ssp
;
1502 struct nilfs_finfo
*finfo
= NULL
;
1503 union nilfs_binfo binfo
;
1504 struct buffer_head
*bh
, *bh_org
;
1511 blocknr
= segbuf
->sb_pseg_start
+ segbuf
->sb_sum
.nsumblk
;
1512 ssp
.bh
= NILFS_SEGBUF_FIRST_BH(&segbuf
->sb_segsum_buffers
);
1513 ssp
.offset
= sizeof(struct nilfs_segment_summary
);
1515 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
, b_assoc_buffers
) {
1516 if (bh
== segbuf
->sb_super_root
)
1519 finfo
= nilfs_segctor_map_segsum_entry(
1520 sci
, &ssp
, sizeof(*finfo
));
1521 ino
= le64_to_cpu(finfo
->fi_ino
);
1522 nblocks
= le32_to_cpu(finfo
->fi_nblocks
);
1523 ndatablk
= le32_to_cpu(finfo
->fi_ndatablk
);
1525 if (buffer_nilfs_node(bh
))
1526 inode
= NILFS_BTNC_I(bh
->b_page
->mapping
);
1528 inode
= NILFS_AS_I(bh
->b_page
->mapping
);
1530 if (mode
== SC_LSEG_DSYNC
)
1531 sc_op
= &nilfs_sc_dsync_ops
;
1532 else if (ino
== NILFS_DAT_INO
)
1533 sc_op
= &nilfs_sc_dat_ops
;
1534 else /* file blocks */
1535 sc_op
= &nilfs_sc_file_ops
;
1539 err
= nilfs_bmap_assign(NILFS_I(inode
)->i_bmap
, &bh
, blocknr
,
1542 nilfs_list_replace_buffer(bh_org
, bh
);
1548 sc_op
->write_data_binfo(sci
, &ssp
, &binfo
);
1550 sc_op
->write_node_binfo(sci
, &ssp
, &binfo
);
1553 if (--nblocks
== 0) {
1557 } else if (ndatablk
> 0)
1564 err
= nilfs_handle_bmap_error(err
, __func__
, inode
, sci
->sc_super
);
1568 static int nilfs_segctor_assign(struct nilfs_sc_info
*sci
, int mode
)
1570 struct nilfs_segment_buffer
*segbuf
;
1573 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1574 err
= nilfs_segctor_update_payload_blocknr(sci
, segbuf
, mode
);
1577 nilfs_segbuf_fill_in_segsum(segbuf
);
1583 nilfs_copy_replace_page_buffers(struct page
*page
, struct list_head
*out
)
1585 struct page
*clone_page
;
1586 struct buffer_head
*bh
, *head
, *bh2
;
1589 bh
= head
= page_buffers(page
);
1591 clone_page
= nilfs_alloc_private_page(bh
->b_bdev
, bh
->b_size
, 0);
1592 if (unlikely(!clone_page
))
1595 bh2
= page_buffers(clone_page
);
1596 kaddr
= kmap_atomic(page
, KM_USER0
);
1598 if (list_empty(&bh
->b_assoc_buffers
))
1601 page_cache_get(clone_page
); /* for each bh */
1602 memcpy(bh2
->b_data
, kaddr
+ bh_offset(bh
), bh2
->b_size
);
1603 bh2
->b_blocknr
= bh
->b_blocknr
;
1604 list_replace(&bh
->b_assoc_buffers
, &bh2
->b_assoc_buffers
);
1605 list_add_tail(&bh
->b_assoc_buffers
, out
);
1606 } while (bh
= bh
->b_this_page
, bh2
= bh2
->b_this_page
, bh
!= head
);
1607 kunmap_atomic(kaddr
, KM_USER0
);
1609 if (!TestSetPageWriteback(clone_page
))
1610 inc_zone_page_state(clone_page
, NR_WRITEBACK
);
1611 unlock_page(clone_page
);
1616 static int nilfs_test_page_to_be_frozen(struct page
*page
)
1618 struct address_space
*mapping
= page
->mapping
;
1620 if (!mapping
|| !mapping
->host
|| S_ISDIR(mapping
->host
->i_mode
))
1623 if (page_mapped(page
)) {
1624 ClearPageChecked(page
);
1627 return PageChecked(page
);
1630 static int nilfs_begin_page_io(struct page
*page
, struct list_head
*out
)
1632 if (!page
|| PageWriteback(page
))
1633 /* For split b-tree node pages, this function may be called
1634 twice. We ignore the 2nd or later calls by this check. */
1638 clear_page_dirty_for_io(page
);
1639 set_page_writeback(page
);
1642 if (nilfs_test_page_to_be_frozen(page
)) {
1643 int err
= nilfs_copy_replace_page_buffers(page
, out
);
1650 static int nilfs_segctor_prepare_write(struct nilfs_sc_info
*sci
,
1651 struct page
**failed_page
)
1653 struct nilfs_segment_buffer
*segbuf
;
1654 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1655 struct list_head
*list
= &sci
->sc_copied_buffers
;
1658 *failed_page
= NULL
;
1659 list_for_each_entry(segbuf
, &sci
->sc_segbufs
, sb_list
) {
1660 struct buffer_head
*bh
;
1662 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1664 if (bh
->b_page
!= bd_page
) {
1667 clear_page_dirty_for_io(bd_page
);
1668 set_page_writeback(bd_page
);
1669 unlock_page(bd_page
);
1671 bd_page
= bh
->b_page
;
1675 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1677 if (bh
== segbuf
->sb_super_root
) {
1678 if (bh
->b_page
!= bd_page
) {
1680 clear_page_dirty_for_io(bd_page
);
1681 set_page_writeback(bd_page
);
1682 unlock_page(bd_page
);
1683 bd_page
= bh
->b_page
;
1687 if (bh
->b_page
!= fs_page
) {
1688 err
= nilfs_begin_page_io(fs_page
, list
);
1689 if (unlikely(err
)) {
1690 *failed_page
= fs_page
;
1693 fs_page
= bh
->b_page
;
1699 clear_page_dirty_for_io(bd_page
);
1700 set_page_writeback(bd_page
);
1701 unlock_page(bd_page
);
1703 err
= nilfs_begin_page_io(fs_page
, list
);
1705 *failed_page
= fs_page
;
1710 static int nilfs_segctor_write(struct nilfs_sc_info
*sci
,
1711 struct the_nilfs
*nilfs
)
1715 ret
= nilfs_write_logs(&sci
->sc_segbufs
, nilfs
);
1716 list_splice_tail_init(&sci
->sc_segbufs
, &sci
->sc_write_logs
);
1720 static void __nilfs_end_page_io(struct page
*page
, int err
)
1723 if (!nilfs_page_buffers_clean(page
))
1724 __set_page_dirty_nobuffers(page
);
1725 ClearPageError(page
);
1727 __set_page_dirty_nobuffers(page
);
1731 if (buffer_nilfs_allocated(page_buffers(page
))) {
1732 if (TestClearPageWriteback(page
))
1733 dec_zone_page_state(page
, NR_WRITEBACK
);
1735 end_page_writeback(page
);
1738 static void nilfs_end_page_io(struct page
*page
, int err
)
1743 if (buffer_nilfs_node(page_buffers(page
)) && !PageWriteback(page
)) {
1745 * For b-tree node pages, this function may be called twice
1746 * or more because they might be split in a segment.
1748 if (PageDirty(page
)) {
1750 * For pages holding split b-tree node buffers, dirty
1751 * flag on the buffers may be cleared discretely.
1752 * In that case, the page is once redirtied for
1753 * remaining buffers, and it must be cancelled if
1754 * all the buffers get cleaned later.
1757 if (nilfs_page_buffers_clean(page
))
1758 __nilfs_clear_page_dirty(page
);
1764 __nilfs_end_page_io(page
, err
);
1767 static void nilfs_clear_copied_buffers(struct list_head
*list
, int err
)
1769 struct buffer_head
*bh
, *head
;
1772 while (!list_empty(list
)) {
1773 bh
= list_entry(list
->next
, struct buffer_head
,
1776 page_cache_get(page
);
1777 head
= bh
= page_buffers(page
);
1779 if (!list_empty(&bh
->b_assoc_buffers
)) {
1780 list_del_init(&bh
->b_assoc_buffers
);
1782 set_buffer_uptodate(bh
);
1783 clear_buffer_dirty(bh
);
1784 clear_buffer_nilfs_volatile(bh
);
1786 brelse(bh
); /* for b_assoc_buffers */
1788 } while ((bh
= bh
->b_this_page
) != head
);
1790 __nilfs_end_page_io(page
, err
);
1791 page_cache_release(page
);
1795 static void nilfs_abort_logs(struct list_head
*logs
, struct page
*failed_page
,
1798 struct nilfs_segment_buffer
*segbuf
;
1799 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1800 struct buffer_head
*bh
;
1802 if (list_empty(logs
))
1805 list_for_each_entry(segbuf
, logs
, sb_list
) {
1806 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1808 if (bh
->b_page
!= bd_page
) {
1810 end_page_writeback(bd_page
);
1811 bd_page
= bh
->b_page
;
1815 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1817 if (bh
== segbuf
->sb_super_root
) {
1818 if (bh
->b_page
!= bd_page
) {
1819 end_page_writeback(bd_page
);
1820 bd_page
= bh
->b_page
;
1824 if (bh
->b_page
!= fs_page
) {
1825 nilfs_end_page_io(fs_page
, err
);
1826 if (fs_page
&& fs_page
== failed_page
)
1828 fs_page
= bh
->b_page
;
1833 end_page_writeback(bd_page
);
1835 nilfs_end_page_io(fs_page
, err
);
1838 static void nilfs_segctor_abort_construction(struct nilfs_sc_info
*sci
,
1839 struct the_nilfs
*nilfs
, int err
)
1844 list_splice_tail_init(&sci
->sc_write_logs
, &logs
);
1845 ret
= nilfs_wait_on_logs(&logs
);
1846 nilfs_abort_logs(&logs
, NULL
, ret
? : err
);
1848 list_splice_tail_init(&sci
->sc_segbufs
, &logs
);
1849 nilfs_cancel_segusage(&logs
, nilfs
->ns_sufile
);
1850 nilfs_free_incomplete_logs(&logs
, nilfs
);
1851 nilfs_clear_copied_buffers(&sci
->sc_copied_buffers
, err
);
1853 if (sci
->sc_stage
.flags
& NILFS_CF_SUFREED
) {
1854 ret
= nilfs_sufile_cancel_freev(nilfs
->ns_sufile
,
1858 WARN_ON(ret
); /* do not happen */
1861 nilfs_destroy_logs(&logs
);
1864 static void nilfs_set_next_segment(struct the_nilfs
*nilfs
,
1865 struct nilfs_segment_buffer
*segbuf
)
1867 nilfs
->ns_segnum
= segbuf
->sb_segnum
;
1868 nilfs
->ns_nextnum
= segbuf
->sb_nextnum
;
1869 nilfs
->ns_pseg_offset
= segbuf
->sb_pseg_start
- segbuf
->sb_fseg_start
1870 + segbuf
->sb_sum
.nblocks
;
1871 nilfs
->ns_seg_seq
= segbuf
->sb_sum
.seg_seq
;
1872 nilfs
->ns_ctime
= segbuf
->sb_sum
.ctime
;
1875 static void nilfs_segctor_complete_write(struct nilfs_sc_info
*sci
)
1877 struct nilfs_segment_buffer
*segbuf
;
1878 struct page
*bd_page
= NULL
, *fs_page
= NULL
;
1879 struct the_nilfs
*nilfs
= sci
->sc_sbi
->s_nilfs
;
1880 int update_sr
= false;
1882 list_for_each_entry(segbuf
, &sci
->sc_write_logs
, sb_list
) {
1883 struct buffer_head
*bh
;
1885 list_for_each_entry(bh
, &segbuf
->sb_segsum_buffers
,
1887 set_buffer_uptodate(bh
);
1888 clear_buffer_dirty(bh
);
1889 if (bh
->b_page
!= bd_page
) {
1891 end_page_writeback(bd_page
);
1892 bd_page
= bh
->b_page
;
1896 * We assume that the buffers which belong to the same page
1897 * continue over the buffer list.
1898 * Under this assumption, the last BHs of pages is
1899 * identifiable by the discontinuity of bh->b_page
1900 * (page != fs_page).
1902 * For B-tree node blocks, however, this assumption is not
1903 * guaranteed. The cleanup code of B-tree node pages needs
1906 list_for_each_entry(bh
, &segbuf
->sb_payload_buffers
,
1908 set_buffer_uptodate(bh
);
1909 clear_buffer_dirty(bh
);
1910 clear_buffer_nilfs_volatile(bh
);
1911 if (bh
== segbuf
->sb_super_root
) {
1912 if (bh
->b_page
!= bd_page
) {
1913 end_page_writeback(bd_page
);
1914 bd_page
= bh
->b_page
;
1919 if (bh
->b_page
!= fs_page
) {
1920 nilfs_end_page_io(fs_page
, 0);
1921 fs_page
= bh
->b_page
;
1925 if (!nilfs_segbuf_simplex(segbuf
)) {
1926 if (segbuf
->sb_sum
.flags
& NILFS_SS_LOGBGN
) {
1927 set_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
);
1928 sci
->sc_lseg_stime
= jiffies
;
1930 if (segbuf
->sb_sum
.flags
& NILFS_SS_LOGEND
)
1931 clear_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
);
1935 * Since pages may continue over multiple segment buffers,
1936 * end of the last page must be checked outside of the loop.
1939 end_page_writeback(bd_page
);
1941 nilfs_end_page_io(fs_page
, 0);
1943 nilfs_clear_copied_buffers(&sci
->sc_copied_buffers
, 0);
1945 nilfs_drop_collected_inodes(&sci
->sc_dirty_files
);
1947 if (nilfs_doing_gc()) {
1948 nilfs_drop_collected_inodes(&sci
->sc_gc_inodes
);
1950 nilfs_commit_gcdat_inode(nilfs
);
1952 nilfs
->ns_nongc_ctime
= sci
->sc_seg_ctime
;
1954 sci
->sc_nblk_inc
+= sci
->sc_nblk_this_inc
;
1956 segbuf
= NILFS_LAST_SEGBUF(&sci
->sc_write_logs
);
1957 nilfs_set_next_segment(nilfs
, segbuf
);
1960 nilfs_set_last_segment(nilfs
, segbuf
->sb_pseg_start
,
1961 segbuf
->sb_sum
.seg_seq
, nilfs
->ns_cno
++);
1963 clear_bit(NILFS_SC_HAVE_DELTA
, &sci
->sc_flags
);
1964 clear_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
1965 set_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
);
1966 nilfs_segctor_clear_metadata_dirty(sci
);
1968 clear_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
);
1971 static int nilfs_segctor_wait(struct nilfs_sc_info
*sci
)
1975 ret
= nilfs_wait_on_logs(&sci
->sc_write_logs
);
1977 nilfs_segctor_complete_write(sci
);
1978 nilfs_destroy_logs(&sci
->sc_write_logs
);
1983 static int nilfs_segctor_check_in_files(struct nilfs_sc_info
*sci
,
1984 struct nilfs_sb_info
*sbi
)
1986 struct nilfs_inode_info
*ii
, *n
;
1988 spin_lock(&sbi
->s_inode_lock
);
1990 list_for_each_entry_safe(ii
, n
, &sbi
->s_dirty_files
, i_dirty
) {
1992 struct buffer_head
*ibh
;
1995 spin_unlock(&sbi
->s_inode_lock
);
1996 err
= nilfs_ifile_get_inode_block(
1997 sbi
->s_ifile
, ii
->vfs_inode
.i_ino
, &ibh
);
1998 if (unlikely(err
)) {
1999 nilfs_warning(sbi
->s_super
, __func__
,
2000 "failed to get inode block.\n");
2003 nilfs_mdt_mark_buffer_dirty(ibh
);
2004 nilfs_mdt_mark_dirty(sbi
->s_ifile
);
2005 spin_lock(&sbi
->s_inode_lock
);
2006 if (likely(!ii
->i_bh
))
2013 clear_bit(NILFS_I_QUEUED
, &ii
->i_state
);
2014 set_bit(NILFS_I_BUSY
, &ii
->i_state
);
2015 list_del(&ii
->i_dirty
);
2016 list_add_tail(&ii
->i_dirty
, &sci
->sc_dirty_files
);
2018 spin_unlock(&sbi
->s_inode_lock
);
2023 static void nilfs_segctor_check_out_files(struct nilfs_sc_info
*sci
,
2024 struct nilfs_sb_info
*sbi
)
2026 struct nilfs_transaction_info
*ti
= current
->journal_info
;
2027 struct nilfs_inode_info
*ii
, *n
;
2029 spin_lock(&sbi
->s_inode_lock
);
2030 list_for_each_entry_safe(ii
, n
, &sci
->sc_dirty_files
, i_dirty
) {
2031 if (!test_and_clear_bit(NILFS_I_UPDATED
, &ii
->i_state
) ||
2032 test_bit(NILFS_I_DIRTY
, &ii
->i_state
))
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
;
2055 sci
->sc_cno
= nilfs
->ns_cno
;
2057 err
= nilfs_segctor_check_in_files(sci
, sbi
);
2061 if (nilfs_test_metadata_dirty(sbi
))
2062 set_bit(NILFS_SC_DIRTY
, &sci
->sc_flags
);
2064 if (nilfs_segctor_clean(sci
))
2068 sci
->sc_stage
.flags
&= ~NILFS_CF_HISTORY_MASK
;
2070 err
= nilfs_segctor_begin_construction(sci
, nilfs
);
2074 /* Update time stamp */
2075 sci
->sc_seg_ctime
= get_seconds();
2077 err
= nilfs_segctor_collect(sci
, nilfs
, mode
);
2081 /* Avoid empty segment */
2082 if (sci
->sc_stage
.scnt
== NILFS_ST_DONE
&&
2083 nilfs_segbuf_empty(sci
->sc_curseg
)) {
2084 nilfs_segctor_abort_construction(sci
, nilfs
, 1);
2088 err
= nilfs_segctor_assign(sci
, mode
);
2092 if (sci
->sc_stage
.flags
& NILFS_CF_IFILE_STARTED
)
2093 nilfs_segctor_fill_in_file_bmap(sci
, sbi
->s_ifile
);
2095 if (mode
== SC_LSEG_SR
&&
2096 sci
->sc_stage
.scnt
>= NILFS_ST_CPFILE
) {
2097 err
= nilfs_segctor_fill_in_checkpoint(sci
);
2099 goto failed_to_write
;
2101 nilfs_segctor_fill_in_super_root(sci
, nilfs
);
2103 nilfs_segctor_update_segusage(sci
, nilfs
->ns_sufile
);
2105 /* Write partial segments */
2106 err
= nilfs_segctor_prepare_write(sci
, &failed_page
);
2108 nilfs_abort_logs(&sci
->sc_segbufs
, failed_page
, err
);
2109 goto failed_to_write
;
2112 nilfs_add_checksums_on_logs(&sci
->sc_segbufs
,
2113 nilfs
->ns_crc_seed
);
2115 err
= nilfs_segctor_write(sci
, nilfs
);
2117 goto failed_to_write
;
2119 if (sci
->sc_stage
.scnt
== NILFS_ST_DONE
||
2120 nilfs
->ns_blocksize_bits
!= PAGE_CACHE_SHIFT
) {
2122 * At this point, we avoid double buffering
2123 * for blocksize < pagesize because page dirty
2124 * flag is turned off during write and dirty
2125 * buffers are not properly collected for
2126 * pages crossing over segments.
2128 err
= nilfs_segctor_wait(sci
);
2130 goto failed_to_write
;
2132 } while (sci
->sc_stage
.scnt
!= NILFS_ST_DONE
);
2135 nilfs_segctor_check_out_files(sci
, sbi
);
2139 if (sci
->sc_stage
.flags
& NILFS_CF_IFILE_STARTED
)
2140 nilfs_redirty_inodes(&sci
->sc_dirty_files
);
2143 if (nilfs_doing_gc())
2144 nilfs_redirty_inodes(&sci
->sc_gc_inodes
);
2145 nilfs_segctor_abort_construction(sci
, nilfs
, err
);
2150 * nilfs_segctor_start_timer - set timer of background write
2151 * @sci: nilfs_sc_info
2153 * If the timer has already been set, it ignores the new request.
2154 * This function MUST be called within a section locking the segment
2157 static void nilfs_segctor_start_timer(struct nilfs_sc_info
*sci
)
2159 spin_lock(&sci
->sc_state_lock
);
2160 if (!(sci
->sc_state
& NILFS_SEGCTOR_COMMIT
)) {
2161 sci
->sc_timer
.expires
= jiffies
+ sci
->sc_interval
;
2162 add_timer(&sci
->sc_timer
);
2163 sci
->sc_state
|= NILFS_SEGCTOR_COMMIT
;
2165 spin_unlock(&sci
->sc_state_lock
);
2168 static void nilfs_segctor_do_flush(struct nilfs_sc_info
*sci
, int bn
)
2170 spin_lock(&sci
->sc_state_lock
);
2171 if (!(sci
->sc_flush_request
& (1 << bn
))) {
2172 unsigned long prev_req
= sci
->sc_flush_request
;
2174 sci
->sc_flush_request
|= (1 << bn
);
2176 wake_up(&sci
->sc_wait_daemon
);
2178 spin_unlock(&sci
->sc_state_lock
);
2182 * nilfs_flush_segment - trigger a segment construction for resource control
2184 * @ino: inode number of the file to be flushed out.
2186 void nilfs_flush_segment(struct super_block
*sb
, ino_t ino
)
2188 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2189 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2191 if (!sci
|| nilfs_doing_construction())
2193 nilfs_segctor_do_flush(sci
, NILFS_MDT_INODE(sb
, ino
) ? ino
: 0);
2194 /* assign bit 0 to data files */
2197 struct nilfs_segctor_wait_request
{
2204 static int nilfs_segctor_sync(struct nilfs_sc_info
*sci
)
2206 struct nilfs_segctor_wait_request wait_req
;
2209 spin_lock(&sci
->sc_state_lock
);
2210 init_wait(&wait_req
.wq
);
2212 atomic_set(&wait_req
.done
, 0);
2213 wait_req
.seq
= ++sci
->sc_seq_request
;
2214 spin_unlock(&sci
->sc_state_lock
);
2216 init_waitqueue_entry(&wait_req
.wq
, current
);
2217 add_wait_queue(&sci
->sc_wait_request
, &wait_req
.wq
);
2218 set_current_state(TASK_INTERRUPTIBLE
);
2219 wake_up(&sci
->sc_wait_daemon
);
2222 if (atomic_read(&wait_req
.done
)) {
2226 if (!signal_pending(current
)) {
2233 finish_wait(&sci
->sc_wait_request
, &wait_req
.wq
);
2237 static void nilfs_segctor_wakeup(struct nilfs_sc_info
*sci
, int err
)
2239 struct nilfs_segctor_wait_request
*wrq
, *n
;
2240 unsigned long flags
;
2242 spin_lock_irqsave(&sci
->sc_wait_request
.lock
, flags
);
2243 list_for_each_entry_safe(wrq
, n
, &sci
->sc_wait_request
.task_list
,
2245 if (!atomic_read(&wrq
->done
) &&
2246 nilfs_cnt32_ge(sci
->sc_seq_done
, wrq
->seq
)) {
2248 atomic_set(&wrq
->done
, 1);
2250 if (atomic_read(&wrq
->done
)) {
2251 wrq
->wq
.func(&wrq
->wq
,
2252 TASK_UNINTERRUPTIBLE
| TASK_INTERRUPTIBLE
,
2256 spin_unlock_irqrestore(&sci
->sc_wait_request
.lock
, flags
);
2260 * nilfs_construct_segment - construct a logical segment
2263 * Return Value: On success, 0 is retured. On errors, one of the following
2264 * negative error code is returned.
2266 * %-EROFS - Read only filesystem.
2270 * %-ENOSPC - No space left on device (only in a panic state).
2272 * %-ERESTARTSYS - Interrupted.
2274 * %-ENOMEM - Insufficient memory available.
2276 int nilfs_construct_segment(struct super_block
*sb
)
2278 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2279 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2280 struct nilfs_transaction_info
*ti
;
2286 /* A call inside transactions causes a deadlock. */
2287 BUG_ON((ti
= current
->journal_info
) && ti
->ti_magic
== NILFS_TI_MAGIC
);
2289 err
= nilfs_segctor_sync(sci
);
2294 * nilfs_construct_dsync_segment - construct a data-only logical segment
2296 * @inode: inode whose data blocks should be written out
2297 * @start: start byte offset
2298 * @end: end byte offset (inclusive)
2300 * Return Value: On success, 0 is retured. On errors, one of the following
2301 * negative error code is returned.
2303 * %-EROFS - Read only filesystem.
2307 * %-ENOSPC - No space left on device (only in a panic state).
2309 * %-ERESTARTSYS - Interrupted.
2311 * %-ENOMEM - Insufficient memory available.
2313 int nilfs_construct_dsync_segment(struct super_block
*sb
, struct inode
*inode
,
2314 loff_t start
, loff_t end
)
2316 struct nilfs_sb_info
*sbi
= NILFS_SB(sb
);
2317 struct nilfs_sc_info
*sci
= NILFS_SC(sbi
);
2318 struct nilfs_inode_info
*ii
;
2319 struct nilfs_transaction_info ti
;
2325 nilfs_transaction_lock(sbi
, &ti
, 0);
2327 ii
= NILFS_I(inode
);
2328 if (test_bit(NILFS_I_INODE_DIRTY
, &ii
->i_state
) ||
2329 nilfs_test_opt(sbi
, STRICT_ORDER
) ||
2330 test_bit(NILFS_SC_UNCLOSED
, &sci
->sc_flags
) ||
2331 nilfs_discontinued(sbi
->s_nilfs
)) {
2332 nilfs_transaction_unlock(sbi
);
2333 err
= nilfs_segctor_sync(sci
);
2337 spin_lock(&sbi
->s_inode_lock
);
2338 if (!test_bit(NILFS_I_QUEUED
, &ii
->i_state
) &&
2339 !test_bit(NILFS_I_BUSY
, &ii
->i_state
)) {
2340 spin_unlock(&sbi
->s_inode_lock
);
2341 nilfs_transaction_unlock(sbi
);
2344 spin_unlock(&sbi
->s_inode_lock
);
2345 sci
->sc_dsync_inode
= ii
;
2346 sci
->sc_dsync_start
= start
;
2347 sci
->sc_dsync_end
= end
;
2349 err
= nilfs_segctor_do_construct(sci
, SC_LSEG_DSYNC
);
2351 nilfs_transaction_unlock(sbi
);
2355 #define FLUSH_FILE_BIT (0x1) /* data file only */
2356 #define FLUSH_DAT_BIT (1 << NILFS_DAT_INO) /* DAT only */
2359 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2360 * @sci: segment constructor object
2362 static void nilfs_segctor_accept(struct nilfs_sc_info
*sci
)
2364 spin_lock(&sci
->sc_state_lock
);
2365 sci
->sc_seq_accepted
= sci
->sc_seq_request
;
2366 spin_unlock(&sci
->sc_state_lock
);
2367 del_timer_sync(&sci
->sc_timer
);
2371 * nilfs_segctor_notify - notify the result of request to caller threads
2372 * @sci: segment constructor object
2373 * @mode: mode of log forming
2374 * @err: error code to be notified
2376 static void nilfs_segctor_notify(struct nilfs_sc_info
*sci
, int mode
, int err
)
2378 /* Clear requests (even when the construction failed) */
2379 spin_lock(&sci
->sc_state_lock
);
2381 if (mode
== SC_LSEG_SR
) {
2382 sci
->sc_state
&= ~NILFS_SEGCTOR_COMMIT
;
2383 sci
->sc_seq_done
= sci
->sc_seq_accepted
;
2384 nilfs_segctor_wakeup(sci
, err
);
2385 sci
->sc_flush_request
= 0;
2387 if (mode
== SC_FLUSH_FILE
)
2388 sci
->sc_flush_request
&= ~FLUSH_FILE_BIT
;
2389 else if (mode
== SC_FLUSH_DAT
)
2390 sci
->sc_flush_request
&= ~FLUSH_DAT_BIT
;
2392 /* re-enable timer if checkpoint creation was not done */
2393 if ((sci
->sc_state
& NILFS_SEGCTOR_COMMIT
) &&
2394 time_before(jiffies
, sci
->sc_timer
.expires
))
2395 add_timer(&sci
->sc_timer
);
2397 spin_unlock(&sci
->sc_state_lock
);
2401 * nilfs_segctor_construct - form logs and write them to disk
2402 * @sci: segment constructor object
2403 * @mode: mode of log forming
2405 static int nilfs_segctor_construct(struct nilfs_sc_info
*sci
, int mode
)
2407 struct nilfs_sb_info
*sbi
= sci
->sc_sbi
;
2408 struct the_nilfs
*nilfs
= sbi
->s_nilfs
;
2409 struct nilfs_super_block
**sbp
;
2412 nilfs_segctor_accept(sci
);
2414 if (nilfs_discontinued(nilfs
))
2416 if (!nilfs_segctor_confirm(sci
))
2417 err
= nilfs_segctor_do_construct(sci
, mode
);
2420 if (mode
!= SC_FLUSH_DAT
)
2421 atomic_set(&nilfs
->ns_ndirtyblks
, 0);
2422 if (test_bit(NILFS_SC_SUPER_ROOT
, &sci
->sc_flags
) &&
2423 nilfs_discontinued(nilfs
)) {
2424 down_write(&nilfs
->ns_sem
);
2426 sbp
= nilfs_prepare_super(sbi
,
2427 nilfs_sb_will_flip(nilfs
));
2429 nilfs_set_log_cursor(sbp
[0], nilfs
);
2430 err
= nilfs_commit_super(sbi
, NILFS_SB_COMMIT
);
2432 up_write(&nilfs
->ns_sem
);
2436 nilfs_segctor_notify(sci
, mode
, err
);
2440 static void nilfs_construction_timeout(unsigned long data
)
2442 struct task_struct
*p
= (struct task_struct
*)data
;
2447 nilfs_remove_written_gcinodes(struct the_nilfs
*nilfs
, struct list_head
*head
)
2449 struct nilfs_inode_info
*ii
, *n
;
2451 list_for_each_entry_safe(ii
, n
, head
, i_dirty
) {
2452 if (!test_bit(NILFS_I_UPDATED
, &ii
->i_state
))
2454 list_del_init(&ii
->i_dirty
);
2455 iput(&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
);