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