2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "ref-cache.h"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op {
45 struct list_head list;
49 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root, int all);
51 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
52 btrfs_root *extent_root, int all);
53 static struct btrfs_block_group_cache *
54 __btrfs_find_block_group(struct btrfs_root *root,
55 struct btrfs_block_group_cache *hint,
56 u64 search_start, int data, int owner);
57 static int pin_down_bytes(struct btrfs_trans_handle *trans,
58 struct btrfs_root *root,
59 u64 bytenr, u64 num_bytes, int is_data);
60 static int update_block_group(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root,
62 u64 bytenr, u64 num_bytes, int alloc,
65 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
67 return (cache->flags & bits) == bits;
71 * this adds the block group to the fs_info rb tree for the block group
74 int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
75 struct btrfs_block_group_cache *block_group)
78 struct rb_node *parent = NULL;
79 struct btrfs_block_group_cache *cache;
81 spin_lock(&info->block_group_cache_lock);
82 p = &info->block_group_cache_tree.rb_node;
86 cache = rb_entry(parent, struct btrfs_block_group_cache,
88 if (block_group->key.objectid < cache->key.objectid) {
90 } else if (block_group->key.objectid > cache->key.objectid) {
93 spin_unlock(&info->block_group_cache_lock);
98 rb_link_node(&block_group->cache_node, parent, p);
99 rb_insert_color(&block_group->cache_node,
100 &info->block_group_cache_tree);
101 spin_unlock(&info->block_group_cache_lock);
107 * This will return the block group at or after bytenr if contains is 0, else
108 * it will return the block group that contains the bytenr
110 static struct btrfs_block_group_cache *
111 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
114 struct btrfs_block_group_cache *cache, *ret = NULL;
118 spin_lock(&info->block_group_cache_lock);
119 n = info->block_group_cache_tree.rb_node;
122 cache = rb_entry(n, struct btrfs_block_group_cache,
124 end = cache->key.objectid + cache->key.offset - 1;
125 start = cache->key.objectid;
127 if (bytenr < start) {
128 if (!contains && (!ret || start < ret->key.objectid))
131 } else if (bytenr > start) {
132 if (contains && bytenr <= end) {
142 spin_unlock(&info->block_group_cache_lock);
148 * this is only called by cache_block_group, since we could have freed extents
149 * we need to check the pinned_extents for any extents that can't be used yet
150 * since their free space will be released as soon as the transaction commits.
152 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
153 struct btrfs_fs_info *info, u64 start, u64 end)
155 u64 extent_start, extent_end, size;
158 mutex_lock(&info->pinned_mutex);
159 while (start < end) {
160 ret = find_first_extent_bit(&info->pinned_extents, start,
161 &extent_start, &extent_end,
166 if (extent_start == start) {
167 start = extent_end + 1;
168 } else if (extent_start > start && extent_start < end) {
169 size = extent_start - start;
170 ret = btrfs_add_free_space_lock(block_group, start,
173 start = extent_end + 1;
181 ret = btrfs_add_free_space_lock(block_group, start, size);
184 mutex_unlock(&info->pinned_mutex);
189 static int cache_block_group(struct btrfs_root *root,
190 struct btrfs_block_group_cache *block_group)
192 struct btrfs_path *path;
194 struct btrfs_key key;
195 struct extent_buffer *leaf;
204 root = root->fs_info->extent_root;
206 if (block_group->cached)
209 path = btrfs_alloc_path();
215 * we get into deadlocks with paths held by callers of this function.
216 * since the alloc_mutex is protecting things right now, just
217 * skip the locking here
219 path->skip_locking = 1;
220 first_free = max_t(u64, block_group->key.objectid,
221 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
222 key.objectid = block_group->key.objectid;
224 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
225 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
228 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
232 leaf = path->nodes[0];
233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
234 if (key.objectid + key.offset > first_free)
235 first_free = key.objectid + key.offset;
238 leaf = path->nodes[0];
239 slot = path->slots[0];
240 if (slot >= btrfs_header_nritems(leaf)) {
241 ret = btrfs_next_leaf(root, path);
249 btrfs_item_key_to_cpu(leaf, &key, slot);
250 if (key.objectid < block_group->key.objectid)
253 if (key.objectid >= block_group->key.objectid +
254 block_group->key.offset)
257 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
263 add_new_free_space(block_group, root->fs_info, last,
266 last = key.objectid + key.offset;
275 add_new_free_space(block_group, root->fs_info, last,
276 block_group->key.objectid +
277 block_group->key.offset);
279 block_group->cached = 1;
282 btrfs_free_path(path);
287 * return the block group that starts at or after bytenr
289 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
293 struct btrfs_block_group_cache *cache;
295 cache = block_group_cache_tree_search(info, bytenr, 0);
301 * return the block group that contains teh given bytenr
303 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
307 struct btrfs_block_group_cache *cache;
309 cache = block_group_cache_tree_search(info, bytenr, 1);
314 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
317 struct list_head *head = &info->space_info;
318 struct list_head *cur;
319 struct btrfs_space_info *found;
320 list_for_each(cur, head) {
321 found = list_entry(cur, struct btrfs_space_info, list);
322 if (found->flags == flags)
328 static u64 div_factor(u64 num, int factor)
337 static struct btrfs_block_group_cache *
338 __btrfs_find_block_group(struct btrfs_root *root,
339 struct btrfs_block_group_cache *hint,
340 u64 search_start, int data, int owner)
342 struct btrfs_block_group_cache *cache;
343 struct btrfs_block_group_cache *found_group = NULL;
344 struct btrfs_fs_info *info = root->fs_info;
352 if (data & BTRFS_BLOCK_GROUP_METADATA)
356 struct btrfs_block_group_cache *shint;
357 shint = btrfs_lookup_first_block_group(info, search_start);
358 if (shint && block_group_bits(shint, data)) {
359 spin_lock(&shint->lock);
360 used = btrfs_block_group_used(&shint->item);
361 if (used + shint->pinned + shint->reserved <
362 div_factor(shint->key.offset, factor)) {
363 spin_unlock(&shint->lock);
366 spin_unlock(&shint->lock);
369 if (hint && block_group_bits(hint, data)) {
370 spin_lock(&hint->lock);
371 used = btrfs_block_group_used(&hint->item);
372 if (used + hint->pinned + hint->reserved <
373 div_factor(hint->key.offset, factor)) {
374 spin_unlock(&hint->lock);
377 spin_unlock(&hint->lock);
378 last = hint->key.objectid + hint->key.offset;
381 last = max(hint->key.objectid, search_start);
387 cache = btrfs_lookup_first_block_group(root->fs_info, last);
391 spin_lock(&cache->lock);
392 last = cache->key.objectid + cache->key.offset;
393 used = btrfs_block_group_used(&cache->item);
395 if (block_group_bits(cache, data)) {
396 free_check = div_factor(cache->key.offset, factor);
397 if (used + cache->pinned + cache->reserved <
400 spin_unlock(&cache->lock);
404 spin_unlock(&cache->lock);
412 if (!full_search && factor < 10) {
422 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
423 struct btrfs_block_group_cache
424 *hint, u64 search_start,
428 struct btrfs_block_group_cache *ret;
429 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
433 /* simple helper to search for an existing extent at a given offset */
434 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
437 struct btrfs_key key;
438 struct btrfs_path *path;
440 path = btrfs_alloc_path();
442 key.objectid = start;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
447 btrfs_free_path(path);
452 * Back reference rules. Back refs have three main goals:
454 * 1) differentiate between all holders of references to an extent so that
455 * when a reference is dropped we can make sure it was a valid reference
456 * before freeing the extent.
458 * 2) Provide enough information to quickly find the holders of an extent
459 * if we notice a given block is corrupted or bad.
461 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
462 * maintenance. This is actually the same as #2, but with a slightly
463 * different use case.
465 * File extents can be referenced by:
467 * - multiple snapshots, subvolumes, or different generations in one subvol
468 * - different files inside a single subvolume
469 * - different offsets inside a file (bookend extents in file.c)
471 * The extent ref structure has fields for:
473 * - Objectid of the subvolume root
474 * - Generation number of the tree holding the reference
475 * - objectid of the file holding the reference
476 * - number of references holding by parent node (alway 1 for tree blocks)
478 * Btree leaf may hold multiple references to a file extent. In most cases,
479 * these references are from same file and the corresponding offsets inside
480 * the file are close together.
482 * When a file extent is allocated the fields are filled in:
483 * (root_key.objectid, trans->transid, inode objectid, 1)
485 * When a leaf is cow'd new references are added for every file extent found
486 * in the leaf. It looks similar to the create case, but trans->transid will
487 * be different when the block is cow'd.
489 * (root_key.objectid, trans->transid, inode objectid,
490 * number of references in the leaf)
492 * When a file extent is removed either during snapshot deletion or
493 * file truncation, we find the corresponding back reference and check
494 * the following fields:
496 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
499 * Btree extents can be referenced by:
501 * - Different subvolumes
502 * - Different generations of the same subvolume
504 * When a tree block is created, back references are inserted:
506 * (root->root_key.objectid, trans->transid, level, 1)
508 * When a tree block is cow'd, new back references are added for all the
509 * blocks it points to. If the tree block isn't in reference counted root,
510 * the old back references are removed. These new back references are of
511 * the form (trans->transid will have increased since creation):
513 * (root->root_key.objectid, trans->transid, level, 1)
515 * When a backref is in deleting, the following fields are checked:
517 * if backref was for a tree root:
518 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
520 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
522 * Back Reference Key composing:
524 * The key objectid corresponds to the first byte in the extent, the key
525 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
526 * byte of parent extent. If a extent is tree root, the key offset is set
527 * to the key objectid.
530 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
532 struct btrfs_path *path,
533 u64 bytenr, u64 parent,
534 u64 ref_root, u64 ref_generation,
535 u64 owner_objectid, int del)
537 struct btrfs_key key;
538 struct btrfs_extent_ref *ref;
539 struct extent_buffer *leaf;
543 key.objectid = bytenr;
544 key.type = BTRFS_EXTENT_REF_KEY;
547 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
555 leaf = path->nodes[0];
556 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
557 ref_objectid = btrfs_ref_objectid(leaf, ref);
558 if (btrfs_ref_root(leaf, ref) != ref_root ||
559 btrfs_ref_generation(leaf, ref) != ref_generation ||
560 (ref_objectid != owner_objectid &&
561 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
572 * updates all the backrefs that are pending on update_list for the
575 static int noinline update_backrefs(struct btrfs_trans_handle *trans,
576 struct btrfs_root *extent_root,
577 struct btrfs_path *path,
578 struct list_head *update_list)
580 struct btrfs_key key;
581 struct btrfs_extent_ref *ref;
582 struct btrfs_fs_info *info = extent_root->fs_info;
583 struct pending_extent_op *op;
584 struct extent_buffer *leaf;
586 struct list_head *cur = update_list->next;
588 u64 ref_root = extent_root->root_key.objectid;
590 op = list_entry(cur, struct pending_extent_op, list);
593 key.objectid = op->bytenr;
594 key.type = BTRFS_EXTENT_REF_KEY;
595 key.offset = op->orig_parent;
597 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
600 leaf = path->nodes[0];
603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
605 ref_objectid = btrfs_ref_objectid(leaf, ref);
607 if (btrfs_ref_root(leaf, ref) != ref_root ||
608 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
609 (ref_objectid != op->level &&
610 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
611 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
612 "owner %u\n", op->bytenr, op->orig_parent,
613 ref_root, op->level);
614 btrfs_print_leaf(extent_root, leaf);
618 key.objectid = op->bytenr;
619 key.offset = op->parent;
620 key.type = BTRFS_EXTENT_REF_KEY;
621 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
623 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
624 btrfs_set_ref_generation(leaf, ref, op->generation);
628 list_del_init(&op->list);
629 unlock_extent(&info->extent_ins, op->bytenr,
630 op->bytenr + op->num_bytes - 1, GFP_NOFS);
633 if (cur == update_list) {
634 btrfs_mark_buffer_dirty(path->nodes[0]);
635 btrfs_release_path(extent_root, path);
639 op = list_entry(cur, struct pending_extent_op, list);
642 while (path->slots[0] < btrfs_header_nritems(leaf)) {
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644 if (key.objectid == op->bytenr &&
645 key.type == BTRFS_EXTENT_REF_KEY)
650 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(extent_root, path);
658 static int noinline insert_extents(struct btrfs_trans_handle *trans,
659 struct btrfs_root *extent_root,
660 struct btrfs_path *path,
661 struct list_head *insert_list, int nr)
663 struct btrfs_key *keys;
665 struct pending_extent_op *op;
666 struct extent_buffer *leaf;
667 struct list_head *cur = insert_list->next;
668 struct btrfs_fs_info *info = extent_root->fs_info;
669 u64 ref_root = extent_root->root_key.objectid;
670 int i = 0, last = 0, ret;
676 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
680 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
686 list_for_each_entry(op, insert_list, list) {
687 keys[i].objectid = op->bytenr;
688 keys[i].offset = op->num_bytes;
689 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
690 data_size[i] = sizeof(struct btrfs_extent_item);
693 keys[i].objectid = op->bytenr;
694 keys[i].offset = op->parent;
695 keys[i].type = BTRFS_EXTENT_REF_KEY;
696 data_size[i] = sizeof(struct btrfs_extent_ref);
700 op = list_entry(cur, struct pending_extent_op, list);
704 ret = btrfs_insert_some_items(trans, extent_root, path,
705 keys+i, data_size+i, total-i);
711 leaf = path->nodes[0];
712 for (c = 0; c < ret; c++) {
713 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
716 * if the first item we inserted was a backref, then
717 * the EXTENT_ITEM will be the odd c's, else it will
720 if ((ref_first && (c % 2)) ||
721 (!ref_first && !(c % 2))) {
722 struct btrfs_extent_item *itm;
724 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
725 struct btrfs_extent_item);
726 btrfs_set_extent_refs(path->nodes[0], itm, 1);
729 struct btrfs_extent_ref *ref;
731 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_ref);
733 btrfs_set_ref_root(leaf, ref, ref_root);
734 btrfs_set_ref_generation(leaf, ref,
736 btrfs_set_ref_objectid(leaf, ref, op->level);
737 btrfs_set_ref_num_refs(leaf, ref, 1);
742 * using del to see when its ok to free up the
743 * pending_extent_op. In the case where we insert the
744 * last item on the list in order to help do batching
745 * we need to not free the extent op until we actually
746 * insert the extent_item
749 unlock_extent(&info->extent_ins, op->bytenr,
750 op->bytenr + op->num_bytes - 1,
753 list_del_init(&op->list);
755 if (cur != insert_list)
757 struct pending_extent_op,
761 btrfs_mark_buffer_dirty(leaf);
762 btrfs_release_path(extent_root, path);
765 * Ok backref's and items usually go right next to eachother,
766 * but if we could only insert 1 item that means that we
767 * inserted on the end of a leaf, and we have no idea what may
768 * be on the next leaf so we just play it safe. In order to
769 * try and help this case we insert the last thing on our
770 * insert list so hopefully it will end up being the last
771 * thing on the leaf and everything else will be before it,
772 * which will let us insert a whole bunch of items at the same
775 if (ret == 1 && !last && (i + ret < total)) {
777 * last: where we will pick up the next time around
778 * i: our current key to insert, will be total - 1
779 * cur: the current op we are screwing with
784 cur = insert_list->prev;
785 op = list_entry(cur, struct pending_extent_op, list);
788 * ok we successfully inserted the last item on the
789 * list, lets reset everything
791 * i: our current key to insert, so where we left off
793 * last: done with this
794 * cur: the op we are messing with
796 * total: since we inserted the last key, we need to
797 * decrement total so we dont overflow
801 cur = insert_list->next;
802 op = list_entry(cur, struct pending_extent_op, list);
816 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root,
818 struct btrfs_path *path,
819 u64 bytenr, u64 parent,
820 u64 ref_root, u64 ref_generation,
823 struct btrfs_key key;
824 struct extent_buffer *leaf;
825 struct btrfs_extent_ref *ref;
829 key.objectid = bytenr;
830 key.type = BTRFS_EXTENT_REF_KEY;
833 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
835 leaf = path->nodes[0];
836 ref = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_ref);
838 btrfs_set_ref_root(leaf, ref, ref_root);
839 btrfs_set_ref_generation(leaf, ref, ref_generation);
840 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
841 btrfs_set_ref_num_refs(leaf, ref, 1);
842 } else if (ret == -EEXIST) {
844 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 if (btrfs_ref_root(leaf, ref) != ref_root ||
849 btrfs_ref_generation(leaf, ref) != ref_generation) {
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
859 existing_owner = btrfs_ref_objectid(leaf, ref);
860 if (existing_owner != owner_objectid &&
861 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
862 btrfs_set_ref_objectid(leaf, ref,
863 BTRFS_MULTIPLE_OBJECTIDS);
869 btrfs_mark_buffer_dirty(path->nodes[0]);
871 btrfs_release_path(root, path);
875 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
877 struct btrfs_path *path)
879 struct extent_buffer *leaf;
880 struct btrfs_extent_ref *ref;
884 leaf = path->nodes[0];
885 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
886 num_refs = btrfs_ref_num_refs(leaf, ref);
887 BUG_ON(num_refs == 0);
890 ret = btrfs_del_item(trans, root, path);
892 btrfs_set_ref_num_refs(leaf, ref, num_refs);
893 btrfs_mark_buffer_dirty(leaf);
895 btrfs_release_path(root, path);
899 static int noinline free_extents(struct btrfs_trans_handle *trans,
900 struct btrfs_root *extent_root,
901 struct list_head *del_list)
903 struct btrfs_fs_info *info = extent_root->fs_info;
904 struct btrfs_path *path;
905 struct btrfs_key key, found_key;
906 struct extent_buffer *leaf;
907 struct list_head *cur;
908 struct pending_extent_op *op;
909 struct btrfs_extent_item *ei;
910 int ret, num_to_del, extent_slot = 0, found_extent = 0;
914 path = btrfs_alloc_path();
920 /* search for the backref for the current ref we want to delete */
921 cur = del_list->next;
922 op = list_entry(cur, struct pending_extent_op, list);
923 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
925 extent_root->root_key.objectid,
926 op->orig_generation, op->level, 1);
928 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
929 "owner %u\n", op->bytenr,
930 extent_root->root_key.objectid, op->orig_generation,
932 btrfs_print_leaf(extent_root, path->nodes[0]);
937 extent_slot = path->slots[0];
942 * if we aren't the first item on the leaf we can move back one and see
943 * if our ref is right next to our extent item
945 if (likely(extent_slot)) {
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
949 if (found_key.objectid == op->bytenr &&
950 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
951 found_key.offset == op->num_bytes) {
958 * if we didn't find the extent we need to delete the backref and then
959 * search for the extent item key so we can update its ref count
962 key.objectid = op->bytenr;
963 key.type = BTRFS_EXTENT_ITEM_KEY;
964 key.offset = op->num_bytes;
966 ret = remove_extent_backref(trans, extent_root, path);
968 btrfs_release_path(extent_root, path);
969 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
971 extent_slot = path->slots[0];
974 /* this is where we update the ref count for the extent */
975 leaf = path->nodes[0];
976 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
977 refs = btrfs_extent_refs(leaf, ei);
980 btrfs_set_extent_refs(leaf, ei, refs);
982 btrfs_mark_buffer_dirty(leaf);
985 * This extent needs deleting. The reason cur_slot is extent_slot +
986 * num_to_del is because extent_slot points to the slot where the extent
987 * is, and if the backref was not right next to the extent we will be
988 * deleting at least 1 item, and will want to start searching at the
989 * slot directly next to extent_slot. However if we did find the
990 * backref next to the extent item them we will be deleting at least 2
991 * items and will want to start searching directly after the ref slot
994 struct list_head *pos, *n, *end;
995 int cur_slot = extent_slot+num_to_del;
999 path->slots[0] = extent_slot;
1000 bytes_freed = op->num_bytes;
1002 mutex_lock(&info->pinned_mutex);
1003 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1004 op->num_bytes, op->level >=
1005 BTRFS_FIRST_FREE_OBJECTID);
1006 mutex_unlock(&info->pinned_mutex);
1011 * we need to see if we can delete multiple things at once, so
1012 * start looping through the list of extents we are wanting to
1013 * delete and see if their extent/backref's are right next to
1014 * eachother and the extents only have 1 ref
1016 for (pos = cur->next; pos != del_list; pos = pos->next) {
1017 struct pending_extent_op *tmp;
1019 tmp = list_entry(pos, struct pending_extent_op, list);
1021 /* we only want to delete extent+ref at this stage */
1022 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1025 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1026 if (found_key.objectid != tmp->bytenr ||
1027 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1028 found_key.offset != tmp->num_bytes)
1031 /* check to make sure this extent only has one ref */
1032 ei = btrfs_item_ptr(leaf, cur_slot,
1033 struct btrfs_extent_item);
1034 if (btrfs_extent_refs(leaf, ei) != 1)
1037 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1038 if (found_key.objectid != tmp->bytenr ||
1039 found_key.type != BTRFS_EXTENT_REF_KEY ||
1040 found_key.offset != tmp->orig_parent)
1044 * the ref is right next to the extent, we can set the
1045 * ref count to 0 since we will delete them both now
1047 btrfs_set_extent_refs(leaf, ei, 0);
1049 /* pin down the bytes for this extent */
1050 mutex_lock(&info->pinned_mutex);
1051 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1052 tmp->num_bytes, tmp->level >=
1053 BTRFS_FIRST_FREE_OBJECTID);
1054 mutex_unlock(&info->pinned_mutex);
1058 * use the del field to tell if we need to go ahead and
1059 * free up the extent when we delete the item or not.
1062 bytes_freed += tmp->num_bytes;
1069 /* update the free space counters */
1070 spin_lock_irq(&info->delalloc_lock);
1071 super_used = btrfs_super_bytes_used(&info->super_copy);
1072 btrfs_set_super_bytes_used(&info->super_copy,
1073 super_used - bytes_freed);
1074 spin_unlock_irq(&info->delalloc_lock);
1076 root_used = btrfs_root_used(&extent_root->root_item);
1077 btrfs_set_root_used(&extent_root->root_item,
1078 root_used - bytes_freed);
1080 /* delete the items */
1081 ret = btrfs_del_items(trans, extent_root, path,
1082 path->slots[0], num_to_del);
1086 * loop through the extents we deleted and do the cleanup work
1089 for (pos = cur, n = pos->next; pos != end;
1090 pos = n, n = pos->next) {
1091 struct pending_extent_op *tmp;
1092 #ifdef BIO_RW_DISCARD
1094 struct btrfs_multi_bio *multi = NULL;
1096 tmp = list_entry(pos, struct pending_extent_op, list);
1099 * remember tmp->del tells us wether or not we pinned
1102 ret = update_block_group(trans, extent_root,
1103 tmp->bytenr, tmp->num_bytes, 0,
1107 #ifdef BIO_RW_DISCARD
1108 ret = btrfs_map_block(&info->mapping_tree, READ,
1109 tmp->bytenr, &map_length, &multi,
1112 struct btrfs_bio_stripe *stripe;
1115 stripe = multi->stripe;
1117 if (map_length > tmp->num_bytes)
1118 map_length = tmp->num_bytes;
1120 for (i = 0; i < multi->num_stripes;
1122 blkdev_issue_discard(stripe->dev->bdev,
1123 stripe->physical >> 9,
1128 list_del_init(&tmp->list);
1129 unlock_extent(&info->extent_ins, tmp->bytenr,
1130 tmp->bytenr + tmp->num_bytes - 1,
1134 } else if (refs && found_extent) {
1136 * the ref and extent were right next to eachother, but the
1137 * extent still has a ref, so just free the backref and keep
1140 ret = remove_extent_backref(trans, extent_root, path);
1143 list_del_init(&op->list);
1144 unlock_extent(&info->extent_ins, op->bytenr,
1145 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1149 * the extent has multiple refs and the backref we were looking
1150 * for was not right next to it, so just unlock and go next,
1153 list_del_init(&op->list);
1154 unlock_extent(&info->extent_ins, op->bytenr,
1155 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1159 btrfs_release_path(extent_root, path);
1160 if (!list_empty(del_list))
1164 btrfs_free_path(path);
1168 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root, u64 bytenr,
1170 u64 orig_parent, u64 parent,
1171 u64 orig_root, u64 ref_root,
1172 u64 orig_generation, u64 ref_generation,
1176 struct btrfs_root *extent_root = root->fs_info->extent_root;
1177 struct btrfs_path *path;
1179 if (root == root->fs_info->extent_root) {
1180 struct pending_extent_op *extent_op;
1183 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1184 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1185 mutex_lock(&root->fs_info->extent_ins_mutex);
1186 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1187 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1189 ret = get_state_private(&root->fs_info->extent_ins,
1192 extent_op = (struct pending_extent_op *)
1193 (unsigned long)priv;
1194 BUG_ON(extent_op->parent != orig_parent);
1195 BUG_ON(extent_op->generation != orig_generation);
1197 extent_op->parent = parent;
1198 extent_op->generation = ref_generation;
1200 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1203 extent_op->type = PENDING_BACKREF_UPDATE;
1204 extent_op->bytenr = bytenr;
1205 extent_op->num_bytes = num_bytes;
1206 extent_op->parent = parent;
1207 extent_op->orig_parent = orig_parent;
1208 extent_op->generation = ref_generation;
1209 extent_op->orig_generation = orig_generation;
1210 extent_op->level = (int)owner_objectid;
1211 INIT_LIST_HEAD(&extent_op->list);
1214 set_extent_bits(&root->fs_info->extent_ins,
1215 bytenr, bytenr + num_bytes - 1,
1216 EXTENT_WRITEBACK, GFP_NOFS);
1217 set_state_private(&root->fs_info->extent_ins,
1218 bytenr, (unsigned long)extent_op);
1220 mutex_unlock(&root->fs_info->extent_ins_mutex);
1224 path = btrfs_alloc_path();
1227 ret = lookup_extent_backref(trans, extent_root, path,
1228 bytenr, orig_parent, orig_root,
1229 orig_generation, owner_objectid, 1);
1232 ret = remove_extent_backref(trans, extent_root, path);
1235 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1236 parent, ref_root, ref_generation,
1239 finish_current_insert(trans, extent_root, 0);
1240 del_pending_extents(trans, extent_root, 0);
1242 btrfs_free_path(path);
1246 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root, u64 bytenr,
1248 u64 orig_parent, u64 parent,
1249 u64 ref_root, u64 ref_generation,
1253 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1254 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1256 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1257 parent, ref_root, ref_root,
1258 ref_generation, ref_generation,
1263 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, u64 bytenr,
1265 u64 orig_parent, u64 parent,
1266 u64 orig_root, u64 ref_root,
1267 u64 orig_generation, u64 ref_generation,
1270 struct btrfs_path *path;
1272 struct btrfs_key key;
1273 struct extent_buffer *l;
1274 struct btrfs_extent_item *item;
1277 path = btrfs_alloc_path();
1282 key.objectid = bytenr;
1283 key.type = BTRFS_EXTENT_ITEM_KEY;
1284 key.offset = (u64)-1;
1286 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1290 BUG_ON(ret == 0 || path->slots[0] == 0);
1295 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1296 if (key.objectid != bytenr) {
1297 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1298 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1301 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1303 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1304 refs = btrfs_extent_refs(l, item);
1305 btrfs_set_extent_refs(l, item, refs + 1);
1306 btrfs_mark_buffer_dirty(path->nodes[0]);
1308 btrfs_release_path(root->fs_info->extent_root, path);
1311 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1312 path, bytenr, parent,
1313 ref_root, ref_generation,
1316 finish_current_insert(trans, root->fs_info->extent_root, 0);
1317 del_pending_extents(trans, root->fs_info->extent_root, 0);
1319 btrfs_free_path(path);
1323 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1324 struct btrfs_root *root,
1325 u64 bytenr, u64 num_bytes, u64 parent,
1326 u64 ref_root, u64 ref_generation,
1330 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1331 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1333 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1334 0, ref_root, 0, ref_generation,
1339 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root)
1342 finish_current_insert(trans, root->fs_info->extent_root, 1);
1343 del_pending_extents(trans, root->fs_info->extent_root, 1);
1347 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1348 struct btrfs_root *root, u64 bytenr,
1349 u64 num_bytes, u32 *refs)
1351 struct btrfs_path *path;
1353 struct btrfs_key key;
1354 struct extent_buffer *l;
1355 struct btrfs_extent_item *item;
1357 WARN_ON(num_bytes < root->sectorsize);
1358 path = btrfs_alloc_path();
1360 key.objectid = bytenr;
1361 key.offset = num_bytes;
1362 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1363 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1368 btrfs_print_leaf(root, path->nodes[0]);
1369 printk("failed to find block number %Lu\n", bytenr);
1373 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1374 *refs = btrfs_extent_refs(l, item);
1376 btrfs_free_path(path);
1380 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root, u64 bytenr)
1383 struct btrfs_root *extent_root = root->fs_info->extent_root;
1384 struct btrfs_path *path;
1385 struct extent_buffer *leaf;
1386 struct btrfs_extent_ref *ref_item;
1387 struct btrfs_key key;
1388 struct btrfs_key found_key;
1394 key.objectid = bytenr;
1395 key.offset = (u64)-1;
1396 key.type = BTRFS_EXTENT_ITEM_KEY;
1398 path = btrfs_alloc_path();
1399 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1405 if (path->slots[0] == 0)
1409 leaf = path->nodes[0];
1410 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1412 if (found_key.objectid != bytenr ||
1413 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1416 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1418 leaf = path->nodes[0];
1419 nritems = btrfs_header_nritems(leaf);
1420 if (path->slots[0] >= nritems) {
1421 ret = btrfs_next_leaf(extent_root, path);
1428 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1429 if (found_key.objectid != bytenr)
1432 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1437 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1438 struct btrfs_extent_ref);
1439 ref_root = btrfs_ref_root(leaf, ref_item);
1440 if (ref_root != root->root_key.objectid &&
1441 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1445 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1454 btrfs_free_path(path);
1458 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1459 struct extent_buffer *buf, u32 nr_extents)
1461 struct btrfs_key key;
1462 struct btrfs_file_extent_item *fi;
1470 if (!root->ref_cows)
1473 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1475 root_gen = root->root_key.offset;
1478 root_gen = trans->transid - 1;
1481 level = btrfs_header_level(buf);
1482 nritems = btrfs_header_nritems(buf);
1485 struct btrfs_leaf_ref *ref;
1486 struct btrfs_extent_info *info;
1488 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1494 ref->root_gen = root_gen;
1495 ref->bytenr = buf->start;
1496 ref->owner = btrfs_header_owner(buf);
1497 ref->generation = btrfs_header_generation(buf);
1498 ref->nritems = nr_extents;
1499 info = ref->extents;
1501 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1503 btrfs_item_key_to_cpu(buf, &key, i);
1504 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1506 fi = btrfs_item_ptr(buf, i,
1507 struct btrfs_file_extent_item);
1508 if (btrfs_file_extent_type(buf, fi) ==
1509 BTRFS_FILE_EXTENT_INLINE)
1511 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1512 if (disk_bytenr == 0)
1515 info->bytenr = disk_bytenr;
1517 btrfs_file_extent_disk_num_bytes(buf, fi);
1518 info->objectid = key.objectid;
1519 info->offset = key.offset;
1523 ret = btrfs_add_leaf_ref(root, ref, shared);
1524 if (ret == -EEXIST && shared) {
1525 struct btrfs_leaf_ref *old;
1526 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1528 btrfs_remove_leaf_ref(root, old);
1529 btrfs_free_leaf_ref(root, old);
1530 ret = btrfs_add_leaf_ref(root, ref, shared);
1533 btrfs_free_leaf_ref(root, ref);
1539 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1540 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1547 u64 orig_generation;
1549 u32 nr_file_extents = 0;
1550 struct btrfs_key key;
1551 struct btrfs_file_extent_item *fi;
1556 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1557 u64, u64, u64, u64, u64, u64, u64, u64);
1559 ref_root = btrfs_header_owner(buf);
1560 ref_generation = btrfs_header_generation(buf);
1561 orig_root = btrfs_header_owner(orig_buf);
1562 orig_generation = btrfs_header_generation(orig_buf);
1564 nritems = btrfs_header_nritems(buf);
1565 level = btrfs_header_level(buf);
1567 if (root->ref_cows) {
1568 process_func = __btrfs_inc_extent_ref;
1571 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1574 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1576 process_func = __btrfs_update_extent_ref;
1579 for (i = 0; i < nritems; i++) {
1582 btrfs_item_key_to_cpu(buf, &key, i);
1583 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1585 fi = btrfs_item_ptr(buf, i,
1586 struct btrfs_file_extent_item);
1587 if (btrfs_file_extent_type(buf, fi) ==
1588 BTRFS_FILE_EXTENT_INLINE)
1590 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1596 ret = process_func(trans, root, bytenr,
1597 orig_buf->start, buf->start,
1598 orig_root, ref_root,
1599 orig_generation, ref_generation,
1608 bytenr = btrfs_node_blockptr(buf, i);
1609 ret = process_func(trans, root, bytenr,
1610 orig_buf->start, buf->start,
1611 orig_root, ref_root,
1612 orig_generation, ref_generation,
1624 *nr_extents = nr_file_extents;
1626 *nr_extents = nritems;
1634 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1635 struct btrfs_root *root, struct extent_buffer *orig_buf,
1636 struct extent_buffer *buf, int start_slot, int nr)
1643 u64 orig_generation;
1644 struct btrfs_key key;
1645 struct btrfs_file_extent_item *fi;
1651 BUG_ON(start_slot < 0);
1652 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1654 ref_root = btrfs_header_owner(buf);
1655 ref_generation = btrfs_header_generation(buf);
1656 orig_root = btrfs_header_owner(orig_buf);
1657 orig_generation = btrfs_header_generation(orig_buf);
1658 level = btrfs_header_level(buf);
1660 if (!root->ref_cows) {
1662 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1665 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1669 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1672 btrfs_item_key_to_cpu(buf, &key, slot);
1673 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1675 fi = btrfs_item_ptr(buf, slot,
1676 struct btrfs_file_extent_item);
1677 if (btrfs_file_extent_type(buf, fi) ==
1678 BTRFS_FILE_EXTENT_INLINE)
1680 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1683 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1684 orig_buf->start, buf->start,
1685 orig_root, ref_root,
1686 orig_generation, ref_generation,
1691 bytenr = btrfs_node_blockptr(buf, slot);
1692 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1693 orig_buf->start, buf->start,
1694 orig_root, ref_root,
1695 orig_generation, ref_generation,
1707 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1708 struct btrfs_root *root,
1709 struct btrfs_path *path,
1710 struct btrfs_block_group_cache *cache)
1714 struct btrfs_root *extent_root = root->fs_info->extent_root;
1716 struct extent_buffer *leaf;
1718 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1723 leaf = path->nodes[0];
1724 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1725 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1726 btrfs_mark_buffer_dirty(leaf);
1727 btrfs_release_path(extent_root, path);
1729 finish_current_insert(trans, extent_root, 0);
1730 pending_ret = del_pending_extents(trans, extent_root, 0);
1739 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root)
1742 struct btrfs_block_group_cache *cache, *entry;
1746 struct btrfs_path *path;
1749 path = btrfs_alloc_path();
1755 spin_lock(&root->fs_info->block_group_cache_lock);
1756 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1757 n; n = rb_next(n)) {
1758 entry = rb_entry(n, struct btrfs_block_group_cache,
1765 spin_unlock(&root->fs_info->block_group_cache_lock);
1771 last += cache->key.offset;
1773 err = write_one_cache_group(trans, root,
1776 * if we fail to write the cache group, we want
1777 * to keep it marked dirty in hopes that a later
1785 btrfs_free_path(path);
1789 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1790 u64 total_bytes, u64 bytes_used,
1791 struct btrfs_space_info **space_info)
1793 struct btrfs_space_info *found;
1795 found = __find_space_info(info, flags);
1797 spin_lock(&found->lock);
1798 found->total_bytes += total_bytes;
1799 found->bytes_used += bytes_used;
1801 spin_unlock(&found->lock);
1802 *space_info = found;
1805 found = kzalloc(sizeof(*found), GFP_NOFS);
1809 list_add(&found->list, &info->space_info);
1810 INIT_LIST_HEAD(&found->block_groups);
1811 init_rwsem(&found->groups_sem);
1812 spin_lock_init(&found->lock);
1813 found->flags = flags;
1814 found->total_bytes = total_bytes;
1815 found->bytes_used = bytes_used;
1816 found->bytes_pinned = 0;
1817 found->bytes_reserved = 0;
1818 found->bytes_readonly = 0;
1820 found->force_alloc = 0;
1821 *space_info = found;
1825 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1827 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1828 BTRFS_BLOCK_GROUP_RAID1 |
1829 BTRFS_BLOCK_GROUP_RAID10 |
1830 BTRFS_BLOCK_GROUP_DUP);
1832 if (flags & BTRFS_BLOCK_GROUP_DATA)
1833 fs_info->avail_data_alloc_bits |= extra_flags;
1834 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1835 fs_info->avail_metadata_alloc_bits |= extra_flags;
1836 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1837 fs_info->avail_system_alloc_bits |= extra_flags;
1841 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1843 spin_lock(&cache->space_info->lock);
1844 spin_lock(&cache->lock);
1846 cache->space_info->bytes_readonly += cache->key.offset -
1847 btrfs_block_group_used(&cache->item);
1850 spin_unlock(&cache->lock);
1851 spin_unlock(&cache->space_info->lock);
1854 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1856 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1858 if (num_devices == 1)
1859 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1860 if (num_devices < 4)
1861 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1863 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1864 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1865 BTRFS_BLOCK_GROUP_RAID10))) {
1866 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1869 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1870 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1871 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1874 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1875 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1876 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1877 (flags & BTRFS_BLOCK_GROUP_DUP)))
1878 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1882 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1883 struct btrfs_root *extent_root, u64 alloc_bytes,
1884 u64 flags, int force)
1886 struct btrfs_space_info *space_info;
1890 mutex_lock(&extent_root->fs_info->chunk_mutex);
1892 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1894 space_info = __find_space_info(extent_root->fs_info, flags);
1896 ret = update_space_info(extent_root->fs_info, flags,
1900 BUG_ON(!space_info);
1902 spin_lock(&space_info->lock);
1903 if (space_info->force_alloc) {
1905 space_info->force_alloc = 0;
1907 if (space_info->full) {
1908 spin_unlock(&space_info->lock);
1912 thresh = space_info->total_bytes - space_info->bytes_readonly;
1913 thresh = div_factor(thresh, 6);
1915 (space_info->bytes_used + space_info->bytes_pinned +
1916 space_info->bytes_reserved + alloc_bytes) < thresh) {
1917 spin_unlock(&space_info->lock);
1920 spin_unlock(&space_info->lock);
1922 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1924 printk("space info full %Lu\n", flags);
1925 space_info->full = 1;
1928 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1932 static int update_block_group(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root,
1934 u64 bytenr, u64 num_bytes, int alloc,
1937 struct btrfs_block_group_cache *cache;
1938 struct btrfs_fs_info *info = root->fs_info;
1939 u64 total = num_bytes;
1944 cache = btrfs_lookup_block_group(info, bytenr);
1947 byte_in_group = bytenr - cache->key.objectid;
1948 WARN_ON(byte_in_group > cache->key.offset);
1950 spin_lock(&cache->space_info->lock);
1951 spin_lock(&cache->lock);
1953 old_val = btrfs_block_group_used(&cache->item);
1954 num_bytes = min(total, cache->key.offset - byte_in_group);
1956 old_val += num_bytes;
1957 cache->space_info->bytes_used += num_bytes;
1959 cache->space_info->bytes_readonly -= num_bytes;
1962 btrfs_set_block_group_used(&cache->item, old_val);
1963 spin_unlock(&cache->lock);
1964 spin_unlock(&cache->space_info->lock);
1966 old_val -= num_bytes;
1967 cache->space_info->bytes_used -= num_bytes;
1969 cache->space_info->bytes_readonly += num_bytes;
1970 btrfs_set_block_group_used(&cache->item, old_val);
1971 spin_unlock(&cache->lock);
1972 spin_unlock(&cache->space_info->lock);
1975 ret = btrfs_add_free_space(cache, bytenr,
1982 bytenr += num_bytes;
1987 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1989 struct btrfs_block_group_cache *cache;
1991 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1995 return cache->key.objectid;
1998 int btrfs_update_pinned_extents(struct btrfs_root *root,
1999 u64 bytenr, u64 num, int pin)
2002 struct btrfs_block_group_cache *cache;
2003 struct btrfs_fs_info *fs_info = root->fs_info;
2005 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2007 set_extent_dirty(&fs_info->pinned_extents,
2008 bytenr, bytenr + num - 1, GFP_NOFS);
2010 clear_extent_dirty(&fs_info->pinned_extents,
2011 bytenr, bytenr + num - 1, GFP_NOFS);
2014 cache = btrfs_lookup_block_group(fs_info, bytenr);
2016 len = min(num, cache->key.offset -
2017 (bytenr - cache->key.objectid));
2019 spin_lock(&cache->space_info->lock);
2020 spin_lock(&cache->lock);
2021 cache->pinned += len;
2022 cache->space_info->bytes_pinned += len;
2023 spin_unlock(&cache->lock);
2024 spin_unlock(&cache->space_info->lock);
2025 fs_info->total_pinned += len;
2027 spin_lock(&cache->space_info->lock);
2028 spin_lock(&cache->lock);
2029 cache->pinned -= len;
2030 cache->space_info->bytes_pinned -= len;
2031 spin_unlock(&cache->lock);
2032 spin_unlock(&cache->space_info->lock);
2033 fs_info->total_pinned -= len;
2041 static int update_reserved_extents(struct btrfs_root *root,
2042 u64 bytenr, u64 num, int reserve)
2045 struct btrfs_block_group_cache *cache;
2046 struct btrfs_fs_info *fs_info = root->fs_info;
2049 cache = btrfs_lookup_block_group(fs_info, bytenr);
2051 len = min(num, cache->key.offset -
2052 (bytenr - cache->key.objectid));
2054 spin_lock(&cache->space_info->lock);
2055 spin_lock(&cache->lock);
2057 cache->reserved += len;
2058 cache->space_info->bytes_reserved += len;
2060 cache->reserved -= len;
2061 cache->space_info->bytes_reserved -= len;
2063 spin_unlock(&cache->lock);
2064 spin_unlock(&cache->space_info->lock);
2071 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2076 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2079 mutex_lock(&root->fs_info->pinned_mutex);
2081 ret = find_first_extent_bit(pinned_extents, last,
2082 &start, &end, EXTENT_DIRTY);
2085 set_extent_dirty(copy, start, end, GFP_NOFS);
2088 mutex_unlock(&root->fs_info->pinned_mutex);
2092 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2093 struct btrfs_root *root,
2094 struct extent_io_tree *unpin)
2099 struct btrfs_block_group_cache *cache;
2101 mutex_lock(&root->fs_info->pinned_mutex);
2103 ret = find_first_extent_bit(unpin, 0, &start, &end,
2107 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2108 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2109 cache = btrfs_lookup_block_group(root->fs_info, start);
2111 btrfs_add_free_space(cache, start, end - start + 1);
2112 if (need_resched()) {
2113 mutex_unlock(&root->fs_info->pinned_mutex);
2115 mutex_lock(&root->fs_info->pinned_mutex);
2118 mutex_unlock(&root->fs_info->pinned_mutex);
2122 static int finish_current_insert(struct btrfs_trans_handle *trans,
2123 struct btrfs_root *extent_root, int all)
2130 struct btrfs_fs_info *info = extent_root->fs_info;
2131 struct btrfs_path *path;
2132 struct pending_extent_op *extent_op, *tmp;
2133 struct list_head insert_list, update_list;
2135 int num_inserts = 0, max_inserts;
2137 path = btrfs_alloc_path();
2138 INIT_LIST_HEAD(&insert_list);
2139 INIT_LIST_HEAD(&update_list);
2141 max_inserts = extent_root->leafsize /
2142 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2143 sizeof(struct btrfs_extent_ref) +
2144 sizeof(struct btrfs_extent_item));
2146 mutex_lock(&info->extent_ins_mutex);
2148 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2149 &end, EXTENT_WRITEBACK);
2151 if (skipped && all && !num_inserts) {
2155 mutex_unlock(&info->extent_ins_mutex);
2159 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2163 if (need_resched()) {
2164 mutex_unlock(&info->extent_ins_mutex);
2166 mutex_lock(&info->extent_ins_mutex);
2171 ret = get_state_private(&info->extent_ins, start, &priv);
2173 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2175 if (extent_op->type == PENDING_EXTENT_INSERT) {
2177 list_add_tail(&extent_op->list, &insert_list);
2179 if (num_inserts == max_inserts) {
2180 mutex_unlock(&info->extent_ins_mutex);
2183 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2184 list_add_tail(&extent_op->list, &update_list);
2192 * process teh update list, clear the writeback bit for it, and if
2193 * somebody marked this thing for deletion then just unlock it and be
2194 * done, the free_extents will handle it
2196 mutex_lock(&info->extent_ins_mutex);
2197 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2198 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2199 extent_op->bytenr + extent_op->num_bytes - 1,
2200 EXTENT_WRITEBACK, GFP_NOFS);
2201 if (extent_op->del) {
2202 list_del_init(&extent_op->list);
2203 unlock_extent(&info->extent_ins, extent_op->bytenr,
2204 extent_op->bytenr + extent_op->num_bytes
2209 mutex_unlock(&info->extent_ins_mutex);
2212 * still have things left on the update list, go ahead an update
2215 if (!list_empty(&update_list)) {
2216 ret = update_backrefs(trans, extent_root, path, &update_list);
2221 * if no inserts need to be done, but we skipped some extents and we
2222 * need to make sure everything is cleaned then reset everything and
2223 * go back to the beginning
2225 if (!num_inserts && all && skipped) {
2228 INIT_LIST_HEAD(&update_list);
2229 INIT_LIST_HEAD(&insert_list);
2231 } else if (!num_inserts) {
2236 * process the insert extents list. Again if we are deleting this
2237 * extent, then just unlock it, pin down the bytes if need be, and be
2238 * done with it. Saves us from having to actually insert the extent
2239 * into the tree and then subsequently come along and delete it
2241 mutex_lock(&info->extent_ins_mutex);
2242 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2243 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2244 extent_op->bytenr + extent_op->num_bytes - 1,
2245 EXTENT_WRITEBACK, GFP_NOFS);
2246 if (extent_op->del) {
2247 list_del_init(&extent_op->list);
2248 unlock_extent(&info->extent_ins, extent_op->bytenr,
2249 extent_op->bytenr + extent_op->num_bytes
2252 mutex_lock(&extent_root->fs_info->pinned_mutex);
2253 ret = pin_down_bytes(trans, extent_root,
2255 extent_op->num_bytes, 0);
2256 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2258 ret = update_block_group(trans, extent_root,
2260 extent_op->num_bytes,
2267 mutex_unlock(&info->extent_ins_mutex);
2269 ret = insert_extents(trans, extent_root, path, &insert_list,
2274 * if we broke out of the loop in order to insert stuff because we hit
2275 * the maximum number of inserts at a time we can handle, then loop
2276 * back and pick up where we left off
2278 if (num_inserts == max_inserts) {
2279 INIT_LIST_HEAD(&insert_list);
2280 INIT_LIST_HEAD(&update_list);
2286 * again, if we need to make absolutely sure there are no more pending
2287 * extent operations left and we know that we skipped some, go back to
2288 * the beginning and do it all again
2290 if (all && skipped) {
2291 INIT_LIST_HEAD(&insert_list);
2292 INIT_LIST_HEAD(&update_list);
2299 btrfs_free_path(path);
2303 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2304 struct btrfs_root *root,
2305 u64 bytenr, u64 num_bytes, int is_data)
2308 struct extent_buffer *buf;
2313 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2317 /* we can reuse a block if it hasn't been written
2318 * and it is from this transaction. We can't
2319 * reuse anything from the tree log root because
2320 * it has tiny sub-transactions.
2322 if (btrfs_buffer_uptodate(buf, 0) &&
2323 btrfs_try_tree_lock(buf)) {
2324 u64 header_owner = btrfs_header_owner(buf);
2325 u64 header_transid = btrfs_header_generation(buf);
2326 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2327 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2328 header_transid == trans->transid &&
2329 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2330 clean_tree_block(NULL, root, buf);
2331 btrfs_tree_unlock(buf);
2332 free_extent_buffer(buf);
2335 btrfs_tree_unlock(buf);
2337 free_extent_buffer(buf);
2339 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2346 * remove an extent from the root, returns 0 on success
2348 static int __free_extent(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *root,
2350 u64 bytenr, u64 num_bytes, u64 parent,
2351 u64 root_objectid, u64 ref_generation,
2352 u64 owner_objectid, int pin, int mark_free)
2354 struct btrfs_path *path;
2355 struct btrfs_key key;
2356 struct btrfs_fs_info *info = root->fs_info;
2357 struct btrfs_root *extent_root = info->extent_root;
2358 struct extent_buffer *leaf;
2360 int extent_slot = 0;
2361 int found_extent = 0;
2363 struct btrfs_extent_item *ei;
2366 key.objectid = bytenr;
2367 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2368 key.offset = num_bytes;
2369 path = btrfs_alloc_path();
2374 ret = lookup_extent_backref(trans, extent_root, path,
2375 bytenr, parent, root_objectid,
2376 ref_generation, owner_objectid, 1);
2378 struct btrfs_key found_key;
2379 extent_slot = path->slots[0];
2380 while(extent_slot > 0) {
2382 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2384 if (found_key.objectid != bytenr)
2386 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2387 found_key.offset == num_bytes) {
2391 if (path->slots[0] - extent_slot > 5)
2394 if (!found_extent) {
2395 ret = remove_extent_backref(trans, extent_root, path);
2397 btrfs_release_path(extent_root, path);
2398 ret = btrfs_search_slot(trans, extent_root,
2401 printk(KERN_ERR "umm, got %d back from search"
2402 ", was looking for %Lu\n", ret,
2404 btrfs_print_leaf(extent_root, path->nodes[0]);
2407 extent_slot = path->slots[0];
2410 btrfs_print_leaf(extent_root, path->nodes[0]);
2412 printk("Unable to find ref byte nr %Lu root %Lu "
2413 "gen %Lu owner %Lu\n", bytenr,
2414 root_objectid, ref_generation, owner_objectid);
2417 leaf = path->nodes[0];
2418 ei = btrfs_item_ptr(leaf, extent_slot,
2419 struct btrfs_extent_item);
2420 refs = btrfs_extent_refs(leaf, ei);
2423 btrfs_set_extent_refs(leaf, ei, refs);
2425 btrfs_mark_buffer_dirty(leaf);
2427 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2428 struct btrfs_extent_ref *ref;
2429 ref = btrfs_item_ptr(leaf, path->slots[0],
2430 struct btrfs_extent_ref);
2431 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2432 /* if the back ref and the extent are next to each other
2433 * they get deleted below in one shot
2435 path->slots[0] = extent_slot;
2437 } else if (found_extent) {
2438 /* otherwise delete the extent back ref */
2439 ret = remove_extent_backref(trans, extent_root, path);
2441 /* if refs are 0, we need to setup the path for deletion */
2443 btrfs_release_path(extent_root, path);
2444 ret = btrfs_search_slot(trans, extent_root, &key, path,
2453 #ifdef BIO_RW_DISCARD
2454 u64 map_length = num_bytes;
2455 struct btrfs_multi_bio *multi = NULL;
2459 mutex_lock(&root->fs_info->pinned_mutex);
2460 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2461 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2462 mutex_unlock(&root->fs_info->pinned_mutex);
2468 /* block accounting for super block */
2469 spin_lock_irq(&info->delalloc_lock);
2470 super_used = btrfs_super_bytes_used(&info->super_copy);
2471 btrfs_set_super_bytes_used(&info->super_copy,
2472 super_used - num_bytes);
2473 spin_unlock_irq(&info->delalloc_lock);
2475 /* block accounting for root item */
2476 root_used = btrfs_root_used(&root->root_item);
2477 btrfs_set_root_used(&root->root_item,
2478 root_used - num_bytes);
2479 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2482 btrfs_release_path(extent_root, path);
2483 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2487 #ifdef BIO_RW_DISCARD
2488 /* Tell the block device(s) that the sectors can be discarded */
2489 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2490 bytenr, &map_length, &multi, 0);
2492 struct btrfs_bio_stripe *stripe = multi->stripes;
2495 if (map_length > num_bytes)
2496 map_length = num_bytes;
2498 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2499 blkdev_issue_discard(stripe->dev->bdev,
2500 stripe->physical >> 9,
2507 btrfs_free_path(path);
2508 finish_current_insert(trans, extent_root, 0);
2513 * find all the blocks marked as pending in the radix tree and remove
2514 * them from the extent map
2516 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2517 btrfs_root *extent_root, int all)
2525 int nr = 0, skipped = 0;
2526 struct extent_io_tree *pending_del;
2527 struct extent_io_tree *extent_ins;
2528 struct pending_extent_op *extent_op;
2529 struct btrfs_fs_info *info = extent_root->fs_info;
2530 struct list_head delete_list;
2532 INIT_LIST_HEAD(&delete_list);
2533 extent_ins = &extent_root->fs_info->extent_ins;
2534 pending_del = &extent_root->fs_info->pending_del;
2537 mutex_lock(&info->extent_ins_mutex);
2539 ret = find_first_extent_bit(pending_del, search, &start, &end,
2542 if (all && skipped && !nr) {
2546 mutex_unlock(&info->extent_ins_mutex);
2550 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2555 if (need_resched()) {
2556 mutex_unlock(&info->extent_ins_mutex);
2558 mutex_lock(&info->extent_ins_mutex);
2565 ret = get_state_private(pending_del, start, &priv);
2567 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2569 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2571 if (!test_range_bit(extent_ins, start, end,
2572 EXTENT_WRITEBACK, 0)) {
2573 list_add_tail(&extent_op->list, &delete_list);
2578 ret = get_state_private(&info->extent_ins, start,
2581 extent_op = (struct pending_extent_op *)
2582 (unsigned long)priv;
2584 clear_extent_bits(&info->extent_ins, start, end,
2585 EXTENT_WRITEBACK, GFP_NOFS);
2587 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2588 list_add_tail(&extent_op->list, &delete_list);
2594 mutex_lock(&extent_root->fs_info->pinned_mutex);
2595 ret = pin_down_bytes(trans, extent_root, start,
2596 end + 1 - start, 0);
2597 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2599 ret = update_block_group(trans, extent_root, start,
2600 end + 1 - start, 0, ret > 0);
2602 unlock_extent(extent_ins, start, end, GFP_NOFS);
2611 if (need_resched()) {
2612 mutex_unlock(&info->extent_ins_mutex);
2614 mutex_lock(&info->extent_ins_mutex);
2619 ret = free_extents(trans, extent_root, &delete_list);
2623 if (all && skipped) {
2624 INIT_LIST_HEAD(&delete_list);
2634 * remove an extent from the root, returns 0 on success
2636 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2637 struct btrfs_root *root,
2638 u64 bytenr, u64 num_bytes, u64 parent,
2639 u64 root_objectid, u64 ref_generation,
2640 u64 owner_objectid, int pin)
2642 struct btrfs_root *extent_root = root->fs_info->extent_root;
2646 WARN_ON(num_bytes < root->sectorsize);
2647 if (root == extent_root) {
2648 struct pending_extent_op *extent_op = NULL;
2650 mutex_lock(&root->fs_info->extent_ins_mutex);
2651 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2652 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2654 ret = get_state_private(&root->fs_info->extent_ins,
2657 extent_op = (struct pending_extent_op *)
2658 (unsigned long)priv;
2661 if (extent_op->type == PENDING_EXTENT_INSERT) {
2662 mutex_unlock(&root->fs_info->extent_ins_mutex);
2668 ref_generation = extent_op->orig_generation;
2669 parent = extent_op->orig_parent;
2672 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2675 extent_op->type = PENDING_EXTENT_DELETE;
2676 extent_op->bytenr = bytenr;
2677 extent_op->num_bytes = num_bytes;
2678 extent_op->parent = parent;
2679 extent_op->orig_parent = parent;
2680 extent_op->generation = ref_generation;
2681 extent_op->orig_generation = ref_generation;
2682 extent_op->level = (int)owner_objectid;
2683 INIT_LIST_HEAD(&extent_op->list);
2686 set_extent_bits(&root->fs_info->pending_del,
2687 bytenr, bytenr + num_bytes - 1,
2688 EXTENT_WRITEBACK, GFP_NOFS);
2689 set_state_private(&root->fs_info->pending_del,
2690 bytenr, (unsigned long)extent_op);
2691 mutex_unlock(&root->fs_info->extent_ins_mutex);
2694 /* if metadata always pin */
2695 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2696 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2697 struct btrfs_block_group_cache *cache;
2699 /* btrfs_free_reserved_extent */
2700 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2702 btrfs_add_free_space(cache, bytenr, num_bytes);
2703 update_reserved_extents(root, bytenr, num_bytes, 0);
2709 /* if data pin when any transaction has committed this */
2710 if (ref_generation != trans->transid)
2713 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2714 root_objectid, ref_generation,
2715 owner_objectid, pin, pin == 0);
2717 finish_current_insert(trans, root->fs_info->extent_root, 0);
2718 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2719 return ret ? ret : pending_ret;
2722 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2723 struct btrfs_root *root,
2724 u64 bytenr, u64 num_bytes, u64 parent,
2725 u64 root_objectid, u64 ref_generation,
2726 u64 owner_objectid, int pin)
2730 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2731 root_objectid, ref_generation,
2732 owner_objectid, pin);
2736 static u64 stripe_align(struct btrfs_root *root, u64 val)
2738 u64 mask = ((u64)root->stripesize - 1);
2739 u64 ret = (val + mask) & ~mask;
2744 * walks the btree of allocated extents and find a hole of a given size.
2745 * The key ins is changed to record the hole:
2746 * ins->objectid == block start
2747 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2748 * ins->offset == number of blocks
2749 * Any available blocks before search_start are skipped.
2751 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2752 struct btrfs_root *orig_root,
2753 u64 num_bytes, u64 empty_size,
2754 u64 search_start, u64 search_end,
2755 u64 hint_byte, struct btrfs_key *ins,
2756 u64 exclude_start, u64 exclude_nr,
2760 struct btrfs_root * root = orig_root->fs_info->extent_root;
2761 u64 total_needed = num_bytes;
2762 u64 *last_ptr = NULL;
2763 u64 last_wanted = 0;
2764 struct btrfs_block_group_cache *block_group = NULL;
2765 int chunk_alloc_done = 0;
2766 int empty_cluster = 2 * 1024 * 1024;
2767 int allowed_chunk_alloc = 0;
2768 struct list_head *head = NULL, *cur = NULL;
2771 struct btrfs_space_info *space_info;
2773 WARN_ON(num_bytes < root->sectorsize);
2774 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2778 if (orig_root->ref_cows || empty_size)
2779 allowed_chunk_alloc = 1;
2781 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2782 last_ptr = &root->fs_info->last_alloc;
2783 empty_cluster = 64 * 1024;
2786 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2787 last_ptr = &root->fs_info->last_data_alloc;
2791 hint_byte = *last_ptr;
2792 last_wanted = *last_ptr;
2794 empty_size += empty_cluster;
2798 search_start = max(search_start, first_logical_byte(root, 0));
2799 search_start = max(search_start, hint_byte);
2801 if (last_wanted && search_start != last_wanted) {
2803 empty_size += empty_cluster;
2806 total_needed += empty_size;
2807 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2809 block_group = btrfs_lookup_first_block_group(root->fs_info,
2811 space_info = __find_space_info(root->fs_info, data);
2813 down_read(&space_info->groups_sem);
2815 struct btrfs_free_space *free_space;
2817 * the only way this happens if our hint points to a block
2818 * group thats not of the proper type, while looping this
2819 * should never happen
2825 goto new_group_no_lock;
2827 mutex_lock(&block_group->alloc_mutex);
2828 if (unlikely(!block_group_bits(block_group, data)))
2831 ret = cache_block_group(root, block_group);
2833 mutex_unlock(&block_group->alloc_mutex);
2837 if (block_group->ro)
2840 free_space = btrfs_find_free_space(block_group, search_start,
2843 u64 start = block_group->key.objectid;
2844 u64 end = block_group->key.objectid +
2845 block_group->key.offset;
2847 search_start = stripe_align(root, free_space->offset);
2849 /* move on to the next group */
2850 if (search_start + num_bytes >= search_end)
2853 /* move on to the next group */
2854 if (search_start + num_bytes > end)
2857 if (last_wanted && search_start != last_wanted) {
2858 total_needed += empty_cluster;
2859 empty_size += empty_cluster;
2862 * if search_start is still in this block group
2863 * then we just re-search this block group
2865 if (search_start >= start &&
2866 search_start < end) {
2867 mutex_unlock(&block_group->alloc_mutex);
2871 /* else we go to the next block group */
2875 if (exclude_nr > 0 &&
2876 (search_start + num_bytes > exclude_start &&
2877 search_start < exclude_start + exclude_nr)) {
2878 search_start = exclude_start + exclude_nr;
2880 * if search_start is still in this block group
2881 * then we just re-search this block group
2883 if (search_start >= start &&
2884 search_start < end) {
2885 mutex_unlock(&block_group->alloc_mutex);
2890 /* else we go to the next block group */
2894 ins->objectid = search_start;
2895 ins->offset = num_bytes;
2897 btrfs_remove_free_space_lock(block_group, search_start,
2899 /* we are all good, lets return */
2900 mutex_unlock(&block_group->alloc_mutex);
2904 mutex_unlock(&block_group->alloc_mutex);
2906 /* don't try to compare new allocations against the
2907 * last allocation any more
2912 * Here's how this works.
2913 * loop == 0: we were searching a block group via a hint
2914 * and didn't find anything, so we start at
2915 * the head of the block groups and keep searching
2916 * loop == 1: we're searching through all of the block groups
2917 * if we hit the head again we have searched
2918 * all of the block groups for this space and we
2919 * need to try and allocate, if we cant error out.
2920 * loop == 2: we allocated more space and are looping through
2921 * all of the block groups again.
2924 head = &space_info->block_groups;
2927 } else if (loop == 1 && cur == head) {
2930 /* at this point we give up on the empty_size
2931 * allocations and just try to allocate the min
2934 * The extra_loop field was set if an empty_size
2935 * allocation was attempted above, and if this
2936 * is try we need to try the loop again without
2937 * the additional empty_size.
2939 total_needed -= empty_size;
2941 keep_going = extra_loop;
2944 if (allowed_chunk_alloc && !chunk_alloc_done) {
2945 up_read(&space_info->groups_sem);
2946 ret = do_chunk_alloc(trans, root, num_bytes +
2947 2 * 1024 * 1024, data, 1);
2948 down_read(&space_info->groups_sem);
2951 head = &space_info->block_groups;
2953 * we've allocated a new chunk, keep
2957 chunk_alloc_done = 1;
2958 } else if (!allowed_chunk_alloc) {
2959 space_info->force_alloc = 1;
2968 } else if (cur == head) {
2972 block_group = list_entry(cur, struct btrfs_block_group_cache,
2974 search_start = block_group->key.objectid;
2978 /* we found what we needed */
2979 if (ins->objectid) {
2980 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2981 trans->block_group = block_group;
2984 *last_ptr = ins->objectid + ins->offset;
2987 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
2988 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
2989 loop, allowed_chunk_alloc);
2993 up_read(&space_info->groups_sem);
2997 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2999 struct btrfs_block_group_cache *cache;
3000 struct list_head *l;
3002 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
3003 info->total_bytes - info->bytes_used - info->bytes_pinned -
3004 info->bytes_reserved, (info->full) ? "" : "not ");
3006 down_read(&info->groups_sem);
3007 list_for_each(l, &info->block_groups) {
3008 cache = list_entry(l, struct btrfs_block_group_cache, list);
3009 spin_lock(&cache->lock);
3010 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
3011 "%Lu pinned %Lu reserved\n",
3012 cache->key.objectid, cache->key.offset,
3013 btrfs_block_group_used(&cache->item),
3014 cache->pinned, cache->reserved);
3015 btrfs_dump_free_space(cache, bytes);
3016 spin_unlock(&cache->lock);
3018 up_read(&info->groups_sem);
3021 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 u64 num_bytes, u64 min_alloc_size,
3024 u64 empty_size, u64 hint_byte,
3025 u64 search_end, struct btrfs_key *ins,
3029 u64 search_start = 0;
3031 struct btrfs_fs_info *info = root->fs_info;
3034 alloc_profile = info->avail_data_alloc_bits &
3035 info->data_alloc_profile;
3036 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3037 } else if (root == root->fs_info->chunk_root) {
3038 alloc_profile = info->avail_system_alloc_bits &
3039 info->system_alloc_profile;
3040 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3042 alloc_profile = info->avail_metadata_alloc_bits &
3043 info->metadata_alloc_profile;
3044 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3047 data = btrfs_reduce_alloc_profile(root, data);
3049 * the only place that sets empty_size is btrfs_realloc_node, which
3050 * is not called recursively on allocations
3052 if (empty_size || root->ref_cows) {
3053 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3054 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3056 BTRFS_BLOCK_GROUP_METADATA |
3057 (info->metadata_alloc_profile &
3058 info->avail_metadata_alloc_bits), 0);
3060 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3061 num_bytes + 2 * 1024 * 1024, data, 0);
3064 WARN_ON(num_bytes < root->sectorsize);
3065 ret = find_free_extent(trans, root, num_bytes, empty_size,
3066 search_start, search_end, hint_byte, ins,
3067 trans->alloc_exclude_start,
3068 trans->alloc_exclude_nr, data);
3070 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3071 num_bytes = num_bytes >> 1;
3072 num_bytes = num_bytes & ~(root->sectorsize - 1);
3073 num_bytes = max(num_bytes, min_alloc_size);
3074 do_chunk_alloc(trans, root->fs_info->extent_root,
3075 num_bytes, data, 1);
3079 struct btrfs_space_info *sinfo;
3081 sinfo = __find_space_info(root->fs_info, data);
3082 printk("allocation failed flags %Lu, wanted %Lu\n",
3084 dump_space_info(sinfo, num_bytes);
3091 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3093 struct btrfs_block_group_cache *cache;
3095 cache = btrfs_lookup_block_group(root->fs_info, start);
3097 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
3100 btrfs_add_free_space(cache, start, len);
3101 update_reserved_extents(root, start, len, 0);
3105 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3106 struct btrfs_root *root,
3107 u64 num_bytes, u64 min_alloc_size,
3108 u64 empty_size, u64 hint_byte,
3109 u64 search_end, struct btrfs_key *ins,
3113 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3114 empty_size, hint_byte, search_end, ins,
3116 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3120 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3121 struct btrfs_root *root, u64 parent,
3122 u64 root_objectid, u64 ref_generation,
3123 u64 owner, struct btrfs_key *ins)
3129 u64 num_bytes = ins->offset;
3131 struct btrfs_fs_info *info = root->fs_info;
3132 struct btrfs_root *extent_root = info->extent_root;
3133 struct btrfs_extent_item *extent_item;
3134 struct btrfs_extent_ref *ref;
3135 struct btrfs_path *path;
3136 struct btrfs_key keys[2];
3139 parent = ins->objectid;
3141 /* block accounting for super block */
3142 spin_lock_irq(&info->delalloc_lock);
3143 super_used = btrfs_super_bytes_used(&info->super_copy);
3144 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3145 spin_unlock_irq(&info->delalloc_lock);
3147 /* block accounting for root item */
3148 root_used = btrfs_root_used(&root->root_item);
3149 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3151 if (root == extent_root) {
3152 struct pending_extent_op *extent_op;
3154 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3157 extent_op->type = PENDING_EXTENT_INSERT;
3158 extent_op->bytenr = ins->objectid;
3159 extent_op->num_bytes = ins->offset;
3160 extent_op->parent = parent;
3161 extent_op->orig_parent = 0;
3162 extent_op->generation = ref_generation;
3163 extent_op->orig_generation = 0;
3164 extent_op->level = (int)owner;
3165 INIT_LIST_HEAD(&extent_op->list);
3168 mutex_lock(&root->fs_info->extent_ins_mutex);
3169 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3170 ins->objectid + ins->offset - 1,
3171 EXTENT_WRITEBACK, GFP_NOFS);
3172 set_state_private(&root->fs_info->extent_ins,
3173 ins->objectid, (unsigned long)extent_op);
3174 mutex_unlock(&root->fs_info->extent_ins_mutex);
3178 memcpy(&keys[0], ins, sizeof(*ins));
3179 keys[1].objectid = ins->objectid;
3180 keys[1].type = BTRFS_EXTENT_REF_KEY;
3181 keys[1].offset = parent;
3182 sizes[0] = sizeof(*extent_item);
3183 sizes[1] = sizeof(*ref);
3185 path = btrfs_alloc_path();
3188 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3192 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3193 struct btrfs_extent_item);
3194 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3195 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3196 struct btrfs_extent_ref);
3198 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3199 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3200 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3201 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3203 btrfs_mark_buffer_dirty(path->nodes[0]);
3205 trans->alloc_exclude_start = 0;
3206 trans->alloc_exclude_nr = 0;
3207 btrfs_free_path(path);
3208 finish_current_insert(trans, extent_root, 0);
3209 pending_ret = del_pending_extents(trans, extent_root, 0);
3219 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
3221 printk("update block group failed for %Lu %Lu\n",
3222 ins->objectid, ins->offset);
3229 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *root, u64 parent,
3231 u64 root_objectid, u64 ref_generation,
3232 u64 owner, struct btrfs_key *ins)
3236 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3238 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3239 ref_generation, owner, ins);
3240 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3245 * this is used by the tree logging recovery code. It records that
3246 * an extent has been allocated and makes sure to clear the free
3247 * space cache bits as well
3249 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3250 struct btrfs_root *root, u64 parent,
3251 u64 root_objectid, u64 ref_generation,
3252 u64 owner, struct btrfs_key *ins)
3255 struct btrfs_block_group_cache *block_group;
3257 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3258 mutex_lock(&block_group->alloc_mutex);
3259 cache_block_group(root, block_group);
3261 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3263 mutex_unlock(&block_group->alloc_mutex);
3265 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3266 ref_generation, owner, ins);
3271 * finds a free extent and does all the dirty work required for allocation
3272 * returns the key for the extent through ins, and a tree buffer for
3273 * the first block of the extent through buf.
3275 * returns 0 if everything worked, non-zero otherwise.
3277 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3278 struct btrfs_root *root,
3279 u64 num_bytes, u64 parent, u64 min_alloc_size,
3280 u64 root_objectid, u64 ref_generation,
3281 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3282 u64 search_end, struct btrfs_key *ins, u64 data)
3286 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3287 min_alloc_size, empty_size, hint_byte,
3288 search_end, ins, data);
3290 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3291 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3292 root_objectid, ref_generation,
3293 owner_objectid, ins);
3297 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3302 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3303 struct btrfs_root *root,
3304 u64 bytenr, u32 blocksize)
3306 struct extent_buffer *buf;
3308 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3310 return ERR_PTR(-ENOMEM);
3311 btrfs_set_header_generation(buf, trans->transid);
3312 btrfs_tree_lock(buf);
3313 clean_tree_block(trans, root, buf);
3314 btrfs_set_buffer_uptodate(buf);
3315 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3316 set_extent_dirty(&root->dirty_log_pages, buf->start,
3317 buf->start + buf->len - 1, GFP_NOFS);
3319 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3320 buf->start + buf->len - 1, GFP_NOFS);
3322 trans->blocks_used++;
3327 * helper function to allocate a block for a given tree
3328 * returns the tree buffer or NULL.
3330 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3331 struct btrfs_root *root,
3332 u32 blocksize, u64 parent,
3339 struct btrfs_key ins;
3341 struct extent_buffer *buf;
3343 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3344 root_objectid, ref_generation, level,
3345 empty_size, hint, (u64)-1, &ins, 0);
3348 return ERR_PTR(ret);
3351 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3355 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3356 struct btrfs_root *root, struct extent_buffer *leaf)
3359 u64 leaf_generation;
3360 struct btrfs_key key;
3361 struct btrfs_file_extent_item *fi;
3366 BUG_ON(!btrfs_is_leaf(leaf));
3367 nritems = btrfs_header_nritems(leaf);
3368 leaf_owner = btrfs_header_owner(leaf);
3369 leaf_generation = btrfs_header_generation(leaf);
3371 for (i = 0; i < nritems; i++) {
3375 btrfs_item_key_to_cpu(leaf, &key, i);
3376 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3378 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3379 if (btrfs_file_extent_type(leaf, fi) ==
3380 BTRFS_FILE_EXTENT_INLINE)
3383 * FIXME make sure to insert a trans record that
3384 * repeats the snapshot del on crash
3386 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3387 if (disk_bytenr == 0)
3390 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3391 btrfs_file_extent_disk_num_bytes(leaf, fi),
3392 leaf->start, leaf_owner, leaf_generation,
3396 atomic_inc(&root->fs_info->throttle_gen);
3397 wake_up(&root->fs_info->transaction_throttle);
3403 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3404 struct btrfs_root *root,
3405 struct btrfs_leaf_ref *ref)
3409 struct btrfs_extent_info *info = ref->extents;
3411 for (i = 0; i < ref->nritems; i++) {
3412 ret = __btrfs_free_extent(trans, root, info->bytenr,
3413 info->num_bytes, ref->bytenr,
3414 ref->owner, ref->generation,
3417 atomic_inc(&root->fs_info->throttle_gen);
3418 wake_up(&root->fs_info->transaction_throttle);
3428 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3433 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3436 #if 0 // some debugging code in case we see problems here
3437 /* if the refs count is one, it won't get increased again. But
3438 * if the ref count is > 1, someone may be decreasing it at
3439 * the same time we are.
3442 struct extent_buffer *eb = NULL;
3443 eb = btrfs_find_create_tree_block(root, start, len);
3445 btrfs_tree_lock(eb);
3447 mutex_lock(&root->fs_info->alloc_mutex);
3448 ret = lookup_extent_ref(NULL, root, start, len, refs);
3450 mutex_unlock(&root->fs_info->alloc_mutex);
3453 btrfs_tree_unlock(eb);
3454 free_extent_buffer(eb);
3457 printk("block %llu went down to one during drop_snap\n",
3458 (unsigned long long)start);
3469 * helper function for drop_snapshot, this walks down the tree dropping ref
3470 * counts as it goes.
3472 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3473 struct btrfs_root *root,
3474 struct btrfs_path *path, int *level)
3480 struct extent_buffer *next;
3481 struct extent_buffer *cur;
3482 struct extent_buffer *parent;
3483 struct btrfs_leaf_ref *ref;
3488 WARN_ON(*level < 0);
3489 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3490 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3491 path->nodes[*level]->len, &refs);
3497 * walk down to the last node level and free all the leaves
3499 while(*level >= 0) {
3500 WARN_ON(*level < 0);
3501 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3502 cur = path->nodes[*level];
3504 if (btrfs_header_level(cur) != *level)
3507 if (path->slots[*level] >=
3508 btrfs_header_nritems(cur))
3511 ret = btrfs_drop_leaf_ref(trans, root, cur);
3515 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3516 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3517 blocksize = btrfs_level_size(root, *level - 1);
3519 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3522 parent = path->nodes[*level];
3523 root_owner = btrfs_header_owner(parent);
3524 root_gen = btrfs_header_generation(parent);
3525 path->slots[*level]++;
3527 ret = __btrfs_free_extent(trans, root, bytenr,
3528 blocksize, parent->start,
3529 root_owner, root_gen,
3533 atomic_inc(&root->fs_info->throttle_gen);
3534 wake_up(&root->fs_info->transaction_throttle);
3540 * at this point, we have a single ref, and since the
3541 * only place referencing this extent is a dead root
3542 * the reference count should never go higher.
3543 * So, we don't need to check it again
3546 ref = btrfs_lookup_leaf_ref(root, bytenr);
3547 if (ref && ref->generation != ptr_gen) {
3548 btrfs_free_leaf_ref(root, ref);
3552 ret = cache_drop_leaf_ref(trans, root, ref);
3554 btrfs_remove_leaf_ref(root, ref);
3555 btrfs_free_leaf_ref(root, ref);
3559 if (printk_ratelimit()) {
3560 printk("leaf ref miss for bytenr %llu\n",
3561 (unsigned long long)bytenr);
3564 next = btrfs_find_tree_block(root, bytenr, blocksize);
3565 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3566 free_extent_buffer(next);
3568 next = read_tree_block(root, bytenr, blocksize,
3573 * this is a debugging check and can go away
3574 * the ref should never go all the way down to 1
3577 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3583 WARN_ON(*level <= 0);
3584 if (path->nodes[*level-1])
3585 free_extent_buffer(path->nodes[*level-1]);
3586 path->nodes[*level-1] = next;
3587 *level = btrfs_header_level(next);
3588 path->slots[*level] = 0;
3592 WARN_ON(*level < 0);
3593 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3595 if (path->nodes[*level] == root->node) {
3596 parent = path->nodes[*level];
3597 bytenr = path->nodes[*level]->start;
3599 parent = path->nodes[*level + 1];
3600 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3603 blocksize = btrfs_level_size(root, *level);
3604 root_owner = btrfs_header_owner(parent);
3605 root_gen = btrfs_header_generation(parent);
3607 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3608 parent->start, root_owner, root_gen,
3610 free_extent_buffer(path->nodes[*level]);
3611 path->nodes[*level] = NULL;
3620 * helper function for drop_subtree, this function is similar to
3621 * walk_down_tree. The main difference is that it checks reference
3622 * counts while tree blocks are locked.
3624 static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3625 struct btrfs_root *root,
3626 struct btrfs_path *path, int *level)
3628 struct extent_buffer *next;
3629 struct extent_buffer *cur;
3630 struct extent_buffer *parent;
3637 cur = path->nodes[*level];
3638 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3644 while (*level >= 0) {
3645 cur = path->nodes[*level];
3647 ret = btrfs_drop_leaf_ref(trans, root, cur);
3649 clean_tree_block(trans, root, cur);
3652 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3653 clean_tree_block(trans, root, cur);
3657 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3658 blocksize = btrfs_level_size(root, *level - 1);
3659 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3661 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3662 btrfs_tree_lock(next);
3664 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3668 parent = path->nodes[*level];
3669 ret = btrfs_free_extent(trans, root, bytenr,
3670 blocksize, parent->start,
3671 btrfs_header_owner(parent),
3672 btrfs_header_generation(parent),
3675 path->slots[*level]++;
3676 btrfs_tree_unlock(next);
3677 free_extent_buffer(next);
3681 *level = btrfs_header_level(next);
3682 path->nodes[*level] = next;
3683 path->slots[*level] = 0;
3684 path->locks[*level] = 1;
3688 parent = path->nodes[*level + 1];
3689 bytenr = path->nodes[*level]->start;
3690 blocksize = path->nodes[*level]->len;
3692 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3693 parent->start, btrfs_header_owner(parent),
3694 btrfs_header_generation(parent), *level, 1);
3697 if (path->locks[*level]) {
3698 btrfs_tree_unlock(path->nodes[*level]);
3699 path->locks[*level] = 0;
3701 free_extent_buffer(path->nodes[*level]);
3702 path->nodes[*level] = NULL;
3709 * helper for dropping snapshots. This walks back up the tree in the path
3710 * to find the first node higher up where we haven't yet gone through
3713 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3714 struct btrfs_root *root,
3715 struct btrfs_path *path,
3716 int *level, int max_level)
3720 struct btrfs_root_item *root_item = &root->root_item;
3725 for (i = *level; i < max_level && path->nodes[i]; i++) {
3726 slot = path->slots[i];
3727 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3728 struct extent_buffer *node;
3729 struct btrfs_disk_key disk_key;
3730 node = path->nodes[i];
3733 WARN_ON(*level == 0);
3734 btrfs_node_key(node, &disk_key, path->slots[i]);
3735 memcpy(&root_item->drop_progress,
3736 &disk_key, sizeof(disk_key));
3737 root_item->drop_level = i;
3740 struct extent_buffer *parent;
3741 if (path->nodes[*level] == root->node)
3742 parent = path->nodes[*level];
3744 parent = path->nodes[*level + 1];
3746 root_owner = btrfs_header_owner(parent);
3747 root_gen = btrfs_header_generation(parent);
3749 clean_tree_block(trans, root, path->nodes[*level]);
3750 ret = btrfs_free_extent(trans, root,
3751 path->nodes[*level]->start,
3752 path->nodes[*level]->len,
3753 parent->start, root_owner,
3754 root_gen, *level, 1);
3756 if (path->locks[*level]) {
3757 btrfs_tree_unlock(path->nodes[*level]);
3758 path->locks[*level] = 0;
3760 free_extent_buffer(path->nodes[*level]);
3761 path->nodes[*level] = NULL;
3769 * drop the reference count on the tree rooted at 'snap'. This traverses
3770 * the tree freeing any blocks that have a ref count of zero after being
3773 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3779 struct btrfs_path *path;
3782 struct btrfs_root_item *root_item = &root->root_item;
3784 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3785 path = btrfs_alloc_path();
3788 level = btrfs_header_level(root->node);
3790 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3791 path->nodes[level] = root->node;
3792 extent_buffer_get(root->node);
3793 path->slots[level] = 0;
3795 struct btrfs_key key;
3796 struct btrfs_disk_key found_key;
3797 struct extent_buffer *node;
3799 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3800 level = root_item->drop_level;
3801 path->lowest_level = level;
3802 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3807 node = path->nodes[level];
3808 btrfs_node_key(node, &found_key, path->slots[level]);
3809 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3810 sizeof(found_key)));
3812 * unlock our path, this is safe because only this
3813 * function is allowed to delete this snapshot
3815 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3816 if (path->nodes[i] && path->locks[i]) {
3818 btrfs_tree_unlock(path->nodes[i]);
3823 wret = walk_down_tree(trans, root, path, &level);
3829 wret = walk_up_tree(trans, root, path, &level,
3835 if (trans->transaction->in_commit) {
3839 atomic_inc(&root->fs_info->throttle_gen);
3840 wake_up(&root->fs_info->transaction_throttle);
3842 for (i = 0; i <= orig_level; i++) {
3843 if (path->nodes[i]) {
3844 free_extent_buffer(path->nodes[i]);
3845 path->nodes[i] = NULL;
3849 btrfs_free_path(path);
3853 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root,
3855 struct extent_buffer *node,
3856 struct extent_buffer *parent)
3858 struct btrfs_path *path;
3864 path = btrfs_alloc_path();
3867 BUG_ON(!btrfs_tree_locked(parent));
3868 parent_level = btrfs_header_level(parent);
3869 extent_buffer_get(parent);
3870 path->nodes[parent_level] = parent;
3871 path->slots[parent_level] = btrfs_header_nritems(parent);
3873 BUG_ON(!btrfs_tree_locked(node));
3874 level = btrfs_header_level(node);
3875 extent_buffer_get(node);
3876 path->nodes[level] = node;
3877 path->slots[level] = 0;
3880 wret = walk_down_subtree(trans, root, path, &level);
3886 wret = walk_up_tree(trans, root, path, &level, parent_level);
3893 btrfs_free_path(path);
3897 static unsigned long calc_ra(unsigned long start, unsigned long last,
3900 return min(last, start + nr - 1);
3903 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3908 unsigned long first_index;
3909 unsigned long last_index;
3912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3913 struct file_ra_state *ra;
3914 struct btrfs_ordered_extent *ordered;
3915 unsigned int total_read = 0;
3916 unsigned int total_dirty = 0;
3919 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3921 mutex_lock(&inode->i_mutex);
3922 first_index = start >> PAGE_CACHE_SHIFT;
3923 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3925 /* make sure the dirty trick played by the caller work */
3926 ret = invalidate_inode_pages2_range(inode->i_mapping,
3927 first_index, last_index);
3931 file_ra_state_init(ra, inode->i_mapping);
3933 for (i = first_index ; i <= last_index; i++) {
3934 if (total_read % ra->ra_pages == 0) {
3935 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3936 calc_ra(i, last_index, ra->ra_pages));
3940 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3942 page = grab_cache_page(inode->i_mapping, i);
3947 if (!PageUptodate(page)) {
3948 btrfs_readpage(NULL, page);
3950 if (!PageUptodate(page)) {
3952 page_cache_release(page);
3957 wait_on_page_writeback(page);
3959 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3960 page_end = page_start + PAGE_CACHE_SIZE - 1;
3961 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3963 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3965 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3967 page_cache_release(page);
3968 btrfs_start_ordered_extent(inode, ordered, 1);
3969 btrfs_put_ordered_extent(ordered);
3972 set_page_extent_mapped(page);
3974 btrfs_set_extent_delalloc(inode, page_start, page_end);
3975 if (i == first_index)
3976 set_extent_bits(io_tree, page_start, page_end,
3977 EXTENT_BOUNDARY, GFP_NOFS);
3979 set_page_dirty(page);
3982 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3984 page_cache_release(page);
3989 mutex_unlock(&inode->i_mutex);
3990 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3994 static int noinline relocate_data_extent(struct inode *reloc_inode,
3995 struct btrfs_key *extent_key,
3998 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3999 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4000 struct extent_map *em;
4001 u64 start = extent_key->objectid - offset;
4002 u64 end = start + extent_key->offset - 1;
4004 em = alloc_extent_map(GFP_NOFS);
4005 BUG_ON(!em || IS_ERR(em));
4008 em->len = extent_key->offset;
4009 em->block_len = extent_key->offset;
4010 em->block_start = extent_key->objectid;
4011 em->bdev = root->fs_info->fs_devices->latest_bdev;
4012 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4014 /* setup extent map to cheat btrfs_readpage */
4015 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4018 spin_lock(&em_tree->lock);
4019 ret = add_extent_mapping(em_tree, em);
4020 spin_unlock(&em_tree->lock);
4021 if (ret != -EEXIST) {
4022 free_extent_map(em);
4025 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4027 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4029 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4032 struct btrfs_ref_path {
4034 u64 nodes[BTRFS_MAX_LEVEL];
4036 u64 root_generation;
4043 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4044 u64 new_nodes[BTRFS_MAX_LEVEL];
4047 struct disk_extent {
4058 static int is_cowonly_root(u64 root_objectid)
4060 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4061 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4062 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4063 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4064 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4069 static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *extent_root,
4071 struct btrfs_ref_path *ref_path,
4074 struct extent_buffer *leaf;
4075 struct btrfs_path *path;
4076 struct btrfs_extent_ref *ref;
4077 struct btrfs_key key;
4078 struct btrfs_key found_key;
4084 path = btrfs_alloc_path();
4089 ref_path->lowest_level = -1;
4090 ref_path->current_level = -1;
4091 ref_path->shared_level = -1;
4095 level = ref_path->current_level - 1;
4096 while (level >= -1) {
4098 if (level < ref_path->lowest_level)
4102 bytenr = ref_path->nodes[level];
4104 bytenr = ref_path->extent_start;
4106 BUG_ON(bytenr == 0);
4108 parent = ref_path->nodes[level + 1];
4109 ref_path->nodes[level + 1] = 0;
4110 ref_path->current_level = level;
4111 BUG_ON(parent == 0);
4113 key.objectid = bytenr;
4114 key.offset = parent + 1;
4115 key.type = BTRFS_EXTENT_REF_KEY;
4117 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4122 leaf = path->nodes[0];
4123 nritems = btrfs_header_nritems(leaf);
4124 if (path->slots[0] >= nritems) {
4125 ret = btrfs_next_leaf(extent_root, path);
4130 leaf = path->nodes[0];
4133 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4134 if (found_key.objectid == bytenr &&
4135 found_key.type == BTRFS_EXTENT_REF_KEY) {
4136 if (level < ref_path->shared_level)
4137 ref_path->shared_level = level;
4142 btrfs_release_path(extent_root, path);
4145 /* reached lowest level */
4149 level = ref_path->current_level;
4150 while (level < BTRFS_MAX_LEVEL - 1) {
4153 bytenr = ref_path->nodes[level];
4155 bytenr = ref_path->extent_start;
4157 BUG_ON(bytenr == 0);
4159 key.objectid = bytenr;
4161 key.type = BTRFS_EXTENT_REF_KEY;
4163 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4167 leaf = path->nodes[0];
4168 nritems = btrfs_header_nritems(leaf);
4169 if (path->slots[0] >= nritems) {
4170 ret = btrfs_next_leaf(extent_root, path);
4174 /* the extent was freed by someone */
4175 if (ref_path->lowest_level == level)
4177 btrfs_release_path(extent_root, path);
4180 leaf = path->nodes[0];
4183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4184 if (found_key.objectid != bytenr ||
4185 found_key.type != BTRFS_EXTENT_REF_KEY) {
4186 /* the extent was freed by someone */
4187 if (ref_path->lowest_level == level) {
4191 btrfs_release_path(extent_root, path);
4195 ref = btrfs_item_ptr(leaf, path->slots[0],
4196 struct btrfs_extent_ref);
4197 ref_objectid = btrfs_ref_objectid(leaf, ref);
4198 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4200 level = (int)ref_objectid;
4201 BUG_ON(level >= BTRFS_MAX_LEVEL);
4202 ref_path->lowest_level = level;
4203 ref_path->current_level = level;
4204 ref_path->nodes[level] = bytenr;
4206 WARN_ON(ref_objectid != level);
4209 WARN_ON(level != -1);
4213 if (ref_path->lowest_level == level) {
4214 ref_path->owner_objectid = ref_objectid;
4215 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4219 * the block is tree root or the block isn't in reference
4222 if (found_key.objectid == found_key.offset ||
4223 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4224 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4225 ref_path->root_generation =
4226 btrfs_ref_generation(leaf, ref);
4228 /* special reference from the tree log */
4229 ref_path->nodes[0] = found_key.offset;
4230 ref_path->current_level = 0;
4237 BUG_ON(ref_path->nodes[level] != 0);
4238 ref_path->nodes[level] = found_key.offset;
4239 ref_path->current_level = level;
4242 * the reference was created in the running transaction,
4243 * no need to continue walking up.
4245 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4246 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4247 ref_path->root_generation =
4248 btrfs_ref_generation(leaf, ref);
4253 btrfs_release_path(extent_root, path);
4256 /* reached max tree level, but no tree root found. */
4259 btrfs_free_path(path);
4263 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4264 struct btrfs_root *extent_root,
4265 struct btrfs_ref_path *ref_path,
4268 memset(ref_path, 0, sizeof(*ref_path));
4269 ref_path->extent_start = extent_start;
4271 return __next_ref_path(trans, extent_root, ref_path, 1);
4274 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4275 struct btrfs_root *extent_root,
4276 struct btrfs_ref_path *ref_path)
4278 return __next_ref_path(trans, extent_root, ref_path, 0);
4281 static int noinline get_new_locations(struct inode *reloc_inode,
4282 struct btrfs_key *extent_key,
4283 u64 offset, int no_fragment,
4284 struct disk_extent **extents,
4287 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4288 struct btrfs_path *path;
4289 struct btrfs_file_extent_item *fi;
4290 struct extent_buffer *leaf;
4291 struct disk_extent *exts = *extents;
4292 struct btrfs_key found_key;
4297 int max = *nr_extents;
4300 WARN_ON(!no_fragment && *extents);
4303 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4308 path = btrfs_alloc_path();
4311 cur_pos = extent_key->objectid - offset;
4312 last_byte = extent_key->objectid + extent_key->offset;
4313 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4323 leaf = path->nodes[0];
4324 nritems = btrfs_header_nritems(leaf);
4325 if (path->slots[0] >= nritems) {
4326 ret = btrfs_next_leaf(root, path);
4331 leaf = path->nodes[0];
4334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4335 if (found_key.offset != cur_pos ||
4336 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4337 found_key.objectid != reloc_inode->i_ino)
4340 fi = btrfs_item_ptr(leaf, path->slots[0],
4341 struct btrfs_file_extent_item);
4342 if (btrfs_file_extent_type(leaf, fi) !=
4343 BTRFS_FILE_EXTENT_REG ||
4344 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4348 struct disk_extent *old = exts;
4350 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4351 memcpy(exts, old, sizeof(*exts) * nr);
4352 if (old != *extents)
4356 exts[nr].disk_bytenr =
4357 btrfs_file_extent_disk_bytenr(leaf, fi);
4358 exts[nr].disk_num_bytes =
4359 btrfs_file_extent_disk_num_bytes(leaf, fi);
4360 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4361 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4362 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4363 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4364 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4365 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4367 BUG_ON(exts[nr].offset > 0);
4368 BUG_ON(exts[nr].compression || exts[nr].encryption);
4369 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4371 cur_pos += exts[nr].num_bytes;
4374 if (cur_pos + offset >= last_byte)
4384 WARN_ON(cur_pos + offset > last_byte);
4385 if (cur_pos + offset < last_byte) {
4391 btrfs_free_path(path);
4393 if (exts != *extents)
4402 static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4403 struct btrfs_root *root,
4404 struct btrfs_path *path,
4405 struct btrfs_key *extent_key,
4406 struct btrfs_key *leaf_key,
4407 struct btrfs_ref_path *ref_path,
4408 struct disk_extent *new_extents,
4411 struct extent_buffer *leaf;
4412 struct btrfs_file_extent_item *fi;
4413 struct inode *inode = NULL;
4414 struct btrfs_key key;
4422 int extent_locked = 0;
4426 memcpy(&key, leaf_key, sizeof(key));
4427 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4428 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4429 if (key.objectid < ref_path->owner_objectid ||
4430 (key.objectid == ref_path->owner_objectid &&
4431 key.type < BTRFS_EXTENT_DATA_KEY)) {
4432 key.objectid = ref_path->owner_objectid;
4433 key.type = BTRFS_EXTENT_DATA_KEY;
4439 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4443 leaf = path->nodes[0];
4444 nritems = btrfs_header_nritems(leaf);
4446 if (extent_locked && ret > 0) {
4448 * the file extent item was modified by someone
4449 * before the extent got locked.
4451 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4452 lock_end, GFP_NOFS);
4456 if (path->slots[0] >= nritems) {
4457 if (++nr_scaned > 2)
4460 BUG_ON(extent_locked);
4461 ret = btrfs_next_leaf(root, path);
4466 leaf = path->nodes[0];
4467 nritems = btrfs_header_nritems(leaf);
4470 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4472 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4473 if ((key.objectid > ref_path->owner_objectid) ||
4474 (key.objectid == ref_path->owner_objectid &&
4475 key.type > BTRFS_EXTENT_DATA_KEY) ||
4476 (key.offset >= first_pos + extent_key->offset))
4480 if (inode && key.objectid != inode->i_ino) {
4481 BUG_ON(extent_locked);
4482 btrfs_release_path(root, path);
4483 mutex_unlock(&inode->i_mutex);
4489 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4494 fi = btrfs_item_ptr(leaf, path->slots[0],
4495 struct btrfs_file_extent_item);
4496 extent_type = btrfs_file_extent_type(leaf, fi);
4497 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4498 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4499 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4500 extent_key->objectid)) {
4506 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4507 ext_offset = btrfs_file_extent_offset(leaf, fi);
4509 if (first_pos > key.offset - ext_offset)
4510 first_pos = key.offset - ext_offset;
4512 if (!extent_locked) {
4513 lock_start = key.offset;
4514 lock_end = lock_start + num_bytes - 1;
4516 if (lock_start > key.offset ||
4517 lock_end + 1 < key.offset + num_bytes) {
4518 unlock_extent(&BTRFS_I(inode)->io_tree,
4519 lock_start, lock_end, GFP_NOFS);
4525 btrfs_release_path(root, path);
4527 inode = btrfs_iget_locked(root->fs_info->sb,
4528 key.objectid, root);
4529 if (inode->i_state & I_NEW) {
4530 BTRFS_I(inode)->root = root;
4531 BTRFS_I(inode)->location.objectid =
4533 BTRFS_I(inode)->location.type =
4534 BTRFS_INODE_ITEM_KEY;
4535 BTRFS_I(inode)->location.offset = 0;
4536 btrfs_read_locked_inode(inode);
4537 unlock_new_inode(inode);
4540 * some code call btrfs_commit_transaction while
4541 * holding the i_mutex, so we can't use mutex_lock
4544 if (is_bad_inode(inode) ||
4545 !mutex_trylock(&inode->i_mutex)) {
4548 key.offset = (u64)-1;
4553 if (!extent_locked) {
4554 struct btrfs_ordered_extent *ordered;
4556 btrfs_release_path(root, path);
4558 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4559 lock_end, GFP_NOFS);
4560 ordered = btrfs_lookup_first_ordered_extent(inode,
4563 ordered->file_offset <= lock_end &&
4564 ordered->file_offset + ordered->len > lock_start) {
4565 unlock_extent(&BTRFS_I(inode)->io_tree,
4566 lock_start, lock_end, GFP_NOFS);
4567 btrfs_start_ordered_extent(inode, ordered, 1);
4568 btrfs_put_ordered_extent(ordered);
4569 key.offset += num_bytes;
4573 btrfs_put_ordered_extent(ordered);
4579 if (nr_extents == 1) {
4580 /* update extent pointer in place */
4581 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4582 new_extents[0].disk_bytenr);
4583 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4584 new_extents[0].disk_num_bytes);
4585 btrfs_mark_buffer_dirty(leaf);
4587 btrfs_drop_extent_cache(inode, key.offset,
4588 key.offset + num_bytes - 1, 0);
4590 ret = btrfs_inc_extent_ref(trans, root,
4591 new_extents[0].disk_bytenr,
4592 new_extents[0].disk_num_bytes,
4594 root->root_key.objectid,
4599 ret = btrfs_free_extent(trans, root,
4600 extent_key->objectid,
4603 btrfs_header_owner(leaf),
4604 btrfs_header_generation(leaf),
4608 btrfs_release_path(root, path);
4609 key.offset += num_bytes;
4617 * drop old extent pointer at first, then insert the
4618 * new pointers one bye one
4620 btrfs_release_path(root, path);
4621 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4622 key.offset + num_bytes,
4623 key.offset, &alloc_hint);
4626 for (i = 0; i < nr_extents; i++) {
4627 if (ext_offset >= new_extents[i].num_bytes) {
4628 ext_offset -= new_extents[i].num_bytes;
4631 extent_len = min(new_extents[i].num_bytes -
4632 ext_offset, num_bytes);
4634 ret = btrfs_insert_empty_item(trans, root,
4639 leaf = path->nodes[0];
4640 fi = btrfs_item_ptr(leaf, path->slots[0],
4641 struct btrfs_file_extent_item);
4642 btrfs_set_file_extent_generation(leaf, fi,
4644 btrfs_set_file_extent_type(leaf, fi,
4645 BTRFS_FILE_EXTENT_REG);
4646 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4647 new_extents[i].disk_bytenr);
4648 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4649 new_extents[i].disk_num_bytes);
4650 btrfs_set_file_extent_ram_bytes(leaf, fi,
4651 new_extents[i].ram_bytes);
4653 btrfs_set_file_extent_compression(leaf, fi,
4654 new_extents[i].compression);
4655 btrfs_set_file_extent_encryption(leaf, fi,
4656 new_extents[i].encryption);
4657 btrfs_set_file_extent_other_encoding(leaf, fi,
4658 new_extents[i].other_encoding);
4660 btrfs_set_file_extent_num_bytes(leaf, fi,
4662 ext_offset += new_extents[i].offset;
4663 btrfs_set_file_extent_offset(leaf, fi,
4665 btrfs_mark_buffer_dirty(leaf);
4667 btrfs_drop_extent_cache(inode, key.offset,
4668 key.offset + extent_len - 1, 0);
4670 ret = btrfs_inc_extent_ref(trans, root,
4671 new_extents[i].disk_bytenr,
4672 new_extents[i].disk_num_bytes,
4674 root->root_key.objectid,
4675 trans->transid, key.objectid);
4677 btrfs_release_path(root, path);
4679 inode_add_bytes(inode, extent_len);
4682 num_bytes -= extent_len;
4683 key.offset += extent_len;
4688 BUG_ON(i >= nr_extents);
4692 if (extent_locked) {
4693 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4694 lock_end, GFP_NOFS);
4698 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4699 key.offset >= first_pos + extent_key->offset)
4706 btrfs_release_path(root, path);
4708 mutex_unlock(&inode->i_mutex);
4709 if (extent_locked) {
4710 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4711 lock_end, GFP_NOFS);
4718 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4719 struct btrfs_root *root,
4720 struct extent_buffer *buf, u64 orig_start)
4725 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4726 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4728 level = btrfs_header_level(buf);
4730 struct btrfs_leaf_ref *ref;
4731 struct btrfs_leaf_ref *orig_ref;
4733 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4737 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4739 btrfs_free_leaf_ref(root, orig_ref);
4743 ref->nritems = orig_ref->nritems;
4744 memcpy(ref->extents, orig_ref->extents,
4745 sizeof(ref->extents[0]) * ref->nritems);
4747 btrfs_free_leaf_ref(root, orig_ref);
4749 ref->root_gen = trans->transid;
4750 ref->bytenr = buf->start;
4751 ref->owner = btrfs_header_owner(buf);
4752 ref->generation = btrfs_header_generation(buf);
4753 ret = btrfs_add_leaf_ref(root, ref, 0);
4755 btrfs_free_leaf_ref(root, ref);
4760 static int noinline invalidate_extent_cache(struct btrfs_root *root,
4761 struct extent_buffer *leaf,
4762 struct btrfs_block_group_cache *group,
4763 struct btrfs_root *target_root)
4765 struct btrfs_key key;
4766 struct inode *inode = NULL;
4767 struct btrfs_file_extent_item *fi;
4769 u64 skip_objectid = 0;
4773 nritems = btrfs_header_nritems(leaf);
4774 for (i = 0; i < nritems; i++) {
4775 btrfs_item_key_to_cpu(leaf, &key, i);
4776 if (key.objectid == skip_objectid ||
4777 key.type != BTRFS_EXTENT_DATA_KEY)
4779 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4780 if (btrfs_file_extent_type(leaf, fi) ==
4781 BTRFS_FILE_EXTENT_INLINE)
4783 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4785 if (!inode || inode->i_ino != key.objectid) {
4787 inode = btrfs_ilookup(target_root->fs_info->sb,
4788 key.objectid, target_root, 1);
4791 skip_objectid = key.objectid;
4794 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4796 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4797 key.offset + num_bytes - 1, GFP_NOFS);
4798 btrfs_drop_extent_cache(inode, key.offset,
4799 key.offset + num_bytes - 1, 1);
4800 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4801 key.offset + num_bytes - 1, GFP_NOFS);
4808 static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4809 struct btrfs_root *root,
4810 struct extent_buffer *leaf,
4811 struct btrfs_block_group_cache *group,
4812 struct inode *reloc_inode)
4814 struct btrfs_key key;
4815 struct btrfs_key extent_key;
4816 struct btrfs_file_extent_item *fi;
4817 struct btrfs_leaf_ref *ref;
4818 struct disk_extent *new_extent;
4827 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4828 BUG_ON(!new_extent);
4830 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4834 nritems = btrfs_header_nritems(leaf);
4835 for (i = 0; i < nritems; i++) {
4836 btrfs_item_key_to_cpu(leaf, &key, i);
4837 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4839 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4840 if (btrfs_file_extent_type(leaf, fi) ==
4841 BTRFS_FILE_EXTENT_INLINE)
4843 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4844 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4849 if (bytenr >= group->key.objectid + group->key.offset ||
4850 bytenr + num_bytes <= group->key.objectid)
4853 extent_key.objectid = bytenr;
4854 extent_key.offset = num_bytes;
4855 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4857 ret = get_new_locations(reloc_inode, &extent_key,
4858 group->key.objectid, 1,
4859 &new_extent, &nr_extent);
4864 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4865 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4866 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4867 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4869 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4870 new_extent->disk_bytenr);
4871 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4872 new_extent->disk_num_bytes);
4873 btrfs_mark_buffer_dirty(leaf);
4875 ret = btrfs_inc_extent_ref(trans, root,
4876 new_extent->disk_bytenr,
4877 new_extent->disk_num_bytes,
4879 root->root_key.objectid,
4880 trans->transid, key.objectid);
4882 ret = btrfs_free_extent(trans, root,
4883 bytenr, num_bytes, leaf->start,
4884 btrfs_header_owner(leaf),
4885 btrfs_header_generation(leaf),
4891 BUG_ON(ext_index + 1 != ref->nritems);
4892 btrfs_free_leaf_ref(root, ref);
4896 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4897 struct btrfs_root *root)
4899 struct btrfs_root *reloc_root;
4902 if (root->reloc_root) {
4903 reloc_root = root->reloc_root;
4904 root->reloc_root = NULL;
4905 list_add(&reloc_root->dead_list,
4906 &root->fs_info->dead_reloc_roots);
4908 btrfs_set_root_bytenr(&reloc_root->root_item,
4909 reloc_root->node->start);
4910 btrfs_set_root_level(&root->root_item,
4911 btrfs_header_level(reloc_root->node));
4912 memset(&reloc_root->root_item.drop_progress, 0,
4913 sizeof(struct btrfs_disk_key));
4914 reloc_root->root_item.drop_level = 0;
4916 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4917 &reloc_root->root_key,
4918 &reloc_root->root_item);
4924 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4926 struct btrfs_trans_handle *trans;
4927 struct btrfs_root *reloc_root;
4928 struct btrfs_root *prev_root = NULL;
4929 struct list_head dead_roots;
4933 INIT_LIST_HEAD(&dead_roots);
4934 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4936 while (!list_empty(&dead_roots)) {
4937 reloc_root = list_entry(dead_roots.prev,
4938 struct btrfs_root, dead_list);
4939 list_del_init(&reloc_root->dead_list);
4941 BUG_ON(reloc_root->commit_root != NULL);
4943 trans = btrfs_join_transaction(root, 1);
4946 mutex_lock(&root->fs_info->drop_mutex);
4947 ret = btrfs_drop_snapshot(trans, reloc_root);
4950 mutex_unlock(&root->fs_info->drop_mutex);
4952 nr = trans->blocks_used;
4953 ret = btrfs_end_transaction(trans, root);
4955 btrfs_btree_balance_dirty(root, nr);
4958 free_extent_buffer(reloc_root->node);
4960 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4961 &reloc_root->root_key);
4963 mutex_unlock(&root->fs_info->drop_mutex);
4965 nr = trans->blocks_used;
4966 ret = btrfs_end_transaction(trans, root);
4968 btrfs_btree_balance_dirty(root, nr);
4971 prev_root = reloc_root;
4974 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4980 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4982 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4986 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4988 struct btrfs_root *reloc_root;
4989 struct btrfs_trans_handle *trans;
4990 struct btrfs_key location;
4994 mutex_lock(&root->fs_info->tree_reloc_mutex);
4995 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4997 found = !list_empty(&root->fs_info->dead_reloc_roots);
4998 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5001 trans = btrfs_start_transaction(root, 1);
5003 ret = btrfs_commit_transaction(trans, root);
5007 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5008 location.offset = (u64)-1;
5009 location.type = BTRFS_ROOT_ITEM_KEY;
5011 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5012 BUG_ON(!reloc_root);
5013 btrfs_orphan_cleanup(reloc_root);
5017 static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5018 struct btrfs_root *root)
5020 struct btrfs_root *reloc_root;
5021 struct extent_buffer *eb;
5022 struct btrfs_root_item *root_item;
5023 struct btrfs_key root_key;
5026 BUG_ON(!root->ref_cows);
5027 if (root->reloc_root)
5030 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5033 ret = btrfs_copy_root(trans, root, root->commit_root,
5034 &eb, BTRFS_TREE_RELOC_OBJECTID);
5037 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5038 root_key.offset = root->root_key.objectid;
5039 root_key.type = BTRFS_ROOT_ITEM_KEY;
5041 memcpy(root_item, &root->root_item, sizeof(root_item));
5042 btrfs_set_root_refs(root_item, 0);
5043 btrfs_set_root_bytenr(root_item, eb->start);
5044 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5045 btrfs_set_root_generation(root_item, trans->transid);
5047 btrfs_tree_unlock(eb);
5048 free_extent_buffer(eb);
5050 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5051 &root_key, root_item);
5055 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5057 BUG_ON(!reloc_root);
5058 reloc_root->last_trans = trans->transid;
5059 reloc_root->commit_root = NULL;
5060 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5062 root->reloc_root = reloc_root;
5067 * Core function of space balance.
5069 * The idea is using reloc trees to relocate tree blocks in reference
5070 * counted roots. There is one reloc tree for each subvol, and all
5071 * reloc trees share same root key objectid. Reloc trees are snapshots
5072 * of the latest committed roots of subvols (root->commit_root).
5074 * To relocate a tree block referenced by a subvol, there are two steps.
5075 * COW the block through subvol's reloc tree, then update block pointer
5076 * in the subvol to point to the new block. Since all reloc trees share
5077 * same root key objectid, doing special handing for tree blocks owned
5078 * by them is easy. Once a tree block has been COWed in one reloc tree,
5079 * we can use the resulting new block directly when the same block is
5080 * required to COW again through other reloc trees. By this way, relocated
5081 * tree blocks are shared between reloc trees, so they are also shared
5084 static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5085 struct btrfs_root *root,
5086 struct btrfs_path *path,
5087 struct btrfs_key *first_key,
5088 struct btrfs_ref_path *ref_path,
5089 struct btrfs_block_group_cache *group,
5090 struct inode *reloc_inode)
5092 struct btrfs_root *reloc_root;
5093 struct extent_buffer *eb = NULL;
5094 struct btrfs_key *keys;
5098 int lowest_level = 0;
5101 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5102 lowest_level = ref_path->owner_objectid;
5104 if (!root->ref_cows) {
5105 path->lowest_level = lowest_level;
5106 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5108 path->lowest_level = 0;
5109 btrfs_release_path(root, path);
5113 mutex_lock(&root->fs_info->tree_reloc_mutex);
5114 ret = init_reloc_tree(trans, root);
5116 reloc_root = root->reloc_root;
5118 shared_level = ref_path->shared_level;
5119 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5121 keys = ref_path->node_keys;
5122 nodes = ref_path->new_nodes;
5123 memset(&keys[shared_level + 1], 0,
5124 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5125 memset(&nodes[shared_level + 1], 0,
5126 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5128 if (nodes[lowest_level] == 0) {
5129 path->lowest_level = lowest_level;
5130 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5133 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5134 eb = path->nodes[level];
5135 if (!eb || eb == reloc_root->node)
5137 nodes[level] = eb->start;
5139 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5141 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5144 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5145 eb = path->nodes[0];
5146 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5147 group, reloc_inode);
5150 btrfs_release_path(reloc_root, path);
5152 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5158 * replace tree blocks in the fs tree with tree blocks in
5161 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5164 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5165 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5168 extent_buffer_get(path->nodes[0]);
5169 eb = path->nodes[0];
5170 btrfs_release_path(reloc_root, path);
5171 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5173 free_extent_buffer(eb);
5176 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5177 path->lowest_level = 0;
5181 static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5182 struct btrfs_root *root,
5183 struct btrfs_path *path,
5184 struct btrfs_key *first_key,
5185 struct btrfs_ref_path *ref_path)
5189 ret = relocate_one_path(trans, root, path, first_key,
5190 ref_path, NULL, NULL);
5193 if (root == root->fs_info->extent_root)
5194 btrfs_extent_post_op(trans, root);
5199 static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5200 struct btrfs_root *extent_root,
5201 struct btrfs_path *path,
5202 struct btrfs_key *extent_key)
5206 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5209 ret = btrfs_del_item(trans, extent_root, path);
5211 btrfs_release_path(extent_root, path);
5215 static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5216 struct btrfs_ref_path *ref_path)
5218 struct btrfs_key root_key;
5220 root_key.objectid = ref_path->root_objectid;
5221 root_key.type = BTRFS_ROOT_ITEM_KEY;
5222 if (is_cowonly_root(ref_path->root_objectid))
5223 root_key.offset = 0;
5225 root_key.offset = (u64)-1;
5227 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5230 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5231 struct btrfs_path *path,
5232 struct btrfs_key *extent_key,
5233 struct btrfs_block_group_cache *group,
5234 struct inode *reloc_inode, int pass)
5236 struct btrfs_trans_handle *trans;
5237 struct btrfs_root *found_root;
5238 struct btrfs_ref_path *ref_path = NULL;
5239 struct disk_extent *new_extents = NULL;
5244 struct btrfs_key first_key;
5248 trans = btrfs_start_transaction(extent_root, 1);
5251 if (extent_key->objectid == 0) {
5252 ret = del_extent_zero(trans, extent_root, path, extent_key);
5256 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5262 for (loops = 0; ; loops++) {
5264 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5265 extent_key->objectid);
5267 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5274 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5275 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5278 found_root = read_ref_root(extent_root->fs_info, ref_path);
5279 BUG_ON(!found_root);
5281 * for reference counted tree, only process reference paths
5282 * rooted at the latest committed root.
5284 if (found_root->ref_cows &&
5285 ref_path->root_generation != found_root->root_key.offset)
5288 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5291 * copy data extents to new locations
5293 u64 group_start = group->key.objectid;
5294 ret = relocate_data_extent(reloc_inode,
5303 level = ref_path->owner_objectid;
5306 if (prev_block != ref_path->nodes[level]) {
5307 struct extent_buffer *eb;
5308 u64 block_start = ref_path->nodes[level];
5309 u64 block_size = btrfs_level_size(found_root, level);
5311 eb = read_tree_block(found_root, block_start,
5313 btrfs_tree_lock(eb);
5314 BUG_ON(level != btrfs_header_level(eb));
5317 btrfs_item_key_to_cpu(eb, &first_key, 0);
5319 btrfs_node_key_to_cpu(eb, &first_key, 0);
5321 btrfs_tree_unlock(eb);
5322 free_extent_buffer(eb);
5323 prev_block = block_start;
5326 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5329 * use fallback method to process the remaining
5333 u64 group_start = group->key.objectid;
5334 new_extents = kmalloc(sizeof(*new_extents),
5337 ret = get_new_locations(reloc_inode,
5345 btrfs_record_root_in_trans(found_root);
5346 ret = replace_one_extent(trans, found_root,
5348 &first_key, ref_path,
5349 new_extents, nr_extents);
5355 btrfs_record_root_in_trans(found_root);
5356 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5357 ret = relocate_tree_block(trans, found_root, path,
5358 &first_key, ref_path);
5361 * try to update data extent references while
5362 * keeping metadata shared between snapshots.
5364 ret = relocate_one_path(trans, found_root, path,
5365 &first_key, ref_path,
5366 group, reloc_inode);
5373 btrfs_end_transaction(trans, extent_root);
5379 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5382 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5383 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5385 num_devices = root->fs_info->fs_devices->rw_devices;
5386 if (num_devices == 1) {
5387 stripped |= BTRFS_BLOCK_GROUP_DUP;
5388 stripped = flags & ~stripped;
5390 /* turn raid0 into single device chunks */
5391 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5394 /* turn mirroring into duplication */
5395 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5396 BTRFS_BLOCK_GROUP_RAID10))
5397 return stripped | BTRFS_BLOCK_GROUP_DUP;
5400 /* they already had raid on here, just return */
5401 if (flags & stripped)
5404 stripped |= BTRFS_BLOCK_GROUP_DUP;
5405 stripped = flags & ~stripped;
5407 /* switch duplicated blocks with raid1 */
5408 if (flags & BTRFS_BLOCK_GROUP_DUP)
5409 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5411 /* turn single device chunks into raid0 */
5412 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5417 int __alloc_chunk_for_shrink(struct btrfs_root *root,
5418 struct btrfs_block_group_cache *shrink_block_group,
5421 struct btrfs_trans_handle *trans;
5422 u64 new_alloc_flags;
5425 spin_lock(&shrink_block_group->lock);
5426 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5427 spin_unlock(&shrink_block_group->lock);
5429 trans = btrfs_start_transaction(root, 1);
5430 spin_lock(&shrink_block_group->lock);
5432 new_alloc_flags = update_block_group_flags(root,
5433 shrink_block_group->flags);
5434 if (new_alloc_flags != shrink_block_group->flags) {
5436 btrfs_block_group_used(&shrink_block_group->item);
5438 calc = shrink_block_group->key.offset;
5440 spin_unlock(&shrink_block_group->lock);
5442 do_chunk_alloc(trans, root->fs_info->extent_root,
5443 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5445 btrfs_end_transaction(trans, root);
5447 spin_unlock(&shrink_block_group->lock);
5451 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5452 struct btrfs_root *root,
5453 u64 objectid, u64 size)
5455 struct btrfs_path *path;
5456 struct btrfs_inode_item *item;
5457 struct extent_buffer *leaf;
5460 path = btrfs_alloc_path();
5464 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5468 leaf = path->nodes[0];
5469 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5470 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5471 btrfs_set_inode_generation(leaf, item, 1);
5472 btrfs_set_inode_size(leaf, item, size);
5473 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5474 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5475 BTRFS_INODE_NOCOMPRESS);
5476 btrfs_mark_buffer_dirty(leaf);
5477 btrfs_release_path(root, path);
5479 btrfs_free_path(path);
5483 static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5484 struct btrfs_block_group_cache *group)
5486 struct inode *inode = NULL;
5487 struct btrfs_trans_handle *trans;
5488 struct btrfs_root *root;
5489 struct btrfs_key root_key;
5490 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5493 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5494 root_key.type = BTRFS_ROOT_ITEM_KEY;
5495 root_key.offset = (u64)-1;
5496 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5498 return ERR_CAST(root);
5500 trans = btrfs_start_transaction(root, 1);
5503 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5507 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5510 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5511 group->key.offset, 0, group->key.offset,
5515 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5516 if (inode->i_state & I_NEW) {
5517 BTRFS_I(inode)->root = root;
5518 BTRFS_I(inode)->location.objectid = objectid;
5519 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5520 BTRFS_I(inode)->location.offset = 0;
5521 btrfs_read_locked_inode(inode);
5522 unlock_new_inode(inode);
5523 BUG_ON(is_bad_inode(inode));
5528 err = btrfs_orphan_add(trans, inode);
5530 btrfs_end_transaction(trans, root);
5534 inode = ERR_PTR(err);
5539 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5541 struct btrfs_trans_handle *trans;
5542 struct btrfs_path *path;
5543 struct btrfs_fs_info *info = root->fs_info;
5544 struct extent_buffer *leaf;
5545 struct inode *reloc_inode;
5546 struct btrfs_block_group_cache *block_group;
5547 struct btrfs_key key;
5556 root = root->fs_info->extent_root;
5558 block_group = btrfs_lookup_block_group(info, group_start);
5559 BUG_ON(!block_group);
5561 printk("btrfs relocating block group %llu flags %llu\n",
5562 (unsigned long long)block_group->key.objectid,
5563 (unsigned long long)block_group->flags);
5565 path = btrfs_alloc_path();
5568 reloc_inode = create_reloc_inode(info, block_group);
5569 BUG_ON(IS_ERR(reloc_inode));
5571 __alloc_chunk_for_shrink(root, block_group, 1);
5572 set_block_group_readonly(block_group);
5574 btrfs_start_delalloc_inodes(info->tree_root);
5575 btrfs_wait_ordered_extents(info->tree_root, 0);
5580 key.objectid = block_group->key.objectid;
5583 cur_byte = key.objectid;
5585 trans = btrfs_start_transaction(info->tree_root, 1);
5586 btrfs_commit_transaction(trans, info->tree_root);
5588 mutex_lock(&root->fs_info->cleaner_mutex);
5589 btrfs_clean_old_snapshots(info->tree_root);
5590 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5591 mutex_unlock(&root->fs_info->cleaner_mutex);
5594 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5598 leaf = path->nodes[0];
5599 nritems = btrfs_header_nritems(leaf);
5600 if (path->slots[0] >= nritems) {
5601 ret = btrfs_next_leaf(root, path);
5608 leaf = path->nodes[0];
5609 nritems = btrfs_header_nritems(leaf);
5612 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5614 if (key.objectid >= block_group->key.objectid +
5615 block_group->key.offset)
5618 if (progress && need_resched()) {
5619 btrfs_release_path(root, path);
5626 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5627 key.objectid + key.offset <= cur_byte) {
5633 cur_byte = key.objectid + key.offset;
5634 btrfs_release_path(root, path);
5636 __alloc_chunk_for_shrink(root, block_group, 0);
5637 ret = relocate_one_extent(root, path, &key, block_group,
5643 key.objectid = cur_byte;
5648 btrfs_release_path(root, path);
5651 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5652 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5653 WARN_ON(reloc_inode->i_mapping->nrpages);
5656 if (total_found > 0) {
5657 printk("btrfs found %llu extents in pass %d\n",
5658 (unsigned long long)total_found, pass);
5660 if (total_found == skipped && pass > 2) {
5662 reloc_inode = create_reloc_inode(info, block_group);
5668 /* delete reloc_inode */
5671 /* unpin extents in this range */
5672 trans = btrfs_start_transaction(info->tree_root, 1);
5673 btrfs_commit_transaction(trans, info->tree_root);
5675 spin_lock(&block_group->lock);
5676 WARN_ON(block_group->pinned > 0);
5677 WARN_ON(block_group->reserved > 0);
5678 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5679 spin_unlock(&block_group->lock);
5682 btrfs_free_path(path);
5686 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5687 struct btrfs_key *key)
5690 struct btrfs_key found_key;
5691 struct extent_buffer *leaf;
5694 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5699 slot = path->slots[0];
5700 leaf = path->nodes[0];
5701 if (slot >= btrfs_header_nritems(leaf)) {
5702 ret = btrfs_next_leaf(root, path);
5709 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5711 if (found_key.objectid >= key->objectid &&
5712 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5723 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5725 struct btrfs_block_group_cache *block_group;
5728 spin_lock(&info->block_group_cache_lock);
5729 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5730 block_group = rb_entry(n, struct btrfs_block_group_cache,
5732 rb_erase(&block_group->cache_node,
5733 &info->block_group_cache_tree);
5734 spin_unlock(&info->block_group_cache_lock);
5736 btrfs_remove_free_space_cache(block_group);
5737 down_write(&block_group->space_info->groups_sem);
5738 list_del(&block_group->list);
5739 up_write(&block_group->space_info->groups_sem);
5742 spin_lock(&info->block_group_cache_lock);
5744 spin_unlock(&info->block_group_cache_lock);
5748 int btrfs_read_block_groups(struct btrfs_root *root)
5750 struct btrfs_path *path;
5752 struct btrfs_block_group_cache *cache;
5753 struct btrfs_fs_info *info = root->fs_info;
5754 struct btrfs_space_info *space_info;
5755 struct btrfs_key key;
5756 struct btrfs_key found_key;
5757 struct extent_buffer *leaf;
5759 root = info->extent_root;
5762 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5763 path = btrfs_alloc_path();
5768 ret = find_first_block_group(root, path, &key);
5776 leaf = path->nodes[0];
5777 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5778 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5784 spin_lock_init(&cache->lock);
5785 mutex_init(&cache->alloc_mutex);
5786 INIT_LIST_HEAD(&cache->list);
5787 read_extent_buffer(leaf, &cache->item,
5788 btrfs_item_ptr_offset(leaf, path->slots[0]),
5789 sizeof(cache->item));
5790 memcpy(&cache->key, &found_key, sizeof(found_key));
5792 key.objectid = found_key.objectid + found_key.offset;
5793 btrfs_release_path(root, path);
5794 cache->flags = btrfs_block_group_flags(&cache->item);
5796 ret = update_space_info(info, cache->flags, found_key.offset,
5797 btrfs_block_group_used(&cache->item),
5800 cache->space_info = space_info;
5801 down_write(&space_info->groups_sem);
5802 list_add_tail(&cache->list, &space_info->block_groups);
5803 up_write(&space_info->groups_sem);
5805 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5808 set_avail_alloc_bits(root->fs_info, cache->flags);
5809 if (btrfs_chunk_readonly(root, cache->key.objectid))
5810 set_block_group_readonly(cache);
5814 btrfs_free_path(path);
5818 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5819 struct btrfs_root *root, u64 bytes_used,
5820 u64 type, u64 chunk_objectid, u64 chunk_offset,
5824 struct btrfs_root *extent_root;
5825 struct btrfs_block_group_cache *cache;
5827 extent_root = root->fs_info->extent_root;
5829 root->fs_info->last_trans_new_blockgroup = trans->transid;
5831 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5835 cache->key.objectid = chunk_offset;
5836 cache->key.offset = size;
5837 spin_lock_init(&cache->lock);
5838 mutex_init(&cache->alloc_mutex);
5839 INIT_LIST_HEAD(&cache->list);
5840 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5842 btrfs_set_block_group_used(&cache->item, bytes_used);
5843 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5844 cache->flags = type;
5845 btrfs_set_block_group_flags(&cache->item, type);
5847 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5848 &cache->space_info);
5850 down_write(&cache->space_info->groups_sem);
5851 list_add_tail(&cache->list, &cache->space_info->block_groups);
5852 up_write(&cache->space_info->groups_sem);
5854 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5857 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5858 sizeof(cache->item));
5861 finish_current_insert(trans, extent_root, 0);
5862 ret = del_pending_extents(trans, extent_root, 0);
5864 set_avail_alloc_bits(extent_root->fs_info, type);
5869 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5870 struct btrfs_root *root, u64 group_start)
5872 struct btrfs_path *path;
5873 struct btrfs_block_group_cache *block_group;
5874 struct btrfs_key key;
5877 root = root->fs_info->extent_root;
5879 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5880 BUG_ON(!block_group);
5881 BUG_ON(!block_group->ro);
5883 memcpy(&key, &block_group->key, sizeof(key));
5885 path = btrfs_alloc_path();
5888 btrfs_remove_free_space_cache(block_group);
5889 rb_erase(&block_group->cache_node,
5890 &root->fs_info->block_group_cache_tree);
5891 down_write(&block_group->space_info->groups_sem);
5892 list_del(&block_group->list);
5893 up_write(&block_group->space_info->groups_sem);
5895 spin_lock(&block_group->space_info->lock);
5896 block_group->space_info->total_bytes -= block_group->key.offset;
5897 block_group->space_info->bytes_readonly -= block_group->key.offset;
5898 spin_unlock(&block_group->space_info->lock);
5899 block_group->space_info->full = 0;
5902 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5903 kfree(shrink_block_group);
5906 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5912 ret = btrfs_del_item(trans, root, path);
5914 btrfs_free_path(path);