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