2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
40 struct pending_extent_op {
51 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
52 btrfs_root *extent_root);
53 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
54 btrfs_root *extent_root);
56 void maybe_lock_mutex(struct btrfs_root *root)
60 void maybe_unlock_mutex(struct btrfs_root *root)
64 static int cache_block_group(struct btrfs_root *root,
65 struct btrfs_block_group_cache *block_group)
67 struct btrfs_path *path;
70 struct extent_buffer *leaf;
71 struct extent_io_tree *free_space_cache;
81 root = root->fs_info->extent_root;
82 free_space_cache = &root->fs_info->free_space_cache;
84 if (block_group->cached)
87 path = btrfs_alloc_path();
92 first_free = block_group->key.objectid;
93 key.objectid = block_group->key.objectid;
95 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
96 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
99 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
103 leaf = path->nodes[0];
104 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
105 if (key.objectid + key.offset > first_free)
106 first_free = key.objectid + key.offset;
109 leaf = path->nodes[0];
110 slot = path->slots[0];
111 if (slot >= btrfs_header_nritems(leaf)) {
112 ret = btrfs_next_leaf(root, path);
121 btrfs_item_key_to_cpu(leaf, &key, slot);
122 if (key.objectid < block_group->key.objectid) {
125 if (key.objectid >= block_group->key.objectid +
126 block_group->key.offset) {
130 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
135 if (key.objectid > last) {
136 hole_size = key.objectid - last;
137 set_extent_dirty(free_space_cache, last,
138 last + hole_size - 1,
141 last = key.objectid + key.offset;
149 if (block_group->key.objectid +
150 block_group->key.offset > last) {
151 hole_size = block_group->key.objectid +
152 block_group->key.offset - last;
153 set_extent_dirty(free_space_cache, last,
154 last + hole_size - 1, GFP_NOFS);
156 block_group->cached = 1;
158 btrfs_free_path(path);
162 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
166 struct extent_io_tree *block_group_cache;
167 struct btrfs_block_group_cache *block_group = NULL;
173 block_group_cache = &info->block_group_cache;
174 ret = find_first_extent_bit(block_group_cache,
175 bytenr, &start, &end,
176 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
181 ret = get_state_private(block_group_cache, start, &ptr);
185 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
186 if (block_group->key.objectid <= bytenr && bytenr <
187 block_group->key.objectid + block_group->key.offset)
192 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
194 return (cache->flags & bits) == bits;
197 static int noinline find_search_start(struct btrfs_root *root,
198 struct btrfs_block_group_cache **cache_ret,
199 u64 *start_ret, int num, int data)
202 struct btrfs_block_group_cache *cache = *cache_ret;
207 u64 search_start = *start_ret;
214 ret = cache_block_group(root, cache);
218 last = max(search_start, cache->key.objectid);
219 if (!block_group_bits(cache, data)) {
224 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
225 last, &start, &end, EXTENT_DIRTY);
232 start = max(last, start);
234 if (last - start < num) {
235 if (last == cache->key.objectid + cache->key.offset)
239 if (start + num > cache->key.objectid + cache->key.offset)
245 cache = btrfs_lookup_block_group(root->fs_info, search_start);
247 printk("Unable to find block group for %llu\n",
248 (unsigned long long)search_start);
254 last = cache->key.objectid + cache->key.offset;
256 cache = btrfs_lookup_block_group(root->fs_info, last);
266 if (cache_miss && !cache->cached) {
267 cache_block_group(root, cache);
269 cache = btrfs_lookup_block_group(root->fs_info, last);
271 cache = btrfs_find_block_group(root, cache, last, data, 0);
279 static u64 div_factor(u64 num, int factor)
288 static int block_group_state_bits(u64 flags)
291 if (flags & BTRFS_BLOCK_GROUP_DATA)
292 bits |= BLOCK_GROUP_DATA;
293 if (flags & BTRFS_BLOCK_GROUP_METADATA)
294 bits |= BLOCK_GROUP_METADATA;
295 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
296 bits |= BLOCK_GROUP_SYSTEM;
300 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
301 struct btrfs_block_group_cache
302 *hint, u64 search_start,
305 struct btrfs_block_group_cache *cache;
306 struct extent_io_tree *block_group_cache;
307 struct btrfs_block_group_cache *found_group = NULL;
308 struct btrfs_fs_info *info = root->fs_info;
321 block_group_cache = &info->block_group_cache;
326 bit = block_group_state_bits(data);
329 struct btrfs_block_group_cache *shint;
330 shint = btrfs_lookup_block_group(info, search_start);
331 if (shint && block_group_bits(shint, data)) {
332 used = btrfs_block_group_used(&shint->item);
333 if (used + shint->pinned <
334 div_factor(shint->key.offset, factor)) {
339 if (hint && block_group_bits(hint, data)) {
340 used = btrfs_block_group_used(&hint->item);
341 if (used + hint->pinned <
342 div_factor(hint->key.offset, factor)) {
345 last = hint->key.objectid + hint->key.offset;
349 hint_last = max(hint->key.objectid, search_start);
351 hint_last = search_start;
357 ret = find_first_extent_bit(block_group_cache, last,
362 ret = get_state_private(block_group_cache, start, &ptr);
366 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
367 last = cache->key.objectid + cache->key.offset;
368 used = btrfs_block_group_used(&cache->item);
370 if (block_group_bits(cache, data)) {
372 free_check = cache->key.offset;
374 free_check = div_factor(cache->key.offset,
377 if (used + cache->pinned < free_check) {
394 * Back reference rules. Back refs have three main goals:
396 * 1) differentiate between all holders of references to an extent so that
397 * when a reference is dropped we can make sure it was a valid reference
398 * before freeing the extent.
400 * 2) Provide enough information to quickly find the holders of an extent
401 * if we notice a given block is corrupted or bad.
403 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
404 * maintenance. This is actually the same as #2, but with a slightly
405 * different use case.
407 * File extents can be referenced by:
409 * - multiple snapshots, subvolumes, or different generations in one subvol
410 * - different files inside a single subvolume
411 * - different offsets inside a file (bookend extents in file.c)
413 * The extent ref structure has fields for:
415 * - Objectid of the subvolume root
416 * - Generation number of the tree holding the reference
417 * - objectid of the file holding the reference
418 * - offset in the file corresponding to the key holding the reference
419 * - number of references holding by parent node (alway 1 for tree blocks)
421 * Btree leaf may hold multiple references to a file extent. In most cases,
422 * these references are from same file and the corresponding offsets inside
423 * the file are close together. So inode objectid and offset in file are
424 * just hints, they provide hints about where in the btree the references
425 * can be found and when we can stop searching.
427 * When a file extent is allocated the fields are filled in:
428 * (root_key.objectid, trans->transid, inode objectid, offset in file, 1)
430 * When a leaf is cow'd new references are added for every file extent found
431 * in the leaf. It looks similar to the create case, but trans->transid will
432 * be different when the block is cow'd.
434 * (root_key.objectid, trans->transid, inode objectid, offset in file,
435 * number of references in the leaf)
437 * Because inode objectid and offset in file are just hints, they are not
438 * used when backrefs are deleted. When a file extent is removed either
439 * during snapshot deletion or file truncation, we find the corresponding
440 * back back reference and check the following fields.
442 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf))
444 * Btree extents can be referenced by:
446 * - Different subvolumes
447 * - Different generations of the same subvolume
449 * When a tree block is created, back references are inserted:
451 * (root->root_key.objectid, trans->transid, level, 0, 1)
453 * When a tree block is cow'd, new back references are added for all the
454 * blocks it points to. If the tree block isn't in reference counted root,
455 * the old back references are removed. These new back references are of
456 * the form (trans->transid will have increased since creation):
458 * (root->root_key.objectid, trans->transid, level, 0, 1)
460 * When a backref is in deleting, the following fields are checked:
462 * if backref was for a tree root:
463 * (btrfs_header_owner(itself), btrfs_header_generation(itself))
465 * (btrfs_header_owner(parent), btrfs_header_generation(parent))
467 * Back Reference Key composing:
469 * The key objectid corresponds to the first byte in the extent, the key
470 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
471 * byte of parent extent. If a extent is tree root, the key offset is set
472 * to the key objectid.
475 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
476 struct btrfs_root *root,
477 struct btrfs_path *path,
478 u64 bytenr, u64 parent,
479 u64 ref_root, u64 ref_generation,
480 u64 owner_objectid, int del)
482 struct btrfs_key key;
483 struct btrfs_extent_ref *ref;
484 struct extent_buffer *leaf;
488 key.objectid = bytenr;
489 key.type = BTRFS_EXTENT_REF_KEY;
492 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
500 leaf = path->nodes[0];
501 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
502 ref_objectid = btrfs_ref_objectid(leaf, ref);
503 if (btrfs_ref_root(leaf, ref) != ref_root ||
504 btrfs_ref_generation(leaf, ref) != ref_generation ||
505 (ref_objectid != owner_objectid &&
506 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
516 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct btrfs_path *path,
519 u64 bytenr, u64 parent,
520 u64 ref_root, u64 ref_generation,
523 struct btrfs_key key;
524 struct extent_buffer *leaf;
525 struct btrfs_extent_ref *ref;
529 key.objectid = bytenr;
530 key.type = BTRFS_EXTENT_REF_KEY;
533 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
535 leaf = path->nodes[0];
536 ref = btrfs_item_ptr(leaf, path->slots[0],
537 struct btrfs_extent_ref);
538 btrfs_set_ref_root(leaf, ref, ref_root);
539 btrfs_set_ref_generation(leaf, ref, ref_generation);
540 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
541 btrfs_set_ref_num_refs(leaf, ref, 1);
542 } else if (ret == -EEXIST) {
544 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
545 leaf = path->nodes[0];
546 ref = btrfs_item_ptr(leaf, path->slots[0],
547 struct btrfs_extent_ref);
548 if (btrfs_ref_root(leaf, ref) != ref_root ||
549 btrfs_ref_generation(leaf, ref) != ref_generation) {
555 num_refs = btrfs_ref_num_refs(leaf, ref);
556 BUG_ON(num_refs == 0);
557 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
559 existing_owner = btrfs_ref_objectid(leaf, ref);
560 if (existing_owner != owner_objectid &&
561 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
562 btrfs_set_ref_objectid(leaf, ref,
563 BTRFS_MULTIPLE_OBJECTIDS);
569 btrfs_mark_buffer_dirty(path->nodes[0]);
571 btrfs_release_path(root, path);
575 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root,
577 struct btrfs_path *path)
579 struct extent_buffer *leaf;
580 struct btrfs_extent_ref *ref;
584 leaf = path->nodes[0];
585 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
586 num_refs = btrfs_ref_num_refs(leaf, ref);
587 BUG_ON(num_refs == 0);
590 ret = btrfs_del_item(trans, root, path);
592 btrfs_set_ref_num_refs(leaf, ref, num_refs);
593 btrfs_mark_buffer_dirty(leaf);
595 btrfs_release_path(root, path);
599 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
600 struct btrfs_root *root, u64 bytenr,
601 u64 orig_parent, u64 parent,
602 u64 orig_root, u64 ref_root,
603 u64 orig_generation, u64 ref_generation,
607 struct btrfs_root *extent_root = root->fs_info->extent_root;
608 struct btrfs_path *path;
610 if (root == root->fs_info->extent_root) {
611 struct pending_extent_op *extent_op;
614 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
615 num_bytes = btrfs_level_size(root, (int)owner_objectid);
616 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
617 bytenr + num_bytes - 1, EXTENT_LOCKED, 0)) {
619 ret = get_state_private(&root->fs_info->extent_ins,
622 extent_op = (struct pending_extent_op *)
624 BUG_ON(extent_op->parent != orig_parent);
625 BUG_ON(extent_op->generation != orig_generation);
626 extent_op->parent = parent;
627 extent_op->generation = ref_generation;
629 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
632 extent_op->type = PENDING_BACKREF_UPDATE;
633 extent_op->bytenr = bytenr;
634 extent_op->num_bytes = num_bytes;
635 extent_op->parent = parent;
636 extent_op->orig_parent = orig_parent;
637 extent_op->generation = ref_generation;
638 extent_op->orig_generation = orig_generation;
639 extent_op->level = (int)owner_objectid;
641 set_extent_bits(&root->fs_info->extent_ins,
642 bytenr, bytenr + num_bytes - 1,
643 EXTENT_LOCKED, GFP_NOFS);
644 set_state_private(&root->fs_info->extent_ins,
645 bytenr, (unsigned long)extent_op);
650 path = btrfs_alloc_path();
653 ret = lookup_extent_backref(trans, extent_root, path,
654 bytenr, orig_parent, orig_root,
655 orig_generation, owner_objectid, 1);
658 ret = remove_extent_backref(trans, extent_root, path);
661 ret = insert_extent_backref(trans, extent_root, path, bytenr,
662 parent, ref_root, ref_generation,
665 finish_current_insert(trans, extent_root);
666 del_pending_extents(trans, extent_root);
668 btrfs_free_path(path);
672 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root, u64 bytenr,
674 u64 orig_parent, u64 parent,
675 u64 ref_root, u64 ref_generation,
679 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
680 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
682 maybe_lock_mutex(root);
683 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
684 parent, ref_root, ref_root,
685 ref_generation, ref_generation,
687 maybe_unlock_mutex(root);
691 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
692 struct btrfs_root *root, u64 bytenr,
693 u64 orig_parent, u64 parent,
694 u64 orig_root, u64 ref_root,
695 u64 orig_generation, u64 ref_generation,
698 struct btrfs_path *path;
700 struct btrfs_key key;
701 struct extent_buffer *l;
702 struct btrfs_extent_item *item;
705 path = btrfs_alloc_path();
710 key.objectid = bytenr;
711 key.type = BTRFS_EXTENT_ITEM_KEY;
712 key.offset = (u64)-1;
714 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
718 BUG_ON(ret == 0 || path->slots[0] == 0);
723 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
724 BUG_ON(key.objectid != bytenr);
725 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
727 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
728 refs = btrfs_extent_refs(l, item);
729 btrfs_set_extent_refs(l, item, refs + 1);
730 btrfs_mark_buffer_dirty(path->nodes[0]);
732 btrfs_release_path(root->fs_info->extent_root, path);
735 ret = insert_extent_backref(trans, root->fs_info->extent_root,
736 path, bytenr, parent,
737 ref_root, ref_generation,
740 finish_current_insert(trans, root->fs_info->extent_root);
741 del_pending_extents(trans, root->fs_info->extent_root);
743 btrfs_free_path(path);
747 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root,
749 u64 bytenr, u64 num_bytes, u64 parent,
750 u64 ref_root, u64 ref_generation,
754 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
755 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
757 maybe_lock_mutex(root);
758 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
759 0, ref_root, 0, ref_generation,
761 maybe_unlock_mutex(root);
765 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
766 struct btrfs_root *root)
768 finish_current_insert(trans, root->fs_info->extent_root);
769 del_pending_extents(trans, root->fs_info->extent_root);
773 int lookup_extent_ref(struct btrfs_trans_handle *trans,
774 struct btrfs_root *root, u64 bytenr,
775 u64 num_bytes, u32 *refs)
777 struct btrfs_path *path;
779 struct btrfs_key key;
780 struct extent_buffer *l;
781 struct btrfs_extent_item *item;
783 WARN_ON(num_bytes < root->sectorsize);
784 path = btrfs_alloc_path();
786 key.objectid = bytenr;
787 key.offset = num_bytes;
788 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
789 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
794 btrfs_print_leaf(root, path->nodes[0]);
795 printk("failed to find block number %Lu\n", bytenr);
799 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
800 *refs = btrfs_extent_refs(l, item);
802 btrfs_free_path(path);
806 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
807 struct extent_buffer *orig_buf, struct extent_buffer *buf,
816 u32 nr_file_extents = 0;
817 struct btrfs_key key;
818 struct btrfs_file_extent_item *fi;
823 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
824 u64, u64, u64, u64, u64, u64, u64, u64);
826 ref_root = btrfs_header_owner(buf);
827 ref_generation = btrfs_header_generation(buf);
828 orig_root = btrfs_header_owner(orig_buf);
829 orig_generation = btrfs_header_generation(orig_buf);
831 nritems = btrfs_header_nritems(buf);
832 level = btrfs_header_level(buf);
834 if (root->ref_cows) {
835 process_func = __btrfs_inc_extent_ref;
838 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
840 process_func = __btrfs_update_extent_ref;
843 for (i = 0; i < nritems; i++) {
846 btrfs_item_key_to_cpu(buf, &key, i);
847 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
849 fi = btrfs_item_ptr(buf, i,
850 struct btrfs_file_extent_item);
851 if (btrfs_file_extent_type(buf, fi) ==
852 BTRFS_FILE_EXTENT_INLINE)
854 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
860 maybe_lock_mutex(root);
861 ret = process_func(trans, root, bytenr,
862 orig_buf->start, buf->start,
864 orig_generation, ref_generation,
866 maybe_unlock_mutex(root);
874 bytenr = btrfs_node_blockptr(buf, i);
875 maybe_lock_mutex(root);
876 ret = process_func(trans, root, bytenr,
877 orig_buf->start, buf->start,
879 orig_generation, ref_generation,
881 maybe_unlock_mutex(root);
892 *nr_extents = nr_file_extents;
894 *nr_extents = nritems;
900 for (i =0; i < faili; i++) {
903 btrfs_item_key_to_cpu(buf, &key, i);
904 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
906 fi = btrfs_item_ptr(buf, i,
907 struct btrfs_file_extent_item);
908 if (btrfs_file_extent_type(buf, fi) ==
909 BTRFS_FILE_EXTENT_INLINE)
911 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
912 if (disk_bytenr == 0)
914 err = btrfs_free_extent(trans, root, disk_bytenr,
915 btrfs_file_extent_disk_num_bytes(buf,
919 bytenr = btrfs_node_blockptr(buf, i);
920 err = btrfs_free_extent(trans, root, bytenr,
921 btrfs_level_size(root, level - 1), 0);
929 int btrfs_update_ref(struct btrfs_trans_handle *trans,
930 struct btrfs_root *root, struct extent_buffer *orig_buf,
931 struct extent_buffer *buf, int start_slot, int nr)
939 struct btrfs_key key;
940 struct btrfs_file_extent_item *fi;
946 BUG_ON(start_slot < 0);
947 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
949 ref_root = btrfs_header_owner(buf);
950 ref_generation = btrfs_header_generation(buf);
951 orig_root = btrfs_header_owner(orig_buf);
952 orig_generation = btrfs_header_generation(orig_buf);
953 level = btrfs_header_level(buf);
955 if (!root->ref_cows) {
957 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
961 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
964 btrfs_item_key_to_cpu(buf, &key, slot);
965 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
967 fi = btrfs_item_ptr(buf, slot,
968 struct btrfs_file_extent_item);
969 if (btrfs_file_extent_type(buf, fi) ==
970 BTRFS_FILE_EXTENT_INLINE)
972 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
976 maybe_lock_mutex(root);
977 ret = __btrfs_update_extent_ref(trans, root, bytenr,
978 orig_buf->start, buf->start,
980 orig_generation, ref_generation,
982 maybe_unlock_mutex(root);
986 bytenr = btrfs_node_blockptr(buf, slot);
987 maybe_lock_mutex(root);
988 ret = __btrfs_update_extent_ref(trans, root, bytenr,
989 orig_buf->start, buf->start,
991 orig_generation, ref_generation,
993 maybe_unlock_mutex(root);
1004 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct btrfs_path *path,
1007 struct btrfs_block_group_cache *cache)
1011 struct btrfs_root *extent_root = root->fs_info->extent_root;
1013 struct extent_buffer *leaf;
1015 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1020 leaf = path->nodes[0];
1021 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1022 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1023 btrfs_mark_buffer_dirty(leaf);
1024 btrfs_release_path(extent_root, path);
1026 finish_current_insert(trans, extent_root);
1027 pending_ret = del_pending_extents(trans, extent_root);
1036 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1037 struct btrfs_root *root)
1039 struct extent_io_tree *block_group_cache;
1040 struct btrfs_block_group_cache *cache;
1044 struct btrfs_path *path;
1050 block_group_cache = &root->fs_info->block_group_cache;
1051 path = btrfs_alloc_path();
1056 ret = find_first_extent_bit(block_group_cache, last,
1057 &start, &end, BLOCK_GROUP_DIRTY);
1062 ret = get_state_private(block_group_cache, start, &ptr);
1065 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1066 err = write_one_cache_group(trans, root,
1069 * if we fail to write the cache group, we want
1070 * to keep it marked dirty in hopes that a later
1077 clear_extent_bits(block_group_cache, start, end,
1078 BLOCK_GROUP_DIRTY, GFP_NOFS);
1080 btrfs_free_path(path);
1084 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1087 struct list_head *head = &info->space_info;
1088 struct list_head *cur;
1089 struct btrfs_space_info *found;
1090 list_for_each(cur, head) {
1091 found = list_entry(cur, struct btrfs_space_info, list);
1092 if (found->flags == flags)
1099 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1100 u64 total_bytes, u64 bytes_used,
1101 struct btrfs_space_info **space_info)
1103 struct btrfs_space_info *found;
1105 found = __find_space_info(info, flags);
1107 found->total_bytes += total_bytes;
1108 found->bytes_used += bytes_used;
1109 WARN_ON(found->total_bytes < found->bytes_used);
1110 *space_info = found;
1113 found = kmalloc(sizeof(*found), GFP_NOFS);
1117 list_add(&found->list, &info->space_info);
1118 found->flags = flags;
1119 found->total_bytes = total_bytes;
1120 found->bytes_used = bytes_used;
1121 found->bytes_pinned = 0;
1123 *space_info = found;
1128 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1130 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1131 BTRFS_BLOCK_GROUP_RAID1 |
1132 BTRFS_BLOCK_GROUP_DUP);
1134 if (flags & BTRFS_BLOCK_GROUP_DATA)
1135 fs_info->avail_data_alloc_bits |= extra_flags;
1136 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1137 fs_info->avail_metadata_alloc_bits |= extra_flags;
1138 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1139 fs_info->avail_system_alloc_bits |= extra_flags;
1143 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *extent_root, u64 alloc_bytes,
1147 struct btrfs_space_info *space_info;
1153 space_info = __find_space_info(extent_root->fs_info, flags);
1155 ret = update_space_info(extent_root->fs_info, flags,
1159 BUG_ON(!space_info);
1161 if (space_info->full)
1164 thresh = div_factor(space_info->total_bytes, 7);
1165 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1169 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1170 if (ret == -ENOSPC) {
1171 space_info->full = 1;
1177 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1178 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1183 static int update_block_group(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root,
1185 u64 bytenr, u64 num_bytes, int alloc,
1188 struct btrfs_block_group_cache *cache;
1189 struct btrfs_fs_info *info = root->fs_info;
1190 u64 total = num_bytes;
1197 cache = btrfs_lookup_block_group(info, bytenr);
1201 byte_in_group = bytenr - cache->key.objectid;
1202 WARN_ON(byte_in_group > cache->key.offset);
1203 start = cache->key.objectid;
1204 end = start + cache->key.offset - 1;
1205 set_extent_bits(&info->block_group_cache, start, end,
1206 BLOCK_GROUP_DIRTY, GFP_NOFS);
1208 old_val = btrfs_block_group_used(&cache->item);
1209 num_bytes = min(total, cache->key.offset - byte_in_group);
1211 old_val += num_bytes;
1212 cache->space_info->bytes_used += num_bytes;
1214 old_val -= num_bytes;
1215 cache->space_info->bytes_used -= num_bytes;
1217 set_extent_dirty(&info->free_space_cache,
1218 bytenr, bytenr + num_bytes - 1,
1222 btrfs_set_block_group_used(&cache->item, old_val);
1224 bytenr += num_bytes;
1229 static int update_pinned_extents(struct btrfs_root *root,
1230 u64 bytenr, u64 num, int pin)
1233 struct btrfs_block_group_cache *cache;
1234 struct btrfs_fs_info *fs_info = root->fs_info;
1237 set_extent_dirty(&fs_info->pinned_extents,
1238 bytenr, bytenr + num - 1, GFP_NOFS);
1240 clear_extent_dirty(&fs_info->pinned_extents,
1241 bytenr, bytenr + num - 1, GFP_NOFS);
1244 cache = btrfs_lookup_block_group(fs_info, bytenr);
1246 len = min(num, cache->key.offset -
1247 (bytenr - cache->key.objectid));
1249 cache->pinned += len;
1250 cache->space_info->bytes_pinned += len;
1251 fs_info->total_pinned += len;
1253 cache->pinned -= len;
1254 cache->space_info->bytes_pinned -= len;
1255 fs_info->total_pinned -= len;
1263 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1268 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1272 ret = find_first_extent_bit(pinned_extents, last,
1273 &start, &end, EXTENT_DIRTY);
1276 set_extent_dirty(copy, start, end, GFP_NOFS);
1282 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1283 struct btrfs_root *root,
1284 struct extent_io_tree *unpin)
1289 struct extent_io_tree *free_space_cache;
1290 free_space_cache = &root->fs_info->free_space_cache;
1293 ret = find_first_extent_bit(unpin, 0, &start, &end,
1297 update_pinned_extents(root, start, end + 1 - start, 0);
1298 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1299 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1304 static int finish_current_insert(struct btrfs_trans_handle *trans,
1305 struct btrfs_root *extent_root)
1310 struct btrfs_fs_info *info = extent_root->fs_info;
1311 struct btrfs_path *path;
1312 struct btrfs_extent_ref *ref;
1313 struct pending_extent_op *extent_op;
1314 struct btrfs_key key;
1315 struct btrfs_extent_item extent_item;
1319 btrfs_set_stack_extent_refs(&extent_item, 1);
1320 path = btrfs_alloc_path();
1323 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1324 &end, EXTENT_LOCKED);
1328 ret = get_state_private(&info->extent_ins, start, &priv);
1330 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1332 if (extent_op->type == PENDING_EXTENT_INSERT) {
1333 key.objectid = start;
1334 key.offset = end + 1 - start;
1335 key.type = BTRFS_EXTENT_ITEM_KEY;
1336 err = btrfs_insert_item(trans, extent_root, &key,
1337 &extent_item, sizeof(extent_item));
1340 clear_extent_bits(&info->extent_ins, start, end,
1341 EXTENT_LOCKED, GFP_NOFS);
1343 err = insert_extent_backref(trans, extent_root, path,
1344 start, extent_op->parent,
1345 extent_root->root_key.objectid,
1346 extent_op->generation,
1349 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1350 err = lookup_extent_backref(trans, extent_root, path,
1351 start, extent_op->orig_parent,
1352 extent_root->root_key.objectid,
1353 extent_op->orig_generation,
1354 extent_op->level, 0);
1357 clear_extent_bits(&info->extent_ins, start, end,
1358 EXTENT_LOCKED, GFP_NOFS);
1360 key.objectid = start;
1361 key.offset = extent_op->parent;
1362 key.type = BTRFS_EXTENT_REF_KEY;
1363 err = btrfs_set_item_key_safe(trans, extent_root, path,
1366 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1367 struct btrfs_extent_ref);
1368 btrfs_set_ref_generation(path->nodes[0], ref,
1369 extent_op->generation);
1370 btrfs_mark_buffer_dirty(path->nodes[0]);
1371 btrfs_release_path(extent_root, path);
1377 btrfs_free_path(path);
1381 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *root,
1383 u64 bytenr, u64 num_bytes, int is_data)
1386 struct extent_buffer *buf;
1391 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1395 /* we can reuse a block if it hasn't been written
1396 * and it is from this transaction. We can't
1397 * reuse anything from the tree log root because
1398 * it has tiny sub-transactions.
1400 if (btrfs_buffer_uptodate(buf, 0)) {
1401 u64 header_owner = btrfs_header_owner(buf);
1402 u64 header_transid = btrfs_header_generation(buf);
1403 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1404 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
1405 header_transid == trans->transid &&
1406 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1407 clean_tree_block(NULL, root, buf);
1408 free_extent_buffer(buf);
1412 free_extent_buffer(buf);
1414 update_pinned_extents(root, bytenr, num_bytes, 1);
1421 * remove an extent from the root, returns 0 on success
1423 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1424 *root, u64 bytenr, u64 num_bytes, u64 parent,
1425 u64 root_objectid, u64 ref_generation,
1426 u64 owner_objectid, int pin, int mark_free)
1428 struct btrfs_path *path;
1429 struct btrfs_key key;
1430 struct btrfs_fs_info *info = root->fs_info;
1431 struct btrfs_extent_ops *ops = info->extent_ops;
1432 struct btrfs_root *extent_root = info->extent_root;
1433 struct extent_buffer *leaf;
1435 int extent_slot = 0;
1436 int found_extent = 0;
1438 struct btrfs_extent_item *ei;
1441 key.objectid = bytenr;
1442 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1443 key.offset = num_bytes;
1445 path = btrfs_alloc_path();
1449 ret = lookup_extent_backref(trans, extent_root, path,
1450 bytenr, parent, root_objectid,
1451 ref_generation, owner_objectid, 1);
1453 struct btrfs_key found_key;
1454 extent_slot = path->slots[0];
1455 while(extent_slot > 0) {
1457 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1459 if (found_key.objectid != bytenr)
1461 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1462 found_key.offset == num_bytes) {
1466 if (path->slots[0] - extent_slot > 5)
1469 if (!found_extent) {
1470 ret = remove_extent_backref(trans, extent_root, path);
1472 btrfs_release_path(extent_root, path);
1473 ret = btrfs_search_slot(trans, extent_root,
1476 extent_slot = path->slots[0];
1479 btrfs_print_leaf(extent_root, path->nodes[0]);
1480 printk("Unable to find ref byte nr %llu root %llu "
1481 " gen %llu owner %llu\n",
1482 (unsigned long long)bytenr,
1483 (unsigned long long)root_objectid,
1484 (unsigned long long)ref_generation,
1485 (unsigned long long)owner_objectid);
1489 leaf = path->nodes[0];
1490 ei = btrfs_item_ptr(leaf, extent_slot,
1491 struct btrfs_extent_item);
1492 refs = btrfs_extent_refs(leaf, ei);
1495 btrfs_set_extent_refs(leaf, ei, refs);
1497 btrfs_mark_buffer_dirty(leaf);
1499 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1500 struct btrfs_extent_ref *ref;
1501 ref = btrfs_item_ptr(leaf, path->slots[0],
1502 struct btrfs_extent_ref);
1503 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1504 /* if the back ref and the extent are next to each other
1505 * they get deleted below in one shot
1507 path->slots[0] = extent_slot;
1509 } else if (found_extent) {
1510 /* otherwise delete the extent back ref */
1511 ret = remove_extent_backref(trans, extent_root, path);
1513 /* if refs are 0, we need to setup the path for deletion */
1515 btrfs_release_path(extent_root, path);
1516 ret = btrfs_search_slot(trans, extent_root, &key, path,
1529 ret = pin_down_bytes(trans, root, bytenr, num_bytes, 0);
1535 /* block accounting for super block */
1536 super_used = btrfs_super_bytes_used(&info->super_copy);
1537 btrfs_set_super_bytes_used(&info->super_copy,
1538 super_used - num_bytes);
1540 /* block accounting for root item */
1541 root_used = btrfs_root_used(&root->root_item);
1542 btrfs_set_root_used(&root->root_item,
1543 root_used - num_bytes);
1544 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1549 if (ops && ops->free_extent)
1550 ops->free_extent(root, bytenr, num_bytes);
1552 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1556 btrfs_free_path(path);
1557 finish_current_insert(trans, extent_root);
1562 * find all the blocks marked as pending in the radix tree and remove
1563 * them from the extent map
1565 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1566 btrfs_root *extent_root)
1574 struct extent_io_tree *pending_del;
1575 struct extent_io_tree *extent_ins;
1576 struct pending_extent_op *extent_op;
1578 extent_ins = &extent_root->fs_info->extent_ins;
1579 pending_del = &extent_root->fs_info->pending_del;
1582 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1587 ret = get_state_private(pending_del, start, &priv);
1589 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1591 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1594 ret = pin_down_bytes(trans, extent_root, start,
1595 end + 1 - start, 0);
1596 mark_free = ret > 0;
1597 if (!test_range_bit(extent_ins, start, end,
1598 EXTENT_LOCKED, 0)) {
1600 ret = __free_extent(trans, extent_root,
1601 start, end + 1 - start,
1602 extent_op->orig_parent,
1603 extent_root->root_key.objectid,
1604 extent_op->orig_generation,
1605 extent_op->level, 0, mark_free);
1609 ret = get_state_private(extent_ins, start, &priv);
1611 extent_op = (struct pending_extent_op *)
1612 (unsigned long)priv;
1614 clear_extent_bits(extent_ins, start, end,
1615 EXTENT_LOCKED, GFP_NOFS);
1617 if (extent_op->type == PENDING_BACKREF_UPDATE)
1620 ret = update_block_group(trans, extent_root, start,
1621 end + 1 - start, 0, mark_free);
1632 * remove an extent from the root, returns 0 on success
1634 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1635 *root, u64 bytenr, u64 num_bytes, u64 parent,
1636 u64 root_objectid, u64 ref_generation,
1637 u64 owner_objectid, int pin)
1639 struct btrfs_root *extent_root = root->fs_info->extent_root;
1643 WARN_ON(num_bytes < root->sectorsize);
1644 if (root == extent_root) {
1645 struct pending_extent_op *extent_op;
1647 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1650 extent_op->type = PENDING_EXTENT_DELETE;
1651 extent_op->bytenr = bytenr;
1652 extent_op->num_bytes = num_bytes;
1653 extent_op->parent = parent;
1654 extent_op->orig_parent = parent;
1655 extent_op->generation = ref_generation;
1656 extent_op->orig_generation = ref_generation;
1657 extent_op->level = (int)owner_objectid;
1659 set_extent_bits(&root->fs_info->pending_del,
1660 bytenr, bytenr + num_bytes - 1,
1661 EXTENT_LOCKED, GFP_NOFS);
1662 set_state_private(&root->fs_info->pending_del,
1663 bytenr, (unsigned long)extent_op);
1666 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
1667 root_objectid, ref_generation,
1668 owner_objectid, pin, pin == 0);
1669 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
1670 return ret ? ret : pending_ret;
1673 static u64 stripe_align(struct btrfs_root *root, u64 val)
1675 u64 mask = ((u64)root->stripesize - 1);
1676 u64 ret = (val + mask) & ~mask;
1681 * walks the btree of allocated extents and find a hole of a given size.
1682 * The key ins is changed to record the hole:
1683 * ins->objectid == block start
1684 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1685 * ins->offset == number of blocks
1686 * Any available blocks before search_start are skipped.
1688 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1689 struct btrfs_root *orig_root,
1690 u64 num_bytes, u64 empty_size,
1691 u64 search_start, u64 search_end,
1692 u64 hint_byte, struct btrfs_key *ins,
1693 u64 exclude_start, u64 exclude_nr,
1697 u64 orig_search_start = search_start;
1698 struct btrfs_root * root = orig_root->fs_info->extent_root;
1699 struct btrfs_fs_info *info = root->fs_info;
1700 u64 total_needed = num_bytes;
1701 struct btrfs_block_group_cache *block_group;
1705 WARN_ON(num_bytes < root->sectorsize);
1706 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1708 if (search_end == (u64)-1)
1709 search_end = btrfs_super_total_bytes(&info->super_copy);
1712 block_group = btrfs_lookup_block_group(info, hint_byte);
1714 hint_byte = search_start;
1715 block_group = btrfs_find_block_group(root, block_group,
1716 hint_byte, data, 1);
1718 block_group = btrfs_find_block_group(root,
1720 search_start, data, 1);
1723 total_needed += empty_size;
1727 block_group = btrfs_lookup_block_group(info, search_start);
1729 block_group = btrfs_lookup_block_group(info,
1732 ret = find_search_start(root, &block_group, &search_start,
1733 total_needed, data);
1737 search_start = stripe_align(root, search_start);
1738 ins->objectid = search_start;
1739 ins->offset = num_bytes;
1741 if (ins->objectid + num_bytes >= search_end)
1744 if (ins->objectid + num_bytes >
1745 block_group->key.objectid + block_group->key.offset) {
1746 search_start = block_group->key.objectid +
1747 block_group->key.offset;
1751 if (test_range_bit(&info->extent_ins, ins->objectid,
1752 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1753 search_start = ins->objectid + num_bytes;
1757 if (test_range_bit(&info->pinned_extents, ins->objectid,
1758 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1759 search_start = ins->objectid + num_bytes;
1763 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1764 ins->objectid < exclude_start + exclude_nr)) {
1765 search_start = exclude_start + exclude_nr;
1769 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
1770 block_group = btrfs_lookup_block_group(info, ins->objectid);
1772 trans->block_group = block_group;
1774 ins->offset = num_bytes;
1778 if (search_start + num_bytes >= search_end) {
1780 search_start = orig_search_start;
1787 total_needed -= empty_size;
1792 block_group = btrfs_lookup_block_group(info, search_start);
1794 block_group = btrfs_find_block_group(root, block_group,
1795 search_start, data, 0);
1802 * finds a free extent and does all the dirty work required for allocation
1803 * returns the key for the extent through ins, and a tree buffer for
1804 * the first block of the extent through buf.
1806 * returns 0 if everything worked, non-zero otherwise.
1808 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 u64 num_bytes, u64 parent,
1811 u64 root_objectid, u64 ref_generation,
1812 u64 owner, u64 empty_size, u64 hint_byte,
1813 u64 search_end, struct btrfs_key *ins, int data)
1817 u64 super_used, root_used;
1818 u64 search_start = 0;
1821 struct btrfs_fs_info *info = root->fs_info;
1822 struct btrfs_root *extent_root = info->extent_root;
1823 struct btrfs_path *path;
1824 struct btrfs_extent_item *extent_item;
1825 struct btrfs_extent_ref *ref;
1826 struct btrfs_key keys[2];
1828 if (info->extent_ops) {
1829 struct btrfs_extent_ops *ops = info->extent_ops;
1830 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
1836 alloc_profile = info->avail_data_alloc_bits &
1837 info->data_alloc_profile;
1838 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1839 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
1840 info->system_allocs >= 0) {
1841 alloc_profile = info->avail_system_alloc_bits &
1842 info->system_alloc_profile;
1843 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1845 alloc_profile = info->avail_metadata_alloc_bits &
1846 info->metadata_alloc_profile;
1847 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1850 if (root->ref_cows) {
1851 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
1852 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1854 BTRFS_BLOCK_GROUP_METADATA);
1857 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1858 num_bytes + 2 * 1024 * 1024, data);
1862 WARN_ON(num_bytes < root->sectorsize);
1863 ret = find_free_extent(trans, root, num_bytes, empty_size,
1864 search_start, search_end, hint_byte, ins,
1865 trans->alloc_exclude_start,
1866 trans->alloc_exclude_nr, data);
1873 parent = ins->objectid;
1875 /* block accounting for super block */
1876 super_used = btrfs_super_bytes_used(&info->super_copy);
1877 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1879 /* block accounting for root item */
1880 root_used = btrfs_root_used(&root->root_item);
1881 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1883 clear_extent_dirty(&root->fs_info->free_space_cache,
1884 ins->objectid, ins->objectid + ins->offset - 1,
1887 if (root == extent_root) {
1888 struct pending_extent_op *extent_op;
1890 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1893 extent_op->type = PENDING_EXTENT_INSERT;
1894 extent_op->bytenr = ins->objectid;
1895 extent_op->num_bytes = ins->offset;
1896 extent_op->parent = parent;
1897 extent_op->orig_parent = 0;
1898 extent_op->generation = ref_generation;
1899 extent_op->orig_generation = 0;
1900 extent_op->level = (int)owner;
1902 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1903 ins->objectid + ins->offset - 1,
1904 EXTENT_LOCKED, GFP_NOFS);
1905 set_state_private(&root->fs_info->extent_ins,
1906 ins->objectid, (unsigned long)extent_op);
1910 WARN_ON(trans->alloc_exclude_nr);
1911 trans->alloc_exclude_start = ins->objectid;
1912 trans->alloc_exclude_nr = ins->offset;
1914 memcpy(&keys[0], ins, sizeof(*ins));
1915 keys[1].objectid = ins->objectid;
1916 keys[1].type = BTRFS_EXTENT_REF_KEY;
1917 keys[1].offset = parent;
1918 sizes[0] = sizeof(*extent_item);
1919 sizes[1] = sizeof(*ref);
1921 path = btrfs_alloc_path();
1924 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1928 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1929 struct btrfs_extent_item);
1930 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1931 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1932 struct btrfs_extent_ref);
1934 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1935 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1936 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1937 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
1939 btrfs_mark_buffer_dirty(path->nodes[0]);
1941 trans->alloc_exclude_start = 0;
1942 trans->alloc_exclude_nr = 0;
1943 btrfs_free_path(path);
1944 finish_current_insert(trans, extent_root);
1945 pending_ret = del_pending_extents(trans, extent_root);
1955 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1957 printk("update block group failed for %llu %llu\n",
1958 (unsigned long long)ins->objectid,
1959 (unsigned long long)ins->offset);
1966 * helper function to allocate a block for a given tree
1967 * returns the tree buffer or NULL.
1969 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1970 struct btrfs_root *root,
1971 u32 blocksize, u64 parent,
1978 struct btrfs_key ins;
1980 struct extent_buffer *buf;
1982 ret = btrfs_alloc_extent(trans, root, blocksize, parent,
1983 root_objectid, ref_generation,
1984 level, empty_size, hint,
1988 return ERR_PTR(ret);
1990 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1993 parent = ins.objectid;
1994 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1995 parent, root->root_key.objectid,
1996 ref_generation, level, 0);
1998 return ERR_PTR(-ENOMEM);
2000 btrfs_set_buffer_uptodate(buf);
2001 trans->blocks_used++;
2005 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2006 struct btrfs_root *root,
2007 struct extent_buffer *leaf)
2010 u64 leaf_generation;
2011 struct btrfs_key key;
2012 struct btrfs_file_extent_item *fi;
2017 BUG_ON(!btrfs_is_leaf(leaf));
2018 nritems = btrfs_header_nritems(leaf);
2019 leaf_owner = btrfs_header_owner(leaf);
2020 leaf_generation = btrfs_header_generation(leaf);
2022 for (i = 0; i < nritems; i++) {
2025 btrfs_item_key_to_cpu(leaf, &key, i);
2026 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2028 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2029 if (btrfs_file_extent_type(leaf, fi) ==
2030 BTRFS_FILE_EXTENT_INLINE)
2033 * FIXME make sure to insert a trans record that
2034 * repeats the snapshot del on crash
2036 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2037 if (disk_bytenr == 0)
2039 ret = btrfs_free_extent(trans, root, disk_bytenr,
2040 btrfs_file_extent_disk_num_bytes(leaf, fi),
2041 leaf->start, leaf_owner, leaf_generation,
2048 static void noinline reada_walk_down(struct btrfs_root *root,
2049 struct extent_buffer *node,
2062 nritems = btrfs_header_nritems(node);
2063 level = btrfs_header_level(node);
2067 for (i = slot; i < nritems && skipped < 32; i++) {
2068 bytenr = btrfs_node_blockptr(node, i);
2069 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2070 (last > bytenr && last - bytenr > 32 * 1024))) {
2074 blocksize = btrfs_level_size(root, level - 1);
2076 ret = lookup_extent_ref(NULL, root, bytenr,
2084 mutex_unlock(&root->fs_info->fs_mutex);
2085 ret = readahead_tree_block(root, bytenr, blocksize,
2086 btrfs_node_ptr_generation(node, i));
2087 last = bytenr + blocksize;
2089 mutex_lock(&root->fs_info->fs_mutex);
2096 * helper function for drop_snapshot, this walks down the tree dropping ref
2097 * counts as it goes.
2099 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2100 struct btrfs_root *root,
2101 struct btrfs_path *path, int *level)
2107 struct extent_buffer *next;
2108 struct extent_buffer *cur;
2109 struct extent_buffer *parent;
2114 WARN_ON(*level < 0);
2115 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2116 ret = lookup_extent_ref(trans, root,
2117 path->nodes[*level]->start,
2118 path->nodes[*level]->len, &refs);
2124 * walk down to the last node level and free all the leaves
2126 while(*level >= 0) {
2127 WARN_ON(*level < 0);
2128 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2129 cur = path->nodes[*level];
2131 if (btrfs_header_level(cur) != *level)
2134 if (path->slots[*level] >=
2135 btrfs_header_nritems(cur))
2138 ret = drop_leaf_ref(trans, root, cur);
2142 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2143 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2144 blocksize = btrfs_level_size(root, *level - 1);
2145 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
2148 parent = path->nodes[*level];
2149 root_owner = btrfs_header_owner(parent);
2150 root_gen = btrfs_header_generation(parent);
2151 path->slots[*level]++;
2152 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2153 parent->start, root_owner,
2154 root_gen, *level - 1, 1);
2158 next = btrfs_find_tree_block(root, bytenr, blocksize);
2159 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2160 free_extent_buffer(next);
2161 reada_walk_down(root, cur, path->slots[*level]);
2162 mutex_unlock(&root->fs_info->fs_mutex);
2163 next = read_tree_block(root, bytenr, blocksize,
2165 mutex_lock(&root->fs_info->fs_mutex);
2167 WARN_ON(*level <= 0);
2168 if (path->nodes[*level-1])
2169 free_extent_buffer(path->nodes[*level-1]);
2170 path->nodes[*level-1] = next;
2171 *level = btrfs_header_level(next);
2172 path->slots[*level] = 0;
2175 WARN_ON(*level < 0);
2176 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2178 if (path->nodes[*level] == root->node) {
2179 root_owner = root->root_key.objectid;
2180 parent = path->nodes[*level];
2182 parent = path->nodes[*level + 1];
2183 root_owner = btrfs_header_owner(parent);
2186 root_gen = btrfs_header_generation(parent);
2187 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2188 path->nodes[*level]->len, parent->start,
2189 root_owner, root_gen, *level, 1);
2190 free_extent_buffer(path->nodes[*level]);
2191 path->nodes[*level] = NULL;
2198 * helper for dropping snapshots. This walks back up the tree in the path
2199 * to find the first node higher up where we haven't yet gone through
2202 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2203 struct btrfs_root *root,
2204 struct btrfs_path *path, int *level)
2208 struct btrfs_root_item *root_item = &root->root_item;
2213 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2214 slot = path->slots[i];
2215 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2216 struct extent_buffer *node;
2217 struct btrfs_disk_key disk_key;
2218 node = path->nodes[i];
2221 WARN_ON(*level == 0);
2222 btrfs_node_key(node, &disk_key, path->slots[i]);
2223 memcpy(&root_item->drop_progress,
2224 &disk_key, sizeof(disk_key));
2225 root_item->drop_level = i;
2228 struct extent_buffer *parent;
2229 if (path->nodes[*level] == root->node)
2230 parent = path->nodes[*level];
2232 parent = path->nodes[*level + 1];
2234 root_owner = btrfs_header_owner(parent);
2235 root_gen = btrfs_header_generation(parent);
2236 ret = btrfs_free_extent(trans, root,
2237 path->nodes[*level]->start,
2238 path->nodes[*level]->len,
2239 parent->start, root_owner,
2240 root_gen, *level, 1);
2242 free_extent_buffer(path->nodes[*level]);
2243 path->nodes[*level] = NULL;
2251 * drop the reference count on the tree rooted at 'snap'. This traverses
2252 * the tree freeing any blocks that have a ref count of zero after being
2255 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2261 struct btrfs_path *path;
2264 struct btrfs_root_item *root_item = &root->root_item;
2266 path = btrfs_alloc_path();
2269 level = btrfs_header_level(root->node);
2271 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2272 path->nodes[level] = root->node;
2273 extent_buffer_get(root->node);
2274 path->slots[level] = 0;
2276 struct btrfs_key key;
2277 struct btrfs_disk_key found_key;
2278 struct extent_buffer *node;
2280 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2281 level = root_item->drop_level;
2282 path->lowest_level = level;
2283 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2288 node = path->nodes[level];
2289 btrfs_node_key(node, &found_key, path->slots[level]);
2290 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2291 sizeof(found_key)));
2294 wret = walk_down_tree(trans, root, path, &level);
2300 wret = walk_up_tree(trans, root, path, &level);
2310 for (i = 0; i <= orig_level; i++) {
2311 if (path->nodes[i]) {
2312 free_extent_buffer(path->nodes[i]);
2313 path->nodes[i] = NULL;
2317 btrfs_free_path(path);
2321 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2328 ret = find_first_extent_bit(&info->block_group_cache, 0,
2329 &start, &end, (unsigned int)-1);
2332 ret = get_state_private(&info->block_group_cache, start, &ptr);
2334 kfree((void *)(unsigned long)ptr);
2335 clear_extent_bits(&info->block_group_cache, start,
2336 end, (unsigned int)-1, GFP_NOFS);
2339 ret = find_first_extent_bit(&info->free_space_cache, 0,
2340 &start, &end, EXTENT_DIRTY);
2343 clear_extent_dirty(&info->free_space_cache, start,
2349 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2350 struct btrfs_key *key)
2353 struct btrfs_key found_key;
2354 struct extent_buffer *leaf;
2357 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2361 slot = path->slots[0];
2362 leaf = path->nodes[0];
2363 if (slot >= btrfs_header_nritems(leaf)) {
2364 ret = btrfs_next_leaf(root, path);
2371 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2373 if (found_key.objectid >= key->objectid &&
2374 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2383 int btrfs_read_block_groups(struct btrfs_root *root)
2385 struct btrfs_path *path;
2388 struct btrfs_block_group_cache *cache;
2389 struct btrfs_fs_info *info = root->fs_info;
2390 struct btrfs_space_info *space_info;
2391 struct extent_io_tree *block_group_cache;
2392 struct btrfs_key key;
2393 struct btrfs_key found_key;
2394 struct extent_buffer *leaf;
2396 block_group_cache = &info->block_group_cache;
2398 root = info->extent_root;
2401 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2402 path = btrfs_alloc_path();
2407 ret = find_first_block_group(root, path, &key);
2415 leaf = path->nodes[0];
2416 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2417 cache = kmalloc(sizeof(*cache), GFP_NOFS);
2423 read_extent_buffer(leaf, &cache->item,
2424 btrfs_item_ptr_offset(leaf, path->slots[0]),
2425 sizeof(cache->item));
2426 memcpy(&cache->key, &found_key, sizeof(found_key));
2429 key.objectid = found_key.objectid + found_key.offset;
2430 btrfs_release_path(root, path);
2431 cache->flags = btrfs_block_group_flags(&cache->item);
2433 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
2434 bit = BLOCK_GROUP_DATA;
2435 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2436 bit = BLOCK_GROUP_SYSTEM;
2437 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
2438 bit = BLOCK_GROUP_METADATA;
2440 set_avail_alloc_bits(info, cache->flags);
2442 ret = update_space_info(info, cache->flags, found_key.offset,
2443 btrfs_block_group_used(&cache->item),
2446 cache->space_info = space_info;
2448 /* use EXTENT_LOCKED to prevent merging */
2449 set_extent_bits(block_group_cache, found_key.objectid,
2450 found_key.objectid + found_key.offset - 1,
2451 bit | EXTENT_LOCKED, GFP_NOFS);
2452 set_state_private(block_group_cache, found_key.objectid,
2453 (unsigned long)cache);
2456 btrfs_super_total_bytes(&info->super_copy))
2461 btrfs_free_path(path);
2465 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root, u64 bytes_used,
2467 u64 type, u64 chunk_objectid, u64 chunk_offset,
2472 struct btrfs_root *extent_root;
2473 struct btrfs_block_group_cache *cache;
2474 struct extent_io_tree *block_group_cache;
2476 extent_root = root->fs_info->extent_root;
2477 block_group_cache = &root->fs_info->block_group_cache;
2479 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2481 cache->key.objectid = chunk_offset;
2482 cache->key.offset = size;
2484 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2485 btrfs_set_block_group_used(&cache->item, bytes_used);
2486 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2487 cache->flags = type;
2488 btrfs_set_block_group_flags(&cache->item, type);
2490 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
2491 &cache->space_info);
2494 bit = block_group_state_bits(type);
2495 set_extent_bits(block_group_cache, chunk_offset,
2496 chunk_offset + size - 1,
2497 bit | EXTENT_LOCKED, GFP_NOFS);
2499 set_state_private(block_group_cache, chunk_offset,
2500 (unsigned long)cache);
2501 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2502 sizeof(cache->item));
2505 finish_current_insert(trans, extent_root);
2506 ret = del_pending_extents(trans, extent_root);
2508 set_avail_alloc_bits(extent_root->fs_info, type);
2513 * This is for converter use only.
2515 * In that case, we don't know where are free blocks located.
2516 * Therefore all block group cache entries must be setup properly
2517 * before doing any block allocation.
2519 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
2520 struct btrfs_root *root)
2528 u64 total_metadata = 0;
2532 struct btrfs_root *extent_root;
2533 struct btrfs_block_group_cache *cache;
2534 struct extent_io_tree *block_group_cache;
2536 extent_root = root->fs_info->extent_root;
2537 block_group_cache = &root->fs_info->block_group_cache;
2538 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2539 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2540 group_align = 64 * root->sectorsize;
2543 while (cur_start < total_bytes) {
2544 group_size = total_bytes / 12;
2545 group_size = min_t(u64, group_size, total_bytes - cur_start);
2546 if (cur_start == 0) {
2547 bit = BLOCK_GROUP_SYSTEM;
2548 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
2550 group_size &= ~(group_align - 1);
2551 group_size = max_t(u64, group_size, 32 * 1024 * 1024);
2552 group_size = min_t(u64, group_size, 128 * 1024 * 1024);
2554 group_size &= ~(group_align - 1);
2555 if (total_data >= total_metadata * 2) {
2556 group_type = BTRFS_BLOCK_GROUP_METADATA;
2557 group_size = min_t(u64, group_size,
2558 1ULL * 1024 * 1024 * 1024);
2559 total_metadata += group_size;
2561 group_type = BTRFS_BLOCK_GROUP_DATA;
2562 group_size = min_t(u64, group_size,
2563 5ULL * 1024 * 1024 * 1024);
2564 total_data += group_size;
2566 if ((total_bytes - cur_start) * 4 < group_size * 5)
2567 group_size = total_bytes - cur_start;
2570 cache = kzalloc(sizeof(*cache), GFP_NOFS);
2573 cache->key.objectid = cur_start;
2574 cache->key.offset = group_size;
2575 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2577 btrfs_set_block_group_used(&cache->item, 0);
2578 btrfs_set_block_group_chunk_objectid(&cache->item,
2580 btrfs_set_block_group_flags(&cache->item, group_type);
2582 cache->flags = group_type;
2584 ret = update_space_info(root->fs_info, group_type, group_size,
2585 0, &cache->space_info);
2587 set_avail_alloc_bits(extent_root->fs_info, group_type);
2589 set_extent_bits(block_group_cache, cur_start,
2590 cur_start + group_size - 1,
2591 bit | EXTENT_LOCKED, GFP_NOFS);
2592 set_state_private(block_group_cache, cur_start,
2593 (unsigned long)cache);
2594 cur_start += group_size;
2596 /* then insert all the items */
2598 while(cur_start < total_bytes) {
2599 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
2602 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2603 sizeof(cache->item));
2606 finish_current_insert(trans, extent_root);
2607 ret = del_pending_extents(trans, extent_root);
2610 cur_start = cache->key.objectid + cache->key.offset;
2615 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2616 struct btrfs_root *root,
2617 u64 bytenr, u64 num_bytes, int alloc,
2620 return update_block_group(trans, root, bytenr, num_bytes,