projects/linux·blob master · refs

1039 lines · 31.4 KiB

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