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