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); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 97 | if (path->slots[0] == 0) { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 98 | ret = 1; |
| 99 | goto out; |
| 100 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 101 | l = path->nodes[0]; |
| 102 | slot = path->slots[0] - 1; |
| 103 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 104 | if (found_key.objectid != objectid || |
| 105 | found_key.type != BTRFS_ROOT_ITEM_KEY) { |
| 106 | ret = 1; |
| 107 | goto out; |
| 108 | } |
| 109 | if (item) |
| 110 | read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot), |
| 111 | sizeof(*item)); |
| 112 | if (key) |
| 113 | memcpy(key, &found_key, sizeof(found_key)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 114 | ret = 0; |
| 115 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 116 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 117 | return ret; |
| 118 | } |
| 119 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 120 | int btrfs_set_root_node(struct btrfs_root_item *item, |
| 121 | struct extent_buffer *node) |
| 122 | { |
| 123 | btrfs_set_root_bytenr(item, node->start); |
| 124 | btrfs_set_root_level(item, btrfs_header_level(node)); |
| 125 | btrfs_set_root_generation(item, btrfs_header_generation(node)); |
| 126 | return 0; |
| 127 | } |
| 128 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 129 | /* |
| 130 | * copy the data in 'item' into the btree |
| 131 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 132 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 133 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 134 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 135 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 136 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 137 | struct extent_buffer *l; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 138 | int ret; |
| 139 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 140 | unsigned long ptr; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 141 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 142 | path = btrfs_alloc_path(); |
| 143 | BUG_ON(!path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 144 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 145 | if (ret < 0) |
| 146 | goto out; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 147 | |
| 148 | if (ret != 0) { |
| 149 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 150 | printk(KERN_CRIT "unable to update root key %llu %u %llu\n", |
| 151 | (unsigned long long)key->objectid, key->type, |
| 152 | (unsigned long long)key->offset); |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 153 | BUG_ON(1); |
| 154 | } |
| 155 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 156 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 157 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 158 | ptr = btrfs_item_ptr_offset(l, slot); |
| 159 | write_extent_buffer(l, item, ptr, sizeof(*item)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 160 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 161 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 162 | btrfs_release_path(root, path); |
| 163 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 167 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 168 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 169 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 170 | { |
| 171 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 172 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 173 | return ret; |
| 174 | } |
| 175 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 176 | /* |
| 177 | * 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] | 178 | * the process of being deleted if we crashed. This is any root item with an |
| 179 | * offset lower than the latest root. They need to be queued for deletion to |
| 180 | * finish what was happening when we crashed. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 181 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 182 | int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 183 | { |
| 184 | struct btrfs_root *dead_root; |
| 185 | struct btrfs_item *item; |
| 186 | struct btrfs_root_item *ri; |
| 187 | struct btrfs_key key; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 188 | struct btrfs_key found_key; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 189 | struct btrfs_path *path; |
| 190 | int ret; |
| 191 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 192 | struct extent_buffer *leaf; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 193 | int slot; |
| 194 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 195 | key.objectid = objectid; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 196 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 197 | key.offset = 0; |
| 198 | path = btrfs_alloc_path(); |
| 199 | if (!path) |
| 200 | return -ENOMEM; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 201 | |
| 202 | again: |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 203 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 204 | if (ret < 0) |
| 205 | goto err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 206 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 207 | leaf = path->nodes[0]; |
| 208 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 209 | slot = path->slots[0]; |
| 210 | if (slot >= nritems) { |
| 211 | ret = btrfs_next_leaf(root, path); |
| 212 | if (ret) |
| 213 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 214 | leaf = path->nodes[0]; |
| 215 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 216 | slot = path->slots[0]; |
| 217 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 218 | item = btrfs_item_nr(leaf, slot); |
| 219 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 220 | if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) |
| 221 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 222 | |
| 223 | if (key.objectid < objectid) |
| 224 | goto next; |
| 225 | |
| 226 | if (key.objectid > objectid) |
| 227 | break; |
| 228 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 229 | ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 230 | if (btrfs_disk_root_refs(leaf, ri) != 0) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 231 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 232 | |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 233 | memcpy(&found_key, &key, sizeof(key)); |
| 234 | key.offset++; |
| 235 | btrfs_release_path(root, path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 236 | dead_root = |
| 237 | btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 238 | &found_key); |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 239 | if (IS_ERR(dead_root)) { |
| 240 | ret = PTR_ERR(dead_root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 241 | goto err; |
| 242 | } |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 243 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 244 | ret = btrfs_add_dead_root(dead_root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 245 | if (ret) |
| 246 | goto err; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 247 | goto again; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 248 | next: |
| 249 | slot++; |
| 250 | path->slots[0]++; |
| 251 | } |
| 252 | ret = 0; |
| 253 | err: |
| 254 | btrfs_free_path(path); |
| 255 | return ret; |
| 256 | } |
| 257 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 258 | int btrfs_find_orphan_roots(struct btrfs_root *tree_root) |
| 259 | { |
| 260 | struct extent_buffer *leaf; |
| 261 | struct btrfs_path *path; |
| 262 | struct btrfs_key key; |
| 263 | int err = 0; |
| 264 | int ret; |
| 265 | |
| 266 | path = btrfs_alloc_path(); |
| 267 | if (!path) |
| 268 | return -ENOMEM; |
| 269 | |
| 270 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 271 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 272 | key.offset = 0; |
| 273 | |
| 274 | while (1) { |
| 275 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); |
| 276 | if (ret < 0) { |
| 277 | err = ret; |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | leaf = path->nodes[0]; |
| 282 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 283 | ret = btrfs_next_leaf(tree_root, path); |
| 284 | if (ret < 0) |
| 285 | err = ret; |
| 286 | if (ret != 0) |
| 287 | break; |
| 288 | leaf = path->nodes[0]; |
| 289 | } |
| 290 | |
| 291 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 292 | btrfs_release_path(tree_root, path); |
| 293 | |
| 294 | if (key.objectid != BTRFS_ORPHAN_OBJECTID || |
| 295 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 296 | break; |
| 297 | |
| 298 | ret = btrfs_find_dead_roots(tree_root, key.offset); |
| 299 | if (ret) { |
| 300 | err = ret; |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | key.offset++; |
| 305 | } |
| 306 | |
| 307 | btrfs_free_path(path); |
| 308 | return err; |
| 309 | } |
| 310 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 311 | /* drop the root item for 'key' from 'root' */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 312 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 313 | struct btrfs_key *key) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 314 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 315 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 316 | int ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 317 | u32 refs; |
| 318 | struct btrfs_root_item *ri; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 319 | struct extent_buffer *leaf; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 320 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 321 | path = btrfs_alloc_path(); |
| 322 | BUG_ON(!path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 323 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 324 | if (ret < 0) |
| 325 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 326 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 327 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 328 | leaf = path->nodes[0]; |
| 329 | ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 330 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 331 | refs = btrfs_disk_root_refs(leaf, ri); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 332 | BUG_ON(refs != 0); |
| 333 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 334 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 335 | btrfs_release_path(root, path); |
| 336 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 337 | return ret; |
| 338 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 339 | |
| 340 | int btrfs_del_root_ref(struct btrfs_trans_handle *trans, |
| 341 | struct btrfs_root *tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 342 | u64 root_id, u64 ref_id, u64 dirid, u64 *sequence, |
| 343 | const char *name, int name_len) |
| 344 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 345 | { |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 346 | struct btrfs_path *path; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 347 | struct btrfs_root_ref *ref; |
| 348 | struct extent_buffer *leaf; |
| 349 | struct btrfs_key key; |
| 350 | unsigned long ptr; |
| 351 | int err = 0; |
| 352 | int ret; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 353 | |
| 354 | path = btrfs_alloc_path(); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 355 | if (!path) |
| 356 | return -ENOMEM; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 357 | |
| 358 | key.objectid = root_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 359 | key.type = BTRFS_ROOT_BACKREF_KEY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 360 | key.offset = ref_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 361 | again: |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 362 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 363 | BUG_ON(ret < 0); |
| 364 | if (ret == 0) { |
| 365 | leaf = path->nodes[0]; |
| 366 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 367 | struct btrfs_root_ref); |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 368 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 369 | WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid); |
| 370 | WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len); |
| 371 | ptr = (unsigned long)(ref + 1); |
| 372 | WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len)); |
| 373 | *sequence = btrfs_root_ref_sequence(leaf, ref); |
| 374 | |
| 375 | ret = btrfs_del_item(trans, tree_root, path); |
| 376 | BUG_ON(ret); |
| 377 | } else |
| 378 | err = -ENOENT; |
| 379 | |
| 380 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { |
| 381 | btrfs_release_path(tree_root, path); |
| 382 | key.objectid = ref_id; |
| 383 | key.type = BTRFS_ROOT_REF_KEY; |
| 384 | key.offset = root_id; |
| 385 | goto again; |
| 386 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 387 | |
| 388 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 389 | return err; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 392 | int btrfs_find_root_ref(struct btrfs_root *tree_root, |
| 393 | struct btrfs_path *path, |
| 394 | u64 root_id, u64 ref_id) |
| 395 | { |
| 396 | struct btrfs_key key; |
| 397 | int ret; |
| 398 | |
| 399 | key.objectid = root_id; |
| 400 | key.type = BTRFS_ROOT_REF_KEY; |
| 401 | key.offset = ref_id; |
| 402 | |
| 403 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); |
| 404 | return ret; |
| 405 | } |
| 406 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 407 | /* |
| 408 | * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY |
| 409 | * or BTRFS_ROOT_BACKREF_KEY. |
| 410 | * |
| 411 | * The dirid, sequence, name and name_len refer to the directory entry |
| 412 | * that is referencing the root. |
| 413 | * |
| 414 | * For a forward ref, the root_id is the id of the tree referencing |
| 415 | * the root and ref_id is the id of the subvol or snapshot. |
| 416 | * |
| 417 | * For a back ref the root_id is the id of the subvol or snapshot and |
| 418 | * ref_id is the id of the tree referencing it. |
| 419 | */ |
| 420 | int btrfs_add_root_ref(struct btrfs_trans_handle *trans, |
| 421 | struct btrfs_root *tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 422 | u64 root_id, u64 ref_id, u64 dirid, u64 sequence, |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 423 | const char *name, int name_len) |
| 424 | { |
| 425 | struct btrfs_key key; |
| 426 | int ret; |
| 427 | struct btrfs_path *path; |
| 428 | struct btrfs_root_ref *ref; |
| 429 | struct extent_buffer *leaf; |
| 430 | unsigned long ptr; |
| 431 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 432 | path = btrfs_alloc_path(); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 433 | if (!path) |
| 434 | return -ENOMEM; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 435 | |
| 436 | key.objectid = root_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 437 | key.type = BTRFS_ROOT_BACKREF_KEY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 438 | key.offset = ref_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 439 | again: |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 440 | ret = btrfs_insert_empty_item(trans, tree_root, path, &key, |
| 441 | sizeof(*ref) + name_len); |
| 442 | BUG_ON(ret); |
| 443 | |
| 444 | leaf = path->nodes[0]; |
| 445 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 446 | btrfs_set_root_ref_dirid(leaf, ref, dirid); |
| 447 | btrfs_set_root_ref_sequence(leaf, ref, sequence); |
| 448 | btrfs_set_root_ref_name_len(leaf, ref, name_len); |
| 449 | ptr = (unsigned long)(ref + 1); |
| 450 | write_extent_buffer(leaf, name, ptr, name_len); |
| 451 | btrfs_mark_buffer_dirty(leaf); |
| 452 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 453 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { |
| 454 | btrfs_release_path(tree_root, path); |
| 455 | key.objectid = ref_id; |
| 456 | key.type = BTRFS_ROOT_REF_KEY; |
| 457 | key.offset = root_id; |
| 458 | goto again; |
| 459 | } |
| 460 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 461 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 462 | return 0; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 463 | } |