Rename the extent_map code to extent_io
[btrfs-progs-unstable/devel.git] / extent-tree.c
blobd3d872a7442f6ff6471230c2f6ef44325b441677
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "print-tree.h"
26 #include "transaction.h"
27 #include "crc32c.h"
29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
31 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
33 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
35 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
37 static int find_previous_extent(struct btrfs_root *root,
38 struct btrfs_path *path)
40 struct btrfs_key found_key;
41 struct extent_buffer *leaf;
42 int ret;
44 while(1) {
45 if (path->slots[0] == 0) {
46 ret = btrfs_prev_leaf(root, path);
47 if (ret != 0)
48 return ret;
49 } else {
50 path->slots[0]--;
52 leaf = path->nodes[0];
53 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
54 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
55 return 0;
57 return 1;
59 static int cache_block_group(struct btrfs_root *root,
60 struct btrfs_block_group_cache *block_group)
62 struct btrfs_path *path;
63 int ret;
64 struct btrfs_key key;
65 struct extent_buffer *leaf;
66 struct extent_io_tree *free_space_cache;
67 int slot;
68 u64 last = 0;
69 u64 hole_size;
70 u64 first_free;
71 int found = 0;
73 if (!block_group)
74 return 0;
76 root = root->fs_info->extent_root;
77 free_space_cache = &root->fs_info->free_space_cache;
79 if (block_group->cached)
80 return 0;
82 path = btrfs_alloc_path();
83 if (!path)
84 return -ENOMEM;
86 path->reada = 2;
87 first_free = block_group->key.objectid;
88 key.objectid = block_group->key.objectid;
89 key.offset = 0;
90 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
91 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
92 if (ret < 0)
93 return ret;
94 ret = find_previous_extent(root, path);
95 if (ret < 0)
96 return ret;
97 if (ret == 0) {
98 leaf = path->nodes[0];
99 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
100 if (key.objectid + key.offset > first_free)
101 first_free = key.objectid + key.offset;
103 while(1) {
104 leaf = path->nodes[0];
105 slot = path->slots[0];
106 if (slot >= btrfs_header_nritems(leaf)) {
107 ret = btrfs_next_leaf(root, path);
108 if (ret < 0)
109 goto err;
110 if (ret == 0) {
111 continue;
112 } else {
113 break;
116 btrfs_item_key_to_cpu(leaf, &key, slot);
117 if (key.objectid < block_group->key.objectid) {
118 goto next;
120 if (key.objectid >= block_group->key.objectid +
121 block_group->key.offset) {
122 break;
125 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
126 if (!found) {
127 last = first_free;
128 found = 1;
130 if (key.objectid > last) {
131 hole_size = key.objectid - last;
132 set_extent_dirty(free_space_cache, last,
133 last + hole_size - 1,
134 GFP_NOFS);
136 last = key.objectid + key.offset;
138 next:
139 path->slots[0]++;
142 if (!found)
143 last = first_free;
144 if (block_group->key.objectid +
145 block_group->key.offset > last) {
146 hole_size = block_group->key.objectid +
147 block_group->key.offset - last;
148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1, GFP_NOFS);
151 block_group->cached = 1;
152 err:
153 btrfs_free_path(path);
154 return 0;
157 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
158 btrfs_fs_info *info,
159 u64 bytenr)
161 struct extent_io_tree *block_group_cache;
162 struct btrfs_block_group_cache *block_group = NULL;
163 u64 ptr;
164 u64 start;
165 u64 end;
166 int ret;
168 block_group_cache = &info->block_group_cache;
169 ret = find_first_extent_bit(block_group_cache,
170 bytenr, &start, &end,
171 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
172 if (ret) {
173 return NULL;
175 ret = get_state_private(block_group_cache, start, &ptr);
176 if (ret)
177 return NULL;
179 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
180 if (block_group->key.objectid <= bytenr && bytenr <
181 block_group->key.objectid + block_group->key.offset)
182 return block_group;
183 return NULL;
185 static u64 noinline find_search_start(struct btrfs_root *root,
186 struct btrfs_block_group_cache **cache_ret,
187 u64 search_start, int num, int data)
189 int ret;
190 struct btrfs_block_group_cache *cache = *cache_ret;
191 u64 last;
192 u64 start = 0;
193 u64 end = 0;
194 u64 cache_miss = 0;
195 int wrapped = 0;
197 if (!cache) {
198 goto out;
200 again:
201 ret = cache_block_group(root, cache);
202 if (ret)
203 goto out;
205 last = max(search_start, cache->key.objectid);
207 while(1) {
208 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
209 last, &start, &end, EXTENT_DIRTY);
210 if (ret) {
211 if (!cache_miss)
212 cache_miss = last;
213 goto new_group;
216 start = max(last, start);
217 last = end + 1;
218 if (last - start < num) {
219 if (last == cache->key.objectid + cache->key.offset)
220 cache_miss = start;
221 continue;
223 if (data != BTRFS_BLOCK_GROUP_MIXED &&
224 start + num > cache->key.objectid + cache->key.offset)
225 goto new_group;
226 return start;
228 out:
229 cache = btrfs_lookup_block_group(root->fs_info, search_start);
230 if (!cache) {
231 printk("Unable to find block group for %Lu\n",
232 search_start);
233 WARN_ON(1);
234 return search_start;
236 return search_start;
238 new_group:
239 last = cache->key.objectid + cache->key.offset;
240 wrapped:
241 cache = btrfs_lookup_block_group(root->fs_info, last);
242 if (!cache) {
243 no_cache:
244 if (!wrapped) {
245 wrapped = 1;
246 last = search_start;
247 data = BTRFS_BLOCK_GROUP_MIXED;
248 goto wrapped;
250 goto out;
252 if (cache_miss && !cache->cached) {
253 cache_block_group(root, cache);
254 last = cache_miss;
255 cache = btrfs_lookup_block_group(root->fs_info, last);
257 cache = btrfs_find_block_group(root, cache, last, data, 0);
258 if (!cache)
259 goto no_cache;
260 *cache_ret = cache;
261 cache_miss = 0;
262 goto again;
265 static u64 div_factor(u64 num, int factor)
267 if (factor == 10)
268 return num;
269 num *= factor;
270 num /= 10;
271 return num;
274 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
275 struct btrfs_block_group_cache
276 *hint, u64 search_start,
277 int data, int owner)
279 struct btrfs_block_group_cache *cache;
280 struct extent_io_tree *block_group_cache;
281 struct btrfs_block_group_cache *found_group = NULL;
282 struct btrfs_fs_info *info = root->fs_info;
283 u64 used;
284 u64 last = 0;
285 u64 hint_last;
286 u64 start;
287 u64 end;
288 u64 free_check;
289 u64 ptr;
290 int bit;
291 int ret;
292 int full_search = 0;
293 int factor = 8;
294 int data_swap = 0;
296 block_group_cache = &info->block_group_cache;
298 if (!owner)
299 factor = 8;
301 if (data == BTRFS_BLOCK_GROUP_MIXED) {
302 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
303 factor = 10;
304 } else if (data)
305 bit = BLOCK_GROUP_DATA;
306 else
307 bit = BLOCK_GROUP_METADATA;
309 if (search_start) {
310 struct btrfs_block_group_cache *shint;
311 shint = btrfs_lookup_block_group(info, search_start);
312 if (shint && (shint->data == data ||
313 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
314 used = btrfs_block_group_used(&shint->item);
315 if (used + shint->pinned <
316 div_factor(shint->key.offset, factor)) {
317 return shint;
321 if (hint && (hint->data == data ||
322 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
323 used = btrfs_block_group_used(&hint->item);
324 if (used + hint->pinned <
325 div_factor(hint->key.offset, factor)) {
326 return hint;
328 last = hint->key.objectid + hint->key.offset;
329 hint_last = last;
330 } else {
331 if (hint)
332 hint_last = max(hint->key.objectid, search_start);
333 else
334 hint_last = search_start;
336 last = hint_last;
338 again:
339 while(1) {
340 ret = find_first_extent_bit(block_group_cache, last,
341 &start, &end, bit);
342 if (ret)
343 break;
345 ret = get_state_private(block_group_cache, start, &ptr);
346 if (ret)
347 break;
349 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
350 last = cache->key.objectid + cache->key.offset;
351 used = btrfs_block_group_used(&cache->item);
353 if (full_search)
354 free_check = cache->key.offset;
355 else
356 free_check = div_factor(cache->key.offset, factor);
357 if (used + cache->pinned < free_check) {
358 found_group = cache;
359 goto found;
361 cond_resched();
363 if (!full_search) {
364 last = search_start;
365 full_search = 1;
366 goto again;
368 if (!data_swap) {
369 data_swap = 1;
370 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
371 last = search_start;
372 goto again;
374 found:
375 return found_group;
378 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
379 u64 owner, u64 owner_offset)
381 u32 high_crc = ~(u32)0;
382 u32 low_crc = ~(u32)0;
383 __le64 lenum;
385 lenum = cpu_to_le64(root_objectid);
386 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
387 lenum = cpu_to_le64(ref_generation);
388 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
389 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
390 lenum = cpu_to_le64(owner);
391 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
392 lenum = cpu_to_le64(owner_offset);
393 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
395 return ((u64)high_crc << 32) | (u64)low_crc;
398 static int match_extent_ref(struct extent_buffer *leaf,
399 struct btrfs_extent_ref *disk_ref,
400 struct btrfs_extent_ref *cpu_ref)
402 int ret;
403 int len;
405 if (cpu_ref->objectid)
406 len = sizeof(*cpu_ref);
407 else
408 len = 2 * sizeof(u64);
409 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
410 len);
411 return ret == 0;
414 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
415 struct btrfs_root *root,
416 struct btrfs_path *path, u64 bytenr,
417 u64 root_objectid,
418 u64 ref_generation, u64 owner,
419 u64 owner_offset, int del)
421 u64 hash;
422 struct btrfs_key key;
423 struct btrfs_key found_key;
424 struct btrfs_extent_ref ref;
425 struct extent_buffer *leaf;
426 struct btrfs_extent_ref *disk_ref;
427 int ret;
428 int ret2;
430 btrfs_set_stack_ref_root(&ref, root_objectid);
431 btrfs_set_stack_ref_generation(&ref, ref_generation);
432 btrfs_set_stack_ref_objectid(&ref, owner);
433 btrfs_set_stack_ref_offset(&ref, owner_offset);
435 hash = hash_extent_ref(root_objectid, ref_generation, owner,
436 owner_offset);
437 key.offset = hash;
438 key.objectid = bytenr;
439 key.type = BTRFS_EXTENT_REF_KEY;
441 while (1) {
442 ret = btrfs_search_slot(trans, root, &key, path,
443 del ? -1 : 0, del);
444 if (ret < 0)
445 goto out;
446 leaf = path->nodes[0];
447 if (ret != 0) {
448 u32 nritems = btrfs_header_nritems(leaf);
449 if (path->slots[0] >= nritems) {
450 ret2 = btrfs_next_leaf(root, path);
451 if (ret2)
452 goto out;
453 leaf = path->nodes[0];
455 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
456 if (found_key.objectid != bytenr ||
457 found_key.type != BTRFS_EXTENT_REF_KEY)
458 goto out;
459 key.offset = found_key.offset;
460 if (del) {
461 btrfs_release_path(root, path);
462 continue;
465 disk_ref = btrfs_item_ptr(path->nodes[0],
466 path->slots[0],
467 struct btrfs_extent_ref);
468 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
469 ret = 0;
470 goto out;
472 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
473 key.offset = found_key.offset + 1;
474 btrfs_release_path(root, path);
476 out:
477 return ret;
481 * Back reference rules. Back refs have three main goals:
483 * 1) differentiate between all holders of references to an extent so that
484 * when a reference is dropped we can make sure it was a valid reference
485 * before freeing the extent.
487 * 2) Provide enough information to quickly find the holders of an extent
488 * if we notice a given block is corrupted or bad.
490 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
491 * maintenance. This is actually the same as #2, but with a slightly
492 * different use case.
494 * File extents can be referenced by:
496 * - multiple snapshots, subvolumes, or different generations in one subvol
497 * - different files inside a single subvolume (in theory, not implemented yet)
498 * - different offsets inside a file (bookend extents in file.c)
500 * The extent ref structure has fields for:
502 * - Objectid of the subvolume root
503 * - Generation number of the tree holding the reference
504 * - objectid of the file holding the reference
505 * - offset in the file corresponding to the key holding the reference
507 * When a file extent is allocated the fields are filled in:
508 * (root_key.objectid, trans->transid, inode objectid, offset in file)
510 * When a leaf is cow'd new references are added for every file extent found
511 * in the leaf. It looks the same as the create case, but trans->transid
512 * will be different when the block is cow'd.
514 * (root_key.objectid, trans->transid, inode objectid, offset in file)
516 * When a file extent is removed either during snapshot deletion or file
517 * truncation, the corresponding back reference is found
518 * by searching for:
520 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
521 * inode objectid, offset in file)
523 * Btree extents can be referenced by:
525 * - Different subvolumes
526 * - Different generations of the same subvolume
528 * Storing sufficient information for a full reverse mapping of a btree
529 * block would require storing the lowest key of the block in the backref,
530 * and it would require updating that lowest key either before write out or
531 * every time it changed. Instead, the objectid of the lowest key is stored
532 * along with the level of the tree block. This provides a hint
533 * about where in the btree the block can be found. Searches through the
534 * btree only need to look for a pointer to that block, so they stop one
535 * level higher than the level recorded in the backref.
537 * Some btrees do not do reference counting on their extents. These
538 * include the extent tree and the tree of tree roots. Backrefs for these
539 * trees always have a generation of zero.
541 * When a tree block is created, back references are inserted:
543 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
545 * When a tree block is cow'd in a reference counted root,
546 * new back references are added for all the blocks it points to.
547 * These are of the form (trans->transid will have increased since creation):
549 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
551 * Because the lowest_key_objectid and the level are just hints
552 * they are not used when backrefs are deleted. When a backref is deleted:
554 * if backref was for a tree root:
555 * root_objectid = root->root_key.objectid
556 * else
557 * root_objectid = btrfs_header_owner(parent)
559 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
561 * Back Reference Key hashing:
563 * Back references have four fields, each 64 bits long. Unfortunately,
564 * This is hashed into a single 64 bit number and placed into the key offset.
565 * The key objectid corresponds to the first byte in the extent, and the
566 * key type is set to BTRFS_EXTENT_REF_KEY
568 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
569 struct btrfs_root *root,
570 struct btrfs_path *path, u64 bytenr,
571 u64 root_objectid, u64 ref_generation,
572 u64 owner, u64 owner_offset)
574 u64 hash;
575 struct btrfs_key key;
576 struct btrfs_extent_ref ref;
577 struct btrfs_extent_ref *disk_ref;
578 int ret;
580 btrfs_set_stack_ref_root(&ref, root_objectid);
581 btrfs_set_stack_ref_generation(&ref, ref_generation);
582 btrfs_set_stack_ref_objectid(&ref, owner);
583 btrfs_set_stack_ref_offset(&ref, owner_offset);
585 hash = hash_extent_ref(root_objectid, ref_generation, owner,
586 owner_offset);
587 key.offset = hash;
588 key.objectid = bytenr;
589 key.type = BTRFS_EXTENT_REF_KEY;
591 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
592 while (ret == -EEXIST) {
593 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
594 struct btrfs_extent_ref);
595 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
596 goto out;
597 key.offset++;
598 btrfs_release_path(root, path);
599 ret = btrfs_insert_empty_item(trans, root, path, &key,
600 sizeof(ref));
602 if (ret)
603 goto out;
604 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
605 struct btrfs_extent_ref);
606 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
607 sizeof(ref));
608 btrfs_mark_buffer_dirty(path->nodes[0]);
609 out:
610 btrfs_release_path(root, path);
611 return ret;
614 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
615 struct btrfs_root *root,
616 u64 bytenr, u64 num_bytes,
617 u64 root_objectid, u64 ref_generation,
618 u64 owner, u64 owner_offset)
620 struct btrfs_path *path;
621 int ret;
622 struct btrfs_key key;
623 struct extent_buffer *l;
624 struct btrfs_extent_item *item;
625 u32 refs;
627 WARN_ON(num_bytes < root->sectorsize);
628 path = btrfs_alloc_path();
629 if (!path)
630 return -ENOMEM;
632 key.objectid = bytenr;
633 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
634 key.offset = num_bytes;
635 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
636 0, 1);
637 if (ret < 0)
638 return ret;
639 if (ret != 0) {
640 BUG();
642 BUG_ON(ret != 0);
643 l = path->nodes[0];
644 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
645 refs = btrfs_extent_refs(l, item);
646 btrfs_set_extent_refs(l, item, refs + 1);
647 btrfs_mark_buffer_dirty(path->nodes[0]);
649 btrfs_release_path(root->fs_info->extent_root, path);
651 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
652 path, bytenr, root_objectid,
653 ref_generation, owner, owner_offset);
654 BUG_ON(ret);
655 finish_current_insert(trans, root->fs_info->extent_root);
656 del_pending_extents(trans, root->fs_info->extent_root);
658 btrfs_free_path(path);
659 return 0;
662 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
663 struct btrfs_root *root)
665 finish_current_insert(trans, root->fs_info->extent_root);
666 del_pending_extents(trans, root->fs_info->extent_root);
667 return 0;
670 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
671 struct btrfs_root *root, u64 bytenr,
672 u64 num_bytes, u32 *refs)
674 struct btrfs_path *path;
675 int ret;
676 struct btrfs_key key;
677 struct extent_buffer *l;
678 struct btrfs_extent_item *item;
680 WARN_ON(num_bytes < root->sectorsize);
681 path = btrfs_alloc_path();
682 key.objectid = bytenr;
683 key.offset = num_bytes;
684 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
685 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
686 0, 0);
687 if (ret < 0)
688 goto out;
689 if (ret != 0) {
690 btrfs_print_leaf(root, path->nodes[0]);
691 printk("failed to find block number %Lu\n", bytenr);
692 BUG();
694 l = path->nodes[0];
695 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
696 *refs = btrfs_extent_refs(l, item);
697 out:
698 btrfs_free_path(path);
699 return 0;
702 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
703 struct btrfs_path *count_path,
704 u64 first_extent)
706 struct btrfs_root *extent_root = root->fs_info->extent_root;
707 struct btrfs_path *path;
708 u64 bytenr;
709 u64 found_objectid;
710 u64 root_objectid = root->root_key.objectid;
711 u32 total_count = 0;
712 u32 cur_count;
713 u32 refs;
714 u32 nritems;
715 int ret;
716 struct btrfs_key key;
717 struct btrfs_key found_key;
718 struct extent_buffer *l;
719 struct btrfs_extent_item *item;
720 struct btrfs_extent_ref *ref_item;
721 int level = -1;
723 path = btrfs_alloc_path();
724 again:
725 if (level == -1)
726 bytenr = first_extent;
727 else
728 bytenr = count_path->nodes[level]->start;
730 cur_count = 0;
731 key.objectid = bytenr;
732 key.offset = 0;
734 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
735 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
736 if (ret < 0)
737 goto out;
738 BUG_ON(ret == 0);
740 l = path->nodes[0];
741 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
743 if (found_key.objectid != bytenr ||
744 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
745 goto out;
748 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
749 refs = btrfs_extent_refs(l, item);
750 while (1) {
751 nritems = btrfs_header_nritems(l);
752 if (path->slots[0] >= nritems) {
753 ret = btrfs_next_leaf(extent_root, path);
754 if (ret == 0)
755 continue;
756 break;
758 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
759 if (found_key.objectid != bytenr)
760 break;
761 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
762 path->slots[0]++;
763 continue;
766 cur_count++;
767 ref_item = btrfs_item_ptr(l, path->slots[0],
768 struct btrfs_extent_ref);
769 found_objectid = btrfs_ref_root(l, ref_item);
771 if (found_objectid != root_objectid) {
772 total_count = 2;
773 goto out;
775 total_count = 1;
776 path->slots[0]++;
778 if (cur_count == 0) {
779 total_count = 0;
780 goto out;
782 if (level >= 0 && root->node == count_path->nodes[level])
783 goto out;
784 level++;
785 btrfs_release_path(root, path);
786 goto again;
788 out:
789 btrfs_free_path(path);
790 return total_count;
792 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
793 struct btrfs_root *root, u64 owner_objectid)
795 u64 generation;
796 u64 key_objectid;
797 u64 level;
798 u32 nritems;
799 struct btrfs_disk_key disk_key;
801 level = btrfs_header_level(root->node);
802 generation = trans->transid;
803 nritems = btrfs_header_nritems(root->node);
804 if (nritems > 0) {
805 if (level == 0)
806 btrfs_item_key(root->node, &disk_key, 0);
807 else
808 btrfs_node_key(root->node, &disk_key, 0);
809 key_objectid = btrfs_disk_key_objectid(&disk_key);
810 } else {
811 key_objectid = 0;
813 return btrfs_inc_extent_ref(trans, root, root->node->start,
814 root->node->len, owner_objectid,
815 generation, level, key_objectid);
818 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
819 struct extent_buffer *buf)
821 u64 bytenr;
822 u32 nritems;
823 struct btrfs_key key;
824 struct btrfs_file_extent_item *fi;
825 int i;
826 int level;
827 int ret;
828 int faili;
830 if (!root->ref_cows)
831 return 0;
833 level = btrfs_header_level(buf);
834 nritems = btrfs_header_nritems(buf);
835 for (i = 0; i < nritems; i++) {
836 if (level == 0) {
837 u64 disk_bytenr;
838 btrfs_item_key_to_cpu(buf, &key, i);
839 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
840 continue;
841 fi = btrfs_item_ptr(buf, i,
842 struct btrfs_file_extent_item);
843 if (btrfs_file_extent_type(buf, fi) ==
844 BTRFS_FILE_EXTENT_INLINE)
845 continue;
846 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
847 if (disk_bytenr == 0)
848 continue;
849 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
850 btrfs_file_extent_disk_num_bytes(buf, fi),
851 root->root_key.objectid, trans->transid,
852 key.objectid, key.offset);
853 if (ret) {
854 faili = i;
855 goto fail;
857 } else {
858 bytenr = btrfs_node_blockptr(buf, i);
859 btrfs_node_key_to_cpu(buf, &key, i);
860 ret = btrfs_inc_extent_ref(trans, root, bytenr,
861 btrfs_level_size(root, level - 1),
862 root->root_key.objectid,
863 trans->transid,
864 level - 1, key.objectid);
865 if (ret) {
866 faili = i;
867 goto fail;
871 return 0;
872 fail:
873 WARN_ON(1);
874 #if 0
875 for (i =0; i < faili; i++) {
876 if (level == 0) {
877 u64 disk_bytenr;
878 btrfs_item_key_to_cpu(buf, &key, i);
879 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
880 continue;
881 fi = btrfs_item_ptr(buf, i,
882 struct btrfs_file_extent_item);
883 if (btrfs_file_extent_type(buf, fi) ==
884 BTRFS_FILE_EXTENT_INLINE)
885 continue;
886 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
887 if (disk_bytenr == 0)
888 continue;
889 err = btrfs_free_extent(trans, root, disk_bytenr,
890 btrfs_file_extent_disk_num_bytes(buf,
891 fi), 0);
892 BUG_ON(err);
893 } else {
894 bytenr = btrfs_node_blockptr(buf, i);
895 err = btrfs_free_extent(trans, root, bytenr,
896 btrfs_level_size(root, level - 1), 0);
897 BUG_ON(err);
900 #endif
901 return ret;
904 static int write_one_cache_group(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
906 struct btrfs_path *path,
907 struct btrfs_block_group_cache *cache)
909 int ret;
910 int pending_ret;
911 struct btrfs_root *extent_root = root->fs_info->extent_root;
912 unsigned long bi;
913 struct extent_buffer *leaf;
915 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
916 if (ret < 0)
917 goto fail;
918 BUG_ON(ret);
920 leaf = path->nodes[0];
921 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
922 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
923 btrfs_mark_buffer_dirty(leaf);
924 btrfs_release_path(extent_root, path);
925 fail:
926 finish_current_insert(trans, extent_root);
927 pending_ret = del_pending_extents(trans, extent_root);
928 if (ret)
929 return ret;
930 if (pending_ret)
931 return pending_ret;
932 return 0;
936 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root)
939 struct extent_io_tree *block_group_cache;
940 struct btrfs_block_group_cache *cache;
941 int ret;
942 int err = 0;
943 int werr = 0;
944 struct btrfs_path *path;
945 u64 last = 0;
946 u64 start;
947 u64 end;
948 u64 ptr;
950 block_group_cache = &root->fs_info->block_group_cache;
951 path = btrfs_alloc_path();
952 if (!path)
953 return -ENOMEM;
955 while(1) {
956 ret = find_first_extent_bit(block_group_cache, last,
957 &start, &end, BLOCK_GROUP_DIRTY);
958 if (ret)
959 break;
961 last = end + 1;
962 ret = get_state_private(block_group_cache, start, &ptr);
963 if (ret)
964 break;
966 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
967 err = write_one_cache_group(trans, root,
968 path, cache);
970 * if we fail to write the cache group, we want
971 * to keep it marked dirty in hopes that a later
972 * write will work
974 if (err) {
975 werr = err;
976 continue;
978 clear_extent_bits(block_group_cache, start, end,
979 BLOCK_GROUP_DIRTY, GFP_NOFS);
981 btrfs_free_path(path);
982 return werr;
985 static int update_block_group(struct btrfs_trans_handle *trans,
986 struct btrfs_root *root,
987 u64 bytenr, u64 num_bytes, int alloc,
988 int mark_free, int data)
990 struct btrfs_block_group_cache *cache;
991 struct btrfs_fs_info *info = root->fs_info;
992 u64 total = num_bytes;
993 u64 old_val;
994 u64 byte_in_group;
995 u64 start;
996 u64 end;
998 while(total) {
999 cache = btrfs_lookup_block_group(info, bytenr);
1000 if (!cache) {
1001 return -1;
1003 byte_in_group = bytenr - cache->key.objectid;
1004 WARN_ON(byte_in_group > cache->key.offset);
1005 start = cache->key.objectid;
1006 end = start + cache->key.offset - 1;
1007 set_extent_bits(&info->block_group_cache, start, end,
1008 BLOCK_GROUP_DIRTY, GFP_NOFS);
1010 old_val = btrfs_block_group_used(&cache->item);
1011 num_bytes = min(total, cache->key.offset - byte_in_group);
1012 if (alloc) {
1013 if (cache->data != data &&
1014 old_val < (cache->key.offset >> 1)) {
1015 int bit_to_clear;
1016 int bit_to_set;
1017 cache->data = data;
1018 if (data) {
1019 bit_to_clear = BLOCK_GROUP_METADATA;
1020 bit_to_set = BLOCK_GROUP_DATA;
1021 cache->item.flags &=
1022 ~BTRFS_BLOCK_GROUP_MIXED;
1023 cache->item.flags |=
1024 BTRFS_BLOCK_GROUP_DATA;
1025 } else {
1026 bit_to_clear = BLOCK_GROUP_DATA;
1027 bit_to_set = BLOCK_GROUP_METADATA;
1028 cache->item.flags &=
1029 ~BTRFS_BLOCK_GROUP_MIXED;
1030 cache->item.flags &=
1031 ~BTRFS_BLOCK_GROUP_DATA;
1033 clear_extent_bits(&info->block_group_cache,
1034 start, end, bit_to_clear,
1035 GFP_NOFS);
1036 set_extent_bits(&info->block_group_cache,
1037 start, end, bit_to_set,
1038 GFP_NOFS);
1039 } else if (cache->data != data &&
1040 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1041 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1042 set_extent_bits(&info->block_group_cache,
1043 start, end,
1044 BLOCK_GROUP_DATA |
1045 BLOCK_GROUP_METADATA,
1046 GFP_NOFS);
1048 old_val += num_bytes;
1049 } else {
1050 old_val -= num_bytes;
1051 if (mark_free) {
1052 set_extent_dirty(&info->free_space_cache,
1053 bytenr, bytenr + num_bytes - 1,
1054 GFP_NOFS);
1057 btrfs_set_block_group_used(&cache->item, old_val);
1058 total -= num_bytes;
1059 bytenr += num_bytes;
1061 return 0;
1063 static int update_pinned_extents(struct btrfs_root *root,
1064 u64 bytenr, u64 num, int pin)
1066 u64 len;
1067 struct btrfs_block_group_cache *cache;
1068 struct btrfs_fs_info *fs_info = root->fs_info;
1070 if (pin) {
1071 set_extent_dirty(&fs_info->pinned_extents,
1072 bytenr, bytenr + num - 1, GFP_NOFS);
1073 } else {
1074 clear_extent_dirty(&fs_info->pinned_extents,
1075 bytenr, bytenr + num - 1, GFP_NOFS);
1077 while (num > 0) {
1078 cache = btrfs_lookup_block_group(fs_info, bytenr);
1079 WARN_ON(!cache);
1080 len = min(num, cache->key.offset -
1081 (bytenr - cache->key.objectid));
1082 if (pin) {
1083 cache->pinned += len;
1084 fs_info->total_pinned += len;
1085 } else {
1086 cache->pinned -= len;
1087 fs_info->total_pinned -= len;
1089 bytenr += len;
1090 num -= len;
1092 return 0;
1095 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1097 u64 last = 0;
1098 u64 start;
1099 u64 end;
1100 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1101 int ret;
1103 while(1) {
1104 ret = find_first_extent_bit(pinned_extents, last,
1105 &start, &end, EXTENT_DIRTY);
1106 if (ret)
1107 break;
1108 set_extent_dirty(copy, start, end, GFP_NOFS);
1109 last = end + 1;
1111 return 0;
1114 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1115 struct btrfs_root *root,
1116 struct extent_io_tree *unpin)
1118 u64 start;
1119 u64 end;
1120 int ret;
1121 struct extent_io_tree *free_space_cache;
1122 free_space_cache = &root->fs_info->free_space_cache;
1124 while(1) {
1125 ret = find_first_extent_bit(unpin, 0, &start, &end,
1126 EXTENT_DIRTY);
1127 if (ret)
1128 break;
1129 update_pinned_extents(root, start, end + 1 - start, 0);
1130 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1131 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1133 return 0;
1136 static int finish_current_insert(struct btrfs_trans_handle *trans,
1137 struct btrfs_root *extent_root)
1139 u64 start;
1140 u64 end;
1141 struct btrfs_fs_info *info = extent_root->fs_info;
1142 struct extent_buffer *eb;
1143 struct btrfs_path *path;
1144 struct btrfs_key ins;
1145 struct btrfs_disk_key first;
1146 struct btrfs_extent_item extent_item;
1147 int ret;
1148 int level;
1149 int err = 0;
1151 btrfs_set_stack_extent_refs(&extent_item, 1);
1152 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1153 path = btrfs_alloc_path();
1155 while(1) {
1156 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1157 &end, EXTENT_LOCKED);
1158 if (ret)
1159 break;
1161 ins.objectid = start;
1162 ins.offset = end + 1 - start;
1163 err = btrfs_insert_item(trans, extent_root, &ins,
1164 &extent_item, sizeof(extent_item));
1165 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1166 GFP_NOFS);
1167 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1168 level = btrfs_header_level(eb);
1169 if (level == 0) {
1170 btrfs_item_key(eb, &first, 0);
1171 } else {
1172 btrfs_node_key(eb, &first, 0);
1174 err = btrfs_insert_extent_backref(trans, extent_root, path,
1175 start, extent_root->root_key.objectid,
1176 0, level,
1177 btrfs_disk_key_objectid(&first));
1178 BUG_ON(err);
1179 free_extent_buffer(eb);
1181 btrfs_free_path(path);
1182 return 0;
1185 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1186 int pending)
1188 int err = 0;
1189 struct extent_buffer *buf;
1191 if (!pending) {
1192 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1193 if (buf) {
1194 if (btrfs_buffer_uptodate(buf)) {
1195 u64 transid =
1196 root->fs_info->running_transaction->transid;
1197 if (btrfs_header_generation(buf) == transid) {
1198 free_extent_buffer(buf);
1199 return 1;
1202 free_extent_buffer(buf);
1204 update_pinned_extents(root, bytenr, num_bytes, 1);
1205 } else {
1206 set_extent_bits(&root->fs_info->pending_del,
1207 bytenr, bytenr + num_bytes - 1,
1208 EXTENT_LOCKED, GFP_NOFS);
1210 BUG_ON(err < 0);
1211 return 0;
1215 * remove an extent from the root, returns 0 on success
1217 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1218 *root, u64 bytenr, u64 num_bytes,
1219 u64 root_objectid, u64 ref_generation,
1220 u64 owner_objectid, u64 owner_offset, int pin,
1221 int mark_free)
1223 struct btrfs_path *path;
1224 struct btrfs_key key;
1225 struct btrfs_fs_info *info = root->fs_info;
1226 struct btrfs_extent_ops *ops = info->extent_ops;
1227 struct btrfs_root *extent_root = info->extent_root;
1228 struct extent_buffer *leaf;
1229 int ret;
1230 struct btrfs_extent_item *ei;
1231 u32 refs;
1233 key.objectid = bytenr;
1234 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1235 key.offset = num_bytes;
1237 path = btrfs_alloc_path();
1238 if (!path)
1239 return -ENOMEM;
1241 ret = lookup_extent_backref(trans, extent_root, path,
1242 bytenr, root_objectid,
1243 ref_generation,
1244 owner_objectid, owner_offset, 1);
1245 if (ret == 0) {
1246 ret = btrfs_del_item(trans, extent_root, path);
1247 } else {
1248 btrfs_print_leaf(extent_root, path->nodes[0]);
1249 WARN_ON(1);
1250 printk("Unable to find ref byte nr %Lu root %Lu "
1251 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1252 root_objectid, ref_generation, owner_objectid,
1253 owner_offset);
1255 btrfs_release_path(extent_root, path);
1256 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1257 if (ret < 0)
1258 return ret;
1259 BUG_ON(ret);
1261 leaf = path->nodes[0];
1262 ei = btrfs_item_ptr(leaf, path->slots[0],
1263 struct btrfs_extent_item);
1264 refs = btrfs_extent_refs(leaf, ei);
1265 BUG_ON(refs == 0);
1266 refs -= 1;
1267 btrfs_set_extent_refs(leaf, ei, refs);
1268 btrfs_mark_buffer_dirty(leaf);
1270 if (refs == 0) {
1271 u64 super_used;
1272 u64 root_used;
1274 if (pin) {
1275 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1276 if (ret > 0)
1277 mark_free = 1;
1278 BUG_ON(ret < 0);
1281 /* block accounting for super block */
1282 super_used = btrfs_super_bytes_used(&info->super_copy);
1283 btrfs_set_super_bytes_used(&info->super_copy,
1284 super_used - num_bytes);
1286 /* block accounting for root item */
1287 root_used = btrfs_root_used(&root->root_item);
1288 btrfs_set_root_used(&root->root_item,
1289 root_used - num_bytes);
1290 ret = btrfs_del_item(trans, extent_root, path);
1291 if (ret)
1292 return ret;
1294 if (ops && ops->free_extent)
1295 ops->free_extent(root, bytenr, num_bytes);
1297 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1298 mark_free, 0);
1299 BUG_ON(ret);
1301 btrfs_free_path(path);
1302 finish_current_insert(trans, extent_root);
1303 return ret;
1307 * find all the blocks marked as pending in the radix tree and remove
1308 * them from the extent map
1310 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1311 btrfs_root *extent_root)
1313 int ret;
1314 int err = 0;
1315 u64 start;
1316 u64 end;
1317 struct extent_io_tree *pending_del;
1318 struct extent_io_tree *pinned_extents;
1320 pending_del = &extent_root->fs_info->pending_del;
1321 pinned_extents = &extent_root->fs_info->pinned_extents;
1323 while(1) {
1324 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1325 EXTENT_LOCKED);
1326 if (ret)
1327 break;
1328 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1329 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1330 GFP_NOFS);
1331 ret = __free_extent(trans, extent_root,
1332 start, end + 1 - start,
1333 extent_root->root_key.objectid,
1334 0, 0, 0, 0, 0);
1335 if (ret)
1336 err = ret;
1338 return err;
1342 * remove an extent from the root, returns 0 on success
1344 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1345 *root, u64 bytenr, u64 num_bytes,
1346 u64 root_objectid, u64 ref_generation,
1347 u64 owner_objectid, u64 owner_offset, int pin)
1349 struct btrfs_root *extent_root = root->fs_info->extent_root;
1350 int pending_ret;
1351 int ret;
1353 WARN_ON(num_bytes < root->sectorsize);
1354 if (!root->ref_cows)
1355 ref_generation = 0;
1357 if (root == extent_root) {
1358 pin_down_bytes(root, bytenr, num_bytes, 1);
1359 return 0;
1361 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1362 ref_generation, owner_objectid, owner_offset,
1363 pin, pin == 0);
1364 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1365 return ret ? ret : pending_ret;
1368 static u64 stripe_align(struct btrfs_root *root, u64 val)
1370 u64 mask = ((u64)root->stripesize - 1);
1371 u64 ret = (val + mask) & ~mask;
1372 return ret;
1376 * walks the btree of allocated extents and find a hole of a given size.
1377 * The key ins is changed to record the hole:
1378 * ins->objectid == block start
1379 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1380 * ins->offset == number of blocks
1381 * Any available blocks before search_start are skipped.
1383 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1384 struct btrfs_root *orig_root,
1385 u64 num_bytes, u64 empty_size,
1386 u64 search_start, u64 search_end,
1387 u64 hint_byte, struct btrfs_key *ins,
1388 u64 exclude_start, u64 exclude_nr,
1389 int data)
1391 struct btrfs_path *path;
1392 struct btrfs_key key;
1393 u64 hole_size = 0;
1394 u64 aligned;
1395 int ret;
1396 int slot = 0;
1397 u64 last_byte = 0;
1398 u64 orig_search_start = search_start;
1399 int start_found;
1400 struct extent_buffer *l;
1401 struct btrfs_root * root = orig_root->fs_info->extent_root;
1402 struct btrfs_fs_info *info = root->fs_info;
1403 u64 total_needed = num_bytes;
1404 int level;
1405 struct btrfs_block_group_cache *block_group;
1406 int full_scan = 0;
1407 int wrapped = 0;
1408 u64 cached_start;
1410 WARN_ON(num_bytes < root->sectorsize);
1411 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1413 level = btrfs_header_level(root->node);
1415 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
1416 data = BTRFS_BLOCK_GROUP_MIXED;
1419 if (search_end == (u64)-1)
1420 search_end = btrfs_super_total_bytes(&info->super_copy);
1421 if (hint_byte) {
1422 block_group = btrfs_lookup_block_group(info, hint_byte);
1423 if (!block_group)
1424 hint_byte = search_start;
1425 block_group = btrfs_find_block_group(root, block_group,
1426 hint_byte, data, 1);
1427 } else {
1428 block_group = btrfs_find_block_group(root,
1429 trans->block_group,
1430 search_start, data, 1);
1433 total_needed += empty_size;
1434 path = btrfs_alloc_path();
1435 check_failed:
1436 if (!block_group) {
1437 block_group = btrfs_lookup_block_group(info, search_start);
1438 if (!block_group)
1439 block_group = btrfs_lookup_block_group(info,
1440 orig_search_start);
1442 search_start = find_search_start(root, &block_group, search_start,
1443 total_needed, data);
1444 search_start = stripe_align(root, search_start);
1445 cached_start = search_start;
1446 btrfs_init_path(path);
1447 ins->objectid = search_start;
1448 ins->offset = 0;
1449 start_found = 0;
1450 path->reada = 2;
1452 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1453 if (ret < 0)
1454 goto error;
1455 ret = find_previous_extent(root, path);
1456 if (ret < 0)
1457 goto error;
1458 l = path->nodes[0];
1459 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1460 while (1) {
1461 l = path->nodes[0];
1462 slot = path->slots[0];
1463 if (slot >= btrfs_header_nritems(l)) {
1464 ret = btrfs_next_leaf(root, path);
1465 if (ret == 0)
1466 continue;
1467 if (ret < 0)
1468 goto error;
1470 search_start = max(search_start,
1471 block_group->key.objectid);
1472 if (!start_found) {
1473 aligned = stripe_align(root, search_start);
1474 ins->objectid = aligned;
1475 if (aligned >= search_end) {
1476 ret = -ENOSPC;
1477 goto error;
1479 ins->offset = search_end - aligned;
1480 start_found = 1;
1481 goto check_pending;
1483 ins->objectid = stripe_align(root,
1484 last_byte > search_start ?
1485 last_byte : search_start);
1486 if (search_end <= ins->objectid) {
1487 ret = -ENOSPC;
1488 goto error;
1490 ins->offset = search_end - ins->objectid;
1491 BUG_ON(ins->objectid >= search_end);
1492 goto check_pending;
1494 btrfs_item_key_to_cpu(l, &key, slot);
1496 if (key.objectid >= search_start && key.objectid > last_byte &&
1497 start_found) {
1498 if (last_byte < search_start)
1499 last_byte = search_start;
1500 aligned = stripe_align(root, last_byte);
1501 hole_size = key.objectid - aligned;
1502 if (key.objectid > aligned && hole_size >= num_bytes) {
1503 ins->objectid = aligned;
1504 ins->offset = hole_size;
1505 goto check_pending;
1508 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1509 if (!start_found && btrfs_key_type(&key) ==
1510 BTRFS_BLOCK_GROUP_ITEM_KEY) {
1511 last_byte = key.objectid;
1512 start_found = 1;
1514 goto next;
1518 start_found = 1;
1519 last_byte = key.objectid + key.offset;
1521 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1522 last_byte >= block_group->key.objectid +
1523 block_group->key.offset) {
1524 btrfs_release_path(root, path);
1525 search_start = block_group->key.objectid +
1526 block_group->key.offset;
1527 goto new_group;
1529 next:
1530 path->slots[0]++;
1531 cond_resched();
1533 check_pending:
1534 /* we have to make sure we didn't find an extent that has already
1535 * been allocated by the map tree or the original allocation
1537 btrfs_release_path(root, path);
1538 BUG_ON(ins->objectid < search_start);
1540 if (ins->objectid + num_bytes >= search_end)
1541 goto enospc;
1542 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1543 ins->objectid + num_bytes > block_group->
1544 key.objectid + block_group->key.offset) {
1545 search_start = block_group->key.objectid +
1546 block_group->key.offset;
1547 goto new_group;
1549 if (test_range_bit(&info->extent_ins, ins->objectid,
1550 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1551 search_start = ins->objectid + num_bytes;
1552 goto new_group;
1554 if (test_range_bit(&info->pinned_extents, ins->objectid,
1555 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1556 search_start = ins->objectid + num_bytes;
1557 goto new_group;
1559 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1560 ins->objectid < exclude_start + exclude_nr)) {
1561 search_start = exclude_start + exclude_nr;
1562 goto new_group;
1564 if (!data) {
1565 block_group = btrfs_lookup_block_group(info, ins->objectid);
1566 if (block_group)
1567 trans->block_group = block_group;
1569 ins->offset = num_bytes;
1570 btrfs_free_path(path);
1571 return 0;
1573 new_group:
1574 if (search_start + num_bytes >= search_end) {
1575 enospc:
1576 search_start = orig_search_start;
1577 if (full_scan) {
1578 ret = -ENOSPC;
1579 goto error;
1581 if (wrapped) {
1582 if (!full_scan)
1583 total_needed -= empty_size;
1584 full_scan = 1;
1585 data = BTRFS_BLOCK_GROUP_MIXED;
1586 } else
1587 wrapped = 1;
1589 block_group = btrfs_lookup_block_group(info, search_start);
1590 cond_resched();
1591 block_group = btrfs_find_block_group(root, block_group,
1592 search_start, data, 0);
1593 goto check_failed;
1595 error:
1596 btrfs_release_path(root, path);
1597 btrfs_free_path(path);
1598 return ret;
1601 * finds a free extent and does all the dirty work required for allocation
1602 * returns the key for the extent through ins, and a tree buffer for
1603 * the first block of the extent through buf.
1605 * returns 0 if everything worked, non-zero otherwise.
1607 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1608 struct btrfs_root *root,
1609 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1610 u64 owner, u64 owner_offset,
1611 u64 empty_size, u64 hint_byte,
1612 u64 search_end, struct btrfs_key *ins, int data)
1614 int ret;
1615 int pending_ret;
1616 u64 super_used, root_used;
1617 u64 search_start = 0;
1619 u64 new_hint;
1621 struct btrfs_fs_info *info = root->fs_info;
1622 struct btrfs_extent_ops *ops = info->extent_ops;
1623 struct btrfs_root *extent_root = info->extent_root;
1624 struct btrfs_extent_item extent_item;
1625 struct btrfs_path *path;
1627 btrfs_set_stack_extent_refs(&extent_item, 1);
1630 new_hint = max(hint_byte, root->fs_info->alloc_start);
1631 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1632 hint_byte = new_hint;
1634 WARN_ON(num_bytes < root->sectorsize);
1635 if (ops && ops->alloc_extent) {
1636 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1637 } else {
1638 ret = find_free_extent(trans, root, num_bytes, empty_size,
1639 search_start, search_end, hint_byte,
1640 ins, trans->alloc_exclude_start,
1641 trans->alloc_exclude_nr, data);
1643 BUG_ON(ret);
1644 if (ret)
1645 return ret;
1647 /* block accounting for super block */
1648 super_used = btrfs_super_bytes_used(&info->super_copy);
1649 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1651 /* block accounting for root item */
1652 root_used = btrfs_root_used(&root->root_item);
1653 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1655 clear_extent_dirty(&root->fs_info->free_space_cache,
1656 ins->objectid, ins->objectid + ins->offset - 1,
1657 GFP_NOFS);
1659 if (root == extent_root) {
1660 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1661 ins->objectid + ins->offset - 1,
1662 EXTENT_LOCKED, GFP_NOFS);
1663 WARN_ON(data == 1);
1664 goto update_block;
1667 WARN_ON(trans->alloc_exclude_nr);
1668 trans->alloc_exclude_start = ins->objectid;
1669 trans->alloc_exclude_nr = ins->offset;
1670 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1671 sizeof(extent_item));
1673 trans->alloc_exclude_start = 0;
1674 trans->alloc_exclude_nr = 0;
1675 BUG_ON(ret);
1677 path = btrfs_alloc_path();
1678 BUG_ON(!path);
1679 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1680 ins->objectid, root_objectid,
1681 ref_generation, owner, owner_offset);
1683 BUG_ON(ret);
1684 btrfs_free_path(path);
1685 finish_current_insert(trans, extent_root);
1686 pending_ret = del_pending_extents(trans, extent_root);
1688 if (ret) {
1689 return ret;
1691 if (pending_ret) {
1692 return pending_ret;
1695 update_block:
1696 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1697 data);
1698 BUG_ON(ret);
1699 return 0;
1703 * helper function to allocate a block for a given tree
1704 * returns the tree buffer or NULL.
1706 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1707 struct btrfs_root *root,
1708 u32 blocksize,
1709 u64 root_objectid, u64 hint,
1710 u64 empty_size)
1712 u64 ref_generation;
1714 if (root->ref_cows)
1715 ref_generation = trans->transid;
1716 else
1717 ref_generation = 0;
1720 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1721 ref_generation, 0, 0, hint, empty_size);
1725 * helper function to allocate a block for a given tree
1726 * returns the tree buffer or NULL.
1728 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root,
1730 u32 blocksize,
1731 u64 root_objectid,
1732 u64 ref_generation,
1733 u64 first_objectid,
1734 int level,
1735 u64 hint,
1736 u64 empty_size)
1738 struct btrfs_key ins;
1739 int ret;
1740 struct extent_buffer *buf;
1742 ret = btrfs_alloc_extent(trans, root, blocksize,
1743 root_objectid, ref_generation,
1744 level, first_objectid, empty_size, hint,
1745 (u64)-1, &ins, 0);
1746 if (ret) {
1747 BUG_ON(ret > 0);
1748 return ERR_PTR(ret);
1750 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1751 if (!buf) {
1752 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1753 root->root_key.objectid, ref_generation,
1754 0, 0, 0);
1755 return ERR_PTR(-ENOMEM);
1757 btrfs_set_buffer_uptodate(buf);
1758 trans->blocks_used++;
1759 return buf;
1762 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1763 struct btrfs_root *root,
1764 struct extent_buffer *leaf)
1766 u64 leaf_owner;
1767 u64 leaf_generation;
1768 struct btrfs_key key;
1769 struct btrfs_file_extent_item *fi;
1770 int i;
1771 int nritems;
1772 int ret;
1774 BUG_ON(!btrfs_is_leaf(leaf));
1775 nritems = btrfs_header_nritems(leaf);
1776 leaf_owner = btrfs_header_owner(leaf);
1777 leaf_generation = btrfs_header_generation(leaf);
1779 for (i = 0; i < nritems; i++) {
1780 u64 disk_bytenr;
1782 btrfs_item_key_to_cpu(leaf, &key, i);
1783 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1784 continue;
1785 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1786 if (btrfs_file_extent_type(leaf, fi) ==
1787 BTRFS_FILE_EXTENT_INLINE)
1788 continue;
1790 * FIXME make sure to insert a trans record that
1791 * repeats the snapshot del on crash
1793 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1794 if (disk_bytenr == 0)
1795 continue;
1796 ret = btrfs_free_extent(trans, root, disk_bytenr,
1797 btrfs_file_extent_disk_num_bytes(leaf, fi),
1798 leaf_owner, leaf_generation,
1799 key.objectid, key.offset, 0);
1800 BUG_ON(ret);
1802 return 0;
1805 static void noinline reada_walk_down(struct btrfs_root *root,
1806 struct extent_buffer *node)
1808 int i;
1809 u32 nritems;
1810 u64 bytenr;
1811 int ret;
1812 u32 refs;
1813 int level;
1814 u32 blocksize;
1816 nritems = btrfs_header_nritems(node);
1817 level = btrfs_header_level(node);
1818 for (i = 0; i < nritems; i++) {
1819 bytenr = btrfs_node_blockptr(node, i);
1820 blocksize = btrfs_level_size(root, level - 1);
1821 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1822 BUG_ON(ret);
1823 if (refs != 1)
1824 continue;
1825 mutex_unlock(&root->fs_info->fs_mutex);
1826 ret = readahead_tree_block(root, bytenr, blocksize);
1827 cond_resched();
1828 mutex_lock(&root->fs_info->fs_mutex);
1829 if (ret)
1830 break;
1835 * helper function for drop_snapshot, this walks down the tree dropping ref
1836 * counts as it goes.
1838 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1839 struct btrfs_root *root,
1840 struct btrfs_path *path, int *level)
1842 u64 root_owner;
1843 u64 root_gen;
1844 u64 bytenr;
1845 struct extent_buffer *next;
1846 struct extent_buffer *cur;
1847 struct extent_buffer *parent;
1848 u32 blocksize;
1849 int ret;
1850 u32 refs;
1852 WARN_ON(*level < 0);
1853 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1854 ret = lookup_extent_ref(trans, root,
1855 path->nodes[*level]->start,
1856 path->nodes[*level]->len, &refs);
1857 BUG_ON(ret);
1858 if (refs > 1)
1859 goto out;
1862 * walk down to the last node level and free all the leaves
1864 while(*level >= 0) {
1865 WARN_ON(*level < 0);
1866 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1867 cur = path->nodes[*level];
1869 if (*level > 0 && path->slots[*level] == 0)
1870 reada_walk_down(root, cur);
1872 if (btrfs_header_level(cur) != *level)
1873 WARN_ON(1);
1875 if (path->slots[*level] >=
1876 btrfs_header_nritems(cur))
1877 break;
1878 if (*level == 0) {
1879 ret = drop_leaf_ref(trans, root, cur);
1880 BUG_ON(ret);
1881 break;
1883 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1884 blocksize = btrfs_level_size(root, *level - 1);
1885 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1886 BUG_ON(ret);
1887 if (refs != 1) {
1888 parent = path->nodes[*level];
1889 root_owner = btrfs_header_owner(parent);
1890 root_gen = btrfs_header_generation(parent);
1891 path->slots[*level]++;
1892 ret = btrfs_free_extent(trans, root, bytenr,
1893 blocksize, root_owner,
1894 root_gen, 0, 0, 1);
1895 BUG_ON(ret);
1896 continue;
1898 next = btrfs_find_tree_block(root, bytenr, blocksize);
1899 if (!next || !btrfs_buffer_uptodate(next)) {
1900 free_extent_buffer(next);
1901 mutex_unlock(&root->fs_info->fs_mutex);
1902 next = read_tree_block(root, bytenr, blocksize);
1903 mutex_lock(&root->fs_info->fs_mutex);
1905 /* we dropped the lock, check one more time */
1906 ret = lookup_extent_ref(trans, root, bytenr,
1907 blocksize, &refs);
1908 BUG_ON(ret);
1909 if (refs != 1) {
1910 parent = path->nodes[*level];
1911 root_owner = btrfs_header_owner(parent);
1912 root_gen = btrfs_header_generation(parent);
1914 path->slots[*level]++;
1915 free_extent_buffer(next);
1916 ret = btrfs_free_extent(trans, root, bytenr,
1917 blocksize,
1918 root_owner,
1919 root_gen, 0, 0, 1);
1920 BUG_ON(ret);
1921 continue;
1924 WARN_ON(*level <= 0);
1925 if (path->nodes[*level-1])
1926 free_extent_buffer(path->nodes[*level-1]);
1927 path->nodes[*level-1] = next;
1928 *level = btrfs_header_level(next);
1929 path->slots[*level] = 0;
1931 out:
1932 WARN_ON(*level < 0);
1933 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1935 if (path->nodes[*level] == root->node) {
1936 root_owner = root->root_key.objectid;
1937 parent = path->nodes[*level];
1938 } else {
1939 parent = path->nodes[*level + 1];
1940 root_owner = btrfs_header_owner(parent);
1943 root_gen = btrfs_header_generation(parent);
1944 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1945 path->nodes[*level]->len,
1946 root_owner, root_gen, 0, 0, 1);
1947 free_extent_buffer(path->nodes[*level]);
1948 path->nodes[*level] = NULL;
1949 *level += 1;
1950 BUG_ON(ret);
1951 return 0;
1955 * helper for dropping snapshots. This walks back up the tree in the path
1956 * to find the first node higher up where we haven't yet gone through
1957 * all the slots
1959 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1960 struct btrfs_root *root,
1961 struct btrfs_path *path, int *level)
1963 u64 root_owner;
1964 u64 root_gen;
1965 struct btrfs_root_item *root_item = &root->root_item;
1966 int i;
1967 int slot;
1968 int ret;
1970 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1971 slot = path->slots[i];
1972 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1973 struct extent_buffer *node;
1974 struct btrfs_disk_key disk_key;
1975 node = path->nodes[i];
1976 path->slots[i]++;
1977 *level = i;
1978 WARN_ON(*level == 0);
1979 btrfs_node_key(node, &disk_key, path->slots[i]);
1980 memcpy(&root_item->drop_progress,
1981 &disk_key, sizeof(disk_key));
1982 root_item->drop_level = i;
1983 return 0;
1984 } else {
1985 if (path->nodes[*level] == root->node) {
1986 root_owner = root->root_key.objectid;
1987 root_gen =
1988 btrfs_header_generation(path->nodes[*level]);
1989 } else {
1990 struct extent_buffer *node;
1991 node = path->nodes[*level + 1];
1992 root_owner = btrfs_header_owner(node);
1993 root_gen = btrfs_header_generation(node);
1995 ret = btrfs_free_extent(trans, root,
1996 path->nodes[*level]->start,
1997 path->nodes[*level]->len,
1998 root_owner, root_gen, 0, 0, 1);
1999 BUG_ON(ret);
2000 free_extent_buffer(path->nodes[*level]);
2001 path->nodes[*level] = NULL;
2002 *level = i + 1;
2005 return 1;
2009 * drop the reference count on the tree rooted at 'snap'. This traverses
2010 * the tree freeing any blocks that have a ref count of zero after being
2011 * decremented.
2013 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2014 *root)
2016 int ret = 0;
2017 int wret;
2018 int level;
2019 struct btrfs_path *path;
2020 int i;
2021 int orig_level;
2022 struct btrfs_root_item *root_item = &root->root_item;
2024 path = btrfs_alloc_path();
2025 BUG_ON(!path);
2027 level = btrfs_header_level(root->node);
2028 orig_level = level;
2029 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2030 path->nodes[level] = root->node;
2031 extent_buffer_get(root->node);
2032 path->slots[level] = 0;
2033 } else {
2034 struct btrfs_key key;
2035 struct btrfs_disk_key found_key;
2036 struct extent_buffer *node;
2038 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2039 level = root_item->drop_level;
2040 path->lowest_level = level;
2041 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2042 if (wret < 0) {
2043 ret = wret;
2044 goto out;
2046 node = path->nodes[level];
2047 btrfs_node_key(node, &found_key, path->slots[level]);
2048 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2049 sizeof(found_key)));
2051 while(1) {
2052 wret = walk_down_tree(trans, root, path, &level);
2053 if (wret < 0)
2054 ret = wret;
2055 if (wret != 0)
2056 break;
2058 wret = walk_up_tree(trans, root, path, &level);
2059 if (wret < 0)
2060 ret = wret;
2061 if (wret != 0)
2062 break;
2064 ret = -EAGAIN;
2065 break;
2068 for (i = 0; i <= orig_level; i++) {
2069 if (path->nodes[i]) {
2070 free_extent_buffer(path->nodes[i]);
2071 path->nodes[i] = NULL;
2074 out:
2075 btrfs_free_path(path);
2076 return ret;
2079 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2081 u64 start;
2082 u64 end;
2083 u64 ptr;
2084 int ret;
2085 while(1) {
2086 ret = find_first_extent_bit(&info->block_group_cache, 0,
2087 &start, &end, (unsigned int)-1);
2088 if (ret)
2089 break;
2090 ret = get_state_private(&info->block_group_cache, start, &ptr);
2091 if (!ret)
2092 kfree((void *)(unsigned long)ptr);
2093 clear_extent_bits(&info->block_group_cache, start,
2094 end, (unsigned int)-1, GFP_NOFS);
2096 while(1) {
2097 ret = find_first_extent_bit(&info->free_space_cache, 0,
2098 &start, &end, EXTENT_DIRTY);
2099 if (ret)
2100 break;
2101 clear_extent_dirty(&info->free_space_cache, start,
2102 end, GFP_NOFS);
2104 return 0;
2107 int btrfs_read_block_groups(struct btrfs_root *root)
2109 struct btrfs_path *path;
2110 int ret;
2111 int err = 0;
2112 int bit;
2113 struct btrfs_block_group_cache *cache;
2114 struct btrfs_fs_info *info = root->fs_info;
2115 struct extent_io_tree *block_group_cache;
2116 struct btrfs_key key;
2117 struct btrfs_key found_key;
2118 struct extent_buffer *leaf;
2120 block_group_cache = &info->block_group_cache;
2122 root = info->extent_root;
2123 key.objectid = 0;
2124 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2125 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2127 path = btrfs_alloc_path();
2128 if (!path)
2129 return -ENOMEM;
2131 while(1) {
2132 ret = btrfs_search_slot(NULL, info->extent_root,
2133 &key, path, 0, 0);
2134 if (ret != 0) {
2135 err = ret;
2136 break;
2138 leaf = path->nodes[0];
2139 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2140 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2141 if (!cache) {
2142 err = -1;
2143 break;
2146 read_extent_buffer(leaf, &cache->item,
2147 btrfs_item_ptr_offset(leaf, path->slots[0]),
2148 sizeof(cache->item));
2149 memcpy(&cache->key, &found_key, sizeof(found_key));
2150 cache->cached = 0;
2151 cache->pinned = 0;
2152 key.objectid = found_key.objectid + found_key.offset;
2153 btrfs_release_path(root, path);
2155 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2156 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2157 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2158 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2159 bit = BLOCK_GROUP_DATA;
2160 cache->data = BTRFS_BLOCK_GROUP_DATA;
2161 } else {
2162 bit = BLOCK_GROUP_METADATA;
2163 cache->data = 0;
2166 /* use EXTENT_LOCKED to prevent merging */
2167 set_extent_bits(block_group_cache, found_key.objectid,
2168 found_key.objectid + found_key.offset - 1,
2169 bit | EXTENT_LOCKED, GFP_NOFS);
2170 set_state_private(block_group_cache, found_key.objectid,
2171 (unsigned long)cache);
2173 if (key.objectid >=
2174 btrfs_super_total_bytes(&info->super_copy))
2175 break;
2178 btrfs_free_path(path);
2179 return 0;
2182 static int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
2183 struct btrfs_root *root,
2184 struct btrfs_key *key,
2185 struct btrfs_block_group_item *bi)
2187 int ret;
2188 int pending_ret;
2189 struct btrfs_root *extent_root;
2191 extent_root = root->fs_info->extent_root;
2192 ret = btrfs_insert_item(trans, extent_root, key, bi, sizeof(*bi));
2193 finish_current_insert(trans, extent_root);
2194 pending_ret = del_pending_extents(trans, extent_root);
2195 if (ret)
2196 return ret;
2197 if (pending_ret)
2198 return pending_ret;
2199 return 0;
2202 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2203 struct btrfs_root *root)
2205 u64 group_size;
2206 u64 bytes_used;
2207 u64 total_bytes;
2208 u64 cur_start;
2209 u64 nr = 0;
2210 int ret;
2211 int bit;
2212 struct btrfs_root *extent_root;
2213 struct btrfs_block_group_cache *cache;
2214 struct extent_io_tree *block_group_cache;
2216 extent_root = root->fs_info->extent_root;
2217 block_group_cache = &root->fs_info->block_group_cache;
2218 group_size = BTRFS_BLOCK_GROUP_SIZE;
2219 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
2220 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2222 cur_start = 0;
2223 while (cur_start < total_bytes) {
2224 cache = malloc(sizeof(*cache));
2225 BUG_ON(!cache);
2226 cache->key.objectid = cur_start;
2227 cache->key.offset = group_size;
2228 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2229 memset(&cache->item, 0, sizeof(cache->item));
2230 if (nr == 0)
2231 btrfs_set_block_group_used(&cache->item, bytes_used);
2232 if (nr++ % 3) {
2233 bit = BLOCK_GROUP_DATA;
2234 cache->data = 1;
2235 cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;
2236 } else {
2237 bit = BLOCK_GROUP_METADATA;
2238 cache->data = 0;
2241 set_extent_bits(block_group_cache, cur_start,
2242 cur_start + group_size - 1,
2243 bit | EXTENT_LOCKED, GFP_NOFS);
2244 set_state_private(block_group_cache, cur_start,
2245 (unsigned long)cache);
2246 cur_start += group_size;
2248 /* then insert all the items */
2249 cur_start = 0;
2250 while(cur_start < total_bytes) {
2251 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2252 BUG_ON(!cache);
2253 ret = btrfs_insert_block_group(trans, root, &cache->key,
2254 &cache->item);
2255 BUG_ON(ret);
2256 cur_start += group_size;
2258 return 0;
2261 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
2262 u64 owner, u64 owner_offset)
2264 return hash_extent_ref(root_objectid, ref_generation,
2265 owner, owner_offset);
2268 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2269 struct btrfs_root *root,
2270 u64 bytenr, u64 num_bytes, int alloc,
2271 int mark_free, int data)
2273 return update_block_group(trans, root, bytenr, num_bytes,
2274 alloc, mark_free, data);