blob: 272b9890c9826d07a0564e7fe0d7c27bb402b81e [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>
20#include "ctree.h"
21#include "ref-cache.h"
22#include "transaction.h"
23
Yanbcc63ab2008-07-30 16:29:20 -040024struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
25 int nr_extents)
Yan Zheng31153d82008-07-28 15:32:19 -040026{
27 struct btrfs_leaf_ref *ref;
Yanbcc63ab2008-07-30 16:29:20 -040028 size_t size = btrfs_leaf_ref_size(nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -040029
Yanbcc63ab2008-07-30 16:29:20 -040030 ref = kmalloc(size, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -040031 if (ref) {
Yanbcc63ab2008-07-30 16:29:20 -040032 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
Yan Zheng31153d82008-07-28 15:32:19 -040036 memset(ref, 0, sizeof(*ref));
37 atomic_set(&ref->usage, 1);
Chris Mason017e5362008-07-28 15:32:51 -040038 INIT_LIST_HEAD(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -040039 }
40 return ref;
41}
42
Yanbcc63ab2008-07-30 16:29:20 -040043void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -040044{
45 if (!ref)
46 return;
47 WARN_ON(atomic_read(&ref->usage) == 0);
48 if (atomic_dec_and_test(&ref->usage)) {
Yanbcc63ab2008-07-30 16:29:20 -040049 size_t size = btrfs_leaf_ref_size(ref->nritems);
50
Yan Zheng31153d82008-07-28 15:32:19 -040051 BUG_ON(ref->in_tree);
52 kfree(ref);
Yanbcc63ab2008-07-30 16:29:20 -040053
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);
Yan Zheng31153d82008-07-28 15:32:19 -040057 }
58}
59
Chris Mason017e5362008-07-28 15:32:51 -040060static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
Yan Zheng31153d82008-07-28 15:32:19 -040061 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;
Yan Zheng31153d82008-07-28 15:32:19 -040066
67 while(*p) {
68 parent = *p;
69 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
70 WARN_ON(!entry->in_tree);
71
Chris Mason017e5362008-07-28 15:32:51 -040072 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040073 p = &(*p)->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -040074 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040075 p = &(*p)->rb_right;
76 else
77 return parent;
78 }
Yanbcc63ab2008-07-30 16:29:20 -040079
Yan Zheng31153d82008-07-28 15:32:19 -040080 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
81 entry->in_tree = 1;
82 rb_link_node(node, parent, p);
83 rb_insert_color(node, root);
84 return NULL;
85}
86
Chris Mason017e5362008-07-28 15:32:51 -040087static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040088{
89 struct rb_node * n = root->rb_node;
90 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -040091
92 while(n) {
93 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
94 WARN_ON(!entry->in_tree);
95
Chris Mason017e5362008-07-28 15:32:51 -040096 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040097 n = n->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -040098 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040099 n = n->rb_right;
100 else
101 return n;
102 }
103 return NULL;
104}
105
Yanbcc63ab2008-07-30 16:29:20 -0400106int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen)
Yan Zheng31153d82008-07-28 15:32:19 -0400107{
Yan Zheng31153d82008-07-28 15:32:19 -0400108 struct btrfs_leaf_ref *ref = NULL;
109 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
110
111 if (!tree)
112 return 0;
113
114 spin_lock(&tree->lock);
Yanbcc63ab2008-07-30 16:29:20 -0400115 while(!list_empty(&tree->list)) {
116 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
117 BUG_ON(!ref->in_tree);
118 if (ref->root_gen > max_root_gen)
119 break;
120
Yan Zheng31153d82008-07-28 15:32:19 -0400121 rb_erase(&ref->rb_node, &tree->root);
122 ref->in_tree = 0;
Chris Mason017e5362008-07-28 15:32:51 -0400123 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400124
125 spin_unlock(&tree->lock);
Yanbcc63ab2008-07-30 16:29:20 -0400126 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400127 cond_resched();
128 spin_lock(&tree->lock);
129 }
130 spin_unlock(&tree->lock);
131 return 0;
132}
133
134struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
Chris Mason017e5362008-07-28 15:32:51 -0400135 u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400136{
137 struct rb_node *rb;
138 struct btrfs_leaf_ref *ref = NULL;
139 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
140
141 if (!tree)
142 return NULL;
143
144 spin_lock(&tree->lock);
Chris Mason017e5362008-07-28 15:32:51 -0400145 rb = tree_search(&tree->root, bytenr);
146 if (rb)
147 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -0400148 if (ref)
149 atomic_inc(&ref->usage);
150 spin_unlock(&tree->lock);
151 return ref;
152}
153
154int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
155{
156 int ret = 0;
157 struct rb_node *rb;
Yan Zheng31153d82008-07-28 15:32:19 -0400158 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400159
160 spin_lock(&tree->lock);
Chris Mason017e5362008-07-28 15:32:51 -0400161 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -0400162 if (rb) {
163 ret = -EEXIST;
164 } else {
Yan Zheng31153d82008-07-28 15:32:19 -0400165 atomic_inc(&ref->usage);
Chris Mason017e5362008-07-28 15:32:51 -0400166 list_add_tail(&ref->list, &tree->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400167 }
168 spin_unlock(&tree->lock);
169 return ret;
170}
171
172int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
173{
Yan Zheng31153d82008-07-28 15:32:19 -0400174 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400175
176 BUG_ON(!ref->in_tree);
177 spin_lock(&tree->lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400178
Yan Zheng31153d82008-07-28 15:32:19 -0400179 rb_erase(&ref->rb_node, &tree->root);
180 ref->in_tree = 0;
Chris Mason017e5362008-07-28 15:32:51 -0400181 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400182
183 spin_unlock(&tree->lock);
184
Yanbcc63ab2008-07-30 16:29:20 -0400185 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400186 return 0;
187}