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