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