2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
40 struct pending_extent_op {
51 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
52 btrfs_root *extent_root);
53 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
54 btrfs_root *extent_root);
56 void maybe_lock_mutex(struct btrfs_root *root)
60 void maybe_unlock_mutex(struct btrfs_root *root)
64 static int remove_sb_from_cache(struct btrfs_root *root,
65 struct btrfs_block_group_cache *cache)
71 struct extent_io_tree *free_space_cache;
73 free_space_cache = &root->fs_info->free_space_cache;
74 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75 bytenr = btrfs_sb_offset(i);
76 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77 cache->key.objectid, bytenr, 0,
78 &logical, &nr, &stripe_len);
81 clear_extent_dirty(free_space_cache, logical[nr],
82 logical[nr] + stripe_len - 1, GFP_NOFS);
89 static int cache_block_group(struct btrfs_root *root,
90 struct btrfs_block_group_cache *block_group)
92 struct btrfs_path *path;
95 struct extent_buffer *leaf;
96 struct extent_io_tree *free_space_cache;
106 root = root->fs_info->extent_root;
107 free_space_cache = &root->fs_info->free_space_cache;
109 if (block_group->cached)
112 path = btrfs_alloc_path();
117 first_free = block_group->key.objectid;
118 key.objectid = block_group->key.objectid;
120 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
124 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
128 leaf = path->nodes[0];
129 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
130 if (key.objectid + key.offset > first_free)
131 first_free = key.objectid + key.offset;
134 leaf = path->nodes[0];
135 slot = path->slots[0];
136 if (slot >= btrfs_header_nritems(leaf)) {
137 ret = btrfs_next_leaf(root, path);
146 btrfs_item_key_to_cpu(leaf, &key, slot);
147 if (key.objectid < block_group->key.objectid) {
150 if (key.objectid >= block_group->key.objectid +
151 block_group->key.offset) {
155 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
160 if (key.objectid > last) {
161 hole_size = key.objectid - last;
162 set_extent_dirty(free_space_cache, last,
163 last + hole_size - 1,
166 last = key.objectid + key.offset;
174 if (block_group->key.objectid +
175 block_group->key.offset > last) {
176 hole_size = block_group->key.objectid +
177 block_group->key.offset - last;
178 set_extent_dirty(free_space_cache, last,
179 last + hole_size - 1, GFP_NOFS);
181 remove_sb_from_cache(root, block_group);
182 block_group->cached = 1;
184 btrfs_free_path(path);
188 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
192 struct extent_io_tree *block_group_cache;
193 struct btrfs_block_group_cache *block_group = NULL;
199 bytenr = max_t(u64, bytenr,
200 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
201 block_group_cache = &info->block_group_cache;
202 ret = find_first_extent_bit(block_group_cache,
203 bytenr, &start, &end,
204 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
209 ret = get_state_private(block_group_cache, start, &ptr);
213 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
217 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
221 struct extent_io_tree *block_group_cache;
222 struct btrfs_block_group_cache *block_group = NULL;
228 block_group_cache = &info->block_group_cache;
229 ret = find_first_extent_bit(block_group_cache,
230 bytenr, &start, &end,
231 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
236 ret = get_state_private(block_group_cache, start, &ptr);
240 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
241 if (block_group->key.objectid <= bytenr && bytenr <
242 block_group->key.objectid + block_group->key.offset)
247 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
249 return (cache->flags & bits) == bits;
252 static int noinline find_search_start(struct btrfs_root *root,
253 struct btrfs_block_group_cache **cache_ret,
254 u64 *start_ret, int num, int data)
257 struct btrfs_block_group_cache *cache = *cache_ret;
261 u64 search_start = *start_ret;
268 ret = cache_block_group(root, cache);
272 last = max(search_start, cache->key.objectid);
273 if (cache->ro || !block_group_bits(cache, data)) {
278 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
279 last, &start, &end, EXTENT_DIRTY);
284 start = max(last, start);
286 if (last - start < num) {
289 if (start + num > cache->key.objectid + cache->key.offset) {
296 cache = btrfs_lookup_block_group(root->fs_info, search_start);
298 printk("Unable to find block group for %llu\n",
299 (unsigned long long)search_start);
305 last = cache->key.objectid + cache->key.offset;
307 cache = btrfs_lookup_first_block_group(root->fs_info, last);
317 cache = btrfs_find_block_group(root, cache, last, data, 0);
318 cache = btrfs_find_block_group(root, cache, last, data, 0);
326 static u64 div_factor(u64 num, int factor)
335 static int block_group_state_bits(u64 flags)
338 if (flags & BTRFS_BLOCK_GROUP_DATA)
339 bits |= BLOCK_GROUP_DATA;
340 if (flags & BTRFS_BLOCK_GROUP_METADATA)
341 bits |= BLOCK_GROUP_METADATA;
342 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
343 bits |= BLOCK_GROUP_SYSTEM;
347 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
348 struct btrfs_block_group_cache
349 *hint, u64 search_start,
352 struct btrfs_block_group_cache *cache;
353 struct extent_io_tree *block_group_cache;
354 struct btrfs_block_group_cache *found_group = NULL;
355 struct btrfs_fs_info *info = root->fs_info;
368 block_group_cache = &info->block_group_cache;
373 bit = block_group_state_bits(data);
376 struct btrfs_block_group_cache *shint;
377 shint = btrfs_lookup_block_group(info, search_start);
378 if (shint && !shint->ro && block_group_bits(shint, data)) {
379 used = btrfs_block_group_used(&shint->item);
380 if (used + shint->pinned <
381 div_factor(shint->key.offset, factor)) {
386 if (hint && !hint->ro && block_group_bits(hint, data)) {
387 used = btrfs_block_group_used(&hint->item);
388 if (used + hint->pinned <
389 div_factor(hint->key.offset, factor)) {
392 last = hint->key.objectid + hint->key.offset;
396 hint_last = max(hint->key.objectid, search_start);
398 hint_last = search_start;
404 ret = find_first_extent_bit(block_group_cache, last,
409 ret = get_state_private(block_group_cache, start, &ptr);
413 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
414 last = cache->key.objectid + cache->key.offset;
415 used = btrfs_block_group_used(&cache->item);
417 if (!cache->ro && block_group_bits(cache, data)) {
419 free_check = cache->key.offset;
421 free_check = div_factor(cache->key.offset,
424 if (used + cache->pinned < free_check) {
441 * Back reference rules. Back refs have three main goals:
443 * 1) differentiate between all holders of references to an extent so that
444 * when a reference is dropped we can make sure it was a valid reference
445 * before freeing the extent.
447 * 2) Provide enough information to quickly find the holders of an extent
448 * if we notice a given block is corrupted or bad.
450 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
451 * maintenance. This is actually the same as #2, but with a slightly
452 * different use case.
454 * File extents can be referenced by:
456 * - multiple snapshots, subvolumes, or different generations in one subvol
457 * - different files inside a single subvolume
458 * - different offsets inside a file (bookend extents in file.c)
460 * The extent ref structure has fields for:
462 * - Objectid of the subvolume root
463 * - Generation number of the tree holding the reference
464 * - objectid of the file holding the reference
465 * - offset in the file corresponding to the key holding the reference
466 * - number of references holding by parent node (alway 1 for tree blocks)
468 * Btree leaf may hold multiple references to a file extent. In most cases,
469 * these references are from same file and the corresponding offsets inside
470 * the file are close together. So inode objectid and offset in file are
471 * just hints, they provide hints about where in the btree the references
472 * can be found and when we can stop searching.
474 * When a file extent is allocated the fields are filled in:
475 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
477 * When a leaf is cow'd new references are added for every file extent found
478 * in the leaf. It looks similar to the create case, but trans->transid will
479 * be different when the block is cow'd.
481 * (root_key.objectid, trans->transid, inode objectid, offset in file,
482 * number of references in the leaf)
484 * Because inode objectid and offset in file are just hints, they are not
485 * used when backrefs are deleted. When a file extent is removed either
486 * during snapshot deletion or file truncation, we find the corresponding
487 * back back reference and check the following fields.
489 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
491 * Btree extents can be referenced by:
493 * - Different subvolumes
494 * - Different generations of the same subvolume
496 * When a tree block is created, back references are inserted:
498 * (root->root_key.objectid, trans->transid, level, 0, 1)
500 * When a tree block is cow'd, new back references are added for all the
501 * blocks it points to. If the tree block isn't in reference counted root,
502 * the old back references are removed. These new back references are of
503 * the form (trans->transid will have increased since creation):
505 * (root->root_key.objectid, trans->transid, level, 0, 1)
507 * When a backref is in deleting, the following fields are checked:
509 * if backref was for a tree root:
510 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
512 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
514 * Back Reference Key composing:
516 * The key objectid corresponds to the first byte in the extent, the key
517 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
518 * byte of parent extent. If a extent is tree root, the key offset is set
519 * to the key objectid.
522 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root,
524 struct btrfs_path *path,
525 u64 bytenr, u64 parent,
526 u64 ref_root, u64 ref_generation,
527 u64 owner_objectid, int del)
529 struct btrfs_key key;
530 struct btrfs_extent_ref *ref;
531 struct extent_buffer *leaf;
535 key.objectid = bytenr;
536 key.type = BTRFS_EXTENT_REF_KEY;
539 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
547 leaf = path->nodes[0];
548 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
549 ref_objectid = btrfs_ref_objectid(leaf, ref);
550 if (btrfs_ref_root(leaf, ref) != ref_root ||
551 btrfs_ref_generation(leaf, ref) != ref_generation ||
552 (ref_objectid != owner_objectid &&
553 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
563 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct btrfs_path *path,
566 u64 bytenr, u64 parent,
567 u64 ref_root, u64 ref_generation,
570 struct btrfs_key key;
571 struct extent_buffer *leaf;
572 struct btrfs_extent_ref *ref;
576 key.objectid = bytenr;
577 key.type = BTRFS_EXTENT_REF_KEY;
580 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
582 leaf = path->nodes[0];
583 ref = btrfs_item_ptr(leaf, path->slots[0],
584 struct btrfs_extent_ref);
585 btrfs_set_ref_root(leaf, ref, ref_root);
586 btrfs_set_ref_generation(leaf, ref, ref_generation);
587 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
588 btrfs_set_ref_num_refs(leaf, ref, 1);
589 } else if (ret == -EEXIST) {
591 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
592 leaf = path->nodes[0];
593 ref = btrfs_item_ptr(leaf, path->slots[0],
594 struct btrfs_extent_ref);
595 if (btrfs_ref_root(leaf, ref) != ref_root ||
596 btrfs_ref_generation(leaf, ref) != ref_generation) {
602 num_refs = btrfs_ref_num_refs(leaf, ref);
603 BUG_ON(num_refs == 0);
604 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
606 existing_owner = btrfs_ref_objectid(leaf, ref);
607 if (existing_owner != owner_objectid &&
608 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
609 btrfs_set_ref_objectid(leaf, ref,
610 BTRFS_MULTIPLE_OBJECTIDS);
616 btrfs_mark_buffer_dirty(path->nodes[0]);
618 btrfs_release_path(root, path);
622 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
623 struct btrfs_root *root,
624 struct btrfs_path *path)
626 struct extent_buffer *leaf;
627 struct btrfs_extent_ref *ref;
631 leaf = path->nodes[0];
632 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
633 num_refs = btrfs_ref_num_refs(leaf, ref);
634 BUG_ON(num_refs == 0);
637 ret = btrfs_del_item(trans, root, path);
639 btrfs_set_ref_num_refs(leaf, ref, num_refs);
640 btrfs_mark_buffer_dirty(leaf);
642 btrfs_release_path(root, path);
646 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root, u64 bytenr,
648 u64 orig_parent, u64 parent,
649 u64 orig_root, u64 ref_root,
650 u64 orig_generation, u64 ref_generation,
654 struct btrfs_root *extent_root = root->fs_info->extent_root;
655 struct btrfs_path *path;
657 if (root == root->fs_info->extent_root) {
658 struct pending_extent_op *extent_op;
661 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
662 num_bytes = btrfs_level_size(root, (int)owner_objectid);
663 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
664 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
666 ret = get_state_private(&root->fs_info->extent_ins,
669 extent_op = (struct pending_extent_op *)
671 BUG_ON(extent_op->parent != orig_parent);
672 BUG_ON(extent_op->generation != orig_generation);
673 extent_op->parent = parent;
674 extent_op->generation = ref_generation;
676 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
679 extent_op->type = PENDING_BACKREF_UPDATE;
680 extent_op->bytenr = bytenr;
681 extent_op->num_bytes = num_bytes;
682 extent_op->parent = parent;
683 extent_op->orig_parent = orig_parent;
684 extent_op->generation = ref_generation;
685 extent_op->orig_generation = orig_generation;
686 extent_op->level = (int)owner_objectid;
688 set_extent_bits(&root->fs_info->extent_ins,
689 bytenr, bytenr + num_bytes - 1,
690 EXTENT_LOCKED, GFP_NOFS);
691 set_state_private(&root->fs_info->extent_ins,
692 bytenr, (unsigned long)extent_op);
697 path = btrfs_alloc_path();
700 ret = lookup_extent_backref(trans, extent_root, path,
701 bytenr, orig_parent, orig_root,
702 orig_generation, owner_objectid, 1);
705 ret = remove_extent_backref(trans, extent_root, path);
708 ret = insert_extent_backref(trans, extent_root, path, bytenr,
709 parent, ref_root, ref_generation,
712 finish_current_insert(trans, extent_root);
713 del_pending_extents(trans, extent_root);
715 btrfs_free_path(path);
719 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root, u64 bytenr,
721 u64 orig_parent, u64 parent,
722 u64 ref_root, u64 ref_generation,
726 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
727 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
729 maybe_lock_mutex(root);
730 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
731 parent, ref_root, ref_root,
732 ref_generation, ref_generation,
734 maybe_unlock_mutex(root);
738 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root, u64 bytenr,
740 u64 orig_parent, u64 parent,
741 u64 orig_root, u64 ref_root,
742 u64 orig_generation, u64 ref_generation,
745 struct btrfs_path *path;
747 struct btrfs_key key;
748 struct extent_buffer *l;
749 struct btrfs_extent_item *item;
752 path = btrfs_alloc_path();
757 key.objectid = bytenr;
758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = (u64)-1;
761 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
765 BUG_ON(ret == 0 || path->slots[0] == 0);
770 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771 BUG_ON(key.objectid != bytenr);
772 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
774 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
775 refs = btrfs_extent_refs(l, item);
776 btrfs_set_extent_refs(l, item, refs + 1);
777 btrfs_mark_buffer_dirty(path->nodes[0]);
779 btrfs_release_path(root->fs_info->extent_root, path);
782 ret = insert_extent_backref(trans, root->fs_info->extent_root,
783 path, bytenr, parent,
784 ref_root, ref_generation,
787 finish_current_insert(trans, root->fs_info->extent_root);
788 del_pending_extents(trans, root->fs_info->extent_root);
790 btrfs_free_path(path);
794 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
795 struct btrfs_root *root,
796 u64 bytenr, u64 num_bytes, u64 parent,
797 u64 ref_root, u64 ref_generation,
801 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
802 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
804 maybe_lock_mutex(root);
805 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
806 0, ref_root, 0, ref_generation,
808 maybe_unlock_mutex(root);
812 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
813 struct btrfs_root *root)
815 finish_current_insert(trans, root->fs_info->extent_root);
816 del_pending_extents(trans, root->fs_info->extent_root);
820 int lookup_extent_ref(struct btrfs_trans_handle *trans,
821 struct btrfs_root *root, u64 bytenr,
822 u64 num_bytes, u32 *refs)
824 struct btrfs_path *path;
826 struct btrfs_key key;
827 struct extent_buffer *l;
828 struct btrfs_extent_item *item;
830 WARN_ON(num_bytes < root->sectorsize);
831 path = btrfs_alloc_path();
833 key.objectid = bytenr;
834 key.offset = num_bytes;
835 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
836 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
841 btrfs_print_leaf(root, path->nodes[0]);
842 printk("failed to find block number %Lu\n", bytenr);
846 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
847 *refs = btrfs_extent_refs(l, item);
849 btrfs_free_path(path);
853 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
854 struct extent_buffer *orig_buf, struct extent_buffer *buf,
863 u32 nr_file_extents = 0;
864 struct btrfs_key key;
865 struct btrfs_file_extent_item *fi;
870 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
871 u64, u64, u64, u64, u64, u64, u64, u64);
873 ref_root = btrfs_header_owner(buf);
874 ref_generation = btrfs_header_generation(buf);
875 orig_root = btrfs_header_owner(orig_buf);
876 orig_generation = btrfs_header_generation(orig_buf);
878 nritems = btrfs_header_nritems(buf);
879 level = btrfs_header_level(buf);
881 if (root->ref_cows) {
882 process_func = __btrfs_inc_extent_ref;
885 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
887 process_func = __btrfs_update_extent_ref;
890 for (i = 0; i < nritems; i++) {
893 btrfs_item_key_to_cpu(buf, &key, i);
894 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
896 fi = btrfs_item_ptr(buf, i,
897 struct btrfs_file_extent_item);
898 if (btrfs_file_extent_type(buf, fi) ==
899 BTRFS_FILE_EXTENT_INLINE)
901 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
907 maybe_lock_mutex(root);
908 ret = process_func(trans, root, bytenr,
909 orig_buf->start, buf->start,
911 orig_generation, ref_generation,
913 maybe_unlock_mutex(root);
921 bytenr = btrfs_node_blockptr(buf, i);
922 maybe_lock_mutex(root);
923 ret = process_func(trans, root, bytenr,
924 orig_buf->start, buf->start,
926 orig_generation, ref_generation,
928 maybe_unlock_mutex(root);
939 *nr_extents = nr_file_extents;
941 *nr_extents = nritems;
947 for (i =0; i < faili; i++) {
950 btrfs_item_key_to_cpu(buf, &key, i);
951 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
953 fi = btrfs_item_ptr(buf, i,
954 struct btrfs_file_extent_item);
955 if (btrfs_file_extent_type(buf, fi) ==
956 BTRFS_FILE_EXTENT_INLINE)
958 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
959 if (disk_bytenr == 0)
961 err = btrfs_free_extent(trans, root, disk_bytenr,
962 btrfs_file_extent_disk_num_bytes(buf,
966 bytenr = btrfs_node_blockptr(buf, i);
967 err = btrfs_free_extent(trans, root, bytenr,
968 btrfs_level_size(root, level - 1), 0);
976 int btrfs_update_ref(struct btrfs_trans_handle *trans,
977 struct btrfs_root *root, struct extent_buffer *orig_buf,
978 struct extent_buffer *buf, int start_slot, int nr)
986 struct btrfs_key key;
987 struct btrfs_file_extent_item *fi;
993 BUG_ON(start_slot < 0);
994 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
996 ref_root = btrfs_header_owner(buf);
997 ref_generation = btrfs_header_generation(buf);
998 orig_root = btrfs_header_owner(orig_buf);
999 orig_generation = btrfs_header_generation(orig_buf);
1000 level = btrfs_header_level(buf);
1002 if (!root->ref_cows) {
1004 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1008 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1011 btrfs_item_key_to_cpu(buf, &key, slot);
1012 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1014 fi = btrfs_item_ptr(buf, slot,
1015 struct btrfs_file_extent_item);
1016 if (btrfs_file_extent_type(buf, fi) ==
1017 BTRFS_FILE_EXTENT_INLINE)
1019 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1023 maybe_lock_mutex(root);
1024 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1025 orig_buf->start, buf->start,
1026 orig_root, ref_root,
1027 orig_generation, ref_generation,
1029 maybe_unlock_mutex(root);
1033 bytenr = btrfs_node_blockptr(buf, slot);
1034 maybe_lock_mutex(root);
1035 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1036 orig_buf->start, buf->start,
1037 orig_root, ref_root,
1038 orig_generation, ref_generation,
1040 maybe_unlock_mutex(root);
1051 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1052 struct btrfs_root *root,
1053 struct btrfs_path *path,
1054 struct btrfs_block_group_cache *cache)
1058 struct btrfs_root *extent_root = root->fs_info->extent_root;
1060 struct extent_buffer *leaf;
1062 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1067 leaf = path->nodes[0];
1068 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1069 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1070 btrfs_mark_buffer_dirty(leaf);
1071 btrfs_release_path(extent_root, path);
1073 finish_current_insert(trans, extent_root);
1074 pending_ret = del_pending_extents(trans, extent_root);
1083 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1084 struct btrfs_root *root)
1086 struct extent_io_tree *block_group_cache;
1087 struct btrfs_block_group_cache *cache;
1091 struct btrfs_path *path;
1097 block_group_cache = &root->fs_info->block_group_cache;
1098 path = btrfs_alloc_path();
1103 ret = find_first_extent_bit(block_group_cache, last,
1104 &start, &end, BLOCK_GROUP_DIRTY);
1109 ret = get_state_private(block_group_cache, start, &ptr);
1112 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1113 err = write_one_cache_group(trans, root,
1116 * if we fail to write the cache group, we want
1117 * to keep it marked dirty in hopes that a later
1124 clear_extent_bits(block_group_cache, start, end,
1125 BLOCK_GROUP_DIRTY, GFP_NOFS);
1127 btrfs_free_path(path);
1131 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1134 struct list_head *head = &info->space_info;
1135 struct list_head *cur;
1136 struct btrfs_space_info *found;
1137 list_for_each(cur, head) {
1138 found = list_entry(cur, struct btrfs_space_info, list);
1139 if (found->flags == flags)
1146 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1147 u64 total_bytes, u64 bytes_used,
1148 struct btrfs_space_info **space_info)
1150 struct btrfs_space_info *found;
1152 found = __find_space_info(info, flags);
1154 found->total_bytes += total_bytes;
1155 found->bytes_used += bytes_used;
1156 WARN_ON(found->total_bytes < found->bytes_used);
1157 *space_info = found;
1160 found = kmalloc(sizeof(*found), GFP_NOFS);
1164 list_add(&found->list, &info->space_info);
1165 found->flags = flags;
1166 found->total_bytes = total_bytes;
1167 found->bytes_used = bytes_used;
1168 found->bytes_pinned = 0;
1170 *space_info = found;
1175 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1177 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1178 BTRFS_BLOCK_GROUP_RAID1 |
1179 BTRFS_BLOCK_GROUP_DUP);
1181 if (flags & BTRFS_BLOCK_GROUP_DATA)
1182 fs_info->avail_data_alloc_bits |= extra_flags;
1183 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1184 fs_info->avail_metadata_alloc_bits |= extra_flags;
1185 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1186 fs_info->avail_system_alloc_bits |= extra_flags;
1190 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *extent_root, u64 alloc_bytes,
1194 struct btrfs_space_info *space_info;
1200 space_info = __find_space_info(extent_root->fs_info, flags);
1202 ret = update_space_info(extent_root->fs_info, flags,
1206 BUG_ON(!space_info);
1208 if (space_info->full)
1211 thresh = div_factor(space_info->total_bytes, 7);
1212 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1216 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1217 if (ret == -ENOSPC) {
1218 space_info->full = 1;
1224 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1225 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1230 static int update_block_group(struct btrfs_trans_handle *trans,
1231 struct btrfs_root *root,
1232 u64 bytenr, u64 num_bytes, int alloc,
1235 struct btrfs_block_group_cache *cache;
1236 struct btrfs_fs_info *info = root->fs_info;
1237 u64 total = num_bytes;
1244 cache = btrfs_lookup_block_group(info, bytenr);
1248 byte_in_group = bytenr - cache->key.objectid;
1249 WARN_ON(byte_in_group > cache->key.offset);
1250 start = cache->key.objectid;
1251 end = start + cache->key.offset - 1;
1252 set_extent_bits(&info->block_group_cache, start, end,
1253 BLOCK_GROUP_DIRTY, GFP_NOFS);
1255 old_val = btrfs_block_group_used(&cache->item);
1256 num_bytes = min(total, cache->key.offset - byte_in_group);
1258 old_val += num_bytes;
1259 cache->space_info->bytes_used += num_bytes;
1261 old_val -= num_bytes;
1262 cache->space_info->bytes_used -= num_bytes;
1264 set_extent_dirty(&info->free_space_cache,
1265 bytenr, bytenr + num_bytes - 1,
1269 btrfs_set_block_group_used(&cache->item, old_val);
1271 bytenr += num_bytes;
1276 static int update_pinned_extents(struct btrfs_root *root,
1277 u64 bytenr, u64 num, int pin)
1280 struct btrfs_block_group_cache *cache;
1281 struct btrfs_fs_info *fs_info = root->fs_info;
1284 set_extent_dirty(&fs_info->pinned_extents,
1285 bytenr, bytenr + num - 1, GFP_NOFS);
1287 clear_extent_dirty(&fs_info->pinned_extents,
1288 bytenr, bytenr + num - 1, GFP_NOFS);
1291 cache = btrfs_lookup_block_group(fs_info, bytenr);
1293 len = min(num, cache->key.offset -
1294 (bytenr - cache->key.objectid));
1296 cache->pinned += len;
1297 cache->space_info->bytes_pinned += len;
1298 fs_info->total_pinned += len;
1300 cache->pinned -= len;
1301 cache->space_info->bytes_pinned -= len;
1302 fs_info->total_pinned -= len;
1310 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1315 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1319 ret = find_first_extent_bit(pinned_extents, last,
1320 &start, &end, EXTENT_DIRTY);
1323 set_extent_dirty(copy, start, end, GFP_NOFS);
1329 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1330 struct btrfs_root *root,
1331 struct extent_io_tree *unpin)
1336 struct extent_io_tree *free_space_cache;
1337 free_space_cache = &root->fs_info->free_space_cache;
1340 ret = find_first_extent_bit(unpin, 0, &start, &end,
1344 update_pinned_extents(root, start, end + 1 - start, 0);
1345 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1346 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1351 static int finish_current_insert(struct btrfs_trans_handle *trans,
1352 struct btrfs_root *extent_root)
1357 struct btrfs_fs_info *info = extent_root->fs_info;
1358 struct btrfs_path *path;
1359 struct btrfs_extent_ref *ref;
1360 struct pending_extent_op *extent_op;
1361 struct btrfs_key key;
1362 struct btrfs_extent_item extent_item;
1366 btrfs_set_stack_extent_refs(&extent_item, 1);
1367 path = btrfs_alloc_path();
1370 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1371 &end, EXTENT_LOCKED);
1375 ret = get_state_private(&info->extent_ins, start, &priv);
1377 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1379 if (extent_op->type == PENDING_EXTENT_INSERT) {
1380 key.objectid = start;
1381 key.offset = end + 1 - start;
1382 key.type = BTRFS_EXTENT_ITEM_KEY;
1383 err = btrfs_insert_item(trans, extent_root, &key,
1384 &extent_item, sizeof(extent_item));
1387 clear_extent_bits(&info->extent_ins, start, end,
1388 EXTENT_LOCKED, GFP_NOFS);
1390 err = insert_extent_backref(trans, extent_root, path,
1391 start, extent_op->parent,
1392 extent_root->root_key.objectid,
1393 extent_op->generation,
1396 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1397 err = lookup_extent_backref(trans, extent_root, path,
1398 start, extent_op->orig_parent,
1399 extent_root->root_key.objectid,
1400 extent_op->orig_generation,
1401 extent_op->level, 0);
1404 clear_extent_bits(&info->extent_ins, start, end,
1405 EXTENT_LOCKED, GFP_NOFS);
1407 key.objectid = start;
1408 key.offset = extent_op->parent;
1409 key.type = BTRFS_EXTENT_REF_KEY;
1410 err = btrfs_set_item_key_safe(trans, extent_root, path,
1413 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1414 struct btrfs_extent_ref);
1415 btrfs_set_ref_generation(path->nodes[0], ref,
1416 extent_op->generation);
1417 btrfs_mark_buffer_dirty(path->nodes[0]);
1418 btrfs_release_path(extent_root, path);
1424 btrfs_free_path(path);
1428 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1429 struct btrfs_root *root,
1430 u64 bytenr, u64 num_bytes, int is_data)
1433 struct extent_buffer *buf;
1438 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1442 /* we can reuse a block if it hasn't been written
1443 * and it is from this transaction. We can't
1444 * reuse anything from the tree log root because
1445 * it has tiny sub-transactions.
1447 if (btrfs_buffer_uptodate(buf, 0)) {
1448 u64 header_owner = btrfs_header_owner(buf);
1449 u64 header_transid = btrfs_header_generation(buf);
1450 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1451 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
1452 header_transid == trans->transid &&
1453 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1454 clean_tree_block(NULL, root, buf);
1455 free_extent_buffer(buf);
1459 free_extent_buffer(buf);
1461 update_pinned_extents(root, bytenr, num_bytes, 1);
1468 * remove an extent from the root, returns 0 on success
1470 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1471 *root, u64 bytenr, u64 num_bytes, u64 parent,
1472 u64 root_objectid, u64 ref_generation,
1473 u64 owner_objectid, int pin, int mark_free)
1475 struct btrfs_path *path;
1476 struct btrfs_key key;
1477 struct btrfs_fs_info *info = root->fs_info;
1478 struct btrfs_extent_ops *ops = info->extent_ops;
1479 struct btrfs_root *extent_root = info->extent_root;
1480 struct extent_buffer *leaf;
1482 int extent_slot = 0;
1483 int found_extent = 0;
1485 struct btrfs_extent_item *ei;
1488 key.objectid = bytenr;
1489 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1490 key.offset = num_bytes;
1492 path = btrfs_alloc_path();
1496 ret = lookup_extent_backref(trans, extent_root, path,
1497 bytenr, parent, root_objectid,
1498 ref_generation, owner_objectid, 1);
1500 struct btrfs_key found_key;
1501 extent_slot = path->slots[0];
1502 while(extent_slot > 0) {
1504 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1506 if (found_key.objectid != bytenr)
1508 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1509 found_key.offset == num_bytes) {
1513 if (path->slots[0] - extent_slot > 5)
1516 if (!found_extent) {
1517 ret = remove_extent_backref(trans, extent_root, path);
1519 btrfs_release_path(extent_root, path);
1520 ret = btrfs_search_slot(trans, extent_root,
1523 extent_slot = path->slots[0];
1526 btrfs_print_leaf(extent_root, path->nodes[0]);
1527 printk("Unable to find ref byte nr %llu root %llu "
1528 " gen %llu owner %llu\n",
1529 (unsigned long long)bytenr,
1530 (unsigned long long)root_objectid,
1531 (unsigned long long)ref_generation,
1532 (unsigned long long)owner_objectid);
1536 leaf = path->nodes[0];
1537 ei = btrfs_item_ptr(leaf, extent_slot,
1538 struct btrfs_extent_item);
1539 refs = btrfs_extent_refs(leaf, ei);
1542 btrfs_set_extent_refs(leaf, ei, refs);
1544 btrfs_mark_buffer_dirty(leaf);
1546 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1547 struct btrfs_extent_ref *ref;
1548 ref = btrfs_item_ptr(leaf, path->slots[0],
1549 struct btrfs_extent_ref);
1550 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1551 /* if the back ref and the extent are next to each other
1552 * they get deleted below in one shot
1554 path->slots[0] = extent_slot;
1556 } else if (found_extent) {
1557 /* otherwise delete the extent back ref */
1558 ret = remove_extent_backref(trans, extent_root, path);
1560 /* if refs are 0, we need to setup the path for deletion */
1562 btrfs_release_path(extent_root, path);
1563 ret = btrfs_search_slot(trans, extent_root, &key, path,
1576 ret = pin_down_bytes(trans, root, bytenr, num_bytes, 0);
1582 /* block accounting for super block */
1583 super_used = btrfs_super_bytes_used(&info->super_copy);
1584 btrfs_set_super_bytes_used(&info->super_copy,
1585 super_used - num_bytes);
1587 /* block accounting for root item */
1588 root_used = btrfs_root_used(&root->root_item);
1589 btrfs_set_root_used(&root->root_item,
1590 root_used - num_bytes);
1591 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1596 if (ops && ops->free_extent)
1597 ops->free_extent(root, bytenr, num_bytes);
1599 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1603 btrfs_free_path(path);
1604 finish_current_insert(trans, extent_root);
1609 * find all the blocks marked as pending in the radix tree and remove
1610 * them from the extent map
1612 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1613 btrfs_root *extent_root)
1621 struct extent_io_tree *pending_del;
1622 struct extent_io_tree *extent_ins;
1623 struct pending_extent_op *extent_op;
1625 extent_ins = &extent_root->fs_info->extent_ins;
1626 pending_del = &extent_root->fs_info->pending_del;
1629 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1634 ret = get_state_private(pending_del, start, &priv);
1636 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1638 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1641 ret = pin_down_bytes(trans, extent_root, start,
1642 end + 1 - start, 0);
1643 mark_free = ret > 0;
1644 if (!test_range_bit(extent_ins, start, end,
1645 EXTENT_LOCKED, 0)) {
1647 ret = __free_extent(trans, extent_root,
1648 start, end + 1 - start,
1649 extent_op->orig_parent,
1650 extent_root->root_key.objectid,
1651 extent_op->orig_generation,
1652 extent_op->level, 0, mark_free);
1656 ret = get_state_private(extent_ins, start, &priv);
1658 extent_op = (struct pending_extent_op *)
1659 (unsigned long)priv;
1661 clear_extent_bits(extent_ins, start, end,
1662 EXTENT_LOCKED, GFP_NOFS);
1664 if (extent_op->type == PENDING_BACKREF_UPDATE)
1667 ret = update_block_group(trans, extent_root, start,
1668 end + 1 - start, 0, mark_free);
1679 * remove an extent from the root, returns 0 on success
1681 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1682 *root, u64 bytenr, u64 num_bytes, u64 parent,
1683 u64 root_objectid, u64 ref_generation,
1684 u64 owner_objectid, int pin)
1686 struct btrfs_root *extent_root = root->fs_info->extent_root;
1690 WARN_ON(num_bytes < root->sectorsize);
1691 if (root == extent_root) {
1692 struct pending_extent_op *extent_op;
1694 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1697 extent_op->type = PENDING_EXTENT_DELETE;
1698 extent_op->bytenr = bytenr;
1699 extent_op->num_bytes = num_bytes;
1700 extent_op->parent = parent;
1701 extent_op->orig_parent = parent;
1702 extent_op->generation = ref_generation;
1703 extent_op->orig_generation = ref_generation;
1704 extent_op->level = (int)owner_objectid;
1706 set_extent_bits(&root->fs_info->pending_del,
1707 bytenr, bytenr + num_bytes - 1,
1708 EXTENT_LOCKED, GFP_NOFS);
1709 set_state_private(&root->fs_info->pending_del,
1710 bytenr, (unsigned long)extent_op);
1713 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
1714 root_objectid, ref_generation,
1715 owner_objectid, pin, pin == 0);
1716 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1717 return ret ? ret : pending_ret;
1720 static u64 stripe_align(struct btrfs_root *root, u64 val)
1722 u64 mask = ((u64)root->stripesize - 1);
1723 u64 ret = (val + mask) & ~mask;
1728 * walks the btree of allocated extents and find a hole of a given size.
1729 * The key ins is changed to record the hole:
1730 * ins->objectid == block start
1731 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1732 * ins->offset == number of blocks
1733 * Any available blocks before search_start are skipped.
1735 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *orig_root,
1737 u64 num_bytes, u64 empty_size,
1738 u64 search_start, u64 search_end,
1739 u64 hint_byte, struct btrfs_key *ins,
1740 u64 exclude_start, u64 exclude_nr,
1744 u64 orig_search_start = search_start;
1745 struct btrfs_root * root = orig_root->fs_info->extent_root;
1746 struct btrfs_fs_info *info = root->fs_info;
1747 u64 total_needed = num_bytes;
1748 struct btrfs_block_group_cache *block_group;
1752 WARN_ON(num_bytes < root->sectorsize);
1753 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1756 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1758 hint_byte = search_start;
1759 block_group = btrfs_find_block_group(root, block_group,
1760 hint_byte, data, 1);
1762 block_group = btrfs_find_block_group(root,
1764 search_start, data, 1);
1767 total_needed += empty_size;
1771 block_group = btrfs_lookup_first_block_group(info,
1774 block_group = btrfs_lookup_first_block_group(info,
1777 ret = find_search_start(root, &block_group, &search_start,
1778 total_needed, data);
1782 search_start = stripe_align(root, search_start);
1783 ins->objectid = search_start;
1784 ins->offset = num_bytes;
1786 if (ins->objectid + num_bytes >
1787 block_group->key.objectid + block_group->key.offset) {
1788 search_start = block_group->key.objectid +
1789 block_group->key.offset;
1793 if (test_range_bit(&info->extent_ins, ins->objectid,
1794 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1795 search_start = ins->objectid + num_bytes;
1799 if (test_range_bit(&info->pinned_extents, ins->objectid,
1800 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1801 search_start = ins->objectid + num_bytes;
1805 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1806 ins->objectid < exclude_start + exclude_nr)) {
1807 search_start = exclude_start + exclude_nr;
1811 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1812 block_group = btrfs_lookup_block_group(info, ins->objectid);
1814 trans->block_group = block_group;
1816 ins->offset = num_bytes;
1820 block_group = btrfs_lookup_first_block_group(info, search_start);
1822 search_start = orig_search_start;
1829 total_needed -= empty_size;
1835 block_group = btrfs_find_block_group(root, block_group,
1836 search_start, data, 0);
1843 * finds a free extent and does all the dirty work required for allocation
1844 * returns the key for the extent through ins, and a tree buffer for
1845 * the first block of the extent through buf.
1847 * returns 0 if everything worked, non-zero otherwise.
1849 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1850 struct btrfs_root *root,
1851 u64 num_bytes, u64 parent,
1852 u64 root_objectid, u64 ref_generation,
1853 u64 owner, u64 empty_size, u64 hint_byte,
1854 u64 search_end, struct btrfs_key *ins, int data)
1858 u64 super_used, root_used;
1859 u64 search_start = 0;
1862 struct btrfs_fs_info *info = root->fs_info;
1863 struct btrfs_root *extent_root = info->extent_root;
1864 struct btrfs_path *path;
1865 struct btrfs_extent_item *extent_item;
1866 struct btrfs_extent_ref *ref;
1867 struct btrfs_key keys[2];
1869 if (info->extent_ops) {
1870 struct btrfs_extent_ops *ops = info->extent_ops;
1871 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1877 alloc_profile = info->avail_data_alloc_bits &
1878 info->data_alloc_profile;
1879 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1880 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
1881 info->system_allocs >= 0) {
1882 alloc_profile = info->avail_system_alloc_bits &
1883 info->system_alloc_profile;
1884 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1886 alloc_profile = info->avail_metadata_alloc_bits &
1887 info->metadata_alloc_profile;
1888 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1891 if (root->ref_cows) {
1892 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1893 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1895 BTRFS_BLOCK_GROUP_METADATA);
1898 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1899 num_bytes + 2 * 1024 * 1024, data);
1903 WARN_ON(num_bytes < root->sectorsize);
1904 ret = find_free_extent(trans, root, num_bytes, empty_size,
1905 search_start, search_end, hint_byte, ins,
1906 trans->alloc_exclude_start,
1907 trans->alloc_exclude_nr, data);
1914 parent = ins->objectid;
1916 /* block accounting for super block */
1917 super_used = btrfs_super_bytes_used(&info->super_copy);
1918 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1920 /* block accounting for root item */
1921 root_used = btrfs_root_used(&root->root_item);
1922 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1924 clear_extent_dirty(&root->fs_info->free_space_cache,
1925 ins->objectid, ins->objectid + ins->offset - 1,
1928 if (root == extent_root) {
1929 struct pending_extent_op *extent_op;
1931 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1934 extent_op->type = PENDING_EXTENT_INSERT;
1935 extent_op->bytenr = ins->objectid;
1936 extent_op->num_bytes = ins->offset;
1937 extent_op->parent = parent;
1938 extent_op->orig_parent = 0;
1939 extent_op->generation = ref_generation;
1940 extent_op->orig_generation = 0;
1941 extent_op->level = (int)owner;
1943 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1944 ins->objectid + ins->offset - 1,
1945 EXTENT_LOCKED, GFP_NOFS);
1946 set_state_private(&root->fs_info->extent_ins,
1947 ins->objectid, (unsigned long)extent_op);
1951 WARN_ON(trans->alloc_exclude_nr);
1952 trans->alloc_exclude_start = ins->objectid;
1953 trans->alloc_exclude_nr = ins->offset;
1955 memcpy(&keys[0], ins, sizeof(*ins));
1956 keys[1].objectid = ins->objectid;
1957 keys[1].type = BTRFS_EXTENT_REF_KEY;
1958 keys[1].offset = parent;
1959 sizes[0] = sizeof(*extent_item);
1960 sizes[1] = sizeof(*ref);
1962 path = btrfs_alloc_path();
1965 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1969 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1970 struct btrfs_extent_item);
1971 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1972 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1973 struct btrfs_extent_ref);
1975 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1976 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1977 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1978 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
1980 btrfs_mark_buffer_dirty(path->nodes[0]);
1982 trans->alloc_exclude_start = 0;
1983 trans->alloc_exclude_nr = 0;
1984 btrfs_free_path(path);
1985 finish_current_insert(trans, extent_root);
1986 pending_ret = del_pending_extents(trans, extent_root);
1996 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1998 printk("update block group failed for %llu %llu\n",
1999 (unsigned long long)ins->objectid,
2000 (unsigned long long)ins->offset);
2007 * helper function to allocate a block for a given tree
2008 * returns the tree buffer or NULL.
2010 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 u32 blocksize, u64 parent,
2019 struct btrfs_key ins;
2021 struct extent_buffer *buf;
2023 ret = btrfs_alloc_extent(trans, root, blocksize, parent,
2024 root_objectid, ref_generation,
2025 level, empty_size, hint,
2029 return ERR_PTR(ret);
2031 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2034 parent = ins.objectid;
2035 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2036 parent, root->root_key.objectid,
2037 ref_generation, level, 0);
2039 return ERR_PTR(-ENOMEM);
2041 btrfs_set_buffer_uptodate(buf);
2042 trans->blocks_used++;
2046 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2047 struct btrfs_root *root,
2048 struct extent_buffer *leaf)
2051 u64 leaf_generation;
2052 struct btrfs_key key;
2053 struct btrfs_file_extent_item *fi;
2058 BUG_ON(!btrfs_is_leaf(leaf));
2059 nritems = btrfs_header_nritems(leaf);
2060 leaf_owner = btrfs_header_owner(leaf);
2061 leaf_generation = btrfs_header_generation(leaf);
2063 for (i = 0; i < nritems; i++) {
2066 btrfs_item_key_to_cpu(leaf, &key, i);
2067 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2069 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2070 if (btrfs_file_extent_type(leaf, fi) ==
2071 BTRFS_FILE_EXTENT_INLINE)
2074 * FIXME make sure to insert a trans record that
2075 * repeats the snapshot del on crash
2077 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2078 if (disk_bytenr == 0)
2080 ret = btrfs_free_extent(trans, root, disk_bytenr,
2081 btrfs_file_extent_disk_num_bytes(leaf, fi),
2082 leaf->start, leaf_owner, leaf_generation,
2089 static void noinline reada_walk_down(struct btrfs_root *root,
2090 struct extent_buffer *node,
2103 nritems = btrfs_header_nritems(node);
2104 level = btrfs_header_level(node);
2108 for (i = slot; i < nritems && skipped < 32; i++) {
2109 bytenr = btrfs_node_blockptr(node, i);
2110 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2111 (last > bytenr && last - bytenr > 32 * 1024))) {
2115 blocksize = btrfs_level_size(root, level - 1);
2117 ret = lookup_extent_ref(NULL, root, bytenr,
2125 mutex_unlock(&root->fs_info->fs_mutex);
2126 ret = readahead_tree_block(root, bytenr, blocksize,
2127 btrfs_node_ptr_generation(node, i));
2128 last = bytenr + blocksize;
2130 mutex_lock(&root->fs_info->fs_mutex);
2137 * helper function for drop_snapshot, this walks down the tree dropping ref
2138 * counts as it goes.
2140 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2141 struct btrfs_root *root,
2142 struct btrfs_path *path, int *level)
2148 struct extent_buffer *next;
2149 struct extent_buffer *cur;
2150 struct extent_buffer *parent;
2155 WARN_ON(*level < 0);
2156 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2157 ret = lookup_extent_ref(trans, root,
2158 path->nodes[*level]->start,
2159 path->nodes[*level]->len, &refs);
2165 * walk down to the last node level and free all the leaves
2167 while(*level >= 0) {
2168 WARN_ON(*level < 0);
2169 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2170 cur = path->nodes[*level];
2172 if (btrfs_header_level(cur) != *level)
2175 if (path->slots[*level] >=
2176 btrfs_header_nritems(cur))
2179 ret = drop_leaf_ref(trans, root, cur);
2183 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2184 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2185 blocksize = btrfs_level_size(root, *level - 1);
2186 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2189 parent = path->nodes[*level];
2190 root_owner = btrfs_header_owner(parent);
2191 root_gen = btrfs_header_generation(parent);
2192 path->slots[*level]++;
2193 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2194 parent->start, root_owner,
2195 root_gen, *level - 1, 1);
2199 next = btrfs_find_tree_block(root, bytenr, blocksize);
2200 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2201 free_extent_buffer(next);
2202 reada_walk_down(root, cur, path->slots[*level]);
2203 mutex_unlock(&root->fs_info->fs_mutex);
2204 next = read_tree_block(root, bytenr, blocksize,
2206 mutex_lock(&root->fs_info->fs_mutex);
2208 WARN_ON(*level <= 0);
2209 if (path->nodes[*level-1])
2210 free_extent_buffer(path->nodes[*level-1]);
2211 path->nodes[*level-1] = next;
2212 *level = btrfs_header_level(next);
2213 path->slots[*level] = 0;
2216 WARN_ON(*level < 0);
2217 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2219 if (path->nodes[*level] == root->node) {
2220 root_owner = root->root_key.objectid;
2221 parent = path->nodes[*level];
2223 parent = path->nodes[*level + 1];
2224 root_owner = btrfs_header_owner(parent);
2227 root_gen = btrfs_header_generation(parent);
2228 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2229 path->nodes[*level]->len, parent->start,
2230 root_owner, root_gen, *level, 1);
2231 free_extent_buffer(path->nodes[*level]);
2232 path->nodes[*level] = NULL;
2239 * helper for dropping snapshots. This walks back up the tree in the path
2240 * to find the first node higher up where we haven't yet gone through
2243 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2244 struct btrfs_root *root,
2245 struct btrfs_path *path, int *level)
2249 struct btrfs_root_item *root_item = &root->root_item;
2254 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2255 slot = path->slots[i];
2256 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2257 struct extent_buffer *node;
2258 struct btrfs_disk_key disk_key;
2259 node = path->nodes[i];
2262 WARN_ON(*level == 0);
2263 btrfs_node_key(node, &disk_key, path->slots[i]);
2264 memcpy(&root_item->drop_progress,
2265 &disk_key, sizeof(disk_key));
2266 root_item->drop_level = i;
2269 struct extent_buffer *parent;
2270 if (path->nodes[*level] == root->node)
2271 parent = path->nodes[*level];
2273 parent = path->nodes[*level + 1];
2275 root_owner = btrfs_header_owner(parent);
2276 root_gen = btrfs_header_generation(parent);
2277 ret = btrfs_free_extent(trans, root,
2278 path->nodes[*level]->start,
2279 path->nodes[*level]->len,
2280 parent->start, root_owner,
2281 root_gen, *level, 1);
2283 free_extent_buffer(path->nodes[*level]);
2284 path->nodes[*level] = NULL;
2292 * drop the reference count on the tree rooted at 'snap'. This traverses
2293 * the tree freeing any blocks that have a ref count of zero after being
2296 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2302 struct btrfs_path *path;
2305 struct btrfs_root_item *root_item = &root->root_item;
2307 path = btrfs_alloc_path();
2310 level = btrfs_header_level(root->node);
2312 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2313 path->nodes[level] = root->node;
2314 extent_buffer_get(root->node);
2315 path->slots[level] = 0;
2317 struct btrfs_key key;
2318 struct btrfs_disk_key found_key;
2319 struct extent_buffer *node;
2321 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2322 level = root_item->drop_level;
2323 path->lowest_level = level;
2324 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2329 node = path->nodes[level];
2330 btrfs_node_key(node, &found_key, path->slots[level]);
2331 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2332 sizeof(found_key)));
2335 wret = walk_down_tree(trans, root, path, &level);
2341 wret = walk_up_tree(trans, root, path, &level);
2351 for (i = 0; i <= orig_level; i++) {
2352 if (path->nodes[i]) {
2353 free_extent_buffer(path->nodes[i]);
2354 path->nodes[i] = NULL;
2358 btrfs_free_path(path);
2362 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2369 ret = find_first_extent_bit(&info->block_group_cache, 0,
2370 &start, &end, (unsigned int)-1);
2373 ret = get_state_private(&info->block_group_cache, start, &ptr);
2375 kfree((void *)(unsigned long)ptr);
2376 clear_extent_bits(&info->block_group_cache, start,
2377 end, (unsigned int)-1, GFP_NOFS);
2380 ret = find_first_extent_bit(&info->free_space_cache, 0,
2381 &start, &end, EXTENT_DIRTY);
2384 clear_extent_dirty(&info->free_space_cache, start,
2390 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2391 struct btrfs_key *key)
2394 struct btrfs_key found_key;
2395 struct extent_buffer *leaf;
2398 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2402 slot = path->slots[0];
2403 leaf = path->nodes[0];
2404 if (slot >= btrfs_header_nritems(leaf)) {
2405 ret = btrfs_next_leaf(root, path);
2412 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2414 if (found_key.objectid >= key->objectid &&
2415 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2424 int btrfs_read_block_groups(struct btrfs_root *root)
2426 struct btrfs_path *path;
2429 struct btrfs_block_group_cache *cache;
2430 struct btrfs_fs_info *info = root->fs_info;
2431 struct btrfs_space_info *space_info;
2432 struct extent_io_tree *block_group_cache;
2433 struct btrfs_key key;
2434 struct btrfs_key found_key;
2435 struct extent_buffer *leaf;
2437 block_group_cache = &info->block_group_cache;
2439 root = info->extent_root;
2442 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2443 path = btrfs_alloc_path();
2448 ret = find_first_block_group(root, path, &key);
2456 leaf = path->nodes[0];
2457 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2458 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2464 read_extent_buffer(leaf, &cache->item,
2465 btrfs_item_ptr_offset(leaf, path->slots[0]),
2466 sizeof(cache->item));
2467 memcpy(&cache->key, &found_key, sizeof(found_key));
2470 key.objectid = found_key.objectid + found_key.offset;
2471 btrfs_release_path(root, path);
2472 cache->flags = btrfs_block_group_flags(&cache->item);
2474 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
2475 bit = BLOCK_GROUP_DATA;
2476 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2477 bit = BLOCK_GROUP_SYSTEM;
2478 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
2479 bit = BLOCK_GROUP_METADATA;
2481 set_avail_alloc_bits(info, cache->flags);
2482 if (btrfs_chunk_readonly(root, cache->key.objectid))
2485 ret = update_space_info(info, cache->flags, found_key.offset,
2486 btrfs_block_group_used(&cache->item),
2489 cache->space_info = space_info;
2491 /* use EXTENT_LOCKED to prevent merging */
2492 set_extent_bits(block_group_cache, found_key.objectid,
2493 found_key.objectid + found_key.offset - 1,
2494 bit | EXTENT_LOCKED, GFP_NOFS);
2495 set_state_private(block_group_cache, found_key.objectid,
2496 (unsigned long)cache);
2500 btrfs_free_path(path);
2504 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2505 struct btrfs_root *root, u64 bytes_used,
2506 u64 type, u64 chunk_objectid, u64 chunk_offset,
2511 struct btrfs_root *extent_root;
2512 struct btrfs_block_group_cache *cache;
2513 struct extent_io_tree *block_group_cache;
2515 extent_root = root->fs_info->extent_root;
2516 block_group_cache = &root->fs_info->block_group_cache;
2518 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2520 cache->key.objectid = chunk_offset;
2521 cache->key.offset = size;
2523 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2524 btrfs_set_block_group_used(&cache->item, bytes_used);
2525 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2526 cache->flags = type;
2527 btrfs_set_block_group_flags(&cache->item, type);
2529 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
2530 &cache->space_info);
2533 bit = block_group_state_bits(type);
2534 set_extent_bits(block_group_cache, chunk_offset,
2535 chunk_offset + size - 1,
2536 bit | EXTENT_LOCKED, GFP_NOFS);
2538 set_state_private(block_group_cache, chunk_offset,
2539 (unsigned long)cache);
2540 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2541 sizeof(cache->item));
2544 finish_current_insert(trans, extent_root);
2545 ret = del_pending_extents(trans, extent_root);
2547 set_avail_alloc_bits(extent_root->fs_info, type);
2552 * This is for converter use only.
2554 * In that case, we don't know where are free blocks located.
2555 * Therefore all block group cache entries must be setup properly
2556 * before doing any block allocation.
2558 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2559 struct btrfs_root *root)
2567 u64 total_metadata = 0;
2571 struct btrfs_root *extent_root;
2572 struct btrfs_block_group_cache *cache;
2573 struct extent_io_tree *block_group_cache;
2575 extent_root = root->fs_info->extent_root;
2576 block_group_cache = &root->fs_info->block_group_cache;
2577 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2578 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2579 group_align = 64 * root->sectorsize;
2582 while (cur_start < total_bytes) {
2583 group_size = total_bytes / 12;
2584 group_size = min_t(u64, group_size, total_bytes - cur_start);
2585 if (cur_start == 0) {
2586 bit = BLOCK_GROUP_SYSTEM;
2587 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
2589 group_size &= ~(group_align - 1);
2590 group_size = max_t(u64, group_size, 32 * 1024 * 1024);
2591 group_size = min_t(u64, group_size, 128 * 1024 * 1024);
2593 group_size &= ~(group_align - 1);
2594 if (total_data >= total_metadata * 2) {
2595 group_type = BTRFS_BLOCK_GROUP_METADATA;
2596 group_size = min_t(u64, group_size,
2597 1ULL * 1024 * 1024 * 1024);
2598 total_metadata += group_size;
2600 group_type = BTRFS_BLOCK_GROUP_DATA;
2601 group_size = min_t(u64, group_size,
2602 5ULL * 1024 * 1024 * 1024);
2603 total_data += group_size;
2605 if ((total_bytes - cur_start) * 4 < group_size * 5)
2606 group_size = total_bytes - cur_start;
2609 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2612 cache->key.objectid = cur_start;
2613 cache->key.offset = group_size;
2614 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2616 btrfs_set_block_group_used(&cache->item, 0);
2617 btrfs_set_block_group_chunk_objectid(&cache->item,
2619 btrfs_set_block_group_flags(&cache->item, group_type);
2621 cache->flags = group_type;
2623 ret = update_space_info(root->fs_info, group_type, group_size,
2624 0, &cache->space_info);
2626 set_avail_alloc_bits(extent_root->fs_info, group_type);
2628 set_extent_bits(block_group_cache, cur_start,
2629 cur_start + group_size - 1,
2630 bit | EXTENT_LOCKED, GFP_NOFS);
2631 set_state_private(block_group_cache, cur_start,
2632 (unsigned long)cache);
2633 cur_start += group_size;
2635 /* then insert all the items */
2637 while(cur_start < total_bytes) {
2638 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2641 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2642 sizeof(cache->item));
2645 finish_current_insert(trans, extent_root);
2646 ret = del_pending_extents(trans, extent_root);
2649 cur_start = cache->key.objectid + cache->key.offset;
2654 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2655 struct btrfs_root *root,
2656 u64 bytenr, u64 num_bytes, int alloc,
2659 return update_block_group(trans, root, bytenr, num_bytes,