btrfs-progs: Add some simple end-to-end tests for btrfs-convert
[platform/upstream/btrfs-progs.git] / qgroup-verify.c
1 /*
2  * Copyright (C) 2014 SUSE.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  *
18  * Authors: Mark Fasheh <mfasheh@suse.de>
19  */
20
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
32 #include "qgroup-verify.h"
33
34 /*#define QGROUP_VERIFY_DEBUG*/
35 static unsigned long tot_extents_scanned = 0;
36
37 static void add_bytes(u64 root_objectid, u64 num_bytes, int exclusive);
38
39 struct qgroup_count {
40         u64                             qgroupid;
41
42         struct btrfs_disk_key           key;
43         struct btrfs_qgroup_info_item   diskinfo;
44
45         struct btrfs_qgroup_info_item   info;
46
47         struct rb_node                  rb_node;
48 };
49
50 struct counts_tree {
51         struct rb_root          root;
52         unsigned int            num_groups;
53 } counts = { .root = RB_ROOT };
54
55 struct rb_root by_bytenr = RB_ROOT;
56
57 /*
58  * List of interior tree blocks. We walk this list after loading the
59  * extent tree to resolve implied refs. For each interior node we'll
60  * place a shared ref in the ref tree against each child object. This
61  * allows the shared ref resolving code to do the actual work later of
62  * finding roots to account against.
63  *
64  * An implied ref is when a tree block has refs on it that may not
65  * exist in any of it's child nodes. Even though the refs might not
66  * exist further down the tree, the fact that our interior node has a
67  * ref means we need to account anything below it to all it's roots.
68  */
69 struct ulist *tree_blocks = NULL;       /* unode->val = bytenr, ->aux
70                                          * = tree_block pointer */
71 struct tree_block {
72         int                     level;
73         u64                     num_bytes;
74 };
75
76 struct ref {
77         u64                     bytenr;
78         u64                     num_bytes;
79         u64                     parent;
80         u64                     root;
81
82         struct rb_node          bytenr_node;
83 };
84
85 #ifdef QGROUP_VERIFY_DEBUG
86 static void print_ref(struct ref *ref)
87 {
88         printf("bytenr: %llu\t\tnum_bytes: %llu\t\t parent: %llu\t\t"
89                "root: %llu\n", ref->bytenr, ref->num_bytes,
90                ref->parent, ref->root);
91 }
92
93 static void print_all_refs(void)
94 {
95         unsigned long count = 0;
96         struct ref *ref;
97         struct rb_node *node;
98
99         node = rb_first(&by_bytenr);
100         while (node) {
101                 ref = rb_entry(node, struct ref, bytenr_node);
102
103                 print_ref(ref);
104
105                 count++;
106                 node = rb_next(node);
107         }
108
109         printf("%lu extents scanned with %lu refs in total.\n",
110                tot_extents_scanned, count);
111 }
112 #endif
113
114 /*
115  * Store by bytenr in rbtree
116  *
117  * The tree is sorted in ascending order by bytenr, then parent, then
118  * root. Since full refs have a parent == 0, those will come before
119  * shared refs.
120  */
121 static int compare_ref(struct ref *orig, u64 bytenr, u64 root, u64 parent)
122 {
123         if (bytenr < orig->bytenr)
124                 return -1;
125         if (bytenr > orig->bytenr)
126                 return 1;
127
128         if (parent < orig->parent)
129                 return -1;
130         if (parent > orig->parent)
131                 return 1;
132
133         if (root < orig->root)
134                 return -1;
135         if (root > orig->root)
136                 return 1;
137
138         return 0;
139 }
140
141 /*
142  * insert a new ref into the tree.  returns the existing ref entry
143  * if one is already there.
144  */
145 static struct ref *insert_ref(struct ref *ref)
146 {
147         int ret;
148         struct rb_node **p = &by_bytenr.rb_node;
149         struct rb_node *parent = NULL;
150         struct ref *curr;
151
152         while (*p) {
153                 parent = *p;
154                 curr = rb_entry(parent, struct ref, bytenr_node);
155
156                 ret = compare_ref(curr, ref->bytenr, ref->root, ref->parent);
157                 if (ret < 0)
158                         p = &(*p)->rb_left;
159                 else if (ret > 0)
160                         p = &(*p)->rb_right;
161                 else
162                         return curr;
163         }
164
165         rb_link_node(&ref->bytenr_node, parent, p);
166         rb_insert_color(&ref->bytenr_node, &by_bytenr);
167         return ref;
168 }
169
170 /*
171  * Partial search, returns the first ref with matching bytenr. Caller
172  * can walk forward from there.
173  *
174  * Leftmost refs will be full refs - this is used to our advantage
175  * when resolving roots.
176  */
177 static struct ref *find_ref_bytenr(u64 bytenr)
178 {
179         struct rb_node *n = by_bytenr.rb_node;
180         struct ref *ref;
181
182         while (n) {
183                 ref = rb_entry(n, struct ref, bytenr_node);
184
185                 if (bytenr < ref->bytenr)
186                         n = n->rb_left;
187                 else if (bytenr > ref->bytenr)
188                         n = n->rb_right;
189                 else {
190                         /* Walk to the left to find the first item */
191                         struct rb_node *node_left = rb_prev(&ref->bytenr_node);
192                         struct ref *ref_left;
193
194                         while (node_left) {
195                                 ref_left = rb_entry(node_left, struct ref,
196                                                     bytenr_node);
197                                 if (ref_left->bytenr != ref->bytenr)
198                                         break;
199                                 ref = ref_left;
200                                 node_left = rb_prev(node_left);
201                         }
202                         return ref;
203                 }
204         }
205         return NULL;
206 }
207
208 static struct ref *find_ref(u64 bytenr, u64 root, u64 parent)
209 {
210         struct rb_node *n = by_bytenr.rb_node;
211         struct ref *ref;
212         int ret;
213
214         while (n) {
215                 ref = rb_entry(n, struct ref, bytenr_node);
216
217                 ret = compare_ref(ref, bytenr, root, parent);
218                 if (ret < 0)
219                         n = n->rb_left;
220                 else if (ret > 0)
221                         n = n->rb_right;
222                 else
223                         return ref;
224         }
225         return NULL;
226 }
227
228 static struct ref *alloc_ref(u64 bytenr, u64 root, u64 parent, u64 num_bytes)
229 {
230         struct ref *ref = find_ref(bytenr, root, parent);
231
232         BUG_ON(parent && root);
233
234         if (ref == NULL) {
235                 ref = calloc(1, sizeof(*ref));
236                 if (ref) {
237                         ref->bytenr = bytenr;
238                         ref->root = root;
239                         ref->parent = parent;
240                         ref->num_bytes = num_bytes;
241
242                         insert_ref(ref);
243                 }
244         }
245         return ref;
246 }
247
248 static void free_ref_node(struct rb_node *node)
249 {
250         struct ref *ref = rb_entry(node, struct ref, bytenr_node);
251         free(ref);
252 }
253
254 FREE_RB_BASED_TREE(ref, free_ref_node);
255
256 /*
257  * Resolves all the possible roots for the ref at parent.
258  */
259 static void find_parent_roots(struct ulist *roots, u64 parent)
260 {
261         struct ref *ref;
262         struct rb_node *node;
263
264         /*
265          * Search the rbtree for the first ref with bytenr == parent.
266          * Walk forward so long as bytenr == parent, adding resolved root ids.
267          * For each unresolved root, we recurse
268          */
269         ref = find_ref_bytenr(parent);
270         node = &ref->bytenr_node;
271         BUG_ON(ref == NULL);
272         BUG_ON(ref->bytenr != parent);
273
274         {
275                 /*
276                  * Random sanity check, are we actually getting the
277                  * leftmost node?
278                  */
279                 struct rb_node *prev_node = rb_prev(&ref->bytenr_node);
280                 struct ref *prev;
281                 if (prev_node) {
282                         prev = rb_entry(prev_node, struct ref, bytenr_node);
283                         BUG_ON(prev->bytenr == parent);
284                 }
285         }
286
287         do {
288                 if (ref->root)
289                         ulist_add(roots, ref->root, 0, 0);
290                 else
291                         find_parent_roots(roots, ref->parent);
292
293                 node = rb_next(node);
294                 if (node)
295                         ref = rb_entry(node, struct ref, bytenr_node);
296         } while (node && ref->bytenr == parent);
297 }
298
299 /*
300  * Account each ref. Walk the refs, for each set of refs in a
301  * given bytenr:
302  *
303  * - add the roots for direct refs to the ref roots ulist
304  *
305  * - resolve all possible roots for shared refs, insert each
306  *   of those into ref_roots ulist (this is a recursive process)
307  *
308  * - Walk ref_roots ulist, adding extent bytes to each qgroup count that
309  *    cooresponds to a found root.
310  */
311 static void account_all_refs(void)
312 {
313         int exclusive;
314         struct ref *ref;
315         struct rb_node *node;
316         u64 bytenr, num_bytes;
317         struct ulist *roots = ulist_alloc(0);
318         struct ulist_iterator uiter;
319         struct ulist_node *unode;
320
321         node = rb_first(&by_bytenr);
322         while (node) {
323                 ulist_reinit(roots);
324
325                 ref = rb_entry(node, struct ref, bytenr_node);
326                 /*
327                  * Walk forward through the list of refs for this
328                  * bytenr, adding roots to our ulist. If it's a full
329                  * ref, then we have the easy case. Otherwise we need
330                  * to search for roots.
331                  */
332                 bytenr = ref->bytenr;
333                 num_bytes = ref->num_bytes;
334                 do {
335                         BUG_ON(ref->bytenr != bytenr);
336                         BUG_ON(ref->num_bytes != num_bytes);
337                         if (ref->root)
338                                 ulist_add(roots, ref->root, 0, 0);
339                         else
340                                 find_parent_roots(roots, ref->parent);
341
342                         /*
343                          * When we leave this inner loop, node is set
344                          * to next in our tree and will be turned into
345                          * a ref object up top
346                          */
347                         node = rb_next(node);
348                         if (node)
349                                 ref = rb_entry(node, struct ref, bytenr_node);
350                 } while (node && ref->bytenr == bytenr);
351
352                 /*
353                  * Now that we have all roots, we can properly account
354                  * this extent against the corresponding qgroups.
355                  */
356                 if (roots->nnodes == 1)
357                         exclusive = 1;
358                 else
359                         exclusive = 0;
360
361                 ULIST_ITER_INIT(&uiter);
362                 while ((unode = ulist_next(roots, &uiter))) {
363                         BUG_ON(unode->val == 0ULL);
364                         /* We only want to account fs trees */
365                         if (is_fstree(unode->val))
366                                 add_bytes(unode->val, num_bytes, exclusive);
367                 }
368         }
369
370         ulist_free(roots);
371 }
372
373 static u64 resolve_one_root(u64 bytenr)
374 {
375         struct ref *ref = find_ref_bytenr(bytenr);
376
377         BUG_ON(ref == NULL);
378
379         if (ref->root)
380                 return ref->root;
381         return resolve_one_root(ref->parent);
382 }
383
384 static inline struct tree_block *unode_tree_block(struct ulist_node *unode)
385 {
386         return (struct tree_block *)unode->aux;
387 }
388 static inline u64 unode_bytenr(struct ulist_node *unode)
389 {
390         return unode->val;
391 }
392
393 static int alloc_tree_block(u64 bytenr, u64 num_bytes, int level)
394 {
395         struct tree_block *block = calloc(1, sizeof(*block));
396
397         if (block) {
398                 block->num_bytes = num_bytes;
399                 block->level = level;
400                 if (ulist_add(tree_blocks, bytenr, (unsigned long long)block, 0) >= 0)
401                         return 0;
402                 free(block);
403         }
404         return -ENOMEM;
405 }
406
407 static void free_tree_blocks(void)
408 {
409         struct ulist_iterator uiter;
410         struct ulist_node *unode;
411
412         if (!tree_blocks)
413                 return;
414
415         ULIST_ITER_INIT(&uiter);
416         while ((unode = ulist_next(tree_blocks, &uiter)))
417                 free(unode_tree_block(unode));
418         ulist_free(tree_blocks);        
419         tree_blocks = NULL;
420 }
421
422 #ifdef QGROUP_VERIFY_DEBUG
423 static void print_tree_block(u64 bytenr, struct tree_block *block)
424 {
425         struct ref *ref;
426         struct rb_node *node;
427
428         printf("tree block: %llu\t\tlevel: %d\n", (unsigned long long)bytenr,
429                block->level);
430
431         ref = find_ref_bytenr(bytenr);
432         node = &ref->bytenr_node;
433         do {
434                 print_ref(ref);
435                 node = rb_next(node);
436                 if (node)
437                         ref = rb_entry(node, struct ref, bytenr_node);
438         } while (node && ref->bytenr == bytenr);
439
440         printf("\n");
441 }
442
443 static void print_all_tree_blocks(void)
444 {
445         struct ulist_iterator uiter;
446         struct ulist_node *unode;
447
448         if (!tree_blocks)
449                 return;
450
451         printf("Listing all found interior tree nodes:\n");
452
453         ULIST_ITER_INIT(&uiter);
454         while ((unode = ulist_next(tree_blocks, &uiter)))
455                 print_tree_block(unode_bytenr(unode), unode_tree_block(unode));
456 }
457 #endif
458
459 static int add_refs_for_leaf_items(struct extent_buffer *eb, u64 ref_parent)
460 {
461         int nr, i;
462         int extent_type;
463         u64 bytenr, num_bytes;
464         struct btrfs_key key;
465         struct btrfs_disk_key disk_key;
466         struct btrfs_file_extent_item *fi;
467
468         nr = btrfs_header_nritems(eb);
469         for (i = 0; i < nr; i++) {
470                 btrfs_item_key(eb, &disk_key, i);
471                 btrfs_disk_key_to_cpu(&key, &disk_key);
472
473                 if (key.type != BTRFS_EXTENT_DATA_KEY)
474                         continue;
475
476                 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
477                 /* filter out: inline, disk_bytenr == 0, compressed?
478                  * not if we can avoid it */
479                 extent_type = btrfs_file_extent_type(eb, fi);
480
481                 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
482                         continue;
483
484                 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
485                 if (!bytenr)
486                         continue;
487
488                 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
489                 if (alloc_ref(bytenr, 0, ref_parent, num_bytes) == NULL)
490                         return ENOMEM;
491         }
492
493         return 0;
494 }
495
496 static int travel_tree(struct btrfs_fs_info *info, struct btrfs_root *root,
497                        u64 bytenr, u64 num_bytes, u64 ref_parent)
498 {
499         int ret, nr, i;
500         struct extent_buffer *eb;
501         u64 new_bytenr;
502         u64 new_num_bytes;
503
504 //      printf("travel_tree: bytenr: %llu\tnum_bytes: %llu\tref_parent: %llu\n",
505 //             bytenr, num_bytes, ref_parent);
506
507         eb = read_tree_block(root, bytenr, num_bytes, 0);
508         if (!eb)
509                 return -EIO;
510
511         ret = 0;
512         /* Don't add a ref for our starting tree block to itself */
513         if (bytenr != ref_parent) {
514                 if (alloc_ref(bytenr, 0, ref_parent, num_bytes) == NULL)
515                         return ENOMEM;
516         }
517
518         if (btrfs_is_leaf(eb)) {
519                 ret = add_refs_for_leaf_items(eb, ref_parent);
520                 goto out;
521         }
522
523         /*
524          * Interior nodes are tuples of (key, bytenr) where key is the
525          * leftmost key in the tree block pointed to by bytenr. We
526          * don't have to care about key here, just follow the bytenr
527          * pointer.
528          */
529         nr = btrfs_header_nritems(eb);
530         for (i = 0; i < nr; i++) {
531                 new_bytenr = btrfs_node_blockptr(eb, i);
532                 new_num_bytes = btrfs_level_size(root,
533                                                  btrfs_header_level(eb) - 1);
534
535                 ret = travel_tree(info, root, new_bytenr, new_num_bytes,
536                                   ref_parent);
537         }
538
539 out:
540         free_extent_buffer(eb);
541         return ret;
542 }
543
544 static int add_refs_for_implied(struct btrfs_fs_info *info, u64 bytenr,
545                                 struct tree_block *block)
546 {
547         int ret;
548         u64 root_bytenr = resolve_one_root(bytenr);
549         struct btrfs_root *root;
550         struct btrfs_key key;
551
552         key.objectid = root_bytenr;
553         key.type = BTRFS_ROOT_ITEM_KEY;
554         key.offset = (u64)-1;
555
556         /*
557          * XXX: Don't free the root object as we don't know whether it
558          * came off our fs_info struct or not.
559          */
560         root = btrfs_read_fs_root(info, &key);
561         if (!root || IS_ERR(root))
562                 return ENOENT;
563
564         ret = travel_tree(info, root, bytenr, block->num_bytes, bytenr);
565         if (ret)
566                 return ret;
567
568         return 0;
569 }
570
571 /*
572  * Place shared refs in the ref tree for each child of an interior tree node.
573  */
574 static int map_implied_refs(struct btrfs_fs_info *info)
575 {
576         int ret = 0;
577         struct ulist_iterator uiter;
578         struct ulist_node *unode;
579
580         ULIST_ITER_INIT(&uiter);
581         while ((unode = ulist_next(tree_blocks, &uiter))) {
582                 ret = add_refs_for_implied(info, unode_bytenr(unode),
583                                            unode_tree_block(unode));
584                 if (ret)
585                         goto out;
586         }
587 out:
588         return ret;
589 }
590
591 /*
592  * insert a new root into the tree.  returns the existing root entry
593  * if one is already there.  qgroupid is used
594  * as the key
595  */
596 static int insert_count(struct qgroup_count *qc)
597 {
598         struct rb_node **p = &counts.root.rb_node;
599         struct rb_node *parent = NULL;
600         struct qgroup_count *curr;
601
602         while (*p) {
603                 parent = *p;
604                 curr = rb_entry(parent, struct qgroup_count, rb_node);
605
606                 if (qc->qgroupid < curr->qgroupid)
607                         p = &(*p)->rb_left;
608                 else if (qc->qgroupid > curr->qgroupid)
609                         p = &(*p)->rb_right;
610                 else
611                         return EEXIST;
612         }
613         counts.num_groups++;
614         rb_link_node(&qc->rb_node, parent, p);
615         rb_insert_color(&qc->rb_node, &counts.root);
616         return 0;
617 }
618
619 static struct qgroup_count *find_count(u64 qgroupid)
620 {
621         struct rb_node *n = counts.root.rb_node;
622         struct qgroup_count *count;
623
624         while (n) {
625                 count = rb_entry(n, struct qgroup_count, rb_node);
626
627                 if (qgroupid < count->qgroupid)
628                         n = n->rb_left;
629                 else if (qgroupid > count->qgroupid)
630                         n = n->rb_right;
631                 else
632                         return count;
633         }
634         return NULL;
635 }
636
637 static struct qgroup_count *alloc_count(struct btrfs_disk_key *key,
638                                         struct extent_buffer *leaf,
639                                         struct btrfs_qgroup_info_item *disk)
640 {
641         struct qgroup_count *c = calloc(1, sizeof(*c));
642         struct btrfs_qgroup_info_item *item;
643
644         if (c) {
645                 c->qgroupid = btrfs_disk_key_offset(key);
646                 c->key = *key;
647
648                 item = &c->diskinfo;
649                 item->generation = btrfs_qgroup_info_generation(leaf, disk);
650                 item->referenced = btrfs_qgroup_info_referenced(leaf, disk);
651                 item->referenced_compressed =
652                         btrfs_qgroup_info_referenced_compressed(leaf, disk);
653                 item->exclusive = btrfs_qgroup_info_exclusive(leaf, disk);
654                 item->exclusive_compressed =
655                         btrfs_qgroup_info_exclusive_compressed(leaf, disk);
656
657                 if (insert_count(c)) {
658                         free(c);
659                         c = NULL;
660                 }
661         }
662         return c;
663 }
664
665 static void add_bytes(u64 root_objectid, u64 num_bytes, int exclusive)
666 {
667         struct qgroup_count *count = find_count(root_objectid);
668         struct btrfs_qgroup_info_item *qg;
669
670         BUG_ON(num_bytes < 4096); /* Random sanity check. */
671
672         if (!count)
673                 return;
674
675         qg = &count->info;
676
677         qg->referenced += num_bytes;
678         /*
679          * count of compressed bytes is unimplemented, so we do the
680          * same as kernel.
681          */
682         qg->referenced_compressed += num_bytes;
683
684         if (exclusive) {
685                 qg->exclusive += num_bytes;
686                 qg->exclusive_compressed += num_bytes;
687         }
688 }
689
690 static int load_quota_info(struct btrfs_fs_info *info)
691 {
692         int ret;
693         struct btrfs_root *root = info->quota_root;
694         struct btrfs_path path;
695         struct btrfs_key key;
696         struct btrfs_disk_key disk_key;
697         struct extent_buffer *leaf;
698         struct btrfs_qgroup_info_item *item;
699         struct qgroup_count *count;
700         int i, nr;
701
702         btrfs_init_path(&path);
703
704         key.offset = 0;
705         key.objectid = 0;
706         key.type = 0;
707
708         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
709         if (ret < 0) {
710                 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
711                 goto out;
712         }
713
714         while (1) {
715                 leaf = path.nodes[0];
716
717                 nr = btrfs_header_nritems(leaf);
718                 for(i = 0; i < nr; i++) {
719                         btrfs_item_key(leaf, &disk_key, i);
720                         btrfs_disk_key_to_cpu(&key, &disk_key);
721
722                         if (key.type == BTRFS_QGROUP_RELATION_KEY)
723                                 printf("Ignoring qgroup relation key %llu\n",
724                                        key.objectid);
725
726                         /*
727                          * Ignore: BTRFS_QGROUP_STATUS_KEY,
728                          * BTRFS_QGROUP_LIMIT_KEY, BTRFS_QGROUP_RELATION_KEY
729                          */
730                         if (key.type != BTRFS_QGROUP_INFO_KEY)
731                                 continue;
732
733                         item = btrfs_item_ptr(leaf, i,
734                                               struct btrfs_qgroup_info_item);
735
736                         count = alloc_count(&disk_key, leaf, item);
737                         if (!count) {
738                                 ret = ENOMEM;
739                                 fprintf(stderr, "ERROR: out of memory\n");
740                                 goto out;
741                         }
742                 }
743
744                 ret = btrfs_next_leaf(root, &path);
745                 if (ret != 0)
746                         break;
747         }
748
749         ret = 0;
750         btrfs_release_path(&path);
751 out:
752         return ret;
753 }
754
755 static int add_inline_refs(struct btrfs_fs_info *info,
756                            struct extent_buffer *ei_leaf, int slot,
757                            u64 bytenr, u64 num_bytes, int meta_item)
758 {
759         struct btrfs_extent_item *ei;
760         struct btrfs_extent_inline_ref *iref;
761         struct btrfs_extent_data_ref *dref;
762         u64 flags, root_obj, offset, parent;
763         u32 item_size = btrfs_item_size_nr(ei_leaf, slot);
764         int type;
765         unsigned long end;
766         unsigned long ptr;
767
768         ei = btrfs_item_ptr(ei_leaf, slot, struct btrfs_extent_item);
769         flags = btrfs_extent_flags(ei_leaf, ei);
770
771         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !meta_item) {
772                 struct btrfs_tree_block_info *tbinfo;
773                 tbinfo = (struct btrfs_tree_block_info *)(ei + 1);
774                 iref = (struct btrfs_extent_inline_ref *)(tbinfo + 1);
775         } else {
776                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
777         }
778
779         ptr = (unsigned long)iref;
780         end = (unsigned long)ei + item_size;
781         while (ptr < end) {
782                 iref = (struct btrfs_extent_inline_ref *)ptr;
783
784                 parent = root_obj = 0;
785                 offset = btrfs_extent_inline_ref_offset(ei_leaf, iref);
786                 type = btrfs_extent_inline_ref_type(ei_leaf, iref);
787                 switch (type) {
788                 case BTRFS_TREE_BLOCK_REF_KEY:
789                         root_obj = offset;
790                         break;
791                 case BTRFS_EXTENT_DATA_REF_KEY:
792                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
793                         root_obj = btrfs_extent_data_ref_root(ei_leaf, dref);
794                         break;
795                 case BTRFS_SHARED_DATA_REF_KEY:
796                 case BTRFS_SHARED_BLOCK_REF_KEY:
797                         parent = offset;
798                         break;
799                 default:
800                         return 1;
801                 }
802
803                 if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
804                         return ENOMEM;
805
806                 ptr += btrfs_extent_inline_ref_size(type);
807         }
808
809         return 0;
810 }
811
812 static int add_keyed_ref(struct btrfs_fs_info *info,
813                          struct btrfs_key *key,
814                          struct extent_buffer *leaf, int slot,
815                          u64 bytenr, u64 num_bytes)
816 {
817         u64 root_obj = 0, parent = 0;
818         struct btrfs_extent_data_ref *dref;
819
820         switch(key->type) {
821         case BTRFS_TREE_BLOCK_REF_KEY:
822                 root_obj = key->offset;
823                 break;
824         case BTRFS_EXTENT_DATA_REF_KEY:
825                 dref = btrfs_item_ptr(leaf, slot, struct btrfs_extent_data_ref);
826                 root_obj = btrfs_extent_data_ref_root(leaf, dref);
827                 break;
828         case BTRFS_SHARED_DATA_REF_KEY:
829         case BTRFS_SHARED_BLOCK_REF_KEY:
830                 parent = key->offset;
831                 break;
832         default:
833                 return 1;
834         }
835
836         if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
837                 return ENOMEM;
838
839         return 0;
840 }
841
842 /*
843  * return value of 0 indicates leaf or not meta data. The code that
844  * calls this does not need to make a distinction between the two as
845  * it is only concerned with intermediate blocks which will always
846  * have level > 0.
847  */
848 static int get_tree_block_level(struct btrfs_key *key,
849                                 struct extent_buffer *ei_leaf,
850                                 int slot)
851 {
852         int level = 0;
853         int meta_key = key->type == BTRFS_METADATA_ITEM_KEY;
854         u64 flags;
855         struct btrfs_extent_item *ei;
856
857         ei = btrfs_item_ptr(ei_leaf, slot, struct btrfs_extent_item);
858         flags = btrfs_extent_flags(ei_leaf, ei);
859
860         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !meta_key) {
861                 struct btrfs_tree_block_info *tbinfo;
862                 tbinfo = (struct btrfs_tree_block_info *)(ei + 1);
863                 level = btrfs_tree_block_level(ei_leaf, tbinfo);
864         } else if (meta_key) {
865                 /* skinny metadata */
866                 level = (int)key->offset;
867         }
868         return level;
869 }
870
871 /*
872  * Walk the extent tree, allocating a ref item for every ref and
873  * storing it in the bytenr tree.
874  */
875 static int scan_extents(struct btrfs_fs_info *info,
876                         u64 start, u64 end)
877 {
878         int ret, i, nr, level;
879         struct btrfs_root *root = info->extent_root;
880         struct btrfs_key key;
881         struct btrfs_path path;
882         struct btrfs_disk_key disk_key;
883         struct extent_buffer *leaf;
884         u64 bytenr = 0, num_bytes = 0;
885
886         btrfs_init_path(&path);
887
888         key.objectid = start;
889         key.type = 0;
890         key.offset = 0;
891
892         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
893         if (ret < 0) {
894                 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
895                 goto out;
896         }
897         path.reada = 1;
898
899         while (1) {
900                 leaf = path.nodes[0];
901
902                 nr = btrfs_header_nritems(leaf);
903                 for(i = 0; i < nr; i++) {
904                         btrfs_item_key(leaf, &disk_key, i);
905                         btrfs_disk_key_to_cpu(&key, &disk_key);
906
907                         if (key.objectid < start)
908                                 continue;
909
910                         if (key.objectid > end)
911                                 goto done;
912
913                         if (key.type == BTRFS_EXTENT_ITEM_KEY ||
914                             key.type == BTRFS_METADATA_ITEM_KEY) {
915                                 int meta = 0;
916
917                                 tot_extents_scanned++;
918
919                                 bytenr = key.objectid;
920                                 num_bytes = key.offset;
921                                 if (key.type == BTRFS_METADATA_ITEM_KEY) {
922                                         num_bytes = info->extent_root->leafsize;
923                                         meta = 1;
924                                 }
925
926                                 ret = add_inline_refs(info, leaf, i, bytenr,
927                                                       num_bytes, meta);
928                                 if (ret)
929                                         goto out;
930
931                                 level = get_tree_block_level(&key, leaf, i);
932                                 if (level) {
933                                         if (alloc_tree_block(bytenr, num_bytes,
934                                                              level))
935                                                 return ENOMEM;
936                                 }
937
938                                 continue;
939                         }
940
941                         if (key.type > BTRFS_SHARED_DATA_REF_KEY)
942                                 continue;
943                         if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
944                                 continue;
945
946                         /*
947                          * Keyed refs should come after their extent
948                          * item in the tree. As a result, the value of
949                          * bytenr and num_bytes should be unchanged
950                          * from the above block that catches the
951                          * original extent item.
952                          */
953                         BUG_ON(key.objectid != bytenr);
954
955                         ret = add_keyed_ref(info, &key, leaf, i, bytenr,
956                                             num_bytes);
957                         if (ret)
958                                 goto out;
959                 }
960
961                 ret = btrfs_next_leaf(root, &path);
962                 if (ret != 0) {
963                         if (ret < 0) {
964                                 fprintf(stderr,
965                                         "ERROR: Next leaf failed: %d\n", ret);
966                                 goto out;
967                         }
968                         break;
969                 }
970         }
971 done:
972         ret = 0;
973 out:
974         btrfs_release_path(&path);
975
976         return ret;
977 }
978
979 static void print_fields(u64 bytes, u64 bytes_compressed, char *prefix,
980                          char *type)
981 {
982         printf("%s\t\t%s %llu %s compressed %llu\n",
983                prefix, type, (unsigned long long)bytes, type,
984                (unsigned long long)bytes_compressed);
985 }
986
987 static void print_fields_signed(long long bytes,
988                                 long long bytes_compressed,
989                                 char *prefix, char *type)
990 {
991         printf("%s\t\t%s %lld %s compressed %lld\n",
992                prefix, type, bytes, type, bytes_compressed);
993 }
994
995 static void print_qgroup_difference(struct qgroup_count *count, int verbose)
996 {
997         int is_different;
998         struct btrfs_qgroup_info_item *info = &count->info;
999         struct btrfs_qgroup_info_item *disk = &count->diskinfo;
1000         long long excl_diff = info->exclusive - disk->exclusive;
1001         long long ref_diff = info->referenced - disk->referenced;
1002
1003         is_different = excl_diff || ref_diff;
1004
1005         if (verbose || is_different) {
1006                 printf("Counts for qgroup id: %llu %s\n",
1007                        (unsigned long long)count->qgroupid,
1008                        is_different ? "are different" : "");
1009
1010                 print_fields(info->referenced, info->referenced_compressed,
1011                              "our:", "referenced");
1012                 print_fields(disk->referenced, disk->referenced_compressed,
1013                              "disk:", "referenced");
1014                 if (ref_diff)
1015                         print_fields_signed(ref_diff, ref_diff,
1016                                             "diff:", "referenced");
1017                 print_fields(info->exclusive, info->exclusive_compressed,
1018                              "our:", "exclusive");
1019                 print_fields(disk->exclusive, disk->exclusive_compressed,
1020                              "disk:", "exclusive");
1021                 if (excl_diff)
1022                         print_fields_signed(excl_diff, excl_diff,
1023                                             "diff:", "exclusive");
1024         }
1025 }
1026
1027 void print_qgroup_report(int all)
1028 {
1029         struct rb_node *node;
1030         struct qgroup_count *c;
1031
1032         node = rb_first(&counts.root);
1033         while (node) {
1034                 c = rb_entry(node, struct qgroup_count, rb_node);
1035                 print_qgroup_difference(c, all);
1036                 node = rb_next(node);
1037         }
1038 }
1039
1040 int qgroup_verify_all(struct btrfs_fs_info *info)
1041 {
1042         int ret;
1043
1044         if (!info->quota_enabled)
1045                 return 0;
1046
1047         tree_blocks = ulist_alloc(0);
1048         if (!tree_blocks) {
1049                 fprintf(stderr,
1050                         "ERROR: Out of memory while allocating ulist.\n");
1051                 return ENOMEM;
1052         }
1053
1054         ret = load_quota_info(info);
1055         if (ret) {
1056                 fprintf(stderr, "ERROR: Loading qgroups from disk: %d\n", ret);
1057                 goto out;
1058         }
1059
1060         /*
1061          * Put all extent refs into our rbtree
1062          */
1063         ret = scan_extents(info, 0, ~0ULL);
1064         if (ret) {
1065                 fprintf(stderr, "ERROR: while scanning extent tree: %d\n", ret);
1066                 goto out;
1067         }
1068
1069         ret = map_implied_refs(info);
1070         if (ret) {
1071                 fprintf(stderr, "ERROR: while mapping refs: %d\n", ret);
1072                 goto out;
1073         }
1074
1075         account_all_refs();
1076
1077 out:
1078         /*
1079          * Don't free the qgroup count records as they will be walked
1080          * later via the print function.
1081          */
1082         free_tree_blocks();
1083         free_ref_tree(&by_bytenr);
1084         return ret;
1085 }