]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/root-tree.c
Btrfs: trivial include fixups
[net-next-2.6.git] / fs / btrfs / root-tree.c
CommitLineData
6cbd5570
CM
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
3768f368 19#include "ctree.h"
5eda7b5e 20#include "transaction.h"
3768f368
CM
21#include "disk-io.h"
22#include "print-tree.h"
23
24int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25 struct btrfs_root_item *item, struct btrfs_key *key)
26{
5caf2a00 27 struct btrfs_path *path;
3768f368
CM
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;
5eda7b5e 35 search_key.offset = (u64)-1;
3768f368 36
5caf2a00
CM
37 path = btrfs_alloc_path();
38 BUG_ON(!path);
5caf2a00 39 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
3768f368
CM
40 if (ret < 0)
41 goto out;
42 BUG_ON(ret == 0);
5caf2a00
CM
43 l = btrfs_buffer_leaf(path->nodes[0]);
44 BUG_ON(path->slots[0] == 0);
45 slot = path->slots[0] - 1;
62e2749e 46 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
3768f368
CM
47 ret = 1;
48 goto out;
49 }
123abc88 50 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
3768f368
CM
51 sizeof(*item));
52 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
3768f368
CM
53 ret = 0;
54out:
5caf2a00
CM
55 btrfs_release_path(root, path);
56 btrfs_free_path(path);
3768f368
CM
57 return ret;
58}
59
e089f05c
CM
60int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
61 *root, struct btrfs_key *key, struct btrfs_root_item
62 *item)
3768f368 63{
5caf2a00 64 struct btrfs_path *path;
3768f368
CM
65 struct btrfs_leaf *l;
66 int ret;
67 int slot;
d6025579 68 struct btrfs_root_item *update_item;
3768f368 69
5caf2a00
CM
70 path = btrfs_alloc_path();
71 BUG_ON(!path);
5caf2a00 72 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
3768f368
CM
73 if (ret < 0)
74 goto out;
75 BUG_ON(ret != 0);
5caf2a00
CM
76 l = btrfs_buffer_leaf(path->nodes[0]);
77 slot = path->slots[0];
d6025579
CM
78 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
79 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
5caf2a00 80 btrfs_mark_buffer_dirty(path->nodes[0]);
3768f368 81out:
5caf2a00
CM
82 btrfs_release_path(root, path);
83 btrfs_free_path(path);
3768f368
CM
84 return ret;
85}
86
e089f05c
CM
87int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
88 *root, struct btrfs_key *key, struct btrfs_root_item
89 *item)
3768f368
CM
90{
91 int ret;
e089f05c 92 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
93 return ret;
94}
95
5eda7b5e
CM
96int btrfs_find_dead_roots(struct btrfs_root *root)
97{
98 struct btrfs_root *dead_root;
99 struct btrfs_item *item;
100 struct btrfs_root_item *ri;
101 struct btrfs_key key;
102 struct btrfs_path *path;
103 int ret;
104 u32 nritems;
105 struct btrfs_leaf *leaf;
106 int slot;
107
108 key.objectid = 0;
109 key.flags = 0;
110 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
111 key.offset = 0;
112 path = btrfs_alloc_path();
113 if (!path)
114 return -ENOMEM;
115 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
116 if (ret < 0)
117 goto err;
118 while(1) {
119 leaf = btrfs_buffer_leaf(path->nodes[0]);
120 nritems = btrfs_header_nritems(&leaf->header);
121 slot = path->slots[0];
122 if (slot >= nritems) {
123 ret = btrfs_next_leaf(root, path);
124 if (ret)
125 break;
126 leaf = btrfs_buffer_leaf(path->nodes[0]);
127 nritems = btrfs_header_nritems(&leaf->header);
128 slot = path->slots[0];
129 }
130 item = leaf->items + slot;
131 btrfs_disk_key_to_cpu(&key, &item->key);
132 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
133 goto next;
134 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
135 if (btrfs_root_refs(ri) != 0)
136 goto next;
137 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
138 if (IS_ERR(root)) {
139 ret = PTR_ERR(root);
140 goto err;
141 }
5eda7b5e
CM
142 ret = btrfs_add_dead_root(dead_root,
143 &root->fs_info->dead_roots);
144 if (ret)
145 goto err;
146next:
147 slot++;
148 path->slots[0]++;
149 }
150 ret = 0;
151err:
152 btrfs_free_path(path);
153 return ret;
154}
155
e089f05c
CM
156int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
157 struct btrfs_key *key)
3768f368 158{
5caf2a00 159 struct btrfs_path *path;
3768f368 160 int ret;
c5739bba
CM
161 u32 refs;
162 struct btrfs_root_item *ri;
3768f368 163
5caf2a00
CM
164 path = btrfs_alloc_path();
165 BUG_ON(!path);
5caf2a00 166 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3768f368
CM
167 if (ret < 0)
168 goto out;
169 BUG_ON(ret != 0);
c5739bba
CM
170 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
171 path->slots[0], struct btrfs_root_item);
172
173 refs = btrfs_root_refs(ri);
5eda7b5e
CM
174 BUG_ON(refs != 0);
175 ret = btrfs_del_item(trans, root, path);
3768f368 176out:
5caf2a00
CM
177 btrfs_release_path(root, path);
178 btrfs_free_path(path);
3768f368
CM
179 return ret;
180}