2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/crc32c.h>
24 #include "print-tree.h"
25 #include "transaction.h"
27 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
28 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
29 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
31 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
33 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
34 btrfs_root *extent_root);
36 static int cache_block_group(struct btrfs_root *root,
37 struct btrfs_block_group_cache *block_group)
39 struct btrfs_path *path;
42 struct extent_buffer *leaf;
43 struct extent_map_tree *free_space_cache;
53 root = root->fs_info->extent_root;
54 free_space_cache = &root->fs_info->free_space_cache;
56 if (block_group->cached)
59 path = btrfs_alloc_path();
64 first_free = block_group->key.objectid;
65 key.objectid = block_group->key.objectid;
68 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
69 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret && path->slots[0] > 0)
78 leaf = path->nodes[0];
79 slot = path->slots[0];
80 if (slot >= btrfs_header_nritems(leaf)) {
81 ret = btrfs_next_leaf(root, path);
91 btrfs_item_key_to_cpu(leaf, &key, slot);
92 if (key.objectid < block_group->key.objectid) {
93 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
94 key.objectid + key.offset > first_free)
95 first_free = key.objectid + key.offset;
99 if (key.objectid >= block_group->key.objectid +
100 block_group->key.offset) {
104 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
109 if (key.objectid > last) {
110 hole_size = key.objectid - last;
111 set_extent_dirty(free_space_cache, last,
112 last + hole_size - 1,
115 last = key.objectid + key.offset;
123 if (block_group->key.objectid +
124 block_group->key.offset > last) {
125 hole_size = block_group->key.objectid +
126 block_group->key.offset - last;
127 set_extent_dirty(free_space_cache, last,
128 last + hole_size - 1, GFP_NOFS);
130 block_group->cached = 1;
132 btrfs_free_path(path);
136 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 struct extent_map_tree *block_group_cache;
141 struct btrfs_block_group_cache *block_group = NULL;
147 block_group_cache = &info->block_group_cache;
148 ret = find_first_extent_bit(block_group_cache,
149 bytenr, &start, &end,
150 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
154 ret = get_state_private(block_group_cache, start, &ptr);
158 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
159 if (block_group->key.objectid <= bytenr && bytenr <
160 block_group->key.objectid + block_group->key.offset)
164 static u64 find_search_start(struct btrfs_root *root,
165 struct btrfs_block_group_cache **cache_ret,
166 u64 search_start, int num,
167 int data, int full_scan)
170 struct btrfs_block_group_cache *cache = *cache_ret;
181 ret = cache_block_group(root, cache);
185 last = max(search_start, cache->key.objectid);
188 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
189 last, &start, &end, EXTENT_DIRTY);
196 start = max(last, start);
198 if (last - start < num) {
199 if (last == cache->key.objectid + cache->key.offset)
203 if (data != BTRFS_BLOCK_GROUP_MIXED &&
204 start + num > cache->key.objectid + cache->key.offset)
209 cache = btrfs_lookup_block_group(root->fs_info, search_start);
211 printk("Unable to find block group for %Lu\n",
219 last = cache->key.objectid + cache->key.offset;
221 cache = btrfs_lookup_block_group(root->fs_info, last);
227 data = BTRFS_BLOCK_GROUP_MIXED;
232 if (cache_miss && !cache->cached) {
233 cache_block_group(root, cache);
235 cache = btrfs_lookup_block_group(root->fs_info, last);
237 cache = btrfs_find_block_group(root, cache, last, data, 0);
245 static u64 div_factor(u64 num, int factor)
254 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
255 struct btrfs_block_group_cache
256 *hint, u64 search_start,
259 struct btrfs_block_group_cache *cache;
260 struct extent_map_tree *block_group_cache;
261 struct btrfs_block_group_cache *found_group = NULL;
262 struct btrfs_fs_info *info = root->fs_info;
276 block_group_cache = &info->block_group_cache;
281 if (data == BTRFS_BLOCK_GROUP_MIXED) {
282 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
285 bit = BLOCK_GROUP_DATA;
287 bit = BLOCK_GROUP_METADATA;
290 struct btrfs_block_group_cache *shint;
291 shint = btrfs_lookup_block_group(info, search_start);
292 if (shint && (shint->data == data ||
293 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
294 used = btrfs_block_group_used(&shint->item);
295 if (used + shint->pinned <
296 div_factor(shint->key.offset, factor)) {
301 if (hint && (hint->data == data ||
302 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
303 used = btrfs_block_group_used(&hint->item);
304 if (used + hint->pinned <
305 div_factor(hint->key.offset, factor)) {
308 last = hint->key.objectid + hint->key.offset;
312 hint_last = max(hint->key.objectid, search_start);
314 hint_last = search_start;
320 ret = find_first_extent_bit(block_group_cache, last,
325 ret = get_state_private(block_group_cache, start, &ptr);
329 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
330 last = cache->key.objectid + cache->key.offset;
331 used = btrfs_block_group_used(&cache->item);
334 free_check = cache->key.offset;
336 free_check = div_factor(cache->key.offset, factor);
337 if (used + cache->pinned < free_check) {
350 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
358 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
359 u64 owner, u64 owner_offset)
361 u32 high_crc = ~(u32)0;
362 u32 low_crc = ~(u32)0;
365 lenum = cpu_to_le64(root_objectid);
366 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
367 lenum = cpu_to_le64(ref_generation);
368 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
371 lenum = cpu_to_le64(owner);
372 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
373 lenum = cpu_to_le64(owner_offset);
374 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
376 return ((u64)high_crc << 32) | (u64)low_crc;
379 static int match_extent_ref(struct extent_buffer *leaf,
380 struct btrfs_extent_ref *disk_ref,
381 struct btrfs_extent_ref *cpu_ref)
386 if (cpu_ref->objectid)
387 len = sizeof(*cpu_ref);
389 len = 2 * sizeof(u64);
390 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
395 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root,
397 struct btrfs_path *path, u64 bytenr,
398 u64 root_objectid, u64 ref_generation,
399 u64 owner, u64 owner_offset, int del)
402 struct btrfs_key key;
403 struct btrfs_key found_key;
404 struct btrfs_extent_ref ref;
405 struct extent_buffer *leaf;
406 struct btrfs_extent_ref *disk_ref;
410 btrfs_set_stack_ref_root(&ref, root_objectid);
411 btrfs_set_stack_ref_generation(&ref, ref_generation);
412 btrfs_set_stack_ref_objectid(&ref, owner);
413 btrfs_set_stack_ref_offset(&ref, owner_offset);
415 hash = hash_extent_ref(root_objectid, ref_generation, owner,
418 key.objectid = bytenr;
419 key.type = BTRFS_EXTENT_REF_KEY;
422 ret = btrfs_search_slot(trans, root, &key, path,
426 leaf = path->nodes[0];
428 u32 nritems = btrfs_header_nritems(leaf);
429 if (path->slots[0] >= nritems) {
430 ret2 = btrfs_next_leaf(root, path);
433 leaf = path->nodes[0];
435 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
436 if (found_key.objectid != bytenr ||
437 found_key.type != BTRFS_EXTENT_REF_KEY)
439 key.offset = found_key.offset;
441 btrfs_release_path(root, path);
445 disk_ref = btrfs_item_ptr(path->nodes[0],
447 struct btrfs_extent_ref);
448 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
452 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
453 key.offset = found_key.offset + 1;
454 btrfs_release_path(root, path);
461 * Back reference rules. Back refs have three main goals:
463 * 1) differentiate between all holders of references to an extent so that
464 * when a reference is dropped we can make sure it was a valid reference
465 * before freeing the extent.
467 * 2) Provide enough information to quickly find the holders of an extent
468 * if we notice a given block is corrupted or bad.
470 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
471 * maintenance. This is actually the same as #2, but with a slightly
472 * different use case.
474 * File extents can be referenced by:
476 * - multiple snapshots, subvolumes, or different generations in one subvol
477 * - different files inside a single subvolume (in theory, not implemented yet)
478 * - different offsets inside a file (bookend extents in file.c)
480 * The extent ref structure has fields for:
482 * - Objectid of the subvolume root
483 * - Generation number of the tree holding the reference
484 * - objectid of the file holding the reference
485 * - offset in the file corresponding to the key holding the reference
487 * When a file extent is allocated the fields are filled in:
488 * (root_key.objectid, trans->transid, inode objectid, offset in file)
490 * When a leaf is cow'd new references are added for every file extent found
491 * in the leaf. It looks the same as the create case, but trans->transid
492 * will be different when the block is cow'd.
494 * (root_key.objectid, trans->transid, inode objectid, offset in file)
496 * When a file extent is removed either during snapshot deletion or file
497 * truncation, the corresponding back reference is found
500 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
501 * inode objectid, offset in file)
503 * Btree extents can be referenced by:
505 * - Different subvolumes
506 * - Different generations of the same subvolume
508 * Storing sufficient information for a full reverse mapping of a btree
509 * block would require storing the lowest key of the block in the backref,
510 * and it would require updating that lowest key either before write out or
511 * every time it changed. Instead, the objectid of the lowest key is stored
512 * along with the level of the tree block. This provides a hint
513 * about where in the btree the block can be found. Searches through the
514 * btree only need to look for a pointer to that block, so they stop one
515 * level higher than the level recorded in the backref.
517 * Some btrees do not do reference counting on their extents. These
518 * include the extent tree and the tree of tree roots. Backrefs for these
519 * trees always have a generation of zero.
521 * When a tree block is created, back references are inserted:
523 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
525 * When a tree block is cow'd in a reference counted root,
526 * new back references are added for all the blocks it points to.
527 * These are of the form (trans->transid will have increased since creation):
529 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
531 * Because the lowest_key_objectid and the level are just hints
532 * they are not used when backrefs are deleted. When a backref is deleted:
534 * if backref was for a tree root:
535 * root_objectid = root->root_key.objectid
537 * root_objectid = btrfs_header_owner(parent)
539 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
541 * Back Reference Key hashing:
543 * Back references have four fields, each 64 bits long. Unfortunately,
544 * This is hashed into a single 64 bit number and placed into the key offset.
545 * The key objectid corresponds to the first byte in the extent, and the
546 * key type is set to BTRFS_EXTENT_REF_KEY
548 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
549 struct btrfs_root *root,
550 struct btrfs_path *path, u64 bytenr,
551 u64 root_objectid, u64 ref_generation,
552 u64 owner, u64 owner_offset)
555 struct btrfs_key key;
556 struct btrfs_extent_ref ref;
557 struct btrfs_extent_ref *disk_ref;
560 btrfs_set_stack_ref_root(&ref, root_objectid);
561 btrfs_set_stack_ref_generation(&ref, ref_generation);
562 btrfs_set_stack_ref_objectid(&ref, owner);
563 btrfs_set_stack_ref_offset(&ref, owner_offset);
565 hash = hash_extent_ref(root_objectid, ref_generation, owner,
568 key.objectid = bytenr;
569 key.type = BTRFS_EXTENT_REF_KEY;
571 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
572 while (ret == -EEXIST) {
573 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
574 struct btrfs_extent_ref);
575 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
578 btrfs_release_path(root, path);
579 ret = btrfs_insert_empty_item(trans, root, path, &key,
584 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
585 struct btrfs_extent_ref);
586 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
588 btrfs_mark_buffer_dirty(path->nodes[0]);
590 btrfs_release_path(root, path);
594 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
595 struct btrfs_root *root,
596 u64 bytenr, u64 num_bytes,
597 u64 root_objectid, u64 ref_generation,
598 u64 owner, u64 owner_offset)
600 struct btrfs_path *path;
602 struct btrfs_key key;
603 struct extent_buffer *l;
604 struct btrfs_extent_item *item;
607 WARN_ON(num_bytes < root->sectorsize);
608 path = btrfs_alloc_path();
612 key.objectid = bytenr;
613 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
614 key.offset = num_bytes;
615 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
624 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
625 refs = btrfs_extent_refs(l, item);
626 btrfs_set_extent_refs(l, item, refs + 1);
627 btrfs_mark_buffer_dirty(path->nodes[0]);
629 btrfs_release_path(root->fs_info->extent_root, path);
631 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
632 path, bytenr, root_objectid,
633 ref_generation, owner, owner_offset);
635 finish_current_insert(trans, root->fs_info->extent_root);
636 del_pending_extents(trans, root->fs_info->extent_root);
638 btrfs_free_path(path);
642 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
643 struct btrfs_root *root)
645 finish_current_insert(trans, root->fs_info->extent_root);
646 del_pending_extents(trans, root->fs_info->extent_root);
650 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root, u64 bytenr,
652 u64 num_bytes, u32 *refs)
654 struct btrfs_path *path;
656 struct btrfs_key key;
657 struct extent_buffer *l;
658 struct btrfs_extent_item *item;
660 WARN_ON(num_bytes < root->sectorsize);
661 path = btrfs_alloc_path();
662 key.objectid = bytenr;
663 key.offset = num_bytes;
664 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
665 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
670 btrfs_print_leaf(root, path->nodes[0]);
671 printk("failed to find block number %Lu\n", bytenr);
675 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
676 *refs = btrfs_extent_refs(l, item);
678 btrfs_free_path(path);
682 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
683 struct btrfs_path *count_path,
686 struct btrfs_root *extent_root = root->fs_info->extent_root;
687 struct btrfs_path *path;
690 u64 root_objectid = 0;
696 struct btrfs_key key;
697 struct btrfs_key found_key;
698 struct extent_buffer *l;
699 struct btrfs_extent_item *item;
700 struct btrfs_extent_ref *ref_item;
703 path = btrfs_alloc_path();
706 bytenr = first_extent;
708 bytenr = count_path->nodes[level]->start;
711 key.objectid = bytenr;
714 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
715 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
721 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
723 if (found_key.objectid != bytenr ||
724 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
728 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
729 refs = btrfs_extent_refs(l, item);
731 nritems = btrfs_header_nritems(l);
732 if (path->slots[0] >= nritems) {
733 ret = btrfs_next_leaf(extent_root, path);
738 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
739 if (found_key.objectid != bytenr)
741 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
747 ref_item = btrfs_item_ptr(l, path->slots[0],
748 struct btrfs_extent_ref);
749 found_objectid = btrfs_ref_root(l, ref_item);
751 if (found_objectid != root_objectid)
757 if (root_objectid == 0)
758 root_objectid = found_objectid;
762 if (cur_count == 0) {
768 if (level >= 0 && root->node == count_path->nodes[level])
771 btrfs_release_path(root, path);
775 btrfs_free_path(path);
780 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
781 struct btrfs_root *root, u64 owner_objectid)
787 struct btrfs_disk_key disk_key;
789 level = btrfs_header_level(root->node);
790 generation = trans->transid;
791 nritems = btrfs_header_nritems(root->node);
794 btrfs_item_key(root->node, &disk_key, 0);
796 btrfs_node_key(root->node, &disk_key, 0);
797 key_objectid = btrfs_disk_key_objectid(&disk_key);
801 return btrfs_inc_extent_ref(trans, root, root->node->start,
802 root->node->len, owner_objectid,
803 generation, level, key_objectid);
806 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
807 struct extent_buffer *buf)
811 struct btrfs_key key;
812 struct btrfs_file_extent_item *fi;
821 level = btrfs_header_level(buf);
822 nritems = btrfs_header_nritems(buf);
823 for (i = 0; i < nritems; i++) {
826 btrfs_item_key_to_cpu(buf, &key, i);
827 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
829 fi = btrfs_item_ptr(buf, i,
830 struct btrfs_file_extent_item);
831 if (btrfs_file_extent_type(buf, fi) ==
832 BTRFS_FILE_EXTENT_INLINE)
834 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
835 if (disk_bytenr == 0)
837 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
838 btrfs_file_extent_disk_num_bytes(buf, fi),
839 root->root_key.objectid, trans->transid,
840 key.objectid, key.offset);
846 bytenr = btrfs_node_blockptr(buf, i);
847 btrfs_node_key_to_cpu(buf, &key, i);
848 ret = btrfs_inc_extent_ref(trans, root, bytenr,
849 btrfs_level_size(root, level - 1),
850 root->root_key.objectid,
852 level - 1, key.objectid);
863 for (i =0; i < faili; i++) {
866 btrfs_item_key_to_cpu(buf, &key, i);
867 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
869 fi = btrfs_item_ptr(buf, i,
870 struct btrfs_file_extent_item);
871 if (btrfs_file_extent_type(buf, fi) ==
872 BTRFS_FILE_EXTENT_INLINE)
874 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
875 if (disk_bytenr == 0)
877 err = btrfs_free_extent(trans, root, disk_bytenr,
878 btrfs_file_extent_disk_num_bytes(buf,
882 bytenr = btrfs_node_blockptr(buf, i);
883 err = btrfs_free_extent(trans, root, bytenr,
884 btrfs_level_size(root, level - 1), 0);
892 static int write_one_cache_group(struct btrfs_trans_handle *trans,
893 struct btrfs_root *root,
894 struct btrfs_path *path,
895 struct btrfs_block_group_cache *cache)
899 struct btrfs_root *extent_root = root->fs_info->extent_root;
901 struct extent_buffer *leaf;
903 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
908 leaf = path->nodes[0];
909 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
910 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
911 btrfs_mark_buffer_dirty(leaf);
912 btrfs_release_path(extent_root, path);
914 finish_current_insert(trans, extent_root);
915 pending_ret = del_pending_extents(trans, extent_root);
924 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
925 struct btrfs_root *root)
927 struct extent_map_tree *block_group_cache;
928 struct btrfs_block_group_cache *cache;
932 struct btrfs_path *path;
938 block_group_cache = &root->fs_info->block_group_cache;
939 path = btrfs_alloc_path();
944 ret = find_first_extent_bit(block_group_cache, last,
945 &start, &end, BLOCK_GROUP_DIRTY);
950 ret = get_state_private(block_group_cache, start, &ptr);
954 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
955 err = write_one_cache_group(trans, root,
958 * if we fail to write the cache group, we want
959 * to keep it marked dirty in hopes that a later
966 clear_extent_bits(block_group_cache, start, end,
967 BLOCK_GROUP_DIRTY, GFP_NOFS);
969 btrfs_free_path(path);
973 static int update_block_group(struct btrfs_trans_handle *trans,
974 struct btrfs_root *root,
975 u64 bytenr, u64 num_bytes, int alloc,
976 int mark_free, int data)
978 struct btrfs_block_group_cache *cache;
979 struct btrfs_fs_info *info = root->fs_info;
980 u64 total = num_bytes;
987 cache = btrfs_lookup_block_group(info, bytenr);
991 byte_in_group = bytenr - cache->key.objectid;
992 WARN_ON(byte_in_group > cache->key.offset);
993 start = cache->key.objectid;
994 end = start + cache->key.offset - 1;
995 set_extent_bits(&info->block_group_cache, start, end,
996 BLOCK_GROUP_DIRTY, GFP_NOFS);
998 old_val = btrfs_block_group_used(&cache->item);
999 num_bytes = min(total, cache->key.offset - byte_in_group);
1001 if (cache->data != data &&
1002 old_val < (cache->key.offset >> 1)) {
1007 bit_to_clear = BLOCK_GROUP_METADATA;
1008 bit_to_set = BLOCK_GROUP_DATA;
1009 cache->item.flags &=
1010 ~BTRFS_BLOCK_GROUP_MIXED;
1011 cache->item.flags |=
1012 BTRFS_BLOCK_GROUP_DATA;
1014 bit_to_clear = BLOCK_GROUP_DATA;
1015 bit_to_set = BLOCK_GROUP_METADATA;
1016 cache->item.flags &=
1017 ~BTRFS_BLOCK_GROUP_MIXED;
1018 cache->item.flags &=
1019 ~BTRFS_BLOCK_GROUP_DATA;
1021 clear_extent_bits(&info->block_group_cache,
1022 start, end, bit_to_clear,
1024 set_extent_bits(&info->block_group_cache,
1025 start, end, bit_to_set,
1027 } else if (cache->data != data &&
1028 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1029 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1030 set_extent_bits(&info->block_group_cache,
1033 BLOCK_GROUP_METADATA,
1036 old_val += num_bytes;
1038 old_val -= num_bytes;
1040 set_extent_dirty(&info->free_space_cache,
1041 bytenr, bytenr + num_bytes - 1,
1045 btrfs_set_block_group_used(&cache->item, old_val);
1047 bytenr += num_bytes;
1051 static int update_pinned_extents(struct btrfs_root *root,
1052 u64 bytenr, u64 num, int pin)
1055 struct btrfs_block_group_cache *cache;
1056 struct btrfs_fs_info *fs_info = root->fs_info;
1059 set_extent_dirty(&fs_info->pinned_extents,
1060 bytenr, bytenr + num - 1, GFP_NOFS);
1062 clear_extent_dirty(&fs_info->pinned_extents,
1063 bytenr, bytenr + num - 1, GFP_NOFS);
1066 cache = btrfs_lookup_block_group(fs_info, bytenr);
1068 len = min(num, cache->key.offset -
1069 (bytenr - cache->key.objectid));
1071 cache->pinned += len;
1072 fs_info->total_pinned += len;
1074 cache->pinned -= len;
1075 fs_info->total_pinned -= len;
1083 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
1088 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
1092 ret = find_first_extent_bit(pinned_extents, last,
1093 &start, &end, EXTENT_DIRTY);
1096 set_extent_dirty(copy, start, end, GFP_NOFS);
1102 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1103 struct btrfs_root *root,
1104 struct extent_map_tree *unpin)
1109 struct extent_map_tree *free_space_cache;
1110 free_space_cache = &root->fs_info->free_space_cache;
1113 ret = find_first_extent_bit(unpin, 0, &start, &end,
1117 update_pinned_extents(root, start, end + 1 - start, 0);
1118 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1119 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1124 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
1125 btrfs_root *extent_root)
1129 struct btrfs_fs_info *info = extent_root->fs_info;
1130 struct extent_buffer *eb;
1131 struct btrfs_path *path;
1132 struct btrfs_key ins;
1133 struct btrfs_disk_key first;
1134 struct btrfs_extent_item extent_item;
1139 btrfs_set_stack_extent_refs(&extent_item, 1);
1140 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1141 path = btrfs_alloc_path();
1144 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1145 &end, EXTENT_LOCKED);
1149 ins.objectid = start;
1150 ins.offset = end + 1 - start;
1151 err = btrfs_insert_item(trans, extent_root, &ins,
1152 &extent_item, sizeof(extent_item));
1153 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1155 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1156 level = btrfs_header_level(eb);
1158 btrfs_item_key(eb, &first, 0);
1160 btrfs_node_key(eb, &first, 0);
1162 err = btrfs_insert_extent_backref(trans, extent_root, path,
1163 start, extent_root->root_key.objectid,
1165 btrfs_disk_key_objectid(&first));
1167 free_extent_buffer(eb);
1169 btrfs_free_path(path);
1173 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1177 struct extent_buffer *buf;
1180 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1182 if (btrfs_buffer_uptodate(buf)) {
1184 root->fs_info->running_transaction->transid;
1185 if (btrfs_header_generation(buf) == transid) {
1186 free_extent_buffer(buf);
1190 free_extent_buffer(buf);
1192 update_pinned_extents(root, bytenr, num_bytes, 1);
1194 set_extent_bits(&root->fs_info->pending_del,
1195 bytenr, bytenr + num_bytes - 1,
1196 EXTENT_LOCKED, GFP_NOFS);
1203 * remove an extent from the root, returns 0 on success
1205 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1206 *root, u64 bytenr, u64 num_bytes,
1207 u64 root_objectid, u64 ref_generation,
1208 u64 owner_objectid, u64 owner_offset, int pin,
1211 struct btrfs_path *path;
1212 struct btrfs_key key;
1213 struct btrfs_fs_info *info = root->fs_info;
1214 struct btrfs_root *extent_root = info->extent_root;
1215 struct extent_buffer *leaf;
1217 struct btrfs_extent_item *ei;
1220 key.objectid = bytenr;
1221 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1222 key.offset = num_bytes;
1224 path = btrfs_alloc_path();
1228 ret = lookup_extent_backref(trans, extent_root, path,
1229 bytenr, root_objectid,
1231 owner_objectid, owner_offset, 1);
1233 ret = btrfs_del_item(trans, extent_root, path);
1235 btrfs_print_leaf(extent_root, path->nodes[0]);
1237 printk("Unable to find ref byte nr %Lu root %Lu "
1238 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1239 root_objectid, ref_generation, owner_objectid,
1242 btrfs_release_path(extent_root, path);
1243 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1248 leaf = path->nodes[0];
1249 ei = btrfs_item_ptr(leaf, path->slots[0],
1250 struct btrfs_extent_item);
1251 refs = btrfs_extent_refs(leaf, ei);
1254 btrfs_set_extent_refs(leaf, ei, refs);
1255 btrfs_mark_buffer_dirty(leaf);
1262 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1268 /* block accounting for super block */
1269 super_used = btrfs_super_bytes_used(&info->super_copy);
1270 btrfs_set_super_bytes_used(&info->super_copy,
1271 super_used - num_bytes);
1273 /* block accounting for root item */
1274 root_used = btrfs_root_used(&root->root_item);
1275 btrfs_set_root_used(&root->root_item,
1276 root_used - num_bytes);
1278 ret = btrfs_del_item(trans, extent_root, path);
1282 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1286 btrfs_free_path(path);
1287 finish_current_insert(trans, extent_root);
1292 * find all the blocks marked as pending in the radix tree and remove
1293 * them from the extent map
1295 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1296 btrfs_root *extent_root)
1302 struct extent_map_tree *pending_del;
1303 struct extent_map_tree *pinned_extents;
1305 pending_del = &extent_root->fs_info->pending_del;
1306 pinned_extents = &extent_root->fs_info->pinned_extents;
1309 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1313 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1314 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1316 ret = __free_extent(trans, extent_root,
1317 start, end + 1 - start,
1318 extent_root->root_key.objectid,
1327 * remove an extent from the root, returns 0 on success
1329 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1330 *root, u64 bytenr, u64 num_bytes,
1331 u64 root_objectid, u64 ref_generation,
1332 u64 owner_objectid, u64 owner_offset, int pin)
1334 struct btrfs_root *extent_root = root->fs_info->extent_root;
1338 WARN_ON(num_bytes < root->sectorsize);
1339 if (!root->ref_cows)
1342 if (root == extent_root) {
1343 pin_down_bytes(root, bytenr, num_bytes, 1);
1346 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1347 ref_generation, owner_objectid, owner_offset,
1349 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1350 return ret ? ret : pending_ret;
1353 static u64 stripe_align(struct btrfs_root *root, u64 val)
1355 u64 mask = ((u64)root->stripesize - 1);
1356 u64 ret = (val + mask) & ~mask;
1361 * walks the btree of allocated extents and find a hole of a given size.
1362 * The key ins is changed to record the hole:
1363 * ins->objectid == block start
1364 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1365 * ins->offset == number of blocks
1366 * Any available blocks before search_start are skipped.
1368 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1369 *orig_root, u64 num_bytes, u64 empty_size,
1370 u64 search_start, u64 search_end, u64 hint_byte,
1371 struct btrfs_key *ins, u64 exclude_start,
1372 u64 exclude_nr, int data)
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;
1625 struct btrfs_fs_info *info = root->fs_info;
1626 struct btrfs_root *extent_root = info->extent_root;
1627 struct btrfs_extent_item extent_item;
1628 struct btrfs_path *path;
1630 btrfs_set_stack_extent_refs(&extent_item, 1);
1632 WARN_ON(num_bytes < root->sectorsize);
1633 ret = find_free_extent(trans, root, num_bytes, empty_size,
1634 search_start, search_end, hint_byte, ins,
1635 trans->alloc_exclude_start,
1636 trans->alloc_exclude_nr, data);
1641 /* block accounting for super block */
1642 super_used = btrfs_super_bytes_used(&info->super_copy);
1643 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1645 /* block accounting for root item */
1646 root_used = btrfs_root_used(&root->root_item);
1647 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1649 clear_extent_dirty(&root->fs_info->free_space_cache,
1650 ins->objectid, ins->objectid + ins->offset - 1,
1653 if (root == extent_root) {
1654 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1655 ins->objectid + ins->offset - 1,
1656 EXTENT_LOCKED, GFP_NOFS);
1661 WARN_ON(trans->alloc_exclude_nr);
1662 trans->alloc_exclude_start = ins->objectid;
1663 trans->alloc_exclude_nr = ins->offset;
1664 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1665 sizeof(extent_item));
1667 trans->alloc_exclude_start = 0;
1668 trans->alloc_exclude_nr = 0;
1671 path = btrfs_alloc_path();
1673 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1674 ins->objectid, root_objectid,
1675 ref_generation, owner, owner_offset);
1678 btrfs_free_path(path);
1679 finish_current_insert(trans, extent_root);
1680 pending_ret = del_pending_extents(trans, extent_root);
1690 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1697 * helper function to allocate a block for a given tree
1698 * returns the tree buffer or NULL.
1700 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1701 struct btrfs_root *root,
1703 u64 root_objectid, u64 hint,
1709 ref_generation = trans->transid;
1714 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1715 ref_generation, 0, 0, hint, empty_size);
1719 * helper function to allocate a block for a given tree
1720 * returns the tree buffer or NULL.
1722 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *root,
1732 struct btrfs_key ins;
1734 struct extent_buffer *buf;
1736 ret = btrfs_alloc_extent(trans, root, blocksize,
1737 root_objectid, ref_generation,
1738 level, first_objectid, empty_size, hint,
1742 return ERR_PTR(ret);
1744 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1746 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1747 root->root_key.objectid, ref_generation,
1749 return ERR_PTR(-ENOMEM);
1751 btrfs_set_buffer_uptodate(buf);
1752 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1753 buf->start + buf->len - 1, GFP_NOFS);
1754 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1755 buf->start, buf->start + buf->len - 1,
1756 EXTENT_CSUM, GFP_NOFS);
1757 buf->flags |= EXTENT_CSUM;
1758 btrfs_set_buffer_defrag(buf);
1759 trans->blocks_used++;
1763 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1764 struct btrfs_root *root, struct extent_buffer *leaf)
1767 u64 leaf_generation;
1768 struct btrfs_key key;
1769 struct btrfs_file_extent_item *fi;
1774 BUG_ON(!btrfs_is_leaf(leaf));
1775 nritems = btrfs_header_nritems(leaf);
1776 leaf_owner = btrfs_header_owner(leaf);
1777 leaf_generation = btrfs_header_generation(leaf);
1779 for (i = 0; i < nritems; i++) {
1782 btrfs_item_key_to_cpu(leaf, &key, i);
1783 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1785 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1786 if (btrfs_file_extent_type(leaf, fi) ==
1787 BTRFS_FILE_EXTENT_INLINE)
1790 * FIXME make sure to insert a trans record that
1791 * repeats the snapshot del on crash
1793 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1794 if (disk_bytenr == 0)
1796 ret = btrfs_free_extent(trans, root, disk_bytenr,
1797 btrfs_file_extent_disk_num_bytes(leaf, fi),
1798 leaf_owner, leaf_generation,
1799 key.objectid, key.offset, 0);
1805 static void reada_walk_down(struct btrfs_root *root,
1806 struct extent_buffer *node)
1816 nritems = btrfs_header_nritems(node);
1817 level = btrfs_header_level(node);
1818 for (i = 0; i < nritems; i++) {
1819 bytenr = btrfs_node_blockptr(node, i);
1820 blocksize = btrfs_level_size(root, level - 1);
1821 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1825 mutex_unlock(&root->fs_info->fs_mutex);
1826 ret = readahead_tree_block(root, bytenr, blocksize);
1828 mutex_lock(&root->fs_info->fs_mutex);
1835 * helper function for drop_snapshot, this walks down the tree dropping ref
1836 * counts as it goes.
1838 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1839 *root, struct btrfs_path *path, int *level)
1844 struct extent_buffer *next;
1845 struct extent_buffer *cur;
1846 struct extent_buffer *parent;
1851 WARN_ON(*level < 0);
1852 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1853 ret = lookup_extent_ref(trans, root,
1854 path->nodes[*level]->start,
1855 path->nodes[*level]->len, &refs);
1861 * walk down to the last node level and free all the leaves
1863 while(*level >= 0) {
1864 WARN_ON(*level < 0);
1865 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1866 cur = path->nodes[*level];
1868 if (*level > 0 && path->slots[*level] == 0)
1869 reada_walk_down(root, cur);
1871 if (btrfs_header_level(cur) != *level)
1874 if (path->slots[*level] >=
1875 btrfs_header_nritems(cur))
1878 ret = drop_leaf_ref(trans, root, cur);
1882 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1883 blocksize = btrfs_level_size(root, *level - 1);
1884 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1887 parent = path->nodes[*level];
1888 root_owner = btrfs_header_owner(parent);
1889 root_gen = btrfs_header_generation(parent);
1890 path->slots[*level]++;
1891 ret = btrfs_free_extent(trans, root, bytenr,
1892 blocksize, root_owner,
1897 next = btrfs_find_tree_block(root, bytenr, blocksize);
1898 if (!next || !btrfs_buffer_uptodate(next)) {
1899 free_extent_buffer(next);
1900 mutex_unlock(&root->fs_info->fs_mutex);
1901 next = read_tree_block(root, bytenr, blocksize);
1902 mutex_lock(&root->fs_info->fs_mutex);
1904 /* we dropped the lock, check one more time */
1905 ret = lookup_extent_ref(trans, root, bytenr,
1909 parent = path->nodes[*level];
1910 root_owner = btrfs_header_owner(parent);
1911 root_gen = btrfs_header_generation(parent);
1913 path->slots[*level]++;
1914 free_extent_buffer(next);
1915 ret = btrfs_free_extent(trans, root, bytenr,
1923 WARN_ON(*level <= 0);
1924 if (path->nodes[*level-1])
1925 free_extent_buffer(path->nodes[*level-1]);
1926 path->nodes[*level-1] = next;
1927 *level = btrfs_header_level(next);
1928 path->slots[*level] = 0;
1931 WARN_ON(*level < 0);
1932 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1934 if (path->nodes[*level] == root->node) {
1935 root_owner = root->root_key.objectid;
1936 parent = path->nodes[*level];
1938 parent = path->nodes[*level + 1];
1939 root_owner = btrfs_header_owner(parent);
1942 root_gen = btrfs_header_generation(parent);
1943 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1944 path->nodes[*level]->len,
1945 root_owner, root_gen, 0, 0, 1);
1946 free_extent_buffer(path->nodes[*level]);
1947 path->nodes[*level] = NULL;
1954 * helper for dropping snapshots. This walks back up the tree in the path
1955 * to find the first node higher up where we haven't yet gone through
1958 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1959 *root, struct btrfs_path *path, int *level)
1963 struct btrfs_root_item *root_item = &root->root_item;
1968 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1969 slot = path->slots[i];
1970 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1971 struct extent_buffer *node;
1972 struct btrfs_disk_key disk_key;
1973 node = path->nodes[i];
1976 WARN_ON(*level == 0);
1977 btrfs_node_key(node, &disk_key, path->slots[i]);
1978 memcpy(&root_item->drop_progress,
1979 &disk_key, sizeof(disk_key));
1980 root_item->drop_level = i;
1983 if (path->nodes[*level] == root->node) {
1984 root_owner = root->root_key.objectid;
1986 btrfs_header_generation(path->nodes[*level]);
1988 struct extent_buffer *node;
1989 node = path->nodes[*level + 1];
1990 root_owner = btrfs_header_owner(node);
1991 root_gen = btrfs_header_generation(node);
1993 ret = btrfs_free_extent(trans, root,
1994 path->nodes[*level]->start,
1995 path->nodes[*level]->len,
1996 root_owner, root_gen, 0, 0, 1);
1998 free_extent_buffer(path->nodes[*level]);
1999 path->nodes[*level] = NULL;
2007 * drop the reference count on the tree rooted at 'snap'. This traverses
2008 * the tree freeing any blocks that have a ref count of zero after being
2011 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2017 struct btrfs_path *path;
2020 struct btrfs_root_item *root_item = &root->root_item;
2022 path = btrfs_alloc_path();
2025 level = btrfs_header_level(root->node);
2027 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2028 path->nodes[level] = root->node;
2029 extent_buffer_get(root->node);
2030 path->slots[level] = 0;
2032 struct btrfs_key key;
2033 struct btrfs_disk_key found_key;
2034 struct extent_buffer *node;
2036 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2037 level = root_item->drop_level;
2038 path->lowest_level = level;
2039 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2044 node = path->nodes[level];
2045 btrfs_node_key(node, &found_key, path->slots[level]);
2046 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2047 sizeof(found_key)));
2050 wret = walk_down_tree(trans, root, path, &level);
2056 wret = walk_up_tree(trans, root, path, &level);
2064 for (i = 0; i <= orig_level; i++) {
2065 if (path->nodes[i]) {
2066 free_extent_buffer(path->nodes[i]);
2067 path->nodes[i] = NULL;
2071 btrfs_free_path(path);
2075 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2082 ret = find_first_extent_bit(&info->block_group_cache, 0,
2083 &start, &end, (unsigned int)-1);
2086 ret = get_state_private(&info->block_group_cache, start, &ptr);
2088 kfree((void *)(unsigned long)ptr);
2089 clear_extent_bits(&info->block_group_cache, start,
2090 end, (unsigned int)-1, GFP_NOFS);
2093 ret = find_first_extent_bit(&info->free_space_cache, 0,
2094 &start, &end, EXTENT_DIRTY);
2097 clear_extent_dirty(&info->free_space_cache, start,
2103 int btrfs_read_block_groups(struct btrfs_root *root)
2105 struct btrfs_path *path;
2109 struct btrfs_block_group_cache *cache;
2110 struct btrfs_fs_info *info = root->fs_info;
2111 struct extent_map_tree *block_group_cache;
2112 struct btrfs_key key;
2113 struct btrfs_key found_key;
2114 struct extent_buffer *leaf;
2116 block_group_cache = &info->block_group_cache;
2118 root = info->extent_root;
2120 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2121 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2123 path = btrfs_alloc_path();
2128 ret = btrfs_search_slot(NULL, info->extent_root,
2134 leaf = path->nodes[0];
2135 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2136 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2142 read_extent_buffer(leaf, &cache->item,
2143 btrfs_item_ptr_offset(leaf, path->slots[0]),
2144 sizeof(cache->item));
2145 memcpy(&cache->key, &found_key, sizeof(found_key));
2148 key.objectid = found_key.objectid + found_key.offset;
2149 btrfs_release_path(root, path);
2151 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2152 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2153 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2154 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2155 bit = BLOCK_GROUP_DATA;
2156 cache->data = BTRFS_BLOCK_GROUP_DATA;
2158 bit = BLOCK_GROUP_METADATA;
2162 /* use EXTENT_LOCKED to prevent merging */
2163 set_extent_bits(block_group_cache, found_key.objectid,
2164 found_key.objectid + found_key.offset - 1,
2165 bit | EXTENT_LOCKED, GFP_NOFS);
2166 set_state_private(block_group_cache, found_key.objectid,
2167 (unsigned long)cache);
2170 btrfs_super_total_bytes(&info->super_copy))
2174 btrfs_free_path(path);