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