File Index
Files and directories
Sign in
FILE

segment.h

File preview with syntax highlighting.

linux-6.19.14/fs/f2fs/segment.h C
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * fs/f2fs/segment.h
   4 *
   5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   6 *             http://www.samsung.com/
   7 */
   8#include <linux/blkdev.h>
   9#include <linux/backing-dev.h>
  10
  11/* constant macro */
  12#define NULL_SEGNO			((unsigned int)(~0))
  13#define NULL_SECNO			((unsigned int)(~0))
  14
  15#define DEF_RECLAIM_PREFREE_SEGMENTS	5	/* 5% over total segments */
  16#define DEF_MAX_RECLAIM_PREFREE_SEGMENTS	4096	/* 8GB in maximum */
  17
  18#define F2FS_MIN_SEGMENTS	9 /* SB + 2 (CP + SIT + NAT) + SSA + MAIN */
  19#define F2FS_MIN_META_SEGMENTS	8 /* SB + 2 (CP + SIT + NAT) + SSA */
  20
  21#define INVALID_MTIME ULLONG_MAX /* no valid blocks in a segment/section */
  22
  23/* L: Logical segment # in volume, R: Relative segment # in main area */
  24#define GET_L2R_SEGNO(free_i, segno)	((segno) - (free_i)->start_segno)
  25#define GET_R2L_SEGNO(free_i, segno)	((segno) + (free_i)->start_segno)
  26
  27#define IS_DATASEG(t)	((t) <= CURSEG_COLD_DATA)
  28#define IS_NODESEG(t)	((t) >= CURSEG_HOT_NODE && (t) <= CURSEG_COLD_NODE)
  29#define SE_PAGETYPE(se)	((IS_NODESEG((se)->type) ? NODE : DATA))
  30
  31static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
  32						unsigned short seg_type)
  33{
  34	f2fs_bug_on(sbi, seg_type >= NR_PERSISTENT_LOG);
  35}
  36
  37#define MAIN_BLKADDR(sbi)						\
  38	(SM_I(sbi) ? SM_I(sbi)->main_blkaddr : 				\
  39		le32_to_cpu(F2FS_RAW_SUPER(sbi)->main_blkaddr))
  40#define SEG0_BLKADDR(sbi)						\
  41	(SM_I(sbi) ? SM_I(sbi)->seg0_blkaddr : 				\
  42		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment0_blkaddr))
  43
  44#define MAIN_SEGS(sbi)	(SM_I(sbi)->main_segments)
  45#define MAIN_SECS(sbi)	((sbi)->total_sections)
  46
  47#define TOTAL_SEGS(sbi)							\
  48	(SM_I(sbi) ? SM_I(sbi)->segment_count : 				\
  49		le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count))
  50#define TOTAL_BLKS(sbi)	(SEGS_TO_BLKS(sbi, TOTAL_SEGS(sbi)))
  51
  52#define MAX_BLKADDR(sbi)	(SEG0_BLKADDR(sbi) + TOTAL_BLKS(sbi))
  53#define SEGMENT_SIZE(sbi)	(1ULL << ((sbi)->log_blocksize +	\
  54					(sbi)->log_blocks_per_seg))
  55
  56#define START_BLOCK(sbi, segno)	(SEG0_BLKADDR(sbi) +			\
  57	 (SEGS_TO_BLKS(sbi, GET_R2L_SEGNO(FREE_I(sbi), segno))))
  58
  59#define NEXT_FREE_BLKADDR(sbi, curseg)					\
  60	(START_BLOCK(sbi, (curseg)->segno) + (curseg)->next_blkoff)
  61
  62#define GET_SEGOFF_FROM_SEG0(sbi, blk_addr)	((blk_addr) - SEG0_BLKADDR(sbi))
  63#define GET_SEGNO_FROM_SEG0(sbi, blk_addr)				\
  64	(BLKS_TO_SEGS(sbi, GET_SEGOFF_FROM_SEG0(sbi, blk_addr)))
  65#define GET_BLKOFF_FROM_SEG0(sbi, blk_addr)				\
  66	(GET_SEGOFF_FROM_SEG0(sbi, blk_addr) & (BLKS_PER_SEG(sbi) - 1))
  67
  68#define GET_SEGNO(sbi, blk_addr)					\
  69	((!__is_valid_data_blkaddr(blk_addr)) ?			\
  70	NULL_SEGNO : GET_L2R_SEGNO(FREE_I(sbi),			\
  71		GET_SEGNO_FROM_SEG0(sbi, blk_addr)))
  72#ifdef CONFIG_BLK_DEV_ZONED
  73#define CAP_BLKS_PER_SEC(sbi)					\
  74	(BLKS_PER_SEC(sbi) - (sbi)->unusable_blocks_per_sec)
  75#define CAP_SEGS_PER_SEC(sbi)					\
  76	(SEGS_PER_SEC(sbi) -					\
  77	BLKS_TO_SEGS(sbi, (sbi)->unusable_blocks_per_sec))
  78#else
  79#define CAP_BLKS_PER_SEC(sbi) BLKS_PER_SEC(sbi)
  80#define CAP_SEGS_PER_SEC(sbi) SEGS_PER_SEC(sbi)
  81#endif
  82#define GET_START_SEG_FROM_SEC(sbi, segno)			\
  83	(rounddown(segno, SEGS_PER_SEC(sbi)))
  84#define GET_SEC_FROM_SEG(sbi, segno)				\
  85	(((segno) == -1) ? -1 : (segno) / SEGS_PER_SEC(sbi))
  86#define GET_SEG_FROM_SEC(sbi, secno)				\
  87	((secno) * SEGS_PER_SEC(sbi))
  88#define GET_ZONE_FROM_SEC(sbi, secno)				\
  89	(((secno) == -1) ? -1 : (secno) / (sbi)->secs_per_zone)
  90#define GET_ZONE_FROM_SEG(sbi, segno)				\
  91	GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
  92
  93#define GET_SUM_BLOCK(sbi, segno)	\
  94	(SM_I(sbi)->ssa_blkaddr + (segno / (sbi)->sums_per_block))
  95#define GET_SUM_BLKOFF(sbi, segno) (segno % (sbi)->sums_per_block)
  96#define SUM_BLK_PAGE_ADDR(sbi, folio, segno)	\
  97	(folio_address(folio) + GET_SUM_BLKOFF(sbi, segno) * (sbi)->sum_blocksize)
  98
  99#define GET_SUM_TYPE(footer) ((footer)->entry_type)
 100#define SET_SUM_TYPE(footer, type) ((footer)->entry_type = (type))
 101
 102#define SIT_ENTRY_OFFSET(sit_i, segno)					\
 103	((segno) % (sit_i)->sents_per_block)
 104#define SIT_BLOCK_OFFSET(segno)					\
 105	((segno) / SIT_ENTRY_PER_BLOCK)
 106#define	START_SEGNO(segno)		\
 107	(SIT_BLOCK_OFFSET(segno) * SIT_ENTRY_PER_BLOCK)
 108#define SIT_BLK_CNT(sbi)			\
 109	DIV_ROUND_UP(MAIN_SEGS(sbi), SIT_ENTRY_PER_BLOCK)
 110#define f2fs_bitmap_size(nr)			\
 111	(BITS_TO_LONGS(nr) * sizeof(unsigned long))
 112
 113#define SECTOR_FROM_BLOCK(blk_addr)					\
 114	(((sector_t)blk_addr) << F2FS_LOG_SECTORS_PER_BLOCK)
 115#define SECTOR_TO_BLOCK(sectors)					\
 116	((sectors) >> F2FS_LOG_SECTORS_PER_BLOCK)
 117
 118/*
 119 * In the victim_sel_policy->alloc_mode, there are three block allocation modes.
 120 * LFS writes data sequentially with cleaning operations.
 121 * SSR (Slack Space Recycle) reuses obsolete space without cleaning operations.
 122 * AT_SSR (Age Threshold based Slack Space Recycle) merges fragments into
 123 * fragmented segment which has similar aging degree.
 124 */
 125enum {
 126	LFS = 0,
 127	SSR,
 128	AT_SSR,
 129};
 130
 131/*
 132 * In the victim_sel_policy->gc_mode, there are three gc, aka cleaning, modes.
 133 * GC_CB is based on cost-benefit algorithm.
 134 * GC_GREEDY is based on greedy algorithm.
 135 * GC_AT is based on age-threshold algorithm.
 136 */
 137enum {
 138	GC_CB = 0,
 139	GC_GREEDY,
 140	GC_AT,
 141	ALLOC_NEXT,
 142	FLUSH_DEVICE,
 143	MAX_GC_POLICY,
 144};
 145
 146/*
 147 * BG_GC means the background cleaning job.
 148 * FG_GC means the on-demand cleaning job.
 149 */
 150enum {
 151	BG_GC = 0,
 152	FG_GC,
 153};
 154
 155/* for a function parameter to select a victim segment */
 156struct victim_sel_policy {
 157	int alloc_mode;			/* LFS or SSR */
 158	int gc_mode;			/* GC_CB or GC_GREEDY */
 159	unsigned long *dirty_bitmap;	/* dirty segment/section bitmap */
 160	unsigned int max_search;	/*
 161					 * maximum # of segments/sections
 162					 * to search
 163					 */
 164	unsigned int offset;		/* last scanned bitmap offset */
 165	unsigned int ofs_unit;		/* bitmap search unit */
 166	unsigned int min_cost;		/* minimum cost */
 167	unsigned long long oldest_age;	/* oldest age of segments having the same min cost */
 168	unsigned int min_segno;		/* segment # having min. cost */
 169	unsigned long long age;		/* mtime of GCed section*/
 170	unsigned long long age_threshold;/* age threshold */
 171	bool one_time_gc;		/* one time GC */
 172};
 173
 174struct seg_entry {
 175	unsigned int type:6;		/* segment type like CURSEG_XXX_TYPE */
 176	unsigned int valid_blocks:10;	/* # of valid blocks */
 177	unsigned int ckpt_valid_blocks:10;	/* # of valid blocks last cp */
 178	unsigned int padding:6;		/* padding */
 179	unsigned char *cur_valid_map;	/* validity bitmap of blocks */
 180#ifdef CONFIG_F2FS_CHECK_FS
 181	unsigned char *cur_valid_map_mir;	/* mirror of current valid bitmap */
 182#endif
 183	/*
 184	 * # of valid blocks and the validity bitmap stored in the last
 185	 * checkpoint pack. This information is used by the SSR mode.
 186	 */
 187	unsigned char *ckpt_valid_map;	/* validity bitmap of blocks last cp */
 188	unsigned char *discard_map;
 189	unsigned long long mtime;	/* modification time of the segment */
 190};
 191
 192struct sec_entry {
 193	unsigned int valid_blocks;	/* # of valid blocks in a section */
 194	unsigned int ckpt_valid_blocks; /* # of valid blocks last cp in a section */
 195};
 196
 197#define MAX_SKIP_GC_COUNT			16
 198
 199struct revoke_entry {
 200	struct list_head list;
 201	block_t old_addr;		/* for revoking when fail to commit */
 202	pgoff_t index;
 203};
 204
 205struct sit_info {
 206	block_t sit_base_addr;		/* start block address of SIT area */
 207	block_t sit_blocks;		/* # of blocks used by SIT area */
 208	block_t written_valid_blocks;	/* # of valid blocks in main area */
 209	char *bitmap;			/* all bitmaps pointer */
 210	char *sit_bitmap;		/* SIT bitmap pointer */
 211#ifdef CONFIG_F2FS_CHECK_FS
 212	char *sit_bitmap_mir;		/* SIT bitmap mirror */
 213
 214	/* bitmap of segments to be ignored by GC in case of errors */
 215	unsigned long *invalid_segmap;
 216#endif
 217	unsigned int bitmap_size;	/* SIT bitmap size */
 218
 219	unsigned long *tmp_map;			/* bitmap for temporal use */
 220	unsigned long *dirty_sentries_bitmap;	/* bitmap for dirty sentries */
 221	unsigned int dirty_sentries;		/* # of dirty sentries */
 222	unsigned int sents_per_block;		/* # of SIT entries per block */
 223	struct rw_semaphore sentry_lock;	/* to protect SIT cache */
 224	struct seg_entry *sentries;		/* SIT segment-level cache */
 225	struct sec_entry *sec_entries;		/* SIT section-level cache */
 226
 227	/* for cost-benefit algorithm in cleaning procedure */
 228	unsigned long long elapsed_time;	/* elapsed time after mount */
 229	unsigned long long mounted_time;	/* mount time */
 230	unsigned long long min_mtime;		/* min. modification time */
 231	unsigned long long max_mtime;		/* max. modification time */
 232	unsigned long long dirty_min_mtime;	/* rerange candidates in GC_AT */
 233	unsigned long long dirty_max_mtime;	/* rerange candidates in GC_AT */
 234
 235	unsigned int last_victim[MAX_GC_POLICY]; /* last victim segment # */
 236};
 237
 238struct free_segmap_info {
 239	unsigned int start_segno;	/* start segment number logically */
 240	unsigned int free_segments;	/* # of free segments */
 241	unsigned int free_sections;	/* # of free sections */
 242	spinlock_t segmap_lock;		/* free segmap lock */
 243	unsigned long *free_segmap;	/* free segment bitmap */
 244	unsigned long *free_secmap;	/* free section bitmap */
 245};
 246
 247/* Notice: The order of dirty type is same with CURSEG_XXX in f2fs.h */
 248enum dirty_type {
 249	DIRTY_HOT_DATA,		/* dirty segments assigned as hot data logs */
 250	DIRTY_WARM_DATA,	/* dirty segments assigned as warm data logs */
 251	DIRTY_COLD_DATA,	/* dirty segments assigned as cold data logs */
 252	DIRTY_HOT_NODE,		/* dirty segments assigned as hot node logs */
 253	DIRTY_WARM_NODE,	/* dirty segments assigned as warm node logs */
 254	DIRTY_COLD_NODE,	/* dirty segments assigned as cold node logs */
 255	DIRTY,			/* to count # of dirty segments */
 256	PRE,			/* to count # of entirely obsolete segments */
 257	NR_DIRTY_TYPE
 258};
 259
 260struct dirty_seglist_info {
 261	unsigned long *dirty_segmap[NR_DIRTY_TYPE];
 262	unsigned long *dirty_secmap;
 263	struct mutex seglist_lock;		/* lock for segment bitmaps */
 264	int nr_dirty[NR_DIRTY_TYPE];		/* # of dirty segments */
 265	unsigned long *victim_secmap;		/* background GC victims */
 266	unsigned long *pinned_secmap;		/* pinned victims from foreground GC */
 267	unsigned int pinned_secmap_cnt;		/* count of victims which has pinned data */
 268	bool enable_pin_section;		/* enable pinning section */
 269};
 270
 271/* for active log information */
 272struct curseg_info {
 273	struct mutex curseg_mutex;		/* lock for consistency */
 274	struct f2fs_summary_block *sum_blk;	/* cached summary block */
 275	struct rw_semaphore journal_rwsem;	/* protect journal area */
 276	struct f2fs_journal *journal;		/* cached journal info */
 277	unsigned char alloc_type;		/* current allocation type */
 278	unsigned short seg_type;		/* segment type like CURSEG_XXX_TYPE */
 279	unsigned int segno;			/* current segment number */
 280	unsigned short next_blkoff;		/* next block offset to write */
 281	unsigned int zone;			/* current zone number */
 282	unsigned int next_segno;		/* preallocated segment */
 283	int fragment_remained_chunk;		/* remained block size in a chunk for block fragmentation mode */
 284	bool inited;				/* indicate inmem log is inited */
 285};
 286
 287struct sit_entry_set {
 288	struct list_head set_list;	/* link with all sit sets */
 289	unsigned int start_segno;	/* start segno of sits in set */
 290	unsigned int entry_cnt;		/* the # of sit entries in set */
 291};
 292
 293/*
 294 * inline functions
 295 */
 296static inline struct curseg_info *CURSEG_I(struct f2fs_sb_info *sbi, int type)
 297{
 298	return (struct curseg_info *)(SM_I(sbi)->curseg_array + type);
 299}
 300
 301static inline bool is_curseg(struct f2fs_sb_info *sbi, unsigned int segno)
 302{
 303	int i;
 304
 305	for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
 306		if (segno == CURSEG_I(sbi, i)->segno)
 307			return true;
 308	}
 309	return false;
 310}
 311
 312static inline bool is_cursec(struct f2fs_sb_info *sbi, unsigned int secno)
 313{
 314	int i;
 315
 316	for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
 317		if (secno == GET_SEC_FROM_SEG(sbi, CURSEG_I(sbi, i)->segno))
 318			return true;
 319	}
 320	return false;
 321}
 322
 323static inline struct seg_entry *get_seg_entry(struct f2fs_sb_info *sbi,
 324						unsigned int segno)
 325{
 326	struct sit_info *sit_i = SIT_I(sbi);
 327	return &sit_i->sentries[segno];
 328}
 329
 330static inline struct sec_entry *get_sec_entry(struct f2fs_sb_info *sbi,
 331						unsigned int segno)
 332{
 333	struct sit_info *sit_i = SIT_I(sbi);
 334	return &sit_i->sec_entries[GET_SEC_FROM_SEG(sbi, segno)];
 335}
 336
 337static inline unsigned int get_valid_blocks(struct f2fs_sb_info *sbi,
 338				unsigned int segno, bool use_section)
 339{
 340	/*
 341	 * In order to get # of valid blocks in a section instantly from many
 342	 * segments, f2fs manages two counting structures separately.
 343	 */
 344	if (use_section && __is_large_section(sbi))
 345		return get_sec_entry(sbi, segno)->valid_blocks;
 346	else
 347		return get_seg_entry(sbi, segno)->valid_blocks;
 348}
 349
 350static inline unsigned int get_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
 351				unsigned int segno, bool use_section)
 352{
 353	if (use_section && __is_large_section(sbi))
 354		return get_sec_entry(sbi, segno)->ckpt_valid_blocks;
 355	else
 356		return get_seg_entry(sbi, segno)->ckpt_valid_blocks;
 357}
 358
 359static inline void set_ckpt_valid_blocks(struct f2fs_sb_info *sbi,
 360		unsigned int segno)
 361{
 362	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 363	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
 364	unsigned int blocks = 0;
 365	int i;
 366
 367	for (i = 0; i < SEGS_PER_SEC(sbi); i++, start_segno++) {
 368		struct seg_entry *se = get_seg_entry(sbi, start_segno);
 369
 370		blocks += se->ckpt_valid_blocks;
 371	}
 372	get_sec_entry(sbi, segno)->ckpt_valid_blocks = blocks;
 373}
 374
 375#ifdef CONFIG_F2FS_CHECK_FS
 376static inline void sanity_check_valid_blocks(struct f2fs_sb_info *sbi,
 377		unsigned int segno)
 378{
 379	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 380	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
 381	unsigned int blocks = 0;
 382	int i;
 383
 384	for (i = 0; i < SEGS_PER_SEC(sbi); i++, start_segno++) {
 385		struct seg_entry *se = get_seg_entry(sbi, start_segno);
 386
 387		blocks += se->ckpt_valid_blocks;
 388	}
 389
 390	if (blocks != get_sec_entry(sbi, segno)->ckpt_valid_blocks) {
 391		f2fs_err(sbi,
 392			"Inconsistent ckpt valid blocks: "
 393			"seg entry(%d) vs sec entry(%d) at secno %d",
 394			blocks, get_sec_entry(sbi, segno)->ckpt_valid_blocks, secno);
 395		f2fs_bug_on(sbi, 1);
 396	}
 397}
 398#else
 399static inline void sanity_check_valid_blocks(struct f2fs_sb_info *sbi,
 400			unsigned int segno)
 401{
 402}
 403#endif
 404static inline void seg_info_from_raw_sit(struct seg_entry *se,
 405					struct f2fs_sit_entry *rs)
 406{
 407	se->valid_blocks = GET_SIT_VBLOCKS(rs);
 408	se->ckpt_valid_blocks = GET_SIT_VBLOCKS(rs);
 409	memcpy(se->cur_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
 410	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
 411#ifdef CONFIG_F2FS_CHECK_FS
 412	memcpy(se->cur_valid_map_mir, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
 413#endif
 414	se->type = GET_SIT_TYPE(rs);
 415	se->mtime = le64_to_cpu(rs->mtime);
 416}
 417
 418static inline void __seg_info_to_raw_sit(struct seg_entry *se,
 419					struct f2fs_sit_entry *rs)
 420{
 421	unsigned short raw_vblocks = (se->type << SIT_VBLOCKS_SHIFT) |
 422					se->valid_blocks;
 423	rs->vblocks = cpu_to_le16(raw_vblocks);
 424	memcpy(rs->valid_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE);
 425	rs->mtime = cpu_to_le64(se->mtime);
 426}
 427
 428static inline void seg_info_to_sit_folio(struct f2fs_sb_info *sbi,
 429				struct folio *folio, unsigned int start)
 430{
 431	struct f2fs_sit_block *raw_sit;
 432	struct seg_entry *se;
 433	struct f2fs_sit_entry *rs;
 434	unsigned int end = min(start + SIT_ENTRY_PER_BLOCK,
 435					(unsigned long)MAIN_SEGS(sbi));
 436	int i;
 437
 438	raw_sit = folio_address(folio);
 439	memset(raw_sit, 0, PAGE_SIZE);
 440	for (i = 0; i < end - start; i++) {
 441		rs = &raw_sit->entries[i];
 442		se = get_seg_entry(sbi, start + i);
 443		__seg_info_to_raw_sit(se, rs);
 444	}
 445}
 446
 447static inline void seg_info_to_raw_sit(struct seg_entry *se,
 448					struct f2fs_sit_entry *rs)
 449{
 450	__seg_info_to_raw_sit(se, rs);
 451
 452	memcpy(se->ckpt_valid_map, rs->valid_map, SIT_VBLOCK_MAP_SIZE);
 453	se->ckpt_valid_blocks = se->valid_blocks;
 454}
 455
 456static inline unsigned int find_next_inuse(struct free_segmap_info *free_i,
 457		unsigned int max, unsigned int segno)
 458{
 459	unsigned int ret;
 460	spin_lock(&free_i->segmap_lock);
 461	ret = find_next_bit(free_i->free_segmap, max, segno);
 462	spin_unlock(&free_i->segmap_lock);
 463	return ret;
 464}
 465
 466static inline void __set_free(struct f2fs_sb_info *sbi, unsigned int segno)
 467{
 468	struct free_segmap_info *free_i = FREE_I(sbi);
 469	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 470	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
 471	unsigned int next;
 472
 473	spin_lock(&free_i->segmap_lock);
 474	clear_bit(segno, free_i->free_segmap);
 475	free_i->free_segments++;
 476
 477	next = find_next_bit(free_i->free_segmap,
 478			start_segno + SEGS_PER_SEC(sbi), start_segno);
 479	if (next >= start_segno + f2fs_usable_segs_in_sec(sbi)) {
 480		clear_bit(secno, free_i->free_secmap);
 481		free_i->free_sections++;
 482	}
 483	spin_unlock(&free_i->segmap_lock);
 484}
 485
 486static inline void __set_inuse(struct f2fs_sb_info *sbi,
 487		unsigned int segno)
 488{
 489	struct free_segmap_info *free_i = FREE_I(sbi);
 490	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 491
 492	set_bit(segno, free_i->free_segmap);
 493	free_i->free_segments--;
 494	if (!test_and_set_bit(secno, free_i->free_secmap))
 495		free_i->free_sections--;
 496}
 497
 498static inline void __set_test_and_free(struct f2fs_sb_info *sbi,
 499		unsigned int segno, bool inmem)
 500{
 501	struct free_segmap_info *free_i = FREE_I(sbi);
 502	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 503	unsigned int start_segno = GET_SEG_FROM_SEC(sbi, secno);
 504	unsigned int next;
 505	bool ret;
 506
 507	spin_lock(&free_i->segmap_lock);
 508	ret = test_and_clear_bit(segno, free_i->free_segmap);
 509	if (!ret)
 510		goto unlock_out;
 511
 512	free_i->free_segments++;
 513
 514	if (!inmem && is_cursec(sbi, secno))
 515		goto unlock_out;
 516
 517	/* check large section */
 518	next = find_next_bit(free_i->free_segmap,
 519			     start_segno + SEGS_PER_SEC(sbi), start_segno);
 520	if (next < start_segno + f2fs_usable_segs_in_sec(sbi))
 521		goto unlock_out;
 522
 523	ret = test_and_clear_bit(secno, free_i->free_secmap);
 524	if (!ret)
 525		goto unlock_out;
 526
 527	free_i->free_sections++;
 528
 529	if (GET_SEC_FROM_SEG(sbi, sbi->next_victim_seg[BG_GC]) == secno)
 530		sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
 531	if (GET_SEC_FROM_SEG(sbi, sbi->next_victim_seg[FG_GC]) == secno)
 532		sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
 533
 534unlock_out:
 535	spin_unlock(&free_i->segmap_lock);
 536}
 537
 538static inline void __set_test_and_inuse(struct f2fs_sb_info *sbi,
 539		unsigned int segno)
 540{
 541	struct free_segmap_info *free_i = FREE_I(sbi);
 542	unsigned int secno = GET_SEC_FROM_SEG(sbi, segno);
 543
 544	spin_lock(&free_i->segmap_lock);
 545	if (!test_and_set_bit(segno, free_i->free_segmap)) {
 546		free_i->free_segments--;
 547		if (!test_and_set_bit(secno, free_i->free_secmap))
 548			free_i->free_sections--;
 549	}
 550	spin_unlock(&free_i->segmap_lock);
 551}
 552
 553static inline void get_sit_bitmap(struct f2fs_sb_info *sbi,
 554		void *dst_addr)
 555{
 556	struct sit_info *sit_i = SIT_I(sbi);
 557
 558#ifdef CONFIG_F2FS_CHECK_FS
 559	if (memcmp(sit_i->sit_bitmap, sit_i->sit_bitmap_mir,
 560						sit_i->bitmap_size))
 561		f2fs_bug_on(sbi, 1);
 562#endif
 563	memcpy(dst_addr, sit_i->sit_bitmap, sit_i->bitmap_size);
 564}
 565
 566static inline block_t written_block_count(struct f2fs_sb_info *sbi)
 567{
 568	return SIT_I(sbi)->written_valid_blocks;
 569}
 570
 571static inline unsigned int free_segments(struct f2fs_sb_info *sbi)
 572{
 573	return FREE_I(sbi)->free_segments;
 574}
 575
 576static inline unsigned int reserved_segments(struct f2fs_sb_info *sbi)
 577{
 578	return SM_I(sbi)->reserved_segments;
 579}
 580
 581static inline unsigned int free_sections(struct f2fs_sb_info *sbi)
 582{
 583	return FREE_I(sbi)->free_sections;
 584}
 585
 586static inline unsigned int prefree_segments(struct f2fs_sb_info *sbi)
 587{
 588	return DIRTY_I(sbi)->nr_dirty[PRE];
 589}
 590
 591static inline unsigned int dirty_segments(struct f2fs_sb_info *sbi)
 592{
 593	return DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_DATA] +
 594		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_DATA] +
 595		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_DATA] +
 596		DIRTY_I(sbi)->nr_dirty[DIRTY_HOT_NODE] +
 597		DIRTY_I(sbi)->nr_dirty[DIRTY_WARM_NODE] +
 598		DIRTY_I(sbi)->nr_dirty[DIRTY_COLD_NODE];
 599}
 600
 601static inline int overprovision_segments(struct f2fs_sb_info *sbi)
 602{
 603	return SM_I(sbi)->ovp_segments;
 604}
 605
 606static inline int reserved_sections(struct f2fs_sb_info *sbi)
 607{
 608	return GET_SEC_FROM_SEG(sbi, reserved_segments(sbi));
 609}
 610
 611static inline unsigned int get_left_section_blocks(struct f2fs_sb_info *sbi,
 612					enum log_type type, unsigned int segno)
 613{
 614	if (f2fs_lfs_mode(sbi)) {
 615		unsigned int used_blocks = __is_large_section(sbi) ? SEGS_TO_BLKS(sbi,
 616				(segno - GET_START_SEG_FROM_SEC(sbi, segno))) : 0;
 617		return CAP_BLKS_PER_SEC(sbi) - used_blocks -
 618			CURSEG_I(sbi, type)->next_blkoff;
 619	}
 620	return CAP_BLKS_PER_SEC(sbi) - get_ckpt_valid_blocks(sbi, segno, true);
 621}
 622
 623static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi,
 624			unsigned int node_blocks, unsigned int data_blocks,
 625			unsigned int dent_blocks)
 626{
 627	unsigned int segno, left_blocks, blocks;
 628	int i;
 629
 630	/* check current data/node sections in the worst case. */
 631	for (i = CURSEG_HOT_DATA; i < NR_PERSISTENT_LOG; i++) {
 632		segno = CURSEG_I(sbi, i)->segno;
 633
 634		if (unlikely(segno == NULL_SEGNO))
 635			return false;
 636
 637		left_blocks = get_left_section_blocks(sbi, i, segno);
 638
 639		blocks = i <= CURSEG_COLD_DATA ? data_blocks : node_blocks;
 640		if (blocks > left_blocks)
 641			return false;
 642	}
 643
 644	/* check current data section for dentry blocks. */
 645	segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno;
 646
 647	if (unlikely(segno == NULL_SEGNO))
 648		return false;
 649
 650	left_blocks = get_left_section_blocks(sbi, CURSEG_HOT_DATA, segno);
 651
 652	if (dent_blocks > left_blocks)
 653		return false;
 654	return true;
 655}
 656
 657/*
 658 * calculate needed sections for dirty node/dentry and call
 659 * has_curseg_enough_space, please note that, it needs to account
 660 * dirty data as well in lfs mode when checkpoint is disabled.
 661 */
 662static inline void __get_secs_required(struct f2fs_sb_info *sbi,
 663		unsigned int *lower_p, unsigned int *upper_p, bool *curseg_p)
 664{
 665	unsigned int total_node_blocks = get_pages(sbi, F2FS_DIRTY_NODES) +
 666					get_pages(sbi, F2FS_DIRTY_DENTS) +
 667					get_pages(sbi, F2FS_DIRTY_IMETA);
 668	unsigned int total_dent_blocks = get_pages(sbi, F2FS_DIRTY_DENTS);
 669	unsigned int total_data_blocks = 0;
 670	unsigned int node_secs = total_node_blocks / CAP_BLKS_PER_SEC(sbi);
 671	unsigned int dent_secs = total_dent_blocks / CAP_BLKS_PER_SEC(sbi);
 672	unsigned int data_secs = 0;
 673	unsigned int node_blocks = total_node_blocks % CAP_BLKS_PER_SEC(sbi);
 674	unsigned int dent_blocks = total_dent_blocks % CAP_BLKS_PER_SEC(sbi);
 675	unsigned int data_blocks = 0;
 676
 677	if (f2fs_lfs_mode(sbi)) {
 678		total_data_blocks = get_pages(sbi, F2FS_DIRTY_DATA);
 679		data_secs = total_data_blocks / CAP_BLKS_PER_SEC(sbi);
 680		data_blocks = total_data_blocks % CAP_BLKS_PER_SEC(sbi);
 681	}
 682
 683	if (lower_p)
 684		*lower_p = node_secs + dent_secs + data_secs;
 685	if (upper_p)
 686		*upper_p = node_secs + dent_secs + data_secs +
 687			(node_blocks ? 1 : 0) + (dent_blocks ? 1 : 0) +
 688			(data_blocks ? 1 : 0);
 689	if (curseg_p)
 690		*curseg_p = has_curseg_enough_space(sbi,
 691				node_blocks, data_blocks, dent_blocks);
 692}
 693
 694static inline bool has_not_enough_free_secs(struct f2fs_sb_info *sbi,
 695					int freed, int needed)
 696{
 697	unsigned int free_secs, lower_secs, upper_secs;
 698	bool curseg_space;
 699
 700	if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
 701		return false;
 702
 703	__get_secs_required(sbi, &lower_secs, &upper_secs, &curseg_space);
 704
 705	free_secs = free_sections(sbi) + freed;
 706	lower_secs += needed + reserved_sections(sbi);
 707	upper_secs += needed + reserved_sections(sbi);
 708
 709	if (free_secs > upper_secs)
 710		return false;
 711	if (free_secs <= lower_secs)
 712		return true;
 713	return !curseg_space;
 714}
 715
 716static inline bool has_enough_free_secs(struct f2fs_sb_info *sbi,
 717					int freed, int needed)
 718{
 719	return !has_not_enough_free_secs(sbi, freed, needed);
 720}
 721
 722static inline bool has_enough_free_blks(struct f2fs_sb_info *sbi)
 723{
 724	unsigned int total_free_blocks = 0;
 725	unsigned int avail_user_block_count;
 726
 727	spin_lock(&sbi->stat_lock);
 728
 729	avail_user_block_count = get_available_block_count(sbi, NULL, true);
 730	total_free_blocks = avail_user_block_count - (unsigned int)valid_user_blocks(sbi);
 731
 732	spin_unlock(&sbi->stat_lock);
 733
 734	return total_free_blocks > 0;
 735}
 736
 737static inline bool f2fs_is_checkpoint_ready(struct f2fs_sb_info *sbi)
 738{
 739	if (likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
 740		return true;
 741	if (likely(has_enough_free_secs(sbi, 0, 0)))
 742		return true;
 743	if (!f2fs_lfs_mode(sbi) &&
 744		likely(has_enough_free_blks(sbi)))
 745		return true;
 746	return false;
 747}
 748
 749static inline bool excess_prefree_segs(struct f2fs_sb_info *sbi)
 750{
 751	return prefree_segments(sbi) > SM_I(sbi)->rec_prefree_segments;
 752}
 753
 754static inline int utilization(struct f2fs_sb_info *sbi)
 755{
 756	return div_u64((u64)valid_user_blocks(sbi) * 100,
 757					sbi->user_block_count);
 758}
 759
 760/*
 761 * Sometimes f2fs may be better to drop out-of-place update policy.
 762 * And, users can control the policy through sysfs entries.
 763 * There are five policies with triggering conditions as follows.
 764 * F2FS_IPU_FORCE - all the time,
 765 * F2FS_IPU_SSR - if SSR mode is activated,
 766 * F2FS_IPU_UTIL - if FS utilization is over threashold,
 767 * F2FS_IPU_SSR_UTIL - if SSR mode is activated and FS utilization is over
 768 *                     threashold,
 769 * F2FS_IPU_FSYNC - activated in fsync path only for high performance flash
 770 *                     storages. IPU will be triggered only if the # of dirty
 771 *                     pages over min_fsync_blocks. (=default option)
 772 * F2FS_IPU_ASYNC - do IPU given by asynchronous write requests.
 773 * F2FS_IPU_NOCACHE - disable IPU bio cache.
 774 * F2FS_IPU_HONOR_OPU_WRITE - use OPU write prior to IPU write if inode has
 775 *                            FI_OPU_WRITE flag.
 776 * F2FS_IPU_DISABLE - disable IPU. (=default option in LFS mode)
 777 */
 778#define DEF_MIN_IPU_UTIL	70
 779#define DEF_MIN_FSYNC_BLOCKS	8
 780#define DEF_MIN_HOT_BLOCKS	16
 781
 782#define SMALL_VOLUME_SEGMENTS	(16 * 512)	/* 16GB */
 783
 784#define F2FS_IPU_DISABLE	0
 785
 786/* Modification on enum should be synchronized with ipu_mode_names array */
 787enum {
 788	F2FS_IPU_FORCE,
 789	F2FS_IPU_SSR,
 790	F2FS_IPU_UTIL,
 791	F2FS_IPU_SSR_UTIL,
 792	F2FS_IPU_FSYNC,
 793	F2FS_IPU_ASYNC,
 794	F2FS_IPU_NOCACHE,
 795	F2FS_IPU_HONOR_OPU_WRITE,
 796	F2FS_IPU_MAX,
 797};
 798
 799static inline bool IS_F2FS_IPU_DISABLE(struct f2fs_sb_info *sbi)
 800{
 801	return SM_I(sbi)->ipu_policy == F2FS_IPU_DISABLE;
 802}
 803
 804#define F2FS_IPU_POLICY(name)					\
 805static inline bool IS_##name(struct f2fs_sb_info *sbi)		\
 806{								\
 807	return SM_I(sbi)->ipu_policy & BIT(name);		\
 808}
 809
 810F2FS_IPU_POLICY(F2FS_IPU_FORCE);
 811F2FS_IPU_POLICY(F2FS_IPU_SSR);
 812F2FS_IPU_POLICY(F2FS_IPU_UTIL);
 813F2FS_IPU_POLICY(F2FS_IPU_SSR_UTIL);
 814F2FS_IPU_POLICY(F2FS_IPU_FSYNC);
 815F2FS_IPU_POLICY(F2FS_IPU_ASYNC);
 816F2FS_IPU_POLICY(F2FS_IPU_NOCACHE);
 817F2FS_IPU_POLICY(F2FS_IPU_HONOR_OPU_WRITE);
 818
 819static inline unsigned int curseg_segno(struct f2fs_sb_info *sbi,
 820		int type)
 821{
 822	struct curseg_info *curseg = CURSEG_I(sbi, type);
 823	return curseg->segno;
 824}
 825
 826static inline unsigned char curseg_alloc_type(struct f2fs_sb_info *sbi,
 827		int type)
 828{
 829	struct curseg_info *curseg = CURSEG_I(sbi, type);
 830	return curseg->alloc_type;
 831}
 832
 833static inline bool valid_main_segno(struct f2fs_sb_info *sbi,
 834		unsigned int segno)
 835{
 836	return segno <= (MAIN_SEGS(sbi) - 1);
 837}
 838
 839static inline void verify_fio_blkaddr(struct f2fs_io_info *fio)
 840{
 841	struct f2fs_sb_info *sbi = fio->sbi;
 842
 843	if (__is_valid_data_blkaddr(fio->old_blkaddr))
 844		verify_blkaddr(sbi, fio->old_blkaddr, __is_meta_io(fio) ?
 845					META_GENERIC : DATA_GENERIC);
 846	verify_blkaddr(sbi, fio->new_blkaddr, __is_meta_io(fio) ?
 847					META_GENERIC : DATA_GENERIC_ENHANCE);
 848}
 849
 850/*
 851 * Summary block is always treated as an invalid block
 852 */
 853static inline int check_block_count(struct f2fs_sb_info *sbi,
 854		int segno, struct f2fs_sit_entry *raw_sit)
 855{
 856	bool is_valid  = test_bit_le(0, raw_sit->valid_map) ? true : false;
 857	int valid_blocks = 0;
 858	int cur_pos = 0, next_pos;
 859	unsigned int usable_blks_per_seg = f2fs_usable_blks_in_seg(sbi, segno);
 860
 861	/* check bitmap with valid block count */
 862	do {
 863		if (is_valid) {
 864			next_pos = find_next_zero_bit_le(&raw_sit->valid_map,
 865					usable_blks_per_seg,
 866					cur_pos);
 867			valid_blocks += next_pos - cur_pos;
 868		} else
 869			next_pos = find_next_bit_le(&raw_sit->valid_map,
 870					usable_blks_per_seg,
 871					cur_pos);
 872		cur_pos = next_pos;
 873		is_valid = !is_valid;
 874	} while (cur_pos < usable_blks_per_seg);
 875
 876	if (unlikely(GET_SIT_VBLOCKS(raw_sit) != valid_blocks)) {
 877		f2fs_err(sbi, "Mismatch valid blocks %d vs. %d",
 878			 GET_SIT_VBLOCKS(raw_sit), valid_blocks);
 879		set_sbi_flag(sbi, SBI_NEED_FSCK);
 880		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
 881		return -EFSCORRUPTED;
 882	}
 883
 884	if (usable_blks_per_seg < BLKS_PER_SEG(sbi))
 885		f2fs_bug_on(sbi, find_next_bit_le(&raw_sit->valid_map,
 886				BLKS_PER_SEG(sbi),
 887				usable_blks_per_seg) != BLKS_PER_SEG(sbi));
 888
 889	/* check segment usage, and check boundary of a given segment number */
 890	if (unlikely(GET_SIT_VBLOCKS(raw_sit) > usable_blks_per_seg
 891					|| !valid_main_segno(sbi, segno))) {
 892		f2fs_err(sbi, "Wrong valid blocks %d or segno %u",
 893			 GET_SIT_VBLOCKS(raw_sit), segno);
 894		set_sbi_flag(sbi, SBI_NEED_FSCK);
 895		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SIT);
 896		return -EFSCORRUPTED;
 897	}
 898	return 0;
 899}
 900
 901static inline pgoff_t current_sit_addr(struct f2fs_sb_info *sbi,
 902						unsigned int start)
 903{
 904	struct sit_info *sit_i = SIT_I(sbi);
 905	unsigned int offset = SIT_BLOCK_OFFSET(start);
 906	block_t blk_addr = sit_i->sit_base_addr + offset;
 907
 908	f2fs_bug_on(sbi, !valid_main_segno(sbi, start));
 909
 910#ifdef CONFIG_F2FS_CHECK_FS
 911	if (f2fs_test_bit(offset, sit_i->sit_bitmap) !=
 912			f2fs_test_bit(offset, sit_i->sit_bitmap_mir))
 913		f2fs_bug_on(sbi, 1);
 914#endif
 915
 916	/* calculate sit block address */
 917	if (f2fs_test_bit(offset, sit_i->sit_bitmap))
 918		blk_addr += sit_i->sit_blocks;
 919
 920	return blk_addr;
 921}
 922
 923static inline pgoff_t next_sit_addr(struct f2fs_sb_info *sbi,
 924						pgoff_t block_addr)
 925{
 926	struct sit_info *sit_i = SIT_I(sbi);
 927	block_addr -= sit_i->sit_base_addr;
 928	if (block_addr < sit_i->sit_blocks)
 929		block_addr += sit_i->sit_blocks;
 930	else
 931		block_addr -= sit_i->sit_blocks;
 932
 933	return block_addr + sit_i->sit_base_addr;
 934}
 935
 936static inline void set_to_next_sit(struct sit_info *sit_i, unsigned int start)
 937{
 938	unsigned int block_off = SIT_BLOCK_OFFSET(start);
 939
 940	f2fs_change_bit(block_off, sit_i->sit_bitmap);
 941#ifdef CONFIG_F2FS_CHECK_FS
 942	f2fs_change_bit(block_off, sit_i->sit_bitmap_mir);
 943#endif
 944}
 945
 946static inline unsigned long long get_mtime(struct f2fs_sb_info *sbi,
 947						bool base_time)
 948{
 949	struct sit_info *sit_i = SIT_I(sbi);
 950	time64_t diff, now = ktime_get_boottime_seconds();
 951
 952	if (now >= sit_i->mounted_time)
 953		return sit_i->elapsed_time + now - sit_i->mounted_time;
 954
 955	/* system time is set to the past */
 956	if (!base_time) {
 957		diff = sit_i->mounted_time - now;
 958		if (sit_i->elapsed_time >= diff)
 959			return sit_i->elapsed_time - diff;
 960		return 0;
 961	}
 962	return sit_i->elapsed_time;
 963}
 964
 965static inline void set_summary(struct f2fs_summary *sum, nid_t nid,
 966			unsigned int ofs_in_node, unsigned char version)
 967{
 968	sum->nid = cpu_to_le32(nid);
 969	sum->ofs_in_node = cpu_to_le16(ofs_in_node);
 970	sum->version = version;
 971}
 972
 973static inline block_t start_sum_block(struct f2fs_sb_info *sbi)
 974{
 975	return __start_cp_addr(sbi) +
 976		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
 977}
 978
 979static inline block_t sum_blk_addr(struct f2fs_sb_info *sbi, int base, int type)
 980{
 981	return __start_cp_addr(sbi) +
 982		le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_total_block_count)
 983				- (base + 1) + type;
 984}
 985
 986static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
 987{
 988	if (is_cursec(sbi, secno) || (sbi->cur_victim_sec == secno))
 989		return true;
 990	return false;
 991}
 992
 993/*
 994 * It is very important to gather dirty pages and write at once, so that we can
 995 * submit a big bio without interfering other data writes.
 996 * By default, 512 pages for directory data,
 997 * 512 pages (2MB) * 8 for nodes, and
 998 * 256 pages * 8 for meta are set.
 999 */
1000static inline int nr_pages_to_skip(struct f2fs_sb_info *sbi, int type)
1001{
1002	if (sbi->sb->s_bdi->wb.dirty_exceeded)
1003		return 0;
1004
1005	if (type == DATA)
1006		return BLKS_PER_SEG(sbi);
1007	else if (type == NODE)
1008		return SEGS_TO_BLKS(sbi, 8);
1009	else if (type == META)
1010		return 8 * BIO_MAX_VECS;
1011	else
1012		return 0;
1013}
1014
1015/*
1016 * When writing pages, it'd better align nr_to_write for segment size.
1017 */
1018static inline long nr_pages_to_write(struct f2fs_sb_info *sbi, int type,
1019					struct writeback_control *wbc)
1020{
1021	long nr_to_write, desired;
1022
1023	if (wbc->sync_mode != WB_SYNC_NONE)
1024		return 0;
1025
1026	nr_to_write = wbc->nr_to_write;
1027	desired = BIO_MAX_VECS;
1028	if (type == NODE)
1029		desired <<= 1;
1030
1031	wbc->nr_to_write = desired;
1032	return desired - nr_to_write;
1033}
1034
1035static inline void wake_up_discard_thread(struct f2fs_sb_info *sbi, bool force)
1036{
1037	struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
1038	bool wakeup = false;
1039	int i;
1040
1041	if (force)
1042		goto wake_up;
1043
1044	mutex_lock(&dcc->cmd_lock);
1045	for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
1046		if (i + 1 < dcc->discard_granularity)
1047			break;
1048		if (!list_empty(&dcc->pend_list[i])) {
1049			wakeup = true;
1050			break;
1051		}
1052	}
1053	mutex_unlock(&dcc->cmd_lock);
1054	if (!wakeup || !is_idle(sbi, DISCARD_TIME))
1055		return;
1056wake_up:
1057	dcc->discard_wake = true;
1058	wake_up_interruptible_all(&dcc->discard_wait_queue);
1059}
~/ Read-only file preview.