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