blob: 3bbeca13f70d4715faf8ac2974f82e043c7c6a35 [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
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 */
11#include <linux/blkdev.h>
12
13/* constant macro */
14#define NULL_SEGNO ((unsigned int)(~0))
15#define NULL_SECNO ((unsigned int)(~0))
16
Jaegeuk Kimf183b112014-03-19 14:17:21 +090017#define DEF_RECLAIM_PREFREE_SEGMENTS 5 /* 5% over total segments */
Linus Torvalds8005ecc2012-12-20 13:54:51 -080018
19/* L: Logical segment # in volume, R: Relative segment # in main area */
20#define GET_L2R_SEGNO(free_i, segno) (segno - free_i->start_segno)
21#define GET_R2L_SEGNO(free_i, segno) (segno + free_i->start_segno)
22
Changman Leeb1a94e82013-11-15 10:42:51 +090023#define IS_DATASEG(t) (t <= CURSEG_COLD_DATA)
24#define IS_NODESEG(t) (t >= CURSEG_HOT_NODE)
Linus Torvalds8005ecc2012-12-20 13:54:51 -080025
26#define IS_CURSEG(sbi, seg) \
27 ((seg == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) || \
28 (seg == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) || \
29 (seg == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) || \
30 (seg == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) || \
31 (seg == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) || \
32 (seg == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno))
33
34#define IS_CURSEC(sbi, secno) \
35 ((secno == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno / \
36 sbi->segs_per_sec) || \
37 (secno == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno / \
38 sbi->segs_per_sec) || \
39 (secno == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno / \
40 sbi->segs_per_sec) || \
41 (secno == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno / \
42 sbi->segs_per_sec) || \
43 (secno == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno / \
44 sbi->segs_per_sec) || \
45 (secno == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno / \
46 sbi->segs_per_sec)) \
47
Jaegeuk Kime0cea842015-02-18 20:43:11 -060048#define MAIN_BLKADDR(sbi) (SM_I(sbi)->main_blkaddr)
49#define SEG0_BLKADDR(sbi) (SM_I(sbi)->seg0_blkaddr)
50
51#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
52#define MAIN_SECS(sbi) (sbi->total_sections)
53
54#define TOTAL_SEGS(sbi) (SM_I(sbi)->segment_count)
55#define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
56
57#define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
58#define SEGMENT_SIZE(sbi) (1ULL << (sbi->log_blocksize + \
59 sbi->log_blocks_per_seg))
60
61#define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
Linus Torvalds8005ecc2012-12-20 13:54:51 -080062 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
Jaegeuk Kime0cea842015-02-18 20:43:11 -060063
Linus Torvalds8005ecc2012-12-20 13:54:51 -080064#define NEXT_FREE_BLKADDR(sbi, curseg) \
65 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
66
Jaegeuk Kime0cea842015-02-18 20:43:11 -060067#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
Linus Torvalds8005ecc2012-12-20 13:54:51 -080068#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
69 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
Jaegeuk Kim26d12822014-02-04 13:01:10 +090070#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
71 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
72
Linus Torvalds8005ecc2012-12-20 13:54:51 -080073#define GET_SEGNO(sbi, blk_addr) \
74 (((blk_addr == NULL_ADDR) || (blk_addr == NEW_ADDR)) ? \
75 NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi), \
76 GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
77#define GET_SECNO(sbi, segno) \
78 ((segno) / sbi->segs_per_sec)
79#define GET_ZONENO_FROM_SEGNO(sbi, segno) \
80 ((segno / sbi->segs_per_sec) / sbi->secs_per_zone)
81
82#define GET_SUM_BLOCK(sbi, segno) \
83 ((sbi->sm_info->ssa_blkaddr) + segno)
84
85#define GET_SUM_TYPE(footer) ((footer)->entry_type)
86#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
87
88#define SIT_ENTRY_OFFSET(sit_i, segno) \
89 (segno % sit_i->sents_per_block)
Jaegeuk Kime0cea842015-02-18 20:43:11 -060090#define SIT_BLOCK_OFFSET(segno) \
Linus Torvalds8005ecc2012-12-20 13:54:51 -080091 (segno / SIT_ENTRY_PER_BLOCK)
Jaegeuk Kime0cea842015-02-18 20:43:11 -060092#define START_SEGNO(segno) \
93 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
Changman Leeb1a94e82013-11-15 10:42:51 +090094#define SIT_BLK_CNT(sbi) \
Jaegeuk Kime0cea842015-02-18 20:43:11 -060095 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
Linus Torvalds8005ecc2012-12-20 13:54:51 -080096#define f2fs_bitmap_size(nr) \
97 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
Linus Torvalds8005ecc2012-12-20 13:54:51 -080098
Jaegeuk Kime0cea842015-02-18 20:43:11 -060099#define SECTOR_FROM_BLOCK(blk_addr) \
100 (((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
101#define SECTOR_TO_BLOCK(sectors) \
102 (sectors >> F2FS_LOG_SECTORS_PER_BLOCK)
103#define MAX_BIO_BLOCKS(sbi) \
104 ((int)min((int)max_hw_blocks(sbi), BIO_MAX_PAGES))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800105
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800106/*
107 * indicate a block allocation direction: RIGHT and LEFT.
108 * RIGHT means allocating new sections towards the end of volume.
109 * LEFT means the opposite direction.
110 */
111enum {
112 ALLOC_RIGHT = 0,
113 ALLOC_LEFT
114};
115
116/*
117 * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
118 * LFS writes data sequentially with cleaning operations.
119 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
120 */
121enum {
122 LFS = 0,
123 SSR
124};
125
126/*
127 * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
128 * GC_CB is based on cost-benefit algorithm.
129 * GC_GREEDY is based on greedy algorithm.
130 */
131enum {
132 GC_CB = 0,
133 GC_GREEDY
134};
135
136/*
137 * BG_GC means the background cleaning job.
138 * FG_GC means the on-demand cleaning job.
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600139 * FORCE_FG_GC means on-demand cleaning job in background.
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800140 */
141enum {
142 BG_GC = 0,
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600143 FG_GC,
144 FORCE_FG_GC,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800145};
146
147/* for a function parameter to select a victim segment */
148struct victim_sel_policy {
149 int alloc_mode; /* LFS or SSR */
150 int gc_mode; /* GC_CB or GC_GREEDY */
151 unsigned long *dirty_segmap; /* dirty segment bitmap */
152 unsigned int max_search; /* maximum # of segments to search */
153 unsigned int offset; /* last scanned bitmap offset */
154 unsigned int ofs_unit; /* bitmap search unit */
155 unsigned int min_cost; /* minimum cost */
156 unsigned int min_segno; /* segment # having min. cost */
157};
158
159struct seg_entry {
160 unsigned short valid_blocks; /* # of valid blocks */
161 unsigned char *cur_valid_map; /* validity bitmap of blocks */
162 /*
163 * # of valid blocks and the validity bitmap stored in the the last
164 * checkpoint pack. This information is used by the SSR mode.
165 */
166 unsigned short ckpt_valid_blocks;
167 unsigned char *ckpt_valid_map;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600168 unsigned char *discard_map;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800169 unsigned char type; /* segment type like CURSEG_XXX_TYPE */
170 unsigned long long mtime; /* modification time of the segment */
171};
172
173struct sec_entry {
174 unsigned int valid_blocks; /* # of valid blocks in a section */
175};
176
177struct segment_allocation {
178 void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
179};
180
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600181/*
182 * this value is set in page as a private data which indicate that
183 * the page is atomically written, and it is in inmem_pages list.
184 */
185#define ATOMIC_WRITTEN_PAGE 0x0000ffff
186
187#define IS_ATOMIC_WRITTEN_PAGE(page) \
188 (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
189
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600190struct inmem_pages {
191 struct list_head list;
192 struct page *page;
193};
194
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800195struct sit_info {
196 const struct segment_allocation *s_ops;
197
198 block_t sit_base_addr; /* start block address of SIT area */
199 block_t sit_blocks; /* # of blocks used by SIT area */
200 block_t written_valid_blocks; /* # of valid blocks in main area */
201 char *sit_bitmap; /* SIT bitmap pointer */
202 unsigned int bitmap_size; /* SIT bitmap size */
203
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600204 unsigned long *tmp_map; /* bitmap for temporal use */
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800205 unsigned long *dirty_sentries_bitmap; /* bitmap for dirty sentries */
206 unsigned int dirty_sentries; /* # of dirty sentries */
207 unsigned int sents_per_block; /* # of SIT entries per block */
208 struct mutex sentry_lock; /* to protect SIT cache */
209 struct seg_entry *sentries; /* SIT segment-level cache */
210 struct sec_entry *sec_entries; /* SIT section-level cache */
211
212 /* for cost-benefit algorithm in cleaning procedure */
213 unsigned long long elapsed_time; /* elapsed time after mount */
214 unsigned long long mounted_time; /* mount time */
215 unsigned long long min_mtime; /* min. modification time */
216 unsigned long long max_mtime; /* max. modification time */
217};
218
219struct free_segmap_info {
220 unsigned int start_segno; /* start segment number logically */
221 unsigned int free_segments; /* # of free segments */
222 unsigned int free_sections; /* # of free sections */
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600223 spinlock_t segmap_lock; /* free segmap lock */
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800224 unsigned long *free_segmap; /* free segment bitmap */
225 unsigned long *free_secmap; /* free section bitmap */
226};
227
228/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
229enum dirty_type {
230 DIRTY_HOT_DATA, /* dirty segments assigned as hot data logs */
231 DIRTY_WARM_DATA, /* dirty segments assigned as warm data logs */
232 DIRTY_COLD_DATA, /* dirty segments assigned as cold data logs */
233 DIRTY_HOT_NODE, /* dirty segments assigned as hot node logs */
234 DIRTY_WARM_NODE, /* dirty segments assigned as warm node logs */
235 DIRTY_COLD_NODE, /* dirty segments assigned as cold node logs */
236 DIRTY, /* to count # of dirty segments */
237 PRE, /* to count # of entirely obsolete segments */
238 NR_DIRTY_TYPE
239};
240
241struct dirty_seglist_info {
242 const struct victim_selection *v_ops; /* victim selction operation */
243 unsigned long *dirty_segmap[NR_DIRTY_TYPE];
244 struct mutex seglist_lock; /* lock for segment bitmaps */
245 int nr_dirty[NR_DIRTY_TYPE]; /* # of dirty segments */
246 unsigned long *victim_secmap; /* background GC victims */
247};
248
249/* victim selection function for cleaning and SSR */
250struct victim_selection {
251 int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
252 int, int, char);
253};
254
255/* for active log information */
256struct curseg_info {
257 struct mutex curseg_mutex; /* lock for consistency */
258 struct f2fs_summary_block *sum_blk; /* cached summary block */
259 unsigned char alloc_type; /* current allocation type */
260 unsigned int segno; /* current segment number */
261 unsigned short next_blkoff; /* next block offset to write */
262 unsigned int zone; /* current zone number */
263 unsigned int next_segno; /* preallocated segment */
264};
265
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600266struct sit_entry_set {
267 struct list_head set_list; /* link with all sit sets */
268 unsigned int start_segno; /* start segno of sits in set */
269 unsigned int entry_cnt; /* the # of sit entries in set */
270};
271
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800272/*
273 * inline functions
274 */
275static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
276{
277 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
278}
279
280static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
281 unsigned int segno)
282{
283 struct sit_info *sit_i = SIT_I(sbi);
284 return &sit_i->sentries[segno];
285}
286
287static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
288 unsigned int segno)
289{
290 struct sit_info *sit_i = SIT_I(sbi);
291 return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
292}
293
294static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
295 unsigned int segno, int section)
296{
297 /*
298 * In order to get # of valid blocks in a section instantly from many
299 * segments, f2fs manages two counting structures separately.
300 */
301 if (section > 1)
302 return get_sec_entry(sbi, segno)->valid_blocks;
303 else
304 return get_seg_entry(sbi, segno)->valid_blocks;
305}
306
307static inline void seg_info_from_raw_sit(struct seg_entry *se,
308 struct f2fs_sit_entry *rs)
309{
310 se->valid_blocks = GET_SIT_VBLOCKS(rs);
311 se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
312 memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
313 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
314 se->type = GET_SIT_TYPE(rs);
315 se->mtime = le64_to_cpu(rs->mtime);
316}
317
318static inline void seg_info_to_raw_sit(struct seg_entry *se,
319 struct f2fs_sit_entry *rs)
320{
321 unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
322 se->valid_blocks;
323 rs->vblocks = cpu_to_le16(raw_vblocks);
324 memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
325 memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
326 se->ckpt_valid_blocks = se->valid_blocks;
327 rs->mtime = cpu_to_le64(se->mtime);
328}
329
330static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
331 unsigned int max, unsigned int segno)
332{
333 unsigned int ret;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600334 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800335 ret = find_next_bit(free_i->free_segmap, max, segno);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600336 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800337 return ret;
338}
339
340static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
341{
342 struct free_segmap_info *free_i = FREE_I(sbi);
343 unsigned int secno = segno / sbi->segs_per_sec;
344 unsigned int start_segno = secno * sbi->segs_per_sec;
345 unsigned int next;
346
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600347 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800348 clear_bit(segno, free_i->free_segmap);
349 free_i->free_segments++;
350
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600351 next = find_next_bit(free_i->free_segmap,
352 start_segno + sbi->segs_per_sec, start_segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800353 if (next >= start_segno + sbi->segs_per_sec) {
354 clear_bit(secno, free_i->free_secmap);
355 free_i->free_sections++;
356 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600357 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800358}
359
360static inline void __set_inuse(struct f2fs_sb_info *sbi,
361 unsigned int segno)
362{
363 struct free_segmap_info *free_i = FREE_I(sbi);
364 unsigned int secno = segno / sbi->segs_per_sec;
365 set_bit(segno, free_i->free_segmap);
366 free_i->free_segments--;
367 if (!test_and_set_bit(secno, free_i->free_secmap))
368 free_i->free_sections--;
369}
370
371static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
372 unsigned int segno)
373{
374 struct free_segmap_info *free_i = FREE_I(sbi);
375 unsigned int secno = segno / sbi->segs_per_sec;
376 unsigned int start_segno = secno * sbi->segs_per_sec;
377 unsigned int next;
378
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600379 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800380 if (test_and_clear_bit(segno, free_i->free_segmap)) {
381 free_i->free_segments++;
382
Chao Yu51716f32014-07-14 16:45:15 +0800383 next = find_next_bit(free_i->free_segmap,
384 start_segno + sbi->segs_per_sec, start_segno);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800385 if (next >= start_segno + sbi->segs_per_sec) {
386 if (test_and_clear_bit(secno, free_i->free_secmap))
387 free_i->free_sections++;
388 }
389 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600390 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800391}
392
393static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
394 unsigned int segno)
395{
396 struct free_segmap_info *free_i = FREE_I(sbi);
397 unsigned int secno = segno / sbi->segs_per_sec;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600398 spin_lock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800399 if (!test_and_set_bit(segno, free_i->free_segmap)) {
400 free_i->free_segments--;
401 if (!test_and_set_bit(secno, free_i->free_secmap))
402 free_i->free_sections--;
403 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600404 spin_unlock(&free_i->segmap_lock);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800405}
406
407static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
408 void *dst_addr)
409{
410 struct sit_info *sit_i = SIT_I(sbi);
411 memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
412}
413
414static inline block_t written_block_count(struct f2fs_sb_info *sbi)
415{
Jaegeuk Kim0cf300a2014-02-24 13:00:13 +0900416 return SIT_I(sbi)->written_valid_blocks;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800417}
418
419static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
420{
Jaegeuk Kim0cf300a2014-02-24 13:00:13 +0900421 return FREE_I(sbi)->free_segments;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800422}
423
424static inline int reserved_segments(struct f2fs_sb_info *sbi)
425{
426 return SM_I(sbi)->reserved_segments;
427}
428
429static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
430{
Jaegeuk Kim0cf300a2014-02-24 13:00:13 +0900431 return FREE_I(sbi)->free_sections;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800432}
433
434static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
435{
436 return DIRTY_I(sbi)->nr_dirty[PRE];
437}
438
439static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
440{
441 return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
442 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
443 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
444 DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
445 DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
446 DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
447}
448
449static inline int overprovision_segments(struct f2fs_sb_info *sbi)
450{
451 return SM_I(sbi)->ovp_segments;
452}
453
454static inline int overprovision_sections(struct f2fs_sb_info *sbi)
455{
456 return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
457}
458
459static inline int reserved_sections(struct f2fs_sb_info *sbi)
460{
461 return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
462}
463
464static inline bool need_SSR(struct f2fs_sb_info *sbi)
465{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600466 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
467 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
468 return free_sections(sbi) <= (node_secs + 2 * dent_secs +
469 reserved_sections(sbi) + 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800470}
471
472static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi, int freed)
473{
474 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
475 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
476
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600477 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800478 return false;
479
Changman Leeb1a94e82013-11-15 10:42:51 +0900480 return (free_sections(sbi) + freed) <= (node_secs + 2 * dent_secs +
481 reserved_sections(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800482}
483
484static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
485{
Changman Leeb1a94e82013-11-15 10:42:51 +0900486 return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800487}
488
489static inline int utilization(struct f2fs_sb_info *sbi)
490{
Changman Leeb1a94e82013-11-15 10:42:51 +0900491 return div_u64((u64)valid_user_blocks(sbi) * 100,
492 sbi->user_block_count);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800493}
494
495/*
496 * Sometimes f2fs may be better to drop out-of-place update policy.
Changman Leeb1a94e82013-11-15 10:42:51 +0900497 * And, users can control the policy through sysfs entries.
498 * There are five policies with triggering conditions as follows.
499 * F2FS_IPU_FORCE - all the time,
500 * F2FS_IPU_SSR - if SSR mode is activated,
501 * F2FS_IPU_UTIL - if FS utilization is over threashold,
502 * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
503 * threashold,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600504 * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
505 * storages. IPU will be triggered only if the # of dirty
506 * pages over min_fsync_blocks.
Changman Leeb1a94e82013-11-15 10:42:51 +0900507 * F2FS_IPUT_DISABLE - disable IPU. (=default option)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800508 */
Changman Leeb1a94e82013-11-15 10:42:51 +0900509#define DEF_MIN_IPU_UTIL 70
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600510#define DEF_MIN_FSYNC_BLOCKS 8
Changman Leeb1a94e82013-11-15 10:42:51 +0900511
512enum {
513 F2FS_IPU_FORCE,
514 F2FS_IPU_SSR,
515 F2FS_IPU_UTIL,
516 F2FS_IPU_SSR_UTIL,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600517 F2FS_IPU_FSYNC,
Changman Leeb1a94e82013-11-15 10:42:51 +0900518};
519
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800520static inline bool need_inplace_update(struct inode *inode)
521{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600522 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
523 unsigned int policy = SM_I(sbi)->ipu_policy;
Changman Leeb1a94e82013-11-15 10:42:51 +0900524
525 /* IPU can be done only for the user data */
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600526 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800527 return false;
Changman Leeb1a94e82013-11-15 10:42:51 +0900528
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600529 if (policy & (0x1 << F2FS_IPU_FORCE))
530 return true;
531 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
532 return true;
533 if (policy & (0x1 << F2FS_IPU_UTIL) &&
534 utilization(sbi) > SM_I(sbi)->min_ipu_util)
535 return true;
536 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
537 utilization(sbi) > SM_I(sbi)->min_ipu_util)
Jaegeuk Kimf421ceb2014-07-24 19:11:43 -0700538 return true;
539
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600540 /* this is only set during fdatasync */
541 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
542 is_inode_flag_set(F2FS_I(inode), FI_NEED_IPU))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800543 return true;
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600544
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800545 return false;
546}
547
548static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
549 int type)
550{
551 struct curseg_info *curseg = CURSEG_I(sbi, type);
552 return curseg->segno;
553}
554
555static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
556 int type)
557{
558 struct curseg_info *curseg = CURSEG_I(sbi, type);
559 return curseg->alloc_type;
560}
561
562static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
563{
564 struct curseg_info *curseg = CURSEG_I(sbi, type);
565 return curseg->next_blkoff;
566}
567
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800568static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
569{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600570 f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800571}
572
573static inline void verify_block_addr(struct f2fs_sb_info *sbi, block_t blk_addr)
574{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600575 f2fs_bug_on(sbi, blk_addr < SEG0_BLKADDR(sbi)
576 || blk_addr >= MAX_BLKADDR(sbi));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800577}
578
579/*
arter97f4081402014-08-06 23:22:50 +0900580 * Summary block is always treated as an invalid block
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800581 */
582static inline void check_block_count(struct f2fs_sb_info *sbi,
583 int segno, struct f2fs_sit_entry *raw_sit)
584{
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600585#ifdef CONFIG_F2FS_CHECK_FS
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800586 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
587 int valid_blocks = 0;
588 int cur_pos = 0, next_pos;
589
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800590 /* check bitmap with valid block count */
591 do {
592 if (is_valid) {
593 next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
594 sbi->blocks_per_seg,
595 cur_pos);
596 valid_blocks += next_pos - cur_pos;
597 } else
598 next_pos = find_next_bit_le(&raw_sit->valid_map,
599 sbi->blocks_per_seg,
600 cur_pos);
601 cur_pos = next_pos;
602 is_valid = !is_valid;
603 } while (cur_pos < sbi->blocks_per_seg);
604 BUG_ON(GET_SIT_VBLOCKS(raw_sit) != valid_blocks);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800605#endif
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600606 /* check segment usage, and check boundary of a given segment number */
607 f2fs_bug_on(sbi, GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
608 || segno > TOTAL_SEGS(sbi) - 1);
609}
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800610
611static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
612 unsigned int start)
613{
614 struct sit_info *sit_i = SIT_I(sbi);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600615 unsigned int offset = SIT_BLOCK_OFFSET(start);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800616 block_t blk_addr = sit_i->sit_base_addr + offset;
617
618 check_seg_range(sbi, start);
619
620 /* calculate sit block address */
621 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
622 blk_addr += sit_i->sit_blocks;
623
624 return blk_addr;
625}
626
627static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
628 pgoff_t block_addr)
629{
630 struct sit_info *sit_i = SIT_I(sbi);
631 block_addr -= sit_i->sit_base_addr;
632 if (block_addr < sit_i->sit_blocks)
633 block_addr += sit_i->sit_blocks;
634 else
635 block_addr -= sit_i->sit_blocks;
636
637 return block_addr + sit_i->sit_base_addr;
638}
639
640static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
641{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600642 unsigned int block_off = SIT_BLOCK_OFFSET(start);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800643
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600644 f2fs_change_bit(block_off, sit_i->sit_bitmap);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800645}
646
647static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
648{
649 struct sit_info *sit_i = SIT_I(sbi);
650 return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
651 sit_i->mounted_time;
652}
653
654static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
655 unsigned int ofs_in_node, unsigned char version)
656{
657 sum->nid = cpu_to_le32(nid);
658 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
659 sum->version = version;
660}
661
662static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
663{
664 return __start_cp_addr(sbi) +
665 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
666}
667
668static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
669{
670 return __start_cp_addr(sbi) +
671 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
672 - (base + 1) + type;
673}
674
675static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
676{
677 if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
678 return true;
679 return false;
680}
681
682static inline unsigned int max_hw_blocks(struct f2fs_sb_info *sbi)
683{
684 struct block_device *bdev = sbi->sb->s_bdev;
685 struct request_queue *q = bdev_get_queue(bdev);
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600686 return SECTOR_TO_BLOCK(queue_max_sectors(q));
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800687}
Jaegeuk Kimc7f9f432014-03-18 12:40:49 +0900688
689/*
690 * It is very important to gather dirty pages and write at once, so that we can
691 * submit a big bio without interfering other data writes.
692 * By default, 512 pages for directory data,
693 * 512 pages (2MB) * 3 for three types of nodes, and
694 * max_bio_blocks for meta are set.
695 */
696static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
697{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600698 if (sbi->sb->s_bdi->dirty_exceeded)
699 return 0;
700
Jaegeuk Kimc7f9f432014-03-18 12:40:49 +0900701 if (type == DATA)
702 return sbi->blocks_per_seg;
703 else if (type == NODE)
704 return 3 * sbi->blocks_per_seg;
705 else if (type == META)
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600706 return MAX_BIO_BLOCKS(sbi);
Jaegeuk Kimc7f9f432014-03-18 12:40:49 +0900707 else
708 return 0;
709}
Jaegeuk Kim1f1eaf42014-03-18 13:47:11 +0900710
711/*
712 * When writing pages, it'd better align nr_to_write for segment size.
713 */
714static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
715 struct writeback_control *wbc)
716{
717 long nr_to_write, desired;
718
719 if (wbc->sync_mode != WB_SYNC_NONE)
720 return 0;
721
722 nr_to_write = wbc->nr_to_write;
723
724 if (type == DATA)
725 desired = 4096;
726 else if (type == NODE)
727 desired = 3 * max_hw_blocks(sbi);
728 else
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600729 desired = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim1f1eaf42014-03-18 13:47:11 +0900730
731 wbc->nr_to_write = desired;
732 return desired - nr_to_write;
733}