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