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"
29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
31 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
33 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
37 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
38 btrfs_root *extent_root);
41 static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group)
44 struct btrfs_path *path;
47 struct extent_buffer *leaf;
48 struct extent_io_tree *free_space_cache;
58 root = root->fs_info->extent_root;
59 free_space_cache = &root->fs_info->free_space_cache;
61 if (block_group->cached)
64 path = btrfs_alloc_path();
69 first_free = block_group->key.objectid;
70 key.objectid = block_group->key.objectid;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset;
86 leaf = path->nodes[0];
87 slot = path->slots[0];
88 if (slot >= btrfs_header_nritems(leaf)) {
89 ret = btrfs_next_leaf(root, path);
98 btrfs_item_key_to_cpu(leaf, &key, slot);
99 if (key.objectid < block_group->key.objectid) {
102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) {
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
112 if (key.objectid > last) {
113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1,
118 last = key.objectid + key.offset;
126 if (block_group->key.objectid +
127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last;
130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS);
133 block_group->cached = 1;
135 btrfs_free_path(path);
139 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
143 struct extent_io_tree *block_group_cache;
144 struct btrfs_block_group_cache *block_group = NULL;
150 bytenr = max_t(u64, bytenr,
151 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
152 block_group_cache = &info->block_group_cache;
153 ret = find_first_extent_bit(block_group_cache,
154 bytenr, &start, &end,
155 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
160 ret = get_state_private(block_group_cache, start, &ptr);
164 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
168 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
172 struct extent_io_tree *block_group_cache;
173 struct btrfs_block_group_cache *block_group = NULL;
179 bytenr = max_t(u64, bytenr,
180 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
181 block_group_cache = &info->block_group_cache;
182 ret = find_first_extent_bit(block_group_cache,
183 bytenr, &start, &end,
184 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
189 ret = get_state_private(block_group_cache, start, &ptr);
193 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
194 if (block_group->key.objectid <= bytenr && bytenr <
195 block_group->key.objectid + block_group->key.offset)
200 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
202 return (cache->flags & bits) == bits;
205 static int noinline find_search_start(struct btrfs_root *root,
206 struct btrfs_block_group_cache **cache_ret,
207 u64 *start_ret, u64 num, int data)
210 struct btrfs_block_group_cache *cache = *cache_ret;
211 struct extent_io_tree *free_space_cache;
212 struct extent_state *state;
217 u64 search_start = *start_ret;
220 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
221 free_space_cache = &root->fs_info->free_space_cache;
227 ret = cache_block_group(root, cache);
232 last = max(search_start, cache->key.objectid);
233 if (!block_group_bits(cache, data) || cache->ro)
236 spin_lock_irq(&free_space_cache->lock);
237 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
242 spin_unlock_irq(&free_space_cache->lock);
246 start = max(last, state->start);
247 last = state->end + 1;
248 if (last - start < num) {
250 state = extent_state_next(state);
251 } while(state && !(state->state & EXTENT_DIRTY));
254 spin_unlock_irq(&free_space_cache->lock);
258 if (start + num > cache->key.objectid + cache->key.offset)
260 if (!block_group_bits(cache, data)) {
261 printk("block group bits don't match %Lu %d\n", cache->flags, data);
267 cache = btrfs_lookup_block_group(root->fs_info, search_start);
269 printk("Unable to find block group for %Lu\n", search_start);
275 last = cache->key.objectid + cache->key.offset;
277 cache = btrfs_lookup_first_block_group(root->fs_info, last);
278 if (!cache || cache->key.objectid >= total_fs_bytes) {
287 if (cache_miss && !cache->cached) {
288 cache_block_group(root, cache);
290 cache = btrfs_lookup_first_block_group(root->fs_info, last);
293 cache = btrfs_find_block_group(root, cache, last, data, 0);
300 static u64 div_factor(u64 num, int factor)
309 static int block_group_state_bits(u64 flags)
312 if (flags & BTRFS_BLOCK_GROUP_DATA)
313 bits |= BLOCK_GROUP_DATA;
314 if (flags & BTRFS_BLOCK_GROUP_METADATA)
315 bits |= BLOCK_GROUP_METADATA;
316 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
317 bits |= BLOCK_GROUP_SYSTEM;
321 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
322 struct btrfs_block_group_cache
323 *hint, u64 search_start,
326 struct btrfs_block_group_cache *cache;
327 struct extent_io_tree *block_group_cache;
328 struct btrfs_block_group_cache *found_group = NULL;
329 struct btrfs_fs_info *info = root->fs_info;
342 block_group_cache = &info->block_group_cache;
344 if (data & BTRFS_BLOCK_GROUP_METADATA)
347 bit = block_group_state_bits(data);
350 struct btrfs_block_group_cache *shint;
351 shint = btrfs_lookup_first_block_group(info, search_start);
352 if (shint && block_group_bits(shint, data) && !shint->ro) {
353 used = btrfs_block_group_used(&shint->item);
354 if (used + shint->pinned <
355 div_factor(shint->key.offset, factor)) {
360 if (hint && !hint->ro && block_group_bits(hint, data)) {
361 used = btrfs_block_group_used(&hint->item);
362 if (used + hint->pinned <
363 div_factor(hint->key.offset, factor)) {
366 last = hint->key.objectid + hint->key.offset;
369 last = max(hint->key.objectid, search_start);
375 ret = find_first_extent_bit(block_group_cache, last,
380 ret = get_state_private(block_group_cache, start, &ptr);
386 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
387 last = cache->key.objectid + cache->key.offset;
388 used = btrfs_block_group_used(&cache->item);
390 if (!cache->ro && block_group_bits(cache, data)) {
391 free_check = div_factor(cache->key.offset, factor);
392 if (used + cache->pinned < free_check) {
404 if (!full_search && factor < 10) {
414 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
415 u64 owner, u64 owner_offset)
417 u32 high_crc = ~(u32)0;
418 u32 low_crc = ~(u32)0;
420 lenum = cpu_to_le64(root_objectid);
421 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
422 lenum = cpu_to_le64(ref_generation);
423 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
424 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
425 lenum = cpu_to_le64(owner);
426 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
427 lenum = cpu_to_le64(owner_offset);
428 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
430 return ((u64)high_crc << 32) | (u64)low_crc;
433 static int match_extent_ref(struct extent_buffer *leaf,
434 struct btrfs_extent_ref *disk_ref,
435 struct btrfs_extent_ref *cpu_ref)
440 if (cpu_ref->objectid)
441 len = sizeof(*cpu_ref);
443 len = 2 * sizeof(u64);
444 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
449 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
450 struct btrfs_root *root,
451 struct btrfs_path *path, u64 bytenr,
453 u64 ref_generation, u64 owner,
454 u64 owner_offset, int del)
457 struct btrfs_key key;
458 struct btrfs_key found_key;
459 struct btrfs_extent_ref ref;
460 struct extent_buffer *leaf;
461 struct btrfs_extent_ref *disk_ref;
465 btrfs_set_stack_ref_root(&ref, root_objectid);
466 btrfs_set_stack_ref_generation(&ref, ref_generation);
467 btrfs_set_stack_ref_objectid(&ref, owner);
468 btrfs_set_stack_ref_offset(&ref, owner_offset);
470 hash = hash_extent_ref(root_objectid, ref_generation, owner,
473 key.objectid = bytenr;
474 key.type = BTRFS_EXTENT_REF_KEY;
477 ret = btrfs_search_slot(trans, root, &key, path,
481 leaf = path->nodes[0];
483 u32 nritems = btrfs_header_nritems(leaf);
484 if (path->slots[0] >= nritems) {
485 ret2 = btrfs_next_leaf(root, path);
488 leaf = path->nodes[0];
490 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
491 if (found_key.objectid != bytenr ||
492 found_key.type != BTRFS_EXTENT_REF_KEY)
494 key.offset = found_key.offset;
496 btrfs_release_path(root, path);
500 disk_ref = btrfs_item_ptr(path->nodes[0],
502 struct btrfs_extent_ref);
503 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
507 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
508 key.offset = found_key.offset + 1;
509 btrfs_release_path(root, path);
516 * Back reference rules. Back refs have three main goals:
518 * 1) differentiate between all holders of references to an extent so that
519 * when a reference is dropped we can make sure it was a valid reference
520 * before freeing the extent.
522 * 2) Provide enough information to quickly find the holders of an extent
523 * if we notice a given block is corrupted or bad.
525 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
526 * maintenance. This is actually the same as #2, but with a slightly
527 * different use case.
529 * File extents can be referenced by:
531 * - multiple snapshots, subvolumes, or different generations in one subvol
532 * - different files inside a single subvolume (in theory, not implemented yet)
533 * - different offsets inside a file (bookend extents in file.c)
535 * The extent ref structure has fields for:
537 * - Objectid of the subvolume root
538 * - Generation number of the tree holding the reference
539 * - objectid of the file holding the reference
540 * - offset in the file corresponding to the key holding the reference
542 * When a file extent is allocated the fields are filled in:
543 * (root_key.objectid, trans->transid, inode objectid, offset in file)
545 * When a leaf is cow'd new references are added for every file extent found
546 * in the leaf. It looks the same as the create case, but trans->transid
547 * will be different when the block is cow'd.
549 * (root_key.objectid, trans->transid, inode objectid, offset in file)
551 * When a file extent is removed either during snapshot deletion or file
552 * truncation, the corresponding back reference is found
555 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
556 * inode objectid, offset in file)
558 * Btree extents can be referenced by:
560 * - Different subvolumes
561 * - Different generations of the same subvolume
563 * Storing sufficient information for a full reverse mapping of a btree
564 * block would require storing the lowest key of the block in the backref,
565 * and it would require updating that lowest key either before write out or
566 * every time it changed. Instead, the objectid of the lowest key is stored
567 * along with the level of the tree block. This provides a hint
568 * about where in the btree the block can be found. Searches through the
569 * btree only need to look for a pointer to that block, so they stop one
570 * level higher than the level recorded in the backref.
572 * Some btrees do not do reference counting on their extents. These
573 * include the extent tree and the tree of tree roots. Backrefs for these
574 * trees always have a generation of zero.
576 * When a tree block is created, back references are inserted:
578 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
580 * When a tree block is cow'd in a reference counted root,
581 * new back references are added for all the blocks it points to.
582 * These are of the form (trans->transid will have increased since creation):
584 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
586 * Because the lowest_key_objectid and the level are just hints
587 * they are not used when backrefs are deleted. When a backref is deleted:
589 * if backref was for a tree root:
590 * root_objectid = root->root_key.objectid
592 * root_objectid = btrfs_header_owner(parent)
594 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
596 * Back Reference Key hashing:
598 * Back references have four fields, each 64 bits long. Unfortunately,
599 * This is hashed into a single 64 bit number and placed into the key offset.
600 * The key objectid corresponds to the first byte in the extent, and the
601 * key type is set to BTRFS_EXTENT_REF_KEY
603 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
604 struct btrfs_root *root,
605 struct btrfs_path *path, u64 bytenr,
606 u64 root_objectid, u64 ref_generation,
607 u64 owner, u64 owner_offset)
610 struct btrfs_key key;
611 struct btrfs_extent_ref ref;
612 struct btrfs_extent_ref *disk_ref;
615 btrfs_set_stack_ref_root(&ref, root_objectid);
616 btrfs_set_stack_ref_generation(&ref, ref_generation);
617 btrfs_set_stack_ref_objectid(&ref, owner);
618 btrfs_set_stack_ref_offset(&ref, owner_offset);
620 hash = hash_extent_ref(root_objectid, ref_generation, owner,
623 key.objectid = bytenr;
624 key.type = BTRFS_EXTENT_REF_KEY;
626 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
627 while (ret == -EEXIST) {
628 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
629 struct btrfs_extent_ref);
630 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
633 btrfs_release_path(root, path);
634 ret = btrfs_insert_empty_item(trans, root, path, &key,
639 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
640 struct btrfs_extent_ref);
641 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
643 btrfs_mark_buffer_dirty(path->nodes[0]);
645 btrfs_release_path(root, path);
649 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
650 struct btrfs_root *root,
651 u64 bytenr, u64 num_bytes,
652 u64 root_objectid, u64 ref_generation,
653 u64 owner, u64 owner_offset)
655 struct btrfs_path *path;
657 struct btrfs_key key;
658 struct extent_buffer *l;
659 struct btrfs_extent_item *item;
662 WARN_ON(num_bytes < root->sectorsize);
663 path = btrfs_alloc_path();
668 key.objectid = bytenr;
669 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
670 key.offset = num_bytes;
671 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
680 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
681 refs = btrfs_extent_refs(l, item);
682 btrfs_set_extent_refs(l, item, refs + 1);
683 btrfs_mark_buffer_dirty(path->nodes[0]);
685 btrfs_release_path(root->fs_info->extent_root, path);
688 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
689 path, bytenr, root_objectid,
690 ref_generation, owner, owner_offset);
692 finish_current_insert(trans, root->fs_info->extent_root);
693 del_pending_extents(trans, root->fs_info->extent_root);
695 btrfs_free_path(path);
699 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
700 struct btrfs_root *root)
702 finish_current_insert(trans, root->fs_info->extent_root);
703 del_pending_extents(trans, root->fs_info->extent_root);
707 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root, u64 bytenr,
709 u64 num_bytes, u32 *refs)
711 struct btrfs_path *path;
713 struct btrfs_key key;
714 struct extent_buffer *l;
715 struct btrfs_extent_item *item;
717 WARN_ON(num_bytes < root->sectorsize);
718 path = btrfs_alloc_path();
720 key.objectid = bytenr;
721 key.offset = num_bytes;
722 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
723 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
728 btrfs_print_leaf(root, path->nodes[0]);
729 printk("failed to find block number %Lu\n", bytenr);
733 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
734 *refs = btrfs_extent_refs(l, item);
736 btrfs_free_path(path);
740 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
741 struct btrfs_path *count_path,
745 struct btrfs_root *extent_root = root->fs_info->extent_root;
746 struct btrfs_path *path;
750 u64 root_objectid = root->root_key.objectid;
756 struct btrfs_key key;
757 struct btrfs_key found_key;
758 struct extent_buffer *l;
759 struct btrfs_extent_item *item;
760 struct btrfs_extent_ref *ref_item;
763 path = btrfs_alloc_path();
766 bytenr = first_extent;
768 bytenr = count_path->nodes[level]->start;
771 key.objectid = bytenr;
774 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
775 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
781 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
783 if (found_key.objectid != bytenr ||
784 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
788 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
789 extent_refs = btrfs_extent_refs(l, item);
792 nritems = btrfs_header_nritems(l);
793 if (path->slots[0] >= nritems) {
794 ret = btrfs_next_leaf(extent_root, path);
799 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
800 if (found_key.objectid != bytenr)
803 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
809 ref_item = btrfs_item_ptr(l, path->slots[0],
810 struct btrfs_extent_ref);
811 found_objectid = btrfs_ref_root(l, ref_item);
813 if (found_objectid != root_objectid) {
818 found_owner = btrfs_ref_objectid(l, ref_item);
819 if (found_owner != expected_owner) {
824 * nasty. we don't count a reference held by
825 * the running transaction. This allows nodatacow
826 * to avoid cow most of the time
828 if (found_owner >= BTRFS_FIRST_FREE_OBJECTID &&
829 btrfs_ref_generation(l, ref_item) ==
830 root->fs_info->generation) {
838 * if there is more than one reference against a data extent,
839 * we have to assume the other ref is another snapshot
841 if (level == -1 && extent_refs > 1) {
845 if (cur_count == 0) {
849 if (level >= 0 && root->node == count_path->nodes[level])
852 btrfs_release_path(root, path);
856 btrfs_free_path(path);
859 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
860 struct btrfs_root *root, u64 owner_objectid)
866 struct btrfs_disk_key disk_key;
868 level = btrfs_header_level(root->node);
869 generation = trans->transid;
870 nritems = btrfs_header_nritems(root->node);
873 btrfs_item_key(root->node, &disk_key, 0);
875 btrfs_node_key(root->node, &disk_key, 0);
876 key_objectid = btrfs_disk_key_objectid(&disk_key);
880 return btrfs_inc_extent_ref(trans, root, root->node->start,
881 root->node->len, owner_objectid,
882 generation, level, key_objectid);
885 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
886 struct extent_buffer *buf)
890 struct btrfs_key key;
891 struct btrfs_file_extent_item *fi;
900 level = btrfs_header_level(buf);
901 nritems = btrfs_header_nritems(buf);
902 for (i = 0; i < nritems; i++) {
905 btrfs_item_key_to_cpu(buf, &key, i);
906 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
908 fi = btrfs_item_ptr(buf, i,
909 struct btrfs_file_extent_item);
910 if (btrfs_file_extent_type(buf, fi) ==
911 BTRFS_FILE_EXTENT_INLINE)
913 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
914 if (disk_bytenr == 0)
916 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
917 btrfs_file_extent_disk_num_bytes(buf, fi),
918 root->root_key.objectid, trans->transid,
919 key.objectid, key.offset);
925 bytenr = btrfs_node_blockptr(buf, i);
926 btrfs_node_key_to_cpu(buf, &key, i);
927 ret = btrfs_inc_extent_ref(trans, root, bytenr,
928 btrfs_level_size(root, level - 1),
929 root->root_key.objectid,
931 level - 1, key.objectid);
942 for (i =0; i < faili; i++) {
945 btrfs_item_key_to_cpu(buf, &key, i);
946 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
948 fi = btrfs_item_ptr(buf, i,
949 struct btrfs_file_extent_item);
950 if (btrfs_file_extent_type(buf, fi) ==
951 BTRFS_FILE_EXTENT_INLINE)
953 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
954 if (disk_bytenr == 0)
956 err = btrfs_free_extent(trans, root, disk_bytenr,
957 btrfs_file_extent_disk_num_bytes(buf,
961 bytenr = btrfs_node_blockptr(buf, i);
962 err = btrfs_free_extent(trans, root, bytenr,
963 btrfs_level_size(root, level - 1), 0);
971 static int write_one_cache_group(struct btrfs_trans_handle *trans,
972 struct btrfs_root *root,
973 struct btrfs_path *path,
974 struct btrfs_block_group_cache *cache)
978 struct btrfs_root *extent_root = root->fs_info->extent_root;
980 struct extent_buffer *leaf;
982 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
987 leaf = path->nodes[0];
988 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
989 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
990 btrfs_mark_buffer_dirty(leaf);
991 btrfs_release_path(extent_root, path);
993 finish_current_insert(trans, extent_root);
994 pending_ret = del_pending_extents(trans, extent_root);
1003 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1004 struct btrfs_root *root)
1006 struct extent_io_tree *block_group_cache;
1007 struct btrfs_block_group_cache *cache;
1011 struct btrfs_path *path;
1017 block_group_cache = &root->fs_info->block_group_cache;
1018 path = btrfs_alloc_path();
1023 ret = find_first_extent_bit(block_group_cache, last,
1024 &start, &end, BLOCK_GROUP_DIRTY);
1029 ret = get_state_private(block_group_cache, start, &ptr);
1032 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1033 err = write_one_cache_group(trans, root,
1036 * if we fail to write the cache group, we want
1037 * to keep it marked dirty in hopes that a later
1044 clear_extent_bits(block_group_cache, start, end,
1045 BLOCK_GROUP_DIRTY, GFP_NOFS);
1047 btrfs_free_path(path);
1051 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1054 struct list_head *head = &info->space_info;
1055 struct list_head *cur;
1056 struct btrfs_space_info *found;
1057 list_for_each(cur, head) {
1058 found = list_entry(cur, struct btrfs_space_info, list);
1059 if (found->flags == flags)
1066 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1067 u64 total_bytes, u64 bytes_used,
1068 struct btrfs_space_info **space_info)
1070 struct btrfs_space_info *found;
1072 found = __find_space_info(info, flags);
1074 found->total_bytes += total_bytes;
1075 found->bytes_used += bytes_used;
1077 WARN_ON(found->total_bytes < found->bytes_used);
1078 *space_info = found;
1081 found = kmalloc(sizeof(*found), GFP_NOFS);
1085 list_add(&found->list, &info->space_info);
1086 found->flags = flags;
1087 found->total_bytes = total_bytes;
1088 found->bytes_used = bytes_used;
1089 found->bytes_pinned = 0;
1091 found->force_alloc = 0;
1092 *space_info = found;
1096 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1098 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1099 BTRFS_BLOCK_GROUP_RAID1 |
1100 BTRFS_BLOCK_GROUP_RAID10 |
1101 BTRFS_BLOCK_GROUP_DUP);
1103 if (flags & BTRFS_BLOCK_GROUP_DATA)
1104 fs_info->avail_data_alloc_bits |= extra_flags;
1105 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1106 fs_info->avail_metadata_alloc_bits |= extra_flags;
1107 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1108 fs_info->avail_system_alloc_bits |= extra_flags;
1112 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1114 u64 num_devices = root->fs_info->fs_devices->num_devices;
1116 if (num_devices == 1)
1117 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1118 if (num_devices < 4)
1119 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1121 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1122 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1123 BTRFS_BLOCK_GROUP_RAID10))) {
1124 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1127 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1128 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1129 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1132 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1133 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1134 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1135 (flags & BTRFS_BLOCK_GROUP_DUP)))
1136 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1140 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *extent_root, u64 alloc_bytes,
1142 u64 flags, int force)
1144 struct btrfs_space_info *space_info;
1150 flags = reduce_alloc_profile(extent_root, flags);
1152 space_info = __find_space_info(extent_root->fs_info, flags);
1154 ret = update_space_info(extent_root->fs_info, flags,
1158 BUG_ON(!space_info);
1160 if (space_info->force_alloc) {
1162 space_info->force_alloc = 0;
1164 if (space_info->full)
1167 thresh = div_factor(space_info->total_bytes, 6);
1169 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1173 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1174 if (ret == -ENOSPC) {
1175 printk("space info full %Lu\n", flags);
1176 space_info->full = 1;
1181 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1182 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1188 static int update_block_group(struct btrfs_trans_handle *trans,
1189 struct btrfs_root *root,
1190 u64 bytenr, u64 num_bytes, int alloc,
1193 struct btrfs_block_group_cache *cache;
1194 struct btrfs_fs_info *info = root->fs_info;
1195 u64 total = num_bytes;
1202 cache = btrfs_lookup_block_group(info, bytenr);
1206 byte_in_group = bytenr - cache->key.objectid;
1207 WARN_ON(byte_in_group > cache->key.offset);
1208 start = cache->key.objectid;
1209 end = start + cache->key.offset - 1;
1210 set_extent_bits(&info->block_group_cache, start, end,
1211 BLOCK_GROUP_DIRTY, GFP_NOFS);
1213 old_val = btrfs_block_group_used(&cache->item);
1214 num_bytes = min(total, cache->key.offset - byte_in_group);
1216 old_val += num_bytes;
1217 cache->space_info->bytes_used += num_bytes;
1219 old_val -= num_bytes;
1220 cache->space_info->bytes_used -= num_bytes;
1222 set_extent_dirty(&info->free_space_cache,
1223 bytenr, bytenr + num_bytes - 1,
1227 btrfs_set_block_group_used(&cache->item, old_val);
1229 bytenr += num_bytes;
1234 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1239 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1240 search_start, &start, &end,
1241 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1242 BLOCK_GROUP_SYSTEM);
1249 static int update_pinned_extents(struct btrfs_root *root,
1250 u64 bytenr, u64 num, int pin)
1253 struct btrfs_block_group_cache *cache;
1254 struct btrfs_fs_info *fs_info = root->fs_info;
1257 set_extent_dirty(&fs_info->pinned_extents,
1258 bytenr, bytenr + num - 1, GFP_NOFS);
1260 clear_extent_dirty(&fs_info->pinned_extents,
1261 bytenr, bytenr + num - 1, GFP_NOFS);
1264 cache = btrfs_lookup_block_group(fs_info, bytenr);
1266 u64 first = first_logical_byte(root, bytenr);
1267 WARN_ON(first < bytenr);
1268 len = min(first - bytenr, num);
1270 len = min(num, cache->key.offset -
1271 (bytenr - cache->key.objectid));
1275 cache->pinned += len;
1276 cache->space_info->bytes_pinned += len;
1278 fs_info->total_pinned += len;
1281 cache->pinned -= len;
1282 cache->space_info->bytes_pinned -= len;
1284 fs_info->total_pinned -= len;
1292 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1297 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1301 ret = find_first_extent_bit(pinned_extents, last,
1302 &start, &end, EXTENT_DIRTY);
1305 set_extent_dirty(copy, start, end, GFP_NOFS);
1311 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1312 struct btrfs_root *root,
1313 struct extent_io_tree *unpin)
1318 struct extent_io_tree *free_space_cache;
1319 free_space_cache = &root->fs_info->free_space_cache;
1322 ret = find_first_extent_bit(unpin, 0, &start, &end,
1326 update_pinned_extents(root, start, end + 1 - start, 0);
1327 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1328 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1333 static int finish_current_insert(struct btrfs_trans_handle *trans,
1334 struct btrfs_root *extent_root)
1338 struct btrfs_fs_info *info = extent_root->fs_info;
1339 struct extent_buffer *eb;
1340 struct btrfs_path *path;
1341 struct btrfs_key ins;
1342 struct btrfs_disk_key first;
1343 struct btrfs_extent_item extent_item;
1348 btrfs_set_stack_extent_refs(&extent_item, 1);
1349 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1350 path = btrfs_alloc_path();
1353 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1354 &end, EXTENT_LOCKED);
1358 ins.objectid = start;
1359 ins.offset = end + 1 - start;
1360 err = btrfs_insert_item(trans, extent_root, &ins,
1361 &extent_item, sizeof(extent_item));
1362 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1364 eb = read_tree_block(extent_root, ins.objectid, ins.offset,
1366 level = btrfs_header_level(eb);
1368 btrfs_item_key(eb, &first, 0);
1370 btrfs_node_key(eb, &first, 0);
1372 err = btrfs_insert_extent_backref(trans, extent_root, path,
1373 start, extent_root->root_key.objectid,
1375 btrfs_disk_key_objectid(&first));
1377 free_extent_buffer(eb);
1379 btrfs_free_path(path);
1383 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1387 struct extent_buffer *buf;
1390 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1392 if (btrfs_buffer_uptodate(buf, 0)) {
1394 root->fs_info->running_transaction->transid;
1395 u64 header_transid =
1396 btrfs_header_generation(buf);
1397 if (header_transid == transid &&
1398 !btrfs_header_flag(buf,
1399 BTRFS_HEADER_FLAG_WRITTEN)) {
1400 clean_tree_block(NULL, root, buf);
1401 free_extent_buffer(buf);
1405 free_extent_buffer(buf);
1407 update_pinned_extents(root, bytenr, num_bytes, 1);
1409 set_extent_bits(&root->fs_info->pending_del,
1410 bytenr, bytenr + num_bytes - 1,
1411 EXTENT_LOCKED, GFP_NOFS);
1418 * remove an extent from the root, returns 0 on success
1420 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1421 *root, u64 bytenr, u64 num_bytes,
1422 u64 root_objectid, u64 ref_generation,
1423 u64 owner_objectid, u64 owner_offset, int pin,
1426 struct btrfs_path *path;
1427 struct btrfs_key key;
1428 struct btrfs_fs_info *info = root->fs_info;
1429 struct btrfs_root *extent_root = info->extent_root;
1430 struct extent_buffer *leaf;
1432 int extent_slot = 0;
1433 int found_extent = 0;
1435 struct btrfs_extent_item *ei;
1438 key.objectid = bytenr;
1439 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1440 key.offset = num_bytes;
1441 path = btrfs_alloc_path();
1446 ret = lookup_extent_backref(trans, extent_root, path,
1447 bytenr, root_objectid,
1449 owner_objectid, owner_offset, 1);
1451 struct btrfs_key found_key;
1452 extent_slot = path->slots[0];
1453 while(extent_slot > 0) {
1455 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1457 if (found_key.objectid != bytenr)
1459 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1460 found_key.offset == num_bytes) {
1464 if (path->slots[0] - extent_slot > 5)
1468 ret = btrfs_del_item(trans, extent_root, path);
1470 btrfs_print_leaf(extent_root, path->nodes[0]);
1472 printk("Unable to find ref byte nr %Lu root %Lu "
1473 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1474 root_objectid, ref_generation, owner_objectid,
1477 if (!found_extent) {
1478 btrfs_release_path(extent_root, path);
1479 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1483 extent_slot = path->slots[0];
1486 leaf = path->nodes[0];
1487 ei = btrfs_item_ptr(leaf, extent_slot,
1488 struct btrfs_extent_item);
1489 refs = btrfs_extent_refs(leaf, ei);
1492 btrfs_set_extent_refs(leaf, ei, refs);
1494 btrfs_mark_buffer_dirty(leaf);
1496 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1497 /* if the back ref and the extent are next to each other
1498 * they get deleted below in one shot
1500 path->slots[0] = extent_slot;
1502 } else if (found_extent) {
1503 /* otherwise delete the extent back ref */
1504 ret = btrfs_del_item(trans, extent_root, path);
1506 /* if refs are 0, we need to setup the path for deletion */
1508 btrfs_release_path(extent_root, path);
1509 ret = btrfs_search_slot(trans, extent_root, &key, path,
1522 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1528 /* block accounting for super block */
1529 super_used = btrfs_super_bytes_used(&info->super_copy);
1530 btrfs_set_super_bytes_used(&info->super_copy,
1531 super_used - num_bytes);
1533 /* block accounting for root item */
1534 root_used = btrfs_root_used(&root->root_item);
1535 btrfs_set_root_used(&root->root_item,
1536 root_used - num_bytes);
1537 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1542 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1546 btrfs_free_path(path);
1547 finish_current_insert(trans, extent_root);
1552 * find all the blocks marked as pending in the radix tree and remove
1553 * them from the extent map
1555 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1556 btrfs_root *extent_root)
1562 struct extent_io_tree *pending_del;
1563 struct extent_io_tree *pinned_extents;
1565 pending_del = &extent_root->fs_info->pending_del;
1566 pinned_extents = &extent_root->fs_info->pinned_extents;
1569 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1573 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1574 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1576 ret = __free_extent(trans, extent_root,
1577 start, end + 1 - start,
1578 extent_root->root_key.objectid,
1587 * remove an extent from the root, returns 0 on success
1589 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1590 *root, u64 bytenr, u64 num_bytes,
1591 u64 root_objectid, u64 ref_generation,
1592 u64 owner_objectid, u64 owner_offset, int pin)
1594 struct btrfs_root *extent_root = root->fs_info->extent_root;
1598 WARN_ON(num_bytes < root->sectorsize);
1599 if (!root->ref_cows)
1602 if (root == extent_root) {
1603 pin_down_bytes(root, bytenr, num_bytes, 1);
1606 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1607 ref_generation, owner_objectid, owner_offset,
1609 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1610 return ret ? ret : pending_ret;
1613 static u64 stripe_align(struct btrfs_root *root, u64 val)
1615 u64 mask = ((u64)root->stripesize - 1);
1616 u64 ret = (val + mask) & ~mask;
1621 * walks the btree of allocated extents and find a hole of a given size.
1622 * The key ins is changed to record the hole:
1623 * ins->objectid == block start
1624 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1625 * ins->offset == number of blocks
1626 * Any available blocks before search_start are skipped.
1628 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1629 struct btrfs_root *orig_root,
1630 u64 num_bytes, u64 empty_size,
1631 u64 search_start, u64 search_end,
1632 u64 hint_byte, struct btrfs_key *ins,
1633 u64 exclude_start, u64 exclude_nr,
1637 u64 orig_search_start;
1638 struct btrfs_root * root = orig_root->fs_info->extent_root;
1639 struct btrfs_fs_info *info = root->fs_info;
1640 u64 total_needed = num_bytes;
1641 u64 *last_ptr = NULL;
1642 struct btrfs_block_group_cache *block_group;
1645 int chunk_alloc_done = 0;
1646 int empty_cluster = 2 * 1024 * 1024;
1647 int allowed_chunk_alloc = 0;
1649 WARN_ON(num_bytes < root->sectorsize);
1650 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1652 if (orig_root->ref_cows || empty_size)
1653 allowed_chunk_alloc = 1;
1655 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1656 last_ptr = &root->fs_info->last_alloc;
1657 empty_cluster = 256 * 1024;
1660 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1661 last_ptr = &root->fs_info->last_data_alloc;
1666 hint_byte = *last_ptr;
1668 empty_size += empty_cluster;
1672 search_start = max(search_start, first_logical_byte(root, 0));
1673 orig_search_start = search_start;
1675 if (search_end == (u64)-1)
1676 search_end = btrfs_super_total_bytes(&info->super_copy);
1679 block_group = btrfs_lookup_first_block_group(info, hint_byte);
1681 hint_byte = search_start;
1682 block_group = btrfs_find_block_group(root, block_group,
1683 hint_byte, data, 1);
1684 if (last_ptr && *last_ptr == 0 && block_group)
1685 hint_byte = block_group->key.objectid;
1687 block_group = btrfs_find_block_group(root,
1689 search_start, data, 1);
1691 search_start = max(search_start, hint_byte);
1693 total_needed += empty_size;
1697 block_group = btrfs_lookup_first_block_group(info,
1700 block_group = btrfs_lookup_first_block_group(info,
1703 if (full_scan && !chunk_alloc_done) {
1704 if (allowed_chunk_alloc) {
1705 do_chunk_alloc(trans, root,
1706 num_bytes + 2 * 1024 * 1024, data, 1);
1707 allowed_chunk_alloc = 0;
1708 } else if (block_group && block_group_bits(block_group, data)) {
1709 block_group->space_info->force_alloc = 1;
1711 chunk_alloc_done = 1;
1713 ret = find_search_start(root, &block_group, &search_start,
1714 total_needed, data);
1715 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1717 block_group = btrfs_lookup_first_block_group(info,
1719 search_start = orig_search_start;
1720 ret = find_search_start(root, &block_group, &search_start,
1721 total_needed, data);
1728 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1731 empty_size += empty_cluster;
1732 total_needed += empty_size;
1734 block_group = btrfs_lookup_first_block_group(info,
1736 search_start = orig_search_start;
1737 ret = find_search_start(root, &block_group,
1738 &search_start, total_needed, data);
1745 search_start = stripe_align(root, search_start);
1746 ins->objectid = search_start;
1747 ins->offset = num_bytes;
1749 if (ins->objectid + num_bytes >= search_end)
1752 if (ins->objectid + num_bytes >
1753 block_group->key.objectid + block_group->key.offset) {
1754 search_start = block_group->key.objectid +
1755 block_group->key.offset;
1759 if (test_range_bit(&info->extent_ins, ins->objectid,
1760 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1761 search_start = ins->objectid + num_bytes;
1765 if (test_range_bit(&info->pinned_extents, ins->objectid,
1766 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1767 search_start = ins->objectid + num_bytes;
1771 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1772 ins->objectid < exclude_start + exclude_nr)) {
1773 search_start = exclude_start + exclude_nr;
1777 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1778 block_group = btrfs_lookup_block_group(info, ins->objectid);
1780 trans->block_group = block_group;
1782 ins->offset = num_bytes;
1784 *last_ptr = ins->objectid + ins->offset;
1786 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1793 if (search_start + num_bytes >= search_end) {
1795 search_start = orig_search_start;
1802 total_needed -= empty_size;
1807 block_group = btrfs_lookup_first_block_group(info, search_start);
1809 block_group = btrfs_find_block_group(root, block_group,
1810 search_start, data, 0);
1818 * finds a free extent and does all the dirty work required for allocation
1819 * returns the key for the extent through ins, and a tree buffer for
1820 * the first block of the extent through buf.
1822 * returns 0 if everything worked, non-zero otherwise.
1824 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1825 struct btrfs_root *root,
1826 u64 num_bytes, u64 min_alloc_size,
1827 u64 root_objectid, u64 ref_generation,
1828 u64 owner, u64 owner_offset,
1829 u64 empty_size, u64 hint_byte,
1830 u64 search_end, struct btrfs_key *ins, u64 data)
1836 u64 search_start = 0;
1839 struct btrfs_fs_info *info = root->fs_info;
1840 struct btrfs_root *extent_root = info->extent_root;
1841 struct btrfs_extent_item *extent_item;
1842 struct btrfs_extent_ref *ref;
1843 struct btrfs_path *path;
1844 struct btrfs_key keys[2];
1847 alloc_profile = info->avail_data_alloc_bits &
1848 info->data_alloc_profile;
1849 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1850 } else if (root == root->fs_info->chunk_root) {
1851 alloc_profile = info->avail_system_alloc_bits &
1852 info->system_alloc_profile;
1853 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1855 alloc_profile = info->avail_metadata_alloc_bits &
1856 info->metadata_alloc_profile;
1857 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1860 data = reduce_alloc_profile(root, data);
1862 * the only place that sets empty_size is btrfs_realloc_node, which
1863 * is not called recursively on allocations
1865 if (empty_size || root->ref_cows) {
1866 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1867 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1869 BTRFS_BLOCK_GROUP_METADATA |
1870 (info->metadata_alloc_profile &
1871 info->avail_metadata_alloc_bits), 0);
1874 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1875 num_bytes + 2 * 1024 * 1024, data, 0);
1879 WARN_ON(num_bytes < root->sectorsize);
1880 ret = find_free_extent(trans, root, num_bytes, empty_size,
1881 search_start, search_end, hint_byte, ins,
1882 trans->alloc_exclude_start,
1883 trans->alloc_exclude_nr, data);
1885 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1886 num_bytes = num_bytes >> 1;
1887 num_bytes = max(num_bytes, min_alloc_size);
1888 do_chunk_alloc(trans, root->fs_info->extent_root,
1889 num_bytes, data, 1);
1893 printk("allocation failed flags %Lu\n", data);
1899 /* block accounting for super block */
1900 super_used = btrfs_super_bytes_used(&info->super_copy);
1901 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1903 /* block accounting for root item */
1904 root_used = btrfs_root_used(&root->root_item);
1905 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1907 clear_extent_dirty(&root->fs_info->free_space_cache,
1908 ins->objectid, ins->objectid + ins->offset - 1,
1911 if (root == extent_root) {
1912 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1913 ins->objectid + ins->offset - 1,
1914 EXTENT_LOCKED, GFP_NOFS);
1918 WARN_ON(trans->alloc_exclude_nr);
1919 trans->alloc_exclude_start = ins->objectid;
1920 trans->alloc_exclude_nr = ins->offset;
1922 memcpy(&keys[0], ins, sizeof(*ins));
1923 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1924 owner, owner_offset);
1925 keys[1].objectid = ins->objectid;
1926 keys[1].type = BTRFS_EXTENT_REF_KEY;
1927 sizes[0] = sizeof(*extent_item);
1928 sizes[1] = sizeof(*ref);
1930 path = btrfs_alloc_path();
1933 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1937 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1938 struct btrfs_extent_item);
1939 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1940 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1941 struct btrfs_extent_ref);
1943 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1944 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1945 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1946 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1948 btrfs_mark_buffer_dirty(path->nodes[0]);
1950 trans->alloc_exclude_start = 0;
1951 trans->alloc_exclude_nr = 0;
1952 btrfs_free_path(path);
1953 finish_current_insert(trans, extent_root);
1954 pending_ret = del_pending_extents(trans, extent_root);
1964 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1966 printk("update block group failed for %Lu %Lu\n",
1967 ins->objectid, ins->offset);
1974 * helper function to allocate a block for a given tree
1975 * returns the tree buffer or NULL.
1977 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1978 struct btrfs_root *root,
1980 u64 root_objectid, u64 hint,
1986 ref_generation = trans->transid;
1991 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1992 ref_generation, 0, 0, hint, empty_size);
1996 * helper function to allocate a block for a given tree
1997 * returns the tree buffer or NULL.
1999 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2000 struct btrfs_root *root,
2009 struct btrfs_key ins;
2011 struct extent_buffer *buf;
2013 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
2014 root_objectid, ref_generation,
2015 level, first_objectid, empty_size, hint,
2019 return ERR_PTR(ret);
2021 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2023 btrfs_free_extent(trans, root, ins.objectid, blocksize,
2024 root->root_key.objectid, ref_generation,
2026 return ERR_PTR(-ENOMEM);
2028 btrfs_set_header_generation(buf, trans->transid);
2029 clean_tree_block(trans, root, buf);
2030 btrfs_set_buffer_uptodate(buf);
2032 if (PageDirty(buf->first_page)) {
2033 printk("page %lu dirty\n", buf->first_page->index);
2037 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2038 buf->start + buf->len - 1, GFP_NOFS);
2039 if (!btrfs_test_opt(root, SSD))
2040 btrfs_set_buffer_defrag(buf);
2041 trans->blocks_used++;
2045 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2046 struct btrfs_root *root,
2047 struct extent_buffer *leaf)
2050 u64 leaf_generation;
2051 struct btrfs_key key;
2052 struct btrfs_file_extent_item *fi;
2057 BUG_ON(!btrfs_is_leaf(leaf));
2058 nritems = btrfs_header_nritems(leaf);
2059 leaf_owner = btrfs_header_owner(leaf);
2060 leaf_generation = btrfs_header_generation(leaf);
2062 for (i = 0; i < nritems; i++) {
2065 btrfs_item_key_to_cpu(leaf, &key, i);
2066 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2068 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2069 if (btrfs_file_extent_type(leaf, fi) ==
2070 BTRFS_FILE_EXTENT_INLINE)
2073 * FIXME make sure to insert a trans record that
2074 * repeats the snapshot del on crash
2076 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2077 if (disk_bytenr == 0)
2079 ret = btrfs_free_extent(trans, root, disk_bytenr,
2080 btrfs_file_extent_disk_num_bytes(leaf, fi),
2081 leaf_owner, leaf_generation,
2082 key.objectid, key.offset, 0);
2088 static void noinline reada_walk_down(struct btrfs_root *root,
2089 struct extent_buffer *node,
2102 nritems = btrfs_header_nritems(node);
2103 level = btrfs_header_level(node);
2107 for (i = slot; i < nritems && skipped < 32; i++) {
2108 bytenr = btrfs_node_blockptr(node, i);
2109 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2110 (last > bytenr && last - bytenr > 32 * 1024))) {
2114 blocksize = btrfs_level_size(root, level - 1);
2116 ret = lookup_extent_ref(NULL, root, bytenr,
2124 mutex_unlock(&root->fs_info->fs_mutex);
2125 ret = readahead_tree_block(root, bytenr, blocksize,
2126 btrfs_node_ptr_generation(node, i));
2127 last = bytenr + blocksize;
2129 mutex_lock(&root->fs_info->fs_mutex);
2136 * helper function for drop_snapshot, this walks down the tree dropping ref
2137 * counts as it goes.
2139 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2140 struct btrfs_root *root,
2141 struct btrfs_path *path, int *level)
2147 struct extent_buffer *next;
2148 struct extent_buffer *cur;
2149 struct extent_buffer *parent;
2154 WARN_ON(*level < 0);
2155 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2156 ret = lookup_extent_ref(trans, root,
2157 path->nodes[*level]->start,
2158 path->nodes[*level]->len, &refs);
2164 * walk down to the last node level and free all the leaves
2166 while(*level >= 0) {
2167 WARN_ON(*level < 0);
2168 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2169 cur = path->nodes[*level];
2171 if (btrfs_header_level(cur) != *level)
2174 if (path->slots[*level] >=
2175 btrfs_header_nritems(cur))
2178 ret = drop_leaf_ref(trans, root, cur);
2182 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2183 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2184 blocksize = btrfs_level_size(root, *level - 1);
2185 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2188 parent = path->nodes[*level];
2189 root_owner = btrfs_header_owner(parent);
2190 root_gen = btrfs_header_generation(parent);
2191 path->slots[*level]++;
2192 ret = btrfs_free_extent(trans, root, bytenr,
2193 blocksize, root_owner,
2198 next = btrfs_find_tree_block(root, bytenr, blocksize);
2199 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2200 free_extent_buffer(next);
2201 reada_walk_down(root, cur, path->slots[*level]);
2203 mutex_unlock(&root->fs_info->fs_mutex);
2204 next = read_tree_block(root, bytenr, blocksize,
2206 mutex_lock(&root->fs_info->fs_mutex);
2208 /* we've dropped the lock, double check */
2209 ret = lookup_extent_ref(trans, root, bytenr,
2213 parent = path->nodes[*level];
2214 root_owner = btrfs_header_owner(parent);
2215 root_gen = btrfs_header_generation(parent);
2217 path->slots[*level]++;
2218 free_extent_buffer(next);
2219 ret = btrfs_free_extent(trans, root, bytenr,
2227 WARN_ON(*level <= 0);
2228 if (path->nodes[*level-1])
2229 free_extent_buffer(path->nodes[*level-1]);
2230 path->nodes[*level-1] = next;
2231 *level = btrfs_header_level(next);
2232 path->slots[*level] = 0;
2235 WARN_ON(*level < 0);
2236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2238 if (path->nodes[*level] == root->node) {
2239 root_owner = root->root_key.objectid;
2240 parent = path->nodes[*level];
2242 parent = path->nodes[*level + 1];
2243 root_owner = btrfs_header_owner(parent);
2246 root_gen = btrfs_header_generation(parent);
2247 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2248 path->nodes[*level]->len,
2249 root_owner, root_gen, 0, 0, 1);
2250 free_extent_buffer(path->nodes[*level]);
2251 path->nodes[*level] = NULL;
2258 * helper for dropping snapshots. This walks back up the tree in the path
2259 * to find the first node higher up where we haven't yet gone through
2262 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2263 struct btrfs_root *root,
2264 struct btrfs_path *path, int *level)
2268 struct btrfs_root_item *root_item = &root->root_item;
2273 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2274 slot = path->slots[i];
2275 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2276 struct extent_buffer *node;
2277 struct btrfs_disk_key disk_key;
2278 node = path->nodes[i];
2281 WARN_ON(*level == 0);
2282 btrfs_node_key(node, &disk_key, path->slots[i]);
2283 memcpy(&root_item->drop_progress,
2284 &disk_key, sizeof(disk_key));
2285 root_item->drop_level = i;
2288 if (path->nodes[*level] == root->node) {
2289 root_owner = root->root_key.objectid;
2291 btrfs_header_generation(path->nodes[*level]);
2293 struct extent_buffer *node;
2294 node = path->nodes[*level + 1];
2295 root_owner = btrfs_header_owner(node);
2296 root_gen = btrfs_header_generation(node);
2298 ret = btrfs_free_extent(trans, root,
2299 path->nodes[*level]->start,
2300 path->nodes[*level]->len,
2301 root_owner, root_gen, 0, 0, 1);
2303 free_extent_buffer(path->nodes[*level]);
2304 path->nodes[*level] = NULL;
2312 * drop the reference count on the tree rooted at 'snap'. This traverses
2313 * the tree freeing any blocks that have a ref count of zero after being
2316 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2322 struct btrfs_path *path;
2325 struct btrfs_root_item *root_item = &root->root_item;
2327 path = btrfs_alloc_path();
2330 level = btrfs_header_level(root->node);
2332 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2333 path->nodes[level] = root->node;
2334 extent_buffer_get(root->node);
2335 path->slots[level] = 0;
2337 struct btrfs_key key;
2338 struct btrfs_disk_key found_key;
2339 struct extent_buffer *node;
2341 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2342 level = root_item->drop_level;
2343 path->lowest_level = level;
2344 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2349 node = path->nodes[level];
2350 btrfs_node_key(node, &found_key, path->slots[level]);
2351 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2352 sizeof(found_key)));
2355 wret = walk_down_tree(trans, root, path, &level);
2361 wret = walk_up_tree(trans, root, path, &level);
2369 for (i = 0; i <= orig_level; i++) {
2370 if (path->nodes[i]) {
2371 free_extent_buffer(path->nodes[i]);
2372 path->nodes[i] = NULL;
2376 btrfs_free_path(path);
2380 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2387 ret = find_first_extent_bit(&info->block_group_cache, 0,
2388 &start, &end, (unsigned int)-1);
2391 ret = get_state_private(&info->block_group_cache, start, &ptr);
2393 kfree((void *)(unsigned long)ptr);
2394 clear_extent_bits(&info->block_group_cache, start,
2395 end, (unsigned int)-1, GFP_NOFS);
2398 ret = find_first_extent_bit(&info->free_space_cache, 0,
2399 &start, &end, EXTENT_DIRTY);
2402 clear_extent_dirty(&info->free_space_cache, start,
2408 static unsigned long calc_ra(unsigned long start, unsigned long last,
2411 return min(last, start + nr - 1);
2414 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2419 unsigned long last_index;
2422 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2423 struct file_ra_state *ra;
2424 unsigned long total_read = 0;
2425 unsigned long ra_pages;
2426 struct btrfs_trans_handle *trans;
2428 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2430 mutex_lock(&inode->i_mutex);
2431 i = start >> PAGE_CACHE_SHIFT;
2432 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2434 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2436 file_ra_state_init(ra, inode->i_mapping);
2438 for (; i <= last_index; i++) {
2439 if (total_read % ra_pages == 0) {
2440 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2441 calc_ra(i, last_index, ra_pages));
2444 if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size)
2445 goto truncate_racing;
2447 page = grab_cache_page(inode->i_mapping, i);
2451 if (!PageUptodate(page)) {
2452 btrfs_readpage(NULL, page);
2454 if (!PageUptodate(page)) {
2456 page_cache_release(page);
2460 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2461 ClearPageDirty(page);
2463 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2465 wait_on_page_writeback(page);
2466 set_page_extent_mapped(page);
2467 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2468 page_end = page_start + PAGE_CACHE_SIZE - 1;
2470 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2472 set_extent_delalloc(io_tree, page_start,
2473 page_end, GFP_NOFS);
2474 set_page_dirty(page);
2476 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2478 page_cache_release(page);
2480 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2485 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2487 btrfs_add_ordered_inode(inode);
2488 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2489 mark_inode_dirty(inode);
2491 mutex_unlock(&inode->i_mutex);
2495 vmtruncate(inode, inode->i_size);
2496 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2502 * The back references tell us which tree holds a ref on a block,
2503 * but it is possible for the tree root field in the reference to
2504 * reflect the original root before a snapshot was made. In this
2505 * case we should search through all the children of a given root
2506 * to find potential holders of references on a block.
2508 * Instead, we do something a little less fancy and just search
2509 * all the roots for a given key/block combination.
2511 static int find_root_for_ref(struct btrfs_root *root,
2512 struct btrfs_path *path,
2513 struct btrfs_key *key0,
2516 struct btrfs_root **found_root,
2519 struct btrfs_key root_location;
2520 struct btrfs_root *cur_root = *found_root;
2521 struct btrfs_file_extent_item *file_extent;
2522 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
2527 root_location.offset = (u64)-1;
2528 root_location.type = BTRFS_ROOT_ITEM_KEY;
2529 path->lowest_level = level;
2532 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
2534 if (ret == 0 && file_key) {
2535 struct extent_buffer *leaf = path->nodes[0];
2536 file_extent = btrfs_item_ptr(leaf, path->slots[0],
2537 struct btrfs_file_extent_item);
2538 if (btrfs_file_extent_type(leaf, file_extent) ==
2539 BTRFS_FILE_EXTENT_REG) {
2541 btrfs_file_extent_disk_bytenr(leaf,
2544 } else if (!file_key) {
2545 if (path->nodes[level])
2546 found_bytenr = path->nodes[level]->start;
2549 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2550 if (!path->nodes[i])
2552 free_extent_buffer(path->nodes[i]);
2553 path->nodes[i] = NULL;
2555 btrfs_release_path(cur_root, path);
2557 if (found_bytenr == bytenr) {
2558 *found_root = cur_root;
2562 ret = btrfs_search_root(root->fs_info->tree_root,
2563 root_search_start, &root_search_start);
2567 root_location.objectid = root_search_start;
2568 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
2576 path->lowest_level = 0;
2581 * note, this releases the path
2583 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2584 struct btrfs_path *path,
2585 struct btrfs_key *extent_key,
2586 u64 *last_file_objectid,
2587 u64 *last_file_offset,
2588 u64 *last_file_root,
2591 struct inode *inode;
2592 struct btrfs_root *found_root;
2593 struct btrfs_key root_location;
2594 struct btrfs_key found_key;
2595 struct btrfs_extent_ref *ref;
2603 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2604 struct btrfs_extent_ref);
2605 ref_root = btrfs_ref_root(path->nodes[0], ref);
2606 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2607 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2608 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2609 btrfs_release_path(extent_root, path);
2611 root_location.objectid = ref_root;
2613 root_location.offset = 0;
2615 root_location.offset = (u64)-1;
2616 root_location.type = BTRFS_ROOT_ITEM_KEY;
2618 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2620 BUG_ON(!found_root);
2622 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2623 found_key.objectid = ref_objectid;
2624 found_key.type = BTRFS_EXTENT_DATA_KEY;
2625 found_key.offset = ref_offset;
2628 if (last_extent == extent_key->objectid &&
2629 *last_file_objectid == ref_objectid &&
2630 *last_file_offset == ref_offset &&
2631 *last_file_root == ref_root)
2634 ret = find_root_for_ref(extent_root, path, &found_key,
2635 level, 1, &found_root,
2636 extent_key->objectid);
2641 if (last_extent == extent_key->objectid &&
2642 *last_file_objectid == ref_objectid &&
2643 *last_file_offset == ref_offset &&
2644 *last_file_root == ref_root)
2647 mutex_unlock(&extent_root->fs_info->fs_mutex);
2648 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2649 ref_objectid, found_root);
2650 if (inode->i_state & I_NEW) {
2651 /* the inode and parent dir are two different roots */
2652 BTRFS_I(inode)->root = found_root;
2653 BTRFS_I(inode)->location.objectid = ref_objectid;
2654 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2655 BTRFS_I(inode)->location.offset = 0;
2656 btrfs_read_locked_inode(inode);
2657 unlock_new_inode(inode);
2660 /* this can happen if the reference is not against
2661 * the latest version of the tree root
2663 if (is_bad_inode(inode)) {
2664 mutex_lock(&extent_root->fs_info->fs_mutex);
2667 *last_file_objectid = inode->i_ino;
2668 *last_file_root = found_root->root_key.objectid;
2669 *last_file_offset = ref_offset;
2671 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2673 mutex_lock(&extent_root->fs_info->fs_mutex);
2675 struct btrfs_trans_handle *trans;
2676 struct extent_buffer *eb;
2679 eb = read_tree_block(found_root, extent_key->objectid,
2680 extent_key->offset, 0);
2681 level = btrfs_header_level(eb);
2684 btrfs_item_key_to_cpu(eb, &found_key, 0);
2686 btrfs_node_key_to_cpu(eb, &found_key, 0);
2688 free_extent_buffer(eb);
2690 ret = find_root_for_ref(extent_root, path, &found_key,
2691 level, 0, &found_root,
2692 extent_key->objectid);
2697 trans = btrfs_start_transaction(found_root, 1);
2699 path->lowest_level = level;
2701 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2703 path->lowest_level = 0;
2704 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2705 if (!path->nodes[i])
2707 free_extent_buffer(path->nodes[i]);
2708 path->nodes[i] = NULL;
2710 btrfs_release_path(found_root, path);
2711 if (found_root == found_root->fs_info->extent_root)
2712 btrfs_extent_post_op(trans, found_root);
2713 btrfs_end_transaction(trans, found_root);
2720 static int noinline del_extent_zero(struct btrfs_root *extent_root,
2721 struct btrfs_path *path,
2722 struct btrfs_key *extent_key)
2725 struct btrfs_trans_handle *trans;
2727 trans = btrfs_start_transaction(extent_root, 1);
2728 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
2735 ret = btrfs_del_item(trans, extent_root, path);
2737 btrfs_end_transaction(trans, extent_root);
2741 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2742 struct btrfs_path *path,
2743 struct btrfs_key *extent_key)
2745 struct btrfs_key key;
2746 struct btrfs_key found_key;
2747 struct extent_buffer *leaf;
2748 u64 last_file_objectid = 0;
2749 u64 last_file_root = 0;
2750 u64 last_file_offset = (u64)-1;
2751 u64 last_extent = 0;
2756 if (extent_key->objectid == 0) {
2757 ret = del_extent_zero(extent_root, path, extent_key);
2760 key.objectid = extent_key->objectid;
2761 key.type = BTRFS_EXTENT_REF_KEY;
2765 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2771 leaf = path->nodes[0];
2772 nritems = btrfs_header_nritems(leaf);
2773 if (path->slots[0] == nritems) {
2774 ret = btrfs_next_leaf(extent_root, path);
2781 leaf = path->nodes[0];
2784 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2785 if (found_key.objectid != extent_key->objectid) {
2789 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
2793 key.offset = found_key.offset + 1;
2794 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2796 ret = relocate_one_reference(extent_root, path, extent_key,
2797 &last_file_objectid,
2799 &last_file_root, last_extent);
2802 last_extent = extent_key->objectid;
2806 btrfs_release_path(extent_root, path);
2810 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2813 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2814 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2816 num_devices = root->fs_info->fs_devices->num_devices;
2817 if (num_devices == 1) {
2818 stripped |= BTRFS_BLOCK_GROUP_DUP;
2819 stripped = flags & ~stripped;
2821 /* turn raid0 into single device chunks */
2822 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2825 /* turn mirroring into duplication */
2826 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2827 BTRFS_BLOCK_GROUP_RAID10))
2828 return stripped | BTRFS_BLOCK_GROUP_DUP;
2831 /* they already had raid on here, just return */
2832 if (flags & stripped)
2835 stripped |= BTRFS_BLOCK_GROUP_DUP;
2836 stripped = flags & ~stripped;
2838 /* switch duplicated blocks with raid1 */
2839 if (flags & BTRFS_BLOCK_GROUP_DUP)
2840 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2842 /* turn single device chunks into raid0 */
2843 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2848 int __alloc_chunk_for_shrink(struct btrfs_root *root,
2849 struct btrfs_block_group_cache *shrink_block_group,
2852 struct btrfs_trans_handle *trans;
2853 u64 new_alloc_flags;
2856 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2858 trans = btrfs_start_transaction(root, 1);
2859 new_alloc_flags = update_block_group_flags(root,
2860 shrink_block_group->flags);
2861 if (new_alloc_flags != shrink_block_group->flags) {
2863 btrfs_block_group_used(&shrink_block_group->item);
2865 calc = shrink_block_group->key.offset;
2867 do_chunk_alloc(trans, root->fs_info->extent_root,
2868 calc + 2 * 1024 * 1024, new_alloc_flags, force);
2869 btrfs_end_transaction(trans, root);
2874 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
2876 struct btrfs_trans_handle *trans;
2877 struct btrfs_root *tree_root = root->fs_info->tree_root;
2878 struct btrfs_path *path;
2881 u64 shrink_last_byte;
2882 struct btrfs_block_group_cache *shrink_block_group;
2883 struct btrfs_fs_info *info = root->fs_info;
2884 struct btrfs_key key;
2885 struct btrfs_key found_key;
2886 struct extent_buffer *leaf;
2891 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2893 BUG_ON(!shrink_block_group);
2895 shrink_last_byte = shrink_block_group->key.objectid +
2896 shrink_block_group->key.offset;
2898 shrink_block_group->space_info->total_bytes -=
2899 shrink_block_group->key.offset;
2900 path = btrfs_alloc_path();
2901 root = root->fs_info->extent_root;
2904 printk("btrfs relocating block group %llu flags %llu\n",
2905 (unsigned long long)shrink_start,
2906 (unsigned long long)shrink_block_group->flags);
2908 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
2912 shrink_block_group->ro = 1;
2916 key.objectid = shrink_start;
2919 cur_byte = key.objectid;
2921 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2925 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
2930 leaf = path->nodes[0];
2931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2932 if (found_key.objectid + found_key.offset > shrink_start &&
2933 found_key.objectid < shrink_last_byte) {
2934 cur_byte = found_key.objectid;
2935 key.objectid = cur_byte;
2938 btrfs_release_path(root, path);
2941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2945 leaf = path->nodes[0];
2946 nritems = btrfs_header_nritems(leaf);
2948 if (path->slots[0] >= nritems) {
2949 ret = btrfs_next_leaf(root, path);
2956 leaf = path->nodes[0];
2957 nritems = btrfs_header_nritems(leaf);
2960 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2962 if (found_key.objectid >= shrink_last_byte)
2965 if (progress && need_resched()) {
2966 memcpy(&key, &found_key, sizeof(key));
2967 mutex_unlock(&root->fs_info->fs_mutex);
2969 mutex_lock(&root->fs_info->fs_mutex);
2970 btrfs_release_path(root, path);
2971 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2977 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2978 found_key.objectid + found_key.offset <= cur_byte) {
2979 memcpy(&key, &found_key, sizeof(key));
2986 cur_byte = found_key.objectid + found_key.offset;
2987 key.objectid = cur_byte;
2988 btrfs_release_path(root, path);
2989 ret = relocate_one_extent(root, path, &found_key);
2990 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
2993 btrfs_release_path(root, path);
2995 if (total_found > 0) {
2996 printk("btrfs relocate found %llu last extent was %llu\n",
2997 (unsigned long long)total_found,
2998 (unsigned long long)found_key.objectid);
2999 trans = btrfs_start_transaction(tree_root, 1);
3000 btrfs_commit_transaction(trans, tree_root);
3002 mutex_unlock(&root->fs_info->fs_mutex);
3003 btrfs_clean_old_snapshots(tree_root);
3004 mutex_lock(&root->fs_info->fs_mutex);
3006 trans = btrfs_start_transaction(tree_root, 1);
3007 btrfs_commit_transaction(trans, tree_root);
3012 * we've freed all the extents, now remove the block
3013 * group item from the tree
3015 trans = btrfs_start_transaction(root, 1);
3016 memcpy(&key, &shrink_block_group->key, sizeof(key));
3018 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3024 clear_extent_bits(&info->block_group_cache, key.objectid,
3025 key.objectid + key.offset - 1,
3026 (unsigned int)-1, GFP_NOFS);
3029 clear_extent_bits(&info->free_space_cache,
3030 key.objectid, key.objectid + key.offset - 1,
3031 (unsigned int)-1, GFP_NOFS);
3033 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3034 kfree(shrink_block_group);
3036 btrfs_del_item(trans, root, path);
3037 btrfs_commit_transaction(trans, root);
3039 /* the code to unpin extents might set a few bits in the free
3040 * space cache for this range again
3042 clear_extent_bits(&info->free_space_cache,
3043 key.objectid, key.objectid + key.offset - 1,
3044 (unsigned int)-1, GFP_NOFS);
3046 btrfs_free_path(path);
3050 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3051 struct btrfs_key *key)
3054 struct btrfs_key found_key;
3055 struct extent_buffer *leaf;
3058 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3062 slot = path->slots[0];
3063 leaf = path->nodes[0];
3064 if (slot >= btrfs_header_nritems(leaf)) {
3065 ret = btrfs_next_leaf(root, path);
3072 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3074 if (found_key.objectid >= key->objectid &&
3075 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3084 int btrfs_read_block_groups(struct btrfs_root *root)
3086 struct btrfs_path *path;
3089 struct btrfs_block_group_cache *cache;
3090 struct btrfs_fs_info *info = root->fs_info;
3091 struct btrfs_space_info *space_info;
3092 struct extent_io_tree *block_group_cache;
3093 struct btrfs_key key;
3094 struct btrfs_key found_key;
3095 struct extent_buffer *leaf;
3097 block_group_cache = &info->block_group_cache;
3098 root = info->extent_root;
3101 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3102 path = btrfs_alloc_path();
3107 ret = find_first_block_group(root, path, &key);
3115 leaf = path->nodes[0];
3116 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3117 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3123 read_extent_buffer(leaf, &cache->item,
3124 btrfs_item_ptr_offset(leaf, path->slots[0]),
3125 sizeof(cache->item));
3126 memcpy(&cache->key, &found_key, sizeof(found_key));
3128 key.objectid = found_key.objectid + found_key.offset;
3129 btrfs_release_path(root, path);
3130 cache->flags = btrfs_block_group_flags(&cache->item);
3132 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3133 bit = BLOCK_GROUP_DATA;
3134 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3135 bit = BLOCK_GROUP_SYSTEM;
3136 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3137 bit = BLOCK_GROUP_METADATA;
3139 set_avail_alloc_bits(info, cache->flags);
3141 ret = update_space_info(info, cache->flags, found_key.offset,
3142 btrfs_block_group_used(&cache->item),
3145 cache->space_info = space_info;
3147 /* use EXTENT_LOCKED to prevent merging */
3148 set_extent_bits(block_group_cache, found_key.objectid,
3149 found_key.objectid + found_key.offset - 1,
3150 bit | EXTENT_LOCKED, GFP_NOFS);
3151 set_state_private(block_group_cache, found_key.objectid,
3152 (unsigned long)cache);
3155 btrfs_super_total_bytes(&info->super_copy))
3160 btrfs_free_path(path);
3164 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3165 struct btrfs_root *root, u64 bytes_used,
3166 u64 type, u64 chunk_objectid, u64 chunk_offset,
3171 struct btrfs_root *extent_root;
3172 struct btrfs_block_group_cache *cache;
3173 struct extent_io_tree *block_group_cache;
3175 extent_root = root->fs_info->extent_root;
3176 block_group_cache = &root->fs_info->block_group_cache;
3178 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3180 cache->key.objectid = chunk_offset;
3181 cache->key.offset = size;
3182 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3184 btrfs_set_block_group_used(&cache->item, bytes_used);
3185 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3186 cache->flags = type;
3187 btrfs_set_block_group_flags(&cache->item, type);
3189 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3190 &cache->space_info);
3193 bit = block_group_state_bits(type);
3194 set_extent_bits(block_group_cache, chunk_offset,
3195 chunk_offset + size - 1,
3196 bit | EXTENT_LOCKED, GFP_NOFS);
3198 set_state_private(block_group_cache, chunk_offset,
3199 (unsigned long)cache);
3200 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3201 sizeof(cache->item));
3204 finish_current_insert(trans, extent_root);
3205 ret = del_pending_extents(trans, extent_root);
3207 set_avail_alloc_bits(extent_root->fs_info, type);