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>
22 #include <linux/version.h>
28 #include "print-tree.h"
29 #include "transaction.h"
32 #include "ref-cache.h"
35 #define PENDING_EXTENT_INSERT 0
36 #define PENDING_EXTENT_DELETE 1
37 #define PENDING_BACKREF_UPDATE 2
39 struct pending_extent_op {
48 struct list_head list;
52 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
53 btrfs_root *extent_root, int all);
54 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
55 btrfs_root *extent_root, int all);
56 static struct btrfs_block_group_cache *
57 __btrfs_find_block_group(struct btrfs_root *root,
58 struct btrfs_block_group_cache *hint,
59 u64 search_start, int data, int owner);
60 static int pin_down_bytes(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root,
62 u64 bytenr, u64 num_bytes, int is_data);
63 static int update_block_group(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 u64 bytenr, u64 num_bytes, int alloc,
68 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
70 return (cache->flags & bits) == bits;
74 * this adds the block group to the fs_info rb tree for the block group
77 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
78 struct btrfs_block_group_cache *block_group)
81 struct rb_node *parent = NULL;
82 struct btrfs_block_group_cache *cache;
84 spin_lock(&info->block_group_cache_lock);
85 p = &info->block_group_cache_tree.rb_node;
89 cache = rb_entry(parent, struct btrfs_block_group_cache,
91 if (block_group->key.objectid < cache->key.objectid) {
93 } else if (block_group->key.objectid > cache->key.objectid) {
96 spin_unlock(&info->block_group_cache_lock);
101 rb_link_node(&block_group->cache_node, parent, p);
102 rb_insert_color(&block_group->cache_node,
103 &info->block_group_cache_tree);
104 spin_unlock(&info->block_group_cache_lock);
110 * This will return the block group at or after bytenr if contains is 0, else
111 * it will return the block group that contains the bytenr
113 static struct btrfs_block_group_cache *
114 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
117 struct btrfs_block_group_cache *cache, *ret = NULL;
121 spin_lock(&info->block_group_cache_lock);
122 n = info->block_group_cache_tree.rb_node;
125 cache = rb_entry(n, struct btrfs_block_group_cache,
127 end = cache->key.objectid + cache->key.offset - 1;
128 start = cache->key.objectid;
130 if (bytenr < start) {
131 if (!contains && (!ret || start < ret->key.objectid))
134 } else if (bytenr > start) {
135 if (contains && bytenr <= end) {
145 spin_unlock(&info->block_group_cache_lock);
151 * this is only called by cache_block_group, since we could have freed extents
152 * we need to check the pinned_extents for any extents that can't be used yet
153 * since their free space will be released as soon as the transaction commits.
155 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
156 struct btrfs_fs_info *info, u64 start, u64 end)
158 u64 extent_start, extent_end, size;
161 mutex_lock(&info->pinned_mutex);
162 while (start < end) {
163 ret = find_first_extent_bit(&info->pinned_extents, start,
164 &extent_start, &extent_end,
169 if (extent_start == start) {
170 start = extent_end + 1;
171 } else if (extent_start > start && extent_start < end) {
172 size = extent_start - start;
173 ret = btrfs_add_free_space(block_group, start,
176 start = extent_end + 1;
184 ret = btrfs_add_free_space(block_group, start, size);
187 mutex_unlock(&info->pinned_mutex);
192 static int remove_sb_from_cache(struct btrfs_root *root,
193 struct btrfs_block_group_cache *cache)
200 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
201 bytenr = btrfs_sb_offset(i);
202 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
203 cache->key.objectid, bytenr, 0,
204 &logical, &nr, &stripe_len);
207 btrfs_remove_free_space(cache, logical[nr],
215 static int cache_block_group(struct btrfs_root *root,
216 struct btrfs_block_group_cache *block_group)
218 struct btrfs_path *path;
220 struct btrfs_key key;
221 struct extent_buffer *leaf;
223 u64 last = block_group->key.objectid;
228 root = root->fs_info->extent_root;
230 if (block_group->cached)
233 path = btrfs_alloc_path();
239 * we get into deadlocks with paths held by callers of this function.
240 * since the alloc_mutex is protecting things right now, just
241 * skip the locking here
243 path->skip_locking = 1;
246 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
247 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
252 leaf = path->nodes[0];
253 slot = path->slots[0];
254 if (slot >= btrfs_header_nritems(leaf)) {
255 ret = btrfs_next_leaf(root, path);
263 btrfs_item_key_to_cpu(leaf, &key, slot);
264 if (key.objectid < block_group->key.objectid)
267 if (key.objectid >= block_group->key.objectid +
268 block_group->key.offset)
271 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
272 add_new_free_space(block_group, root->fs_info, last,
275 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 remove_sb_from_cache(root, block_group);
286 block_group->cached = 1;
289 btrfs_free_path(path);
294 * return the block group that starts at or after bytenr
296 static struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
300 struct btrfs_block_group_cache *cache;
302 cache = block_group_cache_tree_search(info, bytenr, 0);
308 * return the block group that contains teh given bytenr
310 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
314 struct btrfs_block_group_cache *cache;
316 cache = block_group_cache_tree_search(info, bytenr, 1);
321 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
324 struct list_head *head = &info->space_info;
325 struct list_head *cur;
326 struct btrfs_space_info *found;
327 list_for_each(cur, head) {
328 found = list_entry(cur, struct btrfs_space_info, list);
329 if (found->flags == flags)
335 static u64 div_factor(u64 num, int factor)
344 static struct btrfs_block_group_cache *
345 __btrfs_find_block_group(struct btrfs_root *root,
346 struct btrfs_block_group_cache *hint,
347 u64 search_start, int data, int owner)
349 struct btrfs_block_group_cache *cache;
350 struct btrfs_block_group_cache *found_group = NULL;
351 struct btrfs_fs_info *info = root->fs_info;
359 if (data & BTRFS_BLOCK_GROUP_METADATA)
363 struct btrfs_block_group_cache *shint;
364 shint = btrfs_lookup_first_block_group(info, search_start);
365 if (shint && block_group_bits(shint, data)) {
366 spin_lock(&shint->lock);
367 used = btrfs_block_group_used(&shint->item);
368 if (used + shint->pinned + shint->reserved <
369 div_factor(shint->key.offset, factor)) {
370 spin_unlock(&shint->lock);
373 spin_unlock(&shint->lock);
376 if (hint && block_group_bits(hint, data)) {
377 spin_lock(&hint->lock);
378 used = btrfs_block_group_used(&hint->item);
379 if (used + hint->pinned + hint->reserved <
380 div_factor(hint->key.offset, factor)) {
381 spin_unlock(&hint->lock);
384 spin_unlock(&hint->lock);
385 last = hint->key.objectid + hint->key.offset;
388 last = max(hint->key.objectid, search_start);
394 cache = btrfs_lookup_first_block_group(root->fs_info, last);
398 spin_lock(&cache->lock);
399 last = cache->key.objectid + cache->key.offset;
400 used = btrfs_block_group_used(&cache->item);
402 if (block_group_bits(cache, data)) {
403 free_check = div_factor(cache->key.offset, factor);
404 if (used + cache->pinned + cache->reserved <
407 spin_unlock(&cache->lock);
411 spin_unlock(&cache->lock);
419 if (!full_search && factor < 10) {
429 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
430 struct btrfs_block_group_cache
431 *hint, u64 search_start,
435 struct btrfs_block_group_cache *ret;
436 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
440 /* simple helper to search for an existing extent at a given offset */
441 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
444 struct btrfs_key key;
445 struct btrfs_path *path;
447 path = btrfs_alloc_path();
449 key.objectid = start;
451 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
452 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
454 btrfs_free_path(path);
459 * Back reference rules. Back refs have three main goals:
461 * 1) differentiate between all holders of references to an extent so that
462 * when a reference is dropped we can make sure it was a valid reference
463 * before freeing the extent.
465 * 2) Provide enough information to quickly find the holders of an extent
466 * if we notice a given block is corrupted or bad.
468 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
469 * maintenance. This is actually the same as #2, but with a slightly
470 * different use case.
472 * File extents can be referenced by:
474 * - multiple snapshots, subvolumes, or different generations in one subvol
475 * - different files inside a single subvolume
476 * - different offsets inside a file (bookend extents in file.c)
478 * The extent ref structure has fields for:
480 * - Objectid of the subvolume root
481 * - Generation number of the tree holding the reference
482 * - objectid of the file holding the reference
483 * - number of references holding by parent node (alway 1 for tree blocks)
485 * Btree leaf may hold multiple references to a file extent. In most cases,
486 * these references are from same file and the corresponding offsets inside
487 * the file are close together.
489 * When a file extent is allocated the fields are filled in:
490 * (root_key.objectid, trans->transid, inode objectid, 1)
492 * When a leaf is cow'd new references are added for every file extent found
493 * in the leaf. It looks similar to the create case, but trans->transid will
494 * be different when the block is cow'd.
496 * (root_key.objectid, trans->transid, inode objectid,
497 * number of references in the leaf)
499 * When a file extent is removed either during snapshot deletion or
500 * file truncation, we find the corresponding back reference and check
501 * the following fields:
503 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
506 * Btree extents can be referenced by:
508 * - Different subvolumes
509 * - Different generations of the same subvolume
511 * When a tree block is created, back references are inserted:
513 * (root->root_key.objectid, trans->transid, level, 1)
515 * When a tree block is cow'd, new back references are added for all the
516 * blocks it points to. If the tree block isn't in reference counted root,
517 * the old back references are removed. These new back references are of
518 * the form (trans->transid will have increased since creation):
520 * (root->root_key.objectid, trans->transid, level, 1)
522 * When a backref is in deleting, the following fields are checked:
524 * if backref was for a tree root:
525 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
527 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
529 * Back Reference Key composing:
531 * The key objectid corresponds to the first byte in the extent, the key
532 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
533 * byte of parent extent. If a extent is tree root, the key offset is set
534 * to the key objectid.
537 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
538 struct btrfs_root *root,
539 struct btrfs_path *path,
540 u64 bytenr, u64 parent,
541 u64 ref_root, u64 ref_generation,
542 u64 owner_objectid, int del)
544 struct btrfs_key key;
545 struct btrfs_extent_ref *ref;
546 struct extent_buffer *leaf;
550 key.objectid = bytenr;
551 key.type = BTRFS_EXTENT_REF_KEY;
554 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
562 leaf = path->nodes[0];
563 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
564 ref_objectid = btrfs_ref_objectid(leaf, ref);
565 if (btrfs_ref_root(leaf, ref) != ref_root ||
566 btrfs_ref_generation(leaf, ref) != ref_generation ||
567 (ref_objectid != owner_objectid &&
568 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
579 * updates all the backrefs that are pending on update_list for the
582 static int noinline update_backrefs(struct btrfs_trans_handle *trans,
583 struct btrfs_root *extent_root,
584 struct btrfs_path *path,
585 struct list_head *update_list)
587 struct btrfs_key key;
588 struct btrfs_extent_ref *ref;
589 struct btrfs_fs_info *info = extent_root->fs_info;
590 struct pending_extent_op *op;
591 struct extent_buffer *leaf;
593 struct list_head *cur = update_list->next;
595 u64 ref_root = extent_root->root_key.objectid;
597 op = list_entry(cur, struct pending_extent_op, list);
600 key.objectid = op->bytenr;
601 key.type = BTRFS_EXTENT_REF_KEY;
602 key.offset = op->orig_parent;
604 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
607 leaf = path->nodes[0];
610 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
612 ref_objectid = btrfs_ref_objectid(leaf, ref);
614 if (btrfs_ref_root(leaf, ref) != ref_root ||
615 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
616 (ref_objectid != op->level &&
617 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
618 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
619 "owner %u\n", op->bytenr, op->orig_parent,
620 ref_root, op->level);
621 btrfs_print_leaf(extent_root, leaf);
625 key.objectid = op->bytenr;
626 key.offset = op->parent;
627 key.type = BTRFS_EXTENT_REF_KEY;
628 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
630 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
631 btrfs_set_ref_generation(leaf, ref, op->generation);
635 list_del_init(&op->list);
636 unlock_extent(&info->extent_ins, op->bytenr,
637 op->bytenr + op->num_bytes - 1, GFP_NOFS);
640 if (cur == update_list) {
641 btrfs_mark_buffer_dirty(path->nodes[0]);
642 btrfs_release_path(extent_root, path);
646 op = list_entry(cur, struct pending_extent_op, list);
649 while (path->slots[0] < btrfs_header_nritems(leaf)) {
650 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
651 if (key.objectid == op->bytenr &&
652 key.type == BTRFS_EXTENT_REF_KEY)
657 btrfs_mark_buffer_dirty(path->nodes[0]);
658 btrfs_release_path(extent_root, path);
665 static int noinline insert_extents(struct btrfs_trans_handle *trans,
666 struct btrfs_root *extent_root,
667 struct btrfs_path *path,
668 struct list_head *insert_list, int nr)
670 struct btrfs_key *keys;
672 struct pending_extent_op *op;
673 struct extent_buffer *leaf;
674 struct list_head *cur = insert_list->next;
675 struct btrfs_fs_info *info = extent_root->fs_info;
676 u64 ref_root = extent_root->root_key.objectid;
677 int i = 0, last = 0, ret;
683 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
687 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
693 list_for_each_entry(op, insert_list, list) {
694 keys[i].objectid = op->bytenr;
695 keys[i].offset = op->num_bytes;
696 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
697 data_size[i] = sizeof(struct btrfs_extent_item);
700 keys[i].objectid = op->bytenr;
701 keys[i].offset = op->parent;
702 keys[i].type = BTRFS_EXTENT_REF_KEY;
703 data_size[i] = sizeof(struct btrfs_extent_ref);
707 op = list_entry(cur, struct pending_extent_op, list);
711 ret = btrfs_insert_some_items(trans, extent_root, path,
712 keys+i, data_size+i, total-i);
718 leaf = path->nodes[0];
719 for (c = 0; c < ret; c++) {
720 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
723 * if the first item we inserted was a backref, then
724 * the EXTENT_ITEM will be the odd c's, else it will
727 if ((ref_first && (c % 2)) ||
728 (!ref_first && !(c % 2))) {
729 struct btrfs_extent_item *itm;
731 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_item);
733 btrfs_set_extent_refs(path->nodes[0], itm, 1);
736 struct btrfs_extent_ref *ref;
738 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
739 struct btrfs_extent_ref);
740 btrfs_set_ref_root(leaf, ref, ref_root);
741 btrfs_set_ref_generation(leaf, ref,
743 btrfs_set_ref_objectid(leaf, ref, op->level);
744 btrfs_set_ref_num_refs(leaf, ref, 1);
749 * using del to see when its ok to free up the
750 * pending_extent_op. In the case where we insert the
751 * last item on the list in order to help do batching
752 * we need to not free the extent op until we actually
753 * insert the extent_item
756 unlock_extent(&info->extent_ins, op->bytenr,
757 op->bytenr + op->num_bytes - 1,
760 list_del_init(&op->list);
762 if (cur != insert_list)
764 struct pending_extent_op,
768 btrfs_mark_buffer_dirty(leaf);
769 btrfs_release_path(extent_root, path);
772 * Ok backref's and items usually go right next to eachother,
773 * but if we could only insert 1 item that means that we
774 * inserted on the end of a leaf, and we have no idea what may
775 * be on the next leaf so we just play it safe. In order to
776 * try and help this case we insert the last thing on our
777 * insert list so hopefully it will end up being the last
778 * thing on the leaf and everything else will be before it,
779 * which will let us insert a whole bunch of items at the same
782 if (ret == 1 && !last && (i + ret < total)) {
784 * last: where we will pick up the next time around
785 * i: our current key to insert, will be total - 1
786 * cur: the current op we are screwing with
791 cur = insert_list->prev;
792 op = list_entry(cur, struct pending_extent_op, list);
795 * ok we successfully inserted the last item on the
796 * list, lets reset everything
798 * i: our current key to insert, so where we left off
800 * last: done with this
801 * cur: the op we are messing with
803 * total: since we inserted the last key, we need to
804 * decrement total so we dont overflow
810 cur = insert_list->next;
811 op = list_entry(cur, struct pending_extent_op,
826 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_path *path,
829 u64 bytenr, u64 parent,
830 u64 ref_root, u64 ref_generation,
833 struct btrfs_key key;
834 struct extent_buffer *leaf;
835 struct btrfs_extent_ref *ref;
839 key.objectid = bytenr;
840 key.type = BTRFS_EXTENT_REF_KEY;
843 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 btrfs_set_ref_root(leaf, ref, ref_root);
849 btrfs_set_ref_generation(leaf, ref, ref_generation);
850 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
851 btrfs_set_ref_num_refs(leaf, ref, 1);
852 } else if (ret == -EEXIST) {
854 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
855 leaf = path->nodes[0];
856 ref = btrfs_item_ptr(leaf, path->slots[0],
857 struct btrfs_extent_ref);
858 if (btrfs_ref_root(leaf, ref) != ref_root ||
859 btrfs_ref_generation(leaf, ref) != ref_generation) {
865 num_refs = btrfs_ref_num_refs(leaf, ref);
866 BUG_ON(num_refs == 0);
867 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
869 existing_owner = btrfs_ref_objectid(leaf, ref);
870 if (existing_owner != owner_objectid &&
871 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
872 btrfs_set_ref_objectid(leaf, ref,
873 BTRFS_MULTIPLE_OBJECTIDS);
879 btrfs_mark_buffer_dirty(path->nodes[0]);
881 btrfs_release_path(root, path);
885 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root,
887 struct btrfs_path *path)
889 struct extent_buffer *leaf;
890 struct btrfs_extent_ref *ref;
894 leaf = path->nodes[0];
895 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
896 num_refs = btrfs_ref_num_refs(leaf, ref);
897 BUG_ON(num_refs == 0);
900 ret = btrfs_del_item(trans, root, path);
902 btrfs_set_ref_num_refs(leaf, ref, num_refs);
903 btrfs_mark_buffer_dirty(leaf);
905 btrfs_release_path(root, path);
909 #ifdef BIO_RW_DISCARD
910 static void btrfs_issue_discard(struct block_device *bdev,
913 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
914 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
916 blkdev_issue_discard(bdev, start >> 9, len >> 9);
921 static int noinline free_extents(struct btrfs_trans_handle *trans,
922 struct btrfs_root *extent_root,
923 struct list_head *del_list)
925 struct btrfs_fs_info *info = extent_root->fs_info;
926 struct btrfs_path *path;
927 struct btrfs_key key, found_key;
928 struct extent_buffer *leaf;
929 struct list_head *cur;
930 struct pending_extent_op *op;
931 struct btrfs_extent_item *ei;
932 int ret, num_to_del, extent_slot = 0, found_extent = 0;
936 path = btrfs_alloc_path();
942 /* search for the backref for the current ref we want to delete */
943 cur = del_list->next;
944 op = list_entry(cur, struct pending_extent_op, list);
945 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
947 extent_root->root_key.objectid,
948 op->orig_generation, op->level, 1);
950 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
951 "owner %u\n", op->bytenr,
952 extent_root->root_key.objectid, op->orig_generation,
954 btrfs_print_leaf(extent_root, path->nodes[0]);
959 extent_slot = path->slots[0];
964 * if we aren't the first item on the leaf we can move back one and see
965 * if our ref is right next to our extent item
967 if (likely(extent_slot)) {
969 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
971 if (found_key.objectid == op->bytenr &&
972 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
973 found_key.offset == op->num_bytes) {
980 * if we didn't find the extent we need to delete the backref and then
981 * search for the extent item key so we can update its ref count
984 key.objectid = op->bytenr;
985 key.type = BTRFS_EXTENT_ITEM_KEY;
986 key.offset = op->num_bytes;
988 ret = remove_extent_backref(trans, extent_root, path);
990 btrfs_release_path(extent_root, path);
991 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
993 extent_slot = path->slots[0];
996 /* this is where we update the ref count for the extent */
997 leaf = path->nodes[0];
998 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
999 refs = btrfs_extent_refs(leaf, ei);
1002 btrfs_set_extent_refs(leaf, ei, refs);
1004 btrfs_mark_buffer_dirty(leaf);
1007 * This extent needs deleting. The reason cur_slot is extent_slot +
1008 * num_to_del is because extent_slot points to the slot where the extent
1009 * is, and if the backref was not right next to the extent we will be
1010 * deleting at least 1 item, and will want to start searching at the
1011 * slot directly next to extent_slot. However if we did find the
1012 * backref next to the extent item them we will be deleting at least 2
1013 * items and will want to start searching directly after the ref slot
1016 struct list_head *pos, *n, *end;
1017 int cur_slot = extent_slot+num_to_del;
1021 path->slots[0] = extent_slot;
1022 bytes_freed = op->num_bytes;
1024 mutex_lock(&info->pinned_mutex);
1025 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1026 op->num_bytes, op->level >=
1027 BTRFS_FIRST_FREE_OBJECTID);
1028 mutex_unlock(&info->pinned_mutex);
1033 * we need to see if we can delete multiple things at once, so
1034 * start looping through the list of extents we are wanting to
1035 * delete and see if their extent/backref's are right next to
1036 * eachother and the extents only have 1 ref
1038 for (pos = cur->next; pos != del_list; pos = pos->next) {
1039 struct pending_extent_op *tmp;
1041 tmp = list_entry(pos, struct pending_extent_op, list);
1043 /* we only want to delete extent+ref at this stage */
1044 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1047 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1048 if (found_key.objectid != tmp->bytenr ||
1049 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1050 found_key.offset != tmp->num_bytes)
1053 /* check to make sure this extent only has one ref */
1054 ei = btrfs_item_ptr(leaf, cur_slot,
1055 struct btrfs_extent_item);
1056 if (btrfs_extent_refs(leaf, ei) != 1)
1059 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1060 if (found_key.objectid != tmp->bytenr ||
1061 found_key.type != BTRFS_EXTENT_REF_KEY ||
1062 found_key.offset != tmp->orig_parent)
1066 * the ref is right next to the extent, we can set the
1067 * ref count to 0 since we will delete them both now
1069 btrfs_set_extent_refs(leaf, ei, 0);
1071 /* pin down the bytes for this extent */
1072 mutex_lock(&info->pinned_mutex);
1073 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1074 tmp->num_bytes, tmp->level >=
1075 BTRFS_FIRST_FREE_OBJECTID);
1076 mutex_unlock(&info->pinned_mutex);
1080 * use the del field to tell if we need to go ahead and
1081 * free up the extent when we delete the item or not.
1084 bytes_freed += tmp->num_bytes;
1091 /* update the free space counters */
1092 spin_lock_irq(&info->delalloc_lock);
1093 super_used = btrfs_super_bytes_used(&info->super_copy);
1094 btrfs_set_super_bytes_used(&info->super_copy,
1095 super_used - bytes_freed);
1096 spin_unlock_irq(&info->delalloc_lock);
1098 root_used = btrfs_root_used(&extent_root->root_item);
1099 btrfs_set_root_used(&extent_root->root_item,
1100 root_used - bytes_freed);
1102 /* delete the items */
1103 ret = btrfs_del_items(trans, extent_root, path,
1104 path->slots[0], num_to_del);
1108 * loop through the extents we deleted and do the cleanup work
1111 for (pos = cur, n = pos->next; pos != end;
1112 pos = n, n = pos->next) {
1113 struct pending_extent_op *tmp;
1114 #ifdef BIO_RW_DISCARD
1116 struct btrfs_multi_bio *multi = NULL;
1118 tmp = list_entry(pos, struct pending_extent_op, list);
1121 * remember tmp->del tells us wether or not we pinned
1124 ret = update_block_group(trans, extent_root,
1125 tmp->bytenr, tmp->num_bytes, 0,
1129 #ifdef BIO_RW_DISCARD
1130 map_length = tmp->num_bytes;
1131 ret = btrfs_map_block(&info->mapping_tree, READ,
1132 tmp->bytenr, &map_length, &multi,
1135 struct btrfs_bio_stripe *stripe;
1138 stripe = multi->stripes;
1140 if (map_length > tmp->num_bytes)
1141 map_length = tmp->num_bytes;
1143 for (i = 0; i < multi->num_stripes;
1145 btrfs_issue_discard(stripe->dev->bdev,
1151 list_del_init(&tmp->list);
1152 unlock_extent(&info->extent_ins, tmp->bytenr,
1153 tmp->bytenr + tmp->num_bytes - 1,
1157 } else if (refs && found_extent) {
1159 * the ref and extent were right next to eachother, but the
1160 * extent still has a ref, so just free the backref and keep
1163 ret = remove_extent_backref(trans, extent_root, path);
1166 list_del_init(&op->list);
1167 unlock_extent(&info->extent_ins, op->bytenr,
1168 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1172 * the extent has multiple refs and the backref we were looking
1173 * for was not right next to it, so just unlock and go next,
1176 list_del_init(&op->list);
1177 unlock_extent(&info->extent_ins, op->bytenr,
1178 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1182 btrfs_release_path(extent_root, path);
1183 if (!list_empty(del_list))
1187 btrfs_free_path(path);
1191 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1192 struct btrfs_root *root, u64 bytenr,
1193 u64 orig_parent, u64 parent,
1194 u64 orig_root, u64 ref_root,
1195 u64 orig_generation, u64 ref_generation,
1199 struct btrfs_root *extent_root = root->fs_info->extent_root;
1200 struct btrfs_path *path;
1202 if (root == root->fs_info->extent_root) {
1203 struct pending_extent_op *extent_op;
1206 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1207 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1208 mutex_lock(&root->fs_info->extent_ins_mutex);
1209 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1210 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1212 ret = get_state_private(&root->fs_info->extent_ins,
1215 extent_op = (struct pending_extent_op *)
1216 (unsigned long)priv;
1217 BUG_ON(extent_op->parent != orig_parent);
1218 BUG_ON(extent_op->generation != orig_generation);
1220 extent_op->parent = parent;
1221 extent_op->generation = ref_generation;
1223 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1226 extent_op->type = PENDING_BACKREF_UPDATE;
1227 extent_op->bytenr = bytenr;
1228 extent_op->num_bytes = num_bytes;
1229 extent_op->parent = parent;
1230 extent_op->orig_parent = orig_parent;
1231 extent_op->generation = ref_generation;
1232 extent_op->orig_generation = orig_generation;
1233 extent_op->level = (int)owner_objectid;
1234 INIT_LIST_HEAD(&extent_op->list);
1237 set_extent_bits(&root->fs_info->extent_ins,
1238 bytenr, bytenr + num_bytes - 1,
1239 EXTENT_WRITEBACK, GFP_NOFS);
1240 set_state_private(&root->fs_info->extent_ins,
1241 bytenr, (unsigned long)extent_op);
1243 mutex_unlock(&root->fs_info->extent_ins_mutex);
1247 path = btrfs_alloc_path();
1250 ret = lookup_extent_backref(trans, extent_root, path,
1251 bytenr, orig_parent, orig_root,
1252 orig_generation, owner_objectid, 1);
1255 ret = remove_extent_backref(trans, extent_root, path);
1258 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1259 parent, ref_root, ref_generation,
1262 finish_current_insert(trans, extent_root, 0);
1263 del_pending_extents(trans, extent_root, 0);
1265 btrfs_free_path(path);
1269 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1270 struct btrfs_root *root, u64 bytenr,
1271 u64 orig_parent, u64 parent,
1272 u64 ref_root, u64 ref_generation,
1276 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1277 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1279 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1280 parent, ref_root, ref_root,
1281 ref_generation, ref_generation,
1286 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1287 struct btrfs_root *root, u64 bytenr,
1288 u64 orig_parent, u64 parent,
1289 u64 orig_root, u64 ref_root,
1290 u64 orig_generation, u64 ref_generation,
1293 struct btrfs_path *path;
1295 struct btrfs_key key;
1296 struct extent_buffer *l;
1297 struct btrfs_extent_item *item;
1300 path = btrfs_alloc_path();
1305 key.objectid = bytenr;
1306 key.type = BTRFS_EXTENT_ITEM_KEY;
1307 key.offset = (u64)-1;
1309 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1313 BUG_ON(ret == 0 || path->slots[0] == 0);
1318 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1319 if (key.objectid != bytenr) {
1320 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1321 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1324 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1326 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1327 refs = btrfs_extent_refs(l, item);
1328 btrfs_set_extent_refs(l, item, refs + 1);
1329 btrfs_mark_buffer_dirty(path->nodes[0]);
1331 btrfs_release_path(root->fs_info->extent_root, path);
1334 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1335 path, bytenr, parent,
1336 ref_root, ref_generation,
1339 finish_current_insert(trans, root->fs_info->extent_root, 0);
1340 del_pending_extents(trans, root->fs_info->extent_root, 0);
1342 btrfs_free_path(path);
1346 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1347 struct btrfs_root *root,
1348 u64 bytenr, u64 num_bytes, u64 parent,
1349 u64 ref_root, u64 ref_generation,
1353 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1354 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1356 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1357 0, ref_root, 0, ref_generation,
1362 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root)
1365 finish_current_insert(trans, root->fs_info->extent_root, 1);
1366 del_pending_extents(trans, root->fs_info->extent_root, 1);
1370 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1371 struct btrfs_root *root, u64 bytenr,
1372 u64 num_bytes, u32 *refs)
1374 struct btrfs_path *path;
1376 struct btrfs_key key;
1377 struct extent_buffer *l;
1378 struct btrfs_extent_item *item;
1380 WARN_ON(num_bytes < root->sectorsize);
1381 path = btrfs_alloc_path();
1383 key.objectid = bytenr;
1384 key.offset = num_bytes;
1385 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1386 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1391 btrfs_print_leaf(root, path->nodes[0]);
1392 printk("failed to find block number %Lu\n", bytenr);
1396 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1397 *refs = btrfs_extent_refs(l, item);
1399 btrfs_free_path(path);
1403 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1404 struct btrfs_root *root, u64 bytenr)
1406 struct btrfs_root *extent_root = root->fs_info->extent_root;
1407 struct btrfs_path *path;
1408 struct extent_buffer *leaf;
1409 struct btrfs_extent_ref *ref_item;
1410 struct btrfs_key key;
1411 struct btrfs_key found_key;
1417 key.objectid = bytenr;
1418 key.offset = (u64)-1;
1419 key.type = BTRFS_EXTENT_ITEM_KEY;
1421 path = btrfs_alloc_path();
1422 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1428 if (path->slots[0] == 0)
1432 leaf = path->nodes[0];
1433 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1435 if (found_key.objectid != bytenr ||
1436 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1439 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1441 leaf = path->nodes[0];
1442 nritems = btrfs_header_nritems(leaf);
1443 if (path->slots[0] >= nritems) {
1444 ret = btrfs_next_leaf(extent_root, path);
1451 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1452 if (found_key.objectid != bytenr)
1455 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1460 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1461 struct btrfs_extent_ref);
1462 ref_root = btrfs_ref_root(leaf, ref_item);
1463 if (ref_root != root->root_key.objectid &&
1464 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1468 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1477 btrfs_free_path(path);
1481 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1482 struct extent_buffer *buf, u32 nr_extents)
1484 struct btrfs_key key;
1485 struct btrfs_file_extent_item *fi;
1493 if (!root->ref_cows)
1496 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1498 root_gen = root->root_key.offset;
1501 root_gen = trans->transid - 1;
1504 level = btrfs_header_level(buf);
1505 nritems = btrfs_header_nritems(buf);
1508 struct btrfs_leaf_ref *ref;
1509 struct btrfs_extent_info *info;
1511 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1517 ref->root_gen = root_gen;
1518 ref->bytenr = buf->start;
1519 ref->owner = btrfs_header_owner(buf);
1520 ref->generation = btrfs_header_generation(buf);
1521 ref->nritems = nr_extents;
1522 info = ref->extents;
1524 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1526 btrfs_item_key_to_cpu(buf, &key, i);
1527 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1529 fi = btrfs_item_ptr(buf, i,
1530 struct btrfs_file_extent_item);
1531 if (btrfs_file_extent_type(buf, fi) ==
1532 BTRFS_FILE_EXTENT_INLINE)
1534 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1535 if (disk_bytenr == 0)
1538 info->bytenr = disk_bytenr;
1540 btrfs_file_extent_disk_num_bytes(buf, fi);
1541 info->objectid = key.objectid;
1542 info->offset = key.offset;
1546 ret = btrfs_add_leaf_ref(root, ref, shared);
1547 if (ret == -EEXIST && shared) {
1548 struct btrfs_leaf_ref *old;
1549 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1551 btrfs_remove_leaf_ref(root, old);
1552 btrfs_free_leaf_ref(root, old);
1553 ret = btrfs_add_leaf_ref(root, ref, shared);
1556 btrfs_free_leaf_ref(root, ref);
1562 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1563 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1570 u64 orig_generation;
1572 u32 nr_file_extents = 0;
1573 struct btrfs_key key;
1574 struct btrfs_file_extent_item *fi;
1579 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1580 u64, u64, u64, u64, u64, u64, u64, u64);
1582 ref_root = btrfs_header_owner(buf);
1583 ref_generation = btrfs_header_generation(buf);
1584 orig_root = btrfs_header_owner(orig_buf);
1585 orig_generation = btrfs_header_generation(orig_buf);
1587 nritems = btrfs_header_nritems(buf);
1588 level = btrfs_header_level(buf);
1590 if (root->ref_cows) {
1591 process_func = __btrfs_inc_extent_ref;
1594 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1597 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1599 process_func = __btrfs_update_extent_ref;
1602 for (i = 0; i < nritems; i++) {
1605 btrfs_item_key_to_cpu(buf, &key, i);
1606 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1608 fi = btrfs_item_ptr(buf, i,
1609 struct btrfs_file_extent_item);
1610 if (btrfs_file_extent_type(buf, fi) ==
1611 BTRFS_FILE_EXTENT_INLINE)
1613 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1619 ret = process_func(trans, root, bytenr,
1620 orig_buf->start, buf->start,
1621 orig_root, ref_root,
1622 orig_generation, ref_generation,
1631 bytenr = btrfs_node_blockptr(buf, i);
1632 ret = process_func(trans, root, bytenr,
1633 orig_buf->start, buf->start,
1634 orig_root, ref_root,
1635 orig_generation, ref_generation,
1647 *nr_extents = nr_file_extents;
1649 *nr_extents = nritems;
1657 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1658 struct btrfs_root *root, struct extent_buffer *orig_buf,
1659 struct extent_buffer *buf, int start_slot, int nr)
1666 u64 orig_generation;
1667 struct btrfs_key key;
1668 struct btrfs_file_extent_item *fi;
1674 BUG_ON(start_slot < 0);
1675 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1677 ref_root = btrfs_header_owner(buf);
1678 ref_generation = btrfs_header_generation(buf);
1679 orig_root = btrfs_header_owner(orig_buf);
1680 orig_generation = btrfs_header_generation(orig_buf);
1681 level = btrfs_header_level(buf);
1683 if (!root->ref_cows) {
1685 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1688 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1692 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1695 btrfs_item_key_to_cpu(buf, &key, slot);
1696 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1698 fi = btrfs_item_ptr(buf, slot,
1699 struct btrfs_file_extent_item);
1700 if (btrfs_file_extent_type(buf, fi) ==
1701 BTRFS_FILE_EXTENT_INLINE)
1703 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1706 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1707 orig_buf->start, buf->start,
1708 orig_root, ref_root,
1709 orig_generation, ref_generation,
1714 bytenr = btrfs_node_blockptr(buf, slot);
1715 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1716 orig_buf->start, buf->start,
1717 orig_root, ref_root,
1718 orig_generation, ref_generation,
1730 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1731 struct btrfs_root *root,
1732 struct btrfs_path *path,
1733 struct btrfs_block_group_cache *cache)
1737 struct btrfs_root *extent_root = root->fs_info->extent_root;
1739 struct extent_buffer *leaf;
1741 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1746 leaf = path->nodes[0];
1747 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1748 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1749 btrfs_mark_buffer_dirty(leaf);
1750 btrfs_release_path(extent_root, path);
1752 finish_current_insert(trans, extent_root, 0);
1753 pending_ret = del_pending_extents(trans, extent_root, 0);
1762 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1763 struct btrfs_root *root)
1765 struct btrfs_block_group_cache *cache, *entry;
1769 struct btrfs_path *path;
1772 path = btrfs_alloc_path();
1778 spin_lock(&root->fs_info->block_group_cache_lock);
1779 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1780 n; n = rb_next(n)) {
1781 entry = rb_entry(n, struct btrfs_block_group_cache,
1788 spin_unlock(&root->fs_info->block_group_cache_lock);
1794 last += cache->key.offset;
1796 err = write_one_cache_group(trans, root,
1799 * if we fail to write the cache group, we want
1800 * to keep it marked dirty in hopes that a later
1808 btrfs_free_path(path);
1812 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1813 u64 total_bytes, u64 bytes_used,
1814 struct btrfs_space_info **space_info)
1816 struct btrfs_space_info *found;
1818 found = __find_space_info(info, flags);
1820 spin_lock(&found->lock);
1821 found->total_bytes += total_bytes;
1822 found->bytes_used += bytes_used;
1824 spin_unlock(&found->lock);
1825 *space_info = found;
1828 found = kzalloc(sizeof(*found), GFP_NOFS);
1832 list_add(&found->list, &info->space_info);
1833 INIT_LIST_HEAD(&found->block_groups);
1834 init_rwsem(&found->groups_sem);
1835 spin_lock_init(&found->lock);
1836 found->flags = flags;
1837 found->total_bytes = total_bytes;
1838 found->bytes_used = bytes_used;
1839 found->bytes_pinned = 0;
1840 found->bytes_reserved = 0;
1841 found->bytes_readonly = 0;
1843 found->force_alloc = 0;
1844 *space_info = found;
1848 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1850 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1851 BTRFS_BLOCK_GROUP_RAID1 |
1852 BTRFS_BLOCK_GROUP_RAID10 |
1853 BTRFS_BLOCK_GROUP_DUP);
1855 if (flags & BTRFS_BLOCK_GROUP_DATA)
1856 fs_info->avail_data_alloc_bits |= extra_flags;
1857 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1858 fs_info->avail_metadata_alloc_bits |= extra_flags;
1859 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1860 fs_info->avail_system_alloc_bits |= extra_flags;
1864 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1866 spin_lock(&cache->space_info->lock);
1867 spin_lock(&cache->lock);
1869 cache->space_info->bytes_readonly += cache->key.offset -
1870 btrfs_block_group_used(&cache->item);
1873 spin_unlock(&cache->lock);
1874 spin_unlock(&cache->space_info->lock);
1877 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1879 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1881 if (num_devices == 1)
1882 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1883 if (num_devices < 4)
1884 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1886 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1887 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1888 BTRFS_BLOCK_GROUP_RAID10))) {
1889 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1892 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1893 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1894 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1897 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1898 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1899 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1900 (flags & BTRFS_BLOCK_GROUP_DUP)))
1901 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1905 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1906 struct btrfs_root *extent_root, u64 alloc_bytes,
1907 u64 flags, int force)
1909 struct btrfs_space_info *space_info;
1913 mutex_lock(&extent_root->fs_info->chunk_mutex);
1915 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1917 space_info = __find_space_info(extent_root->fs_info, flags);
1919 ret = update_space_info(extent_root->fs_info, flags,
1923 BUG_ON(!space_info);
1925 spin_lock(&space_info->lock);
1926 if (space_info->force_alloc) {
1928 space_info->force_alloc = 0;
1930 if (space_info->full) {
1931 spin_unlock(&space_info->lock);
1935 thresh = space_info->total_bytes - space_info->bytes_readonly;
1936 thresh = div_factor(thresh, 6);
1938 (space_info->bytes_used + space_info->bytes_pinned +
1939 space_info->bytes_reserved + alloc_bytes) < thresh) {
1940 spin_unlock(&space_info->lock);
1943 spin_unlock(&space_info->lock);
1945 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1947 printk("space info full %Lu\n", flags);
1948 space_info->full = 1;
1951 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1955 static int update_block_group(struct btrfs_trans_handle *trans,
1956 struct btrfs_root *root,
1957 u64 bytenr, u64 num_bytes, int alloc,
1960 struct btrfs_block_group_cache *cache;
1961 struct btrfs_fs_info *info = root->fs_info;
1962 u64 total = num_bytes;
1967 cache = btrfs_lookup_block_group(info, bytenr);
1970 byte_in_group = bytenr - cache->key.objectid;
1971 WARN_ON(byte_in_group > cache->key.offset);
1973 spin_lock(&cache->space_info->lock);
1974 spin_lock(&cache->lock);
1976 old_val = btrfs_block_group_used(&cache->item);
1977 num_bytes = min(total, cache->key.offset - byte_in_group);
1979 old_val += num_bytes;
1980 cache->space_info->bytes_used += num_bytes;
1982 cache->space_info->bytes_readonly -= num_bytes;
1983 btrfs_set_block_group_used(&cache->item, old_val);
1984 spin_unlock(&cache->lock);
1985 spin_unlock(&cache->space_info->lock);
1987 old_val -= num_bytes;
1988 cache->space_info->bytes_used -= num_bytes;
1990 cache->space_info->bytes_readonly += num_bytes;
1991 btrfs_set_block_group_used(&cache->item, old_val);
1992 spin_unlock(&cache->lock);
1993 spin_unlock(&cache->space_info->lock);
1996 ret = btrfs_add_free_space(cache, bytenr,
2003 bytenr += num_bytes;
2008 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2010 struct btrfs_block_group_cache *cache;
2012 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2016 return cache->key.objectid;
2019 int btrfs_update_pinned_extents(struct btrfs_root *root,
2020 u64 bytenr, u64 num, int pin)
2023 struct btrfs_block_group_cache *cache;
2024 struct btrfs_fs_info *fs_info = root->fs_info;
2026 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2028 set_extent_dirty(&fs_info->pinned_extents,
2029 bytenr, bytenr + num - 1, GFP_NOFS);
2031 clear_extent_dirty(&fs_info->pinned_extents,
2032 bytenr, bytenr + num - 1, GFP_NOFS);
2035 cache = btrfs_lookup_block_group(fs_info, bytenr);
2037 len = min(num, cache->key.offset -
2038 (bytenr - cache->key.objectid));
2040 spin_lock(&cache->space_info->lock);
2041 spin_lock(&cache->lock);
2042 cache->pinned += len;
2043 cache->space_info->bytes_pinned += len;
2044 spin_unlock(&cache->lock);
2045 spin_unlock(&cache->space_info->lock);
2046 fs_info->total_pinned += len;
2048 spin_lock(&cache->space_info->lock);
2049 spin_lock(&cache->lock);
2050 cache->pinned -= len;
2051 cache->space_info->bytes_pinned -= len;
2052 spin_unlock(&cache->lock);
2053 spin_unlock(&cache->space_info->lock);
2054 fs_info->total_pinned -= len;
2056 btrfs_add_free_space(cache, bytenr, len);
2064 static int update_reserved_extents(struct btrfs_root *root,
2065 u64 bytenr, u64 num, int reserve)
2068 struct btrfs_block_group_cache *cache;
2069 struct btrfs_fs_info *fs_info = root->fs_info;
2072 cache = btrfs_lookup_block_group(fs_info, bytenr);
2074 len = min(num, cache->key.offset -
2075 (bytenr - cache->key.objectid));
2077 spin_lock(&cache->space_info->lock);
2078 spin_lock(&cache->lock);
2080 cache->reserved += len;
2081 cache->space_info->bytes_reserved += len;
2083 cache->reserved -= len;
2084 cache->space_info->bytes_reserved -= len;
2086 spin_unlock(&cache->lock);
2087 spin_unlock(&cache->space_info->lock);
2094 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2099 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2102 mutex_lock(&root->fs_info->pinned_mutex);
2104 ret = find_first_extent_bit(pinned_extents, last,
2105 &start, &end, EXTENT_DIRTY);
2108 set_extent_dirty(copy, start, end, GFP_NOFS);
2111 mutex_unlock(&root->fs_info->pinned_mutex);
2115 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2116 struct btrfs_root *root,
2117 struct extent_io_tree *unpin)
2123 mutex_lock(&root->fs_info->pinned_mutex);
2125 ret = find_first_extent_bit(unpin, 0, &start, &end,
2129 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2130 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2131 if (need_resched()) {
2132 mutex_unlock(&root->fs_info->pinned_mutex);
2134 mutex_lock(&root->fs_info->pinned_mutex);
2137 mutex_unlock(&root->fs_info->pinned_mutex);
2141 static int finish_current_insert(struct btrfs_trans_handle *trans,
2142 struct btrfs_root *extent_root, int all)
2149 struct btrfs_fs_info *info = extent_root->fs_info;
2150 struct btrfs_path *path;
2151 struct pending_extent_op *extent_op, *tmp;
2152 struct list_head insert_list, update_list;
2154 int num_inserts = 0, max_inserts;
2156 path = btrfs_alloc_path();
2157 INIT_LIST_HEAD(&insert_list);
2158 INIT_LIST_HEAD(&update_list);
2160 max_inserts = extent_root->leafsize /
2161 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2162 sizeof(struct btrfs_extent_ref) +
2163 sizeof(struct btrfs_extent_item));
2165 mutex_lock(&info->extent_ins_mutex);
2167 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2168 &end, EXTENT_WRITEBACK);
2170 if (skipped && all && !num_inserts) {
2175 mutex_unlock(&info->extent_ins_mutex);
2179 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2183 if (need_resched()) {
2184 mutex_unlock(&info->extent_ins_mutex);
2186 mutex_lock(&info->extent_ins_mutex);
2191 ret = get_state_private(&info->extent_ins, start, &priv);
2193 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2195 if (extent_op->type == PENDING_EXTENT_INSERT) {
2197 list_add_tail(&extent_op->list, &insert_list);
2199 if (num_inserts == max_inserts) {
2200 mutex_unlock(&info->extent_ins_mutex);
2203 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2204 list_add_tail(&extent_op->list, &update_list);
2212 * process the update list, clear the writeback bit for it, and if
2213 * somebody marked this thing for deletion then just unlock it and be
2214 * done, the free_extents will handle it
2216 mutex_lock(&info->extent_ins_mutex);
2217 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2218 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2219 extent_op->bytenr + extent_op->num_bytes - 1,
2220 EXTENT_WRITEBACK, GFP_NOFS);
2221 if (extent_op->del) {
2222 list_del_init(&extent_op->list);
2223 unlock_extent(&info->extent_ins, extent_op->bytenr,
2224 extent_op->bytenr + extent_op->num_bytes
2229 mutex_unlock(&info->extent_ins_mutex);
2232 * still have things left on the update list, go ahead an update
2235 if (!list_empty(&update_list)) {
2236 ret = update_backrefs(trans, extent_root, path, &update_list);
2241 * if no inserts need to be done, but we skipped some extents and we
2242 * need to make sure everything is cleaned then reset everything and
2243 * go back to the beginning
2245 if (!num_inserts && all && skipped) {
2248 INIT_LIST_HEAD(&update_list);
2249 INIT_LIST_HEAD(&insert_list);
2251 } else if (!num_inserts) {
2256 * process the insert extents list. Again if we are deleting this
2257 * extent, then just unlock it, pin down the bytes if need be, and be
2258 * done with it. Saves us from having to actually insert the extent
2259 * into the tree and then subsequently come along and delete it
2261 mutex_lock(&info->extent_ins_mutex);
2262 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2263 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2264 extent_op->bytenr + extent_op->num_bytes - 1,
2265 EXTENT_WRITEBACK, GFP_NOFS);
2266 if (extent_op->del) {
2267 list_del_init(&extent_op->list);
2268 unlock_extent(&info->extent_ins, extent_op->bytenr,
2269 extent_op->bytenr + extent_op->num_bytes
2272 mutex_lock(&extent_root->fs_info->pinned_mutex);
2273 ret = pin_down_bytes(trans, extent_root,
2275 extent_op->num_bytes, 0);
2276 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2278 ret = update_block_group(trans, extent_root,
2280 extent_op->num_bytes,
2287 mutex_unlock(&info->extent_ins_mutex);
2289 ret = insert_extents(trans, extent_root, path, &insert_list,
2294 * if we broke out of the loop in order to insert stuff because we hit
2295 * the maximum number of inserts at a time we can handle, then loop
2296 * back and pick up where we left off
2298 if (num_inserts == max_inserts) {
2299 INIT_LIST_HEAD(&insert_list);
2300 INIT_LIST_HEAD(&update_list);
2306 * again, if we need to make absolutely sure there are no more pending
2307 * extent operations left and we know that we skipped some, go back to
2308 * the beginning and do it all again
2310 if (all && skipped) {
2311 INIT_LIST_HEAD(&insert_list);
2312 INIT_LIST_HEAD(&update_list);
2319 btrfs_free_path(path);
2323 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2324 struct btrfs_root *root,
2325 u64 bytenr, u64 num_bytes, int is_data)
2328 struct extent_buffer *buf;
2333 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2337 /* we can reuse a block if it hasn't been written
2338 * and it is from this transaction. We can't
2339 * reuse anything from the tree log root because
2340 * it has tiny sub-transactions.
2342 if (btrfs_buffer_uptodate(buf, 0) &&
2343 btrfs_try_tree_lock(buf)) {
2344 u64 header_owner = btrfs_header_owner(buf);
2345 u64 header_transid = btrfs_header_generation(buf);
2346 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2347 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2348 header_transid == trans->transid &&
2349 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2350 clean_tree_block(NULL, root, buf);
2351 btrfs_tree_unlock(buf);
2352 free_extent_buffer(buf);
2355 btrfs_tree_unlock(buf);
2357 free_extent_buffer(buf);
2359 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2366 * remove an extent from the root, returns 0 on success
2368 static int __free_extent(struct btrfs_trans_handle *trans,
2369 struct btrfs_root *root,
2370 u64 bytenr, u64 num_bytes, u64 parent,
2371 u64 root_objectid, u64 ref_generation,
2372 u64 owner_objectid, int pin, int mark_free)
2374 struct btrfs_path *path;
2375 struct btrfs_key key;
2376 struct btrfs_fs_info *info = root->fs_info;
2377 struct btrfs_root *extent_root = info->extent_root;
2378 struct extent_buffer *leaf;
2380 int extent_slot = 0;
2381 int found_extent = 0;
2383 struct btrfs_extent_item *ei;
2386 key.objectid = bytenr;
2387 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2388 key.offset = num_bytes;
2389 path = btrfs_alloc_path();
2394 ret = lookup_extent_backref(trans, extent_root, path,
2395 bytenr, parent, root_objectid,
2396 ref_generation, owner_objectid, 1);
2398 struct btrfs_key found_key;
2399 extent_slot = path->slots[0];
2400 while(extent_slot > 0) {
2402 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2404 if (found_key.objectid != bytenr)
2406 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2407 found_key.offset == num_bytes) {
2411 if (path->slots[0] - extent_slot > 5)
2414 if (!found_extent) {
2415 ret = remove_extent_backref(trans, extent_root, path);
2417 btrfs_release_path(extent_root, path);
2418 ret = btrfs_search_slot(trans, extent_root,
2421 printk(KERN_ERR "umm, got %d back from search"
2422 ", was looking for %Lu\n", ret,
2424 btrfs_print_leaf(extent_root, path->nodes[0]);
2427 extent_slot = path->slots[0];
2430 btrfs_print_leaf(extent_root, path->nodes[0]);
2432 printk("Unable to find ref byte nr %Lu root %Lu "
2433 "gen %Lu owner %Lu\n", bytenr,
2434 root_objectid, ref_generation, owner_objectid);
2437 leaf = path->nodes[0];
2438 ei = btrfs_item_ptr(leaf, extent_slot,
2439 struct btrfs_extent_item);
2440 refs = btrfs_extent_refs(leaf, ei);
2443 btrfs_set_extent_refs(leaf, ei, refs);
2445 btrfs_mark_buffer_dirty(leaf);
2447 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2448 struct btrfs_extent_ref *ref;
2449 ref = btrfs_item_ptr(leaf, path->slots[0],
2450 struct btrfs_extent_ref);
2451 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2452 /* if the back ref and the extent are next to each other
2453 * they get deleted below in one shot
2455 path->slots[0] = extent_slot;
2457 } else if (found_extent) {
2458 /* otherwise delete the extent back ref */
2459 ret = remove_extent_backref(trans, extent_root, path);
2461 /* if refs are 0, we need to setup the path for deletion */
2463 btrfs_release_path(extent_root, path);
2464 ret = btrfs_search_slot(trans, extent_root, &key, path,
2473 #ifdef BIO_RW_DISCARD
2474 u64 map_length = num_bytes;
2475 struct btrfs_multi_bio *multi = NULL;
2479 mutex_lock(&root->fs_info->pinned_mutex);
2480 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2481 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2482 mutex_unlock(&root->fs_info->pinned_mutex);
2487 /* block accounting for super block */
2488 spin_lock_irq(&info->delalloc_lock);
2489 super_used = btrfs_super_bytes_used(&info->super_copy);
2490 btrfs_set_super_bytes_used(&info->super_copy,
2491 super_used - num_bytes);
2492 spin_unlock_irq(&info->delalloc_lock);
2494 /* block accounting for root item */
2495 root_used = btrfs_root_used(&root->root_item);
2496 btrfs_set_root_used(&root->root_item,
2497 root_used - num_bytes);
2498 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2501 btrfs_release_path(extent_root, path);
2502 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2506 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2507 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2511 #ifdef BIO_RW_DISCARD
2512 /* Tell the block device(s) that the sectors can be discarded */
2513 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2514 bytenr, &map_length, &multi, 0);
2516 struct btrfs_bio_stripe *stripe = multi->stripes;
2519 if (map_length > num_bytes)
2520 map_length = num_bytes;
2522 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2523 btrfs_issue_discard(stripe->dev->bdev,
2531 btrfs_free_path(path);
2532 finish_current_insert(trans, extent_root, 0);
2537 * find all the blocks marked as pending in the radix tree and remove
2538 * them from the extent map
2540 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2541 btrfs_root *extent_root, int all)
2549 int nr = 0, skipped = 0;
2550 struct extent_io_tree *pending_del;
2551 struct extent_io_tree *extent_ins;
2552 struct pending_extent_op *extent_op;
2553 struct btrfs_fs_info *info = extent_root->fs_info;
2554 struct list_head delete_list;
2556 INIT_LIST_HEAD(&delete_list);
2557 extent_ins = &extent_root->fs_info->extent_ins;
2558 pending_del = &extent_root->fs_info->pending_del;
2561 mutex_lock(&info->extent_ins_mutex);
2563 ret = find_first_extent_bit(pending_del, search, &start, &end,
2566 if (all && skipped && !nr) {
2570 mutex_unlock(&info->extent_ins_mutex);
2574 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2579 if (need_resched()) {
2580 mutex_unlock(&info->extent_ins_mutex);
2582 mutex_lock(&info->extent_ins_mutex);
2589 ret = get_state_private(pending_del, start, &priv);
2591 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2593 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2595 if (!test_range_bit(extent_ins, start, end,
2596 EXTENT_WRITEBACK, 0)) {
2597 list_add_tail(&extent_op->list, &delete_list);
2602 ret = get_state_private(&info->extent_ins, start,
2605 extent_op = (struct pending_extent_op *)
2606 (unsigned long)priv;
2608 clear_extent_bits(&info->extent_ins, start, end,
2609 EXTENT_WRITEBACK, GFP_NOFS);
2611 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2612 list_add_tail(&extent_op->list, &delete_list);
2618 mutex_lock(&extent_root->fs_info->pinned_mutex);
2619 ret = pin_down_bytes(trans, extent_root, start,
2620 end + 1 - start, 0);
2621 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2623 ret = update_block_group(trans, extent_root, start,
2624 end + 1 - start, 0, ret > 0);
2626 unlock_extent(extent_ins, start, end, GFP_NOFS);
2635 if (need_resched()) {
2636 mutex_unlock(&info->extent_ins_mutex);
2638 mutex_lock(&info->extent_ins_mutex);
2643 ret = free_extents(trans, extent_root, &delete_list);
2647 if (all && skipped) {
2648 INIT_LIST_HEAD(&delete_list);
2658 * remove an extent from the root, returns 0 on success
2660 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2661 struct btrfs_root *root,
2662 u64 bytenr, u64 num_bytes, u64 parent,
2663 u64 root_objectid, u64 ref_generation,
2664 u64 owner_objectid, int pin)
2666 struct btrfs_root *extent_root = root->fs_info->extent_root;
2670 WARN_ON(num_bytes < root->sectorsize);
2671 if (root == extent_root) {
2672 struct pending_extent_op *extent_op = NULL;
2674 mutex_lock(&root->fs_info->extent_ins_mutex);
2675 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2676 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2678 ret = get_state_private(&root->fs_info->extent_ins,
2681 extent_op = (struct pending_extent_op *)
2682 (unsigned long)priv;
2685 if (extent_op->type == PENDING_EXTENT_INSERT) {
2686 mutex_unlock(&root->fs_info->extent_ins_mutex);
2692 ref_generation = extent_op->orig_generation;
2693 parent = extent_op->orig_parent;
2696 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2699 extent_op->type = PENDING_EXTENT_DELETE;
2700 extent_op->bytenr = bytenr;
2701 extent_op->num_bytes = num_bytes;
2702 extent_op->parent = parent;
2703 extent_op->orig_parent = parent;
2704 extent_op->generation = ref_generation;
2705 extent_op->orig_generation = ref_generation;
2706 extent_op->level = (int)owner_objectid;
2707 INIT_LIST_HEAD(&extent_op->list);
2710 set_extent_bits(&root->fs_info->pending_del,
2711 bytenr, bytenr + num_bytes - 1,
2712 EXTENT_WRITEBACK, GFP_NOFS);
2713 set_state_private(&root->fs_info->pending_del,
2714 bytenr, (unsigned long)extent_op);
2715 mutex_unlock(&root->fs_info->extent_ins_mutex);
2718 /* if metadata always pin */
2719 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2720 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2721 struct btrfs_block_group_cache *cache;
2723 /* btrfs_free_reserved_extent */
2724 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2726 btrfs_add_free_space(cache, bytenr, num_bytes);
2727 update_reserved_extents(root, bytenr, num_bytes, 0);
2733 /* if data pin when any transaction has committed this */
2734 if (ref_generation != trans->transid)
2737 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2738 root_objectid, ref_generation,
2739 owner_objectid, pin, pin == 0);
2741 finish_current_insert(trans, root->fs_info->extent_root, 0);
2742 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2743 return ret ? ret : pending_ret;
2746 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root,
2748 u64 bytenr, u64 num_bytes, u64 parent,
2749 u64 root_objectid, u64 ref_generation,
2750 u64 owner_objectid, int pin)
2754 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2755 root_objectid, ref_generation,
2756 owner_objectid, pin);
2760 static u64 stripe_align(struct btrfs_root *root, u64 val)
2762 u64 mask = ((u64)root->stripesize - 1);
2763 u64 ret = (val + mask) & ~mask;
2768 * walks the btree of allocated extents and find a hole of a given size.
2769 * The key ins is changed to record the hole:
2770 * ins->objectid == block start
2771 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2772 * ins->offset == number of blocks
2773 * Any available blocks before search_start are skipped.
2775 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2776 struct btrfs_root *orig_root,
2777 u64 num_bytes, u64 empty_size,
2778 u64 search_start, u64 search_end,
2779 u64 hint_byte, struct btrfs_key *ins,
2780 u64 exclude_start, u64 exclude_nr,
2784 struct btrfs_root * root = orig_root->fs_info->extent_root;
2785 u64 total_needed = num_bytes;
2786 u64 *last_ptr = NULL;
2787 u64 last_wanted = 0;
2788 struct btrfs_block_group_cache *block_group = NULL;
2789 int chunk_alloc_done = 0;
2790 int empty_cluster = 2 * 1024 * 1024;
2791 int allowed_chunk_alloc = 0;
2792 struct list_head *head = NULL, *cur = NULL;
2795 struct btrfs_space_info *space_info;
2797 WARN_ON(num_bytes < root->sectorsize);
2798 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2802 if (orig_root->ref_cows || empty_size)
2803 allowed_chunk_alloc = 1;
2805 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2806 last_ptr = &root->fs_info->last_alloc;
2807 empty_cluster = 64 * 1024;
2810 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2811 last_ptr = &root->fs_info->last_data_alloc;
2815 hint_byte = *last_ptr;
2816 last_wanted = *last_ptr;
2818 empty_size += empty_cluster;
2822 search_start = max(search_start, first_logical_byte(root, 0));
2823 search_start = max(search_start, hint_byte);
2825 if (last_wanted && search_start != last_wanted) {
2827 empty_size += empty_cluster;
2830 total_needed += empty_size;
2831 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2833 block_group = btrfs_lookup_first_block_group(root->fs_info,
2835 space_info = __find_space_info(root->fs_info, data);
2837 down_read(&space_info->groups_sem);
2839 struct btrfs_free_space *free_space;
2841 * the only way this happens if our hint points to a block
2842 * group thats not of the proper type, while looping this
2843 * should never happen
2849 goto new_group_no_lock;
2851 if (unlikely(!block_group->cached)) {
2852 mutex_lock(&block_group->cache_mutex);
2853 ret = cache_block_group(root, block_group);
2854 mutex_unlock(&block_group->cache_mutex);
2859 mutex_lock(&block_group->alloc_mutex);
2860 if (unlikely(!block_group_bits(block_group, data)))
2863 if (unlikely(block_group->ro))
2866 free_space = btrfs_find_free_space(block_group, search_start,
2869 u64 start = block_group->key.objectid;
2870 u64 end = block_group->key.objectid +
2871 block_group->key.offset;
2873 search_start = stripe_align(root, free_space->offset);
2875 /* move on to the next group */
2876 if (search_start + num_bytes >= search_end)
2879 /* move on to the next group */
2880 if (search_start + num_bytes > end)
2883 if (last_wanted && search_start != last_wanted) {
2884 total_needed += empty_cluster;
2885 empty_size += empty_cluster;
2888 * if search_start is still in this block group
2889 * then we just re-search this block group
2891 if (search_start >= start &&
2892 search_start < end) {
2893 mutex_unlock(&block_group->alloc_mutex);
2897 /* else we go to the next block group */
2901 if (exclude_nr > 0 &&
2902 (search_start + num_bytes > exclude_start &&
2903 search_start < exclude_start + exclude_nr)) {
2904 search_start = exclude_start + exclude_nr;
2906 * if search_start is still in this block group
2907 * then we just re-search this block group
2909 if (search_start >= start &&
2910 search_start < end) {
2911 mutex_unlock(&block_group->alloc_mutex);
2916 /* else we go to the next block group */
2920 ins->objectid = search_start;
2921 ins->offset = num_bytes;
2923 btrfs_remove_free_space_lock(block_group, search_start,
2925 /* we are all good, lets return */
2926 mutex_unlock(&block_group->alloc_mutex);
2930 mutex_unlock(&block_group->alloc_mutex);
2932 /* don't try to compare new allocations against the
2933 * last allocation any more
2938 * Here's how this works.
2939 * loop == 0: we were searching a block group via a hint
2940 * and didn't find anything, so we start at
2941 * the head of the block groups and keep searching
2942 * loop == 1: we're searching through all of the block groups
2943 * if we hit the head again we have searched
2944 * all of the block groups for this space and we
2945 * need to try and allocate, if we cant error out.
2946 * loop == 2: we allocated more space and are looping through
2947 * all of the block groups again.
2950 head = &space_info->block_groups;
2953 } else if (loop == 1 && cur == head) {
2956 /* at this point we give up on the empty_size
2957 * allocations and just try to allocate the min
2960 * The extra_loop field was set if an empty_size
2961 * allocation was attempted above, and if this
2962 * is try we need to try the loop again without
2963 * the additional empty_size.
2965 total_needed -= empty_size;
2967 keep_going = extra_loop;
2970 if (allowed_chunk_alloc && !chunk_alloc_done) {
2971 up_read(&space_info->groups_sem);
2972 ret = do_chunk_alloc(trans, root, num_bytes +
2973 2 * 1024 * 1024, data, 1);
2974 down_read(&space_info->groups_sem);
2977 head = &space_info->block_groups;
2979 * we've allocated a new chunk, keep
2983 chunk_alloc_done = 1;
2984 } else if (!allowed_chunk_alloc) {
2985 space_info->force_alloc = 1;
2994 } else if (cur == head) {
2998 block_group = list_entry(cur, struct btrfs_block_group_cache,
3000 search_start = block_group->key.objectid;
3004 /* we found what we needed */
3005 if (ins->objectid) {
3006 if (!(data & BTRFS_BLOCK_GROUP_DATA))
3007 trans->block_group = block_group;
3010 *last_ptr = ins->objectid + ins->offset;
3013 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
3014 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
3015 loop, allowed_chunk_alloc);
3019 up_read(&space_info->groups_sem);
3023 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3025 struct btrfs_block_group_cache *cache;
3026 struct list_head *l;
3028 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
3029 info->total_bytes - info->bytes_used - info->bytes_pinned -
3030 info->bytes_reserved, (info->full) ? "" : "not ");
3032 down_read(&info->groups_sem);
3033 list_for_each(l, &info->block_groups) {
3034 cache = list_entry(l, struct btrfs_block_group_cache, list);
3035 spin_lock(&cache->lock);
3036 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
3037 "%Lu pinned %Lu reserved\n",
3038 cache->key.objectid, cache->key.offset,
3039 btrfs_block_group_used(&cache->item),
3040 cache->pinned, cache->reserved);
3041 btrfs_dump_free_space(cache, bytes);
3042 spin_unlock(&cache->lock);
3044 up_read(&info->groups_sem);
3047 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3048 struct btrfs_root *root,
3049 u64 num_bytes, u64 min_alloc_size,
3050 u64 empty_size, u64 hint_byte,
3051 u64 search_end, struct btrfs_key *ins,
3055 u64 search_start = 0;
3057 struct btrfs_fs_info *info = root->fs_info;
3060 alloc_profile = info->avail_data_alloc_bits &
3061 info->data_alloc_profile;
3062 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3063 } else if (root == root->fs_info->chunk_root) {
3064 alloc_profile = info->avail_system_alloc_bits &
3065 info->system_alloc_profile;
3066 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3068 alloc_profile = info->avail_metadata_alloc_bits &
3069 info->metadata_alloc_profile;
3070 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3073 data = btrfs_reduce_alloc_profile(root, data);
3075 * the only place that sets empty_size is btrfs_realloc_node, which
3076 * is not called recursively on allocations
3078 if (empty_size || root->ref_cows) {
3079 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3080 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3082 BTRFS_BLOCK_GROUP_METADATA |
3083 (info->metadata_alloc_profile &
3084 info->avail_metadata_alloc_bits), 0);
3086 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3087 num_bytes + 2 * 1024 * 1024, data, 0);
3090 WARN_ON(num_bytes < root->sectorsize);
3091 ret = find_free_extent(trans, root, num_bytes, empty_size,
3092 search_start, search_end, hint_byte, ins,
3093 trans->alloc_exclude_start,
3094 trans->alloc_exclude_nr, data);
3096 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3097 num_bytes = num_bytes >> 1;
3098 num_bytes = num_bytes & ~(root->sectorsize - 1);
3099 num_bytes = max(num_bytes, min_alloc_size);
3100 do_chunk_alloc(trans, root->fs_info->extent_root,
3101 num_bytes, data, 1);
3105 struct btrfs_space_info *sinfo;
3107 sinfo = __find_space_info(root->fs_info, data);
3108 printk("allocation failed flags %Lu, wanted %Lu\n",
3110 dump_space_info(sinfo, num_bytes);
3117 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3119 struct btrfs_block_group_cache *cache;
3121 cache = btrfs_lookup_block_group(root->fs_info, start);
3123 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
3126 btrfs_add_free_space(cache, start, len);
3127 update_reserved_extents(root, start, len, 0);
3131 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root,
3133 u64 num_bytes, u64 min_alloc_size,
3134 u64 empty_size, u64 hint_byte,
3135 u64 search_end, struct btrfs_key *ins,
3139 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3140 empty_size, hint_byte, search_end, ins,
3142 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3146 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3147 struct btrfs_root *root, u64 parent,
3148 u64 root_objectid, u64 ref_generation,
3149 u64 owner, struct btrfs_key *ins)
3155 u64 num_bytes = ins->offset;
3157 struct btrfs_fs_info *info = root->fs_info;
3158 struct btrfs_root *extent_root = info->extent_root;
3159 struct btrfs_extent_item *extent_item;
3160 struct btrfs_extent_ref *ref;
3161 struct btrfs_path *path;
3162 struct btrfs_key keys[2];
3165 parent = ins->objectid;
3167 /* block accounting for super block */
3168 spin_lock_irq(&info->delalloc_lock);
3169 super_used = btrfs_super_bytes_used(&info->super_copy);
3170 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3171 spin_unlock_irq(&info->delalloc_lock);
3173 /* block accounting for root item */
3174 root_used = btrfs_root_used(&root->root_item);
3175 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3177 if (root == extent_root) {
3178 struct pending_extent_op *extent_op;
3180 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3183 extent_op->type = PENDING_EXTENT_INSERT;
3184 extent_op->bytenr = ins->objectid;
3185 extent_op->num_bytes = ins->offset;
3186 extent_op->parent = parent;
3187 extent_op->orig_parent = 0;
3188 extent_op->generation = ref_generation;
3189 extent_op->orig_generation = 0;
3190 extent_op->level = (int)owner;
3191 INIT_LIST_HEAD(&extent_op->list);
3194 mutex_lock(&root->fs_info->extent_ins_mutex);
3195 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3196 ins->objectid + ins->offset - 1,
3197 EXTENT_WRITEBACK, GFP_NOFS);
3198 set_state_private(&root->fs_info->extent_ins,
3199 ins->objectid, (unsigned long)extent_op);
3200 mutex_unlock(&root->fs_info->extent_ins_mutex);
3204 memcpy(&keys[0], ins, sizeof(*ins));
3205 keys[1].objectid = ins->objectid;
3206 keys[1].type = BTRFS_EXTENT_REF_KEY;
3207 keys[1].offset = parent;
3208 sizes[0] = sizeof(*extent_item);
3209 sizes[1] = sizeof(*ref);
3211 path = btrfs_alloc_path();
3214 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3218 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3219 struct btrfs_extent_item);
3220 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3221 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3222 struct btrfs_extent_ref);
3224 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3225 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3226 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3227 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3229 btrfs_mark_buffer_dirty(path->nodes[0]);
3231 trans->alloc_exclude_start = 0;
3232 trans->alloc_exclude_nr = 0;
3233 btrfs_free_path(path);
3234 finish_current_insert(trans, extent_root, 0);
3235 pending_ret = del_pending_extents(trans, extent_root, 0);
3245 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
3247 printk("update block group failed for %Lu %Lu\n",
3248 ins->objectid, ins->offset);
3255 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3256 struct btrfs_root *root, u64 parent,
3257 u64 root_objectid, u64 ref_generation,
3258 u64 owner, struct btrfs_key *ins)
3262 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3264 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3265 ref_generation, owner, ins);
3266 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3271 * this is used by the tree logging recovery code. It records that
3272 * an extent has been allocated and makes sure to clear the free
3273 * space cache bits as well
3275 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root, u64 parent,
3277 u64 root_objectid, u64 ref_generation,
3278 u64 owner, struct btrfs_key *ins)
3281 struct btrfs_block_group_cache *block_group;
3283 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3284 mutex_lock(&block_group->cache_mutex);
3285 cache_block_group(root, block_group);
3286 mutex_unlock(&block_group->cache_mutex);
3288 ret = btrfs_remove_free_space(block_group, ins->objectid,
3291 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3292 ref_generation, owner, ins);
3297 * finds a free extent and does all the dirty work required for allocation
3298 * returns the key for the extent through ins, and a tree buffer for
3299 * the first block of the extent through buf.
3301 * returns 0 if everything worked, non-zero otherwise.
3303 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3304 struct btrfs_root *root,
3305 u64 num_bytes, u64 parent, u64 min_alloc_size,
3306 u64 root_objectid, u64 ref_generation,
3307 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3308 u64 search_end, struct btrfs_key *ins, u64 data)
3312 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3313 min_alloc_size, empty_size, hint_byte,
3314 search_end, ins, data);
3316 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3317 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3318 root_objectid, ref_generation,
3319 owner_objectid, ins);
3323 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3328 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *root,
3330 u64 bytenr, u32 blocksize)
3332 struct extent_buffer *buf;
3334 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3336 return ERR_PTR(-ENOMEM);
3337 btrfs_set_header_generation(buf, trans->transid);
3338 btrfs_tree_lock(buf);
3339 clean_tree_block(trans, root, buf);
3340 btrfs_set_buffer_uptodate(buf);
3341 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3342 set_extent_dirty(&root->dirty_log_pages, buf->start,
3343 buf->start + buf->len - 1, GFP_NOFS);
3345 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3346 buf->start + buf->len - 1, GFP_NOFS);
3348 trans->blocks_used++;
3353 * helper function to allocate a block for a given tree
3354 * returns the tree buffer or NULL.
3356 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3357 struct btrfs_root *root,
3358 u32 blocksize, u64 parent,
3365 struct btrfs_key ins;
3367 struct extent_buffer *buf;
3369 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3370 root_objectid, ref_generation, level,
3371 empty_size, hint, (u64)-1, &ins, 0);
3374 return ERR_PTR(ret);
3377 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3381 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3382 struct btrfs_root *root, struct extent_buffer *leaf)
3385 u64 leaf_generation;
3386 struct btrfs_key key;
3387 struct btrfs_file_extent_item *fi;
3392 BUG_ON(!btrfs_is_leaf(leaf));
3393 nritems = btrfs_header_nritems(leaf);
3394 leaf_owner = btrfs_header_owner(leaf);
3395 leaf_generation = btrfs_header_generation(leaf);
3397 for (i = 0; i < nritems; i++) {
3401 btrfs_item_key_to_cpu(leaf, &key, i);
3402 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3404 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3405 if (btrfs_file_extent_type(leaf, fi) ==
3406 BTRFS_FILE_EXTENT_INLINE)
3409 * FIXME make sure to insert a trans record that
3410 * repeats the snapshot del on crash
3412 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3413 if (disk_bytenr == 0)
3416 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3417 btrfs_file_extent_disk_num_bytes(leaf, fi),
3418 leaf->start, leaf_owner, leaf_generation,
3422 atomic_inc(&root->fs_info->throttle_gen);
3423 wake_up(&root->fs_info->transaction_throttle);
3429 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3430 struct btrfs_root *root,
3431 struct btrfs_leaf_ref *ref)
3435 struct btrfs_extent_info *info = ref->extents;
3437 for (i = 0; i < ref->nritems; i++) {
3438 ret = __btrfs_free_extent(trans, root, info->bytenr,
3439 info->num_bytes, ref->bytenr,
3440 ref->owner, ref->generation,
3443 atomic_inc(&root->fs_info->throttle_gen);
3444 wake_up(&root->fs_info->transaction_throttle);
3454 static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3459 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3462 #if 0 // some debugging code in case we see problems here
3463 /* if the refs count is one, it won't get increased again. But
3464 * if the ref count is > 1, someone may be decreasing it at
3465 * the same time we are.
3468 struct extent_buffer *eb = NULL;
3469 eb = btrfs_find_create_tree_block(root, start, len);
3471 btrfs_tree_lock(eb);
3473 mutex_lock(&root->fs_info->alloc_mutex);
3474 ret = lookup_extent_ref(NULL, root, start, len, refs);
3476 mutex_unlock(&root->fs_info->alloc_mutex);
3479 btrfs_tree_unlock(eb);
3480 free_extent_buffer(eb);
3483 printk("block %llu went down to one during drop_snap\n",
3484 (unsigned long long)start);
3495 * helper function for drop_snapshot, this walks down the tree dropping ref
3496 * counts as it goes.
3498 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3499 struct btrfs_root *root,
3500 struct btrfs_path *path, int *level)
3506 struct extent_buffer *next;
3507 struct extent_buffer *cur;
3508 struct extent_buffer *parent;
3509 struct btrfs_leaf_ref *ref;
3514 WARN_ON(*level < 0);
3515 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3516 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3517 path->nodes[*level]->len, &refs);
3523 * walk down to the last node level and free all the leaves
3525 while(*level >= 0) {
3526 WARN_ON(*level < 0);
3527 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3528 cur = path->nodes[*level];
3530 if (btrfs_header_level(cur) != *level)
3533 if (path->slots[*level] >=
3534 btrfs_header_nritems(cur))
3537 ret = btrfs_drop_leaf_ref(trans, root, cur);
3541 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3542 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3543 blocksize = btrfs_level_size(root, *level - 1);
3545 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3548 parent = path->nodes[*level];
3549 root_owner = btrfs_header_owner(parent);
3550 root_gen = btrfs_header_generation(parent);
3551 path->slots[*level]++;
3553 ret = __btrfs_free_extent(trans, root, bytenr,
3554 blocksize, parent->start,
3555 root_owner, root_gen,
3559 atomic_inc(&root->fs_info->throttle_gen);
3560 wake_up(&root->fs_info->transaction_throttle);
3566 * at this point, we have a single ref, and since the
3567 * only place referencing this extent is a dead root
3568 * the reference count should never go higher.
3569 * So, we don't need to check it again
3572 ref = btrfs_lookup_leaf_ref(root, bytenr);
3573 if (ref && ref->generation != ptr_gen) {
3574 btrfs_free_leaf_ref(root, ref);
3578 ret = cache_drop_leaf_ref(trans, root, ref);
3580 btrfs_remove_leaf_ref(root, ref);
3581 btrfs_free_leaf_ref(root, ref);
3585 if (printk_ratelimit()) {
3586 printk("leaf ref miss for bytenr %llu\n",
3587 (unsigned long long)bytenr);
3590 next = btrfs_find_tree_block(root, bytenr, blocksize);
3591 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3592 free_extent_buffer(next);
3594 next = read_tree_block(root, bytenr, blocksize,
3599 * this is a debugging check and can go away
3600 * the ref should never go all the way down to 1
3603 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3609 WARN_ON(*level <= 0);
3610 if (path->nodes[*level-1])
3611 free_extent_buffer(path->nodes[*level-1]);
3612 path->nodes[*level-1] = next;
3613 *level = btrfs_header_level(next);
3614 path->slots[*level] = 0;
3618 WARN_ON(*level < 0);
3619 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3621 if (path->nodes[*level] == root->node) {
3622 parent = path->nodes[*level];
3623 bytenr = path->nodes[*level]->start;
3625 parent = path->nodes[*level + 1];
3626 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3629 blocksize = btrfs_level_size(root, *level);
3630 root_owner = btrfs_header_owner(parent);
3631 root_gen = btrfs_header_generation(parent);
3633 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3634 parent->start, root_owner, root_gen,
3636 free_extent_buffer(path->nodes[*level]);
3637 path->nodes[*level] = NULL;
3646 * helper function for drop_subtree, this function is similar to
3647 * walk_down_tree. The main difference is that it checks reference
3648 * counts while tree blocks are locked.
3650 static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3651 struct btrfs_root *root,
3652 struct btrfs_path *path, int *level)
3654 struct extent_buffer *next;
3655 struct extent_buffer *cur;
3656 struct extent_buffer *parent;
3663 cur = path->nodes[*level];
3664 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3670 while (*level >= 0) {
3671 cur = path->nodes[*level];
3673 ret = btrfs_drop_leaf_ref(trans, root, cur);
3675 clean_tree_block(trans, root, cur);
3678 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3679 clean_tree_block(trans, root, cur);
3683 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3684 blocksize = btrfs_level_size(root, *level - 1);
3685 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3687 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3688 btrfs_tree_lock(next);
3690 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3694 parent = path->nodes[*level];
3695 ret = btrfs_free_extent(trans, root, bytenr,
3696 blocksize, parent->start,
3697 btrfs_header_owner(parent),
3698 btrfs_header_generation(parent),
3701 path->slots[*level]++;
3702 btrfs_tree_unlock(next);
3703 free_extent_buffer(next);
3707 *level = btrfs_header_level(next);
3708 path->nodes[*level] = next;
3709 path->slots[*level] = 0;
3710 path->locks[*level] = 1;
3714 parent = path->nodes[*level + 1];
3715 bytenr = path->nodes[*level]->start;
3716 blocksize = path->nodes[*level]->len;
3718 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3719 parent->start, btrfs_header_owner(parent),
3720 btrfs_header_generation(parent), *level, 1);
3723 if (path->locks[*level]) {
3724 btrfs_tree_unlock(path->nodes[*level]);
3725 path->locks[*level] = 0;
3727 free_extent_buffer(path->nodes[*level]);
3728 path->nodes[*level] = NULL;
3735 * helper for dropping snapshots. This walks back up the tree in the path
3736 * to find the first node higher up where we haven't yet gone through
3739 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3740 struct btrfs_root *root,
3741 struct btrfs_path *path,
3742 int *level, int max_level)
3746 struct btrfs_root_item *root_item = &root->root_item;
3751 for (i = *level; i < max_level && path->nodes[i]; i++) {
3752 slot = path->slots[i];
3753 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3754 struct extent_buffer *node;
3755 struct btrfs_disk_key disk_key;
3756 node = path->nodes[i];
3759 WARN_ON(*level == 0);
3760 btrfs_node_key(node, &disk_key, path->slots[i]);
3761 memcpy(&root_item->drop_progress,
3762 &disk_key, sizeof(disk_key));
3763 root_item->drop_level = i;
3766 struct extent_buffer *parent;
3767 if (path->nodes[*level] == root->node)
3768 parent = path->nodes[*level];
3770 parent = path->nodes[*level + 1];
3772 root_owner = btrfs_header_owner(parent);
3773 root_gen = btrfs_header_generation(parent);
3775 clean_tree_block(trans, root, path->nodes[*level]);
3776 ret = btrfs_free_extent(trans, root,
3777 path->nodes[*level]->start,
3778 path->nodes[*level]->len,
3779 parent->start, root_owner,
3780 root_gen, *level, 1);
3782 if (path->locks[*level]) {
3783 btrfs_tree_unlock(path->nodes[*level]);
3784 path->locks[*level] = 0;
3786 free_extent_buffer(path->nodes[*level]);
3787 path->nodes[*level] = NULL;
3795 * drop the reference count on the tree rooted at 'snap'. This traverses
3796 * the tree freeing any blocks that have a ref count of zero after being
3799 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3805 struct btrfs_path *path;
3808 struct btrfs_root_item *root_item = &root->root_item;
3810 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3811 path = btrfs_alloc_path();
3814 level = btrfs_header_level(root->node);
3816 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3817 path->nodes[level] = root->node;
3818 extent_buffer_get(root->node);
3819 path->slots[level] = 0;
3821 struct btrfs_key key;
3822 struct btrfs_disk_key found_key;
3823 struct extent_buffer *node;
3825 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3826 level = root_item->drop_level;
3827 path->lowest_level = level;
3828 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3833 node = path->nodes[level];
3834 btrfs_node_key(node, &found_key, path->slots[level]);
3835 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3836 sizeof(found_key)));
3838 * unlock our path, this is safe because only this
3839 * function is allowed to delete this snapshot
3841 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3842 if (path->nodes[i] && path->locks[i]) {
3844 btrfs_tree_unlock(path->nodes[i]);
3849 wret = walk_down_tree(trans, root, path, &level);
3855 wret = walk_up_tree(trans, root, path, &level,
3861 if (trans->transaction->in_commit) {
3865 atomic_inc(&root->fs_info->throttle_gen);
3866 wake_up(&root->fs_info->transaction_throttle);
3868 for (i = 0; i <= orig_level; i++) {
3869 if (path->nodes[i]) {
3870 free_extent_buffer(path->nodes[i]);
3871 path->nodes[i] = NULL;
3875 btrfs_free_path(path);
3879 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3880 struct btrfs_root *root,
3881 struct extent_buffer *node,
3882 struct extent_buffer *parent)
3884 struct btrfs_path *path;
3890 path = btrfs_alloc_path();
3893 BUG_ON(!btrfs_tree_locked(parent));
3894 parent_level = btrfs_header_level(parent);
3895 extent_buffer_get(parent);
3896 path->nodes[parent_level] = parent;
3897 path->slots[parent_level] = btrfs_header_nritems(parent);
3899 BUG_ON(!btrfs_tree_locked(node));
3900 level = btrfs_header_level(node);
3901 extent_buffer_get(node);
3902 path->nodes[level] = node;
3903 path->slots[level] = 0;
3906 wret = walk_down_subtree(trans, root, path, &level);
3912 wret = walk_up_tree(trans, root, path, &level, parent_level);
3919 btrfs_free_path(path);
3923 static unsigned long calc_ra(unsigned long start, unsigned long last,
3926 return min(last, start + nr - 1);
3929 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3934 unsigned long first_index;
3935 unsigned long last_index;
3938 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3939 struct file_ra_state *ra;
3940 struct btrfs_ordered_extent *ordered;
3941 unsigned int total_read = 0;
3942 unsigned int total_dirty = 0;
3945 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3947 mutex_lock(&inode->i_mutex);
3948 first_index = start >> PAGE_CACHE_SHIFT;
3949 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3951 /* make sure the dirty trick played by the caller work */
3952 ret = invalidate_inode_pages2_range(inode->i_mapping,
3953 first_index, last_index);
3957 file_ra_state_init(ra, inode->i_mapping);
3959 for (i = first_index ; i <= last_index; i++) {
3960 if (total_read % ra->ra_pages == 0) {
3961 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3962 calc_ra(i, last_index, ra->ra_pages));
3966 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3968 page = grab_cache_page(inode->i_mapping, i);
3973 if (!PageUptodate(page)) {
3974 btrfs_readpage(NULL, page);
3976 if (!PageUptodate(page)) {
3978 page_cache_release(page);
3983 wait_on_page_writeback(page);
3985 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3986 page_end = page_start + PAGE_CACHE_SIZE - 1;
3987 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3989 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3991 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3993 page_cache_release(page);
3994 btrfs_start_ordered_extent(inode, ordered, 1);
3995 btrfs_put_ordered_extent(ordered);
3998 set_page_extent_mapped(page);
4000 btrfs_set_extent_delalloc(inode, page_start, page_end);
4001 if (i == first_index)
4002 set_extent_bits(io_tree, page_start, page_end,
4003 EXTENT_BOUNDARY, GFP_NOFS);
4005 set_page_dirty(page);
4008 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4010 page_cache_release(page);
4015 mutex_unlock(&inode->i_mutex);
4016 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
4020 static int noinline relocate_data_extent(struct inode *reloc_inode,
4021 struct btrfs_key *extent_key,
4024 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4025 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4026 struct extent_map *em;
4027 u64 start = extent_key->objectid - offset;
4028 u64 end = start + extent_key->offset - 1;
4030 em = alloc_extent_map(GFP_NOFS);
4031 BUG_ON(!em || IS_ERR(em));
4034 em->len = extent_key->offset;
4035 em->block_len = extent_key->offset;
4036 em->block_start = extent_key->objectid;
4037 em->bdev = root->fs_info->fs_devices->latest_bdev;
4038 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4040 /* setup extent map to cheat btrfs_readpage */
4041 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4044 spin_lock(&em_tree->lock);
4045 ret = add_extent_mapping(em_tree, em);
4046 spin_unlock(&em_tree->lock);
4047 if (ret != -EEXIST) {
4048 free_extent_map(em);
4051 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4053 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4055 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4058 struct btrfs_ref_path {
4060 u64 nodes[BTRFS_MAX_LEVEL];
4062 u64 root_generation;
4069 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4070 u64 new_nodes[BTRFS_MAX_LEVEL];
4073 struct disk_extent {
4084 static int is_cowonly_root(u64 root_objectid)
4086 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4087 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4088 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4089 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4090 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4095 static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4096 struct btrfs_root *extent_root,
4097 struct btrfs_ref_path *ref_path,
4100 struct extent_buffer *leaf;
4101 struct btrfs_path *path;
4102 struct btrfs_extent_ref *ref;
4103 struct btrfs_key key;
4104 struct btrfs_key found_key;
4110 path = btrfs_alloc_path();
4115 ref_path->lowest_level = -1;
4116 ref_path->current_level = -1;
4117 ref_path->shared_level = -1;
4121 level = ref_path->current_level - 1;
4122 while (level >= -1) {
4124 if (level < ref_path->lowest_level)
4128 bytenr = ref_path->nodes[level];
4130 bytenr = ref_path->extent_start;
4132 BUG_ON(bytenr == 0);
4134 parent = ref_path->nodes[level + 1];
4135 ref_path->nodes[level + 1] = 0;
4136 ref_path->current_level = level;
4137 BUG_ON(parent == 0);
4139 key.objectid = bytenr;
4140 key.offset = parent + 1;
4141 key.type = BTRFS_EXTENT_REF_KEY;
4143 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4148 leaf = path->nodes[0];
4149 nritems = btrfs_header_nritems(leaf);
4150 if (path->slots[0] >= nritems) {
4151 ret = btrfs_next_leaf(extent_root, path);
4156 leaf = path->nodes[0];
4159 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4160 if (found_key.objectid == bytenr &&
4161 found_key.type == BTRFS_EXTENT_REF_KEY) {
4162 if (level < ref_path->shared_level)
4163 ref_path->shared_level = level;
4168 btrfs_release_path(extent_root, path);
4171 /* reached lowest level */
4175 level = ref_path->current_level;
4176 while (level < BTRFS_MAX_LEVEL - 1) {
4179 bytenr = ref_path->nodes[level];
4181 bytenr = ref_path->extent_start;
4183 BUG_ON(bytenr == 0);
4185 key.objectid = bytenr;
4187 key.type = BTRFS_EXTENT_REF_KEY;
4189 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4193 leaf = path->nodes[0];
4194 nritems = btrfs_header_nritems(leaf);
4195 if (path->slots[0] >= nritems) {
4196 ret = btrfs_next_leaf(extent_root, path);
4200 /* the extent was freed by someone */
4201 if (ref_path->lowest_level == level)
4203 btrfs_release_path(extent_root, path);
4206 leaf = path->nodes[0];
4209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4210 if (found_key.objectid != bytenr ||
4211 found_key.type != BTRFS_EXTENT_REF_KEY) {
4212 /* the extent was freed by someone */
4213 if (ref_path->lowest_level == level) {
4217 btrfs_release_path(extent_root, path);
4221 ref = btrfs_item_ptr(leaf, path->slots[0],
4222 struct btrfs_extent_ref);
4223 ref_objectid = btrfs_ref_objectid(leaf, ref);
4224 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4226 level = (int)ref_objectid;
4227 BUG_ON(level >= BTRFS_MAX_LEVEL);
4228 ref_path->lowest_level = level;
4229 ref_path->current_level = level;
4230 ref_path->nodes[level] = bytenr;
4232 WARN_ON(ref_objectid != level);
4235 WARN_ON(level != -1);
4239 if (ref_path->lowest_level == level) {
4240 ref_path->owner_objectid = ref_objectid;
4241 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4245 * the block is tree root or the block isn't in reference
4248 if (found_key.objectid == found_key.offset ||
4249 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4250 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4251 ref_path->root_generation =
4252 btrfs_ref_generation(leaf, ref);
4254 /* special reference from the tree log */
4255 ref_path->nodes[0] = found_key.offset;
4256 ref_path->current_level = 0;
4263 BUG_ON(ref_path->nodes[level] != 0);
4264 ref_path->nodes[level] = found_key.offset;
4265 ref_path->current_level = level;
4268 * the reference was created in the running transaction,
4269 * no need to continue walking up.
4271 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4272 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4273 ref_path->root_generation =
4274 btrfs_ref_generation(leaf, ref);
4279 btrfs_release_path(extent_root, path);
4282 /* reached max tree level, but no tree root found. */
4285 btrfs_free_path(path);
4289 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4290 struct btrfs_root *extent_root,
4291 struct btrfs_ref_path *ref_path,
4294 memset(ref_path, 0, sizeof(*ref_path));
4295 ref_path->extent_start = extent_start;
4297 return __next_ref_path(trans, extent_root, ref_path, 1);
4300 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4301 struct btrfs_root *extent_root,
4302 struct btrfs_ref_path *ref_path)
4304 return __next_ref_path(trans, extent_root, ref_path, 0);
4307 static int noinline get_new_locations(struct inode *reloc_inode,
4308 struct btrfs_key *extent_key,
4309 u64 offset, int no_fragment,
4310 struct disk_extent **extents,
4313 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4314 struct btrfs_path *path;
4315 struct btrfs_file_extent_item *fi;
4316 struct extent_buffer *leaf;
4317 struct disk_extent *exts = *extents;
4318 struct btrfs_key found_key;
4323 int max = *nr_extents;
4326 WARN_ON(!no_fragment && *extents);
4329 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4334 path = btrfs_alloc_path();
4337 cur_pos = extent_key->objectid - offset;
4338 last_byte = extent_key->objectid + extent_key->offset;
4339 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4349 leaf = path->nodes[0];
4350 nritems = btrfs_header_nritems(leaf);
4351 if (path->slots[0] >= nritems) {
4352 ret = btrfs_next_leaf(root, path);
4357 leaf = path->nodes[0];
4360 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4361 if (found_key.offset != cur_pos ||
4362 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4363 found_key.objectid != reloc_inode->i_ino)
4366 fi = btrfs_item_ptr(leaf, path->slots[0],
4367 struct btrfs_file_extent_item);
4368 if (btrfs_file_extent_type(leaf, fi) !=
4369 BTRFS_FILE_EXTENT_REG ||
4370 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4374 struct disk_extent *old = exts;
4376 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4377 memcpy(exts, old, sizeof(*exts) * nr);
4378 if (old != *extents)
4382 exts[nr].disk_bytenr =
4383 btrfs_file_extent_disk_bytenr(leaf, fi);
4384 exts[nr].disk_num_bytes =
4385 btrfs_file_extent_disk_num_bytes(leaf, fi);
4386 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4387 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4388 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4389 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4390 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4391 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4393 BUG_ON(exts[nr].offset > 0);
4394 BUG_ON(exts[nr].compression || exts[nr].encryption);
4395 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4397 cur_pos += exts[nr].num_bytes;
4400 if (cur_pos + offset >= last_byte)
4410 WARN_ON(cur_pos + offset > last_byte);
4411 if (cur_pos + offset < last_byte) {
4417 btrfs_free_path(path);
4419 if (exts != *extents)
4428 static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4429 struct btrfs_root *root,
4430 struct btrfs_path *path,
4431 struct btrfs_key *extent_key,
4432 struct btrfs_key *leaf_key,
4433 struct btrfs_ref_path *ref_path,
4434 struct disk_extent *new_extents,
4437 struct extent_buffer *leaf;
4438 struct btrfs_file_extent_item *fi;
4439 struct inode *inode = NULL;
4440 struct btrfs_key key;
4448 int extent_locked = 0;
4452 memcpy(&key, leaf_key, sizeof(key));
4453 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4454 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4455 if (key.objectid < ref_path->owner_objectid ||
4456 (key.objectid == ref_path->owner_objectid &&
4457 key.type < BTRFS_EXTENT_DATA_KEY)) {
4458 key.objectid = ref_path->owner_objectid;
4459 key.type = BTRFS_EXTENT_DATA_KEY;
4465 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4469 leaf = path->nodes[0];
4470 nritems = btrfs_header_nritems(leaf);
4472 if (extent_locked && ret > 0) {
4474 * the file extent item was modified by someone
4475 * before the extent got locked.
4477 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4478 lock_end, GFP_NOFS);
4482 if (path->slots[0] >= nritems) {
4483 if (++nr_scaned > 2)
4486 BUG_ON(extent_locked);
4487 ret = btrfs_next_leaf(root, path);
4492 leaf = path->nodes[0];
4493 nritems = btrfs_header_nritems(leaf);
4496 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4498 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4499 if ((key.objectid > ref_path->owner_objectid) ||
4500 (key.objectid == ref_path->owner_objectid &&
4501 key.type > BTRFS_EXTENT_DATA_KEY) ||
4502 (key.offset >= first_pos + extent_key->offset))
4506 if (inode && key.objectid != inode->i_ino) {
4507 BUG_ON(extent_locked);
4508 btrfs_release_path(root, path);
4509 mutex_unlock(&inode->i_mutex);
4515 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4520 fi = btrfs_item_ptr(leaf, path->slots[0],
4521 struct btrfs_file_extent_item);
4522 extent_type = btrfs_file_extent_type(leaf, fi);
4523 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4524 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4525 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4526 extent_key->objectid)) {
4532 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4533 ext_offset = btrfs_file_extent_offset(leaf, fi);
4535 if (first_pos > key.offset - ext_offset)
4536 first_pos = key.offset - ext_offset;
4538 if (!extent_locked) {
4539 lock_start = key.offset;
4540 lock_end = lock_start + num_bytes - 1;
4542 if (lock_start > key.offset ||
4543 lock_end + 1 < key.offset + num_bytes) {
4544 unlock_extent(&BTRFS_I(inode)->io_tree,
4545 lock_start, lock_end, GFP_NOFS);
4551 btrfs_release_path(root, path);
4553 inode = btrfs_iget_locked(root->fs_info->sb,
4554 key.objectid, root);
4555 if (inode->i_state & I_NEW) {
4556 BTRFS_I(inode)->root = root;
4557 BTRFS_I(inode)->location.objectid =
4559 BTRFS_I(inode)->location.type =
4560 BTRFS_INODE_ITEM_KEY;
4561 BTRFS_I(inode)->location.offset = 0;
4562 btrfs_read_locked_inode(inode);
4563 unlock_new_inode(inode);
4566 * some code call btrfs_commit_transaction while
4567 * holding the i_mutex, so we can't use mutex_lock
4570 if (is_bad_inode(inode) ||
4571 !mutex_trylock(&inode->i_mutex)) {
4574 key.offset = (u64)-1;
4579 if (!extent_locked) {
4580 struct btrfs_ordered_extent *ordered;
4582 btrfs_release_path(root, path);
4584 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4585 lock_end, GFP_NOFS);
4586 ordered = btrfs_lookup_first_ordered_extent(inode,
4589 ordered->file_offset <= lock_end &&
4590 ordered->file_offset + ordered->len > lock_start) {
4591 unlock_extent(&BTRFS_I(inode)->io_tree,
4592 lock_start, lock_end, GFP_NOFS);
4593 btrfs_start_ordered_extent(inode, ordered, 1);
4594 btrfs_put_ordered_extent(ordered);
4595 key.offset += num_bytes;
4599 btrfs_put_ordered_extent(ordered);
4605 if (nr_extents == 1) {
4606 /* update extent pointer in place */
4607 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4608 new_extents[0].disk_bytenr);
4609 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4610 new_extents[0].disk_num_bytes);
4611 btrfs_mark_buffer_dirty(leaf);
4613 btrfs_drop_extent_cache(inode, key.offset,
4614 key.offset + num_bytes - 1, 0);
4616 ret = btrfs_inc_extent_ref(trans, root,
4617 new_extents[0].disk_bytenr,
4618 new_extents[0].disk_num_bytes,
4620 root->root_key.objectid,
4625 ret = btrfs_free_extent(trans, root,
4626 extent_key->objectid,
4629 btrfs_header_owner(leaf),
4630 btrfs_header_generation(leaf),
4634 btrfs_release_path(root, path);
4635 key.offset += num_bytes;
4643 * drop old extent pointer at first, then insert the
4644 * new pointers one bye one
4646 btrfs_release_path(root, path);
4647 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4648 key.offset + num_bytes,
4649 key.offset, &alloc_hint);
4652 for (i = 0; i < nr_extents; i++) {
4653 if (ext_offset >= new_extents[i].num_bytes) {
4654 ext_offset -= new_extents[i].num_bytes;
4657 extent_len = min(new_extents[i].num_bytes -
4658 ext_offset, num_bytes);
4660 ret = btrfs_insert_empty_item(trans, root,
4665 leaf = path->nodes[0];
4666 fi = btrfs_item_ptr(leaf, path->slots[0],
4667 struct btrfs_file_extent_item);
4668 btrfs_set_file_extent_generation(leaf, fi,
4670 btrfs_set_file_extent_type(leaf, fi,
4671 BTRFS_FILE_EXTENT_REG);
4672 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4673 new_extents[i].disk_bytenr);
4674 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4675 new_extents[i].disk_num_bytes);
4676 btrfs_set_file_extent_ram_bytes(leaf, fi,
4677 new_extents[i].ram_bytes);
4679 btrfs_set_file_extent_compression(leaf, fi,
4680 new_extents[i].compression);
4681 btrfs_set_file_extent_encryption(leaf, fi,
4682 new_extents[i].encryption);
4683 btrfs_set_file_extent_other_encoding(leaf, fi,
4684 new_extents[i].other_encoding);
4686 btrfs_set_file_extent_num_bytes(leaf, fi,
4688 ext_offset += new_extents[i].offset;
4689 btrfs_set_file_extent_offset(leaf, fi,
4691 btrfs_mark_buffer_dirty(leaf);
4693 btrfs_drop_extent_cache(inode, key.offset,
4694 key.offset + extent_len - 1, 0);
4696 ret = btrfs_inc_extent_ref(trans, root,
4697 new_extents[i].disk_bytenr,
4698 new_extents[i].disk_num_bytes,
4700 root->root_key.objectid,
4701 trans->transid, key.objectid);
4703 btrfs_release_path(root, path);
4705 inode_add_bytes(inode, extent_len);
4708 num_bytes -= extent_len;
4709 key.offset += extent_len;
4714 BUG_ON(i >= nr_extents);
4718 if (extent_locked) {
4719 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4720 lock_end, GFP_NOFS);
4724 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4725 key.offset >= first_pos + extent_key->offset)
4732 btrfs_release_path(root, path);
4734 mutex_unlock(&inode->i_mutex);
4735 if (extent_locked) {
4736 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4737 lock_end, GFP_NOFS);
4744 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4745 struct btrfs_root *root,
4746 struct extent_buffer *buf, u64 orig_start)
4751 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4752 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4754 level = btrfs_header_level(buf);
4756 struct btrfs_leaf_ref *ref;
4757 struct btrfs_leaf_ref *orig_ref;
4759 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4763 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4765 btrfs_free_leaf_ref(root, orig_ref);
4769 ref->nritems = orig_ref->nritems;
4770 memcpy(ref->extents, orig_ref->extents,
4771 sizeof(ref->extents[0]) * ref->nritems);
4773 btrfs_free_leaf_ref(root, orig_ref);
4775 ref->root_gen = trans->transid;
4776 ref->bytenr = buf->start;
4777 ref->owner = btrfs_header_owner(buf);
4778 ref->generation = btrfs_header_generation(buf);
4779 ret = btrfs_add_leaf_ref(root, ref, 0);
4781 btrfs_free_leaf_ref(root, ref);
4786 static int noinline invalidate_extent_cache(struct btrfs_root *root,
4787 struct extent_buffer *leaf,
4788 struct btrfs_block_group_cache *group,
4789 struct btrfs_root *target_root)
4791 struct btrfs_key key;
4792 struct inode *inode = NULL;
4793 struct btrfs_file_extent_item *fi;
4795 u64 skip_objectid = 0;
4799 nritems = btrfs_header_nritems(leaf);
4800 for (i = 0; i < nritems; i++) {
4801 btrfs_item_key_to_cpu(leaf, &key, i);
4802 if (key.objectid == skip_objectid ||
4803 key.type != BTRFS_EXTENT_DATA_KEY)
4805 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4806 if (btrfs_file_extent_type(leaf, fi) ==
4807 BTRFS_FILE_EXTENT_INLINE)
4809 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4811 if (!inode || inode->i_ino != key.objectid) {
4813 inode = btrfs_ilookup(target_root->fs_info->sb,
4814 key.objectid, target_root, 1);
4817 skip_objectid = key.objectid;
4820 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4822 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4823 key.offset + num_bytes - 1, GFP_NOFS);
4824 btrfs_drop_extent_cache(inode, key.offset,
4825 key.offset + num_bytes - 1, 1);
4826 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4827 key.offset + num_bytes - 1, GFP_NOFS);
4834 static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4835 struct btrfs_root *root,
4836 struct extent_buffer *leaf,
4837 struct btrfs_block_group_cache *group,
4838 struct inode *reloc_inode)
4840 struct btrfs_key key;
4841 struct btrfs_key extent_key;
4842 struct btrfs_file_extent_item *fi;
4843 struct btrfs_leaf_ref *ref;
4844 struct disk_extent *new_extent;
4853 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4854 BUG_ON(!new_extent);
4856 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4860 nritems = btrfs_header_nritems(leaf);
4861 for (i = 0; i < nritems; i++) {
4862 btrfs_item_key_to_cpu(leaf, &key, i);
4863 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4865 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4866 if (btrfs_file_extent_type(leaf, fi) ==
4867 BTRFS_FILE_EXTENT_INLINE)
4869 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4870 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4875 if (bytenr >= group->key.objectid + group->key.offset ||
4876 bytenr + num_bytes <= group->key.objectid)
4879 extent_key.objectid = bytenr;
4880 extent_key.offset = num_bytes;
4881 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4883 ret = get_new_locations(reloc_inode, &extent_key,
4884 group->key.objectid, 1,
4885 &new_extent, &nr_extent);
4890 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4891 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4892 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4893 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4895 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4896 new_extent->disk_bytenr);
4897 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4898 new_extent->disk_num_bytes);
4899 btrfs_mark_buffer_dirty(leaf);
4901 ret = btrfs_inc_extent_ref(trans, root,
4902 new_extent->disk_bytenr,
4903 new_extent->disk_num_bytes,
4905 root->root_key.objectid,
4906 trans->transid, key.objectid);
4908 ret = btrfs_free_extent(trans, root,
4909 bytenr, num_bytes, leaf->start,
4910 btrfs_header_owner(leaf),
4911 btrfs_header_generation(leaf),
4917 BUG_ON(ext_index + 1 != ref->nritems);
4918 btrfs_free_leaf_ref(root, ref);
4922 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4923 struct btrfs_root *root)
4925 struct btrfs_root *reloc_root;
4928 if (root->reloc_root) {
4929 reloc_root = root->reloc_root;
4930 root->reloc_root = NULL;
4931 list_add(&reloc_root->dead_list,
4932 &root->fs_info->dead_reloc_roots);
4934 btrfs_set_root_bytenr(&reloc_root->root_item,
4935 reloc_root->node->start);
4936 btrfs_set_root_level(&root->root_item,
4937 btrfs_header_level(reloc_root->node));
4938 memset(&reloc_root->root_item.drop_progress, 0,
4939 sizeof(struct btrfs_disk_key));
4940 reloc_root->root_item.drop_level = 0;
4942 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4943 &reloc_root->root_key,
4944 &reloc_root->root_item);
4950 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4952 struct btrfs_trans_handle *trans;
4953 struct btrfs_root *reloc_root;
4954 struct btrfs_root *prev_root = NULL;
4955 struct list_head dead_roots;
4959 INIT_LIST_HEAD(&dead_roots);
4960 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4962 while (!list_empty(&dead_roots)) {
4963 reloc_root = list_entry(dead_roots.prev,
4964 struct btrfs_root, dead_list);
4965 list_del_init(&reloc_root->dead_list);
4967 BUG_ON(reloc_root->commit_root != NULL);
4969 trans = btrfs_join_transaction(root, 1);
4972 mutex_lock(&root->fs_info->drop_mutex);
4973 ret = btrfs_drop_snapshot(trans, reloc_root);
4976 mutex_unlock(&root->fs_info->drop_mutex);
4978 nr = trans->blocks_used;
4979 ret = btrfs_end_transaction(trans, root);
4981 btrfs_btree_balance_dirty(root, nr);
4984 free_extent_buffer(reloc_root->node);
4986 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4987 &reloc_root->root_key);
4989 mutex_unlock(&root->fs_info->drop_mutex);
4991 nr = trans->blocks_used;
4992 ret = btrfs_end_transaction(trans, root);
4994 btrfs_btree_balance_dirty(root, nr);
4997 prev_root = reloc_root;
5000 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5006 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5008 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5012 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5014 struct btrfs_root *reloc_root;
5015 struct btrfs_trans_handle *trans;
5016 struct btrfs_key location;
5020 mutex_lock(&root->fs_info->tree_reloc_mutex);
5021 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5023 found = !list_empty(&root->fs_info->dead_reloc_roots);
5024 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5027 trans = btrfs_start_transaction(root, 1);
5029 ret = btrfs_commit_transaction(trans, root);
5033 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5034 location.offset = (u64)-1;
5035 location.type = BTRFS_ROOT_ITEM_KEY;
5037 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5038 BUG_ON(!reloc_root);
5039 btrfs_orphan_cleanup(reloc_root);
5043 static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5044 struct btrfs_root *root)
5046 struct btrfs_root *reloc_root;
5047 struct extent_buffer *eb;
5048 struct btrfs_root_item *root_item;
5049 struct btrfs_key root_key;
5052 BUG_ON(!root->ref_cows);
5053 if (root->reloc_root)
5056 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5059 ret = btrfs_copy_root(trans, root, root->commit_root,
5060 &eb, BTRFS_TREE_RELOC_OBJECTID);
5063 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5064 root_key.offset = root->root_key.objectid;
5065 root_key.type = BTRFS_ROOT_ITEM_KEY;
5067 memcpy(root_item, &root->root_item, sizeof(root_item));
5068 btrfs_set_root_refs(root_item, 0);
5069 btrfs_set_root_bytenr(root_item, eb->start);
5070 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5071 btrfs_set_root_generation(root_item, trans->transid);
5073 btrfs_tree_unlock(eb);
5074 free_extent_buffer(eb);
5076 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5077 &root_key, root_item);
5081 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5083 BUG_ON(!reloc_root);
5084 reloc_root->last_trans = trans->transid;
5085 reloc_root->commit_root = NULL;
5086 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5088 root->reloc_root = reloc_root;
5093 * Core function of space balance.
5095 * The idea is using reloc trees to relocate tree blocks in reference
5096 * counted roots. There is one reloc tree for each subvol, and all
5097 * reloc trees share same root key objectid. Reloc trees are snapshots
5098 * of the latest committed roots of subvols (root->commit_root).
5100 * To relocate a tree block referenced by a subvol, there are two steps.
5101 * COW the block through subvol's reloc tree, then update block pointer
5102 * in the subvol to point to the new block. Since all reloc trees share
5103 * same root key objectid, doing special handing for tree blocks owned
5104 * by them is easy. Once a tree block has been COWed in one reloc tree,
5105 * we can use the resulting new block directly when the same block is
5106 * required to COW again through other reloc trees. By this way, relocated
5107 * tree blocks are shared between reloc trees, so they are also shared
5110 static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5111 struct btrfs_root *root,
5112 struct btrfs_path *path,
5113 struct btrfs_key *first_key,
5114 struct btrfs_ref_path *ref_path,
5115 struct btrfs_block_group_cache *group,
5116 struct inode *reloc_inode)
5118 struct btrfs_root *reloc_root;
5119 struct extent_buffer *eb = NULL;
5120 struct btrfs_key *keys;
5124 int lowest_level = 0;
5127 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5128 lowest_level = ref_path->owner_objectid;
5130 if (!root->ref_cows) {
5131 path->lowest_level = lowest_level;
5132 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5134 path->lowest_level = 0;
5135 btrfs_release_path(root, path);
5139 mutex_lock(&root->fs_info->tree_reloc_mutex);
5140 ret = init_reloc_tree(trans, root);
5142 reloc_root = root->reloc_root;
5144 shared_level = ref_path->shared_level;
5145 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5147 keys = ref_path->node_keys;
5148 nodes = ref_path->new_nodes;
5149 memset(&keys[shared_level + 1], 0,
5150 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5151 memset(&nodes[shared_level + 1], 0,
5152 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5154 if (nodes[lowest_level] == 0) {
5155 path->lowest_level = lowest_level;
5156 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5159 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5160 eb = path->nodes[level];
5161 if (!eb || eb == reloc_root->node)
5163 nodes[level] = eb->start;
5165 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5167 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5170 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5171 eb = path->nodes[0];
5172 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5173 group, reloc_inode);
5176 btrfs_release_path(reloc_root, path);
5178 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5184 * replace tree blocks in the fs tree with tree blocks in
5187 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5190 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5191 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5194 extent_buffer_get(path->nodes[0]);
5195 eb = path->nodes[0];
5196 btrfs_release_path(reloc_root, path);
5197 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5199 free_extent_buffer(eb);
5202 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5203 path->lowest_level = 0;
5207 static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5208 struct btrfs_root *root,
5209 struct btrfs_path *path,
5210 struct btrfs_key *first_key,
5211 struct btrfs_ref_path *ref_path)
5215 ret = relocate_one_path(trans, root, path, first_key,
5216 ref_path, NULL, NULL);
5219 if (root == root->fs_info->extent_root)
5220 btrfs_extent_post_op(trans, root);
5225 static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5226 struct btrfs_root *extent_root,
5227 struct btrfs_path *path,
5228 struct btrfs_key *extent_key)
5232 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5235 ret = btrfs_del_item(trans, extent_root, path);
5237 btrfs_release_path(extent_root, path);
5241 static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5242 struct btrfs_ref_path *ref_path)
5244 struct btrfs_key root_key;
5246 root_key.objectid = ref_path->root_objectid;
5247 root_key.type = BTRFS_ROOT_ITEM_KEY;
5248 if (is_cowonly_root(ref_path->root_objectid))
5249 root_key.offset = 0;
5251 root_key.offset = (u64)-1;
5253 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5256 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5257 struct btrfs_path *path,
5258 struct btrfs_key *extent_key,
5259 struct btrfs_block_group_cache *group,
5260 struct inode *reloc_inode, int pass)
5262 struct btrfs_trans_handle *trans;
5263 struct btrfs_root *found_root;
5264 struct btrfs_ref_path *ref_path = NULL;
5265 struct disk_extent *new_extents = NULL;
5270 struct btrfs_key first_key;
5274 trans = btrfs_start_transaction(extent_root, 1);
5277 if (extent_key->objectid == 0) {
5278 ret = del_extent_zero(trans, extent_root, path, extent_key);
5282 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5288 for (loops = 0; ; loops++) {
5290 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5291 extent_key->objectid);
5293 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5300 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5301 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5304 found_root = read_ref_root(extent_root->fs_info, ref_path);
5305 BUG_ON(!found_root);
5307 * for reference counted tree, only process reference paths
5308 * rooted at the latest committed root.
5310 if (found_root->ref_cows &&
5311 ref_path->root_generation != found_root->root_key.offset)
5314 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5317 * copy data extents to new locations
5319 u64 group_start = group->key.objectid;
5320 ret = relocate_data_extent(reloc_inode,
5329 level = ref_path->owner_objectid;
5332 if (prev_block != ref_path->nodes[level]) {
5333 struct extent_buffer *eb;
5334 u64 block_start = ref_path->nodes[level];
5335 u64 block_size = btrfs_level_size(found_root, level);
5337 eb = read_tree_block(found_root, block_start,
5339 btrfs_tree_lock(eb);
5340 BUG_ON(level != btrfs_header_level(eb));
5343 btrfs_item_key_to_cpu(eb, &first_key, 0);
5345 btrfs_node_key_to_cpu(eb, &first_key, 0);
5347 btrfs_tree_unlock(eb);
5348 free_extent_buffer(eb);
5349 prev_block = block_start;
5352 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5355 * use fallback method to process the remaining
5359 u64 group_start = group->key.objectid;
5360 new_extents = kmalloc(sizeof(*new_extents),
5363 ret = get_new_locations(reloc_inode,
5371 btrfs_record_root_in_trans(found_root);
5372 ret = replace_one_extent(trans, found_root,
5374 &first_key, ref_path,
5375 new_extents, nr_extents);
5381 btrfs_record_root_in_trans(found_root);
5382 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5383 ret = relocate_tree_block(trans, found_root, path,
5384 &first_key, ref_path);
5387 * try to update data extent references while
5388 * keeping metadata shared between snapshots.
5390 ret = relocate_one_path(trans, found_root, path,
5391 &first_key, ref_path,
5392 group, reloc_inode);
5399 btrfs_end_transaction(trans, extent_root);
5405 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5408 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5409 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5411 num_devices = root->fs_info->fs_devices->rw_devices;
5412 if (num_devices == 1) {
5413 stripped |= BTRFS_BLOCK_GROUP_DUP;
5414 stripped = flags & ~stripped;
5416 /* turn raid0 into single device chunks */
5417 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5420 /* turn mirroring into duplication */
5421 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5422 BTRFS_BLOCK_GROUP_RAID10))
5423 return stripped | BTRFS_BLOCK_GROUP_DUP;
5426 /* they already had raid on here, just return */
5427 if (flags & stripped)
5430 stripped |= BTRFS_BLOCK_GROUP_DUP;
5431 stripped = flags & ~stripped;
5433 /* switch duplicated blocks with raid1 */
5434 if (flags & BTRFS_BLOCK_GROUP_DUP)
5435 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5437 /* turn single device chunks into raid0 */
5438 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5443 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5444 struct btrfs_block_group_cache *shrink_block_group,
5447 struct btrfs_trans_handle *trans;
5448 u64 new_alloc_flags;
5451 spin_lock(&shrink_block_group->lock);
5452 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5453 spin_unlock(&shrink_block_group->lock);
5455 trans = btrfs_start_transaction(root, 1);
5456 spin_lock(&shrink_block_group->lock);
5458 new_alloc_flags = update_block_group_flags(root,
5459 shrink_block_group->flags);
5460 if (new_alloc_flags != shrink_block_group->flags) {
5462 btrfs_block_group_used(&shrink_block_group->item);
5464 calc = shrink_block_group->key.offset;
5466 spin_unlock(&shrink_block_group->lock);
5468 do_chunk_alloc(trans, root->fs_info->extent_root,
5469 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5471 btrfs_end_transaction(trans, root);
5473 spin_unlock(&shrink_block_group->lock);
5477 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5478 struct btrfs_root *root,
5479 u64 objectid, u64 size)
5481 struct btrfs_path *path;
5482 struct btrfs_inode_item *item;
5483 struct extent_buffer *leaf;
5486 path = btrfs_alloc_path();
5490 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5494 leaf = path->nodes[0];
5495 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5496 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5497 btrfs_set_inode_generation(leaf, item, 1);
5498 btrfs_set_inode_size(leaf, item, size);
5499 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5500 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5501 BTRFS_INODE_NOCOMPRESS);
5502 btrfs_mark_buffer_dirty(leaf);
5503 btrfs_release_path(root, path);
5505 btrfs_free_path(path);
5509 static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5510 struct btrfs_block_group_cache *group)
5512 struct inode *inode = NULL;
5513 struct btrfs_trans_handle *trans;
5514 struct btrfs_root *root;
5515 struct btrfs_key root_key;
5516 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5519 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5520 root_key.type = BTRFS_ROOT_ITEM_KEY;
5521 root_key.offset = (u64)-1;
5522 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5524 return ERR_CAST(root);
5526 trans = btrfs_start_transaction(root, 1);
5529 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5533 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5536 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5537 group->key.offset, 0, group->key.offset,
5541 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5542 if (inode->i_state & I_NEW) {
5543 BTRFS_I(inode)->root = root;
5544 BTRFS_I(inode)->location.objectid = objectid;
5545 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5546 BTRFS_I(inode)->location.offset = 0;
5547 btrfs_read_locked_inode(inode);
5548 unlock_new_inode(inode);
5549 BUG_ON(is_bad_inode(inode));
5554 err = btrfs_orphan_add(trans, inode);
5556 btrfs_end_transaction(trans, root);
5560 inode = ERR_PTR(err);
5565 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5567 struct btrfs_trans_handle *trans;
5568 struct btrfs_path *path;
5569 struct btrfs_fs_info *info = root->fs_info;
5570 struct extent_buffer *leaf;
5571 struct inode *reloc_inode;
5572 struct btrfs_block_group_cache *block_group;
5573 struct btrfs_key key;
5582 root = root->fs_info->extent_root;
5584 block_group = btrfs_lookup_block_group(info, group_start);
5585 BUG_ON(!block_group);
5587 printk("btrfs relocating block group %llu flags %llu\n",
5588 (unsigned long long)block_group->key.objectid,
5589 (unsigned long long)block_group->flags);
5591 path = btrfs_alloc_path();
5594 reloc_inode = create_reloc_inode(info, block_group);
5595 BUG_ON(IS_ERR(reloc_inode));
5597 __alloc_chunk_for_shrink(root, block_group, 1);
5598 set_block_group_readonly(block_group);
5600 btrfs_start_delalloc_inodes(info->tree_root);
5601 btrfs_wait_ordered_extents(info->tree_root, 0);
5606 key.objectid = block_group->key.objectid;
5609 cur_byte = key.objectid;
5611 trans = btrfs_start_transaction(info->tree_root, 1);
5612 btrfs_commit_transaction(trans, info->tree_root);
5614 mutex_lock(&root->fs_info->cleaner_mutex);
5615 btrfs_clean_old_snapshots(info->tree_root);
5616 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5617 mutex_unlock(&root->fs_info->cleaner_mutex);
5620 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5624 leaf = path->nodes[0];
5625 nritems = btrfs_header_nritems(leaf);
5626 if (path->slots[0] >= nritems) {
5627 ret = btrfs_next_leaf(root, path);
5634 leaf = path->nodes[0];
5635 nritems = btrfs_header_nritems(leaf);
5638 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5640 if (key.objectid >= block_group->key.objectid +
5641 block_group->key.offset)
5644 if (progress && need_resched()) {
5645 btrfs_release_path(root, path);
5652 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5653 key.objectid + key.offset <= cur_byte) {
5659 cur_byte = key.objectid + key.offset;
5660 btrfs_release_path(root, path);
5662 __alloc_chunk_for_shrink(root, block_group, 0);
5663 ret = relocate_one_extent(root, path, &key, block_group,
5669 key.objectid = cur_byte;
5674 btrfs_release_path(root, path);
5677 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5678 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5679 WARN_ON(reloc_inode->i_mapping->nrpages);
5682 if (total_found > 0) {
5683 printk("btrfs found %llu extents in pass %d\n",
5684 (unsigned long long)total_found, pass);
5686 if (total_found == skipped && pass > 2) {
5688 reloc_inode = create_reloc_inode(info, block_group);
5694 /* delete reloc_inode */
5697 /* unpin extents in this range */
5698 trans = btrfs_start_transaction(info->tree_root, 1);
5699 btrfs_commit_transaction(trans, info->tree_root);
5701 spin_lock(&block_group->lock);
5702 WARN_ON(block_group->pinned > 0);
5703 WARN_ON(block_group->reserved > 0);
5704 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5705 spin_unlock(&block_group->lock);
5708 btrfs_free_path(path);
5712 static int find_first_block_group(struct btrfs_root *root,
5713 struct btrfs_path *path, struct btrfs_key *key)
5716 struct btrfs_key found_key;
5717 struct extent_buffer *leaf;
5720 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5725 slot = path->slots[0];
5726 leaf = path->nodes[0];
5727 if (slot >= btrfs_header_nritems(leaf)) {
5728 ret = btrfs_next_leaf(root, path);
5735 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5737 if (found_key.objectid >= key->objectid &&
5738 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5749 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5751 struct btrfs_block_group_cache *block_group;
5754 spin_lock(&info->block_group_cache_lock);
5755 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5756 block_group = rb_entry(n, struct btrfs_block_group_cache,
5758 rb_erase(&block_group->cache_node,
5759 &info->block_group_cache_tree);
5760 spin_unlock(&info->block_group_cache_lock);
5762 btrfs_remove_free_space_cache(block_group);
5763 down_write(&block_group->space_info->groups_sem);
5764 list_del(&block_group->list);
5765 up_write(&block_group->space_info->groups_sem);
5768 spin_lock(&info->block_group_cache_lock);
5770 spin_unlock(&info->block_group_cache_lock);
5774 int btrfs_read_block_groups(struct btrfs_root *root)
5776 struct btrfs_path *path;
5778 struct btrfs_block_group_cache *cache;
5779 struct btrfs_fs_info *info = root->fs_info;
5780 struct btrfs_space_info *space_info;
5781 struct btrfs_key key;
5782 struct btrfs_key found_key;
5783 struct extent_buffer *leaf;
5785 root = info->extent_root;
5788 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5789 path = btrfs_alloc_path();
5794 ret = find_first_block_group(root, path, &key);
5802 leaf = path->nodes[0];
5803 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5804 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5810 spin_lock_init(&cache->lock);
5811 mutex_init(&cache->alloc_mutex);
5812 mutex_init(&cache->cache_mutex);
5813 INIT_LIST_HEAD(&cache->list);
5814 read_extent_buffer(leaf, &cache->item,
5815 btrfs_item_ptr_offset(leaf, path->slots[0]),
5816 sizeof(cache->item));
5817 memcpy(&cache->key, &found_key, sizeof(found_key));
5819 key.objectid = found_key.objectid + found_key.offset;
5820 btrfs_release_path(root, path);
5821 cache->flags = btrfs_block_group_flags(&cache->item);
5823 ret = update_space_info(info, cache->flags, found_key.offset,
5824 btrfs_block_group_used(&cache->item),
5827 cache->space_info = space_info;
5828 down_write(&space_info->groups_sem);
5829 list_add_tail(&cache->list, &space_info->block_groups);
5830 up_write(&space_info->groups_sem);
5832 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5835 set_avail_alloc_bits(root->fs_info, cache->flags);
5836 if (btrfs_chunk_readonly(root, cache->key.objectid))
5837 set_block_group_readonly(cache);
5841 btrfs_free_path(path);
5845 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5846 struct btrfs_root *root, u64 bytes_used,
5847 u64 type, u64 chunk_objectid, u64 chunk_offset,
5851 struct btrfs_root *extent_root;
5852 struct btrfs_block_group_cache *cache;
5854 extent_root = root->fs_info->extent_root;
5856 root->fs_info->last_trans_new_blockgroup = trans->transid;
5858 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5862 cache->key.objectid = chunk_offset;
5863 cache->key.offset = size;
5864 spin_lock_init(&cache->lock);
5865 mutex_init(&cache->alloc_mutex);
5866 mutex_init(&cache->cache_mutex);
5867 INIT_LIST_HEAD(&cache->list);
5868 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5870 btrfs_set_block_group_used(&cache->item, bytes_used);
5871 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5872 cache->flags = type;
5873 btrfs_set_block_group_flags(&cache->item, type);
5875 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5876 &cache->space_info);
5878 down_write(&cache->space_info->groups_sem);
5879 list_add_tail(&cache->list, &cache->space_info->block_groups);
5880 up_write(&cache->space_info->groups_sem);
5882 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5885 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5886 sizeof(cache->item));
5889 finish_current_insert(trans, extent_root, 0);
5890 ret = del_pending_extents(trans, extent_root, 0);
5892 set_avail_alloc_bits(extent_root->fs_info, type);
5897 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5898 struct btrfs_root *root, u64 group_start)
5900 struct btrfs_path *path;
5901 struct btrfs_block_group_cache *block_group;
5902 struct btrfs_key key;
5905 root = root->fs_info->extent_root;
5907 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5908 BUG_ON(!block_group);
5909 BUG_ON(!block_group->ro);
5911 memcpy(&key, &block_group->key, sizeof(key));
5913 path = btrfs_alloc_path();
5916 btrfs_remove_free_space_cache(block_group);
5917 rb_erase(&block_group->cache_node,
5918 &root->fs_info->block_group_cache_tree);
5919 down_write(&block_group->space_info->groups_sem);
5920 list_del(&block_group->list);
5921 up_write(&block_group->space_info->groups_sem);
5923 spin_lock(&block_group->space_info->lock);
5924 block_group->space_info->total_bytes -= block_group->key.offset;
5925 block_group->space_info->bytes_readonly -= block_group->key.offset;
5926 spin_unlock(&block_group->space_info->lock);
5927 block_group->space_info->full = 0;
5930 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5931 kfree(shrink_block_group);
5934 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5940 ret = btrfs_del_item(trans, root, path);
5942 btrfs_free_path(path);