2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "ref-cache.h"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op {
47 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root);
49 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root);
51 static struct btrfs_block_group_cache *
52 __btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
56 void maybe_lock_mutex(struct btrfs_root *root)
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_lock(&root->fs_info->alloc_mutex);
65 void maybe_unlock_mutex(struct btrfs_root *root)
67 if (root != root->fs_info->extent_root &&
68 root != root->fs_info->chunk_root &&
69 root != root->fs_info->dev_root) {
70 mutex_unlock(&root->fs_info->alloc_mutex);
74 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76 return (cache->flags & bits) == bits;
80 * this adds the block group to the fs_info rb tree for the block group
83 int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
97 if (block_group->key.objectid < cache->key.objectid) {
99 } else if (block_group->key.objectid > cache->key.objectid) {
102 spin_unlock(&info->block_group_cache_lock);
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
119 static struct btrfs_block_group_cache *
120 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
123 struct btrfs_block_group_cache *cache, *ret = NULL;
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
131 cache = rb_entry(n, struct btrfs_block_group_cache,
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
151 spin_unlock(&info->block_group_cache_lock);
157 * this is only called by cache_block_group, since we could have freed extents
158 * we need to check the pinned_extents for any extents that can't be used yet
159 * since their free space will be released as soon as the transaction commits.
161 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
162 struct btrfs_fs_info *info, u64 start, u64 end)
164 u64 extent_start, extent_end, size;
167 while (start < end) {
168 ret = find_first_extent_bit(&info->pinned_extents, start,
169 &extent_start, &extent_end,
174 if (extent_start == start) {
175 start = extent_end + 1;
176 } else if (extent_start > start && extent_start < end) {
177 size = extent_start - start;
178 ret = btrfs_add_free_space(block_group, start, size);
180 start = extent_end + 1;
188 ret = btrfs_add_free_space(block_group, start, size);
195 static int cache_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache *block_group)
198 struct btrfs_path *path;
200 struct btrfs_key key;
201 struct extent_buffer *leaf;
210 root = root->fs_info->extent_root;
212 if (block_group->cached)
215 path = btrfs_alloc_path();
221 * we get into deadlocks with paths held by callers of this function.
222 * since the alloc_mutex is protecting things right now, just
223 * skip the locking here
225 path->skip_locking = 1;
226 first_free = max_t(u64, block_group->key.objectid,
227 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
228 key.objectid = block_group->key.objectid;
230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
231 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
234 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
238 leaf = path->nodes[0];
239 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
240 if (key.objectid + key.offset > first_free)
241 first_free = key.objectid + key.offset;
244 leaf = path->nodes[0];
245 slot = path->slots[0];
246 if (slot >= btrfs_header_nritems(leaf)) {
247 ret = btrfs_next_leaf(root, path);
255 btrfs_item_key_to_cpu(leaf, &key, slot);
256 if (key.objectid < block_group->key.objectid)
259 if (key.objectid >= block_group->key.objectid +
260 block_group->key.offset)
263 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
269 add_new_free_space(block_group, root->fs_info, last,
272 last = key.objectid + key.offset;
281 add_new_free_space(block_group, root->fs_info, last,
282 block_group->key.objectid +
283 block_group->key.offset);
285 block_group->cached = 1;
288 btrfs_free_path(path);
293 * return the block group that starts at or after bytenr
295 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
299 struct btrfs_block_group_cache *cache;
301 cache = block_group_cache_tree_search(info, bytenr, 0);
307 * return the block group that contains teh given bytenr
309 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
313 struct btrfs_block_group_cache *cache;
315 cache = block_group_cache_tree_search(info, bytenr, 1);
320 static int noinline find_free_space(struct btrfs_root *root,
321 struct btrfs_block_group_cache **cache_ret,
322 u64 *start_ret, u64 num, int data)
325 struct btrfs_block_group_cache *cache = *cache_ret;
326 struct btrfs_free_space *info = NULL;
328 u64 search_start = *start_ret;
330 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
334 last = max(search_start, cache->key.objectid);
337 ret = cache_block_group(root, cache);
341 if (cache->ro || !block_group_bits(cache, data))
344 info = btrfs_find_free_space(cache, last, num);
346 *start_ret = info->offset;
351 last = cache->key.objectid + cache->key.offset;
353 cache = btrfs_lookup_first_block_group(root->fs_info, last);
364 static u64 div_factor(u64 num, int factor)
373 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
376 struct list_head *head = &info->space_info;
377 struct list_head *cur;
378 struct btrfs_space_info *found;
379 list_for_each(cur, head) {
380 found = list_entry(cur, struct btrfs_space_info, list);
381 if (found->flags == flags)
387 static struct btrfs_block_group_cache *
388 __btrfs_find_block_group(struct btrfs_root *root,
389 struct btrfs_block_group_cache *hint,
390 u64 search_start, int data, int owner)
392 struct btrfs_block_group_cache *cache;
393 struct btrfs_block_group_cache *found_group = NULL;
394 struct btrfs_fs_info *info = root->fs_info;
402 if (data & BTRFS_BLOCK_GROUP_METADATA)
406 struct btrfs_block_group_cache *shint;
407 shint = btrfs_lookup_first_block_group(info, search_start);
408 if (shint && block_group_bits(shint, data) && !shint->ro) {
409 spin_lock(&shint->lock);
410 used = btrfs_block_group_used(&shint->item);
411 if (used + shint->pinned + shint->reserved <
412 div_factor(shint->key.offset, factor)) {
413 spin_unlock(&shint->lock);
416 spin_unlock(&shint->lock);
419 if (hint && !hint->ro && block_group_bits(hint, data)) {
420 spin_lock(&hint->lock);
421 used = btrfs_block_group_used(&hint->item);
422 if (used + hint->pinned + hint->reserved <
423 div_factor(hint->key.offset, factor)) {
424 spin_unlock(&hint->lock);
427 spin_unlock(&hint->lock);
428 last = hint->key.objectid + hint->key.offset;
431 last = max(hint->key.objectid, search_start);
437 cache = btrfs_lookup_first_block_group(root->fs_info, last);
441 spin_lock(&cache->lock);
442 last = cache->key.objectid + cache->key.offset;
443 used = btrfs_block_group_used(&cache->item);
445 if (!cache->ro && block_group_bits(cache, data)) {
446 free_check = div_factor(cache->key.offset, factor);
447 if (used + cache->pinned + cache->reserved <
450 spin_unlock(&cache->lock);
454 spin_unlock(&cache->lock);
462 if (!full_search && factor < 10) {
472 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
473 struct btrfs_block_group_cache
474 *hint, u64 search_start,
478 struct btrfs_block_group_cache *ret;
479 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
483 /* simple helper to search for an existing extent at a given offset */
484 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
487 struct btrfs_key key;
488 struct btrfs_path *path;
490 path = btrfs_alloc_path();
492 maybe_lock_mutex(root);
493 key.objectid = start;
495 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
496 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
498 maybe_unlock_mutex(root);
499 btrfs_free_path(path);
504 * Back reference rules. Back refs have three main goals:
506 * 1) differentiate between all holders of references to an extent so that
507 * when a reference is dropped we can make sure it was a valid reference
508 * before freeing the extent.
510 * 2) Provide enough information to quickly find the holders of an extent
511 * if we notice a given block is corrupted or bad.
513 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
514 * maintenance. This is actually the same as #2, but with a slightly
515 * different use case.
517 * File extents can be referenced by:
519 * - multiple snapshots, subvolumes, or different generations in one subvol
520 * - different files inside a single subvolume
521 * - different offsets inside a file (bookend extents in file.c)
523 * The extent ref structure has fields for:
525 * - Objectid of the subvolume root
526 * - Generation number of the tree holding the reference
527 * - objectid of the file holding the reference
528 * - offset in the file corresponding to the key holding the reference
529 * - number of references holding by parent node (alway 1 for tree blocks)
531 * Btree leaf may hold multiple references to a file extent. In most cases,
532 * these references are from same file and the corresponding offsets inside
533 * the file are close together. So inode objectid and offset in file are
534 * just hints, they provide hints about where in the btree the references
535 * can be found and when we can stop searching.
537 * When a file extent is allocated the fields are filled in:
538 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
540 * When a leaf is cow'd new references are added for every file extent found
541 * in the leaf. It looks similar to the create case, but trans->transid will
542 * be different when the block is cow'd.
544 * (root_key.objectid, trans->transid, inode objectid, offset in file,
545 * number of references in the leaf)
547 * Because inode objectid and offset in file are just hints, they are not
548 * used when backrefs are deleted. When a file extent is removed either
549 * during snapshot deletion or file truncation, we find the corresponding
550 * back back reference and check the following fields.
552 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
554 * Btree extents can be referenced by:
556 * - Different subvolumes
557 * - Different generations of the same subvolume
559 * When a tree block is created, back references are inserted:
561 * (root->root_key.objectid, trans->transid, level, 0, 1)
563 * When a tree block is cow'd, new back references are added for all the
564 * blocks it points to. If the tree block isn't in reference counted root,
565 * the old back references are removed. These new back references are of
566 * the form (trans->transid will have increased since creation):
568 * (root->root_key.objectid, trans->transid, level, 0, 1)
570 * When a backref is in deleting, the following fields are checked:
572 * if backref was for a tree root:
573 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
575 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
577 * Back Reference Key composing:
579 * The key objectid corresponds to the first byte in the extent, the key
580 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
581 * byte of parent extent. If a extent is tree root, the key offset is set
582 * to the key objectid.
585 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root,
587 struct btrfs_path *path, u64 bytenr,
588 u64 parent, u64 ref_root,
589 u64 ref_generation, int del)
591 struct btrfs_key key;
592 struct btrfs_extent_ref *ref;
593 struct extent_buffer *leaf;
596 key.objectid = bytenr;
597 key.type = BTRFS_EXTENT_REF_KEY;
600 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
608 leaf = path->nodes[0];
609 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
610 if (btrfs_ref_root(leaf, ref) != ref_root ||
611 btrfs_ref_generation(leaf, ref) != ref_generation) {
621 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path,
624 u64 bytenr, u64 parent,
625 u64 ref_root, u64 ref_generation,
626 u64 owner_objectid, u64 owner_offset)
628 struct btrfs_key key;
629 struct extent_buffer *leaf;
630 struct btrfs_extent_ref *ref;
634 key.objectid = bytenr;
635 key.type = BTRFS_EXTENT_REF_KEY;
638 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
640 leaf = path->nodes[0];
641 ref = btrfs_item_ptr(leaf, path->slots[0],
642 struct btrfs_extent_ref);
643 btrfs_set_ref_root(leaf, ref, ref_root);
644 btrfs_set_ref_generation(leaf, ref, ref_generation);
645 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
646 btrfs_set_ref_offset(leaf, ref, owner_offset);
647 btrfs_set_ref_num_refs(leaf, ref, 1);
648 } else if (ret == -EEXIST) {
650 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
651 leaf = path->nodes[0];
652 ref = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_extent_ref);
654 if (btrfs_ref_root(leaf, ref) != ref_root ||
655 btrfs_ref_generation(leaf, ref) != ref_generation) {
661 num_refs = btrfs_ref_num_refs(leaf, ref);
662 BUG_ON(num_refs == 0);
663 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
665 existing_owner = btrfs_ref_objectid(leaf, ref);
666 if (existing_owner == owner_objectid &&
667 btrfs_ref_offset(leaf, ref) > owner_offset) {
668 btrfs_set_ref_offset(leaf, ref, owner_offset);
669 } else if (existing_owner != owner_objectid &&
670 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
671 btrfs_set_ref_objectid(leaf, ref,
672 BTRFS_MULTIPLE_OBJECTIDS);
673 btrfs_set_ref_offset(leaf, ref, 0);
679 btrfs_mark_buffer_dirty(path->nodes[0]);
681 btrfs_release_path(root, path);
685 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
686 struct btrfs_root *root,
687 struct btrfs_path *path)
689 struct extent_buffer *leaf;
690 struct btrfs_extent_ref *ref;
694 leaf = path->nodes[0];
695 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
696 num_refs = btrfs_ref_num_refs(leaf, ref);
697 BUG_ON(num_refs == 0);
700 ret = btrfs_del_item(trans, root, path);
702 btrfs_set_ref_num_refs(leaf, ref, num_refs);
703 btrfs_mark_buffer_dirty(leaf);
705 btrfs_release_path(root, path);
709 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
711 u64 orig_parent, u64 parent,
712 u64 orig_root, u64 ref_root,
713 u64 orig_generation, u64 ref_generation,
714 u64 owner_objectid, u64 owner_offset)
717 struct btrfs_root *extent_root = root->fs_info->extent_root;
718 struct btrfs_path *path;
720 if (root == root->fs_info->extent_root) {
721 struct pending_extent_op *extent_op;
724 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
725 num_bytes = btrfs_level_size(root, (int)owner_objectid);
726 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
727 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
729 ret = get_state_private(&root->fs_info->extent_ins,
732 extent_op = (struct pending_extent_op *)
734 BUG_ON(extent_op->parent != orig_parent);
735 BUG_ON(extent_op->generation != orig_generation);
736 extent_op->parent = parent;
737 extent_op->generation = ref_generation;
739 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
742 extent_op->type = PENDING_BACKREF_UPDATE;
743 extent_op->bytenr = bytenr;
744 extent_op->num_bytes = num_bytes;
745 extent_op->parent = parent;
746 extent_op->orig_parent = orig_parent;
747 extent_op->generation = ref_generation;
748 extent_op->orig_generation = orig_generation;
749 extent_op->level = (int)owner_objectid;
751 set_extent_bits(&root->fs_info->extent_ins,
752 bytenr, bytenr + num_bytes - 1,
753 EXTENT_LOCKED, GFP_NOFS);
754 set_state_private(&root->fs_info->extent_ins,
755 bytenr, (unsigned long)extent_op);
760 path = btrfs_alloc_path();
763 ret = lookup_extent_backref(trans, extent_root, path,
764 bytenr, orig_parent, orig_root,
768 ret = remove_extent_backref(trans, extent_root, path);
771 ret = insert_extent_backref(trans, extent_root, path, bytenr,
772 parent, ref_root, ref_generation,
773 owner_objectid, owner_offset);
775 finish_current_insert(trans, extent_root);
776 del_pending_extents(trans, extent_root);
778 btrfs_free_path(path);
782 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
783 struct btrfs_root *root, u64 bytenr,
784 u64 orig_parent, u64 parent,
785 u64 ref_root, u64 ref_generation,
786 u64 owner_objectid, u64 owner_offset)
789 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
790 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
792 maybe_lock_mutex(root);
793 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
794 parent, ref_root, ref_root,
795 ref_generation, ref_generation,
796 owner_objectid, owner_offset);
797 maybe_unlock_mutex(root);
801 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
802 struct btrfs_root *root, u64 bytenr,
803 u64 orig_parent, u64 parent,
804 u64 orig_root, u64 ref_root,
805 u64 orig_generation, u64 ref_generation,
806 u64 owner_objectid, u64 owner_offset)
808 struct btrfs_path *path;
810 struct btrfs_key key;
811 struct extent_buffer *l;
812 struct btrfs_extent_item *item;
815 path = btrfs_alloc_path();
820 key.objectid = bytenr;
821 key.type = BTRFS_EXTENT_ITEM_KEY;
822 key.offset = (u64)-1;
824 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
828 BUG_ON(ret == 0 || path->slots[0] == 0);
833 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
834 BUG_ON(key.objectid != bytenr);
835 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
837 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
838 refs = btrfs_extent_refs(l, item);
839 btrfs_set_extent_refs(l, item, refs + 1);
840 btrfs_mark_buffer_dirty(path->nodes[0]);
842 btrfs_release_path(root->fs_info->extent_root, path);
845 ret = insert_extent_backref(trans, root->fs_info->extent_root,
846 path, bytenr, parent,
847 ref_root, ref_generation,
848 owner_objectid, owner_offset);
850 finish_current_insert(trans, root->fs_info->extent_root);
851 del_pending_extents(trans, root->fs_info->extent_root);
853 btrfs_free_path(path);
857 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
858 struct btrfs_root *root,
859 u64 bytenr, u64 num_bytes, u64 parent,
860 u64 ref_root, u64 ref_generation,
861 u64 owner_objectid, u64 owner_offset)
864 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
865 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
867 maybe_lock_mutex(root);
868 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
869 0, ref_root, 0, ref_generation,
870 owner_objectid, owner_offset);
871 maybe_unlock_mutex(root);
875 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
876 struct btrfs_root *root)
878 finish_current_insert(trans, root->fs_info->extent_root);
879 del_pending_extents(trans, root->fs_info->extent_root);
883 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, u64 bytenr,
885 u64 num_bytes, u32 *refs)
887 struct btrfs_path *path;
889 struct btrfs_key key;
890 struct extent_buffer *l;
891 struct btrfs_extent_item *item;
893 WARN_ON(num_bytes < root->sectorsize);
894 path = btrfs_alloc_path();
896 key.objectid = bytenr;
897 key.offset = num_bytes;
898 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
899 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
904 btrfs_print_leaf(root, path->nodes[0]);
905 printk("failed to find block number %Lu\n", bytenr);
909 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
910 *refs = btrfs_extent_refs(l, item);
912 btrfs_free_path(path);
916 static int get_reference_status(struct btrfs_root *root, u64 bytenr,
917 u64 parent_gen, u64 ref_objectid,
918 u64 *min_generation, u32 *ref_count)
920 struct btrfs_root *extent_root = root->fs_info->extent_root;
921 struct btrfs_path *path;
922 struct extent_buffer *leaf;
923 struct btrfs_extent_ref *ref_item;
924 struct btrfs_key key;
925 struct btrfs_key found_key;
926 u64 root_objectid = root->root_key.objectid;
931 key.objectid = bytenr;
932 key.offset = (u64)-1;
933 key.type = BTRFS_EXTENT_ITEM_KEY;
935 path = btrfs_alloc_path();
936 mutex_lock(&root->fs_info->alloc_mutex);
937 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
941 if (ret < 0 || path->slots[0] == 0)
945 leaf = path->nodes[0];
946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
948 if (found_key.objectid != bytenr ||
949 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
955 *min_generation = (u64)-1;
958 leaf = path->nodes[0];
959 nritems = btrfs_header_nritems(leaf);
960 if (path->slots[0] >= nritems) {
961 ret = btrfs_next_leaf(extent_root, path);
968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
969 if (found_key.objectid != bytenr)
972 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
977 ref_item = btrfs_item_ptr(leaf, path->slots[0],
978 struct btrfs_extent_ref);
979 ref_generation = btrfs_ref_generation(leaf, ref_item);
981 * For (parent_gen > 0 && parent_gen > ref_generation):
983 * we reach here through the oldest root, therefore
984 * all other reference from same snapshot should have
985 * a larger generation.
987 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
988 (parent_gen > 0 && parent_gen > ref_generation) ||
989 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
990 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
996 if (*min_generation > ref_generation)
997 *min_generation = ref_generation;
1003 mutex_unlock(&root->fs_info->alloc_mutex);
1004 btrfs_free_path(path);
1008 int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
1009 struct btrfs_root *root,
1010 struct btrfs_key *key, u64 bytenr)
1012 struct btrfs_root *old_root;
1013 struct btrfs_path *path = NULL;
1014 struct extent_buffer *eb;
1015 struct btrfs_file_extent_item *item;
1023 BUG_ON(trans == NULL);
1024 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
1025 ret = get_reference_status(root, bytenr, 0, key->objectid,
1026 &min_generation, &ref_count);
1033 old_root = root->dirty_root->root;
1034 ref_generation = old_root->root_key.offset;
1036 /* all references are created in running transaction */
1037 if (min_generation > ref_generation) {
1042 path = btrfs_alloc_path();
1048 path->skip_locking = 1;
1049 /* if no item found, the extent is referenced by other snapshot */
1050 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
1054 eb = path->nodes[0];
1055 item = btrfs_item_ptr(eb, path->slots[0],
1056 struct btrfs_file_extent_item);
1057 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
1058 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
1063 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
1065 eb = path->nodes[level];
1068 extent_start = eb->start;
1070 extent_start = bytenr;
1072 ret = get_reference_status(root, extent_start, ref_generation,
1073 0, &min_generation, &ref_count);
1077 if (ref_count != 1) {
1082 ref_generation = btrfs_header_generation(eb);
1087 btrfs_free_path(path);
1091 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1092 struct extent_buffer *buf, u32 nr_extents)
1094 struct btrfs_key key;
1095 struct btrfs_file_extent_item *fi;
1103 if (!root->ref_cows)
1106 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1108 root_gen = root->root_key.offset;
1111 root_gen = trans->transid - 1;
1114 level = btrfs_header_level(buf);
1115 nritems = btrfs_header_nritems(buf);
1118 struct btrfs_leaf_ref *ref;
1119 struct btrfs_extent_info *info;
1121 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1127 ref->root_gen = root_gen;
1128 ref->bytenr = buf->start;
1129 ref->owner = btrfs_header_owner(buf);
1130 ref->generation = btrfs_header_generation(buf);
1131 ref->nritems = nr_extents;
1132 info = ref->extents;
1134 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1136 btrfs_item_key_to_cpu(buf, &key, i);
1137 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1139 fi = btrfs_item_ptr(buf, i,
1140 struct btrfs_file_extent_item);
1141 if (btrfs_file_extent_type(buf, fi) ==
1142 BTRFS_FILE_EXTENT_INLINE)
1144 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1145 if (disk_bytenr == 0)
1148 info->bytenr = disk_bytenr;
1150 btrfs_file_extent_disk_num_bytes(buf, fi);
1151 info->objectid = key.objectid;
1152 info->offset = key.offset;
1156 ret = btrfs_add_leaf_ref(root, ref, shared);
1158 btrfs_free_leaf_ref(root, ref);
1164 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1165 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1172 u64 orig_generation;
1174 u32 nr_file_extents = 0;
1175 struct btrfs_key key;
1176 struct btrfs_file_extent_item *fi;
1181 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1182 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1184 ref_root = btrfs_header_owner(buf);
1185 ref_generation = btrfs_header_generation(buf);
1186 orig_root = btrfs_header_owner(orig_buf);
1187 orig_generation = btrfs_header_generation(orig_buf);
1189 nritems = btrfs_header_nritems(buf);
1190 level = btrfs_header_level(buf);
1192 if (root->ref_cows) {
1193 process_func = __btrfs_inc_extent_ref;
1196 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1199 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1201 process_func = __btrfs_update_extent_ref;
1204 for (i = 0; i < nritems; i++) {
1207 btrfs_item_key_to_cpu(buf, &key, i);
1208 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1210 fi = btrfs_item_ptr(buf, i,
1211 struct btrfs_file_extent_item);
1212 if (btrfs_file_extent_type(buf, fi) ==
1213 BTRFS_FILE_EXTENT_INLINE)
1215 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1221 maybe_lock_mutex(root);
1222 ret = process_func(trans, root, bytenr,
1223 orig_buf->start, buf->start,
1224 orig_root, ref_root,
1225 orig_generation, ref_generation,
1226 key.objectid, key.offset);
1227 maybe_unlock_mutex(root);
1235 bytenr = btrfs_node_blockptr(buf, i);
1236 maybe_lock_mutex(root);
1237 ret = process_func(trans, root, bytenr,
1238 orig_buf->start, buf->start,
1239 orig_root, ref_root,
1240 orig_generation, ref_generation,
1242 maybe_unlock_mutex(root);
1253 *nr_extents = nr_file_extents;
1255 *nr_extents = nritems;
1263 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, struct extent_buffer *orig_buf,
1265 struct extent_buffer *buf, int start_slot, int nr)
1272 u64 orig_generation;
1273 struct btrfs_key key;
1274 struct btrfs_file_extent_item *fi;
1280 BUG_ON(start_slot < 0);
1281 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1283 ref_root = btrfs_header_owner(buf);
1284 ref_generation = btrfs_header_generation(buf);
1285 orig_root = btrfs_header_owner(orig_buf);
1286 orig_generation = btrfs_header_generation(orig_buf);
1287 level = btrfs_header_level(buf);
1289 if (!root->ref_cows) {
1291 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1294 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1298 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1301 btrfs_item_key_to_cpu(buf, &key, slot);
1302 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1304 fi = btrfs_item_ptr(buf, slot,
1305 struct btrfs_file_extent_item);
1306 if (btrfs_file_extent_type(buf, fi) ==
1307 BTRFS_FILE_EXTENT_INLINE)
1309 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1312 maybe_lock_mutex(root);
1313 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1314 orig_buf->start, buf->start,
1315 orig_root, ref_root,
1316 orig_generation, ref_generation,
1317 key.objectid, key.offset);
1318 maybe_unlock_mutex(root);
1322 bytenr = btrfs_node_blockptr(buf, slot);
1323 maybe_lock_mutex(root);
1324 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1325 orig_buf->start, buf->start,
1326 orig_root, ref_root,
1327 orig_generation, ref_generation,
1329 maybe_unlock_mutex(root);
1340 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1341 struct btrfs_root *root,
1342 struct btrfs_path *path,
1343 struct btrfs_block_group_cache *cache)
1347 struct btrfs_root *extent_root = root->fs_info->extent_root;
1349 struct extent_buffer *leaf;
1351 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1356 leaf = path->nodes[0];
1357 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1358 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1359 btrfs_mark_buffer_dirty(leaf);
1360 btrfs_release_path(extent_root, path);
1362 finish_current_insert(trans, extent_root);
1363 pending_ret = del_pending_extents(trans, extent_root);
1372 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1373 struct btrfs_root *root)
1375 struct btrfs_block_group_cache *cache, *entry;
1379 struct btrfs_path *path;
1382 path = btrfs_alloc_path();
1386 mutex_lock(&root->fs_info->alloc_mutex);
1389 spin_lock(&root->fs_info->block_group_cache_lock);
1390 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1391 n; n = rb_next(n)) {
1392 entry = rb_entry(n, struct btrfs_block_group_cache,
1399 spin_unlock(&root->fs_info->block_group_cache_lock);
1405 last += cache->key.offset;
1407 err = write_one_cache_group(trans, root,
1410 * if we fail to write the cache group, we want
1411 * to keep it marked dirty in hopes that a later
1419 btrfs_free_path(path);
1420 mutex_unlock(&root->fs_info->alloc_mutex);
1424 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1425 u64 total_bytes, u64 bytes_used,
1426 struct btrfs_space_info **space_info)
1428 struct btrfs_space_info *found;
1430 found = __find_space_info(info, flags);
1432 found->total_bytes += total_bytes;
1433 found->bytes_used += bytes_used;
1435 *space_info = found;
1438 found = kmalloc(sizeof(*found), GFP_NOFS);
1442 list_add(&found->list, &info->space_info);
1443 INIT_LIST_HEAD(&found->block_groups);
1444 spin_lock_init(&found->lock);
1445 found->flags = flags;
1446 found->total_bytes = total_bytes;
1447 found->bytes_used = bytes_used;
1448 found->bytes_pinned = 0;
1449 found->bytes_reserved = 0;
1451 found->force_alloc = 0;
1452 *space_info = found;
1456 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1458 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1459 BTRFS_BLOCK_GROUP_RAID1 |
1460 BTRFS_BLOCK_GROUP_RAID10 |
1461 BTRFS_BLOCK_GROUP_DUP);
1463 if (flags & BTRFS_BLOCK_GROUP_DATA)
1464 fs_info->avail_data_alloc_bits |= extra_flags;
1465 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1466 fs_info->avail_metadata_alloc_bits |= extra_flags;
1467 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1468 fs_info->avail_system_alloc_bits |= extra_flags;
1472 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1474 u64 num_devices = root->fs_info->fs_devices->num_devices;
1476 if (num_devices == 1)
1477 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1478 if (num_devices < 4)
1479 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1481 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1482 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1483 BTRFS_BLOCK_GROUP_RAID10))) {
1484 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1487 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1488 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1489 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1492 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1493 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1494 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1495 (flags & BTRFS_BLOCK_GROUP_DUP)))
1496 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1500 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1501 struct btrfs_root *extent_root, u64 alloc_bytes,
1502 u64 flags, int force)
1504 struct btrfs_space_info *space_info;
1510 flags = reduce_alloc_profile(extent_root, flags);
1512 space_info = __find_space_info(extent_root->fs_info, flags);
1514 ret = update_space_info(extent_root->fs_info, flags,
1518 BUG_ON(!space_info);
1520 if (space_info->force_alloc) {
1522 space_info->force_alloc = 0;
1524 if (space_info->full)
1527 thresh = div_factor(space_info->total_bytes, 6);
1529 (space_info->bytes_used + space_info->bytes_pinned +
1530 space_info->bytes_reserved + alloc_bytes) < thresh)
1533 mutex_lock(&extent_root->fs_info->chunk_mutex);
1534 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1535 if (ret == -ENOSPC) {
1536 printk("space info full %Lu\n", flags);
1537 space_info->full = 1;
1542 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1543 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1547 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1552 static int update_block_group(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root,
1554 u64 bytenr, u64 num_bytes, int alloc,
1557 struct btrfs_block_group_cache *cache;
1558 struct btrfs_fs_info *info = root->fs_info;
1559 u64 total = num_bytes;
1563 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1565 cache = btrfs_lookup_block_group(info, bytenr);
1569 byte_in_group = bytenr - cache->key.objectid;
1570 WARN_ON(byte_in_group > cache->key.offset);
1572 spin_lock(&cache->lock);
1574 old_val = btrfs_block_group_used(&cache->item);
1575 num_bytes = min(total, cache->key.offset - byte_in_group);
1577 old_val += num_bytes;
1578 cache->space_info->bytes_used += num_bytes;
1579 btrfs_set_block_group_used(&cache->item, old_val);
1580 spin_unlock(&cache->lock);
1582 old_val -= num_bytes;
1583 cache->space_info->bytes_used -= num_bytes;
1584 btrfs_set_block_group_used(&cache->item, old_val);
1585 spin_unlock(&cache->lock);
1588 ret = btrfs_add_free_space(cache, bytenr,
1595 bytenr += num_bytes;
1600 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1602 struct btrfs_block_group_cache *cache;
1604 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1608 return cache->key.objectid;
1611 int btrfs_update_pinned_extents(struct btrfs_root *root,
1612 u64 bytenr, u64 num, int pin)
1615 struct btrfs_block_group_cache *cache;
1616 struct btrfs_fs_info *fs_info = root->fs_info;
1618 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1620 set_extent_dirty(&fs_info->pinned_extents,
1621 bytenr, bytenr + num - 1, GFP_NOFS);
1623 clear_extent_dirty(&fs_info->pinned_extents,
1624 bytenr, bytenr + num - 1, GFP_NOFS);
1627 cache = btrfs_lookup_block_group(fs_info, bytenr);
1629 len = min(num, cache->key.offset -
1630 (bytenr - cache->key.objectid));
1632 spin_lock(&cache->lock);
1633 cache->pinned += len;
1634 cache->space_info->bytes_pinned += len;
1635 spin_unlock(&cache->lock);
1636 fs_info->total_pinned += len;
1638 spin_lock(&cache->lock);
1639 cache->pinned -= len;
1640 cache->space_info->bytes_pinned -= len;
1641 spin_unlock(&cache->lock);
1642 fs_info->total_pinned -= len;
1650 static int update_reserved_extents(struct btrfs_root *root,
1651 u64 bytenr, u64 num, int reserve)
1654 struct btrfs_block_group_cache *cache;
1655 struct btrfs_fs_info *fs_info = root->fs_info;
1657 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1659 cache = btrfs_lookup_block_group(fs_info, bytenr);
1661 len = min(num, cache->key.offset -
1662 (bytenr - cache->key.objectid));
1664 spin_lock(&cache->lock);
1665 cache->reserved += len;
1666 cache->space_info->bytes_reserved += len;
1667 spin_unlock(&cache->lock);
1669 spin_lock(&cache->lock);
1670 cache->reserved -= len;
1671 cache->space_info->bytes_reserved -= len;
1672 spin_unlock(&cache->lock);
1680 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1685 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1689 ret = find_first_extent_bit(pinned_extents, last,
1690 &start, &end, EXTENT_DIRTY);
1693 set_extent_dirty(copy, start, end, GFP_NOFS);
1699 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct extent_io_tree *unpin)
1706 struct btrfs_block_group_cache *cache;
1708 mutex_lock(&root->fs_info->alloc_mutex);
1710 ret = find_first_extent_bit(unpin, 0, &start, &end,
1714 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
1715 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1716 cache = btrfs_lookup_block_group(root->fs_info, start);
1718 btrfs_add_free_space(cache, start, end - start + 1);
1719 if (need_resched()) {
1720 mutex_unlock(&root->fs_info->alloc_mutex);
1722 mutex_lock(&root->fs_info->alloc_mutex);
1725 mutex_unlock(&root->fs_info->alloc_mutex);
1729 static int finish_current_insert(struct btrfs_trans_handle *trans,
1730 struct btrfs_root *extent_root)
1735 struct btrfs_fs_info *info = extent_root->fs_info;
1736 struct btrfs_path *path;
1737 struct btrfs_extent_ref *ref;
1738 struct pending_extent_op *extent_op;
1739 struct btrfs_key key;
1740 struct btrfs_extent_item extent_item;
1744 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1745 btrfs_set_stack_extent_refs(&extent_item, 1);
1746 path = btrfs_alloc_path();
1749 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1750 &end, EXTENT_LOCKED);
1754 ret = get_state_private(&info->extent_ins, start, &priv);
1756 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1758 if (extent_op->type == PENDING_EXTENT_INSERT) {
1759 key.objectid = start;
1760 key.offset = end + 1 - start;
1761 key.type = BTRFS_EXTENT_ITEM_KEY;
1762 err = btrfs_insert_item(trans, extent_root, &key,
1763 &extent_item, sizeof(extent_item));
1766 clear_extent_bits(&info->extent_ins, start, end,
1767 EXTENT_LOCKED, GFP_NOFS);
1769 err = insert_extent_backref(trans, extent_root, path,
1770 start, extent_op->parent,
1771 extent_root->root_key.objectid,
1772 extent_op->generation,
1773 extent_op->level, 0);
1775 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1776 err = lookup_extent_backref(trans, extent_root, path,
1777 start, extent_op->orig_parent,
1778 extent_root->root_key.objectid,
1779 extent_op->orig_generation, 0);
1782 clear_extent_bits(&info->extent_ins, start, end,
1783 EXTENT_LOCKED, GFP_NOFS);
1785 key.objectid = start;
1786 key.offset = extent_op->parent;
1787 key.type = BTRFS_EXTENT_REF_KEY;
1788 err = btrfs_set_item_key_safe(trans, extent_root, path,
1791 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1792 struct btrfs_extent_ref);
1793 btrfs_set_ref_generation(path->nodes[0], ref,
1794 extent_op->generation);
1795 btrfs_mark_buffer_dirty(path->nodes[0]);
1796 btrfs_release_path(extent_root, path);
1802 if (need_resched()) {
1803 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1805 mutex_lock(&extent_root->fs_info->alloc_mutex);
1808 btrfs_free_path(path);
1812 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 u64 bytenr, u64 num_bytes, int is_data)
1817 struct extent_buffer *buf;
1819 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1823 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1827 /* we can reuse a block if it hasn't been written
1828 * and it is from this transaction. We can't
1829 * reuse anything from the tree log root because
1830 * it has tiny sub-transactions.
1832 if (btrfs_buffer_uptodate(buf, 0) &&
1833 btrfs_try_tree_lock(buf)) {
1834 u64 header_owner = btrfs_header_owner(buf);
1835 u64 header_transid = btrfs_header_generation(buf);
1836 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1837 header_transid == trans->transid &&
1838 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1839 clean_tree_block(NULL, root, buf);
1840 btrfs_tree_unlock(buf);
1841 free_extent_buffer(buf);
1844 btrfs_tree_unlock(buf);
1846 free_extent_buffer(buf);
1848 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1855 * remove an extent from the root, returns 0 on success
1857 static int __free_extent(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 u64 bytenr, u64 num_bytes, u64 parent,
1860 u64 root_objectid, u64 ref_generation,
1861 u64 owner_objectid, u64 owner_offset,
1862 int pin, int mark_free)
1864 struct btrfs_path *path;
1865 struct btrfs_key key;
1866 struct btrfs_fs_info *info = root->fs_info;
1867 struct btrfs_root *extent_root = info->extent_root;
1868 struct extent_buffer *leaf;
1870 int extent_slot = 0;
1871 int found_extent = 0;
1873 struct btrfs_extent_item *ei;
1876 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1877 key.objectid = bytenr;
1878 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1879 key.offset = num_bytes;
1880 path = btrfs_alloc_path();
1885 ret = lookup_extent_backref(trans, extent_root, path, bytenr, parent,
1886 root_objectid, ref_generation, 1);
1888 struct btrfs_key found_key;
1889 extent_slot = path->slots[0];
1890 while(extent_slot > 0) {
1892 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1894 if (found_key.objectid != bytenr)
1896 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1897 found_key.offset == num_bytes) {
1901 if (path->slots[0] - extent_slot > 5)
1904 if (!found_extent) {
1905 ret = remove_extent_backref(trans, extent_root, path);
1907 btrfs_release_path(extent_root, path);
1908 ret = btrfs_search_slot(trans, extent_root,
1911 extent_slot = path->slots[0];
1914 btrfs_print_leaf(extent_root, path->nodes[0]);
1916 printk("Unable to find ref byte nr %Lu root %Lu "
1917 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1918 root_objectid, ref_generation, owner_objectid,
1922 leaf = path->nodes[0];
1923 ei = btrfs_item_ptr(leaf, extent_slot,
1924 struct btrfs_extent_item);
1925 refs = btrfs_extent_refs(leaf, ei);
1928 btrfs_set_extent_refs(leaf, ei, refs);
1930 btrfs_mark_buffer_dirty(leaf);
1932 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1933 struct btrfs_extent_ref *ref;
1934 ref = btrfs_item_ptr(leaf, path->slots[0],
1935 struct btrfs_extent_ref);
1936 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1937 /* if the back ref and the extent are next to each other
1938 * they get deleted below in one shot
1940 path->slots[0] = extent_slot;
1942 } else if (found_extent) {
1943 /* otherwise delete the extent back ref */
1944 ret = remove_extent_backref(trans, extent_root, path);
1946 /* if refs are 0, we need to setup the path for deletion */
1948 btrfs_release_path(extent_root, path);
1949 ret = btrfs_search_slot(trans, extent_root, &key, path,
1958 #ifdef BIO_RW_DISCARD
1959 u64 map_length = num_bytes;
1960 struct btrfs_multi_bio *multi = NULL;
1964 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1965 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
1971 /* block accounting for super block */
1972 spin_lock_irq(&info->delalloc_lock);
1973 super_used = btrfs_super_bytes_used(&info->super_copy);
1974 btrfs_set_super_bytes_used(&info->super_copy,
1975 super_used - num_bytes);
1976 spin_unlock_irq(&info->delalloc_lock);
1978 /* block accounting for root item */
1979 root_used = btrfs_root_used(&root->root_item);
1980 btrfs_set_root_used(&root->root_item,
1981 root_used - num_bytes);
1982 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1985 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1989 #ifdef BIO_RW_DISCARD
1990 /* Tell the block device(s) that the sectors can be discarded */
1991 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1992 bytenr, &map_length, &multi, 0);
1994 struct btrfs_bio_stripe *stripe = multi->stripes;
1997 if (map_length > num_bytes)
1998 map_length = num_bytes;
2000 for (i = 0; i < multi->num_stripes; i++, stripe++) {
2001 blkdev_issue_discard(stripe->dev->bdev,
2002 stripe->physical >> 9,
2009 btrfs_free_path(path);
2010 finish_current_insert(trans, extent_root);
2015 * find all the blocks marked as pending in the radix tree and remove
2016 * them from the extent map
2018 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2019 btrfs_root *extent_root)
2027 struct extent_io_tree *pending_del;
2028 struct extent_io_tree *extent_ins;
2029 struct pending_extent_op *extent_op;
2031 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2032 extent_ins = &extent_root->fs_info->extent_ins;
2033 pending_del = &extent_root->fs_info->pending_del;
2036 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2041 ret = get_state_private(pending_del, start, &priv);
2043 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2045 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2048 ret = pin_down_bytes(trans, extent_root, start,
2049 end + 1 - start, 0);
2050 mark_free = ret > 0;
2051 if (!test_range_bit(extent_ins, start, end,
2052 EXTENT_LOCKED, 0)) {
2054 ret = __free_extent(trans, extent_root,
2055 start, end + 1 - start,
2056 extent_op->orig_parent,
2057 extent_root->root_key.objectid,
2058 extent_op->orig_generation,
2059 extent_op->level, 0, 0, mark_free);
2063 ret = get_state_private(extent_ins, start, &priv);
2065 extent_op = (struct pending_extent_op *)
2066 (unsigned long)priv;
2068 clear_extent_bits(extent_ins, start, end,
2069 EXTENT_LOCKED, GFP_NOFS);
2071 if (extent_op->type == PENDING_BACKREF_UPDATE)
2074 ret = update_block_group(trans, extent_root, start,
2075 end + 1 - start, 0, mark_free);
2082 if (need_resched()) {
2083 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2085 mutex_lock(&extent_root->fs_info->alloc_mutex);
2092 * remove an extent from the root, returns 0 on success
2094 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2095 struct btrfs_root *root,
2096 u64 bytenr, u64 num_bytes, u64 parent,
2097 u64 root_objectid, u64 ref_generation,
2098 u64 owner_objectid, u64 owner_offset, int pin)
2100 struct btrfs_root *extent_root = root->fs_info->extent_root;
2104 WARN_ON(num_bytes < root->sectorsize);
2105 if (root == extent_root) {
2106 struct pending_extent_op *extent_op;
2108 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2111 extent_op->type = PENDING_EXTENT_DELETE;
2112 extent_op->bytenr = bytenr;
2113 extent_op->num_bytes = num_bytes;
2114 extent_op->parent = parent;
2115 extent_op->orig_parent = parent;
2116 extent_op->generation = ref_generation;
2117 extent_op->orig_generation = ref_generation;
2118 extent_op->level = (int)owner_objectid;
2120 set_extent_bits(&root->fs_info->pending_del,
2121 bytenr, bytenr + num_bytes - 1,
2122 EXTENT_LOCKED, GFP_NOFS);
2123 set_state_private(&root->fs_info->pending_del,
2124 bytenr, (unsigned long)extent_op);
2127 /* if metadata always pin */
2128 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2129 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2130 struct btrfs_block_group_cache *cache;
2132 /* btrfs_free_reserved_extent */
2133 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2135 btrfs_add_free_space(cache, bytenr, num_bytes);
2136 update_reserved_extents(root, bytenr, num_bytes, 0);
2142 /* if data pin when any transaction has committed this */
2143 if (ref_generation != trans->transid)
2146 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2147 root_objectid, ref_generation, owner_objectid,
2148 owner_offset, pin, pin == 0);
2150 finish_current_insert(trans, root->fs_info->extent_root);
2151 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2152 return ret ? ret : pending_ret;
2155 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2156 struct btrfs_root *root,
2157 u64 bytenr, u64 num_bytes, u64 parent,
2158 u64 root_objectid, u64 ref_generation,
2159 u64 owner_objectid, u64 owner_offset, int pin)
2163 maybe_lock_mutex(root);
2164 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2165 root_objectid, ref_generation,
2166 owner_objectid, owner_offset, pin);
2167 maybe_unlock_mutex(root);
2171 static u64 stripe_align(struct btrfs_root *root, u64 val)
2173 u64 mask = ((u64)root->stripesize - 1);
2174 u64 ret = (val + mask) & ~mask;
2179 * walks the btree of allocated extents and find a hole of a given size.
2180 * The key ins is changed to record the hole:
2181 * ins->objectid == block start
2182 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2183 * ins->offset == number of blocks
2184 * Any available blocks before search_start are skipped.
2186 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2187 struct btrfs_root *orig_root,
2188 u64 num_bytes, u64 empty_size,
2189 u64 search_start, u64 search_end,
2190 u64 hint_byte, struct btrfs_key *ins,
2191 u64 exclude_start, u64 exclude_nr,
2195 u64 orig_search_start;
2196 struct btrfs_root * root = orig_root->fs_info->extent_root;
2197 struct btrfs_fs_info *info = root->fs_info;
2198 u64 total_needed = num_bytes;
2199 u64 *last_ptr = NULL;
2200 struct btrfs_block_group_cache *block_group;
2201 int chunk_alloc_done = 0;
2202 int empty_cluster = 2 * 1024 * 1024;
2203 int allowed_chunk_alloc = 0;
2205 WARN_ON(num_bytes < root->sectorsize);
2206 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2208 if (orig_root->ref_cows || empty_size)
2209 allowed_chunk_alloc = 1;
2211 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2212 last_ptr = &root->fs_info->last_alloc;
2213 empty_cluster = 256 * 1024;
2216 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2217 last_ptr = &root->fs_info->last_data_alloc;
2219 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2220 last_ptr = &root->fs_info->last_log_alloc;
2221 if (!last_ptr == 0 && root->fs_info->last_alloc) {
2222 *last_ptr = root->fs_info->last_alloc + empty_cluster;
2228 hint_byte = *last_ptr;
2230 empty_size += empty_cluster;
2233 search_start = max(search_start, first_logical_byte(root, 0));
2234 orig_search_start = search_start;
2236 search_start = max(search_start, hint_byte);
2237 total_needed += empty_size;
2240 block_group = btrfs_lookup_first_block_group(info, search_start);
2243 * Ok this looks a little tricky, buts its really simple. First if we
2244 * didn't find a block group obviously we want to start over.
2245 * Secondly, if the block group we found does not match the type we
2246 * need, and we have a last_ptr and its not 0, chances are the last
2247 * allocation we made was at the end of the block group, so lets go
2248 * ahead and skip the looking through the rest of the block groups and
2249 * start at the beginning. This helps with metadata allocations,
2250 * since you are likely to have a bunch of data block groups to search
2251 * through first before you realize that you need to start over, so go
2252 * ahead and start over and save the time.
2254 if (!block_group || (!block_group_bits(block_group, data) &&
2255 last_ptr && *last_ptr)) {
2256 if (search_start != orig_search_start) {
2257 if (last_ptr && *last_ptr)
2259 search_start = orig_search_start;
2261 } else if (!chunk_alloc_done && allowed_chunk_alloc) {
2262 ret = do_chunk_alloc(trans, root,
2263 num_bytes + 2 * 1024 * 1024,
2268 chunk_alloc_done = 1;
2269 search_start = orig_search_start;
2278 * this is going to seach through all of the existing block groups it
2279 * can find, so if we don't find something we need to see if we can
2280 * allocate what we need.
2282 ret = find_free_space(root, &block_group, &search_start,
2283 total_needed, data);
2284 if (ret == -ENOSPC) {
2286 * instead of allocating, start at the original search start
2287 * and see if there is something to be found, if not then we
2290 if (search_start != orig_search_start) {
2291 if (last_ptr && *last_ptr) {
2293 total_needed += empty_cluster;
2295 search_start = orig_search_start;
2300 * we've already allocated, we're pretty screwed
2302 if (chunk_alloc_done) {
2304 } else if (!allowed_chunk_alloc && block_group &&
2305 block_group_bits(block_group, data)) {
2306 block_group->space_info->force_alloc = 1;
2308 } else if (!allowed_chunk_alloc) {
2312 ret = do_chunk_alloc(trans, root, num_bytes + 2 * 1024 * 1024,
2318 chunk_alloc_done = 1;
2320 search_start = block_group->key.objectid +
2321 block_group->key.offset;
2323 search_start = orig_search_start;
2330 search_start = stripe_align(root, search_start);
2331 ins->objectid = search_start;
2332 ins->offset = num_bytes;
2334 if (ins->objectid + num_bytes >= search_end) {
2335 search_start = orig_search_start;
2336 if (chunk_alloc_done) {
2343 if (ins->objectid + num_bytes >
2344 block_group->key.objectid + block_group->key.offset) {
2345 if (search_start == orig_search_start && chunk_alloc_done) {
2349 search_start = block_group->key.objectid +
2350 block_group->key.offset;
2354 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2355 ins->objectid < exclude_start + exclude_nr)) {
2356 search_start = exclude_start + exclude_nr;
2360 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2361 trans->block_group = block_group;
2363 ins->offset = num_bytes;
2365 *last_ptr = ins->objectid + ins->offset;
2367 btrfs_super_total_bytes(&root->fs_info->super_copy))
2376 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2378 struct btrfs_block_group_cache *cache;
2379 struct list_head *l;
2381 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2382 info->total_bytes - info->bytes_used - info->bytes_pinned -
2383 info->bytes_reserved, (info->full) ? "" : "not ");
2385 spin_lock(&info->lock);
2386 list_for_each(l, &info->block_groups) {
2387 cache = list_entry(l, struct btrfs_block_group_cache, list);
2388 spin_lock(&cache->lock);
2389 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
2390 "%Lu pinned %Lu reserved\n",
2391 cache->key.objectid, cache->key.offset,
2392 btrfs_block_group_used(&cache->item),
2393 cache->pinned, cache->reserved);
2394 btrfs_dump_free_space(cache, bytes);
2395 spin_unlock(&cache->lock);
2397 spin_unlock(&info->lock);
2400 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2401 struct btrfs_root *root,
2402 u64 num_bytes, u64 min_alloc_size,
2403 u64 empty_size, u64 hint_byte,
2404 u64 search_end, struct btrfs_key *ins,
2408 u64 search_start = 0;
2410 struct btrfs_fs_info *info = root->fs_info;
2411 struct btrfs_block_group_cache *cache;
2414 alloc_profile = info->avail_data_alloc_bits &
2415 info->data_alloc_profile;
2416 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2417 } else if (root == root->fs_info->chunk_root) {
2418 alloc_profile = info->avail_system_alloc_bits &
2419 info->system_alloc_profile;
2420 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2422 alloc_profile = info->avail_metadata_alloc_bits &
2423 info->metadata_alloc_profile;
2424 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2427 data = reduce_alloc_profile(root, data);
2429 * the only place that sets empty_size is btrfs_realloc_node, which
2430 * is not called recursively on allocations
2432 if (empty_size || root->ref_cows) {
2433 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2434 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2436 BTRFS_BLOCK_GROUP_METADATA |
2437 (info->metadata_alloc_profile &
2438 info->avail_metadata_alloc_bits), 0);
2440 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2441 num_bytes + 2 * 1024 * 1024, data, 0);
2444 WARN_ON(num_bytes < root->sectorsize);
2445 ret = find_free_extent(trans, root, num_bytes, empty_size,
2446 search_start, search_end, hint_byte, ins,
2447 trans->alloc_exclude_start,
2448 trans->alloc_exclude_nr, data);
2450 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2451 num_bytes = num_bytes >> 1;
2452 num_bytes = num_bytes & ~(root->sectorsize - 1);
2453 num_bytes = max(num_bytes, min_alloc_size);
2454 do_chunk_alloc(trans, root->fs_info->extent_root,
2455 num_bytes, data, 1);
2459 struct btrfs_space_info *sinfo;
2461 sinfo = __find_space_info(root->fs_info, data);
2462 printk("allocation failed flags %Lu, wanted %Lu\n",
2464 dump_space_info(sinfo, num_bytes);
2467 cache = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2469 printk(KERN_ERR "Unable to find block group for %Lu\n", ins->objectid);
2473 ret = btrfs_remove_free_space(cache, ins->objectid, ins->offset);
2478 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2480 struct btrfs_block_group_cache *cache;
2482 maybe_lock_mutex(root);
2483 cache = btrfs_lookup_block_group(root->fs_info, start);
2485 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2486 maybe_unlock_mutex(root);
2489 btrfs_add_free_space(cache, start, len);
2490 maybe_unlock_mutex(root);
2494 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2495 struct btrfs_root *root,
2496 u64 num_bytes, u64 min_alloc_size,
2497 u64 empty_size, u64 hint_byte,
2498 u64 search_end, struct btrfs_key *ins,
2502 maybe_lock_mutex(root);
2503 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2504 empty_size, hint_byte, search_end, ins,
2506 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2507 maybe_unlock_mutex(root);
2511 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2512 struct btrfs_root *root, u64 parent,
2513 u64 root_objectid, u64 ref_generation,
2514 u64 owner, u64 owner_offset,
2515 struct btrfs_key *ins)
2521 u64 num_bytes = ins->offset;
2523 struct btrfs_fs_info *info = root->fs_info;
2524 struct btrfs_root *extent_root = info->extent_root;
2525 struct btrfs_extent_item *extent_item;
2526 struct btrfs_extent_ref *ref;
2527 struct btrfs_path *path;
2528 struct btrfs_key keys[2];
2531 parent = ins->objectid;
2533 /* block accounting for super block */
2534 spin_lock_irq(&info->delalloc_lock);
2535 super_used = btrfs_super_bytes_used(&info->super_copy);
2536 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2537 spin_unlock_irq(&info->delalloc_lock);
2539 /* block accounting for root item */
2540 root_used = btrfs_root_used(&root->root_item);
2541 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2543 if (root == extent_root) {
2544 struct pending_extent_op *extent_op;
2546 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2549 extent_op->type = PENDING_EXTENT_INSERT;
2550 extent_op->bytenr = ins->objectid;
2551 extent_op->num_bytes = ins->offset;
2552 extent_op->parent = parent;
2553 extent_op->orig_parent = 0;
2554 extent_op->generation = ref_generation;
2555 extent_op->orig_generation = 0;
2556 extent_op->level = (int)owner;
2558 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2559 ins->objectid + ins->offset - 1,
2560 EXTENT_LOCKED, GFP_NOFS);
2561 set_state_private(&root->fs_info->extent_ins,
2562 ins->objectid, (unsigned long)extent_op);
2566 memcpy(&keys[0], ins, sizeof(*ins));
2567 keys[1].objectid = ins->objectid;
2568 keys[1].type = BTRFS_EXTENT_REF_KEY;
2569 keys[1].offset = parent;
2570 sizes[0] = sizeof(*extent_item);
2571 sizes[1] = sizeof(*ref);
2573 path = btrfs_alloc_path();
2576 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2580 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2581 struct btrfs_extent_item);
2582 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2583 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2584 struct btrfs_extent_ref);
2586 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2587 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2588 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2589 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2590 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
2592 btrfs_mark_buffer_dirty(path->nodes[0]);
2594 trans->alloc_exclude_start = 0;
2595 trans->alloc_exclude_nr = 0;
2596 btrfs_free_path(path);
2597 finish_current_insert(trans, extent_root);
2598 pending_ret = del_pending_extents(trans, extent_root);
2608 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
2610 printk("update block group failed for %Lu %Lu\n",
2611 ins->objectid, ins->offset);
2618 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2619 struct btrfs_root *root, u64 parent,
2620 u64 root_objectid, u64 ref_generation,
2621 u64 owner, u64 owner_offset,
2622 struct btrfs_key *ins)
2626 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2628 maybe_lock_mutex(root);
2629 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2630 root_objectid, ref_generation,
2631 owner, owner_offset, ins);
2632 update_reserved_extents(root, ins->objectid, ins->offset, 0);
2633 maybe_unlock_mutex(root);
2638 * this is used by the tree logging recovery code. It records that
2639 * an extent has been allocated and makes sure to clear the free
2640 * space cache bits as well
2642 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root, u64 parent,
2644 u64 root_objectid, u64 ref_generation,
2645 u64 owner, u64 owner_offset,
2646 struct btrfs_key *ins)
2649 struct btrfs_block_group_cache *block_group;
2651 maybe_lock_mutex(root);
2652 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2653 cache_block_group(root, block_group);
2655 ret = btrfs_remove_free_space(block_group, ins->objectid, ins->offset);
2657 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2658 root_objectid, ref_generation,
2659 owner, owner_offset, ins);
2660 maybe_unlock_mutex(root);
2665 * finds a free extent and does all the dirty work required for allocation
2666 * returns the key for the extent through ins, and a tree buffer for
2667 * the first block of the extent through buf.
2669 * returns 0 if everything worked, non-zero otherwise.
2671 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2672 struct btrfs_root *root,
2673 u64 num_bytes, u64 parent, u64 min_alloc_size,
2674 u64 root_objectid, u64 ref_generation,
2675 u64 owner_objectid, u64 owner_offset,
2676 u64 empty_size, u64 hint_byte,
2677 u64 search_end, struct btrfs_key *ins, u64 data)
2681 maybe_lock_mutex(root);
2683 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2684 min_alloc_size, empty_size, hint_byte,
2685 search_end, ins, data);
2687 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2688 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2689 root_objectid, ref_generation,
2690 owner_objectid, owner_offset, ins);
2694 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2696 maybe_unlock_mutex(root);
2700 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2701 struct btrfs_root *root,
2702 u64 bytenr, u32 blocksize)
2704 struct extent_buffer *buf;
2706 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2708 return ERR_PTR(-ENOMEM);
2709 btrfs_set_header_generation(buf, trans->transid);
2710 btrfs_tree_lock(buf);
2711 clean_tree_block(trans, root, buf);
2712 btrfs_set_buffer_uptodate(buf);
2713 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2714 set_extent_dirty(&root->dirty_log_pages, buf->start,
2715 buf->start + buf->len - 1, GFP_NOFS);
2717 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2718 buf->start + buf->len - 1, GFP_NOFS);
2720 trans->blocks_used++;
2725 * helper function to allocate a block for a given tree
2726 * returns the tree buffer or NULL.
2728 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2729 struct btrfs_root *root,
2730 u32 blocksize, u64 parent,
2737 struct btrfs_key ins;
2739 struct extent_buffer *buf;
2741 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2742 root_objectid, ref_generation, level, 0,
2743 empty_size, hint, (u64)-1, &ins, 0);
2746 return ERR_PTR(ret);
2749 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
2753 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2754 struct btrfs_root *root, struct extent_buffer *leaf)
2757 u64 leaf_generation;
2758 struct btrfs_key key;
2759 struct btrfs_file_extent_item *fi;
2764 BUG_ON(!btrfs_is_leaf(leaf));
2765 nritems = btrfs_header_nritems(leaf);
2766 leaf_owner = btrfs_header_owner(leaf);
2767 leaf_generation = btrfs_header_generation(leaf);
2769 for (i = 0; i < nritems; i++) {
2773 btrfs_item_key_to_cpu(leaf, &key, i);
2774 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2776 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2777 if (btrfs_file_extent_type(leaf, fi) ==
2778 BTRFS_FILE_EXTENT_INLINE)
2781 * FIXME make sure to insert a trans record that
2782 * repeats the snapshot del on crash
2784 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2785 if (disk_bytenr == 0)
2788 mutex_lock(&root->fs_info->alloc_mutex);
2789 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2790 btrfs_file_extent_disk_num_bytes(leaf, fi),
2791 leaf->start, leaf_owner, leaf_generation,
2792 key.objectid, key.offset, 0);
2793 mutex_unlock(&root->fs_info->alloc_mutex);
2796 atomic_inc(&root->fs_info->throttle_gen);
2797 wake_up(&root->fs_info->transaction_throttle);
2803 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct btrfs_leaf_ref *ref)
2809 struct btrfs_extent_info *info = ref->extents;
2811 for (i = 0; i < ref->nritems; i++) {
2812 mutex_lock(&root->fs_info->alloc_mutex);
2813 ret = __btrfs_free_extent(trans, root, info->bytenr,
2814 info->num_bytes, ref->bytenr,
2815 ref->owner, ref->generation,
2816 info->objectid, info->offset, 0);
2817 mutex_unlock(&root->fs_info->alloc_mutex);
2819 atomic_inc(&root->fs_info->throttle_gen);
2820 wake_up(&root->fs_info->transaction_throttle);
2830 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2835 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
2838 #if 0 // some debugging code in case we see problems here
2839 /* if the refs count is one, it won't get increased again. But
2840 * if the ref count is > 1, someone may be decreasing it at
2841 * the same time we are.
2844 struct extent_buffer *eb = NULL;
2845 eb = btrfs_find_create_tree_block(root, start, len);
2847 btrfs_tree_lock(eb);
2849 mutex_lock(&root->fs_info->alloc_mutex);
2850 ret = lookup_extent_ref(NULL, root, start, len, refs);
2852 mutex_unlock(&root->fs_info->alloc_mutex);
2855 btrfs_tree_unlock(eb);
2856 free_extent_buffer(eb);
2859 printk("block %llu went down to one during drop_snap\n",
2860 (unsigned long long)start);
2871 * helper function for drop_snapshot, this walks down the tree dropping ref
2872 * counts as it goes.
2874 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2875 struct btrfs_root *root,
2876 struct btrfs_path *path, int *level)
2882 struct extent_buffer *next;
2883 struct extent_buffer *cur;
2884 struct extent_buffer *parent;
2885 struct btrfs_leaf_ref *ref;
2890 WARN_ON(*level < 0);
2891 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2892 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
2893 path->nodes[*level]->len, &refs);
2899 * walk down to the last node level and free all the leaves
2901 while(*level >= 0) {
2902 WARN_ON(*level < 0);
2903 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2904 cur = path->nodes[*level];
2906 if (btrfs_header_level(cur) != *level)
2909 if (path->slots[*level] >=
2910 btrfs_header_nritems(cur))
2913 ret = btrfs_drop_leaf_ref(trans, root, cur);
2917 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2918 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2919 blocksize = btrfs_level_size(root, *level - 1);
2921 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
2924 parent = path->nodes[*level];
2925 root_owner = btrfs_header_owner(parent);
2926 root_gen = btrfs_header_generation(parent);
2927 path->slots[*level]++;
2929 mutex_lock(&root->fs_info->alloc_mutex);
2930 ret = __btrfs_free_extent(trans, root, bytenr,
2931 blocksize, parent->start,
2932 root_owner, root_gen, 0, 0, 1);
2934 mutex_unlock(&root->fs_info->alloc_mutex);
2936 atomic_inc(&root->fs_info->throttle_gen);
2937 wake_up(&root->fs_info->transaction_throttle);
2943 * at this point, we have a single ref, and since the
2944 * only place referencing this extent is a dead root
2945 * the reference count should never go higher.
2946 * So, we don't need to check it again
2949 ref = btrfs_lookup_leaf_ref(root, bytenr);
2951 ret = cache_drop_leaf_ref(trans, root, ref);
2953 btrfs_remove_leaf_ref(root, ref);
2954 btrfs_free_leaf_ref(root, ref);
2958 if (printk_ratelimit())
2959 printk("leaf ref miss for bytenr %llu\n",
2960 (unsigned long long)bytenr);
2962 next = btrfs_find_tree_block(root, bytenr, blocksize);
2963 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2964 free_extent_buffer(next);
2966 next = read_tree_block(root, bytenr, blocksize,
2971 * this is a debugging check and can go away
2972 * the ref should never go all the way down to 1
2975 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2981 WARN_ON(*level <= 0);
2982 if (path->nodes[*level-1])
2983 free_extent_buffer(path->nodes[*level-1]);
2984 path->nodes[*level-1] = next;
2985 *level = btrfs_header_level(next);
2986 path->slots[*level] = 0;
2990 WARN_ON(*level < 0);
2991 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2993 if (path->nodes[*level] == root->node) {
2994 parent = path->nodes[*level];
2995 bytenr = path->nodes[*level]->start;
2997 parent = path->nodes[*level + 1];
2998 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3001 blocksize = btrfs_level_size(root, *level);
3002 root_owner = btrfs_header_owner(parent);
3003 root_gen = btrfs_header_generation(parent);
3005 mutex_lock(&root->fs_info->alloc_mutex);
3006 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
3007 parent->start, root_owner, root_gen,
3009 mutex_unlock(&root->fs_info->alloc_mutex);
3010 free_extent_buffer(path->nodes[*level]);
3011 path->nodes[*level] = NULL;
3020 * helper for dropping snapshots. This walks back up the tree in the path
3021 * to find the first node higher up where we haven't yet gone through
3024 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3025 struct btrfs_root *root,
3026 struct btrfs_path *path, int *level)
3030 struct btrfs_root_item *root_item = &root->root_item;
3035 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3036 slot = path->slots[i];
3037 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3038 struct extent_buffer *node;
3039 struct btrfs_disk_key disk_key;
3040 node = path->nodes[i];
3043 WARN_ON(*level == 0);
3044 btrfs_node_key(node, &disk_key, path->slots[i]);
3045 memcpy(&root_item->drop_progress,
3046 &disk_key, sizeof(disk_key));
3047 root_item->drop_level = i;
3050 struct extent_buffer *parent;
3051 if (path->nodes[*level] == root->node)
3052 parent = path->nodes[*level];
3054 parent = path->nodes[*level + 1];
3056 root_owner = btrfs_header_owner(parent);
3057 root_gen = btrfs_header_generation(parent);
3058 ret = btrfs_free_extent(trans, root,
3059 path->nodes[*level]->start,
3060 path->nodes[*level]->len,
3062 root_owner, root_gen, 0, 0, 1);
3064 free_extent_buffer(path->nodes[*level]);
3065 path->nodes[*level] = NULL;
3073 * drop the reference count on the tree rooted at 'snap'. This traverses
3074 * the tree freeing any blocks that have a ref count of zero after being
3077 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3083 struct btrfs_path *path;
3086 struct btrfs_root_item *root_item = &root->root_item;
3088 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3089 path = btrfs_alloc_path();
3092 level = btrfs_header_level(root->node);
3094 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3095 path->nodes[level] = root->node;
3096 extent_buffer_get(root->node);
3097 path->slots[level] = 0;
3099 struct btrfs_key key;
3100 struct btrfs_disk_key found_key;
3101 struct extent_buffer *node;
3103 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3104 level = root_item->drop_level;
3105 path->lowest_level = level;
3106 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3111 node = path->nodes[level];
3112 btrfs_node_key(node, &found_key, path->slots[level]);
3113 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3114 sizeof(found_key)));
3116 * unlock our path, this is safe because only this
3117 * function is allowed to delete this snapshot
3119 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3120 if (path->nodes[i] && path->locks[i]) {
3122 btrfs_tree_unlock(path->nodes[i]);
3127 wret = walk_down_tree(trans, root, path, &level);
3133 wret = walk_up_tree(trans, root, path, &level);
3138 if (trans->transaction->in_commit) {
3142 atomic_inc(&root->fs_info->throttle_gen);
3143 wake_up(&root->fs_info->transaction_throttle);
3145 for (i = 0; i <= orig_level; i++) {
3146 if (path->nodes[i]) {
3147 free_extent_buffer(path->nodes[i]);
3148 path->nodes[i] = NULL;
3152 btrfs_free_path(path);
3156 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3158 struct btrfs_block_group_cache *block_group;
3161 mutex_lock(&info->alloc_mutex);
3162 spin_lock(&info->block_group_cache_lock);
3163 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
3164 block_group = rb_entry(n, struct btrfs_block_group_cache,
3167 spin_unlock(&info->block_group_cache_lock);
3168 btrfs_remove_free_space_cache(block_group);
3169 spin_lock(&info->block_group_cache_lock);
3171 rb_erase(&block_group->cache_node,
3172 &info->block_group_cache_tree);
3174 spin_lock(&block_group->space_info->lock);
3175 list_del(&block_group->list);
3176 spin_unlock(&block_group->space_info->lock);
3179 spin_unlock(&info->block_group_cache_lock);
3180 mutex_unlock(&info->alloc_mutex);
3184 static unsigned long calc_ra(unsigned long start, unsigned long last,
3187 return min(last, start + nr - 1);
3190 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3195 unsigned long last_index;
3198 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3199 struct file_ra_state *ra;
3200 unsigned long total_read = 0;
3201 unsigned long ra_pages;
3202 struct btrfs_ordered_extent *ordered;
3203 struct btrfs_trans_handle *trans;
3205 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3207 mutex_lock(&inode->i_mutex);
3208 i = start >> PAGE_CACHE_SHIFT;
3209 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3211 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
3213 file_ra_state_init(ra, inode->i_mapping);
3215 for (; i <= last_index; i++) {
3216 if (total_read % ra_pages == 0) {
3217 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3218 calc_ra(i, last_index, ra_pages));
3222 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3223 goto truncate_racing;
3224 page = grab_cache_page(inode->i_mapping, i);
3228 if (!PageUptodate(page)) {
3229 btrfs_readpage(NULL, page);
3231 if (!PageUptodate(page)) {
3233 page_cache_release(page);
3237 wait_on_page_writeback(page);
3239 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3240 page_end = page_start + PAGE_CACHE_SIZE - 1;
3241 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3243 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3245 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3247 page_cache_release(page);
3248 btrfs_start_ordered_extent(inode, ordered, 1);
3249 btrfs_put_ordered_extent(ordered);
3252 set_page_extent_mapped(page);
3255 * make sure page_mkwrite is called for this page if userland
3256 * wants to change it from mmap
3258 clear_page_dirty_for_io(page);
3260 btrfs_set_extent_delalloc(inode, page_start, page_end);
3261 set_page_dirty(page);
3263 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3265 page_cache_release(page);
3269 /* we have to start the IO in order to get the ordered extents
3270 * instantiated. This allows the relocation to code to wait
3271 * for all the ordered extents to hit the disk.
3273 * Otherwise, it would constantly loop over the same extents
3274 * because the old ones don't get deleted until the IO is
3277 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
3280 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
3282 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
3283 mark_inode_dirty(inode);
3285 mutex_unlock(&inode->i_mutex);
3289 vmtruncate(inode, inode->i_size);
3290 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
3296 * The back references tell us which tree holds a ref on a block,
3297 * but it is possible for the tree root field in the reference to
3298 * reflect the original root before a snapshot was made. In this
3299 * case we should search through all the children of a given root
3300 * to find potential holders of references on a block.
3302 * Instead, we do something a little less fancy and just search
3303 * all the roots for a given key/block combination.
3305 static int find_root_for_ref(struct btrfs_root *root,
3306 struct btrfs_path *path,
3307 struct btrfs_key *key0,
3310 struct btrfs_root **found_root,
3313 struct btrfs_key root_location;
3314 struct btrfs_root *cur_root = *found_root;
3315 struct btrfs_file_extent_item *file_extent;
3316 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3320 root_location.offset = (u64)-1;
3321 root_location.type = BTRFS_ROOT_ITEM_KEY;
3322 path->lowest_level = level;
3325 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3327 if (ret == 0 && file_key) {
3328 struct extent_buffer *leaf = path->nodes[0];
3329 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3330 struct btrfs_file_extent_item);
3331 if (btrfs_file_extent_type(leaf, file_extent) ==
3332 BTRFS_FILE_EXTENT_REG) {
3334 btrfs_file_extent_disk_bytenr(leaf,
3337 } else if (!file_key) {
3338 if (path->nodes[level])
3339 found_bytenr = path->nodes[level]->start;
3342 btrfs_release_path(cur_root, path);
3344 if (found_bytenr == bytenr) {
3345 *found_root = cur_root;
3349 ret = btrfs_search_root(root->fs_info->tree_root,
3350 root_search_start, &root_search_start);
3354 root_location.objectid = root_search_start;
3355 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3363 path->lowest_level = 0;
3368 * note, this releases the path
3370 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
3371 struct btrfs_path *path,
3372 struct btrfs_key *extent_key,
3373 u64 *last_file_objectid,
3374 u64 *last_file_offset,
3375 u64 *last_file_root,
3378 struct inode *inode;
3379 struct btrfs_root *found_root;
3380 struct btrfs_key root_location;
3381 struct btrfs_key found_key;
3382 struct btrfs_extent_ref *ref;
3390 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3392 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3393 struct btrfs_extent_ref);
3394 ref_root = btrfs_ref_root(path->nodes[0], ref);
3395 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3396 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3397 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3398 btrfs_release_path(extent_root, path);
3400 root_location.objectid = ref_root;
3402 root_location.offset = 0;
3404 root_location.offset = (u64)-1;
3405 root_location.type = BTRFS_ROOT_ITEM_KEY;
3407 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
3409 BUG_ON(!found_root);
3410 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3412 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
3413 found_key.objectid = ref_objectid;
3414 found_key.type = BTRFS_EXTENT_DATA_KEY;
3415 found_key.offset = ref_offset;
3418 if (last_extent == extent_key->objectid &&
3419 *last_file_objectid == ref_objectid &&
3420 *last_file_offset == ref_offset &&
3421 *last_file_root == ref_root)
3424 ret = find_root_for_ref(extent_root, path, &found_key,
3425 level, 1, &found_root,
3426 extent_key->objectid);
3431 if (last_extent == extent_key->objectid &&
3432 *last_file_objectid == ref_objectid &&
3433 *last_file_offset == ref_offset &&
3434 *last_file_root == ref_root)
3437 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3438 ref_objectid, found_root);
3439 if (inode->i_state & I_NEW) {
3440 /* the inode and parent dir are two different roots */
3441 BTRFS_I(inode)->root = found_root;
3442 BTRFS_I(inode)->location.objectid = ref_objectid;
3443 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3444 BTRFS_I(inode)->location.offset = 0;
3445 btrfs_read_locked_inode(inode);
3446 unlock_new_inode(inode);
3449 /* this can happen if the reference is not against
3450 * the latest version of the tree root
3452 if (is_bad_inode(inode))
3455 *last_file_objectid = inode->i_ino;
3456 *last_file_root = found_root->root_key.objectid;
3457 *last_file_offset = ref_offset;
3459 relocate_inode_pages(inode, ref_offset, extent_key->offset);
3462 struct btrfs_trans_handle *trans;
3463 struct extent_buffer *eb;
3466 eb = read_tree_block(found_root, extent_key->objectid,
3467 extent_key->offset, 0);
3468 btrfs_tree_lock(eb);
3469 level = btrfs_header_level(eb);
3472 btrfs_item_key_to_cpu(eb, &found_key, 0);
3474 btrfs_node_key_to_cpu(eb, &found_key, 0);
3476 btrfs_tree_unlock(eb);
3477 free_extent_buffer(eb);
3479 ret = find_root_for_ref(extent_root, path, &found_key,
3480 level, 0, &found_root,
3481 extent_key->objectid);
3487 * right here almost anything could happen to our key,
3488 * but that's ok. The cow below will either relocate it
3489 * or someone else will have relocated it. Either way,
3490 * it is in a different spot than it was before and
3494 trans = btrfs_start_transaction(found_root, 1);
3496 if (found_root == extent_root->fs_info->extent_root ||
3497 found_root == extent_root->fs_info->chunk_root ||
3498 found_root == extent_root->fs_info->dev_root) {
3500 mutex_lock(&extent_root->fs_info->alloc_mutex);
3503 path->lowest_level = level;
3505 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3507 path->lowest_level = 0;
3508 btrfs_release_path(found_root, path);
3510 if (found_root == found_root->fs_info->extent_root)
3511 btrfs_extent_post_op(trans, found_root);
3513 mutex_unlock(&extent_root->fs_info->alloc_mutex);
3515 btrfs_end_transaction(trans, found_root);
3519 mutex_lock(&extent_root->fs_info->alloc_mutex);
3523 static int noinline del_extent_zero(struct btrfs_root *extent_root,
3524 struct btrfs_path *path,
3525 struct btrfs_key *extent_key)
3528 struct btrfs_trans_handle *trans;
3530 trans = btrfs_start_transaction(extent_root, 1);
3531 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3538 ret = btrfs_del_item(trans, extent_root, path);
3540 btrfs_end_transaction(trans, extent_root);
3544 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3545 struct btrfs_path *path,
3546 struct btrfs_key *extent_key)
3548 struct btrfs_key key;
3549 struct btrfs_key found_key;
3550 struct extent_buffer *leaf;
3551 u64 last_file_objectid = 0;
3552 u64 last_file_root = 0;
3553 u64 last_file_offset = (u64)-1;
3554 u64 last_extent = 0;
3559 if (extent_key->objectid == 0) {
3560 ret = del_extent_zero(extent_root, path, extent_key);
3563 key.objectid = extent_key->objectid;
3564 key.type = BTRFS_EXTENT_REF_KEY;
3568 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3574 leaf = path->nodes[0];
3575 nritems = btrfs_header_nritems(leaf);
3576 if (path->slots[0] == nritems) {
3577 ret = btrfs_next_leaf(extent_root, path);
3584 leaf = path->nodes[0];
3587 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3588 if (found_key.objectid != extent_key->objectid) {
3592 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
3596 key.offset = found_key.offset + 1;
3597 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3599 ret = relocate_one_reference(extent_root, path, extent_key,
3600 &last_file_objectid,
3602 &last_file_root, last_extent);
3605 last_extent = extent_key->objectid;
3609 btrfs_release_path(extent_root, path);
3613 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3616 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3617 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3619 num_devices = root->fs_info->fs_devices->num_devices;
3620 if (num_devices == 1) {
3621 stripped |= BTRFS_BLOCK_GROUP_DUP;
3622 stripped = flags & ~stripped;
3624 /* turn raid0 into single device chunks */
3625 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3628 /* turn mirroring into duplication */
3629 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3630 BTRFS_BLOCK_GROUP_RAID10))
3631 return stripped | BTRFS_BLOCK_GROUP_DUP;
3634 /* they already had raid on here, just return */
3635 if (flags & stripped)
3638 stripped |= BTRFS_BLOCK_GROUP_DUP;
3639 stripped = flags & ~stripped;
3641 /* switch duplicated blocks with raid1 */
3642 if (flags & BTRFS_BLOCK_GROUP_DUP)
3643 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3645 /* turn single device chunks into raid0 */
3646 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3651 int __alloc_chunk_for_shrink(struct btrfs_root *root,
3652 struct btrfs_block_group_cache *shrink_block_group,
3655 struct btrfs_trans_handle *trans;
3656 u64 new_alloc_flags;
3659 spin_lock(&shrink_block_group->lock);
3660 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
3661 spin_unlock(&shrink_block_group->lock);
3662 mutex_unlock(&root->fs_info->alloc_mutex);
3664 trans = btrfs_start_transaction(root, 1);
3665 mutex_lock(&root->fs_info->alloc_mutex);
3666 spin_lock(&shrink_block_group->lock);
3668 new_alloc_flags = update_block_group_flags(root,
3669 shrink_block_group->flags);
3670 if (new_alloc_flags != shrink_block_group->flags) {
3672 btrfs_block_group_used(&shrink_block_group->item);
3674 calc = shrink_block_group->key.offset;
3676 spin_unlock(&shrink_block_group->lock);
3678 do_chunk_alloc(trans, root->fs_info->extent_root,
3679 calc + 2 * 1024 * 1024, new_alloc_flags, force);
3681 mutex_unlock(&root->fs_info->alloc_mutex);
3682 btrfs_end_transaction(trans, root);
3683 mutex_lock(&root->fs_info->alloc_mutex);
3685 spin_unlock(&shrink_block_group->lock);
3689 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
3691 struct btrfs_trans_handle *trans;
3692 struct btrfs_root *tree_root = root->fs_info->tree_root;
3693 struct btrfs_path *path;
3696 u64 shrink_last_byte;
3697 struct btrfs_block_group_cache *shrink_block_group;
3698 struct btrfs_key key;
3699 struct btrfs_key found_key;
3700 struct extent_buffer *leaf;
3705 mutex_lock(&root->fs_info->alloc_mutex);
3706 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3708 BUG_ON(!shrink_block_group);
3710 shrink_last_byte = shrink_block_group->key.objectid +
3711 shrink_block_group->key.offset;
3713 shrink_block_group->space_info->total_bytes -=
3714 shrink_block_group->key.offset;
3715 path = btrfs_alloc_path();
3716 root = root->fs_info->extent_root;
3719 printk("btrfs relocating block group %llu flags %llu\n",
3720 (unsigned long long)shrink_start,
3721 (unsigned long long)shrink_block_group->flags);
3723 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
3727 shrink_block_group->ro = 1;
3731 key.objectid = shrink_start;
3734 cur_byte = key.objectid;
3736 mutex_unlock(&root->fs_info->alloc_mutex);
3738 btrfs_start_delalloc_inodes(root);
3739 btrfs_wait_ordered_extents(tree_root, 0);
3741 mutex_lock(&root->fs_info->alloc_mutex);
3743 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3747 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
3752 leaf = path->nodes[0];
3753 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3754 if (found_key.objectid + found_key.offset > shrink_start &&
3755 found_key.objectid < shrink_last_byte) {
3756 cur_byte = found_key.objectid;
3757 key.objectid = cur_byte;
3760 btrfs_release_path(root, path);
3763 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3768 leaf = path->nodes[0];
3769 nritems = btrfs_header_nritems(leaf);
3770 if (path->slots[0] >= nritems) {
3771 ret = btrfs_next_leaf(root, path);
3778 leaf = path->nodes[0];
3779 nritems = btrfs_header_nritems(leaf);
3782 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3784 if (found_key.objectid >= shrink_last_byte)
3787 if (progress && need_resched()) {
3788 memcpy(&key, &found_key, sizeof(key));
3790 btrfs_release_path(root, path);
3791 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3797 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3798 found_key.objectid + found_key.offset <= cur_byte) {
3799 memcpy(&key, &found_key, sizeof(key));
3806 cur_byte = found_key.objectid + found_key.offset;
3807 key.objectid = cur_byte;
3808 btrfs_release_path(root, path);
3809 ret = relocate_one_extent(root, path, &found_key);
3810 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
3813 btrfs_release_path(root, path);
3815 if (total_found > 0) {
3816 printk("btrfs relocate found %llu last extent was %llu\n",
3817 (unsigned long long)total_found,
3818 (unsigned long long)found_key.objectid);
3819 mutex_unlock(&root->fs_info->alloc_mutex);
3820 trans = btrfs_start_transaction(tree_root, 1);
3821 btrfs_commit_transaction(trans, tree_root);
3823 btrfs_clean_old_snapshots(tree_root);
3825 btrfs_start_delalloc_inodes(root);
3826 btrfs_wait_ordered_extents(tree_root, 0);
3828 trans = btrfs_start_transaction(tree_root, 1);
3829 btrfs_commit_transaction(trans, tree_root);
3830 mutex_lock(&root->fs_info->alloc_mutex);
3835 * we've freed all the extents, now remove the block
3836 * group item from the tree
3838 mutex_unlock(&root->fs_info->alloc_mutex);
3840 trans = btrfs_start_transaction(root, 1);
3842 mutex_lock(&root->fs_info->alloc_mutex);
3843 memcpy(&key, &shrink_block_group->key, sizeof(key));
3845 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3849 btrfs_end_transaction(trans, root);
3853 spin_lock(&root->fs_info->block_group_cache_lock);
3854 rb_erase(&shrink_block_group->cache_node,
3855 &root->fs_info->block_group_cache_tree);
3856 spin_unlock(&root->fs_info->block_group_cache_lock);
3858 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3861 btrfs_end_transaction(trans, root);
3865 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3866 kfree(shrink_block_group);
3869 btrfs_del_item(trans, root, path);
3870 btrfs_release_path(root, path);
3871 mutex_unlock(&root->fs_info->alloc_mutex);
3872 btrfs_commit_transaction(trans, root);
3874 mutex_lock(&root->fs_info->alloc_mutex);
3876 /* the code to unpin extents might set a few bits in the free
3877 * space cache for this range again
3880 ret = btrfs_remove_free_space(shrink_block_group, key.objectid,
3883 btrfs_free_path(path);
3884 mutex_unlock(&root->fs_info->alloc_mutex);
3888 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3889 struct btrfs_key *key)
3892 struct btrfs_key found_key;
3893 struct extent_buffer *leaf;
3896 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3901 slot = path->slots[0];
3902 leaf = path->nodes[0];
3903 if (slot >= btrfs_header_nritems(leaf)) {
3904 ret = btrfs_next_leaf(root, path);
3911 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3913 if (found_key.objectid >= key->objectid &&
3914 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3925 int btrfs_read_block_groups(struct btrfs_root *root)
3927 struct btrfs_path *path;
3929 struct btrfs_block_group_cache *cache;
3930 struct btrfs_fs_info *info = root->fs_info;
3931 struct btrfs_space_info *space_info;
3932 struct btrfs_key key;
3933 struct btrfs_key found_key;
3934 struct extent_buffer *leaf;
3936 root = info->extent_root;
3939 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3940 path = btrfs_alloc_path();
3944 mutex_lock(&root->fs_info->alloc_mutex);
3946 ret = find_first_block_group(root, path, &key);
3954 leaf = path->nodes[0];
3955 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3956 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3962 spin_lock_init(&cache->lock);
3963 INIT_LIST_HEAD(&cache->list);
3964 read_extent_buffer(leaf, &cache->item,
3965 btrfs_item_ptr_offset(leaf, path->slots[0]),
3966 sizeof(cache->item));
3967 memcpy(&cache->key, &found_key, sizeof(found_key));
3969 key.objectid = found_key.objectid + found_key.offset;
3970 btrfs_release_path(root, path);
3971 cache->flags = btrfs_block_group_flags(&cache->item);
3973 ret = update_space_info(info, cache->flags, found_key.offset,
3974 btrfs_block_group_used(&cache->item),
3977 cache->space_info = space_info;
3978 spin_lock(&space_info->lock);
3979 list_add(&cache->list, &space_info->block_groups);
3980 spin_unlock(&space_info->lock);
3982 ret = btrfs_add_block_group_cache(root->fs_info, cache);
3987 btrfs_free_path(path);
3988 mutex_unlock(&root->fs_info->alloc_mutex);
3992 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3993 struct btrfs_root *root, u64 bytes_used,
3994 u64 type, u64 chunk_objectid, u64 chunk_offset,
3998 struct btrfs_root *extent_root;
3999 struct btrfs_block_group_cache *cache;
4001 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
4002 extent_root = root->fs_info->extent_root;
4004 root->fs_info->last_trans_new_blockgroup = trans->transid;
4006 cache = kzalloc(sizeof(*cache), GFP_NOFS);
4010 cache->key.objectid = chunk_offset;
4011 cache->key.offset = size;
4012 spin_lock_init(&cache->lock);
4013 INIT_LIST_HEAD(&cache->list);
4014 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
4016 btrfs_set_block_group_used(&cache->item, bytes_used);
4017 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
4018 cache->flags = type;
4019 btrfs_set_block_group_flags(&cache->item, type);
4021 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
4022 &cache->space_info);
4024 spin_lock(&cache->space_info->lock);
4025 list_add(&cache->list, &cache->space_info->block_groups);
4026 spin_unlock(&cache->space_info->lock);
4028 ret = btrfs_add_block_group_cache(root->fs_info, cache);
4031 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
4032 sizeof(cache->item));
4035 finish_current_insert(trans, extent_root);
4036 ret = del_pending_extents(trans, extent_root);
4038 set_avail_alloc_bits(extent_root->fs_info, type);