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