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;
104 root = root->fs_info->extent_root;
105 free_space_cache = &root->fs_info->free_space_cache;
107 if (block_group->cached)
110 path = btrfs_alloc_path();
115 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
118 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
124 leaf = path->nodes[0];
125 slot = path->slots[0];
126 if (slot >= btrfs_header_nritems(leaf)) {
127 ret = btrfs_next_leaf(root, path);
136 btrfs_item_key_to_cpu(leaf, &key, slot);
137 if (key.objectid < block_group->key.objectid) {
140 if (key.objectid >= block_group->key.objectid +
141 block_group->key.offset) {
145 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
146 if (key.objectid > last) {
147 hole_size = key.objectid - last;
148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1,
152 last = key.objectid + key.offset;
158 if (block_group->key.objectid +
159 block_group->key.offset > last) {
160 hole_size = block_group->key.objectid +
161 block_group->key.offset - last;
162 set_extent_dirty(free_space_cache, last,
163 last + hole_size - 1, GFP_NOFS);
165 remove_sb_from_cache(root, block_group);
166 block_group->cached = 1;
168 btrfs_free_path(path);
172 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
176 struct extent_io_tree *block_group_cache;
177 struct btrfs_block_group_cache *block_group = NULL;
183 bytenr = max_t(u64, bytenr,
184 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
185 block_group_cache = &info->block_group_cache;
186 ret = find_first_extent_bit(block_group_cache,
187 bytenr, &start, &end,
188 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
193 ret = get_state_private(block_group_cache, start, &ptr);
197 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
201 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
205 struct extent_io_tree *block_group_cache;
206 struct btrfs_block_group_cache *block_group = NULL;
212 block_group_cache = &info->block_group_cache;
213 ret = find_first_extent_bit(block_group_cache,
214 bytenr, &start, &end,
215 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
220 ret = get_state_private(block_group_cache, start, &ptr);
224 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
225 if (block_group->key.objectid <= bytenr && bytenr <
226 block_group->key.objectid + block_group->key.offset)
231 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
233 return (cache->flags & bits) == bits;
236 static int noinline find_search_start(struct btrfs_root *root,
237 struct btrfs_block_group_cache **cache_ret,
238 u64 *start_ret, int num, int data)
241 struct btrfs_block_group_cache *cache = *cache_ret;
245 u64 search_start = *start_ret;
252 ret = cache_block_group(root, cache);
256 last = max(search_start, cache->key.objectid);
257 if (cache->ro || !block_group_bits(cache, data)) {
262 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
263 last, &start, &end, EXTENT_DIRTY);
268 start = max(last, start);
270 if (last - start < num) {
273 if (start + num > cache->key.objectid + cache->key.offset) {
280 cache = btrfs_lookup_block_group(root->fs_info, search_start);
282 printk("Unable to find block group for %llu\n",
283 (unsigned long long)search_start);
289 last = cache->key.objectid + cache->key.offset;
291 cache = btrfs_lookup_first_block_group(root->fs_info, last);
301 cache = btrfs_find_block_group(root, cache, last, data, 0);
302 cache = btrfs_find_block_group(root, cache, last, data, 0);
310 static u64 div_factor(u64 num, int factor)
319 static int block_group_state_bits(u64 flags)
322 if (flags & BTRFS_BLOCK_GROUP_DATA)
323 bits |= BLOCK_GROUP_DATA;
324 if (flags & BTRFS_BLOCK_GROUP_METADATA)
325 bits |= BLOCK_GROUP_METADATA;
326 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
327 bits |= BLOCK_GROUP_SYSTEM;
331 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
332 struct btrfs_block_group_cache
333 *hint, u64 search_start,
336 struct btrfs_block_group_cache *cache;
337 struct extent_io_tree *block_group_cache;
338 struct btrfs_block_group_cache *found_group = NULL;
339 struct btrfs_fs_info *info = root->fs_info;
352 block_group_cache = &info->block_group_cache;
357 bit = block_group_state_bits(data);
360 struct btrfs_block_group_cache *shint;
361 shint = btrfs_lookup_block_group(info, search_start);
362 if (shint && !shint->ro && block_group_bits(shint, data)) {
363 used = btrfs_block_group_used(&shint->item);
364 if (used + shint->pinned <
365 div_factor(shint->key.offset, factor)) {
370 if (hint && !hint->ro && block_group_bits(hint, data)) {
371 used = btrfs_block_group_used(&hint->item);
372 if (used + hint->pinned <
373 div_factor(hint->key.offset, factor)) {
376 last = hint->key.objectid + hint->key.offset;
380 hint_last = max(hint->key.objectid, search_start);
382 hint_last = search_start;
388 ret = find_first_extent_bit(block_group_cache, last,
393 ret = get_state_private(block_group_cache, start, &ptr);
397 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
398 last = cache->key.objectid + cache->key.offset;
399 used = btrfs_block_group_used(&cache->item);
401 if (!cache->ro && block_group_bits(cache, data)) {
403 free_check = cache->key.offset;
405 free_check = div_factor(cache->key.offset,
408 if (used + cache->pinned < free_check) {
425 * Back reference rules. Back refs have three main goals:
427 * 1) differentiate between all holders of references to an extent so that
428 * when a reference is dropped we can make sure it was a valid reference
429 * before freeing the extent.
431 * 2) Provide enough information to quickly find the holders of an extent
432 * if we notice a given block is corrupted or bad.
434 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
435 * maintenance. This is actually the same as #2, but with a slightly
436 * different use case.
438 * File extents can be referenced by:
440 * - multiple snapshots, subvolumes, or different generations in one subvol
441 * - different files inside a single subvolume
442 * - different offsets inside a file (bookend extents in file.c)
444 * The extent ref structure has fields for:
446 * - Objectid of the subvolume root
447 * - Generation number of the tree holding the reference
448 * - objectid of the file holding the reference
449 * - offset in the file corresponding to the key holding the reference
450 * - number of references holding by parent node (alway 1 for tree blocks)
452 * Btree leaf may hold multiple references to a file extent. In most cases,
453 * these references are from same file and the corresponding offsets inside
454 * the file are close together. So inode objectid and offset in file are
455 * just hints, they provide hints about where in the btree the references
456 * can be found and when we can stop searching.
458 * When a file extent is allocated the fields are filled in:
459 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
461 * When a leaf is cow'd new references are added for every file extent found
462 * in the leaf. It looks similar to the create case, but trans->transid will
463 * be different when the block is cow'd.
465 * (root_key.objectid, trans->transid, inode objectid, offset in file,
466 * number of references in the leaf)
468 * Because inode objectid and offset in file are just hints, they are not
469 * used when backrefs are deleted. When a file extent is removed either
470 * during snapshot deletion or file truncation, we find the corresponding
471 * back back reference and check the following fields.
473 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
475 * Btree extents can be referenced by:
477 * - Different subvolumes
478 * - Different generations of the same subvolume
480 * When a tree block is created, back references are inserted:
482 * (root->root_key.objectid, trans->transid, level, 0, 1)
484 * When a tree block is cow'd, new back references are added for all the
485 * blocks it points to. If the tree block isn't in reference counted root,
486 * the old back references are removed. These new back references are of
487 * the form (trans->transid will have increased since creation):
489 * (root->root_key.objectid, trans->transid, level, 0, 1)
491 * When a backref is in deleting, the following fields are checked:
493 * if backref was for a tree root:
494 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
496 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
498 * Back Reference Key composing:
500 * The key objectid corresponds to the first byte in the extent, the key
501 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
502 * byte of parent extent. If a extent is tree root, the key offset is set
503 * to the key objectid.
506 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 u64 bytenr, u64 parent,
510 u64 ref_root, u64 ref_generation,
511 u64 owner_objectid, int del)
513 struct btrfs_key key;
514 struct btrfs_extent_ref *ref;
515 struct extent_buffer *leaf;
519 key.objectid = bytenr;
520 key.type = BTRFS_EXTENT_REF_KEY;
523 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
531 leaf = path->nodes[0];
532 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
533 ref_objectid = btrfs_ref_objectid(leaf, ref);
534 if (btrfs_ref_root(leaf, ref) != ref_root ||
535 btrfs_ref_generation(leaf, ref) != ref_generation ||
536 (ref_objectid != owner_objectid &&
537 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
547 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
548 struct btrfs_root *root,
549 struct btrfs_path *path,
550 u64 bytenr, u64 parent,
551 u64 ref_root, u64 ref_generation,
554 struct btrfs_key key;
555 struct extent_buffer *leaf;
556 struct btrfs_extent_ref *ref;
560 key.objectid = bytenr;
561 key.type = BTRFS_EXTENT_REF_KEY;
564 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
566 leaf = path->nodes[0];
567 ref = btrfs_item_ptr(leaf, path->slots[0],
568 struct btrfs_extent_ref);
569 btrfs_set_ref_root(leaf, ref, ref_root);
570 btrfs_set_ref_generation(leaf, ref, ref_generation);
571 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
572 btrfs_set_ref_num_refs(leaf, ref, 1);
573 } else if (ret == -EEXIST) {
575 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
576 leaf = path->nodes[0];
577 ref = btrfs_item_ptr(leaf, path->slots[0],
578 struct btrfs_extent_ref);
579 if (btrfs_ref_root(leaf, ref) != ref_root ||
580 btrfs_ref_generation(leaf, ref) != ref_generation) {
586 num_refs = btrfs_ref_num_refs(leaf, ref);
587 BUG_ON(num_refs == 0);
588 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
590 existing_owner = btrfs_ref_objectid(leaf, ref);
591 if (existing_owner != owner_objectid &&
592 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
593 btrfs_set_ref_objectid(leaf, ref,
594 BTRFS_MULTIPLE_OBJECTIDS);
600 btrfs_mark_buffer_dirty(path->nodes[0]);
602 btrfs_release_path(root, path);
606 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
607 struct btrfs_root *root,
608 struct btrfs_path *path)
610 struct extent_buffer *leaf;
611 struct btrfs_extent_ref *ref;
615 leaf = path->nodes[0];
616 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
617 num_refs = btrfs_ref_num_refs(leaf, ref);
618 BUG_ON(num_refs == 0);
621 ret = btrfs_del_item(trans, root, path);
623 btrfs_set_ref_num_refs(leaf, ref, num_refs);
624 btrfs_mark_buffer_dirty(leaf);
626 btrfs_release_path(root, path);
630 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
631 struct btrfs_root *root, u64 bytenr,
632 u64 orig_parent, u64 parent,
633 u64 orig_root, u64 ref_root,
634 u64 orig_generation, u64 ref_generation,
638 struct btrfs_root *extent_root = root->fs_info->extent_root;
639 struct btrfs_path *path;
641 if (root == root->fs_info->extent_root) {
642 struct pending_extent_op *extent_op;
645 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
646 num_bytes = btrfs_level_size(root, (int)owner_objectid);
647 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
648 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
650 ret = get_state_private(&root->fs_info->extent_ins,
653 extent_op = (struct pending_extent_op *)
655 BUG_ON(extent_op->parent != orig_parent);
656 BUG_ON(extent_op->generation != orig_generation);
657 extent_op->parent = parent;
658 extent_op->generation = ref_generation;
660 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
663 extent_op->type = PENDING_BACKREF_UPDATE;
664 extent_op->bytenr = bytenr;
665 extent_op->num_bytes = num_bytes;
666 extent_op->parent = parent;
667 extent_op->orig_parent = orig_parent;
668 extent_op->generation = ref_generation;
669 extent_op->orig_generation = orig_generation;
670 extent_op->level = (int)owner_objectid;
672 set_extent_bits(&root->fs_info->extent_ins,
673 bytenr, bytenr + num_bytes - 1,
674 EXTENT_LOCKED, GFP_NOFS);
675 set_state_private(&root->fs_info->extent_ins,
676 bytenr, (unsigned long)extent_op);
681 path = btrfs_alloc_path();
684 ret = lookup_extent_backref(trans, extent_root, path,
685 bytenr, orig_parent, orig_root,
686 orig_generation, owner_objectid, 1);
689 ret = remove_extent_backref(trans, extent_root, path);
692 ret = insert_extent_backref(trans, extent_root, path, bytenr,
693 parent, ref_root, ref_generation,
696 finish_current_insert(trans, extent_root);
697 del_pending_extents(trans, extent_root);
699 btrfs_free_path(path);
703 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
704 struct btrfs_root *root, u64 bytenr,
705 u64 orig_parent, u64 parent,
706 u64 ref_root, u64 ref_generation,
710 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
711 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
713 maybe_lock_mutex(root);
714 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
715 parent, ref_root, ref_root,
716 ref_generation, ref_generation,
718 maybe_unlock_mutex(root);
722 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
723 struct btrfs_root *root, u64 bytenr,
724 u64 orig_parent, u64 parent,
725 u64 orig_root, u64 ref_root,
726 u64 orig_generation, u64 ref_generation,
729 struct btrfs_path *path;
731 struct btrfs_key key;
732 struct extent_buffer *l;
733 struct btrfs_extent_item *item;
736 path = btrfs_alloc_path();
741 key.objectid = bytenr;
742 key.type = BTRFS_EXTENT_ITEM_KEY;
743 key.offset = (u64)-1;
745 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
749 BUG_ON(ret == 0 || path->slots[0] == 0);
754 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
755 BUG_ON(key.objectid != bytenr);
756 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
758 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
759 refs = btrfs_extent_refs(l, item);
760 btrfs_set_extent_refs(l, item, refs + 1);
761 btrfs_mark_buffer_dirty(path->nodes[0]);
763 btrfs_release_path(root->fs_info->extent_root, path);
766 ret = insert_extent_backref(trans, root->fs_info->extent_root,
767 path, bytenr, parent,
768 ref_root, ref_generation,
771 finish_current_insert(trans, root->fs_info->extent_root);
772 del_pending_extents(trans, root->fs_info->extent_root);
774 btrfs_free_path(path);
778 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
779 struct btrfs_root *root,
780 u64 bytenr, u64 num_bytes, u64 parent,
781 u64 ref_root, u64 ref_generation,
785 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
786 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
788 maybe_lock_mutex(root);
789 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
790 0, ref_root, 0, ref_generation,
792 maybe_unlock_mutex(root);
796 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
797 struct btrfs_root *root)
799 finish_current_insert(trans, root->fs_info->extent_root);
800 del_pending_extents(trans, root->fs_info->extent_root);
804 int lookup_extent_ref(struct btrfs_trans_handle *trans,
805 struct btrfs_root *root, u64 bytenr,
806 u64 num_bytes, u32 *refs)
808 struct btrfs_path *path;
810 struct btrfs_key key;
811 struct extent_buffer *l;
812 struct btrfs_extent_item *item;
814 WARN_ON(num_bytes < root->sectorsize);
815 path = btrfs_alloc_path();
817 key.objectid = bytenr;
818 key.offset = num_bytes;
819 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
820 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
825 btrfs_print_leaf(root, path->nodes[0]);
826 printk("failed to find block number %Lu\n", bytenr);
830 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
831 *refs = btrfs_extent_refs(l, item);
833 btrfs_free_path(path);
837 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
838 struct extent_buffer *orig_buf, struct extent_buffer *buf,
847 u32 nr_file_extents = 0;
848 struct btrfs_key key;
849 struct btrfs_file_extent_item *fi;
854 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
855 u64, u64, u64, u64, u64, u64, u64, u64);
857 ref_root = btrfs_header_owner(buf);
858 ref_generation = btrfs_header_generation(buf);
859 orig_root = btrfs_header_owner(orig_buf);
860 orig_generation = btrfs_header_generation(orig_buf);
862 nritems = btrfs_header_nritems(buf);
863 level = btrfs_header_level(buf);
865 if (root->ref_cows) {
866 process_func = __btrfs_inc_extent_ref;
869 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
871 process_func = __btrfs_update_extent_ref;
874 for (i = 0; i < nritems; i++) {
877 btrfs_item_key_to_cpu(buf, &key, i);
878 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
880 fi = btrfs_item_ptr(buf, i,
881 struct btrfs_file_extent_item);
882 if (btrfs_file_extent_type(buf, fi) ==
883 BTRFS_FILE_EXTENT_INLINE)
885 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
891 maybe_lock_mutex(root);
892 ret = process_func(trans, root, bytenr,
893 orig_buf->start, buf->start,
895 orig_generation, ref_generation,
897 maybe_unlock_mutex(root);
905 bytenr = btrfs_node_blockptr(buf, i);
906 maybe_lock_mutex(root);
907 ret = process_func(trans, root, bytenr,
908 orig_buf->start, buf->start,
910 orig_generation, ref_generation,
912 maybe_unlock_mutex(root);
923 *nr_extents = nr_file_extents;
925 *nr_extents = nritems;
931 for (i =0; i < faili; i++) {
934 btrfs_item_key_to_cpu(buf, &key, i);
935 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
937 fi = btrfs_item_ptr(buf, i,
938 struct btrfs_file_extent_item);
939 if (btrfs_file_extent_type(buf, fi) ==
940 BTRFS_FILE_EXTENT_INLINE)
942 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
943 if (disk_bytenr == 0)
945 err = btrfs_free_extent(trans, root, disk_bytenr,
946 btrfs_file_extent_disk_num_bytes(buf,
950 bytenr = btrfs_node_blockptr(buf, i);
951 err = btrfs_free_extent(trans, root, bytenr,
952 btrfs_level_size(root, level - 1), 0);
960 int btrfs_update_ref(struct btrfs_trans_handle *trans,
961 struct btrfs_root *root, struct extent_buffer *orig_buf,
962 struct extent_buffer *buf, int start_slot, int nr)
970 struct btrfs_key key;
971 struct btrfs_file_extent_item *fi;
977 BUG_ON(start_slot < 0);
978 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
980 ref_root = btrfs_header_owner(buf);
981 ref_generation = btrfs_header_generation(buf);
982 orig_root = btrfs_header_owner(orig_buf);
983 orig_generation = btrfs_header_generation(orig_buf);
984 level = btrfs_header_level(buf);
986 if (!root->ref_cows) {
988 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
992 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
995 btrfs_item_key_to_cpu(buf, &key, slot);
996 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
998 fi = btrfs_item_ptr(buf, slot,
999 struct btrfs_file_extent_item);
1000 if (btrfs_file_extent_type(buf, fi) ==
1001 BTRFS_FILE_EXTENT_INLINE)
1003 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1007 maybe_lock_mutex(root);
1008 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1009 orig_buf->start, buf->start,
1010 orig_root, ref_root,
1011 orig_generation, ref_generation,
1013 maybe_unlock_mutex(root);
1017 bytenr = btrfs_node_blockptr(buf, slot);
1018 maybe_lock_mutex(root);
1019 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1020 orig_buf->start, buf->start,
1021 orig_root, ref_root,
1022 orig_generation, ref_generation,
1024 maybe_unlock_mutex(root);
1035 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1036 struct btrfs_root *root,
1037 struct btrfs_path *path,
1038 struct btrfs_block_group_cache *cache)
1042 struct btrfs_root *extent_root = root->fs_info->extent_root;
1044 struct extent_buffer *leaf;
1046 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1051 leaf = path->nodes[0];
1052 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1053 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1054 btrfs_mark_buffer_dirty(leaf);
1055 btrfs_release_path(extent_root, path);
1057 finish_current_insert(trans, extent_root);
1058 pending_ret = del_pending_extents(trans, extent_root);
1067 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1068 struct btrfs_root *root)
1070 struct extent_io_tree *block_group_cache;
1071 struct btrfs_block_group_cache *cache;
1075 struct btrfs_path *path;
1081 block_group_cache = &root->fs_info->block_group_cache;
1082 path = btrfs_alloc_path();
1087 ret = find_first_extent_bit(block_group_cache, last,
1088 &start, &end, BLOCK_GROUP_DIRTY);
1093 ret = get_state_private(block_group_cache, start, &ptr);
1096 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1097 err = write_one_cache_group(trans, root,
1100 * if we fail to write the cache group, we want
1101 * to keep it marked dirty in hopes that a later
1108 clear_extent_bits(block_group_cache, start, end,
1109 BLOCK_GROUP_DIRTY, GFP_NOFS);
1111 btrfs_free_path(path);
1115 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1118 struct list_head *head = &info->space_info;
1119 struct list_head *cur;
1120 struct btrfs_space_info *found;
1121 list_for_each(cur, head) {
1122 found = list_entry(cur, struct btrfs_space_info, list);
1123 if (found->flags == flags)
1130 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1131 u64 total_bytes, u64 bytes_used,
1132 struct btrfs_space_info **space_info)
1134 struct btrfs_space_info *found;
1136 found = __find_space_info(info, flags);
1138 found->total_bytes += total_bytes;
1139 found->bytes_used += bytes_used;
1140 WARN_ON(found->total_bytes < found->bytes_used);
1141 *space_info = found;
1144 found = kmalloc(sizeof(*found), GFP_NOFS);
1148 list_add(&found->list, &info->space_info);
1149 found->flags = flags;
1150 found->total_bytes = total_bytes;
1151 found->bytes_used = bytes_used;
1152 found->bytes_pinned = 0;
1154 *space_info = found;
1159 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1161 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1162 BTRFS_BLOCK_GROUP_RAID1 |
1163 BTRFS_BLOCK_GROUP_DUP);
1165 if (flags & BTRFS_BLOCK_GROUP_DATA)
1166 fs_info->avail_data_alloc_bits |= extra_flags;
1167 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1168 fs_info->avail_metadata_alloc_bits |= extra_flags;
1169 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1170 fs_info->avail_system_alloc_bits |= extra_flags;
1174 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1175 struct btrfs_root *extent_root, u64 alloc_bytes,
1178 struct btrfs_space_info *space_info;
1184 space_info = __find_space_info(extent_root->fs_info, flags);
1186 ret = update_space_info(extent_root->fs_info, flags,
1190 BUG_ON(!space_info);
1192 if (space_info->full)
1195 thresh = div_factor(space_info->total_bytes, 7);
1196 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1200 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1201 if (ret == -ENOSPC) {
1202 space_info->full = 1;
1208 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1209 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1214 static int update_block_group(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 u64 bytenr, u64 num_bytes, int alloc,
1219 struct btrfs_block_group_cache *cache;
1220 struct btrfs_fs_info *info = root->fs_info;
1221 u64 total = num_bytes;
1228 cache = btrfs_lookup_block_group(info, bytenr);
1232 byte_in_group = bytenr - cache->key.objectid;
1233 WARN_ON(byte_in_group > cache->key.offset);
1234 start = cache->key.objectid;
1235 end = start + cache->key.offset - 1;
1236 set_extent_bits(&info->block_group_cache, start, end,
1237 BLOCK_GROUP_DIRTY, GFP_NOFS);
1239 old_val = btrfs_block_group_used(&cache->item);
1240 num_bytes = min(total, cache->key.offset - byte_in_group);
1242 old_val += num_bytes;
1243 cache->space_info->bytes_used += num_bytes;
1245 old_val -= num_bytes;
1246 cache->space_info->bytes_used -= num_bytes;
1248 set_extent_dirty(&info->free_space_cache,
1249 bytenr, bytenr + num_bytes - 1,
1253 btrfs_set_block_group_used(&cache->item, old_val);
1255 bytenr += num_bytes;
1260 static int update_pinned_extents(struct btrfs_root *root,
1261 u64 bytenr, u64 num, int pin)
1264 struct btrfs_block_group_cache *cache;
1265 struct btrfs_fs_info *fs_info = root->fs_info;
1268 set_extent_dirty(&fs_info->pinned_extents,
1269 bytenr, bytenr + num - 1, GFP_NOFS);
1271 clear_extent_dirty(&fs_info->pinned_extents,
1272 bytenr, bytenr + num - 1, GFP_NOFS);
1275 cache = btrfs_lookup_block_group(fs_info, bytenr);
1277 len = min(num, cache->key.offset -
1278 (bytenr - cache->key.objectid));
1280 cache->pinned += len;
1281 cache->space_info->bytes_pinned += len;
1282 fs_info->total_pinned += len;
1284 cache->pinned -= len;
1285 cache->space_info->bytes_pinned -= len;
1286 fs_info->total_pinned -= len;
1294 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1299 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1303 ret = find_first_extent_bit(pinned_extents, last,
1304 &start, &end, EXTENT_DIRTY);
1307 set_extent_dirty(copy, start, end, GFP_NOFS);
1313 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1314 struct btrfs_root *root,
1315 struct extent_io_tree *unpin)
1320 struct extent_io_tree *free_space_cache;
1321 free_space_cache = &root->fs_info->free_space_cache;
1324 ret = find_first_extent_bit(unpin, 0, &start, &end,
1328 update_pinned_extents(root, start, end + 1 - start, 0);
1329 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1330 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1335 static int finish_current_insert(struct btrfs_trans_handle *trans,
1336 struct btrfs_root *extent_root)
1341 struct btrfs_fs_info *info = extent_root->fs_info;
1342 struct btrfs_path *path;
1343 struct btrfs_extent_ref *ref;
1344 struct pending_extent_op *extent_op;
1345 struct btrfs_key key;
1346 struct btrfs_extent_item extent_item;
1350 btrfs_set_stack_extent_refs(&extent_item, 1);
1351 path = btrfs_alloc_path();
1354 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1355 &end, EXTENT_LOCKED);
1359 ret = get_state_private(&info->extent_ins, start, &priv);
1361 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1363 if (extent_op->type == PENDING_EXTENT_INSERT) {
1364 key.objectid = start;
1365 key.offset = end + 1 - start;
1366 key.type = BTRFS_EXTENT_ITEM_KEY;
1367 err = btrfs_insert_item(trans, extent_root, &key,
1368 &extent_item, sizeof(extent_item));
1371 clear_extent_bits(&info->extent_ins, start, end,
1372 EXTENT_LOCKED, GFP_NOFS);
1374 err = insert_extent_backref(trans, extent_root, path,
1375 start, extent_op->parent,
1376 extent_root->root_key.objectid,
1377 extent_op->generation,
1380 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1381 err = lookup_extent_backref(trans, extent_root, path,
1382 start, extent_op->orig_parent,
1383 extent_root->root_key.objectid,
1384 extent_op->orig_generation,
1385 extent_op->level, 0);
1388 clear_extent_bits(&info->extent_ins, start, end,
1389 EXTENT_LOCKED, GFP_NOFS);
1391 key.objectid = start;
1392 key.offset = extent_op->parent;
1393 key.type = BTRFS_EXTENT_REF_KEY;
1394 err = btrfs_set_item_key_safe(trans, extent_root, path,
1397 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1398 struct btrfs_extent_ref);
1399 btrfs_set_ref_generation(path->nodes[0], ref,
1400 extent_op->generation);
1401 btrfs_mark_buffer_dirty(path->nodes[0]);
1402 btrfs_release_path(extent_root, path);
1408 btrfs_free_path(path);
1412 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1413 struct btrfs_root *root,
1414 u64 bytenr, u64 num_bytes, int is_data)
1417 struct extent_buffer *buf;
1422 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1426 /* we can reuse a block if it hasn't been written
1427 * and it is from this transaction. We can't
1428 * reuse anything from the tree log root because
1429 * it has tiny sub-transactions.
1431 if (btrfs_buffer_uptodate(buf, 0)) {
1432 u64 header_owner = btrfs_header_owner(buf);
1433 u64 header_transid = btrfs_header_generation(buf);
1434 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1435 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
1436 header_transid == trans->transid &&
1437 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1438 clean_tree_block(NULL, root, buf);
1439 free_extent_buffer(buf);
1443 free_extent_buffer(buf);
1445 update_pinned_extents(root, bytenr, num_bytes, 1);
1452 * remove an extent from the root, returns 0 on success
1454 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1455 *root, u64 bytenr, u64 num_bytes, u64 parent,
1456 u64 root_objectid, u64 ref_generation,
1457 u64 owner_objectid, int pin, int mark_free)
1459 struct btrfs_path *path;
1460 struct btrfs_key key;
1461 struct btrfs_fs_info *info = root->fs_info;
1462 struct btrfs_extent_ops *ops = info->extent_ops;
1463 struct btrfs_root *extent_root = info->extent_root;
1464 struct extent_buffer *leaf;
1466 int extent_slot = 0;
1467 int found_extent = 0;
1469 struct btrfs_extent_item *ei;
1472 key.objectid = bytenr;
1473 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1474 key.offset = num_bytes;
1476 path = btrfs_alloc_path();
1480 ret = lookup_extent_backref(trans, extent_root, path,
1481 bytenr, parent, root_objectid,
1482 ref_generation, owner_objectid, 1);
1484 struct btrfs_key found_key;
1485 extent_slot = path->slots[0];
1486 while(extent_slot > 0) {
1488 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1490 if (found_key.objectid != bytenr)
1492 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1493 found_key.offset == num_bytes) {
1497 if (path->slots[0] - extent_slot > 5)
1500 if (!found_extent) {
1501 ret = remove_extent_backref(trans, extent_root, path);
1503 btrfs_release_path(extent_root, path);
1504 ret = btrfs_search_slot(trans, extent_root,
1507 extent_slot = path->slots[0];
1510 btrfs_print_leaf(extent_root, path->nodes[0]);
1511 printk("Unable to find ref byte nr %llu root %llu "
1512 " gen %llu owner %llu\n",
1513 (unsigned long long)bytenr,
1514 (unsigned long long)root_objectid,
1515 (unsigned long long)ref_generation,
1516 (unsigned long long)owner_objectid);
1520 leaf = path->nodes[0];
1521 ei = btrfs_item_ptr(leaf, extent_slot,
1522 struct btrfs_extent_item);
1523 refs = btrfs_extent_refs(leaf, ei);
1526 btrfs_set_extent_refs(leaf, ei, refs);
1528 btrfs_mark_buffer_dirty(leaf);
1530 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1531 struct btrfs_extent_ref *ref;
1532 ref = btrfs_item_ptr(leaf, path->slots[0],
1533 struct btrfs_extent_ref);
1534 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1535 /* if the back ref and the extent are next to each other
1536 * they get deleted below in one shot
1538 path->slots[0] = extent_slot;
1540 } else if (found_extent) {
1541 /* otherwise delete the extent back ref */
1542 ret = remove_extent_backref(trans, extent_root, path);
1544 /* if refs are 0, we need to setup the path for deletion */
1546 btrfs_release_path(extent_root, path);
1547 ret = btrfs_search_slot(trans, extent_root, &key, path,
1561 /* block accounting for super block */
1562 super_used = btrfs_super_bytes_used(&info->super_copy);
1563 btrfs_set_super_bytes_used(&info->super_copy,
1564 super_used - num_bytes);
1566 /* block accounting for root item */
1567 root_used = btrfs_root_used(&root->root_item);
1568 btrfs_set_root_used(&root->root_item,
1569 root_used - num_bytes);
1570 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1575 if (ops && ops->free_extent) {
1576 ret = ops->free_extent(root, bytenr, num_bytes);
1584 ret = pin_down_bytes(trans, root, bytenr, num_bytes, 0);
1590 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
1591 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
1595 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1599 btrfs_free_path(path);
1600 finish_current_insert(trans, extent_root);
1605 * find all the blocks marked as pending in the radix tree and remove
1606 * them from the extent map
1608 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1609 btrfs_root *extent_root)
1617 struct extent_io_tree *pending_del;
1618 struct extent_io_tree *extent_ins;
1619 struct pending_extent_op *extent_op;
1621 extent_ins = &extent_root->fs_info->extent_ins;
1622 pending_del = &extent_root->fs_info->pending_del;
1625 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1630 ret = get_state_private(pending_del, start, &priv);
1632 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1634 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1637 ret = pin_down_bytes(trans, extent_root, start,
1638 end + 1 - start, 0);
1639 mark_free = ret > 0;
1640 if (!test_range_bit(extent_ins, start, end,
1641 EXTENT_LOCKED, 0)) {
1643 ret = __free_extent(trans, extent_root,
1644 start, end + 1 - start,
1645 extent_op->orig_parent,
1646 extent_root->root_key.objectid,
1647 extent_op->orig_generation,
1648 extent_op->level, 0, mark_free);
1652 ret = get_state_private(extent_ins, start, &priv);
1654 extent_op = (struct pending_extent_op *)
1655 (unsigned long)priv;
1657 clear_extent_bits(extent_ins, start, end,
1658 EXTENT_LOCKED, GFP_NOFS);
1660 if (extent_op->type == PENDING_BACKREF_UPDATE)
1663 ret = update_block_group(trans, extent_root, start,
1664 end + 1 - start, 0, mark_free);
1675 * remove an extent from the root, returns 0 on success
1677 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1678 *root, u64 bytenr, u64 num_bytes, u64 parent,
1679 u64 root_objectid, u64 ref_generation,
1680 u64 owner_objectid, int pin)
1682 struct btrfs_root *extent_root = root->fs_info->extent_root;
1686 WARN_ON(num_bytes < root->sectorsize);
1687 if (root == extent_root) {
1688 struct pending_extent_op *extent_op;
1690 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1693 extent_op->type = PENDING_EXTENT_DELETE;
1694 extent_op->bytenr = bytenr;
1695 extent_op->num_bytes = num_bytes;
1696 extent_op->parent = parent;
1697 extent_op->orig_parent = parent;
1698 extent_op->generation = ref_generation;
1699 extent_op->orig_generation = ref_generation;
1700 extent_op->level = (int)owner_objectid;
1702 set_extent_bits(&root->fs_info->pending_del,
1703 bytenr, bytenr + num_bytes - 1,
1704 EXTENT_LOCKED, GFP_NOFS);
1705 set_state_private(&root->fs_info->pending_del,
1706 bytenr, (unsigned long)extent_op);
1709 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
1710 root_objectid, ref_generation,
1711 owner_objectid, pin, pin == 0);
1712 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1713 return ret ? ret : pending_ret;
1716 static u64 stripe_align(struct btrfs_root *root, u64 val)
1718 u64 mask = ((u64)root->stripesize - 1);
1719 u64 ret = (val + mask) & ~mask;
1724 * walks the btree of allocated extents and find a hole of a given size.
1725 * The key ins is changed to record the hole:
1726 * ins->objectid == block start
1727 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1728 * ins->offset == number of blocks
1729 * Any available blocks before search_start are skipped.
1731 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *orig_root,
1733 u64 num_bytes, u64 empty_size,
1734 u64 search_start, u64 search_end,
1735 u64 hint_byte, struct btrfs_key *ins,
1736 u64 exclude_start, u64 exclude_nr,
1740 u64 orig_search_start = search_start;
1741 struct btrfs_root * root = orig_root->fs_info->extent_root;
1742 struct btrfs_fs_info *info = root->fs_info;
1743 u64 total_needed = num_bytes;
1744 struct btrfs_block_group_cache *block_group;
1748 WARN_ON(num_bytes < root->sectorsize);
1749 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1752 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1754 hint_byte = search_start;
1755 block_group = btrfs_find_block_group(root, block_group,
1756 hint_byte, data, 1);
1758 block_group = btrfs_find_block_group(root,
1760 search_start, data, 1);
1763 total_needed += empty_size;
1767 block_group = btrfs_lookup_first_block_group(info,
1770 block_group = btrfs_lookup_first_block_group(info,
1773 ret = find_search_start(root, &block_group, &search_start,
1774 total_needed, data);
1778 search_start = stripe_align(root, search_start);
1779 ins->objectid = search_start;
1780 ins->offset = num_bytes;
1782 if (ins->objectid + num_bytes >
1783 block_group->key.objectid + block_group->key.offset) {
1784 search_start = block_group->key.objectid +
1785 block_group->key.offset;
1789 if (test_range_bit(&info->extent_ins, ins->objectid,
1790 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1791 search_start = ins->objectid + num_bytes;
1795 if (test_range_bit(&info->pinned_extents, ins->objectid,
1796 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1797 search_start = ins->objectid + num_bytes;
1801 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1802 ins->objectid < exclude_start + exclude_nr)) {
1803 search_start = exclude_start + exclude_nr;
1807 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1808 block_group = btrfs_lookup_block_group(info, ins->objectid);
1810 trans->block_group = block_group;
1812 ins->offset = num_bytes;
1816 block_group = btrfs_lookup_first_block_group(info, search_start);
1818 search_start = orig_search_start;
1825 total_needed -= empty_size;
1831 block_group = btrfs_find_block_group(root, block_group,
1832 search_start, data, 0);
1839 * finds a free extent and does all the dirty work required for allocation
1840 * returns the key for the extent through ins, and a tree buffer for
1841 * the first block of the extent through buf.
1843 * returns 0 if everything worked, non-zero otherwise.
1845 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1846 struct btrfs_root *root,
1847 u64 num_bytes, u64 parent,
1848 u64 root_objectid, u64 ref_generation,
1849 u64 owner, u64 empty_size, u64 hint_byte,
1850 u64 search_end, struct btrfs_key *ins, int data)
1854 u64 super_used, root_used;
1855 u64 search_start = 0;
1858 struct btrfs_fs_info *info = root->fs_info;
1859 struct btrfs_root *extent_root = info->extent_root;
1860 struct btrfs_path *path;
1861 struct btrfs_extent_item *extent_item;
1862 struct btrfs_extent_ref *ref;
1863 struct btrfs_key keys[2];
1865 if (info->extent_ops) {
1866 struct btrfs_extent_ops *ops = info->extent_ops;
1867 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1873 alloc_profile = info->avail_data_alloc_bits &
1874 info->data_alloc_profile;
1875 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1876 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
1877 info->system_allocs >= 0) {
1878 alloc_profile = info->avail_system_alloc_bits &
1879 info->system_alloc_profile;
1880 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1882 alloc_profile = info->avail_metadata_alloc_bits &
1883 info->metadata_alloc_profile;
1884 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1887 if (root->ref_cows) {
1888 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1889 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1891 BTRFS_BLOCK_GROUP_METADATA);
1894 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1895 num_bytes + 2 * 1024 * 1024, data);
1899 WARN_ON(num_bytes < root->sectorsize);
1900 ret = find_free_extent(trans, root, num_bytes, empty_size,
1901 search_start, search_end, hint_byte, ins,
1902 trans->alloc_exclude_start,
1903 trans->alloc_exclude_nr, data);
1910 parent = ins->objectid;
1912 /* block accounting for super block */
1913 super_used = btrfs_super_bytes_used(&info->super_copy);
1914 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1916 /* block accounting for root item */
1917 root_used = btrfs_root_used(&root->root_item);
1918 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1920 clear_extent_dirty(&root->fs_info->free_space_cache,
1921 ins->objectid, ins->objectid + ins->offset - 1,
1924 if (root == extent_root) {
1925 struct pending_extent_op *extent_op;
1927 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1930 extent_op->type = PENDING_EXTENT_INSERT;
1931 extent_op->bytenr = ins->objectid;
1932 extent_op->num_bytes = ins->offset;
1933 extent_op->parent = parent;
1934 extent_op->orig_parent = 0;
1935 extent_op->generation = ref_generation;
1936 extent_op->orig_generation = 0;
1937 extent_op->level = (int)owner;
1939 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1940 ins->objectid + ins->offset - 1,
1941 EXTENT_LOCKED, GFP_NOFS);
1942 set_state_private(&root->fs_info->extent_ins,
1943 ins->objectid, (unsigned long)extent_op);
1947 WARN_ON(trans->alloc_exclude_nr);
1948 trans->alloc_exclude_start = ins->objectid;
1949 trans->alloc_exclude_nr = ins->offset;
1951 memcpy(&keys[0], ins, sizeof(*ins));
1952 keys[1].objectid = ins->objectid;
1953 keys[1].type = BTRFS_EXTENT_REF_KEY;
1954 keys[1].offset = parent;
1955 sizes[0] = sizeof(*extent_item);
1956 sizes[1] = sizeof(*ref);
1958 path = btrfs_alloc_path();
1961 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1965 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1966 struct btrfs_extent_item);
1967 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1968 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1969 struct btrfs_extent_ref);
1971 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1972 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1973 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1974 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
1976 btrfs_mark_buffer_dirty(path->nodes[0]);
1978 trans->alloc_exclude_start = 0;
1979 trans->alloc_exclude_nr = 0;
1980 btrfs_free_path(path);
1981 finish_current_insert(trans, extent_root);
1982 pending_ret = del_pending_extents(trans, extent_root);
1992 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1994 printk("update block group failed for %llu %llu\n",
1995 (unsigned long long)ins->objectid,
1996 (unsigned long long)ins->offset);
2003 * helper function to allocate a block for a given tree
2004 * returns the tree buffer or NULL.
2006 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2007 struct btrfs_root *root,
2008 u32 blocksize, u64 parent,
2015 struct btrfs_key ins;
2017 struct extent_buffer *buf;
2019 ret = btrfs_alloc_extent(trans, root, blocksize, parent,
2020 root_objectid, ref_generation,
2021 level, empty_size, hint,
2025 return ERR_PTR(ret);
2027 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2030 parent = ins.objectid;
2031 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2032 parent, root->root_key.objectid,
2033 ref_generation, level, 0);
2035 return ERR_PTR(-ENOMEM);
2037 btrfs_set_buffer_uptodate(buf);
2038 trans->blocks_used++;
2042 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2043 struct btrfs_root *root,
2044 struct extent_buffer *leaf)
2047 u64 leaf_generation;
2048 struct btrfs_key key;
2049 struct btrfs_file_extent_item *fi;
2054 BUG_ON(!btrfs_is_leaf(leaf));
2055 nritems = btrfs_header_nritems(leaf);
2056 leaf_owner = btrfs_header_owner(leaf);
2057 leaf_generation = btrfs_header_generation(leaf);
2059 for (i = 0; i < nritems; i++) {
2062 btrfs_item_key_to_cpu(leaf, &key, i);
2063 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2065 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2066 if (btrfs_file_extent_type(leaf, fi) ==
2067 BTRFS_FILE_EXTENT_INLINE)
2070 * FIXME make sure to insert a trans record that
2071 * repeats the snapshot del on crash
2073 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2074 if (disk_bytenr == 0)
2076 ret = btrfs_free_extent(trans, root, disk_bytenr,
2077 btrfs_file_extent_disk_num_bytes(leaf, fi),
2078 leaf->start, leaf_owner, leaf_generation,
2085 static void noinline reada_walk_down(struct btrfs_root *root,
2086 struct extent_buffer *node,
2099 nritems = btrfs_header_nritems(node);
2100 level = btrfs_header_level(node);
2104 for (i = slot; i < nritems && skipped < 32; i++) {
2105 bytenr = btrfs_node_blockptr(node, i);
2106 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2107 (last > bytenr && last - bytenr > 32 * 1024))) {
2111 blocksize = btrfs_level_size(root, level - 1);
2113 ret = lookup_extent_ref(NULL, root, bytenr,
2121 mutex_unlock(&root->fs_info->fs_mutex);
2122 ret = readahead_tree_block(root, bytenr, blocksize,
2123 btrfs_node_ptr_generation(node, i));
2124 last = bytenr + blocksize;
2126 mutex_lock(&root->fs_info->fs_mutex);
2133 * helper function for drop_snapshot, this walks down the tree dropping ref
2134 * counts as it goes.
2136 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
2138 struct btrfs_path *path, int *level)
2144 struct extent_buffer *next;
2145 struct extent_buffer *cur;
2146 struct extent_buffer *parent;
2151 WARN_ON(*level < 0);
2152 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2153 ret = lookup_extent_ref(trans, root,
2154 path->nodes[*level]->start,
2155 path->nodes[*level]->len, &refs);
2161 * walk down to the last node level and free all the leaves
2163 while(*level >= 0) {
2164 WARN_ON(*level < 0);
2165 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2166 cur = path->nodes[*level];
2168 if (btrfs_header_level(cur) != *level)
2171 if (path->slots[*level] >=
2172 btrfs_header_nritems(cur))
2175 ret = drop_leaf_ref(trans, root, cur);
2179 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2180 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2181 blocksize = btrfs_level_size(root, *level - 1);
2182 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2185 parent = path->nodes[*level];
2186 root_owner = btrfs_header_owner(parent);
2187 root_gen = btrfs_header_generation(parent);
2188 path->slots[*level]++;
2189 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2190 parent->start, root_owner,
2191 root_gen, *level - 1, 1);
2195 next = btrfs_find_tree_block(root, bytenr, blocksize);
2196 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2197 free_extent_buffer(next);
2198 reada_walk_down(root, cur, path->slots[*level]);
2199 mutex_unlock(&root->fs_info->fs_mutex);
2200 next = read_tree_block(root, bytenr, blocksize,
2202 mutex_lock(&root->fs_info->fs_mutex);
2204 WARN_ON(*level <= 0);
2205 if (path->nodes[*level-1])
2206 free_extent_buffer(path->nodes[*level-1]);
2207 path->nodes[*level-1] = next;
2208 *level = btrfs_header_level(next);
2209 path->slots[*level] = 0;
2212 WARN_ON(*level < 0);
2213 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2215 if (path->nodes[*level] == root->node) {
2216 root_owner = root->root_key.objectid;
2217 parent = path->nodes[*level];
2219 parent = path->nodes[*level + 1];
2220 root_owner = btrfs_header_owner(parent);
2223 root_gen = btrfs_header_generation(parent);
2224 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2225 path->nodes[*level]->len, parent->start,
2226 root_owner, root_gen, *level, 1);
2227 free_extent_buffer(path->nodes[*level]);
2228 path->nodes[*level] = NULL;
2235 * helper for dropping snapshots. This walks back up the tree in the path
2236 * to find the first node higher up where we haven't yet gone through
2239 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2240 struct btrfs_root *root,
2241 struct btrfs_path *path, int *level)
2245 struct btrfs_root_item *root_item = &root->root_item;
2250 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2251 slot = path->slots[i];
2252 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2253 struct extent_buffer *node;
2254 struct btrfs_disk_key disk_key;
2255 node = path->nodes[i];
2258 WARN_ON(*level == 0);
2259 btrfs_node_key(node, &disk_key, path->slots[i]);
2260 memcpy(&root_item->drop_progress,
2261 &disk_key, sizeof(disk_key));
2262 root_item->drop_level = i;
2265 struct extent_buffer *parent;
2266 if (path->nodes[*level] == root->node)
2267 parent = path->nodes[*level];
2269 parent = path->nodes[*level + 1];
2271 root_owner = btrfs_header_owner(parent);
2272 root_gen = btrfs_header_generation(parent);
2273 ret = btrfs_free_extent(trans, root,
2274 path->nodes[*level]->start,
2275 path->nodes[*level]->len,
2276 parent->start, root_owner,
2277 root_gen, *level, 1);
2279 free_extent_buffer(path->nodes[*level]);
2280 path->nodes[*level] = NULL;
2288 * drop the reference count on the tree rooted at 'snap'. This traverses
2289 * the tree freeing any blocks that have a ref count of zero after being
2292 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2298 struct btrfs_path *path;
2301 struct btrfs_root_item *root_item = &root->root_item;
2303 path = btrfs_alloc_path();
2306 level = btrfs_header_level(root->node);
2308 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2309 path->nodes[level] = root->node;
2310 extent_buffer_get(root->node);
2311 path->slots[level] = 0;
2313 struct btrfs_key key;
2314 struct btrfs_disk_key found_key;
2315 struct extent_buffer *node;
2317 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2318 level = root_item->drop_level;
2319 path->lowest_level = level;
2320 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2325 node = path->nodes[level];
2326 btrfs_node_key(node, &found_key, path->slots[level]);
2327 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2328 sizeof(found_key)));
2331 wret = walk_down_tree(trans, root, path, &level);
2337 wret = walk_up_tree(trans, root, path, &level);
2347 for (i = 0; i <= orig_level; i++) {
2348 if (path->nodes[i]) {
2349 free_extent_buffer(path->nodes[i]);
2350 path->nodes[i] = NULL;
2354 btrfs_free_path(path);
2358 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2365 ret = find_first_extent_bit(&info->block_group_cache, 0,
2366 &start, &end, (unsigned int)-1);
2369 ret = get_state_private(&info->block_group_cache, start, &ptr);
2371 kfree((void *)(unsigned long)ptr);
2372 clear_extent_bits(&info->block_group_cache, start,
2373 end, (unsigned int)-1, GFP_NOFS);
2376 ret = find_first_extent_bit(&info->free_space_cache, 0,
2377 &start, &end, EXTENT_DIRTY);
2380 clear_extent_dirty(&info->free_space_cache, start,
2386 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2387 struct btrfs_key *key)
2390 struct btrfs_key found_key;
2391 struct extent_buffer *leaf;
2394 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2398 slot = path->slots[0];
2399 leaf = path->nodes[0];
2400 if (slot >= btrfs_header_nritems(leaf)) {
2401 ret = btrfs_next_leaf(root, path);
2408 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2410 if (found_key.objectid >= key->objectid &&
2411 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2420 int btrfs_read_block_groups(struct btrfs_root *root)
2422 struct btrfs_path *path;
2425 struct btrfs_block_group_cache *cache;
2426 struct btrfs_fs_info *info = root->fs_info;
2427 struct btrfs_space_info *space_info;
2428 struct extent_io_tree *block_group_cache;
2429 struct btrfs_key key;
2430 struct btrfs_key found_key;
2431 struct extent_buffer *leaf;
2433 block_group_cache = &info->block_group_cache;
2435 root = info->extent_root;
2438 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2439 path = btrfs_alloc_path();
2444 ret = find_first_block_group(root, path, &key);
2452 leaf = path->nodes[0];
2453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2454 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2460 read_extent_buffer(leaf, &cache->item,
2461 btrfs_item_ptr_offset(leaf, path->slots[0]),
2462 sizeof(cache->item));
2463 memcpy(&cache->key, &found_key, sizeof(found_key));
2466 key.objectid = found_key.objectid + found_key.offset;
2467 btrfs_release_path(root, path);
2468 cache->flags = btrfs_block_group_flags(&cache->item);
2470 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
2471 bit = BLOCK_GROUP_DATA;
2472 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2473 bit = BLOCK_GROUP_SYSTEM;
2474 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
2475 bit = BLOCK_GROUP_METADATA;
2477 set_avail_alloc_bits(info, cache->flags);
2478 if (btrfs_chunk_readonly(root, cache->key.objectid))
2481 ret = update_space_info(info, cache->flags, found_key.offset,
2482 btrfs_block_group_used(&cache->item),
2485 cache->space_info = space_info;
2487 /* use EXTENT_LOCKED to prevent merging */
2488 set_extent_bits(block_group_cache, found_key.objectid,
2489 found_key.objectid + found_key.offset - 1,
2490 bit | EXTENT_LOCKED, GFP_NOFS);
2491 set_state_private(block_group_cache, found_key.objectid,
2492 (unsigned long)cache);
2496 btrfs_free_path(path);
2500 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2501 struct btrfs_root *root, u64 bytes_used,
2502 u64 type, u64 chunk_objectid, u64 chunk_offset,
2507 struct btrfs_root *extent_root;
2508 struct btrfs_block_group_cache *cache;
2509 struct extent_io_tree *block_group_cache;
2511 extent_root = root->fs_info->extent_root;
2512 block_group_cache = &root->fs_info->block_group_cache;
2514 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2516 cache->key.objectid = chunk_offset;
2517 cache->key.offset = size;
2519 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2520 btrfs_set_block_group_used(&cache->item, bytes_used);
2521 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2522 cache->flags = type;
2523 btrfs_set_block_group_flags(&cache->item, type);
2525 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
2526 &cache->space_info);
2529 bit = block_group_state_bits(type);
2530 set_extent_bits(block_group_cache, chunk_offset,
2531 chunk_offset + size - 1,
2532 bit | EXTENT_LOCKED, GFP_NOFS);
2534 set_state_private(block_group_cache, chunk_offset,
2535 (unsigned long)cache);
2536 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2537 sizeof(cache->item));
2540 finish_current_insert(trans, extent_root);
2541 ret = del_pending_extents(trans, extent_root);
2543 set_avail_alloc_bits(extent_root->fs_info, type);
2548 * This is for converter use only.
2550 * In that case, we don't know where are free blocks located.
2551 * Therefore all block group cache entries must be setup properly
2552 * before doing any block allocation.
2554 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2555 struct btrfs_root *root)
2563 u64 total_metadata = 0;
2567 struct btrfs_root *extent_root;
2568 struct btrfs_block_group_cache *cache;
2569 struct extent_io_tree *block_group_cache;
2571 extent_root = root->fs_info->extent_root;
2572 block_group_cache = &root->fs_info->block_group_cache;
2573 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2574 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2575 group_align = 64 * root->sectorsize;
2578 while (cur_start < total_bytes) {
2579 group_size = total_bytes / 12;
2580 group_size = min_t(u64, group_size, total_bytes - cur_start);
2581 if (cur_start == 0) {
2582 bit = BLOCK_GROUP_SYSTEM;
2583 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
2585 group_size &= ~(group_align - 1);
2586 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
2587 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
2589 group_size &= ~(group_align - 1);
2590 if (total_data >= total_metadata * 2) {
2591 group_type = BTRFS_BLOCK_GROUP_METADATA;
2592 group_size = min_t(u64, group_size,
2593 1ULL * 1024 * 1024 * 1024);
2594 total_metadata += group_size;
2596 group_type = BTRFS_BLOCK_GROUP_DATA;
2597 group_size = min_t(u64, group_size,
2598 5ULL * 1024 * 1024 * 1024);
2599 total_data += group_size;
2601 if ((total_bytes - cur_start) * 4 < group_size * 5)
2602 group_size = total_bytes - cur_start;
2605 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2608 cache->key.objectid = cur_start;
2609 cache->key.offset = group_size;
2610 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2612 btrfs_set_block_group_used(&cache->item, 0);
2613 btrfs_set_block_group_chunk_objectid(&cache->item,
2615 btrfs_set_block_group_flags(&cache->item, group_type);
2617 cache->flags = group_type;
2619 ret = update_space_info(root->fs_info, group_type, group_size,
2620 0, &cache->space_info);
2622 set_avail_alloc_bits(extent_root->fs_info, group_type);
2624 set_extent_bits(block_group_cache, cur_start,
2625 cur_start + group_size - 1,
2626 bit | EXTENT_LOCKED, GFP_NOFS);
2627 set_state_private(block_group_cache, cur_start,
2628 (unsigned long)cache);
2629 cur_start += group_size;
2631 /* then insert all the items */
2633 while(cur_start < total_bytes) {
2634 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2637 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2638 sizeof(cache->item));
2641 finish_current_insert(trans, extent_root);
2642 ret = del_pending_extents(trans, extent_root);
2645 cur_start = cache->key.objectid + cache->key.offset;
2650 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2651 struct btrfs_root *root,
2652 u64 bytenr, u64 num_bytes, int alloc,
2655 return update_block_group(trans, root, bytenr, num_bytes,