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