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