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