Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Chris Mason | bd56b30 | 2009-02-04 09:27:02 -0500 | [diff] [blame] | 21 | #include <linux/sort.h> |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "ref-cache.h" |
| 24 | #include "transaction.h" |
| 25 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 26 | /* |
| 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 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 36 | struct btrfs_leaf_ref *btrfs_alloc_leaf_ref(struct btrfs_root *root, |
| 37 | int nr_extents) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 38 | { |
| 39 | struct btrfs_leaf_ref *ref; |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 40 | size_t size = btrfs_leaf_ref_size(nr_extents); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 41 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 42 | ref = kmalloc(size, GFP_NOFS); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 43 | if (ref) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 44 | 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 Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 48 | memset(ref, 0, sizeof(*ref)); |
| 49 | atomic_set(&ref->usage, 1); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 50 | INIT_LIST_HEAD(&ref->list); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 51 | } |
| 52 | return ref; |
| 53 | } |
| 54 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 55 | /* |
| 56 | * free a leaf reference struct and update the counters for the |
| 57 | * total ref cache size |
| 58 | */ |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 59 | void btrfs_free_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 60 | { |
| 61 | if (!ref) |
| 62 | return; |
| 63 | WARN_ON(atomic_read(&ref->usage) == 0); |
| 64 | if (atomic_dec_and_test(&ref->usage)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 65 | size_t size = btrfs_leaf_ref_size(ref->nritems); |
| 66 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 67 | BUG_ON(ref->in_tree); |
| 68 | kfree(ref); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 69 | |
| 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 Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 76 | static struct rb_node *tree_insert(struct rb_root *root, u64 bytenr, |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 77 | struct rb_node *node) |
| 78 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 79 | struct rb_node **p = &root->rb_node; |
| 80 | struct rb_node *parent = NULL; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 81 | struct btrfs_leaf_ref *entry; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 82 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 83 | while (*p) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 84 | parent = *p; |
| 85 | entry = rb_entry(parent, struct btrfs_leaf_ref, rb_node); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 86 | |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 87 | if (bytenr < entry->bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 88 | p = &(*p)->rb_left; |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 89 | else if (bytenr > entry->bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 90 | p = &(*p)->rb_right; |
| 91 | else |
| 92 | return parent; |
| 93 | } |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 94 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 95 | entry = rb_entry(node, struct btrfs_leaf_ref, rb_node); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 96 | rb_link_node(node, parent, p); |
| 97 | rb_insert_color(node, root); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 101 | static struct rb_node *tree_search(struct rb_root *root, u64 bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 102 | { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 103 | struct rb_node *n = root->rb_node; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 104 | struct btrfs_leaf_ref *entry; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 105 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 106 | while (n) { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 107 | entry = rb_entry(n, struct btrfs_leaf_ref, rb_node); |
| 108 | WARN_ON(!entry->in_tree); |
| 109 | |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 110 | if (bytenr < entry->bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 111 | n = n->rb_left; |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 112 | else if (bytenr > entry->bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 113 | n = n->rb_right; |
| 114 | else |
| 115 | return n; |
| 116 | } |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 120 | int btrfs_remove_leaf_refs(struct btrfs_root *root, u64 max_root_gen, |
| 121 | int shared) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 122 | { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 123 | struct btrfs_leaf_ref *ref = NULL; |
| 124 | struct btrfs_leaf_ref_tree *tree = root->ref_tree; |
| 125 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 126 | if (shared) |
| 127 | tree = &root->fs_info->shared_ref_tree; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 128 | if (!tree) |
| 129 | return 0; |
| 130 | |
| 131 | spin_lock(&tree->lock); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 132 | while (!list_empty(&tree->list)) { |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 133 | ref = list_entry(tree->list.next, struct btrfs_leaf_ref, list); |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 134 | BUG_ON(ref->tree != tree); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 135 | if (ref->root_gen > max_root_gen) |
| 136 | break; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 137 | if (!xchg(&ref->in_tree, 0)) { |
| 138 | cond_resched_lock(&tree->lock); |
| 139 | continue; |
| 140 | } |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 141 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 142 | rb_erase(&ref->rb_node, &tree->root); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 143 | list_del_init(&ref->list); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 144 | |
| 145 | spin_unlock(&tree->lock); |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 146 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 147 | cond_resched(); |
| 148 | spin_lock(&tree->lock); |
| 149 | } |
| 150 | spin_unlock(&tree->lock); |
| 151 | return 0; |
| 152 | } |
| 153 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 154 | /* |
| 155 | * find the leaf ref for a given extent. This returns the ref struct with |
| 156 | * a usage reference incremented |
| 157 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 158 | struct btrfs_leaf_ref *btrfs_lookup_leaf_ref(struct btrfs_root *root, |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 159 | u64 bytenr) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 160 | { |
| 161 | struct rb_node *rb; |
| 162 | struct btrfs_leaf_ref *ref = NULL; |
| 163 | struct btrfs_leaf_ref_tree *tree = root->ref_tree; |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 164 | again: |
| 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 Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 183 | /* |
| 184 | * add a fully filled in leaf ref struct |
| 185 | * remove all the refs older than a given root generation |
| 186 | */ |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 187 | int btrfs_add_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref, |
| 188 | int shared) |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 189 | { |
| 190 | int ret = 0; |
| 191 | struct rb_node *rb; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 192 | struct btrfs_leaf_ref_tree *tree = root->ref_tree; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 193 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 194 | if (shared) |
| 195 | tree = &root->fs_info->shared_ref_tree; |
| 196 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 197 | spin_lock(&tree->lock); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 198 | rb = tree_insert(&tree->root, ref->bytenr, &ref->rb_node); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 199 | if (rb) { |
| 200 | ret = -EEXIST; |
| 201 | } else { |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 202 | atomic_inc(&ref->usage); |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 203 | ref->tree = tree; |
| 204 | ref->in_tree = 1; |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 205 | list_add_tail(&ref->list, &tree->list); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 206 | } |
| 207 | spin_unlock(&tree->lock); |
| 208 | return ret; |
| 209 | } |
| 210 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 211 | /* |
| 212 | * remove a single leaf ref from the tree. This drops the ref held by the tree |
| 213 | * only |
| 214 | */ |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 215 | int btrfs_remove_leaf_ref(struct btrfs_root *root, struct btrfs_leaf_ref *ref) |
| 216 | { |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 217 | struct btrfs_leaf_ref_tree *tree; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 218 | |
Zheng Yan | e465768 | 2008-09-26 10:04:53 -0400 | [diff] [blame] | 219 | if (!xchg(&ref->in_tree, 0)) |
| 220 | return 0; |
| 221 | |
| 222 | tree = ref->tree; |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 223 | spin_lock(&tree->lock); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 224 | |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 225 | rb_erase(&ref->rb_node, &tree->root); |
Chris Mason | 017e536 | 2008-07-28 15:32:51 -0400 | [diff] [blame] | 226 | list_del_init(&ref->list); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 227 | |
| 228 | spin_unlock(&tree->lock); |
| 229 | |
Yan | bcc63ab | 2008-07-30 16:29:20 -0400 | [diff] [blame] | 230 | btrfs_free_leaf_ref(root, ref); |
Yan Zheng | 31153d8 | 2008-07-28 15:32:19 -0400 | [diff] [blame] | 231 | return 0; |
| 232 | } |