162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * fs/f2fs/segment.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2012 Samsung Electronics Co., Ltd.
662306a36Sopenharmony_ci *             http://www.samsung.com/
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/blkdev.h>
962306a36Sopenharmony_ci#include <linux/backing-dev.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/* constant macro */
1262306a36Sopenharmony_ci#define NULL_SEGNO			((unsigned int)(~0))
1362306a36Sopenharmony_ci#define NULL_SECNO			((unsigned int)(~0))
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define DEF_RECLAIM_PREFREE_SEGMENTS	5	/* 5% over total segments */
1662306a36Sopenharmony_ci#define DEF_MAX_RECLAIM_PREFREE_SEGMENTS	4096	/* 8GB in maximum */
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
1962306a36Sopenharmony_ci#define F2FS_MIN_META_SEGMENTS	8 /* SB + 2 (CP + SIT + NAT) + SSA */
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/* L: Logical segment # in volume, R: Relative segment # in main area */
2262306a36Sopenharmony_ci#define GET_L2R_SEGNO(free_i, segno)	((segno) - (free_i)->start_segno)
2362306a36Sopenharmony_ci#define GET_R2L_SEGNO(free_i, segno)	((segno) + (free_i)->start_segno)
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#define IS_DATASEG(t)	((t) <= CURSEG_COLD_DATA)
2662306a36Sopenharmony_ci#define IS_NODESEG(t)	((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
2762306a36Sopenharmony_ci#define SE_PAGETYPE(se)	((IS_NODESEG((se)->type) ? NODE : DATA))
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
3062306a36Sopenharmony_ci						unsigned short seg_type)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	f2fs_bug_on(sbi, seg_type >= NR_PERSISTENT_LOG);
3362306a36Sopenharmony_ci}
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#define IS_HOT(t)	((t) == CURSEG_HOT_NODE || (t) == CURSEG_HOT_DATA)
3662306a36Sopenharmony_ci#define IS_WARM(t)	((t) == CURSEG_WARM_NODE || (t) == CURSEG_WARM_DATA)
3762306a36Sopenharmony_ci#define IS_COLD(t)	((t) == CURSEG_COLD_NODE || (t) == CURSEG_COLD_DATA)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define IS_CURSEG(sbi, seg)						\
4062306a36Sopenharmony_ci	(((seg) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||	\
4162306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) ||	\
4262306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) ||	\
4362306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||	\
4462306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) ||	\
4562306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno) ||	\
4662306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno) ||	\
4762306a36Sopenharmony_ci	 ((seg) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno))
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define IS_CURSEC(sbi, secno)						\
5062306a36Sopenharmony_ci	(((secno) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno /		\
5162306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
5262306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno /		\
5362306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
5462306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno /		\
5562306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
5662306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno /		\
5762306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
5862306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno /		\
5962306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
6062306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno /		\
6162306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
6262306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno /	\
6362306a36Sopenharmony_ci	  (sbi)->segs_per_sec) ||	\
6462306a36Sopenharmony_ci	 ((secno) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno /	\
6562306a36Sopenharmony_ci	  (sbi)->segs_per_sec))
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define MAIN_BLKADDR(sbi)						\
6862306a36Sopenharmony_ci	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr : 				\
6962306a36Sopenharmony_ci		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
7062306a36Sopenharmony_ci#define SEG0_BLKADDR(sbi)						\
7162306a36Sopenharmony_ci	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : 				\
7262306a36Sopenharmony_ci		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
7562306a36Sopenharmony_ci#define MAIN_SECS(sbi)	((sbi)->total_sections)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define TOTAL_SEGS(sbi)							\
7862306a36Sopenharmony_ci	(SM_I(sbi) ? SM_I(sbi)->segment_count : 				\
7962306a36Sopenharmony_ci		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
8062306a36Sopenharmony_ci#define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
8362306a36Sopenharmony_ci#define SEGMENT_SIZE(sbi)	(1ULL << ((sbi)->log_blocksize +	\
8462306a36Sopenharmony_ci					(sbi)->log_blocks_per_seg))
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#define START_BLOCK(sbi, segno)	(SEG0_BLKADDR(sbi) +			\
8762306a36Sopenharmony_ci	 (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define NEXT_FREE_BLKADDR(sbi, curseg)					\
9062306a36Sopenharmony_ci	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)	((blk_addr) - SEG0_BLKADDR(sbi))
9362306a36Sopenharmony_ci#define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
9462306a36Sopenharmony_ci	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
9562306a36Sopenharmony_ci#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
9662306a36Sopenharmony_ci	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & ((sbi)->blocks_per_seg - 1))
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci#define GET_SEGNO(sbi, blk_addr)					\
9962306a36Sopenharmony_ci	((!__is_valid_data_blkaddr(blk_addr)) ?			\
10062306a36Sopenharmony_ci	NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi),			\
10162306a36Sopenharmony_ci		GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
10262306a36Sopenharmony_ci#define BLKS_PER_SEC(sbi)					\
10362306a36Sopenharmony_ci	((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
10462306a36Sopenharmony_ci#define CAP_BLKS_PER_SEC(sbi)					\
10562306a36Sopenharmony_ci	((sbi)->segs_per_sec * (sbi)->blocks_per_seg -		\
10662306a36Sopenharmony_ci	 (sbi)->unusable_blocks_per_sec)
10762306a36Sopenharmony_ci#define CAP_SEGS_PER_SEC(sbi)					\
10862306a36Sopenharmony_ci	((sbi)->segs_per_sec - ((sbi)->unusable_blocks_per_sec >>\
10962306a36Sopenharmony_ci	(sbi)->log_blocks_per_seg))
11062306a36Sopenharmony_ci#define GET_SEC_FROM_SEG(sbi, segno)				\
11162306a36Sopenharmony_ci	(((segno) == -1) ? -1: (segno) / (sbi)->segs_per_sec)
11262306a36Sopenharmony_ci#define GET_SEG_FROM_SEC(sbi, secno)				\
11362306a36Sopenharmony_ci	((secno) * (sbi)->segs_per_sec)
11462306a36Sopenharmony_ci#define GET_ZONE_FROM_SEC(sbi, secno)				\
11562306a36Sopenharmony_ci	(((secno) == -1) ? -1: (secno) / (sbi)->secs_per_zone)
11662306a36Sopenharmony_ci#define GET_ZONE_FROM_SEG(sbi, segno)				\
11762306a36Sopenharmony_ci	GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define GET_SUM_BLOCK(sbi, segno)				\
12062306a36Sopenharmony_ci	((sbi)->sm_info->ssa_blkaddr + (segno))
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci#define GET_SUM_TYPE(footer) ((footer)->entry_type)
12362306a36Sopenharmony_ci#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define SIT_ENTRY_OFFSET(sit_i, segno)					\
12662306a36Sopenharmony_ci	((segno) % (sit_i)->sents_per_block)
12762306a36Sopenharmony_ci#define SIT_BLOCK_OFFSET(segno)					\
12862306a36Sopenharmony_ci	((segno) / SIT_ENTRY_PER_BLOCK)
12962306a36Sopenharmony_ci#define	START_SEGNO(segno)		\
13062306a36Sopenharmony_ci	(SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
13162306a36Sopenharmony_ci#define SIT_BLK_CNT(sbi)			\
13262306a36Sopenharmony_ci	DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
13362306a36Sopenharmony_ci#define f2fs_bitmap_size(nr)			\
13462306a36Sopenharmony_ci	(BITS_TO_LONGS(nr) * sizeof(unsigned long))
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#define SECTOR_FROM_BLOCK(blk_addr)					\
13762306a36Sopenharmony_ci	(((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
13862306a36Sopenharmony_ci#define SECTOR_TO_BLOCK(sectors)					\
13962306a36Sopenharmony_ci	((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci/*
14262306a36Sopenharmony_ci * indicate a block allocation direction: RIGHT and LEFT.
14362306a36Sopenharmony_ci * RIGHT means allocating new sections towards the end of volume.
14462306a36Sopenharmony_ci * LEFT means the opposite direction.
14562306a36Sopenharmony_ci */
14662306a36Sopenharmony_cienum {
14762306a36Sopenharmony_ci	ALLOC_RIGHT = 0,
14862306a36Sopenharmony_ci	ALLOC_LEFT
14962306a36Sopenharmony_ci};
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci/*
15262306a36Sopenharmony_ci * In the victim_sel_policy->alloc_mode, there are three block allocation modes.
15362306a36Sopenharmony_ci * LFS writes data sequentially with cleaning operations.
15462306a36Sopenharmony_ci * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
15562306a36Sopenharmony_ci * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
15662306a36Sopenharmony_ci * fragmented segment which has similar aging degree.
15762306a36Sopenharmony_ci */
15862306a36Sopenharmony_cienum {
15962306a36Sopenharmony_ci	LFS = 0,
16062306a36Sopenharmony_ci	SSR,
16162306a36Sopenharmony_ci	AT_SSR,
16262306a36Sopenharmony_ci};
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/*
16562306a36Sopenharmony_ci * In the victim_sel_policy->gc_mode, there are three gc, aka cleaning, modes.
16662306a36Sopenharmony_ci * GC_CB is based on cost-benefit algorithm.
16762306a36Sopenharmony_ci * GC_GREEDY is based on greedy algorithm.
16862306a36Sopenharmony_ci * GC_AT is based on age-threshold algorithm.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_cienum {
17162306a36Sopenharmony_ci	GC_CB = 0,
17262306a36Sopenharmony_ci	GC_GREEDY,
17362306a36Sopenharmony_ci	GC_AT,
17462306a36Sopenharmony_ci	ALLOC_NEXT,
17562306a36Sopenharmony_ci	FLUSH_DEVICE,
17662306a36Sopenharmony_ci	MAX_GC_POLICY,
17762306a36Sopenharmony_ci};
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/*
18062306a36Sopenharmony_ci * BG_GC means the background cleaning job.
18162306a36Sopenharmony_ci * FG_GC means the on-demand cleaning job.
18262306a36Sopenharmony_ci */
18362306a36Sopenharmony_cienum {
18462306a36Sopenharmony_ci	BG_GC = 0,
18562306a36Sopenharmony_ci	FG_GC,
18662306a36Sopenharmony_ci};
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci/* for a function parameter to select a victim segment */
18962306a36Sopenharmony_cistruct victim_sel_policy {
19062306a36Sopenharmony_ci	int alloc_mode;			/* LFS or SSR */
19162306a36Sopenharmony_ci	int gc_mode;			/* GC_CB or GC_GREEDY */
19262306a36Sopenharmony_ci	unsigned long *dirty_bitmap;	/* dirty segment/section bitmap */
19362306a36Sopenharmony_ci	unsigned int max_search;	/*
19462306a36Sopenharmony_ci					 * maximum # of segments/sections
19562306a36Sopenharmony_ci					 * to search
19662306a36Sopenharmony_ci					 */
19762306a36Sopenharmony_ci	unsigned int offset;		/* last scanned bitmap offset */
19862306a36Sopenharmony_ci	unsigned int ofs_unit;		/* bitmap search unit */
19962306a36Sopenharmony_ci	unsigned int min_cost;		/* minimum cost */
20062306a36Sopenharmony_ci	unsigned long long oldest_age;	/* oldest age of segments having the same min cost */
20162306a36Sopenharmony_ci	unsigned int min_segno;		/* segment # having min. cost */
20262306a36Sopenharmony_ci	unsigned long long age;		/* mtime of GCed section*/
20362306a36Sopenharmony_ci	unsigned long long age_threshold;/* age threshold */
20462306a36Sopenharmony_ci};
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistruct seg_entry {
20762306a36Sopenharmony_ci	unsigned int type:6;		/* segment type like CURSEG_XXX_TYPE */
20862306a36Sopenharmony_ci	unsigned int valid_blocks:10;	/* # of valid blocks */
20962306a36Sopenharmony_ci	unsigned int ckpt_valid_blocks:10;	/* # of valid blocks last cp */
21062306a36Sopenharmony_ci	unsigned int padding:6;		/* padding */
21162306a36Sopenharmony_ci	unsigned char *cur_valid_map;	/* validity bitmap of blocks */
21262306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
21362306a36Sopenharmony_ci	unsigned char *cur_valid_map_mir;	/* mirror of current valid bitmap */
21462306a36Sopenharmony_ci#endif
21562306a36Sopenharmony_ci	/*
21662306a36Sopenharmony_ci	 * # of valid blocks and the validity bitmap stored in the last
21762306a36Sopenharmony_ci	 * checkpoint pack. This information is used by the SSR mode.
21862306a36Sopenharmony_ci	 */
21962306a36Sopenharmony_ci	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp */
22062306a36Sopenharmony_ci	unsigned char *discard_map;
22162306a36Sopenharmony_ci	unsigned long long mtime;	/* modification time of the segment */
22262306a36Sopenharmony_ci};
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_cistruct sec_entry {
22562306a36Sopenharmony_ci	unsigned int valid_blocks;	/* # of valid blocks in a section */
22662306a36Sopenharmony_ci};
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#define MAX_SKIP_GC_COUNT			16
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistruct revoke_entry {
23162306a36Sopenharmony_ci	struct list_head list;
23262306a36Sopenharmony_ci	block_t old_addr;		/* for revoking when fail to commit */
23362306a36Sopenharmony_ci	pgoff_t index;
23462306a36Sopenharmony_ci};
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_cistruct sit_info {
23762306a36Sopenharmony_ci	block_t sit_base_addr;		/* start block address of SIT area */
23862306a36Sopenharmony_ci	block_t sit_blocks;		/* # of blocks used by SIT area */
23962306a36Sopenharmony_ci	block_t written_valid_blocks;	/* # of valid blocks in main area */
24062306a36Sopenharmony_ci	char *bitmap;			/* all bitmaps pointer */
24162306a36Sopenharmony_ci	char *sit_bitmap;		/* SIT bitmap pointer */
24262306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
24362306a36Sopenharmony_ci	char *sit_bitmap_mir;		/* SIT bitmap mirror */
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	/* bitmap of segments to be ignored by GC in case of errors */
24662306a36Sopenharmony_ci	unsigned long *invalid_segmap;
24762306a36Sopenharmony_ci#endif
24862306a36Sopenharmony_ci	unsigned int bitmap_size;	/* SIT bitmap size */
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	unsigned long *tmp_map;			/* bitmap for temporal use */
25162306a36Sopenharmony_ci	unsigned long *dirty_sentries_bitmap;	/* bitmap for dirty sentries */
25262306a36Sopenharmony_ci	unsigned int dirty_sentries;		/* # of dirty sentries */
25362306a36Sopenharmony_ci	unsigned int sents_per_block;		/* # of SIT entries per block */
25462306a36Sopenharmony_ci	struct rw_semaphore sentry_lock;	/* to protect SIT cache */
25562306a36Sopenharmony_ci	struct seg_entry *sentries;		/* SIT segment-level cache */
25662306a36Sopenharmony_ci	struct sec_entry *sec_entries;		/* SIT section-level cache */
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	/* for cost-benefit algorithm in cleaning procedure */
25962306a36Sopenharmony_ci	unsigned long long elapsed_time;	/* elapsed time after mount */
26062306a36Sopenharmony_ci	unsigned long long mounted_time;	/* mount time */
26162306a36Sopenharmony_ci	unsigned long long min_mtime;		/* min. modification time */
26262306a36Sopenharmony_ci	unsigned long long max_mtime;		/* max. modification time */
26362306a36Sopenharmony_ci	unsigned long long dirty_min_mtime;	/* rerange candidates in GC_AT */
26462306a36Sopenharmony_ci	unsigned long long dirty_max_mtime;	/* rerange candidates in GC_AT */
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
26762306a36Sopenharmony_ci};
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_cistruct free_segmap_info {
27062306a36Sopenharmony_ci	unsigned int start_segno;	/* start segment number logically */
27162306a36Sopenharmony_ci	unsigned int free_segments;	/* # of free segments */
27262306a36Sopenharmony_ci	unsigned int free_sections;	/* # of free sections */
27362306a36Sopenharmony_ci	spinlock_t segmap_lock;		/* free segmap lock */
27462306a36Sopenharmony_ci	unsigned long *free_segmap;	/* free segment bitmap */
27562306a36Sopenharmony_ci	unsigned long *free_secmap;	/* free section bitmap */
27662306a36Sopenharmony_ci};
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
27962306a36Sopenharmony_cienum dirty_type {
28062306a36Sopenharmony_ci	DIRTY_HOT_DATA,		/* dirty segments assigned as hot data logs */
28162306a36Sopenharmony_ci	DIRTY_WARM_DATA,	/* dirty segments assigned as warm data logs */
28262306a36Sopenharmony_ci	DIRTY_COLD_DATA,	/* dirty segments assigned as cold data logs */
28362306a36Sopenharmony_ci	DIRTY_HOT_NODE,		/* dirty segments assigned as hot node logs */
28462306a36Sopenharmony_ci	DIRTY_WARM_NODE,	/* dirty segments assigned as warm node logs */
28562306a36Sopenharmony_ci	DIRTY_COLD_NODE,	/* dirty segments assigned as cold node logs */
28662306a36Sopenharmony_ci	DIRTY,			/* to count # of dirty segments */
28762306a36Sopenharmony_ci	PRE,			/* to count # of entirely obsolete segments */
28862306a36Sopenharmony_ci	NR_DIRTY_TYPE
28962306a36Sopenharmony_ci};
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistruct dirty_seglist_info {
29262306a36Sopenharmony_ci	unsigned long *dirty_segmap[NR_DIRTY_TYPE];
29362306a36Sopenharmony_ci	unsigned long *dirty_secmap;
29462306a36Sopenharmony_ci	struct mutex seglist_lock;		/* lock for segment bitmaps */
29562306a36Sopenharmony_ci	int nr_dirty[NR_DIRTY_TYPE];		/* # of dirty segments */
29662306a36Sopenharmony_ci	unsigned long *victim_secmap;		/* background GC victims */
29762306a36Sopenharmony_ci	unsigned long *pinned_secmap;		/* pinned victims from foreground GC */
29862306a36Sopenharmony_ci	unsigned int pinned_secmap_cnt;		/* count of victims which has pinned data */
29962306a36Sopenharmony_ci	bool enable_pin_section;		/* enable pinning section */
30062306a36Sopenharmony_ci};
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci/* for active log information */
30362306a36Sopenharmony_cistruct curseg_info {
30462306a36Sopenharmony_ci	struct mutex curseg_mutex;		/* lock for consistency */
30562306a36Sopenharmony_ci	struct f2fs_summary_block *sum_blk;	/* cached summary block */
30662306a36Sopenharmony_ci	struct rw_semaphore journal_rwsem;	/* protect journal area */
30762306a36Sopenharmony_ci	struct f2fs_journal *journal;		/* cached journal info */
30862306a36Sopenharmony_ci	unsigned char alloc_type;		/* current allocation type */
30962306a36Sopenharmony_ci	unsigned short seg_type;		/* segment type like CURSEG_XXX_TYPE */
31062306a36Sopenharmony_ci	unsigned int segno;			/* current segment number */
31162306a36Sopenharmony_ci	unsigned short next_blkoff;		/* next block offset to write */
31262306a36Sopenharmony_ci	unsigned int zone;			/* current zone number */
31362306a36Sopenharmony_ci	unsigned int next_segno;		/* preallocated segment */
31462306a36Sopenharmony_ci	int fragment_remained_chunk;		/* remained block size in a chunk for block fragmentation mode */
31562306a36Sopenharmony_ci	bool inited;				/* indicate inmem log is inited */
31662306a36Sopenharmony_ci};
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_cistruct sit_entry_set {
31962306a36Sopenharmony_ci	struct list_head set_list;	/* link with all sit sets */
32062306a36Sopenharmony_ci	unsigned int start_segno;	/* start segno of sits in set */
32162306a36Sopenharmony_ci	unsigned int entry_cnt;		/* the # of sit entries in set */
32262306a36Sopenharmony_ci};
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci/*
32562306a36Sopenharmony_ci * inline functions
32662306a36Sopenharmony_ci */
32762306a36Sopenharmony_cistatic inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
32862306a36Sopenharmony_ci{
32962306a36Sopenharmony_ci	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
33062306a36Sopenharmony_ci}
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_cistatic inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
33362306a36Sopenharmony_ci						unsigned int segno)
33462306a36Sopenharmony_ci{
33562306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
33662306a36Sopenharmony_ci	return &sit_i->sentries[segno];
33762306a36Sopenharmony_ci}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cistatic inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
34062306a36Sopenharmony_ci						unsigned int segno)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
34362306a36Sopenharmony_ci	return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
34462306a36Sopenharmony_ci}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_cistatic inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
34762306a36Sopenharmony_ci				unsigned int segno, bool use_section)
34862306a36Sopenharmony_ci{
34962306a36Sopenharmony_ci	/*
35062306a36Sopenharmony_ci	 * In order to get # of valid blocks in a section instantly from many
35162306a36Sopenharmony_ci	 * segments, f2fs manages two counting structures separately.
35262306a36Sopenharmony_ci	 */
35362306a36Sopenharmony_ci	if (use_section && __is_large_section(sbi))
35462306a36Sopenharmony_ci		return get_sec_entry(sbi, segno)->valid_blocks;
35562306a36Sopenharmony_ci	else
35662306a36Sopenharmony_ci		return get_seg_entry(sbi, segno)->valid_blocks;
35762306a36Sopenharmony_ci}
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_cistatic inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
36062306a36Sopenharmony_ci				unsigned int segno, bool use_section)
36162306a36Sopenharmony_ci{
36262306a36Sopenharmony_ci	if (use_section && __is_large_section(sbi)) {
36362306a36Sopenharmony_ci		unsigned int start_segno = START_SEGNO(segno);
36462306a36Sopenharmony_ci		unsigned int blocks = 0;
36562306a36Sopenharmony_ci		int i;
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci		for (i = 0; i < sbi->segs_per_sec; i++, start_segno++) {
36862306a36Sopenharmony_ci			struct seg_entry *se = get_seg_entry(sbi, start_segno);
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci			blocks += se->ckpt_valid_blocks;
37162306a36Sopenharmony_ci		}
37262306a36Sopenharmony_ci		return blocks;
37362306a36Sopenharmony_ci	}
37462306a36Sopenharmony_ci	return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
37562306a36Sopenharmony_ci}
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_cistatic inline void seg_info_from_raw_sit(struct seg_entry *se,
37862306a36Sopenharmony_ci					struct f2fs_sit_entry *rs)
37962306a36Sopenharmony_ci{
38062306a36Sopenharmony_ci	se->valid_blocks = GET_SIT_VBLOCKS(rs);
38162306a36Sopenharmony_ci	se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
38262306a36Sopenharmony_ci	memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
38362306a36Sopenharmony_ci	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
38462306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
38562306a36Sopenharmony_ci	memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
38662306a36Sopenharmony_ci#endif
38762306a36Sopenharmony_ci	se->type = GET_SIT_TYPE(rs);
38862306a36Sopenharmony_ci	se->mtime = le64_to_cpu(rs->mtime);
38962306a36Sopenharmony_ci}
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_cistatic inline void __seg_info_to_raw_sit(struct seg_entry *se,
39262306a36Sopenharmony_ci					struct f2fs_sit_entry *rs)
39362306a36Sopenharmony_ci{
39462306a36Sopenharmony_ci	unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
39562306a36Sopenharmony_ci					se->valid_blocks;
39662306a36Sopenharmony_ci	rs->vblocks = cpu_to_le16(raw_vblocks);
39762306a36Sopenharmony_ci	memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
39862306a36Sopenharmony_ci	rs->mtime = cpu_to_le64(se->mtime);
39962306a36Sopenharmony_ci}
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_cistatic inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
40262306a36Sopenharmony_ci				struct page *page, unsigned int start)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	struct f2fs_sit_block *raw_sit;
40562306a36Sopenharmony_ci	struct seg_entry *se;
40662306a36Sopenharmony_ci	struct f2fs_sit_entry *rs;
40762306a36Sopenharmony_ci	unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
40862306a36Sopenharmony_ci					(unsigned long)MAIN_SEGS(sbi));
40962306a36Sopenharmony_ci	int i;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	raw_sit = (struct f2fs_sit_block *)page_address(page);
41262306a36Sopenharmony_ci	memset(raw_sit, 0, PAGE_SIZE);
41362306a36Sopenharmony_ci	for (i = 0; i < end - start; i++) {
41462306a36Sopenharmony_ci		rs = &raw_sit->entries[i];
41562306a36Sopenharmony_ci		se = get_seg_entry(sbi, start + i);
41662306a36Sopenharmony_ci		__seg_info_to_raw_sit(se, rs);
41762306a36Sopenharmony_ci	}
41862306a36Sopenharmony_ci}
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_cistatic inline void seg_info_to_raw_sit(struct seg_entry *se,
42162306a36Sopenharmony_ci					struct f2fs_sit_entry *rs)
42262306a36Sopenharmony_ci{
42362306a36Sopenharmony_ci	__seg_info_to_raw_sit(se, rs);
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
42662306a36Sopenharmony_ci	se->ckpt_valid_blocks = se->valid_blocks;
42762306a36Sopenharmony_ci}
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_cistatic inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
43062306a36Sopenharmony_ci		unsigned int max, unsigned int segno)
43162306a36Sopenharmony_ci{
43262306a36Sopenharmony_ci	unsigned int ret;
43362306a36Sopenharmony_ci	spin_lock(&free_i->segmap_lock);
43462306a36Sopenharmony_ci	ret = find_next_bit(free_i->free_segmap, max, segno);
43562306a36Sopenharmony_ci	spin_unlock(&free_i->segmap_lock);
43662306a36Sopenharmony_ci	return ret;
43762306a36Sopenharmony_ci}
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_cistatic inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
44062306a36Sopenharmony_ci{
44162306a36Sopenharmony_ci	struct free_segmap_info *free_i = FREE_I(sbi);
44262306a36Sopenharmony_ci	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
44362306a36Sopenharmony_ci	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
44462306a36Sopenharmony_ci	unsigned int next;
44562306a36Sopenharmony_ci	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	spin_lock(&free_i->segmap_lock);
44862306a36Sopenharmony_ci	clear_bit(segno, free_i->free_segmap);
44962306a36Sopenharmony_ci	free_i->free_segments++;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	next = find_next_bit(free_i->free_segmap,
45262306a36Sopenharmony_ci			start_segno + sbi->segs_per_sec, start_segno);
45362306a36Sopenharmony_ci	if (next >= start_segno + usable_segs) {
45462306a36Sopenharmony_ci		clear_bit(secno, free_i->free_secmap);
45562306a36Sopenharmony_ci		free_i->free_sections++;
45662306a36Sopenharmony_ci	}
45762306a36Sopenharmony_ci	spin_unlock(&free_i->segmap_lock);
45862306a36Sopenharmony_ci}
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_cistatic inline void __set_inuse(struct f2fs_sb_info *sbi,
46162306a36Sopenharmony_ci		unsigned int segno)
46262306a36Sopenharmony_ci{
46362306a36Sopenharmony_ci	struct free_segmap_info *free_i = FREE_I(sbi);
46462306a36Sopenharmony_ci	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	set_bit(segno, free_i->free_segmap);
46762306a36Sopenharmony_ci	free_i->free_segments--;
46862306a36Sopenharmony_ci	if (!test_and_set_bit(secno, free_i->free_secmap))
46962306a36Sopenharmony_ci		free_i->free_sections--;
47062306a36Sopenharmony_ci}
47162306a36Sopenharmony_ci
47262306a36Sopenharmony_cistatic inline void __set_test_and_free(struct f2fs_sb_info *sbi,
47362306a36Sopenharmony_ci		unsigned int segno, bool inmem)
47462306a36Sopenharmony_ci{
47562306a36Sopenharmony_ci	struct free_segmap_info *free_i = FREE_I(sbi);
47662306a36Sopenharmony_ci	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
47762306a36Sopenharmony_ci	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
47862306a36Sopenharmony_ci	unsigned int next;
47962306a36Sopenharmony_ci	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci	spin_lock(&free_i->segmap_lock);
48262306a36Sopenharmony_ci	if (test_and_clear_bit(segno, free_i->free_segmap)) {
48362306a36Sopenharmony_ci		free_i->free_segments++;
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci		if (!inmem && IS_CURSEC(sbi, secno))
48662306a36Sopenharmony_ci			goto skip_free;
48762306a36Sopenharmony_ci		next = find_next_bit(free_i->free_segmap,
48862306a36Sopenharmony_ci				start_segno + sbi->segs_per_sec, start_segno);
48962306a36Sopenharmony_ci		if (next >= start_segno + usable_segs) {
49062306a36Sopenharmony_ci			if (test_and_clear_bit(secno, free_i->free_secmap))
49162306a36Sopenharmony_ci				free_i->free_sections++;
49262306a36Sopenharmony_ci		}
49362306a36Sopenharmony_ci	}
49462306a36Sopenharmony_ciskip_free:
49562306a36Sopenharmony_ci	spin_unlock(&free_i->segmap_lock);
49662306a36Sopenharmony_ci}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_cistatic inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
49962306a36Sopenharmony_ci		unsigned int segno)
50062306a36Sopenharmony_ci{
50162306a36Sopenharmony_ci	struct free_segmap_info *free_i = FREE_I(sbi);
50262306a36Sopenharmony_ci	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	spin_lock(&free_i->segmap_lock);
50562306a36Sopenharmony_ci	if (!test_and_set_bit(segno, free_i->free_segmap)) {
50662306a36Sopenharmony_ci		free_i->free_segments--;
50762306a36Sopenharmony_ci		if (!test_and_set_bit(secno, free_i->free_secmap))
50862306a36Sopenharmony_ci			free_i->free_sections--;
50962306a36Sopenharmony_ci	}
51062306a36Sopenharmony_ci	spin_unlock(&free_i->segmap_lock);
51162306a36Sopenharmony_ci}
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_cistatic inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
51462306a36Sopenharmony_ci		void *dst_addr)
51562306a36Sopenharmony_ci{
51662306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
51962306a36Sopenharmony_ci	if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
52062306a36Sopenharmony_ci						sit_i->bitmap_size))
52162306a36Sopenharmony_ci		f2fs_bug_on(sbi, 1);
52262306a36Sopenharmony_ci#endif
52362306a36Sopenharmony_ci	memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
52462306a36Sopenharmony_ci}
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_cistatic inline block_t written_block_count(struct f2fs_sb_info *sbi)
52762306a36Sopenharmony_ci{
52862306a36Sopenharmony_ci	return SIT_I(sbi)->written_valid_blocks;
52962306a36Sopenharmony_ci}
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_cistatic inline unsigned int free_segments(struct f2fs_sb_info *sbi)
53262306a36Sopenharmony_ci{
53362306a36Sopenharmony_ci	return FREE_I(sbi)->free_segments;
53462306a36Sopenharmony_ci}
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_cistatic inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
53762306a36Sopenharmony_ci{
53862306a36Sopenharmony_ci	return SM_I(sbi)->reserved_segments +
53962306a36Sopenharmony_ci			SM_I(sbi)->additional_reserved_segments;
54062306a36Sopenharmony_ci}
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_cistatic inline unsigned int free_sections(struct f2fs_sb_info *sbi)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	return FREE_I(sbi)->free_sections;
54562306a36Sopenharmony_ci}
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_cistatic inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
54862306a36Sopenharmony_ci{
54962306a36Sopenharmony_ci	return DIRTY_I(sbi)->nr_dirty[PRE];
55062306a36Sopenharmony_ci}
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_cistatic inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
55362306a36Sopenharmony_ci{
55462306a36Sopenharmony_ci	return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
55562306a36Sopenharmony_ci		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
55662306a36Sopenharmony_ci		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
55762306a36Sopenharmony_ci		DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
55862306a36Sopenharmony_ci		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
55962306a36Sopenharmony_ci		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
56062306a36Sopenharmony_ci}
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_cistatic inline int overprovision_segments(struct f2fs_sb_info *sbi)
56362306a36Sopenharmony_ci{
56462306a36Sopenharmony_ci	return SM_I(sbi)->ovp_segments;
56562306a36Sopenharmony_ci}
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_cistatic inline int reserved_sections(struct f2fs_sb_info *sbi)
56862306a36Sopenharmony_ci{
56962306a36Sopenharmony_ci	return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
57062306a36Sopenharmony_ci}
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_cistatic inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
57362306a36Sopenharmony_ci			unsigned int node_blocks, unsigned int dent_blocks)
57462306a36Sopenharmony_ci{
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	unsigned segno, left_blocks;
57762306a36Sopenharmony_ci	int i;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci	/* check current node sections in the worst case. */
58062306a36Sopenharmony_ci	for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
58162306a36Sopenharmony_ci		segno = CURSEG_I(sbi, i)->segno;
58262306a36Sopenharmony_ci		left_blocks = CAP_BLKS_PER_SEC(sbi) -
58362306a36Sopenharmony_ci				get_ckpt_valid_blocks(sbi, segno, true);
58462306a36Sopenharmony_ci		if (node_blocks > left_blocks)
58562306a36Sopenharmony_ci			return false;
58662306a36Sopenharmony_ci	}
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	/* check current data section for dentry blocks. */
58962306a36Sopenharmony_ci	segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
59062306a36Sopenharmony_ci	left_blocks = CAP_BLKS_PER_SEC(sbi) -
59162306a36Sopenharmony_ci			get_ckpt_valid_blocks(sbi, segno, true);
59262306a36Sopenharmony_ci	if (dent_blocks > left_blocks)
59362306a36Sopenharmony_ci		return false;
59462306a36Sopenharmony_ci	return true;
59562306a36Sopenharmony_ci}
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci/*
59862306a36Sopenharmony_ci * calculate needed sections for dirty node/dentry
59962306a36Sopenharmony_ci * and call has_curseg_enough_space
60062306a36Sopenharmony_ci */
60162306a36Sopenharmony_cistatic inline void __get_secs_required(struct f2fs_sb_info *sbi,
60262306a36Sopenharmony_ci		unsigned int *lower_p, unsigned int *upper_p, bool *curseg_p)
60362306a36Sopenharmony_ci{
60462306a36Sopenharmony_ci	unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
60562306a36Sopenharmony_ci					get_pages(sbi, F2FS_DIRTY_DENTS) +
60662306a36Sopenharmony_ci					get_pages(sbi, F2FS_DIRTY_IMETA);
60762306a36Sopenharmony_ci	unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
60862306a36Sopenharmony_ci	unsigned int node_secs = total_node_blocks / CAP_BLKS_PER_SEC(sbi);
60962306a36Sopenharmony_ci	unsigned int dent_secs = total_dent_blocks / CAP_BLKS_PER_SEC(sbi);
61062306a36Sopenharmony_ci	unsigned int node_blocks = total_node_blocks % CAP_BLKS_PER_SEC(sbi);
61162306a36Sopenharmony_ci	unsigned int dent_blocks = total_dent_blocks % CAP_BLKS_PER_SEC(sbi);
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	if (lower_p)
61462306a36Sopenharmony_ci		*lower_p = node_secs + dent_secs;
61562306a36Sopenharmony_ci	if (upper_p)
61662306a36Sopenharmony_ci		*upper_p = node_secs + dent_secs +
61762306a36Sopenharmony_ci			(node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0);
61862306a36Sopenharmony_ci	if (curseg_p)
61962306a36Sopenharmony_ci		*curseg_p = has_curseg_enough_space(sbi,
62062306a36Sopenharmony_ci				node_blocks, dent_blocks);
62162306a36Sopenharmony_ci}
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_cistatic inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
62462306a36Sopenharmony_ci					int freed, int needed)
62562306a36Sopenharmony_ci{
62662306a36Sopenharmony_ci	unsigned int free_secs, lower_secs, upper_secs;
62762306a36Sopenharmony_ci	bool curseg_space;
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_ci	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
63062306a36Sopenharmony_ci		return false;
63162306a36Sopenharmony_ci
63262306a36Sopenharmony_ci	__get_secs_required(sbi, &lower_secs, &upper_secs, &curseg_space);
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci	free_secs = free_sections(sbi) + freed;
63562306a36Sopenharmony_ci	lower_secs += needed + reserved_sections(sbi);
63662306a36Sopenharmony_ci	upper_secs += needed + reserved_sections(sbi);
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	if (free_secs > upper_secs)
63962306a36Sopenharmony_ci		return false;
64062306a36Sopenharmony_ci	if (free_secs <= lower_secs)
64162306a36Sopenharmony_ci		return true;
64262306a36Sopenharmony_ci	return !curseg_space;
64362306a36Sopenharmony_ci}
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_cistatic inline bool has_enough_free_secs(struct f2fs_sb_info *sbi,
64662306a36Sopenharmony_ci					int freed, int needed)
64762306a36Sopenharmony_ci{
64862306a36Sopenharmony_ci	return !has_not_enough_free_secs(sbi, freed, needed);
64962306a36Sopenharmony_ci}
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_cistatic inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
65262306a36Sopenharmony_ci{
65362306a36Sopenharmony_ci	if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
65462306a36Sopenharmony_ci		return true;
65562306a36Sopenharmony_ci	if (likely(has_enough_free_secs(sbi, 0, 0)))
65662306a36Sopenharmony_ci		return true;
65762306a36Sopenharmony_ci	return false;
65862306a36Sopenharmony_ci}
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_cistatic inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
66162306a36Sopenharmony_ci{
66262306a36Sopenharmony_ci	return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
66362306a36Sopenharmony_ci}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_cistatic inline int utilization(struct f2fs_sb_info *sbi)
66662306a36Sopenharmony_ci{
66762306a36Sopenharmony_ci	return div_u64((u64)valid_user_blocks(sbi) * 100,
66862306a36Sopenharmony_ci					sbi->user_block_count);
66962306a36Sopenharmony_ci}
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci/*
67262306a36Sopenharmony_ci * Sometimes f2fs may be better to drop out-of-place update policy.
67362306a36Sopenharmony_ci * And, users can control the policy through sysfs entries.
67462306a36Sopenharmony_ci * There are five policies with triggering conditions as follows.
67562306a36Sopenharmony_ci * F2FS_IPU_FORCE - all the time,
67662306a36Sopenharmony_ci * F2FS_IPU_SSR - if SSR mode is activated,
67762306a36Sopenharmony_ci * F2FS_IPU_UTIL - if FS utilization is over threashold,
67862306a36Sopenharmony_ci * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
67962306a36Sopenharmony_ci *                     threashold,
68062306a36Sopenharmony_ci * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
68162306a36Sopenharmony_ci *                     storages. IPU will be triggered only if the # of dirty
68262306a36Sopenharmony_ci *                     pages over min_fsync_blocks. (=default option)
68362306a36Sopenharmony_ci * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
68462306a36Sopenharmony_ci * F2FS_IPU_NOCACHE - disable IPU bio cache.
68562306a36Sopenharmony_ci * F2FS_IPU_HONOR_OPU_WRITE - use OPU write prior to IPU write if inode has
68662306a36Sopenharmony_ci *                            FI_OPU_WRITE flag.
68762306a36Sopenharmony_ci * F2FS_IPU_DISABLE - disable IPU. (=default option in LFS mode)
68862306a36Sopenharmony_ci */
68962306a36Sopenharmony_ci#define DEF_MIN_IPU_UTIL	70
69062306a36Sopenharmony_ci#define DEF_MIN_FSYNC_BLOCKS	8
69162306a36Sopenharmony_ci#define DEF_MIN_HOT_BLOCKS	16
69262306a36Sopenharmony_ci
69362306a36Sopenharmony_ci#define SMALL_VOLUME_SEGMENTS	(16 * 512)	/* 16GB */
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci#define F2FS_IPU_DISABLE	0
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci/* Modification on enum should be synchronized with ipu_mode_names array */
69862306a36Sopenharmony_cienum {
69962306a36Sopenharmony_ci	F2FS_IPU_FORCE,
70062306a36Sopenharmony_ci	F2FS_IPU_SSR,
70162306a36Sopenharmony_ci	F2FS_IPU_UTIL,
70262306a36Sopenharmony_ci	F2FS_IPU_SSR_UTIL,
70362306a36Sopenharmony_ci	F2FS_IPU_FSYNC,
70462306a36Sopenharmony_ci	F2FS_IPU_ASYNC,
70562306a36Sopenharmony_ci	F2FS_IPU_NOCACHE,
70662306a36Sopenharmony_ci	F2FS_IPU_HONOR_OPU_WRITE,
70762306a36Sopenharmony_ci	F2FS_IPU_MAX,
70862306a36Sopenharmony_ci};
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_cistatic inline bool IS_F2FS_IPU_DISABLE(struct f2fs_sb_info *sbi)
71162306a36Sopenharmony_ci{
71262306a36Sopenharmony_ci	return SM_I(sbi)->ipu_policy == F2FS_IPU_DISABLE;
71362306a36Sopenharmony_ci}
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_ci#define F2FS_IPU_POLICY(name)					\
71662306a36Sopenharmony_cistatic inline bool IS_##name(struct f2fs_sb_info *sbi)		\
71762306a36Sopenharmony_ci{								\
71862306a36Sopenharmony_ci	return SM_I(sbi)->ipu_policy & BIT(name);		\
71962306a36Sopenharmony_ci}
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_FORCE);
72262306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_SSR);
72362306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_UTIL);
72462306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_SSR_UTIL);
72562306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_FSYNC);
72662306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_ASYNC);
72762306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_NOCACHE);
72862306a36Sopenharmony_ciF2FS_IPU_POLICY(F2FS_IPU_HONOR_OPU_WRITE);
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_cistatic inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
73162306a36Sopenharmony_ci		int type)
73262306a36Sopenharmony_ci{
73362306a36Sopenharmony_ci	struct curseg_info *curseg = CURSEG_I(sbi, type);
73462306a36Sopenharmony_ci	return curseg->segno;
73562306a36Sopenharmony_ci}
73662306a36Sopenharmony_ci
73762306a36Sopenharmony_cistatic inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
73862306a36Sopenharmony_ci		int type)
73962306a36Sopenharmony_ci{
74062306a36Sopenharmony_ci	struct curseg_info *curseg = CURSEG_I(sbi, type);
74162306a36Sopenharmony_ci	return curseg->alloc_type;
74262306a36Sopenharmony_ci}
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_cistatic inline bool valid_main_segno(struct f2fs_sb_info *sbi,
74562306a36Sopenharmony_ci		unsigned int segno)
74662306a36Sopenharmony_ci{
74762306a36Sopenharmony_ci	return segno <= (MAIN_SEGS(sbi) - 1);
74862306a36Sopenharmony_ci}
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_cistatic inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
75162306a36Sopenharmony_ci{
75262306a36Sopenharmony_ci	struct f2fs_sb_info *sbi = fio->sbi;
75362306a36Sopenharmony_ci
75462306a36Sopenharmony_ci	if (__is_valid_data_blkaddr(fio->old_blkaddr))
75562306a36Sopenharmony_ci		verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
75662306a36Sopenharmony_ci					META_GENERIC : DATA_GENERIC);
75762306a36Sopenharmony_ci	verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
75862306a36Sopenharmony_ci					META_GENERIC : DATA_GENERIC_ENHANCE);
75962306a36Sopenharmony_ci}
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci/*
76262306a36Sopenharmony_ci * Summary block is always treated as an invalid block
76362306a36Sopenharmony_ci */
76462306a36Sopenharmony_cistatic inline int check_block_count(struct f2fs_sb_info *sbi,
76562306a36Sopenharmony_ci		int segno, struct f2fs_sit_entry *raw_sit)
76662306a36Sopenharmony_ci{
76762306a36Sopenharmony_ci	bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
76862306a36Sopenharmony_ci	int valid_blocks = 0;
76962306a36Sopenharmony_ci	int cur_pos = 0, next_pos;
77062306a36Sopenharmony_ci	unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
77162306a36Sopenharmony_ci
77262306a36Sopenharmony_ci	/* check bitmap with valid block count */
77362306a36Sopenharmony_ci	do {
77462306a36Sopenharmony_ci		if (is_valid) {
77562306a36Sopenharmony_ci			next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
77662306a36Sopenharmony_ci					usable_blks_per_seg,
77762306a36Sopenharmony_ci					cur_pos);
77862306a36Sopenharmony_ci			valid_blocks += next_pos - cur_pos;
77962306a36Sopenharmony_ci		} else
78062306a36Sopenharmony_ci			next_pos = find_next_bit_le(&raw_sit->valid_map,
78162306a36Sopenharmony_ci					usable_blks_per_seg,
78262306a36Sopenharmony_ci					cur_pos);
78362306a36Sopenharmony_ci		cur_pos = next_pos;
78462306a36Sopenharmony_ci		is_valid = !is_valid;
78562306a36Sopenharmony_ci	} while (cur_pos < usable_blks_per_seg);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci	if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
78862306a36Sopenharmony_ci		f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
78962306a36Sopenharmony_ci			 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
79062306a36Sopenharmony_ci		set_sbi_flag(sbi, SBI_NEED_FSCK);
79162306a36Sopenharmony_ci		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
79262306a36Sopenharmony_ci		return -EFSCORRUPTED;
79362306a36Sopenharmony_ci	}
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	if (usable_blks_per_seg < sbi->blocks_per_seg)
79662306a36Sopenharmony_ci		f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
79762306a36Sopenharmony_ci				sbi->blocks_per_seg,
79862306a36Sopenharmony_ci				usable_blks_per_seg) != sbi->blocks_per_seg);
79962306a36Sopenharmony_ci
80062306a36Sopenharmony_ci	/* check segment usage, and check boundary of a given segment number */
80162306a36Sopenharmony_ci	if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
80262306a36Sopenharmony_ci					|| !valid_main_segno(sbi, segno))) {
80362306a36Sopenharmony_ci		f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
80462306a36Sopenharmony_ci			 GET_SIT_VBLOCKS(raw_sit), segno);
80562306a36Sopenharmony_ci		set_sbi_flag(sbi, SBI_NEED_FSCK);
80662306a36Sopenharmony_ci		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
80762306a36Sopenharmony_ci		return -EFSCORRUPTED;
80862306a36Sopenharmony_ci	}
80962306a36Sopenharmony_ci	return 0;
81062306a36Sopenharmony_ci}
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_cistatic inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
81362306a36Sopenharmony_ci						unsigned int start)
81462306a36Sopenharmony_ci{
81562306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
81662306a36Sopenharmony_ci	unsigned int offset = SIT_BLOCK_OFFSET(start);
81762306a36Sopenharmony_ci	block_t blk_addr = sit_i->sit_base_addr + offset;
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	f2fs_bug_on(sbi, !valid_main_segno(sbi, start));
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
82262306a36Sopenharmony_ci	if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
82362306a36Sopenharmony_ci			f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
82462306a36Sopenharmony_ci		f2fs_bug_on(sbi, 1);
82562306a36Sopenharmony_ci#endif
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci	/* calculate sit block address */
82862306a36Sopenharmony_ci	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
82962306a36Sopenharmony_ci		blk_addr += sit_i->sit_blocks;
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	return blk_addr;
83262306a36Sopenharmony_ci}
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_cistatic inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
83562306a36Sopenharmony_ci						pgoff_t block_addr)
83662306a36Sopenharmony_ci{
83762306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
83862306a36Sopenharmony_ci	block_addr -= sit_i->sit_base_addr;
83962306a36Sopenharmony_ci	if (block_addr < sit_i->sit_blocks)
84062306a36Sopenharmony_ci		block_addr += sit_i->sit_blocks;
84162306a36Sopenharmony_ci	else
84262306a36Sopenharmony_ci		block_addr -= sit_i->sit_blocks;
84362306a36Sopenharmony_ci
84462306a36Sopenharmony_ci	return block_addr + sit_i->sit_base_addr;
84562306a36Sopenharmony_ci}
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_cistatic inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
84862306a36Sopenharmony_ci{
84962306a36Sopenharmony_ci	unsigned int block_off = SIT_BLOCK_OFFSET(start);
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci	f2fs_change_bit(block_off, sit_i->sit_bitmap);
85262306a36Sopenharmony_ci#ifdef CONFIG_F2FS_CHECK_FS
85362306a36Sopenharmony_ci	f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
85462306a36Sopenharmony_ci#endif
85562306a36Sopenharmony_ci}
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_cistatic inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
85862306a36Sopenharmony_ci						bool base_time)
85962306a36Sopenharmony_ci{
86062306a36Sopenharmony_ci	struct sit_info *sit_i = SIT_I(sbi);
86162306a36Sopenharmony_ci	time64_t diff, now = ktime_get_boottime_seconds();
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci	if (now >= sit_i->mounted_time)
86462306a36Sopenharmony_ci		return sit_i->elapsed_time + now - sit_i->mounted_time;
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci	/* system time is set to the past */
86762306a36Sopenharmony_ci	if (!base_time) {
86862306a36Sopenharmony_ci		diff = sit_i->mounted_time - now;
86962306a36Sopenharmony_ci		if (sit_i->elapsed_time >= diff)
87062306a36Sopenharmony_ci			return sit_i->elapsed_time - diff;
87162306a36Sopenharmony_ci		return 0;
87262306a36Sopenharmony_ci	}
87362306a36Sopenharmony_ci	return sit_i->elapsed_time;
87462306a36Sopenharmony_ci}
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_cistatic inline void set_summary(struct f2fs_summary *sum, nid_t nid,
87762306a36Sopenharmony_ci			unsigned int ofs_in_node, unsigned char version)
87862306a36Sopenharmony_ci{
87962306a36Sopenharmony_ci	sum->nid = cpu_to_le32(nid);
88062306a36Sopenharmony_ci	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
88162306a36Sopenharmony_ci	sum->version = version;
88262306a36Sopenharmony_ci}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_cistatic inline block_t start_sum_block(struct f2fs_sb_info *sbi)
88562306a36Sopenharmony_ci{
88662306a36Sopenharmony_ci	return __start_cp_addr(sbi) +
88762306a36Sopenharmony_ci		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
88862306a36Sopenharmony_ci}
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_cistatic inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
89162306a36Sopenharmony_ci{
89262306a36Sopenharmony_ci	return __start_cp_addr(sbi) +
89362306a36Sopenharmony_ci		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
89462306a36Sopenharmony_ci				- (base + 1) + type;
89562306a36Sopenharmony_ci}
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_cistatic inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
89862306a36Sopenharmony_ci{
89962306a36Sopenharmony_ci	if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
90062306a36Sopenharmony_ci		return true;
90162306a36Sopenharmony_ci	return false;
90262306a36Sopenharmony_ci}
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ci/*
90562306a36Sopenharmony_ci * It is very important to gather dirty pages and write at once, so that we can
90662306a36Sopenharmony_ci * submit a big bio without interfering other data writes.
90762306a36Sopenharmony_ci * By default, 512 pages for directory data,
90862306a36Sopenharmony_ci * 512 pages (2MB) * 8 for nodes, and
90962306a36Sopenharmony_ci * 256 pages * 8 for meta are set.
91062306a36Sopenharmony_ci */
91162306a36Sopenharmony_cistatic inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
91262306a36Sopenharmony_ci{
91362306a36Sopenharmony_ci	if (sbi->sb->s_bdi->wb.dirty_exceeded)
91462306a36Sopenharmony_ci		return 0;
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	if (type == DATA)
91762306a36Sopenharmony_ci		return sbi->blocks_per_seg;
91862306a36Sopenharmony_ci	else if (type == NODE)
91962306a36Sopenharmony_ci		return 8 * sbi->blocks_per_seg;
92062306a36Sopenharmony_ci	else if (type == META)
92162306a36Sopenharmony_ci		return 8 * BIO_MAX_VECS;
92262306a36Sopenharmony_ci	else
92362306a36Sopenharmony_ci		return 0;
92462306a36Sopenharmony_ci}
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_ci/*
92762306a36Sopenharmony_ci * When writing pages, it'd better align nr_to_write for segment size.
92862306a36Sopenharmony_ci */
92962306a36Sopenharmony_cistatic inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
93062306a36Sopenharmony_ci					struct writeback_control *wbc)
93162306a36Sopenharmony_ci{
93262306a36Sopenharmony_ci	long nr_to_write, desired;
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci	if (wbc->sync_mode != WB_SYNC_NONE)
93562306a36Sopenharmony_ci		return 0;
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci	nr_to_write = wbc->nr_to_write;
93862306a36Sopenharmony_ci	desired = BIO_MAX_VECS;
93962306a36Sopenharmony_ci	if (type == NODE)
94062306a36Sopenharmony_ci		desired <<= 1;
94162306a36Sopenharmony_ci
94262306a36Sopenharmony_ci	wbc->nr_to_write = desired;
94362306a36Sopenharmony_ci	return desired - nr_to_write;
94462306a36Sopenharmony_ci}
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_cistatic inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
94762306a36Sopenharmony_ci{
94862306a36Sopenharmony_ci	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
94962306a36Sopenharmony_ci	bool wakeup = false;
95062306a36Sopenharmony_ci	int i;
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci	if (force)
95362306a36Sopenharmony_ci		goto wake_up;
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci	mutex_lock(&dcc->cmd_lock);
95662306a36Sopenharmony_ci	for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
95762306a36Sopenharmony_ci		if (i + 1 < dcc->discard_granularity)
95862306a36Sopenharmony_ci			break;
95962306a36Sopenharmony_ci		if (!list_empty(&dcc->pend_list[i])) {
96062306a36Sopenharmony_ci			wakeup = true;
96162306a36Sopenharmony_ci			break;
96262306a36Sopenharmony_ci		}
96362306a36Sopenharmony_ci	}
96462306a36Sopenharmony_ci	mutex_unlock(&dcc->cmd_lock);
96562306a36Sopenharmony_ci	if (!wakeup || !is_idle(sbi, DISCARD_TIME))
96662306a36Sopenharmony_ci		return;
96762306a36Sopenharmony_ciwake_up:
96862306a36Sopenharmony_ci	dcc->discard_wake = true;
96962306a36Sopenharmony_ci	wake_up_interruptible_all(&dcc->discard_wait_queue);
97062306a36Sopenharmony_ci}
971