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) && !shint->ro) {
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 && !hint->ro && 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 (!cache->ro && block_group_bits(cache, data)) {
396 free_check = div_factor(cache->key.offset, factor);
397 if (used + cache->pinned + cache->reserved <
400 spin_unlock(&cache->lock);
404 spin_unlock(&cache->lock);
412 if (!full_search && factor < 10) {
422 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
423 struct btrfs_block_group_cache
424 *hint, u64 search_start,
428 struct btrfs_block_group_cache *ret;
429 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
433 /* simple helper to search for an existing extent at a given offset */
434 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
437 struct btrfs_key key;
438 struct btrfs_path *path;
440 path = btrfs_alloc_path();
442 key.objectid = start;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
447 btrfs_free_path(path);
452 * Back reference rules. Back refs have three main goals:
454 * 1) differentiate between all holders of references to an extent so that
455 * when a reference is dropped we can make sure it was a valid reference
456 * before freeing the extent.
458 * 2) Provide enough information to quickly find the holders of an extent
459 * if we notice a given block is corrupted or bad.
461 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
462 * maintenance. This is actually the same as #2, but with a slightly
463 * different use case.
465 * File extents can be referenced by:
467 * - multiple snapshots, subvolumes, or different generations in one subvol
468 * - different files inside a single subvolume
469 * - different offsets inside a file (bookend extents in file.c)
471 * The extent ref structure has fields for:
473 * - Objectid of the subvolume root
474 * - Generation number of the tree holding the reference
475 * - objectid of the file holding the reference
476 * - number of references holding by parent node (alway 1 for tree blocks)
478 * Btree leaf may hold multiple references to a file extent. In most cases,
479 * these references are from same file and the corresponding offsets inside
480 * the file are close together.
482 * When a file extent is allocated the fields are filled in:
483 * (root_key.objectid, trans->transid, inode objectid, 1)
485 * When a leaf is cow'd new references are added for every file extent found
486 * in the leaf. It looks similar to the create case, but trans->transid will
487 * be different when the block is cow'd.
489 * (root_key.objectid, trans->transid, inode objectid,
490 * number of references in the leaf)
492 * When a file extent is removed either during snapshot deletion or
493 * file truncation, we find the corresponding back reference and check
494 * the following fields:
496 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
499 * Btree extents can be referenced by:
501 * - Different subvolumes
502 * - Different generations of the same subvolume
504 * When a tree block is created, back references are inserted:
506 * (root->root_key.objectid, trans->transid, level, 1)
508 * When a tree block is cow'd, new back references are added for all the
509 * blocks it points to. If the tree block isn't in reference counted root,
510 * the old back references are removed. These new back references are of
511 * the form (trans->transid will have increased since creation):
513 * (root->root_key.objectid, trans->transid, level, 1)
515 * When a backref is in deleting, the following fields are checked:
517 * if backref was for a tree root:
518 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
520 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
522 * Back Reference Key composing:
524 * The key objectid corresponds to the first byte in the extent, the key
525 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
526 * byte of parent extent. If a extent is tree root, the key offset is set
527 * to the key objectid.
530 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
532 struct btrfs_path *path,
533 u64 bytenr, u64 parent,
534 u64 ref_root, u64 ref_generation,
535 u64 owner_objectid, int del)
537 struct btrfs_key key;
538 struct btrfs_extent_ref *ref;
539 struct extent_buffer *leaf;
543 key.objectid = bytenr;
544 key.type = BTRFS_EXTENT_REF_KEY;
547 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
555 leaf = path->nodes[0];
556 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
557 ref_objectid = btrfs_ref_objectid(leaf, ref);
558 if (btrfs_ref_root(leaf, ref) != ref_root ||
559 btrfs_ref_generation(leaf, ref) != ref_generation ||
560 (ref_objectid != owner_objectid &&
561 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
572 * updates all the backrefs that are pending on update_list for the
575 static int noinline update_backrefs(struct btrfs_trans_handle *trans,
576 struct btrfs_root *extent_root,
577 struct btrfs_path *path,
578 struct list_head *update_list)
580 struct btrfs_key key;
581 struct btrfs_extent_ref *ref;
582 struct btrfs_fs_info *info = extent_root->fs_info;
583 struct pending_extent_op *op;
584 struct extent_buffer *leaf;
586 struct list_head *cur = update_list->next;
588 u64 ref_root = extent_root->root_key.objectid;
590 op = list_entry(cur, struct pending_extent_op, list);
593 key.objectid = op->bytenr;
594 key.type = BTRFS_EXTENT_REF_KEY;
595 key.offset = op->orig_parent;
597 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
600 leaf = path->nodes[0];
603 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
605 ref_objectid = btrfs_ref_objectid(leaf, ref);
607 if (btrfs_ref_root(leaf, ref) != ref_root ||
608 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
609 (ref_objectid != op->level &&
610 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
611 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
612 "owner %u\n", op->bytenr, op->orig_parent,
613 ref_root, op->level);
614 btrfs_print_leaf(extent_root, leaf);
618 key.objectid = op->bytenr;
619 key.offset = op->parent;
620 key.type = BTRFS_EXTENT_REF_KEY;
621 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
623 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
624 btrfs_set_ref_generation(leaf, ref, op->generation);
628 list_del_init(&op->list);
629 unlock_extent(&info->extent_ins, op->bytenr,
630 op->bytenr + op->num_bytes - 1, GFP_NOFS);
633 if (cur == update_list) {
634 btrfs_mark_buffer_dirty(path->nodes[0]);
635 btrfs_release_path(extent_root, path);
639 op = list_entry(cur, struct pending_extent_op, list);
642 while (path->slots[0] < btrfs_header_nritems(leaf)) {
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
644 if (key.objectid == op->bytenr &&
645 key.type == BTRFS_EXTENT_REF_KEY)
650 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(extent_root, path);
658 static int noinline insert_extents(struct btrfs_trans_handle *trans,
659 struct btrfs_root *extent_root,
660 struct btrfs_path *path,
661 struct list_head *insert_list, int nr)
663 struct btrfs_key *keys;
665 struct pending_extent_op *op;
666 struct extent_buffer *leaf;
667 struct list_head *cur = insert_list->next;
668 struct btrfs_fs_info *info = extent_root->fs_info;
669 u64 ref_root = extent_root->root_key.objectid;
670 int i = 0, last = 0, ret;
676 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
680 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
686 list_for_each_entry(op, insert_list, list) {
687 keys[i].objectid = op->bytenr;
688 keys[i].offset = op->num_bytes;
689 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
690 data_size[i] = sizeof(struct btrfs_extent_item);
693 keys[i].objectid = op->bytenr;
694 keys[i].offset = op->parent;
695 keys[i].type = BTRFS_EXTENT_REF_KEY;
696 data_size[i] = sizeof(struct btrfs_extent_ref);
700 op = list_entry(cur, struct pending_extent_op, list);
704 ret = btrfs_insert_some_items(trans, extent_root, path,
705 keys+i, data_size+i, total-i);
711 leaf = path->nodes[0];
712 for (c = 0; c < ret; c++) {
713 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
716 * if the first item we inserted was a backref, then
717 * the EXTENT_ITEM will be the odd c's, else it will
720 if ((ref_first && (c % 2)) ||
721 (!ref_first && !(c % 2))) {
722 struct btrfs_extent_item *itm;
724 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
725 struct btrfs_extent_item);
726 btrfs_set_extent_refs(path->nodes[0], itm, 1);
729 struct btrfs_extent_ref *ref;
731 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
732 struct btrfs_extent_ref);
733 btrfs_set_ref_root(leaf, ref, ref_root);
734 btrfs_set_ref_generation(leaf, ref,
736 btrfs_set_ref_objectid(leaf, ref, op->level);
737 btrfs_set_ref_num_refs(leaf, ref, 1);
742 * using del to see when its ok to free up the
743 * pending_extent_op. In the case where we insert the
744 * last item on the list in order to help do batching
745 * we need to not free the extent op until we actually
746 * insert the extent_item
749 unlock_extent(&info->extent_ins, op->bytenr,
750 op->bytenr + op->num_bytes - 1,
753 list_del_init(&op->list);
755 if (cur != insert_list)
757 struct pending_extent_op,
761 btrfs_mark_buffer_dirty(leaf);
762 btrfs_release_path(extent_root, path);
765 * Ok backref's and items usually go right next to eachother,
766 * but if we could only insert 1 item that means that we
767 * inserted on the end of a leaf, and we have no idea what may
768 * be on the next leaf so we just play it safe. In order to
769 * try and help this case we insert the last thing on our
770 * insert list so hopefully it will end up being the last
771 * thing on the leaf and everything else will be before it,
772 * which will let us insert a whole bunch of items at the same
775 if (ret == 1 && !last && (i + ret < total)) {
777 * last: where we will pick up the next time around
778 * i: our current key to insert, will be total - 1
779 * cur: the current op we are screwing with
784 cur = insert_list->prev;
785 op = list_entry(cur, struct pending_extent_op, list);
788 * ok we successfully inserted the last item on the
789 * list, lets reset everything
791 * i: our current key to insert, so where we left off
793 * last: done with this
794 * cur: the op we are messing with
796 * total: since we inserted the last key, we need to
797 * decrement total so we dont overflow
801 cur = insert_list->next;
802 op = list_entry(cur, struct pending_extent_op, list);
816 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
817 struct btrfs_root *root,
818 struct btrfs_path *path,
819 u64 bytenr, u64 parent,
820 u64 ref_root, u64 ref_generation,
823 struct btrfs_key key;
824 struct extent_buffer *leaf;
825 struct btrfs_extent_ref *ref;
829 key.objectid = bytenr;
830 key.type = BTRFS_EXTENT_REF_KEY;
833 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
835 leaf = path->nodes[0];
836 ref = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_ref);
838 btrfs_set_ref_root(leaf, ref, ref_root);
839 btrfs_set_ref_generation(leaf, ref, ref_generation);
840 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
841 btrfs_set_ref_num_refs(leaf, ref, 1);
842 } else if (ret == -EEXIST) {
844 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
845 leaf = path->nodes[0];
846 ref = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref);
848 if (btrfs_ref_root(leaf, ref) != ref_root ||
849 btrfs_ref_generation(leaf, ref) != ref_generation) {
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
859 existing_owner = btrfs_ref_objectid(leaf, ref);
860 if (existing_owner != owner_objectid &&
861 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
862 btrfs_set_ref_objectid(leaf, ref,
863 BTRFS_MULTIPLE_OBJECTIDS);
869 btrfs_mark_buffer_dirty(path->nodes[0]);
871 btrfs_release_path(root, path);
875 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root,
877 struct btrfs_path *path)
879 struct extent_buffer *leaf;
880 struct btrfs_extent_ref *ref;
884 leaf = path->nodes[0];
885 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
886 num_refs = btrfs_ref_num_refs(leaf, ref);
887 BUG_ON(num_refs == 0);
890 ret = btrfs_del_item(trans, root, path);
892 btrfs_set_ref_num_refs(leaf, ref, num_refs);
893 btrfs_mark_buffer_dirty(leaf);
895 btrfs_release_path(root, path);
899 static int noinline free_extents(struct btrfs_trans_handle *trans,
900 struct btrfs_root *extent_root,
901 struct list_head *del_list)
903 struct btrfs_fs_info *info = extent_root->fs_info;
904 struct btrfs_path *path;
905 struct btrfs_key key, found_key;
906 struct extent_buffer *leaf;
907 struct list_head *cur;
908 struct pending_extent_op *op;
909 struct btrfs_extent_item *ei;
910 int ret, num_to_del, extent_slot = 0, found_extent = 0;
914 path = btrfs_alloc_path();
920 /* search for the backref for the current ref we want to delete */
921 cur = del_list->next;
922 op = list_entry(cur, struct pending_extent_op, list);
923 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
925 extent_root->root_key.objectid,
926 op->orig_generation, op->level, 1);
928 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
929 "owner %u\n", op->bytenr,
930 extent_root->root_key.objectid, op->orig_generation,
932 btrfs_print_leaf(extent_root, path->nodes[0]);
937 extent_slot = path->slots[0];
942 * if we aren't the first item on the leaf we can move back one and see
943 * if our ref is right next to our extent item
945 if (likely(extent_slot)) {
947 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
949 if (found_key.objectid == op->bytenr &&
950 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
951 found_key.offset == op->num_bytes) {
958 * if we didn't find the extent we need to delete the backref and then
959 * search for the extent item key so we can update its ref count
962 key.objectid = op->bytenr;
963 key.type = BTRFS_EXTENT_ITEM_KEY;
964 key.offset = op->num_bytes;
966 ret = remove_extent_backref(trans, extent_root, path);
968 btrfs_release_path(extent_root, path);
969 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
971 extent_slot = path->slots[0];
974 /* this is where we update the ref count for the extent */
975 leaf = path->nodes[0];
976 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
977 refs = btrfs_extent_refs(leaf, ei);
980 btrfs_set_extent_refs(leaf, ei, refs);
982 btrfs_mark_buffer_dirty(leaf);
985 * This extent needs deleting. The reason cur_slot is extent_slot +
986 * num_to_del is because extent_slot points to the slot where the extent
987 * is, and if the backref was not right next to the extent we will be
988 * deleting at least 1 item, and will want to start searching at the
989 * slot directly next to extent_slot. However if we did find the
990 * backref next to the extent item them we will be deleting at least 2
991 * items and will want to start searching directly after the ref slot
994 struct list_head *pos, *n, *end;
995 int cur_slot = extent_slot+num_to_del;
999 path->slots[0] = extent_slot;
1000 bytes_freed = op->num_bytes;
1003 * we need to see if we can delete multiple things at once, so
1004 * start looping through the list of extents we are wanting to
1005 * delete and see if their extent/backref's are right next to
1006 * eachother and the extents only have 1 ref
1008 for (pos = cur->next; pos != del_list; pos = pos->next) {
1009 struct pending_extent_op *tmp;
1011 tmp = list_entry(pos, struct pending_extent_op, list);
1013 /* we only want to delete extent+ref at this stage */
1014 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1017 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1018 if (found_key.objectid != tmp->bytenr ||
1019 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1020 found_key.offset != tmp->num_bytes)
1023 /* check to make sure this extent only has one ref */
1024 ei = btrfs_item_ptr(leaf, cur_slot,
1025 struct btrfs_extent_item);
1026 if (btrfs_extent_refs(leaf, ei) != 1)
1029 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1030 if (found_key.objectid != tmp->bytenr ||
1031 found_key.type != BTRFS_EXTENT_REF_KEY ||
1032 found_key.offset != tmp->orig_parent)
1036 * the ref is right next to the extent, we can set the
1037 * ref count to 0 since we will delete them both now
1039 btrfs_set_extent_refs(leaf, ei, 0);
1041 /* pin down the bytes for this extent */
1042 mutex_lock(&info->pinned_mutex);
1043 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1044 tmp->num_bytes, tmp->level >=
1045 BTRFS_FIRST_FREE_OBJECTID);
1046 mutex_unlock(&info->pinned_mutex);
1050 * use the del field to tell if we need to go ahead and
1051 * free up the extent when we delete the item or not.
1054 bytes_freed += tmp->num_bytes;
1061 /* update the free space counters */
1062 spin_lock_irq(&info->delalloc_lock);
1063 super_used = btrfs_super_bytes_used(&info->super_copy);
1064 btrfs_set_super_bytes_used(&info->super_copy,
1065 super_used - bytes_freed);
1066 spin_unlock_irq(&info->delalloc_lock);
1068 root_used = btrfs_root_used(&extent_root->root_item);
1069 btrfs_set_root_used(&extent_root->root_item,
1070 root_used - bytes_freed);
1072 /* delete the items */
1073 ret = btrfs_del_items(trans, extent_root, path,
1074 path->slots[0], num_to_del);
1078 * loop through the extents we deleted and do the cleanup work
1081 for (pos = cur, n = pos->next; pos != end;
1082 pos = n, n = pos->next) {
1083 struct pending_extent_op *tmp;
1084 #ifdef BIO_RW_DISCARD
1086 struct btrfs_multi_bio *multi = NULL;
1088 tmp = list_entry(pos, struct pending_extent_op, list);
1091 * remember tmp->del tells us wether or not we pinned
1094 ret = update_block_group(trans, extent_root,
1095 tmp->bytenr, tmp->num_bytes, 0,
1099 #ifdef BIO_RW_DISCARD
1100 ret = btrfs_map_block(&info->mapping_tree, READ,
1101 tmp->bytenr, &map_length, &multi,
1104 struct btrfs_bio_stripe *stripe;
1107 stripe = multi->stripe;
1109 if (map_length > tmp->num_bytes)
1110 map_length = tmp->num_bytes;
1112 for (i = 0; i < multi->num_stripes;
1114 blkdev_issue_discard(stripe->dev->bdev,
1115 stripe->physical >> 9,
1120 list_del_init(&tmp->list);
1121 unlock_extent(&info->extent_ins, tmp->bytenr,
1122 tmp->bytenr + tmp->num_bytes - 1,
1126 } else if (refs && found_extent) {
1128 * the ref and extent were right next to eachother, but the
1129 * extent still has a ref, so just free the backref and keep
1132 ret = remove_extent_backref(trans, extent_root, path);
1135 list_del_init(&op->list);
1136 unlock_extent(&info->extent_ins, op->bytenr,
1137 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1141 * the extent has multiple refs and the backref we were looking
1142 * for was not right next to it, so just unlock and go next,
1145 list_del_init(&op->list);
1146 unlock_extent(&info->extent_ins, op->bytenr,
1147 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1151 btrfs_release_path(extent_root, path);
1152 if (!list_empty(del_list))
1156 btrfs_free_path(path);
1160 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1161 struct btrfs_root *root, u64 bytenr,
1162 u64 orig_parent, u64 parent,
1163 u64 orig_root, u64 ref_root,
1164 u64 orig_generation, u64 ref_generation,
1168 struct btrfs_root *extent_root = root->fs_info->extent_root;
1169 struct btrfs_path *path;
1171 if (root == root->fs_info->extent_root) {
1172 struct pending_extent_op *extent_op;
1175 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1176 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1177 mutex_lock(&root->fs_info->extent_ins_mutex);
1178 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1179 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1181 ret = get_state_private(&root->fs_info->extent_ins,
1184 extent_op = (struct pending_extent_op *)
1185 (unsigned long)priv;
1186 BUG_ON(extent_op->parent != orig_parent);
1187 BUG_ON(extent_op->generation != orig_generation);
1189 extent_op->parent = parent;
1190 extent_op->generation = ref_generation;
1192 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1195 extent_op->type = PENDING_BACKREF_UPDATE;
1196 extent_op->bytenr = bytenr;
1197 extent_op->num_bytes = num_bytes;
1198 extent_op->parent = parent;
1199 extent_op->orig_parent = orig_parent;
1200 extent_op->generation = ref_generation;
1201 extent_op->orig_generation = orig_generation;
1202 extent_op->level = (int)owner_objectid;
1203 INIT_LIST_HEAD(&extent_op->list);
1206 set_extent_bits(&root->fs_info->extent_ins,
1207 bytenr, bytenr + num_bytes - 1,
1208 EXTENT_WRITEBACK, GFP_NOFS);
1209 set_state_private(&root->fs_info->extent_ins,
1210 bytenr, (unsigned long)extent_op);
1212 mutex_unlock(&root->fs_info->extent_ins_mutex);
1216 path = btrfs_alloc_path();
1219 ret = lookup_extent_backref(trans, extent_root, path,
1220 bytenr, orig_parent, orig_root,
1221 orig_generation, owner_objectid, 1);
1224 ret = remove_extent_backref(trans, extent_root, path);
1227 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1228 parent, ref_root, ref_generation,
1231 finish_current_insert(trans, extent_root, 0);
1232 del_pending_extents(trans, extent_root, 0);
1234 btrfs_free_path(path);
1238 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1239 struct btrfs_root *root, u64 bytenr,
1240 u64 orig_parent, u64 parent,
1241 u64 ref_root, u64 ref_generation,
1245 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1246 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1248 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1249 parent, ref_root, ref_root,
1250 ref_generation, ref_generation,
1255 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1256 struct btrfs_root *root, u64 bytenr,
1257 u64 orig_parent, u64 parent,
1258 u64 orig_root, u64 ref_root,
1259 u64 orig_generation, u64 ref_generation,
1262 struct btrfs_path *path;
1264 struct btrfs_key key;
1265 struct extent_buffer *l;
1266 struct btrfs_extent_item *item;
1269 path = btrfs_alloc_path();
1274 key.objectid = bytenr;
1275 key.type = BTRFS_EXTENT_ITEM_KEY;
1276 key.offset = (u64)-1;
1278 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1282 BUG_ON(ret == 0 || path->slots[0] == 0);
1287 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1288 if (key.objectid != bytenr) {
1289 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1290 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1293 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1295 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1296 refs = btrfs_extent_refs(l, item);
1297 btrfs_set_extent_refs(l, item, refs + 1);
1298 btrfs_mark_buffer_dirty(path->nodes[0]);
1300 btrfs_release_path(root->fs_info->extent_root, path);
1303 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1304 path, bytenr, parent,
1305 ref_root, ref_generation,
1308 finish_current_insert(trans, root->fs_info->extent_root, 0);
1309 del_pending_extents(trans, root->fs_info->extent_root, 0);
1311 btrfs_free_path(path);
1315 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1316 struct btrfs_root *root,
1317 u64 bytenr, u64 num_bytes, u64 parent,
1318 u64 ref_root, u64 ref_generation,
1322 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1323 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1325 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1326 0, ref_root, 0, ref_generation,
1331 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root)
1334 finish_current_insert(trans, root->fs_info->extent_root, 1);
1335 del_pending_extents(trans, root->fs_info->extent_root, 1);
1339 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root, u64 bytenr,
1341 u64 num_bytes, u32 *refs)
1343 struct btrfs_path *path;
1345 struct btrfs_key key;
1346 struct extent_buffer *l;
1347 struct btrfs_extent_item *item;
1349 WARN_ON(num_bytes < root->sectorsize);
1350 path = btrfs_alloc_path();
1352 key.objectid = bytenr;
1353 key.offset = num_bytes;
1354 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1355 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1360 btrfs_print_leaf(root, path->nodes[0]);
1361 printk("failed to find block number %Lu\n", bytenr);
1365 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1366 *refs = btrfs_extent_refs(l, item);
1368 btrfs_free_path(path);
1372 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root, u64 bytenr)
1375 struct btrfs_root *extent_root = root->fs_info->extent_root;
1376 struct btrfs_path *path;
1377 struct extent_buffer *leaf;
1378 struct btrfs_extent_ref *ref_item;
1379 struct btrfs_key key;
1380 struct btrfs_key found_key;
1386 key.objectid = bytenr;
1387 key.offset = (u64)-1;
1388 key.type = BTRFS_EXTENT_ITEM_KEY;
1390 path = btrfs_alloc_path();
1391 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1397 if (path->slots[0] == 0)
1401 leaf = path->nodes[0];
1402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1404 if (found_key.objectid != bytenr ||
1405 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1408 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1410 leaf = path->nodes[0];
1411 nritems = btrfs_header_nritems(leaf);
1412 if (path->slots[0] >= nritems) {
1413 ret = btrfs_next_leaf(extent_root, path);
1420 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1421 if (found_key.objectid != bytenr)
1424 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1429 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1430 struct btrfs_extent_ref);
1431 ref_root = btrfs_ref_root(leaf, ref_item);
1432 if (ref_root != root->root_key.objectid &&
1433 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1437 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1446 btrfs_free_path(path);
1450 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1451 struct extent_buffer *buf, u32 nr_extents)
1453 struct btrfs_key key;
1454 struct btrfs_file_extent_item *fi;
1462 if (!root->ref_cows)
1465 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1467 root_gen = root->root_key.offset;
1470 root_gen = trans->transid - 1;
1473 level = btrfs_header_level(buf);
1474 nritems = btrfs_header_nritems(buf);
1477 struct btrfs_leaf_ref *ref;
1478 struct btrfs_extent_info *info;
1480 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1486 ref->root_gen = root_gen;
1487 ref->bytenr = buf->start;
1488 ref->owner = btrfs_header_owner(buf);
1489 ref->generation = btrfs_header_generation(buf);
1490 ref->nritems = nr_extents;
1491 info = ref->extents;
1493 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1495 btrfs_item_key_to_cpu(buf, &key, i);
1496 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1498 fi = btrfs_item_ptr(buf, i,
1499 struct btrfs_file_extent_item);
1500 if (btrfs_file_extent_type(buf, fi) ==
1501 BTRFS_FILE_EXTENT_INLINE)
1503 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1504 if (disk_bytenr == 0)
1507 info->bytenr = disk_bytenr;
1509 btrfs_file_extent_disk_num_bytes(buf, fi);
1510 info->objectid = key.objectid;
1511 info->offset = key.offset;
1515 ret = btrfs_add_leaf_ref(root, ref, shared);
1516 if (ret == -EEXIST && shared) {
1517 struct btrfs_leaf_ref *old;
1518 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1520 btrfs_remove_leaf_ref(root, old);
1521 btrfs_free_leaf_ref(root, old);
1522 ret = btrfs_add_leaf_ref(root, ref, shared);
1525 btrfs_free_leaf_ref(root, ref);
1531 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1532 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1539 u64 orig_generation;
1541 u32 nr_file_extents = 0;
1542 struct btrfs_key key;
1543 struct btrfs_file_extent_item *fi;
1548 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1549 u64, u64, u64, u64, u64, u64, u64, u64);
1551 ref_root = btrfs_header_owner(buf);
1552 ref_generation = btrfs_header_generation(buf);
1553 orig_root = btrfs_header_owner(orig_buf);
1554 orig_generation = btrfs_header_generation(orig_buf);
1556 nritems = btrfs_header_nritems(buf);
1557 level = btrfs_header_level(buf);
1559 if (root->ref_cows) {
1560 process_func = __btrfs_inc_extent_ref;
1563 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1566 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1568 process_func = __btrfs_update_extent_ref;
1571 for (i = 0; i < nritems; i++) {
1574 btrfs_item_key_to_cpu(buf, &key, i);
1575 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1577 fi = btrfs_item_ptr(buf, i,
1578 struct btrfs_file_extent_item);
1579 if (btrfs_file_extent_type(buf, fi) ==
1580 BTRFS_FILE_EXTENT_INLINE)
1582 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1588 ret = process_func(trans, root, bytenr,
1589 orig_buf->start, buf->start,
1590 orig_root, ref_root,
1591 orig_generation, ref_generation,
1600 bytenr = btrfs_node_blockptr(buf, i);
1601 ret = process_func(trans, root, bytenr,
1602 orig_buf->start, buf->start,
1603 orig_root, ref_root,
1604 orig_generation, ref_generation,
1616 *nr_extents = nr_file_extents;
1618 *nr_extents = nritems;
1626 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1627 struct btrfs_root *root, struct extent_buffer *orig_buf,
1628 struct extent_buffer *buf, int start_slot, int nr)
1635 u64 orig_generation;
1636 struct btrfs_key key;
1637 struct btrfs_file_extent_item *fi;
1643 BUG_ON(start_slot < 0);
1644 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1646 ref_root = btrfs_header_owner(buf);
1647 ref_generation = btrfs_header_generation(buf);
1648 orig_root = btrfs_header_owner(orig_buf);
1649 orig_generation = btrfs_header_generation(orig_buf);
1650 level = btrfs_header_level(buf);
1652 if (!root->ref_cows) {
1654 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1657 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1661 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1664 btrfs_item_key_to_cpu(buf, &key, slot);
1665 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1667 fi = btrfs_item_ptr(buf, slot,
1668 struct btrfs_file_extent_item);
1669 if (btrfs_file_extent_type(buf, fi) ==
1670 BTRFS_FILE_EXTENT_INLINE)
1672 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1675 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1676 orig_buf->start, buf->start,
1677 orig_root, ref_root,
1678 orig_generation, ref_generation,
1683 bytenr = btrfs_node_blockptr(buf, slot);
1684 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1685 orig_buf->start, buf->start,
1686 orig_root, ref_root,
1687 orig_generation, ref_generation,
1699 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 struct btrfs_block_group_cache *cache)
1706 struct btrfs_root *extent_root = root->fs_info->extent_root;
1708 struct extent_buffer *leaf;
1710 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1715 leaf = path->nodes[0];
1716 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1717 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1718 btrfs_mark_buffer_dirty(leaf);
1719 btrfs_release_path(extent_root, path);
1721 finish_current_insert(trans, extent_root, 0);
1722 pending_ret = del_pending_extents(trans, extent_root, 0);
1731 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root)
1734 struct btrfs_block_group_cache *cache, *entry;
1738 struct btrfs_path *path;
1741 path = btrfs_alloc_path();
1747 spin_lock(&root->fs_info->block_group_cache_lock);
1748 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1749 n; n = rb_next(n)) {
1750 entry = rb_entry(n, struct btrfs_block_group_cache,
1757 spin_unlock(&root->fs_info->block_group_cache_lock);
1763 last += cache->key.offset;
1765 err = write_one_cache_group(trans, root,
1768 * if we fail to write the cache group, we want
1769 * to keep it marked dirty in hopes that a later
1777 btrfs_free_path(path);
1781 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1782 u64 total_bytes, u64 bytes_used,
1783 struct btrfs_space_info **space_info)
1785 struct btrfs_space_info *found;
1787 found = __find_space_info(info, flags);
1789 spin_lock(&found->lock);
1790 found->total_bytes += total_bytes;
1791 found->bytes_used += bytes_used;
1793 spin_unlock(&found->lock);
1794 *space_info = found;
1797 found = kzalloc(sizeof(*found), GFP_NOFS);
1801 list_add(&found->list, &info->space_info);
1802 INIT_LIST_HEAD(&found->block_groups);
1803 init_rwsem(&found->groups_sem);
1804 spin_lock_init(&found->lock);
1805 found->flags = flags;
1806 found->total_bytes = total_bytes;
1807 found->bytes_used = bytes_used;
1808 found->bytes_pinned = 0;
1809 found->bytes_reserved = 0;
1810 found->bytes_readonly = 0;
1812 found->force_alloc = 0;
1813 *space_info = found;
1817 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1819 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1820 BTRFS_BLOCK_GROUP_RAID1 |
1821 BTRFS_BLOCK_GROUP_RAID10 |
1822 BTRFS_BLOCK_GROUP_DUP);
1824 if (flags & BTRFS_BLOCK_GROUP_DATA)
1825 fs_info->avail_data_alloc_bits |= extra_flags;
1826 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1827 fs_info->avail_metadata_alloc_bits |= extra_flags;
1828 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1829 fs_info->avail_system_alloc_bits |= extra_flags;
1833 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1835 spin_lock(&cache->space_info->lock);
1836 spin_lock(&cache->lock);
1838 cache->space_info->bytes_readonly += cache->key.offset -
1839 btrfs_block_group_used(&cache->item);
1842 spin_unlock(&cache->lock);
1843 spin_unlock(&cache->space_info->lock);
1846 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1848 u64 num_devices = root->fs_info->fs_devices->num_devices;
1850 if (num_devices == 1)
1851 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1852 if (num_devices < 4)
1853 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1855 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1856 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1857 BTRFS_BLOCK_GROUP_RAID10))) {
1858 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1861 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1862 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1863 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1866 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1867 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1868 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1869 (flags & BTRFS_BLOCK_GROUP_DUP)))
1870 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1874 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1875 struct btrfs_root *extent_root, u64 alloc_bytes,
1876 u64 flags, int force)
1878 struct btrfs_space_info *space_info;
1884 mutex_lock(&extent_root->fs_info->chunk_mutex);
1886 flags = reduce_alloc_profile(extent_root, flags);
1888 space_info = __find_space_info(extent_root->fs_info, flags);
1890 ret = update_space_info(extent_root->fs_info, flags,
1894 BUG_ON(!space_info);
1896 spin_lock(&space_info->lock);
1897 if (space_info->force_alloc) {
1899 space_info->force_alloc = 0;
1901 if (space_info->full) {
1902 spin_unlock(&space_info->lock);
1906 thresh = space_info->total_bytes - space_info->bytes_readonly;
1907 thresh = div_factor(thresh, 6);
1909 (space_info->bytes_used + space_info->bytes_pinned +
1910 space_info->bytes_reserved + alloc_bytes) < thresh) {
1911 spin_unlock(&space_info->lock);
1914 spin_unlock(&space_info->lock);
1916 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1918 printk("space info full %Lu\n", flags);
1919 space_info->full = 1;
1923 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1924 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1927 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1931 static int update_block_group(struct btrfs_trans_handle *trans,
1932 struct btrfs_root *root,
1933 u64 bytenr, u64 num_bytes, int alloc,
1936 struct btrfs_block_group_cache *cache;
1937 struct btrfs_fs_info *info = root->fs_info;
1938 u64 total = num_bytes;
1943 cache = btrfs_lookup_block_group(info, bytenr);
1946 byte_in_group = bytenr - cache->key.objectid;
1947 WARN_ON(byte_in_group > cache->key.offset);
1949 spin_lock(&cache->space_info->lock);
1950 spin_lock(&cache->lock);
1952 old_val = btrfs_block_group_used(&cache->item);
1953 num_bytes = min(total, cache->key.offset - byte_in_group);
1955 old_val += num_bytes;
1956 cache->space_info->bytes_used += num_bytes;
1958 cache->space_info->bytes_readonly -= num_bytes;
1961 btrfs_set_block_group_used(&cache->item, old_val);
1962 spin_unlock(&cache->lock);
1963 spin_unlock(&cache->space_info->lock);
1965 old_val -= num_bytes;
1966 cache->space_info->bytes_used -= num_bytes;
1968 cache->space_info->bytes_readonly += num_bytes;
1969 btrfs_set_block_group_used(&cache->item, old_val);
1970 spin_unlock(&cache->lock);
1971 spin_unlock(&cache->space_info->lock);
1974 ret = btrfs_add_free_space(cache, bytenr,
1981 bytenr += num_bytes;
1986 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1988 struct btrfs_block_group_cache *cache;
1990 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1994 return cache->key.objectid;
1997 int btrfs_update_pinned_extents(struct btrfs_root *root,
1998 u64 bytenr, u64 num, int pin)
2001 struct btrfs_block_group_cache *cache;
2002 struct btrfs_fs_info *fs_info = root->fs_info;
2004 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2006 set_extent_dirty(&fs_info->pinned_extents,
2007 bytenr, bytenr + num - 1, GFP_NOFS);
2009 clear_extent_dirty(&fs_info->pinned_extents,
2010 bytenr, bytenr + num - 1, GFP_NOFS);
2013 cache = btrfs_lookup_block_group(fs_info, bytenr);
2015 len = min(num, cache->key.offset -
2016 (bytenr - cache->key.objectid));
2018 spin_lock(&cache->space_info->lock);
2019 spin_lock(&cache->lock);
2020 cache->pinned += len;
2021 cache->space_info->bytes_pinned += len;
2022 spin_unlock(&cache->lock);
2023 spin_unlock(&cache->space_info->lock);
2024 fs_info->total_pinned += len;
2026 spin_lock(&cache->space_info->lock);
2027 spin_lock(&cache->lock);
2028 cache->pinned -= len;
2029 cache->space_info->bytes_pinned -= len;
2030 spin_unlock(&cache->lock);
2031 spin_unlock(&cache->space_info->lock);
2032 fs_info->total_pinned -= len;
2040 static int update_reserved_extents(struct btrfs_root *root,
2041 u64 bytenr, u64 num, int reserve)
2044 struct btrfs_block_group_cache *cache;
2045 struct btrfs_fs_info *fs_info = root->fs_info;
2048 cache = btrfs_lookup_block_group(fs_info, bytenr);
2050 len = min(num, cache->key.offset -
2051 (bytenr - cache->key.objectid));
2053 spin_lock(&cache->space_info->lock);
2054 spin_lock(&cache->lock);
2056 cache->reserved += len;
2057 cache->space_info->bytes_reserved += len;
2059 cache->reserved -= len;
2060 cache->space_info->bytes_reserved -= len;
2062 spin_unlock(&cache->lock);
2063 spin_unlock(&cache->space_info->lock);
2070 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2075 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2078 mutex_lock(&root->fs_info->pinned_mutex);
2080 ret = find_first_extent_bit(pinned_extents, last,
2081 &start, &end, EXTENT_DIRTY);
2084 set_extent_dirty(copy, start, end, GFP_NOFS);
2087 mutex_unlock(&root->fs_info->pinned_mutex);
2091 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2092 struct btrfs_root *root,
2093 struct extent_io_tree *unpin)
2098 struct btrfs_block_group_cache *cache;
2100 mutex_lock(&root->fs_info->pinned_mutex);
2102 ret = find_first_extent_bit(unpin, 0, &start, &end,
2106 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2107 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2108 cache = btrfs_lookup_block_group(root->fs_info, start);
2110 btrfs_add_free_space(cache, start, end - start + 1);
2111 if (need_resched()) {
2112 mutex_unlock(&root->fs_info->pinned_mutex);
2114 mutex_lock(&root->fs_info->pinned_mutex);
2117 mutex_unlock(&root->fs_info->pinned_mutex);
2121 static int finish_current_insert(struct btrfs_trans_handle *trans,
2122 struct btrfs_root *extent_root, int all)
2129 struct btrfs_fs_info *info = extent_root->fs_info;
2130 struct btrfs_path *path;
2131 struct pending_extent_op *extent_op, *tmp;
2132 struct list_head insert_list, update_list;
2134 int num_inserts = 0, max_inserts;
2136 path = btrfs_alloc_path();
2137 INIT_LIST_HEAD(&insert_list);
2138 INIT_LIST_HEAD(&update_list);
2140 max_inserts = extent_root->leafsize /
2141 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2142 sizeof(struct btrfs_extent_ref) +
2143 sizeof(struct btrfs_extent_item));
2145 mutex_lock(&info->extent_ins_mutex);
2147 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2148 &end, EXTENT_WRITEBACK);
2150 if (skipped && all && !num_inserts) {
2154 mutex_unlock(&info->extent_ins_mutex);
2158 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2162 if (need_resched()) {
2163 mutex_unlock(&info->extent_ins_mutex);
2165 mutex_lock(&info->extent_ins_mutex);
2170 ret = get_state_private(&info->extent_ins, start, &priv);
2172 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2174 if (extent_op->type == PENDING_EXTENT_INSERT) {
2176 list_add_tail(&extent_op->list, &insert_list);
2178 if (num_inserts == max_inserts) {
2179 mutex_unlock(&info->extent_ins_mutex);
2182 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2183 list_add_tail(&extent_op->list, &update_list);
2191 * process teh update list, clear the writeback bit for it, and if
2192 * somebody marked this thing for deletion then just unlock it and be
2193 * done, the free_extents will handle it
2195 mutex_lock(&info->extent_ins_mutex);
2196 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2197 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2198 extent_op->bytenr + extent_op->num_bytes - 1,
2199 EXTENT_WRITEBACK, GFP_NOFS);
2200 if (extent_op->del) {
2201 list_del_init(&extent_op->list);
2202 unlock_extent(&info->extent_ins, extent_op->bytenr,
2203 extent_op->bytenr + extent_op->num_bytes
2208 mutex_unlock(&info->extent_ins_mutex);
2211 * still have things left on the update list, go ahead an update
2214 if (!list_empty(&update_list)) {
2215 ret = update_backrefs(trans, extent_root, path, &update_list);
2220 * if no inserts need to be done, but we skipped some extents and we
2221 * need to make sure everything is cleaned then reset everything and
2222 * go back to the beginning
2224 if (!num_inserts && all && skipped) {
2227 INIT_LIST_HEAD(&update_list);
2228 INIT_LIST_HEAD(&insert_list);
2230 } else if (!num_inserts) {
2235 * process the insert extents list. Again if we are deleting this
2236 * extent, then just unlock it, pin down the bytes if need be, and be
2237 * done with it. Saves us from having to actually insert the extent
2238 * into the tree and then subsequently come along and delete it
2240 mutex_lock(&info->extent_ins_mutex);
2241 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2242 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2243 extent_op->bytenr + extent_op->num_bytes - 1,
2244 EXTENT_WRITEBACK, GFP_NOFS);
2245 if (extent_op->del) {
2246 list_del_init(&extent_op->list);
2247 unlock_extent(&info->extent_ins, extent_op->bytenr,
2248 extent_op->bytenr + extent_op->num_bytes
2251 mutex_lock(&extent_root->fs_info->pinned_mutex);
2252 ret = pin_down_bytes(trans, extent_root,
2254 extent_op->num_bytes, 0);
2255 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2257 ret = update_block_group(trans, extent_root,
2259 extent_op->num_bytes,
2266 mutex_unlock(&info->extent_ins_mutex);
2268 ret = insert_extents(trans, extent_root, path, &insert_list,
2273 * if we broke out of the loop in order to insert stuff because we hit
2274 * the maximum number of inserts at a time we can handle, then loop
2275 * back and pick up where we left off
2277 if (num_inserts == max_inserts) {
2278 INIT_LIST_HEAD(&insert_list);
2279 INIT_LIST_HEAD(&update_list);
2285 * again, if we need to make absolutely sure there are no more pending
2286 * extent operations left and we know that we skipped some, go back to
2287 * the beginning and do it all again
2289 if (all && skipped) {
2290 INIT_LIST_HEAD(&insert_list);
2291 INIT_LIST_HEAD(&update_list);
2298 btrfs_free_path(path);
2302 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2303 struct btrfs_root *root,
2304 u64 bytenr, u64 num_bytes, int is_data)
2307 struct extent_buffer *buf;
2312 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2316 /* we can reuse a block if it hasn't been written
2317 * and it is from this transaction. We can't
2318 * reuse anything from the tree log root because
2319 * it has tiny sub-transactions.
2321 if (btrfs_buffer_uptodate(buf, 0) &&
2322 btrfs_try_tree_lock(buf)) {
2323 u64 header_owner = btrfs_header_owner(buf);
2324 u64 header_transid = btrfs_header_generation(buf);
2325 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2326 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2327 header_transid == trans->transid &&
2328 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2329 clean_tree_block(NULL, root, buf);
2330 btrfs_tree_unlock(buf);
2331 free_extent_buffer(buf);
2334 btrfs_tree_unlock(buf);
2336 free_extent_buffer(buf);
2338 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2345 * remove an extent from the root, returns 0 on success
2347 static int __free_extent(struct btrfs_trans_handle *trans,
2348 struct btrfs_root *root,
2349 u64 bytenr, u64 num_bytes, u64 parent,
2350 u64 root_objectid, u64 ref_generation,
2351 u64 owner_objectid, int pin, int mark_free)
2353 struct btrfs_path *path;
2354 struct btrfs_key key;
2355 struct btrfs_fs_info *info = root->fs_info;
2356 struct btrfs_root *extent_root = info->extent_root;
2357 struct extent_buffer *leaf;
2359 int extent_slot = 0;
2360 int found_extent = 0;
2362 struct btrfs_extent_item *ei;
2365 key.objectid = bytenr;
2366 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2367 key.offset = num_bytes;
2368 path = btrfs_alloc_path();
2373 ret = lookup_extent_backref(trans, extent_root, path,
2374 bytenr, parent, root_objectid,
2375 ref_generation, owner_objectid, 1);
2377 struct btrfs_key found_key;
2378 extent_slot = path->slots[0];
2379 while(extent_slot > 0) {
2381 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2383 if (found_key.objectid != bytenr)
2385 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2386 found_key.offset == num_bytes) {
2390 if (path->slots[0] - extent_slot > 5)
2393 if (!found_extent) {
2394 ret = remove_extent_backref(trans, extent_root, path);
2396 btrfs_release_path(extent_root, path);
2397 ret = btrfs_search_slot(trans, extent_root,
2400 printk(KERN_ERR "umm, got %d back from search"
2401 ", was looking for %Lu\n", ret,
2403 btrfs_print_leaf(extent_root, path->nodes[0]);
2406 extent_slot = path->slots[0];
2409 btrfs_print_leaf(extent_root, path->nodes[0]);
2411 printk("Unable to find ref byte nr %Lu root %Lu "
2412 "gen %Lu owner %Lu\n", bytenr,
2413 root_objectid, ref_generation, owner_objectid);
2416 leaf = path->nodes[0];
2417 ei = btrfs_item_ptr(leaf, extent_slot,
2418 struct btrfs_extent_item);
2419 refs = btrfs_extent_refs(leaf, ei);
2422 btrfs_set_extent_refs(leaf, ei, refs);
2424 btrfs_mark_buffer_dirty(leaf);
2426 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2427 struct btrfs_extent_ref *ref;
2428 ref = btrfs_item_ptr(leaf, path->slots[0],
2429 struct btrfs_extent_ref);
2430 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2431 /* if the back ref and the extent are next to each other
2432 * they get deleted below in one shot
2434 path->slots[0] = extent_slot;
2436 } else if (found_extent) {
2437 /* otherwise delete the extent back ref */
2438 ret = remove_extent_backref(trans, extent_root, path);
2440 /* if refs are 0, we need to setup the path for deletion */
2442 btrfs_release_path(extent_root, path);
2443 ret = btrfs_search_slot(trans, extent_root, &key, path,
2452 #ifdef BIO_RW_DISCARD
2453 u64 map_length = num_bytes;
2454 struct btrfs_multi_bio *multi = NULL;
2458 mutex_lock(&root->fs_info->pinned_mutex);
2459 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2460 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2461 mutex_unlock(&root->fs_info->pinned_mutex);
2467 /* block accounting for super block */
2468 spin_lock_irq(&info->delalloc_lock);
2469 super_used = btrfs_super_bytes_used(&info->super_copy);
2470 btrfs_set_super_bytes_used(&info->super_copy,
2471 super_used - num_bytes);
2472 spin_unlock_irq(&info->delalloc_lock);
2474 /* block accounting for root item */
2475 root_used = btrfs_root_used(&root->root_item);
2476 btrfs_set_root_used(&root->root_item,
2477 root_used - num_bytes);
2478 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2481 btrfs_release_path(extent_root, path);
2482 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2486 #ifdef BIO_RW_DISCARD
2487 /* Tell the block device(s) that the sectors can be discarded */
2488 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2489 bytenr, &map_length, &multi, 0);
2491 struct btrfs_bio_stripe *stripe = multi->stripes;
2494 if (map_length > num_bytes)
2495 map_length = num_bytes;
2497 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2498 blkdev_issue_discard(stripe->dev->bdev,
2499 stripe->physical >> 9,
2506 btrfs_free_path(path);
2507 finish_current_insert(trans, extent_root, 0);
2512 * find all the blocks marked as pending in the radix tree and remove
2513 * them from the extent map
2515 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2516 btrfs_root *extent_root, int all)
2524 int nr = 0, skipped = 0;
2525 struct extent_io_tree *pending_del;
2526 struct extent_io_tree *extent_ins;
2527 struct pending_extent_op *extent_op;
2528 struct btrfs_fs_info *info = extent_root->fs_info;
2529 struct list_head delete_list;
2531 INIT_LIST_HEAD(&delete_list);
2532 extent_ins = &extent_root->fs_info->extent_ins;
2533 pending_del = &extent_root->fs_info->pending_del;
2536 mutex_lock(&info->extent_ins_mutex);
2538 ret = find_first_extent_bit(pending_del, search, &start, &end,
2541 if (all && skipped && !nr) {
2545 mutex_unlock(&info->extent_ins_mutex);
2549 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2554 if (need_resched()) {
2555 mutex_unlock(&info->extent_ins_mutex);
2557 mutex_lock(&info->extent_ins_mutex);
2564 ret = get_state_private(pending_del, start, &priv);
2566 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2568 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2570 if (!test_range_bit(extent_ins, start, end,
2571 EXTENT_WRITEBACK, 0)) {
2572 list_add_tail(&extent_op->list, &delete_list);
2577 ret = get_state_private(&info->extent_ins, start,
2580 extent_op = (struct pending_extent_op *)
2581 (unsigned long)priv;
2583 clear_extent_bits(&info->extent_ins, start, end,
2584 EXTENT_WRITEBACK, GFP_NOFS);
2586 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2587 list_add_tail(&extent_op->list, &delete_list);
2593 mutex_lock(&extent_root->fs_info->pinned_mutex);
2594 ret = pin_down_bytes(trans, extent_root, start,
2595 end + 1 - start, 0);
2596 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2598 ret = update_block_group(trans, extent_root, start,
2599 end + 1 - start, 0, ret > 0);
2601 unlock_extent(extent_ins, start, end, GFP_NOFS);
2610 if (need_resched()) {
2611 mutex_unlock(&info->extent_ins_mutex);
2613 mutex_lock(&info->extent_ins_mutex);
2618 ret = free_extents(trans, extent_root, &delete_list);
2622 if (all && skipped) {
2623 INIT_LIST_HEAD(&delete_list);
2633 * remove an extent from the root, returns 0 on success
2635 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2636 struct btrfs_root *root,
2637 u64 bytenr, u64 num_bytes, u64 parent,
2638 u64 root_objectid, u64 ref_generation,
2639 u64 owner_objectid, int pin)
2641 struct btrfs_root *extent_root = root->fs_info->extent_root;
2645 WARN_ON(num_bytes < root->sectorsize);
2646 if (root == extent_root) {
2647 struct pending_extent_op *extent_op = NULL;
2649 mutex_lock(&root->fs_info->extent_ins_mutex);
2650 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2651 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2653 ret = get_state_private(&root->fs_info->extent_ins,
2656 extent_op = (struct pending_extent_op *)
2657 (unsigned long)priv;
2660 if (extent_op->type == PENDING_EXTENT_INSERT) {
2661 mutex_unlock(&root->fs_info->extent_ins_mutex);
2667 ref_generation = extent_op->orig_generation;
2668 parent = extent_op->orig_parent;
2671 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2674 extent_op->type = PENDING_EXTENT_DELETE;
2675 extent_op->bytenr = bytenr;
2676 extent_op->num_bytes = num_bytes;
2677 extent_op->parent = parent;
2678 extent_op->orig_parent = parent;
2679 extent_op->generation = ref_generation;
2680 extent_op->orig_generation = ref_generation;
2681 extent_op->level = (int)owner_objectid;
2682 INIT_LIST_HEAD(&extent_op->list);
2685 set_extent_bits(&root->fs_info->pending_del,
2686 bytenr, bytenr + num_bytes - 1,
2687 EXTENT_WRITEBACK, GFP_NOFS);
2688 set_state_private(&root->fs_info->pending_del,
2689 bytenr, (unsigned long)extent_op);
2690 mutex_unlock(&root->fs_info->extent_ins_mutex);
2693 /* if metadata always pin */
2694 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2695 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2696 struct btrfs_block_group_cache *cache;
2698 /* btrfs_free_reserved_extent */
2699 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2701 btrfs_add_free_space(cache, bytenr, num_bytes);
2702 update_reserved_extents(root, bytenr, num_bytes, 0);
2708 /* if data pin when any transaction has committed this */
2709 if (ref_generation != trans->transid)
2712 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2713 root_objectid, ref_generation,
2714 owner_objectid, pin, pin == 0);
2716 finish_current_insert(trans, root->fs_info->extent_root, 0);
2717 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2718 return ret ? ret : pending_ret;
2721 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2722 struct btrfs_root *root,
2723 u64 bytenr, u64 num_bytes, u64 parent,
2724 u64 root_objectid, u64 ref_generation,
2725 u64 owner_objectid, int pin)
2729 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2730 root_objectid, ref_generation,
2731 owner_objectid, pin);
2735 static u64 stripe_align(struct btrfs_root *root, u64 val)
2737 u64 mask = ((u64)root->stripesize - 1);
2738 u64 ret = (val + mask) & ~mask;
2743 * walks the btree of allocated extents and find a hole of a given size.
2744 * The key ins is changed to record the hole:
2745 * ins->objectid == block start
2746 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2747 * ins->offset == number of blocks
2748 * Any available blocks before search_start are skipped.
2750 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2751 struct btrfs_root *orig_root,
2752 u64 num_bytes, u64 empty_size,
2753 u64 search_start, u64 search_end,
2754 u64 hint_byte, struct btrfs_key *ins,
2755 u64 exclude_start, u64 exclude_nr,
2759 struct btrfs_root * root = orig_root->fs_info->extent_root;
2760 u64 total_needed = num_bytes;
2761 u64 *last_ptr = NULL;
2762 u64 last_wanted = 0;
2763 struct btrfs_block_group_cache *block_group = NULL;
2764 int chunk_alloc_done = 0;
2765 int empty_cluster = 2 * 1024 * 1024;
2766 int allowed_chunk_alloc = 0;
2767 struct list_head *head = NULL, *cur = NULL;
2770 struct btrfs_space_info *space_info;
2772 WARN_ON(num_bytes < root->sectorsize);
2773 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2777 if (orig_root->ref_cows || empty_size)
2778 allowed_chunk_alloc = 1;
2780 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2781 last_ptr = &root->fs_info->last_alloc;
2782 empty_cluster = 64 * 1024;
2785 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2786 last_ptr = &root->fs_info->last_data_alloc;
2790 hint_byte = *last_ptr;
2791 last_wanted = *last_ptr;
2793 empty_size += empty_cluster;
2797 search_start = max(search_start, first_logical_byte(root, 0));
2798 search_start = max(search_start, hint_byte);
2800 if (last_wanted && search_start != last_wanted) {
2802 empty_size += empty_cluster;
2805 total_needed += empty_size;
2806 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2808 block_group = btrfs_lookup_first_block_group(root->fs_info,
2810 space_info = __find_space_info(root->fs_info, data);
2812 down_read(&space_info->groups_sem);
2814 struct btrfs_free_space *free_space;
2816 * the only way this happens if our hint points to a block
2817 * group thats not of the proper type, while looping this
2818 * should never happen
2824 goto new_group_no_lock;
2826 mutex_lock(&block_group->alloc_mutex);
2827 if (unlikely(!block_group_bits(block_group, data)))
2830 ret = cache_block_group(root, block_group);
2832 mutex_unlock(&block_group->alloc_mutex);
2836 if (block_group->ro)
2839 free_space = btrfs_find_free_space(block_group, search_start,
2842 u64 start = block_group->key.objectid;
2843 u64 end = block_group->key.objectid +
2844 block_group->key.offset;
2846 search_start = stripe_align(root, free_space->offset);
2848 /* move on to the next group */
2849 if (search_start + num_bytes >= search_end)
2852 /* move on to the next group */
2853 if (search_start + num_bytes > end)
2856 if (last_wanted && search_start != last_wanted) {
2857 total_needed += empty_cluster;
2858 empty_size += empty_cluster;
2861 * if search_start is still in this block group
2862 * then we just re-search this block group
2864 if (search_start >= start &&
2865 search_start < end) {
2866 mutex_unlock(&block_group->alloc_mutex);
2870 /* else we go to the next block group */
2874 if (exclude_nr > 0 &&
2875 (search_start + num_bytes > exclude_start &&
2876 search_start < exclude_start + exclude_nr)) {
2877 search_start = exclude_start + exclude_nr;
2879 * if search_start is still in this block group
2880 * then we just re-search this block group
2882 if (search_start >= start &&
2883 search_start < end) {
2884 mutex_unlock(&block_group->alloc_mutex);
2889 /* else we go to the next block group */
2893 ins->objectid = search_start;
2894 ins->offset = num_bytes;
2896 btrfs_remove_free_space_lock(block_group, search_start,
2898 /* we are all good, lets return */
2899 mutex_unlock(&block_group->alloc_mutex);
2903 mutex_unlock(&block_group->alloc_mutex);
2905 /* don't try to compare new allocations against the
2906 * last allocation any more
2911 * Here's how this works.
2912 * loop == 0: we were searching a block group via a hint
2913 * and didn't find anything, so we start at
2914 * the head of the block groups and keep searching
2915 * loop == 1: we're searching through all of the block groups
2916 * if we hit the head again we have searched
2917 * all of the block groups for this space and we
2918 * need to try and allocate, if we cant error out.
2919 * loop == 2: we allocated more space and are looping through
2920 * all of the block groups again.
2923 head = &space_info->block_groups;
2926 } else if (loop == 1 && cur == head) {
2929 /* at this point we give up on the empty_size
2930 * allocations and just try to allocate the min
2933 * The extra_loop field was set if an empty_size
2934 * allocation was attempted above, and if this
2935 * is try we need to try the loop again without
2936 * the additional empty_size.
2938 total_needed -= empty_size;
2940 keep_going = extra_loop;
2943 if (allowed_chunk_alloc && !chunk_alloc_done) {
2944 up_read(&space_info->groups_sem);
2945 ret = do_chunk_alloc(trans, root, num_bytes +
2946 2 * 1024 * 1024, data, 1);
2947 down_read(&space_info->groups_sem);
2950 head = &space_info->block_groups;
2952 * we've allocated a new chunk, keep
2956 chunk_alloc_done = 1;
2957 } else if (!allowed_chunk_alloc) {
2958 space_info->force_alloc = 1;
2967 } else if (cur == head) {
2971 block_group = list_entry(cur, struct btrfs_block_group_cache,
2973 search_start = block_group->key.objectid;
2977 /* we found what we needed */
2978 if (ins->objectid) {
2979 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2980 trans->block_group = block_group;
2983 *last_ptr = ins->objectid + ins->offset;
2989 up_read(&space_info->groups_sem);
2993 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2995 struct btrfs_block_group_cache *cache;
2996 struct list_head *l;
2998 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2999 info->total_bytes - info->bytes_used - info->bytes_pinned -
3000 info->bytes_reserved, (info->full) ? "" : "not ");
3002 down_read(&info->groups_sem);
3003 list_for_each(l, &info->block_groups) {
3004 cache = list_entry(l, struct btrfs_block_group_cache, list);
3005 spin_lock(&cache->lock);
3006 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
3007 "%Lu pinned %Lu reserved\n",
3008 cache->key.objectid, cache->key.offset,
3009 btrfs_block_group_used(&cache->item),
3010 cache->pinned, cache->reserved);
3011 btrfs_dump_free_space(cache, bytes);
3012 spin_unlock(&cache->lock);
3014 up_read(&info->groups_sem);
3017 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *root,
3019 u64 num_bytes, u64 min_alloc_size,
3020 u64 empty_size, u64 hint_byte,
3021 u64 search_end, struct btrfs_key *ins,
3025 u64 search_start = 0;
3027 struct btrfs_fs_info *info = root->fs_info;
3030 alloc_profile = info->avail_data_alloc_bits &
3031 info->data_alloc_profile;
3032 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3033 } else if (root == root->fs_info->chunk_root) {
3034 alloc_profile = info->avail_system_alloc_bits &
3035 info->system_alloc_profile;
3036 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3038 alloc_profile = info->avail_metadata_alloc_bits &
3039 info->metadata_alloc_profile;
3040 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3043 data = reduce_alloc_profile(root, data);
3045 * the only place that sets empty_size is btrfs_realloc_node, which
3046 * is not called recursively on allocations
3048 if (empty_size || root->ref_cows) {
3049 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3050 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3052 BTRFS_BLOCK_GROUP_METADATA |
3053 (info->metadata_alloc_profile &
3054 info->avail_metadata_alloc_bits), 0);
3056 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3057 num_bytes + 2 * 1024 * 1024, data, 0);
3060 WARN_ON(num_bytes < root->sectorsize);
3061 ret = find_free_extent(trans, root, num_bytes, empty_size,
3062 search_start, search_end, hint_byte, ins,
3063 trans->alloc_exclude_start,
3064 trans->alloc_exclude_nr, data);
3066 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3067 num_bytes = num_bytes >> 1;
3068 num_bytes = num_bytes & ~(root->sectorsize - 1);
3069 num_bytes = max(num_bytes, min_alloc_size);
3070 do_chunk_alloc(trans, root->fs_info->extent_root,
3071 num_bytes, data, 1);
3075 struct btrfs_space_info *sinfo;
3077 sinfo = __find_space_info(root->fs_info, data);
3078 printk("allocation failed flags %Lu, wanted %Lu\n",
3080 dump_space_info(sinfo, num_bytes);
3087 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3089 struct btrfs_block_group_cache *cache;
3091 cache = btrfs_lookup_block_group(root->fs_info, start);
3093 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
3096 btrfs_add_free_space(cache, start, len);
3097 update_reserved_extents(root, start, len, 0);
3101 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3102 struct btrfs_root *root,
3103 u64 num_bytes, u64 min_alloc_size,
3104 u64 empty_size, u64 hint_byte,
3105 u64 search_end, struct btrfs_key *ins,
3109 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3110 empty_size, hint_byte, search_end, ins,
3112 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3116 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3117 struct btrfs_root *root, u64 parent,
3118 u64 root_objectid, u64 ref_generation,
3119 u64 owner, struct btrfs_key *ins)
3125 u64 num_bytes = ins->offset;
3127 struct btrfs_fs_info *info = root->fs_info;
3128 struct btrfs_root *extent_root = info->extent_root;
3129 struct btrfs_extent_item *extent_item;
3130 struct btrfs_extent_ref *ref;
3131 struct btrfs_path *path;
3132 struct btrfs_key keys[2];
3135 parent = ins->objectid;
3137 /* block accounting for super block */
3138 spin_lock_irq(&info->delalloc_lock);
3139 super_used = btrfs_super_bytes_used(&info->super_copy);
3140 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3141 spin_unlock_irq(&info->delalloc_lock);
3143 /* block accounting for root item */
3144 root_used = btrfs_root_used(&root->root_item);
3145 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3147 if (root == extent_root) {
3148 struct pending_extent_op *extent_op;
3150 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3153 extent_op->type = PENDING_EXTENT_INSERT;
3154 extent_op->bytenr = ins->objectid;
3155 extent_op->num_bytes = ins->offset;
3156 extent_op->parent = parent;
3157 extent_op->orig_parent = 0;
3158 extent_op->generation = ref_generation;
3159 extent_op->orig_generation = 0;
3160 extent_op->level = (int)owner;
3161 INIT_LIST_HEAD(&extent_op->list);
3164 mutex_lock(&root->fs_info->extent_ins_mutex);
3165 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3166 ins->objectid + ins->offset - 1,
3167 EXTENT_WRITEBACK, GFP_NOFS);
3168 set_state_private(&root->fs_info->extent_ins,
3169 ins->objectid, (unsigned long)extent_op);
3170 mutex_unlock(&root->fs_info->extent_ins_mutex);
3174 memcpy(&keys[0], ins, sizeof(*ins));
3175 keys[1].objectid = ins->objectid;
3176 keys[1].type = BTRFS_EXTENT_REF_KEY;
3177 keys[1].offset = parent;
3178 sizes[0] = sizeof(*extent_item);
3179 sizes[1] = sizeof(*ref);
3181 path = btrfs_alloc_path();
3184 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3188 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3189 struct btrfs_extent_item);
3190 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3191 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3192 struct btrfs_extent_ref);
3194 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3195 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3196 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3197 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3199 btrfs_mark_buffer_dirty(path->nodes[0]);
3201 trans->alloc_exclude_start = 0;
3202 trans->alloc_exclude_nr = 0;
3203 btrfs_free_path(path);
3204 finish_current_insert(trans, extent_root, 0);
3205 pending_ret = del_pending_extents(trans, extent_root, 0);
3215 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
3217 printk("update block group failed for %Lu %Lu\n",
3218 ins->objectid, ins->offset);
3225 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3226 struct btrfs_root *root, u64 parent,
3227 u64 root_objectid, u64 ref_generation,
3228 u64 owner, struct btrfs_key *ins)
3232 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3234 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3235 ref_generation, owner, ins);
3236 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3241 * this is used by the tree logging recovery code. It records that
3242 * an extent has been allocated and makes sure to clear the free
3243 * space cache bits as well
3245 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3246 struct btrfs_root *root, u64 parent,
3247 u64 root_objectid, u64 ref_generation,
3248 u64 owner, struct btrfs_key *ins)
3251 struct btrfs_block_group_cache *block_group;
3253 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3254 mutex_lock(&block_group->alloc_mutex);
3255 cache_block_group(root, block_group);
3257 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
3259 mutex_unlock(&block_group->alloc_mutex);
3261 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3262 ref_generation, owner, ins);
3267 * finds a free extent and does all the dirty work required for allocation
3268 * returns the key for the extent through ins, and a tree buffer for
3269 * the first block of the extent through buf.
3271 * returns 0 if everything worked, non-zero otherwise.
3273 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
3275 u64 num_bytes, u64 parent, u64 min_alloc_size,
3276 u64 root_objectid, u64 ref_generation,
3277 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3278 u64 search_end, struct btrfs_key *ins, u64 data)
3282 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3283 min_alloc_size, empty_size, hint_byte,
3284 search_end, ins, data);
3286 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3287 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3288 root_objectid, ref_generation,
3289 owner_objectid, ins);
3293 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3298 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3299 struct btrfs_root *root,
3300 u64 bytenr, u32 blocksize)
3302 struct extent_buffer *buf;
3304 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3306 return ERR_PTR(-ENOMEM);
3307 btrfs_set_header_generation(buf, trans->transid);
3308 btrfs_tree_lock(buf);
3309 clean_tree_block(trans, root, buf);
3310 btrfs_set_buffer_uptodate(buf);
3311 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3312 set_extent_dirty(&root->dirty_log_pages, buf->start,
3313 buf->start + buf->len - 1, GFP_NOFS);
3315 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3316 buf->start + buf->len - 1, GFP_NOFS);
3318 trans->blocks_used++;
3323 * helper function to allocate a block for a given tree
3324 * returns the tree buffer or NULL.
3326 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root,
3328 u32 blocksize, u64 parent,
3335 struct btrfs_key ins;
3337 struct extent_buffer *buf;
3339 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3340 root_objectid, ref_generation, level,
3341 empty_size, hint, (u64)-1, &ins, 0);
3344 return ERR_PTR(ret);
3347 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3351 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3352 struct btrfs_root *root, struct extent_buffer *leaf)
3355 u64 leaf_generation;
3356 struct btrfs_key key;
3357 struct btrfs_file_extent_item *fi;
3362 BUG_ON(!btrfs_is_leaf(leaf));
3363 nritems = btrfs_header_nritems(leaf);
3364 leaf_owner = btrfs_header_owner(leaf);
3365 leaf_generation = btrfs_header_generation(leaf);
3367 for (i = 0; i < nritems; i++) {
3371 btrfs_item_key_to_cpu(leaf, &key, i);
3372 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3374 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3375 if (btrfs_file_extent_type(leaf, fi) ==
3376 BTRFS_FILE_EXTENT_INLINE)
3379 * FIXME make sure to insert a trans record that
3380 * repeats the snapshot del on crash
3382 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3383 if (disk_bytenr == 0)
3386 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3387 btrfs_file_extent_disk_num_bytes(leaf, fi),
3388 leaf->start, leaf_owner, leaf_generation,
3392 atomic_inc(&root->fs_info->throttle_gen);
3393 wake_up(&root->fs_info->transaction_throttle);
3399 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3400 struct btrfs_root *root,
3401 struct btrfs_leaf_ref *ref)
3405 struct btrfs_extent_info *info = ref->extents;
3407 for (i = 0; i < ref->nritems; i++) {
3408 ret = __btrfs_free_extent(trans, root, info->bytenr,
3409 info->num_bytes, ref->bytenr,
3410 ref->owner, ref->generation,
3413 atomic_inc(&root->fs_info->throttle_gen);
3414 wake_up(&root->fs_info->transaction_throttle);
3424 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
3429 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3432 #if 0 // some debugging code in case we see problems here
3433 /* if the refs count is one, it won't get increased again. But
3434 * if the ref count is > 1, someone may be decreasing it at
3435 * the same time we are.
3438 struct extent_buffer *eb = NULL;
3439 eb = btrfs_find_create_tree_block(root, start, len);
3441 btrfs_tree_lock(eb);
3443 mutex_lock(&root->fs_info->alloc_mutex);
3444 ret = lookup_extent_ref(NULL, root, start, len, refs);
3446 mutex_unlock(&root->fs_info->alloc_mutex);
3449 btrfs_tree_unlock(eb);
3450 free_extent_buffer(eb);
3453 printk("block %llu went down to one during drop_snap\n",
3454 (unsigned long long)start);
3465 * helper function for drop_snapshot, this walks down the tree dropping ref
3466 * counts as it goes.
3468 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3469 struct btrfs_root *root,
3470 struct btrfs_path *path, int *level)
3476 struct extent_buffer *next;
3477 struct extent_buffer *cur;
3478 struct extent_buffer *parent;
3479 struct btrfs_leaf_ref *ref;
3484 WARN_ON(*level < 0);
3485 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3486 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3487 path->nodes[*level]->len, &refs);
3493 * walk down to the last node level and free all the leaves
3495 while(*level >= 0) {
3496 WARN_ON(*level < 0);
3497 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3498 cur = path->nodes[*level];
3500 if (btrfs_header_level(cur) != *level)
3503 if (path->slots[*level] >=
3504 btrfs_header_nritems(cur))
3507 ret = btrfs_drop_leaf_ref(trans, root, cur);
3511 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3512 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3513 blocksize = btrfs_level_size(root, *level - 1);
3515 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3518 parent = path->nodes[*level];
3519 root_owner = btrfs_header_owner(parent);
3520 root_gen = btrfs_header_generation(parent);
3521 path->slots[*level]++;
3523 ret = __btrfs_free_extent(trans, root, bytenr,
3524 blocksize, parent->start,
3525 root_owner, root_gen,
3529 atomic_inc(&root->fs_info->throttle_gen);
3530 wake_up(&root->fs_info->transaction_throttle);
3536 * at this point, we have a single ref, and since the
3537 * only place referencing this extent is a dead root
3538 * the reference count should never go higher.
3539 * So, we don't need to check it again
3542 ref = btrfs_lookup_leaf_ref(root, bytenr);
3543 if (ref && ref->generation != ptr_gen) {
3544 btrfs_free_leaf_ref(root, ref);
3548 ret = cache_drop_leaf_ref(trans, root, ref);
3550 btrfs_remove_leaf_ref(root, ref);
3551 btrfs_free_leaf_ref(root, ref);
3555 if (printk_ratelimit()) {
3556 printk("leaf ref miss for bytenr %llu\n",
3557 (unsigned long long)bytenr);
3560 next = btrfs_find_tree_block(root, bytenr, blocksize);
3561 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3562 free_extent_buffer(next);
3564 next = read_tree_block(root, bytenr, blocksize,
3569 * this is a debugging check and can go away
3570 * the ref should never go all the way down to 1
3573 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3579 WARN_ON(*level <= 0);
3580 if (path->nodes[*level-1])
3581 free_extent_buffer(path->nodes[*level-1]);
3582 path->nodes[*level-1] = next;
3583 *level = btrfs_header_level(next);
3584 path->slots[*level] = 0;
3588 WARN_ON(*level < 0);
3589 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3591 if (path->nodes[*level] == root->node) {
3592 parent = path->nodes[*level];
3593 bytenr = path->nodes[*level]->start;
3595 parent = path->nodes[*level + 1];
3596 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3599 blocksize = btrfs_level_size(root, *level);
3600 root_owner = btrfs_header_owner(parent);
3601 root_gen = btrfs_header_generation(parent);
3603 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3604 parent->start, root_owner, root_gen,
3606 free_extent_buffer(path->nodes[*level]);
3607 path->nodes[*level] = NULL;
3616 * helper function for drop_subtree, this function is similar to
3617 * walk_down_tree. The main difference is that it checks reference
3618 * counts while tree blocks are locked.
3620 static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3621 struct btrfs_root *root,
3622 struct btrfs_path *path, int *level)
3624 struct extent_buffer *next;
3625 struct extent_buffer *cur;
3626 struct extent_buffer *parent;
3633 cur = path->nodes[*level];
3634 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3640 while (*level >= 0) {
3641 cur = path->nodes[*level];
3643 ret = btrfs_drop_leaf_ref(trans, root, cur);
3645 clean_tree_block(trans, root, cur);
3648 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3649 clean_tree_block(trans, root, cur);
3653 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3654 blocksize = btrfs_level_size(root, *level - 1);
3655 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3657 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3658 btrfs_tree_lock(next);
3660 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3664 parent = path->nodes[*level];
3665 ret = btrfs_free_extent(trans, root, bytenr,
3666 blocksize, parent->start,
3667 btrfs_header_owner(parent),
3668 btrfs_header_generation(parent),
3671 path->slots[*level]++;
3672 btrfs_tree_unlock(next);
3673 free_extent_buffer(next);
3677 *level = btrfs_header_level(next);
3678 path->nodes[*level] = next;
3679 path->slots[*level] = 0;
3680 path->locks[*level] = 1;
3684 parent = path->nodes[*level + 1];
3685 bytenr = path->nodes[*level]->start;
3686 blocksize = path->nodes[*level]->len;
3688 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3689 parent->start, btrfs_header_owner(parent),
3690 btrfs_header_generation(parent), *level, 1);
3693 if (path->locks[*level]) {
3694 btrfs_tree_unlock(path->nodes[*level]);
3695 path->locks[*level] = 0;
3697 free_extent_buffer(path->nodes[*level]);
3698 path->nodes[*level] = NULL;
3705 * helper for dropping snapshots. This walks back up the tree in the path
3706 * to find the first node higher up where we haven't yet gone through
3709 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3710 struct btrfs_root *root,
3711 struct btrfs_path *path,
3712 int *level, int max_level)
3716 struct btrfs_root_item *root_item = &root->root_item;
3721 for (i = *level; i < max_level && path->nodes[i]; i++) {
3722 slot = path->slots[i];
3723 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3724 struct extent_buffer *node;
3725 struct btrfs_disk_key disk_key;
3726 node = path->nodes[i];
3729 WARN_ON(*level == 0);
3730 btrfs_node_key(node, &disk_key, path->slots[i]);
3731 memcpy(&root_item->drop_progress,
3732 &disk_key, sizeof(disk_key));
3733 root_item->drop_level = i;
3736 struct extent_buffer *parent;
3737 if (path->nodes[*level] == root->node)
3738 parent = path->nodes[*level];
3740 parent = path->nodes[*level + 1];
3742 root_owner = btrfs_header_owner(parent);
3743 root_gen = btrfs_header_generation(parent);
3745 clean_tree_block(trans, root, path->nodes[*level]);
3746 ret = btrfs_free_extent(trans, root,
3747 path->nodes[*level]->start,
3748 path->nodes[*level]->len,
3749 parent->start, root_owner,
3750 root_gen, *level, 1);
3752 if (path->locks[*level]) {
3753 btrfs_tree_unlock(path->nodes[*level]);
3754 path->locks[*level] = 0;
3756 free_extent_buffer(path->nodes[*level]);
3757 path->nodes[*level] = NULL;
3765 * drop the reference count on the tree rooted at 'snap'. This traverses
3766 * the tree freeing any blocks that have a ref count of zero after being
3769 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3775 struct btrfs_path *path;
3778 struct btrfs_root_item *root_item = &root->root_item;
3780 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3781 path = btrfs_alloc_path();
3784 level = btrfs_header_level(root->node);
3786 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3787 path->nodes[level] = root->node;
3788 extent_buffer_get(root->node);
3789 path->slots[level] = 0;
3791 struct btrfs_key key;
3792 struct btrfs_disk_key found_key;
3793 struct extent_buffer *node;
3795 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3796 level = root_item->drop_level;
3797 path->lowest_level = level;
3798 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3803 node = path->nodes[level];
3804 btrfs_node_key(node, &found_key, path->slots[level]);
3805 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3806 sizeof(found_key)));
3808 * unlock our path, this is safe because only this
3809 * function is allowed to delete this snapshot
3811 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3812 if (path->nodes[i] && path->locks[i]) {
3814 btrfs_tree_unlock(path->nodes[i]);
3819 wret = walk_down_tree(trans, root, path, &level);
3825 wret = walk_up_tree(trans, root, path, &level,
3831 if (trans->transaction->in_commit) {
3835 atomic_inc(&root->fs_info->throttle_gen);
3836 wake_up(&root->fs_info->transaction_throttle);
3838 for (i = 0; i <= orig_level; i++) {
3839 if (path->nodes[i]) {
3840 free_extent_buffer(path->nodes[i]);
3841 path->nodes[i] = NULL;
3845 btrfs_free_path(path);
3849 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3850 struct btrfs_root *root,
3851 struct extent_buffer *node,
3852 struct extent_buffer *parent)
3854 struct btrfs_path *path;
3860 path = btrfs_alloc_path();
3863 BUG_ON(!btrfs_tree_locked(parent));
3864 parent_level = btrfs_header_level(parent);
3865 extent_buffer_get(parent);
3866 path->nodes[parent_level] = parent;
3867 path->slots[parent_level] = btrfs_header_nritems(parent);
3869 BUG_ON(!btrfs_tree_locked(node));
3870 level = btrfs_header_level(node);
3871 extent_buffer_get(node);
3872 path->nodes[level] = node;
3873 path->slots[level] = 0;
3876 wret = walk_down_subtree(trans, root, path, &level);
3882 wret = walk_up_tree(trans, root, path, &level, parent_level);
3889 btrfs_free_path(path);
3893 static unsigned long calc_ra(unsigned long start, unsigned long last,
3896 return min(last, start + nr - 1);
3899 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3904 unsigned long first_index;
3905 unsigned long last_index;
3908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3909 struct file_ra_state *ra;
3910 struct btrfs_ordered_extent *ordered;
3911 unsigned int total_read = 0;
3912 unsigned int total_dirty = 0;
3915 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3917 mutex_lock(&inode->i_mutex);
3918 first_index = start >> PAGE_CACHE_SHIFT;
3919 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3921 /* make sure the dirty trick played by the caller work */
3922 ret = invalidate_inode_pages2_range(inode->i_mapping,
3923 first_index, last_index);
3927 file_ra_state_init(ra, inode->i_mapping);
3929 for (i = first_index ; i <= last_index; i++) {
3930 if (total_read % ra->ra_pages == 0) {
3931 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3932 calc_ra(i, last_index, ra->ra_pages));
3936 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3938 page = grab_cache_page(inode->i_mapping, i);
3943 if (!PageUptodate(page)) {
3944 btrfs_readpage(NULL, page);
3946 if (!PageUptodate(page)) {
3948 page_cache_release(page);
3953 wait_on_page_writeback(page);
3955 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3956 page_end = page_start + PAGE_CACHE_SIZE - 1;
3957 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3959 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3961 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3963 page_cache_release(page);
3964 btrfs_start_ordered_extent(inode, ordered, 1);
3965 btrfs_put_ordered_extent(ordered);
3968 set_page_extent_mapped(page);
3970 btrfs_set_extent_delalloc(inode, page_start, page_end);
3971 if (i == first_index)
3972 set_extent_bits(io_tree, page_start, page_end,
3973 EXTENT_BOUNDARY, GFP_NOFS);
3975 set_page_dirty(page);
3978 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3980 page_cache_release(page);
3985 mutex_unlock(&inode->i_mutex);
3986 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3990 static int noinline relocate_data_extent(struct inode *reloc_inode,
3991 struct btrfs_key *extent_key,
3994 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3995 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3996 struct extent_map *em;
3997 u64 start = extent_key->objectid - offset;
3998 u64 end = start + extent_key->offset - 1;
4000 em = alloc_extent_map(GFP_NOFS);
4001 BUG_ON(!em || IS_ERR(em));
4004 em->len = extent_key->offset;
4005 em->block_len = extent_key->offset;
4006 em->block_start = extent_key->objectid;
4007 em->bdev = root->fs_info->fs_devices->latest_bdev;
4008 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4010 /* setup extent map to cheat btrfs_readpage */
4011 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4014 spin_lock(&em_tree->lock);
4015 ret = add_extent_mapping(em_tree, em);
4016 spin_unlock(&em_tree->lock);
4017 if (ret != -EEXIST) {
4018 free_extent_map(em);
4021 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4023 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4025 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4028 struct btrfs_ref_path {
4030 u64 nodes[BTRFS_MAX_LEVEL];
4032 u64 root_generation;
4039 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4040 u64 new_nodes[BTRFS_MAX_LEVEL];
4043 struct disk_extent {
4054 static int is_cowonly_root(u64 root_objectid)
4056 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4057 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4058 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4059 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4060 root_objectid == BTRFS_TREE_LOG_OBJECTID)
4065 static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *extent_root,
4067 struct btrfs_ref_path *ref_path,
4070 struct extent_buffer *leaf;
4071 struct btrfs_path *path;
4072 struct btrfs_extent_ref *ref;
4073 struct btrfs_key key;
4074 struct btrfs_key found_key;
4080 path = btrfs_alloc_path();
4085 ref_path->lowest_level = -1;
4086 ref_path->current_level = -1;
4087 ref_path->shared_level = -1;
4091 level = ref_path->current_level - 1;
4092 while (level >= -1) {
4094 if (level < ref_path->lowest_level)
4098 bytenr = ref_path->nodes[level];
4100 bytenr = ref_path->extent_start;
4102 BUG_ON(bytenr == 0);
4104 parent = ref_path->nodes[level + 1];
4105 ref_path->nodes[level + 1] = 0;
4106 ref_path->current_level = level;
4107 BUG_ON(parent == 0);
4109 key.objectid = bytenr;
4110 key.offset = parent + 1;
4111 key.type = BTRFS_EXTENT_REF_KEY;
4113 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4118 leaf = path->nodes[0];
4119 nritems = btrfs_header_nritems(leaf);
4120 if (path->slots[0] >= nritems) {
4121 ret = btrfs_next_leaf(extent_root, path);
4126 leaf = path->nodes[0];
4129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4130 if (found_key.objectid == bytenr &&
4131 found_key.type == BTRFS_EXTENT_REF_KEY) {
4132 if (level < ref_path->shared_level)
4133 ref_path->shared_level = level;
4138 btrfs_release_path(extent_root, path);
4141 /* reached lowest level */
4145 level = ref_path->current_level;
4146 while (level < BTRFS_MAX_LEVEL - 1) {
4149 bytenr = ref_path->nodes[level];
4151 bytenr = ref_path->extent_start;
4153 BUG_ON(bytenr == 0);
4155 key.objectid = bytenr;
4157 key.type = BTRFS_EXTENT_REF_KEY;
4159 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4163 leaf = path->nodes[0];
4164 nritems = btrfs_header_nritems(leaf);
4165 if (path->slots[0] >= nritems) {
4166 ret = btrfs_next_leaf(extent_root, path);
4170 /* the extent was freed by someone */
4171 if (ref_path->lowest_level == level)
4173 btrfs_release_path(extent_root, path);
4176 leaf = path->nodes[0];
4179 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4180 if (found_key.objectid != bytenr ||
4181 found_key.type != BTRFS_EXTENT_REF_KEY) {
4182 /* the extent was freed by someone */
4183 if (ref_path->lowest_level == level) {
4187 btrfs_release_path(extent_root, path);
4191 ref = btrfs_item_ptr(leaf, path->slots[0],
4192 struct btrfs_extent_ref);
4193 ref_objectid = btrfs_ref_objectid(leaf, ref);
4194 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4196 level = (int)ref_objectid;
4197 BUG_ON(level >= BTRFS_MAX_LEVEL);
4198 ref_path->lowest_level = level;
4199 ref_path->current_level = level;
4200 ref_path->nodes[level] = bytenr;
4202 WARN_ON(ref_objectid != level);
4205 WARN_ON(level != -1);
4209 if (ref_path->lowest_level == level) {
4210 ref_path->owner_objectid = ref_objectid;
4211 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4215 * the block is tree root or the block isn't in reference
4218 if (found_key.objectid == found_key.offset ||
4219 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4220 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4221 ref_path->root_generation =
4222 btrfs_ref_generation(leaf, ref);
4224 /* special reference from the tree log */
4225 ref_path->nodes[0] = found_key.offset;
4226 ref_path->current_level = 0;
4233 BUG_ON(ref_path->nodes[level] != 0);
4234 ref_path->nodes[level] = found_key.offset;
4235 ref_path->current_level = level;
4238 * the reference was created in the running transaction,
4239 * no need to continue walking up.
4241 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4242 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4243 ref_path->root_generation =
4244 btrfs_ref_generation(leaf, ref);
4249 btrfs_release_path(extent_root, path);
4252 /* reached max tree level, but no tree root found. */
4255 btrfs_free_path(path);
4259 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4260 struct btrfs_root *extent_root,
4261 struct btrfs_ref_path *ref_path,
4264 memset(ref_path, 0, sizeof(*ref_path));
4265 ref_path->extent_start = extent_start;
4267 return __next_ref_path(trans, extent_root, ref_path, 1);
4270 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4271 struct btrfs_root *extent_root,
4272 struct btrfs_ref_path *ref_path)
4274 return __next_ref_path(trans, extent_root, ref_path, 0);
4277 static int noinline get_new_locations(struct inode *reloc_inode,
4278 struct btrfs_key *extent_key,
4279 u64 offset, int no_fragment,
4280 struct disk_extent **extents,
4283 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4284 struct btrfs_path *path;
4285 struct btrfs_file_extent_item *fi;
4286 struct extent_buffer *leaf;
4287 struct disk_extent *exts = *extents;
4288 struct btrfs_key found_key;
4293 int max = *nr_extents;
4296 WARN_ON(!no_fragment && *extents);
4299 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4304 path = btrfs_alloc_path();
4307 cur_pos = extent_key->objectid - offset;
4308 last_byte = extent_key->objectid + extent_key->offset;
4309 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4319 leaf = path->nodes[0];
4320 nritems = btrfs_header_nritems(leaf);
4321 if (path->slots[0] >= nritems) {
4322 ret = btrfs_next_leaf(root, path);
4327 leaf = path->nodes[0];
4330 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4331 if (found_key.offset != cur_pos ||
4332 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4333 found_key.objectid != reloc_inode->i_ino)
4336 fi = btrfs_item_ptr(leaf, path->slots[0],
4337 struct btrfs_file_extent_item);
4338 if (btrfs_file_extent_type(leaf, fi) !=
4339 BTRFS_FILE_EXTENT_REG ||
4340 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4344 struct disk_extent *old = exts;
4346 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4347 memcpy(exts, old, sizeof(*exts) * nr);
4348 if (old != *extents)
4352 exts[nr].disk_bytenr =
4353 btrfs_file_extent_disk_bytenr(leaf, fi);
4354 exts[nr].disk_num_bytes =
4355 btrfs_file_extent_disk_num_bytes(leaf, fi);
4356 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4357 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4358 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4359 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4360 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4361 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4363 BUG_ON(exts[nr].offset > 0);
4364 BUG_ON(exts[nr].compression || exts[nr].encryption);
4365 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4367 cur_pos += exts[nr].num_bytes;
4370 if (cur_pos + offset >= last_byte)
4380 WARN_ON(cur_pos + offset > last_byte);
4381 if (cur_pos + offset < last_byte) {
4387 btrfs_free_path(path);
4389 if (exts != *extents)
4398 static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4399 struct btrfs_root *root,
4400 struct btrfs_path *path,
4401 struct btrfs_key *extent_key,
4402 struct btrfs_key *leaf_key,
4403 struct btrfs_ref_path *ref_path,
4404 struct disk_extent *new_extents,
4407 struct extent_buffer *leaf;
4408 struct btrfs_file_extent_item *fi;
4409 struct inode *inode = NULL;
4410 struct btrfs_key key;
4418 int extent_locked = 0;
4422 memcpy(&key, leaf_key, sizeof(key));
4423 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4424 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4425 if (key.objectid < ref_path->owner_objectid ||
4426 (key.objectid == ref_path->owner_objectid &&
4427 key.type < BTRFS_EXTENT_DATA_KEY)) {
4428 key.objectid = ref_path->owner_objectid;
4429 key.type = BTRFS_EXTENT_DATA_KEY;
4435 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4439 leaf = path->nodes[0];
4440 nritems = btrfs_header_nritems(leaf);
4442 if (extent_locked && ret > 0) {
4444 * the file extent item was modified by someone
4445 * before the extent got locked.
4447 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4448 lock_end, GFP_NOFS);
4452 if (path->slots[0] >= nritems) {
4453 if (++nr_scaned > 2)
4456 BUG_ON(extent_locked);
4457 ret = btrfs_next_leaf(root, path);
4462 leaf = path->nodes[0];
4463 nritems = btrfs_header_nritems(leaf);
4466 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4468 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4469 if ((key.objectid > ref_path->owner_objectid) ||
4470 (key.objectid == ref_path->owner_objectid &&
4471 key.type > BTRFS_EXTENT_DATA_KEY) ||
4472 (key.offset >= first_pos + extent_key->offset))
4476 if (inode && key.objectid != inode->i_ino) {
4477 BUG_ON(extent_locked);
4478 btrfs_release_path(root, path);
4479 mutex_unlock(&inode->i_mutex);
4485 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4490 fi = btrfs_item_ptr(leaf, path->slots[0],
4491 struct btrfs_file_extent_item);
4492 extent_type = btrfs_file_extent_type(leaf, fi);
4493 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4494 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4495 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4496 extent_key->objectid)) {
4502 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4503 ext_offset = btrfs_file_extent_offset(leaf, fi);
4505 if (first_pos > key.offset - ext_offset)
4506 first_pos = key.offset - ext_offset;
4508 if (!extent_locked) {
4509 lock_start = key.offset;
4510 lock_end = lock_start + num_bytes - 1;
4512 if (lock_start > key.offset ||
4513 lock_end + 1 < key.offset + num_bytes) {
4514 unlock_extent(&BTRFS_I(inode)->io_tree,
4515 lock_start, lock_end, GFP_NOFS);
4521 btrfs_release_path(root, path);
4523 inode = btrfs_iget_locked(root->fs_info->sb,
4524 key.objectid, root);
4525 if (inode->i_state & I_NEW) {
4526 BTRFS_I(inode)->root = root;
4527 BTRFS_I(inode)->location.objectid =
4529 BTRFS_I(inode)->location.type =
4530 BTRFS_INODE_ITEM_KEY;
4531 BTRFS_I(inode)->location.offset = 0;
4532 btrfs_read_locked_inode(inode);
4533 unlock_new_inode(inode);
4536 * some code call btrfs_commit_transaction while
4537 * holding the i_mutex, so we can't use mutex_lock
4540 if (is_bad_inode(inode) ||
4541 !mutex_trylock(&inode->i_mutex)) {
4544 key.offset = (u64)-1;
4549 if (!extent_locked) {
4550 struct btrfs_ordered_extent *ordered;
4552 btrfs_release_path(root, path);
4554 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4555 lock_end, GFP_NOFS);
4556 ordered = btrfs_lookup_first_ordered_extent(inode,
4559 ordered->file_offset <= lock_end &&
4560 ordered->file_offset + ordered->len > lock_start) {
4561 unlock_extent(&BTRFS_I(inode)->io_tree,
4562 lock_start, lock_end, GFP_NOFS);
4563 btrfs_start_ordered_extent(inode, ordered, 1);
4564 btrfs_put_ordered_extent(ordered);
4565 key.offset += num_bytes;
4569 btrfs_put_ordered_extent(ordered);
4575 if (nr_extents == 1) {
4576 /* update extent pointer in place */
4577 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4578 new_extents[0].disk_bytenr);
4579 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4580 new_extents[0].disk_num_bytes);
4581 btrfs_mark_buffer_dirty(leaf);
4583 btrfs_drop_extent_cache(inode, key.offset,
4584 key.offset + num_bytes - 1, 0);
4586 ret = btrfs_inc_extent_ref(trans, root,
4587 new_extents[0].disk_bytenr,
4588 new_extents[0].disk_num_bytes,
4590 root->root_key.objectid,
4595 ret = btrfs_free_extent(trans, root,
4596 extent_key->objectid,
4599 btrfs_header_owner(leaf),
4600 btrfs_header_generation(leaf),
4604 btrfs_release_path(root, path);
4605 key.offset += num_bytes;
4613 * drop old extent pointer at first, then insert the
4614 * new pointers one bye one
4616 btrfs_release_path(root, path);
4617 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4618 key.offset + num_bytes,
4619 key.offset, &alloc_hint);
4622 for (i = 0; i < nr_extents; i++) {
4623 if (ext_offset >= new_extents[i].num_bytes) {
4624 ext_offset -= new_extents[i].num_bytes;
4627 extent_len = min(new_extents[i].num_bytes -
4628 ext_offset, num_bytes);
4630 ret = btrfs_insert_empty_item(trans, root,
4635 leaf = path->nodes[0];
4636 fi = btrfs_item_ptr(leaf, path->slots[0],
4637 struct btrfs_file_extent_item);
4638 btrfs_set_file_extent_generation(leaf, fi,
4640 btrfs_set_file_extent_type(leaf, fi,
4641 BTRFS_FILE_EXTENT_REG);
4642 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4643 new_extents[i].disk_bytenr);
4644 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4645 new_extents[i].disk_num_bytes);
4646 btrfs_set_file_extent_ram_bytes(leaf, fi,
4647 new_extents[i].ram_bytes);
4649 btrfs_set_file_extent_compression(leaf, fi,
4650 new_extents[i].compression);
4651 btrfs_set_file_extent_encryption(leaf, fi,
4652 new_extents[i].encryption);
4653 btrfs_set_file_extent_other_encoding(leaf, fi,
4654 new_extents[i].other_encoding);
4656 btrfs_set_file_extent_num_bytes(leaf, fi,
4658 ext_offset += new_extents[i].offset;
4659 btrfs_set_file_extent_offset(leaf, fi,
4661 btrfs_mark_buffer_dirty(leaf);
4663 btrfs_drop_extent_cache(inode, key.offset,
4664 key.offset + extent_len - 1, 0);
4666 ret = btrfs_inc_extent_ref(trans, root,
4667 new_extents[i].disk_bytenr,
4668 new_extents[i].disk_num_bytes,
4670 root->root_key.objectid,
4671 trans->transid, key.objectid);
4673 btrfs_release_path(root, path);
4675 inode_add_bytes(inode, extent_len);
4678 num_bytes -= extent_len;
4679 key.offset += extent_len;
4684 BUG_ON(i >= nr_extents);
4688 if (extent_locked) {
4689 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4690 lock_end, GFP_NOFS);
4694 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4695 key.offset >= first_pos + extent_key->offset)
4702 btrfs_release_path(root, path);
4704 mutex_unlock(&inode->i_mutex);
4705 if (extent_locked) {
4706 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4707 lock_end, GFP_NOFS);
4714 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4715 struct btrfs_root *root,
4716 struct extent_buffer *buf, u64 orig_start)
4721 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4722 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4724 level = btrfs_header_level(buf);
4726 struct btrfs_leaf_ref *ref;
4727 struct btrfs_leaf_ref *orig_ref;
4729 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4733 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4735 btrfs_free_leaf_ref(root, orig_ref);
4739 ref->nritems = orig_ref->nritems;
4740 memcpy(ref->extents, orig_ref->extents,
4741 sizeof(ref->extents[0]) * ref->nritems);
4743 btrfs_free_leaf_ref(root, orig_ref);
4745 ref->root_gen = trans->transid;
4746 ref->bytenr = buf->start;
4747 ref->owner = btrfs_header_owner(buf);
4748 ref->generation = btrfs_header_generation(buf);
4749 ret = btrfs_add_leaf_ref(root, ref, 0);
4751 btrfs_free_leaf_ref(root, ref);
4756 static int noinline invalidate_extent_cache(struct btrfs_root *root,
4757 struct extent_buffer *leaf,
4758 struct btrfs_block_group_cache *group,
4759 struct btrfs_root *target_root)
4761 struct btrfs_key key;
4762 struct inode *inode = NULL;
4763 struct btrfs_file_extent_item *fi;
4765 u64 skip_objectid = 0;
4769 nritems = btrfs_header_nritems(leaf);
4770 for (i = 0; i < nritems; i++) {
4771 btrfs_item_key_to_cpu(leaf, &key, i);
4772 if (key.objectid == skip_objectid ||
4773 key.type != BTRFS_EXTENT_DATA_KEY)
4775 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4776 if (btrfs_file_extent_type(leaf, fi) ==
4777 BTRFS_FILE_EXTENT_INLINE)
4779 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4781 if (!inode || inode->i_ino != key.objectid) {
4783 inode = btrfs_ilookup(target_root->fs_info->sb,
4784 key.objectid, target_root, 1);
4787 skip_objectid = key.objectid;
4790 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4792 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4793 key.offset + num_bytes - 1, GFP_NOFS);
4794 btrfs_drop_extent_cache(inode, key.offset,
4795 key.offset + num_bytes - 1, 1);
4796 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4797 key.offset + num_bytes - 1, GFP_NOFS);
4804 static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4805 struct btrfs_root *root,
4806 struct extent_buffer *leaf,
4807 struct btrfs_block_group_cache *group,
4808 struct inode *reloc_inode)
4810 struct btrfs_key key;
4811 struct btrfs_key extent_key;
4812 struct btrfs_file_extent_item *fi;
4813 struct btrfs_leaf_ref *ref;
4814 struct disk_extent *new_extent;
4823 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4824 BUG_ON(!new_extent);
4826 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4830 nritems = btrfs_header_nritems(leaf);
4831 for (i = 0; i < nritems; i++) {
4832 btrfs_item_key_to_cpu(leaf, &key, i);
4833 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4835 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4836 if (btrfs_file_extent_type(leaf, fi) ==
4837 BTRFS_FILE_EXTENT_INLINE)
4839 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4840 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4845 if (bytenr >= group->key.objectid + group->key.offset ||
4846 bytenr + num_bytes <= group->key.objectid)
4849 extent_key.objectid = bytenr;
4850 extent_key.offset = num_bytes;
4851 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4853 ret = get_new_locations(reloc_inode, &extent_key,
4854 group->key.objectid, 1,
4855 &new_extent, &nr_extent);
4860 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4861 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4862 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4863 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4865 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4866 new_extent->disk_bytenr);
4867 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4868 new_extent->disk_num_bytes);
4869 btrfs_mark_buffer_dirty(leaf);
4871 ret = btrfs_inc_extent_ref(trans, root,
4872 new_extent->disk_bytenr,
4873 new_extent->disk_num_bytes,
4875 root->root_key.objectid,
4876 trans->transid, key.objectid);
4878 ret = btrfs_free_extent(trans, root,
4879 bytenr, num_bytes, leaf->start,
4880 btrfs_header_owner(leaf),
4881 btrfs_header_generation(leaf),
4887 BUG_ON(ext_index + 1 != ref->nritems);
4888 btrfs_free_leaf_ref(root, ref);
4892 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4893 struct btrfs_root *root)
4895 struct btrfs_root *reloc_root;
4898 if (root->reloc_root) {
4899 reloc_root = root->reloc_root;
4900 root->reloc_root = NULL;
4901 list_add(&reloc_root->dead_list,
4902 &root->fs_info->dead_reloc_roots);
4904 btrfs_set_root_bytenr(&reloc_root->root_item,
4905 reloc_root->node->start);
4906 btrfs_set_root_level(&root->root_item,
4907 btrfs_header_level(reloc_root->node));
4908 memset(&reloc_root->root_item.drop_progress, 0,
4909 sizeof(struct btrfs_disk_key));
4910 reloc_root->root_item.drop_level = 0;
4912 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4913 &reloc_root->root_key,
4914 &reloc_root->root_item);
4920 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4922 struct btrfs_trans_handle *trans;
4923 struct btrfs_root *reloc_root;
4924 struct btrfs_root *prev_root = NULL;
4925 struct list_head dead_roots;
4929 INIT_LIST_HEAD(&dead_roots);
4930 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4932 while (!list_empty(&dead_roots)) {
4933 reloc_root = list_entry(dead_roots.prev,
4934 struct btrfs_root, dead_list);
4935 list_del_init(&reloc_root->dead_list);
4937 BUG_ON(reloc_root->commit_root != NULL);
4939 trans = btrfs_join_transaction(root, 1);
4942 mutex_lock(&root->fs_info->drop_mutex);
4943 ret = btrfs_drop_snapshot(trans, reloc_root);
4946 mutex_unlock(&root->fs_info->drop_mutex);
4948 nr = trans->blocks_used;
4949 ret = btrfs_end_transaction(trans, root);
4951 btrfs_btree_balance_dirty(root, nr);
4954 free_extent_buffer(reloc_root->node);
4956 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4957 &reloc_root->root_key);
4959 mutex_unlock(&root->fs_info->drop_mutex);
4961 nr = trans->blocks_used;
4962 ret = btrfs_end_transaction(trans, root);
4964 btrfs_btree_balance_dirty(root, nr);
4967 prev_root = reloc_root;
4970 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4976 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4978 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4982 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4984 struct btrfs_root *reloc_root;
4985 struct btrfs_trans_handle *trans;
4986 struct btrfs_key location;
4990 mutex_lock(&root->fs_info->tree_reloc_mutex);
4991 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4993 found = !list_empty(&root->fs_info->dead_reloc_roots);
4994 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4997 trans = btrfs_start_transaction(root, 1);
4999 ret = btrfs_commit_transaction(trans, root);
5003 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5004 location.offset = (u64)-1;
5005 location.type = BTRFS_ROOT_ITEM_KEY;
5007 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5008 BUG_ON(!reloc_root);
5009 btrfs_orphan_cleanup(reloc_root);
5013 static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5014 struct btrfs_root *root)
5016 struct btrfs_root *reloc_root;
5017 struct extent_buffer *eb;
5018 struct btrfs_root_item *root_item;
5019 struct btrfs_key root_key;
5022 BUG_ON(!root->ref_cows);
5023 if (root->reloc_root)
5026 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5029 ret = btrfs_copy_root(trans, root, root->commit_root,
5030 &eb, BTRFS_TREE_RELOC_OBJECTID);
5033 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5034 root_key.offset = root->root_key.objectid;
5035 root_key.type = BTRFS_ROOT_ITEM_KEY;
5037 memcpy(root_item, &root->root_item, sizeof(root_item));
5038 btrfs_set_root_refs(root_item, 0);
5039 btrfs_set_root_bytenr(root_item, eb->start);
5040 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5041 btrfs_set_root_generation(root_item, trans->transid);
5043 btrfs_tree_unlock(eb);
5044 free_extent_buffer(eb);
5046 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5047 &root_key, root_item);
5051 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5053 BUG_ON(!reloc_root);
5054 reloc_root->last_trans = trans->transid;
5055 reloc_root->commit_root = NULL;
5056 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5058 root->reloc_root = reloc_root;
5063 * Core function of space balance.
5065 * The idea is using reloc trees to relocate tree blocks in reference
5066 * counted roots. There is one reloc tree for each subvol, and all
5067 * reloc trees share same root key objectid. Reloc trees are snapshots
5068 * of the latest committed roots of subvols (root->commit_root).
5070 * To relocate a tree block referenced by a subvol, there are two steps.
5071 * COW the block through subvol's reloc tree, then update block pointer
5072 * in the subvol to point to the new block. Since all reloc trees share
5073 * same root key objectid, doing special handing for tree blocks owned
5074 * by them is easy. Once a tree block has been COWed in one reloc tree,
5075 * we can use the resulting new block directly when the same block is
5076 * required to COW again through other reloc trees. By this way, relocated
5077 * tree blocks are shared between reloc trees, so they are also shared
5080 static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5081 struct btrfs_root *root,
5082 struct btrfs_path *path,
5083 struct btrfs_key *first_key,
5084 struct btrfs_ref_path *ref_path,
5085 struct btrfs_block_group_cache *group,
5086 struct inode *reloc_inode)
5088 struct btrfs_root *reloc_root;
5089 struct extent_buffer *eb = NULL;
5090 struct btrfs_key *keys;
5094 int lowest_level = 0;
5097 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5098 lowest_level = ref_path->owner_objectid;
5100 if (!root->ref_cows) {
5101 path->lowest_level = lowest_level;
5102 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5104 path->lowest_level = 0;
5105 btrfs_release_path(root, path);
5109 mutex_lock(&root->fs_info->tree_reloc_mutex);
5110 ret = init_reloc_tree(trans, root);
5112 reloc_root = root->reloc_root;
5114 shared_level = ref_path->shared_level;
5115 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5117 keys = ref_path->node_keys;
5118 nodes = ref_path->new_nodes;
5119 memset(&keys[shared_level + 1], 0,
5120 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5121 memset(&nodes[shared_level + 1], 0,
5122 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5124 if (nodes[lowest_level] == 0) {
5125 path->lowest_level = lowest_level;
5126 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5129 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5130 eb = path->nodes[level];
5131 if (!eb || eb == reloc_root->node)
5133 nodes[level] = eb->start;
5135 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5137 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5139 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5140 eb = path->nodes[0];
5141 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5142 group, reloc_inode);
5145 btrfs_release_path(reloc_root, path);
5147 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5153 * replace tree blocks in the fs tree with tree blocks in
5156 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5159 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5160 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5163 extent_buffer_get(path->nodes[0]);
5164 eb = path->nodes[0];
5165 btrfs_release_path(reloc_root, path);
5166 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5168 free_extent_buffer(eb);
5171 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5172 path->lowest_level = 0;
5176 static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5177 struct btrfs_root *root,
5178 struct btrfs_path *path,
5179 struct btrfs_key *first_key,
5180 struct btrfs_ref_path *ref_path)
5184 ret = relocate_one_path(trans, root, path, first_key,
5185 ref_path, NULL, NULL);
5188 if (root == root->fs_info->extent_root)
5189 btrfs_extent_post_op(trans, root);
5194 static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5195 struct btrfs_root *extent_root,
5196 struct btrfs_path *path,
5197 struct btrfs_key *extent_key)
5201 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5204 ret = btrfs_del_item(trans, extent_root, path);
5206 btrfs_release_path(extent_root, path);
5210 static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5211 struct btrfs_ref_path *ref_path)
5213 struct btrfs_key root_key;
5215 root_key.objectid = ref_path->root_objectid;
5216 root_key.type = BTRFS_ROOT_ITEM_KEY;
5217 if (is_cowonly_root(ref_path->root_objectid))
5218 root_key.offset = 0;
5220 root_key.offset = (u64)-1;
5222 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5225 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5226 struct btrfs_path *path,
5227 struct btrfs_key *extent_key,
5228 struct btrfs_block_group_cache *group,
5229 struct inode *reloc_inode, int pass)
5231 struct btrfs_trans_handle *trans;
5232 struct btrfs_root *found_root;
5233 struct btrfs_ref_path *ref_path = NULL;
5234 struct disk_extent *new_extents = NULL;
5239 struct btrfs_key first_key;
5243 trans = btrfs_start_transaction(extent_root, 1);
5246 if (extent_key->objectid == 0) {
5247 ret = del_extent_zero(trans, extent_root, path, extent_key);
5251 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5257 for (loops = 0; ; loops++) {
5259 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5260 extent_key->objectid);
5262 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5269 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5270 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5273 found_root = read_ref_root(extent_root->fs_info, ref_path);
5274 BUG_ON(!found_root);
5276 * for reference counted tree, only process reference paths
5277 * rooted at the latest committed root.
5279 if (found_root->ref_cows &&
5280 ref_path->root_generation != found_root->root_key.offset)
5283 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5286 * copy data extents to new locations
5288 u64 group_start = group->key.objectid;
5289 ret = relocate_data_extent(reloc_inode,
5298 level = ref_path->owner_objectid;
5301 if (prev_block != ref_path->nodes[level]) {
5302 struct extent_buffer *eb;
5303 u64 block_start = ref_path->nodes[level];
5304 u64 block_size = btrfs_level_size(found_root, level);
5306 eb = read_tree_block(found_root, block_start,
5308 btrfs_tree_lock(eb);
5309 BUG_ON(level != btrfs_header_level(eb));
5312 btrfs_item_key_to_cpu(eb, &first_key, 0);
5314 btrfs_node_key_to_cpu(eb, &first_key, 0);
5316 btrfs_tree_unlock(eb);
5317 free_extent_buffer(eb);
5318 prev_block = block_start;
5321 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5324 * use fallback method to process the remaining
5328 u64 group_start = group->key.objectid;
5329 new_extents = kmalloc(sizeof(*new_extents),
5332 ret = get_new_locations(reloc_inode,
5340 btrfs_record_root_in_trans(found_root);
5341 ret = replace_one_extent(trans, found_root,
5343 &first_key, ref_path,
5344 new_extents, nr_extents);
5350 btrfs_record_root_in_trans(found_root);
5351 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5352 ret = relocate_tree_block(trans, found_root, path,
5353 &first_key, ref_path);
5356 * try to update data extent references while
5357 * keeping metadata shared between snapshots.
5359 ret = relocate_one_path(trans, found_root, path,
5360 &first_key, ref_path,
5361 group, reloc_inode);
5368 btrfs_end_transaction(trans, extent_root);
5374 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5377 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5378 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5380 num_devices = root->fs_info->fs_devices->num_devices;
5381 if (num_devices == 1) {
5382 stripped |= BTRFS_BLOCK_GROUP_DUP;
5383 stripped = flags & ~stripped;
5385 /* turn raid0 into single device chunks */
5386 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5389 /* turn mirroring into duplication */
5390 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5391 BTRFS_BLOCK_GROUP_RAID10))
5392 return stripped | BTRFS_BLOCK_GROUP_DUP;
5395 /* they already had raid on here, just return */
5396 if (flags & stripped)
5399 stripped |= BTRFS_BLOCK_GROUP_DUP;
5400 stripped = flags & ~stripped;
5402 /* switch duplicated blocks with raid1 */
5403 if (flags & BTRFS_BLOCK_GROUP_DUP)
5404 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5406 /* turn single device chunks into raid0 */
5407 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5412 int __alloc_chunk_for_shrink(struct btrfs_root *root,
5413 struct btrfs_block_group_cache *shrink_block_group,
5416 struct btrfs_trans_handle *trans;
5417 u64 new_alloc_flags;
5420 spin_lock(&shrink_block_group->lock);
5421 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5422 spin_unlock(&shrink_block_group->lock);
5424 trans = btrfs_start_transaction(root, 1);
5425 spin_lock(&shrink_block_group->lock);
5427 new_alloc_flags = update_block_group_flags(root,
5428 shrink_block_group->flags);
5429 if (new_alloc_flags != shrink_block_group->flags) {
5431 btrfs_block_group_used(&shrink_block_group->item);
5433 calc = shrink_block_group->key.offset;
5435 spin_unlock(&shrink_block_group->lock);
5437 do_chunk_alloc(trans, root->fs_info->extent_root,
5438 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5440 btrfs_end_transaction(trans, root);
5442 spin_unlock(&shrink_block_group->lock);
5446 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5447 struct btrfs_root *root,
5448 u64 objectid, u64 size)
5450 struct btrfs_path *path;
5451 struct btrfs_inode_item *item;
5452 struct extent_buffer *leaf;
5455 path = btrfs_alloc_path();
5459 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5463 leaf = path->nodes[0];
5464 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5465 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5466 btrfs_set_inode_generation(leaf, item, 1);
5467 btrfs_set_inode_size(leaf, item, size);
5468 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5469 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
5470 BTRFS_INODE_NOCOMPRESS);
5471 btrfs_mark_buffer_dirty(leaf);
5472 btrfs_release_path(root, path);
5474 btrfs_free_path(path);
5478 static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5479 struct btrfs_block_group_cache *group)
5481 struct inode *inode = NULL;
5482 struct btrfs_trans_handle *trans;
5483 struct btrfs_root *root;
5484 struct btrfs_key root_key;
5485 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5488 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5489 root_key.type = BTRFS_ROOT_ITEM_KEY;
5490 root_key.offset = (u64)-1;
5491 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5493 return ERR_CAST(root);
5495 trans = btrfs_start_transaction(root, 1);
5498 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5502 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5505 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5506 group->key.offset, 0, group->key.offset,
5510 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5511 if (inode->i_state & I_NEW) {
5512 BTRFS_I(inode)->root = root;
5513 BTRFS_I(inode)->location.objectid = objectid;
5514 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5515 BTRFS_I(inode)->location.offset = 0;
5516 btrfs_read_locked_inode(inode);
5517 unlock_new_inode(inode);
5518 BUG_ON(is_bad_inode(inode));
5523 err = btrfs_orphan_add(trans, inode);
5525 btrfs_end_transaction(trans, root);
5529 inode = ERR_PTR(err);
5534 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5536 struct btrfs_trans_handle *trans;
5537 struct btrfs_path *path;
5538 struct btrfs_fs_info *info = root->fs_info;
5539 struct extent_buffer *leaf;
5540 struct inode *reloc_inode;
5541 struct btrfs_block_group_cache *block_group;
5542 struct btrfs_key key;
5551 root = root->fs_info->extent_root;
5553 block_group = btrfs_lookup_block_group(info, group_start);
5554 BUG_ON(!block_group);
5556 printk("btrfs relocating block group %llu flags %llu\n",
5557 (unsigned long long)block_group->key.objectid,
5558 (unsigned long long)block_group->flags);
5560 path = btrfs_alloc_path();
5563 reloc_inode = create_reloc_inode(info, block_group);
5564 BUG_ON(IS_ERR(reloc_inode));
5566 __alloc_chunk_for_shrink(root, block_group, 1);
5567 set_block_group_readonly(block_group);
5569 btrfs_start_delalloc_inodes(info->tree_root);
5570 btrfs_wait_ordered_extents(info->tree_root, 0);
5575 key.objectid = block_group->key.objectid;
5578 cur_byte = key.objectid;
5580 trans = btrfs_start_transaction(info->tree_root, 1);
5581 btrfs_commit_transaction(trans, info->tree_root);
5583 mutex_lock(&root->fs_info->cleaner_mutex);
5584 btrfs_clean_old_snapshots(info->tree_root);
5585 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5586 mutex_unlock(&root->fs_info->cleaner_mutex);
5589 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5593 leaf = path->nodes[0];
5594 nritems = btrfs_header_nritems(leaf);
5595 if (path->slots[0] >= nritems) {
5596 ret = btrfs_next_leaf(root, path);
5603 leaf = path->nodes[0];
5604 nritems = btrfs_header_nritems(leaf);
5607 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5609 if (key.objectid >= block_group->key.objectid +
5610 block_group->key.offset)
5613 if (progress && need_resched()) {
5614 btrfs_release_path(root, path);
5621 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5622 key.objectid + key.offset <= cur_byte) {
5628 cur_byte = key.objectid + key.offset;
5629 btrfs_release_path(root, path);
5631 __alloc_chunk_for_shrink(root, block_group, 0);
5632 ret = relocate_one_extent(root, path, &key, block_group,
5638 key.objectid = cur_byte;
5643 btrfs_release_path(root, path);
5646 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5647 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5648 WARN_ON(reloc_inode->i_mapping->nrpages);
5651 if (total_found > 0) {
5652 printk("btrfs found %llu extents in pass %d\n",
5653 (unsigned long long)total_found, pass);
5655 if (total_found == skipped && pass > 2) {
5657 reloc_inode = create_reloc_inode(info, block_group);
5663 /* delete reloc_inode */
5666 /* unpin extents in this range */
5667 trans = btrfs_start_transaction(info->tree_root, 1);
5668 btrfs_commit_transaction(trans, info->tree_root);
5670 spin_lock(&block_group->lock);
5671 WARN_ON(block_group->pinned > 0);
5672 WARN_ON(block_group->reserved > 0);
5673 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5674 spin_unlock(&block_group->lock);
5677 btrfs_free_path(path);
5681 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5682 struct btrfs_key *key)
5685 struct btrfs_key found_key;
5686 struct extent_buffer *leaf;
5689 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5694 slot = path->slots[0];
5695 leaf = path->nodes[0];
5696 if (slot >= btrfs_header_nritems(leaf)) {
5697 ret = btrfs_next_leaf(root, path);
5704 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5706 if (found_key.objectid >= key->objectid &&
5707 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5718 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5720 struct btrfs_block_group_cache *block_group;
5723 spin_lock(&info->block_group_cache_lock);
5724 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5725 block_group = rb_entry(n, struct btrfs_block_group_cache,
5727 rb_erase(&block_group->cache_node,
5728 &info->block_group_cache_tree);
5729 spin_unlock(&info->block_group_cache_lock);
5731 btrfs_remove_free_space_cache(block_group);
5732 down_write(&block_group->space_info->groups_sem);
5733 list_del(&block_group->list);
5734 up_write(&block_group->space_info->groups_sem);
5737 spin_lock(&info->block_group_cache_lock);
5739 spin_unlock(&info->block_group_cache_lock);
5743 int btrfs_read_block_groups(struct btrfs_root *root)
5745 struct btrfs_path *path;
5747 struct btrfs_block_group_cache *cache;
5748 struct btrfs_fs_info *info = root->fs_info;
5749 struct btrfs_space_info *space_info;
5750 struct btrfs_key key;
5751 struct btrfs_key found_key;
5752 struct extent_buffer *leaf;
5754 root = info->extent_root;
5757 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5758 path = btrfs_alloc_path();
5763 ret = find_first_block_group(root, path, &key);
5771 leaf = path->nodes[0];
5772 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5773 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5779 spin_lock_init(&cache->lock);
5780 mutex_init(&cache->alloc_mutex);
5781 INIT_LIST_HEAD(&cache->list);
5782 read_extent_buffer(leaf, &cache->item,
5783 btrfs_item_ptr_offset(leaf, path->slots[0]),
5784 sizeof(cache->item));
5785 memcpy(&cache->key, &found_key, sizeof(found_key));
5787 key.objectid = found_key.objectid + found_key.offset;
5788 btrfs_release_path(root, path);
5789 cache->flags = btrfs_block_group_flags(&cache->item);
5791 ret = update_space_info(info, cache->flags, found_key.offset,
5792 btrfs_block_group_used(&cache->item),
5795 cache->space_info = space_info;
5796 down_write(&space_info->groups_sem);
5797 list_add_tail(&cache->list, &space_info->block_groups);
5798 up_write(&space_info->groups_sem);
5800 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5803 set_avail_alloc_bits(root->fs_info, cache->flags);
5807 btrfs_free_path(path);
5811 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5812 struct btrfs_root *root, u64 bytes_used,
5813 u64 type, u64 chunk_objectid, u64 chunk_offset,
5817 struct btrfs_root *extent_root;
5818 struct btrfs_block_group_cache *cache;
5820 extent_root = root->fs_info->extent_root;
5822 root->fs_info->last_trans_new_blockgroup = trans->transid;
5824 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5828 cache->key.objectid = chunk_offset;
5829 cache->key.offset = size;
5830 spin_lock_init(&cache->lock);
5831 mutex_init(&cache->alloc_mutex);
5832 INIT_LIST_HEAD(&cache->list);
5833 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5835 btrfs_set_block_group_used(&cache->item, bytes_used);
5836 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5837 cache->flags = type;
5838 btrfs_set_block_group_flags(&cache->item, type);
5840 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5841 &cache->space_info);
5843 down_write(&cache->space_info->groups_sem);
5844 list_add_tail(&cache->list, &cache->space_info->block_groups);
5845 up_write(&cache->space_info->groups_sem);
5847 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5850 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5851 sizeof(cache->item));
5854 finish_current_insert(trans, extent_root, 0);
5855 ret = del_pending_extents(trans, extent_root, 0);
5857 set_avail_alloc_bits(extent_root->fs_info, type);
5862 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5863 struct btrfs_root *root, u64 group_start)
5865 struct btrfs_path *path;
5866 struct btrfs_block_group_cache *block_group;
5867 struct btrfs_key key;
5870 root = root->fs_info->extent_root;
5872 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5873 BUG_ON(!block_group);
5874 BUG_ON(!block_group->ro);
5876 memcpy(&key, &block_group->key, sizeof(key));
5878 path = btrfs_alloc_path();
5881 btrfs_remove_free_space_cache(block_group);
5882 rb_erase(&block_group->cache_node,
5883 &root->fs_info->block_group_cache_tree);
5884 down_write(&block_group->space_info->groups_sem);
5885 list_del(&block_group->list);
5886 up_write(&block_group->space_info->groups_sem);
5888 spin_lock(&block_group->space_info->lock);
5889 block_group->space_info->total_bytes -= block_group->key.offset;
5890 block_group->space_info->bytes_readonly -= block_group->key.offset;
5891 spin_unlock(&block_group->space_info->lock);
5894 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5895 kfree(shrink_block_group);
5898 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5904 ret = btrfs_del_item(trans, root, path);
5906 btrfs_free_path(path);