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>
20 #include <linux/slab.h>
21 #include <linux/sort.h>
23 #include "ref-cache.h"
24 #include "transaction.h"
27 * leaf refs are used to cache the information about which extents
28 * a given leaf has references on. This allows us to process that leaf
29 * in btrfs_drop_snapshot without needing to read it back from disk.
33 * kmalloc a leaf reference struct and update the counters for the
34 * total ref cache size
36 struct btrfs_leaf_ref
*btrfs_alloc_leaf_ref(struct btrfs_root
*root
,
39 struct btrfs_leaf_ref
*ref
;
40 size_t size
= btrfs_leaf_ref_size(nr_extents
);
42 ref
= kmalloc(size
, GFP_NOFS
);
44 spin_lock(&root
->fs_info
->ref_cache_lock
);
45 root
->fs_info
->total_ref_cache_size
+= size
;
46 spin_unlock(&root
->fs_info
->ref_cache_lock
);
48 memset(ref
, 0, sizeof(*ref
));
49 atomic_set(&ref
->usage
, 1);
50 INIT_LIST_HEAD(&ref
->list
);
56 * free a leaf reference struct and update the counters for the
57 * total ref cache size
59 void btrfs_free_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
)
63 WARN_ON(atomic_read(&ref
->usage
) == 0);
64 if (atomic_dec_and_test(&ref
->usage
)) {
65 size_t size
= btrfs_leaf_ref_size(ref
->nritems
);
70 spin_lock(&root
->fs_info
->ref_cache_lock
);
71 root
->fs_info
->total_ref_cache_size
-= size
;
72 spin_unlock(&root
->fs_info
->ref_cache_lock
);
76 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
79 struct rb_node
**p
= &root
->rb_node
;
80 struct rb_node
*parent
= NULL
;
81 struct btrfs_leaf_ref
*entry
;
85 entry
= rb_entry(parent
, struct btrfs_leaf_ref
, rb_node
);
87 if (bytenr
< entry
->bytenr
)
89 else if (bytenr
> entry
->bytenr
)
95 entry
= rb_entry(node
, struct btrfs_leaf_ref
, rb_node
);
96 rb_link_node(node
, parent
, p
);
97 rb_insert_color(node
, root
);
101 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
103 struct rb_node
*n
= root
->rb_node
;
104 struct btrfs_leaf_ref
*entry
;
107 entry
= rb_entry(n
, struct btrfs_leaf_ref
, rb_node
);
108 WARN_ON(!entry
->in_tree
);
110 if (bytenr
< entry
->bytenr
)
112 else if (bytenr
> entry
->bytenr
)
120 int btrfs_remove_leaf_refs(struct btrfs_root
*root
, u64 max_root_gen
,
123 struct btrfs_leaf_ref
*ref
= NULL
;
124 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
127 tree
= &root
->fs_info
->shared_ref_tree
;
131 spin_lock(&tree
->lock
);
132 while (!list_empty(&tree
->list
)) {
133 ref
= list_entry(tree
->list
.next
, struct btrfs_leaf_ref
, list
);
134 BUG_ON(ref
->tree
!= tree
);
135 if (ref
->root_gen
> max_root_gen
)
137 if (!xchg(&ref
->in_tree
, 0)) {
138 cond_resched_lock(&tree
->lock
);
142 rb_erase(&ref
->rb_node
, &tree
->root
);
143 list_del_init(&ref
->list
);
145 spin_unlock(&tree
->lock
);
146 btrfs_free_leaf_ref(root
, ref
);
148 spin_lock(&tree
->lock
);
150 spin_unlock(&tree
->lock
);
155 * find the leaf ref for a given extent. This returns the ref struct with
156 * a usage reference incremented
158 struct btrfs_leaf_ref
*btrfs_lookup_leaf_ref(struct btrfs_root
*root
,
162 struct btrfs_leaf_ref
*ref
= NULL
;
163 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
166 spin_lock(&tree
->lock
);
167 rb
= tree_search(&tree
->root
, bytenr
);
169 ref
= rb_entry(rb
, struct btrfs_leaf_ref
, rb_node
);
171 atomic_inc(&ref
->usage
);
172 spin_unlock(&tree
->lock
);
176 if (tree
!= &root
->fs_info
->shared_ref_tree
) {
177 tree
= &root
->fs_info
->shared_ref_tree
;
184 * add a fully filled in leaf ref struct
185 * remove all the refs older than a given root generation
187 int btrfs_add_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
,
192 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
195 tree
= &root
->fs_info
->shared_ref_tree
;
197 spin_lock(&tree
->lock
);
198 rb
= tree_insert(&tree
->root
, ref
->bytenr
, &ref
->rb_node
);
202 atomic_inc(&ref
->usage
);
205 list_add_tail(&ref
->list
, &tree
->list
);
207 spin_unlock(&tree
->lock
);
212 * remove a single leaf ref from the tree. This drops the ref held by the tree
215 int btrfs_remove_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
)
217 struct btrfs_leaf_ref_tree
*tree
;
219 if (!xchg(&ref
->in_tree
, 0))
223 spin_lock(&tree
->lock
);
225 rb_erase(&ref
->rb_node
, &tree
->root
);
226 list_del_init(&ref
->list
);
228 spin_unlock(&tree
->lock
);
230 btrfs_free_leaf_ref(root
, ref
);