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(int nr_extents)
26 struct btrfs_leaf_ref *ref;
28 ref = kmalloc(btrfs_leaf_ref_size(nr_extents), GFP_NOFS);
30 memset(ref, 0, sizeof(*ref));
31 atomic_set(&ref->usage, 1);
32 INIT_LIST_HEAD(&ref->list);
37 void btrfs_free_leaf_ref(struct btrfs_leaf_ref *ref)
41 WARN_ON(atomic_read(&ref->usage) == 0);
42 if (atomic_dec_and_test(&ref->usage)) {
48 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
51 struct rb_node ** p = &root->rb_node;
52 struct rb_node * parent = NULL;
53 struct btrfs_leaf_ref *entry;
57 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
58 WARN_ON(!entry->in_tree);
60 if (bytenr < entry->bytenr)
62 else if (bytenr > entry->bytenr)
68 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
70 rb_link_node(node, parent, p);
71 rb_insert_color(node, root);
75 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
77 struct rb_node * n = root->rb_node;
78 struct btrfs_leaf_ref *entry;
81 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
82 WARN_ON(!entry->in_tree);
84 if (bytenr < entry->bytenr)
86 else if (bytenr > entry->bytenr)
94 int btrfs_remove_leaf_refs(struct btrfs_root *root)
97 struct btrfs_leaf_ref *ref = NULL;
98 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
103 spin_lock(&tree->lock);
104 while(!btrfs_leaf_ref_tree_empty(tree)) {
105 rb = rb_first(&tree->root);
106 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
107 rb_erase(&ref->rb_node, &tree->root);
109 list_del_init(&ref->list);
111 spin_unlock(&tree->lock);
113 btrfs_free_leaf_ref(ref);
116 spin_lock(&tree->lock);
118 spin_unlock(&tree->lock);
122 struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
126 struct btrfs_leaf_ref *ref = NULL;
127 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
132 spin_lock(&tree->lock);
133 rb = tree_search(&tree->root, bytenr);
135 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
137 atomic_inc(&ref->usage);
138 spin_unlock(&tree->lock);
142 int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
146 size_t size = btrfs_leaf_ref_size(ref->nritems);
147 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
149 spin_lock(&tree->lock);
150 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
154 spin_lock(&root->fs_info->ref_cache_lock);
155 root->fs_info->total_ref_cache_size += size;
156 spin_unlock(&root->fs_info->ref_cache_lock);
157 atomic_inc(&ref->usage);
158 list_add_tail(&ref->list, &tree->list);
160 spin_unlock(&tree->lock);
164 int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
166 size_t size = btrfs_leaf_ref_size(ref->nritems);
167 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
169 BUG_ON(!ref->in_tree);
170 spin_lock(&tree->lock);
172 spin_lock(&root->fs_info->ref_cache_lock);
173 root->fs_info->total_ref_cache_size -= size;
174 spin_unlock(&root->fs_info->ref_cache_lock);
176 rb_erase(&ref->rb_node, &tree->root);
178 list_del_init(&ref->list);
180 spin_unlock(&tree->lock);
182 btrfs_free_leaf_ref(ref);