1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/bio.h>
11 #include <linux/blkdev.h>
12 #include <linux/sched/mm.h>
13 #include <linux/prefetch.h>
14 #include <linux/kthread.h>
15 #include <linux/swap.h>
16 #include <linux/timer.h>
17 #include <linux/freezer.h>
18 #include <linux/sched/signal.h>
19 #include <linux/random.h>
26 #include <trace/events/f2fs.h>
28 #define __reverse_ffz(x) __reverse_ffs(~(x))
30 static struct kmem_cache *discard_entry_slab;
31 static struct kmem_cache *discard_cmd_slab;
32 static struct kmem_cache *sit_entry_set_slab;
33 static struct kmem_cache *revoke_entry_slab;
35 static unsigned long __reverse_ulong(unsigned char *str)
37 unsigned long tmp = 0;
38 int shift = 24, idx = 0;
40 #if BITS_PER_LONG == 64
44 tmp |= (unsigned long)str[idx++] << shift;
45 shift -= BITS_PER_BYTE;
51 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
52 * MSB and LSB are reversed in a byte by f2fs_set_bit.
54 static inline unsigned long __reverse_ffs(unsigned long word)
58 #if BITS_PER_LONG == 64
59 if ((word & 0xffffffff00000000UL) == 0)
64 if ((word & 0xffff0000) == 0)
69 if ((word & 0xff00) == 0)
74 if ((word & 0xf0) == 0)
79 if ((word & 0xc) == 0)
84 if ((word & 0x2) == 0)
90 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
91 * f2fs_set_bit makes MSB and LSB reversed in a byte.
92 * @size must be integral times of unsigned long.
95 * f2fs_set_bit(0, bitmap) => 1000 0000
96 * f2fs_set_bit(7, bitmap) => 0000 0001
98 static unsigned long __find_rev_next_bit(const unsigned long *addr,
99 unsigned long size, unsigned long offset)
101 const unsigned long *p = addr + BIT_WORD(offset);
102 unsigned long result = size;
108 size -= (offset & ~(BITS_PER_LONG - 1));
109 offset %= BITS_PER_LONG;
115 tmp = __reverse_ulong((unsigned char *)p);
117 tmp &= ~0UL >> offset;
118 if (size < BITS_PER_LONG)
119 tmp &= (~0UL << (BITS_PER_LONG - size));
123 if (size <= BITS_PER_LONG)
125 size -= BITS_PER_LONG;
131 return result - size + __reverse_ffs(tmp);
134 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
135 unsigned long size, unsigned long offset)
137 const unsigned long *p = addr + BIT_WORD(offset);
138 unsigned long result = size;
144 size -= (offset & ~(BITS_PER_LONG - 1));
145 offset %= BITS_PER_LONG;
151 tmp = __reverse_ulong((unsigned char *)p);
154 tmp |= ~0UL << (BITS_PER_LONG - offset);
155 if (size < BITS_PER_LONG)
160 if (size <= BITS_PER_LONG)
162 size -= BITS_PER_LONG;
168 return result - size + __reverse_ffz(tmp);
171 bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
173 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
174 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
175 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
177 if (f2fs_lfs_mode(sbi))
179 if (sbi->gc_mode == GC_URGENT_HIGH)
181 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
184 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
185 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
188 void f2fs_abort_atomic_write(struct inode *inode, bool clean)
190 struct f2fs_inode_info *fi = F2FS_I(inode);
192 if (!f2fs_is_atomic_file(inode))
196 truncate_inode_pages_final(inode->i_mapping);
198 release_atomic_write_cnt(inode);
199 clear_inode_flag(inode, FI_ATOMIC_COMMITTED);
200 clear_inode_flag(inode, FI_ATOMIC_FILE);
201 stat_dec_atomic_inode(inode);
203 F2FS_I(inode)->atomic_write_task = NULL;
206 f2fs_i_size_write(inode, fi->original_i_size);
207 fi->original_i_size = 0;
209 /* avoid stale dirty inode during eviction */
210 sync_inode_metadata(inode, 0);
213 static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,
214 block_t new_addr, block_t *old_addr, bool recover)
216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
217 struct dnode_of_data dn;
222 set_new_dnode(&dn, inode, NULL, NULL, 0);
223 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE_RA);
225 if (err == -ENOMEM) {
226 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
232 err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
239 /* dn.data_blkaddr is always valid */
240 if (!__is_valid_data_blkaddr(new_addr)) {
241 if (new_addr == NULL_ADDR)
242 dec_valid_block_count(sbi, inode, 1);
243 f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
244 f2fs_update_data_blkaddr(&dn, new_addr);
246 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
247 new_addr, ni.version, true, true);
252 err = inc_valid_block_count(sbi, inode, &count, true);
258 *old_addr = dn.data_blkaddr;
259 f2fs_truncate_data_blocks_range(&dn, 1);
260 dec_valid_block_count(sbi, F2FS_I(inode)->cow_inode, count);
262 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
263 ni.version, true, false);
268 trace_f2fs_replace_atomic_write_block(inode, F2FS_I(inode)->cow_inode,
269 index, old_addr ? *old_addr : 0, new_addr, recover);
273 static void __complete_revoke_list(struct inode *inode, struct list_head *head,
276 struct revoke_entry *cur, *tmp;
278 list_for_each_entry_safe(cur, tmp, head, list) {
280 __replace_atomic_write_block(inode, cur->index,
281 cur->old_addr, NULL, true);
282 list_del(&cur->list);
283 kmem_cache_free(revoke_entry_slab, cur);
287 static int __f2fs_commit_atomic_write(struct inode *inode)
289 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
290 struct f2fs_inode_info *fi = F2FS_I(inode);
291 struct inode *cow_inode = fi->cow_inode;
292 struct revoke_entry *new;
293 struct list_head revoke_list;
295 struct dnode_of_data dn;
296 pgoff_t len = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
297 pgoff_t off = 0, blen, index;
300 INIT_LIST_HEAD(&revoke_list);
303 blen = min_t(pgoff_t, ADDRS_PER_BLOCK(cow_inode), len);
305 set_new_dnode(&dn, cow_inode, NULL, NULL, 0);
306 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
307 if (ret && ret != -ENOENT) {
309 } else if (ret == -ENOENT) {
311 if (dn.max_level == 0)
316 blen = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, cow_inode),
319 for (i = 0; i < blen; i++, dn.ofs_in_node++, index++) {
320 blkaddr = f2fs_data_blkaddr(&dn);
322 if (!__is_valid_data_blkaddr(blkaddr)) {
324 } else if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
325 DATA_GENERIC_ENHANCE)) {
328 f2fs_handle_error(sbi,
329 ERROR_INVALID_BLKADDR);
333 new = f2fs_kmem_cache_alloc(revoke_entry_slab, GFP_NOFS,
336 ret = __replace_atomic_write_block(inode, index, blkaddr,
337 &new->old_addr, false);
340 kmem_cache_free(revoke_entry_slab, new);
344 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
346 list_add_tail(&new->list, &revoke_list);
356 sbi->revoked_atomic_block += fi->atomic_write_cnt;
358 sbi->committed_atomic_block += fi->atomic_write_cnt;
359 set_inode_flag(inode, FI_ATOMIC_COMMITTED);
362 __complete_revoke_list(inode, &revoke_list, ret ? true : false);
367 int f2fs_commit_atomic_write(struct inode *inode)
369 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
370 struct f2fs_inode_info *fi = F2FS_I(inode);
373 err = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
377 f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
380 err = __f2fs_commit_atomic_write(inode);
383 f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
389 * This function balances dirty node and dentry pages.
390 * In addition, it controls garbage collection.
392 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
394 if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
395 f2fs_show_injection_info(sbi, FAULT_CHECKPOINT);
396 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_FAULT_INJECT);
399 /* balance_fs_bg is able to be pending */
400 if (need && excess_cached_nats(sbi))
401 f2fs_balance_fs_bg(sbi, false);
403 if (!f2fs_is_checkpoint_ready(sbi))
407 * We should do GC or end up with checkpoint, if there are so many dirty
408 * dir/node pages without enough free segments.
410 if (has_not_enough_free_secs(sbi, 0, 0)) {
411 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread &&
412 sbi->gc_thread->f2fs_gc_task) {
415 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait,
416 TASK_UNINTERRUPTIBLE);
417 wake_up(&sbi->gc_thread->gc_wait_queue_head);
419 finish_wait(&sbi->gc_thread->fggc_wq, &wait);
421 struct f2fs_gc_control gc_control = {
422 .victim_segno = NULL_SEGNO,
423 .init_gc_type = BG_GC,
425 .should_migrate_blocks = false,
426 .err_gc_skipped = false,
428 f2fs_down_write(&sbi->gc_lock);
429 f2fs_gc(sbi, &gc_control);
434 static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi)
436 int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2;
437 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS);
438 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA);
439 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES);
440 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META);
441 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA);
442 unsigned int threshold = sbi->blocks_per_seg * factor *
443 DEFAULT_DIRTY_THRESHOLD;
444 unsigned int global_threshold = threshold * 3 / 2;
446 if (dents >= threshold || qdata >= threshold ||
447 nodes >= threshold || meta >= threshold ||
450 return dents + qdata + nodes + meta + imeta > global_threshold;
453 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg)
455 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
458 /* try to shrink extent cache when there is no enough memory */
459 if (!f2fs_available_free_memory(sbi, READ_EXTENT_CACHE))
460 f2fs_shrink_read_extent_tree(sbi,
461 READ_EXTENT_CACHE_SHRINK_NUMBER);
463 /* check the # of cached NAT entries */
464 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
465 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
467 if (!f2fs_available_free_memory(sbi, FREE_NIDS))
468 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
470 f2fs_build_free_nids(sbi, false, false);
472 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) ||
473 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi))
476 /* there is background inflight IO or foreground operation recently */
477 if (is_inflight_io(sbi, REQ_TIME) ||
478 (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem)))
481 /* exceed periodical checkpoint timeout threshold */
482 if (f2fs_time_over(sbi, CP_TIME))
485 /* checkpoint is the only way to shrink partial cached entries */
486 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) &&
487 f2fs_available_free_memory(sbi, INO_ENTRIES))
491 if (test_opt(sbi, DATA_FLUSH) && from_bg) {
492 struct blk_plug plug;
494 mutex_lock(&sbi->flush_lock);
496 blk_start_plug(&plug);
497 f2fs_sync_dirty_inodes(sbi, FILE_INODE, false);
498 blk_finish_plug(&plug);
500 mutex_unlock(&sbi->flush_lock);
502 f2fs_sync_fs(sbi->sb, 1);
503 stat_inc_bg_cp_count(sbi->stat_info);
506 static int __submit_flush_wait(struct f2fs_sb_info *sbi,
507 struct block_device *bdev)
509 int ret = blkdev_issue_flush(bdev);
511 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
512 test_opt(sbi, FLUSH_MERGE), ret);
516 static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
521 if (!f2fs_is_multi_device(sbi))
522 return __submit_flush_wait(sbi, sbi->sb->s_bdev);
524 for (i = 0; i < sbi->s_ndevs; i++) {
525 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
527 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
534 static int issue_flush_thread(void *data)
536 struct f2fs_sb_info *sbi = data;
537 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
538 wait_queue_head_t *q = &fcc->flush_wait_queue;
540 if (kthread_should_stop())
543 if (!llist_empty(&fcc->issue_list)) {
544 struct flush_cmd *cmd, *next;
547 fcc->dispatch_list = llist_del_all(&fcc->issue_list);
548 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
550 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
552 ret = submit_flush_wait(sbi, cmd->ino);
553 atomic_inc(&fcc->issued_flush);
555 llist_for_each_entry_safe(cmd, next,
556 fcc->dispatch_list, llnode) {
558 complete(&cmd->wait);
560 fcc->dispatch_list = NULL;
563 wait_event_interruptible(*q,
564 kthread_should_stop() || !llist_empty(&fcc->issue_list));
568 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
570 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
571 struct flush_cmd cmd;
574 if (test_opt(sbi, NOBARRIER))
577 if (!test_opt(sbi, FLUSH_MERGE)) {
578 atomic_inc(&fcc->queued_flush);
579 ret = submit_flush_wait(sbi, ino);
580 atomic_dec(&fcc->queued_flush);
581 atomic_inc(&fcc->issued_flush);
585 if (atomic_inc_return(&fcc->queued_flush) == 1 ||
586 f2fs_is_multi_device(sbi)) {
587 ret = submit_flush_wait(sbi, ino);
588 atomic_dec(&fcc->queued_flush);
590 atomic_inc(&fcc->issued_flush);
595 init_completion(&cmd.wait);
597 llist_add(&cmd.llnode, &fcc->issue_list);
600 * update issue_list before we wake up issue_flush thread, this
601 * smp_mb() pairs with another barrier in ___wait_event(), see
602 * more details in comments of waitqueue_active().
606 if (waitqueue_active(&fcc->flush_wait_queue))
607 wake_up(&fcc->flush_wait_queue);
609 if (fcc->f2fs_issue_flush) {
610 wait_for_completion(&cmd.wait);
611 atomic_dec(&fcc->queued_flush);
613 struct llist_node *list;
615 list = llist_del_all(&fcc->issue_list);
617 wait_for_completion(&cmd.wait);
618 atomic_dec(&fcc->queued_flush);
620 struct flush_cmd *tmp, *next;
622 ret = submit_flush_wait(sbi, ino);
624 llist_for_each_entry_safe(tmp, next, list, llnode) {
627 atomic_dec(&fcc->queued_flush);
631 complete(&tmp->wait);
639 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
641 dev_t dev = sbi->sb->s_bdev->bd_dev;
642 struct flush_cmd_control *fcc;
645 if (SM_I(sbi)->fcc_info) {
646 fcc = SM_I(sbi)->fcc_info;
647 if (fcc->f2fs_issue_flush)
652 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
655 atomic_set(&fcc->issued_flush, 0);
656 atomic_set(&fcc->queued_flush, 0);
657 init_waitqueue_head(&fcc->flush_wait_queue);
658 init_llist_head(&fcc->issue_list);
659 SM_I(sbi)->fcc_info = fcc;
660 if (!test_opt(sbi, FLUSH_MERGE))
664 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
665 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
666 if (IS_ERR(fcc->f2fs_issue_flush)) {
667 err = PTR_ERR(fcc->f2fs_issue_flush);
669 SM_I(sbi)->fcc_info = NULL;
676 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
678 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
680 if (fcc && fcc->f2fs_issue_flush) {
681 struct task_struct *flush_thread = fcc->f2fs_issue_flush;
683 fcc->f2fs_issue_flush = NULL;
684 kthread_stop(flush_thread);
688 SM_I(sbi)->fcc_info = NULL;
692 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
696 if (!f2fs_is_multi_device(sbi))
699 if (test_opt(sbi, NOBARRIER))
702 for (i = 1; i < sbi->s_ndevs; i++) {
703 int count = DEFAULT_RETRY_IO_COUNT;
705 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
709 ret = __submit_flush_wait(sbi, FDEV(i).bdev);
711 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
712 } while (ret && --count);
715 f2fs_stop_checkpoint(sbi, false,
716 STOP_CP_REASON_FLUSH_FAIL);
720 spin_lock(&sbi->dev_lock);
721 f2fs_clear_bit(i, (char *)&sbi->dirty_device);
722 spin_unlock(&sbi->dev_lock);
728 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
729 enum dirty_type dirty_type)
731 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
733 /* need not be added */
734 if (IS_CURSEG(sbi, segno))
737 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
738 dirty_i->nr_dirty[dirty_type]++;
740 if (dirty_type == DIRTY) {
741 struct seg_entry *sentry = get_seg_entry(sbi, segno);
742 enum dirty_type t = sentry->type;
744 if (unlikely(t >= DIRTY)) {
748 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
749 dirty_i->nr_dirty[t]++;
751 if (__is_large_section(sbi)) {
752 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
753 block_t valid_blocks =
754 get_valid_blocks(sbi, segno, true);
756 f2fs_bug_on(sbi, unlikely(!valid_blocks ||
757 valid_blocks == CAP_BLKS_PER_SEC(sbi)));
759 if (!IS_CURSEC(sbi, secno))
760 set_bit(secno, dirty_i->dirty_secmap);
765 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
766 enum dirty_type dirty_type)
768 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
769 block_t valid_blocks;
771 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
772 dirty_i->nr_dirty[dirty_type]--;
774 if (dirty_type == DIRTY) {
775 struct seg_entry *sentry = get_seg_entry(sbi, segno);
776 enum dirty_type t = sentry->type;
778 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
779 dirty_i->nr_dirty[t]--;
781 valid_blocks = get_valid_blocks(sbi, segno, true);
782 if (valid_blocks == 0) {
783 clear_bit(GET_SEC_FROM_SEG(sbi, segno),
784 dirty_i->victim_secmap);
785 #ifdef CONFIG_F2FS_CHECK_FS
786 clear_bit(segno, SIT_I(sbi)->invalid_segmap);
789 if (__is_large_section(sbi)) {
790 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
793 valid_blocks == CAP_BLKS_PER_SEC(sbi)) {
794 clear_bit(secno, dirty_i->dirty_secmap);
798 if (!IS_CURSEC(sbi, secno))
799 set_bit(secno, dirty_i->dirty_secmap);
805 * Should not occur error such as -ENOMEM.
806 * Adding dirty entry into seglist is not critical operation.
807 * If a given segment is one of current working segments, it won't be added.
809 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
811 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
812 unsigned short valid_blocks, ckpt_valid_blocks;
813 unsigned int usable_blocks;
815 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
818 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno);
819 mutex_lock(&dirty_i->seglist_lock);
821 valid_blocks = get_valid_blocks(sbi, segno, false);
822 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false);
824 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) ||
825 ckpt_valid_blocks == usable_blocks)) {
826 __locate_dirty_segment(sbi, segno, PRE);
827 __remove_dirty_segment(sbi, segno, DIRTY);
828 } else if (valid_blocks < usable_blocks) {
829 __locate_dirty_segment(sbi, segno, DIRTY);
831 /* Recovery routine with SSR needs this */
832 __remove_dirty_segment(sbi, segno, DIRTY);
835 mutex_unlock(&dirty_i->seglist_lock);
838 /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */
839 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi)
841 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
844 mutex_lock(&dirty_i->seglist_lock);
845 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
846 if (get_valid_blocks(sbi, segno, false))
848 if (IS_CURSEG(sbi, segno))
850 __locate_dirty_segment(sbi, segno, PRE);
851 __remove_dirty_segment(sbi, segno, DIRTY);
853 mutex_unlock(&dirty_i->seglist_lock);
856 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi)
859 (overprovision_segments(sbi) - reserved_segments(sbi));
860 block_t ovp_holes = ovp_hole_segs << sbi->log_blocks_per_seg;
861 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
862 block_t holes[2] = {0, 0}; /* DATA and NODE */
864 struct seg_entry *se;
867 mutex_lock(&dirty_i->seglist_lock);
868 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
869 se = get_seg_entry(sbi, segno);
870 if (IS_NODESEG(se->type))
871 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) -
874 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) -
877 mutex_unlock(&dirty_i->seglist_lock);
879 unusable = holes[DATA] > holes[NODE] ? holes[DATA] : holes[NODE];
880 if (unusable > ovp_holes)
881 return unusable - ovp_holes;
885 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable)
888 (overprovision_segments(sbi) - reserved_segments(sbi));
889 if (unusable > F2FS_OPTION(sbi).unusable_cap)
891 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) &&
892 dirty_segments(sbi) > ovp_hole_segs)
897 /* This is only used by SBI_CP_DISABLED */
898 static unsigned int get_free_segment(struct f2fs_sb_info *sbi)
900 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
901 unsigned int segno = 0;
903 mutex_lock(&dirty_i->seglist_lock);
904 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) {
905 if (get_valid_blocks(sbi, segno, false))
907 if (get_ckpt_valid_blocks(sbi, segno, false))
909 mutex_unlock(&dirty_i->seglist_lock);
912 mutex_unlock(&dirty_i->seglist_lock);
916 static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
917 struct block_device *bdev, block_t lstart,
918 block_t start, block_t len)
920 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
921 struct list_head *pend_list;
922 struct discard_cmd *dc;
924 f2fs_bug_on(sbi, !len);
926 pend_list = &dcc->pend_list[plist_idx(len)];
928 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL);
929 INIT_LIST_HEAD(&dc->list);
938 init_completion(&dc->wait);
939 list_add_tail(&dc->list, pend_list);
940 spin_lock_init(&dc->lock);
942 atomic_inc(&dcc->discard_cmd_cnt);
943 dcc->undiscard_blks += len;
948 static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
949 struct block_device *bdev, block_t lstart,
950 block_t start, block_t len,
951 struct rb_node *parent, struct rb_node **p,
954 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
955 struct discard_cmd *dc;
957 dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
959 rb_link_node(&dc->rb_node, parent, p);
960 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost);
965 static void __detach_discard_cmd(struct discard_cmd_control *dcc,
966 struct discard_cmd *dc)
968 if (dc->state == D_DONE)
969 atomic_sub(dc->queued, &dcc->queued_discard);
972 rb_erase_cached(&dc->rb_node, &dcc->root);
973 dcc->undiscard_blks -= dc->len;
975 kmem_cache_free(discard_cmd_slab, dc);
977 atomic_dec(&dcc->discard_cmd_cnt);
980 static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
981 struct discard_cmd *dc)
983 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
986 trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
988 spin_lock_irqsave(&dc->lock, flags);
990 spin_unlock_irqrestore(&dc->lock, flags);
993 spin_unlock_irqrestore(&dc->lock, flags);
995 f2fs_bug_on(sbi, dc->ref);
997 if (dc->error == -EOPNOTSUPP)
1002 "%sF2FS-fs (%s): Issue discard(%u, %u, %u) failed, ret: %d",
1003 KERN_INFO, sbi->sb->s_id,
1004 dc->lstart, dc->start, dc->len, dc->error);
1005 __detach_discard_cmd(dcc, dc);
1008 static void f2fs_submit_discard_endio(struct bio *bio)
1010 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
1011 unsigned long flags;
1013 spin_lock_irqsave(&dc->lock, flags);
1015 dc->error = blk_status_to_errno(bio->bi_status);
1017 if (!dc->bio_ref && dc->state == D_SUBMIT) {
1019 complete_all(&dc->wait);
1021 spin_unlock_irqrestore(&dc->lock, flags);
1025 static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
1026 block_t start, block_t end)
1028 #ifdef CONFIG_F2FS_CHECK_FS
1029 struct seg_entry *sentry;
1031 block_t blk = start;
1032 unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
1036 segno = GET_SEGNO(sbi, blk);
1037 sentry = get_seg_entry(sbi, segno);
1038 offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
1040 if (end < START_BLOCK(sbi, segno + 1))
1041 size = GET_BLKOFF_FROM_SEG0(sbi, end);
1044 map = (unsigned long *)(sentry->cur_valid_map);
1045 offset = __find_rev_next_bit(map, size, offset);
1046 f2fs_bug_on(sbi, offset != size);
1047 blk = START_BLOCK(sbi, segno + 1);
1052 static void __init_discard_policy(struct f2fs_sb_info *sbi,
1053 struct discard_policy *dpolicy,
1054 int discard_type, unsigned int granularity)
1056 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1059 dpolicy->type = discard_type;
1060 dpolicy->sync = true;
1061 dpolicy->ordered = false;
1062 dpolicy->granularity = granularity;
1064 dpolicy->max_requests = dcc->max_discard_request;
1065 dpolicy->io_aware_gran = MAX_PLIST_NUM;
1066 dpolicy->timeout = false;
1068 if (discard_type == DPOLICY_BG) {
1069 dpolicy->min_interval = dcc->min_discard_issue_time;
1070 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1071 dpolicy->max_interval = dcc->max_discard_issue_time;
1072 dpolicy->io_aware = true;
1073 dpolicy->sync = false;
1074 dpolicy->ordered = true;
1075 if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
1076 dpolicy->granularity = 1;
1077 if (atomic_read(&dcc->discard_cmd_cnt))
1078 dpolicy->max_interval =
1079 dcc->min_discard_issue_time;
1081 } else if (discard_type == DPOLICY_FORCE) {
1082 dpolicy->min_interval = dcc->min_discard_issue_time;
1083 dpolicy->mid_interval = dcc->mid_discard_issue_time;
1084 dpolicy->max_interval = dcc->max_discard_issue_time;
1085 dpolicy->io_aware = false;
1086 } else if (discard_type == DPOLICY_FSTRIM) {
1087 dpolicy->io_aware = false;
1088 } else if (discard_type == DPOLICY_UMOUNT) {
1089 dpolicy->io_aware = false;
1090 /* we need to issue all to keep CP_TRIMMED_FLAG */
1091 dpolicy->granularity = 1;
1092 dpolicy->timeout = true;
1096 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1097 struct block_device *bdev, block_t lstart,
1098 block_t start, block_t len);
1099 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
1100 static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
1101 struct discard_policy *dpolicy,
1102 struct discard_cmd *dc,
1103 unsigned int *issued)
1105 struct block_device *bdev = dc->bdev;
1106 unsigned int max_discard_blocks =
1107 SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev));
1108 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1109 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1110 &(dcc->fstrim_list) : &(dcc->wait_list);
1111 blk_opf_t flag = dpolicy->sync ? REQ_SYNC : 0;
1112 block_t lstart, start, len, total_len;
1115 if (dc->state != D_PREP)
1118 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
1121 trace_f2fs_issue_discard(bdev, dc->start, dc->len);
1123 lstart = dc->lstart;
1130 while (total_len && *issued < dpolicy->max_requests && !err) {
1131 struct bio *bio = NULL;
1132 unsigned long flags;
1135 if (len > max_discard_blocks) {
1136 len = max_discard_blocks;
1141 if (*issued == dpolicy->max_requests)
1146 if (time_to_inject(sbi, FAULT_DISCARD)) {
1147 f2fs_show_injection_info(sbi, FAULT_DISCARD);
1151 err = __blkdev_issue_discard(bdev,
1152 SECTOR_FROM_BLOCK(start),
1153 SECTOR_FROM_BLOCK(len),
1157 spin_lock_irqsave(&dc->lock, flags);
1158 if (dc->state == D_PARTIAL)
1159 dc->state = D_SUBMIT;
1160 spin_unlock_irqrestore(&dc->lock, flags);
1165 f2fs_bug_on(sbi, !bio);
1168 * should keep before submission to avoid D_DONE
1171 spin_lock_irqsave(&dc->lock, flags);
1173 dc->state = D_SUBMIT;
1175 dc->state = D_PARTIAL;
1177 spin_unlock_irqrestore(&dc->lock, flags);
1179 atomic_inc(&dcc->queued_discard);
1181 list_move_tail(&dc->list, wait_list);
1183 /* sanity check on discard range */
1184 __check_sit_bitmap(sbi, lstart, lstart + len);
1186 bio->bi_private = dc;
1187 bio->bi_end_io = f2fs_submit_discard_endio;
1188 bio->bi_opf |= flag;
1191 atomic_inc(&dcc->issued_discard);
1193 f2fs_update_iostat(sbi, NULL, FS_DISCARD, len * F2FS_BLKSIZE);
1202 dcc->undiscard_blks -= len;
1203 __update_discard_tree_range(sbi, bdev, lstart, start, len);
1208 static void __insert_discard_tree(struct f2fs_sb_info *sbi,
1209 struct block_device *bdev, block_t lstart,
1210 block_t start, block_t len,
1211 struct rb_node **insert_p,
1212 struct rb_node *insert_parent)
1214 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1216 struct rb_node *parent = NULL;
1217 bool leftmost = true;
1219 if (insert_p && insert_parent) {
1220 parent = insert_parent;
1225 p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent,
1228 __attach_discard_cmd(sbi, bdev, lstart, start, len, parent,
1232 static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
1233 struct discard_cmd *dc)
1235 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
1238 static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
1239 struct discard_cmd *dc, block_t blkaddr)
1241 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1242 struct discard_info di = dc->di;
1243 bool modified = false;
1245 if (dc->state == D_DONE || dc->len == 1) {
1246 __remove_discard_cmd(sbi, dc);
1250 dcc->undiscard_blks -= di.len;
1252 if (blkaddr > di.lstart) {
1253 dc->len = blkaddr - dc->lstart;
1254 dcc->undiscard_blks += dc->len;
1255 __relocate_discard_cmd(dcc, dc);
1259 if (blkaddr < di.lstart + di.len - 1) {
1261 __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
1262 di.start + blkaddr + 1 - di.lstart,
1263 di.lstart + di.len - 1 - blkaddr,
1269 dcc->undiscard_blks += dc->len;
1270 __relocate_discard_cmd(dcc, dc);
1275 static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
1276 struct block_device *bdev, block_t lstart,
1277 block_t start, block_t len)
1279 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1280 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1281 struct discard_cmd *dc;
1282 struct discard_info di = {0};
1283 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1284 unsigned int max_discard_blocks =
1285 SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev));
1286 block_t end = lstart + len;
1288 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1290 (struct rb_entry **)&prev_dc,
1291 (struct rb_entry **)&next_dc,
1292 &insert_p, &insert_parent, true, NULL);
1298 di.len = next_dc ? next_dc->lstart - lstart : len;
1299 di.len = min(di.len, len);
1304 struct rb_node *node;
1305 bool merged = false;
1306 struct discard_cmd *tdc = NULL;
1309 di.lstart = prev_dc->lstart + prev_dc->len;
1310 if (di.lstart < lstart)
1312 if (di.lstart >= end)
1315 if (!next_dc || next_dc->lstart > end)
1316 di.len = end - di.lstart;
1318 di.len = next_dc->lstart - di.lstart;
1319 di.start = start + di.lstart - lstart;
1325 if (prev_dc && prev_dc->state == D_PREP &&
1326 prev_dc->bdev == bdev &&
1327 __is_discard_back_mergeable(&di, &prev_dc->di,
1328 max_discard_blocks)) {
1329 prev_dc->di.len += di.len;
1330 dcc->undiscard_blks += di.len;
1331 __relocate_discard_cmd(dcc, prev_dc);
1337 if (next_dc && next_dc->state == D_PREP &&
1338 next_dc->bdev == bdev &&
1339 __is_discard_front_mergeable(&di, &next_dc->di,
1340 max_discard_blocks)) {
1341 next_dc->di.lstart = di.lstart;
1342 next_dc->di.len += di.len;
1343 next_dc->di.start = di.start;
1344 dcc->undiscard_blks += di.len;
1345 __relocate_discard_cmd(dcc, next_dc);
1347 __remove_discard_cmd(sbi, tdc);
1352 __insert_discard_tree(sbi, bdev, di.lstart, di.start,
1353 di.len, NULL, NULL);
1360 node = rb_next(&prev_dc->rb_node);
1361 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1365 static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
1366 struct block_device *bdev, block_t blkstart, block_t blklen)
1368 block_t lblkstart = blkstart;
1370 if (!f2fs_bdev_support_discard(bdev))
1373 trace_f2fs_queue_discard(bdev, blkstart, blklen);
1375 if (f2fs_is_multi_device(sbi)) {
1376 int devi = f2fs_target_device_index(sbi, blkstart);
1378 blkstart -= FDEV(devi).start_blk;
1380 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
1381 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
1382 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
1386 static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
1387 struct discard_policy *dpolicy)
1389 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1390 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
1391 struct rb_node **insert_p = NULL, *insert_parent = NULL;
1392 struct discard_cmd *dc;
1393 struct blk_plug plug;
1394 unsigned int pos = dcc->next_pos;
1395 unsigned int issued = 0;
1396 bool io_interrupted = false;
1398 mutex_lock(&dcc->cmd_lock);
1399 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
1401 (struct rb_entry **)&prev_dc,
1402 (struct rb_entry **)&next_dc,
1403 &insert_p, &insert_parent, true, NULL);
1407 blk_start_plug(&plug);
1410 struct rb_node *node;
1413 if (dc->state != D_PREP)
1416 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) {
1417 io_interrupted = true;
1421 dcc->next_pos = dc->lstart + dc->len;
1422 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1424 if (issued >= dpolicy->max_requests)
1427 node = rb_next(&dc->rb_node);
1429 __remove_discard_cmd(sbi, dc);
1430 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
1433 blk_finish_plug(&plug);
1438 mutex_unlock(&dcc->cmd_lock);
1440 if (!issued && io_interrupted)
1445 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1446 struct discard_policy *dpolicy);
1448 static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
1449 struct discard_policy *dpolicy)
1451 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1452 struct list_head *pend_list;
1453 struct discard_cmd *dc, *tmp;
1454 struct blk_plug plug;
1456 bool io_interrupted = false;
1458 if (dpolicy->timeout)
1459 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT);
1463 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1464 if (dpolicy->timeout &&
1465 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1468 if (i + 1 < dpolicy->granularity)
1471 if (i + 1 < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
1472 return __issue_discard_cmd_orderly(sbi, dpolicy);
1474 pend_list = &dcc->pend_list[i];
1476 mutex_lock(&dcc->cmd_lock);
1477 if (list_empty(pend_list))
1479 if (unlikely(dcc->rbtree_check))
1480 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
1482 blk_start_plug(&plug);
1483 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1484 f2fs_bug_on(sbi, dc->state != D_PREP);
1486 if (dpolicy->timeout &&
1487 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT))
1490 if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
1491 !is_idle(sbi, DISCARD_TIME)) {
1492 io_interrupted = true;
1496 __submit_discard_cmd(sbi, dpolicy, dc, &issued);
1498 if (issued >= dpolicy->max_requests)
1501 blk_finish_plug(&plug);
1503 mutex_unlock(&dcc->cmd_lock);
1505 if (issued >= dpolicy->max_requests || io_interrupted)
1509 if (dpolicy->type == DPOLICY_UMOUNT && issued) {
1510 __wait_all_discard_cmd(sbi, dpolicy);
1514 if (!issued && io_interrupted)
1520 static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
1522 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1523 struct list_head *pend_list;
1524 struct discard_cmd *dc, *tmp;
1526 bool dropped = false;
1528 mutex_lock(&dcc->cmd_lock);
1529 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1530 pend_list = &dcc->pend_list[i];
1531 list_for_each_entry_safe(dc, tmp, pend_list, list) {
1532 f2fs_bug_on(sbi, dc->state != D_PREP);
1533 __remove_discard_cmd(sbi, dc);
1537 mutex_unlock(&dcc->cmd_lock);
1542 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
1544 __drop_discard_cmd(sbi);
1547 static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
1548 struct discard_cmd *dc)
1550 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1551 unsigned int len = 0;
1553 wait_for_completion_io(&dc->wait);
1554 mutex_lock(&dcc->cmd_lock);
1555 f2fs_bug_on(sbi, dc->state != D_DONE);
1560 __remove_discard_cmd(sbi, dc);
1562 mutex_unlock(&dcc->cmd_lock);
1567 static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
1568 struct discard_policy *dpolicy,
1569 block_t start, block_t end)
1571 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1572 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
1573 &(dcc->fstrim_list) : &(dcc->wait_list);
1574 struct discard_cmd *dc = NULL, *iter, *tmp;
1575 unsigned int trimmed = 0;
1580 mutex_lock(&dcc->cmd_lock);
1581 list_for_each_entry_safe(iter, tmp, wait_list, list) {
1582 if (iter->lstart + iter->len <= start || end <= iter->lstart)
1584 if (iter->len < dpolicy->granularity)
1586 if (iter->state == D_DONE && !iter->ref) {
1587 wait_for_completion_io(&iter->wait);
1589 trimmed += iter->len;
1590 __remove_discard_cmd(sbi, iter);
1597 mutex_unlock(&dcc->cmd_lock);
1600 trimmed += __wait_one_discard_bio(sbi, dc);
1607 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
1608 struct discard_policy *dpolicy)
1610 struct discard_policy dp;
1611 unsigned int discard_blks;
1614 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
1617 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
1618 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1619 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
1620 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
1622 return discard_blks;
1625 /* This should be covered by global mutex, &sit_i->sentry_lock */
1626 static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
1628 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1629 struct discard_cmd *dc;
1630 bool need_wait = false;
1632 mutex_lock(&dcc->cmd_lock);
1633 dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
1636 if (dc->state == D_PREP) {
1637 __punch_discard_cmd(sbi, dc, blkaddr);
1643 mutex_unlock(&dcc->cmd_lock);
1646 __wait_one_discard_bio(sbi, dc);
1649 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
1651 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1653 if (dcc && dcc->f2fs_issue_discard) {
1654 struct task_struct *discard_thread = dcc->f2fs_issue_discard;
1656 dcc->f2fs_issue_discard = NULL;
1657 kthread_stop(discard_thread);
1661 /* This comes from f2fs_put_super */
1662 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi)
1664 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1665 struct discard_policy dpolicy;
1668 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
1669 dcc->discard_granularity);
1670 __issue_discard_cmd(sbi, &dpolicy);
1671 dropped = __drop_discard_cmd(sbi);
1673 /* just to make sure there is no pending discard commands */
1674 __wait_all_discard_cmd(sbi, NULL);
1676 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
1680 static int issue_discard_thread(void *data)
1682 struct f2fs_sb_info *sbi = data;
1683 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1684 wait_queue_head_t *q = &dcc->discard_wait_queue;
1685 struct discard_policy dpolicy;
1686 unsigned int wait_ms = dcc->min_discard_issue_time;
1692 if (sbi->gc_mode == GC_URGENT_HIGH ||
1693 !f2fs_available_free_memory(sbi, DISCARD_CACHE))
1694 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
1696 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
1697 dcc->discard_granularity);
1699 if (!atomic_read(&dcc->discard_cmd_cnt))
1700 wait_ms = dpolicy.max_interval;
1702 wait_event_interruptible_timeout(*q,
1703 kthread_should_stop() || freezing(current) ||
1705 msecs_to_jiffies(wait_ms));
1707 if (dcc->discard_wake)
1708 dcc->discard_wake = 0;
1710 /* clean up pending candidates before going to sleep */
1711 if (atomic_read(&dcc->queued_discard))
1712 __wait_all_discard_cmd(sbi, NULL);
1714 if (try_to_freeze())
1716 if (f2fs_readonly(sbi->sb))
1718 if (kthread_should_stop())
1720 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
1721 wait_ms = dpolicy.max_interval;
1724 if (!atomic_read(&dcc->discard_cmd_cnt))
1727 sb_start_intwrite(sbi->sb);
1729 issued = __issue_discard_cmd(sbi, &dpolicy);
1731 __wait_all_discard_cmd(sbi, &dpolicy);
1732 wait_ms = dpolicy.min_interval;
1733 } else if (issued == -1) {
1734 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME);
1736 wait_ms = dpolicy.mid_interval;
1738 wait_ms = dpolicy.max_interval;
1741 sb_end_intwrite(sbi->sb);
1743 } while (!kthread_should_stop());
1747 #ifdef CONFIG_BLK_DEV_ZONED
1748 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
1749 struct block_device *bdev, block_t blkstart, block_t blklen)
1751 sector_t sector, nr_sects;
1752 block_t lblkstart = blkstart;
1755 if (f2fs_is_multi_device(sbi)) {
1756 devi = f2fs_target_device_index(sbi, blkstart);
1757 if (blkstart < FDEV(devi).start_blk ||
1758 blkstart > FDEV(devi).end_blk) {
1759 f2fs_err(sbi, "Invalid block %x", blkstart);
1762 blkstart -= FDEV(devi).start_blk;
1765 /* For sequential zones, reset the zone write pointer */
1766 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) {
1767 sector = SECTOR_FROM_BLOCK(blkstart);
1768 nr_sects = SECTOR_FROM_BLOCK(blklen);
1770 if (sector & (bdev_zone_sectors(bdev) - 1) ||
1771 nr_sects != bdev_zone_sectors(bdev)) {
1772 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)",
1773 devi, sbi->s_ndevs ? FDEV(devi).path : "",
1777 trace_f2fs_issue_reset_zone(bdev, blkstart);
1778 return blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET,
1779 sector, nr_sects, GFP_NOFS);
1782 /* For conventional zones, use regular discard if supported */
1783 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
1787 static int __issue_discard_async(struct f2fs_sb_info *sbi,
1788 struct block_device *bdev, block_t blkstart, block_t blklen)
1790 #ifdef CONFIG_BLK_DEV_ZONED
1791 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev))
1792 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
1794 return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
1797 static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
1798 block_t blkstart, block_t blklen)
1800 sector_t start = blkstart, len = 0;
1801 struct block_device *bdev;
1802 struct seg_entry *se;
1803 unsigned int offset;
1807 bdev = f2fs_target_device(sbi, blkstart, NULL);
1809 for (i = blkstart; i < blkstart + blklen; i++, len++) {
1811 struct block_device *bdev2 =
1812 f2fs_target_device(sbi, i, NULL);
1814 if (bdev2 != bdev) {
1815 err = __issue_discard_async(sbi, bdev,
1825 se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
1826 offset = GET_BLKOFF_FROM_SEG0(sbi, i);
1828 if (f2fs_block_unit_discard(sbi) &&
1829 !f2fs_test_and_set_bit(offset, se->discard_map))
1830 sbi->discard_blks--;
1834 err = __issue_discard_async(sbi, bdev, start, len);
1838 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
1841 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
1842 int max_blocks = sbi->blocks_per_seg;
1843 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
1844 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
1845 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
1846 unsigned long *discard_map = (unsigned long *)se->discard_map;
1847 unsigned long *dmap = SIT_I(sbi)->tmp_map;
1848 unsigned int start = 0, end = -1;
1849 bool force = (cpc->reason & CP_DISCARD);
1850 struct discard_entry *de = NULL;
1851 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
1854 if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi) ||
1855 !f2fs_block_unit_discard(sbi))
1859 if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
1860 SM_I(sbi)->dcc_info->nr_discards >=
1861 SM_I(sbi)->dcc_info->max_discards)
1865 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
1866 for (i = 0; i < entries; i++)
1867 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
1868 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
1870 while (force || SM_I(sbi)->dcc_info->nr_discards <=
1871 SM_I(sbi)->dcc_info->max_discards) {
1872 start = __find_rev_next_bit(dmap, max_blocks, end + 1);
1873 if (start >= max_blocks)
1876 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
1877 if (force && start && end != max_blocks
1878 && (end - start) < cpc->trim_minlen)
1885 de = f2fs_kmem_cache_alloc(discard_entry_slab,
1886 GFP_F2FS_ZERO, true, NULL);
1887 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
1888 list_add_tail(&de->list, head);
1891 for (i = start; i < end; i++)
1892 __set_bit_le(i, (void *)de->discard_map);
1894 SM_I(sbi)->dcc_info->nr_discards += end - start;
1899 static void release_discard_addr(struct discard_entry *entry)
1901 list_del(&entry->list);
1902 kmem_cache_free(discard_entry_slab, entry);
1905 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
1907 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
1908 struct discard_entry *entry, *this;
1911 list_for_each_entry_safe(entry, this, head, list)
1912 release_discard_addr(entry);
1916 * Should call f2fs_clear_prefree_segments after checkpoint is done.
1918 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
1920 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1923 mutex_lock(&dirty_i->seglist_lock);
1924 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
1925 __set_test_and_free(sbi, segno, false);
1926 mutex_unlock(&dirty_i->seglist_lock);
1929 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
1930 struct cp_control *cpc)
1932 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1933 struct list_head *head = &dcc->entry_list;
1934 struct discard_entry *entry, *this;
1935 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
1936 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
1937 unsigned int start = 0, end = -1;
1938 unsigned int secno, start_segno;
1939 bool force = (cpc->reason & CP_DISCARD);
1940 bool section_alignment = F2FS_OPTION(sbi).discard_unit ==
1941 DISCARD_UNIT_SECTION;
1943 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi))
1944 section_alignment = true;
1946 mutex_lock(&dirty_i->seglist_lock);
1951 if (section_alignment && end != -1)
1953 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
1954 if (start >= MAIN_SEGS(sbi))
1956 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
1959 if (section_alignment) {
1960 start = rounddown(start, sbi->segs_per_sec);
1961 end = roundup(end, sbi->segs_per_sec);
1964 for (i = start; i < end; i++) {
1965 if (test_and_clear_bit(i, prefree_map))
1966 dirty_i->nr_dirty[PRE]--;
1969 if (!f2fs_realtime_discard_enable(sbi))
1972 if (force && start >= cpc->trim_start &&
1973 (end - 1) <= cpc->trim_end)
1976 if (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi)) {
1977 f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
1978 (end - start) << sbi->log_blocks_per_seg);
1982 secno = GET_SEC_FROM_SEG(sbi, start);
1983 start_segno = GET_SEG_FROM_SEC(sbi, secno);
1984 if (!IS_CURSEC(sbi, secno) &&
1985 !get_valid_blocks(sbi, start, true))
1986 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
1987 sbi->segs_per_sec << sbi->log_blocks_per_seg);
1989 start = start_segno + sbi->segs_per_sec;
1995 mutex_unlock(&dirty_i->seglist_lock);
1997 if (!f2fs_block_unit_discard(sbi))
2000 /* send small discards */
2001 list_for_each_entry_safe(entry, this, head, list) {
2002 unsigned int cur_pos = 0, next_pos, len, total_len = 0;
2003 bool is_valid = test_bit_le(0, entry->discard_map);
2007 next_pos = find_next_zero_bit_le(entry->discard_map,
2008 sbi->blocks_per_seg, cur_pos);
2009 len = next_pos - cur_pos;
2011 if (f2fs_sb_has_blkzoned(sbi) ||
2012 (force && len < cpc->trim_minlen))
2015 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
2019 next_pos = find_next_bit_le(entry->discard_map,
2020 sbi->blocks_per_seg, cur_pos);
2024 is_valid = !is_valid;
2026 if (cur_pos < sbi->blocks_per_seg)
2029 release_discard_addr(entry);
2030 dcc->nr_discards -= total_len;
2034 wake_up_discard_thread(sbi, false);
2037 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi)
2039 dev_t dev = sbi->sb->s_bdev->bd_dev;
2040 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2043 if (!f2fs_realtime_discard_enable(sbi))
2046 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
2047 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
2048 if (IS_ERR(dcc->f2fs_issue_discard)) {
2049 err = PTR_ERR(dcc->f2fs_issue_discard);
2050 dcc->f2fs_issue_discard = NULL;
2056 static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
2058 struct discard_cmd_control *dcc;
2061 if (SM_I(sbi)->dcc_info) {
2062 dcc = SM_I(sbi)->dcc_info;
2066 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
2070 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
2071 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
2072 dcc->discard_granularity = sbi->blocks_per_seg;
2073 else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
2074 dcc->discard_granularity = BLKS_PER_SEC(sbi);
2076 INIT_LIST_HEAD(&dcc->entry_list);
2077 for (i = 0; i < MAX_PLIST_NUM; i++)
2078 INIT_LIST_HEAD(&dcc->pend_list[i]);
2079 INIT_LIST_HEAD(&dcc->wait_list);
2080 INIT_LIST_HEAD(&dcc->fstrim_list);
2081 mutex_init(&dcc->cmd_lock);
2082 atomic_set(&dcc->issued_discard, 0);
2083 atomic_set(&dcc->queued_discard, 0);
2084 atomic_set(&dcc->discard_cmd_cnt, 0);
2085 dcc->nr_discards = 0;
2086 dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
2087 dcc->max_discard_request = DEF_MAX_DISCARD_REQUEST;
2088 dcc->min_discard_issue_time = DEF_MIN_DISCARD_ISSUE_TIME;
2089 dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME;
2090 dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME;
2091 dcc->undiscard_blks = 0;
2093 dcc->root = RB_ROOT_CACHED;
2094 dcc->rbtree_check = false;
2096 init_waitqueue_head(&dcc->discard_wait_queue);
2097 SM_I(sbi)->dcc_info = dcc;
2099 err = f2fs_start_discard_thread(sbi);
2102 SM_I(sbi)->dcc_info = NULL;
2108 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
2110 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2115 f2fs_stop_discard_thread(sbi);
2118 * Recovery can cache discard commands, so in error path of
2119 * fill_super(), it needs to give a chance to handle them.
2121 if (unlikely(atomic_read(&dcc->discard_cmd_cnt)))
2122 f2fs_issue_discard_timeout(sbi);
2125 SM_I(sbi)->dcc_info = NULL;
2128 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
2130 struct sit_info *sit_i = SIT_I(sbi);
2132 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
2133 sit_i->dirty_sentries++;
2140 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
2141 unsigned int segno, int modified)
2143 struct seg_entry *se = get_seg_entry(sbi, segno);
2147 __mark_sit_entry_dirty(sbi, segno);
2150 static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi,
2153 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2155 if (segno == NULL_SEGNO)
2157 return get_seg_entry(sbi, segno)->mtime;
2160 static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr,
2161 unsigned long long old_mtime)
2163 struct seg_entry *se;
2164 unsigned int segno = GET_SEGNO(sbi, blkaddr);
2165 unsigned long long ctime = get_mtime(sbi, false);
2166 unsigned long long mtime = old_mtime ? old_mtime : ctime;
2168 if (segno == NULL_SEGNO)
2171 se = get_seg_entry(sbi, segno);
2176 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime,
2177 se->valid_blocks + 1);
2179 if (ctime > SIT_I(sbi)->max_mtime)
2180 SIT_I(sbi)->max_mtime = ctime;
2183 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
2185 struct seg_entry *se;
2186 unsigned int segno, offset;
2187 long int new_vblocks;
2189 #ifdef CONFIG_F2FS_CHECK_FS
2193 segno = GET_SEGNO(sbi, blkaddr);
2195 se = get_seg_entry(sbi, segno);
2196 new_vblocks = se->valid_blocks + del;
2197 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2199 f2fs_bug_on(sbi, (new_vblocks < 0 ||
2200 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno))));
2202 se->valid_blocks = new_vblocks;
2204 /* Update valid block bitmap */
2206 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
2207 #ifdef CONFIG_F2FS_CHECK_FS
2208 mir_exist = f2fs_test_and_set_bit(offset,
2209 se->cur_valid_map_mir);
2210 if (unlikely(exist != mir_exist)) {
2211 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d",
2213 f2fs_bug_on(sbi, 1);
2216 if (unlikely(exist)) {
2217 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u",
2219 f2fs_bug_on(sbi, 1);
2224 if (f2fs_block_unit_discard(sbi) &&
2225 !f2fs_test_and_set_bit(offset, se->discard_map))
2226 sbi->discard_blks--;
2229 * SSR should never reuse block which is checkpointed
2230 * or newly invalidated.
2232 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
2233 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
2234 se->ckpt_valid_blocks++;
2237 exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
2238 #ifdef CONFIG_F2FS_CHECK_FS
2239 mir_exist = f2fs_test_and_clear_bit(offset,
2240 se->cur_valid_map_mir);
2241 if (unlikely(exist != mir_exist)) {
2242 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d",
2244 f2fs_bug_on(sbi, 1);
2247 if (unlikely(!exist)) {
2248 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u",
2250 f2fs_bug_on(sbi, 1);
2253 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2255 * If checkpoints are off, we must not reuse data that
2256 * was used in the previous checkpoint. If it was used
2257 * before, we must track that to know how much space we
2260 if (f2fs_test_bit(offset, se->ckpt_valid_map)) {
2261 spin_lock(&sbi->stat_lock);
2262 sbi->unusable_block_count++;
2263 spin_unlock(&sbi->stat_lock);
2267 if (f2fs_block_unit_discard(sbi) &&
2268 f2fs_test_and_clear_bit(offset, se->discard_map))
2269 sbi->discard_blks++;
2271 if (!f2fs_test_bit(offset, se->ckpt_valid_map))
2272 se->ckpt_valid_blocks += del;
2274 __mark_sit_entry_dirty(sbi, segno);
2276 /* update total number of valid blocks to be written in ckpt area */
2277 SIT_I(sbi)->written_valid_blocks += del;
2279 if (__is_large_section(sbi))
2280 get_sec_entry(sbi, segno)->valid_blocks += del;
2283 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
2285 unsigned int segno = GET_SEGNO(sbi, addr);
2286 struct sit_info *sit_i = SIT_I(sbi);
2288 f2fs_bug_on(sbi, addr == NULL_ADDR);
2289 if (addr == NEW_ADDR || addr == COMPRESS_ADDR)
2292 invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
2293 f2fs_invalidate_compress_page(sbi, addr);
2295 /* add it into sit main buffer */
2296 down_write(&sit_i->sentry_lock);
2298 update_segment_mtime(sbi, addr, 0);
2299 update_sit_entry(sbi, addr, -1);
2301 /* add it into dirty seglist */
2302 locate_dirty_segment(sbi, segno);
2304 up_write(&sit_i->sentry_lock);
2307 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
2309 struct sit_info *sit_i = SIT_I(sbi);
2310 unsigned int segno, offset;
2311 struct seg_entry *se;
2314 if (!__is_valid_data_blkaddr(blkaddr))
2317 down_read(&sit_i->sentry_lock);
2319 segno = GET_SEGNO(sbi, blkaddr);
2320 se = get_seg_entry(sbi, segno);
2321 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
2323 if (f2fs_test_bit(offset, se->ckpt_valid_map))
2326 up_read(&sit_i->sentry_lock);
2332 * This function should be resided under the curseg_mutex lock
2334 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
2335 struct f2fs_summary *sum)
2337 struct curseg_info *curseg = CURSEG_I(sbi, type);
2338 void *addr = curseg->sum_blk;
2340 addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
2341 memcpy(addr, sum, sizeof(struct f2fs_summary));
2345 * Calculate the number of current summary pages for writing
2347 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
2349 int valid_sum_count = 0;
2352 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
2353 if (sbi->ckpt->alloc_type[i] == SSR)
2354 valid_sum_count += sbi->blocks_per_seg;
2357 valid_sum_count += le16_to_cpu(
2358 F2FS_CKPT(sbi)->cur_data_blkoff[i]);
2360 valid_sum_count += curseg_blkoff(sbi, i);
2364 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
2365 SUM_FOOTER_SIZE) / SUMMARY_SIZE;
2366 if (valid_sum_count <= sum_in_page)
2368 else if ((valid_sum_count - sum_in_page) <=
2369 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
2375 * Caller should put this summary page
2377 struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
2379 if (unlikely(f2fs_cp_error(sbi)))
2380 return ERR_PTR(-EIO);
2381 return f2fs_get_meta_page_retry(sbi, GET_SUM_BLOCK(sbi, segno));
2384 void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
2385 void *src, block_t blk_addr)
2387 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2389 memcpy(page_address(page), src, PAGE_SIZE);
2390 set_page_dirty(page);
2391 f2fs_put_page(page, 1);
2394 static void write_sum_page(struct f2fs_sb_info *sbi,
2395 struct f2fs_summary_block *sum_blk, block_t blk_addr)
2397 f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
2400 static void write_current_sum_page(struct f2fs_sb_info *sbi,
2401 int type, block_t blk_addr)
2403 struct curseg_info *curseg = CURSEG_I(sbi, type);
2404 struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
2405 struct f2fs_summary_block *src = curseg->sum_blk;
2406 struct f2fs_summary_block *dst;
2408 dst = (struct f2fs_summary_block *)page_address(page);
2409 memset(dst, 0, PAGE_SIZE);
2411 mutex_lock(&curseg->curseg_mutex);
2413 down_read(&curseg->journal_rwsem);
2414 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
2415 up_read(&curseg->journal_rwsem);
2417 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
2418 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
2420 mutex_unlock(&curseg->curseg_mutex);
2422 set_page_dirty(page);
2423 f2fs_put_page(page, 1);
2426 static int is_next_segment_free(struct f2fs_sb_info *sbi,
2427 struct curseg_info *curseg, int type)
2429 unsigned int segno = curseg->segno + 1;
2430 struct free_segmap_info *free_i = FREE_I(sbi);
2432 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
2433 return !test_bit(segno, free_i->free_segmap);
2438 * Find a new segment from the free segments bitmap to right order
2439 * This function should be returned with success, otherwise BUG
2441 static void get_new_segment(struct f2fs_sb_info *sbi,
2442 unsigned int *newseg, bool new_sec, int dir)
2444 struct free_segmap_info *free_i = FREE_I(sbi);
2445 unsigned int segno, secno, zoneno;
2446 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
2447 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
2448 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
2449 unsigned int left_start = hint;
2454 spin_lock(&free_i->segmap_lock);
2456 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
2457 segno = find_next_zero_bit(free_i->free_segmap,
2458 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
2459 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
2463 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
2464 if (secno >= MAIN_SECS(sbi)) {
2465 if (dir == ALLOC_RIGHT) {
2466 secno = find_first_zero_bit(free_i->free_secmap,
2468 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
2471 left_start = hint - 1;
2477 while (test_bit(left_start, free_i->free_secmap)) {
2478 if (left_start > 0) {
2482 left_start = find_first_zero_bit(free_i->free_secmap,
2484 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
2489 segno = GET_SEG_FROM_SEC(sbi, secno);
2490 zoneno = GET_ZONE_FROM_SEC(sbi, secno);
2492 /* give up on finding another zone */
2495 if (sbi->secs_per_zone == 1)
2497 if (zoneno == old_zoneno)
2499 if (dir == ALLOC_LEFT) {
2500 if (!go_left && zoneno + 1 >= total_zones)
2502 if (go_left && zoneno == 0)
2505 for (i = 0; i < NR_CURSEG_TYPE; i++)
2506 if (CURSEG_I(sbi, i)->zone == zoneno)
2509 if (i < NR_CURSEG_TYPE) {
2510 /* zone is in user, try another */
2512 hint = zoneno * sbi->secs_per_zone - 1;
2513 else if (zoneno + 1 >= total_zones)
2516 hint = (zoneno + 1) * sbi->secs_per_zone;
2518 goto find_other_zone;
2521 /* set it as dirty segment in free segmap */
2522 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
2523 __set_inuse(sbi, segno);
2525 spin_unlock(&free_i->segmap_lock);
2528 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
2530 struct curseg_info *curseg = CURSEG_I(sbi, type);
2531 struct summary_footer *sum_footer;
2532 unsigned short seg_type = curseg->seg_type;
2534 curseg->inited = true;
2535 curseg->segno = curseg->next_segno;
2536 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
2537 curseg->next_blkoff = 0;
2538 curseg->next_segno = NULL_SEGNO;
2540 sum_footer = &(curseg->sum_blk->footer);
2541 memset(sum_footer, 0, sizeof(struct summary_footer));
2543 sanity_check_seg_type(sbi, seg_type);
2545 if (IS_DATASEG(seg_type))
2546 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
2547 if (IS_NODESEG(seg_type))
2548 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
2549 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified);
2552 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
2554 struct curseg_info *curseg = CURSEG_I(sbi, type);
2555 unsigned short seg_type = curseg->seg_type;
2557 sanity_check_seg_type(sbi, seg_type);
2558 if (f2fs_need_rand_seg(sbi))
2559 return prandom_u32_max(MAIN_SECS(sbi) * sbi->segs_per_sec);
2561 /* if segs_per_sec is large than 1, we need to keep original policy. */
2562 if (__is_large_section(sbi))
2563 return curseg->segno;
2565 /* inmem log may not locate on any segment after mount */
2566 if (!curseg->inited)
2569 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2572 if (test_opt(sbi, NOHEAP) &&
2573 (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)))
2576 if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
2577 return SIT_I(sbi)->last_victim[ALLOC_NEXT];
2579 /* find segments from 0 to reuse freed segments */
2580 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
2583 return curseg->segno;
2587 * Allocate a current working segment.
2588 * This function always allocates a free segment in LFS manner.
2590 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
2592 struct curseg_info *curseg = CURSEG_I(sbi, type);
2593 unsigned short seg_type = curseg->seg_type;
2594 unsigned int segno = curseg->segno;
2595 int dir = ALLOC_LEFT;
2598 write_sum_page(sbi, curseg->sum_blk,
2599 GET_SUM_BLOCK(sbi, segno));
2600 if (seg_type == CURSEG_WARM_DATA || seg_type == CURSEG_COLD_DATA)
2603 if (test_opt(sbi, NOHEAP))
2606 segno = __get_next_segno(sbi, type);
2607 get_new_segment(sbi, &segno, new_sec, dir);
2608 curseg->next_segno = segno;
2609 reset_curseg(sbi, type, 1);
2610 curseg->alloc_type = LFS;
2611 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
2612 curseg->fragment_remained_chunk =
2613 prandom_u32_max(sbi->max_fragment_chunk) + 1;
2616 static int __next_free_blkoff(struct f2fs_sb_info *sbi,
2617 int segno, block_t start)
2619 struct seg_entry *se = get_seg_entry(sbi, segno);
2620 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
2621 unsigned long *target_map = SIT_I(sbi)->tmp_map;
2622 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
2623 unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
2626 for (i = 0; i < entries; i++)
2627 target_map[i] = ckpt_map[i] | cur_map[i];
2629 return __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
2633 * If a segment is written by LFS manner, next block offset is just obtained
2634 * by increasing the current block offset. However, if a segment is written by
2635 * SSR manner, next block offset obtained by calling __next_free_blkoff
2637 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
2638 struct curseg_info *seg)
2640 if (seg->alloc_type == SSR) {
2642 __next_free_blkoff(sbi, seg->segno,
2643 seg->next_blkoff + 1);
2646 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) {
2647 /* To allocate block chunks in different sizes, use random number */
2648 if (--seg->fragment_remained_chunk <= 0) {
2649 seg->fragment_remained_chunk =
2650 prandom_u32_max(sbi->max_fragment_chunk) + 1;
2652 prandom_u32_max(sbi->max_fragment_hole) + 1;
2658 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno)
2660 return __next_free_blkoff(sbi, segno, 0) < sbi->blocks_per_seg;
2664 * This function always allocates a used segment(from dirty seglist) by SSR
2665 * manner, so it should recover the existing segment information of valid blocks
2667 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool flush)
2669 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
2670 struct curseg_info *curseg = CURSEG_I(sbi, type);
2671 unsigned int new_segno = curseg->next_segno;
2672 struct f2fs_summary_block *sum_node;
2673 struct page *sum_page;
2676 write_sum_page(sbi, curseg->sum_blk,
2677 GET_SUM_BLOCK(sbi, curseg->segno));
2679 __set_test_and_inuse(sbi, new_segno);
2681 mutex_lock(&dirty_i->seglist_lock);
2682 __remove_dirty_segment(sbi, new_segno, PRE);
2683 __remove_dirty_segment(sbi, new_segno, DIRTY);
2684 mutex_unlock(&dirty_i->seglist_lock);
2686 reset_curseg(sbi, type, 1);
2687 curseg->alloc_type = SSR;
2688 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0);
2690 sum_page = f2fs_get_sum_page(sbi, new_segno);
2691 if (IS_ERR(sum_page)) {
2692 /* GC won't be able to use stale summary pages by cp_error */
2693 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE);
2696 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
2697 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
2698 f2fs_put_page(sum_page, 1);
2701 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2702 int alloc_mode, unsigned long long age);
2704 static void get_atssr_segment(struct f2fs_sb_info *sbi, int type,
2705 int target_type, int alloc_mode,
2706 unsigned long long age)
2708 struct curseg_info *curseg = CURSEG_I(sbi, type);
2710 curseg->seg_type = target_type;
2712 if (get_ssr_segment(sbi, type, alloc_mode, age)) {
2713 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno);
2715 curseg->seg_type = se->type;
2716 change_curseg(sbi, type, true);
2718 /* allocate cold segment by default */
2719 curseg->seg_type = CURSEG_COLD_DATA;
2720 new_curseg(sbi, type, true);
2722 stat_inc_seg_type(sbi, curseg);
2725 static void __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi)
2727 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC);
2729 if (!sbi->am.atgc_enabled)
2732 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2734 mutex_lock(&curseg->curseg_mutex);
2735 down_write(&SIT_I(sbi)->sentry_lock);
2737 get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, CURSEG_COLD_DATA, SSR, 0);
2739 up_write(&SIT_I(sbi)->sentry_lock);
2740 mutex_unlock(&curseg->curseg_mutex);
2742 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2745 void f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi)
2747 __f2fs_init_atgc_curseg(sbi);
2750 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2752 struct curseg_info *curseg = CURSEG_I(sbi, type);
2754 mutex_lock(&curseg->curseg_mutex);
2755 if (!curseg->inited)
2758 if (get_valid_blocks(sbi, curseg->segno, false)) {
2759 write_sum_page(sbi, curseg->sum_blk,
2760 GET_SUM_BLOCK(sbi, curseg->segno));
2762 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2763 __set_test_and_free(sbi, curseg->segno, true);
2764 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2767 mutex_unlock(&curseg->curseg_mutex);
2770 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi)
2772 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2774 if (sbi->am.atgc_enabled)
2775 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2778 static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type)
2780 struct curseg_info *curseg = CURSEG_I(sbi, type);
2782 mutex_lock(&curseg->curseg_mutex);
2783 if (!curseg->inited)
2785 if (get_valid_blocks(sbi, curseg->segno, false))
2788 mutex_lock(&DIRTY_I(sbi)->seglist_lock);
2789 __set_test_and_inuse(sbi, curseg->segno);
2790 mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
2792 mutex_unlock(&curseg->curseg_mutex);
2795 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi)
2797 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED);
2799 if (sbi->am.atgc_enabled)
2800 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC);
2803 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type,
2804 int alloc_mode, unsigned long long age)
2806 struct curseg_info *curseg = CURSEG_I(sbi, type);
2807 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
2808 unsigned segno = NULL_SEGNO;
2809 unsigned short seg_type = curseg->seg_type;
2811 bool reversed = false;
2813 sanity_check_seg_type(sbi, seg_type);
2815 /* f2fs_need_SSR() already forces to do this */
2816 if (!v_ops->get_victim(sbi, &segno, BG_GC, seg_type, alloc_mode, age)) {
2817 curseg->next_segno = segno;
2821 /* For node segments, let's do SSR more intensively */
2822 if (IS_NODESEG(seg_type)) {
2823 if (seg_type >= CURSEG_WARM_NODE) {
2825 i = CURSEG_COLD_NODE;
2827 i = CURSEG_HOT_NODE;
2829 cnt = NR_CURSEG_NODE_TYPE;
2831 if (seg_type >= CURSEG_WARM_DATA) {
2833 i = CURSEG_COLD_DATA;
2835 i = CURSEG_HOT_DATA;
2837 cnt = NR_CURSEG_DATA_TYPE;
2840 for (; cnt-- > 0; reversed ? i-- : i++) {
2843 if (!v_ops->get_victim(sbi, &segno, BG_GC, i, alloc_mode, age)) {
2844 curseg->next_segno = segno;
2849 /* find valid_blocks=0 in dirty list */
2850 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2851 segno = get_free_segment(sbi);
2852 if (segno != NULL_SEGNO) {
2853 curseg->next_segno = segno;
2861 * flush out current segment and replace it with new segment
2862 * This function should be returned with success, otherwise BUG
2864 static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
2865 int type, bool force)
2867 struct curseg_info *curseg = CURSEG_I(sbi, type);
2870 new_curseg(sbi, type, true);
2871 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
2872 curseg->seg_type == CURSEG_WARM_NODE)
2873 new_curseg(sbi, type, false);
2874 else if (curseg->alloc_type == LFS &&
2875 is_next_segment_free(sbi, curseg, type) &&
2876 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
2877 new_curseg(sbi, type, false);
2878 else if (f2fs_need_SSR(sbi) &&
2879 get_ssr_segment(sbi, type, SSR, 0))
2880 change_curseg(sbi, type, true);
2882 new_curseg(sbi, type, false);
2884 stat_inc_seg_type(sbi, curseg);
2887 void f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
2888 unsigned int start, unsigned int end)
2890 struct curseg_info *curseg = CURSEG_I(sbi, type);
2893 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2894 mutex_lock(&curseg->curseg_mutex);
2895 down_write(&SIT_I(sbi)->sentry_lock);
2897 segno = CURSEG_I(sbi, type)->segno;
2898 if (segno < start || segno > end)
2901 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0))
2902 change_curseg(sbi, type, true);
2904 new_curseg(sbi, type, true);
2906 stat_inc_seg_type(sbi, curseg);
2908 locate_dirty_segment(sbi, segno);
2910 up_write(&SIT_I(sbi)->sentry_lock);
2912 if (segno != curseg->segno)
2913 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u",
2914 type, segno, curseg->segno);
2916 mutex_unlock(&curseg->curseg_mutex);
2917 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2920 static void __allocate_new_segment(struct f2fs_sb_info *sbi, int type,
2921 bool new_sec, bool force)
2923 struct curseg_info *curseg = CURSEG_I(sbi, type);
2924 unsigned int old_segno;
2926 if (!curseg->inited)
2929 if (force || curseg->next_blkoff ||
2930 get_valid_blocks(sbi, curseg->segno, new_sec))
2933 if (!get_ckpt_valid_blocks(sbi, curseg->segno, new_sec))
2936 old_segno = curseg->segno;
2937 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
2938 locate_dirty_segment(sbi, old_segno);
2941 static void __allocate_new_section(struct f2fs_sb_info *sbi,
2942 int type, bool force)
2944 __allocate_new_segment(sbi, type, true, force);
2947 void f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force)
2949 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2950 down_write(&SIT_I(sbi)->sentry_lock);
2951 __allocate_new_section(sbi, type, force);
2952 up_write(&SIT_I(sbi)->sentry_lock);
2953 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2956 void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
2960 f2fs_down_read(&SM_I(sbi)->curseg_lock);
2961 down_write(&SIT_I(sbi)->sentry_lock);
2962 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
2963 __allocate_new_segment(sbi, i, false, false);
2964 up_write(&SIT_I(sbi)->sentry_lock);
2965 f2fs_up_read(&SM_I(sbi)->curseg_lock);
2968 static const struct segment_allocation default_salloc_ops = {
2969 .allocate_segment = allocate_segment_by_default,
2972 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
2973 struct cp_control *cpc)
2975 __u64 trim_start = cpc->trim_start;
2976 bool has_candidate = false;
2978 down_write(&SIT_I(sbi)->sentry_lock);
2979 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
2980 if (add_discard_addrs(sbi, cpc, true)) {
2981 has_candidate = true;
2985 up_write(&SIT_I(sbi)->sentry_lock);
2987 cpc->trim_start = trim_start;
2988 return has_candidate;
2991 static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
2992 struct discard_policy *dpolicy,
2993 unsigned int start, unsigned int end)
2995 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
2996 struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
2997 struct rb_node **insert_p = NULL, *insert_parent = NULL;
2998 struct discard_cmd *dc;
2999 struct blk_plug plug;
3001 unsigned int trimmed = 0;
3006 mutex_lock(&dcc->cmd_lock);
3007 if (unlikely(dcc->rbtree_check))
3008 f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
3011 dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
3013 (struct rb_entry **)&prev_dc,
3014 (struct rb_entry **)&next_dc,
3015 &insert_p, &insert_parent, true, NULL);
3019 blk_start_plug(&plug);
3021 while (dc && dc->lstart <= end) {
3022 struct rb_node *node;
3025 if (dc->len < dpolicy->granularity)
3028 if (dc->state != D_PREP) {
3029 list_move_tail(&dc->list, &dcc->fstrim_list);
3033 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
3035 if (issued >= dpolicy->max_requests) {
3036 start = dc->lstart + dc->len;
3039 __remove_discard_cmd(sbi, dc);
3041 blk_finish_plug(&plug);
3042 mutex_unlock(&dcc->cmd_lock);
3043 trimmed += __wait_all_discard_cmd(sbi, NULL);
3044 f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT);
3048 node = rb_next(&dc->rb_node);
3050 __remove_discard_cmd(sbi, dc);
3051 dc = rb_entry_safe(node, struct discard_cmd, rb_node);
3053 if (fatal_signal_pending(current))
3057 blk_finish_plug(&plug);
3058 mutex_unlock(&dcc->cmd_lock);
3063 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
3065 __u64 start = F2FS_BYTES_TO_BLK(range->start);
3066 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
3067 unsigned int start_segno, end_segno;
3068 block_t start_block, end_block;
3069 struct cp_control cpc;
3070 struct discard_policy dpolicy;
3071 unsigned long long trimmed = 0;
3073 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi);
3075 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
3078 if (end < MAIN_BLKADDR(sbi))
3081 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
3082 f2fs_warn(sbi, "Found FS corruption, run fsck to fix.");
3083 return -EFSCORRUPTED;
3086 /* start/end segment number in main_area */
3087 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
3088 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
3089 GET_SEGNO(sbi, end);
3091 start_segno = rounddown(start_segno, sbi->segs_per_sec);
3092 end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
3095 cpc.reason = CP_DISCARD;
3096 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
3097 cpc.trim_start = start_segno;
3098 cpc.trim_end = end_segno;
3100 if (sbi->discard_blks == 0)
3103 f2fs_down_write(&sbi->gc_lock);
3104 err = f2fs_write_checkpoint(sbi, &cpc);
3105 f2fs_up_write(&sbi->gc_lock);
3110 * We filed discard candidates, but actually we don't need to wait for
3111 * all of them, since they'll be issued in idle time along with runtime
3112 * discard option. User configuration looks like using runtime discard
3113 * or periodic fstrim instead of it.
3115 if (f2fs_realtime_discard_enable(sbi))
3118 start_block = START_BLOCK(sbi, start_segno);
3119 end_block = START_BLOCK(sbi, end_segno + 1);
3121 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
3122 trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
3123 start_block, end_block);
3125 trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
3126 start_block, end_block);
3129 range->len = F2FS_BLK_TO_BYTES(trimmed);
3133 static bool __has_curseg_space(struct f2fs_sb_info *sbi,
3134 struct curseg_info *curseg)
3136 return curseg->next_blkoff < f2fs_usable_blks_in_seg(sbi,
3140 int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
3143 case WRITE_LIFE_SHORT:
3144 return CURSEG_HOT_DATA;
3145 case WRITE_LIFE_EXTREME:
3146 return CURSEG_COLD_DATA;
3148 return CURSEG_WARM_DATA;
3152 static int __get_segment_type_2(struct f2fs_io_info *fio)
3154 if (fio->type == DATA)
3155 return CURSEG_HOT_DATA;
3157 return CURSEG_HOT_NODE;
3160 static int __get_segment_type_4(struct f2fs_io_info *fio)
3162 if (fio->type == DATA) {
3163 struct inode *inode = fio->page->mapping->host;
3165 if (S_ISDIR(inode->i_mode))
3166 return CURSEG_HOT_DATA;
3168 return CURSEG_COLD_DATA;
3170 if (IS_DNODE(fio->page) && is_cold_node(fio->page))
3171 return CURSEG_WARM_NODE;
3173 return CURSEG_COLD_NODE;
3177 static int __get_segment_type_6(struct f2fs_io_info *fio)
3179 if (fio->type == DATA) {
3180 struct inode *inode = fio->page->mapping->host;
3182 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE))
3183 return CURSEG_COLD_DATA_PINNED;
3185 if (page_private_gcing(fio->page)) {
3186 if (fio->sbi->am.atgc_enabled &&
3187 (fio->io_type == FS_DATA_IO) &&
3188 (fio->sbi->gc_mode != GC_URGENT_HIGH))
3189 return CURSEG_ALL_DATA_ATGC;
3191 return CURSEG_COLD_DATA;
3193 if (file_is_cold(inode) || f2fs_need_compress_data(inode))
3194 return CURSEG_COLD_DATA;
3195 if (file_is_hot(inode) ||
3196 is_inode_flag_set(inode, FI_HOT_DATA) ||
3197 f2fs_is_cow_file(inode))
3198 return CURSEG_HOT_DATA;
3199 return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
3201 if (IS_DNODE(fio->page))
3202 return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
3204 return CURSEG_COLD_NODE;
3208 static int __get_segment_type(struct f2fs_io_info *fio)
3212 switch (F2FS_OPTION(fio->sbi).active_logs) {
3214 type = __get_segment_type_2(fio);
3217 type = __get_segment_type_4(fio);
3220 type = __get_segment_type_6(fio);
3223 f2fs_bug_on(fio->sbi, true);
3228 else if (IS_WARM(type))
3235 void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
3236 block_t old_blkaddr, block_t *new_blkaddr,
3237 struct f2fs_summary *sum, int type,
3238 struct f2fs_io_info *fio)
3240 struct sit_info *sit_i = SIT_I(sbi);
3241 struct curseg_info *curseg = CURSEG_I(sbi, type);
3242 unsigned long long old_mtime;
3243 bool from_gc = (type == CURSEG_ALL_DATA_ATGC);
3244 struct seg_entry *se = NULL;
3246 f2fs_down_read(&SM_I(sbi)->curseg_lock);
3248 mutex_lock(&curseg->curseg_mutex);
3249 down_write(&sit_i->sentry_lock);
3252 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO);
3253 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr));
3254 sanity_check_seg_type(sbi, se->type);
3255 f2fs_bug_on(sbi, IS_NODESEG(se->type));
3257 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
3259 f2fs_bug_on(sbi, curseg->next_blkoff >= sbi->blocks_per_seg);
3261 f2fs_wait_discard_bio(sbi, *new_blkaddr);
3264 * __add_sum_entry should be resided under the curseg_mutex
3265 * because, this function updates a summary entry in the
3266 * current summary block.
3268 __add_sum_entry(sbi, type, sum);
3270 __refresh_next_blkoff(sbi, curseg);
3272 stat_inc_block_count(sbi, curseg);
3275 old_mtime = get_segment_mtime(sbi, old_blkaddr);
3277 update_segment_mtime(sbi, old_blkaddr, 0);
3280 update_segment_mtime(sbi, *new_blkaddr, old_mtime);
3283 * SIT information should be updated before segment allocation,
3284 * since SSR needs latest valid block information.
3286 update_sit_entry(sbi, *new_blkaddr, 1);
3287 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
3288 update_sit_entry(sbi, old_blkaddr, -1);
3290 if (!__has_curseg_space(sbi, curseg)) {
3292 get_atssr_segment(sbi, type, se->type,
3295 sit_i->s_ops->allocate_segment(sbi, type, false);
3298 * segment dirty status should be updated after segment allocation,
3299 * so we just need to update status only one time after previous
3300 * segment being closed.
3302 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3303 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
3305 up_write(&sit_i->sentry_lock);
3307 if (page && IS_NODESEG(type)) {
3308 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
3310 f2fs_inode_chksum_set(sbi, page);
3314 struct f2fs_bio_info *io;
3316 if (F2FS_IO_ALIGNED(sbi))
3319 INIT_LIST_HEAD(&fio->list);
3321 io = sbi->write_io[fio->type] + fio->temp;
3322 spin_lock(&io->io_lock);
3323 list_add_tail(&fio->list, &io->io_list);
3324 spin_unlock(&io->io_lock);
3327 mutex_unlock(&curseg->curseg_mutex);
3329 f2fs_up_read(&SM_I(sbi)->curseg_lock);
3332 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
3333 block_t blkaddr, unsigned int blkcnt)
3335 if (!f2fs_is_multi_device(sbi))
3339 unsigned int devidx = f2fs_target_device_index(sbi, blkaddr);
3340 unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1;
3342 /* update device state for fsync */
3343 f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO);
3345 /* update device state for checkpoint */
3346 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
3347 spin_lock(&sbi->dev_lock);
3348 f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
3349 spin_unlock(&sbi->dev_lock);
3359 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
3361 int type = __get_segment_type(fio);
3362 bool keep_order = (f2fs_lfs_mode(fio->sbi) && type == CURSEG_COLD_DATA);
3365 f2fs_down_read(&fio->sbi->io_order_lock);
3367 f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
3368 &fio->new_blkaddr, sum, type, fio);
3369 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) {
3370 invalidate_mapping_pages(META_MAPPING(fio->sbi),
3371 fio->old_blkaddr, fio->old_blkaddr);
3372 f2fs_invalidate_compress_page(fio->sbi, fio->old_blkaddr);
3375 /* writeout dirty page into bdev */
3376 f2fs_submit_page_write(fio);
3378 fio->old_blkaddr = fio->new_blkaddr;
3382 f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1);
3385 f2fs_up_read(&fio->sbi->io_order_lock);
3388 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
3389 enum iostat_type io_type)
3391 struct f2fs_io_info fio = {
3396 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
3397 .old_blkaddr = page->index,
3398 .new_blkaddr = page->index,
3400 .encrypted_page = NULL,
3404 if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
3405 fio.op_flags &= ~REQ_META;
3407 set_page_writeback(page);
3408 ClearPageError(page);
3409 f2fs_submit_page_write(&fio);
3411 stat_inc_meta_count(sbi, page->index);
3412 f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE);
3415 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
3417 struct f2fs_summary sum;
3419 set_summary(&sum, nid, 0, 0);
3420 do_write_page(&sum, fio);
3422 f2fs_update_iostat(fio->sbi, NULL, fio->io_type, F2FS_BLKSIZE);
3425 void f2fs_outplace_write_data(struct dnode_of_data *dn,
3426 struct f2fs_io_info *fio)
3428 struct f2fs_sb_info *sbi = fio->sbi;
3429 struct f2fs_summary sum;
3431 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
3432 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
3433 do_write_page(&sum, fio);
3434 f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
3436 f2fs_update_iostat(sbi, dn->inode, fio->io_type, F2FS_BLKSIZE);
3439 int f2fs_inplace_write_data(struct f2fs_io_info *fio)
3442 struct f2fs_sb_info *sbi = fio->sbi;
3445 fio->new_blkaddr = fio->old_blkaddr;
3446 /* i/o temperature is needed for passing down write hints */
3447 __get_segment_type(fio);
3449 segno = GET_SEGNO(sbi, fio->new_blkaddr);
3451 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
3452 set_sbi_flag(sbi, SBI_NEED_FSCK);
3453 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.",
3455 err = -EFSCORRUPTED;
3456 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
3460 if (f2fs_cp_error(sbi)) {
3466 invalidate_mapping_pages(META_MAPPING(sbi),
3467 fio->new_blkaddr, fio->new_blkaddr);
3469 stat_inc_inplace_blocks(fio->sbi);
3471 if (fio->bio && !(SM_I(sbi)->ipu_policy & (1 << F2FS_IPU_NOCACHE)))
3472 err = f2fs_merge_page_bio(fio);
3474 err = f2fs_submit_page_bio(fio);
3476 f2fs_update_device_state(fio->sbi, fio->ino,
3477 fio->new_blkaddr, 1);
3478 f2fs_update_iostat(fio->sbi, fio->page->mapping->host,
3479 fio->io_type, F2FS_BLKSIZE);
3484 if (fio->bio && *(fio->bio)) {
3485 struct bio *bio = *(fio->bio);
3487 bio->bi_status = BLK_STS_IOERR;
3494 static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
3499 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
3500 if (CURSEG_I(sbi, i)->segno == segno)
3506 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
3507 block_t old_blkaddr, block_t new_blkaddr,
3508 bool recover_curseg, bool recover_newaddr,
3511 struct sit_info *sit_i = SIT_I(sbi);
3512 struct curseg_info *curseg;
3513 unsigned int segno, old_cursegno;
3514 struct seg_entry *se;
3516 unsigned short old_blkoff;
3517 unsigned char old_alloc_type;
3519 segno = GET_SEGNO(sbi, new_blkaddr);
3520 se = get_seg_entry(sbi, segno);
3523 f2fs_down_write(&SM_I(sbi)->curseg_lock);
3525 if (!recover_curseg) {
3526 /* for recovery flow */
3527 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
3528 if (old_blkaddr == NULL_ADDR)
3529 type = CURSEG_COLD_DATA;
3531 type = CURSEG_WARM_DATA;
3534 if (IS_CURSEG(sbi, segno)) {
3535 /* se->type is volatile as SSR allocation */
3536 type = __f2fs_get_curseg(sbi, segno);
3537 f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
3539 type = CURSEG_WARM_DATA;
3543 f2fs_bug_on(sbi, !IS_DATASEG(type));
3544 curseg = CURSEG_I(sbi, type);
3546 mutex_lock(&curseg->curseg_mutex);
3547 down_write(&sit_i->sentry_lock);
3549 old_cursegno = curseg->segno;
3550 old_blkoff = curseg->next_blkoff;
3551 old_alloc_type = curseg->alloc_type;
3553 /* change the current segment */
3554 if (segno != curseg->segno) {
3555 curseg->next_segno = segno;
3556 change_curseg(sbi, type, true);
3559 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
3560 __add_sum_entry(sbi, type, sum);
3562 if (!recover_curseg || recover_newaddr) {
3564 update_segment_mtime(sbi, new_blkaddr, 0);
3565 update_sit_entry(sbi, new_blkaddr, 1);
3567 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
3568 invalidate_mapping_pages(META_MAPPING(sbi),
3569 old_blkaddr, old_blkaddr);
3570 f2fs_invalidate_compress_page(sbi, old_blkaddr);
3572 update_segment_mtime(sbi, old_blkaddr, 0);
3573 update_sit_entry(sbi, old_blkaddr, -1);
3576 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
3577 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
3579 locate_dirty_segment(sbi, old_cursegno);
3581 if (recover_curseg) {
3582 if (old_cursegno != curseg->segno) {
3583 curseg->next_segno = old_cursegno;
3584 change_curseg(sbi, type, true);
3586 curseg->next_blkoff = old_blkoff;
3587 curseg->alloc_type = old_alloc_type;
3590 up_write(&sit_i->sentry_lock);
3591 mutex_unlock(&curseg->curseg_mutex);
3592 f2fs_up_write(&SM_I(sbi)->curseg_lock);
3595 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
3596 block_t old_addr, block_t new_addr,
3597 unsigned char version, bool recover_curseg,
3598 bool recover_newaddr)
3600 struct f2fs_summary sum;
3602 set_summary(&sum, dn->nid, dn->ofs_in_node, version);
3604 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
3605 recover_curseg, recover_newaddr, false);
3607 f2fs_update_data_blkaddr(dn, new_addr);
3610 void f2fs_wait_on_page_writeback(struct page *page,
3611 enum page_type type, bool ordered, bool locked)
3613 if (PageWriteback(page)) {
3614 struct f2fs_sb_info *sbi = F2FS_P_SB(page);
3616 /* submit cached LFS IO */
3617 f2fs_submit_merged_write_cond(sbi, NULL, page, 0, type);
3618 /* sbumit cached IPU IO */
3619 f2fs_submit_merged_ipu_write(sbi, NULL, page);
3621 wait_on_page_writeback(page);
3622 f2fs_bug_on(sbi, locked && PageWriteback(page));
3624 wait_for_stable_page(page);
3629 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
3631 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3634 if (!f2fs_post_read_required(inode))
3637 if (!__is_valid_data_blkaddr(blkaddr))
3640 cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
3642 f2fs_wait_on_page_writeback(cpage, DATA, true, true);
3643 f2fs_put_page(cpage, 1);
3647 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
3650 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3653 if (!f2fs_post_read_required(inode))
3656 for (i = 0; i < len; i++)
3657 f2fs_wait_on_block_writeback(inode, blkaddr + i);
3659 invalidate_mapping_pages(META_MAPPING(sbi), blkaddr, blkaddr + len - 1);
3662 static int read_compacted_summaries(struct f2fs_sb_info *sbi)
3664 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3665 struct curseg_info *seg_i;
3666 unsigned char *kaddr;
3671 start = start_sum_block(sbi);
3673 page = f2fs_get_meta_page(sbi, start++);
3675 return PTR_ERR(page);
3676 kaddr = (unsigned char *)page_address(page);
3678 /* Step 1: restore nat cache */
3679 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3680 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
3682 /* Step 2: restore sit cache */
3683 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3684 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
3685 offset = 2 * SUM_JOURNAL_SIZE;
3687 /* Step 3: restore summary entries */
3688 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3689 unsigned short blk_off;
3692 seg_i = CURSEG_I(sbi, i);
3693 segno = le32_to_cpu(ckpt->cur_data_segno[i]);
3694 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
3695 seg_i->next_segno = segno;
3696 reset_curseg(sbi, i, 0);
3697 seg_i->alloc_type = ckpt->alloc_type[i];
3698 seg_i->next_blkoff = blk_off;
3700 if (seg_i->alloc_type == SSR)
3701 blk_off = sbi->blocks_per_seg;
3703 for (j = 0; j < blk_off; j++) {
3704 struct f2fs_summary *s;
3706 s = (struct f2fs_summary *)(kaddr + offset);
3707 seg_i->sum_blk->entries[j] = *s;
3708 offset += SUMMARY_SIZE;
3709 if (offset + SUMMARY_SIZE <= PAGE_SIZE -
3713 f2fs_put_page(page, 1);
3716 page = f2fs_get_meta_page(sbi, start++);
3718 return PTR_ERR(page);
3719 kaddr = (unsigned char *)page_address(page);
3723 f2fs_put_page(page, 1);
3727 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
3729 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
3730 struct f2fs_summary_block *sum;
3731 struct curseg_info *curseg;
3733 unsigned short blk_off;
3734 unsigned int segno = 0;
3735 block_t blk_addr = 0;
3738 /* get segment number and block addr */
3739 if (IS_DATASEG(type)) {
3740 segno = le32_to_cpu(ckpt->cur_data_segno[type]);
3741 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
3743 if (__exist_node_summaries(sbi))
3744 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type);
3746 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
3748 segno = le32_to_cpu(ckpt->cur_node_segno[type -
3750 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
3752 if (__exist_node_summaries(sbi))
3753 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
3754 type - CURSEG_HOT_NODE);
3756 blk_addr = GET_SUM_BLOCK(sbi, segno);
3759 new = f2fs_get_meta_page(sbi, blk_addr);
3761 return PTR_ERR(new);
3762 sum = (struct f2fs_summary_block *)page_address(new);
3764 if (IS_NODESEG(type)) {
3765 if (__exist_node_summaries(sbi)) {
3766 struct f2fs_summary *ns = &sum->entries[0];
3769 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
3771 ns->ofs_in_node = 0;
3774 err = f2fs_restore_node_summary(sbi, segno, sum);
3780 /* set uncompleted segment to curseg */
3781 curseg = CURSEG_I(sbi, type);
3782 mutex_lock(&curseg->curseg_mutex);
3784 /* update journal info */
3785 down_write(&curseg->journal_rwsem);
3786 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
3787 up_write(&curseg->journal_rwsem);
3789 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
3790 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
3791 curseg->next_segno = segno;
3792 reset_curseg(sbi, type, 0);
3793 curseg->alloc_type = ckpt->alloc_type[type];
3794 curseg->next_blkoff = blk_off;
3795 mutex_unlock(&curseg->curseg_mutex);
3797 f2fs_put_page(new, 1);
3801 static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
3803 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
3804 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
3805 int type = CURSEG_HOT_DATA;
3808 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
3809 int npages = f2fs_npages_for_summary_flush(sbi, true);
3812 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
3815 /* restore for compacted data summary */
3816 err = read_compacted_summaries(sbi);
3819 type = CURSEG_HOT_NODE;
3822 if (__exist_node_summaries(sbi))
3823 f2fs_ra_meta_pages(sbi,
3824 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type),
3825 NR_CURSEG_PERSIST_TYPE - type, META_CP, true);
3827 for (; type <= CURSEG_COLD_NODE; type++) {
3828 err = read_normal_summaries(sbi, type);
3833 /* sanity check for summary blocks */
3834 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
3835 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) {
3836 f2fs_err(sbi, "invalid journal entries nats %u sits %u",
3837 nats_in_cursum(nat_j), sits_in_cursum(sit_j));
3844 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
3847 unsigned char *kaddr;
3848 struct f2fs_summary *summary;
3849 struct curseg_info *seg_i;
3850 int written_size = 0;
3853 page = f2fs_grab_meta_page(sbi, blkaddr++);
3854 kaddr = (unsigned char *)page_address(page);
3855 memset(kaddr, 0, PAGE_SIZE);
3857 /* Step 1: write nat cache */
3858 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
3859 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
3860 written_size += SUM_JOURNAL_SIZE;
3862 /* Step 2: write sit cache */
3863 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
3864 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
3865 written_size += SUM_JOURNAL_SIZE;
3867 /* Step 3: write summary entries */
3868 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
3869 unsigned short blkoff;
3871 seg_i = CURSEG_I(sbi, i);
3872 if (sbi->ckpt->alloc_type[i] == SSR)
3873 blkoff = sbi->blocks_per_seg;
3875 blkoff = curseg_blkoff(sbi, i);
3877 for (j = 0; j < blkoff; j++) {
3879 page = f2fs_grab_meta_page(sbi, blkaddr++);
3880 kaddr = (unsigned char *)page_address(page);
3881 memset(kaddr, 0, PAGE_SIZE);
3884 summary = (struct f2fs_summary *)(kaddr + written_size);
3885 *summary = seg_i->sum_blk->entries[j];
3886 written_size += SUMMARY_SIZE;
3888 if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
3892 set_page_dirty(page);
3893 f2fs_put_page(page, 1);
3898 set_page_dirty(page);
3899 f2fs_put_page(page, 1);
3903 static void write_normal_summaries(struct f2fs_sb_info *sbi,
3904 block_t blkaddr, int type)
3908 if (IS_DATASEG(type))
3909 end = type + NR_CURSEG_DATA_TYPE;
3911 end = type + NR_CURSEG_NODE_TYPE;
3913 for (i = type; i < end; i++)
3914 write_current_sum_page(sbi, i, blkaddr + (i - type));
3917 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
3919 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
3920 write_compacted_summaries(sbi, start_blk);
3922 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
3925 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
3927 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
3930 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
3931 unsigned int val, int alloc)
3935 if (type == NAT_JOURNAL) {
3936 for (i = 0; i < nats_in_cursum(journal); i++) {
3937 if (le32_to_cpu(nid_in_journal(journal, i)) == val)
3940 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
3941 return update_nats_in_cursum(journal, 1);
3942 } else if (type == SIT_JOURNAL) {
3943 for (i = 0; i < sits_in_cursum(journal); i++)
3944 if (le32_to_cpu(segno_in_journal(journal, i)) == val)
3946 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
3947 return update_sits_in_cursum(journal, 1);
3952 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
3955 return f2fs_get_meta_page(sbi, current_sit_addr(sbi, segno));
3958 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
3961 struct sit_info *sit_i = SIT_I(sbi);
3963 pgoff_t src_off, dst_off;
3965 src_off = current_sit_addr(sbi, start);
3966 dst_off = next_sit_addr(sbi, src_off);
3968 page = f2fs_grab_meta_page(sbi, dst_off);
3969 seg_info_to_sit_page(sbi, page, start);
3971 set_page_dirty(page);
3972 set_to_next_sit(sit_i, start);
3977 static struct sit_entry_set *grab_sit_entry_set(void)
3979 struct sit_entry_set *ses =
3980 f2fs_kmem_cache_alloc(sit_entry_set_slab,
3981 GFP_NOFS, true, NULL);
3984 INIT_LIST_HEAD(&ses->set_list);
3988 static void release_sit_entry_set(struct sit_entry_set *ses)
3990 list_del(&ses->set_list);
3991 kmem_cache_free(sit_entry_set_slab, ses);
3994 static void adjust_sit_entry_set(struct sit_entry_set *ses,
3995 struct list_head *head)
3997 struct sit_entry_set *next = ses;
3999 if (list_is_last(&ses->set_list, head))
4002 list_for_each_entry_continue(next, head, set_list)
4003 if (ses->entry_cnt <= next->entry_cnt) {
4004 list_move_tail(&ses->set_list, &next->set_list);
4008 list_move_tail(&ses->set_list, head);
4011 static void add_sit_entry(unsigned int segno, struct list_head *head)
4013 struct sit_entry_set *ses;
4014 unsigned int start_segno = START_SEGNO(segno);
4016 list_for_each_entry(ses, head, set_list) {
4017 if (ses->start_segno == start_segno) {
4019 adjust_sit_entry_set(ses, head);
4024 ses = grab_sit_entry_set();
4026 ses->start_segno = start_segno;
4028 list_add(&ses->set_list, head);
4031 static void add_sits_in_set(struct f2fs_sb_info *sbi)
4033 struct f2fs_sm_info *sm_info = SM_I(sbi);
4034 struct list_head *set_list = &sm_info->sit_entry_set;
4035 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
4038 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
4039 add_sit_entry(segno, set_list);
4042 static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
4044 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4045 struct f2fs_journal *journal = curseg->journal;
4048 down_write(&curseg->journal_rwsem);
4049 for (i = 0; i < sits_in_cursum(journal); i++) {
4053 segno = le32_to_cpu(segno_in_journal(journal, i));
4054 dirtied = __mark_sit_entry_dirty(sbi, segno);
4057 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
4059 update_sits_in_cursum(journal, -i);
4060 up_write(&curseg->journal_rwsem);
4064 * CP calls this function, which flushes SIT entries including sit_journal,
4065 * and moves prefree segs to free segs.
4067 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
4069 struct sit_info *sit_i = SIT_I(sbi);
4070 unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
4071 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4072 struct f2fs_journal *journal = curseg->journal;
4073 struct sit_entry_set *ses, *tmp;
4074 struct list_head *head = &SM_I(sbi)->sit_entry_set;
4075 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS);
4076 struct seg_entry *se;
4078 down_write(&sit_i->sentry_lock);
4080 if (!sit_i->dirty_sentries)
4084 * add and account sit entries of dirty bitmap in sit entry
4087 add_sits_in_set(sbi);
4090 * if there are no enough space in journal to store dirty sit
4091 * entries, remove all entries from journal and add and account
4092 * them in sit entry set.
4094 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) ||
4096 remove_sits_in_journal(sbi);
4099 * there are two steps to flush sit entries:
4100 * #1, flush sit entries to journal in current cold data summary block.
4101 * #2, flush sit entries to sit page.
4103 list_for_each_entry_safe(ses, tmp, head, set_list) {
4104 struct page *page = NULL;
4105 struct f2fs_sit_block *raw_sit = NULL;
4106 unsigned int start_segno = ses->start_segno;
4107 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
4108 (unsigned long)MAIN_SEGS(sbi));
4109 unsigned int segno = start_segno;
4112 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
4116 down_write(&curseg->journal_rwsem);
4118 page = get_next_sit_page(sbi, start_segno);
4119 raw_sit = page_address(page);
4122 /* flush dirty sit entries in region of current sit set */
4123 for_each_set_bit_from(segno, bitmap, end) {
4124 int offset, sit_offset;
4126 se = get_seg_entry(sbi, segno);
4127 #ifdef CONFIG_F2FS_CHECK_FS
4128 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
4129 SIT_VBLOCK_MAP_SIZE))
4130 f2fs_bug_on(sbi, 1);
4133 /* add discard candidates */
4134 if (!(cpc->reason & CP_DISCARD)) {
4135 cpc->trim_start = segno;
4136 add_discard_addrs(sbi, cpc, false);
4140 offset = f2fs_lookup_journal_in_cursum(journal,
4141 SIT_JOURNAL, segno, 1);
4142 f2fs_bug_on(sbi, offset < 0);
4143 segno_in_journal(journal, offset) =
4145 seg_info_to_raw_sit(se,
4146 &sit_in_journal(journal, offset));
4147 check_block_count(sbi, segno,
4148 &sit_in_journal(journal, offset));
4150 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
4151 seg_info_to_raw_sit(se,
4152 &raw_sit->entries[sit_offset]);
4153 check_block_count(sbi, segno,
4154 &raw_sit->entries[sit_offset]);
4157 __clear_bit(segno, bitmap);
4158 sit_i->dirty_sentries--;
4163 up_write(&curseg->journal_rwsem);
4165 f2fs_put_page(page, 1);
4167 f2fs_bug_on(sbi, ses->entry_cnt);
4168 release_sit_entry_set(ses);
4171 f2fs_bug_on(sbi, !list_empty(head));
4172 f2fs_bug_on(sbi, sit_i->dirty_sentries);
4174 if (cpc->reason & CP_DISCARD) {
4175 __u64 trim_start = cpc->trim_start;
4177 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
4178 add_discard_addrs(sbi, cpc, false);
4180 cpc->trim_start = trim_start;
4182 up_write(&sit_i->sentry_lock);
4184 set_prefree_as_free_segments(sbi);
4187 static int build_sit_info(struct f2fs_sb_info *sbi)
4189 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
4190 struct sit_info *sit_i;
4191 unsigned int sit_segs, start;
4192 char *src_bitmap, *bitmap;
4193 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size;
4194 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0;
4196 /* allocate memory for SIT information */
4197 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
4201 SM_I(sbi)->sit_info = sit_i;
4204 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
4207 if (!sit_i->sentries)
4210 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4211 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size,
4213 if (!sit_i->dirty_sentries_bitmap)
4216 #ifdef CONFIG_F2FS_CHECK_FS
4217 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map);
4219 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map);
4221 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4225 bitmap = sit_i->bitmap;
4227 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4228 sit_i->sentries[start].cur_valid_map = bitmap;
4229 bitmap += SIT_VBLOCK_MAP_SIZE;
4231 sit_i->sentries[start].ckpt_valid_map = bitmap;
4232 bitmap += SIT_VBLOCK_MAP_SIZE;
4234 #ifdef CONFIG_F2FS_CHECK_FS
4235 sit_i->sentries[start].cur_valid_map_mir = bitmap;
4236 bitmap += SIT_VBLOCK_MAP_SIZE;
4240 sit_i->sentries[start].discard_map = bitmap;
4241 bitmap += SIT_VBLOCK_MAP_SIZE;
4245 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
4246 if (!sit_i->tmp_map)
4249 if (__is_large_section(sbi)) {
4250 sit_i->sec_entries =
4251 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
4254 if (!sit_i->sec_entries)
4258 /* get information related with SIT */
4259 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
4261 /* setup SIT bitmap from ckeckpoint pack */
4262 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
4263 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
4265 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL);
4266 if (!sit_i->sit_bitmap)
4269 #ifdef CONFIG_F2FS_CHECK_FS
4270 sit_i->sit_bitmap_mir = kmemdup(src_bitmap,
4271 sit_bitmap_size, GFP_KERNEL);
4272 if (!sit_i->sit_bitmap_mir)
4275 sit_i->invalid_segmap = f2fs_kvzalloc(sbi,
4276 main_bitmap_size, GFP_KERNEL);
4277 if (!sit_i->invalid_segmap)
4281 /* init SIT information */
4282 sit_i->s_ops = &default_salloc_ops;
4284 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
4285 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
4286 sit_i->written_valid_blocks = 0;
4287 sit_i->bitmap_size = sit_bitmap_size;
4288 sit_i->dirty_sentries = 0;
4289 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
4290 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
4291 sit_i->mounted_time = ktime_get_boottime_seconds();
4292 init_rwsem(&sit_i->sentry_lock);
4296 static int build_free_segmap(struct f2fs_sb_info *sbi)
4298 struct free_segmap_info *free_i;
4299 unsigned int bitmap_size, sec_bitmap_size;
4301 /* allocate memory for free segmap information */
4302 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
4306 SM_I(sbi)->free_info = free_i;
4308 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4309 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
4310 if (!free_i->free_segmap)
4313 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4314 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
4315 if (!free_i->free_secmap)
4318 /* set all segments as dirty temporarily */
4319 memset(free_i->free_segmap, 0xff, bitmap_size);
4320 memset(free_i->free_secmap, 0xff, sec_bitmap_size);
4322 /* init free segmap information */
4323 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
4324 free_i->free_segments = 0;
4325 free_i->free_sections = 0;
4326 spin_lock_init(&free_i->segmap_lock);
4330 static int build_curseg(struct f2fs_sb_info *sbi)
4332 struct curseg_info *array;
4335 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE,
4336 sizeof(*array)), GFP_KERNEL);
4340 SM_I(sbi)->curseg_array = array;
4342 for (i = 0; i < NO_CHECK_TYPE; i++) {
4343 mutex_init(&array[i].curseg_mutex);
4344 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
4345 if (!array[i].sum_blk)
4347 init_rwsem(&array[i].journal_rwsem);
4348 array[i].journal = f2fs_kzalloc(sbi,
4349 sizeof(struct f2fs_journal), GFP_KERNEL);
4350 if (!array[i].journal)
4352 if (i < NR_PERSISTENT_LOG)
4353 array[i].seg_type = CURSEG_HOT_DATA + i;
4354 else if (i == CURSEG_COLD_DATA_PINNED)
4355 array[i].seg_type = CURSEG_COLD_DATA;
4356 else if (i == CURSEG_ALL_DATA_ATGC)
4357 array[i].seg_type = CURSEG_COLD_DATA;
4358 array[i].segno = NULL_SEGNO;
4359 array[i].next_blkoff = 0;
4360 array[i].inited = false;
4362 return restore_curseg_summaries(sbi);
4365 static int build_sit_entries(struct f2fs_sb_info *sbi)
4367 struct sit_info *sit_i = SIT_I(sbi);
4368 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
4369 struct f2fs_journal *journal = curseg->journal;
4370 struct seg_entry *se;
4371 struct f2fs_sit_entry sit;
4372 int sit_blk_cnt = SIT_BLK_CNT(sbi);
4373 unsigned int i, start, end;
4374 unsigned int readed, start_blk = 0;
4376 block_t sit_valid_blocks[2] = {0, 0};
4379 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS,
4382 start = start_blk * sit_i->sents_per_block;
4383 end = (start_blk + readed) * sit_i->sents_per_block;
4385 for (; start < end && start < MAIN_SEGS(sbi); start++) {
4386 struct f2fs_sit_block *sit_blk;
4389 se = &sit_i->sentries[start];
4390 page = get_current_sit_page(sbi, start);
4392 return PTR_ERR(page);
4393 sit_blk = (struct f2fs_sit_block *)page_address(page);
4394 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
4395 f2fs_put_page(page, 1);
4397 err = check_block_count(sbi, start, &sit);
4400 seg_info_from_raw_sit(se, &sit);
4402 if (se->type >= NR_PERSISTENT_LOG) {
4403 f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
4405 f2fs_handle_error(sbi,
4406 ERROR_INCONSISTENT_SUM_TYPE);
4407 return -EFSCORRUPTED;
4410 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
4412 if (f2fs_block_unit_discard(sbi)) {
4413 /* build discard map only one time */
4414 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4415 memset(se->discard_map, 0xff,
4416 SIT_VBLOCK_MAP_SIZE);
4418 memcpy(se->discard_map,
4420 SIT_VBLOCK_MAP_SIZE);
4421 sbi->discard_blks +=
4422 sbi->blocks_per_seg -
4427 if (__is_large_section(sbi))
4428 get_sec_entry(sbi, start)->valid_blocks +=
4431 start_blk += readed;
4432 } while (start_blk < sit_blk_cnt);
4434 down_read(&curseg->journal_rwsem);
4435 for (i = 0; i < sits_in_cursum(journal); i++) {
4436 unsigned int old_valid_blocks;
4438 start = le32_to_cpu(segno_in_journal(journal, i));
4439 if (start >= MAIN_SEGS(sbi)) {
4440 f2fs_err(sbi, "Wrong journal entry on segno %u",
4442 err = -EFSCORRUPTED;
4443 f2fs_handle_error(sbi, ERROR_CORRUPTED_JOURNAL);
4447 se = &sit_i->sentries[start];
4448 sit = sit_in_journal(journal, i);
4450 old_valid_blocks = se->valid_blocks;
4452 sit_valid_blocks[SE_PAGETYPE(se)] -= old_valid_blocks;
4454 err = check_block_count(sbi, start, &sit);
4457 seg_info_from_raw_sit(se, &sit);
4459 if (se->type >= NR_PERSISTENT_LOG) {
4460 f2fs_err(sbi, "Invalid segment type: %u, segno: %u",
4462 err = -EFSCORRUPTED;
4463 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE);
4467 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks;
4469 if (f2fs_block_unit_discard(sbi)) {
4470 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
4471 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
4473 memcpy(se->discard_map, se->cur_valid_map,
4474 SIT_VBLOCK_MAP_SIZE);
4475 sbi->discard_blks += old_valid_blocks;
4476 sbi->discard_blks -= se->valid_blocks;
4480 if (__is_large_section(sbi)) {
4481 get_sec_entry(sbi, start)->valid_blocks +=
4483 get_sec_entry(sbi, start)->valid_blocks -=
4487 up_read(&curseg->journal_rwsem);
4492 if (sit_valid_blocks[NODE] != valid_node_count(sbi)) {
4493 f2fs_err(sbi, "SIT is corrupted node# %u vs %u",
4494 sit_valid_blocks[NODE], valid_node_count(sbi));
4495 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NODE_COUNT);
4496 return -EFSCORRUPTED;
4499 if (sit_valid_blocks[DATA] + sit_valid_blocks[NODE] >
4500 valid_user_blocks(sbi)) {
4501 f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u",
4502 sit_valid_blocks[DATA], sit_valid_blocks[NODE],
4503 valid_user_blocks(sbi));
4504 f2fs_handle_error(sbi, ERROR_INCONSISTENT_BLOCK_COUNT);
4505 return -EFSCORRUPTED;
4511 static void init_free_segmap(struct f2fs_sb_info *sbi)
4515 struct seg_entry *sentry;
4517 for (start = 0; start < MAIN_SEGS(sbi); start++) {
4518 if (f2fs_usable_blks_in_seg(sbi, start) == 0)
4520 sentry = get_seg_entry(sbi, start);
4521 if (!sentry->valid_blocks)
4522 __set_free(sbi, start);
4524 SIT_I(sbi)->written_valid_blocks +=
4525 sentry->valid_blocks;
4528 /* set use the current segments */
4529 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
4530 struct curseg_info *curseg_t = CURSEG_I(sbi, type);
4532 __set_test_and_inuse(sbi, curseg_t->segno);
4536 static void init_dirty_segmap(struct f2fs_sb_info *sbi)
4538 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4539 struct free_segmap_info *free_i = FREE_I(sbi);
4540 unsigned int segno = 0, offset = 0, secno;
4541 block_t valid_blocks, usable_blks_in_seg;
4544 /* find dirty segment based on free segmap */
4545 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
4546 if (segno >= MAIN_SEGS(sbi))
4549 valid_blocks = get_valid_blocks(sbi, segno, false);
4550 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno);
4551 if (valid_blocks == usable_blks_in_seg || !valid_blocks)
4553 if (valid_blocks > usable_blks_in_seg) {
4554 f2fs_bug_on(sbi, 1);
4557 mutex_lock(&dirty_i->seglist_lock);
4558 __locate_dirty_segment(sbi, segno, DIRTY);
4559 mutex_unlock(&dirty_i->seglist_lock);
4562 if (!__is_large_section(sbi))
4565 mutex_lock(&dirty_i->seglist_lock);
4566 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
4567 valid_blocks = get_valid_blocks(sbi, segno, true);
4568 secno = GET_SEC_FROM_SEG(sbi, segno);
4570 if (!valid_blocks || valid_blocks == CAP_BLKS_PER_SEC(sbi))
4572 if (IS_CURSEC(sbi, secno))
4574 set_bit(secno, dirty_i->dirty_secmap);
4576 mutex_unlock(&dirty_i->seglist_lock);
4579 static int init_victim_secmap(struct f2fs_sb_info *sbi)
4581 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
4582 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4584 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4585 if (!dirty_i->victim_secmap)
4588 dirty_i->pinned_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
4589 if (!dirty_i->pinned_secmap)
4592 dirty_i->pinned_secmap_cnt = 0;
4593 dirty_i->enable_pin_section = true;
4597 static int build_dirty_segmap(struct f2fs_sb_info *sbi)
4599 struct dirty_seglist_info *dirty_i;
4600 unsigned int bitmap_size, i;
4602 /* allocate memory for dirty segments list information */
4603 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
4608 SM_I(sbi)->dirty_info = dirty_i;
4609 mutex_init(&dirty_i->seglist_lock);
4611 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
4613 for (i = 0; i < NR_DIRTY_TYPE; i++) {
4614 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
4616 if (!dirty_i->dirty_segmap[i])
4620 if (__is_large_section(sbi)) {
4621 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
4622 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi,
4623 bitmap_size, GFP_KERNEL);
4624 if (!dirty_i->dirty_secmap)
4628 init_dirty_segmap(sbi);
4629 return init_victim_secmap(sbi);
4632 static int sanity_check_curseg(struct f2fs_sb_info *sbi)
4637 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
4638 * In LFS curseg, all blkaddr after .next_blkoff should be unused.
4640 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4641 struct curseg_info *curseg = CURSEG_I(sbi, i);
4642 struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
4643 unsigned int blkofs = curseg->next_blkoff;
4645 if (f2fs_sb_has_readonly(sbi) &&
4646 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE)
4649 sanity_check_seg_type(sbi, curseg->seg_type);
4651 if (curseg->alloc_type != LFS && curseg->alloc_type != SSR) {
4653 "Current segment has invalid alloc_type:%d",
4654 curseg->alloc_type);
4655 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
4656 return -EFSCORRUPTED;
4659 if (f2fs_test_bit(blkofs, se->cur_valid_map))
4662 if (curseg->alloc_type == SSR)
4665 for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
4666 if (!f2fs_test_bit(blkofs, se->cur_valid_map))
4670 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
4671 i, curseg->segno, curseg->alloc_type,
4672 curseg->next_blkoff, blkofs);
4673 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG);
4674 return -EFSCORRUPTED;
4680 #ifdef CONFIG_BLK_DEV_ZONED
4682 static int check_zone_write_pointer(struct f2fs_sb_info *sbi,
4683 struct f2fs_dev_info *fdev,
4684 struct blk_zone *zone)
4686 unsigned int wp_segno, wp_blkoff, zone_secno, zone_segno, segno;
4687 block_t zone_block, wp_block, last_valid_block;
4688 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4690 struct seg_entry *se;
4692 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4695 wp_block = fdev->start_blk + (zone->wp >> log_sectors_per_block);
4696 wp_segno = GET_SEGNO(sbi, wp_block);
4697 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4698 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block);
4699 zone_segno = GET_SEGNO(sbi, zone_block);
4700 zone_secno = GET_SEC_FROM_SEG(sbi, zone_segno);
4702 if (zone_segno >= MAIN_SEGS(sbi))
4706 * Skip check of zones cursegs point to, since
4707 * fix_curseg_write_pointer() checks them.
4709 for (i = 0; i < NO_CHECK_TYPE; i++)
4710 if (zone_secno == GET_SEC_FROM_SEG(sbi,
4711 CURSEG_I(sbi, i)->segno))
4715 * Get last valid block of the zone.
4717 last_valid_block = zone_block - 1;
4718 for (s = sbi->segs_per_sec - 1; s >= 0; s--) {
4719 segno = zone_segno + s;
4720 se = get_seg_entry(sbi, segno);
4721 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
4722 if (f2fs_test_bit(b, se->cur_valid_map)) {
4723 last_valid_block = START_BLOCK(sbi, segno) + b;
4726 if (last_valid_block >= zone_block)
4731 * If last valid block is beyond the write pointer, report the
4732 * inconsistency. This inconsistency does not cause write error
4733 * because the zone will not be selected for write operation until
4734 * it get discarded. Just report it.
4736 if (last_valid_block >= wp_block) {
4737 f2fs_notice(sbi, "Valid block beyond write pointer: "
4738 "valid block[0x%x,0x%x] wp[0x%x,0x%x]",
4739 GET_SEGNO(sbi, last_valid_block),
4740 GET_BLKOFF_FROM_SEG0(sbi, last_valid_block),
4741 wp_segno, wp_blkoff);
4746 * If there is no valid block in the zone and if write pointer is
4747 * not at zone start, reset the write pointer.
4749 if (last_valid_block + 1 == zone_block && zone->wp != zone->start) {
4751 "Zone without valid block has non-zero write "
4752 "pointer. Reset the write pointer: wp[0x%x,0x%x]",
4753 wp_segno, wp_blkoff);
4754 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block,
4755 zone->len >> log_sectors_per_block);
4757 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4766 static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi,
4767 block_t zone_blkaddr)
4771 for (i = 0; i < sbi->s_ndevs; i++) {
4772 if (!bdev_is_zoned(FDEV(i).bdev))
4774 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr &&
4775 zone_blkaddr <= FDEV(i).end_blk))
4782 static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx,
4785 memcpy(data, zone, sizeof(struct blk_zone));
4789 static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
4791 struct curseg_info *cs = CURSEG_I(sbi, type);
4792 struct f2fs_dev_info *zbd;
4793 struct blk_zone zone;
4794 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off;
4795 block_t cs_zone_block, wp_block;
4796 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
4797 sector_t zone_sector;
4800 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4801 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4803 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4807 /* report zone for the sector the curseg points to */
4808 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4809 << log_sectors_per_block;
4810 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4811 report_one_zone_cb, &zone);
4813 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4818 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4821 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block);
4822 wp_segno = GET_SEGNO(sbi, wp_block);
4823 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
4824 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0);
4826 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff &&
4830 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: "
4831 "curseg[0x%x,0x%x] wp[0x%x,0x%x]",
4832 type, cs->segno, cs->next_blkoff, wp_segno, wp_blkoff);
4834 f2fs_notice(sbi, "Assign new section to curseg[%d]: "
4835 "curseg[0x%x,0x%x]", type, cs->segno, cs->next_blkoff);
4837 f2fs_allocate_new_section(sbi, type, true);
4839 /* check consistency of the zone curseg pointed to */
4840 if (check_zone_write_pointer(sbi, zbd, &zone))
4843 /* check newly assigned zone */
4844 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno);
4845 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section));
4847 zbd = get_target_zoned_dev(sbi, cs_zone_block);
4851 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk)
4852 << log_sectors_per_block;
4853 err = blkdev_report_zones(zbd->bdev, zone_sector, 1,
4854 report_one_zone_cb, &zone);
4856 f2fs_err(sbi, "Report zone failed: %s errno=(%d)",
4861 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ)
4864 if (zone.wp != zone.start) {
4866 "New zone for curseg[%d] is not yet discarded. "
4867 "Reset the zone: curseg[0x%x,0x%x]",
4868 type, cs->segno, cs->next_blkoff);
4869 err = __f2fs_issue_discard_zone(sbi, zbd->bdev,
4870 zone_sector >> log_sectors_per_block,
4871 zone.len >> log_sectors_per_block);
4873 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)",
4882 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4886 for (i = 0; i < NR_PERSISTENT_LOG; i++) {
4887 ret = fix_curseg_write_pointer(sbi, i);
4895 struct check_zone_write_pointer_args {
4896 struct f2fs_sb_info *sbi;
4897 struct f2fs_dev_info *fdev;
4900 static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx,
4903 struct check_zone_write_pointer_args *args;
4905 args = (struct check_zone_write_pointer_args *)data;
4907 return check_zone_write_pointer(args->sbi, args->fdev, zone);
4910 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4913 struct check_zone_write_pointer_args args;
4915 for (i = 0; i < sbi->s_ndevs; i++) {
4916 if (!bdev_is_zoned(FDEV(i).bdev))
4920 args.fdev = &FDEV(i);
4921 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES,
4922 check_zone_write_pointer_cb, &args);
4931 * Return the number of usable blocks in a segment. The number of blocks
4932 * returned is always equal to the number of blocks in a segment for
4933 * segments fully contained within a sequential zone capacity or a
4934 * conventional zone. For segments partially contained in a sequential
4935 * zone capacity, the number of usable blocks up to the zone capacity
4936 * is returned. 0 is returned in all other cases.
4938 static inline unsigned int f2fs_usable_zone_blks_in_seg(
4939 struct f2fs_sb_info *sbi, unsigned int segno)
4941 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr;
4944 if (!sbi->unusable_blocks_per_sec)
4945 return sbi->blocks_per_seg;
4947 secno = GET_SEC_FROM_SEG(sbi, segno);
4948 seg_start = START_BLOCK(sbi, segno);
4949 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
4950 sec_cap_blkaddr = sec_start_blkaddr + CAP_BLKS_PER_SEC(sbi);
4953 * If segment starts before zone capacity and spans beyond
4954 * zone capacity, then usable blocks are from seg start to
4955 * zone capacity. If the segment starts after the zone capacity,
4956 * then there are no usable blocks.
4958 if (seg_start >= sec_cap_blkaddr)
4960 if (seg_start + sbi->blocks_per_seg > sec_cap_blkaddr)
4961 return sec_cap_blkaddr - seg_start;
4963 return sbi->blocks_per_seg;
4966 int f2fs_fix_curseg_write_pointer(struct f2fs_sb_info *sbi)
4971 int f2fs_check_write_pointer(struct f2fs_sb_info *sbi)
4976 static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi,
4983 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
4986 if (f2fs_sb_has_blkzoned(sbi))
4987 return f2fs_usable_zone_blks_in_seg(sbi, segno);
4989 return sbi->blocks_per_seg;
4992 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi,
4995 if (f2fs_sb_has_blkzoned(sbi))
4996 return CAP_SEGS_PER_SEC(sbi);
4998 return sbi->segs_per_sec;
5002 * Update min, max modified time for cost-benefit GC algorithm
5004 static void init_min_max_mtime(struct f2fs_sb_info *sbi)
5006 struct sit_info *sit_i = SIT_I(sbi);
5009 down_write(&sit_i->sentry_lock);
5011 sit_i->min_mtime = ULLONG_MAX;
5013 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
5015 unsigned long long mtime = 0;
5017 for (i = 0; i < sbi->segs_per_sec; i++)
5018 mtime += get_seg_entry(sbi, segno + i)->mtime;
5020 mtime = div_u64(mtime, sbi->segs_per_sec);
5022 if (sit_i->min_mtime > mtime)
5023 sit_i->min_mtime = mtime;
5025 sit_i->max_mtime = get_mtime(sbi, false);
5026 sit_i->dirty_max_mtime = 0;
5027 up_write(&sit_i->sentry_lock);
5030 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
5032 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
5033 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
5034 struct f2fs_sm_info *sm_info;
5037 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
5042 sbi->sm_info = sm_info;
5043 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
5044 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
5045 sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
5046 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
5047 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
5048 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
5049 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
5050 sm_info->rec_prefree_segments = sm_info->main_segments *
5051 DEF_RECLAIM_PREFREE_SEGMENTS / 100;
5052 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
5053 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
5055 if (!f2fs_lfs_mode(sbi))
5056 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
5057 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
5058 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
5059 sm_info->min_seq_blocks = sbi->blocks_per_seg;
5060 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
5061 sm_info->min_ssr_sections = reserved_sections(sbi);
5063 INIT_LIST_HEAD(&sm_info->sit_entry_set);
5065 init_f2fs_rwsem(&sm_info->curseg_lock);
5067 if (!f2fs_readonly(sbi->sb)) {
5068 err = f2fs_create_flush_cmd_control(sbi);
5073 err = create_discard_cmd_control(sbi);
5077 err = build_sit_info(sbi);
5080 err = build_free_segmap(sbi);
5083 err = build_curseg(sbi);
5087 /* reinit free segmap based on SIT */
5088 err = build_sit_entries(sbi);
5092 init_free_segmap(sbi);
5093 err = build_dirty_segmap(sbi);
5097 err = sanity_check_curseg(sbi);
5101 init_min_max_mtime(sbi);
5105 static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
5106 enum dirty_type dirty_type)
5108 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5110 mutex_lock(&dirty_i->seglist_lock);
5111 kvfree(dirty_i->dirty_segmap[dirty_type]);
5112 dirty_i->nr_dirty[dirty_type] = 0;
5113 mutex_unlock(&dirty_i->seglist_lock);
5116 static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
5118 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5120 kvfree(dirty_i->pinned_secmap);
5121 kvfree(dirty_i->victim_secmap);
5124 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
5126 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
5132 /* discard pre-free/dirty segments list */
5133 for (i = 0; i < NR_DIRTY_TYPE; i++)
5134 discard_dirty_segmap(sbi, i);
5136 if (__is_large_section(sbi)) {
5137 mutex_lock(&dirty_i->seglist_lock);
5138 kvfree(dirty_i->dirty_secmap);
5139 mutex_unlock(&dirty_i->seglist_lock);
5142 destroy_victim_secmap(sbi);
5143 SM_I(sbi)->dirty_info = NULL;
5147 static void destroy_curseg(struct f2fs_sb_info *sbi)
5149 struct curseg_info *array = SM_I(sbi)->curseg_array;
5154 SM_I(sbi)->curseg_array = NULL;
5155 for (i = 0; i < NR_CURSEG_TYPE; i++) {
5156 kfree(array[i].sum_blk);
5157 kfree(array[i].journal);
5162 static void destroy_free_segmap(struct f2fs_sb_info *sbi)
5164 struct free_segmap_info *free_i = SM_I(sbi)->free_info;
5168 SM_I(sbi)->free_info = NULL;
5169 kvfree(free_i->free_segmap);
5170 kvfree(free_i->free_secmap);
5174 static void destroy_sit_info(struct f2fs_sb_info *sbi)
5176 struct sit_info *sit_i = SIT_I(sbi);
5181 if (sit_i->sentries)
5182 kvfree(sit_i->bitmap);
5183 kfree(sit_i->tmp_map);
5185 kvfree(sit_i->sentries);
5186 kvfree(sit_i->sec_entries);
5187 kvfree(sit_i->dirty_sentries_bitmap);
5189 SM_I(sbi)->sit_info = NULL;
5190 kvfree(sit_i->sit_bitmap);
5191 #ifdef CONFIG_F2FS_CHECK_FS
5192 kvfree(sit_i->sit_bitmap_mir);
5193 kvfree(sit_i->invalid_segmap);
5198 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
5200 struct f2fs_sm_info *sm_info = SM_I(sbi);
5204 f2fs_destroy_flush_cmd_control(sbi, true);
5205 destroy_discard_cmd_control(sbi);
5206 destroy_dirty_segmap(sbi);
5207 destroy_curseg(sbi);
5208 destroy_free_segmap(sbi);
5209 destroy_sit_info(sbi);
5210 sbi->sm_info = NULL;
5214 int __init f2fs_create_segment_manager_caches(void)
5216 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry",
5217 sizeof(struct discard_entry));
5218 if (!discard_entry_slab)
5221 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd",
5222 sizeof(struct discard_cmd));
5223 if (!discard_cmd_slab)
5224 goto destroy_discard_entry;
5226 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set",
5227 sizeof(struct sit_entry_set));
5228 if (!sit_entry_set_slab)
5229 goto destroy_discard_cmd;
5231 revoke_entry_slab = f2fs_kmem_cache_create("f2fs_revoke_entry",
5232 sizeof(struct revoke_entry));
5233 if (!revoke_entry_slab)
5234 goto destroy_sit_entry_set;
5237 destroy_sit_entry_set:
5238 kmem_cache_destroy(sit_entry_set_slab);
5239 destroy_discard_cmd:
5240 kmem_cache_destroy(discard_cmd_slab);
5241 destroy_discard_entry:
5242 kmem_cache_destroy(discard_entry_slab);
5247 void f2fs_destroy_segment_manager_caches(void)
5249 kmem_cache_destroy(sit_entry_set_slab);
5250 kmem_cache_destroy(discard_cmd_slab);
5251 kmem_cache_destroy(discard_entry_slab);
5252 kmem_cache_destroy(revoke_entry_slab);