1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 #define GC_THREAD_MIN_WB_PAGES 1 /*
9 * a threshold to determine
10 * whether IO subsystem is idle
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 */
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 */
25 #define LIMIT_INVALID_BLOCK 40 /* percentage over total user space */
26 #define LIMIT_FREE_BLOCK 40 /* percentage over invalid + free space */
28 #define DEF_GC_FAILED_PINNED_FILES 2048
30 /* Search max. number of dirty segments to select a victim segment */
31 #define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
33 struct f2fs_gc_kthread
{
34 struct task_struct
*f2fs_gc_task
;
35 wait_queue_head_t gc_wait_queue_head
;
37 /* for gc sleep time */
38 unsigned int urgent_sleep_time
;
39 unsigned int min_sleep_time
;
40 unsigned int max_sleep_time
;
41 unsigned int no_gc_sleep_time
;
43 /* for changing gc mode */
47 struct gc_inode_list
{
48 struct list_head ilist
;
49 struct radix_tree_root iroot
;
53 unsigned long long mtime
; /* mtime of section */
54 unsigned int segno
; /* section No. */
58 struct rb_node rb_node
; /* rb node located in rb-tree */
61 unsigned long long mtime
; /* mtime of section */
62 unsigned int segno
; /* segment No. */
64 struct victim_info vi
; /* victim info */
66 struct list_head list
;
74 * On a Zoned device zone-capacity can be less than zone-size and if
75 * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
76 * starting just before zone-capacity has some blocks spanning across the
77 * zone-capacity, these blocks are not usable.
78 * Such spanning segments can be in free list so calculate the sum of usable
79 * blocks in currently free segments including normal and spanning segments.
81 static inline block_t
free_segs_blk_count_zoned(struct f2fs_sb_info
*sbi
)
83 block_t free_seg_blks
= 0;
84 struct free_segmap_info
*free_i
= FREE_I(sbi
);
87 spin_lock(&free_i
->segmap_lock
);
88 for (j
= 0; j
< MAIN_SEGS(sbi
); j
++)
89 if (!test_bit(j
, free_i
->free_segmap
))
90 free_seg_blks
+= f2fs_usable_blks_in_seg(sbi
, j
);
91 spin_unlock(&free_i
->segmap_lock
);
96 static inline block_t
free_segs_blk_count(struct f2fs_sb_info
*sbi
)
98 if (f2fs_sb_has_blkzoned(sbi
))
99 return free_segs_blk_count_zoned(sbi
);
101 return free_segments(sbi
) << sbi
->log_blocks_per_seg
;
104 static inline block_t
free_user_blocks(struct f2fs_sb_info
*sbi
)
106 block_t free_blks
, ovp_blks
;
108 free_blks
= free_segs_blk_count(sbi
);
109 ovp_blks
= overprovision_segments(sbi
) << sbi
->log_blocks_per_seg
;
111 if (free_blks
< ovp_blks
)
114 return free_blks
- ovp_blks
;
117 static inline block_t
limit_invalid_user_blocks(struct f2fs_sb_info
*sbi
)
119 return (long)(sbi
->user_block_count
* LIMIT_INVALID_BLOCK
) / 100;
122 static inline block_t
limit_free_user_blocks(struct f2fs_sb_info
*sbi
)
124 block_t reclaimable_user_blocks
= sbi
->user_block_count
-
125 written_block_count(sbi
);
126 return (long)(reclaimable_user_blocks
* LIMIT_FREE_BLOCK
) / 100;
129 static inline void increase_sleep_time(struct f2fs_gc_kthread
*gc_th
,
132 unsigned int min_time
= gc_th
->min_sleep_time
;
133 unsigned int max_time
= gc_th
->max_sleep_time
;
135 if (*wait
== gc_th
->no_gc_sleep_time
)
138 if ((long long)*wait
+ (long long)min_time
> (long long)max_time
)
144 static inline void decrease_sleep_time(struct f2fs_gc_kthread
*gc_th
,
147 unsigned int min_time
= gc_th
->min_sleep_time
;
149 if (*wait
== gc_th
->no_gc_sleep_time
)
150 *wait
= gc_th
->max_sleep_time
;
152 if ((long long)*wait
- (long long)min_time
< (long long)min_time
)
158 static inline bool has_enough_invalid_blocks(struct f2fs_sb_info
*sbi
)
160 block_t invalid_user_blocks
= sbi
->user_block_count
-
161 written_block_count(sbi
);
163 * Background GC is triggered with the following conditions.
164 * 1. There are a number of invalid blocks.
165 * 2. There is not enough free space.
167 if (invalid_user_blocks
> limit_invalid_user_blocks(sbi
) &&
168 free_user_blocks(sbi
) < limit_free_user_blocks(sbi
))