Btrfs: Allow tails larger than one page
[linux-2.6-block.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 28 struct btrfs_key search_key;
5f39d397
CM
29 struct btrfs_key found_key;
30 struct extent_buffer *l;
3768f368
CM
31 int ret;
32 int slot;
33
34 search_key.objectid = objectid;
5f39d397 35 search_key.type = (u8)-1;
5eda7b5e 36 search_key.offset = (u64)-1;
3768f368 37
5caf2a00
CM
38 path = btrfs_alloc_path();
39 BUG_ON(!path);
5caf2a00 40 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
3768f368
CM
41 if (ret < 0)
42 goto out;
5f39d397 43
3768f368 44 BUG_ON(ret == 0);
5f39d397 45 l = path->nodes[0];
5caf2a00
CM
46 BUG_ON(path->slots[0] == 0);
47 slot = path->slots[0] - 1;
5f39d397
CM
48 btrfs_item_key_to_cpu(l, &found_key, slot);
49 if (found_key.objectid != objectid) {
3768f368
CM
50 ret = 1;
51 goto out;
52 }
5f39d397
CM
53 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
54 sizeof(*item));
55 memcpy(key, &found_key, sizeof(found_key));
3768f368
CM
56 ret = 0;
57out:
5caf2a00
CM
58 btrfs_release_path(root, path);
59 btrfs_free_path(path);
3768f368
CM
60 return ret;
61}
62
e089f05c
CM
63int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
64 *root, struct btrfs_key *key, struct btrfs_root_item
65 *item)
3768f368 66{
5caf2a00 67 struct btrfs_path *path;
5f39d397 68 struct extent_buffer *l;
3768f368
CM
69 int ret;
70 int slot;
5f39d397 71 unsigned long ptr;
3768f368 72
5caf2a00
CM
73 path = btrfs_alloc_path();
74 BUG_ON(!path);
5caf2a00 75 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
3768f368
CM
76 if (ret < 0)
77 goto out;
78 BUG_ON(ret != 0);
5f39d397 79 l = path->nodes[0];
5caf2a00 80 slot = path->slots[0];
5f39d397
CM
81 ptr = btrfs_item_ptr_offset(l, slot);
82 write_extent_buffer(l, item, ptr, sizeof(*item));
5caf2a00 83 btrfs_mark_buffer_dirty(path->nodes[0]);
3768f368 84out:
5caf2a00
CM
85 btrfs_release_path(root, path);
86 btrfs_free_path(path);
3768f368
CM
87 return ret;
88}
89
e089f05c
CM
90int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
91 *root, struct btrfs_key *key, struct btrfs_root_item
92 *item)
3768f368
CM
93{
94 int ret;
e089f05c 95 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
96 return ret;
97}
98
5ce14bbc
CM
99int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
100 struct btrfs_root *latest)
5eda7b5e
CM
101{
102 struct btrfs_root *dead_root;
103 struct btrfs_item *item;
104 struct btrfs_root_item *ri;
105 struct btrfs_key key;
106 struct btrfs_path *path;
107 int ret;
108 u32 nritems;
5f39d397 109 struct extent_buffer *leaf;
5eda7b5e
CM
110 int slot;
111
5ce14bbc 112 key.objectid = objectid;
5eda7b5e
CM
113 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
114 key.offset = 0;
115 path = btrfs_alloc_path();
116 if (!path)
117 return -ENOMEM;
118 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
119 if (ret < 0)
120 goto err;
121 while(1) {
5f39d397
CM
122 leaf = path->nodes[0];
123 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
124 slot = path->slots[0];
125 if (slot >= nritems) {
126 ret = btrfs_next_leaf(root, path);
127 if (ret)
128 break;
5f39d397
CM
129 leaf = path->nodes[0];
130 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
131 slot = path->slots[0];
132 }
5f39d397
CM
133 item = btrfs_item_nr(leaf, slot);
134 btrfs_item_key_to_cpu(leaf, &key, slot);
5eda7b5e
CM
135 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
136 goto next;
5ce14bbc
CM
137
138 if (key.objectid < objectid)
139 goto next;
140
141 if (key.objectid > objectid)
142 break;
143
5eda7b5e 144 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
5f39d397 145 if (btrfs_disk_root_refs(leaf, ri) != 0)
5eda7b5e 146 goto next;
5ce14bbc 147
5eda7b5e 148 dead_root = btrfs_read_fs_root_no_radix(root->fs_info, &key);
a1f39630
A
149 if (IS_ERR(dead_root)) {
150 ret = PTR_ERR(dead_root);
5eda7b5e
CM
151 goto err;
152 }
5ce14bbc
CM
153
154 ret = btrfs_add_dead_root(dead_root, latest,
5eda7b5e
CM
155 &root->fs_info->dead_roots);
156 if (ret)
157 goto err;
158next:
159 slot++;
160 path->slots[0]++;
161 }
162 ret = 0;
163err:
164 btrfs_free_path(path);
165 return ret;
166}
167
e089f05c
CM
168int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
169 struct btrfs_key *key)
3768f368 170{
5caf2a00 171 struct btrfs_path *path;
3768f368 172 int ret;
c5739bba
CM
173 u32 refs;
174 struct btrfs_root_item *ri;
5f39d397 175 struct extent_buffer *leaf;
3768f368 176
5caf2a00
CM
177 path = btrfs_alloc_path();
178 BUG_ON(!path);
5caf2a00 179 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3768f368
CM
180 if (ret < 0)
181 goto out;
182 BUG_ON(ret != 0);
5f39d397
CM
183 leaf = path->nodes[0];
184 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
c5739bba 185
5f39d397 186 refs = btrfs_disk_root_refs(leaf, ri);
5eda7b5e
CM
187 BUG_ON(refs != 0);
188 ret = btrfs_del_item(trans, root, path);
3768f368 189out:
5caf2a00
CM
190 btrfs_release_path(root, path);
191 btrfs_free_path(path);
3768f368
CM
192 return ret;
193}