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.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "ref-cache.h"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op {
47 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
49 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
51 static struct btrfs_block_group_cache *
52 __btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
56 void maybe_lock_mutex(struct btrfs_root *root)
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_lock(&root->fs_info->alloc_mutex);
65 void maybe_unlock_mutex(struct btrfs_root *root)
67 if (root != root->fs_info->extent_root &&
68 root != root->fs_info->chunk_root &&
69 root != root->fs_info->dev_root) {
70 mutex_unlock(&root->fs_info->alloc_mutex);
74 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76 return (cache->flags & bits) == bits;
80 * this adds the block group to the fs_info rb tree for the block group
83 int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
97 if (block_group->key.objectid < cache->key.objectid) {
99 } else if (block_group->key.objectid > cache->key.objectid) {
102 spin_unlock(&info->block_group_cache_lock);
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
119 static struct btrfs_block_group_cache *
120 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
123 struct btrfs_block_group_cache *cache, *ret = NULL;
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
131 cache = rb_entry(n, struct btrfs_block_group_cache,
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
151 spin_unlock(&info->block_group_cache_lock);
157 * this is only called by cache_block_group, since we could have freed extents
158 * we need to check the pinned_extents for any extents that can't be used yet
159 * since their free space will be released as soon as the transaction commits.
161 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
162 struct btrfs_fs_info *info, u64 start, u64 end)
164 u64 extent_start, extent_end, size;
167 while (start < end) {
168 ret = find_first_extent_bit(&info->pinned_extents, start,
169 &extent_start, &extent_end,
174 if (extent_start == start) {
175 start = extent_end + 1;
176 } else if (extent_start > start && extent_start < end) {
177 size = extent_start - start;
178 ret = btrfs_add_free_space(block_group, start, size);
180 start = extent_end + 1;
188 ret = btrfs_add_free_space(block_group, start, size);
195 static int cache_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache *block_group)
198 struct btrfs_path *path;
200 struct btrfs_key key;
201 struct extent_buffer *leaf;
210 root = root->fs_info->extent_root;
212 if (block_group->cached)
215 path = btrfs_alloc_path();
221 * we get into deadlocks with paths held by callers of this function.
222 * since the alloc_mutex is protecting things right now, just
223 * skip the locking here
225 path->skip_locking = 1;
226 first_free = max_t(u64, block_group->key.objectid,
227 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
228 key.objectid = block_group->key.objectid;
230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
234 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
238 leaf = path->nodes[0];
239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
240 if (key.objectid + key.offset > first_free)
241 first_free = key.objectid + key.offset;
244 leaf = path->nodes[0];
245 slot = path->slots[0];
246 if (slot >= btrfs_header_nritems(leaf)) {
247 ret = btrfs_next_leaf(root, path);
255 btrfs_item_key_to_cpu(leaf, &key, slot);
256 if (key.objectid < block_group->key.objectid)
259 if (key.objectid >= block_group->key.objectid +
260 block_group->key.offset)
263 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
269 add_new_free_space(block_group, root->fs_info, last,
272 last = key.objectid + key.offset;
281 add_new_free_space(block_group, root->fs_info, last,
282 block_group->key.objectid +
283 block_group->key.offset);
285 block_group->cached = 1;
288 btrfs_free_path(path);
293 * return the block group that starts at or after bytenr
295 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
299 struct btrfs_block_group_cache *cache;
301 cache = block_group_cache_tree_search(info, bytenr, 0);
307 * return the block group that contains teh given bytenr
309 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
313 struct btrfs_block_group_cache *cache;
315 cache = block_group_cache_tree_search(info, bytenr, 1);
320 static int noinline find_free_space(struct btrfs_root *root,
321 struct btrfs_block_group_cache **cache_ret,
322 u64 *start_ret, u64 num, int data)
325 struct btrfs_block_group_cache *cache = *cache_ret;
326 struct btrfs_free_space *info = NULL;
329 u64 search_start = *start_ret;
331 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
332 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
337 last = max(search_start, cache->key.objectid);
340 ret = cache_block_group(root, cache);
344 if (cache->ro || !block_group_bits(cache, data))
347 info = btrfs_find_free_space(cache, last, num);
349 *start_ret = info->offset;
354 last = cache->key.objectid + cache->key.offset;
356 cache = btrfs_lookup_first_block_group(root->fs_info, last);
357 if (!cache || cache->key.objectid >= total_fs_bytes)
367 static u64 div_factor(u64 num, int factor)
376 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
379 struct list_head *head = &info->space_info;
380 struct list_head *cur;
381 struct btrfs_space_info *found;
382 list_for_each(cur, head) {
383 found = list_entry(cur, struct btrfs_space_info, list);
384 if (found->flags == flags)
391 static struct btrfs_block_group_cache *
392 __btrfs_find_block_group(struct btrfs_root *root,
393 struct btrfs_block_group_cache *hint,
394 u64 search_start, int data, int owner)
396 struct btrfs_block_group_cache *cache;
397 struct btrfs_block_group_cache *found_group = NULL;
398 struct btrfs_fs_info *info = root->fs_info;
399 struct btrfs_space_info *sinfo;
407 if (data & BTRFS_BLOCK_GROUP_METADATA)
411 struct btrfs_block_group_cache *shint;
412 shint = btrfs_lookup_first_block_group(info, search_start);
413 if (shint && block_group_bits(shint, data) && !shint->ro) {
414 spin_lock(&shint->lock);
415 used = btrfs_block_group_used(&shint->item);
416 if (used + shint->pinned <
417 div_factor(shint->key.offset, factor)) {
418 spin_unlock(&shint->lock);
421 spin_unlock(&shint->lock);
424 if (hint && !hint->ro && block_group_bits(hint, data)) {
425 spin_lock(&hint->lock);
426 used = btrfs_block_group_used(&hint->item);
427 if (used + hint->pinned <
428 div_factor(hint->key.offset, factor)) {
429 spin_unlock(&hint->lock);
432 spin_unlock(&hint->lock);
433 last = hint->key.objectid + hint->key.offset;
436 last = max(hint->key.objectid, search_start);
440 sinfo = __find_space_info(root->fs_info, data);
449 spin_lock(&sinfo->lock);
450 list_for_each(l, &sinfo->block_groups) {
451 struct btrfs_block_group_cache *entry;
452 entry = list_entry(l, struct btrfs_block_group_cache,
454 if ((entry->key.objectid >= last) &&
455 (!cache || (entry->key.objectid <
456 cache->key.objectid)))
459 spin_unlock(&sinfo->lock);
464 spin_lock(&cache->lock);
465 last = cache->key.objectid + cache->key.offset;
466 used = btrfs_block_group_used(&cache->item);
468 if (!cache->ro && block_group_bits(cache, data)) {
469 free_check = div_factor(cache->key.offset, factor);
470 if (used + cache->pinned < free_check) {
472 spin_unlock(&cache->lock);
476 spin_unlock(&cache->lock);
484 if (!full_search && factor < 10) {
494 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
495 struct btrfs_block_group_cache
496 *hint, u64 search_start,
500 struct btrfs_block_group_cache *ret;
501 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
505 /* simple helper to search for an existing extent at a given offset */
506 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
509 struct btrfs_key key;
510 struct btrfs_path *path;
512 path = btrfs_alloc_path();
514 maybe_lock_mutex(root);
515 key.objectid = start;
517 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
518 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
520 maybe_unlock_mutex(root);
521 btrfs_free_path(path);
526 * Back reference rules. Back refs have three main goals:
528 * 1) differentiate between all holders of references to an extent so that
529 * when a reference is dropped we can make sure it was a valid reference
530 * before freeing the extent.
532 * 2) Provide enough information to quickly find the holders of an extent
533 * if we notice a given block is corrupted or bad.
535 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
536 * maintenance. This is actually the same as #2, but with a slightly
537 * different use case.
539 * File extents can be referenced by:
541 * - multiple snapshots, subvolumes, or different generations in one subvol
542 * - different files inside a single subvolume
543 * - different offsets inside a file (bookend extents in file.c)
545 * The extent ref structure has fields for:
547 * - Objectid of the subvolume root
548 * - Generation number of the tree holding the reference
549 * - objectid of the file holding the reference
550 * - offset in the file corresponding to the key holding the reference
551 * - number of references holding by parent node (alway 1 for tree blocks)
553 * Btree leaf may hold multiple references to a file extent. In most cases,
554 * these references are from same file and the corresponding offsets inside
555 * the file are close together. So inode objectid and offset in file are
556 * just hints, they provide hints about where in the btree the references
557 * can be found and when we can stop searching.
559 * When a file extent is allocated the fields are filled in:
560 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
562 * When a leaf is cow'd new references are added for every file extent found
563 * in the leaf. It looks similar to the create case, but trans->transid will
564 * be different when the block is cow'd.
566 * (root_key.objectid, trans->transid, inode objectid, offset in file,
567 * number of references in the leaf)
569 * Because inode objectid and offset in file are just hints, they are not
570 * used when backrefs are deleted. When a file extent is removed either
571 * during snapshot deletion or file truncation, we find the corresponding
572 * back back reference and check the following fields.
574 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
576 * Btree extents can be referenced by:
578 * - Different subvolumes
579 * - Different generations of the same subvolume
581 * When a tree block is created, back references are inserted:
583 * (root->root_key.objectid, trans->transid, level, 0, 1)
585 * When a tree block is cow'd, new back references are added for all the
586 * blocks it points to. If the tree block isn't in reference counted root,
587 * the old back references are removed. These new back references are of
588 * the form (trans->transid will have increased since creation):
590 * (root->root_key.objectid, trans->transid, level, 0, 1)
592 * When a backref is in deleting, the following fields are checked:
594 * if backref was for a tree root:
595 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
597 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
599 * Back Reference Key composing:
601 * The key objectid corresponds to the first byte in the extent, the key
602 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
603 * byte of parent extent. If a extent is tree root, the key offset is set
604 * to the key objectid.
607 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
608 struct btrfs_root *root,
609 struct btrfs_path *path, u64 bytenr,
610 u64 parent, u64 ref_root,
611 u64 ref_generation, int del)
613 struct btrfs_key key;
614 struct btrfs_extent_ref *ref;
615 struct extent_buffer *leaf;
618 key.objectid = bytenr;
619 key.type = BTRFS_EXTENT_REF_KEY;
622 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 if (btrfs_ref_root(leaf, ref) != ref_root ||
633 btrfs_ref_generation(leaf, ref) != ref_generation) {
643 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root,
645 struct btrfs_path *path,
646 u64 bytenr, u64 parent,
647 u64 ref_root, u64 ref_generation,
648 u64 owner_objectid, u64 owner_offset)
650 struct btrfs_key key;
651 struct extent_buffer *leaf;
652 struct btrfs_extent_ref *ref;
656 key.objectid = bytenr;
657 key.type = BTRFS_EXTENT_REF_KEY;
660 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
662 leaf = path->nodes[0];
663 ref = btrfs_item_ptr(leaf, path->slots[0],
664 struct btrfs_extent_ref);
665 btrfs_set_ref_root(leaf, ref, ref_root);
666 btrfs_set_ref_generation(leaf, ref, ref_generation);
667 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 btrfs_set_ref_num_refs(leaf, ref, 1);
670 } else if (ret == -EEXIST) {
672 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
673 leaf = path->nodes[0];
674 ref = btrfs_item_ptr(leaf, path->slots[0],
675 struct btrfs_extent_ref);
676 if (btrfs_ref_root(leaf, ref) != ref_root ||
677 btrfs_ref_generation(leaf, ref) != ref_generation) {
683 num_refs = btrfs_ref_num_refs(leaf, ref);
684 BUG_ON(num_refs == 0);
685 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
687 existing_owner = btrfs_ref_objectid(leaf, ref);
688 if (existing_owner == owner_objectid &&
689 btrfs_ref_offset(leaf, ref) > owner_offset) {
690 btrfs_set_ref_offset(leaf, ref, owner_offset);
691 } else if (existing_owner != owner_objectid &&
692 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
693 btrfs_set_ref_objectid(leaf, ref,
694 BTRFS_MULTIPLE_OBJECTIDS);
695 btrfs_set_ref_offset(leaf, ref, 0);
701 btrfs_mark_buffer_dirty(path->nodes[0]);
703 btrfs_release_path(root, path);
707 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root,
709 struct btrfs_path *path)
711 struct extent_buffer *leaf;
712 struct btrfs_extent_ref *ref;
716 leaf = path->nodes[0];
717 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
718 num_refs = btrfs_ref_num_refs(leaf, ref);
719 BUG_ON(num_refs == 0);
722 ret = btrfs_del_item(trans, root, path);
724 btrfs_set_ref_num_refs(leaf, ref, num_refs);
725 btrfs_mark_buffer_dirty(leaf);
727 btrfs_release_path(root, path);
731 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root, u64 bytenr,
733 u64 orig_parent, u64 parent,
734 u64 orig_root, u64 ref_root,
735 u64 orig_generation, u64 ref_generation,
736 u64 owner_objectid, u64 owner_offset)
739 struct btrfs_root *extent_root = root->fs_info->extent_root;
740 struct btrfs_path *path;
742 if (root == root->fs_info->extent_root) {
743 struct pending_extent_op *extent_op;
746 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
747 num_bytes = btrfs_level_size(root, (int)owner_objectid);
748 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
749 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
751 ret = get_state_private(&root->fs_info->extent_ins,
754 extent_op = (struct pending_extent_op *)
756 BUG_ON(extent_op->parent != orig_parent);
757 BUG_ON(extent_op->generation != orig_generation);
758 extent_op->parent = parent;
759 extent_op->generation = ref_generation;
761 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
764 extent_op->type = PENDING_BACKREF_UPDATE;
765 extent_op->bytenr = bytenr;
766 extent_op->num_bytes = num_bytes;
767 extent_op->parent = parent;
768 extent_op->orig_parent = orig_parent;
769 extent_op->generation = ref_generation;
770 extent_op->orig_generation = orig_generation;
771 extent_op->level = (int)owner_objectid;
773 set_extent_bits(&root->fs_info->extent_ins,
774 bytenr, bytenr + num_bytes - 1,
775 EXTENT_LOCKED, GFP_NOFS);
776 set_state_private(&root->fs_info->extent_ins,
777 bytenr, (unsigned long)extent_op);
782 path = btrfs_alloc_path();
785 ret = lookup_extent_backref(trans, extent_root, path,
786 bytenr, orig_parent, orig_root,
790 ret = remove_extent_backref(trans, extent_root, path);
793 ret = insert_extent_backref(trans, extent_root, path, bytenr,
794 parent, ref_root, ref_generation,
795 owner_objectid, owner_offset);
797 finish_current_insert(trans, extent_root);
798 del_pending_extents(trans, extent_root);
800 btrfs_free_path(path);
804 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
805 struct btrfs_root *root, u64 bytenr,
806 u64 orig_parent, u64 parent,
807 u64 ref_root, u64 ref_generation,
808 u64 owner_objectid, u64 owner_offset)
811 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
812 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
814 maybe_lock_mutex(root);
815 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
816 parent, ref_root, ref_root,
817 ref_generation, ref_generation,
818 owner_objectid, owner_offset);
819 maybe_unlock_mutex(root);
823 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
824 struct btrfs_root *root, u64 bytenr,
825 u64 orig_parent, u64 parent,
826 u64 orig_root, u64 ref_root,
827 u64 orig_generation, u64 ref_generation,
828 u64 owner_objectid, u64 owner_offset)
830 struct btrfs_path *path;
832 struct btrfs_key key;
833 struct extent_buffer *l;
834 struct btrfs_extent_item *item;
837 path = btrfs_alloc_path();
842 key.objectid = bytenr;
843 key.type = BTRFS_EXTENT_ITEM_KEY;
844 key.offset = (u64)-1;
846 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
850 BUG_ON(ret == 0 || path->slots[0] == 0);
855 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
856 BUG_ON(key.objectid != bytenr);
857 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
859 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
860 refs = btrfs_extent_refs(l, item);
861 btrfs_set_extent_refs(l, item, refs + 1);
862 btrfs_mark_buffer_dirty(path->nodes[0]);
864 btrfs_release_path(root->fs_info->extent_root, path);
867 ret = insert_extent_backref(trans, root->fs_info->extent_root,
868 path, bytenr, parent,
869 ref_root, ref_generation,
870 owner_objectid, owner_offset);
872 finish_current_insert(trans, root->fs_info->extent_root);
873 del_pending_extents(trans, root->fs_info->extent_root);
875 btrfs_free_path(path);
879 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
880 struct btrfs_root *root,
881 u64 bytenr, u64 num_bytes, u64 parent,
882 u64 ref_root, u64 ref_generation,
883 u64 owner_objectid, u64 owner_offset)
886 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
887 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
889 maybe_lock_mutex(root);
890 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
891 0, ref_root, 0, ref_generation,
892 owner_objectid, owner_offset);
893 maybe_unlock_mutex(root);
897 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
898 struct btrfs_root *root)
900 finish_current_insert(trans, root->fs_info->extent_root);
901 del_pending_extents(trans, root->fs_info->extent_root);
905 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root, u64 bytenr,
907 u64 num_bytes, u32 *refs)
909 struct btrfs_path *path;
911 struct btrfs_key key;
912 struct extent_buffer *l;
913 struct btrfs_extent_item *item;
915 WARN_ON(num_bytes < root->sectorsize);
916 path = btrfs_alloc_path();
918 key.objectid = bytenr;
919 key.offset = num_bytes;
920 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
921 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
926 btrfs_print_leaf(root, path->nodes[0]);
927 printk("failed to find block number %Lu\n", bytenr);
931 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
932 *refs = btrfs_extent_refs(l, item);
934 btrfs_free_path(path);
938 static int get_reference_status(struct btrfs_root *root, u64 bytenr,
939 u64 parent_gen, u64 ref_objectid,
940 u64 *min_generation, u32 *ref_count)
942 struct btrfs_root *extent_root = root->fs_info->extent_root;
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_extent_ref *ref_item;
946 struct btrfs_key key;
947 struct btrfs_key found_key;
948 u64 root_objectid = root->root_key.objectid;
953 key.objectid = bytenr;
954 key.offset = (u64)-1;
955 key.type = BTRFS_EXTENT_ITEM_KEY;
957 path = btrfs_alloc_path();
958 mutex_lock(&root->fs_info->alloc_mutex);
959 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
963 if (ret < 0 || path->slots[0] == 0)
967 leaf = path->nodes[0];
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
970 if (found_key.objectid != bytenr ||
971 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
977 *min_generation = (u64)-1;
980 leaf = path->nodes[0];
981 nritems = btrfs_header_nritems(leaf);
982 if (path->slots[0] >= nritems) {
983 ret = btrfs_next_leaf(extent_root, path);
990 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
991 if (found_key.objectid != bytenr)
994 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
999 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_extent_ref);
1001 ref_generation = btrfs_ref_generation(leaf, ref_item);
1003 * For (parent_gen > 0 && parent_gen > ref_generation):
1005 * we reach here through the oldest root, therefore
1006 * all other reference from same snapshot should have
1007 * a larger generation.
1009 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
1010 (parent_gen > 0 && parent_gen > ref_generation) ||
1011 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1012 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
1018 if (*min_generation > ref_generation)
1019 *min_generation = ref_generation;
1025 mutex_unlock(&root->fs_info->alloc_mutex);
1026 btrfs_free_path(path);
1030 int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1031 struct btrfs_root *root,
1032 struct btrfs_key *key, u64 bytenr)
1034 struct btrfs_root *old_root;
1035 struct btrfs_path *path = NULL;
1036 struct extent_buffer *eb;
1037 struct btrfs_file_extent_item *item;
1045 BUG_ON(trans == NULL);
1046 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1047 ret = get_reference_status(root, bytenr, 0, key->objectid,
1048 &min_generation, &ref_count);
1055 old_root = root->dirty_root->root;
1056 ref_generation = old_root->root_key.offset;
1058 /* all references are created in running transaction */
1059 if (min_generation > ref_generation) {
1064 path = btrfs_alloc_path();
1070 path->skip_locking = 1;
1071 /* if no item found, the extent is referenced by other snapshot */
1072 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1076 eb = path->nodes[0];
1077 item = btrfs_item_ptr(eb, path->slots[0],
1078 struct btrfs_file_extent_item);
1079 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1080 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1085 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1087 eb = path->nodes[level];
1090 extent_start = eb->start;
1092 extent_start = bytenr;
1094 ret = get_reference_status(root, extent_start, ref_generation,
1095 0, &min_generation, &ref_count);
1099 if (ref_count != 1) {
1104 ref_generation = btrfs_header_generation(eb);
1109 btrfs_free_path(path);
1113 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1114 struct extent_buffer *buf, u32 nr_extents)
1117 struct btrfs_key key;
1118 struct btrfs_file_extent_item *fi;
1123 if (!root->ref_cows)
1126 level = btrfs_header_level(buf);
1127 nritems = btrfs_header_nritems(buf);
1130 struct btrfs_leaf_ref *ref;
1131 struct btrfs_extent_info *info;
1133 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1139 ref->root_gen = root->root_key.offset;
1140 ref->bytenr = buf->start;
1141 ref->owner = btrfs_header_owner(buf);
1142 ref->generation = btrfs_header_generation(buf);
1143 ref->nritems = nr_extents;
1144 info = ref->extents;
1146 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1148 btrfs_item_key_to_cpu(buf, &key, i);
1149 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1151 fi = btrfs_item_ptr(buf, i,
1152 struct btrfs_file_extent_item);
1153 if (btrfs_file_extent_type(buf, fi) ==
1154 BTRFS_FILE_EXTENT_INLINE)
1156 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1157 if (disk_bytenr == 0)
1160 info->bytenr = disk_bytenr;
1162 btrfs_file_extent_disk_num_bytes(buf, fi);
1163 info->objectid = key.objectid;
1164 info->offset = key.offset;
1168 BUG_ON(!root->ref_tree);
1169 ret = btrfs_add_leaf_ref(root, ref);
1171 btrfs_free_leaf_ref(root, ref);
1177 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1178 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1185 u64 orig_generation;
1187 u32 nr_file_extents = 0;
1188 struct btrfs_key key;
1189 struct btrfs_file_extent_item *fi;
1194 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1195 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1197 ref_root = btrfs_header_owner(buf);
1198 ref_generation = btrfs_header_generation(buf);
1199 orig_root = btrfs_header_owner(orig_buf);
1200 orig_generation = btrfs_header_generation(orig_buf);
1202 nritems = btrfs_header_nritems(buf);
1203 level = btrfs_header_level(buf);
1205 if (root->ref_cows) {
1206 process_func = __btrfs_inc_extent_ref;
1209 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1212 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1214 process_func = __btrfs_update_extent_ref;
1217 for (i = 0; i < nritems; i++) {
1220 btrfs_item_key_to_cpu(buf, &key, i);
1221 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1223 fi = btrfs_item_ptr(buf, i,
1224 struct btrfs_file_extent_item);
1225 if (btrfs_file_extent_type(buf, fi) ==
1226 BTRFS_FILE_EXTENT_INLINE)
1228 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1234 maybe_lock_mutex(root);
1235 ret = process_func(trans, root, bytenr,
1236 orig_buf->start, buf->start,
1237 orig_root, ref_root,
1238 orig_generation, ref_generation,
1239 key.objectid, key.offset);
1240 maybe_unlock_mutex(root);
1248 bytenr = btrfs_node_blockptr(buf, i);
1249 maybe_lock_mutex(root);
1250 ret = process_func(trans, root, bytenr,
1251 orig_buf->start, buf->start,
1252 orig_root, ref_root,
1253 orig_generation, ref_generation,
1255 maybe_unlock_mutex(root);
1266 *nr_extents = nr_file_extents;
1268 *nr_extents = nritems;
1276 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root, struct extent_buffer *orig_buf,
1278 struct extent_buffer *buf, int start_slot, int nr)
1285 u64 orig_generation;
1286 struct btrfs_key key;
1287 struct btrfs_file_extent_item *fi;
1293 BUG_ON(start_slot < 0);
1294 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1296 ref_root = btrfs_header_owner(buf);
1297 ref_generation = btrfs_header_generation(buf);
1298 orig_root = btrfs_header_owner(orig_buf);
1299 orig_generation = btrfs_header_generation(orig_buf);
1300 level = btrfs_header_level(buf);
1302 if (!root->ref_cows) {
1304 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1307 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1311 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1314 btrfs_item_key_to_cpu(buf, &key, slot);
1315 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1317 fi = btrfs_item_ptr(buf, slot,
1318 struct btrfs_file_extent_item);
1319 if (btrfs_file_extent_type(buf, fi) ==
1320 BTRFS_FILE_EXTENT_INLINE)
1322 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1325 maybe_lock_mutex(root);
1326 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1327 orig_buf->start, buf->start,
1328 orig_root, ref_root,
1329 orig_generation, ref_generation,
1330 key.objectid, key.offset);
1331 maybe_unlock_mutex(root);
1335 bytenr = btrfs_node_blockptr(buf, slot);
1336 maybe_lock_mutex(root);
1337 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1338 orig_buf->start, buf->start,
1339 orig_root, ref_root,
1340 orig_generation, ref_generation,
1342 maybe_unlock_mutex(root);
1353 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1354 struct btrfs_root *root,
1355 struct btrfs_path *path,
1356 struct btrfs_block_group_cache *cache)
1360 struct btrfs_root *extent_root = root->fs_info->extent_root;
1362 struct extent_buffer *leaf;
1364 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1369 leaf = path->nodes[0];
1370 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1371 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1372 btrfs_mark_buffer_dirty(leaf);
1373 btrfs_release_path(extent_root, path);
1375 finish_current_insert(trans, extent_root);
1376 pending_ret = del_pending_extents(trans, extent_root);
1385 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root)
1388 struct btrfs_block_group_cache *cache, *entry;
1392 struct btrfs_path *path;
1395 path = btrfs_alloc_path();
1399 mutex_lock(&root->fs_info->alloc_mutex);
1402 spin_lock(&root->fs_info->block_group_cache_lock);
1403 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1404 n; n = rb_next(n)) {
1405 entry = rb_entry(n, struct btrfs_block_group_cache,
1412 spin_unlock(&root->fs_info->block_group_cache_lock);
1417 last += cache->key.offset;
1419 err = write_one_cache_group(trans, root,
1422 * if we fail to write the cache group, we want
1423 * to keep it marked dirty in hopes that a later
1433 btrfs_free_path(path);
1434 mutex_unlock(&root->fs_info->alloc_mutex);
1438 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1439 u64 total_bytes, u64 bytes_used,
1440 struct btrfs_space_info **space_info)
1442 struct btrfs_space_info *found;
1444 found = __find_space_info(info, flags);
1446 found->total_bytes += total_bytes;
1447 found->bytes_used += bytes_used;
1449 *space_info = found;
1452 found = kmalloc(sizeof(*found), GFP_NOFS);
1456 list_add(&found->list, &info->space_info);
1457 INIT_LIST_HEAD(&found->block_groups);
1458 spin_lock_init(&found->lock);
1459 found->flags = flags;
1460 found->total_bytes = total_bytes;
1461 found->bytes_used = bytes_used;
1462 found->bytes_pinned = 0;
1464 found->force_alloc = 0;
1465 *space_info = found;
1469 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1471 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1472 BTRFS_BLOCK_GROUP_RAID1 |
1473 BTRFS_BLOCK_GROUP_RAID10 |
1474 BTRFS_BLOCK_GROUP_DUP);
1476 if (flags & BTRFS_BLOCK_GROUP_DATA)
1477 fs_info->avail_data_alloc_bits |= extra_flags;
1478 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1479 fs_info->avail_metadata_alloc_bits |= extra_flags;
1480 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1481 fs_info->avail_system_alloc_bits |= extra_flags;
1485 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1487 u64 num_devices = root->fs_info->fs_devices->num_devices;
1489 if (num_devices == 1)
1490 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1491 if (num_devices < 4)
1492 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1494 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1495 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1496 BTRFS_BLOCK_GROUP_RAID10))) {
1497 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1500 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1501 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1502 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1505 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1506 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1507 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1508 (flags & BTRFS_BLOCK_GROUP_DUP)))
1509 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1513 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1514 struct btrfs_root *extent_root, u64 alloc_bytes,
1515 u64 flags, int force)
1517 struct btrfs_space_info *space_info;
1523 flags = reduce_alloc_profile(extent_root, flags);
1525 space_info = __find_space_info(extent_root->fs_info, flags);
1527 ret = update_space_info(extent_root->fs_info, flags,
1531 BUG_ON(!space_info);
1533 if (space_info->force_alloc) {
1535 space_info->force_alloc = 0;
1537 if (space_info->full)
1540 thresh = div_factor(space_info->total_bytes, 6);
1542 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1546 mutex_lock(&extent_root->fs_info->chunk_mutex);
1547 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1548 if (ret == -ENOSPC) {
1549 printk("space info full %Lu\n", flags);
1550 space_info->full = 1;
1555 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1556 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1560 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1565 static int update_block_group(struct btrfs_trans_handle *trans,
1566 struct btrfs_root *root,
1567 u64 bytenr, u64 num_bytes, int alloc,
1570 struct btrfs_block_group_cache *cache;
1571 struct btrfs_fs_info *info = root->fs_info;
1572 u64 total = num_bytes;
1576 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1578 cache = btrfs_lookup_block_group(info, bytenr);
1582 byte_in_group = bytenr - cache->key.objectid;
1583 WARN_ON(byte_in_group > cache->key.offset);
1585 spin_lock(&cache->lock);
1587 old_val = btrfs_block_group_used(&cache->item);
1588 num_bytes = min(total, cache->key.offset - byte_in_group);
1590 old_val += num_bytes;
1591 cache->space_info->bytes_used += num_bytes;
1592 btrfs_set_block_group_used(&cache->item, old_val);
1593 spin_unlock(&cache->lock);
1595 old_val -= num_bytes;
1596 cache->space_info->bytes_used -= num_bytes;
1597 btrfs_set_block_group_used(&cache->item, old_val);
1598 spin_unlock(&cache->lock);
1601 ret = btrfs_add_free_space(cache, bytenr,
1608 bytenr += num_bytes;
1613 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1615 struct btrfs_block_group_cache *cache;
1617 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1621 return cache->key.objectid;
1625 int btrfs_update_pinned_extents(struct btrfs_root *root,
1626 u64 bytenr, u64 num, int pin)
1629 struct btrfs_block_group_cache *cache;
1630 struct btrfs_fs_info *fs_info = root->fs_info;
1632 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1634 set_extent_dirty(&fs_info->pinned_extents,
1635 bytenr, bytenr + num - 1, GFP_NOFS);
1637 clear_extent_dirty(&fs_info->pinned_extents,
1638 bytenr, bytenr + num - 1, GFP_NOFS);
1641 cache = btrfs_lookup_block_group(fs_info, bytenr);
1643 u64 first = first_logical_byte(root, bytenr);
1644 WARN_ON(first < bytenr);
1645 len = min(first - bytenr, num);
1647 len = min(num, cache->key.offset -
1648 (bytenr - cache->key.objectid));
1652 spin_lock(&cache->lock);
1653 cache->pinned += len;
1654 cache->space_info->bytes_pinned += len;
1655 spin_unlock(&cache->lock);
1657 fs_info->total_pinned += len;
1660 spin_lock(&cache->lock);
1661 cache->pinned -= len;
1662 cache->space_info->bytes_pinned -= len;
1663 spin_unlock(&cache->lock);
1665 fs_info->total_pinned -= len;
1673 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1678 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1682 ret = find_first_extent_bit(pinned_extents, last,
1683 &start, &end, EXTENT_DIRTY);
1686 set_extent_dirty(copy, start, end, GFP_NOFS);
1692 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1693 struct btrfs_root *root,
1694 struct extent_io_tree *unpin)
1699 struct btrfs_block_group_cache *cache;
1701 mutex_lock(&root->fs_info->alloc_mutex);
1703 ret = find_first_extent_bit(unpin, 0, &start, &end,
1707 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
1708 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1709 cache = btrfs_lookup_block_group(root->fs_info, start);
1711 btrfs_add_free_space(cache, start, end - start + 1);
1712 if (need_resched()) {
1713 mutex_unlock(&root->fs_info->alloc_mutex);
1715 mutex_lock(&root->fs_info->alloc_mutex);
1718 mutex_unlock(&root->fs_info->alloc_mutex);
1722 static int finish_current_insert(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *extent_root)
1728 struct btrfs_fs_info *info = extent_root->fs_info;
1729 struct btrfs_path *path;
1730 struct btrfs_extent_ref *ref;
1731 struct pending_extent_op *extent_op;
1732 struct btrfs_key key;
1733 struct btrfs_extent_item extent_item;
1737 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1738 btrfs_set_stack_extent_refs(&extent_item, 1);
1739 path = btrfs_alloc_path();
1742 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1743 &end, EXTENT_LOCKED);
1747 ret = get_state_private(&info->extent_ins, start, &priv);
1749 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1751 if (extent_op->type == PENDING_EXTENT_INSERT) {
1752 key.objectid = start;
1753 key.offset = end + 1 - start;
1754 key.type = BTRFS_EXTENT_ITEM_KEY;
1755 err = btrfs_insert_item(trans, extent_root, &key,
1756 &extent_item, sizeof(extent_item));
1759 clear_extent_bits(&info->extent_ins, start, end,
1760 EXTENT_LOCKED, GFP_NOFS);
1762 err = insert_extent_backref(trans, extent_root, path,
1763 start, extent_op->parent,
1764 extent_root->root_key.objectid,
1765 extent_op->generation,
1766 extent_op->level, 0);
1768 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1769 err = lookup_extent_backref(trans, extent_root, path,
1770 start, extent_op->orig_parent,
1771 extent_root->root_key.objectid,
1772 extent_op->orig_generation, 0);
1775 clear_extent_bits(&info->extent_ins, start, end,
1776 EXTENT_LOCKED, GFP_NOFS);
1778 key.objectid = start;
1779 key.offset = extent_op->parent;
1780 key.type = BTRFS_EXTENT_REF_KEY;
1781 err = btrfs_set_item_key_safe(trans, extent_root, path,
1784 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1785 struct btrfs_extent_ref);
1786 btrfs_set_ref_generation(path->nodes[0], ref,
1787 extent_op->generation);
1788 btrfs_mark_buffer_dirty(path->nodes[0]);
1789 btrfs_release_path(extent_root, path);
1795 if (need_resched()) {
1796 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1798 mutex_lock(&extent_root->fs_info->alloc_mutex);
1801 btrfs_free_path(path);
1805 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1806 struct btrfs_root *root,
1807 u64 bytenr, u64 num_bytes, int is_data)
1810 struct extent_buffer *buf;
1812 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1816 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1820 /* we can reuse a block if it hasn't been written
1821 * and it is from this transaction. We can't
1822 * reuse anything from the tree log root because
1823 * it has tiny sub-transactions.
1825 if (btrfs_buffer_uptodate(buf, 0) &&
1826 btrfs_try_tree_lock(buf)) {
1827 u64 header_owner = btrfs_header_owner(buf);
1828 u64 header_transid = btrfs_header_generation(buf);
1829 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1830 header_transid == trans->transid &&
1831 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1832 clean_tree_block(NULL, root, buf);
1833 btrfs_tree_unlock(buf);
1834 free_extent_buffer(buf);
1837 btrfs_tree_unlock(buf);
1839 free_extent_buffer(buf);
1841 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1848 * remove an extent from the root, returns 0 on success
1850 static int __free_extent(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes, u64 parent,
1853 u64 root_objectid, u64 ref_generation,
1854 u64 owner_objectid, u64 owner_offset,
1855 int pin, int mark_free)
1857 struct btrfs_path *path;
1858 struct btrfs_key key;
1859 struct btrfs_fs_info *info = root->fs_info;
1860 struct btrfs_root *extent_root = info->extent_root;
1861 struct extent_buffer *leaf;
1863 int extent_slot = 0;
1864 int found_extent = 0;
1866 struct btrfs_extent_item *ei;
1869 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1870 key.objectid = bytenr;
1871 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1872 key.offset = num_bytes;
1873 path = btrfs_alloc_path();
1878 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1879 root_objectid, ref_generation, 1);
1881 struct btrfs_key found_key;
1882 extent_slot = path->slots[0];
1883 while(extent_slot > 0) {
1885 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1887 if (found_key.objectid != bytenr)
1889 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1890 found_key.offset == num_bytes) {
1894 if (path->slots[0] - extent_slot > 5)
1897 if (!found_extent) {
1898 ret = remove_extent_backref(trans, extent_root, path);
1900 btrfs_release_path(extent_root, path);
1901 ret = btrfs_search_slot(trans, extent_root,
1904 extent_slot = path->slots[0];
1907 btrfs_print_leaf(extent_root, path->nodes[0]);
1909 printk("Unable to find ref byte nr %Lu root %Lu "
1910 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1911 root_objectid, ref_generation, owner_objectid,
1915 leaf = path->nodes[0];
1916 ei = btrfs_item_ptr(leaf, extent_slot,
1917 struct btrfs_extent_item);
1918 refs = btrfs_extent_refs(leaf, ei);
1921 btrfs_set_extent_refs(leaf, ei, refs);
1923 btrfs_mark_buffer_dirty(leaf);
1925 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1926 struct btrfs_extent_ref *ref;
1927 ref = btrfs_item_ptr(leaf, path->slots[0],
1928 struct btrfs_extent_ref);
1929 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1930 /* if the back ref and the extent are next to each other
1931 * they get deleted below in one shot
1933 path->slots[0] = extent_slot;
1935 } else if (found_extent) {
1936 /* otherwise delete the extent back ref */
1937 ret = remove_extent_backref(trans, extent_root, path);
1939 /* if refs are 0, we need to setup the path for deletion */
1941 btrfs_release_path(extent_root, path);
1942 ret = btrfs_search_slot(trans, extent_root, &key, path,
1951 #ifdef BIO_RW_DISCARD
1952 u64 map_length = num_bytes;
1953 struct btrfs_multi_bio *multi = NULL;
1957 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1958 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
1964 /* block accounting for super block */
1965 spin_lock_irq(&info->delalloc_lock);
1966 super_used = btrfs_super_bytes_used(&info->super_copy);
1967 btrfs_set_super_bytes_used(&info->super_copy,
1968 super_used - num_bytes);
1969 spin_unlock_irq(&info->delalloc_lock);
1971 /* block accounting for root item */
1972 root_used = btrfs_root_used(&root->root_item);
1973 btrfs_set_root_used(&root->root_item,
1974 root_used - num_bytes);
1975 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1978 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1982 #ifdef BIO_RW_DISCARD
1983 /* Tell the block device(s) that the sectors can be discarded */
1984 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1985 bytenr, &map_length, &multi, 0);
1987 struct btrfs_bio_stripe *stripe = multi->stripes;
1990 if (map_length > num_bytes)
1991 map_length = num_bytes;
1993 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1994 blkdev_issue_discard(stripe->dev->bdev,
1995 stripe->physical >> 9,
2002 btrfs_free_path(path);
2003 finish_current_insert(trans, extent_root);
2008 * find all the blocks marked as pending in the radix tree and remove
2009 * them from the extent map
2011 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2012 btrfs_root *extent_root)
2020 struct extent_io_tree *pending_del;
2021 struct extent_io_tree *extent_ins;
2022 struct pending_extent_op *extent_op;
2024 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2025 extent_ins = &extent_root->fs_info->extent_ins;
2026 pending_del = &extent_root->fs_info->pending_del;
2029 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2034 ret = get_state_private(pending_del, start, &priv);
2036 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2038 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2041 ret = pin_down_bytes(trans, extent_root, start,
2042 end + 1 - start, 0);
2043 mark_free = ret > 0;
2044 if (!test_range_bit(extent_ins, start, end,
2045 EXTENT_LOCKED, 0)) {
2047 ret = __free_extent(trans, extent_root,
2048 start, end + 1 - start,
2049 extent_op->orig_parent,
2050 extent_root->root_key.objectid,
2051 extent_op->orig_generation,
2052 extent_op->level, 0, 0, mark_free);
2056 ret = get_state_private(extent_ins, start, &priv);
2058 extent_op = (struct pending_extent_op *)
2059 (unsigned long)priv;
2061 clear_extent_bits(extent_ins, start, end,
2062 EXTENT_LOCKED, GFP_NOFS);
2064 if (extent_op->type == PENDING_BACKREF_UPDATE)
2067 ret = update_block_group(trans, extent_root, start,
2068 end + 1 - start, 0, mark_free);
2075 if (need_resched()) {
2076 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2078 mutex_lock(&extent_root->fs_info->alloc_mutex);
2085 * remove an extent from the root, returns 0 on success
2087 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2088 struct btrfs_root *root,
2089 u64 bytenr, u64 num_bytes, u64 parent,
2090 u64 root_objectid, u64 ref_generation,
2091 u64 owner_objectid, u64 owner_offset, int pin)
2093 struct btrfs_root *extent_root = root->fs_info->extent_root;
2097 WARN_ON(num_bytes < root->sectorsize);
2098 if (root == extent_root) {
2099 struct pending_extent_op *extent_op;
2101 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2104 extent_op->type = PENDING_EXTENT_DELETE;
2105 extent_op->bytenr = bytenr;
2106 extent_op->num_bytes = num_bytes;
2107 extent_op->parent = parent;
2108 extent_op->orig_parent = parent;
2109 extent_op->generation = ref_generation;
2110 extent_op->orig_generation = ref_generation;
2111 extent_op->level = (int)owner_objectid;
2113 set_extent_bits(&root->fs_info->pending_del,
2114 bytenr, bytenr + num_bytes - 1,
2115 EXTENT_LOCKED, GFP_NOFS);
2116 set_state_private(&root->fs_info->pending_del,
2117 bytenr, (unsigned long)extent_op);
2120 /* if metadata always pin */
2121 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2122 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2123 struct btrfs_block_group_cache *cache;
2125 /* btrfs_free_reserved_extent */
2126 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2128 btrfs_add_free_space(cache, bytenr, num_bytes);
2134 /* if data pin when any transaction has committed this */
2135 if (ref_generation != trans->transid)
2138 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2139 root_objectid, ref_generation, owner_objectid,
2140 owner_offset, pin, pin == 0);
2142 finish_current_insert(trans, root->fs_info->extent_root);
2143 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2144 return ret ? ret : pending_ret;
2147 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2148 struct btrfs_root *root,
2149 u64 bytenr, u64 num_bytes, u64 parent,
2150 u64 root_objectid, u64 ref_generation,
2151 u64 owner_objectid, u64 owner_offset, int pin)
2155 maybe_lock_mutex(root);
2156 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2157 root_objectid, ref_generation,
2158 owner_objectid, owner_offset, pin);
2159 maybe_unlock_mutex(root);
2163 static u64 stripe_align(struct btrfs_root *root, u64 val)
2165 u64 mask = ((u64)root->stripesize - 1);
2166 u64 ret = (val + mask) & ~mask;
2171 * walks the btree of allocated extents and find a hole of a given size.
2172 * The key ins is changed to record the hole:
2173 * ins->objectid == block start
2174 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2175 * ins->offset == number of blocks
2176 * Any available blocks before search_start are skipped.
2178 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2179 struct btrfs_root *orig_root,
2180 u64 num_bytes, u64 empty_size,
2181 u64 search_start, u64 search_end,
2182 u64 hint_byte, struct btrfs_key *ins,
2183 u64 exclude_start, u64 exclude_nr,
2187 u64 orig_search_start;
2188 struct btrfs_root * root = orig_root->fs_info->extent_root;
2189 struct btrfs_fs_info *info = root->fs_info;
2190 u64 total_needed = num_bytes;
2191 u64 *last_ptr = NULL;
2192 struct btrfs_block_group_cache *block_group;
2193 int chunk_alloc_done = 0;
2194 int empty_cluster = 2 * 1024 * 1024;
2195 int allowed_chunk_alloc = 0;
2197 WARN_ON(num_bytes < root->sectorsize);
2198 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2200 if (orig_root->ref_cows || empty_size)
2201 allowed_chunk_alloc = 1;
2203 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2204 last_ptr = &root->fs_info->last_alloc;
2205 empty_cluster = 256 * 1024;
2208 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2209 last_ptr = &root->fs_info->last_data_alloc;
2211 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2212 last_ptr = &root->fs_info->last_log_alloc;
2213 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2214 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2220 hint_byte = *last_ptr;
2222 empty_size += empty_cluster;
2225 search_start = max(search_start, first_logical_byte(root, 0));
2226 orig_search_start = search_start;
2228 if (search_end == (u64)-1)
2229 search_end = btrfs_super_total_bytes(&info->super_copy);
2231 search_start = max(search_start, hint_byte);
2232 total_needed += empty_size;
2235 block_group = btrfs_lookup_block_group(info, search_start);
2238 * Ok this looks a little tricky, buts its really simple. First if we
2239 * didn't find a block group obviously we want to start over.
2240 * Secondly, if the block group we found does not match the type we
2241 * need, and we have a last_ptr and its not 0, chances are the last
2242 * allocation we made was at the end of the block group, so lets go
2243 * ahead and skip the looking through the rest of the block groups and
2244 * start at the beginning. This helps with metadata allocations,
2245 * since you are likely to have a bunch of data block groups to search
2246 * through first before you realize that you need to start over, so go
2247 * ahead and start over and save the time.
2249 if (!block_group || (!block_group_bits(block_group, data) &&
2250 last_ptr && *last_ptr)) {
2251 if (search_start != orig_search_start) {
2252 if (last_ptr && *last_ptr)
2254 search_start = orig_search_start;
2256 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2257 ret = do_chunk_alloc(trans, root,
2258 num_bytes + 2 * 1024 * 1024,
2261 struct btrfs_space_info *info;
2263 info = __find_space_info(root->fs_info, data);
2267 chunk_alloc_done = 1;
2268 search_start = orig_search_start;
2277 * this is going to seach through all of the existing block groups it
2278 * can find, so if we don't find something we need to see if we can
2279 * allocate what we need.
2281 ret = find_free_space(root, &block_group, &search_start,
2282 total_needed, data);
2283 if (ret == -ENOSPC) {
2285 * instead of allocating, start at the original search start
2286 * and see if there is something to be found, if not then we
2289 if (search_start != orig_search_start) {
2290 if (last_ptr && *last_ptr) {
2292 total_needed += empty_cluster;
2294 search_start = orig_search_start;
2299 * we've already allocated, we're pretty screwed
2301 if (chunk_alloc_done) {
2303 } else if (!allowed_chunk_alloc && block_group &&
2304 block_group_bits(block_group, data)) {
2305 block_group->space_info->force_alloc = 1;
2307 } else if (!allowed_chunk_alloc) {
2311 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2317 chunk_alloc_done = 1;
2319 search_start = block_group->key.objectid +
2320 block_group->key.offset;
2322 search_start = orig_search_start;
2329 search_start = stripe_align(root, search_start);
2330 ins->objectid = search_start;
2331 ins->offset = num_bytes;
2333 if (ins->objectid + num_bytes >= search_end) {
2334 search_start = orig_search_start;
2335 if (chunk_alloc_done) {
2342 if (ins->objectid + num_bytes >
2343 block_group->key.objectid + block_group->key.offset) {
2344 if (search_start == orig_search_start && chunk_alloc_done) {
2348 search_start = block_group->key.objectid +
2349 block_group->key.offset;
2353 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2354 ins->objectid < exclude_start + exclude_nr)) {
2355 search_start = exclude_start + exclude_nr;
2359 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2360 trans->block_group = block_group;
2362 ins->offset = num_bytes;
2364 *last_ptr = ins->objectid + ins->offset;
2366 btrfs_super_total_bytes(&root->fs_info->super_copy))
2375 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2377 struct btrfs_block_group_cache *cache;
2378 struct list_head *l;
2380 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2381 info->total_bytes - info->bytes_used - info->bytes_pinned,
2382 (info->full) ? "" : "not ");
2384 spin_lock(&info->lock);
2385 list_for_each(l, &info->block_groups) {
2386 cache = list_entry(l, struct btrfs_block_group_cache, list);
2387 spin_lock(&cache->lock);
2388 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
2390 cache->key.objectid, cache->key.offset,
2391 btrfs_block_group_used(&cache->item), cache->pinned);
2392 btrfs_dump_free_space(cache, bytes);
2393 spin_unlock(&cache->lock);
2395 spin_unlock(&info->lock);
2397 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2398 struct btrfs_root *root,
2399 u64 num_bytes, u64 min_alloc_size,
2400 u64 empty_size, u64 hint_byte,
2401 u64 search_end, struct btrfs_key *ins,
2405 u64 search_start = 0;
2407 struct btrfs_fs_info *info = root->fs_info;
2408 struct btrfs_block_group_cache *cache;
2411 alloc_profile = info->avail_data_alloc_bits &
2412 info->data_alloc_profile;
2413 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2414 } else if (root == root->fs_info->chunk_root) {
2415 alloc_profile = info->avail_system_alloc_bits &
2416 info->system_alloc_profile;
2417 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2419 alloc_profile = info->avail_metadata_alloc_bits &
2420 info->metadata_alloc_profile;
2421 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2424 data = reduce_alloc_profile(root, data);
2426 * the only place that sets empty_size is btrfs_realloc_node, which
2427 * is not called recursively on allocations
2429 if (empty_size || root->ref_cows) {
2430 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2431 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2433 BTRFS_BLOCK_GROUP_METADATA |
2434 (info->metadata_alloc_profile &
2435 info->avail_metadata_alloc_bits), 0);
2437 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2438 num_bytes + 2 * 1024 * 1024, data, 0);
2441 WARN_ON(num_bytes < root->sectorsize);
2442 ret = find_free_extent(trans, root, num_bytes, empty_size,
2443 search_start, search_end, hint_byte, ins,
2444 trans->alloc_exclude_start,
2445 trans->alloc_exclude_nr, data);
2447 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2448 num_bytes = num_bytes >> 1;
2449 num_bytes = num_bytes & ~(root->sectorsize - 1);
2450 num_bytes = max(num_bytes, min_alloc_size);
2451 do_chunk_alloc(trans, root->fs_info->extent_root,
2452 num_bytes, data, 1);
2456 struct btrfs_space_info *sinfo;
2458 sinfo = __find_space_info(root->fs_info, data);
2459 printk("allocation failed flags %Lu, wanted %Lu\n",
2461 dump_space_info(sinfo, num_bytes);
2464 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2466 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2470 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2475 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2477 struct btrfs_block_group_cache *cache;
2479 maybe_lock_mutex(root);
2480 cache = btrfs_lookup_block_group(root->fs_info, start);
2482 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2483 maybe_unlock_mutex(root);
2486 btrfs_add_free_space(cache, start, len);
2487 maybe_unlock_mutex(root);
2491 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2492 struct btrfs_root *root,
2493 u64 num_bytes, u64 min_alloc_size,
2494 u64 empty_size, u64 hint_byte,
2495 u64 search_end, struct btrfs_key *ins,
2499 maybe_lock_mutex(root);
2500 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2501 empty_size, hint_byte, search_end, ins,
2503 maybe_unlock_mutex(root);
2507 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2508 struct btrfs_root *root, u64 parent,
2509 u64 root_objectid, u64 ref_generation,
2510 u64 owner, u64 owner_offset,
2511 struct btrfs_key *ins)
2517 u64 num_bytes = ins->offset;
2519 struct btrfs_fs_info *info = root->fs_info;
2520 struct btrfs_root *extent_root = info->extent_root;
2521 struct btrfs_extent_item *extent_item;
2522 struct btrfs_extent_ref *ref;
2523 struct btrfs_path *path;
2524 struct btrfs_key keys[2];
2527 parent = ins->objectid;
2529 /* block accounting for super block */
2530 spin_lock_irq(&info->delalloc_lock);
2531 super_used = btrfs_super_bytes_used(&info->super_copy);
2532 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2533 spin_unlock_irq(&info->delalloc_lock);
2535 /* block accounting for root item */
2536 root_used = btrfs_root_used(&root->root_item);
2537 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2539 if (root == extent_root) {
2540 struct pending_extent_op *extent_op;
2542 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2545 extent_op->type = PENDING_EXTENT_INSERT;
2546 extent_op->bytenr = ins->objectid;
2547 extent_op->num_bytes = ins->offset;
2548 extent_op->parent = parent;
2549 extent_op->orig_parent = 0;
2550 extent_op->generation = ref_generation;
2551 extent_op->orig_generation = 0;
2552 extent_op->level = (int)owner;
2554 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2555 ins->objectid + ins->offset - 1,
2556 EXTENT_LOCKED, GFP_NOFS);
2557 set_state_private(&root->fs_info->extent_ins,
2558 ins->objectid, (unsigned long)extent_op);
2562 memcpy(&keys[0], ins, sizeof(*ins));
2563 keys[1].objectid = ins->objectid;
2564 keys[1].type = BTRFS_EXTENT_REF_KEY;
2565 keys[1].offset = parent;
2566 sizes[0] = sizeof(*extent_item);
2567 sizes[1] = sizeof(*ref);
2569 path = btrfs_alloc_path();
2572 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2576 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2577 struct btrfs_extent_item);
2578 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2579 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2580 struct btrfs_extent_ref);
2582 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2583 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2584 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2585 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2586 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
2588 btrfs_mark_buffer_dirty(path->nodes[0]);
2590 trans->alloc_exclude_start = 0;
2591 trans->alloc_exclude_nr = 0;
2592 btrfs_free_path(path);
2593 finish_current_insert(trans, extent_root);
2594 pending_ret = del_pending_extents(trans, extent_root);
2604 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
2606 printk("update block group failed for %Lu %Lu\n",
2607 ins->objectid, ins->offset);
2614 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2615 struct btrfs_root *root, u64 parent,
2616 u64 root_objectid, u64 ref_generation,
2617 u64 owner, u64 owner_offset,
2618 struct btrfs_key *ins)
2622 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2624 maybe_lock_mutex(root);
2625 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2626 root_objectid, ref_generation,
2627 owner, owner_offset, ins);
2628 maybe_unlock_mutex(root);
2633 * this is used by the tree logging recovery code. It records that
2634 * an extent has been allocated and makes sure to clear the free
2635 * space cache bits as well
2637 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2638 struct btrfs_root *root, u64 parent,
2639 u64 root_objectid, u64 ref_generation,
2640 u64 owner, u64 owner_offset,
2641 struct btrfs_key *ins)
2644 struct btrfs_block_group_cache *block_group;
2646 maybe_lock_mutex(root);
2647 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2648 cache_block_group(root, block_group);
2650 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2652 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2653 root_objectid, ref_generation,
2654 owner, owner_offset, ins);
2655 maybe_unlock_mutex(root);
2660 * finds a free extent and does all the dirty work required for allocation
2661 * returns the key for the extent through ins, and a tree buffer for
2662 * the first block of the extent through buf.
2664 * returns 0 if everything worked, non-zero otherwise.
2666 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root,
2668 u64 num_bytes, u64 parent, u64 min_alloc_size,
2669 u64 root_objectid, u64 ref_generation,
2670 u64 owner_objectid, u64 owner_offset,
2671 u64 empty_size, u64 hint_byte,
2672 u64 search_end, struct btrfs_key *ins, u64 data)
2676 maybe_lock_mutex(root);
2678 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2679 min_alloc_size, empty_size, hint_byte,
2680 search_end, ins, data);
2682 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2683 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2684 root_objectid, ref_generation,
2685 owner_objectid, owner_offset, ins);
2689 maybe_unlock_mutex(root);
2693 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2694 struct btrfs_root *root,
2695 u64 bytenr, u32 blocksize)
2697 struct extent_buffer *buf;
2699 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2701 return ERR_PTR(-ENOMEM);
2702 btrfs_set_header_generation(buf, trans->transid);
2703 btrfs_tree_lock(buf);
2704 clean_tree_block(trans, root, buf);
2705 btrfs_set_buffer_uptodate(buf);
2706 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2707 set_extent_dirty(&root->dirty_log_pages, buf->start,
2708 buf->start + buf->len - 1, GFP_NOFS);
2710 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2711 buf->start + buf->len - 1, GFP_NOFS);
2713 trans->blocks_used++;
2718 * helper function to allocate a block for a given tree
2719 * returns the tree buffer or NULL.
2721 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *root,
2723 u32 blocksize, u64 parent,
2730 struct btrfs_key ins;
2732 struct extent_buffer *buf;
2734 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2735 root_objectid, ref_generation, level, 0,
2736 empty_size, hint, (u64)-1, &ins, 0);
2739 return ERR_PTR(ret);
2742 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
2746 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root, struct extent_buffer *leaf)
2750 u64 leaf_generation;
2751 struct btrfs_key key;
2752 struct btrfs_file_extent_item *fi;
2757 BUG_ON(!btrfs_is_leaf(leaf));
2758 nritems = btrfs_header_nritems(leaf);
2759 leaf_owner = btrfs_header_owner(leaf);
2760 leaf_generation = btrfs_header_generation(leaf);
2762 for (i = 0; i < nritems; i++) {
2766 btrfs_item_key_to_cpu(leaf, &key, i);
2767 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2769 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2770 if (btrfs_file_extent_type(leaf, fi) ==
2771 BTRFS_FILE_EXTENT_INLINE)
2774 * FIXME make sure to insert a trans record that
2775 * repeats the snapshot del on crash
2777 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2778 if (disk_bytenr == 0)
2781 mutex_lock(&root->fs_info->alloc_mutex);
2782 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2783 btrfs_file_extent_disk_num_bytes(leaf, fi),
2784 leaf->start, leaf_owner, leaf_generation,
2785 key.objectid, key.offset, 0);
2786 mutex_unlock(&root->fs_info->alloc_mutex);
2789 atomic_inc(&root->fs_info->throttle_gen);
2790 wake_up(&root->fs_info->transaction_throttle);
2796 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2797 struct btrfs_root *root,
2798 struct btrfs_leaf_ref *ref)
2802 struct btrfs_extent_info *info = ref->extents;
2804 for (i = 0; i < ref->nritems; i++) {
2805 mutex_lock(&root->fs_info->alloc_mutex);
2806 ret = __btrfs_free_extent(trans, root, info->bytenr,
2807 info->num_bytes, ref->bytenr,
2808 ref->owner, ref->generation,
2809 info->objectid, info->offset, 0);
2810 mutex_unlock(&root->fs_info->alloc_mutex);
2812 atomic_inc(&root->fs_info->throttle_gen);
2813 wake_up(&root->fs_info->transaction_throttle);
2823 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2828 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
2831 #if 0 // some debugging code in case we see problems here
2832 /* if the refs count is one, it won't get increased again. But
2833 * if the ref count is > 1, someone may be decreasing it at
2834 * the same time we are.
2837 struct extent_buffer *eb = NULL;
2838 eb = btrfs_find_create_tree_block(root, start, len);
2840 btrfs_tree_lock(eb);
2842 mutex_lock(&root->fs_info->alloc_mutex);
2843 ret = lookup_extent_ref(NULL, root, start, len, refs);
2845 mutex_unlock(&root->fs_info->alloc_mutex);
2848 btrfs_tree_unlock(eb);
2849 free_extent_buffer(eb);
2852 printk("block %llu went down to one during drop_snap\n",
2853 (unsigned long long)start);
2864 * helper function for drop_snapshot, this walks down the tree dropping ref
2865 * counts as it goes.
2867 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2868 struct btrfs_root *root,
2869 struct btrfs_path *path, int *level)
2875 struct extent_buffer *next;
2876 struct extent_buffer *cur;
2877 struct extent_buffer *parent;
2878 struct btrfs_leaf_ref *ref;
2883 WARN_ON(*level < 0);
2884 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2885 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
2886 path->nodes[*level]->len, &refs);
2892 * walk down to the last node level and free all the leaves
2894 while(*level >= 0) {
2895 WARN_ON(*level < 0);
2896 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2897 cur = path->nodes[*level];
2899 if (btrfs_header_level(cur) != *level)
2902 if (path->slots[*level] >=
2903 btrfs_header_nritems(cur))
2906 ret = btrfs_drop_leaf_ref(trans, root, cur);
2910 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2911 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2912 blocksize = btrfs_level_size(root, *level - 1);
2914 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
2917 parent = path->nodes[*level];
2918 root_owner = btrfs_header_owner(parent);
2919 root_gen = btrfs_header_generation(parent);
2920 path->slots[*level]++;
2922 mutex_lock(&root->fs_info->alloc_mutex);
2923 ret = __btrfs_free_extent(trans, root, bytenr,
2924 blocksize, parent->start,
2925 root_owner, root_gen, 0, 0, 1);
2927 mutex_unlock(&root->fs_info->alloc_mutex);
2929 atomic_inc(&root->fs_info->throttle_gen);
2930 wake_up(&root->fs_info->transaction_throttle);
2936 * at this point, we have a single ref, and since the
2937 * only place referencing this extent is a dead root
2938 * the reference count should never go higher.
2939 * So, we don't need to check it again
2942 ref = btrfs_lookup_leaf_ref(root, bytenr);
2944 ret = cache_drop_leaf_ref(trans, root, ref);
2946 btrfs_remove_leaf_ref(root, ref);
2947 btrfs_free_leaf_ref(root, ref);
2951 if (printk_ratelimit())
2952 printk("leaf ref miss for bytenr %llu\n",
2953 (unsigned long long)bytenr);
2955 next = btrfs_find_tree_block(root, bytenr, blocksize);
2956 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2957 free_extent_buffer(next);
2959 next = read_tree_block(root, bytenr, blocksize,
2964 * this is a debugging check and can go away
2965 * the ref should never go all the way down to 1
2968 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2974 WARN_ON(*level <= 0);
2975 if (path->nodes[*level-1])
2976 free_extent_buffer(path->nodes[*level-1]);
2977 path->nodes[*level-1] = next;
2978 *level = btrfs_header_level(next);
2979 path->slots[*level] = 0;
2983 WARN_ON(*level < 0);
2984 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2986 if (path->nodes[*level] == root->node) {
2987 parent = path->nodes[*level];
2988 bytenr = path->nodes[*level]->start;
2990 parent = path->nodes[*level + 1];
2991 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
2994 blocksize = btrfs_level_size(root, *level);
2995 root_owner = btrfs_header_owner(parent);
2996 root_gen = btrfs_header_generation(parent);
2998 mutex_lock(&root->fs_info->alloc_mutex);
2999 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3000 parent->start, root_owner, root_gen,
3002 mutex_unlock(&root->fs_info->alloc_mutex);
3003 free_extent_buffer(path->nodes[*level]);
3004 path->nodes[*level] = NULL;
3013 * helper for dropping snapshots. This walks back up the tree in the path
3014 * to find the first node higher up where we haven't yet gone through
3017 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root,
3019 struct btrfs_path *path, int *level)
3023 struct btrfs_root_item *root_item = &root->root_item;
3028 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3029 slot = path->slots[i];
3030 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3031 struct extent_buffer *node;
3032 struct btrfs_disk_key disk_key;
3033 node = path->nodes[i];
3036 WARN_ON(*level == 0);
3037 btrfs_node_key(node, &disk_key, path->slots[i]);
3038 memcpy(&root_item->drop_progress,
3039 &disk_key, sizeof(disk_key));
3040 root_item->drop_level = i;
3043 struct extent_buffer *parent;
3044 if (path->nodes[*level] == root->node)
3045 parent = path->nodes[*level];
3047 parent = path->nodes[*level + 1];
3049 root_owner = btrfs_header_owner(parent);
3050 root_gen = btrfs_header_generation(parent);
3051 ret = btrfs_free_extent(trans, root,
3052 path->nodes[*level]->start,
3053 path->nodes[*level]->len,
3055 root_owner, root_gen, 0, 0, 1);
3057 free_extent_buffer(path->nodes[*level]);
3058 path->nodes[*level] = NULL;
3066 * drop the reference count on the tree rooted at 'snap'. This traverses
3067 * the tree freeing any blocks that have a ref count of zero after being
3070 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3076 struct btrfs_path *path;
3079 struct btrfs_root_item *root_item = &root->root_item;
3081 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3082 path = btrfs_alloc_path();
3085 level = btrfs_header_level(root->node);
3087 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3088 path->nodes[level] = root->node;
3089 extent_buffer_get(root->node);
3090 path->slots[level] = 0;
3092 struct btrfs_key key;
3093 struct btrfs_disk_key found_key;
3094 struct extent_buffer *node;
3096 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3097 level = root_item->drop_level;
3098 path->lowest_level = level;
3099 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3104 node = path->nodes[level];
3105 btrfs_node_key(node, &found_key, path->slots[level]);
3106 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3107 sizeof(found_key)));
3109 * unlock our path, this is safe because only this
3110 * function is allowed to delete this snapshot
3112 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3113 if (path->nodes[i] && path->locks[i]) {
3115 btrfs_tree_unlock(path->nodes[i]);
3120 wret = walk_down_tree(trans, root, path, &level);
3126 wret = walk_up_tree(trans, root, path, &level);
3131 if (trans->transaction->in_commit) {
3135 atomic_inc(&root->fs_info->throttle_gen);
3136 wake_up(&root->fs_info->transaction_throttle);
3138 for (i = 0; i <= orig_level; i++) {
3139 if (path->nodes[i]) {
3140 free_extent_buffer(path->nodes[i]);
3141 path->nodes[i] = NULL;
3145 btrfs_free_path(path);
3149 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3151 struct btrfs_block_group_cache *block_group;
3154 mutex_lock(&info->alloc_mutex);
3155 spin_lock(&info->block_group_cache_lock);
3156 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3157 block_group = rb_entry(n, struct btrfs_block_group_cache,
3160 btrfs_remove_free_space_cache(block_group);
3161 rb_erase(&block_group->cache_node,
3162 &info->block_group_cache_tree);
3163 spin_lock(&block_group->space_info->lock);
3164 list_del(&block_group->list);
3165 spin_unlock(&block_group->space_info->lock);
3168 spin_unlock(&info->block_group_cache_lock);
3169 mutex_unlock(&info->alloc_mutex);
3173 static unsigned long calc_ra(unsigned long start, unsigned long last,
3176 return min(last, start + nr - 1);
3179 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3184 unsigned long last_index;
3187 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3188 struct file_ra_state *ra;
3189 unsigned long total_read = 0;
3190 unsigned long ra_pages;
3191 struct btrfs_ordered_extent *ordered;
3192 struct btrfs_trans_handle *trans;
3194 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3196 mutex_lock(&inode->i_mutex);
3197 i = start >> PAGE_CACHE_SHIFT;
3198 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3200 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3202 file_ra_state_init(ra, inode->i_mapping);
3204 for (; i <= last_index; i++) {
3205 if (total_read % ra_pages == 0) {
3206 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3207 calc_ra(i, last_index, ra_pages));
3211 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3212 goto truncate_racing;
3213 page = grab_cache_page(inode->i_mapping, i);
3217 if (!PageUptodate(page)) {
3218 btrfs_readpage(NULL, page);
3220 if (!PageUptodate(page)) {
3222 page_cache_release(page);
3226 wait_on_page_writeback(page);
3228 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3229 page_end = page_start + PAGE_CACHE_SIZE - 1;
3230 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3232 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3234 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3236 page_cache_release(page);
3237 btrfs_start_ordered_extent(inode, ordered, 1);
3238 btrfs_put_ordered_extent(ordered);
3241 set_page_extent_mapped(page);
3244 * make sure page_mkwrite is called for this page if userland
3245 * wants to change it from mmap
3247 clear_page_dirty_for_io(page);
3249 btrfs_set_extent_delalloc(inode, page_start, page_end);
3250 set_page_dirty(page);
3252 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3254 page_cache_release(page);
3258 /* we have to start the IO in order to get the ordered extents
3259 * instantiated. This allows the relocation to code to wait
3260 * for all the ordered extents to hit the disk.
3262 * Otherwise, it would constantly loop over the same extents
3263 * because the old ones don't get deleted until the IO is
3266 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3269 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3271 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3272 mark_inode_dirty(inode);
3274 mutex_unlock(&inode->i_mutex);
3278 vmtruncate(inode, inode->i_size);
3279 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3285 * The back references tell us which tree holds a ref on a block,
3286 * but it is possible for the tree root field in the reference to
3287 * reflect the original root before a snapshot was made. In this
3288 * case we should search through all the children of a given root
3289 * to find potential holders of references on a block.
3291 * Instead, we do something a little less fancy and just search
3292 * all the roots for a given key/block combination.
3294 static int find_root_for_ref(struct btrfs_root *root,
3295 struct btrfs_path *path,
3296 struct btrfs_key *key0,
3299 struct btrfs_root **found_root,
3302 struct btrfs_key root_location;
3303 struct btrfs_root *cur_root = *found_root;
3304 struct btrfs_file_extent_item *file_extent;
3305 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3309 root_location.offset = (u64)-1;
3310 root_location.type = BTRFS_ROOT_ITEM_KEY;
3311 path->lowest_level = level;
3314 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3316 if (ret == 0 && file_key) {
3317 struct extent_buffer *leaf = path->nodes[0];
3318 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3319 struct btrfs_file_extent_item);
3320 if (btrfs_file_extent_type(leaf, file_extent) ==
3321 BTRFS_FILE_EXTENT_REG) {
3323 btrfs_file_extent_disk_bytenr(leaf,
3326 } else if (!file_key) {
3327 if (path->nodes[level])
3328 found_bytenr = path->nodes[level]->start;
3331 btrfs_release_path(cur_root, path);
3333 if (found_bytenr == bytenr) {
3334 *found_root = cur_root;
3338 ret = btrfs_search_root(root->fs_info->tree_root,
3339 root_search_start, &root_search_start);
3343 root_location.objectid = root_search_start;
3344 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3352 path->lowest_level = 0;
3357 * note, this releases the path
3359 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
3360 struct btrfs_path *path,
3361 struct btrfs_key *extent_key,
3362 u64 *last_file_objectid,
3363 u64 *last_file_offset,
3364 u64 *last_file_root,
3367 struct inode *inode;
3368 struct btrfs_root *found_root;
3369 struct btrfs_key root_location;
3370 struct btrfs_key found_key;
3371 struct btrfs_extent_ref *ref;
3379 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3381 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3382 struct btrfs_extent_ref);
3383 ref_root = btrfs_ref_root(path->nodes[0], ref);
3384 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3385 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3386 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3387 btrfs_release_path(extent_root, path);
3389 root_location.objectid = ref_root;
3391 root_location.offset = 0;
3393 root_location.offset = (u64)-1;
3394 root_location.type = BTRFS_ROOT_ITEM_KEY;
3396 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
3398 BUG_ON(!found_root);
3399 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3401 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
3402 found_key.objectid = ref_objectid;
3403 found_key.type = BTRFS_EXTENT_DATA_KEY;
3404 found_key.offset = ref_offset;
3407 if (last_extent == extent_key->objectid &&
3408 *last_file_objectid == ref_objectid &&
3409 *last_file_offset == ref_offset &&
3410 *last_file_root == ref_root)
3413 ret = find_root_for_ref(extent_root, path, &found_key,
3414 level, 1, &found_root,
3415 extent_key->objectid);
3420 if (last_extent == extent_key->objectid &&
3421 *last_file_objectid == ref_objectid &&
3422 *last_file_offset == ref_offset &&
3423 *last_file_root == ref_root)
3426 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3427 ref_objectid, found_root);
3428 if (inode->i_state & I_NEW) {
3429 /* the inode and parent dir are two different roots */
3430 BTRFS_I(inode)->root = found_root;
3431 BTRFS_I(inode)->location.objectid = ref_objectid;
3432 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3433 BTRFS_I(inode)->location.offset = 0;
3434 btrfs_read_locked_inode(inode);
3435 unlock_new_inode(inode);
3438 /* this can happen if the reference is not against
3439 * the latest version of the tree root
3441 if (is_bad_inode(inode))
3444 *last_file_objectid = inode->i_ino;
3445 *last_file_root = found_root->root_key.objectid;
3446 *last_file_offset = ref_offset;
3448 relocate_inode_pages(inode, ref_offset, extent_key->offset);
3451 struct btrfs_trans_handle *trans;
3452 struct extent_buffer *eb;
3455 eb = read_tree_block(found_root, extent_key->objectid,
3456 extent_key->offset, 0);
3457 btrfs_tree_lock(eb);
3458 level = btrfs_header_level(eb);
3461 btrfs_item_key_to_cpu(eb, &found_key, 0);
3463 btrfs_node_key_to_cpu(eb, &found_key, 0);
3465 btrfs_tree_unlock(eb);
3466 free_extent_buffer(eb);
3468 ret = find_root_for_ref(extent_root, path, &found_key,
3469 level, 0, &found_root,
3470 extent_key->objectid);
3476 * right here almost anything could happen to our key,
3477 * but that's ok. The cow below will either relocate it
3478 * or someone else will have relocated it. Either way,
3479 * it is in a different spot than it was before and
3483 trans = btrfs_start_transaction(found_root, 1);
3485 if (found_root == extent_root->fs_info->extent_root ||
3486 found_root == extent_root->fs_info->chunk_root ||
3487 found_root == extent_root->fs_info->dev_root) {
3489 mutex_lock(&extent_root->fs_info->alloc_mutex);
3492 path->lowest_level = level;
3494 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3496 path->lowest_level = 0;
3497 btrfs_release_path(found_root, path);
3499 if (found_root == found_root->fs_info->extent_root)
3500 btrfs_extent_post_op(trans, found_root);
3502 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3504 btrfs_end_transaction(trans, found_root);
3508 mutex_lock(&extent_root->fs_info->alloc_mutex);
3512 static int noinline del_extent_zero(struct btrfs_root *extent_root,
3513 struct btrfs_path *path,
3514 struct btrfs_key *extent_key)
3517 struct btrfs_trans_handle *trans;
3519 trans = btrfs_start_transaction(extent_root, 1);
3520 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3527 ret = btrfs_del_item(trans, extent_root, path);
3529 btrfs_end_transaction(trans, extent_root);
3533 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3534 struct btrfs_path *path,
3535 struct btrfs_key *extent_key)
3537 struct btrfs_key key;
3538 struct btrfs_key found_key;
3539 struct extent_buffer *leaf;
3540 u64 last_file_objectid = 0;
3541 u64 last_file_root = 0;
3542 u64 last_file_offset = (u64)-1;
3543 u64 last_extent = 0;
3548 if (extent_key->objectid == 0) {
3549 ret = del_extent_zero(extent_root, path, extent_key);
3552 key.objectid = extent_key->objectid;
3553 key.type = BTRFS_EXTENT_REF_KEY;
3557 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3563 leaf = path->nodes[0];
3564 nritems = btrfs_header_nritems(leaf);
3565 if (path->slots[0] == nritems) {
3566 ret = btrfs_next_leaf(extent_root, path);
3573 leaf = path->nodes[0];
3576 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3577 if (found_key.objectid != extent_key->objectid) {
3581 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
3585 key.offset = found_key.offset + 1;
3586 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3588 ret = relocate_one_reference(extent_root, path, extent_key,
3589 &last_file_objectid,
3591 &last_file_root, last_extent);
3594 last_extent = extent_key->objectid;
3598 btrfs_release_path(extent_root, path);
3602 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3605 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3606 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3608 num_devices = root->fs_info->fs_devices->num_devices;
3609 if (num_devices == 1) {
3610 stripped |= BTRFS_BLOCK_GROUP_DUP;
3611 stripped = flags & ~stripped;
3613 /* turn raid0 into single device chunks */
3614 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3617 /* turn mirroring into duplication */
3618 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3619 BTRFS_BLOCK_GROUP_RAID10))
3620 return stripped | BTRFS_BLOCK_GROUP_DUP;
3623 /* they already had raid on here, just return */
3624 if (flags & stripped)
3627 stripped |= BTRFS_BLOCK_GROUP_DUP;
3628 stripped = flags & ~stripped;
3630 /* switch duplicated blocks with raid1 */
3631 if (flags & BTRFS_BLOCK_GROUP_DUP)
3632 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3634 /* turn single device chunks into raid0 */
3635 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3640 int __alloc_chunk_for_shrink(struct btrfs_root *root,
3641 struct btrfs_block_group_cache *shrink_block_group,
3644 struct btrfs_trans_handle *trans;
3645 u64 new_alloc_flags;
3648 spin_lock(&shrink_block_group->lock);
3649 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
3650 spin_unlock(&shrink_block_group->lock);
3651 mutex_unlock(&root->fs_info->alloc_mutex);
3653 trans = btrfs_start_transaction(root, 1);
3654 mutex_lock(&root->fs_info->alloc_mutex);
3655 spin_lock(&shrink_block_group->lock);
3657 new_alloc_flags = update_block_group_flags(root,
3658 shrink_block_group->flags);
3659 if (new_alloc_flags != shrink_block_group->flags) {
3661 btrfs_block_group_used(&shrink_block_group->item);
3663 calc = shrink_block_group->key.offset;
3665 spin_unlock(&shrink_block_group->lock);
3667 do_chunk_alloc(trans, root->fs_info->extent_root,
3668 calc + 2 * 1024 * 1024, new_alloc_flags, force);
3670 mutex_unlock(&root->fs_info->alloc_mutex);
3671 btrfs_end_transaction(trans, root);
3672 mutex_lock(&root->fs_info->alloc_mutex);
3674 spin_unlock(&shrink_block_group->lock);
3678 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
3680 struct btrfs_trans_handle *trans;
3681 struct btrfs_root *tree_root = root->fs_info->tree_root;
3682 struct btrfs_path *path;
3685 u64 shrink_last_byte;
3686 struct btrfs_block_group_cache *shrink_block_group;
3687 struct btrfs_key key;
3688 struct btrfs_key found_key;
3689 struct extent_buffer *leaf;
3694 mutex_lock(&root->fs_info->alloc_mutex);
3695 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3697 BUG_ON(!shrink_block_group);
3699 shrink_last_byte = shrink_block_group->key.objectid +
3700 shrink_block_group->key.offset;
3702 shrink_block_group->space_info->total_bytes -=
3703 shrink_block_group->key.offset;
3704 path = btrfs_alloc_path();
3705 root = root->fs_info->extent_root;
3708 printk("btrfs relocating block group %llu flags %llu\n",
3709 (unsigned long long)shrink_start,
3710 (unsigned long long)shrink_block_group->flags);
3712 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
3716 shrink_block_group->ro = 1;
3720 key.objectid = shrink_start;
3723 cur_byte = key.objectid;
3725 mutex_unlock(&root->fs_info->alloc_mutex);
3727 btrfs_start_delalloc_inodes(root);
3728 btrfs_wait_ordered_extents(tree_root, 0);
3730 mutex_lock(&root->fs_info->alloc_mutex);
3732 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3736 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
3741 leaf = path->nodes[0];
3742 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3743 if (found_key.objectid + found_key.offset > shrink_start &&
3744 found_key.objectid < shrink_last_byte) {
3745 cur_byte = found_key.objectid;
3746 key.objectid = cur_byte;
3749 btrfs_release_path(root, path);
3752 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3757 leaf = path->nodes[0];
3758 nritems = btrfs_header_nritems(leaf);
3759 if (path->slots[0] >= nritems) {
3760 ret = btrfs_next_leaf(root, path);
3767 leaf = path->nodes[0];
3768 nritems = btrfs_header_nritems(leaf);
3771 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3773 if (found_key.objectid >= shrink_last_byte)
3776 if (progress && need_resched()) {
3777 memcpy(&key, &found_key, sizeof(key));
3779 btrfs_release_path(root, path);
3780 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3786 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3787 found_key.objectid + found_key.offset <= cur_byte) {
3788 memcpy(&key, &found_key, sizeof(key));
3795 cur_byte = found_key.objectid + found_key.offset;
3796 key.objectid = cur_byte;
3797 btrfs_release_path(root, path);
3798 ret = relocate_one_extent(root, path, &found_key);
3799 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
3802 btrfs_release_path(root, path);
3804 if (total_found > 0) {
3805 printk("btrfs relocate found %llu last extent was %llu\n",
3806 (unsigned long long)total_found,
3807 (unsigned long long)found_key.objectid);
3808 mutex_unlock(&root->fs_info->alloc_mutex);
3809 trans = btrfs_start_transaction(tree_root, 1);
3810 btrfs_commit_transaction(trans, tree_root);
3812 btrfs_clean_old_snapshots(tree_root);
3814 btrfs_start_delalloc_inodes(root);
3815 btrfs_wait_ordered_extents(tree_root, 0);
3817 trans = btrfs_start_transaction(tree_root, 1);
3818 btrfs_commit_transaction(trans, tree_root);
3819 mutex_lock(&root->fs_info->alloc_mutex);
3824 * we've freed all the extents, now remove the block
3825 * group item from the tree
3827 mutex_unlock(&root->fs_info->alloc_mutex);
3829 trans = btrfs_start_transaction(root, 1);
3831 mutex_lock(&root->fs_info->alloc_mutex);
3832 memcpy(&key, &shrink_block_group->key, sizeof(key));
3834 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3838 btrfs_end_transaction(trans, root);
3842 spin_lock(&root->fs_info->block_group_cache_lock);
3843 rb_erase(&shrink_block_group->cache_node,
3844 &root->fs_info->block_group_cache_tree);
3845 spin_unlock(&root->fs_info->block_group_cache_lock);
3847 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3850 btrfs_end_transaction(trans, root);
3854 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3855 kfree(shrink_block_group);
3858 btrfs_del_item(trans, root, path);
3859 btrfs_release_path(root, path);
3860 mutex_unlock(&root->fs_info->alloc_mutex);
3861 btrfs_commit_transaction(trans, root);
3863 mutex_lock(&root->fs_info->alloc_mutex);
3865 /* the code to unpin extents might set a few bits in the free
3866 * space cache for this range again
3869 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3872 btrfs_free_path(path);
3873 mutex_unlock(&root->fs_info->alloc_mutex);
3877 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3878 struct btrfs_key *key)
3881 struct btrfs_key found_key;
3882 struct extent_buffer *leaf;
3885 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3890 slot = path->slots[0];
3891 leaf = path->nodes[0];
3892 if (slot >= btrfs_header_nritems(leaf)) {
3893 ret = btrfs_next_leaf(root, path);
3900 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3902 if (found_key.objectid >= key->objectid &&
3903 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3914 int btrfs_read_block_groups(struct btrfs_root *root)
3916 struct btrfs_path *path;
3918 struct btrfs_block_group_cache *cache;
3919 struct btrfs_fs_info *info = root->fs_info;
3920 struct btrfs_space_info *space_info;
3921 struct btrfs_key key;
3922 struct btrfs_key found_key;
3923 struct extent_buffer *leaf;
3925 root = info->extent_root;
3928 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3929 path = btrfs_alloc_path();
3933 mutex_lock(&root->fs_info->alloc_mutex);
3935 ret = find_first_block_group(root, path, &key);
3943 leaf = path->nodes[0];
3944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3945 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3951 spin_lock_init(&cache->lock);
3952 INIT_LIST_HEAD(&cache->list);
3953 read_extent_buffer(leaf, &cache->item,
3954 btrfs_item_ptr_offset(leaf, path->slots[0]),
3955 sizeof(cache->item));
3956 memcpy(&cache->key, &found_key, sizeof(found_key));
3958 key.objectid = found_key.objectid + found_key.offset;
3959 btrfs_release_path(root, path);
3960 cache->flags = btrfs_block_group_flags(&cache->item);
3962 ret = update_space_info(info, cache->flags, found_key.offset,
3963 btrfs_block_group_used(&cache->item),
3966 cache->space_info = space_info;
3967 spin_lock(&space_info->lock);
3968 list_add(&cache->list, &space_info->block_groups);
3969 spin_unlock(&space_info->lock);
3971 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3975 btrfs_super_total_bytes(&info->super_copy))
3980 btrfs_free_path(path);
3981 mutex_unlock(&root->fs_info->alloc_mutex);
3985 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3986 struct btrfs_root *root, u64 bytes_used,
3987 u64 type, u64 chunk_objectid, u64 chunk_offset,
3991 struct btrfs_root *extent_root;
3992 struct btrfs_block_group_cache *cache;
3994 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
3995 extent_root = root->fs_info->extent_root;
3997 root->fs_info->last_trans_new_blockgroup = trans->transid;
3999 cache = kzalloc(sizeof(*cache), GFP_NOFS);
4003 cache->key.objectid = chunk_offset;
4004 cache->key.offset = size;
4005 spin_lock_init(&cache->lock);
4006 INIT_LIST_HEAD(&cache->list);
4007 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
4009 btrfs_set_block_group_used(&cache->item, bytes_used);
4010 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4011 cache->flags = type;
4012 btrfs_set_block_group_flags(&cache->item, type);
4014 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4015 &cache->space_info);
4017 spin_lock(&cache->space_info->lock);
4018 list_add(&cache->list, &cache->space_info->block_groups);
4019 spin_unlock(&cache->space_info->lock);
4021 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4024 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4025 sizeof(cache->item));
4028 finish_current_insert(trans, extent_root);
4029 ret = del_pending_extents(trans, extent_root);
4031 set_avail_alloc_bits(extent_root->fs_info, type);