]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/ref-cache.c
30fcb7aea5b5e709298fb813193f431738c811b1
[linux-2.6-omap-h63xx.git] / fs / btrfs / ref-cache.c
1 /*
2  * Copyright (C) 2008 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <linux/sched.h>
20 #include "ctree.h"
21 #include "ref-cache.h"
22 #include "transaction.h"
23
24 struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
25                                             int nr_extents)
26 {
27         struct btrfs_leaf_ref *ref;
28         size_t size = btrfs_leaf_ref_size(nr_extents);
29
30         ref = kmalloc(size, GFP_NOFS);
31         if (ref) {
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);
35
36                 memset(ref, 0, sizeof(*ref));
37                 atomic_set(&ref->usage, 1);
38                 INIT_LIST_HEAD(&ref->list);
39         }
40         return ref;
41 }
42
43 void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
44 {
45         if (!ref)
46                 return;
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);
50
51                 BUG_ON(ref->in_tree);
52                 kfree(ref);
53
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);
57         }
58 }
59
60 static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
61                                    struct rb_node *node)
62 {
63         struct rb_node ** p = &root->rb_node;
64         struct rb_node * parent = NULL;
65         struct btrfs_leaf_ref *entry;
66
67         while(*p) {
68                 parent = *p;
69                 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
70
71                 if (bytenr < entry->bytenr)
72                         p = &(*p)->rb_left;
73                 else if (bytenr > entry->bytenr)
74                         p = &(*p)->rb_right;
75                 else
76                         return parent;
77         }
78
79         entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
80         rb_link_node(node, parent, p);
81         rb_insert_color(node, root);
82         return NULL;
83 }
84
85 static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
86 {
87         struct rb_node * n = root->rb_node;
88         struct btrfs_leaf_ref *entry;
89
90         while(n) {
91                 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
92                 WARN_ON(!entry->in_tree);
93
94                 if (bytenr < entry->bytenr)
95                         n = n->rb_left;
96                 else if (bytenr > entry->bytenr)
97                         n = n->rb_right;
98                 else
99                         return n;
100         }
101         return NULL;
102 }
103
104 int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen,
105                            int shared)
106 {
107         struct btrfs_leaf_ref *ref = NULL;
108         struct btrfs_leaf_ref_tree *tree = root->ref_tree;
109
110         if (shared)
111                 tree = &root->fs_info->shared_ref_tree;
112         if (!tree)
113                 return 0;
114
115         spin_lock(&tree->lock);
116         while(!list_empty(&tree->list)) {
117                 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
118                 BUG_ON(ref->tree != tree);
119                 if (ref->root_gen > max_root_gen)
120                         break;
121                 if (!xchg(&ref->in_tree, 0)) {
122                         cond_resched_lock(&tree->lock);
123                         continue;
124                 }
125
126                 rb_erase(&ref->rb_node, &tree->root);
127                 list_del_init(&ref->list);
128
129                 spin_unlock(&tree->lock);
130                 btrfs_free_leaf_ref(root, ref);
131                 cond_resched();
132                 spin_lock(&tree->lock);
133         }
134         spin_unlock(&tree->lock);
135         return 0;
136 }
137
138 struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
139                                              u64 bytenr)
140 {
141         struct rb_node *rb;
142         struct btrfs_leaf_ref *ref = NULL;
143         struct btrfs_leaf_ref_tree *tree = root->ref_tree;
144 again:
145         if (tree) {
146                 spin_lock(&tree->lock);
147                 rb = tree_search(&tree->root, bytenr);
148                 if (rb)
149                         ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
150                 if (ref)
151                         atomic_inc(&ref->usage);
152                 spin_unlock(&tree->lock);
153                 if (ref)
154                         return ref;
155         }
156         if (tree != &root->fs_info->shared_ref_tree) {
157                 tree = &root->fs_info->shared_ref_tree;
158                 goto again;
159         }
160         return NULL;
161 }
162
163 int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref,
164                        int shared)
165 {
166         int ret = 0;
167         struct rb_node *rb;
168         struct btrfs_leaf_ref_tree *tree = root->ref_tree;
169
170         if (shared)
171                 tree = &root->fs_info->shared_ref_tree;
172
173         spin_lock(&tree->lock);
174         rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
175         if (rb) {
176                 ret = -EEXIST;
177         } else {
178                 atomic_inc(&ref->usage);
179                 ref->tree = tree;
180                 ref->in_tree = 1;
181                 list_add_tail(&ref->list, &tree->list);
182         }
183         spin_unlock(&tree->lock);
184         return ret;
185 }
186
187 int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
188 {
189         struct btrfs_leaf_ref_tree *tree;
190
191         if (!xchg(&ref->in_tree, 0))
192                 return 0;
193
194         tree = ref->tree;
195         spin_lock(&tree->lock);
196
197         rb_erase(&ref->rb_node, &tree->root);
198         list_del_init(&ref->list);
199
200         spin_unlock(&tree->lock);
201
202         btrfs_free_leaf_ref(root, ref);
203         return 0;
204 }