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