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 | /* |
| 25 | * returns 0 on finding something, 1 if no more roots are there |
| 26 | * and < 0 on error |
| 27 | */ |
| 28 | int btrfs_search_root(struct btrfs_root *root, u64 search_start, |
| 29 | u64 *found_objectid) |
| 30 | { |
| 31 | struct btrfs_path *path; |
| 32 | struct btrfs_key search_key; |
| 33 | int ret; |
| 34 | |
| 35 | root = root->fs_info->tree_root; |
| 36 | search_key.objectid = search_start; |
| 37 | search_key.type = (u8)-1; |
| 38 | search_key.offset = (u64)-1; |
| 39 | |
| 40 | path = btrfs_alloc_path(); |
| 41 | BUG_ON(!path); |
| 42 | again: |
| 43 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
| 44 | if (ret < 0) |
| 45 | goto out; |
| 46 | if (ret == 0) { |
| 47 | ret = 1; |
| 48 | goto out; |
| 49 | } |
| 50 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { |
| 51 | ret = btrfs_next_leaf(root, path); |
| 52 | if (ret) |
| 53 | goto out; |
| 54 | } |
| 55 | btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]); |
| 56 | if (search_key.type != BTRFS_ROOT_ITEM_KEY) { |
| 57 | search_key.offset++; |
| 58 | btrfs_release_path(root, path); |
| 59 | goto again; |
| 60 | } |
| 61 | ret = 0; |
| 62 | *found_objectid = search_key.objectid; |
| 63 | |
| 64 | out: |
| 65 | btrfs_free_path(path); |
| 66 | return ret; |
| 67 | } |
| 68 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 69 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, |
| 70 | struct btrfs_root_item *item, struct btrfs_key *key) |
| 71 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 72 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 73 | struct btrfs_key search_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 74 | struct btrfs_key found_key; |
| 75 | struct extent_buffer *l; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 76 | int ret; |
| 77 | int slot; |
| 78 | |
| 79 | search_key.objectid = objectid; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 80 | search_key.type = (u8)-1; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 81 | search_key.offset = (u64)-1; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 82 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 83 | path = btrfs_alloc_path(); |
| 84 | BUG_ON(!path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 85 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 86 | if (ret < 0) |
| 87 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 88 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 89 | BUG_ON(ret == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 90 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 91 | BUG_ON(path->slots[0] == 0); |
| 92 | slot = path->slots[0] - 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 93 | btrfs_item_key_to_cpu(l, &found_key, slot); |
| 94 | if (found_key.objectid != objectid) { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 95 | ret = 1; |
| 96 | goto out; |
| 97 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 98 | read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot), |
| 99 | sizeof(*item)); |
| 100 | memcpy(key, &found_key, sizeof(found_key)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 101 | ret = 0; |
| 102 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 103 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 104 | return ret; |
| 105 | } |
| 106 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 107 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 108 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 109 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 110 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 111 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 112 | struct extent_buffer *l; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 113 | int ret; |
| 114 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 115 | unsigned long ptr; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 116 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 117 | path = btrfs_alloc_path(); |
| 118 | BUG_ON(!path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 119 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 120 | if (ret < 0) |
| 121 | goto out; |
Chris Mason | d666746 | 2008-01-03 14:51:00 -0500 | [diff] [blame] | 122 | |
| 123 | if (ret != 0) { |
| 124 | btrfs_print_leaf(root, path->nodes[0]); |
| 125 | printk("unable to update root key %Lu %u %Lu\n", |
| 126 | key->objectid, key->type, key->offset); |
| 127 | BUG_ON(1); |
| 128 | } |
| 129 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 130 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 131 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 132 | ptr = btrfs_item_ptr_offset(l, slot); |
| 133 | write_extent_buffer(l, item, ptr, sizeof(*item)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 134 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 135 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 136 | btrfs_release_path(root, path); |
| 137 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 141 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 142 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 143 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 144 | { |
| 145 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 146 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 147 | return ret; |
| 148 | } |
| 149 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 150 | int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid, |
| 151 | struct btrfs_root *latest) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 152 | { |
| 153 | struct btrfs_root *dead_root; |
| 154 | struct btrfs_item *item; |
| 155 | struct btrfs_root_item *ri; |
| 156 | struct btrfs_key key; |
| 157 | struct btrfs_path *path; |
| 158 | int ret; |
| 159 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 160 | struct extent_buffer *leaf; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 161 | int slot; |
| 162 | |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 163 | key.objectid = objectid; |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 164 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 165 | key.offset = 0; |
| 166 | path = btrfs_alloc_path(); |
| 167 | if (!path) |
| 168 | return -ENOMEM; |
| 169 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 170 | if (ret < 0) |
| 171 | goto err; |
| 172 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 173 | leaf = path->nodes[0]; |
| 174 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 175 | slot = path->slots[0]; |
| 176 | if (slot >= nritems) { |
| 177 | ret = btrfs_next_leaf(root, path); |
| 178 | if (ret) |
| 179 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 180 | leaf = path->nodes[0]; |
| 181 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 182 | slot = path->slots[0]; |
| 183 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 184 | item = btrfs_item_nr(leaf, slot); |
| 185 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 186 | if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY) |
| 187 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 188 | |
| 189 | if (key.objectid < objectid) |
| 190 | goto next; |
| 191 | |
| 192 | if (key.objectid > objectid) |
| 193 | break; |
| 194 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 195 | ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 196 | if (btrfs_disk_root_refs(leaf, ri) != 0) |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 197 | goto next; |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 198 | |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 199 | dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key); |
Aneesh | a1f39630 | 2007-07-11 10:03:27 -0400 | [diff] [blame] | 200 | if (IS_ERR(dead_root)) { |
| 201 | ret = PTR_ERR(dead_root); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 202 | goto err; |
| 203 | } |
Chris Mason | 5ce14bb | 2007-09-11 11:15:39 -0400 | [diff] [blame] | 204 | |
| 205 | ret = btrfs_add_dead_root(dead_root, latest, |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 206 | &root->fs_info->dead_roots); |
| 207 | if (ret) |
| 208 | goto err; |
| 209 | next: |
| 210 | slot++; |
| 211 | path->slots[0]++; |
| 212 | } |
| 213 | ret = 0; |
| 214 | err: |
| 215 | btrfs_free_path(path); |
| 216 | return ret; |
| 217 | } |
| 218 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 219 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 220 | struct btrfs_key *key) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 221 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 222 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 223 | int ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 224 | u32 refs; |
| 225 | struct btrfs_root_item *ri; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 226 | struct extent_buffer *leaf; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 227 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 228 | path = btrfs_alloc_path(); |
| 229 | BUG_ON(!path); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 230 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 231 | if (ret < 0) |
| 232 | goto out; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 233 | if (ret) { |
| 234 | btrfs_print_leaf(root, path->nodes[0]); |
| 235 | printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset); |
| 236 | |
| 237 | } |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 238 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 239 | leaf = path->nodes[0]; |
| 240 | ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 241 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 242 | refs = btrfs_disk_root_refs(leaf, ri); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 243 | BUG_ON(refs != 0); |
| 244 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 245 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 246 | btrfs_release_path(root, path); |
| 247 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 248 | return ret; |
| 249 | } |