btrfs-progs: fix csum root copy-n-paste error
[btrfs-progs-unstable/devel.git] / qgroup-verify.c
blobc0c61d0bdb5f65d5d2e759122fe1c3d82c6c3275
1 /*
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>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <uuid/uuid.h>
24 #include "kerncompat.h"
25 #include "radix-tree.h"
26 #include "ctree.h"
27 #include "disk-io.h"
28 #include "print-tree.h"
29 #include "utils.h"
30 #include "ulist.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);
40 struct qgroup_count {
41 u64 qgroupid;
42 int subvol_exists;
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;
52 struct counts_tree {
53 struct rb_root root;
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 */
73 struct tree_block {
74 int level;
75 u64 num_bytes;
78 struct ref {
79 u64 bytenr;
80 u64 num_bytes;
81 u64 parent;
82 u64 root;
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;
98 struct ref *ref;
99 struct rb_node *node;
101 node = rb_first(&by_bytenr);
102 while (node) {
103 ref = rb_entry(node, struct ref, bytenr_node);
105 print_ref(ref);
107 count++;
108 node = rb_next(node);
111 printf("%lu extents scanned with %lu refs in total.\n",
112 tot_extents_scanned, count);
114 #endif
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
121 * shared refs.
123 static int compare_ref(struct ref *orig, u64 bytenr, u64 root, u64 parent)
125 if (bytenr < orig->bytenr)
126 return -1;
127 if (bytenr > orig->bytenr)
128 return 1;
130 if (parent < orig->parent)
131 return -1;
132 if (parent > orig->parent)
133 return 1;
135 if (root < orig->root)
136 return -1;
137 if (root > orig->root)
138 return 1;
140 return 0;
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)
149 int ret;
150 struct rb_node **p = &by_bytenr.rb_node;
151 struct rb_node *parent = NULL;
152 struct ref *curr;
154 while (*p) {
155 parent = *p;
156 curr = rb_entry(parent, struct ref, bytenr_node);
158 ret = compare_ref(curr, ref->bytenr, ref->root, ref->parent);
159 if (ret < 0)
160 p = &(*p)->rb_left;
161 else if (ret > 0)
162 p = &(*p)->rb_right;
163 else
164 return curr;
167 rb_link_node(&ref->bytenr_node, parent, p);
168 rb_insert_color(&ref->bytenr_node, &by_bytenr);
169 return ref;
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;
182 struct ref *ref;
184 while (n) {
185 ref = rb_entry(n, struct ref, bytenr_node);
187 if (bytenr < ref->bytenr)
188 n = n->rb_left;
189 else if (bytenr > ref->bytenr)
190 n = n->rb_right;
191 else {
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;
196 while (node_left) {
197 ref_left = rb_entry(node_left, struct ref,
198 bytenr_node);
199 if (ref_left->bytenr != ref->bytenr)
200 break;
201 ref = ref_left;
202 node_left = rb_prev(node_left);
204 return ref;
207 return NULL;
210 static struct ref *find_ref(u64 bytenr, u64 root, u64 parent)
212 struct rb_node *n = by_bytenr.rb_node;
213 struct ref *ref;
214 int ret;
216 while (n) {
217 ref = rb_entry(n, struct ref, bytenr_node);
219 ret = compare_ref(ref, bytenr, root, parent);
220 if (ret < 0)
221 n = n->rb_left;
222 else if (ret > 0)
223 n = n->rb_right;
224 else
225 return ref;
227 return NULL;
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);
236 if (ref == NULL) {
237 ref = calloc(1, sizeof(*ref));
238 if (ref) {
239 ref->bytenr = bytenr;
240 ref->root = root;
241 ref->parent = parent;
242 ref->num_bytes = num_bytes;
244 insert_ref(ref);
247 return ref;
250 static void free_ref_node(struct rb_node *node)
252 struct ref *ref = rb_entry(node, struct ref, bytenr_node);
253 free(ref);
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)
263 struct ref *ref;
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;
273 BUG_ON(ref == NULL);
274 BUG_ON(ref->bytenr != parent);
278 * Random sanity check, are we actually getting the
279 * leftmost node?
281 struct rb_node *prev_node = rb_prev(&ref->bytenr_node);
282 struct ref *prev;
283 if (prev_node) {
284 prev = rb_entry(prev_node, struct ref, bytenr_node);
285 BUG_ON(prev->bytenr == parent);
289 do {
290 if (ref->root)
291 ulist_add(roots, ref->root, 0, 0);
292 else
293 find_parent_roots(roots, ref->parent);
295 node = rb_next(node);
296 if (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
305 * given bytenr:
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)
317 int exclusive;
318 struct ref *ref;
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);
326 while (node) {
327 ulist_reinit(roots);
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;
338 do {
339 BUG_ON(ref->bytenr != bytenr);
340 BUG_ON(ref->num_bytes != num_bytes);
341 if (ref->root)
342 ulist_add(roots, ref->root, 0, 0);
343 else
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);
352 if (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)
361 exclusive = 1;
362 else
363 exclusive = 0;
365 if (search_subvol)
366 print_subvol_info(search_subvol, bytenr, num_bytes,
367 roots);
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);
378 ulist_free(roots);
381 static u64 resolve_one_root(u64 bytenr)
383 struct ref *ref = find_ref_bytenr(bytenr);
385 BUG_ON(ref == NULL);
387 if (ref->root)
388 return ref->root;
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)
398 return unode->val;
401 static int alloc_tree_block(u64 bytenr, u64 num_bytes, int level)
403 struct tree_block *block = calloc(1, sizeof(*block));
405 if (block) {
406 block->num_bytes = num_bytes;
407 block->level = level;
408 if (ulist_add(tree_blocks, bytenr, ptr_to_u64(block), 0) >= 0)
409 return 0;
410 free(block);
412 return -ENOMEM;
415 static void free_tree_blocks(void)
417 struct ulist_iterator uiter;
418 struct ulist_node *unode;
420 if (!tree_blocks)
421 return;
423 ULIST_ITER_INIT(&uiter);
424 while ((unode = ulist_next(tree_blocks, &uiter)))
425 free(unode_tree_block(unode));
426 ulist_free(tree_blocks);
427 tree_blocks = NULL;
430 #ifdef QGROUP_VERIFY_DEBUG
431 static void print_tree_block(u64 bytenr, struct tree_block *block)
433 struct ref *ref;
434 struct rb_node *node;
436 printf("tree block: %llu\t\tlevel: %d\n", (unsigned long long)bytenr,
437 block->level);
439 ref = find_ref_bytenr(bytenr);
440 node = &ref->bytenr_node;
441 do {
442 print_ref(ref);
443 node = rb_next(node);
444 if (node)
445 ref = rb_entry(node, struct ref, bytenr_node);
446 } while (node && ref->bytenr == bytenr);
448 printf("\n");
451 static void print_all_tree_blocks(void)
453 struct ulist_iterator uiter;
454 struct ulist_node *unode;
456 if (!tree_blocks)
457 return;
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));
465 #endif
467 static int add_refs_for_leaf_items(struct extent_buffer *eb, u64 ref_parent)
469 int nr, i;
470 int extent_type;
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)
482 continue;
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)
490 continue;
492 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
493 if (!bytenr)
494 continue;
496 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
497 if (alloc_ref(bytenr, 0, ref_parent, num_bytes) == NULL)
498 return ENOMEM;
501 return 0;
504 static int travel_tree(struct btrfs_fs_info *info, struct btrfs_root *root,
505 u64 bytenr, u64 num_bytes, u64 ref_parent)
507 int ret, nr, i;
508 struct extent_buffer *eb;
509 u64 new_bytenr;
510 u64 new_num_bytes;
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 (!eb)
517 return -EIO;
519 ret = 0;
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)
523 return ENOMEM;
526 if (btrfs_is_leaf(eb)) {
527 ret = add_refs_for_leaf_items(eb, ref_parent);
528 goto out;
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
535 * pointer.
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,
544 ref_parent);
547 out:
548 free_extent_buffer(eb);
549 return ret;
552 static int add_refs_for_implied(struct btrfs_fs_info *info, u64 bytenr,
553 struct tree_block *block)
555 int ret;
556 u64 root_bytenr = resolve_one_root(bytenr);
557 struct btrfs_root *root;
558 struct btrfs_key key;
560 key.objectid = root_bytenr;
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))
570 return ENOENT;
572 ret = travel_tree(info, root, bytenr, block->num_bytes, bytenr);
573 if (ret)
574 return ret;
576 return 0;
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)
584 int ret = 0;
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));
592 if (ret)
593 goto out;
595 out:
596 return ret;
600 * insert a new root into the tree. returns the existing root entry
601 * if one is already there. qgroupid is used
602 * as the key
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;
610 while (*p) {
611 parent = *p;
612 curr = rb_entry(parent, struct qgroup_count, rb_node);
614 if (qc->qgroupid < curr->qgroupid)
615 p = &(*p)->rb_left;
616 else if (qc->qgroupid > curr->qgroupid)
617 p = &(*p)->rb_right;
618 else
619 return EEXIST;
621 counts.num_groups++;
622 rb_link_node(&qc->rb_node, parent, p);
623 rb_insert_color(&qc->rb_node, &counts.root);
624 return 0;
627 static struct qgroup_count *find_count(u64 qgroupid)
629 struct rb_node *n = counts.root.rb_node;
630 struct qgroup_count *count;
632 while (n) {
633 count = rb_entry(n, struct qgroup_count, rb_node);
635 if (qgroupid < count->qgroupid)
636 n = n->rb_left;
637 else if (qgroupid > count->qgroupid)
638 n = n->rb_right;
639 else
640 return count;
642 return NULL;
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;
652 if (c) {
653 c->qgroupid = btrfs_disk_key_offset(key);
654 c->key = *key;
656 item = &c->diskinfo;
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)) {
666 free(c);
667 c = NULL;
670 return 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. */
680 if (!count)
681 return;
683 qg = &count->info;
685 qg->referenced += num_bytes;
687 * count of compressed bytes is unimplemented, so we do the
688 * same as kernel.
690 qg->referenced_compressed += num_bytes;
692 if (exclusive) {
693 qg->exclusive += num_bytes;
694 qg->exclusive_compressed += num_bytes;
698 static int load_quota_info(struct btrfs_fs_info *info)
700 int ret;
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;
710 int i, nr;
712 btrfs_init_path(&path);
714 key.offset = 0;
715 key.objectid = 0;
716 key.type = 0;
718 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
719 if (ret < 0) {
720 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
721 goto out;
724 while (1) {
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",
734 key.objectid);
737 * Ignore: BTRFS_QGROUP_STATUS_KEY,
738 * BTRFS_QGROUP_LIMIT_KEY, BTRFS_QGROUP_RELATION_KEY
740 if (key.type != BTRFS_QGROUP_INFO_KEY)
741 continue;
743 item = btrfs_item_ptr(leaf, i,
744 struct btrfs_qgroup_info_item);
746 count = alloc_count(&disk_key, leaf, item);
747 if (!count) {
748 ret = ENOMEM;
749 fprintf(stderr, "ERROR: out of memory\n");
750 goto out;
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;
759 free(tmproot);
763 ret = btrfs_next_leaf(root, &path);
764 if (ret != 0)
765 break;
768 ret = 0;
769 btrfs_release_path(&path);
770 out:
771 return ret;
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);
783 int type;
784 unsigned long end;
785 unsigned long ptr;
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);
794 } else {
795 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
798 ptr = (unsigned long)iref;
799 end = (unsigned long)ei + item_size;
800 while (ptr < end) {
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);
806 switch (type) {
807 case BTRFS_TREE_BLOCK_REF_KEY:
808 root_obj = offset;
809 break;
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);
813 break;
814 case BTRFS_SHARED_DATA_REF_KEY:
815 case BTRFS_SHARED_BLOCK_REF_KEY:
816 parent = offset;
817 break;
818 default:
819 return 1;
822 if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
823 return ENOMEM;
825 ptr += btrfs_extent_inline_ref_size(type);
828 return 0;
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;
839 switch(key->type) {
840 case BTRFS_TREE_BLOCK_REF_KEY:
841 root_obj = key->offset;
842 break;
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);
846 break;
847 case BTRFS_SHARED_DATA_REF_KEY:
848 case BTRFS_SHARED_BLOCK_REF_KEY:
849 parent = key->offset;
850 break;
851 default:
852 return 1;
855 if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
856 return ENOMEM;
858 return 0;
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
865 * have level > 0.
867 static int get_tree_block_level(struct btrfs_key *key,
868 struct extent_buffer *ei_leaf,
869 int slot)
871 int level = 0;
872 int meta_key = key->type == BTRFS_METADATA_ITEM_KEY;
873 u64 flags;
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;
887 return level;
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,
895 u64 start, u64 end)
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;
908 key.type = 0;
909 key.offset = 0;
911 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
912 if (ret < 0) {
913 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
914 goto out;
916 path.reada = 1;
918 while (1) {
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)
927 continue;
929 if (key.objectid > end)
930 goto done;
932 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
933 key.type == BTRFS_METADATA_ITEM_KEY) {
934 int meta = 0;
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;
942 meta = 1;
945 ret = add_inline_refs(info, leaf, i, bytenr,
946 num_bytes, meta);
947 if (ret)
948 goto out;
950 level = get_tree_block_level(&key, leaf, i);
951 if (level) {
952 if (alloc_tree_block(bytenr, num_bytes,
953 level))
954 return ENOMEM;
957 continue;
960 if (key.type > BTRFS_SHARED_DATA_REF_KEY)
961 continue;
962 if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
963 continue;
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,
975 num_bytes);
976 if (ret)
977 goto out;
980 ret = btrfs_next_leaf(root, &path);
981 if (ret != 0) {
982 if (ret < 0) {
983 fprintf(stderr,
984 "ERROR: Next leaf failed: %d\n", ret);
985 goto out;
987 break;
990 done:
991 ret = 0;
992 out:
993 btrfs_release_path(&path);
995 return ret;
998 static void print_fields(u64 bytes, u64 bytes_compressed, char *prefix,
999 char *type)
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)
1016 int is_different;
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");
1033 if (ref_diff)
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");
1040 if (excl_diff)
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);
1052 while (node) {
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)
1061 int ret;
1063 if (!info->quota_enabled)
1064 return 0;
1066 tree_blocks = ulist_alloc(0);
1067 if (!tree_blocks) {
1068 fprintf(stderr,
1069 "ERROR: Out of memory while allocating ulist.\n");
1070 return ENOMEM;
1073 ret = load_quota_info(info);
1074 if (ret) {
1075 fprintf(stderr, "ERROR: Loading qgroups from disk: %d\n", ret);
1076 goto out;
1080 * Put all extent refs into our rbtree
1082 ret = scan_extents(info, 0, ~0ULL);
1083 if (ret) {
1084 fprintf(stderr, "ERROR: while scanning extent tree: %d\n", ret);
1085 goto out;
1088 ret = map_implied_refs(info);
1089 if (ret) {
1090 fprintf(stderr, "ERROR: while mapping refs: %d\n", ret);
1091 goto out;
1094 account_all_refs(1, 0);
1096 out:
1098 * Don't free the qgroup count records as they will be walked
1099 * later via the print function.
1101 free_tree_blocks();
1102 free_ref_tree(&by_bytenr);
1103 return ret;
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);
1118 printf("\n");
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);
1132 return;
1139 int print_extent_state(struct btrfs_fs_info *info, u64 subvol)
1141 int ret;
1143 tree_blocks = ulist_alloc(0);
1144 if (!tree_blocks) {
1145 fprintf(stderr,
1146 "ERROR: Out of memory while allocating ulist.\n");
1147 return ENOMEM;
1151 * Put all extent refs into our rbtree
1153 ret = scan_extents(info, 0, ~0ULL);
1154 if (ret) {
1155 fprintf(stderr, "ERROR: while scanning extent tree: %d\n", ret);
1156 goto out;
1159 ret = map_implied_refs(info);
1160 if (ret) {
1161 fprintf(stderr, "ERROR: while mapping refs: %d\n", ret);
1162 goto out;
1165 printf("Offset\t\tLen\tRoot Refs\tRoots\n");
1166 account_all_refs(0, subvol);
1168 out:
1169 free_tree_blocks();
1170 free_ref_tree(&by_bytenr);
1171 return ret;