2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/crc32c.h>
21 #include <linux/pagemap.h>
25 #include "print-tree.h"
26 #include "transaction.h"
28 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
29 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
30 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
32 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
34 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
37 static int cache_block_group(struct btrfs_root *root,
38 struct btrfs_block_group_cache *block_group)
40 struct btrfs_path *path;
43 struct extent_buffer *leaf;
44 struct extent_map_tree *free_space_cache;
54 root = root->fs_info->extent_root;
55 free_space_cache = &root->fs_info->free_space_cache;
57 if (block_group->cached)
60 path = btrfs_alloc_path();
65 first_free = block_group->key.objectid;
66 key.objectid = block_group->key.objectid;
69 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
70 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
75 if (ret && path->slots[0] > 0)
79 leaf = path->nodes[0];
80 slot = path->slots[0];
81 if (slot >= btrfs_header_nritems(leaf)) {
82 ret = btrfs_next_leaf(root, path);
92 btrfs_item_key_to_cpu(leaf, &key, slot);
93 if (key.objectid < block_group->key.objectid) {
94 if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
95 key.objectid + key.offset > first_free)
96 first_free = key.objectid + key.offset;
100 if (key.objectid >= block_group->key.objectid +
101 block_group->key.offset) {
105 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
110 if (key.objectid > last) {
111 hole_size = key.objectid - last;
112 set_extent_dirty(free_space_cache, last,
113 last + hole_size - 1,
116 last = key.objectid + key.offset;
124 if (block_group->key.objectid +
125 block_group->key.offset > last) {
126 hole_size = block_group->key.objectid +
127 block_group->key.offset - last;
128 set_extent_dirty(free_space_cache, last,
129 last + hole_size - 1, GFP_NOFS);
131 block_group->cached = 1;
133 btrfs_free_path(path);
137 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
141 struct extent_map_tree *block_group_cache;
142 struct btrfs_block_group_cache *block_group = NULL;
148 block_group_cache = &info->block_group_cache;
149 ret = find_first_extent_bit(block_group_cache,
150 bytenr, &start, &end,
151 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
155 ret = get_state_private(block_group_cache, start, &ptr);
159 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
160 if (block_group->key.objectid <= bytenr && bytenr <
161 block_group->key.objectid + block_group->key.offset)
165 static u64 find_search_start(struct btrfs_root *root,
166 struct btrfs_block_group_cache **cache_ret,
167 u64 search_start, int num,
168 int data, int full_scan)
171 struct btrfs_block_group_cache *cache = *cache_ret;
182 ret = cache_block_group(root, cache);
186 last = max(search_start, cache->key.objectid);
189 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
190 last, &start, &end, EXTENT_DIRTY);
197 start = max(last, start);
199 if (last - start < num) {
200 if (last == cache->key.objectid + cache->key.offset)
204 if (data != BTRFS_BLOCK_GROUP_MIXED &&
205 start + num > cache->key.objectid + cache->key.offset)
210 cache = btrfs_lookup_block_group(root->fs_info, search_start);
212 printk("Unable to find block group for %Lu\n",
220 last = cache->key.objectid + cache->key.offset;
222 cache = btrfs_lookup_block_group(root->fs_info, last);
228 data = BTRFS_BLOCK_GROUP_MIXED;
233 if (cache_miss && !cache->cached) {
234 cache_block_group(root, cache);
236 cache = btrfs_lookup_block_group(root->fs_info, last);
238 cache = btrfs_find_block_group(root, cache, last, data, 0);
246 static u64 div_factor(u64 num, int factor)
255 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
256 struct btrfs_block_group_cache
257 *hint, u64 search_start,
260 struct btrfs_block_group_cache *cache;
261 struct extent_map_tree *block_group_cache;
262 struct btrfs_block_group_cache *found_group = NULL;
263 struct btrfs_fs_info *info = root->fs_info;
277 block_group_cache = &info->block_group_cache;
282 if (data == BTRFS_BLOCK_GROUP_MIXED) {
283 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
286 bit = BLOCK_GROUP_DATA;
288 bit = BLOCK_GROUP_METADATA;
291 struct btrfs_block_group_cache *shint;
292 shint = btrfs_lookup_block_group(info, search_start);
293 if (shint && (shint->data == data ||
294 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
295 used = btrfs_block_group_used(&shint->item);
296 if (used + shint->pinned <
297 div_factor(shint->key.offset, factor)) {
302 if (hint && (hint->data == data ||
303 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
304 used = btrfs_block_group_used(&hint->item);
305 if (used + hint->pinned <
306 div_factor(hint->key.offset, factor)) {
309 last = hint->key.objectid + hint->key.offset;
313 hint_last = max(hint->key.objectid, search_start);
315 hint_last = search_start;
321 ret = find_first_extent_bit(block_group_cache, last,
326 ret = get_state_private(block_group_cache, start, &ptr);
330 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
331 last = cache->key.objectid + cache->key.offset;
332 used = btrfs_block_group_used(&cache->item);
335 free_check = cache->key.offset;
337 free_check = div_factor(cache->key.offset, factor);
338 if (used + cache->pinned < free_check) {
351 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
359 static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
360 u64 owner, u64 owner_offset)
362 u32 high_crc = ~(u32)0;
363 u32 low_crc = ~(u32)0;
366 lenum = cpu_to_le64(root_objectid);
367 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
368 lenum = cpu_to_le64(ref_generation);
369 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
372 lenum = cpu_to_le64(owner);
373 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
374 lenum = cpu_to_le64(owner_offset);
375 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
377 return ((u64)high_crc << 32) | (u64)low_crc;
380 static int match_extent_ref(struct extent_buffer *leaf,
381 struct btrfs_extent_ref *disk_ref,
382 struct btrfs_extent_ref *cpu_ref)
387 if (cpu_ref->objectid)
388 len = sizeof(*cpu_ref);
390 len = 2 * sizeof(u64);
391 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
396 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root,
398 struct btrfs_path *path, u64 bytenr,
399 u64 root_objectid, u64 ref_generation,
400 u64 owner, u64 owner_offset, int del)
403 struct btrfs_key key;
404 struct btrfs_key found_key;
405 struct btrfs_extent_ref ref;
406 struct extent_buffer *leaf;
407 struct btrfs_extent_ref *disk_ref;
411 btrfs_set_stack_ref_root(&ref, root_objectid);
412 btrfs_set_stack_ref_generation(&ref, ref_generation);
413 btrfs_set_stack_ref_objectid(&ref, owner);
414 btrfs_set_stack_ref_offset(&ref, owner_offset);
416 hash = hash_extent_ref(root_objectid, ref_generation, owner,
419 key.objectid = bytenr;
420 key.type = BTRFS_EXTENT_REF_KEY;
423 ret = btrfs_search_slot(trans, root, &key, path,
427 leaf = path->nodes[0];
429 u32 nritems = btrfs_header_nritems(leaf);
430 if (path->slots[0] >= nritems) {
431 ret2 = btrfs_next_leaf(root, path);
434 leaf = path->nodes[0];
436 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
437 if (found_key.objectid != bytenr ||
438 found_key.type != BTRFS_EXTENT_REF_KEY)
440 key.offset = found_key.offset;
442 btrfs_release_path(root, path);
446 disk_ref = btrfs_item_ptr(path->nodes[0],
448 struct btrfs_extent_ref);
449 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
453 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
454 key.offset = found_key.offset + 1;
455 btrfs_release_path(root, path);
462 * Back reference rules. Back refs have three main goals:
464 * 1) differentiate between all holders of references to an extent so that
465 * when a reference is dropped we can make sure it was a valid reference
466 * before freeing the extent.
468 * 2) Provide enough information to quickly find the holders of an extent
469 * if we notice a given block is corrupted or bad.
471 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
472 * maintenance. This is actually the same as #2, but with a slightly
473 * different use case.
475 * File extents can be referenced by:
477 * - multiple snapshots, subvolumes, or different generations in one subvol
478 * - different files inside a single subvolume (in theory, not implemented yet)
479 * - different offsets inside a file (bookend extents in file.c)
481 * The extent ref structure has fields for:
483 * - Objectid of the subvolume root
484 * - Generation number of the tree holding the reference
485 * - objectid of the file holding the reference
486 * - offset in the file corresponding to the key holding the reference
488 * When a file extent is allocated the fields are filled in:
489 * (root_key.objectid, trans->transid, inode objectid, offset in file)
491 * When a leaf is cow'd new references are added for every file extent found
492 * in the leaf. It looks the same as the create case, but trans->transid
493 * will be different when the block is cow'd.
495 * (root_key.objectid, trans->transid, inode objectid, offset in file)
497 * When a file extent is removed either during snapshot deletion or file
498 * truncation, the corresponding back reference is found
501 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
502 * inode objectid, offset in file)
504 * Btree extents can be referenced by:
506 * - Different subvolumes
507 * - Different generations of the same subvolume
509 * Storing sufficient information for a full reverse mapping of a btree
510 * block would require storing the lowest key of the block in the backref,
511 * and it would require updating that lowest key either before write out or
512 * every time it changed. Instead, the objectid of the lowest key is stored
513 * along with the level of the tree block. This provides a hint
514 * about where in the btree the block can be found. Searches through the
515 * btree only need to look for a pointer to that block, so they stop one
516 * level higher than the level recorded in the backref.
518 * Some btrees do not do reference counting on their extents. These
519 * include the extent tree and the tree of tree roots. Backrefs for these
520 * trees always have a generation of zero.
522 * When a tree block is created, back references are inserted:
524 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
526 * When a tree block is cow'd in a reference counted root,
527 * new back references are added for all the blocks it points to.
528 * These are of the form (trans->transid will have increased since creation):
530 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
532 * Because the lowest_key_objectid and the level are just hints
533 * they are not used when backrefs are deleted. When a backref is deleted:
535 * if backref was for a tree root:
536 * root_objectid = root->root_key.objectid
538 * root_objectid = btrfs_header_owner(parent)
540 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
542 * Back Reference Key hashing:
544 * Back references have four fields, each 64 bits long. Unfortunately,
545 * This is hashed into a single 64 bit number and placed into the key offset.
546 * The key objectid corresponds to the first byte in the extent, and the
547 * key type is set to BTRFS_EXTENT_REF_KEY
549 int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 struct btrfs_path *path, u64 bytenr,
552 u64 root_objectid, u64 ref_generation,
553 u64 owner, u64 owner_offset)
556 struct btrfs_key key;
557 struct btrfs_extent_ref ref;
558 struct btrfs_extent_ref *disk_ref;
561 btrfs_set_stack_ref_root(&ref, root_objectid);
562 btrfs_set_stack_ref_generation(&ref, ref_generation);
563 btrfs_set_stack_ref_objectid(&ref, owner);
564 btrfs_set_stack_ref_offset(&ref, owner_offset);
566 hash = hash_extent_ref(root_objectid, ref_generation, owner,
569 key.objectid = bytenr;
570 key.type = BTRFS_EXTENT_REF_KEY;
572 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
573 while (ret == -EEXIST) {
574 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
575 struct btrfs_extent_ref);
576 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
579 btrfs_release_path(root, path);
580 ret = btrfs_insert_empty_item(trans, root, path, &key,
585 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
586 struct btrfs_extent_ref);
587 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
589 btrfs_mark_buffer_dirty(path->nodes[0]);
591 btrfs_release_path(root, path);
595 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
596 struct btrfs_root *root,
597 u64 bytenr, u64 num_bytes,
598 u64 root_objectid, u64 ref_generation,
599 u64 owner, u64 owner_offset)
601 struct btrfs_path *path;
603 struct btrfs_key key;
604 struct extent_buffer *l;
605 struct btrfs_extent_item *item;
608 WARN_ON(num_bytes < root->sectorsize);
609 path = btrfs_alloc_path();
613 key.objectid = bytenr;
614 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
615 key.offset = num_bytes;
616 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
625 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
626 refs = btrfs_extent_refs(l, item);
627 btrfs_set_extent_refs(l, item, refs + 1);
628 btrfs_mark_buffer_dirty(path->nodes[0]);
630 btrfs_release_path(root->fs_info->extent_root, path);
632 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
633 path, bytenr, root_objectid,
634 ref_generation, owner, owner_offset);
636 finish_current_insert(trans, root->fs_info->extent_root);
637 del_pending_extents(trans, root->fs_info->extent_root);
639 btrfs_free_path(path);
643 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
644 struct btrfs_root *root)
646 finish_current_insert(trans, root->fs_info->extent_root);
647 del_pending_extents(trans, root->fs_info->extent_root);
651 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
652 struct btrfs_root *root, u64 bytenr,
653 u64 num_bytes, u32 *refs)
655 struct btrfs_path *path;
657 struct btrfs_key key;
658 struct extent_buffer *l;
659 struct btrfs_extent_item *item;
661 WARN_ON(num_bytes < root->sectorsize);
662 path = btrfs_alloc_path();
663 key.objectid = bytenr;
664 key.offset = num_bytes;
665 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
666 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
671 btrfs_print_leaf(root, path->nodes[0]);
672 printk("failed to find block number %Lu\n", bytenr);
676 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
677 *refs = btrfs_extent_refs(l, item);
679 btrfs_free_path(path);
683 u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
684 struct btrfs_path *count_path,
687 struct btrfs_root *extent_root = root->fs_info->extent_root;
688 struct btrfs_path *path;
691 u64 root_objectid = root->root_key.objectid;
697 struct btrfs_key key;
698 struct btrfs_key found_key;
699 struct extent_buffer *l;
700 struct btrfs_extent_item *item;
701 struct btrfs_extent_ref *ref_item;
704 path = btrfs_alloc_path();
707 bytenr = first_extent;
709 bytenr = count_path->nodes[level]->start;
712 key.objectid = bytenr;
715 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
716 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
722 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
724 if (found_key.objectid != bytenr ||
725 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
729 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
730 refs = btrfs_extent_refs(l, item);
732 nritems = btrfs_header_nritems(l);
733 if (path->slots[0] >= nritems) {
734 ret = btrfs_next_leaf(extent_root, path);
739 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
740 if (found_key.objectid != bytenr)
742 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
748 ref_item = btrfs_item_ptr(l, path->slots[0],
749 struct btrfs_extent_ref);
750 found_objectid = btrfs_ref_root(l, ref_item);
752 if (found_objectid != root_objectid) {
759 if (cur_count == 0) {
765 if (level >= 0 && root->node == count_path->nodes[level])
768 btrfs_release_path(root, path);
772 btrfs_free_path(path);
777 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
778 struct btrfs_root *root, u64 owner_objectid)
784 struct btrfs_disk_key disk_key;
786 level = btrfs_header_level(root->node);
787 generation = trans->transid;
788 nritems = btrfs_header_nritems(root->node);
791 btrfs_item_key(root->node, &disk_key, 0);
793 btrfs_node_key(root->node, &disk_key, 0);
794 key_objectid = btrfs_disk_key_objectid(&disk_key);
798 return btrfs_inc_extent_ref(trans, root, root->node->start,
799 root->node->len, owner_objectid,
800 generation, level, key_objectid);
803 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
804 struct extent_buffer *buf)
808 struct btrfs_key key;
809 struct btrfs_file_extent_item *fi;
818 level = btrfs_header_level(buf);
819 nritems = btrfs_header_nritems(buf);
820 for (i = 0; i < nritems; i++) {
823 btrfs_item_key_to_cpu(buf, &key, i);
824 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
826 fi = btrfs_item_ptr(buf, i,
827 struct btrfs_file_extent_item);
828 if (btrfs_file_extent_type(buf, fi) ==
829 BTRFS_FILE_EXTENT_INLINE)
831 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
832 if (disk_bytenr == 0)
834 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
835 btrfs_file_extent_disk_num_bytes(buf, fi),
836 root->root_key.objectid, trans->transid,
837 key.objectid, key.offset);
843 bytenr = btrfs_node_blockptr(buf, i);
844 btrfs_node_key_to_cpu(buf, &key, i);
845 ret = btrfs_inc_extent_ref(trans, root, bytenr,
846 btrfs_level_size(root, level - 1),
847 root->root_key.objectid,
849 level - 1, key.objectid);
860 for (i =0; i < faili; i++) {
863 btrfs_item_key_to_cpu(buf, &key, i);
864 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
866 fi = btrfs_item_ptr(buf, i,
867 struct btrfs_file_extent_item);
868 if (btrfs_file_extent_type(buf, fi) ==
869 BTRFS_FILE_EXTENT_INLINE)
871 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
872 if (disk_bytenr == 0)
874 err = btrfs_free_extent(trans, root, disk_bytenr,
875 btrfs_file_extent_disk_num_bytes(buf,
879 bytenr = btrfs_node_blockptr(buf, i);
880 err = btrfs_free_extent(trans, root, bytenr,
881 btrfs_level_size(root, level - 1), 0);
889 static int write_one_cache_group(struct btrfs_trans_handle *trans,
890 struct btrfs_root *root,
891 struct btrfs_path *path,
892 struct btrfs_block_group_cache *cache)
896 struct btrfs_root *extent_root = root->fs_info->extent_root;
898 struct extent_buffer *leaf;
900 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
905 leaf = path->nodes[0];
906 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
907 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
908 btrfs_mark_buffer_dirty(leaf);
909 btrfs_release_path(extent_root, path);
911 finish_current_insert(trans, extent_root);
912 pending_ret = del_pending_extents(trans, extent_root);
921 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
922 struct btrfs_root *root)
924 struct extent_map_tree *block_group_cache;
925 struct btrfs_block_group_cache *cache;
929 struct btrfs_path *path;
935 block_group_cache = &root->fs_info->block_group_cache;
936 path = btrfs_alloc_path();
941 ret = find_first_extent_bit(block_group_cache, last,
942 &start, &end, BLOCK_GROUP_DIRTY);
947 ret = get_state_private(block_group_cache, start, &ptr);
951 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
952 err = write_one_cache_group(trans, root,
955 * if we fail to write the cache group, we want
956 * to keep it marked dirty in hopes that a later
963 clear_extent_bits(block_group_cache, start, end,
964 BLOCK_GROUP_DIRTY, GFP_NOFS);
966 btrfs_free_path(path);
970 static int update_block_group(struct btrfs_trans_handle *trans,
971 struct btrfs_root *root,
972 u64 bytenr, u64 num_bytes, int alloc,
973 int mark_free, int data)
975 struct btrfs_block_group_cache *cache;
976 struct btrfs_fs_info *info = root->fs_info;
977 u64 total = num_bytes;
984 cache = btrfs_lookup_block_group(info, bytenr);
988 byte_in_group = bytenr - cache->key.objectid;
989 WARN_ON(byte_in_group > cache->key.offset);
990 start = cache->key.objectid;
991 end = start + cache->key.offset - 1;
992 set_extent_bits(&info->block_group_cache, start, end,
993 BLOCK_GROUP_DIRTY, GFP_NOFS);
995 old_val = btrfs_block_group_used(&cache->item);
996 num_bytes = min(total, cache->key.offset - byte_in_group);
998 if (cache->data != data &&
999 old_val < (cache->key.offset >> 1)) {
1004 bit_to_clear = BLOCK_GROUP_METADATA;
1005 bit_to_set = BLOCK_GROUP_DATA;
1006 cache->item.flags &=
1007 ~BTRFS_BLOCK_GROUP_MIXED;
1008 cache->item.flags |=
1009 BTRFS_BLOCK_GROUP_DATA;
1011 bit_to_clear = BLOCK_GROUP_DATA;
1012 bit_to_set = BLOCK_GROUP_METADATA;
1013 cache->item.flags &=
1014 ~BTRFS_BLOCK_GROUP_MIXED;
1015 cache->item.flags &=
1016 ~BTRFS_BLOCK_GROUP_DATA;
1018 clear_extent_bits(&info->block_group_cache,
1019 start, end, bit_to_clear,
1021 set_extent_bits(&info->block_group_cache,
1022 start, end, bit_to_set,
1024 } else if (cache->data != data &&
1025 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
1026 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1027 set_extent_bits(&info->block_group_cache,
1030 BLOCK_GROUP_METADATA,
1033 old_val += num_bytes;
1035 old_val -= num_bytes;
1037 set_extent_dirty(&info->free_space_cache,
1038 bytenr, bytenr + num_bytes - 1,
1042 btrfs_set_block_group_used(&cache->item, old_val);
1044 bytenr += num_bytes;
1048 static int update_pinned_extents(struct btrfs_root *root,
1049 u64 bytenr, u64 num, int pin)
1052 struct btrfs_block_group_cache *cache;
1053 struct btrfs_fs_info *fs_info = root->fs_info;
1056 set_extent_dirty(&fs_info->pinned_extents,
1057 bytenr, bytenr + num - 1, GFP_NOFS);
1059 clear_extent_dirty(&fs_info->pinned_extents,
1060 bytenr, bytenr + num - 1, GFP_NOFS);
1063 cache = btrfs_lookup_block_group(fs_info, bytenr);
1065 len = min(num, cache->key.offset -
1066 (bytenr - cache->key.objectid));
1068 cache->pinned += len;
1069 fs_info->total_pinned += len;
1071 cache->pinned -= len;
1072 fs_info->total_pinned -= len;
1080 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
1085 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
1089 ret = find_first_extent_bit(pinned_extents, last,
1090 &start, &end, EXTENT_DIRTY);
1093 set_extent_dirty(copy, start, end, GFP_NOFS);
1099 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root,
1101 struct extent_map_tree *unpin)
1106 struct extent_map_tree *free_space_cache;
1107 free_space_cache = &root->fs_info->free_space_cache;
1110 ret = find_first_extent_bit(unpin, 0, &start, &end,
1114 update_pinned_extents(root, start, end + 1 - start, 0);
1115 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1116 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1121 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
1122 btrfs_root *extent_root)
1126 struct btrfs_fs_info *info = extent_root->fs_info;
1127 struct extent_buffer *eb;
1128 struct btrfs_path *path;
1129 struct btrfs_key ins;
1130 struct btrfs_disk_key first;
1131 struct btrfs_extent_item extent_item;
1136 btrfs_set_stack_extent_refs(&extent_item, 1);
1137 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
1138 path = btrfs_alloc_path();
1141 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1142 &end, EXTENT_LOCKED);
1146 ins.objectid = start;
1147 ins.offset = end + 1 - start;
1148 err = btrfs_insert_item(trans, extent_root, &ins,
1149 &extent_item, sizeof(extent_item));
1150 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1152 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1153 level = btrfs_header_level(eb);
1155 btrfs_item_key(eb, &first, 0);
1157 btrfs_node_key(eb, &first, 0);
1159 err = btrfs_insert_extent_backref(trans, extent_root, path,
1160 start, extent_root->root_key.objectid,
1162 btrfs_disk_key_objectid(&first));
1164 free_extent_buffer(eb);
1166 btrfs_free_path(path);
1170 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1174 struct extent_buffer *buf;
1177 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1179 if (btrfs_buffer_uptodate(buf)) {
1181 root->fs_info->running_transaction->transid;
1182 if (btrfs_header_generation(buf) == transid) {
1183 free_extent_buffer(buf);
1187 free_extent_buffer(buf);
1189 update_pinned_extents(root, bytenr, num_bytes, 1);
1191 set_extent_bits(&root->fs_info->pending_del,
1192 bytenr, bytenr + num_bytes - 1,
1193 EXTENT_LOCKED, GFP_NOFS);
1200 * remove an extent from the root, returns 0 on success
1202 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1203 *root, u64 bytenr, u64 num_bytes,
1204 u64 root_objectid, u64 ref_generation,
1205 u64 owner_objectid, u64 owner_offset, int pin,
1208 struct btrfs_path *path;
1209 struct btrfs_key key;
1210 struct btrfs_fs_info *info = root->fs_info;
1211 struct btrfs_root *extent_root = info->extent_root;
1212 struct extent_buffer *leaf;
1214 struct btrfs_extent_item *ei;
1217 key.objectid = bytenr;
1218 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1219 key.offset = num_bytes;
1221 path = btrfs_alloc_path();
1225 ret = lookup_extent_backref(trans, extent_root, path,
1226 bytenr, root_objectid,
1228 owner_objectid, owner_offset, 1);
1230 ret = btrfs_del_item(trans, extent_root, path);
1232 btrfs_print_leaf(extent_root, path->nodes[0]);
1234 printk("Unable to find ref byte nr %Lu root %Lu "
1235 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1236 root_objectid, ref_generation, owner_objectid,
1239 btrfs_release_path(extent_root, path);
1240 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1245 leaf = path->nodes[0];
1246 ei = btrfs_item_ptr(leaf, path->slots[0],
1247 struct btrfs_extent_item);
1248 refs = btrfs_extent_refs(leaf, ei);
1251 btrfs_set_extent_refs(leaf, ei, refs);
1252 btrfs_mark_buffer_dirty(leaf);
1259 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
1265 /* block accounting for super block */
1266 super_used = btrfs_super_bytes_used(&info->super_copy);
1267 btrfs_set_super_bytes_used(&info->super_copy,
1268 super_used - num_bytes);
1270 /* block accounting for root item */
1271 root_used = btrfs_root_used(&root->root_item);
1272 btrfs_set_root_used(&root->root_item,
1273 root_used - num_bytes);
1275 ret = btrfs_del_item(trans, extent_root, path);
1279 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1283 btrfs_free_path(path);
1284 finish_current_insert(trans, extent_root);
1289 * find all the blocks marked as pending in the radix tree and remove
1290 * them from the extent map
1292 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1293 btrfs_root *extent_root)
1299 struct extent_map_tree *pending_del;
1300 struct extent_map_tree *pinned_extents;
1302 pending_del = &extent_root->fs_info->pending_del;
1303 pinned_extents = &extent_root->fs_info->pinned_extents;
1306 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1310 update_pinned_extents(extent_root, start, end + 1 - start, 1);
1311 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1313 ret = __free_extent(trans, extent_root,
1314 start, end + 1 - start,
1315 extent_root->root_key.objectid,
1324 * remove an extent from the root, returns 0 on success
1326 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1327 *root, u64 bytenr, u64 num_bytes,
1328 u64 root_objectid, u64 ref_generation,
1329 u64 owner_objectid, u64 owner_offset, int pin)
1331 struct btrfs_root *extent_root = root->fs_info->extent_root;
1335 WARN_ON(num_bytes < root->sectorsize);
1336 if (!root->ref_cows)
1339 if (root == extent_root) {
1340 pin_down_bytes(root, bytenr, num_bytes, 1);
1343 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1344 ref_generation, owner_objectid, owner_offset,
1346 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1347 return ret ? ret : pending_ret;
1350 static u64 stripe_align(struct btrfs_root *root, u64 val)
1352 u64 mask = ((u64)root->stripesize - 1);
1353 u64 ret = (val + mask) & ~mask;
1358 * walks the btree of allocated extents and find a hole of a given size.
1359 * The key ins is changed to record the hole:
1360 * ins->objectid == block start
1361 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1362 * ins->offset == number of blocks
1363 * Any available blocks before search_start are skipped.
1365 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1366 *orig_root, u64 num_bytes, u64 empty_size,
1367 u64 search_start, u64 search_end, u64 hint_byte,
1368 struct btrfs_key *ins, u64 exclude_start,
1369 u64 exclude_nr, int data)
1371 struct btrfs_path *path;
1372 struct btrfs_key key;
1378 u64 orig_search_start = search_start;
1380 struct extent_buffer *l;
1381 struct btrfs_root * root = orig_root->fs_info->extent_root;
1382 struct btrfs_fs_info *info = root->fs_info;
1383 u64 total_needed = num_bytes;
1385 struct btrfs_block_group_cache *block_group;
1390 WARN_ON(num_bytes < root->sectorsize);
1391 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1393 level = btrfs_header_level(root->node);
1395 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
1396 data = BTRFS_BLOCK_GROUP_MIXED;
1399 if (search_end == (u64)-1)
1400 search_end = btrfs_super_total_bytes(&info->super_copy);
1402 block_group = btrfs_lookup_block_group(info, hint_byte);
1404 hint_byte = search_start;
1405 block_group = btrfs_find_block_group(root, block_group,
1406 hint_byte, data, 1);
1408 block_group = btrfs_find_block_group(root,
1410 search_start, data, 1);
1413 total_needed += empty_size;
1414 path = btrfs_alloc_path();
1417 block_group = btrfs_lookup_block_group(info, search_start);
1419 block_group = btrfs_lookup_block_group(info,
1422 search_start = find_search_start(root, &block_group, search_start,
1423 total_needed, data, full_scan);
1424 search_start = stripe_align(root, search_start);
1425 cached_start = search_start;
1426 btrfs_init_path(path);
1427 ins->objectid = search_start;
1432 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1436 if (path->slots[0] > 0) {
1441 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1444 * walk backwards to find the first extent item key
1446 while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1447 if (path->slots[0] == 0) {
1448 ret = btrfs_prev_leaf(root, path);
1450 ret = btrfs_search_slot(trans, root, ins,
1454 if (path->slots[0] > 0)
1462 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1466 slot = path->slots[0];
1467 if (slot >= btrfs_header_nritems(l)) {
1468 ret = btrfs_next_leaf(root, path);
1474 search_start = max(search_start,
1475 block_group->key.objectid);
1477 aligned = stripe_align(root, search_start);
1478 ins->objectid = aligned;
1479 if (aligned >= search_end) {
1483 ins->offset = search_end - aligned;
1487 ins->objectid = stripe_align(root,
1488 last_byte > search_start ?
1489 last_byte : search_start);
1490 if (search_end <= ins->objectid) {
1494 ins->offset = search_end - ins->objectid;
1495 BUG_ON(ins->objectid >= search_end);
1498 btrfs_item_key_to_cpu(l, &key, slot);
1500 if (key.objectid >= search_start && key.objectid > last_byte &&
1502 if (last_byte < search_start)
1503 last_byte = search_start;
1504 aligned = stripe_align(root, last_byte);
1505 hole_size = key.objectid - aligned;
1506 if (key.objectid > aligned && hole_size >= num_bytes) {
1507 ins->objectid = aligned;
1508 ins->offset = hole_size;
1512 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1513 if (!start_found && btrfs_key_type(&key) ==
1514 BTRFS_BLOCK_GROUP_ITEM_KEY) {
1515 last_byte = key.objectid;
1523 last_byte = key.objectid + key.offset;
1525 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1526 last_byte >= block_group->key.objectid +
1527 block_group->key.offset) {
1528 btrfs_release_path(root, path);
1529 search_start = block_group->key.objectid +
1530 block_group->key.offset;
1538 /* we have to make sure we didn't find an extent that has already
1539 * been allocated by the map tree or the original allocation
1541 btrfs_release_path(root, path);
1542 BUG_ON(ins->objectid < search_start);
1544 if (ins->objectid + num_bytes >= search_end)
1546 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1547 ins->objectid + num_bytes > block_group->
1548 key.objectid + block_group->key.offset) {
1549 search_start = block_group->key.objectid +
1550 block_group->key.offset;
1553 if (test_range_bit(&info->extent_ins, ins->objectid,
1554 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1555 search_start = ins->objectid + num_bytes;
1558 if (test_range_bit(&info->pinned_extents, ins->objectid,
1559 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1560 search_start = ins->objectid + num_bytes;
1563 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1564 ins->objectid < exclude_start + exclude_nr)) {
1565 search_start = exclude_start + exclude_nr;
1569 block_group = btrfs_lookup_block_group(info, ins->objectid);
1571 trans->block_group = block_group;
1573 ins->offset = num_bytes;
1574 btrfs_free_path(path);
1578 if (search_start + num_bytes >= search_end) {
1580 search_start = orig_search_start;
1587 total_needed -= empty_size;
1589 data = BTRFS_BLOCK_GROUP_MIXED;
1593 block_group = btrfs_lookup_block_group(info, search_start);
1595 block_group = btrfs_find_block_group(root, block_group,
1596 search_start, data, 0);
1600 btrfs_release_path(root, path);
1601 btrfs_free_path(path);
1605 * finds a free extent and does all the dirty work required for allocation
1606 * returns the key for the extent through ins, and a tree buffer for
1607 * the first block of the extent through buf.
1609 * returns 0 if everything worked, non-zero otherwise.
1611 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1612 struct btrfs_root *root,
1613 u64 num_bytes, u64 root_objectid, u64 ref_generation,
1614 u64 owner, u64 owner_offset,
1615 u64 empty_size, u64 hint_byte,
1616 u64 search_end, struct btrfs_key *ins, int data)
1620 u64 super_used, root_used;
1621 u64 search_start = 0;
1623 struct btrfs_fs_info *info = root->fs_info;
1624 struct btrfs_root *extent_root = info->extent_root;
1625 struct btrfs_extent_item extent_item;
1626 struct btrfs_path *path;
1628 btrfs_set_stack_extent_refs(&extent_item, 1);
1630 new_hint = max(hint_byte, root->fs_info->alloc_start);
1631 if (new_hint < btrfs_super_total_bytes(&info->super_copy))
1632 hint_byte = new_hint;
1634 WARN_ON(num_bytes < root->sectorsize);
1635 ret = find_free_extent(trans, root, num_bytes, empty_size,
1636 search_start, search_end, hint_byte, ins,
1637 trans->alloc_exclude_start,
1638 trans->alloc_exclude_nr, data);
1643 /* block accounting for super block */
1644 super_used = btrfs_super_bytes_used(&info->super_copy);
1645 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1647 /* block accounting for root item */
1648 root_used = btrfs_root_used(&root->root_item);
1649 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1651 clear_extent_dirty(&root->fs_info->free_space_cache,
1652 ins->objectid, ins->objectid + ins->offset - 1,
1655 if (root == extent_root) {
1656 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1657 ins->objectid + ins->offset - 1,
1658 EXTENT_LOCKED, GFP_NOFS);
1663 WARN_ON(trans->alloc_exclude_nr);
1664 trans->alloc_exclude_start = ins->objectid;
1665 trans->alloc_exclude_nr = ins->offset;
1666 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1667 sizeof(extent_item));
1669 trans->alloc_exclude_start = 0;
1670 trans->alloc_exclude_nr = 0;
1673 path = btrfs_alloc_path();
1675 ret = btrfs_insert_extent_backref(trans, extent_root, path,
1676 ins->objectid, root_objectid,
1677 ref_generation, owner, owner_offset);
1680 btrfs_free_path(path);
1681 finish_current_insert(trans, extent_root);
1682 pending_ret = del_pending_extents(trans, extent_root);
1692 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1699 * helper function to allocate a block for a given tree
1700 * returns the tree buffer or NULL.
1702 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1703 struct btrfs_root *root,
1705 u64 root_objectid, u64 hint,
1711 ref_generation = trans->transid;
1716 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1717 ref_generation, 0, 0, hint, empty_size);
1721 * helper function to allocate a block for a given tree
1722 * returns the tree buffer or NULL.
1724 struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1725 struct btrfs_root *root,
1734 struct btrfs_key ins;
1736 struct extent_buffer *buf;
1738 ret = btrfs_alloc_extent(trans, root, blocksize,
1739 root_objectid, ref_generation,
1740 level, first_objectid, empty_size, hint,
1744 return ERR_PTR(ret);
1746 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1748 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1749 root->root_key.objectid, ref_generation,
1751 return ERR_PTR(-ENOMEM);
1753 btrfs_set_buffer_uptodate(buf);
1754 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1755 buf->start + buf->len - 1, GFP_NOFS);
1756 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1757 buf->start, buf->start + buf->len - 1,
1758 EXTENT_CSUM, GFP_NOFS);
1759 buf->flags |= EXTENT_CSUM;
1760 btrfs_set_buffer_defrag(buf);
1761 trans->blocks_used++;
1765 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1766 struct btrfs_root *root, struct extent_buffer *leaf)
1769 u64 leaf_generation;
1770 struct btrfs_key key;
1771 struct btrfs_file_extent_item *fi;
1776 BUG_ON(!btrfs_is_leaf(leaf));
1777 nritems = btrfs_header_nritems(leaf);
1778 leaf_owner = btrfs_header_owner(leaf);
1779 leaf_generation = btrfs_header_generation(leaf);
1781 for (i = 0; i < nritems; i++) {
1784 btrfs_item_key_to_cpu(leaf, &key, i);
1785 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1787 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1788 if (btrfs_file_extent_type(leaf, fi) ==
1789 BTRFS_FILE_EXTENT_INLINE)
1792 * FIXME make sure to insert a trans record that
1793 * repeats the snapshot del on crash
1795 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1796 if (disk_bytenr == 0)
1798 ret = btrfs_free_extent(trans, root, disk_bytenr,
1799 btrfs_file_extent_disk_num_bytes(leaf, fi),
1800 leaf_owner, leaf_generation,
1801 key.objectid, key.offset, 0);
1807 static void reada_walk_down(struct btrfs_root *root,
1808 struct extent_buffer *node)
1818 nritems = btrfs_header_nritems(node);
1819 level = btrfs_header_level(node);
1820 for (i = 0; i < nritems; i++) {
1821 bytenr = btrfs_node_blockptr(node, i);
1822 blocksize = btrfs_level_size(root, level - 1);
1823 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1827 mutex_unlock(&root->fs_info->fs_mutex);
1828 ret = readahead_tree_block(root, bytenr, blocksize);
1830 mutex_lock(&root->fs_info->fs_mutex);
1837 * helper function for drop_snapshot, this walks down the tree dropping ref
1838 * counts as it goes.
1840 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1841 *root, struct btrfs_path *path, int *level)
1846 struct extent_buffer *next;
1847 struct extent_buffer *cur;
1848 struct extent_buffer *parent;
1853 WARN_ON(*level < 0);
1854 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1855 ret = lookup_extent_ref(trans, root,
1856 path->nodes[*level]->start,
1857 path->nodes[*level]->len, &refs);
1863 * walk down to the last node level and free all the leaves
1865 while(*level >= 0) {
1866 WARN_ON(*level < 0);
1867 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1868 cur = path->nodes[*level];
1870 if (*level > 0 && path->slots[*level] == 0)
1871 reada_walk_down(root, cur);
1873 if (btrfs_header_level(cur) != *level)
1876 if (path->slots[*level] >=
1877 btrfs_header_nritems(cur))
1880 ret = drop_leaf_ref(trans, root, cur);
1884 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1885 blocksize = btrfs_level_size(root, *level - 1);
1886 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1889 parent = path->nodes[*level];
1890 root_owner = btrfs_header_owner(parent);
1891 root_gen = btrfs_header_generation(parent);
1892 path->slots[*level]++;
1893 ret = btrfs_free_extent(trans, root, bytenr,
1894 blocksize, root_owner,
1899 next = btrfs_find_tree_block(root, bytenr, blocksize);
1900 if (!next || !btrfs_buffer_uptodate(next)) {
1901 free_extent_buffer(next);
1902 mutex_unlock(&root->fs_info->fs_mutex);
1903 next = read_tree_block(root, bytenr, blocksize);
1904 mutex_lock(&root->fs_info->fs_mutex);
1906 /* we dropped the lock, check one more time */
1907 ret = lookup_extent_ref(trans, root, bytenr,
1911 parent = path->nodes[*level];
1912 root_owner = btrfs_header_owner(parent);
1913 root_gen = btrfs_header_generation(parent);
1915 path->slots[*level]++;
1916 free_extent_buffer(next);
1917 ret = btrfs_free_extent(trans, root, bytenr,
1925 WARN_ON(*level <= 0);
1926 if (path->nodes[*level-1])
1927 free_extent_buffer(path->nodes[*level-1]);
1928 path->nodes[*level-1] = next;
1929 *level = btrfs_header_level(next);
1930 path->slots[*level] = 0;
1933 WARN_ON(*level < 0);
1934 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1936 if (path->nodes[*level] == root->node) {
1937 root_owner = root->root_key.objectid;
1938 parent = path->nodes[*level];
1940 parent = path->nodes[*level + 1];
1941 root_owner = btrfs_header_owner(parent);
1944 root_gen = btrfs_header_generation(parent);
1945 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1946 path->nodes[*level]->len,
1947 root_owner, root_gen, 0, 0, 1);
1948 free_extent_buffer(path->nodes[*level]);
1949 path->nodes[*level] = NULL;
1956 * helper for dropping snapshots. This walks back up the tree in the path
1957 * to find the first node higher up where we haven't yet gone through
1960 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1961 *root, struct btrfs_path *path, int *level)
1965 struct btrfs_root_item *root_item = &root->root_item;
1970 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1971 slot = path->slots[i];
1972 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1973 struct extent_buffer *node;
1974 struct btrfs_disk_key disk_key;
1975 node = path->nodes[i];
1978 WARN_ON(*level == 0);
1979 btrfs_node_key(node, &disk_key, path->slots[i]);
1980 memcpy(&root_item->drop_progress,
1981 &disk_key, sizeof(disk_key));
1982 root_item->drop_level = i;
1985 if (path->nodes[*level] == root->node) {
1986 root_owner = root->root_key.objectid;
1988 btrfs_header_generation(path->nodes[*level]);
1990 struct extent_buffer *node;
1991 node = path->nodes[*level + 1];
1992 root_owner = btrfs_header_owner(node);
1993 root_gen = btrfs_header_generation(node);
1995 ret = btrfs_free_extent(trans, root,
1996 path->nodes[*level]->start,
1997 path->nodes[*level]->len,
1998 root_owner, root_gen, 0, 0, 1);
2000 free_extent_buffer(path->nodes[*level]);
2001 path->nodes[*level] = NULL;
2009 * drop the reference count on the tree rooted at 'snap'. This traverses
2010 * the tree freeing any blocks that have a ref count of zero after being
2013 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2019 struct btrfs_path *path;
2022 struct btrfs_root_item *root_item = &root->root_item;
2024 path = btrfs_alloc_path();
2027 level = btrfs_header_level(root->node);
2029 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2030 path->nodes[level] = root->node;
2031 extent_buffer_get(root->node);
2032 path->slots[level] = 0;
2034 struct btrfs_key key;
2035 struct btrfs_disk_key found_key;
2036 struct extent_buffer *node;
2038 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2039 level = root_item->drop_level;
2040 path->lowest_level = level;
2041 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2046 node = path->nodes[level];
2047 btrfs_node_key(node, &found_key, path->slots[level]);
2048 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2049 sizeof(found_key)));
2052 wret = walk_down_tree(trans, root, path, &level);
2058 wret = walk_up_tree(trans, root, path, &level);
2066 for (i = 0; i <= orig_level; i++) {
2067 if (path->nodes[i]) {
2068 free_extent_buffer(path->nodes[i]);
2069 path->nodes[i] = NULL;
2073 btrfs_free_path(path);
2077 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2084 ret = find_first_extent_bit(&info->block_group_cache, 0,
2085 &start, &end, (unsigned int)-1);
2088 ret = get_state_private(&info->block_group_cache, start, &ptr);
2090 kfree((void *)(unsigned long)ptr);
2091 clear_extent_bits(&info->block_group_cache, start,
2092 end, (unsigned int)-1, GFP_NOFS);
2095 ret = find_first_extent_bit(&info->free_space_cache, 0,
2096 &start, &end, EXTENT_DIRTY);
2099 clear_extent_dirty(&info->free_space_cache, start,
2105 static int relocate_inode_pages(struct inode *inode, u64 start, u64 len)
2110 u64 existing_delalloc;
2111 unsigned long last_index;
2112 unsigned long first_index;
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2117 struct file_ra_state ra;
2119 mutex_lock(&inode->i_mutex);
2120 first_index = start >> PAGE_CACHE_SHIFT;
2121 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2123 memset(&ra, 0, sizeof(ra));
2124 file_ra_state_init(&ra, inode->i_mapping);
2125 btrfs_force_ra(inode->i_mapping, &ra, NULL, first_index, last_index);
2127 for (i = first_index; i <= last_index; i++) {
2128 page = grab_cache_page(inode->i_mapping, i);
2131 if (!PageUptodate(page)) {
2132 btrfs_readpage(NULL, page);
2134 if (!PageUptodate(page)) {
2136 page_cache_release(page);
2140 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2141 page_end = page_start + PAGE_CACHE_SIZE - 1;
2143 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2145 delalloc_start = page_start;
2147 count_range_bits(&BTRFS_I(inode)->extent_tree,
2148 &delalloc_start, page_end,
2149 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
2151 set_extent_delalloc(em_tree, page_start,
2152 page_end, GFP_NOFS);
2154 spin_lock(&root->fs_info->delalloc_lock);
2155 root->fs_info->delalloc_bytes += PAGE_CACHE_SIZE -
2157 spin_unlock(&root->fs_info->delalloc_lock);
2159 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2160 set_page_dirty(page);
2162 page_cache_release(page);
2166 mutex_unlock(&inode->i_mutex);
2170 static int relocate_one_reference(struct btrfs_root *extent_root,
2171 struct btrfs_path *path,
2172 struct btrfs_key *extent_key,
2173 u64 ref_root, u64 ref_gen, u64 ref_objectid,
2176 struct inode *inode;
2177 struct btrfs_root *found_root;
2178 struct btrfs_key root_location;
2181 root_location.objectid = ref_root;
2183 root_location.offset = 0;
2185 root_location.offset = (u64)-1;
2186 root_location.type = BTRFS_ROOT_ITEM_KEY;
2188 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
2190 BUG_ON(!found_root);
2192 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2193 mutex_unlock(&extent_root->fs_info->fs_mutex);
2194 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2195 ref_objectid, found_root);
2196 if (inode->i_state & I_NEW) {
2197 /* the inode and parent dir are two different roots */
2198 BTRFS_I(inode)->root = found_root;
2199 BTRFS_I(inode)->location.objectid = ref_objectid;
2200 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2201 BTRFS_I(inode)->location.offset = 0;
2202 btrfs_read_locked_inode(inode);
2203 unlock_new_inode(inode);
2206 /* this can happen if the reference is not against
2207 * the latest version of the tree root
2209 if (is_bad_inode(inode)) {
2210 mutex_lock(&extent_root->fs_info->fs_mutex);
2213 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2214 /* FIXME, data=ordered will help get rid of this */
2215 filemap_fdatawrite(inode->i_mapping);
2217 mutex_lock(&extent_root->fs_info->fs_mutex);
2219 struct btrfs_trans_handle *trans;
2220 struct btrfs_key found_key;
2221 struct extent_buffer *eb;
2225 trans = btrfs_start_transaction(found_root, 1);
2226 eb = read_tree_block(found_root, extent_key->objectid,
2227 extent_key->offset);
2228 level = btrfs_header_level(eb);
2231 btrfs_item_key_to_cpu(eb, &found_key, 0);
2233 btrfs_node_key_to_cpu(eb, &found_key, 0);
2235 free_extent_buffer(eb);
2237 path->lowest_level = level;
2239 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2241 path->lowest_level = 0;
2242 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2243 if (!path->nodes[i])
2245 free_extent_buffer(path->nodes[i]);
2246 path->nodes[i] = NULL;
2248 btrfs_release_path(found_root, path);
2249 btrfs_end_transaction(trans, found_root);
2256 static int relocate_one_extent(struct btrfs_root *extent_root,
2257 struct btrfs_path *path,
2258 struct btrfs_key *extent_key)
2260 struct btrfs_key key;
2261 struct btrfs_key found_key;
2262 struct btrfs_extent_ref *ref;
2263 struct extent_buffer *leaf;
2272 key.objectid = extent_key->objectid;
2273 key.type = BTRFS_EXTENT_REF_KEY;
2277 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2285 leaf = path->nodes[0];
2286 nritems = btrfs_header_nritems(leaf);
2287 if (path->slots[0] == nritems)
2290 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2291 if (found_key.objectid != extent_key->objectid)
2294 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2297 key.offset = found_key.offset + 1;
2298 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2300 ref = btrfs_item_ptr(leaf, path->slots[0],
2301 struct btrfs_extent_ref);
2302 ref_root = btrfs_ref_root(leaf, ref);
2303 ref_gen = btrfs_ref_generation(leaf, ref);
2304 ref_objectid = btrfs_ref_objectid(leaf, ref);
2305 ref_offset = btrfs_ref_offset(leaf, ref);
2306 btrfs_release_path(extent_root, path);
2308 ret = relocate_one_reference(extent_root, path,
2309 extent_key, ref_root, ref_gen,
2310 ref_objectid, ref_offset);
2316 btrfs_release_path(extent_root, path);
2320 static int find_overlapping_extent(struct btrfs_root *root,
2321 struct btrfs_path *path, u64 new_size)
2323 struct btrfs_key found_key;
2324 struct extent_buffer *leaf;
2328 if (path->slots[0] == 0) {
2329 ret = btrfs_prev_leaf(root, path);
2338 leaf = path->nodes[0];
2339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2340 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2341 if (found_key.objectid + found_key.offset > new_size)
2350 int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
2352 struct btrfs_trans_handle *trans;
2353 struct btrfs_root *tree_root = root->fs_info->tree_root;
2354 struct btrfs_path *path;
2358 struct btrfs_fs_info *info = root->fs_info;
2359 struct extent_map_tree *block_group_cache;
2360 struct btrfs_key key;
2361 struct btrfs_key found_key = { 0, 0, 0 };
2362 struct extent_buffer *leaf;
2367 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2368 block_group_cache = &info->block_group_cache;
2369 path = btrfs_alloc_path();
2370 root = root->fs_info->extent_root;
2375 key.objectid = new_size;
2376 cur_byte = key.objectid;
2380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2384 leaf = path->nodes[0];
2385 if (key.objectid == new_size - 1) {
2386 ret = find_overlapping_extent(root, path, new_size);
2388 btrfs_release_path(root, path);
2389 ret = btrfs_search_slot(NULL, root, &key,
2395 nritems = btrfs_header_nritems(leaf);
2397 slot = path->slots[0];
2399 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2400 if (slot == nritems ||
2401 btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
2403 if (path->slots[0] >= nritems) {
2404 ret = btrfs_next_leaf(root, path);
2414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2415 if (found_key.objectid + found_key.offset <= cur_byte)
2418 cur_byte = found_key.objectid + found_key.offset;
2419 key.objectid = cur_byte;
2420 btrfs_release_path(root, path);
2421 ret = relocate_one_extent(root, path, &found_key);
2424 btrfs_release_path(root, path);
2426 if (total_found > 0) {
2427 trans = btrfs_start_transaction(tree_root, 1);
2428 btrfs_commit_transaction(trans, tree_root);
2430 mutex_unlock(&root->fs_info->fs_mutex);
2431 btrfs_clean_old_snapshots(tree_root);
2432 mutex_lock(&root->fs_info->fs_mutex);
2434 trans = btrfs_start_transaction(tree_root, 1);
2435 btrfs_commit_transaction(trans, tree_root);
2439 trans = btrfs_start_transaction(root, 1);
2440 key.objectid = new_size;
2444 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2448 leaf = path->nodes[0];
2449 nritems = btrfs_header_nritems(leaf);
2451 slot = path->slots[0];
2453 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2454 if (slot == nritems ||
2455 btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
2456 if (slot < nritems) {
2457 printk("shrinker found key %Lu %u %Lu\n",
2458 found_key.objectid, found_key.type,
2462 if (path->slots[0] >= nritems) {
2463 ret = btrfs_next_leaf(root, path);
2473 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2474 ret = get_state_private(&info->block_group_cache,
2475 found_key.objectid, &ptr);
2477 kfree((void *)(unsigned long)ptr);
2479 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2480 found_key.objectid + found_key.offset - 1,
2481 (unsigned int)-1, GFP_NOFS);
2483 key.objectid = found_key.objectid + 1;
2484 btrfs_del_item(trans, root, path);
2485 btrfs_release_path(root, path);
2487 clear_extent_dirty(&info->free_space_cache, new_size, (u64)-1,
2489 btrfs_commit_transaction(trans, root);
2491 btrfs_free_path(path);
2495 int btrfs_grow_extent_tree(struct btrfs_trans_handle *trans,
2496 struct btrfs_root *root, u64 new_size)
2498 struct btrfs_path *path;
2502 struct btrfs_block_group_cache *cache;
2503 struct btrfs_block_group_item *item;
2504 struct btrfs_fs_info *info = root->fs_info;
2505 struct extent_map_tree *block_group_cache;
2506 struct btrfs_key key;
2507 struct extent_buffer *leaf;
2511 old_size = btrfs_super_total_bytes(&info->super_copy);
2512 block_group_cache = &info->block_group_cache;
2514 root = info->extent_root;
2516 cache = btrfs_lookup_block_group(root->fs_info, old_size - 1);
2518 cur_byte = cache->key.objectid + cache->key.offset;
2519 if (cur_byte >= new_size)
2522 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2523 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2525 path = btrfs_alloc_path();
2529 while(cur_byte < new_size) {
2530 key.objectid = cur_byte;
2531 ret = btrfs_insert_empty_item(trans, root, path, &key,
2532 sizeof(struct btrfs_block_group_item));
2534 leaf = path->nodes[0];
2535 item = btrfs_item_ptr(leaf, path->slots[0],
2536 struct btrfs_block_group_item);
2538 btrfs_set_disk_block_group_used(leaf, item, 0);
2540 btrfs_set_disk_block_group_flags(leaf, item,
2541 BTRFS_BLOCK_GROUP_DATA);
2543 btrfs_set_disk_block_group_flags(leaf, item, 0);
2547 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2550 read_extent_buffer(leaf, &cache->item, (unsigned long)item,
2551 sizeof(cache->item));
2553 memcpy(&cache->key, &key, sizeof(key));
2556 cur_byte = key.objectid + key.offset;
2557 btrfs_release_path(root, path);
2559 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2560 bit = BLOCK_GROUP_DATA;
2561 cache->data = BTRFS_BLOCK_GROUP_DATA;
2563 bit = BLOCK_GROUP_METADATA;
2567 /* use EXTENT_LOCKED to prevent merging */
2568 set_extent_bits(block_group_cache, key.objectid,
2569 key.objectid + key.offset - 1,
2570 bit | EXTENT_LOCKED, GFP_NOFS);
2571 set_state_private(block_group_cache, key.objectid,
2572 (unsigned long)cache);
2574 btrfs_free_path(path);
2576 btrfs_set_super_total_bytes(&info->super_copy, new_size);
2580 int btrfs_read_block_groups(struct btrfs_root *root)
2582 struct btrfs_path *path;
2586 struct btrfs_block_group_cache *cache;
2587 struct btrfs_fs_info *info = root->fs_info;
2588 struct extent_map_tree *block_group_cache;
2589 struct btrfs_key key;
2590 struct btrfs_key found_key;
2591 struct extent_buffer *leaf;
2593 block_group_cache = &info->block_group_cache;
2595 root = info->extent_root;
2597 key.offset = BTRFS_BLOCK_GROUP_SIZE;
2598 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2600 path = btrfs_alloc_path();
2605 ret = btrfs_search_slot(NULL, info->extent_root,
2611 leaf = path->nodes[0];
2612 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2613 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2619 read_extent_buffer(leaf, &cache->item,
2620 btrfs_item_ptr_offset(leaf, path->slots[0]),
2621 sizeof(cache->item));
2622 memcpy(&cache->key, &found_key, sizeof(found_key));
2625 key.objectid = found_key.objectid + found_key.offset;
2626 btrfs_release_path(root, path);
2628 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
2629 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
2630 cache->data = BTRFS_BLOCK_GROUP_MIXED;
2631 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
2632 bit = BLOCK_GROUP_DATA;
2633 cache->data = BTRFS_BLOCK_GROUP_DATA;
2635 bit = BLOCK_GROUP_METADATA;
2639 /* use EXTENT_LOCKED to prevent merging */
2640 set_extent_bits(block_group_cache, found_key.objectid,
2641 found_key.objectid + found_key.offset - 1,
2642 bit | EXTENT_LOCKED, GFP_NOFS);
2643 set_state_private(block_group_cache, found_key.objectid,
2644 (unsigned long)cache);
2647 btrfs_super_total_bytes(&info->super_copy))
2651 btrfs_free_path(path);