blob: eafd89f0c77e8c5496a73957f5d9f31e22d0aa41 [file] [log] [blame]
Olivier Deprez157378f2022-04-04 15:47:50 +02001/* SPDX-License-Identifier: GPL-2.0 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * fs/f2fs/segment.h
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
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 */
Olivier Deprez157378f2022-04-04 15:47:50 +020019#define F2FS_MIN_META_SEGMENTS 8 /* SB + 2 (CP + SIT + NAT) + SSA */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020
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)
Olivier Deprez157378f2022-04-04 15:47:50 +020026#define IS_NODESEG(t) ((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
Olivier Deprez92d4c212022-12-06 15:05:30 +010027#define SE_PAGETYPE(se) ((IS_NODESEG((se)->type) ? NODE : DATA))
Olivier Deprez157378f2022-04-04 15:47:50 +020028
29static 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}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
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) || \
Olivier Deprez157378f2022-04-04 15:47:50 +020045 ((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))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
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 / \
Olivier Deprez157378f2022-04-04 15:47:50 +020061 (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))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066
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) \
David Brazdil0f672f62019-12-10 10:32:29 +000099 ((!__is_valid_data_blkaddr(blk_addr)) ? \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000100 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) \
Olivier Deprez0e641232021-09-23 10:07:05 +0200105 (((segno) == -1) ? -1: (segno) / (sbi)->segs_per_sec)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106#define GET_SEG_FROM_SEC(sbi, secno) \
107 ((secno) * (sbi)->segs_per_sec)
108#define GET_ZONE_FROM_SEC(sbi, secno) \
Olivier Deprez0e641232021-09-23 10:07:05 +0200109 (((secno) == -1) ? -1: (secno) / (sbi)->secs_per_zone)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110#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) \
David Brazdil0f672f62019-12-10 10:32:29 +0000126 DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127#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
135/*
136 * indicate a block allocation direction: RIGHT and LEFT.
137 * RIGHT means allocating new sections towards the end of volume.
138 * LEFT means the opposite direction.
139 */
140enum {
141 ALLOC_RIGHT = 0,
142 ALLOC_LEFT
143};
144
145/*
146 * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
147 * LFS writes data sequentially with cleaning operations.
148 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
150 * fragmented segment which has similar aging degree.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151 */
152enum {
153 LFS = 0,
Olivier Deprez157378f2022-04-04 15:47:50 +0200154 SSR,
155 AT_SSR,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156};
157
158/*
159 * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
160 * GC_CB is based on cost-benefit algorithm.
161 * GC_GREEDY is based on greedy algorithm.
Olivier Deprez157378f2022-04-04 15:47:50 +0200162 * GC_AT is based on age-threshold algorithm.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000163 */
164enum {
165 GC_CB = 0,
166 GC_GREEDY,
Olivier Deprez157378f2022-04-04 15:47:50 +0200167 GC_AT,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168 ALLOC_NEXT,
169 FLUSH_DEVICE,
170 MAX_GC_POLICY,
171};
172
173/*
174 * BG_GC means the background cleaning job.
175 * FG_GC means the on-demand cleaning job.
176 * FORCE_FG_GC means on-demand cleaning job in background.
177 */
178enum {
179 BG_GC = 0,
180 FG_GC,
181 FORCE_FG_GC,
182};
183
184/* for a function parameter to select a victim segment */
185struct victim_sel_policy {
186 int alloc_mode; /* LFS or SSR */
187 int gc_mode; /* GC_CB or GC_GREEDY */
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 unsigned long *dirty_bitmap; /* dirty segment/section bitmap */
189 unsigned int max_search; /*
190 * maximum # of segments/sections
191 * to search
192 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 unsigned int offset; /* last scanned bitmap offset */
194 unsigned int ofs_unit; /* bitmap search unit */
195 unsigned int min_cost; /* minimum cost */
Olivier Deprez157378f2022-04-04 15:47:50 +0200196 unsigned long long oldest_age; /* oldest age of segments having the same min cost */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 unsigned int min_segno; /* segment # having min. cost */
Olivier Deprez157378f2022-04-04 15:47:50 +0200198 unsigned long long age; /* mtime of GCed section*/
199 unsigned long long age_threshold;/* age threshold */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000200};
201
202struct seg_entry {
203 unsigned int type:6; /* segment type like CURSEG_XXX_TYPE */
204 unsigned int valid_blocks:10; /* # of valid blocks */
205 unsigned int ckpt_valid_blocks:10; /* # of valid blocks last cp */
206 unsigned int padding:6; /* padding */
207 unsigned char *cur_valid_map; /* validity bitmap of blocks */
208#ifdef CONFIG_F2FS_CHECK_FS
209 unsigned char *cur_valid_map_mir; /* mirror of current valid bitmap */
210#endif
211 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200212 * # of valid blocks and the validity bitmap stored in the last
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000213 * checkpoint pack. This information is used by the SSR mode.
214 */
215 unsigned char *ckpt_valid_map; /* validity bitmap of blocks last cp */
216 unsigned char *discard_map;
217 unsigned long long mtime; /* modification time of the segment */
218};
219
220struct sec_entry {
221 unsigned int valid_blocks; /* # of valid blocks in a section */
222};
223
224struct segment_allocation {
225 void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
226};
227
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228#define MAX_SKIP_GC_COUNT 16
229
230struct inmem_pages {
231 struct list_head list;
232 struct page *page;
233 block_t old_addr; /* for revoking when fail to commit */
234};
235
236struct sit_info {
237 const struct segment_allocation *s_ops;
238
239 block_t sit_base_addr; /* start block address of SIT area */
240 block_t sit_blocks; /* # of blocks used by SIT area */
241 block_t written_valid_blocks; /* # of valid blocks in main area */
David Brazdil0f672f62019-12-10 10:32:29 +0000242 char *bitmap; /* all bitmaps pointer */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243 char *sit_bitmap; /* SIT bitmap pointer */
244#ifdef CONFIG_F2FS_CHECK_FS
245 char *sit_bitmap_mir; /* SIT bitmap mirror */
David Brazdil0f672f62019-12-10 10:32:29 +0000246
247 /* bitmap of segments to be ignored by GC in case of errors */
248 unsigned long *invalid_segmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000249#endif
250 unsigned int bitmap_size; /* SIT bitmap size */
251
252 unsigned long *tmp_map; /* bitmap for temporal use */
253 unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
254 unsigned int dirty_sentries; /* # of dirty sentries */
255 unsigned int sents_per_block; /* # of SIT entries per block */
256 struct rw_semaphore sentry_lock; /* to protect SIT cache */
257 struct seg_entry *sentries; /* SIT segment-level cache */
258 struct sec_entry *sec_entries; /* SIT section-level cache */
259
260 /* for cost-benefit algorithm in cleaning procedure */
261 unsigned long long elapsed_time; /* elapsed time after mount */
262 unsigned long long mounted_time; /* mount time */
263 unsigned long long min_mtime; /* min. modification time */
264 unsigned long long max_mtime; /* max. modification time */
Olivier Deprez157378f2022-04-04 15:47:50 +0200265 unsigned long long dirty_min_mtime; /* rerange candidates in GC_AT */
266 unsigned long long dirty_max_mtime; /* rerange candidates in GC_AT */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267
268 unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
269};
270
271struct free_segmap_info {
272 unsigned int start_segno; /* start segment number logically */
273 unsigned int free_segments; /* # of free segments */
274 unsigned int free_sections; /* # of free sections */
275 spinlock_t segmap_lock; /* free segmap lock */
276 unsigned long *free_segmap; /* free segment bitmap */
277 unsigned long *free_secmap; /* free section bitmap */
278};
279
280/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
281enum dirty_type {
282 DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
283 DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
284 DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
285 DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
286 DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
287 DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
288 DIRTY, /* to count # of dirty segments */
289 PRE, /* to count # of entirely obsolete segments */
290 NR_DIRTY_TYPE
291};
292
293struct dirty_seglist_info {
294 const struct victim_selection *v_ops; /* victim selction operation */
295 unsigned long *dirty_segmap[NR_DIRTY_TYPE];
Olivier Deprez157378f2022-04-04 15:47:50 +0200296 unsigned long *dirty_secmap;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 struct mutex seglist_lock; /* lock for segment bitmaps */
298 int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
299 unsigned long *victim_secmap; /* background GC victims */
300};
301
302/* victim selection function for cleaning and SSR */
303struct victim_selection {
304 int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
Olivier Deprez157378f2022-04-04 15:47:50 +0200305 int, int, char, unsigned long long);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306};
307
308/* for active log information */
309struct curseg_info {
310 struct mutex curseg_mutex; /* lock for consistency */
311 struct f2fs_summary_block *sum_blk; /* cached summary block */
312 struct rw_semaphore journal_rwsem; /* protect journal area */
313 struct f2fs_journal *journal; /* cached journal info */
314 unsigned char alloc_type; /* current allocation type */
Olivier Deprez157378f2022-04-04 15:47:50 +0200315 unsigned short seg_type; /* segment type like CURSEG_XXX_TYPE */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316 unsigned int segno; /* current segment number */
317 unsigned short next_blkoff; /* next block offset to write */
318 unsigned int zone; /* current zone number */
319 unsigned int next_segno; /* preallocated segment */
Olivier Deprez157378f2022-04-04 15:47:50 +0200320 bool inited; /* indicate inmem log is inited */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321};
322
323struct sit_entry_set {
324 struct list_head set_list; /* link with all sit sets */
325 unsigned int start_segno; /* start segno of sits in set */
326 unsigned int entry_cnt; /* the # of sit entries in set */
327};
328
329/*
330 * inline functions
331 */
332static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
333{
334 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
335}
336
337static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
338 unsigned int segno)
339{
340 struct sit_info *sit_i = SIT_I(sbi);
341 return &sit_i->sentries[segno];
342}
343
344static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
345 unsigned int segno)
346{
347 struct sit_info *sit_i = SIT_I(sbi);
348 return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
349}
350
351static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
352 unsigned int segno, bool use_section)
353{
354 /*
355 * In order to get # of valid blocks in a section instantly from many
356 * segments, f2fs manages two counting structures separately.
357 */
David Brazdil0f672f62019-12-10 10:32:29 +0000358 if (use_section && __is_large_section(sbi))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000359 return get_sec_entry(sbi, segno)->valid_blocks;
360 else
361 return get_seg_entry(sbi, segno)->valid_blocks;
362}
363
David Brazdil0f672f62019-12-10 10:32:29 +0000364static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
Olivier Deprez157378f2022-04-04 15:47:50 +0200365 unsigned int segno, bool use_section)
David Brazdil0f672f62019-12-10 10:32:29 +0000366{
Olivier Deprez157378f2022-04-04 15:47:50 +0200367 if (use_section && __is_large_section(sbi)) {
368 unsigned int start_segno = START_SEGNO(segno);
369 unsigned int blocks = 0;
370 int i;
371
372 for (i = 0; i < sbi->segs_per_sec; i++, start_segno++) {
373 struct seg_entry *se = get_seg_entry(sbi, start_segno);
374
375 blocks += se->ckpt_valid_blocks;
376 }
377 return blocks;
378 }
David Brazdil0f672f62019-12-10 10:32:29 +0000379 return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
380}
381
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382static inline void seg_info_from_raw_sit(struct seg_entry *se,
383 struct f2fs_sit_entry *rs)
384{
385 se->valid_blocks = GET_SIT_VBLOCKS(rs);
386 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
387 memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
388 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
389#ifdef CONFIG_F2FS_CHECK_FS
390 memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
391#endif
392 se->type = GET_SIT_TYPE(rs);
393 se->mtime = le64_to_cpu(rs->mtime);
394}
395
396static inline void __seg_info_to_raw_sit(struct seg_entry *se,
397 struct f2fs_sit_entry *rs)
398{
399 unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
400 se->valid_blocks;
401 rs->vblocks = cpu_to_le16(raw_vblocks);
402 memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
403 rs->mtime = cpu_to_le64(se->mtime);
404}
405
406static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
407 struct page *page, unsigned int start)
408{
409 struct f2fs_sit_block *raw_sit;
410 struct seg_entry *se;
411 struct f2fs_sit_entry *rs;
412 unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
413 (unsigned long)MAIN_SEGS(sbi));
414 int i;
415
416 raw_sit = (struct f2fs_sit_block *)page_address(page);
417 memset(raw_sit, 0, PAGE_SIZE);
418 for (i = 0; i < end - start; i++) {
419 rs = &raw_sit->entries[i];
420 se = get_seg_entry(sbi, start + i);
421 __seg_info_to_raw_sit(se, rs);
422 }
423}
424
425static inline void seg_info_to_raw_sit(struct seg_entry *se,
426 struct f2fs_sit_entry *rs)
427{
428 __seg_info_to_raw_sit(se, rs);
429
430 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
431 se->ckpt_valid_blocks = se->valid_blocks;
432}
433
434static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
435 unsigned int max, unsigned int segno)
436{
437 unsigned int ret;
438 spin_lock(&free_i->segmap_lock);
439 ret = find_next_bit(free_i->free_segmap, max, segno);
440 spin_unlock(&free_i->segmap_lock);
441 return ret;
442}
443
444static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
445{
446 struct free_segmap_info *free_i = FREE_I(sbi);
447 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
448 unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
449 unsigned int next;
Olivier Deprez157378f2022-04-04 15:47:50 +0200450 unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000451
452 spin_lock(&free_i->segmap_lock);
453 clear_bit(segno, free_i->free_segmap);
454 free_i->free_segments++;
455
456 next = find_next_bit(free_i->free_segmap,
457 start_segno + sbi->segs_per_sec, start_segno);
Olivier Deprez157378f2022-04-04 15:47:50 +0200458 if (next >= start_segno + usable_segs) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000459 clear_bit(secno, free_i->free_secmap);
460 free_i->free_sections++;
461 }
462 spin_unlock(&free_i->segmap_lock);
463}
464
465static inline void __set_inuse(struct f2fs_sb_info *sbi,
466 unsigned int segno)
467{
468 struct free_segmap_info *free_i = FREE_I(sbi);
469 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
470
471 set_bit(segno, free_i->free_segmap);
472 free_i->free_segments--;
473 if (!test_and_set_bit(secno, free_i->free_secmap))
474 free_i->free_sections--;
475}
476
477static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
Olivier Deprez157378f2022-04-04 15:47:50 +0200478 unsigned int segno, bool inmem)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479{
480 struct free_segmap_info *free_i = FREE_I(sbi);
481 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
482 unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
483 unsigned int next;
Olivier Deprez157378f2022-04-04 15:47:50 +0200484 unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485
486 spin_lock(&free_i->segmap_lock);
487 if (test_and_clear_bit(segno, free_i->free_segmap)) {
488 free_i->free_segments++;
489
Olivier Deprez157378f2022-04-04 15:47:50 +0200490 if (!inmem && IS_CURSEC(sbi, secno))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 goto skip_free;
492 next = find_next_bit(free_i->free_segmap,
493 start_segno + sbi->segs_per_sec, start_segno);
Olivier Deprez157378f2022-04-04 15:47:50 +0200494 if (next >= start_segno + usable_segs) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495 if (test_and_clear_bit(secno, free_i->free_secmap))
496 free_i->free_sections++;
497 }
498 }
499skip_free:
500 spin_unlock(&free_i->segmap_lock);
501}
502
503static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
504 unsigned int segno)
505{
506 struct free_segmap_info *free_i = FREE_I(sbi);
507 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
508
509 spin_lock(&free_i->segmap_lock);
510 if (!test_and_set_bit(segno, free_i->free_segmap)) {
511 free_i->free_segments--;
512 if (!test_and_set_bit(secno, free_i->free_secmap))
513 free_i->free_sections--;
514 }
515 spin_unlock(&free_i->segmap_lock);
516}
517
518static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
519 void *dst_addr)
520{
521 struct sit_info *sit_i = SIT_I(sbi);
522
523#ifdef CONFIG_F2FS_CHECK_FS
524 if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
525 sit_i->bitmap_size))
526 f2fs_bug_on(sbi, 1);
527#endif
528 memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
529}
530
531static inline block_t written_block_count(struct f2fs_sb_info *sbi)
532{
533 return SIT_I(sbi)->written_valid_blocks;
534}
535
536static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
537{
538 return FREE_I(sbi)->free_segments;
539}
540
Olivier Deprez157378f2022-04-04 15:47:50 +0200541static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000542{
Olivier Deprez157378f2022-04-04 15:47:50 +0200543 return SM_I(sbi)->reserved_segments +
544 SM_I(sbi)->additional_reserved_segments;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000545}
546
547static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
548{
549 return FREE_I(sbi)->free_sections;
550}
551
552static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
553{
554 return DIRTY_I(sbi)->nr_dirty[PRE];
555}
556
557static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
558{
559 return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
560 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
561 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
562 DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
563 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
564 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
565}
566
567static inline int overprovision_segments(struct f2fs_sb_info *sbi)
568{
569 return SM_I(sbi)->ovp_segments;
570}
571
572static inline int reserved_sections(struct f2fs_sb_info *sbi)
573{
Olivier Deprez157378f2022-04-04 15:47:50 +0200574 return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000575}
576
Olivier Deprez92d4c212022-12-06 15:05:30 +0100577static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
578 unsigned int node_blocks, unsigned int dent_blocks)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579{
Olivier Deprez92d4c212022-12-06 15:05:30 +0100580
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000581 unsigned int segno, left_blocks;
582 int i;
583
584 /* check current node segment */
585 for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
586 segno = CURSEG_I(sbi, i)->segno;
Olivier Deprez157378f2022-04-04 15:47:50 +0200587 left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
588 get_seg_entry(sbi, segno)->ckpt_valid_blocks;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000589
590 if (node_blocks > left_blocks)
591 return false;
592 }
593
594 /* check current data segment */
595 segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
Olivier Deprez157378f2022-04-04 15:47:50 +0200596 left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597 get_seg_entry(sbi, segno)->ckpt_valid_blocks;
598 if (dent_blocks > left_blocks)
599 return false;
600 return true;
601}
602
603static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
604 int freed, int needed)
605{
Olivier Deprez92d4c212022-12-06 15:05:30 +0100606 unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
607 get_pages(sbi, F2FS_DIRTY_DENTS) +
608 get_pages(sbi, F2FS_DIRTY_IMETA);
609 unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
610 unsigned int node_secs = total_node_blocks / BLKS_PER_SEC(sbi);
611 unsigned int dent_secs = total_dent_blocks / BLKS_PER_SEC(sbi);
612 unsigned int node_blocks = total_node_blocks % BLKS_PER_SEC(sbi);
613 unsigned int dent_blocks = total_dent_blocks % BLKS_PER_SEC(sbi);
614 unsigned int free, need_lower, need_upper;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000615
616 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
617 return false;
618
Olivier Deprez92d4c212022-12-06 15:05:30 +0100619 free = free_sections(sbi) + freed;
620 need_lower = node_secs + dent_secs + reserved_sections(sbi) + needed;
621 need_upper = need_lower + (node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0);
622
623 if (free > need_upper)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624 return false;
Olivier Deprez92d4c212022-12-06 15:05:30 +0100625 else if (free <= need_lower)
626 return true;
627 return !has_curseg_enough_space(sbi, node_blocks, dent_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000628}
629
David Brazdil0f672f62019-12-10 10:32:29 +0000630static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
631{
632 if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
633 return true;
634 if (likely(!has_not_enough_free_secs(sbi, 0, 0)))
635 return true;
636 return false;
637}
638
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000639static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
640{
641 return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
642}
643
644static inline int utilization(struct f2fs_sb_info *sbi)
645{
646 return div_u64((u64)valid_user_blocks(sbi) * 100,
647 sbi->user_block_count);
648}
649
650/*
651 * Sometimes f2fs may be better to drop out-of-place update policy.
652 * And, users can control the policy through sysfs entries.
653 * There are five policies with triggering conditions as follows.
654 * F2FS_IPU_FORCE - all the time,
655 * F2FS_IPU_SSR - if SSR mode is activated,
656 * F2FS_IPU_UTIL - if FS utilization is over threashold,
657 * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
658 * threashold,
659 * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
660 * storages. IPU will be triggered only if the # of dirty
Olivier Deprez157378f2022-04-04 15:47:50 +0200661 * pages over min_fsync_blocks. (=default option)
662 * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
663 * F2FS_IPU_NOCACHE - disable IPU bio cache.
664 * F2FS_IPUT_DISABLE - disable IPU. (=default option in LFS mode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000665 */
666#define DEF_MIN_IPU_UTIL 70
667#define DEF_MIN_FSYNC_BLOCKS 8
668#define DEF_MIN_HOT_BLOCKS 16
669
670#define SMALL_VOLUME_SEGMENTS (16 * 512) /* 16GB */
671
672enum {
673 F2FS_IPU_FORCE,
674 F2FS_IPU_SSR,
675 F2FS_IPU_UTIL,
676 F2FS_IPU_SSR_UTIL,
677 F2FS_IPU_FSYNC,
678 F2FS_IPU_ASYNC,
Olivier Deprez157378f2022-04-04 15:47:50 +0200679 F2FS_IPU_NOCACHE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000680};
681
682static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
683 int type)
684{
685 struct curseg_info *curseg = CURSEG_I(sbi, type);
686 return curseg->segno;
687}
688
689static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
690 int type)
691{
692 struct curseg_info *curseg = CURSEG_I(sbi, type);
693 return curseg->alloc_type;
694}
695
696static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
697{
698 struct curseg_info *curseg = CURSEG_I(sbi, type);
699 return curseg->next_blkoff;
700}
701
702static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
703{
704 f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
705}
706
David Brazdil0f672f62019-12-10 10:32:29 +0000707static inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000708{
709 struct f2fs_sb_info *sbi = fio->sbi;
710
David Brazdil0f672f62019-12-10 10:32:29 +0000711 if (__is_valid_data_blkaddr(fio->old_blkaddr))
712 verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
713 META_GENERIC : DATA_GENERIC);
714 verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
715 META_GENERIC : DATA_GENERIC_ENHANCE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000716}
717
718/*
719 * Summary block is always treated as an invalid block
720 */
721static inline int check_block_count(struct f2fs_sb_info *sbi,
722 int segno, struct f2fs_sit_entry *raw_sit)
723{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000724 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
725 int valid_blocks = 0;
726 int cur_pos = 0, next_pos;
Olivier Deprez157378f2022-04-04 15:47:50 +0200727 unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000728
729 /* check bitmap with valid block count */
730 do {
731 if (is_valid) {
732 next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
Olivier Deprez157378f2022-04-04 15:47:50 +0200733 usable_blks_per_seg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000734 cur_pos);
735 valid_blocks += next_pos - cur_pos;
736 } else
737 next_pos = find_next_bit_le(&raw_sit->valid_map,
Olivier Deprez157378f2022-04-04 15:47:50 +0200738 usable_blks_per_seg,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 cur_pos);
740 cur_pos = next_pos;
741 is_valid = !is_valid;
Olivier Deprez157378f2022-04-04 15:47:50 +0200742 } while (cur_pos < usable_blks_per_seg);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000743
744 if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000745 f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
746 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000747 set_sbi_flag(sbi, SBI_NEED_FSCK);
David Brazdil0f672f62019-12-10 10:32:29 +0000748 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749 }
David Brazdil0f672f62019-12-10 10:32:29 +0000750
Olivier Deprez157378f2022-04-04 15:47:50 +0200751 if (usable_blks_per_seg < sbi->blocks_per_seg)
752 f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
753 sbi->blocks_per_seg,
754 usable_blks_per_seg) != sbi->blocks_per_seg);
755
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000756 /* check segment usage, and check boundary of a given segment number */
Olivier Deprez157378f2022-04-04 15:47:50 +0200757 if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000758 || segno > TOTAL_SEGS(sbi) - 1)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000759 f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
760 GET_SIT_VBLOCKS(raw_sit), segno);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000761 set_sbi_flag(sbi, SBI_NEED_FSCK);
David Brazdil0f672f62019-12-10 10:32:29 +0000762 return -EFSCORRUPTED;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000763 }
764 return 0;
765}
766
767static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
768 unsigned int start)
769{
770 struct sit_info *sit_i = SIT_I(sbi);
771 unsigned int offset = SIT_BLOCK_OFFSET(start);
772 block_t blk_addr = sit_i->sit_base_addr + offset;
773
774 check_seg_range(sbi, start);
775
776#ifdef CONFIG_F2FS_CHECK_FS
777 if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
778 f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
779 f2fs_bug_on(sbi, 1);
780#endif
781
782 /* calculate sit block address */
783 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
784 blk_addr += sit_i->sit_blocks;
785
786 return blk_addr;
787}
788
789static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
790 pgoff_t block_addr)
791{
792 struct sit_info *sit_i = SIT_I(sbi);
793 block_addr -= sit_i->sit_base_addr;
794 if (block_addr < sit_i->sit_blocks)
795 block_addr += sit_i->sit_blocks;
796 else
797 block_addr -= sit_i->sit_blocks;
798
799 return block_addr + sit_i->sit_base_addr;
800}
801
802static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
803{
804 unsigned int block_off = SIT_BLOCK_OFFSET(start);
805
806 f2fs_change_bit(block_off, sit_i->sit_bitmap);
807#ifdef CONFIG_F2FS_CHECK_FS
808 f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
809#endif
810}
811
812static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
813 bool base_time)
814{
815 struct sit_info *sit_i = SIT_I(sbi);
Olivier Deprez157378f2022-04-04 15:47:50 +0200816 time64_t diff, now = ktime_get_boottime_seconds();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000817
818 if (now >= sit_i->mounted_time)
819 return sit_i->elapsed_time + now - sit_i->mounted_time;
820
821 /* system time is set to the past */
822 if (!base_time) {
823 diff = sit_i->mounted_time - now;
824 if (sit_i->elapsed_time >= diff)
825 return sit_i->elapsed_time - diff;
826 return 0;
827 }
828 return sit_i->elapsed_time;
829}
830
831static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
832 unsigned int ofs_in_node, unsigned char version)
833{
834 sum->nid = cpu_to_le32(nid);
835 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
836 sum->version = version;
837}
838
839static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
840{
841 return __start_cp_addr(sbi) +
842 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
843}
844
845static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
846{
847 return __start_cp_addr(sbi) +
848 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
849 - (base + 1) + type;
850}
851
852static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
853{
854 if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
855 return true;
856 return false;
857}
858
859/*
860 * It is very important to gather dirty pages and write at once, so that we can
861 * submit a big bio without interfering other data writes.
862 * By default, 512 pages for directory data,
863 * 512 pages (2MB) * 8 for nodes, and
864 * 256 pages * 8 for meta are set.
865 */
866static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
867{
868 if (sbi->sb->s_bdi->wb.dirty_exceeded)
869 return 0;
870
871 if (type == DATA)
872 return sbi->blocks_per_seg;
873 else if (type == NODE)
874 return 8 * sbi->blocks_per_seg;
875 else if (type == META)
876 return 8 * BIO_MAX_PAGES;
877 else
878 return 0;
879}
880
881/*
882 * When writing pages, it'd better align nr_to_write for segment size.
883 */
884static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
885 struct writeback_control *wbc)
886{
887 long nr_to_write, desired;
888
889 if (wbc->sync_mode != WB_SYNC_NONE)
890 return 0;
891
892 nr_to_write = wbc->nr_to_write;
893 desired = BIO_MAX_PAGES;
894 if (type == NODE)
895 desired <<= 1;
896
897 wbc->nr_to_write = desired;
898 return desired - nr_to_write;
899}
900
901static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
902{
903 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
904 bool wakeup = false;
905 int i;
906
907 if (force)
908 goto wake_up;
909
910 mutex_lock(&dcc->cmd_lock);
911 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
912 if (i + 1 < dcc->discard_granularity)
913 break;
914 if (!list_empty(&dcc->pend_list[i])) {
915 wakeup = true;
916 break;
917 }
918 }
919 mutex_unlock(&dcc->cmd_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000920 if (!wakeup || !is_idle(sbi, DISCARD_TIME))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000921 return;
922wake_up:
923 dcc->discard_wake = 1;
924 wake_up_interruptible_all(&dcc->discard_wait_queue);
925}