blob: faca7fdb54b0816b4702b94a2742bb6d69814889 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kim39a53e02012-11-28 13:37:31 +09002 * 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 */
Jaegeuk Kimac5d1562013-04-29 16:58:39 +090011#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040012#include <linux/backing-dev.h>
Jaegeuk Kimac5d1562013-04-29 16:58:39 +090013
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090014/* constant macro */
15#define NULL_SEGNO ((unsigned int)(~0))
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +090016#define NULL_SECNO ((unsigned int)(~0))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090017
Jaegeuk Kim58c41032014-03-19 14:17:21 +090018#define DEF_RECLAIM_PREFREE_SEGMENTS 5 /* 5% over total segments */
Jaegeuk Kim44a83492016-07-13 18:23:35 -070019#define DEF_MAX_RECLAIM_PREFREE_SEGMENTS 4096 /* 8GB in maximum */
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +090020
Jaegeuk Kim240ceff2016-12-05 13:56:04 -080021#define F2FS_MIN_SEGMENTS 9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
22
Namjae Jeon6224da82013-04-06 14:44:32 +090023/* L: Logical segment # in volume, R: Relative segment # in main area */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090024#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)
26
Changman Lee61ae45c2013-11-21 20:04:21 +090027#define IS_DATASEG(t) (t <= CURSEG_COLD_DATA)
28#define IS_NODESEG(t) (t >= CURSEG_HOT_NODE)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090029
Jaegeuk Kim5c773ba2013-03-31 12:30:04 +090030#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))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090037
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 / \
50 sbi->segs_per_sec)) \
51
Yunlei He89d78cf2018-03-08 16:29:13 +080052#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))
Jaegeuk Kim7cd85582014-09-23 11:23:01 -070058
59#define MAIN_SEGS(sbi) (SM_I(sbi)->main_segments)
60#define MAIN_SECS(sbi) (sbi->total_sections)
61
Yunlei He89d78cf2018-03-08 16:29:13 +080062#define TOTAL_SEGS(sbi) \
63 (SM_I(sbi) ? SM_I(sbi)->segment_count : \
64 le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
Jaegeuk Kim7cd85582014-09-23 11:23:01 -070065#define TOTAL_BLKS(sbi) (TOTAL_SEGS(sbi) << sbi->log_blocks_per_seg)
66
67#define MAX_BLKADDR(sbi) (SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
Dan Carpenter8a219842014-09-25 14:39:17 +030068#define SEGMENT_SIZE(sbi) (1ULL << (sbi->log_blocksize + \
Jaegeuk Kim7cd85582014-09-23 11:23:01 -070069 sbi->log_blocks_per_seg))
70
71#define START_BLOCK(sbi, segno) (SEG0_BLKADDR(sbi) + \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090072 (GET_R2L_SEGNO(FREE_I(sbi), segno) << sbi->log_blocks_per_seg))
Jaegeuk Kim7cd85582014-09-23 11:23:01 -070073
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090074#define NEXT_FREE_BLKADDR(sbi, curseg) \
75 (START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff)
76
Jaegeuk Kim7cd85582014-09-23 11:23:01 -070077#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr) ((blk_addr) - SEG0_BLKADDR(sbi))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090078#define GET_SEGNO_FROM_SEG0(sbi, blk_addr) \
79 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) >> sbi->log_blocks_per_seg)
Jaegeuk Kim491c0852014-02-04 13:01:10 +090080#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr) \
81 (GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (sbi->blocks_per_seg - 1))
82
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090083#define GET_SEGNO(sbi, blk_addr) \
Chao Yud4511882018-06-05 17:44:11 +080084 ((!is_valid_data_blkaddr(sbi, blk_addr)) ? \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +090085 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)
91
92#define GET_SUM_BLOCK(sbi, segno) \
93 ((sbi->sm_info->ssa_blkaddr) + segno)
94
95#define GET_SUM_TYPE(footer) ((footer)->entry_type)
96#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = type)
97
98#define SIT_ENTRY_OFFSET(sit_i, segno) \
99 (segno % sit_i->sents_per_block)
Chao Yud3a14af2014-09-04 18:11:47 +0800100#define SIT_BLOCK_OFFSET(segno) \
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900101 (segno / SIT_ENTRY_PER_BLOCK)
Chao Yud3a14af2014-09-04 18:11:47 +0800102#define START_SEGNO(segno) \
103 (SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
Chao Yu74de5932013-11-22 09:09:59 +0800104#define SIT_BLK_CNT(sbi) \
Jaegeuk Kim7cd85582014-09-23 11:23:01 -0700105 ((MAIN_SEGS(sbi) + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900106#define f2fs_bitmap_size(nr) \
107 (BITS_TO_LONGS(nr) * sizeof(unsigned long))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900108
Chao Yu55cf9cb2014-09-15 18:01:10 +0800109#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)
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700113#define MAX_BIO_BLOCKS(sbi) \
114 ((int)min((int)max_hw_blocks(sbi), BIO_MAX_PAGES))
Jaegeuk Kim3cd8a232012-12-10 09:26:05 +0900115
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900116/*
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.
120 */
121enum {
122 ALLOC_RIGHT = 0,
123 ALLOC_LEFT
124};
125
126/*
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.
130 */
131enum {
132 LFS = 0,
133 SSR
134};
135
136/*
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.
140 */
141enum {
142 GC_CB = 0,
143 GC_GREEDY
144};
145
146/*
147 * BG_GC means the background cleaning job.
148 * FG_GC means the on-demand cleaning job.
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700149 * FORCE_FG_GC means on-demand cleaning job in background.
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900150 */
151enum {
152 BG_GC = 0,
Jaegeuk Kim6aefd932015-10-05 11:02:54 -0700153 FG_GC,
154 FORCE_FG_GC,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900155};
156
157/* for a function parameter to select a victim segment */
158struct 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 */
Jin Xua26b7c82013-09-05 12:45:26 +0800162 unsigned int max_search; /* maximum # of segments to search */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900163 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 */
167};
168
169struct seg_entry {
Chao Yuf51b4ce2016-05-04 23:19:48 +0800170 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 */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900174 unsigned char *cur_valid_map; /* validity bitmap of blocks */
175 /*
176 * # of valid blocks and the validity bitmap stored in the the last
177 * checkpoint pack. This information is used by the SSR mode.
178 */
Chao Yuf51b4ce2016-05-04 23:19:48 +0800179 unsigned char *ckpt_valid_map; /* validity bitmap of blocks last cp */
Jaegeuk Kima66cdd92015-04-30 22:37:50 -0700180 unsigned char *discard_map;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900181 unsigned long long mtime; /* modification time of the segment */
182};
183
184struct sec_entry {
185 unsigned int valid_blocks; /* # of valid blocks in a section */
186};
187
188struct segment_allocation {
189 void (*allocate_segment)(struct f2fs_sb_info *, int, bool);
190};
191
Chao Yudecd36b2015-08-07 18:42:09 +0800192/*
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.
195 */
Jaegeuk Kimd48dfc22016-01-29 16:21:15 -0800196#define ATOMIC_WRITTEN_PAGE ((unsigned long)-1)
Chao Yudecd36b2015-08-07 18:42:09 +0800197
198#define IS_ATOMIC_WRITTEN_PAGE(page) \
199 (page_private(page) == (unsigned long)ATOMIC_WRITTEN_PAGE)
200
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700201struct inmem_pages {
202 struct list_head list;
203 struct page *page;
Chao Yu28bc1062016-02-06 14:40:34 +0800204 block_t old_addr; /* for revoking when fail to commit */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700205};
206
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900207struct sit_info {
208 const struct segment_allocation *s_ops;
209
210 block_t sit_base_addr; /* start block address of SIT area */
211 block_t sit_blocks; /* # of blocks used by SIT area */
212 block_t written_valid_blocks; /* # of valid blocks in main area */
213 char *sit_bitmap; /* SIT bitmap pointer */
214 unsigned int bitmap_size; /* SIT bitmap size */
215
Jaegeuk Kim60a3b782015-02-10 16:44:29 -0800216 unsigned long *tmp_map; /* bitmap for temporal use */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900217 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 */
223
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 */
229};
230
231struct 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 */
Chao Yu1a118cc2015-02-11 18:20:38 +0800235 spinlock_t segmap_lock; /* free segmap lock */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900236 unsigned long *free_segmap; /* free segment bitmap */
237 unsigned long *free_secmap; /* free section bitmap */
238};
239
240/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
241enum dirty_type {
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 */
250 NR_DIRTY_TYPE
251};
252
253struct 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 */
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900258 unsigned long *victim_secmap; /* background GC victims */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900259};
260
261/* victim selection function for cleaning and SSR */
262struct victim_selection {
263 int (*get_victim)(struct f2fs_sb_info *, unsigned int *,
264 int, int, char);
265};
266
267/* for active log information */
268struct curseg_info {
269 struct mutex curseg_mutex; /* lock for consistency */
270 struct f2fs_summary_block *sum_blk; /* cached summary block */
Chao Yub7ad7512016-02-19 18:08:46 +0800271 struct rw_semaphore journal_rwsem; /* protect journal area */
272 struct f2fs_journal *journal; /* cached journal info */
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900273 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 */
278};
279
Chao Yu184a5cd2014-09-04 18:13:01 +0800280struct 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 */
284};
285
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900286/*
287 * inline functions
288 */
289static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
290{
291 return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
292}
293
294static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
295 unsigned int segno)
296{
297 struct sit_info *sit_i = SIT_I(sbi);
298 return &sit_i->sentries[segno];
299}
300
301static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
302 unsigned int segno)
303{
304 struct sit_info *sit_i = SIT_I(sbi);
305 return &sit_i->sec_entries[GET_SECNO(sbi, segno)];
306}
307
308static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
309 unsigned int segno, int section)
310{
311 /*
312 * In order to get # of valid blocks in a section instantly from many
313 * segments, f2fs manages two counting structures separately.
314 */
315 if (section > 1)
316 return get_sec_entry(sbi, segno)->valid_blocks;
317 else
318 return get_seg_entry(sbi, segno)->valid_blocks;
319}
320
321static inline void seg_info_from_raw_sit(struct seg_entry *se,
322 struct f2fs_sit_entry *rs)
323{
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);
330}
331
332static inline void seg_info_to_raw_sit(struct seg_entry *se,
333 struct f2fs_sit_entry *rs)
334{
335 unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
336 se->valid_blocks;
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);
342}
343
344static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
345 unsigned int max, unsigned int segno)
346{
347 unsigned int ret;
Chao Yu1a118cc2015-02-11 18:20:38 +0800348 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900349 ret = find_next_bit(free_i->free_segmap, max, segno);
Chao Yu1a118cc2015-02-11 18:20:38 +0800350 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900351 return ret;
352}
353
354static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
355{
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;
359 unsigned int next;
360
Chao Yu1a118cc2015-02-11 18:20:38 +0800361 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900362 clear_bit(segno, free_i->free_segmap);
363 free_i->free_segments++;
364
Wanpeng Li7fd97012015-03-06 15:00:55 +0800365 next = find_next_bit(free_i->free_segmap,
366 start_segno + sbi->segs_per_sec, start_segno);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900367 if (next >= start_segno + sbi->segs_per_sec) {
368 clear_bit(secno, free_i->free_secmap);
369 free_i->free_sections++;
370 }
Chao Yu1a118cc2015-02-11 18:20:38 +0800371 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900372}
373
374static inline void __set_inuse(struct f2fs_sb_info *sbi,
375 unsigned int segno)
376{
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--;
383}
384
385static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
386 unsigned int segno)
387{
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;
391 unsigned int next;
392
Chao Yu1a118cc2015-02-11 18:20:38 +0800393 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900394 if (test_and_clear_bit(segno, free_i->free_segmap)) {
395 free_i->free_segments++;
396
Yunlong Song4af55602018-07-12 23:09:26 +0800397 if (IS_CURSEC(sbi, secno))
398 goto skip_free;
Chao Yuf1121ab2014-07-14 16:45:15 +0800399 next = find_next_bit(free_i->free_segmap,
400 start_segno + sbi->segs_per_sec, start_segno);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900401 if (next >= start_segno + sbi->segs_per_sec) {
402 if (test_and_clear_bit(secno, free_i->free_secmap))
403 free_i->free_sections++;
404 }
405 }
Yunlong Song4af55602018-07-12 23:09:26 +0800406skip_free:
Chao Yu1a118cc2015-02-11 18:20:38 +0800407 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900408}
409
410static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
411 unsigned int segno)
412{
413 struct free_segmap_info *free_i = FREE_I(sbi);
414 unsigned int secno = segno / sbi->segs_per_sec;
Chao Yu1a118cc2015-02-11 18:20:38 +0800415 spin_lock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900416 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--;
420 }
Chao Yu1a118cc2015-02-11 18:20:38 +0800421 spin_unlock(&free_i->segmap_lock);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900422}
423
424static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
425 void *dst_addr)
426{
427 struct sit_info *sit_i = SIT_I(sbi);
428 memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
429}
430
431static inline block_t written_block_count(struct f2fs_sb_info *sbi)
432{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +0900433 return SIT_I(sbi)->written_valid_blocks;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900434}
435
436static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
437{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +0900438 return FREE_I(sbi)->free_segments;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900439}
440
441static inline int reserved_segments(struct f2fs_sb_info *sbi)
442{
443 return SM_I(sbi)->reserved_segments;
444}
445
446static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
447{
Jaegeuk Kim8b8343f2014-02-24 13:00:13 +0900448 return FREE_I(sbi)->free_sections;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900449}
450
451static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
452{
453 return DIRTY_I(sbi)->nr_dirty[PRE];
454}
455
456static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
457{
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];
464}
465
466static inline int overprovision_segments(struct f2fs_sb_info *sbi)
467{
468 return SM_I(sbi)->ovp_segments;
469}
470
471static inline int overprovision_sections(struct f2fs_sb_info *sbi)
472{
473 return ((unsigned int) overprovision_segments(sbi)) / sbi->segs_per_sec;
474}
475
476static inline int reserved_sections(struct f2fs_sb_info *sbi)
477{
478 return ((unsigned int) reserved_segments(sbi)) / sbi->segs_per_sec;
479}
480
481static inline bool need_SSR(struct f2fs_sb_info *sbi)
482{
Jaegeuk Kim95dd8972014-09-17 17:52:58 -0700483 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
484 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700485
486 if (test_opt(sbi, LFS))
487 return false;
488
Jaegeuk Kim95dd8972014-09-17 17:52:58 -0700489 return free_sections(sbi) <= (node_secs + 2 * dent_secs +
490 reserved_sections(sbi) + 1);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900491}
492
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700493static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
494 int freed, int needed)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900495{
Namjae Jeon5ac206c2013-02-02 23:52:59 +0900496 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
497 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
Jaegeuk Kim43727522013-02-04 15:11:17 +0900498
Jaegeuk Kim0f18b462016-05-20 11:10:10 -0700499 node_secs += get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
500
Chao Yucaf00472015-01-28 17:48:42 +0800501 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
Jaegeuk Kim029cd282012-12-21 17:20:21 +0900502 return false;
503
Jaegeuk Kim7f3037a2016-09-01 12:02:51 -0700504 return (free_sections(sbi) + freed) <=
505 (node_secs + 2 * dent_secs + reserved_sections(sbi) + needed);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900506}
507
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +0900508static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
509{
Chris Fries6c311ec2014-01-17 14:44:39 -0600510 return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
Jaegeuk Kim81eb8d62013-10-24 13:31:34 +0900511}
512
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900513static inline int utilization(struct f2fs_sb_info *sbi)
514{
Chris Fries6c311ec2014-01-17 14:44:39 -0600515 return div_u64((u64)valid_user_blocks(sbi) * 100,
516 sbi->user_block_count);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900517}
518
519/*
520 * Sometimes f2fs may be better to drop out-of-place update policy.
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900521 * 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
527 * threashold,
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700528 * 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.
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900531 * F2FS_IPUT_DISABLE - disable IPU. (=default option)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900532 */
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900533#define DEF_MIN_IPU_UTIL 70
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700534#define DEF_MIN_FSYNC_BLOCKS 8
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900535
536enum {
537 F2FS_IPU_FORCE,
538 F2FS_IPU_SSR,
539 F2FS_IPU_UTIL,
540 F2FS_IPU_SSR_UTIL,
Jaegeuk Kimc1ce1b02014-09-10 16:53:02 -0700541 F2FS_IPU_FSYNC,
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900542};
543
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900544static inline bool need_inplace_update(struct inode *inode)
545{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700546 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -0700547 unsigned int policy = SM_I(sbi)->ipu_policy;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900548
549 /* IPU can be done only for the user data */
Jaegeuk Kim88b88a62014-10-06 17:39:50 -0700550 if (S_ISDIR(inode->i_mode) || f2fs_is_atomic_file(inode))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900551 return false;
Jaegeuk Kim216fbd62013-11-07 13:13:42 +0900552
Jaegeuk Kim36abef42016-06-03 19:29:38 -0700553 if (test_opt(sbi, LFS))
554 return false;
555
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -0700556 if (policy & (0x1 << F2FS_IPU_FORCE))
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900557 return true;
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -0700558 if (policy & (0x1 << F2FS_IPU_SSR) && need_SSR(sbi))
559 return true;
560 if (policy & (0x1 << F2FS_IPU_UTIL) &&
561 utilization(sbi) > SM_I(sbi)->min_ipu_util)
562 return true;
563 if (policy & (0x1 << F2FS_IPU_SSR_UTIL) && need_SSR(sbi) &&
564 utilization(sbi) > SM_I(sbi)->min_ipu_util)
565 return true;
566
567 /* this is only set during fdatasync */
568 if (policy & (0x1 << F2FS_IPU_FSYNC) &&
Jaegeuk Kim91942322016-05-20 10:13:22 -0700569 is_inode_flag_set(inode, FI_NEED_IPU))
Jaegeuk Kim9b5f1362014-09-16 18:30:54 -0700570 return true;
571
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900572 return false;
573}
574
575static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
576 int type)
577{
578 struct curseg_info *curseg = CURSEG_I(sbi, type);
579 return curseg->segno;
580}
581
582static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
583 int type)
584{
585 struct curseg_info *curseg = CURSEG_I(sbi, type);
586 return curseg->alloc_type;
587}
588
589static inline unsigned short curseg_blkoff(struct f2fs_sb_info *sbi, int type)
590{
591 struct curseg_info *curseg = CURSEG_I(sbi, type);
592 return curseg->next_blkoff;
593}
594
595static inline void check_seg_range(struct f2fs_sb_info *sbi, unsigned int segno)
596{
Liu Xue7a04f642015-07-27 10:17:59 +0000597 f2fs_bug_on(sbi, segno > TOTAL_SEGS(sbi) - 1);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900598}
599
Yunlei He89d78cf2018-03-08 16:29:13 +0800600static inline void verify_block_addr(struct f2fs_io_info *fio, block_t blk_addr)
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900601{
Yunlei He89d78cf2018-03-08 16:29:13 +0800602 struct f2fs_sb_info *sbi = fio->sbi;
603
Chao Yuaafb3712018-08-01 19:13:44 +0800604 if (__is_meta_io(fio))
Chao Yud4511882018-06-05 17:44:11 +0800605 verify_blkaddr(sbi, blk_addr, META_GENERIC);
Yunlei He89d78cf2018-03-08 16:29:13 +0800606 else
Chao Yud4511882018-06-05 17:44:11 +0800607 verify_blkaddr(sbi, blk_addr, DATA_GENERIC);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900608}
609
610/*
arter97e1c42042014-08-06 23:22:50 +0900611 * Summary block is always treated as an invalid block
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900612 */
Jaegeuk Kim3fd96202017-12-19 19:16:34 -0800613static inline int check_block_count(struct f2fs_sb_info *sbi,
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900614 int segno, struct f2fs_sit_entry *raw_sit)
615{
Chao Yu44c60bf2013-10-29 14:50:40 +0800616 bool is_valid = test_bit_le(0, raw_sit->valid_map) ? true : false;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900617 int valid_blocks = 0;
Chao Yu44c60bf2013-10-29 14:50:40 +0800618 int cur_pos = 0, next_pos;
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900619
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900620 /* check bitmap with valid block count */
Chao Yu44c60bf2013-10-29 14:50:40 +0800621 do {
622 if (is_valid) {
623 next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
624 sbi->blocks_per_seg,
625 cur_pos);
626 valid_blocks += next_pos - cur_pos;
627 } else
628 next_pos = find_next_bit_le(&raw_sit->valid_map,
629 sbi->blocks_per_seg,
630 cur_pos);
631 cur_pos = next_pos;
632 is_valid = !is_valid;
633 } while (cur_pos < sbi->blocks_per_seg);
Jaegeuk Kim3fd96202017-12-19 19:16:34 -0800634
635 if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
636 f2fs_msg(sbi->sb, KERN_ERR,
637 "Mismatch valid blocks %d vs. %d",
638 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
639 set_sbi_flag(sbi, SBI_NEED_FSCK);
640 return -EINVAL;
641 }
Chao Yudff15a22019-04-15 15:30:51 +0800642
Jaegeuk Kim4c278392015-08-11 16:01:30 -0700643 /* check segment usage, and check boundary of a given segment number */
Jaegeuk Kim3fd96202017-12-19 19:16:34 -0800644 if (unlikely(GET_SIT_VBLOCKS(raw_sit) > sbi->blocks_per_seg
645 || segno > TOTAL_SEGS(sbi) - 1)) {
646 f2fs_msg(sbi->sb, KERN_ERR,
647 "Wrong valid blocks %d or segno %u",
648 GET_SIT_VBLOCKS(raw_sit), segno);
649 set_sbi_flag(sbi, SBI_NEED_FSCK);
650 return -EINVAL;
651 }
652 return 0;
Liu Xue7a04f642015-07-27 10:17:59 +0000653}
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900654
655static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
656 unsigned int start)
657{
658 struct sit_info *sit_i = SIT_I(sbi);
Chao Yud3a14af2014-09-04 18:11:47 +0800659 unsigned int offset = SIT_BLOCK_OFFSET(start);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900660 block_t blk_addr = sit_i->sit_base_addr + offset;
661
662 check_seg_range(sbi, start);
663
664 /* calculate sit block address */
665 if (f2fs_test_bit(offset, sit_i->sit_bitmap))
666 blk_addr += sit_i->sit_blocks;
667
668 return blk_addr;
669}
670
671static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
672 pgoff_t block_addr)
673{
674 struct sit_info *sit_i = SIT_I(sbi);
675 block_addr -= sit_i->sit_base_addr;
676 if (block_addr < sit_i->sit_blocks)
677 block_addr += sit_i->sit_blocks;
678 else
679 block_addr -= sit_i->sit_blocks;
680
681 return block_addr + sit_i->sit_base_addr;
682}
683
684static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
685{
Chao Yud3a14af2014-09-04 18:11:47 +0800686 unsigned int block_off = SIT_BLOCK_OFFSET(start);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900687
Gu Zhengc6ac4c02014-10-20 17:45:50 +0800688 f2fs_change_bit(block_off, sit_i->sit_bitmap);
Jaegeuk Kim39a53e02012-11-28 13:37:31 +0900689}
690
691static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi)
692{
693 struct sit_info *sit_i = SIT_I(sbi);
694 return sit_i->elapsed_time + CURRENT_TIME_SEC.tv_sec -
695 sit_i->mounted_time;
696}
697
698static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
699 unsigned int ofs_in_node, unsigned char version)
700{
701 sum->nid = cpu_to_le32(nid);
702 sum->ofs_in_node = cpu_to_le16(ofs_in_node);
703 sum->version = version;
704}
705
706static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
707{
708 return __start_cp_addr(sbi) +
709 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
710}
711
712static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
713{
714 return __start_cp_addr(sbi) +
715 le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
716 - (base + 1) + type;
717}
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900718
Hou Pengyang4992ba22017-02-16 12:34:31 +0000719static inline bool no_fggc_candidate(struct f2fs_sb_info *sbi,
720 unsigned int secno)
721{
722 if (get_valid_blocks(sbi, secno, sbi->segs_per_sec) >=
723 sbi->fggc_threshold)
724 return true;
725 return false;
726}
727
Jaegeuk Kim5ec4e492013-03-31 13:26:03 +0900728static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
729{
730 if (IS_CURSEC(sbi, secno) || (sbi->cur_victim_sec == secno))
731 return true;
732 return false;
733}
Jaegeuk Kimac5d1562013-04-29 16:58:39 +0900734
735static inline unsigned int max_hw_blocks(struct f2fs_sb_info *sbi)
736{
737 struct block_device *bdev = sbi->sb->s_bdev;
738 struct request_queue *q = bdev_get_queue(bdev);
Chao Yu55cf9cb2014-09-15 18:01:10 +0800739 return SECTOR_TO_BLOCK(queue_max_sectors(q));
Jaegeuk Kimac5d1562013-04-29 16:58:39 +0900740}
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900741
742/*
743 * It is very important to gather dirty pages and write at once, so that we can
744 * submit a big bio without interfering other data writes.
745 * By default, 512 pages for directory data,
746 * 512 pages (2MB) * 3 for three types of nodes, and
747 * max_bio_blocks for meta are set.
748 */
749static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
750{
Tejun Heoa88a3412015-05-22 17:13:28 -0400751 if (sbi->sb->s_bdi->wb.dirty_exceeded)
Jaegeuk Kim510184c2014-11-06 17:23:08 -0800752 return 0;
753
Jaegeuk Kima1257022015-10-08 10:40:07 -0700754 if (type == DATA)
755 return sbi->blocks_per_seg;
756 else if (type == NODE)
Jaegeuk Kim2c237eb2016-06-16 16:44:11 -0700757 return 8 * sbi->blocks_per_seg;
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900758 else if (type == META)
Jaegeuk Kim2c237eb2016-06-16 16:44:11 -0700759 return 8 * MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim87d6f892014-03-18 12:40:49 +0900760 else
761 return 0;
762}
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900763
764/*
765 * When writing pages, it'd better align nr_to_write for segment size.
766 */
767static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
768 struct writeback_control *wbc)
769{
770 long nr_to_write, desired;
771
772 if (wbc->sync_mode != WB_SYNC_NONE)
773 return 0;
774
775 nr_to_write = wbc->nr_to_write;
776
Jaegeuk Kim28ea6162016-05-25 17:17:56 -0700777 if (type == NODE)
Jaegeuk Kim2c237eb2016-06-16 16:44:11 -0700778 desired = 2 * max_hw_blocks(sbi);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900779 else
Jaegeuk Kim90a893c2014-09-22 16:21:07 -0700780 desired = MAX_BIO_BLOCKS(sbi);
Jaegeuk Kim50c8cdb2014-03-18 13:47:11 +0900781
782 wbc->nr_to_write = desired;
783 return desired - nr_to_write;
784}