blob: 6f0acc4c9eab7675dd9cacc0804bc530f3db46d3 [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
Chris Masond352ac62008-09-29 15:18:18 -040024/*
25 * leaf refs are used to cache the information about which extents
26 * a given leaf has references on. This allows us to process that leaf
27 * in btrfs_drop_snapshot without needing to read it back from disk.
28 */
29
30/*
31 * kmalloc a leaf reference struct and update the counters for the
32 * total ref cache size
33 */
Yanbcc63ab2008-07-30 16:29:20 -040034struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
35 int nr_extents)
Yan Zheng31153d82008-07-28 15:32:19 -040036{
37 struct btrfs_leaf_ref *ref;
Yanbcc63ab2008-07-30 16:29:20 -040038 size_t size = btrfs_leaf_ref_size(nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -040039
Yanbcc63ab2008-07-30 16:29:20 -040040 ref = kmalloc(size, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -040041 if (ref) {
Yanbcc63ab2008-07-30 16:29:20 -040042 spin_lock(&root->fs_info->ref_cache_lock);
43 root->fs_info->total_ref_cache_size += size;
44 spin_unlock(&root->fs_info->ref_cache_lock);
45
Yan Zheng31153d82008-07-28 15:32:19 -040046 memset(ref, 0, sizeof(*ref));
47 atomic_set(&ref->usage, 1);
Chris Mason017e5362008-07-28 15:32:51 -040048 INIT_LIST_HEAD(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -040049 }
50 return ref;
51}
52
Chris Masond352ac62008-09-29 15:18:18 -040053/*
54 * free a leaf reference struct and update the counters for the
55 * total ref cache size
56 */
Yanbcc63ab2008-07-30 16:29:20 -040057void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -040058{
59 if (!ref)
60 return;
61 WARN_ON(atomic_read(&ref->usage) == 0);
62 if (atomic_dec_and_test(&ref->usage)) {
Yanbcc63ab2008-07-30 16:29:20 -040063 size_t size = btrfs_leaf_ref_size(ref->nritems);
64
Yan Zheng31153d82008-07-28 15:32:19 -040065 BUG_ON(ref->in_tree);
66 kfree(ref);
Yanbcc63ab2008-07-30 16:29:20 -040067
68 spin_lock(&root->fs_info->ref_cache_lock);
69 root->fs_info->total_ref_cache_size -= size;
70 spin_unlock(&root->fs_info->ref_cache_lock);
Yan Zheng31153d82008-07-28 15:32:19 -040071 }
72}
73
Chris Mason017e5362008-07-28 15:32:51 -040074static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
Yan Zheng31153d82008-07-28 15:32:19 -040075 struct rb_node *node)
76{
Chris Masond3977122009-01-05 21:25:51 -050077 struct rb_node **p = &root->rb_node;
78 struct rb_node *parent = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -040079 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -040080
Chris Masond3977122009-01-05 21:25:51 -050081 while (*p) {
Yan Zheng31153d82008-07-28 15:32:19 -040082 parent = *p;
83 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040084
Chris Mason017e5362008-07-28 15:32:51 -040085 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040086 p = &(*p)->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -040087 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040088 p = &(*p)->rb_right;
89 else
90 return parent;
91 }
Yanbcc63ab2008-07-30 16:29:20 -040092
Yan Zheng31153d82008-07-28 15:32:19 -040093 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040094 rb_link_node(node, parent, p);
95 rb_insert_color(node, root);
96 return NULL;
97}
98
Chris Mason017e5362008-07-28 15:32:51 -040099static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400100{
Chris Masond3977122009-01-05 21:25:51 -0500101 struct rb_node *n = root->rb_node;
Yan Zheng31153d82008-07-28 15:32:19 -0400102 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -0400103
Chris Masond3977122009-01-05 21:25:51 -0500104 while (n) {
Yan Zheng31153d82008-07-28 15:32:19 -0400105 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
106 WARN_ON(!entry->in_tree);
107
Chris Mason017e5362008-07-28 15:32:51 -0400108 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400109 n = n->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -0400110 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400111 n = n->rb_right;
112 else
113 return n;
114 }
115 return NULL;
116}
117
Zheng Yane4657682008-09-26 10:04:53 -0400118int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen,
119 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400120{
Yan Zheng31153d82008-07-28 15:32:19 -0400121 struct btrfs_leaf_ref *ref = NULL;
122 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
123
Zheng Yane4657682008-09-26 10:04:53 -0400124 if (shared)
125 tree = &root->fs_info->shared_ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400126 if (!tree)
127 return 0;
128
129 spin_lock(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500130 while (!list_empty(&tree->list)) {
Yanbcc63ab2008-07-30 16:29:20 -0400131 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
Zheng Yane4657682008-09-26 10:04:53 -0400132 BUG_ON(ref->tree != tree);
Yanbcc63ab2008-07-30 16:29:20 -0400133 if (ref->root_gen > max_root_gen)
134 break;
Zheng Yane4657682008-09-26 10:04:53 -0400135 if (!xchg(&ref->in_tree, 0)) {
136 cond_resched_lock(&tree->lock);
137 continue;
138 }
Yanbcc63ab2008-07-30 16:29:20 -0400139
Yan Zheng31153d82008-07-28 15:32:19 -0400140 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400141 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400142
143 spin_unlock(&tree->lock);
Yanbcc63ab2008-07-30 16:29:20 -0400144 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400145 cond_resched();
146 spin_lock(&tree->lock);
147 }
148 spin_unlock(&tree->lock);
149 return 0;
150}
151
Chris Masond352ac62008-09-29 15:18:18 -0400152/*
153 * find the leaf ref for a given extent. This returns the ref struct with
154 * a usage reference incremented
155 */
Yan Zheng31153d82008-07-28 15:32:19 -0400156struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
Chris Mason017e5362008-07-28 15:32:51 -0400157 u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400158{
159 struct rb_node *rb;
160 struct btrfs_leaf_ref *ref = NULL;
161 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Zheng Yane4657682008-09-26 10:04:53 -0400162again:
163 if (tree) {
164 spin_lock(&tree->lock);
165 rb = tree_search(&tree->root, bytenr);
166 if (rb)
167 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
168 if (ref)
169 atomic_inc(&ref->usage);
170 spin_unlock(&tree->lock);
171 if (ref)
172 return ref;
173 }
174 if (tree != &root->fs_info->shared_ref_tree) {
175 tree = &root->fs_info->shared_ref_tree;
176 goto again;
177 }
178 return NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400179}
180
Chris Masond352ac62008-09-29 15:18:18 -0400181/*
182 * add a fully filled in leaf ref struct
183 * remove all the refs older than a given root generation
184 */
Zheng Yane4657682008-09-26 10:04:53 -0400185int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref,
186 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400187{
188 int ret = 0;
189 struct rb_node *rb;
Yan Zheng31153d82008-07-28 15:32:19 -0400190 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400191
Zheng Yane4657682008-09-26 10:04:53 -0400192 if (shared)
193 tree = &root->fs_info->shared_ref_tree;
194
Yan Zheng31153d82008-07-28 15:32:19 -0400195 spin_lock(&tree->lock);
Chris Mason017e5362008-07-28 15:32:51 -0400196 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -0400197 if (rb) {
198 ret = -EEXIST;
199 } else {
Yan Zheng31153d82008-07-28 15:32:19 -0400200 atomic_inc(&ref->usage);
Zheng Yane4657682008-09-26 10:04:53 -0400201 ref->tree = tree;
202 ref->in_tree = 1;
Chris Mason017e5362008-07-28 15:32:51 -0400203 list_add_tail(&ref->list, &tree->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400204 }
205 spin_unlock(&tree->lock);
206 return ret;
207}
208
Chris Masond352ac62008-09-29 15:18:18 -0400209/*
210 * remove a single leaf ref from the tree. This drops the ref held by the tree
211 * only
212 */
Yan Zheng31153d82008-07-28 15:32:19 -0400213int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
214{
Zheng Yane4657682008-09-26 10:04:53 -0400215 struct btrfs_leaf_ref_tree *tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400216
Zheng Yane4657682008-09-26 10:04:53 -0400217 if (!xchg(&ref->in_tree, 0))
218 return 0;
219
220 tree = ref->tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400221 spin_lock(&tree->lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400222
Yan Zheng31153d82008-07-28 15:32:19 -0400223 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400224 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400225
226 spin_unlock(&tree->lock);
227
Yanbcc63ab2008-07-30 16:29:20 -0400228 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400229 return 0;
230}