f2fs: clean up get_valid_blocks with consistent parameter
[linux-2.6-block.git] / fs / f2fs / segment.h
CommitLineData
0a8165d7 1/*
39a53e0c
JK
2 * fs/f2fs/segment.h
3 *
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
ac5d156c 11#include <linux/blkdev.h>
66114cad 12#include <linux/backing-dev.h>
ac5d156c 13
39a53e0c
JK
14/* constant macro */
15#define NULL_SEGNO ((unsigned int)(~0))
5ec4e49f 16#define NULL_SECNO ((unsigned int)(~0))
39a53e0c 17
58c41035 18#define DEF_RECLAIM_PREFREE_SEGMENTS 5 /* 5% over total segments */
44a83499 19#define DEF_MAX_RECLAIM_PREFREE_SEGMENTS 4096 /* 8GB in maximum */
81eb8d6e 20
2040fce8
JK
21#define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
22
6224da87 23/* L: Logical segment # in volume, R: Relative segment # in main area */
68afcf2d
TK
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)
39a53e0c 26
68afcf2d
TK
27#define IS_DATASEG(t) ((t) <= CURSEG_COLD_DATA)
28#define IS_NODESEG(t) ((t) >= CURSEG_HOT_NODE)
39a53e0c 29
5c773ba3 30#define IS_CURSEG(sbi, seg) \
68afcf2d
TK
31 (((seg) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) || \
32 ((seg) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
33 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
34 ((seg) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) || \
35 ((seg) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
36 ((seg) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
39a53e0c
JK
37
38#define IS_CURSEC(sbi, secno) \
68afcf2d
TK
39 (((secno) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / \
40 (sbi)->segs_per_sec) || \
41 ((secno) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / \
42 (sbi)->segs_per_sec) || \
43 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / \
44 (sbi)->segs_per_sec) || \
45 ((secno) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / \
46 (sbi)->segs_per_sec) || \
47 ((secno) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / \
48 (sbi)->segs_per_sec) || \
49 ((secno) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
50 (sbi)->segs_per_sec)) \
39a53e0c 51
7cd8558b
JK
52#define MAIN_BLKADDR(sbi) (SM_I(sbi)->main_blkaddr)
53#define SEG0_BLKADDR(sbi) (SM_I(sbi)->seg0_blkaddr)
54
55#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
68afcf2d 56#define MAIN_SECS(sbi) ((sbi)->total_sections)
7cd8558b
JK
57
58#define TOTAL_SEGS(sbi) (SM_I(sbi)->segment_count)
68afcf2d 59#define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
7cd8558b
JK
60
61#define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
68afcf2d
TK
62#define SEGMENT_SIZE(sbi) (1ULL << ((sbi)->log_blocksize + \
63 (sbi)->log_blocks_per_seg))
7cd8558b
JK
64
65#define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
68afcf2d 66 (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
7cd8558b 67
39a53e0c 68#define NEXT_FREE_BLKADDR(sbi, curseg) \
68afcf2d 69 (START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
39a53e0c 70
7cd8558b 71#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
39a53e0c 72#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
68afcf2d 73 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
491c0854 74#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
68afcf2d 75 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & ((sbi)->blocks_per_seg - 1))
491c0854 76
39a53e0c 77#define GET_SEGNO(sbi, blk_addr) \
68afcf2d 78 ((((blk_addr) == NULL_ADDR) || ((blk_addr) == NEW_ADDR)) ? \
39a53e0c
JK
79 NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
80 GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
81#define GET_SECNO(sbi, segno) \
68afcf2d 82 ((segno) / (sbi)->segs_per_sec)
63fcf8e8
JK
83#define GET_SEGNO_FROM_SECNO(sbi, secno) \
84 ((secno) * (sbi)->segs_per_sec)
39a53e0c 85#define GET_ZONENO_FROM_SEGNO(sbi, segno) \
68afcf2d 86 (((segno) / (sbi)->segs_per_sec) / (sbi)->secs_per_zone)
39a53e0c
JK
87
88#define GET_SUM_BLOCK(sbi, segno) \
68afcf2d 89 ((sbi)->sm_info->ssa_blkaddr + (segno))
39a53e0c
JK
90
91#define GET_SUM_TYPE(footer) ((footer)->entry_type)
68afcf2d 92#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
39a53e0c
JK
93
94#define SIT_ENTRY_OFFSET(sit_i, segno) \
68afcf2d 95 ((segno) % (sit_i)->sents_per_block)
d3a14afd 96#define SIT_BLOCK_OFFSET(segno) \
68afcf2d 97 ((segno) / SIT_ENTRY_PER_BLOCK)
d3a14afd
CY
98#define START_SEGNO(segno) \
99 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
74de593a 100#define SIT_BLK_CNT(sbi) \
7cd8558b 101 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
39a53e0c
JK
102#define f2fs_bitmap_size(nr) \
103 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
39a53e0c 104
55cf9cb6
CY
105#define SECTOR_FROM_BLOCK(blk_addr) \
106 (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
107#define SECTOR_TO_BLOCK(sectors) \
68afcf2d 108 ((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
3cd8a239 109
39a53e0c
JK
110/*
111 * indicate a block allocation direction: RIGHT and LEFT.
112 * RIGHT means allocating new sections towards the end of volume.
113 * LEFT means the opposite direction.
114 */
115enum {
116 ALLOC_RIGHT = 0,
117 ALLOC_LEFT
118};
119
120/*
121 * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
122 * LFS writes data sequentially with cleaning operations.
123 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
124 */
125enum {
126 LFS = 0,
127 SSR
128};
129
130/*
131 * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
132 * GC_CB is based on cost-benefit algorithm.
133 * GC_GREEDY is based on greedy algorithm.
134 */
135enum {
136 GC_CB = 0,
137 GC_GREEDY
138};
139
140/*
141 * BG_GC means the background cleaning job.
142 * FG_GC means the on-demand cleaning job.
6aefd93b 143 * FORCE_FG_GC means on-demand cleaning job in background.
39a53e0c
JK
144 */
145enum {
146 BG_GC = 0,
6aefd93b
JK
147 FG_GC,
148 FORCE_FG_GC,
39a53e0c
JK
149};
150
151/* for a function parameter to select a victim segment */
152struct victim_sel_policy {
153 int alloc_mode; /* LFS or SSR */
154 int gc_mode; /* GC_CB or GC_GREEDY */
155 unsigned long *dirty_segmap; /* dirty segment bitmap */
a26b7c8a 156 unsigned int max_search; /* maximum # of segments to search */
39a53e0c
JK
157 unsigned int offset; /* last scanned bitmap offset */
158 unsigned int ofs_unit; /* bitmap search unit */
159 unsigned int min_cost; /* minimum cost */
160 unsigned int min_segno; /* segment # having min. cost */
161};
162
163struct seg_entry {
f51b4ce6
CY
164 unsigned int type:6; /* segment type like CURSEG_XXX_TYPE */
165 unsigned int valid_blocks:10; /* # of valid blocks */
166 unsigned int ckpt_valid_blocks:10; /* # of valid blocks last cp */
167 unsigned int padding:6; /* padding */
39a53e0c 168 unsigned char *cur_valid_map; /* validity bitmap of blocks */
355e7891
CY
169#ifdef CONFIG_F2FS_CHECK_FS
170 unsigned char *cur_valid_map_mir; /* mirror of current valid bitmap */
171#endif
39a53e0c
JK
172 /*
173 * # of valid blocks and the validity bitmap stored in the the last
174 * checkpoint pack. This information is used by the SSR mode.
175 */
f51b4ce6 176 unsigned char *ckpt_valid_map; /* validity bitmap of blocks last cp */
a66cdd98 177 unsigned char *discard_map;
39a53e0c
JK
178 unsigned long long mtime; /* modification time of the segment */
179};
180
181struct sec_entry {
182 unsigned int valid_blocks; /* # of valid blocks in a section */
183};
184
185struct segment_allocation {
186 void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
187};
188
decd36b6
CY
189/*
190 * this value is set in page as a private data which indicate that
191 * the page is atomically written, and it is in inmem_pages list.
192 */
d48dfc20 193#define ATOMIC_WRITTEN_PAGE ((unsigned long)-1)
0a595eba 194#define DUMMY_WRITTEN_PAGE ((unsigned long)-2)
decd36b6
CY
195
196#define IS_ATOMIC_WRITTEN_PAGE(page) \
197 (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
0a595eba
JK
198#define IS_DUMMY_WRITTEN_PAGE(page) \
199 (page_private(page) == (unsigned long)DUMMY_WRITTEN_PAGE)
decd36b6 200
88b88a66
JK
201struct inmem_pages {
202 struct list_head list;
203 struct page *page;
28bc106b 204 block_t old_addr; /* for revoking when fail to commit */
88b88a66
JK
205};
206
39a53e0c
JK
207struct sit_info {
208 const struct segment_allocation *s_ops;
209
210 block_t sit_base_addr; /* start block address of SIT area */
211 block_t sit_blocks; /* # of blocks used by SIT area */
212 block_t written_valid_blocks; /* # of valid blocks in main area */
213 char *sit_bitmap; /* SIT bitmap pointer */
ae27d62e
CY
214#ifdef CONFIG_F2FS_CHECK_FS
215 char *sit_bitmap_mir; /* SIT bitmap mirror */
216#endif
39a53e0c
JK
217 unsigned int bitmap_size; /* SIT bitmap size */
218
60a3b782 219 unsigned long *tmp_map; /* bitmap for temporal use */
39a53e0c
JK
220 unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
221 unsigned int dirty_sentries; /* # of dirty sentries */
222 unsigned int sents_per_block; /* # of SIT entries per block */
223 struct mutex sentry_lock; /* to protect SIT cache */
224 struct seg_entry *sentries; /* SIT segment-level cache */
225 struct sec_entry *sec_entries; /* SIT section-level cache */
226
227 /* for cost-benefit algorithm in cleaning procedure */
228 unsigned long long elapsed_time; /* elapsed time after mount */
229 unsigned long long mounted_time; /* mount time */
230 unsigned long long min_mtime; /* min. modification time */
231 unsigned long long max_mtime; /* max. modification time */
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 */
1a118ccf 238 spinlock_t segmap_lock; /* free segmap lock */
39a53e0c
JK
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 const struct victim_selection *v_ops; /* victim selction operation */
258 unsigned long *dirty_segmap[NR_DIRTY_TYPE];
259 struct mutex seglist_lock; /* lock for segment bitmaps */
260 int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
5ec4e49f 261 unsigned long *victim_secmap; /* background GC victims */
39a53e0c
JK
262};
263
264/* victim selection function for cleaning and SSR */
265struct victim_selection {
266 int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
267 int, int, char);
268};
269
270/* for active log information */
271struct curseg_info {
272 struct mutex curseg_mutex; /* lock for consistency */
273 struct f2fs_summary_block *sum_blk; /* cached summary block */
b7ad7512
CY
274 struct rw_semaphore journal_rwsem; /* protect journal area */
275 struct f2fs_journal *journal; /* cached journal info */
39a53e0c
JK
276 unsigned char alloc_type; /* current allocation type */
277 unsigned int segno; /* current segment number */
278 unsigned short next_blkoff; /* next block offset to write */
279 unsigned int zone; /* current zone number */
280 unsigned int next_segno; /* preallocated segment */
281};
282
184a5cd2
CY
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
39a53e0c
JK
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 struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
298 unsigned int segno)
299{
300 struct sit_info *sit_i = SIT_I(sbi);
301 return &sit_i->sentries[segno];
302}
303
304static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
305 unsigned int segno)
306{
307 struct sit_info *sit_i = SIT_I(sbi);
308 return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
309}
310
311static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
302bd348 312 unsigned int segno, bool use_section)
39a53e0c
JK
313{
314 /*
315 * In order to get # of valid blocks in a section instantly from many
316 * segments, f2fs manages two counting structures separately.
317 */
302bd348 318 if (use_section && sbi->segs_per_sec > 1)
39a53e0c
JK
319 return get_sec_entry(sbi, segno)->valid_blocks;
320 else
321 return get_seg_entry(sbi, segno)->valid_blocks;
322}
323
324static inline void seg_info_from_raw_sit(struct seg_entry *se,
325 struct f2fs_sit_entry *rs)
326{
327 se->valid_blocks = GET_SIT_VBLOCKS(rs);
328 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
329 memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
330 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
355e7891
CY
331#ifdef CONFIG_F2FS_CHECK_FS
332 memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
333#endif
39a53e0c
JK
334 se->type = GET_SIT_TYPE(rs);
335 se->mtime = le64_to_cpu(rs->mtime);
336}
337
338static inline void seg_info_to_raw_sit(struct seg_entry *se,
339 struct f2fs_sit_entry *rs)
340{
341 unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
342 se->valid_blocks;
343 rs->vblocks = cpu_to_le16(raw_vblocks);
344 memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
345 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
346 se->ckpt_valid_blocks = se->valid_blocks;
347 rs->mtime = cpu_to_le64(se->mtime);
348}
349
350static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
351 unsigned int max, unsigned int segno)
352{
353 unsigned int ret;
1a118ccf 354 spin_lock(&free_i->segmap_lock);
39a53e0c 355 ret = find_next_bit(free_i->free_segmap, max, segno);
1a118ccf 356 spin_unlock(&free_i->segmap_lock);
39a53e0c
JK
357 return ret;
358}
359
360static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
361{
362 struct free_segmap_info *free_i = FREE_I(sbi);
363 unsigned int secno = segno / sbi->segs_per_sec;
364 unsigned int start_segno = secno * sbi->segs_per_sec;
365 unsigned int next;
366
1a118ccf 367 spin_lock(&free_i->segmap_lock);
39a53e0c
JK
368 clear_bit(segno, free_i->free_segmap);
369 free_i->free_segments++;
370
7fd97019
WL
371 next = find_next_bit(free_i->free_segmap,
372 start_segno + sbi->segs_per_sec, start_segno);
39a53e0c
JK
373 if (next >= start_segno + sbi->segs_per_sec) {
374 clear_bit(secno, free_i->free_secmap);
375 free_i->free_sections++;
376 }
1a118ccf 377 spin_unlock(&free_i->segmap_lock);
39a53e0c
JK
378}
379
380static inline void __set_inuse(struct f2fs_sb_info *sbi,
381 unsigned int segno)
382{
383 struct free_segmap_info *free_i = FREE_I(sbi);
384 unsigned int secno = segno / sbi->segs_per_sec;
385 set_bit(segno, free_i->free_segmap);
386 free_i->free_segments--;
387 if (!test_and_set_bit(secno, free_i->free_secmap))
388 free_i->free_sections--;
389}
390
391static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
392 unsigned int segno)
393{
394 struct free_segmap_info *free_i = FREE_I(sbi);
395 unsigned int secno = segno / sbi->segs_per_sec;
396 unsigned int start_segno = secno * sbi->segs_per_sec;
397 unsigned int next;
398
1a118ccf 399 spin_lock(&free_i->segmap_lock);
39a53e0c
JK
400 if (test_and_clear_bit(segno, free_i->free_segmap)) {
401 free_i->free_segments++;
402
f1121ab0
CY
403 next = find_next_bit(free_i->free_segmap,
404 start_segno + sbi->segs_per_sec, start_segno);
39a53e0c
JK
405 if (next >= start_segno + sbi->segs_per_sec) {
406 if (test_and_clear_bit(secno, free_i->free_secmap))
407 free_i->free_sections++;
408 }
409 }
1a118ccf 410 spin_unlock(&free_i->segmap_lock);
39a53e0c
JK
411}
412
413static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
414 unsigned int segno)
415{
416 struct free_segmap_info *free_i = FREE_I(sbi);
417 unsigned int secno = segno / sbi->segs_per_sec;
1a118ccf 418 spin_lock(&free_i->segmap_lock);
39a53e0c
JK
419 if (!test_and_set_bit(segno, free_i->free_segmap)) {
420 free_i->free_segments--;
421 if (!test_and_set_bit(secno, free_i->free_secmap))
422 free_i->free_sections--;
423 }
1a118ccf 424 spin_unlock(&free_i->segmap_lock);
39a53e0c
JK
425}
426
427static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
428 void *dst_addr)
429{
430 struct sit_info *sit_i = SIT_I(sbi);
ae27d62e
CY
431
432#ifdef CONFIG_F2FS_CHECK_FS
433 if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
434 sit_i->bitmap_size))
435 f2fs_bug_on(sbi, 1);
436#endif
39a53e0c
JK
437 memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
438}
439
440static inline block_t written_block_count(struct f2fs_sb_info *sbi)
441{
8b8343fa 442 return SIT_I(sbi)->written_valid_blocks;
39a53e0c
JK
443}
444
445static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
446{
8b8343fa 447 return FREE_I(sbi)->free_segments;
39a53e0c
JK
448}
449
450static inline int reserved_segments(struct f2fs_sb_info *sbi)
451{
452 return SM_I(sbi)->reserved_segments;
453}
454
455static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
456{
8b8343fa 457 return FREE_I(sbi)->free_sections;
39a53e0c
JK
458}
459
460static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
461{
462 return DIRTY_I(sbi)->nr_dirty[PRE];
463}
464
465static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
466{
467 return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
468 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
469 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
470 DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
471 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
472 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
473}
474
475static inline int overprovision_segments(struct f2fs_sb_info *sbi)
476{
477 return SM_I(sbi)->ovp_segments;
478}
479
480static inline int overprovision_sections(struct f2fs_sb_info *sbi)
481{
482 return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
483}
484
485static inline int reserved_sections(struct f2fs_sb_info *sbi)
486{
487 return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
488}
489
490static inline bool need_SSR(struct f2fs_sb_info *sbi)
491{
95dd8973
JK
492 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
493 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
b9610bdf 494 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
36abef4e
JK
495
496 if (test_opt(sbi, LFS))
497 return false;
498
b9610bdf 499 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
796dbbfe 500 2 * reserved_sections(sbi));
39a53e0c
JK
501}
502
7f3037a5
JK
503static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
504 int freed, int needed)
39a53e0c 505{
5ac206cf
NJ
506 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
507 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
b9610bdf 508 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
0f18b462 509
caf0047e 510 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
029cd28c
JK
511 return false;
512
7f3037a5 513 return (free_sections(sbi) + freed) <=
b9610bdf
JK
514 (node_secs + 2 * dent_secs + imeta_secs +
515 reserved_sections(sbi) + needed);
39a53e0c
JK
516}
517
81eb8d6e
JK
518static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
519{
6c311ec6 520 return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
81eb8d6e
JK
521}
522
39a53e0c
JK
523static inline int utilization(struct f2fs_sb_info *sbi)
524{
6c311ec6
CF
525 return div_u64((u64)valid_user_blocks(sbi) * 100,
526 sbi->user_block_count);
39a53e0c
JK
527}
528
529/*
530 * Sometimes f2fs may be better to drop out-of-place update policy.
216fbd64
JK
531 * And, users can control the policy through sysfs entries.
532 * There are five policies with triggering conditions as follows.
533 * F2FS_IPU_FORCE - all the time,
534 * F2FS_IPU_SSR - if SSR mode is activated,
535 * F2FS_IPU_UTIL - if FS utilization is over threashold,
536 * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
537 * threashold,
c1ce1b02
JK
538 * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
539 * storages. IPU will be triggered only if the # of dirty
540 * pages over min_fsync_blocks.
216fbd64 541 * F2FS_IPUT_DISABLE - disable IPU. (=default option)
39a53e0c 542 */
216fbd64 543#define DEF_MIN_IPU_UTIL 70
c1ce1b02 544#define DEF_MIN_FSYNC_BLOCKS 8
ef095d19 545#define DEF_MIN_HOT_BLOCKS 16
216fbd64
JK
546
547enum {
548 F2FS_IPU_FORCE,
549 F2FS_IPU_SSR,
550 F2FS_IPU_UTIL,
551 F2FS_IPU_SSR_UTIL,
c1ce1b02 552 F2FS_IPU_FSYNC,
216fbd64
JK
553};
554
39a53e0c
JK
555static inline bool need_inplace_update(struct inode *inode)
556{
4081363f 557 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
9b5f136f 558 unsigned int policy = SM_I(sbi)->ipu_policy;
216fbd64
JK
559
560 /* IPU can be done only for the user data */
88b88a66 561 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
39a53e0c 562 return false;
216fbd64 563
36abef4e
JK
564 if (test_opt(sbi, LFS))
565 return false;
566
9b5f136f 567 if (policy & (0x1 << F2FS_IPU_FORCE))
39a53e0c 568 return true;
9b5f136f
JK
569 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
570 return true;
571 if (policy & (0x1 << F2FS_IPU_UTIL) &&
572 utilization(sbi) > SM_I(sbi)->min_ipu_util)
573 return true;
574 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
575 utilization(sbi) > SM_I(sbi)->min_ipu_util)
576 return true;
577
578 /* this is only set during fdatasync */
579 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
91942321 580 is_inode_flag_set(inode, FI_NEED_IPU))
9b5f136f
JK
581 return true;
582
39a53e0c
JK
583 return false;
584}
585
586static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
587 int type)
588{
589 struct curseg_info *curseg = CURSEG_I(sbi, type);
590 return curseg->segno;
591}
592
593static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
594 int type)
595{
596 struct curseg_info *curseg = CURSEG_I(sbi, type);
597 return curseg->alloc_type;
598}
599
600static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
601{
602 struct curseg_info *curseg = CURSEG_I(sbi, type);
603 return curseg->next_blkoff;
604}
605
606static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
607{
7a04f64d 608 f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
39a53e0c
JK
609}
610
39a53e0c
JK
611static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
612{
bb413d6a
YH
613 BUG_ON(blk_addr < SEG0_BLKADDR(sbi)
614 || blk_addr >= MAX_BLKADDR(sbi));
39a53e0c
JK
615}
616
617/*
e1c42045 618 * Summary block is always treated as an invalid block
39a53e0c
JK
619 */
620static inline void check_block_count(struct f2fs_sb_info *sbi,
621 int segno, struct f2fs_sit_entry *raw_sit)
622{
4c278394 623#ifdef CONFIG_F2FS_CHECK_FS
44c60bf2 624 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
39a53e0c 625 int valid_blocks = 0;
44c60bf2 626 int cur_pos = 0, next_pos;
39a53e0c 627
39a53e0c 628 /* check bitmap with valid block count */
44c60bf2
CY
629 do {
630 if (is_valid) {
631 next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
632 sbi->blocks_per_seg,
633 cur_pos);
634 valid_blocks += next_pos - cur_pos;
635 } else
636 next_pos = find_next_bit_le(&raw_sit->valid_map,
637 sbi->blocks_per_seg,
638 cur_pos);
639 cur_pos = next_pos;
640 is_valid = !is_valid;
641 } while (cur_pos < sbi->blocks_per_seg);
39a53e0c 642 BUG_ON(GET_SIT_VBLOCKS(raw_sit) != valid_blocks);
5d56b671 643#endif
4c278394
JK
644 /* check segment usage, and check boundary of a given segment number */
645 f2fs_bug_on(sbi, GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
646 || segno > TOTAL_SEGS(sbi) - 1);
7a04f64d 647}
39a53e0c
JK
648
649static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
650 unsigned int start)
651{
652 struct sit_info *sit_i = SIT_I(sbi);
d3a14afd 653 unsigned int offset = SIT_BLOCK_OFFSET(start);
39a53e0c
JK
654 block_t blk_addr = sit_i->sit_base_addr + offset;
655
656 check_seg_range(sbi, start);
657
ae27d62e
CY
658#ifdef CONFIG_F2FS_CHECK_FS
659 if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
660 f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
661 f2fs_bug_on(sbi, 1);
662#endif
663
39a53e0c
JK
664 /* calculate sit block address */
665 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
666 blk_addr += sit_i->sit_blocks;
667
668 return blk_addr;
669}
670
671static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
672 pgoff_t block_addr)
673{
674 struct sit_info *sit_i = SIT_I(sbi);
675 block_addr -= sit_i->sit_base_addr;
676 if (block_addr < sit_i->sit_blocks)
677 block_addr += sit_i->sit_blocks;
678 else
679 block_addr -= sit_i->sit_blocks;
680
681 return block_addr + sit_i->sit_base_addr;
682}
683
684static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
685{
d3a14afd 686 unsigned int block_off = SIT_BLOCK_OFFSET(start);
39a53e0c 687
c6ac4c0e 688 f2fs_change_bit(block_off, sit_i->sit_bitmap);
ae27d62e
CY
689#ifdef CONFIG_F2FS_CHECK_FS
690 f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
691#endif
39a53e0c
JK
692}
693
694static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
695{
696 struct sit_info *sit_i = SIT_I(sbi);
697 return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
698 sit_i->mounted_time;
699}
700
701static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
702 unsigned int ofs_in_node, unsigned char version)
703{
704 sum->nid = cpu_to_le32(nid);
705 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
706 sum->version = version;
707}
708
709static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
710{
711 return __start_cp_addr(sbi) +
712 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
713}
714
715static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
716{
717 return __start_cp_addr(sbi) +
718 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
719 - (base + 1) + type;
720}
5ec4e49f 721
e93b9865
HP
722static inline bool no_fggc_candidate(struct f2fs_sb_info *sbi,
723 unsigned int secno)
724{
302bd348
JK
725 if (get_valid_blocks(sbi, GET_SEGNO_FROM_SECNO(sbi, secno), true) >=
726 sbi->fggc_threshold)
e93b9865
HP
727 return true;
728 return false;
729}
730
5ec4e49f
JK
731static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
732{
733 if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
734 return true;
735 return false;
736}
ac5d156c 737
87d6f890
JK
738/*
739 * It is very important to gather dirty pages and write at once, so that we can
740 * submit a big bio without interfering other data writes.
741 * By default, 512 pages for directory data,
727ebb09
KM
742 * 512 pages (2MB) * 8 for nodes, and
743 * 256 pages * 8 for meta are set.
87d6f890
JK
744 */
745static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
746{
a88a341a 747 if (sbi->sb->s_bdi->wb.dirty_exceeded)
510184c8
JK
748 return 0;
749
a1257023
JK
750 if (type == DATA)
751 return sbi->blocks_per_seg;
752 else if (type == NODE)
2c237eba 753 return 8 * sbi->blocks_per_seg;
87d6f890 754 else if (type == META)
664ba972 755 return 8 * BIO_MAX_PAGES;
87d6f890
JK
756 else
757 return 0;
758}
50c8cdb3
JK
759
760/*
761 * When writing pages, it'd better align nr_to_write for segment size.
762 */
763static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
764 struct writeback_control *wbc)
765{
766 long nr_to_write, desired;
767
768 if (wbc->sync_mode != WB_SYNC_NONE)
769 return 0;
770
771 nr_to_write = wbc->nr_to_write;
664ba972 772 desired = BIO_MAX_PAGES;
28ea6162 773 if (type == NODE)
664ba972 774 desired <<= 1;
50c8cdb3
JK
775
776 wbc->nr_to_write = desired;
777 return desired - nr_to_write;
778}