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.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
37 btrfs_root *extent_root);
38 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
39 btrfs_root *extent_root);
41 static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group)
44 struct btrfs_path *path;
47 struct extent_buffer *leaf;
48 struct extent_io_tree *free_space_cache;
58 root = root->fs_info->extent_root;
59 free_space_cache = &root->fs_info->free_space_cache;
61 if (block_group->cached)
64 path = btrfs_alloc_path();
69 first_free = block_group->key.objectid;
70 key.objectid = block_group->key.objectid;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset;
86 leaf = path->nodes[0];
87 slot = path->slots[0];
88 if (slot >= btrfs_header_nritems(leaf)) {
89 ret = btrfs_next_leaf(root, path);
98 btrfs_item_key_to_cpu(leaf, &key, slot);
99 if (key.objectid < block_group->key.objectid) {
102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) {
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
112 if (key.objectid > last) {
113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1,
118 last = key.objectid + key.offset;
126 if (block_group->key.objectid +
127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last;
130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS);
133 block_group->cached = 1;
135 btrfs_free_path(path);
139 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
143 struct extent_io_tree *block_group_cache;
144 struct btrfs_block_group_cache *block_group = NULL;
150 block_group_cache = &info->block_group_cache;
151 ret = find_first_extent_bit(block_group_cache,
152 bytenr, &start, &end,
153 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
158 ret = get_state_private(block_group_cache, start, &ptr);
162 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
163 if (block_group->key.objectid <= bytenr && bytenr <
164 block_group->key.objectid + block_group->key.offset)
169 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
171 if ((bits & BLOCK_GROUP_DATA) &&
172 (cache->flags & BTRFS_BLOCK_GROUP_DATA))
174 if ((bits & BLOCK_GROUP_METADATA) &&
175 (cache->flags & BTRFS_BLOCK_GROUP_METADATA))
177 if ((bits & BLOCK_GROUP_SYSTEM) &&
178 (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM))
183 static int noinline find_search_start(struct btrfs_root *root,
184 struct btrfs_block_group_cache **cache_ret,
185 u64 *start_ret, int num, int data)
188 struct btrfs_block_group_cache *cache = *cache_ret;
193 u64 search_start = *start_ret;
200 ret = cache_block_group(root, cache);
204 last = max(search_start, cache->key.objectid);
205 if (!block_group_bits(cache, data)) {
210 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
211 last, &start, &end, EXTENT_DIRTY);
218 start = max(last, start);
220 if (last - start < num) {
221 if (last == cache->key.objectid + cache->key.offset)
225 if (start + num > cache->key.objectid + cache->key.offset)
231 cache = btrfs_lookup_block_group(root->fs_info, search_start);
233 printk("Unable to find block group for %Lu\n", search_start);
239 last = cache->key.objectid + cache->key.offset;
241 cache = btrfs_lookup_block_group(root->fs_info, last);
251 if (cache_miss && !cache->cached) {
252 cache_block_group(root, cache);
254 cache = btrfs_lookup_block_group(root->fs_info, last);
256 cache = btrfs_find_block_group(root, cache, last, data, 0);
264 static u64 div_factor(u64 num, int factor)
273 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
274 struct btrfs_block_group_cache
275 *hint, u64 search_start,
278 struct btrfs_block_group_cache *cache;
279 struct extent_io_tree *block_group_cache;
280 struct btrfs_block_group_cache *found_group = NULL;
281 struct btrfs_fs_info *info = root->fs_info;
294 block_group_cache = &info->block_group_cache;
302 struct btrfs_block_group_cache *shint;
303 shint = btrfs_lookup_block_group(info, search_start);
304 if (shint && block_group_bits(shint, data)) {
305 used = btrfs_block_group_used(&shint->item);
306 if (used + shint->pinned <
307 div_factor(shint->key.offset, factor)) {
312 if (hint && block_group_bits(hint, data)) {
313 used = btrfs_block_group_used(&hint->item);
314 if (used + hint->pinned <
315 div_factor(hint->key.offset, factor)) {
318 last = hint->key.objectid + hint->key.offset;
322 hint_last = max(hint->key.objectid, search_start);
324 hint_last = search_start;
330 ret = find_first_extent_bit(block_group_cache, last,
335 ret = get_state_private(block_group_cache, start, &ptr);
339 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
340 last = cache->key.objectid + cache->key.offset;
341 used = btrfs_block_group_used(&cache->item);
344 free_check = cache->key.offset;
346 free_check = div_factor(cache->key.offset, factor);
347 if (used + cache->pinned < free_check) {
362 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
363 u64 owner, u64 owner_offset)
365 u32 high_crc = ~(u32)0;
366 u32 low_crc = ~(u32)0;
369 lenum = cpu_to_le64(root_objectid);
370 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
371 lenum = cpu_to_le64(ref_generation);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
373 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
374 lenum = cpu_to_le64(owner);
375 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
376 lenum = cpu_to_le64(owner_offset);
377 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
379 return ((u64)high_crc << 32) | (u64)low_crc;
382 static int match_extent_ref(struct extent_buffer *leaf,
383 struct btrfs_extent_ref *disk_ref,
384 struct btrfs_extent_ref *cpu_ref)
389 if (cpu_ref->objectid)
390 len = sizeof(*cpu_ref);
392 len = 2 * sizeof(u64);
393 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
398 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
399 struct btrfs_root *root,
400 struct btrfs_path *path, u64 bytenr,
402 u64 ref_generation, u64 owner,
403 u64 owner_offset, int del)
406 struct btrfs_key key;
407 struct btrfs_key found_key;
408 struct btrfs_extent_ref ref;
409 struct extent_buffer *leaf;
410 struct btrfs_extent_ref *disk_ref;
414 btrfs_set_stack_ref_root(&ref, root_objectid);
415 btrfs_set_stack_ref_generation(&ref, ref_generation);
416 btrfs_set_stack_ref_objectid(&ref, owner);
417 btrfs_set_stack_ref_offset(&ref, owner_offset);
419 hash = hash_extent_ref(root_objectid, ref_generation, owner,
422 key.objectid = bytenr;
423 key.type = BTRFS_EXTENT_REF_KEY;
426 ret = btrfs_search_slot(trans, root, &key, path,
430 leaf = path->nodes[0];
432 u32 nritems = btrfs_header_nritems(leaf);
433 if (path->slots[0] >= nritems) {
434 ret2 = btrfs_next_leaf(root, path);
437 leaf = path->nodes[0];
439 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
440 if (found_key.objectid != bytenr ||
441 found_key.type != BTRFS_EXTENT_REF_KEY)
443 key.offset = found_key.offset;
445 btrfs_release_path(root, path);
449 disk_ref = btrfs_item_ptr(path->nodes[0],
451 struct btrfs_extent_ref);
452 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
456 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
457 key.offset = found_key.offset + 1;
458 btrfs_release_path(root, path);
465 * Back reference rules. Back refs have three main goals:
467 * 1) differentiate between all holders of references to an extent so that
468 * when a reference is dropped we can make sure it was a valid reference
469 * before freeing the extent.
471 * 2) Provide enough information to quickly find the holders of an extent
472 * if we notice a given block is corrupted or bad.
474 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
475 * maintenance. This is actually the same as #2, but with a slightly
476 * different use case.
478 * File extents can be referenced by:
480 * - multiple snapshots, subvolumes, or different generations in one subvol
481 * - different files inside a single subvolume (in theory, not implemented yet)
482 * - different offsets inside a file (bookend extents in file.c)
484 * The extent ref structure has fields for:
486 * - Objectid of the subvolume root
487 * - Generation number of the tree holding the reference
488 * - objectid of the file holding the reference
489 * - offset in the file corresponding to the key holding the reference
491 * When a file extent is allocated the fields are filled in:
492 * (root_key.objectid, trans->transid, inode objectid, offset in file)
494 * When a leaf is cow'd new references are added for every file extent found
495 * in the leaf. It looks the same as the create case, but trans->transid
496 * will be different when the block is cow'd.
498 * (root_key.objectid, trans->transid, inode objectid, offset in file)
500 * When a file extent is removed either during snapshot deletion or file
501 * truncation, the corresponding back reference is found
504 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
505 * inode objectid, offset in file)
507 * Btree extents can be referenced by:
509 * - Different subvolumes
510 * - Different generations of the same subvolume
512 * Storing sufficient information for a full reverse mapping of a btree
513 * block would require storing the lowest key of the block in the backref,
514 * and it would require updating that lowest key either before write out or
515 * every time it changed. Instead, the objectid of the lowest key is stored
516 * along with the level of the tree block. This provides a hint
517 * about where in the btree the block can be found. Searches through the
518 * btree only need to look for a pointer to that block, so they stop one
519 * level higher than the level recorded in the backref.
521 * Some btrees do not do reference counting on their extents. These
522 * include the extent tree and the tree of tree roots. Backrefs for these
523 * trees always have a generation of zero.
525 * When a tree block is created, back references are inserted:
527 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
529 * When a tree block is cow'd in a reference counted root,
530 * new back references are added for all the blocks it points to.
531 * These are of the form (trans->transid will have increased since creation):
533 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
535 * Because the lowest_key_objectid and the level are just hints
536 * they are not used when backrefs are deleted. When a backref is deleted:
538 * if backref was for a tree root:
539 * root_objectid = root->root_key.objectid
541 * root_objectid = btrfs_header_owner(parent)
543 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
545 * Back Reference Key hashing:
547 * Back references have four fields, each 64 bits long. Unfortunately,
548 * This is hashed into a single 64 bit number and placed into the key offset.
549 * The key objectid corresponds to the first byte in the extent, and the
550 * key type is set to BTRFS_EXTENT_REF_KEY
552 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
554 struct btrfs_path *path, u64 bytenr,
555 u64 root_objectid, u64 ref_generation,
556 u64 owner, u64 owner_offset)
559 struct btrfs_key key;
560 struct btrfs_extent_ref ref;
561 struct btrfs_extent_ref *disk_ref;
564 btrfs_set_stack_ref_root(&ref, root_objectid);
565 btrfs_set_stack_ref_generation(&ref, ref_generation);
566 btrfs_set_stack_ref_objectid(&ref, owner);
567 btrfs_set_stack_ref_offset(&ref, owner_offset);
569 hash = hash_extent_ref(root_objectid, ref_generation, owner,
572 key.objectid = bytenr;
573 key.type = BTRFS_EXTENT_REF_KEY;
575 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
576 while (ret == -EEXIST) {
577 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
578 struct btrfs_extent_ref);
579 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
582 btrfs_release_path(root, path);
583 ret = btrfs_insert_empty_item(trans, root, path, &key,
588 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
589 struct btrfs_extent_ref);
590 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
592 btrfs_mark_buffer_dirty(path->nodes[0]);
594 btrfs_release_path(root, path);
598 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
600 u64 bytenr, u64 num_bytes,
601 u64 root_objectid, u64 ref_generation,
602 u64 owner, u64 owner_offset)
604 struct btrfs_path *path;
606 struct btrfs_key key;
607 struct extent_buffer *l;
608 struct btrfs_extent_item *item;
611 WARN_ON(num_bytes < root->sectorsize);
612 path = btrfs_alloc_path();
616 key.objectid = bytenr;
617 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
618 key.offset = num_bytes;
619 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
628 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
629 refs = btrfs_extent_refs(l, item);
630 btrfs_set_extent_refs(l, item, refs + 1);
631 btrfs_mark_buffer_dirty(path->nodes[0]);
633 btrfs_release_path(root->fs_info->extent_root, path);
635 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
636 path, bytenr, root_objectid,
637 ref_generation, owner, owner_offset);
639 finish_current_insert(trans, root->fs_info->extent_root);
640 del_pending_extents(trans, root->fs_info->extent_root);
642 btrfs_free_path(path);
646 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root)
649 finish_current_insert(trans, root->fs_info->extent_root);
650 del_pending_extents(trans, root->fs_info->extent_root);
654 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
655 struct btrfs_root *root, u64 bytenr,
656 u64 num_bytes, u32 *refs)
658 struct btrfs_path *path;
660 struct btrfs_key key;
661 struct extent_buffer *l;
662 struct btrfs_extent_item *item;
664 WARN_ON(num_bytes < root->sectorsize);
665 path = btrfs_alloc_path();
666 key.objectid = bytenr;
667 key.offset = num_bytes;
668 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
669 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
674 btrfs_print_leaf(root, path->nodes[0]);
675 printk("failed to find block number %Lu\n", bytenr);
679 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
680 *refs = btrfs_extent_refs(l, item);
682 btrfs_free_path(path);
686 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
687 struct btrfs_path *count_path,
690 struct btrfs_root *extent_root = root->fs_info->extent_root;
691 struct btrfs_path *path;
694 u64 root_objectid = root->root_key.objectid;
700 struct btrfs_key key;
701 struct btrfs_key found_key;
702 struct extent_buffer *l;
703 struct btrfs_extent_item *item;
704 struct btrfs_extent_ref *ref_item;
707 path = btrfs_alloc_path();
710 bytenr = first_extent;
712 bytenr = count_path->nodes[level]->start;
715 key.objectid = bytenr;
718 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
719 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
725 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
727 if (found_key.objectid != bytenr ||
728 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
732 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
733 refs = btrfs_extent_refs(l, item);
735 nritems = btrfs_header_nritems(l);
736 if (path->slots[0] >= nritems) {
737 ret = btrfs_next_leaf(extent_root, path);
742 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
743 if (found_key.objectid != bytenr)
745 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
751 ref_item = btrfs_item_ptr(l, path->slots[0],
752 struct btrfs_extent_ref);
753 found_objectid = btrfs_ref_root(l, ref_item);
755 if (found_objectid != root_objectid) {
762 if (cur_count == 0) {
766 if (level >= 0 && root->node == count_path->nodes[level])
769 btrfs_release_path(root, path);
773 btrfs_free_path(path);
776 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
777 struct btrfs_root *root, u64 owner_objectid)
783 struct btrfs_disk_key disk_key;
785 level = btrfs_header_level(root->node);
786 generation = trans->transid;
787 nritems = btrfs_header_nritems(root->node);
790 btrfs_item_key(root->node, &disk_key, 0);
792 btrfs_node_key(root->node, &disk_key, 0);
793 key_objectid = btrfs_disk_key_objectid(&disk_key);
797 return btrfs_inc_extent_ref(trans, root, root->node->start,
798 root->node->len, owner_objectid,
799 generation, level, key_objectid);
802 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
803 struct extent_buffer *buf)
807 struct btrfs_key key;
808 struct btrfs_file_extent_item *fi;
817 level = btrfs_header_level(buf);
818 nritems = btrfs_header_nritems(buf);
819 for (i = 0; i < nritems; i++) {
822 btrfs_item_key_to_cpu(buf, &key, i);
823 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
825 fi = btrfs_item_ptr(buf, i,
826 struct btrfs_file_extent_item);
827 if (btrfs_file_extent_type(buf, fi) ==
828 BTRFS_FILE_EXTENT_INLINE)
830 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
831 if (disk_bytenr == 0)
833 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
834 btrfs_file_extent_disk_num_bytes(buf, fi),
835 root->root_key.objectid, trans->transid,
836 key.objectid, key.offset);
842 bytenr = btrfs_node_blockptr(buf, i);
843 btrfs_node_key_to_cpu(buf, &key, i);
844 ret = btrfs_inc_extent_ref(trans, root, bytenr,
845 btrfs_level_size(root, level - 1),
846 root->root_key.objectid,
848 level - 1, key.objectid);
859 for (i =0; i < faili; i++) {
862 btrfs_item_key_to_cpu(buf, &key, i);
863 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
865 fi = btrfs_item_ptr(buf, i,
866 struct btrfs_file_extent_item);
867 if (btrfs_file_extent_type(buf, fi) ==
868 BTRFS_FILE_EXTENT_INLINE)
870 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
871 if (disk_bytenr == 0)
873 err = btrfs_free_extent(trans, root, disk_bytenr,
874 btrfs_file_extent_disk_num_bytes(buf,
878 bytenr = btrfs_node_blockptr(buf, i);
879 err = btrfs_free_extent(trans, root, bytenr,
880 btrfs_level_size(root, level - 1), 0);
888 static int write_one_cache_group(struct btrfs_trans_handle *trans,
889 struct btrfs_root *root,
890 struct btrfs_path *path,
891 struct btrfs_block_group_cache *cache)
895 struct btrfs_root *extent_root = root->fs_info->extent_root;
897 struct extent_buffer *leaf;
899 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
904 leaf = path->nodes[0];
905 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
906 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
907 btrfs_mark_buffer_dirty(leaf);
908 btrfs_release_path(extent_root, path);
910 finish_current_insert(trans, extent_root);
911 pending_ret = del_pending_extents(trans, extent_root);
920 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
921 struct btrfs_root *root)
923 struct extent_io_tree *block_group_cache;
924 struct btrfs_block_group_cache *cache;
928 struct btrfs_path *path;
934 block_group_cache = &root->fs_info->block_group_cache;
935 path = btrfs_alloc_path();
940 ret = find_first_extent_bit(block_group_cache, last,
941 &start, &end, BLOCK_GROUP_DIRTY);
946 ret = get_state_private(block_group_cache, start, &ptr);
950 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
951 err = write_one_cache_group(trans, root,
954 * if we fail to write the cache group, we want
955 * to keep it marked dirty in hopes that a later
962 clear_extent_bits(block_group_cache, start, end,
963 BLOCK_GROUP_DIRTY, GFP_NOFS);
965 btrfs_free_path(path);
969 static int update_block_group(struct btrfs_trans_handle *trans,
970 struct btrfs_root *root,
971 u64 bytenr, u64 num_bytes, int alloc,
974 struct btrfs_block_group_cache *cache;
975 struct btrfs_fs_info *info = root->fs_info;
976 u64 total = num_bytes;
983 cache = btrfs_lookup_block_group(info, bytenr);
987 byte_in_group = bytenr - cache->key.objectid;
988 WARN_ON(byte_in_group > cache->key.offset);
989 start = cache->key.objectid;
990 end = start + cache->key.offset - 1;
991 set_extent_bits(&info->block_group_cache, start, end,
992 BLOCK_GROUP_DIRTY, GFP_NOFS);
994 old_val = btrfs_block_group_used(&cache->item);
995 num_bytes = min(total, cache->key.offset - byte_in_group);
997 old_val += num_bytes;
999 old_val -= num_bytes;
1001 set_extent_dirty(&info->free_space_cache,
1002 bytenr, bytenr + num_bytes - 1,
1006 btrfs_set_block_group_used(&cache->item, old_val);
1008 bytenr += num_bytes;
1012 static int update_pinned_extents(struct btrfs_root *root,
1013 u64 bytenr, u64 num, int pin)
1016 struct btrfs_block_group_cache *cache;
1017 struct btrfs_fs_info *fs_info = root->fs_info;
1020 set_extent_dirty(&fs_info->pinned_extents,
1021 bytenr, bytenr + num - 1, GFP_NOFS);
1023 clear_extent_dirty(&fs_info->pinned_extents,
1024 bytenr, bytenr + num - 1, GFP_NOFS);
1027 cache = btrfs_lookup_block_group(fs_info, bytenr);
1029 len = min(num, cache->key.offset -
1030 (bytenr - cache->key.objectid));
1032 cache->pinned += len;
1033 fs_info->total_pinned += len;
1035 cache->pinned -= len;
1036 fs_info->total_pinned -= len;
1044 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1049 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1053 ret = find_first_extent_bit(pinned_extents, last,
1054 &start, &end, EXTENT_DIRTY);
1057 set_extent_dirty(copy, start, end, GFP_NOFS);
1063 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1064 struct btrfs_root *root,
1065 struct extent_io_tree *unpin)
1070 struct extent_io_tree *free_space_cache;
1071 free_space_cache = &root->fs_info->free_space_cache;
1074 ret = find_first_extent_bit(unpin, 0, &start, &end,
1078 update_pinned_extents(root, start, end + 1 - start, 0);
1079 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1080 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1085 static int finish_current_insert(struct btrfs_trans_handle *trans,
1086 struct btrfs_root *extent_root)
1090 struct btrfs_fs_info *info = extent_root->fs_info;
1091 struct extent_buffer *eb;
1092 struct btrfs_path *path;
1093 struct btrfs_key ins;
1094 struct btrfs_disk_key first;
1095 struct btrfs_extent_item extent_item;
1100 btrfs_set_stack_extent_refs(&extent_item, 1);
1101 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1102 path = btrfs_alloc_path();
1105 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1106 &end, EXTENT_LOCKED);
1110 ins.objectid = start;
1111 ins.offset = end + 1 - start;
1112 err = btrfs_insert_item(trans, extent_root, &ins,
1113 &extent_item, sizeof(extent_item));
1114 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1116 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1117 level = btrfs_header_level(eb);
1119 btrfs_item_key(eb, &first, 0);
1121 btrfs_node_key(eb, &first, 0);
1123 err = btrfs_insert_extent_backref(trans, extent_root, path,
1124 start, extent_root->root_key.objectid,
1126 btrfs_disk_key_objectid(&first));
1128 free_extent_buffer(eb);
1130 btrfs_free_path(path);
1134 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1138 struct extent_buffer *buf;
1141 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1143 if (btrfs_buffer_uptodate(buf)) {
1145 root->fs_info->running_transaction->transid;
1146 if (btrfs_header_generation(buf) == transid) {
1147 free_extent_buffer(buf);
1151 free_extent_buffer(buf);
1153 update_pinned_extents(root, bytenr, num_bytes, 1);
1155 set_extent_bits(&root->fs_info->pending_del,
1156 bytenr, bytenr + num_bytes - 1,
1157 EXTENT_LOCKED, GFP_NOFS);
1164 * remove an extent from the root, returns 0 on success
1166 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1167 *root, u64 bytenr, u64 num_bytes,
1168 u64 root_objectid, u64 ref_generation,
1169 u64 owner_objectid, u64 owner_offset, int pin,
1172 struct btrfs_path *path;
1173 struct btrfs_key key;
1174 struct btrfs_fs_info *info = root->fs_info;
1175 struct btrfs_extent_ops *ops = info->extent_ops;
1176 struct btrfs_root *extent_root = info->extent_root;
1177 struct extent_buffer *leaf;
1179 int extent_slot = 0;
1180 int found_extent = 0;
1182 struct btrfs_extent_item *ei;
1185 key.objectid = bytenr;
1186 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1187 key.offset = num_bytes;
1189 path = btrfs_alloc_path();
1193 ret = lookup_extent_backref(trans, extent_root, path,
1194 bytenr, root_objectid,
1196 owner_objectid, owner_offset, 1);
1198 struct btrfs_key found_key;
1199 extent_slot = path->slots[0];
1200 while(extent_slot > 0) {
1202 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1204 if (found_key.objectid != bytenr)
1206 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1207 found_key.offset == num_bytes) {
1211 if (path->slots[0] - extent_slot > 5)
1215 ret = btrfs_del_item(trans, extent_root, path);
1217 btrfs_print_leaf(extent_root, path->nodes[0]);
1219 printk("Unable to find ref byte nr %Lu root %Lu "
1220 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1221 root_objectid, ref_generation, owner_objectid,
1224 if (!found_extent) {
1225 btrfs_release_path(extent_root, path);
1226 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1230 extent_slot = path->slots[0];
1233 leaf = path->nodes[0];
1234 ei = btrfs_item_ptr(leaf, extent_slot,
1235 struct btrfs_extent_item);
1236 refs = btrfs_extent_refs(leaf, ei);
1239 btrfs_set_extent_refs(leaf, ei, refs);
1241 btrfs_mark_buffer_dirty(leaf);
1243 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1244 /* if the back ref and the extent are next to each other
1245 * they get deleted below in one shot
1247 path->slots[0] = extent_slot;
1249 } else if (found_extent) {
1250 /* otherwise delete the extent back ref */
1251 ret = btrfs_del_item(trans, extent_root, path);
1253 /* if refs are 0, we need to setup the path for deletion */
1255 btrfs_release_path(extent_root, path);
1256 ret = btrfs_search_slot(trans, extent_root, &key, path,
1269 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1275 /* block accounting for super block */
1276 super_used = btrfs_super_bytes_used(&info->super_copy);
1277 btrfs_set_super_bytes_used(&info->super_copy,
1278 super_used - num_bytes);
1280 /* block accounting for root item */
1281 root_used = btrfs_root_used(&root->root_item);
1282 btrfs_set_root_used(&root->root_item,
1283 root_used - num_bytes);
1284 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1289 if (ops && ops->free_extent)
1290 ops->free_extent(root, bytenr, num_bytes);
1292 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1296 btrfs_free_path(path);
1297 finish_current_insert(trans, extent_root);
1302 * find all the blocks marked as pending in the radix tree and remove
1303 * them from the extent map
1305 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1306 btrfs_root *extent_root)
1312 struct extent_io_tree *pending_del;
1313 struct extent_io_tree *pinned_extents;
1315 pending_del = &extent_root->fs_info->pending_del;
1316 pinned_extents = &extent_root->fs_info->pinned_extents;
1319 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1323 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1324 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1326 ret = __free_extent(trans, extent_root,
1327 start, end + 1 - start,
1328 extent_root->root_key.objectid,
1337 * remove an extent from the root, returns 0 on success
1339 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1340 *root, u64 bytenr, u64 num_bytes,
1341 u64 root_objectid, u64 ref_generation,
1342 u64 owner_objectid, u64 owner_offset, int pin)
1344 struct btrfs_root *extent_root = root->fs_info->extent_root;
1348 WARN_ON(num_bytes < root->sectorsize);
1349 if (!root->ref_cows)
1352 if (root == extent_root) {
1353 pin_down_bytes(root, bytenr, num_bytes, 1);
1356 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1357 ref_generation, owner_objectid, owner_offset,
1359 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1360 return ret ? ret : pending_ret;
1363 static u64 stripe_align(struct btrfs_root *root, u64 val)
1365 u64 mask = ((u64)root->stripesize - 1);
1366 u64 ret = (val + mask) & ~mask;
1371 * walks the btree of allocated extents and find a hole of a given size.
1372 * The key ins is changed to record the hole:
1373 * ins->objectid == block start
1374 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1375 * ins->offset == number of blocks
1376 * Any available blocks before search_start are skipped.
1378 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1379 struct btrfs_root *orig_root,
1380 u64 num_bytes, u64 empty_size,
1381 u64 search_start, u64 search_end,
1382 u64 hint_byte, struct btrfs_key *ins,
1383 u64 exclude_start, u64 exclude_nr,
1387 u64 orig_search_start = search_start;
1388 struct btrfs_root * root = orig_root->fs_info->extent_root;
1389 struct btrfs_fs_info *info = root->fs_info;
1390 u64 total_needed = num_bytes;
1391 struct btrfs_block_group_cache *block_group;
1395 WARN_ON(num_bytes < root->sectorsize);
1396 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1398 if (search_end == (u64)-1)
1399 search_end = btrfs_super_total_bytes(&info->super_copy);
1402 block_group = btrfs_lookup_block_group(info, hint_byte);
1404 hint_byte = search_start;
1405 block_group = btrfs_find_block_group(root, block_group,
1406 hint_byte, data, 1);
1408 block_group = btrfs_find_block_group(root,
1410 search_start, data, 1);
1413 total_needed += empty_size;
1417 block_group = btrfs_lookup_block_group(info, search_start);
1419 block_group = btrfs_lookup_block_group(info,
1422 ret = find_search_start(root, &block_group, &search_start,
1423 total_needed, data);
1427 search_start = stripe_align(root, search_start);
1428 ins->objectid = search_start;
1429 ins->offset = num_bytes;
1431 if (ins->objectid + num_bytes >= search_end)
1434 if (ins->objectid + num_bytes >
1435 block_group->key.objectid + block_group->key.offset) {
1436 search_start = block_group->key.objectid +
1437 block_group->key.offset;
1441 if (test_range_bit(&info->extent_ins, ins->objectid,
1442 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1443 search_start = ins->objectid + num_bytes;
1447 if (test_range_bit(&info->pinned_extents, ins->objectid,
1448 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1449 search_start = ins->objectid + num_bytes;
1453 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1454 ins->objectid < exclude_start + exclude_nr)) {
1455 search_start = exclude_start + exclude_nr;
1459 if (!(data & BLOCK_GROUP_DATA)) {
1460 block_group = btrfs_lookup_block_group(info, ins->objectid);
1462 trans->block_group = block_group;
1464 ins->offset = num_bytes;
1468 if (search_start + num_bytes >= search_end) {
1470 search_start = orig_search_start;
1477 total_needed -= empty_size;
1482 block_group = btrfs_lookup_block_group(info, search_start);
1484 block_group = btrfs_find_block_group(root, block_group,
1485 search_start, data, 0);
1492 * finds a free extent and does all the dirty work required for allocation
1493 * returns the key for the extent through ins, and a tree buffer for
1494 * the first block of the extent through buf.
1496 * returns 0 if everything worked, non-zero otherwise.
1498 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1499 struct btrfs_root *root,
1500 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1501 u64 owner, u64 owner_offset,
1502 u64 empty_size, u64 hint_byte,
1503 u64 search_end, struct btrfs_key *ins, int data)
1507 u64 super_used, root_used;
1508 u64 search_start = 0;
1509 struct btrfs_fs_info *info = root->fs_info;
1510 struct btrfs_extent_ops *ops = info->extent_ops;
1512 struct btrfs_root *extent_root = info->extent_root;
1513 struct btrfs_path *path;
1514 struct btrfs_extent_item *extent_item;
1515 struct btrfs_extent_ref *ref;
1516 struct btrfs_key keys[2];
1519 data = BLOCK_GROUP_DATA;
1520 else if (info->force_system_allocs || root == root->fs_info->chunk_root)
1521 data = BLOCK_GROUP_SYSTEM;
1523 data = BLOCK_GROUP_METADATA;
1525 WARN_ON(num_bytes < root->sectorsize);
1526 if (ops && ops->alloc_extent) {
1527 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1529 ret = find_free_extent(trans, root, num_bytes, empty_size,
1530 search_start, search_end, hint_byte,
1531 ins, trans->alloc_exclude_start,
1532 trans->alloc_exclude_nr, data);
1538 /* block accounting for super block */
1539 super_used = btrfs_super_bytes_used(&info->super_copy);
1540 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1542 /* block accounting for root item */
1543 root_used = btrfs_root_used(&root->root_item);
1544 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1546 clear_extent_dirty(&root->fs_info->free_space_cache,
1547 ins->objectid, ins->objectid + ins->offset - 1,
1550 if (root == extent_root) {
1551 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1552 ins->objectid + ins->offset - 1,
1553 EXTENT_LOCKED, GFP_NOFS);
1557 WARN_ON(trans->alloc_exclude_nr);
1558 trans->alloc_exclude_start = ins->objectid;
1559 trans->alloc_exclude_nr = ins->offset;
1561 memcpy(&keys[0], ins, sizeof(*ins));
1562 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1563 owner, owner_offset);
1564 keys[1].objectid = ins->objectid;
1565 keys[1].type = BTRFS_EXTENT_REF_KEY;
1566 sizes[0] = sizeof(*extent_item);
1567 sizes[1] = sizeof(*ref);
1569 path = btrfs_alloc_path();
1572 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1576 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1577 struct btrfs_extent_item);
1578 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1579 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1580 struct btrfs_extent_ref);
1582 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1583 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1584 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1585 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1587 btrfs_mark_buffer_dirty(path->nodes[0]);
1589 trans->alloc_exclude_start = 0;
1590 trans->alloc_exclude_nr = 0;
1591 btrfs_free_path(path);
1592 finish_current_insert(trans, extent_root);
1593 pending_ret = del_pending_extents(trans, extent_root);
1603 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1605 printk("update block group failed for %Lu %Lu\n",
1606 ins->objectid, ins->offset);
1613 * helper function to allocate a block for a given tree
1614 * returns the tree buffer or NULL.
1616 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1617 struct btrfs_root *root,
1619 u64 root_objectid, u64 hint,
1625 ref_generation = trans->transid;
1630 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1631 ref_generation, 0, 0, hint, empty_size);
1635 * helper function to allocate a block for a given tree
1636 * returns the tree buffer or NULL.
1638 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1639 struct btrfs_root *root,
1648 struct btrfs_key ins;
1650 struct extent_buffer *buf;
1652 ret = btrfs_alloc_extent(trans, root, blocksize,
1653 root_objectid, ref_generation,
1654 level, first_objectid, empty_size, hint,
1658 return ERR_PTR(ret);
1660 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1662 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1663 root->root_key.objectid, ref_generation,
1666 return ERR_PTR(-ENOMEM);
1668 btrfs_set_buffer_uptodate(buf);
1669 trans->blocks_used++;
1673 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1674 struct btrfs_root *root,
1675 struct extent_buffer *leaf)
1678 u64 leaf_generation;
1679 struct btrfs_key key;
1680 struct btrfs_file_extent_item *fi;
1685 BUG_ON(!btrfs_is_leaf(leaf));
1686 nritems = btrfs_header_nritems(leaf);
1687 leaf_owner = btrfs_header_owner(leaf);
1688 leaf_generation = btrfs_header_generation(leaf);
1690 for (i = 0; i < nritems; i++) {
1693 btrfs_item_key_to_cpu(leaf, &key, i);
1694 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1696 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1697 if (btrfs_file_extent_type(leaf, fi) ==
1698 BTRFS_FILE_EXTENT_INLINE)
1701 * FIXME make sure to insert a trans record that
1702 * repeats the snapshot del on crash
1704 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1705 if (disk_bytenr == 0)
1707 ret = btrfs_free_extent(trans, root, disk_bytenr,
1708 btrfs_file_extent_disk_num_bytes(leaf, fi),
1709 leaf_owner, leaf_generation,
1710 key.objectid, key.offset, 0);
1716 static void noinline reada_walk_down(struct btrfs_root *root,
1717 struct extent_buffer *node,
1730 nritems = btrfs_header_nritems(node);
1731 level = btrfs_header_level(node);
1735 for (i = slot; i < nritems && skipped < 32; i++) {
1736 bytenr = btrfs_node_blockptr(node, i);
1737 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
1738 (last > bytenr && last - bytenr > 32 * 1024))) {
1742 blocksize = btrfs_level_size(root, level - 1);
1744 ret = lookup_extent_ref(NULL, root, bytenr,
1752 mutex_unlock(&root->fs_info->fs_mutex);
1753 ret = readahead_tree_block(root, bytenr, blocksize);
1754 last = bytenr + blocksize;
1756 mutex_lock(&root->fs_info->fs_mutex);
1763 * helper function for drop_snapshot, this walks down the tree dropping ref
1764 * counts as it goes.
1766 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1767 struct btrfs_root *root,
1768 struct btrfs_path *path, int *level)
1773 struct extent_buffer *next;
1774 struct extent_buffer *cur;
1775 struct extent_buffer *parent;
1780 WARN_ON(*level < 0);
1781 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1782 ret = lookup_extent_ref(trans, root,
1783 path->nodes[*level]->start,
1784 path->nodes[*level]->len, &refs);
1790 * walk down to the last node level and free all the leaves
1792 while(*level >= 0) {
1793 WARN_ON(*level < 0);
1794 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1795 cur = path->nodes[*level];
1797 if (btrfs_header_level(cur) != *level)
1800 if (path->slots[*level] >=
1801 btrfs_header_nritems(cur))
1804 ret = drop_leaf_ref(trans, root, cur);
1808 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1809 blocksize = btrfs_level_size(root, *level - 1);
1810 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1813 parent = path->nodes[*level];
1814 root_owner = btrfs_header_owner(parent);
1815 root_gen = btrfs_header_generation(parent);
1816 path->slots[*level]++;
1817 ret = btrfs_free_extent(trans, root, bytenr,
1818 blocksize, root_owner,
1823 next = btrfs_find_tree_block(root, bytenr, blocksize);
1824 if (!next || !btrfs_buffer_uptodate(next)) {
1825 free_extent_buffer(next);
1826 reada_walk_down(root, cur, path->slots[*level]);
1827 mutex_unlock(&root->fs_info->fs_mutex);
1828 next = read_tree_block(root, bytenr, blocksize);
1829 mutex_lock(&root->fs_info->fs_mutex);
1831 /* we dropped the lock, check one more time */
1832 ret = lookup_extent_ref(trans, root, bytenr,
1836 parent = path->nodes[*level];
1837 root_owner = btrfs_header_owner(parent);
1838 root_gen = btrfs_header_generation(parent);
1840 path->slots[*level]++;
1841 free_extent_buffer(next);
1842 ret = btrfs_free_extent(trans, root, bytenr,
1850 WARN_ON(*level <= 0);
1851 if (path->nodes[*level-1])
1852 free_extent_buffer(path->nodes[*level-1]);
1853 path->nodes[*level-1] = next;
1854 *level = btrfs_header_level(next);
1855 path->slots[*level] = 0;
1858 WARN_ON(*level < 0);
1859 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1861 if (path->nodes[*level] == root->node) {
1862 root_owner = root->root_key.objectid;
1863 parent = path->nodes[*level];
1865 parent = path->nodes[*level + 1];
1866 root_owner = btrfs_header_owner(parent);
1869 root_gen = btrfs_header_generation(parent);
1870 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1871 path->nodes[*level]->len,
1872 root_owner, root_gen, 0, 0, 1);
1873 free_extent_buffer(path->nodes[*level]);
1874 path->nodes[*level] = NULL;
1881 * helper for dropping snapshots. This walks back up the tree in the path
1882 * to find the first node higher up where we haven't yet gone through
1885 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_path *path, int *level)
1891 struct btrfs_root_item *root_item = &root->root_item;
1896 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1897 slot = path->slots[i];
1898 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1899 struct extent_buffer *node;
1900 struct btrfs_disk_key disk_key;
1901 node = path->nodes[i];
1904 WARN_ON(*level == 0);
1905 btrfs_node_key(node, &disk_key, path->slots[i]);
1906 memcpy(&root_item->drop_progress,
1907 &disk_key, sizeof(disk_key));
1908 root_item->drop_level = i;
1911 if (path->nodes[*level] == root->node) {
1912 root_owner = root->root_key.objectid;
1914 btrfs_header_generation(path->nodes[*level]);
1916 struct extent_buffer *node;
1917 node = path->nodes[*level + 1];
1918 root_owner = btrfs_header_owner(node);
1919 root_gen = btrfs_header_generation(node);
1921 ret = btrfs_free_extent(trans, root,
1922 path->nodes[*level]->start,
1923 path->nodes[*level]->len,
1924 root_owner, root_gen, 0, 0, 1);
1926 free_extent_buffer(path->nodes[*level]);
1927 path->nodes[*level] = NULL;
1935 * drop the reference count on the tree rooted at 'snap'. This traverses
1936 * the tree freeing any blocks that have a ref count of zero after being
1939 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1945 struct btrfs_path *path;
1948 struct btrfs_root_item *root_item = &root->root_item;
1950 path = btrfs_alloc_path();
1953 level = btrfs_header_level(root->node);
1955 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1956 path->nodes[level] = root->node;
1957 extent_buffer_get(root->node);
1958 path->slots[level] = 0;
1960 struct btrfs_key key;
1961 struct btrfs_disk_key found_key;
1962 struct extent_buffer *node;
1964 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1965 level = root_item->drop_level;
1966 path->lowest_level = level;
1967 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1972 node = path->nodes[level];
1973 btrfs_node_key(node, &found_key, path->slots[level]);
1974 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1975 sizeof(found_key)));
1978 wret = walk_down_tree(trans, root, path, &level);
1984 wret = walk_up_tree(trans, root, path, &level);
1994 for (i = 0; i <= orig_level; i++) {
1995 if (path->nodes[i]) {
1996 free_extent_buffer(path->nodes[i]);
1997 path->nodes[i] = NULL;
2001 btrfs_free_path(path);
2005 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2012 ret = find_first_extent_bit(&info->block_group_cache, 0,
2013 &start, &end, (unsigned int)-1);
2016 ret = get_state_private(&info->block_group_cache, start, &ptr);
2018 kfree((void *)(unsigned long)ptr);
2019 clear_extent_bits(&info->block_group_cache, start,
2020 end, (unsigned int)-1, GFP_NOFS);
2023 ret = find_first_extent_bit(&info->free_space_cache, 0,
2024 &start, &end, EXTENT_DIRTY);
2027 clear_extent_dirty(&info->free_space_cache, start,
2033 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2034 struct btrfs_key *key)
2037 struct btrfs_key found_key;
2038 struct extent_buffer *leaf;
2041 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2045 slot = path->slots[0];
2046 leaf = path->nodes[0];
2047 if (slot >= btrfs_header_nritems(leaf)) {
2048 ret = btrfs_next_leaf(root, path);
2055 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2057 if (found_key.objectid >= key->objectid &&
2058 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2067 int btrfs_read_block_groups(struct btrfs_root *root)
2069 struct btrfs_path *path;
2072 struct btrfs_block_group_cache *cache;
2073 struct btrfs_fs_info *info = root->fs_info;
2074 struct extent_io_tree *block_group_cache;
2075 struct btrfs_key key;
2076 struct btrfs_key found_key;
2077 struct extent_buffer *leaf;
2079 block_group_cache = &info->block_group_cache;
2081 root = info->extent_root;
2084 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2085 path = btrfs_alloc_path();
2090 ret = find_first_block_group(root, path, &key);
2098 leaf = path->nodes[0];
2099 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2100 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2106 read_extent_buffer(leaf, &cache->item,
2107 btrfs_item_ptr_offset(leaf, path->slots[0]),
2108 sizeof(cache->item));
2109 memcpy(&cache->key, &found_key, sizeof(found_key));
2112 key.objectid = found_key.objectid + found_key.offset;
2113 btrfs_release_path(root, path);
2114 cache->flags = btrfs_block_group_flags(&cache->item);
2116 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
2117 bit = BLOCK_GROUP_DATA;
2118 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2119 bit = BLOCK_GROUP_SYSTEM;
2120 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
2121 bit = BLOCK_GROUP_METADATA;
2124 /* use EXTENT_LOCKED to prevent merging */
2125 set_extent_bits(block_group_cache, found_key.objectid,
2126 found_key.objectid + found_key.offset - 1,
2127 bit | EXTENT_LOCKED, GFP_NOFS);
2128 set_state_private(block_group_cache, found_key.objectid,
2129 (unsigned long)cache);
2132 btrfs_super_total_bytes(&info->super_copy))
2137 btrfs_free_path(path);
2141 static int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
2142 struct btrfs_root *root,
2143 struct btrfs_key *key,
2144 struct btrfs_block_group_item *bi)
2148 struct btrfs_root *extent_root;
2150 extent_root = root->fs_info->extent_root;
2151 ret = btrfs_insert_item(trans, extent_root, key, bi, sizeof(*bi));
2152 finish_current_insert(trans, extent_root);
2153 pending_ret = del_pending_extents(trans, extent_root);
2161 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2162 struct btrfs_root *root, u64 bytes_used,
2163 u64 type, u64 chunk_tree, u64 chunk_objectid,
2168 struct btrfs_root *extent_root;
2169 struct btrfs_block_group_cache *cache;
2170 struct extent_io_tree *block_group_cache;
2172 extent_root = root->fs_info->extent_root;
2173 block_group_cache = &root->fs_info->block_group_cache;
2175 cache = malloc(sizeof(*cache));
2177 cache->key.objectid = chunk_objectid;
2178 cache->key.offset = size;
2180 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2181 memset(&cache->item, 0, sizeof(cache->item));
2182 btrfs_set_block_group_used(&cache->item, bytes_used);
2183 btrfs_set_block_group_chunk_tree(&cache->item, chunk_tree);
2184 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2185 cache->flags = type;
2186 btrfs_set_block_group_flags(&cache->item, type);
2188 if (type & BTRFS_BLOCK_GROUP_DATA) {
2189 bit = BLOCK_GROUP_DATA;
2190 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2191 bit = BLOCK_GROUP_SYSTEM;
2192 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2193 bit = BLOCK_GROUP_METADATA;
2195 set_extent_bits(block_group_cache, chunk_objectid,
2196 chunk_objectid + size - 1,
2197 bit | EXTENT_LOCKED, GFP_NOFS);
2198 set_state_private(block_group_cache, chunk_objectid,
2199 (unsigned long)cache);
2200 ret = btrfs_insert_block_group(trans, root, &cache->key, &cache->item);
2205 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
2206 u64 owner, u64 owner_offset)
2208 return hash_extent_ref(root_objectid, ref_generation,
2209 owner, owner_offset);
2212 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2213 struct btrfs_root *root,
2214 u64 bytenr, u64 num_bytes, int alloc,
2217 return update_block_group(trans, root, bytenr, num_bytes,