2 * Copyright (C) 2014 SUSE. 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.
18 * Authors: Mark Fasheh <mfasheh@suse.de>
23 #include <uuid/uuid.h>
24 #include "kerncompat.h"
25 #include "radix-tree.h"
28 #include "print-tree.h"
31 #include "rbtree-utils.h"
33 #include "qgroup-verify.h"
35 /*#define QGROUP_VERIFY_DEBUG*/
36 static unsigned long tot_extents_scanned
= 0;
38 static void add_bytes(u64 root_objectid
, u64 num_bytes
, int exclusive
);
44 struct btrfs_disk_key key
;
45 struct btrfs_qgroup_info_item diskinfo
;
47 struct btrfs_qgroup_info_item info
;
49 struct rb_node rb_node
;
54 unsigned int num_groups
;
55 } counts
= { .root
= RB_ROOT
};
57 struct rb_root by_bytenr
= RB_ROOT
;
60 * List of interior tree blocks. We walk this list after loading the
61 * extent tree to resolve implied refs. For each interior node we'll
62 * place a shared ref in the ref tree against each child object. This
63 * allows the shared ref resolving code to do the actual work later of
64 * finding roots to account against.
66 * An implied ref is when a tree block has refs on it that may not
67 * exist in any of its child nodes. Even though the refs might not
68 * exist further down the tree, the fact that our interior node has a
69 * ref means we need to account anything below it to all its roots.
71 struct ulist
*tree_blocks
= NULL
; /* unode->val = bytenr, ->aux
72 * = tree_block pointer */
84 struct rb_node bytenr_node
;
87 #ifdef QGROUP_VERIFY_DEBUG
88 static void print_ref(struct ref
*ref
)
90 printf("bytenr: %llu\t\tnum_bytes: %llu\t\t parent: %llu\t\t"
91 "root: %llu\n", ref
->bytenr
, ref
->num_bytes
,
92 ref
->parent
, ref
->root
);
95 static void print_all_refs(void)
97 unsigned long count
= 0;
101 node
= rb_first(&by_bytenr
);
103 ref
= rb_entry(node
, struct ref
, bytenr_node
);
108 node
= rb_next(node
);
111 printf("%lu extents scanned with %lu refs in total.\n",
112 tot_extents_scanned
, count
);
117 * Store by bytenr in rbtree
119 * The tree is sorted in ascending order by bytenr, then parent, then
120 * root. Since full refs have a parent == 0, those will come before
123 static int compare_ref(struct ref
*orig
, u64 bytenr
, u64 root
, u64 parent
)
125 if (bytenr
< orig
->bytenr
)
127 if (bytenr
> orig
->bytenr
)
130 if (parent
< orig
->parent
)
132 if (parent
> orig
->parent
)
135 if (root
< orig
->root
)
137 if (root
> orig
->root
)
144 * insert a new ref into the tree. returns the existing ref entry
145 * if one is already there.
147 static struct ref
*insert_ref(struct ref
*ref
)
150 struct rb_node
**p
= &by_bytenr
.rb_node
;
151 struct rb_node
*parent
= NULL
;
156 curr
= rb_entry(parent
, struct ref
, bytenr_node
);
158 ret
= compare_ref(curr
, ref
->bytenr
, ref
->root
, ref
->parent
);
167 rb_link_node(&ref
->bytenr_node
, parent
, p
);
168 rb_insert_color(&ref
->bytenr_node
, &by_bytenr
);
173 * Partial search, returns the first ref with matching bytenr. Caller
174 * can walk forward from there.
176 * Leftmost refs will be full refs - this is used to our advantage
177 * when resolving roots.
179 static struct ref
*find_ref_bytenr(u64 bytenr
)
181 struct rb_node
*n
= by_bytenr
.rb_node
;
185 ref
= rb_entry(n
, struct ref
, bytenr_node
);
187 if (bytenr
< ref
->bytenr
)
189 else if (bytenr
> ref
->bytenr
)
192 /* Walk to the left to find the first item */
193 struct rb_node
*node_left
= rb_prev(&ref
->bytenr_node
);
194 struct ref
*ref_left
;
197 ref_left
= rb_entry(node_left
, struct ref
,
199 if (ref_left
->bytenr
!= ref
->bytenr
)
202 node_left
= rb_prev(node_left
);
210 static struct ref
*find_ref(u64 bytenr
, u64 root
, u64 parent
)
212 struct rb_node
*n
= by_bytenr
.rb_node
;
217 ref
= rb_entry(n
, struct ref
, bytenr_node
);
219 ret
= compare_ref(ref
, bytenr
, root
, parent
);
230 static struct ref
*alloc_ref(u64 bytenr
, u64 root
, u64 parent
, u64 num_bytes
)
232 struct ref
*ref
= find_ref(bytenr
, root
, parent
);
234 BUG_ON(parent
&& root
);
237 ref
= calloc(1, sizeof(*ref
));
239 ref
->bytenr
= bytenr
;
241 ref
->parent
= parent
;
242 ref
->num_bytes
= num_bytes
;
250 static void free_ref_node(struct rb_node
*node
)
252 struct ref
*ref
= rb_entry(node
, struct ref
, bytenr_node
);
256 FREE_RB_BASED_TREE(ref
, free_ref_node
);
259 * Resolves all the possible roots for the ref at parent.
261 static void find_parent_roots(struct ulist
*roots
, u64 parent
)
264 struct rb_node
*node
;
267 * Search the rbtree for the first ref with bytenr == parent.
268 * Walk forward so long as bytenr == parent, adding resolved root ids.
269 * For each unresolved root, we recurse
271 ref
= find_ref_bytenr(parent
);
272 node
= &ref
->bytenr_node
;
274 BUG_ON(ref
->bytenr
!= parent
);
278 * Random sanity check, are we actually getting the
281 struct rb_node
*prev_node
= rb_prev(&ref
->bytenr_node
);
284 prev
= rb_entry(prev_node
, struct ref
, bytenr_node
);
285 BUG_ON(prev
->bytenr
== parent
);
291 ulist_add(roots
, ref
->root
, 0, 0);
293 find_parent_roots(roots
, ref
->parent
);
295 node
= rb_next(node
);
297 ref
= rb_entry(node
, struct ref
, bytenr_node
);
298 } while (node
&& ref
->bytenr
== parent
);
301 static void print_subvol_info(u64 subvolid
, u64 bytenr
, u64 num_bytes
,
302 struct ulist
*roots
);
304 * Account each ref. Walk the refs, for each set of refs in a
307 * - add the roots for direct refs to the ref roots ulist
309 * - resolve all possible roots for shared refs, insert each
310 * of those into ref_roots ulist (this is a recursive process)
312 * - Walk ref_roots ulist, adding extent bytes to each qgroup count that
313 * cooresponds to a found root.
315 static void account_all_refs(int do_qgroups
, u64 search_subvol
)
319 struct rb_node
*node
;
320 u64 bytenr
, num_bytes
;
321 struct ulist
*roots
= ulist_alloc(0);
322 struct ulist_iterator uiter
;
323 struct ulist_node
*unode
;
325 node
= rb_first(&by_bytenr
);
329 ref
= rb_entry(node
, struct ref
, bytenr_node
);
331 * Walk forward through the list of refs for this
332 * bytenr, adding roots to our ulist. If it's a full
333 * ref, then we have the easy case. Otherwise we need
334 * to search for roots.
336 bytenr
= ref
->bytenr
;
337 num_bytes
= ref
->num_bytes
;
339 BUG_ON(ref
->bytenr
!= bytenr
);
340 BUG_ON(ref
->num_bytes
!= num_bytes
);
342 ulist_add(roots
, ref
->root
, 0, 0);
344 find_parent_roots(roots
, ref
->parent
);
347 * When we leave this inner loop, node is set
348 * to next in our tree and will be turned into
349 * a ref object up top
351 node
= rb_next(node
);
353 ref
= rb_entry(node
, struct ref
, bytenr_node
);
354 } while (node
&& ref
->bytenr
== bytenr
);
357 * Now that we have all roots, we can properly account
358 * this extent against the corresponding qgroups.
360 if (roots
->nnodes
== 1)
366 print_subvol_info(search_subvol
, bytenr
, num_bytes
,
369 ULIST_ITER_INIT(&uiter
);
370 while ((unode
= ulist_next(roots
, &uiter
))) {
371 BUG_ON(unode
->val
== 0ULL);
372 /* We only want to account fs trees */
373 if (is_fstree(unode
->val
) && do_qgroups
)
374 add_bytes(unode
->val
, num_bytes
, exclusive
);
381 static u64
resolve_one_root(u64 bytenr
)
383 struct ref
*ref
= find_ref_bytenr(bytenr
);
389 return resolve_one_root(ref
->parent
);
392 static inline struct tree_block
*unode_tree_block(struct ulist_node
*unode
)
394 return u64_to_ptr(unode
->aux
);
396 static inline u64
unode_bytenr(struct ulist_node
*unode
)
401 static int alloc_tree_block(u64 bytenr
, u64 num_bytes
, int level
)
403 struct tree_block
*block
= calloc(1, sizeof(*block
));
406 block
->num_bytes
= num_bytes
;
407 block
->level
= level
;
408 if (ulist_add(tree_blocks
, bytenr
, ptr_to_u64(block
), 0) >= 0)
415 static void free_tree_blocks(void)
417 struct ulist_iterator uiter
;
418 struct ulist_node
*unode
;
423 ULIST_ITER_INIT(&uiter
);
424 while ((unode
= ulist_next(tree_blocks
, &uiter
)))
425 free(unode_tree_block(unode
));
426 ulist_free(tree_blocks
);
430 #ifdef QGROUP_VERIFY_DEBUG
431 static void print_tree_block(u64 bytenr
, struct tree_block
*block
)
434 struct rb_node
*node
;
436 printf("tree block: %llu\t\tlevel: %d\n", (unsigned long long)bytenr
,
439 ref
= find_ref_bytenr(bytenr
);
440 node
= &ref
->bytenr_node
;
443 node
= rb_next(node
);
445 ref
= rb_entry(node
, struct ref
, bytenr_node
);
446 } while (node
&& ref
->bytenr
== bytenr
);
451 static void print_all_tree_blocks(void)
453 struct ulist_iterator uiter
;
454 struct ulist_node
*unode
;
459 printf("Listing all found interior tree nodes:\n");
461 ULIST_ITER_INIT(&uiter
);
462 while ((unode
= ulist_next(tree_blocks
, &uiter
)))
463 print_tree_block(unode_bytenr(unode
), unode_tree_block(unode
));
467 static int add_refs_for_leaf_items(struct extent_buffer
*eb
, u64 ref_parent
)
471 u64 bytenr
, num_bytes
;
472 struct btrfs_key key
;
473 struct btrfs_disk_key disk_key
;
474 struct btrfs_file_extent_item
*fi
;
476 nr
= btrfs_header_nritems(eb
);
477 for (i
= 0; i
< nr
; i
++) {
478 btrfs_item_key(eb
, &disk_key
, i
);
479 btrfs_disk_key_to_cpu(&key
, &disk_key
);
481 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
484 fi
= btrfs_item_ptr(eb
, i
, struct btrfs_file_extent_item
);
485 /* filter out: inline, disk_bytenr == 0, compressed?
486 * not if we can avoid it */
487 extent_type
= btrfs_file_extent_type(eb
, fi
);
489 if (extent_type
== BTRFS_FILE_EXTENT_INLINE
)
492 bytenr
= btrfs_file_extent_disk_bytenr(eb
, fi
);
496 num_bytes
= btrfs_file_extent_disk_num_bytes(eb
, fi
);
497 if (alloc_ref(bytenr
, 0, ref_parent
, num_bytes
) == NULL
)
504 static int travel_tree(struct btrfs_fs_info
*info
, struct btrfs_root
*root
,
505 u64 bytenr
, u64 num_bytes
, u64 ref_parent
)
508 struct extent_buffer
*eb
;
512 // printf("travel_tree: bytenr: %llu\tnum_bytes: %llu\tref_parent: %llu\n",
513 // bytenr, num_bytes, ref_parent);
515 eb
= read_tree_block(root
, bytenr
, num_bytes
, 0);
516 if (!extent_buffer_uptodate(eb
))
520 /* Don't add a ref for our starting tree block to itself */
521 if (bytenr
!= ref_parent
) {
522 if (alloc_ref(bytenr
, 0, ref_parent
, num_bytes
) == NULL
)
526 if (btrfs_is_leaf(eb
)) {
527 ret
= add_refs_for_leaf_items(eb
, ref_parent
);
532 * Interior nodes are tuples of (key, bytenr) where key is the
533 * leftmost key in the tree block pointed to by bytenr. We
534 * don't have to care about key here, just follow the bytenr
537 nr
= btrfs_header_nritems(eb
);
538 for (i
= 0; i
< nr
; i
++) {
539 new_bytenr
= btrfs_node_blockptr(eb
, i
);
540 new_num_bytes
= btrfs_level_size(root
,
541 btrfs_header_level(eb
) - 1);
543 ret
= travel_tree(info
, root
, new_bytenr
, new_num_bytes
,
548 free_extent_buffer(eb
);
552 static int add_refs_for_implied(struct btrfs_fs_info
*info
, u64 bytenr
,
553 struct tree_block
*block
)
556 u64 root_id
= resolve_one_root(bytenr
);
557 struct btrfs_root
*root
;
558 struct btrfs_key key
;
560 key
.objectid
= root_id
;
561 key
.type
= BTRFS_ROOT_ITEM_KEY
;
562 key
.offset
= (u64
)-1;
565 * XXX: Don't free the root object as we don't know whether it
566 * came off our fs_info struct or not.
568 root
= btrfs_read_fs_root(info
, &key
);
569 if (!root
|| IS_ERR(root
))
572 ret
= travel_tree(info
, root
, bytenr
, block
->num_bytes
, bytenr
);
580 * Place shared refs in the ref tree for each child of an interior tree node.
582 static int map_implied_refs(struct btrfs_fs_info
*info
)
585 struct ulist_iterator uiter
;
586 struct ulist_node
*unode
;
588 ULIST_ITER_INIT(&uiter
);
589 while ((unode
= ulist_next(tree_blocks
, &uiter
))) {
590 ret
= add_refs_for_implied(info
, unode_bytenr(unode
),
591 unode_tree_block(unode
));
600 * insert a new root into the tree. returns the existing root entry
601 * if one is already there. qgroupid is used
604 static int insert_count(struct qgroup_count
*qc
)
606 struct rb_node
**p
= &counts
.root
.rb_node
;
607 struct rb_node
*parent
= NULL
;
608 struct qgroup_count
*curr
;
612 curr
= rb_entry(parent
, struct qgroup_count
, rb_node
);
614 if (qc
->qgroupid
< curr
->qgroupid
)
616 else if (qc
->qgroupid
> curr
->qgroupid
)
622 rb_link_node(&qc
->rb_node
, parent
, p
);
623 rb_insert_color(&qc
->rb_node
, &counts
.root
);
627 static struct qgroup_count
*find_count(u64 qgroupid
)
629 struct rb_node
*n
= counts
.root
.rb_node
;
630 struct qgroup_count
*count
;
633 count
= rb_entry(n
, struct qgroup_count
, rb_node
);
635 if (qgroupid
< count
->qgroupid
)
637 else if (qgroupid
> count
->qgroupid
)
645 static struct qgroup_count
*alloc_count(struct btrfs_disk_key
*key
,
646 struct extent_buffer
*leaf
,
647 struct btrfs_qgroup_info_item
*disk
)
649 struct qgroup_count
*c
= calloc(1, sizeof(*c
));
650 struct btrfs_qgroup_info_item
*item
;
653 c
->qgroupid
= btrfs_disk_key_offset(key
);
657 item
->generation
= btrfs_qgroup_info_generation(leaf
, disk
);
658 item
->referenced
= btrfs_qgroup_info_referenced(leaf
, disk
);
659 item
->referenced_compressed
=
660 btrfs_qgroup_info_referenced_compressed(leaf
, disk
);
661 item
->exclusive
= btrfs_qgroup_info_exclusive(leaf
, disk
);
662 item
->exclusive_compressed
=
663 btrfs_qgroup_info_exclusive_compressed(leaf
, disk
);
665 if (insert_count(c
)) {
673 static void add_bytes(u64 root_objectid
, u64 num_bytes
, int exclusive
)
675 struct qgroup_count
*count
= find_count(root_objectid
);
676 struct btrfs_qgroup_info_item
*qg
;
678 BUG_ON(num_bytes
< 4096); /* Random sanity check. */
685 qg
->referenced
+= num_bytes
;
687 * count of compressed bytes is unimplemented, so we do the
690 qg
->referenced_compressed
+= num_bytes
;
693 qg
->exclusive
+= num_bytes
;
694 qg
->exclusive_compressed
+= num_bytes
;
698 static int load_quota_info(struct btrfs_fs_info
*info
)
701 struct btrfs_root
*root
= info
->quota_root
;
702 struct btrfs_root
*tmproot
;
703 struct btrfs_path path
;
704 struct btrfs_key key
;
705 struct btrfs_key root_key
;
706 struct btrfs_disk_key disk_key
;
707 struct extent_buffer
*leaf
;
708 struct btrfs_qgroup_info_item
*item
;
709 struct qgroup_count
*count
;
712 btrfs_init_path(&path
);
718 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
720 fprintf(stderr
, "ERROR: Couldn't search slot: %d\n", ret
);
725 leaf
= path
.nodes
[0];
727 nr
= btrfs_header_nritems(leaf
);
728 for(i
= 0; i
< nr
; i
++) {
729 btrfs_item_key(leaf
, &disk_key
, i
);
730 btrfs_disk_key_to_cpu(&key
, &disk_key
);
732 if (key
.type
== BTRFS_QGROUP_RELATION_KEY
)
733 printf("Ignoring qgroup relation key %llu\n",
737 * Ignore: BTRFS_QGROUP_STATUS_KEY,
738 * BTRFS_QGROUP_LIMIT_KEY, BTRFS_QGROUP_RELATION_KEY
740 if (key
.type
!= BTRFS_QGROUP_INFO_KEY
)
743 item
= btrfs_item_ptr(leaf
, i
,
744 struct btrfs_qgroup_info_item
);
746 count
= alloc_count(&disk_key
, leaf
, item
);
749 fprintf(stderr
, "ERROR: out of memory\n");
753 root_key
.objectid
= key
.offset
;
754 root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
755 root_key
.offset
= (u64
)-1;
756 tmproot
= btrfs_read_fs_root_no_cache(info
, &root_key
);
757 if (tmproot
&& !IS_ERR(tmproot
)) {
758 count
->subvol_exists
= 1;
763 ret
= btrfs_next_leaf(root
, &path
);
769 btrfs_release_path(&path
);
774 static int add_inline_refs(struct btrfs_fs_info
*info
,
775 struct extent_buffer
*ei_leaf
, int slot
,
776 u64 bytenr
, u64 num_bytes
, int meta_item
)
778 struct btrfs_extent_item
*ei
;
779 struct btrfs_extent_inline_ref
*iref
;
780 struct btrfs_extent_data_ref
*dref
;
781 u64 flags
, root_obj
, offset
, parent
;
782 u32 item_size
= btrfs_item_size_nr(ei_leaf
, slot
);
787 ei
= btrfs_item_ptr(ei_leaf
, slot
, struct btrfs_extent_item
);
788 flags
= btrfs_extent_flags(ei_leaf
, ei
);
790 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !meta_item
) {
791 struct btrfs_tree_block_info
*tbinfo
;
792 tbinfo
= (struct btrfs_tree_block_info
*)(ei
+ 1);
793 iref
= (struct btrfs_extent_inline_ref
*)(tbinfo
+ 1);
795 iref
= (struct btrfs_extent_inline_ref
*)(ei
+ 1);
798 ptr
= (unsigned long)iref
;
799 end
= (unsigned long)ei
+ item_size
;
801 iref
= (struct btrfs_extent_inline_ref
*)ptr
;
803 parent
= root_obj
= 0;
804 offset
= btrfs_extent_inline_ref_offset(ei_leaf
, iref
);
805 type
= btrfs_extent_inline_ref_type(ei_leaf
, iref
);
807 case BTRFS_TREE_BLOCK_REF_KEY
:
810 case BTRFS_EXTENT_DATA_REF_KEY
:
811 dref
= (struct btrfs_extent_data_ref
*)(&iref
->offset
);
812 root_obj
= btrfs_extent_data_ref_root(ei_leaf
, dref
);
814 case BTRFS_SHARED_DATA_REF_KEY
:
815 case BTRFS_SHARED_BLOCK_REF_KEY
:
822 if (alloc_ref(bytenr
, root_obj
, parent
, num_bytes
) == NULL
)
825 ptr
+= btrfs_extent_inline_ref_size(type
);
831 static int add_keyed_ref(struct btrfs_fs_info
*info
,
832 struct btrfs_key
*key
,
833 struct extent_buffer
*leaf
, int slot
,
834 u64 bytenr
, u64 num_bytes
)
836 u64 root_obj
= 0, parent
= 0;
837 struct btrfs_extent_data_ref
*dref
;
840 case BTRFS_TREE_BLOCK_REF_KEY
:
841 root_obj
= key
->offset
;
843 case BTRFS_EXTENT_DATA_REF_KEY
:
844 dref
= btrfs_item_ptr(leaf
, slot
, struct btrfs_extent_data_ref
);
845 root_obj
= btrfs_extent_data_ref_root(leaf
, dref
);
847 case BTRFS_SHARED_DATA_REF_KEY
:
848 case BTRFS_SHARED_BLOCK_REF_KEY
:
849 parent
= key
->offset
;
855 if (alloc_ref(bytenr
, root_obj
, parent
, num_bytes
) == NULL
)
862 * return value of 0 indicates leaf or not meta data. The code that
863 * calls this does not need to make a distinction between the two as
864 * it is only concerned with intermediate blocks which will always
867 static int get_tree_block_level(struct btrfs_key
*key
,
868 struct extent_buffer
*ei_leaf
,
872 int meta_key
= key
->type
== BTRFS_METADATA_ITEM_KEY
;
874 struct btrfs_extent_item
*ei
;
876 ei
= btrfs_item_ptr(ei_leaf
, slot
, struct btrfs_extent_item
);
877 flags
= btrfs_extent_flags(ei_leaf
, ei
);
879 if (flags
& BTRFS_EXTENT_FLAG_TREE_BLOCK
&& !meta_key
) {
880 struct btrfs_tree_block_info
*tbinfo
;
881 tbinfo
= (struct btrfs_tree_block_info
*)(ei
+ 1);
882 level
= btrfs_tree_block_level(ei_leaf
, tbinfo
);
883 } else if (meta_key
) {
884 /* skinny metadata */
885 level
= (int)key
->offset
;
891 * Walk the extent tree, allocating a ref item for every ref and
892 * storing it in the bytenr tree.
894 static int scan_extents(struct btrfs_fs_info
*info
,
897 int ret
, i
, nr
, level
;
898 struct btrfs_root
*root
= info
->extent_root
;
899 struct btrfs_key key
;
900 struct btrfs_path path
;
901 struct btrfs_disk_key disk_key
;
902 struct extent_buffer
*leaf
;
903 u64 bytenr
= 0, num_bytes
= 0;
905 btrfs_init_path(&path
);
907 key
.objectid
= start
;
911 ret
= btrfs_search_slot(NULL
, root
, &key
, &path
, 0, 0);
913 fprintf(stderr
, "ERROR: Couldn't search slot: %d\n", ret
);
919 leaf
= path
.nodes
[0];
921 nr
= btrfs_header_nritems(leaf
);
922 for(i
= 0; i
< nr
; i
++) {
923 btrfs_item_key(leaf
, &disk_key
, i
);
924 btrfs_disk_key_to_cpu(&key
, &disk_key
);
926 if (key
.objectid
< start
)
929 if (key
.objectid
> end
)
932 if (key
.type
== BTRFS_EXTENT_ITEM_KEY
||
933 key
.type
== BTRFS_METADATA_ITEM_KEY
) {
936 tot_extents_scanned
++;
938 bytenr
= key
.objectid
;
939 num_bytes
= key
.offset
;
940 if (key
.type
== BTRFS_METADATA_ITEM_KEY
) {
941 num_bytes
= info
->extent_root
->leafsize
;
945 ret
= add_inline_refs(info
, leaf
, i
, bytenr
,
950 level
= get_tree_block_level(&key
, leaf
, i
);
952 if (alloc_tree_block(bytenr
, num_bytes
,
960 if (key
.type
> BTRFS_SHARED_DATA_REF_KEY
)
962 if (key
.type
< BTRFS_TREE_BLOCK_REF_KEY
)
966 * Keyed refs should come after their extent
967 * item in the tree. As a result, the value of
968 * bytenr and num_bytes should be unchanged
969 * from the above block that catches the
970 * original extent item.
972 BUG_ON(key
.objectid
!= bytenr
);
974 ret
= add_keyed_ref(info
, &key
, leaf
, i
, bytenr
,
980 ret
= btrfs_next_leaf(root
, &path
);
984 "ERROR: Next leaf failed: %d\n", ret
);
993 btrfs_release_path(&path
);
998 static void print_fields(u64 bytes
, u64 bytes_compressed
, char *prefix
,
1001 printf("%s\t\t%s %llu %s compressed %llu\n",
1002 prefix
, type
, (unsigned long long)bytes
, type
,
1003 (unsigned long long)bytes_compressed
);
1006 static void print_fields_signed(long long bytes
,
1007 long long bytes_compressed
,
1008 char *prefix
, char *type
)
1010 printf("%s\t\t%s %lld %s compressed %lld\n",
1011 prefix
, type
, bytes
, type
, bytes_compressed
);
1014 static void print_qgroup_difference(struct qgroup_count
*count
, int verbose
)
1017 struct btrfs_qgroup_info_item
*info
= &count
->info
;
1018 struct btrfs_qgroup_info_item
*disk
= &count
->diskinfo
;
1019 long long excl_diff
= info
->exclusive
- disk
->exclusive
;
1020 long long ref_diff
= info
->referenced
- disk
->referenced
;
1022 is_different
= excl_diff
|| ref_diff
;
1024 if (verbose
|| (is_different
&& count
->subvol_exists
)) {
1025 printf("Counts for qgroup id: %llu %s\n",
1026 (unsigned long long)count
->qgroupid
,
1027 is_different
? "are different" : "");
1029 print_fields(info
->referenced
, info
->referenced_compressed
,
1030 "our:", "referenced");
1031 print_fields(disk
->referenced
, disk
->referenced_compressed
,
1032 "disk:", "referenced");
1034 print_fields_signed(ref_diff
, ref_diff
,
1035 "diff:", "referenced");
1036 print_fields(info
->exclusive
, info
->exclusive_compressed
,
1037 "our:", "exclusive");
1038 print_fields(disk
->exclusive
, disk
->exclusive_compressed
,
1039 "disk:", "exclusive");
1041 print_fields_signed(excl_diff
, excl_diff
,
1042 "diff:", "exclusive");
1046 void print_qgroup_report(int all
)
1048 struct rb_node
*node
;
1049 struct qgroup_count
*c
;
1051 node
= rb_first(&counts
.root
);
1053 c
= rb_entry(node
, struct qgroup_count
, rb_node
);
1054 print_qgroup_difference(c
, all
);
1055 node
= rb_next(node
);
1059 int qgroup_verify_all(struct btrfs_fs_info
*info
)
1063 if (!info
->quota_enabled
)
1066 tree_blocks
= ulist_alloc(0);
1069 "ERROR: Out of memory while allocating ulist.\n");
1073 ret
= load_quota_info(info
);
1075 fprintf(stderr
, "ERROR: Loading qgroups from disk: %d\n", ret
);
1080 * Put all extent refs into our rbtree
1082 ret
= scan_extents(info
, 0, ~0ULL);
1084 fprintf(stderr
, "ERROR: while scanning extent tree: %d\n", ret
);
1088 ret
= map_implied_refs(info
);
1090 fprintf(stderr
, "ERROR: while mapping refs: %d\n", ret
);
1094 account_all_refs(1, 0);
1098 * Don't free the qgroup count records as they will be walked
1099 * later via the print function.
1102 free_ref_tree(&by_bytenr
);
1106 static void __print_subvol_info(u64 bytenr
, u64 num_bytes
, struct ulist
*roots
)
1108 int n
= roots
->nnodes
;
1109 struct ulist_iterator uiter
;
1110 struct ulist_node
*unode
;
1112 printf("%llu\t%llu\t%d\t", bytenr
, num_bytes
, n
);
1114 ULIST_ITER_INIT(&uiter
);
1115 while ((unode
= ulist_next(roots
, &uiter
))) {
1116 printf("%llu ", unode
->val
);
1121 static void print_subvol_info(u64 subvolid
, u64 bytenr
, u64 num_bytes
,
1122 struct ulist
*roots
)
1124 struct ulist_iterator uiter
;
1125 struct ulist_node
*unode
;
1127 ULIST_ITER_INIT(&uiter
);
1128 while ((unode
= ulist_next(roots
, &uiter
))) {
1129 BUG_ON(unode
->val
== 0ULL);
1130 if (unode
->val
== subvolid
) {
1131 __print_subvol_info(bytenr
, num_bytes
, roots
);
1139 int print_extent_state(struct btrfs_fs_info
*info
, u64 subvol
)
1143 tree_blocks
= ulist_alloc(0);
1146 "ERROR: Out of memory while allocating ulist.\n");
1151 * Put all extent refs into our rbtree
1153 ret
= scan_extents(info
, 0, ~0ULL);
1155 fprintf(stderr
, "ERROR: while scanning extent tree: %d\n", ret
);
1159 ret
= map_implied_refs(info
);
1161 fprintf(stderr
, "ERROR: while mapping refs: %d\n", ret
);
1165 printf("Offset\t\tLen\tRoot Refs\tRoots\n");
1166 account_all_refs(0, subvol
);
1170 free_ref_tree(&by_bytenr
);