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