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.
19 #include <linux/sched.h>
20 #include <linux/crc32c.h>
21 #include <linux/pagemap.h>
25 #include "print-tree.h"
26 #include "transaction.h"
28 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
29 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
30 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
32 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
34 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
36 static int find_previous_extent(struct btrfs_root *root,
37 struct btrfs_path *path)
39 struct btrfs_key found_key;
40 struct extent_buffer *leaf;
44 if (path->slots[0] == 0) {
45 ret = btrfs_prev_leaf(root, path);
51 leaf = path->nodes[0];
52 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
53 if (found_key.type == BTRFS_EXTENT_ITEM_KEY)
59 static int cache_block_group(struct btrfs_root *root,
60 struct btrfs_block_group_cache *block_group)
62 struct btrfs_path *path;
65 struct extent_buffer *leaf;
66 struct extent_map_tree *free_space_cache;
76 root = root->fs_info->extent_root;
77 free_space_cache = &root->fs_info->free_space_cache;
79 if (block_group->cached)
82 path = btrfs_alloc_path();
87 first_free = block_group->key.objectid;
88 key.objectid = block_group->key.objectid;
90 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
91 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
94 ret = find_previous_extent(root, path);
98 leaf = path->nodes[0];
99 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
100 if (key.objectid + key.offset > first_free)
101 first_free = key.objectid + key.offset;
104 leaf = path->nodes[0];
105 slot = path->slots[0];
106 if (slot >= btrfs_header_nritems(leaf)) {
107 ret = btrfs_next_leaf(root, path);
116 btrfs_item_key_to_cpu(leaf, &key, slot);
117 if (key.objectid < block_group->key.objectid) {
120 if (key.objectid >= block_group->key.objectid +
121 block_group->key.offset) {
125 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
130 if (key.objectid > last) {
131 hole_size = key.objectid - last;
132 set_extent_dirty(free_space_cache, last,
133 last + hole_size - 1,
136 last = key.objectid + key.offset;
144 if (block_group->key.objectid +
145 block_group->key.offset > last) {
146 hole_size = block_group->key.objectid +
147 block_group->key.offset - last;
148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1, GFP_NOFS);
151 block_group->cached = 1;
153 btrfs_free_path(path);
157 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
161 struct extent_map_tree *block_group_cache;
162 struct btrfs_block_group_cache *block_group = NULL;
168 block_group_cache = &info->block_group_cache;
169 ret = find_first_extent_bit(block_group_cache,
170 bytenr, &start, &end,
171 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
175 ret = get_state_private(block_group_cache, start, &ptr);
179 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
180 if (block_group->key.objectid <= bytenr && bytenr <
181 block_group->key.objectid + block_group->key.offset)
185 static u64 noinline find_search_start(struct btrfs_root *root,
186 struct btrfs_block_group_cache **cache_ret,
187 u64 search_start, int num, int data)
190 struct btrfs_block_group_cache *cache = *cache_ret;
201 ret = cache_block_group(root, cache);
205 last = max(search_start, cache->key.objectid);
208 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
209 last, &start, &end, EXTENT_DIRTY);
216 start = max(last, start);
218 if (last - start < num) {
219 if (last == cache->key.objectid + cache->key.offset)
223 if (data != BTRFS_BLOCK_GROUP_MIXED &&
224 start + num > cache->key.objectid + cache->key.offset)
229 cache = btrfs_lookup_block_group(root->fs_info, search_start);
231 printk("Unable to find block group for %Lu\n",
239 last = cache->key.objectid + cache->key.offset;
241 cache = btrfs_lookup_block_group(root->fs_info, last);
247 data = BTRFS_BLOCK_GROUP_MIXED;
252 if (cache_miss && !cache->cached) {
253 cache_block_group(root, cache);
255 cache = btrfs_lookup_block_group(root->fs_info, last);
257 cache = btrfs_find_block_group(root, cache, last, data, 0);
265 static u64 div_factor(u64 num, int factor)
274 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
275 struct btrfs_block_group_cache
276 *hint, u64 search_start,
279 struct btrfs_block_group_cache *cache;
280 struct extent_map_tree *block_group_cache;
281 struct btrfs_block_group_cache *found_group = NULL;
282 struct btrfs_fs_info *info = root->fs_info;
296 block_group_cache = &info->block_group_cache;
301 if (data == BTRFS_BLOCK_GROUP_MIXED) {
302 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
305 bit = BLOCK_GROUP_DATA;
307 bit = BLOCK_GROUP_METADATA;
310 struct btrfs_block_group_cache *shint;
311 shint = btrfs_lookup_block_group(info, search_start);
312 if (shint && (shint->data == data ||
313 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
314 used = btrfs_block_group_used(&shint->item);
315 if (used + shint->pinned <
316 div_factor(shint->key.offset, factor)) {
321 if (hint && (hint->data == data ||
322 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
323 used = btrfs_block_group_used(&hint->item);
324 if (used + hint->pinned <
325 div_factor(hint->key.offset, factor)) {
328 last = hint->key.objectid + hint->key.offset;
332 hint_last = max(hint->key.objectid, search_start);
334 hint_last = search_start;
340 ret = find_first_extent_bit(block_group_cache, last,
345 ret = get_state_private(block_group_cache, start, &ptr);
349 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
350 last = cache->key.objectid + cache->key.offset;
351 used = btrfs_block_group_used(&cache->item);
354 free_check = cache->key.offset;
356 free_check = div_factor(cache->key.offset, factor);
357 if (used + cache->pinned < free_check) {
370 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
378 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
379 u64 owner, u64 owner_offset)
381 u32 high_crc = ~(u32)0;
382 u32 low_crc = ~(u32)0;
385 lenum = cpu_to_le64(root_objectid);
386 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
387 lenum = cpu_to_le64(ref_generation);
388 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
391 lenum = cpu_to_le64(owner);
392 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
393 lenum = cpu_to_le64(owner_offset);
394 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
396 return ((u64)high_crc << 32) | (u64)low_crc;
399 static int match_extent_ref(struct extent_buffer *leaf,
400 struct btrfs_extent_ref *disk_ref,
401 struct btrfs_extent_ref *cpu_ref)
406 if (cpu_ref->objectid)
407 len = sizeof(*cpu_ref);
409 len = 2 * sizeof(u64);
410 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
415 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
416 struct btrfs_root *root,
417 struct btrfs_path *path, u64 bytenr,
419 u64 ref_generation, u64 owner,
420 u64 owner_offset, int del)
423 struct btrfs_key key;
424 struct btrfs_key found_key;
425 struct btrfs_extent_ref ref;
426 struct extent_buffer *leaf;
427 struct btrfs_extent_ref *disk_ref;
431 btrfs_set_stack_ref_root(&ref, root_objectid);
432 btrfs_set_stack_ref_generation(&ref, ref_generation);
433 btrfs_set_stack_ref_objectid(&ref, owner);
434 btrfs_set_stack_ref_offset(&ref, owner_offset);
436 hash = hash_extent_ref(root_objectid, ref_generation, owner,
439 key.objectid = bytenr;
440 key.type = BTRFS_EXTENT_REF_KEY;
443 ret = btrfs_search_slot(trans, root, &key, path,
447 leaf = path->nodes[0];
449 u32 nritems = btrfs_header_nritems(leaf);
450 if (path->slots[0] >= nritems) {
451 ret2 = btrfs_next_leaf(root, path);
454 leaf = path->nodes[0];
456 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
457 if (found_key.objectid != bytenr ||
458 found_key.type != BTRFS_EXTENT_REF_KEY)
460 key.offset = found_key.offset;
462 btrfs_release_path(root, path);
466 disk_ref = btrfs_item_ptr(path->nodes[0],
468 struct btrfs_extent_ref);
469 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
473 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
474 key.offset = found_key.offset + 1;
475 btrfs_release_path(root, path);
482 * Back reference rules. Back refs have three main goals:
484 * 1) differentiate between all holders of references to an extent so that
485 * when a reference is dropped we can make sure it was a valid reference
486 * before freeing the extent.
488 * 2) Provide enough information to quickly find the holders of an extent
489 * if we notice a given block is corrupted or bad.
491 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
492 * maintenance. This is actually the same as #2, but with a slightly
493 * different use case.
495 * File extents can be referenced by:
497 * - multiple snapshots, subvolumes, or different generations in one subvol
498 * - different files inside a single subvolume (in theory, not implemented yet)
499 * - different offsets inside a file (bookend extents in file.c)
501 * The extent ref structure has fields for:
503 * - Objectid of the subvolume root
504 * - Generation number of the tree holding the reference
505 * - objectid of the file holding the reference
506 * - offset in the file corresponding to the key holding the reference
508 * When a file extent is allocated the fields are filled in:
509 * (root_key.objectid, trans->transid, inode objectid, offset in file)
511 * When a leaf is cow'd new references are added for every file extent found
512 * in the leaf. It looks the same as the create case, but trans->transid
513 * will be different when the block is cow'd.
515 * (root_key.objectid, trans->transid, inode objectid, offset in file)
517 * When a file extent is removed either during snapshot deletion or file
518 * truncation, the corresponding back reference is found
521 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
522 * inode objectid, offset in file)
524 * Btree extents can be referenced by:
526 * - Different subvolumes
527 * - Different generations of the same subvolume
529 * Storing sufficient information for a full reverse mapping of a btree
530 * block would require storing the lowest key of the block in the backref,
531 * and it would require updating that lowest key either before write out or
532 * every time it changed. Instead, the objectid of the lowest key is stored
533 * along with the level of the tree block. This provides a hint
534 * about where in the btree the block can be found. Searches through the
535 * btree only need to look for a pointer to that block, so they stop one
536 * level higher than the level recorded in the backref.
538 * Some btrees do not do reference counting on their extents. These
539 * include the extent tree and the tree of tree roots. Backrefs for these
540 * trees always have a generation of zero.
542 * When a tree block is created, back references are inserted:
544 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
546 * When a tree block is cow'd in a reference counted root,
547 * new back references are added for all the blocks it points to.
548 * These are of the form (trans->transid will have increased since creation):
550 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
552 * Because the lowest_key_objectid and the level are just hints
553 * they are not used when backrefs are deleted. When a backref is deleted:
555 * if backref was for a tree root:
556 * root_objectid = root->root_key.objectid
558 * root_objectid = btrfs_header_owner(parent)
560 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
562 * Back Reference Key hashing:
564 * Back references have four fields, each 64 bits long. Unfortunately,
565 * This is hashed into a single 64 bit number and placed into the key offset.
566 * The key objectid corresponds to the first byte in the extent, and the
567 * key type is set to BTRFS_EXTENT_REF_KEY
569 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 struct btrfs_path *path, u64 bytenr,
572 u64 root_objectid, u64 ref_generation,
573 u64 owner, u64 owner_offset)
576 struct btrfs_key key;
577 struct btrfs_extent_ref ref;
578 struct btrfs_extent_ref *disk_ref;
581 btrfs_set_stack_ref_root(&ref, root_objectid);
582 btrfs_set_stack_ref_generation(&ref, ref_generation);
583 btrfs_set_stack_ref_objectid(&ref, owner);
584 btrfs_set_stack_ref_offset(&ref, owner_offset);
586 hash = hash_extent_ref(root_objectid, ref_generation, owner,
589 key.objectid = bytenr;
590 key.type = BTRFS_EXTENT_REF_KEY;
592 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
593 while (ret == -EEXIST) {
594 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
595 struct btrfs_extent_ref);
596 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
599 btrfs_release_path(root, path);
600 ret = btrfs_insert_empty_item(trans, root, path, &key,
605 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
606 struct btrfs_extent_ref);
607 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
609 btrfs_mark_buffer_dirty(path->nodes[0]);
611 btrfs_release_path(root, path);
615 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
616 struct btrfs_root *root,
617 u64 bytenr, u64 num_bytes,
618 u64 root_objectid, u64 ref_generation,
619 u64 owner, u64 owner_offset)
621 struct btrfs_path *path;
623 struct btrfs_key key;
624 struct extent_buffer *l;
625 struct btrfs_extent_item *item;
628 WARN_ON(num_bytes < root->sectorsize);
629 path = btrfs_alloc_path();
634 key.objectid = bytenr;
635 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
636 key.offset = num_bytes;
637 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
646 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
647 refs = btrfs_extent_refs(l, item);
648 btrfs_set_extent_refs(l, item, refs + 1);
649 btrfs_mark_buffer_dirty(path->nodes[0]);
651 btrfs_release_path(root->fs_info->extent_root, path);
654 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
655 path, bytenr, root_objectid,
656 ref_generation, owner, owner_offset);
658 finish_current_insert(trans, root->fs_info->extent_root);
659 del_pending_extents(trans, root->fs_info->extent_root);
661 btrfs_free_path(path);
665 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
666 struct btrfs_root *root)
668 finish_current_insert(trans, root->fs_info->extent_root);
669 del_pending_extents(trans, root->fs_info->extent_root);
673 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
674 struct btrfs_root *root, u64 bytenr,
675 u64 num_bytes, u32 *refs)
677 struct btrfs_path *path;
679 struct btrfs_key key;
680 struct extent_buffer *l;
681 struct btrfs_extent_item *item;
683 WARN_ON(num_bytes < root->sectorsize);
684 path = btrfs_alloc_path();
686 key.objectid = bytenr;
687 key.offset = num_bytes;
688 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
689 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
694 btrfs_print_leaf(root, path->nodes[0]);
695 printk("failed to find block number %Lu\n", bytenr);
699 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
700 *refs = btrfs_extent_refs(l, item);
702 btrfs_free_path(path);
706 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
707 struct btrfs_path *count_path,
710 struct btrfs_root *extent_root = root->fs_info->extent_root;
711 struct btrfs_path *path;
714 u64 root_objectid = root->root_key.objectid;
719 struct btrfs_key key;
720 struct btrfs_key found_key;
721 struct extent_buffer *l;
722 struct btrfs_extent_item *item;
723 struct btrfs_extent_ref *ref_item;
726 path = btrfs_alloc_path();
729 bytenr = first_extent;
731 bytenr = count_path->nodes[level]->start;
734 key.objectid = bytenr;
737 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
738 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
744 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
746 if (found_key.objectid != bytenr ||
747 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
751 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
754 nritems = btrfs_header_nritems(l);
755 if (path->slots[0] >= nritems) {
756 ret = btrfs_next_leaf(extent_root, path);
761 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
762 if (found_key.objectid != bytenr)
765 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
771 ref_item = btrfs_item_ptr(l, path->slots[0],
772 struct btrfs_extent_ref);
773 found_objectid = btrfs_ref_root(l, ref_item);
775 if (found_objectid != root_objectid) {
782 if (cur_count == 0) {
786 if (level >= 0 && root->node == count_path->nodes[level])
789 btrfs_release_path(root, path);
793 btrfs_free_path(path);
796 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
797 struct btrfs_root *root, u64 owner_objectid)
803 struct btrfs_disk_key disk_key;
805 level = btrfs_header_level(root->node);
806 generation = trans->transid;
807 nritems = btrfs_header_nritems(root->node);
810 btrfs_item_key(root->node, &disk_key, 0);
812 btrfs_node_key(root->node, &disk_key, 0);
813 key_objectid = btrfs_disk_key_objectid(&disk_key);
817 return btrfs_inc_extent_ref(trans, root, root->node->start,
818 root->node->len, owner_objectid,
819 generation, level, key_objectid);
822 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
823 struct extent_buffer *buf)
827 struct btrfs_key key;
828 struct btrfs_file_extent_item *fi;
837 level = btrfs_header_level(buf);
838 nritems = btrfs_header_nritems(buf);
839 for (i = 0; i < nritems; i++) {
842 btrfs_item_key_to_cpu(buf, &key, i);
843 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
845 fi = btrfs_item_ptr(buf, i,
846 struct btrfs_file_extent_item);
847 if (btrfs_file_extent_type(buf, fi) ==
848 BTRFS_FILE_EXTENT_INLINE)
850 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
851 if (disk_bytenr == 0)
853 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
854 btrfs_file_extent_disk_num_bytes(buf, fi),
855 root->root_key.objectid, trans->transid,
856 key.objectid, key.offset);
862 bytenr = btrfs_node_blockptr(buf, i);
863 btrfs_node_key_to_cpu(buf, &key, i);
864 ret = btrfs_inc_extent_ref(trans, root, bytenr,
865 btrfs_level_size(root, level - 1),
866 root->root_key.objectid,
868 level - 1, key.objectid);
879 for (i =0; i < faili; i++) {
882 btrfs_item_key_to_cpu(buf, &key, i);
883 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
885 fi = btrfs_item_ptr(buf, i,
886 struct btrfs_file_extent_item);
887 if (btrfs_file_extent_type(buf, fi) ==
888 BTRFS_FILE_EXTENT_INLINE)
890 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
891 if (disk_bytenr == 0)
893 err = btrfs_free_extent(trans, root, disk_bytenr,
894 btrfs_file_extent_disk_num_bytes(buf,
898 bytenr = btrfs_node_blockptr(buf, i);
899 err = btrfs_free_extent(trans, root, bytenr,
900 btrfs_level_size(root, level - 1), 0);
908 static int write_one_cache_group(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root,
910 struct btrfs_path *path,
911 struct btrfs_block_group_cache *cache)
915 struct btrfs_root *extent_root = root->fs_info->extent_root;
917 struct extent_buffer *leaf;
919 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
924 leaf = path->nodes[0];
925 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
926 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
927 btrfs_mark_buffer_dirty(leaf);
928 btrfs_release_path(extent_root, path);
930 finish_current_insert(trans, extent_root);
931 pending_ret = del_pending_extents(trans, extent_root);
940 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root)
943 struct extent_map_tree *block_group_cache;
944 struct btrfs_block_group_cache *cache;
948 struct btrfs_path *path;
954 block_group_cache = &root->fs_info->block_group_cache;
955 path = btrfs_alloc_path();
960 ret = find_first_extent_bit(block_group_cache, last,
961 &start, &end, BLOCK_GROUP_DIRTY);
966 ret = get_state_private(block_group_cache, start, &ptr);
970 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
971 err = write_one_cache_group(trans, root,
974 * if we fail to write the cache group, we want
975 * to keep it marked dirty in hopes that a later
982 clear_extent_bits(block_group_cache, start, end,
983 BLOCK_GROUP_DIRTY, GFP_NOFS);
985 btrfs_free_path(path);
989 static int update_block_group(struct btrfs_trans_handle *trans,
990 struct btrfs_root *root,
991 u64 bytenr, u64 num_bytes, int alloc,
992 int mark_free, int data)
994 struct btrfs_block_group_cache *cache;
995 struct btrfs_fs_info *info = root->fs_info;
996 u64 total = num_bytes;
1003 cache = btrfs_lookup_block_group(info, bytenr);
1007 byte_in_group = bytenr - cache->key.objectid;
1008 WARN_ON(byte_in_group > cache->key.offset);
1009 start = cache->key.objectid;
1010 end = start + cache->key.offset - 1;
1011 set_extent_bits(&info->block_group_cache, start, end,
1012 BLOCK_GROUP_DIRTY, GFP_NOFS);
1014 old_val = btrfs_block_group_used(&cache->item);
1015 num_bytes = min(total, cache->key.offset - byte_in_group);
1017 if (cache->data != data &&
1018 old_val < (cache->key.offset >> 1)) {
1023 bit_to_clear = BLOCK_GROUP_METADATA;
1024 bit_to_set = BLOCK_GROUP_DATA;
1025 cache->item.flags &=
1026 ~BTRFS_BLOCK_GROUP_MIXED;
1027 cache->item.flags |=
1028 BTRFS_BLOCK_GROUP_DATA;
1030 bit_to_clear = BLOCK_GROUP_DATA;
1031 bit_to_set = BLOCK_GROUP_METADATA;
1032 cache->item.flags &=
1033 ~BTRFS_BLOCK_GROUP_MIXED;
1034 cache->item.flags &=
1035 ~BTRFS_BLOCK_GROUP_DATA;
1037 clear_extent_bits(&info->block_group_cache,
1038 start, end, bit_to_clear,
1040 set_extent_bits(&info->block_group_cache,
1041 start, end, bit_to_set,
1043 } else if (cache->data != data &&
1044 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1045 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1046 set_extent_bits(&info->block_group_cache,
1049 BLOCK_GROUP_METADATA,
1052 old_val += num_bytes;
1054 old_val -= num_bytes;
1056 set_extent_dirty(&info->free_space_cache,
1057 bytenr, bytenr + num_bytes - 1,
1061 btrfs_set_block_group_used(&cache->item, old_val);
1063 bytenr += num_bytes;
1067 static int update_pinned_extents(struct btrfs_root *root,
1068 u64 bytenr, u64 num, int pin)
1071 struct btrfs_block_group_cache *cache;
1072 struct btrfs_fs_info *fs_info = root->fs_info;
1075 set_extent_dirty(&fs_info->pinned_extents,
1076 bytenr, bytenr + num - 1, GFP_NOFS);
1078 clear_extent_dirty(&fs_info->pinned_extents,
1079 bytenr, bytenr + num - 1, GFP_NOFS);
1082 cache = btrfs_lookup_block_group(fs_info, bytenr);
1084 len = min(num, cache->key.offset -
1085 (bytenr - cache->key.objectid));
1087 cache->pinned += len;
1088 fs_info->total_pinned += len;
1090 cache->pinned -= len;
1091 fs_info->total_pinned -= len;
1099 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
1104 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
1108 ret = find_first_extent_bit(pinned_extents, last,
1109 &start, &end, EXTENT_DIRTY);
1112 set_extent_dirty(copy, start, end, GFP_NOFS);
1118 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1119 struct btrfs_root *root,
1120 struct extent_map_tree *unpin)
1125 struct extent_map_tree *free_space_cache;
1126 free_space_cache = &root->fs_info->free_space_cache;
1129 ret = find_first_extent_bit(unpin, 0, &start, &end,
1133 update_pinned_extents(root, start, end + 1 - start, 0);
1134 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1135 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1140 static int finish_current_insert(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *extent_root)
1145 struct btrfs_fs_info *info = extent_root->fs_info;
1146 struct extent_buffer *eb;
1147 struct btrfs_path *path;
1148 struct btrfs_key ins;
1149 struct btrfs_disk_key first;
1150 struct btrfs_extent_item extent_item;
1155 btrfs_set_stack_extent_refs(&extent_item, 1);
1156 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1157 path = btrfs_alloc_path();
1160 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1161 &end, EXTENT_LOCKED);
1165 ins.objectid = start;
1166 ins.offset = end + 1 - start;
1167 err = btrfs_insert_item(trans, extent_root, &ins,
1168 &extent_item, sizeof(extent_item));
1169 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1171 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1172 level = btrfs_header_level(eb);
1174 btrfs_item_key(eb, &first, 0);
1176 btrfs_node_key(eb, &first, 0);
1178 err = btrfs_insert_extent_backref(trans, extent_root, path,
1179 start, extent_root->root_key.objectid,
1181 btrfs_disk_key_objectid(&first));
1183 free_extent_buffer(eb);
1185 btrfs_free_path(path);
1189 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1193 struct extent_buffer *buf;
1196 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1198 if (btrfs_buffer_uptodate(buf)) {
1200 root->fs_info->running_transaction->transid;
1201 u64 header_transid =
1202 btrfs_header_generation(buf);
1203 if (header_transid == transid) {
1204 free_extent_buffer(buf);
1208 free_extent_buffer(buf);
1210 update_pinned_extents(root, bytenr, num_bytes, 1);
1212 set_extent_bits(&root->fs_info->pending_del,
1213 bytenr, bytenr + num_bytes - 1,
1214 EXTENT_LOCKED, GFP_NOFS);
1221 * remove an extent from the root, returns 0 on success
1223 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1224 *root, u64 bytenr, u64 num_bytes,
1225 u64 root_objectid, u64 ref_generation,
1226 u64 owner_objectid, u64 owner_offset, int pin,
1229 struct btrfs_path *path;
1230 struct btrfs_key key;
1231 struct btrfs_fs_info *info = root->fs_info;
1232 struct btrfs_root *extent_root = info->extent_root;
1233 struct extent_buffer *leaf;
1235 struct btrfs_extent_item *ei;
1238 key.objectid = bytenr;
1239 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1240 key.offset = num_bytes;
1242 path = btrfs_alloc_path();
1247 ret = lookup_extent_backref(trans, extent_root, path,
1248 bytenr, root_objectid,
1250 owner_objectid, owner_offset, 1);
1252 ret = btrfs_del_item(trans, extent_root, path);
1254 btrfs_print_leaf(extent_root, path->nodes[0]);
1256 printk("Unable to find ref byte nr %Lu root %Lu "
1257 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1258 root_objectid, ref_generation, owner_objectid,
1261 btrfs_release_path(extent_root, path);
1262 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1267 leaf = path->nodes[0];
1268 ei = btrfs_item_ptr(leaf, path->slots[0],
1269 struct btrfs_extent_item);
1270 refs = btrfs_extent_refs(leaf, ei);
1273 btrfs_set_extent_refs(leaf, ei, refs);
1274 btrfs_mark_buffer_dirty(leaf);
1281 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1287 /* block accounting for super block */
1288 super_used = btrfs_super_bytes_used(&info->super_copy);
1289 btrfs_set_super_bytes_used(&info->super_copy,
1290 super_used - num_bytes);
1292 /* block accounting for root item */
1293 root_used = btrfs_root_used(&root->root_item);
1294 btrfs_set_root_used(&root->root_item,
1295 root_used - num_bytes);
1297 ret = btrfs_del_item(trans, extent_root, path);
1301 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1305 btrfs_free_path(path);
1306 finish_current_insert(trans, extent_root);
1311 * find all the blocks marked as pending in the radix tree and remove
1312 * them from the extent map
1314 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1315 btrfs_root *extent_root)
1321 struct extent_map_tree *pending_del;
1322 struct extent_map_tree *pinned_extents;
1324 pending_del = &extent_root->fs_info->pending_del;
1325 pinned_extents = &extent_root->fs_info->pinned_extents;
1328 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1332 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1333 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1335 ret = __free_extent(trans, extent_root,
1336 start, end + 1 - start,
1337 extent_root->root_key.objectid,
1346 * remove an extent from the root, returns 0 on success
1348 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1349 *root, u64 bytenr, u64 num_bytes,
1350 u64 root_objectid, u64 ref_generation,
1351 u64 owner_objectid, u64 owner_offset, int pin)
1353 struct btrfs_root *extent_root = root->fs_info->extent_root;
1357 WARN_ON(num_bytes < root->sectorsize);
1358 if (!root->ref_cows)
1361 if (root == extent_root) {
1362 pin_down_bytes(root, bytenr, num_bytes, 1);
1365 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1366 ref_generation, owner_objectid, owner_offset,
1368 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1369 return ret ? ret : pending_ret;
1372 static u64 stripe_align(struct btrfs_root *root, u64 val)
1374 u64 mask = ((u64)root->stripesize - 1);
1375 u64 ret = (val + mask) & ~mask;
1380 * walks the btree of allocated extents and find a hole of a given size.
1381 * The key ins is changed to record the hole:
1382 * ins->objectid == block start
1383 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1384 * ins->offset == number of blocks
1385 * Any available blocks before search_start are skipped.
1387 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1388 struct btrfs_root *orig_root,
1389 u64 num_bytes, u64 empty_size,
1390 u64 search_start, u64 search_end,
1391 u64 hint_byte, struct btrfs_key *ins,
1392 u64 exclude_start, u64 exclude_nr,
1395 struct btrfs_path *path;
1396 struct btrfs_key key;
1402 u64 orig_search_start = search_start;
1404 struct extent_buffer *l;
1405 struct btrfs_root * root = orig_root->fs_info->extent_root;
1406 struct btrfs_fs_info *info = root->fs_info;
1407 u64 total_needed = num_bytes;
1409 struct btrfs_block_group_cache *block_group;
1414 WARN_ON(num_bytes < root->sectorsize);
1415 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1417 level = btrfs_header_level(root->node);
1419 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
1420 data = BTRFS_BLOCK_GROUP_MIXED;
1423 if (search_end == (u64)-1)
1424 search_end = btrfs_super_total_bytes(&info->super_copy);
1426 block_group = btrfs_lookup_block_group(info, hint_byte);
1428 hint_byte = search_start;
1429 block_group = btrfs_find_block_group(root, block_group,
1430 hint_byte, data, 1);
1432 block_group = btrfs_find_block_group(root,
1434 search_start, data, 1);
1437 total_needed += empty_size;
1438 path = btrfs_alloc_path();
1441 block_group = btrfs_lookup_block_group(info, search_start);
1443 block_group = btrfs_lookup_block_group(info,
1446 search_start = find_search_start(root, &block_group, search_start,
1447 total_needed, data);
1448 search_start = stripe_align(root, search_start);
1449 cached_start = search_start;
1450 btrfs_init_path(path);
1451 ins->objectid = search_start;
1456 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1459 ret = find_previous_extent(root, path);
1463 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1466 slot = path->slots[0];
1467 if (slot >= btrfs_header_nritems(l)) {
1468 ret = btrfs_next_leaf(root, path);
1474 search_start = max(search_start,
1475 block_group->key.objectid);
1477 aligned = stripe_align(root, search_start);
1478 ins->objectid = aligned;
1479 if (aligned >= search_end) {
1483 ins->offset = search_end - aligned;
1487 ins->objectid = stripe_align(root,
1488 last_byte > search_start ?
1489 last_byte : search_start);
1490 if (search_end <= ins->objectid) {
1494 ins->offset = search_end - ins->objectid;
1495 BUG_ON(ins->objectid >= search_end);
1498 btrfs_item_key_to_cpu(l, &key, slot);
1500 if (key.objectid >= search_start && key.objectid > last_byte &&
1502 if (last_byte < search_start)
1503 last_byte = search_start;
1504 aligned = stripe_align(root, last_byte);
1505 hole_size = key.objectid - aligned;
1506 if (key.objectid > aligned && hole_size >= num_bytes) {
1507 ins->objectid = aligned;
1508 ins->offset = hole_size;
1512 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1513 if (!start_found && btrfs_key_type(&key) ==
1514 BTRFS_BLOCK_GROUP_ITEM_KEY) {
1515 last_byte = key.objectid;
1523 last_byte = key.objectid + key.offset;
1525 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1526 last_byte >= block_group->key.objectid +
1527 block_group->key.offset) {
1528 btrfs_release_path(root, path);
1529 search_start = block_group->key.objectid +
1530 block_group->key.offset;
1538 /* we have to make sure we didn't find an extent that has already
1539 * been allocated by the map tree or the original allocation
1541 btrfs_release_path(root, path);
1542 BUG_ON(ins->objectid < search_start);
1544 if (ins->objectid + num_bytes >= search_end)
1546 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1547 ins->objectid + num_bytes > block_group->
1548 key.objectid + block_group->key.offset) {
1549 search_start = block_group->key.objectid +
1550 block_group->key.offset;
1553 if (test_range_bit(&info->extent_ins, ins->objectid,
1554 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1555 search_start = ins->objectid + num_bytes;
1558 if (test_range_bit(&info->pinned_extents, ins->objectid,
1559 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1560 search_start = ins->objectid + num_bytes;
1563 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1564 ins->objectid < exclude_start + exclude_nr)) {
1565 search_start = exclude_start + exclude_nr;
1569 block_group = btrfs_lookup_block_group(info, ins->objectid);
1571 trans->block_group = block_group;
1573 ins->offset = num_bytes;
1574 btrfs_free_path(path);
1578 if (search_start + num_bytes >= search_end) {
1580 search_start = orig_search_start;
1587 total_needed -= empty_size;
1589 data = BTRFS_BLOCK_GROUP_MIXED;
1593 block_group = btrfs_lookup_block_group(info, search_start);
1595 block_group = btrfs_find_block_group(root, block_group,
1596 search_start, data, 0);
1600 btrfs_release_path(root, path);
1601 btrfs_free_path(path);
1605 * finds a free extent and does all the dirty work required for allocation
1606 * returns the key for the extent through ins, and a tree buffer for
1607 * the first block of the extent through buf.
1609 * returns 0 if everything worked, non-zero otherwise.
1611 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1614 u64 owner, u64 owner_offset,
1615 u64 empty_size, u64 hint_byte,
1616 u64 search_end, struct btrfs_key *ins, int data)
1620 u64 super_used, root_used;
1621 u64 search_start = 0;
1623 struct btrfs_fs_info *info = root->fs_info;
1624 struct btrfs_root *extent_root = info->extent_root;
1625 struct btrfs_extent_item extent_item;
1626 struct btrfs_path *path;
1628 btrfs_set_stack_extent_refs(&extent_item, 1);
1630 new_hint = max(hint_byte, root->fs_info->alloc_start);
1631 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1632 hint_byte = new_hint;
1634 WARN_ON(num_bytes < root->sectorsize);
1635 ret = find_free_extent(trans, root, num_bytes, empty_size,
1636 search_start, search_end, hint_byte, ins,
1637 trans->alloc_exclude_start,
1638 trans->alloc_exclude_nr, data);
1643 /* block accounting for super block */
1644 super_used = btrfs_super_bytes_used(&info->super_copy);
1645 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1647 /* block accounting for root item */
1648 root_used = btrfs_root_used(&root->root_item);
1649 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1651 clear_extent_dirty(&root->fs_info->free_space_cache,
1652 ins->objectid, ins->objectid + ins->offset - 1,
1655 if (root == extent_root) {
1656 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1657 ins->objectid + ins->offset - 1,
1658 EXTENT_LOCKED, GFP_NOFS);
1663 WARN_ON(trans->alloc_exclude_nr);
1664 trans->alloc_exclude_start = ins->objectid;
1665 trans->alloc_exclude_nr = ins->offset;
1666 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1667 sizeof(extent_item));
1669 trans->alloc_exclude_start = 0;
1670 trans->alloc_exclude_nr = 0;
1673 path = btrfs_alloc_path();
1675 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1676 ins->objectid, root_objectid,
1677 ref_generation, owner, owner_offset);
1680 btrfs_free_path(path);
1681 finish_current_insert(trans, extent_root);
1682 pending_ret = del_pending_extents(trans, extent_root);
1692 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1699 * helper function to allocate a block for a given tree
1700 * returns the tree buffer or NULL.
1702 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1703 struct btrfs_root *root,
1705 u64 root_objectid, u64 hint,
1711 ref_generation = trans->transid;
1716 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1717 ref_generation, 0, 0, hint, empty_size);
1721 * helper function to allocate a block for a given tree
1722 * returns the tree buffer or NULL.
1724 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root,
1734 struct btrfs_key ins;
1736 struct extent_buffer *buf;
1738 ret = btrfs_alloc_extent(trans, root, blocksize,
1739 root_objectid, ref_generation,
1740 level, first_objectid, empty_size, hint,
1744 return ERR_PTR(ret);
1746 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1748 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1749 root->root_key.objectid, ref_generation,
1751 return ERR_PTR(-ENOMEM);
1753 btrfs_set_buffer_uptodate(buf);
1754 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1755 buf->start + buf->len - 1, GFP_NOFS);
1756 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1757 buf->start, buf->start + buf->len - 1,
1758 EXTENT_CSUM, GFP_NOFS);
1759 buf->flags |= EXTENT_CSUM;
1760 btrfs_set_buffer_defrag(buf);
1761 trans->blocks_used++;
1765 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root,
1767 struct extent_buffer *leaf)
1770 u64 leaf_generation;
1771 struct btrfs_key key;
1772 struct btrfs_file_extent_item *fi;
1777 BUG_ON(!btrfs_is_leaf(leaf));
1778 nritems = btrfs_header_nritems(leaf);
1779 leaf_owner = btrfs_header_owner(leaf);
1780 leaf_generation = btrfs_header_generation(leaf);
1782 for (i = 0; i < nritems; i++) {
1785 btrfs_item_key_to_cpu(leaf, &key, i);
1786 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1788 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1789 if (btrfs_file_extent_type(leaf, fi) ==
1790 BTRFS_FILE_EXTENT_INLINE)
1793 * FIXME make sure to insert a trans record that
1794 * repeats the snapshot del on crash
1796 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1797 if (disk_bytenr == 0)
1799 ret = btrfs_free_extent(trans, root, disk_bytenr,
1800 btrfs_file_extent_disk_num_bytes(leaf, fi),
1801 leaf_owner, leaf_generation,
1802 key.objectid, key.offset, 0);
1808 static void noinline reada_walk_down(struct btrfs_root *root,
1809 struct extent_buffer *node)
1819 nritems = btrfs_header_nritems(node);
1820 level = btrfs_header_level(node);
1821 for (i = 0; i < nritems; i++) {
1822 bytenr = btrfs_node_blockptr(node, i);
1823 blocksize = btrfs_level_size(root, level - 1);
1824 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1828 mutex_unlock(&root->fs_info->fs_mutex);
1829 ret = readahead_tree_block(root, bytenr, blocksize);
1831 mutex_lock(&root->fs_info->fs_mutex);
1838 * helper function for drop_snapshot, this walks down the tree dropping ref
1839 * counts as it goes.
1841 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1842 struct btrfs_root *root,
1843 struct btrfs_path *path, int *level)
1848 struct extent_buffer *next;
1849 struct extent_buffer *cur;
1850 struct extent_buffer *parent;
1855 WARN_ON(*level < 0);
1856 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1857 ret = lookup_extent_ref(trans, root,
1858 path->nodes[*level]->start,
1859 path->nodes[*level]->len, &refs);
1865 * walk down to the last node level and free all the leaves
1867 while(*level >= 0) {
1868 WARN_ON(*level < 0);
1869 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1870 cur = path->nodes[*level];
1872 if (*level > 0 && path->slots[*level] == 0)
1873 reada_walk_down(root, cur);
1875 if (btrfs_header_level(cur) != *level)
1878 if (path->slots[*level] >=
1879 btrfs_header_nritems(cur))
1882 ret = drop_leaf_ref(trans, root, cur);
1886 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1887 blocksize = btrfs_level_size(root, *level - 1);
1888 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1891 parent = path->nodes[*level];
1892 root_owner = btrfs_header_owner(parent);
1893 root_gen = btrfs_header_generation(parent);
1894 path->slots[*level]++;
1895 ret = btrfs_free_extent(trans, root, bytenr,
1896 blocksize, root_owner,
1901 next = btrfs_find_tree_block(root, bytenr, blocksize);
1902 if (!next || !btrfs_buffer_uptodate(next)) {
1903 free_extent_buffer(next);
1904 mutex_unlock(&root->fs_info->fs_mutex);
1905 next = read_tree_block(root, bytenr, blocksize);
1906 mutex_lock(&root->fs_info->fs_mutex);
1908 /* we dropped the lock, check one more time */
1909 ret = lookup_extent_ref(trans, root, bytenr,
1913 parent = path->nodes[*level];
1914 root_owner = btrfs_header_owner(parent);
1915 root_gen = btrfs_header_generation(parent);
1917 path->slots[*level]++;
1918 free_extent_buffer(next);
1919 ret = btrfs_free_extent(trans, root, bytenr,
1927 WARN_ON(*level <= 0);
1928 if (path->nodes[*level-1])
1929 free_extent_buffer(path->nodes[*level-1]);
1930 path->nodes[*level-1] = next;
1931 *level = btrfs_header_level(next);
1932 path->slots[*level] = 0;
1935 WARN_ON(*level < 0);
1936 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1938 if (path->nodes[*level] == root->node) {
1939 root_owner = root->root_key.objectid;
1940 parent = path->nodes[*level];
1942 parent = path->nodes[*level + 1];
1943 root_owner = btrfs_header_owner(parent);
1946 root_gen = btrfs_header_generation(parent);
1947 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1948 path->nodes[*level]->len,
1949 root_owner, root_gen, 0, 0, 1);
1950 free_extent_buffer(path->nodes[*level]);
1951 path->nodes[*level] = NULL;
1958 * helper for dropping snapshots. This walks back up the tree in the path
1959 * to find the first node higher up where we haven't yet gone through
1962 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1963 struct btrfs_root *root,
1964 struct btrfs_path *path, int *level)
1968 struct btrfs_root_item *root_item = &root->root_item;
1973 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1974 slot = path->slots[i];
1975 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1976 struct extent_buffer *node;
1977 struct btrfs_disk_key disk_key;
1978 node = path->nodes[i];
1981 WARN_ON(*level == 0);
1982 btrfs_node_key(node, &disk_key, path->slots[i]);
1983 memcpy(&root_item->drop_progress,
1984 &disk_key, sizeof(disk_key));
1985 root_item->drop_level = i;
1988 if (path->nodes[*level] == root->node) {
1989 root_owner = root->root_key.objectid;
1991 btrfs_header_generation(path->nodes[*level]);
1993 struct extent_buffer *node;
1994 node = path->nodes[*level + 1];
1995 root_owner = btrfs_header_owner(node);
1996 root_gen = btrfs_header_generation(node);
1998 ret = btrfs_free_extent(trans, root,
1999 path->nodes[*level]->start,
2000 path->nodes[*level]->len,
2001 root_owner, root_gen, 0, 0, 1);
2003 free_extent_buffer(path->nodes[*level]);
2004 path->nodes[*level] = NULL;
2012 * drop the reference count on the tree rooted at 'snap'. This traverses
2013 * the tree freeing any blocks that have a ref count of zero after being
2016 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2022 struct btrfs_path *path;
2025 struct btrfs_root_item *root_item = &root->root_item;
2027 path = btrfs_alloc_path();
2030 level = btrfs_header_level(root->node);
2032 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2033 path->nodes[level] = root->node;
2034 extent_buffer_get(root->node);
2035 path->slots[level] = 0;
2037 struct btrfs_key key;
2038 struct btrfs_disk_key found_key;
2039 struct extent_buffer *node;
2041 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2042 level = root_item->drop_level;
2043 path->lowest_level = level;
2044 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2049 node = path->nodes[level];
2050 btrfs_node_key(node, &found_key, path->slots[level]);
2051 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2052 sizeof(found_key)));
2055 wret = walk_down_tree(trans, root, path, &level);
2061 wret = walk_up_tree(trans, root, path, &level);
2069 for (i = 0; i <= orig_level; i++) {
2070 if (path->nodes[i]) {
2071 free_extent_buffer(path->nodes[i]);
2072 path->nodes[i] = NULL;
2076 btrfs_free_path(path);
2080 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2087 ret = find_first_extent_bit(&info->block_group_cache, 0,
2088 &start, &end, (unsigned int)-1);
2091 ret = get_state_private(&info->block_group_cache, start, &ptr);
2093 kfree((void *)(unsigned long)ptr);
2094 clear_extent_bits(&info->block_group_cache, start,
2095 end, (unsigned int)-1, GFP_NOFS);
2098 ret = find_first_extent_bit(&info->free_space_cache, 0,
2099 &start, &end, EXTENT_DIRTY);
2102 clear_extent_dirty(&info->free_space_cache, start,
2108 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2114 u64 existing_delalloc;
2115 unsigned long last_index;
2118 struct btrfs_root *root = BTRFS_I(inode)->root;
2119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2120 struct file_ra_state *ra;
2122 ra = kzalloc(sizeof(*ra), GFP_NOFS);
2124 mutex_lock(&inode->i_mutex);
2125 i = start >> PAGE_CACHE_SHIFT;
2126 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2128 file_ra_state_init(ra, inode->i_mapping);
2129 btrfs_force_ra(inode->i_mapping, ra, NULL, i, last_index);
2132 for (; i <= last_index; i++) {
2133 page = grab_cache_page(inode->i_mapping, i);
2136 if (!PageUptodate(page)) {
2137 btrfs_readpage(NULL, page);
2139 if (!PageUptodate(page)) {
2141 page_cache_release(page);
2145 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2146 page_end = page_start + PAGE_CACHE_SIZE - 1;
2148 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2150 delalloc_start = page_start;
2152 count_range_bits(&BTRFS_I(inode)->extent_tree,
2153 &delalloc_start, page_end,
2154 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2156 set_extent_delalloc(em_tree, page_start,
2157 page_end, GFP_NOFS);
2159 spin_lock(&root->fs_info->delalloc_lock);
2160 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2162 spin_unlock(&root->fs_info->delalloc_lock);
2164 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2165 set_page_dirty(page);
2167 page_cache_release(page);
2171 mutex_unlock(&inode->i_mutex);
2176 * note, this releases the path
2178 static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2179 struct btrfs_path *path,
2180 struct btrfs_key *extent_key)
2182 struct inode *inode;
2183 struct btrfs_root *found_root;
2184 struct btrfs_key *root_location;
2185 struct btrfs_extent_ref *ref;
2192 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2193 struct btrfs_extent_ref);
2194 ref_root = btrfs_ref_root(path->nodes[0], ref);
2195 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2196 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2197 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2198 btrfs_release_path(extent_root, path);
2200 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2201 root_location->objectid = ref_root;
2203 root_location->offset = 0;
2205 root_location->offset = (u64)-1;
2206 root_location->type = BTRFS_ROOT_ITEM_KEY;
2208 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2210 BUG_ON(!found_root);
2211 kfree(root_location);
2213 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2214 mutex_unlock(&extent_root->fs_info->fs_mutex);
2215 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2216 ref_objectid, found_root);
2217 if (inode->i_state & I_NEW) {
2218 /* the inode and parent dir are two different roots */
2219 BTRFS_I(inode)->root = found_root;
2220 BTRFS_I(inode)->location.objectid = ref_objectid;
2221 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2222 BTRFS_I(inode)->location.offset = 0;
2223 btrfs_read_locked_inode(inode);
2224 unlock_new_inode(inode);
2227 /* this can happen if the reference is not against
2228 * the latest version of the tree root
2230 if (is_bad_inode(inode)) {
2231 mutex_lock(&extent_root->fs_info->fs_mutex);
2234 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2235 /* FIXME, data=ordered will help get rid of this */
2236 filemap_fdatawrite(inode->i_mapping);
2238 mutex_lock(&extent_root->fs_info->fs_mutex);
2240 struct btrfs_trans_handle *trans;
2241 struct btrfs_key found_key;
2242 struct extent_buffer *eb;
2246 trans = btrfs_start_transaction(found_root, 1);
2247 eb = read_tree_block(found_root, extent_key->objectid,
2248 extent_key->offset);
2249 level = btrfs_header_level(eb);
2252 btrfs_item_key_to_cpu(eb, &found_key, 0);
2254 btrfs_node_key_to_cpu(eb, &found_key, 0);
2256 free_extent_buffer(eb);
2258 path->lowest_level = level;
2260 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2262 path->lowest_level = 0;
2263 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2264 if (!path->nodes[i])
2266 free_extent_buffer(path->nodes[i]);
2267 path->nodes[i] = NULL;
2269 btrfs_release_path(found_root, path);
2270 btrfs_end_transaction(trans, found_root);
2277 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2278 struct btrfs_path *path,
2279 struct btrfs_key *extent_key)
2281 struct btrfs_key key;
2282 struct btrfs_key found_key;
2283 struct extent_buffer *leaf;
2288 key.objectid = extent_key->objectid;
2289 key.type = BTRFS_EXTENT_REF_KEY;
2293 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2301 leaf = path->nodes[0];
2302 nritems = btrfs_header_nritems(leaf);
2303 if (path->slots[0] == nritems)
2306 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2307 if (found_key.objectid != extent_key->objectid)
2310 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2313 key.offset = found_key.offset + 1;
2314 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2316 ret = relocate_one_reference(extent_root, path, extent_key);
2322 btrfs_release_path(extent_root, path);
2326 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2328 struct btrfs_trans_handle *trans;
2329 struct btrfs_root *tree_root = root->fs_info->tree_root;
2330 struct btrfs_path *path;
2333 struct btrfs_fs_info *info = root->fs_info;
2334 struct extent_map_tree *block_group_cache;
2335 struct btrfs_key key;
2336 struct btrfs_key found_key;
2337 struct extent_buffer *leaf;
2342 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2343 block_group_cache = &info->block_group_cache;
2344 path = btrfs_alloc_path();
2345 root = root->fs_info->extent_root;
2350 key.objectid = new_size;
2353 cur_byte = key.objectid;
2355 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2359 ret = find_previous_extent(root, path);
2363 leaf = path->nodes[0];
2364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2365 if (found_key.objectid + found_key.offset > new_size) {
2366 cur_byte = found_key.objectid;
2367 key.objectid = cur_byte;
2370 btrfs_release_path(root, path);
2373 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2377 leaf = path->nodes[0];
2378 nritems = btrfs_header_nritems(leaf);
2380 if (path->slots[0] >= nritems) {
2381 ret = btrfs_next_leaf(root, path);
2388 leaf = path->nodes[0];
2389 nritems = btrfs_header_nritems(leaf);
2392 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2394 if (progress && need_resched()) {
2395 memcpy(&key, &found_key, sizeof(key));
2396 mutex_unlock(&root->fs_info->fs_mutex);
2398 mutex_lock(&root->fs_info->fs_mutex);
2399 btrfs_release_path(root, path);
2400 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2406 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2407 found_key.objectid + found_key.offset <= cur_byte) {
2413 cur_byte = found_key.objectid + found_key.offset;
2414 key.objectid = cur_byte;
2415 btrfs_release_path(root, path);
2416 ret = relocate_one_extent(root, path, &found_key);
2419 btrfs_release_path(root, path);
2421 if (total_found > 0) {
2422 trans = btrfs_start_transaction(tree_root, 1);
2423 btrfs_commit_transaction(trans, tree_root);
2425 mutex_unlock(&root->fs_info->fs_mutex);
2426 btrfs_clean_old_snapshots(tree_root);
2427 mutex_lock(&root->fs_info->fs_mutex);
2429 trans = btrfs_start_transaction(tree_root, 1);
2430 btrfs_commit_transaction(trans, tree_root);
2434 trans = btrfs_start_transaction(root, 1);
2435 key.objectid = new_size;
2441 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2445 leaf = path->nodes[0];
2446 nritems = btrfs_header_nritems(leaf);
2448 if (path->slots[0] >= nritems) {
2449 ret = btrfs_next_leaf(root, path);
2456 leaf = path->nodes[0];
2457 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2460 * btrfs_next_leaf doesn't cow buffers, we have to
2461 * do the search again
2463 memcpy(&key, &found_key, sizeof(key));
2464 btrfs_release_path(root, path);
2468 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2469 if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2470 printk("shrinker found key %Lu %u %Lu\n",
2471 found_key.objectid, found_key.type,
2476 ret = get_state_private(&info->block_group_cache,
2477 found_key.objectid, &ptr);
2479 kfree((void *)(unsigned long)ptr);
2481 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2482 found_key.objectid + found_key.offset - 1,
2483 (unsigned int)-1, GFP_NOFS);
2485 key.objectid = found_key.objectid + 1;
2486 btrfs_del_item(trans, root, path);
2487 btrfs_release_path(root, path);
2489 if (need_resched()) {
2490 mutex_unlock(&root->fs_info->fs_mutex);
2492 mutex_lock(&root->fs_info->fs_mutex);
2495 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2497 btrfs_commit_transaction(trans, root);
2499 btrfs_free_path(path);
2503 int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2504 struct btrfs_root *root, u64 new_size)
2506 struct btrfs_path *path;
2511 struct btrfs_block_group_cache *cache;
2512 struct btrfs_block_group_item *item;
2513 struct btrfs_fs_info *info = root->fs_info;
2514 struct extent_map_tree *block_group_cache;
2515 struct btrfs_key key;
2516 struct extent_buffer *leaf;
2520 old_size = btrfs_super_total_bytes(&info->super_copy);
2521 block_group_cache = &info->block_group_cache;
2523 root = info->extent_root;
2525 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2527 cur_byte = cache->key.objectid + cache->key.offset;
2528 if (cur_byte >= new_size)
2531 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2532 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2534 path = btrfs_alloc_path();
2538 while(cur_byte < new_size) {
2539 key.objectid = cur_byte;
2540 ret = btrfs_insert_empty_item(trans, root, path, &key,
2541 sizeof(struct btrfs_block_group_item));
2543 leaf = path->nodes[0];
2544 item = btrfs_item_ptr(leaf, path->slots[0],
2545 struct btrfs_block_group_item);
2547 btrfs_set_disk_block_group_used(leaf, item, 0);
2548 div_long_long_rem(nr, 3, &rem);
2550 btrfs_set_disk_block_group_flags(leaf, item,
2551 BTRFS_BLOCK_GROUP_DATA);
2553 btrfs_set_disk_block_group_flags(leaf, item, 0);
2557 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2560 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2561 sizeof(cache->item));
2563 memcpy(&cache->key, &key, sizeof(key));
2566 cur_byte = key.objectid + key.offset;
2567 btrfs_release_path(root, path);
2569 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2570 bit = BLOCK_GROUP_DATA;
2571 cache->data = BTRFS_BLOCK_GROUP_DATA;
2573 bit = BLOCK_GROUP_METADATA;
2577 /* use EXTENT_LOCKED to prevent merging */
2578 set_extent_bits(block_group_cache, key.objectid,
2579 key.objectid + key.offset - 1,
2580 bit | EXTENT_LOCKED, GFP_NOFS);
2581 set_state_private(block_group_cache, key.objectid,
2582 (unsigned long)cache);
2584 btrfs_free_path(path);
2586 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2590 int btrfs_read_block_groups(struct btrfs_root *root)
2592 struct btrfs_path *path;
2596 struct btrfs_block_group_cache *cache;
2597 struct btrfs_fs_info *info = root->fs_info;
2598 struct extent_map_tree *block_group_cache;
2599 struct btrfs_key key;
2600 struct btrfs_key found_key;
2601 struct extent_buffer *leaf;
2603 block_group_cache = &info->block_group_cache;
2605 root = info->extent_root;
2607 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2608 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2610 path = btrfs_alloc_path();
2615 ret = btrfs_search_slot(NULL, info->extent_root,
2621 leaf = path->nodes[0];
2622 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2623 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2629 read_extent_buffer(leaf, &cache->item,
2630 btrfs_item_ptr_offset(leaf, path->slots[0]),
2631 sizeof(cache->item));
2632 memcpy(&cache->key, &found_key, sizeof(found_key));
2635 key.objectid = found_key.objectid + found_key.offset;
2636 btrfs_release_path(root, path);
2638 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2639 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2640 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2641 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2642 bit = BLOCK_GROUP_DATA;
2643 cache->data = BTRFS_BLOCK_GROUP_DATA;
2645 bit = BLOCK_GROUP_METADATA;
2649 /* use EXTENT_LOCKED to prevent merging */
2650 set_extent_bits(block_group_cache, found_key.objectid,
2651 found_key.objectid + found_key.offset - 1,
2652 bit | EXTENT_LOCKED, GFP_NOFS);
2653 set_state_private(block_group_cache, found_key.objectid,
2654 (unsigned long)cache);
2657 btrfs_super_total_bytes(&info->super_copy))
2661 btrfs_free_path(path);