2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/version.h>
28 #include "print-tree.h"
29 #include "transaction.h"
32 #include "ref-cache.h"
35 #define PENDING_EXTENT_INSERT 0
36 #define PENDING_EXTENT_DELETE 1
37 #define PENDING_BACKREF_UPDATE 2
39 struct pending_extent_op {
48 struct list_head list;
52 static int finish_current_insert(struct btrfs_trans_handle *trans,
53 struct btrfs_root *extent_root, int all);
54 static int del_pending_extents(struct btrfs_trans_handle *trans,
55 struct btrfs_root *extent_root, int all);
56 static int pin_down_bytes(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 bytenr, u64 num_bytes, int is_data);
59 static int update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc,
64 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
66 return (cache->flags & bits) == bits;
70 * this adds the block group to the fs_info rb tree for the block group
73 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
74 struct btrfs_block_group_cache *block_group)
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
87 if (block_group->key.objectid < cache->key.objectid) {
89 } else if (block_group->key.objectid > cache->key.objectid) {
92 spin_unlock(&info->block_group_cache_lock);
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
109 static struct btrfs_block_group_cache *
110 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
113 struct btrfs_block_group_cache *cache, *ret = NULL;
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
121 cache = rb_entry(n, struct btrfs_block_group_cache,
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
142 atomic_inc(&ret->count);
143 spin_unlock(&info->block_group_cache_lock);
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
153 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
156 u64 extent_start, extent_end, size;
159 mutex_lock(&info->pinned_mutex);
160 while (start < end) {
161 ret = find_first_extent_bit(&info->pinned_extents, start,
162 &extent_start, &extent_end,
167 if (extent_start == start) {
168 start = extent_end + 1;
169 } else if (extent_start > start && extent_start < end) {
170 size = extent_start - start;
171 ret = btrfs_add_free_space(block_group, start,
174 start = extent_end + 1;
182 ret = btrfs_add_free_space(block_group, start, size);
185 mutex_unlock(&info->pinned_mutex);
190 static int remove_sb_from_cache(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr, 0,
202 &logical, &nr, &stripe_len);
205 btrfs_remove_free_space(cache, logical[nr],
213 static int cache_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache *block_group)
216 struct btrfs_path *path;
218 struct btrfs_key key;
219 struct extent_buffer *leaf;
226 root = root->fs_info->extent_root;
228 if (block_group->cached)
231 path = btrfs_alloc_path();
237 * we get into deadlocks with paths held by callers of this function.
238 * since the alloc_mutex is protecting things right now, just
239 * skip the locking here
241 path->skip_locking = 1;
242 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
245 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
251 leaf = path->nodes[0];
252 slot = path->slots[0];
253 if (slot >= btrfs_header_nritems(leaf)) {
254 ret = btrfs_next_leaf(root, path);
262 btrfs_item_key_to_cpu(leaf, &key, slot);
263 if (key.objectid < block_group->key.objectid)
266 if (key.objectid >= block_group->key.objectid +
267 block_group->key.offset)
270 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
271 add_new_free_space(block_group, root->fs_info, last,
274 last = key.objectid + key.offset;
280 add_new_free_space(block_group, root->fs_info, last,
281 block_group->key.objectid +
282 block_group->key.offset);
284 remove_sb_from_cache(root, block_group);
285 block_group->cached = 1;
288 btrfs_free_path(path);
293 * return the block group that starts at or after bytenr
295 static struct btrfs_block_group_cache *
296 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
298 struct btrfs_block_group_cache *cache;
300 cache = block_group_cache_tree_search(info, bytenr, 0);
306 * return the block group that contains teh given bytenr
308 struct btrfs_block_group_cache *btrfs_lookup_block_group(
309 struct btrfs_fs_info *info,
312 struct btrfs_block_group_cache *cache;
314 cache = block_group_cache_tree_search(info, bytenr, 1);
319 static inline void put_block_group(struct btrfs_block_group_cache *cache)
321 if (atomic_dec_and_test(&cache->count))
325 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
328 struct list_head *head = &info->space_info;
329 struct list_head *cur;
330 struct btrfs_space_info *found;
331 list_for_each(cur, head) {
332 found = list_entry(cur, struct btrfs_space_info, list);
333 if (found->flags == flags)
339 static u64 div_factor(u64 num, int factor)
348 u64 btrfs_find_block_group(struct btrfs_root *root,
349 u64 search_start, u64 search_hint, int owner)
351 struct btrfs_block_group_cache *cache;
353 u64 last = max(search_hint, search_start);
360 cache = btrfs_lookup_first_block_group(root->fs_info, last);
364 spin_lock(&cache->lock);
365 last = cache->key.objectid + cache->key.offset;
366 used = btrfs_block_group_used(&cache->item);
368 if ((full_search || !cache->ro) &&
369 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
370 if (used + cache->pinned + cache->reserved <
371 div_factor(cache->key.offset, factor)) {
372 group_start = cache->key.objectid;
373 spin_unlock(&cache->lock);
374 put_block_group(cache);
378 spin_unlock(&cache->lock);
379 put_block_group(cache);
387 if (!full_search && factor < 10) {
397 /* simple helper to search for an existing extent at a given offset */
398 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
401 struct btrfs_key key;
402 struct btrfs_path *path;
404 path = btrfs_alloc_path();
406 key.objectid = start;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
409 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
411 btrfs_free_path(path);
416 * Back reference rules. Back refs have three main goals:
418 * 1) differentiate between all holders of references to an extent so that
419 * when a reference is dropped we can make sure it was a valid reference
420 * before freeing the extent.
422 * 2) Provide enough information to quickly find the holders of an extent
423 * if we notice a given block is corrupted or bad.
425 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
426 * maintenance. This is actually the same as #2, but with a slightly
427 * different use case.
429 * File extents can be referenced by:
431 * - multiple snapshots, subvolumes, or different generations in one subvol
432 * - different files inside a single subvolume
433 * - different offsets inside a file (bookend extents in file.c)
435 * The extent ref structure has fields for:
437 * - Objectid of the subvolume root
438 * - Generation number of the tree holding the reference
439 * - objectid of the file holding the reference
440 * - number of references holding by parent node (alway 1 for tree blocks)
442 * Btree leaf may hold multiple references to a file extent. In most cases,
443 * these references are from same file and the corresponding offsets inside
444 * the file are close together.
446 * When a file extent is allocated the fields are filled in:
447 * (root_key.objectid, trans->transid, inode objectid, 1)
449 * When a leaf is cow'd new references are added for every file extent found
450 * in the leaf. It looks similar to the create case, but trans->transid will
451 * be different when the block is cow'd.
453 * (root_key.objectid, trans->transid, inode objectid,
454 * number of references in the leaf)
456 * When a file extent is removed either during snapshot deletion or
457 * file truncation, we find the corresponding back reference and check
458 * the following fields:
460 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
463 * Btree extents can be referenced by:
465 * - Different subvolumes
466 * - Different generations of the same subvolume
468 * When a tree block is created, back references are inserted:
470 * (root->root_key.objectid, trans->transid, level, 1)
472 * When a tree block is cow'd, new back references are added for all the
473 * blocks it points to. If the tree block isn't in reference counted root,
474 * the old back references are removed. These new back references are of
475 * the form (trans->transid will have increased since creation):
477 * (root->root_key.objectid, trans->transid, level, 1)
479 * When a backref is in deleting, the following fields are checked:
481 * if backref was for a tree root:
482 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
484 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
486 * Back Reference Key composing:
488 * The key objectid corresponds to the first byte in the extent, the key
489 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
490 * byte of parent extent. If a extent is tree root, the key offset is set
491 * to the key objectid.
494 static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
495 struct btrfs_root *root,
496 struct btrfs_path *path,
497 u64 bytenr, u64 parent,
498 u64 ref_root, u64 ref_generation,
499 u64 owner_objectid, int del)
501 struct btrfs_key key;
502 struct btrfs_extent_ref *ref;
503 struct extent_buffer *leaf;
507 key.objectid = bytenr;
508 key.type = BTRFS_EXTENT_REF_KEY;
511 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
519 leaf = path->nodes[0];
520 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
521 ref_objectid = btrfs_ref_objectid(leaf, ref);
522 if (btrfs_ref_root(leaf, ref) != ref_root ||
523 btrfs_ref_generation(leaf, ref) != ref_generation ||
524 (ref_objectid != owner_objectid &&
525 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
536 * updates all the backrefs that are pending on update_list for the
539 static noinline int update_backrefs(struct btrfs_trans_handle *trans,
540 struct btrfs_root *extent_root,
541 struct btrfs_path *path,
542 struct list_head *update_list)
544 struct btrfs_key key;
545 struct btrfs_extent_ref *ref;
546 struct btrfs_fs_info *info = extent_root->fs_info;
547 struct pending_extent_op *op;
548 struct extent_buffer *leaf;
550 struct list_head *cur = update_list->next;
552 u64 ref_root = extent_root->root_key.objectid;
554 op = list_entry(cur, struct pending_extent_op, list);
557 key.objectid = op->bytenr;
558 key.type = BTRFS_EXTENT_REF_KEY;
559 key.offset = op->orig_parent;
561 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
564 leaf = path->nodes[0];
567 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
569 ref_objectid = btrfs_ref_objectid(leaf, ref);
571 if (btrfs_ref_root(leaf, ref) != ref_root ||
572 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
573 (ref_objectid != op->level &&
574 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
575 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
576 "root %llu, owner %u\n",
577 (unsigned long long)op->bytenr,
578 (unsigned long long)op->orig_parent,
579 (unsigned long long)ref_root, op->level);
580 btrfs_print_leaf(extent_root, leaf);
584 key.objectid = op->bytenr;
585 key.offset = op->parent;
586 key.type = BTRFS_EXTENT_REF_KEY;
587 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590 btrfs_set_ref_generation(leaf, ref, op->generation);
594 list_del_init(&op->list);
595 unlock_extent(&info->extent_ins, op->bytenr,
596 op->bytenr + op->num_bytes - 1, GFP_NOFS);
599 if (cur == update_list) {
600 btrfs_mark_buffer_dirty(path->nodes[0]);
601 btrfs_release_path(extent_root, path);
605 op = list_entry(cur, struct pending_extent_op, list);
608 while (path->slots[0] < btrfs_header_nritems(leaf)) {
609 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
610 if (key.objectid == op->bytenr &&
611 key.type == BTRFS_EXTENT_REF_KEY)
616 btrfs_mark_buffer_dirty(path->nodes[0]);
617 btrfs_release_path(extent_root, path);
624 static noinline int insert_extents(struct btrfs_trans_handle *trans,
625 struct btrfs_root *extent_root,
626 struct btrfs_path *path,
627 struct list_head *insert_list, int nr)
629 struct btrfs_key *keys;
631 struct pending_extent_op *op;
632 struct extent_buffer *leaf;
633 struct list_head *cur = insert_list->next;
634 struct btrfs_fs_info *info = extent_root->fs_info;
635 u64 ref_root = extent_root->root_key.objectid;
636 int i = 0, last = 0, ret;
642 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
646 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
652 list_for_each_entry(op, insert_list, list) {
653 keys[i].objectid = op->bytenr;
654 keys[i].offset = op->num_bytes;
655 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
656 data_size[i] = sizeof(struct btrfs_extent_item);
659 keys[i].objectid = op->bytenr;
660 keys[i].offset = op->parent;
661 keys[i].type = BTRFS_EXTENT_REF_KEY;
662 data_size[i] = sizeof(struct btrfs_extent_ref);
666 op = list_entry(cur, struct pending_extent_op, list);
670 ret = btrfs_insert_some_items(trans, extent_root, path,
671 keys+i, data_size+i, total-i);
677 leaf = path->nodes[0];
678 for (c = 0; c < ret; c++) {
679 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
682 * if the first item we inserted was a backref, then
683 * the EXTENT_ITEM will be the odd c's, else it will
686 if ((ref_first && (c % 2)) ||
687 (!ref_first && !(c % 2))) {
688 struct btrfs_extent_item *itm;
690 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
691 struct btrfs_extent_item);
692 btrfs_set_extent_refs(path->nodes[0], itm, 1);
695 struct btrfs_extent_ref *ref;
697 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
698 struct btrfs_extent_ref);
699 btrfs_set_ref_root(leaf, ref, ref_root);
700 btrfs_set_ref_generation(leaf, ref,
702 btrfs_set_ref_objectid(leaf, ref, op->level);
703 btrfs_set_ref_num_refs(leaf, ref, 1);
708 * using del to see when its ok to free up the
709 * pending_extent_op. In the case where we insert the
710 * last item on the list in order to help do batching
711 * we need to not free the extent op until we actually
712 * insert the extent_item
715 unlock_extent(&info->extent_ins, op->bytenr,
716 op->bytenr + op->num_bytes - 1,
719 list_del_init(&op->list);
721 if (cur != insert_list)
723 struct pending_extent_op,
727 btrfs_mark_buffer_dirty(leaf);
728 btrfs_release_path(extent_root, path);
731 * Ok backref's and items usually go right next to eachother,
732 * but if we could only insert 1 item that means that we
733 * inserted on the end of a leaf, and we have no idea what may
734 * be on the next leaf so we just play it safe. In order to
735 * try and help this case we insert the last thing on our
736 * insert list so hopefully it will end up being the last
737 * thing on the leaf and everything else will be before it,
738 * which will let us insert a whole bunch of items at the same
741 if (ret == 1 && !last && (i + ret < total)) {
743 * last: where we will pick up the next time around
744 * i: our current key to insert, will be total - 1
745 * cur: the current op we are screwing with
750 cur = insert_list->prev;
751 op = list_entry(cur, struct pending_extent_op, list);
754 * ok we successfully inserted the last item on the
755 * list, lets reset everything
757 * i: our current key to insert, so where we left off
759 * last: done with this
760 * cur: the op we are messing with
762 * total: since we inserted the last key, we need to
763 * decrement total so we dont overflow
769 cur = insert_list->next;
770 op = list_entry(cur, struct pending_extent_op,
785 static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
786 struct btrfs_root *root,
787 struct btrfs_path *path,
788 u64 bytenr, u64 parent,
789 u64 ref_root, u64 ref_generation,
792 struct btrfs_key key;
793 struct extent_buffer *leaf;
794 struct btrfs_extent_ref *ref;
798 key.objectid = bytenr;
799 key.type = BTRFS_EXTENT_REF_KEY;
802 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
804 leaf = path->nodes[0];
805 ref = btrfs_item_ptr(leaf, path->slots[0],
806 struct btrfs_extent_ref);
807 btrfs_set_ref_root(leaf, ref, ref_root);
808 btrfs_set_ref_generation(leaf, ref, ref_generation);
809 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
810 btrfs_set_ref_num_refs(leaf, ref, 1);
811 } else if (ret == -EEXIST) {
813 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
814 leaf = path->nodes[0];
815 ref = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_ref);
817 if (btrfs_ref_root(leaf, ref) != ref_root ||
818 btrfs_ref_generation(leaf, ref) != ref_generation) {
824 num_refs = btrfs_ref_num_refs(leaf, ref);
825 BUG_ON(num_refs == 0);
826 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
828 existing_owner = btrfs_ref_objectid(leaf, ref);
829 if (existing_owner != owner_objectid &&
830 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
831 btrfs_set_ref_objectid(leaf, ref,
832 BTRFS_MULTIPLE_OBJECTIDS);
838 btrfs_mark_buffer_dirty(path->nodes[0]);
840 btrfs_release_path(root, path);
844 static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
845 struct btrfs_root *root,
846 struct btrfs_path *path)
848 struct extent_buffer *leaf;
849 struct btrfs_extent_ref *ref;
853 leaf = path->nodes[0];
854 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
859 ret = btrfs_del_item(trans, root, path);
861 btrfs_set_ref_num_refs(leaf, ref, num_refs);
862 btrfs_mark_buffer_dirty(leaf);
864 btrfs_release_path(root, path);
868 #ifdef BIO_RW_DISCARD
869 static void btrfs_issue_discard(struct block_device *bdev,
872 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
873 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
875 blkdev_issue_discard(bdev, start >> 9, len >> 9);
880 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
883 #ifdef BIO_RW_DISCARD
885 u64 map_length = num_bytes;
886 struct btrfs_multi_bio *multi = NULL;
888 /* Tell the block device(s) that the sectors can be discarded */
889 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
890 bytenr, &map_length, &multi, 0);
892 struct btrfs_bio_stripe *stripe = multi->stripes;
895 if (map_length > num_bytes)
896 map_length = num_bytes;
898 for (i = 0; i < multi->num_stripes; i++, stripe++) {
899 btrfs_issue_discard(stripe->dev->bdev,
912 static noinline int free_extents(struct btrfs_trans_handle *trans,
913 struct btrfs_root *extent_root,
914 struct list_head *del_list)
916 struct btrfs_fs_info *info = extent_root->fs_info;
917 struct btrfs_path *path;
918 struct btrfs_key key, found_key;
919 struct extent_buffer *leaf;
920 struct list_head *cur;
921 struct pending_extent_op *op;
922 struct btrfs_extent_item *ei;
923 int ret, num_to_del, extent_slot = 0, found_extent = 0;
927 path = btrfs_alloc_path();
933 /* search for the backref for the current ref we want to delete */
934 cur = del_list->next;
935 op = list_entry(cur, struct pending_extent_op, list);
936 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
938 extent_root->root_key.objectid,
939 op->orig_generation, op->level, 1);
941 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
942 "root %llu gen %llu owner %u\n",
943 (unsigned long long)op->bytenr,
944 (unsigned long long)extent_root->root_key.objectid,
945 (unsigned long long)op->orig_generation, op->level);
946 btrfs_print_leaf(extent_root, path->nodes[0]);
951 extent_slot = path->slots[0];
956 * if we aren't the first item on the leaf we can move back one and see
957 * if our ref is right next to our extent item
959 if (likely(extent_slot)) {
961 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
963 if (found_key.objectid == op->bytenr &&
964 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
965 found_key.offset == op->num_bytes) {
972 * if we didn't find the extent we need to delete the backref and then
973 * search for the extent item key so we can update its ref count
976 key.objectid = op->bytenr;
977 key.type = BTRFS_EXTENT_ITEM_KEY;
978 key.offset = op->num_bytes;
980 ret = remove_extent_backref(trans, extent_root, path);
982 btrfs_release_path(extent_root, path);
983 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
985 extent_slot = path->slots[0];
988 /* this is where we update the ref count for the extent */
989 leaf = path->nodes[0];
990 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
991 refs = btrfs_extent_refs(leaf, ei);
994 btrfs_set_extent_refs(leaf, ei, refs);
996 btrfs_mark_buffer_dirty(leaf);
999 * This extent needs deleting. The reason cur_slot is extent_slot +
1000 * num_to_del is because extent_slot points to the slot where the extent
1001 * is, and if the backref was not right next to the extent we will be
1002 * deleting at least 1 item, and will want to start searching at the
1003 * slot directly next to extent_slot. However if we did find the
1004 * backref next to the extent item them we will be deleting at least 2
1005 * items and will want to start searching directly after the ref slot
1008 struct list_head *pos, *n, *end;
1009 int cur_slot = extent_slot+num_to_del;
1013 path->slots[0] = extent_slot;
1014 bytes_freed = op->num_bytes;
1016 mutex_lock(&info->pinned_mutex);
1017 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1018 op->num_bytes, op->level >=
1019 BTRFS_FIRST_FREE_OBJECTID);
1020 mutex_unlock(&info->pinned_mutex);
1025 * we need to see if we can delete multiple things at once, so
1026 * start looping through the list of extents we are wanting to
1027 * delete and see if their extent/backref's are right next to
1028 * eachother and the extents only have 1 ref
1030 for (pos = cur->next; pos != del_list; pos = pos->next) {
1031 struct pending_extent_op *tmp;
1033 tmp = list_entry(pos, struct pending_extent_op, list);
1035 /* we only want to delete extent+ref at this stage */
1036 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1039 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1040 if (found_key.objectid != tmp->bytenr ||
1041 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1042 found_key.offset != tmp->num_bytes)
1045 /* check to make sure this extent only has one ref */
1046 ei = btrfs_item_ptr(leaf, cur_slot,
1047 struct btrfs_extent_item);
1048 if (btrfs_extent_refs(leaf, ei) != 1)
1051 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1052 if (found_key.objectid != tmp->bytenr ||
1053 found_key.type != BTRFS_EXTENT_REF_KEY ||
1054 found_key.offset != tmp->orig_parent)
1058 * the ref is right next to the extent, we can set the
1059 * ref count to 0 since we will delete them both now
1061 btrfs_set_extent_refs(leaf, ei, 0);
1063 /* pin down the bytes for this extent */
1064 mutex_lock(&info->pinned_mutex);
1065 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1066 tmp->num_bytes, tmp->level >=
1067 BTRFS_FIRST_FREE_OBJECTID);
1068 mutex_unlock(&info->pinned_mutex);
1072 * use the del field to tell if we need to go ahead and
1073 * free up the extent when we delete the item or not.
1076 bytes_freed += tmp->num_bytes;
1083 /* update the free space counters */
1084 spin_lock(&info->delalloc_lock);
1085 super_used = btrfs_super_bytes_used(&info->super_copy);
1086 btrfs_set_super_bytes_used(&info->super_copy,
1087 super_used - bytes_freed);
1089 root_used = btrfs_root_used(&extent_root->root_item);
1090 btrfs_set_root_used(&extent_root->root_item,
1091 root_used - bytes_freed);
1092 spin_unlock(&info->delalloc_lock);
1094 /* delete the items */
1095 ret = btrfs_del_items(trans, extent_root, path,
1096 path->slots[0], num_to_del);
1100 * loop through the extents we deleted and do the cleanup work
1103 for (pos = cur, n = pos->next; pos != end;
1104 pos = n, n = pos->next) {
1105 struct pending_extent_op *tmp;
1106 tmp = list_entry(pos, struct pending_extent_op, list);
1109 * remember tmp->del tells us wether or not we pinned
1112 ret = update_block_group(trans, extent_root,
1113 tmp->bytenr, tmp->num_bytes, 0,
1117 list_del_init(&tmp->list);
1118 unlock_extent(&info->extent_ins, tmp->bytenr,
1119 tmp->bytenr + tmp->num_bytes - 1,
1123 } else if (refs && found_extent) {
1125 * the ref and extent were right next to eachother, but the
1126 * extent still has a ref, so just free the backref and keep
1129 ret = remove_extent_backref(trans, extent_root, path);
1132 list_del_init(&op->list);
1133 unlock_extent(&info->extent_ins, op->bytenr,
1134 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1138 * the extent has multiple refs and the backref we were looking
1139 * for was not right next to it, so just unlock and go next,
1142 list_del_init(&op->list);
1143 unlock_extent(&info->extent_ins, op->bytenr,
1144 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1148 btrfs_release_path(extent_root, path);
1149 if (!list_empty(del_list))
1153 btrfs_free_path(path);
1157 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root, u64 bytenr,
1159 u64 orig_parent, u64 parent,
1160 u64 orig_root, u64 ref_root,
1161 u64 orig_generation, u64 ref_generation,
1165 struct btrfs_root *extent_root = root->fs_info->extent_root;
1166 struct btrfs_path *path;
1168 if (root == root->fs_info->extent_root) {
1169 struct pending_extent_op *extent_op;
1172 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1173 num_bytes = btrfs_level_size(root, (int)owner_objectid);
1174 mutex_lock(&root->fs_info->extent_ins_mutex);
1175 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
1176 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
1178 ret = get_state_private(&root->fs_info->extent_ins,
1181 extent_op = (struct pending_extent_op *)
1182 (unsigned long)priv;
1183 BUG_ON(extent_op->parent != orig_parent);
1184 BUG_ON(extent_op->generation != orig_generation);
1186 extent_op->parent = parent;
1187 extent_op->generation = ref_generation;
1189 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1192 extent_op->type = PENDING_BACKREF_UPDATE;
1193 extent_op->bytenr = bytenr;
1194 extent_op->num_bytes = num_bytes;
1195 extent_op->parent = parent;
1196 extent_op->orig_parent = orig_parent;
1197 extent_op->generation = ref_generation;
1198 extent_op->orig_generation = orig_generation;
1199 extent_op->level = (int)owner_objectid;
1200 INIT_LIST_HEAD(&extent_op->list);
1203 set_extent_bits(&root->fs_info->extent_ins,
1204 bytenr, bytenr + num_bytes - 1,
1205 EXTENT_WRITEBACK, GFP_NOFS);
1206 set_state_private(&root->fs_info->extent_ins,
1207 bytenr, (unsigned long)extent_op);
1209 mutex_unlock(&root->fs_info->extent_ins_mutex);
1213 path = btrfs_alloc_path();
1216 ret = lookup_extent_backref(trans, extent_root, path,
1217 bytenr, orig_parent, orig_root,
1218 orig_generation, owner_objectid, 1);
1221 ret = remove_extent_backref(trans, extent_root, path);
1224 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1225 parent, ref_root, ref_generation,
1228 finish_current_insert(trans, extent_root, 0);
1229 del_pending_extents(trans, extent_root, 0);
1231 btrfs_free_path(path);
1235 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1236 struct btrfs_root *root, u64 bytenr,
1237 u64 orig_parent, u64 parent,
1238 u64 ref_root, u64 ref_generation,
1242 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1243 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1245 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1246 parent, ref_root, ref_root,
1247 ref_generation, ref_generation,
1252 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1253 struct btrfs_root *root, u64 bytenr,
1254 u64 orig_parent, u64 parent,
1255 u64 orig_root, u64 ref_root,
1256 u64 orig_generation, u64 ref_generation,
1259 struct btrfs_path *path;
1261 struct btrfs_key key;
1262 struct extent_buffer *l;
1263 struct btrfs_extent_item *item;
1266 path = btrfs_alloc_path();
1271 key.objectid = bytenr;
1272 key.type = BTRFS_EXTENT_ITEM_KEY;
1273 key.offset = (u64)-1;
1275 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1279 BUG_ON(ret == 0 || path->slots[0] == 0);
1284 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1285 if (key.objectid != bytenr) {
1286 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1287 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1288 (unsigned long long)bytenr,
1289 (unsigned long long)key.objectid);
1292 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1294 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1295 refs = btrfs_extent_refs(l, item);
1296 btrfs_set_extent_refs(l, item, refs + 1);
1297 btrfs_mark_buffer_dirty(path->nodes[0]);
1299 btrfs_release_path(root->fs_info->extent_root, path);
1302 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1303 path, bytenr, parent,
1304 ref_root, ref_generation,
1307 finish_current_insert(trans, root->fs_info->extent_root, 0);
1308 del_pending_extents(trans, root->fs_info->extent_root, 0);
1310 btrfs_free_path(path);
1314 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1315 struct btrfs_root *root,
1316 u64 bytenr, u64 num_bytes, u64 parent,
1317 u64 ref_root, u64 ref_generation,
1321 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1322 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1324 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1325 0, ref_root, 0, ref_generation,
1330 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root)
1333 finish_current_insert(trans, root->fs_info->extent_root, 1);
1334 del_pending_extents(trans, root->fs_info->extent_root, 1);
1338 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root, u64 bytenr,
1340 u64 num_bytes, u32 *refs)
1342 struct btrfs_path *path;
1344 struct btrfs_key key;
1345 struct extent_buffer *l;
1346 struct btrfs_extent_item *item;
1348 WARN_ON(num_bytes < root->sectorsize);
1349 path = btrfs_alloc_path();
1351 key.objectid = bytenr;
1352 key.offset = num_bytes;
1353 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1354 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1359 btrfs_print_leaf(root, path->nodes[0]);
1360 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1361 (unsigned long long)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 objectid, 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) ||
1434 objectid != btrfs_ref_objectid(leaf, ref_item)) {
1438 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1447 btrfs_free_path(path);
1451 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1452 struct extent_buffer *buf, u32 nr_extents)
1454 struct btrfs_key key;
1455 struct btrfs_file_extent_item *fi;
1463 if (!root->ref_cows)
1466 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1468 root_gen = root->root_key.offset;
1471 root_gen = trans->transid - 1;
1474 level = btrfs_header_level(buf);
1475 nritems = btrfs_header_nritems(buf);
1478 struct btrfs_leaf_ref *ref;
1479 struct btrfs_extent_info *info;
1481 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1487 ref->root_gen = root_gen;
1488 ref->bytenr = buf->start;
1489 ref->owner = btrfs_header_owner(buf);
1490 ref->generation = btrfs_header_generation(buf);
1491 ref->nritems = nr_extents;
1492 info = ref->extents;
1494 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1496 btrfs_item_key_to_cpu(buf, &key, i);
1497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1499 fi = btrfs_item_ptr(buf, i,
1500 struct btrfs_file_extent_item);
1501 if (btrfs_file_extent_type(buf, fi) ==
1502 BTRFS_FILE_EXTENT_INLINE)
1504 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1505 if (disk_bytenr == 0)
1508 info->bytenr = disk_bytenr;
1510 btrfs_file_extent_disk_num_bytes(buf, fi);
1511 info->objectid = key.objectid;
1512 info->offset = key.offset;
1516 ret = btrfs_add_leaf_ref(root, ref, shared);
1517 if (ret == -EEXIST && shared) {
1518 struct btrfs_leaf_ref *old;
1519 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1521 btrfs_remove_leaf_ref(root, old);
1522 btrfs_free_leaf_ref(root, old);
1523 ret = btrfs_add_leaf_ref(root, ref, shared);
1526 btrfs_free_leaf_ref(root, ref);
1532 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1533 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1540 u64 orig_generation;
1542 u32 nr_file_extents = 0;
1543 struct btrfs_key key;
1544 struct btrfs_file_extent_item *fi;
1549 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1550 u64, u64, u64, u64, u64, u64, u64, u64);
1552 ref_root = btrfs_header_owner(buf);
1553 ref_generation = btrfs_header_generation(buf);
1554 orig_root = btrfs_header_owner(orig_buf);
1555 orig_generation = btrfs_header_generation(orig_buf);
1557 nritems = btrfs_header_nritems(buf);
1558 level = btrfs_header_level(buf);
1560 if (root->ref_cows) {
1561 process_func = __btrfs_inc_extent_ref;
1564 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1567 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1569 process_func = __btrfs_update_extent_ref;
1572 for (i = 0; i < nritems; i++) {
1575 btrfs_item_key_to_cpu(buf, &key, i);
1576 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1578 fi = btrfs_item_ptr(buf, i,
1579 struct btrfs_file_extent_item);
1580 if (btrfs_file_extent_type(buf, fi) ==
1581 BTRFS_FILE_EXTENT_INLINE)
1583 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1589 ret = process_func(trans, root, bytenr,
1590 orig_buf->start, buf->start,
1591 orig_root, ref_root,
1592 orig_generation, ref_generation,
1601 bytenr = btrfs_node_blockptr(buf, i);
1602 ret = process_func(trans, root, bytenr,
1603 orig_buf->start, buf->start,
1604 orig_root, ref_root,
1605 orig_generation, ref_generation,
1617 *nr_extents = nr_file_extents;
1619 *nr_extents = nritems;
1627 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1628 struct btrfs_root *root, struct extent_buffer *orig_buf,
1629 struct extent_buffer *buf, int start_slot, int nr)
1636 u64 orig_generation;
1637 struct btrfs_key key;
1638 struct btrfs_file_extent_item *fi;
1644 BUG_ON(start_slot < 0);
1645 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1647 ref_root = btrfs_header_owner(buf);
1648 ref_generation = btrfs_header_generation(buf);
1649 orig_root = btrfs_header_owner(orig_buf);
1650 orig_generation = btrfs_header_generation(orig_buf);
1651 level = btrfs_header_level(buf);
1653 if (!root->ref_cows) {
1655 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1658 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1662 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1665 btrfs_item_key_to_cpu(buf, &key, slot);
1666 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1668 fi = btrfs_item_ptr(buf, slot,
1669 struct btrfs_file_extent_item);
1670 if (btrfs_file_extent_type(buf, fi) ==
1671 BTRFS_FILE_EXTENT_INLINE)
1673 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1676 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1677 orig_buf->start, buf->start,
1678 orig_root, ref_root,
1679 orig_generation, ref_generation,
1684 bytenr = btrfs_node_blockptr(buf, slot);
1685 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1686 orig_buf->start, buf->start,
1687 orig_root, ref_root,
1688 orig_generation, ref_generation,
1700 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1701 struct btrfs_root *root,
1702 struct btrfs_path *path,
1703 struct btrfs_block_group_cache *cache)
1707 struct btrfs_root *extent_root = root->fs_info->extent_root;
1709 struct extent_buffer *leaf;
1711 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1716 leaf = path->nodes[0];
1717 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1718 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1719 btrfs_mark_buffer_dirty(leaf);
1720 btrfs_release_path(extent_root, path);
1722 finish_current_insert(trans, extent_root, 0);
1723 pending_ret = del_pending_extents(trans, extent_root, 0);
1732 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root)
1735 struct btrfs_block_group_cache *cache, *entry;
1739 struct btrfs_path *path;
1742 path = btrfs_alloc_path();
1748 spin_lock(&root->fs_info->block_group_cache_lock);
1749 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1750 n; n = rb_next(n)) {
1751 entry = rb_entry(n, struct btrfs_block_group_cache,
1758 spin_unlock(&root->fs_info->block_group_cache_lock);
1764 last += cache->key.offset;
1766 err = write_one_cache_group(trans, root,
1769 * if we fail to write the cache group, we want
1770 * to keep it marked dirty in hopes that a later
1778 btrfs_free_path(path);
1782 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1784 struct btrfs_block_group_cache *block_group;
1787 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1788 if (!block_group || block_group->ro)
1791 put_block_group(block_group);
1795 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1796 u64 total_bytes, u64 bytes_used,
1797 struct btrfs_space_info **space_info)
1799 struct btrfs_space_info *found;
1801 found = __find_space_info(info, flags);
1803 spin_lock(&found->lock);
1804 found->total_bytes += total_bytes;
1805 found->bytes_used += bytes_used;
1807 spin_unlock(&found->lock);
1808 *space_info = found;
1811 found = kzalloc(sizeof(*found), GFP_NOFS);
1815 list_add(&found->list, &info->space_info);
1816 INIT_LIST_HEAD(&found->block_groups);
1817 init_rwsem(&found->groups_sem);
1818 spin_lock_init(&found->lock);
1819 found->flags = flags;
1820 found->total_bytes = total_bytes;
1821 found->bytes_used = bytes_used;
1822 found->bytes_pinned = 0;
1823 found->bytes_reserved = 0;
1824 found->bytes_readonly = 0;
1826 found->force_alloc = 0;
1827 *space_info = found;
1831 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1833 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1834 BTRFS_BLOCK_GROUP_RAID1 |
1835 BTRFS_BLOCK_GROUP_RAID10 |
1836 BTRFS_BLOCK_GROUP_DUP);
1838 if (flags & BTRFS_BLOCK_GROUP_DATA)
1839 fs_info->avail_data_alloc_bits |= extra_flags;
1840 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1841 fs_info->avail_metadata_alloc_bits |= extra_flags;
1842 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1843 fs_info->avail_system_alloc_bits |= extra_flags;
1847 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1849 spin_lock(&cache->space_info->lock);
1850 spin_lock(&cache->lock);
1852 cache->space_info->bytes_readonly += cache->key.offset -
1853 btrfs_block_group_used(&cache->item);
1856 spin_unlock(&cache->lock);
1857 spin_unlock(&cache->space_info->lock);
1860 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1862 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1864 if (num_devices == 1)
1865 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1866 if (num_devices < 4)
1867 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1869 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1870 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1871 BTRFS_BLOCK_GROUP_RAID10))) {
1872 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1875 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1876 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1877 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1880 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1881 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1882 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1883 (flags & BTRFS_BLOCK_GROUP_DUP)))
1884 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1888 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1889 struct btrfs_root *extent_root, u64 alloc_bytes,
1890 u64 flags, int force)
1892 struct btrfs_space_info *space_info;
1896 mutex_lock(&extent_root->fs_info->chunk_mutex);
1898 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1900 space_info = __find_space_info(extent_root->fs_info, flags);
1902 ret = update_space_info(extent_root->fs_info, flags,
1906 BUG_ON(!space_info);
1908 spin_lock(&space_info->lock);
1909 if (space_info->force_alloc) {
1911 space_info->force_alloc = 0;
1913 if (space_info->full) {
1914 spin_unlock(&space_info->lock);
1918 thresh = space_info->total_bytes - space_info->bytes_readonly;
1919 thresh = div_factor(thresh, 6);
1921 (space_info->bytes_used + space_info->bytes_pinned +
1922 space_info->bytes_reserved + alloc_bytes) < thresh) {
1923 spin_unlock(&space_info->lock);
1926 spin_unlock(&space_info->lock);
1928 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1930 space_info->full = 1;
1932 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1936 static int update_block_group(struct btrfs_trans_handle *trans,
1937 struct btrfs_root *root,
1938 u64 bytenr, u64 num_bytes, int alloc,
1941 struct btrfs_block_group_cache *cache;
1942 struct btrfs_fs_info *info = root->fs_info;
1943 u64 total = num_bytes;
1948 cache = btrfs_lookup_block_group(info, bytenr);
1951 byte_in_group = bytenr - cache->key.objectid;
1952 WARN_ON(byte_in_group > cache->key.offset);
1954 spin_lock(&cache->space_info->lock);
1955 spin_lock(&cache->lock);
1957 old_val = btrfs_block_group_used(&cache->item);
1958 num_bytes = min(total, cache->key.offset - byte_in_group);
1960 old_val += num_bytes;
1961 cache->space_info->bytes_used += num_bytes;
1963 cache->space_info->bytes_readonly -= num_bytes;
1964 btrfs_set_block_group_used(&cache->item, old_val);
1965 spin_unlock(&cache->lock);
1966 spin_unlock(&cache->space_info->lock);
1968 old_val -= num_bytes;
1969 cache->space_info->bytes_used -= num_bytes;
1971 cache->space_info->bytes_readonly += num_bytes;
1972 btrfs_set_block_group_used(&cache->item, old_val);
1973 spin_unlock(&cache->lock);
1974 spin_unlock(&cache->space_info->lock);
1978 ret = btrfs_discard_extent(root, bytenr,
1982 ret = btrfs_add_free_space(cache, bytenr,
1987 put_block_group(cache);
1989 bytenr += num_bytes;
1994 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1996 struct btrfs_block_group_cache *cache;
1999 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2003 bytenr = cache->key.objectid;
2004 put_block_group(cache);
2009 int btrfs_update_pinned_extents(struct btrfs_root *root,
2010 u64 bytenr, u64 num, int pin)
2013 struct btrfs_block_group_cache *cache;
2014 struct btrfs_fs_info *fs_info = root->fs_info;
2016 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2018 set_extent_dirty(&fs_info->pinned_extents,
2019 bytenr, bytenr + num - 1, GFP_NOFS);
2021 clear_extent_dirty(&fs_info->pinned_extents,
2022 bytenr, bytenr + num - 1, GFP_NOFS);
2025 cache = btrfs_lookup_block_group(fs_info, bytenr);
2027 len = min(num, cache->key.offset -
2028 (bytenr - cache->key.objectid));
2030 spin_lock(&cache->space_info->lock);
2031 spin_lock(&cache->lock);
2032 cache->pinned += len;
2033 cache->space_info->bytes_pinned += len;
2034 spin_unlock(&cache->lock);
2035 spin_unlock(&cache->space_info->lock);
2036 fs_info->total_pinned += len;
2038 spin_lock(&cache->space_info->lock);
2039 spin_lock(&cache->lock);
2040 cache->pinned -= len;
2041 cache->space_info->bytes_pinned -= len;
2042 spin_unlock(&cache->lock);
2043 spin_unlock(&cache->space_info->lock);
2044 fs_info->total_pinned -= len;
2046 btrfs_add_free_space(cache, bytenr, len);
2048 put_block_group(cache);
2055 static int update_reserved_extents(struct btrfs_root *root,
2056 u64 bytenr, u64 num, int reserve)
2059 struct btrfs_block_group_cache *cache;
2060 struct btrfs_fs_info *fs_info = root->fs_info;
2063 cache = btrfs_lookup_block_group(fs_info, bytenr);
2065 len = min(num, cache->key.offset -
2066 (bytenr - cache->key.objectid));
2068 spin_lock(&cache->space_info->lock);
2069 spin_lock(&cache->lock);
2071 cache->reserved += len;
2072 cache->space_info->bytes_reserved += len;
2074 cache->reserved -= len;
2075 cache->space_info->bytes_reserved -= len;
2077 spin_unlock(&cache->lock);
2078 spin_unlock(&cache->space_info->lock);
2079 put_block_group(cache);
2086 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2091 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2094 mutex_lock(&root->fs_info->pinned_mutex);
2096 ret = find_first_extent_bit(pinned_extents, last,
2097 &start, &end, EXTENT_DIRTY);
2100 set_extent_dirty(copy, start, end, GFP_NOFS);
2103 mutex_unlock(&root->fs_info->pinned_mutex);
2107 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
2109 struct extent_io_tree *unpin)
2115 mutex_lock(&root->fs_info->pinned_mutex);
2117 ret = find_first_extent_bit(unpin, 0, &start, &end,
2122 ret = btrfs_discard_extent(root, start, end + 1 - start);
2124 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2125 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2127 if (need_resched()) {
2128 mutex_unlock(&root->fs_info->pinned_mutex);
2130 mutex_lock(&root->fs_info->pinned_mutex);
2133 mutex_unlock(&root->fs_info->pinned_mutex);
2137 static int finish_current_insert(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *extent_root, int all)
2145 struct btrfs_fs_info *info = extent_root->fs_info;
2146 struct btrfs_path *path;
2147 struct pending_extent_op *extent_op, *tmp;
2148 struct list_head insert_list, update_list;
2150 int num_inserts = 0, max_inserts;
2152 path = btrfs_alloc_path();
2153 INIT_LIST_HEAD(&insert_list);
2154 INIT_LIST_HEAD(&update_list);
2156 max_inserts = extent_root->leafsize /
2157 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2158 sizeof(struct btrfs_extent_ref) +
2159 sizeof(struct btrfs_extent_item));
2161 mutex_lock(&info->extent_ins_mutex);
2163 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2164 &end, EXTENT_WRITEBACK);
2166 if (skipped && all && !num_inserts) {
2171 mutex_unlock(&info->extent_ins_mutex);
2175 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2179 if (need_resched()) {
2180 mutex_unlock(&info->extent_ins_mutex);
2182 mutex_lock(&info->extent_ins_mutex);
2187 ret = get_state_private(&info->extent_ins, start, &priv);
2189 extent_op = (struct pending_extent_op *)(unsigned long) priv;
2191 if (extent_op->type == PENDING_EXTENT_INSERT) {
2193 list_add_tail(&extent_op->list, &insert_list);
2195 if (num_inserts == max_inserts) {
2196 mutex_unlock(&info->extent_ins_mutex);
2199 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2200 list_add_tail(&extent_op->list, &update_list);
2208 * process the update list, clear the writeback bit for it, and if
2209 * somebody marked this thing for deletion then just unlock it and be
2210 * done, the free_extents will handle it
2212 mutex_lock(&info->extent_ins_mutex);
2213 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2214 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2215 extent_op->bytenr + extent_op->num_bytes - 1,
2216 EXTENT_WRITEBACK, GFP_NOFS);
2217 if (extent_op->del) {
2218 list_del_init(&extent_op->list);
2219 unlock_extent(&info->extent_ins, extent_op->bytenr,
2220 extent_op->bytenr + extent_op->num_bytes
2225 mutex_unlock(&info->extent_ins_mutex);
2228 * still have things left on the update list, go ahead an update
2231 if (!list_empty(&update_list)) {
2232 ret = update_backrefs(trans, extent_root, path, &update_list);
2237 * if no inserts need to be done, but we skipped some extents and we
2238 * need to make sure everything is cleaned then reset everything and
2239 * go back to the beginning
2241 if (!num_inserts && all && skipped) {
2244 INIT_LIST_HEAD(&update_list);
2245 INIT_LIST_HEAD(&insert_list);
2247 } else if (!num_inserts) {
2252 * process the insert extents list. Again if we are deleting this
2253 * extent, then just unlock it, pin down the bytes if need be, and be
2254 * done with it. Saves us from having to actually insert the extent
2255 * into the tree and then subsequently come along and delete it
2257 mutex_lock(&info->extent_ins_mutex);
2258 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2259 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2260 extent_op->bytenr + extent_op->num_bytes - 1,
2261 EXTENT_WRITEBACK, GFP_NOFS);
2262 if (extent_op->del) {
2264 list_del_init(&extent_op->list);
2265 unlock_extent(&info->extent_ins, extent_op->bytenr,
2266 extent_op->bytenr + extent_op->num_bytes
2269 mutex_lock(&extent_root->fs_info->pinned_mutex);
2270 ret = pin_down_bytes(trans, extent_root,
2272 extent_op->num_bytes, 0);
2273 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2275 spin_lock(&info->delalloc_lock);
2276 used = btrfs_super_bytes_used(&info->super_copy);
2277 btrfs_set_super_bytes_used(&info->super_copy,
2278 used - extent_op->num_bytes);
2279 used = btrfs_root_used(&extent_root->root_item);
2280 btrfs_set_root_used(&extent_root->root_item,
2281 used - extent_op->num_bytes);
2282 spin_unlock(&info->delalloc_lock);
2284 ret = update_block_group(trans, extent_root,
2286 extent_op->num_bytes,
2293 mutex_unlock(&info->extent_ins_mutex);
2295 ret = insert_extents(trans, extent_root, path, &insert_list,
2300 * if we broke out of the loop in order to insert stuff because we hit
2301 * the maximum number of inserts at a time we can handle, then loop
2302 * back and pick up where we left off
2304 if (num_inserts == max_inserts) {
2305 INIT_LIST_HEAD(&insert_list);
2306 INIT_LIST_HEAD(&update_list);
2312 * again, if we need to make absolutely sure there are no more pending
2313 * extent operations left and we know that we skipped some, go back to
2314 * the beginning and do it all again
2316 if (all && skipped) {
2317 INIT_LIST_HEAD(&insert_list);
2318 INIT_LIST_HEAD(&update_list);
2325 btrfs_free_path(path);
2329 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2330 struct btrfs_root *root,
2331 u64 bytenr, u64 num_bytes, int is_data)
2334 struct extent_buffer *buf;
2339 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2343 /* we can reuse a block if it hasn't been written
2344 * and it is from this transaction. We can't
2345 * reuse anything from the tree log root because
2346 * it has tiny sub-transactions.
2348 if (btrfs_buffer_uptodate(buf, 0) &&
2349 btrfs_try_tree_lock(buf)) {
2350 u64 header_owner = btrfs_header_owner(buf);
2351 u64 header_transid = btrfs_header_generation(buf);
2352 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2353 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2354 header_transid == trans->transid &&
2355 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2356 clean_tree_block(NULL, root, buf);
2357 btrfs_tree_unlock(buf);
2358 free_extent_buffer(buf);
2361 btrfs_tree_unlock(buf);
2363 free_extent_buffer(buf);
2365 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2372 * remove an extent from the root, returns 0 on success
2374 static int __free_extent(struct btrfs_trans_handle *trans,
2375 struct btrfs_root *root,
2376 u64 bytenr, u64 num_bytes, u64 parent,
2377 u64 root_objectid, u64 ref_generation,
2378 u64 owner_objectid, int pin, int mark_free)
2380 struct btrfs_path *path;
2381 struct btrfs_key key;
2382 struct btrfs_fs_info *info = root->fs_info;
2383 struct btrfs_root *extent_root = info->extent_root;
2384 struct extent_buffer *leaf;
2386 int extent_slot = 0;
2387 int found_extent = 0;
2389 struct btrfs_extent_item *ei;
2392 key.objectid = bytenr;
2393 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2394 key.offset = num_bytes;
2395 path = btrfs_alloc_path();
2400 ret = lookup_extent_backref(trans, extent_root, path,
2401 bytenr, parent, root_objectid,
2402 ref_generation, owner_objectid, 1);
2404 struct btrfs_key found_key;
2405 extent_slot = path->slots[0];
2406 while (extent_slot > 0) {
2408 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2410 if (found_key.objectid != bytenr)
2412 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2413 found_key.offset == num_bytes) {
2417 if (path->slots[0] - extent_slot > 5)
2420 if (!found_extent) {
2421 ret = remove_extent_backref(trans, extent_root, path);
2423 btrfs_release_path(extent_root, path);
2424 ret = btrfs_search_slot(trans, extent_root,
2427 printk(KERN_ERR "umm, got %d back from search"
2428 ", was looking for %llu\n", ret,
2429 (unsigned long long)bytenr);
2430 btrfs_print_leaf(extent_root, path->nodes[0]);
2433 extent_slot = path->slots[0];
2436 btrfs_print_leaf(extent_root, path->nodes[0]);
2438 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2439 "root %llu gen %llu owner %llu\n",
2440 (unsigned long long)bytenr,
2441 (unsigned long long)root_objectid,
2442 (unsigned long long)ref_generation,
2443 (unsigned long long)owner_objectid);
2446 leaf = path->nodes[0];
2447 ei = btrfs_item_ptr(leaf, extent_slot,
2448 struct btrfs_extent_item);
2449 refs = btrfs_extent_refs(leaf, ei);
2452 btrfs_set_extent_refs(leaf, ei, refs);
2454 btrfs_mark_buffer_dirty(leaf);
2456 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
2457 struct btrfs_extent_ref *ref;
2458 ref = btrfs_item_ptr(leaf, path->slots[0],
2459 struct btrfs_extent_ref);
2460 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
2461 /* if the back ref and the extent are next to each other
2462 * they get deleted below in one shot
2464 path->slots[0] = extent_slot;
2466 } else if (found_extent) {
2467 /* otherwise delete the extent back ref */
2468 ret = remove_extent_backref(trans, extent_root, path);
2470 /* if refs are 0, we need to setup the path for deletion */
2472 btrfs_release_path(extent_root, path);
2473 ret = btrfs_search_slot(trans, extent_root, &key, path,
2484 mutex_lock(&root->fs_info->pinned_mutex);
2485 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2486 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2487 mutex_unlock(&root->fs_info->pinned_mutex);
2492 /* block accounting for super block */
2493 spin_lock(&info->delalloc_lock);
2494 super_used = btrfs_super_bytes_used(&info->super_copy);
2495 btrfs_set_super_bytes_used(&info->super_copy,
2496 super_used - num_bytes);
2498 /* block accounting for root item */
2499 root_used = btrfs_root_used(&root->root_item);
2500 btrfs_set_root_used(&root->root_item,
2501 root_used - num_bytes);
2502 spin_unlock(&info->delalloc_lock);
2503 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2506 btrfs_release_path(extent_root, path);
2508 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2509 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2513 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2517 btrfs_free_path(path);
2518 finish_current_insert(trans, extent_root, 0);
2523 * find all the blocks marked as pending in the radix tree and remove
2524 * them from the extent map
2526 static int del_pending_extents(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *extent_root, int all)
2535 int nr = 0, skipped = 0;
2536 struct extent_io_tree *pending_del;
2537 struct extent_io_tree *extent_ins;
2538 struct pending_extent_op *extent_op;
2539 struct btrfs_fs_info *info = extent_root->fs_info;
2540 struct list_head delete_list;
2542 INIT_LIST_HEAD(&delete_list);
2543 extent_ins = &extent_root->fs_info->extent_ins;
2544 pending_del = &extent_root->fs_info->pending_del;
2547 mutex_lock(&info->extent_ins_mutex);
2549 ret = find_first_extent_bit(pending_del, search, &start, &end,
2552 if (all && skipped && !nr) {
2556 mutex_unlock(&info->extent_ins_mutex);
2560 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2565 if (need_resched()) {
2566 mutex_unlock(&info->extent_ins_mutex);
2568 mutex_lock(&info->extent_ins_mutex);
2575 ret = get_state_private(pending_del, start, &priv);
2577 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2579 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2581 if (!test_range_bit(extent_ins, start, end,
2582 EXTENT_WRITEBACK, 0)) {
2583 list_add_tail(&extent_op->list, &delete_list);
2588 ret = get_state_private(&info->extent_ins, start,
2591 extent_op = (struct pending_extent_op *)
2592 (unsigned long)priv;
2594 clear_extent_bits(&info->extent_ins, start, end,
2595 EXTENT_WRITEBACK, GFP_NOFS);
2597 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2598 list_add_tail(&extent_op->list, &delete_list);
2604 mutex_lock(&extent_root->fs_info->pinned_mutex);
2605 ret = pin_down_bytes(trans, extent_root, start,
2606 end + 1 - start, 0);
2607 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2609 ret = update_block_group(trans, extent_root, start,
2610 end + 1 - start, 0, ret > 0);
2612 unlock_extent(extent_ins, start, end, GFP_NOFS);
2621 if (need_resched()) {
2622 mutex_unlock(&info->extent_ins_mutex);
2624 mutex_lock(&info->extent_ins_mutex);
2629 ret = free_extents(trans, extent_root, &delete_list);
2633 if (all && skipped) {
2634 INIT_LIST_HEAD(&delete_list);
2644 * remove an extent from the root, returns 0 on success
2646 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2647 struct btrfs_root *root,
2648 u64 bytenr, u64 num_bytes, u64 parent,
2649 u64 root_objectid, u64 ref_generation,
2650 u64 owner_objectid, int pin)
2652 struct btrfs_root *extent_root = root->fs_info->extent_root;
2656 WARN_ON(num_bytes < root->sectorsize);
2657 if (root == extent_root) {
2658 struct pending_extent_op *extent_op = NULL;
2660 mutex_lock(&root->fs_info->extent_ins_mutex);
2661 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2662 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2664 ret = get_state_private(&root->fs_info->extent_ins,
2667 extent_op = (struct pending_extent_op *)
2668 (unsigned long)priv;
2671 if (extent_op->type == PENDING_EXTENT_INSERT) {
2672 mutex_unlock(&root->fs_info->extent_ins_mutex);
2678 ref_generation = extent_op->orig_generation;
2679 parent = extent_op->orig_parent;
2682 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2685 extent_op->type = PENDING_EXTENT_DELETE;
2686 extent_op->bytenr = bytenr;
2687 extent_op->num_bytes = num_bytes;
2688 extent_op->parent = parent;
2689 extent_op->orig_parent = parent;
2690 extent_op->generation = ref_generation;
2691 extent_op->orig_generation = ref_generation;
2692 extent_op->level = (int)owner_objectid;
2693 INIT_LIST_HEAD(&extent_op->list);
2696 set_extent_bits(&root->fs_info->pending_del,
2697 bytenr, bytenr + num_bytes - 1,
2698 EXTENT_WRITEBACK, GFP_NOFS);
2699 set_state_private(&root->fs_info->pending_del,
2700 bytenr, (unsigned long)extent_op);
2701 mutex_unlock(&root->fs_info->extent_ins_mutex);
2704 /* if metadata always pin */
2705 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2706 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2707 struct btrfs_block_group_cache *cache;
2709 /* btrfs_free_reserved_extent */
2710 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2712 btrfs_add_free_space(cache, bytenr, num_bytes);
2713 put_block_group(cache);
2714 update_reserved_extents(root, bytenr, num_bytes, 0);
2720 /* if data pin when any transaction has committed this */
2721 if (ref_generation != trans->transid)
2724 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2725 root_objectid, ref_generation,
2726 owner_objectid, pin, pin == 0);
2728 finish_current_insert(trans, root->fs_info->extent_root, 0);
2729 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2730 return ret ? ret : pending_ret;
2733 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2734 struct btrfs_root *root,
2735 u64 bytenr, u64 num_bytes, u64 parent,
2736 u64 root_objectid, u64 ref_generation,
2737 u64 owner_objectid, int pin)
2741 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2742 root_objectid, ref_generation,
2743 owner_objectid, pin);
2747 static u64 stripe_align(struct btrfs_root *root, u64 val)
2749 u64 mask = ((u64)root->stripesize - 1);
2750 u64 ret = (val + mask) & ~mask;
2755 * walks the btree of allocated extents and find a hole of a given size.
2756 * The key ins is changed to record the hole:
2757 * ins->objectid == block start
2758 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2759 * ins->offset == number of blocks
2760 * Any available blocks before search_start are skipped.
2762 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *orig_root,
2764 u64 num_bytes, u64 empty_size,
2765 u64 search_start, u64 search_end,
2766 u64 hint_byte, struct btrfs_key *ins,
2767 u64 exclude_start, u64 exclude_nr,
2771 struct btrfs_root *root = orig_root->fs_info->extent_root;
2772 u64 total_needed = num_bytes;
2773 u64 *last_ptr = NULL;
2774 u64 last_wanted = 0;
2775 struct btrfs_block_group_cache *block_group = NULL;
2776 int chunk_alloc_done = 0;
2777 int empty_cluster = 2 * 1024 * 1024;
2778 int allowed_chunk_alloc = 0;
2779 struct list_head *head = NULL, *cur = NULL;
2782 struct btrfs_space_info *space_info;
2784 WARN_ON(num_bytes < root->sectorsize);
2785 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2789 if (orig_root->ref_cows || empty_size)
2790 allowed_chunk_alloc = 1;
2792 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2793 last_ptr = &root->fs_info->last_alloc;
2794 empty_cluster = 64 * 1024;
2797 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2798 last_ptr = &root->fs_info->last_data_alloc;
2802 hint_byte = *last_ptr;
2803 last_wanted = *last_ptr;
2805 empty_size += empty_cluster;
2809 search_start = max(search_start, first_logical_byte(root, 0));
2810 search_start = max(search_start, hint_byte);
2812 if (last_wanted && search_start != last_wanted) {
2814 empty_size += empty_cluster;
2817 total_needed += empty_size;
2818 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2820 block_group = btrfs_lookup_first_block_group(root->fs_info,
2822 space_info = __find_space_info(root->fs_info, data);
2824 down_read(&space_info->groups_sem);
2826 struct btrfs_free_space *free_space;
2828 * the only way this happens if our hint points to a block
2829 * group thats not of the proper type, while looping this
2830 * should never happen
2836 goto new_group_no_lock;
2838 if (unlikely(!block_group->cached)) {
2839 mutex_lock(&block_group->cache_mutex);
2840 ret = cache_block_group(root, block_group);
2841 mutex_unlock(&block_group->cache_mutex);
2846 mutex_lock(&block_group->alloc_mutex);
2847 if (unlikely(!block_group_bits(block_group, data)))
2850 if (unlikely(block_group->ro))
2853 free_space = btrfs_find_free_space(block_group, search_start,
2856 u64 start = block_group->key.objectid;
2857 u64 end = block_group->key.objectid +
2858 block_group->key.offset;
2860 search_start = stripe_align(root, free_space->offset);
2862 /* move on to the next group */
2863 if (search_start + num_bytes >= search_end)
2866 /* move on to the next group */
2867 if (search_start + num_bytes > end)
2870 if (last_wanted && search_start != last_wanted) {
2871 total_needed += empty_cluster;
2872 empty_size += empty_cluster;
2875 * if search_start is still in this block group
2876 * then we just re-search this block group
2878 if (search_start >= start &&
2879 search_start < end) {
2880 mutex_unlock(&block_group->alloc_mutex);
2884 /* else we go to the next block group */
2888 if (exclude_nr > 0 &&
2889 (search_start + num_bytes > exclude_start &&
2890 search_start < exclude_start + exclude_nr)) {
2891 search_start = exclude_start + exclude_nr;
2893 * if search_start is still in this block group
2894 * then we just re-search this block group
2896 if (search_start >= start &&
2897 search_start < end) {
2898 mutex_unlock(&block_group->alloc_mutex);
2903 /* else we go to the next block group */
2907 ins->objectid = search_start;
2908 ins->offset = num_bytes;
2910 btrfs_remove_free_space_lock(block_group, search_start,
2912 /* we are all good, lets return */
2913 mutex_unlock(&block_group->alloc_mutex);
2917 mutex_unlock(&block_group->alloc_mutex);
2918 put_block_group(block_group);
2921 /* don't try to compare new allocations against the
2922 * last allocation any more
2927 * Here's how this works.
2928 * loop == 0: we were searching a block group via a hint
2929 * and didn't find anything, so we start at
2930 * the head of the block groups and keep searching
2931 * loop == 1: we're searching through all of the block groups
2932 * if we hit the head again we have searched
2933 * all of the block groups for this space and we
2934 * need to try and allocate, if we cant error out.
2935 * loop == 2: we allocated more space and are looping through
2936 * all of the block groups again.
2939 head = &space_info->block_groups;
2942 } else if (loop == 1 && cur == head) {
2945 /* at this point we give up on the empty_size
2946 * allocations and just try to allocate the min
2949 * The extra_loop field was set if an empty_size
2950 * allocation was attempted above, and if this
2951 * is try we need to try the loop again without
2952 * the additional empty_size.
2954 total_needed -= empty_size;
2956 keep_going = extra_loop;
2959 if (allowed_chunk_alloc && !chunk_alloc_done) {
2960 up_read(&space_info->groups_sem);
2961 ret = do_chunk_alloc(trans, root, num_bytes +
2962 2 * 1024 * 1024, data, 1);
2963 down_read(&space_info->groups_sem);
2966 head = &space_info->block_groups;
2968 * we've allocated a new chunk, keep
2972 chunk_alloc_done = 1;
2973 } else if (!allowed_chunk_alloc) {
2974 space_info->force_alloc = 1;
2983 } else if (cur == head) {
2987 block_group = list_entry(cur, struct btrfs_block_group_cache,
2989 atomic_inc(&block_group->count);
2991 search_start = block_group->key.objectid;
2995 /* we found what we needed */
2996 if (ins->objectid) {
2997 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2998 trans->block_group = block_group->key.objectid;
3001 *last_ptr = ins->objectid + ins->offset;
3004 printk(KERN_ERR "btrfs searching for %llu bytes, "
3005 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3006 (unsigned long long)total_needed,
3007 (unsigned long long)num_bytes,
3008 loop, allowed_chunk_alloc);
3012 put_block_group(block_group);
3014 up_read(&space_info->groups_sem);
3018 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3020 struct btrfs_block_group_cache *cache;
3021 struct list_head *l;
3023 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3024 (unsigned long long)(info->total_bytes - info->bytes_used -
3025 info->bytes_pinned - info->bytes_reserved),
3026 (info->full) ? "" : "not ");
3028 down_read(&info->groups_sem);
3029 list_for_each(l, &info->block_groups) {
3030 cache = list_entry(l, struct btrfs_block_group_cache, list);
3031 spin_lock(&cache->lock);
3032 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3033 "%llu pinned %llu reserved\n",
3034 (unsigned long long)cache->key.objectid,
3035 (unsigned long long)cache->key.offset,
3036 (unsigned long long)btrfs_block_group_used(&cache->item),
3037 (unsigned long long)cache->pinned,
3038 (unsigned long long)cache->reserved);
3039 btrfs_dump_free_space(cache, bytes);
3040 spin_unlock(&cache->lock);
3042 up_read(&info->groups_sem);
3045 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
3047 u64 num_bytes, u64 min_alloc_size,
3048 u64 empty_size, u64 hint_byte,
3049 u64 search_end, struct btrfs_key *ins,
3053 u64 search_start = 0;
3055 struct btrfs_fs_info *info = root->fs_info;
3058 alloc_profile = info->avail_data_alloc_bits &
3059 info->data_alloc_profile;
3060 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
3061 } else if (root == root->fs_info->chunk_root) {
3062 alloc_profile = info->avail_system_alloc_bits &
3063 info->system_alloc_profile;
3064 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
3066 alloc_profile = info->avail_metadata_alloc_bits &
3067 info->metadata_alloc_profile;
3068 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
3071 data = btrfs_reduce_alloc_profile(root, data);
3073 * the only place that sets empty_size is btrfs_realloc_node, which
3074 * is not called recursively on allocations
3076 if (empty_size || root->ref_cows) {
3077 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
3078 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3080 BTRFS_BLOCK_GROUP_METADATA |
3081 (info->metadata_alloc_profile &
3082 info->avail_metadata_alloc_bits), 0);
3084 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3085 num_bytes + 2 * 1024 * 1024, data, 0);
3088 WARN_ON(num_bytes < root->sectorsize);
3089 ret = find_free_extent(trans, root, num_bytes, empty_size,
3090 search_start, search_end, hint_byte, ins,
3091 trans->alloc_exclude_start,
3092 trans->alloc_exclude_nr, data);
3094 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3095 num_bytes = num_bytes >> 1;
3096 num_bytes = num_bytes & ~(root->sectorsize - 1);
3097 num_bytes = max(num_bytes, min_alloc_size);
3098 do_chunk_alloc(trans, root->fs_info->extent_root,
3099 num_bytes, data, 1);
3103 struct btrfs_space_info *sinfo;
3105 sinfo = __find_space_info(root->fs_info, data);
3106 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3107 "wanted %llu\n", (unsigned long long)data,
3108 (unsigned long long)num_bytes);
3109 dump_space_info(sinfo, num_bytes);
3116 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3118 struct btrfs_block_group_cache *cache;
3121 cache = btrfs_lookup_block_group(root->fs_info, start);
3123 printk(KERN_ERR "Unable to find block group for %llu\n",
3124 (unsigned long long)start);
3128 ret = btrfs_discard_extent(root, start, len);
3130 btrfs_add_free_space(cache, start, len);
3131 put_block_group(cache);
3132 update_reserved_extents(root, start, len, 0);
3137 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3138 struct btrfs_root *root,
3139 u64 num_bytes, u64 min_alloc_size,
3140 u64 empty_size, u64 hint_byte,
3141 u64 search_end, struct btrfs_key *ins,
3145 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3146 empty_size, hint_byte, search_end, ins,
3148 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3152 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3153 struct btrfs_root *root, u64 parent,
3154 u64 root_objectid, u64 ref_generation,
3155 u64 owner, struct btrfs_key *ins)
3161 u64 num_bytes = ins->offset;
3163 struct btrfs_fs_info *info = root->fs_info;
3164 struct btrfs_root *extent_root = info->extent_root;
3165 struct btrfs_extent_item *extent_item;
3166 struct btrfs_extent_ref *ref;
3167 struct btrfs_path *path;
3168 struct btrfs_key keys[2];
3171 parent = ins->objectid;
3173 /* block accounting for super block */
3174 spin_lock(&info->delalloc_lock);
3175 super_used = btrfs_super_bytes_used(&info->super_copy);
3176 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
3178 /* block accounting for root item */
3179 root_used = btrfs_root_used(&root->root_item);
3180 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
3181 spin_unlock(&info->delalloc_lock);
3183 if (root == extent_root) {
3184 struct pending_extent_op *extent_op;
3186 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3189 extent_op->type = PENDING_EXTENT_INSERT;
3190 extent_op->bytenr = ins->objectid;
3191 extent_op->num_bytes = ins->offset;
3192 extent_op->parent = parent;
3193 extent_op->orig_parent = 0;
3194 extent_op->generation = ref_generation;
3195 extent_op->orig_generation = 0;
3196 extent_op->level = (int)owner;
3197 INIT_LIST_HEAD(&extent_op->list);
3200 mutex_lock(&root->fs_info->extent_ins_mutex);
3201 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3202 ins->objectid + ins->offset - 1,
3203 EXTENT_WRITEBACK, GFP_NOFS);
3204 set_state_private(&root->fs_info->extent_ins,
3205 ins->objectid, (unsigned long)extent_op);
3206 mutex_unlock(&root->fs_info->extent_ins_mutex);
3210 memcpy(&keys[0], ins, sizeof(*ins));
3211 keys[1].objectid = ins->objectid;
3212 keys[1].type = BTRFS_EXTENT_REF_KEY;
3213 keys[1].offset = parent;
3214 sizes[0] = sizeof(*extent_item);
3215 sizes[1] = sizeof(*ref);
3217 path = btrfs_alloc_path();
3220 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3224 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3225 struct btrfs_extent_item);
3226 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3227 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3228 struct btrfs_extent_ref);
3230 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3231 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3232 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
3233 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
3235 btrfs_mark_buffer_dirty(path->nodes[0]);
3237 trans->alloc_exclude_start = 0;
3238 trans->alloc_exclude_nr = 0;
3239 btrfs_free_path(path);
3240 finish_current_insert(trans, extent_root, 0);
3241 pending_ret = del_pending_extents(trans, extent_root, 0);
3251 ret = update_block_group(trans, root, ins->objectid,
3254 printk(KERN_ERR "btrfs update block group failed for %llu "
3255 "%llu\n", (unsigned long long)ins->objectid,
3256 (unsigned long long)ins->offset);
3263 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3264 struct btrfs_root *root, u64 parent,
3265 u64 root_objectid, u64 ref_generation,
3266 u64 owner, struct btrfs_key *ins)
3270 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3272 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3273 ref_generation, owner, ins);
3274 update_reserved_extents(root, ins->objectid, ins->offset, 0);
3279 * this is used by the tree logging recovery code. It records that
3280 * an extent has been allocated and makes sure to clear the free
3281 * space cache bits as well
3283 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3284 struct btrfs_root *root, u64 parent,
3285 u64 root_objectid, u64 ref_generation,
3286 u64 owner, struct btrfs_key *ins)
3289 struct btrfs_block_group_cache *block_group;
3291 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3292 mutex_lock(&block_group->cache_mutex);
3293 cache_block_group(root, block_group);
3294 mutex_unlock(&block_group->cache_mutex);
3296 ret = btrfs_remove_free_space(block_group, ins->objectid,
3299 put_block_group(block_group);
3300 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3301 ref_generation, owner, ins);
3306 * finds a free extent and does all the dirty work required for allocation
3307 * returns the key for the extent through ins, and a tree buffer for
3308 * the first block of the extent through buf.
3310 * returns 0 if everything worked, non-zero otherwise.
3312 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3313 struct btrfs_root *root,
3314 u64 num_bytes, u64 parent, u64 min_alloc_size,
3315 u64 root_objectid, u64 ref_generation,
3316 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3317 u64 search_end, struct btrfs_key *ins, u64 data)
3321 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3322 min_alloc_size, empty_size, hint_byte,
3323 search_end, ins, data);
3325 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3326 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3327 root_objectid, ref_generation,
3328 owner_objectid, ins);
3332 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3337 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3338 struct btrfs_root *root,
3339 u64 bytenr, u32 blocksize)
3341 struct extent_buffer *buf;
3343 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3345 return ERR_PTR(-ENOMEM);
3346 btrfs_set_header_generation(buf, trans->transid);
3347 btrfs_tree_lock(buf);
3348 clean_tree_block(trans, root, buf);
3349 btrfs_set_buffer_uptodate(buf);
3350 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3351 set_extent_dirty(&root->dirty_log_pages, buf->start,
3352 buf->start + buf->len - 1, GFP_NOFS);
3354 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3355 buf->start + buf->len - 1, GFP_NOFS);
3357 trans->blocks_used++;
3362 * helper function to allocate a block for a given tree
3363 * returns the tree buffer or NULL.
3365 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root,
3367 u32 blocksize, u64 parent,
3374 struct btrfs_key ins;
3376 struct extent_buffer *buf;
3378 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3379 root_objectid, ref_generation, level,
3380 empty_size, hint, (u64)-1, &ins, 0);
3383 return ERR_PTR(ret);
3386 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
3390 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3391 struct btrfs_root *root, struct extent_buffer *leaf)
3394 u64 leaf_generation;
3395 struct btrfs_key key;
3396 struct btrfs_file_extent_item *fi;
3401 BUG_ON(!btrfs_is_leaf(leaf));
3402 nritems = btrfs_header_nritems(leaf);
3403 leaf_owner = btrfs_header_owner(leaf);
3404 leaf_generation = btrfs_header_generation(leaf);
3406 for (i = 0; i < nritems; i++) {
3410 btrfs_item_key_to_cpu(leaf, &key, i);
3411 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3413 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3414 if (btrfs_file_extent_type(leaf, fi) ==
3415 BTRFS_FILE_EXTENT_INLINE)
3418 * FIXME make sure to insert a trans record that
3419 * repeats the snapshot del on crash
3421 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3422 if (disk_bytenr == 0)
3425 ret = __btrfs_free_extent(trans, root, disk_bytenr,
3426 btrfs_file_extent_disk_num_bytes(leaf, fi),
3427 leaf->start, leaf_owner, leaf_generation,
3431 atomic_inc(&root->fs_info->throttle_gen);
3432 wake_up(&root->fs_info->transaction_throttle);
3438 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_leaf_ref *ref)
3444 struct btrfs_extent_info *info = ref->extents;
3446 for (i = 0; i < ref->nritems; i++) {
3447 ret = __btrfs_free_extent(trans, root, info->bytenr,
3448 info->num_bytes, ref->bytenr,
3449 ref->owner, ref->generation,
3452 atomic_inc(&root->fs_info->throttle_gen);
3453 wake_up(&root->fs_info->transaction_throttle);
3463 static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3468 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
3471 #if 0 /* some debugging code in case we see problems here */
3472 /* if the refs count is one, it won't get increased again. But
3473 * if the ref count is > 1, someone may be decreasing it at
3474 * the same time we are.
3477 struct extent_buffer *eb = NULL;
3478 eb = btrfs_find_create_tree_block(root, start, len);
3480 btrfs_tree_lock(eb);
3482 mutex_lock(&root->fs_info->alloc_mutex);
3483 ret = lookup_extent_ref(NULL, root, start, len, refs);
3485 mutex_unlock(&root->fs_info->alloc_mutex);
3488 btrfs_tree_unlock(eb);
3489 free_extent_buffer(eb);
3492 printk(KERN_ERR "btrfs block %llu went down to one "
3493 "during drop_snap\n", (unsigned long long)start);
3504 * helper function for drop_snapshot, this walks down the tree dropping ref
3505 * counts as it goes.
3507 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3508 struct btrfs_root *root,
3509 struct btrfs_path *path, int *level)
3515 struct extent_buffer *next;
3516 struct extent_buffer *cur;
3517 struct extent_buffer *parent;
3518 struct btrfs_leaf_ref *ref;
3523 WARN_ON(*level < 0);
3524 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3525 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
3526 path->nodes[*level]->len, &refs);
3532 * walk down to the last node level and free all the leaves
3534 while (*level >= 0) {
3535 WARN_ON(*level < 0);
3536 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3537 cur = path->nodes[*level];
3539 if (btrfs_header_level(cur) != *level)
3542 if (path->slots[*level] >=
3543 btrfs_header_nritems(cur))
3546 ret = btrfs_drop_leaf_ref(trans, root, cur);
3550 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3551 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3552 blocksize = btrfs_level_size(root, *level - 1);
3554 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3557 parent = path->nodes[*level];
3558 root_owner = btrfs_header_owner(parent);
3559 root_gen = btrfs_header_generation(parent);
3560 path->slots[*level]++;
3562 ret = __btrfs_free_extent(trans, root, bytenr,
3563 blocksize, parent->start,
3564 root_owner, root_gen,
3568 atomic_inc(&root->fs_info->throttle_gen);
3569 wake_up(&root->fs_info->transaction_throttle);
3575 * at this point, we have a single ref, and since the
3576 * only place referencing this extent is a dead root
3577 * the reference count should never go higher.
3578 * So, we don't need to check it again
3581 ref = btrfs_lookup_leaf_ref(root, bytenr);
3582 if (ref && ref->generation != ptr_gen) {
3583 btrfs_free_leaf_ref(root, ref);
3587 ret = cache_drop_leaf_ref(trans, root, ref);
3589 btrfs_remove_leaf_ref(root, ref);
3590 btrfs_free_leaf_ref(root, ref);
3595 next = btrfs_find_tree_block(root, bytenr, blocksize);
3596 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3597 free_extent_buffer(next);
3599 next = read_tree_block(root, bytenr, blocksize,
3604 * this is a debugging check and can go away
3605 * the ref should never go all the way down to 1
3608 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3614 WARN_ON(*level <= 0);
3615 if (path->nodes[*level-1])
3616 free_extent_buffer(path->nodes[*level-1]);
3617 path->nodes[*level-1] = next;
3618 *level = btrfs_header_level(next);
3619 path->slots[*level] = 0;
3623 WARN_ON(*level < 0);
3624 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3626 if (path->nodes[*level] == root->node) {
3627 parent = path->nodes[*level];
3628 bytenr = path->nodes[*level]->start;
3630 parent = path->nodes[*level + 1];
3631 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3634 blocksize = btrfs_level_size(root, *level);
3635 root_owner = btrfs_header_owner(parent);
3636 root_gen = btrfs_header_generation(parent);
3638 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3639 parent->start, root_owner, root_gen,
3641 free_extent_buffer(path->nodes[*level]);
3642 path->nodes[*level] = NULL;
3651 * helper function for drop_subtree, this function is similar to
3652 * walk_down_tree. The main difference is that it checks reference
3653 * counts while tree blocks are locked.
3655 static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
3657 struct btrfs_path *path, int *level)
3659 struct extent_buffer *next;
3660 struct extent_buffer *cur;
3661 struct extent_buffer *parent;
3668 cur = path->nodes[*level];
3669 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3675 while (*level >= 0) {
3676 cur = path->nodes[*level];
3678 ret = btrfs_drop_leaf_ref(trans, root, cur);
3680 clean_tree_block(trans, root, cur);
3683 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3684 clean_tree_block(trans, root, cur);
3688 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3689 blocksize = btrfs_level_size(root, *level - 1);
3690 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3692 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3693 btrfs_tree_lock(next);
3695 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3699 parent = path->nodes[*level];
3700 ret = btrfs_free_extent(trans, root, bytenr,
3701 blocksize, parent->start,
3702 btrfs_header_owner(parent),
3703 btrfs_header_generation(parent),
3706 path->slots[*level]++;
3707 btrfs_tree_unlock(next);
3708 free_extent_buffer(next);
3712 *level = btrfs_header_level(next);
3713 path->nodes[*level] = next;
3714 path->slots[*level] = 0;
3715 path->locks[*level] = 1;
3719 parent = path->nodes[*level + 1];
3720 bytenr = path->nodes[*level]->start;
3721 blocksize = path->nodes[*level]->len;
3723 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3724 parent->start, btrfs_header_owner(parent),
3725 btrfs_header_generation(parent), *level, 1);
3728 if (path->locks[*level]) {
3729 btrfs_tree_unlock(path->nodes[*level]);
3730 path->locks[*level] = 0;
3732 free_extent_buffer(path->nodes[*level]);
3733 path->nodes[*level] = NULL;
3740 * helper for dropping snapshots. This walks back up the tree in the path
3741 * to find the first node higher up where we haven't yet gone through
3744 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
3745 struct btrfs_root *root,
3746 struct btrfs_path *path,
3747 int *level, int max_level)
3751 struct btrfs_root_item *root_item = &root->root_item;
3756 for (i = *level; i < max_level && path->nodes[i]; i++) {
3757 slot = path->slots[i];
3758 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3759 struct extent_buffer *node;
3760 struct btrfs_disk_key disk_key;
3761 node = path->nodes[i];
3764 WARN_ON(*level == 0);
3765 btrfs_node_key(node, &disk_key, path->slots[i]);
3766 memcpy(&root_item->drop_progress,
3767 &disk_key, sizeof(disk_key));
3768 root_item->drop_level = i;
3771 struct extent_buffer *parent;
3772 if (path->nodes[*level] == root->node)
3773 parent = path->nodes[*level];
3775 parent = path->nodes[*level + 1];
3777 root_owner = btrfs_header_owner(parent);
3778 root_gen = btrfs_header_generation(parent);
3780 clean_tree_block(trans, root, path->nodes[*level]);
3781 ret = btrfs_free_extent(trans, root,
3782 path->nodes[*level]->start,
3783 path->nodes[*level]->len,
3784 parent->start, root_owner,
3785 root_gen, *level, 1);
3787 if (path->locks[*level]) {
3788 btrfs_tree_unlock(path->nodes[*level]);
3789 path->locks[*level] = 0;
3791 free_extent_buffer(path->nodes[*level]);
3792 path->nodes[*level] = NULL;
3800 * drop the reference count on the tree rooted at 'snap'. This traverses
3801 * the tree freeing any blocks that have a ref count of zero after being
3804 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3810 struct btrfs_path *path;
3813 struct btrfs_root_item *root_item = &root->root_item;
3815 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3816 path = btrfs_alloc_path();
3819 level = btrfs_header_level(root->node);
3821 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3822 path->nodes[level] = root->node;
3823 extent_buffer_get(root->node);
3824 path->slots[level] = 0;
3826 struct btrfs_key key;
3827 struct btrfs_disk_key found_key;
3828 struct extent_buffer *node;
3830 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3831 level = root_item->drop_level;
3832 path->lowest_level = level;
3833 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3838 node = path->nodes[level];
3839 btrfs_node_key(node, &found_key, path->slots[level]);
3840 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3841 sizeof(found_key)));
3843 * unlock our path, this is safe because only this
3844 * function is allowed to delete this snapshot
3846 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3847 if (path->nodes[i] && path->locks[i]) {
3849 btrfs_tree_unlock(path->nodes[i]);
3854 wret = walk_down_tree(trans, root, path, &level);
3860 wret = walk_up_tree(trans, root, path, &level,
3866 if (trans->transaction->in_commit) {
3870 atomic_inc(&root->fs_info->throttle_gen);
3871 wake_up(&root->fs_info->transaction_throttle);
3873 for (i = 0; i <= orig_level; i++) {
3874 if (path->nodes[i]) {
3875 free_extent_buffer(path->nodes[i]);
3876 path->nodes[i] = NULL;
3880 btrfs_free_path(path);
3884 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root,
3886 struct extent_buffer *node,
3887 struct extent_buffer *parent)
3889 struct btrfs_path *path;
3895 path = btrfs_alloc_path();
3898 BUG_ON(!btrfs_tree_locked(parent));
3899 parent_level = btrfs_header_level(parent);
3900 extent_buffer_get(parent);
3901 path->nodes[parent_level] = parent;
3902 path->slots[parent_level] = btrfs_header_nritems(parent);
3904 BUG_ON(!btrfs_tree_locked(node));
3905 level = btrfs_header_level(node);
3906 extent_buffer_get(node);
3907 path->nodes[level] = node;
3908 path->slots[level] = 0;
3911 wret = walk_down_subtree(trans, root, path, &level);
3917 wret = walk_up_tree(trans, root, path, &level, parent_level);
3924 btrfs_free_path(path);
3928 static unsigned long calc_ra(unsigned long start, unsigned long last,
3931 return min(last, start + nr - 1);
3934 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
3939 unsigned long first_index;
3940 unsigned long last_index;
3943 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3944 struct file_ra_state *ra;
3945 struct btrfs_ordered_extent *ordered;
3946 unsigned int total_read = 0;
3947 unsigned int total_dirty = 0;
3950 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3952 mutex_lock(&inode->i_mutex);
3953 first_index = start >> PAGE_CACHE_SHIFT;
3954 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3956 /* make sure the dirty trick played by the caller work */
3957 ret = invalidate_inode_pages2_range(inode->i_mapping,
3958 first_index, last_index);
3962 file_ra_state_init(ra, inode->i_mapping);
3964 for (i = first_index ; i <= last_index; i++) {
3965 if (total_read % ra->ra_pages == 0) {
3966 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3967 calc_ra(i, last_index, ra->ra_pages));
3971 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3973 page = grab_cache_page(inode->i_mapping, i);
3978 if (!PageUptodate(page)) {
3979 btrfs_readpage(NULL, page);
3981 if (!PageUptodate(page)) {
3983 page_cache_release(page);
3988 wait_on_page_writeback(page);
3990 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3991 page_end = page_start + PAGE_CACHE_SIZE - 1;
3992 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3994 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3996 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3998 page_cache_release(page);
3999 btrfs_start_ordered_extent(inode, ordered, 1);
4000 btrfs_put_ordered_extent(ordered);
4003 set_page_extent_mapped(page);
4005 if (i == first_index)
4006 set_extent_bits(io_tree, page_start, page_end,
4007 EXTENT_BOUNDARY, GFP_NOFS);
4008 btrfs_set_extent_delalloc(inode, page_start, page_end);
4010 set_page_dirty(page);
4013 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4015 page_cache_release(page);
4020 mutex_unlock(&inode->i_mutex);
4021 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
4025 static noinline int relocate_data_extent(struct inode *reloc_inode,
4026 struct btrfs_key *extent_key,
4029 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4030 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4031 struct extent_map *em;
4032 u64 start = extent_key->objectid - offset;
4033 u64 end = start + extent_key->offset - 1;
4035 em = alloc_extent_map(GFP_NOFS);
4036 BUG_ON(!em || IS_ERR(em));
4039 em->len = extent_key->offset;
4040 em->block_len = extent_key->offset;
4041 em->block_start = extent_key->objectid;
4042 em->bdev = root->fs_info->fs_devices->latest_bdev;
4043 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4045 /* setup extent map to cheat btrfs_readpage */
4046 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4049 spin_lock(&em_tree->lock);
4050 ret = add_extent_mapping(em_tree, em);
4051 spin_unlock(&em_tree->lock);
4052 if (ret != -EEXIST) {
4053 free_extent_map(em);
4056 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4058 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4060 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4063 struct btrfs_ref_path {
4065 u64 nodes[BTRFS_MAX_LEVEL];
4067 u64 root_generation;
4074 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4075 u64 new_nodes[BTRFS_MAX_LEVEL];
4078 struct disk_extent {
4089 static int is_cowonly_root(u64 root_objectid)
4091 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4092 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4093 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4094 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4095 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4096 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
4101 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
4102 struct btrfs_root *extent_root,
4103 struct btrfs_ref_path *ref_path,
4106 struct extent_buffer *leaf;
4107 struct btrfs_path *path;
4108 struct btrfs_extent_ref *ref;
4109 struct btrfs_key key;
4110 struct btrfs_key found_key;
4116 path = btrfs_alloc_path();
4121 ref_path->lowest_level = -1;
4122 ref_path->current_level = -1;
4123 ref_path->shared_level = -1;
4127 level = ref_path->current_level - 1;
4128 while (level >= -1) {
4130 if (level < ref_path->lowest_level)
4134 bytenr = ref_path->nodes[level];
4136 bytenr = ref_path->extent_start;
4137 BUG_ON(bytenr == 0);
4139 parent = ref_path->nodes[level + 1];
4140 ref_path->nodes[level + 1] = 0;
4141 ref_path->current_level = level;
4142 BUG_ON(parent == 0);
4144 key.objectid = bytenr;
4145 key.offset = parent + 1;
4146 key.type = BTRFS_EXTENT_REF_KEY;
4148 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4153 leaf = path->nodes[0];
4154 nritems = btrfs_header_nritems(leaf);
4155 if (path->slots[0] >= nritems) {
4156 ret = btrfs_next_leaf(extent_root, path);
4161 leaf = path->nodes[0];
4164 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4165 if (found_key.objectid == bytenr &&
4166 found_key.type == BTRFS_EXTENT_REF_KEY) {
4167 if (level < ref_path->shared_level)
4168 ref_path->shared_level = level;
4173 btrfs_release_path(extent_root, path);
4176 /* reached lowest level */
4180 level = ref_path->current_level;
4181 while (level < BTRFS_MAX_LEVEL - 1) {
4185 bytenr = ref_path->nodes[level];
4187 bytenr = ref_path->extent_start;
4189 BUG_ON(bytenr == 0);
4191 key.objectid = bytenr;
4193 key.type = BTRFS_EXTENT_REF_KEY;
4195 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4199 leaf = path->nodes[0];
4200 nritems = btrfs_header_nritems(leaf);
4201 if (path->slots[0] >= nritems) {
4202 ret = btrfs_next_leaf(extent_root, path);
4206 /* the extent was freed by someone */
4207 if (ref_path->lowest_level == level)
4209 btrfs_release_path(extent_root, path);
4212 leaf = path->nodes[0];
4215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4216 if (found_key.objectid != bytenr ||
4217 found_key.type != BTRFS_EXTENT_REF_KEY) {
4218 /* the extent was freed by someone */
4219 if (ref_path->lowest_level == level) {
4223 btrfs_release_path(extent_root, path);
4227 ref = btrfs_item_ptr(leaf, path->slots[0],
4228 struct btrfs_extent_ref);
4229 ref_objectid = btrfs_ref_objectid(leaf, ref);
4230 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4232 level = (int)ref_objectid;
4233 BUG_ON(level >= BTRFS_MAX_LEVEL);
4234 ref_path->lowest_level = level;
4235 ref_path->current_level = level;
4236 ref_path->nodes[level] = bytenr;
4238 WARN_ON(ref_objectid != level);
4241 WARN_ON(level != -1);
4245 if (ref_path->lowest_level == level) {
4246 ref_path->owner_objectid = ref_objectid;
4247 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4251 * the block is tree root or the block isn't in reference
4254 if (found_key.objectid == found_key.offset ||
4255 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4256 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4257 ref_path->root_generation =
4258 btrfs_ref_generation(leaf, ref);
4260 /* special reference from the tree log */
4261 ref_path->nodes[0] = found_key.offset;
4262 ref_path->current_level = 0;
4269 BUG_ON(ref_path->nodes[level] != 0);
4270 ref_path->nodes[level] = found_key.offset;
4271 ref_path->current_level = level;
4274 * the reference was created in the running transaction,
4275 * no need to continue walking up.
4277 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4278 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4279 ref_path->root_generation =
4280 btrfs_ref_generation(leaf, ref);
4285 btrfs_release_path(extent_root, path);
4288 /* reached max tree level, but no tree root found. */
4291 btrfs_free_path(path);
4295 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4296 struct btrfs_root *extent_root,
4297 struct btrfs_ref_path *ref_path,
4300 memset(ref_path, 0, sizeof(*ref_path));
4301 ref_path->extent_start = extent_start;
4303 return __next_ref_path(trans, extent_root, ref_path, 1);
4306 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4307 struct btrfs_root *extent_root,
4308 struct btrfs_ref_path *ref_path)
4310 return __next_ref_path(trans, extent_root, ref_path, 0);
4313 static noinline int get_new_locations(struct inode *reloc_inode,
4314 struct btrfs_key *extent_key,
4315 u64 offset, int no_fragment,
4316 struct disk_extent **extents,
4319 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4320 struct btrfs_path *path;
4321 struct btrfs_file_extent_item *fi;
4322 struct extent_buffer *leaf;
4323 struct disk_extent *exts = *extents;
4324 struct btrfs_key found_key;
4329 int max = *nr_extents;
4332 WARN_ON(!no_fragment && *extents);
4335 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4340 path = btrfs_alloc_path();
4343 cur_pos = extent_key->objectid - offset;
4344 last_byte = extent_key->objectid + extent_key->offset;
4345 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4355 leaf = path->nodes[0];
4356 nritems = btrfs_header_nritems(leaf);
4357 if (path->slots[0] >= nritems) {
4358 ret = btrfs_next_leaf(root, path);
4363 leaf = path->nodes[0];
4366 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4367 if (found_key.offset != cur_pos ||
4368 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4369 found_key.objectid != reloc_inode->i_ino)
4372 fi = btrfs_item_ptr(leaf, path->slots[0],
4373 struct btrfs_file_extent_item);
4374 if (btrfs_file_extent_type(leaf, fi) !=
4375 BTRFS_FILE_EXTENT_REG ||
4376 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4380 struct disk_extent *old = exts;
4382 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4383 memcpy(exts, old, sizeof(*exts) * nr);
4384 if (old != *extents)
4388 exts[nr].disk_bytenr =
4389 btrfs_file_extent_disk_bytenr(leaf, fi);
4390 exts[nr].disk_num_bytes =
4391 btrfs_file_extent_disk_num_bytes(leaf, fi);
4392 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4393 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4394 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4395 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4396 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4397 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4399 BUG_ON(exts[nr].offset > 0);
4400 BUG_ON(exts[nr].compression || exts[nr].encryption);
4401 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4403 cur_pos += exts[nr].num_bytes;
4406 if (cur_pos + offset >= last_byte)
4416 BUG_ON(cur_pos + offset > last_byte);
4417 if (cur_pos + offset < last_byte) {
4423 btrfs_free_path(path);
4425 if (exts != *extents)
4434 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root,
4436 struct btrfs_path *path,
4437 struct btrfs_key *extent_key,
4438 struct btrfs_key *leaf_key,
4439 struct btrfs_ref_path *ref_path,
4440 struct disk_extent *new_extents,
4443 struct extent_buffer *leaf;
4444 struct btrfs_file_extent_item *fi;
4445 struct inode *inode = NULL;
4446 struct btrfs_key key;
4454 int extent_locked = 0;
4458 memcpy(&key, leaf_key, sizeof(key));
4459 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
4460 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4461 if (key.objectid < ref_path->owner_objectid ||
4462 (key.objectid == ref_path->owner_objectid &&
4463 key.type < BTRFS_EXTENT_DATA_KEY)) {
4464 key.objectid = ref_path->owner_objectid;
4465 key.type = BTRFS_EXTENT_DATA_KEY;
4471 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4475 leaf = path->nodes[0];
4476 nritems = btrfs_header_nritems(leaf);
4478 if (extent_locked && ret > 0) {
4480 * the file extent item was modified by someone
4481 * before the extent got locked.
4483 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4484 lock_end, GFP_NOFS);
4488 if (path->slots[0] >= nritems) {
4489 if (++nr_scaned > 2)
4492 BUG_ON(extent_locked);
4493 ret = btrfs_next_leaf(root, path);
4498 leaf = path->nodes[0];
4499 nritems = btrfs_header_nritems(leaf);
4502 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4504 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4505 if ((key.objectid > ref_path->owner_objectid) ||
4506 (key.objectid == ref_path->owner_objectid &&
4507 key.type > BTRFS_EXTENT_DATA_KEY) ||
4508 (key.offset >= first_pos + extent_key->offset))
4512 if (inode && key.objectid != inode->i_ino) {
4513 BUG_ON(extent_locked);
4514 btrfs_release_path(root, path);
4515 mutex_unlock(&inode->i_mutex);
4521 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4526 fi = btrfs_item_ptr(leaf, path->slots[0],
4527 struct btrfs_file_extent_item);
4528 extent_type = btrfs_file_extent_type(leaf, fi);
4529 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4530 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4531 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4532 extent_key->objectid)) {
4538 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4539 ext_offset = btrfs_file_extent_offset(leaf, fi);
4541 if (first_pos > key.offset - ext_offset)
4542 first_pos = key.offset - ext_offset;
4544 if (!extent_locked) {
4545 lock_start = key.offset;
4546 lock_end = lock_start + num_bytes - 1;
4548 if (lock_start > key.offset ||
4549 lock_end + 1 < key.offset + num_bytes) {
4550 unlock_extent(&BTRFS_I(inode)->io_tree,
4551 lock_start, lock_end, GFP_NOFS);
4557 btrfs_release_path(root, path);
4559 inode = btrfs_iget_locked(root->fs_info->sb,
4560 key.objectid, root);
4561 if (inode->i_state & I_NEW) {
4562 BTRFS_I(inode)->root = root;
4563 BTRFS_I(inode)->location.objectid =
4565 BTRFS_I(inode)->location.type =
4566 BTRFS_INODE_ITEM_KEY;
4567 BTRFS_I(inode)->location.offset = 0;
4568 btrfs_read_locked_inode(inode);
4569 unlock_new_inode(inode);
4572 * some code call btrfs_commit_transaction while
4573 * holding the i_mutex, so we can't use mutex_lock
4576 if (is_bad_inode(inode) ||
4577 !mutex_trylock(&inode->i_mutex)) {
4580 key.offset = (u64)-1;
4585 if (!extent_locked) {
4586 struct btrfs_ordered_extent *ordered;
4588 btrfs_release_path(root, path);
4590 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4591 lock_end, GFP_NOFS);
4592 ordered = btrfs_lookup_first_ordered_extent(inode,
4595 ordered->file_offset <= lock_end &&
4596 ordered->file_offset + ordered->len > lock_start) {
4597 unlock_extent(&BTRFS_I(inode)->io_tree,
4598 lock_start, lock_end, GFP_NOFS);
4599 btrfs_start_ordered_extent(inode, ordered, 1);
4600 btrfs_put_ordered_extent(ordered);
4601 key.offset += num_bytes;
4605 btrfs_put_ordered_extent(ordered);
4611 if (nr_extents == 1) {
4612 /* update extent pointer in place */
4613 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4614 new_extents[0].disk_bytenr);
4615 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4616 new_extents[0].disk_num_bytes);
4617 btrfs_mark_buffer_dirty(leaf);
4619 btrfs_drop_extent_cache(inode, key.offset,
4620 key.offset + num_bytes - 1, 0);
4622 ret = btrfs_inc_extent_ref(trans, root,
4623 new_extents[0].disk_bytenr,
4624 new_extents[0].disk_num_bytes,
4626 root->root_key.objectid,
4631 ret = btrfs_free_extent(trans, root,
4632 extent_key->objectid,
4635 btrfs_header_owner(leaf),
4636 btrfs_header_generation(leaf),
4640 btrfs_release_path(root, path);
4641 key.offset += num_bytes;
4649 * drop old extent pointer at first, then insert the
4650 * new pointers one bye one
4652 btrfs_release_path(root, path);
4653 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4654 key.offset + num_bytes,
4655 key.offset, &alloc_hint);
4658 for (i = 0; i < nr_extents; i++) {
4659 if (ext_offset >= new_extents[i].num_bytes) {
4660 ext_offset -= new_extents[i].num_bytes;
4663 extent_len = min(new_extents[i].num_bytes -
4664 ext_offset, num_bytes);
4666 ret = btrfs_insert_empty_item(trans, root,
4671 leaf = path->nodes[0];
4672 fi = btrfs_item_ptr(leaf, path->slots[0],
4673 struct btrfs_file_extent_item);
4674 btrfs_set_file_extent_generation(leaf, fi,
4676 btrfs_set_file_extent_type(leaf, fi,
4677 BTRFS_FILE_EXTENT_REG);
4678 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4679 new_extents[i].disk_bytenr);
4680 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4681 new_extents[i].disk_num_bytes);
4682 btrfs_set_file_extent_ram_bytes(leaf, fi,
4683 new_extents[i].ram_bytes);
4685 btrfs_set_file_extent_compression(leaf, fi,
4686 new_extents[i].compression);
4687 btrfs_set_file_extent_encryption(leaf, fi,
4688 new_extents[i].encryption);
4689 btrfs_set_file_extent_other_encoding(leaf, fi,
4690 new_extents[i].other_encoding);
4692 btrfs_set_file_extent_num_bytes(leaf, fi,
4694 ext_offset += new_extents[i].offset;
4695 btrfs_set_file_extent_offset(leaf, fi,
4697 btrfs_mark_buffer_dirty(leaf);
4699 btrfs_drop_extent_cache(inode, key.offset,
4700 key.offset + extent_len - 1, 0);
4702 ret = btrfs_inc_extent_ref(trans, root,
4703 new_extents[i].disk_bytenr,
4704 new_extents[i].disk_num_bytes,
4706 root->root_key.objectid,
4707 trans->transid, key.objectid);
4709 btrfs_release_path(root, path);
4711 inode_add_bytes(inode, extent_len);
4714 num_bytes -= extent_len;
4715 key.offset += extent_len;
4720 BUG_ON(i >= nr_extents);
4724 if (extent_locked) {
4725 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4726 lock_end, GFP_NOFS);
4730 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4731 key.offset >= first_pos + extent_key->offset)
4738 btrfs_release_path(root, path);
4740 mutex_unlock(&inode->i_mutex);
4741 if (extent_locked) {
4742 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4743 lock_end, GFP_NOFS);
4750 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4751 struct btrfs_root *root,
4752 struct extent_buffer *buf, u64 orig_start)
4757 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4758 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4760 level = btrfs_header_level(buf);
4762 struct btrfs_leaf_ref *ref;
4763 struct btrfs_leaf_ref *orig_ref;
4765 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4769 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4771 btrfs_free_leaf_ref(root, orig_ref);
4775 ref->nritems = orig_ref->nritems;
4776 memcpy(ref->extents, orig_ref->extents,
4777 sizeof(ref->extents[0]) * ref->nritems);
4779 btrfs_free_leaf_ref(root, orig_ref);
4781 ref->root_gen = trans->transid;
4782 ref->bytenr = buf->start;
4783 ref->owner = btrfs_header_owner(buf);
4784 ref->generation = btrfs_header_generation(buf);
4785 ret = btrfs_add_leaf_ref(root, ref, 0);
4787 btrfs_free_leaf_ref(root, ref);
4792 static noinline int invalidate_extent_cache(struct btrfs_root *root,
4793 struct extent_buffer *leaf,
4794 struct btrfs_block_group_cache *group,
4795 struct btrfs_root *target_root)
4797 struct btrfs_key key;
4798 struct inode *inode = NULL;
4799 struct btrfs_file_extent_item *fi;
4801 u64 skip_objectid = 0;
4805 nritems = btrfs_header_nritems(leaf);
4806 for (i = 0; i < nritems; i++) {
4807 btrfs_item_key_to_cpu(leaf, &key, i);
4808 if (key.objectid == skip_objectid ||
4809 key.type != BTRFS_EXTENT_DATA_KEY)
4811 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4812 if (btrfs_file_extent_type(leaf, fi) ==
4813 BTRFS_FILE_EXTENT_INLINE)
4815 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4817 if (!inode || inode->i_ino != key.objectid) {
4819 inode = btrfs_ilookup(target_root->fs_info->sb,
4820 key.objectid, target_root, 1);
4823 skip_objectid = key.objectid;
4826 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4828 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4829 key.offset + num_bytes - 1, GFP_NOFS);
4830 btrfs_drop_extent_cache(inode, key.offset,
4831 key.offset + num_bytes - 1, 1);
4832 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4833 key.offset + num_bytes - 1, GFP_NOFS);
4840 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4841 struct btrfs_root *root,
4842 struct extent_buffer *leaf,
4843 struct btrfs_block_group_cache *group,
4844 struct inode *reloc_inode)
4846 struct btrfs_key key;
4847 struct btrfs_key extent_key;
4848 struct btrfs_file_extent_item *fi;
4849 struct btrfs_leaf_ref *ref;
4850 struct disk_extent *new_extent;
4859 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4860 BUG_ON(!new_extent);
4862 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4866 nritems = btrfs_header_nritems(leaf);
4867 for (i = 0; i < nritems; i++) {
4868 btrfs_item_key_to_cpu(leaf, &key, i);
4869 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4871 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4872 if (btrfs_file_extent_type(leaf, fi) ==
4873 BTRFS_FILE_EXTENT_INLINE)
4875 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4876 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4881 if (bytenr >= group->key.objectid + group->key.offset ||
4882 bytenr + num_bytes <= group->key.objectid)
4885 extent_key.objectid = bytenr;
4886 extent_key.offset = num_bytes;
4887 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4889 ret = get_new_locations(reloc_inode, &extent_key,
4890 group->key.objectid, 1,
4891 &new_extent, &nr_extent);
4896 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4897 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4898 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4899 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4901 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4902 new_extent->disk_bytenr);
4903 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4904 new_extent->disk_num_bytes);
4905 btrfs_mark_buffer_dirty(leaf);
4907 ret = btrfs_inc_extent_ref(trans, root,
4908 new_extent->disk_bytenr,
4909 new_extent->disk_num_bytes,
4911 root->root_key.objectid,
4912 trans->transid, key.objectid);
4914 ret = btrfs_free_extent(trans, root,
4915 bytenr, num_bytes, leaf->start,
4916 btrfs_header_owner(leaf),
4917 btrfs_header_generation(leaf),
4923 BUG_ON(ext_index + 1 != ref->nritems);
4924 btrfs_free_leaf_ref(root, ref);
4928 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4929 struct btrfs_root *root)
4931 struct btrfs_root *reloc_root;
4934 if (root->reloc_root) {
4935 reloc_root = root->reloc_root;
4936 root->reloc_root = NULL;
4937 list_add(&reloc_root->dead_list,
4938 &root->fs_info->dead_reloc_roots);
4940 btrfs_set_root_bytenr(&reloc_root->root_item,
4941 reloc_root->node->start);
4942 btrfs_set_root_level(&root->root_item,
4943 btrfs_header_level(reloc_root->node));
4944 memset(&reloc_root->root_item.drop_progress, 0,
4945 sizeof(struct btrfs_disk_key));
4946 reloc_root->root_item.drop_level = 0;
4948 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4949 &reloc_root->root_key,
4950 &reloc_root->root_item);
4956 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4958 struct btrfs_trans_handle *trans;
4959 struct btrfs_root *reloc_root;
4960 struct btrfs_root *prev_root = NULL;
4961 struct list_head dead_roots;
4965 INIT_LIST_HEAD(&dead_roots);
4966 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4968 while (!list_empty(&dead_roots)) {
4969 reloc_root = list_entry(dead_roots.prev,
4970 struct btrfs_root, dead_list);
4971 list_del_init(&reloc_root->dead_list);
4973 BUG_ON(reloc_root->commit_root != NULL);
4975 trans = btrfs_join_transaction(root, 1);
4978 mutex_lock(&root->fs_info->drop_mutex);
4979 ret = btrfs_drop_snapshot(trans, reloc_root);
4982 mutex_unlock(&root->fs_info->drop_mutex);
4984 nr = trans->blocks_used;
4985 ret = btrfs_end_transaction(trans, root);
4987 btrfs_btree_balance_dirty(root, nr);
4990 free_extent_buffer(reloc_root->node);
4992 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4993 &reloc_root->root_key);
4995 mutex_unlock(&root->fs_info->drop_mutex);
4997 nr = trans->blocks_used;
4998 ret = btrfs_end_transaction(trans, root);
5000 btrfs_btree_balance_dirty(root, nr);
5003 prev_root = reloc_root;
5006 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5012 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5014 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5018 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5020 struct btrfs_root *reloc_root;
5021 struct btrfs_trans_handle *trans;
5022 struct btrfs_key location;
5026 mutex_lock(&root->fs_info->tree_reloc_mutex);
5027 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5029 found = !list_empty(&root->fs_info->dead_reloc_roots);
5030 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5033 trans = btrfs_start_transaction(root, 1);
5035 ret = btrfs_commit_transaction(trans, root);
5039 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5040 location.offset = (u64)-1;
5041 location.type = BTRFS_ROOT_ITEM_KEY;
5043 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5044 BUG_ON(!reloc_root);
5045 btrfs_orphan_cleanup(reloc_root);
5049 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
5050 struct btrfs_root *root)
5052 struct btrfs_root *reloc_root;
5053 struct extent_buffer *eb;
5054 struct btrfs_root_item *root_item;
5055 struct btrfs_key root_key;
5058 BUG_ON(!root->ref_cows);
5059 if (root->reloc_root)
5062 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5065 ret = btrfs_copy_root(trans, root, root->commit_root,
5066 &eb, BTRFS_TREE_RELOC_OBJECTID);
5069 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5070 root_key.offset = root->root_key.objectid;
5071 root_key.type = BTRFS_ROOT_ITEM_KEY;
5073 memcpy(root_item, &root->root_item, sizeof(root_item));
5074 btrfs_set_root_refs(root_item, 0);
5075 btrfs_set_root_bytenr(root_item, eb->start);
5076 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5077 btrfs_set_root_generation(root_item, trans->transid);
5079 btrfs_tree_unlock(eb);
5080 free_extent_buffer(eb);
5082 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5083 &root_key, root_item);
5087 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5089 BUG_ON(!reloc_root);
5090 reloc_root->last_trans = trans->transid;
5091 reloc_root->commit_root = NULL;
5092 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5094 root->reloc_root = reloc_root;
5099 * Core function of space balance.
5101 * The idea is using reloc trees to relocate tree blocks in reference
5102 * counted roots. There is one reloc tree for each subvol, and all
5103 * reloc trees share same root key objectid. Reloc trees are snapshots
5104 * of the latest committed roots of subvols (root->commit_root).
5106 * To relocate a tree block referenced by a subvol, there are two steps.
5107 * COW the block through subvol's reloc tree, then update block pointer
5108 * in the subvol to point to the new block. Since all reloc trees share
5109 * same root key objectid, doing special handing for tree blocks owned
5110 * by them is easy. Once a tree block has been COWed in one reloc tree,
5111 * we can use the resulting new block directly when the same block is
5112 * required to COW again through other reloc trees. By this way, relocated
5113 * tree blocks are shared between reloc trees, so they are also shared
5116 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5117 struct btrfs_root *root,
5118 struct btrfs_path *path,
5119 struct btrfs_key *first_key,
5120 struct btrfs_ref_path *ref_path,
5121 struct btrfs_block_group_cache *group,
5122 struct inode *reloc_inode)
5124 struct btrfs_root *reloc_root;
5125 struct extent_buffer *eb = NULL;
5126 struct btrfs_key *keys;
5130 int lowest_level = 0;
5133 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5134 lowest_level = ref_path->owner_objectid;
5136 if (!root->ref_cows) {
5137 path->lowest_level = lowest_level;
5138 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5140 path->lowest_level = 0;
5141 btrfs_release_path(root, path);
5145 mutex_lock(&root->fs_info->tree_reloc_mutex);
5146 ret = init_reloc_tree(trans, root);
5148 reloc_root = root->reloc_root;
5150 shared_level = ref_path->shared_level;
5151 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5153 keys = ref_path->node_keys;
5154 nodes = ref_path->new_nodes;
5155 memset(&keys[shared_level + 1], 0,
5156 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5157 memset(&nodes[shared_level + 1], 0,
5158 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5160 if (nodes[lowest_level] == 0) {
5161 path->lowest_level = lowest_level;
5162 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5165 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5166 eb = path->nodes[level];
5167 if (!eb || eb == reloc_root->node)
5169 nodes[level] = eb->start;
5171 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5173 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5176 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5177 eb = path->nodes[0];
5178 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5179 group, reloc_inode);
5182 btrfs_release_path(reloc_root, path);
5184 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5190 * replace tree blocks in the fs tree with tree blocks in
5193 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5196 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5197 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5200 extent_buffer_get(path->nodes[0]);
5201 eb = path->nodes[0];
5202 btrfs_release_path(reloc_root, path);
5203 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5205 free_extent_buffer(eb);
5208 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5209 path->lowest_level = 0;
5213 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5214 struct btrfs_root *root,
5215 struct btrfs_path *path,
5216 struct btrfs_key *first_key,
5217 struct btrfs_ref_path *ref_path)
5221 ret = relocate_one_path(trans, root, path, first_key,
5222 ref_path, NULL, NULL);
5225 if (root == root->fs_info->extent_root)
5226 btrfs_extent_post_op(trans, root);
5231 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5232 struct btrfs_root *extent_root,
5233 struct btrfs_path *path,
5234 struct btrfs_key *extent_key)
5238 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5241 ret = btrfs_del_item(trans, extent_root, path);
5243 btrfs_release_path(extent_root, path);
5247 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5248 struct btrfs_ref_path *ref_path)
5250 struct btrfs_key root_key;
5252 root_key.objectid = ref_path->root_objectid;
5253 root_key.type = BTRFS_ROOT_ITEM_KEY;
5254 if (is_cowonly_root(ref_path->root_objectid))
5255 root_key.offset = 0;
5257 root_key.offset = (u64)-1;
5259 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5262 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5263 struct btrfs_path *path,
5264 struct btrfs_key *extent_key,
5265 struct btrfs_block_group_cache *group,
5266 struct inode *reloc_inode, int pass)
5268 struct btrfs_trans_handle *trans;
5269 struct btrfs_root *found_root;
5270 struct btrfs_ref_path *ref_path = NULL;
5271 struct disk_extent *new_extents = NULL;
5276 struct btrfs_key first_key;
5280 trans = btrfs_start_transaction(extent_root, 1);
5283 if (extent_key->objectid == 0) {
5284 ret = del_extent_zero(trans, extent_root, path, extent_key);
5288 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5294 for (loops = 0; ; loops++) {
5296 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5297 extent_key->objectid);
5299 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5306 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5307 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5310 found_root = read_ref_root(extent_root->fs_info, ref_path);
5311 BUG_ON(!found_root);
5313 * for reference counted tree, only process reference paths
5314 * rooted at the latest committed root.
5316 if (found_root->ref_cows &&
5317 ref_path->root_generation != found_root->root_key.offset)
5320 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5323 * copy data extents to new locations
5325 u64 group_start = group->key.objectid;
5326 ret = relocate_data_extent(reloc_inode,
5335 level = ref_path->owner_objectid;
5338 if (prev_block != ref_path->nodes[level]) {
5339 struct extent_buffer *eb;
5340 u64 block_start = ref_path->nodes[level];
5341 u64 block_size = btrfs_level_size(found_root, level);
5343 eb = read_tree_block(found_root, block_start,
5345 btrfs_tree_lock(eb);
5346 BUG_ON(level != btrfs_header_level(eb));
5349 btrfs_item_key_to_cpu(eb, &first_key, 0);
5351 btrfs_node_key_to_cpu(eb, &first_key, 0);
5353 btrfs_tree_unlock(eb);
5354 free_extent_buffer(eb);
5355 prev_block = block_start;
5358 btrfs_record_root_in_trans(found_root);
5359 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5361 * try to update data extent references while
5362 * keeping metadata shared between snapshots.
5365 ret = relocate_one_path(trans, found_root,
5366 path, &first_key, ref_path,
5367 group, reloc_inode);
5373 * use fallback method to process the remaining
5377 u64 group_start = group->key.objectid;
5378 new_extents = kmalloc(sizeof(*new_extents),
5381 ret = get_new_locations(reloc_inode,
5389 ret = replace_one_extent(trans, found_root,
5391 &first_key, ref_path,
5392 new_extents, nr_extents);
5394 ret = relocate_tree_block(trans, found_root, path,
5395 &first_key, ref_path);
5402 btrfs_end_transaction(trans, extent_root);
5408 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5411 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5412 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5414 num_devices = root->fs_info->fs_devices->rw_devices;
5415 if (num_devices == 1) {
5416 stripped |= BTRFS_BLOCK_GROUP_DUP;
5417 stripped = flags & ~stripped;
5419 /* turn raid0 into single device chunks */
5420 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5423 /* turn mirroring into duplication */
5424 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5425 BTRFS_BLOCK_GROUP_RAID10))
5426 return stripped | BTRFS_BLOCK_GROUP_DUP;
5429 /* they already had raid on here, just return */
5430 if (flags & stripped)
5433 stripped |= BTRFS_BLOCK_GROUP_DUP;
5434 stripped = flags & ~stripped;
5436 /* switch duplicated blocks with raid1 */
5437 if (flags & BTRFS_BLOCK_GROUP_DUP)
5438 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5440 /* turn single device chunks into raid0 */
5441 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5446 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5447 struct btrfs_block_group_cache *shrink_block_group,
5450 struct btrfs_trans_handle *trans;
5451 u64 new_alloc_flags;
5454 spin_lock(&shrink_block_group->lock);
5455 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5456 spin_unlock(&shrink_block_group->lock);
5458 trans = btrfs_start_transaction(root, 1);
5459 spin_lock(&shrink_block_group->lock);
5461 new_alloc_flags = update_block_group_flags(root,
5462 shrink_block_group->flags);
5463 if (new_alloc_flags != shrink_block_group->flags) {
5465 btrfs_block_group_used(&shrink_block_group->item);
5467 calc = shrink_block_group->key.offset;
5469 spin_unlock(&shrink_block_group->lock);
5471 do_chunk_alloc(trans, root->fs_info->extent_root,
5472 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5474 btrfs_end_transaction(trans, root);
5476 spin_unlock(&shrink_block_group->lock);
5480 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5481 struct btrfs_root *root,
5482 u64 objectid, u64 size)
5484 struct btrfs_path *path;
5485 struct btrfs_inode_item *item;
5486 struct extent_buffer *leaf;
5489 path = btrfs_alloc_path();
5493 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5497 leaf = path->nodes[0];
5498 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5499 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5500 btrfs_set_inode_generation(leaf, item, 1);
5501 btrfs_set_inode_size(leaf, item, size);
5502 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5503 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5504 btrfs_mark_buffer_dirty(leaf);
5505 btrfs_release_path(root, path);
5507 btrfs_free_path(path);
5511 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5512 struct btrfs_block_group_cache *group)
5514 struct inode *inode = NULL;
5515 struct btrfs_trans_handle *trans;
5516 struct btrfs_root *root;
5517 struct btrfs_key root_key;
5518 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5521 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5522 root_key.type = BTRFS_ROOT_ITEM_KEY;
5523 root_key.offset = (u64)-1;
5524 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5526 return ERR_CAST(root);
5528 trans = btrfs_start_transaction(root, 1);
5531 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5535 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5538 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5539 group->key.offset, 0, group->key.offset,
5543 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5544 if (inode->i_state & I_NEW) {
5545 BTRFS_I(inode)->root = root;
5546 BTRFS_I(inode)->location.objectid = objectid;
5547 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5548 BTRFS_I(inode)->location.offset = 0;
5549 btrfs_read_locked_inode(inode);
5550 unlock_new_inode(inode);
5551 BUG_ON(is_bad_inode(inode));
5555 BTRFS_I(inode)->index_cnt = group->key.objectid;
5557 err = btrfs_orphan_add(trans, inode);
5559 btrfs_end_transaction(trans, root);
5563 inode = ERR_PTR(err);
5568 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5571 struct btrfs_ordered_sum *sums;
5572 struct btrfs_sector_sum *sector_sum;
5573 struct btrfs_ordered_extent *ordered;
5574 struct btrfs_root *root = BTRFS_I(inode)->root;
5575 struct list_head list;
5580 INIT_LIST_HEAD(&list);
5582 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5583 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5585 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5586 ret = btrfs_lookup_csums_range(root, disk_bytenr,
5587 disk_bytenr + len - 1, &list);
5589 while (!list_empty(&list)) {
5590 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5591 list_del_init(&sums->list);
5593 sector_sum = sums->sums;
5594 sums->bytenr = ordered->start;
5597 while (offset < sums->len) {
5598 sector_sum->bytenr += ordered->start - disk_bytenr;
5600 offset += root->sectorsize;
5603 btrfs_add_ordered_sum(inode, ordered, sums);
5605 btrfs_put_ordered_extent(ordered);
5609 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5611 struct btrfs_trans_handle *trans;
5612 struct btrfs_path *path;
5613 struct btrfs_fs_info *info = root->fs_info;
5614 struct extent_buffer *leaf;
5615 struct inode *reloc_inode;
5616 struct btrfs_block_group_cache *block_group;
5617 struct btrfs_key key;
5626 root = root->fs_info->extent_root;
5628 block_group = btrfs_lookup_block_group(info, group_start);
5629 BUG_ON(!block_group);
5631 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5632 (unsigned long long)block_group->key.objectid,
5633 (unsigned long long)block_group->flags);
5635 path = btrfs_alloc_path();
5638 reloc_inode = create_reloc_inode(info, block_group);
5639 BUG_ON(IS_ERR(reloc_inode));
5641 __alloc_chunk_for_shrink(root, block_group, 1);
5642 set_block_group_readonly(block_group);
5644 btrfs_start_delalloc_inodes(info->tree_root);
5645 btrfs_wait_ordered_extents(info->tree_root, 0);
5650 key.objectid = block_group->key.objectid;
5653 cur_byte = key.objectid;
5655 trans = btrfs_start_transaction(info->tree_root, 1);
5656 btrfs_commit_transaction(trans, info->tree_root);
5658 mutex_lock(&root->fs_info->cleaner_mutex);
5659 btrfs_clean_old_snapshots(info->tree_root);
5660 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5661 mutex_unlock(&root->fs_info->cleaner_mutex);
5664 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5668 leaf = path->nodes[0];
5669 nritems = btrfs_header_nritems(leaf);
5670 if (path->slots[0] >= nritems) {
5671 ret = btrfs_next_leaf(root, path);
5678 leaf = path->nodes[0];
5679 nritems = btrfs_header_nritems(leaf);
5682 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5684 if (key.objectid >= block_group->key.objectid +
5685 block_group->key.offset)
5688 if (progress && need_resched()) {
5689 btrfs_release_path(root, path);
5696 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5697 key.objectid + key.offset <= cur_byte) {
5703 cur_byte = key.objectid + key.offset;
5704 btrfs_release_path(root, path);
5706 __alloc_chunk_for_shrink(root, block_group, 0);
5707 ret = relocate_one_extent(root, path, &key, block_group,
5713 key.objectid = cur_byte;
5718 btrfs_release_path(root, path);
5721 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5722 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5725 if (total_found > 0) {
5726 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
5727 (unsigned long long)total_found, pass);
5729 if (total_found == skipped && pass > 2) {
5731 reloc_inode = create_reloc_inode(info, block_group);
5737 /* delete reloc_inode */
5740 /* unpin extents in this range */
5741 trans = btrfs_start_transaction(info->tree_root, 1);
5742 btrfs_commit_transaction(trans, info->tree_root);
5744 spin_lock(&block_group->lock);
5745 WARN_ON(block_group->pinned > 0);
5746 WARN_ON(block_group->reserved > 0);
5747 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5748 spin_unlock(&block_group->lock);
5749 put_block_group(block_group);
5752 btrfs_free_path(path);
5756 static int find_first_block_group(struct btrfs_root *root,
5757 struct btrfs_path *path, struct btrfs_key *key)
5760 struct btrfs_key found_key;
5761 struct extent_buffer *leaf;
5764 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5769 slot = path->slots[0];
5770 leaf = path->nodes[0];
5771 if (slot >= btrfs_header_nritems(leaf)) {
5772 ret = btrfs_next_leaf(root, path);
5779 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5781 if (found_key.objectid >= key->objectid &&
5782 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5793 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5795 struct btrfs_block_group_cache *block_group;
5798 spin_lock(&info->block_group_cache_lock);
5799 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5800 block_group = rb_entry(n, struct btrfs_block_group_cache,
5802 rb_erase(&block_group->cache_node,
5803 &info->block_group_cache_tree);
5804 spin_unlock(&info->block_group_cache_lock);
5806 btrfs_remove_free_space_cache(block_group);
5807 down_write(&block_group->space_info->groups_sem);
5808 list_del(&block_group->list);
5809 up_write(&block_group->space_info->groups_sem);
5811 WARN_ON(atomic_read(&block_group->count) != 1);
5814 spin_lock(&info->block_group_cache_lock);
5816 spin_unlock(&info->block_group_cache_lock);
5820 int btrfs_read_block_groups(struct btrfs_root *root)
5822 struct btrfs_path *path;
5824 struct btrfs_block_group_cache *cache;
5825 struct btrfs_fs_info *info = root->fs_info;
5826 struct btrfs_space_info *space_info;
5827 struct btrfs_key key;
5828 struct btrfs_key found_key;
5829 struct extent_buffer *leaf;
5831 root = info->extent_root;
5834 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5835 path = btrfs_alloc_path();
5840 ret = find_first_block_group(root, path, &key);
5848 leaf = path->nodes[0];
5849 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5850 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5856 atomic_set(&cache->count, 1);
5857 spin_lock_init(&cache->lock);
5858 mutex_init(&cache->alloc_mutex);
5859 mutex_init(&cache->cache_mutex);
5860 INIT_LIST_HEAD(&cache->list);
5861 read_extent_buffer(leaf, &cache->item,
5862 btrfs_item_ptr_offset(leaf, path->slots[0]),
5863 sizeof(cache->item));
5864 memcpy(&cache->key, &found_key, sizeof(found_key));
5866 key.objectid = found_key.objectid + found_key.offset;
5867 btrfs_release_path(root, path);
5868 cache->flags = btrfs_block_group_flags(&cache->item);
5870 ret = update_space_info(info, cache->flags, found_key.offset,
5871 btrfs_block_group_used(&cache->item),
5874 cache->space_info = space_info;
5875 down_write(&space_info->groups_sem);
5876 list_add_tail(&cache->list, &space_info->block_groups);
5877 up_write(&space_info->groups_sem);
5879 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5882 set_avail_alloc_bits(root->fs_info, cache->flags);
5883 if (btrfs_chunk_readonly(root, cache->key.objectid))
5884 set_block_group_readonly(cache);
5888 btrfs_free_path(path);
5892 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5893 struct btrfs_root *root, u64 bytes_used,
5894 u64 type, u64 chunk_objectid, u64 chunk_offset,
5898 struct btrfs_root *extent_root;
5899 struct btrfs_block_group_cache *cache;
5901 extent_root = root->fs_info->extent_root;
5903 root->fs_info->last_trans_new_blockgroup = trans->transid;
5905 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5909 cache->key.objectid = chunk_offset;
5910 cache->key.offset = size;
5911 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5912 atomic_set(&cache->count, 1);
5913 spin_lock_init(&cache->lock);
5914 mutex_init(&cache->alloc_mutex);
5915 mutex_init(&cache->cache_mutex);
5916 INIT_LIST_HEAD(&cache->list);
5918 btrfs_set_block_group_used(&cache->item, bytes_used);
5919 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5920 cache->flags = type;
5921 btrfs_set_block_group_flags(&cache->item, type);
5923 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5924 &cache->space_info);
5926 down_write(&cache->space_info->groups_sem);
5927 list_add_tail(&cache->list, &cache->space_info->block_groups);
5928 up_write(&cache->space_info->groups_sem);
5930 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5933 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5934 sizeof(cache->item));
5937 finish_current_insert(trans, extent_root, 0);
5938 ret = del_pending_extents(trans, extent_root, 0);
5940 set_avail_alloc_bits(extent_root->fs_info, type);
5945 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5946 struct btrfs_root *root, u64 group_start)
5948 struct btrfs_path *path;
5949 struct btrfs_block_group_cache *block_group;
5950 struct btrfs_key key;
5953 root = root->fs_info->extent_root;
5955 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5956 BUG_ON(!block_group);
5957 BUG_ON(!block_group->ro);
5959 memcpy(&key, &block_group->key, sizeof(key));
5961 path = btrfs_alloc_path();
5964 btrfs_remove_free_space_cache(block_group);
5965 rb_erase(&block_group->cache_node,
5966 &root->fs_info->block_group_cache_tree);
5967 down_write(&block_group->space_info->groups_sem);
5968 list_del(&block_group->list);
5969 up_write(&block_group->space_info->groups_sem);
5971 spin_lock(&block_group->space_info->lock);
5972 block_group->space_info->total_bytes -= block_group->key.offset;
5973 block_group->space_info->bytes_readonly -= block_group->key.offset;
5974 spin_unlock(&block_group->space_info->lock);
5975 block_group->space_info->full = 0;
5977 put_block_group(block_group);
5978 put_block_group(block_group);
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5986 ret = btrfs_del_item(trans, root, path);
5988 btrfs_free_path(path);