4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
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.
11 #include <linux/blkdev.h>
12 #include <linux/backing-dev.h>
15 #define NULL_SEGNO ((unsigned int)(~0))
16 #define NULL_SECNO ((unsigned int)(~0))
18 #define DEF_RECLAIM_PREFREE_SEGMENTS 5 /* 5% over total segments */
19 #define DEF_MAX_RECLAIM_PREFREE_SEGMENTS 4096 /* 8GB in maximum */
21 #define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
23 /* L: Logical segment # in volume, R: Relative segment # in main area */
24 #define GET_L2R_SEGNO(free_i, segno) (segno - free_i->start_segno)
25 #define GET_R2L_SEGNO(free_i, segno) (segno + free_i->start_segno)
27 #define IS_DATASEG(t) (t <= CURSEG_COLD_DATA)
28 #define IS_NODESEG(t) (t >= CURSEG_HOT_NODE)
30 #define IS_CURSEG(sbi, seg) \
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))
38 #define IS_CURSEC(sbi, secno) \
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 / \
52 #define MAIN_BLKADDR(sbi) \
53 (SM_I(sbi) ? SM_I(sbi)->main_blkaddr : \
54 le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
55 #define SEG0_BLKADDR(sbi) \
56 (SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : \
57 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
59 #define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
60 #define MAIN_SECS(sbi) (sbi->total_sections)
62 #define TOTAL_SEGS(sbi) \
63 (SM_I(sbi) ? SM_I(sbi)->segment_count : \
64 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
65 #define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
67 #define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
68 #define SEGMENT_SIZE(sbi) (1ULL << (sbi->log_blocksize + \
69 sbi->log_blocks_per_seg))
71 #define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
72 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
74 #define NEXT_FREE_BLKADDR(sbi, curseg) \
75 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
77 #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
78 #define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
79 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
80 #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
81 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
83 #define GET_SEGNO(sbi, blk_addr) \
84 ((!is_valid_data_blkaddr(sbi, blk_addr)) ? \
85 NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
86 GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
87 #define GET_SECNO(sbi, segno) \
88 ((segno) / sbi->segs_per_sec)
89 #define GET_ZONENO_FROM_SEGNO(sbi, segno) \
90 ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
92 #define GET_SUM_BLOCK(sbi, segno) \
93 ((sbi->sm_info->ssa_blkaddr) + segno)
95 #define GET_SUM_TYPE(footer) ((footer)->entry_type)
96 #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
98 #define SIT_ENTRY_OFFSET(sit_i, segno) \
99 (segno % sit_i->sents_per_block)
100 #define SIT_BLOCK_OFFSET(segno) \
101 (segno / SIT_ENTRY_PER_BLOCK)
102 #define START_SEGNO(segno) \
103 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
104 #define SIT_BLK_CNT(sbi) \
105 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
106 #define f2fs_bitmap_size(nr) \
107 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
109 #define SECTOR_FROM_BLOCK(blk_addr) \
110 (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
111 #define SECTOR_TO_BLOCK(sectors) \
112 (sectors >> F2FS_LOG_SECTORS_PER_BLOCK)
113 #define MAX_BIO_BLOCKS(sbi) \
114 ((int)min((int)max_hw_blocks(sbi), BIO_MAX_PAGES))
117 * indicate a block allocation direction: RIGHT and LEFT.
118 * RIGHT means allocating new sections towards the end of volume.
119 * LEFT means the opposite direction.
127 * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
128 * LFS writes data sequentially with cleaning operations.
129 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
137 * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
138 * GC_CB is based on cost-benefit algorithm.
139 * GC_GREEDY is based on greedy algorithm.
147 * BG_GC means the background cleaning job.
148 * FG_GC means the on-demand cleaning job.
149 * FORCE_FG_GC means on-demand cleaning job in background.
157 /* for a function parameter to select a victim segment */
158 struct victim_sel_policy
{
159 int alloc_mode
; /* LFS or SSR */
160 int gc_mode
; /* GC_CB or GC_GREEDY */
161 unsigned long *dirty_segmap
; /* dirty segment bitmap */
162 unsigned int max_search
; /* maximum # of segments to search */
163 unsigned int offset
; /* last scanned bitmap offset */
164 unsigned int ofs_unit
; /* bitmap search unit */
165 unsigned int min_cost
; /* minimum cost */
166 unsigned int min_segno
; /* segment # having min. cost */
170 unsigned int type
:6; /* segment type like CURSEG_XXX_TYPE */
171 unsigned int valid_blocks
:10; /* # of valid blocks */
172 unsigned int ckpt_valid_blocks
:10; /* # of valid blocks last cp */
173 unsigned int padding
:6; /* padding */
174 unsigned char *cur_valid_map
; /* validity bitmap of blocks */
176 * # of valid blocks and the validity bitmap stored in the the last
177 * checkpoint pack. This information is used by the SSR mode.
179 unsigned char *ckpt_valid_map
; /* validity bitmap of blocks last cp */
180 unsigned char *discard_map
;
181 unsigned long long mtime
; /* modification time of the segment */
185 unsigned int valid_blocks
; /* # of valid blocks in a section */
188 struct segment_allocation
{
189 void (*allocate_segment
)(struct f2fs_sb_info
*, int, bool);
193 * this value is set in page as a private data which indicate that
194 * the page is atomically written, and it is in inmem_pages list.
196 #define ATOMIC_WRITTEN_PAGE ((unsigned long)-1)
198 #define IS_ATOMIC_WRITTEN_PAGE(page) \
199 (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
202 struct list_head list
;
204 block_t old_addr
; /* for revoking when fail to commit */
208 const struct segment_allocation
*s_ops
;
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 */
214 unsigned int bitmap_size
; /* SIT bitmap size */
216 unsigned long *tmp_map
; /* bitmap for temporal use */
217 unsigned long *dirty_sentries_bitmap
; /* bitmap for dirty sentries */
218 unsigned int dirty_sentries
; /* # of dirty sentries */
219 unsigned int sents_per_block
; /* # of SIT entries per block */
220 struct mutex sentry_lock
; /* to protect SIT cache */
221 struct seg_entry
*sentries
; /* SIT segment-level cache */
222 struct sec_entry
*sec_entries
; /* SIT section-level cache */
224 /* for cost-benefit algorithm in cleaning procedure */
225 unsigned long long elapsed_time
; /* elapsed time after mount */
226 unsigned long long mounted_time
; /* mount time */
227 unsigned long long min_mtime
; /* min. modification time */
228 unsigned long long max_mtime
; /* max. modification time */
231 struct free_segmap_info
{
232 unsigned int start_segno
; /* start segment number logically */
233 unsigned int free_segments
; /* # of free segments */
234 unsigned int free_sections
; /* # of free sections */
235 spinlock_t segmap_lock
; /* free segmap lock */
236 unsigned long *free_segmap
; /* free segment bitmap */
237 unsigned long *free_secmap
; /* free section bitmap */
240 /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
242 DIRTY_HOT_DATA
, /* dirty segments assigned as hot data logs */
243 DIRTY_WARM_DATA
, /* dirty segments assigned as warm data logs */
244 DIRTY_COLD_DATA
, /* dirty segments assigned as cold data logs */
245 DIRTY_HOT_NODE
, /* dirty segments assigned as hot node logs */
246 DIRTY_WARM_NODE
, /* dirty segments assigned as warm node logs */
247 DIRTY_COLD_NODE
, /* dirty segments assigned as cold node logs */
248 DIRTY
, /* to count # of dirty segments */
249 PRE
, /* to count # of entirely obsolete segments */
253 struct dirty_seglist_info
{
254 const struct victim_selection
*v_ops
; /* victim selction operation */
255 unsigned long *dirty_segmap
[NR_DIRTY_TYPE
];
256 struct mutex seglist_lock
; /* lock for segment bitmaps */
257 int nr_dirty
[NR_DIRTY_TYPE
]; /* # of dirty segments */
258 unsigned long *victim_secmap
; /* background GC victims */
261 /* victim selection function for cleaning and SSR */
262 struct victim_selection
{
263 int (*get_victim
)(struct f2fs_sb_info
*, unsigned int *,
267 /* for active log information */
269 struct mutex curseg_mutex
; /* lock for consistency */
270 struct f2fs_summary_block
*sum_blk
; /* cached summary block */
271 struct rw_semaphore journal_rwsem
; /* protect journal area */
272 struct f2fs_journal
*journal
; /* cached journal info */
273 unsigned char alloc_type
; /* current allocation type */
274 unsigned int segno
; /* current segment number */
275 unsigned short next_blkoff
; /* next block offset to write */
276 unsigned int zone
; /* current zone number */
277 unsigned int next_segno
; /* preallocated segment */
280 struct sit_entry_set
{
281 struct list_head set_list
; /* link with all sit sets */
282 unsigned int start_segno
; /* start segno of sits in set */
283 unsigned int entry_cnt
; /* the # of sit entries in set */
289 static inline struct curseg_info
*CURSEG_I(struct f2fs_sb_info
*sbi
, int type
)
291 return (struct curseg_info
*)(SM_I(sbi
)->curseg_array
+ type
);
294 static inline struct seg_entry
*get_seg_entry(struct f2fs_sb_info
*sbi
,
297 struct sit_info
*sit_i
= SIT_I(sbi
);
298 return &sit_i
->sentries
[segno
];
301 static inline struct sec_entry
*get_sec_entry(struct f2fs_sb_info
*sbi
,
304 struct sit_info
*sit_i
= SIT_I(sbi
);
305 return &sit_i
->sec_entries
[GET_SECNO(sbi
, segno
)];
308 static inline unsigned int get_valid_blocks(struct f2fs_sb_info
*sbi
,
309 unsigned int segno
, int section
)
312 * In order to get # of valid blocks in a section instantly from many
313 * segments, f2fs manages two counting structures separately.
316 return get_sec_entry(sbi
, segno
)->valid_blocks
;
318 return get_seg_entry(sbi
, segno
)->valid_blocks
;
321 static inline void seg_info_from_raw_sit(struct seg_entry
*se
,
322 struct f2fs_sit_entry
*rs
)
324 se
->valid_blocks
= GET_SIT_VBLOCKS(rs
);
325 se
->ckpt_valid_blocks
= GET_SIT_VBLOCKS(rs
);
326 memcpy(se
->cur_valid_map
, rs
->valid_map
, SIT_VBLOCK_MAP_SIZE
);
327 memcpy(se
->ckpt_valid_map
, rs
->valid_map
, SIT_VBLOCK_MAP_SIZE
);
328 se
->type
= GET_SIT_TYPE(rs
);
329 se
->mtime
= le64_to_cpu(rs
->mtime
);
332 static inline void seg_info_to_raw_sit(struct seg_entry
*se
,
333 struct f2fs_sit_entry
*rs
)
335 unsigned short raw_vblocks
= (se
->type
<< SIT_VBLOCKS_SHIFT
) |
337 rs
->vblocks
= cpu_to_le16(raw_vblocks
);
338 memcpy(rs
->valid_map
, se
->cur_valid_map
, SIT_VBLOCK_MAP_SIZE
);
339 memcpy(se
->ckpt_valid_map
, rs
->valid_map
, SIT_VBLOCK_MAP_SIZE
);
340 se
->ckpt_valid_blocks
= se
->valid_blocks
;
341 rs
->mtime
= cpu_to_le64(se
->mtime
);
344 static inline unsigned int find_next_inuse(struct free_segmap_info
*free_i
,
345 unsigned int max
, unsigned int segno
)
348 spin_lock(&free_i
->segmap_lock
);
349 ret
= find_next_bit(free_i
->free_segmap
, max
, segno
);
350 spin_unlock(&free_i
->segmap_lock
);
354 static inline void __set_free(struct f2fs_sb_info
*sbi
, unsigned int segno
)
356 struct free_segmap_info
*free_i
= FREE_I(sbi
);
357 unsigned int secno
= segno
/ sbi
->segs_per_sec
;
358 unsigned int start_segno
= secno
* sbi
->segs_per_sec
;
361 spin_lock(&free_i
->segmap_lock
);
362 clear_bit(segno
, free_i
->free_segmap
);
363 free_i
->free_segments
++;
365 next
= find_next_bit(free_i
->free_segmap
,
366 start_segno
+ sbi
->segs_per_sec
, start_segno
);
367 if (next
>= start_segno
+ sbi
->segs_per_sec
) {
368 clear_bit(secno
, free_i
->free_secmap
);
369 free_i
->free_sections
++;
371 spin_unlock(&free_i
->segmap_lock
);
374 static inline void __set_inuse(struct f2fs_sb_info
*sbi
,
377 struct free_segmap_info
*free_i
= FREE_I(sbi
);
378 unsigned int secno
= segno
/ sbi
->segs_per_sec
;
379 set_bit(segno
, free_i
->free_segmap
);
380 free_i
->free_segments
--;
381 if (!test_and_set_bit(secno
, free_i
->free_secmap
))
382 free_i
->free_sections
--;
385 static inline void __set_test_and_free(struct f2fs_sb_info
*sbi
,
388 struct free_segmap_info
*free_i
= FREE_I(sbi
);
389 unsigned int secno
= segno
/ sbi
->segs_per_sec
;
390 unsigned int start_segno
= secno
* sbi
->segs_per_sec
;
393 spin_lock(&free_i
->segmap_lock
);
394 if (test_and_clear_bit(segno
, free_i
->free_segmap
)) {
395 free_i
->free_segments
++;
397 if (IS_CURSEC(sbi
, secno
))
399 next
= find_next_bit(free_i
->free_segmap
,
400 start_segno
+ sbi
->segs_per_sec
, start_segno
);
401 if (next
>= start_segno
+ sbi
->segs_per_sec
) {
402 if (test_and_clear_bit(secno
, free_i
->free_secmap
))
403 free_i
->free_sections
++;
407 spin_unlock(&free_i
->segmap_lock
);
410 static inline void __set_test_and_inuse(struct f2fs_sb_info
*sbi
,
413 struct free_segmap_info
*free_i
= FREE_I(sbi
);
414 unsigned int secno
= segno
/ sbi
->segs_per_sec
;
415 spin_lock(&free_i
->segmap_lock
);
416 if (!test_and_set_bit(segno
, free_i
->free_segmap
)) {
417 free_i
->free_segments
--;
418 if (!test_and_set_bit(secno
, free_i
->free_secmap
))
419 free_i
->free_sections
--;
421 spin_unlock(&free_i
->segmap_lock
);
424 static inline void get_sit_bitmap(struct f2fs_sb_info
*sbi
,
427 struct sit_info
*sit_i
= SIT_I(sbi
);
428 memcpy(dst_addr
, sit_i
->sit_bitmap
, sit_i
->bitmap_size
);
431 static inline block_t
written_block_count(struct f2fs_sb_info
*sbi
)
433 return SIT_I(sbi
)->written_valid_blocks
;
436 static inline unsigned int free_segments(struct f2fs_sb_info
*sbi
)
438 return FREE_I(sbi
)->free_segments
;
441 static inline int reserved_segments(struct f2fs_sb_info
*sbi
)
443 return SM_I(sbi
)->reserved_segments
;
446 static inline unsigned int free_sections(struct f2fs_sb_info
*sbi
)
448 return FREE_I(sbi
)->free_sections
;
451 static inline unsigned int prefree_segments(struct f2fs_sb_info
*sbi
)
453 return DIRTY_I(sbi
)->nr_dirty
[PRE
];
456 static inline unsigned int dirty_segments(struct f2fs_sb_info
*sbi
)
458 return DIRTY_I(sbi
)->nr_dirty
[DIRTY_HOT_DATA
] +
459 DIRTY_I(sbi
)->nr_dirty
[DIRTY_WARM_DATA
] +
460 DIRTY_I(sbi
)->nr_dirty
[DIRTY_COLD_DATA
] +
461 DIRTY_I(sbi
)->nr_dirty
[DIRTY_HOT_NODE
] +
462 DIRTY_I(sbi
)->nr_dirty
[DIRTY_WARM_NODE
] +
463 DIRTY_I(sbi
)->nr_dirty
[DIRTY_COLD_NODE
];
466 static inline int overprovision_segments(struct f2fs_sb_info
*sbi
)
468 return SM_I(sbi
)->ovp_segments
;
471 static inline int overprovision_sections(struct f2fs_sb_info
*sbi
)
473 return ((unsigned int) overprovision_segments(sbi
)) / sbi
->segs_per_sec
;
476 static inline int reserved_sections(struct f2fs_sb_info
*sbi
)
478 return ((unsigned int) reserved_segments(sbi
)) / sbi
->segs_per_sec
;
481 static inline bool need_SSR(struct f2fs_sb_info
*sbi
)
483 int node_secs
= get_blocktype_secs(sbi
, F2FS_DIRTY_NODES
);
484 int dent_secs
= get_blocktype_secs(sbi
, F2FS_DIRTY_DENTS
);
486 if (test_opt(sbi
, LFS
))
489 return free_sections(sbi
) <= (node_secs
+ 2 * dent_secs
+
490 reserved_sections(sbi
) + 1);
493 static inline bool has_not_enough_free_secs(struct f2fs_sb_info
*sbi
,
494 int freed
, int needed
)
496 int node_secs
= get_blocktype_secs(sbi
, F2FS_DIRTY_NODES
);
497 int dent_secs
= get_blocktype_secs(sbi
, F2FS_DIRTY_DENTS
);
499 node_secs
+= get_blocktype_secs(sbi
, F2FS_DIRTY_IMETA
);
501 if (unlikely(is_sbi_flag_set(sbi
, SBI_POR_DOING
)))
504 return (free_sections(sbi
) + freed
) <=
505 (node_secs
+ 2 * dent_secs
+ reserved_sections(sbi
) + needed
);
508 static inline bool excess_prefree_segs(struct f2fs_sb_info
*sbi
)
510 return prefree_segments(sbi
) > SM_I(sbi
)->rec_prefree_segments
;
513 static inline int utilization(struct f2fs_sb_info
*sbi
)
515 return div_u64((u64
)valid_user_blocks(sbi
) * 100,
516 sbi
->user_block_count
);
520 * Sometimes f2fs may be better to drop out-of-place update policy.
521 * And, users can control the policy through sysfs entries.
522 * There are five policies with triggering conditions as follows.
523 * F2FS_IPU_FORCE - all the time,
524 * F2FS_IPU_SSR - if SSR mode is activated,
525 * F2FS_IPU_UTIL - if FS utilization is over threashold,
526 * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
528 * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
529 * storages. IPU will be triggered only if the # of dirty
530 * pages over min_fsync_blocks.
531 * F2FS_IPUT_DISABLE - disable IPU. (=default option)
533 #define DEF_MIN_IPU_UTIL 70
534 #define DEF_MIN_FSYNC_BLOCKS 8
544 static inline bool need_inplace_update(struct inode
*inode
)
546 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
547 unsigned int policy
= SM_I(sbi
)->ipu_policy
;
549 /* IPU can be done only for the user data */
550 if (S_ISDIR(inode
->i_mode
) || f2fs_is_atomic_file(inode
))
553 if (test_opt(sbi
, LFS
))
556 if (policy
& (0x1 << F2FS_IPU_FORCE
))
558 if (policy
& (0x1 << F2FS_IPU_SSR
) && need_SSR(sbi
))
560 if (policy
& (0x1 << F2FS_IPU_UTIL
) &&
561 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
563 if (policy
& (0x1 << F2FS_IPU_SSR_UTIL
) && need_SSR(sbi
) &&
564 utilization(sbi
) > SM_I(sbi
)->min_ipu_util
)
567 /* this is only set during fdatasync */
568 if (policy
& (0x1 << F2FS_IPU_FSYNC
) &&
569 is_inode_flag_set(inode
, FI_NEED_IPU
))
575 static inline unsigned int curseg_segno(struct f2fs_sb_info
*sbi
,
578 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
579 return curseg
->segno
;
582 static inline unsigned char curseg_alloc_type(struct f2fs_sb_info
*sbi
,
585 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
586 return curseg
->alloc_type
;
589 static inline unsigned short curseg_blkoff(struct f2fs_sb_info
*sbi
, int type
)
591 struct curseg_info
*curseg
= CURSEG_I(sbi
, type
);
592 return curseg
->next_blkoff
;
595 static inline void check_seg_range(struct f2fs_sb_info
*sbi
, unsigned int segno
)
597 f2fs_bug_on(sbi
, segno
> TOTAL_SEGS(sbi
) - 1);
600 static inline void verify_block_addr(struct f2fs_io_info
*fio
, block_t blk_addr
)
602 struct f2fs_sb_info
*sbi
= fio
->sbi
;
604 if (__is_meta_io(fio
))
605 verify_blkaddr(sbi
, blk_addr
, META_GENERIC
);
607 verify_blkaddr(sbi
, blk_addr
, DATA_GENERIC
);
611 * Summary block is always treated as an invalid block
613 static inline int check_block_count(struct f2fs_sb_info
*sbi
,
614 int segno
, struct f2fs_sit_entry
*raw_sit
)
616 #ifdef CONFIG_F2FS_CHECK_FS
617 bool is_valid
= test_bit_le(0, raw_sit
->valid_map
) ? true : false;
618 int valid_blocks
= 0;
619 int cur_pos
= 0, next_pos
;
621 /* check bitmap with valid block count */
624 next_pos
= find_next_zero_bit_le(&raw_sit
->valid_map
,
627 valid_blocks
+= next_pos
- cur_pos
;
629 next_pos
= find_next_bit_le(&raw_sit
->valid_map
,
633 is_valid
= !is_valid
;
634 } while (cur_pos
< sbi
->blocks_per_seg
);
636 if (unlikely(GET_SIT_VBLOCKS(raw_sit
) != valid_blocks
)) {
637 f2fs_msg(sbi
->sb
, KERN_ERR
,
638 "Mismatch valid blocks %d vs. %d",
639 GET_SIT_VBLOCKS(raw_sit
), valid_blocks
);
640 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
644 /* check segment usage, and check boundary of a given segment number */
645 if (unlikely(GET_SIT_VBLOCKS(raw_sit
) > sbi
->blocks_per_seg
646 || segno
> TOTAL_SEGS(sbi
) - 1)) {
647 f2fs_msg(sbi
->sb
, KERN_ERR
,
648 "Wrong valid blocks %d or segno %u",
649 GET_SIT_VBLOCKS(raw_sit
), segno
);
650 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
656 static inline pgoff_t
current_sit_addr(struct f2fs_sb_info
*sbi
,
659 struct sit_info
*sit_i
= SIT_I(sbi
);
660 unsigned int offset
= SIT_BLOCK_OFFSET(start
);
661 block_t blk_addr
= sit_i
->sit_base_addr
+ offset
;
663 check_seg_range(sbi
, start
);
665 /* calculate sit block address */
666 if (f2fs_test_bit(offset
, sit_i
->sit_bitmap
))
667 blk_addr
+= sit_i
->sit_blocks
;
672 static inline pgoff_t
next_sit_addr(struct f2fs_sb_info
*sbi
,
675 struct sit_info
*sit_i
= SIT_I(sbi
);
676 block_addr
-= sit_i
->sit_base_addr
;
677 if (block_addr
< sit_i
->sit_blocks
)
678 block_addr
+= sit_i
->sit_blocks
;
680 block_addr
-= sit_i
->sit_blocks
;
682 return block_addr
+ sit_i
->sit_base_addr
;
685 static inline void set_to_next_sit(struct sit_info
*sit_i
, unsigned int start
)
687 unsigned int block_off
= SIT_BLOCK_OFFSET(start
);
689 f2fs_change_bit(block_off
, sit_i
->sit_bitmap
);
692 static inline unsigned long long get_mtime(struct f2fs_sb_info
*sbi
)
694 struct sit_info
*sit_i
= SIT_I(sbi
);
695 return sit_i
->elapsed_time
+ CURRENT_TIME_SEC
.tv_sec
-
699 static inline void set_summary(struct f2fs_summary
*sum
, nid_t nid
,
700 unsigned int ofs_in_node
, unsigned char version
)
702 sum
->nid
= cpu_to_le32(nid
);
703 sum
->ofs_in_node
= cpu_to_le16(ofs_in_node
);
704 sum
->version
= version
;
707 static inline block_t
start_sum_block(struct f2fs_sb_info
*sbi
)
709 return __start_cp_addr(sbi
) +
710 le32_to_cpu(F2FS_CKPT(sbi
)->cp_pack_start_sum
);
713 static inline block_t
sum_blk_addr(struct f2fs_sb_info
*sbi
, int base
, int type
)
715 return __start_cp_addr(sbi
) +
716 le32_to_cpu(F2FS_CKPT(sbi
)->cp_pack_total_block_count
)
720 static inline bool no_fggc_candidate(struct f2fs_sb_info
*sbi
,
723 if (get_valid_blocks(sbi
, secno
, sbi
->segs_per_sec
) >=
729 static inline bool sec_usage_check(struct f2fs_sb_info
*sbi
, unsigned int secno
)
731 if (IS_CURSEC(sbi
, secno
) || (sbi
->cur_victim_sec
== secno
))
736 static inline unsigned int max_hw_blocks(struct f2fs_sb_info
*sbi
)
738 struct block_device
*bdev
= sbi
->sb
->s_bdev
;
739 struct request_queue
*q
= bdev_get_queue(bdev
);
740 return SECTOR_TO_BLOCK(queue_max_sectors(q
));
744 * It is very important to gather dirty pages and write at once, so that we can
745 * submit a big bio without interfering other data writes.
746 * By default, 512 pages for directory data,
747 * 512 pages (2MB) * 3 for three types of nodes, and
748 * max_bio_blocks for meta are set.
750 static inline int nr_pages_to_skip(struct f2fs_sb_info
*sbi
, int type
)
752 if (sbi
->sb
->s_bdi
->wb
.dirty_exceeded
)
756 return sbi
->blocks_per_seg
;
757 else if (type
== NODE
)
758 return 8 * sbi
->blocks_per_seg
;
759 else if (type
== META
)
760 return 8 * MAX_BIO_BLOCKS(sbi
);
766 * When writing pages, it'd better align nr_to_write for segment size.
768 static inline long nr_pages_to_write(struct f2fs_sb_info
*sbi
, int type
,
769 struct writeback_control
*wbc
)
771 long nr_to_write
, desired
;
773 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
776 nr_to_write
= wbc
->nr_to_write
;
779 desired
= 2 * max_hw_blocks(sbi
);
781 desired
= MAX_BIO_BLOCKS(sbi
);
783 wbc
->nr_to_write
= desired
;
784 return desired
- nr_to_write
;