blob: 6949eebc9dc1813da6280b31a0cbb0e6db603b70 [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masonfec577f2007-02-26 10:40:21 -05002#include "ctree.h"
3#include "disk-io.h"
4#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Masonfec577f2007-02-26 10:40:21 -05006
Chris Masone089f052007-03-16 16:20:31 -04007static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masonfbdc7622007-05-30 10:22:12 -04008 *orig_root, u64 num_blocks, u64 search_start,
9 u64 search_end, u64 hint_block,
10 struct btrfs_key *ins, int data);
Chris Masone089f052007-03-16 16:20:31 -040011static int finish_current_insert(struct btrfs_trans_handle *trans, struct
12 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040013static int del_pending_extents(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -050015
Chris Masonde428b62007-05-18 13:28:27 -040016static void reada_extent_leaves(struct btrfs_root *root,
17 struct btrfs_path *path, u64 limit)
18{
19 struct btrfs_node *node;
20 int i;
21 int nritems;
22 u64 item_objectid;
23 u64 blocknr;
24 int slot;
25 int ret;
26
27 if (!path->nodes[1])
28 return;
29 node = btrfs_buffer_node(path->nodes[1]);
30 slot = path->slots[1] + 1;
31 nritems = btrfs_header_nritems(&node->header);
32 for (i = slot; i < nritems && i < slot + 8; i++) {
33 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
34 if (item_objectid > limit)
35 break;
36 blocknr = btrfs_node_blockptr(node, i);
37 ret = readahead_tree_block(root, blocknr);
38 if (ret)
39 break;
40 }
41}
42
Chris Masone37c9e62007-05-09 20:13:14 -040043static int cache_block_group(struct btrfs_root *root,
44 struct btrfs_block_group_cache *block_group)
45{
46 struct btrfs_path *path;
47 int ret;
48 struct btrfs_key key;
49 struct btrfs_leaf *leaf;
50 struct radix_tree_root *extent_radix;
51 int slot;
52 u64 i;
53 u64 last = 0;
54 u64 hole_size;
Chris Masonde428b62007-05-18 13:28:27 -040055 u64 limit;
Chris Masone37c9e62007-05-09 20:13:14 -040056 int found = 0;
57
58 root = root->fs_info->extent_root;
59 extent_radix = &root->fs_info->extent_map_radix;
60
61 if (block_group->cached)
62 return 0;
63 if (block_group->data)
64 return 0;
65 path = btrfs_alloc_path();
66 if (!path)
67 return -ENOMEM;
Chris Masone37c9e62007-05-09 20:13:14 -040068 key.objectid = block_group->key.objectid;
69 key.flags = 0;
70 key.offset = 0;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
72 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
73 if (ret < 0)
74 return ret;
75 if (ret && path->slots[0] > 0)
76 path->slots[0]--;
Chris Masonde428b62007-05-18 13:28:27 -040077 limit = block_group->key.objectid + block_group->key.offset;
78 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040079 while(1) {
80 leaf = btrfs_buffer_leaf(path->nodes[0]);
81 slot = path->slots[0];
82 if (slot >= btrfs_header_nritems(&leaf->header)) {
Chris Masonde428b62007-05-18 13:28:27 -040083 reada_extent_leaves(root, path, limit);
Chris Masone37c9e62007-05-09 20:13:14 -040084 ret = btrfs_next_leaf(root, path);
Chris Masonde428b62007-05-18 13:28:27 -040085 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040086 continue;
Chris Masonde428b62007-05-18 13:28:27 -040087 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040088 if (found) {
89 hole_size = block_group->key.objectid +
90 block_group->key.offset - last;
91 } else {
92 last = block_group->key.objectid;
93 hole_size = block_group->key.offset;
94 }
95 for (i = 0; i < hole_size; i++) {
96 set_radix_bit(extent_radix,
97 last + i);
98 }
99 break;
100 }
101 }
102 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
103 if (key.objectid >= block_group->key.objectid +
104 block_group->key.offset) {
105 if (found) {
106 hole_size = block_group->key.objectid +
107 block_group->key.offset - last;
108 } else {
109 last = block_group->key.objectid;
110 hole_size = block_group->key.offset;
111 }
112 for (i = 0; i < hole_size; i++) {
113 set_radix_bit(extent_radix, last + i);
114 }
115 break;
116 }
117 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
118 if (!found) {
119 last = key.objectid + key.offset;
120 found = 1;
121 } else {
122 hole_size = key.objectid - last;
123 for (i = 0; i < hole_size; i++) {
124 set_radix_bit(extent_radix, last + i);
125 }
126 last = key.objectid + key.offset;
127 }
128 }
129 path->slots[0]++;
130 }
131
132 block_group->cached = 1;
133 btrfs_free_path(path);
134 return 0;
135}
136
Chris Mason5276aed2007-06-11 21:33:38 -0400137struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
138 btrfs_fs_info *info,
139 u64 blocknr)
Chris Masonbe744172007-05-06 10:15:01 -0400140{
141 struct btrfs_block_group_cache *block_group;
142 int ret;
143
144 ret = radix_tree_gang_lookup(&info->block_group_radix,
145 (void **)&block_group,
146 blocknr, 1);
147 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400148 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400149 block_group->key.objectid + block_group->key.offset)
150 return block_group;
151 }
152 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
153 (void **)&block_group,
154 blocknr, 1);
155 if (ret) {
Chris Mason3e1ad542007-05-07 20:03:49 -0400156 if (block_group->key.objectid <= blocknr && blocknr <=
Chris Masonbe744172007-05-06 10:15:01 -0400157 block_group->key.objectid + block_group->key.offset)
158 return block_group;
159 }
Chris Masonbe744172007-05-06 10:15:01 -0400160 return NULL;
161}
162
Chris Masone37c9e62007-05-09 20:13:14 -0400163static u64 leaf_range(struct btrfs_root *root)
164{
165 u64 size = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason84f54cf2007-06-12 07:43:08 -0400166 do_div(size, sizeof(struct btrfs_extent_item) +
167 sizeof(struct btrfs_item));
Chris Masone37c9e62007-05-09 20:13:14 -0400168 return size;
169}
170
171static u64 find_search_start(struct btrfs_root *root,
172 struct btrfs_block_group_cache **cache_ret,
173 u64 search_start, int num)
174{
175 unsigned long gang[8];
176 int ret;
177 struct btrfs_block_group_cache *cache = *cache_ret;
178 u64 last = max(search_start, cache->key.objectid);
179
180 if (cache->data)
181 goto out;
182 if (num > 1) {
183 last = max(last, cache->last_prealloc);
184 }
185again:
186 cache_block_group(root, cache);
187 while(1) {
188 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
189 gang, last, ARRAY_SIZE(gang));
190 if (!ret)
191 goto out;
192 last = gang[ret-1] + 1;
193 if (num > 1) {
194 if (ret != ARRAY_SIZE(gang)) {
195 goto new_group;
196 }
197 if (gang[ret-1] - gang[0] > leaf_range(root)) {
198 continue;
199 }
200 }
201 if (gang[0] >= cache->key.objectid + cache->key.offset) {
202 goto new_group;
203 }
204 return gang[0];
205 }
206out:
207 return max(cache->last_alloc, search_start);
208
209new_group:
Chris Mason5276aed2007-06-11 21:33:38 -0400210 cache = btrfs_lookup_block_group(root->fs_info,
211 last + cache->key.offset - 1);
Chris Masone37c9e62007-05-09 20:13:14 -0400212 if (!cache) {
213 return max((*cache_ret)->last_alloc, search_start);
214 }
215 cache = btrfs_find_block_group(root, cache,
Chris Masonde428b62007-05-18 13:28:27 -0400216 last + cache->key.offset - 1, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400217 *cache_ret = cache;
218 goto again;
219}
220
Chris Mason84f54cf2007-06-12 07:43:08 -0400221static u64 div_factor(u64 num, int factor)
222{
223 num *= factor;
224 do_div(num, 10);
225 return num;
226}
227
Chris Mason31f3c992007-04-30 15:25:45 -0400228struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
229 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400230 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400231 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400232{
233 struct btrfs_block_group_cache *cache[8];
Chris Mason31f3c992007-04-30 15:25:45 -0400234 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400235 struct btrfs_fs_info *info = root->fs_info;
Chris Masonbe744172007-05-06 10:15:01 -0400236 struct radix_tree_root *radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400237 struct radix_tree_root *swap_radix;
Chris Masoncd1bc462007-04-27 10:08:34 -0400238 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400239 u64 last = 0;
240 u64 hint_last;
Chris Masoncd1bc462007-04-27 10:08:34 -0400241 int i;
242 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400243 int full_search = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400244 int factor = 8;
Chris Mason1e2677e2007-05-29 16:52:18 -0400245 int data_swap = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400246
247 if (!owner)
248 factor = 5;
Chris Masonbe744172007-05-06 10:15:01 -0400249
Chris Mason1e2677e2007-05-29 16:52:18 -0400250 if (data) {
Chris Masonbe744172007-05-06 10:15:01 -0400251 radix = &info->block_group_data_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400252 swap_radix = &info->block_group_radix;
253 } else {
Chris Masonbe744172007-05-06 10:15:01 -0400254 radix = &info->block_group_radix;
Chris Mason1e2677e2007-05-29 16:52:18 -0400255 swap_radix = &info->block_group_data_radix;
256 }
Chris Masonbe744172007-05-06 10:15:01 -0400257
258 if (search_start) {
259 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400260 shint = btrfs_lookup_block_group(info, search_start);
Chris Masonbe744172007-05-06 10:15:01 -0400261 if (shint->data == data) {
262 used = btrfs_block_group_used(&shint->item);
263 if (used + shint->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400264 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400265 return shint;
266 }
267 }
268 }
269 if (hint && hint->data == data) {
Chris Mason31f3c992007-04-30 15:25:45 -0400270 used = btrfs_block_group_used(&hint->item);
Chris Mason84f54cf2007-06-12 07:43:08 -0400271 if (used + hint->pinned <
272 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400273 return hint;
274 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400275 if (used >= div_factor(hint->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400276 radix_tree_tag_clear(radix,
277 hint->key.objectid +
278 hint->key.offset - 1,
279 BTRFS_BLOCK_GROUP_AVAIL);
280 }
Chris Mason8d7be552007-05-10 11:24:42 -0400281 last = hint->key.offset * 3;
Chris Masonbe744172007-05-06 10:15:01 -0400282 if (hint->key.objectid >= last)
Chris Masone37c9e62007-05-09 20:13:14 -0400283 last = max(search_start + hint->key.offset - 1,
284 hint->key.objectid - last);
Chris Masonbe744172007-05-06 10:15:01 -0400285 else
286 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400287 hint_last = last;
288 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400289 if (hint)
290 hint_last = max(hint->key.objectid, search_start);
291 else
292 hint_last = search_start;
293
294 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400295 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400296 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400297 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
Chris Masoncd1bc462007-04-27 10:08:34 -0400298 last, ARRAY_SIZE(cache),
Chris Mason31f3c992007-04-30 15:25:45 -0400299 BTRFS_BLOCK_GROUP_AVAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 if (!ret)
301 break;
302 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400303 last = cache[i]->key.objectid +
304 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400305 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400306 if (used + cache[i]->pinned <
Chris Mason84f54cf2007-06-12 07:43:08 -0400307 div_factor(cache[i]->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400308 found_group = cache[i];
309 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400311 if (used >= div_factor(cache[i]->key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -0400312 radix_tree_tag_clear(radix,
313 cache[i]->key.objectid +
314 cache[i]->key.offset - 1,
315 BTRFS_BLOCK_GROUP_AVAIL);
316 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400317 }
Chris Masonde428b62007-05-18 13:28:27 -0400318 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400319 }
Chris Mason31f3c992007-04-30 15:25:45 -0400320 last = hint_last;
321again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400322 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -0400323 ret = radix_tree_gang_lookup(radix, (void **)cache,
324 last, ARRAY_SIZE(cache));
Chris Masoncd1bc462007-04-27 10:08:34 -0400325 if (!ret)
326 break;
327 for (i = 0; i < ret; i++) {
Chris Masonbe08c1b2007-05-03 09:06:49 -0400328 last = cache[i]->key.objectid +
329 cache[i]->key.offset;
Chris Masoncd1bc462007-04-27 10:08:34 -0400330 used = btrfs_block_group_used(&cache[i]->item);
Chris Masonbe744172007-05-06 10:15:01 -0400331 if (used + cache[i]->pinned < cache[i]->key.offset) {
Chris Mason31f3c992007-04-30 15:25:45 -0400332 found_group = cache[i];
333 goto found;
Chris Masoncd1bc462007-04-27 10:08:34 -0400334 }
Chris Masonbe744172007-05-06 10:15:01 -0400335 if (used >= cache[i]->key.offset) {
336 radix_tree_tag_clear(radix,
337 cache[i]->key.objectid +
338 cache[i]->key.offset - 1,
339 BTRFS_BLOCK_GROUP_AVAIL);
340 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400341 }
Chris Masonde428b62007-05-18 13:28:27 -0400342 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400343 }
Chris Mason31f3c992007-04-30 15:25:45 -0400344 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400345 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400346 full_search = 1;
347 goto again;
348 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400349 if (!data_swap) {
350 struct radix_tree_root *tmp = radix;
351 data_swap = 1;
352 radix = swap_radix;
353 swap_radix = tmp;
354 last = search_start;
355 goto again;
356 }
Chris Mason31f3c992007-04-30 15:25:45 -0400357 if (!found_group) {
Chris Masonbe744172007-05-06 10:15:01 -0400358 ret = radix_tree_gang_lookup(radix,
Chris Mason31f3c992007-04-30 15:25:45 -0400359 (void **)&found_group, 0, 1);
Chris Mason1e2677e2007-05-29 16:52:18 -0400360 if (ret == 0) {
361 ret = radix_tree_gang_lookup(swap_radix,
362 (void **)&found_group,
363 0, 1);
364 }
Chris Mason31f3c992007-04-30 15:25:45 -0400365 BUG_ON(ret != 1);
366 }
Chris Masonbe744172007-05-06 10:15:01 -0400367found:
Chris Mason31f3c992007-04-30 15:25:45 -0400368 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400369}
370
Chris Masonb18c6682007-04-17 13:26:50 -0400371int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
372 struct btrfs_root *root,
373 u64 blocknr, u64 num_blocks)
Chris Mason02217ed2007-03-02 16:08:05 -0500374{
Chris Mason5caf2a02007-04-02 11:20:42 -0400375 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500376 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400377 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400378 struct btrfs_leaf *l;
379 struct btrfs_extent_item *item;
Chris Masone2fa7222007-03-12 16:22:34 -0400380 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400381 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500382
Chris Masonfbdc7622007-05-30 10:22:12 -0400383 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
Chris Masonbe08c1b2007-05-03 09:06:49 -0400384 &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400385 path = btrfs_alloc_path();
386 BUG_ON(!path);
387 btrfs_init_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500388 key.objectid = blocknr;
389 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400390 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason6407bf62007-03-27 06:33:00 -0400391 key.offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -0400392 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400393 0, 1);
Chris Masona429e512007-04-18 16:15:28 -0400394 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500395 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400396 }
Chris Mason02217ed2007-03-02 16:08:05 -0500397 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400398 l = btrfs_buffer_leaf(path->nodes[0]);
399 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400400 refs = btrfs_extent_refs(item);
401 btrfs_set_extent_refs(item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400402 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500403
Chris Mason5caf2a02007-04-02 11:20:42 -0400404 btrfs_release_path(root->fs_info->extent_root, path);
405 btrfs_free_path(path);
Chris Mason9f5fae22007-03-20 14:38:32 -0400406 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400407 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason02217ed2007-03-02 16:08:05 -0500408 return 0;
409}
410
Chris Masonb18c6682007-04-17 13:26:50 -0400411static int lookup_extent_ref(struct btrfs_trans_handle *trans,
412 struct btrfs_root *root, u64 blocknr,
413 u64 num_blocks, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500414{
Chris Mason5caf2a02007-04-02 11:20:42 -0400415 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500416 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400417 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -0400418 struct btrfs_leaf *l;
419 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400420
421 path = btrfs_alloc_path();
422 btrfs_init_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500423 key.objectid = blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400424 key.offset = num_blocks;
Chris Mason62e27492007-03-15 12:56:47 -0400425 key.flags = 0;
426 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400428 0, 0);
Chris Masona28ec192007-03-06 20:08:01 -0500429 if (ret != 0)
430 BUG();
Chris Mason5caf2a02007-04-02 11:20:42 -0400431 l = btrfs_buffer_leaf(path->nodes[0]);
432 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400433 *refs = btrfs_extent_refs(item);
Chris Mason5caf2a02007-04-02 11:20:42 -0400434 btrfs_release_path(root->fs_info->extent_root, path);
435 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500436 return 0;
437}
438
Chris Masonc5739bb2007-04-10 09:27:04 -0400439int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
441{
Chris Masonb18c6682007-04-17 13:26:50 -0400442 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400443}
444
Chris Masone089f052007-03-16 16:20:31 -0400445int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone20d96d2007-03-22 12:13:20 -0400446 struct buffer_head *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500447{
448 u64 blocknr;
Chris Masone20d96d2007-03-22 12:13:20 -0400449 struct btrfs_node *buf_node;
Chris Mason6407bf62007-03-27 06:33:00 -0400450 struct btrfs_leaf *buf_leaf;
451 struct btrfs_disk_key *key;
452 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500453 int i;
Chris Mason6407bf62007-03-27 06:33:00 -0400454 int leaf;
455 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500456
Chris Mason3768f362007-03-13 16:47:54 -0400457 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500458 return 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400459 buf_node = btrfs_buffer_node(buf);
Chris Mason6407bf62007-03-27 06:33:00 -0400460 leaf = btrfs_is_leaf(buf_node);
461 buf_leaf = btrfs_buffer_leaf(buf);
Chris Masone20d96d2007-03-22 12:13:20 -0400462 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
Chris Mason6407bf62007-03-27 06:33:00 -0400463 if (leaf) {
Chris Mason3a686372007-05-24 13:35:57 -0400464 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -0400465 key = &buf_leaf->items[i].key;
466 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
467 continue;
468 fi = btrfs_item_ptr(buf_leaf, i,
469 struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -0400470 if (btrfs_file_extent_type(fi) ==
471 BTRFS_FILE_EXTENT_INLINE)
472 continue;
Chris Mason3a686372007-05-24 13:35:57 -0400473 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
474 if (disk_blocknr == 0)
475 continue;
476 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -0400477 btrfs_file_extent_disk_num_blocks(fi));
478 BUG_ON(ret);
479 } else {
480 blocknr = btrfs_node_blockptr(buf_node, i);
Chris Masonb18c6682007-04-17 13:26:50 -0400481 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
Chris Mason6407bf62007-03-27 06:33:00 -0400482 BUG_ON(ret);
483 }
Chris Mason02217ed2007-03-02 16:08:05 -0500484 }
485 return 0;
486}
487
Chris Mason9078a3e2007-04-26 16:46:15 -0400488static int write_one_cache_group(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct btrfs_path *path,
491 struct btrfs_block_group_cache *cache)
492{
493 int ret;
494 int pending_ret;
495 struct btrfs_root *extent_root = root->fs_info->extent_root;
496 struct btrfs_block_group_item *bi;
497 struct btrfs_key ins;
498
Chris Masonfbdc7622007-05-30 10:22:12 -0400499 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400500 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
501 BUG_ON(ret);
502 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
503 struct btrfs_block_group_item);
504 memcpy(bi, &cache->item, sizeof(*bi));
505 mark_buffer_dirty(path->nodes[0]);
506 btrfs_release_path(extent_root, path);
507
508 finish_current_insert(trans, extent_root);
509 pending_ret = del_pending_extents(trans, extent_root);
510 if (ret)
511 return ret;
512 if (pending_ret)
513 return pending_ret;
Chris Masonbe744172007-05-06 10:15:01 -0400514 if (cache->data)
515 cache->last_alloc = cache->first_free;
Chris Mason9078a3e2007-04-26 16:46:15 -0400516 return 0;
517
518}
519
Chris Masonbe744172007-05-06 10:15:01 -0400520static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -0400523{
524 struct btrfs_block_group_cache *cache[8];
525 int ret;
526 int err = 0;
527 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400528 int i;
529 struct btrfs_path *path;
530
531 path = btrfs_alloc_path();
532 if (!path)
533 return -ENOMEM;
534
535 while(1) {
536 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
537 0, ARRAY_SIZE(cache),
538 BTRFS_BLOCK_GROUP_DIRTY);
539 if (!ret)
540 break;
541 for (i = 0; i < ret; i++) {
542 radix_tree_tag_clear(radix, cache[i]->key.objectid +
543 cache[i]->key.offset - 1,
544 BTRFS_BLOCK_GROUP_DIRTY);
545 err = write_one_cache_group(trans, root,
546 path, cache[i]);
547 if (err)
548 werr = err;
549 }
550 }
551 btrfs_free_path(path);
552 return werr;
553}
554
Chris Masonbe744172007-05-06 10:15:01 -0400555int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root)
557{
558 int ret;
559 int ret2;
560 ret = write_dirty_block_radix(trans, root,
561 &root->fs_info->block_group_radix);
562 ret2 = write_dirty_block_radix(trans, root,
563 &root->fs_info->block_group_data_radix);
564 if (ret)
565 return ret;
566 if (ret2)
567 return ret2;
568 return 0;
569}
570
Chris Mason9078a3e2007-04-26 16:46:15 -0400571static int update_block_group(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root,
Chris Mason1e2677e2007-05-29 16:52:18 -0400573 u64 blocknr, u64 num, int alloc, int mark_free,
574 int data)
Chris Mason9078a3e2007-04-26 16:46:15 -0400575{
576 struct btrfs_block_group_cache *cache;
577 struct btrfs_fs_info *info = root->fs_info;
578 u64 total = num;
579 u64 old_val;
580 u64 block_in_group;
Chris Masone37c9e62007-05-09 20:13:14 -0400581 u64 i;
Chris Mason1e2677e2007-05-29 16:52:18 -0400582 int ret;
Chris Mason3e1ad542007-05-07 20:03:49 -0400583
Chris Mason9078a3e2007-04-26 16:46:15 -0400584 while(total) {
Chris Mason5276aed2007-06-11 21:33:38 -0400585 cache = btrfs_lookup_block_group(info, blocknr);
Chris Mason3e1ad542007-05-07 20:03:49 -0400586 if (!cache) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400587 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
588 blocknr);
Chris Mason9078a3e2007-04-26 16:46:15 -0400589 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -0400590 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400591 block_in_group = blocknr - cache->key.objectid;
592 WARN_ON(block_in_group > cache->key.offset);
Chris Mason3e1ad542007-05-07 20:03:49 -0400593 radix_tree_tag_set(cache->radix, cache->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -0400594 cache->key.offset - 1,
Chris Mason9078a3e2007-04-26 16:46:15 -0400595 BTRFS_BLOCK_GROUP_DIRTY);
596
597 old_val = btrfs_block_group_used(&cache->item);
598 num = min(total, cache->key.offset - block_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -0400599 if (alloc) {
Chris Masoncd1bc462007-04-27 10:08:34 -0400600 if (blocknr > cache->last_alloc)
601 cache->last_alloc = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400602 if (!cache->data) {
603 for (i = 0; i < num; i++) {
604 clear_radix_bit(&info->extent_map_radix,
605 blocknr + i);
606 }
607 }
Chris Mason1e2677e2007-05-29 16:52:18 -0400608 if (cache->data != data &&
Chris Mason84f54cf2007-06-12 07:43:08 -0400609 old_val < (cache->key.offset >> 1)) {
Chris Mason1e2677e2007-05-29 16:52:18 -0400610 cache->data = data;
611 radix_tree_delete(cache->radix,
612 cache->key.objectid +
613 cache->key.offset - 1);
614
615 if (data) {
616 cache->radix =
617 &info->block_group_data_radix;
618 cache->item.flags |=
619 BTRFS_BLOCK_GROUP_DATA;
620 } else {
621 cache->radix = &info->block_group_radix;
622 cache->item.flags &=
623 ~BTRFS_BLOCK_GROUP_DATA;
624 }
625 ret = radix_tree_insert(cache->radix,
626 cache->key.objectid +
627 cache->key.offset - 1,
628 (void *)cache);
629 }
630 old_val += num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400631 } else {
Chris Mason9078a3e2007-04-26 16:46:15 -0400632 old_val -= num;
Chris Masoncd1bc462007-04-27 10:08:34 -0400633 if (blocknr < cache->first_free)
634 cache->first_free = blocknr;
Chris Masone37c9e62007-05-09 20:13:14 -0400635 if (!cache->data && mark_free) {
636 for (i = 0; i < num; i++) {
637 set_radix_bit(&info->extent_map_radix,
638 blocknr + i);
639 }
640 }
Chris Mason84f54cf2007-06-12 07:43:08 -0400641 if (old_val < (cache->key.offset >> 1) &&
642 old_val + num >= (cache->key.offset >> 1)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400643 radix_tree_tag_set(cache->radix,
644 cache->key.objectid +
645 cache->key.offset - 1,
646 BTRFS_BLOCK_GROUP_AVAIL);
647 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400648 }
Chris Mason9078a3e2007-04-26 16:46:15 -0400649 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masone37c9e62007-05-09 20:13:14 -0400650 total -= num;
651 blocknr += num;
Chris Mason9078a3e2007-04-26 16:46:15 -0400652 }
653 return 0;
654}
655
Chris Masonbe08c1b2007-05-03 09:06:49 -0400656static int try_remove_page(struct address_space *mapping, unsigned long index)
657{
658 int ret;
659 ret = invalidate_mapping_pages(mapping, index, index);
660 return ret;
661}
662
Chris Masone089f052007-03-16 16:20:31 -0400663int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
664 btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -0500665{
Chris Mason8ef97622007-03-26 10:15:30 -0400666 unsigned long gang[8];
Chris Masonbe08c1b2007-05-03 09:06:49 -0400667 struct inode *btree_inode = root->fs_info->btree_inode;
Chris Masonbe744172007-05-06 10:15:01 -0400668 struct btrfs_block_group_cache *block_group;
Chris Mason88fd1462007-03-16 08:56:18 -0400669 u64 first = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500670 int ret;
671 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400672 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
Chris Masone37c9e62007-05-09 20:13:14 -0400673 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
Chris Masona28ec192007-03-06 20:08:01 -0500674
675 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400676 ret = find_first_radix_bit(pinned_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400677 ARRAY_SIZE(gang));
Chris Masona28ec192007-03-06 20:08:01 -0500678 if (!ret)
679 break;
Chris Mason88fd1462007-03-16 08:56:18 -0400680 if (!first)
Chris Mason8ef97622007-03-26 10:15:30 -0400681 first = gang[0];
Chris Mason0579da42007-03-07 16:15:30 -0500682 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400683 clear_radix_bit(pinned_radix, gang[i]);
Chris Mason5276aed2007-06-11 21:33:38 -0400684 block_group = btrfs_lookup_block_group(root->fs_info,
685 gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400686 if (block_group) {
687 WARN_ON(block_group->pinned == 0);
688 block_group->pinned--;
689 if (gang[i] < block_group->last_alloc)
690 block_group->last_alloc = gang[i];
Chris Masone37c9e62007-05-09 20:13:14 -0400691 if (gang[i] < block_group->last_prealloc)
692 block_group->last_prealloc = gang[i];
693 if (!block_group->data)
694 set_radix_bit(extent_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400695 }
Chris Masonbe08c1b2007-05-03 09:06:49 -0400696 try_remove_page(btree_inode->i_mapping,
697 gang[i] << (PAGE_CACHE_SHIFT -
698 btree_inode->i_blkbits));
Chris Mason0579da42007-03-07 16:15:30 -0500699 }
Chris Masona28ec192007-03-06 20:08:01 -0500700 }
701 return 0;
702}
703
Chris Masone089f052007-03-16 16:20:31 -0400704static int finish_current_insert(struct btrfs_trans_handle *trans, struct
705 btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -0500706{
Chris Masone2fa7222007-03-12 16:22:34 -0400707 struct btrfs_key ins;
Chris Mason234b63a2007-03-13 10:46:10 -0400708 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -0500709 int i;
710 int ret;
Chris Mason1261ec42007-03-20 20:35:03 -0400711 u64 super_blocks_used;
712 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason037e6392007-03-07 11:50:24 -0500713
Chris Masoncf27e1e2007-03-13 09:49:06 -0400714 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason037e6392007-03-07 11:50:24 -0500715 ins.offset = 1;
716 ins.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400717 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5d0c3e62007-04-23 17:01:05 -0400718 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
Chris Mason037e6392007-03-07 11:50:24 -0500719
Chris Masonf2458e12007-04-25 15:52:25 -0400720 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
721 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
Chris Mason1261ec42007-03-20 20:35:03 -0400722 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
723 btrfs_set_super_blocks_used(info->disk_super,
724 super_blocks_used + 1);
Chris Masone089f052007-03-16 16:20:31 -0400725 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
726 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -0500727 BUG_ON(ret);
728 }
Chris Masonf2458e12007-04-25 15:52:25 -0400729 extent_root->fs_info->extent_tree_insert_nr = 0;
730 extent_root->fs_info->extent_tree_prealloc_nr = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500731 return 0;
732}
733
Chris Mason8ef97622007-03-26 10:15:30 -0400734static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -0400735{
736 int err;
Chris Mason78fae272007-03-25 11:35:08 -0400737 struct btrfs_header *header;
Chris Mason8ef97622007-03-26 10:15:30 -0400738 struct buffer_head *bh;
Chris Mason78fae272007-03-25 11:35:08 -0400739
Chris Masonf4b9aa82007-03-27 11:05:53 -0400740 if (!pending) {
Chris Masond98237b2007-03-28 13:57:48 -0400741 bh = btrfs_find_tree_block(root, blocknr);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400742 if (bh) {
743 if (buffer_uptodate(bh)) {
744 u64 transid =
745 root->fs_info->running_transaction->transid;
746 header = btrfs_buffer_header(bh);
747 if (btrfs_header_generation(header) ==
748 transid) {
749 btrfs_block_release(root, bh);
750 return 0;
751 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400752 }
Chris Masond6025572007-03-30 14:27:56 -0400753 btrfs_block_release(root, bh);
Chris Mason8ef97622007-03-26 10:15:30 -0400754 }
Chris Mason8ef97622007-03-26 10:15:30 -0400755 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400756 if (!err) {
757 struct btrfs_block_group_cache *cache;
Chris Mason5276aed2007-06-11 21:33:38 -0400758 cache = btrfs_lookup_block_group(root->fs_info,
759 blocknr);
Chris Masonbe744172007-05-06 10:15:01 -0400760 if (cache)
761 cache->pinned++;
762 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400763 } else {
764 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
765 }
Chris Masonbe744172007-05-06 10:15:01 -0400766 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400767 return 0;
768}
769
Chris Masona28ec192007-03-06 20:08:01 -0500770/*
771 * remove an extent from the root, returns 0 on success
772 */
Chris Masone089f052007-03-16 16:20:31 -0400773static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone37c9e62007-05-09 20:13:14 -0400774 *root, u64 blocknr, u64 num_blocks, int pin,
775 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -0500776{
Chris Mason5caf2a02007-04-02 11:20:42 -0400777 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400778 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -0400779 struct btrfs_fs_info *info = root->fs_info;
780 struct btrfs_root *extent_root = info->extent_root;
Chris Masona28ec192007-03-06 20:08:01 -0500781 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -0400782 struct btrfs_extent_item *ei;
Chris Masone2fa7222007-03-12 16:22:34 -0400783 struct btrfs_key ins;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400784 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500785
Chris Masona28ec192007-03-06 20:08:01 -0500786 key.objectid = blocknr;
787 key.flags = 0;
Chris Mason62e27492007-03-15 12:56:47 -0400788 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masona28ec192007-03-06 20:08:01 -0500789 key.offset = num_blocks;
790
Chris Masonfbdc7622007-05-30 10:22:12 -0400791 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400792 path = btrfs_alloc_path();
793 BUG_ON(!path);
794 btrfs_init_path(path);
Chris Mason5f26f772007-04-05 10:38:44 -0400795
Chris Mason5caf2a02007-04-02 11:20:42 -0400796 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500797 if (ret) {
Chris Mason2e635a22007-03-21 11:12:56 -0400798 printk("failed to find %Lu\n", key.objectid);
Chris Mason234b63a2007-03-13 10:46:10 -0400799 btrfs_print_tree(extent_root, extent_root->node);
Chris Mason2e635a22007-03-21 11:12:56 -0400800 printk("failed to find %Lu\n", key.objectid);
Chris Masona28ec192007-03-06 20:08:01 -0500801 BUG();
802 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400803 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason123abc82007-03-14 14:14:43 -0400804 struct btrfs_extent_item);
Chris Masona28ec192007-03-06 20:08:01 -0500805 BUG_ON(ei->refs == 0);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400806 refs = btrfs_extent_refs(ei) - 1;
807 btrfs_set_extent_refs(ei, refs);
Chris Mason5caf2a02007-04-02 11:20:42 -0400808 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masoncf27e1e2007-03-13 09:49:06 -0400809 if (refs == 0) {
Chris Mason1261ec42007-03-20 20:35:03 -0400810 u64 super_blocks_used;
Chris Mason78fae272007-03-25 11:35:08 -0400811
812 if (pin) {
Chris Mason8ef97622007-03-26 10:15:30 -0400813 ret = pin_down_block(root, blocknr, 0);
Chris Mason78fae272007-03-25 11:35:08 -0400814 BUG_ON(ret);
815 }
816
Chris Mason1261ec42007-03-20 20:35:03 -0400817 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
818 btrfs_set_super_blocks_used(info->disk_super,
819 super_blocks_used - num_blocks);
Chris Mason5caf2a02007-04-02 11:20:42 -0400820 ret = btrfs_del_item(trans, extent_root, path);
Chris Masona28ec192007-03-06 20:08:01 -0500821 if (ret)
822 BUG();
Chris Masone37c9e62007-05-09 20:13:14 -0400823 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
Chris Mason1e2677e2007-05-29 16:52:18 -0400824 mark_free, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -0400825 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -0500826 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400827 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -0400828 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -0500829 return ret;
830}
831
832/*
Chris Masonfec577f2007-02-26 10:40:21 -0500833 * find all the blocks marked as pending in the radix tree and remove
834 * them from the extent map
835 */
Chris Masone089f052007-03-16 16:20:31 -0400836static int del_pending_extents(struct btrfs_trans_handle *trans, struct
837 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -0500838{
839 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400840 int wret;
841 int err = 0;
Chris Mason8ef97622007-03-26 10:15:30 -0400842 unsigned long gang[4];
Chris Masonfec577f2007-02-26 10:40:21 -0500843 int i;
Chris Mason8ef97622007-03-26 10:15:30 -0400844 struct radix_tree_root *pending_radix;
845 struct radix_tree_root *pinned_radix;
Chris Masonbe744172007-05-06 10:15:01 -0400846 struct btrfs_block_group_cache *cache;
Chris Mason8ef97622007-03-26 10:15:30 -0400847
848 pending_radix = &extent_root->fs_info->pending_del_radix;
849 pinned_radix = &extent_root->fs_info->pinned_radix;
Chris Masonfec577f2007-02-26 10:40:21 -0500850
851 while(1) {
Chris Masone37c9e62007-05-09 20:13:14 -0400852 ret = find_first_radix_bit(pending_radix, gang, 0,
Chris Mason8ef97622007-03-26 10:15:30 -0400853 ARRAY_SIZE(gang));
Chris Masonfec577f2007-02-26 10:40:21 -0500854 if (!ret)
855 break;
856 for (i = 0; i < ret; i++) {
Chris Mason8ef97622007-03-26 10:15:30 -0400857 wret = set_radix_bit(pinned_radix, gang[i]);
Chris Masonbe744172007-05-06 10:15:01 -0400858 if (wret == 0) {
Chris Mason5276aed2007-06-11 21:33:38 -0400859 cache =
860 btrfs_lookup_block_group(extent_root->fs_info,
Chris Masonbe744172007-05-06 10:15:01 -0400861 gang[i]);
862 if (cache)
863 cache->pinned++;
864 }
865 if (wret < 0) {
866 printk(KERN_CRIT "set_radix_bit, err %d\n",
867 wret);
868 BUG_ON(wret < 0);
869 }
Chris Mason8ef97622007-03-26 10:15:30 -0400870 wret = clear_radix_bit(pending_radix, gang[i]);
871 BUG_ON(wret);
Chris Masond5719762007-03-23 10:01:08 -0400872 wret = __free_extent(trans, extent_root,
Chris Masone37c9e62007-05-09 20:13:14 -0400873 gang[i], 1, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400874 if (wret)
875 err = wret;
Chris Masonfec577f2007-02-26 10:40:21 -0500876 }
877 }
Chris Masone20d96d2007-03-22 12:13:20 -0400878 return err;
Chris Masonfec577f2007-02-26 10:40:21 -0500879}
880
881/*
882 * remove an extent from the root, returns 0 on success
883 */
Chris Masone089f052007-03-16 16:20:31 -0400884int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
885 *root, u64 blocknr, u64 num_blocks, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -0500886{
Chris Mason9f5fae22007-03-20 14:38:32 -0400887 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -0500888 int pending_ret;
889 int ret;
Chris Masona28ec192007-03-06 20:08:01 -0500890
891 if (root == extent_root) {
Chris Mason8ef97622007-03-26 10:15:30 -0400892 pin_down_block(root, blocknr, 1);
Chris Masona28ec192007-03-06 20:08:01 -0500893 return 0;
894 }
Chris Masone37c9e62007-05-09 20:13:14 -0400895 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400896 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -0500897 return ret ? ret : pending_ret;
898}
899
900/*
901 * walks the btree of allocated extents and find a hole of a given size.
902 * The key ins is changed to record the hole:
903 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -0400904 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -0500905 * ins->offset == number of blocks
906 * Any available blocks before search_start are skipped.
907 */
Chris Masone089f052007-03-16 16:20:31 -0400908static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
909 *orig_root, u64 num_blocks, u64 search_start, u64
Chris Masonfbdc7622007-05-30 10:22:12 -0400910 search_end, u64 hint_block,
911 struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -0500912{
Chris Mason5caf2a02007-04-02 11:20:42 -0400913 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -0400914 struct btrfs_key key;
Chris Masonfec577f2007-02-26 10:40:21 -0500915 int ret;
916 u64 hole_size = 0;
917 int slot = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400918 u64 last_block = 0;
Chris Mason037e6392007-03-07 11:50:24 -0500919 u64 test_block;
Chris Masonbe744172007-05-06 10:15:01 -0400920 u64 orig_search_start = search_start;
Chris Masonfec577f2007-02-26 10:40:21 -0500921 int start_found;
Chris Mason234b63a2007-03-13 10:46:10 -0400922 struct btrfs_leaf *l;
Chris Mason9f5fae22007-03-20 14:38:32 -0400923 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -0400924 struct btrfs_fs_info *info = root->fs_info;
Chris Mason0579da42007-03-07 16:15:30 -0500925 int total_needed = num_blocks;
Chris Masonf2458e12007-04-25 15:52:25 -0400926 int total_found = 0;
927 int fill_prealloc = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400928 int level;
Chris Masonbe08c1b2007-05-03 09:06:49 -0400929 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400930 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -0400931 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400932 u64 limit;
Chris Masonfec577f2007-02-26 10:40:21 -0500933
Chris Masonb1a4d962007-04-04 15:27:52 -0400934 path = btrfs_alloc_path();
935 ins->flags = 0;
936 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
937
Chris Masone20d96d2007-03-22 12:13:20 -0400938 level = btrfs_header_level(btrfs_buffer_header(root->node));
Chris Masonf2458e12007-04-25 15:52:25 -0400939 if (num_blocks == 0) {
940 fill_prealloc = 1;
941 num_blocks = 1;
Chris Mason308535a2007-04-28 15:17:08 -0400942 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
Chris Masonf2458e12007-04-25 15:52:25 -0400943 }
Chris Mason3e1ad542007-05-07 20:03:49 -0400944 if (search_end == (u64)-1)
945 search_end = btrfs_super_total_blocks(info->disk_super);
Chris Masonfbdc7622007-05-30 10:22:12 -0400946 if (hint_block) {
Chris Mason5276aed2007-06-11 21:33:38 -0400947 block_group = btrfs_lookup_block_group(info, hint_block);
Chris Masonbe744172007-05-06 10:15:01 -0400948 block_group = btrfs_find_block_group(root, block_group,
Chris Masonfbdc7622007-05-30 10:22:12 -0400949 hint_block, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400950 } else {
951 block_group = btrfs_find_block_group(root,
952 trans->block_group, 0,
Chris Masonde428b62007-05-18 13:28:27 -0400953 data, 1);
Chris Masonbe744172007-05-06 10:15:01 -0400954 }
955
956check_failed:
Chris Mason1e2677e2007-05-29 16:52:18 -0400957 if (!block_group->data)
Chris Masone37c9e62007-05-09 20:13:14 -0400958 search_start = find_search_start(root, &block_group,
959 search_start, total_needed);
Chris Masonfbdc7622007-05-30 10:22:12 -0400960 else if (!full_scan)
Chris Masone37c9e62007-05-09 20:13:14 -0400961 search_start = max(block_group->last_alloc, search_start);
962
Chris Mason5caf2a02007-04-02 11:20:42 -0400963 btrfs_init_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -0500964 ins->objectid = search_start;
965 ins->offset = 0;
Chris Masonfec577f2007-02-26 10:40:21 -0500966 start_found = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400967
Chris Mason5caf2a02007-04-02 11:20:42 -0400968 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
Chris Mason0f70abe2007-02-28 16:46:22 -0500969 if (ret < 0)
970 goto error;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500971
Chris Masone37c9e62007-05-09 20:13:14 -0400972 if (path->slots[0] > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400973 path->slots[0]--;
Chris Masone37c9e62007-05-09 20:13:14 -0400974 }
975
976 l = btrfs_buffer_leaf(path->nodes[0]);
977 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
978 /*
979 * a rare case, go back one key if we hit a block group item
980 * instead of an extent item
981 */
982 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
983 key.objectid + key.offset >= search_start) {
984 ins->objectid = key.objectid;
985 ins->offset = key.offset - 1;
986 btrfs_release_path(root, path);
987 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
988 if (ret < 0)
989 goto error;
990
991 if (path->slots[0] > 0) {
992 path->slots[0]--;
993 }
994 }
Chris Mason0579da42007-03-07 16:15:30 -0500995
Chris Masonfec577f2007-02-26 10:40:21 -0500996 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400997 l = btrfs_buffer_leaf(path->nodes[0]);
998 slot = path->slots[0];
Chris Mason7518a232007-03-12 12:01:18 -0400999 if (slot >= btrfs_header_nritems(&l->header)) {
Chris Masonf2458e12007-04-25 15:52:25 -04001000 if (fill_prealloc) {
1001 info->extent_tree_prealloc_nr = 0;
1002 total_found = 0;
1003 }
Chris Masonde428b62007-05-18 13:28:27 -04001004 if (start_found)
1005 limit = last_block +
Chris Mason84f54cf2007-06-12 07:43:08 -04001006 (block_group->key.offset >> 1);
Chris Masonde428b62007-05-18 13:28:27 -04001007 else
1008 limit = search_start +
Chris Mason84f54cf2007-06-12 07:43:08 -04001009 (block_group->key.offset >> 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001010 ret = btrfs_next_leaf(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001011 if (ret == 0)
1012 continue;
Chris Mason0f70abe2007-02-28 16:46:22 -05001013 if (ret < 0)
1014 goto error;
Chris Masonfec577f2007-02-26 10:40:21 -05001015 if (!start_found) {
1016 ins->objectid = search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001017 ins->offset = search_end - search_start;
Chris Masonfec577f2007-02-26 10:40:21 -05001018 start_found = 1;
1019 goto check_pending;
1020 }
1021 ins->objectid = last_block > search_start ?
1022 last_block : search_start;
Chris Mason3e1ad542007-05-07 20:03:49 -04001023 ins->offset = search_end - ins->objectid;
Chris Masonfec577f2007-02-26 10:40:21 -05001024 goto check_pending;
1025 }
Chris Masone37c9e62007-05-09 20:13:14 -04001026
Chris Masone2fa7222007-03-12 16:22:34 -04001027 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
Chris Masone37c9e62007-05-09 20:13:14 -04001028 if (key.objectid >= search_start && key.objectid > last_block &&
1029 start_found) {
1030 if (last_block < search_start)
1031 last_block = search_start;
1032 hole_size = key.objectid - last_block;
1033 if (hole_size >= num_blocks) {
1034 ins->objectid = last_block;
1035 ins->offset = hole_size;
1036 goto check_pending;
Chris Mason0579da42007-03-07 16:15:30 -05001037 }
Chris Masonfec577f2007-02-26 10:40:21 -05001038 }
Chris Masone37c9e62007-05-09 20:13:14 -04001039
1040 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1041 goto next;
1042
Chris Mason0579da42007-03-07 16:15:30 -05001043 start_found = 1;
Chris Masone2fa7222007-03-12 16:22:34 -04001044 last_block = key.objectid + key.offset;
Chris Masonfbdc7622007-05-30 10:22:12 -04001045 if (!full_scan && last_block >= block_group->key.objectid +
Chris Masonbe744172007-05-06 10:15:01 -04001046 block_group->key.offset) {
1047 btrfs_release_path(root, path);
1048 search_start = block_group->key.objectid +
1049 block_group->key.offset * 2;
1050 goto new_group;
1051 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001052next:
Chris Mason5caf2a02007-04-02 11:20:42 -04001053 path->slots[0]++;
Chris Masonde428b62007-05-18 13:28:27 -04001054 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05001055 }
1056 // FIXME -ENOSPC
1057check_pending:
1058 /* we have to make sure we didn't find an extent that has already
1059 * been allocated by the map tree or the original allocation
1060 */
Chris Mason5caf2a02007-04-02 11:20:42 -04001061 btrfs_release_path(root, path);
Chris Masonfec577f2007-02-26 10:40:21 -05001062 BUG_ON(ins->objectid < search_start);
Chris Masone37c9e62007-05-09 20:13:14 -04001063
Chris Mason3e1ad542007-05-07 20:03:49 -04001064 if (ins->objectid + num_blocks >= search_end) {
Chris Masonfbdc7622007-05-30 10:22:12 -04001065 if (full_scan) {
1066 ret = -ENOSPC;
1067 goto error;
1068 }
Chris Masonbe744172007-05-06 10:15:01 -04001069 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001070 if (wrapped)
1071 full_scan = 1;
1072 else
1073 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001074 goto new_group;
Chris Mason06a2f9f2007-04-28 08:48:10 -04001075 }
Chris Mason037e6392007-03-07 11:50:24 -05001076 for (test_block = ins->objectid;
Chris Masonf2458e12007-04-25 15:52:25 -04001077 test_block < ins->objectid + num_blocks; test_block++) {
1078 if (test_radix_bit(&info->pinned_radix, test_block)) {
Chris Mason037e6392007-03-07 11:50:24 -05001079 search_start = test_block + 1;
Chris Masonbe744172007-05-06 10:15:01 -04001080 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001081 }
1082 }
Chris Masonf2458e12007-04-25 15:52:25 -04001083 if (!fill_prealloc && info->extent_tree_insert_nr) {
1084 u64 last =
1085 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1086 if (ins->objectid + num_blocks >
1087 info->extent_tree_insert[0] &&
1088 ins->objectid <= last) {
1089 search_start = last + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001090 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001091 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001092 }
1093 }
1094 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1095 u64 first =
1096 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1097 if (ins->objectid + num_blocks > first &&
1098 ins->objectid <= info->extent_tree_prealloc[0]) {
1099 search_start = info->extent_tree_prealloc[0] + 1;
Chris Masone37c9e62007-05-09 20:13:14 -04001100 WARN_ON(!full_scan);
Chris Masonbe744172007-05-06 10:15:01 -04001101 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001102 }
1103 }
1104 if (fill_prealloc) {
1105 int nr;
1106 test_block = ins->objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001107 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1108 leaf_range(root)) {
1109 total_found = 0;
1110 info->extent_tree_prealloc_nr = total_found;
1111 }
Chris Masonf2458e12007-04-25 15:52:25 -04001112 while(test_block < ins->objectid + ins->offset &&
1113 total_found < total_needed) {
1114 nr = total_needed - total_found - 1;
1115 BUG_ON(nr < 0);
Chris Masoncd1bc462007-04-27 10:08:34 -04001116 info->extent_tree_prealloc[nr] = test_block;
Chris Masonf2458e12007-04-25 15:52:25 -04001117 total_found++;
1118 test_block++;
1119 }
1120 if (total_found < total_needed) {
1121 search_start = test_block;
Chris Masonbe744172007-05-06 10:15:01 -04001122 goto new_group;
Chris Masonf2458e12007-04-25 15:52:25 -04001123 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001124 info->extent_tree_prealloc_nr = total_found;
Chris Masonf2458e12007-04-25 15:52:25 -04001125 }
Chris Masone37c9e62007-05-09 20:13:14 -04001126 if (!data) {
Chris Mason5276aed2007-06-11 21:33:38 -04001127 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Masone37c9e62007-05-09 20:13:14 -04001128 if (block_group) {
1129 if (fill_prealloc)
1130 block_group->last_prealloc =
1131 info->extent_tree_prealloc[total_needed-1];
1132 else
1133 trans->block_group = block_group;
1134 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001135 }
Chris Mason037e6392007-03-07 11:50:24 -05001136 ins->offset = num_blocks;
Chris Mason5caf2a02007-04-02 11:20:42 -04001137 btrfs_free_path(path);
Chris Masonfec577f2007-02-26 10:40:21 -05001138 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001139
1140new_group:
Chris Mason3e1ad542007-05-07 20:03:49 -04001141 if (search_start + num_blocks >= search_end) {
Chris Masonbe744172007-05-06 10:15:01 -04001142 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001143 if (full_scan) {
1144 ret = -ENOSPC;
1145 goto error;
1146 }
1147 if (wrapped)
1148 full_scan = 1;
1149 else
1150 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001151 }
Chris Mason5276aed2007-06-11 21:33:38 -04001152 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001153 cond_resched();
Chris Masonbe744172007-05-06 10:15:01 -04001154 if (!full_scan)
1155 block_group = btrfs_find_block_group(root, block_group,
Chris Masonde428b62007-05-18 13:28:27 -04001156 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001157 goto check_failed;
1158
Chris Mason0f70abe2007-02-28 16:46:22 -05001159error:
Chris Mason5caf2a02007-04-02 11:20:42 -04001160 btrfs_release_path(root, path);
1161 btrfs_free_path(path);
Chris Mason0f70abe2007-02-28 16:46:22 -05001162 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001163}
Chris Masonfec577f2007-02-26 10:40:21 -05001164/*
Chris Masonfec577f2007-02-26 10:40:21 -05001165 * finds a free extent and does all the dirty work required for allocation
1166 * returns the key for the extent through ins, and a tree buffer for
1167 * the first block of the extent through buf.
1168 *
1169 * returns 0 if everything worked, non-zero otherwise.
1170 */
Chris Mason4d775672007-04-20 20:23:12 -04001171int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root, u64 owner,
Chris Masonfbdc7622007-05-30 10:22:12 -04001173 u64 num_blocks, u64 hint_block,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001174 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001175{
1176 int ret;
1177 int pending_ret;
Chris Mason1261ec42007-03-20 20:35:03 -04001178 u64 super_blocks_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001179 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04001180 struct btrfs_fs_info *info = root->fs_info;
1181 struct btrfs_root *extent_root = info->extent_root;
Chris Mason234b63a2007-03-13 10:46:10 -04001182 struct btrfs_extent_item extent_item;
Chris Masonf2458e12007-04-25 15:52:25 -04001183 struct btrfs_key prealloc_key;
Chris Mason037e6392007-03-07 11:50:24 -05001184
Chris Masoncf27e1e2007-03-13 09:49:06 -04001185 btrfs_set_extent_refs(&extent_item, 1);
Chris Mason4d775672007-04-20 20:23:12 -04001186 btrfs_set_extent_owner(&extent_item, owner);
Chris Masonfec577f2007-02-26 10:40:21 -05001187
Chris Mason037e6392007-03-07 11:50:24 -05001188 if (root == extent_root) {
Chris Masonf2458e12007-04-25 15:52:25 -04001189 int nr;
1190 BUG_ON(info->extent_tree_prealloc_nr == 0);
Chris Mason037e6392007-03-07 11:50:24 -05001191 BUG_ON(num_blocks != 1);
Chris Mason037e6392007-03-07 11:50:24 -05001192 ins->offset = 1;
Chris Masonf2458e12007-04-25 15:52:25 -04001193 info->extent_tree_prealloc_nr--;
1194 nr = info->extent_tree_prealloc_nr;
1195 ins->objectid = info->extent_tree_prealloc[nr];
1196 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1197 ins->objectid;
Chris Mason9078a3e2007-04-26 16:46:15 -04001198 ret = update_block_group(trans, root,
Chris Mason1e2677e2007-05-29 16:52:18 -04001199 ins->objectid, ins->offset, 1, 0, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001200 BUG_ON(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001201 return 0;
1202 }
Chris Masone37c9e62007-05-09 20:13:14 -04001203
1204 /*
1205 * if we're doing a data allocation, preallocate room in the
1206 * extent tree first. This way the extent tree blocks end up
1207 * in the correct block group.
1208 */
1209 if (data) {
Chris Masonde428b62007-05-18 13:28:27 -04001210 ret = find_free_extent(trans, root, 0, 0,
Chris Masonfbdc7622007-05-30 10:22:12 -04001211 search_end, 0, &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001212 if (ret) {
1213 return ret;
1214 }
1215 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1216 int nr = info->extent_tree_prealloc_nr;
1217 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1218 } else {
1219 search_start = info->extent_tree_prealloc[0] + 1;
1220 }
1221 }
Chris Masonfbdc7622007-05-30 10:22:12 -04001222 if (hint_block < search_start)
1223 hint_block = search_start;
Chris Masonf2458e12007-04-25 15:52:25 -04001224 /* do the real allocation */
Chris Masone089f052007-03-16 16:20:31 -04001225 ret = find_free_extent(trans, root, num_blocks, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001226 search_end, hint_block, ins, data);
Chris Masone37c9e62007-05-09 20:13:14 -04001227 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001228 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001229 }
Chris Masonfec577f2007-02-26 10:40:21 -05001230
Chris Masone37c9e62007-05-09 20:13:14 -04001231 /*
1232 * if we're doing a metadata allocation, preallocate space in the
1233 * extent tree second. This way, we don't create a tiny hole
1234 * in the allocation map between any unused preallocation blocks
1235 * and the metadata block we're actually allocating. On disk,
1236 * it'll go:
1237 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1238 * The unused prealloc will get reused the next time around.
1239 */
1240 if (!data) {
1241 if (ins->objectid + ins->offset >= search_end)
1242 search_end = ins->objectid - 1;
1243 else
1244 search_start = ins->objectid + ins->offset;
Chris Mason3e1ad542007-05-07 20:03:49 -04001245
Chris Masonfbdc7622007-05-30 10:22:12 -04001246 if (hint_block < search_start)
1247 hint_block = search_start;
1248
Chris Masone37c9e62007-05-09 20:13:14 -04001249 ret = find_free_extent(trans, root, 0, search_start,
Chris Masonfbdc7622007-05-30 10:22:12 -04001250 search_end, hint_block,
1251 &prealloc_key, 0);
Chris Masone37c9e62007-05-09 20:13:14 -04001252 if (ret) {
1253 return ret;
1254 }
1255 }
Chris Masonf2458e12007-04-25 15:52:25 -04001256
Chris Mason1261ec42007-03-20 20:35:03 -04001257 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1258 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1259 num_blocks);
Chris Masone089f052007-03-16 16:20:31 -04001260 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1261 sizeof(extent_item));
Chris Mason037e6392007-03-07 11:50:24 -05001262
Chris Masone089f052007-03-16 16:20:31 -04001263 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001264 pending_ret = del_pending_extents(trans, extent_root);
Chris Masone37c9e62007-05-09 20:13:14 -04001265 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001266 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001267 }
1268 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001269 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001270 }
Chris Mason1e2677e2007-05-29 16:52:18 -04001271 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1272 data);
Chris Masonfabb5682007-06-07 22:13:21 -04001273 BUG_ON(ret);
Chris Mason037e6392007-03-07 11:50:24 -05001274 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001275}
1276
1277/*
1278 * helper function to allocate a block for a given tree
1279 * returns the tree buffer or NULL.
1280 */
Chris Masone20d96d2007-03-22 12:13:20 -04001281struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Mason31f3c992007-04-30 15:25:45 -04001282 struct btrfs_root *root, u64 hint)
Chris Masonfec577f2007-02-26 10:40:21 -05001283{
Chris Masone2fa7222007-03-12 16:22:34 -04001284 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001285 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001286 struct buffer_head *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001287
Chris Mason4d775672007-04-20 20:23:12 -04001288 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
Chris Masonde428b62007-05-18 13:28:27 -04001289 1, hint, (unsigned long)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001290 if (ret) {
1291 BUG();
1292 return NULL;
1293 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001294 BUG_ON(ret);
Chris Masond98237b2007-03-28 13:57:48 -04001295 buf = btrfs_find_create_tree_block(root, ins.objectid);
Chris Masondf2ce342007-03-23 11:00:45 -04001296 set_buffer_uptodate(buf);
Chris Mason090d1872007-05-01 08:53:32 -04001297 set_buffer_checked(buf);
Chris Mason7c4452b2007-04-28 09:29:35 -04001298 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
Chris Masonfec577f2007-02-26 10:40:21 -05001299 return buf;
1300}
Chris Masona28ec192007-03-06 20:08:01 -05001301
Chris Mason6407bf62007-03-27 06:33:00 -04001302static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1303 struct btrfs_root *root, struct buffer_head *cur)
1304{
1305 struct btrfs_disk_key *key;
1306 struct btrfs_leaf *leaf;
1307 struct btrfs_file_extent_item *fi;
1308 int i;
1309 int nritems;
1310 int ret;
1311
1312 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1313 leaf = btrfs_buffer_leaf(cur);
1314 nritems = btrfs_header_nritems(&leaf->header);
1315 for (i = 0; i < nritems; i++) {
Chris Mason3a686372007-05-24 13:35:57 -04001316 u64 disk_blocknr;
Chris Mason6407bf62007-03-27 06:33:00 -04001317 key = &leaf->items[i].key;
1318 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1319 continue;
1320 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason236454d2007-04-19 13:37:44 -04001321 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1322 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001323 /*
1324 * FIXME make sure to insert a trans record that
1325 * repeats the snapshot del on crash
1326 */
Chris Mason3a686372007-05-24 13:35:57 -04001327 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1328 if (disk_blocknr == 0)
1329 continue;
1330 ret = btrfs_free_extent(trans, root, disk_blocknr,
Chris Mason6407bf62007-03-27 06:33:00 -04001331 btrfs_file_extent_disk_num_blocks(fi),
1332 0);
1333 BUG_ON(ret);
1334 }
1335 return 0;
1336}
1337
Chris Mason9aca1d52007-03-13 11:09:37 -04001338/*
1339 * helper function for drop_snapshot, this walks down the tree dropping ref
1340 * counts as it goes.
1341 */
Chris Masone089f052007-03-16 16:20:31 -04001342static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1343 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001344{
Chris Masone20d96d2007-03-22 12:13:20 -04001345 struct buffer_head *next;
1346 struct buffer_head *cur;
Chris Mason20524f02007-03-10 06:35:47 -05001347 u64 blocknr;
1348 int ret;
1349 u32 refs;
1350
Chris Mason5caf2a02007-04-02 11:20:42 -04001351 WARN_ON(*level < 0);
1352 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Masonb18c6682007-04-17 13:26:50 -04001353 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
Chris Mason6407bf62007-03-27 06:33:00 -04001354 1, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05001355 BUG_ON(ret);
1356 if (refs > 1)
1357 goto out;
Chris Mason9aca1d52007-03-13 11:09:37 -04001358 /*
1359 * walk down to the last node level and free all the leaves
1360 */
Chris Mason6407bf62007-03-27 06:33:00 -04001361 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001362 WARN_ON(*level < 0);
1363 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05001364 cur = path->nodes[*level];
Chris Mason2c90e5d2007-04-02 10:50:19 -04001365 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1366 WARN_ON(1);
Chris Mason7518a232007-03-12 12:01:18 -04001367 if (path->slots[*level] >=
Chris Masone20d96d2007-03-22 12:13:20 -04001368 btrfs_header_nritems(btrfs_buffer_header(cur)))
Chris Mason20524f02007-03-10 06:35:47 -05001369 break;
Chris Mason6407bf62007-03-27 06:33:00 -04001370 if (*level == 0) {
1371 ret = drop_leaf_ref(trans, root, cur);
1372 BUG_ON(ret);
1373 break;
1374 }
Chris Masone20d96d2007-03-22 12:13:20 -04001375 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1376 path->slots[*level]);
Chris Masonb18c6682007-04-17 13:26:50 -04001377 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04001378 BUG_ON(ret);
1379 if (refs != 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001380 path->slots[*level]++;
Chris Masone089f052007-03-16 16:20:31 -04001381 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05001382 BUG_ON(ret);
1383 continue;
1384 }
Chris Mason20524f02007-03-10 06:35:47 -05001385 next = read_tree_block(root, blocknr);
Chris Mason5caf2a02007-04-02 11:20:42 -04001386 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04001387 if (path->nodes[*level-1])
Chris Mason234b63a2007-03-13 10:46:10 -04001388 btrfs_block_release(root, path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05001389 path->nodes[*level-1] = next;
Chris Masone20d96d2007-03-22 12:13:20 -04001390 *level = btrfs_header_level(btrfs_buffer_header(next));
Chris Mason20524f02007-03-10 06:35:47 -05001391 path->slots[*level] = 0;
1392 }
1393out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001394 WARN_ON(*level < 0);
1395 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason6407bf62007-03-27 06:33:00 -04001396 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001397 bh_blocknr(path->nodes[*level]), 1, 1);
Chris Mason234b63a2007-03-13 10:46:10 -04001398 btrfs_block_release(root, path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05001399 path->nodes[*level] = NULL;
1400 *level += 1;
1401 BUG_ON(ret);
1402 return 0;
1403}
1404
Chris Mason9aca1d52007-03-13 11:09:37 -04001405/*
1406 * helper for dropping snapshots. This walks back up the tree in the path
1407 * to find the first node higher up where we haven't yet gone through
1408 * all the slots
1409 */
Chris Masone089f052007-03-16 16:20:31 -04001410static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1411 *root, struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05001412{
1413 int i;
1414 int slot;
1415 int ret;
Chris Mason234b63a2007-03-13 10:46:10 -04001416 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05001417 slot = path->slots[i];
Chris Masone20d96d2007-03-22 12:13:20 -04001418 if (slot < btrfs_header_nritems(
1419 btrfs_buffer_header(path->nodes[i])) - 1) {
Chris Mason20524f02007-03-10 06:35:47 -05001420 path->slots[i]++;
1421 *level = i;
1422 return 0;
1423 } else {
Chris Masone089f052007-03-16 16:20:31 -04001424 ret = btrfs_free_extent(trans, root,
Chris Mason7eccb902007-04-11 15:53:25 -04001425 bh_blocknr(path->nodes[*level]),
Chris Masone089f052007-03-16 16:20:31 -04001426 1, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04001427 BUG_ON(ret);
Chris Mason234b63a2007-03-13 10:46:10 -04001428 btrfs_block_release(root, path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04001429 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05001430 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05001431 }
1432 }
1433 return 1;
1434}
1435
Chris Mason9aca1d52007-03-13 11:09:37 -04001436/*
1437 * drop the reference count on the tree rooted at 'snap'. This traverses
1438 * the tree freeing any blocks that have a ref count of zero after being
1439 * decremented.
1440 */
Chris Masone089f052007-03-16 16:20:31 -04001441int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -04001442 *root, struct buffer_head *snap)
Chris Mason20524f02007-03-10 06:35:47 -05001443{
Chris Mason3768f362007-03-13 16:47:54 -04001444 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04001445 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05001446 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001447 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05001448 int i;
1449 int orig_level;
1450
Chris Mason5caf2a02007-04-02 11:20:42 -04001451 path = btrfs_alloc_path();
1452 BUG_ON(!path);
1453 btrfs_init_path(path);
Chris Mason20524f02007-03-10 06:35:47 -05001454
Chris Masone20d96d2007-03-22 12:13:20 -04001455 level = btrfs_header_level(btrfs_buffer_header(snap));
Chris Mason20524f02007-03-10 06:35:47 -05001456 orig_level = level;
Chris Mason5caf2a02007-04-02 11:20:42 -04001457 path->nodes[level] = snap;
1458 path->slots[level] = 0;
Chris Mason20524f02007-03-10 06:35:47 -05001459 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001460 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001461 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001462 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001463 if (wret < 0)
1464 ret = wret;
1465
Chris Mason5caf2a02007-04-02 11:20:42 -04001466 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04001467 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05001468 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04001469 if (wret < 0)
1470 ret = wret;
Chris Mason35b7e472007-05-02 15:53:43 -04001471 btrfs_btree_balance_dirty(root);
Chris Mason20524f02007-03-10 06:35:47 -05001472 }
Chris Mason83e15a22007-03-12 09:03:27 -04001473 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001474 if (path->nodes[i]) {
1475 btrfs_block_release(root, path->nodes[i]);
Chris Mason83e15a22007-03-12 09:03:27 -04001476 }
Chris Mason20524f02007-03-10 06:35:47 -05001477 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001478 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04001479 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05001480}
Chris Mason9078a3e2007-04-26 16:46:15 -04001481
Chris Masonbe744172007-05-06 10:15:01 -04001482static int free_block_group_radix(struct radix_tree_root *radix)
Chris Mason9078a3e2007-04-26 16:46:15 -04001483{
1484 int ret;
1485 struct btrfs_block_group_cache *cache[8];
1486 int i;
1487
1488 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001489 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
Chris Mason9078a3e2007-04-26 16:46:15 -04001490 ARRAY_SIZE(cache));
1491 if (!ret)
1492 break;
1493 for (i = 0; i < ret; i++) {
Chris Masonbe744172007-05-06 10:15:01 -04001494 radix_tree_delete(radix, cache[i]->key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001495 cache[i]->key.offset - 1);
1496 kfree(cache[i]);
1497 }
1498 }
1499 return 0;
1500}
1501
Chris Masonbe744172007-05-06 10:15:01 -04001502int btrfs_free_block_groups(struct btrfs_fs_info *info)
1503{
1504 int ret;
1505 int ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001506 unsigned long gang[16];
1507 int i;
Chris Masonbe744172007-05-06 10:15:01 -04001508
1509 ret = free_block_group_radix(&info->block_group_radix);
1510 ret2 = free_block_group_radix(&info->block_group_data_radix);
1511 if (ret)
1512 return ret;
1513 if (ret2)
1514 return ret2;
Chris Masone37c9e62007-05-09 20:13:14 -04001515
1516 while(1) {
1517 ret = find_first_radix_bit(&info->extent_map_radix,
1518 gang, 0, ARRAY_SIZE(gang));
1519 if (!ret)
1520 break;
1521 for (i = 0; i < ret; i++) {
1522 clear_radix_bit(&info->extent_map_radix, gang[i]);
1523 }
1524 }
Chris Masonbe744172007-05-06 10:15:01 -04001525 return 0;
1526}
1527
Chris Mason9078a3e2007-04-26 16:46:15 -04001528int btrfs_read_block_groups(struct btrfs_root *root)
1529{
1530 struct btrfs_path *path;
1531 int ret;
1532 int err = 0;
1533 struct btrfs_block_group_item *bi;
1534 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04001535 struct btrfs_fs_info *info = root->fs_info;
1536 struct radix_tree_root *radix;
Chris Mason9078a3e2007-04-26 16:46:15 -04001537 struct btrfs_key key;
1538 struct btrfs_key found_key;
1539 struct btrfs_leaf *leaf;
Chris Mason84f54cf2007-06-12 07:43:08 -04001540 u64 group_size_blocks;
Chris Mason31f3c992007-04-30 15:25:45 -04001541 u64 used;
Chris Mason9078a3e2007-04-26 16:46:15 -04001542
Chris Mason84f54cf2007-06-12 07:43:08 -04001543 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1544 root->fs_info->sb->s_blocksize_bits;
Chris Masonbe744172007-05-06 10:15:01 -04001545 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04001546 key.objectid = 0;
1547 key.offset = group_size_blocks;
1548 key.flags = 0;
1549 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1550
1551 path = btrfs_alloc_path();
1552 if (!path)
1553 return -ENOMEM;
1554
1555 while(1) {
Chris Masonbe744172007-05-06 10:15:01 -04001556 ret = btrfs_search_slot(NULL, info->extent_root,
Chris Mason9078a3e2007-04-26 16:46:15 -04001557 &key, path, 0, 0);
1558 if (ret != 0) {
1559 err = ret;
1560 break;
1561 }
1562 leaf = btrfs_buffer_leaf(path->nodes[0]);
1563 btrfs_disk_key_to_cpu(&found_key,
1564 &leaf->items[path->slots[0]].key);
1565 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1566 if (!cache) {
1567 err = -1;
1568 break;
1569 }
Chris Mason3e1ad542007-05-07 20:03:49 -04001570
Chris Mason9078a3e2007-04-26 16:46:15 -04001571 bi = btrfs_item_ptr(leaf, path->slots[0],
1572 struct btrfs_block_group_item);
Chris Mason1e2677e2007-05-29 16:52:18 -04001573 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1574 radix = &info->block_group_data_radix;
1575 cache->data = 1;
1576 } else {
1577 radix = &info->block_group_radix;
1578 cache->data = 0;
1579 }
1580
Chris Mason9078a3e2007-04-26 16:46:15 -04001581 memcpy(&cache->item, bi, sizeof(*bi));
1582 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason31f3c992007-04-30 15:25:45 -04001583 cache->last_alloc = cache->key.objectid;
1584 cache->first_free = cache->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -04001585 cache->last_prealloc = cache->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001586 cache->pinned = 0;
Chris Masone37c9e62007-05-09 20:13:14 -04001587 cache->cached = 0;
1588
Chris Mason3e1ad542007-05-07 20:03:49 -04001589 cache->radix = radix;
1590
Chris Mason9078a3e2007-04-26 16:46:15 -04001591 key.objectid = found_key.objectid + found_key.offset;
1592 btrfs_release_path(root, path);
Chris Masonbe744172007-05-06 10:15:01 -04001593 ret = radix_tree_insert(radix, found_key.objectid +
Chris Mason9078a3e2007-04-26 16:46:15 -04001594 found_key.offset - 1,
1595 (void *)cache);
1596 BUG_ON(ret);
Chris Mason31f3c992007-04-30 15:25:45 -04001597 used = btrfs_block_group_used(bi);
Chris Mason84f54cf2007-06-12 07:43:08 -04001598 if (used < div_factor(key.offset, 8)) {
Chris Masonbe744172007-05-06 10:15:01 -04001599 radix_tree_tag_set(radix, found_key.objectid +
Chris Mason31f3c992007-04-30 15:25:45 -04001600 found_key.offset - 1,
1601 BTRFS_BLOCK_GROUP_AVAIL);
1602 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001603 if (key.objectid >=
Chris Masonbe744172007-05-06 10:15:01 -04001604 btrfs_super_total_blocks(info->disk_super))
Chris Mason9078a3e2007-04-26 16:46:15 -04001605 break;
1606 }
1607
1608 btrfs_free_path(path);
1609 return 0;
1610}