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 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 19 | #include <linux/uuid.h> |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 20 | #include "ctree.h" |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 21 | #include "transaction.h" |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 22 | #include "disk-io.h" |
| 23 | #include "print-tree.h" |
| 24 | |
Chris Mason | bf4ef67 | 2008-05-08 13:26:18 -0400 | [diff] [blame] | 25 | /* |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 26 | * Read a root item from the tree. In case we detect a root item smaller then |
| 27 | * sizeof(root_item), we know it's an old version of the root structure and |
| 28 | * initialize all new fields to zero. The same happens if we detect mismatching |
| 29 | * generation numbers as then we know the root was once mounted with an older |
| 30 | * kernel that was not aware of the root item structure change. |
| 31 | */ |
| 32 | void btrfs_read_root_item(struct btrfs_root *root, |
| 33 | struct extent_buffer *eb, int slot, |
| 34 | struct btrfs_root_item *item) |
| 35 | { |
| 36 | uuid_le uuid; |
| 37 | int len; |
| 38 | int need_reset = 0; |
| 39 | |
| 40 | len = btrfs_item_size_nr(eb, slot); |
| 41 | read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot), |
| 42 | min_t(int, len, (int)sizeof(*item))); |
| 43 | if (len < sizeof(*item)) |
| 44 | need_reset = 1; |
| 45 | if (!need_reset && btrfs_root_generation(item) |
| 46 | != btrfs_root_generation_v2(item)) { |
| 47 | if (btrfs_root_generation_v2(item) != 0) { |
| 48 | printk(KERN_WARNING "btrfs: mismatching " |
| 49 | "generation and generation_v2 " |
| 50 | "found in root item. This root " |
| 51 | "was probably mounted with an " |
| 52 | "older kernel. Resetting all " |
| 53 | "new fields.\n"); |
| 54 | } |
| 55 | need_reset = 1; |
| 56 | } |
| 57 | if (need_reset) { |
| 58 | memset(&item->generation_v2, 0, |
| 59 | sizeof(*item) - offsetof(struct btrfs_root_item, |
| 60 | generation_v2)); |
| 61 | |
| 62 | uuid_le_gen(&uuid); |
| 63 | memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 68 | * lookup the root with the highest offset for a given objectid. The key we do |
| 69 | * find is copied into 'key'. If we find something return 0, otherwise 1, < 0 |
| 70 | * on error. |
| 71 | */ |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 72 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, |
| 73 | struct btrfs_root_item *item, struct btrfs_key *key) |
| 74 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 75 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 76 | struct btrfs_key search_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 77 | struct btrfs_key found_key; |
| 78 | struct extent_buffer *l; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 79 | int ret; |
| 80 | int slot; |
| 81 | |
| 82 | search_key.objectid = objectid; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 83 | search_key.type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 84 | search_key.offset = (u64)-1; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 85 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 86 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 87 | if (!path) |
| 88 | return -ENOMEM; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 89 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 90 | if (ret < 0) |
| 91 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 92 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 93 | BUG_ON(ret == 0); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 94 | if (path->slots[0] == 0) { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 95 | ret = 1; |
| 96 | goto out; |
| 97 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 98 | l = path->nodes[0]; |
| 99 | slot = path->slots[0] - 1; |
| 100 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 101 | if (found_key.objectid != objectid || |
| 102 | found_key.type != BTRFS_ROOT_ITEM_KEY) { |
| 103 | ret = 1; |
| 104 | goto out; |
| 105 | } |
| 106 | if (item) |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 107 | btrfs_read_root_item(root, l, slot, item); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 108 | if (key) |
| 109 | memcpy(key, &found_key, sizeof(found_key)); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 110 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 111 | ret = 0; |
| 112 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 113 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 114 | return ret; |
| 115 | } |
| 116 | |
Mark Fasheh | bf5f32e | 2011-07-14 21:23:06 +0000 | [diff] [blame] | 117 | void btrfs_set_root_node(struct btrfs_root_item *item, |
| 118 | struct extent_buffer *node) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 119 | { |
| 120 | btrfs_set_root_bytenr(item, node->start); |
| 121 | btrfs_set_root_level(item, btrfs_header_level(node)); |
| 122 | btrfs_set_root_generation(item, btrfs_header_generation(node)); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 125 | /* |
| 126 | * copy the data in 'item' into the btree |
| 127 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 128 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 129 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 130 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 131 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 132 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 133 | struct extent_buffer *l; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 134 | int ret; |
| 135 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 136 | unsigned long ptr; |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 137 | int old_len; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 138 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 139 | path = btrfs_alloc_path(); |
Jeff Mahoney | b45a9d8 | 2011-10-03 23:22:44 -0400 | [diff] [blame] | 140 | if (!path) |
| 141 | return -ENOMEM; |
| 142 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 143 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 144 | if (ret < 0) { |
| 145 | btrfs_abort_transaction(trans, root, ret); |
| 146 | goto out; |
| 147 | } |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 148 | |
| 149 | if (ret != 0) { |
| 150 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 151 | printk(KERN_CRIT "unable to update root key %llu %u %llu\n", |
| 152 | (unsigned long long)key->objectid, key->type, |
| 153 | (unsigned long long)key->offset); |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 154 | BUG_ON(1); |
| 155 | } |
| 156 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 157 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 158 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 159 | ptr = btrfs_item_ptr_offset(l, slot); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 160 | old_len = btrfs_item_size_nr(l, slot); |
| 161 | |
| 162 | /* |
| 163 | * If this is the first time we update the root item which originated |
| 164 | * from an older kernel, we need to enlarge the item size to make room |
| 165 | * for the added fields. |
| 166 | */ |
| 167 | if (old_len < sizeof(*item)) { |
| 168 | btrfs_release_path(path); |
| 169 | ret = btrfs_search_slot(trans, root, key, path, |
| 170 | -1, 1); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 171 | if (ret < 0) { |
| 172 | btrfs_abort_transaction(trans, root, ret); |
| 173 | goto out; |
| 174 | } |
| 175 | |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 176 | ret = btrfs_del_item(trans, root, path); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 177 | if (ret < 0) { |
| 178 | btrfs_abort_transaction(trans, root, ret); |
| 179 | goto out; |
| 180 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 181 | btrfs_release_path(path); |
| 182 | ret = btrfs_insert_empty_item(trans, root, path, |
| 183 | key, sizeof(*item)); |
David Sterba | 005d642 | 2012-09-18 07:52:32 -0600 | [diff] [blame] | 184 | if (ret < 0) { |
| 185 | btrfs_abort_transaction(trans, root, ret); |
| 186 | goto out; |
| 187 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 188 | l = path->nodes[0]; |
| 189 | slot = path->slots[0]; |
| 190 | ptr = btrfs_item_ptr_offset(l, slot); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Update generation_v2 so at the next mount we know the new root |
| 195 | * fields are valid. |
| 196 | */ |
| 197 | btrfs_set_root_generation_v2(item, btrfs_root_generation(item)); |
| 198 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 199 | write_extent_buffer(l, item, ptr, sizeof(*item)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 200 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 201 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 202 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 203 | return ret; |
| 204 | } |
| 205 | |
Jeff Mahoney | d16cb05 | 2011-10-03 23:22:34 -0400 | [diff] [blame] | 206 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 207 | struct btrfs_key *key, struct btrfs_root_item *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 208 | { |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 209 | /* |
| 210 | * Make sure generation v1 and v2 match. See update_root for details. |
| 211 | */ |
| 212 | btrfs_set_root_generation_v2(item, btrfs_root_generation(item)); |
Jeff Mahoney | d16cb05 | 2011-10-03 23:22:34 -0400 | [diff] [blame] | 213 | return btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 216 | /* |
| 217 | * 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] | 218 | * the process of being deleted if we crashed. This is any root item with an |
| 219 | * offset lower than the latest root. They need to be queued for deletion to |
| 220 | * finish what was happening when we crashed. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 221 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 222 | int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 223 | { |
| 224 | struct btrfs_root *dead_root; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 225 | struct btrfs_root_item *ri; |
| 226 | struct btrfs_key key; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 227 | struct btrfs_key found_key; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 228 | struct btrfs_path *path; |
| 229 | int ret; |
| 230 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 231 | struct extent_buffer *leaf; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 232 | int slot; |
| 233 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 234 | key.objectid = objectid; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 235 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 236 | key.offset = 0; |
| 237 | path = btrfs_alloc_path(); |
| 238 | if (!path) |
| 239 | return -ENOMEM; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 240 | |
| 241 | again: |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 242 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 243 | if (ret < 0) |
| 244 | goto err; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 245 | while (1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 246 | leaf = path->nodes[0]; |
| 247 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 248 | slot = path->slots[0]; |
| 249 | if (slot >= nritems) { |
| 250 | ret = btrfs_next_leaf(root, path); |
| 251 | if (ret) |
| 252 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 253 | leaf = path->nodes[0]; |
| 254 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 255 | slot = path->slots[0]; |
| 256 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 257 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 258 | if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) |
| 259 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 260 | |
| 261 | if (key.objectid < objectid) |
| 262 | goto next; |
| 263 | |
| 264 | if (key.objectid > objectid) |
| 265 | break; |
| 266 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 267 | ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 268 | if (btrfs_disk_root_refs(leaf, ri) != 0) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 269 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 270 | |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 271 | memcpy(&found_key, &key, sizeof(key)); |
| 272 | key.offset++; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 273 | btrfs_release_path(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 274 | dead_root = |
| 275 | btrfs_read_fs_root_no_radix(root->fs_info->tree_root, |
| 276 | &found_key); |
Aneesh | a1f3963 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 277 | if (IS_ERR(dead_root)) { |
| 278 | ret = PTR_ERR(dead_root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 279 | goto err; |
| 280 | } |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 281 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 282 | ret = btrfs_add_dead_root(dead_root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 283 | if (ret) |
| 284 | goto err; |
Chris Mason | a7a16fd | 2008-06-26 10:34:20 -0400 | [diff] [blame] | 285 | goto again; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 286 | next: |
| 287 | slot++; |
| 288 | path->slots[0]++; |
| 289 | } |
| 290 | ret = 0; |
| 291 | err: |
| 292 | btrfs_free_path(path); |
| 293 | return ret; |
| 294 | } |
| 295 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 296 | int btrfs_find_orphan_roots(struct btrfs_root *tree_root) |
| 297 | { |
| 298 | struct extent_buffer *leaf; |
| 299 | struct btrfs_path *path; |
| 300 | struct btrfs_key key; |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 301 | struct btrfs_key root_key; |
| 302 | struct btrfs_root *root; |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 303 | int err = 0; |
| 304 | int ret; |
| 305 | |
| 306 | path = btrfs_alloc_path(); |
| 307 | if (!path) |
| 308 | return -ENOMEM; |
| 309 | |
| 310 | key.objectid = BTRFS_ORPHAN_OBJECTID; |
| 311 | key.type = BTRFS_ORPHAN_ITEM_KEY; |
| 312 | key.offset = 0; |
| 313 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 314 | root_key.type = BTRFS_ROOT_ITEM_KEY; |
| 315 | root_key.offset = (u64)-1; |
| 316 | |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 317 | while (1) { |
| 318 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); |
| 319 | if (ret < 0) { |
| 320 | err = ret; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | leaf = path->nodes[0]; |
| 325 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
| 326 | ret = btrfs_next_leaf(tree_root, path); |
| 327 | if (ret < 0) |
| 328 | err = ret; |
| 329 | if (ret != 0) |
| 330 | break; |
| 331 | leaf = path->nodes[0]; |
| 332 | } |
| 333 | |
| 334 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 335 | btrfs_release_path(path); |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 336 | |
| 337 | if (key.objectid != BTRFS_ORPHAN_OBJECTID || |
| 338 | key.type != BTRFS_ORPHAN_ITEM_KEY) |
| 339 | break; |
| 340 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 341 | root_key.objectid = key.offset; |
| 342 | key.offset++; |
| 343 | |
| 344 | root = btrfs_read_fs_root_no_name(tree_root->fs_info, |
| 345 | &root_key); |
| 346 | if (!IS_ERR(root)) |
| 347 | continue; |
| 348 | |
| 349 | ret = PTR_ERR(root); |
| 350 | if (ret != -ENOENT) { |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 351 | err = ret; |
| 352 | break; |
| 353 | } |
| 354 | |
Yan, Zheng | d68fc57 | 2010-05-16 10:49:58 -0400 | [diff] [blame] | 355 | ret = btrfs_find_dead_roots(tree_root, root_key.objectid); |
| 356 | if (ret) { |
| 357 | err = ret; |
| 358 | break; |
| 359 | } |
Yan, Zheng | 76dda93 | 2009-09-21 16:00:26 -0400 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | btrfs_free_path(path); |
| 363 | return err; |
| 364 | } |
| 365 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 366 | /* drop the root item for 'key' from 'root' */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 367 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 368 | struct btrfs_key *key) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 369 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 370 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 371 | int ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 372 | struct btrfs_root_item *ri; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 373 | struct extent_buffer *leaf; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 374 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 375 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 376 | if (!path) |
| 377 | return -ENOMEM; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 378 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 379 | if (ret < 0) |
| 380 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 381 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 382 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 383 | leaf = path->nodes[0]; |
| 384 | ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 385 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 386 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 387 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 388 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 389 | return ret; |
| 390 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 391 | |
| 392 | int btrfs_del_root_ref(struct btrfs_trans_handle *trans, |
| 393 | struct btrfs_root *tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 394 | u64 root_id, u64 ref_id, u64 dirid, u64 *sequence, |
| 395 | const char *name, int name_len) |
| 396 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 397 | { |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 398 | struct btrfs_path *path; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 399 | struct btrfs_root_ref *ref; |
| 400 | struct extent_buffer *leaf; |
| 401 | struct btrfs_key key; |
| 402 | unsigned long ptr; |
| 403 | int err = 0; |
| 404 | int ret; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 405 | |
| 406 | path = btrfs_alloc_path(); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 407 | if (!path) |
| 408 | return -ENOMEM; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 409 | |
| 410 | key.objectid = root_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 411 | key.type = BTRFS_ROOT_BACKREF_KEY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 412 | key.offset = ref_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 413 | again: |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 414 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 415 | BUG_ON(ret < 0); |
| 416 | if (ret == 0) { |
| 417 | leaf = path->nodes[0]; |
| 418 | ref = btrfs_item_ptr(leaf, path->slots[0], |
| 419 | struct btrfs_root_ref); |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 420 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 421 | WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid); |
| 422 | WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len); |
| 423 | ptr = (unsigned long)(ref + 1); |
| 424 | WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len)); |
| 425 | *sequence = btrfs_root_ref_sequence(leaf, ref); |
| 426 | |
| 427 | ret = btrfs_del_item(trans, tree_root, path); |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 428 | if (ret) { |
| 429 | err = ret; |
| 430 | goto out; |
| 431 | } |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 432 | } else |
| 433 | err = -ENOENT; |
| 434 | |
| 435 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 436 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 437 | key.objectid = ref_id; |
| 438 | key.type = BTRFS_ROOT_REF_KEY; |
| 439 | key.offset = root_id; |
| 440 | goto again; |
| 441 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 442 | |
Tsutomu Itoh | 65a246c | 2011-05-19 04:37:44 +0000 | [diff] [blame] | 443 | out: |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 444 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 445 | return err; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 446 | } |
| 447 | |
Chris Mason | ea9e8b1 | 2008-11-17 21:14:24 -0500 | [diff] [blame] | 448 | int btrfs_find_root_ref(struct btrfs_root *tree_root, |
| 449 | struct btrfs_path *path, |
| 450 | u64 root_id, u64 ref_id) |
| 451 | { |
| 452 | struct btrfs_key key; |
| 453 | int ret; |
| 454 | |
| 455 | key.objectid = root_id; |
| 456 | key.type = BTRFS_ROOT_REF_KEY; |
| 457 | key.offset = ref_id; |
| 458 | |
| 459 | ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0); |
| 460 | return ret; |
| 461 | } |
| 462 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 463 | /* |
| 464 | * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY |
| 465 | * or BTRFS_ROOT_BACKREF_KEY. |
| 466 | * |
| 467 | * The dirid, sequence, name and name_len refer to the directory entry |
| 468 | * that is referencing the root. |
| 469 | * |
| 470 | * For a forward ref, the root_id is the id of the tree referencing |
| 471 | * the root and ref_id is the id of the subvol or snapshot. |
| 472 | * |
| 473 | * For a back ref the root_id is the id of the subvol or snapshot and |
| 474 | * ref_id is the id of the tree referencing it. |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 475 | * |
| 476 | * Will return 0, -ENOMEM, or anything from the CoW path |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 477 | */ |
| 478 | int btrfs_add_root_ref(struct btrfs_trans_handle *trans, |
| 479 | struct btrfs_root *tree_root, |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 480 | u64 root_id, u64 ref_id, u64 dirid, u64 sequence, |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 481 | const char *name, int name_len) |
| 482 | { |
| 483 | struct btrfs_key key; |
| 484 | int ret; |
| 485 | struct btrfs_path *path; |
| 486 | struct btrfs_root_ref *ref; |
| 487 | struct extent_buffer *leaf; |
| 488 | unsigned long ptr; |
| 489 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 490 | path = btrfs_alloc_path(); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 491 | if (!path) |
| 492 | return -ENOMEM; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 493 | |
| 494 | key.objectid = root_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 495 | key.type = BTRFS_ROOT_BACKREF_KEY; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 496 | key.offset = ref_id; |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 497 | again: |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 498 | ret = btrfs_insert_empty_item(trans, tree_root, path, &key, |
| 499 | sizeof(*ref) + name_len); |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 500 | if (ret) { |
| 501 | btrfs_abort_transaction(trans, tree_root, ret); |
| 502 | btrfs_free_path(path); |
| 503 | return ret; |
| 504 | } |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 505 | |
| 506 | leaf = path->nodes[0]; |
| 507 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 508 | btrfs_set_root_ref_dirid(leaf, ref, dirid); |
| 509 | btrfs_set_root_ref_sequence(leaf, ref, sequence); |
| 510 | btrfs_set_root_ref_name_len(leaf, ref, name_len); |
| 511 | ptr = (unsigned long)(ref + 1); |
| 512 | write_extent_buffer(leaf, name, ptr, name_len); |
| 513 | btrfs_mark_buffer_dirty(leaf); |
| 514 | |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 515 | if (key.type == BTRFS_ROOT_BACKREF_KEY) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 516 | btrfs_release_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 517 | key.objectid = ref_id; |
| 518 | key.type = BTRFS_ROOT_REF_KEY; |
| 519 | key.offset = root_id; |
| 520 | goto again; |
| 521 | } |
| 522 | |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 523 | btrfs_free_path(path); |
Yan, Zheng | 4df27c4d | 2009-09-21 15:56:00 -0400 | [diff] [blame] | 524 | return 0; |
Chris Mason | 0660b5a | 2008-11-17 20:37:39 -0500 | [diff] [blame] | 525 | } |
Li Zefan | 08fe4db | 2011-03-28 02:01:25 +0000 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * Old btrfs forgets to init root_item->flags and root_item->byte_limit |
| 529 | * for subvolumes. To work around this problem, we steal a bit from |
| 530 | * root_item->inode_item->flags, and use it to indicate if those fields |
| 531 | * have been properly initialized. |
| 532 | */ |
| 533 | void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item) |
| 534 | { |
| 535 | u64 inode_flags = le64_to_cpu(root_item->inode.flags); |
| 536 | |
| 537 | if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) { |
| 538 | inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT; |
| 539 | root_item->inode.flags = cpu_to_le64(inode_flags); |
| 540 | root_item->flags = 0; |
| 541 | root_item->byte_limit = 0; |
| 542 | } |
| 543 | } |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 544 | |
| 545 | void btrfs_update_root_times(struct btrfs_trans_handle *trans, |
| 546 | struct btrfs_root *root) |
| 547 | { |
| 548 | struct btrfs_root_item *item = &root->root_item; |
| 549 | struct timespec ct = CURRENT_TIME; |
| 550 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 551 | spin_lock(&root->root_item_lock); |
Dan Carpenter | dadd110 | 2012-07-30 02:10:44 -0600 | [diff] [blame] | 552 | item->ctransid = cpu_to_le64(trans->transid); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 553 | item->ctime.sec = cpu_to_le64(ct.tv_sec); |
Dan Carpenter | dadd110 | 2012-07-30 02:10:44 -0600 | [diff] [blame] | 554 | item->ctime.nsec = cpu_to_le32(ct.tv_nsec); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 555 | spin_unlock(&root->root_item_lock); |
Alexander Block | 8ea05e3 | 2012-07-25 17:35:53 +0200 | [diff] [blame] | 556 | } |