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