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/vmalloc.h> 18 #include <linux/swap.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 *sit_entry_set_slab; 30 static struct kmem_cache *inmem_entry_slab; 31 32 /* 33 * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since 34 * MSB and LSB are reversed in a byte by f2fs_set_bit. 35 */ 36 static inline unsigned long __reverse_ffs(unsigned long word) 37 { 38 int num = 0; 39 40 #if BITS_PER_LONG == 64 41 if ((word & 0xffffffff) == 0) { 42 num += 32; 43 word >>= 32; 44 } 45 #endif 46 if ((word & 0xffff) == 0) { 47 num += 16; 48 word >>= 16; 49 } 50 if ((word & 0xff) == 0) { 51 num += 8; 52 word >>= 8; 53 } 54 if ((word & 0xf0) == 0) 55 num += 4; 56 else 57 word >>= 4; 58 if ((word & 0xc) == 0) 59 num += 2; 60 else 61 word >>= 2; 62 if ((word & 0x2) == 0) 63 num += 1; 64 return num; 65 } 66 67 /* 68 * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because 69 * f2fs_set_bit makes MSB and LSB reversed in a byte. 70 * Example: 71 * LSB <--> MSB 72 * f2fs_set_bit(0, bitmap) => 0000 0001 73 * f2fs_set_bit(7, bitmap) => 1000 0000 74 */ 75 static unsigned long __find_rev_next_bit(const unsigned long *addr, 76 unsigned long size, unsigned long offset) 77 { 78 const unsigned long *p = addr + BIT_WORD(offset); 79 unsigned long result = offset & ~(BITS_PER_LONG - 1); 80 unsigned long tmp; 81 unsigned long mask, submask; 82 unsigned long quot, rest; 83 84 if (offset >= size) 85 return size; 86 87 size -= result; 88 offset %= BITS_PER_LONG; 89 if (!offset) 90 goto aligned; 91 92 tmp = *(p++); 93 quot = (offset >> 3) << 3; 94 rest = offset & 0x7; 95 mask = ~0UL << quot; 96 submask = (unsigned char)(0xff << rest) >> rest; 97 submask <<= quot; 98 mask &= submask; 99 tmp &= mask; 100 if (size < BITS_PER_LONG) 101 goto found_first; 102 if (tmp) 103 goto found_middle; 104 105 size -= BITS_PER_LONG; 106 result += BITS_PER_LONG; 107 aligned: 108 while (size & ~(BITS_PER_LONG-1)) { 109 tmp = *(p++); 110 if (tmp) 111 goto found_middle; 112 result += BITS_PER_LONG; 113 size -= BITS_PER_LONG; 114 } 115 if (!size) 116 return result; 117 tmp = *p; 118 found_first: 119 tmp &= (~0UL >> (BITS_PER_LONG - size)); 120 if (tmp == 0UL) /* Are any bits set? */ 121 return result + size; /* Nope. */ 122 found_middle: 123 return result + __reverse_ffs(tmp); 124 } 125 126 static unsigned long __find_rev_next_zero_bit(const unsigned long *addr, 127 unsigned long size, unsigned long offset) 128 { 129 const unsigned long *p = addr + BIT_WORD(offset); 130 unsigned long result = offset & ~(BITS_PER_LONG - 1); 131 unsigned long tmp; 132 unsigned long mask, submask; 133 unsigned long quot, rest; 134 135 if (offset >= size) 136 return size; 137 138 size -= result; 139 offset %= BITS_PER_LONG; 140 if (!offset) 141 goto aligned; 142 143 tmp = *(p++); 144 quot = (offset >> 3) << 3; 145 rest = offset & 0x7; 146 mask = ~(~0UL << quot); 147 submask = (unsigned char)~((unsigned char)(0xff << rest) >> rest); 148 submask <<= quot; 149 mask += submask; 150 tmp |= mask; 151 if (size < BITS_PER_LONG) 152 goto found_first; 153 if (~tmp) 154 goto found_middle; 155 156 size -= BITS_PER_LONG; 157 result += BITS_PER_LONG; 158 aligned: 159 while (size & ~(BITS_PER_LONG - 1)) { 160 tmp = *(p++); 161 if (~tmp) 162 goto found_middle; 163 result += BITS_PER_LONG; 164 size -= BITS_PER_LONG; 165 } 166 if (!size) 167 return result; 168 tmp = *p; 169 170 found_first: 171 tmp |= ~0UL << size; 172 if (tmp == ~0UL) /* Are any bits zero? */ 173 return result + size; /* Nope. */ 174 found_middle: 175 return result + __reverse_ffz(tmp); 176 } 177 178 void register_inmem_page(struct inode *inode, struct page *page) 179 { 180 struct f2fs_inode_info *fi = F2FS_I(inode); 181 struct inmem_pages *new; 182 int err; 183 184 SetPagePrivate(page); 185 f2fs_trace_pid(page); 186 187 new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS); 188 189 /* add atomic page indices to the list */ 190 new->page = page; 191 INIT_LIST_HEAD(&new->list); 192 retry: 193 /* increase reference count with clean state */ 194 mutex_lock(&fi->inmem_lock); 195 err = radix_tree_insert(&fi->inmem_root, page->index, new); 196 if (err == -EEXIST) { 197 mutex_unlock(&fi->inmem_lock); 198 kmem_cache_free(inmem_entry_slab, new); 199 return; 200 } else if (err) { 201 mutex_unlock(&fi->inmem_lock); 202 goto retry; 203 } 204 get_page(page); 205 list_add_tail(&new->list, &fi->inmem_pages); 206 inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); 207 mutex_unlock(&fi->inmem_lock); 208 209 trace_f2fs_register_inmem_page(page, INMEM); 210 } 211 212 void commit_inmem_pages(struct inode *inode, bool abort) 213 { 214 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 215 struct f2fs_inode_info *fi = F2FS_I(inode); 216 struct inmem_pages *cur, *tmp; 217 bool submit_bio = false; 218 struct f2fs_io_info fio = { 219 .sbi = sbi, 220 .type = DATA, 221 .rw = WRITE_SYNC | REQ_PRIO, 222 }; 223 224 /* 225 * The abort is true only when f2fs_evict_inode is called. 226 * Basically, the f2fs_evict_inode doesn't produce any data writes, so 227 * that we don't need to call f2fs_balance_fs. 228 * Otherwise, f2fs_gc in f2fs_balance_fs can wait forever until this 229 * inode becomes free by iget_locked in f2fs_iget. 230 */ 231 if (!abort) { 232 f2fs_balance_fs(sbi); 233 f2fs_lock_op(sbi); 234 } 235 236 mutex_lock(&fi->inmem_lock); 237 list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) { 238 if (!abort) { 239 lock_page(cur->page); 240 if (cur->page->mapping == inode->i_mapping) { 241 f2fs_wait_on_page_writeback(cur->page, DATA); 242 if (clear_page_dirty_for_io(cur->page)) 243 inode_dec_dirty_pages(inode); 244 trace_f2fs_commit_inmem_page(cur->page, INMEM); 245 fio.page = cur->page; 246 do_write_data_page(&fio); 247 submit_bio = true; 248 } 249 f2fs_put_page(cur->page, 1); 250 } else { 251 trace_f2fs_commit_inmem_page(cur->page, INMEM_DROP); 252 put_page(cur->page); 253 } 254 radix_tree_delete(&fi->inmem_root, cur->page->index); 255 list_del(&cur->list); 256 kmem_cache_free(inmem_entry_slab, cur); 257 dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES); 258 } 259 mutex_unlock(&fi->inmem_lock); 260 261 if (!abort) { 262 f2fs_unlock_op(sbi); 263 if (submit_bio) 264 f2fs_submit_merged_bio(sbi, DATA, WRITE); 265 } 266 } 267 268 /* 269 * This function balances dirty node and dentry pages. 270 * In addition, it controls garbage collection. 271 */ 272 void f2fs_balance_fs(struct f2fs_sb_info *sbi) 273 { 274 /* 275 * We should do GC or end up with checkpoint, if there are so many dirty 276 * dir/node pages without enough free segments. 277 */ 278 if (has_not_enough_free_secs(sbi, 0)) { 279 mutex_lock(&sbi->gc_mutex); 280 f2fs_gc(sbi); 281 } 282 } 283 284 void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi) 285 { 286 /* try to shrink extent cache when there is no enough memory */ 287 f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER); 288 289 /* check the # of cached NAT entries and prefree segments */ 290 if (try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK) || 291 excess_prefree_segs(sbi) || 292 !available_free_memory(sbi, INO_ENTRIES)) 293 f2fs_sync_fs(sbi->sb, true); 294 } 295 296 static int issue_flush_thread(void *data) 297 { 298 struct f2fs_sb_info *sbi = data; 299 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; 300 wait_queue_head_t *q = &fcc->flush_wait_queue; 301 repeat: 302 if (kthread_should_stop()) 303 return 0; 304 305 if (!llist_empty(&fcc->issue_list)) { 306 struct bio *bio = bio_alloc(GFP_NOIO, 0); 307 struct flush_cmd *cmd, *next; 308 int ret; 309 310 fcc->dispatch_list = llist_del_all(&fcc->issue_list); 311 fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list); 312 313 bio->bi_bdev = sbi->sb->s_bdev; 314 ret = submit_bio_wait(WRITE_FLUSH, bio); 315 316 llist_for_each_entry_safe(cmd, next, 317 fcc->dispatch_list, llnode) { 318 cmd->ret = ret; 319 complete(&cmd->wait); 320 } 321 bio_put(bio); 322 fcc->dispatch_list = NULL; 323 } 324 325 wait_event_interruptible(*q, 326 kthread_should_stop() || !llist_empty(&fcc->issue_list)); 327 goto repeat; 328 } 329 330 int f2fs_issue_flush(struct f2fs_sb_info *sbi) 331 { 332 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; 333 struct flush_cmd cmd; 334 335 trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER), 336 test_opt(sbi, FLUSH_MERGE)); 337 338 if (test_opt(sbi, NOBARRIER)) 339 return 0; 340 341 if (!test_opt(sbi, FLUSH_MERGE)) 342 return blkdev_issue_flush(sbi->sb->s_bdev, GFP_KERNEL, NULL); 343 344 init_completion(&cmd.wait); 345 346 llist_add(&cmd.llnode, &fcc->issue_list); 347 348 if (!fcc->dispatch_list) 349 wake_up(&fcc->flush_wait_queue); 350 351 wait_for_completion(&cmd.wait); 352 353 return cmd.ret; 354 } 355 356 int create_flush_cmd_control(struct f2fs_sb_info *sbi) 357 { 358 dev_t dev = sbi->sb->s_bdev->bd_dev; 359 struct flush_cmd_control *fcc; 360 int err = 0; 361 362 fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL); 363 if (!fcc) 364 return -ENOMEM; 365 init_waitqueue_head(&fcc->flush_wait_queue); 366 init_llist_head(&fcc->issue_list); 367 SM_I(sbi)->cmd_control_info = fcc; 368 fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi, 369 "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev)); 370 if (IS_ERR(fcc->f2fs_issue_flush)) { 371 err = PTR_ERR(fcc->f2fs_issue_flush); 372 kfree(fcc); 373 SM_I(sbi)->cmd_control_info = NULL; 374 return err; 375 } 376 377 return err; 378 } 379 380 void destroy_flush_cmd_control(struct f2fs_sb_info *sbi) 381 { 382 struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info; 383 384 if (fcc && fcc->f2fs_issue_flush) 385 kthread_stop(fcc->f2fs_issue_flush); 386 kfree(fcc); 387 SM_I(sbi)->cmd_control_info = NULL; 388 } 389 390 static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 391 enum dirty_type dirty_type) 392 { 393 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 394 395 /* need not be added */ 396 if (IS_CURSEG(sbi, segno)) 397 return; 398 399 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type])) 400 dirty_i->nr_dirty[dirty_type]++; 401 402 if (dirty_type == DIRTY) { 403 struct seg_entry *sentry = get_seg_entry(sbi, segno); 404 enum dirty_type t = sentry->type; 405 406 if (unlikely(t >= DIRTY)) { 407 f2fs_bug_on(sbi, 1); 408 return; 409 } 410 if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t])) 411 dirty_i->nr_dirty[t]++; 412 } 413 } 414 415 static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno, 416 enum dirty_type dirty_type) 417 { 418 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 419 420 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type])) 421 dirty_i->nr_dirty[dirty_type]--; 422 423 if (dirty_type == DIRTY) { 424 struct seg_entry *sentry = get_seg_entry(sbi, segno); 425 enum dirty_type t = sentry->type; 426 427 if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t])) 428 dirty_i->nr_dirty[t]--; 429 430 if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0) 431 clear_bit(GET_SECNO(sbi, segno), 432 dirty_i->victim_secmap); 433 } 434 } 435 436 /* 437 * Should not occur error such as -ENOMEM. 438 * Adding dirty entry into seglist is not critical operation. 439 * If a given segment is one of current working segments, it won't be added. 440 */ 441 static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno) 442 { 443 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 444 unsigned short valid_blocks; 445 446 if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno)) 447 return; 448 449 mutex_lock(&dirty_i->seglist_lock); 450 451 valid_blocks = get_valid_blocks(sbi, segno, 0); 452 453 if (valid_blocks == 0) { 454 __locate_dirty_segment(sbi, segno, PRE); 455 __remove_dirty_segment(sbi, segno, DIRTY); 456 } else if (valid_blocks < sbi->blocks_per_seg) { 457 __locate_dirty_segment(sbi, segno, DIRTY); 458 } else { 459 /* Recovery routine with SSR needs this */ 460 __remove_dirty_segment(sbi, segno, DIRTY); 461 } 462 463 mutex_unlock(&dirty_i->seglist_lock); 464 } 465 466 static int f2fs_issue_discard(struct f2fs_sb_info *sbi, 467 block_t blkstart, block_t blklen) 468 { 469 sector_t start = SECTOR_FROM_BLOCK(blkstart); 470 sector_t len = SECTOR_FROM_BLOCK(blklen); 471 struct seg_entry *se; 472 unsigned int offset; 473 block_t i; 474 475 for (i = blkstart; i < blkstart + blklen; i++) { 476 se = get_seg_entry(sbi, GET_SEGNO(sbi, i)); 477 offset = GET_BLKOFF_FROM_SEG0(sbi, i); 478 479 if (!f2fs_test_and_set_bit(offset, se->discard_map)) 480 sbi->discard_blks--; 481 } 482 trace_f2fs_issue_discard(sbi->sb, blkstart, blklen); 483 return blkdev_issue_discard(sbi->sb->s_bdev, start, len, GFP_NOFS, 0); 484 } 485 486 void discard_next_dnode(struct f2fs_sb_info *sbi, block_t blkaddr) 487 { 488 if (f2fs_issue_discard(sbi, blkaddr, 1)) { 489 struct page *page = grab_meta_page(sbi, blkaddr); 490 /* zero-filled page */ 491 set_page_dirty(page); 492 f2fs_put_page(page, 1); 493 } 494 } 495 496 static void __add_discard_entry(struct f2fs_sb_info *sbi, 497 struct cp_control *cpc, struct seg_entry *se, 498 unsigned int start, unsigned int end) 499 { 500 struct list_head *head = &SM_I(sbi)->discard_list; 501 struct discard_entry *new, *last; 502 503 if (!list_empty(head)) { 504 last = list_last_entry(head, struct discard_entry, list); 505 if (START_BLOCK(sbi, cpc->trim_start) + start == 506 last->blkaddr + last->len) { 507 last->len += end - start; 508 goto done; 509 } 510 } 511 512 new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS); 513 INIT_LIST_HEAD(&new->list); 514 new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start; 515 new->len = end - start; 516 list_add_tail(&new->list, head); 517 done: 518 SM_I(sbi)->nr_discards += end - start; 519 cpc->trimmed += end - start; 520 } 521 522 static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc) 523 { 524 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 525 int max_blocks = sbi->blocks_per_seg; 526 struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start); 527 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 528 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 529 unsigned long *discard_map = (unsigned long *)se->discard_map; 530 unsigned long *dmap = SIT_I(sbi)->tmp_map; 531 unsigned int start = 0, end = -1; 532 bool force = (cpc->reason == CP_DISCARD); 533 int i; 534 535 if (se->valid_blocks == max_blocks) 536 return; 537 538 if (!force) { 539 if (!test_opt(sbi, DISCARD) || !se->valid_blocks || 540 SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards) 541 return; 542 } 543 544 /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */ 545 for (i = 0; i < entries; i++) 546 dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] : 547 (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i]; 548 549 while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) { 550 start = __find_rev_next_bit(dmap, max_blocks, end + 1); 551 if (start >= max_blocks) 552 break; 553 554 end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1); 555 __add_discard_entry(sbi, cpc, se, start, end); 556 } 557 } 558 559 void release_discard_addrs(struct f2fs_sb_info *sbi) 560 { 561 struct list_head *head = &(SM_I(sbi)->discard_list); 562 struct discard_entry *entry, *this; 563 564 /* drop caches */ 565 list_for_each_entry_safe(entry, this, head, list) { 566 list_del(&entry->list); 567 kmem_cache_free(discard_entry_slab, entry); 568 } 569 } 570 571 /* 572 * Should call clear_prefree_segments after checkpoint is done. 573 */ 574 static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi) 575 { 576 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 577 unsigned int segno; 578 579 mutex_lock(&dirty_i->seglist_lock); 580 for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi)) 581 __set_test_and_free(sbi, segno); 582 mutex_unlock(&dirty_i->seglist_lock); 583 } 584 585 void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc) 586 { 587 struct list_head *head = &(SM_I(sbi)->discard_list); 588 struct discard_entry *entry, *this; 589 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 590 unsigned long *prefree_map = dirty_i->dirty_segmap[PRE]; 591 unsigned int start = 0, end = -1; 592 593 mutex_lock(&dirty_i->seglist_lock); 594 595 while (1) { 596 int i; 597 start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1); 598 if (start >= MAIN_SEGS(sbi)) 599 break; 600 end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi), 601 start + 1); 602 603 for (i = start; i < end; i++) 604 clear_bit(i, prefree_map); 605 606 dirty_i->nr_dirty[PRE] -= end - start; 607 608 if (!test_opt(sbi, DISCARD)) 609 continue; 610 611 f2fs_issue_discard(sbi, START_BLOCK(sbi, start), 612 (end - start) << sbi->log_blocks_per_seg); 613 } 614 mutex_unlock(&dirty_i->seglist_lock); 615 616 /* send small discards */ 617 list_for_each_entry_safe(entry, this, head, list) { 618 if (cpc->reason == CP_DISCARD && entry->len < cpc->trim_minlen) 619 goto skip; 620 f2fs_issue_discard(sbi, entry->blkaddr, entry->len); 621 skip: 622 list_del(&entry->list); 623 SM_I(sbi)->nr_discards -= entry->len; 624 kmem_cache_free(discard_entry_slab, entry); 625 } 626 } 627 628 static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno) 629 { 630 struct sit_info *sit_i = SIT_I(sbi); 631 632 if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) { 633 sit_i->dirty_sentries++; 634 return false; 635 } 636 637 return true; 638 } 639 640 static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type, 641 unsigned int segno, int modified) 642 { 643 struct seg_entry *se = get_seg_entry(sbi, segno); 644 se->type = type; 645 if (modified) 646 __mark_sit_entry_dirty(sbi, segno); 647 } 648 649 static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del) 650 { 651 struct seg_entry *se; 652 unsigned int segno, offset; 653 long int new_vblocks; 654 655 segno = GET_SEGNO(sbi, blkaddr); 656 657 se = get_seg_entry(sbi, segno); 658 new_vblocks = se->valid_blocks + del; 659 offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr); 660 661 f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) || 662 (new_vblocks > sbi->blocks_per_seg))); 663 664 se->valid_blocks = new_vblocks; 665 se->mtime = get_mtime(sbi); 666 SIT_I(sbi)->max_mtime = se->mtime; 667 668 /* Update valid block bitmap */ 669 if (del > 0) { 670 if (f2fs_test_and_set_bit(offset, se->cur_valid_map)) 671 f2fs_bug_on(sbi, 1); 672 if (!f2fs_test_and_set_bit(offset, se->discard_map)) 673 sbi->discard_blks--; 674 } else { 675 if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map)) 676 f2fs_bug_on(sbi, 1); 677 if (f2fs_test_and_clear_bit(offset, se->discard_map)) 678 sbi->discard_blks++; 679 } 680 if (!f2fs_test_bit(offset, se->ckpt_valid_map)) 681 se->ckpt_valid_blocks += del; 682 683 __mark_sit_entry_dirty(sbi, segno); 684 685 /* update total number of valid blocks to be written in ckpt area */ 686 SIT_I(sbi)->written_valid_blocks += del; 687 688 if (sbi->segs_per_sec > 1) 689 get_sec_entry(sbi, segno)->valid_blocks += del; 690 } 691 692 void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new) 693 { 694 update_sit_entry(sbi, new, 1); 695 if (GET_SEGNO(sbi, old) != NULL_SEGNO) 696 update_sit_entry(sbi, old, -1); 697 698 locate_dirty_segment(sbi, GET_SEGNO(sbi, old)); 699 locate_dirty_segment(sbi, GET_SEGNO(sbi, new)); 700 } 701 702 void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr) 703 { 704 unsigned int segno = GET_SEGNO(sbi, addr); 705 struct sit_info *sit_i = SIT_I(sbi); 706 707 f2fs_bug_on(sbi, addr == NULL_ADDR); 708 if (addr == NEW_ADDR) 709 return; 710 711 /* add it into sit main buffer */ 712 mutex_lock(&sit_i->sentry_lock); 713 714 update_sit_entry(sbi, addr, -1); 715 716 /* add it into dirty seglist */ 717 locate_dirty_segment(sbi, segno); 718 719 mutex_unlock(&sit_i->sentry_lock); 720 } 721 722 /* 723 * This function should be resided under the curseg_mutex lock 724 */ 725 static void __add_sum_entry(struct f2fs_sb_info *sbi, int type, 726 struct f2fs_summary *sum) 727 { 728 struct curseg_info *curseg = CURSEG_I(sbi, type); 729 void *addr = curseg->sum_blk; 730 addr += curseg->next_blkoff * sizeof(struct f2fs_summary); 731 memcpy(addr, sum, sizeof(struct f2fs_summary)); 732 } 733 734 /* 735 * Calculate the number of current summary pages for writing 736 */ 737 int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra) 738 { 739 int valid_sum_count = 0; 740 int i, sum_in_page; 741 742 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 743 if (sbi->ckpt->alloc_type[i] == SSR) 744 valid_sum_count += sbi->blocks_per_seg; 745 else { 746 if (for_ra) 747 valid_sum_count += le16_to_cpu( 748 F2FS_CKPT(sbi)->cur_data_blkoff[i]); 749 else 750 valid_sum_count += curseg_blkoff(sbi, i); 751 } 752 } 753 754 sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE - 755 SUM_FOOTER_SIZE) / SUMMARY_SIZE; 756 if (valid_sum_count <= sum_in_page) 757 return 1; 758 else if ((valid_sum_count - sum_in_page) <= 759 (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE) 760 return 2; 761 return 3; 762 } 763 764 /* 765 * Caller should put this summary page 766 */ 767 struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno) 768 { 769 return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno)); 770 } 771 772 static void write_sum_page(struct f2fs_sb_info *sbi, 773 struct f2fs_summary_block *sum_blk, block_t blk_addr) 774 { 775 struct page *page = grab_meta_page(sbi, blk_addr); 776 void *kaddr = page_address(page); 777 memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE); 778 set_page_dirty(page); 779 f2fs_put_page(page, 1); 780 } 781 782 static int is_next_segment_free(struct f2fs_sb_info *sbi, int type) 783 { 784 struct curseg_info *curseg = CURSEG_I(sbi, type); 785 unsigned int segno = curseg->segno + 1; 786 struct free_segmap_info *free_i = FREE_I(sbi); 787 788 if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec) 789 return !test_bit(segno, free_i->free_segmap); 790 return 0; 791 } 792 793 /* 794 * Find a new segment from the free segments bitmap to right order 795 * This function should be returned with success, otherwise BUG 796 */ 797 static void get_new_segment(struct f2fs_sb_info *sbi, 798 unsigned int *newseg, bool new_sec, int dir) 799 { 800 struct free_segmap_info *free_i = FREE_I(sbi); 801 unsigned int segno, secno, zoneno; 802 unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone; 803 unsigned int hint = *newseg / sbi->segs_per_sec; 804 unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg); 805 unsigned int left_start = hint; 806 bool init = true; 807 int go_left = 0; 808 int i; 809 810 spin_lock(&free_i->segmap_lock); 811 812 if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) { 813 segno = find_next_zero_bit(free_i->free_segmap, 814 MAIN_SEGS(sbi), *newseg + 1); 815 if (segno - *newseg < sbi->segs_per_sec - 816 (*newseg % sbi->segs_per_sec)) 817 goto got_it; 818 } 819 find_other_zone: 820 secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint); 821 if (secno >= MAIN_SECS(sbi)) { 822 if (dir == ALLOC_RIGHT) { 823 secno = find_next_zero_bit(free_i->free_secmap, 824 MAIN_SECS(sbi), 0); 825 f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi)); 826 } else { 827 go_left = 1; 828 left_start = hint - 1; 829 } 830 } 831 if (go_left == 0) 832 goto skip_left; 833 834 while (test_bit(left_start, free_i->free_secmap)) { 835 if (left_start > 0) { 836 left_start--; 837 continue; 838 } 839 left_start = find_next_zero_bit(free_i->free_secmap, 840 MAIN_SECS(sbi), 0); 841 f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi)); 842 break; 843 } 844 secno = left_start; 845 skip_left: 846 hint = secno; 847 segno = secno * sbi->segs_per_sec; 848 zoneno = secno / sbi->secs_per_zone; 849 850 /* give up on finding another zone */ 851 if (!init) 852 goto got_it; 853 if (sbi->secs_per_zone == 1) 854 goto got_it; 855 if (zoneno == old_zoneno) 856 goto got_it; 857 if (dir == ALLOC_LEFT) { 858 if (!go_left && zoneno + 1 >= total_zones) 859 goto got_it; 860 if (go_left && zoneno == 0) 861 goto got_it; 862 } 863 for (i = 0; i < NR_CURSEG_TYPE; i++) 864 if (CURSEG_I(sbi, i)->zone == zoneno) 865 break; 866 867 if (i < NR_CURSEG_TYPE) { 868 /* zone is in user, try another */ 869 if (go_left) 870 hint = zoneno * sbi->secs_per_zone - 1; 871 else if (zoneno + 1 >= total_zones) 872 hint = 0; 873 else 874 hint = (zoneno + 1) * sbi->secs_per_zone; 875 init = false; 876 goto find_other_zone; 877 } 878 got_it: 879 /* set it as dirty segment in free segmap */ 880 f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap)); 881 __set_inuse(sbi, segno); 882 *newseg = segno; 883 spin_unlock(&free_i->segmap_lock); 884 } 885 886 static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified) 887 { 888 struct curseg_info *curseg = CURSEG_I(sbi, type); 889 struct summary_footer *sum_footer; 890 891 curseg->segno = curseg->next_segno; 892 curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno); 893 curseg->next_blkoff = 0; 894 curseg->next_segno = NULL_SEGNO; 895 896 sum_footer = &(curseg->sum_blk->footer); 897 memset(sum_footer, 0, sizeof(struct summary_footer)); 898 if (IS_DATASEG(type)) 899 SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA); 900 if (IS_NODESEG(type)) 901 SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE); 902 __set_sit_entry_type(sbi, type, curseg->segno, modified); 903 } 904 905 /* 906 * Allocate a current working segment. 907 * This function always allocates a free segment in LFS manner. 908 */ 909 static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec) 910 { 911 struct curseg_info *curseg = CURSEG_I(sbi, type); 912 unsigned int segno = curseg->segno; 913 int dir = ALLOC_LEFT; 914 915 write_sum_page(sbi, curseg->sum_blk, 916 GET_SUM_BLOCK(sbi, segno)); 917 if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA) 918 dir = ALLOC_RIGHT; 919 920 if (test_opt(sbi, NOHEAP)) 921 dir = ALLOC_RIGHT; 922 923 get_new_segment(sbi, &segno, new_sec, dir); 924 curseg->next_segno = segno; 925 reset_curseg(sbi, type, 1); 926 curseg->alloc_type = LFS; 927 } 928 929 static void __next_free_blkoff(struct f2fs_sb_info *sbi, 930 struct curseg_info *seg, block_t start) 931 { 932 struct seg_entry *se = get_seg_entry(sbi, seg->segno); 933 int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long); 934 unsigned long *target_map = SIT_I(sbi)->tmp_map; 935 unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map; 936 unsigned long *cur_map = (unsigned long *)se->cur_valid_map; 937 int i, pos; 938 939 for (i = 0; i < entries; i++) 940 target_map[i] = ckpt_map[i] | cur_map[i]; 941 942 pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start); 943 944 seg->next_blkoff = pos; 945 } 946 947 /* 948 * If a segment is written by LFS manner, next block offset is just obtained 949 * by increasing the current block offset. However, if a segment is written by 950 * SSR manner, next block offset obtained by calling __next_free_blkoff 951 */ 952 static void __refresh_next_blkoff(struct f2fs_sb_info *sbi, 953 struct curseg_info *seg) 954 { 955 if (seg->alloc_type == SSR) 956 __next_free_blkoff(sbi, seg, seg->next_blkoff + 1); 957 else 958 seg->next_blkoff++; 959 } 960 961 /* 962 * This function always allocates a used segment(from dirty seglist) by SSR 963 * manner, so it should recover the existing segment information of valid blocks 964 */ 965 static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse) 966 { 967 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 968 struct curseg_info *curseg = CURSEG_I(sbi, type); 969 unsigned int new_segno = curseg->next_segno; 970 struct f2fs_summary_block *sum_node; 971 struct page *sum_page; 972 973 write_sum_page(sbi, curseg->sum_blk, 974 GET_SUM_BLOCK(sbi, curseg->segno)); 975 __set_test_and_inuse(sbi, new_segno); 976 977 mutex_lock(&dirty_i->seglist_lock); 978 __remove_dirty_segment(sbi, new_segno, PRE); 979 __remove_dirty_segment(sbi, new_segno, DIRTY); 980 mutex_unlock(&dirty_i->seglist_lock); 981 982 reset_curseg(sbi, type, 1); 983 curseg->alloc_type = SSR; 984 __next_free_blkoff(sbi, curseg, 0); 985 986 if (reuse) { 987 sum_page = get_sum_page(sbi, new_segno); 988 sum_node = (struct f2fs_summary_block *)page_address(sum_page); 989 memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE); 990 f2fs_put_page(sum_page, 1); 991 } 992 } 993 994 static int get_ssr_segment(struct f2fs_sb_info *sbi, int type) 995 { 996 struct curseg_info *curseg = CURSEG_I(sbi, type); 997 const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops; 998 999 if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0)) 1000 return v_ops->get_victim(sbi, 1001 &(curseg)->next_segno, BG_GC, type, SSR); 1002 1003 /* For data segments, let's do SSR more intensively */ 1004 for (; type >= CURSEG_HOT_DATA; type--) 1005 if (v_ops->get_victim(sbi, &(curseg)->next_segno, 1006 BG_GC, type, SSR)) 1007 return 1; 1008 return 0; 1009 } 1010 1011 /* 1012 * flush out current segment and replace it with new segment 1013 * This function should be returned with success, otherwise BUG 1014 */ 1015 static void allocate_segment_by_default(struct f2fs_sb_info *sbi, 1016 int type, bool force) 1017 { 1018 struct curseg_info *curseg = CURSEG_I(sbi, type); 1019 1020 if (force) 1021 new_curseg(sbi, type, true); 1022 else if (type == CURSEG_WARM_NODE) 1023 new_curseg(sbi, type, false); 1024 else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type)) 1025 new_curseg(sbi, type, false); 1026 else if (need_SSR(sbi) && get_ssr_segment(sbi, type)) 1027 change_curseg(sbi, type, true); 1028 else 1029 new_curseg(sbi, type, false); 1030 1031 stat_inc_seg_type(sbi, curseg); 1032 } 1033 1034 static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type) 1035 { 1036 struct curseg_info *curseg = CURSEG_I(sbi, type); 1037 unsigned int old_segno; 1038 1039 old_segno = curseg->segno; 1040 SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true); 1041 locate_dirty_segment(sbi, old_segno); 1042 } 1043 1044 void allocate_new_segments(struct f2fs_sb_info *sbi) 1045 { 1046 int i; 1047 1048 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) 1049 __allocate_new_segments(sbi, i); 1050 } 1051 1052 static const struct segment_allocation default_salloc_ops = { 1053 .allocate_segment = allocate_segment_by_default, 1054 }; 1055 1056 int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range) 1057 { 1058 __u64 start = F2FS_BYTES_TO_BLK(range->start); 1059 __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1; 1060 unsigned int start_segno, end_segno; 1061 struct cp_control cpc; 1062 1063 if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize) 1064 return -EINVAL; 1065 1066 cpc.trimmed = 0; 1067 if (end <= MAIN_BLKADDR(sbi)) 1068 goto out; 1069 1070 /* start/end segment number in main_area */ 1071 start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start); 1072 end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 : 1073 GET_SEGNO(sbi, end); 1074 cpc.reason = CP_DISCARD; 1075 cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen)); 1076 1077 /* do checkpoint to issue discard commands safely */ 1078 for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) { 1079 cpc.trim_start = start_segno; 1080 1081 if (sbi->discard_blks == 0) 1082 break; 1083 else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi)) 1084 cpc.trim_end = end_segno; 1085 else 1086 cpc.trim_end = min_t(unsigned int, 1087 rounddown(start_segno + 1088 BATCHED_TRIM_SEGMENTS(sbi), 1089 sbi->segs_per_sec) - 1, end_segno); 1090 1091 mutex_lock(&sbi->gc_mutex); 1092 write_checkpoint(sbi, &cpc); 1093 mutex_unlock(&sbi->gc_mutex); 1094 } 1095 out: 1096 range->len = F2FS_BLK_TO_BYTES(cpc.trimmed); 1097 return 0; 1098 } 1099 1100 static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type) 1101 { 1102 struct curseg_info *curseg = CURSEG_I(sbi, type); 1103 if (curseg->next_blkoff < sbi->blocks_per_seg) 1104 return true; 1105 return false; 1106 } 1107 1108 static int __get_segment_type_2(struct page *page, enum page_type p_type) 1109 { 1110 if (p_type == DATA) 1111 return CURSEG_HOT_DATA; 1112 else 1113 return CURSEG_HOT_NODE; 1114 } 1115 1116 static int __get_segment_type_4(struct page *page, enum page_type p_type) 1117 { 1118 if (p_type == DATA) { 1119 struct inode *inode = page->mapping->host; 1120 1121 if (S_ISDIR(inode->i_mode)) 1122 return CURSEG_HOT_DATA; 1123 else 1124 return CURSEG_COLD_DATA; 1125 } else { 1126 if (IS_DNODE(page) && is_cold_node(page)) 1127 return CURSEG_WARM_NODE; 1128 else 1129 return CURSEG_COLD_NODE; 1130 } 1131 } 1132 1133 static int __get_segment_type_6(struct page *page, enum page_type p_type) 1134 { 1135 if (p_type == DATA) { 1136 struct inode *inode = page->mapping->host; 1137 1138 if (S_ISDIR(inode->i_mode)) 1139 return CURSEG_HOT_DATA; 1140 else if (is_cold_data(page) || file_is_cold(inode)) 1141 return CURSEG_COLD_DATA; 1142 else 1143 return CURSEG_WARM_DATA; 1144 } else { 1145 if (IS_DNODE(page)) 1146 return is_cold_node(page) ? CURSEG_WARM_NODE : 1147 CURSEG_HOT_NODE; 1148 else 1149 return CURSEG_COLD_NODE; 1150 } 1151 } 1152 1153 static int __get_segment_type(struct page *page, enum page_type p_type) 1154 { 1155 switch (F2FS_P_SB(page)->active_logs) { 1156 case 2: 1157 return __get_segment_type_2(page, p_type); 1158 case 4: 1159 return __get_segment_type_4(page, p_type); 1160 } 1161 /* NR_CURSEG_TYPE(6) logs by default */ 1162 f2fs_bug_on(F2FS_P_SB(page), 1163 F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE); 1164 return __get_segment_type_6(page, p_type); 1165 } 1166 1167 void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page, 1168 block_t old_blkaddr, block_t *new_blkaddr, 1169 struct f2fs_summary *sum, int type) 1170 { 1171 struct sit_info *sit_i = SIT_I(sbi); 1172 struct curseg_info *curseg; 1173 bool direct_io = (type == CURSEG_DIRECT_IO); 1174 1175 type = direct_io ? CURSEG_WARM_DATA : type; 1176 1177 curseg = CURSEG_I(sbi, type); 1178 1179 mutex_lock(&curseg->curseg_mutex); 1180 mutex_lock(&sit_i->sentry_lock); 1181 1182 /* direct_io'ed data is aligned to the segment for better performance */ 1183 if (direct_io && curseg->next_blkoff) 1184 __allocate_new_segments(sbi, type); 1185 1186 *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg); 1187 1188 /* 1189 * __add_sum_entry should be resided under the curseg_mutex 1190 * because, this function updates a summary entry in the 1191 * current summary block. 1192 */ 1193 __add_sum_entry(sbi, type, sum); 1194 1195 __refresh_next_blkoff(sbi, curseg); 1196 1197 stat_inc_block_count(sbi, curseg); 1198 1199 if (!__has_curseg_space(sbi, type)) 1200 sit_i->s_ops->allocate_segment(sbi, type, false); 1201 /* 1202 * SIT information should be updated before segment allocation, 1203 * since SSR needs latest valid block information. 1204 */ 1205 refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr); 1206 1207 mutex_unlock(&sit_i->sentry_lock); 1208 1209 if (page && IS_NODESEG(type)) 1210 fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg)); 1211 1212 mutex_unlock(&curseg->curseg_mutex); 1213 } 1214 1215 static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio) 1216 { 1217 int type = __get_segment_type(fio->page, fio->type); 1218 1219 allocate_data_block(fio->sbi, fio->page, fio->blk_addr, 1220 &fio->blk_addr, sum, type); 1221 1222 /* writeout dirty page into bdev */ 1223 f2fs_submit_page_mbio(fio); 1224 } 1225 1226 void write_meta_page(struct f2fs_sb_info *sbi, struct page *page) 1227 { 1228 struct f2fs_io_info fio = { 1229 .sbi = sbi, 1230 .type = META, 1231 .rw = WRITE_SYNC | REQ_META | REQ_PRIO, 1232 .blk_addr = page->index, 1233 .page = page, 1234 }; 1235 1236 set_page_writeback(page); 1237 f2fs_submit_page_mbio(&fio); 1238 } 1239 1240 void write_node_page(unsigned int nid, struct f2fs_io_info *fio) 1241 { 1242 struct f2fs_summary sum; 1243 1244 set_summary(&sum, nid, 0, 0); 1245 do_write_page(&sum, fio); 1246 } 1247 1248 void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio) 1249 { 1250 struct f2fs_sb_info *sbi = fio->sbi; 1251 struct f2fs_summary sum; 1252 struct node_info ni; 1253 1254 f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR); 1255 get_node_info(sbi, dn->nid, &ni); 1256 set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version); 1257 do_write_page(&sum, fio); 1258 dn->data_blkaddr = fio->blk_addr; 1259 } 1260 1261 void rewrite_data_page(struct f2fs_io_info *fio) 1262 { 1263 stat_inc_inplace_blocks(fio->sbi); 1264 f2fs_submit_page_mbio(fio); 1265 } 1266 1267 void f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, 1268 block_t old_blkaddr, block_t new_blkaddr, 1269 bool recover_curseg) 1270 { 1271 struct sit_info *sit_i = SIT_I(sbi); 1272 struct curseg_info *curseg; 1273 unsigned int segno, old_cursegno; 1274 struct seg_entry *se; 1275 int type; 1276 unsigned short old_blkoff; 1277 1278 segno = GET_SEGNO(sbi, new_blkaddr); 1279 se = get_seg_entry(sbi, segno); 1280 type = se->type; 1281 1282 if (!recover_curseg) { 1283 /* for recovery flow */ 1284 if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) { 1285 if (old_blkaddr == NULL_ADDR) 1286 type = CURSEG_COLD_DATA; 1287 else 1288 type = CURSEG_WARM_DATA; 1289 } 1290 } else { 1291 if (!IS_CURSEG(sbi, segno)) 1292 type = CURSEG_WARM_DATA; 1293 } 1294 1295 curseg = CURSEG_I(sbi, type); 1296 1297 mutex_lock(&curseg->curseg_mutex); 1298 mutex_lock(&sit_i->sentry_lock); 1299 1300 old_cursegno = curseg->segno; 1301 old_blkoff = curseg->next_blkoff; 1302 1303 /* change the current segment */ 1304 if (segno != curseg->segno) { 1305 curseg->next_segno = segno; 1306 change_curseg(sbi, type, true); 1307 } 1308 1309 curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr); 1310 __add_sum_entry(sbi, type, sum); 1311 1312 refresh_sit_entry(sbi, old_blkaddr, new_blkaddr); 1313 locate_dirty_segment(sbi, old_cursegno); 1314 1315 if (recover_curseg) { 1316 if (old_cursegno != curseg->segno) { 1317 curseg->next_segno = old_cursegno; 1318 change_curseg(sbi, type, true); 1319 } 1320 curseg->next_blkoff = old_blkoff; 1321 } 1322 1323 mutex_unlock(&sit_i->sentry_lock); 1324 mutex_unlock(&curseg->curseg_mutex); 1325 } 1326 1327 static inline bool is_merged_page(struct f2fs_sb_info *sbi, 1328 struct page *page, enum page_type type) 1329 { 1330 enum page_type btype = PAGE_TYPE_OF_BIO(type); 1331 struct f2fs_bio_info *io = &sbi->write_io[btype]; 1332 struct bio_vec *bvec; 1333 int i; 1334 1335 down_read(&io->io_rwsem); 1336 if (!io->bio) 1337 goto out; 1338 1339 bio_for_each_segment_all(bvec, io->bio, i) { 1340 if (page == bvec->bv_page) { 1341 up_read(&io->io_rwsem); 1342 return true; 1343 } 1344 } 1345 1346 out: 1347 up_read(&io->io_rwsem); 1348 return false; 1349 } 1350 1351 void f2fs_wait_on_page_writeback(struct page *page, 1352 enum page_type type) 1353 { 1354 if (PageWriteback(page)) { 1355 struct f2fs_sb_info *sbi = F2FS_P_SB(page); 1356 1357 if (is_merged_page(sbi, page, type)) 1358 f2fs_submit_merged_bio(sbi, type, WRITE); 1359 wait_on_page_writeback(page); 1360 } 1361 } 1362 1363 static int read_compacted_summaries(struct f2fs_sb_info *sbi) 1364 { 1365 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1366 struct curseg_info *seg_i; 1367 unsigned char *kaddr; 1368 struct page *page; 1369 block_t start; 1370 int i, j, offset; 1371 1372 start = start_sum_block(sbi); 1373 1374 page = get_meta_page(sbi, start++); 1375 kaddr = (unsigned char *)page_address(page); 1376 1377 /* Step 1: restore nat cache */ 1378 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 1379 memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE); 1380 1381 /* Step 2: restore sit cache */ 1382 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 1383 memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE, 1384 SUM_JOURNAL_SIZE); 1385 offset = 2 * SUM_JOURNAL_SIZE; 1386 1387 /* Step 3: restore summary entries */ 1388 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 1389 unsigned short blk_off; 1390 unsigned int segno; 1391 1392 seg_i = CURSEG_I(sbi, i); 1393 segno = le32_to_cpu(ckpt->cur_data_segno[i]); 1394 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]); 1395 seg_i->next_segno = segno; 1396 reset_curseg(sbi, i, 0); 1397 seg_i->alloc_type = ckpt->alloc_type[i]; 1398 seg_i->next_blkoff = blk_off; 1399 1400 if (seg_i->alloc_type == SSR) 1401 blk_off = sbi->blocks_per_seg; 1402 1403 for (j = 0; j < blk_off; j++) { 1404 struct f2fs_summary *s; 1405 s = (struct f2fs_summary *)(kaddr + offset); 1406 seg_i->sum_blk->entries[j] = *s; 1407 offset += SUMMARY_SIZE; 1408 if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE - 1409 SUM_FOOTER_SIZE) 1410 continue; 1411 1412 f2fs_put_page(page, 1); 1413 page = NULL; 1414 1415 page = get_meta_page(sbi, start++); 1416 kaddr = (unsigned char *)page_address(page); 1417 offset = 0; 1418 } 1419 } 1420 f2fs_put_page(page, 1); 1421 return 0; 1422 } 1423 1424 static int read_normal_summaries(struct f2fs_sb_info *sbi, int type) 1425 { 1426 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1427 struct f2fs_summary_block *sum; 1428 struct curseg_info *curseg; 1429 struct page *new; 1430 unsigned short blk_off; 1431 unsigned int segno = 0; 1432 block_t blk_addr = 0; 1433 1434 /* get segment number and block addr */ 1435 if (IS_DATASEG(type)) { 1436 segno = le32_to_cpu(ckpt->cur_data_segno[type]); 1437 blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type - 1438 CURSEG_HOT_DATA]); 1439 if (__exist_node_summaries(sbi)) 1440 blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type); 1441 else 1442 blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type); 1443 } else { 1444 segno = le32_to_cpu(ckpt->cur_node_segno[type - 1445 CURSEG_HOT_NODE]); 1446 blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type - 1447 CURSEG_HOT_NODE]); 1448 if (__exist_node_summaries(sbi)) 1449 blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE, 1450 type - CURSEG_HOT_NODE); 1451 else 1452 blk_addr = GET_SUM_BLOCK(sbi, segno); 1453 } 1454 1455 new = get_meta_page(sbi, blk_addr); 1456 sum = (struct f2fs_summary_block *)page_address(new); 1457 1458 if (IS_NODESEG(type)) { 1459 if (__exist_node_summaries(sbi)) { 1460 struct f2fs_summary *ns = &sum->entries[0]; 1461 int i; 1462 for (i = 0; i < sbi->blocks_per_seg; i++, ns++) { 1463 ns->version = 0; 1464 ns->ofs_in_node = 0; 1465 } 1466 } else { 1467 int err; 1468 1469 err = restore_node_summary(sbi, segno, sum); 1470 if (err) { 1471 f2fs_put_page(new, 1); 1472 return err; 1473 } 1474 } 1475 } 1476 1477 /* set uncompleted segment to curseg */ 1478 curseg = CURSEG_I(sbi, type); 1479 mutex_lock(&curseg->curseg_mutex); 1480 memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE); 1481 curseg->next_segno = segno; 1482 reset_curseg(sbi, type, 0); 1483 curseg->alloc_type = ckpt->alloc_type[type]; 1484 curseg->next_blkoff = blk_off; 1485 mutex_unlock(&curseg->curseg_mutex); 1486 f2fs_put_page(new, 1); 1487 return 0; 1488 } 1489 1490 static int restore_curseg_summaries(struct f2fs_sb_info *sbi) 1491 { 1492 int type = CURSEG_HOT_DATA; 1493 int err; 1494 1495 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) { 1496 int npages = npages_for_summary_flush(sbi, true); 1497 1498 if (npages >= 2) 1499 ra_meta_pages(sbi, start_sum_block(sbi), npages, 1500 META_CP); 1501 1502 /* restore for compacted data summary */ 1503 if (read_compacted_summaries(sbi)) 1504 return -EINVAL; 1505 type = CURSEG_HOT_NODE; 1506 } 1507 1508 if (__exist_node_summaries(sbi)) 1509 ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type), 1510 NR_CURSEG_TYPE - type, META_CP); 1511 1512 for (; type <= CURSEG_COLD_NODE; type++) { 1513 err = read_normal_summaries(sbi, type); 1514 if (err) 1515 return err; 1516 } 1517 1518 return 0; 1519 } 1520 1521 static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr) 1522 { 1523 struct page *page; 1524 unsigned char *kaddr; 1525 struct f2fs_summary *summary; 1526 struct curseg_info *seg_i; 1527 int written_size = 0; 1528 int i, j; 1529 1530 page = grab_meta_page(sbi, blkaddr++); 1531 kaddr = (unsigned char *)page_address(page); 1532 1533 /* Step 1: write nat cache */ 1534 seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA); 1535 memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE); 1536 written_size += SUM_JOURNAL_SIZE; 1537 1538 /* Step 2: write sit cache */ 1539 seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA); 1540 memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits, 1541 SUM_JOURNAL_SIZE); 1542 written_size += SUM_JOURNAL_SIZE; 1543 1544 /* Step 3: write summary entries */ 1545 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) { 1546 unsigned short blkoff; 1547 seg_i = CURSEG_I(sbi, i); 1548 if (sbi->ckpt->alloc_type[i] == SSR) 1549 blkoff = sbi->blocks_per_seg; 1550 else 1551 blkoff = curseg_blkoff(sbi, i); 1552 1553 for (j = 0; j < blkoff; j++) { 1554 if (!page) { 1555 page = grab_meta_page(sbi, blkaddr++); 1556 kaddr = (unsigned char *)page_address(page); 1557 written_size = 0; 1558 } 1559 summary = (struct f2fs_summary *)(kaddr + written_size); 1560 *summary = seg_i->sum_blk->entries[j]; 1561 written_size += SUMMARY_SIZE; 1562 1563 if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE - 1564 SUM_FOOTER_SIZE) 1565 continue; 1566 1567 set_page_dirty(page); 1568 f2fs_put_page(page, 1); 1569 page = NULL; 1570 } 1571 } 1572 if (page) { 1573 set_page_dirty(page); 1574 f2fs_put_page(page, 1); 1575 } 1576 } 1577 1578 static void write_normal_summaries(struct f2fs_sb_info *sbi, 1579 block_t blkaddr, int type) 1580 { 1581 int i, end; 1582 if (IS_DATASEG(type)) 1583 end = type + NR_CURSEG_DATA_TYPE; 1584 else 1585 end = type + NR_CURSEG_NODE_TYPE; 1586 1587 for (i = type; i < end; i++) { 1588 struct curseg_info *sum = CURSEG_I(sbi, i); 1589 mutex_lock(&sum->curseg_mutex); 1590 write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type)); 1591 mutex_unlock(&sum->curseg_mutex); 1592 } 1593 } 1594 1595 void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 1596 { 1597 if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) 1598 write_compacted_summaries(sbi, start_blk); 1599 else 1600 write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA); 1601 } 1602 1603 void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk) 1604 { 1605 write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE); 1606 } 1607 1608 int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type, 1609 unsigned int val, int alloc) 1610 { 1611 int i; 1612 1613 if (type == NAT_JOURNAL) { 1614 for (i = 0; i < nats_in_cursum(sum); i++) { 1615 if (le32_to_cpu(nid_in_journal(sum, i)) == val) 1616 return i; 1617 } 1618 if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES) 1619 return update_nats_in_cursum(sum, 1); 1620 } else if (type == SIT_JOURNAL) { 1621 for (i = 0; i < sits_in_cursum(sum); i++) 1622 if (le32_to_cpu(segno_in_journal(sum, i)) == val) 1623 return i; 1624 if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES) 1625 return update_sits_in_cursum(sum, 1); 1626 } 1627 return -1; 1628 } 1629 1630 static struct page *get_current_sit_page(struct f2fs_sb_info *sbi, 1631 unsigned int segno) 1632 { 1633 return get_meta_page(sbi, current_sit_addr(sbi, segno)); 1634 } 1635 1636 static struct page *get_next_sit_page(struct f2fs_sb_info *sbi, 1637 unsigned int start) 1638 { 1639 struct sit_info *sit_i = SIT_I(sbi); 1640 struct page *src_page, *dst_page; 1641 pgoff_t src_off, dst_off; 1642 void *src_addr, *dst_addr; 1643 1644 src_off = current_sit_addr(sbi, start); 1645 dst_off = next_sit_addr(sbi, src_off); 1646 1647 /* get current sit block page without lock */ 1648 src_page = get_meta_page(sbi, src_off); 1649 dst_page = grab_meta_page(sbi, dst_off); 1650 f2fs_bug_on(sbi, PageDirty(src_page)); 1651 1652 src_addr = page_address(src_page); 1653 dst_addr = page_address(dst_page); 1654 memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE); 1655 1656 set_page_dirty(dst_page); 1657 f2fs_put_page(src_page, 1); 1658 1659 set_to_next_sit(sit_i, start); 1660 1661 return dst_page; 1662 } 1663 1664 static struct sit_entry_set *grab_sit_entry_set(void) 1665 { 1666 struct sit_entry_set *ses = 1667 f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_ATOMIC); 1668 1669 ses->entry_cnt = 0; 1670 INIT_LIST_HEAD(&ses->set_list); 1671 return ses; 1672 } 1673 1674 static void release_sit_entry_set(struct sit_entry_set *ses) 1675 { 1676 list_del(&ses->set_list); 1677 kmem_cache_free(sit_entry_set_slab, ses); 1678 } 1679 1680 static void adjust_sit_entry_set(struct sit_entry_set *ses, 1681 struct list_head *head) 1682 { 1683 struct sit_entry_set *next = ses; 1684 1685 if (list_is_last(&ses->set_list, head)) 1686 return; 1687 1688 list_for_each_entry_continue(next, head, set_list) 1689 if (ses->entry_cnt <= next->entry_cnt) 1690 break; 1691 1692 list_move_tail(&ses->set_list, &next->set_list); 1693 } 1694 1695 static void add_sit_entry(unsigned int segno, struct list_head *head) 1696 { 1697 struct sit_entry_set *ses; 1698 unsigned int start_segno = START_SEGNO(segno); 1699 1700 list_for_each_entry(ses, head, set_list) { 1701 if (ses->start_segno == start_segno) { 1702 ses->entry_cnt++; 1703 adjust_sit_entry_set(ses, head); 1704 return; 1705 } 1706 } 1707 1708 ses = grab_sit_entry_set(); 1709 1710 ses->start_segno = start_segno; 1711 ses->entry_cnt++; 1712 list_add(&ses->set_list, head); 1713 } 1714 1715 static void add_sits_in_set(struct f2fs_sb_info *sbi) 1716 { 1717 struct f2fs_sm_info *sm_info = SM_I(sbi); 1718 struct list_head *set_list = &sm_info->sit_entry_set; 1719 unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap; 1720 unsigned int segno; 1721 1722 for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi)) 1723 add_sit_entry(segno, set_list); 1724 } 1725 1726 static void remove_sits_in_journal(struct f2fs_sb_info *sbi) 1727 { 1728 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1729 struct f2fs_summary_block *sum = curseg->sum_blk; 1730 int i; 1731 1732 for (i = sits_in_cursum(sum) - 1; i >= 0; i--) { 1733 unsigned int segno; 1734 bool dirtied; 1735 1736 segno = le32_to_cpu(segno_in_journal(sum, i)); 1737 dirtied = __mark_sit_entry_dirty(sbi, segno); 1738 1739 if (!dirtied) 1740 add_sit_entry(segno, &SM_I(sbi)->sit_entry_set); 1741 } 1742 update_sits_in_cursum(sum, -sits_in_cursum(sum)); 1743 } 1744 1745 /* 1746 * CP calls this function, which flushes SIT entries including sit_journal, 1747 * and moves prefree segs to free segs. 1748 */ 1749 void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc) 1750 { 1751 struct sit_info *sit_i = SIT_I(sbi); 1752 unsigned long *bitmap = sit_i->dirty_sentries_bitmap; 1753 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1754 struct f2fs_summary_block *sum = curseg->sum_blk; 1755 struct sit_entry_set *ses, *tmp; 1756 struct list_head *head = &SM_I(sbi)->sit_entry_set; 1757 bool to_journal = true; 1758 struct seg_entry *se; 1759 1760 mutex_lock(&curseg->curseg_mutex); 1761 mutex_lock(&sit_i->sentry_lock); 1762 1763 if (!sit_i->dirty_sentries) 1764 goto out; 1765 1766 /* 1767 * add and account sit entries of dirty bitmap in sit entry 1768 * set temporarily 1769 */ 1770 add_sits_in_set(sbi); 1771 1772 /* 1773 * if there are no enough space in journal to store dirty sit 1774 * entries, remove all entries from journal and add and account 1775 * them in sit entry set. 1776 */ 1777 if (!__has_cursum_space(sum, sit_i->dirty_sentries, SIT_JOURNAL)) 1778 remove_sits_in_journal(sbi); 1779 1780 /* 1781 * there are two steps to flush sit entries: 1782 * #1, flush sit entries to journal in current cold data summary block. 1783 * #2, flush sit entries to sit page. 1784 */ 1785 list_for_each_entry_safe(ses, tmp, head, set_list) { 1786 struct page *page = NULL; 1787 struct f2fs_sit_block *raw_sit = NULL; 1788 unsigned int start_segno = ses->start_segno; 1789 unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK, 1790 (unsigned long)MAIN_SEGS(sbi)); 1791 unsigned int segno = start_segno; 1792 1793 if (to_journal && 1794 !__has_cursum_space(sum, ses->entry_cnt, SIT_JOURNAL)) 1795 to_journal = false; 1796 1797 if (!to_journal) { 1798 page = get_next_sit_page(sbi, start_segno); 1799 raw_sit = page_address(page); 1800 } 1801 1802 /* flush dirty sit entries in region of current sit set */ 1803 for_each_set_bit_from(segno, bitmap, end) { 1804 int offset, sit_offset; 1805 1806 se = get_seg_entry(sbi, segno); 1807 1808 /* add discard candidates */ 1809 if (cpc->reason != CP_DISCARD) { 1810 cpc->trim_start = segno; 1811 add_discard_addrs(sbi, cpc); 1812 } 1813 1814 if (to_journal) { 1815 offset = lookup_journal_in_cursum(sum, 1816 SIT_JOURNAL, segno, 1); 1817 f2fs_bug_on(sbi, offset < 0); 1818 segno_in_journal(sum, offset) = 1819 cpu_to_le32(segno); 1820 seg_info_to_raw_sit(se, 1821 &sit_in_journal(sum, offset)); 1822 } else { 1823 sit_offset = SIT_ENTRY_OFFSET(sit_i, segno); 1824 seg_info_to_raw_sit(se, 1825 &raw_sit->entries[sit_offset]); 1826 } 1827 1828 __clear_bit(segno, bitmap); 1829 sit_i->dirty_sentries--; 1830 ses->entry_cnt--; 1831 } 1832 1833 if (!to_journal) 1834 f2fs_put_page(page, 1); 1835 1836 f2fs_bug_on(sbi, ses->entry_cnt); 1837 release_sit_entry_set(ses); 1838 } 1839 1840 f2fs_bug_on(sbi, !list_empty(head)); 1841 f2fs_bug_on(sbi, sit_i->dirty_sentries); 1842 out: 1843 if (cpc->reason == CP_DISCARD) { 1844 for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) 1845 add_discard_addrs(sbi, cpc); 1846 } 1847 mutex_unlock(&sit_i->sentry_lock); 1848 mutex_unlock(&curseg->curseg_mutex); 1849 1850 set_prefree_as_free_segments(sbi); 1851 } 1852 1853 static int build_sit_info(struct f2fs_sb_info *sbi) 1854 { 1855 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 1856 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 1857 struct sit_info *sit_i; 1858 unsigned int sit_segs, start; 1859 char *src_bitmap, *dst_bitmap; 1860 unsigned int bitmap_size; 1861 1862 /* allocate memory for SIT information */ 1863 sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL); 1864 if (!sit_i) 1865 return -ENOMEM; 1866 1867 SM_I(sbi)->sit_info = sit_i; 1868 1869 sit_i->sentries = vzalloc(MAIN_SEGS(sbi) * sizeof(struct seg_entry)); 1870 if (!sit_i->sentries) 1871 return -ENOMEM; 1872 1873 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 1874 sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL); 1875 if (!sit_i->dirty_sentries_bitmap) 1876 return -ENOMEM; 1877 1878 for (start = 0; start < MAIN_SEGS(sbi); start++) { 1879 sit_i->sentries[start].cur_valid_map 1880 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 1881 sit_i->sentries[start].ckpt_valid_map 1882 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 1883 sit_i->sentries[start].discard_map 1884 = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 1885 if (!sit_i->sentries[start].cur_valid_map || 1886 !sit_i->sentries[start].ckpt_valid_map || 1887 !sit_i->sentries[start].discard_map) 1888 return -ENOMEM; 1889 } 1890 1891 sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL); 1892 if (!sit_i->tmp_map) 1893 return -ENOMEM; 1894 1895 if (sbi->segs_per_sec > 1) { 1896 sit_i->sec_entries = vzalloc(MAIN_SECS(sbi) * 1897 sizeof(struct sec_entry)); 1898 if (!sit_i->sec_entries) 1899 return -ENOMEM; 1900 } 1901 1902 /* get information related with SIT */ 1903 sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1; 1904 1905 /* setup SIT bitmap from ckeckpoint pack */ 1906 bitmap_size = __bitmap_size(sbi, SIT_BITMAP); 1907 src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP); 1908 1909 dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL); 1910 if (!dst_bitmap) 1911 return -ENOMEM; 1912 1913 /* init SIT information */ 1914 sit_i->s_ops = &default_salloc_ops; 1915 1916 sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr); 1917 sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg; 1918 sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count); 1919 sit_i->sit_bitmap = dst_bitmap; 1920 sit_i->bitmap_size = bitmap_size; 1921 sit_i->dirty_sentries = 0; 1922 sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK; 1923 sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time); 1924 sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec; 1925 mutex_init(&sit_i->sentry_lock); 1926 return 0; 1927 } 1928 1929 static int build_free_segmap(struct f2fs_sb_info *sbi) 1930 { 1931 struct free_segmap_info *free_i; 1932 unsigned int bitmap_size, sec_bitmap_size; 1933 1934 /* allocate memory for free segmap information */ 1935 free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL); 1936 if (!free_i) 1937 return -ENOMEM; 1938 1939 SM_I(sbi)->free_info = free_i; 1940 1941 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 1942 free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL); 1943 if (!free_i->free_segmap) 1944 return -ENOMEM; 1945 1946 sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 1947 free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL); 1948 if (!free_i->free_secmap) 1949 return -ENOMEM; 1950 1951 /* set all segments as dirty temporarily */ 1952 memset(free_i->free_segmap, 0xff, bitmap_size); 1953 memset(free_i->free_secmap, 0xff, sec_bitmap_size); 1954 1955 /* init free segmap information */ 1956 free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi)); 1957 free_i->free_segments = 0; 1958 free_i->free_sections = 0; 1959 spin_lock_init(&free_i->segmap_lock); 1960 return 0; 1961 } 1962 1963 static int build_curseg(struct f2fs_sb_info *sbi) 1964 { 1965 struct curseg_info *array; 1966 int i; 1967 1968 array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL); 1969 if (!array) 1970 return -ENOMEM; 1971 1972 SM_I(sbi)->curseg_array = array; 1973 1974 for (i = 0; i < NR_CURSEG_TYPE; i++) { 1975 mutex_init(&array[i].curseg_mutex); 1976 array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL); 1977 if (!array[i].sum_blk) 1978 return -ENOMEM; 1979 array[i].segno = NULL_SEGNO; 1980 array[i].next_blkoff = 0; 1981 } 1982 return restore_curseg_summaries(sbi); 1983 } 1984 1985 static void build_sit_entries(struct f2fs_sb_info *sbi) 1986 { 1987 struct sit_info *sit_i = SIT_I(sbi); 1988 struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA); 1989 struct f2fs_summary_block *sum = curseg->sum_blk; 1990 int sit_blk_cnt = SIT_BLK_CNT(sbi); 1991 unsigned int i, start, end; 1992 unsigned int readed, start_blk = 0; 1993 int nrpages = MAX_BIO_BLOCKS(sbi); 1994 1995 do { 1996 readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT); 1997 1998 start = start_blk * sit_i->sents_per_block; 1999 end = (start_blk + readed) * sit_i->sents_per_block; 2000 2001 for (; start < end && start < MAIN_SEGS(sbi); start++) { 2002 struct seg_entry *se = &sit_i->sentries[start]; 2003 struct f2fs_sit_block *sit_blk; 2004 struct f2fs_sit_entry sit; 2005 struct page *page; 2006 2007 mutex_lock(&curseg->curseg_mutex); 2008 for (i = 0; i < sits_in_cursum(sum); i++) { 2009 if (le32_to_cpu(segno_in_journal(sum, i)) 2010 == start) { 2011 sit = sit_in_journal(sum, i); 2012 mutex_unlock(&curseg->curseg_mutex); 2013 goto got_it; 2014 } 2015 } 2016 mutex_unlock(&curseg->curseg_mutex); 2017 2018 page = get_current_sit_page(sbi, start); 2019 sit_blk = (struct f2fs_sit_block *)page_address(page); 2020 sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)]; 2021 f2fs_put_page(page, 1); 2022 got_it: 2023 check_block_count(sbi, start, &sit); 2024 seg_info_from_raw_sit(se, &sit); 2025 2026 /* build discard map only one time */ 2027 memcpy(se->discard_map, se->cur_valid_map, SIT_VBLOCK_MAP_SIZE); 2028 sbi->discard_blks += sbi->blocks_per_seg - se->valid_blocks; 2029 2030 if (sbi->segs_per_sec > 1) { 2031 struct sec_entry *e = get_sec_entry(sbi, start); 2032 e->valid_blocks += se->valid_blocks; 2033 } 2034 } 2035 start_blk += readed; 2036 } while (start_blk < sit_blk_cnt); 2037 } 2038 2039 static void init_free_segmap(struct f2fs_sb_info *sbi) 2040 { 2041 unsigned int start; 2042 int type; 2043 2044 for (start = 0; start < MAIN_SEGS(sbi); start++) { 2045 struct seg_entry *sentry = get_seg_entry(sbi, start); 2046 if (!sentry->valid_blocks) 2047 __set_free(sbi, start); 2048 } 2049 2050 /* set use the current segments */ 2051 for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) { 2052 struct curseg_info *curseg_t = CURSEG_I(sbi, type); 2053 __set_test_and_inuse(sbi, curseg_t->segno); 2054 } 2055 } 2056 2057 static void init_dirty_segmap(struct f2fs_sb_info *sbi) 2058 { 2059 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2060 struct free_segmap_info *free_i = FREE_I(sbi); 2061 unsigned int segno = 0, offset = 0; 2062 unsigned short valid_blocks; 2063 2064 while (1) { 2065 /* find dirty segment based on free segmap */ 2066 segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset); 2067 if (segno >= MAIN_SEGS(sbi)) 2068 break; 2069 offset = segno + 1; 2070 valid_blocks = get_valid_blocks(sbi, segno, 0); 2071 if (valid_blocks == sbi->blocks_per_seg || !valid_blocks) 2072 continue; 2073 if (valid_blocks > sbi->blocks_per_seg) { 2074 f2fs_bug_on(sbi, 1); 2075 continue; 2076 } 2077 mutex_lock(&dirty_i->seglist_lock); 2078 __locate_dirty_segment(sbi, segno, DIRTY); 2079 mutex_unlock(&dirty_i->seglist_lock); 2080 } 2081 } 2082 2083 static int init_victim_secmap(struct f2fs_sb_info *sbi) 2084 { 2085 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2086 unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi)); 2087 2088 dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL); 2089 if (!dirty_i->victim_secmap) 2090 return -ENOMEM; 2091 return 0; 2092 } 2093 2094 static int build_dirty_segmap(struct f2fs_sb_info *sbi) 2095 { 2096 struct dirty_seglist_info *dirty_i; 2097 unsigned int bitmap_size, i; 2098 2099 /* allocate memory for dirty segments list information */ 2100 dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL); 2101 if (!dirty_i) 2102 return -ENOMEM; 2103 2104 SM_I(sbi)->dirty_info = dirty_i; 2105 mutex_init(&dirty_i->seglist_lock); 2106 2107 bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi)); 2108 2109 for (i = 0; i < NR_DIRTY_TYPE; i++) { 2110 dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL); 2111 if (!dirty_i->dirty_segmap[i]) 2112 return -ENOMEM; 2113 } 2114 2115 init_dirty_segmap(sbi); 2116 return init_victim_secmap(sbi); 2117 } 2118 2119 /* 2120 * Update min, max modified time for cost-benefit GC algorithm 2121 */ 2122 static void init_min_max_mtime(struct f2fs_sb_info *sbi) 2123 { 2124 struct sit_info *sit_i = SIT_I(sbi); 2125 unsigned int segno; 2126 2127 mutex_lock(&sit_i->sentry_lock); 2128 2129 sit_i->min_mtime = LLONG_MAX; 2130 2131 for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) { 2132 unsigned int i; 2133 unsigned long long mtime = 0; 2134 2135 for (i = 0; i < sbi->segs_per_sec; i++) 2136 mtime += get_seg_entry(sbi, segno + i)->mtime; 2137 2138 mtime = div_u64(mtime, sbi->segs_per_sec); 2139 2140 if (sit_i->min_mtime > mtime) 2141 sit_i->min_mtime = mtime; 2142 } 2143 sit_i->max_mtime = get_mtime(sbi); 2144 mutex_unlock(&sit_i->sentry_lock); 2145 } 2146 2147 int build_segment_manager(struct f2fs_sb_info *sbi) 2148 { 2149 struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi); 2150 struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi); 2151 struct f2fs_sm_info *sm_info; 2152 int err; 2153 2154 sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL); 2155 if (!sm_info) 2156 return -ENOMEM; 2157 2158 /* init sm info */ 2159 sbi->sm_info = sm_info; 2160 sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr); 2161 sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr); 2162 sm_info->segment_count = le32_to_cpu(raw_super->segment_count); 2163 sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count); 2164 sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count); 2165 sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main); 2166 sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr); 2167 sm_info->rec_prefree_segments = sm_info->main_segments * 2168 DEF_RECLAIM_PREFREE_SEGMENTS / 100; 2169 sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC; 2170 sm_info->min_ipu_util = DEF_MIN_IPU_UTIL; 2171 sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS; 2172 2173 INIT_LIST_HEAD(&sm_info->discard_list); 2174 sm_info->nr_discards = 0; 2175 sm_info->max_discards = 0; 2176 2177 sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS; 2178 2179 INIT_LIST_HEAD(&sm_info->sit_entry_set); 2180 2181 if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) { 2182 err = create_flush_cmd_control(sbi); 2183 if (err) 2184 return err; 2185 } 2186 2187 err = build_sit_info(sbi); 2188 if (err) 2189 return err; 2190 err = build_free_segmap(sbi); 2191 if (err) 2192 return err; 2193 err = build_curseg(sbi); 2194 if (err) 2195 return err; 2196 2197 /* reinit free segmap based on SIT */ 2198 build_sit_entries(sbi); 2199 2200 init_free_segmap(sbi); 2201 err = build_dirty_segmap(sbi); 2202 if (err) 2203 return err; 2204 2205 init_min_max_mtime(sbi); 2206 return 0; 2207 } 2208 2209 static void discard_dirty_segmap(struct f2fs_sb_info *sbi, 2210 enum dirty_type dirty_type) 2211 { 2212 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2213 2214 mutex_lock(&dirty_i->seglist_lock); 2215 kfree(dirty_i->dirty_segmap[dirty_type]); 2216 dirty_i->nr_dirty[dirty_type] = 0; 2217 mutex_unlock(&dirty_i->seglist_lock); 2218 } 2219 2220 static void destroy_victim_secmap(struct f2fs_sb_info *sbi) 2221 { 2222 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2223 kfree(dirty_i->victim_secmap); 2224 } 2225 2226 static void destroy_dirty_segmap(struct f2fs_sb_info *sbi) 2227 { 2228 struct dirty_seglist_info *dirty_i = DIRTY_I(sbi); 2229 int i; 2230 2231 if (!dirty_i) 2232 return; 2233 2234 /* discard pre-free/dirty segments list */ 2235 for (i = 0; i < NR_DIRTY_TYPE; i++) 2236 discard_dirty_segmap(sbi, i); 2237 2238 destroy_victim_secmap(sbi); 2239 SM_I(sbi)->dirty_info = NULL; 2240 kfree(dirty_i); 2241 } 2242 2243 static void destroy_curseg(struct f2fs_sb_info *sbi) 2244 { 2245 struct curseg_info *array = SM_I(sbi)->curseg_array; 2246 int i; 2247 2248 if (!array) 2249 return; 2250 SM_I(sbi)->curseg_array = NULL; 2251 for (i = 0; i < NR_CURSEG_TYPE; i++) 2252 kfree(array[i].sum_blk); 2253 kfree(array); 2254 } 2255 2256 static void destroy_free_segmap(struct f2fs_sb_info *sbi) 2257 { 2258 struct free_segmap_info *free_i = SM_I(sbi)->free_info; 2259 if (!free_i) 2260 return; 2261 SM_I(sbi)->free_info = NULL; 2262 kfree(free_i->free_segmap); 2263 kfree(free_i->free_secmap); 2264 kfree(free_i); 2265 } 2266 2267 static void destroy_sit_info(struct f2fs_sb_info *sbi) 2268 { 2269 struct sit_info *sit_i = SIT_I(sbi); 2270 unsigned int start; 2271 2272 if (!sit_i) 2273 return; 2274 2275 if (sit_i->sentries) { 2276 for (start = 0; start < MAIN_SEGS(sbi); start++) { 2277 kfree(sit_i->sentries[start].cur_valid_map); 2278 kfree(sit_i->sentries[start].ckpt_valid_map); 2279 kfree(sit_i->sentries[start].discard_map); 2280 } 2281 } 2282 kfree(sit_i->tmp_map); 2283 2284 vfree(sit_i->sentries); 2285 vfree(sit_i->sec_entries); 2286 kfree(sit_i->dirty_sentries_bitmap); 2287 2288 SM_I(sbi)->sit_info = NULL; 2289 kfree(sit_i->sit_bitmap); 2290 kfree(sit_i); 2291 } 2292 2293 void destroy_segment_manager(struct f2fs_sb_info *sbi) 2294 { 2295 struct f2fs_sm_info *sm_info = SM_I(sbi); 2296 2297 if (!sm_info) 2298 return; 2299 destroy_flush_cmd_control(sbi); 2300 destroy_dirty_segmap(sbi); 2301 destroy_curseg(sbi); 2302 destroy_free_segmap(sbi); 2303 destroy_sit_info(sbi); 2304 sbi->sm_info = NULL; 2305 kfree(sm_info); 2306 } 2307 2308 int __init create_segment_manager_caches(void) 2309 { 2310 discard_entry_slab = f2fs_kmem_cache_create("discard_entry", 2311 sizeof(struct discard_entry)); 2312 if (!discard_entry_slab) 2313 goto fail; 2314 2315 sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set", 2316 sizeof(struct sit_entry_set)); 2317 if (!sit_entry_set_slab) 2318 goto destory_discard_entry; 2319 2320 inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry", 2321 sizeof(struct inmem_pages)); 2322 if (!inmem_entry_slab) 2323 goto destroy_sit_entry_set; 2324 return 0; 2325 2326 destroy_sit_entry_set: 2327 kmem_cache_destroy(sit_entry_set_slab); 2328 destory_discard_entry: 2329 kmem_cache_destroy(discard_entry_slab); 2330 fail: 2331 return -ENOMEM; 2332 } 2333 2334 void destroy_segment_manager_caches(void) 2335 { 2336 kmem_cache_destroy(sit_entry_set_slab); 2337 kmem_cache_destroy(discard_entry_slab); 2338 kmem_cache_destroy(inmem_entry_slab); 2339 } 2340