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>
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
37 btrfs_root *extent_root);
38 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
39 btrfs_root *extent_root);
40 static struct btrfs_block_group_cache *
41 __btrfs_find_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *hint,
43 u64 search_start, int data, int owner);
45 void maybe_lock_mutex(struct btrfs_root *root)
47 if (root != root->fs_info->extent_root &&
48 root != root->fs_info->chunk_root &&
49 root != root->fs_info->dev_root) {
50 mutex_lock(&root->fs_info->alloc_mutex);
54 void maybe_unlock_mutex(struct btrfs_root *root)
56 if (root != root->fs_info->extent_root &&
57 root != root->fs_info->chunk_root &&
58 root != root->fs_info->dev_root) {
59 mutex_unlock(&root->fs_info->alloc_mutex);
63 static int cache_block_group(struct btrfs_root *root,
64 struct btrfs_block_group_cache *block_group)
66 struct btrfs_path *path;
69 struct extent_buffer *leaf;
70 struct extent_io_tree *free_space_cache;
80 root = root->fs_info->extent_root;
81 free_space_cache = &root->fs_info->free_space_cache;
83 if (block_group->cached)
86 path = btrfs_alloc_path();
92 * we get into deadlocks with paths held by callers of this function.
93 * since the alloc_mutex is protecting things right now, just
94 * skip the locking here
96 path->skip_locking = 1;
97 first_free = block_group->key.objectid;
98 key.objectid = block_group->key.objectid;
100 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
104 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
108 leaf = path->nodes[0];
109 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
110 if (key.objectid + key.offset > first_free)
111 first_free = key.objectid + key.offset;
114 leaf = path->nodes[0];
115 slot = path->slots[0];
116 if (slot >= btrfs_header_nritems(leaf)) {
117 ret = btrfs_next_leaf(root, path);
126 btrfs_item_key_to_cpu(leaf, &key, slot);
127 if (key.objectid < block_group->key.objectid) {
130 if (key.objectid >= block_group->key.objectid +
131 block_group->key.offset) {
135 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
140 if (key.objectid > last) {
141 hole_size = key.objectid - last;
142 set_extent_dirty(free_space_cache, last,
143 last + hole_size - 1,
146 last = key.objectid + key.offset;
154 if (block_group->key.objectid +
155 block_group->key.offset > last) {
156 hole_size = block_group->key.objectid +
157 block_group->key.offset - last;
158 set_extent_dirty(free_space_cache, last,
159 last + hole_size - 1, GFP_NOFS);
161 block_group->cached = 1;
163 btrfs_free_path(path);
167 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
171 struct extent_io_tree *block_group_cache;
172 struct btrfs_block_group_cache *block_group = NULL;
178 bytenr = max_t(u64, bytenr,
179 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
180 block_group_cache = &info->block_group_cache;
181 ret = find_first_extent_bit(block_group_cache,
182 bytenr, &start, &end,
183 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
188 ret = get_state_private(block_group_cache, start, &ptr);
192 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
196 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
200 struct extent_io_tree *block_group_cache;
201 struct btrfs_block_group_cache *block_group = NULL;
207 bytenr = max_t(u64, bytenr,
208 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
209 block_group_cache = &info->block_group_cache;
210 ret = find_first_extent_bit(block_group_cache,
211 bytenr, &start, &end,
212 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
217 ret = get_state_private(block_group_cache, start, &ptr);
221 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
222 if (block_group->key.objectid <= bytenr && bytenr <
223 block_group->key.objectid + block_group->key.offset)
228 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
230 return (cache->flags & bits) == bits;
233 static int noinline find_search_start(struct btrfs_root *root,
234 struct btrfs_block_group_cache **cache_ret,
235 u64 *start_ret, u64 num, int data)
238 struct btrfs_block_group_cache *cache = *cache_ret;
239 struct extent_io_tree *free_space_cache;
240 struct extent_state *state;
245 u64 search_start = *start_ret;
248 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
249 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
250 free_space_cache = &root->fs_info->free_space_cache;
256 ret = cache_block_group(root, cache);
261 last = max(search_start, cache->key.objectid);
262 if (!block_group_bits(cache, data) || cache->ro)
265 spin_lock_irq(&free_space_cache->lock);
266 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
271 spin_unlock_irq(&free_space_cache->lock);
275 start = max(last, state->start);
276 last = state->end + 1;
277 if (last - start < num) {
279 state = extent_state_next(state);
280 } while(state && !(state->state & EXTENT_DIRTY));
283 spin_unlock_irq(&free_space_cache->lock);
287 if (start + num > cache->key.objectid + cache->key.offset)
289 if (!block_group_bits(cache, data)) {
290 printk("block group bits don't match %Lu %d\n", cache->flags, data);
296 cache = btrfs_lookup_block_group(root->fs_info, search_start);
298 printk("Unable to find block group for %Lu\n", search_start);
304 last = cache->key.objectid + cache->key.offset;
306 cache = btrfs_lookup_first_block_group(root->fs_info, last);
307 if (!cache || cache->key.objectid >= total_fs_bytes) {
316 if (cache_miss && !cache->cached) {
317 cache_block_group(root, cache);
319 cache = btrfs_lookup_first_block_group(root->fs_info, last);
322 cache = btrfs_find_block_group(root, cache, last, data, 0);
329 static u64 div_factor(u64 num, int factor)
338 static int block_group_state_bits(u64 flags)
341 if (flags & BTRFS_BLOCK_GROUP_DATA)
342 bits |= BLOCK_GROUP_DATA;
343 if (flags & BTRFS_BLOCK_GROUP_METADATA)
344 bits |= BLOCK_GROUP_METADATA;
345 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
346 bits |= BLOCK_GROUP_SYSTEM;
350 static struct btrfs_block_group_cache *
351 __btrfs_find_block_group(struct btrfs_root *root,
352 struct btrfs_block_group_cache *hint,
353 u64 search_start, int data, int owner)
355 struct btrfs_block_group_cache *cache;
356 struct extent_io_tree *block_group_cache;
357 struct btrfs_block_group_cache *found_group = NULL;
358 struct btrfs_fs_info *info = root->fs_info;
371 block_group_cache = &info->block_group_cache;
373 if (data & BTRFS_BLOCK_GROUP_METADATA)
376 bit = block_group_state_bits(data);
379 struct btrfs_block_group_cache *shint;
380 shint = btrfs_lookup_first_block_group(info, search_start);
381 if (shint && block_group_bits(shint, data) && !shint->ro) {
382 spin_lock(&shint->lock);
383 used = btrfs_block_group_used(&shint->item);
384 if (used + shint->pinned <
385 div_factor(shint->key.offset, factor)) {
386 spin_unlock(&shint->lock);
389 spin_unlock(&shint->lock);
392 if (hint && !hint->ro && block_group_bits(hint, data)) {
393 spin_lock(&hint->lock);
394 used = btrfs_block_group_used(&hint->item);
395 if (used + hint->pinned <
396 div_factor(hint->key.offset, factor)) {
397 spin_unlock(&hint->lock);
400 spin_unlock(&hint->lock);
401 last = hint->key.objectid + hint->key.offset;
404 last = max(hint->key.objectid, search_start);
410 ret = find_first_extent_bit(block_group_cache, last,
415 ret = get_state_private(block_group_cache, start, &ptr);
421 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
422 spin_lock(&cache->lock);
423 last = cache->key.objectid + cache->key.offset;
424 used = btrfs_block_group_used(&cache->item);
426 if (!cache->ro && block_group_bits(cache, data)) {
427 free_check = div_factor(cache->key.offset, factor);
428 if (used + cache->pinned < free_check) {
430 spin_unlock(&cache->lock);
434 spin_unlock(&cache->lock);
442 if (!full_search && factor < 10) {
452 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
453 struct btrfs_block_group_cache
454 *hint, u64 search_start,
458 struct btrfs_block_group_cache *ret;
459 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
462 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
463 u64 owner, u64 owner_offset)
465 u32 high_crc = ~(u32)0;
466 u32 low_crc = ~(u32)0;
468 lenum = cpu_to_le64(root_objectid);
469 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
470 lenum = cpu_to_le64(ref_generation);
471 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
472 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
473 lenum = cpu_to_le64(owner);
474 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
475 lenum = cpu_to_le64(owner_offset);
476 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
478 return ((u64)high_crc << 32) | (u64)low_crc;
481 static int match_extent_ref(struct extent_buffer *leaf,
482 struct btrfs_extent_ref *disk_ref,
483 struct btrfs_extent_ref *cpu_ref)
488 if (cpu_ref->objectid)
489 len = sizeof(*cpu_ref);
491 len = 2 * sizeof(u64);
492 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
497 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root,
499 struct btrfs_path *path, u64 bytenr,
501 u64 ref_generation, u64 owner,
502 u64 owner_offset, int del)
505 struct btrfs_key key;
506 struct btrfs_key found_key;
507 struct btrfs_extent_ref ref;
508 struct extent_buffer *leaf;
509 struct btrfs_extent_ref *disk_ref;
513 btrfs_set_stack_ref_root(&ref, root_objectid);
514 btrfs_set_stack_ref_generation(&ref, ref_generation);
515 btrfs_set_stack_ref_objectid(&ref, owner);
516 btrfs_set_stack_ref_offset(&ref, owner_offset);
518 hash = hash_extent_ref(root_objectid, ref_generation, owner,
521 key.objectid = bytenr;
522 key.type = BTRFS_EXTENT_REF_KEY;
525 ret = btrfs_search_slot(trans, root, &key, path,
529 leaf = path->nodes[0];
531 u32 nritems = btrfs_header_nritems(leaf);
532 if (path->slots[0] >= nritems) {
533 ret2 = btrfs_next_leaf(root, path);
536 leaf = path->nodes[0];
538 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
539 if (found_key.objectid != bytenr ||
540 found_key.type != BTRFS_EXTENT_REF_KEY)
542 key.offset = found_key.offset;
544 btrfs_release_path(root, path);
548 disk_ref = btrfs_item_ptr(path->nodes[0],
550 struct btrfs_extent_ref);
551 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
555 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
556 key.offset = found_key.offset + 1;
557 btrfs_release_path(root, path);
564 * Back reference rules. Back refs have three main goals:
566 * 1) differentiate between all holders of references to an extent so that
567 * when a reference is dropped we can make sure it was a valid reference
568 * before freeing the extent.
570 * 2) Provide enough information to quickly find the holders of an extent
571 * if we notice a given block is corrupted or bad.
573 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
574 * maintenance. This is actually the same as #2, but with a slightly
575 * different use case.
577 * File extents can be referenced by:
579 * - multiple snapshots, subvolumes, or different generations in one subvol
580 * - different files inside a single subvolume (in theory, not implemented yet)
581 * - different offsets inside a file (bookend extents in file.c)
583 * The extent ref structure has fields for:
585 * - Objectid of the subvolume root
586 * - Generation number of the tree holding the reference
587 * - objectid of the file holding the reference
588 * - offset in the file corresponding to the key holding the reference
590 * When a file extent is allocated the fields are filled in:
591 * (root_key.objectid, trans->transid, inode objectid, offset in file)
593 * When a leaf is cow'd new references are added for every file extent found
594 * in the leaf. It looks the same as the create case, but trans->transid
595 * will be different when the block is cow'd.
597 * (root_key.objectid, trans->transid, inode objectid, offset in file)
599 * When a file extent is removed either during snapshot deletion or file
600 * truncation, the corresponding back reference is found
603 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
604 * inode objectid, offset in file)
606 * Btree extents can be referenced by:
608 * - Different subvolumes
609 * - Different generations of the same subvolume
611 * Storing sufficient information for a full reverse mapping of a btree
612 * block would require storing the lowest key of the block in the backref,
613 * and it would require updating that lowest key either before write out or
614 * every time it changed. Instead, the objectid of the lowest key is stored
615 * along with the level of the tree block. This provides a hint
616 * about where in the btree the block can be found. Searches through the
617 * btree only need to look for a pointer to that block, so they stop one
618 * level higher than the level recorded in the backref.
620 * Some btrees do not do reference counting on their extents. These
621 * include the extent tree and the tree of tree roots. Backrefs for these
622 * trees always have a generation of zero.
624 * When a tree block is created, back references are inserted:
626 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
628 * When a tree block is cow'd in a reference counted root,
629 * new back references are added for all the blocks it points to.
630 * These are of the form (trans->transid will have increased since creation):
632 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
634 * Because the lowest_key_objectid and the level are just hints
635 * they are not used when backrefs are deleted. When a backref is deleted:
637 * if backref was for a tree root:
638 * root_objectid = root->root_key.objectid
640 * root_objectid = btrfs_header_owner(parent)
642 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
644 * Back Reference Key hashing:
646 * Back references have four fields, each 64 bits long. Unfortunately,
647 * This is hashed into a single 64 bit number and placed into the key offset.
648 * The key objectid corresponds to the first byte in the extent, and the
649 * key type is set to BTRFS_EXTENT_REF_KEY
651 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
652 struct btrfs_root *root,
653 struct btrfs_path *path, u64 bytenr,
654 u64 root_objectid, u64 ref_generation,
655 u64 owner, u64 owner_offset)
658 struct btrfs_key key;
659 struct btrfs_extent_ref ref;
660 struct btrfs_extent_ref *disk_ref;
663 btrfs_set_stack_ref_root(&ref, root_objectid);
664 btrfs_set_stack_ref_generation(&ref, ref_generation);
665 btrfs_set_stack_ref_objectid(&ref, owner);
666 btrfs_set_stack_ref_offset(&ref, owner_offset);
668 hash = hash_extent_ref(root_objectid, ref_generation, owner,
671 key.objectid = bytenr;
672 key.type = BTRFS_EXTENT_REF_KEY;
674 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
675 while (ret == -EEXIST) {
676 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
677 struct btrfs_extent_ref);
678 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
681 btrfs_release_path(root, path);
682 ret = btrfs_insert_empty_item(trans, root, path, &key,
687 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
688 struct btrfs_extent_ref);
689 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
691 btrfs_mark_buffer_dirty(path->nodes[0]);
693 btrfs_release_path(root, path);
697 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
698 struct btrfs_root *root,
699 u64 bytenr, u64 num_bytes,
700 u64 root_objectid, u64 ref_generation,
701 u64 owner, u64 owner_offset)
703 struct btrfs_path *path;
705 struct btrfs_key key;
706 struct extent_buffer *l;
707 struct btrfs_extent_item *item;
710 WARN_ON(num_bytes < root->sectorsize);
711 path = btrfs_alloc_path();
716 key.objectid = bytenr;
717 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
718 key.offset = num_bytes;
719 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
728 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
729 refs = btrfs_extent_refs(l, item);
730 btrfs_set_extent_refs(l, item, refs + 1);
731 btrfs_mark_buffer_dirty(path->nodes[0]);
733 btrfs_release_path(root->fs_info->extent_root, path);
736 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
737 path, bytenr, root_objectid,
738 ref_generation, owner, owner_offset);
740 finish_current_insert(trans, root->fs_info->extent_root);
741 del_pending_extents(trans, root->fs_info->extent_root);
743 btrfs_free_path(path);
747 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
749 u64 bytenr, u64 num_bytes,
750 u64 root_objectid, u64 ref_generation,
751 u64 owner, u64 owner_offset)
755 mutex_lock(&root->fs_info->alloc_mutex);
756 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
757 root_objectid, ref_generation,
758 owner, owner_offset);
759 mutex_unlock(&root->fs_info->alloc_mutex);
763 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
764 struct btrfs_root *root)
766 finish_current_insert(trans, root->fs_info->extent_root);
767 del_pending_extents(trans, root->fs_info->extent_root);
771 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
772 struct btrfs_root *root, u64 bytenr,
773 u64 num_bytes, u32 *refs)
775 struct btrfs_path *path;
777 struct btrfs_key key;
778 struct extent_buffer *l;
779 struct btrfs_extent_item *item;
781 WARN_ON(num_bytes < root->sectorsize);
782 path = btrfs_alloc_path();
784 key.objectid = bytenr;
785 key.offset = num_bytes;
786 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
787 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
792 btrfs_print_leaf(root, path->nodes[0]);
793 printk("failed to find block number %Lu\n", bytenr);
797 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
798 *refs = btrfs_extent_refs(l, item);
800 btrfs_free_path(path);
804 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
805 struct btrfs_path *count_path,
809 struct btrfs_root *extent_root = root->fs_info->extent_root;
810 struct btrfs_path *path;
814 u64 root_objectid = root->root_key.objectid;
820 struct btrfs_key key;
821 struct btrfs_key found_key;
822 struct extent_buffer *l;
823 struct btrfs_extent_item *item;
824 struct btrfs_extent_ref *ref_item;
827 /* FIXME, needs locking */
830 mutex_lock(&root->fs_info->alloc_mutex);
831 path = btrfs_alloc_path();
834 bytenr = first_extent;
836 bytenr = count_path->nodes[level]->start;
839 key.objectid = bytenr;
842 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
843 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
849 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
851 if (found_key.objectid != bytenr ||
852 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
856 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
857 extent_refs = btrfs_extent_refs(l, item);
860 nritems = btrfs_header_nritems(l);
861 if (path->slots[0] >= nritems) {
862 ret = btrfs_next_leaf(extent_root, path);
867 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
868 if (found_key.objectid != bytenr)
871 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
877 ref_item = btrfs_item_ptr(l, path->slots[0],
878 struct btrfs_extent_ref);
879 found_objectid = btrfs_ref_root(l, ref_item);
881 if (found_objectid != root_objectid) {
886 found_owner = btrfs_ref_objectid(l, ref_item);
887 if (found_owner != expected_owner) {
892 * nasty. we don't count a reference held by
893 * the running transaction. This allows nodatacow
894 * to avoid cow most of the time
896 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
897 btrfs_ref_generation(l, ref_item) ==
898 root->fs_info->generation) {
906 * if there is more than one reference against a data extent,
907 * we have to assume the other ref is another snapshot
909 if (level == -1 && extent_refs > 1) {
913 if (cur_count == 0) {
917 if (level >= 0 && root->node == count_path->nodes[level])
920 btrfs_release_path(root, path);
924 btrfs_free_path(path);
925 mutex_unlock(&root->fs_info->alloc_mutex);
929 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
930 struct extent_buffer *buf)
934 struct btrfs_key key;
935 struct btrfs_file_extent_item *fi;
944 level = btrfs_header_level(buf);
945 nritems = btrfs_header_nritems(buf);
946 for (i = 0; i < nritems; i++) {
950 btrfs_item_key_to_cpu(buf, &key, i);
951 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
953 fi = btrfs_item_ptr(buf, i,
954 struct btrfs_file_extent_item);
955 if (btrfs_file_extent_type(buf, fi) ==
956 BTRFS_FILE_EXTENT_INLINE)
958 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
959 if (disk_bytenr == 0)
962 mutex_lock(&root->fs_info->alloc_mutex);
963 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
964 btrfs_file_extent_disk_num_bytes(buf, fi),
965 root->root_key.objectid, trans->transid,
966 key.objectid, key.offset);
967 mutex_unlock(&root->fs_info->alloc_mutex);
974 bytenr = btrfs_node_blockptr(buf, i);
975 btrfs_node_key_to_cpu(buf, &key, i);
977 mutex_lock(&root->fs_info->alloc_mutex);
978 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
979 btrfs_level_size(root, level - 1),
980 root->root_key.objectid,
982 level - 1, key.objectid);
983 mutex_unlock(&root->fs_info->alloc_mutex);
995 for (i =0; i < faili; i++) {
998 btrfs_item_key_to_cpu(buf, &key, i);
999 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1001 fi = btrfs_item_ptr(buf, i,
1002 struct btrfs_file_extent_item);
1003 if (btrfs_file_extent_type(buf, fi) ==
1004 BTRFS_FILE_EXTENT_INLINE)
1006 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1007 if (disk_bytenr == 0)
1009 err = btrfs_free_extent(trans, root, disk_bytenr,
1010 btrfs_file_extent_disk_num_bytes(buf,
1014 bytenr = btrfs_node_blockptr(buf, i);
1015 err = btrfs_free_extent(trans, root, bytenr,
1016 btrfs_level_size(root, level - 1), 0);
1024 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1025 struct btrfs_root *root,
1026 struct btrfs_path *path,
1027 struct btrfs_block_group_cache *cache)
1031 struct btrfs_root *extent_root = root->fs_info->extent_root;
1033 struct extent_buffer *leaf;
1035 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1040 leaf = path->nodes[0];
1041 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1042 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1043 btrfs_mark_buffer_dirty(leaf);
1044 btrfs_release_path(extent_root, path);
1046 finish_current_insert(trans, extent_root);
1047 pending_ret = del_pending_extents(trans, extent_root);
1056 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1057 struct btrfs_root *root)
1059 struct extent_io_tree *block_group_cache;
1060 struct btrfs_block_group_cache *cache;
1064 struct btrfs_path *path;
1070 block_group_cache = &root->fs_info->block_group_cache;
1071 path = btrfs_alloc_path();
1075 mutex_lock(&root->fs_info->alloc_mutex);
1077 ret = find_first_extent_bit(block_group_cache, last,
1078 &start, &end, BLOCK_GROUP_DIRTY);
1083 ret = get_state_private(block_group_cache, start, &ptr);
1086 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1087 err = write_one_cache_group(trans, root,
1090 * if we fail to write the cache group, we want
1091 * to keep it marked dirty in hopes that a later
1098 clear_extent_bits(block_group_cache, start, end,
1099 BLOCK_GROUP_DIRTY, GFP_NOFS);
1101 btrfs_free_path(path);
1102 mutex_unlock(&root->fs_info->alloc_mutex);
1106 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1109 struct list_head *head = &info->space_info;
1110 struct list_head *cur;
1111 struct btrfs_space_info *found;
1112 list_for_each(cur, head) {
1113 found = list_entry(cur, struct btrfs_space_info, list);
1114 if (found->flags == flags)
1121 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1122 u64 total_bytes, u64 bytes_used,
1123 struct btrfs_space_info **space_info)
1125 struct btrfs_space_info *found;
1127 found = __find_space_info(info, flags);
1129 found->total_bytes += total_bytes;
1130 found->bytes_used += bytes_used;
1132 WARN_ON(found->total_bytes < found->bytes_used);
1133 *space_info = found;
1136 found = kmalloc(sizeof(*found), GFP_NOFS);
1140 list_add(&found->list, &info->space_info);
1141 found->flags = flags;
1142 found->total_bytes = total_bytes;
1143 found->bytes_used = bytes_used;
1144 found->bytes_pinned = 0;
1146 found->force_alloc = 0;
1147 *space_info = found;
1151 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1153 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1154 BTRFS_BLOCK_GROUP_RAID1 |
1155 BTRFS_BLOCK_GROUP_RAID10 |
1156 BTRFS_BLOCK_GROUP_DUP);
1158 if (flags & BTRFS_BLOCK_GROUP_DATA)
1159 fs_info->avail_data_alloc_bits |= extra_flags;
1160 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1161 fs_info->avail_metadata_alloc_bits |= extra_flags;
1162 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1163 fs_info->avail_system_alloc_bits |= extra_flags;
1167 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1169 u64 num_devices = root->fs_info->fs_devices->num_devices;
1171 if (num_devices == 1)
1172 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1173 if (num_devices < 4)
1174 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1176 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1177 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1178 BTRFS_BLOCK_GROUP_RAID10))) {
1179 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1182 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1183 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1184 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1187 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1188 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1189 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1190 (flags & BTRFS_BLOCK_GROUP_DUP)))
1191 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1195 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *extent_root, u64 alloc_bytes,
1197 u64 flags, int force)
1199 struct btrfs_space_info *space_info;
1205 flags = reduce_alloc_profile(extent_root, flags);
1207 space_info = __find_space_info(extent_root->fs_info, flags);
1209 ret = update_space_info(extent_root->fs_info, flags,
1213 BUG_ON(!space_info);
1215 if (space_info->force_alloc) {
1217 space_info->force_alloc = 0;
1219 if (space_info->full)
1222 thresh = div_factor(space_info->total_bytes, 6);
1224 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1228 mutex_lock(&extent_root->fs_info->chunk_mutex);
1229 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1230 if (ret == -ENOSPC) {
1231 printk("space info full %Lu\n", flags);
1232 space_info->full = 1;
1237 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1238 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1241 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1246 static int update_block_group(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root,
1248 u64 bytenr, u64 num_bytes, int alloc,
1251 struct btrfs_block_group_cache *cache;
1252 struct btrfs_fs_info *info = root->fs_info;
1253 u64 total = num_bytes;
1259 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1261 cache = btrfs_lookup_block_group(info, bytenr);
1265 byte_in_group = bytenr - cache->key.objectid;
1266 WARN_ON(byte_in_group > cache->key.offset);
1267 start = cache->key.objectid;
1268 end = start + cache->key.offset - 1;
1269 set_extent_bits(&info->block_group_cache, start, end,
1270 BLOCK_GROUP_DIRTY, GFP_NOFS);
1272 spin_lock(&cache->lock);
1273 old_val = btrfs_block_group_used(&cache->item);
1274 num_bytes = min(total, cache->key.offset - byte_in_group);
1276 old_val += num_bytes;
1277 cache->space_info->bytes_used += num_bytes;
1278 btrfs_set_block_group_used(&cache->item, old_val);
1279 spin_unlock(&cache->lock);
1281 old_val -= num_bytes;
1282 cache->space_info->bytes_used -= num_bytes;
1283 btrfs_set_block_group_used(&cache->item, old_val);
1284 spin_unlock(&cache->lock);
1286 set_extent_dirty(&info->free_space_cache,
1287 bytenr, bytenr + num_bytes - 1,
1292 bytenr += num_bytes;
1297 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1302 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1303 search_start, &start, &end,
1304 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1305 BLOCK_GROUP_SYSTEM);
1312 static int update_pinned_extents(struct btrfs_root *root,
1313 u64 bytenr, u64 num, int pin)
1316 struct btrfs_block_group_cache *cache;
1317 struct btrfs_fs_info *fs_info = root->fs_info;
1319 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1321 set_extent_dirty(&fs_info->pinned_extents,
1322 bytenr, bytenr + num - 1, GFP_NOFS);
1324 clear_extent_dirty(&fs_info->pinned_extents,
1325 bytenr, bytenr + num - 1, GFP_NOFS);
1328 cache = btrfs_lookup_block_group(fs_info, bytenr);
1330 u64 first = first_logical_byte(root, bytenr);
1331 WARN_ON(first < bytenr);
1332 len = min(first - bytenr, num);
1334 len = min(num, cache->key.offset -
1335 (bytenr - cache->key.objectid));
1339 spin_lock(&cache->lock);
1340 cache->pinned += len;
1341 cache->space_info->bytes_pinned += len;
1342 spin_unlock(&cache->lock);
1344 fs_info->total_pinned += len;
1347 spin_lock(&cache->lock);
1348 cache->pinned -= len;
1349 cache->space_info->bytes_pinned -= len;
1350 spin_unlock(&cache->lock);
1352 fs_info->total_pinned -= len;
1360 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1365 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1369 ret = find_first_extent_bit(pinned_extents, last,
1370 &start, &end, EXTENT_DIRTY);
1373 set_extent_dirty(copy, start, end, GFP_NOFS);
1379 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1380 struct btrfs_root *root,
1381 struct extent_io_tree *unpin)
1386 struct extent_io_tree *free_space_cache;
1387 free_space_cache = &root->fs_info->free_space_cache;
1389 mutex_lock(&root->fs_info->alloc_mutex);
1391 ret = find_first_extent_bit(unpin, 0, &start, &end,
1395 update_pinned_extents(root, start, end + 1 - start, 0);
1396 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1397 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1398 if (need_resched()) {
1399 mutex_unlock(&root->fs_info->alloc_mutex);
1401 mutex_lock(&root->fs_info->alloc_mutex);
1404 mutex_unlock(&root->fs_info->alloc_mutex);
1408 static int finish_current_insert(struct btrfs_trans_handle *trans,
1409 struct btrfs_root *extent_root)
1413 struct btrfs_fs_info *info = extent_root->fs_info;
1414 struct extent_buffer *eb;
1415 struct btrfs_path *path;
1416 struct btrfs_key ins;
1417 struct btrfs_disk_key first;
1418 struct btrfs_extent_item extent_item;
1423 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1424 btrfs_set_stack_extent_refs(&extent_item, 1);
1425 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1426 path = btrfs_alloc_path();
1429 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1430 &end, EXTENT_LOCKED);
1434 ins.objectid = start;
1435 ins.offset = end + 1 - start;
1436 err = btrfs_insert_item(trans, extent_root, &ins,
1437 &extent_item, sizeof(extent_item));
1438 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1441 eb = btrfs_find_tree_block(extent_root, ins.objectid,
1444 if (!btrfs_buffer_uptodate(eb, trans->transid)) {
1445 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1446 btrfs_read_buffer(eb, trans->transid);
1447 mutex_lock(&extent_root->fs_info->alloc_mutex);
1450 btrfs_tree_lock(eb);
1451 level = btrfs_header_level(eb);
1453 btrfs_item_key(eb, &first, 0);
1455 btrfs_node_key(eb, &first, 0);
1457 btrfs_tree_unlock(eb);
1458 free_extent_buffer(eb);
1460 * the first key is just a hint, so the race we've created
1461 * against reading it is fine
1463 err = btrfs_insert_extent_backref(trans, extent_root, path,
1464 start, extent_root->root_key.objectid,
1466 btrfs_disk_key_objectid(&first));
1468 if (need_resched()) {
1469 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1471 mutex_lock(&extent_root->fs_info->alloc_mutex);
1474 btrfs_free_path(path);
1478 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1483 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1485 struct extent_buffer *buf;
1486 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1488 if (btrfs_try_tree_lock(buf) &&
1489 btrfs_buffer_uptodate(buf, 0)) {
1491 root->fs_info->running_transaction->transid;
1492 u64 header_transid =
1493 btrfs_header_generation(buf);
1494 if (header_transid == transid &&
1495 !btrfs_header_flag(buf,
1496 BTRFS_HEADER_FLAG_WRITTEN)) {
1497 clean_tree_block(NULL, root, buf);
1498 btrfs_tree_unlock(buf);
1499 free_extent_buffer(buf);
1502 btrfs_tree_unlock(buf);
1504 free_extent_buffer(buf);
1506 update_pinned_extents(root, bytenr, num_bytes, 1);
1508 set_extent_bits(&root->fs_info->pending_del,
1509 bytenr, bytenr + num_bytes - 1,
1510 EXTENT_LOCKED, GFP_NOFS);
1517 * remove an extent from the root, returns 0 on success
1519 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1520 *root, u64 bytenr, u64 num_bytes,
1521 u64 root_objectid, u64 ref_generation,
1522 u64 owner_objectid, u64 owner_offset, int pin,
1525 struct btrfs_path *path;
1526 struct btrfs_key key;
1527 struct btrfs_fs_info *info = root->fs_info;
1528 struct btrfs_root *extent_root = info->extent_root;
1529 struct extent_buffer *leaf;
1531 int extent_slot = 0;
1532 int found_extent = 0;
1534 struct btrfs_extent_item *ei;
1537 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
1538 key.objectid = bytenr;
1539 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1540 key.offset = num_bytes;
1541 path = btrfs_alloc_path();
1546 ret = lookup_extent_backref(trans, extent_root, path,
1547 bytenr, root_objectid,
1549 owner_objectid, owner_offset, 1);
1551 struct btrfs_key found_key;
1552 extent_slot = path->slots[0];
1553 while(extent_slot > 0) {
1555 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1557 if (found_key.objectid != bytenr)
1559 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1560 found_key.offset == num_bytes) {
1564 if (path->slots[0] - extent_slot > 5)
1568 ret = btrfs_del_item(trans, extent_root, path);
1570 btrfs_print_leaf(extent_root, path->nodes[0]);
1572 printk("Unable to find ref byte nr %Lu root %Lu "
1573 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1574 root_objectid, ref_generation, owner_objectid,
1577 if (!found_extent) {
1578 btrfs_release_path(extent_root, path);
1579 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1583 extent_slot = path->slots[0];
1586 leaf = path->nodes[0];
1587 ei = btrfs_item_ptr(leaf, extent_slot,
1588 struct btrfs_extent_item);
1589 refs = btrfs_extent_refs(leaf, ei);
1592 btrfs_set_extent_refs(leaf, ei, refs);
1594 btrfs_mark_buffer_dirty(leaf);
1596 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1597 /* if the back ref and the extent are next to each other
1598 * they get deleted below in one shot
1600 path->slots[0] = extent_slot;
1602 } else if (found_extent) {
1603 /* otherwise delete the extent back ref */
1604 ret = btrfs_del_item(trans, extent_root, path);
1606 /* if refs are 0, we need to setup the path for deletion */
1608 btrfs_release_path(extent_root, path);
1609 ret = btrfs_search_slot(trans, extent_root, &key, path,
1622 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1628 /* block accounting for super block */
1629 spin_lock_irq(&info->delalloc_lock);
1630 super_used = btrfs_super_bytes_used(&info->super_copy);
1631 btrfs_set_super_bytes_used(&info->super_copy,
1632 super_used - num_bytes);
1633 spin_unlock_irq(&info->delalloc_lock);
1635 /* block accounting for root item */
1636 root_used = btrfs_root_used(&root->root_item);
1637 btrfs_set_root_used(&root->root_item,
1638 root_used - num_bytes);
1639 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1644 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1648 btrfs_free_path(path);
1649 finish_current_insert(trans, extent_root);
1654 * find all the blocks marked as pending in the radix tree and remove
1655 * them from the extent map
1657 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1658 btrfs_root *extent_root)
1664 struct extent_io_tree *pending_del;
1665 struct extent_io_tree *pinned_extents;
1667 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
1668 pending_del = &extent_root->fs_info->pending_del;
1669 pinned_extents = &extent_root->fs_info->pinned_extents;
1672 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1676 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1678 if (!test_range_bit(&extent_root->fs_info->extent_ins,
1679 start, end, EXTENT_LOCKED, 0)) {
1680 update_pinned_extents(extent_root, start,
1681 end + 1 - start, 1);
1682 ret = __free_extent(trans, extent_root,
1683 start, end + 1 - start,
1684 extent_root->root_key.objectid,
1687 clear_extent_bits(&extent_root->fs_info->extent_ins,
1688 start, end, EXTENT_LOCKED, GFP_NOFS);
1693 if (need_resched()) {
1694 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1696 mutex_lock(&extent_root->fs_info->alloc_mutex);
1703 * remove an extent from the root, returns 0 on success
1705 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1706 struct btrfs_root *root, u64 bytenr,
1707 u64 num_bytes, u64 root_objectid,
1708 u64 ref_generation, u64 owner_objectid,
1709 u64 owner_offset, int pin)
1711 struct btrfs_root *extent_root = root->fs_info->extent_root;
1715 WARN_ON(num_bytes < root->sectorsize);
1716 if (!root->ref_cows)
1719 if (root == extent_root) {
1720 pin_down_bytes(root, bytenr, num_bytes, 1);
1723 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1724 ref_generation, owner_objectid, owner_offset,
1727 finish_current_insert(trans, root->fs_info->extent_root);
1728 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1729 return ret ? ret : pending_ret;
1732 int btrfs_free_extent(struct btrfs_trans_handle *trans,
1733 struct btrfs_root *root, u64 bytenr,
1734 u64 num_bytes, u64 root_objectid,
1735 u64 ref_generation, u64 owner_objectid,
1736 u64 owner_offset, int pin)
1740 maybe_lock_mutex(root);
1741 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1742 root_objectid, ref_generation,
1743 owner_objectid, owner_offset, pin);
1744 maybe_unlock_mutex(root);
1748 static u64 stripe_align(struct btrfs_root *root, u64 val)
1750 u64 mask = ((u64)root->stripesize - 1);
1751 u64 ret = (val + mask) & ~mask;
1756 * walks the btree of allocated extents and find a hole of a given size.
1757 * The key ins is changed to record the hole:
1758 * ins->objectid == block start
1759 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1760 * ins->offset == number of blocks
1761 * Any available blocks before search_start are skipped.
1763 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1764 struct btrfs_root *orig_root,
1765 u64 num_bytes, u64 empty_size,
1766 u64 search_start, u64 search_end,
1767 u64 hint_byte, struct btrfs_key *ins,
1768 u64 exclude_start, u64 exclude_nr,
1772 u64 orig_search_start;
1773 struct btrfs_root * root = orig_root->fs_info->extent_root;
1774 struct btrfs_fs_info *info = root->fs_info;
1775 u64 total_needed = num_bytes;
1776 u64 *last_ptr = NULL;
1777 struct btrfs_block_group_cache *block_group;
1780 int chunk_alloc_done = 0;
1781 int empty_cluster = 2 * 1024 * 1024;
1782 int allowed_chunk_alloc = 0;
1784 WARN_ON(num_bytes < root->sectorsize);
1785 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1787 if (orig_root->ref_cows || empty_size)
1788 allowed_chunk_alloc = 1;
1790 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1791 last_ptr = &root->fs_info->last_alloc;
1792 empty_cluster = 256 * 1024;
1795 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1796 last_ptr = &root->fs_info->last_data_alloc;
1801 hint_byte = *last_ptr;
1803 empty_size += empty_cluster;
1807 search_start = max(search_start, first_logical_byte(root, 0));
1808 orig_search_start = search_start;
1810 if (search_end == (u64)-1)
1811 search_end = btrfs_super_total_bytes(&info->super_copy);
1814 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1816 hint_byte = search_start;
1817 block_group = btrfs_find_block_group(root, block_group,
1818 hint_byte, data, 1);
1819 if (last_ptr && *last_ptr == 0 && block_group)
1820 hint_byte = block_group->key.objectid;
1822 block_group = btrfs_find_block_group(root,
1824 search_start, data, 1);
1826 search_start = max(search_start, hint_byte);
1828 total_needed += empty_size;
1832 block_group = btrfs_lookup_first_block_group(info,
1835 block_group = btrfs_lookup_first_block_group(info,
1838 if (full_scan && !chunk_alloc_done) {
1839 if (allowed_chunk_alloc) {
1840 do_chunk_alloc(trans, root,
1841 num_bytes + 2 * 1024 * 1024, data, 1);
1842 allowed_chunk_alloc = 0;
1843 } else if (block_group && block_group_bits(block_group, data)) {
1844 block_group->space_info->force_alloc = 1;
1846 chunk_alloc_done = 1;
1848 ret = find_search_start(root, &block_group, &search_start,
1849 total_needed, data);
1850 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1852 block_group = btrfs_lookup_first_block_group(info,
1854 search_start = orig_search_start;
1855 ret = find_search_start(root, &block_group, &search_start,
1856 total_needed, data);
1863 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1866 empty_size += empty_cluster;
1867 total_needed += empty_size;
1869 block_group = btrfs_lookup_first_block_group(info,
1871 search_start = orig_search_start;
1872 ret = find_search_start(root, &block_group,
1873 &search_start, total_needed, data);
1880 search_start = stripe_align(root, search_start);
1881 ins->objectid = search_start;
1882 ins->offset = num_bytes;
1884 if (ins->objectid + num_bytes >= search_end)
1887 if (ins->objectid + num_bytes >
1888 block_group->key.objectid + block_group->key.offset) {
1889 search_start = block_group->key.objectid +
1890 block_group->key.offset;
1894 if (test_range_bit(&info->extent_ins, ins->objectid,
1895 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1896 search_start = ins->objectid + num_bytes;
1900 if (test_range_bit(&info->pinned_extents, ins->objectid,
1901 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1902 search_start = ins->objectid + num_bytes;
1906 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1907 ins->objectid < exclude_start + exclude_nr)) {
1908 search_start = exclude_start + exclude_nr;
1912 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1913 block_group = btrfs_lookup_block_group(info, ins->objectid);
1915 trans->block_group = block_group;
1917 ins->offset = num_bytes;
1919 *last_ptr = ins->objectid + ins->offset;
1921 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1928 if (search_start + num_bytes >= search_end) {
1930 search_start = orig_search_start;
1937 total_needed -= empty_size;
1942 block_group = btrfs_lookup_first_block_group(info, search_start);
1944 block_group = btrfs_find_block_group(root, block_group,
1945 search_start, data, 0);
1952 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 u64 num_bytes, u64 min_alloc_size,
1955 u64 empty_size, u64 hint_byte,
1956 u64 search_end, struct btrfs_key *ins,
1960 u64 search_start = 0;
1962 struct btrfs_fs_info *info = root->fs_info;
1965 alloc_profile = info->avail_data_alloc_bits &
1966 info->data_alloc_profile;
1967 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1968 } else if (root == root->fs_info->chunk_root) {
1969 alloc_profile = info->avail_system_alloc_bits &
1970 info->system_alloc_profile;
1971 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1973 alloc_profile = info->avail_metadata_alloc_bits &
1974 info->metadata_alloc_profile;
1975 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1978 data = reduce_alloc_profile(root, data);
1980 * the only place that sets empty_size is btrfs_realloc_node, which
1981 * is not called recursively on allocations
1983 if (empty_size || root->ref_cows) {
1984 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1985 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1987 BTRFS_BLOCK_GROUP_METADATA |
1988 (info->metadata_alloc_profile &
1989 info->avail_metadata_alloc_bits), 0);
1992 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1993 num_bytes + 2 * 1024 * 1024, data, 0);
1997 WARN_ON(num_bytes < root->sectorsize);
1998 ret = find_free_extent(trans, root, num_bytes, empty_size,
1999 search_start, search_end, hint_byte, ins,
2000 trans->alloc_exclude_start,
2001 trans->alloc_exclude_nr, data);
2003 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2004 num_bytes = num_bytes >> 1;
2005 num_bytes = max(num_bytes, min_alloc_size);
2006 do_chunk_alloc(trans, root->fs_info->extent_root,
2007 num_bytes, data, 1);
2011 printk("allocation failed flags %Lu\n", data);
2014 clear_extent_dirty(&root->fs_info->free_space_cache,
2015 ins->objectid, ins->objectid + ins->offset - 1,
2020 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2021 struct btrfs_root *root,
2022 u64 num_bytes, u64 min_alloc_size,
2023 u64 empty_size, u64 hint_byte,
2024 u64 search_end, struct btrfs_key *ins,
2028 maybe_lock_mutex(root);
2029 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2030 empty_size, hint_byte, search_end, ins,
2032 maybe_unlock_mutex(root);
2036 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2037 struct btrfs_root *root,
2038 u64 root_objectid, u64 ref_generation,
2039 u64 owner, u64 owner_offset,
2040 struct btrfs_key *ins)
2046 u64 num_bytes = ins->offset;
2048 struct btrfs_fs_info *info = root->fs_info;
2049 struct btrfs_root *extent_root = info->extent_root;
2050 struct btrfs_extent_item *extent_item;
2051 struct btrfs_extent_ref *ref;
2052 struct btrfs_path *path;
2053 struct btrfs_key keys[2];
2055 /* block accounting for super block */
2056 spin_lock_irq(&info->delalloc_lock);
2057 super_used = btrfs_super_bytes_used(&info->super_copy);
2058 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2059 spin_unlock_irq(&info->delalloc_lock);
2061 /* block accounting for root item */
2062 root_used = btrfs_root_used(&root->root_item);
2063 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2065 if (root == extent_root) {
2066 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2067 ins->objectid + ins->offset - 1,
2068 EXTENT_LOCKED, GFP_NOFS);
2072 memcpy(&keys[0], ins, sizeof(*ins));
2073 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
2074 owner, owner_offset);
2075 keys[1].objectid = ins->objectid;
2076 keys[1].type = BTRFS_EXTENT_REF_KEY;
2077 sizes[0] = sizeof(*extent_item);
2078 sizes[1] = sizeof(*ref);
2080 path = btrfs_alloc_path();
2083 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2087 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2088 struct btrfs_extent_item);
2089 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2090 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2091 struct btrfs_extent_ref);
2093 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2094 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2095 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2096 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2098 btrfs_mark_buffer_dirty(path->nodes[0]);
2100 trans->alloc_exclude_start = 0;
2101 trans->alloc_exclude_nr = 0;
2102 btrfs_free_path(path);
2103 finish_current_insert(trans, extent_root);
2104 pending_ret = del_pending_extents(trans, extent_root);
2114 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
2116 printk("update block group failed for %Lu %Lu\n",
2117 ins->objectid, ins->offset);
2124 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2125 struct btrfs_root *root,
2126 u64 root_objectid, u64 ref_generation,
2127 u64 owner, u64 owner_offset,
2128 struct btrfs_key *ins)
2131 maybe_lock_mutex(root);
2132 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2133 ref_generation, owner,
2135 maybe_unlock_mutex(root);
2139 * finds a free extent and does all the dirty work required for allocation
2140 * returns the key for the extent through ins, and a tree buffer for
2141 * the first block of the extent through buf.
2143 * returns 0 if everything worked, non-zero otherwise.
2145 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2146 struct btrfs_root *root,
2147 u64 num_bytes, u64 min_alloc_size,
2148 u64 root_objectid, u64 ref_generation,
2149 u64 owner, u64 owner_offset,
2150 u64 empty_size, u64 hint_byte,
2151 u64 search_end, struct btrfs_key *ins, u64 data)
2155 maybe_lock_mutex(root);
2157 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2158 min_alloc_size, empty_size, hint_byte,
2159 search_end, ins, data);
2161 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2162 ref_generation, owner,
2166 maybe_unlock_mutex(root);
2170 * helper function to allocate a block for a given tree
2171 * returns the tree buffer or NULL.
2173 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *root,
2183 struct btrfs_key ins;
2185 struct extent_buffer *buf;
2187 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
2188 root_objectid, ref_generation,
2189 level, first_objectid, empty_size, hint,
2193 return ERR_PTR(ret);
2195 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2197 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2198 root->root_key.objectid, ref_generation,
2200 return ERR_PTR(-ENOMEM);
2202 btrfs_set_header_generation(buf, trans->transid);
2203 btrfs_tree_lock(buf);
2204 clean_tree_block(trans, root, buf);
2205 btrfs_set_buffer_uptodate(buf);
2207 if (PageDirty(buf->first_page)) {
2208 printk("page %lu dirty\n", buf->first_page->index);
2212 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2213 buf->start + buf->len - 1, GFP_NOFS);
2214 trans->blocks_used++;
2218 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root,
2220 struct extent_buffer *leaf)
2223 u64 leaf_generation;
2224 struct btrfs_key key;
2225 struct btrfs_file_extent_item *fi;
2230 BUG_ON(!btrfs_is_leaf(leaf));
2231 nritems = btrfs_header_nritems(leaf);
2232 leaf_owner = btrfs_header_owner(leaf);
2233 leaf_generation = btrfs_header_generation(leaf);
2235 mutex_unlock(&root->fs_info->alloc_mutex);
2237 for (i = 0; i < nritems; i++) {
2241 btrfs_item_key_to_cpu(leaf, &key, i);
2242 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2244 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2245 if (btrfs_file_extent_type(leaf, fi) ==
2246 BTRFS_FILE_EXTENT_INLINE)
2249 * FIXME make sure to insert a trans record that
2250 * repeats the snapshot del on crash
2252 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2253 if (disk_bytenr == 0)
2256 mutex_lock(&root->fs_info->alloc_mutex);
2257 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2258 btrfs_file_extent_disk_num_bytes(leaf, fi),
2259 leaf_owner, leaf_generation,
2260 key.objectid, key.offset, 0);
2261 mutex_unlock(&root->fs_info->alloc_mutex);
2265 mutex_lock(&root->fs_info->alloc_mutex);
2269 static void noinline reada_walk_down(struct btrfs_root *root,
2270 struct extent_buffer *node,
2283 nritems = btrfs_header_nritems(node);
2284 level = btrfs_header_level(node);
2288 for (i = slot; i < nritems && skipped < 32; i++) {
2289 bytenr = btrfs_node_blockptr(node, i);
2290 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2291 (last > bytenr && last - bytenr > 32 * 1024))) {
2295 blocksize = btrfs_level_size(root, level - 1);
2297 ret = lookup_extent_ref(NULL, root, bytenr,
2305 ret = readahead_tree_block(root, bytenr, blocksize,
2306 btrfs_node_ptr_generation(node, i));
2307 last = bytenr + blocksize;
2315 * we want to avoid as much random IO as we can with the alloc mutex
2316 * held, so drop the lock and do the lookup, then do it again with the
2319 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2322 mutex_unlock(&root->fs_info->alloc_mutex);
2323 lookup_extent_ref(NULL, root, start, len, refs);
2325 mutex_lock(&root->fs_info->alloc_mutex);
2326 return lookup_extent_ref(NULL, root, start, len, refs);
2330 * helper function for drop_snapshot, this walks down the tree dropping ref
2331 * counts as it goes.
2333 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2334 struct btrfs_root *root,
2335 struct btrfs_path *path, int *level)
2341 struct extent_buffer *next;
2342 struct extent_buffer *cur;
2343 struct extent_buffer *parent;
2348 mutex_lock(&root->fs_info->alloc_mutex);
2350 WARN_ON(*level < 0);
2351 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2352 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
2353 path->nodes[*level]->len, &refs);
2359 * walk down to the last node level and free all the leaves
2361 while(*level >= 0) {
2362 WARN_ON(*level < 0);
2363 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2364 cur = path->nodes[*level];
2366 if (btrfs_header_level(cur) != *level)
2369 if (path->slots[*level] >=
2370 btrfs_header_nritems(cur))
2373 ret = drop_leaf_ref(trans, root, cur);
2377 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2378 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2379 blocksize = btrfs_level_size(root, *level - 1);
2381 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
2384 parent = path->nodes[*level];
2385 root_owner = btrfs_header_owner(parent);
2386 root_gen = btrfs_header_generation(parent);
2387 path->slots[*level]++;
2388 ret = __btrfs_free_extent(trans, root, bytenr,
2389 blocksize, root_owner,
2394 next = btrfs_find_tree_block(root, bytenr, blocksize);
2395 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2396 free_extent_buffer(next);
2397 mutex_unlock(&root->fs_info->alloc_mutex);
2399 if (path->slots[*level] == 0)
2400 reada_walk_down(root, cur, path->slots[*level]);
2402 next = read_tree_block(root, bytenr, blocksize,
2405 mutex_lock(&root->fs_info->alloc_mutex);
2407 /* we've dropped the lock, double check */
2408 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2412 parent = path->nodes[*level];
2413 root_owner = btrfs_header_owner(parent);
2414 root_gen = btrfs_header_generation(parent);
2416 path->slots[*level]++;
2417 free_extent_buffer(next);
2418 ret = __btrfs_free_extent(trans, root, bytenr,
2426 WARN_ON(*level <= 0);
2427 if (path->nodes[*level-1])
2428 free_extent_buffer(path->nodes[*level-1]);
2429 path->nodes[*level-1] = next;
2430 *level = btrfs_header_level(next);
2431 path->slots[*level] = 0;
2434 WARN_ON(*level < 0);
2435 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2437 if (path->nodes[*level] == root->node) {
2438 root_owner = root->root_key.objectid;
2439 parent = path->nodes[*level];
2441 parent = path->nodes[*level + 1];
2442 root_owner = btrfs_header_owner(parent);
2445 root_gen = btrfs_header_generation(parent);
2446 ret = __btrfs_free_extent(trans, root, path->nodes[*level]->start,
2447 path->nodes[*level]->len,
2448 root_owner, root_gen, 0, 0, 1);
2449 free_extent_buffer(path->nodes[*level]);
2450 path->nodes[*level] = NULL;
2453 mutex_unlock(&root->fs_info->alloc_mutex);
2459 * helper for dropping snapshots. This walks back up the tree in the path
2460 * to find the first node higher up where we haven't yet gone through
2463 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2464 struct btrfs_root *root,
2465 struct btrfs_path *path, int *level)
2469 struct btrfs_root_item *root_item = &root->root_item;
2474 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2475 slot = path->slots[i];
2476 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2477 struct extent_buffer *node;
2478 struct btrfs_disk_key disk_key;
2479 node = path->nodes[i];
2482 WARN_ON(*level == 0);
2483 btrfs_node_key(node, &disk_key, path->slots[i]);
2484 memcpy(&root_item->drop_progress,
2485 &disk_key, sizeof(disk_key));
2486 root_item->drop_level = i;
2489 if (path->nodes[*level] == root->node) {
2490 root_owner = root->root_key.objectid;
2492 btrfs_header_generation(path->nodes[*level]);
2494 struct extent_buffer *node;
2495 node = path->nodes[*level + 1];
2496 root_owner = btrfs_header_owner(node);
2497 root_gen = btrfs_header_generation(node);
2499 ret = btrfs_free_extent(trans, root,
2500 path->nodes[*level]->start,
2501 path->nodes[*level]->len,
2502 root_owner, root_gen, 0, 0, 1);
2504 free_extent_buffer(path->nodes[*level]);
2505 path->nodes[*level] = NULL;
2513 * drop the reference count on the tree rooted at 'snap'. This traverses
2514 * the tree freeing any blocks that have a ref count of zero after being
2517 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2523 struct btrfs_path *path;
2526 struct btrfs_root_item *root_item = &root->root_item;
2528 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
2529 path = btrfs_alloc_path();
2532 level = btrfs_header_level(root->node);
2534 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2535 path->nodes[level] = root->node;
2536 extent_buffer_get(root->node);
2537 path->slots[level] = 0;
2539 struct btrfs_key key;
2540 struct btrfs_disk_key found_key;
2541 struct extent_buffer *node;
2543 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2544 level = root_item->drop_level;
2545 path->lowest_level = level;
2546 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2551 node = path->nodes[level];
2552 btrfs_node_key(node, &found_key, path->slots[level]);
2553 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2554 sizeof(found_key)));
2556 * unlock our path, this is safe because only this
2557 * function is allowed to delete this snapshot
2559 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2560 if (path->nodes[i] && path->locks[i]) {
2562 btrfs_tree_unlock(path->nodes[i]);
2567 wret = walk_down_tree(trans, root, path, &level);
2573 wret = walk_up_tree(trans, root, path, &level);
2578 if (trans->transaction->in_commit) {
2583 for (i = 0; i <= orig_level; i++) {
2584 if (path->nodes[i]) {
2585 free_extent_buffer(path->nodes[i]);
2586 path->nodes[i] = NULL;
2590 btrfs_free_path(path);
2594 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2601 mutex_lock(&info->alloc_mutex);
2603 ret = find_first_extent_bit(&info->block_group_cache, 0,
2604 &start, &end, (unsigned int)-1);
2607 ret = get_state_private(&info->block_group_cache, start, &ptr);
2609 kfree((void *)(unsigned long)ptr);
2610 clear_extent_bits(&info->block_group_cache, start,
2611 end, (unsigned int)-1, GFP_NOFS);
2614 ret = find_first_extent_bit(&info->free_space_cache, 0,
2615 &start, &end, EXTENT_DIRTY);
2618 clear_extent_dirty(&info->free_space_cache, start,
2621 mutex_unlock(&info->alloc_mutex);
2625 static unsigned long calc_ra(unsigned long start, unsigned long last,
2628 return min(last, start + nr - 1);
2631 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2636 unsigned long last_index;
2639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2640 struct file_ra_state *ra;
2641 unsigned long total_read = 0;
2642 unsigned long ra_pages;
2643 struct btrfs_ordered_extent *ordered;
2644 struct btrfs_trans_handle *trans;
2646 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2648 mutex_lock(&inode->i_mutex);
2649 i = start >> PAGE_CACHE_SHIFT;
2650 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2652 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2654 file_ra_state_init(ra, inode->i_mapping);
2656 for (; i <= last_index; i++) {
2657 if (total_read % ra_pages == 0) {
2658 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2659 calc_ra(i, last_index, ra_pages));
2663 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
2664 goto truncate_racing;
2665 page = grab_cache_page(inode->i_mapping, i);
2669 if (!PageUptodate(page)) {
2670 btrfs_readpage(NULL, page);
2672 if (!PageUptodate(page)) {
2674 page_cache_release(page);
2678 wait_on_page_writeback(page);
2680 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2681 page_end = page_start + PAGE_CACHE_SIZE - 1;
2682 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2684 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2686 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2688 page_cache_release(page);
2689 btrfs_start_ordered_extent(inode, ordered, 1);
2690 btrfs_put_ordered_extent(ordered);
2693 set_page_extent_mapped(page);
2696 set_extent_delalloc(io_tree, page_start,
2697 page_end, GFP_NOFS);
2698 set_page_dirty(page);
2700 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2702 page_cache_release(page);
2706 /* we have to start the IO in order to get the ordered extents
2707 * instantiated. This allows the relocation to code to wait
2708 * for all the ordered extents to hit the disk.
2710 * Otherwise, it would constantly loop over the same extents
2711 * because the old ones don't get deleted until the IO is
2714 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
2717 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2719 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2720 mark_inode_dirty(inode);
2722 mutex_unlock(&inode->i_mutex);
2726 vmtruncate(inode, inode->i_size);
2727 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2733 * The back references tell us which tree holds a ref on a block,
2734 * but it is possible for the tree root field in the reference to
2735 * reflect the original root before a snapshot was made. In this
2736 * case we should search through all the children of a given root
2737 * to find potential holders of references on a block.
2739 * Instead, we do something a little less fancy and just search
2740 * all the roots for a given key/block combination.
2742 static int find_root_for_ref(struct btrfs_root *root,
2743 struct btrfs_path *path,
2744 struct btrfs_key *key0,
2747 struct btrfs_root **found_root,
2750 struct btrfs_key root_location;
2751 struct btrfs_root *cur_root = *found_root;
2752 struct btrfs_file_extent_item *file_extent;
2753 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2757 root_location.offset = (u64)-1;
2758 root_location.type = BTRFS_ROOT_ITEM_KEY;
2759 path->lowest_level = level;
2762 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2764 if (ret == 0 && file_key) {
2765 struct extent_buffer *leaf = path->nodes[0];
2766 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2767 struct btrfs_file_extent_item);
2768 if (btrfs_file_extent_type(leaf, file_extent) ==
2769 BTRFS_FILE_EXTENT_REG) {
2771 btrfs_file_extent_disk_bytenr(leaf,
2774 } else if (!file_key) {
2775 if (path->nodes[level])
2776 found_bytenr = path->nodes[level]->start;
2779 btrfs_release_path(cur_root, path);
2781 if (found_bytenr == bytenr) {
2782 *found_root = cur_root;
2786 ret = btrfs_search_root(root->fs_info->tree_root,
2787 root_search_start, &root_search_start);
2791 root_location.objectid = root_search_start;
2792 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2800 path->lowest_level = 0;
2805 * note, this releases the path
2807 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2808 struct btrfs_path *path,
2809 struct btrfs_key *extent_key,
2810 u64 *last_file_objectid,
2811 u64 *last_file_offset,
2812 u64 *last_file_root,
2815 struct inode *inode;
2816 struct btrfs_root *found_root;
2817 struct btrfs_key root_location;
2818 struct btrfs_key found_key;
2819 struct btrfs_extent_ref *ref;
2827 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
2829 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2830 struct btrfs_extent_ref);
2831 ref_root = btrfs_ref_root(path->nodes[0], ref);
2832 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2833 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2834 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2835 btrfs_release_path(extent_root, path);
2837 root_location.objectid = ref_root;
2839 root_location.offset = 0;
2841 root_location.offset = (u64)-1;
2842 root_location.type = BTRFS_ROOT_ITEM_KEY;
2844 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2846 BUG_ON(!found_root);
2847 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2849 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2850 found_key.objectid = ref_objectid;
2851 found_key.type = BTRFS_EXTENT_DATA_KEY;
2852 found_key.offset = ref_offset;
2855 if (last_extent == extent_key->objectid &&
2856 *last_file_objectid == ref_objectid &&
2857 *last_file_offset == ref_offset &&
2858 *last_file_root == ref_root)
2861 ret = find_root_for_ref(extent_root, path, &found_key,
2862 level, 1, &found_root,
2863 extent_key->objectid);
2868 if (last_extent == extent_key->objectid &&
2869 *last_file_objectid == ref_objectid &&
2870 *last_file_offset == ref_offset &&
2871 *last_file_root == ref_root)
2874 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2875 ref_objectid, found_root);
2876 if (inode->i_state & I_NEW) {
2877 /* the inode and parent dir are two different roots */
2878 BTRFS_I(inode)->root = found_root;
2879 BTRFS_I(inode)->location.objectid = ref_objectid;
2880 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2881 BTRFS_I(inode)->location.offset = 0;
2882 btrfs_read_locked_inode(inode);
2883 unlock_new_inode(inode);
2886 /* this can happen if the reference is not against
2887 * the latest version of the tree root
2889 if (is_bad_inode(inode))
2892 *last_file_objectid = inode->i_ino;
2893 *last_file_root = found_root->root_key.objectid;
2894 *last_file_offset = ref_offset;
2896 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2899 struct btrfs_trans_handle *trans;
2900 struct extent_buffer *eb;
2903 eb = read_tree_block(found_root, extent_key->objectid,
2904 extent_key->offset, 0);
2905 btrfs_tree_lock(eb);
2906 level = btrfs_header_level(eb);
2909 btrfs_item_key_to_cpu(eb, &found_key, 0);
2911 btrfs_node_key_to_cpu(eb, &found_key, 0);
2913 btrfs_tree_unlock(eb);
2914 free_extent_buffer(eb);
2916 ret = find_root_for_ref(extent_root, path, &found_key,
2917 level, 0, &found_root,
2918 extent_key->objectid);
2924 * right here almost anything could happen to our key,
2925 * but that's ok. The cow below will either relocate it
2926 * or someone else will have relocated it. Either way,
2927 * it is in a different spot than it was before and
2931 trans = btrfs_start_transaction(found_root, 1);
2933 if (found_root == extent_root->fs_info->extent_root ||
2934 found_root == extent_root->fs_info->chunk_root ||
2935 found_root == extent_root->fs_info->dev_root) {
2937 mutex_lock(&extent_root->fs_info->alloc_mutex);
2940 path->lowest_level = level;
2942 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2944 path->lowest_level = 0;
2945 btrfs_release_path(found_root, path);
2947 if (found_root == found_root->fs_info->extent_root)
2948 btrfs_extent_post_op(trans, found_root);
2950 mutex_unlock(&extent_root->fs_info->alloc_mutex);
2952 btrfs_end_transaction(trans, found_root);
2956 mutex_lock(&extent_root->fs_info->alloc_mutex);
2960 static int noinline del_extent_zero(struct btrfs_root *extent_root,
2961 struct btrfs_path *path,
2962 struct btrfs_key *extent_key)
2965 struct btrfs_trans_handle *trans;
2967 trans = btrfs_start_transaction(extent_root, 1);
2968 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2975 ret = btrfs_del_item(trans, extent_root, path);
2977 btrfs_end_transaction(trans, extent_root);
2981 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2982 struct btrfs_path *path,
2983 struct btrfs_key *extent_key)
2985 struct btrfs_key key;
2986 struct btrfs_key found_key;
2987 struct extent_buffer *leaf;
2988 u64 last_file_objectid = 0;
2989 u64 last_file_root = 0;
2990 u64 last_file_offset = (u64)-1;
2991 u64 last_extent = 0;
2996 if (extent_key->objectid == 0) {
2997 ret = del_extent_zero(extent_root, path, extent_key);
3000 key.objectid = extent_key->objectid;
3001 key.type = BTRFS_EXTENT_REF_KEY;
3005 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3011 leaf = path->nodes[0];
3012 nritems = btrfs_header_nritems(leaf);
3013 if (path->slots[0] == nritems) {
3014 ret = btrfs_next_leaf(extent_root, path);
3021 leaf = path->nodes[0];
3024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3025 if (found_key.objectid != extent_key->objectid) {
3029 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
3033 key.offset = found_key.offset + 1;
3034 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3036 ret = relocate_one_reference(extent_root, path, extent_key,
3037 &last_file_objectid,
3039 &last_file_root, last_extent);
3042 last_extent = extent_key->objectid;
3046 btrfs_release_path(extent_root, path);
3050 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3053 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3054 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3056 num_devices = root->fs_info->fs_devices->num_devices;
3057 if (num_devices == 1) {
3058 stripped |= BTRFS_BLOCK_GROUP_DUP;
3059 stripped = flags & ~stripped;
3061 /* turn raid0 into single device chunks */
3062 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3065 /* turn mirroring into duplication */
3066 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3067 BTRFS_BLOCK_GROUP_RAID10))
3068 return stripped | BTRFS_BLOCK_GROUP_DUP;
3071 /* they already had raid on here, just return */
3072 if (flags & stripped)
3075 stripped |= BTRFS_BLOCK_GROUP_DUP;
3076 stripped = flags & ~stripped;
3078 /* switch duplicated blocks with raid1 */
3079 if (flags & BTRFS_BLOCK_GROUP_DUP)
3080 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3082 /* turn single device chunks into raid0 */
3083 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3088 int __alloc_chunk_for_shrink(struct btrfs_root *root,
3089 struct btrfs_block_group_cache *shrink_block_group,
3092 struct btrfs_trans_handle *trans;
3093 u64 new_alloc_flags;
3096 spin_lock(&shrink_block_group->lock);
3097 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
3098 spin_unlock(&shrink_block_group->lock);
3099 mutex_unlock(&root->fs_info->alloc_mutex);
3101 trans = btrfs_start_transaction(root, 1);
3102 mutex_lock(&root->fs_info->alloc_mutex);
3103 spin_lock(&shrink_block_group->lock);
3105 new_alloc_flags = update_block_group_flags(root,
3106 shrink_block_group->flags);
3107 if (new_alloc_flags != shrink_block_group->flags) {
3109 btrfs_block_group_used(&shrink_block_group->item);
3111 calc = shrink_block_group->key.offset;
3113 spin_unlock(&shrink_block_group->lock);
3115 do_chunk_alloc(trans, root->fs_info->extent_root,
3116 calc + 2 * 1024 * 1024, new_alloc_flags, force);
3118 mutex_unlock(&root->fs_info->alloc_mutex);
3119 btrfs_end_transaction(trans, root);
3120 mutex_lock(&root->fs_info->alloc_mutex);
3122 spin_unlock(&shrink_block_group->lock);
3126 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
3128 struct btrfs_trans_handle *trans;
3129 struct btrfs_root *tree_root = root->fs_info->tree_root;
3130 struct btrfs_path *path;
3133 u64 shrink_last_byte;
3134 struct btrfs_block_group_cache *shrink_block_group;
3135 struct btrfs_fs_info *info = root->fs_info;
3136 struct btrfs_key key;
3137 struct btrfs_key found_key;
3138 struct extent_buffer *leaf;
3143 mutex_lock(&root->fs_info->alloc_mutex);
3144 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3146 BUG_ON(!shrink_block_group);
3148 shrink_last_byte = shrink_block_group->key.objectid +
3149 shrink_block_group->key.offset;
3151 shrink_block_group->space_info->total_bytes -=
3152 shrink_block_group->key.offset;
3153 path = btrfs_alloc_path();
3154 root = root->fs_info->extent_root;
3157 printk("btrfs relocating block group %llu flags %llu\n",
3158 (unsigned long long)shrink_start,
3159 (unsigned long long)shrink_block_group->flags);
3161 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
3165 shrink_block_group->ro = 1;
3169 key.objectid = shrink_start;
3172 cur_byte = key.objectid;
3174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3178 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
3183 leaf = path->nodes[0];
3184 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3185 if (found_key.objectid + found_key.offset > shrink_start &&
3186 found_key.objectid < shrink_last_byte) {
3187 cur_byte = found_key.objectid;
3188 key.objectid = cur_byte;
3191 btrfs_release_path(root, path);
3194 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3199 leaf = path->nodes[0];
3200 nritems = btrfs_header_nritems(leaf);
3201 if (path->slots[0] >= nritems) {
3202 ret = btrfs_next_leaf(root, path);
3209 leaf = path->nodes[0];
3210 nritems = btrfs_header_nritems(leaf);
3213 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3215 if (found_key.objectid >= shrink_last_byte)
3218 if (progress && need_resched()) {
3219 memcpy(&key, &found_key, sizeof(key));
3221 btrfs_release_path(root, path);
3222 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3228 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3229 found_key.objectid + found_key.offset <= cur_byte) {
3230 memcpy(&key, &found_key, sizeof(key));
3237 cur_byte = found_key.objectid + found_key.offset;
3238 key.objectid = cur_byte;
3239 btrfs_release_path(root, path);
3240 ret = relocate_one_extent(root, path, &found_key);
3241 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
3244 btrfs_release_path(root, path);
3246 if (total_found > 0) {
3247 printk("btrfs relocate found %llu last extent was %llu\n",
3248 (unsigned long long)total_found,
3249 (unsigned long long)found_key.objectid);
3250 mutex_unlock(&root->fs_info->alloc_mutex);
3251 trans = btrfs_start_transaction(tree_root, 1);
3252 btrfs_commit_transaction(trans, tree_root);
3254 btrfs_clean_old_snapshots(tree_root);
3256 btrfs_wait_ordered_extents(tree_root);
3258 trans = btrfs_start_transaction(tree_root, 1);
3259 btrfs_commit_transaction(trans, tree_root);
3260 mutex_lock(&root->fs_info->alloc_mutex);
3265 * we've freed all the extents, now remove the block
3266 * group item from the tree
3268 mutex_unlock(&root->fs_info->alloc_mutex);
3270 trans = btrfs_start_transaction(root, 1);
3272 mutex_lock(&root->fs_info->alloc_mutex);
3273 memcpy(&key, &shrink_block_group->key, sizeof(key));
3275 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3279 btrfs_end_transaction(trans, root);
3283 clear_extent_bits(&info->block_group_cache, key.objectid,
3284 key.objectid + key.offset - 1,
3285 (unsigned int)-1, GFP_NOFS);
3288 clear_extent_bits(&info->free_space_cache,
3289 key.objectid, key.objectid + key.offset - 1,
3290 (unsigned int)-1, GFP_NOFS);
3292 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3293 kfree(shrink_block_group);
3295 btrfs_del_item(trans, root, path);
3296 btrfs_release_path(root, path);
3297 mutex_unlock(&root->fs_info->alloc_mutex);
3298 btrfs_commit_transaction(trans, root);
3300 mutex_lock(&root->fs_info->alloc_mutex);
3302 /* the code to unpin extents might set a few bits in the free
3303 * space cache for this range again
3305 clear_extent_bits(&info->free_space_cache,
3306 key.objectid, key.objectid + key.offset - 1,
3307 (unsigned int)-1, GFP_NOFS);
3309 btrfs_free_path(path);
3310 mutex_unlock(&root->fs_info->alloc_mutex);
3314 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3315 struct btrfs_key *key)
3318 struct btrfs_key found_key;
3319 struct extent_buffer *leaf;
3322 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3327 slot = path->slots[0];
3328 leaf = path->nodes[0];
3329 if (slot >= btrfs_header_nritems(leaf)) {
3330 ret = btrfs_next_leaf(root, path);
3337 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3339 if (found_key.objectid >= key->objectid &&
3340 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3351 int btrfs_read_block_groups(struct btrfs_root *root)
3353 struct btrfs_path *path;
3356 struct btrfs_block_group_cache *cache;
3357 struct btrfs_fs_info *info = root->fs_info;
3358 struct btrfs_space_info *space_info;
3359 struct extent_io_tree *block_group_cache;
3360 struct btrfs_key key;
3361 struct btrfs_key found_key;
3362 struct extent_buffer *leaf;
3364 block_group_cache = &info->block_group_cache;
3365 root = info->extent_root;
3368 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3369 path = btrfs_alloc_path();
3373 mutex_lock(&root->fs_info->alloc_mutex);
3375 ret = find_first_block_group(root, path, &key);
3383 leaf = path->nodes[0];
3384 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3385 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3391 spin_lock_init(&cache->lock);
3392 read_extent_buffer(leaf, &cache->item,
3393 btrfs_item_ptr_offset(leaf, path->slots[0]),
3394 sizeof(cache->item));
3395 memcpy(&cache->key, &found_key, sizeof(found_key));
3397 key.objectid = found_key.objectid + found_key.offset;
3398 btrfs_release_path(root, path);
3399 cache->flags = btrfs_block_group_flags(&cache->item);
3401 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3402 bit = BLOCK_GROUP_DATA;
3403 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3404 bit = BLOCK_GROUP_SYSTEM;
3405 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3406 bit = BLOCK_GROUP_METADATA;
3408 set_avail_alloc_bits(info, cache->flags);
3410 ret = update_space_info(info, cache->flags, found_key.offset,
3411 btrfs_block_group_used(&cache->item),
3414 cache->space_info = space_info;
3416 /* use EXTENT_LOCKED to prevent merging */
3417 set_extent_bits(block_group_cache, found_key.objectid,
3418 found_key.objectid + found_key.offset - 1,
3419 EXTENT_LOCKED, GFP_NOFS);
3420 set_state_private(block_group_cache, found_key.objectid,
3421 (unsigned long)cache);
3422 set_extent_bits(block_group_cache, found_key.objectid,
3423 found_key.objectid + found_key.offset - 1,
3424 bit | EXTENT_LOCKED, GFP_NOFS);
3426 btrfs_super_total_bytes(&info->super_copy))
3431 btrfs_free_path(path);
3432 mutex_unlock(&root->fs_info->alloc_mutex);
3436 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3437 struct btrfs_root *root, u64 bytes_used,
3438 u64 type, u64 chunk_objectid, u64 chunk_offset,
3443 struct btrfs_root *extent_root;
3444 struct btrfs_block_group_cache *cache;
3445 struct extent_io_tree *block_group_cache;
3447 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
3448 extent_root = root->fs_info->extent_root;
3449 block_group_cache = &root->fs_info->block_group_cache;
3451 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3453 cache->key.objectid = chunk_offset;
3454 cache->key.offset = size;
3455 spin_lock_init(&cache->lock);
3456 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3458 btrfs_set_block_group_used(&cache->item, bytes_used);
3459 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3460 cache->flags = type;
3461 btrfs_set_block_group_flags(&cache->item, type);
3463 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3464 &cache->space_info);
3467 bit = block_group_state_bits(type);
3468 set_extent_bits(block_group_cache, chunk_offset,
3469 chunk_offset + size - 1,
3470 EXTENT_LOCKED, GFP_NOFS);
3471 set_state_private(block_group_cache, chunk_offset,
3472 (unsigned long)cache);
3473 set_extent_bits(block_group_cache, chunk_offset,
3474 chunk_offset + size - 1,
3475 bit | EXTENT_LOCKED, GFP_NOFS);
3477 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3478 sizeof(cache->item));
3481 finish_current_insert(trans, extent_root);
3482 ret = del_pending_extents(trans, extent_root);
3484 set_avail_alloc_bits(extent_root->fs_info, type);