Btrfs: Fix the defragmention code and the block relocation code for data=ordered
[deliverable/linux.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
bf4ef679
CM
24/*
25 * returns 0 on finding something, 1 if no more roots are there
26 * and < 0 on error
27 */
28int btrfs_search_root(struct btrfs_root *root, u64 search_start,
29 u64 *found_objectid)
30{
31 struct btrfs_path *path;
32 struct btrfs_key search_key;
33 int ret;
34
35 root = root->fs_info->tree_root;
36 search_key.objectid = search_start;
37 search_key.type = (u8)-1;
38 search_key.offset = (u64)-1;
39
40 path = btrfs_alloc_path();
41 BUG_ON(!path);
42again:
43 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
44 if (ret < 0)
45 goto out;
46 if (ret == 0) {
47 ret = 1;
48 goto out;
49 }
50 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
51 ret = btrfs_next_leaf(root, path);
52 if (ret)
53 goto out;
54 }
55 btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
56 if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
57 search_key.offset++;
58 btrfs_release_path(root, path);
59 goto again;
60 }
61 ret = 0;
62 *found_objectid = search_key.objectid;
63
64out:
65 btrfs_free_path(path);
66 return ret;
67}
68
3768f368
CM
69int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
70 struct btrfs_root_item *item, struct btrfs_key *key)
71{
5caf2a00 72 struct btrfs_path *path;
3768f368 73 struct btrfs_key search_key;
5f39d397
CM
74 struct btrfs_key found_key;
75 struct extent_buffer *l;
3768f368
CM
76 int ret;
77 int slot;
78
79 search_key.objectid = objectid;
5f39d397 80 search_key.type = (u8)-1;
5eda7b5e 81 search_key.offset = (u64)-1;
3768f368 82
5caf2a00
CM
83 path = btrfs_alloc_path();
84 BUG_ON(!path);
5caf2a00 85 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
3768f368
CM
86 if (ret < 0)
87 goto out;
5f39d397 88
3768f368 89 BUG_ON(ret == 0);
5f39d397 90 l = path->nodes[0];
5caf2a00
CM
91 BUG_ON(path->slots[0] == 0);
92 slot = path->slots[0] - 1;
5f39d397
CM
93 btrfs_item_key_to_cpu(l, &found_key, slot);
94 if (found_key.objectid != objectid) {
3768f368
CM
95 ret = 1;
96 goto out;
97 }
5f39d397
CM
98 read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
99 sizeof(*item));
100 memcpy(key, &found_key, sizeof(found_key));
3768f368
CM
101 ret = 0;
102out:
5caf2a00 103 btrfs_free_path(path);
3768f368
CM
104 return ret;
105}
106
e089f05c
CM
107int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
108 *root, struct btrfs_key *key, struct btrfs_root_item
109 *item)
3768f368 110{
5caf2a00 111 struct btrfs_path *path;
5f39d397 112 struct extent_buffer *l;
3768f368
CM
113 int ret;
114 int slot;
5f39d397 115 unsigned long ptr;
3768f368 116
5caf2a00
CM
117 path = btrfs_alloc_path();
118 BUG_ON(!path);
5caf2a00 119 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
3768f368
CM
120 if (ret < 0)
121 goto out;
d6667462
CM
122
123 if (ret != 0) {
124 btrfs_print_leaf(root, path->nodes[0]);
125 printk("unable to update root key %Lu %u %Lu\n",
126 key->objectid, key->type, key->offset);
127 BUG_ON(1);
128 }
129
5f39d397 130 l = path->nodes[0];
5caf2a00 131 slot = path->slots[0];
5f39d397
CM
132 ptr = btrfs_item_ptr_offset(l, slot);
133 write_extent_buffer(l, item, ptr, sizeof(*item));
5caf2a00 134 btrfs_mark_buffer_dirty(path->nodes[0]);
3768f368 135out:
5caf2a00
CM
136 btrfs_release_path(root, path);
137 btrfs_free_path(path);
3768f368
CM
138 return ret;
139}
140
e089f05c
CM
141int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
142 *root, struct btrfs_key *key, struct btrfs_root_item
143 *item)
3768f368
CM
144{
145 int ret;
e089f05c 146 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
3768f368
CM
147 return ret;
148}
149
5ce14bbc
CM
150int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid,
151 struct btrfs_root *latest)
5eda7b5e
CM
152{
153 struct btrfs_root *dead_root;
154 struct btrfs_item *item;
155 struct btrfs_root_item *ri;
156 struct btrfs_key key;
a7a16fd7 157 struct btrfs_key found_key;
5eda7b5e
CM
158 struct btrfs_path *path;
159 int ret;
160 u32 nritems;
5f39d397 161 struct extent_buffer *leaf;
5eda7b5e
CM
162 int slot;
163
5ce14bbc 164 key.objectid = objectid;
5eda7b5e
CM
165 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
166 key.offset = 0;
167 path = btrfs_alloc_path();
168 if (!path)
169 return -ENOMEM;
a7a16fd7
CM
170
171again:
5eda7b5e
CM
172 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
173 if (ret < 0)
174 goto err;
175 while(1) {
5f39d397
CM
176 leaf = path->nodes[0];
177 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
178 slot = path->slots[0];
179 if (slot >= nritems) {
180 ret = btrfs_next_leaf(root, path);
181 if (ret)
182 break;
5f39d397
CM
183 leaf = path->nodes[0];
184 nritems = btrfs_header_nritems(leaf);
5eda7b5e
CM
185 slot = path->slots[0];
186 }
5f39d397
CM
187 item = btrfs_item_nr(leaf, slot);
188 btrfs_item_key_to_cpu(leaf, &key, slot);
5eda7b5e
CM
189 if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
190 goto next;
5ce14bbc
CM
191
192 if (key.objectid < objectid)
193 goto next;
194
195 if (key.objectid > objectid)
196 break;
197
5eda7b5e 198 ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
5f39d397 199 if (btrfs_disk_root_refs(leaf, ri) != 0)
5eda7b5e 200 goto next;
5ce14bbc 201
a7a16fd7
CM
202 memcpy(&found_key, &key, sizeof(key));
203 key.offset++;
204 btrfs_release_path(root, path);
205 dead_root = btrfs_read_fs_root_no_radix(root->fs_info,
206 &found_key);
a1f39630
A
207 if (IS_ERR(dead_root)) {
208 ret = PTR_ERR(dead_root);
5eda7b5e
CM
209 goto err;
210 }
5ce14bbc
CM
211
212 ret = btrfs_add_dead_root(dead_root, latest,
5eda7b5e
CM
213 &root->fs_info->dead_roots);
214 if (ret)
215 goto err;
a7a16fd7 216 goto again;
5eda7b5e
CM
217next:
218 slot++;
219 path->slots[0]++;
220 }
221 ret = 0;
222err:
223 btrfs_free_path(path);
224 return ret;
225}
226
e089f05c
CM
227int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
228 struct btrfs_key *key)
3768f368 229{
5caf2a00 230 struct btrfs_path *path;
3768f368 231 int ret;
c5739bba
CM
232 u32 refs;
233 struct btrfs_root_item *ri;
5f39d397 234 struct extent_buffer *leaf;
3768f368 235
5caf2a00
CM
236 path = btrfs_alloc_path();
237 BUG_ON(!path);
5caf2a00 238 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
3768f368
CM
239 if (ret < 0)
240 goto out;
edbd8d4e
CM
241 if (ret) {
242btrfs_print_leaf(root, path->nodes[0]);
243printk("failed to del %Lu %u %Lu\n", key->objectid, key->type, key->offset);
244
245 }
3768f368 246 BUG_ON(ret != 0);
5f39d397
CM
247 leaf = path->nodes[0];
248 ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
c5739bba 249
5f39d397 250 refs = btrfs_disk_root_refs(leaf, ri);
5eda7b5e
CM
251 BUG_ON(refs != 0);
252 ret = btrfs_del_item(trans, root, path);
3768f368 253out:
5caf2a00
CM
254 btrfs_release_path(root, path);
255 btrfs_free_path(path);
3768f368
CM
256 return ret;
257}
This page took 0.049015 seconds and 5 git commands to generate.