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