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