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