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