| Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2007 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 |  | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 19 | #include "ctree.h" | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 20 | #include "transaction.h" | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 21 | #include "disk-io.h" | 
 | 22 | #include "print-tree.h" | 
 | 23 |  | 
| Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 24 | /* | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 25 |  *  search forward for a root, starting with objectid 'search_start' | 
 | 26 |  *  if a root key is found, the objectid we find is filled into 'found_objectid' | 
 | 27 |  *  and 0 is returned.  < 0 is returned on error, 1 if there is nothing | 
 | 28 |  *  left in the tree. | 
| Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 29 |  */ | 
 | 30 | int btrfs_search_root(struct btrfs_root *root, u64 search_start, | 
 | 31 | 		      u64 *found_objectid) | 
 | 32 | { | 
 | 33 | 	struct btrfs_path *path; | 
 | 34 | 	struct btrfs_key search_key; | 
 | 35 | 	int ret; | 
 | 36 |  | 
 | 37 | 	root = root->fs_info->tree_root; | 
 | 38 | 	search_key.objectid = search_start; | 
 | 39 | 	search_key.type = (u8)-1; | 
 | 40 | 	search_key.offset = (u64)-1; | 
 | 41 |  | 
 | 42 | 	path = btrfs_alloc_path(); | 
 | 43 | 	BUG_ON(!path); | 
 | 44 | again: | 
 | 45 | 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | 
 | 46 | 	if (ret < 0) | 
 | 47 | 		goto out; | 
 | 48 | 	if (ret == 0) { | 
 | 49 | 		ret = 1; | 
 | 50 | 		goto out; | 
 | 51 | 	} | 
 | 52 | 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | 
 | 53 | 		ret = btrfs_next_leaf(root, path); | 
 | 54 | 		if (ret) | 
 | 55 | 			goto out; | 
 | 56 | 	} | 
 | 57 | 	btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]); | 
 | 58 | 	if (search_key.type != BTRFS_ROOT_ITEM_KEY) { | 
 | 59 | 		search_key.offset++; | 
 | 60 | 		btrfs_release_path(root, path); | 
 | 61 | 		goto again; | 
 | 62 | 	} | 
 | 63 | 	ret = 0; | 
 | 64 | 	*found_objectid = search_key.objectid; | 
 | 65 |  | 
 | 66 | out: | 
 | 67 | 	btrfs_free_path(path); | 
 | 68 | 	return ret; | 
 | 69 | } | 
 | 70 |  | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 71 | /* | 
 | 72 |  * lookup the root with the highest offset for a given objectid.  The key we do | 
 | 73 |  * find is copied into 'key'.  If we find something return 0, otherwise 1, < 0 | 
 | 74 |  * on error. | 
 | 75 |  */ | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 76 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, | 
 | 77 | 			struct btrfs_root_item *item, struct btrfs_key *key) | 
 | 78 | { | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 79 | 	struct btrfs_path *path; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 80 | 	struct btrfs_key search_key; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 81 | 	struct btrfs_key found_key; | 
 | 82 | 	struct extent_buffer *l; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 83 | 	int ret; | 
 | 84 | 	int slot; | 
 | 85 |  | 
 | 86 | 	search_key.objectid = objectid; | 
| Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 87 | 	search_key.type = BTRFS_ROOT_ITEM_KEY; | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 88 | 	search_key.offset = (u64)-1; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 89 |  | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 90 | 	path = btrfs_alloc_path(); | 
 | 91 | 	BUG_ON(!path); | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 92 | 	ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 93 | 	if (ret < 0) | 
 | 94 | 		goto out; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 95 |  | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 96 | 	BUG_ON(ret == 0); | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 97 | 	l = path->nodes[0]; | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 98 | 	BUG_ON(path->slots[0] == 0); | 
 | 99 | 	slot = path->slots[0] - 1; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 100 | 	btrfs_item_key_to_cpu(l, &found_key, slot); | 
 | 101 | 	if (found_key.objectid != objectid) { | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 102 | 		ret = 1; | 
 | 103 | 		goto out; | 
 | 104 | 	} | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 105 | 	read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot), | 
 | 106 | 			   sizeof(*item)); | 
 | 107 | 	memcpy(key, &found_key, sizeof(found_key)); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 108 | 	ret = 0; | 
 | 109 | out: | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 110 | 	btrfs_free_path(path); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 111 | 	return ret; | 
 | 112 | } | 
 | 113 |  | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 114 | /* | 
 | 115 |  * copy the data in 'item' into the btree | 
 | 116 |  */ | 
| Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 117 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root | 
 | 118 | 		      *root, struct btrfs_key *key, struct btrfs_root_item | 
 | 119 | 		      *item) | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 120 | { | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 121 | 	struct btrfs_path *path; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 122 | 	struct extent_buffer *l; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 123 | 	int ret; | 
 | 124 | 	int slot; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 125 | 	unsigned long ptr; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 126 |  | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 127 | 	path = btrfs_alloc_path(); | 
 | 128 | 	BUG_ON(!path); | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 129 | 	ret = btrfs_search_slot(trans, root, key, path, 0, 1); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 130 | 	if (ret < 0) | 
 | 131 | 		goto out; | 
| Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 132 |  | 
 | 133 | 	if (ret != 0) { | 
 | 134 | 		btrfs_print_leaf(root, path->nodes[0]); | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 135 | 		printk(KERN_CRIT "unable to update root key %llu %u %llu\n", | 
 | 136 | 		       (unsigned long long)key->objectid, key->type, | 
 | 137 | 		       (unsigned long long)key->offset); | 
| Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 138 | 		BUG_ON(1); | 
 | 139 | 	} | 
 | 140 |  | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 141 | 	l = path->nodes[0]; | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 142 | 	slot = path->slots[0]; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 143 | 	ptr = btrfs_item_ptr_offset(l, slot); | 
 | 144 | 	write_extent_buffer(l, item, ptr, sizeof(*item)); | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 145 | 	btrfs_mark_buffer_dirty(path->nodes[0]); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 146 | out: | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 147 | 	btrfs_release_path(root, path); | 
 | 148 | 	btrfs_free_path(path); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 149 | 	return ret; | 
 | 150 | } | 
 | 151 |  | 
| Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 152 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root | 
 | 153 | 		      *root, struct btrfs_key *key, struct btrfs_root_item | 
 | 154 | 		      *item) | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 155 | { | 
 | 156 | 	int ret; | 
| Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 157 | 	ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 158 | 	return ret; | 
 | 159 | } | 
 | 160 |  | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 161 | /* | 
 | 162 |  * at mount time we want to find all the old transaction snapshots that were in | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 163 |  * the process of being deleted if we crashed.  This is any root item with an | 
 | 164 |  * offset lower than the latest root.  They need to be queued for deletion to | 
 | 165 |  * finish what was happening when we crashed. | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 166 |  */ | 
| Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 167 | int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, | 
 | 168 | 			  struct btrfs_root *latest) | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 169 | { | 
 | 170 | 	struct btrfs_root *dead_root; | 
 | 171 | 	struct btrfs_item *item; | 
 | 172 | 	struct btrfs_root_item *ri; | 
 | 173 | 	struct btrfs_key key; | 
| Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 174 | 	struct btrfs_key found_key; | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 175 | 	struct btrfs_path *path; | 
 | 176 | 	int ret; | 
 | 177 | 	u32 nritems; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 178 | 	struct extent_buffer *leaf; | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 179 | 	int slot; | 
 | 180 |  | 
| Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 181 | 	key.objectid = objectid; | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 182 | 	btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | 
 | 183 | 	key.offset = 0; | 
 | 184 | 	path = btrfs_alloc_path(); | 
 | 185 | 	if (!path) | 
 | 186 | 		return -ENOMEM; | 
| Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 187 |  | 
 | 188 | again: | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 189 | 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 
 | 190 | 	if (ret < 0) | 
 | 191 | 		goto err; | 
| Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 192 | 	while (1) { | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 193 | 		leaf = path->nodes[0]; | 
 | 194 | 		nritems = btrfs_header_nritems(leaf); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 195 | 		slot = path->slots[0]; | 
 | 196 | 		if (slot >= nritems) { | 
 | 197 | 			ret = btrfs_next_leaf(root, path); | 
 | 198 | 			if (ret) | 
 | 199 | 				break; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 200 | 			leaf = path->nodes[0]; | 
 | 201 | 			nritems = btrfs_header_nritems(leaf); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 202 | 			slot = path->slots[0]; | 
 | 203 | 		} | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 204 | 		item = btrfs_item_nr(leaf, slot); | 
 | 205 | 		btrfs_item_key_to_cpu(leaf, &key, slot); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 206 | 		if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) | 
 | 207 | 			goto next; | 
| Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 208 |  | 
 | 209 | 		if (key.objectid < objectid) | 
 | 210 | 			goto next; | 
 | 211 |  | 
 | 212 | 		if (key.objectid > objectid) | 
 | 213 | 			break; | 
 | 214 |  | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 215 | 		ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 216 | 		if (btrfs_disk_root_refs(leaf, ri) != 0) | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 217 | 			goto next; | 
| Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 218 |  | 
| Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 219 | 		memcpy(&found_key, &key, sizeof(key)); | 
 | 220 | 		key.offset++; | 
 | 221 | 		btrfs_release_path(root, path); | 
| Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 222 | 		dead_root = | 
 | 223 | 			btrfs_read_fs_root_no_radix(root->fs_info->tree_root, | 
 | 224 | 						    &found_key); | 
| Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 225 | 		if (IS_ERR(dead_root)) { | 
 | 226 | 			ret = PTR_ERR(dead_root); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 227 | 			goto err; | 
 | 228 | 		} | 
| Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 229 |  | 
| Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 230 | 		if (objectid == BTRFS_TREE_RELOC_OBJECTID) | 
 | 231 | 			ret = btrfs_add_dead_reloc_root(dead_root); | 
 | 232 | 		else | 
 | 233 | 			ret = btrfs_add_dead_root(dead_root, latest); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 234 | 		if (ret) | 
 | 235 | 			goto err; | 
| Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 236 | 		goto again; | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 237 | next: | 
 | 238 | 		slot++; | 
 | 239 | 		path->slots[0]++; | 
 | 240 | 	} | 
 | 241 | 	ret = 0; | 
 | 242 | err: | 
 | 243 | 	btrfs_free_path(path); | 
 | 244 | 	return ret; | 
 | 245 | } | 
 | 246 |  | 
| Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 247 | /* drop the root item for 'key' from 'root' */ | 
| Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 248 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, | 
 | 249 | 		   struct btrfs_key *key) | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 250 | { | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 251 | 	struct btrfs_path *path; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 252 | 	int ret; | 
| Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 253 | 	u32 refs; | 
 | 254 | 	struct btrfs_root_item *ri; | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 255 | 	struct extent_buffer *leaf; | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 256 |  | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 257 | 	path = btrfs_alloc_path(); | 
 | 258 | 	BUG_ON(!path); | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 259 | 	ret = btrfs_search_slot(trans, root, key, path, -1, 1); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 260 | 	if (ret < 0) | 
 | 261 | 		goto out; | 
| Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 262 |  | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 263 | 	BUG_ON(ret != 0); | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 264 | 	leaf = path->nodes[0]; | 
 | 265 | 	ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); | 
| Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 266 |  | 
| Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 267 | 	refs = btrfs_disk_root_refs(leaf, ri); | 
| Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 268 | 	BUG_ON(refs != 0); | 
 | 269 | 	ret = btrfs_del_item(trans, root, path); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 270 | out: | 
| Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 271 | 	btrfs_release_path(root, path); | 
 | 272 | 	btrfs_free_path(path); | 
| Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 273 | 	return ret; | 
 | 274 | } | 
| Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 275 |  | 
| Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 276 | #if 0 /* this will get used when snapshot deletion is implemented */ | 
| Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 277 | int btrfs_del_root_ref(struct btrfs_trans_handle *trans, | 
 | 278 | 		       struct btrfs_root *tree_root, | 
 | 279 | 		       u64 root_id, u8 type, u64 ref_id) | 
 | 280 | { | 
 | 281 | 	struct btrfs_key key; | 
 | 282 | 	int ret; | 
 | 283 | 	struct btrfs_path *path; | 
 | 284 |  | 
 | 285 | 	path = btrfs_alloc_path(); | 
 | 286 |  | 
 | 287 | 	key.objectid = root_id; | 
 | 288 | 	key.type = type; | 
 | 289 | 	key.offset = ref_id; | 
 | 290 |  | 
 | 291 | 	ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); | 
 | 292 | 	BUG_ON(ret); | 
 | 293 |  | 
 | 294 | 	ret = btrfs_del_item(trans, tree_root, path); | 
 | 295 | 	BUG_ON(ret); | 
 | 296 |  | 
 | 297 | 	btrfs_free_path(path); | 
 | 298 | 	return ret; | 
 | 299 | } | 
| Christoph Hellwig | b295086 | 2008-12-02 09:54:17 -0500 | [diff] [blame] | 300 | #endif | 
| Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 301 |  | 
| Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 302 | int btrfs_find_root_ref(struct btrfs_root *tree_root, | 
 | 303 | 		   struct btrfs_path *path, | 
 | 304 | 		   u64 root_id, u64 ref_id) | 
 | 305 | { | 
 | 306 | 	struct btrfs_key key; | 
 | 307 | 	int ret; | 
 | 308 |  | 
 | 309 | 	key.objectid = root_id; | 
 | 310 | 	key.type = BTRFS_ROOT_REF_KEY; | 
 | 311 | 	key.offset = ref_id; | 
 | 312 |  | 
 | 313 | 	ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); | 
 | 314 | 	return ret; | 
 | 315 | } | 
 | 316 |  | 
 | 317 |  | 
| Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 318 | /* | 
 | 319 |  * add a btrfs_root_ref item.  type is either BTRFS_ROOT_REF_KEY | 
 | 320 |  * or BTRFS_ROOT_BACKREF_KEY. | 
 | 321 |  * | 
 | 322 |  * The dirid, sequence, name and name_len refer to the directory entry | 
 | 323 |  * that is referencing the root. | 
 | 324 |  * | 
 | 325 |  * For a forward ref, the root_id is the id of the tree referencing | 
 | 326 |  * the root and ref_id is the id of the subvol  or snapshot. | 
 | 327 |  * | 
 | 328 |  * For a back ref the root_id is the id of the subvol or snapshot and | 
 | 329 |  * ref_id is the id of the tree referencing it. | 
 | 330 |  */ | 
 | 331 | int btrfs_add_root_ref(struct btrfs_trans_handle *trans, | 
 | 332 | 		       struct btrfs_root *tree_root, | 
 | 333 | 		       u64 root_id, u8 type, u64 ref_id, | 
 | 334 | 		       u64 dirid, u64 sequence, | 
 | 335 | 		       const char *name, int name_len) | 
 | 336 | { | 
 | 337 | 	struct btrfs_key key; | 
 | 338 | 	int ret; | 
 | 339 | 	struct btrfs_path *path; | 
 | 340 | 	struct btrfs_root_ref *ref; | 
 | 341 | 	struct extent_buffer *leaf; | 
 | 342 | 	unsigned long ptr; | 
 | 343 |  | 
 | 344 |  | 
 | 345 | 	path = btrfs_alloc_path(); | 
 | 346 |  | 
 | 347 | 	key.objectid = root_id; | 
 | 348 | 	key.type = type; | 
 | 349 | 	key.offset = ref_id; | 
 | 350 |  | 
 | 351 | 	ret = btrfs_insert_empty_item(trans, tree_root, path, &key, | 
 | 352 | 				      sizeof(*ref) + name_len); | 
 | 353 | 	BUG_ON(ret); | 
 | 354 |  | 
 | 355 | 	leaf = path->nodes[0]; | 
 | 356 | 	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); | 
 | 357 | 	btrfs_set_root_ref_dirid(leaf, ref, dirid); | 
 | 358 | 	btrfs_set_root_ref_sequence(leaf, ref, sequence); | 
 | 359 | 	btrfs_set_root_ref_name_len(leaf, ref, name_len); | 
 | 360 | 	ptr = (unsigned long)(ref + 1); | 
 | 361 | 	write_extent_buffer(leaf, name, ptr, name_len); | 
 | 362 | 	btrfs_mark_buffer_dirty(leaf); | 
 | 363 |  | 
 | 364 | 	btrfs_free_path(path); | 
 | 365 | 	return ret; | 
 | 366 | } |