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