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