linux/fs/f2fs/extent_cache.c
<<
>>
Prefs
   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
  18static 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
  30static 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
  49struct 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
  61struct 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 */
  95struct 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
 154lookup_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
 168bool 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_info(sbi, "inconsistent rbtree, cur(%u, %u) next(%u, %u)",
 188                                  cur_re->ofs, cur_re->len,
 189                                  next_re->ofs, next_re->len);
 190                        return false;
 191                }
 192
 193                cur = next;
 194        }
 195#endif
 196        return true;
 197}
 198
 199static struct kmem_cache *extent_tree_slab;
 200static struct kmem_cache *extent_node_slab;
 201
 202static struct extent_node *__attach_extent_node(struct f2fs_sb_info *sbi,
 203                                struct extent_tree *et, struct extent_info *ei,
 204                                struct rb_node *parent, struct rb_node **p,
 205                                bool leftmost)
 206{
 207        struct extent_node *en;
 208
 209        en = kmem_cache_alloc(extent_node_slab, GFP_ATOMIC);
 210        if (!en)
 211                return NULL;
 212
 213        en->ei = *ei;
 214        INIT_LIST_HEAD(&en->list);
 215        en->et = et;
 216
 217        rb_link_node(&en->rb_node, parent, p);
 218        rb_insert_color_cached(&en->rb_node, &et->root, leftmost);
 219        atomic_inc(&et->node_cnt);
 220        atomic_inc(&sbi->total_ext_node);
 221        return en;
 222}
 223
 224static void __detach_extent_node(struct f2fs_sb_info *sbi,
 225                                struct extent_tree *et, struct extent_node *en)
 226{
 227        rb_erase_cached(&en->rb_node, &et->root);
 228        atomic_dec(&et->node_cnt);
 229        atomic_dec(&sbi->total_ext_node);
 230
 231        if (et->cached_en == en)
 232                et->cached_en = NULL;
 233        kmem_cache_free(extent_node_slab, en);
 234}
 235
 236/*
 237 * Flow to release an extent_node:
 238 * 1. list_del_init
 239 * 2. __detach_extent_node
 240 * 3. kmem_cache_free.
 241 */
 242static void __release_extent_node(struct f2fs_sb_info *sbi,
 243                        struct extent_tree *et, struct extent_node *en)
 244{
 245        spin_lock(&sbi->extent_lock);
 246        f2fs_bug_on(sbi, list_empty(&en->list));
 247        list_del_init(&en->list);
 248        spin_unlock(&sbi->extent_lock);
 249
 250        __detach_extent_node(sbi, et, en);
 251}
 252
 253static struct extent_tree *__grab_extent_tree(struct inode *inode)
 254{
 255        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 256        struct extent_tree *et;
 257        nid_t ino = inode->i_ino;
 258
 259        mutex_lock(&sbi->extent_tree_lock);
 260        et = radix_tree_lookup(&sbi->extent_tree_root, ino);
 261        if (!et) {
 262                et = f2fs_kmem_cache_alloc(extent_tree_slab, GFP_NOFS);
 263                f2fs_radix_tree_insert(&sbi->extent_tree_root, ino, et);
 264                memset(et, 0, sizeof(struct extent_tree));
 265                et->ino = ino;
 266                et->root = RB_ROOT_CACHED;
 267                et->cached_en = NULL;
 268                rwlock_init(&et->lock);
 269                INIT_LIST_HEAD(&et->list);
 270                atomic_set(&et->node_cnt, 0);
 271                atomic_inc(&sbi->total_ext_tree);
 272        } else {
 273                atomic_dec(&sbi->total_zombie_tree);
 274                list_del_init(&et->list);
 275        }
 276        mutex_unlock(&sbi->extent_tree_lock);
 277
 278        /* never died until evict_inode */
 279        F2FS_I(inode)->extent_tree = et;
 280
 281        return et;
 282}
 283
 284static struct extent_node *__init_extent_tree(struct f2fs_sb_info *sbi,
 285                                struct extent_tree *et, struct extent_info *ei)
 286{
 287        struct rb_node **p = &et->root.rb_root.rb_node;
 288        struct extent_node *en;
 289
 290        en = __attach_extent_node(sbi, et, ei, NULL, p, true);
 291        if (!en)
 292                return NULL;
 293
 294        et->largest = en->ei;
 295        et->cached_en = en;
 296        return en;
 297}
 298
 299static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
 300                                        struct extent_tree *et)
 301{
 302        struct rb_node *node, *next;
 303        struct extent_node *en;
 304        unsigned int count = atomic_read(&et->node_cnt);
 305
 306        node = rb_first_cached(&et->root);
 307        while (node) {
 308                next = rb_next(node);
 309                en = rb_entry(node, struct extent_node, rb_node);
 310                __release_extent_node(sbi, et, en);
 311                node = next;
 312        }
 313
 314        return count - atomic_read(&et->node_cnt);
 315}
 316
 317static void __drop_largest_extent(struct extent_tree *et,
 318                                        pgoff_t fofs, unsigned int len)
 319{
 320        if (fofs < et->largest.fofs + et->largest.len &&
 321                        fofs + len > et->largest.fofs) {
 322                et->largest.len = 0;
 323                et->largest_updated = true;
 324        }
 325}
 326
 327/* return true, if inode page is changed */
 328static bool __f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
 329{
 330        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 331        struct extent_tree *et;
 332        struct extent_node *en;
 333        struct extent_info ei;
 334
 335        if (!f2fs_may_extent_tree(inode)) {
 336                /* drop largest extent */
 337                if (i_ext && i_ext->len) {
 338                        i_ext->len = 0;
 339                        return true;
 340                }
 341                return false;
 342        }
 343
 344        et = __grab_extent_tree(inode);
 345
 346        if (!i_ext || !i_ext->len)
 347                return false;
 348
 349        get_extent_info(&ei, i_ext);
 350
 351        write_lock(&et->lock);
 352        if (atomic_read(&et->node_cnt))
 353                goto out;
 354
 355        en = __init_extent_tree(sbi, et, &ei);
 356        if (en) {
 357                spin_lock(&sbi->extent_lock);
 358                list_add_tail(&en->list, &sbi->extent_list);
 359                spin_unlock(&sbi->extent_lock);
 360        }
 361out:
 362        write_unlock(&et->lock);
 363        return false;
 364}
 365
 366bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext)
 367{
 368        bool ret =  __f2fs_init_extent_tree(inode, i_ext);
 369
 370        if (!F2FS_I(inode)->extent_tree)
 371                set_inode_flag(inode, FI_NO_EXTENT);
 372
 373        return ret;
 374}
 375
 376static bool f2fs_lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
 377                                                        struct extent_info *ei)
 378{
 379        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 380        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 381        struct extent_node *en;
 382        bool ret = false;
 383
 384        f2fs_bug_on(sbi, !et);
 385
 386        trace_f2fs_lookup_extent_tree_start(inode, pgofs);
 387
 388        read_lock(&et->lock);
 389
 390        if (et->largest.fofs <= pgofs &&
 391                        et->largest.fofs + et->largest.len > pgofs) {
 392                *ei = et->largest;
 393                ret = true;
 394                stat_inc_largest_node_hit(sbi);
 395                goto out;
 396        }
 397
 398        en = (struct extent_node *)f2fs_lookup_rb_tree(&et->root,
 399                                (struct rb_entry *)et->cached_en, pgofs);
 400        if (!en)
 401                goto out;
 402
 403        if (en == et->cached_en)
 404                stat_inc_cached_node_hit(sbi);
 405        else
 406                stat_inc_rbtree_node_hit(sbi);
 407
 408        *ei = en->ei;
 409        spin_lock(&sbi->extent_lock);
 410        if (!list_empty(&en->list)) {
 411                list_move_tail(&en->list, &sbi->extent_list);
 412                et->cached_en = en;
 413        }
 414        spin_unlock(&sbi->extent_lock);
 415        ret = true;
 416out:
 417        stat_inc_total_hit(sbi);
 418        read_unlock(&et->lock);
 419
 420        trace_f2fs_lookup_extent_tree_end(inode, pgofs, ei);
 421        return ret;
 422}
 423
 424static struct extent_node *__try_merge_extent_node(struct f2fs_sb_info *sbi,
 425                                struct extent_tree *et, struct extent_info *ei,
 426                                struct extent_node *prev_ex,
 427                                struct extent_node *next_ex)
 428{
 429        struct extent_node *en = NULL;
 430
 431        if (prev_ex && __is_back_mergeable(ei, &prev_ex->ei)) {
 432                prev_ex->ei.len += ei->len;
 433                ei = &prev_ex->ei;
 434                en = prev_ex;
 435        }
 436
 437        if (next_ex && __is_front_mergeable(ei, &next_ex->ei)) {
 438                next_ex->ei.fofs = ei->fofs;
 439                next_ex->ei.blk = ei->blk;
 440                next_ex->ei.len += ei->len;
 441                if (en)
 442                        __release_extent_node(sbi, et, prev_ex);
 443
 444                en = next_ex;
 445        }
 446
 447        if (!en)
 448                return NULL;
 449
 450        __try_update_largest_extent(et, en);
 451
 452        spin_lock(&sbi->extent_lock);
 453        if (!list_empty(&en->list)) {
 454                list_move_tail(&en->list, &sbi->extent_list);
 455                et->cached_en = en;
 456        }
 457        spin_unlock(&sbi->extent_lock);
 458        return en;
 459}
 460
 461static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
 462                                struct extent_tree *et, struct extent_info *ei,
 463                                struct rb_node **insert_p,
 464                                struct rb_node *insert_parent,
 465                                bool leftmost)
 466{
 467        struct rb_node **p;
 468        struct rb_node *parent = NULL;
 469        struct extent_node *en = NULL;
 470
 471        if (insert_p && insert_parent) {
 472                parent = insert_parent;
 473                p = insert_p;
 474                goto do_insert;
 475        }
 476
 477        leftmost = true;
 478
 479        p = f2fs_lookup_rb_tree_for_insert(sbi, &et->root, &parent,
 480                                                ei->fofs, &leftmost);
 481do_insert:
 482        en = __attach_extent_node(sbi, et, ei, parent, p, leftmost);
 483        if (!en)
 484                return NULL;
 485
 486        __try_update_largest_extent(et, en);
 487
 488        /* update in global extent list */
 489        spin_lock(&sbi->extent_lock);
 490        list_add_tail(&en->list, &sbi->extent_list);
 491        et->cached_en = en;
 492        spin_unlock(&sbi->extent_lock);
 493        return en;
 494}
 495
 496static void f2fs_update_extent_tree_range(struct inode *inode,
 497                                pgoff_t fofs, block_t blkaddr, unsigned int len)
 498{
 499        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 500        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 501        struct extent_node *en = NULL, *en1 = NULL;
 502        struct extent_node *prev_en = NULL, *next_en = NULL;
 503        struct extent_info ei, dei, prev;
 504        struct rb_node **insert_p = NULL, *insert_parent = NULL;
 505        unsigned int end = fofs + len;
 506        unsigned int pos = (unsigned int)fofs;
 507        bool updated = false;
 508        bool leftmost = false;
 509
 510        if (!et)
 511                return;
 512
 513        trace_f2fs_update_extent_tree_range(inode, fofs, blkaddr, len);
 514
 515        write_lock(&et->lock);
 516
 517        if (is_inode_flag_set(inode, FI_NO_EXTENT)) {
 518                write_unlock(&et->lock);
 519                return;
 520        }
 521
 522        prev = et->largest;
 523        dei.len = 0;
 524
 525        /*
 526         * drop largest extent before lookup, in case it's already
 527         * been shrunk from extent tree
 528         */
 529        __drop_largest_extent(et, fofs, len);
 530
 531        /* 1. lookup first extent node in range [fofs, fofs + len - 1] */
 532        en = (struct extent_node *)f2fs_lookup_rb_tree_ret(&et->root,
 533                                        (struct rb_entry *)et->cached_en, fofs,
 534                                        (struct rb_entry **)&prev_en,
 535                                        (struct rb_entry **)&next_en,
 536                                        &insert_p, &insert_parent, false,
 537                                        &leftmost);
 538        if (!en)
 539                en = next_en;
 540
 541        /* 2. invlidate all extent nodes in range [fofs, fofs + len - 1] */
 542        while (en && en->ei.fofs < end) {
 543                unsigned int org_end;
 544                int parts = 0;  /* # of parts current extent split into */
 545
 546                next_en = en1 = NULL;
 547
 548                dei = en->ei;
 549                org_end = dei.fofs + dei.len;
 550                f2fs_bug_on(sbi, pos >= org_end);
 551
 552                if (pos > dei.fofs &&   pos - dei.fofs >= F2FS_MIN_EXTENT_LEN) {
 553                        en->ei.len = pos - en->ei.fofs;
 554                        prev_en = en;
 555                        parts = 1;
 556                }
 557
 558                if (end < org_end && org_end - end >= F2FS_MIN_EXTENT_LEN) {
 559                        if (parts) {
 560                                set_extent_info(&ei, end,
 561                                                end - dei.fofs + dei.blk,
 562                                                org_end - end);
 563                                en1 = __insert_extent_tree(sbi, et, &ei,
 564                                                        NULL, NULL, true);
 565                                next_en = en1;
 566                        } else {
 567                                en->ei.fofs = end;
 568                                en->ei.blk += end - dei.fofs;
 569                                en->ei.len -= end - dei.fofs;
 570                                next_en = en;
 571                        }
 572                        parts++;
 573                }
 574
 575                if (!next_en) {
 576                        struct rb_node *node = rb_next(&en->rb_node);
 577
 578                        next_en = rb_entry_safe(node, struct extent_node,
 579                                                rb_node);
 580                }
 581
 582                if (parts)
 583                        __try_update_largest_extent(et, en);
 584                else
 585                        __release_extent_node(sbi, et, en);
 586
 587                /*
 588                 * if original extent is split into zero or two parts, extent
 589                 * tree has been altered by deletion or insertion, therefore
 590                 * invalidate pointers regard to tree.
 591                 */
 592                if (parts != 1) {
 593                        insert_p = NULL;
 594                        insert_parent = NULL;
 595                }
 596                en = next_en;
 597        }
 598
 599        /* 3. update extent in extent cache */
 600        if (blkaddr) {
 601
 602                set_extent_info(&ei, fofs, blkaddr, len);
 603                if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
 604                        __insert_extent_tree(sbi, et, &ei,
 605                                        insert_p, insert_parent, leftmost);
 606
 607                /* give up extent_cache, if split and small updates happen */
 608                if (dei.len >= 1 &&
 609                                prev.len < F2FS_MIN_EXTENT_LEN &&
 610                                et->largest.len < F2FS_MIN_EXTENT_LEN) {
 611                        et->largest.len = 0;
 612                        et->largest_updated = true;
 613                        set_inode_flag(inode, FI_NO_EXTENT);
 614                }
 615        }
 616
 617        if (is_inode_flag_set(inode, FI_NO_EXTENT))
 618                __free_extent_tree(sbi, et);
 619
 620        if (et->largest_updated) {
 621                et->largest_updated = false;
 622                updated = true;
 623        }
 624
 625        write_unlock(&et->lock);
 626
 627        if (updated)
 628                f2fs_mark_inode_dirty_sync(inode, true);
 629}
 630
 631unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink)
 632{
 633        struct extent_tree *et, *next;
 634        struct extent_node *en;
 635        unsigned int node_cnt = 0, tree_cnt = 0;
 636        int remained;
 637
 638        if (!test_opt(sbi, EXTENT_CACHE))
 639                return 0;
 640
 641        if (!atomic_read(&sbi->total_zombie_tree))
 642                goto free_node;
 643
 644        if (!mutex_trylock(&sbi->extent_tree_lock))
 645                goto out;
 646
 647        /* 1. remove unreferenced extent tree */
 648        list_for_each_entry_safe(et, next, &sbi->zombie_list, list) {
 649                if (atomic_read(&et->node_cnt)) {
 650                        write_lock(&et->lock);
 651                        node_cnt += __free_extent_tree(sbi, et);
 652                        write_unlock(&et->lock);
 653                }
 654                f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
 655                list_del_init(&et->list);
 656                radix_tree_delete(&sbi->extent_tree_root, et->ino);
 657                kmem_cache_free(extent_tree_slab, et);
 658                atomic_dec(&sbi->total_ext_tree);
 659                atomic_dec(&sbi->total_zombie_tree);
 660                tree_cnt++;
 661
 662                if (node_cnt + tree_cnt >= nr_shrink)
 663                        goto unlock_out;
 664                cond_resched();
 665        }
 666        mutex_unlock(&sbi->extent_tree_lock);
 667
 668free_node:
 669        /* 2. remove LRU extent entries */
 670        if (!mutex_trylock(&sbi->extent_tree_lock))
 671                goto out;
 672
 673        remained = nr_shrink - (node_cnt + tree_cnt);
 674
 675        spin_lock(&sbi->extent_lock);
 676        for (; remained > 0; remained--) {
 677                if (list_empty(&sbi->extent_list))
 678                        break;
 679                en = list_first_entry(&sbi->extent_list,
 680                                        struct extent_node, list);
 681                et = en->et;
 682                if (!write_trylock(&et->lock)) {
 683                        /* refresh this extent node's position in extent list */
 684                        list_move_tail(&en->list, &sbi->extent_list);
 685                        continue;
 686                }
 687
 688                list_del_init(&en->list);
 689                spin_unlock(&sbi->extent_lock);
 690
 691                __detach_extent_node(sbi, et, en);
 692
 693                write_unlock(&et->lock);
 694                node_cnt++;
 695                spin_lock(&sbi->extent_lock);
 696        }
 697        spin_unlock(&sbi->extent_lock);
 698
 699unlock_out:
 700        mutex_unlock(&sbi->extent_tree_lock);
 701out:
 702        trace_f2fs_shrink_extent_tree(sbi, node_cnt, tree_cnt);
 703
 704        return node_cnt + tree_cnt;
 705}
 706
 707unsigned int f2fs_destroy_extent_node(struct inode *inode)
 708{
 709        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 710        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 711        unsigned int node_cnt = 0;
 712
 713        if (!et || !atomic_read(&et->node_cnt))
 714                return 0;
 715
 716        write_lock(&et->lock);
 717        node_cnt = __free_extent_tree(sbi, et);
 718        write_unlock(&et->lock);
 719
 720        return node_cnt;
 721}
 722
 723void f2fs_drop_extent_tree(struct inode *inode)
 724{
 725        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 726        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 727        bool updated = false;
 728
 729        if (!f2fs_may_extent_tree(inode))
 730                return;
 731
 732        set_inode_flag(inode, FI_NO_EXTENT);
 733
 734        write_lock(&et->lock);
 735        __free_extent_tree(sbi, et);
 736        if (et->largest.len) {
 737                et->largest.len = 0;
 738                updated = true;
 739        }
 740        write_unlock(&et->lock);
 741        if (updated)
 742                f2fs_mark_inode_dirty_sync(inode, true);
 743}
 744
 745void f2fs_destroy_extent_tree(struct inode *inode)
 746{
 747        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 748        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 749        unsigned int node_cnt = 0;
 750
 751        if (!et)
 752                return;
 753
 754        if (inode->i_nlink && !is_bad_inode(inode) &&
 755                                        atomic_read(&et->node_cnt)) {
 756                mutex_lock(&sbi->extent_tree_lock);
 757                list_add_tail(&et->list, &sbi->zombie_list);
 758                atomic_inc(&sbi->total_zombie_tree);
 759                mutex_unlock(&sbi->extent_tree_lock);
 760                return;
 761        }
 762
 763        /* free all extent info belong to this extent tree */
 764        node_cnt = f2fs_destroy_extent_node(inode);
 765
 766        /* delete extent tree entry in radix tree */
 767        mutex_lock(&sbi->extent_tree_lock);
 768        f2fs_bug_on(sbi, atomic_read(&et->node_cnt));
 769        radix_tree_delete(&sbi->extent_tree_root, inode->i_ino);
 770        kmem_cache_free(extent_tree_slab, et);
 771        atomic_dec(&sbi->total_ext_tree);
 772        mutex_unlock(&sbi->extent_tree_lock);
 773
 774        F2FS_I(inode)->extent_tree = NULL;
 775
 776        trace_f2fs_destroy_extent_tree(inode, node_cnt);
 777}
 778
 779bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
 780                                        struct extent_info *ei)
 781{
 782        if (!f2fs_may_extent_tree(inode))
 783                return false;
 784
 785        return f2fs_lookup_extent_tree(inode, pgofs, ei);
 786}
 787
 788void f2fs_update_extent_cache(struct dnode_of_data *dn)
 789{
 790        pgoff_t fofs;
 791        block_t blkaddr;
 792
 793        if (!f2fs_may_extent_tree(dn->inode))
 794                return;
 795
 796        if (dn->data_blkaddr == NEW_ADDR)
 797                blkaddr = NULL_ADDR;
 798        else
 799                blkaddr = dn->data_blkaddr;
 800
 801        fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_page), dn->inode) +
 802                                                                dn->ofs_in_node;
 803        f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, 1);
 804}
 805
 806void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
 807                                pgoff_t fofs, block_t blkaddr, unsigned int len)
 808
 809{
 810        if (!f2fs_may_extent_tree(dn->inode))
 811                return;
 812
 813        f2fs_update_extent_tree_range(dn->inode, fofs, blkaddr, len);
 814}
 815
 816void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
 817{
 818        INIT_RADIX_TREE(&sbi->extent_tree_root, GFP_NOIO);
 819        mutex_init(&sbi->extent_tree_lock);
 820        INIT_LIST_HEAD(&sbi->extent_list);
 821        spin_lock_init(&sbi->extent_lock);
 822        atomic_set(&sbi->total_ext_tree, 0);
 823        INIT_LIST_HEAD(&sbi->zombie_list);
 824        atomic_set(&sbi->total_zombie_tree, 0);
 825        atomic_set(&sbi->total_ext_node, 0);
 826}
 827
 828int __init f2fs_create_extent_cache(void)
 829{
 830        extent_tree_slab = f2fs_kmem_cache_create("f2fs_extent_tree",
 831                        sizeof(struct extent_tree));
 832        if (!extent_tree_slab)
 833                return -ENOMEM;
 834        extent_node_slab = f2fs_kmem_cache_create("f2fs_extent_node",
 835                        sizeof(struct extent_node));
 836        if (!extent_node_slab) {
 837                kmem_cache_destroy(extent_tree_slab);
 838                return -ENOMEM;
 839        }
 840        return 0;
 841}
 842
 843void f2fs_destroy_extent_cache(void)
 844{
 845        kmem_cache_destroy(extent_node_slab);
 846        kmem_cache_destroy(extent_tree_slab);
 847}
 848