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/sort.h>
22 #include "ref-cache.h"
23 #include "transaction.h"
26 * leaf refs are used to cache the information about which extents
27 * a given leaf has references on. This allows us to process that leaf
28 * in btrfs_drop_snapshot without needing to read it back from disk.
32 * kmalloc a leaf reference struct and update the counters for the
33 * total ref cache size
35 struct btrfs_leaf_ref
*btrfs_alloc_leaf_ref(struct btrfs_root
*root
,
38 struct btrfs_leaf_ref
*ref
;
39 size_t size
= btrfs_leaf_ref_size(nr_extents
);
41 ref
= kmalloc(size
, GFP_NOFS
);
43 spin_lock(&root
->fs_info
->ref_cache_lock
);
44 root
->fs_info
->total_ref_cache_size
+= size
;
45 spin_unlock(&root
->fs_info
->ref_cache_lock
);
47 memset(ref
, 0, sizeof(*ref
));
48 atomic_set(&ref
->usage
, 1);
49 INIT_LIST_HEAD(&ref
->list
);
55 * free a leaf reference struct and update the counters for the
56 * total ref cache size
58 void btrfs_free_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
)
62 WARN_ON(atomic_read(&ref
->usage
) == 0);
63 if (atomic_dec_and_test(&ref
->usage
)) {
64 size_t size
= btrfs_leaf_ref_size(ref
->nritems
);
69 spin_lock(&root
->fs_info
->ref_cache_lock
);
70 root
->fs_info
->total_ref_cache_size
-= size
;
71 spin_unlock(&root
->fs_info
->ref_cache_lock
);
75 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 bytenr
,
78 struct rb_node
**p
= &root
->rb_node
;
79 struct rb_node
*parent
= NULL
;
80 struct btrfs_leaf_ref
*entry
;
84 entry
= rb_entry(parent
, struct btrfs_leaf_ref
, rb_node
);
86 if (bytenr
< entry
->bytenr
)
88 else if (bytenr
> entry
->bytenr
)
94 entry
= rb_entry(node
, struct btrfs_leaf_ref
, rb_node
);
95 rb_link_node(node
, parent
, p
);
96 rb_insert_color(node
, root
);
100 static struct rb_node
*tree_search(struct rb_root
*root
, u64 bytenr
)
102 struct rb_node
*n
= root
->rb_node
;
103 struct btrfs_leaf_ref
*entry
;
106 entry
= rb_entry(n
, struct btrfs_leaf_ref
, rb_node
);
107 WARN_ON(!entry
->in_tree
);
109 if (bytenr
< entry
->bytenr
)
111 else if (bytenr
> entry
->bytenr
)
119 int btrfs_remove_leaf_refs(struct btrfs_root
*root
, u64 max_root_gen
,
122 struct btrfs_leaf_ref
*ref
= NULL
;
123 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
126 tree
= &root
->fs_info
->shared_ref_tree
;
130 spin_lock(&tree
->lock
);
131 while (!list_empty(&tree
->list
)) {
132 ref
= list_entry(tree
->list
.next
, struct btrfs_leaf_ref
, list
);
133 BUG_ON(ref
->tree
!= tree
);
134 if (ref
->root_gen
> max_root_gen
)
136 if (!xchg(&ref
->in_tree
, 0)) {
137 cond_resched_lock(&tree
->lock
);
141 rb_erase(&ref
->rb_node
, &tree
->root
);
142 list_del_init(&ref
->list
);
144 spin_unlock(&tree
->lock
);
145 btrfs_free_leaf_ref(root
, ref
);
147 spin_lock(&tree
->lock
);
149 spin_unlock(&tree
->lock
);
154 * find the leaf ref for a given extent. This returns the ref struct with
155 * a usage reference incremented
157 struct btrfs_leaf_ref
*btrfs_lookup_leaf_ref(struct btrfs_root
*root
,
161 struct btrfs_leaf_ref
*ref
= NULL
;
162 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
165 spin_lock(&tree
->lock
);
166 rb
= tree_search(&tree
->root
, bytenr
);
168 ref
= rb_entry(rb
, struct btrfs_leaf_ref
, rb_node
);
170 atomic_inc(&ref
->usage
);
171 spin_unlock(&tree
->lock
);
175 if (tree
!= &root
->fs_info
->shared_ref_tree
) {
176 tree
= &root
->fs_info
->shared_ref_tree
;
183 * add a fully filled in leaf ref struct
184 * remove all the refs older than a given root generation
186 int btrfs_add_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
,
191 struct btrfs_leaf_ref_tree
*tree
= root
->ref_tree
;
194 tree
= &root
->fs_info
->shared_ref_tree
;
196 spin_lock(&tree
->lock
);
197 rb
= tree_insert(&tree
->root
, ref
->bytenr
, &ref
->rb_node
);
201 atomic_inc(&ref
->usage
);
204 list_add_tail(&ref
->list
, &tree
->list
);
206 spin_unlock(&tree
->lock
);
211 * remove a single leaf ref from the tree. This drops the ref held by the tree
214 int btrfs_remove_leaf_ref(struct btrfs_root
*root
, struct btrfs_leaf_ref
*ref
)
216 struct btrfs_leaf_ref_tree
*tree
;
218 if (!xchg(&ref
->in_tree
, 0))
222 spin_lock(&tree
->lock
);
224 rb_erase(&ref
->rb_node
, &tree
->root
);
225 list_del_init(&ref
->list
);
227 spin_unlock(&tree
->lock
);
229 btrfs_free_leaf_ref(root
, ref
);