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