2 * Copyright (C) 2008 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
21 #include "ref-cache.h"
22 #include "transaction.h"
24 struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
27 struct btrfs_leaf_ref *ref;
28 size_t size = btrfs_leaf_ref_size(nr_extents);
30 ref = kmalloc(size, GFP_NOFS);
32 spin_lock(&root->fs_info->ref_cache_lock);
33 root->fs_info->total_ref_cache_size += size;
34 spin_unlock(&root->fs_info->ref_cache_lock);
36 memset(ref, 0, sizeof(*ref));
37 atomic_set(&ref->usage, 1);
38 INIT_LIST_HEAD(&ref->list);
43 void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
47 WARN_ON(atomic_read(&ref->usage) == 0);
48 if (atomic_dec_and_test(&ref->usage)) {
49 size_t size = btrfs_leaf_ref_size(ref->nritems);
54 spin_lock(&root->fs_info->ref_cache_lock);
55 root->fs_info->total_ref_cache_size -= size;
56 spin_unlock(&root->fs_info->ref_cache_lock);
60 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
63 struct rb_node ** p = &root->rb_node;
64 struct rb_node * parent = NULL;
65 struct btrfs_leaf_ref *entry;
69 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
70 WARN_ON(!entry->in_tree);
72 if (bytenr < entry->bytenr)
74 else if (bytenr > entry->bytenr)
80 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
82 rb_link_node(node, parent, p);
83 rb_insert_color(node, root);
87 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
89 struct rb_node * n = root->rb_node;
90 struct btrfs_leaf_ref *entry;
93 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
94 WARN_ON(!entry->in_tree);
96 if (bytenr < entry->bytenr)
98 else if (bytenr > entry->bytenr)
106 int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen)
108 struct btrfs_leaf_ref *ref = NULL;
109 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
114 spin_lock(&tree->lock);
115 while(!list_empty(&tree->list)) {
116 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
117 BUG_ON(!ref->in_tree);
118 if (ref->root_gen > max_root_gen)
121 rb_erase(&ref->rb_node, &tree->root);
123 list_del_init(&ref->list);
125 spin_unlock(&tree->lock);
126 btrfs_free_leaf_ref(root, ref);
128 spin_lock(&tree->lock);
130 spin_unlock(&tree->lock);
134 struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
138 struct btrfs_leaf_ref *ref = NULL;
139 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
144 spin_lock(&tree->lock);
145 rb = tree_search(&tree->root, bytenr);
147 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
149 atomic_inc(&ref->usage);
150 spin_unlock(&tree->lock);
154 int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
158 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
160 spin_lock(&tree->lock);
161 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
165 atomic_inc(&ref->usage);
166 list_add_tail(&ref->list, &tree->list);
168 spin_unlock(&tree->lock);
172 int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
174 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
176 BUG_ON(!ref->in_tree);
177 spin_lock(&tree->lock);
179 rb_erase(&ref->rb_node, &tree->root);
181 list_del_init(&ref->list);
183 spin_unlock(&tree->lock);
185 btrfs_free_leaf_ref(root, ref);