2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/module.h>
21 #include "transaction.h"
23 #include "print-tree.h"
25 int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
26 struct btrfs_root_item *item, struct btrfs_key *key)
28 struct btrfs_path *path;
29 struct btrfs_key search_key;
34 search_key.objectid = objectid;
35 search_key.flags = (u32)-1;
36 search_key.offset = (u64)-1;
38 path = btrfs_alloc_path();
40 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
44 l = btrfs_buffer_leaf(path->nodes[0]);
45 BUG_ON(path->slots[0] == 0);
46 slot = path->slots[0] - 1;
47 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
51 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
53 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
56 btrfs_release_path(root, path);
57 btrfs_free_path(path);
61 int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
62 *root, struct btrfs_key *key, struct btrfs_root_item
65 struct btrfs_path *path;
69 struct btrfs_root_item *update_item;
71 path = btrfs_alloc_path();
73 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
77 l = btrfs_buffer_leaf(path->nodes[0]);
78 slot = path->slots[0];
79 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
80 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
81 btrfs_mark_buffer_dirty(path->nodes[0]);
83 btrfs_release_path(root, path);
84 btrfs_free_path(path);
88 int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
89 *root, struct btrfs_key *key, struct btrfs_root_item
93 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
97 int btrfs_find_dead_roots(struct btrfs_root *root)
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;
106 struct btrfs_leaf *leaf;
111 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
113 path = btrfs_alloc_path();
116 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
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);
127 leaf = btrfs_buffer_leaf(path->nodes[0]);
128 nritems = btrfs_header_nritems(&leaf->header);
129 slot = path->slots[0];
131 item = leaf->items + slot;
132 btrfs_disk_key_to_cpu(&key, &item->key);
133 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
135 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
136 if (btrfs_root_refs(ri) != 0)
138 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
143 ret = btrfs_add_dead_root(dead_root,
144 &root->fs_info->dead_roots);
153 btrfs_free_path(path);
157 int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
158 struct btrfs_key *key)
160 struct btrfs_path *path;
163 struct btrfs_root_item *ri;
165 path = btrfs_alloc_path();
167 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
171 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
172 path->slots[0], struct btrfs_root_item);
174 refs = btrfs_root_refs(ri);
176 ret = btrfs_del_item(trans, root, path);
178 btrfs_release_path(root, path);
179 btrfs_free_path(path);