blob: a97314cf6bd6ef7ac44aa60a4485261a74e241a7 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Masonbd56b302009-02-04 09:27:02 -050021#include <linux/sort.h>
Yan Zheng31153d82008-07-28 15:32:19 -040022#include "ctree.h"
23#include "ref-cache.h"
24#include "transaction.h"
25
Chris Masond352ac62008-09-29 15:18:18 -040026/*
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.
30 */
31
32/*
33 * kmalloc a leaf reference struct and update the counters for the
34 * total ref cache size
35 */
Yanbcc63ab2008-07-30 16:29:20 -040036struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root,
37 int nr_extents)
Yan Zheng31153d82008-07-28 15:32:19 -040038{
39 struct btrfs_leaf_ref *ref;
Yanbcc63ab2008-07-30 16:29:20 -040040 size_t size = btrfs_leaf_ref_size(nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -040041
Yanbcc63ab2008-07-30 16:29:20 -040042 ref = kmalloc(size, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -040043 if (ref) {
Yanbcc63ab2008-07-30 16:29:20 -040044 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);
47
Yan Zheng31153d82008-07-28 15:32:19 -040048 memset(ref, 0, sizeof(*ref));
49 atomic_set(&ref->usage, 1);
Chris Mason017e5362008-07-28 15:32:51 -040050 INIT_LIST_HEAD(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -040051 }
52 return ref;
53}
54
Chris Masond352ac62008-09-29 15:18:18 -040055/*
56 * free a leaf reference struct and update the counters for the
57 * total ref cache size
58 */
Yanbcc63ab2008-07-30 16:29:20 -040059void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -040060{
61 if (!ref)
62 return;
63 WARN_ON(atomic_read(&ref->usage) == 0);
64 if (atomic_dec_and_test(&ref->usage)) {
Yanbcc63ab2008-07-30 16:29:20 -040065 size_t size = btrfs_leaf_ref_size(ref->nritems);
66
Yan Zheng31153d82008-07-28 15:32:19 -040067 BUG_ON(ref->in_tree);
68 kfree(ref);
Yanbcc63ab2008-07-30 16:29:20 -040069
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);
Yan Zheng31153d82008-07-28 15:32:19 -040073 }
74}
75
Chris Mason017e5362008-07-28 15:32:51 -040076static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr,
Yan Zheng31153d82008-07-28 15:32:19 -040077 struct rb_node *node)
78{
Chris Masond3977122009-01-05 21:25:51 -050079 struct rb_node **p = &root->rb_node;
80 struct rb_node *parent = NULL;
Yan Zheng31153d82008-07-28 15:32:19 -040081 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -040082
Chris Masond3977122009-01-05 21:25:51 -050083 while (*p) {
Yan Zheng31153d82008-07-28 15:32:19 -040084 parent = *p;
85 entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040086
Chris Mason017e5362008-07-28 15:32:51 -040087 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040088 p = &(*p)->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -040089 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -040090 p = &(*p)->rb_right;
91 else
92 return parent;
93 }
Yanbcc63ab2008-07-30 16:29:20 -040094
Yan Zheng31153d82008-07-28 15:32:19 -040095 entry = rb_entry(node, struct btrfs_leaf_ref, rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -040096 rb_link_node(node, parent, p);
97 rb_insert_color(node, root);
98 return NULL;
99}
100
Chris Mason017e5362008-07-28 15:32:51 -0400101static struct rb_node *tree_search(struct rb_root *root, u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400102{
Chris Masond3977122009-01-05 21:25:51 -0500103 struct rb_node *n = root->rb_node;
Yan Zheng31153d82008-07-28 15:32:19 -0400104 struct btrfs_leaf_ref *entry;
Yan Zheng31153d82008-07-28 15:32:19 -0400105
Chris Masond3977122009-01-05 21:25:51 -0500106 while (n) {
Yan Zheng31153d82008-07-28 15:32:19 -0400107 entry = rb_entry(n, struct btrfs_leaf_ref, rb_node);
108 WARN_ON(!entry->in_tree);
109
Chris Mason017e5362008-07-28 15:32:51 -0400110 if (bytenr < entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400111 n = n->rb_left;
Chris Mason017e5362008-07-28 15:32:51 -0400112 else if (bytenr > entry->bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400113 n = n->rb_right;
114 else
115 return n;
116 }
117 return NULL;
118}
119
Zheng Yane4657682008-09-26 10:04:53 -0400120int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen,
121 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400122{
Yan Zheng31153d82008-07-28 15:32:19 -0400123 struct btrfs_leaf_ref *ref = NULL;
124 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
125
Zheng Yane4657682008-09-26 10:04:53 -0400126 if (shared)
127 tree = &root->fs_info->shared_ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400128 if (!tree)
129 return 0;
130
131 spin_lock(&tree->lock);
Chris Masond3977122009-01-05 21:25:51 -0500132 while (!list_empty(&tree->list)) {
Yanbcc63ab2008-07-30 16:29:20 -0400133 ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list);
Zheng Yane4657682008-09-26 10:04:53 -0400134 BUG_ON(ref->tree != tree);
Yanbcc63ab2008-07-30 16:29:20 -0400135 if (ref->root_gen > max_root_gen)
136 break;
Zheng Yane4657682008-09-26 10:04:53 -0400137 if (!xchg(&ref->in_tree, 0)) {
138 cond_resched_lock(&tree->lock);
139 continue;
140 }
Yanbcc63ab2008-07-30 16:29:20 -0400141
Yan Zheng31153d82008-07-28 15:32:19 -0400142 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400143 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400144
145 spin_unlock(&tree->lock);
Yanbcc63ab2008-07-30 16:29:20 -0400146 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400147 cond_resched();
148 spin_lock(&tree->lock);
149 }
150 spin_unlock(&tree->lock);
151 return 0;
152}
153
Chris Masond352ac62008-09-29 15:18:18 -0400154/*
155 * find the leaf ref for a given extent. This returns the ref struct with
156 * a usage reference incremented
157 */
Yan Zheng31153d82008-07-28 15:32:19 -0400158struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root,
Chris Mason017e5362008-07-28 15:32:51 -0400159 u64 bytenr)
Yan Zheng31153d82008-07-28 15:32:19 -0400160{
161 struct rb_node *rb;
162 struct btrfs_leaf_ref *ref = NULL;
163 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Zheng Yane4657682008-09-26 10:04:53 -0400164again:
165 if (tree) {
166 spin_lock(&tree->lock);
167 rb = tree_search(&tree->root, bytenr);
168 if (rb)
169 ref = rb_entry(rb, struct btrfs_leaf_ref, rb_node);
170 if (ref)
171 atomic_inc(&ref->usage);
172 spin_unlock(&tree->lock);
173 if (ref)
174 return ref;
175 }
176 if (tree != &root->fs_info->shared_ref_tree) {
177 tree = &root->fs_info->shared_ref_tree;
178 goto again;
179 }
180 return NULL;
Yan Zheng31153d82008-07-28 15:32:19 -0400181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/*
184 * add a fully filled in leaf ref struct
185 * remove all the refs older than a given root generation
186 */
Zheng Yane4657682008-09-26 10:04:53 -0400187int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref,
188 int shared)
Yan Zheng31153d82008-07-28 15:32:19 -0400189{
190 int ret = 0;
191 struct rb_node *rb;
Yan Zheng31153d82008-07-28 15:32:19 -0400192 struct btrfs_leaf_ref_tree *tree = root->ref_tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400193
Zheng Yane4657682008-09-26 10:04:53 -0400194 if (shared)
195 tree = &root->fs_info->shared_ref_tree;
196
Yan Zheng31153d82008-07-28 15:32:19 -0400197 spin_lock(&tree->lock);
Chris Mason017e5362008-07-28 15:32:51 -0400198 rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node);
Yan Zheng31153d82008-07-28 15:32:19 -0400199 if (rb) {
200 ret = -EEXIST;
201 } else {
Yan Zheng31153d82008-07-28 15:32:19 -0400202 atomic_inc(&ref->usage);
Zheng Yane4657682008-09-26 10:04:53 -0400203 ref->tree = tree;
204 ref->in_tree = 1;
Chris Mason017e5362008-07-28 15:32:51 -0400205 list_add_tail(&ref->list, &tree->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400206 }
207 spin_unlock(&tree->lock);
208 return ret;
209}
210
Chris Masond352ac62008-09-29 15:18:18 -0400211/*
212 * remove a single leaf ref from the tree. This drops the ref held by the tree
213 * only
214 */
Yan Zheng31153d82008-07-28 15:32:19 -0400215int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref)
216{
Zheng Yane4657682008-09-26 10:04:53 -0400217 struct btrfs_leaf_ref_tree *tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400218
Zheng Yane4657682008-09-26 10:04:53 -0400219 if (!xchg(&ref->in_tree, 0))
220 return 0;
221
222 tree = ref->tree;
Yan Zheng31153d82008-07-28 15:32:19 -0400223 spin_lock(&tree->lock);
Yan Zheng31153d82008-07-28 15:32:19 -0400224
Yan Zheng31153d82008-07-28 15:32:19 -0400225 rb_erase(&ref->rb_node, &tree->root);
Chris Mason017e5362008-07-28 15:32:51 -0400226 list_del_init(&ref->list);
Yan Zheng31153d82008-07-28 15:32:19 -0400227
228 spin_unlock(&tree->lock);
229
Yanbcc63ab2008-07-30 16:29:20 -0400230 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -0400231 return 0;
232}