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