Blame view

fs/f2fs/segment.h 27.4 KB
d29fbcdb0   Nishad Kamdar   f2fs: Use the cor...
1
  /* SPDX-License-Identifier: GPL-2.0 */
0a8165d7c   Jaegeuk Kim   f2fs: adjust kern...
2
  /*
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
3
4
5
6
   * fs/f2fs/segment.h
   *
   * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   *             http://www.samsung.com/
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
7
   */
ac5d156c7   Jaegeuk Kim   f2fs: modify the ...
8
  #include <linux/blkdev.h>
66114cad6   Tejun Heo   writeback: separa...
9
  #include <linux/backing-dev.h>
ac5d156c7   Jaegeuk Kim   f2fs: modify the ...
10

39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
11
12
  /* constant macro */
  #define NULL_SEGNO			((unsigned int)(~0))
5ec4e49f9   Jaegeuk Kim   f2fs: change GC b...
13
  #define NULL_SECNO			((unsigned int)(~0))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
14

58c410351   Jaegeuk Kim   f2fs: change recl...
15
  #define DEF_RECLAIM_PREFREE_SEGMENTS	5	/* 5% over total segments */
44a83499d   Jaegeuk Kim   f2fs: add maximum...
16
  #define DEF_MAX_RECLAIM_PREFREE_SEGMENTS	4096	/* 8GB in maximum */
81eb8d6e2   Jaegeuk Kim   f2fs: reclaim pre...
17

2040fce83   Jaegeuk Kim   f2fs: detect wron...
18
  #define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
f99ba9add   Wang Xiaojun   f2fs: fix wrong t...
19
  #define F2FS_MIN_META_SEGMENTS	8 /* SB + 2 (CP + SIT + NAT) + SSA */
2040fce83   Jaegeuk Kim   f2fs: detect wron...
20

6224da875   Namjae Jeon   f2fs: fix typo mi...
21
  /* L: Logical segment # in volume, R: Relative segment # in main area */
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
22
23
  #define GET_L2R_SEGNO(free_i, segno)	((segno) - (free_i)->start_segno)
  #define GET_R2L_SEGNO(free_i, segno)	((segno) + (free_i)->start_segno)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
24

68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
25
  #define IS_DATASEG(t)	((t) <= CURSEG_COLD_DATA)
d0b9e42ab   Chao Yu   f2fs: introduce i...
26
  #define IS_NODESEG(t)	((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
27

093749e29   Chao Yu   f2fs: support age...
28
29
30
31
32
  static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
  						unsigned short seg_type)
  {
  	f2fs_bug_on(sbi, seg_type >= NR_PERSISTENT_LOG);
  }
a912b54d3   Jaegeuk Kim   f2fs: split bio c...
33
34
35
  #define IS_HOT(t)	((t) == CURSEG_HOT_NODE || (t) == CURSEG_HOT_DATA)
  #define IS_WARM(t)	((t) == CURSEG_WARM_NODE || (t) == CURSEG_WARM_DATA)
  #define IS_COLD(t)	((t) == CURSEG_COLD_NODE || (t) == CURSEG_COLD_DATA)
5c773ba33   Jaegeuk Kim   f2fs: do not use ...
36
  #define IS_CURSEG(sbi, seg)						\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
37
38
39
40
41
  	(((seg) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno) ||	\
  	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno) ||	\
  	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno) ||	\
  	 ((seg) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno) ||	\
  	 ((seg) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno) ||	\
d0b9e42ab   Chao Yu   f2fs: introduce i...
42
  	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno) ||	\
093749e29   Chao Yu   f2fs: support age...
43
44
  	 ((seg) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno) ||	\
  	 ((seg) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
45
46
  
  #define IS_CURSEC(sbi, secno)						\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
47
48
49
50
51
52
53
54
55
56
57
  	(((secno) == CURSEG_I(sbi, CURSEG_HOT_DATA)->segno /		\
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_DATA)->segno /		\
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA)->segno /		\
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_HOT_NODE)->segno /		\
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_WARM_NODE)->segno /		\
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_NODE)->segno /		\
d0b9e42ab   Chao Yu   f2fs: introduce i...
58
59
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_COLD_DATA_PINNED)->segno /	\
093749e29   Chao Yu   f2fs: support age...
60
61
  	  (sbi)->segs_per_sec) ||	\
  	 ((secno) == CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC)->segno /	\
d0b9e42ab   Chao Yu   f2fs: introduce i...
62
  	  (sbi)->segs_per_sec))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
63

0833721ec   Yunlei He   f2fs: check blkad...
64
65
66
67
68
69
  #define MAIN_BLKADDR(sbi)						\
  	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr : 				\
  		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
  #define SEG0_BLKADDR(sbi)						\
  	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : 				\
  		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
7cd8558ba   Jaegeuk Kim   f2fs: check the u...
70
71
  
  #define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
72
  #define MAIN_SECS(sbi)	((sbi)->total_sections)
7cd8558ba   Jaegeuk Kim   f2fs: check the u...
73

0833721ec   Yunlei He   f2fs: check blkad...
74
75
76
  #define TOTAL_SEGS(sbi)							\
  	(SM_I(sbi) ? SM_I(sbi)->segment_count : 				\
  		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
77
  #define TOTAL_BLKS(sbi)	(TOTAL_SEGS(sbi) << (sbi)->log_blocks_per_seg)
7cd8558ba   Jaegeuk Kim   f2fs: check the u...
78
79
  
  #define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
80
81
  #define SEGMENT_SIZE(sbi)	(1ULL << ((sbi)->log_blocksize +	\
  					(sbi)->log_blocks_per_seg))
7cd8558ba   Jaegeuk Kim   f2fs: check the u...
82
83
  
  #define START_BLOCK(sbi, segno)	(SEG0_BLKADDR(sbi) +			\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
84
  	 (GET_R2L_SEGNO(FREE_I(sbi), segno) << (sbi)->log_blocks_per_seg))
7cd8558ba   Jaegeuk Kim   f2fs: check the u...
85

39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
86
  #define NEXT_FREE_BLKADDR(sbi, curseg)					\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
87
  	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
88

7cd8558ba   Jaegeuk Kim   f2fs: check the u...
89
  #define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)	((blk_addr) - SEG0_BLKADDR(sbi))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
90
  #define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
91
  	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> (sbi)->log_blocks_per_seg)
491c0854b   Jaegeuk Kim   f2fs: clean up wi...
92
  #define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
93
  	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & ((sbi)->blocks_per_seg - 1))
491c0854b   Jaegeuk Kim   f2fs: clean up wi...
94

39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
95
  #define GET_SEGNO(sbi, blk_addr)					\
93770ab7a   Chao Yu   f2fs: introduce D...
96
  	((!__is_valid_data_blkaddr(blk_addr)) ?			\
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
97
98
  	NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi),			\
  		GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
99
100
101
  #define BLKS_PER_SEC(sbi)					\
  	((sbi)->segs_per_sec * (sbi)->blocks_per_seg)
  #define GET_SEC_FROM_SEG(sbi, segno)				\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
102
  	((segno) / (sbi)->segs_per_sec)
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
103
  #define GET_SEG_FROM_SEC(sbi, secno)				\
63fcf8e8d   Jaegeuk Kim   f2fs: use segment...
104
  	((secno) * (sbi)->segs_per_sec)
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
105
106
107
108
  #define GET_ZONE_FROM_SEC(sbi, secno)				\
  	((secno) / (sbi)->secs_per_zone)
  #define GET_ZONE_FROM_SEG(sbi, segno)				\
  	GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
109
110
  
  #define GET_SUM_BLOCK(sbi, segno)				\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
111
  	((sbi)->sm_info->ssa_blkaddr + (segno))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
112
113
  
  #define GET_SUM_TYPE(footer) ((footer)->entry_type)
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
114
  #define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
115
116
  
  #define SIT_ENTRY_OFFSET(sit_i, segno)					\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
117
  	((segno) % (sit_i)->sents_per_block)
d3a14afd5   Chao Yu   f2fs: remove unne...
118
  #define SIT_BLOCK_OFFSET(segno)					\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
119
  	((segno) / SIT_ENTRY_PER_BLOCK)
d3a14afd5   Chao Yu   f2fs: remove unne...
120
121
  #define	START_SEGNO(segno)		\
  	(SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
74de593af   Chao Yu   f2fs: read contig...
122
  #define SIT_BLK_CNT(sbi)			\
f91108b80   Geert Uytterhoeven   f2fs: Use DIV_ROU...
123
  	DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
124
125
  #define f2fs_bitmap_size(nr)			\
  	(BITS_TO_LONGS(nr) * sizeof(unsigned long))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
126

55cf9cb63   Chao Yu   f2fs: support lar...
127
128
129
  #define SECTOR_FROM_BLOCK(blk_addr)					\
  	(((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
  #define SECTOR_TO_BLOCK(sectors)					\
68afcf2d3   Tomohiro Kusumi   f2fs: guard macro...
130
  	((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
3cd8a2394   Jaegeuk Kim   f2fs: cleanup the...
131

39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  /*
   * indicate a block allocation direction: RIGHT and LEFT.
   * RIGHT means allocating new sections towards the end of volume.
   * LEFT means the opposite direction.
   */
  enum {
  	ALLOC_RIGHT = 0,
  	ALLOC_LEFT
  };
  
  /*
   * In the victim_sel_policy->alloc_mode, there are two block allocation modes.
   * LFS writes data sequentially with cleaning operations.
   * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
093749e29   Chao Yu   f2fs: support age...
146
147
   * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
   * fragmented segment which has similar aging degree.
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
148
149
150
   */
  enum {
  	LFS = 0,
093749e29   Chao Yu   f2fs: support age...
151
152
  	SSR,
  	AT_SSR,
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
153
154
155
156
157
158
  };
  
  /*
   * In the victim_sel_policy->gc_mode, there are two gc, aka cleaning, modes.
   * GC_CB is based on cost-benefit algorithm.
   * GC_GREEDY is based on greedy algorithm.
093749e29   Chao Yu   f2fs: support age...
159
   * GC_AT is based on age-threshold algorithm.
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
160
161
162
   */
  enum {
  	GC_CB = 0,
e066b83c9   Jaegeuk Kim   f2fs: add ioctl t...
163
  	GC_GREEDY,
093749e29   Chao Yu   f2fs: support age...
164
  	GC_AT,
e066b83c9   Jaegeuk Kim   f2fs: add ioctl t...
165
166
167
  	ALLOC_NEXT,
  	FLUSH_DEVICE,
  	MAX_GC_POLICY,
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
168
169
170
171
172
  };
  
  /*
   * BG_GC means the background cleaning job.
   * FG_GC means the on-demand cleaning job.
6aefd93b0   Jaegeuk Kim   f2fs: introduce b...
173
   * FORCE_FG_GC means on-demand cleaning job in background.
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
174
175
176
   */
  enum {
  	BG_GC = 0,
6aefd93b0   Jaegeuk Kim   f2fs: introduce b...
177
178
  	FG_GC,
  	FORCE_FG_GC,
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
179
180
181
182
183
184
  };
  
  /* for a function parameter to select a victim segment */
  struct victim_sel_policy {
  	int alloc_mode;			/* LFS or SSR */
  	int gc_mode;			/* GC_CB or GC_GREEDY */
da52f8ade   Jack Qiu   f2fs: get the rig...
185
186
187
188
189
  	unsigned long *dirty_bitmap;	/* dirty segment/section bitmap */
  	unsigned int max_search;	/*
  					 * maximum # of segments/sections
  					 * to search
  					 */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
190
191
192
  	unsigned int offset;		/* last scanned bitmap offset */
  	unsigned int ofs_unit;		/* bitmap search unit */
  	unsigned int min_cost;		/* minimum cost */
093749e29   Chao Yu   f2fs: support age...
193
  	unsigned long long oldest_age;	/* oldest age of segments having the same min cost */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
194
  	unsigned int min_segno;		/* segment # having min. cost */
093749e29   Chao Yu   f2fs: support age...
195
196
  	unsigned long long age;		/* mtime of GCed section*/
  	unsigned long long age_threshold;/* age threshold */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
197
198
199
  };
  
  struct seg_entry {
f51b4ce6c   Chao Yu   f2fs: shrink size...
200
201
202
203
  	unsigned int type:6;		/* segment type like CURSEG_XXX_TYPE */
  	unsigned int valid_blocks:10;	/* # of valid blocks */
  	unsigned int ckpt_valid_blocks:10;	/* # of valid blocks last cp */
  	unsigned int padding:6;		/* padding */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
204
  	unsigned char *cur_valid_map;	/* validity bitmap of blocks */
355e78913   Chao Yu   f2fs: check in-me...
205
206
207
  #ifdef CONFIG_F2FS_CHECK_FS
  	unsigned char *cur_valid_map_mir;	/* mirror of current valid bitmap */
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
208
  	/*
887e03739   Randy Dunlap   f2fs: segment.h: ...
209
  	 * # of valid blocks and the validity bitmap stored in the last
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
210
211
  	 * checkpoint pack. This information is used by the SSR mode.
  	 */
f51b4ce6c   Chao Yu   f2fs: shrink size...
212
  	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp */
a66cdd985   Jaegeuk Kim   f2fs: introduce d...
213
  	unsigned char *discard_map;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
214
215
216
217
218
219
220
221
222
223
  	unsigned long long mtime;	/* modification time of the segment */
  };
  
  struct sec_entry {
  	unsigned int valid_blocks;	/* # of valid blocks in a section */
  };
  
  struct segment_allocation {
  	void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
  };
6f8d44550   Jaegeuk Kim   f2fs: avoid fi->i...
224
  #define MAX_SKIP_GC_COUNT			16
2ef79ecb5   Chao Yu   f2fs: avoid stuck...
225

88b88a667   Jaegeuk Kim   f2fs: support ato...
226
227
228
  struct inmem_pages {
  	struct list_head list;
  	struct page *page;
28bc106b2   Chao Yu   f2fs: support rev...
229
  	block_t old_addr;		/* for revoking when fail to commit */
88b88a667   Jaegeuk Kim   f2fs: support ato...
230
  };
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
231
232
233
234
235
236
  struct sit_info {
  	const struct segment_allocation *s_ops;
  
  	block_t sit_base_addr;		/* start block address of SIT area */
  	block_t sit_blocks;		/* # of blocks used by SIT area */
  	block_t written_valid_blocks;	/* # of valid blocks in main area */
2fde3dd14   Chao Yu   f2fs: allocate me...
237
  	char *bitmap;			/* all bitmaps pointer */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
238
  	char *sit_bitmap;		/* SIT bitmap pointer */
ae27d62e6   Chao Yu   f2fs: check in-me...
239
240
  #ifdef CONFIG_F2FS_CHECK_FS
  	char *sit_bitmap_mir;		/* SIT bitmap mirror */
bbf9f7d90   Sahitya Tummala   f2fs: Fix indefin...
241
242
243
  
  	/* bitmap of segments to be ignored by GC in case of errors */
  	unsigned long *invalid_segmap;
ae27d62e6   Chao Yu   f2fs: check in-me...
244
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
245
  	unsigned int bitmap_size;	/* SIT bitmap size */
60a3b782b   Jaegeuk Kim   f2fs: avoid varia...
246
  	unsigned long *tmp_map;			/* bitmap for temporal use */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
247
248
249
  	unsigned long *dirty_sentries_bitmap;	/* bitmap for dirty sentries */
  	unsigned int dirty_sentries;		/* # of dirty sentries */
  	unsigned int sents_per_block;		/* # of SIT entries per block */
3d26fa6be   Chao Yu   f2fs: use rw_sema...
250
  	struct rw_semaphore sentry_lock;	/* to protect SIT cache */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
251
252
253
254
255
256
257
258
  	struct seg_entry *sentries;		/* SIT segment-level cache */
  	struct sec_entry *sec_entries;		/* SIT section-level cache */
  
  	/* for cost-benefit algorithm in cleaning procedure */
  	unsigned long long elapsed_time;	/* elapsed time after mount */
  	unsigned long long mounted_time;	/* mount time */
  	unsigned long long min_mtime;		/* min. modification time */
  	unsigned long long max_mtime;		/* max. modification time */
093749e29   Chao Yu   f2fs: support age...
259
260
  	unsigned long long dirty_min_mtime;	/* rerange candidates in GC_AT */
  	unsigned long long dirty_max_mtime;	/* rerange candidates in GC_AT */
e066b83c9   Jaegeuk Kim   f2fs: add ioctl t...
261
262
  
  	unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
263
264
265
266
267
268
  };
  
  struct free_segmap_info {
  	unsigned int start_segno;	/* start segment number logically */
  	unsigned int free_segments;	/* # of free segments */
  	unsigned int free_sections;	/* # of free sections */
1a118ccfd   Chao Yu   f2fs: use spinloc...
269
  	spinlock_t segmap_lock;		/* free segmap lock */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  	unsigned long *free_segmap;	/* free segment bitmap */
  	unsigned long *free_secmap;	/* free section bitmap */
  };
  
  /* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
  enum dirty_type {
  	DIRTY_HOT_DATA,		/* dirty segments assigned as hot data logs */
  	DIRTY_WARM_DATA,	/* dirty segments assigned as warm data logs */
  	DIRTY_COLD_DATA,	/* dirty segments assigned as cold data logs */
  	DIRTY_HOT_NODE,		/* dirty segments assigned as hot node logs */
  	DIRTY_WARM_NODE,	/* dirty segments assigned as warm node logs */
  	DIRTY_COLD_NODE,	/* dirty segments assigned as cold node logs */
  	DIRTY,			/* to count # of dirty segments */
  	PRE,			/* to count # of entirely obsolete segments */
  	NR_DIRTY_TYPE
  };
  
  struct dirty_seglist_info {
  	const struct victim_selection *v_ops;	/* victim selction operation */
  	unsigned long *dirty_segmap[NR_DIRTY_TYPE];
da52f8ade   Jack Qiu   f2fs: get the rig...
290
  	unsigned long *dirty_secmap;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
291
292
  	struct mutex seglist_lock;		/* lock for segment bitmaps */
  	int nr_dirty[NR_DIRTY_TYPE];		/* # of dirty segments */
5ec4e49f9   Jaegeuk Kim   f2fs: change GC b...
293
  	unsigned long *victim_secmap;		/* background GC victims */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
294
295
296
297
298
  };
  
  /* victim selection function for cleaning and SSR */
  struct victim_selection {
  	int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
093749e29   Chao Yu   f2fs: support age...
299
  					int, int, char, unsigned long long);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
300
301
302
303
304
305
  };
  
  /* for active log information */
  struct curseg_info {
  	struct mutex curseg_mutex;		/* lock for consistency */
  	struct f2fs_summary_block *sum_blk;	/* cached summary block */
b7ad7512b   Chao Yu   f2fs: split journ...
306
307
  	struct rw_semaphore journal_rwsem;	/* protect journal area */
  	struct f2fs_journal *journal;		/* cached journal info */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
308
  	unsigned char alloc_type;		/* current allocation type */
d0b9e42ab   Chao Yu   f2fs: introduce i...
309
  	unsigned short seg_type;		/* segment type like CURSEG_XXX_TYPE */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
310
311
312
313
  	unsigned int segno;			/* current segment number */
  	unsigned short next_blkoff;		/* next block offset to write */
  	unsigned int zone;			/* current zone number */
  	unsigned int next_segno;		/* preallocated segment */
d0b9e42ab   Chao Yu   f2fs: introduce i...
314
  	bool inited;				/* indicate inmem log is inited */
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
315
  };
184a5cd2c   Chao Yu   f2fs: refactor fl...
316
317
318
319
320
  struct sit_entry_set {
  	struct list_head set_list;	/* link with all sit sets */
  	unsigned int start_segno;	/* start segno of sits in set */
  	unsigned int entry_cnt;		/* the # of sit entries in set */
  };
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  /*
   * inline functions
   */
  static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
  {
  	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
  }
  
  static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
  						unsigned int segno)
  {
  	struct sit_info *sit_i = SIT_I(sbi);
  	return &sit_i->sentries[segno];
  }
  
  static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
  						unsigned int segno)
  {
  	struct sit_info *sit_i = SIT_I(sbi);
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
340
  	return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
341
342
343
  }
  
  static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
302bd3488   Jaegeuk Kim   f2fs: clean up ge...
344
  				unsigned int segno, bool use_section)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
345
346
347
348
349
  {
  	/*
  	 * In order to get # of valid blocks in a section instantly from many
  	 * segments, f2fs manages two counting structures separately.
  	 */
2c70c5e38   Chao Yu   f2fs: introduce _...
350
  	if (use_section && __is_large_section(sbi))
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
351
352
353
354
  		return get_sec_entry(sbi, segno)->valid_blocks;
  	else
  		return get_seg_entry(sbi, segno)->valid_blocks;
  }
4354994f0   Daniel Rosenberg   f2fs: checkpoint ...
355
356
357
358
359
  static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
  				unsigned int segno)
  {
  	return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  }
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
360
361
362
363
364
365
366
  static inline void seg_info_from_raw_sit(struct seg_entry *se,
  					struct f2fs_sit_entry *rs)
  {
  	se->valid_blocks = GET_SIT_VBLOCKS(rs);
  	se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
  	memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
355e78913   Chao Yu   f2fs: check in-me...
367
368
369
  #ifdef CONFIG_F2FS_CHECK_FS
  	memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
370
371
372
  	se->type = GET_SIT_TYPE(rs);
  	se->mtime = le64_to_cpu(rs->mtime);
  }
068c3cd85   Yunlei He   f2fs: rebuild sit...
373
  static inline void __seg_info_to_raw_sit(struct seg_entry *se,
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
374
375
376
377
378
379
  					struct f2fs_sit_entry *rs)
  {
  	unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
  					se->valid_blocks;
  	rs->vblocks = cpu_to_le16(raw_vblocks);
  	memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
068c3cd85   Yunlei He   f2fs: rebuild sit...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  	rs->mtime = cpu_to_le64(se->mtime);
  }
  
  static inline void seg_info_to_sit_page(struct f2fs_sb_info *sbi,
  				struct page *page, unsigned int start)
  {
  	struct f2fs_sit_block *raw_sit;
  	struct seg_entry *se;
  	struct f2fs_sit_entry *rs;
  	unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
  					(unsigned long)MAIN_SEGS(sbi));
  	int i;
  
  	raw_sit = (struct f2fs_sit_block *)page_address(page);
81114baa8   Chao Yu   f2fs: don't use G...
394
  	memset(raw_sit, 0, PAGE_SIZE);
068c3cd85   Yunlei He   f2fs: rebuild sit...
395
396
397
398
399
400
401
402
403
404
405
  	for (i = 0; i < end - start; i++) {
  		rs = &raw_sit->entries[i];
  		se = get_seg_entry(sbi, start + i);
  		__seg_info_to_raw_sit(se, rs);
  	}
  }
  
  static inline void seg_info_to_raw_sit(struct seg_entry *se,
  					struct f2fs_sit_entry *rs)
  {
  	__seg_info_to_raw_sit(se, rs);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
406
407
  	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
  	se->ckpt_valid_blocks = se->valid_blocks;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
408
409
410
411
412
413
  }
  
  static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
  		unsigned int max, unsigned int segno)
  {
  	unsigned int ret;
1a118ccfd   Chao Yu   f2fs: use spinloc...
414
  	spin_lock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
415
  	ret = find_next_bit(free_i->free_segmap, max, segno);
1a118ccfd   Chao Yu   f2fs: use spinloc...
416
  	spin_unlock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
417
418
419
420
421
422
  	return ret;
  }
  
  static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
  {
  	struct free_segmap_info *free_i = FREE_I(sbi);
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
423
424
  	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
425
  	unsigned int next;
de881df97   Aravind Ramesh   f2fs: support zon...
426
  	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
427

1a118ccfd   Chao Yu   f2fs: use spinloc...
428
  	spin_lock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
429
430
  	clear_bit(segno, free_i->free_segmap);
  	free_i->free_segments++;
7fd97019b   Wanpeng Li   f2fs: reduce sear...
431
432
  	next = find_next_bit(free_i->free_segmap,
  			start_segno + sbi->segs_per_sec, start_segno);
de881df97   Aravind Ramesh   f2fs: support zon...
433
  	if (next >= start_segno + usable_segs) {
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
434
435
436
  		clear_bit(secno, free_i->free_secmap);
  		free_i->free_sections++;
  	}
1a118ccfd   Chao Yu   f2fs: use spinloc...
437
  	spin_unlock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
438
439
440
441
442
443
  }
  
  static inline void __set_inuse(struct f2fs_sb_info *sbi,
  		unsigned int segno)
  {
  	struct free_segmap_info *free_i = FREE_I(sbi);
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
444
  	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
445
446
447
448
449
450
451
  	set_bit(segno, free_i->free_segmap);
  	free_i->free_segments--;
  	if (!test_and_set_bit(secno, free_i->free_secmap))
  		free_i->free_sections--;
  }
  
  static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
d0b9e42ab   Chao Yu   f2fs: introduce i...
452
  		unsigned int segno, bool inmem)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
453
454
  {
  	struct free_segmap_info *free_i = FREE_I(sbi);
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
455
456
  	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
  	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
457
  	unsigned int next;
de881df97   Aravind Ramesh   f2fs: support zon...
458
  	unsigned int usable_segs = f2fs_usable_segs_in_sec(sbi, segno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
459

1a118ccfd   Chao Yu   f2fs: use spinloc...
460
  	spin_lock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
461
462
  	if (test_and_clear_bit(segno, free_i->free_segmap)) {
  		free_i->free_segments++;
d0b9e42ab   Chao Yu   f2fs: introduce i...
463
  		if (!inmem && IS_CURSEC(sbi, secno))
3611ce991   Yunlong Song   f2fs: do not set ...
464
  			goto skip_free;
f1121ab0b   Chao Yu   f2fs: reduce sear...
465
466
  		next = find_next_bit(free_i->free_segmap,
  				start_segno + sbi->segs_per_sec, start_segno);
de881df97   Aravind Ramesh   f2fs: support zon...
467
  		if (next >= start_segno + usable_segs) {
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
468
469
470
471
  			if (test_and_clear_bit(secno, free_i->free_secmap))
  				free_i->free_sections++;
  		}
  	}
3611ce991   Yunlong Song   f2fs: do not set ...
472
  skip_free:
1a118ccfd   Chao Yu   f2fs: use spinloc...
473
  	spin_unlock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
474
475
476
477
478
479
  }
  
  static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
  		unsigned int segno)
  {
  	struct free_segmap_info *free_i = FREE_I(sbi);
4ddb1a4d4   Jaegeuk Kim   f2fs: clean up so...
480
  	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
1a118ccfd   Chao Yu   f2fs: use spinloc...
481
  	spin_lock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
482
483
484
485
486
  	if (!test_and_set_bit(segno, free_i->free_segmap)) {
  		free_i->free_segments--;
  		if (!test_and_set_bit(secno, free_i->free_secmap))
  			free_i->free_sections--;
  	}
1a118ccfd   Chao Yu   f2fs: use spinloc...
487
  	spin_unlock(&free_i->segmap_lock);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
488
489
490
491
492
493
  }
  
  static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
  		void *dst_addr)
  {
  	struct sit_info *sit_i = SIT_I(sbi);
ae27d62e6   Chao Yu   f2fs: check in-me...
494
495
496
497
498
499
  
  #ifdef CONFIG_F2FS_CHECK_FS
  	if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
  						sit_i->bitmap_size))
  		f2fs_bug_on(sbi, 1);
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
500
501
502
503
504
  	memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
  }
  
  static inline block_t written_block_count(struct f2fs_sb_info *sbi)
  {
8b8343fa9   Jaegeuk Kim   f2fs: implement a...
505
  	return SIT_I(sbi)->written_valid_blocks;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
506
507
508
509
  }
  
  static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
  {
8b8343fa9   Jaegeuk Kim   f2fs: implement a...
510
  	return FREE_I(sbi)->free_segments;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
511
  }
4470eb287   Xiaojun Wang   f2fs: change retu...
512
  static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
513
514
515
516
517
518
  {
  	return SM_I(sbi)->reserved_segments;
  }
  
  static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
  {
8b8343fa9   Jaegeuk Kim   f2fs: implement a...
519
  	return FREE_I(sbi)->free_sections;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
  }
  
  static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
  {
  	return DIRTY_I(sbi)->nr_dirty[PRE];
  }
  
  static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
  {
  	return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
  		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
  		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
  		DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
  		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
  		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
  }
  
  static inline int overprovision_segments(struct f2fs_sb_info *sbi)
  {
  	return SM_I(sbi)->ovp_segments;
  }
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
541
542
  static inline int reserved_sections(struct f2fs_sb_info *sbi)
  {
4470eb287   Xiaojun Wang   f2fs: change retu...
543
  	return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
544
  }
bf34c93d2   Chao Yu   f2fs: check curse...
545
546
547
548
549
550
551
552
553
554
555
  static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi)
  {
  	unsigned int node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
  					get_pages(sbi, F2FS_DIRTY_DENTS);
  	unsigned int dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
  	unsigned int segno, left_blocks;
  	int i;
  
  	/* check current node segment */
  	for (i = CURSEG_HOT_NODE; i <= CURSEG_COLD_NODE; i++) {
  		segno = CURSEG_I(sbi, i)->segno;
de881df97   Aravind Ramesh   f2fs: support zon...
556
557
  		left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
  				get_seg_entry(sbi, segno)->ckpt_valid_blocks;
bf34c93d2   Chao Yu   f2fs: check curse...
558
559
560
561
562
563
564
  
  		if (node_blocks > left_blocks)
  			return false;
  	}
  
  	/* check current data segment */
  	segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
de881df97   Aravind Ramesh   f2fs: support zon...
565
  	left_blocks = f2fs_usable_blks_in_seg(sbi, segno) -
bf34c93d2   Chao Yu   f2fs: check curse...
566
567
568
569
570
  			get_seg_entry(sbi, segno)->ckpt_valid_blocks;
  	if (dent_blocks > left_blocks)
  		return false;
  	return true;
  }
7f3037a5e   Jaegeuk Kim   f2fs: check free_...
571
572
  static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
  					int freed, int needed)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
573
  {
5ac206cf4   Namjae Jeon   f2fs: make an acc...
574
575
  	int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  	int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
b9610bdfc   Jaegeuk Kim   f2fs: count dirty...
576
  	int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
0f18b462b   Jaegeuk Kim   f2fs: flush inode...
577

caf0047e7   Chao Yu   f2fs: merge flags...
578
  	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
029cd28c1   Jaegeuk Kim   f2fs: fix equatio...
579
  		return false;
bf34c93d2   Chao Yu   f2fs: check curse...
580
581
582
  	if (free_sections(sbi) + freed == reserved_sections(sbi) + needed &&
  			has_curseg_enough_space(sbi))
  		return false;
7f3037a5e   Jaegeuk Kim   f2fs: check free_...
583
  	return (free_sections(sbi) + freed) <=
b9610bdfc   Jaegeuk Kim   f2fs: count dirty...
584
585
  		(node_secs + 2 * dent_secs + imeta_secs +
  		reserved_sections(sbi) + needed);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
586
  }
00e09c0bc   Chao Yu   f2fs: enhance f2f...
587
  static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
4354994f0   Daniel Rosenberg   f2fs: checkpoint ...
588
589
  {
  	if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
00e09c0bc   Chao Yu   f2fs: enhance f2f...
590
  		return true;
4354994f0   Daniel Rosenberg   f2fs: checkpoint ...
591
  	if (likely(!has_not_enough_free_secs(sbi, 0, 0)))
00e09c0bc   Chao Yu   f2fs: enhance f2f...
592
593
  		return true;
  	return false;
4354994f0   Daniel Rosenberg   f2fs: checkpoint ...
594
  }
81eb8d6e2   Jaegeuk Kim   f2fs: reclaim pre...
595
596
  static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
  {
6c311ec6c   Chris Fries   f2fs: clean check...
597
  	return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
81eb8d6e2   Jaegeuk Kim   f2fs: reclaim pre...
598
  }
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
599
600
  static inline int utilization(struct f2fs_sb_info *sbi)
  {
6c311ec6c   Chris Fries   f2fs: clean check...
601
602
  	return div_u64((u64)valid_user_blocks(sbi) * 100,
  					sbi->user_block_count);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
603
604
605
606
  }
  
  /*
   * Sometimes f2fs may be better to drop out-of-place update policy.
216fbd644   Jaegeuk Kim   f2fs: introduce s...
607
608
609
610
611
612
613
   * And, users can control the policy through sysfs entries.
   * There are five policies with triggering conditions as follows.
   * F2FS_IPU_FORCE - all the time,
   * F2FS_IPU_SSR - if SSR mode is activated,
   * F2FS_IPU_UTIL - if FS utilization is over threashold,
   * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
   *                     threashold,
c1ce1b02b   Jaegeuk Kim   f2fs: give an opt...
614
615
   * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
   *                     storages. IPU will be triggered only if the # of dirty
d7b0a23d8   Jaegeuk Kim   f2fs: update f2fs...
616
617
   *                     pages over min_fsync_blocks. (=default option)
   * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
0e7f41974   Jaegeuk Kim   f2fs: add a way t...
618
   * F2FS_IPU_NOCACHE - disable IPU bio cache.
d7b0a23d8   Jaegeuk Kim   f2fs: update f2fs...
619
   * F2FS_IPUT_DISABLE - disable IPU. (=default option in LFS mode)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
620
   */
216fbd644   Jaegeuk Kim   f2fs: introduce s...
621
  #define DEF_MIN_IPU_UTIL	70
c1ce1b02b   Jaegeuk Kim   f2fs: give an opt...
622
  #define DEF_MIN_FSYNC_BLOCKS	8
ef095d19e   Jaegeuk Kim   f2fs: write small...
623
  #define DEF_MIN_HOT_BLOCKS	16
216fbd644   Jaegeuk Kim   f2fs: introduce s...
624

84b89e5d9   Jaegeuk Kim   f2fs: add auto tu...
625
  #define SMALL_VOLUME_SEGMENTS	(16 * 512)	/* 16GB */
216fbd644   Jaegeuk Kim   f2fs: introduce s...
626
627
628
629
630
  enum {
  	F2FS_IPU_FORCE,
  	F2FS_IPU_SSR,
  	F2FS_IPU_UTIL,
  	F2FS_IPU_SSR_UTIL,
c1ce1b02b   Jaegeuk Kim   f2fs: give an opt...
631
  	F2FS_IPU_FSYNC,
04485987f   Hou Pengyang   f2fs: introduce a...
632
  	F2FS_IPU_ASYNC,
0e7f41974   Jaegeuk Kim   f2fs: add a way t...
633
  	F2FS_IPU_NOCACHE,
216fbd644   Jaegeuk Kim   f2fs: introduce s...
634
  };
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
  		int type)
  {
  	struct curseg_info *curseg = CURSEG_I(sbi, type);
  	return curseg->segno;
  }
  
  static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
  		int type)
  {
  	struct curseg_info *curseg = CURSEG_I(sbi, type);
  	return curseg->alloc_type;
  }
  
  static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
  {
  	struct curseg_info *curseg = CURSEG_I(sbi, type);
  	return curseg->next_blkoff;
  }
  
  static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
  {
7a04f64d4   Liu Xue   f2fs: unify f2fs_...
657
  	f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
658
  }
93770ab7a   Chao Yu   f2fs: introduce D...
659
  static inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
660
  {
0833721ec   Yunlei He   f2fs: check blkad...
661
  	struct f2fs_sb_info *sbi = fio->sbi;
93770ab7a   Chao Yu   f2fs: introduce D...
662
663
664
665
666
  	if (__is_valid_data_blkaddr(fio->old_blkaddr))
  		verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
  					META_GENERIC : DATA_GENERIC);
  	verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
  					META_GENERIC : DATA_GENERIC_ENHANCE);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
667
668
669
  }
  
  /*
e1c420452   arter97   f2fs: fix typo
670
   * Summary block is always treated as an invalid block
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
671
   */
c39a1b348   Jaegeuk Kim   f2fs: return erro...
672
  static inline int check_block_count(struct f2fs_sb_info *sbi,
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
673
674
  		int segno, struct f2fs_sit_entry *raw_sit)
  {
44c60bf2b   Chao Yu   f2fs: check all o...
675
  	bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
676
  	int valid_blocks = 0;
44c60bf2b   Chao Yu   f2fs: check all o...
677
  	int cur_pos = 0, next_pos;
de881df97   Aravind Ramesh   f2fs: support zon...
678
  	unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
679

39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
680
  	/* check bitmap with valid block count */
44c60bf2b   Chao Yu   f2fs: check all o...
681
682
683
  	do {
  		if (is_valid) {
  			next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
de881df97   Aravind Ramesh   f2fs: support zon...
684
  					usable_blks_per_seg,
44c60bf2b   Chao Yu   f2fs: check all o...
685
686
687
688
  					cur_pos);
  			valid_blocks += next_pos - cur_pos;
  		} else
  			next_pos = find_next_bit_le(&raw_sit->valid_map,
de881df97   Aravind Ramesh   f2fs: support zon...
689
  					usable_blks_per_seg,
44c60bf2b   Chao Yu   f2fs: check all o...
690
691
692
  					cur_pos);
  		cur_pos = next_pos;
  		is_valid = !is_valid;
de881df97   Aravind Ramesh   f2fs: support zon...
693
  	} while (cur_pos < usable_blks_per_seg);
c39a1b348   Jaegeuk Kim   f2fs: return erro...
694
695
  
  	if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
dcbb4c10e   Joe Perches   f2fs: introduce f...
696
697
  		f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
  			 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
c39a1b348   Jaegeuk Kim   f2fs: return erro...
698
  		set_sbi_flag(sbi, SBI_NEED_FSCK);
10f966bbf   Chao Yu   f2fs: use generic...
699
  		return -EFSCORRUPTED;
c39a1b348   Jaegeuk Kim   f2fs: return erro...
700
  	}
e95bcdb2f   Chao Yu   f2fs: fix to do s...
701

de881df97   Aravind Ramesh   f2fs: support zon...
702
703
704
705
  	if (usable_blks_per_seg < sbi->blocks_per_seg)
  		f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
  				sbi->blocks_per_seg,
  				usable_blks_per_seg) != sbi->blocks_per_seg);
4c278394b   Jaegeuk Kim   f2fs: avoid a bui...
706
  	/* check segment usage, and check boundary of a given segment number */
de881df97   Aravind Ramesh   f2fs: support zon...
707
  	if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
c39a1b348   Jaegeuk Kim   f2fs: return erro...
708
  					|| segno > TOTAL_SEGS(sbi) - 1)) {
dcbb4c10e   Joe Perches   f2fs: introduce f...
709
710
  		f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
  			 GET_SIT_VBLOCKS(raw_sit), segno);
c39a1b348   Jaegeuk Kim   f2fs: return erro...
711
  		set_sbi_flag(sbi, SBI_NEED_FSCK);
10f966bbf   Chao Yu   f2fs: use generic...
712
  		return -EFSCORRUPTED;
c39a1b348   Jaegeuk Kim   f2fs: return erro...
713
714
  	}
  	return 0;
7a04f64d4   Liu Xue   f2fs: unify f2fs_...
715
  }
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
716
717
718
719
720
  
  static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
  						unsigned int start)
  {
  	struct sit_info *sit_i = SIT_I(sbi);
d3a14afd5   Chao Yu   f2fs: remove unne...
721
  	unsigned int offset = SIT_BLOCK_OFFSET(start);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
722
723
724
  	block_t blk_addr = sit_i->sit_base_addr + offset;
  
  	check_seg_range(sbi, start);
ae27d62e6   Chao Yu   f2fs: check in-me...
725
726
727
728
729
  #ifdef CONFIG_F2FS_CHECK_FS
  	if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
  			f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
  		f2fs_bug_on(sbi, 1);
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
  	/* calculate sit block address */
  	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  		blk_addr += sit_i->sit_blocks;
  
  	return blk_addr;
  }
  
  static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
  						pgoff_t block_addr)
  {
  	struct sit_info *sit_i = SIT_I(sbi);
  	block_addr -= sit_i->sit_base_addr;
  	if (block_addr < sit_i->sit_blocks)
  		block_addr += sit_i->sit_blocks;
  	else
  		block_addr -= sit_i->sit_blocks;
  
  	return block_addr + sit_i->sit_base_addr;
  }
  
  static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
  {
d3a14afd5   Chao Yu   f2fs: remove unne...
752
  	unsigned int block_off = SIT_BLOCK_OFFSET(start);
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
753

c6ac4c0ec   Gu Zheng   f2fs: introduce f...
754
  	f2fs_change_bit(block_off, sit_i->sit_bitmap);
ae27d62e6   Chao Yu   f2fs: check in-me...
755
756
757
  #ifdef CONFIG_F2FS_CHECK_FS
  	f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
  #endif
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
758
  }
a1f72ac2c   Chao Yu   f2fs: fix to upda...
759
760
  static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
  						bool base_time)
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
761
762
  {
  	struct sit_info *sit_i = SIT_I(sbi);
a7e679b53   Jaegeuk Kim   f2fs: show mounte...
763
  	time64_t diff, now = ktime_get_boottime_seconds();
48fbfe50f   Deepa Dinamani   fs: f2fs: use kti...
764

a1f72ac2c   Chao Yu   f2fs: fix to upda...
765
766
767
768
769
770
771
772
773
774
775
  	if (now >= sit_i->mounted_time)
  		return sit_i->elapsed_time + now - sit_i->mounted_time;
  
  	/* system time is set to the past */
  	if (!base_time) {
  		diff = sit_i->mounted_time - now;
  		if (sit_i->elapsed_time >= diff)
  			return sit_i->elapsed_time - diff;
  		return 0;
  	}
  	return sit_i->elapsed_time;
39a53e0ce   Jaegeuk Kim   f2fs: add superbl...
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  }
  
  static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
  			unsigned int ofs_in_node, unsigned char version)
  {
  	sum->nid = cpu_to_le32(nid);
  	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
  	sum->version = version;
  }
  
  static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
  {
  	return __start_cp_addr(sbi) +
  		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  }
  
  static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
  {
  	return __start_cp_addr(sbi) +
  		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
  				- (base + 1) + type;
  }
5ec4e49f9   Jaegeuk Kim   f2fs: change GC b...
798
799
800
801
802
803
804
  
  static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
  {
  	if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
  		return true;
  	return false;
  }
ac5d156c7   Jaegeuk Kim   f2fs: modify the ...
805

87d6f8909   Jaegeuk Kim   f2fs: avoid small...
806
807
808
809
  /*
   * It is very important to gather dirty pages and write at once, so that we can
   * submit a big bio without interfering other data writes.
   * By default, 512 pages for directory data,
727ebb091   Kinglong Mee   f2fs: update the ...
810
811
   * 512 pages (2MB) * 8 for nodes, and
   * 256 pages * 8 for meta are set.
87d6f8909   Jaegeuk Kim   f2fs: avoid small...
812
813
814
   */
  static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
  {
a88a341a7   Tejun Heo   writeback: move b...
815
  	if (sbi->sb->s_bdi->wb.dirty_exceeded)
510184c89   Jaegeuk Kim   f2fs: do not skip...
816
  		return 0;
a12570232   Jaegeuk Kim   Revert "f2fs: do ...
817
818
819
  	if (type == DATA)
  		return sbi->blocks_per_seg;
  	else if (type == NODE)
2c237ebaa   Jaegeuk Kim   f2fs: avoid writi...
820
  		return 8 * sbi->blocks_per_seg;
87d6f8909   Jaegeuk Kim   f2fs: avoid small...
821
  	else if (type == META)
664ba972d   Jaegeuk Kim   f2fs: use BIO_MAX...
822
  		return 8 * BIO_MAX_PAGES;
87d6f8909   Jaegeuk Kim   f2fs: avoid small...
823
824
825
  	else
  		return 0;
  }
50c8cdb35   Jaegeuk Kim   f2fs: introduce n...
826
827
828
829
830
831
832
833
834
835
836
837
838
  
  /*
   * When writing pages, it'd better align nr_to_write for segment size.
   */
  static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
  					struct writeback_control *wbc)
  {
  	long nr_to_write, desired;
  
  	if (wbc->sync_mode != WB_SYNC_NONE)
  		return 0;
  
  	nr_to_write = wbc->nr_to_write;
664ba972d   Jaegeuk Kim   f2fs: use BIO_MAX...
839
  	desired = BIO_MAX_PAGES;
28ea6162e   Jaegeuk Kim   f2fs: do not skip...
840
  	if (type == NODE)
664ba972d   Jaegeuk Kim   f2fs: use BIO_MAX...
841
  		desired <<= 1;
50c8cdb35   Jaegeuk Kim   f2fs: introduce n...
842
843
844
845
  
  	wbc->nr_to_write = desired;
  	return desired - nr_to_write;
  }
01983c715   Jaegeuk Kim   f2fs: wake up dis...
846
847
848
849
850
851
852
853
854
855
856
  
  static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
  {
  	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  	bool wakeup = false;
  	int i;
  
  	if (force)
  		goto wake_up;
  
  	mutex_lock(&dcc->cmd_lock);
78997b569   Chao Yu   f2fs: split disca...
857
858
859
  	for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  		if (i + 1 < dcc->discard_granularity)
  			break;
01983c715   Jaegeuk Kim   f2fs: wake up dis...
860
861
862
863
864
865
  		if (!list_empty(&dcc->pend_list[i])) {
  			wakeup = true;
  			break;
  		}
  	}
  	mutex_unlock(&dcc->cmd_lock);
b460866d2   Jaegeuk Kim   f2fs: don't wake ...
866
  	if (!wakeup || !is_idle(sbi, DISCARD_TIME))
01983c715   Jaegeuk Kim   f2fs: wake up dis...
867
868
869
870
871
  		return;
  wake_up:
  	dcc->discard_wake = 1;
  	wake_up_interruptible_all(&dcc->discard_wait_queue);
  }