1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * f2fs extent cache support 4 * 5 * Copyright (c) 2015 Motorola Mobility 6 * Copyright (c) 2015 Samsung Electronics 7 * Authors: Jaegeuk Kim <jaegeuk@kernel.org> 8 * Chao Yu <chao2.yu@samsung.com> 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/f2fs_fs.h> 13 14 #include "f2fs.h" 15 #include "node.h" 16 #include <trace/events/f2fs.h> 17 18 static struct rb_entry *__lookup_rb_tree_fast(struct rb_entry *cached_re, 19 unsigned int ofs) 20 { 21 if (cached_re) { 22 if (cached_re->ofs <= ofs && 23 cached_re->ofs + cached_re->len > ofs) { 24 return cached_re; 25 } 26 } 27 return NULL; 28 } 29 30 static struct rb_entry *__lookup_rb_tree_slow(struct rb_root_cached *root, 31 unsigned int ofs) 32 { 33 struct rb_node *node = root->rb_root.rb_node; 34 struct rb_entry *re; 35 36 while (node) { 37 re = rb_entry(node, struct rb_entry, rb_node); 38 39 if (ofs < re->ofs) 40 node = node->rb_left; 41 else if (ofs >= re->ofs + re->len) 42 node = node->rb_right; 43 else 44 return re; 45 } 46 return NULL; 47 } 48 49 struct rb_entry *f2fs_lookup_rb_tree(struct rb_root_cached *root, 50 struct rb_entry *cached_re, unsigned int ofs) 51 { 52 struct rb_entry *re; 53 54 re = __lookup_rb_tree_fast(cached_re, ofs); 55 if (!re) 56 return __lookup_rb_tree_slow(root, ofs); 57 58 return re; 59 } 60 61 struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi, 62 struct rb_root_cached *root, 63 struct rb_node **parent, 64 unsigned int ofs, bool *leftmost) 65 { 66 struct rb_node **p = &root->rb_root.rb_node; 67 struct rb_entry *re; 68 69 while (*p) { 70 *parent = *p; 71 re = rb_entry(*parent, struct rb_entry, rb_node); 72 73 if (ofs < re->ofs) { 74 p = &(*p)->rb_left; 75 } else if (ofs >= re->ofs + re->len) { 76 p = &(*p)->rb_right; 77 *leftmost = false; 78 } else { 79 f2fs_bug_on(sbi, 1); 80 } 81 } 82 83 return p; 84 } 85 86 /* 87 * lookup rb entry in position of @ofs in rb-tree, 88 * if hit, return the entry, otherwise, return NULL 89 * @prev_ex: extent before ofs 90 * @next_ex: extent after ofs 91 * @insert_p: insert point for new extent at ofs 92 * in order to simpfy the insertion after. 93 * tree must stay unchanged between lookup and insertion. 94 */ 95 struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root_cached *root, 96 struct rb_entry *cached_re, 97 unsigned int ofs, 98 struct rb_entry **prev_entry, 99 struct rb_entry **next_entry, 100 struct rb_node ***insert_p, 101 struct rb_node **insert_parent, 102 bool force, bool *leftmost) 103 { 104 struct rb_node **pnode = &root->rb_root.rb_node; 105 struct rb_node *parent = NULL, *tmp_node; 106 struct rb_entry *re = cached_re; 107 108 *insert_p = NULL; 109 *insert_parent = NULL; 110 *prev_entry = NULL; 111 *next_entry = NULL; 112 113 if (RB_EMPTY_ROOT(&root->rb_root)) 114 return NULL; 115 116 if (re) { 117 if (re->ofs <= ofs && re->ofs + re->len > ofs) 118 goto lookup_neighbors; 119 } 120 121 if (leftmost) 122 *leftmost = true; 123 124 while (*pnode) { 125 parent = *pnode; 126 re = rb_entry(*pnode, struct rb_entry, rb_node); 127 128 if (ofs < re->ofs) { 129 pnode = &(*pnode)->rb_left; 130 } else if (ofs >= re->ofs + re->len) { 131 pnode = &(*pnode)->rb_right; 132 if (leftmost) 133 *leftmost = false; 134 } else { 135 goto lookup_neighbors; 136 } 137 } 138 139 *insert_p = pnode; 140 *insert_parent = parent; 141 142 re = rb_entry(parent, struct rb_entry, rb_node); 143 tmp_node = parent; 144 if (parent && ofs > re->ofs) 145 tmp_node = rb_next(parent); 146 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); 147 148 tmp_node = parent; 149 if (parent && ofs < re->ofs) 150 tmp_node = rb_prev(parent); 151 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); 152 return NULL; 153 154 lookup_neighbors: 155 if (ofs == re->ofs || force) { 156 /* lookup prev node for merging backward later */ 157 tmp_node = rb_prev(&re->rb_node); 158 *prev_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); 159 } 160 if (ofs == re->ofs + re->len - 1 || force) { 161 /* lookup next node for merging frontward later */ 162 tmp_node = rb_next(&re->rb_node); 163 *next_entry = rb_entry_safe(tmp_node, struct rb_entry, rb_node); 164 } 165 return re; 166 } 167 168 bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi, 169 struct rb_root_cached *root) 170 { 171 #ifdef CONFIG_F2FS_CHECK_FS 172 struct rb_node *cur = rb_first_cached(root), *next; 173 struct rb_entry *cur_re, *next_re; 174 175 if (!cur) 176 return true; 177 178 while (cur) { 179 next = rb_next(cur); 180 if (!next) 181 return true; 182 183 cur_re = rb_entry(cur, struct rb_entry, rb_node); 184 next_re = rb_entry(next, struct rb_entry, rb_node); 185 186 if (cur_re->ofs + cur_re->len > next_re->ofs) { 187 f2fs_msg(sbi->sb, KERN_INFO, "inconsistent rbtree, " 188 "cur(%u, %u) next(%u, %u)", 189 cur_re->ofs, cur_re->len, 190 next_re->ofs, next_re->len); 191 return false; 192 } 193 194 cur = next; 195 } 196 #endif 197 return true; 198 } 199 200 static struct kmem_cache *extent_tree_slab; 201 static struct kmem_cache *extent_node_slab; 202 203 static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi, 204 struct extent_tree *et, struct extent_info *ei, 205 struct rb_node *parent, struct rb_node **p, 206 bool leftmost) 207 { 208 struct extent_node *en; 209 210 en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC); 211 if (!en) 212 return NULL; 213 214 en->ei = *ei; 215 INIT_LIST_HEAD(&en->list); 216 en->et = et; 217 218 rb_link_node(&en->rb_node, parent, p); 219 rb_insert_color_cached(&en->rb_node, &et->root, leftmost); 220 atomic_inc(&et->node_cnt); 221 atomic_inc(&sbi->total_ext_node); 222 return en; 223 } 224 225 static void __detach_extent_node(struct f2fs_sb_info *sbi, 226 struct extent_tree *et, struct extent_node *en) 227 { 228 rb_erase_cached(&en->rb_node, &et->root); 229 atomic_dec(&et->node_cnt); 230 atomic_dec(&sbi->total_ext_node); 231 232 if (et->cached_en == en) 233 et->cached_en = NULL; 234 kmem_cache_free(extent_node_slab, en); 235 } 236 237 /* 238 * Flow to release an extent_node: 239 * 1. list_del_init 240 * 2. __detach_extent_node 241 * 3. kmem_cache_free. 242 */ 243 static void __release_extent_node(struct f2fs_sb_info *sbi, 244 struct extent_tree *et, struct extent_node *en) 245 { 246 spin_lock(&sbi->extent_lock); 247 f2fs_bug_on(sbi, list_empty(&en->list)); 248 list_del_init(&en->list); 249 spin_unlock(&sbi->extent_lock); 250 251 __detach_extent_node(sbi, et, en); 252 } 253 254 static struct extent_tree *__grab_extent_tree(struct inode *inode) 255 { 256 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 257 struct extent_tree *et; 258 nid_t ino = inode->i_ino; 259 260 mutex_lock(&sbi->extent_tree_lock); 261 et = radix_tree_lookup(&sbi->extent_tree_root, ino); 262 if (!et) { 263 et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS); 264 f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et); 265 memset(et, 0, sizeof(struct extent_tree)); 266 et->ino = ino; 267 et->root = RB_ROOT_CACHED; 268 et->cached_en = NULL; 269 rwlock_init(&et->lock); 270 INIT_LIST_HEAD(&et->list); 271 atomic_set(&et->node_cnt, 0); 272 atomic_inc(&sbi->total_ext_tree); 273 } else { 274 atomic_dec(&sbi->total_zombie_tree); 275 list_del_init(&et->list); 276 } 277 mutex_unlock(&sbi->extent_tree_lock); 278 279 /* never died until evict_inode */ 280 F2FS_I(inode)->extent_tree = et; 281 282 return et; 283 } 284 285 static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi, 286 struct extent_tree *et, struct extent_info *ei) 287 { 288 struct rb_node **p = &et->root.rb_root.rb_node; 289 struct extent_node *en; 290 291 en = __attach_extent_node(sbi, et, ei, NULL, p, true); 292 if (!en) 293 return NULL; 294 295 et->largest = en->ei; 296 et->cached_en = en; 297 return en; 298 } 299 300 static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi, 301 struct extent_tree *et) 302 { 303 struct rb_node *node, *next; 304 struct extent_node *en; 305 unsigned int count = atomic_read(&et->node_cnt); 306 307 node = rb_first_cached(&et->root); 308 while (node) { 309 next = rb_next(node); 310 en = rb_entry(node, struct extent_node, rb_node); 311 __release_extent_node(sbi, et, en); 312 node = next; 313 } 314 315 return count - atomic_read(&et->node_cnt); 316 } 317 318 static void __drop_largest_extent(struct extent_tree *et, 319 pgoff_t fofs, unsigned int len) 320 { 321 if (fofs < et->largest.fofs + et->largest.len && 322 fofs + len > et->largest.fofs) { 323 et->largest.len = 0; 324 et->largest_updated = true; 325 } 326 } 327 328 /* return true, if inode page is changed */ 329 static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext) 330 { 331 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 332 struct extent_tree *et; 333 struct extent_node *en; 334 struct extent_info ei; 335 336 if (!f2fs_may_extent_tree(inode)) { 337 /* drop largest extent */ 338 if (i_ext && i_ext->len) { 339 i_ext->len = 0; 340 return true; 341 } 342 return false; 343 } 344 345 et = __grab_extent_tree(inode); 346 347 if (!i_ext || !i_ext->len) 348 return false; 349 350 get_extent_info(&ei, i_ext); 351 352 write_lock(&et->lock); 353 if (atomic_read(&et->node_cnt)) 354 goto out; 355 356 en = __init_extent_tree(sbi, et, &ei); 357 if (en) { 358 spin_lock(&sbi->extent_lock); 359 list_add_tail(&en->list, &sbi->extent_list); 360 spin_unlock(&sbi->extent_lock); 361 } 362 out: 363 write_unlock(&et->lock); 364 return false; 365 } 366 367 bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext) 368 { 369 bool ret = __f2fs_init_extent_tree(inode, i_ext); 370 371 if (!F2FS_I(inode)->extent_tree) 372 set_inode_flag(inode, FI_NO_EXTENT); 373 374 return ret; 375 } 376 377 static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs, 378 struct extent_info *ei) 379 { 380 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 381 struct extent_tree *et = F2FS_I(inode)->extent_tree; 382 struct extent_node *en; 383 bool ret = false; 384 385 f2fs_bug_on(sbi, !et); 386 387 trace_f2fs_lookup_extent_tree_start(inode, pgofs); 388 389 read_lock(&et->lock); 390 391 if (et->largest.fofs <= pgofs && 392 et->largest.fofs + et->largest.len > pgofs) { 393 *ei = et->largest; 394 ret = true; 395 stat_inc_largest_node_hit(sbi); 396 goto out; 397 } 398 399 en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root, 400 (struct rb_entry *)et->cached_en, pgofs); 401 if (!en) 402 goto out; 403 404 if (en == et->cached_en) 405 stat_inc_cached_node_hit(sbi); 406 else 407 stat_inc_rbtree_node_hit(sbi); 408 409 *ei = en->ei; 410 spin_lock(&sbi->extent_lock); 411 if (!list_empty(&en->list)) { 412 list_move_tail(&en->list, &sbi->extent_list); 413 et->cached_en = en; 414 } 415 spin_unlock(&sbi->extent_lock); 416 ret = true; 417 out: 418 stat_inc_total_hit(sbi); 419 read_unlock(&et->lock); 420 421 trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei); 422 return ret; 423 } 424 425 static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi, 426 struct extent_tree *et, struct extent_info *ei, 427 struct extent_node *prev_ex, 428 struct extent_node *next_ex) 429 { 430 struct extent_node *en = NULL; 431 432 if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) { 433 prev_ex->ei.len += ei->len; 434 ei = &prev_ex->ei; 435 en = prev_ex; 436 } 437 438 if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) { 439 next_ex->ei.fofs = ei->fofs; 440 next_ex->ei.blk = ei->blk; 441 next_ex->ei.len += ei->len; 442 if (en) 443 __release_extent_node(sbi, et, prev_ex); 444 445 en = next_ex; 446 } 447 448 if (!en) 449 return NULL; 450 451 __try_update_largest_extent(et, en); 452 453 spin_lock(&sbi->extent_lock); 454 if (!list_empty(&en->list)) { 455 list_move_tail(&en->list, &sbi->extent_list); 456 et->cached_en = en; 457 } 458 spin_unlock(&sbi->extent_lock); 459 return en; 460 } 461 462 static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi, 463 struct extent_tree *et, struct extent_info *ei, 464 struct rb_node **insert_p, 465 struct rb_node *insert_parent, 466 bool leftmost) 467 { 468 struct rb_node **p; 469 struct rb_node *parent = NULL; 470 struct extent_node *en = NULL; 471 472 if (insert_p && insert_parent) { 473 parent = insert_parent; 474 p = insert_p; 475 goto do_insert; 476 } 477 478 leftmost = true; 479 480 p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent, 481 ei->fofs, &leftmost); 482 do_insert: 483 en = __attach_extent_node(sbi, et, ei, parent, p, leftmost); 484 if (!en) 485 return NULL; 486 487 __try_update_largest_extent(et, en); 488 489 /* update in global extent list */ 490 spin_lock(&sbi->extent_lock); 491 list_add_tail(&en->list, &sbi->extent_list); 492 et->cached_en = en; 493 spin_unlock(&sbi->extent_lock); 494 return en; 495 } 496 497 static void f2fs_update_extent_tree_range(struct inode *inode, 498 pgoff_t fofs, block_t blkaddr, unsigned int len) 499 { 500 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 501 struct extent_tree *et = F2FS_I(inode)->extent_tree; 502 struct extent_node *en = NULL, *en1 = NULL; 503 struct extent_node *prev_en = NULL, *next_en = NULL; 504 struct extent_info ei, dei, prev; 505 struct rb_node **insert_p = NULL, *insert_parent = NULL; 506 unsigned int end = fofs + len; 507 unsigned int pos = (unsigned int)fofs; 508 bool updated = false; 509 bool leftmost; 510 511 if (!et) 512 return; 513 514 trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len); 515 516 write_lock(&et->lock); 517 518 if (is_inode_flag_set(inode, FI_NO_EXTENT)) { 519 write_unlock(&et->lock); 520 return; 521 } 522 523 prev = et->largest; 524 dei.len = 0; 525 526 /* 527 * drop largest extent before lookup, in case it's already 528 * been shrunk from extent tree 529 */ 530 __drop_largest_extent(et, fofs, len); 531 532 /* 1. lookup first extent node in range [fofs, fofs + len - 1] */ 533 en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root, 534 (struct rb_entry *)et->cached_en, fofs, 535 (struct rb_entry **)&prev_en, 536 (struct rb_entry **)&next_en, 537 &insert_p, &insert_parent, false, 538 &leftmost); 539 if (!en) 540 en = next_en; 541 542 /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */ 543 while (en && en->ei.fofs < end) { 544 unsigned int org_end; 545 int parts = 0; /* # of parts current extent split into */ 546 547 next_en = en1 = NULL; 548 549 dei = en->ei; 550 org_end = dei.fofs + dei.len; 551 f2fs_bug_on(sbi, pos >= org_end); 552 553 if (pos > dei.fofs && pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) { 554 en->ei.len = pos - en->ei.fofs; 555 prev_en = en; 556 parts = 1; 557 } 558 559 if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) { 560 if (parts) { 561 set_extent_info(&ei, end, 562 end - dei.fofs + dei.blk, 563 org_end - end); 564 en1 = __insert_extent_tree(sbi, et, &ei, 565 NULL, NULL, true); 566 next_en = en1; 567 } else { 568 en->ei.fofs = end; 569 en->ei.blk += end - dei.fofs; 570 en->ei.len -= end - dei.fofs; 571 next_en = en; 572 } 573 parts++; 574 } 575 576 if (!next_en) { 577 struct rb_node *node = rb_next(&en->rb_node); 578 579 next_en = rb_entry_safe(node, struct extent_node, 580 rb_node); 581 } 582 583 if (parts) 584 __try_update_largest_extent(et, en); 585 else 586 __release_extent_node(sbi, et, en); 587 588 /* 589 * if original extent is split into zero or two parts, extent 590 * tree has been altered by deletion or insertion, therefore 591 * invalidate pointers regard to tree. 592 */ 593 if (parts != 1) { 594 insert_p = NULL; 595 insert_parent = NULL; 596 } 597 en = next_en; 598 } 599 600 /* 3. update extent in extent cache */ 601 if (blkaddr) { 602 603 set_extent_info(&ei, fofs, blkaddr, len); 604 if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en)) 605 __insert_extent_tree(sbi, et, &ei, 606 insert_p, insert_parent, leftmost); 607 608 /* give up extent_cache, if split and small updates happen */ 609 if (dei.len >= 1 && 610 prev.len < F2FS_MIN_EXTENT_LEN && 611 et->largest.len < F2FS_MIN_EXTENT_LEN) { 612 et->largest.len = 0; 613 et->largest_updated = true; 614 set_inode_flag(inode, FI_NO_EXTENT); 615 } 616 } 617 618 if (is_inode_flag_set(inode, FI_NO_EXTENT)) 619 __free_extent_tree(sbi, et); 620 621 if (et->largest_updated) { 622 et->largest_updated = false; 623 updated = true; 624 } 625 626 write_unlock(&et->lock); 627 628 if (updated) 629 f2fs_mark_inode_dirty_sync(inode, true); 630 } 631 632 unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink) 633 { 634 struct extent_tree *et, *next; 635 struct extent_node *en; 636 unsigned int node_cnt = 0, tree_cnt = 0; 637 int remained; 638 639 if (!test_opt(sbi, EXTENT_CACHE)) 640 return 0; 641 642 if (!atomic_read(&sbi->total_zombie_tree)) 643 goto free_node; 644 645 if (!mutex_trylock(&sbi->extent_tree_lock)) 646 goto out; 647 648 /* 1. remove unreferenced extent tree */ 649 list_for_each_entry_safe(et, next, &sbi->zombie_list, list) { 650 if (atomic_read(&et->node_cnt)) { 651 write_lock(&et->lock); 652 node_cnt += __free_extent_tree(sbi, et); 653 write_unlock(&et->lock); 654 } 655 f2fs_bug_on(sbi, atomic_read(&et->node_cnt)); 656 list_del_init(&et->list); 657 radix_tree_delete(&sbi->extent_tree_root, et->ino); 658 kmem_cache_free(extent_tree_slab, et); 659 atomic_dec(&sbi->total_ext_tree); 660 atomic_dec(&sbi->total_zombie_tree); 661 tree_cnt++; 662 663 if (node_cnt + tree_cnt >= nr_shrink) 664 goto unlock_out; 665 cond_resched(); 666 } 667 mutex_unlock(&sbi->extent_tree_lock); 668 669 free_node: 670 /* 2. remove LRU extent entries */ 671 if (!mutex_trylock(&sbi->extent_tree_lock)) 672 goto out; 673 674 remained = nr_shrink - (node_cnt + tree_cnt); 675 676 spin_lock(&sbi->extent_lock); 677 for (; remained > 0; remained--) { 678 if (list_empty(&sbi->extent_list)) 679 break; 680 en = list_first_entry(&sbi->extent_list, 681 struct extent_node, list); 682 et = en->et; 683 if (!write_trylock(&et->lock)) { 684 /* refresh this extent node's position in extent list */ 685 list_move_tail(&en->list, &sbi->extent_list); 686 continue; 687 } 688 689 list_del_init(&en->list); 690 spin_unlock(&sbi->extent_lock); 691 692 __detach_extent_node(sbi, et, en); 693 694 write_unlock(&et->lock); 695 node_cnt++; 696 spin_lock(&sbi->extent_lock); 697 } 698 spin_unlock(&sbi->extent_lock); 699 700 unlock_out: 701 mutex_unlock(&sbi->extent_tree_lock); 702 out: 703 trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt); 704 705 return node_cnt + tree_cnt; 706 } 707 708 unsigned int f2fs_destroy_extent_node(struct inode *inode) 709 { 710 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 711 struct extent_tree *et = F2FS_I(inode)->extent_tree; 712 unsigned int node_cnt = 0; 713 714 if (!et || !atomic_read(&et->node_cnt)) 715 return 0; 716 717 write_lock(&et->lock); 718 node_cnt = __free_extent_tree(sbi, et); 719 write_unlock(&et->lock); 720 721 return node_cnt; 722 } 723 724 void f2fs_drop_extent_tree(struct inode *inode) 725 { 726 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 727 struct extent_tree *et = F2FS_I(inode)->extent_tree; 728 bool updated = false; 729 730 if (!f2fs_may_extent_tree(inode)) 731 return; 732 733 set_inode_flag(inode, FI_NO_EXTENT); 734 735 write_lock(&et->lock); 736 __free_extent_tree(sbi, et); 737 if (et->largest.len) { 738 et->largest.len = 0; 739 updated = true; 740 } 741 write_unlock(&et->lock); 742 if (updated) 743 f2fs_mark_inode_dirty_sync(inode, true); 744 } 745 746 void f2fs_destroy_extent_tree(struct inode *inode) 747 { 748 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 749 struct extent_tree *et = F2FS_I(inode)->extent_tree; 750 unsigned int node_cnt = 0; 751 752 if (!et) 753 return; 754 755 if (inode->i_nlink && !is_bad_inode(inode) && 756 atomic_read(&et->node_cnt)) { 757 mutex_lock(&sbi->extent_tree_lock); 758 list_add_tail(&et->list, &sbi->zombie_list); 759 atomic_inc(&sbi->total_zombie_tree); 760 mutex_unlock(&sbi->extent_tree_lock); 761 return; 762 } 763 764 /* free all extent info belong to this extent tree */ 765 node_cnt = f2fs_destroy_extent_node(inode); 766 767 /* delete extent tree entry in radix tree */ 768 mutex_lock(&sbi->extent_tree_lock); 769 f2fs_bug_on(sbi, atomic_read(&et->node_cnt)); 770 radix_tree_delete(&sbi->extent_tree_root, inode->i_ino); 771 kmem_cache_free(extent_tree_slab, et); 772 atomic_dec(&sbi->total_ext_tree); 773 mutex_unlock(&sbi->extent_tree_lock); 774 775 F2FS_I(inode)->extent_tree = NULL; 776 777 trace_f2fs_destroy_extent_tree(inode, node_cnt); 778 } 779 780 bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs, 781 struct extent_info *ei) 782 { 783 if (!f2fs_may_extent_tree(inode)) 784 return false; 785 786 return f2fs_lookup_extent_tree(inode, pgofs, ei); 787 } 788 789 void f2fs_update_extent_cache(struct dnode_of_data *dn) 790 { 791 pgoff_t fofs; 792 block_t blkaddr; 793 794 if (!f2fs_may_extent_tree(dn->inode)) 795 return; 796 797 if (dn->data_blkaddr == NEW_ADDR) 798 blkaddr = NULL_ADDR; 799 else 800 blkaddr = dn->data_blkaddr; 801 802 fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) + 803 dn->ofs_in_node; 804 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1); 805 } 806 807 void f2fs_update_extent_cache_range(struct dnode_of_data *dn, 808 pgoff_t fofs, block_t blkaddr, unsigned int len) 809 810 { 811 if (!f2fs_may_extent_tree(dn->inode)) 812 return; 813 814 f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len); 815 } 816 817 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi) 818 { 819 INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO); 820 mutex_init(&sbi->extent_tree_lock); 821 INIT_LIST_HEAD(&sbi->extent_list); 822 spin_lock_init(&sbi->extent_lock); 823 atomic_set(&sbi->total_ext_tree, 0); 824 INIT_LIST_HEAD(&sbi->zombie_list); 825 atomic_set(&sbi->total_zombie_tree, 0); 826 atomic_set(&sbi->total_ext_node, 0); 827 } 828 829 int __init f2fs_create_extent_cache(void) 830 { 831 extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree", 832 sizeof(struct extent_tree)); 833 if (!extent_tree_slab) 834 return -ENOMEM; 835 extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node", 836 sizeof(struct extent_node)); 837 if (!extent_node_slab) { 838 kmem_cache_destroy(extent_tree_slab); 839 return -ENOMEM; 840 } 841 return 0; 842 } 843 844 void f2fs_destroy_extent_cache(void) 845 { 846 kmem_cache_destroy(extent_node_slab); 847 kmem_cache_destroy(extent_tree_slab); 848 } 849