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.
23 #include "kerncompat.h"
24 #include "radix-tree.h"
27 #include "print-tree.h"
28 #include "transaction.h"
31 #include "free-space-cache.h"
34 #define PENDING_EXTENT_INSERT 0
35 #define PENDING_EXTENT_DELETE 1
36 #define PENDING_BACKREF_UPDATE 2
38 struct pending_extent_op {
43 struct btrfs_disk_key key;
47 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
48 struct btrfs_root *root,
49 u64 root_objectid, u64 generation,
50 u64 flags, struct btrfs_disk_key *key,
51 int level, struct btrfs_key *ins);
52 static int __free_extent(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root,
54 u64 bytenr, u64 num_bytes, u64 parent,
55 u64 root_objectid, u64 owner_objectid,
56 u64 owner_offset, int refs_to_drop);
57 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
58 btrfs_root *extent_root);
59 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
60 btrfs_root *extent_root);
61 static struct btrfs_block_group_cache *
62 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
63 *hint, u64 search_start, int data, int owner);
65 static int remove_sb_from_cache(struct btrfs_root *root,
66 struct btrfs_block_group_cache *cache)
72 struct extent_io_tree *free_space_cache;
74 free_space_cache = &root->fs_info->free_space_cache;
75 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
76 bytenr = btrfs_sb_offset(i);
77 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
78 cache->key.objectid, bytenr, 0,
79 &logical, &nr, &stripe_len);
82 clear_extent_dirty(free_space_cache, logical[nr],
83 logical[nr] + stripe_len - 1);
90 static int cache_block_group(struct btrfs_root *root,
91 struct btrfs_block_group_cache *block_group)
93 struct btrfs_path *path;
96 struct extent_buffer *leaf;
97 struct extent_io_tree *free_space_cache;
105 root = root->fs_info->extent_root;
106 free_space_cache = &root->fs_info->free_space_cache;
108 if (block_group->cached)
111 path = btrfs_alloc_path();
116 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
126 leaf = path->nodes[0];
127 slot = path->slots[0];
128 if (slot >= btrfs_header_nritems(leaf)) {
129 ret = btrfs_next_leaf(root, path);
138 btrfs_item_key_to_cpu(leaf, &key, slot);
139 if (key.objectid < block_group->key.objectid) {
142 if (key.objectid >= block_group->key.objectid +
143 block_group->key.offset) {
147 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
148 key.type == BTRFS_METADATA_ITEM_KEY) {
149 if (key.objectid > last) {
150 hole_size = key.objectid - last;
151 set_extent_dirty(free_space_cache, last,
152 last + hole_size - 1);
154 if (key.type == BTRFS_METADATA_ITEM_KEY)
155 last = key.objectid + root->nodesize;
157 last = key.objectid + key.offset;
163 if (block_group->key.objectid +
164 block_group->key.offset > last) {
165 hole_size = block_group->key.objectid +
166 block_group->key.offset - last;
167 set_extent_dirty(free_space_cache, last, last + hole_size - 1);
169 remove_sb_from_cache(root, block_group);
170 block_group->cached = 1;
172 btrfs_free_path(path);
176 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
180 struct extent_io_tree *block_group_cache;
181 struct btrfs_block_group_cache *block_group = NULL;
187 bytenr = max_t(u64, bytenr,
188 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
189 block_group_cache = &info->block_group_cache;
190 ret = find_first_extent_bit(block_group_cache,
191 bytenr, &start, &end,
192 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
197 ret = get_state_private(block_group_cache, start, &ptr);
201 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
205 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
209 struct extent_io_tree *block_group_cache;
210 struct btrfs_block_group_cache *block_group = NULL;
216 block_group_cache = &info->block_group_cache;
217 ret = find_first_extent_bit(block_group_cache,
218 bytenr, &start, &end,
219 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
224 ret = get_state_private(block_group_cache, start, &ptr);
228 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
229 if (block_group->key.objectid <= bytenr && bytenr <
230 block_group->key.objectid + block_group->key.offset)
235 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
237 return (cache->flags & bits) == bits;
240 static int noinline find_search_start(struct btrfs_root *root,
241 struct btrfs_block_group_cache **cache_ret,
242 u64 *start_ret, int num, int data)
245 struct btrfs_block_group_cache *cache = *cache_ret;
246 u64 last = *start_ret;
249 u64 search_start = *start_ret;
255 ret = cache_block_group(root, cache);
259 last = max(search_start, cache->key.objectid);
260 if (cache->ro || !block_group_bits(cache, data))
264 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
265 last, &start, &end, EXTENT_DIRTY);
270 start = max(last, start);
272 if (last - start < num) {
275 if (start + num > cache->key.objectid + cache->key.offset) {
283 cache = btrfs_lookup_block_group(root->fs_info, search_start);
285 printk("Unable to find block group for %llu\n",
286 (unsigned long long)search_start);
292 last = cache->key.objectid + cache->key.offset;
294 cache = btrfs_lookup_first_block_group(root->fs_info, last);
307 static int block_group_state_bits(u64 flags)
310 if (flags & BTRFS_BLOCK_GROUP_DATA)
311 bits |= BLOCK_GROUP_DATA;
312 if (flags & BTRFS_BLOCK_GROUP_METADATA)
313 bits |= BLOCK_GROUP_METADATA;
314 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
315 bits |= BLOCK_GROUP_SYSTEM;
319 static struct btrfs_block_group_cache *
320 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
321 *hint, u64 search_start, int data, int owner)
323 struct btrfs_block_group_cache *cache;
324 struct extent_io_tree *block_group_cache;
325 struct btrfs_block_group_cache *found_group = NULL;
326 struct btrfs_fs_info *info = root->fs_info;
339 block_group_cache = &info->block_group_cache;
344 bit = block_group_state_bits(data);
347 struct btrfs_block_group_cache *shint;
348 shint = btrfs_lookup_block_group(info, search_start);
349 if (shint && !shint->ro && block_group_bits(shint, data)) {
350 used = btrfs_block_group_used(&shint->item);
351 if (used + shint->pinned <
352 div_factor(shint->key.offset, factor)) {
357 if (hint && !hint->ro && block_group_bits(hint, data)) {
358 used = btrfs_block_group_used(&hint->item);
359 if (used + hint->pinned <
360 div_factor(hint->key.offset, factor)) {
363 last = hint->key.objectid + hint->key.offset;
367 hint_last = max(hint->key.objectid, search_start);
369 hint_last = search_start;
375 ret = find_first_extent_bit(block_group_cache, last,
380 ret = get_state_private(block_group_cache, start, &ptr);
384 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
385 last = cache->key.objectid + cache->key.offset;
386 used = btrfs_block_group_used(&cache->item);
388 if (!cache->ro && block_group_bits(cache, data)) {
390 free_check = cache->key.offset;
392 free_check = div_factor(cache->key.offset,
395 if (used + cache->pinned < free_check) {
412 * Back reference rules. Back refs have three main goals:
414 * 1) differentiate between all holders of references to an extent so that
415 * when a reference is dropped we can make sure it was a valid reference
416 * before freeing the extent.
418 * 2) Provide enough information to quickly find the holders of an extent
419 * if we notice a given block is corrupted or bad.
421 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
422 * maintenance. This is actually the same as #2, but with a slightly
423 * different use case.
425 * There are two kinds of back refs. The implicit back refs is optimized
426 * for pointers in non-shared tree blocks. For a given pointer in a block,
427 * back refs of this kind provide information about the block's owner tree
428 * and the pointer's key. These information allow us to find the block by
429 * b-tree searching. The full back refs is for pointers in tree blocks not
430 * referenced by their owner trees. The location of tree block is recorded
431 * in the back refs. Actually the full back refs is generic, and can be
432 * used in all cases the implicit back refs is used. The major shortcoming
433 * of the full back refs is its overhead. Every time a tree block gets
434 * COWed, we have to update back refs entry for all pointers in it.
436 * For a newly allocated tree block, we use implicit back refs for
437 * pointers in it. This means most tree related operations only involve
438 * implicit back refs. For a tree block created in old transaction, the
439 * only way to drop a reference to it is COW it. So we can detect the
440 * event that tree block loses its owner tree's reference and do the
441 * back refs conversion.
443 * When a tree block is COW'd through a tree, there are four cases:
445 * The reference count of the block is one and the tree is the block's
446 * owner tree. Nothing to do in this case.
448 * The reference count of the block is one and the tree is not the
449 * block's owner tree. In this case, full back refs is used for pointers
450 * in the block. Remove these full back refs, add implicit back refs for
451 * every pointers in the new block.
453 * The reference count of the block is greater than one and the tree is
454 * the block's owner tree. In this case, implicit back refs is used for
455 * pointers in the block. Add full back refs for every pointers in the
456 * block, increase lower level extents' reference counts. The original
457 * implicit back refs are entailed to the new block.
459 * The reference count of the block is greater than one and the tree is
460 * not the block's owner tree. Add implicit back refs for every pointer in
461 * the new block, increase lower level extents' reference count.
463 * Back Reference Key composing:
465 * The key objectid corresponds to the first byte in the extent,
466 * The key type is used to differentiate between types of back refs.
467 * There are different meanings of the key offset for different types
470 * File extents can be referenced by:
472 * - multiple snapshots, subvolumes, or different generations in one subvol
473 * - different files inside a single subvolume
474 * - different offsets inside a file (bookend extents in file.c)
476 * The extent ref structure for the implicit back refs has fields for:
478 * - Objectid of the subvolume root
479 * - objectid of the file holding the reference
480 * - original offset in the file
481 * - how many bookend extents
483 * The key offset for the implicit back refs is hash of the first
486 * The extent ref structure for the full back refs has field for:
488 * - number of pointers in the tree leaf
490 * The key offset for the implicit back refs is the first byte of
493 * When a file extent is allocated, The implicit back refs is used.
494 * the fields are filled in:
496 * (root_key.objectid, inode objectid, offset in file, 1)
498 * When a file extent is removed file truncation, we find the
499 * corresponding implicit back refs and check the following fields:
501 * (btrfs_header_owner(leaf), inode objectid, offset in file)
503 * Btree extents can be referenced by:
505 * - Different subvolumes
507 * Both the implicit back refs and the full back refs for tree blocks
508 * only consist of key. The key offset for the implicit back refs is
509 * objectid of block's owner tree. The key offset for the full back refs
510 * is the first byte of parent block.
512 * When implicit back refs is used, information about the lowest key and
513 * level of the tree block are required. These information are stored in
514 * tree block info structure.
517 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
518 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
519 struct btrfs_root *root,
520 struct btrfs_path *path,
521 u64 owner, u32 extra_size)
523 struct btrfs_extent_item *item;
524 struct btrfs_extent_item_v0 *ei0;
525 struct btrfs_extent_ref_v0 *ref0;
526 struct btrfs_tree_block_info *bi;
527 struct extent_buffer *leaf;
528 struct btrfs_key key;
529 struct btrfs_key found_key;
530 u32 new_size = sizeof(*item);
534 leaf = path->nodes[0];
535 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
537 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
538 ei0 = btrfs_item_ptr(leaf, path->slots[0],
539 struct btrfs_extent_item_v0);
540 refs = btrfs_extent_refs_v0(leaf, ei0);
542 if (owner == (u64)-1) {
544 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
545 ret = btrfs_next_leaf(root, path);
549 leaf = path->nodes[0];
551 btrfs_item_key_to_cpu(leaf, &found_key,
553 BUG_ON(key.objectid != found_key.objectid);
554 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
558 ref0 = btrfs_item_ptr(leaf, path->slots[0],
559 struct btrfs_extent_ref_v0);
560 owner = btrfs_ref_objectid_v0(leaf, ref0);
564 btrfs_release_path(path);
566 if (owner < BTRFS_FIRST_FREE_OBJECTID)
567 new_size += sizeof(*bi);
569 new_size -= sizeof(*ei0);
570 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
575 ret = btrfs_extend_item(root, path, new_size);
578 leaf = path->nodes[0];
579 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
580 btrfs_set_extent_refs(leaf, item, refs);
581 /* FIXME: get real generation */
582 btrfs_set_extent_generation(leaf, item, 0);
583 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
584 btrfs_set_extent_flags(leaf, item,
585 BTRFS_EXTENT_FLAG_TREE_BLOCK |
586 BTRFS_BLOCK_FLAG_FULL_BACKREF);
587 bi = (struct btrfs_tree_block_info *)(item + 1);
588 /* FIXME: get first key of the block */
589 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
590 btrfs_set_tree_block_level(leaf, bi, (int)owner);
592 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
594 btrfs_mark_buffer_dirty(leaf);
599 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
601 u32 high_crc = ~(u32)0;
602 u32 low_crc = ~(u32)0;
605 lenum = cpu_to_le64(root_objectid);
606 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
607 lenum = cpu_to_le64(owner);
608 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
609 lenum = cpu_to_le64(offset);
610 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
612 return ((u64)high_crc << 31) ^ (u64)low_crc;
615 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
616 struct btrfs_extent_data_ref *ref)
618 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
619 btrfs_extent_data_ref_objectid(leaf, ref),
620 btrfs_extent_data_ref_offset(leaf, ref));
623 static int match_extent_data_ref(struct extent_buffer *leaf,
624 struct btrfs_extent_data_ref *ref,
625 u64 root_objectid, u64 owner, u64 offset)
627 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
628 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
629 btrfs_extent_data_ref_offset(leaf, ref) != offset)
634 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
635 struct btrfs_root *root,
636 struct btrfs_path *path,
637 u64 bytenr, u64 parent,
639 u64 owner, u64 offset)
641 struct btrfs_key key;
642 struct btrfs_extent_data_ref *ref;
643 struct extent_buffer *leaf;
649 key.objectid = bytenr;
651 key.type = BTRFS_SHARED_DATA_REF_KEY;
654 key.type = BTRFS_EXTENT_DATA_REF_KEY;
655 key.offset = hash_extent_data_ref(root_objectid,
660 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
669 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
670 key.type = BTRFS_EXTENT_REF_V0_KEY;
671 btrfs_release_path(path);
672 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
683 leaf = path->nodes[0];
684 nritems = btrfs_header_nritems(leaf);
686 if (path->slots[0] >= nritems) {
687 ret = btrfs_next_leaf(root, path);
693 leaf = path->nodes[0];
694 nritems = btrfs_header_nritems(leaf);
698 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
699 if (key.objectid != bytenr ||
700 key.type != BTRFS_EXTENT_DATA_REF_KEY)
703 ref = btrfs_item_ptr(leaf, path->slots[0],
704 struct btrfs_extent_data_ref);
706 if (match_extent_data_ref(leaf, ref, root_objectid,
709 btrfs_release_path(path);
721 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root,
723 struct btrfs_path *path,
724 u64 bytenr, u64 parent,
725 u64 root_objectid, u64 owner,
726 u64 offset, int refs_to_add)
728 struct btrfs_key key;
729 struct extent_buffer *leaf;
734 key.objectid = bytenr;
736 key.type = BTRFS_SHARED_DATA_REF_KEY;
738 size = sizeof(struct btrfs_shared_data_ref);
740 key.type = BTRFS_EXTENT_DATA_REF_KEY;
741 key.offset = hash_extent_data_ref(root_objectid,
743 size = sizeof(struct btrfs_extent_data_ref);
746 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
747 if (ret && ret != -EEXIST)
750 leaf = path->nodes[0];
752 struct btrfs_shared_data_ref *ref;
753 ref = btrfs_item_ptr(leaf, path->slots[0],
754 struct btrfs_shared_data_ref);
756 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
758 num_refs = btrfs_shared_data_ref_count(leaf, ref);
759 num_refs += refs_to_add;
760 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
763 struct btrfs_extent_data_ref *ref;
764 while (ret == -EEXIST) {
765 ref = btrfs_item_ptr(leaf, path->slots[0],
766 struct btrfs_extent_data_ref);
767 if (match_extent_data_ref(leaf, ref, root_objectid,
770 btrfs_release_path(path);
773 ret = btrfs_insert_empty_item(trans, root, path, &key,
775 if (ret && ret != -EEXIST)
778 leaf = path->nodes[0];
780 ref = btrfs_item_ptr(leaf, path->slots[0],
781 struct btrfs_extent_data_ref);
783 btrfs_set_extent_data_ref_root(leaf, ref,
785 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
786 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
787 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
789 num_refs = btrfs_extent_data_ref_count(leaf, ref);
790 num_refs += refs_to_add;
791 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
794 btrfs_mark_buffer_dirty(leaf);
797 btrfs_release_path(path);
801 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
802 struct btrfs_root *root,
803 struct btrfs_path *path,
806 struct btrfs_key key;
807 struct btrfs_extent_data_ref *ref1 = NULL;
808 struct btrfs_shared_data_ref *ref2 = NULL;
809 struct extent_buffer *leaf;
813 leaf = path->nodes[0];
814 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
816 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
817 ref1 = btrfs_item_ptr(leaf, path->slots[0],
818 struct btrfs_extent_data_ref);
819 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
820 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
821 ref2 = btrfs_item_ptr(leaf, path->slots[0],
822 struct btrfs_shared_data_ref);
823 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
824 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
825 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
826 struct btrfs_extent_ref_v0 *ref0;
827 ref0 = btrfs_item_ptr(leaf, path->slots[0],
828 struct btrfs_extent_ref_v0);
829 num_refs = btrfs_ref_count_v0(leaf, ref0);
835 BUG_ON(num_refs < refs_to_drop);
836 num_refs -= refs_to_drop;
839 ret = btrfs_del_item(trans, root, path);
841 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
842 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
843 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
844 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
845 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
847 struct btrfs_extent_ref_v0 *ref0;
848 ref0 = btrfs_item_ptr(leaf, path->slots[0],
849 struct btrfs_extent_ref_v0);
850 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
853 btrfs_mark_buffer_dirty(leaf);
858 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
859 struct btrfs_extent_inline_ref *iref)
861 struct btrfs_key key;
862 struct extent_buffer *leaf;
863 struct btrfs_extent_data_ref *ref1;
864 struct btrfs_shared_data_ref *ref2;
867 leaf = path->nodes[0];
868 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
870 if (btrfs_extent_inline_ref_type(leaf, iref) ==
871 BTRFS_EXTENT_DATA_REF_KEY) {
872 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
873 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
875 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
876 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
878 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
879 ref1 = btrfs_item_ptr(leaf, path->slots[0],
880 struct btrfs_extent_data_ref);
881 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
882 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
883 ref2 = btrfs_item_ptr(leaf, path->slots[0],
884 struct btrfs_shared_data_ref);
885 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
886 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
887 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
888 struct btrfs_extent_ref_v0 *ref0;
889 ref0 = btrfs_item_ptr(leaf, path->slots[0],
890 struct btrfs_extent_ref_v0);
891 num_refs = btrfs_ref_count_v0(leaf, ref0);
899 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 struct btrfs_path *path,
902 u64 bytenr, u64 parent,
905 struct btrfs_key key;
908 key.objectid = bytenr;
910 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
913 key.type = BTRFS_TREE_BLOCK_REF_KEY;
914 key.offset = root_objectid;
917 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
920 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
921 if (ret == -ENOENT && parent) {
922 btrfs_release_path(path);
923 key.type = BTRFS_EXTENT_REF_V0_KEY;
924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
932 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct btrfs_path *path,
935 u64 bytenr, u64 parent,
938 struct btrfs_key key;
941 key.objectid = bytenr;
943 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
946 key.type = BTRFS_TREE_BLOCK_REF_KEY;
947 key.offset = root_objectid;
950 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
952 btrfs_release_path(path);
956 static inline int extent_ref_type(u64 parent, u64 owner)
959 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
961 type = BTRFS_SHARED_BLOCK_REF_KEY;
963 type = BTRFS_TREE_BLOCK_REF_KEY;
966 type = BTRFS_SHARED_DATA_REF_KEY;
968 type = BTRFS_EXTENT_DATA_REF_KEY;
973 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
974 struct btrfs_root *root,
975 struct btrfs_path *path,
976 struct btrfs_extent_inline_ref **ref_ret,
977 u64 bytenr, u64 num_bytes,
978 u64 parent, u64 root_objectid,
979 u64 owner, u64 offset, int insert)
981 struct btrfs_key key;
982 struct extent_buffer *leaf;
983 struct btrfs_extent_item *ei;
984 struct btrfs_extent_inline_ref *iref;
994 int skinny_metadata =
995 btrfs_fs_incompat(root->fs_info, SKINNY_METADATA);
997 key.objectid = bytenr;
998 key.type = BTRFS_EXTENT_ITEM_KEY;
999 key.offset = num_bytes;
1001 want = extent_ref_type(parent, owner);
1003 extra_size = btrfs_extent_inline_ref_size(want);
1007 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1008 skinny_metadata = 1;
1009 key.type = BTRFS_METADATA_ITEM_KEY;
1011 } else if (skinny_metadata) {
1012 skinny_metadata = 0;
1016 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1023 * We may be a newly converted file system which still has the old fat
1024 * extent entries for metadata, so try and see if we have one of those.
1026 if (ret > 0 && skinny_metadata) {
1027 skinny_metadata = 0;
1028 if (path->slots[0]) {
1030 btrfs_item_key_to_cpu(path->nodes[0], &key,
1032 if (key.objectid == bytenr &&
1033 key.type == BTRFS_EXTENT_ITEM_KEY &&
1034 key.offset == num_bytes)
1038 key.type = BTRFS_EXTENT_ITEM_KEY;
1039 key.offset = num_bytes;
1040 btrfs_release_path(path);
1046 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1052 leaf = path->nodes[0];
1053 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1054 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1055 if (item_size < sizeof(*ei)) {
1060 ret = convert_extent_item_v0(trans, root, path, owner,
1066 leaf = path->nodes[0];
1067 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1070 if (item_size < sizeof(*ei)) {
1071 printf("Size is %u, needs to be %u, slot %d\n",
1072 (unsigned)item_size,
1073 (unsigned)sizeof(*ei), path->slots[0]);
1074 btrfs_print_leaf(root, leaf);
1077 BUG_ON(item_size < sizeof(*ei));
1079 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1080 flags = btrfs_extent_flags(leaf, ei);
1082 ptr = (unsigned long)(ei + 1);
1083 end = (unsigned long)ei + item_size;
1085 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1086 ptr += sizeof(struct btrfs_tree_block_info);
1088 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1089 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1100 iref = (struct btrfs_extent_inline_ref *)ptr;
1101 type = btrfs_extent_inline_ref_type(leaf, iref);
1105 ptr += btrfs_extent_inline_ref_size(type);
1109 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1110 struct btrfs_extent_data_ref *dref;
1111 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1112 if (match_extent_data_ref(leaf, dref, root_objectid,
1117 if (hash_extent_data_ref_item(leaf, dref) <
1118 hash_extent_data_ref(root_objectid, owner, offset))
1122 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1124 if (parent == ref_offset) {
1128 if (ref_offset < parent)
1131 if (root_objectid == ref_offset) {
1135 if (ref_offset < root_objectid)
1139 ptr += btrfs_extent_inline_ref_size(type);
1141 if (err == -ENOENT && insert) {
1142 if (item_size + extra_size >=
1143 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1148 * To add new inline back ref, we have to make sure
1149 * there is no corresponding back ref item.
1150 * For simplicity, we just do not add new inline back
1151 * ref if there is any back ref item.
1153 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1154 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1159 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1164 static int setup_inline_extent_backref(struct btrfs_root *root,
1165 struct btrfs_path *path,
1166 struct btrfs_extent_inline_ref *iref,
1167 u64 parent, u64 root_objectid,
1168 u64 owner, u64 offset, int refs_to_add)
1170 struct extent_buffer *leaf;
1171 struct btrfs_extent_item *ei;
1174 unsigned long item_offset;
1180 leaf = path->nodes[0];
1181 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1182 item_offset = (unsigned long)iref - (unsigned long)ei;
1184 type = extent_ref_type(parent, owner);
1185 size = btrfs_extent_inline_ref_size(type);
1187 ret = btrfs_extend_item(root, path, size);
1190 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1191 refs = btrfs_extent_refs(leaf, ei);
1192 refs += refs_to_add;
1193 btrfs_set_extent_refs(leaf, ei, refs);
1195 ptr = (unsigned long)ei + item_offset;
1196 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1197 if (ptr < end - size)
1198 memmove_extent_buffer(leaf, ptr + size, ptr,
1201 iref = (struct btrfs_extent_inline_ref *)ptr;
1202 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1203 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1204 struct btrfs_extent_data_ref *dref;
1205 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1206 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1207 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1208 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1209 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1210 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1211 struct btrfs_shared_data_ref *sref;
1212 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1213 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1214 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1215 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1216 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1218 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1220 btrfs_mark_buffer_dirty(leaf);
1224 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1225 struct btrfs_root *root,
1226 struct btrfs_path *path,
1227 struct btrfs_extent_inline_ref **ref_ret,
1228 u64 bytenr, u64 num_bytes, u64 parent,
1229 u64 root_objectid, u64 owner, u64 offset)
1233 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1234 bytenr, num_bytes, parent,
1235 root_objectid, owner, offset, 0);
1239 btrfs_release_path(path);
1242 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1243 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1246 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1247 root_objectid, owner, offset);
1252 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1253 struct btrfs_root *root,
1254 struct btrfs_path *path,
1255 struct btrfs_extent_inline_ref *iref,
1258 struct extent_buffer *leaf;
1259 struct btrfs_extent_item *ei;
1260 struct btrfs_extent_data_ref *dref = NULL;
1261 struct btrfs_shared_data_ref *sref = NULL;
1270 leaf = path->nodes[0];
1271 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1272 refs = btrfs_extent_refs(leaf, ei);
1273 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1274 refs += refs_to_mod;
1275 btrfs_set_extent_refs(leaf, ei, refs);
1277 type = btrfs_extent_inline_ref_type(leaf, iref);
1279 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1280 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1281 refs = btrfs_extent_data_ref_count(leaf, dref);
1282 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1283 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1284 refs = btrfs_shared_data_ref_count(leaf, sref);
1287 BUG_ON(refs_to_mod != -1);
1290 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1291 refs += refs_to_mod;
1294 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1295 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1297 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1299 size = btrfs_extent_inline_ref_size(type);
1300 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1301 ptr = (unsigned long)iref;
1302 end = (unsigned long)ei + item_size;
1303 if (ptr + size < end)
1304 memmove_extent_buffer(leaf, ptr, ptr + size,
1307 ret = btrfs_truncate_item(root, path, item_size, 1);
1310 btrfs_mark_buffer_dirty(leaf);
1314 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1315 struct btrfs_root *root,
1316 struct btrfs_path *path,
1317 u64 bytenr, u64 num_bytes, u64 parent,
1318 u64 root_objectid, u64 owner,
1319 u64 offset, int refs_to_add)
1321 struct btrfs_extent_inline_ref *iref;
1324 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1325 bytenr, num_bytes, parent,
1326 root_objectid, owner, offset, 1);
1328 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1329 ret = update_inline_extent_backref(trans, root, path, iref,
1331 } else if (ret == -ENOENT) {
1332 ret = setup_inline_extent_backref(root, path, iref,
1333 parent, root_objectid,
1334 owner, offset, refs_to_add);
1339 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root,
1341 struct btrfs_path *path,
1342 u64 bytenr, u64 parent, u64 root_objectid,
1343 u64 owner, u64 offset, int refs_to_add)
1347 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1348 ret = insert_extent_data_ref(trans, root, path, bytenr,
1349 parent, root_objectid,
1350 owner, offset, refs_to_add);
1352 BUG_ON(refs_to_add != 1);
1353 ret = insert_tree_block_ref(trans, root, path, bytenr,
1354 parent, root_objectid);
1359 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1360 struct btrfs_root *root,
1361 struct btrfs_path *path,
1362 struct btrfs_extent_inline_ref *iref,
1363 int refs_to_drop, int is_data)
1367 BUG_ON(!is_data && refs_to_drop != 1);
1369 ret = update_inline_extent_backref(trans, root, path, iref,
1371 } else if (is_data) {
1372 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1374 ret = btrfs_del_item(trans, root, path);
1379 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1380 struct btrfs_root *root,
1381 u64 bytenr, u64 num_bytes, u64 parent,
1382 u64 root_objectid, u64 owner, u64 offset)
1384 struct btrfs_path *path;
1385 struct extent_buffer *leaf;
1386 struct btrfs_extent_item *item;
1391 path = btrfs_alloc_path();
1397 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1398 path, bytenr, num_bytes, parent,
1399 root_objectid, owner, offset, 1);
1403 if (ret != -EAGAIN) {
1408 leaf = path->nodes[0];
1409 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1410 refs = btrfs_extent_refs(leaf, item);
1411 btrfs_set_extent_refs(leaf, item, refs + 1);
1413 btrfs_mark_buffer_dirty(leaf);
1414 btrfs_release_path(path);
1418 /* now insert the actual backref */
1419 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1420 path, bytenr, parent, root_objectid,
1425 btrfs_free_path(path);
1426 finish_current_insert(trans, root->fs_info->extent_root);
1427 del_pending_extents(trans, root->fs_info->extent_root);
1432 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root)
1435 finish_current_insert(trans, root->fs_info->extent_root);
1436 del_pending_extents(trans, root->fs_info->extent_root);
1440 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1441 struct btrfs_root *root, u64 bytenr,
1442 u64 offset, int metadata, u64 *refs, u64 *flags)
1444 struct btrfs_path *path;
1446 struct btrfs_key key;
1447 struct extent_buffer *l;
1448 struct btrfs_extent_item *item;
1454 !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
1455 offset = root->nodesize;
1459 path = btrfs_alloc_path();
1464 key.objectid = bytenr;
1465 key.offset = offset;
1467 key.type = BTRFS_METADATA_ITEM_KEY;
1469 key.type = BTRFS_EXTENT_ITEM_KEY;
1472 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1478 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1479 * we didn't find what we wanted check and see if we have a normal ref
1480 * right next to us, or re-search if we are on the edge of the leaf just
1483 if (ret > 0 && metadata) {
1484 if (path->slots[0]) {
1486 btrfs_item_key_to_cpu(path->nodes[0], &key,
1488 if (key.objectid == bytenr &&
1489 key.type == BTRFS_EXTENT_ITEM_KEY &&
1490 key.offset == root->nodesize)
1495 btrfs_release_path(path);
1496 key.type = BTRFS_EXTENT_ITEM_KEY;
1497 key.offset = root->nodesize;
1509 item_size = btrfs_item_size_nr(l, path->slots[0]);
1510 if (item_size >= sizeof(*item)) {
1511 item = btrfs_item_ptr(l, path->slots[0],
1512 struct btrfs_extent_item);
1513 num_refs = btrfs_extent_refs(l, item);
1514 extent_flags = btrfs_extent_flags(l, item);
1516 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1517 struct btrfs_extent_item_v0 *ei0;
1518 BUG_ON(item_size != sizeof(*ei0));
1519 ei0 = btrfs_item_ptr(l, path->slots[0],
1520 struct btrfs_extent_item_v0);
1521 num_refs = btrfs_extent_refs_v0(l, ei0);
1522 /* FIXME: this isn't correct for data */
1523 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1528 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1532 *flags = extent_flags;
1534 btrfs_free_path(path);
1538 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1539 struct btrfs_root *root,
1540 u64 bytenr, int level, u64 flags)
1542 struct btrfs_path *path;
1544 struct btrfs_key key;
1545 struct extent_buffer *l;
1546 struct btrfs_extent_item *item;
1548 int skinny_metadata =
1549 btrfs_fs_incompat(root->fs_info, SKINNY_METADATA);
1551 path = btrfs_alloc_path();
1556 key.objectid = bytenr;
1557 if (skinny_metadata) {
1559 key.type = BTRFS_METADATA_ITEM_KEY;
1561 key.offset = root->nodesize;
1562 key.type = BTRFS_EXTENT_ITEM_KEY;
1566 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1571 if (ret > 0 && skinny_metadata) {
1572 skinny_metadata = 0;
1573 if (path->slots[0]) {
1575 btrfs_item_key_to_cpu(path->nodes[0], &key,
1577 if (key.objectid == bytenr &&
1578 key.offset == root->nodesize &&
1579 key.type == BTRFS_EXTENT_ITEM_KEY)
1583 btrfs_release_path(path);
1584 key.offset = root->nodesize;
1585 key.type = BTRFS_EXTENT_ITEM_KEY;
1591 btrfs_print_leaf(root, path->nodes[0]);
1592 printk("failed to find block number %Lu\n",
1593 (unsigned long long)bytenr);
1597 item_size = btrfs_item_size_nr(l, path->slots[0]);
1598 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1599 if (item_size < sizeof(*item)) {
1600 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1606 item_size = btrfs_item_size_nr(l, path->slots[0]);
1609 BUG_ON(item_size < sizeof(*item));
1610 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1611 flags |= btrfs_extent_flags(l, item);
1612 btrfs_set_extent_flags(l, item, flags);
1614 btrfs_free_path(path);
1615 finish_current_insert(trans, root->fs_info->extent_root);
1616 del_pending_extents(trans, root->fs_info->extent_root);
1620 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1621 struct btrfs_root *root,
1622 struct extent_buffer *buf,
1623 int record_parent, int inc)
1630 struct btrfs_key key;
1631 struct btrfs_file_extent_item *fi;
1635 int (*process_func)(struct btrfs_trans_handle *trans,
1636 struct btrfs_root *root,
1637 u64, u64, u64, u64, u64, u64);
1639 ref_root = btrfs_header_owner(buf);
1640 nritems = btrfs_header_nritems(buf);
1641 level = btrfs_header_level(buf);
1643 if (!root->ref_cows && level == 0)
1647 process_func = btrfs_inc_extent_ref;
1649 process_func = btrfs_free_extent;
1652 parent = buf->start;
1656 for (i = 0; i < nritems; i++) {
1659 btrfs_item_key_to_cpu(buf, &key, i);
1660 if (key.type != BTRFS_EXTENT_DATA_KEY)
1662 fi = btrfs_item_ptr(buf, i,
1663 struct btrfs_file_extent_item);
1664 if (btrfs_file_extent_type(buf, fi) ==
1665 BTRFS_FILE_EXTENT_INLINE)
1667 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1671 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1672 key.offset -= btrfs_file_extent_offset(buf, fi);
1673 ret = process_func(trans, root, bytenr, num_bytes,
1674 parent, ref_root, key.objectid,
1681 bytenr = btrfs_node_blockptr(buf, i);
1682 num_bytes = root->nodesize;
1683 ret = process_func(trans, root, bytenr, num_bytes,
1684 parent, ref_root, level - 1, 0);
1697 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1698 struct extent_buffer *buf, int record_parent)
1700 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1703 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1704 struct extent_buffer *buf, int record_parent)
1706 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1709 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root,
1711 struct btrfs_path *path,
1712 struct btrfs_block_group_cache *cache)
1716 struct btrfs_root *extent_root = root->fs_info->extent_root;
1718 struct extent_buffer *leaf;
1720 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1725 leaf = path->nodes[0];
1726 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1727 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1728 btrfs_mark_buffer_dirty(leaf);
1729 btrfs_release_path(path);
1731 finish_current_insert(trans, extent_root);
1732 pending_ret = del_pending_extents(trans, extent_root);
1741 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root)
1744 struct extent_io_tree *block_group_cache;
1745 struct btrfs_block_group_cache *cache;
1747 struct btrfs_path *path;
1753 block_group_cache = &root->fs_info->block_group_cache;
1754 path = btrfs_alloc_path();
1759 ret = find_first_extent_bit(block_group_cache, last,
1760 &start, &end, BLOCK_GROUP_DIRTY);
1769 ret = get_state_private(block_group_cache, start, &ptr);
1772 clear_extent_bits(block_group_cache, start, end,
1775 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1776 ret = write_one_cache_group(trans, root, path, cache);
1778 btrfs_free_path(path);
1782 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1785 struct btrfs_space_info *found;
1787 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
1789 list_for_each_entry(found, &info->space_info, list) {
1790 if (found->flags & flags)
1797 static int free_space_info(struct btrfs_fs_info *fs_info, u64 flags,
1798 u64 total_bytes, u64 bytes_used,
1799 struct btrfs_space_info **space_info)
1801 struct btrfs_space_info *found;
1803 /* only support free block group which is empty */
1807 found = __find_space_info(fs_info, flags);
1810 if (found->total_bytes < total_bytes) {
1812 "WARNING: bad space info to free %llu only have %llu\n",
1813 total_bytes, found->total_bytes);
1816 found->total_bytes -= total_bytes;
1818 *space_info = found;
1822 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1823 u64 total_bytes, u64 bytes_used,
1824 struct btrfs_space_info **space_info)
1826 struct btrfs_space_info *found;
1828 found = __find_space_info(info, flags);
1830 found->total_bytes += total_bytes;
1831 found->bytes_used += bytes_used;
1832 if (found->total_bytes < found->bytes_used) {
1833 fprintf(stderr, "warning, bad space info total_bytes "
1835 (unsigned long long)found->total_bytes,
1836 (unsigned long long)found->bytes_used);
1838 *space_info = found;
1841 found = kmalloc(sizeof(*found), GFP_NOFS);
1845 list_add(&found->list, &info->space_info);
1846 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
1847 found->total_bytes = total_bytes;
1848 found->bytes_used = bytes_used;
1849 found->bytes_pinned = 0;
1851 *space_info = found;
1856 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1858 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1859 BTRFS_BLOCK_GROUP_RAID1 |
1860 BTRFS_BLOCK_GROUP_RAID10 |
1861 BTRFS_BLOCK_GROUP_RAID5 |
1862 BTRFS_BLOCK_GROUP_RAID6 |
1863 BTRFS_BLOCK_GROUP_DUP);
1865 if (flags & BTRFS_BLOCK_GROUP_DATA)
1866 fs_info->avail_data_alloc_bits |= extra_flags;
1867 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1868 fs_info->avail_metadata_alloc_bits |= extra_flags;
1869 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1870 fs_info->avail_system_alloc_bits |= extra_flags;
1874 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1875 struct btrfs_root *extent_root, u64 alloc_bytes,
1878 struct btrfs_space_info *space_info;
1884 space_info = __find_space_info(extent_root->fs_info, flags);
1886 ret = update_space_info(extent_root->fs_info, flags,
1890 BUG_ON(!space_info);
1892 if (space_info->full)
1895 thresh = div_factor(space_info->total_bytes, 7);
1896 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1901 * Avoid allocating given chunk type
1903 if (extent_root->fs_info->avoid_meta_chunk_alloc &&
1904 (flags & BTRFS_BLOCK_GROUP_METADATA))
1906 if (extent_root->fs_info->avoid_sys_chunk_alloc &&
1907 (flags & BTRFS_BLOCK_GROUP_SYSTEM))
1910 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1912 if (ret == -ENOSPC) {
1913 space_info->full = 1;
1919 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1920 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1925 static int update_block_group(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root,
1927 u64 bytenr, u64 num_bytes, int alloc,
1930 struct btrfs_block_group_cache *cache;
1931 struct btrfs_fs_info *info = root->fs_info;
1932 u64 total = num_bytes;
1938 /* block accounting for super block */
1939 old_val = btrfs_super_bytes_used(info->super_copy);
1941 old_val += num_bytes;
1943 old_val -= num_bytes;
1944 btrfs_set_super_bytes_used(info->super_copy, old_val);
1946 /* block accounting for root item */
1947 old_val = btrfs_root_used(&root->root_item);
1949 old_val += num_bytes;
1951 old_val -= num_bytes;
1952 btrfs_set_root_used(&root->root_item, old_val);
1955 cache = btrfs_lookup_block_group(info, bytenr);
1959 byte_in_group = bytenr - cache->key.objectid;
1960 WARN_ON(byte_in_group > cache->key.offset);
1961 start = cache->key.objectid;
1962 end = start + cache->key.offset - 1;
1963 set_extent_bits(&info->block_group_cache, start, end,
1966 old_val = btrfs_block_group_used(&cache->item);
1967 num_bytes = min(total, cache->key.offset - byte_in_group);
1970 old_val += num_bytes;
1971 cache->space_info->bytes_used += num_bytes;
1973 old_val -= num_bytes;
1974 cache->space_info->bytes_used -= num_bytes;
1976 set_extent_dirty(&info->free_space_cache,
1977 bytenr, bytenr + num_bytes - 1);
1980 btrfs_set_block_group_used(&cache->item, old_val);
1982 bytenr += num_bytes;
1987 static int update_pinned_extents(struct btrfs_root *root,
1988 u64 bytenr, u64 num, int pin)
1991 struct btrfs_block_group_cache *cache;
1992 struct btrfs_fs_info *fs_info = root->fs_info;
1995 set_extent_dirty(&fs_info->pinned_extents,
1996 bytenr, bytenr + num - 1);
1998 clear_extent_dirty(&fs_info->pinned_extents,
1999 bytenr, bytenr + num - 1);
2002 cache = btrfs_lookup_block_group(fs_info, bytenr);
2004 len = min((u64)root->sectorsize, num);
2008 len = min(num, cache->key.offset -
2009 (bytenr - cache->key.objectid));
2011 cache->pinned += len;
2012 cache->space_info->bytes_pinned += len;
2013 fs_info->total_pinned += len;
2015 cache->pinned -= len;
2016 cache->space_info->bytes_pinned -= len;
2017 fs_info->total_pinned -= len;
2026 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2027 struct btrfs_root *root,
2028 struct extent_io_tree *unpin)
2033 struct extent_io_tree *free_space_cache;
2034 free_space_cache = &root->fs_info->free_space_cache;
2037 ret = find_first_extent_bit(unpin, 0, &start, &end,
2041 update_pinned_extents(root, start, end + 1 - start, 0);
2042 clear_extent_dirty(unpin, start, end);
2043 set_extent_dirty(free_space_cache, start, end);
2048 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2054 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2055 &end, EXTENT_LOCKED);
2057 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2063 static int finish_current_insert(struct btrfs_trans_handle *trans,
2064 struct btrfs_root *extent_root)
2069 struct btrfs_fs_info *info = extent_root->fs_info;
2070 struct pending_extent_op *extent_op;
2071 struct btrfs_key key;
2073 int skinny_metadata =
2074 btrfs_fs_incompat(extent_root->fs_info, SKINNY_METADATA);
2077 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2078 &end, EXTENT_LOCKED);
2082 ret = get_state_private(&info->extent_ins, start, &priv);
2084 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2086 if (extent_op->type == PENDING_EXTENT_INSERT) {
2087 key.objectid = start;
2088 if (skinny_metadata) {
2089 key.offset = extent_op->level;
2090 key.type = BTRFS_METADATA_ITEM_KEY;
2092 key.offset = extent_op->num_bytes;
2093 key.type = BTRFS_EXTENT_ITEM_KEY;
2095 ret = alloc_reserved_tree_block(trans, extent_root,
2096 extent_root->root_key.objectid,
2100 extent_op->level, &key);
2106 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED);
2112 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2113 struct btrfs_root *root,
2114 u64 bytenr, u64 num_bytes, int is_data)
2117 struct extent_buffer *buf;
2122 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2126 /* we can reuse a block if it hasn't been written
2127 * and it is from this transaction. We can't
2128 * reuse anything from the tree log root because
2129 * it has tiny sub-transactions.
2131 if (btrfs_buffer_uptodate(buf, 0)) {
2132 u64 header_owner = btrfs_header_owner(buf);
2133 u64 header_transid = btrfs_header_generation(buf);
2134 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2135 header_transid == trans->transid &&
2136 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2137 clean_tree_block(NULL, root, buf);
2138 free_extent_buffer(buf);
2142 free_extent_buffer(buf);
2144 update_pinned_extents(root, bytenr, num_bytes, 1);
2150 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2151 u64 bytenr, u64 num_bytes)
2153 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2156 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2157 u64 bytenr, u64 num_bytes)
2159 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2163 * remove an extent from the root, returns 0 on success
2165 static int __free_extent(struct btrfs_trans_handle *trans,
2166 struct btrfs_root *root,
2167 u64 bytenr, u64 num_bytes, u64 parent,
2168 u64 root_objectid, u64 owner_objectid,
2169 u64 owner_offset, int refs_to_drop)
2172 struct btrfs_key key;
2173 struct btrfs_path *path;
2174 struct btrfs_root *extent_root = root->fs_info->extent_root;
2175 struct extent_buffer *leaf;
2176 struct btrfs_extent_item *ei;
2177 struct btrfs_extent_inline_ref *iref;
2180 int extent_slot = 0;
2181 int found_extent = 0;
2185 int skinny_metadata =
2186 btrfs_fs_incompat(extent_root->fs_info, SKINNY_METADATA);
2188 if (root->fs_info->free_extent_hook) {
2189 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2190 parent, root_objectid, owner_objectid,
2191 owner_offset, refs_to_drop);
2194 path = btrfs_alloc_path();
2200 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2202 skinny_metadata = 0;
2203 BUG_ON(!is_data && refs_to_drop != 1);
2205 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2206 bytenr, num_bytes, parent,
2207 root_objectid, owner_objectid,
2210 extent_slot = path->slots[0];
2211 while (extent_slot >= 0) {
2212 btrfs_item_key_to_cpu(path->nodes[0], &key,
2214 if (key.objectid != bytenr)
2216 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2217 key.offset == num_bytes) {
2221 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2222 key.offset == owner_objectid) {
2226 if (path->slots[0] - extent_slot > 5)
2230 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2231 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2232 if (found_extent && item_size < sizeof(*ei))
2235 if (!found_extent) {
2237 ret = remove_extent_backref(trans, extent_root, path,
2241 btrfs_release_path(path);
2243 key.objectid = bytenr;
2245 if (skinny_metadata) {
2246 key.type = BTRFS_METADATA_ITEM_KEY;
2247 key.offset = owner_objectid;
2249 key.type = BTRFS_EXTENT_ITEM_KEY;
2250 key.offset = num_bytes;
2253 ret = btrfs_search_slot(trans, extent_root,
2255 if (ret > 0 && skinny_metadata && path->slots[0]) {
2257 btrfs_item_key_to_cpu(path->nodes[0],
2260 if (key.objectid == bytenr &&
2261 key.type == BTRFS_EXTENT_ITEM_KEY &&
2262 key.offset == num_bytes)
2266 if (ret > 0 && skinny_metadata) {
2267 skinny_metadata = 0;
2268 btrfs_release_path(path);
2269 key.type = BTRFS_EXTENT_ITEM_KEY;
2270 key.offset = num_bytes;
2271 ret = btrfs_search_slot(trans, extent_root,
2276 printk(KERN_ERR "umm, got %d back from search"
2277 ", was looking for %llu\n", ret,
2278 (unsigned long long)bytenr);
2279 btrfs_print_leaf(extent_root, path->nodes[0]);
2282 extent_slot = path->slots[0];
2285 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2286 "parent %llu root %llu owner %llu offset %llu\n",
2287 (unsigned long long)bytenr,
2288 (unsigned long long)parent,
2289 (unsigned long long)root_objectid,
2290 (unsigned long long)owner_objectid,
2291 (unsigned long long)owner_offset);
2296 leaf = path->nodes[0];
2297 item_size = btrfs_item_size_nr(leaf, extent_slot);
2298 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2299 if (item_size < sizeof(*ei)) {
2300 BUG_ON(found_extent || extent_slot != path->slots[0]);
2301 ret = convert_extent_item_v0(trans, extent_root, path,
2305 btrfs_release_path(path);
2307 key.objectid = bytenr;
2308 key.type = BTRFS_EXTENT_ITEM_KEY;
2309 key.offset = num_bytes;
2311 ret = btrfs_search_slot(trans, extent_root, &key, path,
2314 printk(KERN_ERR "umm, got %d back from search"
2315 ", was looking for %llu\n", ret,
2316 (unsigned long long)bytenr);
2317 btrfs_print_leaf(extent_root, path->nodes[0]);
2320 extent_slot = path->slots[0];
2321 leaf = path->nodes[0];
2322 item_size = btrfs_item_size_nr(leaf, extent_slot);
2325 BUG_ON(item_size < sizeof(*ei));
2326 ei = btrfs_item_ptr(leaf, extent_slot,
2327 struct btrfs_extent_item);
2328 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2329 key.type == BTRFS_EXTENT_ITEM_KEY) {
2330 struct btrfs_tree_block_info *bi;
2331 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2332 bi = (struct btrfs_tree_block_info *)(ei + 1);
2333 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2336 refs = btrfs_extent_refs(leaf, ei);
2337 BUG_ON(refs < refs_to_drop);
2338 refs -= refs_to_drop;
2342 * In the case of inline back ref, reference count will
2343 * be updated by remove_extent_backref
2346 BUG_ON(!found_extent);
2348 btrfs_set_extent_refs(leaf, ei, refs);
2349 btrfs_mark_buffer_dirty(leaf);
2352 ret = remove_extent_backref(trans, extent_root, path,
2362 BUG_ON(is_data && refs_to_drop !=
2363 extent_data_ref_count(path, iref));
2365 BUG_ON(path->slots[0] != extent_slot);
2367 BUG_ON(path->slots[0] != extent_slot + 1);
2368 path->slots[0] = extent_slot;
2374 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2381 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2384 btrfs_release_path(path);
2387 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2391 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2394 btrfs_free_path(path);
2395 finish_current_insert(trans, extent_root);
2400 * find all the blocks marked as pending in the radix tree and remove
2401 * them from the extent map
2403 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2404 btrfs_root *extent_root)
2411 struct extent_io_tree *pending_del;
2412 struct extent_io_tree *extent_ins;
2413 struct pending_extent_op *extent_op;
2415 extent_ins = &extent_root->fs_info->extent_ins;
2416 pending_del = &extent_root->fs_info->pending_del;
2419 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2424 ret = get_state_private(pending_del, start, &priv);
2426 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2428 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED);
2430 if (!test_range_bit(extent_ins, start, end,
2431 EXTENT_LOCKED, 0)) {
2432 ret = __free_extent(trans, extent_root,
2433 start, end + 1 - start, 0,
2434 extent_root->root_key.objectid,
2435 extent_op->level, 0, 1);
2439 ret = get_state_private(extent_ins, start, &priv);
2441 extent_op = (struct pending_extent_op *)
2442 (unsigned long)priv;
2444 clear_extent_bits(extent_ins, start, end,
2447 if (extent_op->type == PENDING_BACKREF_UPDATE)
2459 int btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2460 struct btrfs_root *root,
2461 struct extent_buffer *buf,
2462 u64 parent, int last_ref)
2464 return btrfs_free_extent(trans, root, buf->start, buf->len, parent,
2465 root->root_key.objectid,
2466 btrfs_header_level(buf), 0);
2470 * remove an extent from the root, returns 0 on success
2473 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2474 struct btrfs_root *root,
2475 u64 bytenr, u64 num_bytes, u64 parent,
2476 u64 root_objectid, u64 owner, u64 offset)
2478 struct btrfs_root *extent_root = root->fs_info->extent_root;
2482 WARN_ON(num_bytes < root->sectorsize);
2483 if (root == extent_root) {
2484 struct pending_extent_op *extent_op;
2486 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2489 extent_op->type = PENDING_EXTENT_DELETE;
2490 extent_op->bytenr = bytenr;
2491 extent_op->num_bytes = num_bytes;
2492 extent_op->level = (int)owner;
2494 set_extent_bits(&root->fs_info->pending_del,
2495 bytenr, bytenr + num_bytes - 1,
2497 set_state_private(&root->fs_info->pending_del,
2498 bytenr, (unsigned long)extent_op);
2501 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2502 root_objectid, owner, offset, 1);
2503 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2504 return ret ? ret : pending_ret;
2507 static u64 stripe_align(struct btrfs_root *root, u64 val)
2509 u64 mask = ((u64)root->stripesize - 1);
2510 u64 ret = (val + mask) & ~mask;
2515 * walks the btree of allocated extents and find a hole of a given size.
2516 * The key ins is changed to record the hole:
2517 * ins->objectid == block start
2518 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2519 * ins->offset == number of blocks
2520 * Any available blocks before search_start are skipped.
2522 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2523 struct btrfs_root *orig_root,
2524 u64 num_bytes, u64 empty_size,
2525 u64 search_start, u64 search_end,
2526 u64 hint_byte, struct btrfs_key *ins,
2527 u64 exclude_start, u64 exclude_nr,
2531 u64 orig_search_start = search_start;
2532 struct btrfs_root * root = orig_root->fs_info->extent_root;
2533 struct btrfs_fs_info *info = root->fs_info;
2534 u64 total_needed = num_bytes;
2535 struct btrfs_block_group_cache *block_group;
2539 WARN_ON(num_bytes < root->sectorsize);
2540 ins->type = BTRFS_EXTENT_ITEM_KEY;
2542 search_start = stripe_align(root, search_start);
2545 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2547 hint_byte = search_start;
2548 block_group = btrfs_find_block_group(root, block_group,
2549 hint_byte, data, 1);
2551 block_group = btrfs_find_block_group(root,
2553 search_start, data, 1);
2556 total_needed += empty_size;
2559 search_start = stripe_align(root, search_start);
2561 block_group = btrfs_lookup_first_block_group(info,
2564 block_group = btrfs_lookup_first_block_group(info,
2567 ret = find_search_start(root, &block_group, &search_start,
2568 total_needed, data);
2572 ins->objectid = search_start;
2573 ins->offset = num_bytes;
2575 if (ins->objectid + num_bytes >
2576 block_group->key.objectid + block_group->key.offset) {
2577 search_start = block_group->key.objectid +
2578 block_group->key.offset;
2582 if (test_range_bit(&info->extent_ins, ins->objectid,
2583 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2584 search_start = ins->objectid + num_bytes;
2588 if (test_range_bit(&info->pinned_extents, ins->objectid,
2589 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2590 search_start = ins->objectid + num_bytes;
2594 if (info->excluded_extents &&
2595 test_range_bit(info->excluded_extents, ins->objectid,
2596 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2597 search_start = ins->objectid + num_bytes;
2601 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2602 ins->objectid < exclude_start + exclude_nr)) {
2603 search_start = exclude_start + exclude_nr;
2607 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2608 if (check_crossing_stripes(info, ins->objectid, num_bytes)) {
2609 struct btrfs_block_group_cache *bg_cache;
2612 bg_cache = btrfs_lookup_block_group(info, ins->objectid);
2615 bg_offset = ins->objectid - bg_cache->key.objectid;
2617 search_start = round_up(bg_offset + num_bytes,
2618 BTRFS_STRIPE_LEN) + bg_offset;
2622 block_group = btrfs_lookup_block_group(info, ins->objectid);
2624 trans->block_group = block_group;
2626 ins->offset = num_bytes;
2630 block_group = btrfs_lookup_first_block_group(info, search_start);
2632 search_start = orig_search_start;
2639 total_needed -= empty_size;
2645 block_group = btrfs_find_block_group(root, block_group,
2646 search_start, data, 0);
2653 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2654 struct btrfs_root *root,
2655 u64 num_bytes, u64 empty_size,
2656 u64 hint_byte, u64 search_end,
2657 struct btrfs_key *ins, int data)
2660 u64 search_start = 0;
2662 struct btrfs_fs_info *info = root->fs_info;
2665 alloc_profile = info->avail_data_alloc_bits &
2666 info->data_alloc_profile;
2667 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2668 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2669 info->system_allocs >= 0) {
2670 alloc_profile = info->avail_system_alloc_bits &
2671 info->system_alloc_profile;
2672 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2674 alloc_profile = info->avail_metadata_alloc_bits &
2675 info->metadata_alloc_profile;
2676 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2679 if (root->ref_cows) {
2680 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2681 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2683 BTRFS_BLOCK_GROUP_METADATA);
2686 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2687 num_bytes + SZ_2M, data);
2691 WARN_ON(num_bytes < root->sectorsize);
2692 ret = find_free_extent(trans, root, num_bytes, empty_size,
2693 search_start, search_end, hint_byte, ins,
2694 trans->alloc_exclude_start,
2695 trans->alloc_exclude_nr, data);
2697 clear_extent_dirty(&root->fs_info->free_space_cache,
2698 ins->objectid, ins->objectid + ins->offset - 1);
2702 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2703 struct btrfs_root *root,
2704 u64 root_objectid, u64 generation,
2705 u64 flags, struct btrfs_disk_key *key,
2706 int level, struct btrfs_key *ins)
2709 struct btrfs_fs_info *fs_info = root->fs_info;
2710 struct btrfs_extent_item *extent_item;
2711 struct btrfs_tree_block_info *block_info;
2712 struct btrfs_extent_inline_ref *iref;
2713 struct btrfs_path *path;
2714 struct extent_buffer *leaf;
2715 u32 size = sizeof(*extent_item) + sizeof(*iref);
2716 int skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
2718 if (!skinny_metadata)
2719 size += sizeof(*block_info);
2721 path = btrfs_alloc_path();
2725 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2729 leaf = path->nodes[0];
2730 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2731 struct btrfs_extent_item);
2732 btrfs_set_extent_refs(leaf, extent_item, 1);
2733 btrfs_set_extent_generation(leaf, extent_item, generation);
2734 btrfs_set_extent_flags(leaf, extent_item,
2735 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2737 if (skinny_metadata) {
2738 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2740 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2741 btrfs_set_tree_block_key(leaf, block_info, key);
2742 btrfs_set_tree_block_level(leaf, block_info, level);
2743 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2746 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2747 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2749 btrfs_mark_buffer_dirty(leaf);
2750 btrfs_free_path(path);
2752 ret = update_block_group(trans, root, ins->objectid, root->nodesize,
2757 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2758 struct btrfs_root *root, u64 num_bytes,
2759 u64 root_objectid, u64 generation,
2760 u64 flags, struct btrfs_disk_key *key,
2761 int level, u64 empty_size, u64 hint_byte,
2762 u64 search_end, struct btrfs_key *ins)
2765 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2766 hint_byte, search_end, ins, 0);
2769 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2770 struct pending_extent_op *extent_op;
2772 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2775 extent_op->type = PENDING_EXTENT_INSERT;
2776 extent_op->bytenr = ins->objectid;
2777 extent_op->num_bytes = ins->offset;
2778 extent_op->level = level;
2779 extent_op->flags = flags;
2780 memcpy(&extent_op->key, key, sizeof(*key));
2782 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2783 ins->objectid + ins->offset - 1,
2785 set_state_private(&root->fs_info->extent_ins,
2786 ins->objectid, (unsigned long)extent_op);
2788 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
2789 ins->offset = level;
2790 ins->type = BTRFS_METADATA_ITEM_KEY;
2792 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2795 finish_current_insert(trans, root->fs_info->extent_root);
2796 del_pending_extents(trans, root->fs_info->extent_root);
2802 * helper function to allocate a block for a given tree
2803 * returns the tree buffer or NULL.
2805 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2806 struct btrfs_root *root,
2807 u32 blocksize, u64 root_objectid,
2808 struct btrfs_disk_key *key, int level,
2809 u64 hint, u64 empty_size)
2811 struct btrfs_key ins;
2813 struct extent_buffer *buf;
2815 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2816 trans->transid, 0, key, level,
2817 empty_size, hint, (u64)-1, &ins);
2820 return ERR_PTR(ret);
2823 buf = btrfs_find_create_tree_block(root->fs_info, ins.objectid,
2826 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2827 0, root->root_key.objectid, level, 0);
2829 return ERR_PTR(-ENOMEM);
2831 btrfs_set_buffer_uptodate(buf);
2832 trans->blocks_used++;
2839 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2840 struct btrfs_root *root,
2841 struct extent_buffer *leaf)
2844 u64 leaf_generation;
2845 struct btrfs_key key;
2846 struct btrfs_file_extent_item *fi;
2851 BUG_ON(!btrfs_is_leaf(leaf));
2852 nritems = btrfs_header_nritems(leaf);
2853 leaf_owner = btrfs_header_owner(leaf);
2854 leaf_generation = btrfs_header_generation(leaf);
2856 for (i = 0; i < nritems; i++) {
2859 btrfs_item_key_to_cpu(leaf, &key, i);
2860 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2862 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2863 if (btrfs_file_extent_type(leaf, fi) ==
2864 BTRFS_FILE_EXTENT_INLINE)
2867 * FIXME make sure to insert a trans record that
2868 * repeats the snapshot del on crash
2870 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2871 if (disk_bytenr == 0)
2873 ret = btrfs_free_extent(trans, root, disk_bytenr,
2874 btrfs_file_extent_disk_num_bytes(leaf, fi),
2875 leaf->start, leaf_owner, leaf_generation,
2882 static void noinline reada_walk_down(struct btrfs_root *root,
2883 struct extent_buffer *node,
2896 nritems = btrfs_header_nritems(node);
2897 level = btrfs_header_level(node);
2901 for (i = slot; i < nritems && skipped < 32; i++) {
2902 bytenr = btrfs_node_blockptr(node, i);
2903 if (last && ((bytenr > last && bytenr - last > SZ_32K) ||
2904 (last > bytenr && last - bytenr > SZ_32K))) {
2908 blocksize = btrfs_level_size(root, level - 1);
2910 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2918 mutex_unlock(&root->fs_info->fs_mutex);
2919 ret = readahead_tree_block(root, bytenr, blocksize,
2920 btrfs_node_ptr_generation(node, i));
2921 last = bytenr + blocksize;
2923 mutex_lock(&root->fs_info->fs_mutex);
2930 * helper function for drop_snapshot, this walks down the tree dropping ref
2931 * counts as it goes.
2933 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2934 struct btrfs_root *root,
2935 struct btrfs_path *path, int *level)
2941 struct extent_buffer *next;
2942 struct extent_buffer *cur;
2943 struct extent_buffer *parent;
2948 WARN_ON(*level < 0);
2949 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2950 ret = btrfs_lookup_extent_ref(trans, root,
2951 path->nodes[*level]->start,
2952 path->nodes[*level]->len, &refs);
2958 * walk down to the last node level and free all the leaves
2960 while(*level >= 0) {
2961 WARN_ON(*level < 0);
2962 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2963 cur = path->nodes[*level];
2965 if (btrfs_header_level(cur) != *level)
2968 if (path->slots[*level] >=
2969 btrfs_header_nritems(cur))
2972 ret = drop_leaf_ref(trans, root, cur);
2976 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2977 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2978 blocksize = btrfs_level_size(root, *level - 1);
2979 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2983 parent = path->nodes[*level];
2984 root_owner = btrfs_header_owner(parent);
2985 root_gen = btrfs_header_generation(parent);
2986 path->slots[*level]++;
2987 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2988 parent->start, root_owner,
2989 root_gen, *level - 1, 1);
2993 next = btrfs_find_tree_block(root, bytenr, blocksize);
2994 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2995 free_extent_buffer(next);
2996 reada_walk_down(root, cur, path->slots[*level]);
2997 mutex_unlock(&root->fs_info->fs_mutex);
2998 next = read_tree_block(root, bytenr, blocksize,
3000 mutex_lock(&root->fs_info->fs_mutex);
3001 if (!extent_buffer_uptodate(next)) {
3003 ret = PTR_ERR(next);
3009 WARN_ON(*level <= 0);
3010 if (path->nodes[*level-1])
3011 free_extent_buffer(path->nodes[*level-1]);
3012 path->nodes[*level-1] = next;
3013 *level = btrfs_header_level(next);
3014 path->slots[*level] = 0;
3017 WARN_ON(*level < 0);
3018 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3020 if (path->nodes[*level] == root->node) {
3021 root_owner = root->root_key.objectid;
3022 parent = path->nodes[*level];
3024 parent = path->nodes[*level + 1];
3025 root_owner = btrfs_header_owner(parent);
3028 root_gen = btrfs_header_generation(parent);
3029 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3030 path->nodes[*level]->len, parent->start,
3031 root_owner, root_gen, *level, 1);
3032 free_extent_buffer(path->nodes[*level]);
3033 path->nodes[*level] = NULL;
3040 * helper for dropping snapshots. This walks back up the tree in the path
3041 * to find the first node higher up where we haven't yet gone through
3044 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3045 struct btrfs_root *root,
3046 struct btrfs_path *path, int *level)
3050 struct btrfs_root_item *root_item = &root->root_item;
3055 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3056 slot = path->slots[i];
3057 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3058 struct extent_buffer *node;
3059 struct btrfs_disk_key disk_key;
3060 node = path->nodes[i];
3063 WARN_ON(*level == 0);
3064 btrfs_node_key(node, &disk_key, path->slots[i]);
3065 memcpy(&root_item->drop_progress,
3066 &disk_key, sizeof(disk_key));
3067 root_item->drop_level = i;
3070 struct extent_buffer *parent;
3071 if (path->nodes[*level] == root->node)
3072 parent = path->nodes[*level];
3074 parent = path->nodes[*level + 1];
3076 root_owner = btrfs_header_owner(parent);
3077 root_gen = btrfs_header_generation(parent);
3078 ret = btrfs_free_extent(trans, root,
3079 path->nodes[*level]->start,
3080 path->nodes[*level]->len,
3081 parent->start, root_owner,
3082 root_gen, *level, 1);
3084 free_extent_buffer(path->nodes[*level]);
3085 path->nodes[*level] = NULL;
3094 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3096 struct btrfs_space_info *sinfo;
3097 struct btrfs_block_group_cache *cache;
3104 ret = find_first_extent_bit(&info->block_group_cache, 0,
3105 &start, &end, (unsigned int)-1);
3108 ret = get_state_private(&info->block_group_cache, start, &ptr);
3110 cache = u64_to_ptr(ptr);
3111 if (cache->free_space_ctl) {
3112 btrfs_remove_free_space_cache(cache);
3113 kfree(cache->free_space_ctl);
3117 clear_extent_bits(&info->block_group_cache, start,
3118 end, (unsigned int)-1);
3121 ret = find_first_extent_bit(&info->free_space_cache, 0,
3122 &start, &end, EXTENT_DIRTY);
3125 clear_extent_dirty(&info->free_space_cache, start, end);
3128 while (!list_empty(&info->space_info)) {
3129 sinfo = list_entry(info->space_info.next,
3130 struct btrfs_space_info, list);
3131 list_del_init(&sinfo->list);
3137 static int find_first_block_group(struct btrfs_root *root,
3138 struct btrfs_path *path, struct btrfs_key *key)
3141 struct btrfs_key found_key;
3142 struct extent_buffer *leaf;
3145 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3149 slot = path->slots[0];
3150 leaf = path->nodes[0];
3151 if (slot >= btrfs_header_nritems(leaf)) {
3152 ret = btrfs_next_leaf(root, path);
3159 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3161 if (found_key.objectid >= key->objectid &&
3162 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3171 static void account_super_bytes(struct btrfs_fs_info *fs_info,
3172 struct btrfs_block_group_cache *cache)
3179 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
3180 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
3181 cache->bytes_super += stripe_len;
3184 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3185 bytenr = btrfs_sb_offset(i);
3186 ret = btrfs_rmap_block(&fs_info->mapping_tree,
3187 cache->key.objectid, bytenr,
3188 0, &logical, &nr, &stripe_len);
3195 if (logical[nr] > cache->key.objectid +
3199 if (logical[nr] + stripe_len <= cache->key.objectid)
3202 start = logical[nr];
3203 if (start < cache->key.objectid) {
3204 start = cache->key.objectid;
3205 len = (logical[nr] + stripe_len) - start;
3207 len = min_t(u64, stripe_len,
3208 cache->key.objectid +
3209 cache->key.offset - start);
3212 cache->bytes_super += len;
3219 int btrfs_read_block_groups(struct btrfs_root *root)
3221 struct btrfs_path *path;
3224 struct btrfs_block_group_cache *cache;
3225 struct btrfs_fs_info *info = root->fs_info;
3226 struct btrfs_space_info *space_info;
3227 struct extent_io_tree *block_group_cache;
3228 struct btrfs_key key;
3229 struct btrfs_key found_key;
3230 struct extent_buffer *leaf;
3232 block_group_cache = &info->block_group_cache;
3234 root = info->extent_root;
3237 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3238 path = btrfs_alloc_path();
3243 ret = find_first_block_group(root, path, &key);
3251 leaf = path->nodes[0];
3252 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3253 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3259 read_extent_buffer(leaf, &cache->item,
3260 btrfs_item_ptr_offset(leaf, path->slots[0]),
3261 sizeof(cache->item));
3262 memcpy(&cache->key, &found_key, sizeof(found_key));
3265 key.objectid = found_key.objectid + found_key.offset;
3266 if (found_key.offset == 0)
3268 btrfs_release_path(path);
3269 cache->flags = btrfs_block_group_flags(&cache->item);
3271 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3272 bit = BLOCK_GROUP_DATA;
3273 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3274 bit = BLOCK_GROUP_SYSTEM;
3275 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3276 bit = BLOCK_GROUP_METADATA;
3278 set_avail_alloc_bits(info, cache->flags);
3279 if (btrfs_chunk_readonly(root, cache->key.objectid))
3282 account_super_bytes(info, cache);
3284 ret = update_space_info(info, cache->flags, found_key.offset,
3285 btrfs_block_group_used(&cache->item),
3288 cache->space_info = space_info;
3290 /* use EXTENT_LOCKED to prevent merging */
3291 set_extent_bits(block_group_cache, found_key.objectid,
3292 found_key.objectid + found_key.offset - 1,
3293 bit | EXTENT_LOCKED);
3294 set_state_private(block_group_cache, found_key.objectid,
3295 (unsigned long)cache);
3299 btrfs_free_path(path);
3303 struct btrfs_block_group_cache *
3304 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3305 u64 chunk_objectid, u64 chunk_offset, u64 size)
3309 struct btrfs_block_group_cache *cache;
3310 struct extent_io_tree *block_group_cache;
3312 block_group_cache = &fs_info->block_group_cache;
3314 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3316 cache->key.objectid = chunk_offset;
3317 cache->key.offset = size;
3319 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3320 btrfs_set_block_group_used(&cache->item, bytes_used);
3321 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3322 cache->flags = type;
3323 btrfs_set_block_group_flags(&cache->item, type);
3325 account_super_bytes(fs_info, cache);
3326 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3327 &cache->space_info);
3330 bit = block_group_state_bits(type);
3331 ret = set_extent_bits(block_group_cache, chunk_offset,
3332 chunk_offset + size - 1,
3333 bit | EXTENT_LOCKED);
3336 ret = set_state_private(block_group_cache, chunk_offset,
3337 (unsigned long)cache);
3339 set_avail_alloc_bits(fs_info, type);
3344 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3345 struct btrfs_root *root, u64 bytes_used,
3346 u64 type, u64 chunk_objectid, u64 chunk_offset,
3350 struct btrfs_root *extent_root;
3351 struct btrfs_block_group_cache *cache;
3353 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3354 chunk_objectid, chunk_offset, size);
3355 extent_root = root->fs_info->extent_root;
3356 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3357 sizeof(cache->item));
3360 ret = finish_current_insert(trans, extent_root);
3362 ret = del_pending_extents(trans, extent_root);
3369 * This is for converter use only.
3371 * In that case, we don't know where are free blocks located.
3372 * Therefore all block group cache entries must be setup properly
3373 * before doing any block allocation.
3375 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3376 struct btrfs_root *root)
3384 u64 total_metadata = 0;
3388 struct btrfs_root *extent_root;
3389 struct btrfs_block_group_cache *cache;
3390 struct extent_io_tree *block_group_cache;
3392 extent_root = root->fs_info->extent_root;
3393 block_group_cache = &root->fs_info->block_group_cache;
3394 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3395 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3396 group_align = 64 * root->sectorsize;
3399 while (cur_start < total_bytes) {
3400 group_size = total_bytes / 12;
3401 group_size = min_t(u64, group_size, total_bytes - cur_start);
3402 if (cur_start == 0) {
3403 bit = BLOCK_GROUP_SYSTEM;
3404 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3406 group_size &= ~(group_align - 1);
3407 group_size = max_t(u64, group_size, SZ_8M);
3408 group_size = min_t(u64, group_size, SZ_32M);
3410 group_size &= ~(group_align - 1);
3411 if (total_data >= total_metadata * 2) {
3412 group_type = BTRFS_BLOCK_GROUP_METADATA;
3413 group_size = min_t(u64, group_size, SZ_1G);
3414 total_metadata += group_size;
3416 group_type = BTRFS_BLOCK_GROUP_DATA;
3417 group_size = min_t(u64, group_size,
3419 total_data += group_size;
3421 if ((total_bytes - cur_start) * 4 < group_size * 5)
3422 group_size = total_bytes - cur_start;
3425 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3428 cache->key.objectid = cur_start;
3429 cache->key.offset = group_size;
3430 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3432 btrfs_set_block_group_used(&cache->item, 0);
3433 btrfs_set_block_group_chunk_objectid(&cache->item,
3435 btrfs_set_block_group_flags(&cache->item, group_type);
3437 cache->flags = group_type;
3439 ret = update_space_info(root->fs_info, group_type, group_size,
3440 0, &cache->space_info);
3442 set_avail_alloc_bits(extent_root->fs_info, group_type);
3444 set_extent_bits(block_group_cache, cur_start,
3445 cur_start + group_size - 1,
3446 bit | EXTENT_LOCKED);
3447 set_state_private(block_group_cache, cur_start,
3448 (unsigned long)cache);
3449 cur_start += group_size;
3451 /* then insert all the items */
3453 while(cur_start < total_bytes) {
3454 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3457 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3458 sizeof(cache->item));
3461 finish_current_insert(trans, extent_root);
3462 ret = del_pending_extents(trans, extent_root);
3465 cur_start = cache->key.objectid + cache->key.offset;
3470 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3471 struct btrfs_root *root,
3472 u64 bytenr, u64 num_bytes, int alloc,
3475 return update_block_group(trans, root, bytenr, num_bytes,
3480 * Just remove a block group item in extent tree
3481 * Caller should ensure the block group is empty and all space is pinned.
3482 * Or new tree block/data may be allocated into it.
3484 static int free_block_group_item(struct btrfs_trans_handle *trans,
3485 struct btrfs_fs_info *fs_info,
3486 u64 bytenr, u64 len)
3488 struct btrfs_path *path;
3489 struct btrfs_key key;
3490 struct btrfs_root *root = fs_info->extent_root;
3493 key.objectid = bytenr;
3495 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3497 path = btrfs_alloc_path();
3501 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3509 ret = btrfs_del_item(trans, root, path);
3511 btrfs_free_path(path);
3515 static int free_dev_extent_item(struct btrfs_trans_handle *trans,
3516 struct btrfs_fs_info *fs_info,
3517 u64 devid, u64 dev_offset)
3519 struct btrfs_root *root = fs_info->dev_root;
3520 struct btrfs_path *path;
3521 struct btrfs_key key;
3524 path = btrfs_alloc_path();
3528 key.objectid = devid;
3529 key.type = BTRFS_DEV_EXTENT_KEY;
3530 key.offset = dev_offset;
3532 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3540 ret = btrfs_del_item(trans, root, path);
3542 btrfs_free_path(path);
3546 static int free_chunk_dev_extent_items(struct btrfs_trans_handle *trans,
3547 struct btrfs_fs_info *fs_info,
3550 struct btrfs_chunk *chunk = NULL;
3551 struct btrfs_root *root= fs_info->chunk_root;
3552 struct btrfs_path *path;
3553 struct btrfs_key key;
3558 path = btrfs_alloc_path();
3562 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3563 key.type = BTRFS_CHUNK_ITEM_KEY;
3564 key.offset = chunk_offset;
3566 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
3573 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
3574 struct btrfs_chunk);
3575 num_stripes = btrfs_chunk_num_stripes(path->nodes[0], chunk);
3576 for (i = 0; i < num_stripes; i++) {
3577 ret = free_dev_extent_item(trans, fs_info,
3578 btrfs_stripe_devid_nr(path->nodes[0], chunk, i),
3579 btrfs_stripe_offset_nr(path->nodes[0], chunk, i));
3584 btrfs_free_path(path);
3588 static int free_system_chunk_item(struct btrfs_super_block *super,
3589 struct btrfs_key *key)
3591 struct btrfs_disk_key *disk_key;
3592 struct btrfs_key cpu_key;
3593 u32 array_size = btrfs_super_sys_array_size(super);
3594 char *ptr = (char *)super->sys_chunk_array;
3598 while (cur < btrfs_super_sys_array_size(super)) {
3599 struct btrfs_chunk *chunk;
3603 disk_key = (struct btrfs_disk_key *)(ptr + cur);
3604 btrfs_disk_key_to_cpu(&cpu_key, disk_key);
3605 if (cpu_key.type != BTRFS_CHUNK_ITEM_KEY) {
3611 chunk = (struct btrfs_chunk *)(ptr + cur + sizeof(*disk_key));
3612 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
3613 chunk_len = btrfs_chunk_item_size(num_stripes) +
3616 if (key->objectid == cpu_key.objectid &&
3617 key->offset == cpu_key.offset &&
3618 key->type == cpu_key.type) {
3619 memmove(ptr + cur, ptr + cur + chunk_len,
3620 array_size - cur - chunk_len);
3621 array_size -= chunk_len;
3622 btrfs_set_super_sys_array_size(super, array_size);
3633 static int free_chunk_item(struct btrfs_trans_handle *trans,
3634 struct btrfs_fs_info *fs_info,
3637 struct btrfs_path *path;
3638 struct btrfs_key key;
3639 struct btrfs_root *root = fs_info->chunk_root;
3640 struct btrfs_chunk *chunk;
3644 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3645 key.offset = bytenr;
3646 key.type = BTRFS_CHUNK_ITEM_KEY;
3648 path = btrfs_alloc_path();
3652 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3659 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
3660 struct btrfs_chunk);
3661 chunk_type = btrfs_chunk_type(path->nodes[0], chunk);
3663 ret = btrfs_del_item(trans, root, path);
3667 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3668 ret = free_system_chunk_item(fs_info->super_copy, &key);
3670 btrfs_free_path(path);
3674 static u64 get_dev_extent_len(struct map_lookup *map)
3678 switch (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
3679 case 0: /* Single */
3680 case BTRFS_BLOCK_GROUP_DUP:
3681 case BTRFS_BLOCK_GROUP_RAID1:
3684 case BTRFS_BLOCK_GROUP_RAID5:
3685 div = (map->num_stripes - 1);
3687 case BTRFS_BLOCK_GROUP_RAID6:
3688 div = (map->num_stripes - 2);
3690 case BTRFS_BLOCK_GROUP_RAID10:
3691 div = (map->num_stripes / map->sub_stripes);
3694 /* normally, read chunk security hook should handled it */
3697 return map->ce.size / div;
3700 /* free block group/chunk related caches */
3701 static int free_block_group_cache(struct btrfs_trans_handle *trans,
3702 struct btrfs_fs_info *fs_info,
3703 u64 bytenr, u64 len)
3705 struct btrfs_block_group_cache *cache;
3706 struct cache_extent *ce;
3707 struct map_lookup *map;
3712 /* Free block group cache first */
3713 cache = btrfs_lookup_block_group(fs_info, bytenr);
3716 flags = cache->flags;
3717 if (cache->free_space_ctl) {
3718 btrfs_remove_free_space_cache(cache);
3719 kfree(cache->free_space_ctl);
3721 clear_extent_bits(&fs_info->block_group_cache, bytenr, bytenr + len,
3723 ret = free_space_info(fs_info, flags, len, 0, NULL);
3728 /* Then free mapping info and dev usage info */
3729 ce = search_cache_extent(&fs_info->mapping_tree.cache_tree, bytenr);
3730 if (!ce || ce->start != bytenr) {
3734 map = container_of(ce, struct map_lookup, ce);
3735 for (i = 0; i < map->num_stripes; i++) {
3736 struct btrfs_device *device;
3738 device = map->stripes[i].dev;
3739 device->bytes_used -= get_dev_extent_len(map);
3740 ret = btrfs_update_device(trans, device);
3744 remove_cache_extent(&fs_info->mapping_tree.cache_tree, ce);
3750 int btrfs_free_block_group(struct btrfs_trans_handle *trans,
3751 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len)
3753 struct btrfs_root *extent_root = fs_info->extent_root;
3754 struct btrfs_path *path;
3755 struct btrfs_block_group_item *bgi;
3756 struct btrfs_key key;
3759 path = btrfs_alloc_path();
3763 key.objectid = bytenr;
3764 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3767 /* Double check the block group to ensure it's empty */
3768 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3776 bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
3777 struct btrfs_block_group_item);
3778 if (btrfs_disk_block_group_used(path->nodes[0], bgi)) {
3780 "WARNING: block group [%llu,%llu) is not empty\n",
3781 bytenr, bytenr + len);
3785 btrfs_release_path(path);
3788 * Now pin all space in the block group, to prevent further transaction
3789 * allocate space from it.
3790 * Every operation needs a transaction must be in the range.
3792 btrfs_pin_extent(fs_info, bytenr, len);
3794 /* delete block group item and chunk item */
3795 ret = free_block_group_item(trans, fs_info, bytenr, len);
3798 "failed to free block group item for [%llu,%llu)\n",
3799 bytenr, bytenr + len);
3800 btrfs_unpin_extent(fs_info, bytenr, len);
3804 ret = free_chunk_dev_extent_items(trans, fs_info, bytenr);
3807 "failed to dev extents belongs to [%llu,%llu)\n",
3808 bytenr, bytenr + len);
3809 btrfs_unpin_extent(fs_info, bytenr, len);
3812 ret = free_chunk_item(trans, fs_info, bytenr);
3815 "failed to free chunk for [%llu,%llu)\n",
3816 bytenr, bytenr + len);
3817 btrfs_unpin_extent(fs_info, bytenr, len);
3821 /* Now release the block_group_cache */
3822 ret = free_block_group_cache(trans, fs_info, bytenr, len);
3823 btrfs_unpin_extent(fs_info, bytenr, len);
3826 btrfs_free_path(path);
3831 * Fixup block accounting. The initial block accounting created by
3832 * make_block_groups isn't accuracy in this case.
3834 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3835 struct btrfs_root *root)
3841 struct btrfs_path path;
3842 struct btrfs_key key;
3843 struct extent_buffer *leaf;
3844 struct btrfs_block_group_cache *cache;
3845 struct btrfs_fs_info *fs_info = root->fs_info;
3847 root = root->fs_info->extent_root;
3849 while(extent_root_pending_ops(fs_info)) {
3850 ret = finish_current_insert(trans, root);
3853 ret = del_pending_extents(trans, root);
3859 cache = btrfs_lookup_first_block_group(fs_info, start);
3862 start = cache->key.objectid + cache->key.offset;
3863 btrfs_set_block_group_used(&cache->item, 0);
3864 cache->space_info->bytes_used = 0;
3865 set_extent_bits(&root->fs_info->block_group_cache,
3866 cache->key.objectid,
3867 cache->key.objectid + cache->key.offset -1,
3871 btrfs_init_path(&path);
3874 key.type = BTRFS_EXTENT_ITEM_KEY;
3875 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3880 leaf = path.nodes[0];
3881 slot = path.slots[0];
3882 if (slot >= btrfs_header_nritems(leaf)) {
3883 ret = btrfs_next_leaf(root, &path);
3888 leaf = path.nodes[0];
3889 slot = path.slots[0];
3891 btrfs_item_key_to_cpu(leaf, &key, slot);
3892 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3893 bytes_used += key.offset;
3894 ret = btrfs_update_block_group(trans, root,
3895 key.objectid, key.offset, 1, 0);
3897 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3898 bytes_used += root->nodesize;
3899 ret = btrfs_update_block_group(trans, root,
3900 key.objectid, root->nodesize, 1, 0);
3905 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3906 btrfs_release_path(&path);
3910 static void __get_extent_size(struct btrfs_root *root, struct btrfs_path *path,
3911 u64 *start, u64 *len)
3913 struct btrfs_key key;
3915 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3916 BUG_ON(!(key.type == BTRFS_EXTENT_ITEM_KEY ||
3917 key.type == BTRFS_METADATA_ITEM_KEY));
3918 *start = key.objectid;
3919 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3922 *len = root->nodesize;
3926 * Find first overlap extent for range [bytenr, bytenr + len)
3927 * Return 0 for found and point path to it.
3928 * Return >0 for not found.
3931 int btrfs_search_overlap_extent(struct btrfs_root *root,
3932 struct btrfs_path *path, u64 bytenr, u64 len)
3934 struct btrfs_key key;
3939 key.objectid = bytenr;
3940 key.type = BTRFS_EXTENT_DATA_KEY;
3941 key.offset = (u64)-1;
3943 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3948 ret = btrfs_previous_extent_item(root, path, 0);
3951 /* no previous, check next extent */
3954 __get_extent_size(root, path, &cur_start, &cur_len);
3956 if (cur_start + cur_len > bytenr)
3960 ret = btrfs_next_extent_item(root, path, bytenr + len);
3963 /* No next, prev already checked, no overlap */
3966 __get_extent_size(root, path, &cur_start, &cur_len);
3968 if (cur_start < bytenr + len)
3973 static int __btrfs_record_file_extent(struct btrfs_trans_handle *trans,
3974 struct btrfs_root *root, u64 objectid,
3975 struct btrfs_inode_item *inode,
3976 u64 file_pos, u64 disk_bytenr,
3980 struct btrfs_fs_info *info = root->fs_info;
3981 struct btrfs_root *extent_root = info->extent_root;
3982 struct extent_buffer *leaf;
3983 struct btrfs_file_extent_item *fi;
3984 struct btrfs_key ins_key;
3985 struct btrfs_path *path;
3986 struct btrfs_extent_item *ei;
3988 u64 extent_num_bytes;
3991 u64 num_bytes = *ret_num_bytes;
3994 * All supported file system should not use its 0 extent.
3997 * And hole extent has no size limit, no need to loop.
3999 if (disk_bytenr == 0) {
4000 ret = btrfs_insert_file_extent(trans, root, objectid,
4001 file_pos, disk_bytenr,
4002 num_bytes, num_bytes);
4005 num_bytes = min_t(u64, num_bytes, BTRFS_MAX_EXTENT_SIZE);
4007 path = btrfs_alloc_path();
4011 /* First to check extent overlap */
4012 ret = btrfs_search_overlap_extent(extent_root, path, disk_bytenr,
4021 __get_extent_size(extent_root, path, &cur_start, &cur_len);
4023 * For convert case, this extent should be a subset of
4026 BUG_ON(disk_bytenr < cur_start);
4028 extent_bytenr = cur_start;
4029 extent_num_bytes = cur_len;
4030 extent_offset = disk_bytenr - extent_bytenr;
4032 /* No overlap, create new extent */
4033 btrfs_release_path(path);
4034 ins_key.objectid = disk_bytenr;
4035 ins_key.offset = num_bytes;
4036 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4038 ret = btrfs_insert_empty_item(trans, extent_root, path,
4039 &ins_key, sizeof(*ei));
4041 leaf = path->nodes[0];
4042 ei = btrfs_item_ptr(leaf, path->slots[0],
4043 struct btrfs_extent_item);
4045 btrfs_set_extent_refs(leaf, ei, 0);
4046 btrfs_set_extent_generation(leaf, ei, 0);
4047 btrfs_set_extent_flags(leaf, ei,
4048 BTRFS_EXTENT_FLAG_DATA);
4049 btrfs_mark_buffer_dirty(leaf);
4051 ret = btrfs_update_block_group(trans, root, disk_bytenr,
4055 } else if (ret != -EEXIST) {
4058 btrfs_extent_post_op(trans, extent_root);
4059 extent_bytenr = disk_bytenr;
4060 extent_num_bytes = num_bytes;
4063 btrfs_release_path(path);
4064 ins_key.objectid = objectid;
4065 ins_key.offset = file_pos;
4066 ins_key.type = BTRFS_EXTENT_DATA_KEY;
4067 ret = btrfs_insert_empty_item(trans, root, path, &ins_key,
4071 leaf = path->nodes[0];
4072 fi = btrfs_item_ptr(leaf, path->slots[0],
4073 struct btrfs_file_extent_item);
4074 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
4075 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
4076 btrfs_set_file_extent_disk_bytenr(leaf, fi, extent_bytenr);
4077 btrfs_set_file_extent_disk_num_bytes(leaf, fi, extent_num_bytes);
4078 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
4079 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
4080 btrfs_set_file_extent_ram_bytes(leaf, fi, extent_num_bytes);
4081 btrfs_set_file_extent_compression(leaf, fi, 0);
4082 btrfs_set_file_extent_encryption(leaf, fi, 0);
4083 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
4084 btrfs_mark_buffer_dirty(leaf);
4086 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
4087 btrfs_set_stack_inode_nbytes(inode, nbytes);
4088 btrfs_release_path(path);
4090 ret = btrfs_inc_extent_ref(trans, root, extent_bytenr, extent_num_bytes,
4091 0, root->root_key.objectid, objectid,
4092 file_pos - extent_offset);
4096 *ret_num_bytes = min(extent_num_bytes - extent_offset, num_bytes);
4098 btrfs_free_path(path);
4103 * Record a file extent. Do all the required works, such as inserting
4104 * file extent item, inserting extent item and backref item into extent
4105 * tree and updating block accounting.
4107 int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
4108 struct btrfs_root *root, u64 objectid,
4109 struct btrfs_inode_item *inode,
4110 u64 file_pos, u64 disk_bytenr,
4113 u64 cur_disk_bytenr = disk_bytenr;
4114 u64 cur_file_pos = file_pos;
4115 u64 cur_num_bytes = num_bytes;
4118 while (num_bytes > 0) {
4119 ret = __btrfs_record_file_extent(trans, root, objectid,
4120 inode, cur_file_pos,
4125 cur_disk_bytenr += cur_num_bytes;
4126 cur_file_pos += cur_num_bytes;
4127 num_bytes -= cur_num_bytes;
4133 static int add_excluded_extent(struct btrfs_root *root,
4134 u64 start, u64 num_bytes)
4136 u64 end = start + num_bytes - 1;
4137 set_extent_bits(&root->fs_info->pinned_extents,
4138 start, end, EXTENT_UPTODATE);
4142 void free_excluded_extents(struct btrfs_root *root,
4143 struct btrfs_block_group_cache *cache)
4147 start = cache->key.objectid;
4148 end = start + cache->key.offset - 1;
4150 clear_extent_bits(&root->fs_info->pinned_extents,
4151 start, end, EXTENT_UPTODATE);
4154 int exclude_super_stripes(struct btrfs_root *root,
4155 struct btrfs_block_group_cache *cache)
4162 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
4163 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
4164 cache->bytes_super += stripe_len;
4165 ret = add_excluded_extent(root, cache->key.objectid,
4171 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4172 bytenr = btrfs_sb_offset(i);
4173 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
4174 cache->key.objectid, bytenr,
4175 0, &logical, &nr, &stripe_len);
4182 if (logical[nr] > cache->key.objectid +
4186 if (logical[nr] + stripe_len <= cache->key.objectid)
4189 start = logical[nr];
4190 if (start < cache->key.objectid) {
4191 start = cache->key.objectid;
4192 len = (logical[nr] + stripe_len) - start;
4194 len = min_t(u64, stripe_len,
4195 cache->key.objectid +
4196 cache->key.offset - start);
4199 cache->bytes_super += len;
4200 ret = add_excluded_extent(root, start, len);
4212 u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
4213 struct btrfs_fs_info *info, u64 start, u64 end)
4215 u64 extent_start, extent_end, size, total_added = 0;
4218 while (start < end) {
4219 ret = find_first_extent_bit(&info->pinned_extents, start,
4220 &extent_start, &extent_end,
4221 EXTENT_DIRTY | EXTENT_UPTODATE);
4225 if (extent_start <= start) {
4226 start = extent_end + 1;
4227 } else if (extent_start > start && extent_start < end) {
4228 size = extent_start - start;
4229 total_added += size;
4230 ret = btrfs_add_free_space(block_group->free_space_ctl,
4232 BUG_ON(ret); /* -ENOMEM or logic error */
4233 start = extent_end + 1;
4241 total_added += size;
4242 ret = btrfs_add_free_space(block_group->free_space_ctl, start,
4244 BUG_ON(ret); /* -ENOMEM or logic error */