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