Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * fs/f2fs/segment.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *             http://www.samsung.com/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/backing-dev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /* constant macro */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #define NULL_SEGNO			((unsigned int)(~0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #define NULL_SECNO			((unsigned int)(~0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define DEF_RECLAIM_PREFREE_SEGMENTS	5	/* 5% over total segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define DEF_MAX_RECLAIM_PREFREE_SEGMENTS	4096	/* 8GB in maximum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define F2FS_MIN_META_SEGMENTS	8 /* SB + 2 (CP + SIT + NAT) + SSA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* L: Logical segment # in volume, R: Relative segment # in main area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define GET_L2R_SEGNO(free_i, segno)	((segno) - (free_i)->start_segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define GET_R2L_SEGNO(free_i, segno)	((segno) + (free_i)->start_segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define IS_DATASEG(t)	((t) <= CURSEG_COLD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define IS_NODESEG(t)	((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 						unsigned short seg_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	f2fs_bug_on(sbi, seg_type >= NR_PERSISTENT_LOG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define IS_HOT(t)	((t) == CURSEG_HOT_NODE || (t) == CURSEG_HOT_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define IS_WARM(t)	((t) == CURSEG_WARM_NODE || (t) == CURSEG_WARM_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define IS_COLD(t)	((t) == CURSEG_COLD_NODE || (t) == CURSEG_COLD_DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define IS_CURSEG(sbi, seg)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	(((seg) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 ((seg) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 ((seg) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define IS_CURSEC(sbi, secno)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	(((secno) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 ((secno) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno /		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno /	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	  (sbi)->segs_per_sec) ||	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 ((secno) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno /	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	  (sbi)->segs_per_sec))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define MAIN_BLKADDR(sbi)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr : 				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define SEG0_BLKADDR(sbi)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : 				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define MAIN_SECS(sbi)	((sbi)->total_sections)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define TOTAL_SEGS(sbi)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	(SM_I(sbi) ? SM_I(sbi)->segment_count : 				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define SEGMENT_SIZE(sbi)	(1ULL << ((sbi)->log_blocksize +	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 					(sbi)->log_blocks_per_seg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define START_BLOCK(sbi, segno)	(SEG0_BLKADDR(sbi) +			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define NEXT_FREE_BLKADDR(sbi, curseg)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)	((blk_addr) - SEG0_BLKADDR(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & ((sbi)->blocks_per_seg - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define GET_SEGNO(sbi, blk_addr)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	((!__is_valid_data_blkaddr(blk_addr)) ?			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi),			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define BLKS_PER_SEC(sbi)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define GET_SEC_FROM_SEG(sbi, segno)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	(((segno) == -1) ? -1: (segno) / (sbi)->segs_per_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define GET_SEG_FROM_SEC(sbi, secno)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	((secno) * (sbi)->segs_per_sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define GET_ZONE_FROM_SEC(sbi, secno)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	(((secno) == -1) ? -1: (secno) / (sbi)->secs_per_zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define GET_ZONE_FROM_SEG(sbi, segno)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define GET_SUM_BLOCK(sbi, segno)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	((sbi)->sm_info->ssa_blkaddr + (segno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define GET_SUM_TYPE(footer) ((footer)->entry_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define SIT_ENTRY_OFFSET(sit_i, segno)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	((segno) % (sit_i)->sents_per_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define SIT_BLOCK_OFFSET(segno)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	((segno) / SIT_ENTRY_PER_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define	START_SEGNO(segno)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	(SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define SIT_BLK_CNT(sbi)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define f2fs_bitmap_size(nr)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	(BITS_TO_LONGS(nr) * sizeof(unsigned long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define SECTOR_FROM_BLOCK(blk_addr)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	(((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define SECTOR_TO_BLOCK(sectors)					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * indicate a block allocation direction: RIGHT and LEFT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * RIGHT means allocating new sections towards the end of volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  * LEFT means the opposite direction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	ALLOC_RIGHT = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ALLOC_LEFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)  * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  * LFS writes data sequentially with cleaning operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)  * fragmented segment which has similar aging degree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	LFS = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	SSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	AT_SSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * GC_CB is based on cost-benefit algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * GC_GREEDY is based on greedy algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * GC_AT is based on age-threshold algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	GC_CB = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	GC_GREEDY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	GC_AT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	ALLOC_NEXT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	FLUSH_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	MAX_GC_POLICY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * BG_GC means the background cleaning job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * FG_GC means the on-demand cleaning job.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	BG_GC = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	FG_GC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* for a function parameter to select a victim segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct victim_sel_policy {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	int alloc_mode;			/* LFS or SSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	int gc_mode;			/* GC_CB or GC_GREEDY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	unsigned long *dirty_bitmap;	/* dirty segment/section bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	unsigned int max_search;	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 					 * maximum # of segments/sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 					 * to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 					 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	unsigned int offset;		/* last scanned bitmap offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	unsigned int ofs_unit;		/* bitmap search unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	unsigned int min_cost;		/* minimum cost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	unsigned long long oldest_age;	/* oldest age of segments having the same min cost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	unsigned int min_segno;		/* segment # having min. cost */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	unsigned long long age;		/* mtime of GCed section*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	unsigned long long age_threshold;/* age threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct seg_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	unsigned int type:6;		/* segment type like CURSEG_XXX_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	unsigned int valid_blocks:10;	/* # of valid blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	unsigned int ckpt_valid_blocks:10;	/* # of valid blocks last cp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	unsigned int padding:6;		/* padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	unsigned char *cur_valid_map;	/* validity bitmap of blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	unsigned char *cur_valid_map_mir;	/* mirror of current valid bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * # of valid blocks and the validity bitmap stored in the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * checkpoint pack. This information is used by the SSR mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	unsigned char *discard_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	unsigned long long mtime;	/* modification time of the segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) struct sec_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	unsigned int valid_blocks;	/* # of valid blocks in a section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct segment_allocation {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #define MAX_SKIP_GC_COUNT			16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct inmem_pages {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	block_t old_addr;		/* for revoking when fail to commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct sit_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	const struct segment_allocation *s_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	block_t sit_base_addr;		/* start block address of SIT area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	block_t sit_blocks;		/* # of blocks used by SIT area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	block_t written_valid_blocks;	/* # of valid blocks in main area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	char *bitmap;			/* all bitmaps pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	char *sit_bitmap;		/* SIT bitmap pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	char *sit_bitmap_mir;		/* SIT bitmap mirror */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	/* bitmap of segments to be ignored by GC in case of errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	unsigned long *invalid_segmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	unsigned int bitmap_size;	/* SIT bitmap size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	unsigned long *tmp_map;			/* bitmap for temporal use */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	unsigned long *dirty_sentries_bitmap;	/* bitmap for dirty sentries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	unsigned int dirty_sentries;		/* # of dirty sentries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	unsigned int sents_per_block;		/* # of SIT entries per block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct rw_semaphore sentry_lock;	/* to protect SIT cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct seg_entry *sentries;		/* SIT segment-level cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	struct sec_entry *sec_entries;		/* SIT section-level cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	/* for cost-benefit algorithm in cleaning procedure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	unsigned long long elapsed_time;	/* elapsed time after mount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	unsigned long long mounted_time;	/* mount time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	unsigned long long min_mtime;		/* min. modification time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	unsigned long long max_mtime;		/* max. modification time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	unsigned long long dirty_min_mtime;	/* rerange candidates in GC_AT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	unsigned long long dirty_max_mtime;	/* rerange candidates in GC_AT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct free_segmap_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	unsigned int start_segno;	/* start segment number logically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	unsigned int free_segments;	/* # of free segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	unsigned int free_sections;	/* # of free sections */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	spinlock_t segmap_lock;		/* free segmap lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	unsigned long *free_segmap;	/* free segment bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	unsigned long *free_secmap;	/* free section bitmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) enum dirty_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	DIRTY_HOT_DATA,		/* dirty segments assigned as hot data logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	DIRTY_WARM_DATA,	/* dirty segments assigned as warm data logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	DIRTY_COLD_DATA,	/* dirty segments assigned as cold data logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	DIRTY_HOT_NODE,		/* dirty segments assigned as hot node logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	DIRTY_WARM_NODE,	/* dirty segments assigned as warm node logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	DIRTY_COLD_NODE,	/* dirty segments assigned as cold node logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	DIRTY,			/* to count # of dirty segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	PRE,			/* to count # of entirely obsolete segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	NR_DIRTY_TYPE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) struct dirty_seglist_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	const struct victim_selection *v_ops;	/* victim selction operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	unsigned long *dirty_segmap[NR_DIRTY_TYPE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	unsigned long *dirty_secmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct mutex seglist_lock;		/* lock for segment bitmaps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	int nr_dirty[NR_DIRTY_TYPE];		/* # of dirty segments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	unsigned long *victim_secmap;		/* background GC victims */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* victim selection function for cleaning and SSR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) struct victim_selection {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 					int, int, char, unsigned long long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /* for active log information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct curseg_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct mutex curseg_mutex;		/* lock for consistency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct f2fs_summary_block *sum_blk;	/* cached summary block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	struct rw_semaphore journal_rwsem;	/* protect journal area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	struct f2fs_journal *journal;		/* cached journal info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	unsigned char alloc_type;		/* current allocation type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	unsigned short seg_type;		/* segment type like CURSEG_XXX_TYPE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	unsigned int segno;			/* current segment number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned short next_blkoff;		/* next block offset to write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	unsigned int zone;			/* current zone number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	unsigned int next_segno;		/* preallocated segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	bool inited;				/* indicate inmem log is inited */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct sit_entry_set {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct list_head set_list;	/* link with all sit sets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	unsigned int start_segno;	/* start segno of sits in set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	unsigned int entry_cnt;		/* the # of sit entries in set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * inline functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 						unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return &sit_i->sentries[segno];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 						unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 				unsigned int segno, bool use_section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	 * In order to get # of valid blocks in a section instantly from many
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	 * segments, f2fs manages two counting structures separately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (use_section && __is_large_section(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return get_sec_entry(sbi, segno)->valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return get_seg_entry(sbi, segno)->valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				unsigned int segno, bool use_section)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	if (use_section && __is_large_section(sbi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		unsigned int start_segno = START_SEGNO(segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		unsigned int blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		for (i = 0; i < sbi->segs_per_sec; i++, start_segno++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			struct seg_entry *se = get_seg_entry(sbi, start_segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			blocks += se->ckpt_valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		return blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static inline void seg_info_from_raw_sit(struct seg_entry *se,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 					struct f2fs_sit_entry *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	se->valid_blocks = GET_SIT_VBLOCKS(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	se->type = GET_SIT_TYPE(rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	se->mtime = le64_to_cpu(rs->mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static inline void __seg_info_to_raw_sit(struct seg_entry *se,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 					struct f2fs_sit_entry *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 					se->valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	rs->vblocks = cpu_to_le16(raw_vblocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	rs->mtime = cpu_to_le64(se->mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 				struct page *page, unsigned int start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct f2fs_sit_block *raw_sit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct seg_entry *se;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	struct f2fs_sit_entry *rs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 					(unsigned long)MAIN_SEGS(sbi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	raw_sit = (struct f2fs_sit_block *)page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	memset(raw_sit, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	for (i = 0; i < end - start; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		rs = &raw_sit->entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		se = get_seg_entry(sbi, start + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		__seg_info_to_raw_sit(se, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static inline void seg_info_to_raw_sit(struct seg_entry *se,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 					struct f2fs_sit_entry *rs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	__seg_info_to_raw_sit(se, rs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	se->ckpt_valid_blocks = se->valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		unsigned int max, unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	unsigned int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	spin_lock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	ret = find_next_bit(free_i->free_segmap, max, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	spin_unlock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct free_segmap_info *free_i = FREE_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	unsigned int next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	spin_lock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	clear_bit(segno, free_i->free_segmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	free_i->free_segments++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	next = find_next_bit(free_i->free_segmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			start_segno + sbi->segs_per_sec, start_segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	if (next >= start_segno + usable_segs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		clear_bit(secno, free_i->free_secmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		free_i->free_sections++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	spin_unlock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) static inline void __set_inuse(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 		unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct free_segmap_info *free_i = FREE_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	set_bit(segno, free_i->free_segmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	free_i->free_segments--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (!test_and_set_bit(secno, free_i->free_secmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		free_i->free_sections--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		unsigned int segno, bool inmem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	struct free_segmap_info *free_i = FREE_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	unsigned int next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	spin_lock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (test_and_clear_bit(segno, free_i->free_segmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		free_i->free_segments++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (!inmem && IS_CURSEC(sbi, secno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			goto skip_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		next = find_next_bit(free_i->free_segmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 				start_segno + sbi->segs_per_sec, start_segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		if (next >= start_segno + usable_segs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			if (test_and_clear_bit(secno, free_i->free_secmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 				free_i->free_sections++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) skip_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	spin_unlock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	struct free_segmap_info *free_i = FREE_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	spin_lock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	if (!test_and_set_bit(segno, free_i->free_segmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		free_i->free_segments--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (!test_and_set_bit(secno, free_i->free_secmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			free_i->free_sections--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	spin_unlock(&free_i->segmap_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		void *dst_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 						sit_i->bitmap_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		f2fs_bug_on(sbi, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static inline block_t written_block_count(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	return SIT_I(sbi)->written_valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	return FREE_I(sbi)->free_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	return SM_I(sbi)->reserved_segments +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			SM_I(sbi)->additional_reserved_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	return FREE_I(sbi)->free_sections;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	return DIRTY_I(sbi)->nr_dirty[PRE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static inline int overprovision_segments(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	return SM_I(sbi)->ovp_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static inline int reserved_sections(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	unsigned int node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 					get_pages(sbi, F2FS_DIRTY_DENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	unsigned int segno, left_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	/* check current node segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		segno = CURSEG_I(sbi, i)->segno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 				get_seg_entry(sbi, segno)->ckpt_valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		if (node_blocks > left_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	/* check current data segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			get_seg_entry(sbi, segno)->ckpt_valid_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	if (dent_blocks > left_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 					int freed, int needed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	if (free_sections(sbi) + freed == reserved_sections(sbi) + needed &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			has_curseg_enough_space(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	return (free_sections(sbi) + freed) <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		(node_secs + 2 * dent_secs + imeta_secs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		reserved_sections(sbi) + needed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (likely(!has_not_enough_free_secs(sbi, 0, 0)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static inline int utilization(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	return div_u64((u64)valid_user_blocks(sbi) * 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 					sbi->user_block_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)  * Sometimes f2fs may be better to drop out-of-place update policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)  * And, users can control the policy through sysfs entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)  * There are five policies with triggering conditions as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)  * F2FS_IPU_FORCE - all the time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  * F2FS_IPU_SSR - if SSR mode is activated,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  * F2FS_IPU_UTIL - if FS utilization is over threashold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)  * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)  *                     threashold,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)  * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  *                     storages. IPU will be triggered only if the # of dirty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  *                     pages over min_fsync_blocks. (=default option)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)  * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)  * F2FS_IPU_NOCACHE - disable IPU bio cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)  * F2FS_IPUT_DISABLE - disable IPU. (=default option in LFS mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) #define DEF_MIN_IPU_UTIL	70
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) #define DEF_MIN_FSYNC_BLOCKS	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) #define DEF_MIN_HOT_BLOCKS	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) #define SMALL_VOLUME_SEGMENTS	(16 * 512)	/* 16GB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	F2FS_IPU_FORCE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	F2FS_IPU_SSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	F2FS_IPU_UTIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	F2FS_IPU_SSR_UTIL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	F2FS_IPU_FSYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	F2FS_IPU_ASYNC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	F2FS_IPU_NOCACHE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	struct curseg_info *curseg = CURSEG_I(sbi, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	return curseg->segno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	struct curseg_info *curseg = CURSEG_I(sbi, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	return curseg->alloc_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	struct curseg_info *curseg = CURSEG_I(sbi, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return curseg->next_blkoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) static inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	struct f2fs_sb_info *sbi = fio->sbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	if (__is_valid_data_blkaddr(fio->old_blkaddr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 					META_GENERIC : DATA_GENERIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 					META_GENERIC : DATA_GENERIC_ENHANCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)  * Summary block is always treated as an invalid block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) static inline int check_block_count(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		int segno, struct f2fs_sit_entry *raw_sit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	int valid_blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	int cur_pos = 0, next_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	/* check bitmap with valid block count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		if (is_valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 			next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 					usable_blks_per_seg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 					cur_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			valid_blocks += next_pos - cur_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 			next_pos = find_next_bit_le(&raw_sit->valid_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 					usable_blks_per_seg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 					cur_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		cur_pos = next_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		is_valid = !is_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	} while (cur_pos < usable_blks_per_seg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 			 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		set_sbi_flag(sbi, SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	if (usable_blks_per_seg < sbi->blocks_per_seg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 				sbi->blocks_per_seg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 				usable_blks_per_seg) != sbi->blocks_per_seg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	/* check segment usage, and check boundary of a given segment number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 					|| segno > TOTAL_SEGS(sbi) - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 			 GET_SIT_VBLOCKS(raw_sit), segno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		set_sbi_flag(sbi, SBI_NEED_FSCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 						unsigned int start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	unsigned int offset = SIT_BLOCK_OFFSET(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	block_t blk_addr = sit_i->sit_base_addr + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	check_seg_range(sbi, start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 			f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		f2fs_bug_on(sbi, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	/* calculate sit block address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 		blk_addr += sit_i->sit_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	return blk_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 						pgoff_t block_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	block_addr -= sit_i->sit_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (block_addr < sit_i->sit_blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		block_addr += sit_i->sit_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 		block_addr -= sit_i->sit_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	return block_addr + sit_i->sit_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	unsigned int block_off = SIT_BLOCK_OFFSET(start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	f2fs_change_bit(block_off, sit_i->sit_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) #ifdef CONFIG_F2FS_CHECK_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 						bool base_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	struct sit_info *sit_i = SIT_I(sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	time64_t diff, now = ktime_get_boottime_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	if (now >= sit_i->mounted_time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		return sit_i->elapsed_time + now - sit_i->mounted_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	/* system time is set to the past */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	if (!base_time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 		diff = sit_i->mounted_time - now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		if (sit_i->elapsed_time >= diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 			return sit_i->elapsed_time - diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	return sit_i->elapsed_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 			unsigned int ofs_in_node, unsigned char version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	sum->nid = cpu_to_le32(nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	sum->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	return __start_cp_addr(sbi) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	return __start_cp_addr(sbi) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 				- (base + 1) + type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)  * It is very important to gather dirty pages and write at once, so that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)  * submit a big bio without interfering other data writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)  * By default, 512 pages for directory data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)  * 512 pages (2MB) * 8 for nodes, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)  * 256 pages * 8 for meta are set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	if (sbi->sb->s_bdi->wb.dirty_exceeded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	if (type == DATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 		return sbi->blocks_per_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 	else if (type == NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 		return 8 * sbi->blocks_per_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	else if (type == META)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 		return 8 * BIO_MAX_PAGES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)  * When writing pages, it'd better align nr_to_write for segment size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 					struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	long nr_to_write, desired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	if (wbc->sync_mode != WB_SYNC_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	nr_to_write = wbc->nr_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	desired = BIO_MAX_PAGES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	if (type == NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		desired <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	wbc->nr_to_write = desired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	return desired - nr_to_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	bool wakeup = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	if (force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 		goto wake_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	mutex_lock(&dcc->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 		if (i + 1 < dcc->discard_granularity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 		if (!list_empty(&dcc->pend_list[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 			wakeup = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	mutex_unlock(&dcc->cmd_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	if (!wakeup || !is_idle(sbi, DISCARD_TIME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) wake_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	dcc->discard_wake = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	wake_up_interruptible_all(&dcc->discard_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }