1 // SPDX-License-Identifier: GPL-2.0+
3 * NILFS segment constructor.
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
7 * Written by Ryusuke Konishi.
11 #include <linux/pagemap.h>
12 #include <linux/buffer_head.h>
13 #include <linux/writeback.h>
14 #include <linux/bitops.h>
15 #include <linux/bio.h>
16 #include <linux/completion.h>
17 #include <linux/blkdev.h>
18 #include <linux/backing-dev.h>
19 #include <linux/freezer.h>
20 #include <linux/kthread.h>
21 #include <linux/crc32.h>
22 #include <linux/pagevec.h>
23 #include <linux/slab.h>
24 #include <linux/sched/signal.h>
39 #define SC_N_INODEVEC 16 /* Size of locally allocated inode vector */
41 #define SC_MAX_SEGDELTA 64 /*
42 * Upper limit of the number of segments
43 * appended in collection retry loop
46 /* Construction mode */
48 SC_LSEG_SR = 1, /* Make a logical segment having a super root */
50 * Flush data blocks of a given file and make
51 * a logical segment without a super root.
54 * Flush data files, leads to segment writes without
55 * creating a checkpoint.
58 * Flush DAT file. This also creates segments
59 * without a checkpoint.
63 /* Stage numbers of dirty block collection */
66 NILFS_ST_GC, /* Collecting dirty blocks for GC */
72 NILFS_ST_SR, /* Super root */
73 NILFS_ST_DSYNC, /* Data sync blocks */
77 #define CREATE_TRACE_POINTS
78 #include <trace/events/nilfs2.h>
81 * nilfs_sc_cstage_inc(), nilfs_sc_cstage_set(), nilfs_sc_cstage_get() are
82 * wrapper functions of stage count (nilfs_sc_info->sc_stage.scnt). Users of
83 * the variable must use them because transition of stage count must involve
84 * trace events (trace_nilfs2_collection_stage_transition).
86 * nilfs_sc_cstage_get() isn't required for the above purpose because it doesn't
87 * produce tracepoint events. It is provided just for making the intention
90 static inline void nilfs_sc_cstage_inc(struct nilfs_sc_info *sci)
93 trace_nilfs2_collection_stage_transition(sci);
96 static inline void nilfs_sc_cstage_set(struct nilfs_sc_info *sci, int next_scnt)
98 sci->sc_stage.scnt = next_scnt;
99 trace_nilfs2_collection_stage_transition(sci);
102 static inline int nilfs_sc_cstage_get(struct nilfs_sc_info *sci)
104 return sci->sc_stage.scnt;
107 /* State flags of collection */
108 #define NILFS_CF_NODE 0x0001 /* Collecting node blocks */
109 #define NILFS_CF_IFILE_STARTED 0x0002 /* IFILE stage has started */
110 #define NILFS_CF_SUFREED 0x0004 /* segment usages has been freed */
111 #define NILFS_CF_HISTORY_MASK (NILFS_CF_IFILE_STARTED | NILFS_CF_SUFREED)
113 /* Operations depending on the construction mode and file type */
114 struct nilfs_sc_operations {
115 int (*collect_data)(struct nilfs_sc_info *, struct buffer_head *,
117 int (*collect_node)(struct nilfs_sc_info *, struct buffer_head *,
119 int (*collect_bmap)(struct nilfs_sc_info *, struct buffer_head *,
121 void (*write_data_binfo)(struct nilfs_sc_info *,
122 struct nilfs_segsum_pointer *,
123 union nilfs_binfo *);
124 void (*write_node_binfo)(struct nilfs_sc_info *,
125 struct nilfs_segsum_pointer *,
126 union nilfs_binfo *);
132 static void nilfs_segctor_start_timer(struct nilfs_sc_info *);
133 static void nilfs_segctor_do_flush(struct nilfs_sc_info *, int);
134 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *);
135 static void nilfs_dispose_list(struct the_nilfs *, struct list_head *, int);
137 #define nilfs_cnt32_ge(a, b) \
138 (typecheck(__u32, a) && typecheck(__u32, b) && \
139 ((__s32)((a) - (b)) >= 0))
141 static int nilfs_prepare_segment_lock(struct super_block *sb,
142 struct nilfs_transaction_info *ti)
144 struct nilfs_transaction_info *cur_ti = current->journal_info;
148 if (cur_ti->ti_magic == NILFS_TI_MAGIC)
149 return ++cur_ti->ti_count;
152 * If journal_info field is occupied by other FS,
153 * it is saved and will be restored on
154 * nilfs_transaction_commit().
156 nilfs_warn(sb, "journal info from a different FS");
157 save = current->journal_info;
160 ti = kmem_cache_alloc(nilfs_transaction_cachep, GFP_NOFS);
163 ti->ti_flags = NILFS_TI_DYNAMIC_ALLOC;
169 ti->ti_magic = NILFS_TI_MAGIC;
170 current->journal_info = ti;
175 * nilfs_transaction_begin - start indivisible file operations.
177 * @ti: nilfs_transaction_info
178 * @vacancy_check: flags for vacancy rate checks
180 * nilfs_transaction_begin() acquires a reader/writer semaphore, called
181 * the segment semaphore, to make a segment construction and write tasks
182 * exclusive. The function is used with nilfs_transaction_commit() in pairs.
183 * The region enclosed by these two functions can be nested. To avoid a
184 * deadlock, the semaphore is only acquired or released in the outermost call.
186 * This function allocates a nilfs_transaction_info struct to keep context
187 * information on it. It is initialized and hooked onto the current task in
188 * the outermost call. If a pre-allocated struct is given to @ti, it is used
189 * instead; otherwise a new struct is assigned from a slab.
191 * When @vacancy_check flag is set, this function will check the amount of
192 * free space, and will wait for the GC to reclaim disk space if low capacity.
194 * Return: 0 on success, or one of the following negative error codes on
196 * * %-ENOMEM - Insufficient memory available.
197 * * %-ENOSPC - No space left on device (if checking free space).
199 int nilfs_transaction_begin(struct super_block *sb,
200 struct nilfs_transaction_info *ti,
203 struct the_nilfs *nilfs;
204 int ret = nilfs_prepare_segment_lock(sb, ti);
205 struct nilfs_transaction_info *trace_ti;
207 if (unlikely(ret < 0))
210 trace_ti = current->journal_info;
212 trace_nilfs2_transaction_transition(sb, trace_ti,
213 trace_ti->ti_count, trace_ti->ti_flags,
214 TRACE_NILFS2_TRANSACTION_BEGIN);
218 sb_start_intwrite(sb);
220 nilfs = sb->s_fs_info;
221 down_read(&nilfs->ns_segctor_sem);
222 if (vacancy_check && nilfs_near_disk_full(nilfs)) {
223 up_read(&nilfs->ns_segctor_sem);
228 trace_ti = current->journal_info;
229 trace_nilfs2_transaction_transition(sb, trace_ti, trace_ti->ti_count,
231 TRACE_NILFS2_TRANSACTION_BEGIN);
235 ti = current->journal_info;
236 current->journal_info = ti->ti_save;
237 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
238 kmem_cache_free(nilfs_transaction_cachep, ti);
244 * nilfs_transaction_commit - commit indivisible file operations.
247 * nilfs_transaction_commit() releases the read semaphore which is
248 * acquired by nilfs_transaction_begin(). This is only performed
249 * in outermost call of this function. If a commit flag is set,
250 * nilfs_transaction_commit() sets a timer to start the segment
251 * constructor. If a sync flag is set, it starts construction
254 * Return: 0 on success, or a negative error code on failure.
256 int nilfs_transaction_commit(struct super_block *sb)
258 struct nilfs_transaction_info *ti = current->journal_info;
259 struct the_nilfs *nilfs = sb->s_fs_info;
262 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
263 ti->ti_flags |= NILFS_TI_COMMIT;
264 if (ti->ti_count > 0) {
266 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
267 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
270 if (nilfs->ns_writer) {
271 struct nilfs_sc_info *sci = nilfs->ns_writer;
273 if (ti->ti_flags & NILFS_TI_COMMIT)
274 nilfs_segctor_start_timer(sci);
275 if (atomic_read(&nilfs->ns_ndirtyblks) > sci->sc_watermark)
276 nilfs_segctor_do_flush(sci, 0);
278 up_read(&nilfs->ns_segctor_sem);
279 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
280 ti->ti_flags, TRACE_NILFS2_TRANSACTION_COMMIT);
282 current->journal_info = ti->ti_save;
284 if (ti->ti_flags & NILFS_TI_SYNC)
285 err = nilfs_construct_segment(sb);
286 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
287 kmem_cache_free(nilfs_transaction_cachep, ti);
292 void nilfs_transaction_abort(struct super_block *sb)
294 struct nilfs_transaction_info *ti = current->journal_info;
295 struct the_nilfs *nilfs = sb->s_fs_info;
297 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
298 if (ti->ti_count > 0) {
300 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
301 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
304 up_read(&nilfs->ns_segctor_sem);
306 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
307 ti->ti_flags, TRACE_NILFS2_TRANSACTION_ABORT);
309 current->journal_info = ti->ti_save;
310 if (ti->ti_flags & NILFS_TI_DYNAMIC_ALLOC)
311 kmem_cache_free(nilfs_transaction_cachep, ti);
315 void nilfs_relax_pressure_in_lock(struct super_block *sb)
317 struct the_nilfs *nilfs = sb->s_fs_info;
318 struct nilfs_sc_info *sci = nilfs->ns_writer;
320 if (sb_rdonly(sb) || unlikely(!sci) || !sci->sc_flush_request)
323 set_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
324 up_read(&nilfs->ns_segctor_sem);
326 down_write(&nilfs->ns_segctor_sem);
327 if (sci->sc_flush_request &&
328 test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags)) {
329 struct nilfs_transaction_info *ti = current->journal_info;
331 ti->ti_flags |= NILFS_TI_WRITER;
332 nilfs_segctor_do_immediate_flush(sci);
333 ti->ti_flags &= ~NILFS_TI_WRITER;
335 downgrade_write(&nilfs->ns_segctor_sem);
338 static void nilfs_transaction_lock(struct super_block *sb,
339 struct nilfs_transaction_info *ti,
342 struct nilfs_transaction_info *cur_ti = current->journal_info;
343 struct the_nilfs *nilfs = sb->s_fs_info;
344 struct nilfs_sc_info *sci = nilfs->ns_writer;
347 ti->ti_flags = NILFS_TI_WRITER;
349 ti->ti_save = cur_ti;
350 ti->ti_magic = NILFS_TI_MAGIC;
351 current->journal_info = ti;
354 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
355 ti->ti_flags, TRACE_NILFS2_TRANSACTION_TRYLOCK);
357 down_write(&nilfs->ns_segctor_sem);
358 if (!test_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags))
361 nilfs_segctor_do_immediate_flush(sci);
363 up_write(&nilfs->ns_segctor_sem);
367 ti->ti_flags |= NILFS_TI_GC;
369 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
370 ti->ti_flags, TRACE_NILFS2_TRANSACTION_LOCK);
373 static void nilfs_transaction_unlock(struct super_block *sb)
375 struct nilfs_transaction_info *ti = current->journal_info;
376 struct the_nilfs *nilfs = sb->s_fs_info;
378 BUG_ON(ti == NULL || ti->ti_magic != NILFS_TI_MAGIC);
379 BUG_ON(ti->ti_count > 0);
381 up_write(&nilfs->ns_segctor_sem);
382 current->journal_info = ti->ti_save;
384 trace_nilfs2_transaction_transition(sb, ti, ti->ti_count,
385 ti->ti_flags, TRACE_NILFS2_TRANSACTION_UNLOCK);
388 static void *nilfs_segctor_map_segsum_entry(struct nilfs_sc_info *sci,
389 struct nilfs_segsum_pointer *ssp,
392 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
393 unsigned int blocksize = sci->sc_super->s_blocksize;
396 if (unlikely(ssp->offset + bytes > blocksize)) {
398 BUG_ON(NILFS_SEGBUF_BH_IS_LAST(ssp->bh,
399 &segbuf->sb_segsum_buffers));
400 ssp->bh = NILFS_SEGBUF_NEXT_BH(ssp->bh);
402 p = ssp->bh->b_data + ssp->offset;
403 ssp->offset += bytes;
408 * nilfs_segctor_reset_segment_buffer - reset the current segment buffer
409 * @sci: nilfs_sc_info
411 * Return: 0 on success, or a negative error code on failure.
413 static int nilfs_segctor_reset_segment_buffer(struct nilfs_sc_info *sci)
415 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
416 struct buffer_head *sumbh;
417 unsigned int sumbytes;
418 unsigned int flags = 0;
421 if (nilfs_doing_gc())
423 err = nilfs_segbuf_reset(segbuf, flags, sci->sc_seg_ctime, sci->sc_cno);
427 sumbh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
428 sumbytes = segbuf->sb_sum.sumbytes;
429 sci->sc_finfo_ptr.bh = sumbh; sci->sc_finfo_ptr.offset = sumbytes;
430 sci->sc_binfo_ptr.bh = sumbh; sci->sc_binfo_ptr.offset = sumbytes;
431 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
436 * nilfs_segctor_zeropad_segsum - zero pad the rest of the segment summary area
437 * @sci: segment constructor object
439 * nilfs_segctor_zeropad_segsum() zero-fills unallocated space at the end of
440 * the current segment summary block.
442 static void nilfs_segctor_zeropad_segsum(struct nilfs_sc_info *sci)
444 struct nilfs_segsum_pointer *ssp;
446 ssp = sci->sc_blk_cnt > 0 ? &sci->sc_binfo_ptr : &sci->sc_finfo_ptr;
447 if (ssp->offset < ssp->bh->b_size)
448 memset(ssp->bh->b_data + ssp->offset, 0,
449 ssp->bh->b_size - ssp->offset);
452 static int nilfs_segctor_feed_segment(struct nilfs_sc_info *sci)
454 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
455 if (NILFS_SEGBUF_IS_LAST(sci->sc_curseg, &sci->sc_segbufs))
457 * The current segment is filled up
460 nilfs_segctor_zeropad_segsum(sci);
461 sci->sc_curseg = NILFS_NEXT_SEGBUF(sci->sc_curseg);
462 return nilfs_segctor_reset_segment_buffer(sci);
465 static int nilfs_segctor_add_super_root(struct nilfs_sc_info *sci)
467 struct nilfs_segment_buffer *segbuf = sci->sc_curseg;
470 if (segbuf->sb_sum.nblocks >= segbuf->sb_rest_blocks) {
471 err = nilfs_segctor_feed_segment(sci);
474 segbuf = sci->sc_curseg;
476 err = nilfs_segbuf_extend_payload(segbuf, &segbuf->sb_super_root);
478 segbuf->sb_sum.flags |= NILFS_SS_SR;
483 * Functions for making segment summary and payloads
485 static int nilfs_segctor_segsum_block_required(
486 struct nilfs_sc_info *sci, const struct nilfs_segsum_pointer *ssp,
487 unsigned int binfo_size)
489 unsigned int blocksize = sci->sc_super->s_blocksize;
490 /* Size of finfo and binfo is enough small against blocksize */
492 return ssp->offset + binfo_size +
493 (!sci->sc_blk_cnt ? sizeof(struct nilfs_finfo) : 0) >
497 static void nilfs_segctor_begin_finfo(struct nilfs_sc_info *sci,
500 sci->sc_curseg->sb_sum.nfinfo++;
501 sci->sc_binfo_ptr = sci->sc_finfo_ptr;
502 nilfs_segctor_map_segsum_entry(
503 sci, &sci->sc_binfo_ptr, sizeof(struct nilfs_finfo));
505 if (NILFS_I(inode)->i_root &&
506 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags))
507 set_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
511 static void nilfs_segctor_end_finfo(struct nilfs_sc_info *sci,
514 struct nilfs_finfo *finfo;
515 struct nilfs_inode_info *ii;
516 struct nilfs_segment_buffer *segbuf;
519 if (sci->sc_blk_cnt == 0)
524 if (ii->i_type & NILFS_I_TYPE_GC)
526 else if (NILFS_ROOT_METADATA_FILE(inode->i_ino))
531 finfo = nilfs_segctor_map_segsum_entry(sci, &sci->sc_finfo_ptr,
533 finfo->fi_ino = cpu_to_le64(inode->i_ino);
534 finfo->fi_nblocks = cpu_to_le32(sci->sc_blk_cnt);
535 finfo->fi_ndatablk = cpu_to_le32(sci->sc_datablk_cnt);
536 finfo->fi_cno = cpu_to_le64(cno);
538 segbuf = sci->sc_curseg;
539 segbuf->sb_sum.sumbytes = sci->sc_binfo_ptr.offset +
540 sci->sc_super->s_blocksize * (segbuf->sb_sum.nsumblk - 1);
541 sci->sc_finfo_ptr = sci->sc_binfo_ptr;
542 sci->sc_blk_cnt = sci->sc_datablk_cnt = 0;
545 static int nilfs_segctor_add_file_block(struct nilfs_sc_info *sci,
546 struct buffer_head *bh,
548 unsigned int binfo_size)
550 struct nilfs_segment_buffer *segbuf;
551 int required, err = 0;
554 segbuf = sci->sc_curseg;
555 required = nilfs_segctor_segsum_block_required(
556 sci, &sci->sc_binfo_ptr, binfo_size);
557 if (segbuf->sb_sum.nblocks + required + 1 > segbuf->sb_rest_blocks) {
558 nilfs_segctor_end_finfo(sci, inode);
559 err = nilfs_segctor_feed_segment(sci);
564 if (unlikely(required)) {
565 nilfs_segctor_zeropad_segsum(sci);
566 err = nilfs_segbuf_extend_segsum(segbuf);
570 if (sci->sc_blk_cnt == 0)
571 nilfs_segctor_begin_finfo(sci, inode);
573 nilfs_segctor_map_segsum_entry(sci, &sci->sc_binfo_ptr, binfo_size);
574 /* Substitution to vblocknr is delayed until update_blocknr() */
575 nilfs_segbuf_add_file_buffer(segbuf, bh);
582 * Callback functions that enumerate, mark, and collect dirty blocks
584 static int nilfs_collect_file_data(struct nilfs_sc_info *sci,
585 struct buffer_head *bh, struct inode *inode)
589 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
593 err = nilfs_segctor_add_file_block(sci, bh, inode,
594 sizeof(struct nilfs_binfo_v));
596 sci->sc_datablk_cnt++;
600 static int nilfs_collect_file_node(struct nilfs_sc_info *sci,
601 struct buffer_head *bh,
604 return nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
607 static int nilfs_collect_file_bmap(struct nilfs_sc_info *sci,
608 struct buffer_head *bh,
611 WARN_ON(!buffer_dirty(bh));
612 return nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
615 static void nilfs_write_file_data_binfo(struct nilfs_sc_info *sci,
616 struct nilfs_segsum_pointer *ssp,
617 union nilfs_binfo *binfo)
619 struct nilfs_binfo_v *binfo_v = nilfs_segctor_map_segsum_entry(
620 sci, ssp, sizeof(*binfo_v));
621 *binfo_v = binfo->bi_v;
624 static void nilfs_write_file_node_binfo(struct nilfs_sc_info *sci,
625 struct nilfs_segsum_pointer *ssp,
626 union nilfs_binfo *binfo)
628 __le64 *vblocknr = nilfs_segctor_map_segsum_entry(
629 sci, ssp, sizeof(*vblocknr));
630 *vblocknr = binfo->bi_v.bi_vblocknr;
633 static const struct nilfs_sc_operations nilfs_sc_file_ops = {
634 .collect_data = nilfs_collect_file_data,
635 .collect_node = nilfs_collect_file_node,
636 .collect_bmap = nilfs_collect_file_bmap,
637 .write_data_binfo = nilfs_write_file_data_binfo,
638 .write_node_binfo = nilfs_write_file_node_binfo,
641 static int nilfs_collect_dat_data(struct nilfs_sc_info *sci,
642 struct buffer_head *bh, struct inode *inode)
646 err = nilfs_bmap_propagate(NILFS_I(inode)->i_bmap, bh);
650 err = nilfs_segctor_add_file_block(sci, bh, inode, sizeof(__le64));
652 sci->sc_datablk_cnt++;
656 static int nilfs_collect_dat_bmap(struct nilfs_sc_info *sci,
657 struct buffer_head *bh, struct inode *inode)
659 WARN_ON(!buffer_dirty(bh));
660 return nilfs_segctor_add_file_block(sci, bh, inode,
661 sizeof(struct nilfs_binfo_dat));
664 static void nilfs_write_dat_data_binfo(struct nilfs_sc_info *sci,
665 struct nilfs_segsum_pointer *ssp,
666 union nilfs_binfo *binfo)
668 __le64 *blkoff = nilfs_segctor_map_segsum_entry(sci, ssp,
670 *blkoff = binfo->bi_dat.bi_blkoff;
673 static void nilfs_write_dat_node_binfo(struct nilfs_sc_info *sci,
674 struct nilfs_segsum_pointer *ssp,
675 union nilfs_binfo *binfo)
677 struct nilfs_binfo_dat *binfo_dat =
678 nilfs_segctor_map_segsum_entry(sci, ssp, sizeof(*binfo_dat));
679 *binfo_dat = binfo->bi_dat;
682 static const struct nilfs_sc_operations nilfs_sc_dat_ops = {
683 .collect_data = nilfs_collect_dat_data,
684 .collect_node = nilfs_collect_file_node,
685 .collect_bmap = nilfs_collect_dat_bmap,
686 .write_data_binfo = nilfs_write_dat_data_binfo,
687 .write_node_binfo = nilfs_write_dat_node_binfo,
690 static const struct nilfs_sc_operations nilfs_sc_dsync_ops = {
691 .collect_data = nilfs_collect_file_data,
692 .collect_node = NULL,
693 .collect_bmap = NULL,
694 .write_data_binfo = nilfs_write_file_data_binfo,
695 .write_node_binfo = NULL,
698 static size_t nilfs_lookup_dirty_data_buffers(struct inode *inode,
699 struct list_head *listp,
701 loff_t start, loff_t end)
703 struct address_space *mapping = inode->i_mapping;
704 struct folio_batch fbatch;
705 pgoff_t index = 0, last = ULONG_MAX;
709 if (unlikely(start != 0 || end != LLONG_MAX)) {
711 * A valid range is given for sync-ing data pages. The
712 * range is rounded to per-page; extra dirty buffers
713 * may be included if blocksize < pagesize.
715 index = start >> PAGE_SHIFT;
716 last = end >> PAGE_SHIFT;
718 folio_batch_init(&fbatch);
720 if (unlikely(index > last) ||
721 !filemap_get_folios_tag(mapping, &index, last,
722 PAGECACHE_TAG_DIRTY, &fbatch))
725 for (i = 0; i < folio_batch_count(&fbatch); i++) {
726 struct buffer_head *bh, *head;
727 struct folio *folio = fbatch.folios[i];
730 if (unlikely(folio->mapping != mapping)) {
731 /* Exclude folios removed from the address space */
735 head = folio_buffers(folio);
737 head = create_empty_buffers(folio,
738 i_blocksize(inode), 0);
742 if (!buffer_dirty(bh) || buffer_async_write(bh))
745 list_add_tail(&bh->b_assoc_buffers, listp);
747 if (unlikely(ndirties >= nlimit)) {
749 folio_batch_release(&fbatch);
753 } while (bh = bh->b_this_page, bh != head);
757 folio_batch_release(&fbatch);
762 static void nilfs_lookup_dirty_node_buffers(struct inode *inode,
763 struct list_head *listp)
765 struct nilfs_inode_info *ii = NILFS_I(inode);
766 struct inode *btnc_inode = ii->i_assoc_inode;
767 struct folio_batch fbatch;
768 struct buffer_head *bh, *head;
774 folio_batch_init(&fbatch);
776 while (filemap_get_folios_tag(btnc_inode->i_mapping, &index,
777 (pgoff_t)-1, PAGECACHE_TAG_DIRTY, &fbatch)) {
778 for (i = 0; i < folio_batch_count(&fbatch); i++) {
779 bh = head = folio_buffers(fbatch.folios[i]);
781 if (buffer_dirty(bh) &&
782 !buffer_async_write(bh)) {
784 list_add_tail(&bh->b_assoc_buffers,
787 bh = bh->b_this_page;
788 } while (bh != head);
790 folio_batch_release(&fbatch);
795 static void nilfs_dispose_list(struct the_nilfs *nilfs,
796 struct list_head *head, int force)
798 struct nilfs_inode_info *ii, *n;
799 struct nilfs_inode_info *ivec[SC_N_INODEVEC], **pii;
802 while (!list_empty(head)) {
803 spin_lock(&nilfs->ns_inode_lock);
804 list_for_each_entry_safe(ii, n, head, i_dirty) {
805 list_del_init(&ii->i_dirty);
807 if (unlikely(ii->i_bh)) {
811 } else if (test_bit(NILFS_I_DIRTY, &ii->i_state)) {
812 set_bit(NILFS_I_QUEUED, &ii->i_state);
813 list_add_tail(&ii->i_dirty,
814 &nilfs->ns_dirty_files);
818 if (nv == SC_N_INODEVEC)
821 spin_unlock(&nilfs->ns_inode_lock);
823 for (pii = ivec; nv > 0; pii++, nv--)
824 iput(&(*pii)->vfs_inode);
828 static void nilfs_iput_work_func(struct work_struct *work)
830 struct nilfs_sc_info *sci = container_of(work, struct nilfs_sc_info,
832 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
834 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 0);
837 static int nilfs_test_metadata_dirty(struct the_nilfs *nilfs,
838 struct nilfs_root *root)
842 if (nilfs_mdt_fetch_dirty(root->ifile))
844 if (nilfs_mdt_fetch_dirty(nilfs->ns_cpfile))
846 if (nilfs_mdt_fetch_dirty(nilfs->ns_sufile))
848 if ((ret || nilfs_doing_gc()) && nilfs_mdt_fetch_dirty(nilfs->ns_dat))
853 static int nilfs_segctor_clean(struct nilfs_sc_info *sci)
855 return list_empty(&sci->sc_dirty_files) &&
856 !test_bit(NILFS_SC_DIRTY, &sci->sc_flags) &&
857 sci->sc_nfreesegs == 0 &&
858 (!nilfs_doing_gc() || list_empty(&sci->sc_gc_inodes));
861 static int nilfs_segctor_confirm(struct nilfs_sc_info *sci)
863 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
866 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
867 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
869 spin_lock(&nilfs->ns_inode_lock);
870 if (list_empty(&nilfs->ns_dirty_files) && nilfs_segctor_clean(sci))
873 spin_unlock(&nilfs->ns_inode_lock);
877 static void nilfs_segctor_clear_metadata_dirty(struct nilfs_sc_info *sci)
879 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
881 nilfs_mdt_clear_dirty(sci->sc_root->ifile);
882 nilfs_mdt_clear_dirty(nilfs->ns_cpfile);
883 nilfs_mdt_clear_dirty(nilfs->ns_sufile);
884 nilfs_mdt_clear_dirty(nilfs->ns_dat);
887 static void nilfs_fill_in_file_bmap(struct inode *ifile,
888 struct nilfs_inode_info *ii)
891 struct buffer_head *ibh;
892 struct nilfs_inode *raw_inode;
894 if (test_bit(NILFS_I_BMAP, &ii->i_state)) {
897 raw_inode = nilfs_ifile_map_inode(ifile, ii->vfs_inode.i_ino,
899 nilfs_bmap_write(ii->i_bmap, raw_inode);
900 nilfs_ifile_unmap_inode(raw_inode);
904 static void nilfs_segctor_fill_in_file_bmap(struct nilfs_sc_info *sci)
906 struct nilfs_inode_info *ii;
908 list_for_each_entry(ii, &sci->sc_dirty_files, i_dirty) {
909 nilfs_fill_in_file_bmap(sci->sc_root->ifile, ii);
910 set_bit(NILFS_I_COLLECTED, &ii->i_state);
915 * nilfs_write_root_mdt_inode - export root metadata inode information to
917 * @inode: inode object of the root metadata file
918 * @raw_inode: on-disk inode
920 * nilfs_write_root_mdt_inode() writes inode information and bmap data of
921 * @inode to the inode area of the metadata file allocated on the super root
922 * block created to finalize the log. Since super root blocks are configured
923 * each time, this function zero-fills the unused area of @raw_inode.
925 static void nilfs_write_root_mdt_inode(struct inode *inode,
926 struct nilfs_inode *raw_inode)
928 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
930 nilfs_write_inode_common(inode, raw_inode);
932 /* zero-fill unused portion of raw_inode */
933 raw_inode->i_xattr = 0;
934 raw_inode->i_pad = 0;
935 memset((void *)raw_inode + sizeof(*raw_inode), 0,
936 nilfs->ns_inode_size - sizeof(*raw_inode));
938 nilfs_bmap_write(NILFS_I(inode)->i_bmap, raw_inode);
941 static void nilfs_segctor_fill_in_super_root(struct nilfs_sc_info *sci,
942 struct the_nilfs *nilfs)
944 struct buffer_head *bh_sr;
945 struct nilfs_super_root *raw_sr;
946 unsigned int isz, srsz;
948 bh_sr = NILFS_LAST_SEGBUF(&sci->sc_segbufs)->sb_super_root;
951 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
952 isz = nilfs->ns_inode_size;
953 srsz = NILFS_SR_BYTES(isz);
955 raw_sr->sr_sum = 0; /* Ensure initialization within this update */
956 raw_sr->sr_bytes = cpu_to_le16(srsz);
957 raw_sr->sr_nongc_ctime
958 = cpu_to_le64(nilfs_doing_gc() ?
959 nilfs->ns_nongc_ctime : sci->sc_seg_ctime);
960 raw_sr->sr_flags = 0;
962 nilfs_write_root_mdt_inode(nilfs->ns_dat, (void *)raw_sr +
963 NILFS_SR_DAT_OFFSET(isz));
964 nilfs_write_root_mdt_inode(nilfs->ns_cpfile, (void *)raw_sr +
965 NILFS_SR_CPFILE_OFFSET(isz));
966 nilfs_write_root_mdt_inode(nilfs->ns_sufile, (void *)raw_sr +
967 NILFS_SR_SUFILE_OFFSET(isz));
969 memset((void *)raw_sr + srsz, 0, nilfs->ns_blocksize - srsz);
970 set_buffer_uptodate(bh_sr);
971 unlock_buffer(bh_sr);
974 static void nilfs_redirty_inodes(struct list_head *head)
976 struct nilfs_inode_info *ii;
978 list_for_each_entry(ii, head, i_dirty) {
979 if (test_bit(NILFS_I_COLLECTED, &ii->i_state))
980 clear_bit(NILFS_I_COLLECTED, &ii->i_state);
984 static void nilfs_drop_collected_inodes(struct list_head *head)
986 struct nilfs_inode_info *ii;
988 list_for_each_entry(ii, head, i_dirty) {
989 if (!test_and_clear_bit(NILFS_I_COLLECTED, &ii->i_state))
992 clear_bit(NILFS_I_INODE_SYNC, &ii->i_state);
993 set_bit(NILFS_I_UPDATED, &ii->i_state);
997 static int nilfs_segctor_apply_buffers(struct nilfs_sc_info *sci,
999 struct list_head *listp,
1000 int (*collect)(struct nilfs_sc_info *,
1001 struct buffer_head *,
1004 struct buffer_head *bh, *n;
1008 list_for_each_entry_safe(bh, n, listp, b_assoc_buffers) {
1009 list_del_init(&bh->b_assoc_buffers);
1010 err = collect(sci, bh, inode);
1013 goto dispose_buffers;
1019 while (!list_empty(listp)) {
1020 bh = list_first_entry(listp, struct buffer_head,
1022 list_del_init(&bh->b_assoc_buffers);
1028 static size_t nilfs_segctor_buffer_rest(struct nilfs_sc_info *sci)
1030 /* Remaining number of blocks within segment buffer */
1031 return sci->sc_segbuf_nblocks -
1032 (sci->sc_nblk_this_inc + sci->sc_curseg->sb_sum.nblocks);
1035 static int nilfs_segctor_scan_file(struct nilfs_sc_info *sci,
1036 struct inode *inode,
1037 const struct nilfs_sc_operations *sc_ops)
1039 LIST_HEAD(data_buffers);
1040 LIST_HEAD(node_buffers);
1043 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1044 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1046 n = nilfs_lookup_dirty_data_buffers(
1047 inode, &data_buffers, rest + 1, 0, LLONG_MAX);
1049 err = nilfs_segctor_apply_buffers(
1050 sci, inode, &data_buffers,
1051 sc_ops->collect_data);
1052 BUG_ON(!err); /* always receive -E2BIG or true error */
1056 nilfs_lookup_dirty_node_buffers(inode, &node_buffers);
1058 if (!(sci->sc_stage.flags & NILFS_CF_NODE)) {
1059 err = nilfs_segctor_apply_buffers(
1060 sci, inode, &data_buffers, sc_ops->collect_data);
1061 if (unlikely(err)) {
1062 /* dispose node list */
1063 nilfs_segctor_apply_buffers(
1064 sci, inode, &node_buffers, NULL);
1067 sci->sc_stage.flags |= NILFS_CF_NODE;
1070 err = nilfs_segctor_apply_buffers(
1071 sci, inode, &node_buffers, sc_ops->collect_node);
1075 nilfs_bmap_lookup_dirty_buffers(NILFS_I(inode)->i_bmap, &node_buffers);
1076 err = nilfs_segctor_apply_buffers(
1077 sci, inode, &node_buffers, sc_ops->collect_bmap);
1081 nilfs_segctor_end_finfo(sci, inode);
1082 sci->sc_stage.flags &= ~NILFS_CF_NODE;
1088 static int nilfs_segctor_scan_file_dsync(struct nilfs_sc_info *sci,
1089 struct inode *inode)
1091 LIST_HEAD(data_buffers);
1092 size_t n, rest = nilfs_segctor_buffer_rest(sci);
1095 n = nilfs_lookup_dirty_data_buffers(inode, &data_buffers, rest + 1,
1096 sci->sc_dsync_start,
1099 err = nilfs_segctor_apply_buffers(sci, inode, &data_buffers,
1100 nilfs_collect_file_data);
1102 nilfs_segctor_end_finfo(sci, inode);
1104 /* always receive -E2BIG or true error if n > rest */
1110 * nilfs_free_segments - free the segments given by an array of segment numbers
1111 * @nilfs: nilfs object
1112 * @segnumv: array of segment numbers to be freed
1113 * @nsegs: number of segments to be freed in @segnumv
1115 * nilfs_free_segments() wraps nilfs_sufile_freev() and
1116 * nilfs_sufile_cancel_freev(), and edits the segment usage metadata file
1117 * (sufile) to free all segments given by @segnumv and @nsegs at once. If
1118 * it fails midway, it cancels the changes so that none of the segments are
1119 * freed. If @nsegs is 0, this function does nothing.
1121 * The freeing of segments is not finalized until the writing of a log with
1122 * a super root block containing this sufile change is complete, and it can
1123 * be canceled with nilfs_sufile_cancel_freev() until then.
1125 * Return: 0 on success, or one of the following negative error codes on
1127 * * %-EINVAL - Invalid segment number.
1128 * * %-EIO - I/O error (including metadata corruption).
1129 * * %-ENOMEM - Insufficient memory available.
1131 static int nilfs_free_segments(struct the_nilfs *nilfs, __u64 *segnumv,
1140 ret = nilfs_sufile_freev(nilfs->ns_sufile, segnumv, nsegs, &ndone);
1141 if (unlikely(ret)) {
1142 nilfs_sufile_cancel_freev(nilfs->ns_sufile, segnumv, ndone,
1145 * If a segment usage of the segments to be freed is in a
1146 * hole block, nilfs_sufile_freev() will return -ENOENT.
1147 * In this case, -EINVAL should be returned to the caller
1148 * since there is something wrong with the given segment
1149 * number array. This error can only occur during GC, so
1150 * there is no need to worry about it propagating to other
1151 * callers (such as fsync).
1153 if (ret == -ENOENT) {
1154 nilfs_err(nilfs->ns_sb,
1155 "The segment usage entry %llu to be freed is invalid (in a hole)",
1156 (unsigned long long)segnumv[ndone]);
1163 static int nilfs_segctor_collect_blocks(struct nilfs_sc_info *sci, int mode)
1165 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1166 struct list_head *head;
1167 struct nilfs_inode_info *ii;
1170 switch (nilfs_sc_cstage_get(sci)) {
1173 sci->sc_stage.flags = 0;
1175 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags)) {
1176 sci->sc_nblk_inc = 0;
1177 sci->sc_curseg->sb_sum.flags = NILFS_SS_LOGBGN;
1178 if (mode == SC_LSEG_DSYNC) {
1179 nilfs_sc_cstage_set(sci, NILFS_ST_DSYNC);
1184 sci->sc_stage.dirty_file_ptr = NULL;
1185 sci->sc_stage.gc_inode_ptr = NULL;
1186 if (mode == SC_FLUSH_DAT) {
1187 nilfs_sc_cstage_set(sci, NILFS_ST_DAT);
1190 nilfs_sc_cstage_inc(sci);
1193 if (nilfs_doing_gc()) {
1194 head = &sci->sc_gc_inodes;
1195 ii = list_prepare_entry(sci->sc_stage.gc_inode_ptr,
1197 list_for_each_entry_continue(ii, head, i_dirty) {
1198 err = nilfs_segctor_scan_file(
1199 sci, &ii->vfs_inode,
1200 &nilfs_sc_file_ops);
1201 if (unlikely(err)) {
1202 sci->sc_stage.gc_inode_ptr = list_entry(
1204 struct nilfs_inode_info,
1208 set_bit(NILFS_I_COLLECTED, &ii->i_state);
1210 sci->sc_stage.gc_inode_ptr = NULL;
1212 nilfs_sc_cstage_inc(sci);
1215 head = &sci->sc_dirty_files;
1216 ii = list_prepare_entry(sci->sc_stage.dirty_file_ptr, head,
1218 list_for_each_entry_continue(ii, head, i_dirty) {
1219 clear_bit(NILFS_I_DIRTY, &ii->i_state);
1221 err = nilfs_segctor_scan_file(sci, &ii->vfs_inode,
1222 &nilfs_sc_file_ops);
1223 if (unlikely(err)) {
1224 sci->sc_stage.dirty_file_ptr =
1225 list_entry(ii->i_dirty.prev,
1226 struct nilfs_inode_info,
1230 /* sci->sc_stage.dirty_file_ptr = NILFS_I(inode); */
1231 /* XXX: required ? */
1233 sci->sc_stage.dirty_file_ptr = NULL;
1234 if (mode == SC_FLUSH_FILE) {
1235 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
1238 nilfs_sc_cstage_inc(sci);
1239 sci->sc_stage.flags |= NILFS_CF_IFILE_STARTED;
1241 case NILFS_ST_IFILE:
1242 err = nilfs_segctor_scan_file(sci, sci->sc_root->ifile,
1243 &nilfs_sc_file_ops);
1246 nilfs_sc_cstage_inc(sci);
1247 /* Creating a checkpoint */
1248 err = nilfs_cpfile_create_checkpoint(nilfs->ns_cpfile,
1253 case NILFS_ST_CPFILE:
1254 err = nilfs_segctor_scan_file(sci, nilfs->ns_cpfile,
1255 &nilfs_sc_file_ops);
1258 nilfs_sc_cstage_inc(sci);
1260 case NILFS_ST_SUFILE:
1261 err = nilfs_free_segments(nilfs, sci->sc_freesegs,
1265 sci->sc_stage.flags |= NILFS_CF_SUFREED;
1267 err = nilfs_segctor_scan_file(sci, nilfs->ns_sufile,
1268 &nilfs_sc_file_ops);
1271 nilfs_sc_cstage_inc(sci);
1275 err = nilfs_segctor_scan_file(sci, nilfs->ns_dat,
1279 if (mode == SC_FLUSH_DAT) {
1280 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
1283 nilfs_sc_cstage_inc(sci);
1286 if (mode == SC_LSEG_SR) {
1287 /* Appending a super root */
1288 err = nilfs_segctor_add_super_root(sci);
1292 /* End of a logical segment */
1293 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1294 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
1296 case NILFS_ST_DSYNC:
1298 sci->sc_curseg->sb_sum.flags |= NILFS_SS_SYNDT;
1299 ii = sci->sc_dsync_inode;
1300 if (!test_bit(NILFS_I_BUSY, &ii->i_state))
1303 err = nilfs_segctor_scan_file_dsync(sci, &ii->vfs_inode);
1306 sci->sc_curseg->sb_sum.flags |= NILFS_SS_LOGEND;
1307 nilfs_sc_cstage_set(sci, NILFS_ST_DONE);
1320 * nilfs_segctor_begin_construction - setup segment buffer to make a new log
1321 * @sci: nilfs_sc_info
1322 * @nilfs: nilfs object
1324 * Return: 0 on success, or a negative error code on failure.
1326 static int nilfs_segctor_begin_construction(struct nilfs_sc_info *sci,
1327 struct the_nilfs *nilfs)
1329 struct nilfs_segment_buffer *segbuf, *prev;
1333 segbuf = nilfs_segbuf_new(sci->sc_super);
1334 if (unlikely(!segbuf))
1337 if (list_empty(&sci->sc_write_logs)) {
1338 nilfs_segbuf_map(segbuf, nilfs->ns_segnum,
1339 nilfs->ns_pseg_offset, nilfs);
1340 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1341 nilfs_shift_to_next_segment(nilfs);
1342 nilfs_segbuf_map(segbuf, nilfs->ns_segnum, 0, nilfs);
1345 segbuf->sb_sum.seg_seq = nilfs->ns_seg_seq;
1346 nextnum = nilfs->ns_nextnum;
1348 if (nilfs->ns_segnum == nilfs->ns_nextnum)
1349 /* Start from the head of a new full segment */
1353 prev = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1354 nilfs_segbuf_map_cont(segbuf, prev);
1355 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq;
1356 nextnum = prev->sb_nextnum;
1358 if (segbuf->sb_rest_blocks < NILFS_PSEG_MIN_BLOCKS) {
1359 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1360 segbuf->sb_sum.seg_seq++;
1365 err = nilfs_sufile_mark_dirty(nilfs->ns_sufile, segbuf->sb_segnum);
1370 err = nilfs_sufile_alloc(nilfs->ns_sufile, &nextnum);
1374 nilfs_segbuf_set_next_segnum(segbuf, nextnum, nilfs);
1376 BUG_ON(!list_empty(&sci->sc_segbufs));
1377 list_add_tail(&segbuf->sb_list, &sci->sc_segbufs);
1378 sci->sc_segbuf_nblocks = segbuf->sb_rest_blocks;
1382 nilfs_segbuf_free(segbuf);
1386 static int nilfs_segctor_extend_segments(struct nilfs_sc_info *sci,
1387 struct the_nilfs *nilfs, int nadd)
1389 struct nilfs_segment_buffer *segbuf, *prev;
1390 struct inode *sufile = nilfs->ns_sufile;
1395 prev = NILFS_LAST_SEGBUF(&sci->sc_segbufs);
1397 * Since the segment specified with nextnum might be allocated during
1398 * the previous construction, the buffer including its segusage may
1399 * not be dirty. The following call ensures that the buffer is dirty
1400 * and will pin the buffer on memory until the sufile is written.
1402 err = nilfs_sufile_mark_dirty(sufile, prev->sb_nextnum);
1406 for (i = 0; i < nadd; i++) {
1407 /* extend segment info */
1409 segbuf = nilfs_segbuf_new(sci->sc_super);
1410 if (unlikely(!segbuf))
1413 /* map this buffer to region of segment on-disk */
1414 nilfs_segbuf_map(segbuf, prev->sb_nextnum, 0, nilfs);
1415 sci->sc_segbuf_nblocks += segbuf->sb_rest_blocks;
1417 /* allocate the next next full segment */
1418 err = nilfs_sufile_alloc(sufile, &nextnextnum);
1422 segbuf->sb_sum.seg_seq = prev->sb_sum.seg_seq + 1;
1423 nilfs_segbuf_set_next_segnum(segbuf, nextnextnum, nilfs);
1425 list_add_tail(&segbuf->sb_list, &list);
1428 list_splice_tail(&list, &sci->sc_segbufs);
1432 nilfs_segbuf_free(segbuf);
1434 list_for_each_entry(segbuf, &list, sb_list) {
1435 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1436 WARN_ON(ret); /* never fails */
1438 nilfs_destroy_logs(&list);
1442 static void nilfs_free_incomplete_logs(struct list_head *logs,
1443 struct the_nilfs *nilfs)
1445 struct nilfs_segment_buffer *segbuf, *prev;
1446 struct inode *sufile = nilfs->ns_sufile;
1449 segbuf = NILFS_FIRST_SEGBUF(logs);
1450 if (nilfs->ns_nextnum != segbuf->sb_nextnum) {
1451 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1452 WARN_ON(ret); /* never fails */
1454 if (atomic_read(&segbuf->sb_err)) {
1455 /* Case 1: The first segment failed */
1456 if (segbuf->sb_pseg_start != segbuf->sb_fseg_start)
1458 * Case 1a: Partial segment appended into an existing
1461 nilfs_terminate_segment(nilfs, segbuf->sb_fseg_start,
1462 segbuf->sb_fseg_end);
1463 else /* Case 1b: New full segment */
1464 set_nilfs_discontinued(nilfs);
1468 list_for_each_entry_continue(segbuf, logs, sb_list) {
1469 if (prev->sb_nextnum != segbuf->sb_nextnum) {
1470 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1471 WARN_ON(ret); /* never fails */
1473 if (atomic_read(&segbuf->sb_err) &&
1474 segbuf->sb_segnum != nilfs->ns_nextnum)
1475 /* Case 2: extended segment (!= next) failed */
1476 nilfs_sufile_set_error(sufile, segbuf->sb_segnum);
1481 static void nilfs_segctor_update_segusage(struct nilfs_sc_info *sci,
1482 struct inode *sufile)
1484 struct nilfs_segment_buffer *segbuf;
1485 unsigned long live_blocks;
1488 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1489 live_blocks = segbuf->sb_sum.nblocks +
1490 (segbuf->sb_pseg_start - segbuf->sb_fseg_start);
1491 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1494 WARN_ON(ret); /* always succeed because the segusage is dirty */
1498 static void nilfs_cancel_segusage(struct list_head *logs, struct inode *sufile)
1500 struct nilfs_segment_buffer *segbuf;
1503 segbuf = NILFS_FIRST_SEGBUF(logs);
1504 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1505 segbuf->sb_pseg_start -
1506 segbuf->sb_fseg_start, 0);
1507 WARN_ON(ret); /* always succeed because the segusage is dirty */
1509 list_for_each_entry_continue(segbuf, logs, sb_list) {
1510 ret = nilfs_sufile_set_segment_usage(sufile, segbuf->sb_segnum,
1512 WARN_ON(ret); /* always succeed */
1516 static void nilfs_segctor_truncate_segments(struct nilfs_sc_info *sci,
1517 struct nilfs_segment_buffer *last,
1518 struct inode *sufile)
1520 struct nilfs_segment_buffer *segbuf = last;
1523 list_for_each_entry_continue(segbuf, &sci->sc_segbufs, sb_list) {
1524 sci->sc_segbuf_nblocks -= segbuf->sb_rest_blocks;
1525 ret = nilfs_sufile_free(sufile, segbuf->sb_nextnum);
1528 nilfs_truncate_logs(&sci->sc_segbufs, last);
1532 static int nilfs_segctor_collect(struct nilfs_sc_info *sci,
1533 struct the_nilfs *nilfs, int mode)
1535 struct nilfs_cstage prev_stage = sci->sc_stage;
1538 /* Collection retry loop */
1540 sci->sc_nblk_this_inc = 0;
1541 sci->sc_curseg = NILFS_FIRST_SEGBUF(&sci->sc_segbufs);
1543 err = nilfs_segctor_reset_segment_buffer(sci);
1547 err = nilfs_segctor_collect_blocks(sci, mode);
1548 sci->sc_nblk_this_inc += sci->sc_curseg->sb_sum.nblocks;
1552 if (unlikely(err != -E2BIG))
1555 /* The current segment is filled up */
1556 if (mode != SC_LSEG_SR ||
1557 nilfs_sc_cstage_get(sci) < NILFS_ST_CPFILE)
1560 nilfs_clear_logs(&sci->sc_segbufs);
1562 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1563 err = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1567 WARN_ON(err); /* do not happen */
1568 sci->sc_stage.flags &= ~NILFS_CF_SUFREED;
1571 err = nilfs_segctor_extend_segments(sci, nilfs, nadd);
1575 nadd = min_t(int, nadd << 1, SC_MAX_SEGDELTA);
1576 sci->sc_stage = prev_stage;
1578 nilfs_segctor_zeropad_segsum(sci);
1579 nilfs_segctor_truncate_segments(sci, sci->sc_curseg, nilfs->ns_sufile);
1586 static void nilfs_list_replace_buffer(struct buffer_head *old_bh,
1587 struct buffer_head *new_bh)
1589 BUG_ON(!list_empty(&new_bh->b_assoc_buffers));
1591 list_replace_init(&old_bh->b_assoc_buffers, &new_bh->b_assoc_buffers);
1592 /* The caller must release old_bh */
1596 nilfs_segctor_update_payload_blocknr(struct nilfs_sc_info *sci,
1597 struct nilfs_segment_buffer *segbuf,
1600 struct inode *inode = NULL;
1602 unsigned long nfinfo = segbuf->sb_sum.nfinfo;
1603 unsigned long nblocks = 0, ndatablk = 0;
1604 const struct nilfs_sc_operations *sc_op = NULL;
1605 struct nilfs_segsum_pointer ssp;
1606 struct nilfs_finfo *finfo = NULL;
1607 union nilfs_binfo binfo;
1608 struct buffer_head *bh, *bh_org;
1615 blocknr = segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk;
1616 ssp.bh = NILFS_SEGBUF_FIRST_BH(&segbuf->sb_segsum_buffers);
1617 ssp.offset = sizeof(struct nilfs_segment_summary);
1619 list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
1620 if (bh == segbuf->sb_super_root)
1623 finfo = nilfs_segctor_map_segsum_entry(
1624 sci, &ssp, sizeof(*finfo));
1625 ino = le64_to_cpu(finfo->fi_ino);
1626 nblocks = le32_to_cpu(finfo->fi_nblocks);
1627 ndatablk = le32_to_cpu(finfo->fi_ndatablk);
1629 inode = bh->b_folio->mapping->host;
1631 if (mode == SC_LSEG_DSYNC)
1632 sc_op = &nilfs_sc_dsync_ops;
1633 else if (ino == NILFS_DAT_INO)
1634 sc_op = &nilfs_sc_dat_ops;
1635 else /* file blocks */
1636 sc_op = &nilfs_sc_file_ops;
1640 err = nilfs_bmap_assign(NILFS_I(inode)->i_bmap, &bh, blocknr,
1643 nilfs_list_replace_buffer(bh_org, bh);
1649 sc_op->write_data_binfo(sci, &ssp, &binfo);
1651 sc_op->write_node_binfo(sci, &ssp, &binfo);
1654 if (--nblocks == 0) {
1658 } else if (ndatablk > 0)
1668 static int nilfs_segctor_assign(struct nilfs_sc_info *sci, int mode)
1670 struct nilfs_segment_buffer *segbuf;
1673 list_for_each_entry(segbuf, &sci->sc_segbufs, sb_list) {
1674 err = nilfs_segctor_update_payload_blocknr(sci, segbuf, mode);
1677 nilfs_segbuf_fill_in_segsum(segbuf);
1682 static void nilfs_begin_folio_io(struct folio *folio)
1684 if (!folio || folio_test_writeback(folio))
1686 * For split b-tree node pages, this function may be called
1687 * twice. We ignore the 2nd or later calls by this check.
1692 folio_clear_dirty_for_io(folio);
1693 folio_start_writeback(folio);
1694 folio_unlock(folio);
1698 * nilfs_prepare_write_logs - prepare to write logs
1699 * @logs: logs to prepare for writing
1700 * @seed: checksum seed value
1702 * nilfs_prepare_write_logs() adds checksums and prepares the block
1703 * buffers/folios for writing logs. In order to stabilize folios of
1704 * memory-mapped file blocks by putting them in writeback state before
1705 * calculating the checksums, first prepare to write payload blocks other
1706 * than segment summary and super root blocks in which the checksums will
1709 static void nilfs_prepare_write_logs(struct list_head *logs, u32 seed)
1711 struct nilfs_segment_buffer *segbuf;
1712 struct folio *bd_folio = NULL, *fs_folio = NULL;
1713 struct buffer_head *bh;
1715 /* Prepare to write payload blocks */
1716 list_for_each_entry(segbuf, logs, sb_list) {
1717 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1719 if (bh == segbuf->sb_super_root)
1721 set_buffer_async_write(bh);
1722 if (bh->b_folio != fs_folio) {
1723 nilfs_begin_folio_io(fs_folio);
1724 fs_folio = bh->b_folio;
1728 nilfs_begin_folio_io(fs_folio);
1730 nilfs_add_checksums_on_logs(logs, seed);
1732 /* Prepare to write segment summary blocks */
1733 list_for_each_entry(segbuf, logs, sb_list) {
1734 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1736 mark_buffer_dirty(bh);
1737 if (bh->b_folio == bd_folio)
1740 folio_lock(bd_folio);
1741 folio_wait_writeback(bd_folio);
1742 folio_clear_dirty_for_io(bd_folio);
1743 folio_start_writeback(bd_folio);
1744 folio_unlock(bd_folio);
1746 bd_folio = bh->b_folio;
1750 /* Prepare to write super root block */
1751 bh = NILFS_LAST_SEGBUF(logs)->sb_super_root;
1753 mark_buffer_dirty(bh);
1754 if (bh->b_folio != bd_folio) {
1755 folio_lock(bd_folio);
1756 folio_wait_writeback(bd_folio);
1757 folio_clear_dirty_for_io(bd_folio);
1758 folio_start_writeback(bd_folio);
1759 folio_unlock(bd_folio);
1760 bd_folio = bh->b_folio;
1765 folio_lock(bd_folio);
1766 folio_wait_writeback(bd_folio);
1767 folio_clear_dirty_for_io(bd_folio);
1768 folio_start_writeback(bd_folio);
1769 folio_unlock(bd_folio);
1773 static int nilfs_segctor_write(struct nilfs_sc_info *sci,
1774 struct the_nilfs *nilfs)
1778 ret = nilfs_write_logs(&sci->sc_segbufs, nilfs);
1779 list_splice_tail_init(&sci->sc_segbufs, &sci->sc_write_logs);
1783 static void nilfs_end_folio_io(struct folio *folio, int err)
1788 if (buffer_nilfs_node(folio_buffers(folio)) &&
1789 !folio_test_writeback(folio)) {
1791 * For b-tree node pages, this function may be called twice
1792 * or more because they might be split in a segment.
1794 if (folio_test_dirty(folio)) {
1796 * For pages holding split b-tree node buffers, dirty
1797 * flag on the buffers may be cleared discretely.
1798 * In that case, the page is once redirtied for
1799 * remaining buffers, and it must be cancelled if
1800 * all the buffers get cleaned later.
1803 if (nilfs_folio_buffers_clean(folio))
1804 __nilfs_clear_folio_dirty(folio);
1805 folio_unlock(folio);
1810 if (err || !nilfs_folio_buffers_clean(folio))
1811 filemap_dirty_folio(folio->mapping, folio);
1813 folio_end_writeback(folio);
1816 static void nilfs_abort_logs(struct list_head *logs, int err)
1818 struct nilfs_segment_buffer *segbuf;
1819 struct folio *bd_folio = NULL, *fs_folio = NULL;
1820 struct buffer_head *bh;
1822 if (list_empty(logs))
1825 list_for_each_entry(segbuf, logs, sb_list) {
1826 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1828 clear_buffer_uptodate(bh);
1829 if (bh->b_folio != bd_folio) {
1831 folio_end_writeback(bd_folio);
1832 bd_folio = bh->b_folio;
1836 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1838 if (bh == segbuf->sb_super_root) {
1839 clear_buffer_uptodate(bh);
1840 if (bh->b_folio != bd_folio) {
1841 folio_end_writeback(bd_folio);
1842 bd_folio = bh->b_folio;
1846 clear_buffer_async_write(bh);
1847 if (bh->b_folio != fs_folio) {
1848 nilfs_end_folio_io(fs_folio, err);
1849 fs_folio = bh->b_folio;
1854 folio_end_writeback(bd_folio);
1856 nilfs_end_folio_io(fs_folio, err);
1859 static void nilfs_segctor_abort_construction(struct nilfs_sc_info *sci,
1860 struct the_nilfs *nilfs, int err)
1865 list_splice_tail_init(&sci->sc_write_logs, &logs);
1866 ret = nilfs_wait_on_logs(&logs);
1867 nilfs_abort_logs(&logs, ret ? : err);
1869 list_splice_tail_init(&sci->sc_segbufs, &logs);
1870 if (list_empty(&logs))
1871 return; /* if the first segment buffer preparation failed */
1873 nilfs_cancel_segusage(&logs, nilfs->ns_sufile);
1874 nilfs_free_incomplete_logs(&logs, nilfs);
1876 if (sci->sc_stage.flags & NILFS_CF_SUFREED) {
1877 ret = nilfs_sufile_cancel_freev(nilfs->ns_sufile,
1881 WARN_ON(ret); /* do not happen */
1884 nilfs_destroy_logs(&logs);
1887 static void nilfs_set_next_segment(struct the_nilfs *nilfs,
1888 struct nilfs_segment_buffer *segbuf)
1890 nilfs->ns_segnum = segbuf->sb_segnum;
1891 nilfs->ns_nextnum = segbuf->sb_nextnum;
1892 nilfs->ns_pseg_offset = segbuf->sb_pseg_start - segbuf->sb_fseg_start
1893 + segbuf->sb_sum.nblocks;
1894 nilfs->ns_seg_seq = segbuf->sb_sum.seg_seq;
1895 nilfs->ns_ctime = segbuf->sb_sum.ctime;
1898 static void nilfs_segctor_complete_write(struct nilfs_sc_info *sci)
1900 struct nilfs_segment_buffer *segbuf;
1901 struct folio *bd_folio = NULL, *fs_folio = NULL;
1902 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
1903 int update_sr = false;
1905 list_for_each_entry(segbuf, &sci->sc_write_logs, sb_list) {
1906 struct buffer_head *bh;
1908 list_for_each_entry(bh, &segbuf->sb_segsum_buffers,
1910 set_buffer_uptodate(bh);
1911 clear_buffer_dirty(bh);
1912 if (bh->b_folio != bd_folio) {
1914 folio_end_writeback(bd_folio);
1915 bd_folio = bh->b_folio;
1919 * We assume that the buffers which belong to the same folio
1920 * continue over the buffer list.
1921 * Under this assumption, the last BHs of folios is
1922 * identifiable by the discontinuity of bh->b_folio
1923 * (folio != fs_folio).
1925 * For B-tree node blocks, however, this assumption is not
1926 * guaranteed. The cleanup code of B-tree node folios needs
1929 list_for_each_entry(bh, &segbuf->sb_payload_buffers,
1931 const unsigned long set_bits = BIT(BH_Uptodate);
1932 const unsigned long clear_bits =
1933 (BIT(BH_Dirty) | BIT(BH_Async_Write) |
1934 BIT(BH_Delay) | BIT(BH_NILFS_Volatile) |
1935 BIT(BH_NILFS_Redirected));
1937 if (bh == segbuf->sb_super_root) {
1938 set_buffer_uptodate(bh);
1939 clear_buffer_dirty(bh);
1940 if (bh->b_folio != bd_folio) {
1941 folio_end_writeback(bd_folio);
1942 bd_folio = bh->b_folio;
1947 set_mask_bits(&bh->b_state, clear_bits, set_bits);
1948 if (bh->b_folio != fs_folio) {
1949 nilfs_end_folio_io(fs_folio, 0);
1950 fs_folio = bh->b_folio;
1954 if (!nilfs_segbuf_simplex(segbuf)) {
1955 if (segbuf->sb_sum.flags & NILFS_SS_LOGBGN) {
1956 set_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1957 sci->sc_lseg_stime = jiffies;
1959 if (segbuf->sb_sum.flags & NILFS_SS_LOGEND)
1960 clear_bit(NILFS_SC_UNCLOSED, &sci->sc_flags);
1964 * Since folios may continue over multiple segment buffers,
1965 * end of the last folio must be checked outside of the loop.
1968 folio_end_writeback(bd_folio);
1970 nilfs_end_folio_io(fs_folio, 0);
1972 nilfs_drop_collected_inodes(&sci->sc_dirty_files);
1974 if (nilfs_doing_gc())
1975 nilfs_drop_collected_inodes(&sci->sc_gc_inodes);
1977 nilfs->ns_nongc_ctime = sci->sc_seg_ctime;
1979 sci->sc_nblk_inc += sci->sc_nblk_this_inc;
1981 segbuf = NILFS_LAST_SEGBUF(&sci->sc_write_logs);
1982 nilfs_set_next_segment(nilfs, segbuf);
1985 nilfs->ns_flushed_device = 0;
1986 nilfs_set_last_segment(nilfs, segbuf->sb_pseg_start,
1987 segbuf->sb_sum.seg_seq, nilfs->ns_cno++);
1989 clear_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags);
1990 clear_bit(NILFS_SC_DIRTY, &sci->sc_flags);
1991 set_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1992 nilfs_segctor_clear_metadata_dirty(sci);
1994 clear_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags);
1997 static int nilfs_segctor_wait(struct nilfs_sc_info *sci)
2001 ret = nilfs_wait_on_logs(&sci->sc_write_logs);
2003 nilfs_segctor_complete_write(sci);
2004 nilfs_destroy_logs(&sci->sc_write_logs);
2009 static int nilfs_segctor_collect_dirty_files(struct nilfs_sc_info *sci,
2010 struct the_nilfs *nilfs)
2012 struct nilfs_inode_info *ii, *n;
2013 struct inode *ifile = sci->sc_root->ifile;
2015 spin_lock(&nilfs->ns_inode_lock);
2017 list_for_each_entry_safe(ii, n, &nilfs->ns_dirty_files, i_dirty) {
2019 struct buffer_head *ibh;
2022 spin_unlock(&nilfs->ns_inode_lock);
2023 err = nilfs_ifile_get_inode_block(
2024 ifile, ii->vfs_inode.i_ino, &ibh);
2025 if (unlikely(err)) {
2026 nilfs_warn(sci->sc_super,
2027 "log writer: error %d getting inode block (ino=%lu)",
2028 err, ii->vfs_inode.i_ino);
2031 spin_lock(&nilfs->ns_inode_lock);
2032 if (likely(!ii->i_bh))
2039 // Always redirty the buffer to avoid race condition
2040 mark_buffer_dirty(ii->i_bh);
2041 nilfs_mdt_mark_dirty(ifile);
2043 clear_bit(NILFS_I_QUEUED, &ii->i_state);
2044 set_bit(NILFS_I_BUSY, &ii->i_state);
2045 list_move_tail(&ii->i_dirty, &sci->sc_dirty_files);
2047 spin_unlock(&nilfs->ns_inode_lock);
2052 static void nilfs_segctor_drop_written_files(struct nilfs_sc_info *sci,
2053 struct the_nilfs *nilfs)
2055 struct nilfs_inode_info *ii, *n;
2056 int during_mount = !(sci->sc_super->s_flags & SB_ACTIVE);
2057 int defer_iput = false;
2059 spin_lock(&nilfs->ns_inode_lock);
2060 list_for_each_entry_safe(ii, n, &sci->sc_dirty_files, i_dirty) {
2061 if (!test_and_clear_bit(NILFS_I_UPDATED, &ii->i_state) ||
2062 test_bit(NILFS_I_DIRTY, &ii->i_state))
2065 clear_bit(NILFS_I_BUSY, &ii->i_state);
2068 list_del_init(&ii->i_dirty);
2069 if (!ii->vfs_inode.i_nlink || during_mount) {
2071 * Defer calling iput() to avoid deadlocks if
2072 * i_nlink == 0 or mount is not yet finished.
2074 list_add_tail(&ii->i_dirty, &sci->sc_iput_queue);
2077 spin_unlock(&nilfs->ns_inode_lock);
2078 iput(&ii->vfs_inode);
2079 spin_lock(&nilfs->ns_inode_lock);
2082 spin_unlock(&nilfs->ns_inode_lock);
2085 schedule_work(&sci->sc_iput_work);
2089 * Main procedure of segment constructor
2091 static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)
2093 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2096 if (sb_rdonly(sci->sc_super))
2099 nilfs_sc_cstage_set(sci, NILFS_ST_INIT);
2100 sci->sc_cno = nilfs->ns_cno;
2102 err = nilfs_segctor_collect_dirty_files(sci, nilfs);
2106 if (nilfs_test_metadata_dirty(nilfs, sci->sc_root))
2107 set_bit(NILFS_SC_DIRTY, &sci->sc_flags);
2109 if (nilfs_segctor_clean(sci))
2113 sci->sc_stage.flags &= ~NILFS_CF_HISTORY_MASK;
2115 err = nilfs_segctor_begin_construction(sci, nilfs);
2119 /* Update time stamp */
2120 sci->sc_seg_ctime = ktime_get_real_seconds();
2122 err = nilfs_segctor_collect(sci, nilfs, mode);
2126 /* Avoid empty segment */
2127 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE &&
2128 nilfs_segbuf_empty(sci->sc_curseg)) {
2129 nilfs_segctor_abort_construction(sci, nilfs, 1);
2133 err = nilfs_segctor_assign(sci, mode);
2137 if (sci->sc_stage.flags & NILFS_CF_IFILE_STARTED)
2138 nilfs_segctor_fill_in_file_bmap(sci);
2140 if (mode == SC_LSEG_SR &&
2141 nilfs_sc_cstage_get(sci) >= NILFS_ST_CPFILE) {
2142 err = nilfs_cpfile_finalize_checkpoint(
2143 nilfs->ns_cpfile, nilfs->ns_cno, sci->sc_root,
2144 sci->sc_nblk_inc + sci->sc_nblk_this_inc,
2146 !test_bit(NILFS_SC_HAVE_DELTA, &sci->sc_flags));
2148 goto failed_to_write;
2150 nilfs_segctor_fill_in_super_root(sci, nilfs);
2152 nilfs_segctor_update_segusage(sci, nilfs->ns_sufile);
2154 /* Write partial segments */
2155 nilfs_prepare_write_logs(&sci->sc_segbufs, nilfs->ns_crc_seed);
2157 err = nilfs_segctor_write(sci, nilfs);
2159 goto failed_to_write;
2161 if (nilfs_sc_cstage_get(sci) == NILFS_ST_DONE ||
2162 nilfs->ns_blocksize_bits != PAGE_SHIFT) {
2164 * At this point, we avoid double buffering
2165 * for blocksize < pagesize because page dirty
2166 * flag is turned off during write and dirty
2167 * buffers are not properly collected for
2168 * pages crossing over segments.
2170 err = nilfs_segctor_wait(sci);
2172 goto failed_to_write;
2174 } while (nilfs_sc_cstage_get(sci) != NILFS_ST_DONE);
2177 nilfs_segctor_drop_written_files(sci, nilfs);
2182 if (mode == SC_LSEG_SR && nilfs_sc_cstage_get(sci) >= NILFS_ST_IFILE)
2183 nilfs_redirty_inodes(&sci->sc_dirty_files);
2184 if (nilfs_doing_gc())
2185 nilfs_redirty_inodes(&sci->sc_gc_inodes);
2186 nilfs_segctor_abort_construction(sci, nilfs, err);
2191 * nilfs_segctor_start_timer - set timer of background write
2192 * @sci: nilfs_sc_info
2194 * If the timer has already been set, it ignores the new request.
2195 * This function MUST be called within a section locking the segment
2198 static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
2200 spin_lock(&sci->sc_state_lock);
2201 if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2203 sci->sc_timer.expires = jiffies + sci->sc_interval;
2204 add_timer(&sci->sc_timer);
2206 sci->sc_state |= NILFS_SEGCTOR_COMMIT;
2208 spin_unlock(&sci->sc_state_lock);
2211 static void nilfs_segctor_do_flush(struct nilfs_sc_info *sci, int bn)
2213 spin_lock(&sci->sc_state_lock);
2214 if (!(sci->sc_flush_request & BIT(bn))) {
2215 unsigned long prev_req = sci->sc_flush_request;
2217 sci->sc_flush_request |= BIT(bn);
2219 wake_up(&sci->sc_wait_daemon);
2221 spin_unlock(&sci->sc_state_lock);
2224 struct nilfs_segctor_wait_request {
2225 wait_queue_entry_t wq;
2231 static int nilfs_segctor_sync(struct nilfs_sc_info *sci)
2233 struct nilfs_segctor_wait_request wait_req;
2236 init_wait(&wait_req.wq);
2238 atomic_set(&wait_req.done, 0);
2239 init_waitqueue_entry(&wait_req.wq, current);
2242 * To prevent a race issue where completion notifications from the
2243 * log writer thread are missed, increment the request sequence count
2244 * "sc_seq_request" and insert a wait queue entry using the current
2245 * sequence number into the "sc_wait_request" queue at the same time
2246 * within the lock section of "sc_state_lock".
2248 spin_lock(&sci->sc_state_lock);
2249 wait_req.seq = ++sci->sc_seq_request;
2250 add_wait_queue(&sci->sc_wait_request, &wait_req.wq);
2251 spin_unlock(&sci->sc_state_lock);
2253 wake_up(&sci->sc_wait_daemon);
2256 set_current_state(TASK_INTERRUPTIBLE);
2259 * Synchronize only while the log writer thread is alive.
2260 * Leave flushing out after the log writer thread exits to
2261 * the cleanup work in nilfs_segctor_destroy().
2266 if (atomic_read(&wait_req.done)) {
2270 if (!signal_pending(current)) {
2277 finish_wait(&sci->sc_wait_request, &wait_req.wq);
2281 static void nilfs_segctor_wakeup(struct nilfs_sc_info *sci, int err, bool force)
2283 struct nilfs_segctor_wait_request *wrq, *n;
2284 unsigned long flags;
2286 spin_lock_irqsave(&sci->sc_wait_request.lock, flags);
2287 list_for_each_entry_safe(wrq, n, &sci->sc_wait_request.head, wq.entry) {
2288 if (!atomic_read(&wrq->done) &&
2289 (force || nilfs_cnt32_ge(sci->sc_seq_done, wrq->seq))) {
2291 atomic_set(&wrq->done, 1);
2293 if (atomic_read(&wrq->done)) {
2294 wrq->wq.func(&wrq->wq,
2295 TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
2299 spin_unlock_irqrestore(&sci->sc_wait_request.lock, flags);
2303 * nilfs_construct_segment - construct a logical segment
2306 * Return: 0 on success, or one of the following negative error codes on
2308 * * %-EIO - I/O error (including metadata corruption).
2309 * * %-ENOMEM - Insufficient memory available.
2310 * * %-ENOSPC - No space left on device (only in a panic state).
2311 * * %-ERESTARTSYS - Interrupted.
2312 * * %-EROFS - Read only filesystem.
2314 int nilfs_construct_segment(struct super_block *sb)
2316 struct the_nilfs *nilfs = sb->s_fs_info;
2317 struct nilfs_sc_info *sci = nilfs->ns_writer;
2318 struct nilfs_transaction_info *ti;
2320 if (sb_rdonly(sb) || unlikely(!sci))
2323 /* A call inside transactions causes a deadlock. */
2324 BUG_ON((ti = current->journal_info) && ti->ti_magic == NILFS_TI_MAGIC);
2326 return nilfs_segctor_sync(sci);
2330 * nilfs_construct_dsync_segment - construct a data-only logical segment
2332 * @inode: inode whose data blocks should be written out
2333 * @start: start byte offset
2334 * @end: end byte offset (inclusive)
2336 * Return: 0 on success, or one of the following negative error codes on
2338 * * %-EIO - I/O error (including metadata corruption).
2339 * * %-ENOMEM - Insufficient memory available.
2340 * * %-ENOSPC - No space left on device (only in a panic state).
2341 * * %-ERESTARTSYS - Interrupted.
2342 * * %-EROFS - Read only filesystem.
2344 int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
2345 loff_t start, loff_t end)
2347 struct the_nilfs *nilfs = sb->s_fs_info;
2348 struct nilfs_sc_info *sci = nilfs->ns_writer;
2349 struct nilfs_inode_info *ii;
2350 struct nilfs_transaction_info ti;
2353 if (sb_rdonly(sb) || unlikely(!sci))
2356 nilfs_transaction_lock(sb, &ti, 0);
2358 ii = NILFS_I(inode);
2359 if (test_bit(NILFS_I_INODE_SYNC, &ii->i_state) ||
2360 nilfs_test_opt(nilfs, STRICT_ORDER) ||
2361 test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2362 nilfs_discontinued(nilfs)) {
2363 nilfs_transaction_unlock(sb);
2364 err = nilfs_segctor_sync(sci);
2368 spin_lock(&nilfs->ns_inode_lock);
2369 if (!test_bit(NILFS_I_QUEUED, &ii->i_state) &&
2370 !test_bit(NILFS_I_BUSY, &ii->i_state)) {
2371 spin_unlock(&nilfs->ns_inode_lock);
2372 nilfs_transaction_unlock(sb);
2375 spin_unlock(&nilfs->ns_inode_lock);
2376 sci->sc_dsync_inode = ii;
2377 sci->sc_dsync_start = start;
2378 sci->sc_dsync_end = end;
2380 err = nilfs_segctor_do_construct(sci, SC_LSEG_DSYNC);
2382 nilfs->ns_flushed_device = 0;
2384 nilfs_transaction_unlock(sb);
2388 #define FLUSH_FILE_BIT (0x1) /* data file only */
2389 #define FLUSH_DAT_BIT BIT(NILFS_DAT_INO) /* DAT only */
2392 * nilfs_segctor_accept - record accepted sequence count of log-write requests
2393 * @sci: segment constructor object
2395 static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
2397 bool thread_is_alive;
2399 spin_lock(&sci->sc_state_lock);
2400 sci->sc_seq_accepted = sci->sc_seq_request;
2401 thread_is_alive = (bool)sci->sc_task;
2402 spin_unlock(&sci->sc_state_lock);
2405 * This function does not race with the log writer thread's
2406 * termination. Therefore, deleting sc_timer, which should not be
2407 * done after the log writer thread exits, can be done safely outside
2408 * the area protected by sc_state_lock.
2410 if (thread_is_alive)
2411 timer_delete_sync(&sci->sc_timer);
2415 * nilfs_segctor_notify - notify the result of request to caller threads
2416 * @sci: segment constructor object
2417 * @mode: mode of log forming
2418 * @err: error code to be notified
2420 static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
2422 /* Clear requests (even when the construction failed) */
2423 spin_lock(&sci->sc_state_lock);
2425 if (mode == SC_LSEG_SR) {
2426 sci->sc_state &= ~NILFS_SEGCTOR_COMMIT;
2427 sci->sc_seq_done = sci->sc_seq_accepted;
2428 nilfs_segctor_wakeup(sci, err, false);
2429 sci->sc_flush_request = 0;
2431 if (mode == SC_FLUSH_FILE)
2432 sci->sc_flush_request &= ~FLUSH_FILE_BIT;
2433 else if (mode == SC_FLUSH_DAT)
2434 sci->sc_flush_request &= ~FLUSH_DAT_BIT;
2436 /* re-enable timer if checkpoint creation was not done */
2437 if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && sci->sc_task &&
2438 time_before(jiffies, sci->sc_timer.expires))
2439 add_timer(&sci->sc_timer);
2441 spin_unlock(&sci->sc_state_lock);
2445 * nilfs_segctor_construct - form logs and write them to disk
2446 * @sci: segment constructor object
2447 * @mode: mode of log forming
2449 * Return: 0 on success, or a negative error code on failure.
2451 static int nilfs_segctor_construct(struct nilfs_sc_info *sci, int mode)
2453 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2454 struct nilfs_super_block **sbp;
2457 nilfs_segctor_accept(sci);
2459 if (nilfs_discontinued(nilfs))
2461 if (!nilfs_segctor_confirm(sci))
2462 err = nilfs_segctor_do_construct(sci, mode);
2465 if (mode != SC_FLUSH_DAT)
2466 atomic_set(&nilfs->ns_ndirtyblks, 0);
2467 if (test_bit(NILFS_SC_SUPER_ROOT, &sci->sc_flags) &&
2468 nilfs_discontinued(nilfs)) {
2469 down_write(&nilfs->ns_sem);
2471 sbp = nilfs_prepare_super(sci->sc_super,
2472 nilfs_sb_will_flip(nilfs));
2474 nilfs_set_log_cursor(sbp[0], nilfs);
2475 err = nilfs_commit_super(sci->sc_super,
2478 up_write(&nilfs->ns_sem);
2482 nilfs_segctor_notify(sci, mode, err);
2486 static void nilfs_construction_timeout(struct timer_list *t)
2488 struct nilfs_sc_info *sci = timer_container_of(sci, t, sc_timer);
2490 wake_up_process(sci->sc_task);
2494 nilfs_remove_written_gcinodes(struct the_nilfs *nilfs, struct list_head *head)
2496 struct nilfs_inode_info *ii, *n;
2498 list_for_each_entry_safe(ii, n, head, i_dirty) {
2499 if (!test_bit(NILFS_I_UPDATED, &ii->i_state))
2501 list_del_init(&ii->i_dirty);
2502 truncate_inode_pages(&ii->vfs_inode.i_data, 0);
2503 nilfs_btnode_cache_clear(ii->i_assoc_inode->i_mapping);
2504 iput(&ii->vfs_inode);
2508 int nilfs_clean_segments(struct super_block *sb, struct nilfs_argv *argv,
2511 struct the_nilfs *nilfs = sb->s_fs_info;
2512 struct nilfs_sc_info *sci = nilfs->ns_writer;
2513 struct nilfs_transaction_info ti;
2519 nilfs_transaction_lock(sb, &ti, 1);
2521 err = nilfs_mdt_save_to_shadow_map(nilfs->ns_dat);
2525 err = nilfs_ioctl_prepare_clean_segments(nilfs, argv, kbufs);
2526 if (unlikely(err)) {
2527 nilfs_mdt_restore_from_shadow_map(nilfs->ns_dat);
2531 sci->sc_freesegs = kbufs[4];
2532 sci->sc_nfreesegs = argv[4].v_nmembs;
2533 list_splice_tail_init(&nilfs->ns_gc_inodes, &sci->sc_gc_inodes);
2536 err = nilfs_segctor_construct(sci, SC_LSEG_SR);
2537 nilfs_remove_written_gcinodes(nilfs, &sci->sc_gc_inodes);
2542 nilfs_warn(sb, "error %d cleaning segments", err);
2543 set_current_state(TASK_INTERRUPTIBLE);
2544 schedule_timeout(sci->sc_interval);
2546 if (nilfs_test_opt(nilfs, DISCARD)) {
2547 int ret = nilfs_discard_segments(nilfs, sci->sc_freesegs,
2551 "error %d on discard request, turning discards off for the device",
2553 nilfs_clear_opt(nilfs, DISCARD);
2558 sci->sc_freesegs = NULL;
2559 sci->sc_nfreesegs = 0;
2560 nilfs_mdt_clear_shadow_map(nilfs->ns_dat);
2561 nilfs_transaction_unlock(sb);
2565 static void nilfs_segctor_thread_construct(struct nilfs_sc_info *sci, int mode)
2567 struct nilfs_transaction_info ti;
2569 nilfs_transaction_lock(sci->sc_super, &ti, 0);
2570 nilfs_segctor_construct(sci, mode);
2573 * Unclosed segment should be retried. We do this using sc_timer.
2574 * Timeout of sc_timer will invoke complete construction which leads
2575 * to close the current logical segment.
2577 if (test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags))
2578 nilfs_segctor_start_timer(sci);
2580 nilfs_transaction_unlock(sci->sc_super);
2583 static void nilfs_segctor_do_immediate_flush(struct nilfs_sc_info *sci)
2587 spin_lock(&sci->sc_state_lock);
2588 mode = (sci->sc_flush_request & FLUSH_DAT_BIT) ?
2589 SC_FLUSH_DAT : SC_FLUSH_FILE;
2590 spin_unlock(&sci->sc_state_lock);
2593 nilfs_segctor_do_construct(sci, mode);
2595 spin_lock(&sci->sc_state_lock);
2596 sci->sc_flush_request &= (mode == SC_FLUSH_FILE) ?
2597 ~FLUSH_FILE_BIT : ~FLUSH_DAT_BIT;
2598 spin_unlock(&sci->sc_state_lock);
2600 clear_bit(NILFS_SC_PRIOR_FLUSH, &sci->sc_flags);
2603 static int nilfs_segctor_flush_mode(struct nilfs_sc_info *sci)
2605 if (!test_bit(NILFS_SC_UNCLOSED, &sci->sc_flags) ||
2606 time_before(jiffies, sci->sc_lseg_stime + sci->sc_mjcp_freq)) {
2607 if (!(sci->sc_flush_request & ~FLUSH_FILE_BIT))
2608 return SC_FLUSH_FILE;
2609 else if (!(sci->sc_flush_request & ~FLUSH_DAT_BIT))
2610 return SC_FLUSH_DAT;
2616 * nilfs_log_write_required - determine whether log writing is required
2617 * @sci: nilfs_sc_info struct
2618 * @modep: location for storing log writing mode
2620 * Return: true if log writing is required, false otherwise. If log writing
2621 * is required, the mode is stored in the location pointed to by @modep.
2623 static bool nilfs_log_write_required(struct nilfs_sc_info *sci, int *modep)
2625 bool timedout, ret = true;
2627 spin_lock(&sci->sc_state_lock);
2628 timedout = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2629 time_after_eq(jiffies, sci->sc_timer.expires));
2630 if (timedout || sci->sc_seq_request != sci->sc_seq_done)
2631 *modep = SC_LSEG_SR;
2632 else if (sci->sc_flush_request)
2633 *modep = nilfs_segctor_flush_mode(sci);
2637 spin_unlock(&sci->sc_state_lock);
2642 * nilfs_segctor_thread - main loop of the log writer thread
2643 * @arg: pointer to a struct nilfs_sc_info.
2645 * nilfs_segctor_thread() is the main loop function of the log writer kernel
2646 * thread, which determines whether log writing is necessary, and if so,
2647 * performs the log write in the background, or waits if not. It is also
2648 * used to decide the background writeback of the superblock.
2652 static int nilfs_segctor_thread(void *arg)
2654 struct nilfs_sc_info *sci = (struct nilfs_sc_info *)arg;
2655 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2657 nilfs_info(sci->sc_super,
2658 "segctord starting. Construction interval = %lu seconds, CP frequency < %lu seconds",
2659 sci->sc_interval / HZ, sci->sc_mjcp_freq / HZ);
2663 while (!kthread_should_stop()) {
2668 if (freezing(current)) {
2673 prepare_to_wait(&sci->sc_wait_daemon, &wait,
2674 TASK_INTERRUPTIBLE);
2675 should_write = nilfs_log_write_required(sci, &mode);
2678 finish_wait(&sci->sc_wait_daemon, &wait);
2680 if (nilfs_sb_dirty(nilfs) && nilfs_sb_need_update(nilfs))
2681 set_nilfs_discontinued(nilfs);
2684 nilfs_segctor_thread_construct(sci, mode);
2688 spin_lock(&sci->sc_state_lock);
2689 sci->sc_task = NULL;
2690 timer_shutdown_sync(&sci->sc_timer);
2691 spin_unlock(&sci->sc_state_lock);
2696 * Setup & clean-up functions
2698 static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
2699 struct nilfs_root *root)
2701 struct the_nilfs *nilfs = sb->s_fs_info;
2702 struct nilfs_sc_info *sci;
2704 sci = kzalloc(sizeof(*sci), GFP_KERNEL);
2710 nilfs_get_root(root);
2711 sci->sc_root = root;
2713 init_waitqueue_head(&sci->sc_wait_request);
2714 init_waitqueue_head(&sci->sc_wait_daemon);
2715 spin_lock_init(&sci->sc_state_lock);
2716 INIT_LIST_HEAD(&sci->sc_dirty_files);
2717 INIT_LIST_HEAD(&sci->sc_segbufs);
2718 INIT_LIST_HEAD(&sci->sc_write_logs);
2719 INIT_LIST_HEAD(&sci->sc_gc_inodes);
2720 INIT_LIST_HEAD(&sci->sc_iput_queue);
2721 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
2723 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
2724 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
2725 sci->sc_watermark = NILFS_SC_DEFAULT_WATERMARK;
2727 if (nilfs->ns_interval)
2728 sci->sc_interval = HZ * nilfs->ns_interval;
2729 if (nilfs->ns_watermark)
2730 sci->sc_watermark = nilfs->ns_watermark;
2734 static void nilfs_segctor_write_out(struct nilfs_sc_info *sci)
2736 int ret, retrycount = NILFS_SC_CLEANUP_RETRY;
2739 * The segctord thread was stopped and its timer was removed.
2740 * But some tasks remain.
2743 struct nilfs_transaction_info ti;
2745 nilfs_transaction_lock(sci->sc_super, &ti, 0);
2746 ret = nilfs_segctor_construct(sci, SC_LSEG_SR);
2747 nilfs_transaction_unlock(sci->sc_super);
2749 flush_work(&sci->sc_iput_work);
2751 } while (ret && ret != -EROFS && retrycount-- > 0);
2755 * nilfs_segctor_destroy - destroy the segment constructor.
2756 * @sci: nilfs_sc_info
2758 * nilfs_segctor_destroy() kills the segctord thread and frees
2759 * the nilfs_sc_info struct.
2760 * Caller must hold the segment semaphore.
2762 static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
2764 struct the_nilfs *nilfs = sci->sc_super->s_fs_info;
2767 up_write(&nilfs->ns_segctor_sem);
2770 wake_up(&sci->sc_wait_daemon);
2771 kthread_stop(sci->sc_task);
2774 spin_lock(&sci->sc_state_lock);
2775 flag = ((sci->sc_state & NILFS_SEGCTOR_COMMIT) || sci->sc_flush_request
2776 || sci->sc_seq_request != sci->sc_seq_done);
2777 spin_unlock(&sci->sc_state_lock);
2780 * Forcibly wake up tasks waiting in nilfs_segctor_sync(), which can
2781 * be called from delayed iput() via nilfs_evict_inode() and can race
2782 * with the above log writer thread termination.
2784 nilfs_segctor_wakeup(sci, 0, true);
2786 if (flush_work(&sci->sc_iput_work))
2789 if (flag || !nilfs_segctor_confirm(sci))
2790 nilfs_segctor_write_out(sci);
2792 if (!list_empty(&sci->sc_dirty_files)) {
2793 nilfs_warn(sci->sc_super,
2794 "disposed unprocessed dirty file(s) when stopping log writer");
2795 nilfs_dispose_list(nilfs, &sci->sc_dirty_files, 1);
2798 if (!list_empty(&sci->sc_iput_queue)) {
2799 nilfs_warn(sci->sc_super,
2800 "disposed unprocessed inode(s) in iput queue when stopping log writer");
2801 nilfs_dispose_list(nilfs, &sci->sc_iput_queue, 1);
2804 WARN_ON(!list_empty(&sci->sc_segbufs));
2805 WARN_ON(!list_empty(&sci->sc_write_logs));
2807 nilfs_put_root(sci->sc_root);
2809 down_write(&nilfs->ns_segctor_sem);
2815 * nilfs_attach_log_writer - attach log writer
2816 * @sb: super block instance
2817 * @root: root object of the current filesystem tree
2819 * This allocates a log writer object, initializes it, and starts the
2822 * Return: 0 on success, or one of the following negative error codes on
2824 * * %-EINTR - Log writer thread creation failed due to interruption.
2825 * * %-ENOMEM - Insufficient memory available.
2827 int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)
2829 struct the_nilfs *nilfs = sb->s_fs_info;
2830 struct nilfs_sc_info *sci;
2831 struct task_struct *t;
2834 if (nilfs->ns_writer) {
2836 * This happens if the filesystem is made read-only by
2837 * __nilfs_error or nilfs_remount and then remounted
2838 * read/write. In these cases, reuse the existing
2844 sci = nilfs_segctor_new(sb, root);
2848 nilfs->ns_writer = sci;
2849 t = kthread_create(nilfs_segctor_thread, sci, "segctord");
2852 nilfs_err(sb, "error %d creating segctord thread", err);
2853 nilfs_detach_log_writer(sb);
2857 timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0);
2859 wake_up_process(sci->sc_task);
2864 * nilfs_detach_log_writer - destroy log writer
2865 * @sb: super block instance
2867 * This kills log writer daemon, frees the log writer object, and
2868 * destroys list of dirty files.
2870 void nilfs_detach_log_writer(struct super_block *sb)
2872 struct the_nilfs *nilfs = sb->s_fs_info;
2873 LIST_HEAD(garbage_list);
2875 down_write(&nilfs->ns_segctor_sem);
2876 if (nilfs->ns_writer) {
2877 nilfs_segctor_destroy(nilfs->ns_writer);
2878 nilfs->ns_writer = NULL;
2880 set_nilfs_purging(nilfs);
2882 /* Force to free the list of dirty files */
2883 spin_lock(&nilfs->ns_inode_lock);
2884 if (!list_empty(&nilfs->ns_dirty_files)) {
2885 list_splice_init(&nilfs->ns_dirty_files, &garbage_list);
2887 "disposed unprocessed dirty file(s) when detaching log writer");
2889 spin_unlock(&nilfs->ns_inode_lock);
2890 up_write(&nilfs->ns_segctor_sem);
2892 nilfs_dispose_list(nilfs, &garbage_list, 1);
2893 clear_nilfs_purging(nilfs);