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
803 cur = insert_list->next;
804 op = list_entry(cur, struct pending_extent_op,
819 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
820 struct btrfs_root *root,
821 struct btrfs_path *path,
822 u64 bytenr, u64 parent,
823 u64 ref_root, u64 ref_generation,
826 struct btrfs_key key;
827 struct extent_buffer *leaf;
828 struct btrfs_extent_ref *ref;
832 key.objectid = bytenr;
833 key.type = BTRFS_EXTENT_REF_KEY;
836 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
838 leaf = path->nodes[0];
839 ref = btrfs_item_ptr(leaf, path->slots[0],
840 struct btrfs_extent_ref);
841 btrfs_set_ref_root(leaf, ref, ref_root);
842 btrfs_set_ref_generation(leaf, ref, ref_generation);
843 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
844 btrfs_set_ref_num_refs(leaf, ref, 1);
845 } else if (ret == -EEXIST) {
847 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
848 leaf = path->nodes[0];
849 ref = btrfs_item_ptr(leaf, path->slots[0],
850 struct btrfs_extent_ref);
851 if (btrfs_ref_root(leaf, ref) != ref_root ||
852 btrfs_ref_generation(leaf, ref) != ref_generation) {
858 num_refs = btrfs_ref_num_refs(leaf, ref);
859 BUG_ON(num_refs == 0);
860 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
862 existing_owner = btrfs_ref_objectid(leaf, ref);
863 if (existing_owner != owner_objectid &&
864 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
865 btrfs_set_ref_objectid(leaf, ref,
866 BTRFS_MULTIPLE_OBJECTIDS);
872 btrfs_mark_buffer_dirty(path->nodes[0]);
874 btrfs_release_path(root, path);
878 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
879 struct btrfs_root *root,
880 struct btrfs_path *path)
882 struct extent_buffer *leaf;
883 struct btrfs_extent_ref *ref;
887 leaf = path->nodes[0];
888 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
889 num_refs = btrfs_ref_num_refs(leaf, ref);
890 BUG_ON(num_refs == 0);
893 ret = btrfs_del_item(trans, root, path);
895 btrfs_set_ref_num_refs(leaf, ref, num_refs);
896 btrfs_mark_buffer_dirty(leaf);
898 btrfs_release_path(root, path);
902 static int noinline free_extents(struct btrfs_trans_handle *trans,
903 struct btrfs_root *extent_root,
904 struct list_head *del_list)
906 struct btrfs_fs_info *info = extent_root->fs_info;
907 struct btrfs_path *path;
908 struct btrfs_key key, found_key;
909 struct extent_buffer *leaf;
910 struct list_head *cur;
911 struct pending_extent_op *op;
912 struct btrfs_extent_item *ei;
913 int ret, num_to_del, extent_slot = 0, found_extent = 0;
917 path = btrfs_alloc_path();
923 /* search for the backref for the current ref we want to delete */
924 cur = del_list->next;
925 op = list_entry(cur, struct pending_extent_op, list);
926 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
928 extent_root->root_key.objectid,
929 op->orig_generation, op->level, 1);
931 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
932 "owner %u\n", op->bytenr,
933 extent_root->root_key.objectid, op->orig_generation,
935 btrfs_print_leaf(extent_root, path->nodes[0]);
940 extent_slot = path->slots[0];
945 * if we aren't the first item on the leaf we can move back one and see
946 * if our ref is right next to our extent item
948 if (likely(extent_slot)) {
950 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
952 if (found_key.objectid == op->bytenr &&
953 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
954 found_key.offset == op->num_bytes) {
961 * if we didn't find the extent we need to delete the backref and then
962 * search for the extent item key so we can update its ref count
965 key.objectid = op->bytenr;
966 key.type = BTRFS_EXTENT_ITEM_KEY;
967 key.offset = op->num_bytes;
969 ret = remove_extent_backref(trans, extent_root, path);
971 btrfs_release_path(extent_root, path);
972 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
974 extent_slot = path->slots[0];
977 /* this is where we update the ref count for the extent */
978 leaf = path->nodes[0];
979 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
980 refs = btrfs_extent_refs(leaf, ei);
983 btrfs_set_extent_refs(leaf, ei, refs);
985 btrfs_mark_buffer_dirty(leaf);
988 * This extent needs deleting. The reason cur_slot is extent_slot +
989 * num_to_del is because extent_slot points to the slot where the extent
990 * is, and if the backref was not right next to the extent we will be
991 * deleting at least 1 item, and will want to start searching at the
992 * slot directly next to extent_slot. However if we did find the
993 * backref next to the extent item them we will be deleting at least 2
994 * items and will want to start searching directly after the ref slot
997 struct list_head *pos, *n, *end;
998 int cur_slot = extent_slot+num_to_del;
1002 path->slots[0] = extent_slot;
1003 bytes_freed = op->num_bytes;
1005 mutex_lock(&info->pinned_mutex);
1006 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1007 op->num_bytes, op->level >=
1008 BTRFS_FIRST_FREE_OBJECTID);
1009 mutex_unlock(&info->pinned_mutex);
1014 * we need to see if we can delete multiple things at once, so
1015 * start looping through the list of extents we are wanting to
1016 * delete and see if their extent/backref's are right next to
1017 * eachother and the extents only have 1 ref
1019 for (pos = cur->next; pos != del_list; pos = pos->next) {
1020 struct pending_extent_op *tmp;
1022 tmp = list_entry(pos, struct pending_extent_op, list);
1024 /* we only want to delete extent+ref at this stage */
1025 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1028 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1029 if (found_key.objectid != tmp->bytenr ||
1030 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1031 found_key.offset != tmp->num_bytes)
1034 /* check to make sure this extent only has one ref */
1035 ei = btrfs_item_ptr(leaf, cur_slot,
1036 struct btrfs_extent_item);
1037 if (btrfs_extent_refs(leaf, ei) != 1)
1040 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1041 if (found_key.objectid != tmp->bytenr ||
1042 found_key.type != BTRFS_EXTENT_REF_KEY ||
1043 found_key.offset != tmp->orig_parent)
1047 * the ref is right next to the extent, we can set the
1048 * ref count to 0 since we will delete them both now
1050 btrfs_set_extent_refs(leaf, ei, 0);
1052 /* pin down the bytes for this extent */
1053 mutex_lock(&info->pinned_mutex);
1054 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1055 tmp->num_bytes, tmp->level >=
1056 BTRFS_FIRST_FREE_OBJECTID);
1057 mutex_unlock(&info->pinned_mutex);
1061 * use the del field to tell if we need to go ahead and
1062 * free up the extent when we delete the item or not.
1065 bytes_freed += tmp->num_bytes;
1072 /* update the free space counters */
1073 spin_lock_irq(&info->delalloc_lock);
1074 super_used = btrfs_super_bytes_used(&info->super_copy);
1075 btrfs_set_super_bytes_used(&info->super_copy,
1076 super_used - bytes_freed);
1077 spin_unlock_irq(&info->delalloc_lock);
1079 root_used = btrfs_root_used(&extent_root->root_item);
1080 btrfs_set_root_used(&extent_root->root_item,
1081 root_used - bytes_freed);
1083 /* delete the items */
1084 ret = btrfs_del_items(trans, extent_root, path,
1085 path->slots[0], num_to_del);
1089 * loop through the extents we deleted and do the cleanup work
1092 for (pos = cur, n = pos->next; pos != end;
1093 pos = n, n = pos->next) {
1094 struct pending_extent_op *tmp;
1095 #ifdef BIO_RW_DISCARD
1097 struct btrfs_multi_bio *multi = NULL;
1099 tmp = list_entry(pos, struct pending_extent_op, list);
1102 * remember tmp->del tells us wether or not we pinned
1105 ret = update_block_group(trans, extent_root,
1106 tmp->bytenr, tmp->num_bytes, 0,
1110 #ifdef BIO_RW_DISCARD
1111 ret = btrfs_map_block(&info->mapping_tree, READ,
1112 tmp->bytenr, &map_length, &multi,
1115 struct btrfs_bio_stripe *stripe;
1118 stripe = multi->stripe;
1120 if (map_length > tmp->num_bytes)
1121 map_length = tmp->num_bytes;
1123 for (i = 0; i < multi->num_stripes;
1125 blkdev_issue_discard(stripe->dev->bdev,
1126 stripe->physical >> 9,
1131 list_del_init(&tmp->list);
1132 unlock_extent(&info->extent_ins, tmp->bytenr,
1133 tmp->bytenr + tmp->num_bytes - 1,
1137 } else if (refs && found_extent) {
1139 * the ref and extent were right next to eachother, but the
1140 * extent still has a ref, so just free the backref and keep
1143 ret = remove_extent_backref(trans, extent_root, path);
1146 list_del_init(&op->list);
1147 unlock_extent(&info->extent_ins, op->bytenr,
1148 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1152 * the extent has multiple refs and the backref we were looking
1153 * for was not right next to it, so just unlock and go next,
1156 list_del_init(&op->list);
1157 unlock_extent(&info->extent_ins, op->bytenr,
1158 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1162 btrfs_release_path(extent_root, path);
1163 if (!list_empty(del_list))
1167 btrfs_free_path(path);
1171 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root, u64 bytenr,
1173 u64 orig_parent, u64 parent,
1174 u64 orig_root, u64 ref_root,
1175 u64 orig_generation, u64 ref_generation,
1179 struct btrfs_root *extent_root = root->fs_info->extent_root;
1180 struct btrfs_path *path;
1182 if (root == root->fs_info->extent_root) {
1183 struct pending_extent_op *extent_op;
1186 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1187 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1188 mutex_lock(&root->fs_info->extent_ins_mutex);
1189 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1190 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1192 ret = get_state_private(&root->fs_info->extent_ins,
1195 extent_op = (struct pending_extent_op *)
1196 (unsigned long)priv;
1197 BUG_ON(extent_op->parent != orig_parent);
1198 BUG_ON(extent_op->generation != orig_generation);
1200 extent_op->parent = parent;
1201 extent_op->generation = ref_generation;
1203 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1206 extent_op->type = PENDING_BACKREF_UPDATE;
1207 extent_op->bytenr = bytenr;
1208 extent_op->num_bytes = num_bytes;
1209 extent_op->parent = parent;
1210 extent_op->orig_parent = orig_parent;
1211 extent_op->generation = ref_generation;
1212 extent_op->orig_generation = orig_generation;
1213 extent_op->level = (int)owner_objectid;
1214 INIT_LIST_HEAD(&extent_op->list);
1217 set_extent_bits(&root->fs_info->extent_ins,
1218 bytenr, bytenr + num_bytes - 1,
1219 EXTENT_WRITEBACK, GFP_NOFS);
1220 set_state_private(&root->fs_info->extent_ins,
1221 bytenr, (unsigned long)extent_op);
1223 mutex_unlock(&root->fs_info->extent_ins_mutex);
1227 path = btrfs_alloc_path();
1230 ret = lookup_extent_backref(trans, extent_root, path,
1231 bytenr, orig_parent, orig_root,
1232 orig_generation, owner_objectid, 1);
1235 ret = remove_extent_backref(trans, extent_root, path);
1238 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1239 parent, ref_root, ref_generation,
1242 finish_current_insert(trans, extent_root, 0);
1243 del_pending_extents(trans, extent_root, 0);
1245 btrfs_free_path(path);
1249 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1250 struct btrfs_root *root, u64 bytenr,
1251 u64 orig_parent, u64 parent,
1252 u64 ref_root, u64 ref_generation,
1256 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1257 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1259 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1260 parent, ref_root, ref_root,
1261 ref_generation, ref_generation,
1266 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1267 struct btrfs_root *root, u64 bytenr,
1268 u64 orig_parent, u64 parent,
1269 u64 orig_root, u64 ref_root,
1270 u64 orig_generation, u64 ref_generation,
1273 struct btrfs_path *path;
1275 struct btrfs_key key;
1276 struct extent_buffer *l;
1277 struct btrfs_extent_item *item;
1280 path = btrfs_alloc_path();
1285 key.objectid = bytenr;
1286 key.type = BTRFS_EXTENT_ITEM_KEY;
1287 key.offset = (u64)-1;
1289 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1293 BUG_ON(ret == 0 || path->slots[0] == 0);
1298 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1299 if (key.objectid != bytenr) {
1300 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1301 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1304 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1306 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1307 refs = btrfs_extent_refs(l, item);
1308 btrfs_set_extent_refs(l, item, refs + 1);
1309 btrfs_mark_buffer_dirty(path->nodes[0]);
1311 btrfs_release_path(root->fs_info->extent_root, path);
1314 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1315 path, bytenr, parent,
1316 ref_root, ref_generation,
1319 finish_current_insert(trans, root->fs_info->extent_root, 0);
1320 del_pending_extents(trans, root->fs_info->extent_root, 0);
1322 btrfs_free_path(path);
1326 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1328 u64 bytenr, u64 num_bytes, u64 parent,
1329 u64 ref_root, u64 ref_generation,
1333 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1334 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1336 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1337 0, ref_root, 0, ref_generation,
1342 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1343 struct btrfs_root *root)
1345 finish_current_insert(trans, root->fs_info->extent_root, 1);
1346 del_pending_extents(trans, root->fs_info->extent_root, 1);
1350 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1351 struct btrfs_root *root, u64 bytenr,
1352 u64 num_bytes, u32 *refs)
1354 struct btrfs_path *path;
1356 struct btrfs_key key;
1357 struct extent_buffer *l;
1358 struct btrfs_extent_item *item;
1360 WARN_ON(num_bytes < root->sectorsize);
1361 path = btrfs_alloc_path();
1363 key.objectid = bytenr;
1364 key.offset = num_bytes;
1365 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1366 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1371 btrfs_print_leaf(root, path->nodes[0]);
1372 printk("failed to find block number %Lu\n", bytenr);
1376 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1377 *refs = btrfs_extent_refs(l, item);
1379 btrfs_free_path(path);
1383 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1384 struct btrfs_root *root, u64 bytenr)
1386 struct btrfs_root *extent_root = root->fs_info->extent_root;
1387 struct btrfs_path *path;
1388 struct extent_buffer *leaf;
1389 struct btrfs_extent_ref *ref_item;
1390 struct btrfs_key key;
1391 struct btrfs_key found_key;
1397 key.objectid = bytenr;
1398 key.offset = (u64)-1;
1399 key.type = BTRFS_EXTENT_ITEM_KEY;
1401 path = btrfs_alloc_path();
1402 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1408 if (path->slots[0] == 0)
1412 leaf = path->nodes[0];
1413 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1415 if (found_key.objectid != bytenr ||
1416 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1419 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1421 leaf = path->nodes[0];
1422 nritems = btrfs_header_nritems(leaf);
1423 if (path->slots[0] >= nritems) {
1424 ret = btrfs_next_leaf(extent_root, path);
1431 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1432 if (found_key.objectid != bytenr)
1435 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1440 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1441 struct btrfs_extent_ref);
1442 ref_root = btrfs_ref_root(leaf, ref_item);
1443 if (ref_root != root->root_key.objectid &&
1444 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1448 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1457 btrfs_free_path(path);
1461 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1462 struct extent_buffer *buf, u32 nr_extents)
1464 struct btrfs_key key;
1465 struct btrfs_file_extent_item *fi;
1473 if (!root->ref_cows)
1476 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1478 root_gen = root->root_key.offset;
1481 root_gen = trans->transid - 1;
1484 level = btrfs_header_level(buf);
1485 nritems = btrfs_header_nritems(buf);
1488 struct btrfs_leaf_ref *ref;
1489 struct btrfs_extent_info *info;
1491 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1497 ref->root_gen = root_gen;
1498 ref->bytenr = buf->start;
1499 ref->owner = btrfs_header_owner(buf);
1500 ref->generation = btrfs_header_generation(buf);
1501 ref->nritems = nr_extents;
1502 info = ref->extents;
1504 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1506 btrfs_item_key_to_cpu(buf, &key, i);
1507 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1509 fi = btrfs_item_ptr(buf, i,
1510 struct btrfs_file_extent_item);
1511 if (btrfs_file_extent_type(buf, fi) ==
1512 BTRFS_FILE_EXTENT_INLINE)
1514 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1515 if (disk_bytenr == 0)
1518 info->bytenr = disk_bytenr;
1520 btrfs_file_extent_disk_num_bytes(buf, fi);
1521 info->objectid = key.objectid;
1522 info->offset = key.offset;
1526 ret = btrfs_add_leaf_ref(root, ref, shared);
1527 if (ret == -EEXIST && shared) {
1528 struct btrfs_leaf_ref *old;
1529 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1531 btrfs_remove_leaf_ref(root, old);
1532 btrfs_free_leaf_ref(root, old);
1533 ret = btrfs_add_leaf_ref(root, ref, shared);
1536 btrfs_free_leaf_ref(root, ref);
1542 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1543 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1550 u64 orig_generation;
1552 u32 nr_file_extents = 0;
1553 struct btrfs_key key;
1554 struct btrfs_file_extent_item *fi;
1559 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1560 u64, u64, u64, u64, u64, u64, u64, u64);
1562 ref_root = btrfs_header_owner(buf);
1563 ref_generation = btrfs_header_generation(buf);
1564 orig_root = btrfs_header_owner(orig_buf);
1565 orig_generation = btrfs_header_generation(orig_buf);
1567 nritems = btrfs_header_nritems(buf);
1568 level = btrfs_header_level(buf);
1570 if (root->ref_cows) {
1571 process_func = __btrfs_inc_extent_ref;
1574 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1577 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1579 process_func = __btrfs_update_extent_ref;
1582 for (i = 0; i < nritems; i++) {
1585 btrfs_item_key_to_cpu(buf, &key, i);
1586 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1588 fi = btrfs_item_ptr(buf, i,
1589 struct btrfs_file_extent_item);
1590 if (btrfs_file_extent_type(buf, fi) ==
1591 BTRFS_FILE_EXTENT_INLINE)
1593 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1599 ret = process_func(trans, root, bytenr,
1600 orig_buf->start, buf->start,
1601 orig_root, ref_root,
1602 orig_generation, ref_generation,
1611 bytenr = btrfs_node_blockptr(buf, i);
1612 ret = process_func(trans, root, bytenr,
1613 orig_buf->start, buf->start,
1614 orig_root, ref_root,
1615 orig_generation, ref_generation,
1627 *nr_extents = nr_file_extents;
1629 *nr_extents = nritems;
1637 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1638 struct btrfs_root *root, struct extent_buffer *orig_buf,
1639 struct extent_buffer *buf, int start_slot, int nr)
1646 u64 orig_generation;
1647 struct btrfs_key key;
1648 struct btrfs_file_extent_item *fi;
1654 BUG_ON(start_slot < 0);
1655 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1657 ref_root = btrfs_header_owner(buf);
1658 ref_generation = btrfs_header_generation(buf);
1659 orig_root = btrfs_header_owner(orig_buf);
1660 orig_generation = btrfs_header_generation(orig_buf);
1661 level = btrfs_header_level(buf);
1663 if (!root->ref_cows) {
1665 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1668 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1672 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1675 btrfs_item_key_to_cpu(buf, &key, slot);
1676 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1678 fi = btrfs_item_ptr(buf, slot,
1679 struct btrfs_file_extent_item);
1680 if (btrfs_file_extent_type(buf, fi) ==
1681 BTRFS_FILE_EXTENT_INLINE)
1683 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1686 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1687 orig_buf->start, buf->start,
1688 orig_root, ref_root,
1689 orig_generation, ref_generation,
1694 bytenr = btrfs_node_blockptr(buf, slot);
1695 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1696 orig_buf->start, buf->start,
1697 orig_root, ref_root,
1698 orig_generation, ref_generation,
1710 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1711 struct btrfs_root *root,
1712 struct btrfs_path *path,
1713 struct btrfs_block_group_cache *cache)
1717 struct btrfs_root *extent_root = root->fs_info->extent_root;
1719 struct extent_buffer *leaf;
1721 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1726 leaf = path->nodes[0];
1727 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1728 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1729 btrfs_mark_buffer_dirty(leaf);
1730 btrfs_release_path(extent_root, path);
1732 finish_current_insert(trans, extent_root, 0);
1733 pending_ret = del_pending_extents(trans, extent_root, 0);
1742 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root)
1745 struct btrfs_block_group_cache *cache, *entry;
1749 struct btrfs_path *path;
1752 path = btrfs_alloc_path();
1758 spin_lock(&root->fs_info->block_group_cache_lock);
1759 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1760 n; n = rb_next(n)) {
1761 entry = rb_entry(n, struct btrfs_block_group_cache,
1768 spin_unlock(&root->fs_info->block_group_cache_lock);
1774 last += cache->key.offset;
1776 err = write_one_cache_group(trans, root,
1779 * if we fail to write the cache group, we want
1780 * to keep it marked dirty in hopes that a later
1788 btrfs_free_path(path);
1792 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1793 u64 total_bytes, u64 bytes_used,
1794 struct btrfs_space_info **space_info)
1796 struct btrfs_space_info *found;
1798 found = __find_space_info(info, flags);
1800 spin_lock(&found->lock);
1801 found->total_bytes += total_bytes;
1802 found->bytes_used += bytes_used;
1804 spin_unlock(&found->lock);
1805 *space_info = found;
1808 found = kzalloc(sizeof(*found), GFP_NOFS);
1812 list_add(&found->list, &info->space_info);
1813 INIT_LIST_HEAD(&found->block_groups);
1814 init_rwsem(&found->groups_sem);
1815 spin_lock_init(&found->lock);
1816 found->flags = flags;
1817 found->total_bytes = total_bytes;
1818 found->bytes_used = bytes_used;
1819 found->bytes_pinned = 0;
1820 found->bytes_reserved = 0;
1821 found->bytes_readonly = 0;
1823 found->force_alloc = 0;
1824 *space_info = found;
1828 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1830 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1831 BTRFS_BLOCK_GROUP_RAID1 |
1832 BTRFS_BLOCK_GROUP_RAID10 |
1833 BTRFS_BLOCK_GROUP_DUP);
1835 if (flags & BTRFS_BLOCK_GROUP_DATA)
1836 fs_info->avail_data_alloc_bits |= extra_flags;
1837 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1838 fs_info->avail_metadata_alloc_bits |= extra_flags;
1839 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1840 fs_info->avail_system_alloc_bits |= extra_flags;
1844 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1846 spin_lock(&cache->space_info->lock);
1847 spin_lock(&cache->lock);
1849 cache->space_info->bytes_readonly += cache->key.offset -
1850 btrfs_block_group_used(&cache->item);
1853 spin_unlock(&cache->lock);
1854 spin_unlock(&cache->space_info->lock);
1857 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1859 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1861 if (num_devices == 1)
1862 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1863 if (num_devices < 4)
1864 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1866 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1867 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1868 BTRFS_BLOCK_GROUP_RAID10))) {
1869 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1872 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1873 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1874 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1877 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1878 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1879 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1880 (flags & BTRFS_BLOCK_GROUP_DUP)))
1881 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1885 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *extent_root, u64 alloc_bytes,
1887 u64 flags, int force)
1889 struct btrfs_space_info *space_info;
1893 mutex_lock(&extent_root->fs_info->chunk_mutex);
1895 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1897 space_info = __find_space_info(extent_root->fs_info, flags);
1899 ret = update_space_info(extent_root->fs_info, flags,
1903 BUG_ON(!space_info);
1905 spin_lock(&space_info->lock);
1906 if (space_info->force_alloc) {
1908 space_info->force_alloc = 0;
1910 if (space_info->full) {
1911 spin_unlock(&space_info->lock);
1915 thresh = space_info->total_bytes - space_info->bytes_readonly;
1916 thresh = div_factor(thresh, 6);
1918 (space_info->bytes_used + space_info->bytes_pinned +
1919 space_info->bytes_reserved + alloc_bytes) < thresh) {
1920 spin_unlock(&space_info->lock);
1923 spin_unlock(&space_info->lock);
1925 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1927 printk("space info full %Lu\n", flags);
1928 space_info->full = 1;
1931 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1935 static int update_block_group(struct btrfs_trans_handle *trans,
1936 struct btrfs_root *root,
1937 u64 bytenr, u64 num_bytes, int alloc,
1940 struct btrfs_block_group_cache *cache;
1941 struct btrfs_fs_info *info = root->fs_info;
1942 u64 total = num_bytes;
1947 cache = btrfs_lookup_block_group(info, bytenr);
1950 byte_in_group = bytenr - cache->key.objectid;
1951 WARN_ON(byte_in_group > cache->key.offset);
1953 spin_lock(&cache->space_info->lock);
1954 spin_lock(&cache->lock);
1956 old_val = btrfs_block_group_used(&cache->item);
1957 num_bytes = min(total, cache->key.offset - byte_in_group);
1959 old_val += num_bytes;
1960 cache->space_info->bytes_used += num_bytes;
1962 cache->space_info->bytes_readonly -= num_bytes;
1965 btrfs_set_block_group_used(&cache->item, old_val);
1966 spin_unlock(&cache->lock);
1967 spin_unlock(&cache->space_info->lock);
1969 old_val -= num_bytes;
1970 cache->space_info->bytes_used -= num_bytes;
1972 cache->space_info->bytes_readonly += num_bytes;
1973 btrfs_set_block_group_used(&cache->item, old_val);
1974 spin_unlock(&cache->lock);
1975 spin_unlock(&cache->space_info->lock);
1978 ret = btrfs_add_free_space(cache, bytenr,
1985 bytenr += num_bytes;
1990 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1992 struct btrfs_block_group_cache *cache;
1994 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1998 return cache->key.objectid;
2001 int btrfs_update_pinned_extents(struct btrfs_root *root,
2002 u64 bytenr, u64 num, int pin)
2005 struct btrfs_block_group_cache *cache;
2006 struct btrfs_fs_info *fs_info = root->fs_info;
2008 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2010 set_extent_dirty(&fs_info->pinned_extents,
2011 bytenr, bytenr + num - 1, GFP_NOFS);
2013 clear_extent_dirty(&fs_info->pinned_extents,
2014 bytenr, bytenr + num - 1, GFP_NOFS);
2017 cache = btrfs_lookup_block_group(fs_info, bytenr);
2019 len = min(num, cache->key.offset -
2020 (bytenr - cache->key.objectid));
2022 spin_lock(&cache->space_info->lock);
2023 spin_lock(&cache->lock);
2024 cache->pinned += len;
2025 cache->space_info->bytes_pinned += len;
2026 spin_unlock(&cache->lock);
2027 spin_unlock(&cache->space_info->lock);
2028 fs_info->total_pinned += len;
2030 spin_lock(&cache->space_info->lock);
2031 spin_lock(&cache->lock);
2032 cache->pinned -= len;
2033 cache->space_info->bytes_pinned -= len;
2034 spin_unlock(&cache->lock);
2035 spin_unlock(&cache->space_info->lock);
2036 fs_info->total_pinned -= len;
2044 static int update_reserved_extents(struct btrfs_root *root,
2045 u64 bytenr, u64 num, int reserve)
2048 struct btrfs_block_group_cache *cache;
2049 struct btrfs_fs_info *fs_info = root->fs_info;
2052 cache = btrfs_lookup_block_group(fs_info, bytenr);
2054 len = min(num, cache->key.offset -
2055 (bytenr - cache->key.objectid));
2057 spin_lock(&cache->space_info->lock);
2058 spin_lock(&cache->lock);
2060 cache->reserved += len;
2061 cache->space_info->bytes_reserved += len;
2063 cache->reserved -= len;
2064 cache->space_info->bytes_reserved -= len;
2066 spin_unlock(&cache->lock);
2067 spin_unlock(&cache->space_info->lock);
2074 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2079 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2082 mutex_lock(&root->fs_info->pinned_mutex);
2084 ret = find_first_extent_bit(pinned_extents, last,
2085 &start, &end, EXTENT_DIRTY);
2088 set_extent_dirty(copy, start, end, GFP_NOFS);
2091 mutex_unlock(&root->fs_info->pinned_mutex);
2095 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2096 struct btrfs_root *root,
2097 struct extent_io_tree *unpin)
2102 struct btrfs_block_group_cache *cache;
2104 mutex_lock(&root->fs_info->pinned_mutex);
2106 ret = find_first_extent_bit(unpin, 0, &start, &end,
2110 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2111 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2112 cache = btrfs_lookup_block_group(root->fs_info, start);
2114 btrfs_add_free_space(cache, start, end - start + 1);
2115 if (need_resched()) {
2116 mutex_unlock(&root->fs_info->pinned_mutex);
2118 mutex_lock(&root->fs_info->pinned_mutex);
2121 mutex_unlock(&root->fs_info->pinned_mutex);
2125 static int finish_current_insert(struct btrfs_trans_handle *trans,
2126 struct btrfs_root *extent_root, int all)
2133 struct btrfs_fs_info *info = extent_root->fs_info;
2134 struct btrfs_path *path;
2135 struct pending_extent_op *extent_op, *tmp;
2136 struct list_head insert_list, update_list;
2138 int num_inserts = 0, max_inserts;
2140 path = btrfs_alloc_path();
2141 INIT_LIST_HEAD(&insert_list);
2142 INIT_LIST_HEAD(&update_list);
2144 max_inserts = extent_root->leafsize /
2145 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2146 sizeof(struct btrfs_extent_ref) +
2147 sizeof(struct btrfs_extent_item));
2149 mutex_lock(&info->extent_ins_mutex);
2151 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2152 &end, EXTENT_WRITEBACK);
2154 if (skipped && all && !num_inserts) {
2159 mutex_unlock(&info->extent_ins_mutex);
2163 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2167 if (need_resched()) {
2168 mutex_unlock(&info->extent_ins_mutex);
2170 mutex_lock(&info->extent_ins_mutex);
2175 ret = get_state_private(&info->extent_ins, start, &priv);
2177 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2179 if (extent_op->type == PENDING_EXTENT_INSERT) {
2181 list_add_tail(&extent_op->list, &insert_list);
2183 if (num_inserts == max_inserts) {
2184 mutex_unlock(&info->extent_ins_mutex);
2187 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2188 list_add_tail(&extent_op->list, &update_list);
2196 * process the update list, clear the writeback bit for it, and if
2197 * somebody marked this thing for deletion then just unlock it and be
2198 * done, the free_extents will handle it
2200 mutex_lock(&info->extent_ins_mutex);
2201 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2202 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2203 extent_op->bytenr + extent_op->num_bytes - 1,
2204 EXTENT_WRITEBACK, GFP_NOFS);
2205 if (extent_op->del) {
2206 list_del_init(&extent_op->list);
2207 unlock_extent(&info->extent_ins, extent_op->bytenr,
2208 extent_op->bytenr + extent_op->num_bytes
2213 mutex_unlock(&info->extent_ins_mutex);
2216 * still have things left on the update list, go ahead an update
2219 if (!list_empty(&update_list)) {
2220 ret = update_backrefs(trans, extent_root, path, &update_list);
2225 * if no inserts need to be done, but we skipped some extents and we
2226 * need to make sure everything is cleaned then reset everything and
2227 * go back to the beginning
2229 if (!num_inserts && all && skipped) {
2232 INIT_LIST_HEAD(&update_list);
2233 INIT_LIST_HEAD(&insert_list);
2235 } else if (!num_inserts) {
2240 * process the insert extents list. Again if we are deleting this
2241 * extent, then just unlock it, pin down the bytes if need be, and be
2242 * done with it. Saves us from having to actually insert the extent
2243 * into the tree and then subsequently come along and delete it
2245 mutex_lock(&info->extent_ins_mutex);
2246 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2247 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2248 extent_op->bytenr + extent_op->num_bytes - 1,
2249 EXTENT_WRITEBACK, GFP_NOFS);
2250 if (extent_op->del) {
2251 list_del_init(&extent_op->list);
2252 unlock_extent(&info->extent_ins, extent_op->bytenr,
2253 extent_op->bytenr + extent_op->num_bytes
2256 mutex_lock(&extent_root->fs_info->pinned_mutex);
2257 ret = pin_down_bytes(trans, extent_root,
2259 extent_op->num_bytes, 0);
2260 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2262 ret = update_block_group(trans, extent_root,
2264 extent_op->num_bytes,
2271 mutex_unlock(&info->extent_ins_mutex);
2273 ret = insert_extents(trans, extent_root, path, &insert_list,
2278 * if we broke out of the loop in order to insert stuff because we hit
2279 * the maximum number of inserts at a time we can handle, then loop
2280 * back and pick up where we left off
2282 if (num_inserts == max_inserts) {
2283 INIT_LIST_HEAD(&insert_list);
2284 INIT_LIST_HEAD(&update_list);
2290 * again, if we need to make absolutely sure there are no more pending
2291 * extent operations left and we know that we skipped some, go back to
2292 * the beginning and do it all again
2294 if (all && skipped) {
2295 INIT_LIST_HEAD(&insert_list);
2296 INIT_LIST_HEAD(&update_list);
2303 btrfs_free_path(path);
2307 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2308 struct btrfs_root *root,
2309 u64 bytenr, u64 num_bytes, int is_data)
2312 struct extent_buffer *buf;
2317 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2321 /* we can reuse a block if it hasn't been written
2322 * and it is from this transaction. We can't
2323 * reuse anything from the tree log root because
2324 * it has tiny sub-transactions.
2326 if (btrfs_buffer_uptodate(buf, 0) &&
2327 btrfs_try_tree_lock(buf)) {
2328 u64 header_owner = btrfs_header_owner(buf);
2329 u64 header_transid = btrfs_header_generation(buf);
2330 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2331 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2332 header_transid == trans->transid &&
2333 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2334 clean_tree_block(NULL, root, buf);
2335 btrfs_tree_unlock(buf);
2336 free_extent_buffer(buf);
2339 btrfs_tree_unlock(buf);
2341 free_extent_buffer(buf);
2343 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2350 * remove an extent from the root, returns 0 on success
2352 static int __free_extent(struct btrfs_trans_handle *trans,
2353 struct btrfs_root *root,
2354 u64 bytenr, u64 num_bytes, u64 parent,
2355 u64 root_objectid, u64 ref_generation,
2356 u64 owner_objectid, int pin, int mark_free)
2358 struct btrfs_path *path;
2359 struct btrfs_key key;
2360 struct btrfs_fs_info *info = root->fs_info;
2361 struct btrfs_root *extent_root = info->extent_root;
2362 struct extent_buffer *leaf;
2364 int extent_slot = 0;
2365 int found_extent = 0;
2367 struct btrfs_extent_item *ei;
2370 key.objectid = bytenr;
2371 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2372 key.offset = num_bytes;
2373 path = btrfs_alloc_path();
2378 ret = lookup_extent_backref(trans, extent_root, path,
2379 bytenr, parent, root_objectid,
2380 ref_generation, owner_objectid, 1);
2382 struct btrfs_key found_key;
2383 extent_slot = path->slots[0];
2384 while(extent_slot > 0) {
2386 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2388 if (found_key.objectid != bytenr)
2390 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2391 found_key.offset == num_bytes) {
2395 if (path->slots[0] - extent_slot > 5)
2398 if (!found_extent) {
2399 ret = remove_extent_backref(trans, extent_root, path);
2401 btrfs_release_path(extent_root, path);
2402 ret = btrfs_search_slot(trans, extent_root,
2405 printk(KERN_ERR "umm, got %d back from search"
2406 ", was looking for %Lu\n", ret,
2408 btrfs_print_leaf(extent_root, path->nodes[0]);
2411 extent_slot = path->slots[0];
2414 btrfs_print_leaf(extent_root, path->nodes[0]);
2416 printk("Unable to find ref byte nr %Lu root %Lu "
2417 "gen %Lu owner %Lu\n", bytenr,
2418 root_objectid, ref_generation, owner_objectid);
2421 leaf = path->nodes[0];
2422 ei = btrfs_item_ptr(leaf, extent_slot,
2423 struct btrfs_extent_item);
2424 refs = btrfs_extent_refs(leaf, ei);
2427 btrfs_set_extent_refs(leaf, ei, refs);
2429 btrfs_mark_buffer_dirty(leaf);
2431 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2432 struct btrfs_extent_ref *ref;
2433 ref = btrfs_item_ptr(leaf, path->slots[0],
2434 struct btrfs_extent_ref);
2435 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2436 /* if the back ref and the extent are next to each other
2437 * they get deleted below in one shot
2439 path->slots[0] = extent_slot;
2441 } else if (found_extent) {
2442 /* otherwise delete the extent back ref */
2443 ret = remove_extent_backref(trans, extent_root, path);
2445 /* if refs are 0, we need to setup the path for deletion */
2447 btrfs_release_path(extent_root, path);
2448 ret = btrfs_search_slot(trans, extent_root, &key, path,
2457 #ifdef BIO_RW_DISCARD
2458 u64 map_length = num_bytes;
2459 struct btrfs_multi_bio *multi = NULL;
2463 mutex_lock(&root->fs_info->pinned_mutex);
2464 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2465 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2466 mutex_unlock(&root->fs_info->pinned_mutex);
2472 /* block accounting for super block */
2473 spin_lock_irq(&info->delalloc_lock);
2474 super_used = btrfs_super_bytes_used(&info->super_copy);
2475 btrfs_set_super_bytes_used(&info->super_copy,
2476 super_used - num_bytes);
2477 spin_unlock_irq(&info->delalloc_lock);
2479 /* block accounting for root item */
2480 root_used = btrfs_root_used(&root->root_item);
2481 btrfs_set_root_used(&root->root_item,
2482 root_used - num_bytes);
2483 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2486 btrfs_release_path(extent_root, path);
2487 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2491 #ifdef BIO_RW_DISCARD
2492 /* Tell the block device(s) that the sectors can be discarded */
2493 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2494 bytenr, &map_length, &multi, 0);
2496 struct btrfs_bio_stripe *stripe = multi->stripes;
2499 if (map_length > num_bytes)
2500 map_length = num_bytes;
2502 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2503 blkdev_issue_discard(stripe->dev->bdev,
2504 stripe->physical >> 9,
2511 btrfs_free_path(path);
2512 finish_current_insert(trans, extent_root, 0);
2517 * find all the blocks marked as pending in the radix tree and remove
2518 * them from the extent map
2520 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2521 btrfs_root *extent_root, int all)
2529 int nr = 0, skipped = 0;
2530 struct extent_io_tree *pending_del;
2531 struct extent_io_tree *extent_ins;
2532 struct pending_extent_op *extent_op;
2533 struct btrfs_fs_info *info = extent_root->fs_info;
2534 struct list_head delete_list;
2536 INIT_LIST_HEAD(&delete_list);
2537 extent_ins = &extent_root->fs_info->extent_ins;
2538 pending_del = &extent_root->fs_info->pending_del;
2541 mutex_lock(&info->extent_ins_mutex);
2543 ret = find_first_extent_bit(pending_del, search, &start, &end,
2546 if (all && skipped && !nr) {
2550 mutex_unlock(&info->extent_ins_mutex);
2554 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2559 if (need_resched()) {
2560 mutex_unlock(&info->extent_ins_mutex);
2562 mutex_lock(&info->extent_ins_mutex);
2569 ret = get_state_private(pending_del, start, &priv);
2571 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2573 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2575 if (!test_range_bit(extent_ins, start, end,
2576 EXTENT_WRITEBACK, 0)) {
2577 list_add_tail(&extent_op->list, &delete_list);
2582 ret = get_state_private(&info->extent_ins, start,
2585 extent_op = (struct pending_extent_op *)
2586 (unsigned long)priv;
2588 clear_extent_bits(&info->extent_ins, start, end,
2589 EXTENT_WRITEBACK, GFP_NOFS);
2591 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2592 list_add_tail(&extent_op->list, &delete_list);
2598 mutex_lock(&extent_root->fs_info->pinned_mutex);
2599 ret = pin_down_bytes(trans, extent_root, start,
2600 end + 1 - start, 0);
2601 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2603 ret = update_block_group(trans, extent_root, start,
2604 end + 1 - start, 0, ret > 0);
2606 unlock_extent(extent_ins, start, end, GFP_NOFS);
2615 if (need_resched()) {
2616 mutex_unlock(&info->extent_ins_mutex);
2618 mutex_lock(&info->extent_ins_mutex);
2623 ret = free_extents(trans, extent_root, &delete_list);
2627 if (all && skipped) {
2628 INIT_LIST_HEAD(&delete_list);
2638 * remove an extent from the root, returns 0 on success
2640 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2641 struct btrfs_root *root,
2642 u64 bytenr, u64 num_bytes, u64 parent,
2643 u64 root_objectid, u64 ref_generation,
2644 u64 owner_objectid, int pin)
2646 struct btrfs_root *extent_root = root->fs_info->extent_root;
2650 WARN_ON(num_bytes < root->sectorsize);
2651 if (root == extent_root) {
2652 struct pending_extent_op *extent_op = NULL;
2654 mutex_lock(&root->fs_info->extent_ins_mutex);
2655 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2656 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2658 ret = get_state_private(&root->fs_info->extent_ins,
2661 extent_op = (struct pending_extent_op *)
2662 (unsigned long)priv;
2665 if (extent_op->type == PENDING_EXTENT_INSERT) {
2666 mutex_unlock(&root->fs_info->extent_ins_mutex);
2672 ref_generation = extent_op->orig_generation;
2673 parent = extent_op->orig_parent;
2676 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2679 extent_op->type = PENDING_EXTENT_DELETE;
2680 extent_op->bytenr = bytenr;
2681 extent_op->num_bytes = num_bytes;
2682 extent_op->parent = parent;
2683 extent_op->orig_parent = parent;
2684 extent_op->generation = ref_generation;
2685 extent_op->orig_generation = ref_generation;
2686 extent_op->level = (int)owner_objectid;
2687 INIT_LIST_HEAD(&extent_op->list);
2690 set_extent_bits(&root->fs_info->pending_del,
2691 bytenr, bytenr + num_bytes - 1,
2692 EXTENT_WRITEBACK, GFP_NOFS);
2693 set_state_private(&root->fs_info->pending_del,
2694 bytenr, (unsigned long)extent_op);
2695 mutex_unlock(&root->fs_info->extent_ins_mutex);
2698 /* if metadata always pin */
2699 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2700 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2701 struct btrfs_block_group_cache *cache;
2703 /* btrfs_free_reserved_extent */
2704 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2706 btrfs_add_free_space(cache, bytenr, num_bytes);
2707 update_reserved_extents(root, bytenr, num_bytes, 0);
2713 /* if data pin when any transaction has committed this */
2714 if (ref_generation != trans->transid)
2717 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2718 root_objectid, ref_generation,
2719 owner_objectid, pin, pin == 0);
2721 finish_current_insert(trans, root->fs_info->extent_root, 0);
2722 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2723 return ret ? ret : pending_ret;
2726 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2727 struct btrfs_root *root,
2728 u64 bytenr, u64 num_bytes, u64 parent,
2729 u64 root_objectid, u64 ref_generation,
2730 u64 owner_objectid, int pin)
2734 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2735 root_objectid, ref_generation,
2736 owner_objectid, pin);
2740 static u64 stripe_align(struct btrfs_root *root, u64 val)
2742 u64 mask = ((u64)root->stripesize - 1);
2743 u64 ret = (val + mask) & ~mask;
2748 * walks the btree of allocated extents and find a hole of a given size.
2749 * The key ins is changed to record the hole:
2750 * ins->objectid == block start
2751 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2752 * ins->offset == number of blocks
2753 * Any available blocks before search_start are skipped.
2755 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2756 struct btrfs_root *orig_root,
2757 u64 num_bytes, u64 empty_size,
2758 u64 search_start, u64 search_end,
2759 u64 hint_byte, struct btrfs_key *ins,
2760 u64 exclude_start, u64 exclude_nr,
2764 struct btrfs_root * root = orig_root->fs_info->extent_root;
2765 u64 total_needed = num_bytes;
2766 u64 *last_ptr = NULL;
2767 u64 last_wanted = 0;
2768 struct btrfs_block_group_cache *block_group = NULL;
2769 int chunk_alloc_done = 0;
2770 int empty_cluster = 2 * 1024 * 1024;
2771 int allowed_chunk_alloc = 0;
2772 struct list_head *head = NULL, *cur = NULL;
2775 struct btrfs_space_info *space_info;
2777 WARN_ON(num_bytes < root->sectorsize);
2778 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2782 if (orig_root->ref_cows || empty_size)
2783 allowed_chunk_alloc = 1;
2785 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2786 last_ptr = &root->fs_info->last_alloc;
2787 empty_cluster = 64 * 1024;
2790 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2791 last_ptr = &root->fs_info->last_data_alloc;
2795 hint_byte = *last_ptr;
2796 last_wanted = *last_ptr;
2798 empty_size += empty_cluster;
2802 search_start = max(search_start, first_logical_byte(root, 0));
2803 search_start = max(search_start, hint_byte);
2805 if (last_wanted && search_start != last_wanted) {
2807 empty_size += empty_cluster;
2810 total_needed += empty_size;
2811 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2813 block_group = btrfs_lookup_first_block_group(root->fs_info,
2815 space_info = __find_space_info(root->fs_info, data);
2817 down_read(&space_info->groups_sem);
2819 struct btrfs_free_space *free_space;
2821 * the only way this happens if our hint points to a block
2822 * group thats not of the proper type, while looping this
2823 * should never happen
2829 goto new_group_no_lock;
2831 mutex_lock(&block_group->alloc_mutex);
2832 if (unlikely(!block_group_bits(block_group, data)))
2835 ret = cache_block_group(root, block_group);
2837 mutex_unlock(&block_group->alloc_mutex);
2841 if (block_group->ro)
2844 free_space = btrfs_find_free_space(block_group, search_start,
2847 u64 start = block_group->key.objectid;
2848 u64 end = block_group->key.objectid +
2849 block_group->key.offset;
2851 search_start = stripe_align(root, free_space->offset);
2853 /* move on to the next group */
2854 if (search_start + num_bytes >= search_end)
2857 /* move on to the next group */
2858 if (search_start + num_bytes > end)
2861 if (last_wanted && search_start != last_wanted) {
2862 total_needed += empty_cluster;
2863 empty_size += empty_cluster;
2866 * if search_start is still in this block group
2867 * then we just re-search this block group
2869 if (search_start >= start &&
2870 search_start < end) {
2871 mutex_unlock(&block_group->alloc_mutex);
2875 /* else we go to the next block group */
2879 if (exclude_nr > 0 &&
2880 (search_start + num_bytes > exclude_start &&
2881 search_start < exclude_start + exclude_nr)) {
2882 search_start = exclude_start + exclude_nr;
2884 * if search_start is still in this block group
2885 * then we just re-search this block group
2887 if (search_start >= start &&
2888 search_start < end) {
2889 mutex_unlock(&block_group->alloc_mutex);
2894 /* else we go to the next block group */
2898 ins->objectid = search_start;
2899 ins->offset = num_bytes;
2901 btrfs_remove_free_space_lock(block_group, search_start,
2903 /* we are all good, lets return */
2904 mutex_unlock(&block_group->alloc_mutex);
2908 mutex_unlock(&block_group->alloc_mutex);
2910 /* don't try to compare new allocations against the
2911 * last allocation any more
2916 * Here's how this works.
2917 * loop == 0: we were searching a block group via a hint
2918 * and didn't find anything, so we start at
2919 * the head of the block groups and keep searching
2920 * loop == 1: we're searching through all of the block groups
2921 * if we hit the head again we have searched
2922 * all of the block groups for this space and we
2923 * need to try and allocate, if we cant error out.
2924 * loop == 2: we allocated more space and are looping through
2925 * all of the block groups again.
2928 head = &space_info->block_groups;
2931 } else if (loop == 1 && cur == head) {
2934 /* at this point we give up on the empty_size
2935 * allocations and just try to allocate the min
2938 * The extra_loop field was set if an empty_size
2939 * allocation was attempted above, and if this
2940 * is try we need to try the loop again without
2941 * the additional empty_size.
2943 total_needed -= empty_size;
2945 keep_going = extra_loop;
2948 if (allowed_chunk_alloc && !chunk_alloc_done) {
2949 up_read(&space_info->groups_sem);
2950 ret = do_chunk_alloc(trans, root, num_bytes +
2951 2 * 1024 * 1024, data, 1);
2952 down_read(&space_info->groups_sem);
2955 head = &space_info->block_groups;
2957 * we've allocated a new chunk, keep
2961 chunk_alloc_done = 1;
2962 } else if (!allowed_chunk_alloc) {
2963 space_info->force_alloc = 1;
2972 } else if (cur == head) {
2976 block_group = list_entry(cur, struct btrfs_block_group_cache,
2978 search_start = block_group->key.objectid;
2982 /* we found what we needed */
2983 if (ins->objectid) {
2984 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2985 trans->block_group = block_group;
2988 *last_ptr = ins->objectid + ins->offset;
2991 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
2992 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
2993 loop, allowed_chunk_alloc);
2997 up_read(&space_info->groups_sem);
3001 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3003 struct btrfs_block_group_cache *cache;
3004 struct list_head *l;
3006 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
3007 info->total_bytes - info->bytes_used - info->bytes_pinned -
3008 info->bytes_reserved, (info->full) ? "" : "not ");
3010 down_read(&info->groups_sem);
3011 list_for_each(l, &info->block_groups) {
3012 cache = list_entry(l, struct btrfs_block_group_cache, list);
3013 spin_lock(&cache->lock);
3014 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
3015 "%Lu pinned %Lu reserved\n",
3016 cache->key.objectid, cache->key.offset,
3017 btrfs_block_group_used(&cache->item),
3018 cache->pinned, cache->reserved);
3019 btrfs_dump_free_space(cache, bytes);
3020 spin_unlock(&cache->lock);
3022 up_read(&info->groups_sem);
3025 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3026 struct btrfs_root *root,
3027 u64 num_bytes, u64 min_alloc_size,
3028 u64 empty_size, u64 hint_byte,
3029 u64 search_end, struct btrfs_key *ins,
3033 u64 search_start = 0;
3035 struct btrfs_fs_info *info = root->fs_info;
3038 alloc_profile = info->avail_data_alloc_bits &
3039 info->data_alloc_profile;
3040 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3041 } else if (root == root->fs_info->chunk_root) {
3042 alloc_profile = info->avail_system_alloc_bits &
3043 info->system_alloc_profile;
3044 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3046 alloc_profile = info->avail_metadata_alloc_bits &
3047 info->metadata_alloc_profile;
3048 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3051 data = btrfs_reduce_alloc_profile(root, data);
3053 * the only place that sets empty_size is btrfs_realloc_node, which
3054 * is not called recursively on allocations
3056 if (empty_size || root->ref_cows) {
3057 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3058 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3060 BTRFS_BLOCK_GROUP_METADATA |
3061 (info->metadata_alloc_profile &
3062 info->avail_metadata_alloc_bits), 0);
3064 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3065 num_bytes + 2 * 1024 * 1024, data, 0);
3068 WARN_ON(num_bytes < root->sectorsize);
3069 ret = find_free_extent(trans, root, num_bytes, empty_size,
3070 search_start, search_end, hint_byte, ins,
3071 trans->alloc_exclude_start,
3072 trans->alloc_exclude_nr, data);
3074 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3075 num_bytes = num_bytes >> 1;
3076 num_bytes = num_bytes & ~(root->sectorsize - 1);
3077 num_bytes = max(num_bytes, min_alloc_size);
3078 do_chunk_alloc(trans, root->fs_info->extent_root,
3079 num_bytes, data, 1);
3083 struct btrfs_space_info *sinfo;
3085 sinfo = __find_space_info(root->fs_info, data);
3086 printk("allocation failed flags %Lu, wanted %Lu\n",
3088 dump_space_info(sinfo, num_bytes);
3095 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3097 struct btrfs_block_group_cache *cache;
3099 cache = btrfs_lookup_block_group(root->fs_info, start);
3101 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
3104 btrfs_add_free_space(cache, start, len);
3105 update_reserved_extents(root, start, len, 0);
3109 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3110 struct btrfs_root *root,
3111 u64 num_bytes, u64 min_alloc_size,
3112 u64 empty_size, u64 hint_byte,
3113 u64 search_end, struct btrfs_key *ins,
3117 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3118 empty_size, hint_byte, search_end, ins,
3120 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3124 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3125 struct btrfs_root *root, u64 parent,
3126 u64 root_objectid, u64 ref_generation,
3127 u64 owner, struct btrfs_key *ins)
3133 u64 num_bytes = ins->offset;
3135 struct btrfs_fs_info *info = root->fs_info;
3136 struct btrfs_root *extent_root = info->extent_root;
3137 struct btrfs_extent_item *extent_item;
3138 struct btrfs_extent_ref *ref;
3139 struct btrfs_path *path;
3140 struct btrfs_key keys[2];
3143 parent = ins->objectid;
3145 /* block accounting for super block */
3146 spin_lock_irq(&info->delalloc_lock);
3147 super_used = btrfs_super_bytes_used(&info->super_copy);
3148 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3149 spin_unlock_irq(&info->delalloc_lock);
3151 /* block accounting for root item */
3152 root_used = btrfs_root_used(&root->root_item);
3153 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3155 if (root == extent_root) {
3156 struct pending_extent_op *extent_op;
3158 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3161 extent_op->type = PENDING_EXTENT_INSERT;
3162 extent_op->bytenr = ins->objectid;
3163 extent_op->num_bytes = ins->offset;
3164 extent_op->parent = parent;
3165 extent_op->orig_parent = 0;
3166 extent_op->generation = ref_generation;
3167 extent_op->orig_generation = 0;
3168 extent_op->level = (int)owner;
3169 INIT_LIST_HEAD(&extent_op->list);
3172 mutex_lock(&root->fs_info->extent_ins_mutex);
3173 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3174 ins->objectid + ins->offset - 1,
3175 EXTENT_WRITEBACK, GFP_NOFS);
3176 set_state_private(&root->fs_info->extent_ins,
3177 ins->objectid, (unsigned long)extent_op);
3178 mutex_unlock(&root->fs_info->extent_ins_mutex);
3182 memcpy(&keys[0], ins, sizeof(*ins));
3183 keys[1].objectid = ins->objectid;
3184 keys[1].type = BTRFS_EXTENT_REF_KEY;
3185 keys[1].offset = parent;
3186 sizes[0] = sizeof(*extent_item);
3187 sizes[1] = sizeof(*ref);
3189 path = btrfs_alloc_path();
3192 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3196 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3197 struct btrfs_extent_item);
3198 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3199 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3200 struct btrfs_extent_ref);
3202 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3203 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3204 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3205 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3207 btrfs_mark_buffer_dirty(path->nodes[0]);
3209 trans->alloc_exclude_start = 0;
3210 trans->alloc_exclude_nr = 0;
3211 btrfs_free_path(path);
3212 finish_current_insert(trans, extent_root, 0);
3213 pending_ret = del_pending_extents(trans, extent_root, 0);
3223 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
3225 printk("update block group failed for %Lu %Lu\n",
3226 ins->objectid, ins->offset);
3233 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3234 struct btrfs_root *root, u64 parent,
3235 u64 root_objectid, u64 ref_generation,
3236 u64 owner, struct btrfs_key *ins)
3240 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3242 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3243 ref_generation, owner, ins);
3244 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3249 * this is used by the tree logging recovery code. It records that
3250 * an extent has been allocated and makes sure to clear the free
3251 * space cache bits as well
3253 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3254 struct btrfs_root *root, u64 parent,
3255 u64 root_objectid, u64 ref_generation,
3256 u64 owner, struct btrfs_key *ins)
3259 struct btrfs_block_group_cache *block_group;
3261 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3262 mutex_lock(&block_group->alloc_mutex);
3263 cache_block_group(root, block_group);
3265 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3267 mutex_unlock(&block_group->alloc_mutex);
3269 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3270 ref_generation, owner, ins);
3275 * finds a free extent and does all the dirty work required for allocation
3276 * returns the key for the extent through ins, and a tree buffer for
3277 * the first block of the extent through buf.
3279 * returns 0 if everything worked, non-zero otherwise.
3281 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3282 struct btrfs_root *root,
3283 u64 num_bytes, u64 parent, u64 min_alloc_size,
3284 u64 root_objectid, u64 ref_generation,
3285 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3286 u64 search_end, struct btrfs_key *ins, u64 data)
3290 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3291 min_alloc_size, empty_size, hint_byte,
3292 search_end, ins, data);
3294 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3295 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3296 root_objectid, ref_generation,
3297 owner_objectid, ins);
3301 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3306 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3307 struct btrfs_root *root,
3308 u64 bytenr, u32 blocksize)
3310 struct extent_buffer *buf;
3312 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3314 return ERR_PTR(-ENOMEM);
3315 btrfs_set_header_generation(buf, trans->transid);
3316 btrfs_tree_lock(buf);
3317 clean_tree_block(trans, root, buf);
3318 btrfs_set_buffer_uptodate(buf);
3319 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3320 set_extent_dirty(&root->dirty_log_pages, buf->start,
3321 buf->start + buf->len - 1, GFP_NOFS);
3323 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3324 buf->start + buf->len - 1, GFP_NOFS);
3326 trans->blocks_used++;
3331 * helper function to allocate a block for a given tree
3332 * returns the tree buffer or NULL.
3334 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3335 struct btrfs_root *root,
3336 u32 blocksize, u64 parent,
3343 struct btrfs_key ins;
3345 struct extent_buffer *buf;
3347 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3348 root_objectid, ref_generation, level,
3349 empty_size, hint, (u64)-1, &ins, 0);
3352 return ERR_PTR(ret);
3355 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3359 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3360 struct btrfs_root *root, struct extent_buffer *leaf)
3363 u64 leaf_generation;
3364 struct btrfs_key key;
3365 struct btrfs_file_extent_item *fi;
3370 BUG_ON(!btrfs_is_leaf(leaf));
3371 nritems = btrfs_header_nritems(leaf);
3372 leaf_owner = btrfs_header_owner(leaf);
3373 leaf_generation = btrfs_header_generation(leaf);
3375 for (i = 0; i < nritems; i++) {
3379 btrfs_item_key_to_cpu(leaf, &key, i);
3380 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3382 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3383 if (btrfs_file_extent_type(leaf, fi) ==
3384 BTRFS_FILE_EXTENT_INLINE)
3387 * FIXME make sure to insert a trans record that
3388 * repeats the snapshot del on crash
3390 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3391 if (disk_bytenr == 0)
3394 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3395 btrfs_file_extent_disk_num_bytes(leaf, fi),
3396 leaf->start, leaf_owner, leaf_generation,
3400 atomic_inc(&root->fs_info->throttle_gen);
3401 wake_up(&root->fs_info->transaction_throttle);
3407 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3408 struct btrfs_root *root,
3409 struct btrfs_leaf_ref *ref)
3413 struct btrfs_extent_info *info = ref->extents;
3415 for (i = 0; i < ref->nritems; i++) {
3416 ret = __btrfs_free_extent(trans, root, info->bytenr,
3417 info->num_bytes, ref->bytenr,
3418 ref->owner, ref->generation,
3421 atomic_inc(&root->fs_info->throttle_gen);
3422 wake_up(&root->fs_info->transaction_throttle);
3432 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3437 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3440 #if 0 // some debugging code in case we see problems here
3441 /* if the refs count is one, it won't get increased again. But
3442 * if the ref count is > 1, someone may be decreasing it at
3443 * the same time we are.
3446 struct extent_buffer *eb = NULL;
3447 eb = btrfs_find_create_tree_block(root, start, len);
3449 btrfs_tree_lock(eb);
3451 mutex_lock(&root->fs_info->alloc_mutex);
3452 ret = lookup_extent_ref(NULL, root, start, len, refs);
3454 mutex_unlock(&root->fs_info->alloc_mutex);
3457 btrfs_tree_unlock(eb);
3458 free_extent_buffer(eb);
3461 printk("block %llu went down to one during drop_snap\n",
3462 (unsigned long long)start);
3473 * helper function for drop_snapshot, this walks down the tree dropping ref
3474 * counts as it goes.
3476 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3477 struct btrfs_root *root,
3478 struct btrfs_path *path, int *level)
3484 struct extent_buffer *next;
3485 struct extent_buffer *cur;
3486 struct extent_buffer *parent;
3487 struct btrfs_leaf_ref *ref;
3492 WARN_ON(*level < 0);
3493 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3494 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3495 path->nodes[*level]->len, &refs);
3501 * walk down to the last node level and free all the leaves
3503 while(*level >= 0) {
3504 WARN_ON(*level < 0);
3505 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3506 cur = path->nodes[*level];
3508 if (btrfs_header_level(cur) != *level)
3511 if (path->slots[*level] >=
3512 btrfs_header_nritems(cur))
3515 ret = btrfs_drop_leaf_ref(trans, root, cur);
3519 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3520 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3521 blocksize = btrfs_level_size(root, *level - 1);
3523 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3526 parent = path->nodes[*level];
3527 root_owner = btrfs_header_owner(parent);
3528 root_gen = btrfs_header_generation(parent);
3529 path->slots[*level]++;
3531 ret = __btrfs_free_extent(trans, root, bytenr,
3532 blocksize, parent->start,
3533 root_owner, root_gen,
3537 atomic_inc(&root->fs_info->throttle_gen);
3538 wake_up(&root->fs_info->transaction_throttle);
3544 * at this point, we have a single ref, and since the
3545 * only place referencing this extent is a dead root
3546 * the reference count should never go higher.
3547 * So, we don't need to check it again
3550 ref = btrfs_lookup_leaf_ref(root, bytenr);
3551 if (ref && ref->generation != ptr_gen) {
3552 btrfs_free_leaf_ref(root, ref);
3556 ret = cache_drop_leaf_ref(trans, root, ref);
3558 btrfs_remove_leaf_ref(root, ref);
3559 btrfs_free_leaf_ref(root, ref);
3563 if (printk_ratelimit()) {
3564 printk("leaf ref miss for bytenr %llu\n",
3565 (unsigned long long)bytenr);
3568 next = btrfs_find_tree_block(root, bytenr, blocksize);
3569 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3570 free_extent_buffer(next);
3572 next = read_tree_block(root, bytenr, blocksize,
3577 * this is a debugging check and can go away
3578 * the ref should never go all the way down to 1
3581 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3587 WARN_ON(*level <= 0);
3588 if (path->nodes[*level-1])
3589 free_extent_buffer(path->nodes[*level-1]);
3590 path->nodes[*level-1] = next;
3591 *level = btrfs_header_level(next);
3592 path->slots[*level] = 0;
3596 WARN_ON(*level < 0);
3597 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3599 if (path->nodes[*level] == root->node) {
3600 parent = path->nodes[*level];
3601 bytenr = path->nodes[*level]->start;
3603 parent = path->nodes[*level + 1];
3604 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3607 blocksize = btrfs_level_size(root, *level);
3608 root_owner = btrfs_header_owner(parent);
3609 root_gen = btrfs_header_generation(parent);
3611 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3612 parent->start, root_owner, root_gen,
3614 free_extent_buffer(path->nodes[*level]);
3615 path->nodes[*level] = NULL;
3624 * helper function for drop_subtree, this function is similar to
3625 * walk_down_tree. The main difference is that it checks reference
3626 * counts while tree blocks are locked.
3628 static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3629 struct btrfs_root *root,
3630 struct btrfs_path *path, int *level)
3632 struct extent_buffer *next;
3633 struct extent_buffer *cur;
3634 struct extent_buffer *parent;
3641 cur = path->nodes[*level];
3642 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3648 while (*level >= 0) {
3649 cur = path->nodes[*level];
3651 ret = btrfs_drop_leaf_ref(trans, root, cur);
3653 clean_tree_block(trans, root, cur);
3656 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3657 clean_tree_block(trans, root, cur);
3661 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3662 blocksize = btrfs_level_size(root, *level - 1);
3663 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3665 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3666 btrfs_tree_lock(next);
3668 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3672 parent = path->nodes[*level];
3673 ret = btrfs_free_extent(trans, root, bytenr,
3674 blocksize, parent->start,
3675 btrfs_header_owner(parent),
3676 btrfs_header_generation(parent),
3679 path->slots[*level]++;
3680 btrfs_tree_unlock(next);
3681 free_extent_buffer(next);
3685 *level = btrfs_header_level(next);
3686 path->nodes[*level] = next;
3687 path->slots[*level] = 0;
3688 path->locks[*level] = 1;
3692 parent = path->nodes[*level + 1];
3693 bytenr = path->nodes[*level]->start;
3694 blocksize = path->nodes[*level]->len;
3696 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3697 parent->start, btrfs_header_owner(parent),
3698 btrfs_header_generation(parent), *level, 1);
3701 if (path->locks[*level]) {
3702 btrfs_tree_unlock(path->nodes[*level]);
3703 path->locks[*level] = 0;
3705 free_extent_buffer(path->nodes[*level]);
3706 path->nodes[*level] = NULL;
3713 * helper for dropping snapshots. This walks back up the tree in the path
3714 * to find the first node higher up where we haven't yet gone through
3717 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3718 struct btrfs_root *root,
3719 struct btrfs_path *path,
3720 int *level, int max_level)
3724 struct btrfs_root_item *root_item = &root->root_item;
3729 for (i = *level; i < max_level && path->nodes[i]; i++) {
3730 slot = path->slots[i];
3731 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3732 struct extent_buffer *node;
3733 struct btrfs_disk_key disk_key;
3734 node = path->nodes[i];
3737 WARN_ON(*level == 0);
3738 btrfs_node_key(node, &disk_key, path->slots[i]);
3739 memcpy(&root_item->drop_progress,
3740 &disk_key, sizeof(disk_key));
3741 root_item->drop_level = i;
3744 struct extent_buffer *parent;
3745 if (path->nodes[*level] == root->node)
3746 parent = path->nodes[*level];
3748 parent = path->nodes[*level + 1];
3750 root_owner = btrfs_header_owner(parent);
3751 root_gen = btrfs_header_generation(parent);
3753 clean_tree_block(trans, root, path->nodes[*level]);
3754 ret = btrfs_free_extent(trans, root,
3755 path->nodes[*level]->start,
3756 path->nodes[*level]->len,
3757 parent->start, root_owner,
3758 root_gen, *level, 1);
3760 if (path->locks[*level]) {
3761 btrfs_tree_unlock(path->nodes[*level]);
3762 path->locks[*level] = 0;
3764 free_extent_buffer(path->nodes[*level]);
3765 path->nodes[*level] = NULL;
3773 * drop the reference count on the tree rooted at 'snap'. This traverses
3774 * the tree freeing any blocks that have a ref count of zero after being
3777 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3783 struct btrfs_path *path;
3786 struct btrfs_root_item *root_item = &root->root_item;
3788 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3789 path = btrfs_alloc_path();
3792 level = btrfs_header_level(root->node);
3794 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3795 path->nodes[level] = root->node;
3796 extent_buffer_get(root->node);
3797 path->slots[level] = 0;
3799 struct btrfs_key key;
3800 struct btrfs_disk_key found_key;
3801 struct extent_buffer *node;
3803 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3804 level = root_item->drop_level;
3805 path->lowest_level = level;
3806 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3811 node = path->nodes[level];
3812 btrfs_node_key(node, &found_key, path->slots[level]);
3813 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3814 sizeof(found_key)));
3816 * unlock our path, this is safe because only this
3817 * function is allowed to delete this snapshot
3819 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3820 if (path->nodes[i] && path->locks[i]) {
3822 btrfs_tree_unlock(path->nodes[i]);
3827 wret = walk_down_tree(trans, root, path, &level);
3833 wret = walk_up_tree(trans, root, path, &level,
3839 if (trans->transaction->in_commit) {
3843 atomic_inc(&root->fs_info->throttle_gen);
3844 wake_up(&root->fs_info->transaction_throttle);
3846 for (i = 0; i <= orig_level; i++) {
3847 if (path->nodes[i]) {
3848 free_extent_buffer(path->nodes[i]);
3849 path->nodes[i] = NULL;
3853 btrfs_free_path(path);
3857 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3858 struct btrfs_root *root,
3859 struct extent_buffer *node,
3860 struct extent_buffer *parent)
3862 struct btrfs_path *path;
3868 path = btrfs_alloc_path();
3871 BUG_ON(!btrfs_tree_locked(parent));
3872 parent_level = btrfs_header_level(parent);
3873 extent_buffer_get(parent);
3874 path->nodes[parent_level] = parent;
3875 path->slots[parent_level] = btrfs_header_nritems(parent);
3877 BUG_ON(!btrfs_tree_locked(node));
3878 level = btrfs_header_level(node);
3879 extent_buffer_get(node);
3880 path->nodes[level] = node;
3881 path->slots[level] = 0;
3884 wret = walk_down_subtree(trans, root, path, &level);
3890 wret = walk_up_tree(trans, root, path, &level, parent_level);
3897 btrfs_free_path(path);
3901 static unsigned long calc_ra(unsigned long start, unsigned long last,
3904 return min(last, start + nr - 1);
3907 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3912 unsigned long first_index;
3913 unsigned long last_index;
3916 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3917 struct file_ra_state *ra;
3918 struct btrfs_ordered_extent *ordered;
3919 unsigned int total_read = 0;
3920 unsigned int total_dirty = 0;
3923 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3925 mutex_lock(&inode->i_mutex);
3926 first_index = start >> PAGE_CACHE_SHIFT;
3927 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3929 /* make sure the dirty trick played by the caller work */
3930 ret = invalidate_inode_pages2_range(inode->i_mapping,
3931 first_index, last_index);
3935 file_ra_state_init(ra, inode->i_mapping);
3937 for (i = first_index ; i <= last_index; i++) {
3938 if (total_read % ra->ra_pages == 0) {
3939 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3940 calc_ra(i, last_index, ra->ra_pages));
3944 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3946 page = grab_cache_page(inode->i_mapping, i);
3951 if (!PageUptodate(page)) {
3952 btrfs_readpage(NULL, page);
3954 if (!PageUptodate(page)) {
3956 page_cache_release(page);
3961 wait_on_page_writeback(page);
3963 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3964 page_end = page_start + PAGE_CACHE_SIZE - 1;
3965 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3967 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3969 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3971 page_cache_release(page);
3972 btrfs_start_ordered_extent(inode, ordered, 1);
3973 btrfs_put_ordered_extent(ordered);
3976 set_page_extent_mapped(page);
3978 btrfs_set_extent_delalloc(inode, page_start, page_end);
3979 if (i == first_index)
3980 set_extent_bits(io_tree, page_start, page_end,
3981 EXTENT_BOUNDARY, GFP_NOFS);
3983 set_page_dirty(page);
3986 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3988 page_cache_release(page);
3993 mutex_unlock(&inode->i_mutex);
3994 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3998 static int noinline relocate_data_extent(struct inode *reloc_inode,
3999 struct btrfs_key *extent_key,
4002 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4003 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4004 struct extent_map *em;
4005 u64 start = extent_key->objectid - offset;
4006 u64 end = start + extent_key->offset - 1;
4008 em = alloc_extent_map(GFP_NOFS);
4009 BUG_ON(!em || IS_ERR(em));
4012 em->len = extent_key->offset;
4013 em->block_len = extent_key->offset;
4014 em->block_start = extent_key->objectid;
4015 em->bdev = root->fs_info->fs_devices->latest_bdev;
4016 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4018 /* setup extent map to cheat btrfs_readpage */
4019 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4022 spin_lock(&em_tree->lock);
4023 ret = add_extent_mapping(em_tree, em);
4024 spin_unlock(&em_tree->lock);
4025 if (ret != -EEXIST) {
4026 free_extent_map(em);
4029 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4031 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4033 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4036 struct btrfs_ref_path {
4038 u64 nodes[BTRFS_MAX_LEVEL];
4040 u64 root_generation;
4047 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4048 u64 new_nodes[BTRFS_MAX_LEVEL];
4051 struct disk_extent {
4062 static int is_cowonly_root(u64 root_objectid)
4064 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4065 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4066 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4067 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4068 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4073 static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4074 struct btrfs_root *extent_root,
4075 struct btrfs_ref_path *ref_path,
4078 struct extent_buffer *leaf;
4079 struct btrfs_path *path;
4080 struct btrfs_extent_ref *ref;
4081 struct btrfs_key key;
4082 struct btrfs_key found_key;
4088 path = btrfs_alloc_path();
4093 ref_path->lowest_level = -1;
4094 ref_path->current_level = -1;
4095 ref_path->shared_level = -1;
4099 level = ref_path->current_level - 1;
4100 while (level >= -1) {
4102 if (level < ref_path->lowest_level)
4106 bytenr = ref_path->nodes[level];
4108 bytenr = ref_path->extent_start;
4110 BUG_ON(bytenr == 0);
4112 parent = ref_path->nodes[level + 1];
4113 ref_path->nodes[level + 1] = 0;
4114 ref_path->current_level = level;
4115 BUG_ON(parent == 0);
4117 key.objectid = bytenr;
4118 key.offset = parent + 1;
4119 key.type = BTRFS_EXTENT_REF_KEY;
4121 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4126 leaf = path->nodes[0];
4127 nritems = btrfs_header_nritems(leaf);
4128 if (path->slots[0] >= nritems) {
4129 ret = btrfs_next_leaf(extent_root, path);
4134 leaf = path->nodes[0];
4137 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4138 if (found_key.objectid == bytenr &&
4139 found_key.type == BTRFS_EXTENT_REF_KEY) {
4140 if (level < ref_path->shared_level)
4141 ref_path->shared_level = level;
4146 btrfs_release_path(extent_root, path);
4149 /* reached lowest level */
4153 level = ref_path->current_level;
4154 while (level < BTRFS_MAX_LEVEL - 1) {
4157 bytenr = ref_path->nodes[level];
4159 bytenr = ref_path->extent_start;
4161 BUG_ON(bytenr == 0);
4163 key.objectid = bytenr;
4165 key.type = BTRFS_EXTENT_REF_KEY;
4167 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4171 leaf = path->nodes[0];
4172 nritems = btrfs_header_nritems(leaf);
4173 if (path->slots[0] >= nritems) {
4174 ret = btrfs_next_leaf(extent_root, path);
4178 /* the extent was freed by someone */
4179 if (ref_path->lowest_level == level)
4181 btrfs_release_path(extent_root, path);
4184 leaf = path->nodes[0];
4187 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4188 if (found_key.objectid != bytenr ||
4189 found_key.type != BTRFS_EXTENT_REF_KEY) {
4190 /* the extent was freed by someone */
4191 if (ref_path->lowest_level == level) {
4195 btrfs_release_path(extent_root, path);
4199 ref = btrfs_item_ptr(leaf, path->slots[0],
4200 struct btrfs_extent_ref);
4201 ref_objectid = btrfs_ref_objectid(leaf, ref);
4202 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4204 level = (int)ref_objectid;
4205 BUG_ON(level >= BTRFS_MAX_LEVEL);
4206 ref_path->lowest_level = level;
4207 ref_path->current_level = level;
4208 ref_path->nodes[level] = bytenr;
4210 WARN_ON(ref_objectid != level);
4213 WARN_ON(level != -1);
4217 if (ref_path->lowest_level == level) {
4218 ref_path->owner_objectid = ref_objectid;
4219 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4223 * the block is tree root or the block isn't in reference
4226 if (found_key.objectid == found_key.offset ||
4227 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4228 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4229 ref_path->root_generation =
4230 btrfs_ref_generation(leaf, ref);
4232 /* special reference from the tree log */
4233 ref_path->nodes[0] = found_key.offset;
4234 ref_path->current_level = 0;
4241 BUG_ON(ref_path->nodes[level] != 0);
4242 ref_path->nodes[level] = found_key.offset;
4243 ref_path->current_level = level;
4246 * the reference was created in the running transaction,
4247 * no need to continue walking up.
4249 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4250 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4251 ref_path->root_generation =
4252 btrfs_ref_generation(leaf, ref);
4257 btrfs_release_path(extent_root, path);
4260 /* reached max tree level, but no tree root found. */
4263 btrfs_free_path(path);
4267 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4268 struct btrfs_root *extent_root,
4269 struct btrfs_ref_path *ref_path,
4272 memset(ref_path, 0, sizeof(*ref_path));
4273 ref_path->extent_start = extent_start;
4275 return __next_ref_path(trans, extent_root, ref_path, 1);
4278 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4279 struct btrfs_root *extent_root,
4280 struct btrfs_ref_path *ref_path)
4282 return __next_ref_path(trans, extent_root, ref_path, 0);
4285 static int noinline get_new_locations(struct inode *reloc_inode,
4286 struct btrfs_key *extent_key,
4287 u64 offset, int no_fragment,
4288 struct disk_extent **extents,
4291 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4292 struct btrfs_path *path;
4293 struct btrfs_file_extent_item *fi;
4294 struct extent_buffer *leaf;
4295 struct disk_extent *exts = *extents;
4296 struct btrfs_key found_key;
4301 int max = *nr_extents;
4304 WARN_ON(!no_fragment && *extents);
4307 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4312 path = btrfs_alloc_path();
4315 cur_pos = extent_key->objectid - offset;
4316 last_byte = extent_key->objectid + extent_key->offset;
4317 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4327 leaf = path->nodes[0];
4328 nritems = btrfs_header_nritems(leaf);
4329 if (path->slots[0] >= nritems) {
4330 ret = btrfs_next_leaf(root, path);
4335 leaf = path->nodes[0];
4338 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4339 if (found_key.offset != cur_pos ||
4340 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4341 found_key.objectid != reloc_inode->i_ino)
4344 fi = btrfs_item_ptr(leaf, path->slots[0],
4345 struct btrfs_file_extent_item);
4346 if (btrfs_file_extent_type(leaf, fi) !=
4347 BTRFS_FILE_EXTENT_REG ||
4348 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4352 struct disk_extent *old = exts;
4354 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4355 memcpy(exts, old, sizeof(*exts) * nr);
4356 if (old != *extents)
4360 exts[nr].disk_bytenr =
4361 btrfs_file_extent_disk_bytenr(leaf, fi);
4362 exts[nr].disk_num_bytes =
4363 btrfs_file_extent_disk_num_bytes(leaf, fi);
4364 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4365 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4366 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4367 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4368 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4369 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4371 BUG_ON(exts[nr].offset > 0);
4372 BUG_ON(exts[nr].compression || exts[nr].encryption);
4373 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4375 cur_pos += exts[nr].num_bytes;
4378 if (cur_pos + offset >= last_byte)
4388 WARN_ON(cur_pos + offset > last_byte);
4389 if (cur_pos + offset < last_byte) {
4395 btrfs_free_path(path);
4397 if (exts != *extents)
4406 static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4407 struct btrfs_root *root,
4408 struct btrfs_path *path,
4409 struct btrfs_key *extent_key,
4410 struct btrfs_key *leaf_key,
4411 struct btrfs_ref_path *ref_path,
4412 struct disk_extent *new_extents,
4415 struct extent_buffer *leaf;
4416 struct btrfs_file_extent_item *fi;
4417 struct inode *inode = NULL;
4418 struct btrfs_key key;
4426 int extent_locked = 0;
4430 memcpy(&key, leaf_key, sizeof(key));
4431 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4432 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4433 if (key.objectid < ref_path->owner_objectid ||
4434 (key.objectid == ref_path->owner_objectid &&
4435 key.type < BTRFS_EXTENT_DATA_KEY)) {
4436 key.objectid = ref_path->owner_objectid;
4437 key.type = BTRFS_EXTENT_DATA_KEY;
4443 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4447 leaf = path->nodes[0];
4448 nritems = btrfs_header_nritems(leaf);
4450 if (extent_locked && ret > 0) {
4452 * the file extent item was modified by someone
4453 * before the extent got locked.
4455 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4456 lock_end, GFP_NOFS);
4460 if (path->slots[0] >= nritems) {
4461 if (++nr_scaned > 2)
4464 BUG_ON(extent_locked);
4465 ret = btrfs_next_leaf(root, path);
4470 leaf = path->nodes[0];
4471 nritems = btrfs_header_nritems(leaf);
4474 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4476 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4477 if ((key.objectid > ref_path->owner_objectid) ||
4478 (key.objectid == ref_path->owner_objectid &&
4479 key.type > BTRFS_EXTENT_DATA_KEY) ||
4480 (key.offset >= first_pos + extent_key->offset))
4484 if (inode && key.objectid != inode->i_ino) {
4485 BUG_ON(extent_locked);
4486 btrfs_release_path(root, path);
4487 mutex_unlock(&inode->i_mutex);
4493 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4498 fi = btrfs_item_ptr(leaf, path->slots[0],
4499 struct btrfs_file_extent_item);
4500 extent_type = btrfs_file_extent_type(leaf, fi);
4501 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4502 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4503 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4504 extent_key->objectid)) {
4510 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4511 ext_offset = btrfs_file_extent_offset(leaf, fi);
4513 if (first_pos > key.offset - ext_offset)
4514 first_pos = key.offset - ext_offset;
4516 if (!extent_locked) {
4517 lock_start = key.offset;
4518 lock_end = lock_start + num_bytes - 1;
4520 if (lock_start > key.offset ||
4521 lock_end + 1 < key.offset + num_bytes) {
4522 unlock_extent(&BTRFS_I(inode)->io_tree,
4523 lock_start, lock_end, GFP_NOFS);
4529 btrfs_release_path(root, path);
4531 inode = btrfs_iget_locked(root->fs_info->sb,
4532 key.objectid, root);
4533 if (inode->i_state & I_NEW) {
4534 BTRFS_I(inode)->root = root;
4535 BTRFS_I(inode)->location.objectid =
4537 BTRFS_I(inode)->location.type =
4538 BTRFS_INODE_ITEM_KEY;
4539 BTRFS_I(inode)->location.offset = 0;
4540 btrfs_read_locked_inode(inode);
4541 unlock_new_inode(inode);
4544 * some code call btrfs_commit_transaction while
4545 * holding the i_mutex, so we can't use mutex_lock
4548 if (is_bad_inode(inode) ||
4549 !mutex_trylock(&inode->i_mutex)) {
4552 key.offset = (u64)-1;
4557 if (!extent_locked) {
4558 struct btrfs_ordered_extent *ordered;
4560 btrfs_release_path(root, path);
4562 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4563 lock_end, GFP_NOFS);
4564 ordered = btrfs_lookup_first_ordered_extent(inode,
4567 ordered->file_offset <= lock_end &&
4568 ordered->file_offset + ordered->len > lock_start) {
4569 unlock_extent(&BTRFS_I(inode)->io_tree,
4570 lock_start, lock_end, GFP_NOFS);
4571 btrfs_start_ordered_extent(inode, ordered, 1);
4572 btrfs_put_ordered_extent(ordered);
4573 key.offset += num_bytes;
4577 btrfs_put_ordered_extent(ordered);
4583 if (nr_extents == 1) {
4584 /* update extent pointer in place */
4585 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4586 new_extents[0].disk_bytenr);
4587 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4588 new_extents[0].disk_num_bytes);
4589 btrfs_mark_buffer_dirty(leaf);
4591 btrfs_drop_extent_cache(inode, key.offset,
4592 key.offset + num_bytes - 1, 0);
4594 ret = btrfs_inc_extent_ref(trans, root,
4595 new_extents[0].disk_bytenr,
4596 new_extents[0].disk_num_bytes,
4598 root->root_key.objectid,
4603 ret = btrfs_free_extent(trans, root,
4604 extent_key->objectid,
4607 btrfs_header_owner(leaf),
4608 btrfs_header_generation(leaf),
4612 btrfs_release_path(root, path);
4613 key.offset += num_bytes;
4621 * drop old extent pointer at first, then insert the
4622 * new pointers one bye one
4624 btrfs_release_path(root, path);
4625 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4626 key.offset + num_bytes,
4627 key.offset, &alloc_hint);
4630 for (i = 0; i < nr_extents; i++) {
4631 if (ext_offset >= new_extents[i].num_bytes) {
4632 ext_offset -= new_extents[i].num_bytes;
4635 extent_len = min(new_extents[i].num_bytes -
4636 ext_offset, num_bytes);
4638 ret = btrfs_insert_empty_item(trans, root,
4643 leaf = path->nodes[0];
4644 fi = btrfs_item_ptr(leaf, path->slots[0],
4645 struct btrfs_file_extent_item);
4646 btrfs_set_file_extent_generation(leaf, fi,
4648 btrfs_set_file_extent_type(leaf, fi,
4649 BTRFS_FILE_EXTENT_REG);
4650 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4651 new_extents[i].disk_bytenr);
4652 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4653 new_extents[i].disk_num_bytes);
4654 btrfs_set_file_extent_ram_bytes(leaf, fi,
4655 new_extents[i].ram_bytes);
4657 btrfs_set_file_extent_compression(leaf, fi,
4658 new_extents[i].compression);
4659 btrfs_set_file_extent_encryption(leaf, fi,
4660 new_extents[i].encryption);
4661 btrfs_set_file_extent_other_encoding(leaf, fi,
4662 new_extents[i].other_encoding);
4664 btrfs_set_file_extent_num_bytes(leaf, fi,
4666 ext_offset += new_extents[i].offset;
4667 btrfs_set_file_extent_offset(leaf, fi,
4669 btrfs_mark_buffer_dirty(leaf);
4671 btrfs_drop_extent_cache(inode, key.offset,
4672 key.offset + extent_len - 1, 0);
4674 ret = btrfs_inc_extent_ref(trans, root,
4675 new_extents[i].disk_bytenr,
4676 new_extents[i].disk_num_bytes,
4678 root->root_key.objectid,
4679 trans->transid, key.objectid);
4681 btrfs_release_path(root, path);
4683 inode_add_bytes(inode, extent_len);
4686 num_bytes -= extent_len;
4687 key.offset += extent_len;
4692 BUG_ON(i >= nr_extents);
4696 if (extent_locked) {
4697 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4698 lock_end, GFP_NOFS);
4702 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4703 key.offset >= first_pos + extent_key->offset)
4710 btrfs_release_path(root, path);
4712 mutex_unlock(&inode->i_mutex);
4713 if (extent_locked) {
4714 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4715 lock_end, GFP_NOFS);
4722 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4723 struct btrfs_root *root,
4724 struct extent_buffer *buf, u64 orig_start)
4729 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4730 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4732 level = btrfs_header_level(buf);
4734 struct btrfs_leaf_ref *ref;
4735 struct btrfs_leaf_ref *orig_ref;
4737 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4741 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4743 btrfs_free_leaf_ref(root, orig_ref);
4747 ref->nritems = orig_ref->nritems;
4748 memcpy(ref->extents, orig_ref->extents,
4749 sizeof(ref->extents[0]) * ref->nritems);
4751 btrfs_free_leaf_ref(root, orig_ref);
4753 ref->root_gen = trans->transid;
4754 ref->bytenr = buf->start;
4755 ref->owner = btrfs_header_owner(buf);
4756 ref->generation = btrfs_header_generation(buf);
4757 ret = btrfs_add_leaf_ref(root, ref, 0);
4759 btrfs_free_leaf_ref(root, ref);
4764 static int noinline invalidate_extent_cache(struct btrfs_root *root,
4765 struct extent_buffer *leaf,
4766 struct btrfs_block_group_cache *group,
4767 struct btrfs_root *target_root)
4769 struct btrfs_key key;
4770 struct inode *inode = NULL;
4771 struct btrfs_file_extent_item *fi;
4773 u64 skip_objectid = 0;
4777 nritems = btrfs_header_nritems(leaf);
4778 for (i = 0; i < nritems; i++) {
4779 btrfs_item_key_to_cpu(leaf, &key, i);
4780 if (key.objectid == skip_objectid ||
4781 key.type != BTRFS_EXTENT_DATA_KEY)
4783 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4784 if (btrfs_file_extent_type(leaf, fi) ==
4785 BTRFS_FILE_EXTENT_INLINE)
4787 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4789 if (!inode || inode->i_ino != key.objectid) {
4791 inode = btrfs_ilookup(target_root->fs_info->sb,
4792 key.objectid, target_root, 1);
4795 skip_objectid = key.objectid;
4798 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4800 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4801 key.offset + num_bytes - 1, GFP_NOFS);
4802 btrfs_drop_extent_cache(inode, key.offset,
4803 key.offset + num_bytes - 1, 1);
4804 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4805 key.offset + num_bytes - 1, GFP_NOFS);
4812 static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4813 struct btrfs_root *root,
4814 struct extent_buffer *leaf,
4815 struct btrfs_block_group_cache *group,
4816 struct inode *reloc_inode)
4818 struct btrfs_key key;
4819 struct btrfs_key extent_key;
4820 struct btrfs_file_extent_item *fi;
4821 struct btrfs_leaf_ref *ref;
4822 struct disk_extent *new_extent;
4831 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4832 BUG_ON(!new_extent);
4834 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4838 nritems = btrfs_header_nritems(leaf);
4839 for (i = 0; i < nritems; i++) {
4840 btrfs_item_key_to_cpu(leaf, &key, i);
4841 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4843 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4844 if (btrfs_file_extent_type(leaf, fi) ==
4845 BTRFS_FILE_EXTENT_INLINE)
4847 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4848 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4853 if (bytenr >= group->key.objectid + group->key.offset ||
4854 bytenr + num_bytes <= group->key.objectid)
4857 extent_key.objectid = bytenr;
4858 extent_key.offset = num_bytes;
4859 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4861 ret = get_new_locations(reloc_inode, &extent_key,
4862 group->key.objectid, 1,
4863 &new_extent, &nr_extent);
4868 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4869 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4870 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4871 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4873 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4874 new_extent->disk_bytenr);
4875 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4876 new_extent->disk_num_bytes);
4877 btrfs_mark_buffer_dirty(leaf);
4879 ret = btrfs_inc_extent_ref(trans, root,
4880 new_extent->disk_bytenr,
4881 new_extent->disk_num_bytes,
4883 root->root_key.objectid,
4884 trans->transid, key.objectid);
4886 ret = btrfs_free_extent(trans, root,
4887 bytenr, num_bytes, leaf->start,
4888 btrfs_header_owner(leaf),
4889 btrfs_header_generation(leaf),
4895 BUG_ON(ext_index + 1 != ref->nritems);
4896 btrfs_free_leaf_ref(root, ref);
4900 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4901 struct btrfs_root *root)
4903 struct btrfs_root *reloc_root;
4906 if (root->reloc_root) {
4907 reloc_root = root->reloc_root;
4908 root->reloc_root = NULL;
4909 list_add(&reloc_root->dead_list,
4910 &root->fs_info->dead_reloc_roots);
4912 btrfs_set_root_bytenr(&reloc_root->root_item,
4913 reloc_root->node->start);
4914 btrfs_set_root_level(&root->root_item,
4915 btrfs_header_level(reloc_root->node));
4916 memset(&reloc_root->root_item.drop_progress, 0,
4917 sizeof(struct btrfs_disk_key));
4918 reloc_root->root_item.drop_level = 0;
4920 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4921 &reloc_root->root_key,
4922 &reloc_root->root_item);
4928 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4930 struct btrfs_trans_handle *trans;
4931 struct btrfs_root *reloc_root;
4932 struct btrfs_root *prev_root = NULL;
4933 struct list_head dead_roots;
4937 INIT_LIST_HEAD(&dead_roots);
4938 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4940 while (!list_empty(&dead_roots)) {
4941 reloc_root = list_entry(dead_roots.prev,
4942 struct btrfs_root, dead_list);
4943 list_del_init(&reloc_root->dead_list);
4945 BUG_ON(reloc_root->commit_root != NULL);
4947 trans = btrfs_join_transaction(root, 1);
4950 mutex_lock(&root->fs_info->drop_mutex);
4951 ret = btrfs_drop_snapshot(trans, reloc_root);
4954 mutex_unlock(&root->fs_info->drop_mutex);
4956 nr = trans->blocks_used;
4957 ret = btrfs_end_transaction(trans, root);
4959 btrfs_btree_balance_dirty(root, nr);
4962 free_extent_buffer(reloc_root->node);
4964 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4965 &reloc_root->root_key);
4967 mutex_unlock(&root->fs_info->drop_mutex);
4969 nr = trans->blocks_used;
4970 ret = btrfs_end_transaction(trans, root);
4972 btrfs_btree_balance_dirty(root, nr);
4975 prev_root = reloc_root;
4978 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4984 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4986 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4990 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4992 struct btrfs_root *reloc_root;
4993 struct btrfs_trans_handle *trans;
4994 struct btrfs_key location;
4998 mutex_lock(&root->fs_info->tree_reloc_mutex);
4999 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5001 found = !list_empty(&root->fs_info->dead_reloc_roots);
5002 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5005 trans = btrfs_start_transaction(root, 1);
5007 ret = btrfs_commit_transaction(trans, root);
5011 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5012 location.offset = (u64)-1;
5013 location.type = BTRFS_ROOT_ITEM_KEY;
5015 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5016 BUG_ON(!reloc_root);
5017 btrfs_orphan_cleanup(reloc_root);
5021 static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5022 struct btrfs_root *root)
5024 struct btrfs_root *reloc_root;
5025 struct extent_buffer *eb;
5026 struct btrfs_root_item *root_item;
5027 struct btrfs_key root_key;
5030 BUG_ON(!root->ref_cows);
5031 if (root->reloc_root)
5034 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5037 ret = btrfs_copy_root(trans, root, root->commit_root,
5038 &eb, BTRFS_TREE_RELOC_OBJECTID);
5041 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5042 root_key.offset = root->root_key.objectid;
5043 root_key.type = BTRFS_ROOT_ITEM_KEY;
5045 memcpy(root_item, &root->root_item, sizeof(root_item));
5046 btrfs_set_root_refs(root_item, 0);
5047 btrfs_set_root_bytenr(root_item, eb->start);
5048 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5049 btrfs_set_root_generation(root_item, trans->transid);
5051 btrfs_tree_unlock(eb);
5052 free_extent_buffer(eb);
5054 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5055 &root_key, root_item);
5059 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5061 BUG_ON(!reloc_root);
5062 reloc_root->last_trans = trans->transid;
5063 reloc_root->commit_root = NULL;
5064 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5066 root->reloc_root = reloc_root;
5071 * Core function of space balance.
5073 * The idea is using reloc trees to relocate tree blocks in reference
5074 * counted roots. There is one reloc tree for each subvol, and all
5075 * reloc trees share same root key objectid. Reloc trees are snapshots
5076 * of the latest committed roots of subvols (root->commit_root).
5078 * To relocate a tree block referenced by a subvol, there are two steps.
5079 * COW the block through subvol's reloc tree, then update block pointer
5080 * in the subvol to point to the new block. Since all reloc trees share
5081 * same root key objectid, doing special handing for tree blocks owned
5082 * by them is easy. Once a tree block has been COWed in one reloc tree,
5083 * we can use the resulting new block directly when the same block is
5084 * required to COW again through other reloc trees. By this way, relocated
5085 * tree blocks are shared between reloc trees, so they are also shared
5088 static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5089 struct btrfs_root *root,
5090 struct btrfs_path *path,
5091 struct btrfs_key *first_key,
5092 struct btrfs_ref_path *ref_path,
5093 struct btrfs_block_group_cache *group,
5094 struct inode *reloc_inode)
5096 struct btrfs_root *reloc_root;
5097 struct extent_buffer *eb = NULL;
5098 struct btrfs_key *keys;
5102 int lowest_level = 0;
5105 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5106 lowest_level = ref_path->owner_objectid;
5108 if (!root->ref_cows) {
5109 path->lowest_level = lowest_level;
5110 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5112 path->lowest_level = 0;
5113 btrfs_release_path(root, path);
5117 mutex_lock(&root->fs_info->tree_reloc_mutex);
5118 ret = init_reloc_tree(trans, root);
5120 reloc_root = root->reloc_root;
5122 shared_level = ref_path->shared_level;
5123 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5125 keys = ref_path->node_keys;
5126 nodes = ref_path->new_nodes;
5127 memset(&keys[shared_level + 1], 0,
5128 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5129 memset(&nodes[shared_level + 1], 0,
5130 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5132 if (nodes[lowest_level] == 0) {
5133 path->lowest_level = lowest_level;
5134 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5137 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5138 eb = path->nodes[level];
5139 if (!eb || eb == reloc_root->node)
5141 nodes[level] = eb->start;
5143 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5145 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5148 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5149 eb = path->nodes[0];
5150 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5151 group, reloc_inode);
5154 btrfs_release_path(reloc_root, path);
5156 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5162 * replace tree blocks in the fs tree with tree blocks in
5165 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5168 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5169 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5172 extent_buffer_get(path->nodes[0]);
5173 eb = path->nodes[0];
5174 btrfs_release_path(reloc_root, path);
5175 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5177 free_extent_buffer(eb);
5180 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5181 path->lowest_level = 0;
5185 static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5186 struct btrfs_root *root,
5187 struct btrfs_path *path,
5188 struct btrfs_key *first_key,
5189 struct btrfs_ref_path *ref_path)
5193 ret = relocate_one_path(trans, root, path, first_key,
5194 ref_path, NULL, NULL);
5197 if (root == root->fs_info->extent_root)
5198 btrfs_extent_post_op(trans, root);
5203 static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5204 struct btrfs_root *extent_root,
5205 struct btrfs_path *path,
5206 struct btrfs_key *extent_key)
5210 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5213 ret = btrfs_del_item(trans, extent_root, path);
5215 btrfs_release_path(extent_root, path);
5219 static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5220 struct btrfs_ref_path *ref_path)
5222 struct btrfs_key root_key;
5224 root_key.objectid = ref_path->root_objectid;
5225 root_key.type = BTRFS_ROOT_ITEM_KEY;
5226 if (is_cowonly_root(ref_path->root_objectid))
5227 root_key.offset = 0;
5229 root_key.offset = (u64)-1;
5231 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5234 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5235 struct btrfs_path *path,
5236 struct btrfs_key *extent_key,
5237 struct btrfs_block_group_cache *group,
5238 struct inode *reloc_inode, int pass)
5240 struct btrfs_trans_handle *trans;
5241 struct btrfs_root *found_root;
5242 struct btrfs_ref_path *ref_path = NULL;
5243 struct disk_extent *new_extents = NULL;
5248 struct btrfs_key first_key;
5252 trans = btrfs_start_transaction(extent_root, 1);
5255 if (extent_key->objectid == 0) {
5256 ret = del_extent_zero(trans, extent_root, path, extent_key);
5260 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5266 for (loops = 0; ; loops++) {
5268 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5269 extent_key->objectid);
5271 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5278 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5279 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5282 found_root = read_ref_root(extent_root->fs_info, ref_path);
5283 BUG_ON(!found_root);
5285 * for reference counted tree, only process reference paths
5286 * rooted at the latest committed root.
5288 if (found_root->ref_cows &&
5289 ref_path->root_generation != found_root->root_key.offset)
5292 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5295 * copy data extents to new locations
5297 u64 group_start = group->key.objectid;
5298 ret = relocate_data_extent(reloc_inode,
5307 level = ref_path->owner_objectid;
5310 if (prev_block != ref_path->nodes[level]) {
5311 struct extent_buffer *eb;
5312 u64 block_start = ref_path->nodes[level];
5313 u64 block_size = btrfs_level_size(found_root, level);
5315 eb = read_tree_block(found_root, block_start,
5317 btrfs_tree_lock(eb);
5318 BUG_ON(level != btrfs_header_level(eb));
5321 btrfs_item_key_to_cpu(eb, &first_key, 0);
5323 btrfs_node_key_to_cpu(eb, &first_key, 0);
5325 btrfs_tree_unlock(eb);
5326 free_extent_buffer(eb);
5327 prev_block = block_start;
5330 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5333 * use fallback method to process the remaining
5337 u64 group_start = group->key.objectid;
5338 new_extents = kmalloc(sizeof(*new_extents),
5341 ret = get_new_locations(reloc_inode,
5349 btrfs_record_root_in_trans(found_root);
5350 ret = replace_one_extent(trans, found_root,
5352 &first_key, ref_path,
5353 new_extents, nr_extents);
5359 btrfs_record_root_in_trans(found_root);
5360 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5361 ret = relocate_tree_block(trans, found_root, path,
5362 &first_key, ref_path);
5365 * try to update data extent references while
5366 * keeping metadata shared between snapshots.
5368 ret = relocate_one_path(trans, found_root, path,
5369 &first_key, ref_path,
5370 group, reloc_inode);
5377 btrfs_end_transaction(trans, extent_root);
5383 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5386 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5387 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5389 num_devices = root->fs_info->fs_devices->rw_devices;
5390 if (num_devices == 1) {
5391 stripped |= BTRFS_BLOCK_GROUP_DUP;
5392 stripped = flags & ~stripped;
5394 /* turn raid0 into single device chunks */
5395 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5398 /* turn mirroring into duplication */
5399 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5400 BTRFS_BLOCK_GROUP_RAID10))
5401 return stripped | BTRFS_BLOCK_GROUP_DUP;
5404 /* they already had raid on here, just return */
5405 if (flags & stripped)
5408 stripped |= BTRFS_BLOCK_GROUP_DUP;
5409 stripped = flags & ~stripped;
5411 /* switch duplicated blocks with raid1 */
5412 if (flags & BTRFS_BLOCK_GROUP_DUP)
5413 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5415 /* turn single device chunks into raid0 */
5416 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5421 int __alloc_chunk_for_shrink(struct btrfs_root *root,
5422 struct btrfs_block_group_cache *shrink_block_group,
5425 struct btrfs_trans_handle *trans;
5426 u64 new_alloc_flags;
5429 spin_lock(&shrink_block_group->lock);
5430 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5431 spin_unlock(&shrink_block_group->lock);
5433 trans = btrfs_start_transaction(root, 1);
5434 spin_lock(&shrink_block_group->lock);
5436 new_alloc_flags = update_block_group_flags(root,
5437 shrink_block_group->flags);
5438 if (new_alloc_flags != shrink_block_group->flags) {
5440 btrfs_block_group_used(&shrink_block_group->item);
5442 calc = shrink_block_group->key.offset;
5444 spin_unlock(&shrink_block_group->lock);
5446 do_chunk_alloc(trans, root->fs_info->extent_root,
5447 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5449 btrfs_end_transaction(trans, root);
5451 spin_unlock(&shrink_block_group->lock);
5455 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5456 struct btrfs_root *root,
5457 u64 objectid, u64 size)
5459 struct btrfs_path *path;
5460 struct btrfs_inode_item *item;
5461 struct extent_buffer *leaf;
5464 path = btrfs_alloc_path();
5468 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5472 leaf = path->nodes[0];
5473 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5474 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5475 btrfs_set_inode_generation(leaf, item, 1);
5476 btrfs_set_inode_size(leaf, item, size);
5477 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5478 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5479 BTRFS_INODE_NOCOMPRESS);
5480 btrfs_mark_buffer_dirty(leaf);
5481 btrfs_release_path(root, path);
5483 btrfs_free_path(path);
5487 static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5488 struct btrfs_block_group_cache *group)
5490 struct inode *inode = NULL;
5491 struct btrfs_trans_handle *trans;
5492 struct btrfs_root *root;
5493 struct btrfs_key root_key;
5494 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5497 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5498 root_key.type = BTRFS_ROOT_ITEM_KEY;
5499 root_key.offset = (u64)-1;
5500 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5502 return ERR_CAST(root);
5504 trans = btrfs_start_transaction(root, 1);
5507 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5511 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5514 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5515 group->key.offset, 0, group->key.offset,
5519 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5520 if (inode->i_state & I_NEW) {
5521 BTRFS_I(inode)->root = root;
5522 BTRFS_I(inode)->location.objectid = objectid;
5523 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5524 BTRFS_I(inode)->location.offset = 0;
5525 btrfs_read_locked_inode(inode);
5526 unlock_new_inode(inode);
5527 BUG_ON(is_bad_inode(inode));
5532 err = btrfs_orphan_add(trans, inode);
5534 btrfs_end_transaction(trans, root);
5538 inode = ERR_PTR(err);
5543 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5545 struct btrfs_trans_handle *trans;
5546 struct btrfs_path *path;
5547 struct btrfs_fs_info *info = root->fs_info;
5548 struct extent_buffer *leaf;
5549 struct inode *reloc_inode;
5550 struct btrfs_block_group_cache *block_group;
5551 struct btrfs_key key;
5560 root = root->fs_info->extent_root;
5562 block_group = btrfs_lookup_block_group(info, group_start);
5563 BUG_ON(!block_group);
5565 printk("btrfs relocating block group %llu flags %llu\n",
5566 (unsigned long long)block_group->key.objectid,
5567 (unsigned long long)block_group->flags);
5569 path = btrfs_alloc_path();
5572 reloc_inode = create_reloc_inode(info, block_group);
5573 BUG_ON(IS_ERR(reloc_inode));
5575 __alloc_chunk_for_shrink(root, block_group, 1);
5576 set_block_group_readonly(block_group);
5578 btrfs_start_delalloc_inodes(info->tree_root);
5579 btrfs_wait_ordered_extents(info->tree_root, 0);
5584 key.objectid = block_group->key.objectid;
5587 cur_byte = key.objectid;
5589 trans = btrfs_start_transaction(info->tree_root, 1);
5590 btrfs_commit_transaction(trans, info->tree_root);
5592 mutex_lock(&root->fs_info->cleaner_mutex);
5593 btrfs_clean_old_snapshots(info->tree_root);
5594 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5595 mutex_unlock(&root->fs_info->cleaner_mutex);
5598 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5602 leaf = path->nodes[0];
5603 nritems = btrfs_header_nritems(leaf);
5604 if (path->slots[0] >= nritems) {
5605 ret = btrfs_next_leaf(root, path);
5612 leaf = path->nodes[0];
5613 nritems = btrfs_header_nritems(leaf);
5616 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5618 if (key.objectid >= block_group->key.objectid +
5619 block_group->key.offset)
5622 if (progress && need_resched()) {
5623 btrfs_release_path(root, path);
5630 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5631 key.objectid + key.offset <= cur_byte) {
5637 cur_byte = key.objectid + key.offset;
5638 btrfs_release_path(root, path);
5640 __alloc_chunk_for_shrink(root, block_group, 0);
5641 ret = relocate_one_extent(root, path, &key, block_group,
5647 key.objectid = cur_byte;
5652 btrfs_release_path(root, path);
5655 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5656 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5657 WARN_ON(reloc_inode->i_mapping->nrpages);
5660 if (total_found > 0) {
5661 printk("btrfs found %llu extents in pass %d\n",
5662 (unsigned long long)total_found, pass);
5664 if (total_found == skipped && pass > 2) {
5666 reloc_inode = create_reloc_inode(info, block_group);
5672 /* delete reloc_inode */
5675 /* unpin extents in this range */
5676 trans = btrfs_start_transaction(info->tree_root, 1);
5677 btrfs_commit_transaction(trans, info->tree_root);
5679 spin_lock(&block_group->lock);
5680 WARN_ON(block_group->pinned > 0);
5681 WARN_ON(block_group->reserved > 0);
5682 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5683 spin_unlock(&block_group->lock);
5686 btrfs_free_path(path);
5690 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5691 struct btrfs_key *key)
5694 struct btrfs_key found_key;
5695 struct extent_buffer *leaf;
5698 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5703 slot = path->slots[0];
5704 leaf = path->nodes[0];
5705 if (slot >= btrfs_header_nritems(leaf)) {
5706 ret = btrfs_next_leaf(root, path);
5713 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5715 if (found_key.objectid >= key->objectid &&
5716 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5727 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5729 struct btrfs_block_group_cache *block_group;
5732 spin_lock(&info->block_group_cache_lock);
5733 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5734 block_group = rb_entry(n, struct btrfs_block_group_cache,
5736 rb_erase(&block_group->cache_node,
5737 &info->block_group_cache_tree);
5738 spin_unlock(&info->block_group_cache_lock);
5740 btrfs_remove_free_space_cache(block_group);
5741 down_write(&block_group->space_info->groups_sem);
5742 list_del(&block_group->list);
5743 up_write(&block_group->space_info->groups_sem);
5746 spin_lock(&info->block_group_cache_lock);
5748 spin_unlock(&info->block_group_cache_lock);
5752 int btrfs_read_block_groups(struct btrfs_root *root)
5754 struct btrfs_path *path;
5756 struct btrfs_block_group_cache *cache;
5757 struct btrfs_fs_info *info = root->fs_info;
5758 struct btrfs_space_info *space_info;
5759 struct btrfs_key key;
5760 struct btrfs_key found_key;
5761 struct extent_buffer *leaf;
5763 root = info->extent_root;
5766 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5767 path = btrfs_alloc_path();
5772 ret = find_first_block_group(root, path, &key);
5780 leaf = path->nodes[0];
5781 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5782 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5788 spin_lock_init(&cache->lock);
5789 mutex_init(&cache->alloc_mutex);
5790 INIT_LIST_HEAD(&cache->list);
5791 read_extent_buffer(leaf, &cache->item,
5792 btrfs_item_ptr_offset(leaf, path->slots[0]),
5793 sizeof(cache->item));
5794 memcpy(&cache->key, &found_key, sizeof(found_key));
5796 key.objectid = found_key.objectid + found_key.offset;
5797 btrfs_release_path(root, path);
5798 cache->flags = btrfs_block_group_flags(&cache->item);
5800 ret = update_space_info(info, cache->flags, found_key.offset,
5801 btrfs_block_group_used(&cache->item),
5804 cache->space_info = space_info;
5805 down_write(&space_info->groups_sem);
5806 list_add_tail(&cache->list, &space_info->block_groups);
5807 up_write(&space_info->groups_sem);
5809 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5812 set_avail_alloc_bits(root->fs_info, cache->flags);
5813 if (btrfs_chunk_readonly(root, cache->key.objectid))
5814 set_block_group_readonly(cache);
5818 btrfs_free_path(path);
5822 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5823 struct btrfs_root *root, u64 bytes_used,
5824 u64 type, u64 chunk_objectid, u64 chunk_offset,
5828 struct btrfs_root *extent_root;
5829 struct btrfs_block_group_cache *cache;
5831 extent_root = root->fs_info->extent_root;
5833 root->fs_info->last_trans_new_blockgroup = trans->transid;
5835 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5839 cache->key.objectid = chunk_offset;
5840 cache->key.offset = size;
5841 spin_lock_init(&cache->lock);
5842 mutex_init(&cache->alloc_mutex);
5843 INIT_LIST_HEAD(&cache->list);
5844 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5846 btrfs_set_block_group_used(&cache->item, bytes_used);
5847 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5848 cache->flags = type;
5849 btrfs_set_block_group_flags(&cache->item, type);
5851 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5852 &cache->space_info);
5854 down_write(&cache->space_info->groups_sem);
5855 list_add_tail(&cache->list, &cache->space_info->block_groups);
5856 up_write(&cache->space_info->groups_sem);
5858 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5861 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5862 sizeof(cache->item));
5865 finish_current_insert(trans, extent_root, 0);
5866 ret = del_pending_extents(trans, extent_root, 0);
5868 set_avail_alloc_bits(extent_root->fs_info, type);
5873 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5874 struct btrfs_root *root, u64 group_start)
5876 struct btrfs_path *path;
5877 struct btrfs_block_group_cache *block_group;
5878 struct btrfs_key key;
5881 root = root->fs_info->extent_root;
5883 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5884 BUG_ON(!block_group);
5885 BUG_ON(!block_group->ro);
5887 memcpy(&key, &block_group->key, sizeof(key));
5889 path = btrfs_alloc_path();
5892 btrfs_remove_free_space_cache(block_group);
5893 rb_erase(&block_group->cache_node,
5894 &root->fs_info->block_group_cache_tree);
5895 down_write(&block_group->space_info->groups_sem);
5896 list_del(&block_group->list);
5897 up_write(&block_group->space_info->groups_sem);
5899 spin_lock(&block_group->space_info->lock);
5900 block_group->space_info->total_bytes -= block_group->key.offset;
5901 block_group->space_info->bytes_readonly -= block_group->key.offset;
5902 spin_unlock(&block_group->space_info->lock);
5903 block_group->space_info->full = 0;
5906 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5907 kfree(shrink_block_group);
5910 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5916 ret = btrfs_del_item(trans, root, path);
5918 btrfs_free_path(path);