1 /* 2 * fs/f2fs/segment.c 3 * 4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. 5 * http://www.samsung.com/ 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 #include <linux/bio.h> 14 #include <linux/blkdev.h> 15 #include <linux/prefetch.h> 16 #include <linux/kthread.h> 17 #include <linux/swap.h> 18 #include <linux/timer.h> 19 20 #include "f2fs.h" 21 #include "segment.h" 22 #include "node.h" 23 #include "trace.h" 24 #include <trace/events/f2fs.h> 25 26 #define __reverse_ffz(x) __reverse_ffs(~(x)) 27 28 static struct kmem_cache *discard_entry_slab; 29 static struct kmem_cache *discard_cmd_slab; 30 static struct kmem_cache *sit_entry_set_slab; 31 static struct kmem_cache *inmem_entry_slab; 32 33 static unsigned long __reverse_ulong(unsigned char *str) 34 { 35 unsigned long tmp = 0; 36 int shift = 24, idx = 0; 37 38 #if BITS_PER_LONG == 64 39 shift = 56; 40 #endif 41 while (shift >= 0) { 42 tmp |= (unsigned long)str[idx++] << shift; 43 shift -= BITS_PER_BYTE; 44 } 45 return tmp; 46 } 47 48 /* 49 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since 50 * MSB and LSB are reversed in a byte by f2fs_set_bit. 51 */ 52 static inline unsigned long __reverse_ffs(unsigned long word) 53 { 54 int num = 0; 55 56 #if BITS_PER_LONG == 64 57 if ((word & 0xffffffff00000000UL) == 0) 58 num += 32; 59 else 60 word >>= 32; 61 #endif 62 if ((word & 0xffff0000) == 0) 63 num += 16; 64 else 65 word >>= 16; 66 67 if ((word & 0xff00) == 0) 68 num += 8; 69 else 70 word >>= 8; 71 72 if ((word & 0xf0) == 0) 73 num += 4; 74 else 75 word >>= 4; 76 77 if ((word & 0xc) == 0) 78 num += 2; 79 else 80 word >>= 2; 81 82 if ((word & 0x2) == 0) 83 num += 1; 84 return num; 85 } 86 87 /* 88 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because 89 * f2fs_set_bit makes MSB and LSB reversed in a byte. 90 * @size must be integral times of unsigned long. 91 * Example: 92 * MSB <--> LSB 93 * f2fs_set_bit(0, bitmap) => 1000 0000 94 * f2fs_set_bit(7, bitmap) => 0000 0001 95 */ 96 static unsigned long __find_rev_next_bit(const unsigned long *addr, 97 unsigned long size, unsigned long offset) 98 { 99 const unsigned long *p = addr + BIT_WORD(offset); 100 unsigned long result = size; 101 unsigned long tmp; 102 103 if (offset >= size) 104 return size; 105 106 size -= (offset & ~(BITS_PER_LONG - 1)); 107 offset %= BITS_PER_LONG; 108 109 while (1) { 110 if (*p == 0) 111 goto pass; 112 113 tmp = __reverse_ulong((unsigned char *)p); 114 115 tmp &= ~0UL >> offset; 116 if (size < BITS_PER_LONG) 117 tmp &= (~0UL << (BITS_PER_LONG - size)); 118 if (tmp) 119 goto found; 120 pass: 121 if (size <= BITS_PER_LONG) 122 break; 123 size -= BITS_PER_LONG; 124 offset = 0; 125 p++; 126 } 127 return result; 128 found: 129 return result - size + __reverse_ffs(tmp); 130 } 131 132 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, 133 unsigned long size, unsigned long offset) 134 { 135 const unsigned long *p = addr + BIT_WORD(offset); 136 unsigned long result = size; 137 unsigned long tmp; 138 139 if (offset >= size) 140 return size; 141 142 size -= (offset & ~(BITS_PER_LONG - 1)); 143 offset %= BITS_PER_LONG; 144 145 while (1) { 146 if (*p == ~0UL) 147 goto pass; 148 149 tmp = __reverse_ulong((unsigned char *)p); 150 151 if (offset) 152 tmp |= ~0UL << (BITS_PER_LONG - offset); 153 if (size < BITS_PER_LONG) 154 tmp |= ~0UL >> size; 155 if (tmp != ~0UL) 156 goto found; 157 pass: 158 if (size <= BITS_PER_LONG) 159 break; 160 size -= BITS_PER_LONG; 161 offset = 0; 162 p++; 163 } 164 return result; 165 found: 166 return result - size + __reverse_ffz(tmp); 167 } 168 169 void register_inmem_page(struct inode *inode, struct page *page) 170 { 171 struct f2fs_inode_info *fi = F2FS_I(inode); 172 struct inmem_pages *new; 173 174 f2fs_trace_pid(page); 175 176 set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE); 177 SetPagePrivate(page); 178 179 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS); 180 181 /* add atomic page indices to the list */ 182 new->page = page; 183 INIT_LIST_HEAD(&new->list); 184 185 /* increase reference count with clean state */ 186 mutex_lock(&fi->inmem_lock); 187 get_page(page); 188 list_add_tail(&new->list, &fi->inmem_pages); 189 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); 190 mutex_unlock(&fi->inmem_lock); 191 192 trace_f2fs_register_inmem_page(page, INMEM); 193 } 194 195 static int __revoke_inmem_pages(struct inode *inode, 196 struct list_head *head, bool drop, bool recover) 197 { 198 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 199 struct inmem_pages *cur, *tmp; 200 int err = 0; 201 202 list_for_each_entry_safe(cur, tmp, head, list) { 203 struct page *page = cur->page; 204 205 if (drop) 206 trace_f2fs_commit_inmem_page(page, INMEM_DROP); 207 208 lock_page(page); 209 210 if (recover) { 211 struct dnode_of_data dn; 212 struct node_info ni; 213 214 trace_f2fs_commit_inmem_page(page, INMEM_REVOKE); 215 216 set_new_dnode(&dn, inode, NULL, NULL, 0); 217 if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) { 218 err = -EAGAIN; 219 goto next; 220 } 221 get_node_info(sbi, dn.nid, &ni); 222 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, 223 cur->old_addr, ni.version, true, true); 224 f2fs_put_dnode(&dn); 225 } 226 next: 227 /* we don't need to invalidate this in the sccessful status */ 228 if (drop || recover) 229 ClearPageUptodate(page); 230 set_page_private(page, 0); 231 ClearPagePrivate(page); 232 f2fs_put_page(page, 1); 233 234 list_del(&cur->list); 235 kmem_cache_free(inmem_entry_slab, cur); 236 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); 237 } 238 return err; 239 } 240 241 void drop_inmem_pages(struct inode *inode) 242 { 243 struct f2fs_inode_info *fi = F2FS_I(inode); 244 245 mutex_lock(&fi->inmem_lock); 246 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); 247 mutex_unlock(&fi->inmem_lock); 248 249 clear_inode_flag(inode, FI_ATOMIC_FILE); 250 stat_dec_atomic_write(inode); 251 } 252 253 void drop_inmem_page(struct inode *inode, struct page *page) 254 { 255 struct f2fs_inode_info *fi = F2FS_I(inode); 256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 257 struct list_head *head = &fi->inmem_pages; 258 struct inmem_pages *cur = NULL; 259 260 f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page)); 261 262 mutex_lock(&fi->inmem_lock); 263 list_for_each_entry(cur, head, list) { 264 if (cur->page == page) 265 break; 266 } 267 268 f2fs_bug_on(sbi, !cur || cur->page != page); 269 list_del(&cur->list); 270 mutex_unlock(&fi->inmem_lock); 271 272 dec_page_count(sbi, F2FS_INMEM_PAGES); 273 kmem_cache_free(inmem_entry_slab, cur); 274 275 ClearPageUptodate(page); 276 set_page_private(page, 0); 277 ClearPagePrivate(page); 278 f2fs_put_page(page, 0); 279 280 trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE); 281 } 282 283 static int __commit_inmem_pages(struct inode *inode, 284 struct list_head *revoke_list) 285 { 286 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 287 struct f2fs_inode_info *fi = F2FS_I(inode); 288 struct inmem_pages *cur, *tmp; 289 struct f2fs_io_info fio = { 290 .sbi = sbi, 291 .type = DATA, 292 .op = REQ_OP_WRITE, 293 .op_flags = REQ_SYNC | REQ_PRIO, 294 .encrypted_page = NULL, 295 }; 296 pgoff_t last_idx = ULONG_MAX; 297 int err = 0; 298 299 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { 300 struct page *page = cur->page; 301 302 lock_page(page); 303 if (page->mapping == inode->i_mapping) { 304 trace_f2fs_commit_inmem_page(page, INMEM); 305 306 set_page_dirty(page); 307 f2fs_wait_on_page_writeback(page, DATA, true); 308 if (clear_page_dirty_for_io(page)) { 309 inode_dec_dirty_pages(inode); 310 remove_dirty_inode(inode); 311 } 312 313 fio.page = page; 314 err = do_write_data_page(&fio); 315 if (err) { 316 unlock_page(page); 317 break; 318 } 319 320 /* record old blkaddr for revoking */ 321 cur->old_addr = fio.old_blkaddr; 322 last_idx = page->index; 323 } 324 unlock_page(page); 325 list_move_tail(&cur->list, revoke_list); 326 } 327 328 if (last_idx != ULONG_MAX) 329 f2fs_submit_merged_bio_cond(sbi, inode, 0, last_idx, 330 DATA, WRITE); 331 332 if (!err) 333 __revoke_inmem_pages(inode, revoke_list, false, false); 334 335 return err; 336 } 337 338 int commit_inmem_pages(struct inode *inode) 339 { 340 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 341 struct f2fs_inode_info *fi = F2FS_I(inode); 342 struct list_head revoke_list; 343 int err; 344 345 INIT_LIST_HEAD(&revoke_list); 346 f2fs_balance_fs(sbi, true); 347 f2fs_lock_op(sbi); 348 349 set_inode_flag(inode, FI_ATOMIC_COMMIT); 350 351 mutex_lock(&fi->inmem_lock); 352 err = __commit_inmem_pages(inode, &revoke_list); 353 if (err) { 354 int ret; 355 /* 356 * try to revoke all committed pages, but still we could fail 357 * due to no memory or other reason, if that happened, EAGAIN 358 * will be returned, which means in such case, transaction is 359 * already not integrity, caller should use journal to do the 360 * recovery or rewrite & commit last transaction. For other 361 * error number, revoking was done by filesystem itself. 362 */ 363 ret = __revoke_inmem_pages(inode, &revoke_list, false, true); 364 if (ret) 365 err = ret; 366 367 /* drop all uncommitted pages */ 368 __revoke_inmem_pages(inode, &fi->inmem_pages, true, false); 369 } 370 mutex_unlock(&fi->inmem_lock); 371 372 clear_inode_flag(inode, FI_ATOMIC_COMMIT); 373 374 f2fs_unlock_op(sbi); 375 return err; 376 } 377 378 /* 379 * This function balances dirty node and dentry pages. 380 * In addition, it controls garbage collection. 381 */ 382 void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need) 383 { 384 #ifdef CONFIG_F2FS_FAULT_INJECTION 385 if (time_to_inject(sbi, FAULT_CHECKPOINT)) { 386 f2fs_show_injection_info(FAULT_CHECKPOINT); 387 f2fs_stop_checkpoint(sbi, false); 388 } 389 #endif 390 391 if (!need) 392 return; 393 394 /* balance_fs_bg is able to be pending */ 395 if (excess_cached_nats(sbi)) 396 f2fs_balance_fs_bg(sbi); 397 398 /* 399 * We should do GC or end up with checkpoint, if there are so many dirty 400 * dir/node pages without enough free segments. 401 */ 402 if (has_not_enough_free_secs(sbi, 0, 0)) { 403 mutex_lock(&sbi->gc_mutex); 404 f2fs_gc(sbi, false, false); 405 } 406 } 407 408 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) 409 { 410 /* try to shrink extent cache when there is no enough memory */ 411 if (!available_free_memory(sbi, EXTENT_CACHE)) 412 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); 413 414 /* check the # of cached NAT entries */ 415 if (!available_free_memory(sbi, NAT_ENTRIES)) 416 try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK); 417 418 if (!available_free_memory(sbi, FREE_NIDS)) 419 try_to_free_nids(sbi, MAX_FREE_NIDS); 420 else 421 build_free_nids(sbi, false, false); 422 423 if (!is_idle(sbi)) 424 return; 425 426 /* checkpoint is the only way to shrink partial cached entries */ 427 if (!available_free_memory(sbi, NAT_ENTRIES) || 428 !available_free_memory(sbi, INO_ENTRIES) || 429 excess_prefree_segs(sbi) || 430 excess_dirty_nats(sbi) || 431 f2fs_time_over(sbi, CP_TIME)) { 432 if (test_opt(sbi, DATA_FLUSH)) { 433 struct blk_plug plug; 434 435 blk_start_plug(&plug); 436 sync_dirty_inodes(sbi, FILE_INODE); 437 blk_finish_plug(&plug); 438 } 439 f2fs_sync_fs(sbi->sb, true); 440 stat_inc_bg_cp_count(sbi->stat_info); 441 } 442 } 443 444 static int __submit_flush_wait(struct f2fs_sb_info *sbi, 445 struct block_device *bdev) 446 { 447 struct bio *bio = f2fs_bio_alloc(0); 448 int ret; 449 450 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH; 451 bio->bi_bdev = bdev; 452 ret = submit_bio_wait(bio); 453 bio_put(bio); 454 455 trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER), 456 test_opt(sbi, FLUSH_MERGE), ret); 457 return ret; 458 } 459 460 static int submit_flush_wait(struct f2fs_sb_info *sbi) 461 { 462 int ret = __submit_flush_wait(sbi, sbi->sb->s_bdev); 463 int i; 464 465 if (!sbi->s_ndevs || ret) 466 return ret; 467 468 for (i = 1; i < sbi->s_ndevs; i++) { 469 ret = __submit_flush_wait(sbi, FDEV(i).bdev); 470 if (ret) 471 break; 472 } 473 return ret; 474 } 475 476 static int issue_flush_thread(void *data) 477 { 478 struct f2fs_sb_info *sbi = data; 479 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 480 wait_queue_head_t *q = &fcc->flush_wait_queue; 481 repeat: 482 if (kthread_should_stop()) 483 return 0; 484 485 if (!llist_empty(&fcc->issue_list)) { 486 struct flush_cmd *cmd, *next; 487 int ret; 488 489 fcc->dispatch_list = llist_del_all(&fcc->issue_list); 490 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); 491 492 ret = submit_flush_wait(sbi); 493 atomic_inc(&fcc->issued_flush); 494 495 llist_for_each_entry_safe(cmd, next, 496 fcc->dispatch_list, llnode) { 497 cmd->ret = ret; 498 complete(&cmd->wait); 499 } 500 fcc->dispatch_list = NULL; 501 } 502 503 wait_event_interruptible(*q, 504 kthread_should_stop() || !llist_empty(&fcc->issue_list)); 505 goto repeat; 506 } 507 508 int f2fs_issue_flush(struct f2fs_sb_info *sbi) 509 { 510 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 511 struct flush_cmd cmd; 512 int ret; 513 514 if (test_opt(sbi, NOBARRIER)) 515 return 0; 516 517 if (!test_opt(sbi, FLUSH_MERGE)) { 518 ret = submit_flush_wait(sbi); 519 atomic_inc(&fcc->issued_flush); 520 return ret; 521 } 522 523 if (!atomic_read(&fcc->issing_flush)) { 524 atomic_inc(&fcc->issing_flush); 525 ret = submit_flush_wait(sbi); 526 atomic_dec(&fcc->issing_flush); 527 528 atomic_inc(&fcc->issued_flush); 529 return ret; 530 } 531 532 init_completion(&cmd.wait); 533 534 atomic_inc(&fcc->issing_flush); 535 llist_add(&cmd.llnode, &fcc->issue_list); 536 537 if (!fcc->dispatch_list) 538 wake_up(&fcc->flush_wait_queue); 539 540 if (fcc->f2fs_issue_flush) { 541 wait_for_completion(&cmd.wait); 542 atomic_dec(&fcc->issing_flush); 543 } else { 544 llist_del_all(&fcc->issue_list); 545 atomic_set(&fcc->issing_flush, 0); 546 } 547 548 return cmd.ret; 549 } 550 551 int create_flush_cmd_control(struct f2fs_sb_info *sbi) 552 { 553 dev_t dev = sbi->sb->s_bdev->bd_dev; 554 struct flush_cmd_control *fcc; 555 int err = 0; 556 557 if (SM_I(sbi)->fcc_info) { 558 fcc = SM_I(sbi)->fcc_info; 559 goto init_thread; 560 } 561 562 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL); 563 if (!fcc) 564 return -ENOMEM; 565 atomic_set(&fcc->issued_flush, 0); 566 atomic_set(&fcc->issing_flush, 0); 567 init_waitqueue_head(&fcc->flush_wait_queue); 568 init_llist_head(&fcc->issue_list); 569 SM_I(sbi)->fcc_info = fcc; 570 init_thread: 571 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, 572 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); 573 if (IS_ERR(fcc->f2fs_issue_flush)) { 574 err = PTR_ERR(fcc->f2fs_issue_flush); 575 kfree(fcc); 576 SM_I(sbi)->fcc_info = NULL; 577 return err; 578 } 579 580 return err; 581 } 582 583 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free) 584 { 585 struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info; 586 587 if (fcc && fcc->f2fs_issue_flush) { 588 struct task_struct *flush_thread = fcc->f2fs_issue_flush; 589 590 fcc->f2fs_issue_flush = NULL; 591 kthread_stop(flush_thread); 592 } 593 if (free) { 594 kfree(fcc); 595 SM_I(sbi)->fcc_info = NULL; 596 } 597 } 598 599 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 600 enum dirty_type dirty_type) 601 { 602 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 603 604 /* need not be added */ 605 if (IS_CURSEG(sbi, segno)) 606 return; 607 608 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) 609 dirty_i->nr_dirty[dirty_type]++; 610 611 if (dirty_type == DIRTY) { 612 struct seg_entry *sentry = get_seg_entry(sbi, segno); 613 enum dirty_type t = sentry->type; 614 615 if (unlikely(t >= DIRTY)) { 616 f2fs_bug_on(sbi, 1); 617 return; 618 } 619 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) 620 dirty_i->nr_dirty[t]++; 621 } 622 } 623 624 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 625 enum dirty_type dirty_type) 626 { 627 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 628 629 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) 630 dirty_i->nr_dirty[dirty_type]--; 631 632 if (dirty_type == DIRTY) { 633 struct seg_entry *sentry = get_seg_entry(sbi, segno); 634 enum dirty_type t = sentry->type; 635 636 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) 637 dirty_i->nr_dirty[t]--; 638 639 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0) 640 clear_bit(GET_SECNO(sbi, segno), 641 dirty_i->victim_secmap); 642 } 643 } 644 645 /* 646 * Should not occur error such as -ENOMEM. 647 * Adding dirty entry into seglist is not critical operation. 648 * If a given segment is one of current working segments, it won't be added. 649 */ 650 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) 651 { 652 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 653 unsigned short valid_blocks; 654 655 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) 656 return; 657 658 mutex_lock(&dirty_i->seglist_lock); 659 660 valid_blocks = get_valid_blocks(sbi, segno, 0); 661 662 if (valid_blocks == 0) { 663 __locate_dirty_segment(sbi, segno, PRE); 664 __remove_dirty_segment(sbi, segno, DIRTY); 665 } else if (valid_blocks < sbi->blocks_per_seg) { 666 __locate_dirty_segment(sbi, segno, DIRTY); 667 } else { 668 /* Recovery routine with SSR needs this */ 669 __remove_dirty_segment(sbi, segno, DIRTY); 670 } 671 672 mutex_unlock(&dirty_i->seglist_lock); 673 } 674 675 static void __add_discard_cmd(struct f2fs_sb_info *sbi, 676 struct block_device *bdev, block_t lstart, 677 block_t start, block_t len) 678 { 679 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 680 struct list_head *cmd_list = &(dcc->discard_cmd_list); 681 struct discard_cmd *dc; 682 683 dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS); 684 INIT_LIST_HEAD(&dc->list); 685 dc->bdev = bdev; 686 dc->lstart = lstart; 687 dc->start = start; 688 dc->len = len; 689 dc->state = D_PREP; 690 dc->error = 0; 691 init_completion(&dc->wait); 692 693 mutex_lock(&dcc->cmd_lock); 694 list_add_tail(&dc->list, cmd_list); 695 mutex_unlock(&dcc->cmd_lock); 696 } 697 698 static void __remove_discard_cmd(struct f2fs_sb_info *sbi, struct discard_cmd *dc) 699 { 700 if (dc->state == D_DONE) 701 atomic_dec(&(SM_I(sbi)->dcc_info->issing_discard)); 702 703 if (dc->error == -EOPNOTSUPP) 704 dc->error = 0; 705 706 if (dc->error) 707 f2fs_msg(sbi->sb, KERN_INFO, 708 "Issue discard failed, ret: %d", dc->error); 709 list_del(&dc->list); 710 kmem_cache_free(discard_cmd_slab, dc); 711 } 712 713 static void f2fs_submit_discard_endio(struct bio *bio) 714 { 715 struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private; 716 717 complete(&dc->wait); 718 dc->error = bio->bi_error; 719 dc->state = D_DONE; 720 bio_put(bio); 721 } 722 723 /* this function is copied from blkdev_issue_discard from block/blk-lib.c */ 724 static void __submit_discard_cmd(struct f2fs_sb_info *sbi, 725 struct discard_cmd *dc) 726 { 727 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 728 struct bio *bio = NULL; 729 730 if (dc->state != D_PREP) 731 return; 732 733 dc->error = __blkdev_issue_discard(dc->bdev, 734 SECTOR_FROM_BLOCK(dc->start), 735 SECTOR_FROM_BLOCK(dc->len), 736 GFP_NOFS, 0, &bio); 737 if (!dc->error) { 738 /* should keep before submission to avoid D_DONE right away */ 739 dc->state = D_SUBMIT; 740 atomic_inc(&dcc->issued_discard); 741 atomic_inc(&dcc->issing_discard); 742 if (bio) { 743 bio->bi_private = dc; 744 bio->bi_end_io = f2fs_submit_discard_endio; 745 bio->bi_opf |= REQ_SYNC; 746 submit_bio(bio); 747 } 748 } else { 749 __remove_discard_cmd(sbi, dc); 750 } 751 } 752 753 static int __queue_discard_cmd(struct f2fs_sb_info *sbi, 754 struct block_device *bdev, block_t blkstart, block_t blklen) 755 { 756 block_t lblkstart = blkstart; 757 758 trace_f2fs_issue_discard(bdev, blkstart, blklen); 759 760 if (sbi->s_ndevs) { 761 int devi = f2fs_target_device_index(sbi, blkstart); 762 763 blkstart -= FDEV(devi).start_blk; 764 } 765 __add_discard_cmd(sbi, bdev, lblkstart, blkstart, blklen); 766 wake_up(&SM_I(sbi)->dcc_info->discard_wait_queue); 767 return 0; 768 } 769 770 static void __punch_discard_cmd(struct f2fs_sb_info *sbi, 771 struct discard_cmd *dc, block_t blkaddr) 772 { 773 block_t end_block = START_BLOCK(sbi, GET_SEGNO(sbi, blkaddr) + 1); 774 775 if (dc->state == D_DONE || dc->lstart + dc->len <= end_block) { 776 __remove_discard_cmd(sbi, dc); 777 return; 778 } 779 780 if (blkaddr - dc->lstart < dc->lstart + dc->len - end_block) { 781 dc->start += (end_block - dc->lstart); 782 dc->len -= (end_block - dc->lstart); 783 dc->lstart = end_block; 784 } else { 785 dc->len = blkaddr - dc->lstart; 786 } 787 } 788 789 /* This should be covered by global mutex, &sit_i->sentry_lock */ 790 void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr) 791 { 792 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 793 struct list_head *wait_list = &(dcc->discard_cmd_list); 794 struct discard_cmd *dc, *tmp; 795 struct blk_plug plug; 796 797 mutex_lock(&dcc->cmd_lock); 798 799 blk_start_plug(&plug); 800 801 list_for_each_entry_safe(dc, tmp, wait_list, list) { 802 803 if (blkaddr == NULL_ADDR) { 804 __submit_discard_cmd(sbi, dc); 805 continue; 806 } 807 808 if (dc->lstart <= blkaddr && blkaddr < dc->lstart + dc->len) { 809 if (dc->state == D_SUBMIT) 810 wait_for_completion_io(&dc->wait); 811 __punch_discard_cmd(sbi, dc, blkaddr); 812 } 813 } 814 blk_finish_plug(&plug); 815 816 /* this comes from f2fs_put_super */ 817 if (blkaddr == NULL_ADDR) { 818 list_for_each_entry_safe(dc, tmp, wait_list, list) { 819 wait_for_completion_io(&dc->wait); 820 __remove_discard_cmd(sbi, dc); 821 } 822 } 823 mutex_unlock(&dcc->cmd_lock); 824 } 825 826 static int issue_discard_thread(void *data) 827 { 828 struct f2fs_sb_info *sbi = data; 829 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 830 wait_queue_head_t *q = &dcc->discard_wait_queue; 831 struct list_head *cmd_list = &dcc->discard_cmd_list; 832 struct discard_cmd *dc, *tmp; 833 struct blk_plug plug; 834 int iter = 0; 835 repeat: 836 if (kthread_should_stop()) 837 return 0; 838 839 blk_start_plug(&plug); 840 841 mutex_lock(&dcc->cmd_lock); 842 list_for_each_entry_safe(dc, tmp, cmd_list, list) { 843 844 if (is_idle(sbi)) 845 __submit_discard_cmd(sbi, dc); 846 847 if (dc->state == D_PREP && iter++ > DISCARD_ISSUE_RATE) 848 break; 849 if (dc->state == D_DONE) 850 __remove_discard_cmd(sbi, dc); 851 } 852 mutex_unlock(&dcc->cmd_lock); 853 854 blk_finish_plug(&plug); 855 856 iter = 0; 857 congestion_wait(BLK_RW_SYNC, HZ/50); 858 859 wait_event_interruptible(*q, 860 kthread_should_stop() || !list_empty(&dcc->discard_cmd_list)); 861 goto repeat; 862 } 863 864 #ifdef CONFIG_BLK_DEV_ZONED 865 static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi, 866 struct block_device *bdev, block_t blkstart, block_t blklen) 867 { 868 sector_t sector, nr_sects; 869 block_t lblkstart = blkstart; 870 int devi = 0; 871 872 if (sbi->s_ndevs) { 873 devi = f2fs_target_device_index(sbi, blkstart); 874 blkstart -= FDEV(devi).start_blk; 875 } 876 877 /* 878 * We need to know the type of the zone: for conventional zones, 879 * use regular discard if the drive supports it. For sequential 880 * zones, reset the zone write pointer. 881 */ 882 switch (get_blkz_type(sbi, bdev, blkstart)) { 883 884 case BLK_ZONE_TYPE_CONVENTIONAL: 885 if (!blk_queue_discard(bdev_get_queue(bdev))) 886 return 0; 887 return __queue_discard_cmd(sbi, bdev, lblkstart, blklen); 888 case BLK_ZONE_TYPE_SEQWRITE_REQ: 889 case BLK_ZONE_TYPE_SEQWRITE_PREF: 890 sector = SECTOR_FROM_BLOCK(blkstart); 891 nr_sects = SECTOR_FROM_BLOCK(blklen); 892 893 if (sector & (bdev_zone_sectors(bdev) - 1) || 894 nr_sects != bdev_zone_sectors(bdev)) { 895 f2fs_msg(sbi->sb, KERN_INFO, 896 "(%d) %s: Unaligned discard attempted (block %x + %x)", 897 devi, sbi->s_ndevs ? FDEV(devi).path: "", 898 blkstart, blklen); 899 return -EIO; 900 } 901 trace_f2fs_issue_reset_zone(bdev, blkstart); 902 return blkdev_reset_zones(bdev, sector, 903 nr_sects, GFP_NOFS); 904 default: 905 /* Unknown zone type: broken device ? */ 906 return -EIO; 907 } 908 } 909 #endif 910 911 static int __issue_discard_async(struct f2fs_sb_info *sbi, 912 struct block_device *bdev, block_t blkstart, block_t blklen) 913 { 914 #ifdef CONFIG_BLK_DEV_ZONED 915 if (f2fs_sb_mounted_blkzoned(sbi->sb) && 916 bdev_zoned_model(bdev) != BLK_ZONED_NONE) 917 return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen); 918 #endif 919 return __queue_discard_cmd(sbi, bdev, blkstart, blklen); 920 } 921 922 static int f2fs_issue_discard(struct f2fs_sb_info *sbi, 923 block_t blkstart, block_t blklen) 924 { 925 sector_t start = blkstart, len = 0; 926 struct block_device *bdev; 927 struct seg_entry *se; 928 unsigned int offset; 929 block_t i; 930 int err = 0; 931 932 bdev = f2fs_target_device(sbi, blkstart, NULL); 933 934 for (i = blkstart; i < blkstart + blklen; i++, len++) { 935 if (i != start) { 936 struct block_device *bdev2 = 937 f2fs_target_device(sbi, i, NULL); 938 939 if (bdev2 != bdev) { 940 err = __issue_discard_async(sbi, bdev, 941 start, len); 942 if (err) 943 return err; 944 bdev = bdev2; 945 start = i; 946 len = 0; 947 } 948 } 949 950 se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); 951 offset = GET_BLKOFF_FROM_SEG0(sbi, i); 952 953 if (!f2fs_test_and_set_bit(offset, se->discard_map)) 954 sbi->discard_blks--; 955 } 956 957 if (len) 958 err = __issue_discard_async(sbi, bdev, start, len); 959 return err; 960 } 961 962 static void __add_discard_entry(struct f2fs_sb_info *sbi, 963 struct cp_control *cpc, struct seg_entry *se, 964 unsigned int start, unsigned int end) 965 { 966 struct list_head *head = &SM_I(sbi)->dcc_info->discard_entry_list; 967 struct discard_entry *new, *last; 968 969 if (!list_empty(head)) { 970 last = list_last_entry(head, struct discard_entry, list); 971 if (START_BLOCK(sbi, cpc->trim_start) + start == 972 last->blkaddr + last->len && 973 last->len < MAX_DISCARD_BLOCKS(sbi)) { 974 last->len += end - start; 975 goto done; 976 } 977 } 978 979 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS); 980 INIT_LIST_HEAD(&new->list); 981 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start; 982 new->len = end - start; 983 list_add_tail(&new->list, head); 984 done: 985 SM_I(sbi)->dcc_info->nr_discards += end - start; 986 } 987 988 static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc, 989 bool check_only) 990 { 991 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 992 int max_blocks = sbi->blocks_per_seg; 993 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); 994 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 995 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 996 unsigned long *discard_map = (unsigned long *)se->discard_map; 997 unsigned long *dmap = SIT_I(sbi)->tmp_map; 998 unsigned int start = 0, end = -1; 999 bool force = (cpc->reason == CP_DISCARD); 1000 int i; 1001 1002 if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi)) 1003 return false; 1004 1005 if (!force) { 1006 if (!test_opt(sbi, DISCARD) || !se->valid_blocks || 1007 SM_I(sbi)->dcc_info->nr_discards >= 1008 SM_I(sbi)->dcc_info->max_discards) 1009 return false; 1010 } 1011 1012 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ 1013 for (i = 0; i < entries; i++) 1014 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : 1015 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; 1016 1017 while (force || SM_I(sbi)->dcc_info->nr_discards <= 1018 SM_I(sbi)->dcc_info->max_discards) { 1019 start = __find_rev_next_bit(dmap, max_blocks, end + 1); 1020 if (start >= max_blocks) 1021 break; 1022 1023 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); 1024 if (force && start && end != max_blocks 1025 && (end - start) < cpc->trim_minlen) 1026 continue; 1027 1028 if (check_only) 1029 return true; 1030 1031 __add_discard_entry(sbi, cpc, se, start, end); 1032 } 1033 return false; 1034 } 1035 1036 void release_discard_addrs(struct f2fs_sb_info *sbi) 1037 { 1038 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list); 1039 struct discard_entry *entry, *this; 1040 1041 /* drop caches */ 1042 list_for_each_entry_safe(entry, this, head, list) { 1043 list_del(&entry->list); 1044 kmem_cache_free(discard_entry_slab, entry); 1045 } 1046 } 1047 1048 /* 1049 * Should call clear_prefree_segments after checkpoint is done. 1050 */ 1051 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) 1052 { 1053 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1054 unsigned int segno; 1055 1056 mutex_lock(&dirty_i->seglist_lock); 1057 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) 1058 __set_test_and_free(sbi, segno); 1059 mutex_unlock(&dirty_i->seglist_lock); 1060 } 1061 1062 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1063 { 1064 struct list_head *head = &(SM_I(sbi)->dcc_info->discard_entry_list); 1065 struct discard_entry *entry, *this; 1066 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1067 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; 1068 unsigned int start = 0, end = -1; 1069 unsigned int secno, start_segno; 1070 bool force = (cpc->reason == CP_DISCARD); 1071 1072 mutex_lock(&dirty_i->seglist_lock); 1073 1074 while (1) { 1075 int i; 1076 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); 1077 if (start >= MAIN_SEGS(sbi)) 1078 break; 1079 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), 1080 start + 1); 1081 1082 for (i = start; i < end; i++) 1083 clear_bit(i, prefree_map); 1084 1085 dirty_i->nr_dirty[PRE] -= end - start; 1086 1087 if (!test_opt(sbi, DISCARD)) 1088 continue; 1089 1090 if (force && start >= cpc->trim_start && 1091 (end - 1) <= cpc->trim_end) 1092 continue; 1093 1094 if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) { 1095 f2fs_issue_discard(sbi, START_BLOCK(sbi, start), 1096 (end - start) << sbi->log_blocks_per_seg); 1097 continue; 1098 } 1099 next: 1100 secno = GET_SECNO(sbi, start); 1101 start_segno = secno * sbi->segs_per_sec; 1102 if (!IS_CURSEC(sbi, secno) && 1103 !get_valid_blocks(sbi, start, sbi->segs_per_sec)) 1104 f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno), 1105 sbi->segs_per_sec << sbi->log_blocks_per_seg); 1106 1107 start = start_segno + sbi->segs_per_sec; 1108 if (start < end) 1109 goto next; 1110 else 1111 end = start - 1; 1112 } 1113 mutex_unlock(&dirty_i->seglist_lock); 1114 1115 /* send small discards */ 1116 list_for_each_entry_safe(entry, this, head, list) { 1117 if (force && entry->len < cpc->trim_minlen) 1118 goto skip; 1119 f2fs_issue_discard(sbi, entry->blkaddr, entry->len); 1120 cpc->trimmed += entry->len; 1121 skip: 1122 list_del(&entry->list); 1123 SM_I(sbi)->dcc_info->nr_discards -= entry->len; 1124 kmem_cache_free(discard_entry_slab, entry); 1125 } 1126 } 1127 1128 static int create_discard_cmd_control(struct f2fs_sb_info *sbi) 1129 { 1130 dev_t dev = sbi->sb->s_bdev->bd_dev; 1131 struct discard_cmd_control *dcc; 1132 int err = 0; 1133 1134 if (SM_I(sbi)->dcc_info) { 1135 dcc = SM_I(sbi)->dcc_info; 1136 goto init_thread; 1137 } 1138 1139 dcc = kzalloc(sizeof(struct discard_cmd_control), GFP_KERNEL); 1140 if (!dcc) 1141 return -ENOMEM; 1142 1143 INIT_LIST_HEAD(&dcc->discard_entry_list); 1144 INIT_LIST_HEAD(&dcc->discard_cmd_list); 1145 mutex_init(&dcc->cmd_lock); 1146 atomic_set(&dcc->issued_discard, 0); 1147 atomic_set(&dcc->issing_discard, 0); 1148 dcc->nr_discards = 0; 1149 dcc->max_discards = 0; 1150 1151 init_waitqueue_head(&dcc->discard_wait_queue); 1152 SM_I(sbi)->dcc_info = dcc; 1153 init_thread: 1154 dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi, 1155 "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev)); 1156 if (IS_ERR(dcc->f2fs_issue_discard)) { 1157 err = PTR_ERR(dcc->f2fs_issue_discard); 1158 kfree(dcc); 1159 SM_I(sbi)->dcc_info = NULL; 1160 return err; 1161 } 1162 1163 return err; 1164 } 1165 1166 static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi, bool free) 1167 { 1168 struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info; 1169 1170 if (dcc && dcc->f2fs_issue_discard) { 1171 struct task_struct *discard_thread = dcc->f2fs_issue_discard; 1172 1173 dcc->f2fs_issue_discard = NULL; 1174 kthread_stop(discard_thread); 1175 } 1176 if (free) { 1177 kfree(dcc); 1178 SM_I(sbi)->dcc_info = NULL; 1179 } 1180 } 1181 1182 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) 1183 { 1184 struct sit_info *sit_i = SIT_I(sbi); 1185 1186 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { 1187 sit_i->dirty_sentries++; 1188 return false; 1189 } 1190 1191 return true; 1192 } 1193 1194 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, 1195 unsigned int segno, int modified) 1196 { 1197 struct seg_entry *se = get_seg_entry(sbi, segno); 1198 se->type = type; 1199 if (modified) 1200 __mark_sit_entry_dirty(sbi, segno); 1201 } 1202 1203 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) 1204 { 1205 struct seg_entry *se; 1206 unsigned int segno, offset; 1207 long int new_vblocks; 1208 1209 segno = GET_SEGNO(sbi, blkaddr); 1210 1211 se = get_seg_entry(sbi, segno); 1212 new_vblocks = se->valid_blocks + del; 1213 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 1214 1215 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) || 1216 (new_vblocks > sbi->blocks_per_seg))); 1217 1218 se->valid_blocks = new_vblocks; 1219 se->mtime = get_mtime(sbi); 1220 SIT_I(sbi)->max_mtime = se->mtime; 1221 1222 /* Update valid block bitmap */ 1223 if (del > 0) { 1224 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) { 1225 #ifdef CONFIG_F2FS_CHECK_FS 1226 if (f2fs_test_and_set_bit(offset, 1227 se->cur_valid_map_mir)) 1228 f2fs_bug_on(sbi, 1); 1229 else 1230 WARN_ON(1); 1231 #else 1232 f2fs_bug_on(sbi, 1); 1233 #endif 1234 } 1235 if (f2fs_discard_en(sbi) && 1236 !f2fs_test_and_set_bit(offset, se->discard_map)) 1237 sbi->discard_blks--; 1238 1239 /* don't overwrite by SSR to keep node chain */ 1240 if (se->type == CURSEG_WARM_NODE) { 1241 if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map)) 1242 se->ckpt_valid_blocks++; 1243 } 1244 } else { 1245 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) { 1246 #ifdef CONFIG_F2FS_CHECK_FS 1247 if (!f2fs_test_and_clear_bit(offset, 1248 se->cur_valid_map_mir)) 1249 f2fs_bug_on(sbi, 1); 1250 else 1251 WARN_ON(1); 1252 #else 1253 f2fs_bug_on(sbi, 1); 1254 #endif 1255 } 1256 if (f2fs_discard_en(sbi) && 1257 f2fs_test_and_clear_bit(offset, se->discard_map)) 1258 sbi->discard_blks++; 1259 } 1260 if (!f2fs_test_bit(offset, se->ckpt_valid_map)) 1261 se->ckpt_valid_blocks += del; 1262 1263 __mark_sit_entry_dirty(sbi, segno); 1264 1265 /* update total number of valid blocks to be written in ckpt area */ 1266 SIT_I(sbi)->written_valid_blocks += del; 1267 1268 if (sbi->segs_per_sec > 1) 1269 get_sec_entry(sbi, segno)->valid_blocks += del; 1270 } 1271 1272 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new) 1273 { 1274 update_sit_entry(sbi, new, 1); 1275 if (GET_SEGNO(sbi, old) != NULL_SEGNO) 1276 update_sit_entry(sbi, old, -1); 1277 1278 locate_dirty_segment(sbi, GET_SEGNO(sbi, old)); 1279 locate_dirty_segment(sbi, GET_SEGNO(sbi, new)); 1280 } 1281 1282 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) 1283 { 1284 unsigned int segno = GET_SEGNO(sbi, addr); 1285 struct sit_info *sit_i = SIT_I(sbi); 1286 1287 f2fs_bug_on(sbi, addr == NULL_ADDR); 1288 if (addr == NEW_ADDR) 1289 return; 1290 1291 /* add it into sit main buffer */ 1292 mutex_lock(&sit_i->sentry_lock); 1293 1294 update_sit_entry(sbi, addr, -1); 1295 1296 /* add it into dirty seglist */ 1297 locate_dirty_segment(sbi, segno); 1298 1299 mutex_unlock(&sit_i->sentry_lock); 1300 } 1301 1302 bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr) 1303 { 1304 struct sit_info *sit_i = SIT_I(sbi); 1305 unsigned int segno, offset; 1306 struct seg_entry *se; 1307 bool is_cp = false; 1308 1309 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) 1310 return true; 1311 1312 mutex_lock(&sit_i->sentry_lock); 1313 1314 segno = GET_SEGNO(sbi, blkaddr); 1315 se = get_seg_entry(sbi, segno); 1316 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 1317 1318 if (f2fs_test_bit(offset, se->ckpt_valid_map)) 1319 is_cp = true; 1320 1321 mutex_unlock(&sit_i->sentry_lock); 1322 1323 return is_cp; 1324 } 1325 1326 /* 1327 * This function should be resided under the curseg_mutex lock 1328 */ 1329 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, 1330 struct f2fs_summary *sum) 1331 { 1332 struct curseg_info *curseg = CURSEG_I(sbi, type); 1333 void *addr = curseg->sum_blk; 1334 addr += curseg->next_blkoff * sizeof(struct f2fs_summary); 1335 memcpy(addr, sum, sizeof(struct f2fs_summary)); 1336 } 1337 1338 /* 1339 * Calculate the number of current summary pages for writing 1340 */ 1341 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) 1342 { 1343 int valid_sum_count = 0; 1344 int i, sum_in_page; 1345 1346 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 1347 if (sbi->ckpt->alloc_type[i] == SSR) 1348 valid_sum_count += sbi->blocks_per_seg; 1349 else { 1350 if (for_ra) 1351 valid_sum_count += le16_to_cpu( 1352 F2FS_CKPT(sbi)->cur_data_blkoff[i]); 1353 else 1354 valid_sum_count += curseg_blkoff(sbi, i); 1355 } 1356 } 1357 1358 sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE - 1359 SUM_FOOTER_SIZE) / SUMMARY_SIZE; 1360 if (valid_sum_count <= sum_in_page) 1361 return 1; 1362 else if ((valid_sum_count - sum_in_page) <= 1363 (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) 1364 return 2; 1365 return 3; 1366 } 1367 1368 /* 1369 * Caller should put this summary page 1370 */ 1371 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) 1372 { 1373 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno)); 1374 } 1375 1376 void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr) 1377 { 1378 struct page *page = grab_meta_page(sbi, blk_addr); 1379 void *dst = page_address(page); 1380 1381 if (src) 1382 memcpy(dst, src, PAGE_SIZE); 1383 else 1384 memset(dst, 0, PAGE_SIZE); 1385 set_page_dirty(page); 1386 f2fs_put_page(page, 1); 1387 } 1388 1389 static void write_sum_page(struct f2fs_sb_info *sbi, 1390 struct f2fs_summary_block *sum_blk, block_t blk_addr) 1391 { 1392 update_meta_page(sbi, (void *)sum_blk, blk_addr); 1393 } 1394 1395 static void write_current_sum_page(struct f2fs_sb_info *sbi, 1396 int type, block_t blk_addr) 1397 { 1398 struct curseg_info *curseg = CURSEG_I(sbi, type); 1399 struct page *page = grab_meta_page(sbi, blk_addr); 1400 struct f2fs_summary_block *src = curseg->sum_blk; 1401 struct f2fs_summary_block *dst; 1402 1403 dst = (struct f2fs_summary_block *)page_address(page); 1404 1405 mutex_lock(&curseg->curseg_mutex); 1406 1407 down_read(&curseg->journal_rwsem); 1408 memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE); 1409 up_read(&curseg->journal_rwsem); 1410 1411 memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE); 1412 memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE); 1413 1414 mutex_unlock(&curseg->curseg_mutex); 1415 1416 set_page_dirty(page); 1417 f2fs_put_page(page, 1); 1418 } 1419 1420 /* 1421 * Find a new segment from the free segments bitmap to right order 1422 * This function should be returned with success, otherwise BUG 1423 */ 1424 static void get_new_segment(struct f2fs_sb_info *sbi, 1425 unsigned int *newseg, bool new_sec, int dir) 1426 { 1427 struct free_segmap_info *free_i = FREE_I(sbi); 1428 unsigned int segno, secno, zoneno; 1429 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; 1430 unsigned int hint = *newseg / sbi->segs_per_sec; 1431 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg); 1432 unsigned int left_start = hint; 1433 bool init = true; 1434 int go_left = 0; 1435 int i; 1436 1437 spin_lock(&free_i->segmap_lock); 1438 1439 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { 1440 segno = find_next_zero_bit(free_i->free_segmap, 1441 (hint + 1) * sbi->segs_per_sec, *newseg + 1); 1442 if (segno < (hint + 1) * sbi->segs_per_sec) 1443 goto got_it; 1444 } 1445 find_other_zone: 1446 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); 1447 if (secno >= MAIN_SECS(sbi)) { 1448 if (dir == ALLOC_RIGHT) { 1449 secno = find_next_zero_bit(free_i->free_secmap, 1450 MAIN_SECS(sbi), 0); 1451 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); 1452 } else { 1453 go_left = 1; 1454 left_start = hint - 1; 1455 } 1456 } 1457 if (go_left == 0) 1458 goto skip_left; 1459 1460 while (test_bit(left_start, free_i->free_secmap)) { 1461 if (left_start > 0) { 1462 left_start--; 1463 continue; 1464 } 1465 left_start = find_next_zero_bit(free_i->free_secmap, 1466 MAIN_SECS(sbi), 0); 1467 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); 1468 break; 1469 } 1470 secno = left_start; 1471 skip_left: 1472 hint = secno; 1473 segno = secno * sbi->segs_per_sec; 1474 zoneno = secno / sbi->secs_per_zone; 1475 1476 /* give up on finding another zone */ 1477 if (!init) 1478 goto got_it; 1479 if (sbi->secs_per_zone == 1) 1480 goto got_it; 1481 if (zoneno == old_zoneno) 1482 goto got_it; 1483 if (dir == ALLOC_LEFT) { 1484 if (!go_left && zoneno + 1 >= total_zones) 1485 goto got_it; 1486 if (go_left && zoneno == 0) 1487 goto got_it; 1488 } 1489 for (i = 0; i < NR_CURSEG_TYPE; i++) 1490 if (CURSEG_I(sbi, i)->zone == zoneno) 1491 break; 1492 1493 if (i < NR_CURSEG_TYPE) { 1494 /* zone is in user, try another */ 1495 if (go_left) 1496 hint = zoneno * sbi->secs_per_zone - 1; 1497 else if (zoneno + 1 >= total_zones) 1498 hint = 0; 1499 else 1500 hint = (zoneno + 1) * sbi->secs_per_zone; 1501 init = false; 1502 goto find_other_zone; 1503 } 1504 got_it: 1505 /* set it as dirty segment in free segmap */ 1506 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); 1507 __set_inuse(sbi, segno); 1508 *newseg = segno; 1509 spin_unlock(&free_i->segmap_lock); 1510 } 1511 1512 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) 1513 { 1514 struct curseg_info *curseg = CURSEG_I(sbi, type); 1515 struct summary_footer *sum_footer; 1516 1517 curseg->segno = curseg->next_segno; 1518 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno); 1519 curseg->next_blkoff = 0; 1520 curseg->next_segno = NULL_SEGNO; 1521 1522 sum_footer = &(curseg->sum_blk->footer); 1523 memset(sum_footer, 0, sizeof(struct summary_footer)); 1524 if (IS_DATASEG(type)) 1525 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); 1526 if (IS_NODESEG(type)) 1527 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); 1528 __set_sit_entry_type(sbi, type, curseg->segno, modified); 1529 } 1530 1531 static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type) 1532 { 1533 if (type == CURSEG_HOT_DATA || IS_NODESEG(type)) 1534 return 0; 1535 1536 return CURSEG_I(sbi, type)->segno; 1537 } 1538 1539 /* 1540 * Allocate a current working segment. 1541 * This function always allocates a free segment in LFS manner. 1542 */ 1543 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) 1544 { 1545 struct curseg_info *curseg = CURSEG_I(sbi, type); 1546 unsigned int segno = curseg->segno; 1547 int dir = ALLOC_LEFT; 1548 1549 write_sum_page(sbi, curseg->sum_blk, 1550 GET_SUM_BLOCK(sbi, segno)); 1551 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) 1552 dir = ALLOC_RIGHT; 1553 1554 if (test_opt(sbi, NOHEAP)) 1555 dir = ALLOC_RIGHT; 1556 1557 segno = __get_next_segno(sbi, type); 1558 get_new_segment(sbi, &segno, new_sec, dir); 1559 curseg->next_segno = segno; 1560 reset_curseg(sbi, type, 1); 1561 curseg->alloc_type = LFS; 1562 } 1563 1564 static void __next_free_blkoff(struct f2fs_sb_info *sbi, 1565 struct curseg_info *seg, block_t start) 1566 { 1567 struct seg_entry *se = get_seg_entry(sbi, seg->segno); 1568 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 1569 unsigned long *target_map = SIT_I(sbi)->tmp_map; 1570 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 1571 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 1572 int i, pos; 1573 1574 for (i = 0; i < entries; i++) 1575 target_map[i] = ckpt_map[i] | cur_map[i]; 1576 1577 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); 1578 1579 seg->next_blkoff = pos; 1580 } 1581 1582 /* 1583 * If a segment is written by LFS manner, next block offset is just obtained 1584 * by increasing the current block offset. However, if a segment is written by 1585 * SSR manner, next block offset obtained by calling __next_free_blkoff 1586 */ 1587 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, 1588 struct curseg_info *seg) 1589 { 1590 if (seg->alloc_type == SSR) 1591 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); 1592 else 1593 seg->next_blkoff++; 1594 } 1595 1596 /* 1597 * This function always allocates a used segment(from dirty seglist) by SSR 1598 * manner, so it should recover the existing segment information of valid blocks 1599 */ 1600 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) 1601 { 1602 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 1603 struct curseg_info *curseg = CURSEG_I(sbi, type); 1604 unsigned int new_segno = curseg->next_segno; 1605 struct f2fs_summary_block *sum_node; 1606 struct page *sum_page; 1607 1608 write_sum_page(sbi, curseg->sum_blk, 1609 GET_SUM_BLOCK(sbi, curseg->segno)); 1610 __set_test_and_inuse(sbi, new_segno); 1611 1612 mutex_lock(&dirty_i->seglist_lock); 1613 __remove_dirty_segment(sbi, new_segno, PRE); 1614 __remove_dirty_segment(sbi, new_segno, DIRTY); 1615 mutex_unlock(&dirty_i->seglist_lock); 1616 1617 reset_curseg(sbi, type, 1); 1618 curseg->alloc_type = SSR; 1619 __next_free_blkoff(sbi, curseg, 0); 1620 1621 if (reuse) { 1622 sum_page = get_sum_page(sbi, new_segno); 1623 sum_node = (struct f2fs_summary_block *)page_address(sum_page); 1624 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); 1625 f2fs_put_page(sum_page, 1); 1626 } 1627 } 1628 1629 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) 1630 { 1631 struct curseg_info *curseg = CURSEG_I(sbi, type); 1632 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; 1633 int i, cnt; 1634 bool reversed = false; 1635 1636 /* need_SSR() already forces to do this */ 1637 if (v_ops->get_victim(sbi, &(curseg)->next_segno, BG_GC, type, SSR)) 1638 return 1; 1639 1640 /* For node segments, let's do SSR more intensively */ 1641 if (IS_NODESEG(type)) { 1642 if (type >= CURSEG_WARM_NODE) { 1643 reversed = true; 1644 i = CURSEG_COLD_NODE; 1645 } else { 1646 i = CURSEG_HOT_NODE; 1647 } 1648 cnt = NR_CURSEG_NODE_TYPE; 1649 } else { 1650 if (type >= CURSEG_WARM_DATA) { 1651 reversed = true; 1652 i = CURSEG_COLD_DATA; 1653 } else { 1654 i = CURSEG_HOT_DATA; 1655 } 1656 cnt = NR_CURSEG_DATA_TYPE; 1657 } 1658 1659 for (; cnt-- > 0; reversed ? i-- : i++) { 1660 if (i == type) 1661 continue; 1662 if (v_ops->get_victim(sbi, &(curseg)->next_segno, 1663 BG_GC, i, SSR)) 1664 return 1; 1665 } 1666 return 0; 1667 } 1668 1669 /* 1670 * flush out current segment and replace it with new segment 1671 * This function should be returned with success, otherwise BUG 1672 */ 1673 static void allocate_segment_by_default(struct f2fs_sb_info *sbi, 1674 int type, bool force) 1675 { 1676 if (force) 1677 new_curseg(sbi, type, true); 1678 else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) && 1679 type == CURSEG_WARM_NODE) 1680 new_curseg(sbi, type, false); 1681 else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) 1682 change_curseg(sbi, type, true); 1683 else 1684 new_curseg(sbi, type, false); 1685 1686 stat_inc_seg_type(sbi, CURSEG_I(sbi, type)); 1687 } 1688 1689 void allocate_new_segments(struct f2fs_sb_info *sbi) 1690 { 1691 struct curseg_info *curseg; 1692 unsigned int old_segno; 1693 int i; 1694 1695 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 1696 curseg = CURSEG_I(sbi, i); 1697 old_segno = curseg->segno; 1698 SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true); 1699 locate_dirty_segment(sbi, old_segno); 1700 } 1701 } 1702 1703 static const struct segment_allocation default_salloc_ops = { 1704 .allocate_segment = allocate_segment_by_default, 1705 }; 1706 1707 bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1708 { 1709 __u64 trim_start = cpc->trim_start; 1710 bool has_candidate = false; 1711 1712 mutex_lock(&SIT_I(sbi)->sentry_lock); 1713 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) { 1714 if (add_discard_addrs(sbi, cpc, true)) { 1715 has_candidate = true; 1716 break; 1717 } 1718 } 1719 mutex_unlock(&SIT_I(sbi)->sentry_lock); 1720 1721 cpc->trim_start = trim_start; 1722 return has_candidate; 1723 } 1724 1725 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) 1726 { 1727 __u64 start = F2FS_BYTES_TO_BLK(range->start); 1728 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; 1729 unsigned int start_segno, end_segno; 1730 struct cp_control cpc; 1731 int err = 0; 1732 1733 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) 1734 return -EINVAL; 1735 1736 cpc.trimmed = 0; 1737 if (end <= MAIN_BLKADDR(sbi)) 1738 goto out; 1739 1740 if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) { 1741 f2fs_msg(sbi->sb, KERN_WARNING, 1742 "Found FS corruption, run fsck to fix."); 1743 goto out; 1744 } 1745 1746 /* start/end segment number in main_area */ 1747 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); 1748 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : 1749 GET_SEGNO(sbi, end); 1750 cpc.reason = CP_DISCARD; 1751 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen)); 1752 1753 /* do checkpoint to issue discard commands safely */ 1754 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) { 1755 cpc.trim_start = start_segno; 1756 1757 if (sbi->discard_blks == 0) 1758 break; 1759 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi)) 1760 cpc.trim_end = end_segno; 1761 else 1762 cpc.trim_end = min_t(unsigned int, 1763 rounddown(start_segno + 1764 BATCHED_TRIM_SEGMENTS(sbi), 1765 sbi->segs_per_sec) - 1, end_segno); 1766 1767 mutex_lock(&sbi->gc_mutex); 1768 err = write_checkpoint(sbi, &cpc); 1769 mutex_unlock(&sbi->gc_mutex); 1770 if (err) 1771 break; 1772 1773 schedule(); 1774 } 1775 out: 1776 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed); 1777 return err; 1778 } 1779 1780 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) 1781 { 1782 struct curseg_info *curseg = CURSEG_I(sbi, type); 1783 if (curseg->next_blkoff < sbi->blocks_per_seg) 1784 return true; 1785 return false; 1786 } 1787 1788 static int __get_segment_type_2(struct page *page, enum page_type p_type) 1789 { 1790 if (p_type == DATA) 1791 return CURSEG_HOT_DATA; 1792 else 1793 return CURSEG_HOT_NODE; 1794 } 1795 1796 static int __get_segment_type_4(struct page *page, enum page_type p_type) 1797 { 1798 if (p_type == DATA) { 1799 struct inode *inode = page->mapping->host; 1800 1801 if (S_ISDIR(inode->i_mode)) 1802 return CURSEG_HOT_DATA; 1803 else 1804 return CURSEG_COLD_DATA; 1805 } else { 1806 if (IS_DNODE(page) && is_cold_node(page)) 1807 return CURSEG_WARM_NODE; 1808 else 1809 return CURSEG_COLD_NODE; 1810 } 1811 } 1812 1813 static int __get_segment_type_6(struct page *page, enum page_type p_type) 1814 { 1815 if (p_type == DATA) { 1816 struct inode *inode = page->mapping->host; 1817 1818 if (S_ISDIR(inode->i_mode)) 1819 return CURSEG_HOT_DATA; 1820 else if (is_cold_data(page) || file_is_cold(inode)) 1821 return CURSEG_COLD_DATA; 1822 else 1823 return CURSEG_WARM_DATA; 1824 } else { 1825 if (IS_DNODE(page)) 1826 return is_cold_node(page) ? CURSEG_WARM_NODE : 1827 CURSEG_HOT_NODE; 1828 else 1829 return CURSEG_COLD_NODE; 1830 } 1831 } 1832 1833 static int __get_segment_type(struct page *page, enum page_type p_type) 1834 { 1835 switch (F2FS_P_SB(page)->active_logs) { 1836 case 2: 1837 return __get_segment_type_2(page, p_type); 1838 case 4: 1839 return __get_segment_type_4(page, p_type); 1840 } 1841 /* NR_CURSEG_TYPE(6) logs by default */ 1842 f2fs_bug_on(F2FS_P_SB(page), 1843 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE); 1844 return __get_segment_type_6(page, p_type); 1845 } 1846 1847 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, 1848 block_t old_blkaddr, block_t *new_blkaddr, 1849 struct f2fs_summary *sum, int type) 1850 { 1851 struct sit_info *sit_i = SIT_I(sbi); 1852 struct curseg_info *curseg = CURSEG_I(sbi, type); 1853 1854 mutex_lock(&curseg->curseg_mutex); 1855 mutex_lock(&sit_i->sentry_lock); 1856 1857 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); 1858 1859 f2fs_wait_discard_bio(sbi, *new_blkaddr); 1860 1861 /* 1862 * __add_sum_entry should be resided under the curseg_mutex 1863 * because, this function updates a summary entry in the 1864 * current summary block. 1865 */ 1866 __add_sum_entry(sbi, type, sum); 1867 1868 __refresh_next_blkoff(sbi, curseg); 1869 1870 stat_inc_block_count(sbi, curseg); 1871 1872 /* 1873 * SIT information should be updated before segment allocation, 1874 * since SSR needs latest valid block information. 1875 */ 1876 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr); 1877 1878 if (!__has_curseg_space(sbi, type)) 1879 sit_i->s_ops->allocate_segment(sbi, type, false); 1880 1881 mutex_unlock(&sit_i->sentry_lock); 1882 1883 if (page && IS_NODESEG(type)) 1884 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); 1885 1886 mutex_unlock(&curseg->curseg_mutex); 1887 } 1888 1889 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio) 1890 { 1891 int type = __get_segment_type(fio->page, fio->type); 1892 int err; 1893 1894 if (fio->type == NODE || fio->type == DATA) 1895 mutex_lock(&fio->sbi->wio_mutex[fio->type]); 1896 reallocate: 1897 allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr, 1898 &fio->new_blkaddr, sum, type); 1899 1900 /* writeout dirty page into bdev */ 1901 err = f2fs_submit_page_mbio(fio); 1902 if (err == -EAGAIN) { 1903 fio->old_blkaddr = fio->new_blkaddr; 1904 goto reallocate; 1905 } 1906 1907 if (fio->type == NODE || fio->type == DATA) 1908 mutex_unlock(&fio->sbi->wio_mutex[fio->type]); 1909 } 1910 1911 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) 1912 { 1913 struct f2fs_io_info fio = { 1914 .sbi = sbi, 1915 .type = META, 1916 .op = REQ_OP_WRITE, 1917 .op_flags = REQ_SYNC | REQ_META | REQ_PRIO, 1918 .old_blkaddr = page->index, 1919 .new_blkaddr = page->index, 1920 .page = page, 1921 .encrypted_page = NULL, 1922 }; 1923 1924 if (unlikely(page->index >= MAIN_BLKADDR(sbi))) 1925 fio.op_flags &= ~REQ_META; 1926 1927 set_page_writeback(page); 1928 f2fs_submit_page_mbio(&fio); 1929 } 1930 1931 void write_node_page(unsigned int nid, struct f2fs_io_info *fio) 1932 { 1933 struct f2fs_summary sum; 1934 1935 set_summary(&sum, nid, 0, 0); 1936 do_write_page(&sum, fio); 1937 } 1938 1939 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio) 1940 { 1941 struct f2fs_sb_info *sbi = fio->sbi; 1942 struct f2fs_summary sum; 1943 struct node_info ni; 1944 1945 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); 1946 get_node_info(sbi, dn->nid, &ni); 1947 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 1948 do_write_page(&sum, fio); 1949 f2fs_update_data_blkaddr(dn, fio->new_blkaddr); 1950 } 1951 1952 void rewrite_data_page(struct f2fs_io_info *fio) 1953 { 1954 fio->new_blkaddr = fio->old_blkaddr; 1955 stat_inc_inplace_blocks(fio->sbi); 1956 f2fs_submit_page_mbio(fio); 1957 } 1958 1959 void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 1960 block_t old_blkaddr, block_t new_blkaddr, 1961 bool recover_curseg, bool recover_newaddr) 1962 { 1963 struct sit_info *sit_i = SIT_I(sbi); 1964 struct curseg_info *curseg; 1965 unsigned int segno, old_cursegno; 1966 struct seg_entry *se; 1967 int type; 1968 unsigned short old_blkoff; 1969 1970 segno = GET_SEGNO(sbi, new_blkaddr); 1971 se = get_seg_entry(sbi, segno); 1972 type = se->type; 1973 1974 if (!recover_curseg) { 1975 /* for recovery flow */ 1976 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { 1977 if (old_blkaddr == NULL_ADDR) 1978 type = CURSEG_COLD_DATA; 1979 else 1980 type = CURSEG_WARM_DATA; 1981 } 1982 } else { 1983 if (!IS_CURSEG(sbi, segno)) 1984 type = CURSEG_WARM_DATA; 1985 } 1986 1987 curseg = CURSEG_I(sbi, type); 1988 1989 mutex_lock(&curseg->curseg_mutex); 1990 mutex_lock(&sit_i->sentry_lock); 1991 1992 old_cursegno = curseg->segno; 1993 old_blkoff = curseg->next_blkoff; 1994 1995 /* change the current segment */ 1996 if (segno != curseg->segno) { 1997 curseg->next_segno = segno; 1998 change_curseg(sbi, type, true); 1999 } 2000 2001 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); 2002 __add_sum_entry(sbi, type, sum); 2003 2004 if (!recover_curseg || recover_newaddr) 2005 update_sit_entry(sbi, new_blkaddr, 1); 2006 if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) 2007 update_sit_entry(sbi, old_blkaddr, -1); 2008 2009 locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr)); 2010 locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr)); 2011 2012 locate_dirty_segment(sbi, old_cursegno); 2013 2014 if (recover_curseg) { 2015 if (old_cursegno != curseg->segno) { 2016 curseg->next_segno = old_cursegno; 2017 change_curseg(sbi, type, true); 2018 } 2019 curseg->next_blkoff = old_blkoff; 2020 } 2021 2022 mutex_unlock(&sit_i->sentry_lock); 2023 mutex_unlock(&curseg->curseg_mutex); 2024 } 2025 2026 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn, 2027 block_t old_addr, block_t new_addr, 2028 unsigned char version, bool recover_curseg, 2029 bool recover_newaddr) 2030 { 2031 struct f2fs_summary sum; 2032 2033 set_summary(&sum, dn->nid, dn->ofs_in_node, version); 2034 2035 __f2fs_replace_block(sbi, &sum, old_addr, new_addr, 2036 recover_curseg, recover_newaddr); 2037 2038 f2fs_update_data_blkaddr(dn, new_addr); 2039 } 2040 2041 void f2fs_wait_on_page_writeback(struct page *page, 2042 enum page_type type, bool ordered) 2043 { 2044 if (PageWriteback(page)) { 2045 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 2046 2047 f2fs_submit_merged_bio_cond(sbi, page->mapping->host, 2048 0, page->index, type, WRITE); 2049 if (ordered) 2050 wait_on_page_writeback(page); 2051 else 2052 wait_for_stable_page(page); 2053 } 2054 } 2055 2056 void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi, 2057 block_t blkaddr) 2058 { 2059 struct page *cpage; 2060 2061 if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR) 2062 return; 2063 2064 cpage = find_lock_page(META_MAPPING(sbi), blkaddr); 2065 if (cpage) { 2066 f2fs_wait_on_page_writeback(cpage, DATA, true); 2067 f2fs_put_page(cpage, 1); 2068 } 2069 } 2070 2071 static int read_compacted_summaries(struct f2fs_sb_info *sbi) 2072 { 2073 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2074 struct curseg_info *seg_i; 2075 unsigned char *kaddr; 2076 struct page *page; 2077 block_t start; 2078 int i, j, offset; 2079 2080 start = start_sum_block(sbi); 2081 2082 page = get_meta_page(sbi, start++); 2083 kaddr = (unsigned char *)page_address(page); 2084 2085 /* Step 1: restore nat cache */ 2086 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 2087 memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE); 2088 2089 /* Step 2: restore sit cache */ 2090 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 2091 memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE); 2092 offset = 2 * SUM_JOURNAL_SIZE; 2093 2094 /* Step 3: restore summary entries */ 2095 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 2096 unsigned short blk_off; 2097 unsigned int segno; 2098 2099 seg_i = CURSEG_I(sbi, i); 2100 segno = le32_to_cpu(ckpt->cur_data_segno[i]); 2101 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); 2102 seg_i->next_segno = segno; 2103 reset_curseg(sbi, i, 0); 2104 seg_i->alloc_type = ckpt->alloc_type[i]; 2105 seg_i->next_blkoff = blk_off; 2106 2107 if (seg_i->alloc_type == SSR) 2108 blk_off = sbi->blocks_per_seg; 2109 2110 for (j = 0; j < blk_off; j++) { 2111 struct f2fs_summary *s; 2112 s = (struct f2fs_summary *)(kaddr + offset); 2113 seg_i->sum_blk->entries[j] = *s; 2114 offset += SUMMARY_SIZE; 2115 if (offset + SUMMARY_SIZE <= PAGE_SIZE - 2116 SUM_FOOTER_SIZE) 2117 continue; 2118 2119 f2fs_put_page(page, 1); 2120 page = NULL; 2121 2122 page = get_meta_page(sbi, start++); 2123 kaddr = (unsigned char *)page_address(page); 2124 offset = 0; 2125 } 2126 } 2127 f2fs_put_page(page, 1); 2128 return 0; 2129 } 2130 2131 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) 2132 { 2133 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2134 struct f2fs_summary_block *sum; 2135 struct curseg_info *curseg; 2136 struct page *new; 2137 unsigned short blk_off; 2138 unsigned int segno = 0; 2139 block_t blk_addr = 0; 2140 2141 /* get segment number and block addr */ 2142 if (IS_DATASEG(type)) { 2143 segno = le32_to_cpu(ckpt->cur_data_segno[type]); 2144 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - 2145 CURSEG_HOT_DATA]); 2146 if (__exist_node_summaries(sbi)) 2147 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); 2148 else 2149 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 2150 } else { 2151 segno = le32_to_cpu(ckpt->cur_node_segno[type - 2152 CURSEG_HOT_NODE]); 2153 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - 2154 CURSEG_HOT_NODE]); 2155 if (__exist_node_summaries(sbi)) 2156 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, 2157 type - CURSEG_HOT_NODE); 2158 else 2159 blk_addr = GET_SUM_BLOCK(sbi, segno); 2160 } 2161 2162 new = get_meta_page(sbi, blk_addr); 2163 sum = (struct f2fs_summary_block *)page_address(new); 2164 2165 if (IS_NODESEG(type)) { 2166 if (__exist_node_summaries(sbi)) { 2167 struct f2fs_summary *ns = &sum->entries[0]; 2168 int i; 2169 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { 2170 ns->version = 0; 2171 ns->ofs_in_node = 0; 2172 } 2173 } else { 2174 int err; 2175 2176 err = restore_node_summary(sbi, segno, sum); 2177 if (err) { 2178 f2fs_put_page(new, 1); 2179 return err; 2180 } 2181 } 2182 } 2183 2184 /* set uncompleted segment to curseg */ 2185 curseg = CURSEG_I(sbi, type); 2186 mutex_lock(&curseg->curseg_mutex); 2187 2188 /* update journal info */ 2189 down_write(&curseg->journal_rwsem); 2190 memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE); 2191 up_write(&curseg->journal_rwsem); 2192 2193 memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE); 2194 memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE); 2195 curseg->next_segno = segno; 2196 reset_curseg(sbi, type, 0); 2197 curseg->alloc_type = ckpt->alloc_type[type]; 2198 curseg->next_blkoff = blk_off; 2199 mutex_unlock(&curseg->curseg_mutex); 2200 f2fs_put_page(new, 1); 2201 return 0; 2202 } 2203 2204 static int restore_curseg_summaries(struct f2fs_sb_info *sbi) 2205 { 2206 int type = CURSEG_HOT_DATA; 2207 int err; 2208 2209 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) { 2210 int npages = npages_for_summary_flush(sbi, true); 2211 2212 if (npages >= 2) 2213 ra_meta_pages(sbi, start_sum_block(sbi), npages, 2214 META_CP, true); 2215 2216 /* restore for compacted data summary */ 2217 if (read_compacted_summaries(sbi)) 2218 return -EINVAL; 2219 type = CURSEG_HOT_NODE; 2220 } 2221 2222 if (__exist_node_summaries(sbi)) 2223 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type), 2224 NR_CURSEG_TYPE - type, META_CP, true); 2225 2226 for (; type <= CURSEG_COLD_NODE; type++) { 2227 err = read_normal_summaries(sbi, type); 2228 if (err) 2229 return err; 2230 } 2231 2232 return 0; 2233 } 2234 2235 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) 2236 { 2237 struct page *page; 2238 unsigned char *kaddr; 2239 struct f2fs_summary *summary; 2240 struct curseg_info *seg_i; 2241 int written_size = 0; 2242 int i, j; 2243 2244 page = grab_meta_page(sbi, blkaddr++); 2245 kaddr = (unsigned char *)page_address(page); 2246 2247 /* Step 1: write nat cache */ 2248 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 2249 memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE); 2250 written_size += SUM_JOURNAL_SIZE; 2251 2252 /* Step 2: write sit cache */ 2253 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 2254 memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE); 2255 written_size += SUM_JOURNAL_SIZE; 2256 2257 /* Step 3: write summary entries */ 2258 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 2259 unsigned short blkoff; 2260 seg_i = CURSEG_I(sbi, i); 2261 if (sbi->ckpt->alloc_type[i] == SSR) 2262 blkoff = sbi->blocks_per_seg; 2263 else 2264 blkoff = curseg_blkoff(sbi, i); 2265 2266 for (j = 0; j < blkoff; j++) { 2267 if (!page) { 2268 page = grab_meta_page(sbi, blkaddr++); 2269 kaddr = (unsigned char *)page_address(page); 2270 written_size = 0; 2271 } 2272 summary = (struct f2fs_summary *)(kaddr + written_size); 2273 *summary = seg_i->sum_blk->entries[j]; 2274 written_size += SUMMARY_SIZE; 2275 2276 if (written_size + SUMMARY_SIZE <= PAGE_SIZE - 2277 SUM_FOOTER_SIZE) 2278 continue; 2279 2280 set_page_dirty(page); 2281 f2fs_put_page(page, 1); 2282 page = NULL; 2283 } 2284 } 2285 if (page) { 2286 set_page_dirty(page); 2287 f2fs_put_page(page, 1); 2288 } 2289 } 2290 2291 static void write_normal_summaries(struct f2fs_sb_info *sbi, 2292 block_t blkaddr, int type) 2293 { 2294 int i, end; 2295 if (IS_DATASEG(type)) 2296 end = type + NR_CURSEG_DATA_TYPE; 2297 else 2298 end = type + NR_CURSEG_NODE_TYPE; 2299 2300 for (i = type; i < end; i++) 2301 write_current_sum_page(sbi, i, blkaddr + (i - type)); 2302 } 2303 2304 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 2305 { 2306 if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) 2307 write_compacted_summaries(sbi, start_blk); 2308 else 2309 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); 2310 } 2311 2312 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 2313 { 2314 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); 2315 } 2316 2317 int lookup_journal_in_cursum(struct f2fs_journal *journal, int type, 2318 unsigned int val, int alloc) 2319 { 2320 int i; 2321 2322 if (type == NAT_JOURNAL) { 2323 for (i = 0; i < nats_in_cursum(journal); i++) { 2324 if (le32_to_cpu(nid_in_journal(journal, i)) == val) 2325 return i; 2326 } 2327 if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL)) 2328 return update_nats_in_cursum(journal, 1); 2329 } else if (type == SIT_JOURNAL) { 2330 for (i = 0; i < sits_in_cursum(journal); i++) 2331 if (le32_to_cpu(segno_in_journal(journal, i)) == val) 2332 return i; 2333 if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL)) 2334 return update_sits_in_cursum(journal, 1); 2335 } 2336 return -1; 2337 } 2338 2339 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, 2340 unsigned int segno) 2341 { 2342 return get_meta_page(sbi, current_sit_addr(sbi, segno)); 2343 } 2344 2345 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, 2346 unsigned int start) 2347 { 2348 struct sit_info *sit_i = SIT_I(sbi); 2349 struct page *src_page, *dst_page; 2350 pgoff_t src_off, dst_off; 2351 void *src_addr, *dst_addr; 2352 2353 src_off = current_sit_addr(sbi, start); 2354 dst_off = next_sit_addr(sbi, src_off); 2355 2356 /* get current sit block page without lock */ 2357 src_page = get_meta_page(sbi, src_off); 2358 dst_page = grab_meta_page(sbi, dst_off); 2359 f2fs_bug_on(sbi, PageDirty(src_page)); 2360 2361 src_addr = page_address(src_page); 2362 dst_addr = page_address(dst_page); 2363 memcpy(dst_addr, src_addr, PAGE_SIZE); 2364 2365 set_page_dirty(dst_page); 2366 f2fs_put_page(src_page, 1); 2367 2368 set_to_next_sit(sit_i, start); 2369 2370 return dst_page; 2371 } 2372 2373 static struct sit_entry_set *grab_sit_entry_set(void) 2374 { 2375 struct sit_entry_set *ses = 2376 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS); 2377 2378 ses->entry_cnt = 0; 2379 INIT_LIST_HEAD(&ses->set_list); 2380 return ses; 2381 } 2382 2383 static void release_sit_entry_set(struct sit_entry_set *ses) 2384 { 2385 list_del(&ses->set_list); 2386 kmem_cache_free(sit_entry_set_slab, ses); 2387 } 2388 2389 static void adjust_sit_entry_set(struct sit_entry_set *ses, 2390 struct list_head *head) 2391 { 2392 struct sit_entry_set *next = ses; 2393 2394 if (list_is_last(&ses->set_list, head)) 2395 return; 2396 2397 list_for_each_entry_continue(next, head, set_list) 2398 if (ses->entry_cnt <= next->entry_cnt) 2399 break; 2400 2401 list_move_tail(&ses->set_list, &next->set_list); 2402 } 2403 2404 static void add_sit_entry(unsigned int segno, struct list_head *head) 2405 { 2406 struct sit_entry_set *ses; 2407 unsigned int start_segno = START_SEGNO(segno); 2408 2409 list_for_each_entry(ses, head, set_list) { 2410 if (ses->start_segno == start_segno) { 2411 ses->entry_cnt++; 2412 adjust_sit_entry_set(ses, head); 2413 return; 2414 } 2415 } 2416 2417 ses = grab_sit_entry_set(); 2418 2419 ses->start_segno = start_segno; 2420 ses->entry_cnt++; 2421 list_add(&ses->set_list, head); 2422 } 2423 2424 static void add_sits_in_set(struct f2fs_sb_info *sbi) 2425 { 2426 struct f2fs_sm_info *sm_info = SM_I(sbi); 2427 struct list_head *set_list = &sm_info->sit_entry_set; 2428 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; 2429 unsigned int segno; 2430 2431 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) 2432 add_sit_entry(segno, set_list); 2433 } 2434 2435 static void remove_sits_in_journal(struct f2fs_sb_info *sbi) 2436 { 2437 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 2438 struct f2fs_journal *journal = curseg->journal; 2439 int i; 2440 2441 down_write(&curseg->journal_rwsem); 2442 for (i = 0; i < sits_in_cursum(journal); i++) { 2443 unsigned int segno; 2444 bool dirtied; 2445 2446 segno = le32_to_cpu(segno_in_journal(journal, i)); 2447 dirtied = __mark_sit_entry_dirty(sbi, segno); 2448 2449 if (!dirtied) 2450 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); 2451 } 2452 update_sits_in_cursum(journal, -i); 2453 up_write(&curseg->journal_rwsem); 2454 } 2455 2456 /* 2457 * CP calls this function, which flushes SIT entries including sit_journal, 2458 * and moves prefree segs to free segs. 2459 */ 2460 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) 2461 { 2462 struct sit_info *sit_i = SIT_I(sbi); 2463 unsigned long *bitmap = sit_i->dirty_sentries_bitmap; 2464 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 2465 struct f2fs_journal *journal = curseg->journal; 2466 struct sit_entry_set *ses, *tmp; 2467 struct list_head *head = &SM_I(sbi)->sit_entry_set; 2468 bool to_journal = true; 2469 struct seg_entry *se; 2470 2471 mutex_lock(&sit_i->sentry_lock); 2472 2473 if (!sit_i->dirty_sentries) 2474 goto out; 2475 2476 /* 2477 * add and account sit entries of dirty bitmap in sit entry 2478 * set temporarily 2479 */ 2480 add_sits_in_set(sbi); 2481 2482 /* 2483 * if there are no enough space in journal to store dirty sit 2484 * entries, remove all entries from journal and add and account 2485 * them in sit entry set. 2486 */ 2487 if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL)) 2488 remove_sits_in_journal(sbi); 2489 2490 /* 2491 * there are two steps to flush sit entries: 2492 * #1, flush sit entries to journal in current cold data summary block. 2493 * #2, flush sit entries to sit page. 2494 */ 2495 list_for_each_entry_safe(ses, tmp, head, set_list) { 2496 struct page *page = NULL; 2497 struct f2fs_sit_block *raw_sit = NULL; 2498 unsigned int start_segno = ses->start_segno; 2499 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, 2500 (unsigned long)MAIN_SEGS(sbi)); 2501 unsigned int segno = start_segno; 2502 2503 if (to_journal && 2504 !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL)) 2505 to_journal = false; 2506 2507 if (to_journal) { 2508 down_write(&curseg->journal_rwsem); 2509 } else { 2510 page = get_next_sit_page(sbi, start_segno); 2511 raw_sit = page_address(page); 2512 } 2513 2514 /* flush dirty sit entries in region of current sit set */ 2515 for_each_set_bit_from(segno, bitmap, end) { 2516 int offset, sit_offset; 2517 2518 se = get_seg_entry(sbi, segno); 2519 2520 /* add discard candidates */ 2521 if (cpc->reason != CP_DISCARD) { 2522 cpc->trim_start = segno; 2523 add_discard_addrs(sbi, cpc, false); 2524 } 2525 2526 if (to_journal) { 2527 offset = lookup_journal_in_cursum(journal, 2528 SIT_JOURNAL, segno, 1); 2529 f2fs_bug_on(sbi, offset < 0); 2530 segno_in_journal(journal, offset) = 2531 cpu_to_le32(segno); 2532 seg_info_to_raw_sit(se, 2533 &sit_in_journal(journal, offset)); 2534 } else { 2535 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); 2536 seg_info_to_raw_sit(se, 2537 &raw_sit->entries[sit_offset]); 2538 } 2539 2540 __clear_bit(segno, bitmap); 2541 sit_i->dirty_sentries--; 2542 ses->entry_cnt--; 2543 } 2544 2545 if (to_journal) 2546 up_write(&curseg->journal_rwsem); 2547 else 2548 f2fs_put_page(page, 1); 2549 2550 f2fs_bug_on(sbi, ses->entry_cnt); 2551 release_sit_entry_set(ses); 2552 } 2553 2554 f2fs_bug_on(sbi, !list_empty(head)); 2555 f2fs_bug_on(sbi, sit_i->dirty_sentries); 2556 out: 2557 if (cpc->reason == CP_DISCARD) { 2558 __u64 trim_start = cpc->trim_start; 2559 2560 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) 2561 add_discard_addrs(sbi, cpc, false); 2562 2563 cpc->trim_start = trim_start; 2564 } 2565 mutex_unlock(&sit_i->sentry_lock); 2566 2567 set_prefree_as_free_segments(sbi); 2568 } 2569 2570 static int build_sit_info(struct f2fs_sb_info *sbi) 2571 { 2572 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 2573 struct sit_info *sit_i; 2574 unsigned int sit_segs, start; 2575 char *src_bitmap; 2576 unsigned int bitmap_size; 2577 2578 /* allocate memory for SIT information */ 2579 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL); 2580 if (!sit_i) 2581 return -ENOMEM; 2582 2583 SM_I(sbi)->sit_info = sit_i; 2584 2585 sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) * 2586 sizeof(struct seg_entry), GFP_KERNEL); 2587 if (!sit_i->sentries) 2588 return -ENOMEM; 2589 2590 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2591 sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2592 if (!sit_i->dirty_sentries_bitmap) 2593 return -ENOMEM; 2594 2595 for (start = 0; start < MAIN_SEGS(sbi); start++) { 2596 sit_i->sentries[start].cur_valid_map 2597 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 2598 sit_i->sentries[start].ckpt_valid_map 2599 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 2600 if (!sit_i->sentries[start].cur_valid_map || 2601 !sit_i->sentries[start].ckpt_valid_map) 2602 return -ENOMEM; 2603 2604 #ifdef CONFIG_F2FS_CHECK_FS 2605 sit_i->sentries[start].cur_valid_map_mir 2606 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 2607 if (!sit_i->sentries[start].cur_valid_map_mir) 2608 return -ENOMEM; 2609 #endif 2610 2611 if (f2fs_discard_en(sbi)) { 2612 sit_i->sentries[start].discard_map 2613 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 2614 if (!sit_i->sentries[start].discard_map) 2615 return -ENOMEM; 2616 } 2617 } 2618 2619 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 2620 if (!sit_i->tmp_map) 2621 return -ENOMEM; 2622 2623 if (sbi->segs_per_sec > 1) { 2624 sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) * 2625 sizeof(struct sec_entry), GFP_KERNEL); 2626 if (!sit_i->sec_entries) 2627 return -ENOMEM; 2628 } 2629 2630 /* get information related with SIT */ 2631 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; 2632 2633 /* setup SIT bitmap from ckeckpoint pack */ 2634 bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 2635 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 2636 2637 sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); 2638 if (!sit_i->sit_bitmap) 2639 return -ENOMEM; 2640 2641 #ifdef CONFIG_F2FS_CHECK_FS 2642 sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); 2643 if (!sit_i->sit_bitmap_mir) 2644 return -ENOMEM; 2645 #endif 2646 2647 /* init SIT information */ 2648 sit_i->s_ops = &default_salloc_ops; 2649 2650 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); 2651 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; 2652 sit_i->written_valid_blocks = 0; 2653 sit_i->bitmap_size = bitmap_size; 2654 sit_i->dirty_sentries = 0; 2655 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 2656 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); 2657 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec; 2658 mutex_init(&sit_i->sentry_lock); 2659 return 0; 2660 } 2661 2662 static int build_free_segmap(struct f2fs_sb_info *sbi) 2663 { 2664 struct free_segmap_info *free_i; 2665 unsigned int bitmap_size, sec_bitmap_size; 2666 2667 /* allocate memory for free segmap information */ 2668 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL); 2669 if (!free_i) 2670 return -ENOMEM; 2671 2672 SM_I(sbi)->free_info = free_i; 2673 2674 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2675 free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL); 2676 if (!free_i->free_segmap) 2677 return -ENOMEM; 2678 2679 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 2680 free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL); 2681 if (!free_i->free_secmap) 2682 return -ENOMEM; 2683 2684 /* set all segments as dirty temporarily */ 2685 memset(free_i->free_segmap, 0xff, bitmap_size); 2686 memset(free_i->free_secmap, 0xff, sec_bitmap_size); 2687 2688 /* init free segmap information */ 2689 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); 2690 free_i->free_segments = 0; 2691 free_i->free_sections = 0; 2692 spin_lock_init(&free_i->segmap_lock); 2693 return 0; 2694 } 2695 2696 static int build_curseg(struct f2fs_sb_info *sbi) 2697 { 2698 struct curseg_info *array; 2699 int i; 2700 2701 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL); 2702 if (!array) 2703 return -ENOMEM; 2704 2705 SM_I(sbi)->curseg_array = array; 2706 2707 for (i = 0; i < NR_CURSEG_TYPE; i++) { 2708 mutex_init(&array[i].curseg_mutex); 2709 array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL); 2710 if (!array[i].sum_blk) 2711 return -ENOMEM; 2712 init_rwsem(&array[i].journal_rwsem); 2713 array[i].journal = kzalloc(sizeof(struct f2fs_journal), 2714 GFP_KERNEL); 2715 if (!array[i].journal) 2716 return -ENOMEM; 2717 array[i].segno = NULL_SEGNO; 2718 array[i].next_blkoff = 0; 2719 } 2720 return restore_curseg_summaries(sbi); 2721 } 2722 2723 static void build_sit_entries(struct f2fs_sb_info *sbi) 2724 { 2725 struct sit_info *sit_i = SIT_I(sbi); 2726 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 2727 struct f2fs_journal *journal = curseg->journal; 2728 struct seg_entry *se; 2729 struct f2fs_sit_entry sit; 2730 int sit_blk_cnt = SIT_BLK_CNT(sbi); 2731 unsigned int i, start, end; 2732 unsigned int readed, start_blk = 0; 2733 2734 do { 2735 readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES, 2736 META_SIT, true); 2737 2738 start = start_blk * sit_i->sents_per_block; 2739 end = (start_blk + readed) * sit_i->sents_per_block; 2740 2741 for (; start < end && start < MAIN_SEGS(sbi); start++) { 2742 struct f2fs_sit_block *sit_blk; 2743 struct page *page; 2744 2745 se = &sit_i->sentries[start]; 2746 page = get_current_sit_page(sbi, start); 2747 sit_blk = (struct f2fs_sit_block *)page_address(page); 2748 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; 2749 f2fs_put_page(page, 1); 2750 2751 check_block_count(sbi, start, &sit); 2752 seg_info_from_raw_sit(se, &sit); 2753 2754 /* build discard map only one time */ 2755 if (f2fs_discard_en(sbi)) { 2756 memcpy(se->discard_map, se->cur_valid_map, 2757 SIT_VBLOCK_MAP_SIZE); 2758 sbi->discard_blks += sbi->blocks_per_seg - 2759 se->valid_blocks; 2760 } 2761 2762 if (sbi->segs_per_sec > 1) 2763 get_sec_entry(sbi, start)->valid_blocks += 2764 se->valid_blocks; 2765 } 2766 start_blk += readed; 2767 } while (start_blk < sit_blk_cnt); 2768 2769 down_read(&curseg->journal_rwsem); 2770 for (i = 0; i < sits_in_cursum(journal); i++) { 2771 unsigned int old_valid_blocks; 2772 2773 start = le32_to_cpu(segno_in_journal(journal, i)); 2774 se = &sit_i->sentries[start]; 2775 sit = sit_in_journal(journal, i); 2776 2777 old_valid_blocks = se->valid_blocks; 2778 2779 check_block_count(sbi, start, &sit); 2780 seg_info_from_raw_sit(se, &sit); 2781 2782 if (f2fs_discard_en(sbi)) { 2783 memcpy(se->discard_map, se->cur_valid_map, 2784 SIT_VBLOCK_MAP_SIZE); 2785 sbi->discard_blks += old_valid_blocks - 2786 se->valid_blocks; 2787 } 2788 2789 if (sbi->segs_per_sec > 1) 2790 get_sec_entry(sbi, start)->valid_blocks += 2791 se->valid_blocks - old_valid_blocks; 2792 } 2793 up_read(&curseg->journal_rwsem); 2794 } 2795 2796 static void init_free_segmap(struct f2fs_sb_info *sbi) 2797 { 2798 unsigned int start; 2799 int type; 2800 2801 for (start = 0; start < MAIN_SEGS(sbi); start++) { 2802 struct seg_entry *sentry = get_seg_entry(sbi, start); 2803 if (!sentry->valid_blocks) 2804 __set_free(sbi, start); 2805 else 2806 SIT_I(sbi)->written_valid_blocks += 2807 sentry->valid_blocks; 2808 } 2809 2810 /* set use the current segments */ 2811 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { 2812 struct curseg_info *curseg_t = CURSEG_I(sbi, type); 2813 __set_test_and_inuse(sbi, curseg_t->segno); 2814 } 2815 } 2816 2817 static void init_dirty_segmap(struct f2fs_sb_info *sbi) 2818 { 2819 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2820 struct free_segmap_info *free_i = FREE_I(sbi); 2821 unsigned int segno = 0, offset = 0; 2822 unsigned short valid_blocks; 2823 2824 while (1) { 2825 /* find dirty segment based on free segmap */ 2826 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); 2827 if (segno >= MAIN_SEGS(sbi)) 2828 break; 2829 offset = segno + 1; 2830 valid_blocks = get_valid_blocks(sbi, segno, 0); 2831 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks) 2832 continue; 2833 if (valid_blocks > sbi->blocks_per_seg) { 2834 f2fs_bug_on(sbi, 1); 2835 continue; 2836 } 2837 mutex_lock(&dirty_i->seglist_lock); 2838 __locate_dirty_segment(sbi, segno, DIRTY); 2839 mutex_unlock(&dirty_i->seglist_lock); 2840 } 2841 } 2842 2843 static int init_victim_secmap(struct f2fs_sb_info *sbi) 2844 { 2845 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2846 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 2847 2848 dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2849 if (!dirty_i->victim_secmap) 2850 return -ENOMEM; 2851 return 0; 2852 } 2853 2854 static int build_dirty_segmap(struct f2fs_sb_info *sbi) 2855 { 2856 struct dirty_seglist_info *dirty_i; 2857 unsigned int bitmap_size, i; 2858 2859 /* allocate memory for dirty segments list information */ 2860 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL); 2861 if (!dirty_i) 2862 return -ENOMEM; 2863 2864 SM_I(sbi)->dirty_info = dirty_i; 2865 mutex_init(&dirty_i->seglist_lock); 2866 2867 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2868 2869 for (i = 0; i < NR_DIRTY_TYPE; i++) { 2870 dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL); 2871 if (!dirty_i->dirty_segmap[i]) 2872 return -ENOMEM; 2873 } 2874 2875 init_dirty_segmap(sbi); 2876 return init_victim_secmap(sbi); 2877 } 2878 2879 /* 2880 * Update min, max modified time for cost-benefit GC algorithm 2881 */ 2882 static void init_min_max_mtime(struct f2fs_sb_info *sbi) 2883 { 2884 struct sit_info *sit_i = SIT_I(sbi); 2885 unsigned int segno; 2886 2887 mutex_lock(&sit_i->sentry_lock); 2888 2889 sit_i->min_mtime = LLONG_MAX; 2890 2891 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { 2892 unsigned int i; 2893 unsigned long long mtime = 0; 2894 2895 for (i = 0; i < sbi->segs_per_sec; i++) 2896 mtime += get_seg_entry(sbi, segno + i)->mtime; 2897 2898 mtime = div_u64(mtime, sbi->segs_per_sec); 2899 2900 if (sit_i->min_mtime > mtime) 2901 sit_i->min_mtime = mtime; 2902 } 2903 sit_i->max_mtime = get_mtime(sbi); 2904 mutex_unlock(&sit_i->sentry_lock); 2905 } 2906 2907 int build_segment_manager(struct f2fs_sb_info *sbi) 2908 { 2909 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 2910 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2911 struct f2fs_sm_info *sm_info; 2912 int err; 2913 2914 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL); 2915 if (!sm_info) 2916 return -ENOMEM; 2917 2918 /* init sm info */ 2919 sbi->sm_info = sm_info; 2920 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 2921 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 2922 sm_info->segment_count = le32_to_cpu(raw_super->segment_count); 2923 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 2924 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 2925 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); 2926 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 2927 sm_info->rec_prefree_segments = sm_info->main_segments * 2928 DEF_RECLAIM_PREFREE_SEGMENTS / 100; 2929 if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS) 2930 sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS; 2931 2932 if (!test_opt(sbi, LFS)) 2933 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; 2934 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; 2935 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; 2936 2937 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS; 2938 2939 INIT_LIST_HEAD(&sm_info->sit_entry_set); 2940 2941 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) { 2942 err = create_flush_cmd_control(sbi); 2943 if (err) 2944 return err; 2945 } 2946 2947 err = create_discard_cmd_control(sbi); 2948 if (err) 2949 return err; 2950 2951 err = build_sit_info(sbi); 2952 if (err) 2953 return err; 2954 err = build_free_segmap(sbi); 2955 if (err) 2956 return err; 2957 err = build_curseg(sbi); 2958 if (err) 2959 return err; 2960 2961 /* reinit free segmap based on SIT */ 2962 build_sit_entries(sbi); 2963 2964 init_free_segmap(sbi); 2965 err = build_dirty_segmap(sbi); 2966 if (err) 2967 return err; 2968 2969 init_min_max_mtime(sbi); 2970 return 0; 2971 } 2972 2973 static void discard_dirty_segmap(struct f2fs_sb_info *sbi, 2974 enum dirty_type dirty_type) 2975 { 2976 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2977 2978 mutex_lock(&dirty_i->seglist_lock); 2979 kvfree(dirty_i->dirty_segmap[dirty_type]); 2980 dirty_i->nr_dirty[dirty_type] = 0; 2981 mutex_unlock(&dirty_i->seglist_lock); 2982 } 2983 2984 static void destroy_victim_secmap(struct f2fs_sb_info *sbi) 2985 { 2986 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2987 kvfree(dirty_i->victim_secmap); 2988 } 2989 2990 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) 2991 { 2992 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2993 int i; 2994 2995 if (!dirty_i) 2996 return; 2997 2998 /* discard pre-free/dirty segments list */ 2999 for (i = 0; i < NR_DIRTY_TYPE; i++) 3000 discard_dirty_segmap(sbi, i); 3001 3002 destroy_victim_secmap(sbi); 3003 SM_I(sbi)->dirty_info = NULL; 3004 kfree(dirty_i); 3005 } 3006 3007 static void destroy_curseg(struct f2fs_sb_info *sbi) 3008 { 3009 struct curseg_info *array = SM_I(sbi)->curseg_array; 3010 int i; 3011 3012 if (!array) 3013 return; 3014 SM_I(sbi)->curseg_array = NULL; 3015 for (i = 0; i < NR_CURSEG_TYPE; i++) { 3016 kfree(array[i].sum_blk); 3017 kfree(array[i].journal); 3018 } 3019 kfree(array); 3020 } 3021 3022 static void destroy_free_segmap(struct f2fs_sb_info *sbi) 3023 { 3024 struct free_segmap_info *free_i = SM_I(sbi)->free_info; 3025 if (!free_i) 3026 return; 3027 SM_I(sbi)->free_info = NULL; 3028 kvfree(free_i->free_segmap); 3029 kvfree(free_i->free_secmap); 3030 kfree(free_i); 3031 } 3032 3033 static void destroy_sit_info(struct f2fs_sb_info *sbi) 3034 { 3035 struct sit_info *sit_i = SIT_I(sbi); 3036 unsigned int start; 3037 3038 if (!sit_i) 3039 return; 3040 3041 if (sit_i->sentries) { 3042 for (start = 0; start < MAIN_SEGS(sbi); start++) { 3043 kfree(sit_i->sentries[start].cur_valid_map); 3044 #ifdef CONFIG_F2FS_CHECK_FS 3045 kfree(sit_i->sentries[start].cur_valid_map_mir); 3046 #endif 3047 kfree(sit_i->sentries[start].ckpt_valid_map); 3048 kfree(sit_i->sentries[start].discard_map); 3049 } 3050 } 3051 kfree(sit_i->tmp_map); 3052 3053 kvfree(sit_i->sentries); 3054 kvfree(sit_i->sec_entries); 3055 kvfree(sit_i->dirty_sentries_bitmap); 3056 3057 SM_I(sbi)->sit_info = NULL; 3058 kfree(sit_i->sit_bitmap); 3059 #ifdef CONFIG_F2FS_CHECK_FS 3060 kfree(sit_i->sit_bitmap_mir); 3061 #endif 3062 kfree(sit_i); 3063 } 3064 3065 void destroy_segment_manager(struct f2fs_sb_info *sbi) 3066 { 3067 struct f2fs_sm_info *sm_info = SM_I(sbi); 3068 3069 if (!sm_info) 3070 return; 3071 destroy_flush_cmd_control(sbi, true); 3072 destroy_discard_cmd_control(sbi, true); 3073 destroy_dirty_segmap(sbi); 3074 destroy_curseg(sbi); 3075 destroy_free_segmap(sbi); 3076 destroy_sit_info(sbi); 3077 sbi->sm_info = NULL; 3078 kfree(sm_info); 3079 } 3080 3081 int __init create_segment_manager_caches(void) 3082 { 3083 discard_entry_slab = f2fs_kmem_cache_create("discard_entry", 3084 sizeof(struct discard_entry)); 3085 if (!discard_entry_slab) 3086 goto fail; 3087 3088 discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd", 3089 sizeof(struct discard_cmd)); 3090 if (!discard_cmd_slab) 3091 goto destroy_discard_entry; 3092 3093 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set", 3094 sizeof(struct sit_entry_set)); 3095 if (!sit_entry_set_slab) 3096 goto destroy_discard_cmd; 3097 3098 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry", 3099 sizeof(struct inmem_pages)); 3100 if (!inmem_entry_slab) 3101 goto destroy_sit_entry_set; 3102 return 0; 3103 3104 destroy_sit_entry_set: 3105 kmem_cache_destroy(sit_entry_set_slab); 3106 destroy_discard_cmd: 3107 kmem_cache_destroy(discard_cmd_slab); 3108 destroy_discard_entry: 3109 kmem_cache_destroy(discard_entry_slab); 3110 fail: 3111 return -ENOMEM; 3112 } 3113 3114 void destroy_segment_manager_caches(void) 3115 { 3116 kmem_cache_destroy(sit_entry_set_slab); 3117 kmem_cache_destroy(discard_cmd_slab); 3118 kmem_cache_destroy(discard_entry_slab); 3119 kmem_cache_destroy(inmem_entry_slab); 3120 } 3121