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_extent_ops *ops = info->extent_ops;
1213 struct btrfs_root *extent_root = info->extent_root;
1214 struct extent_buffer *leaf;
1216 struct btrfs_extent_item *ei;
1219 key.objectid = bytenr;
1220 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1221 key.offset = num_bytes;
1223 path = btrfs_alloc_path();
1227 ret = lookup_extent_backref(trans, extent_root, path,
1228 bytenr, root_objectid,
1230 owner_objectid, owner_offset, 1);
1232 ret = btrfs_del_item(trans, extent_root, path);
1234 btrfs_print_leaf(extent_root, path->nodes[0]);
1236 printk("Unable to find ref byte nr %Lu root %Lu "
1237 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1238 root_objectid, ref_generation, owner_objectid,
1241 btrfs_release_path(extent_root, path);
1242 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1247 leaf = path->nodes[0];
1248 ei = btrfs_item_ptr(leaf, path->slots[0],
1249 struct btrfs_extent_item);
1250 refs = btrfs_extent_refs(leaf, ei);
1253 btrfs_set_extent_refs(leaf, ei, refs);
1254 btrfs_mark_buffer_dirty(leaf);
1261 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1267 /* block accounting for super block */
1268 super_used = btrfs_super_bytes_used(&info->super_copy);
1269 btrfs_set_super_bytes_used(&info->super_copy,
1270 super_used - num_bytes);
1272 /* block accounting for root item */
1273 root_used = btrfs_root_used(&root->root_item);
1274 btrfs_set_root_used(&root->root_item,
1275 root_used - num_bytes);
1276 ret = btrfs_del_item(trans, extent_root, path);
1280 if (ops && ops->free_extent)
1281 ops->free_extent(root, bytenr, num_bytes);
1283 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1287 btrfs_free_path(path);
1288 finish_current_insert(trans, extent_root);
1293 * find all the blocks marked as pending in the radix tree and remove
1294 * them from the extent map
1296 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1297 btrfs_root *extent_root)
1303 struct extent_map_tree *pending_del;
1304 struct extent_map_tree *pinned_extents;
1306 pending_del = &extent_root->fs_info->pending_del;
1307 pinned_extents = &extent_root->fs_info->pinned_extents;
1310 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1314 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1315 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1317 ret = __free_extent(trans, extent_root,
1318 start, end + 1 - start,
1319 extent_root->root_key.objectid,
1328 * remove an extent from the root, returns 0 on success
1330 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1331 *root, u64 bytenr, u64 num_bytes,
1332 u64 root_objectid, u64 ref_generation,
1333 u64 owner_objectid, u64 owner_offset, int pin)
1335 struct btrfs_root *extent_root = root->fs_info->extent_root;
1339 WARN_ON(num_bytes < root->sectorsize);
1340 if (!root->ref_cows)
1343 if (root == extent_root) {
1344 pin_down_bytes(root, bytenr, num_bytes, 1);
1347 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1348 ref_generation, owner_objectid, owner_offset,
1350 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1351 return ret ? ret : pending_ret;
1354 static u64 stripe_align(struct btrfs_root *root, u64 val)
1356 u64 mask = ((u64)root->stripesize - 1);
1357 u64 ret = (val + mask) & ~mask;
1362 * walks the btree of allocated extents and find a hole of a given size.
1363 * The key ins is changed to record the hole:
1364 * ins->objectid == block start
1365 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1366 * ins->offset == number of blocks
1367 * Any available blocks before search_start are skipped.
1369 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *orig_root,
1371 u64 num_bytes, u64 empty_size,
1372 u64 search_start, u64 search_end,
1373 u64 hint_byte, struct btrfs_key *ins,
1374 u64 exclude_start, u64 exclude_nr,
1377 struct btrfs_path *path;
1378 struct btrfs_key key;
1384 u64 orig_search_start = search_start;
1386 struct extent_buffer *l;
1387 struct btrfs_root * root = orig_root->fs_info->extent_root;
1388 struct btrfs_fs_info *info = root->fs_info;
1389 u64 total_needed = num_bytes;
1391 struct btrfs_block_group_cache *block_group;
1396 WARN_ON(num_bytes < root->sectorsize);
1397 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1399 level = btrfs_header_level(root->node);
1401 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
1402 data = BTRFS_BLOCK_GROUP_MIXED;
1405 if (search_end == (u64)-1)
1406 search_end = btrfs_super_total_bytes(&info->super_copy);
1408 block_group = btrfs_lookup_block_group(info, hint_byte);
1410 hint_byte = search_start;
1411 block_group = btrfs_find_block_group(root, block_group,
1412 hint_byte, data, 1);
1414 block_group = btrfs_find_block_group(root,
1416 search_start, data, 1);
1419 total_needed += empty_size;
1420 path = btrfs_alloc_path();
1423 block_group = btrfs_lookup_block_group(info, search_start);
1425 block_group = btrfs_lookup_block_group(info,
1428 search_start = find_search_start(root, &block_group, search_start,
1429 total_needed, data, full_scan);
1430 search_start = stripe_align(root, search_start);
1431 cached_start = search_start;
1432 btrfs_init_path(path);
1433 ins->objectid = search_start;
1438 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1442 if (path->slots[0] > 0) {
1447 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1450 * walk backwards to find the first extent item key
1452 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1453 if (path->slots[0] == 0) {
1454 ret = btrfs_prev_leaf(root, path);
1456 ret = btrfs_search_slot(trans, root, ins,
1460 if (path->slots[0] > 0)
1468 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1472 slot = path->slots[0];
1473 if (slot >= btrfs_header_nritems(l)) {
1474 ret = btrfs_next_leaf(root, path);
1480 search_start = max(search_start,
1481 block_group->key.objectid);
1483 aligned = stripe_align(root, search_start);
1484 ins->objectid = aligned;
1485 if (aligned >= search_end) {
1489 ins->offset = search_end - aligned;
1493 ins->objectid = stripe_align(root,
1494 last_byte > search_start ?
1495 last_byte : search_start);
1496 if (search_end <= ins->objectid) {
1500 ins->offset = search_end - ins->objectid;
1501 BUG_ON(ins->objectid >= search_end);
1504 btrfs_item_key_to_cpu(l, &key, slot);
1506 if (key.objectid >= search_start && key.objectid > last_byte &&
1508 if (last_byte < search_start)
1509 last_byte = search_start;
1510 aligned = stripe_align(root, last_byte);
1511 hole_size = key.objectid - aligned;
1512 if (key.objectid > aligned && hole_size >= num_bytes) {
1513 ins->objectid = aligned;
1514 ins->offset = hole_size;
1518 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1519 if (!start_found && btrfs_key_type(&key) ==
1520 BTRFS_BLOCK_GROUP_ITEM_KEY) {
1521 last_byte = key.objectid;
1529 last_byte = key.objectid + key.offset;
1531 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1532 last_byte >= block_group->key.objectid +
1533 block_group->key.offset) {
1534 btrfs_release_path(root, path);
1535 search_start = block_group->key.objectid +
1536 block_group->key.offset;
1544 /* we have to make sure we didn't find an extent that has already
1545 * been allocated by the map tree or the original allocation
1547 btrfs_release_path(root, path);
1548 BUG_ON(ins->objectid < search_start);
1550 if (ins->objectid + num_bytes >= search_end)
1552 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1553 ins->objectid + num_bytes > block_group->
1554 key.objectid + block_group->key.offset) {
1555 search_start = block_group->key.objectid +
1556 block_group->key.offset;
1559 if (test_range_bit(&info->extent_ins, ins->objectid,
1560 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1561 search_start = ins->objectid + num_bytes;
1564 if (test_range_bit(&info->pinned_extents, ins->objectid,
1565 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1566 search_start = ins->objectid + num_bytes;
1569 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1570 ins->objectid < exclude_start + exclude_nr)) {
1571 search_start = exclude_start + exclude_nr;
1575 block_group = btrfs_lookup_block_group(info, ins->objectid);
1577 trans->block_group = block_group;
1579 ins->offset = num_bytes;
1580 btrfs_free_path(path);
1584 if (search_start + num_bytes >= search_end) {
1586 search_start = orig_search_start;
1593 total_needed -= empty_size;
1595 data = BTRFS_BLOCK_GROUP_MIXED;
1599 block_group = btrfs_lookup_block_group(info, search_start);
1601 block_group = btrfs_find_block_group(root, block_group,
1602 search_start, data, 0);
1606 btrfs_release_path(root, path);
1607 btrfs_free_path(path);
1611 * finds a free extent and does all the dirty work required for allocation
1612 * returns the key for the extent through ins, and a tree buffer for
1613 * the first block of the extent through buf.
1615 * returns 0 if everything worked, non-zero otherwise.
1617 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1618 struct btrfs_root *root,
1619 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1620 u64 owner, u64 owner_offset,
1621 u64 empty_size, u64 hint_byte,
1622 u64 search_end, struct btrfs_key *ins, int data)
1626 u64 super_used, root_used;
1627 u64 search_start = 0;
1631 struct btrfs_fs_info *info = root->fs_info;
1632 struct btrfs_extent_ops *ops = info->extent_ops;
1633 struct btrfs_root *extent_root = info->extent_root;
1634 struct btrfs_extent_item extent_item;
1635 struct btrfs_path *path;
1637 btrfs_set_stack_extent_refs(&extent_item, 1);
1640 new_hint = max(hint_byte, root->fs_info->alloc_start);
1641 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1642 hint_byte = new_hint;
1644 WARN_ON(num_bytes < root->sectorsize);
1645 if (ops && ops->alloc_extent) {
1646 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1648 ret = find_free_extent(trans, root, num_bytes, empty_size,
1649 search_start, search_end, hint_byte,
1650 ins, trans->alloc_exclude_start,
1651 trans->alloc_exclude_nr, data);
1657 /* block accounting for super block */
1658 super_used = btrfs_super_bytes_used(&info->super_copy);
1659 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1661 /* block accounting for root item */
1662 root_used = btrfs_root_used(&root->root_item);
1663 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1665 clear_extent_dirty(&root->fs_info->free_space_cache,
1666 ins->objectid, ins->objectid + ins->offset - 1,
1669 if (root == extent_root) {
1670 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1671 ins->objectid + ins->offset - 1,
1672 EXTENT_LOCKED, GFP_NOFS);
1677 WARN_ON(trans->alloc_exclude_nr);
1678 trans->alloc_exclude_start = ins->objectid;
1679 trans->alloc_exclude_nr = ins->offset;
1680 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1681 sizeof(extent_item));
1683 trans->alloc_exclude_start = 0;
1684 trans->alloc_exclude_nr = 0;
1687 path = btrfs_alloc_path();
1689 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1690 ins->objectid, root_objectid,
1691 ref_generation, owner, owner_offset);
1694 btrfs_free_path(path);
1695 finish_current_insert(trans, extent_root);
1696 pending_ret = del_pending_extents(trans, extent_root);
1706 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1713 * helper function to allocate a block for a given tree
1714 * returns the tree buffer or NULL.
1716 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1717 struct btrfs_root *root,
1719 u64 root_objectid, u64 hint,
1725 ref_generation = trans->transid;
1730 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1731 ref_generation, 0, 0, hint, empty_size);
1735 * helper function to allocate a block for a given tree
1736 * returns the tree buffer or NULL.
1738 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1739 struct btrfs_root *root,
1748 struct btrfs_key ins;
1750 struct extent_buffer *buf;
1752 ret = btrfs_alloc_extent(trans, root, blocksize,
1753 root_objectid, ref_generation,
1754 level, first_objectid, empty_size, hint,
1758 return ERR_PTR(ret);
1760 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1762 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1763 root->root_key.objectid, ref_generation,
1765 return ERR_PTR(-ENOMEM);
1767 btrfs_set_buffer_uptodate(buf);
1769 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1770 buf->start + buf->len - 1, GFP_NOFS);
1771 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1772 buf->start, buf->start + buf->len - 1,
1773 EXTENT_CSUM, GFP_NOFS);
1774 buf->flags |= EXTENT_CSUM;
1775 btrfs_set_buffer_defrag(buf);
1777 trans->blocks_used++;
1781 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1782 struct btrfs_root *root,
1783 struct extent_buffer *leaf)
1786 u64 leaf_generation;
1787 struct btrfs_key key;
1788 struct btrfs_file_extent_item *fi;
1793 BUG_ON(!btrfs_is_leaf(leaf));
1794 nritems = btrfs_header_nritems(leaf);
1795 leaf_owner = btrfs_header_owner(leaf);
1796 leaf_generation = btrfs_header_generation(leaf);
1798 for (i = 0; i < nritems; i++) {
1801 btrfs_item_key_to_cpu(leaf, &key, i);
1802 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1804 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1805 if (btrfs_file_extent_type(leaf, fi) ==
1806 BTRFS_FILE_EXTENT_INLINE)
1809 * FIXME make sure to insert a trans record that
1810 * repeats the snapshot del on crash
1812 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1813 if (disk_bytenr == 0)
1815 ret = btrfs_free_extent(trans, root, disk_bytenr,
1816 btrfs_file_extent_disk_num_bytes(leaf, fi),
1817 leaf_owner, leaf_generation,
1818 key.objectid, key.offset, 0);
1824 static void noinline reada_walk_down(struct btrfs_root *root,
1825 struct extent_buffer *node)
1835 nritems = btrfs_header_nritems(node);
1836 level = btrfs_header_level(node);
1837 for (i = 0; i < nritems; i++) {
1838 bytenr = btrfs_node_blockptr(node, i);
1839 blocksize = btrfs_level_size(root, level - 1);
1840 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1844 mutex_unlock(&root->fs_info->fs_mutex);
1845 ret = readahead_tree_block(root, bytenr, blocksize);
1847 mutex_lock(&root->fs_info->fs_mutex);
1854 * helper function for drop_snapshot, this walks down the tree dropping ref
1855 * counts as it goes.
1857 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 struct btrfs_path *path, int *level)
1864 struct extent_buffer *next;
1865 struct extent_buffer *cur;
1866 struct extent_buffer *parent;
1871 WARN_ON(*level < 0);
1872 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1873 ret = lookup_extent_ref(trans, root,
1874 path->nodes[*level]->start,
1875 path->nodes[*level]->len, &refs);
1881 * walk down to the last node level and free all the leaves
1883 while(*level >= 0) {
1884 WARN_ON(*level < 0);
1885 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1886 cur = path->nodes[*level];
1888 if (*level > 0 && path->slots[*level] == 0)
1889 reada_walk_down(root, cur);
1891 if (btrfs_header_level(cur) != *level)
1894 if (path->slots[*level] >=
1895 btrfs_header_nritems(cur))
1898 ret = drop_leaf_ref(trans, root, cur);
1902 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1903 blocksize = btrfs_level_size(root, *level - 1);
1904 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1907 parent = path->nodes[*level];
1908 root_owner = btrfs_header_owner(parent);
1909 root_gen = btrfs_header_generation(parent);
1910 path->slots[*level]++;
1911 ret = btrfs_free_extent(trans, root, bytenr,
1912 blocksize, root_owner,
1917 next = btrfs_find_tree_block(root, bytenr, blocksize);
1918 if (!next || !btrfs_buffer_uptodate(next)) {
1919 free_extent_buffer(next);
1920 mutex_unlock(&root->fs_info->fs_mutex);
1921 next = read_tree_block(root, bytenr, blocksize);
1922 mutex_lock(&root->fs_info->fs_mutex);
1924 /* we dropped the lock, check one more time */
1925 ret = lookup_extent_ref(trans, root, bytenr,
1929 parent = path->nodes[*level];
1930 root_owner = btrfs_header_owner(parent);
1931 root_gen = btrfs_header_generation(parent);
1933 path->slots[*level]++;
1934 free_extent_buffer(next);
1935 ret = btrfs_free_extent(trans, root, bytenr,
1943 WARN_ON(*level <= 0);
1944 if (path->nodes[*level-1])
1945 free_extent_buffer(path->nodes[*level-1]);
1946 path->nodes[*level-1] = next;
1947 *level = btrfs_header_level(next);
1948 path->slots[*level] = 0;
1951 WARN_ON(*level < 0);
1952 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1954 if (path->nodes[*level] == root->node) {
1955 root_owner = root->root_key.objectid;
1956 parent = path->nodes[*level];
1958 parent = path->nodes[*level + 1];
1959 root_owner = btrfs_header_owner(parent);
1962 root_gen = btrfs_header_generation(parent);
1963 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1964 path->nodes[*level]->len,
1965 root_owner, root_gen, 0, 0, 1);
1966 free_extent_buffer(path->nodes[*level]);
1967 path->nodes[*level] = NULL;
1974 * helper for dropping snapshots. This walks back up the tree in the path
1975 * to find the first node higher up where we haven't yet gone through
1978 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
1980 struct btrfs_path *path, int *level)
1984 struct btrfs_root_item *root_item = &root->root_item;
1989 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1990 slot = path->slots[i];
1991 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1992 struct extent_buffer *node;
1993 struct btrfs_disk_key disk_key;
1994 node = path->nodes[i];
1997 WARN_ON(*level == 0);
1998 btrfs_node_key(node, &disk_key, path->slots[i]);
1999 memcpy(&root_item->drop_progress,
2000 &disk_key, sizeof(disk_key));
2001 root_item->drop_level = i;
2004 if (path->nodes[*level] == root->node) {
2005 root_owner = root->root_key.objectid;
2007 btrfs_header_generation(path->nodes[*level]);
2009 struct extent_buffer *node;
2010 node = path->nodes[*level + 1];
2011 root_owner = btrfs_header_owner(node);
2012 root_gen = btrfs_header_generation(node);
2014 ret = btrfs_free_extent(trans, root,
2015 path->nodes[*level]->start,
2016 path->nodes[*level]->len,
2017 root_owner, root_gen, 0, 0, 1);
2019 free_extent_buffer(path->nodes[*level]);
2020 path->nodes[*level] = NULL;
2028 * drop the reference count on the tree rooted at 'snap'. This traverses
2029 * the tree freeing any blocks that have a ref count of zero after being
2032 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2038 struct btrfs_path *path;
2041 struct btrfs_root_item *root_item = &root->root_item;
2043 path = btrfs_alloc_path();
2046 level = btrfs_header_level(root->node);
2048 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2049 path->nodes[level] = root->node;
2050 extent_buffer_get(root->node);
2051 path->slots[level] = 0;
2053 struct btrfs_key key;
2054 struct btrfs_disk_key found_key;
2055 struct extent_buffer *node;
2057 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2058 level = root_item->drop_level;
2059 path->lowest_level = level;
2060 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2065 node = path->nodes[level];
2066 btrfs_node_key(node, &found_key, path->slots[level]);
2067 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2068 sizeof(found_key)));
2071 wret = walk_down_tree(trans, root, path, &level);
2077 wret = walk_up_tree(trans, root, path, &level);
2087 for (i = 0; i <= orig_level; i++) {
2088 if (path->nodes[i]) {
2089 free_extent_buffer(path->nodes[i]);
2090 path->nodes[i] = NULL;
2094 btrfs_free_path(path);
2098 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2105 ret = find_first_extent_bit(&info->block_group_cache, 0,
2106 &start, &end, (unsigned int)-1);
2109 ret = get_state_private(&info->block_group_cache, start, &ptr);
2111 kfree((void *)(unsigned long)ptr);
2112 clear_extent_bits(&info->block_group_cache, start,
2113 end, (unsigned int)-1, GFP_NOFS);
2116 ret = find_first_extent_bit(&info->free_space_cache, 0,
2117 &start, &end, EXTENT_DIRTY);
2120 clear_extent_dirty(&info->free_space_cache, start,
2126 int btrfs_read_block_groups(struct btrfs_root *root)
2128 struct btrfs_path *path;
2132 struct btrfs_block_group_cache *cache;
2133 struct btrfs_fs_info *info = root->fs_info;
2134 struct extent_map_tree *block_group_cache;
2135 struct btrfs_key key;
2136 struct btrfs_key found_key;
2137 struct extent_buffer *leaf;
2139 block_group_cache = &info->block_group_cache;
2141 root = info->extent_root;
2143 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2144 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2146 path = btrfs_alloc_path();
2151 ret = btrfs_search_slot(NULL, info->extent_root,
2157 leaf = path->nodes[0];
2158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2159 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2165 read_extent_buffer(leaf, &cache->item,
2166 btrfs_item_ptr_offset(leaf, path->slots[0]),
2167 sizeof(cache->item));
2168 memcpy(&cache->key, &found_key, sizeof(found_key));
2171 key.objectid = found_key.objectid + found_key.offset;
2172 btrfs_release_path(root, path);
2174 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2175 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2176 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2177 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2178 bit = BLOCK_GROUP_DATA;
2179 cache->data = BTRFS_BLOCK_GROUP_DATA;
2181 bit = BLOCK_GROUP_METADATA;
2185 /* use EXTENT_LOCKED to prevent merging */
2186 set_extent_bits(block_group_cache, found_key.objectid,
2187 found_key.objectid + found_key.offset - 1,
2188 bit | EXTENT_LOCKED, GFP_NOFS);
2189 set_state_private(block_group_cache, found_key.objectid,
2190 (unsigned long)cache);
2193 btrfs_super_total_bytes(&info->super_copy))
2197 btrfs_free_path(path);
2201 static int btrfs_insert_block_group(struct btrfs_trans_handle *trans,
2202 struct btrfs_root *root,
2203 struct btrfs_key *key,
2204 struct btrfs_block_group_item *bi)
2208 struct btrfs_root *extent_root;
2210 extent_root = root->fs_info->extent_root;
2211 ret = btrfs_insert_item(trans, extent_root, key, bi, sizeof(*bi));
2212 finish_current_insert(trans, extent_root);
2213 pending_ret = del_pending_extents(trans, extent_root);
2221 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2222 struct btrfs_root *root)
2231 struct btrfs_root *extent_root;
2232 struct btrfs_block_group_cache *cache;
2233 struct extent_map_tree *block_group_cache;
2235 extent_root = root->fs_info->extent_root;
2236 block_group_cache = &root->fs_info->block_group_cache;
2237 group_size = BTRFS_BLOCK_GROUP_SIZE;
2238 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
2239 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2242 while (cur_start < total_bytes) {
2243 cache = malloc(sizeof(*cache));
2245 cache->key.objectid = cur_start;
2246 cache->key.offset = group_size;
2247 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2248 memset(&cache->item, 0, sizeof(cache->item));
2250 btrfs_set_block_group_used(&cache->item, bytes_used);
2252 bit = BLOCK_GROUP_DATA;
2254 cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;
2256 bit = BLOCK_GROUP_METADATA;
2260 set_extent_bits(block_group_cache, cur_start,
2261 cur_start + group_size - 1,
2262 bit | EXTENT_LOCKED, GFP_NOFS);
2263 set_state_private(block_group_cache, cur_start,
2264 (unsigned long)cache);
2265 cur_start += group_size;
2267 /* then insert all the items */
2269 while(cur_start < total_bytes) {
2270 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2272 ret = btrfs_insert_block_group(trans, root, &cache->key,
2275 cur_start += group_size;
2280 u64 btrfs_hash_extent_ref(u64 root_objectid, u64 ref_generation,
2281 u64 owner, u64 owner_offset)
2283 return hash_extent_ref(root_objectid, ref_generation,
2284 owner, owner_offset);
2287 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2288 struct btrfs_root *root,
2289 u64 bytenr, u64 num_bytes, int alloc,
2290 int mark_free, int data)
2292 return update_block_group(trans, root, bytenr, num_bytes,
2293 alloc, mark_free, data);