1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * fs/f2fs/segment.c 4 * 5 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com/ 7 */ 8 #include <linux/fs.h> 9 #include <linux/f2fs_fs.h> 10 #include <linux/bio.h> 11 #include <linux/blkdev.h> 12 #include <linux/sched/mm.h> 13 #include <linux/prefetch.h> 14 #include <linux/kthread.h> 15 #include <linux/swap.h> 16 #include <linux/timer.h> 17 #include <linux/freezer.h> 18 #include <linux/sched/signal.h> 19 #include <linux/random.h> 20 21 #include "f2fs.h" 22 #include "segment.h" 23 #include "node.h" 24 #include "gc.h" 25 #include "iostat.h" 26 #include <trace/events/f2fs.h> 27 28 #define __reverse_ffz(x) __reverse_ffs(~(x)) 29 30 static struct kmem_cache *discard_entry_slab; 31 static struct kmem_cache *discard_cmd_slab; 32 static struct kmem_cache *sit_entry_set_slab; 33 static struct kmem_cache *revoke_entry_slab; 34 35 static unsigned long __reverse_ulong(unsigned char *str) 36 { 37 unsigned long tmp = 0; 38 int shift = 24, idx = 0; 39 40 #if BITS_PER_LONG == 64 41 shift = 56; 42 #endif 43 while (shift >= 0) { 44 tmp |= (unsigned long)str[idx++] << shift; 45 shift -= BITS_PER_BYTE; 46 } 47 return tmp; 48 } 49 50 /* 51 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since 52 * MSB and LSB are reversed in a byte by f2fs_set_bit. 53 */ 54 static inline unsigned long __reverse_ffs(unsigned long word) 55 { 56 int num = 0; 57 58 #if BITS_PER_LONG == 64 59 if ((word & 0xffffffff00000000UL) == 0) 60 num += 32; 61 else 62 word >>= 32; 63 #endif 64 if ((word & 0xffff0000) == 0) 65 num += 16; 66 else 67 word >>= 16; 68 69 if ((word & 0xff00) == 0) 70 num += 8; 71 else 72 word >>= 8; 73 74 if ((word & 0xf0) == 0) 75 num += 4; 76 else 77 word >>= 4; 78 79 if ((word & 0xc) == 0) 80 num += 2; 81 else 82 word >>= 2; 83 84 if ((word & 0x2) == 0) 85 num += 1; 86 return num; 87 } 88 89 /* 90 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because 91 * f2fs_set_bit makes MSB and LSB reversed in a byte. 92 * @size must be integral times of unsigned long. 93 * Example: 94 * MSB <--> LSB 95 * f2fs_set_bit(0, bitmap) => 1000 0000 96 * f2fs_set_bit(7, bitmap) => 0000 0001 97 */ 98 static unsigned long __find_rev_next_bit(const unsigned long *addr, 99 unsigned long size, unsigned long offset) 100 { 101 const unsigned long *p = addr + BIT_WORD(offset); 102 unsigned long result = size; 103 unsigned long tmp; 104 105 if (offset >= size) 106 return size; 107 108 size -= (offset & ~(BITS_PER_LONG - 1)); 109 offset %= BITS_PER_LONG; 110 111 while (1) { 112 if (*p == 0) 113 goto pass; 114 115 tmp = __reverse_ulong((unsigned char *)p); 116 117 tmp &= ~0UL >> offset; 118 if (size < BITS_PER_LONG) 119 tmp &= (~0UL << (BITS_PER_LONG - size)); 120 if (tmp) 121 goto found; 122 pass: 123 if (size <= BITS_PER_LONG) 124 break; 125 size -= BITS_PER_LONG; 126 offset = 0; 127 p++; 128 } 129 return result; 130 found: 131 return result - size + __reverse_ffs(tmp); 132 } 133 134 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, 135 unsigned long size, unsigned long offset) 136 { 137 const unsigned long *p = addr + BIT_WORD(offset); 138 unsigned long result = size; 139 unsigned long tmp; 140 141 if (offset >= size) 142 return size; 143 144 size -= (offset & ~(BITS_PER_LONG - 1)); 145 offset %= BITS_PER_LONG; 146 147 while (1) { 148 if (*p == ~0UL) 149 goto pass; 150 151 tmp = __reverse_ulong((unsigned char *)p); 152 153 if (offset) 154 tmp |= ~0UL << (BITS_PER_LONG - offset); 155 if (size < BITS_PER_LONG) 156 tmp |= ~0UL >> size; 157 if (tmp != ~0UL) 158 goto found; 159 pass: 160 if (size <= BITS_PER_LONG) 161 break; 162 size -= BITS_PER_LONG; 163 offset = 0; 164 p++; 165 } 166 return result; 167 found: 168 return result - size + __reverse_ffz(tmp); 169 } 170 171 bool f2fs_need_SSR(struct f2fs_sb_info *sbi) 172 { 173 int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES); 174 int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS); 175 int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA); 176 177 if (f2fs_lfs_mode(sbi)) 178 return false; 179 if (sbi->gc_mode == GC_URGENT_HIGH) 180 return true; 181 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 182 return true; 183 184 return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs + 185 SM_I(sbi)->min_ssr_sections + reserved_sections(sbi)); 186 } 187 188 void f2fs_abort_atomic_write(struct inode *inode, bool clean) 189 { 190 struct f2fs_inode_info *fi = F2FS_I(inode); 191 192 if (!f2fs_is_atomic_file(inode)) 193 return; 194 195 if (clean) 196 truncate_inode_pages_final(inode->i_mapping); 197 198 release_atomic_write_cnt(inode); 199 clear_inode_flag(inode, FI_ATOMIC_COMMITTED); 200 clear_inode_flag(inode, FI_ATOMIC_REPLACE); 201 clear_inode_flag(inode, FI_ATOMIC_FILE); 202 if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) { 203 clear_inode_flag(inode, FI_ATOMIC_DIRTIED); 204 /* 205 * The vfs inode keeps clean during commit, but the f2fs inode 206 * doesn't. So clear the dirty state after commit and let 207 * f2fs_mark_inode_dirty_sync ensure a consistent dirty state. 208 */ 209 f2fs_inode_synced(inode); 210 f2fs_mark_inode_dirty_sync(inode, true); 211 } 212 stat_dec_atomic_inode(inode); 213 214 F2FS_I(inode)->atomic_write_task = NULL; 215 216 if (clean) { 217 f2fs_i_size_write(inode, fi->original_i_size); 218 fi->original_i_size = 0; 219 } 220 /* avoid stale dirty inode during eviction */ 221 sync_inode_metadata(inode, 0); 222 } 223 224 static int __replace_atomic_write_block(struct inode *inode, pgoff_t index, 225 block_t new_addr, block_t *old_addr, bool recover) 226 { 227 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 228 struct dnode_of_data dn; 229 struct node_info ni; 230 int err; 231 232 retry: 233 set_new_dnode(&dn, inode, NULL, NULL, 0); 234 err = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE); 235 if (err) { 236 if (err == -ENOMEM) { 237 memalloc_retry_wait(GFP_NOFS); 238 goto retry; 239 } 240 return err; 241 } 242 243 err = f2fs_get_node_info(sbi, dn.nid, &ni, false); 244 if (err) { 245 f2fs_put_dnode(&dn); 246 return err; 247 } 248 249 if (recover) { 250 /* dn.data_blkaddr is always valid */ 251 if (!__is_valid_data_blkaddr(new_addr)) { 252 if (new_addr == NULL_ADDR) 253 dec_valid_block_count(sbi, inode, 1); 254 f2fs_invalidate_blocks(sbi, dn.data_blkaddr, 1); 255 f2fs_update_data_blkaddr(&dn, new_addr); 256 } else { 257 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, 258 new_addr, ni.version, true, true); 259 } 260 } else { 261 blkcnt_t count = 1; 262 263 err = inc_valid_block_count(sbi, inode, &count, true); 264 if (err) { 265 f2fs_put_dnode(&dn); 266 return err; 267 } 268 269 *old_addr = dn.data_blkaddr; 270 f2fs_truncate_data_blocks_range(&dn, 1); 271 dec_valid_block_count(sbi, F2FS_I(inode)->cow_inode, count); 272 273 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr, 274 ni.version, true, false); 275 } 276 277 f2fs_put_dnode(&dn); 278 279 trace_f2fs_replace_atomic_write_block(inode, F2FS_I(inode)->cow_inode, 280 index, old_addr ? *old_addr : 0, new_addr, recover); 281 return 0; 282 } 283 284 static void __complete_revoke_list(struct inode *inode, struct list_head *head, 285 bool revoke) 286 { 287 struct revoke_entry *cur, *tmp; 288 pgoff_t start_index = 0; 289 bool truncate = is_inode_flag_set(inode, FI_ATOMIC_REPLACE); 290 291 list_for_each_entry_safe(cur, tmp, head, list) { 292 if (revoke) { 293 __replace_atomic_write_block(inode, cur->index, 294 cur->old_addr, NULL, true); 295 } else if (truncate) { 296 f2fs_truncate_hole(inode, start_index, cur->index); 297 start_index = cur->index + 1; 298 } 299 300 list_del(&cur->list); 301 kmem_cache_free(revoke_entry_slab, cur); 302 } 303 304 if (!revoke && truncate) 305 f2fs_do_truncate_blocks(inode, start_index * PAGE_SIZE, false); 306 } 307 308 static int __f2fs_commit_atomic_write(struct inode *inode) 309 { 310 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 311 struct f2fs_inode_info *fi = F2FS_I(inode); 312 struct inode *cow_inode = fi->cow_inode; 313 struct revoke_entry *new; 314 struct list_head revoke_list; 315 block_t blkaddr; 316 struct dnode_of_data dn; 317 pgoff_t len = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 318 pgoff_t off = 0, blen, index; 319 int ret = 0, i; 320 321 INIT_LIST_HEAD(&revoke_list); 322 323 while (len) { 324 blen = min_t(pgoff_t, ADDRS_PER_BLOCK(cow_inode), len); 325 326 set_new_dnode(&dn, cow_inode, NULL, NULL, 0); 327 ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA); 328 if (ret && ret != -ENOENT) { 329 goto out; 330 } else if (ret == -ENOENT) { 331 ret = 0; 332 if (dn.max_level == 0) 333 goto out; 334 goto next; 335 } 336 337 blen = min((pgoff_t)ADDRS_PER_PAGE(dn.node_folio, cow_inode), 338 len); 339 index = off; 340 for (i = 0; i < blen; i++, dn.ofs_in_node++, index++) { 341 blkaddr = f2fs_data_blkaddr(&dn); 342 343 if (!__is_valid_data_blkaddr(blkaddr)) { 344 continue; 345 } else if (!f2fs_is_valid_blkaddr(sbi, blkaddr, 346 DATA_GENERIC_ENHANCE)) { 347 f2fs_put_dnode(&dn); 348 ret = -EFSCORRUPTED; 349 goto out; 350 } 351 352 new = f2fs_kmem_cache_alloc(revoke_entry_slab, GFP_NOFS, 353 true, NULL); 354 355 ret = __replace_atomic_write_block(inode, index, blkaddr, 356 &new->old_addr, false); 357 if (ret) { 358 f2fs_put_dnode(&dn); 359 kmem_cache_free(revoke_entry_slab, new); 360 goto out; 361 } 362 363 f2fs_update_data_blkaddr(&dn, NULL_ADDR); 364 new->index = index; 365 list_add_tail(&new->list, &revoke_list); 366 } 367 f2fs_put_dnode(&dn); 368 next: 369 off += blen; 370 len -= blen; 371 } 372 373 out: 374 if (time_to_inject(sbi, FAULT_TIMEOUT)) 375 f2fs_io_schedule_timeout_killable(DEFAULT_FAULT_TIMEOUT); 376 377 if (ret) { 378 sbi->revoked_atomic_block += fi->atomic_write_cnt; 379 } else { 380 sbi->committed_atomic_block += fi->atomic_write_cnt; 381 set_inode_flag(inode, FI_ATOMIC_COMMITTED); 382 383 /* 384 * inode may has no FI_ATOMIC_DIRTIED flag due to no write 385 * before commit. 386 */ 387 if (is_inode_flag_set(inode, FI_ATOMIC_DIRTIED)) { 388 /* clear atomic dirty status and set vfs dirty status */ 389 clear_inode_flag(inode, FI_ATOMIC_DIRTIED); 390 f2fs_mark_inode_dirty_sync(inode, true); 391 } 392 } 393 394 __complete_revoke_list(inode, &revoke_list, ret ? true : false); 395 396 return ret; 397 } 398 399 int f2fs_commit_atomic_write(struct inode *inode) 400 { 401 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 402 struct f2fs_inode_info *fi = F2FS_I(inode); 403 int err; 404 405 err = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX); 406 if (err) 407 return err; 408 409 f2fs_down_write(&fi->i_gc_rwsem[WRITE]); 410 f2fs_lock_op(sbi); 411 412 err = __f2fs_commit_atomic_write(inode); 413 414 f2fs_unlock_op(sbi); 415 f2fs_up_write(&fi->i_gc_rwsem[WRITE]); 416 417 return err; 418 } 419 420 /* 421 * This function balances dirty node and dentry pages. 422 * In addition, it controls garbage collection. 423 */ 424 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) 425 { 426 if (f2fs_cp_error(sbi)) 427 return; 428 429 if (time_to_inject(sbi, FAULT_CHECKPOINT)) 430 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_FAULT_INJECT); 431 432 /* balance_fs_bg is able to be pending */ 433 if (need && excess_cached_nats(sbi)) 434 f2fs_balance_fs_bg(sbi, false); 435 436 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 437 return; 438 439 /* 440 * We should do GC or end up with checkpoint, if there are so many dirty 441 * dir/node pages without enough free segments. 442 */ 443 if (has_enough_free_secs(sbi, 0, 0)) 444 return; 445 446 if (test_opt(sbi, GC_MERGE) && sbi->gc_thread && 447 sbi->gc_thread->f2fs_gc_task) { 448 DEFINE_WAIT(wait); 449 450 prepare_to_wait(&sbi->gc_thread->fggc_wq, &wait, 451 TASK_UNINTERRUPTIBLE); 452 wake_up(&sbi->gc_thread->gc_wait_queue_head); 453 io_schedule(); 454 finish_wait(&sbi->gc_thread->fggc_wq, &wait); 455 } else { 456 struct f2fs_gc_control gc_control = { 457 .victim_segno = NULL_SEGNO, 458 .init_gc_type = f2fs_sb_has_blkzoned(sbi) ? 459 FG_GC : BG_GC, 460 .no_bg_gc = true, 461 .should_migrate_blocks = false, 462 .err_gc_skipped = false, 463 .nr_free_secs = 1 }; 464 f2fs_down_write(&sbi->gc_lock); 465 stat_inc_gc_call_count(sbi, FOREGROUND); 466 f2fs_gc(sbi, &gc_control); 467 } 468 } 469 470 static inline bool excess_dirty_threshold(struct f2fs_sb_info *sbi) 471 { 472 int factor = f2fs_rwsem_is_locked(&sbi->cp_rwsem) ? 3 : 2; 473 unsigned int dents = get_pages(sbi, F2FS_DIRTY_DENTS); 474 unsigned int qdata = get_pages(sbi, F2FS_DIRTY_QDATA); 475 unsigned int nodes = get_pages(sbi, F2FS_DIRTY_NODES); 476 unsigned int meta = get_pages(sbi, F2FS_DIRTY_META); 477 unsigned int imeta = get_pages(sbi, F2FS_DIRTY_IMETA); 478 unsigned int threshold = 479 SEGS_TO_BLKS(sbi, (factor * DEFAULT_DIRTY_THRESHOLD)); 480 unsigned int global_threshold = threshold * 3 / 2; 481 482 if (dents >= threshold || qdata >= threshold || 483 nodes >= threshold || meta >= threshold || 484 imeta >= threshold) 485 return true; 486 return dents + qdata + nodes + meta + imeta > global_threshold; 487 } 488 489 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg) 490 { 491 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) 492 return; 493 494 /* try to shrink extent cache when there is no enough memory */ 495 if (!f2fs_available_free_memory(sbi, READ_EXTENT_CACHE)) 496 f2fs_shrink_read_extent_tree(sbi, 497 READ_EXTENT_CACHE_SHRINK_NUMBER); 498 499 /* try to shrink age extent cache when there is no enough memory */ 500 if (!f2fs_available_free_memory(sbi, AGE_EXTENT_CACHE)) 501 f2fs_shrink_age_extent_tree(sbi, 502 AGE_EXTENT_CACHE_SHRINK_NUMBER); 503 504 /* check the # of cached NAT entries */ 505 if (!f2fs_available_free_memory(sbi, NAT_ENTRIES)) 506 f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); 507 508 if (!f2fs_available_free_memory(sbi, FREE_NIDS)) 509 f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS); 510 else 511 f2fs_build_free_nids(sbi, false, false); 512 513 if (excess_dirty_nats(sbi) || excess_dirty_threshold(sbi) || 514 excess_prefree_segs(sbi) || !f2fs_space_for_roll_forward(sbi)) 515 goto do_sync; 516 517 /* there is background inflight IO or foreground operation recently */ 518 if (is_inflight_io(sbi, REQ_TIME) || 519 (!f2fs_time_over(sbi, REQ_TIME) && f2fs_rwsem_is_locked(&sbi->cp_rwsem))) 520 return; 521 522 /* exceed periodical checkpoint timeout threshold */ 523 if (f2fs_time_over(sbi, CP_TIME)) 524 goto do_sync; 525 526 /* checkpoint is the only way to shrink partial cached entries */ 527 if (f2fs_available_free_memory(sbi, NAT_ENTRIES) && 528 f2fs_available_free_memory(sbi, INO_ENTRIES)) 529 return; 530 531 do_sync: 532 if (test_opt(sbi, DATA_FLUSH) && from_bg) { 533 struct blk_plug plug; 534 535 mutex_lock(&sbi->flush_lock); 536 537 blk_start_plug(&plug); 538 f2fs_sync_dirty_inodes(sbi, FILE_INODE, false); 539 blk_finish_plug(&plug); 540 541 mutex_unlock(&sbi->flush_lock); 542 } 543 stat_inc_cp_call_count(sbi, BACKGROUND); 544 f2fs_sync_fs(sbi->sb, 1); 545 } 546 547 static int __submit_flush_wait(struct f2fs_sb_info *sbi, 548 struct block_device *bdev) 549 { 550 int ret = blkdev_issue_flush(bdev); 551 552 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), 553 test_opt(sbi, FLUSH_MERGE), ret); 554 if (!ret) 555 f2fs_update_iostat(sbi, NULL, FS_FLUSH_IO, 0); 556 return ret; 557 } 558 559 static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino) 560 { 561 int ret = 0; 562 int i; 563 564 if (!f2fs_is_multi_device(sbi)) 565 return __submit_flush_wait(sbi, sbi->sb->s_bdev); 566 567 for (i = 0; i < sbi->s_ndevs; i++) { 568 if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO)) 569 continue; 570 ret = __submit_flush_wait(sbi, FDEV(i).bdev); 571 if (ret) 572 break; 573 } 574 return ret; 575 } 576 577 static int issue_flush_thread(void *data) 578 { 579 struct f2fs_sb_info *sbi = data; 580 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 581 wait_queue_head_t *q = &fcc->flush_wait_queue; 582 repeat: 583 if (kthread_should_stop()) 584 return 0; 585 586 if (!llist_empty(&fcc->issue_list)) { 587 struct flush_cmd *cmd, *next; 588 int ret; 589 590 fcc->dispatch_list = llist_del_all(&fcc->issue_list); 591 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); 592 593 cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode); 594 595 ret = submit_flush_wait(sbi, cmd->ino); 596 atomic_inc(&fcc->issued_flush); 597 598 llist_for_each_entry_safe(cmd, next, 599 fcc->dispatch_list, llnode) { 600 cmd->ret = ret; 601 complete(&cmd->wait); 602 } 603 fcc->dispatch_list = NULL; 604 } 605 606 wait_event_interruptible(*q, 607 kthread_should_stop() || !llist_empty(&fcc->issue_list)); 608 goto repeat; 609 } 610 611 int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino) 612 { 613 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 614 struct flush_cmd cmd; 615 int ret; 616 617 if (test_opt(sbi, NOBARRIER)) 618 return 0; 619 620 if (!test_opt(sbi, FLUSH_MERGE)) { 621 atomic_inc(&fcc->queued_flush); 622 ret = submit_flush_wait(sbi, ino); 623 atomic_dec(&fcc->queued_flush); 624 atomic_inc(&fcc->issued_flush); 625 return ret; 626 } 627 628 if (atomic_inc_return(&fcc->queued_flush) == 1 || 629 f2fs_is_multi_device(sbi)) { 630 ret = submit_flush_wait(sbi, ino); 631 atomic_dec(&fcc->queued_flush); 632 633 atomic_inc(&fcc->issued_flush); 634 return ret; 635 } 636 637 cmd.ino = ino; 638 init_completion(&cmd.wait); 639 640 llist_add(&cmd.llnode, &fcc->issue_list); 641 642 /* 643 * update issue_list before we wake up issue_flush thread, this 644 * smp_mb() pairs with another barrier in ___wait_event(), see 645 * more details in comments of waitqueue_active(). 646 */ 647 smp_mb(); 648 649 if (waitqueue_active(&fcc->flush_wait_queue)) 650 wake_up(&fcc->flush_wait_queue); 651 652 if (fcc->f2fs_issue_flush) { 653 wait_for_completion(&cmd.wait); 654 atomic_dec(&fcc->queued_flush); 655 } else { 656 struct llist_node *list; 657 658 list = llist_del_all(&fcc->issue_list); 659 if (!list) { 660 wait_for_completion(&cmd.wait); 661 atomic_dec(&fcc->queued_flush); 662 } else { 663 struct flush_cmd *tmp, *next; 664 665 ret = submit_flush_wait(sbi, ino); 666 667 llist_for_each_entry_safe(tmp, next, list, llnode) { 668 if (tmp == &cmd) { 669 cmd.ret = ret; 670 atomic_dec(&fcc->queued_flush); 671 continue; 672 } 673 tmp->ret = ret; 674 complete(&tmp->wait); 675 } 676 } 677 } 678 679 return cmd.ret; 680 } 681 682 int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi) 683 { 684 dev_t dev = sbi->sb->s_bdev->bd_dev; 685 struct flush_cmd_control *fcc; 686 687 if (SM_I(sbi)->fcc_info) { 688 fcc = SM_I(sbi)->fcc_info; 689 if (fcc->f2fs_issue_flush) 690 return 0; 691 goto init_thread; 692 } 693 694 fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL); 695 if (!fcc) 696 return -ENOMEM; 697 atomic_set(&fcc->issued_flush, 0); 698 atomic_set(&fcc->queued_flush, 0); 699 init_waitqueue_head(&fcc->flush_wait_queue); 700 init_llist_head(&fcc->issue_list); 701 SM_I(sbi)->fcc_info = fcc; 702 if (!test_opt(sbi, FLUSH_MERGE)) 703 return 0; 704 705 init_thread: 706 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, 707 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); 708 if (IS_ERR(fcc->f2fs_issue_flush)) { 709 int err = PTR_ERR(fcc->f2fs_issue_flush); 710 711 fcc->f2fs_issue_flush = NULL; 712 return err; 713 } 714 715 return 0; 716 } 717 718 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free) 719 { 720 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 721 722 if (fcc && fcc->f2fs_issue_flush) { 723 struct task_struct *flush_thread = fcc->f2fs_issue_flush; 724 725 fcc->f2fs_issue_flush = NULL; 726 kthread_stop(flush_thread); 727 } 728 if (free) { 729 kfree(fcc); 730 SM_I(sbi)->fcc_info = NULL; 731 } 732 } 733 734 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi) 735 { 736 int ret = 0, i; 737 738 if (!f2fs_is_multi_device(sbi)) 739 return 0; 740 741 if (test_opt(sbi, NOBARRIER)) 742 return 0; 743 744 for (i = 1; i < sbi->s_ndevs; i++) { 745 int count = DEFAULT_RETRY_IO_COUNT; 746 747 if (!f2fs_test_bit(i, (char *)&sbi->dirty_device)) 748 continue; 749 750 do { 751 ret = __submit_flush_wait(sbi, FDEV(i).bdev); 752 if (ret) 753 f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT); 754 } while (ret && --count); 755 756 if (ret) { 757 f2fs_stop_checkpoint(sbi, false, 758 STOP_CP_REASON_FLUSH_FAIL); 759 break; 760 } 761 762 spin_lock(&sbi->dev_lock); 763 f2fs_clear_bit(i, (char *)&sbi->dirty_device); 764 spin_unlock(&sbi->dev_lock); 765 } 766 767 return ret; 768 } 769 770 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 771 enum dirty_type dirty_type) 772 { 773 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 774 775 /* need not be added */ 776 if (is_curseg(sbi, segno)) 777 return; 778 779 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) 780 dirty_i->nr_dirty[dirty_type]++; 781 782 if (dirty_type == DIRTY) { 783 struct seg_entry *sentry = get_seg_entry(sbi, segno); 784 enum dirty_type t = sentry->type; 785 786 if (unlikely(t >= DIRTY)) { 787 f2fs_bug_on(sbi, 1); 788 return; 789 } 790 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) 791 dirty_i->nr_dirty[t]++; 792 793 if (__is_large_section(sbi)) { 794 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); 795 block_t valid_blocks = 796 get_valid_blocks(sbi, segno, true); 797 798 f2fs_bug_on(sbi, 799 (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) && 800 !valid_blocks) || 801 valid_blocks == CAP_BLKS_PER_SEC(sbi)); 802 803 if (!is_cursec(sbi, secno)) 804 set_bit(secno, dirty_i->dirty_secmap); 805 } 806 } 807 } 808 809 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 810 enum dirty_type dirty_type) 811 { 812 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 813 block_t valid_blocks; 814 815 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) 816 dirty_i->nr_dirty[dirty_type]--; 817 818 if (dirty_type == DIRTY) { 819 struct seg_entry *sentry = get_seg_entry(sbi, segno); 820 enum dirty_type t = sentry->type; 821 822 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) 823 dirty_i->nr_dirty[t]--; 824 825 valid_blocks = get_valid_blocks(sbi, segno, true); 826 if (valid_blocks == 0) { 827 clear_bit(GET_SEC_FROM_SEG(sbi, segno), 828 dirty_i->victim_secmap); 829 #ifdef CONFIG_F2FS_CHECK_FS 830 clear_bit(segno, SIT_I(sbi)->invalid_segmap); 831 #endif 832 } 833 if (__is_large_section(sbi)) { 834 unsigned int secno = GET_SEC_FROM_SEG(sbi, segno); 835 836 if (!valid_blocks || 837 valid_blocks == CAP_BLKS_PER_SEC(sbi)) { 838 clear_bit(secno, dirty_i->dirty_secmap); 839 return; 840 } 841 842 if (!is_cursec(sbi, secno)) 843 set_bit(secno, dirty_i->dirty_secmap); 844 } 845 } 846 } 847 848 /* 849 * Should not occur error such as -ENOMEM. 850 * Adding dirty entry into seglist is not critical operation. 851 * If a given segment is one of current working segments, it won't be added. 852 */ 853 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) 854 { 855 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 856 unsigned short valid_blocks, ckpt_valid_blocks; 857 unsigned int usable_blocks; 858 859 if (segno == NULL_SEGNO || is_curseg(sbi, segno)) 860 return; 861 862 usable_blocks = f2fs_usable_blks_in_seg(sbi, segno); 863 mutex_lock(&dirty_i->seglist_lock); 864 865 valid_blocks = get_valid_blocks(sbi, segno, false); 866 ckpt_valid_blocks = get_ckpt_valid_blocks(sbi, segno, false); 867 868 if (valid_blocks == 0 && (!is_sbi_flag_set(sbi, SBI_CP_DISABLED) || 869 ckpt_valid_blocks == usable_blocks)) { 870 __locate_dirty_segment(sbi, segno, PRE); 871 __remove_dirty_segment(sbi, segno, DIRTY); 872 } else if (valid_blocks < usable_blocks) { 873 __locate_dirty_segment(sbi, segno, DIRTY); 874 } else { 875 /* Recovery routine with SSR needs this */ 876 __remove_dirty_segment(sbi, segno, DIRTY); 877 } 878 879 mutex_unlock(&dirty_i->seglist_lock); 880 } 881 882 /* This moves currently empty dirty blocks to prefree. Must hold seglist_lock */ 883 void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi) 884 { 885 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 886 unsigned int segno; 887 888 mutex_lock(&dirty_i->seglist_lock); 889 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { 890 if (get_valid_blocks(sbi, segno, false)) 891 continue; 892 if (is_curseg(sbi, segno)) 893 continue; 894 __locate_dirty_segment(sbi, segno, PRE); 895 __remove_dirty_segment(sbi, segno, DIRTY); 896 } 897 mutex_unlock(&dirty_i->seglist_lock); 898 } 899 900 block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi) 901 { 902 int ovp_hole_segs = 903 (overprovision_segments(sbi) - reserved_segments(sbi)); 904 block_t ovp_holes = SEGS_TO_BLKS(sbi, ovp_hole_segs); 905 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 906 block_t holes[2] = {0, 0}; /* DATA and NODE */ 907 block_t unusable; 908 struct seg_entry *se; 909 unsigned int segno; 910 911 mutex_lock(&dirty_i->seglist_lock); 912 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { 913 se = get_seg_entry(sbi, segno); 914 if (IS_NODESEG(se->type)) 915 holes[NODE] += f2fs_usable_blks_in_seg(sbi, segno) - 916 se->valid_blocks; 917 else 918 holes[DATA] += f2fs_usable_blks_in_seg(sbi, segno) - 919 se->valid_blocks; 920 } 921 mutex_unlock(&dirty_i->seglist_lock); 922 923 unusable = max(holes[DATA], holes[NODE]); 924 if (unusable > ovp_holes) 925 return unusable - ovp_holes; 926 return 0; 927 } 928 929 int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable) 930 { 931 int ovp_hole_segs = 932 (overprovision_segments(sbi) - reserved_segments(sbi)); 933 934 if (F2FS_OPTION(sbi).unusable_cap_perc == 100) 935 return 0; 936 if (unusable > F2FS_OPTION(sbi).unusable_cap) 937 return -EAGAIN; 938 if (is_sbi_flag_set(sbi, SBI_CP_DISABLED_QUICK) && 939 dirty_segments(sbi) > ovp_hole_segs) 940 return -EAGAIN; 941 if (has_not_enough_free_secs(sbi, 0, 0)) 942 return -EAGAIN; 943 return 0; 944 } 945 946 /* This is only used by SBI_CP_DISABLED */ 947 static unsigned int get_free_segment(struct f2fs_sb_info *sbi) 948 { 949 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 950 unsigned int segno = 0; 951 952 mutex_lock(&dirty_i->seglist_lock); 953 for_each_set_bit(segno, dirty_i->dirty_segmap[DIRTY], MAIN_SEGS(sbi)) { 954 if (get_valid_blocks(sbi, segno, false)) 955 continue; 956 if (get_ckpt_valid_blocks(sbi, segno, false)) 957 continue; 958 mutex_unlock(&dirty_i->seglist_lock); 959 return segno; 960 } 961 mutex_unlock(&dirty_i->seglist_lock); 962 return NULL_SEGNO; 963 } 964 965 static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi, 966 struct block_device *bdev, block_t lstart, 967 block_t start, block_t len) 968 { 969 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 970 struct list_head *pend_list; 971 struct discard_cmd *dc; 972 973 f2fs_bug_on(sbi, !len); 974 975 pend_list = &dcc->pend_list[plist_idx(len)]; 976 977 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS, true, NULL); 978 INIT_LIST_HEAD(&dc->list); 979 dc->bdev = bdev; 980 dc->di.lstart = lstart; 981 dc->di.start = start; 982 dc->di.len = len; 983 dc->ref = 0; 984 dc->state = D_PREP; 985 dc->queued = 0; 986 dc->error = 0; 987 init_completion(&dc->wait); 988 list_add_tail(&dc->list, pend_list); 989 spin_lock_init(&dc->lock); 990 dc->bio_ref = 0; 991 atomic_inc(&dcc->discard_cmd_cnt); 992 dcc->undiscard_blks += len; 993 994 return dc; 995 } 996 997 static bool f2fs_check_discard_tree(struct f2fs_sb_info *sbi) 998 { 999 #ifdef CONFIG_F2FS_CHECK_FS 1000 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1001 struct rb_node *cur = rb_first_cached(&dcc->root), *next; 1002 struct discard_cmd *cur_dc, *next_dc; 1003 1004 while (cur) { 1005 next = rb_next(cur); 1006 if (!next) 1007 return true; 1008 1009 cur_dc = rb_entry(cur, struct discard_cmd, rb_node); 1010 next_dc = rb_entry(next, struct discard_cmd, rb_node); 1011 1012 if (cur_dc->di.lstart + cur_dc->di.len > next_dc->di.lstart) { 1013 f2fs_info(sbi, "broken discard_rbtree, " 1014 "cur(%u, %u) next(%u, %u)", 1015 cur_dc->di.lstart, cur_dc->di.len, 1016 next_dc->di.lstart, next_dc->di.len); 1017 return false; 1018 } 1019 cur = next; 1020 } 1021 #endif 1022 return true; 1023 } 1024 1025 static struct discard_cmd *__lookup_discard_cmd(struct f2fs_sb_info *sbi, 1026 block_t blkaddr) 1027 { 1028 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1029 struct rb_node *node = dcc->root.rb_root.rb_node; 1030 struct discard_cmd *dc; 1031 1032 while (node) { 1033 dc = rb_entry(node, struct discard_cmd, rb_node); 1034 1035 if (blkaddr < dc->di.lstart) 1036 node = node->rb_left; 1037 else if (blkaddr >= dc->di.lstart + dc->di.len) 1038 node = node->rb_right; 1039 else 1040 return dc; 1041 } 1042 return NULL; 1043 } 1044 1045 static struct discard_cmd *__lookup_discard_cmd_ret(struct rb_root_cached *root, 1046 block_t blkaddr, 1047 struct discard_cmd **prev_entry, 1048 struct discard_cmd **next_entry, 1049 struct rb_node ***insert_p, 1050 struct rb_node **insert_parent) 1051 { 1052 struct rb_node **pnode = &root->rb_root.rb_node; 1053 struct rb_node *parent = NULL, *tmp_node; 1054 struct discard_cmd *dc; 1055 1056 *insert_p = NULL; 1057 *insert_parent = NULL; 1058 *prev_entry = NULL; 1059 *next_entry = NULL; 1060 1061 if (RB_EMPTY_ROOT(&root->rb_root)) 1062 return NULL; 1063 1064 while (*pnode) { 1065 parent = *pnode; 1066 dc = rb_entry(*pnode, struct discard_cmd, rb_node); 1067 1068 if (blkaddr < dc->di.lstart) 1069 pnode = &(*pnode)->rb_left; 1070 else if (blkaddr >= dc->di.lstart + dc->di.len) 1071 pnode = &(*pnode)->rb_right; 1072 else 1073 goto lookup_neighbors; 1074 } 1075 1076 *insert_p = pnode; 1077 *insert_parent = parent; 1078 1079 dc = rb_entry(parent, struct discard_cmd, rb_node); 1080 tmp_node = parent; 1081 if (parent && blkaddr > dc->di.lstart) 1082 tmp_node = rb_next(parent); 1083 *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node); 1084 1085 tmp_node = parent; 1086 if (parent && blkaddr < dc->di.lstart) 1087 tmp_node = rb_prev(parent); 1088 *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node); 1089 return NULL; 1090 1091 lookup_neighbors: 1092 /* lookup prev node for merging backward later */ 1093 tmp_node = rb_prev(&dc->rb_node); 1094 *prev_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node); 1095 1096 /* lookup next node for merging frontward later */ 1097 tmp_node = rb_next(&dc->rb_node); 1098 *next_entry = rb_entry_safe(tmp_node, struct discard_cmd, rb_node); 1099 return dc; 1100 } 1101 1102 static void __detach_discard_cmd(struct discard_cmd_control *dcc, 1103 struct discard_cmd *dc) 1104 { 1105 if (dc->state == D_DONE) 1106 atomic_sub(dc->queued, &dcc->queued_discard); 1107 1108 list_del(&dc->list); 1109 rb_erase_cached(&dc->rb_node, &dcc->root); 1110 dcc->undiscard_blks -= dc->di.len; 1111 1112 kmem_cache_free(discard_cmd_slab, dc); 1113 1114 atomic_dec(&dcc->discard_cmd_cnt); 1115 } 1116 1117 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, 1118 struct discard_cmd *dc) 1119 { 1120 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1121 unsigned long flags; 1122 1123 trace_f2fs_remove_discard(dc->bdev, dc->di.start, dc->di.len); 1124 1125 spin_lock_irqsave(&dc->lock, flags); 1126 if (dc->bio_ref) { 1127 spin_unlock_irqrestore(&dc->lock, flags); 1128 return; 1129 } 1130 spin_unlock_irqrestore(&dc->lock, flags); 1131 1132 f2fs_bug_on(sbi, dc->ref); 1133 1134 if (dc->error == -EOPNOTSUPP) 1135 dc->error = 0; 1136 1137 if (dc->error) 1138 f2fs_info_ratelimited(sbi, 1139 "Issue discard(%u, %u, %u) failed, ret: %d", 1140 dc->di.lstart, dc->di.start, dc->di.len, dc->error); 1141 __detach_discard_cmd(dcc, dc); 1142 } 1143 1144 static void f2fs_submit_discard_endio(struct bio *bio) 1145 { 1146 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private; 1147 unsigned long flags; 1148 1149 spin_lock_irqsave(&dc->lock, flags); 1150 if (!dc->error) 1151 dc->error = blk_status_to_errno(bio->bi_status); 1152 dc->bio_ref--; 1153 if (!dc->bio_ref && dc->state == D_SUBMIT) { 1154 dc->state = D_DONE; 1155 complete_all(&dc->wait); 1156 } 1157 spin_unlock_irqrestore(&dc->lock, flags); 1158 bio_put(bio); 1159 } 1160 1161 static void __check_sit_bitmap(struct f2fs_sb_info *sbi, 1162 block_t start, block_t end) 1163 { 1164 #ifdef CONFIG_F2FS_CHECK_FS 1165 struct seg_entry *sentry; 1166 unsigned int segno; 1167 block_t blk = start; 1168 unsigned long offset, size, *map; 1169 1170 while (blk < end) { 1171 segno = GET_SEGNO(sbi, blk); 1172 sentry = get_seg_entry(sbi, segno); 1173 offset = GET_BLKOFF_FROM_SEG0(sbi, blk); 1174 1175 if (end < START_BLOCK(sbi, segno + 1)) 1176 size = GET_BLKOFF_FROM_SEG0(sbi, end); 1177 else 1178 size = BLKS_PER_SEG(sbi); 1179 map = (unsigned long *)(sentry->cur_valid_map); 1180 offset = __find_rev_next_bit(map, size, offset); 1181 f2fs_bug_on(sbi, offset != size); 1182 blk = START_BLOCK(sbi, segno + 1); 1183 } 1184 #endif 1185 } 1186 1187 static void __init_discard_policy(struct f2fs_sb_info *sbi, 1188 struct discard_policy *dpolicy, 1189 int discard_type, unsigned int granularity) 1190 { 1191 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1192 1193 /* common policy */ 1194 dpolicy->type = discard_type; 1195 dpolicy->sync = true; 1196 dpolicy->ordered = false; 1197 dpolicy->granularity = granularity; 1198 1199 dpolicy->max_requests = dcc->max_discard_request; 1200 dpolicy->io_aware_gran = dcc->discard_io_aware_gran; 1201 dpolicy->timeout = false; 1202 1203 if (discard_type == DPOLICY_BG) { 1204 dpolicy->min_interval = dcc->min_discard_issue_time; 1205 dpolicy->mid_interval = dcc->mid_discard_issue_time; 1206 dpolicy->max_interval = dcc->max_discard_issue_time; 1207 if (dcc->discard_io_aware == DPOLICY_IO_AWARE_ENABLE) 1208 dpolicy->io_aware = true; 1209 else if (dcc->discard_io_aware == DPOLICY_IO_AWARE_DISABLE) 1210 dpolicy->io_aware = false; 1211 dpolicy->sync = false; 1212 dpolicy->ordered = true; 1213 if (utilization(sbi) > dcc->discard_urgent_util) { 1214 dpolicy->granularity = MIN_DISCARD_GRANULARITY; 1215 if (atomic_read(&dcc->discard_cmd_cnt)) 1216 dpolicy->max_interval = 1217 dcc->min_discard_issue_time; 1218 } 1219 } else if (discard_type == DPOLICY_FORCE) { 1220 dpolicy->min_interval = dcc->min_discard_issue_time; 1221 dpolicy->mid_interval = dcc->mid_discard_issue_time; 1222 dpolicy->max_interval = dcc->max_discard_issue_time; 1223 dpolicy->io_aware = false; 1224 } else if (discard_type == DPOLICY_FSTRIM) { 1225 dpolicy->io_aware = false; 1226 } else if (discard_type == DPOLICY_UMOUNT) { 1227 dpolicy->io_aware = false; 1228 /* we need to issue all to keep CP_TRIMMED_FLAG */ 1229 dpolicy->granularity = MIN_DISCARD_GRANULARITY; 1230 dpolicy->timeout = true; 1231 } 1232 } 1233 1234 static void __update_discard_tree_range(struct f2fs_sb_info *sbi, 1235 struct block_device *bdev, block_t lstart, 1236 block_t start, block_t len); 1237 1238 #ifdef CONFIG_BLK_DEV_ZONED 1239 static void __submit_zone_reset_cmd(struct f2fs_sb_info *sbi, 1240 struct discard_cmd *dc, blk_opf_t flag, 1241 struct list_head *wait_list, 1242 unsigned int *issued) 1243 { 1244 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1245 struct block_device *bdev = dc->bdev; 1246 struct bio *bio = bio_alloc(bdev, 0, REQ_OP_ZONE_RESET | flag, GFP_NOFS); 1247 unsigned long flags; 1248 1249 trace_f2fs_issue_reset_zone(bdev, dc->di.start); 1250 1251 spin_lock_irqsave(&dc->lock, flags); 1252 dc->state = D_SUBMIT; 1253 dc->bio_ref++; 1254 spin_unlock_irqrestore(&dc->lock, flags); 1255 1256 if (issued) 1257 (*issued)++; 1258 1259 atomic_inc(&dcc->queued_discard); 1260 dc->queued++; 1261 list_move_tail(&dc->list, wait_list); 1262 1263 /* sanity check on discard range */ 1264 __check_sit_bitmap(sbi, dc->di.lstart, dc->di.lstart + dc->di.len); 1265 1266 bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(dc->di.start); 1267 bio->bi_private = dc; 1268 bio->bi_end_io = f2fs_submit_discard_endio; 1269 submit_bio(bio); 1270 1271 atomic_inc(&dcc->issued_discard); 1272 f2fs_update_iostat(sbi, NULL, FS_ZONE_RESET_IO, dc->di.len * F2FS_BLKSIZE); 1273 } 1274 #endif 1275 1276 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */ 1277 static int __submit_discard_cmd(struct f2fs_sb_info *sbi, 1278 struct discard_policy *dpolicy, 1279 struct discard_cmd *dc, int *issued) 1280 { 1281 struct block_device *bdev = dc->bdev; 1282 unsigned int max_discard_blocks = 1283 SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev)); 1284 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1285 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? 1286 &(dcc->fstrim_list) : &(dcc->wait_list); 1287 blk_opf_t flag = dpolicy->sync ? REQ_SYNC : 0; 1288 block_t lstart, start, len, total_len; 1289 int err = 0; 1290 1291 if (dc->state != D_PREP) 1292 return 0; 1293 1294 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) 1295 return 0; 1296 1297 #ifdef CONFIG_BLK_DEV_ZONED 1298 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) { 1299 int devi = f2fs_bdev_index(sbi, bdev); 1300 1301 if (devi < 0) 1302 return -EINVAL; 1303 1304 if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) { 1305 __submit_zone_reset_cmd(sbi, dc, flag, 1306 wait_list, issued); 1307 return 0; 1308 } 1309 } 1310 #endif 1311 1312 /* 1313 * stop issuing discard for any of below cases: 1314 * 1. device is conventional zone, but it doesn't support discard. 1315 * 2. device is regulare device, after snapshot it doesn't support 1316 * discard. 1317 */ 1318 if (!bdev_max_discard_sectors(bdev)) 1319 return -EOPNOTSUPP; 1320 1321 trace_f2fs_issue_discard(bdev, dc->di.start, dc->di.len); 1322 1323 lstart = dc->di.lstart; 1324 start = dc->di.start; 1325 len = dc->di.len; 1326 total_len = len; 1327 1328 dc->di.len = 0; 1329 1330 while (total_len && *issued < dpolicy->max_requests && !err) { 1331 struct bio *bio = NULL; 1332 unsigned long flags; 1333 bool last = true; 1334 1335 if (len > max_discard_blocks) { 1336 len = max_discard_blocks; 1337 last = false; 1338 } 1339 1340 (*issued)++; 1341 if (*issued == dpolicy->max_requests) 1342 last = true; 1343 1344 dc->di.len += len; 1345 1346 err = 0; 1347 if (time_to_inject(sbi, FAULT_DISCARD)) { 1348 err = -EIO; 1349 spin_lock_irqsave(&dc->lock, flags); 1350 if (dc->state == D_PARTIAL) 1351 dc->state = D_SUBMIT; 1352 spin_unlock_irqrestore(&dc->lock, flags); 1353 1354 break; 1355 } 1356 1357 __blkdev_issue_discard(bdev, SECTOR_FROM_BLOCK(start), 1358 SECTOR_FROM_BLOCK(len), GFP_NOFS, &bio); 1359 f2fs_bug_on(sbi, !bio); 1360 1361 /* 1362 * should keep before submission to avoid D_DONE 1363 * right away 1364 */ 1365 spin_lock_irqsave(&dc->lock, flags); 1366 if (last) 1367 dc->state = D_SUBMIT; 1368 else 1369 dc->state = D_PARTIAL; 1370 dc->bio_ref++; 1371 spin_unlock_irqrestore(&dc->lock, flags); 1372 1373 atomic_inc(&dcc->queued_discard); 1374 dc->queued++; 1375 list_move_tail(&dc->list, wait_list); 1376 1377 /* sanity check on discard range */ 1378 __check_sit_bitmap(sbi, lstart, lstart + len); 1379 1380 bio->bi_private = dc; 1381 bio->bi_end_io = f2fs_submit_discard_endio; 1382 bio->bi_opf |= flag; 1383 submit_bio(bio); 1384 1385 atomic_inc(&dcc->issued_discard); 1386 1387 f2fs_update_iostat(sbi, NULL, FS_DISCARD_IO, len * F2FS_BLKSIZE); 1388 1389 lstart += len; 1390 start += len; 1391 total_len -= len; 1392 len = total_len; 1393 } 1394 1395 if (!err && len) { 1396 dcc->undiscard_blks -= len; 1397 __update_discard_tree_range(sbi, bdev, lstart, start, len); 1398 } 1399 return err; 1400 } 1401 1402 static void __insert_discard_cmd(struct f2fs_sb_info *sbi, 1403 struct block_device *bdev, block_t lstart, 1404 block_t start, block_t len) 1405 { 1406 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1407 struct rb_node **p = &dcc->root.rb_root.rb_node; 1408 struct rb_node *parent = NULL; 1409 struct discard_cmd *dc; 1410 bool leftmost = true; 1411 1412 /* look up rb tree to find parent node */ 1413 while (*p) { 1414 parent = *p; 1415 dc = rb_entry(parent, struct discard_cmd, rb_node); 1416 1417 if (lstart < dc->di.lstart) { 1418 p = &(*p)->rb_left; 1419 } else if (lstart >= dc->di.lstart + dc->di.len) { 1420 p = &(*p)->rb_right; 1421 leftmost = false; 1422 } else { 1423 /* Let's skip to add, if exists */ 1424 return; 1425 } 1426 } 1427 1428 dc = __create_discard_cmd(sbi, bdev, lstart, start, len); 1429 1430 rb_link_node(&dc->rb_node, parent, p); 1431 rb_insert_color_cached(&dc->rb_node, &dcc->root, leftmost); 1432 } 1433 1434 static void __relocate_discard_cmd(struct discard_cmd_control *dcc, 1435 struct discard_cmd *dc) 1436 { 1437 list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->di.len)]); 1438 } 1439 1440 static void __punch_discard_cmd(struct f2fs_sb_info *sbi, 1441 struct discard_cmd *dc, block_t blkaddr) 1442 { 1443 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1444 struct discard_info di = dc->di; 1445 bool modified = false; 1446 1447 if (dc->state == D_DONE || dc->di.len == 1) { 1448 __remove_discard_cmd(sbi, dc); 1449 return; 1450 } 1451 1452 dcc->undiscard_blks -= di.len; 1453 1454 if (blkaddr > di.lstart) { 1455 dc->di.len = blkaddr - dc->di.lstart; 1456 dcc->undiscard_blks += dc->di.len; 1457 __relocate_discard_cmd(dcc, dc); 1458 modified = true; 1459 } 1460 1461 if (blkaddr < di.lstart + di.len - 1) { 1462 if (modified) { 1463 __insert_discard_cmd(sbi, dc->bdev, blkaddr + 1, 1464 di.start + blkaddr + 1 - di.lstart, 1465 di.lstart + di.len - 1 - blkaddr); 1466 } else { 1467 dc->di.lstart++; 1468 dc->di.len--; 1469 dc->di.start++; 1470 dcc->undiscard_blks += dc->di.len; 1471 __relocate_discard_cmd(dcc, dc); 1472 } 1473 } 1474 } 1475 1476 static void __update_discard_tree_range(struct f2fs_sb_info *sbi, 1477 struct block_device *bdev, block_t lstart, 1478 block_t start, block_t len) 1479 { 1480 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1481 struct discard_cmd *prev_dc = NULL, *next_dc = NULL; 1482 struct discard_cmd *dc; 1483 struct discard_info di = {0}; 1484 struct rb_node **insert_p = NULL, *insert_parent = NULL; 1485 unsigned int max_discard_blocks = 1486 SECTOR_TO_BLOCK(bdev_max_discard_sectors(bdev)); 1487 block_t end = lstart + len; 1488 1489 dc = __lookup_discard_cmd_ret(&dcc->root, lstart, 1490 &prev_dc, &next_dc, &insert_p, &insert_parent); 1491 if (dc) 1492 prev_dc = dc; 1493 1494 if (!prev_dc) { 1495 di.lstart = lstart; 1496 di.len = next_dc ? next_dc->di.lstart - lstart : len; 1497 di.len = min(di.len, len); 1498 di.start = start; 1499 } 1500 1501 while (1) { 1502 struct rb_node *node; 1503 bool merged = false; 1504 struct discard_cmd *tdc = NULL; 1505 1506 if (prev_dc) { 1507 di.lstart = prev_dc->di.lstart + prev_dc->di.len; 1508 if (di.lstart < lstart) 1509 di.lstart = lstart; 1510 if (di.lstart >= end) 1511 break; 1512 1513 if (!next_dc || next_dc->di.lstart > end) 1514 di.len = end - di.lstart; 1515 else 1516 di.len = next_dc->di.lstart - di.lstart; 1517 di.start = start + di.lstart - lstart; 1518 } 1519 1520 if (!di.len) 1521 goto next; 1522 1523 if (prev_dc && prev_dc->state == D_PREP && 1524 prev_dc->bdev == bdev && 1525 __is_discard_back_mergeable(&di, &prev_dc->di, 1526 max_discard_blocks)) { 1527 prev_dc->di.len += di.len; 1528 dcc->undiscard_blks += di.len; 1529 __relocate_discard_cmd(dcc, prev_dc); 1530 di = prev_dc->di; 1531 tdc = prev_dc; 1532 merged = true; 1533 } 1534 1535 if (next_dc && next_dc->state == D_PREP && 1536 next_dc->bdev == bdev && 1537 __is_discard_front_mergeable(&di, &next_dc->di, 1538 max_discard_blocks)) { 1539 next_dc->di.lstart = di.lstart; 1540 next_dc->di.len += di.len; 1541 next_dc->di.start = di.start; 1542 dcc->undiscard_blks += di.len; 1543 __relocate_discard_cmd(dcc, next_dc); 1544 if (tdc) 1545 __remove_discard_cmd(sbi, tdc); 1546 merged = true; 1547 } 1548 1549 if (!merged) 1550 __insert_discard_cmd(sbi, bdev, 1551 di.lstart, di.start, di.len); 1552 next: 1553 prev_dc = next_dc; 1554 if (!prev_dc) 1555 break; 1556 1557 node = rb_next(&prev_dc->rb_node); 1558 next_dc = rb_entry_safe(node, struct discard_cmd, rb_node); 1559 } 1560 } 1561 1562 #ifdef CONFIG_BLK_DEV_ZONED 1563 static void __queue_zone_reset_cmd(struct f2fs_sb_info *sbi, 1564 struct block_device *bdev, block_t blkstart, block_t lblkstart, 1565 block_t blklen) 1566 { 1567 trace_f2fs_queue_reset_zone(bdev, blkstart); 1568 1569 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); 1570 __insert_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen); 1571 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); 1572 } 1573 #endif 1574 1575 static void __queue_discard_cmd(struct f2fs_sb_info *sbi, 1576 struct block_device *bdev, block_t blkstart, block_t blklen) 1577 { 1578 block_t lblkstart = blkstart; 1579 1580 if (!f2fs_bdev_support_discard(bdev)) 1581 return; 1582 1583 trace_f2fs_queue_discard(bdev, blkstart, blklen); 1584 1585 if (f2fs_is_multi_device(sbi)) { 1586 int devi = f2fs_target_device_index(sbi, blkstart); 1587 1588 blkstart -= FDEV(devi).start_blk; 1589 } 1590 mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock); 1591 __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen); 1592 mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock); 1593 } 1594 1595 static void __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi, 1596 struct discard_policy *dpolicy, int *issued) 1597 { 1598 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1599 struct discard_cmd *prev_dc = NULL, *next_dc = NULL; 1600 struct rb_node **insert_p = NULL, *insert_parent = NULL; 1601 struct discard_cmd *dc; 1602 struct blk_plug plug; 1603 bool io_interrupted = false; 1604 1605 mutex_lock(&dcc->cmd_lock); 1606 dc = __lookup_discard_cmd_ret(&dcc->root, dcc->next_pos, 1607 &prev_dc, &next_dc, &insert_p, &insert_parent); 1608 if (!dc) 1609 dc = next_dc; 1610 1611 blk_start_plug(&plug); 1612 1613 while (dc) { 1614 struct rb_node *node; 1615 int err = 0; 1616 1617 if (dc->state != D_PREP) 1618 goto next; 1619 1620 if (dpolicy->io_aware && !is_idle(sbi, DISCARD_TIME)) { 1621 io_interrupted = true; 1622 break; 1623 } 1624 1625 dcc->next_pos = dc->di.lstart + dc->di.len; 1626 err = __submit_discard_cmd(sbi, dpolicy, dc, issued); 1627 1628 if (*issued >= dpolicy->max_requests) 1629 break; 1630 next: 1631 node = rb_next(&dc->rb_node); 1632 if (err) 1633 __remove_discard_cmd(sbi, dc); 1634 dc = rb_entry_safe(node, struct discard_cmd, rb_node); 1635 } 1636 1637 blk_finish_plug(&plug); 1638 1639 if (!dc) 1640 dcc->next_pos = 0; 1641 1642 mutex_unlock(&dcc->cmd_lock); 1643 1644 if (!(*issued) && io_interrupted) 1645 *issued = -1; 1646 } 1647 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, 1648 struct discard_policy *dpolicy); 1649 1650 static int __issue_discard_cmd(struct f2fs_sb_info *sbi, 1651 struct discard_policy *dpolicy) 1652 { 1653 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1654 struct list_head *pend_list; 1655 struct discard_cmd *dc, *tmp; 1656 struct blk_plug plug; 1657 int i, issued; 1658 bool io_interrupted = false; 1659 1660 if (dpolicy->timeout) 1661 f2fs_update_time(sbi, UMOUNT_DISCARD_TIMEOUT); 1662 1663 retry: 1664 issued = 0; 1665 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { 1666 if (dpolicy->timeout && 1667 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) 1668 break; 1669 1670 if (i + 1 < dpolicy->granularity) 1671 break; 1672 1673 if (i + 1 < dcc->max_ordered_discard && dpolicy->ordered) { 1674 __issue_discard_cmd_orderly(sbi, dpolicy, &issued); 1675 return issued; 1676 } 1677 1678 pend_list = &dcc->pend_list[i]; 1679 1680 mutex_lock(&dcc->cmd_lock); 1681 if (list_empty(pend_list)) 1682 goto next; 1683 if (unlikely(dcc->rbtree_check)) 1684 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi)); 1685 blk_start_plug(&plug); 1686 list_for_each_entry_safe(dc, tmp, pend_list, list) { 1687 f2fs_bug_on(sbi, dc->state != D_PREP); 1688 1689 if (dpolicy->timeout && 1690 f2fs_time_over(sbi, UMOUNT_DISCARD_TIMEOUT)) 1691 break; 1692 1693 if (dpolicy->io_aware && i < dpolicy->io_aware_gran && 1694 !is_idle(sbi, DISCARD_TIME)) { 1695 io_interrupted = true; 1696 break; 1697 } 1698 1699 __submit_discard_cmd(sbi, dpolicy, dc, &issued); 1700 1701 if (issued >= dpolicy->max_requests) 1702 break; 1703 } 1704 blk_finish_plug(&plug); 1705 next: 1706 mutex_unlock(&dcc->cmd_lock); 1707 1708 if (issued >= dpolicy->max_requests || io_interrupted) 1709 break; 1710 } 1711 1712 if (dpolicy->type == DPOLICY_UMOUNT && issued) { 1713 __wait_all_discard_cmd(sbi, dpolicy); 1714 goto retry; 1715 } 1716 1717 if (!issued && io_interrupted) 1718 issued = -1; 1719 1720 return issued; 1721 } 1722 1723 static bool __drop_discard_cmd(struct f2fs_sb_info *sbi) 1724 { 1725 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1726 struct list_head *pend_list; 1727 struct discard_cmd *dc, *tmp; 1728 int i; 1729 bool dropped = false; 1730 1731 mutex_lock(&dcc->cmd_lock); 1732 for (i = MAX_PLIST_NUM - 1; i >= 0; i--) { 1733 pend_list = &dcc->pend_list[i]; 1734 list_for_each_entry_safe(dc, tmp, pend_list, list) { 1735 f2fs_bug_on(sbi, dc->state != D_PREP); 1736 __remove_discard_cmd(sbi, dc); 1737 dropped = true; 1738 } 1739 } 1740 mutex_unlock(&dcc->cmd_lock); 1741 1742 return dropped; 1743 } 1744 1745 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi) 1746 { 1747 __drop_discard_cmd(sbi); 1748 } 1749 1750 static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi, 1751 struct discard_cmd *dc) 1752 { 1753 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1754 unsigned int len = 0; 1755 1756 wait_for_completion_io(&dc->wait); 1757 mutex_lock(&dcc->cmd_lock); 1758 f2fs_bug_on(sbi, dc->state != D_DONE); 1759 dc->ref--; 1760 if (!dc->ref) { 1761 if (!dc->error) 1762 len = dc->di.len; 1763 __remove_discard_cmd(sbi, dc); 1764 } 1765 mutex_unlock(&dcc->cmd_lock); 1766 1767 return len; 1768 } 1769 1770 static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi, 1771 struct discard_policy *dpolicy, 1772 block_t start, block_t end) 1773 { 1774 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1775 struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ? 1776 &(dcc->fstrim_list) : &(dcc->wait_list); 1777 struct discard_cmd *dc = NULL, *iter, *tmp; 1778 unsigned int trimmed = 0; 1779 1780 next: 1781 dc = NULL; 1782 1783 mutex_lock(&dcc->cmd_lock); 1784 list_for_each_entry_safe(iter, tmp, wait_list, list) { 1785 if (iter->di.lstart + iter->di.len <= start || 1786 end <= iter->di.lstart) 1787 continue; 1788 if (iter->di.len < dpolicy->granularity) 1789 continue; 1790 if (iter->state == D_DONE && !iter->ref) { 1791 wait_for_completion_io(&iter->wait); 1792 if (!iter->error) 1793 trimmed += iter->di.len; 1794 __remove_discard_cmd(sbi, iter); 1795 } else { 1796 iter->ref++; 1797 dc = iter; 1798 break; 1799 } 1800 } 1801 mutex_unlock(&dcc->cmd_lock); 1802 1803 if (dc) { 1804 trimmed += __wait_one_discard_bio(sbi, dc); 1805 goto next; 1806 } 1807 1808 return trimmed; 1809 } 1810 1811 static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi, 1812 struct discard_policy *dpolicy) 1813 { 1814 struct discard_policy dp; 1815 unsigned int discard_blks; 1816 1817 if (dpolicy) 1818 return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX); 1819 1820 /* wait all */ 1821 __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, MIN_DISCARD_GRANULARITY); 1822 discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); 1823 __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, MIN_DISCARD_GRANULARITY); 1824 discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX); 1825 1826 return discard_blks; 1827 } 1828 1829 /* This should be covered by global mutex, &sit_i->sentry_lock */ 1830 static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr) 1831 { 1832 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1833 struct discard_cmd *dc; 1834 bool need_wait = false; 1835 1836 mutex_lock(&dcc->cmd_lock); 1837 dc = __lookup_discard_cmd(sbi, blkaddr); 1838 #ifdef CONFIG_BLK_DEV_ZONED 1839 if (dc && f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(dc->bdev)) { 1840 int devi = f2fs_bdev_index(sbi, dc->bdev); 1841 1842 if (devi < 0) { 1843 mutex_unlock(&dcc->cmd_lock); 1844 return; 1845 } 1846 1847 if (f2fs_blkz_is_seq(sbi, devi, dc->di.start)) { 1848 /* force submit zone reset */ 1849 if (dc->state == D_PREP) 1850 __submit_zone_reset_cmd(sbi, dc, REQ_SYNC, 1851 &dcc->wait_list, NULL); 1852 dc->ref++; 1853 mutex_unlock(&dcc->cmd_lock); 1854 /* wait zone reset */ 1855 __wait_one_discard_bio(sbi, dc); 1856 return; 1857 } 1858 } 1859 #endif 1860 if (dc) { 1861 if (dc->state == D_PREP) { 1862 __punch_discard_cmd(sbi, dc, blkaddr); 1863 } else { 1864 dc->ref++; 1865 need_wait = true; 1866 } 1867 } 1868 mutex_unlock(&dcc->cmd_lock); 1869 1870 if (need_wait) 1871 __wait_one_discard_bio(sbi, dc); 1872 } 1873 1874 void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi) 1875 { 1876 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1877 1878 if (dcc && dcc->f2fs_issue_discard) { 1879 struct task_struct *discard_thread = dcc->f2fs_issue_discard; 1880 1881 dcc->f2fs_issue_discard = NULL; 1882 kthread_stop(discard_thread); 1883 } 1884 } 1885 1886 /** 1887 * f2fs_issue_discard_timeout() - Issue all discard cmd within UMOUNT_DISCARD_TIMEOUT 1888 * @sbi: the f2fs_sb_info data for discard cmd to issue 1889 * 1890 * When UMOUNT_DISCARD_TIMEOUT is exceeded, all remaining discard commands will be dropped 1891 * 1892 * Return true if issued all discard cmd or no discard cmd need issue, otherwise return false. 1893 */ 1894 bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi) 1895 { 1896 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1897 struct discard_policy dpolicy; 1898 bool dropped; 1899 1900 if (!atomic_read(&dcc->discard_cmd_cnt)) 1901 return true; 1902 1903 __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT, 1904 dcc->discard_granularity); 1905 __issue_discard_cmd(sbi, &dpolicy); 1906 dropped = __drop_discard_cmd(sbi); 1907 1908 /* just to make sure there is no pending discard commands */ 1909 __wait_all_discard_cmd(sbi, NULL); 1910 1911 f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt)); 1912 return !dropped; 1913 } 1914 1915 static int issue_discard_thread(void *data) 1916 { 1917 struct f2fs_sb_info *sbi = data; 1918 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1919 wait_queue_head_t *q = &dcc->discard_wait_queue; 1920 struct discard_policy dpolicy; 1921 unsigned int wait_ms = dcc->min_discard_issue_time; 1922 int issued; 1923 1924 set_freezable(); 1925 1926 do { 1927 wait_event_freezable_timeout(*q, 1928 kthread_should_stop() || dcc->discard_wake, 1929 msecs_to_jiffies(wait_ms)); 1930 1931 if (sbi->gc_mode == GC_URGENT_HIGH || 1932 !f2fs_available_free_memory(sbi, DISCARD_CACHE)) 1933 __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1934 MIN_DISCARD_GRANULARITY); 1935 else 1936 __init_discard_policy(sbi, &dpolicy, DPOLICY_BG, 1937 dcc->discard_granularity); 1938 1939 if (dcc->discard_wake) 1940 dcc->discard_wake = false; 1941 1942 /* clean up pending candidates before going to sleep */ 1943 if (atomic_read(&dcc->queued_discard)) 1944 __wait_all_discard_cmd(sbi, NULL); 1945 1946 if (f2fs_readonly(sbi->sb)) 1947 continue; 1948 if (kthread_should_stop()) 1949 return 0; 1950 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK) || 1951 !atomic_read(&dcc->discard_cmd_cnt)) { 1952 wait_ms = dpolicy.max_interval; 1953 continue; 1954 } 1955 1956 sb_start_intwrite(sbi->sb); 1957 1958 issued = __issue_discard_cmd(sbi, &dpolicy); 1959 if (issued > 0) { 1960 __wait_all_discard_cmd(sbi, &dpolicy); 1961 wait_ms = dpolicy.min_interval; 1962 } else if (issued == -1) { 1963 wait_ms = f2fs_time_to_wait(sbi, DISCARD_TIME); 1964 if (!wait_ms) 1965 wait_ms = dpolicy.mid_interval; 1966 } else { 1967 wait_ms = dpolicy.max_interval; 1968 } 1969 if (!atomic_read(&dcc->discard_cmd_cnt)) 1970 wait_ms = dpolicy.max_interval; 1971 1972 sb_end_intwrite(sbi->sb); 1973 1974 } while (!kthread_should_stop()); 1975 return 0; 1976 } 1977 1978 #ifdef CONFIG_BLK_DEV_ZONED 1979 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, 1980 struct block_device *bdev, block_t blkstart, block_t blklen) 1981 { 1982 sector_t sector, nr_sects; 1983 block_t lblkstart = blkstart; 1984 int devi = 0; 1985 u64 remainder = 0; 1986 1987 if (f2fs_is_multi_device(sbi)) { 1988 devi = f2fs_target_device_index(sbi, blkstart); 1989 if (blkstart < FDEV(devi).start_blk || 1990 blkstart > FDEV(devi).end_blk) { 1991 f2fs_err(sbi, "Invalid block %x", blkstart); 1992 return -EIO; 1993 } 1994 blkstart -= FDEV(devi).start_blk; 1995 } 1996 1997 /* For sequential zones, reset the zone write pointer */ 1998 if (f2fs_blkz_is_seq(sbi, devi, blkstart)) { 1999 sector = SECTOR_FROM_BLOCK(blkstart); 2000 nr_sects = SECTOR_FROM_BLOCK(blklen); 2001 div64_u64_rem(sector, bdev_zone_sectors(bdev), &remainder); 2002 2003 if (remainder || nr_sects != bdev_zone_sectors(bdev)) { 2004 f2fs_err(sbi, "(%d) %s: Unaligned zone reset attempted (block %x + %x)", 2005 devi, sbi->s_ndevs ? FDEV(devi).path : "", 2006 blkstart, blklen); 2007 return -EIO; 2008 } 2009 2010 if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING))) { 2011 unsigned int nofs_flags; 2012 int ret; 2013 2014 trace_f2fs_issue_reset_zone(bdev, blkstart); 2015 nofs_flags = memalloc_nofs_save(); 2016 ret = blkdev_zone_mgmt(bdev, REQ_OP_ZONE_RESET, 2017 sector, nr_sects); 2018 memalloc_nofs_restore(nofs_flags); 2019 return ret; 2020 } 2021 2022 __queue_zone_reset_cmd(sbi, bdev, blkstart, lblkstart, blklen); 2023 return 0; 2024 } 2025 2026 /* For conventional zones, use regular discard if supported */ 2027 __queue_discard_cmd(sbi, bdev, lblkstart, blklen); 2028 return 0; 2029 } 2030 #endif 2031 2032 static int __issue_discard_async(struct f2fs_sb_info *sbi, 2033 struct block_device *bdev, block_t blkstart, block_t blklen) 2034 { 2035 #ifdef CONFIG_BLK_DEV_ZONED 2036 if (f2fs_sb_has_blkzoned(sbi) && bdev_is_zoned(bdev)) 2037 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); 2038 #endif 2039 __queue_discard_cmd(sbi, bdev, blkstart, blklen); 2040 return 0; 2041 } 2042 2043 static int f2fs_issue_discard(struct f2fs_sb_info *sbi, 2044 block_t blkstart, block_t blklen) 2045 { 2046 sector_t start = blkstart, len = 0; 2047 struct block_device *bdev; 2048 struct seg_entry *se; 2049 unsigned int offset; 2050 block_t i; 2051 int err = 0; 2052 2053 bdev = f2fs_target_device(sbi, blkstart, NULL); 2054 2055 for (i = blkstart; i < blkstart + blklen; i++, len++) { 2056 if (i != start) { 2057 struct block_device *bdev2 = 2058 f2fs_target_device(sbi, i, NULL); 2059 2060 if (bdev2 != bdev) { 2061 err = __issue_discard_async(sbi, bdev, 2062 start, len); 2063 if (err) 2064 return err; 2065 bdev = bdev2; 2066 start = i; 2067 len = 0; 2068 } 2069 } 2070 2071 se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); 2072 offset = GET_BLKOFF_FROM_SEG0(sbi, i); 2073 2074 if (f2fs_block_unit_discard(sbi) && 2075 !f2fs_test_and_set_bit(offset, se->discard_map)) 2076 sbi->discard_blks--; 2077 } 2078 2079 if (len) 2080 err = __issue_discard_async(sbi, bdev, start, len); 2081 return err; 2082 } 2083 2084 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, 2085 bool check_only) 2086 { 2087 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 2088 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); 2089 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 2090 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 2091 unsigned long *discard_map = (unsigned long *)se->discard_map; 2092 unsigned long *dmap = SIT_I(sbi)->tmp_map; 2093 unsigned int start = 0, end = -1; 2094 bool force = (cpc->reason & CP_DISCARD); 2095 struct discard_entry *de = NULL; 2096 struct list_head *head = &SM_I(sbi)->dcc_info->entry_list; 2097 int i; 2098 2099 if (se->valid_blocks == BLKS_PER_SEG(sbi) || 2100 !f2fs_hw_support_discard(sbi) || 2101 !f2fs_block_unit_discard(sbi)) 2102 return false; 2103 2104 if (!force) { 2105 if (!f2fs_realtime_discard_enable(sbi) || 2106 (!se->valid_blocks && 2107 !is_curseg(sbi, cpc->trim_start)) || 2108 SM_I(sbi)->dcc_info->nr_discards >= 2109 SM_I(sbi)->dcc_info->max_discards) 2110 return false; 2111 } 2112 2113 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ 2114 for (i = 0; i < entries; i++) 2115 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : 2116 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; 2117 2118 while (force || SM_I(sbi)->dcc_info->nr_discards <= 2119 SM_I(sbi)->dcc_info->max_discards) { 2120 start = __find_rev_next_bit(dmap, BLKS_PER_SEG(sbi), end + 1); 2121 if (start >= BLKS_PER_SEG(sbi)) 2122 break; 2123 2124 end = __find_rev_next_zero_bit(dmap, 2125 BLKS_PER_SEG(sbi), start + 1); 2126 if (force && start && end != BLKS_PER_SEG(sbi) && 2127 (end - start) < cpc->trim_minlen) 2128 continue; 2129 2130 if (check_only) 2131 return true; 2132 2133 if (!de) { 2134 de = f2fs_kmem_cache_alloc(discard_entry_slab, 2135 GFP_F2FS_ZERO, true, NULL); 2136 de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start); 2137 list_add_tail(&de->list, head); 2138 } 2139 2140 for (i = start; i < end; i++) 2141 __set_bit_le(i, (void *)de->discard_map); 2142 2143 SM_I(sbi)->dcc_info->nr_discards += end - start; 2144 } 2145 return false; 2146 } 2147 2148 static void release_discard_addr(struct discard_entry *entry) 2149 { 2150 list_del(&entry->list); 2151 kmem_cache_free(discard_entry_slab, entry); 2152 } 2153 2154 void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi) 2155 { 2156 struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list); 2157 struct discard_entry *entry, *this; 2158 2159 /* drop caches */ 2160 list_for_each_entry_safe(entry, this, head, list) 2161 release_discard_addr(entry); 2162 } 2163 2164 /* 2165 * Should call f2fs_clear_prefree_segments after checkpoint is done. 2166 */ 2167 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) 2168 { 2169 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2170 unsigned int segno; 2171 2172 mutex_lock(&dirty_i->seglist_lock); 2173 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) 2174 __set_test_and_free(sbi, segno, false); 2175 mutex_unlock(&dirty_i->seglist_lock); 2176 } 2177 2178 void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi, 2179 struct cp_control *cpc) 2180 { 2181 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 2182 struct list_head *head = &dcc->entry_list; 2183 struct discard_entry *entry, *this; 2184 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2185 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; 2186 unsigned int start = 0, end = -1; 2187 unsigned int secno, start_segno; 2188 bool force = (cpc->reason & CP_DISCARD); 2189 bool section_alignment = F2FS_OPTION(sbi).discard_unit == 2190 DISCARD_UNIT_SECTION; 2191 2192 if (f2fs_lfs_mode(sbi) && __is_large_section(sbi)) 2193 section_alignment = true; 2194 2195 mutex_lock(&dirty_i->seglist_lock); 2196 2197 while (1) { 2198 int i; 2199 2200 if (section_alignment && end != -1) 2201 end--; 2202 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); 2203 if (start >= MAIN_SEGS(sbi)) 2204 break; 2205 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), 2206 start + 1); 2207 2208 if (section_alignment) { 2209 start = rounddown(start, SEGS_PER_SEC(sbi)); 2210 end = roundup(end, SEGS_PER_SEC(sbi)); 2211 } 2212 2213 for (i = start; i < end; i++) { 2214 if (test_and_clear_bit(i, prefree_map)) 2215 dirty_i->nr_dirty[PRE]--; 2216 } 2217 2218 if (!f2fs_realtime_discard_enable(sbi)) 2219 continue; 2220 2221 if (force && start >= cpc->trim_start && 2222 (end - 1) <= cpc->trim_end) 2223 continue; 2224 2225 /* Should cover 2MB zoned device for zone-based reset */ 2226 if (!f2fs_sb_has_blkzoned(sbi) && 2227 (!f2fs_lfs_mode(sbi) || !__is_large_section(sbi))) { 2228 f2fs_issue_discard(sbi, START_BLOCK(sbi, start), 2229 SEGS_TO_BLKS(sbi, end - start)); 2230 continue; 2231 } 2232 next: 2233 secno = GET_SEC_FROM_SEG(sbi, start); 2234 start_segno = GET_SEG_FROM_SEC(sbi, secno); 2235 if (!is_cursec(sbi, secno) && 2236 !get_valid_blocks(sbi, start, true)) 2237 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), 2238 BLKS_PER_SEC(sbi)); 2239 2240 start = start_segno + SEGS_PER_SEC(sbi); 2241 if (start < end) 2242 goto next; 2243 else 2244 end = start - 1; 2245 } 2246 mutex_unlock(&dirty_i->seglist_lock); 2247 2248 if (!f2fs_block_unit_discard(sbi)) 2249 goto wakeup; 2250 2251 /* send small discards */ 2252 list_for_each_entry_safe(entry, this, head, list) { 2253 unsigned int cur_pos = 0, next_pos, len, total_len = 0; 2254 bool is_valid = test_bit_le(0, entry->discard_map); 2255 2256 find_next: 2257 if (is_valid) { 2258 next_pos = find_next_zero_bit_le(entry->discard_map, 2259 BLKS_PER_SEG(sbi), cur_pos); 2260 len = next_pos - cur_pos; 2261 2262 if (f2fs_sb_has_blkzoned(sbi) || 2263 (force && len < cpc->trim_minlen)) 2264 goto skip; 2265 2266 f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos, 2267 len); 2268 total_len += len; 2269 } else { 2270 next_pos = find_next_bit_le(entry->discard_map, 2271 BLKS_PER_SEG(sbi), cur_pos); 2272 } 2273 skip: 2274 cur_pos = next_pos; 2275 is_valid = !is_valid; 2276 2277 if (cur_pos < BLKS_PER_SEG(sbi)) 2278 goto find_next; 2279 2280 release_discard_addr(entry); 2281 dcc->nr_discards -= total_len; 2282 } 2283 2284 wakeup: 2285 wake_up_discard_thread(sbi, false); 2286 } 2287 2288 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi) 2289 { 2290 dev_t dev = sbi->sb->s_bdev->bd_dev; 2291 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 2292 int err = 0; 2293 2294 if (f2fs_sb_has_readonly(sbi)) { 2295 f2fs_info(sbi, 2296 "Skip to start discard thread for readonly image"); 2297 return 0; 2298 } 2299 2300 if (!f2fs_realtime_discard_enable(sbi)) 2301 return 0; 2302 2303 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, 2304 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev)); 2305 if (IS_ERR(dcc->f2fs_issue_discard)) { 2306 err = PTR_ERR(dcc->f2fs_issue_discard); 2307 dcc->f2fs_issue_discard = NULL; 2308 } 2309 2310 return err; 2311 } 2312 2313 static int create_discard_cmd_control(struct f2fs_sb_info *sbi) 2314 { 2315 struct discard_cmd_control *dcc; 2316 int err = 0, i; 2317 2318 if (SM_I(sbi)->dcc_info) { 2319 dcc = SM_I(sbi)->dcc_info; 2320 goto init_thread; 2321 } 2322 2323 dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL); 2324 if (!dcc) 2325 return -ENOMEM; 2326 2327 dcc->discard_io_aware_gran = MAX_PLIST_NUM; 2328 dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY; 2329 dcc->max_ordered_discard = DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY; 2330 dcc->discard_io_aware = DPOLICY_IO_AWARE_ENABLE; 2331 if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT || 2332 F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION) 2333 dcc->discard_granularity = BLKS_PER_SEG(sbi); 2334 2335 INIT_LIST_HEAD(&dcc->entry_list); 2336 for (i = 0; i < MAX_PLIST_NUM; i++) 2337 INIT_LIST_HEAD(&dcc->pend_list[i]); 2338 INIT_LIST_HEAD(&dcc->wait_list); 2339 INIT_LIST_HEAD(&dcc->fstrim_list); 2340 mutex_init(&dcc->cmd_lock); 2341 atomic_set(&dcc->issued_discard, 0); 2342 atomic_set(&dcc->queued_discard, 0); 2343 atomic_set(&dcc->discard_cmd_cnt, 0); 2344 dcc->nr_discards = 0; 2345 dcc->max_discards = SEGS_TO_BLKS(sbi, MAIN_SEGS(sbi)); 2346 dcc->max_discard_request = DEF_MAX_DISCARD_REQUEST; 2347 dcc->min_discard_issue_time = DEF_MIN_DISCARD_ISSUE_TIME; 2348 dcc->mid_discard_issue_time = DEF_MID_DISCARD_ISSUE_TIME; 2349 dcc->max_discard_issue_time = DEF_MAX_DISCARD_ISSUE_TIME; 2350 dcc->discard_urgent_util = DEF_DISCARD_URGENT_UTIL; 2351 dcc->undiscard_blks = 0; 2352 dcc->next_pos = 0; 2353 dcc->root = RB_ROOT_CACHED; 2354 dcc->rbtree_check = false; 2355 2356 init_waitqueue_head(&dcc->discard_wait_queue); 2357 SM_I(sbi)->dcc_info = dcc; 2358 init_thread: 2359 err = f2fs_start_discard_thread(sbi); 2360 if (err) { 2361 kfree(dcc); 2362 SM_I(sbi)->dcc_info = NULL; 2363 } 2364 2365 return err; 2366 } 2367 2368 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi) 2369 { 2370 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 2371 2372 if (!dcc) 2373 return; 2374 2375 f2fs_stop_discard_thread(sbi); 2376 2377 /* 2378 * Recovery can cache discard commands, so in error path of 2379 * fill_super(), it needs to give a chance to handle them. 2380 */ 2381 f2fs_issue_discard_timeout(sbi); 2382 2383 kfree(dcc); 2384 SM_I(sbi)->dcc_info = NULL; 2385 } 2386 2387 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) 2388 { 2389 struct sit_info *sit_i = SIT_I(sbi); 2390 2391 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { 2392 sit_i->dirty_sentries++; 2393 return false; 2394 } 2395 2396 return true; 2397 } 2398 2399 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, 2400 unsigned int segno, int modified) 2401 { 2402 struct seg_entry *se = get_seg_entry(sbi, segno); 2403 2404 se->type = type; 2405 if (modified) 2406 __mark_sit_entry_dirty(sbi, segno); 2407 } 2408 2409 static inline unsigned long long get_segment_mtime(struct f2fs_sb_info *sbi, 2410 block_t blkaddr) 2411 { 2412 unsigned int segno = GET_SEGNO(sbi, blkaddr); 2413 2414 if (segno == NULL_SEGNO) 2415 return 0; 2416 return get_seg_entry(sbi, segno)->mtime; 2417 } 2418 2419 static void update_segment_mtime(struct f2fs_sb_info *sbi, block_t blkaddr, 2420 unsigned long long old_mtime) 2421 { 2422 struct seg_entry *se; 2423 unsigned int segno = GET_SEGNO(sbi, blkaddr); 2424 unsigned long long ctime = get_mtime(sbi, false); 2425 unsigned long long mtime = old_mtime ? old_mtime : ctime; 2426 2427 if (segno == NULL_SEGNO) 2428 return; 2429 2430 se = get_seg_entry(sbi, segno); 2431 2432 if (!se->mtime) 2433 se->mtime = mtime; 2434 else 2435 se->mtime = div_u64(se->mtime * se->valid_blocks + mtime, 2436 se->valid_blocks + 1); 2437 2438 if (ctime > SIT_I(sbi)->max_mtime) 2439 SIT_I(sbi)->max_mtime = ctime; 2440 } 2441 2442 /* 2443 * NOTE: when updating multiple blocks at the same time, please ensure 2444 * that the consecutive input blocks belong to the same segment. 2445 */ 2446 static int update_sit_entry_for_release(struct f2fs_sb_info *sbi, struct seg_entry *se, 2447 unsigned int segno, block_t blkaddr, unsigned int offset, int del) 2448 { 2449 bool exist; 2450 #ifdef CONFIG_F2FS_CHECK_FS 2451 bool mir_exist; 2452 #endif 2453 int i; 2454 int del_count = -del; 2455 2456 f2fs_bug_on(sbi, GET_SEGNO(sbi, blkaddr) != GET_SEGNO(sbi, blkaddr + del_count - 1)); 2457 2458 for (i = 0; i < del_count; i++) { 2459 exist = f2fs_test_and_clear_bit(offset + i, se->cur_valid_map); 2460 #ifdef CONFIG_F2FS_CHECK_FS 2461 mir_exist = f2fs_test_and_clear_bit(offset + i, 2462 se->cur_valid_map_mir); 2463 if (unlikely(exist != mir_exist)) { 2464 f2fs_err(sbi, "Inconsistent error when clearing bitmap, blk:%u, old bit:%d", 2465 blkaddr + i, exist); 2466 f2fs_bug_on(sbi, 1); 2467 } 2468 #endif 2469 if (unlikely(!exist)) { 2470 f2fs_err(sbi, "Bitmap was wrongly cleared, blk:%u", blkaddr + i); 2471 f2fs_bug_on(sbi, 1); 2472 se->valid_blocks++; 2473 del += 1; 2474 } else if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 2475 /* 2476 * If checkpoints are off, we must not reuse data that 2477 * was used in the previous checkpoint. If it was used 2478 * before, we must track that to know how much space we 2479 * really have. 2480 */ 2481 if (f2fs_test_bit(offset + i, se->ckpt_valid_map)) { 2482 spin_lock(&sbi->stat_lock); 2483 sbi->unusable_block_count++; 2484 spin_unlock(&sbi->stat_lock); 2485 } 2486 } 2487 2488 if (f2fs_block_unit_discard(sbi) && 2489 f2fs_test_and_clear_bit(offset + i, se->discard_map)) 2490 sbi->discard_blks++; 2491 2492 if (!f2fs_test_bit(offset + i, se->ckpt_valid_map)) { 2493 se->ckpt_valid_blocks -= 1; 2494 if (__is_large_section(sbi)) 2495 get_sec_entry(sbi, segno)->ckpt_valid_blocks -= 1; 2496 } 2497 } 2498 2499 if (__is_large_section(sbi)) 2500 sanity_check_valid_blocks(sbi, segno); 2501 2502 return del; 2503 } 2504 2505 static int update_sit_entry_for_alloc(struct f2fs_sb_info *sbi, struct seg_entry *se, 2506 unsigned int segno, block_t blkaddr, unsigned int offset, int del) 2507 { 2508 bool exist; 2509 #ifdef CONFIG_F2FS_CHECK_FS 2510 bool mir_exist; 2511 #endif 2512 2513 exist = f2fs_test_and_set_bit(offset, se->cur_valid_map); 2514 #ifdef CONFIG_F2FS_CHECK_FS 2515 mir_exist = f2fs_test_and_set_bit(offset, 2516 se->cur_valid_map_mir); 2517 if (unlikely(exist != mir_exist)) { 2518 f2fs_err(sbi, "Inconsistent error when setting bitmap, blk:%u, old bit:%d", 2519 blkaddr, exist); 2520 f2fs_bug_on(sbi, 1); 2521 } 2522 #endif 2523 if (unlikely(exist)) { 2524 f2fs_err(sbi, "Bitmap was wrongly set, blk:%u", blkaddr); 2525 f2fs_bug_on(sbi, 1); 2526 se->valid_blocks--; 2527 del = 0; 2528 } 2529 2530 if (f2fs_block_unit_discard(sbi) && 2531 !f2fs_test_and_set_bit(offset, se->discard_map)) 2532 sbi->discard_blks--; 2533 2534 /* 2535 * SSR should never reuse block which is checkpointed 2536 * or newly invalidated. 2537 */ 2538 if (!is_sbi_flag_set(sbi, SBI_CP_DISABLED)) { 2539 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) { 2540 se->ckpt_valid_blocks++; 2541 if (__is_large_section(sbi)) 2542 get_sec_entry(sbi, segno)->ckpt_valid_blocks++; 2543 } 2544 } 2545 2546 if (!f2fs_test_bit(offset, se->ckpt_valid_map)) { 2547 se->ckpt_valid_blocks += del; 2548 if (__is_large_section(sbi)) 2549 get_sec_entry(sbi, segno)->ckpt_valid_blocks += del; 2550 } 2551 2552 if (__is_large_section(sbi)) 2553 sanity_check_valid_blocks(sbi, segno); 2554 2555 return del; 2556 } 2557 2558 /* 2559 * If releasing blocks, this function supports updating multiple consecutive blocks 2560 * at one time, but please note that these consecutive blocks need to belong to the 2561 * same segment. 2562 */ 2563 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) 2564 { 2565 struct seg_entry *se; 2566 unsigned int segno, offset; 2567 long int new_vblocks; 2568 2569 segno = GET_SEGNO(sbi, blkaddr); 2570 if (segno == NULL_SEGNO) 2571 return; 2572 2573 se = get_seg_entry(sbi, segno); 2574 new_vblocks = se->valid_blocks + del; 2575 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 2576 2577 f2fs_bug_on(sbi, (new_vblocks < 0 || 2578 (new_vblocks > f2fs_usable_blks_in_seg(sbi, segno)))); 2579 2580 se->valid_blocks = new_vblocks; 2581 2582 /* Update valid block bitmap */ 2583 if (del > 0) { 2584 del = update_sit_entry_for_alloc(sbi, se, segno, blkaddr, offset, del); 2585 } else { 2586 del = update_sit_entry_for_release(sbi, se, segno, blkaddr, offset, del); 2587 } 2588 2589 __mark_sit_entry_dirty(sbi, segno); 2590 2591 /* update total number of valid blocks to be written in ckpt area */ 2592 SIT_I(sbi)->written_valid_blocks += del; 2593 2594 if (__is_large_section(sbi)) 2595 get_sec_entry(sbi, segno)->valid_blocks += del; 2596 } 2597 2598 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr, 2599 unsigned int len) 2600 { 2601 unsigned int segno = GET_SEGNO(sbi, addr); 2602 struct sit_info *sit_i = SIT_I(sbi); 2603 block_t addr_start = addr, addr_end = addr + len - 1; 2604 unsigned int seg_num = GET_SEGNO(sbi, addr_end) - segno + 1; 2605 unsigned int i = 1, max_blocks = sbi->blocks_per_seg, cnt; 2606 2607 f2fs_bug_on(sbi, addr == NULL_ADDR); 2608 if (addr == NEW_ADDR || addr == COMPRESS_ADDR) 2609 return; 2610 2611 f2fs_invalidate_internal_cache(sbi, addr, len); 2612 2613 /* add it into sit main buffer */ 2614 down_write(&sit_i->sentry_lock); 2615 2616 if (seg_num == 1) 2617 cnt = len; 2618 else 2619 cnt = max_blocks - GET_BLKOFF_FROM_SEG0(sbi, addr); 2620 2621 do { 2622 update_segment_mtime(sbi, addr_start, 0); 2623 update_sit_entry(sbi, addr_start, -cnt); 2624 2625 /* add it into dirty seglist */ 2626 locate_dirty_segment(sbi, segno); 2627 2628 /* update @addr_start and @cnt and @segno */ 2629 addr_start = START_BLOCK(sbi, ++segno); 2630 if (++i == seg_num) 2631 cnt = GET_BLKOFF_FROM_SEG0(sbi, addr_end) + 1; 2632 else 2633 cnt = max_blocks; 2634 } while (i <= seg_num); 2635 2636 up_write(&sit_i->sentry_lock); 2637 } 2638 2639 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr) 2640 { 2641 struct sit_info *sit_i = SIT_I(sbi); 2642 unsigned int segno, offset; 2643 struct seg_entry *se; 2644 bool is_cp = false; 2645 2646 if (!__is_valid_data_blkaddr(blkaddr)) 2647 return true; 2648 2649 down_read(&sit_i->sentry_lock); 2650 2651 segno = GET_SEGNO(sbi, blkaddr); 2652 se = get_seg_entry(sbi, segno); 2653 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 2654 2655 if (f2fs_test_bit(offset, se->ckpt_valid_map)) 2656 is_cp = true; 2657 2658 up_read(&sit_i->sentry_lock); 2659 2660 return is_cp; 2661 } 2662 2663 static unsigned short f2fs_curseg_valid_blocks(struct f2fs_sb_info *sbi, int type) 2664 { 2665 struct curseg_info *curseg = CURSEG_I(sbi, type); 2666 2667 if (sbi->ckpt->alloc_type[type] == SSR) 2668 return BLKS_PER_SEG(sbi); 2669 return curseg->next_blkoff; 2670 } 2671 2672 /* 2673 * Calculate the number of current summary pages for writing 2674 */ 2675 int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) 2676 { 2677 int valid_sum_count = 0; 2678 int i, sum_in_page; 2679 2680 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 2681 if (sbi->ckpt->alloc_type[i] != SSR && for_ra) 2682 valid_sum_count += 2683 le16_to_cpu(F2FS_CKPT(sbi)->cur_data_blkoff[i]); 2684 else 2685 valid_sum_count += f2fs_curseg_valid_blocks(sbi, i); 2686 } 2687 2688 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE - 2689 SUM_FOOTER_SIZE) / SUMMARY_SIZE; 2690 if (valid_sum_count <= sum_in_page) 2691 return 1; 2692 else if ((valid_sum_count - sum_in_page) <= 2693 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) 2694 return 2; 2695 return 3; 2696 } 2697 2698 /* 2699 * Caller should put this summary folio 2700 */ 2701 struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno) 2702 { 2703 if (unlikely(f2fs_cp_error(sbi))) 2704 return ERR_PTR(-EIO); 2705 return f2fs_get_meta_folio_retry(sbi, GET_SUM_BLOCK(sbi, segno)); 2706 } 2707 2708 void f2fs_update_meta_page(struct f2fs_sb_info *sbi, 2709 void *src, block_t blk_addr) 2710 { 2711 struct folio *folio; 2712 2713 if (SUMS_PER_BLOCK == 1) 2714 folio = f2fs_grab_meta_folio(sbi, blk_addr); 2715 else 2716 folio = f2fs_get_meta_folio_retry(sbi, blk_addr); 2717 2718 if (IS_ERR(folio)) 2719 return; 2720 2721 memcpy(folio_address(folio), src, PAGE_SIZE); 2722 folio_mark_dirty(folio); 2723 f2fs_folio_put(folio, true); 2724 } 2725 2726 static void write_sum_page(struct f2fs_sb_info *sbi, 2727 struct f2fs_summary_block *sum_blk, unsigned int segno) 2728 { 2729 struct folio *folio; 2730 2731 if (SUMS_PER_BLOCK == 1) 2732 return f2fs_update_meta_page(sbi, (void *)sum_blk, 2733 GET_SUM_BLOCK(sbi, segno)); 2734 2735 folio = f2fs_get_sum_folio(sbi, segno); 2736 if (IS_ERR(folio)) 2737 return; 2738 2739 memcpy(SUM_BLK_PAGE_ADDR(folio, segno), sum_blk, sizeof(*sum_blk)); 2740 folio_mark_dirty(folio); 2741 f2fs_folio_put(folio, true); 2742 } 2743 2744 static void write_current_sum_page(struct f2fs_sb_info *sbi, 2745 int type, block_t blk_addr) 2746 { 2747 struct curseg_info *curseg = CURSEG_I(sbi, type); 2748 struct folio *folio = f2fs_grab_meta_folio(sbi, blk_addr); 2749 struct f2fs_summary_block *src = curseg->sum_blk; 2750 struct f2fs_summary_block *dst; 2751 2752 dst = folio_address(folio); 2753 memset(dst, 0, PAGE_SIZE); 2754 2755 mutex_lock(&curseg->curseg_mutex); 2756 2757 down_read(&curseg->journal_rwsem); 2758 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE); 2759 up_read(&curseg->journal_rwsem); 2760 2761 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE); 2762 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE); 2763 2764 mutex_unlock(&curseg->curseg_mutex); 2765 2766 folio_mark_dirty(folio); 2767 f2fs_folio_put(folio, true); 2768 } 2769 2770 static int is_next_segment_free(struct f2fs_sb_info *sbi, 2771 struct curseg_info *curseg) 2772 { 2773 unsigned int segno = curseg->segno + 1; 2774 struct free_segmap_info *free_i = FREE_I(sbi); 2775 2776 if (segno < MAIN_SEGS(sbi) && segno % SEGS_PER_SEC(sbi)) 2777 return !test_bit(segno, free_i->free_segmap); 2778 return 0; 2779 } 2780 2781 /* 2782 * Find a new segment from the free segments bitmap to right order 2783 * This function should be returned with success, otherwise BUG 2784 */ 2785 static int get_new_segment(struct f2fs_sb_info *sbi, 2786 unsigned int *newseg, bool new_sec, bool pinning) 2787 { 2788 struct free_segmap_info *free_i = FREE_I(sbi); 2789 unsigned int segno, secno, zoneno; 2790 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; 2791 unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg); 2792 unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg); 2793 unsigned int alloc_policy = sbi->allocate_section_policy; 2794 unsigned int alloc_hint = sbi->allocate_section_hint; 2795 bool init = true; 2796 int i; 2797 int ret = 0; 2798 2799 spin_lock(&free_i->segmap_lock); 2800 2801 if (time_to_inject(sbi, FAULT_NO_SEGMENT)) { 2802 ret = -ENOSPC; 2803 goto out_unlock; 2804 } 2805 2806 if (!new_sec && ((*newseg + 1) % SEGS_PER_SEC(sbi))) { 2807 segno = find_next_zero_bit(free_i->free_segmap, 2808 GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1); 2809 if (segno < GET_SEG_FROM_SEC(sbi, hint + 1)) 2810 goto got_it; 2811 } 2812 2813 #ifdef CONFIG_BLK_DEV_ZONED 2814 /* 2815 * If we format f2fs on zoned storage, let's try to get pinned sections 2816 * from beginning of the storage, which should be a conventional one. 2817 */ 2818 if (f2fs_sb_has_blkzoned(sbi)) { 2819 /* Prioritize writing to conventional zones */ 2820 if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_PRIOR_CONV || pinning) 2821 segno = 0; 2822 else 2823 segno = max(sbi->first_seq_zone_segno, *newseg); 2824 hint = GET_SEC_FROM_SEG(sbi, segno); 2825 } 2826 #endif 2827 2828 /* 2829 * Prevent allocate_section_hint from exceeding MAIN_SECS() 2830 * due to desynchronization. 2831 */ 2832 if (alloc_policy != ALLOCATE_FORWARD_NOHINT && 2833 alloc_hint > MAIN_SECS(sbi)) 2834 alloc_hint = MAIN_SECS(sbi); 2835 2836 if (alloc_policy == ALLOCATE_FORWARD_FROM_HINT && 2837 hint < alloc_hint) 2838 hint = alloc_hint; 2839 else if (alloc_policy == ALLOCATE_FORWARD_WITHIN_HINT && 2840 hint >= alloc_hint) 2841 hint = 0; 2842 2843 find_other_zone: 2844 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); 2845 2846 #ifdef CONFIG_BLK_DEV_ZONED 2847 if (secno >= MAIN_SECS(sbi) && f2fs_sb_has_blkzoned(sbi)) { 2848 /* Write only to sequential zones */ 2849 if (sbi->blkzone_alloc_policy == BLKZONE_ALLOC_ONLY_SEQ) { 2850 hint = GET_SEC_FROM_SEG(sbi, sbi->first_seq_zone_segno); 2851 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); 2852 } else 2853 secno = find_first_zero_bit(free_i->free_secmap, 2854 MAIN_SECS(sbi)); 2855 if (secno >= MAIN_SECS(sbi)) { 2856 ret = -ENOSPC; 2857 f2fs_bug_on(sbi, 1); 2858 goto out_unlock; 2859 } 2860 } 2861 #endif 2862 2863 if (secno >= MAIN_SECS(sbi)) { 2864 secno = find_first_zero_bit(free_i->free_secmap, 2865 MAIN_SECS(sbi)); 2866 if (secno >= MAIN_SECS(sbi)) { 2867 ret = -ENOSPC; 2868 f2fs_bug_on(sbi, !pinning); 2869 goto out_unlock; 2870 } 2871 } 2872 segno = GET_SEG_FROM_SEC(sbi, secno); 2873 zoneno = GET_ZONE_FROM_SEC(sbi, secno); 2874 2875 /* give up on finding another zone */ 2876 if (!init) 2877 goto got_it; 2878 if (sbi->secs_per_zone == 1) 2879 goto got_it; 2880 if (zoneno == old_zoneno) 2881 goto got_it; 2882 for (i = 0; i < NR_CURSEG_TYPE; i++) 2883 if (CURSEG_I(sbi, i)->zone == zoneno) 2884 break; 2885 2886 if (i < NR_CURSEG_TYPE) { 2887 /* zone is in user, try another */ 2888 if (zoneno + 1 >= total_zones) 2889 hint = 0; 2890 else 2891 hint = (zoneno + 1) * sbi->secs_per_zone; 2892 init = false; 2893 goto find_other_zone; 2894 } 2895 got_it: 2896 /* set it as dirty segment in free segmap */ 2897 if (test_bit(segno, free_i->free_segmap)) { 2898 ret = -EFSCORRUPTED; 2899 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_CORRUPTED_FREE_BITMAP); 2900 goto out_unlock; 2901 } 2902 2903 /* no free section in conventional device or conventional zone */ 2904 if (new_sec && pinning && 2905 f2fs_is_sequential_zone_area(sbi, START_BLOCK(sbi, segno))) { 2906 ret = -EAGAIN; 2907 goto out_unlock; 2908 } 2909 __set_inuse(sbi, segno); 2910 *newseg = segno; 2911 out_unlock: 2912 spin_unlock(&free_i->segmap_lock); 2913 2914 if (ret == -ENOSPC && !pinning) 2915 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_NO_SEGMENT); 2916 return ret; 2917 } 2918 2919 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) 2920 { 2921 struct curseg_info *curseg = CURSEG_I(sbi, type); 2922 struct summary_footer *sum_footer; 2923 unsigned short seg_type = curseg->seg_type; 2924 2925 /* only happen when get_new_segment() fails */ 2926 if (curseg->next_segno == NULL_SEGNO) 2927 return; 2928 2929 curseg->inited = true; 2930 curseg->segno = curseg->next_segno; 2931 curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno); 2932 curseg->next_blkoff = 0; 2933 curseg->next_segno = NULL_SEGNO; 2934 2935 sum_footer = &(curseg->sum_blk->footer); 2936 memset(sum_footer, 0, sizeof(struct summary_footer)); 2937 2938 sanity_check_seg_type(sbi, seg_type); 2939 2940 if (IS_DATASEG(seg_type)) 2941 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); 2942 if (IS_NODESEG(seg_type)) 2943 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); 2944 __set_sit_entry_type(sbi, seg_type, curseg->segno, modified); 2945 } 2946 2947 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) 2948 { 2949 struct curseg_info *curseg = CURSEG_I(sbi, type); 2950 unsigned short seg_type = curseg->seg_type; 2951 2952 sanity_check_seg_type(sbi, seg_type); 2953 if (__is_large_section(sbi)) { 2954 if (f2fs_need_rand_seg(sbi)) { 2955 unsigned int hint = GET_SEC_FROM_SEG(sbi, curseg->segno); 2956 2957 if (GET_SEC_FROM_SEG(sbi, curseg->segno + 1) != hint) 2958 return curseg->segno; 2959 return get_random_u32_inclusive(curseg->segno + 1, 2960 GET_SEG_FROM_SEC(sbi, hint + 1) - 1); 2961 } 2962 return curseg->segno; 2963 } else if (f2fs_need_rand_seg(sbi)) { 2964 return get_random_u32_below(MAIN_SECS(sbi) * SEGS_PER_SEC(sbi)); 2965 } 2966 2967 /* inmem log may not locate on any segment after mount */ 2968 if (!curseg->inited) 2969 return 0; 2970 2971 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 2972 return 0; 2973 2974 if (seg_type == CURSEG_HOT_DATA || IS_NODESEG(seg_type)) 2975 return 0; 2976 2977 if (SIT_I(sbi)->last_victim[ALLOC_NEXT]) 2978 return SIT_I(sbi)->last_victim[ALLOC_NEXT]; 2979 2980 /* find segments from 0 to reuse freed segments */ 2981 if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE) 2982 return 0; 2983 2984 return curseg->segno; 2985 } 2986 2987 static void reset_curseg_fields(struct curseg_info *curseg) 2988 { 2989 curseg->inited = false; 2990 curseg->segno = NULL_SEGNO; 2991 curseg->next_segno = 0; 2992 } 2993 2994 /* 2995 * Allocate a current working segment. 2996 * This function always allocates a free segment in LFS manner. 2997 */ 2998 static int new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) 2999 { 3000 struct curseg_info *curseg = CURSEG_I(sbi, type); 3001 unsigned int segno = curseg->segno; 3002 bool pinning = type == CURSEG_COLD_DATA_PINNED; 3003 int ret; 3004 3005 if (curseg->inited) 3006 write_sum_page(sbi, curseg->sum_blk, segno); 3007 3008 segno = __get_next_segno(sbi, type); 3009 ret = get_new_segment(sbi, &segno, new_sec, pinning); 3010 if (ret) { 3011 if (ret == -ENOSPC) 3012 reset_curseg_fields(curseg); 3013 return ret; 3014 } 3015 3016 curseg->next_segno = segno; 3017 reset_curseg(sbi, type, 1); 3018 curseg->alloc_type = LFS; 3019 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) 3020 curseg->fragment_remained_chunk = 3021 get_random_u32_inclusive(1, sbi->max_fragment_chunk); 3022 return 0; 3023 } 3024 3025 static int __next_free_blkoff(struct f2fs_sb_info *sbi, 3026 int segno, block_t start) 3027 { 3028 struct seg_entry *se = get_seg_entry(sbi, segno); 3029 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 3030 unsigned long *target_map = SIT_I(sbi)->tmp_map; 3031 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 3032 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 3033 int i; 3034 3035 for (i = 0; i < entries; i++) 3036 target_map[i] = ckpt_map[i] | cur_map[i]; 3037 3038 return __find_rev_next_zero_bit(target_map, BLKS_PER_SEG(sbi), start); 3039 } 3040 3041 static int f2fs_find_next_ssr_block(struct f2fs_sb_info *sbi, 3042 struct curseg_info *seg) 3043 { 3044 return __next_free_blkoff(sbi, seg->segno, seg->next_blkoff + 1); 3045 } 3046 3047 bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno) 3048 { 3049 return __next_free_blkoff(sbi, segno, 0) < BLKS_PER_SEG(sbi); 3050 } 3051 3052 /* 3053 * This function always allocates a used segment(from dirty seglist) by SSR 3054 * manner, so it should recover the existing segment information of valid blocks 3055 */ 3056 static int change_curseg(struct f2fs_sb_info *sbi, int type) 3057 { 3058 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 3059 struct curseg_info *curseg = CURSEG_I(sbi, type); 3060 unsigned int new_segno = curseg->next_segno; 3061 struct f2fs_summary_block *sum_node; 3062 struct folio *sum_folio; 3063 3064 if (curseg->inited) 3065 write_sum_page(sbi, curseg->sum_blk, curseg->segno); 3066 3067 __set_test_and_inuse(sbi, new_segno); 3068 3069 mutex_lock(&dirty_i->seglist_lock); 3070 __remove_dirty_segment(sbi, new_segno, PRE); 3071 __remove_dirty_segment(sbi, new_segno, DIRTY); 3072 mutex_unlock(&dirty_i->seglist_lock); 3073 3074 reset_curseg(sbi, type, 1); 3075 curseg->alloc_type = SSR; 3076 curseg->next_blkoff = __next_free_blkoff(sbi, curseg->segno, 0); 3077 3078 sum_folio = f2fs_get_sum_folio(sbi, new_segno); 3079 if (IS_ERR(sum_folio)) { 3080 /* GC won't be able to use stale summary pages by cp_error */ 3081 memset(curseg->sum_blk, 0, SUM_ENTRY_SIZE); 3082 return PTR_ERR(sum_folio); 3083 } 3084 sum_node = SUM_BLK_PAGE_ADDR(sum_folio, new_segno); 3085 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); 3086 f2fs_folio_put(sum_folio, true); 3087 return 0; 3088 } 3089 3090 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type, 3091 int alloc_mode, unsigned long long age); 3092 3093 static int get_atssr_segment(struct f2fs_sb_info *sbi, int type, 3094 int target_type, int alloc_mode, 3095 unsigned long long age) 3096 { 3097 struct curseg_info *curseg = CURSEG_I(sbi, type); 3098 int ret = 0; 3099 3100 curseg->seg_type = target_type; 3101 3102 if (get_ssr_segment(sbi, type, alloc_mode, age)) { 3103 struct seg_entry *se = get_seg_entry(sbi, curseg->next_segno); 3104 3105 curseg->seg_type = se->type; 3106 ret = change_curseg(sbi, type); 3107 } else { 3108 /* allocate cold segment by default */ 3109 curseg->seg_type = CURSEG_COLD_DATA; 3110 ret = new_curseg(sbi, type, true); 3111 } 3112 stat_inc_seg_type(sbi, curseg); 3113 return ret; 3114 } 3115 3116 static int __f2fs_init_atgc_curseg(struct f2fs_sb_info *sbi, bool force) 3117 { 3118 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_ALL_DATA_ATGC); 3119 int ret = 0; 3120 3121 if (!sbi->am.atgc_enabled && !force) 3122 return 0; 3123 3124 f2fs_down_read(&SM_I(sbi)->curseg_lock); 3125 3126 mutex_lock(&curseg->curseg_mutex); 3127 down_write(&SIT_I(sbi)->sentry_lock); 3128 3129 ret = get_atssr_segment(sbi, CURSEG_ALL_DATA_ATGC, 3130 CURSEG_COLD_DATA, SSR, 0); 3131 3132 up_write(&SIT_I(sbi)->sentry_lock); 3133 mutex_unlock(&curseg->curseg_mutex); 3134 3135 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3136 return ret; 3137 } 3138 3139 int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi) 3140 { 3141 return __f2fs_init_atgc_curseg(sbi, false); 3142 } 3143 3144 int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi) 3145 { 3146 int ret; 3147 3148 if (!test_opt(sbi, ATGC)) 3149 return 0; 3150 if (sbi->am.atgc_enabled) 3151 return 0; 3152 if (le64_to_cpu(F2FS_CKPT(sbi)->elapsed_time) < 3153 sbi->am.age_threshold) 3154 return 0; 3155 3156 ret = __f2fs_init_atgc_curseg(sbi, true); 3157 if (!ret) { 3158 sbi->am.atgc_enabled = true; 3159 f2fs_info(sbi, "reenabled age threshold GC"); 3160 } 3161 return ret; 3162 } 3163 3164 static void __f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi, int type) 3165 { 3166 struct curseg_info *curseg = CURSEG_I(sbi, type); 3167 3168 mutex_lock(&curseg->curseg_mutex); 3169 if (!curseg->inited) 3170 goto out; 3171 3172 if (get_valid_blocks(sbi, curseg->segno, false)) { 3173 write_sum_page(sbi, curseg->sum_blk, curseg->segno); 3174 } else { 3175 mutex_lock(&DIRTY_I(sbi)->seglist_lock); 3176 __set_test_and_free(sbi, curseg->segno, true); 3177 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); 3178 } 3179 out: 3180 mutex_unlock(&curseg->curseg_mutex); 3181 } 3182 3183 void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi) 3184 { 3185 __f2fs_save_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); 3186 3187 if (sbi->am.atgc_enabled) 3188 __f2fs_save_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); 3189 } 3190 3191 static void __f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi, int type) 3192 { 3193 struct curseg_info *curseg = CURSEG_I(sbi, type); 3194 3195 mutex_lock(&curseg->curseg_mutex); 3196 if (!curseg->inited) 3197 goto out; 3198 if (get_valid_blocks(sbi, curseg->segno, false)) 3199 goto out; 3200 3201 mutex_lock(&DIRTY_I(sbi)->seglist_lock); 3202 __set_test_and_inuse(sbi, curseg->segno); 3203 mutex_unlock(&DIRTY_I(sbi)->seglist_lock); 3204 out: 3205 mutex_unlock(&curseg->curseg_mutex); 3206 } 3207 3208 void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi) 3209 { 3210 __f2fs_restore_inmem_curseg(sbi, CURSEG_COLD_DATA_PINNED); 3211 3212 if (sbi->am.atgc_enabled) 3213 __f2fs_restore_inmem_curseg(sbi, CURSEG_ALL_DATA_ATGC); 3214 } 3215 3216 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type, 3217 int alloc_mode, unsigned long long age) 3218 { 3219 struct curseg_info *curseg = CURSEG_I(sbi, type); 3220 unsigned segno = NULL_SEGNO; 3221 unsigned short seg_type = curseg->seg_type; 3222 int i, cnt; 3223 bool reversed = false; 3224 3225 sanity_check_seg_type(sbi, seg_type); 3226 3227 /* f2fs_need_SSR() already forces to do this */ 3228 if (!f2fs_get_victim(sbi, &segno, BG_GC, seg_type, 3229 alloc_mode, age, false)) { 3230 curseg->next_segno = segno; 3231 return 1; 3232 } 3233 3234 /* For node segments, let's do SSR more intensively */ 3235 if (IS_NODESEG(seg_type)) { 3236 if (seg_type >= CURSEG_WARM_NODE) { 3237 reversed = true; 3238 i = CURSEG_COLD_NODE; 3239 } else { 3240 i = CURSEG_HOT_NODE; 3241 } 3242 cnt = NR_CURSEG_NODE_TYPE; 3243 } else { 3244 if (seg_type >= CURSEG_WARM_DATA) { 3245 reversed = true; 3246 i = CURSEG_COLD_DATA; 3247 } else { 3248 i = CURSEG_HOT_DATA; 3249 } 3250 cnt = NR_CURSEG_DATA_TYPE; 3251 } 3252 3253 for (; cnt-- > 0; reversed ? i-- : i++) { 3254 if (i == seg_type) 3255 continue; 3256 if (!f2fs_get_victim(sbi, &segno, BG_GC, i, 3257 alloc_mode, age, false)) { 3258 curseg->next_segno = segno; 3259 return 1; 3260 } 3261 } 3262 3263 /* find valid_blocks=0 in dirty list */ 3264 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) { 3265 segno = get_free_segment(sbi); 3266 if (segno != NULL_SEGNO) { 3267 curseg->next_segno = segno; 3268 return 1; 3269 } 3270 } 3271 return 0; 3272 } 3273 3274 static bool need_new_seg(struct f2fs_sb_info *sbi, int type) 3275 { 3276 struct curseg_info *curseg = CURSEG_I(sbi, type); 3277 3278 if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && 3279 curseg->seg_type == CURSEG_WARM_NODE) 3280 return true; 3281 if (curseg->alloc_type == LFS && is_next_segment_free(sbi, curseg) && 3282 likely(!is_sbi_flag_set(sbi, SBI_CP_DISABLED))) 3283 return true; 3284 if (!f2fs_need_SSR(sbi) || !get_ssr_segment(sbi, type, SSR, 0)) 3285 return true; 3286 return false; 3287 } 3288 3289 int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type, 3290 unsigned int start, unsigned int end) 3291 { 3292 struct curseg_info *curseg = CURSEG_I(sbi, type); 3293 unsigned int segno; 3294 int ret = 0; 3295 3296 f2fs_down_read(&SM_I(sbi)->curseg_lock); 3297 mutex_lock(&curseg->curseg_mutex); 3298 down_write(&SIT_I(sbi)->sentry_lock); 3299 3300 segno = CURSEG_I(sbi, type)->segno; 3301 if (segno < start || segno > end) 3302 goto unlock; 3303 3304 if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type, SSR, 0)) 3305 ret = change_curseg(sbi, type); 3306 else 3307 ret = new_curseg(sbi, type, true); 3308 3309 stat_inc_seg_type(sbi, curseg); 3310 3311 locate_dirty_segment(sbi, segno); 3312 unlock: 3313 up_write(&SIT_I(sbi)->sentry_lock); 3314 3315 if (segno != curseg->segno) 3316 f2fs_notice(sbi, "For resize: curseg of type %d: %u ==> %u", 3317 type, segno, curseg->segno); 3318 3319 mutex_unlock(&curseg->curseg_mutex); 3320 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3321 return ret; 3322 } 3323 3324 static int __allocate_new_segment(struct f2fs_sb_info *sbi, int type, 3325 bool new_sec, bool force) 3326 { 3327 struct curseg_info *curseg = CURSEG_I(sbi, type); 3328 unsigned int old_segno; 3329 int err = 0; 3330 3331 if (type == CURSEG_COLD_DATA_PINNED && !curseg->inited) 3332 goto allocate; 3333 3334 if (!force && curseg->inited && 3335 !curseg->next_blkoff && 3336 !get_valid_blocks(sbi, curseg->segno, new_sec) && 3337 !get_ckpt_valid_blocks(sbi, curseg->segno, new_sec)) 3338 return 0; 3339 3340 allocate: 3341 old_segno = curseg->segno; 3342 err = new_curseg(sbi, type, true); 3343 if (err) 3344 return err; 3345 stat_inc_seg_type(sbi, curseg); 3346 locate_dirty_segment(sbi, old_segno); 3347 return 0; 3348 } 3349 3350 int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force) 3351 { 3352 int ret; 3353 3354 f2fs_down_read(&SM_I(sbi)->curseg_lock); 3355 down_write(&SIT_I(sbi)->sentry_lock); 3356 ret = __allocate_new_segment(sbi, type, true, force); 3357 up_write(&SIT_I(sbi)->sentry_lock); 3358 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3359 3360 return ret; 3361 } 3362 3363 int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi) 3364 { 3365 int err; 3366 bool gc_required = true; 3367 3368 retry: 3369 f2fs_lock_op(sbi); 3370 err = f2fs_allocate_new_section(sbi, CURSEG_COLD_DATA_PINNED, false); 3371 f2fs_unlock_op(sbi); 3372 3373 if (f2fs_sb_has_blkzoned(sbi) && err == -EAGAIN && gc_required) { 3374 f2fs_down_write(&sbi->gc_lock); 3375 err = f2fs_gc_range(sbi, 0, sbi->first_seq_zone_segno - 1, 3376 true, ZONED_PIN_SEC_REQUIRED_COUNT); 3377 f2fs_up_write(&sbi->gc_lock); 3378 3379 gc_required = false; 3380 if (!err) 3381 goto retry; 3382 } 3383 3384 return err; 3385 } 3386 3387 int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi) 3388 { 3389 int i; 3390 int err = 0; 3391 3392 f2fs_down_read(&SM_I(sbi)->curseg_lock); 3393 down_write(&SIT_I(sbi)->sentry_lock); 3394 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) 3395 err += __allocate_new_segment(sbi, i, false, false); 3396 up_write(&SIT_I(sbi)->sentry_lock); 3397 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3398 3399 return err; 3400 } 3401 3402 bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi, 3403 struct cp_control *cpc) 3404 { 3405 __u64 trim_start = cpc->trim_start; 3406 bool has_candidate = false; 3407 3408 down_write(&SIT_I(sbi)->sentry_lock); 3409 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) { 3410 if (add_discard_addrs(sbi, cpc, true)) { 3411 has_candidate = true; 3412 break; 3413 } 3414 } 3415 up_write(&SIT_I(sbi)->sentry_lock); 3416 3417 cpc->trim_start = trim_start; 3418 return has_candidate; 3419 } 3420 3421 static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi, 3422 struct discard_policy *dpolicy, 3423 unsigned int start, unsigned int end) 3424 { 3425 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 3426 struct discard_cmd *prev_dc = NULL, *next_dc = NULL; 3427 struct rb_node **insert_p = NULL, *insert_parent = NULL; 3428 struct discard_cmd *dc; 3429 struct blk_plug plug; 3430 int issued; 3431 unsigned int trimmed = 0; 3432 3433 next: 3434 issued = 0; 3435 3436 mutex_lock(&dcc->cmd_lock); 3437 if (unlikely(dcc->rbtree_check)) 3438 f2fs_bug_on(sbi, !f2fs_check_discard_tree(sbi)); 3439 3440 dc = __lookup_discard_cmd_ret(&dcc->root, start, 3441 &prev_dc, &next_dc, &insert_p, &insert_parent); 3442 if (!dc) 3443 dc = next_dc; 3444 3445 blk_start_plug(&plug); 3446 3447 while (dc && dc->di.lstart <= end) { 3448 struct rb_node *node; 3449 int err = 0; 3450 3451 if (dc->di.len < dpolicy->granularity) 3452 goto skip; 3453 3454 if (dc->state != D_PREP) { 3455 list_move_tail(&dc->list, &dcc->fstrim_list); 3456 goto skip; 3457 } 3458 3459 err = __submit_discard_cmd(sbi, dpolicy, dc, &issued); 3460 3461 if (issued >= dpolicy->max_requests) { 3462 start = dc->di.lstart + dc->di.len; 3463 3464 if (err) 3465 __remove_discard_cmd(sbi, dc); 3466 3467 blk_finish_plug(&plug); 3468 mutex_unlock(&dcc->cmd_lock); 3469 trimmed += __wait_all_discard_cmd(sbi, NULL); 3470 f2fs_schedule_timeout(DEFAULT_DISCARD_INTERVAL); 3471 goto next; 3472 } 3473 skip: 3474 node = rb_next(&dc->rb_node); 3475 if (err) 3476 __remove_discard_cmd(sbi, dc); 3477 dc = rb_entry_safe(node, struct discard_cmd, rb_node); 3478 3479 if (fatal_signal_pending(current)) 3480 break; 3481 } 3482 3483 blk_finish_plug(&plug); 3484 mutex_unlock(&dcc->cmd_lock); 3485 3486 return trimmed; 3487 } 3488 3489 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) 3490 { 3491 __u64 start = F2FS_BYTES_TO_BLK(range->start); 3492 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; 3493 unsigned int start_segno, end_segno; 3494 block_t start_block, end_block; 3495 struct cp_control cpc; 3496 struct discard_policy dpolicy; 3497 unsigned long long trimmed = 0; 3498 int err = 0; 3499 bool need_align = f2fs_lfs_mode(sbi) && __is_large_section(sbi); 3500 3501 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) 3502 return -EINVAL; 3503 3504 if (end < MAIN_BLKADDR(sbi)) 3505 goto out; 3506 3507 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { 3508 f2fs_warn(sbi, "Found FS corruption, run fsck to fix."); 3509 return -EFSCORRUPTED; 3510 } 3511 3512 /* start/end segment number in main_area */ 3513 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); 3514 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : 3515 GET_SEGNO(sbi, end); 3516 if (need_align) { 3517 start_segno = rounddown(start_segno, SEGS_PER_SEC(sbi)); 3518 end_segno = roundup(end_segno + 1, SEGS_PER_SEC(sbi)) - 1; 3519 } 3520 3521 cpc.reason = CP_DISCARD; 3522 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen)); 3523 cpc.trim_start = start_segno; 3524 cpc.trim_end = end_segno; 3525 3526 if (sbi->discard_blks == 0) 3527 goto out; 3528 3529 f2fs_down_write(&sbi->gc_lock); 3530 stat_inc_cp_call_count(sbi, TOTAL_CALL); 3531 err = f2fs_write_checkpoint(sbi, &cpc); 3532 f2fs_up_write(&sbi->gc_lock); 3533 if (err) 3534 goto out; 3535 3536 /* 3537 * We filed discard candidates, but actually we don't need to wait for 3538 * all of them, since they'll be issued in idle time along with runtime 3539 * discard option. User configuration looks like using runtime discard 3540 * or periodic fstrim instead of it. 3541 */ 3542 if (f2fs_realtime_discard_enable(sbi)) 3543 goto out; 3544 3545 start_block = START_BLOCK(sbi, start_segno); 3546 end_block = START_BLOCK(sbi, end_segno + 1); 3547 3548 __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen); 3549 trimmed = __issue_discard_cmd_range(sbi, &dpolicy, 3550 start_block, end_block); 3551 3552 trimmed += __wait_discard_cmd_range(sbi, &dpolicy, 3553 start_block, end_block); 3554 out: 3555 if (!err) 3556 range->len = F2FS_BLK_TO_BYTES(trimmed); 3557 return err; 3558 } 3559 3560 int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint) 3561 { 3562 if (F2FS_OPTION(sbi).active_logs == 2) 3563 return CURSEG_HOT_DATA; 3564 else if (F2FS_OPTION(sbi).active_logs == 4) 3565 return CURSEG_COLD_DATA; 3566 3567 /* active_log == 6 */ 3568 switch (hint) { 3569 case WRITE_LIFE_SHORT: 3570 return CURSEG_HOT_DATA; 3571 case WRITE_LIFE_EXTREME: 3572 return CURSEG_COLD_DATA; 3573 default: 3574 return CURSEG_WARM_DATA; 3575 } 3576 } 3577 3578 /* 3579 * This returns write hints for each segment type. This hints will be 3580 * passed down to block layer as below by default. 3581 * 3582 * User F2FS Block 3583 * ---- ---- ----- 3584 * META WRITE_LIFE_NONE|REQ_META 3585 * HOT_NODE WRITE_LIFE_NONE 3586 * WARM_NODE WRITE_LIFE_MEDIUM 3587 * COLD_NODE WRITE_LIFE_LONG 3588 * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME 3589 * extension list " " 3590 * 3591 * -- buffered io 3592 * COLD_DATA WRITE_LIFE_EXTREME 3593 * HOT_DATA WRITE_LIFE_SHORT 3594 * WARM_DATA WRITE_LIFE_NOT_SET 3595 * 3596 * -- direct io 3597 * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME 3598 * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT 3599 * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET 3600 * WRITE_LIFE_NONE " WRITE_LIFE_NONE 3601 * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM 3602 * WRITE_LIFE_LONG " WRITE_LIFE_LONG 3603 */ 3604 enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi, 3605 enum page_type type, enum temp_type temp) 3606 { 3607 switch (type) { 3608 case DATA: 3609 switch (temp) { 3610 case WARM: 3611 return WRITE_LIFE_NOT_SET; 3612 case HOT: 3613 return WRITE_LIFE_SHORT; 3614 case COLD: 3615 return WRITE_LIFE_EXTREME; 3616 default: 3617 return WRITE_LIFE_NONE; 3618 } 3619 case NODE: 3620 switch (temp) { 3621 case WARM: 3622 return WRITE_LIFE_MEDIUM; 3623 case HOT: 3624 return WRITE_LIFE_NONE; 3625 case COLD: 3626 return WRITE_LIFE_LONG; 3627 default: 3628 return WRITE_LIFE_NONE; 3629 } 3630 case META: 3631 return WRITE_LIFE_NONE; 3632 default: 3633 return WRITE_LIFE_NONE; 3634 } 3635 } 3636 3637 static int __get_segment_type_2(struct f2fs_io_info *fio) 3638 { 3639 if (fio->type == DATA) 3640 return CURSEG_HOT_DATA; 3641 else 3642 return CURSEG_HOT_NODE; 3643 } 3644 3645 static int __get_segment_type_4(struct f2fs_io_info *fio) 3646 { 3647 if (fio->type == DATA) { 3648 struct inode *inode = fio_inode(fio); 3649 3650 if (S_ISDIR(inode->i_mode)) 3651 return CURSEG_HOT_DATA; 3652 else 3653 return CURSEG_COLD_DATA; 3654 } else { 3655 if (IS_DNODE(fio->folio) && is_cold_node(fio->folio)) 3656 return CURSEG_WARM_NODE; 3657 else 3658 return CURSEG_COLD_NODE; 3659 } 3660 } 3661 3662 static int __get_age_segment_type(struct inode *inode, pgoff_t pgofs) 3663 { 3664 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 3665 struct extent_info ei = {}; 3666 3667 if (f2fs_lookup_age_extent_cache(inode, pgofs, &ei)) { 3668 if (!ei.age) 3669 return NO_CHECK_TYPE; 3670 if (ei.age <= sbi->hot_data_age_threshold) 3671 return CURSEG_HOT_DATA; 3672 if (ei.age <= sbi->warm_data_age_threshold) 3673 return CURSEG_WARM_DATA; 3674 return CURSEG_COLD_DATA; 3675 } 3676 return NO_CHECK_TYPE; 3677 } 3678 3679 static int __get_segment_type_6(struct f2fs_io_info *fio) 3680 { 3681 if (fio->type == DATA) { 3682 struct inode *inode = fio_inode(fio); 3683 int type; 3684 3685 if (is_inode_flag_set(inode, FI_ALIGNED_WRITE)) 3686 return CURSEG_COLD_DATA_PINNED; 3687 3688 if (page_private_gcing(fio->page)) { 3689 if (fio->sbi->am.atgc_enabled && 3690 (fio->io_type == FS_DATA_IO) && 3691 (fio->sbi->gc_mode != GC_URGENT_HIGH) && 3692 __is_valid_data_blkaddr(fio->old_blkaddr) && 3693 !is_inode_flag_set(inode, FI_OPU_WRITE)) 3694 return CURSEG_ALL_DATA_ATGC; 3695 else 3696 return CURSEG_COLD_DATA; 3697 } 3698 if (file_is_cold(inode) || f2fs_need_compress_data(inode)) 3699 return CURSEG_COLD_DATA; 3700 3701 type = __get_age_segment_type(inode, fio->folio->index); 3702 if (type != NO_CHECK_TYPE) 3703 return type; 3704 3705 if (file_is_hot(inode) || 3706 is_inode_flag_set(inode, FI_HOT_DATA) || 3707 f2fs_is_cow_file(inode) || 3708 is_inode_flag_set(inode, FI_NEED_IPU)) 3709 return CURSEG_HOT_DATA; 3710 return f2fs_rw_hint_to_seg_type(F2FS_I_SB(inode), 3711 inode->i_write_hint); 3712 } else { 3713 if (IS_DNODE(fio->folio)) 3714 return is_cold_node(fio->folio) ? CURSEG_WARM_NODE : 3715 CURSEG_HOT_NODE; 3716 return CURSEG_COLD_NODE; 3717 } 3718 } 3719 3720 enum temp_type f2fs_get_segment_temp(struct f2fs_sb_info *sbi, 3721 enum log_type type) 3722 { 3723 struct curseg_info *curseg = CURSEG_I(sbi, type); 3724 enum temp_type temp = COLD; 3725 3726 switch (curseg->seg_type) { 3727 case CURSEG_HOT_NODE: 3728 case CURSEG_HOT_DATA: 3729 temp = HOT; 3730 break; 3731 case CURSEG_WARM_NODE: 3732 case CURSEG_WARM_DATA: 3733 temp = WARM; 3734 break; 3735 case CURSEG_COLD_NODE: 3736 case CURSEG_COLD_DATA: 3737 temp = COLD; 3738 break; 3739 default: 3740 f2fs_bug_on(sbi, 1); 3741 } 3742 3743 return temp; 3744 } 3745 3746 static int __get_segment_type(struct f2fs_io_info *fio) 3747 { 3748 enum log_type type = CURSEG_HOT_DATA; 3749 3750 switch (F2FS_OPTION(fio->sbi).active_logs) { 3751 case 2: 3752 type = __get_segment_type_2(fio); 3753 break; 3754 case 4: 3755 type = __get_segment_type_4(fio); 3756 break; 3757 case 6: 3758 type = __get_segment_type_6(fio); 3759 break; 3760 default: 3761 f2fs_bug_on(fio->sbi, true); 3762 } 3763 3764 fio->temp = f2fs_get_segment_temp(fio->sbi, type); 3765 3766 return type; 3767 } 3768 3769 static void f2fs_randomize_chunk(struct f2fs_sb_info *sbi, 3770 struct curseg_info *seg) 3771 { 3772 /* To allocate block chunks in different sizes, use random number */ 3773 if (--seg->fragment_remained_chunk > 0) 3774 return; 3775 3776 seg->fragment_remained_chunk = 3777 get_random_u32_inclusive(1, sbi->max_fragment_chunk); 3778 seg->next_blkoff += 3779 get_random_u32_inclusive(1, sbi->max_fragment_hole); 3780 } 3781 3782 int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio, 3783 block_t old_blkaddr, block_t *new_blkaddr, 3784 struct f2fs_summary *sum, int type, 3785 struct f2fs_io_info *fio) 3786 { 3787 struct sit_info *sit_i = SIT_I(sbi); 3788 struct curseg_info *curseg = CURSEG_I(sbi, type); 3789 unsigned long long old_mtime; 3790 bool from_gc = (type == CURSEG_ALL_DATA_ATGC); 3791 struct seg_entry *se = NULL; 3792 bool segment_full = false; 3793 int ret = 0; 3794 3795 f2fs_down_read(&SM_I(sbi)->curseg_lock); 3796 3797 mutex_lock(&curseg->curseg_mutex); 3798 down_write(&sit_i->sentry_lock); 3799 3800 if (curseg->segno == NULL_SEGNO) { 3801 ret = -ENOSPC; 3802 goto out_err; 3803 } 3804 3805 if (from_gc) { 3806 f2fs_bug_on(sbi, GET_SEGNO(sbi, old_blkaddr) == NULL_SEGNO); 3807 se = get_seg_entry(sbi, GET_SEGNO(sbi, old_blkaddr)); 3808 sanity_check_seg_type(sbi, se->type); 3809 f2fs_bug_on(sbi, IS_NODESEG(se->type)); 3810 } 3811 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); 3812 3813 f2fs_bug_on(sbi, curseg->next_blkoff >= BLKS_PER_SEG(sbi)); 3814 3815 f2fs_wait_discard_bio(sbi, *new_blkaddr); 3816 3817 curseg->sum_blk->entries[curseg->next_blkoff] = *sum; 3818 if (curseg->alloc_type == SSR) { 3819 curseg->next_blkoff = f2fs_find_next_ssr_block(sbi, curseg); 3820 } else { 3821 curseg->next_blkoff++; 3822 if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK) 3823 f2fs_randomize_chunk(sbi, curseg); 3824 } 3825 if (curseg->next_blkoff >= f2fs_usable_blks_in_seg(sbi, curseg->segno)) 3826 segment_full = true; 3827 stat_inc_block_count(sbi, curseg); 3828 3829 if (from_gc) { 3830 old_mtime = get_segment_mtime(sbi, old_blkaddr); 3831 } else { 3832 update_segment_mtime(sbi, old_blkaddr, 0); 3833 old_mtime = 0; 3834 } 3835 update_segment_mtime(sbi, *new_blkaddr, old_mtime); 3836 3837 /* 3838 * SIT information should be updated before segment allocation, 3839 * since SSR needs latest valid block information. 3840 */ 3841 update_sit_entry(sbi, *new_blkaddr, 1); 3842 update_sit_entry(sbi, old_blkaddr, -1); 3843 3844 /* 3845 * If the current segment is full, flush it out and replace it with a 3846 * new segment. 3847 */ 3848 if (segment_full) { 3849 if (type == CURSEG_COLD_DATA_PINNED && 3850 !((curseg->segno + 1) % sbi->segs_per_sec)) { 3851 write_sum_page(sbi, curseg->sum_blk, curseg->segno); 3852 reset_curseg_fields(curseg); 3853 goto skip_new_segment; 3854 } 3855 3856 if (from_gc) { 3857 ret = get_atssr_segment(sbi, type, se->type, 3858 AT_SSR, se->mtime); 3859 } else { 3860 if (need_new_seg(sbi, type)) 3861 ret = new_curseg(sbi, type, false); 3862 else 3863 ret = change_curseg(sbi, type); 3864 stat_inc_seg_type(sbi, curseg); 3865 } 3866 3867 if (ret) 3868 goto out_err; 3869 } 3870 3871 skip_new_segment: 3872 /* 3873 * segment dirty status should be updated after segment allocation, 3874 * so we just need to update status only one time after previous 3875 * segment being closed. 3876 */ 3877 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); 3878 locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr)); 3879 3880 if (IS_DATASEG(curseg->seg_type)) { 3881 unsigned long long new_val; 3882 3883 new_val = atomic64_inc_return(&sbi->allocated_data_blocks); 3884 if (unlikely(new_val == ULLONG_MAX)) 3885 atomic64_set(&sbi->allocated_data_blocks, 0); 3886 } 3887 3888 up_write(&sit_i->sentry_lock); 3889 3890 if (folio && IS_NODESEG(curseg->seg_type)) { 3891 fill_node_footer_blkaddr(folio, NEXT_FREE_BLKADDR(sbi, curseg)); 3892 3893 f2fs_inode_chksum_set(sbi, folio); 3894 } 3895 3896 if (fio) { 3897 struct f2fs_bio_info *io; 3898 3899 INIT_LIST_HEAD(&fio->list); 3900 fio->in_list = 1; 3901 io = sbi->write_io[fio->type] + fio->temp; 3902 spin_lock(&io->io_lock); 3903 list_add_tail(&fio->list, &io->io_list); 3904 spin_unlock(&io->io_lock); 3905 } 3906 3907 mutex_unlock(&curseg->curseg_mutex); 3908 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3909 return 0; 3910 3911 out_err: 3912 *new_blkaddr = NULL_ADDR; 3913 up_write(&sit_i->sentry_lock); 3914 mutex_unlock(&curseg->curseg_mutex); 3915 f2fs_up_read(&SM_I(sbi)->curseg_lock); 3916 return ret; 3917 } 3918 3919 void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino, 3920 block_t blkaddr, unsigned int blkcnt) 3921 { 3922 if (!f2fs_is_multi_device(sbi)) 3923 return; 3924 3925 while (1) { 3926 unsigned int devidx = f2fs_target_device_index(sbi, blkaddr); 3927 unsigned int blks = FDEV(devidx).end_blk - blkaddr + 1; 3928 3929 /* update device state for fsync */ 3930 f2fs_set_dirty_device(sbi, ino, devidx, FLUSH_INO); 3931 3932 /* update device state for checkpoint */ 3933 if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) { 3934 spin_lock(&sbi->dev_lock); 3935 f2fs_set_bit(devidx, (char *)&sbi->dirty_device); 3936 spin_unlock(&sbi->dev_lock); 3937 } 3938 3939 if (blkcnt <= blks) 3940 break; 3941 blkcnt -= blks; 3942 blkaddr += blks; 3943 } 3944 } 3945 3946 static int log_type_to_seg_type(enum log_type type) 3947 { 3948 int seg_type = CURSEG_COLD_DATA; 3949 3950 switch (type) { 3951 case CURSEG_HOT_DATA: 3952 case CURSEG_WARM_DATA: 3953 case CURSEG_COLD_DATA: 3954 case CURSEG_HOT_NODE: 3955 case CURSEG_WARM_NODE: 3956 case CURSEG_COLD_NODE: 3957 seg_type = (int)type; 3958 break; 3959 case CURSEG_COLD_DATA_PINNED: 3960 case CURSEG_ALL_DATA_ATGC: 3961 seg_type = CURSEG_COLD_DATA; 3962 break; 3963 default: 3964 break; 3965 } 3966 return seg_type; 3967 } 3968 3969 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio) 3970 { 3971 struct folio *folio = fio->folio; 3972 enum log_type type = __get_segment_type(fio); 3973 int seg_type = log_type_to_seg_type(type); 3974 bool keep_order = (f2fs_lfs_mode(fio->sbi) && 3975 seg_type == CURSEG_COLD_DATA); 3976 int err; 3977 3978 if (keep_order) 3979 f2fs_down_read(&fio->sbi->io_order_lock); 3980 3981 err = f2fs_allocate_data_block(fio->sbi, folio, fio->old_blkaddr, 3982 &fio->new_blkaddr, sum, type, fio); 3983 if (unlikely(err)) { 3984 f2fs_err_ratelimited(fio->sbi, 3985 "%s Failed to allocate data block, ino:%u, index:%lu, type:%d, old_blkaddr:0x%x, new_blkaddr:0x%x, err:%d", 3986 __func__, fio->ino, folio->index, type, 3987 fio->old_blkaddr, fio->new_blkaddr, err); 3988 if (fscrypt_inode_uses_fs_layer_crypto(folio->mapping->host)) 3989 fscrypt_finalize_bounce_page(&fio->encrypted_page); 3990 folio_end_writeback(folio); 3991 if (f2fs_in_warm_node_list(fio->sbi, folio)) 3992 f2fs_del_fsync_node_entry(fio->sbi, folio); 3993 f2fs_bug_on(fio->sbi, !is_set_ckpt_flags(fio->sbi, 3994 CP_ERROR_FLAG)); 3995 goto out; 3996 } 3997 3998 f2fs_bug_on(fio->sbi, !f2fs_is_valid_blkaddr_raw(fio->sbi, 3999 fio->new_blkaddr, DATA_GENERIC_ENHANCE)); 4000 4001 if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO) 4002 f2fs_invalidate_internal_cache(fio->sbi, fio->old_blkaddr, 1); 4003 4004 /* writeout dirty page into bdev */ 4005 f2fs_submit_page_write(fio); 4006 4007 f2fs_update_device_state(fio->sbi, fio->ino, fio->new_blkaddr, 1); 4008 out: 4009 if (keep_order) 4010 f2fs_up_read(&fio->sbi->io_order_lock); 4011 } 4012 4013 void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio, 4014 enum iostat_type io_type) 4015 { 4016 struct f2fs_io_info fio = { 4017 .sbi = sbi, 4018 .type = META, 4019 .temp = HOT, 4020 .op = REQ_OP_WRITE, 4021 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO, 4022 .old_blkaddr = folio->index, 4023 .new_blkaddr = folio->index, 4024 .folio = folio, 4025 .encrypted_page = NULL, 4026 .in_list = 0, 4027 }; 4028 4029 if (unlikely(folio->index >= MAIN_BLKADDR(sbi))) 4030 fio.op_flags &= ~REQ_META; 4031 4032 folio_start_writeback(folio); 4033 f2fs_submit_page_write(&fio); 4034 4035 stat_inc_meta_count(sbi, folio->index); 4036 f2fs_update_iostat(sbi, NULL, io_type, F2FS_BLKSIZE); 4037 } 4038 4039 void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio) 4040 { 4041 struct f2fs_summary sum; 4042 4043 set_summary(&sum, nid, 0, 0); 4044 do_write_page(&sum, fio); 4045 4046 f2fs_update_iostat(fio->sbi, NULL, fio->io_type, F2FS_BLKSIZE); 4047 } 4048 4049 void f2fs_outplace_write_data(struct dnode_of_data *dn, 4050 struct f2fs_io_info *fio) 4051 { 4052 struct f2fs_sb_info *sbi = fio->sbi; 4053 struct f2fs_summary sum; 4054 4055 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); 4056 if (fio->io_type == FS_DATA_IO || fio->io_type == FS_CP_DATA_IO) 4057 f2fs_update_age_extent_cache(dn); 4058 set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version); 4059 do_write_page(&sum, fio); 4060 f2fs_update_data_blkaddr(dn, fio->new_blkaddr); 4061 4062 f2fs_update_iostat(sbi, dn->inode, fio->io_type, F2FS_BLKSIZE); 4063 } 4064 4065 int f2fs_inplace_write_data(struct f2fs_io_info *fio) 4066 { 4067 int err; 4068 struct f2fs_sb_info *sbi = fio->sbi; 4069 unsigned int segno; 4070 4071 fio->new_blkaddr = fio->old_blkaddr; 4072 /* i/o temperature is needed for passing down write hints */ 4073 __get_segment_type(fio); 4074 4075 segno = GET_SEGNO(sbi, fio->new_blkaddr); 4076 4077 if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) { 4078 set_sbi_flag(sbi, SBI_NEED_FSCK); 4079 f2fs_warn(sbi, "%s: incorrect segment(%u) type, run fsck to fix.", 4080 __func__, segno); 4081 err = -EFSCORRUPTED; 4082 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE); 4083 goto drop_bio; 4084 } 4085 4086 if (f2fs_cp_error(sbi)) { 4087 err = -EIO; 4088 goto drop_bio; 4089 } 4090 4091 if (fio->meta_gc) 4092 f2fs_truncate_meta_inode_pages(sbi, fio->new_blkaddr, 1); 4093 4094 stat_inc_inplace_blocks(fio->sbi); 4095 4096 if (fio->bio && !IS_F2FS_IPU_NOCACHE(sbi)) 4097 err = f2fs_merge_page_bio(fio); 4098 else 4099 err = f2fs_submit_page_bio(fio); 4100 if (!err) { 4101 f2fs_update_device_state(fio->sbi, fio->ino, 4102 fio->new_blkaddr, 1); 4103 f2fs_update_iostat(fio->sbi, fio_inode(fio), 4104 fio->io_type, F2FS_BLKSIZE); 4105 } 4106 4107 return err; 4108 drop_bio: 4109 if (fio->bio && *(fio->bio)) { 4110 struct bio *bio = *(fio->bio); 4111 4112 bio->bi_status = BLK_STS_IOERR; 4113 bio_endio(bio); 4114 *(fio->bio) = NULL; 4115 } 4116 return err; 4117 } 4118 4119 static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi, 4120 unsigned int segno) 4121 { 4122 int i; 4123 4124 for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) { 4125 if (CURSEG_I(sbi, i)->segno == segno) 4126 break; 4127 } 4128 return i; 4129 } 4130 4131 void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 4132 block_t old_blkaddr, block_t new_blkaddr, 4133 bool recover_curseg, bool recover_newaddr, 4134 bool from_gc) 4135 { 4136 struct sit_info *sit_i = SIT_I(sbi); 4137 struct curseg_info *curseg; 4138 unsigned int segno, old_cursegno; 4139 struct seg_entry *se; 4140 int type; 4141 unsigned short old_blkoff; 4142 unsigned char old_alloc_type; 4143 4144 segno = GET_SEGNO(sbi, new_blkaddr); 4145 se = get_seg_entry(sbi, segno); 4146 type = se->type; 4147 4148 f2fs_down_write(&SM_I(sbi)->curseg_lock); 4149 4150 if (!recover_curseg) { 4151 /* for recovery flow */ 4152 if (se->valid_blocks == 0 && !is_curseg(sbi, segno)) { 4153 if (old_blkaddr == NULL_ADDR) 4154 type = CURSEG_COLD_DATA; 4155 else 4156 type = CURSEG_WARM_DATA; 4157 } 4158 } else { 4159 if (is_curseg(sbi, segno)) { 4160 /* se->type is volatile as SSR allocation */ 4161 type = __f2fs_get_curseg(sbi, segno); 4162 f2fs_bug_on(sbi, type == NO_CHECK_TYPE); 4163 } else { 4164 type = CURSEG_WARM_DATA; 4165 } 4166 } 4167 4168 curseg = CURSEG_I(sbi, type); 4169 f2fs_bug_on(sbi, !IS_DATASEG(curseg->seg_type)); 4170 4171 mutex_lock(&curseg->curseg_mutex); 4172 down_write(&sit_i->sentry_lock); 4173 4174 old_cursegno = curseg->segno; 4175 old_blkoff = curseg->next_blkoff; 4176 old_alloc_type = curseg->alloc_type; 4177 4178 /* change the current segment */ 4179 if (segno != curseg->segno) { 4180 curseg->next_segno = segno; 4181 if (change_curseg(sbi, type)) 4182 goto out_unlock; 4183 } 4184 4185 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); 4186 curseg->sum_blk->entries[curseg->next_blkoff] = *sum; 4187 4188 if (!recover_curseg || recover_newaddr) { 4189 if (!from_gc) 4190 update_segment_mtime(sbi, new_blkaddr, 0); 4191 update_sit_entry(sbi, new_blkaddr, 1); 4192 } 4193 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) { 4194 f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1); 4195 if (!from_gc) 4196 update_segment_mtime(sbi, old_blkaddr, 0); 4197 update_sit_entry(sbi, old_blkaddr, -1); 4198 } 4199 4200 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); 4201 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); 4202 4203 locate_dirty_segment(sbi, old_cursegno); 4204 4205 if (recover_curseg) { 4206 if (old_cursegno != curseg->segno) { 4207 curseg->next_segno = old_cursegno; 4208 if (change_curseg(sbi, type)) 4209 goto out_unlock; 4210 } 4211 curseg->next_blkoff = old_blkoff; 4212 curseg->alloc_type = old_alloc_type; 4213 } 4214 4215 out_unlock: 4216 up_write(&sit_i->sentry_lock); 4217 mutex_unlock(&curseg->curseg_mutex); 4218 f2fs_up_write(&SM_I(sbi)->curseg_lock); 4219 } 4220 4221 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, 4222 block_t old_addr, block_t new_addr, 4223 unsigned char version, bool recover_curseg, 4224 bool recover_newaddr) 4225 { 4226 struct f2fs_summary sum; 4227 4228 set_summary(&sum, dn->nid, dn->ofs_in_node, version); 4229 4230 f2fs_do_replace_block(sbi, &sum, old_addr, new_addr, 4231 recover_curseg, recover_newaddr, false); 4232 4233 f2fs_update_data_blkaddr(dn, new_addr); 4234 } 4235 4236 void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type, 4237 bool ordered, bool locked) 4238 { 4239 if (folio_test_writeback(folio)) { 4240 struct f2fs_sb_info *sbi = F2FS_F_SB(folio); 4241 4242 /* submit cached LFS IO */ 4243 f2fs_submit_merged_write_cond(sbi, NULL, folio, 0, type); 4244 /* submit cached IPU IO */ 4245 f2fs_submit_merged_ipu_write(sbi, NULL, folio); 4246 if (ordered) { 4247 folio_wait_writeback(folio); 4248 f2fs_bug_on(sbi, locked && folio_test_writeback(folio)); 4249 } else { 4250 folio_wait_stable(folio); 4251 } 4252 } 4253 } 4254 4255 void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr) 4256 { 4257 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4258 struct folio *cfolio; 4259 4260 if (!f2fs_meta_inode_gc_required(inode)) 4261 return; 4262 4263 if (!__is_valid_data_blkaddr(blkaddr)) 4264 return; 4265 4266 cfolio = filemap_lock_folio(META_MAPPING(sbi), blkaddr); 4267 if (!IS_ERR(cfolio)) { 4268 f2fs_folio_wait_writeback(cfolio, DATA, true, true); 4269 f2fs_folio_put(cfolio, true); 4270 } 4271 } 4272 4273 void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr, 4274 block_t len) 4275 { 4276 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 4277 block_t i; 4278 4279 if (!f2fs_meta_inode_gc_required(inode)) 4280 return; 4281 4282 for (i = 0; i < len; i++) 4283 f2fs_wait_on_block_writeback(inode, blkaddr + i); 4284 4285 f2fs_truncate_meta_inode_pages(sbi, blkaddr, len); 4286 } 4287 4288 static int read_compacted_summaries(struct f2fs_sb_info *sbi) 4289 { 4290 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 4291 struct curseg_info *seg_i; 4292 unsigned char *kaddr; 4293 struct folio *folio; 4294 block_t start; 4295 int i, j, offset; 4296 4297 start = start_sum_block(sbi); 4298 4299 folio = f2fs_get_meta_folio(sbi, start++); 4300 if (IS_ERR(folio)) 4301 return PTR_ERR(folio); 4302 kaddr = folio_address(folio); 4303 4304 /* Step 1: restore nat cache */ 4305 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 4306 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE); 4307 4308 /* Step 2: restore sit cache */ 4309 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 4310 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); 4311 offset = 2 * SUM_JOURNAL_SIZE; 4312 4313 /* Step 3: restore summary entries */ 4314 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 4315 unsigned short blk_off; 4316 unsigned int segno; 4317 4318 seg_i = CURSEG_I(sbi, i); 4319 segno = le32_to_cpu(ckpt->cur_data_segno[i]); 4320 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); 4321 seg_i->next_segno = segno; 4322 reset_curseg(sbi, i, 0); 4323 seg_i->alloc_type = ckpt->alloc_type[i]; 4324 seg_i->next_blkoff = blk_off; 4325 4326 if (seg_i->alloc_type == SSR) 4327 blk_off = BLKS_PER_SEG(sbi); 4328 4329 for (j = 0; j < blk_off; j++) { 4330 struct f2fs_summary *s; 4331 4332 s = (struct f2fs_summary *)(kaddr + offset); 4333 seg_i->sum_blk->entries[j] = *s; 4334 offset += SUMMARY_SIZE; 4335 if (offset + SUMMARY_SIZE <= PAGE_SIZE - 4336 SUM_FOOTER_SIZE) 4337 continue; 4338 4339 f2fs_folio_put(folio, true); 4340 4341 folio = f2fs_get_meta_folio(sbi, start++); 4342 if (IS_ERR(folio)) 4343 return PTR_ERR(folio); 4344 kaddr = folio_address(folio); 4345 offset = 0; 4346 } 4347 } 4348 f2fs_folio_put(folio, true); 4349 return 0; 4350 } 4351 4352 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) 4353 { 4354 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 4355 struct f2fs_summary_block *sum; 4356 struct curseg_info *curseg; 4357 struct folio *new; 4358 unsigned short blk_off; 4359 unsigned int segno = 0; 4360 block_t blk_addr = 0; 4361 int err = 0; 4362 4363 /* get segment number and block addr */ 4364 if (IS_DATASEG(type)) { 4365 segno = le32_to_cpu(ckpt->cur_data_segno[type]); 4366 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - 4367 CURSEG_HOT_DATA]); 4368 if (__exist_node_summaries(sbi)) 4369 blk_addr = sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type); 4370 else 4371 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 4372 } else { 4373 segno = le32_to_cpu(ckpt->cur_node_segno[type - 4374 CURSEG_HOT_NODE]); 4375 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - 4376 CURSEG_HOT_NODE]); 4377 if (__exist_node_summaries(sbi)) 4378 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, 4379 type - CURSEG_HOT_NODE); 4380 else 4381 blk_addr = GET_SUM_BLOCK(sbi, segno); 4382 } 4383 4384 new = f2fs_get_meta_folio(sbi, blk_addr); 4385 if (IS_ERR(new)) 4386 return PTR_ERR(new); 4387 sum = folio_address(new); 4388 4389 if (IS_NODESEG(type)) { 4390 if (__exist_node_summaries(sbi)) { 4391 struct f2fs_summary *ns = &sum->entries[0]; 4392 int i; 4393 4394 for (i = 0; i < BLKS_PER_SEG(sbi); i++, ns++) { 4395 ns->version = 0; 4396 ns->ofs_in_node = 0; 4397 } 4398 } else { 4399 err = f2fs_restore_node_summary(sbi, segno, sum); 4400 if (err) 4401 goto out; 4402 } 4403 } 4404 4405 /* set uncompleted segment to curseg */ 4406 curseg = CURSEG_I(sbi, type); 4407 mutex_lock(&curseg->curseg_mutex); 4408 4409 /* update journal info */ 4410 down_write(&curseg->journal_rwsem); 4411 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE); 4412 up_write(&curseg->journal_rwsem); 4413 4414 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE); 4415 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE); 4416 curseg->next_segno = segno; 4417 reset_curseg(sbi, type, 0); 4418 curseg->alloc_type = ckpt->alloc_type[type]; 4419 curseg->next_blkoff = blk_off; 4420 mutex_unlock(&curseg->curseg_mutex); 4421 out: 4422 f2fs_folio_put(new, true); 4423 return err; 4424 } 4425 4426 static int restore_curseg_summaries(struct f2fs_sb_info *sbi) 4427 { 4428 struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal; 4429 struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal; 4430 int type = CURSEG_HOT_DATA; 4431 int err; 4432 4433 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { 4434 int npages = f2fs_npages_for_summary_flush(sbi, true); 4435 4436 if (npages >= 2) 4437 f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages, 4438 META_CP, true); 4439 4440 /* restore for compacted data summary */ 4441 err = read_compacted_summaries(sbi); 4442 if (err) 4443 return err; 4444 type = CURSEG_HOT_NODE; 4445 } 4446 4447 if (__exist_node_summaries(sbi)) 4448 f2fs_ra_meta_pages(sbi, 4449 sum_blk_addr(sbi, NR_CURSEG_PERSIST_TYPE, type), 4450 NR_CURSEG_PERSIST_TYPE - type, META_CP, true); 4451 4452 for (; type <= CURSEG_COLD_NODE; type++) { 4453 err = read_normal_summaries(sbi, type); 4454 if (err) 4455 return err; 4456 } 4457 4458 /* sanity check for summary blocks */ 4459 if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES || 4460 sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES) { 4461 f2fs_err(sbi, "invalid journal entries nats %u sits %u", 4462 nats_in_cursum(nat_j), sits_in_cursum(sit_j)); 4463 return -EINVAL; 4464 } 4465 4466 return 0; 4467 } 4468 4469 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) 4470 { 4471 struct folio *folio; 4472 unsigned char *kaddr; 4473 struct f2fs_summary *summary; 4474 struct curseg_info *seg_i; 4475 int written_size = 0; 4476 int i, j; 4477 4478 folio = f2fs_grab_meta_folio(sbi, blkaddr++); 4479 kaddr = folio_address(folio); 4480 memset(kaddr, 0, PAGE_SIZE); 4481 4482 /* Step 1: write nat cache */ 4483 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 4484 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE); 4485 written_size += SUM_JOURNAL_SIZE; 4486 4487 /* Step 2: write sit cache */ 4488 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 4489 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE); 4490 written_size += SUM_JOURNAL_SIZE; 4491 4492 /* Step 3: write summary entries */ 4493 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 4494 seg_i = CURSEG_I(sbi, i); 4495 for (j = 0; j < f2fs_curseg_valid_blocks(sbi, i); j++) { 4496 if (!folio) { 4497 folio = f2fs_grab_meta_folio(sbi, blkaddr++); 4498 kaddr = folio_address(folio); 4499 memset(kaddr, 0, PAGE_SIZE); 4500 written_size = 0; 4501 } 4502 summary = (struct f2fs_summary *)(kaddr + written_size); 4503 *summary = seg_i->sum_blk->entries[j]; 4504 written_size += SUMMARY_SIZE; 4505 4506 if (written_size + SUMMARY_SIZE <= PAGE_SIZE - 4507 SUM_FOOTER_SIZE) 4508 continue; 4509 4510 folio_mark_dirty(folio); 4511 f2fs_folio_put(folio, true); 4512 folio = NULL; 4513 } 4514 } 4515 if (folio) { 4516 folio_mark_dirty(folio); 4517 f2fs_folio_put(folio, true); 4518 } 4519 } 4520 4521 static void write_normal_summaries(struct f2fs_sb_info *sbi, 4522 block_t blkaddr, int type) 4523 { 4524 int i, end; 4525 4526 if (IS_DATASEG(type)) 4527 end = type + NR_CURSEG_DATA_TYPE; 4528 else 4529 end = type + NR_CURSEG_NODE_TYPE; 4530 4531 for (i = type; i < end; i++) 4532 write_current_sum_page(sbi, i, blkaddr + (i - type)); 4533 } 4534 4535 void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 4536 { 4537 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) 4538 write_compacted_summaries(sbi, start_blk); 4539 else 4540 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); 4541 } 4542 4543 void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 4544 { 4545 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); 4546 } 4547 4548 int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type, 4549 unsigned int val, int alloc) 4550 { 4551 int i; 4552 4553 if (type == NAT_JOURNAL) { 4554 for (i = 0; i < nats_in_cursum(journal); i++) { 4555 if (le32_to_cpu(nid_in_journal(journal, i)) == val) 4556 return i; 4557 } 4558 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL)) 4559 return update_nats_in_cursum(journal, 1); 4560 } else if (type == SIT_JOURNAL) { 4561 for (i = 0; i < sits_in_cursum(journal); i++) 4562 if (le32_to_cpu(segno_in_journal(journal, i)) == val) 4563 return i; 4564 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL)) 4565 return update_sits_in_cursum(journal, 1); 4566 } 4567 return -1; 4568 } 4569 4570 static struct folio *get_current_sit_folio(struct f2fs_sb_info *sbi, 4571 unsigned int segno) 4572 { 4573 return f2fs_get_meta_folio(sbi, current_sit_addr(sbi, segno)); 4574 } 4575 4576 static struct folio *get_next_sit_folio(struct f2fs_sb_info *sbi, 4577 unsigned int start) 4578 { 4579 struct sit_info *sit_i = SIT_I(sbi); 4580 struct folio *folio; 4581 pgoff_t src_off, dst_off; 4582 4583 src_off = current_sit_addr(sbi, start); 4584 dst_off = next_sit_addr(sbi, src_off); 4585 4586 folio = f2fs_grab_meta_folio(sbi, dst_off); 4587 seg_info_to_sit_folio(sbi, folio, start); 4588 4589 folio_mark_dirty(folio); 4590 set_to_next_sit(sit_i, start); 4591 4592 return folio; 4593 } 4594 4595 static struct sit_entry_set *grab_sit_entry_set(void) 4596 { 4597 struct sit_entry_set *ses = 4598 f2fs_kmem_cache_alloc(sit_entry_set_slab, 4599 GFP_NOFS, true, NULL); 4600 4601 ses->entry_cnt = 0; 4602 INIT_LIST_HEAD(&ses->set_list); 4603 return ses; 4604 } 4605 4606 static void release_sit_entry_set(struct sit_entry_set *ses) 4607 { 4608 list_del(&ses->set_list); 4609 kmem_cache_free(sit_entry_set_slab, ses); 4610 } 4611 4612 static void adjust_sit_entry_set(struct sit_entry_set *ses, 4613 struct list_head *head) 4614 { 4615 struct sit_entry_set *next = ses; 4616 4617 if (list_is_last(&ses->set_list, head)) 4618 return; 4619 4620 list_for_each_entry_continue(next, head, set_list) 4621 if (ses->entry_cnt <= next->entry_cnt) { 4622 list_move_tail(&ses->set_list, &next->set_list); 4623 return; 4624 } 4625 4626 list_move_tail(&ses->set_list, head); 4627 } 4628 4629 static void add_sit_entry(unsigned int segno, struct list_head *head) 4630 { 4631 struct sit_entry_set *ses; 4632 unsigned int start_segno = START_SEGNO(segno); 4633 4634 list_for_each_entry(ses, head, set_list) { 4635 if (ses->start_segno == start_segno) { 4636 ses->entry_cnt++; 4637 adjust_sit_entry_set(ses, head); 4638 return; 4639 } 4640 } 4641 4642 ses = grab_sit_entry_set(); 4643 4644 ses->start_segno = start_segno; 4645 ses->entry_cnt++; 4646 list_add(&ses->set_list, head); 4647 } 4648 4649 static void add_sits_in_set(struct f2fs_sb_info *sbi) 4650 { 4651 struct f2fs_sm_info *sm_info = SM_I(sbi); 4652 struct list_head *set_list = &sm_info->sit_entry_set; 4653 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; 4654 unsigned int segno; 4655 4656 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) 4657 add_sit_entry(segno, set_list); 4658 } 4659 4660 static void remove_sits_in_journal(struct f2fs_sb_info *sbi) 4661 { 4662 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 4663 struct f2fs_journal *journal = curseg->journal; 4664 int i; 4665 4666 down_write(&curseg->journal_rwsem); 4667 for (i = 0; i < sits_in_cursum(journal); i++) { 4668 unsigned int segno; 4669 bool dirtied; 4670 4671 segno = le32_to_cpu(segno_in_journal(journal, i)); 4672 dirtied = __mark_sit_entry_dirty(sbi, segno); 4673 4674 if (!dirtied) 4675 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); 4676 } 4677 update_sits_in_cursum(journal, -i); 4678 up_write(&curseg->journal_rwsem); 4679 } 4680 4681 /* 4682 * CP calls this function, which flushes SIT entries including sit_journal, 4683 * and moves prefree segs to free segs. 4684 */ 4685 void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) 4686 { 4687 struct sit_info *sit_i = SIT_I(sbi); 4688 unsigned long *bitmap = sit_i->dirty_sentries_bitmap; 4689 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 4690 struct f2fs_journal *journal = curseg->journal; 4691 struct sit_entry_set *ses, *tmp; 4692 struct list_head *head = &SM_I(sbi)->sit_entry_set; 4693 bool to_journal = !is_sbi_flag_set(sbi, SBI_IS_RESIZEFS); 4694 struct seg_entry *se; 4695 4696 down_write(&sit_i->sentry_lock); 4697 4698 if (!sit_i->dirty_sentries) 4699 goto out; 4700 4701 /* 4702 * add and account sit entries of dirty bitmap in sit entry 4703 * set temporarily 4704 */ 4705 add_sits_in_set(sbi); 4706 4707 /* 4708 * if there are no enough space in journal to store dirty sit 4709 * entries, remove all entries from journal and add and account 4710 * them in sit entry set. 4711 */ 4712 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL) || 4713 !to_journal) 4714 remove_sits_in_journal(sbi); 4715 4716 /* 4717 * there are two steps to flush sit entries: 4718 * #1, flush sit entries to journal in current cold data summary block. 4719 * #2, flush sit entries to sit page. 4720 */ 4721 list_for_each_entry_safe(ses, tmp, head, set_list) { 4722 struct folio *folio = NULL; 4723 struct f2fs_sit_block *raw_sit = NULL; 4724 unsigned int start_segno = ses->start_segno; 4725 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, 4726 (unsigned long)MAIN_SEGS(sbi)); 4727 unsigned int segno = start_segno; 4728 4729 if (to_journal && 4730 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL)) 4731 to_journal = false; 4732 4733 if (to_journal) { 4734 down_write(&curseg->journal_rwsem); 4735 } else { 4736 folio = get_next_sit_folio(sbi, start_segno); 4737 raw_sit = folio_address(folio); 4738 } 4739 4740 /* flush dirty sit entries in region of current sit set */ 4741 for_each_set_bit_from(segno, bitmap, end) { 4742 int offset, sit_offset; 4743 4744 se = get_seg_entry(sbi, segno); 4745 #ifdef CONFIG_F2FS_CHECK_FS 4746 if (memcmp(se->cur_valid_map, se->cur_valid_map_mir, 4747 SIT_VBLOCK_MAP_SIZE)) 4748 f2fs_bug_on(sbi, 1); 4749 #endif 4750 4751 /* add discard candidates */ 4752 if (!(cpc->reason & CP_DISCARD)) { 4753 cpc->trim_start = segno; 4754 add_discard_addrs(sbi, cpc, false); 4755 } 4756 4757 if (to_journal) { 4758 offset = f2fs_lookup_journal_in_cursum(journal, 4759 SIT_JOURNAL, segno, 1); 4760 f2fs_bug_on(sbi, offset < 0); 4761 segno_in_journal(journal, offset) = 4762 cpu_to_le32(segno); 4763 seg_info_to_raw_sit(se, 4764 &sit_in_journal(journal, offset)); 4765 check_block_count(sbi, segno, 4766 &sit_in_journal(journal, offset)); 4767 } else { 4768 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); 4769 seg_info_to_raw_sit(se, 4770 &raw_sit->entries[sit_offset]); 4771 check_block_count(sbi, segno, 4772 &raw_sit->entries[sit_offset]); 4773 } 4774 4775 /* update ckpt_valid_block */ 4776 if (__is_large_section(sbi)) { 4777 set_ckpt_valid_blocks(sbi, segno); 4778 sanity_check_valid_blocks(sbi, segno); 4779 } 4780 4781 __clear_bit(segno, bitmap); 4782 sit_i->dirty_sentries--; 4783 ses->entry_cnt--; 4784 } 4785 4786 if (to_journal) 4787 up_write(&curseg->journal_rwsem); 4788 else 4789 f2fs_folio_put(folio, true); 4790 4791 f2fs_bug_on(sbi, ses->entry_cnt); 4792 release_sit_entry_set(ses); 4793 } 4794 4795 f2fs_bug_on(sbi, !list_empty(head)); 4796 f2fs_bug_on(sbi, sit_i->dirty_sentries); 4797 out: 4798 if (cpc->reason & CP_DISCARD) { 4799 __u64 trim_start = cpc->trim_start; 4800 4801 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) 4802 add_discard_addrs(sbi, cpc, false); 4803 4804 cpc->trim_start = trim_start; 4805 } 4806 up_write(&sit_i->sentry_lock); 4807 4808 set_prefree_as_free_segments(sbi); 4809 } 4810 4811 static int build_sit_info(struct f2fs_sb_info *sbi) 4812 { 4813 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 4814 struct sit_info *sit_i; 4815 unsigned int sit_segs, start; 4816 char *src_bitmap, *bitmap; 4817 unsigned int bitmap_size, main_bitmap_size, sit_bitmap_size; 4818 unsigned int discard_map = f2fs_block_unit_discard(sbi) ? 1 : 0; 4819 4820 /* allocate memory for SIT information */ 4821 sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL); 4822 if (!sit_i) 4823 return -ENOMEM; 4824 4825 SM_I(sbi)->sit_info = sit_i; 4826 4827 sit_i->sentries = 4828 f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry), 4829 MAIN_SEGS(sbi)), 4830 GFP_KERNEL); 4831 if (!sit_i->sentries) 4832 return -ENOMEM; 4833 4834 main_bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 4835 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, main_bitmap_size, 4836 GFP_KERNEL); 4837 if (!sit_i->dirty_sentries_bitmap) 4838 return -ENOMEM; 4839 4840 #ifdef CONFIG_F2FS_CHECK_FS 4841 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (3 + discard_map); 4842 #else 4843 bitmap_size = MAIN_SEGS(sbi) * SIT_VBLOCK_MAP_SIZE * (2 + discard_map); 4844 #endif 4845 sit_i->bitmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); 4846 if (!sit_i->bitmap) 4847 return -ENOMEM; 4848 4849 bitmap = sit_i->bitmap; 4850 4851 for (start = 0; start < MAIN_SEGS(sbi); start++) { 4852 sit_i->sentries[start].cur_valid_map = bitmap; 4853 bitmap += SIT_VBLOCK_MAP_SIZE; 4854 4855 sit_i->sentries[start].ckpt_valid_map = bitmap; 4856 bitmap += SIT_VBLOCK_MAP_SIZE; 4857 4858 #ifdef CONFIG_F2FS_CHECK_FS 4859 sit_i->sentries[start].cur_valid_map_mir = bitmap; 4860 bitmap += SIT_VBLOCK_MAP_SIZE; 4861 #endif 4862 4863 if (discard_map) { 4864 sit_i->sentries[start].discard_map = bitmap; 4865 bitmap += SIT_VBLOCK_MAP_SIZE; 4866 } 4867 } 4868 4869 sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 4870 if (!sit_i->tmp_map) 4871 return -ENOMEM; 4872 4873 if (__is_large_section(sbi)) { 4874 sit_i->sec_entries = 4875 f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry), 4876 MAIN_SECS(sbi)), 4877 GFP_KERNEL); 4878 if (!sit_i->sec_entries) 4879 return -ENOMEM; 4880 } 4881 4882 /* get information related with SIT */ 4883 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; 4884 4885 /* setup SIT bitmap from ckeckpoint pack */ 4886 sit_bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 4887 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 4888 4889 sit_i->sit_bitmap = kmemdup(src_bitmap, sit_bitmap_size, GFP_KERNEL); 4890 if (!sit_i->sit_bitmap) 4891 return -ENOMEM; 4892 4893 #ifdef CONFIG_F2FS_CHECK_FS 4894 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, 4895 sit_bitmap_size, GFP_KERNEL); 4896 if (!sit_i->sit_bitmap_mir) 4897 return -ENOMEM; 4898 4899 sit_i->invalid_segmap = f2fs_kvzalloc(sbi, 4900 main_bitmap_size, GFP_KERNEL); 4901 if (!sit_i->invalid_segmap) 4902 return -ENOMEM; 4903 #endif 4904 4905 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); 4906 sit_i->sit_blocks = SEGS_TO_BLKS(sbi, sit_segs); 4907 sit_i->written_valid_blocks = 0; 4908 sit_i->bitmap_size = sit_bitmap_size; 4909 sit_i->dirty_sentries = 0; 4910 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 4911 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); 4912 sit_i->mounted_time = ktime_get_boottime_seconds(); 4913 init_rwsem(&sit_i->sentry_lock); 4914 return 0; 4915 } 4916 4917 static int build_free_segmap(struct f2fs_sb_info *sbi) 4918 { 4919 struct free_segmap_info *free_i; 4920 unsigned int bitmap_size, sec_bitmap_size; 4921 4922 /* allocate memory for free segmap information */ 4923 free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL); 4924 if (!free_i) 4925 return -ENOMEM; 4926 4927 SM_I(sbi)->free_info = free_i; 4928 4929 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 4930 free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL); 4931 if (!free_i->free_segmap) 4932 return -ENOMEM; 4933 4934 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 4935 free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL); 4936 if (!free_i->free_secmap) 4937 return -ENOMEM; 4938 4939 /* set all segments as dirty temporarily */ 4940 memset(free_i->free_segmap, 0xff, bitmap_size); 4941 memset(free_i->free_secmap, 0xff, sec_bitmap_size); 4942 4943 /* init free segmap information */ 4944 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); 4945 free_i->free_segments = 0; 4946 free_i->free_sections = 0; 4947 spin_lock_init(&free_i->segmap_lock); 4948 return 0; 4949 } 4950 4951 static int build_curseg(struct f2fs_sb_info *sbi) 4952 { 4953 struct curseg_info *array; 4954 int i; 4955 4956 array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, 4957 sizeof(*array)), GFP_KERNEL); 4958 if (!array) 4959 return -ENOMEM; 4960 4961 SM_I(sbi)->curseg_array = array; 4962 4963 for (i = 0; i < NO_CHECK_TYPE; i++) { 4964 mutex_init(&array[i].curseg_mutex); 4965 array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL); 4966 if (!array[i].sum_blk) 4967 return -ENOMEM; 4968 init_rwsem(&array[i].journal_rwsem); 4969 array[i].journal = f2fs_kzalloc(sbi, 4970 sizeof(struct f2fs_journal), GFP_KERNEL); 4971 if (!array[i].journal) 4972 return -ENOMEM; 4973 array[i].seg_type = log_type_to_seg_type(i); 4974 reset_curseg_fields(&array[i]); 4975 } 4976 return restore_curseg_summaries(sbi); 4977 } 4978 4979 static int build_sit_entries(struct f2fs_sb_info *sbi) 4980 { 4981 struct sit_info *sit_i = SIT_I(sbi); 4982 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 4983 struct f2fs_journal *journal = curseg->journal; 4984 struct seg_entry *se; 4985 struct f2fs_sit_entry sit; 4986 int sit_blk_cnt = SIT_BLK_CNT(sbi); 4987 unsigned int i, start, end; 4988 unsigned int readed, start_blk = 0; 4989 int err = 0; 4990 block_t sit_valid_blocks[2] = {0, 0}; 4991 4992 do { 4993 readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_VECS, 4994 META_SIT, true); 4995 4996 start = start_blk * sit_i->sents_per_block; 4997 end = (start_blk + readed) * sit_i->sents_per_block; 4998 4999 for (; start < end && start < MAIN_SEGS(sbi); start++) { 5000 struct f2fs_sit_block *sit_blk; 5001 struct folio *folio; 5002 5003 se = &sit_i->sentries[start]; 5004 folio = get_current_sit_folio(sbi, start); 5005 if (IS_ERR(folio)) 5006 return PTR_ERR(folio); 5007 sit_blk = folio_address(folio); 5008 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; 5009 f2fs_folio_put(folio, true); 5010 5011 err = check_block_count(sbi, start, &sit); 5012 if (err) 5013 return err; 5014 seg_info_from_raw_sit(se, &sit); 5015 5016 if (se->type >= NR_PERSISTENT_LOG) { 5017 f2fs_err(sbi, "Invalid segment type: %u, segno: %u", 5018 se->type, start); 5019 f2fs_handle_error(sbi, 5020 ERROR_INCONSISTENT_SUM_TYPE); 5021 return -EFSCORRUPTED; 5022 } 5023 5024 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks; 5025 5026 if (!f2fs_block_unit_discard(sbi)) 5027 goto init_discard_map_done; 5028 5029 /* build discard map only one time */ 5030 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { 5031 memset(se->discard_map, 0xff, 5032 SIT_VBLOCK_MAP_SIZE); 5033 goto init_discard_map_done; 5034 } 5035 memcpy(se->discard_map, se->cur_valid_map, 5036 SIT_VBLOCK_MAP_SIZE); 5037 sbi->discard_blks += BLKS_PER_SEG(sbi) - 5038 se->valid_blocks; 5039 init_discard_map_done: 5040 if (__is_large_section(sbi)) 5041 get_sec_entry(sbi, start)->valid_blocks += 5042 se->valid_blocks; 5043 } 5044 start_blk += readed; 5045 } while (start_blk < sit_blk_cnt); 5046 5047 down_read(&curseg->journal_rwsem); 5048 for (i = 0; i < sits_in_cursum(journal); i++) { 5049 unsigned int old_valid_blocks; 5050 5051 start = le32_to_cpu(segno_in_journal(journal, i)); 5052 if (start >= MAIN_SEGS(sbi)) { 5053 f2fs_err(sbi, "Wrong journal entry on segno %u", 5054 start); 5055 err = -EFSCORRUPTED; 5056 f2fs_handle_error(sbi, ERROR_CORRUPTED_JOURNAL); 5057 break; 5058 } 5059 5060 se = &sit_i->sentries[start]; 5061 sit = sit_in_journal(journal, i); 5062 5063 old_valid_blocks = se->valid_blocks; 5064 5065 sit_valid_blocks[SE_PAGETYPE(se)] -= old_valid_blocks; 5066 5067 err = check_block_count(sbi, start, &sit); 5068 if (err) 5069 break; 5070 seg_info_from_raw_sit(se, &sit); 5071 5072 if (se->type >= NR_PERSISTENT_LOG) { 5073 f2fs_err(sbi, "Invalid segment type: %u, segno: %u", 5074 se->type, start); 5075 err = -EFSCORRUPTED; 5076 f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUM_TYPE); 5077 break; 5078 } 5079 5080 sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks; 5081 5082 if (f2fs_block_unit_discard(sbi)) { 5083 if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) { 5084 memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE); 5085 } else { 5086 memcpy(se->discard_map, se->cur_valid_map, 5087 SIT_VBLOCK_MAP_SIZE); 5088 sbi->discard_blks += old_valid_blocks; 5089 sbi->discard_blks -= se->valid_blocks; 5090 } 5091 } 5092 5093 if (__is_large_section(sbi)) { 5094 get_sec_entry(sbi, start)->valid_blocks += 5095 se->valid_blocks; 5096 get_sec_entry(sbi, start)->valid_blocks -= 5097 old_valid_blocks; 5098 } 5099 } 5100 up_read(&curseg->journal_rwsem); 5101 5102 /* update ckpt_valid_block */ 5103 if (__is_large_section(sbi)) { 5104 unsigned int segno; 5105 5106 for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) { 5107 set_ckpt_valid_blocks(sbi, segno); 5108 sanity_check_valid_blocks(sbi, segno); 5109 } 5110 } 5111 5112 if (err) 5113 return err; 5114 5115 if (sit_valid_blocks[NODE] != valid_node_count(sbi)) { 5116 f2fs_err(sbi, "SIT is corrupted node# %u vs %u", 5117 sit_valid_blocks[NODE], valid_node_count(sbi)); 5118 f2fs_handle_error(sbi, ERROR_INCONSISTENT_NODE_COUNT); 5119 return -EFSCORRUPTED; 5120 } 5121 5122 if (sit_valid_blocks[DATA] + sit_valid_blocks[NODE] > 5123 valid_user_blocks(sbi)) { 5124 f2fs_err(sbi, "SIT is corrupted data# %u %u vs %u", 5125 sit_valid_blocks[DATA], sit_valid_blocks[NODE], 5126 valid_user_blocks(sbi)); 5127 f2fs_handle_error(sbi, ERROR_INCONSISTENT_BLOCK_COUNT); 5128 return -EFSCORRUPTED; 5129 } 5130 5131 return 0; 5132 } 5133 5134 static void init_free_segmap(struct f2fs_sb_info *sbi) 5135 { 5136 unsigned int start; 5137 int type; 5138 struct seg_entry *sentry; 5139 5140 for (start = 0; start < MAIN_SEGS(sbi); start++) { 5141 if (f2fs_usable_blks_in_seg(sbi, start) == 0) 5142 continue; 5143 sentry = get_seg_entry(sbi, start); 5144 if (!sentry->valid_blocks) 5145 __set_free(sbi, start); 5146 else 5147 SIT_I(sbi)->written_valid_blocks += 5148 sentry->valid_blocks; 5149 } 5150 5151 /* set use the current segments */ 5152 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { 5153 struct curseg_info *curseg_t = CURSEG_I(sbi, type); 5154 5155 __set_test_and_inuse(sbi, curseg_t->segno); 5156 } 5157 } 5158 5159 static void init_dirty_segmap(struct f2fs_sb_info *sbi) 5160 { 5161 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 5162 struct free_segmap_info *free_i = FREE_I(sbi); 5163 unsigned int segno = 0, offset = 0, secno; 5164 block_t valid_blocks, usable_blks_in_seg; 5165 5166 while (1) { 5167 /* find dirty segment based on free segmap */ 5168 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); 5169 if (segno >= MAIN_SEGS(sbi)) 5170 break; 5171 offset = segno + 1; 5172 valid_blocks = get_valid_blocks(sbi, segno, false); 5173 usable_blks_in_seg = f2fs_usable_blks_in_seg(sbi, segno); 5174 if (valid_blocks == usable_blks_in_seg || !valid_blocks) 5175 continue; 5176 if (valid_blocks > usable_blks_in_seg) { 5177 f2fs_bug_on(sbi, 1); 5178 continue; 5179 } 5180 mutex_lock(&dirty_i->seglist_lock); 5181 __locate_dirty_segment(sbi, segno, DIRTY); 5182 mutex_unlock(&dirty_i->seglist_lock); 5183 } 5184 5185 if (!__is_large_section(sbi)) 5186 return; 5187 5188 mutex_lock(&dirty_i->seglist_lock); 5189 for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) { 5190 valid_blocks = get_valid_blocks(sbi, segno, true); 5191 secno = GET_SEC_FROM_SEG(sbi, segno); 5192 5193 if (!valid_blocks || valid_blocks == CAP_BLKS_PER_SEC(sbi)) 5194 continue; 5195 if (is_cursec(sbi, secno)) 5196 continue; 5197 set_bit(secno, dirty_i->dirty_secmap); 5198 } 5199 mutex_unlock(&dirty_i->seglist_lock); 5200 } 5201 5202 static int init_victim_secmap(struct f2fs_sb_info *sbi) 5203 { 5204 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 5205 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 5206 5207 dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); 5208 if (!dirty_i->victim_secmap) 5209 return -ENOMEM; 5210 5211 dirty_i->pinned_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL); 5212 if (!dirty_i->pinned_secmap) 5213 return -ENOMEM; 5214 5215 dirty_i->pinned_secmap_cnt = 0; 5216 dirty_i->enable_pin_section = true; 5217 return 0; 5218 } 5219 5220 static int build_dirty_segmap(struct f2fs_sb_info *sbi) 5221 { 5222 struct dirty_seglist_info *dirty_i; 5223 unsigned int bitmap_size, i; 5224 5225 /* allocate memory for dirty segments list information */ 5226 dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info), 5227 GFP_KERNEL); 5228 if (!dirty_i) 5229 return -ENOMEM; 5230 5231 SM_I(sbi)->dirty_info = dirty_i; 5232 mutex_init(&dirty_i->seglist_lock); 5233 5234 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 5235 5236 for (i = 0; i < NR_DIRTY_TYPE; i++) { 5237 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size, 5238 GFP_KERNEL); 5239 if (!dirty_i->dirty_segmap[i]) 5240 return -ENOMEM; 5241 } 5242 5243 if (__is_large_section(sbi)) { 5244 bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 5245 dirty_i->dirty_secmap = f2fs_kvzalloc(sbi, 5246 bitmap_size, GFP_KERNEL); 5247 if (!dirty_i->dirty_secmap) 5248 return -ENOMEM; 5249 } 5250 5251 init_dirty_segmap(sbi); 5252 return init_victim_secmap(sbi); 5253 } 5254 5255 static int sanity_check_curseg(struct f2fs_sb_info *sbi) 5256 { 5257 int i; 5258 5259 /* 5260 * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr; 5261 * In LFS curseg, all blkaddr after .next_blkoff should be unused. 5262 */ 5263 for (i = 0; i < NR_PERSISTENT_LOG; i++) { 5264 struct curseg_info *curseg = CURSEG_I(sbi, i); 5265 struct seg_entry *se = get_seg_entry(sbi, curseg->segno); 5266 unsigned int blkofs = curseg->next_blkoff; 5267 5268 if (f2fs_sb_has_readonly(sbi) && 5269 i != CURSEG_HOT_DATA && i != CURSEG_HOT_NODE) 5270 continue; 5271 5272 sanity_check_seg_type(sbi, curseg->seg_type); 5273 5274 if (curseg->alloc_type != LFS && curseg->alloc_type != SSR) { 5275 f2fs_err(sbi, 5276 "Current segment has invalid alloc_type:%d", 5277 curseg->alloc_type); 5278 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG); 5279 return -EFSCORRUPTED; 5280 } 5281 5282 if (f2fs_test_bit(blkofs, se->cur_valid_map)) 5283 goto out; 5284 5285 if (curseg->alloc_type == SSR) 5286 continue; 5287 5288 for (blkofs += 1; blkofs < BLKS_PER_SEG(sbi); blkofs++) { 5289 if (!f2fs_test_bit(blkofs, se->cur_valid_map)) 5290 continue; 5291 out: 5292 f2fs_err(sbi, 5293 "Current segment's next free block offset is inconsistent with bitmap, logtype:%u, segno:%u, type:%u, next_blkoff:%u, blkofs:%u", 5294 i, curseg->segno, curseg->alloc_type, 5295 curseg->next_blkoff, blkofs); 5296 f2fs_handle_error(sbi, ERROR_INVALID_CURSEG); 5297 return -EFSCORRUPTED; 5298 } 5299 } 5300 return 0; 5301 } 5302 5303 #ifdef CONFIG_BLK_DEV_ZONED 5304 static int check_zone_write_pointer(struct f2fs_sb_info *sbi, 5305 struct f2fs_dev_info *fdev, 5306 struct blk_zone *zone) 5307 { 5308 unsigned int zone_segno; 5309 block_t zone_block, valid_block_cnt; 5310 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; 5311 int ret; 5312 unsigned int nofs_flags; 5313 5314 if (zone->type != BLK_ZONE_TYPE_SEQWRITE_REQ) 5315 return 0; 5316 5317 zone_block = fdev->start_blk + (zone->start >> log_sectors_per_block); 5318 zone_segno = GET_SEGNO(sbi, zone_block); 5319 5320 /* 5321 * Skip check of zones cursegs point to, since 5322 * fix_curseg_write_pointer() checks them. 5323 */ 5324 if (zone_segno >= MAIN_SEGS(sbi)) 5325 return 0; 5326 5327 /* 5328 * Get # of valid block of the zone. 5329 */ 5330 valid_block_cnt = get_valid_blocks(sbi, zone_segno, true); 5331 if (is_cursec(sbi, GET_SEC_FROM_SEG(sbi, zone_segno))) { 5332 f2fs_notice(sbi, "Open zones: valid block[0x%x,0x%x] cond[%s]", 5333 zone_segno, valid_block_cnt, 5334 blk_zone_cond_str(zone->cond)); 5335 return 0; 5336 } 5337 5338 if ((!valid_block_cnt && zone->cond == BLK_ZONE_COND_EMPTY) || 5339 (valid_block_cnt && zone->cond == BLK_ZONE_COND_FULL)) 5340 return 0; 5341 5342 if (!valid_block_cnt) { 5343 f2fs_notice(sbi, "Zone without valid block has non-zero write " 5344 "pointer. Reset the write pointer: cond[%s]", 5345 blk_zone_cond_str(zone->cond)); 5346 ret = __f2fs_issue_discard_zone(sbi, fdev->bdev, zone_block, 5347 zone->len >> log_sectors_per_block); 5348 if (ret) 5349 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", 5350 fdev->path, ret); 5351 return ret; 5352 } 5353 5354 /* 5355 * If there are valid blocks and the write pointer doesn't match 5356 * with them, we need to report the inconsistency and fill 5357 * the zone till the end to close the zone. This inconsistency 5358 * does not cause write error because the zone will not be 5359 * selected for write operation until it get discarded. 5360 */ 5361 f2fs_notice(sbi, "Valid blocks are not aligned with write " 5362 "pointer: valid block[0x%x,0x%x] cond[%s]", 5363 zone_segno, valid_block_cnt, blk_zone_cond_str(zone->cond)); 5364 5365 nofs_flags = memalloc_nofs_save(); 5366 ret = blkdev_zone_mgmt(fdev->bdev, REQ_OP_ZONE_FINISH, 5367 zone->start, zone->len); 5368 memalloc_nofs_restore(nofs_flags); 5369 if (ret == -EOPNOTSUPP) { 5370 ret = blkdev_issue_zeroout(fdev->bdev, zone->wp, 5371 zone->len - (zone->wp - zone->start), 5372 GFP_NOFS, 0); 5373 if (ret) 5374 f2fs_err(sbi, "Fill up zone failed: %s (errno=%d)", 5375 fdev->path, ret); 5376 } else if (ret) { 5377 f2fs_err(sbi, "Finishing zone failed: %s (errno=%d)", 5378 fdev->path, ret); 5379 } 5380 5381 return ret; 5382 } 5383 5384 static struct f2fs_dev_info *get_target_zoned_dev(struct f2fs_sb_info *sbi, 5385 block_t zone_blkaddr) 5386 { 5387 int i; 5388 5389 for (i = 0; i < sbi->s_ndevs; i++) { 5390 if (!bdev_is_zoned(FDEV(i).bdev)) 5391 continue; 5392 if (sbi->s_ndevs == 1 || (FDEV(i).start_blk <= zone_blkaddr && 5393 zone_blkaddr <= FDEV(i).end_blk)) 5394 return &FDEV(i); 5395 } 5396 5397 return NULL; 5398 } 5399 5400 static int report_one_zone_cb(struct blk_zone *zone, unsigned int idx, 5401 void *data) 5402 { 5403 memcpy(data, zone, sizeof(struct blk_zone)); 5404 return 0; 5405 } 5406 5407 static int do_fix_curseg_write_pointer(struct f2fs_sb_info *sbi, int type) 5408 { 5409 struct curseg_info *cs = CURSEG_I(sbi, type); 5410 struct f2fs_dev_info *zbd; 5411 struct blk_zone zone; 5412 unsigned int cs_section, wp_segno, wp_blkoff, wp_sector_off; 5413 block_t cs_zone_block, wp_block; 5414 unsigned int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT; 5415 sector_t zone_sector; 5416 int err; 5417 5418 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); 5419 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); 5420 5421 zbd = get_target_zoned_dev(sbi, cs_zone_block); 5422 if (!zbd) 5423 return 0; 5424 5425 /* report zone for the sector the curseg points to */ 5426 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk) 5427 << log_sectors_per_block; 5428 err = blkdev_report_zones(zbd->bdev, zone_sector, 1, 5429 report_one_zone_cb, &zone); 5430 if (err != 1) { 5431 f2fs_err(sbi, "Report zone failed: %s errno=(%d)", 5432 zbd->path, err); 5433 return err; 5434 } 5435 5436 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ) 5437 return 0; 5438 5439 /* 5440 * When safely unmounted in the previous mount, we could use current 5441 * segments. Otherwise, allocate new sections. 5442 */ 5443 if (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) { 5444 wp_block = zbd->start_blk + (zone.wp >> log_sectors_per_block); 5445 wp_segno = GET_SEGNO(sbi, wp_block); 5446 wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno); 5447 wp_sector_off = zone.wp & GENMASK(log_sectors_per_block - 1, 0); 5448 5449 if (cs->segno == wp_segno && cs->next_blkoff == wp_blkoff && 5450 wp_sector_off == 0) 5451 return 0; 5452 5453 f2fs_notice(sbi, "Unaligned curseg[%d] with write pointer: " 5454 "curseg[0x%x,0x%x] wp[0x%x,0x%x]", type, cs->segno, 5455 cs->next_blkoff, wp_segno, wp_blkoff); 5456 } 5457 5458 /* Allocate a new section if it's not new. */ 5459 if (cs->next_blkoff || 5460 cs->segno != GET_SEG_FROM_SEC(sbi, GET_ZONE_FROM_SEC(sbi, cs_section))) { 5461 unsigned int old_segno = cs->segno, old_blkoff = cs->next_blkoff; 5462 5463 f2fs_allocate_new_section(sbi, type, true); 5464 f2fs_notice(sbi, "Assign new section to curseg[%d]: " 5465 "[0x%x,0x%x] -> [0x%x,0x%x]", 5466 type, old_segno, old_blkoff, 5467 cs->segno, cs->next_blkoff); 5468 } 5469 5470 /* check consistency of the zone curseg pointed to */ 5471 if (check_zone_write_pointer(sbi, zbd, &zone)) 5472 return -EIO; 5473 5474 /* check newly assigned zone */ 5475 cs_section = GET_SEC_FROM_SEG(sbi, cs->segno); 5476 cs_zone_block = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, cs_section)); 5477 5478 zbd = get_target_zoned_dev(sbi, cs_zone_block); 5479 if (!zbd) 5480 return 0; 5481 5482 zone_sector = (sector_t)(cs_zone_block - zbd->start_blk) 5483 << log_sectors_per_block; 5484 err = blkdev_report_zones(zbd->bdev, zone_sector, 1, 5485 report_one_zone_cb, &zone); 5486 if (err != 1) { 5487 f2fs_err(sbi, "Report zone failed: %s errno=(%d)", 5488 zbd->path, err); 5489 return err; 5490 } 5491 5492 if (zone.type != BLK_ZONE_TYPE_SEQWRITE_REQ) 5493 return 0; 5494 5495 if (zone.wp != zone.start) { 5496 f2fs_notice(sbi, 5497 "New zone for curseg[%d] is not yet discarded. " 5498 "Reset the zone: curseg[0x%x,0x%x]", 5499 type, cs->segno, cs->next_blkoff); 5500 err = __f2fs_issue_discard_zone(sbi, zbd->bdev, cs_zone_block, 5501 zone.len >> log_sectors_per_block); 5502 if (err) { 5503 f2fs_err(sbi, "Discard zone failed: %s (errno=%d)", 5504 zbd->path, err); 5505 return err; 5506 } 5507 } 5508 5509 return 0; 5510 } 5511 5512 static int fix_curseg_write_pointer(struct f2fs_sb_info *sbi) 5513 { 5514 int i, ret; 5515 5516 for (i = 0; i < NR_PERSISTENT_LOG; i++) { 5517 ret = do_fix_curseg_write_pointer(sbi, i); 5518 if (ret) 5519 return ret; 5520 } 5521 5522 return 0; 5523 } 5524 5525 struct check_zone_write_pointer_args { 5526 struct f2fs_sb_info *sbi; 5527 struct f2fs_dev_info *fdev; 5528 }; 5529 5530 static int check_zone_write_pointer_cb(struct blk_zone *zone, unsigned int idx, 5531 void *data) 5532 { 5533 struct check_zone_write_pointer_args *args; 5534 5535 args = (struct check_zone_write_pointer_args *)data; 5536 5537 return check_zone_write_pointer(args->sbi, args->fdev, zone); 5538 } 5539 5540 static int check_write_pointer(struct f2fs_sb_info *sbi) 5541 { 5542 int i, ret; 5543 struct check_zone_write_pointer_args args; 5544 5545 for (i = 0; i < sbi->s_ndevs; i++) { 5546 if (!bdev_is_zoned(FDEV(i).bdev)) 5547 continue; 5548 5549 args.sbi = sbi; 5550 args.fdev = &FDEV(i); 5551 ret = blkdev_report_zones(FDEV(i).bdev, 0, BLK_ALL_ZONES, 5552 check_zone_write_pointer_cb, &args); 5553 if (ret < 0) 5554 return ret; 5555 } 5556 5557 return 0; 5558 } 5559 5560 int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi) 5561 { 5562 int ret; 5563 5564 if (!f2fs_sb_has_blkzoned(sbi) || f2fs_readonly(sbi->sb) || 5565 f2fs_hw_is_readonly(sbi)) 5566 return 0; 5567 5568 f2fs_notice(sbi, "Checking entire write pointers"); 5569 ret = fix_curseg_write_pointer(sbi); 5570 if (!ret) 5571 ret = check_write_pointer(sbi); 5572 return ret; 5573 } 5574 5575 /* 5576 * Return the number of usable blocks in a segment. The number of blocks 5577 * returned is always equal to the number of blocks in a segment for 5578 * segments fully contained within a sequential zone capacity or a 5579 * conventional zone. For segments partially contained in a sequential 5580 * zone capacity, the number of usable blocks up to the zone capacity 5581 * is returned. 0 is returned in all other cases. 5582 */ 5583 static inline unsigned int f2fs_usable_zone_blks_in_seg( 5584 struct f2fs_sb_info *sbi, unsigned int segno) 5585 { 5586 block_t seg_start, sec_start_blkaddr, sec_cap_blkaddr; 5587 unsigned int secno; 5588 5589 if (!sbi->unusable_blocks_per_sec) 5590 return BLKS_PER_SEG(sbi); 5591 5592 secno = GET_SEC_FROM_SEG(sbi, segno); 5593 seg_start = START_BLOCK(sbi, segno); 5594 sec_start_blkaddr = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno)); 5595 sec_cap_blkaddr = sec_start_blkaddr + CAP_BLKS_PER_SEC(sbi); 5596 5597 /* 5598 * If segment starts before zone capacity and spans beyond 5599 * zone capacity, then usable blocks are from seg start to 5600 * zone capacity. If the segment starts after the zone capacity, 5601 * then there are no usable blocks. 5602 */ 5603 if (seg_start >= sec_cap_blkaddr) 5604 return 0; 5605 if (seg_start + BLKS_PER_SEG(sbi) > sec_cap_blkaddr) 5606 return sec_cap_blkaddr - seg_start; 5607 5608 return BLKS_PER_SEG(sbi); 5609 } 5610 #else 5611 int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi) 5612 { 5613 return 0; 5614 } 5615 5616 static inline unsigned int f2fs_usable_zone_blks_in_seg(struct f2fs_sb_info *sbi, 5617 unsigned int segno) 5618 { 5619 return 0; 5620 } 5621 5622 #endif 5623 unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi, 5624 unsigned int segno) 5625 { 5626 if (f2fs_sb_has_blkzoned(sbi)) 5627 return f2fs_usable_zone_blks_in_seg(sbi, segno); 5628 5629 return BLKS_PER_SEG(sbi); 5630 } 5631 5632 unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi) 5633 { 5634 if (f2fs_sb_has_blkzoned(sbi)) 5635 return CAP_SEGS_PER_SEC(sbi); 5636 5637 return SEGS_PER_SEC(sbi); 5638 } 5639 5640 unsigned long long f2fs_get_section_mtime(struct f2fs_sb_info *sbi, 5641 unsigned int segno) 5642 { 5643 unsigned int usable_segs_per_sec = f2fs_usable_segs_in_sec(sbi); 5644 unsigned int secno = 0, start = 0; 5645 unsigned int total_valid_blocks = 0; 5646 unsigned long long mtime = 0; 5647 unsigned int i = 0; 5648 5649 secno = GET_SEC_FROM_SEG(sbi, segno); 5650 start = GET_SEG_FROM_SEC(sbi, secno); 5651 5652 if (!__is_large_section(sbi)) { 5653 mtime = get_seg_entry(sbi, start + i)->mtime; 5654 goto out; 5655 } 5656 5657 for (i = 0; i < usable_segs_per_sec; i++) { 5658 /* for large section, only check the mtime of valid segments */ 5659 struct seg_entry *se = get_seg_entry(sbi, start+i); 5660 5661 mtime += se->mtime * se->valid_blocks; 5662 total_valid_blocks += se->valid_blocks; 5663 } 5664 5665 if (total_valid_blocks == 0) 5666 return INVALID_MTIME; 5667 5668 mtime = div_u64(mtime, total_valid_blocks); 5669 out: 5670 if (unlikely(mtime == INVALID_MTIME)) 5671 mtime -= 1; 5672 return mtime; 5673 } 5674 5675 /* 5676 * Update min, max modified time for cost-benefit GC algorithm 5677 */ 5678 static void init_min_max_mtime(struct f2fs_sb_info *sbi) 5679 { 5680 struct sit_info *sit_i = SIT_I(sbi); 5681 unsigned int segno; 5682 5683 down_write(&sit_i->sentry_lock); 5684 5685 sit_i->min_mtime = ULLONG_MAX; 5686 5687 for (segno = 0; segno < MAIN_SEGS(sbi); segno += SEGS_PER_SEC(sbi)) { 5688 unsigned long long mtime = 0; 5689 5690 mtime = f2fs_get_section_mtime(sbi, segno); 5691 5692 if (sit_i->min_mtime > mtime) 5693 sit_i->min_mtime = mtime; 5694 } 5695 sit_i->max_mtime = get_mtime(sbi, false); 5696 sit_i->dirty_max_mtime = 0; 5697 up_write(&sit_i->sentry_lock); 5698 } 5699 5700 int f2fs_build_segment_manager(struct f2fs_sb_info *sbi) 5701 { 5702 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 5703 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 5704 struct f2fs_sm_info *sm_info; 5705 int err; 5706 5707 sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL); 5708 if (!sm_info) 5709 return -ENOMEM; 5710 5711 /* init sm info */ 5712 sbi->sm_info = sm_info; 5713 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 5714 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 5715 sm_info->segment_count = le32_to_cpu(raw_super->segment_count); 5716 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 5717 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 5718 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); 5719 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 5720 sm_info->rec_prefree_segments = sm_info->main_segments * 5721 DEF_RECLAIM_PREFREE_SEGMENTS / 100; 5722 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS) 5723 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS; 5724 5725 if (!f2fs_lfs_mode(sbi)) 5726 sm_info->ipu_policy = BIT(F2FS_IPU_FSYNC); 5727 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; 5728 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; 5729 sm_info->min_seq_blocks = BLKS_PER_SEG(sbi); 5730 sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS; 5731 sm_info->min_ssr_sections = reserved_sections(sbi); 5732 5733 INIT_LIST_HEAD(&sm_info->sit_entry_set); 5734 5735 init_f2fs_rwsem(&sm_info->curseg_lock); 5736 5737 err = f2fs_create_flush_cmd_control(sbi); 5738 if (err) 5739 return err; 5740 5741 err = create_discard_cmd_control(sbi); 5742 if (err) 5743 return err; 5744 5745 err = build_sit_info(sbi); 5746 if (err) 5747 return err; 5748 err = build_free_segmap(sbi); 5749 if (err) 5750 return err; 5751 err = build_curseg(sbi); 5752 if (err) 5753 return err; 5754 5755 /* reinit free segmap based on SIT */ 5756 err = build_sit_entries(sbi); 5757 if (err) 5758 return err; 5759 5760 init_free_segmap(sbi); 5761 err = build_dirty_segmap(sbi); 5762 if (err) 5763 return err; 5764 5765 err = sanity_check_curseg(sbi); 5766 if (err) 5767 return err; 5768 5769 init_min_max_mtime(sbi); 5770 return 0; 5771 } 5772 5773 static void discard_dirty_segmap(struct f2fs_sb_info *sbi, 5774 enum dirty_type dirty_type) 5775 { 5776 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 5777 5778 mutex_lock(&dirty_i->seglist_lock); 5779 kvfree(dirty_i->dirty_segmap[dirty_type]); 5780 dirty_i->nr_dirty[dirty_type] = 0; 5781 mutex_unlock(&dirty_i->seglist_lock); 5782 } 5783 5784 static void destroy_victim_secmap(struct f2fs_sb_info *sbi) 5785 { 5786 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 5787 5788 kvfree(dirty_i->pinned_secmap); 5789 kvfree(dirty_i->victim_secmap); 5790 } 5791 5792 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) 5793 { 5794 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 5795 int i; 5796 5797 if (!dirty_i) 5798 return; 5799 5800 /* discard pre-free/dirty segments list */ 5801 for (i = 0; i < NR_DIRTY_TYPE; i++) 5802 discard_dirty_segmap(sbi, i); 5803 5804 if (__is_large_section(sbi)) { 5805 mutex_lock(&dirty_i->seglist_lock); 5806 kvfree(dirty_i->dirty_secmap); 5807 mutex_unlock(&dirty_i->seglist_lock); 5808 } 5809 5810 destroy_victim_secmap(sbi); 5811 SM_I(sbi)->dirty_info = NULL; 5812 kfree(dirty_i); 5813 } 5814 5815 static void destroy_curseg(struct f2fs_sb_info *sbi) 5816 { 5817 struct curseg_info *array = SM_I(sbi)->curseg_array; 5818 int i; 5819 5820 if (!array) 5821 return; 5822 SM_I(sbi)->curseg_array = NULL; 5823 for (i = 0; i < NR_CURSEG_TYPE; i++) { 5824 kfree(array[i].sum_blk); 5825 kfree(array[i].journal); 5826 } 5827 kfree(array); 5828 } 5829 5830 static void destroy_free_segmap(struct f2fs_sb_info *sbi) 5831 { 5832 struct free_segmap_info *free_i = SM_I(sbi)->free_info; 5833 5834 if (!free_i) 5835 return; 5836 SM_I(sbi)->free_info = NULL; 5837 kvfree(free_i->free_segmap); 5838 kvfree(free_i->free_secmap); 5839 kfree(free_i); 5840 } 5841 5842 static void destroy_sit_info(struct f2fs_sb_info *sbi) 5843 { 5844 struct sit_info *sit_i = SIT_I(sbi); 5845 5846 if (!sit_i) 5847 return; 5848 5849 if (sit_i->sentries) 5850 kvfree(sit_i->bitmap); 5851 kfree(sit_i->tmp_map); 5852 5853 kvfree(sit_i->sentries); 5854 kvfree(sit_i->sec_entries); 5855 kvfree(sit_i->dirty_sentries_bitmap); 5856 5857 SM_I(sbi)->sit_info = NULL; 5858 kfree(sit_i->sit_bitmap); 5859 #ifdef CONFIG_F2FS_CHECK_FS 5860 kfree(sit_i->sit_bitmap_mir); 5861 kvfree(sit_i->invalid_segmap); 5862 #endif 5863 kfree(sit_i); 5864 } 5865 5866 void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi) 5867 { 5868 struct f2fs_sm_info *sm_info = SM_I(sbi); 5869 5870 if (!sm_info) 5871 return; 5872 f2fs_destroy_flush_cmd_control(sbi, true); 5873 destroy_discard_cmd_control(sbi); 5874 destroy_dirty_segmap(sbi); 5875 destroy_curseg(sbi); 5876 destroy_free_segmap(sbi); 5877 destroy_sit_info(sbi); 5878 sbi->sm_info = NULL; 5879 kfree(sm_info); 5880 } 5881 5882 int __init f2fs_create_segment_manager_caches(void) 5883 { 5884 discard_entry_slab = f2fs_kmem_cache_create("f2fs_discard_entry", 5885 sizeof(struct discard_entry)); 5886 if (!discard_entry_slab) 5887 goto fail; 5888 5889 discard_cmd_slab = f2fs_kmem_cache_create("f2fs_discard_cmd", 5890 sizeof(struct discard_cmd)); 5891 if (!discard_cmd_slab) 5892 goto destroy_discard_entry; 5893 5894 sit_entry_set_slab = f2fs_kmem_cache_create("f2fs_sit_entry_set", 5895 sizeof(struct sit_entry_set)); 5896 if (!sit_entry_set_slab) 5897 goto destroy_discard_cmd; 5898 5899 revoke_entry_slab = f2fs_kmem_cache_create("f2fs_revoke_entry", 5900 sizeof(struct revoke_entry)); 5901 if (!revoke_entry_slab) 5902 goto destroy_sit_entry_set; 5903 return 0; 5904 5905 destroy_sit_entry_set: 5906 kmem_cache_destroy(sit_entry_set_slab); 5907 destroy_discard_cmd: 5908 kmem_cache_destroy(discard_cmd_slab); 5909 destroy_discard_entry: 5910 kmem_cache_destroy(discard_entry_slab); 5911 fail: 5912 return -ENOMEM; 5913 } 5914 5915 void f2fs_destroy_segment_manager_caches(void) 5916 { 5917 kmem_cache_destroy(sit_entry_set_slab); 5918 kmem_cache_destroy(discard_cmd_slab); 5919 kmem_cache_destroy(discard_entry_slab); 5920 kmem_cache_destroy(revoke_entry_slab); 5921 } 5922