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