18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fs/f2fs/gc.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2012 Samsung Electronics Co., Ltd.
68c2ecf20Sopenharmony_ci *             http://www.samsung.com/
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#define GC_THREAD_MIN_WB_PAGES		1	/*
98c2ecf20Sopenharmony_ci						 * a threshold to determine
108c2ecf20Sopenharmony_ci						 * whether IO subsystem is idle
118c2ecf20Sopenharmony_ci						 * or not
128c2ecf20Sopenharmony_ci						 */
138c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_URGENT_SLEEP_TIME	500	/* 500 ms */
148c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_MIN_SLEEP_TIME	30000	/* milliseconds */
158c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_MAX_SLEEP_TIME	60000
168c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_NOGC_SLEEP_TIME	300000	/* wait 5 min */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* choose candidates from sections which has age of more than 7 days */
198c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_AGE_THRESHOLD		(60 * 60 * 24 * 7)
208c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_CANDIDATE_RATIO		20	/* select 20% oldest sections as candidates */
218c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_MAX_CANDIDATE_COUNT	10	/* select at most 10 sections as candidates */
228c2ecf20Sopenharmony_ci#define DEF_GC_THREAD_AGE_WEIGHT		60	/* age weight */
238c2ecf20Sopenharmony_ci#define DEFAULT_ACCURACY_CLASS			10000	/* accuracy class */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define LIMIT_INVALID_BLOCK	40 /* percentage over total user space */
268c2ecf20Sopenharmony_ci#define LIMIT_FREE_BLOCK	40 /* percentage over invalid + free space */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define DEF_GC_FAILED_PINNED_FILES	2048
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* Search max. number of dirty segments to select a victim segment */
318c2ecf20Sopenharmony_ci#define DEF_MAX_VICTIM_SEARCH 4096 /* covers 8GB */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistruct f2fs_gc_kthread {
348c2ecf20Sopenharmony_ci	struct task_struct *f2fs_gc_task;
358c2ecf20Sopenharmony_ci	wait_queue_head_t gc_wait_queue_head;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	/* for gc sleep time */
388c2ecf20Sopenharmony_ci	unsigned int urgent_sleep_time;
398c2ecf20Sopenharmony_ci	unsigned int min_sleep_time;
408c2ecf20Sopenharmony_ci	unsigned int max_sleep_time;
418c2ecf20Sopenharmony_ci	unsigned int no_gc_sleep_time;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	/* for changing gc mode */
448c2ecf20Sopenharmony_ci	unsigned int gc_wake;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	/* for GC_MERGE mount option */
478c2ecf20Sopenharmony_ci	wait_queue_head_t fggc_wq;		/*
488c2ecf20Sopenharmony_ci						 * caller of f2fs_balance_fs()
498c2ecf20Sopenharmony_ci						 * will wait on this wait queue.
508c2ecf20Sopenharmony_ci						 */
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistruct gc_inode_list {
548c2ecf20Sopenharmony_ci	struct list_head ilist;
558c2ecf20Sopenharmony_ci	struct radix_tree_root iroot;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct victim_info {
598c2ecf20Sopenharmony_ci	unsigned long long mtime;	/* mtime of section */
608c2ecf20Sopenharmony_ci	unsigned int segno;		/* section No. */
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct victim_entry {
648c2ecf20Sopenharmony_ci	struct rb_node rb_node;		/* rb node located in rb-tree */
658c2ecf20Sopenharmony_ci	union {
668c2ecf20Sopenharmony_ci		struct {
678c2ecf20Sopenharmony_ci			unsigned long long mtime;	/* mtime of section */
688c2ecf20Sopenharmony_ci			unsigned int segno;		/* segment No. */
698c2ecf20Sopenharmony_ci		};
708c2ecf20Sopenharmony_ci		struct victim_info vi;	/* victim info */
718c2ecf20Sopenharmony_ci	};
728c2ecf20Sopenharmony_ci	struct list_head list;
738c2ecf20Sopenharmony_ci};
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/*
768c2ecf20Sopenharmony_ci * inline functions
778c2ecf20Sopenharmony_ci */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * On a Zoned device zone-capacity can be less than zone-size and if
818c2ecf20Sopenharmony_ci * zone-capacity is not aligned to f2fs segment size(2MB), then the segment
828c2ecf20Sopenharmony_ci * starting just before zone-capacity has some blocks spanning across the
838c2ecf20Sopenharmony_ci * zone-capacity, these blocks are not usable.
848c2ecf20Sopenharmony_ci * Such spanning segments can be in free list so calculate the sum of usable
858c2ecf20Sopenharmony_ci * blocks in currently free segments including normal and spanning segments.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistatic inline block_t free_segs_blk_count_zoned(struct f2fs_sb_info *sbi)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	block_t free_seg_blks = 0;
908c2ecf20Sopenharmony_ci	struct free_segmap_info *free_i = FREE_I(sbi);
918c2ecf20Sopenharmony_ci	int j;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	spin_lock(&free_i->segmap_lock);
948c2ecf20Sopenharmony_ci	for (j = 0; j < MAIN_SEGS(sbi); j++)
958c2ecf20Sopenharmony_ci		if (!test_bit(j, free_i->free_segmap))
968c2ecf20Sopenharmony_ci			free_seg_blks += f2fs_usable_blks_in_seg(sbi, j);
978c2ecf20Sopenharmony_ci	spin_unlock(&free_i->segmap_lock);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	return free_seg_blks;
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic inline block_t free_segs_blk_count(struct f2fs_sb_info *sbi)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	if (f2fs_sb_has_blkzoned(sbi))
1058c2ecf20Sopenharmony_ci		return free_segs_blk_count_zoned(sbi);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	return free_segments(sbi) << sbi->log_blocks_per_seg;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic inline block_t free_user_blocks(struct f2fs_sb_info *sbi)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	block_t free_blks, ovp_blks;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	free_blks = free_segs_blk_count(sbi);
1158c2ecf20Sopenharmony_ci	ovp_blks = overprovision_segments(sbi) << sbi->log_blocks_per_seg;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	if (free_blks < ovp_blks)
1188c2ecf20Sopenharmony_ci		return 0;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return free_blks - ovp_blks;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline block_t limit_invalid_user_blocks(struct f2fs_sb_info *sbi)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	return (long)(sbi->user_block_count * LIMIT_INVALID_BLOCK) / 100;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic inline block_t limit_free_user_blocks(struct f2fs_sb_info *sbi)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	block_t reclaimable_user_blocks = sbi->user_block_count -
1318c2ecf20Sopenharmony_ci		written_block_count(sbi);
1328c2ecf20Sopenharmony_ci	return (long)(reclaimable_user_blocks * LIMIT_FREE_BLOCK) / 100;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline void increase_sleep_time(struct f2fs_gc_kthread *gc_th,
1368c2ecf20Sopenharmony_ci							unsigned int *wait)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	unsigned int min_time = gc_th->min_sleep_time;
1398c2ecf20Sopenharmony_ci	unsigned int max_time = gc_th->max_sleep_time;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (*wait == gc_th->no_gc_sleep_time)
1428c2ecf20Sopenharmony_ci		return;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if ((long long)*wait + (long long)min_time > (long long)max_time)
1458c2ecf20Sopenharmony_ci		*wait = max_time;
1468c2ecf20Sopenharmony_ci	else
1478c2ecf20Sopenharmony_ci		*wait += min_time;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic inline void decrease_sleep_time(struct f2fs_gc_kthread *gc_th,
1518c2ecf20Sopenharmony_ci							unsigned int *wait)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	unsigned int min_time = gc_th->min_sleep_time;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (*wait == gc_th->no_gc_sleep_time)
1568c2ecf20Sopenharmony_ci		*wait = gc_th->max_sleep_time;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if ((long long)*wait - (long long)min_time < (long long)min_time)
1598c2ecf20Sopenharmony_ci		*wait = min_time;
1608c2ecf20Sopenharmony_ci	else
1618c2ecf20Sopenharmony_ci		*wait -= min_time;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline bool has_enough_invalid_blocks(struct f2fs_sb_info *sbi)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	block_t invalid_user_blocks = sbi->user_block_count -
1678c2ecf20Sopenharmony_ci					written_block_count(sbi);
1688c2ecf20Sopenharmony_ci	/*
1698c2ecf20Sopenharmony_ci	 * Background GC is triggered with the following conditions.
1708c2ecf20Sopenharmony_ci	 * 1. There are a number of invalid blocks.
1718c2ecf20Sopenharmony_ci	 * 2. There is not enough free space.
1728c2ecf20Sopenharmony_ci	 */
1738c2ecf20Sopenharmony_ci	if (invalid_user_blocks > limit_invalid_user_blocks(sbi) &&
1748c2ecf20Sopenharmony_ci			free_user_blocks(sbi) < limit_free_user_blocks(sbi))
1758c2ecf20Sopenharmony_ci		return true;
1768c2ecf20Sopenharmony_ci	return false;
1778c2ecf20Sopenharmony_ci}
178