1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/gc.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/fs.h> 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/f2fs_fs.h> 12 #include <linux/kthread.h> 13 #include <linux/delay.h> 14 #include <linux/freezer.h> 15 #include <linux/sched/signal.h> 16 #include <linux/random.h> 17 #include <linux/sched/mm.h> 18 19 #include "f2fs.h" 20 #include "node.h" 21 #include "segment.h" 22 #include "gc.h" 23 #include "iostat.h" 24 #include <trace/events/f2fs.h> 25 26 static struct kmem_cache *victim_entry_slab; 27 28 static unsigned int count_bits(const unsigned long *addr, 29 unsigned int offset, unsigned int len); 30 31 static int gc_thread_func(void *data) 32 { 33 struct f2fs_sb_info *sbi = data; 34 struct f2fs_gc_kthread *gc_th = &sbi->gc_thread; 35 wait_queue_head_t *wq = &sbi->gc_thread.gc_wait_queue_head; 36 wait_queue_head_t *fggc_wq = &sbi->gc_thread.fggc_wq; 37 unsigned int wait_ms; 38 struct f2fs_gc_control gc_control = { 39 .victim_segno = NULL_SEGNO, 40 .should_migrate_blocks = false, 41 .err_gc_skipped = false, 42 .one_time = false }; 43 44 wait_ms = gc_th->min_sleep_time; 45 46 set_freezable(); 47 do { 48 bool sync_mode, foreground = false, gc_boost = false; 49 50 wait_event_freezable_timeout(*wq, 51 kthread_should_stop() || 52 waitqueue_active(fggc_wq) || 53 gc_th->gc_wake, 54 msecs_to_jiffies(wait_ms)); 55 56 if (test_opt(sbi, GC_MERGE) && waitqueue_active(fggc_wq)) { 57 foreground = true; 58 gc_control.one_time = false; 59 } else if (f2fs_sb_has_blkzoned(sbi)) { 60 gc_control.one_time = true; 61 } 62 63 /* give it a try one time */ 64 if (gc_th->gc_wake) 65 gc_th->gc_wake = false; 66 67 if (f2fs_readonly(sbi->sb)) { 68 stat_other_skip_bggc_count(sbi); 69 continue; 70 } 71 if (kthread_should_stop()) 72 break; 73 74 if (sbi->sb->s_writers.frozen >= SB_FREEZE_WRITE) { 75 increase_sleep_time(gc_th, &wait_ms); 76 stat_other_skip_bggc_count(sbi); 77 continue; 78 } 79 80 if (time_to_inject(sbi, FAULT_CHECKPOINT)) 81 f2fs_stop_checkpoint(sbi, false, 82 STOP_CP_REASON_FAULT_INJECT); 83 84 if (!sb_start_write_trylock(sbi->sb)) { 85 stat_other_skip_bggc_count(sbi); 86 continue; 87 } 88 89 /* 90 * [GC triggering condition] 91 * 0. GC is not conducted currently. 92 * 1. There are enough dirty segments. 93 * 2. IO subsystem is idle by checking the # of writeback pages. 94 * 3. IO subsystem is idle by checking the # of requests in 95 * bdev's request list. 96 * 97 * Note) We have to avoid triggering GCs frequently. 98 * Because it is possible that some segments can be 99 * invalidated soon after by user update or deletion. 100 * So, I'd like to wait some time to collect dirty segments. 101 */ 102 if (sbi->gc_mode == GC_URGENT_HIGH || 103 sbi->gc_mode == GC_URGENT_MID) { 104 wait_ms = gc_th->urgent_sleep_time; 105 f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc); 106 goto do_gc; 107 } 108 109 if (foreground) { 110 f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc); 111 goto do_gc; 112 } else if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, 113 &gc_control.lc)) { 114 stat_other_skip_bggc_count(sbi); 115 goto next; 116 } 117 118 if (!is_idle(sbi, GC_TIME)) { 119 increase_sleep_time(gc_th, &wait_ms); 120 f2fs_up_write_trace(&sbi->gc_lock, &gc_control.lc); 121 stat_io_skip_bggc_count(sbi); 122 goto next; 123 } 124 125 if (f2fs_sb_has_blkzoned(sbi)) { 126 if (has_enough_free_blocks(sbi, 127 gc_th->no_zoned_gc_percent)) { 128 wait_ms = gc_th->no_gc_sleep_time; 129 f2fs_up_write_trace(&sbi->gc_lock, 130 &gc_control.lc); 131 goto next; 132 } 133 if (wait_ms == gc_th->no_gc_sleep_time) 134 wait_ms = gc_th->max_sleep_time; 135 } 136 137 if (need_to_boost_gc(sbi)) { 138 decrease_sleep_time(gc_th, &wait_ms); 139 if (f2fs_sb_has_blkzoned(sbi)) 140 gc_boost = true; 141 } else { 142 increase_sleep_time(gc_th, &wait_ms); 143 } 144 do_gc: 145 stat_inc_gc_call_count(sbi, foreground ? 146 FOREGROUND : BACKGROUND); 147 148 sync_mode = (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC) || 149 (gc_boost && gc_th->boost_gc_greedy); 150 151 /* foreground GC was been triggered via f2fs_balance_fs() */ 152 if (foreground && !f2fs_sb_has_blkzoned(sbi)) 153 sync_mode = false; 154 155 gc_control.init_gc_type = sync_mode ? FG_GC : BG_GC; 156 gc_control.no_bg_gc = foreground; 157 gc_control.nr_free_secs = foreground ? 1 : 0; 158 159 /* if return value is not zero, no victim was selected */ 160 if (f2fs_gc(sbi, &gc_control)) { 161 /* don't bother wait_ms by foreground gc */ 162 if (!foreground) 163 wait_ms = gc_th->no_gc_sleep_time; 164 } else { 165 /* reset wait_ms to default sleep time */ 166 if (wait_ms == gc_th->no_gc_sleep_time) 167 wait_ms = gc_th->min_sleep_time; 168 } 169 170 if (foreground) 171 wake_up_all(&gc_th->fggc_wq); 172 173 trace_f2fs_background_gc(sbi->sb, wait_ms, 174 prefree_segments(sbi), free_segments(sbi)); 175 176 /* balancing f2fs's metadata periodically */ 177 f2fs_balance_fs_bg(sbi, true); 178 next: 179 if (sbi->gc_mode != GC_NORMAL) { 180 spin_lock(&sbi->gc_remaining_trials_lock); 181 if (sbi->gc_remaining_trials) { 182 sbi->gc_remaining_trials--; 183 if (!sbi->gc_remaining_trials) 184 sbi->gc_mode = GC_NORMAL; 185 } 186 spin_unlock(&sbi->gc_remaining_trials_lock); 187 } 188 sb_end_write(sbi->sb); 189 190 } while (!kthread_should_stop()); 191 return 0; 192 } 193 194 int f2fs_start_gc_thread(struct f2fs_sb_info *sbi) 195 { 196 struct f2fs_gc_kthread *gc_th = &sbi->gc_thread; 197 dev_t dev = sbi->sb->s_bdev->bd_dev; 198 199 gc_th->urgent_sleep_time = DEF_GC_THREAD_URGENT_SLEEP_TIME; 200 gc_th->valid_thresh_ratio = DEF_GC_THREAD_VALID_THRESH_RATIO; 201 gc_th->boost_gc_multiple = BOOST_GC_MULTIPLE; 202 gc_th->boost_gc_greedy = GC_GREEDY; 203 204 if (f2fs_sb_has_blkzoned(sbi)) { 205 gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME_ZONED; 206 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME_ZONED; 207 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME_ZONED; 208 gc_th->no_zoned_gc_percent = LIMIT_NO_ZONED_GC; 209 gc_th->boost_zoned_gc_percent = LIMIT_BOOST_ZONED_GC; 210 } else { 211 gc_th->min_sleep_time = DEF_GC_THREAD_MIN_SLEEP_TIME; 212 gc_th->max_sleep_time = DEF_GC_THREAD_MAX_SLEEP_TIME; 213 gc_th->no_gc_sleep_time = DEF_GC_THREAD_NOGC_SLEEP_TIME; 214 gc_th->no_zoned_gc_percent = 0; 215 gc_th->boost_zoned_gc_percent = 0; 216 } 217 218 gc_th->gc_wake = false; 219 220 init_waitqueue_head(&gc_th->gc_wait_queue_head); 221 init_waitqueue_head(&gc_th->fggc_wq); 222 gc_th->f2fs_gc_task = kthread_run(gc_thread_func, sbi, 223 "f2fs_gc-%u:%u", MAJOR(dev), MINOR(dev)); 224 if (IS_ERR(gc_th->f2fs_gc_task)) { 225 int err = PTR_ERR(gc_th->f2fs_gc_task); 226 227 gc_th->f2fs_gc_task = NULL; 228 return err; 229 } 230 231 set_user_nice(gc_th->f2fs_gc_task, 232 PRIO_TO_NICE(sbi->critical_task_priority)); 233 return 0; 234 } 235 236 void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi) 237 { 238 struct f2fs_gc_kthread *gc_th = &sbi->gc_thread; 239 240 if (!gc_th->f2fs_gc_task) 241 return; 242 243 kthread_stop(gc_th->f2fs_gc_task); 244 gc_th->f2fs_gc_task = NULL; 245 wake_up_all(&gc_th->fggc_wq); 246 } 247 248 static int select_gc_type(struct f2fs_sb_info *sbi, int gc_type) 249 { 250 int gc_mode; 251 252 if (gc_type == BG_GC) { 253 if (sbi->am.atgc_enabled) 254 gc_mode = GC_AT; 255 else 256 gc_mode = GC_CB; 257 } else { 258 gc_mode = GC_GREEDY; 259 } 260 261 switch (sbi->gc_mode) { 262 case GC_IDLE_CB: 263 case GC_URGENT_LOW: 264 case GC_URGENT_MID: 265 gc_mode = GC_CB; 266 break; 267 case GC_IDLE_GREEDY: 268 case GC_URGENT_HIGH: 269 gc_mode = GC_GREEDY; 270 break; 271 case GC_IDLE_AT: 272 gc_mode = GC_AT; 273 break; 274 } 275 276 return gc_mode; 277 } 278 279 static void select_policy(struct f2fs_sb_info *sbi, int gc_type, 280 int type, struct victim_sel_policy *p) 281 { 282 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 283 284 if (p->alloc_mode == SSR || p->alloc_mode == AT_SSR) { 285 p->gc_mode = GC_GREEDY; 286 p->dirty_bitmap = dirty_i->dirty_segmap[type]; 287 p->max_search = dirty_i->nr_dirty[type]; 288 p->ofs_unit = 1; 289 } else { 290 p->gc_mode = select_gc_type(sbi, gc_type); 291 p->ofs_unit = SEGS_PER_SEC(sbi); 292 if (__is_large_section(sbi)) { 293 p->dirty_bitmap = dirty_i->dirty_secmap; 294 p->max_search = count_bits(p->dirty_bitmap, 295 0, MAIN_SECS(sbi)); 296 } else { 297 p->dirty_bitmap = dirty_i->dirty_segmap[DIRTY]; 298 p->max_search = dirty_i->nr_dirty[DIRTY]; 299 } 300 } 301 302 /* 303 * adjust candidates range, should select all dirty segments for 304 * foreground GC and urgent GC cases. 305 */ 306 if (gc_type != FG_GC && 307 (sbi->gc_mode != GC_URGENT_HIGH) && 308 (p->gc_mode != GC_AT && p->alloc_mode != AT_SSR) && 309 p->max_search > sbi->max_victim_search) 310 p->max_search = sbi->max_victim_search; 311 312 /* let's select beginning hot/small space first. */ 313 if (f2fs_need_rand_seg_blk(sbi, type)) { 314 p->offset = get_random_u32_below(MAIN_SECS(sbi) * 315 SEGS_PER_SEC(sbi)); 316 SIT_I(sbi)->last_victim[p->gc_mode] = p->offset; 317 } else if (type == CURSEG_HOT_DATA || IS_NODESEG(type)) 318 p->offset = 0; 319 else 320 p->offset = SIT_I(sbi)->last_victim[p->gc_mode]; 321 } 322 323 static unsigned int get_max_cost(struct f2fs_sb_info *sbi, 324 struct victim_sel_policy *p) 325 { 326 /* SSR allocates in a segment unit */ 327 if (p->alloc_mode == SSR) 328 return BLKS_PER_SEG(sbi); 329 else if (p->alloc_mode == AT_SSR) 330 return UINT_MAX; 331 332 /* LFS */ 333 if (p->gc_mode == GC_GREEDY) 334 return SEGS_TO_BLKS(sbi, 2 * p->ofs_unit); 335 else if (p->gc_mode == GC_CB) 336 return UINT_MAX; 337 else if (p->gc_mode == GC_AT) 338 return UINT_MAX; 339 else /* No other gc_mode */ 340 return 0; 341 } 342 343 static unsigned int check_bg_victims(struct f2fs_sb_info *sbi) 344 { 345 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 346 unsigned int secno; 347 348 /* 349 * If the gc_type is FG_GC, we can select victim segments 350 * selected by background GC before. 351 * Those segments guarantee they have small valid blocks. 352 */ 353 for_each_set_bit(secno, dirty_i->victim_secmap, MAIN_SECS(sbi)) { 354 if (sec_usage_check(sbi, secno)) 355 continue; 356 clear_bit(secno, dirty_i->victim_secmap); 357 return GET_SEG_FROM_SEC(sbi, secno); 358 } 359 return NULL_SEGNO; 360 } 361 362 static unsigned int get_cb_cost(struct f2fs_sb_info *sbi, unsigned int segno) 363 { 364 struct sit_info *sit_i = SIT_I(sbi); 365 unsigned long long mtime = 0; 366 unsigned int vblocks; 367 unsigned char age = 0; 368 unsigned char u; 369 unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi); 370 371 mtime = f2fs_get_section_mtime(sbi, segno); 372 f2fs_bug_on(sbi, mtime == INVALID_MTIME); 373 vblocks = get_valid_blocks(sbi, segno, true); 374 vblocks = div_u64(vblocks, usable_segs_per_sec); 375 376 u = BLKS_TO_SEGS(sbi, vblocks * 100); 377 378 /* Handle if the system time has changed by the user */ 379 if (mtime < sit_i->min_mtime) 380 sit_i->min_mtime = mtime; 381 if (mtime > sit_i->max_mtime) 382 sit_i->max_mtime = mtime; 383 if (sit_i->max_mtime != sit_i->min_mtime) 384 age = 100 - div64_u64(100 * (mtime - sit_i->min_mtime), 385 sit_i->max_mtime - sit_i->min_mtime); 386 387 return UINT_MAX - ((100 * (100 - u) * age) / (100 + u)); 388 } 389 390 static inline unsigned int get_gc_cost(struct f2fs_sb_info *sbi, 391 unsigned int segno, struct victim_sel_policy *p, 392 unsigned int valid_thresh_ratio) 393 { 394 if (p->alloc_mode == SSR) 395 return get_seg_entry(sbi, segno)->ckpt_valid_blocks; 396 397 if (p->one_time_gc && (valid_thresh_ratio < 100) && 398 (get_valid_blocks(sbi, segno, true) >= 399 CAP_BLKS_PER_SEC(sbi) * valid_thresh_ratio / 100)) 400 return UINT_MAX; 401 402 /* alloc_mode == LFS */ 403 if (p->gc_mode == GC_GREEDY) 404 return get_valid_blocks(sbi, segno, true); 405 else if (p->gc_mode == GC_CB) 406 return get_cb_cost(sbi, segno); 407 408 f2fs_bug_on(sbi, 1); 409 return 0; 410 } 411 412 static unsigned int count_bits(const unsigned long *addr, 413 unsigned int offset, unsigned int len) 414 { 415 unsigned int end = offset + len, sum = 0; 416 417 while (offset < end) { 418 if (test_bit(offset++, addr)) 419 ++sum; 420 } 421 return sum; 422 } 423 424 static bool f2fs_check_victim_tree(struct f2fs_sb_info *sbi, 425 struct rb_root_cached *root) 426 { 427 #ifdef CONFIG_F2FS_CHECK_FS 428 struct rb_node *cur = rb_first_cached(root), *next; 429 struct victim_entry *cur_ve, *next_ve; 430 431 while (cur) { 432 next = rb_next(cur); 433 if (!next) 434 return true; 435 436 cur_ve = rb_entry(cur, struct victim_entry, rb_node); 437 next_ve = rb_entry(next, struct victim_entry, rb_node); 438 439 if (cur_ve->mtime > next_ve->mtime) { 440 f2fs_info(sbi, "broken victim_rbtree, " 441 "cur_mtime(%llu) next_mtime(%llu)", 442 cur_ve->mtime, next_ve->mtime); 443 return false; 444 } 445 cur = next; 446 } 447 #endif 448 return true; 449 } 450 451 static struct victim_entry *__lookup_victim_entry(struct f2fs_sb_info *sbi, 452 unsigned long long mtime) 453 { 454 struct atgc_management *am = &sbi->am; 455 struct rb_node *node = am->root.rb_root.rb_node; 456 struct victim_entry *ve = NULL; 457 458 while (node) { 459 ve = rb_entry(node, struct victim_entry, rb_node); 460 461 if (mtime < ve->mtime) 462 node = node->rb_left; 463 else 464 node = node->rb_right; 465 } 466 return ve; 467 } 468 469 static struct victim_entry *__create_victim_entry(struct f2fs_sb_info *sbi, 470 unsigned long long mtime, unsigned int segno) 471 { 472 struct atgc_management *am = &sbi->am; 473 struct victim_entry *ve; 474 475 ve = f2fs_kmem_cache_alloc(victim_entry_slab, GFP_NOFS, true, NULL); 476 477 ve->mtime = mtime; 478 ve->segno = segno; 479 480 list_add_tail(&ve->list, &am->victim_list); 481 am->victim_count++; 482 483 return ve; 484 } 485 486 static void __insert_victim_entry(struct f2fs_sb_info *sbi, 487 unsigned long long mtime, unsigned int segno) 488 { 489 struct atgc_management *am = &sbi->am; 490 struct rb_root_cached *root = &am->root; 491 struct rb_node **p = &root->rb_root.rb_node; 492 struct rb_node *parent = NULL; 493 struct victim_entry *ve; 494 bool left_most = true; 495 496 /* look up rb tree to find parent node */ 497 while (*p) { 498 parent = *p; 499 ve = rb_entry(parent, struct victim_entry, rb_node); 500 501 if (mtime < ve->mtime) { 502 p = &(*p)->rb_left; 503 } else { 504 p = &(*p)->rb_right; 505 left_most = false; 506 } 507 } 508 509 ve = __create_victim_entry(sbi, mtime, segno); 510 511 rb_link_node(&ve->rb_node, parent, p); 512 rb_insert_color_cached(&ve->rb_node, root, left_most); 513 } 514 515 static void add_victim_entry(struct f2fs_sb_info *sbi, 516 struct victim_sel_policy *p, unsigned int segno) 517 { 518 struct sit_info *sit_i = SIT_I(sbi); 519 unsigned long long mtime = 0; 520 521 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 522 if (p->gc_mode == GC_AT && 523 get_valid_blocks(sbi, segno, true) == 0) 524 return; 525 } 526 527 mtime = f2fs_get_section_mtime(sbi, segno); 528 f2fs_bug_on(sbi, mtime == INVALID_MTIME); 529 530 /* Handle if the system time has changed by the user */ 531 if (mtime < sit_i->min_mtime) 532 sit_i->min_mtime = mtime; 533 if (mtime > sit_i->max_mtime) 534 sit_i->max_mtime = mtime; 535 if (mtime < sit_i->dirty_min_mtime) 536 sit_i->dirty_min_mtime = mtime; 537 if (mtime > sit_i->dirty_max_mtime) 538 sit_i->dirty_max_mtime = mtime; 539 540 /* don't choose young section as candidate */ 541 if (sit_i->dirty_max_mtime - mtime < p->age_threshold) 542 return; 543 544 __insert_victim_entry(sbi, mtime, segno); 545 } 546 547 static void atgc_lookup_victim(struct f2fs_sb_info *sbi, 548 struct victim_sel_policy *p) 549 { 550 struct sit_info *sit_i = SIT_I(sbi); 551 struct atgc_management *am = &sbi->am; 552 struct rb_root_cached *root = &am->root; 553 struct rb_node *node; 554 struct victim_entry *ve; 555 unsigned long long total_time; 556 unsigned long long age, u, accu; 557 unsigned long long max_mtime = sit_i->dirty_max_mtime; 558 unsigned long long min_mtime = sit_i->dirty_min_mtime; 559 unsigned int sec_blocks = CAP_BLKS_PER_SEC(sbi); 560 unsigned int vblocks; 561 unsigned int dirty_threshold = max(am->max_candidate_count, 562 am->candidate_ratio * 563 am->victim_count / 100); 564 unsigned int age_weight = am->age_weight; 565 unsigned int cost; 566 unsigned int iter = 0; 567 568 if (max_mtime < min_mtime) 569 return; 570 571 max_mtime += 1; 572 total_time = max_mtime - min_mtime; 573 574 accu = div64_u64(ULLONG_MAX, total_time); 575 accu = min_t(unsigned long long, div_u64(accu, 100), 576 DEFAULT_ACCURACY_CLASS); 577 578 node = rb_first_cached(root); 579 next: 580 ve = rb_entry_safe(node, struct victim_entry, rb_node); 581 if (!ve) 582 return; 583 584 if (ve->mtime >= max_mtime || ve->mtime < min_mtime) 585 goto skip; 586 587 /* age = 10000 * x% * 60 */ 588 age = div64_u64(accu * (max_mtime - ve->mtime), total_time) * 589 age_weight; 590 591 vblocks = get_valid_blocks(sbi, ve->segno, true); 592 f2fs_bug_on(sbi, !vblocks || vblocks == sec_blocks); 593 594 /* u = 10000 * x% * 40 */ 595 u = div64_u64(accu * (sec_blocks - vblocks), sec_blocks) * 596 (100 - age_weight); 597 598 f2fs_bug_on(sbi, age + u >= UINT_MAX); 599 600 cost = UINT_MAX - (age + u); 601 iter++; 602 603 if (cost < p->min_cost || 604 (cost == p->min_cost && age > p->oldest_age)) { 605 p->min_cost = cost; 606 p->oldest_age = age; 607 p->min_segno = ve->segno; 608 } 609 skip: 610 if (iter < dirty_threshold) { 611 node = rb_next(node); 612 goto next; 613 } 614 } 615 616 /* 617 * select candidates around source section in range of 618 * [target - dirty_threshold, target + dirty_threshold] 619 */ 620 static void atssr_lookup_victim(struct f2fs_sb_info *sbi, 621 struct victim_sel_policy *p) 622 { 623 struct sit_info *sit_i = SIT_I(sbi); 624 struct atgc_management *am = &sbi->am; 625 struct victim_entry *ve; 626 unsigned long long age; 627 unsigned long long max_mtime = sit_i->dirty_max_mtime; 628 unsigned long long min_mtime = sit_i->dirty_min_mtime; 629 unsigned int vblocks; 630 unsigned int dirty_threshold = max(am->max_candidate_count, 631 am->candidate_ratio * 632 am->victim_count / 100); 633 unsigned int cost, iter; 634 int stage = 0; 635 636 if (max_mtime < min_mtime) 637 return; 638 max_mtime += 1; 639 next_stage: 640 iter = 0; 641 ve = __lookup_victim_entry(sbi, p->age); 642 next_node: 643 if (!ve) { 644 if (stage++ == 0) 645 goto next_stage; 646 return; 647 } 648 649 if (ve->mtime >= max_mtime || ve->mtime < min_mtime) 650 goto skip_node; 651 652 age = max_mtime - ve->mtime; 653 654 vblocks = get_seg_entry(sbi, ve->segno)->ckpt_valid_blocks; 655 f2fs_bug_on(sbi, !vblocks); 656 657 /* rare case */ 658 if (vblocks == BLKS_PER_SEG(sbi)) 659 goto skip_node; 660 661 iter++; 662 663 age = max_mtime - abs(p->age - age); 664 cost = UINT_MAX - vblocks; 665 666 if (cost < p->min_cost || 667 (cost == p->min_cost && age > p->oldest_age)) { 668 p->min_cost = cost; 669 p->oldest_age = age; 670 p->min_segno = ve->segno; 671 } 672 skip_node: 673 if (iter < dirty_threshold) { 674 ve = rb_entry(stage == 0 ? rb_prev(&ve->rb_node) : 675 rb_next(&ve->rb_node), 676 struct victim_entry, rb_node); 677 goto next_node; 678 } 679 680 if (stage++ == 0) 681 goto next_stage; 682 } 683 684 static void lookup_victim_by_age(struct f2fs_sb_info *sbi, 685 struct victim_sel_policy *p) 686 { 687 f2fs_bug_on(sbi, !f2fs_check_victim_tree(sbi, &sbi->am.root)); 688 689 if (p->gc_mode == GC_AT) 690 atgc_lookup_victim(sbi, p); 691 else if (p->alloc_mode == AT_SSR) 692 atssr_lookup_victim(sbi, p); 693 else 694 f2fs_bug_on(sbi, 1); 695 } 696 697 static void release_victim_entry(struct f2fs_sb_info *sbi) 698 { 699 struct atgc_management *am = &sbi->am; 700 struct victim_entry *ve, *tmp; 701 702 list_for_each_entry_safe(ve, tmp, &am->victim_list, list) { 703 list_del(&ve->list); 704 kmem_cache_free(victim_entry_slab, ve); 705 am->victim_count--; 706 } 707 708 am->root = RB_ROOT_CACHED; 709 710 f2fs_bug_on(sbi, am->victim_count); 711 f2fs_bug_on(sbi, !list_empty(&am->victim_list)); 712 } 713 714 static bool f2fs_pin_section(struct f2fs_sb_info *sbi, unsigned int segno) 715 { 716 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 717 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); 718 719 if (!dirty_i->enable_pin_section) 720 return false; 721 if (!test_and_set_bit(secno, dirty_i->pinned_secmap)) 722 dirty_i->pinned_secmap_cnt++; 723 return true; 724 } 725 726 static bool f2fs_pinned_section_exists(struct dirty_seglist_info *dirty_i) 727 { 728 return dirty_i->pinned_secmap_cnt; 729 } 730 731 static bool f2fs_section_is_pinned(struct dirty_seglist_info *dirty_i, 732 unsigned int secno) 733 { 734 return dirty_i->enable_pin_section && 735 f2fs_pinned_section_exists(dirty_i) && 736 test_bit(secno, dirty_i->pinned_secmap); 737 } 738 739 static void f2fs_unpin_all_sections(struct f2fs_sb_info *sbi, bool enable) 740 { 741 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 742 743 if (f2fs_pinned_section_exists(DIRTY_I(sbi))) { 744 memset(DIRTY_I(sbi)->pinned_secmap, 0, bitmap_size); 745 DIRTY_I(sbi)->pinned_secmap_cnt = 0; 746 } 747 DIRTY_I(sbi)->enable_pin_section = enable; 748 } 749 750 static int f2fs_gc_pinned_control(struct inode *inode, int gc_type, 751 unsigned int segno) 752 { 753 if (!f2fs_is_pinned_file(inode)) 754 return 0; 755 if (gc_type != FG_GC) 756 return -EBUSY; 757 if (!f2fs_pin_section(F2FS_I_SB(inode), segno)) 758 f2fs_pin_file_control(inode, true); 759 return -EAGAIN; 760 } 761 762 /* 763 * This function is called from two paths. 764 * One is garbage collection and the other is SSR segment selection. 765 * When it is called during GC, it just gets a victim segment 766 * and it does not remove it from dirty seglist. 767 * When it is called from SSR segment selection, it finds a segment 768 * which has minimum valid blocks and removes it from dirty seglist. 769 */ 770 int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result, 771 int gc_type, int type, char alloc_mode, 772 unsigned long long age, bool one_time) 773 { 774 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 775 struct sit_info *sm = SIT_I(sbi); 776 struct victim_sel_policy p = {0}; 777 unsigned int secno, last_victim; 778 unsigned int last_segment; 779 unsigned int nsearched; 780 unsigned int valid_thresh_ratio = 100; 781 bool is_atgc; 782 int ret = 0; 783 784 mutex_lock(&dirty_i->seglist_lock); 785 last_segment = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi); 786 787 p.alloc_mode = alloc_mode; 788 p.age = age; 789 p.age_threshold = sbi->am.age_threshold; 790 if (one_time) { 791 p.one_time_gc = one_time; 792 if (has_enough_free_secs(sbi, 0, NR_PERSISTENT_LOG)) 793 valid_thresh_ratio = sbi->gc_thread.valid_thresh_ratio; 794 } 795 796 retry: 797 select_policy(sbi, gc_type, type, &p); 798 p.min_segno = NULL_SEGNO; 799 p.oldest_age = 0; 800 p.min_cost = get_max_cost(sbi, &p); 801 802 is_atgc = (p.gc_mode == GC_AT || p.alloc_mode == AT_SSR); 803 nsearched = 0; 804 805 if (is_atgc) 806 SIT_I(sbi)->dirty_min_mtime = ULLONG_MAX; 807 808 if (*result != NULL_SEGNO) { 809 if (!get_valid_blocks(sbi, *result, false)) { 810 ret = -ENODATA; 811 goto out; 812 } 813 814 if (sec_usage_check(sbi, GET_SEC_FROM_SEG(sbi, *result))) { 815 ret = -EBUSY; 816 goto out; 817 } 818 if (gc_type == FG_GC) 819 clear_bit(GET_SEC_FROM_SEG(sbi, *result), dirty_i->victim_secmap); 820 p.min_segno = *result; 821 goto got_result; 822 } 823 824 ret = -ENODATA; 825 if (p.max_search == 0) 826 goto out; 827 828 if (__is_large_section(sbi) && p.alloc_mode == LFS) { 829 if (sbi->next_victim_seg[BG_GC] != NULL_SEGNO) { 830 p.min_segno = sbi->next_victim_seg[BG_GC]; 831 *result = p.min_segno; 832 sbi->next_victim_seg[BG_GC] = NULL_SEGNO; 833 goto got_result; 834 } 835 if (gc_type == FG_GC && 836 sbi->next_victim_seg[FG_GC] != NULL_SEGNO) { 837 p.min_segno = sbi->next_victim_seg[FG_GC]; 838 *result = p.min_segno; 839 sbi->next_victim_seg[FG_GC] = NULL_SEGNO; 840 goto got_result; 841 } 842 } 843 844 last_victim = sm->last_victim[p.gc_mode]; 845 if (p.alloc_mode == LFS && gc_type == FG_GC) { 846 p.min_segno = check_bg_victims(sbi); 847 if (p.min_segno != NULL_SEGNO) 848 goto got_it; 849 } 850 851 while (1) { 852 unsigned long cost, *dirty_bitmap; 853 unsigned int unit_no, segno; 854 855 dirty_bitmap = p.dirty_bitmap; 856 unit_no = find_next_bit(dirty_bitmap, 857 last_segment / p.ofs_unit, 858 p.offset / p.ofs_unit); 859 segno = unit_no * p.ofs_unit; 860 if (segno >= last_segment) { 861 if (sm->last_victim[p.gc_mode]) { 862 last_segment = 863 sm->last_victim[p.gc_mode]; 864 sm->last_victim[p.gc_mode] = 0; 865 p.offset = 0; 866 continue; 867 } 868 break; 869 } 870 871 p.offset = segno + p.ofs_unit; 872 nsearched++; 873 874 #ifdef CONFIG_F2FS_CHECK_FS 875 /* 876 * skip selecting the invalid segno (that is failed due to block 877 * validity check failure during GC) to avoid endless GC loop in 878 * such cases. 879 */ 880 if (test_bit(segno, sm->invalid_segmap)) 881 goto next; 882 #endif 883 884 secno = GET_SEC_FROM_SEG(sbi, segno); 885 886 if (sec_usage_check(sbi, secno)) 887 goto next; 888 889 /* Don't touch checkpointed data */ 890 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 891 if (p.alloc_mode == LFS) { 892 /* 893 * LFS is set to find source section during GC. 894 * The victim should have no checkpointed data. 895 */ 896 if (get_ckpt_valid_blocks(sbi, segno, true)) 897 goto next; 898 } else { 899 /* 900 * SSR | AT_SSR are set to find target segment 901 * for writes which can be full by checkpointed 902 * and newly written blocks. 903 */ 904 if (!f2fs_segment_has_free_slot(sbi, segno)) 905 goto next; 906 } 907 908 if (!get_valid_blocks(sbi, segno, true)) 909 goto next; 910 } 911 912 if (gc_type == BG_GC && test_bit(secno, dirty_i->victim_secmap)) 913 goto next; 914 915 if (gc_type == FG_GC && f2fs_section_is_pinned(dirty_i, secno)) 916 goto next; 917 918 if (is_atgc) { 919 add_victim_entry(sbi, &p, segno); 920 goto next; 921 } 922 923 cost = get_gc_cost(sbi, segno, &p, valid_thresh_ratio); 924 925 if (p.min_cost > cost) { 926 p.min_segno = segno; 927 p.min_cost = cost; 928 } 929 next: 930 if (nsearched >= p.max_search) { 931 if (!sm->last_victim[p.gc_mode] && segno <= last_victim) 932 sm->last_victim[p.gc_mode] = 933 last_victim + p.ofs_unit; 934 else 935 sm->last_victim[p.gc_mode] = segno + p.ofs_unit; 936 sm->last_victim[p.gc_mode] %= 937 (MAIN_SECS(sbi) * SEGS_PER_SEC(sbi)); 938 break; 939 } 940 } 941 942 /* get victim for GC_AT/AT_SSR */ 943 if (is_atgc) { 944 lookup_victim_by_age(sbi, &p); 945 release_victim_entry(sbi); 946 } 947 948 if (is_atgc && p.min_segno == NULL_SEGNO && 949 sm->elapsed_time < p.age_threshold) { 950 p.age_threshold = 0; 951 goto retry; 952 } 953 954 if (p.min_segno != NULL_SEGNO) { 955 got_it: 956 *result = (p.min_segno / p.ofs_unit) * p.ofs_unit; 957 got_result: 958 if (p.alloc_mode == LFS) { 959 secno = GET_SEC_FROM_SEG(sbi, p.min_segno); 960 if (gc_type == FG_GC) 961 sbi->cur_victim_sec = secno; 962 else 963 set_bit(secno, dirty_i->victim_secmap); 964 } 965 ret = 0; 966 967 } 968 out: 969 if (p.min_segno != NULL_SEGNO) 970 trace_f2fs_get_victim(sbi->sb, type, gc_type, &p, 971 sbi->cur_victim_sec, 972 prefree_segments(sbi), free_segments(sbi)); 973 mutex_unlock(&dirty_i->seglist_lock); 974 975 return ret; 976 } 977 978 static struct inode *find_gc_inode(struct gc_inode_list *gc_list, nid_t ino) 979 { 980 struct inode_entry *ie; 981 982 ie = radix_tree_lookup(&gc_list->iroot, ino); 983 if (ie) 984 return ie->inode; 985 return NULL; 986 } 987 988 static void add_gc_inode(struct gc_inode_list *gc_list, struct inode *inode) 989 { 990 struct inode_entry *new_ie; 991 992 if (inode == find_gc_inode(gc_list, inode->i_ino)) { 993 iput(inode); 994 return; 995 } 996 new_ie = f2fs_kmem_cache_alloc(f2fs_inode_entry_slab, 997 GFP_NOFS, true, NULL); 998 new_ie->inode = inode; 999 1000 f2fs_radix_tree_insert(&gc_list->iroot, inode->i_ino, new_ie); 1001 list_add_tail(&new_ie->list, &gc_list->ilist); 1002 } 1003 1004 static void put_gc_inode(struct gc_inode_list *gc_list) 1005 { 1006 struct inode_entry *ie, *next_ie; 1007 1008 list_for_each_entry_safe(ie, next_ie, &gc_list->ilist, list) { 1009 radix_tree_delete(&gc_list->iroot, ie->inode->i_ino); 1010 iput(ie->inode); 1011 list_del(&ie->list); 1012 kmem_cache_free(f2fs_inode_entry_slab, ie); 1013 } 1014 } 1015 1016 static int check_valid_map(struct f2fs_sb_info *sbi, 1017 unsigned int segno, int offset) 1018 { 1019 struct sit_info *sit_i = SIT_I(sbi); 1020 struct seg_entry *sentry; 1021 int ret; 1022 1023 down_read(&sit_i->sentry_lock); 1024 sentry = get_seg_entry(sbi, segno); 1025 ret = f2fs_test_bit(offset, sentry->cur_valid_map); 1026 up_read(&sit_i->sentry_lock); 1027 return ret; 1028 } 1029 1030 /* 1031 * This function compares node address got in summary with that in NAT. 1032 * On validity, copy that node with cold status, otherwise (invalid node) 1033 * ignore that. 1034 */ 1035 static int gc_node_segment(struct f2fs_sb_info *sbi, 1036 struct f2fs_summary *sum, unsigned int segno, int gc_type, 1037 struct blk_plug *plug) 1038 { 1039 struct f2fs_summary *entry; 1040 block_t start_addr; 1041 int off; 1042 int phase = 0; 1043 bool fggc = (gc_type == FG_GC); 1044 int submitted = 0; 1045 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); 1046 1047 start_addr = START_BLOCK(sbi, segno); 1048 1049 next_step: 1050 entry = sum; 1051 1052 if (fggc && phase == 2) 1053 atomic_inc(&sbi->wb_sync_req[NODE]); 1054 1055 for (off = 0; off < usable_blks_in_seg; off++, entry++) { 1056 nid_t nid = le32_to_cpu(entry->nid); 1057 struct folio *node_folio; 1058 struct node_info ni; 1059 int err; 1060 1061 /* stop BG_GC if there is not enough free sections. */ 1062 if (gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) 1063 return submitted; 1064 1065 if (check_valid_map(sbi, segno, off) == 0) 1066 continue; 1067 1068 if (phase == 0) { 1069 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1, 1070 META_NAT, true); 1071 continue; 1072 } 1073 1074 if (phase == 1) { 1075 f2fs_ra_node_page(sbi, nid); 1076 continue; 1077 } 1078 1079 /* phase == 2 */ 1080 node_folio = f2fs_get_node_folio(sbi, nid, NODE_TYPE_REGULAR); 1081 if (IS_ERR(node_folio)) 1082 continue; 1083 1084 /* block may become invalid during f2fs_get_node_folio */ 1085 if (check_valid_map(sbi, segno, off) == 0) { 1086 f2fs_folio_put(node_folio, true); 1087 continue; 1088 } 1089 1090 if (f2fs_get_node_info(sbi, nid, &ni, false)) { 1091 f2fs_folio_put(node_folio, true); 1092 continue; 1093 } 1094 1095 if (ni.blk_addr != start_addr + off) { 1096 f2fs_folio_put(node_folio, true); 1097 continue; 1098 } 1099 1100 err = f2fs_move_node_folio(node_folio, gc_type); 1101 if (!err && gc_type == FG_GC) 1102 submitted++; 1103 stat_inc_node_blk_count(sbi, 1, gc_type); 1104 } 1105 1106 if (++phase < 3) { 1107 blk_finish_plug(plug); 1108 blk_start_plug(plug); 1109 goto next_step; 1110 } 1111 1112 if (fggc) 1113 atomic_dec(&sbi->wb_sync_req[NODE]); 1114 return submitted; 1115 } 1116 1117 /* 1118 * Calculate start block index indicating the given node offset. 1119 * Be careful, caller should give this node offset only indicating direct node 1120 * blocks. If any node offsets, which point the other types of node blocks such 1121 * as indirect or double indirect node blocks, are given, it must be a caller's 1122 * bug. 1123 */ 1124 block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode) 1125 { 1126 unsigned int indirect_blks = 2 * NIDS_PER_BLOCK + 4; 1127 unsigned int bidx; 1128 1129 if (node_ofs == 0) 1130 return 0; 1131 1132 if (node_ofs <= 2) { 1133 bidx = node_ofs - 1; 1134 } else if (node_ofs <= indirect_blks) { 1135 int dec = (node_ofs - 4) / (NIDS_PER_BLOCK + 1); 1136 1137 bidx = node_ofs - 2 - dec; 1138 } else { 1139 int dec = (node_ofs - indirect_blks - 3) / (NIDS_PER_BLOCK + 1); 1140 1141 bidx = node_ofs - 5 - dec; 1142 } 1143 return bidx * ADDRS_PER_BLOCK(inode) + ADDRS_PER_INODE(inode); 1144 } 1145 1146 static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 1147 struct node_info *dni, block_t blkaddr, unsigned int *nofs) 1148 { 1149 struct folio *node_folio; 1150 nid_t nid; 1151 unsigned int ofs_in_node, max_addrs, base; 1152 block_t source_blkaddr; 1153 1154 nid = le32_to_cpu(sum->nid); 1155 ofs_in_node = le16_to_cpu(sum->ofs_in_node); 1156 1157 node_folio = f2fs_get_node_folio(sbi, nid, NODE_TYPE_REGULAR); 1158 if (IS_ERR(node_folio)) 1159 return false; 1160 1161 if (f2fs_get_node_info(sbi, nid, dni, false)) { 1162 f2fs_folio_put(node_folio, true); 1163 return false; 1164 } 1165 1166 if (sum->version != dni->version) { 1167 f2fs_warn(sbi, "%s: valid data with mismatched node version.", 1168 __func__); 1169 set_sbi_flag(sbi, SBI_NEED_FSCK); 1170 } 1171 1172 if (f2fs_check_nid_range(sbi, dni->ino)) { 1173 f2fs_folio_put(node_folio, true); 1174 return false; 1175 } 1176 1177 if (IS_INODE(node_folio)) { 1178 base = offset_in_addr(F2FS_INODE(node_folio)); 1179 max_addrs = DEF_ADDRS_PER_INODE; 1180 } else { 1181 base = 0; 1182 max_addrs = DEF_ADDRS_PER_BLOCK; 1183 } 1184 1185 if (base + ofs_in_node >= max_addrs) { 1186 f2fs_err(sbi, "Inconsistent blkaddr offset: base:%u, ofs_in_node:%u, max:%u, ino:%u, nid:%u", 1187 base, ofs_in_node, max_addrs, dni->ino, dni->nid); 1188 f2fs_folio_put(node_folio, true); 1189 return false; 1190 } 1191 1192 *nofs = ofs_of_node(node_folio); 1193 source_blkaddr = data_blkaddr(NULL, node_folio, ofs_in_node); 1194 f2fs_folio_put(node_folio, true); 1195 1196 if (source_blkaddr != blkaddr) { 1197 #ifdef CONFIG_F2FS_CHECK_FS 1198 unsigned int segno = GET_SEGNO(sbi, blkaddr); 1199 unsigned long offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 1200 1201 if (unlikely(check_valid_map(sbi, segno, offset))) { 1202 if (!test_and_set_bit(segno, SIT_I(sbi)->invalid_segmap)) { 1203 f2fs_err(sbi, "mismatched blkaddr %u (source_blkaddr %u) in seg %u", 1204 blkaddr, source_blkaddr, segno); 1205 set_sbi_flag(sbi, SBI_NEED_FSCK); 1206 } 1207 } 1208 #endif 1209 return false; 1210 } 1211 return true; 1212 } 1213 1214 static int ra_data_block(struct inode *inode, pgoff_t index) 1215 { 1216 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 1217 struct address_space *mapping = inode->i_mapping; 1218 struct inode *atomic_inode = NULL; 1219 struct dnode_of_data dn; 1220 struct folio *folio, *efolio; 1221 struct f2fs_io_info fio = { 1222 .sbi = sbi, 1223 .ino = inode->i_ino, 1224 .type = DATA, 1225 .temp = COLD, 1226 .op = REQ_OP_READ, 1227 .op_flags = 0, 1228 .encrypted_page = NULL, 1229 .in_list = 0, 1230 }; 1231 int err = 0; 1232 1233 f2fs_down_read(&F2FS_I(inode)->i_sem); 1234 if (f2fs_is_cow_file(inode)) { 1235 atomic_inode = igrab(F2FS_I(inode)->atomic_inode); 1236 if (!atomic_inode) { 1237 f2fs_up_read(&F2FS_I(inode)->i_sem); 1238 return -EBUSY; 1239 } 1240 mapping = atomic_inode->i_mapping; 1241 } 1242 f2fs_up_read(&F2FS_I(inode)->i_sem); 1243 1244 folio = f2fs_grab_cache_folio(mapping, index, true); 1245 if (IS_ERR(folio)) { 1246 err = PTR_ERR(folio); 1247 goto out_iput; 1248 } 1249 1250 if (f2fs_lookup_read_extent_cache_block(inode, index, 1251 &dn.data_blkaddr)) { 1252 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr, 1253 DATA_GENERIC_ENHANCE_READ))) { 1254 err = -EFSCORRUPTED; 1255 goto put_folio; 1256 } 1257 goto got_it; 1258 } 1259 1260 set_new_dnode(&dn, inode, NULL, NULL, 0); 1261 err = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE); 1262 if (err) 1263 goto put_folio; 1264 f2fs_put_dnode(&dn); 1265 1266 if (!__is_valid_data_blkaddr(dn.data_blkaddr)) { 1267 err = -ENOENT; 1268 goto put_folio; 1269 } 1270 if (unlikely(!f2fs_is_valid_blkaddr(sbi, dn.data_blkaddr, 1271 DATA_GENERIC_ENHANCE))) { 1272 err = -EFSCORRUPTED; 1273 goto put_folio; 1274 } 1275 got_it: 1276 /* read folio */ 1277 fio.folio = folio; 1278 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; 1279 1280 /* 1281 * don't cache encrypted data into meta inode until previous dirty 1282 * data were writebacked to avoid racing between GC and flush. 1283 */ 1284 f2fs_folio_wait_writeback(folio, DATA, true, true); 1285 1286 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); 1287 1288 efolio = f2fs_filemap_get_folio(META_MAPPING(sbi), dn.data_blkaddr, 1289 FGP_LOCK | FGP_CREAT, GFP_NOFS); 1290 if (IS_ERR(efolio)) { 1291 err = PTR_ERR(efolio); 1292 goto put_folio; 1293 } 1294 1295 fio.encrypted_page = &efolio->page; 1296 1297 if (folio_test_uptodate(efolio)) 1298 goto put_encrypted_page; 1299 1300 err = f2fs_submit_page_bio(&fio); 1301 if (err) 1302 goto put_encrypted_page; 1303 f2fs_put_page(fio.encrypted_page, false); 1304 f2fs_folio_put(folio, true); 1305 1306 f2fs_update_iostat(sbi, inode, FS_DATA_READ_IO, F2FS_BLKSIZE); 1307 f2fs_update_iostat(sbi, NULL, FS_GDATA_READ_IO, F2FS_BLKSIZE); 1308 1309 if (atomic_inode) 1310 iput(atomic_inode); 1311 return 0; 1312 put_encrypted_page: 1313 f2fs_put_page(fio.encrypted_page, true); 1314 put_folio: 1315 f2fs_folio_put(folio, true); 1316 out_iput: 1317 if (atomic_inode) 1318 iput(atomic_inode); 1319 return err; 1320 } 1321 1322 /* 1323 * Move data block via META_MAPPING while keeping locked data page. 1324 * This can be used to move blocks, aka LBAs, directly on disk. 1325 */ 1326 static int move_data_block(struct inode *inode, block_t bidx, 1327 int gc_type, unsigned int segno, int off) 1328 { 1329 struct address_space *mapping = inode->i_mapping; 1330 struct inode *atomic_inode = NULL; 1331 struct f2fs_io_info fio = { 1332 .sbi = F2FS_I_SB(inode), 1333 .ino = inode->i_ino, 1334 .type = DATA, 1335 .temp = COLD, 1336 .op = REQ_OP_READ, 1337 .op_flags = 0, 1338 .encrypted_page = NULL, 1339 .in_list = 0, 1340 }; 1341 struct dnode_of_data dn; 1342 struct f2fs_summary sum; 1343 struct node_info ni; 1344 struct folio *folio, *mfolio, *efolio; 1345 block_t newaddr; 1346 int err = 0; 1347 bool lfs_mode = f2fs_lfs_mode(fio.sbi); 1348 int type = fio.sbi->am.atgc_enabled && (gc_type == BG_GC) && 1349 (fio.sbi->gc_mode != GC_URGENT_HIGH) ? 1350 CURSEG_ALL_DATA_ATGC : CURSEG_COLD_DATA; 1351 1352 f2fs_down_read(&F2FS_I(inode)->i_sem); 1353 if (f2fs_is_cow_file(inode)) { 1354 atomic_inode = igrab(F2FS_I(inode)->atomic_inode); 1355 if (!atomic_inode) { 1356 f2fs_up_read(&F2FS_I(inode)->i_sem); 1357 return -EBUSY; 1358 } 1359 mapping = atomic_inode->i_mapping; 1360 } 1361 f2fs_up_read(&F2FS_I(inode)->i_sem); 1362 1363 /* do not read out */ 1364 folio = f2fs_grab_cache_folio(mapping, bidx, false); 1365 if (IS_ERR(folio)) { 1366 err = PTR_ERR(folio); 1367 goto out_iput; 1368 } 1369 1370 if (!check_valid_map(F2FS_I_SB(inode), segno, off)) { 1371 err = -ENOENT; 1372 goto out; 1373 } 1374 1375 err = f2fs_gc_pinned_control(inode, gc_type, segno); 1376 if (err) 1377 goto out; 1378 1379 set_new_dnode(&dn, inode, NULL, NULL, 0); 1380 err = f2fs_get_dnode_of_data(&dn, bidx, LOOKUP_NODE); 1381 if (err) 1382 goto out; 1383 1384 if (unlikely(dn.data_blkaddr == NULL_ADDR)) { 1385 folio_clear_uptodate(folio); 1386 err = -ENOENT; 1387 goto put_out; 1388 } 1389 1390 /* 1391 * don't cache encrypted data into meta inode until previous dirty 1392 * data were writebacked to avoid racing between GC and flush. 1393 */ 1394 f2fs_folio_wait_writeback(folio, DATA, true, true); 1395 1396 f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); 1397 1398 err = f2fs_get_node_info(fio.sbi, dn.nid, &ni, false); 1399 if (err) 1400 goto put_out; 1401 1402 /* read page */ 1403 fio.folio = folio; 1404 fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; 1405 1406 if (lfs_mode) 1407 f2fs_down_write(&fio.sbi->io_order_lock); 1408 1409 mfolio = f2fs_grab_cache_folio(META_MAPPING(fio.sbi), 1410 fio.old_blkaddr, false); 1411 if (IS_ERR(mfolio)) { 1412 err = PTR_ERR(mfolio); 1413 goto up_out; 1414 } 1415 1416 fio.encrypted_page = folio_file_page(mfolio, fio.old_blkaddr); 1417 1418 /* read source block in mfolio */ 1419 if (!folio_test_uptodate(mfolio)) { 1420 err = f2fs_submit_page_bio(&fio); 1421 if (err) { 1422 f2fs_folio_put(mfolio, true); 1423 goto up_out; 1424 } 1425 1426 f2fs_update_iostat(fio.sbi, inode, FS_DATA_READ_IO, 1427 F2FS_BLKSIZE); 1428 f2fs_update_iostat(fio.sbi, NULL, FS_GDATA_READ_IO, 1429 F2FS_BLKSIZE); 1430 1431 folio_lock(mfolio); 1432 if (unlikely(!is_meta_folio(mfolio) || 1433 !folio_test_uptodate(mfolio))) { 1434 err = -EIO; 1435 f2fs_folio_put(mfolio, true); 1436 goto up_out; 1437 } 1438 } 1439 1440 set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version); 1441 1442 /* allocate block address */ 1443 err = f2fs_allocate_data_block(fio.sbi, NULL, fio.old_blkaddr, &newaddr, 1444 &sum, type, NULL); 1445 if (err) { 1446 f2fs_folio_put(mfolio, true); 1447 /* filesystem should shutdown, no need to recovery block */ 1448 goto up_out; 1449 } 1450 1451 efolio = f2fs_filemap_get_folio(META_MAPPING(fio.sbi), newaddr, 1452 FGP_LOCK | FGP_CREAT, GFP_NOFS); 1453 if (IS_ERR(efolio)) { 1454 err = PTR_ERR(efolio); 1455 f2fs_folio_put(mfolio, true); 1456 goto recover_block; 1457 } 1458 1459 fio.encrypted_page = &efolio->page; 1460 1461 /* write target block */ 1462 f2fs_wait_on_page_writeback(fio.encrypted_page, DATA, true, true); 1463 memcpy(page_address(fio.encrypted_page), 1464 folio_address(mfolio), PAGE_SIZE); 1465 f2fs_folio_put(mfolio, true); 1466 1467 f2fs_invalidate_internal_cache(fio.sbi, fio.old_blkaddr, 1); 1468 1469 set_page_dirty(fio.encrypted_page); 1470 if (clear_page_dirty_for_io(fio.encrypted_page)) 1471 dec_page_count(fio.sbi, F2FS_DIRTY_META); 1472 1473 set_page_writeback(fio.encrypted_page); 1474 1475 fio.op = REQ_OP_WRITE; 1476 fio.op_flags = REQ_SYNC; 1477 fio.new_blkaddr = newaddr; 1478 f2fs_submit_page_write(&fio); 1479 1480 f2fs_update_iostat(fio.sbi, NULL, FS_GC_DATA_IO, F2FS_BLKSIZE); 1481 1482 f2fs_update_data_blkaddr(&dn, newaddr); 1483 set_inode_flag(inode, FI_APPEND_WRITE); 1484 1485 f2fs_put_page(fio.encrypted_page, true); 1486 recover_block: 1487 if (err) 1488 f2fs_do_replace_block(fio.sbi, &sum, newaddr, fio.old_blkaddr, 1489 true, true, true); 1490 up_out: 1491 if (lfs_mode) 1492 f2fs_up_write(&fio.sbi->io_order_lock); 1493 put_out: 1494 f2fs_put_dnode(&dn); 1495 out: 1496 f2fs_folio_put(folio, true); 1497 out_iput: 1498 if (atomic_inode) 1499 iput(atomic_inode); 1500 return err; 1501 } 1502 1503 static int move_data_page(struct inode *inode, block_t bidx, int gc_type, 1504 unsigned int segno, int off) 1505 { 1506 struct folio *folio; 1507 int err = 0; 1508 1509 folio = f2fs_get_lock_data_folio(inode, bidx, true); 1510 if (IS_ERR(folio)) 1511 return PTR_ERR(folio); 1512 1513 if (!check_valid_map(F2FS_I_SB(inode), segno, off)) { 1514 err = -ENOENT; 1515 goto out; 1516 } 1517 1518 err = f2fs_gc_pinned_control(inode, gc_type, segno); 1519 if (err) 1520 goto out; 1521 1522 if (gc_type == BG_GC) { 1523 if (folio_test_writeback(folio)) { 1524 err = -EAGAIN; 1525 goto out; 1526 } 1527 folio_mark_dirty(folio); 1528 folio_set_f2fs_gcing(folio); 1529 } else { 1530 struct f2fs_io_info fio = { 1531 .sbi = F2FS_I_SB(inode), 1532 .ino = inode->i_ino, 1533 .type = DATA, 1534 .temp = COLD, 1535 .op = REQ_OP_WRITE, 1536 .op_flags = REQ_SYNC, 1537 .old_blkaddr = NULL_ADDR, 1538 .folio = folio, 1539 .encrypted_page = NULL, 1540 .need_lock = LOCK_REQ, 1541 .io_type = FS_GC_DATA_IO, 1542 }; 1543 bool is_dirty = folio_test_dirty(folio); 1544 1545 retry: 1546 f2fs_folio_wait_writeback(folio, DATA, true, true); 1547 1548 folio_mark_dirty(folio); 1549 if (folio_clear_dirty_for_io(folio)) { 1550 inode_dec_dirty_pages(inode); 1551 f2fs_remove_dirty_inode(inode); 1552 } 1553 1554 folio_set_f2fs_gcing(folio); 1555 1556 err = f2fs_do_write_data_page(&fio); 1557 if (err) { 1558 folio_clear_f2fs_gcing(folio); 1559 if (err == -ENOMEM) { 1560 memalloc_retry_wait(GFP_NOFS); 1561 goto retry; 1562 } 1563 if (is_dirty) 1564 folio_mark_dirty(folio); 1565 } 1566 } 1567 out: 1568 f2fs_folio_put(folio, true); 1569 return err; 1570 } 1571 1572 /* 1573 * This function tries to get parent node of victim data block, and identifies 1574 * data block validity. If the block is valid, copy that with cold status and 1575 * modify parent node. 1576 * If the parent node is not valid or the data block address is different, 1577 * the victim data block is ignored. 1578 */ 1579 static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 1580 struct gc_inode_list *gc_list, unsigned int segno, int gc_type, 1581 bool force_migrate, struct blk_plug *plug) 1582 { 1583 struct super_block *sb = sbi->sb; 1584 struct f2fs_summary *entry; 1585 block_t start_addr; 1586 int off; 1587 int phase = 0; 1588 int submitted = 0; 1589 unsigned int usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); 1590 1591 start_addr = START_BLOCK(sbi, segno); 1592 1593 next_step: 1594 entry = sum; 1595 1596 for (off = 0; off < usable_blks_in_seg; off++, entry++) { 1597 struct inode *inode; 1598 struct node_info dni; /* dnode info for the data */ 1599 unsigned int ofs_in_node, nofs; 1600 block_t start_bidx; 1601 nid_t nid = le32_to_cpu(entry->nid); 1602 1603 /* 1604 * stop BG_GC if there is not enough free sections. 1605 * Or, stop GC if the segment becomes fully valid caused by 1606 * race condition along with SSR block allocation. 1607 */ 1608 if ((gc_type == BG_GC && has_not_enough_free_secs(sbi, 0, 0)) || 1609 (!force_migrate && get_valid_blocks(sbi, segno, true) == 1610 CAP_BLKS_PER_SEC(sbi))) 1611 return submitted; 1612 1613 if (check_valid_map(sbi, segno, off) == 0) 1614 continue; 1615 1616 if (phase == 0) { 1617 f2fs_ra_meta_pages(sbi, NAT_BLOCK_OFFSET(nid), 1, 1618 META_NAT, true); 1619 continue; 1620 } 1621 1622 if (phase == 1) { 1623 f2fs_ra_node_page(sbi, nid); 1624 continue; 1625 } 1626 1627 /* Get an inode by ino with checking validity */ 1628 if (!is_alive(sbi, entry, &dni, start_addr + off, &nofs)) 1629 continue; 1630 1631 if (phase == 2) { 1632 f2fs_ra_node_page(sbi, dni.ino); 1633 continue; 1634 } 1635 1636 ofs_in_node = le16_to_cpu(entry->ofs_in_node); 1637 1638 if (phase == 3) { 1639 struct folio *data_folio; 1640 int err; 1641 1642 inode = f2fs_iget(sb, dni.ino); 1643 if (IS_ERR(inode)) 1644 continue; 1645 1646 if (is_bad_inode(inode) || 1647 special_file(inode->i_mode)) { 1648 iput(inode); 1649 continue; 1650 } 1651 1652 if (f2fs_has_inline_data(inode)) { 1653 iput(inode); 1654 set_sbi_flag(sbi, SBI_NEED_FSCK); 1655 f2fs_err_ratelimited(sbi, 1656 "inode %llu has both inline_data flag and " 1657 "data block, nid=%u, ofs_in_node=%u", 1658 inode->i_ino, dni.nid, ofs_in_node); 1659 continue; 1660 } 1661 1662 err = f2fs_gc_pinned_control(inode, gc_type, segno); 1663 if (err == -EAGAIN) { 1664 iput(inode); 1665 return submitted; 1666 } 1667 1668 if (!f2fs_down_write_trylock( 1669 &F2FS_I(inode)->i_gc_rwsem[WRITE])) { 1670 iput(inode); 1671 sbi->skipped_gc_rwsem++; 1672 continue; 1673 } 1674 1675 start_bidx = f2fs_start_bidx_of_node(nofs, inode) + 1676 ofs_in_node; 1677 1678 if (f2fs_meta_inode_gc_required(inode)) { 1679 int err = ra_data_block(inode, start_bidx); 1680 1681 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1682 if (err) { 1683 iput(inode); 1684 continue; 1685 } 1686 add_gc_inode(gc_list, inode); 1687 continue; 1688 } 1689 1690 data_folio = f2fs_get_read_data_folio(inode, start_bidx, 1691 REQ_RAHEAD, true, NULL); 1692 f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); 1693 if (IS_ERR(data_folio)) { 1694 iput(inode); 1695 continue; 1696 } 1697 1698 f2fs_folio_put(data_folio, false); 1699 add_gc_inode(gc_list, inode); 1700 continue; 1701 } 1702 1703 /* phase 4 */ 1704 inode = find_gc_inode(gc_list, dni.ino); 1705 if (inode) { 1706 struct f2fs_inode_info *fi = F2FS_I(inode); 1707 bool locked = false; 1708 int err; 1709 1710 if (S_ISREG(inode->i_mode)) { 1711 if (!f2fs_down_write_trylock(&fi->i_gc_rwsem[WRITE])) { 1712 sbi->skipped_gc_rwsem++; 1713 continue; 1714 } 1715 if (!f2fs_down_write_trylock( 1716 &fi->i_gc_rwsem[READ])) { 1717 sbi->skipped_gc_rwsem++; 1718 f2fs_up_write(&fi->i_gc_rwsem[WRITE]); 1719 continue; 1720 } 1721 locked = true; 1722 1723 /* wait for all inflight aio data */ 1724 inode_dio_wait(inode); 1725 } 1726 1727 start_bidx = f2fs_start_bidx_of_node(nofs, inode) 1728 + ofs_in_node; 1729 if (f2fs_meta_inode_gc_required(inode)) 1730 err = move_data_block(inode, start_bidx, 1731 gc_type, segno, off); 1732 else 1733 err = move_data_page(inode, start_bidx, gc_type, 1734 segno, off); 1735 1736 if (!err && (gc_type == FG_GC || 1737 f2fs_meta_inode_gc_required(inode))) 1738 submitted++; 1739 1740 if (locked) { 1741 f2fs_up_write(&fi->i_gc_rwsem[READ]); 1742 f2fs_up_write(&fi->i_gc_rwsem[WRITE]); 1743 } 1744 1745 stat_inc_data_blk_count(sbi, 1, gc_type); 1746 } 1747 } 1748 1749 if (++phase < 5) { 1750 blk_finish_plug(plug); 1751 blk_start_plug(plug); 1752 goto next_step; 1753 } 1754 1755 return submitted; 1756 } 1757 1758 static int __get_victim(struct f2fs_sb_info *sbi, unsigned int *victim, 1759 int gc_type, bool one_time) 1760 { 1761 struct sit_info *sit_i = SIT_I(sbi); 1762 int ret; 1763 1764 down_write(&sit_i->sentry_lock); 1765 ret = f2fs_get_victim(sbi, victim, gc_type, NO_CHECK_TYPE, 1766 LFS, 0, one_time); 1767 up_write(&sit_i->sentry_lock); 1768 return ret; 1769 } 1770 1771 static int do_garbage_collect(struct f2fs_sb_info *sbi, 1772 unsigned int start_segno, 1773 struct gc_inode_list *gc_list, int gc_type, 1774 bool force_migrate, bool one_time) 1775 { 1776 struct blk_plug plug; 1777 unsigned int segno = start_segno; 1778 unsigned int end_segno = start_segno + SEGS_PER_SEC(sbi); 1779 unsigned int sec_end_segno; 1780 int seg_freed = 0, migrated = 0; 1781 unsigned char type; 1782 unsigned char data_type; 1783 int submitted = 0, sum_blk_cnt; 1784 1785 if (__is_large_section(sbi)) { 1786 sec_end_segno = rounddown(end_segno, SEGS_PER_SEC(sbi)); 1787 1788 /* 1789 * zone-capacity can be less than zone-size in zoned devices, 1790 * resulting in less than expected usable segments in the zone, 1791 * calculate the end segno in the zone which can be garbage 1792 * collected 1793 */ 1794 if (f2fs_sb_has_blkzoned(sbi)) 1795 sec_end_segno -= SEGS_PER_SEC(sbi) - 1796 f2fs_usable_segs_in_sec(sbi); 1797 1798 if (gc_type == BG_GC || one_time) { 1799 unsigned int window_granularity = 1800 sbi->migration_window_granularity; 1801 1802 if (f2fs_sb_has_blkzoned(sbi) && 1803 !has_enough_free_blocks(sbi, 1804 sbi->gc_thread.boost_zoned_gc_percent)) 1805 window_granularity *= 1806 sbi->gc_thread.boost_gc_multiple; 1807 1808 end_segno = start_segno + window_granularity; 1809 } 1810 1811 if (end_segno > sec_end_segno) 1812 end_segno = sec_end_segno; 1813 } 1814 1815 sanity_check_seg_type(sbi, get_seg_entry(sbi, segno)->type); 1816 1817 segno = rounddown(segno, sbi->sums_per_block); 1818 sum_blk_cnt = DIV_ROUND_UP(end_segno - segno, sbi->sums_per_block); 1819 /* readahead multi ssa blocks those have contiguous address */ 1820 if (__is_large_section(sbi)) 1821 f2fs_ra_meta_pages(sbi, GET_SUM_BLOCK(sbi, segno), 1822 sum_blk_cnt, META_SSA, true); 1823 1824 /* reference all summary page */ 1825 while (segno < end_segno) { 1826 struct folio *sum_folio = f2fs_get_sum_folio(sbi, segno); 1827 1828 segno += sbi->sums_per_block; 1829 if (IS_ERR(sum_folio)) { 1830 int err = PTR_ERR(sum_folio); 1831 1832 end_segno = segno - sbi->sums_per_block; 1833 segno = rounddown(start_segno, sbi->sums_per_block); 1834 while (segno < end_segno) { 1835 sum_folio = filemap_get_folio(META_MAPPING(sbi), 1836 GET_SUM_BLOCK(sbi, segno)); 1837 folio_put_refs(sum_folio, 2); 1838 segno += sbi->sums_per_block; 1839 } 1840 return err; 1841 } 1842 folio_unlock(sum_folio); 1843 } 1844 1845 blk_start_plug(&plug); 1846 1847 segno = start_segno; 1848 while (segno < end_segno) { 1849 unsigned int cur_segno; 1850 1851 /* find segment summary of victim */ 1852 struct folio *sum_folio = filemap_get_folio(META_MAPPING(sbi), 1853 GET_SUM_BLOCK(sbi, segno)); 1854 unsigned int block_end_segno = rounddown(segno, sbi->sums_per_block) 1855 + sbi->sums_per_block; 1856 1857 if (block_end_segno > end_segno) 1858 block_end_segno = end_segno; 1859 1860 if (is_cursec(sbi, GET_SEC_FROM_SEG(sbi, segno))) { 1861 f2fs_err(sbi, "%s: segment %u is used by log", 1862 __func__, segno); 1863 f2fs_bug_on(sbi, 1); 1864 goto next_block; 1865 } 1866 1867 if (!folio_test_uptodate(sum_folio) || 1868 unlikely(f2fs_cp_error(sbi))) 1869 goto next_block; 1870 1871 for (cur_segno = segno; cur_segno < block_end_segno; 1872 cur_segno++) { 1873 struct f2fs_summary_block *sum; 1874 1875 if (get_valid_blocks(sbi, cur_segno, false) == 0) 1876 goto freed; 1877 if (gc_type == BG_GC && __is_large_section(sbi) && 1878 migrated >= sbi->migration_granularity) 1879 continue; 1880 1881 if (migrated == 0) { 1882 type = IS_DATASEG(get_seg_entry(sbi, cur_segno)->type) ? 1883 SUM_TYPE_DATA : SUM_TYPE_NODE; 1884 data_type = (type == SUM_TYPE_DATA) ? DATA : NODE; 1885 } 1886 1887 sum = SUM_BLK_PAGE_ADDR(sbi, sum_folio, cur_segno); 1888 if (type != GET_SUM_TYPE(sum_footer(sbi, sum))) { 1889 f2fs_err(sbi, "Inconsistent segment (%u) type " 1890 "[%d, %d] in SIT and SSA", 1891 cur_segno, type, 1892 GET_SUM_TYPE( 1893 sum_footer(sbi, sum))); 1894 f2fs_stop_checkpoint(sbi, false, 1895 STOP_CP_REASON_CORRUPTED_SUMMARY); 1896 continue; 1897 } 1898 1899 /* 1900 * this is to avoid deadlock: 1901 * - lock_page(sum_page) - f2fs_replace_block 1902 * - check_valid_map() - down_write(sentry_lock) 1903 * - down_read(sentry_lock) - change_curseg() 1904 * - lock_page(sum_page) 1905 */ 1906 if (type == SUM_TYPE_NODE) 1907 submitted += gc_node_segment(sbi, sum->entries, 1908 cur_segno, gc_type, &plug); 1909 else 1910 submitted += gc_data_segment(sbi, sum->entries, 1911 gc_list, cur_segno, 1912 gc_type, force_migrate, &plug); 1913 1914 stat_inc_gc_seg_count(sbi, data_type, gc_type); 1915 sbi->gc_reclaimed_segs[sbi->gc_mode]++; 1916 migrated++; 1917 1918 freed: 1919 if (gc_type == FG_GC && 1920 get_valid_blocks(sbi, cur_segno, false) == 0) 1921 seg_freed++; 1922 1923 if (__is_large_section(sbi)) 1924 sbi->next_victim_seg[gc_type] = 1925 (cur_segno + 1 < sec_end_segno) ? 1926 cur_segno + 1 : NULL_SEGNO; 1927 1928 if (unlikely(freezing(current))) { 1929 folio_put_refs(sum_folio, 2); 1930 goto stop; 1931 } 1932 } 1933 next_block: 1934 folio_put_refs(sum_folio, 2); 1935 segno = block_end_segno; 1936 } 1937 1938 stop: 1939 if (submitted) 1940 f2fs_submit_merged_write(sbi, data_type); 1941 1942 blk_finish_plug(&plug); 1943 1944 if (migrated) 1945 stat_inc_gc_sec_count(sbi, data_type, gc_type); 1946 1947 return seg_freed; 1948 } 1949 1950 int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control) 1951 { 1952 int gc_type = gc_control->init_gc_type; 1953 unsigned int segno = gc_control->victim_segno; 1954 int sec_freed = 0, seg_freed = 0, total_freed = 0, total_sec_freed = 0; 1955 int ret = 0; 1956 struct cp_control cpc; 1957 struct gc_inode_list gc_list = { 1958 .ilist = LIST_HEAD_INIT(gc_list.ilist), 1959 .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS), 1960 }; 1961 unsigned int skipped_round = 0, round = 0; 1962 unsigned int upper_secs; 1963 1964 trace_f2fs_gc_begin(sbi->sb, gc_type, gc_control->no_bg_gc, 1965 gc_control->nr_free_secs, 1966 get_pages(sbi, F2FS_DIRTY_NODES), 1967 get_pages(sbi, F2FS_DIRTY_DENTS), 1968 get_pages(sbi, F2FS_DIRTY_IMETA), 1969 free_sections(sbi), 1970 free_segments(sbi), 1971 reserved_segments(sbi), 1972 prefree_segments(sbi)); 1973 1974 cpc.reason = __get_cp_reason(sbi); 1975 gc_more: 1976 sbi->skipped_gc_rwsem = 0; 1977 if (unlikely(!(sbi->sb->s_flags & SB_ACTIVE))) { 1978 ret = -EINVAL; 1979 goto stop; 1980 } 1981 if (unlikely(f2fs_cp_error(sbi))) { 1982 ret = -EIO; 1983 goto stop; 1984 } 1985 1986 /* Let's run FG_GC, if we don't have enough space. */ 1987 if (has_not_enough_free_secs(sbi, 0, 0)) { 1988 gc_type = FG_GC; 1989 gc_control->one_time = false; 1990 1991 /* 1992 * For example, if there are many prefree_segments below given 1993 * threshold, we can make them free by checkpoint. Then, we 1994 * secure free segments which doesn't need fggc any more. 1995 */ 1996 if (prefree_segments(sbi)) { 1997 stat_inc_cp_call_count(sbi, TOTAL_CALL); 1998 ret = f2fs_write_checkpoint(sbi, &cpc); 1999 if (ret) 2000 goto stop; 2001 /* Reset due to checkpoint */ 2002 sec_freed = 0; 2003 } 2004 } 2005 2006 /* f2fs_balance_fs doesn't need to do BG_GC in critical path. */ 2007 if (gc_type == BG_GC && gc_control->no_bg_gc) { 2008 ret = -EINVAL; 2009 goto stop; 2010 } 2011 retry: 2012 if (unlikely(freezing(current))) { 2013 ret = 0; 2014 goto stop; 2015 } 2016 ret = __get_victim(sbi, &segno, gc_type, gc_control->one_time); 2017 if (ret) { 2018 /* allow to search victim from sections has pinned data */ 2019 if (ret == -ENODATA && gc_type == FG_GC && 2020 f2fs_pinned_section_exists(DIRTY_I(sbi))) { 2021 f2fs_unpin_all_sections(sbi, false); 2022 goto retry; 2023 } 2024 goto stop; 2025 } 2026 2027 seg_freed = do_garbage_collect(sbi, segno, &gc_list, gc_type, 2028 gc_control->should_migrate_blocks, 2029 gc_control->one_time); 2030 if (seg_freed < 0) 2031 goto stop; 2032 2033 total_freed += seg_freed; 2034 2035 if (seg_freed == f2fs_usable_segs_in_sec(sbi)) { 2036 sec_freed++; 2037 total_sec_freed++; 2038 } 2039 2040 if (gc_control->one_time) 2041 goto stop; 2042 2043 if (gc_type == FG_GC) { 2044 sbi->cur_victim_sec = NULL_SEGNO; 2045 2046 if (has_enough_free_secs(sbi, sec_freed, 0)) { 2047 if (!gc_control->no_bg_gc && 2048 total_sec_freed < gc_control->nr_free_secs) 2049 goto go_gc_more; 2050 goto stop; 2051 } 2052 if (sbi->skipped_gc_rwsem) 2053 skipped_round++; 2054 round++; 2055 if (skipped_round > MAX_SKIP_GC_COUNT && 2056 skipped_round * 2 >= round) { 2057 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2058 ret = f2fs_write_checkpoint(sbi, &cpc); 2059 goto stop; 2060 } 2061 } else if (has_enough_free_secs(sbi, 0, 0)) { 2062 goto stop; 2063 } 2064 2065 upper_secs = __get_secs_required(sbi); 2066 2067 /* 2068 * Write checkpoint to reclaim prefree segments. 2069 * We need more three extra sections for writer's data/node/dentry. 2070 */ 2071 if (free_sections(sbi) <= upper_secs + NR_GC_CHECKPOINT_SECS && 2072 prefree_segments(sbi)) { 2073 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2074 ret = f2fs_write_checkpoint(sbi, &cpc); 2075 if (ret) 2076 goto stop; 2077 /* Reset due to checkpoint */ 2078 sec_freed = 0; 2079 } 2080 go_gc_more: 2081 segno = NULL_SEGNO; 2082 goto gc_more; 2083 2084 stop: 2085 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 0; 2086 SIT_I(sbi)->last_victim[FLUSH_DEVICE] = gc_control->victim_segno; 2087 2088 if (gc_type == FG_GC) 2089 f2fs_unpin_all_sections(sbi, true); 2090 2091 trace_f2fs_gc_end(sbi->sb, ret, total_freed, total_sec_freed, 2092 get_pages(sbi, F2FS_DIRTY_NODES), 2093 get_pages(sbi, F2FS_DIRTY_DENTS), 2094 get_pages(sbi, F2FS_DIRTY_IMETA), 2095 free_sections(sbi), 2096 free_segments(sbi), 2097 reserved_segments(sbi), 2098 prefree_segments(sbi)); 2099 2100 f2fs_up_write_trace(&sbi->gc_lock, &gc_control->lc); 2101 2102 put_gc_inode(&gc_list); 2103 2104 if (gc_control->err_gc_skipped && !ret) 2105 ret = total_sec_freed ? 0 : -EAGAIN; 2106 return ret; 2107 } 2108 2109 int __init f2fs_create_garbage_collection_cache(void) 2110 { 2111 victim_entry_slab = f2fs_kmem_cache_create("f2fs_victim_entry", 2112 sizeof(struct victim_entry)); 2113 return victim_entry_slab ? 0 : -ENOMEM; 2114 } 2115 2116 void f2fs_destroy_garbage_collection_cache(void) 2117 { 2118 kmem_cache_destroy(victim_entry_slab); 2119 } 2120 2121 static void init_atgc_management(struct f2fs_sb_info *sbi) 2122 { 2123 struct atgc_management *am = &sbi->am; 2124 2125 if (test_opt(sbi, ATGC) && 2126 SIT_I(sbi)->elapsed_time >= DEF_GC_THREAD_AGE_THRESHOLD) 2127 am->atgc_enabled = true; 2128 2129 am->root = RB_ROOT_CACHED; 2130 INIT_LIST_HEAD(&am->victim_list); 2131 am->victim_count = 0; 2132 2133 am->candidate_ratio = DEF_GC_THREAD_CANDIDATE_RATIO; 2134 am->max_candidate_count = DEF_GC_THREAD_MAX_CANDIDATE_COUNT; 2135 am->age_weight = DEF_GC_THREAD_AGE_WEIGHT; 2136 am->age_threshold = DEF_GC_THREAD_AGE_THRESHOLD; 2137 } 2138 2139 void f2fs_build_gc_manager(struct f2fs_sb_info *sbi) 2140 { 2141 sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES; 2142 2143 /* give warm/cold data area from slower device */ 2144 if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi)) 2145 SIT_I(sbi)->last_victim[ALLOC_NEXT] = 2146 GET_SEGNO(sbi, FDEV(0).end_blk) + 1; 2147 2148 init_atgc_management(sbi); 2149 } 2150 2151 int f2fs_gc_range(struct f2fs_sb_info *sbi, 2152 unsigned int start_seg, unsigned int end_seg, 2153 bool dry_run, unsigned int dry_run_sections, bool lock) 2154 { 2155 struct f2fs_lock_context lc; 2156 unsigned int segno; 2157 unsigned int gc_secs = dry_run_sections; 2158 2159 if (unlikely(f2fs_cp_error(sbi))) 2160 return -EIO; 2161 2162 stat_inc_gc_call_count(sbi, FOREGROUND); 2163 for (segno = start_seg; segno <= end_seg; segno += SEGS_PER_SEC(sbi)) { 2164 struct gc_inode_list gc_list = { 2165 .ilist = LIST_HEAD_INIT(gc_list.ilist), 2166 .iroot = RADIX_TREE_INIT(gc_list.iroot, GFP_NOFS), 2167 }; 2168 int err = 0; 2169 2170 if (lock) 2171 f2fs_down_write_trace(&sbi->gc_lock, &lc); 2172 2173 /* 2174 * avoid migrating empty section, as it can be allocated by 2175 * log in parallel. 2176 */ 2177 if (!get_valid_blocks(sbi, segno, true)) 2178 goto next; 2179 2180 if (is_cursec(sbi, GET_SEC_FROM_SEG(sbi, segno))) 2181 goto next; 2182 2183 do_garbage_collect(sbi, segno, &gc_list, FG_GC, true, false); 2184 put_gc_inode(&gc_list); 2185 2186 /* reset all pinned status during fggc */ 2187 f2fs_unpin_all_sections(sbi, true); 2188 2189 if (!dry_run && get_valid_blocks(sbi, segno, true)) { 2190 err = -EAGAIN; 2191 goto next; 2192 } 2193 if (dry_run && dry_run_sections && 2194 !get_valid_blocks(sbi, segno, true)) { 2195 --gc_secs; 2196 goto next; 2197 } 2198 2199 if (fatal_signal_pending(current)) 2200 err = -ERESTARTSYS; 2201 next: 2202 if (lock) 2203 f2fs_up_write_trace(&sbi->gc_lock, &lc); 2204 if (err) 2205 return err; 2206 if (dry_run && dry_run_sections && !gc_secs) 2207 return 0; 2208 } 2209 2210 return 0; 2211 } 2212 2213 void f2fs_reset_gc_victim_resource(struct f2fs_sb_info *sbi, 2214 unsigned int start, unsigned int end) 2215 { 2216 int i; 2217 2218 mutex_lock(&DIRTY_I(sbi)->seglist_lock); 2219 for (i = 0; i < MAX_GC_POLICY; i++) 2220 if (SIT_I(sbi)->last_victim[i] >= start && 2221 SIT_I(sbi)->last_victim[i] <= end) 2222 SIT_I(sbi)->last_victim[i] = 0; 2223 2224 for (i = BG_GC; i <= FG_GC; i++) 2225 if (sbi->next_victim_seg[i] >= start && 2226 sbi->next_victim_seg[i] <= end) 2227 sbi->next_victim_seg[i] = NULL_SEGNO; 2228 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); 2229 } 2230 2231 static int free_segment_range(struct f2fs_sb_info *sbi, 2232 unsigned int secs, bool dry_run) 2233 { 2234 unsigned int secno, next_inuse, start, end, end_secno; 2235 struct cp_control cpc = { CP_RESIZE, 0, 0, 0 }; 2236 unsigned int freed_secs = 0; 2237 int err = 0; 2238 int type; 2239 2240 MAIN_SECS(sbi) -= secs; 2241 start = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi); 2242 end = MAIN_SEGS(sbi) - 1; 2243 end_secno = GET_SEC_FROM_SEG(sbi, end); 2244 2245 f2fs_reset_gc_victim_resource(sbi, start, end); 2246 2247 spin_lock(&FREE_I(sbi)->segmap_lock); 2248 for (secno = MAIN_SECS(sbi); secno <= end_secno; secno++) { 2249 if (!test_bit(secno, FREE_I(sbi)->free_secmap)) 2250 freed_secs++; 2251 } 2252 FREE_I(sbi)->free_sections -= freed_secs; 2253 spin_unlock(&FREE_I(sbi)->segmap_lock); 2254 2255 /* Move out cursegs from the target range */ 2256 for (type = CURSEG_HOT_DATA; type < NR_CURSEG_TYPE; type++) { 2257 err = f2fs_allocate_segment_for_resize(sbi, type, start, end); 2258 if (err) 2259 goto out; 2260 } 2261 2262 /* do GC to move out valid blocks in the range */ 2263 err = f2fs_gc_range(sbi, start, end, dry_run, 0, false); 2264 if (err || dry_run) 2265 goto out; 2266 2267 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2268 err = f2fs_write_checkpoint(sbi, &cpc); 2269 if (err) 2270 goto out; 2271 2272 next_inuse = find_next_inuse(FREE_I(sbi), end + 1, start); 2273 if (next_inuse <= end) { 2274 f2fs_err(sbi, "segno %u should be free but still inuse!", 2275 next_inuse); 2276 f2fs_bug_on(sbi, 1); 2277 } 2278 out: 2279 spin_lock(&FREE_I(sbi)->segmap_lock); 2280 FREE_I(sbi)->free_sections += freed_secs; 2281 spin_unlock(&FREE_I(sbi)->segmap_lock); 2282 MAIN_SECS(sbi) += secs; 2283 return err; 2284 } 2285 2286 static void update_sb_metadata(struct f2fs_sb_info *sbi, int secs) 2287 { 2288 struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi); 2289 int section_count; 2290 int segment_count; 2291 int segment_count_main; 2292 long long block_count; 2293 int segs = secs * SEGS_PER_SEC(sbi); 2294 2295 f2fs_down_write(&sbi->sb_lock); 2296 2297 section_count = le32_to_cpu(raw_sb->section_count); 2298 segment_count = le32_to_cpu(raw_sb->segment_count); 2299 segment_count_main = le32_to_cpu(raw_sb->segment_count_main); 2300 block_count = le64_to_cpu(raw_sb->block_count); 2301 2302 raw_sb->section_count = cpu_to_le32(section_count + secs); 2303 raw_sb->segment_count = cpu_to_le32(segment_count + segs); 2304 raw_sb->segment_count_main = cpu_to_le32(segment_count_main + segs); 2305 raw_sb->block_count = cpu_to_le64(block_count + 2306 (long long)SEGS_TO_BLKS(sbi, segs)); 2307 if (f2fs_is_multi_device(sbi)) { 2308 int last_dev = sbi->s_ndevs - 1; 2309 int dev_segs = 2310 le32_to_cpu(raw_sb->devs[last_dev].total_segments); 2311 2312 raw_sb->devs[last_dev].total_segments = 2313 cpu_to_le32(dev_segs + segs); 2314 } 2315 2316 f2fs_up_write(&sbi->sb_lock); 2317 } 2318 2319 static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs) 2320 { 2321 int segs = secs * SEGS_PER_SEC(sbi); 2322 long long blks = SEGS_TO_BLKS(sbi, segs); 2323 long long user_block_count = 2324 le64_to_cpu(F2FS_CKPT(sbi)->user_block_count); 2325 2326 SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs; 2327 MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs; 2328 MAIN_SECS(sbi) += secs; 2329 if (sbi->allocate_section_hint > MAIN_SECS(sbi)) 2330 sbi->allocate_section_hint = MAIN_SECS(sbi); 2331 FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs; 2332 FREE_I(sbi)->free_segments = (int)FREE_I(sbi)->free_segments + segs; 2333 F2FS_CKPT(sbi)->user_block_count = cpu_to_le64(user_block_count + blks); 2334 2335 if (f2fs_is_multi_device(sbi)) { 2336 int last_dev = sbi->s_ndevs - 1; 2337 2338 sbi->allocate_section_hint = FDEV(0).total_segments / 2339 SEGS_PER_SEC(sbi); 2340 2341 FDEV(last_dev).total_segments = 2342 (int)FDEV(last_dev).total_segments + segs; 2343 FDEV(last_dev).end_blk = 2344 (long long)FDEV(last_dev).end_blk + blks; 2345 #ifdef CONFIG_BLK_DEV_ZONED 2346 FDEV(last_dev).nr_blkz = FDEV(last_dev).nr_blkz + 2347 div_u64(blks, sbi->blocks_per_blkz); 2348 #endif 2349 } 2350 } 2351 2352 int f2fs_resize_fs(struct file *filp, __u64 block_count) 2353 { 2354 struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp)); 2355 __u64 old_block_count, shrunk_blocks; 2356 struct cp_control cpc = { CP_RESIZE, 0, 0, 0 }; 2357 struct f2fs_lock_context lc; 2358 struct f2fs_lock_context glc; 2359 struct f2fs_lock_context clc; 2360 unsigned int secs; 2361 int err = 0; 2362 __u32 rem; 2363 2364 old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count); 2365 if (block_count > old_block_count) 2366 return -EINVAL; 2367 2368 if (f2fs_is_multi_device(sbi)) { 2369 int last_dev = sbi->s_ndevs - 1; 2370 __u64 last_segs = FDEV(last_dev).total_segments; 2371 2372 if (block_count + SEGS_TO_BLKS(sbi, last_segs) <= 2373 old_block_count) 2374 return -EINVAL; 2375 } 2376 2377 /* new fs size should align to section size */ 2378 div_u64_rem(block_count, BLKS_PER_SEC(sbi), &rem); 2379 if (rem) 2380 return -EINVAL; 2381 2382 if (block_count == old_block_count) 2383 return 0; 2384 2385 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { 2386 f2fs_err(sbi, "Should run fsck to repair first."); 2387 return -EFSCORRUPTED; 2388 } 2389 2390 if (test_opt(sbi, DISABLE_CHECKPOINT)) { 2391 f2fs_err(sbi, "Checkpoint should be enabled."); 2392 return -EINVAL; 2393 } 2394 2395 err = mnt_want_write_file(filp); 2396 if (err) 2397 return err; 2398 2399 shrunk_blocks = old_block_count - block_count; 2400 secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi)); 2401 2402 /* stop other GC */ 2403 if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &glc)) { 2404 err = -EAGAIN; 2405 goto out_drop_write; 2406 } 2407 2408 /* stop CP to protect MAIN_SEC in free_segment_range */ 2409 f2fs_lock_op(sbi, &lc); 2410 2411 spin_lock(&sbi->stat_lock); 2412 if (shrunk_blocks + valid_user_blocks(sbi) + 2413 sbi->current_reserved_blocks + sbi->unusable_block_count + 2414 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count) 2415 err = -ENOSPC; 2416 spin_unlock(&sbi->stat_lock); 2417 2418 if (err) 2419 goto out_unlock; 2420 2421 err = free_segment_range(sbi, secs, true); 2422 2423 out_unlock: 2424 f2fs_unlock_op(sbi, &lc); 2425 f2fs_up_write_trace(&sbi->gc_lock, &glc); 2426 out_drop_write: 2427 mnt_drop_write_file(filp); 2428 if (err) 2429 return err; 2430 2431 err = freeze_super(sbi->sb, FREEZE_HOLDER_KERNEL, NULL); 2432 if (err) 2433 return err; 2434 2435 if (f2fs_readonly(sbi->sb)) { 2436 err = thaw_super(sbi->sb, FREEZE_HOLDER_KERNEL, NULL); 2437 if (err) 2438 return err; 2439 return -EROFS; 2440 } 2441 2442 f2fs_down_write_trace(&sbi->gc_lock, &glc); 2443 f2fs_down_write_trace(&sbi->cp_global_sem, &clc); 2444 2445 spin_lock(&sbi->stat_lock); 2446 if (shrunk_blocks + valid_user_blocks(sbi) + 2447 sbi->current_reserved_blocks + sbi->unusable_block_count + 2448 F2FS_OPTION(sbi).root_reserved_blocks > sbi->user_block_count) 2449 err = -ENOSPC; 2450 else 2451 sbi->user_block_count -= shrunk_blocks; 2452 spin_unlock(&sbi->stat_lock); 2453 if (err) 2454 goto out_err; 2455 2456 set_sbi_flag(sbi, SBI_IS_RESIZEFS); 2457 err = free_segment_range(sbi, secs, false); 2458 if (err) 2459 goto recover_user_blocks; 2460 2461 update_sb_metadata(sbi, -secs); 2462 2463 err = f2fs_commit_super(sbi, false); 2464 if (err) { 2465 update_sb_metadata(sbi, secs); 2466 goto recover_out; 2467 } 2468 2469 update_fs_metadata(sbi, -secs); 2470 clear_sbi_flag(sbi, SBI_IS_RESIZEFS); 2471 set_sbi_flag(sbi, SBI_IS_DIRTY); 2472 2473 stat_inc_cp_call_count(sbi, TOTAL_CALL); 2474 err = f2fs_write_checkpoint(sbi, &cpc); 2475 if (err) { 2476 update_fs_metadata(sbi, secs); 2477 update_sb_metadata(sbi, secs); 2478 f2fs_commit_super(sbi, false); 2479 } 2480 recover_out: 2481 if (err) { 2482 f2fs_bug_on(sbi, err == -EAGAIN); 2483 set_sbi_flag(sbi, SBI_NEED_FSCK); 2484 f2fs_err(sbi, "resize_fs failed, should run fsck to repair!"); 2485 } 2486 recover_user_blocks: 2487 clear_sbi_flag(sbi, SBI_IS_RESIZEFS); 2488 if (err) { 2489 spin_lock(&sbi->stat_lock); 2490 sbi->user_block_count += shrunk_blocks; 2491 spin_unlock(&sbi->stat_lock); 2492 } 2493 out_err: 2494 f2fs_up_write_trace(&sbi->cp_global_sem, &clc); 2495 f2fs_up_write_trace(&sbi->gc_lock, &glc); 2496 thaw_super(sbi->sb, FREEZE_HOLDER_KERNEL, NULL); 2497 return err; 2498 } 2499