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.
21 #include "kerncompat.h"
22 #include "radix-tree.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_DIRTY EXTENT_DIRTY
33 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
35 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
38 static int cache_block_group(struct btrfs_root *root,
39 struct btrfs_block_group_cache *block_group)
41 struct btrfs_path *path;
44 struct extent_buffer *leaf;
45 struct extent_map_tree *free_space_cache;
55 root = root->fs_info->extent_root;
56 free_space_cache = &root->fs_info->free_space_cache;
58 if (block_group->cached)
61 path = btrfs_alloc_path();
66 first_free = block_group->key.objectid;
67 key.objectid = block_group->key.objectid;
70 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
71 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76 if (ret && path->slots[0] > 0)
80 leaf = path->nodes[0];
81 slot = path->slots[0];
82 if (slot >= btrfs_header_nritems(leaf)) {
83 ret = btrfs_next_leaf(root, path);
93 btrfs_item_key_to_cpu(leaf, &key, slot);
94 if (key.objectid < block_group->key.objectid) {
95 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
96 key.objectid + key.offset > first_free)
97 first_free = key.objectid + key.offset;
101 if (key.objectid >= block_group->key.objectid +
102 block_group->key.offset) {
106 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
111 if (key.objectid > last) {
112 hole_size = key.objectid - last;
113 set_extent_dirty(free_space_cache, last,
114 last + hole_size - 1,
117 last = key.objectid + key.offset;
125 if (block_group->key.objectid +
126 block_group->key.offset > last) {
127 hole_size = block_group->key.objectid +
128 block_group->key.offset - last;
129 set_extent_dirty(free_space_cache, last,
130 last + hole_size - 1, GFP_NOFS);
132 block_group->cached = 1;
134 btrfs_free_path(path);
138 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
142 struct extent_map_tree *block_group_cache;
143 struct btrfs_block_group_cache *block_group = NULL;
149 block_group_cache = &info->block_group_cache;
150 ret = find_first_extent_bit(block_group_cache,
151 bytenr, &start, &end,
152 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
156 ret = get_state_private(block_group_cache, start, &ptr);
160 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
161 if (block_group->key.objectid <= bytenr && bytenr <
162 block_group->key.objectid + block_group->key.offset)
167 static u64 noinline find_search_start(struct btrfs_root *root,
168 struct btrfs_block_group_cache **cache_ret,
169 u64 search_start, int num,
170 int data, int full_scan)
173 struct btrfs_block_group_cache *cache = *cache_ret;
184 ret = cache_block_group(root, cache);
188 last = max(search_start, cache->key.objectid);
191 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
192 last, &start, &end, EXTENT_DIRTY);
199 start = max(last, start);
201 if (last - start < num) {
202 if (last == cache->key.objectid + cache->key.offset)
206 if (data != BTRFS_BLOCK_GROUP_MIXED &&
207 start + num > cache->key.objectid + cache->key.offset)
212 cache = btrfs_lookup_block_group(root->fs_info, search_start);
214 printk("Unable to find block group for %Lu\n",
222 last = cache->key.objectid + cache->key.offset;
224 cache = btrfs_lookup_block_group(root->fs_info, last);
230 data = BTRFS_BLOCK_GROUP_MIXED;
235 if (cache_miss && !cache->cached) {
236 cache_block_group(root, cache);
238 cache = btrfs_lookup_block_group(root->fs_info, last);
240 cache = btrfs_find_block_group(root, cache, last, data, 0);
248 static u64 div_factor(u64 num, int factor)
257 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
258 struct btrfs_block_group_cache
259 *hint, u64 search_start,
262 struct btrfs_block_group_cache *cache;
263 struct extent_map_tree *block_group_cache;
264 struct btrfs_block_group_cache *found_group = NULL;
265 struct btrfs_fs_info *info = root->fs_info;
279 block_group_cache = &info->block_group_cache;
284 if (data == BTRFS_BLOCK_GROUP_MIXED) {
285 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
288 bit = BLOCK_GROUP_DATA;
290 bit = BLOCK_GROUP_METADATA;
293 struct btrfs_block_group_cache *shint;
294 shint = btrfs_lookup_block_group(info, search_start);
295 if (shint && (shint->data == data ||
296 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
297 used = btrfs_block_group_used(&shint->item);
298 if (used + shint->pinned <
299 div_factor(shint->key.offset, factor)) {
304 if (hint && (hint->data == data ||
305 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
306 used = btrfs_block_group_used(&hint->item);
307 if (used + hint->pinned <
308 div_factor(hint->key.offset, factor)) {
311 last = hint->key.objectid + hint->key.offset;
315 hint_last = max(hint->key.objectid, search_start);
317 hint_last = search_start;
323 ret = find_first_extent_bit(block_group_cache, last,
328 ret = get_state_private(block_group_cache, start, &ptr);
332 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
333 last = cache->key.objectid + cache->key.offset;
334 used = btrfs_block_group_used(&cache->item);
337 free_check = cache->key.offset;
339 free_check = div_factor(cache->key.offset, factor);
340 if (used + cache->pinned < free_check) {
353 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
361 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
362 u64 owner, u64 owner_offset)
364 u32 high_crc = ~(u32)0;
365 u32 low_crc = ~(u32)0;
368 lenum = cpu_to_le64(root_objectid);
369 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
370 lenum = cpu_to_le64(ref_generation);
371 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
374 lenum = cpu_to_le64(owner);
375 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
376 lenum = cpu_to_le64(owner_offset);
377 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
379 return ((u64)high_crc << 32) | (u64)low_crc;
382 static int match_extent_ref(struct extent_buffer *leaf,
383 struct btrfs_extent_ref *disk_ref,
384 struct btrfs_extent_ref *cpu_ref)
389 if (cpu_ref->objectid)
390 len = sizeof(*cpu_ref);
392 len = 2 * sizeof(u64);
393 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
398 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
399 struct btrfs_root *root,
400 struct btrfs_path *path, u64 bytenr,
402 u64 ref_generation, u64 owner,
403 u64 owner_offset, int del)
406 struct btrfs_key key;
407 struct btrfs_key found_key;
408 struct btrfs_extent_ref ref;
409 struct extent_buffer *leaf;
410 struct btrfs_extent_ref *disk_ref;
414 btrfs_set_stack_ref_root(&ref, root_objectid);
415 btrfs_set_stack_ref_generation(&ref, ref_generation);
416 btrfs_set_stack_ref_objectid(&ref, owner);
417 btrfs_set_stack_ref_offset(&ref, owner_offset);
419 hash = hash_extent_ref(root_objectid, ref_generation, owner,
422 key.objectid = bytenr;
423 key.type = BTRFS_EXTENT_REF_KEY;
426 ret = btrfs_search_slot(trans, root, &key, path,
430 leaf = path->nodes[0];
432 u32 nritems = btrfs_header_nritems(leaf);
433 if (path->slots[0] >= nritems) {
434 ret2 = btrfs_next_leaf(root, path);
437 leaf = path->nodes[0];
439 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
440 if (found_key.objectid != bytenr ||
441 found_key.type != BTRFS_EXTENT_REF_KEY)
443 key.offset = found_key.offset;
445 btrfs_release_path(root, path);
449 disk_ref = btrfs_item_ptr(path->nodes[0],
451 struct btrfs_extent_ref);
452 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
456 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
457 key.offset = found_key.offset + 1;
458 btrfs_release_path(root, path);
465 * Back reference rules. Back refs have three main goals:
467 * 1) differentiate between all holders of references to an extent so that
468 * when a reference is dropped we can make sure it was a valid reference
469 * before freeing the extent.
471 * 2) Provide enough information to quickly find the holders of an extent
472 * if we notice a given block is corrupted or bad.
474 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
475 * maintenance. This is actually the same as #2, but with a slightly
476 * different use case.
478 * File extents can be referenced by:
480 * - multiple snapshots, subvolumes, or different generations in one subvol
481 * - different files inside a single subvolume (in theory, not implemented yet)
482 * - different offsets inside a file (bookend extents in file.c)
484 * The extent ref structure has fields for:
486 * - Objectid of the subvolume root
487 * - Generation number of the tree holding the reference
488 * - objectid of the file holding the reference
489 * - offset in the file corresponding to the key holding the reference
491 * When a file extent is allocated the fields are filled in:
492 * (root_key.objectid, trans->transid, inode objectid, offset in file)
494 * When a leaf is cow'd new references are added for every file extent found
495 * in the leaf. It looks the same as the create case, but trans->transid
496 * will be different when the block is cow'd.
498 * (root_key.objectid, trans->transid, inode objectid, offset in file)
500 * When a file extent is removed either during snapshot deletion or file
501 * truncation, the corresponding back reference is found
504 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
505 * inode objectid, offset in file)
507 * Btree extents can be referenced by:
509 * - Different subvolumes
510 * - Different generations of the same subvolume
512 * Storing sufficient information for a full reverse mapping of a btree
513 * block would require storing the lowest key of the block in the backref,
514 * and it would require updating that lowest key either before write out or
515 * every time it changed. Instead, the objectid of the lowest key is stored
516 * along with the level of the tree block. This provides a hint
517 * about where in the btree the block can be found. Searches through the
518 * btree only need to look for a pointer to that block, so they stop one
519 * level higher than the level recorded in the backref.
521 * Some btrees do not do reference counting on their extents. These
522 * include the extent tree and the tree of tree roots. Backrefs for these
523 * trees always have a generation of zero.
525 * When a tree block is created, back references are inserted:
527 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
529 * When a tree block is cow'd in a reference counted root,
530 * new back references are added for all the blocks it points to.
531 * These are of the form (trans->transid will have increased since creation):
533 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
535 * Because the lowest_key_objectid and the level are just hints
536 * they are not used when backrefs are deleted. When a backref is deleted:
538 * if backref was for a tree root:
539 * root_objectid = root->root_key.objectid
541 * root_objectid = btrfs_header_owner(parent)
543 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
545 * Back Reference Key hashing:
547 * Back references have four fields, each 64 bits long. Unfortunately,
548 * This is hashed into a single 64 bit number and placed into the key offset.
549 * The key objectid corresponds to the first byte in the extent, and the
550 * key type is set to BTRFS_EXTENT_REF_KEY
552 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
553 struct btrfs_root *root,
554 struct btrfs_path *path, u64 bytenr,
555 u64 root_objectid, u64 ref_generation,
556 u64 owner, u64 owner_offset)
559 struct btrfs_key key;
560 struct btrfs_extent_ref ref;
561 struct btrfs_extent_ref *disk_ref;
564 btrfs_set_stack_ref_root(&ref, root_objectid);
565 btrfs_set_stack_ref_generation(&ref, ref_generation);
566 btrfs_set_stack_ref_objectid(&ref, owner);
567 btrfs_set_stack_ref_offset(&ref, owner_offset);
569 hash = hash_extent_ref(root_objectid, ref_generation, owner,
572 key.objectid = bytenr;
573 key.type = BTRFS_EXTENT_REF_KEY;
575 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
576 while (ret == -EEXIST) {
577 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
578 struct btrfs_extent_ref);
579 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
582 btrfs_release_path(root, path);
583 ret = btrfs_insert_empty_item(trans, root, path, &key,
588 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
589 struct btrfs_extent_ref);
590 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
592 btrfs_mark_buffer_dirty(path->nodes[0]);
594 btrfs_release_path(root, path);
598 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
600 u64 bytenr, u64 num_bytes,
601 u64 root_objectid, u64 ref_generation,
602 u64 owner, u64 owner_offset)
604 struct btrfs_path *path;
606 struct btrfs_key key;
607 struct extent_buffer *l;
608 struct btrfs_extent_item *item;
611 WARN_ON(num_bytes < root->sectorsize);
612 path = btrfs_alloc_path();
616 key.objectid = bytenr;
617 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
618 key.offset = num_bytes;
619 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
628 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
629 refs = btrfs_extent_refs(l, item);
630 btrfs_set_extent_refs(l, item, refs + 1);
631 btrfs_mark_buffer_dirty(path->nodes[0]);
633 btrfs_release_path(root->fs_info->extent_root, path);
635 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
636 path, bytenr, root_objectid,
637 ref_generation, owner, owner_offset);
639 finish_current_insert(trans, root->fs_info->extent_root);
640 del_pending_extents(trans, root->fs_info->extent_root);
642 btrfs_free_path(path);
646 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
647 struct btrfs_root *root)
649 finish_current_insert(trans, root->fs_info->extent_root);
650 del_pending_extents(trans, root->fs_info->extent_root);
654 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
655 struct btrfs_root *root, u64 bytenr,
656 u64 num_bytes, u32 *refs)
658 struct btrfs_path *path;
660 struct btrfs_key key;
661 struct extent_buffer *l;
662 struct btrfs_extent_item *item;
664 WARN_ON(num_bytes < root->sectorsize);
665 path = btrfs_alloc_path();
666 key.objectid = bytenr;
667 key.offset = num_bytes;
668 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
669 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
674 btrfs_print_leaf(root, path->nodes[0]);
675 printk("failed to find block number %Lu\n", bytenr);
679 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
680 *refs = btrfs_extent_refs(l, item);
682 btrfs_free_path(path);
686 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
687 struct btrfs_path *count_path,
690 struct btrfs_root *extent_root = root->fs_info->extent_root;
691 struct btrfs_path *path;
694 u64 root_objectid = root->root_key.objectid;
700 struct btrfs_key key;
701 struct btrfs_key found_key;
702 struct extent_buffer *l;
703 struct btrfs_extent_item *item;
704 struct btrfs_extent_ref *ref_item;
707 path = btrfs_alloc_path();
710 bytenr = first_extent;
712 bytenr = count_path->nodes[level]->start;
715 key.objectid = bytenr;
718 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
719 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
725 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
727 if (found_key.objectid != bytenr ||
728 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
732 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
733 refs = btrfs_extent_refs(l, item);
735 nritems = btrfs_header_nritems(l);
736 if (path->slots[0] >= nritems) {
737 ret = btrfs_next_leaf(extent_root, path);
742 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
743 if (found_key.objectid != bytenr)
745 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
751 ref_item = btrfs_item_ptr(l, path->slots[0],
752 struct btrfs_extent_ref);
753 found_objectid = btrfs_ref_root(l, ref_item);
755 if (found_objectid != root_objectid) {
762 if (cur_count == 0) {
766 if (level >= 0 && root->node == count_path->nodes[level])
769 btrfs_release_path(root, path);
773 btrfs_free_path(path);
778 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
779 struct btrfs_root *root, u64 owner_objectid)
785 struct btrfs_disk_key disk_key;
787 level = btrfs_header_level(root->node);
788 generation = trans->transid;
789 nritems = btrfs_header_nritems(root->node);
792 btrfs_item_key(root->node, &disk_key, 0);
794 btrfs_node_key(root->node, &disk_key, 0);
795 key_objectid = btrfs_disk_key_objectid(&disk_key);
799 return btrfs_inc_extent_ref(trans, root, root->node->start,
800 root->node->len, owner_objectid,
801 generation, level, key_objectid);
804 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
805 struct extent_buffer *buf)
809 struct btrfs_key key;
810 struct btrfs_file_extent_item *fi;
819 level = btrfs_header_level(buf);
820 nritems = btrfs_header_nritems(buf);
821 for (i = 0; i < nritems; i++) {
824 btrfs_item_key_to_cpu(buf, &key, i);
825 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
827 fi = btrfs_item_ptr(buf, i,
828 struct btrfs_file_extent_item);
829 if (btrfs_file_extent_type(buf, fi) ==
830 BTRFS_FILE_EXTENT_INLINE)
832 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
833 if (disk_bytenr == 0)
835 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
836 btrfs_file_extent_disk_num_bytes(buf, fi),
837 root->root_key.objectid, trans->transid,
838 key.objectid, key.offset);
844 bytenr = btrfs_node_blockptr(buf, i);
845 btrfs_node_key_to_cpu(buf, &key, i);
846 ret = btrfs_inc_extent_ref(trans, root, bytenr,
847 btrfs_level_size(root, level - 1),
848 root->root_key.objectid,
850 level - 1, key.objectid);
861 for (i =0; i < faili; i++) {
864 btrfs_item_key_to_cpu(buf, &key, i);
865 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
867 fi = btrfs_item_ptr(buf, i,
868 struct btrfs_file_extent_item);
869 if (btrfs_file_extent_type(buf, fi) ==
870 BTRFS_FILE_EXTENT_INLINE)
872 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
873 if (disk_bytenr == 0)
875 err = btrfs_free_extent(trans, root, disk_bytenr,
876 btrfs_file_extent_disk_num_bytes(buf,
880 bytenr = btrfs_node_blockptr(buf, i);
881 err = btrfs_free_extent(trans, root, bytenr,
882 btrfs_level_size(root, level - 1), 0);
890 static int write_one_cache_group(struct btrfs_trans_handle *trans,
891 struct btrfs_root *root,
892 struct btrfs_path *path,
893 struct btrfs_block_group_cache *cache)
897 struct btrfs_root *extent_root = root->fs_info->extent_root;
899 struct extent_buffer *leaf;
901 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
906 leaf = path->nodes[0];
907 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
908 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
909 btrfs_mark_buffer_dirty(leaf);
910 btrfs_release_path(extent_root, path);
912 finish_current_insert(trans, extent_root);
913 pending_ret = del_pending_extents(trans, extent_root);
922 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
923 struct btrfs_root *root)
925 struct extent_map_tree *block_group_cache;
926 struct btrfs_block_group_cache *cache;
930 struct btrfs_path *path;
936 block_group_cache = &root->fs_info->block_group_cache;
937 path = btrfs_alloc_path();
942 ret = find_first_extent_bit(block_group_cache, last,
943 &start, &end, BLOCK_GROUP_DIRTY);
948 ret = get_state_private(block_group_cache, start, &ptr);
952 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
953 err = write_one_cache_group(trans, root,
956 * if we fail to write the cache group, we want
957 * to keep it marked dirty in hopes that a later
964 clear_extent_bits(block_group_cache, start, end,
965 BLOCK_GROUP_DIRTY, GFP_NOFS);
967 btrfs_free_path(path);
971 static int update_block_group(struct btrfs_trans_handle *trans,
972 struct btrfs_root *root,
973 u64 bytenr, u64 num_bytes, int alloc,
974 int mark_free, int data)
976 struct btrfs_block_group_cache *cache;
977 struct btrfs_fs_info *info = root->fs_info;
978 u64 total = num_bytes;
985 cache = btrfs_lookup_block_group(info, bytenr);
989 byte_in_group = bytenr - cache->key.objectid;
990 WARN_ON(byte_in_group > cache->key.offset);
991 start = cache->key.objectid;
992 end = start + cache->key.offset - 1;
993 set_extent_bits(&info->block_group_cache, start, end,
994 BLOCK_GROUP_DIRTY, GFP_NOFS);
996 old_val = btrfs_block_group_used(&cache->item);
997 num_bytes = min(total, cache->key.offset - byte_in_group);
999 if (cache->data != data &&
1000 old_val < (cache->key.offset >> 1)) {
1005 bit_to_clear = BLOCK_GROUP_METADATA;
1006 bit_to_set = BLOCK_GROUP_DATA;
1007 cache->item.flags &=
1008 ~BTRFS_BLOCK_GROUP_MIXED;
1009 cache->item.flags |=
1010 BTRFS_BLOCK_GROUP_DATA;
1012 bit_to_clear = BLOCK_GROUP_DATA;
1013 bit_to_set = BLOCK_GROUP_METADATA;
1014 cache->item.flags &=
1015 ~BTRFS_BLOCK_GROUP_MIXED;
1016 cache->item.flags &=
1017 ~BTRFS_BLOCK_GROUP_DATA;
1019 clear_extent_bits(&info->block_group_cache,
1020 start, end, bit_to_clear,
1022 set_extent_bits(&info->block_group_cache,
1023 start, end, bit_to_set,
1025 } else if (cache->data != data &&
1026 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1027 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1028 set_extent_bits(&info->block_group_cache,
1031 BLOCK_GROUP_METADATA,
1034 old_val += num_bytes;
1036 old_val -= num_bytes;
1038 set_extent_dirty(&info->free_space_cache,
1039 bytenr, bytenr + num_bytes - 1,
1043 btrfs_set_block_group_used(&cache->item, old_val);
1045 bytenr += num_bytes;
1049 static int update_pinned_extents(struct btrfs_root *root,
1050 u64 bytenr, u64 num, int pin)
1053 struct btrfs_block_group_cache *cache;
1054 struct btrfs_fs_info *fs_info = root->fs_info;
1057 set_extent_dirty(&fs_info->pinned_extents,
1058 bytenr, bytenr + num - 1, GFP_NOFS);
1060 clear_extent_dirty(&fs_info->pinned_extents,
1061 bytenr, bytenr + num - 1, GFP_NOFS);
1064 cache = btrfs_lookup_block_group(fs_info, bytenr);
1066 len = min(num, cache->key.offset -
1067 (bytenr - cache->key.objectid));
1069 cache->pinned += len;
1070 fs_info->total_pinned += len;
1072 cache->pinned -= len;
1073 fs_info->total_pinned -= len;
1081 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
1086 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
1090 ret = find_first_extent_bit(pinned_extents, last,
1091 &start, &end, EXTENT_DIRTY);
1094 set_extent_dirty(copy, start, end, GFP_NOFS);
1100 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1101 struct btrfs_root *root,
1102 struct extent_map_tree *unpin)
1107 struct extent_map_tree *free_space_cache;
1108 free_space_cache = &root->fs_info->free_space_cache;
1111 ret = find_first_extent_bit(unpin, 0, &start, &end,
1115 update_pinned_extents(root, start, end + 1 - start, 0);
1116 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1117 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1122 static int finish_current_insert(struct btrfs_trans_handle *trans,
1123 struct btrfs_root *extent_root)
1127 struct btrfs_fs_info *info = extent_root->fs_info;
1128 struct extent_buffer *eb;
1129 struct btrfs_path *path;
1130 struct btrfs_key ins;
1131 struct btrfs_disk_key first;
1132 struct btrfs_extent_item extent_item;
1137 btrfs_set_stack_extent_refs(&extent_item, 1);
1138 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1139 path = btrfs_alloc_path();
1142 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1143 &end, EXTENT_LOCKED);
1147 ins.objectid = start;
1148 ins.offset = end + 1 - start;
1149 err = btrfs_insert_item(trans, extent_root, &ins,
1150 &extent_item, sizeof(extent_item));
1151 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1153 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1154 level = btrfs_header_level(eb);
1156 btrfs_item_key(eb, &first, 0);
1158 btrfs_node_key(eb, &first, 0);
1160 err = btrfs_insert_extent_backref(trans, extent_root, path,
1161 start, extent_root->root_key.objectid,
1163 btrfs_disk_key_objectid(&first));
1165 free_extent_buffer(eb);
1167 btrfs_free_path(path);
1171 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1175 struct extent_buffer *buf;
1178 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1180 if (btrfs_buffer_uptodate(buf)) {
1182 root->fs_info->running_transaction->transid;
1183 if (btrfs_header_generation(buf) == transid) {
1184 free_extent_buffer(buf);
1188 free_extent_buffer(buf);
1190 update_pinned_extents(root, bytenr, num_bytes, 1);
1192 set_extent_bits(&root->fs_info->pending_del,
1193 bytenr, bytenr + num_bytes - 1,
1194 EXTENT_LOCKED, GFP_NOFS);
1201 * remove an extent from the root, returns 0 on success
1203 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1204 *root, u64 bytenr, u64 num_bytes,
1205 u64 root_objectid, u64 ref_generation,
1206 u64 owner_objectid, u64 owner_offset, int pin,
1209 struct btrfs_path *path;
1210 struct btrfs_key key;
1211 struct btrfs_fs_info *info = root->fs_info;
1212 struct btrfs_root *extent_root = info->extent_root;
1213 struct extent_buffer *leaf;
1215 struct btrfs_extent_item *ei;
1218 key.objectid = bytenr;
1219 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1220 key.offset = num_bytes;
1222 path = btrfs_alloc_path();
1226 ret = lookup_extent_backref(trans, extent_root, path,
1227 bytenr, root_objectid,
1229 owner_objectid, owner_offset, 1);
1231 ret = btrfs_del_item(trans, extent_root, path);
1233 btrfs_print_leaf(extent_root, path->nodes[0]);
1235 printk("Unable to find ref byte nr %Lu root %Lu "
1236 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1237 root_objectid, ref_generation, owner_objectid,
1240 btrfs_release_path(extent_root, path);
1241 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1246 leaf = path->nodes[0];
1247 ei = btrfs_item_ptr(leaf, path->slots[0],
1248 struct btrfs_extent_item);
1249 refs = btrfs_extent_refs(leaf, ei);
1252 btrfs_set_extent_refs(leaf, ei, refs);
1253 btrfs_mark_buffer_dirty(leaf);
1260 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1266 /* block accounting for super block */
1267 super_used = btrfs_super_bytes_used(&info->super_copy);
1268 btrfs_set_super_bytes_used(&info->super_copy,
1269 super_used - num_bytes);
1271 /* block accounting for root item */
1272 root_used = btrfs_root_used(&root->root_item);
1273 btrfs_set_root_used(&root->root_item,
1274 root_used - num_bytes);
1276 ret = btrfs_del_item(trans, extent_root, path);
1280 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1284 btrfs_free_path(path);
1285 finish_current_insert(trans, extent_root);
1290 * find all the blocks marked as pending in the radix tree and remove
1291 * them from the extent map
1293 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1294 btrfs_root *extent_root)
1300 struct extent_map_tree *pending_del;
1301 struct extent_map_tree *pinned_extents;
1303 pending_del = &extent_root->fs_info->pending_del;
1304 pinned_extents = &extent_root->fs_info->pinned_extents;
1307 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1311 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1312 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1314 ret = __free_extent(trans, extent_root,
1315 start, end + 1 - start,
1316 extent_root->root_key.objectid,
1325 * remove an extent from the root, returns 0 on success
1327 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1328 *root, u64 bytenr, u64 num_bytes,
1329 u64 root_objectid, u64 ref_generation,
1330 u64 owner_objectid, u64 owner_offset, int pin)
1332 struct btrfs_root *extent_root = root->fs_info->extent_root;
1336 WARN_ON(num_bytes < root->sectorsize);
1337 if (!root->ref_cows)
1340 if (root == extent_root) {
1341 pin_down_bytes(root, bytenr, num_bytes, 1);
1344 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1345 ref_generation, owner_objectid, owner_offset,
1347 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1348 return ret ? ret : pending_ret;
1351 static u64 stripe_align(struct btrfs_root *root, u64 val)
1353 u64 mask = ((u64)root->stripesize - 1);
1354 u64 ret = (val + mask) & ~mask;
1359 * walks the btree of allocated extents and find a hole of a given size.
1360 * The key ins is changed to record the hole:
1361 * ins->objectid == block start
1362 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1363 * ins->offset == number of blocks
1364 * Any available blocks before search_start are skipped.
1366 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1367 struct btrfs_root *orig_root,
1368 u64 num_bytes, u64 empty_size,
1369 u64 search_start, u64 search_end,
1370 u64 hint_byte, struct btrfs_key *ins,
1371 u64 exclude_start, u64 exclude_nr,
1374 struct btrfs_path *path;
1375 struct btrfs_key key;
1381 u64 orig_search_start = search_start;
1383 struct extent_buffer *l;
1384 struct btrfs_root * root = orig_root->fs_info->extent_root;
1385 struct btrfs_fs_info *info = root->fs_info;
1386 u64 total_needed = num_bytes;
1388 struct btrfs_block_group_cache *block_group;
1393 WARN_ON(num_bytes < root->sectorsize);
1394 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1396 level = btrfs_header_level(root->node);
1398 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
1399 data = BTRFS_BLOCK_GROUP_MIXED;
1402 if (search_end == (u64)-1)
1403 search_end = btrfs_super_total_bytes(&info->super_copy);
1405 block_group = btrfs_lookup_block_group(info, hint_byte);
1407 hint_byte = search_start;
1408 block_group = btrfs_find_block_group(root, block_group,
1409 hint_byte, data, 1);
1411 block_group = btrfs_find_block_group(root,
1413 search_start, data, 1);
1416 total_needed += empty_size;
1417 path = btrfs_alloc_path();
1420 block_group = btrfs_lookup_block_group(info, search_start);
1422 block_group = btrfs_lookup_block_group(info,
1425 search_start = find_search_start(root, &block_group, search_start,
1426 total_needed, data, full_scan);
1427 search_start = stripe_align(root, search_start);
1428 cached_start = search_start;
1429 btrfs_init_path(path);
1430 ins->objectid = search_start;
1435 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1439 if (path->slots[0] > 0) {
1444 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1447 * walk backwards to find the first extent item key
1449 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1450 if (path->slots[0] == 0) {
1451 ret = btrfs_prev_leaf(root, path);
1453 ret = btrfs_search_slot(trans, root, ins,
1457 if (path->slots[0] > 0)
1465 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1469 slot = path->slots[0];
1470 if (slot >= btrfs_header_nritems(l)) {
1471 ret = btrfs_next_leaf(root, path);
1477 search_start = max(search_start,
1478 block_group->key.objectid);
1480 aligned = stripe_align(root, search_start);
1481 ins->objectid = aligned;
1482 if (aligned >= search_end) {
1486 ins->offset = search_end - aligned;
1490 ins->objectid = stripe_align(root,
1491 last_byte > search_start ?
1492 last_byte : search_start);
1493 if (search_end <= ins->objectid) {
1497 ins->offset = search_end - ins->objectid;
1498 BUG_ON(ins->objectid >= search_end);
1501 btrfs_item_key_to_cpu(l, &key, slot);
1503 if (key.objectid >= search_start && key.objectid > last_byte &&
1505 if (last_byte < search_start)
1506 last_byte = search_start;
1507 aligned = stripe_align(root, last_byte);
1508 hole_size = key.objectid - aligned;
1509 if (key.objectid > aligned && hole_size >= num_bytes) {
1510 ins->objectid = aligned;
1511 ins->offset = hole_size;
1515 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1516 if (!start_found && btrfs_key_type(&key) ==
1517 BTRFS_BLOCK_GROUP_ITEM_KEY) {
1518 last_byte = key.objectid;
1526 last_byte = key.objectid + key.offset;
1528 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1529 last_byte >= block_group->key.objectid +
1530 block_group->key.offset) {
1531 btrfs_release_path(root, path);
1532 search_start = block_group->key.objectid +
1533 block_group->key.offset;
1541 /* we have to make sure we didn't find an extent that has already
1542 * been allocated by the map tree or the original allocation
1544 btrfs_release_path(root, path);
1545 BUG_ON(ins->objectid < search_start);
1547 if (ins->objectid + num_bytes >= search_end)
1549 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1550 ins->objectid + num_bytes > block_group->
1551 key.objectid + block_group->key.offset) {
1552 search_start = block_group->key.objectid +
1553 block_group->key.offset;
1556 if (test_range_bit(&info->extent_ins, ins->objectid,
1557 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1558 search_start = ins->objectid + num_bytes;
1561 if (test_range_bit(&info->pinned_extents, ins->objectid,
1562 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1563 search_start = ins->objectid + num_bytes;
1566 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1567 ins->objectid < exclude_start + exclude_nr)) {
1568 search_start = exclude_start + exclude_nr;
1572 block_group = btrfs_lookup_block_group(info, ins->objectid);
1574 trans->block_group = block_group;
1576 ins->offset = num_bytes;
1577 btrfs_free_path(path);
1581 if (search_start + num_bytes >= search_end) {
1583 search_start = orig_search_start;
1590 total_needed -= empty_size;
1592 data = BTRFS_BLOCK_GROUP_MIXED;
1596 block_group = btrfs_lookup_block_group(info, search_start);
1598 block_group = btrfs_find_block_group(root, block_group,
1599 search_start, data, 0);
1603 btrfs_release_path(root, path);
1604 btrfs_free_path(path);
1608 * finds a free extent and does all the dirty work required for allocation
1609 * returns the key for the extent through ins, and a tree buffer for
1610 * the first block of the extent through buf.
1612 * returns 0 if everything worked, non-zero otherwise.
1614 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root,
1616 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1617 u64 owner, u64 owner_offset,
1618 u64 empty_size, u64 hint_byte,
1619 u64 search_end, struct btrfs_key *ins, int data)
1623 u64 super_used, root_used;
1624 u64 search_start = 0;
1628 struct btrfs_fs_info *info = root->fs_info;
1629 struct btrfs_root *extent_root = info->extent_root;
1630 struct btrfs_extent_item extent_item;
1631 struct btrfs_path *path;
1633 btrfs_set_stack_extent_refs(&extent_item, 1);
1636 new_hint = max(hint_byte, root->fs_info->alloc_start);
1637 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1638 hint_byte = new_hint;
1641 WARN_ON(num_bytes < root->sectorsize);
1642 ret = find_free_extent(trans, root, num_bytes, empty_size,
1643 search_start, search_end, hint_byte, ins,
1644 trans->alloc_exclude_start,
1645 trans->alloc_exclude_nr, data);
1650 /* block accounting for super block */
1651 super_used = btrfs_super_bytes_used(&info->super_copy);
1652 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1654 /* block accounting for root item */
1655 root_used = btrfs_root_used(&root->root_item);
1656 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1658 clear_extent_dirty(&root->fs_info->free_space_cache,
1659 ins->objectid, ins->objectid + ins->offset - 1,
1662 if (root == extent_root) {
1663 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1664 ins->objectid + ins->offset - 1,
1665 EXTENT_LOCKED, GFP_NOFS);
1670 WARN_ON(trans->alloc_exclude_nr);
1671 trans->alloc_exclude_start = ins->objectid;
1672 trans->alloc_exclude_nr = ins->offset;
1673 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1674 sizeof(extent_item));
1676 trans->alloc_exclude_start = 0;
1677 trans->alloc_exclude_nr = 0;
1680 path = btrfs_alloc_path();
1682 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1683 ins->objectid, root_objectid,
1684 ref_generation, owner, owner_offset);
1687 btrfs_free_path(path);
1688 finish_current_insert(trans, extent_root);
1689 pending_ret = del_pending_extents(trans, extent_root);
1699 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1706 * helper function to allocate a block for a given tree
1707 * returns the tree buffer or NULL.
1709 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1710 struct btrfs_root *root,
1712 u64 root_objectid, u64 hint,
1718 ref_generation = trans->transid;
1723 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1724 ref_generation, 0, 0, hint, empty_size);
1728 * helper function to allocate a block for a given tree
1729 * returns the tree buffer or NULL.
1731 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1732 struct btrfs_root *root,
1741 struct btrfs_key ins;
1743 struct extent_buffer *buf;
1745 ret = btrfs_alloc_extent(trans, root, blocksize,
1746 root_objectid, ref_generation,
1747 level, first_objectid, empty_size, hint,
1751 return ERR_PTR(ret);
1753 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1755 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1756 root->root_key.objectid, ref_generation,
1758 return ERR_PTR(-ENOMEM);
1760 btrfs_set_buffer_uptodate(buf);
1762 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1763 buf->start + buf->len - 1, GFP_NOFS);
1764 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1765 buf->start, buf->start + buf->len - 1,
1766 EXTENT_CSUM, GFP_NOFS);
1767 buf->flags |= EXTENT_CSUM;
1768 btrfs_set_buffer_defrag(buf);
1770 trans->blocks_used++;
1774 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1775 struct btrfs_root *root,
1776 struct extent_buffer *leaf)
1779 u64 leaf_generation;
1780 struct btrfs_key key;
1781 struct btrfs_file_extent_item *fi;
1786 BUG_ON(!btrfs_is_leaf(leaf));
1787 nritems = btrfs_header_nritems(leaf);
1788 leaf_owner = btrfs_header_owner(leaf);
1789 leaf_generation = btrfs_header_generation(leaf);
1791 for (i = 0; i < nritems; i++) {
1794 btrfs_item_key_to_cpu(leaf, &key, i);
1795 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1797 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1798 if (btrfs_file_extent_type(leaf, fi) ==
1799 BTRFS_FILE_EXTENT_INLINE)
1802 * FIXME make sure to insert a trans record that
1803 * repeats the snapshot del on crash
1805 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1806 if (disk_bytenr == 0)
1808 ret = btrfs_free_extent(trans, root, disk_bytenr,
1809 btrfs_file_extent_disk_num_bytes(leaf, fi),
1810 leaf_owner, leaf_generation,
1811 key.objectid, key.offset, 0);
1817 static void noinline reada_walk_down(struct btrfs_root *root,
1818 struct extent_buffer *node)
1828 nritems = btrfs_header_nritems(node);
1829 level = btrfs_header_level(node);
1830 for (i = 0; i < nritems; i++) {
1831 bytenr = btrfs_node_blockptr(node, i);
1832 blocksize = btrfs_level_size(root, level - 1);
1833 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1837 mutex_unlock(&root->fs_info->fs_mutex);
1838 ret = readahead_tree_block(root, bytenr, blocksize);
1840 mutex_lock(&root->fs_info->fs_mutex);
1847 * helper function for drop_snapshot, this walks down the tree dropping ref
1848 * counts as it goes.
1850 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 struct btrfs_path *path, int *level)
1857 struct extent_buffer *next;
1858 struct extent_buffer *cur;
1859 struct extent_buffer *parent;
1864 WARN_ON(*level < 0);
1865 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1866 ret = lookup_extent_ref(trans, root,
1867 path->nodes[*level]->start,
1868 path->nodes[*level]->len, &refs);
1874 * walk down to the last node level and free all the leaves
1876 while(*level >= 0) {
1877 WARN_ON(*level < 0);
1878 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1879 cur = path->nodes[*level];
1881 if (*level > 0 && path->slots[*level] == 0)
1882 reada_walk_down(root, cur);
1884 if (btrfs_header_level(cur) != *level)
1887 if (path->slots[*level] >=
1888 btrfs_header_nritems(cur))
1891 ret = drop_leaf_ref(trans, root, cur);
1895 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1896 blocksize = btrfs_level_size(root, *level - 1);
1897 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1900 parent = path->nodes[*level];
1901 root_owner = btrfs_header_owner(parent);
1902 root_gen = btrfs_header_generation(parent);
1903 path->slots[*level]++;
1904 ret = btrfs_free_extent(trans, root, bytenr,
1905 blocksize, root_owner,
1910 next = btrfs_find_tree_block(root, bytenr, blocksize);
1911 if (!next || !btrfs_buffer_uptodate(next)) {
1912 free_extent_buffer(next);
1913 mutex_unlock(&root->fs_info->fs_mutex);
1914 next = read_tree_block(root, bytenr, blocksize);
1915 mutex_lock(&root->fs_info->fs_mutex);
1917 /* we dropped the lock, check one more time */
1918 ret = lookup_extent_ref(trans, root, bytenr,
1922 parent = path->nodes[*level];
1923 root_owner = btrfs_header_owner(parent);
1924 root_gen = btrfs_header_generation(parent);
1926 path->slots[*level]++;
1927 free_extent_buffer(next);
1928 ret = btrfs_free_extent(trans, root, bytenr,
1936 WARN_ON(*level <= 0);
1937 if (path->nodes[*level-1])
1938 free_extent_buffer(path->nodes[*level-1]);
1939 path->nodes[*level-1] = next;
1940 *level = btrfs_header_level(next);
1941 path->slots[*level] = 0;
1944 WARN_ON(*level < 0);
1945 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1947 if (path->nodes[*level] == root->node) {
1948 root_owner = root->root_key.objectid;
1949 parent = path->nodes[*level];
1951 parent = path->nodes[*level + 1];
1952 root_owner = btrfs_header_owner(parent);
1955 root_gen = btrfs_header_generation(parent);
1956 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1957 path->nodes[*level]->len,
1958 root_owner, root_gen, 0, 0, 1);
1959 free_extent_buffer(path->nodes[*level]);
1960 path->nodes[*level] = NULL;
1967 * helper for dropping snapshots. This walks back up the tree in the path
1968 * to find the first node higher up where we haven't yet gone through
1971 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1972 struct btrfs_root *root,
1973 struct btrfs_path *path, int *level)
1977 struct btrfs_root_item *root_item = &root->root_item;
1982 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1983 slot = path->slots[i];
1984 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1985 struct extent_buffer *node;
1986 struct btrfs_disk_key disk_key;
1987 node = path->nodes[i];
1990 WARN_ON(*level == 0);
1991 btrfs_node_key(node, &disk_key, path->slots[i]);
1992 memcpy(&root_item->drop_progress,
1993 &disk_key, sizeof(disk_key));
1994 root_item->drop_level = i;
1997 if (path->nodes[*level] == root->node) {
1998 root_owner = root->root_key.objectid;
2000 btrfs_header_generation(path->nodes[*level]);
2002 struct extent_buffer *node;
2003 node = path->nodes[*level + 1];
2004 root_owner = btrfs_header_owner(node);
2005 root_gen = btrfs_header_generation(node);
2007 ret = btrfs_free_extent(trans, root,
2008 path->nodes[*level]->start,
2009 path->nodes[*level]->len,
2010 root_owner, root_gen, 0, 0, 1);
2012 free_extent_buffer(path->nodes[*level]);
2013 path->nodes[*level] = NULL;
2021 * drop the reference count on the tree rooted at 'snap'. This traverses
2022 * the tree freeing any blocks that have a ref count of zero after being
2025 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2031 struct btrfs_path *path;
2034 struct btrfs_root_item *root_item = &root->root_item;
2036 path = btrfs_alloc_path();
2039 level = btrfs_header_level(root->node);
2041 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2042 path->nodes[level] = root->node;
2043 extent_buffer_get(root->node);
2044 path->slots[level] = 0;
2046 struct btrfs_key key;
2047 struct btrfs_disk_key found_key;
2048 struct extent_buffer *node;
2050 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2051 level = root_item->drop_level;
2052 path->lowest_level = level;
2053 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2058 node = path->nodes[level];
2059 btrfs_node_key(node, &found_key, path->slots[level]);
2060 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2061 sizeof(found_key)));
2064 wret = walk_down_tree(trans, root, path, &level);
2070 wret = walk_up_tree(trans, root, path, &level);
2080 for (i = 0; i <= orig_level; i++) {
2081 if (path->nodes[i]) {
2082 free_extent_buffer(path->nodes[i]);
2083 path->nodes[i] = NULL;
2087 btrfs_free_path(path);
2091 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2098 ret = find_first_extent_bit(&info->block_group_cache, 0,
2099 &start, &end, (unsigned int)-1);
2102 ret = get_state_private(&info->block_group_cache, start, &ptr);
2104 kfree((void *)(unsigned long)ptr);
2105 clear_extent_bits(&info->block_group_cache, start,
2106 end, (unsigned int)-1, GFP_NOFS);
2109 ret = find_first_extent_bit(&info->free_space_cache, 0,
2110 &start, &end, EXTENT_DIRTY);
2113 clear_extent_dirty(&info->free_space_cache, start,
2119 int btrfs_read_block_groups(struct btrfs_root *root)
2121 struct btrfs_path *path;
2125 struct btrfs_block_group_cache *cache;
2126 struct btrfs_fs_info *info = root->fs_info;
2127 struct extent_map_tree *block_group_cache;
2128 struct btrfs_key key;
2129 struct btrfs_key found_key;
2130 struct extent_buffer *leaf;
2132 block_group_cache = &info->block_group_cache;
2134 root = info->extent_root;
2136 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2137 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2139 path = btrfs_alloc_path();
2144 ret = btrfs_search_slot(NULL, info->extent_root,
2150 leaf = path->nodes[0];
2151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2152 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2158 read_extent_buffer(leaf, &cache->item,
2159 btrfs_item_ptr_offset(leaf, path->slots[0]),
2160 sizeof(cache->item));
2161 memcpy(&cache->key, &found_key, sizeof(found_key));
2164 key.objectid = found_key.objectid + found_key.offset;
2165 btrfs_release_path(root, path);
2167 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2168 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2169 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2170 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2171 bit = BLOCK_GROUP_DATA;
2172 cache->data = BTRFS_BLOCK_GROUP_DATA;
2174 bit = BLOCK_GROUP_METADATA;
2178 /* use EXTENT_LOCKED to prevent merging */
2179 set_extent_bits(block_group_cache, found_key.objectid,
2180 found_key.objectid + found_key.offset - 1,
2181 bit | EXTENT_LOCKED, GFP_NOFS);
2182 set_state_private(block_group_cache, found_key.objectid,
2183 (unsigned long)cache);
2186 btrfs_super_total_bytes(&info->super_copy))
2190 btrfs_free_path(path);
2194 static int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
2195 struct btrfs_root *root,
2196 struct btrfs_key *key,
2197 struct btrfs_block_group_item *bi)
2201 struct btrfs_root *extent_root;
2203 extent_root = root->fs_info->extent_root;
2204 ret = btrfs_insert_item(trans, extent_root, key, bi, sizeof(*bi));
2205 finish_current_insert(trans, extent_root);
2206 pending_ret = del_pending_extents(trans, extent_root);
2214 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2215 struct btrfs_root *root)
2224 struct btrfs_root *extent_root;
2225 struct btrfs_block_group_cache *cache;
2226 struct extent_map_tree *block_group_cache;
2228 extent_root = root->fs_info->extent_root;
2229 block_group_cache = &root->fs_info->block_group_cache;
2230 group_size = BTRFS_BLOCK_GROUP_SIZE;
2231 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
2232 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2235 while (cur_start < total_bytes) {
2236 cache = malloc(sizeof(*cache));
2238 cache->key.objectid = cur_start;
2239 cache->key.offset = group_size;
2240 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2241 memset(&cache->item, 0, sizeof(cache->item));
2243 btrfs_set_block_group_used(&cache->item, bytes_used);
2245 bit = BLOCK_GROUP_DATA;
2247 cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;
2249 bit = BLOCK_GROUP_METADATA;
2253 set_extent_bits(block_group_cache, cur_start,
2254 cur_start + group_size - 1,
2255 bit | EXTENT_LOCKED, GFP_NOFS);
2256 set_state_private(block_group_cache, cur_start,
2257 (unsigned long)cache);
2258 cur_start += group_size;
2260 /* then insert all the items */
2262 while(cur_start < total_bytes) {
2263 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2265 ret = btrfs_insert_block_group(trans, root, &cache->key,
2268 cur_start += group_size;
2273 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
2274 u64 owner, u64 owner_offset)
2276 return hash_extent_ref(root_objectid, ref_generation,
2277 owner, owner_offset);
2280 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2281 struct btrfs_root *root,
2282 u64 bytenr, u64 num_bytes, int alloc,
2283 int mark_free, int data)
2285 return update_block_group(trans, root, bytenr, num_bytes,
2286 alloc, mark_free, data);