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