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