blob: d0cc62bccb948e776fb9845c7da1a8f2119788f6 [file] [log] [blame]
Yan Zheng31153d82008-07-28 15:32:19 -04001/*
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>
Chris Masonbd56b302009-02-04 09:27:02 -050020#include <linux/sort.h>
Yan Zheng31153d82008-07-28 15:32:19 -040021#include "ctree.h"
22#include "ref-cache.h"
23#include "transaction.h"
24
Chris Masond352ac62008-09-29 15:18:18 -040025/*
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.
29 */
30
31/*
32 * kmalloc a leaf reference struct and update the counters for the
33 * total ref cache size
34 */
Yanbcc63ab2008-07-30 16:29:20 -040035struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
36 int nr_extents)
Yan Zheng31153d82008-07-28 15:32:19 -040037{
38 struct btrfs_leaf_ref *ref;
Yanbcc63ab2008-07-30 16:29:20 -040039 size_t size = btrfs_leaf_ref_size(nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -040040
Yanbcc63ab2008-07-30 16:29:20 -040041 ref = kmalloc(size, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -040042 if (ref) {
Yanbcc63ab2008-07-30 16:29:20 -040043 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);
46
Yan Zheng31153d82008-07-28 15:32:19 -040047 memset(ref, 0, sizeof(*ref));
48 atomic_set(&ref->usage, 1);
Chris Mason017e5362008-07-28 15:32:51 -040049 INIT_LIST_HEAD(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -040050 }
51 return ref;
52}
53
Chris Masond352ac62008-09-29 15:18:18 -040054/*
55 * free a leaf reference struct and update the counters for the
56 * total ref cache size
57 */
Yanbcc63ab2008-07-30 16:29:20 -040058void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -040059{
60 if (!ref)
61 return;
62 WARN_ON(atomic_read(&ref->usage) == 0);
63 if (atomic_dec_and_test(&ref->usage)) {
Yanbcc63ab2008-07-30 16:29:20 -040064 size_t size = btrfs_leaf_ref_size(ref->nritems);
65
Yan Zheng31153d82008-07-28 15:32:19 -040066 BUG_ON(ref->in_tree);
67 kfree(ref);
Yanbcc63ab2008-07-30 16:29:20 -040068
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);
Yan Zheng31153d82008-07-28 15:32:19 -040072 }
73}
74
Chris Mason017e5362008-07-28 15:32:51 -040075static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
Yan Zheng31153d82008-07-28 15:32:19 -040076 struct rb_node *node)
77{
Chris Masond3977122009-01-05 21:25:51 -050078 struct rb_node **p = &root->rb_node;
79 struct rb_node *parent = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -040080 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -040081
Chris Masond3977122009-01-05 21:25:51 -050082 while (*p) {
Yan Zheng31153d82008-07-28 15:32:19 -040083 parent = *p;
84 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040085
Chris Mason017e5362008-07-28 15:32:51 -040086 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040087 p = &(*p)->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -040088 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040089 p = &(*p)->rb_right;
90 else
91 return parent;
92 }
Yanbcc63ab2008-07-30 16:29:20 -040093
Yan Zheng31153d82008-07-28 15:32:19 -040094 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040095 rb_link_node(node, parent, p);
96 rb_insert_color(node, root);
97 return NULL;
98}
99
Chris Mason017e5362008-07-28 15:32:51 -0400100static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400101{
Chris Masond3977122009-01-05 21:25:51 -0500102 struct rb_node *n = root->rb_node;
Yan Zheng31153d82008-07-28 15:32:19 -0400103 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -0400104
Chris Masond3977122009-01-05 21:25:51 -0500105 while (n) {
Yan Zheng31153d82008-07-28 15:32:19 -0400106 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
107 WARN_ON(!entry->in_tree);
108
Chris Mason017e5362008-07-28 15:32:51 -0400109 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400110 n = n->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -0400111 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400112 n = n->rb_right;
113 else
114 return n;
115 }
116 return NULL;
117}
118
Zheng Yane4657682008-09-26 10:04:53 -0400119int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen,
120 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400121{
Yan Zheng31153d82008-07-28 15:32:19 -0400122 struct btrfs_leaf_ref *ref = NULL;
123 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
124
Zheng Yane4657682008-09-26 10:04:53 -0400125 if (shared)
126 tree = &root->fs_info->shared_ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400127 if (!tree)
128 return 0;
129
130 spin_lock(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500131 while (!list_empty(&tree->list)) {
Yanbcc63ab2008-07-30 16:29:20 -0400132 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
Zheng Yane4657682008-09-26 10:04:53 -0400133 BUG_ON(ref->tree != tree);
Yanbcc63ab2008-07-30 16:29:20 -0400134 if (ref->root_gen > max_root_gen)
135 break;
Zheng Yane4657682008-09-26 10:04:53 -0400136 if (!xchg(&ref->in_tree, 0)) {
137 cond_resched_lock(&tree->lock);
138 continue;
139 }
Yanbcc63ab2008-07-30 16:29:20 -0400140
Yan Zheng31153d82008-07-28 15:32:19 -0400141 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400142 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400143
144 spin_unlock(&tree->lock);
Yanbcc63ab2008-07-30 16:29:20 -0400145 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400146 cond_resched();
147 spin_lock(&tree->lock);
148 }
149 spin_unlock(&tree->lock);
150 return 0;
151}
152
Chris Masond352ac62008-09-29 15:18:18 -0400153/*
154 * find the leaf ref for a given extent. This returns the ref struct with
155 * a usage reference incremented
156 */
Yan Zheng31153d82008-07-28 15:32:19 -0400157struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
Chris Mason017e5362008-07-28 15:32:51 -0400158 u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400159{
160 struct rb_node *rb;
161 struct btrfs_leaf_ref *ref = NULL;
162 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Zheng Yane4657682008-09-26 10:04:53 -0400163again:
164 if (tree) {
165 spin_lock(&tree->lock);
166 rb = tree_search(&tree->root, bytenr);
167 if (rb)
168 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
169 if (ref)
170 atomic_inc(&ref->usage);
171 spin_unlock(&tree->lock);
172 if (ref)
173 return ref;
174 }
175 if (tree != &root->fs_info->shared_ref_tree) {
176 tree = &root->fs_info->shared_ref_tree;
177 goto again;
178 }
179 return NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/*
183 * add a fully filled in leaf ref struct
184 * remove all the refs older than a given root generation
185 */
Zheng Yane4657682008-09-26 10:04:53 -0400186int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref,
187 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400188{
189 int ret = 0;
190 struct rb_node *rb;
Yan Zheng31153d82008-07-28 15:32:19 -0400191 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400192
Zheng Yane4657682008-09-26 10:04:53 -0400193 if (shared)
194 tree = &root->fs_info->shared_ref_tree;
195
Yan Zheng31153d82008-07-28 15:32:19 -0400196 spin_lock(&tree->lock);
Chris Mason017e5362008-07-28 15:32:51 -0400197 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -0400198 if (rb) {
199 ret = -EEXIST;
200 } else {
Yan Zheng31153d82008-07-28 15:32:19 -0400201 atomic_inc(&ref->usage);
Zheng Yane4657682008-09-26 10:04:53 -0400202 ref->tree = tree;
203 ref->in_tree = 1;
Chris Mason017e5362008-07-28 15:32:51 -0400204 list_add_tail(&ref->list, &tree->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400205 }
206 spin_unlock(&tree->lock);
207 return ret;
208}
209
Chris Masond352ac62008-09-29 15:18:18 -0400210/*
211 * remove a single leaf ref from the tree. This drops the ref held by the tree
212 * only
213 */
Yan Zheng31153d82008-07-28 15:32:19 -0400214int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
215{
Zheng Yane4657682008-09-26 10:04:53 -0400216 struct btrfs_leaf_ref_tree *tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400217
Zheng Yane4657682008-09-26 10:04:53 -0400218 if (!xchg(&ref->in_tree, 0))
219 return 0;
220
221 tree = ref->tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400222 spin_lock(&tree->lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400223
Yan Zheng31153d82008-07-28 15:32:19 -0400224 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400225 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400226
227 spin_unlock(&tree->lock);
228
Yanbcc63ab2008-07-30 16:29:20 -0400229 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400230 return 0;
231}