Btrfs: Add back pointers from extents to the btree or file referencing them
[linux-2.6-block.git] / fs / btrfs / extent-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
ec6b910f 19#include <linux/sched.h>
7bb86316 20#include <linux/crc32c.h>
74493f7a 21#include "hash.h"
fec577fb
CM
22#include "ctree.h"
23#include "disk-io.h"
24#include "print-tree.h"
e089f05c 25#include "transaction.h"
fec577fb 26
96b5179d
CM
27#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
28#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
29#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
30
e089f05c
CM
31static int finish_current_insert(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
e20d96d6
CM
33static int del_pending_extents(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
fec577fb 35
e37c9e69
CM
36static int cache_block_group(struct btrfs_root *root,
37 struct btrfs_block_group_cache *block_group)
38{
39 struct btrfs_path *path;
40 int ret;
41 struct btrfs_key key;
5f39d397 42 struct extent_buffer *leaf;
f510cfec 43 struct extent_map_tree *free_space_cache;
e37c9e69 44 int slot;
e37c9e69
CM
45 u64 last = 0;
46 u64 hole_size;
7d7d6068 47 u64 first_free;
e37c9e69
CM
48 int found = 0;
49
00f5c795
CM
50 if (!block_group)
51 return 0;
52
e37c9e69 53 root = root->fs_info->extent_root;
f510cfec 54 free_space_cache = &root->fs_info->free_space_cache;
e37c9e69
CM
55
56 if (block_group->cached)
57 return 0;
f510cfec 58
e37c9e69
CM
59 path = btrfs_alloc_path();
60 if (!path)
61 return -ENOMEM;
7d7d6068 62
2cc58cf2 63 path->reada = 2;
7d7d6068 64 first_free = block_group->key.objectid;
e37c9e69 65 key.objectid = block_group->key.objectid;
e37c9e69 66 key.offset = 0;
7d7d6068 67
e37c9e69
CM
68 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7d7d6068 70
e37c9e69
CM
71 if (ret < 0)
72 return ret;
7d7d6068 73
e37c9e69
CM
74 if (ret && path->slots[0] > 0)
75 path->slots[0]--;
7d7d6068 76
e37c9e69 77 while(1) {
5f39d397 78 leaf = path->nodes[0];
e37c9e69 79 slot = path->slots[0];
5f39d397 80 if (slot >= btrfs_header_nritems(leaf)) {
e37c9e69 81 ret = btrfs_next_leaf(root, path);
54aa1f4d
CM
82 if (ret < 0)
83 goto err;
de428b63 84 if (ret == 0) {
e37c9e69 85 continue;
de428b63 86 } else {
e37c9e69
CM
87 break;
88 }
89 }
7d7d6068 90
5f39d397 91 btrfs_item_key_to_cpu(leaf, &key, slot);
7d7d6068 92 if (key.objectid < block_group->key.objectid) {
7bb86316
CM
93 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
94 key.objectid + key.offset > first_free)
7d7d6068
Y
95 first_free = key.objectid + key.offset;
96 goto next;
97 }
98
e37c9e69
CM
99 if (key.objectid >= block_group->key.objectid +
100 block_group->key.offset) {
e37c9e69
CM
101 break;
102 }
7d7d6068 103
e37c9e69
CM
104 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
105 if (!found) {
7d7d6068 106 last = first_free;
e37c9e69 107 found = 1;
e37c9e69 108 }
f510cfec
CM
109 if (key.objectid > last) {
110 hole_size = key.objectid - last;
111 set_extent_dirty(free_space_cache, last,
112 last + hole_size - 1,
113 GFP_NOFS);
7d7d6068
Y
114 }
115 last = key.objectid + key.offset;
e37c9e69 116 }
7d7d6068 117next:
e37c9e69
CM
118 path->slots[0]++;
119 }
120
7d7d6068
Y
121 if (!found)
122 last = first_free;
123 if (block_group->key.objectid +
124 block_group->key.offset > last) {
125 hole_size = block_group->key.objectid +
126 block_group->key.offset - last;
f510cfec
CM
127 set_extent_dirty(free_space_cache, last,
128 last + hole_size - 1, GFP_NOFS);
7d7d6068 129 }
e37c9e69 130 block_group->cached = 1;
54aa1f4d 131err:
e37c9e69
CM
132 btrfs_free_path(path);
133 return 0;
134}
135
5276aeda
CM
136struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
137 btrfs_fs_info *info,
db94535d 138 u64 bytenr)
be744175 139{
96b5179d
CM
140 struct extent_map_tree *block_group_cache;
141 struct btrfs_block_group_cache *block_group = NULL;
142 u64 ptr;
143 u64 start;
144 u64 end;
be744175
CM
145 int ret;
146
96b5179d
CM
147 block_group_cache = &info->block_group_cache;
148 ret = find_first_extent_bit(block_group_cache,
db94535d 149 bytenr, &start, &end,
96b5179d 150 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
be744175 151 if (ret) {
96b5179d 152 return NULL;
be744175 153 }
96b5179d
CM
154 ret = get_state_private(block_group_cache, start, &ptr);
155 if (ret)
156 return NULL;
157
ae2f5411 158 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
5cf66426 159 if (block_group->key.objectid <= bytenr && bytenr <
96b5179d
CM
160 block_group->key.objectid + block_group->key.offset)
161 return block_group;
be744175
CM
162 return NULL;
163}
e37c9e69
CM
164static u64 find_search_start(struct btrfs_root *root,
165 struct btrfs_block_group_cache **cache_ret,
5e5745dc
Y
166 u64 search_start, int num,
167 int data, int full_scan)
e37c9e69 168{
e37c9e69
CM
169 int ret;
170 struct btrfs_block_group_cache *cache = *cache_ret;
e19caa5f 171 u64 last;
f510cfec
CM
172 u64 start = 0;
173 u64 end = 0;
257d0ce3 174 u64 cache_miss = 0;
f84a8b36 175 int wrapped = 0;
e37c9e69 176
00f5c795 177 if (!cache) {
1a2b2ac7 178 goto out;
00f5c795 179 }
e37c9e69 180again:
54aa1f4d
CM
181 ret = cache_block_group(root, cache);
182 if (ret)
183 goto out;
f84a8b36 184
e19caa5f
CM
185 last = max(search_start, cache->key.objectid);
186
e37c9e69 187 while(1) {
f510cfec
CM
188 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
189 last, &start, &end, EXTENT_DIRTY);
e19caa5f 190 if (ret) {
257d0ce3
CM
191 if (!cache_miss)
192 cache_miss = last;
e19caa5f
CM
193 goto new_group;
194 }
f510cfec
CM
195
196 start = max(last, start);
197 last = end + 1;
257d0ce3
CM
198 if (last - start < num) {
199 if (last == cache->key.objectid + cache->key.offset)
200 cache_miss = start;
f510cfec 201 continue;
257d0ce3
CM
202 }
203 if (data != BTRFS_BLOCK_GROUP_MIXED &&
5cf66426 204 start + num > cache->key.objectid + cache->key.offset)
e37c9e69 205 goto new_group;
f510cfec 206 return start;
e37c9e69
CM
207 }
208out:
1a2b2ac7
CM
209 cache = btrfs_lookup_block_group(root->fs_info, search_start);
210 if (!cache) {
211 printk("Unable to find block group for %Lu\n",
212 search_start);
213 WARN_ON(1);
214 return search_start;
215 }
1a5bc167 216 return search_start;
e37c9e69
CM
217
218new_group:
e19caa5f 219 last = cache->key.objectid + cache->key.offset;
f84a8b36 220wrapped:
e19caa5f 221 cache = btrfs_lookup_block_group(root->fs_info, last);
e37c9e69 222 if (!cache) {
0e4de584 223no_cache:
f84a8b36
CM
224 if (!wrapped) {
225 wrapped = 1;
226 last = search_start;
227 data = BTRFS_BLOCK_GROUP_MIXED;
228 goto wrapped;
229 }
1a2b2ac7 230 goto out;
e37c9e69 231 }
257d0ce3
CM
232 if (cache_miss && !cache->cached) {
233 cache_block_group(root, cache);
234 last = cache_miss;
257d0ce3
CM
235 cache = btrfs_lookup_block_group(root->fs_info, last);
236 }
1a2b2ac7 237 cache = btrfs_find_block_group(root, cache, last, data, 0);
0e4de584
CM
238 if (!cache)
239 goto no_cache;
e37c9e69 240 *cache_ret = cache;
257d0ce3 241 cache_miss = 0;
e37c9e69
CM
242 goto again;
243}
244
84f54cfa
CM
245static u64 div_factor(u64 num, int factor)
246{
257d0ce3
CM
247 if (factor == 10)
248 return num;
84f54cfa
CM
249 num *= factor;
250 do_div(num, 10);
251 return num;
252}
253
31f3c99b
CM
254struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
255 struct btrfs_block_group_cache
be744175 256 *hint, u64 search_start,
de428b63 257 int data, int owner)
cd1bc465 258{
96b5179d
CM
259 struct btrfs_block_group_cache *cache;
260 struct extent_map_tree *block_group_cache;
31f3c99b 261 struct btrfs_block_group_cache *found_group = NULL;
cd1bc465
CM
262 struct btrfs_fs_info *info = root->fs_info;
263 u64 used;
31f3c99b
CM
264 u64 last = 0;
265 u64 hint_last;
96b5179d
CM
266 u64 start;
267 u64 end;
268 u64 free_check;
269 u64 ptr;
270 int bit;
cd1bc465 271 int ret;
31f3c99b 272 int full_search = 0;
de428b63 273 int factor = 8;
1e2677e0 274 int data_swap = 0;
de428b63 275
96b5179d
CM
276 block_group_cache = &info->block_group_cache;
277
de428b63 278 if (!owner)
f84a8b36 279 factor = 8;
be744175 280
257d0ce3 281 if (data == BTRFS_BLOCK_GROUP_MIXED) {
f84a8b36 282 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
257d0ce3
CM
283 factor = 10;
284 } else if (data)
96b5179d
CM
285 bit = BLOCK_GROUP_DATA;
286 else
287 bit = BLOCK_GROUP_METADATA;
be744175
CM
288
289 if (search_start) {
290 struct btrfs_block_group_cache *shint;
5276aeda 291 shint = btrfs_lookup_block_group(info, search_start);
f84a8b36
CM
292 if (shint && (shint->data == data ||
293 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
be744175 294 used = btrfs_block_group_used(&shint->item);
324ae4df
Y
295 if (used + shint->pinned <
296 div_factor(shint->key.offset, factor)) {
be744175
CM
297 return shint;
298 }
299 }
300 }
f84a8b36
CM
301 if (hint && (hint->data == data ||
302 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
31f3c99b 303 used = btrfs_block_group_used(&hint->item);
324ae4df
Y
304 if (used + hint->pinned <
305 div_factor(hint->key.offset, factor)) {
31f3c99b
CM
306 return hint;
307 }
e19caa5f 308 last = hint->key.objectid + hint->key.offset;
31f3c99b
CM
309 hint_last = last;
310 } else {
e37c9e69
CM
311 if (hint)
312 hint_last = max(hint->key.objectid, search_start);
313 else
314 hint_last = search_start;
315
316 last = hint_last;
31f3c99b 317 }
31f3c99b 318again:
cd1bc465 319 while(1) {
96b5179d
CM
320 ret = find_first_extent_bit(block_group_cache, last,
321 &start, &end, bit);
322 if (ret)
cd1bc465 323 break;
96b5179d
CM
324
325 ret = get_state_private(block_group_cache, start, &ptr);
326 if (ret)
327 break;
328
ae2f5411 329 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
330 last = cache->key.objectid + cache->key.offset;
331 used = btrfs_block_group_used(&cache->item);
332
333 if (full_search)
334 free_check = cache->key.offset;
335 else
336 free_check = div_factor(cache->key.offset, factor);
324ae4df 337 if (used + cache->pinned < free_check) {
96b5179d
CM
338 found_group = cache;
339 goto found;
cd1bc465 340 }
de428b63 341 cond_resched();
cd1bc465 342 }
31f3c99b 343 if (!full_search) {
be744175 344 last = search_start;
31f3c99b
CM
345 full_search = 1;
346 goto again;
347 }
1e2677e0 348 if (!data_swap) {
1e2677e0 349 data_swap = 1;
96b5179d 350 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1e2677e0
CM
351 last = search_start;
352 goto again;
353 }
be744175 354found:
31f3c99b 355 return found_group;
cd1bc465
CM
356}
357
7bb86316 358static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
74493f7a
CM
359 u64 owner, u64 owner_offset)
360{
361 u32 high_crc = ~(u32)0;
362 u32 low_crc = ~(u32)0;
363 __le64 lenum;
364
365 lenum = cpu_to_le64(root_objectid);
366 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
7bb86316
CM
367 lenum = cpu_to_le64(ref_generation);
368 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
74493f7a 369
7bb86316 370#if 0
74493f7a
CM
371 lenum = cpu_to_le64(owner);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
74493f7a
CM
373 lenum = cpu_to_le64(owner_offset);
374 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
7bb86316 375#endif
74493f7a
CM
376 return ((u64)high_crc << 32) | (u64)low_crc;
377}
378
7bb86316
CM
379static int match_extent_ref(struct extent_buffer *leaf,
380 struct btrfs_extent_ref *disk_ref,
381 struct btrfs_extent_ref *cpu_ref)
382{
383 int ret;
384 int len;
385
386 if (cpu_ref->objectid)
387 len = sizeof(*cpu_ref);
388 else
389 len = 2 * sizeof(u64);
390 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
391 len);
392 return ret == 0;
393}
394
395static int lookup_extent_backref(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root,
397 struct btrfs_path *path, u64 bytenr,
398 u64 root_objectid, u64 ref_generation,
399 u64 owner, u64 owner_offset, int del)
74493f7a
CM
400{
401 u64 hash;
402 struct btrfs_key key;
7bb86316 403 struct btrfs_key found_key;
74493f7a 404 struct btrfs_extent_ref ref;
7bb86316
CM
405 struct extent_buffer *leaf;
406 struct btrfs_extent_ref *disk_ref;
407 int ret;
408 int ret2;
409
410 btrfs_set_stack_ref_root(&ref, root_objectid);
411 btrfs_set_stack_ref_generation(&ref, ref_generation);
412 btrfs_set_stack_ref_objectid(&ref, owner);
413 btrfs_set_stack_ref_offset(&ref, owner_offset);
414
415 hash = hash_extent_ref(root_objectid, ref_generation, owner,
416 owner_offset);
417 key.offset = hash;
418 key.objectid = bytenr;
419 key.type = BTRFS_EXTENT_REF_KEY;
420
421 while (1) {
422 ret = btrfs_search_slot(trans, root, &key, path,
423 del ? -1 : 0, del);
424 if (ret < 0)
425 goto out;
426 leaf = path->nodes[0];
427 if (ret != 0) {
428 u32 nritems = btrfs_header_nritems(leaf);
429 if (path->slots[0] >= nritems) {
430 ret2 = btrfs_next_leaf(root, path);
431 if (ret2)
432 goto out;
433 leaf = path->nodes[0];
434 }
435 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
436 if (found_key.objectid != bytenr ||
437 found_key.type != BTRFS_EXTENT_REF_KEY)
438 goto out;
439 key.offset = found_key.offset;
440 if (del) {
441 btrfs_release_path(root, path);
442 continue;
443 }
444 }
445 disk_ref = btrfs_item_ptr(path->nodes[0],
446 path->slots[0],
447 struct btrfs_extent_ref);
448 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
449 ret = 0;
450 goto out;
451 }
452 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
453 key.offset = found_key.offset + 1;
454 btrfs_release_path(root, path);
455 }
456out:
457 return ret;
458}
459
460int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root,
462 struct btrfs_path *path, u64 bytenr,
463 u64 root_objectid, u64 ref_generation,
464 u64 owner, u64 owner_offset)
465{
466 u64 hash;
467 struct btrfs_key key;
468 struct btrfs_extent_ref ref;
469 struct btrfs_extent_ref *disk_ref;
74493f7a
CM
470 int ret;
471
472 btrfs_set_stack_ref_root(&ref, root_objectid);
7bb86316 473 btrfs_set_stack_ref_generation(&ref, ref_generation);
74493f7a
CM
474 btrfs_set_stack_ref_objectid(&ref, owner);
475 btrfs_set_stack_ref_offset(&ref, owner_offset);
476
7bb86316
CM
477 hash = hash_extent_ref(root_objectid, ref_generation, owner,
478 owner_offset);
74493f7a
CM
479 key.offset = hash;
480 key.objectid = bytenr;
481 key.type = BTRFS_EXTENT_REF_KEY;
482
483 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
484 while (ret == -EEXIST) {
7bb86316
CM
485 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
486 struct btrfs_extent_ref);
487 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
488 goto out;
489 key.offset++;
490 btrfs_release_path(root, path);
491 ret = btrfs_insert_empty_item(trans, root, path, &key,
492 sizeof(ref));
74493f7a 493 }
7bb86316
CM
494 if (ret)
495 goto out;
496 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
497 struct btrfs_extent_ref);
498 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
499 sizeof(ref));
500 btrfs_mark_buffer_dirty(path->nodes[0]);
501out:
502 btrfs_release_path(root, path);
503 return ret;
74493f7a
CM
504}
505
b18c6685
CM
506int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
74493f7a 508 u64 bytenr, u64 num_bytes,
7bb86316 509 u64 root_objectid, u64 ref_generation,
74493f7a 510 u64 owner, u64 owner_offset)
02217ed2 511{
5caf2a00 512 struct btrfs_path *path;
02217ed2 513 int ret;
e2fa7227 514 struct btrfs_key key;
5f39d397 515 struct extent_buffer *l;
234b63a0 516 struct btrfs_extent_item *item;
cf27e1ee 517 u32 refs;
037e6390 518
db94535d 519 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 520 path = btrfs_alloc_path();
54aa1f4d
CM
521 if (!path)
522 return -ENOMEM;
26b8003f 523
db94535d 524 key.objectid = bytenr;
62e2749e 525 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 526 key.offset = num_bytes;
5caf2a00 527 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 528 0, 1);
54aa1f4d
CM
529 if (ret < 0)
530 return ret;
a429e513 531 if (ret != 0) {
a28ec197 532 BUG();
a429e513 533 }
02217ed2 534 BUG_ON(ret != 0);
5f39d397 535 l = path->nodes[0];
5caf2a00 536 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397
CM
537 refs = btrfs_extent_refs(l, item);
538 btrfs_set_extent_refs(l, item, refs + 1);
5caf2a00 539 btrfs_mark_buffer_dirty(path->nodes[0]);
a28ec197 540
5caf2a00 541 btrfs_release_path(root->fs_info->extent_root, path);
7bb86316
CM
542
543 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
544 path, bytenr, root_objectid,
545 ref_generation, owner, owner_offset);
546 BUG_ON(ret);
9f5fae2f 547 finish_current_insert(trans, root->fs_info->extent_root);
e20d96d6 548 del_pending_extents(trans, root->fs_info->extent_root);
74493f7a
CM
549
550 btrfs_free_path(path);
02217ed2
CM
551 return 0;
552}
553
e9d0b13b
CM
554int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root)
556{
557 finish_current_insert(trans, root->fs_info->extent_root);
558 del_pending_extents(trans, root->fs_info->extent_root);
559 return 0;
560}
561
b18c6685 562static int lookup_extent_ref(struct btrfs_trans_handle *trans,
db94535d
CM
563 struct btrfs_root *root, u64 bytenr,
564 u64 num_bytes, u32 *refs)
a28ec197 565{
5caf2a00 566 struct btrfs_path *path;
a28ec197 567 int ret;
e2fa7227 568 struct btrfs_key key;
5f39d397 569 struct extent_buffer *l;
234b63a0 570 struct btrfs_extent_item *item;
5caf2a00 571
db94535d 572 WARN_ON(num_bytes < root->sectorsize);
5caf2a00 573 path = btrfs_alloc_path();
db94535d
CM
574 key.objectid = bytenr;
575 key.offset = num_bytes;
62e2749e 576 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
5caf2a00 577 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
9f5fae2f 578 0, 0);
54aa1f4d
CM
579 if (ret < 0)
580 goto out;
5f39d397
CM
581 if (ret != 0) {
582 btrfs_print_leaf(root, path->nodes[0]);
db94535d 583 printk("failed to find block number %Lu\n", bytenr);
a28ec197 584 BUG();
5f39d397
CM
585 }
586 l = path->nodes[0];
5caf2a00 587 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
5f39d397 588 *refs = btrfs_extent_refs(l, item);
54aa1f4d 589out:
5caf2a00 590 btrfs_free_path(path);
a28ec197
CM
591 return 0;
592}
593
c5739bba 594int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
7bb86316 595 struct btrfs_root *root, u64 owner_objectid)
c5739bba 596{
7bb86316
CM
597 u64 generation;
598 u64 key_objectid;
599 u64 level;
600 u32 nritems;
601 struct btrfs_disk_key disk_key;
602
603 level = btrfs_header_level(root->node);
604 generation = trans->transid;
605 nritems = btrfs_header_nritems(root->node);
606 if (nritems > 0) {
607 if (level == 0)
608 btrfs_item_key(root->node, &disk_key, 0);
609 else
610 btrfs_node_key(root->node, &disk_key, 0);
611 key_objectid = btrfs_disk_key_objectid(&disk_key);
612 } else {
613 key_objectid = 0;
614 }
db94535d 615 return btrfs_inc_extent_ref(trans, root, root->node->start,
7bb86316
CM
616 root->node->len, owner_objectid,
617 generation, 0, 0);
c5739bba
CM
618}
619
e089f05c 620int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5f39d397 621 struct extent_buffer *buf)
02217ed2 622{
db94535d 623 u64 bytenr;
5f39d397
CM
624 u32 nritems;
625 struct btrfs_key key;
6407bf6d 626 struct btrfs_file_extent_item *fi;
02217ed2 627 int i;
db94535d 628 int level;
6407bf6d 629 int ret;
54aa1f4d 630 int faili;
a28ec197 631
3768f368 632 if (!root->ref_cows)
a28ec197 633 return 0;
5f39d397 634
db94535d 635 level = btrfs_header_level(buf);
5f39d397
CM
636 nritems = btrfs_header_nritems(buf);
637 for (i = 0; i < nritems; i++) {
db94535d
CM
638 if (level == 0) {
639 u64 disk_bytenr;
5f39d397
CM
640 btrfs_item_key_to_cpu(buf, &key, i);
641 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d 642 continue;
5f39d397 643 fi = btrfs_item_ptr(buf, i,
6407bf6d 644 struct btrfs_file_extent_item);
5f39d397 645 if (btrfs_file_extent_type(buf, fi) ==
236454df
CM
646 BTRFS_FILE_EXTENT_INLINE)
647 continue;
db94535d
CM
648 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
649 if (disk_bytenr == 0)
3a686375 650 continue;
db94535d 651 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
7bb86316
CM
652 btrfs_file_extent_disk_num_bytes(buf, fi),
653 root->root_key.objectid, trans->transid,
654 key.objectid, key.offset);
54aa1f4d
CM
655 if (ret) {
656 faili = i;
657 goto fail;
658 }
6407bf6d 659 } else {
db94535d
CM
660 bytenr = btrfs_node_blockptr(buf, i);
661 ret = btrfs_inc_extent_ref(trans, root, bytenr,
7bb86316
CM
662 btrfs_level_size(root, level - 1),
663 root->root_key.objectid,
664 trans->transid, 0, 0);
54aa1f4d
CM
665 if (ret) {
666 faili = i;
667 goto fail;
668 }
6407bf6d 669 }
02217ed2
CM
670 }
671 return 0;
54aa1f4d 672fail:
ccd467d6 673 WARN_ON(1);
7bb86316 674#if 0
54aa1f4d 675 for (i =0; i < faili; i++) {
db94535d
CM
676 if (level == 0) {
677 u64 disk_bytenr;
5f39d397
CM
678 btrfs_item_key_to_cpu(buf, &key, i);
679 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
54aa1f4d 680 continue;
5f39d397 681 fi = btrfs_item_ptr(buf, i,
54aa1f4d 682 struct btrfs_file_extent_item);
5f39d397 683 if (btrfs_file_extent_type(buf, fi) ==
54aa1f4d
CM
684 BTRFS_FILE_EXTENT_INLINE)
685 continue;
db94535d
CM
686 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
687 if (disk_bytenr == 0)
54aa1f4d 688 continue;
db94535d
CM
689 err = btrfs_free_extent(trans, root, disk_bytenr,
690 btrfs_file_extent_disk_num_bytes(buf,
5f39d397 691 fi), 0);
54aa1f4d
CM
692 BUG_ON(err);
693 } else {
db94535d
CM
694 bytenr = btrfs_node_blockptr(buf, i);
695 err = btrfs_free_extent(trans, root, bytenr,
696 btrfs_level_size(root, level - 1), 0);
54aa1f4d
CM
697 BUG_ON(err);
698 }
699 }
7bb86316 700#endif
54aa1f4d 701 return ret;
02217ed2
CM
702}
703
9078a3e1
CM
704static int write_one_cache_group(struct btrfs_trans_handle *trans,
705 struct btrfs_root *root,
706 struct btrfs_path *path,
707 struct btrfs_block_group_cache *cache)
708{
709 int ret;
710 int pending_ret;
711 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
712 unsigned long bi;
713 struct extent_buffer *leaf;
9078a3e1 714
9078a3e1 715 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
716 if (ret < 0)
717 goto fail;
9078a3e1 718 BUG_ON(ret);
5f39d397
CM
719
720 leaf = path->nodes[0];
721 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
722 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
723 btrfs_mark_buffer_dirty(leaf);
9078a3e1 724 btrfs_release_path(extent_root, path);
54aa1f4d 725fail:
9078a3e1
CM
726 finish_current_insert(trans, extent_root);
727 pending_ret = del_pending_extents(trans, extent_root);
728 if (ret)
729 return ret;
730 if (pending_ret)
731 return pending_ret;
732 return 0;
733
734}
735
96b5179d
CM
736int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root)
9078a3e1 738{
96b5179d
CM
739 struct extent_map_tree *block_group_cache;
740 struct btrfs_block_group_cache *cache;
9078a3e1
CM
741 int ret;
742 int err = 0;
743 int werr = 0;
9078a3e1 744 struct btrfs_path *path;
96b5179d
CM
745 u64 last = 0;
746 u64 start;
747 u64 end;
748 u64 ptr;
9078a3e1 749
96b5179d 750 block_group_cache = &root->fs_info->block_group_cache;
9078a3e1
CM
751 path = btrfs_alloc_path();
752 if (!path)
753 return -ENOMEM;
754
755 while(1) {
96b5179d
CM
756 ret = find_first_extent_bit(block_group_cache, last,
757 &start, &end, BLOCK_GROUP_DIRTY);
758 if (ret)
9078a3e1 759 break;
54aa1f4d 760
96b5179d
CM
761 last = end + 1;
762 ret = get_state_private(block_group_cache, start, &ptr);
763 if (ret)
764 break;
765
ae2f5411 766 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
96b5179d
CM
767 err = write_one_cache_group(trans, root,
768 path, cache);
769 /*
770 * if we fail to write the cache group, we want
771 * to keep it marked dirty in hopes that a later
772 * write will work
773 */
774 if (err) {
775 werr = err;
776 continue;
9078a3e1 777 }
96b5179d
CM
778 clear_extent_bits(block_group_cache, start, end,
779 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
780 }
781 btrfs_free_path(path);
782 return werr;
783}
784
785static int update_block_group(struct btrfs_trans_handle *trans,
786 struct btrfs_root *root,
db94535d
CM
787 u64 bytenr, u64 num_bytes, int alloc,
788 int mark_free, int data)
9078a3e1
CM
789{
790 struct btrfs_block_group_cache *cache;
791 struct btrfs_fs_info *info = root->fs_info;
db94535d 792 u64 total = num_bytes;
9078a3e1 793 u64 old_val;
db94535d 794 u64 byte_in_group;
96b5179d
CM
795 u64 start;
796 u64 end;
3e1ad54f 797
9078a3e1 798 while(total) {
db94535d 799 cache = btrfs_lookup_block_group(info, bytenr);
3e1ad54f 800 if (!cache) {
9078a3e1 801 return -1;
cd1bc465 802 }
db94535d
CM
803 byte_in_group = bytenr - cache->key.objectid;
804 WARN_ON(byte_in_group > cache->key.offset);
96b5179d
CM
805 start = cache->key.objectid;
806 end = start + cache->key.offset - 1;
807 set_extent_bits(&info->block_group_cache, start, end,
808 BLOCK_GROUP_DIRTY, GFP_NOFS);
9078a3e1
CM
809
810 old_val = btrfs_block_group_used(&cache->item);
db94535d 811 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 812 if (alloc) {
1e2677e0 813 if (cache->data != data &&
84f54cfa 814 old_val < (cache->key.offset >> 1)) {
96b5179d
CM
815 int bit_to_clear;
816 int bit_to_set;
96b5179d 817 cache->data = data;
1e2677e0 818 if (data) {
b97f9203
Y
819 bit_to_clear = BLOCK_GROUP_METADATA;
820 bit_to_set = BLOCK_GROUP_DATA;
f84a8b36
CM
821 cache->item.flags &=
822 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
823 cache->item.flags |=
824 BTRFS_BLOCK_GROUP_DATA;
825 } else {
b97f9203
Y
826 bit_to_clear = BLOCK_GROUP_DATA;
827 bit_to_set = BLOCK_GROUP_METADATA;
f84a8b36
CM
828 cache->item.flags &=
829 ~BTRFS_BLOCK_GROUP_MIXED;
1e2677e0
CM
830 cache->item.flags &=
831 ~BTRFS_BLOCK_GROUP_DATA;
832 }
96b5179d
CM
833 clear_extent_bits(&info->block_group_cache,
834 start, end, bit_to_clear,
835 GFP_NOFS);
836 set_extent_bits(&info->block_group_cache,
837 start, end, bit_to_set,
838 GFP_NOFS);
f84a8b36
CM
839 } else if (cache->data != data &&
840 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
841 cache->data = BTRFS_BLOCK_GROUP_MIXED;
842 set_extent_bits(&info->block_group_cache,
843 start, end,
844 BLOCK_GROUP_DATA |
845 BLOCK_GROUP_METADATA,
846 GFP_NOFS);
1e2677e0 847 }
db94535d 848 old_val += num_bytes;
cd1bc465 849 } else {
db94535d 850 old_val -= num_bytes;
f510cfec
CM
851 if (mark_free) {
852 set_extent_dirty(&info->free_space_cache,
db94535d 853 bytenr, bytenr + num_bytes - 1,
f510cfec 854 GFP_NOFS);
e37c9e69 855 }
cd1bc465 856 }
9078a3e1 857 btrfs_set_block_group_used(&cache->item, old_val);
db94535d
CM
858 total -= num_bytes;
859 bytenr += num_bytes;
9078a3e1
CM
860 }
861 return 0;
862}
324ae4df
Y
863static int update_pinned_extents(struct btrfs_root *root,
864 u64 bytenr, u64 num, int pin)
865{
866 u64 len;
867 struct btrfs_block_group_cache *cache;
868 struct btrfs_fs_info *fs_info = root->fs_info;
869
870 if (pin) {
871 set_extent_dirty(&fs_info->pinned_extents,
872 bytenr, bytenr + num - 1, GFP_NOFS);
873 } else {
874 clear_extent_dirty(&fs_info->pinned_extents,
875 bytenr, bytenr + num - 1, GFP_NOFS);
876 }
877 while (num > 0) {
878 cache = btrfs_lookup_block_group(fs_info, bytenr);
879 WARN_ON(!cache);
880 len = min(num, cache->key.offset -
881 (bytenr - cache->key.objectid));
882 if (pin) {
883 cache->pinned += len;
884 fs_info->total_pinned += len;
885 } else {
886 cache->pinned -= len;
887 fs_info->total_pinned -= len;
888 }
889 bytenr += len;
890 num -= len;
891 }
892 return 0;
893}
9078a3e1 894
1a5bc167 895int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
ccd467d6 896{
ccd467d6 897 u64 last = 0;
1a5bc167
CM
898 u64 start;
899 u64 end;
900 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
ccd467d6 901 int ret;
ccd467d6
CM
902
903 while(1) {
1a5bc167
CM
904 ret = find_first_extent_bit(pinned_extents, last,
905 &start, &end, EXTENT_DIRTY);
906 if (ret)
ccd467d6 907 break;
1a5bc167
CM
908 set_extent_dirty(copy, start, end, GFP_NOFS);
909 last = end + 1;
ccd467d6
CM
910 }
911 return 0;
912}
913
914int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
915 struct btrfs_root *root,
1a5bc167 916 struct extent_map_tree *unpin)
a28ec197 917{
1a5bc167
CM
918 u64 start;
919 u64 end;
a28ec197 920 int ret;
f510cfec 921 struct extent_map_tree *free_space_cache;
f510cfec 922 free_space_cache = &root->fs_info->free_space_cache;
a28ec197
CM
923
924 while(1) {
1a5bc167
CM
925 ret = find_first_extent_bit(unpin, 0, &start, &end,
926 EXTENT_DIRTY);
927 if (ret)
a28ec197 928 break;
324ae4df 929 update_pinned_extents(root, start, end + 1 - start, 0);
1a5bc167
CM
930 clear_extent_dirty(unpin, start, end, GFP_NOFS);
931 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
a28ec197
CM
932 }
933 return 0;
934}
935
e089f05c
CM
936static int finish_current_insert(struct btrfs_trans_handle *trans, struct
937 btrfs_root *extent_root)
037e6390 938{
7bb86316
CM
939 u64 start;
940 u64 end;
941 struct btrfs_fs_info *info = extent_root->fs_info;
942 struct btrfs_path *path;
e2fa7227 943 struct btrfs_key ins;
234b63a0 944 struct btrfs_extent_item extent_item;
037e6390 945 int ret;
1a5bc167 946 int err = 0;
037e6390 947
5f39d397 948 btrfs_set_stack_extent_refs(&extent_item, 1);
62e2749e 949 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
7bb86316 950 path = btrfs_alloc_path();
037e6390 951
26b8003f 952 while(1) {
1a5bc167
CM
953 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
954 &end, EXTENT_LOCKED);
955 if (ret)
26b8003f
CM
956 break;
957
1a5bc167
CM
958 ins.objectid = start;
959 ins.offset = end + 1 - start;
960 err = btrfs_insert_item(trans, extent_root, &ins,
961 &extent_item, sizeof(extent_item));
962 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
963 GFP_NOFS);
7bb86316
CM
964 err = btrfs_insert_extent_backref(trans, extent_root, path,
965 start, extent_root->root_key.objectid,
966 0, 0, 0);
967 BUG_ON(err);
037e6390 968 }
7bb86316 969 btrfs_free_path(path);
037e6390
CM
970 return 0;
971}
972
db94535d
CM
973static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
974 int pending)
e20d96d6 975{
1a5bc167 976 int err = 0;
5f39d397 977 struct extent_buffer *buf;
8ef97622 978
f4b9aa8d 979 if (!pending) {
db94535d 980 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
5f39d397
CM
981 if (buf) {
982 if (btrfs_buffer_uptodate(buf)) {
2c90e5d6
CM
983 u64 transid =
984 root->fs_info->running_transaction->transid;
5f39d397
CM
985 if (btrfs_header_generation(buf) == transid) {
986 free_extent_buffer(buf);
c549228f 987 return 1;
2c90e5d6 988 }
f4b9aa8d 989 }
5f39d397 990 free_extent_buffer(buf);
8ef97622 991 }
324ae4df 992 update_pinned_extents(root, bytenr, num_bytes, 1);
f4b9aa8d 993 } else {
1a5bc167 994 set_extent_bits(&root->fs_info->pending_del,
db94535d
CM
995 bytenr, bytenr + num_bytes - 1,
996 EXTENT_LOCKED, GFP_NOFS);
f4b9aa8d 997 }
be744175 998 BUG_ON(err < 0);
e20d96d6
CM
999 return 0;
1000}
1001
fec577fb 1002/*
a28ec197 1003 * remove an extent from the root, returns 0 on success
fec577fb 1004 */
e089f05c 1005static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1006 *root, u64 bytenr, u64 num_bytes,
1007 u64 root_objectid, u64 ref_generation,
1008 u64 owner_objectid, u64 owner_offset, int pin,
e37c9e69 1009 int mark_free)
a28ec197 1010{
5caf2a00 1011 struct btrfs_path *path;
e2fa7227 1012 struct btrfs_key key;
1261ec42
CM
1013 struct btrfs_fs_info *info = root->fs_info;
1014 struct btrfs_root *extent_root = info->extent_root;
5f39d397 1015 struct extent_buffer *leaf;
a28ec197 1016 int ret;
234b63a0 1017 struct btrfs_extent_item *ei;
cf27e1ee 1018 u32 refs;
037e6390 1019
db94535d 1020 key.objectid = bytenr;
62e2749e 1021 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
db94535d 1022 key.offset = num_bytes;
a28ec197 1023
5caf2a00 1024 path = btrfs_alloc_path();
54aa1f4d
CM
1025 if (!path)
1026 return -ENOMEM;
5f26f772 1027
7bb86316
CM
1028 if (ref_generation && owner_objectid == 0 && root_objectid == 3) {
1029//printk("drop backref root %Lu gen %Lu byte %Lu\n", root_objectid, ref_generation, bytenr );
1030 }
1031 ret = lookup_extent_backref(trans, extent_root, path,
1032 bytenr, root_objectid,
1033 ref_generation,
1034 owner_objectid, owner_offset, 1);
1035 if (ret == 0) {
1036 ret = btrfs_del_item(trans, extent_root, path);
1037 } else {
1038 btrfs_print_leaf(extent_root, path->nodes[0]);
1039 WARN_ON(1);
1040 printk("Unable to find ref byte nr %Lu root %Lu "
1041 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1042 root_objectid, ref_generation, owner_objectid,
1043 owner_offset);
1044 }
1045 btrfs_release_path(extent_root, path);
54aa1f4d
CM
1046 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1047 if (ret < 0)
1048 return ret;
1049 BUG_ON(ret);
5f39d397
CM
1050
1051 leaf = path->nodes[0];
1052 ei = btrfs_item_ptr(leaf, path->slots[0],
123abc88 1053 struct btrfs_extent_item);
5f39d397
CM
1054 refs = btrfs_extent_refs(leaf, ei);
1055 BUG_ON(refs == 0);
1056 refs -= 1;
1057 btrfs_set_extent_refs(leaf, ei, refs);
1058 btrfs_mark_buffer_dirty(leaf);
1059
cf27e1ee 1060 if (refs == 0) {
db94535d
CM
1061 u64 super_used;
1062 u64 root_used;
78fae27e
CM
1063
1064 if (pin) {
db94535d 1065 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
c549228f
Y
1066 if (ret > 0)
1067 mark_free = 1;
1068 BUG_ON(ret < 0);
78fae27e
CM
1069 }
1070
58176a96 1071 /* block accounting for super block */
db94535d
CM
1072 super_used = btrfs_super_bytes_used(&info->super_copy);
1073 btrfs_set_super_bytes_used(&info->super_copy,
1074 super_used - num_bytes);
58176a96
JB
1075
1076 /* block accounting for root item */
db94535d 1077 root_used = btrfs_root_used(&root->root_item);
5f39d397 1078 btrfs_set_root_used(&root->root_item,
db94535d 1079 root_used - num_bytes);
58176a96 1080
5caf2a00 1081 ret = btrfs_del_item(trans, extent_root, path);
54aa1f4d
CM
1082 if (ret) {
1083 return ret;
1084 }
db94535d 1085 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1e2677e0 1086 mark_free, 0);
9078a3e1 1087 BUG_ON(ret);
a28ec197 1088 }
5caf2a00 1089 btrfs_free_path(path);
e089f05c 1090 finish_current_insert(trans, extent_root);
a28ec197
CM
1091 return ret;
1092}
1093
a28ec197
CM
1094/*
1095 * find all the blocks marked as pending in the radix tree and remove
1096 * them from the extent map
1097 */
e089f05c
CM
1098static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1099 btrfs_root *extent_root)
a28ec197
CM
1100{
1101 int ret;
e20d96d6 1102 int err = 0;
1a5bc167
CM
1103 u64 start;
1104 u64 end;
1105 struct extent_map_tree *pending_del;
1106 struct extent_map_tree *pinned_extents;
8ef97622 1107
1a5bc167
CM
1108 pending_del = &extent_root->fs_info->pending_del;
1109 pinned_extents = &extent_root->fs_info->pinned_extents;
a28ec197
CM
1110
1111 while(1) {
1a5bc167
CM
1112 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1113 EXTENT_LOCKED);
1114 if (ret)
a28ec197 1115 break;
324ae4df 1116 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1a5bc167
CM
1117 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1118 GFP_NOFS);
1119 ret = __free_extent(trans, extent_root,
7bb86316
CM
1120 start, end + 1 - start,
1121 extent_root->root_key.objectid,
1122 0, 0, 0, 0, 0);
1a5bc167
CM
1123 if (ret)
1124 err = ret;
fec577fb 1125 }
e20d96d6 1126 return err;
fec577fb
CM
1127}
1128
1129/*
1130 * remove an extent from the root, returns 0 on success
1131 */
e089f05c 1132int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
7bb86316
CM
1133 *root, u64 bytenr, u64 num_bytes,
1134 u64 root_objectid, u64 ref_generation,
1135 u64 owner_objectid, u64 owner_offset, int pin)
fec577fb 1136{
9f5fae2f 1137 struct btrfs_root *extent_root = root->fs_info->extent_root;
fec577fb
CM
1138 int pending_ret;
1139 int ret;
a28ec197 1140
db94535d 1141 WARN_ON(num_bytes < root->sectorsize);
7bb86316
CM
1142 if (!root->ref_cows)
1143 ref_generation = 0;
1144
fec577fb 1145 if (root == extent_root) {
db94535d 1146 pin_down_bytes(root, bytenr, num_bytes, 1);
fec577fb
CM
1147 return 0;
1148 }
7bb86316
CM
1149 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1150 ref_generation, owner_objectid, owner_offset,
1151 pin, pin == 0);
e20d96d6 1152 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
fec577fb
CM
1153 return ret ? ret : pending_ret;
1154}
1155
87ee04eb
CM
1156static u64 stripe_align(struct btrfs_root *root, u64 val)
1157{
1158 u64 mask = ((u64)root->stripesize - 1);
1159 u64 ret = (val + mask) & ~mask;
1160 return ret;
1161}
1162
fec577fb
CM
1163/*
1164 * walks the btree of allocated extents and find a hole of a given size.
1165 * The key ins is changed to record the hole:
1166 * ins->objectid == block start
62e2749e 1167 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
1168 * ins->offset == number of blocks
1169 * Any available blocks before search_start are skipped.
1170 */
e089f05c 1171static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
db94535d
CM
1172 *orig_root, u64 num_bytes, u64 empty_size,
1173 u64 search_start, u64 search_end, u64 hint_byte,
f2654de4
CM
1174 struct btrfs_key *ins, u64 exclude_start,
1175 u64 exclude_nr, int data)
fec577fb 1176{
5caf2a00 1177 struct btrfs_path *path;
e2fa7227 1178 struct btrfs_key key;
fec577fb 1179 u64 hole_size = 0;
87ee04eb
CM
1180 u64 aligned;
1181 int ret;
fec577fb 1182 int slot = 0;
db94535d 1183 u64 last_byte = 0;
be744175 1184 u64 orig_search_start = search_start;
fec577fb 1185 int start_found;
5f39d397 1186 struct extent_buffer *l;
9f5fae2f 1187 struct btrfs_root * root = orig_root->fs_info->extent_root;
f2458e1d 1188 struct btrfs_fs_info *info = root->fs_info;
db94535d 1189 u64 total_needed = num_bytes;
e20d96d6 1190 int level;
be08c1b9 1191 struct btrfs_block_group_cache *block_group;
be744175 1192 int full_scan = 0;
fbdc762b 1193 int wrapped = 0;
f84a8b36 1194 u64 cached_start;
fec577fb 1195
db94535d 1196 WARN_ON(num_bytes < root->sectorsize);
b1a4d965
CM
1197 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1198
5f39d397
CM
1199 level = btrfs_header_level(root->node);
1200
015a739c 1201 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
257d0ce3
CM
1202 data = BTRFS_BLOCK_GROUP_MIXED;
1203 }
1204
3e1ad54f 1205 if (search_end == (u64)-1)
db94535d
CM
1206 search_end = btrfs_super_total_bytes(&info->super_copy);
1207 if (hint_byte) {
1208 block_group = btrfs_lookup_block_group(info, hint_byte);
1a2b2ac7
CM
1209 if (!block_group)
1210 hint_byte = search_start;
be744175 1211 block_group = btrfs_find_block_group(root, block_group,
db94535d 1212 hint_byte, data, 1);
be744175
CM
1213 } else {
1214 block_group = btrfs_find_block_group(root,
1a2b2ac7
CM
1215 trans->block_group,
1216 search_start, data, 1);
be744175
CM
1217 }
1218
6702ed49 1219 total_needed += empty_size;
e011599b 1220 path = btrfs_alloc_path();
be744175 1221check_failed:
5e5745dc
Y
1222 search_start = find_search_start(root, &block_group, search_start,
1223 total_needed, data, full_scan);
87ee04eb 1224 search_start = stripe_align(root, search_start);
f84a8b36 1225 cached_start = search_start;
5caf2a00 1226 btrfs_init_path(path);
fec577fb
CM
1227 ins->objectid = search_start;
1228 ins->offset = 0;
fec577fb 1229 start_found = 0;
2cc58cf2 1230 path->reada = 2;
e37c9e69 1231
5caf2a00 1232 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
0f70abe2
CM
1233 if (ret < 0)
1234 goto error;
aa5d6bed 1235
e37c9e69 1236 if (path->slots[0] > 0) {
5caf2a00 1237 path->slots[0]--;
e37c9e69
CM
1238 }
1239
5f39d397
CM
1240 l = path->nodes[0];
1241 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1242
e37c9e69 1243 /*
7bb86316 1244 * walk backwards to find the first extent item key
e37c9e69 1245 */
7bb86316
CM
1246 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1247 if (path->slots[0] == 0) {
1248 ret = btrfs_prev_leaf(root, path);
1249 if (ret != 0) {
1250 ret = btrfs_search_slot(trans, root, ins,
1251 path, 0, 0);
1252 if (ret < 0)
1253 goto error;
1254 if (path->slots[0] > 0)
1255 path->slots[0]--;
1256 break;
1257 }
1258 } else {
e37c9e69
CM
1259 path->slots[0]--;
1260 }
7bb86316
CM
1261 l = path->nodes[0];
1262 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
e37c9e69 1263 }
fec577fb 1264 while (1) {
5f39d397 1265 l = path->nodes[0];
5caf2a00 1266 slot = path->slots[0];
5f39d397 1267 if (slot >= btrfs_header_nritems(l)) {
5caf2a00 1268 ret = btrfs_next_leaf(root, path);
fec577fb
CM
1269 if (ret == 0)
1270 continue;
0f70abe2
CM
1271 if (ret < 0)
1272 goto error;
e19caa5f
CM
1273
1274 search_start = max(search_start,
1275 block_group->key.objectid);
fec577fb 1276 if (!start_found) {
87ee04eb
CM
1277 aligned = stripe_align(root, search_start);
1278 ins->objectid = aligned;
1279 if (aligned >= search_end) {
1280 ret = -ENOSPC;
1281 goto error;
1282 }
1283 ins->offset = search_end - aligned;
fec577fb
CM
1284 start_found = 1;
1285 goto check_pending;
1286 }
87ee04eb
CM
1287 ins->objectid = stripe_align(root,
1288 last_byte > search_start ?
1289 last_byte : search_start);
1290 if (search_end <= ins->objectid) {
1291 ret = -ENOSPC;
1292 goto error;
1293 }
3e1ad54f 1294 ins->offset = search_end - ins->objectid;
e19caa5f 1295 BUG_ON(ins->objectid >= search_end);
fec577fb
CM
1296 goto check_pending;
1297 }
5f39d397 1298 btrfs_item_key_to_cpu(l, &key, slot);
96b5179d 1299
db94535d 1300 if (key.objectid >= search_start && key.objectid > last_byte &&
e37c9e69 1301 start_found) {
db94535d
CM
1302 if (last_byte < search_start)
1303 last_byte = search_start;
87ee04eb
CM
1304 aligned = stripe_align(root, last_byte);
1305 hole_size = key.objectid - aligned;
1306 if (key.objectid > aligned && hole_size >= num_bytes) {
1307 ins->objectid = aligned;
e37c9e69
CM
1308 ins->offset = hole_size;
1309 goto check_pending;
0579da42 1310 }
fec577fb 1311 }
96b5179d 1312 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
7bb86316
CM
1313 if (!start_found && btrfs_key_type(&key) ==
1314 BTRFS_BLOCK_GROUP_ITEM_KEY) {
db94535d 1315 last_byte = key.objectid;
96b5179d
CM
1316 start_found = 1;
1317 }
e37c9e69 1318 goto next;
96b5179d
CM
1319 }
1320
e37c9e69 1321
0579da42 1322 start_found = 1;
db94535d 1323 last_byte = key.objectid + key.offset;
f510cfec 1324
257d0ce3
CM
1325 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1326 last_byte >= block_group->key.objectid +
be744175
CM
1327 block_group->key.offset) {
1328 btrfs_release_path(root, path);
1329 search_start = block_group->key.objectid +
e19caa5f 1330 block_group->key.offset;
be744175
CM
1331 goto new_group;
1332 }
9078a3e1 1333next:
5caf2a00 1334 path->slots[0]++;
de428b63 1335 cond_resched();
fec577fb 1336 }
fec577fb
CM
1337check_pending:
1338 /* we have to make sure we didn't find an extent that has already
1339 * been allocated by the map tree or the original allocation
1340 */
5caf2a00 1341 btrfs_release_path(root, path);
fec577fb 1342 BUG_ON(ins->objectid < search_start);
e37c9e69 1343
db94535d 1344 if (ins->objectid + num_bytes >= search_end)
cf67582b 1345 goto enospc;
257d0ce3 1346 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
5cf66426 1347 ins->objectid + num_bytes > block_group->
e19caa5f
CM
1348 key.objectid + block_group->key.offset) {
1349 search_start = block_group->key.objectid +
1350 block_group->key.offset;
1351 goto new_group;
1352 }
1a5bc167 1353 if (test_range_bit(&info->extent_ins, ins->objectid,
db94535d
CM
1354 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1355 search_start = ins->objectid + num_bytes;
1a5bc167
CM
1356 goto new_group;
1357 }
1358 if (test_range_bit(&info->pinned_extents, ins->objectid,
db94535d
CM
1359 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1360 search_start = ins->objectid + num_bytes;
1a5bc167 1361 goto new_group;
fec577fb 1362 }
db94535d 1363 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
f2654de4
CM
1364 ins->objectid < exclude_start + exclude_nr)) {
1365 search_start = exclude_start + exclude_nr;
1366 goto new_group;
1367 }
e37c9e69 1368 if (!data) {
5276aeda 1369 block_group = btrfs_lookup_block_group(info, ins->objectid);
26b8003f
CM
1370 if (block_group)
1371 trans->block_group = block_group;
f2458e1d 1372 }
db94535d 1373 ins->offset = num_bytes;
5caf2a00 1374 btrfs_free_path(path);
fec577fb 1375 return 0;
be744175
CM
1376
1377new_group:
db94535d 1378 if (search_start + num_bytes >= search_end) {
cf67582b 1379enospc:
be744175 1380 search_start = orig_search_start;
fbdc762b
CM
1381 if (full_scan) {
1382 ret = -ENOSPC;
1383 goto error;
1384 }
6702ed49
CM
1385 if (wrapped) {
1386 if (!full_scan)
1387 total_needed -= empty_size;
fbdc762b 1388 full_scan = 1;
1a2b2ac7 1389 data = BTRFS_BLOCK_GROUP_MIXED;
6702ed49 1390 } else
fbdc762b 1391 wrapped = 1;
be744175 1392 }
5276aeda 1393 block_group = btrfs_lookup_block_group(info, search_start);
fbdc762b 1394 cond_resched();
1a2b2ac7
CM
1395 block_group = btrfs_find_block_group(root, block_group,
1396 search_start, data, 0);
be744175
CM
1397 goto check_failed;
1398
0f70abe2 1399error:
5caf2a00
CM
1400 btrfs_release_path(root, path);
1401 btrfs_free_path(path);
0f70abe2 1402 return ret;
fec577fb 1403}
fec577fb
CM
1404/*
1405 * finds a free extent and does all the dirty work required for allocation
1406 * returns the key for the extent through ins, and a tree buffer for
1407 * the first block of the extent through buf.
1408 *
1409 * returns 0 if everything worked, non-zero otherwise.
1410 */
4d775673 1411int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
7bb86316
CM
1412 struct btrfs_root *root,
1413 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1414 u64 owner, u64 owner_offset,
1415 u64 empty_size, u64 hint_byte,
be08c1b9 1416 u64 search_end, struct btrfs_key *ins, int data)
fec577fb
CM
1417{
1418 int ret;
1419 int pending_ret;
db94535d 1420 u64 super_used, root_used;
fbdc762b 1421 u64 search_start = 0;
1261ec42
CM
1422 struct btrfs_fs_info *info = root->fs_info;
1423 struct btrfs_root *extent_root = info->extent_root;
234b63a0 1424 struct btrfs_extent_item extent_item;
7bb86316 1425 struct btrfs_path *path;
037e6390 1426
5f39d397 1427 btrfs_set_stack_extent_refs(&extent_item, 1);
fec577fb 1428
db94535d
CM
1429 WARN_ON(num_bytes < root->sectorsize);
1430 ret = find_free_extent(trans, root, num_bytes, empty_size,
1431 search_start, search_end, hint_byte, ins,
26b8003f
CM
1432 trans->alloc_exclude_start,
1433 trans->alloc_exclude_nr, data);
ccd467d6 1434 BUG_ON(ret);
f2654de4
CM
1435 if (ret)
1436 return ret;
fec577fb 1437
58176a96 1438 /* block accounting for super block */
db94535d
CM
1439 super_used = btrfs_super_bytes_used(&info->super_copy);
1440 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
26b8003f 1441
58176a96 1442 /* block accounting for root item */
db94535d
CM
1443 root_used = btrfs_root_used(&root->root_item);
1444 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
58176a96 1445
f510cfec
CM
1446 clear_extent_dirty(&root->fs_info->free_space_cache,
1447 ins->objectid, ins->objectid + ins->offset - 1,
1448 GFP_NOFS);
1449
26b8003f 1450 if (root == extent_root) {
1a5bc167
CM
1451 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1452 ins->objectid + ins->offset - 1,
1453 EXTENT_LOCKED, GFP_NOFS);
e19caa5f 1454 WARN_ON(data == 1);
26b8003f
CM
1455 goto update_block;
1456 }
1457
1458 WARN_ON(trans->alloc_exclude_nr);
1459 trans->alloc_exclude_start = ins->objectid;
1460 trans->alloc_exclude_nr = ins->offset;
e089f05c
CM
1461 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1462 sizeof(extent_item));
037e6390 1463
26b8003f
CM
1464 trans->alloc_exclude_start = 0;
1465 trans->alloc_exclude_nr = 0;
7bb86316
CM
1466 BUG_ON(ret);
1467
1468 path = btrfs_alloc_path();
1469 BUG_ON(!path);
1470 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1471 ins->objectid, root_objectid,
1472 ref_generation, owner, owner_offset);
26b8003f 1473
ccd467d6 1474 BUG_ON(ret);
7bb86316 1475 btrfs_free_path(path);
e089f05c 1476 finish_current_insert(trans, extent_root);
e20d96d6 1477 pending_ret = del_pending_extents(trans, extent_root);
f510cfec 1478
e37c9e69 1479 if (ret) {
037e6390 1480 return ret;
e37c9e69
CM
1481 }
1482 if (pending_ret) {
037e6390 1483 return pending_ret;
e37c9e69 1484 }
26b8003f
CM
1485
1486update_block:
1e2677e0
CM
1487 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1488 data);
fabb5681 1489 BUG_ON(ret);
037e6390 1490 return 0;
fec577fb
CM
1491}
1492
1493/*
1494 * helper function to allocate a block for a given tree
1495 * returns the tree buffer or NULL.
1496 */
5f39d397 1497struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
db94535d 1498 struct btrfs_root *root,
7bb86316
CM
1499 u32 blocksize,
1500 u64 root_objectid, u64 hint,
1501 u64 empty_size)
1502{
1503 u64 ref_generation;
1504
1505 if (root->ref_cows)
1506 ref_generation = trans->transid;
1507 else
1508 ref_generation = 0;
1509
1510
1511 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1512 ref_generation, 0, 0, hint, empty_size);
1513}
1514
1515/*
1516 * helper function to allocate a block for a given tree
1517 * returns the tree buffer or NULL.
1518 */
1519struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1520 struct btrfs_root *root,
1521 u32 blocksize,
1522 u64 root_objectid,
1523 u64 ref_generation,
1524 u64 first_objectid,
1525 int level,
1526 u64 hint,
5f39d397 1527 u64 empty_size)
fec577fb 1528{
e2fa7227 1529 struct btrfs_key ins;
fec577fb 1530 int ret;
5f39d397 1531 struct extent_buffer *buf;
fec577fb 1532
7bb86316
CM
1533 ret = btrfs_alloc_extent(trans, root, blocksize,
1534 root_objectid, ref_generation,
1535 first_objectid, level, empty_size, hint,
db94535d 1536 (u64)-1, &ins, 0);
fec577fb 1537 if (ret) {
54aa1f4d
CM
1538 BUG_ON(ret > 0);
1539 return ERR_PTR(ret);
fec577fb 1540 }
db94535d 1541 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
54aa1f4d 1542 if (!buf) {
7bb86316
CM
1543 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1544 root->root_key.objectid, ref_generation,
1545 0, 0, 0);
54aa1f4d
CM
1546 return ERR_PTR(-ENOMEM);
1547 }
5f39d397
CM
1548 btrfs_set_buffer_uptodate(buf);
1549 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1550 buf->start + buf->len - 1, GFP_NOFS);
19c00ddc
CM
1551 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1552 buf->start, buf->start + buf->len - 1,
1553 EXTENT_CSUM, GFP_NOFS);
1554 buf->flags |= EXTENT_CSUM;
6b80053d 1555 btrfs_set_buffer_defrag(buf);
d3c2fdcf 1556 trans->blocks_used++;
fec577fb
CM
1557 return buf;
1558}
a28ec197 1559
6407bf6d 1560static int drop_leaf_ref(struct btrfs_trans_handle *trans,
5f39d397 1561 struct btrfs_root *root, struct extent_buffer *leaf)
6407bf6d 1562{
7bb86316
CM
1563 u64 leaf_owner;
1564 u64 leaf_generation;
5f39d397 1565 struct btrfs_key key;
6407bf6d
CM
1566 struct btrfs_file_extent_item *fi;
1567 int i;
1568 int nritems;
1569 int ret;
1570
5f39d397
CM
1571 BUG_ON(!btrfs_is_leaf(leaf));
1572 nritems = btrfs_header_nritems(leaf);
7bb86316
CM
1573 leaf_owner = btrfs_header_owner(leaf);
1574 leaf_generation = btrfs_header_generation(leaf);
1575
6407bf6d 1576 for (i = 0; i < nritems; i++) {
db94535d 1577 u64 disk_bytenr;
5f39d397
CM
1578
1579 btrfs_item_key_to_cpu(leaf, &key, i);
1580 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
6407bf6d
CM
1581 continue;
1582 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5f39d397
CM
1583 if (btrfs_file_extent_type(leaf, fi) ==
1584 BTRFS_FILE_EXTENT_INLINE)
236454df 1585 continue;
6407bf6d
CM
1586 /*
1587 * FIXME make sure to insert a trans record that
1588 * repeats the snapshot del on crash
1589 */
db94535d
CM
1590 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1591 if (disk_bytenr == 0)
3a686375 1592 continue;
db94535d 1593 ret = btrfs_free_extent(trans, root, disk_bytenr,
7bb86316
CM
1594 btrfs_file_extent_disk_num_bytes(leaf, fi),
1595 leaf_owner, leaf_generation,
1596 key.objectid, key.offset, 0);
6407bf6d
CM
1597 BUG_ON(ret);
1598 }
1599 return 0;
1600}
1601
e011599b 1602static void reada_walk_down(struct btrfs_root *root,
5f39d397 1603 struct extent_buffer *node)
e011599b
CM
1604{
1605 int i;
1606 u32 nritems;
db94535d 1607 u64 bytenr;
e011599b
CM
1608 int ret;
1609 u32 refs;
db94535d
CM
1610 int level;
1611 u32 blocksize;
e011599b 1612
5f39d397 1613 nritems = btrfs_header_nritems(node);
db94535d 1614 level = btrfs_header_level(node);
e011599b 1615 for (i = 0; i < nritems; i++) {
db94535d
CM
1616 bytenr = btrfs_node_blockptr(node, i);
1617 blocksize = btrfs_level_size(root, level - 1);
1618 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
e011599b
CM
1619 BUG_ON(ret);
1620 if (refs != 1)
1621 continue;
409eb95d 1622 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1623 ret = readahead_tree_block(root, bytenr, blocksize);
409eb95d
CM
1624 cond_resched();
1625 mutex_lock(&root->fs_info->fs_mutex);
e011599b
CM
1626 if (ret)
1627 break;
1628 }
1629}
1630
9aca1d51
CM
1631/*
1632 * helper function for drop_snapshot, this walks down the tree dropping ref
1633 * counts as it goes.
1634 */
e089f05c
CM
1635static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1636 *root, struct btrfs_path *path, int *level)
20524f02 1637{
7bb86316
CM
1638 u64 root_owner;
1639 u64 root_gen;
1640 u64 bytenr;
5f39d397
CM
1641 struct extent_buffer *next;
1642 struct extent_buffer *cur;
7bb86316 1643 struct extent_buffer *parent;
db94535d 1644 u32 blocksize;
20524f02
CM
1645 int ret;
1646 u32 refs;
1647
5caf2a00
CM
1648 WARN_ON(*level < 0);
1649 WARN_ON(*level >= BTRFS_MAX_LEVEL);
5f39d397 1650 ret = lookup_extent_ref(trans, root,
db94535d
CM
1651 path->nodes[*level]->start,
1652 path->nodes[*level]->len, &refs);
20524f02
CM
1653 BUG_ON(ret);
1654 if (refs > 1)
1655 goto out;
e011599b 1656
9aca1d51
CM
1657 /*
1658 * walk down to the last node level and free all the leaves
1659 */
6407bf6d 1660 while(*level >= 0) {
5caf2a00
CM
1661 WARN_ON(*level < 0);
1662 WARN_ON(*level >= BTRFS_MAX_LEVEL);
20524f02 1663 cur = path->nodes[*level];
e011599b
CM
1664
1665 if (*level > 0 && path->slots[*level] == 0)
5f39d397 1666 reada_walk_down(root, cur);
e011599b 1667
5f39d397 1668 if (btrfs_header_level(cur) != *level)
2c90e5d6 1669 WARN_ON(1);
e011599b 1670
7518a238 1671 if (path->slots[*level] >=
5f39d397 1672 btrfs_header_nritems(cur))
20524f02 1673 break;
6407bf6d
CM
1674 if (*level == 0) {
1675 ret = drop_leaf_ref(trans, root, cur);
1676 BUG_ON(ret);
1677 break;
1678 }
db94535d
CM
1679 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1680 blocksize = btrfs_level_size(root, *level - 1);
1681 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
6407bf6d
CM
1682 BUG_ON(ret);
1683 if (refs != 1) {
7bb86316
CM
1684 parent = path->nodes[*level];
1685 root_owner = btrfs_header_owner(parent);
1686 root_gen = btrfs_header_generation(parent);
20524f02 1687 path->slots[*level]++;
db94535d 1688 ret = btrfs_free_extent(trans, root, bytenr,
7bb86316
CM
1689 blocksize, root_owner,
1690 root_gen, 0, 0, 1);
20524f02
CM
1691 BUG_ON(ret);
1692 continue;
1693 }
db94535d 1694 next = btrfs_find_tree_block(root, bytenr, blocksize);
5f39d397
CM
1695 if (!next || !btrfs_buffer_uptodate(next)) {
1696 free_extent_buffer(next);
e9d0b13b 1697 mutex_unlock(&root->fs_info->fs_mutex);
db94535d 1698 next = read_tree_block(root, bytenr, blocksize);
e9d0b13b
CM
1699 mutex_lock(&root->fs_info->fs_mutex);
1700
1701 /* we dropped the lock, check one more time */
db94535d
CM
1702 ret = lookup_extent_ref(trans, root, bytenr,
1703 blocksize, &refs);
e9d0b13b
CM
1704 BUG_ON(ret);
1705 if (refs != 1) {
7bb86316
CM
1706 parent = path->nodes[*level];
1707 root_owner = btrfs_header_owner(parent);
1708 root_gen = btrfs_header_generation(parent);
1709
e9d0b13b 1710 path->slots[*level]++;
5f39d397 1711 free_extent_buffer(next);
7bb86316
CM
1712 ret = btrfs_free_extent(trans, root, bytenr,
1713 blocksize,
1714 root_owner,
1715 root_gen, 0, 0, 1);
e9d0b13b
CM
1716 BUG_ON(ret);
1717 continue;
1718 }
1719 }
5caf2a00 1720 WARN_ON(*level <= 0);
83e15a28 1721 if (path->nodes[*level-1])
5f39d397 1722 free_extent_buffer(path->nodes[*level-1]);
20524f02 1723 path->nodes[*level-1] = next;
5f39d397 1724 *level = btrfs_header_level(next);
20524f02
CM
1725 path->slots[*level] = 0;
1726 }
1727out:
5caf2a00
CM
1728 WARN_ON(*level < 0);
1729 WARN_ON(*level >= BTRFS_MAX_LEVEL);
7bb86316
CM
1730
1731 if (path->nodes[*level] == root->node) {
1732 root_owner = root->root_key.objectid;
1733 parent = path->nodes[*level];
1734 } else {
1735 parent = path->nodes[*level + 1];
1736 root_owner = btrfs_header_owner(parent);
1737 }
1738
1739 root_gen = btrfs_header_generation(parent);
db94535d 1740 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
7bb86316
CM
1741 path->nodes[*level]->len,
1742 root_owner, root_gen, 0, 0, 1);
5f39d397 1743 free_extent_buffer(path->nodes[*level]);
20524f02
CM
1744 path->nodes[*level] = NULL;
1745 *level += 1;
1746 BUG_ON(ret);
1747 return 0;
1748}
1749
9aca1d51
CM
1750/*
1751 * helper for dropping snapshots. This walks back up the tree in the path
1752 * to find the first node higher up where we haven't yet gone through
1753 * all the slots
1754 */
e089f05c
CM
1755static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1756 *root, struct btrfs_path *path, int *level)
20524f02 1757{
7bb86316
CM
1758 u64 root_owner;
1759 u64 root_gen;
1760 struct btrfs_root_item *root_item = &root->root_item;
20524f02
CM
1761 int i;
1762 int slot;
1763 int ret;
9f3a7427 1764
234b63a0 1765 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
20524f02 1766 slot = path->slots[i];
5f39d397
CM
1767 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1768 struct extent_buffer *node;
1769 struct btrfs_disk_key disk_key;
1770 node = path->nodes[i];
20524f02
CM
1771 path->slots[i]++;
1772 *level = i;
9f3a7427 1773 WARN_ON(*level == 0);
5f39d397 1774 btrfs_node_key(node, &disk_key, path->slots[i]);
9f3a7427 1775 memcpy(&root_item->drop_progress,
5f39d397 1776 &disk_key, sizeof(disk_key));
9f3a7427 1777 root_item->drop_level = i;
20524f02
CM
1778 return 0;
1779 } else {
7bb86316
CM
1780 if (path->nodes[*level] == root->node) {
1781 root_owner = root->root_key.objectid;
1782 root_gen =
1783 btrfs_header_generation(path->nodes[*level]);
1784 } else {
1785 struct extent_buffer *node;
1786 node = path->nodes[*level + 1];
1787 root_owner = btrfs_header_owner(node);
1788 root_gen = btrfs_header_generation(node);
1789 }
e089f05c 1790 ret = btrfs_free_extent(trans, root,
db94535d 1791 path->nodes[*level]->start,
7bb86316
CM
1792 path->nodes[*level]->len,
1793 root_owner, root_gen, 0, 0, 1);
6407bf6d 1794 BUG_ON(ret);
5f39d397 1795 free_extent_buffer(path->nodes[*level]);
83e15a28 1796 path->nodes[*level] = NULL;
20524f02 1797 *level = i + 1;
20524f02
CM
1798 }
1799 }
1800 return 1;
1801}
1802
9aca1d51
CM
1803/*
1804 * drop the reference count on the tree rooted at 'snap'. This traverses
1805 * the tree freeing any blocks that have a ref count of zero after being
1806 * decremented.
1807 */
e089f05c 1808int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
9f3a7427 1809 *root)
20524f02 1810{
3768f368 1811 int ret = 0;
9aca1d51 1812 int wret;
20524f02 1813 int level;
5caf2a00 1814 struct btrfs_path *path;
20524f02
CM
1815 int i;
1816 int orig_level;
9f3a7427 1817 struct btrfs_root_item *root_item = &root->root_item;
20524f02 1818
5caf2a00
CM
1819 path = btrfs_alloc_path();
1820 BUG_ON(!path);
20524f02 1821
5f39d397 1822 level = btrfs_header_level(root->node);
20524f02 1823 orig_level = level;
9f3a7427
CM
1824 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1825 path->nodes[level] = root->node;
f510cfec 1826 extent_buffer_get(root->node);
9f3a7427
CM
1827 path->slots[level] = 0;
1828 } else {
1829 struct btrfs_key key;
5f39d397
CM
1830 struct btrfs_disk_key found_key;
1831 struct extent_buffer *node;
6702ed49 1832
9f3a7427 1833 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6702ed49
CM
1834 level = root_item->drop_level;
1835 path->lowest_level = level;
9f3a7427 1836 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6702ed49 1837 if (wret < 0) {
9f3a7427
CM
1838 ret = wret;
1839 goto out;
1840 }
5f39d397
CM
1841 node = path->nodes[level];
1842 btrfs_node_key(node, &found_key, path->slots[level]);
1843 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1844 sizeof(found_key)));
9f3a7427 1845 }
20524f02 1846 while(1) {
5caf2a00 1847 wret = walk_down_tree(trans, root, path, &level);
9aca1d51 1848 if (wret > 0)
20524f02 1849 break;
9aca1d51
CM
1850 if (wret < 0)
1851 ret = wret;
1852
5caf2a00 1853 wret = walk_up_tree(trans, root, path, &level);
9aca1d51 1854 if (wret > 0)
20524f02 1855 break;
9aca1d51
CM
1856 if (wret < 0)
1857 ret = wret;
409eb95d 1858 ret = -EAGAIN;
409eb95d 1859 break;
20524f02 1860 }
83e15a28 1861 for (i = 0; i <= orig_level; i++) {
5caf2a00 1862 if (path->nodes[i]) {
5f39d397 1863 free_extent_buffer(path->nodes[i]);
0f82731f 1864 path->nodes[i] = NULL;
83e15a28 1865 }
20524f02 1866 }
9f3a7427 1867out:
5caf2a00 1868 btrfs_free_path(path);
9aca1d51 1869 return ret;
20524f02 1870}
9078a3e1 1871
96b5179d 1872int btrfs_free_block_groups(struct btrfs_fs_info *info)
9078a3e1 1873{
96b5179d
CM
1874 u64 start;
1875 u64 end;
b97f9203 1876 u64 ptr;
9078a3e1 1877 int ret;
9078a3e1 1878 while(1) {
96b5179d
CM
1879 ret = find_first_extent_bit(&info->block_group_cache, 0,
1880 &start, &end, (unsigned int)-1);
1881 if (ret)
9078a3e1 1882 break;
b97f9203
Y
1883 ret = get_state_private(&info->block_group_cache, start, &ptr);
1884 if (!ret)
1885 kfree((void *)(unsigned long)ptr);
96b5179d
CM
1886 clear_extent_bits(&info->block_group_cache, start,
1887 end, (unsigned int)-1, GFP_NOFS);
9078a3e1 1888 }
e37c9e69 1889 while(1) {
f510cfec
CM
1890 ret = find_first_extent_bit(&info->free_space_cache, 0,
1891 &start, &end, EXTENT_DIRTY);
1892 if (ret)
e37c9e69 1893 break;
f510cfec
CM
1894 clear_extent_dirty(&info->free_space_cache, start,
1895 end, GFP_NOFS);
e37c9e69 1896 }
be744175
CM
1897 return 0;
1898}
1899
9078a3e1
CM
1900int btrfs_read_block_groups(struct btrfs_root *root)
1901{
1902 struct btrfs_path *path;
1903 int ret;
1904 int err = 0;
96b5179d 1905 int bit;
9078a3e1 1906 struct btrfs_block_group_cache *cache;
be744175 1907 struct btrfs_fs_info *info = root->fs_info;
96b5179d 1908 struct extent_map_tree *block_group_cache;
9078a3e1
CM
1909 struct btrfs_key key;
1910 struct btrfs_key found_key;
5f39d397 1911 struct extent_buffer *leaf;
96b5179d
CM
1912
1913 block_group_cache = &info->block_group_cache;
9078a3e1 1914
be744175 1915 root = info->extent_root;
9078a3e1 1916 key.objectid = 0;
db94535d 1917 key.offset = BTRFS_BLOCK_GROUP_SIZE;
9078a3e1
CM
1918 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1919
1920 path = btrfs_alloc_path();
1921 if (!path)
1922 return -ENOMEM;
1923
1924 while(1) {
be744175 1925 ret = btrfs_search_slot(NULL, info->extent_root,
9078a3e1
CM
1926 &key, path, 0, 0);
1927 if (ret != 0) {
1928 err = ret;
1929 break;
1930 }
5f39d397
CM
1931 leaf = path->nodes[0];
1932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9078a3e1
CM
1933 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1934 if (!cache) {
1935 err = -1;
1936 break;
1937 }
3e1ad54f 1938
5f39d397
CM
1939 read_extent_buffer(leaf, &cache->item,
1940 btrfs_item_ptr_offset(leaf, path->slots[0]),
1941 sizeof(cache->item));
9078a3e1 1942 memcpy(&cache->key, &found_key, sizeof(found_key));
e37c9e69 1943 cache->cached = 0;
324ae4df 1944 cache->pinned = 0;
9078a3e1
CM
1945 key.objectid = found_key.objectid + found_key.offset;
1946 btrfs_release_path(root, path);
5f39d397 1947
f84a8b36
CM
1948 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1949 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1950 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1951 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
96b5179d 1952 bit = BLOCK_GROUP_DATA;
f84a8b36 1953 cache->data = BTRFS_BLOCK_GROUP_DATA;
96b5179d
CM
1954 } else {
1955 bit = BLOCK_GROUP_METADATA;
1956 cache->data = 0;
31f3c99b 1957 }
96b5179d
CM
1958
1959 /* use EXTENT_LOCKED to prevent merging */
1960 set_extent_bits(block_group_cache, found_key.objectid,
1961 found_key.objectid + found_key.offset - 1,
1962 bit | EXTENT_LOCKED, GFP_NOFS);
1963 set_state_private(block_group_cache, found_key.objectid,
ae2f5411 1964 (unsigned long)cache);
96b5179d 1965
9078a3e1 1966 if (key.objectid >=
db94535d 1967 btrfs_super_total_bytes(&info->super_copy))
9078a3e1
CM
1968 break;
1969 }
1970
1971 btrfs_free_path(path);
1972 return 0;
1973}