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.
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.h"
27 #include "transaction.h"
30 #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, GFP_NOFS);
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,
155 if (key.type == BTRFS_METADATA_ITEM_KEY)
156 last = key.objectid + root->leafsize;
158 last = key.objectid + key.offset;
164 if (block_group->key.objectid +
165 block_group->key.offset > last) {
166 hole_size = block_group->key.objectid +
167 block_group->key.offset - last;
168 set_extent_dirty(free_space_cache, last,
169 last + hole_size - 1, GFP_NOFS);
171 remove_sb_from_cache(root, block_group);
172 block_group->cached = 1;
174 btrfs_free_path(path);
178 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
182 struct extent_io_tree *block_group_cache;
183 struct btrfs_block_group_cache *block_group = NULL;
189 bytenr = max_t(u64, bytenr,
190 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
191 block_group_cache = &info->block_group_cache;
192 ret = find_first_extent_bit(block_group_cache,
193 bytenr, &start, &end,
194 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
199 ret = get_state_private(block_group_cache, start, &ptr);
203 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
207 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
211 struct extent_io_tree *block_group_cache;
212 struct btrfs_block_group_cache *block_group = NULL;
218 block_group_cache = &info->block_group_cache;
219 ret = find_first_extent_bit(block_group_cache,
220 bytenr, &start, &end,
221 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
226 ret = get_state_private(block_group_cache, start, &ptr);
230 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
231 if (block_group->key.objectid <= bytenr && bytenr <
232 block_group->key.objectid + block_group->key.offset)
237 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
239 return (cache->flags & bits) == bits;
242 static int noinline find_search_start(struct btrfs_root *root,
243 struct btrfs_block_group_cache **cache_ret,
244 u64 *start_ret, int num, int data)
247 struct btrfs_block_group_cache *cache = *cache_ret;
248 u64 last = *start_ret;
251 u64 search_start = *start_ret;
257 ret = cache_block_group(root, cache);
261 last = max(search_start, cache->key.objectid);
262 if (cache->ro || !block_group_bits(cache, data))
266 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
267 last, &start, &end, EXTENT_DIRTY);
272 start = max(last, start);
274 if (last - start < num) {
277 if (start + num > cache->key.objectid + cache->key.offset) {
285 cache = btrfs_lookup_block_group(root->fs_info, search_start);
287 printk("Unable to find block group for %llu\n",
288 (unsigned long long)search_start);
294 last = cache->key.objectid + cache->key.offset;
296 cache = btrfs_lookup_first_block_group(root->fs_info, last);
309 static int block_group_state_bits(u64 flags)
312 if (flags & BTRFS_BLOCK_GROUP_DATA)
313 bits |= BLOCK_GROUP_DATA;
314 if (flags & BTRFS_BLOCK_GROUP_METADATA)
315 bits |= BLOCK_GROUP_METADATA;
316 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
317 bits |= BLOCK_GROUP_SYSTEM;
321 static struct btrfs_block_group_cache *
322 btrfs_find_block_group(struct btrfs_root *root, struct btrfs_block_group_cache
323 *hint, u64 search_start, int data, int owner)
325 struct btrfs_block_group_cache *cache;
326 struct extent_io_tree *block_group_cache;
327 struct btrfs_block_group_cache *found_group = NULL;
328 struct btrfs_fs_info *info = root->fs_info;
341 block_group_cache = &info->block_group_cache;
346 bit = block_group_state_bits(data);
349 struct btrfs_block_group_cache *shint;
350 shint = btrfs_lookup_block_group(info, search_start);
351 if (shint && !shint->ro && block_group_bits(shint, data)) {
352 used = btrfs_block_group_used(&shint->item);
353 if (used + shint->pinned <
354 div_factor(shint->key.offset, factor)) {
359 if (hint && !hint->ro && block_group_bits(hint, data)) {
360 used = btrfs_block_group_used(&hint->item);
361 if (used + hint->pinned <
362 div_factor(hint->key.offset, factor)) {
365 last = hint->key.objectid + hint->key.offset;
369 hint_last = max(hint->key.objectid, search_start);
371 hint_last = search_start;
377 ret = find_first_extent_bit(block_group_cache, last,
382 ret = get_state_private(block_group_cache, start, &ptr);
386 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
387 last = cache->key.objectid + cache->key.offset;
388 used = btrfs_block_group_used(&cache->item);
390 if (!cache->ro && block_group_bits(cache, data)) {
392 free_check = cache->key.offset;
394 free_check = div_factor(cache->key.offset,
397 if (used + cache->pinned < free_check) {
414 * Back reference rules. Back refs have three main goals:
416 * 1) differentiate between all holders of references to an extent so that
417 * when a reference is dropped we can make sure it was a valid reference
418 * before freeing the extent.
420 * 2) Provide enough information to quickly find the holders of an extent
421 * if we notice a given block is corrupted or bad.
423 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
424 * maintenance. This is actually the same as #2, but with a slightly
425 * different use case.
427 * There are two kinds of back refs. The implicit back refs is optimized
428 * for pointers in non-shared tree blocks. For a given pointer in a block,
429 * back refs of this kind provide information about the block's owner tree
430 * and the pointer's key. These information allow us to find the block by
431 * b-tree searching. The full back refs is for pointers in tree blocks not
432 * referenced by their owner trees. The location of tree block is recorded
433 * in the back refs. Actually the full back refs is generic, and can be
434 * used in all cases the implicit back refs is used. The major shortcoming
435 * of the full back refs is its overhead. Every time a tree block gets
436 * COWed, we have to update back refs entry for all pointers in it.
438 * For a newly allocated tree block, we use implicit back refs for
439 * pointers in it. This means most tree related operations only involve
440 * implicit back refs. For a tree block created in old transaction, the
441 * only way to drop a reference to it is COW it. So we can detect the
442 * event that tree block loses its owner tree's reference and do the
443 * back refs conversion.
445 * When a tree block is COW'd through a tree, there are four cases:
447 * The reference count of the block is one and the tree is the block's
448 * owner tree. Nothing to do in this case.
450 * The reference count of the block is one and the tree is not the
451 * block's owner tree. In this case, full back refs is used for pointers
452 * in the block. Remove these full back refs, add implicit back refs for
453 * every pointers in the new block.
455 * The reference count of the block is greater than one and the tree is
456 * the block's owner tree. In this case, implicit back refs is used for
457 * pointers in the block. Add full back refs for every pointers in the
458 * block, increase lower level extents' reference counts. The original
459 * implicit back refs are entailed to the new block.
461 * The reference count of the block is greater than one and the tree is
462 * not the block's owner tree. Add implicit back refs for every pointer in
463 * the new block, increase lower level extents' reference count.
465 * Back Reference Key composing:
467 * The key objectid corresponds to the first byte in the extent,
468 * The key type is used to differentiate between types of back refs.
469 * There are different meanings of the key offset for different types
472 * File extents can be referenced by:
474 * - multiple snapshots, subvolumes, or different generations in one subvol
475 * - different files inside a single subvolume
476 * - different offsets inside a file (bookend extents in file.c)
478 * The extent ref structure for the implicit back refs has fields for:
480 * - Objectid of the subvolume root
481 * - objectid of the file holding the reference
482 * - original offset in the file
483 * - how many bookend extents
485 * The key offset for the implicit back refs is hash of the first
488 * The extent ref structure for the full back refs has field for:
490 * - number of pointers in the tree leaf
492 * The key offset for the implicit back refs is the first byte of
495 * When a file extent is allocated, The implicit back refs is used.
496 * the fields are filled in:
498 * (root_key.objectid, inode objectid, offset in file, 1)
500 * When a file extent is removed file truncation, we find the
501 * corresponding implicit back refs and check the following fields:
503 * (btrfs_header_owner(leaf), inode objectid, offset in file)
505 * Btree extents can be referenced by:
507 * - Different subvolumes
509 * Both the implicit back refs and the full back refs for tree blocks
510 * only consist of key. The key offset for the implicit back refs is
511 * objectid of block's owner tree. The key offset for the full back refs
512 * is the first byte of parent block.
514 * When implicit back refs is used, information about the lowest key and
515 * level of the tree block are required. These information are stored in
516 * tree block info structure.
519 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
520 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct btrfs_path *path,
523 u64 owner, u32 extra_size)
525 struct btrfs_extent_item *item;
526 struct btrfs_extent_item_v0 *ei0;
527 struct btrfs_extent_ref_v0 *ref0;
528 struct btrfs_tree_block_info *bi;
529 struct extent_buffer *leaf;
530 struct btrfs_key key;
531 struct btrfs_key found_key;
532 u32 new_size = sizeof(*item);
536 leaf = path->nodes[0];
537 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
539 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
540 ei0 = btrfs_item_ptr(leaf, path->slots[0],
541 struct btrfs_extent_item_v0);
542 refs = btrfs_extent_refs_v0(leaf, ei0);
544 if (owner == (u64)-1) {
546 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
547 ret = btrfs_next_leaf(root, path);
551 leaf = path->nodes[0];
553 btrfs_item_key_to_cpu(leaf, &found_key,
555 BUG_ON(key.objectid != found_key.objectid);
556 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
560 ref0 = btrfs_item_ptr(leaf, path->slots[0],
561 struct btrfs_extent_ref_v0);
562 owner = btrfs_ref_objectid_v0(leaf, ref0);
566 btrfs_release_path(path);
568 if (owner < BTRFS_FIRST_FREE_OBJECTID)
569 new_size += sizeof(*bi);
571 new_size -= sizeof(*ei0);
572 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
577 ret = btrfs_extend_item(trans, root, path, new_size);
580 leaf = path->nodes[0];
581 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
582 btrfs_set_extent_refs(leaf, item, refs);
583 /* FIXME: get real generation */
584 btrfs_set_extent_generation(leaf, item, 0);
585 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
586 btrfs_set_extent_flags(leaf, item,
587 BTRFS_EXTENT_FLAG_TREE_BLOCK |
588 BTRFS_BLOCK_FLAG_FULL_BACKREF);
589 bi = (struct btrfs_tree_block_info *)(item + 1);
590 /* FIXME: get first key of the block */
591 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
592 btrfs_set_tree_block_level(leaf, bi, (int)owner);
594 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
596 btrfs_mark_buffer_dirty(leaf);
601 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
603 u32 high_crc = ~(u32)0;
604 u32 low_crc = ~(u32)0;
607 lenum = cpu_to_le64(root_objectid);
608 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
609 lenum = cpu_to_le64(owner);
610 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
611 lenum = cpu_to_le64(offset);
612 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
614 return ((u64)high_crc << 31) ^ (u64)low_crc;
617 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
618 struct btrfs_extent_data_ref *ref)
620 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
621 btrfs_extent_data_ref_objectid(leaf, ref),
622 btrfs_extent_data_ref_offset(leaf, ref));
625 static int match_extent_data_ref(struct extent_buffer *leaf,
626 struct btrfs_extent_data_ref *ref,
627 u64 root_objectid, u64 owner, u64 offset)
629 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
630 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
631 btrfs_extent_data_ref_offset(leaf, ref) != offset)
636 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
637 struct btrfs_root *root,
638 struct btrfs_path *path,
639 u64 bytenr, u64 parent,
641 u64 owner, u64 offset)
643 struct btrfs_key key;
644 struct btrfs_extent_data_ref *ref;
645 struct extent_buffer *leaf;
651 key.objectid = bytenr;
653 key.type = BTRFS_SHARED_DATA_REF_KEY;
656 key.type = BTRFS_EXTENT_DATA_REF_KEY;
657 key.offset = hash_extent_data_ref(root_objectid,
662 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
671 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
672 key.type = BTRFS_EXTENT_REF_V0_KEY;
673 btrfs_release_path(path);
674 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
685 leaf = path->nodes[0];
686 nritems = btrfs_header_nritems(leaf);
688 if (path->slots[0] >= nritems) {
689 ret = btrfs_next_leaf(root, path);
695 leaf = path->nodes[0];
696 nritems = btrfs_header_nritems(leaf);
700 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
701 if (key.objectid != bytenr ||
702 key.type != BTRFS_EXTENT_DATA_REF_KEY)
705 ref = btrfs_item_ptr(leaf, path->slots[0],
706 struct btrfs_extent_data_ref);
708 if (match_extent_data_ref(leaf, ref, root_objectid,
711 btrfs_release_path(path);
723 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
724 struct btrfs_root *root,
725 struct btrfs_path *path,
726 u64 bytenr, u64 parent,
727 u64 root_objectid, u64 owner,
728 u64 offset, int refs_to_add)
730 struct btrfs_key key;
731 struct extent_buffer *leaf;
736 key.objectid = bytenr;
738 key.type = BTRFS_SHARED_DATA_REF_KEY;
740 size = sizeof(struct btrfs_shared_data_ref);
742 key.type = BTRFS_EXTENT_DATA_REF_KEY;
743 key.offset = hash_extent_data_ref(root_objectid,
745 size = sizeof(struct btrfs_extent_data_ref);
748 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
749 if (ret && ret != -EEXIST)
752 leaf = path->nodes[0];
754 struct btrfs_shared_data_ref *ref;
755 ref = btrfs_item_ptr(leaf, path->slots[0],
756 struct btrfs_shared_data_ref);
758 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
760 num_refs = btrfs_shared_data_ref_count(leaf, ref);
761 num_refs += refs_to_add;
762 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
765 struct btrfs_extent_data_ref *ref;
766 while (ret == -EEXIST) {
767 ref = btrfs_item_ptr(leaf, path->slots[0],
768 struct btrfs_extent_data_ref);
769 if (match_extent_data_ref(leaf, ref, root_objectid,
772 btrfs_release_path(path);
775 ret = btrfs_insert_empty_item(trans, root, path, &key,
777 if (ret && ret != -EEXIST)
780 leaf = path->nodes[0];
782 ref = btrfs_item_ptr(leaf, path->slots[0],
783 struct btrfs_extent_data_ref);
785 btrfs_set_extent_data_ref_root(leaf, ref,
787 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
788 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
789 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
791 num_refs = btrfs_extent_data_ref_count(leaf, ref);
792 num_refs += refs_to_add;
793 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
796 btrfs_mark_buffer_dirty(leaf);
799 btrfs_release_path(path);
803 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
804 struct btrfs_root *root,
805 struct btrfs_path *path,
808 struct btrfs_key key;
809 struct btrfs_extent_data_ref *ref1 = NULL;
810 struct btrfs_shared_data_ref *ref2 = NULL;
811 struct extent_buffer *leaf;
815 leaf = path->nodes[0];
816 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
818 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
819 ref1 = btrfs_item_ptr(leaf, path->slots[0],
820 struct btrfs_extent_data_ref);
821 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
822 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
823 ref2 = btrfs_item_ptr(leaf, path->slots[0],
824 struct btrfs_shared_data_ref);
825 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
826 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
827 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
828 struct btrfs_extent_ref_v0 *ref0;
829 ref0 = btrfs_item_ptr(leaf, path->slots[0],
830 struct btrfs_extent_ref_v0);
831 num_refs = btrfs_ref_count_v0(leaf, ref0);
837 BUG_ON(num_refs < refs_to_drop);
838 num_refs -= refs_to_drop;
841 ret = btrfs_del_item(trans, root, path);
843 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
844 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
845 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
846 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
847 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
849 struct btrfs_extent_ref_v0 *ref0;
850 ref0 = btrfs_item_ptr(leaf, path->slots[0],
851 struct btrfs_extent_ref_v0);
852 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
855 btrfs_mark_buffer_dirty(leaf);
860 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
861 struct btrfs_path *path,
862 struct btrfs_extent_inline_ref *iref)
864 struct btrfs_key key;
865 struct extent_buffer *leaf;
866 struct btrfs_extent_data_ref *ref1;
867 struct btrfs_shared_data_ref *ref2;
870 leaf = path->nodes[0];
871 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
873 if (btrfs_extent_inline_ref_type(leaf, iref) ==
874 BTRFS_EXTENT_DATA_REF_KEY) {
875 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
876 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
878 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
879 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
881 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
882 ref1 = btrfs_item_ptr(leaf, path->slots[0],
883 struct btrfs_extent_data_ref);
884 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
885 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
886 ref2 = btrfs_item_ptr(leaf, path->slots[0],
887 struct btrfs_shared_data_ref);
888 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
889 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
890 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
891 struct btrfs_extent_ref_v0 *ref0;
892 ref0 = btrfs_item_ptr(leaf, path->slots[0],
893 struct btrfs_extent_ref_v0);
894 num_refs = btrfs_ref_count_v0(leaf, ref0);
902 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
903 struct btrfs_root *root,
904 struct btrfs_path *path,
905 u64 bytenr, u64 parent,
908 struct btrfs_key key;
911 key.objectid = bytenr;
913 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
916 key.type = BTRFS_TREE_BLOCK_REF_KEY;
917 key.offset = root_objectid;
920 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
923 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
924 if (ret == -ENOENT && parent) {
925 btrfs_release_path(path);
926 key.type = BTRFS_EXTENT_REF_V0_KEY;
927 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
935 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
936 struct btrfs_root *root,
937 struct btrfs_path *path,
938 u64 bytenr, u64 parent,
941 struct btrfs_key key;
944 key.objectid = bytenr;
946 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
949 key.type = BTRFS_TREE_BLOCK_REF_KEY;
950 key.offset = root_objectid;
953 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
955 btrfs_release_path(path);
959 static inline int extent_ref_type(u64 parent, u64 owner)
962 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
964 type = BTRFS_SHARED_BLOCK_REF_KEY;
966 type = BTRFS_TREE_BLOCK_REF_KEY;
969 type = BTRFS_SHARED_DATA_REF_KEY;
971 type = BTRFS_EXTENT_DATA_REF_KEY;
976 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
977 struct btrfs_root *root,
978 struct btrfs_path *path,
979 struct btrfs_extent_inline_ref **ref_ret,
980 u64 bytenr, u64 num_bytes,
981 u64 parent, u64 root_objectid,
982 u64 owner, u64 offset, int insert)
984 struct btrfs_key key;
985 struct extent_buffer *leaf;
986 struct btrfs_extent_item *ei;
987 struct btrfs_extent_inline_ref *iref;
997 int skinny_metadata =
998 btrfs_fs_incompat(root->fs_info,
999 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1001 key.objectid = bytenr;
1002 key.type = BTRFS_EXTENT_ITEM_KEY;
1003 key.offset = num_bytes;
1005 want = extent_ref_type(parent, owner);
1007 extra_size = btrfs_extent_inline_ref_size(want);
1011 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1012 skinny_metadata = 1;
1013 key.type = BTRFS_METADATA_ITEM_KEY;
1015 } else if (skinny_metadata) {
1016 skinny_metadata = 0;
1020 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1027 * We may be a newly converted file system which still has the old fat
1028 * extent entries for metadata, so try and see if we have one of those.
1030 if (ret > 0 && skinny_metadata) {
1031 skinny_metadata = 0;
1032 if (path->slots[0]) {
1034 btrfs_item_key_to_cpu(path->nodes[0], &key,
1036 if (key.objectid == bytenr &&
1037 key.type == BTRFS_EXTENT_ITEM_KEY &&
1038 key.offset == num_bytes)
1042 key.type = BTRFS_EXTENT_ITEM_KEY;
1043 key.offset = num_bytes;
1044 btrfs_release_path(path);
1050 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1056 leaf = path->nodes[0];
1057 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1058 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1059 if (item_size < sizeof(*ei)) {
1064 ret = convert_extent_item_v0(trans, root, path, owner,
1070 leaf = path->nodes[0];
1071 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1074 if (item_size < sizeof(*ei)) {
1075 printf("Size is %u, needs to be %u, slot %d\n",
1076 (unsigned)item_size,
1077 (unsigned)sizeof(*ei), path->slots[0]);
1078 btrfs_print_leaf(root, leaf);
1081 BUG_ON(item_size < sizeof(*ei));
1083 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1084 flags = btrfs_extent_flags(leaf, ei);
1086 ptr = (unsigned long)(ei + 1);
1087 end = (unsigned long)ei + item_size;
1089 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1090 ptr += sizeof(struct btrfs_tree_block_info);
1092 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1093 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1104 iref = (struct btrfs_extent_inline_ref *)ptr;
1105 type = btrfs_extent_inline_ref_type(leaf, iref);
1109 ptr += btrfs_extent_inline_ref_size(type);
1113 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1114 struct btrfs_extent_data_ref *dref;
1115 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1116 if (match_extent_data_ref(leaf, dref, root_objectid,
1121 if (hash_extent_data_ref_item(leaf, dref) <
1122 hash_extent_data_ref(root_objectid, owner, offset))
1126 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1128 if (parent == ref_offset) {
1132 if (ref_offset < parent)
1135 if (root_objectid == ref_offset) {
1139 if (ref_offset < root_objectid)
1143 ptr += btrfs_extent_inline_ref_size(type);
1145 if (err == -ENOENT && insert) {
1146 if (item_size + extra_size >=
1147 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1152 * To add new inline back ref, we have to make sure
1153 * there is no corresponding back ref item.
1154 * For simplicity, we just do not add new inline back
1155 * ref if there is any back ref item.
1157 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1158 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1163 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1168 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root,
1170 struct btrfs_path *path,
1171 struct btrfs_extent_inline_ref *iref,
1172 u64 parent, u64 root_objectid,
1173 u64 owner, u64 offset, int refs_to_add)
1175 struct extent_buffer *leaf;
1176 struct btrfs_extent_item *ei;
1179 unsigned long item_offset;
1185 leaf = path->nodes[0];
1186 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1187 item_offset = (unsigned long)iref - (unsigned long)ei;
1189 type = extent_ref_type(parent, owner);
1190 size = btrfs_extent_inline_ref_size(type);
1192 ret = btrfs_extend_item(trans, root, path, size);
1195 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1196 refs = btrfs_extent_refs(leaf, ei);
1197 refs += refs_to_add;
1198 btrfs_set_extent_refs(leaf, ei, refs);
1200 ptr = (unsigned long)ei + item_offset;
1201 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1202 if (ptr < end - size)
1203 memmove_extent_buffer(leaf, ptr + size, ptr,
1206 iref = (struct btrfs_extent_inline_ref *)ptr;
1207 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1208 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1209 struct btrfs_extent_data_ref *dref;
1210 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1211 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1212 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1213 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1214 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1215 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1216 struct btrfs_shared_data_ref *sref;
1217 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1218 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1219 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1220 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1221 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1223 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1225 btrfs_mark_buffer_dirty(leaf);
1229 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1230 struct btrfs_root *root,
1231 struct btrfs_path *path,
1232 struct btrfs_extent_inline_ref **ref_ret,
1233 u64 bytenr, u64 num_bytes, u64 parent,
1234 u64 root_objectid, u64 owner, u64 offset)
1238 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1239 bytenr, num_bytes, parent,
1240 root_objectid, owner, offset, 0);
1244 btrfs_release_path(path);
1247 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1248 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1251 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1252 root_objectid, owner, offset);
1257 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1258 struct btrfs_root *root,
1259 struct btrfs_path *path,
1260 struct btrfs_extent_inline_ref *iref,
1263 struct extent_buffer *leaf;
1264 struct btrfs_extent_item *ei;
1265 struct btrfs_extent_data_ref *dref = NULL;
1266 struct btrfs_shared_data_ref *sref = NULL;
1275 leaf = path->nodes[0];
1276 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1277 refs = btrfs_extent_refs(leaf, ei);
1278 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1279 refs += refs_to_mod;
1280 btrfs_set_extent_refs(leaf, ei, refs);
1282 type = btrfs_extent_inline_ref_type(leaf, iref);
1284 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1285 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1286 refs = btrfs_extent_data_ref_count(leaf, dref);
1287 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1288 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1289 refs = btrfs_shared_data_ref_count(leaf, sref);
1292 BUG_ON(refs_to_mod != -1);
1295 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1296 refs += refs_to_mod;
1299 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1300 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1302 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1304 size = btrfs_extent_inline_ref_size(type);
1305 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1306 ptr = (unsigned long)iref;
1307 end = (unsigned long)ei + item_size;
1308 if (ptr + size < end)
1309 memmove_extent_buffer(leaf, ptr, ptr + size,
1312 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1315 btrfs_mark_buffer_dirty(leaf);
1319 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1320 struct btrfs_root *root,
1321 struct btrfs_path *path,
1322 u64 bytenr, u64 num_bytes, u64 parent,
1323 u64 root_objectid, u64 owner,
1324 u64 offset, int refs_to_add)
1326 struct btrfs_extent_inline_ref *iref;
1329 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1330 bytenr, num_bytes, parent,
1331 root_objectid, owner, offset, 1);
1333 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1334 ret = update_inline_extent_backref(trans, root, path, iref,
1336 } else if (ret == -ENOENT) {
1337 ret = setup_inline_extent_backref(trans, root, path, iref,
1338 parent, root_objectid,
1339 owner, offset, refs_to_add);
1344 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1345 struct btrfs_root *root,
1346 struct btrfs_path *path,
1347 u64 bytenr, u64 parent, u64 root_objectid,
1348 u64 owner, u64 offset, int refs_to_add)
1352 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1353 ret = insert_extent_data_ref(trans, root, path, bytenr,
1354 parent, root_objectid,
1355 owner, offset, refs_to_add);
1357 BUG_ON(refs_to_add != 1);
1358 ret = insert_tree_block_ref(trans, root, path, bytenr,
1359 parent, root_objectid);
1364 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root,
1366 struct btrfs_path *path,
1367 struct btrfs_extent_inline_ref *iref,
1368 int refs_to_drop, int is_data)
1372 BUG_ON(!is_data && refs_to_drop != 1);
1374 ret = update_inline_extent_backref(trans, root, path, iref,
1376 } else if (is_data) {
1377 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1379 ret = btrfs_del_item(trans, root, path);
1384 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1385 struct btrfs_root *root,
1386 u64 bytenr, u64 num_bytes, u64 parent,
1387 u64 root_objectid, u64 owner, u64 offset)
1389 struct btrfs_path *path;
1390 struct extent_buffer *leaf;
1391 struct btrfs_extent_item *item;
1396 path = btrfs_alloc_path();
1402 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1403 path, bytenr, num_bytes, parent,
1404 root_objectid, owner, offset, 1);
1408 if (ret != -EAGAIN) {
1413 leaf = path->nodes[0];
1414 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1415 refs = btrfs_extent_refs(leaf, item);
1416 btrfs_set_extent_refs(leaf, item, refs + 1);
1418 btrfs_mark_buffer_dirty(leaf);
1419 btrfs_release_path(path);
1423 /* now insert the actual backref */
1424 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1425 path, bytenr, parent, root_objectid,
1430 btrfs_free_path(path);
1431 finish_current_insert(trans, root->fs_info->extent_root);
1432 del_pending_extents(trans, root->fs_info->extent_root);
1437 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root)
1440 finish_current_insert(trans, root->fs_info->extent_root);
1441 del_pending_extents(trans, root->fs_info->extent_root);
1445 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root, u64 bytenr,
1447 u64 offset, int metadata, u64 *refs, u64 *flags)
1449 struct btrfs_path *path;
1451 struct btrfs_key key;
1452 struct extent_buffer *l;
1453 struct btrfs_extent_item *item;
1459 !btrfs_fs_incompat(root->fs_info,
1460 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1461 offset = root->leafsize;
1465 path = btrfs_alloc_path();
1470 key.objectid = bytenr;
1471 key.offset = offset;
1473 key.type = BTRFS_METADATA_ITEM_KEY;
1475 key.type = BTRFS_EXTENT_ITEM_KEY;
1478 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1484 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1485 * we didn't find what we wanted check and see if we have a normal ref
1486 * right next to us, or re-search if we are on the edge of the leaf just
1489 if (ret > 0 && metadata) {
1490 if (path->slots[0]) {
1492 btrfs_item_key_to_cpu(path->nodes[0], &key,
1494 if (key.objectid == bytenr &&
1495 key.type == BTRFS_EXTENT_ITEM_KEY &&
1496 key.offset == root->leafsize)
1501 btrfs_release_path(path);
1502 key.type = BTRFS_EXTENT_ITEM_KEY;
1503 key.offset = root->leafsize;
1515 item_size = btrfs_item_size_nr(l, path->slots[0]);
1516 if (item_size >= sizeof(*item)) {
1517 item = btrfs_item_ptr(l, path->slots[0],
1518 struct btrfs_extent_item);
1519 num_refs = btrfs_extent_refs(l, item);
1520 extent_flags = btrfs_extent_flags(l, item);
1522 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1523 struct btrfs_extent_item_v0 *ei0;
1524 BUG_ON(item_size != sizeof(*ei0));
1525 ei0 = btrfs_item_ptr(l, path->slots[0],
1526 struct btrfs_extent_item_v0);
1527 num_refs = btrfs_extent_refs_v0(l, ei0);
1528 /* FIXME: this isn't correct for data */
1529 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1534 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1538 *flags = extent_flags;
1540 btrfs_free_path(path);
1544 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1545 struct btrfs_root *root,
1546 u64 bytenr, int level, u64 flags)
1548 struct btrfs_path *path;
1550 struct btrfs_key key;
1551 struct extent_buffer *l;
1552 struct btrfs_extent_item *item;
1554 int skinny_metadata =
1555 btrfs_fs_incompat(root->fs_info,
1556 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1558 path = btrfs_alloc_path();
1563 key.objectid = bytenr;
1564 if (skinny_metadata) {
1566 key.type = BTRFS_METADATA_ITEM_KEY;
1568 key.offset = root->leafsize;
1569 key.type = BTRFS_EXTENT_ITEM_KEY;
1573 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1578 if (ret > 0 && skinny_metadata) {
1579 skinny_metadata = 0;
1580 if (path->slots[0]) {
1582 btrfs_item_key_to_cpu(path->nodes[0], &key,
1584 if (key.objectid == bytenr &&
1585 key.offset == root->leafsize &&
1586 key.type == BTRFS_EXTENT_ITEM_KEY)
1590 btrfs_release_path(path);
1591 key.offset = root->leafsize;
1592 key.type = BTRFS_EXTENT_ITEM_KEY;
1598 btrfs_print_leaf(root, path->nodes[0]);
1599 printk("failed to find block number %Lu\n",
1600 (unsigned long long)bytenr);
1604 item_size = btrfs_item_size_nr(l, path->slots[0]);
1605 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1606 if (item_size < sizeof(*item)) {
1607 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1613 item_size = btrfs_item_size_nr(l, path->slots[0]);
1616 BUG_ON(item_size < sizeof(*item));
1617 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1618 flags |= btrfs_extent_flags(l, item);
1619 btrfs_set_extent_flags(l, item, flags);
1621 btrfs_free_path(path);
1622 finish_current_insert(trans, root->fs_info->extent_root);
1623 del_pending_extents(trans, root->fs_info->extent_root);
1627 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1628 struct btrfs_root *root,
1629 struct extent_buffer *buf,
1630 int record_parent, int inc)
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1642 int (*process_func)(struct btrfs_trans_handle *trans,
1643 struct btrfs_root *root,
1644 u64, u64, u64, u64, u64, u64);
1646 ref_root = btrfs_header_owner(buf);
1647 nritems = btrfs_header_nritems(buf);
1648 level = btrfs_header_level(buf);
1650 if (!root->ref_cows && level == 0)
1654 process_func = btrfs_inc_extent_ref;
1656 process_func = btrfs_free_extent;
1659 parent = buf->start;
1663 for (i = 0; i < nritems; i++) {
1666 btrfs_item_key_to_cpu(buf, &key, i);
1667 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1669 fi = btrfs_item_ptr(buf, i,
1670 struct btrfs_file_extent_item);
1671 if (btrfs_file_extent_type(buf, fi) ==
1672 BTRFS_FILE_EXTENT_INLINE)
1674 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1678 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1679 key.offset -= btrfs_file_extent_offset(buf, fi);
1680 ret = process_func(trans, root, bytenr, num_bytes,
1681 parent, ref_root, key.objectid,
1688 bytenr = btrfs_node_blockptr(buf, i);
1689 num_bytes = btrfs_level_size(root, level - 1);
1690 ret = process_func(trans, root, bytenr, num_bytes,
1691 parent, ref_root, level - 1, 0);
1704 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1705 struct extent_buffer *buf, int record_parent)
1707 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1710 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1711 struct extent_buffer *buf, int record_parent)
1713 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1716 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1717 struct btrfs_root *root,
1718 struct btrfs_path *path,
1719 struct btrfs_block_group_cache *cache)
1723 struct btrfs_root *extent_root = root->fs_info->extent_root;
1725 struct extent_buffer *leaf;
1727 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1732 leaf = path->nodes[0];
1733 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1734 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1735 btrfs_mark_buffer_dirty(leaf);
1736 btrfs_release_path(path);
1738 finish_current_insert(trans, extent_root);
1739 pending_ret = del_pending_extents(trans, extent_root);
1748 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1749 struct btrfs_root *root)
1751 struct extent_io_tree *block_group_cache;
1752 struct btrfs_block_group_cache *cache;
1754 struct btrfs_path *path;
1760 block_group_cache = &root->fs_info->block_group_cache;
1761 path = btrfs_alloc_path();
1766 ret = find_first_extent_bit(block_group_cache, last,
1767 &start, &end, BLOCK_GROUP_DIRTY);
1776 ret = get_state_private(block_group_cache, start, &ptr);
1779 clear_extent_bits(block_group_cache, start, end,
1780 BLOCK_GROUP_DIRTY, GFP_NOFS);
1782 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1783 ret = write_one_cache_group(trans, root, path, cache);
1785 btrfs_free_path(path);
1789 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1792 struct list_head *head = &info->space_info;
1793 struct list_head *cur;
1794 struct btrfs_space_info *found;
1795 list_for_each(cur, head) {
1796 found = list_entry(cur, struct btrfs_space_info, list);
1797 if (found->flags & flags)
1804 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1805 u64 total_bytes, u64 bytes_used,
1806 struct btrfs_space_info **space_info)
1808 struct btrfs_space_info *found;
1810 found = __find_space_info(info, flags);
1812 found->total_bytes += total_bytes;
1813 found->bytes_used += bytes_used;
1814 if (found->total_bytes < found->bytes_used) {
1815 fprintf(stderr, "warning, bad space info total_bytes "
1817 (unsigned long long)found->total_bytes,
1818 (unsigned long long)found->bytes_used);
1820 *space_info = found;
1823 found = kmalloc(sizeof(*found), GFP_NOFS);
1827 list_add(&found->list, &info->space_info);
1828 found->flags = flags;
1829 found->total_bytes = total_bytes;
1830 found->bytes_used = bytes_used;
1831 found->bytes_pinned = 0;
1833 *space_info = found;
1838 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1840 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1841 BTRFS_BLOCK_GROUP_RAID1 |
1842 BTRFS_BLOCK_GROUP_RAID10 |
1843 BTRFS_BLOCK_GROUP_RAID5 |
1844 BTRFS_BLOCK_GROUP_RAID6 |
1845 BTRFS_BLOCK_GROUP_DUP);
1847 if (flags & BTRFS_BLOCK_GROUP_DATA)
1848 fs_info->avail_data_alloc_bits |= extra_flags;
1849 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1850 fs_info->avail_metadata_alloc_bits |= extra_flags;
1851 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1852 fs_info->avail_system_alloc_bits |= extra_flags;
1856 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *extent_root, u64 alloc_bytes,
1860 struct btrfs_space_info *space_info;
1866 space_info = __find_space_info(extent_root->fs_info, flags);
1868 ret = update_space_info(extent_root->fs_info, flags,
1872 BUG_ON(!space_info);
1874 if (space_info->full)
1877 thresh = div_factor(space_info->total_bytes, 7);
1878 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1882 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1884 if (ret == -ENOSPC) {
1885 space_info->full = 1;
1891 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1892 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1897 static int update_block_group(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root,
1899 u64 bytenr, u64 num_bytes, int alloc,
1902 struct btrfs_block_group_cache *cache;
1903 struct btrfs_fs_info *info = root->fs_info;
1904 u64 total = num_bytes;
1910 /* block accounting for super block */
1911 old_val = btrfs_super_bytes_used(info->super_copy);
1913 old_val += num_bytes;
1915 old_val -= num_bytes;
1916 btrfs_set_super_bytes_used(info->super_copy, old_val);
1918 /* block accounting for root item */
1919 old_val = btrfs_root_used(&root->root_item);
1921 old_val += num_bytes;
1923 old_val -= num_bytes;
1924 btrfs_set_root_used(&root->root_item, old_val);
1927 cache = btrfs_lookup_block_group(info, bytenr);
1931 byte_in_group = bytenr - cache->key.objectid;
1932 WARN_ON(byte_in_group > cache->key.offset);
1933 start = cache->key.objectid;
1934 end = start + cache->key.offset - 1;
1935 set_extent_bits(&info->block_group_cache, start, end,
1936 BLOCK_GROUP_DIRTY, GFP_NOFS);
1938 old_val = btrfs_block_group_used(&cache->item);
1939 num_bytes = min(total, cache->key.offset - byte_in_group);
1942 old_val += num_bytes;
1943 cache->space_info->bytes_used += num_bytes;
1945 old_val -= num_bytes;
1946 cache->space_info->bytes_used -= num_bytes;
1948 set_extent_dirty(&info->free_space_cache,
1949 bytenr, bytenr + num_bytes - 1,
1953 btrfs_set_block_group_used(&cache->item, old_val);
1955 bytenr += num_bytes;
1960 static int update_pinned_extents(struct btrfs_root *root,
1961 u64 bytenr, u64 num, int pin)
1964 struct btrfs_block_group_cache *cache;
1965 struct btrfs_fs_info *fs_info = root->fs_info;
1968 set_extent_dirty(&fs_info->pinned_extents,
1969 bytenr, bytenr + num - 1, GFP_NOFS);
1971 clear_extent_dirty(&fs_info->pinned_extents,
1972 bytenr, bytenr + num - 1, GFP_NOFS);
1975 cache = btrfs_lookup_block_group(fs_info, bytenr);
1977 len = min((u64)root->sectorsize, num);
1981 len = min(num, cache->key.offset -
1982 (bytenr - cache->key.objectid));
1984 cache->pinned += len;
1985 cache->space_info->bytes_pinned += len;
1986 fs_info->total_pinned += len;
1988 cache->pinned -= len;
1989 cache->space_info->bytes_pinned -= len;
1990 fs_info->total_pinned -= len;
1999 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2001 struct extent_io_tree *unpin)
2006 struct extent_io_tree *free_space_cache;
2007 free_space_cache = &root->fs_info->free_space_cache;
2010 ret = find_first_extent_bit(unpin, 0, &start, &end,
2014 update_pinned_extents(root, start, end + 1 - start, 0);
2015 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2016 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2021 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2027 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2028 &end, EXTENT_LOCKED);
2030 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2036 static int finish_current_insert(struct btrfs_trans_handle *trans,
2037 struct btrfs_root *extent_root)
2042 struct btrfs_fs_info *info = extent_root->fs_info;
2043 struct pending_extent_op *extent_op;
2044 struct btrfs_key key;
2046 int skinny_metadata =
2047 btrfs_fs_incompat(extent_root->fs_info,
2048 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2051 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2052 &end, EXTENT_LOCKED);
2056 ret = get_state_private(&info->extent_ins, start, &priv);
2058 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2060 if (extent_op->type == PENDING_EXTENT_INSERT) {
2061 key.objectid = start;
2062 if (skinny_metadata) {
2063 key.offset = extent_op->level;
2064 key.type = BTRFS_METADATA_ITEM_KEY;
2066 key.offset = extent_op->num_bytes;
2067 key.type = BTRFS_EXTENT_ITEM_KEY;
2069 ret = alloc_reserved_tree_block(trans, extent_root,
2070 extent_root->root_key.objectid,
2074 extent_op->level, &key);
2080 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2087 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2088 struct btrfs_root *root,
2089 u64 bytenr, u64 num_bytes, int is_data)
2092 struct extent_buffer *buf;
2097 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2101 /* we can reuse a block if it hasn't been written
2102 * and it is from this transaction. We can't
2103 * reuse anything from the tree log root because
2104 * it has tiny sub-transactions.
2106 if (btrfs_buffer_uptodate(buf, 0)) {
2107 u64 header_owner = btrfs_header_owner(buf);
2108 u64 header_transid = btrfs_header_generation(buf);
2109 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2110 header_transid == trans->transid &&
2111 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2112 clean_tree_block(NULL, root, buf);
2113 free_extent_buffer(buf);
2117 free_extent_buffer(buf);
2119 update_pinned_extents(root, bytenr, num_bytes, 1);
2125 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2126 u64 bytenr, u64 num_bytes)
2128 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2131 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2132 u64 bytenr, u64 num_bytes)
2134 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2138 * remove an extent from the root, returns 0 on success
2140 static int __free_extent(struct btrfs_trans_handle *trans,
2141 struct btrfs_root *root,
2142 u64 bytenr, u64 num_bytes, u64 parent,
2143 u64 root_objectid, u64 owner_objectid,
2144 u64 owner_offset, int refs_to_drop)
2147 struct btrfs_key key;
2148 struct btrfs_path *path;
2149 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2150 struct btrfs_root *extent_root = root->fs_info->extent_root;
2151 struct extent_buffer *leaf;
2152 struct btrfs_extent_item *ei;
2153 struct btrfs_extent_inline_ref *iref;
2156 int extent_slot = 0;
2157 int found_extent = 0;
2161 int skinny_metadata =
2162 btrfs_fs_incompat(extent_root->fs_info,
2163 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2165 if (root->fs_info->free_extent_hook) {
2166 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2167 parent, root_objectid, owner_objectid,
2168 owner_offset, refs_to_drop);
2171 path = btrfs_alloc_path();
2177 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2179 skinny_metadata = 0;
2180 BUG_ON(!is_data && refs_to_drop != 1);
2182 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2183 bytenr, num_bytes, parent,
2184 root_objectid, owner_objectid,
2187 extent_slot = path->slots[0];
2188 while (extent_slot >= 0) {
2189 btrfs_item_key_to_cpu(path->nodes[0], &key,
2191 if (key.objectid != bytenr)
2193 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2194 key.offset == num_bytes) {
2198 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2199 key.offset == owner_objectid) {
2203 if (path->slots[0] - extent_slot > 5)
2207 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2208 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2209 if (found_extent && item_size < sizeof(*ei))
2212 if (!found_extent) {
2214 ret = remove_extent_backref(trans, extent_root, path,
2218 btrfs_release_path(path);
2220 key.objectid = bytenr;
2222 if (skinny_metadata) {
2223 key.type = BTRFS_METADATA_ITEM_KEY;
2224 key.offset = owner_objectid;
2226 key.type = BTRFS_EXTENT_ITEM_KEY;
2227 key.offset = num_bytes;
2230 ret = btrfs_search_slot(trans, extent_root,
2232 if (ret > 0 && skinny_metadata && path->slots[0]) {
2234 btrfs_item_key_to_cpu(path->nodes[0],
2237 if (key.objectid == bytenr &&
2238 key.type == BTRFS_EXTENT_ITEM_KEY &&
2239 key.offset == num_bytes)
2243 if (ret > 0 && skinny_metadata) {
2244 skinny_metadata = 0;
2245 btrfs_release_path(path);
2246 key.type = BTRFS_EXTENT_ITEM_KEY;
2247 key.offset = num_bytes;
2248 ret = btrfs_search_slot(trans, extent_root,
2253 printk(KERN_ERR "umm, got %d back from search"
2254 ", was looking for %llu\n", ret,
2255 (unsigned long long)bytenr);
2256 btrfs_print_leaf(extent_root, path->nodes[0]);
2259 extent_slot = path->slots[0];
2262 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2263 "parent %llu root %llu owner %llu offset %llu\n",
2264 (unsigned long long)bytenr,
2265 (unsigned long long)parent,
2266 (unsigned long long)root_objectid,
2267 (unsigned long long)owner_objectid,
2268 (unsigned long long)owner_offset);
2273 leaf = path->nodes[0];
2274 item_size = btrfs_item_size_nr(leaf, extent_slot);
2275 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2276 if (item_size < sizeof(*ei)) {
2277 BUG_ON(found_extent || extent_slot != path->slots[0]);
2278 ret = convert_extent_item_v0(trans, extent_root, path,
2282 btrfs_release_path(path);
2284 key.objectid = bytenr;
2285 key.type = BTRFS_EXTENT_ITEM_KEY;
2286 key.offset = num_bytes;
2288 ret = btrfs_search_slot(trans, extent_root, &key, path,
2291 printk(KERN_ERR "umm, got %d back from search"
2292 ", was looking for %llu\n", ret,
2293 (unsigned long long)bytenr);
2294 btrfs_print_leaf(extent_root, path->nodes[0]);
2297 extent_slot = path->slots[0];
2298 leaf = path->nodes[0];
2299 item_size = btrfs_item_size_nr(leaf, extent_slot);
2302 BUG_ON(item_size < sizeof(*ei));
2303 ei = btrfs_item_ptr(leaf, extent_slot,
2304 struct btrfs_extent_item);
2305 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2306 key.type == BTRFS_EXTENT_ITEM_KEY) {
2307 struct btrfs_tree_block_info *bi;
2308 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2309 bi = (struct btrfs_tree_block_info *)(ei + 1);
2310 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2313 refs = btrfs_extent_refs(leaf, ei);
2314 BUG_ON(refs < refs_to_drop);
2315 refs -= refs_to_drop;
2319 * In the case of inline back ref, reference count will
2320 * be updated by remove_extent_backref
2323 BUG_ON(!found_extent);
2325 btrfs_set_extent_refs(leaf, ei, refs);
2326 btrfs_mark_buffer_dirty(leaf);
2329 ret = remove_extent_backref(trans, extent_root, path,
2339 BUG_ON(is_data && refs_to_drop !=
2340 extent_data_ref_count(root, path, iref));
2342 BUG_ON(path->slots[0] != extent_slot);
2344 BUG_ON(path->slots[0] != extent_slot + 1);
2345 path->slots[0] = extent_slot;
2350 if (ops && ops->free_extent) {
2351 ret = ops->free_extent(root, bytenr, num_bytes);
2359 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2366 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2369 btrfs_release_path(path);
2372 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2376 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2379 btrfs_free_path(path);
2380 finish_current_insert(trans, extent_root);
2385 * find all the blocks marked as pending in the radix tree and remove
2386 * them from the extent map
2388 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2389 btrfs_root *extent_root)
2396 struct extent_io_tree *pending_del;
2397 struct extent_io_tree *extent_ins;
2398 struct pending_extent_op *extent_op;
2400 extent_ins = &extent_root->fs_info->extent_ins;
2401 pending_del = &extent_root->fs_info->pending_del;
2404 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2409 ret = get_state_private(pending_del, start, &priv);
2411 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2413 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2416 if (!test_range_bit(extent_ins, start, end,
2417 EXTENT_LOCKED, 0)) {
2418 ret = __free_extent(trans, extent_root,
2419 start, end + 1 - start, 0,
2420 extent_root->root_key.objectid,
2421 extent_op->level, 0, 1);
2425 ret = get_state_private(extent_ins, start, &priv);
2427 extent_op = (struct pending_extent_op *)
2428 (unsigned long)priv;
2430 clear_extent_bits(extent_ins, start, end,
2431 EXTENT_LOCKED, GFP_NOFS);
2433 if (extent_op->type == PENDING_BACKREF_UPDATE)
2445 * remove an extent from the root, returns 0 on success
2448 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2449 struct btrfs_root *root,
2450 u64 bytenr, u64 num_bytes, u64 parent,
2451 u64 root_objectid, u64 owner, u64 offset)
2453 struct btrfs_root *extent_root = root->fs_info->extent_root;
2457 WARN_ON(num_bytes < root->sectorsize);
2458 if (root == extent_root) {
2459 struct pending_extent_op *extent_op;
2461 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2464 extent_op->type = PENDING_EXTENT_DELETE;
2465 extent_op->bytenr = bytenr;
2466 extent_op->num_bytes = num_bytes;
2467 extent_op->level = (int)owner;
2469 set_extent_bits(&root->fs_info->pending_del,
2470 bytenr, bytenr + num_bytes - 1,
2471 EXTENT_LOCKED, GFP_NOFS);
2472 set_state_private(&root->fs_info->pending_del,
2473 bytenr, (unsigned long)extent_op);
2476 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2477 root_objectid, owner, offset, 1);
2478 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2479 return ret ? ret : pending_ret;
2482 static u64 stripe_align(struct btrfs_root *root, u64 val)
2484 u64 mask = ((u64)root->stripesize - 1);
2485 u64 ret = (val + mask) & ~mask;
2490 * walks the btree of allocated extents and find a hole of a given size.
2491 * The key ins is changed to record the hole:
2492 * ins->objectid == block start
2493 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2494 * ins->offset == number of blocks
2495 * Any available blocks before search_start are skipped.
2497 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *orig_root,
2499 u64 num_bytes, u64 empty_size,
2500 u64 search_start, u64 search_end,
2501 u64 hint_byte, struct btrfs_key *ins,
2502 u64 exclude_start, u64 exclude_nr,
2506 u64 orig_search_start = search_start;
2507 struct btrfs_root * root = orig_root->fs_info->extent_root;
2508 struct btrfs_fs_info *info = root->fs_info;
2509 u64 total_needed = num_bytes;
2510 struct btrfs_block_group_cache *block_group;
2514 WARN_ON(num_bytes < root->sectorsize);
2515 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2517 search_start = stripe_align(root, search_start);
2520 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2522 hint_byte = search_start;
2523 block_group = btrfs_find_block_group(root, block_group,
2524 hint_byte, data, 1);
2526 block_group = btrfs_find_block_group(root,
2528 search_start, data, 1);
2531 total_needed += empty_size;
2534 search_start = stripe_align(root, search_start);
2536 block_group = btrfs_lookup_first_block_group(info,
2539 block_group = btrfs_lookup_first_block_group(info,
2542 ret = find_search_start(root, &block_group, &search_start,
2543 total_needed, data);
2547 ins->objectid = search_start;
2548 ins->offset = num_bytes;
2550 if (ins->objectid + num_bytes >
2551 block_group->key.objectid + block_group->key.offset) {
2552 search_start = block_group->key.objectid +
2553 block_group->key.offset;
2557 if (test_range_bit(&info->extent_ins, ins->objectid,
2558 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2559 search_start = ins->objectid + num_bytes;
2563 if (test_range_bit(&info->pinned_extents, ins->objectid,
2564 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2565 search_start = ins->objectid + num_bytes;
2569 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2570 ins->objectid < exclude_start + exclude_nr)) {
2571 search_start = exclude_start + exclude_nr;
2575 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2576 block_group = btrfs_lookup_block_group(info, ins->objectid);
2578 trans->block_group = block_group;
2580 ins->offset = num_bytes;
2584 block_group = btrfs_lookup_first_block_group(info, search_start);
2586 search_start = orig_search_start;
2593 total_needed -= empty_size;
2599 block_group = btrfs_find_block_group(root, block_group,
2600 search_start, data, 0);
2607 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2608 struct btrfs_root *root,
2609 u64 num_bytes, u64 empty_size,
2610 u64 hint_byte, u64 search_end,
2611 struct btrfs_key *ins, int data)
2614 u64 search_start = 0;
2616 struct btrfs_fs_info *info = root->fs_info;
2618 if (info->extent_ops) {
2619 struct btrfs_extent_ops *ops = info->extent_ops;
2620 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2626 alloc_profile = info->avail_data_alloc_bits &
2627 info->data_alloc_profile;
2628 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2629 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2630 info->system_allocs >= 0) {
2631 alloc_profile = info->avail_system_alloc_bits &
2632 info->system_alloc_profile;
2633 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2635 alloc_profile = info->avail_metadata_alloc_bits &
2636 info->metadata_alloc_profile;
2637 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2640 if (root->ref_cows) {
2641 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2642 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2644 BTRFS_BLOCK_GROUP_METADATA);
2647 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2648 num_bytes + 2 * 1024 * 1024, data);
2652 WARN_ON(num_bytes < root->sectorsize);
2653 ret = find_free_extent(trans, root, num_bytes, empty_size,
2654 search_start, search_end, hint_byte, ins,
2655 trans->alloc_exclude_start,
2656 trans->alloc_exclude_nr, data);
2659 clear_extent_dirty(&root->fs_info->free_space_cache,
2660 ins->objectid, ins->objectid + ins->offset - 1,
2665 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2666 struct btrfs_root *root,
2667 u64 root_objectid, u64 generation,
2668 u64 flags, struct btrfs_disk_key *key,
2669 int level, struct btrfs_key *ins)
2672 struct btrfs_fs_info *fs_info = root->fs_info;
2673 struct btrfs_extent_item *extent_item;
2674 struct btrfs_tree_block_info *block_info;
2675 struct btrfs_extent_inline_ref *iref;
2676 struct btrfs_path *path;
2677 struct extent_buffer *leaf;
2678 u32 size = sizeof(*extent_item) + sizeof(*iref);
2679 int skinny_metadata =
2680 btrfs_fs_incompat(fs_info,
2681 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2683 if (!skinny_metadata)
2684 size += sizeof(*block_info);
2686 path = btrfs_alloc_path();
2689 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2693 leaf = path->nodes[0];
2694 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2695 struct btrfs_extent_item);
2696 btrfs_set_extent_refs(leaf, extent_item, 1);
2697 btrfs_set_extent_generation(leaf, extent_item, generation);
2698 btrfs_set_extent_flags(leaf, extent_item,
2699 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2701 if (skinny_metadata) {
2702 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2704 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2705 btrfs_set_tree_block_key(leaf, block_info, key);
2706 btrfs_set_tree_block_level(leaf, block_info, level);
2707 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2710 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2711 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2713 btrfs_mark_buffer_dirty(leaf);
2714 btrfs_free_path(path);
2716 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2721 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *root, u64 num_bytes,
2723 u64 root_objectid, u64 generation,
2724 u64 flags, struct btrfs_disk_key *key,
2725 int level, u64 empty_size, u64 hint_byte,
2726 u64 search_end, struct btrfs_key *ins)
2729 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2730 hint_byte, search_end, ins, 0);
2733 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2734 struct pending_extent_op *extent_op;
2736 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2739 extent_op->type = PENDING_EXTENT_INSERT;
2740 extent_op->bytenr = ins->objectid;
2741 extent_op->num_bytes = ins->offset;
2742 extent_op->level = level;
2743 extent_op->flags = flags;
2744 memcpy(&extent_op->key, key, sizeof(*key));
2746 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2747 ins->objectid + ins->offset - 1,
2748 EXTENT_LOCKED, GFP_NOFS);
2749 set_state_private(&root->fs_info->extent_ins,
2750 ins->objectid, (unsigned long)extent_op);
2752 if (btrfs_fs_incompat(root->fs_info,
2753 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2754 ins->offset = level;
2755 ins->type = BTRFS_METADATA_ITEM_KEY;
2757 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2760 finish_current_insert(trans, root->fs_info->extent_root);
2761 del_pending_extents(trans, root->fs_info->extent_root);
2767 * helper function to allocate a block for a given tree
2768 * returns the tree buffer or NULL.
2770 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2771 struct btrfs_root *root,
2772 u32 blocksize, u64 root_objectid,
2773 struct btrfs_disk_key *key, int level,
2774 u64 hint, u64 empty_size)
2776 struct btrfs_key ins;
2778 struct extent_buffer *buf;
2780 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2781 trans->transid, 0, key, level,
2782 empty_size, hint, (u64)-1, &ins);
2785 return ERR_PTR(ret);
2788 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2790 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2791 0, root->root_key.objectid, level, 0);
2793 return ERR_PTR(-ENOMEM);
2795 btrfs_set_buffer_uptodate(buf);
2796 trans->blocks_used++;
2803 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct extent_buffer *leaf)
2808 u64 leaf_generation;
2809 struct btrfs_key key;
2810 struct btrfs_file_extent_item *fi;
2815 BUG_ON(!btrfs_is_leaf(leaf));
2816 nritems = btrfs_header_nritems(leaf);
2817 leaf_owner = btrfs_header_owner(leaf);
2818 leaf_generation = btrfs_header_generation(leaf);
2820 for (i = 0; i < nritems; i++) {
2823 btrfs_item_key_to_cpu(leaf, &key, i);
2824 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2826 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2827 if (btrfs_file_extent_type(leaf, fi) ==
2828 BTRFS_FILE_EXTENT_INLINE)
2831 * FIXME make sure to insert a trans record that
2832 * repeats the snapshot del on crash
2834 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2835 if (disk_bytenr == 0)
2837 ret = btrfs_free_extent(trans, root, disk_bytenr,
2838 btrfs_file_extent_disk_num_bytes(leaf, fi),
2839 leaf->start, leaf_owner, leaf_generation,
2846 static void noinline reada_walk_down(struct btrfs_root *root,
2847 struct extent_buffer *node,
2860 nritems = btrfs_header_nritems(node);
2861 level = btrfs_header_level(node);
2865 for (i = slot; i < nritems && skipped < 32; i++) {
2866 bytenr = btrfs_node_blockptr(node, i);
2867 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2868 (last > bytenr && last - bytenr > 32 * 1024))) {
2872 blocksize = btrfs_level_size(root, level - 1);
2874 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2882 mutex_unlock(&root->fs_info->fs_mutex);
2883 ret = readahead_tree_block(root, bytenr, blocksize,
2884 btrfs_node_ptr_generation(node, i));
2885 last = bytenr + blocksize;
2887 mutex_lock(&root->fs_info->fs_mutex);
2894 * helper function for drop_snapshot, this walks down the tree dropping ref
2895 * counts as it goes.
2897 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2898 struct btrfs_root *root,
2899 struct btrfs_path *path, int *level)
2905 struct extent_buffer *next;
2906 struct extent_buffer *cur;
2907 struct extent_buffer *parent;
2912 WARN_ON(*level < 0);
2913 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2914 ret = btrfs_lookup_extent_ref(trans, root,
2915 path->nodes[*level]->start,
2916 path->nodes[*level]->len, &refs);
2922 * walk down to the last node level and free all the leaves
2924 while(*level >= 0) {
2925 WARN_ON(*level < 0);
2926 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2927 cur = path->nodes[*level];
2929 if (btrfs_header_level(cur) != *level)
2932 if (path->slots[*level] >=
2933 btrfs_header_nritems(cur))
2936 ret = drop_leaf_ref(trans, root, cur);
2940 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2941 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2942 blocksize = btrfs_level_size(root, *level - 1);
2943 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2947 parent = path->nodes[*level];
2948 root_owner = btrfs_header_owner(parent);
2949 root_gen = btrfs_header_generation(parent);
2950 path->slots[*level]++;
2951 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2952 parent->start, root_owner,
2953 root_gen, *level - 1, 1);
2957 next = btrfs_find_tree_block(root, bytenr, blocksize);
2958 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2959 free_extent_buffer(next);
2960 reada_walk_down(root, cur, path->slots[*level]);
2961 mutex_unlock(&root->fs_info->fs_mutex);
2962 next = read_tree_block(root, bytenr, blocksize,
2964 mutex_lock(&root->fs_info->fs_mutex);
2965 if (!extent_buffer_uptodate(next)) {
2967 ret = PTR_ERR(next);
2973 WARN_ON(*level <= 0);
2974 if (path->nodes[*level-1])
2975 free_extent_buffer(path->nodes[*level-1]);
2976 path->nodes[*level-1] = next;
2977 *level = btrfs_header_level(next);
2978 path->slots[*level] = 0;
2981 WARN_ON(*level < 0);
2982 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2984 if (path->nodes[*level] == root->node) {
2985 root_owner = root->root_key.objectid;
2986 parent = path->nodes[*level];
2988 parent = path->nodes[*level + 1];
2989 root_owner = btrfs_header_owner(parent);
2992 root_gen = btrfs_header_generation(parent);
2993 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2994 path->nodes[*level]->len, parent->start,
2995 root_owner, root_gen, *level, 1);
2996 free_extent_buffer(path->nodes[*level]);
2997 path->nodes[*level] = NULL;
3004 * helper for dropping snapshots. This walks back up the tree in the path
3005 * to find the first node higher up where we haven't yet gone through
3008 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3009 struct btrfs_root *root,
3010 struct btrfs_path *path, int *level)
3014 struct btrfs_root_item *root_item = &root->root_item;
3019 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3020 slot = path->slots[i];
3021 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3022 struct extent_buffer *node;
3023 struct btrfs_disk_key disk_key;
3024 node = path->nodes[i];
3027 WARN_ON(*level == 0);
3028 btrfs_node_key(node, &disk_key, path->slots[i]);
3029 memcpy(&root_item->drop_progress,
3030 &disk_key, sizeof(disk_key));
3031 root_item->drop_level = i;
3034 struct extent_buffer *parent;
3035 if (path->nodes[*level] == root->node)
3036 parent = path->nodes[*level];
3038 parent = path->nodes[*level + 1];
3040 root_owner = btrfs_header_owner(parent);
3041 root_gen = btrfs_header_generation(parent);
3042 ret = btrfs_free_extent(trans, root,
3043 path->nodes[*level]->start,
3044 path->nodes[*level]->len,
3045 parent->start, root_owner,
3046 root_gen, *level, 1);
3048 free_extent_buffer(path->nodes[*level]);
3049 path->nodes[*level] = NULL;
3058 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3060 struct btrfs_space_info *sinfo;
3061 struct btrfs_block_group_cache *cache;
3068 ret = find_first_extent_bit(&info->block_group_cache, 0,
3069 &start, &end, (unsigned int)-1);
3072 ret = get_state_private(&info->block_group_cache, start, &ptr);
3074 cache = u64_to_ptr(ptr);
3075 if (cache->free_space_ctl) {
3076 btrfs_remove_free_space_cache(cache);
3077 kfree(cache->free_space_ctl);
3081 clear_extent_bits(&info->block_group_cache, start,
3082 end, (unsigned int)-1, GFP_NOFS);
3085 ret = find_first_extent_bit(&info->free_space_cache, 0,
3086 &start, &end, EXTENT_DIRTY);
3089 clear_extent_dirty(&info->free_space_cache, start,
3093 while (!list_empty(&info->space_info)) {
3094 sinfo = list_entry(info->space_info.next,
3095 struct btrfs_space_info, list);
3096 list_del_init(&sinfo->list);
3102 static int find_first_block_group(struct btrfs_root *root,
3103 struct btrfs_path *path, struct btrfs_key *key)
3106 struct btrfs_key found_key;
3107 struct extent_buffer *leaf;
3110 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3114 slot = path->slots[0];
3115 leaf = path->nodes[0];
3116 if (slot >= btrfs_header_nritems(leaf)) {
3117 ret = btrfs_next_leaf(root, path);
3124 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3126 if (found_key.objectid >= key->objectid &&
3127 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3136 int btrfs_read_block_groups(struct btrfs_root *root)
3138 struct btrfs_path *path;
3141 struct btrfs_block_group_cache *cache;
3142 struct btrfs_fs_info *info = root->fs_info;
3143 struct btrfs_space_info *space_info;
3144 struct extent_io_tree *block_group_cache;
3145 struct btrfs_key key;
3146 struct btrfs_key found_key;
3147 struct extent_buffer *leaf;
3149 block_group_cache = &info->block_group_cache;
3151 root = info->extent_root;
3154 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3155 path = btrfs_alloc_path();
3160 ret = find_first_block_group(root, path, &key);
3168 leaf = path->nodes[0];
3169 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3170 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3176 read_extent_buffer(leaf, &cache->item,
3177 btrfs_item_ptr_offset(leaf, path->slots[0]),
3178 sizeof(cache->item));
3179 memcpy(&cache->key, &found_key, sizeof(found_key));
3182 key.objectid = found_key.objectid + found_key.offset;
3183 btrfs_release_path(path);
3184 cache->flags = btrfs_block_group_flags(&cache->item);
3186 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3187 bit = BLOCK_GROUP_DATA;
3188 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3189 bit = BLOCK_GROUP_SYSTEM;
3190 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3191 bit = BLOCK_GROUP_METADATA;
3193 set_avail_alloc_bits(info, cache->flags);
3194 if (btrfs_chunk_readonly(root, cache->key.objectid))
3197 ret = update_space_info(info, cache->flags, found_key.offset,
3198 btrfs_block_group_used(&cache->item),
3201 cache->space_info = space_info;
3203 /* use EXTENT_LOCKED to prevent merging */
3204 set_extent_bits(block_group_cache, found_key.objectid,
3205 found_key.objectid + found_key.offset - 1,
3206 bit | EXTENT_LOCKED, GFP_NOFS);
3207 set_state_private(block_group_cache, found_key.objectid,
3208 (unsigned long)cache);
3212 btrfs_free_path(path);
3216 struct btrfs_block_group_cache *
3217 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3218 u64 chunk_objectid, u64 chunk_offset, u64 size)
3222 struct btrfs_block_group_cache *cache;
3223 struct extent_io_tree *block_group_cache;
3225 block_group_cache = &fs_info->block_group_cache;
3227 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3229 cache->key.objectid = chunk_offset;
3230 cache->key.offset = size;
3232 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3233 btrfs_set_block_group_used(&cache->item, bytes_used);
3234 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3235 cache->flags = type;
3236 btrfs_set_block_group_flags(&cache->item, type);
3238 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3239 &cache->space_info);
3242 bit = block_group_state_bits(type);
3243 ret = set_extent_bits(block_group_cache, chunk_offset,
3244 chunk_offset + size - 1,
3245 bit | EXTENT_LOCKED, GFP_NOFS);
3248 ret = set_state_private(block_group_cache, chunk_offset,
3249 (unsigned long)cache);
3251 set_avail_alloc_bits(fs_info, type);
3256 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3257 struct btrfs_root *root, u64 bytes_used,
3258 u64 type, u64 chunk_objectid, u64 chunk_offset,
3262 struct btrfs_root *extent_root;
3263 struct btrfs_block_group_cache *cache;
3265 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3266 chunk_objectid, chunk_offset, size);
3267 extent_root = root->fs_info->extent_root;
3268 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3269 sizeof(cache->item));
3272 ret = finish_current_insert(trans, extent_root);
3274 ret = del_pending_extents(trans, extent_root);
3281 * This is for converter use only.
3283 * In that case, we don't know where are free blocks located.
3284 * Therefore all block group cache entries must be setup properly
3285 * before doing any block allocation.
3287 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3288 struct btrfs_root *root)
3296 u64 total_metadata = 0;
3300 struct btrfs_root *extent_root;
3301 struct btrfs_block_group_cache *cache;
3302 struct extent_io_tree *block_group_cache;
3304 extent_root = root->fs_info->extent_root;
3305 block_group_cache = &root->fs_info->block_group_cache;
3306 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3307 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3308 group_align = 64 * root->sectorsize;
3311 while (cur_start < total_bytes) {
3312 group_size = total_bytes / 12;
3313 group_size = min_t(u64, group_size, total_bytes - cur_start);
3314 if (cur_start == 0) {
3315 bit = BLOCK_GROUP_SYSTEM;
3316 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3318 group_size &= ~(group_align - 1);
3319 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3320 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3322 group_size &= ~(group_align - 1);
3323 if (total_data >= total_metadata * 2) {
3324 group_type = BTRFS_BLOCK_GROUP_METADATA;
3325 group_size = min_t(u64, group_size,
3326 1ULL * 1024 * 1024 * 1024);
3327 total_metadata += group_size;
3329 group_type = BTRFS_BLOCK_GROUP_DATA;
3330 group_size = min_t(u64, group_size,
3331 5ULL * 1024 * 1024 * 1024);
3332 total_data += group_size;
3334 if ((total_bytes - cur_start) * 4 < group_size * 5)
3335 group_size = total_bytes - cur_start;
3338 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3341 cache->key.objectid = cur_start;
3342 cache->key.offset = group_size;
3343 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3345 btrfs_set_block_group_used(&cache->item, 0);
3346 btrfs_set_block_group_chunk_objectid(&cache->item,
3348 btrfs_set_block_group_flags(&cache->item, group_type);
3350 cache->flags = group_type;
3352 ret = update_space_info(root->fs_info, group_type, group_size,
3353 0, &cache->space_info);
3355 set_avail_alloc_bits(extent_root->fs_info, group_type);
3357 set_extent_bits(block_group_cache, cur_start,
3358 cur_start + group_size - 1,
3359 bit | EXTENT_LOCKED, GFP_NOFS);
3360 set_state_private(block_group_cache, cur_start,
3361 (unsigned long)cache);
3362 cur_start += group_size;
3364 /* then insert all the items */
3366 while(cur_start < total_bytes) {
3367 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3370 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3371 sizeof(cache->item));
3374 finish_current_insert(trans, extent_root);
3375 ret = del_pending_extents(trans, extent_root);
3378 cur_start = cache->key.objectid + cache->key.offset;
3383 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3384 struct btrfs_root *root,
3385 u64 bytenr, u64 num_bytes, int alloc,
3388 return update_block_group(trans, root, bytenr, num_bytes,
3393 * Fixup block accounting. The initial block accounting created by
3394 * make_block_groups isn't accuracy in this case.
3396 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3397 struct btrfs_root *root)
3403 struct btrfs_path path;
3404 struct btrfs_key key;
3405 struct extent_buffer *leaf;
3406 struct btrfs_block_group_cache *cache;
3407 struct btrfs_fs_info *fs_info = root->fs_info;
3409 root = root->fs_info->extent_root;
3411 while(extent_root_pending_ops(fs_info)) {
3412 ret = finish_current_insert(trans, root);
3415 ret = del_pending_extents(trans, root);
3421 cache = btrfs_lookup_first_block_group(fs_info, start);
3424 start = cache->key.objectid + cache->key.offset;
3425 btrfs_set_block_group_used(&cache->item, 0);
3426 cache->space_info->bytes_used = 0;
3427 set_extent_bits(&root->fs_info->block_group_cache,
3428 cache->key.objectid,
3429 cache->key.objectid + cache->key.offset -1,
3430 BLOCK_GROUP_DIRTY, GFP_NOFS);
3433 btrfs_init_path(&path);
3436 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3437 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3442 leaf = path.nodes[0];
3443 slot = path.slots[0];
3444 if (slot >= btrfs_header_nritems(leaf)) {
3445 ret = btrfs_next_leaf(root, &path);
3450 leaf = path.nodes[0];
3451 slot = path.slots[0];
3453 btrfs_item_key_to_cpu(leaf, &key, slot);
3454 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3455 bytes_used += key.offset;
3456 ret = btrfs_update_block_group(trans, root,
3457 key.objectid, key.offset, 1, 0);
3459 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3460 bytes_used += root->leafsize;
3461 ret = btrfs_update_block_group(trans, root,
3462 key.objectid, root->leafsize, 1, 0);
3467 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3468 btrfs_release_path(&path);
3473 * Record a file extent. Do all the required works, such as inserting
3474 * file extent item, inserting extent item and backref item into extent
3475 * tree and updating block accounting.
3477 int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
3478 struct btrfs_root *root, u64 objectid,
3479 struct btrfs_inode_item *inode,
3480 u64 file_pos, u64 disk_bytenr,
3484 struct btrfs_fs_info *info = root->fs_info;
3485 struct btrfs_root *extent_root = info->extent_root;
3486 struct extent_buffer *leaf;
3487 struct btrfs_file_extent_item *fi;
3488 struct btrfs_key ins_key;
3489 struct btrfs_path path;
3490 struct btrfs_extent_item *ei;
3493 if (disk_bytenr == 0) {
3494 ret = btrfs_insert_file_extent(trans, root, objectid,
3495 file_pos, disk_bytenr,
3496 num_bytes, num_bytes);
3500 btrfs_init_path(&path);
3502 ins_key.objectid = objectid;
3503 ins_key.offset = file_pos;
3504 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
3505 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
3509 leaf = path.nodes[0];
3510 fi = btrfs_item_ptr(leaf, path.slots[0],
3511 struct btrfs_file_extent_item);
3512 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
3513 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
3514 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
3515 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
3516 btrfs_set_file_extent_offset(leaf, fi, 0);
3517 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
3518 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
3519 btrfs_set_file_extent_compression(leaf, fi, 0);
3520 btrfs_set_file_extent_encryption(leaf, fi, 0);
3521 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
3522 btrfs_mark_buffer_dirty(leaf);
3524 nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
3525 btrfs_set_stack_inode_nbytes(inode, nbytes);
3527 btrfs_release_path(&path);
3529 ins_key.objectid = disk_bytenr;
3530 ins_key.offset = num_bytes;
3531 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3533 ret = btrfs_insert_empty_item(trans, extent_root, &path,
3534 &ins_key, sizeof(*ei));
3536 leaf = path.nodes[0];
3537 ei = btrfs_item_ptr(leaf, path.slots[0],
3538 struct btrfs_extent_item);
3540 btrfs_set_extent_refs(leaf, ei, 0);
3541 btrfs_set_extent_generation(leaf, ei, 0);
3542 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
3544 btrfs_mark_buffer_dirty(leaf);
3546 ret = btrfs_update_block_group(trans, root, disk_bytenr,
3550 } else if (ret != -EEXIST) {
3553 btrfs_extent_post_op(trans, extent_root);
3555 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
3556 root->root_key.objectid,
3557 objectid, file_pos);
3562 btrfs_release_path(&path);