Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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