btrfs-progs: chunk-recover: handle duplicate cache entries
[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 #include "transaction.h"
33 #include "repair.h"
34
35 #include "qgroup-verify.h"
36
37 /*#define QGROUP_VERIFY_DEBUG*/
38 static unsigned long tot_extents_scanned = 0;
39
40 struct qgroup_count;
41 static struct qgroup_count *find_count(u64 qgroupid);
42
43 struct qgroup_info {
44         u64 referenced;
45         u64 referenced_compressed;
46         u64 exclusive;
47         u64 exclusive_compressed;
48 };
49
50 struct qgroup_count {
51         u64 qgroupid;
52         int subvol_exists;
53
54         struct btrfs_disk_key key;
55         struct qgroup_info diskinfo;
56
57         struct qgroup_info info;
58
59         struct rb_node rb_node;
60
61         /* Parents when we are a child group */
62         struct list_head groups;
63
64         /*
65          * Children when we are a parent group (not currently used but
66          * maintained to mirror kernel handling of qgroups)
67          */
68         struct list_head members;
69
70         u64 cur_refcnt;
71
72         struct list_head bad_list;
73 };
74
75 static struct counts_tree {
76         struct rb_root          root;
77         unsigned int            num_groups;
78         unsigned int            rescan_running:1;
79         unsigned int            qgroup_inconsist:1;
80 } counts = { .root = RB_ROOT };
81
82 static LIST_HEAD(bad_qgroups);
83
84 static struct rb_root by_bytenr = RB_ROOT;
85
86 /*
87  * Glue structure to represent the relations between qgroups. Mirrored
88  * from kernel.
89  */
90 struct btrfs_qgroup_list {
91         struct list_head next_group;
92         struct list_head next_member;
93         struct qgroup_count *group; /* Parent group */
94         struct qgroup_count *member;
95 };
96
97 /* Allow us to reset ref counts during accounting without zeroing each group. */
98 static u64 qgroup_seq = 1ULL;
99
100 static inline void update_cur_refcnt(struct qgroup_count *c)
101 {
102         if (c->cur_refcnt < qgroup_seq)
103                 c->cur_refcnt = qgroup_seq;
104         c->cur_refcnt++;
105 }
106
107 static inline u64 group_get_cur_refcnt(struct qgroup_count *c)
108 {
109         if (c->cur_refcnt < qgroup_seq)
110                 return 0;
111         return c->cur_refcnt - qgroup_seq;
112 }
113
114 static void inc_qgroup_seq(int root_count)
115 {
116         qgroup_seq += root_count + 1;
117 }
118
119 /*
120  * List of interior tree blocks. We walk this list after loading the
121  * extent tree to resolve implied refs. For each interior node we'll
122  * place a shared ref in the ref tree against each child object. This
123  * allows the shared ref resolving code to do the actual work later of
124  * finding roots to account against.
125  *
126  * An implied ref is when a tree block has refs on it that may not
127  * exist in any of its child nodes. Even though the refs might not
128  * exist further down the tree, the fact that our interior node has a
129  * ref means we need to account anything below it to all its roots.
130  */
131 static struct ulist *tree_blocks = NULL;        /* unode->val = bytenr, ->aux
132                                                  * = tree_block pointer */
133 struct tree_block {
134         int                     level;
135         u64                     num_bytes;
136 };
137
138 struct ref {
139         u64                     bytenr;
140         u64                     num_bytes;
141         u64                     parent;
142         u64                     root;
143
144         struct rb_node          bytenr_node;
145 };
146
147 #ifdef QGROUP_VERIFY_DEBUG
148 static void print_ref(struct ref *ref)
149 {
150         printf("bytenr: %llu\t\tnum_bytes: %llu\t\t parent: %llu\t\t"
151                "root: %llu\n", ref->bytenr, ref->num_bytes,
152                ref->parent, ref->root);
153 }
154
155 static void print_all_refs(void)
156 {
157         unsigned long count = 0;
158         struct ref *ref;
159         struct rb_node *node;
160
161         node = rb_first(&by_bytenr);
162         while (node) {
163                 ref = rb_entry(node, struct ref, bytenr_node);
164
165                 print_ref(ref);
166
167                 count++;
168                 node = rb_next(node);
169         }
170
171         printf("%lu extents scanned with %lu refs in total.\n",
172                tot_extents_scanned, count);
173 }
174 #endif
175
176 /*
177  * Store by bytenr in rbtree
178  *
179  * The tree is sorted in ascending order by bytenr, then parent, then
180  * root. Since full refs have a parent == 0, those will come before
181  * shared refs.
182  */
183 static int compare_ref(struct ref *orig, u64 bytenr, u64 root, u64 parent)
184 {
185         if (bytenr < orig->bytenr)
186                 return -1;
187         if (bytenr > orig->bytenr)
188                 return 1;
189
190         if (parent < orig->parent)
191                 return -1;
192         if (parent > orig->parent)
193                 return 1;
194
195         if (root < orig->root)
196                 return -1;
197         if (root > orig->root)
198                 return 1;
199
200         return 0;
201 }
202
203 /*
204  * insert a new ref into the tree.  returns the existing ref entry
205  * if one is already there.
206  */
207 static struct ref *insert_ref(struct ref *ref)
208 {
209         int ret;
210         struct rb_node **p = &by_bytenr.rb_node;
211         struct rb_node *parent = NULL;
212         struct ref *curr;
213
214         while (*p) {
215                 parent = *p;
216                 curr = rb_entry(parent, struct ref, bytenr_node);
217
218                 ret = compare_ref(curr, ref->bytenr, ref->root, ref->parent);
219                 if (ret < 0)
220                         p = &(*p)->rb_left;
221                 else if (ret > 0)
222                         p = &(*p)->rb_right;
223                 else
224                         return curr;
225         }
226
227         rb_link_node(&ref->bytenr_node, parent, p);
228         rb_insert_color(&ref->bytenr_node, &by_bytenr);
229         return ref;
230 }
231
232 /*
233  * Partial search, returns the first ref with matching bytenr. Caller
234  * can walk forward from there.
235  *
236  * Leftmost refs will be full refs - this is used to our advantage
237  * when resolving roots.
238  */
239 static struct ref *find_ref_bytenr(u64 bytenr)
240 {
241         struct rb_node *n = by_bytenr.rb_node;
242         struct ref *ref;
243
244         while (n) {
245                 ref = rb_entry(n, struct ref, bytenr_node);
246
247                 if (bytenr < ref->bytenr)
248                         n = n->rb_left;
249                 else if (bytenr > ref->bytenr)
250                         n = n->rb_right;
251                 else {
252                         /* Walk to the left to find the first item */
253                         struct rb_node *node_left = rb_prev(&ref->bytenr_node);
254                         struct ref *ref_left;
255
256                         while (node_left) {
257                                 ref_left = rb_entry(node_left, struct ref,
258                                                     bytenr_node);
259                                 if (ref_left->bytenr != ref->bytenr)
260                                         break;
261                                 ref = ref_left;
262                                 node_left = rb_prev(node_left);
263                         }
264                         return ref;
265                 }
266         }
267         return NULL;
268 }
269
270 static struct ref *find_ref(u64 bytenr, u64 root, u64 parent)
271 {
272         struct rb_node *n = by_bytenr.rb_node;
273         struct ref *ref;
274         int ret;
275
276         while (n) {
277                 ref = rb_entry(n, struct ref, bytenr_node);
278
279                 ret = compare_ref(ref, bytenr, root, parent);
280                 if (ret < 0)
281                         n = n->rb_left;
282                 else if (ret > 0)
283                         n = n->rb_right;
284                 else
285                         return ref;
286         }
287         return NULL;
288 }
289
290 static struct ref *alloc_ref(u64 bytenr, u64 root, u64 parent, u64 num_bytes)
291 {
292         struct ref *ref = find_ref(bytenr, root, parent);
293
294         BUG_ON(parent && root);
295
296         if (ref == NULL) {
297                 ref = calloc(1, sizeof(*ref));
298                 if (ref) {
299                         ref->bytenr = bytenr;
300                         ref->root = root;
301                         ref->parent = parent;
302                         ref->num_bytes = num_bytes;
303
304                         insert_ref(ref);
305                 }
306         }
307         return ref;
308 }
309
310 static void free_ref_node(struct rb_node *node)
311 {
312         struct ref *ref = rb_entry(node, struct ref, bytenr_node);
313         free(ref);
314 }
315
316 FREE_RB_BASED_TREE(ref, free_ref_node);
317
318 /*
319  * Resolves all the possible roots for the ref at parent.
320  */
321 static int find_parent_roots(struct ulist *roots, u64 parent)
322 {
323         struct ref *ref;
324         struct rb_node *node;
325         int ret;
326
327         /*
328          * Search the rbtree for the first ref with bytenr == parent.
329          * Walk forward so long as bytenr == parent, adding resolved root ids.
330          * For each unresolved root, we recurse
331          */
332         ref = find_ref_bytenr(parent);
333         if (!ref) {
334                 error("bytenr ref not found for parent %llu",
335                                 (unsigned long long)parent);
336                 return -EIO;
337         }
338         node = &ref->bytenr_node;
339         if (ref->bytenr != parent) {
340                 error("found bytenr ref does not match parent: %llu != %llu",
341                                 (unsigned long long)ref->bytenr,
342                                 (unsigned long long)parent);
343                 return -EIO;
344         }
345
346         {
347                 /*
348                  * Random sanity check, are we actually getting the
349                  * leftmost node?
350                  */
351                 struct rb_node *prev_node = rb_prev(&ref->bytenr_node);
352                 struct ref *prev;
353
354                 if (prev_node) {
355                         prev = rb_entry(prev_node, struct ref, bytenr_node);
356                         if (prev->bytenr == parent) {
357                                 error(
358                                 "unexpected: prev bytenr same as parent: %llu",
359                                                 (unsigned long long)parent);
360                                 return -EIO;
361                         }
362                 }
363         }
364
365         do {
366                 if (ref->root) {
367                         if (is_fstree(ref->root)) {
368                                 ret = ulist_add(roots, ref->root, 0, 0);
369                                 if (ret < 0)
370                                         goto out;
371                         }
372                 } else {
373                         ret = find_parent_roots(roots, ref->parent);
374                         if (ret < 0)
375                                 goto out;
376                 }
377
378                 node = rb_next(node);
379                 if (node)
380                         ref = rb_entry(node, struct ref, bytenr_node);
381         } while (node && ref->bytenr == parent);
382
383         ret = 0;
384 out:
385         return ret;
386 }
387
388 static int account_one_extent(struct ulist *roots, u64 bytenr, u64 num_bytes)
389 {
390         int ret;
391         u64 id, nr_roots, nr_refs;
392         struct qgroup_count *count;
393         struct ulist *counts = ulist_alloc(0);
394         struct ulist *tmp = ulist_alloc(0);
395         struct ulist_iterator uiter;
396         struct ulist_iterator tmp_uiter;
397         struct ulist_node *unode;
398         struct ulist_node *tmp_unode;
399         struct btrfs_qgroup_list *glist;
400
401         if (!counts || !tmp) {
402                 ulist_free(counts);
403                 ulist_free(tmp);
404                 return ENOMEM;
405         }
406
407         ULIST_ITER_INIT(&uiter);
408         while ((unode = ulist_next(roots, &uiter))) {
409                 BUG_ON(unode->val == 0ULL);
410
411                 /*
412                  * For each root, find their corresponding tracking group and
413                  * add it to our qgroups list.
414                  */
415                 count = find_count(unode->val);
416                 if (!count)
417                         continue;
418
419                 BUG_ON(!is_fstree(unode->val));
420                 ret = ulist_add(counts, count->qgroupid, ptr_to_u64(count), 0);
421                 if (ret < 0)
422                         goto out;
423
424                 /*
425                  * Now we look for parents (and parents of those...). Use a tmp
426                  * ulist here to avoid re-walking (and re-incrementing) our
427                  * already added items on every loop iteration.
428                  */
429                 ulist_reinit(tmp);
430                 ret = ulist_add(tmp, count->qgroupid, ptr_to_u64(count), 0);
431                 if (ret < 0)
432                         goto out;
433
434                 ULIST_ITER_INIT(&tmp_uiter);
435                 while ((tmp_unode = ulist_next(tmp, &tmp_uiter))) {
436                         /* Bump the refcount on a node every time we see it. */
437                         count = u64_to_ptr(tmp_unode->aux);
438                         update_cur_refcnt(count);
439
440                         list_for_each_entry(glist, &count->groups, next_group) {
441                                 struct qgroup_count *parent;
442                                 parent = glist->group;
443                                 id = parent->qgroupid;
444
445                                 BUG_ON(!count);
446
447                                 ret = ulist_add(counts, id, ptr_to_u64(parent),
448                                                 0);
449                                 if (ret < 0)
450                                         goto out;
451                                 ret = ulist_add(tmp, id, ptr_to_u64(parent),
452                                                 0);
453                                 if (ret < 0)
454                                         goto out;
455                         }
456                 }
457         }
458
459         /*
460          * Now that we have gathered up and counted all the groups, we
461          * can add bytes for this ref.
462          */
463         nr_roots = roots->nnodes;
464         ULIST_ITER_INIT(&uiter);
465         while ((unode = ulist_next(counts, &uiter))) {
466                 count = u64_to_ptr(unode->aux);
467
468                 nr_refs = group_get_cur_refcnt(count);
469                 if (nr_refs) {
470                         count->info.referenced += num_bytes;
471                         count->info.referenced_compressed += num_bytes;
472
473                         if (nr_refs == nr_roots) {
474                                 count->info.exclusive += num_bytes;
475                                 count->info.exclusive_compressed += num_bytes;
476                         }
477                 }
478 #ifdef QGROUP_VERIFY_DEBUG
479                 printf("account (%llu, %llu), qgroup %llu/%llu, rfer %llu,"
480                        " excl %llu, refs %llu, roots %llu\n", bytenr, num_bytes,
481                        btrfs_qgroup_level(count->qgroupid),
482                        btrfs_qgroup_subvid(count->qgroupid),
483                        count->info.referenced, count->info.exclusive, nr_refs,
484                        nr_roots);
485 #endif
486         }
487
488         inc_qgroup_seq(roots->nnodes);
489         ret = 0;
490 out:
491         ulist_free(counts);
492         ulist_free(tmp);
493         return ret;
494 }
495
496 static void print_subvol_info(u64 subvolid, u64 bytenr, u64 num_bytes,
497                               struct ulist *roots);
498 /*
499  * Account each ref. Walk the refs, for each set of refs in a
500  * given bytenr:
501  *
502  * - add the roots for direct refs to the ref roots ulist
503  *
504  * - resolve all possible roots for shared refs, insert each
505  *   of those into ref_roots ulist (this is a recursive process)
506  *
507  * - With all roots resolved we can account the ref - this is done in
508  *   account_one_extent().
509  */
510 static int account_all_refs(int do_qgroups, u64 search_subvol)
511 {
512         struct ref *ref;
513         struct rb_node *node;
514         u64 bytenr, num_bytes;
515         struct ulist *roots = ulist_alloc(0);
516         int ret;
517
518         node = rb_first(&by_bytenr);
519         while (node) {
520                 ulist_reinit(roots);
521
522                 ref = rb_entry(node, struct ref, bytenr_node);
523                 /*
524                  * Walk forward through the list of refs for this
525                  * bytenr, adding roots to our ulist. If it's a full
526                  * ref, then we have the easy case. Otherwise we need
527                  * to search for roots.
528                  */
529                 bytenr = ref->bytenr;
530                 num_bytes = ref->num_bytes;
531                 do {
532                         BUG_ON(ref->bytenr != bytenr);
533                         BUG_ON(ref->num_bytes != num_bytes);
534                         if (ref->root) {
535                                 if (is_fstree(ref->root)) {
536                                         if (ulist_add(roots, ref->root, 0, 0) < 0)
537                                                 goto enomem;
538                                 }
539                         } else {
540                                 ret = find_parent_roots(roots, ref->parent);
541                                 if (ret < 0)
542                                         goto enomem;
543                         }
544
545                         /*
546                          * When we leave this inner loop, node is set
547                          * to next in our tree and will be turned into
548                          * a ref object up top
549                          */
550                         node = rb_next(node);
551                         if (node)
552                                 ref = rb_entry(node, struct ref, bytenr_node);
553                 } while (node && ref->bytenr == bytenr);
554
555                 if (search_subvol)
556                         print_subvol_info(search_subvol, bytenr, num_bytes,
557                                           roots);
558
559                 if (!do_qgroups)
560                         continue;
561
562                 if (account_one_extent(roots, bytenr, num_bytes))
563                         goto enomem;
564         }
565
566         ulist_free(roots);
567         return 0;
568 enomem:
569         error("Out of memory while accounting refs for qgroups");
570         return -ENOMEM;
571 }
572
573 static u64 resolve_one_root(u64 bytenr)
574 {
575         struct ref *ref = find_ref_bytenr(bytenr);
576
577         BUG_ON(ref == NULL);
578
579         if (ref->root)
580                 return ref->root;
581         return resolve_one_root(ref->parent);
582 }
583
584 static inline struct tree_block *unode_tree_block(struct ulist_node *unode)
585 {
586         return u64_to_ptr(unode->aux);
587 }
588 static inline u64 unode_bytenr(struct ulist_node *unode)
589 {
590         return unode->val;
591 }
592
593 static int alloc_tree_block(u64 bytenr, u64 num_bytes, int level)
594 {
595         struct tree_block *block = calloc(1, sizeof(*block));
596
597         if (block) {
598                 block->num_bytes = num_bytes;
599                 block->level = level;
600                 if (ulist_add(tree_blocks, bytenr, ptr_to_u64(block), 0) >= 0)
601                         return 0;
602                 free(block);
603         }
604         return -ENOMEM;
605 }
606
607 static void free_tree_blocks(void)
608 {
609         struct ulist_iterator uiter;
610         struct ulist_node *unode;
611
612         if (!tree_blocks)
613                 return;
614
615         ULIST_ITER_INIT(&uiter);
616         while ((unode = ulist_next(tree_blocks, &uiter)))
617                 free(unode_tree_block(unode));
618         ulist_free(tree_blocks);        
619         tree_blocks = NULL;
620 }
621
622 #ifdef QGROUP_VERIFY_DEBUG
623 static void print_tree_block(u64 bytenr, struct tree_block *block)
624 {
625         struct ref *ref;
626         struct rb_node *node;
627
628         printf("tree block: %llu\t\tlevel: %d\n", (unsigned long long)bytenr,
629                block->level);
630
631         ref = find_ref_bytenr(bytenr);
632         node = &ref->bytenr_node;
633         do {
634                 print_ref(ref);
635                 node = rb_next(node);
636                 if (node)
637                         ref = rb_entry(node, struct ref, bytenr_node);
638         } while (node && ref->bytenr == bytenr);
639
640         printf("\n");
641 }
642
643 static void print_all_tree_blocks(void)
644 {
645         struct ulist_iterator uiter;
646         struct ulist_node *unode;
647
648         if (!tree_blocks)
649                 return;
650
651         printf("Listing all found interior tree nodes:\n");
652
653         ULIST_ITER_INIT(&uiter);
654         while ((unode = ulist_next(tree_blocks, &uiter)))
655                 print_tree_block(unode_bytenr(unode), unode_tree_block(unode));
656 }
657 #endif
658
659 static int add_refs_for_leaf_items(struct extent_buffer *eb, u64 ref_parent)
660 {
661         int nr, i;
662         int extent_type;
663         u64 bytenr, num_bytes;
664         struct btrfs_key key;
665         struct btrfs_disk_key disk_key;
666         struct btrfs_file_extent_item *fi;
667
668         nr = btrfs_header_nritems(eb);
669         for (i = 0; i < nr; i++) {
670                 btrfs_item_key(eb, &disk_key, i);
671                 btrfs_disk_key_to_cpu(&key, &disk_key);
672
673                 if (key.type != BTRFS_EXTENT_DATA_KEY)
674                         continue;
675
676                 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
677                 /* filter out: inline, disk_bytenr == 0, compressed?
678                  * not if we can avoid it */
679                 extent_type = btrfs_file_extent_type(eb, fi);
680
681                 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
682                         continue;
683
684                 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
685                 if (!bytenr)
686                         continue;
687
688                 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
689                 if (alloc_ref(bytenr, 0, ref_parent, num_bytes) == NULL)
690                         return ENOMEM;
691         }
692
693         return 0;
694 }
695
696 static int travel_tree(struct btrfs_fs_info *info, struct btrfs_root *root,
697                        u64 bytenr, u64 num_bytes, u64 ref_parent)
698 {
699         int ret, nr, i;
700         struct extent_buffer *eb;
701         u64 new_bytenr;
702         u64 new_num_bytes;
703
704 //      printf("travel_tree: bytenr: %llu\tnum_bytes: %llu\tref_parent: %llu\n",
705 //             bytenr, num_bytes, ref_parent);
706
707         eb = read_tree_block(root, bytenr, num_bytes, 0);
708         if (!extent_buffer_uptodate(eb))
709                 return -EIO;
710
711         ret = 0;
712         /* Don't add a ref for our starting tree block to itself */
713         if (bytenr != ref_parent) {
714                 if (alloc_ref(bytenr, 0, ref_parent, num_bytes) == NULL)
715                         return ENOMEM;
716         }
717
718         if (btrfs_is_leaf(eb)) {
719                 ret = add_refs_for_leaf_items(eb, ref_parent);
720                 goto out;
721         }
722
723         /*
724          * Interior nodes are tuples of (key, bytenr) where key is the
725          * leftmost key in the tree block pointed to by bytenr. We
726          * don't have to care about key here, just follow the bytenr
727          * pointer.
728          */
729         nr = btrfs_header_nritems(eb);
730         for (i = 0; i < nr; i++) {
731                 new_bytenr = btrfs_node_blockptr(eb, i);
732                 new_num_bytes = root->nodesize;
733
734                 ret = travel_tree(info, root, new_bytenr, new_num_bytes,
735                                   ref_parent);
736         }
737
738 out:
739         free_extent_buffer(eb);
740         return ret;
741 }
742
743 static int add_refs_for_implied(struct btrfs_fs_info *info, u64 bytenr,
744                                 struct tree_block *block)
745 {
746         int ret;
747         u64 root_id = resolve_one_root(bytenr);
748         struct btrfs_root *root;
749         struct btrfs_key key;
750
751         key.objectid = root_id;
752         key.type = BTRFS_ROOT_ITEM_KEY;
753         key.offset = (u64)-1;
754
755         /*
756          * XXX: Don't free the root object as we don't know whether it
757          * came off our fs_info struct or not.
758          */
759         root = btrfs_read_fs_root(info, &key);
760         if (!root || IS_ERR(root))
761                 return ENOENT;
762
763         ret = travel_tree(info, root, bytenr, block->num_bytes, bytenr);
764         if (ret)
765                 return ret;
766
767         return 0;
768 }
769
770 /*
771  * Place shared refs in the ref tree for each child of an interior tree node.
772  */
773 static int map_implied_refs(struct btrfs_fs_info *info)
774 {
775         int ret = 0;
776         struct ulist_iterator uiter;
777         struct ulist_node *unode;
778
779         ULIST_ITER_INIT(&uiter);
780         while ((unode = ulist_next(tree_blocks, &uiter))) {
781                 ret = add_refs_for_implied(info, unode_bytenr(unode),
782                                            unode_tree_block(unode));
783                 if (ret)
784                         goto out;
785         }
786 out:
787         return ret;
788 }
789
790 /*
791  * insert a new root into the tree.  returns the existing root entry
792  * if one is already there.  qgroupid is used
793  * as the key
794  */
795 static int insert_count(struct qgroup_count *qc)
796 {
797         struct rb_node **p = &counts.root.rb_node;
798         struct rb_node *parent = NULL;
799         struct qgroup_count *curr;
800
801         while (*p) {
802                 parent = *p;
803                 curr = rb_entry(parent, struct qgroup_count, rb_node);
804
805                 if (qc->qgroupid < curr->qgroupid)
806                         p = &(*p)->rb_left;
807                 else if (qc->qgroupid > curr->qgroupid)
808                         p = &(*p)->rb_right;
809                 else
810                         return EEXIST;
811         }
812         counts.num_groups++;
813         rb_link_node(&qc->rb_node, parent, p);
814         rb_insert_color(&qc->rb_node, &counts.root);
815         return 0;
816 }
817
818 static struct qgroup_count *find_count(u64 qgroupid)
819 {
820         struct rb_node *n = counts.root.rb_node;
821         struct qgroup_count *count;
822
823         while (n) {
824                 count = rb_entry(n, struct qgroup_count, rb_node);
825
826                 if (qgroupid < count->qgroupid)
827                         n = n->rb_left;
828                 else if (qgroupid > count->qgroupid)
829                         n = n->rb_right;
830                 else
831                         return count;
832         }
833         return NULL;
834 }
835
836 static struct qgroup_count *alloc_count(struct btrfs_disk_key *key,
837                                         struct extent_buffer *leaf,
838                                         struct btrfs_qgroup_info_item *disk)
839 {
840         struct qgroup_count *c = calloc(1, sizeof(*c));
841         struct qgroup_info *item;
842
843         if (c) {
844                 c->qgroupid = btrfs_disk_key_offset(key);
845                 c->key = *key;
846
847                 item = &c->diskinfo;
848                 item->referenced = btrfs_qgroup_info_referenced(leaf, disk);
849                 item->referenced_compressed =
850                         btrfs_qgroup_info_referenced_compressed(leaf, disk);
851                 item->exclusive = btrfs_qgroup_info_exclusive(leaf, disk);
852                 item->exclusive_compressed =
853                         btrfs_qgroup_info_exclusive_compressed(leaf, disk);
854                 INIT_LIST_HEAD(&c->groups);
855                 INIT_LIST_HEAD(&c->members);
856                 INIT_LIST_HEAD(&c->bad_list);
857
858                 if (insert_count(c)) {
859                         free(c);
860                         c = NULL;
861                 }
862         }
863         return c;
864 }
865
866 static int add_qgroup_relation(u64 memberid, u64 parentid)
867 {
868         struct qgroup_count *member;
869         struct qgroup_count *parent;
870         struct btrfs_qgroup_list *list;
871
872         if (memberid > parentid)
873                 return 0;
874
875         member = find_count(memberid);
876         parent = find_count(parentid);
877         if (!member || !parent)
878                 return -ENOENT;
879
880         list = calloc(1, sizeof(*list));
881         if (!list)
882                 return -ENOMEM;
883
884         list->group = parent;
885         list->member = member;
886         list_add_tail(&list->next_group, &member->groups);
887         list_add_tail(&list->next_member, &parent->members);
888
889         return 0;
890 }
891
892 static void read_qgroup_status(struct extent_buffer *eb, int slot,
893                               struct counts_tree *counts)
894 {
895         struct btrfs_qgroup_status_item *status_item;
896         u64 flags;
897
898         status_item = btrfs_item_ptr(eb, slot, struct btrfs_qgroup_status_item);
899         flags = btrfs_qgroup_status_flags(eb, status_item);
900         /*
901          * Since qgroup_inconsist/rescan_running is just one bit,
902          * assign value directly won't work.
903          */
904         counts->qgroup_inconsist = !!(flags &
905                         BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT);
906         counts->rescan_running = !!(flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN);
907 }
908
909 static int load_quota_info(struct btrfs_fs_info *info)
910 {
911         int ret;
912         struct btrfs_root *root = info->quota_root;
913         struct btrfs_root *tmproot;
914         struct btrfs_path path;
915         struct btrfs_key key;
916         struct btrfs_key root_key;
917         struct btrfs_disk_key disk_key;
918         struct extent_buffer *leaf;
919         struct btrfs_qgroup_info_item *item;
920         struct qgroup_count *count;
921         int i, nr;
922         int search_relations = 0;
923
924 loop:
925         /*
926          * Do 2 passes, the first allocates group counts and reads status
927          * items. The 2nd pass picks up relation items and glues them to their
928          * respective count structures.
929          */
930         btrfs_init_path(&path);
931
932         key.offset = 0;
933         key.objectid = search_relations ? 0 : BTRFS_QGROUP_RELATION_KEY;
934         key.type = 0;
935
936         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
937         if (ret < 0) {
938                 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
939                 goto out;
940         }
941
942         while (1) {
943                 leaf = path.nodes[0];
944
945                 nr = btrfs_header_nritems(leaf);
946                 for(i = 0; i < nr; i++) {
947                         btrfs_item_key(leaf, &disk_key, i);
948                         btrfs_disk_key_to_cpu(&key, &disk_key);
949
950                         if (search_relations) {
951                                 if (key.type == BTRFS_QGROUP_RELATION_KEY) {
952                                         ret = add_qgroup_relation(key.objectid,
953                                                                   key.offset);
954                                         if (ret) {
955                                                 error("out of memory");
956                                                 goto out;
957                                         }
958                                 }
959                                 continue;
960                         }
961
962                         if (key.type == BTRFS_QGROUP_STATUS_KEY) {
963                                 read_qgroup_status(leaf, i, &counts);
964                                 continue;
965                         }
966
967                         /*
968                          * At this point, we can ignore anything that
969                          * isn't a qgroup info.
970                          */
971                         if (key.type != BTRFS_QGROUP_INFO_KEY)
972                                 continue;
973
974                         item = btrfs_item_ptr(leaf, i,
975                                               struct btrfs_qgroup_info_item);
976
977                         count = alloc_count(&disk_key, leaf, item);
978                         if (!count) {
979                                 ret = ENOMEM;
980                                 fprintf(stderr, "ERROR: out of memory\n");
981                                 goto out;
982                         }
983
984                         root_key.objectid = key.offset;
985                         root_key.type = BTRFS_ROOT_ITEM_KEY;
986                         root_key.offset = (u64)-1;
987                         tmproot = btrfs_read_fs_root_no_cache(info, &root_key);
988                         if (tmproot && !IS_ERR(tmproot)) {
989                                 count->subvol_exists = 1;
990                                 btrfs_free_fs_root(tmproot);
991                         }
992                 }
993
994                 ret = btrfs_next_leaf(root, &path);
995                 if (ret != 0)
996                         break;
997         }
998
999         ret = 0;
1000         btrfs_release_path(&path);
1001
1002         if (!search_relations) {
1003                 search_relations = 1;
1004                 goto loop;
1005         }
1006
1007 out:
1008         return ret;
1009 }
1010
1011 static int add_inline_refs(struct btrfs_fs_info *info,
1012                            struct extent_buffer *ei_leaf, int slot,
1013                            u64 bytenr, u64 num_bytes, int meta_item)
1014 {
1015         struct btrfs_extent_item *ei;
1016         struct btrfs_extent_inline_ref *iref;
1017         struct btrfs_extent_data_ref *dref;
1018         u64 flags, root_obj, offset, parent;
1019         u32 item_size = btrfs_item_size_nr(ei_leaf, slot);
1020         int type;
1021         unsigned long end;
1022         unsigned long ptr;
1023
1024         ei = btrfs_item_ptr(ei_leaf, slot, struct btrfs_extent_item);
1025         flags = btrfs_extent_flags(ei_leaf, ei);
1026
1027         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !meta_item) {
1028                 struct btrfs_tree_block_info *tbinfo;
1029                 tbinfo = (struct btrfs_tree_block_info *)(ei + 1);
1030                 iref = (struct btrfs_extent_inline_ref *)(tbinfo + 1);
1031         } else {
1032                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1033         }
1034
1035         ptr = (unsigned long)iref;
1036         end = (unsigned long)ei + item_size;
1037         while (ptr < end) {
1038                 iref = (struct btrfs_extent_inline_ref *)ptr;
1039
1040                 parent = root_obj = 0;
1041                 offset = btrfs_extent_inline_ref_offset(ei_leaf, iref);
1042                 type = btrfs_extent_inline_ref_type(ei_leaf, iref);
1043                 switch (type) {
1044                 case BTRFS_TREE_BLOCK_REF_KEY:
1045                         root_obj = offset;
1046                         break;
1047                 case BTRFS_EXTENT_DATA_REF_KEY:
1048                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1049                         root_obj = btrfs_extent_data_ref_root(ei_leaf, dref);
1050                         break;
1051                 case BTRFS_SHARED_DATA_REF_KEY:
1052                 case BTRFS_SHARED_BLOCK_REF_KEY:
1053                         parent = offset;
1054                         break;
1055                 default:
1056                         return 1;
1057                 }
1058
1059                 if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
1060                         return ENOMEM;
1061
1062                 ptr += btrfs_extent_inline_ref_size(type);
1063         }
1064
1065         return 0;
1066 }
1067
1068 static int add_keyed_ref(struct btrfs_fs_info *info,
1069                          struct btrfs_key *key,
1070                          struct extent_buffer *leaf, int slot,
1071                          u64 bytenr, u64 num_bytes)
1072 {
1073         u64 root_obj = 0, parent = 0;
1074         struct btrfs_extent_data_ref *dref;
1075
1076         switch(key->type) {
1077         case BTRFS_TREE_BLOCK_REF_KEY:
1078                 root_obj = key->offset;
1079                 break;
1080         case BTRFS_EXTENT_DATA_REF_KEY:
1081                 dref = btrfs_item_ptr(leaf, slot, struct btrfs_extent_data_ref);
1082                 root_obj = btrfs_extent_data_ref_root(leaf, dref);
1083                 break;
1084         case BTRFS_SHARED_DATA_REF_KEY:
1085         case BTRFS_SHARED_BLOCK_REF_KEY:
1086                 parent = key->offset;
1087                 break;
1088         default:
1089                 return 1;
1090         }
1091
1092         if (alloc_ref(bytenr, root_obj, parent, num_bytes) == NULL)
1093                 return ENOMEM;
1094
1095         return 0;
1096 }
1097
1098 /*
1099  * return value of 0 indicates leaf or not meta data. The code that
1100  * calls this does not need to make a distinction between the two as
1101  * it is only concerned with intermediate blocks which will always
1102  * have level > 0.
1103  */
1104 static int get_tree_block_level(struct btrfs_key *key,
1105                                 struct extent_buffer *ei_leaf,
1106                                 int slot)
1107 {
1108         int level = 0;
1109         int meta_key = key->type == BTRFS_METADATA_ITEM_KEY;
1110         u64 flags;
1111         struct btrfs_extent_item *ei;
1112
1113         ei = btrfs_item_ptr(ei_leaf, slot, struct btrfs_extent_item);
1114         flags = btrfs_extent_flags(ei_leaf, ei);
1115
1116         if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !meta_key) {
1117                 struct btrfs_tree_block_info *tbinfo;
1118                 tbinfo = (struct btrfs_tree_block_info *)(ei + 1);
1119                 level = btrfs_tree_block_level(ei_leaf, tbinfo);
1120         } else if (meta_key) {
1121                 /* skinny metadata */
1122                 level = (int)key->offset;
1123         }
1124         return level;
1125 }
1126
1127 /*
1128  * Walk the extent tree, allocating a ref item for every ref and
1129  * storing it in the bytenr tree.
1130  */
1131 static int scan_extents(struct btrfs_fs_info *info,
1132                         u64 start, u64 end)
1133 {
1134         int ret, i, nr, level;
1135         struct btrfs_root *root = info->extent_root;
1136         struct btrfs_key key;
1137         struct btrfs_path path;
1138         struct btrfs_disk_key disk_key;
1139         struct extent_buffer *leaf;
1140         u64 bytenr = 0, num_bytes = 0;
1141
1142         btrfs_init_path(&path);
1143
1144         key.objectid = start;
1145         key.type = 0;
1146         key.offset = 0;
1147
1148         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1149         if (ret < 0) {
1150                 fprintf(stderr, "ERROR: Couldn't search slot: %d\n", ret);
1151                 goto out;
1152         }
1153         path.reada = 1;
1154
1155         while (1) {
1156                 leaf = path.nodes[0];
1157
1158                 nr = btrfs_header_nritems(leaf);
1159                 for(i = 0; i < nr; i++) {
1160                         btrfs_item_key(leaf, &disk_key, i);
1161                         btrfs_disk_key_to_cpu(&key, &disk_key);
1162
1163                         if (key.objectid < start)
1164                                 continue;
1165
1166                         if (key.objectid > end)
1167                                 goto done;
1168
1169                         if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1170                             key.type == BTRFS_METADATA_ITEM_KEY) {
1171                                 int meta = 0;
1172
1173                                 tot_extents_scanned++;
1174
1175                                 bytenr = key.objectid;
1176                                 num_bytes = key.offset;
1177                                 if (key.type == BTRFS_METADATA_ITEM_KEY) {
1178                                         num_bytes = info->extent_root->nodesize;
1179                                         meta = 1;
1180                                 }
1181
1182                                 ret = add_inline_refs(info, leaf, i, bytenr,
1183                                                       num_bytes, meta);
1184                                 if (ret)
1185                                         goto out;
1186
1187                                 level = get_tree_block_level(&key, leaf, i);
1188                                 if (level) {
1189                                         if (alloc_tree_block(bytenr, num_bytes,
1190                                                              level))
1191                                                 return ENOMEM;
1192                                 }
1193
1194                                 continue;
1195                         }
1196
1197                         if (key.type > BTRFS_SHARED_DATA_REF_KEY)
1198                                 continue;
1199                         if (key.type < BTRFS_TREE_BLOCK_REF_KEY)
1200                                 continue;
1201
1202                         /*
1203                          * Keyed refs should come after their extent
1204                          * item in the tree. As a result, the value of
1205                          * bytenr and num_bytes should be unchanged
1206                          * from the above block that catches the
1207                          * original extent item.
1208                          */
1209                         BUG_ON(key.objectid != bytenr);
1210
1211                         ret = add_keyed_ref(info, &key, leaf, i, bytenr,
1212                                             num_bytes);
1213                         if (ret)
1214                                 goto out;
1215                 }
1216
1217                 ret = btrfs_next_leaf(root, &path);
1218                 if (ret != 0) {
1219                         if (ret < 0) {
1220                                 fprintf(stderr,
1221                                         "ERROR: Next leaf failed: %d\n", ret);
1222                                 goto out;
1223                         }
1224                         break;
1225                 }
1226         }
1227 done:
1228         ret = 0;
1229 out:
1230         btrfs_release_path(&path);
1231
1232         return ret;
1233 }
1234
1235 static void print_fields(u64 bytes, u64 bytes_compressed, char *prefix,
1236                          char *type)
1237 {
1238         printf("%s\t\t%s %llu %s compressed %llu\n",
1239                prefix, type, (unsigned long long)bytes, type,
1240                (unsigned long long)bytes_compressed);
1241 }
1242
1243 static void print_fields_signed(long long bytes,
1244                                 long long bytes_compressed,
1245                                 char *prefix, char *type)
1246 {
1247         printf("%s\t\t%s %lld %s compressed %lld\n",
1248                prefix, type, bytes, type, bytes_compressed);
1249 }
1250
1251 static inline int qgroup_printable(struct qgroup_count *c)
1252 {
1253         return !!(c->subvol_exists || btrfs_qgroup_level(c->qgroupid));
1254 }
1255
1256 static int report_qgroup_difference(struct qgroup_count *count, int verbose)
1257 {
1258         int is_different;
1259         struct qgroup_info *info = &count->info;
1260         struct qgroup_info *disk = &count->diskinfo;
1261         long long excl_diff = info->exclusive - disk->exclusive;
1262         long long ref_diff = info->referenced - disk->referenced;
1263
1264         is_different = excl_diff || ref_diff;
1265
1266         if (verbose || (is_different && qgroup_printable(count))) {
1267                 printf("Counts for qgroup id: %llu/%llu %s\n",
1268                        btrfs_qgroup_level(count->qgroupid),
1269                        btrfs_qgroup_subvid(count->qgroupid),
1270                        is_different ? "are different" : "");
1271
1272                 print_fields(info->referenced, info->referenced_compressed,
1273                              "our:", "referenced");
1274                 print_fields(disk->referenced, disk->referenced_compressed,
1275                              "disk:", "referenced");
1276                 if (ref_diff)
1277                         print_fields_signed(ref_diff, ref_diff,
1278                                             "diff:", "referenced");
1279                 print_fields(info->exclusive, info->exclusive_compressed,
1280                              "our:", "exclusive");
1281                 print_fields(disk->exclusive, disk->exclusive_compressed,
1282                              "disk:", "exclusive");
1283                 if (excl_diff)
1284                         print_fields_signed(excl_diff, excl_diff,
1285                                             "diff:", "exclusive");
1286         }
1287
1288         return is_different;
1289 }
1290
1291 void report_qgroups(int all)
1292 {
1293         struct rb_node *node;
1294         struct qgroup_count *c;
1295
1296         if (!repair && counts.rescan_running) {
1297                 if (all) {
1298                         printf(
1299         "Qgroup rescan is running, a difference in qgroup counts is expected\n");
1300                 } else {
1301                         printf(
1302         "Qgroup rescan is running, qgroups will not be printed.\n");
1303                         return;
1304                 }
1305         }
1306         if (counts.qgroup_inconsist && !counts.rescan_running)
1307                 fprintf(stderr, "Qgroup are marked as inconsistent.\n");
1308         node = rb_first(&counts.root);
1309         while (node) {
1310                 c = rb_entry(node, struct qgroup_count, rb_node);
1311
1312                 if (report_qgroup_difference(c, all))
1313                         list_add_tail(&c->bad_list, &bad_qgroups);
1314
1315                 node = rb_next(node);
1316         }
1317 }
1318
1319 void free_qgroup_counts(void)
1320 {
1321         struct rb_node *node;
1322         struct qgroup_count *c;
1323         struct btrfs_qgroup_list *glist, *tmpglist;
1324
1325         node = rb_first(&counts.root);
1326         while (node) {
1327                 c = rb_entry(node, struct qgroup_count, rb_node);
1328
1329                 list_del(&c->bad_list);
1330
1331                 list_for_each_entry_safe(glist, tmpglist, &c->groups,
1332                                          next_group) {
1333                         list_del(&glist->next_group);
1334                         list_del(&glist->next_member);
1335                         free(glist);
1336                 }
1337                 list_for_each_entry_safe(glist, tmpglist, &c->members,
1338                                          next_group) {
1339                         list_del(&glist->next_group);
1340                         list_del(&glist->next_member);
1341                         free(glist);
1342                 }
1343
1344                 node = rb_next(node);
1345
1346                 rb_erase(&c->rb_node, &counts.root);
1347                 free(c);
1348         }
1349 }
1350
1351 int qgroup_verify_all(struct btrfs_fs_info *info)
1352 {
1353         int ret;
1354
1355         if (!info->quota_enabled)
1356                 return 0;
1357
1358         tree_blocks = ulist_alloc(0);
1359         if (!tree_blocks) {
1360                 fprintf(stderr,
1361                         "ERROR: Out of memory while allocating ulist.\n");
1362                 return ENOMEM;
1363         }
1364
1365         ret = load_quota_info(info);
1366         if (ret) {
1367                 fprintf(stderr, "ERROR: Loading qgroups from disk: %d\n", ret);
1368                 goto out;
1369         }
1370
1371         /*
1372          * Put all extent refs into our rbtree
1373          */
1374         ret = scan_extents(info, 0, ~0ULL);
1375         if (ret) {
1376                 fprintf(stderr, "ERROR: while scanning extent tree: %d\n", ret);
1377                 goto out;
1378         }
1379
1380         ret = map_implied_refs(info);
1381         if (ret) {
1382                 fprintf(stderr, "ERROR: while mapping refs: %d\n", ret);
1383                 goto out;
1384         }
1385
1386         ret = account_all_refs(1, 0);
1387
1388 out:
1389         /*
1390          * Don't free the qgroup count records as they will be walked
1391          * later via the print function.
1392          */
1393         free_tree_blocks();
1394         free_ref_tree(&by_bytenr);
1395         return ret;
1396 }
1397
1398 static void __print_subvol_info(u64 bytenr, u64 num_bytes, struct ulist *roots)
1399 {
1400         int n = roots->nnodes;
1401         struct ulist_iterator uiter;
1402         struct ulist_node *unode;
1403
1404         printf("%llu\t%llu\t%d\t", bytenr, num_bytes, n);
1405
1406         ULIST_ITER_INIT(&uiter);
1407         while ((unode = ulist_next(roots, &uiter))) {
1408                 printf("%llu ", unode->val);
1409         }
1410         printf("\n");
1411 }
1412
1413 static void print_subvol_info(u64 subvolid, u64 bytenr, u64 num_bytes,
1414                               struct ulist *roots)
1415 {
1416         struct ulist_iterator uiter;
1417         struct ulist_node *unode;
1418
1419         ULIST_ITER_INIT(&uiter);
1420         while ((unode = ulist_next(roots, &uiter))) {
1421                 BUG_ON(unode->val == 0ULL);
1422                 if (unode->val == subvolid) {
1423                         __print_subvol_info(bytenr, num_bytes, roots);
1424                         return;
1425                 }
1426         }
1427
1428
1429 }
1430
1431 int print_extent_state(struct btrfs_fs_info *info, u64 subvol)
1432 {
1433         int ret;
1434
1435         tree_blocks = ulist_alloc(0);
1436         if (!tree_blocks) {
1437                 fprintf(stderr,
1438                         "ERROR: Out of memory while allocating ulist.\n");
1439                 return ENOMEM;
1440         }
1441
1442         /*
1443          * Put all extent refs into our rbtree
1444          */
1445         ret = scan_extents(info, 0, ~0ULL);
1446         if (ret) {
1447                 fprintf(stderr, "ERROR: while scanning extent tree: %d\n", ret);
1448                 goto out;
1449         }
1450
1451         ret = map_implied_refs(info);
1452         if (ret) {
1453                 fprintf(stderr, "ERROR: while mapping refs: %d\n", ret);
1454                 goto out;
1455         }
1456
1457         printf("Offset\t\tLen\tRoot Refs\tRoots\n");
1458         ret = account_all_refs(0, subvol);
1459
1460 out:
1461         free_tree_blocks();
1462         free_ref_tree(&by_bytenr);
1463         return ret;
1464 }
1465
1466 static int repair_qgroup_info(struct btrfs_fs_info *info,
1467                               struct qgroup_count *count)
1468 {
1469         int ret;
1470         struct btrfs_root *root = info->quota_root;
1471         struct btrfs_trans_handle *trans;
1472         struct btrfs_path *path;
1473         struct btrfs_qgroup_info_item *info_item;
1474         struct btrfs_key key;
1475
1476         printf("Repair qgroup %llu/%llu\n", btrfs_qgroup_level(count->qgroupid),
1477                btrfs_qgroup_subvid(count->qgroupid));
1478
1479         path = btrfs_alloc_path();
1480         if (!path)
1481                 return -ENOMEM;
1482
1483         trans = btrfs_start_transaction(root, 1);
1484         if (IS_ERR(trans)) {
1485                 btrfs_free_path(path);
1486                 return PTR_ERR(trans);
1487         }
1488
1489         key.objectid = 0;
1490         key.type = BTRFS_QGROUP_INFO_KEY;
1491         key.offset = count->qgroupid;
1492         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1493         if (ret) {
1494                 error("Could not find disk item for qgroup %llu/%llu.\n",
1495                       btrfs_qgroup_level(count->qgroupid),
1496                       btrfs_qgroup_subvid(count->qgroupid));
1497                 if (ret > 0)
1498                         ret = -ENOENT;
1499                 goto out;
1500         }
1501
1502         info_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1503                                    struct btrfs_qgroup_info_item);
1504
1505         btrfs_set_qgroup_info_generation(path->nodes[0], info_item,
1506                                          trans->transid);
1507
1508         btrfs_set_qgroup_info_referenced(path->nodes[0], info_item,
1509                                          count->info.referenced);
1510         btrfs_set_qgroup_info_referenced_compressed(path->nodes[0], info_item,
1511                                             count->info.referenced_compressed);
1512
1513         btrfs_set_qgroup_info_exclusive(path->nodes[0], info_item,
1514                                         count->info.exclusive);
1515         btrfs_set_qgroup_info_exclusive_compressed(path->nodes[0], info_item,
1516                                            count->info.exclusive_compressed);
1517
1518         btrfs_mark_buffer_dirty(path->nodes[0]);
1519
1520 out:
1521         btrfs_commit_transaction(trans, root);
1522         btrfs_free_path(path);
1523
1524         return ret;
1525 }
1526
1527 static int repair_qgroup_status(struct btrfs_fs_info *info)
1528 {
1529         int ret;
1530         struct btrfs_root *root = info->quota_root;
1531         struct btrfs_trans_handle *trans;
1532         struct btrfs_path *path;
1533         struct btrfs_key key;
1534         struct btrfs_qgroup_status_item *status_item;
1535
1536         printf("Repair qgroup status item\n");
1537
1538         path = btrfs_alloc_path();
1539         if (!path)
1540                 return -ENOMEM;
1541
1542         trans = btrfs_start_transaction(root, 1);
1543         if (IS_ERR(trans)) {
1544                 btrfs_free_path(path);
1545                 return PTR_ERR(trans);
1546         }
1547
1548         key.objectid = 0;
1549         key.type = BTRFS_QGROUP_STATUS_KEY;
1550         key.offset = 0;
1551         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1552         if (ret) {
1553                 error("Could not find qgroup status item\n");
1554                 if (ret > 0)
1555                         ret = -ENOENT;
1556                 goto out;
1557         }
1558
1559         status_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1560                                      struct btrfs_qgroup_status_item);
1561         btrfs_set_qgroup_status_flags(path->nodes[0], status_item,
1562                                       BTRFS_QGROUP_STATUS_FLAG_ON);
1563         btrfs_set_qgroup_status_rescan(path->nodes[0], status_item, 0);
1564         btrfs_set_qgroup_status_generation(path->nodes[0], status_item,
1565                                            trans->transid);
1566
1567         btrfs_mark_buffer_dirty(path->nodes[0]);
1568
1569 out:
1570         btrfs_commit_transaction(trans, root);
1571         btrfs_free_path(path);
1572
1573         return ret;
1574 }
1575
1576 int repair_qgroups(struct btrfs_fs_info *info, int *repaired)
1577 {
1578         int ret;
1579         struct qgroup_count *count, *tmpcount;
1580
1581         *repaired = 0;
1582
1583         if (!repair)
1584                 return 0;
1585
1586         list_for_each_entry_safe(count, tmpcount, &bad_qgroups, bad_list) {
1587                 ret = repair_qgroup_info(info, count);
1588                 if (ret) {
1589                         goto out;
1590                 }
1591
1592                 (*repaired)++;
1593
1594                 list_del_init(&count->bad_list);
1595         }
1596
1597         /*
1598          * Do this step last as we want the latest transaction id on
1599          * our qgroup status to avoid a (useless) warning after
1600          * mount.
1601          */
1602         if (*repaired || counts.qgroup_inconsist || counts.rescan_running) {
1603                 ret = repair_qgroup_status(info);
1604                 if (ret)
1605                         goto out;
1606
1607                 (*repaired)++;
1608         }
1609
1610 out:
1611         return ret;
1612 }