^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/f2fs/gc.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2012 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define GC_THREAD_MIN_WB_PAGES 1 /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * a threshold to determine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * whether IO subsystem is idle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define DEF_GC_THREAD_URGENT_SLEEP_TIME 500 /* 500 ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define DEF_GC_THREAD_MIN_SLEEP_TIME 30000 /* milliseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define DEF_GC_THREAD_MAX_SLEEP_TIME 60000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define DEF_GC_THREAD_NOGC_SLEEP_TIME 300000 /* wait 5 min */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* choose candidates from sections which has age of more than 7 days */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define DEF_GC_THREAD_AGE_THRESHOLD (60 * 60 * 24 * 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define DEF_GC_THREAD_CANDIDATE_RATIO 20 /* select 20% oldest sections as candidates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DEF_GC_THREAD_MAX_CANDIDATE_COUNT 10 /* select at most 10 sections as candidates */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define DEF_GC_THREAD_AGE_WEIGHT 60 /* age weight */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define DEFAULT_ACCURACY_CLASS 10000 /* accuracy class */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define LIMIT_FREE_BLOCK 40 /* percentage over invalid + free space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DEF_GC_FAILED_PINNED_FILES 2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Search max. number of dirty segments to select a victim segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct f2fs_gc_kthread {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct task_struct *f2fs_gc_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) wait_queue_head_t gc_wait_queue_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* for gc sleep time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int urgent_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int min_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int max_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int no_gc_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* for changing gc mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) unsigned int gc_wake;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* for GC_MERGE mount option */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) wait_queue_head_t fggc_wq; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * caller of f2fs_balance_fs()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * will wait on this wait queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct gc_inode_list {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct list_head ilist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct radix_tree_root iroot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct victim_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) unsigned long long mtime; /* mtime of section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int segno; /* section No. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct victim_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct rb_node rb_node; /* rb node located in rb-tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned long long mtime; /* mtime of section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) unsigned int segno; /* segment No. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct victim_info vi; /* victim info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * inline functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * On a Zoned device zone-capacity can be less than zone-size and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * starting just before zone-capacity has some blocks spanning across the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * zone-capacity, these blocks are not usable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Such spanning segments can be in free list so calculate the sum of usable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * blocks in currently free segments including normal and spanning segments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) block_t free_seg_blks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct free_segmap_info *free_i = FREE_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) spin_lock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) for (j = 0; j < MAIN_SEGS(sbi); j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!test_bit(j, free_i->free_segmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) free_seg_blks += f2fs_usable_blks_in_seg(sbi, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) spin_unlock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return free_seg_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (f2fs_sb_has_blkzoned(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return free_segs_blk_count_zoned(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return free_segments(sbi) << sbi->log_blocks_per_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static inline block_t free_user_blocks(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) block_t free_blks, ovp_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) free_blks = free_segs_blk_count(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ovp_blks = overprovision_segments(sbi) << sbi->log_blocks_per_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (free_blks < ovp_blks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return free_blks - ovp_blks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static inline block_t limit_invalid_user_blocks(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return (long)(sbi->user_block_count * LIMIT_INVALID_BLOCK) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static inline block_t limit_free_user_blocks(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) block_t reclaimable_user_blocks = sbi->user_block_count -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) written_block_count(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unsigned int *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned int min_time = gc_th->min_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned int max_time = gc_th->max_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (*wait == gc_th->no_gc_sleep_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if ((long long)*wait + (long long)min_time > (long long)max_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *wait = max_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *wait += min_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned int *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned int min_time = gc_th->min_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (*wait == gc_th->no_gc_sleep_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) *wait = gc_th->max_sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if ((long long)*wait - (long long)min_time < (long long)min_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) *wait = min_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *wait -= min_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) block_t invalid_user_blocks = sbi->user_block_count -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) written_block_count(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Background GC is triggered with the following conditions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * 1. There are a number of invalid blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * 2. There is not enough free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (invalid_user_blocks > limit_invalid_user_blocks(sbi) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) free_user_blocks(sbi) < limit_free_user_blocks(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }