Btrfs-progs: create missing root dirid
[platform/upstream/btrfs-progs.git] / cmds-check.c
1 /*
2  * Copyright (C) 2007 Oracle.  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
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <uuid/uuid.h>
30 #include "ctree.h"
31 #include "volumes.h"
32 #include "repair.h"
33 #include "disk-io.h"
34 #include "print-tree.h"
35 #include "transaction.h"
36 #include "version.h"
37 #include "utils.h"
38 #include "commands.h"
39 #include "free-space-cache.h"
40 #include "btrfsck.h"
41 #include "qgroup-verify.h"
42 #include "rbtree-utils.h"
43 #include "backref.h"
44 #include "ulist.h"
45
46 static u64 bytes_used = 0;
47 static u64 total_csum_bytes = 0;
48 static u64 total_btree_bytes = 0;
49 static u64 total_fs_tree_bytes = 0;
50 static u64 total_extent_tree_bytes = 0;
51 static u64 btree_space_waste = 0;
52 static u64 data_bytes_allocated = 0;
53 static u64 data_bytes_referenced = 0;
54 static int found_old_backref = 0;
55 static LIST_HEAD(duplicate_extents);
56 static LIST_HEAD(delete_items);
57 static int repair = 0;
58 static int no_holes = 0;
59 static int init_extent_tree = 0;
60 static int check_data_csum = 0;
61
62 struct extent_backref {
63         struct list_head list;
64         unsigned int is_data:1;
65         unsigned int found_extent_tree:1;
66         unsigned int full_backref:1;
67         unsigned int found_ref:1;
68         unsigned int broken:1;
69 };
70
71 struct data_backref {
72         struct extent_backref node;
73         union {
74                 u64 parent;
75                 u64 root;
76         };
77         u64 owner;
78         u64 offset;
79         u64 disk_bytenr;
80         u64 bytes;
81         u64 ram_bytes;
82         u32 num_refs;
83         u32 found_ref;
84 };
85
86 struct tree_backref {
87         struct extent_backref node;
88         union {
89                 u64 parent;
90                 u64 root;
91         };
92 };
93
94 struct extent_record {
95         struct list_head backrefs;
96         struct list_head dups;
97         struct list_head list;
98         struct cache_extent cache;
99         struct btrfs_disk_key parent_key;
100         u64 start;
101         u64 max_size;
102         u64 nr;
103         u64 refs;
104         u64 extent_item_refs;
105         u64 generation;
106         u64 parent_generation;
107         u64 info_objectid;
108         u32 num_duplicates;
109         u8 info_level;
110         unsigned int found_rec:1;
111         unsigned int content_checked:1;
112         unsigned int owner_ref_checked:1;
113         unsigned int is_root:1;
114         unsigned int metadata:1;
115 };
116
117 struct inode_backref {
118         struct list_head list;
119         unsigned int found_dir_item:1;
120         unsigned int found_dir_index:1;
121         unsigned int found_inode_ref:1;
122         unsigned int filetype:8;
123         int errors;
124         unsigned int ref_type;
125         u64 dir;
126         u64 index;
127         u16 namelen;
128         char name[0];
129 };
130
131 struct dropping_root_item_record {
132         struct list_head list;
133         struct btrfs_root_item ri;
134         struct btrfs_key found_key;
135 };
136
137 #define REF_ERR_NO_DIR_ITEM             (1 << 0)
138 #define REF_ERR_NO_DIR_INDEX            (1 << 1)
139 #define REF_ERR_NO_INODE_REF            (1 << 2)
140 #define REF_ERR_DUP_DIR_ITEM            (1 << 3)
141 #define REF_ERR_DUP_DIR_INDEX           (1 << 4)
142 #define REF_ERR_DUP_INODE_REF           (1 << 5)
143 #define REF_ERR_INDEX_UNMATCH           (1 << 6)
144 #define REF_ERR_FILETYPE_UNMATCH        (1 << 7)
145 #define REF_ERR_NAME_TOO_LONG           (1 << 8) // 100
146 #define REF_ERR_NO_ROOT_REF             (1 << 9)
147 #define REF_ERR_NO_ROOT_BACKREF         (1 << 10)
148 #define REF_ERR_DUP_ROOT_REF            (1 << 11)
149 #define REF_ERR_DUP_ROOT_BACKREF        (1 << 12)
150
151 struct inode_record {
152         struct list_head backrefs;
153         unsigned int checked:1;
154         unsigned int merging:1;
155         unsigned int found_inode_item:1;
156         unsigned int found_dir_item:1;
157         unsigned int found_file_extent:1;
158         unsigned int found_csum_item:1;
159         unsigned int some_csum_missing:1;
160         unsigned int nodatasum:1;
161         int errors;
162
163         u64 ino;
164         u32 nlink;
165         u32 imode;
166         u64 isize;
167         u64 nbytes;
168
169         u32 found_link;
170         u64 found_size;
171         u64 extent_start;
172         u64 extent_end;
173         u64 first_extent_gap;
174
175         u32 refs;
176 };
177
178 #define I_ERR_NO_INODE_ITEM             (1 << 0)
179 #define I_ERR_NO_ORPHAN_ITEM            (1 << 1)
180 #define I_ERR_DUP_INODE_ITEM            (1 << 2)
181 #define I_ERR_DUP_DIR_INDEX             (1 << 3)
182 #define I_ERR_ODD_DIR_ITEM              (1 << 4)
183 #define I_ERR_ODD_FILE_EXTENT           (1 << 5)
184 #define I_ERR_BAD_FILE_EXTENT           (1 << 6)
185 #define I_ERR_FILE_EXTENT_OVERLAP       (1 << 7)
186 #define I_ERR_FILE_EXTENT_DISCOUNT      (1 << 8) // 100
187 #define I_ERR_DIR_ISIZE_WRONG           (1 << 9)
188 #define I_ERR_FILE_NBYTES_WRONG         (1 << 10) // 400
189 #define I_ERR_ODD_CSUM_ITEM             (1 << 11)
190 #define I_ERR_SOME_CSUM_MISSING         (1 << 12)
191 #define I_ERR_LINK_COUNT_WRONG          (1 << 13)
192
193 struct root_backref {
194         struct list_head list;
195         unsigned int found_dir_item:1;
196         unsigned int found_dir_index:1;
197         unsigned int found_back_ref:1;
198         unsigned int found_forward_ref:1;
199         unsigned int reachable:1;
200         int errors;
201         u64 ref_root;
202         u64 dir;
203         u64 index;
204         u16 namelen;
205         char name[0];
206 };
207
208 struct root_record {
209         struct list_head backrefs;
210         struct cache_extent cache;
211         unsigned int found_root_item:1;
212         u64 objectid;
213         u32 found_ref;
214 };
215
216 struct ptr_node {
217         struct cache_extent cache;
218         void *data;
219 };
220
221 struct shared_node {
222         struct cache_extent cache;
223         struct cache_tree root_cache;
224         struct cache_tree inode_cache;
225         struct inode_record *current;
226         u32 refs;
227 };
228
229 struct block_info {
230         u64 start;
231         u32 size;
232 };
233
234 struct walk_control {
235         struct cache_tree shared;
236         struct shared_node *nodes[BTRFS_MAX_LEVEL];
237         int active_node;
238         int root_level;
239 };
240
241 struct bad_item {
242         struct btrfs_key key;
243         u64 root_id;
244         struct list_head list;
245 };
246
247 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
248
249 static void record_root_in_trans(struct btrfs_trans_handle *trans,
250                                  struct btrfs_root *root)
251 {
252         if (root->last_trans != trans->transid) {
253                 root->track_dirty = 1;
254                 root->last_trans = trans->transid;
255                 root->commit_root = root->node;
256                 extent_buffer_get(root->node);
257         }
258 }
259
260 static u8 imode_to_type(u32 imode)
261 {
262 #define S_SHIFT 12
263         static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
264                 [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
265                 [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
266                 [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
267                 [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
268                 [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
269                 [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
270                 [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
271         };
272
273         return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
274 #undef S_SHIFT
275 }
276
277 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
278 {
279         struct device_record *rec1;
280         struct device_record *rec2;
281
282         rec1 = rb_entry(node1, struct device_record, node);
283         rec2 = rb_entry(node2, struct device_record, node);
284         if (rec1->devid > rec2->devid)
285                 return -1;
286         else if (rec1->devid < rec2->devid)
287                 return 1;
288         else
289                 return 0;
290 }
291
292 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
293 {
294         struct inode_record *rec;
295         struct inode_backref *backref;
296         struct inode_backref *orig;
297         size_t size;
298
299         rec = malloc(sizeof(*rec));
300         memcpy(rec, orig_rec, sizeof(*rec));
301         rec->refs = 1;
302         INIT_LIST_HEAD(&rec->backrefs);
303
304         list_for_each_entry(orig, &orig_rec->backrefs, list) {
305                 size = sizeof(*orig) + orig->namelen + 1;
306                 backref = malloc(size);
307                 memcpy(backref, orig, size);
308                 list_add_tail(&backref->list, &rec->backrefs);
309         }
310         return rec;
311 }
312
313 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
314 {
315         u64 root_objectid = root->root_key.objectid;
316         int errors = rec->errors;
317
318         if (!errors)
319                 return;
320         /* reloc root errors, we print its corresponding fs root objectid*/
321         if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
322                 root_objectid = root->root_key.offset;
323                 fprintf(stderr, "reloc");
324         }
325         fprintf(stderr, "root %llu inode %llu errors %x",
326                 (unsigned long long) root_objectid,
327                 (unsigned long long) rec->ino, rec->errors);
328
329         if (errors & I_ERR_NO_INODE_ITEM)
330                 fprintf(stderr, ", no inode item");
331         if (errors & I_ERR_NO_ORPHAN_ITEM)
332                 fprintf(stderr, ", no orphan item");
333         if (errors & I_ERR_DUP_INODE_ITEM)
334                 fprintf(stderr, ", dup inode item");
335         if (errors & I_ERR_DUP_DIR_INDEX)
336                 fprintf(stderr, ", dup dir index");
337         if (errors & I_ERR_ODD_DIR_ITEM)
338                 fprintf(stderr, ", odd dir item");
339         if (errors & I_ERR_ODD_FILE_EXTENT)
340                 fprintf(stderr, ", odd file extent");
341         if (errors & I_ERR_BAD_FILE_EXTENT)
342                 fprintf(stderr, ", bad file extent");
343         if (errors & I_ERR_FILE_EXTENT_OVERLAP)
344                 fprintf(stderr, ", file extent overlap");
345         if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
346                 fprintf(stderr, ", file extent discount");
347         if (errors & I_ERR_DIR_ISIZE_WRONG)
348                 fprintf(stderr, ", dir isize wrong");
349         if (errors & I_ERR_FILE_NBYTES_WRONG)
350                 fprintf(stderr, ", nbytes wrong");
351         if (errors & I_ERR_ODD_CSUM_ITEM)
352                 fprintf(stderr, ", odd csum item");
353         if (errors & I_ERR_SOME_CSUM_MISSING)
354                 fprintf(stderr, ", some csum missing");
355         if (errors & I_ERR_LINK_COUNT_WRONG)
356                 fprintf(stderr, ", link count wrong");
357         fprintf(stderr, "\n");
358 }
359
360 static void print_ref_error(int errors)
361 {
362         if (errors & REF_ERR_NO_DIR_ITEM)
363                 fprintf(stderr, ", no dir item");
364         if (errors & REF_ERR_NO_DIR_INDEX)
365                 fprintf(stderr, ", no dir index");
366         if (errors & REF_ERR_NO_INODE_REF)
367                 fprintf(stderr, ", no inode ref");
368         if (errors & REF_ERR_DUP_DIR_ITEM)
369                 fprintf(stderr, ", dup dir item");
370         if (errors & REF_ERR_DUP_DIR_INDEX)
371                 fprintf(stderr, ", dup dir index");
372         if (errors & REF_ERR_DUP_INODE_REF)
373                 fprintf(stderr, ", dup inode ref");
374         if (errors & REF_ERR_INDEX_UNMATCH)
375                 fprintf(stderr, ", index unmatch");
376         if (errors & REF_ERR_FILETYPE_UNMATCH)
377                 fprintf(stderr, ", filetype unmatch");
378         if (errors & REF_ERR_NAME_TOO_LONG)
379                 fprintf(stderr, ", name too long");
380         if (errors & REF_ERR_NO_ROOT_REF)
381                 fprintf(stderr, ", no root ref");
382         if (errors & REF_ERR_NO_ROOT_BACKREF)
383                 fprintf(stderr, ", no root backref");
384         if (errors & REF_ERR_DUP_ROOT_REF)
385                 fprintf(stderr, ", dup root ref");
386         if (errors & REF_ERR_DUP_ROOT_BACKREF)
387                 fprintf(stderr, ", dup root backref");
388         fprintf(stderr, "\n");
389 }
390
391 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
392                                           u64 ino, int mod)
393 {
394         struct ptr_node *node;
395         struct cache_extent *cache;
396         struct inode_record *rec = NULL;
397         int ret;
398
399         cache = lookup_cache_extent(inode_cache, ino, 1);
400         if (cache) {
401                 node = container_of(cache, struct ptr_node, cache);
402                 rec = node->data;
403                 if (mod && rec->refs > 1) {
404                         node->data = clone_inode_rec(rec);
405                         rec->refs--;
406                         rec = node->data;
407                 }
408         } else if (mod) {
409                 rec = calloc(1, sizeof(*rec));
410                 rec->ino = ino;
411                 rec->extent_start = (u64)-1;
412                 rec->first_extent_gap = (u64)-1;
413                 rec->refs = 1;
414                 INIT_LIST_HEAD(&rec->backrefs);
415
416                 node = malloc(sizeof(*node));
417                 node->cache.start = ino;
418                 node->cache.size = 1;
419                 node->data = rec;
420
421                 if (ino == BTRFS_FREE_INO_OBJECTID)
422                         rec->found_link = 1;
423
424                 ret = insert_cache_extent(inode_cache, &node->cache);
425                 BUG_ON(ret);
426         }
427         return rec;
428 }
429
430 static void free_inode_rec(struct inode_record *rec)
431 {
432         struct inode_backref *backref;
433
434         if (--rec->refs > 0)
435                 return;
436
437         while (!list_empty(&rec->backrefs)) {
438                 backref = list_entry(rec->backrefs.next,
439                                      struct inode_backref, list);
440                 list_del(&backref->list);
441                 free(backref);
442         }
443         free(rec);
444 }
445
446 static int can_free_inode_rec(struct inode_record *rec)
447 {
448         if (!rec->errors && rec->checked && rec->found_inode_item &&
449             rec->nlink == rec->found_link && list_empty(&rec->backrefs))
450                 return 1;
451         return 0;
452 }
453
454 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
455                                  struct inode_record *rec)
456 {
457         struct cache_extent *cache;
458         struct inode_backref *tmp, *backref;
459         struct ptr_node *node;
460         unsigned char filetype;
461
462         if (!rec->found_inode_item)
463                 return;
464
465         filetype = imode_to_type(rec->imode);
466         list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
467                 if (backref->found_dir_item && backref->found_dir_index) {
468                         if (backref->filetype != filetype)
469                                 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
470                         if (!backref->errors && backref->found_inode_ref) {
471                                 list_del(&backref->list);
472                                 free(backref);
473                         }
474                 }
475         }
476
477         if (!rec->checked || rec->merging)
478                 return;
479
480         if (S_ISDIR(rec->imode)) {
481                 if (rec->found_size != rec->isize)
482                         rec->errors |= I_ERR_DIR_ISIZE_WRONG;
483                 if (rec->found_file_extent)
484                         rec->errors |= I_ERR_ODD_FILE_EXTENT;
485         } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
486                 if (rec->found_dir_item)
487                         rec->errors |= I_ERR_ODD_DIR_ITEM;
488                 if (rec->found_size != rec->nbytes)
489                         rec->errors |= I_ERR_FILE_NBYTES_WRONG;
490                 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
491                         rec->first_extent_gap = 0;
492                 if (rec->nlink > 0 && !no_holes &&
493                     (rec->extent_end < rec->isize ||
494                      rec->first_extent_gap < rec->isize))
495                         rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
496         }
497
498         if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
499                 if (rec->found_csum_item && rec->nodatasum)
500                         rec->errors |= I_ERR_ODD_CSUM_ITEM;
501                 if (rec->some_csum_missing && !rec->nodatasum)
502                         rec->errors |= I_ERR_SOME_CSUM_MISSING;
503         }
504
505         BUG_ON(rec->refs != 1);
506         if (can_free_inode_rec(rec)) {
507                 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
508                 node = container_of(cache, struct ptr_node, cache);
509                 BUG_ON(node->data != rec);
510                 remove_cache_extent(inode_cache, &node->cache);
511                 free(node);
512                 free_inode_rec(rec);
513         }
514 }
515
516 static int check_orphan_item(struct btrfs_root *root, u64 ino)
517 {
518         struct btrfs_path path;
519         struct btrfs_key key;
520         int ret;
521
522         key.objectid = BTRFS_ORPHAN_OBJECTID;
523         key.type = BTRFS_ORPHAN_ITEM_KEY;
524         key.offset = ino;
525
526         btrfs_init_path(&path);
527         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
528         btrfs_release_path(&path);
529         if (ret > 0)
530                 ret = -ENOENT;
531         return ret;
532 }
533
534 static int process_inode_item(struct extent_buffer *eb,
535                               int slot, struct btrfs_key *key,
536                               struct shared_node *active_node)
537 {
538         struct inode_record *rec;
539         struct btrfs_inode_item *item;
540
541         rec = active_node->current;
542         BUG_ON(rec->ino != key->objectid || rec->refs > 1);
543         if (rec->found_inode_item) {
544                 rec->errors |= I_ERR_DUP_INODE_ITEM;
545                 return 1;
546         }
547         item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
548         rec->nlink = btrfs_inode_nlink(eb, item);
549         rec->isize = btrfs_inode_size(eb, item);
550         rec->nbytes = btrfs_inode_nbytes(eb, item);
551         rec->imode = btrfs_inode_mode(eb, item);
552         if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
553                 rec->nodatasum = 1;
554         rec->found_inode_item = 1;
555         if (rec->nlink == 0)
556                 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
557         maybe_free_inode_rec(&active_node->inode_cache, rec);
558         return 0;
559 }
560
561 static struct inode_backref *get_inode_backref(struct inode_record *rec,
562                                                 const char *name,
563                                                 int namelen, u64 dir)
564 {
565         struct inode_backref *backref;
566
567         list_for_each_entry(backref, &rec->backrefs, list) {
568                 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
569                         break;
570                 if (backref->dir != dir || backref->namelen != namelen)
571                         continue;
572                 if (memcmp(name, backref->name, namelen))
573                         continue;
574                 return backref;
575         }
576
577         backref = malloc(sizeof(*backref) + namelen + 1);
578         memset(backref, 0, sizeof(*backref));
579         backref->dir = dir;
580         backref->namelen = namelen;
581         memcpy(backref->name, name, namelen);
582         backref->name[namelen] = '\0';
583         list_add_tail(&backref->list, &rec->backrefs);
584         return backref;
585 }
586
587 static int add_inode_backref(struct cache_tree *inode_cache,
588                              u64 ino, u64 dir, u64 index,
589                              const char *name, int namelen,
590                              int filetype, int itemtype, int errors)
591 {
592         struct inode_record *rec;
593         struct inode_backref *backref;
594
595         rec = get_inode_rec(inode_cache, ino, 1);
596         backref = get_inode_backref(rec, name, namelen, dir);
597         if (errors)
598                 backref->errors |= errors;
599         if (itemtype == BTRFS_DIR_INDEX_KEY) {
600                 if (backref->found_dir_index)
601                         backref->errors |= REF_ERR_DUP_DIR_INDEX;
602                 if (backref->found_inode_ref && backref->index != index)
603                         backref->errors |= REF_ERR_INDEX_UNMATCH;
604                 if (backref->found_dir_item && backref->filetype != filetype)
605                         backref->errors |= REF_ERR_FILETYPE_UNMATCH;
606
607                 backref->index = index;
608                 backref->filetype = filetype;
609                 backref->found_dir_index = 1;
610         } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
611                 rec->found_link++;
612                 if (backref->found_dir_item)
613                         backref->errors |= REF_ERR_DUP_DIR_ITEM;
614                 if (backref->found_dir_index && backref->filetype != filetype)
615                         backref->errors |= REF_ERR_FILETYPE_UNMATCH;
616
617                 backref->filetype = filetype;
618                 backref->found_dir_item = 1;
619         } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
620                    (itemtype == BTRFS_INODE_EXTREF_KEY)) {
621                 if (backref->found_inode_ref)
622                         backref->errors |= REF_ERR_DUP_INODE_REF;
623                 if (backref->found_dir_index && backref->index != index)
624                         backref->errors |= REF_ERR_INDEX_UNMATCH;
625                 else
626                         backref->index = index;
627
628                 backref->ref_type = itemtype;
629                 backref->found_inode_ref = 1;
630         } else {
631                 BUG_ON(1);
632         }
633
634         maybe_free_inode_rec(inode_cache, rec);
635         return 0;
636 }
637
638 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
639                             struct cache_tree *dst_cache)
640 {
641         struct inode_backref *backref;
642         u32 dir_count = 0;
643
644         dst->merging = 1;
645         list_for_each_entry(backref, &src->backrefs, list) {
646                 if (backref->found_dir_index) {
647                         add_inode_backref(dst_cache, dst->ino, backref->dir,
648                                         backref->index, backref->name,
649                                         backref->namelen, backref->filetype,
650                                         BTRFS_DIR_INDEX_KEY, backref->errors);
651                 }
652                 if (backref->found_dir_item) {
653                         dir_count++;
654                         add_inode_backref(dst_cache, dst->ino,
655                                         backref->dir, 0, backref->name,
656                                         backref->namelen, backref->filetype,
657                                         BTRFS_DIR_ITEM_KEY, backref->errors);
658                 }
659                 if (backref->found_inode_ref) {
660                         add_inode_backref(dst_cache, dst->ino,
661                                         backref->dir, backref->index,
662                                         backref->name, backref->namelen, 0,
663                                         backref->ref_type, backref->errors);
664                 }
665         }
666
667         if (src->found_dir_item)
668                 dst->found_dir_item = 1;
669         if (src->found_file_extent)
670                 dst->found_file_extent = 1;
671         if (src->found_csum_item)
672                 dst->found_csum_item = 1;
673         if (src->some_csum_missing)
674                 dst->some_csum_missing = 1;
675         if (dst->first_extent_gap > src->first_extent_gap)
676                 dst->first_extent_gap = src->first_extent_gap;
677
678         BUG_ON(src->found_link < dir_count);
679         dst->found_link += src->found_link - dir_count;
680         dst->found_size += src->found_size;
681         if (src->extent_start != (u64)-1) {
682                 if (dst->extent_start == (u64)-1) {
683                         dst->extent_start = src->extent_start;
684                         dst->extent_end = src->extent_end;
685                 } else {
686                         if (dst->extent_end > src->extent_start)
687                                 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
688                         else if (dst->extent_end < src->extent_start &&
689                                  dst->extent_end < dst->first_extent_gap)
690                                 dst->first_extent_gap = dst->extent_end;
691                         if (dst->extent_end < src->extent_end)
692                                 dst->extent_end = src->extent_end;
693                 }
694         }
695
696         dst->errors |= src->errors;
697         if (src->found_inode_item) {
698                 if (!dst->found_inode_item) {
699                         dst->nlink = src->nlink;
700                         dst->isize = src->isize;
701                         dst->nbytes = src->nbytes;
702                         dst->imode = src->imode;
703                         dst->nodatasum = src->nodatasum;
704                         dst->found_inode_item = 1;
705                 } else {
706                         dst->errors |= I_ERR_DUP_INODE_ITEM;
707                 }
708         }
709         dst->merging = 0;
710
711         return 0;
712 }
713
714 static int splice_shared_node(struct shared_node *src_node,
715                               struct shared_node *dst_node)
716 {
717         struct cache_extent *cache;
718         struct ptr_node *node, *ins;
719         struct cache_tree *src, *dst;
720         struct inode_record *rec, *conflict;
721         u64 current_ino = 0;
722         int splice = 0;
723         int ret;
724
725         if (--src_node->refs == 0)
726                 splice = 1;
727         if (src_node->current)
728                 current_ino = src_node->current->ino;
729
730         src = &src_node->root_cache;
731         dst = &dst_node->root_cache;
732 again:
733         cache = search_cache_extent(src, 0);
734         while (cache) {
735                 node = container_of(cache, struct ptr_node, cache);
736                 rec = node->data;
737                 cache = next_cache_extent(cache);
738
739                 if (splice) {
740                         remove_cache_extent(src, &node->cache);
741                         ins = node;
742                 } else {
743                         ins = malloc(sizeof(*ins));
744                         ins->cache.start = node->cache.start;
745                         ins->cache.size = node->cache.size;
746                         ins->data = rec;
747                         rec->refs++;
748                 }
749                 ret = insert_cache_extent(dst, &ins->cache);
750                 if (ret == -EEXIST) {
751                         conflict = get_inode_rec(dst, rec->ino, 1);
752                         merge_inode_recs(rec, conflict, dst);
753                         if (rec->checked) {
754                                 conflict->checked = 1;
755                                 if (dst_node->current == conflict)
756                                         dst_node->current = NULL;
757                         }
758                         maybe_free_inode_rec(dst, conflict);
759                         free_inode_rec(rec);
760                         free(ins);
761                 } else {
762                         BUG_ON(ret);
763                 }
764         }
765
766         if (src == &src_node->root_cache) {
767                 src = &src_node->inode_cache;
768                 dst = &dst_node->inode_cache;
769                 goto again;
770         }
771
772         if (current_ino > 0 && (!dst_node->current ||
773             current_ino > dst_node->current->ino)) {
774                 if (dst_node->current) {
775                         dst_node->current->checked = 1;
776                         maybe_free_inode_rec(dst, dst_node->current);
777                 }
778                 dst_node->current = get_inode_rec(dst, current_ino, 1);
779         }
780         return 0;
781 }
782
783 static void free_inode_ptr(struct cache_extent *cache)
784 {
785         struct ptr_node *node;
786         struct inode_record *rec;
787
788         node = container_of(cache, struct ptr_node, cache);
789         rec = node->data;
790         free_inode_rec(rec);
791         free(node);
792 }
793
794 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
795
796 static struct shared_node *find_shared_node(struct cache_tree *shared,
797                                             u64 bytenr)
798 {
799         struct cache_extent *cache;
800         struct shared_node *node;
801
802         cache = lookup_cache_extent(shared, bytenr, 1);
803         if (cache) {
804                 node = container_of(cache, struct shared_node, cache);
805                 return node;
806         }
807         return NULL;
808 }
809
810 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
811 {
812         int ret;
813         struct shared_node *node;
814
815         node = calloc(1, sizeof(*node));
816         node->cache.start = bytenr;
817         node->cache.size = 1;
818         cache_tree_init(&node->root_cache);
819         cache_tree_init(&node->inode_cache);
820         node->refs = refs;
821
822         ret = insert_cache_extent(shared, &node->cache);
823         BUG_ON(ret);
824         return 0;
825 }
826
827 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
828                              struct walk_control *wc, int level)
829 {
830         struct shared_node *node;
831         struct shared_node *dest;
832
833         if (level == wc->active_node)
834                 return 0;
835
836         BUG_ON(wc->active_node <= level);
837         node = find_shared_node(&wc->shared, bytenr);
838         if (!node) {
839                 add_shared_node(&wc->shared, bytenr, refs);
840                 node = find_shared_node(&wc->shared, bytenr);
841                 wc->nodes[level] = node;
842                 wc->active_node = level;
843                 return 0;
844         }
845
846         if (wc->root_level == wc->active_node &&
847             btrfs_root_refs(&root->root_item) == 0) {
848                 if (--node->refs == 0) {
849                         free_inode_recs_tree(&node->root_cache);
850                         free_inode_recs_tree(&node->inode_cache);
851                         remove_cache_extent(&wc->shared, &node->cache);
852                         free(node);
853                 }
854                 return 1;
855         }
856
857         dest = wc->nodes[wc->active_node];
858         splice_shared_node(node, dest);
859         if (node->refs == 0) {
860                 remove_cache_extent(&wc->shared, &node->cache);
861                 free(node);
862         }
863         return 1;
864 }
865
866 static int leave_shared_node(struct btrfs_root *root,
867                              struct walk_control *wc, int level)
868 {
869         struct shared_node *node;
870         struct shared_node *dest;
871         int i;
872
873         if (level == wc->root_level)
874                 return 0;
875
876         for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
877                 if (wc->nodes[i])
878                         break;
879         }
880         BUG_ON(i >= BTRFS_MAX_LEVEL);
881
882         node = wc->nodes[wc->active_node];
883         wc->nodes[wc->active_node] = NULL;
884         wc->active_node = i;
885
886         dest = wc->nodes[wc->active_node];
887         if (wc->active_node < wc->root_level ||
888             btrfs_root_refs(&root->root_item) > 0) {
889                 BUG_ON(node->refs <= 1);
890                 splice_shared_node(node, dest);
891         } else {
892                 BUG_ON(node->refs < 2);
893                 node->refs--;
894         }
895         return 0;
896 }
897
898 /*
899  * Returns:
900  * < 0 - on error
901  * 1   - if the root with id child_root_id is a child of root parent_root_id
902  * 0   - if the root child_root_id isn't a child of the root parent_root_id but
903  *       has other root(s) as parent(s)
904  * 2   - if the root child_root_id doesn't have any parent roots
905  */
906 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
907                          u64 child_root_id)
908 {
909         struct btrfs_path path;
910         struct btrfs_key key;
911         struct extent_buffer *leaf;
912         int has_parent = 0;
913         int ret;
914
915         btrfs_init_path(&path);
916
917         key.objectid = parent_root_id;
918         key.type = BTRFS_ROOT_REF_KEY;
919         key.offset = child_root_id;
920         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
921                                 0, 0);
922         if (ret < 0)
923                 return ret;
924         btrfs_release_path(&path);
925         if (!ret)
926                 return 1;
927
928         key.objectid = child_root_id;
929         key.type = BTRFS_ROOT_BACKREF_KEY;
930         key.offset = 0;
931         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
932                                 0, 0);
933         if (ret < 0)
934                 goto out;
935
936         while (1) {
937                 leaf = path.nodes[0];
938                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
939                         ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
940                         if (ret)
941                                 break;
942                         leaf = path.nodes[0];
943                 }
944
945                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
946                 if (key.objectid != child_root_id ||
947                     key.type != BTRFS_ROOT_BACKREF_KEY)
948                         break;
949
950                 has_parent = 1;
951
952                 if (key.offset == parent_root_id) {
953                         btrfs_release_path(&path);
954                         return 1;
955                 }
956
957                 path.slots[0]++;
958         }
959 out:
960         btrfs_release_path(&path);
961         if (ret < 0)
962                 return ret;
963         return has_parent ? 0 : 2;
964 }
965
966 static int process_dir_item(struct btrfs_root *root,
967                             struct extent_buffer *eb,
968                             int slot, struct btrfs_key *key,
969                             struct shared_node *active_node)
970 {
971         u32 total;
972         u32 cur = 0;
973         u32 len;
974         u32 name_len;
975         u32 data_len;
976         int error;
977         int nritems = 0;
978         int filetype;
979         struct btrfs_dir_item *di;
980         struct inode_record *rec;
981         struct cache_tree *root_cache;
982         struct cache_tree *inode_cache;
983         struct btrfs_key location;
984         char namebuf[BTRFS_NAME_LEN];
985
986         root_cache = &active_node->root_cache;
987         inode_cache = &active_node->inode_cache;
988         rec = active_node->current;
989         rec->found_dir_item = 1;
990
991         di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
992         total = btrfs_item_size_nr(eb, slot);
993         while (cur < total) {
994                 nritems++;
995                 btrfs_dir_item_key_to_cpu(eb, di, &location);
996                 name_len = btrfs_dir_name_len(eb, di);
997                 data_len = btrfs_dir_data_len(eb, di);
998                 filetype = btrfs_dir_type(eb, di);
999
1000                 rec->found_size += name_len;
1001                 if (name_len <= BTRFS_NAME_LEN) {
1002                         len = name_len;
1003                         error = 0;
1004                 } else {
1005                         len = BTRFS_NAME_LEN;
1006                         error = REF_ERR_NAME_TOO_LONG;
1007                 }
1008                 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
1009
1010                 if (location.type == BTRFS_INODE_ITEM_KEY) {
1011                         add_inode_backref(inode_cache, location.objectid,
1012                                           key->objectid, key->offset, namebuf,
1013                                           len, filetype, key->type, error);
1014                 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
1015                         add_inode_backref(root_cache, location.objectid,
1016                                           key->objectid, key->offset,
1017                                           namebuf, len, filetype,
1018                                           key->type, error);
1019                 } else {
1020                         fprintf(stderr, "invalid location in dir item %u\n",
1021                                 location.type);
1022                         add_inode_backref(inode_cache, BTRFS_MULTIPLE_OBJECTIDS,
1023                                           key->objectid, key->offset, namebuf,
1024                                           len, filetype, key->type, error);
1025                 }
1026
1027                 len = sizeof(*di) + name_len + data_len;
1028                 di = (struct btrfs_dir_item *)((char *)di + len);
1029                 cur += len;
1030         }
1031         if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1032                 rec->errors |= I_ERR_DUP_DIR_INDEX;
1033
1034         return 0;
1035 }
1036
1037 static int process_inode_ref(struct extent_buffer *eb,
1038                              int slot, struct btrfs_key *key,
1039                              struct shared_node *active_node)
1040 {
1041         u32 total;
1042         u32 cur = 0;
1043         u32 len;
1044         u32 name_len;
1045         u64 index;
1046         int error;
1047         struct cache_tree *inode_cache;
1048         struct btrfs_inode_ref *ref;
1049         char namebuf[BTRFS_NAME_LEN];
1050
1051         inode_cache = &active_node->inode_cache;
1052
1053         ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1054         total = btrfs_item_size_nr(eb, slot);
1055         while (cur < total) {
1056                 name_len = btrfs_inode_ref_name_len(eb, ref);
1057                 index = btrfs_inode_ref_index(eb, ref);
1058                 if (name_len <= BTRFS_NAME_LEN) {
1059                         len = name_len;
1060                         error = 0;
1061                 } else {
1062                         len = BTRFS_NAME_LEN;
1063                         error = REF_ERR_NAME_TOO_LONG;
1064                 }
1065                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1066                 add_inode_backref(inode_cache, key->objectid, key->offset,
1067                                   index, namebuf, len, 0, key->type, error);
1068
1069                 len = sizeof(*ref) + name_len;
1070                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1071                 cur += len;
1072         }
1073         return 0;
1074 }
1075
1076 static int process_inode_extref(struct extent_buffer *eb,
1077                                 int slot, struct btrfs_key *key,
1078                                 struct shared_node *active_node)
1079 {
1080         u32 total;
1081         u32 cur = 0;
1082         u32 len;
1083         u32 name_len;
1084         u64 index;
1085         u64 parent;
1086         int error;
1087         struct cache_tree *inode_cache;
1088         struct btrfs_inode_extref *extref;
1089         char namebuf[BTRFS_NAME_LEN];
1090
1091         inode_cache = &active_node->inode_cache;
1092
1093         extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1094         total = btrfs_item_size_nr(eb, slot);
1095         while (cur < total) {
1096                 name_len = btrfs_inode_extref_name_len(eb, extref);
1097                 index = btrfs_inode_extref_index(eb, extref);
1098                 parent = btrfs_inode_extref_parent(eb, extref);
1099                 if (name_len <= BTRFS_NAME_LEN) {
1100                         len = name_len;
1101                         error = 0;
1102                 } else {
1103                         len = BTRFS_NAME_LEN;
1104                         error = REF_ERR_NAME_TOO_LONG;
1105                 }
1106                 read_extent_buffer(eb, namebuf,
1107                                    (unsigned long)(extref + 1), len);
1108                 add_inode_backref(inode_cache, key->objectid, parent,
1109                                   index, namebuf, len, 0, key->type, error);
1110
1111                 len = sizeof(*extref) + name_len;
1112                 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1113                 cur += len;
1114         }
1115         return 0;
1116
1117 }
1118
1119 static int count_csum_range(struct btrfs_root *root, u64 start,
1120                             u64 len, u64 *found)
1121 {
1122         struct btrfs_key key;
1123         struct btrfs_path path;
1124         struct extent_buffer *leaf;
1125         int ret;
1126         size_t size;
1127         *found = 0;
1128         u64 csum_end;
1129         u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1130
1131         btrfs_init_path(&path);
1132
1133         key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1134         key.offset = start;
1135         key.type = BTRFS_EXTENT_CSUM_KEY;
1136
1137         ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1138                                 &key, &path, 0, 0);
1139         if (ret < 0)
1140                 goto out;
1141         if (ret > 0 && path.slots[0] > 0) {
1142                 leaf = path.nodes[0];
1143                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1144                 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1145                     key.type == BTRFS_EXTENT_CSUM_KEY)
1146                         path.slots[0]--;
1147         }
1148
1149         while (len > 0) {
1150                 leaf = path.nodes[0];
1151                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1152                         ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1153                         if (ret > 0)
1154                                 break;
1155                         else if (ret < 0)
1156                                 goto out;
1157                         leaf = path.nodes[0];
1158                 }
1159
1160                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1161                 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1162                     key.type != BTRFS_EXTENT_CSUM_KEY)
1163                         break;
1164
1165                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1166                 if (key.offset >= start + len)
1167                         break;
1168
1169                 if (key.offset > start)
1170                         start = key.offset;
1171
1172                 size = btrfs_item_size_nr(leaf, path.slots[0]);
1173                 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1174                 if (csum_end > start) {
1175                         size = min(csum_end - start, len);
1176                         len -= size;
1177                         start += size;
1178                         *found += size;
1179                 }
1180
1181                 path.slots[0]++;
1182         }
1183 out:
1184         if (ret < 0)
1185                 return ret;
1186         btrfs_release_path(&path);
1187         return 0;
1188 }
1189
1190 static int process_file_extent(struct btrfs_root *root,
1191                                 struct extent_buffer *eb,
1192                                 int slot, struct btrfs_key *key,
1193                                 struct shared_node *active_node)
1194 {
1195         struct inode_record *rec;
1196         struct btrfs_file_extent_item *fi;
1197         u64 num_bytes = 0;
1198         u64 disk_bytenr = 0;
1199         u64 extent_offset = 0;
1200         u64 mask = root->sectorsize - 1;
1201         int extent_type;
1202         int ret;
1203
1204         rec = active_node->current;
1205         BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1206         rec->found_file_extent = 1;
1207
1208         if (rec->extent_start == (u64)-1) {
1209                 rec->extent_start = key->offset;
1210                 rec->extent_end = key->offset;
1211         }
1212
1213         if (rec->extent_end > key->offset)
1214                 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1215         else if (rec->extent_end < key->offset &&
1216                  rec->extent_end < rec->first_extent_gap)
1217                 rec->first_extent_gap = rec->extent_end;
1218
1219         fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1220         extent_type = btrfs_file_extent_type(eb, fi);
1221
1222         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1223                 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1224                 if (num_bytes == 0)
1225                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1226                 rec->found_size += num_bytes;
1227                 num_bytes = (num_bytes + mask) & ~mask;
1228         } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1229                    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1230                 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1231                 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1232                 extent_offset = btrfs_file_extent_offset(eb, fi);
1233                 if (num_bytes == 0 || (num_bytes & mask))
1234                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1235                 if (num_bytes + extent_offset >
1236                     btrfs_file_extent_ram_bytes(eb, fi))
1237                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1238                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1239                     (btrfs_file_extent_compression(eb, fi) ||
1240                      btrfs_file_extent_encryption(eb, fi) ||
1241                      btrfs_file_extent_other_encoding(eb, fi)))
1242                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1243                 if (disk_bytenr > 0)
1244                         rec->found_size += num_bytes;
1245         } else {
1246                 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1247         }
1248         rec->extent_end = key->offset + num_bytes;
1249
1250         if (disk_bytenr > 0) {
1251                 u64 found;
1252                 if (btrfs_file_extent_compression(eb, fi))
1253                         num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1254                 else
1255                         disk_bytenr += extent_offset;
1256
1257                 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1258                 if (ret < 0)
1259                         return ret;
1260                 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1261                         if (found > 0)
1262                                 rec->found_csum_item = 1;
1263                         if (found < num_bytes)
1264                                 rec->some_csum_missing = 1;
1265                 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1266                         if (found > 0)
1267                                 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1268                 }
1269         }
1270         return 0;
1271 }
1272
1273 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1274                             struct walk_control *wc)
1275 {
1276         struct btrfs_key key;
1277         u32 nritems;
1278         int i;
1279         int ret = 0;
1280         struct cache_tree *inode_cache;
1281         struct shared_node *active_node;
1282
1283         if (wc->root_level == wc->active_node &&
1284             btrfs_root_refs(&root->root_item) == 0)
1285                 return 0;
1286
1287         active_node = wc->nodes[wc->active_node];
1288         inode_cache = &active_node->inode_cache;
1289         nritems = btrfs_header_nritems(eb);
1290         for (i = 0; i < nritems; i++) {
1291                 btrfs_item_key_to_cpu(eb, &key, i);
1292
1293                 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1294                         continue;
1295                 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1296                         continue;
1297
1298                 if (active_node->current == NULL ||
1299                     active_node->current->ino < key.objectid) {
1300                         if (active_node->current) {
1301                                 active_node->current->checked = 1;
1302                                 maybe_free_inode_rec(inode_cache,
1303                                                      active_node->current);
1304                         }
1305                         active_node->current = get_inode_rec(inode_cache,
1306                                                              key.objectid, 1);
1307                 }
1308                 switch (key.type) {
1309                 case BTRFS_DIR_ITEM_KEY:
1310                 case BTRFS_DIR_INDEX_KEY:
1311                         ret = process_dir_item(root, eb, i, &key, active_node);
1312                         break;
1313                 case BTRFS_INODE_REF_KEY:
1314                         ret = process_inode_ref(eb, i, &key, active_node);
1315                         break;
1316                 case BTRFS_INODE_EXTREF_KEY:
1317                         ret = process_inode_extref(eb, i, &key, active_node);
1318                         break;
1319                 case BTRFS_INODE_ITEM_KEY:
1320                         ret = process_inode_item(eb, i, &key, active_node);
1321                         break;
1322                 case BTRFS_EXTENT_DATA_KEY:
1323                         ret = process_file_extent(root, eb, i, &key,
1324                                                   active_node);
1325                         break;
1326                 default:
1327                         break;
1328                 };
1329         }
1330         return ret;
1331 }
1332
1333 static void reada_walk_down(struct btrfs_root *root,
1334                             struct extent_buffer *node, int slot)
1335 {
1336         u64 bytenr;
1337         u64 ptr_gen;
1338         u32 nritems;
1339         u32 blocksize;
1340         int i;
1341         int level;
1342
1343         level = btrfs_header_level(node);
1344         if (level != 1)
1345                 return;
1346
1347         nritems = btrfs_header_nritems(node);
1348         blocksize = btrfs_level_size(root, level - 1);
1349         for (i = slot; i < nritems; i++) {
1350                 bytenr = btrfs_node_blockptr(node, i);
1351                 ptr_gen = btrfs_node_ptr_generation(node, i);
1352                 readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1353         }
1354 }
1355
1356 /*
1357  * Check the child node/leaf by the following condition:
1358  * 1. the first item key of the node/leaf should be the same with the one
1359  *    in parent.
1360  * 2. block in parent node should match the child node/leaf.
1361  * 3. generation of parent node and child's header should be consistent.
1362  *
1363  * Or the child node/leaf pointed by the key in parent is not valid.
1364  *
1365  * We hope to check leaf owner too, but since subvol may share leaves,
1366  * which makes leaf owner check not so strong, key check should be
1367  * sufficient enough for that case.
1368  */
1369 static int check_child_node(struct btrfs_root *root,
1370                             struct extent_buffer *parent, int slot,
1371                             struct extent_buffer *child)
1372 {
1373         struct btrfs_key parent_key;
1374         struct btrfs_key child_key;
1375         int ret = 0;
1376
1377         btrfs_node_key_to_cpu(parent, &parent_key, slot);
1378         if (btrfs_header_level(child) == 0)
1379                 btrfs_item_key_to_cpu(child, &child_key, 0);
1380         else
1381                 btrfs_node_key_to_cpu(child, &child_key, 0);
1382
1383         if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
1384                 ret = -EINVAL;
1385                 fprintf(stderr,
1386                         "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
1387                         parent_key.objectid, parent_key.type, parent_key.offset,
1388                         child_key.objectid, child_key.type, child_key.offset);
1389         }
1390         if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
1391                 ret = -EINVAL;
1392                 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
1393                         btrfs_node_blockptr(parent, slot),
1394                         btrfs_header_bytenr(child));
1395         }
1396         if (btrfs_node_ptr_generation(parent, slot) !=
1397             btrfs_header_generation(child)) {
1398                 ret = -EINVAL;
1399                 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
1400                         btrfs_header_generation(child),
1401                         btrfs_node_ptr_generation(parent, slot));
1402         }
1403         return ret;
1404 }
1405
1406 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1407                           struct walk_control *wc, int *level)
1408 {
1409         enum btrfs_tree_block_status status;
1410         u64 bytenr;
1411         u64 ptr_gen;
1412         struct extent_buffer *next;
1413         struct extent_buffer *cur;
1414         u32 blocksize;
1415         int ret, err = 0;
1416         u64 refs;
1417
1418         WARN_ON(*level < 0);
1419         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1420         ret = btrfs_lookup_extent_info(NULL, root,
1421                                        path->nodes[*level]->start,
1422                                        *level, 1, &refs, NULL);
1423         if (ret < 0) {
1424                 err = ret;
1425                 goto out;
1426         }
1427
1428         if (refs > 1) {
1429                 ret = enter_shared_node(root, path->nodes[*level]->start,
1430                                         refs, wc, *level);
1431                 if (ret > 0) {
1432                         err = ret;
1433                         goto out;
1434                 }
1435         }
1436
1437         while (*level >= 0) {
1438                 WARN_ON(*level < 0);
1439                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1440                 cur = path->nodes[*level];
1441
1442                 if (btrfs_header_level(cur) != *level)
1443                         WARN_ON(1);
1444
1445                 if (path->slots[*level] >= btrfs_header_nritems(cur))
1446                         break;
1447                 if (*level == 0) {
1448                         ret = process_one_leaf(root, cur, wc);
1449                         if (ret < 0)
1450                                 err = ret;
1451                         break;
1452                 }
1453                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1454                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1455                 blocksize = btrfs_level_size(root, *level - 1);
1456                 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1457                                                1, &refs, NULL);
1458                 if (ret < 0)
1459                         refs = 0;
1460
1461                 if (refs > 1) {
1462                         ret = enter_shared_node(root, bytenr, refs,
1463                                                 wc, *level - 1);
1464                         if (ret > 0) {
1465                                 path->slots[*level]++;
1466                                 continue;
1467                         }
1468                 }
1469
1470                 next = btrfs_find_tree_block(root, bytenr, blocksize);
1471                 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1472                         free_extent_buffer(next);
1473                         reada_walk_down(root, cur, path->slots[*level]);
1474                         next = read_tree_block(root, bytenr, blocksize,
1475                                                ptr_gen);
1476                         if (!next) {
1477                                 err = -EIO;
1478                                 goto out;
1479                         }
1480                 }
1481
1482                 ret = check_child_node(root, cur, path->slots[*level], next);
1483                 if (ret) {
1484                         err = ret;
1485                         goto out;
1486                 }
1487
1488                 if (btrfs_is_leaf(next))
1489                         status = btrfs_check_leaf(root, NULL, next);
1490                 else
1491                         status = btrfs_check_node(root, NULL, next);
1492                 if (status != BTRFS_TREE_BLOCK_CLEAN) {
1493                         free_extent_buffer(next);
1494                         err = -EIO;
1495                         goto out;
1496                 }
1497
1498                 *level = *level - 1;
1499                 free_extent_buffer(path->nodes[*level]);
1500                 path->nodes[*level] = next;
1501                 path->slots[*level] = 0;
1502         }
1503 out:
1504         path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1505         return err;
1506 }
1507
1508 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1509                         struct walk_control *wc, int *level)
1510 {
1511         int i;
1512         struct extent_buffer *leaf;
1513
1514         for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1515                 leaf = path->nodes[i];
1516                 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1517                         path->slots[i]++;
1518                         *level = i;
1519                         return 0;
1520                 } else {
1521                         free_extent_buffer(path->nodes[*level]);
1522                         path->nodes[*level] = NULL;
1523                         BUG_ON(*level > wc->active_node);
1524                         if (*level == wc->active_node)
1525                                 leave_shared_node(root, wc, *level);
1526                         *level = i + 1;
1527                 }
1528         }
1529         return 1;
1530 }
1531
1532 static int check_root_dir(struct inode_record *rec)
1533 {
1534         struct inode_backref *backref;
1535         int ret = -1;
1536
1537         if (!rec->found_inode_item || rec->errors)
1538                 goto out;
1539         if (rec->nlink != 1 || rec->found_link != 0)
1540                 goto out;
1541         if (list_empty(&rec->backrefs))
1542                 goto out;
1543         backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1544         if (!backref->found_inode_ref)
1545                 goto out;
1546         if (backref->index != 0 || backref->namelen != 2 ||
1547             memcmp(backref->name, "..", 2))
1548                 goto out;
1549         if (backref->found_dir_index || backref->found_dir_item)
1550                 goto out;
1551         ret = 0;
1552 out:
1553         return ret;
1554 }
1555
1556 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1557                               struct btrfs_root *root, struct btrfs_path *path,
1558                               struct inode_record *rec)
1559 {
1560         struct btrfs_inode_item *ei;
1561         struct btrfs_key key;
1562         int ret;
1563
1564         key.objectid = rec->ino;
1565         key.type = BTRFS_INODE_ITEM_KEY;
1566         key.offset = (u64)-1;
1567
1568         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1569         if (ret < 0)
1570                 goto out;
1571         if (ret) {
1572                 if (!path->slots[0]) {
1573                         ret = -ENOENT;
1574                         goto out;
1575                 }
1576                 path->slots[0]--;
1577                 ret = 0;
1578         }
1579         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1580         if (key.objectid != rec->ino) {
1581                 ret = -ENOENT;
1582                 goto out;
1583         }
1584
1585         ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1586                             struct btrfs_inode_item);
1587         btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1588         btrfs_mark_buffer_dirty(path->nodes[0]);
1589         rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1590         printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1591                root->root_key.objectid);
1592 out:
1593         btrfs_release_path(path);
1594         return ret;
1595 }
1596
1597 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1598                                     struct btrfs_root *root,
1599                                     struct btrfs_path *path,
1600                                     struct inode_record *rec)
1601 {
1602         struct btrfs_key key;
1603         int ret;
1604
1605         key.objectid = BTRFS_ORPHAN_OBJECTID;
1606         key.type = BTRFS_ORPHAN_ITEM_KEY;
1607         key.offset = rec->ino;
1608
1609         ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1610         btrfs_release_path(path);
1611         if (!ret)
1612                 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1613         return ret;
1614 }
1615
1616 static int add_missing_dir_index(struct btrfs_root *root,
1617                                  struct cache_tree *inode_cache,
1618                                  struct inode_record *rec,
1619                                  struct inode_backref *backref)
1620 {
1621         struct btrfs_path *path;
1622         struct btrfs_trans_handle *trans;
1623         struct btrfs_dir_item *dir_item;
1624         struct extent_buffer *leaf;
1625         struct btrfs_key key;
1626         struct btrfs_disk_key disk_key;
1627         struct inode_record *dir_rec;
1628         unsigned long name_ptr;
1629         u32 data_size = sizeof(*dir_item) + backref->namelen;
1630         int ret;
1631
1632         path = btrfs_alloc_path();
1633         if (!path)
1634                 return -ENOMEM;
1635
1636         trans = btrfs_start_transaction(root, 1);
1637         if (IS_ERR(trans)) {
1638                 btrfs_free_path(path);
1639                 return PTR_ERR(trans);
1640         }
1641
1642         fprintf(stderr, "repairing missing dir index item for inode %llu\n",
1643                 (unsigned long long)rec->ino);
1644         key.objectid = backref->dir;
1645         key.type = BTRFS_DIR_INDEX_KEY;
1646         key.offset = backref->index;
1647
1648         ret = btrfs_insert_empty_item(trans, root, path, &key, data_size);
1649         BUG_ON(ret);
1650
1651         leaf = path->nodes[0];
1652         dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
1653
1654         disk_key.objectid = cpu_to_le64(rec->ino);
1655         disk_key.type = BTRFS_INODE_ITEM_KEY;
1656         disk_key.offset = 0;
1657
1658         btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
1659         btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
1660         btrfs_set_dir_data_len(leaf, dir_item, 0);
1661         btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
1662         name_ptr = (unsigned long)(dir_item + 1);
1663         write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
1664         btrfs_mark_buffer_dirty(leaf);
1665         btrfs_free_path(path);
1666         btrfs_commit_transaction(trans, root);
1667
1668         backref->found_dir_index = 1;
1669         dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
1670         if (!dir_rec)
1671                 return 0;
1672         dir_rec->found_size += backref->namelen;
1673         if (dir_rec->found_size == dir_rec->isize &&
1674             (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
1675                 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1676         if (dir_rec->found_size != dir_rec->isize)
1677                 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
1678
1679         return 0;
1680 }
1681
1682 static int delete_dir_index(struct btrfs_root *root,
1683                             struct cache_tree *inode_cache,
1684                             struct inode_record *rec,
1685                             struct inode_backref *backref)
1686 {
1687         struct btrfs_trans_handle *trans;
1688         struct btrfs_dir_item *di;
1689         struct btrfs_path *path;
1690         int ret = 0;
1691
1692         path = btrfs_alloc_path();
1693         if (!path)
1694                 return -ENOMEM;
1695
1696         trans = btrfs_start_transaction(root, 1);
1697         if (IS_ERR(trans)) {
1698                 btrfs_free_path(path);
1699                 return PTR_ERR(trans);
1700         }
1701
1702
1703         fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
1704                 (unsigned long long)backref->dir,
1705                 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
1706                 (unsigned long long)root->objectid);
1707
1708         di = btrfs_lookup_dir_index(trans, root, path, backref->dir,
1709                                     backref->name, backref->namelen,
1710                                     backref->index, -1);
1711         if (IS_ERR(di)) {
1712                 ret = PTR_ERR(di);
1713                 btrfs_free_path(path);
1714                 btrfs_commit_transaction(trans, root);
1715                 if (ret == -ENOENT)
1716                         return 0;
1717                 return ret;
1718         }
1719
1720         if (!di)
1721                 ret = btrfs_del_item(trans, root, path);
1722         else
1723                 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1724         BUG_ON(ret);
1725         btrfs_free_path(path);
1726         btrfs_commit_transaction(trans, root);
1727         return ret;
1728 }
1729
1730 static int create_inode_item(struct btrfs_root *root,
1731                              struct inode_record *rec,
1732                              struct inode_backref *backref, int root_dir)
1733 {
1734         struct btrfs_trans_handle *trans;
1735         struct btrfs_inode_item inode_item;
1736         time_t now = time(NULL);
1737         int ret;
1738
1739         trans = btrfs_start_transaction(root, 1);
1740         if (IS_ERR(trans)) {
1741                 ret = PTR_ERR(trans);
1742                 return ret;
1743         }
1744
1745         fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
1746                 "be incomplete, please check permissions and content after "
1747                 "the fsck completes.\n", (unsigned long long)root->objectid,
1748                 (unsigned long long)rec->ino);
1749
1750         memset(&inode_item, 0, sizeof(inode_item));
1751         btrfs_set_stack_inode_generation(&inode_item, trans->transid);
1752         if (root_dir)
1753                 btrfs_set_stack_inode_nlink(&inode_item, 1);
1754         else
1755                 btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
1756         btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
1757         if (rec->found_dir_item) {
1758                 if (rec->found_file_extent)
1759                         fprintf(stderr, "root %llu inode %llu has both a dir "
1760                                 "item and extents, unsure if it is a dir or a "
1761                                 "regular file so setting it as a directory\n",
1762                                 (unsigned long long)root->objectid,
1763                                 (unsigned long long)rec->ino);
1764                 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
1765                 btrfs_set_stack_inode_size(&inode_item, rec->found_size);
1766         } else if (!rec->found_dir_item) {
1767                 btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
1768                 btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
1769         }
1770         btrfs_set_stack_timespec_sec(&inode_item.atime, now);
1771         btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
1772         btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
1773         btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
1774         btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
1775         btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
1776         btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
1777         btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
1778
1779         ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
1780         BUG_ON(ret);
1781         btrfs_commit_transaction(trans, root);
1782         return 0;
1783 }
1784
1785 static int repair_inode_backrefs(struct btrfs_root *root,
1786                                  struct inode_record *rec,
1787                                  struct cache_tree *inode_cache,
1788                                  int delete)
1789 {
1790         struct inode_backref *tmp, *backref;
1791         u64 root_dirid = btrfs_root_dirid(&root->root_item);
1792         int ret = 0;
1793         int repaired = 0;
1794
1795         list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
1796                 if (!delete && rec->ino == root_dirid) {
1797                         if (!rec->found_inode_item) {
1798                                 ret = create_inode_item(root, rec, backref, 1);
1799                                 if (ret)
1800                                         break;
1801                                 repaired++;
1802                         }
1803                 }
1804
1805                 /* Index 0 for root dir's are special, don't mess with it */
1806                 if (rec->ino == root_dirid && backref->index == 0)
1807                         continue;
1808
1809                 if (delete &&
1810                     ((backref->found_dir_index && !backref->found_inode_ref) ||
1811                      (backref->found_dir_index && backref->found_inode_ref &&
1812                       (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
1813                         ret = delete_dir_index(root, inode_cache, rec, backref);
1814                         if (ret)
1815                                 break;
1816                         repaired++;
1817                         list_del(&backref->list);
1818                         free(backref);
1819                 }
1820
1821                 if (!delete && !backref->found_dir_index &&
1822                     backref->found_dir_item && backref->found_inode_ref) {
1823                         ret = add_missing_dir_index(root, inode_cache, rec,
1824                                                     backref);
1825                         if (ret)
1826                                 break;
1827                         repaired++;
1828                         if (backref->found_dir_item &&
1829                             backref->found_dir_index &&
1830                             backref->found_dir_index) {
1831                                 if (!backref->errors &&
1832                                     backref->found_inode_ref) {
1833                                         list_del(&backref->list);
1834                                         free(backref);
1835                                 }
1836                         }
1837                 }
1838
1839                 if (!delete && (!backref->found_dir_index &&
1840                                 !backref->found_dir_item &&
1841                                 backref->found_inode_ref)) {
1842                         struct btrfs_trans_handle *trans;
1843                         struct btrfs_key location;
1844
1845                         location.objectid = rec->ino;
1846                         location.type = BTRFS_INODE_ITEM_KEY;
1847                         location.offset = 0;
1848
1849                         trans = btrfs_start_transaction(root, 1);
1850                         if (IS_ERR(trans)) {
1851                                 ret = PTR_ERR(trans);
1852                                 break;
1853                         }
1854                         fprintf(stderr, "adding missing dir index/item pair "
1855                                 "for inode %llu\n",
1856                                 (unsigned long long)rec->ino);
1857                         ret = btrfs_insert_dir_item(trans, root, backref->name,
1858                                                     backref->namelen,
1859                                                     backref->dir, &location,
1860                                                     imode_to_type(rec->imode),
1861                                                     backref->index);
1862                         BUG_ON(ret);
1863                         btrfs_commit_transaction(trans, root);
1864                         repaired++;
1865                 }
1866
1867                 if (!delete && (backref->found_inode_ref &&
1868                                 backref->found_dir_index &&
1869                                 backref->found_dir_item &&
1870                                 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
1871                                 !rec->found_inode_item)) {
1872                         ret = create_inode_item(root, rec, backref, 0);
1873                         if (ret)
1874                                 break;
1875                         repaired++;
1876                 }
1877
1878         }
1879         return ret ? ret : repaired;
1880 }
1881
1882 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1883 {
1884         struct btrfs_trans_handle *trans;
1885         struct btrfs_path *path;
1886         int ret = 0;
1887
1888         if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1889                 return rec->errors;
1890
1891         path = btrfs_alloc_path();
1892         if (!path)
1893                 return -ENOMEM;
1894
1895         trans = btrfs_start_transaction(root, 1);
1896         if (IS_ERR(trans)) {
1897                 btrfs_free_path(path);
1898                 return PTR_ERR(trans);
1899         }
1900
1901         if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
1902                 ret = repair_inode_isize(trans, root, path, rec);
1903         if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
1904                 ret = repair_inode_orphan_item(trans, root, path, rec);
1905         btrfs_commit_transaction(trans, root);
1906         btrfs_free_path(path);
1907         return ret;
1908 }
1909
1910 static int check_inode_recs(struct btrfs_root *root,
1911                             struct cache_tree *inode_cache)
1912 {
1913         struct cache_extent *cache;
1914         struct ptr_node *node;
1915         struct inode_record *rec;
1916         struct inode_backref *backref;
1917         int stage = 0;
1918         int ret;
1919         int err = 0;
1920         u64 error = 0;
1921         u64 root_dirid = btrfs_root_dirid(&root->root_item);
1922
1923         if (btrfs_root_refs(&root->root_item) == 0) {
1924                 if (!cache_tree_empty(inode_cache))
1925                         fprintf(stderr, "warning line %d\n", __LINE__);
1926                 return 0;
1927         }
1928
1929         /*
1930          * We need to repair backrefs first because we could change some of the
1931          * errors in the inode recs.
1932          *
1933          * We also need to go through and delete invalid backrefs first and then
1934          * add the correct ones second.  We do this because we may get EEXIST
1935          * when adding back the correct index because we hadn't yet deleted the
1936          * invalid index.
1937          *
1938          * For example, if we were missing a dir index then the directories
1939          * isize would be wrong, so if we fixed the isize to what we thought it
1940          * would be and then fixed the backref we'd still have a invalid fs, so
1941          * we need to add back the dir index and then check to see if the isize
1942          * is still wrong.
1943          */
1944         while (stage < 3) {
1945                 stage++;
1946                 if (stage == 3 && !err)
1947                         break;
1948
1949                 cache = search_cache_extent(inode_cache, 0);
1950                 while (repair && cache) {
1951                         node = container_of(cache, struct ptr_node, cache);
1952                         rec = node->data;
1953                         cache = next_cache_extent(cache);
1954
1955                         /* Need to free everything up and rescan */
1956                         if (stage == 3) {
1957                                 remove_cache_extent(inode_cache, &node->cache);
1958                                 free(node);
1959                                 free_inode_rec(rec);
1960                                 continue;
1961                         }
1962
1963                         if (list_empty(&rec->backrefs))
1964                                 continue;
1965
1966                         ret = repair_inode_backrefs(root, rec, inode_cache,
1967                                                     stage == 1);
1968                         if (ret < 0) {
1969                                 err = ret;
1970                                 stage = 2;
1971                                 break;
1972                         } if (ret > 0) {
1973                                 err = -EAGAIN;
1974                         }
1975                 }
1976         }
1977         if (err)
1978                 return err;
1979
1980         rec = get_inode_rec(inode_cache, root_dirid, 0);
1981         if (rec) {
1982                 ret = check_root_dir(rec);
1983                 if (ret) {
1984                         fprintf(stderr, "root %llu root dir %llu error\n",
1985                                 (unsigned long long)root->root_key.objectid,
1986                                 (unsigned long long)root_dirid);
1987                         error++;
1988                 }
1989         } else {
1990                 if (repair) {
1991                         struct btrfs_trans_handle *trans;
1992
1993                         trans = btrfs_start_transaction(root, 1);
1994                         if (IS_ERR(trans)) {
1995                                 err = PTR_ERR(trans);
1996                                 return err;
1997                         }
1998
1999                         fprintf(stderr,
2000                                 "root %llu missing its root dir, recreating\n",
2001                                 (unsigned long long)root->objectid);
2002
2003                         ret = btrfs_make_root_dir(trans, root, root_dirid);
2004                         BUG_ON(ret);
2005
2006                         btrfs_commit_transaction(trans, root);
2007                         return -EAGAIN;
2008                 }
2009
2010                 fprintf(stderr, "root %llu root dir %llu not found\n",
2011                         (unsigned long long)root->root_key.objectid,
2012                         (unsigned long long)root_dirid);
2013         }
2014
2015         while (1) {
2016                 cache = search_cache_extent(inode_cache, 0);
2017                 if (!cache)
2018                         break;
2019                 node = container_of(cache, struct ptr_node, cache);
2020                 rec = node->data;
2021                 remove_cache_extent(inode_cache, &node->cache);
2022                 free(node);
2023                 if (rec->ino == root_dirid ||
2024                     rec->ino == BTRFS_ORPHAN_OBJECTID) {
2025                         free_inode_rec(rec);
2026                         continue;
2027                 }
2028
2029                 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
2030                         ret = check_orphan_item(root, rec->ino);
2031                         if (ret == 0)
2032                                 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2033                         if (can_free_inode_rec(rec)) {
2034                                 free_inode_rec(rec);
2035                                 continue;
2036                         }
2037                 }
2038
2039                 if (repair) {
2040                         ret = try_repair_inode(root, rec);
2041                         if (ret == 0 && can_free_inode_rec(rec)) {
2042                                 free_inode_rec(rec);
2043                                 continue;
2044                         }
2045                         ret = 0;
2046                 }
2047
2048                 error++;
2049                 if (!rec->found_inode_item)
2050                         rec->errors |= I_ERR_NO_INODE_ITEM;
2051                 if (rec->found_link != rec->nlink)
2052                         rec->errors |= I_ERR_LINK_COUNT_WRONG;
2053                 print_inode_error(root, rec);
2054                 list_for_each_entry(backref, &rec->backrefs, list) {
2055                         if (!backref->found_dir_item)
2056                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
2057                         if (!backref->found_dir_index)
2058                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
2059                         if (!backref->found_inode_ref)
2060                                 backref->errors |= REF_ERR_NO_INODE_REF;
2061                         fprintf(stderr, "\tunresolved ref dir %llu index %llu"
2062                                 " namelen %u name %s filetype %d errors %x",
2063                                 (unsigned long long)backref->dir,
2064                                 (unsigned long long)backref->index,
2065                                 backref->namelen, backref->name,
2066                                 backref->filetype, backref->errors);
2067                         print_ref_error(backref->errors);
2068                 }
2069                 free_inode_rec(rec);
2070         }
2071         return (error > 0) ? -1 : 0;
2072 }
2073
2074 static struct root_record *get_root_rec(struct cache_tree *root_cache,
2075                                         u64 objectid)
2076 {
2077         struct cache_extent *cache;
2078         struct root_record *rec = NULL;
2079         int ret;
2080
2081         cache = lookup_cache_extent(root_cache, objectid, 1);
2082         if (cache) {
2083                 rec = container_of(cache, struct root_record, cache);
2084         } else {
2085                 rec = calloc(1, sizeof(*rec));
2086                 rec->objectid = objectid;
2087                 INIT_LIST_HEAD(&rec->backrefs);
2088                 rec->cache.start = objectid;
2089                 rec->cache.size = 1;
2090
2091                 ret = insert_cache_extent(root_cache, &rec->cache);
2092                 BUG_ON(ret);
2093         }
2094         return rec;
2095 }
2096
2097 static struct root_backref *get_root_backref(struct root_record *rec,
2098                                              u64 ref_root, u64 dir, u64 index,
2099                                              const char *name, int namelen)
2100 {
2101         struct root_backref *backref;
2102
2103         list_for_each_entry(backref, &rec->backrefs, list) {
2104                 if (backref->ref_root != ref_root || backref->dir != dir ||
2105                     backref->namelen != namelen)
2106                         continue;
2107                 if (memcmp(name, backref->name, namelen))
2108                         continue;
2109                 return backref;
2110         }
2111
2112         backref = malloc(sizeof(*backref) + namelen + 1);
2113         memset(backref, 0, sizeof(*backref));
2114         backref->ref_root = ref_root;
2115         backref->dir = dir;
2116         backref->index = index;
2117         backref->namelen = namelen;
2118         memcpy(backref->name, name, namelen);
2119         backref->name[namelen] = '\0';
2120         list_add_tail(&backref->list, &rec->backrefs);
2121         return backref;
2122 }
2123
2124 static void free_root_record(struct cache_extent *cache)
2125 {
2126         struct root_record *rec;
2127         struct root_backref *backref;
2128
2129         rec = container_of(cache, struct root_record, cache);
2130         while (!list_empty(&rec->backrefs)) {
2131                 backref = list_entry(rec->backrefs.next,
2132                                      struct root_backref, list);
2133                 list_del(&backref->list);
2134                 free(backref);
2135         }
2136
2137         kfree(rec);
2138 }
2139
2140 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
2141
2142 static int add_root_backref(struct cache_tree *root_cache,
2143                             u64 root_id, u64 ref_root, u64 dir, u64 index,
2144                             const char *name, int namelen,
2145                             int item_type, int errors)
2146 {
2147         struct root_record *rec;
2148         struct root_backref *backref;
2149
2150         rec = get_root_rec(root_cache, root_id);
2151         backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
2152
2153         backref->errors |= errors;
2154
2155         if (item_type != BTRFS_DIR_ITEM_KEY) {
2156                 if (backref->found_dir_index || backref->found_back_ref ||
2157                     backref->found_forward_ref) {
2158                         if (backref->index != index)
2159                                 backref->errors |= REF_ERR_INDEX_UNMATCH;
2160                 } else {
2161                         backref->index = index;
2162                 }
2163         }
2164
2165         if (item_type == BTRFS_DIR_ITEM_KEY) {
2166                 if (backref->found_forward_ref)
2167                         rec->found_ref++;
2168                 backref->found_dir_item = 1;
2169         } else if (item_type == BTRFS_DIR_INDEX_KEY) {
2170                 backref->found_dir_index = 1;
2171         } else if (item_type == BTRFS_ROOT_REF_KEY) {
2172                 if (backref->found_forward_ref)
2173                         backref->errors |= REF_ERR_DUP_ROOT_REF;
2174                 else if (backref->found_dir_item)
2175                         rec->found_ref++;
2176                 backref->found_forward_ref = 1;
2177         } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
2178                 if (backref->found_back_ref)
2179                         backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
2180                 backref->found_back_ref = 1;
2181         } else {
2182                 BUG_ON(1);
2183         }
2184
2185         if (backref->found_forward_ref && backref->found_dir_item)
2186                 backref->reachable = 1;
2187         return 0;
2188 }
2189
2190 static int merge_root_recs(struct btrfs_root *root,
2191                            struct cache_tree *src_cache,
2192                            struct cache_tree *dst_cache)
2193 {
2194         struct cache_extent *cache;
2195         struct ptr_node *node;
2196         struct inode_record *rec;
2197         struct inode_backref *backref;
2198         int ret = 0;
2199
2200         if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2201                 free_inode_recs_tree(src_cache);
2202                 return 0;
2203         }
2204
2205         while (1) {
2206                 cache = search_cache_extent(src_cache, 0);
2207                 if (!cache)
2208                         break;
2209                 node = container_of(cache, struct ptr_node, cache);
2210                 rec = node->data;
2211                 remove_cache_extent(src_cache, &node->cache);
2212                 free(node);
2213
2214                 ret = is_child_root(root, root->objectid, rec->ino);
2215                 if (ret < 0)
2216                         break;
2217                 else if (ret == 0)
2218                         goto skip;
2219
2220                 list_for_each_entry(backref, &rec->backrefs, list) {
2221                         BUG_ON(backref->found_inode_ref);
2222                         if (backref->found_dir_item)
2223                                 add_root_backref(dst_cache, rec->ino,
2224                                         root->root_key.objectid, backref->dir,
2225                                         backref->index, backref->name,
2226                                         backref->namelen, BTRFS_DIR_ITEM_KEY,
2227                                         backref->errors);
2228                         if (backref->found_dir_index)
2229                                 add_root_backref(dst_cache, rec->ino,
2230                                         root->root_key.objectid, backref->dir,
2231                                         backref->index, backref->name,
2232                                         backref->namelen, BTRFS_DIR_INDEX_KEY,
2233                                         backref->errors);
2234                 }
2235 skip:
2236                 free_inode_rec(rec);
2237         }
2238         if (ret < 0)
2239                 return ret;
2240         return 0;
2241 }
2242
2243 static int check_root_refs(struct btrfs_root *root,
2244                            struct cache_tree *root_cache)
2245 {
2246         struct root_record *rec;
2247         struct root_record *ref_root;
2248         struct root_backref *backref;
2249         struct cache_extent *cache;
2250         int loop = 1;
2251         int ret;
2252         int error;
2253         int errors = 0;
2254
2255         rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
2256         rec->found_ref = 1;
2257
2258         /* fixme: this can not detect circular references */
2259         while (loop) {
2260                 loop = 0;
2261                 cache = search_cache_extent(root_cache, 0);
2262                 while (1) {
2263                         if (!cache)
2264                                 break;
2265                         rec = container_of(cache, struct root_record, cache);
2266                         cache = next_cache_extent(cache);
2267
2268                         if (rec->found_ref == 0)
2269                                 continue;
2270
2271                         list_for_each_entry(backref, &rec->backrefs, list) {
2272                                 if (!backref->reachable)
2273                                         continue;
2274
2275                                 ref_root = get_root_rec(root_cache,
2276                                                         backref->ref_root);
2277                                 if (ref_root->found_ref > 0)
2278                                         continue;
2279
2280                                 backref->reachable = 0;
2281                                 rec->found_ref--;
2282                                 if (rec->found_ref == 0)
2283                                         loop = 1;
2284                         }
2285                 }
2286         }
2287
2288         cache = search_cache_extent(root_cache, 0);
2289         while (1) {
2290                 if (!cache)
2291                         break;
2292                 rec = container_of(cache, struct root_record, cache);
2293                 cache = next_cache_extent(cache);
2294
2295                 if (rec->found_ref == 0 &&
2296                     rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
2297                     rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
2298                         ret = check_orphan_item(root->fs_info->tree_root,
2299                                                 rec->objectid);
2300                         if (ret == 0)
2301                                 continue;
2302
2303                         /*
2304                          * If we don't have a root item then we likely just have
2305                          * a dir item in a snapshot for this root but no actual
2306                          * ref key or anything so it's meaningless.
2307                          */
2308                         if (!rec->found_root_item)
2309                                 continue;
2310                         errors++;
2311                         fprintf(stderr, "fs tree %llu not referenced\n",
2312                                 (unsigned long long)rec->objectid);
2313                 }
2314
2315                 error = 0;
2316                 if (rec->found_ref > 0 && !rec->found_root_item)
2317                         error = 1;
2318                 list_for_each_entry(backref, &rec->backrefs, list) {
2319                         if (!backref->found_dir_item)
2320                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
2321                         if (!backref->found_dir_index)
2322                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
2323                         if (!backref->found_back_ref)
2324                                 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
2325                         if (!backref->found_forward_ref)
2326                                 backref->errors |= REF_ERR_NO_ROOT_REF;
2327                         if (backref->reachable && backref->errors)
2328                                 error = 1;
2329                 }
2330                 if (!error)
2331                         continue;
2332
2333                 errors++;
2334                 fprintf(stderr, "fs tree %llu refs %u %s\n",
2335                         (unsigned long long)rec->objectid, rec->found_ref,
2336                          rec->found_root_item ? "" : "not found");
2337
2338                 list_for_each_entry(backref, &rec->backrefs, list) {
2339                         if (!backref->reachable)
2340                                 continue;
2341                         if (!backref->errors && rec->found_root_item)
2342                                 continue;
2343                         fprintf(stderr, "\tunresolved ref root %llu dir %llu"
2344                                 " index %llu namelen %u name %s errors %x\n",
2345                                 (unsigned long long)backref->ref_root,
2346                                 (unsigned long long)backref->dir,
2347                                 (unsigned long long)backref->index,
2348                                 backref->namelen, backref->name,
2349                                 backref->errors);
2350                         print_ref_error(backref->errors);
2351                 }
2352         }
2353         return errors > 0 ? 1 : 0;
2354 }
2355
2356 static int process_root_ref(struct extent_buffer *eb, int slot,
2357                             struct btrfs_key *key,
2358                             struct cache_tree *root_cache)
2359 {
2360         u64 dirid;
2361         u64 index;
2362         u32 len;
2363         u32 name_len;
2364         struct btrfs_root_ref *ref;
2365         char namebuf[BTRFS_NAME_LEN];
2366         int error;
2367
2368         ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
2369
2370         dirid = btrfs_root_ref_dirid(eb, ref);
2371         index = btrfs_root_ref_sequence(eb, ref);
2372         name_len = btrfs_root_ref_name_len(eb, ref);
2373
2374         if (name_len <= BTRFS_NAME_LEN) {
2375                 len = name_len;
2376                 error = 0;
2377         } else {
2378                 len = BTRFS_NAME_LEN;
2379                 error = REF_ERR_NAME_TOO_LONG;
2380         }
2381         read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
2382
2383         if (key->type == BTRFS_ROOT_REF_KEY) {
2384                 add_root_backref(root_cache, key->offset, key->objectid, dirid,
2385                                  index, namebuf, len, key->type, error);
2386         } else {
2387                 add_root_backref(root_cache, key->objectid, key->offset, dirid,
2388                                  index, namebuf, len, key->type, error);
2389         }
2390         return 0;
2391 }
2392
2393 static int check_fs_root(struct btrfs_root *root,
2394                          struct cache_tree *root_cache,
2395                          struct walk_control *wc)
2396 {
2397         int ret = 0;
2398         int err = 0;
2399         int wret;
2400         int level;
2401         struct btrfs_path path;
2402         struct shared_node root_node;
2403         struct root_record *rec;
2404         struct btrfs_root_item *root_item = &root->root_item;
2405         enum btrfs_tree_block_status status;
2406
2407         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2408                 rec = get_root_rec(root_cache, root->root_key.objectid);
2409                 if (btrfs_root_refs(root_item) > 0)
2410                         rec->found_root_item = 1;
2411         }
2412
2413         btrfs_init_path(&path);
2414         memset(&root_node, 0, sizeof(root_node));
2415         cache_tree_init(&root_node.root_cache);
2416         cache_tree_init(&root_node.inode_cache);
2417
2418         level = btrfs_header_level(root->node);
2419         memset(wc->nodes, 0, sizeof(wc->nodes));
2420         wc->nodes[level] = &root_node;
2421         wc->active_node = level;
2422         wc->root_level = level;
2423
2424         /* We may not have checked the root block, lets do that now */
2425         if (btrfs_is_leaf(root->node))
2426                 status = btrfs_check_leaf(root, NULL, root->node);
2427         else
2428                 status = btrfs_check_node(root, NULL, root->node);
2429         if (status != BTRFS_TREE_BLOCK_CLEAN)
2430                 return -EIO;
2431
2432         if (btrfs_root_refs(root_item) > 0 ||
2433             btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2434                 path.nodes[level] = root->node;
2435                 extent_buffer_get(root->node);
2436                 path.slots[level] = 0;
2437         } else {
2438                 struct btrfs_key key;
2439                 struct btrfs_disk_key found_key;
2440
2441                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2442                 level = root_item->drop_level;
2443                 path.lowest_level = level;
2444                 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2445                 if (wret < 0)
2446                         goto skip_walking;
2447                 btrfs_node_key(path.nodes[level], &found_key,
2448                                 path.slots[level]);
2449                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2450                                         sizeof(found_key)));
2451         }
2452
2453         while (1) {
2454                 wret = walk_down_tree(root, &path, wc, &level);
2455                 if (wret < 0)
2456                         ret = wret;
2457                 if (wret != 0)
2458                         break;
2459
2460                 wret = walk_up_tree(root, &path, wc, &level);
2461                 if (wret < 0)
2462                         ret = wret;
2463                 if (wret != 0)
2464                         break;
2465         }
2466 skip_walking:
2467         btrfs_release_path(&path);
2468
2469         err = merge_root_recs(root, &root_node.root_cache, root_cache);
2470         if (err < 0)
2471                 ret = err;
2472
2473         if (root_node.current) {
2474                 root_node.current->checked = 1;
2475                 maybe_free_inode_rec(&root_node.inode_cache,
2476                                 root_node.current);
2477         }
2478
2479         err = check_inode_recs(root, &root_node.inode_cache);
2480         if (!ret)
2481                 ret = err;
2482         return ret;
2483 }
2484
2485 static int fs_root_objectid(u64 objectid)
2486 {
2487         if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
2488             objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2489                 return 1;
2490         return is_fstree(objectid);
2491 }
2492
2493 static int check_fs_roots(struct btrfs_root *root,
2494                           struct cache_tree *root_cache)
2495 {
2496         struct btrfs_path path;
2497         struct btrfs_key key;
2498         struct walk_control wc;
2499         struct extent_buffer *leaf, *tree_node;
2500         struct btrfs_root *tmp_root;
2501         struct btrfs_root *tree_root = root->fs_info->tree_root;
2502         int ret;
2503         int err = 0;
2504
2505         /*
2506          * Just in case we made any changes to the extent tree that weren't
2507          * reflected into the free space cache yet.
2508          */
2509         if (repair)
2510                 reset_cached_block_groups(root->fs_info);
2511         memset(&wc, 0, sizeof(wc));
2512         cache_tree_init(&wc.shared);
2513         btrfs_init_path(&path);
2514
2515 again:
2516         key.offset = 0;
2517         key.objectid = 0;
2518         key.type = BTRFS_ROOT_ITEM_KEY;
2519         ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2520         if (ret < 0) {
2521                 err = 1;
2522                 goto out;
2523         }
2524         tree_node = tree_root->node;
2525         while (1) {
2526                 if (tree_node != tree_root->node) {
2527                         free_root_recs_tree(root_cache);
2528                         btrfs_release_path(&path);
2529                         goto again;
2530                 }
2531                 leaf = path.nodes[0];
2532                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2533                         ret = btrfs_next_leaf(tree_root, &path);
2534                         if (ret) {
2535                                 if (ret < 0)
2536                                         err = 1;
2537                                 break;
2538                         }
2539                         leaf = path.nodes[0];
2540                 }
2541                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2542                 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2543                     fs_root_objectid(key.objectid)) {
2544                         if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2545                                 tmp_root = btrfs_read_fs_root_no_cache(
2546                                                 root->fs_info, &key);
2547                         } else {
2548                                 key.offset = (u64)-1;
2549                                 tmp_root = btrfs_read_fs_root(
2550                                                 root->fs_info, &key);
2551                         }
2552                         if (IS_ERR(tmp_root)) {
2553                                 err = 1;
2554                                 goto next;
2555                         }
2556                         ret = check_fs_root(tmp_root, root_cache, &wc);
2557                         if (ret == -EAGAIN) {
2558                                 free_root_recs_tree(root_cache);
2559                                 btrfs_release_path(&path);
2560                                 goto again;
2561                         }
2562                         if (ret)
2563                                 err = 1;
2564                         if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
2565                                 btrfs_free_fs_root(tmp_root);
2566                 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2567                            key.type == BTRFS_ROOT_BACKREF_KEY) {
2568                         process_root_ref(leaf, path.slots[0], &key,
2569                                          root_cache);
2570                 }
2571 next:
2572                 path.slots[0]++;
2573         }
2574 out:
2575         btrfs_release_path(&path);
2576         if (err)
2577                 free_extent_cache_tree(&wc.shared);
2578         if (!cache_tree_empty(&wc.shared))
2579                 fprintf(stderr, "warning line %d\n", __LINE__);
2580
2581         return err;
2582 }
2583
2584 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2585 {
2586         struct list_head *cur = rec->backrefs.next;
2587         struct extent_backref *back;
2588         struct tree_backref *tback;
2589         struct data_backref *dback;
2590         u64 found = 0;
2591         int err = 0;
2592
2593         while(cur != &rec->backrefs) {
2594                 back = list_entry(cur, struct extent_backref, list);
2595                 cur = cur->next;
2596                 if (!back->found_extent_tree) {
2597                         err = 1;
2598                         if (!print_errs)
2599                                 goto out;
2600                         if (back->is_data) {
2601                                 dback = (struct data_backref *)back;
2602                                 fprintf(stderr, "Backref %llu %s %llu"
2603                                         " owner %llu offset %llu num_refs %lu"
2604                                         " not found in extent tree\n",
2605                                         (unsigned long long)rec->start,
2606                                         back->full_backref ?
2607                                         "parent" : "root",
2608                                         back->full_backref ?
2609                                         (unsigned long long)dback->parent:
2610                                         (unsigned long long)dback->root,
2611                                         (unsigned long long)dback->owner,
2612                                         (unsigned long long)dback->offset,
2613                                         (unsigned long)dback->num_refs);
2614                         } else {
2615                                 tback = (struct tree_backref *)back;
2616                                 fprintf(stderr, "Backref %llu parent %llu"
2617                                         " root %llu not found in extent tree\n",
2618                                         (unsigned long long)rec->start,
2619                                         (unsigned long long)tback->parent,
2620                                         (unsigned long long)tback->root);
2621                         }
2622                 }
2623                 if (!back->is_data && !back->found_ref) {
2624                         err = 1;
2625                         if (!print_errs)
2626                                 goto out;
2627                         tback = (struct tree_backref *)back;
2628                         fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2629                                 (unsigned long long)rec->start,
2630                                 back->full_backref ? "parent" : "root",
2631                                 back->full_backref ?
2632                                 (unsigned long long)tback->parent :
2633                                 (unsigned long long)tback->root, back);
2634                 }
2635                 if (back->is_data) {
2636                         dback = (struct data_backref *)back;
2637                         if (dback->found_ref != dback->num_refs) {
2638                                 err = 1;
2639                                 if (!print_errs)
2640                                         goto out;
2641                                 fprintf(stderr, "Incorrect local backref count"
2642                                         " on %llu %s %llu owner %llu"
2643                                         " offset %llu found %u wanted %u back %p\n",
2644                                         (unsigned long long)rec->start,
2645                                         back->full_backref ?
2646                                         "parent" : "root",
2647                                         back->full_backref ?
2648                                         (unsigned long long)dback->parent:
2649                                         (unsigned long long)dback->root,
2650                                         (unsigned long long)dback->owner,
2651                                         (unsigned long long)dback->offset,
2652                                         dback->found_ref, dback->num_refs, back);
2653                         }
2654                         if (dback->disk_bytenr != rec->start) {
2655                                 err = 1;
2656                                 if (!print_errs)
2657                                         goto out;
2658                                 fprintf(stderr, "Backref disk bytenr does not"
2659                                         " match extent record, bytenr=%llu, "
2660                                         "ref bytenr=%llu\n",
2661                                         (unsigned long long)rec->start,
2662                                         (unsigned long long)dback->disk_bytenr);
2663                         }
2664
2665                         if (dback->bytes != rec->nr) {
2666                                 err = 1;
2667                                 if (!print_errs)
2668                                         goto out;
2669                                 fprintf(stderr, "Backref bytes do not match "
2670                                         "extent backref, bytenr=%llu, ref "
2671                                         "bytes=%llu, backref bytes=%llu\n",
2672                                         (unsigned long long)rec->start,
2673                                         (unsigned long long)rec->nr,
2674                                         (unsigned long long)dback->bytes);
2675                         }
2676                 }
2677                 if (!back->is_data) {
2678                         found += 1;
2679                 } else {
2680                         dback = (struct data_backref *)back;
2681                         found += dback->found_ref;
2682                 }
2683         }
2684         if (found != rec->refs) {
2685                 err = 1;
2686                 if (!print_errs)
2687                         goto out;
2688                 fprintf(stderr, "Incorrect global backref count "
2689                         "on %llu found %llu wanted %llu\n",
2690                         (unsigned long long)rec->start,
2691                         (unsigned long long)found,
2692                         (unsigned long long)rec->refs);
2693         }
2694 out:
2695         return err;
2696 }
2697
2698 static int free_all_extent_backrefs(struct extent_record *rec)
2699 {
2700         struct extent_backref *back;
2701         struct list_head *cur;
2702         while (!list_empty(&rec->backrefs)) {
2703                 cur = rec->backrefs.next;
2704                 back = list_entry(cur, struct extent_backref, list);
2705                 list_del(cur);
2706                 free(back);
2707         }
2708         return 0;
2709 }
2710
2711 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2712                                      struct cache_tree *extent_cache)
2713 {
2714         struct cache_extent *cache;
2715         struct extent_record *rec;
2716
2717         while (1) {
2718                 cache = first_cache_extent(extent_cache);
2719                 if (!cache)
2720                         break;
2721                 rec = container_of(cache, struct extent_record, cache);
2722                 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2723                 remove_cache_extent(extent_cache, cache);
2724                 free_all_extent_backrefs(rec);
2725                 free(rec);
2726         }
2727 }
2728
2729 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2730                                  struct extent_record *rec)
2731 {
2732         if (rec->content_checked && rec->owner_ref_checked &&
2733             rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2734             rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2735                 remove_cache_extent(extent_cache, &rec->cache);
2736                 free_all_extent_backrefs(rec);
2737                 list_del_init(&rec->list);
2738                 free(rec);
2739         }
2740         return 0;
2741 }
2742
2743 static int check_owner_ref(struct btrfs_root *root,
2744                             struct extent_record *rec,
2745                             struct extent_buffer *buf)
2746 {
2747         struct extent_backref *node;
2748         struct tree_backref *back;
2749         struct btrfs_root *ref_root;
2750         struct btrfs_key key;
2751         struct btrfs_path path;
2752         struct extent_buffer *parent;
2753         int level;
2754         int found = 0;
2755         int ret;
2756
2757         list_for_each_entry(node, &rec->backrefs, list) {
2758                 if (node->is_data)
2759                         continue;
2760                 if (!node->found_ref)
2761                         continue;
2762                 if (node->full_backref)
2763                         continue;
2764                 back = (struct tree_backref *)node;
2765                 if (btrfs_header_owner(buf) == back->root)
2766                         return 0;
2767         }
2768         BUG_ON(rec->is_root);
2769
2770         /* try to find the block by search corresponding fs tree */
2771         key.objectid = btrfs_header_owner(buf);
2772         key.type = BTRFS_ROOT_ITEM_KEY;
2773         key.offset = (u64)-1;
2774
2775         ref_root = btrfs_read_fs_root(root->fs_info, &key);
2776         if (IS_ERR(ref_root))
2777                 return 1;
2778
2779         level = btrfs_header_level(buf);
2780         if (level == 0)
2781                 btrfs_item_key_to_cpu(buf, &key, 0);
2782         else
2783                 btrfs_node_key_to_cpu(buf, &key, 0);
2784
2785         btrfs_init_path(&path);
2786         path.lowest_level = level + 1;
2787         ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2788         if (ret < 0)
2789                 return 0;
2790
2791         parent = path.nodes[level + 1];
2792         if (parent && buf->start == btrfs_node_blockptr(parent,
2793                                                         path.slots[level + 1]))
2794                 found = 1;
2795
2796         btrfs_release_path(&path);
2797         return found ? 0 : 1;
2798 }
2799
2800 static int is_extent_tree_record(struct extent_record *rec)
2801 {
2802         struct list_head *cur = rec->backrefs.next;
2803         struct extent_backref *node;
2804         struct tree_backref *back;
2805         int is_extent = 0;
2806
2807         while(cur != &rec->backrefs) {
2808                 node = list_entry(cur, struct extent_backref, list);
2809                 cur = cur->next;
2810                 if (node->is_data)
2811                         return 0;
2812                 back = (struct tree_backref *)node;
2813                 if (node->full_backref)
2814                         return 0;
2815                 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2816                         is_extent = 1;
2817         }
2818         return is_extent;
2819 }
2820
2821
2822 static int record_bad_block_io(struct btrfs_fs_info *info,
2823                                struct cache_tree *extent_cache,
2824                                u64 start, u64 len)
2825 {
2826         struct extent_record *rec;
2827         struct cache_extent *cache;
2828         struct btrfs_key key;
2829
2830         cache = lookup_cache_extent(extent_cache, start, len);
2831         if (!cache)
2832                 return 0;
2833
2834         rec = container_of(cache, struct extent_record, cache);
2835         if (!is_extent_tree_record(rec))
2836                 return 0;
2837
2838         btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2839         return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2840 }
2841
2842 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2843                        struct extent_buffer *buf, int slot)
2844 {
2845         if (btrfs_header_level(buf)) {
2846                 struct btrfs_key_ptr ptr1, ptr2;
2847
2848                 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2849                                    sizeof(struct btrfs_key_ptr));
2850                 read_extent_buffer(buf, &ptr2,
2851                                    btrfs_node_key_ptr_offset(slot + 1),
2852                                    sizeof(struct btrfs_key_ptr));
2853                 write_extent_buffer(buf, &ptr1,
2854                                     btrfs_node_key_ptr_offset(slot + 1),
2855                                     sizeof(struct btrfs_key_ptr));
2856                 write_extent_buffer(buf, &ptr2,
2857                                     btrfs_node_key_ptr_offset(slot),
2858                                     sizeof(struct btrfs_key_ptr));
2859                 if (slot == 0) {
2860                         struct btrfs_disk_key key;
2861                         btrfs_node_key(buf, &key, 0);
2862                         btrfs_fixup_low_keys(root, path, &key,
2863                                              btrfs_header_level(buf) + 1);
2864                 }
2865         } else {
2866                 struct btrfs_item *item1, *item2;
2867                 struct btrfs_key k1, k2;
2868                 char *item1_data, *item2_data;
2869                 u32 item1_offset, item2_offset, item1_size, item2_size;
2870
2871                 item1 = btrfs_item_nr(slot);
2872                 item2 = btrfs_item_nr(slot + 1);
2873                 btrfs_item_key_to_cpu(buf, &k1, slot);
2874                 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2875                 item1_offset = btrfs_item_offset(buf, item1);
2876                 item2_offset = btrfs_item_offset(buf, item2);
2877                 item1_size = btrfs_item_size(buf, item1);
2878                 item2_size = btrfs_item_size(buf, item2);
2879
2880                 item1_data = malloc(item1_size);
2881                 if (!item1_data)
2882                         return -ENOMEM;
2883                 item2_data = malloc(item2_size);
2884                 if (!item2_data) {
2885                         free(item1_data);
2886                         return -ENOMEM;
2887                 }
2888
2889                 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2890                 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2891
2892                 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2893                 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2894                 free(item1_data);
2895                 free(item2_data);
2896
2897                 btrfs_set_item_offset(buf, item1, item2_offset);
2898                 btrfs_set_item_offset(buf, item2, item1_offset);
2899                 btrfs_set_item_size(buf, item1, item2_size);
2900                 btrfs_set_item_size(buf, item2, item1_size);
2901
2902                 path->slots[0] = slot;
2903                 btrfs_set_item_key_unsafe(root, path, &k2);
2904                 path->slots[0] = slot + 1;
2905                 btrfs_set_item_key_unsafe(root, path, &k1);
2906         }
2907         return 0;
2908 }
2909
2910 static int fix_key_order(struct btrfs_trans_handle *trans,
2911                          struct btrfs_root *root,
2912                          struct btrfs_path *path)
2913 {
2914         struct extent_buffer *buf;
2915         struct btrfs_key k1, k2;
2916         int i;
2917         int level = path->lowest_level;
2918         int ret;
2919
2920         buf = path->nodes[level];
2921         for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2922                 if (level) {
2923                         btrfs_node_key_to_cpu(buf, &k1, i);
2924                         btrfs_node_key_to_cpu(buf, &k2, i + 1);
2925                 } else {
2926                         btrfs_item_key_to_cpu(buf, &k1, i);
2927                         btrfs_item_key_to_cpu(buf, &k2, i + 1);
2928                 }
2929                 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2930                         continue;
2931                 ret = swap_values(root, path, buf, i);
2932                 if (ret)
2933                         break;
2934                 btrfs_mark_buffer_dirty(buf);
2935                 i = 0;
2936         }
2937         return ret;
2938 }
2939
2940 static int delete_bogus_item(struct btrfs_trans_handle *trans,
2941                              struct btrfs_root *root,
2942                              struct btrfs_path *path,
2943                              struct extent_buffer *buf, int slot)
2944 {
2945         struct btrfs_key key;
2946         int nritems = btrfs_header_nritems(buf);
2947
2948         btrfs_item_key_to_cpu(buf, &key, slot);
2949
2950         /* These are all the keys we can deal with missing. */
2951         if (key.type != BTRFS_DIR_INDEX_KEY &&
2952             key.type != BTRFS_EXTENT_ITEM_KEY &&
2953             key.type != BTRFS_METADATA_ITEM_KEY &&
2954             key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2955             key.type != BTRFS_EXTENT_DATA_REF_KEY)
2956                 return -1;
2957
2958         printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
2959                (unsigned long long)key.objectid, key.type,
2960                (unsigned long long)key.offset, slot, buf->start);
2961         memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
2962                               btrfs_item_nr_offset(slot + 1),
2963                               sizeof(struct btrfs_item) *
2964                               (nritems - slot - 1));
2965         btrfs_set_header_nritems(buf, nritems - 1);
2966         if (slot == 0) {
2967                 struct btrfs_disk_key disk_key;
2968
2969                 btrfs_item_key(buf, &disk_key, 0);
2970                 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2971         }
2972         btrfs_mark_buffer_dirty(buf);
2973         return 0;
2974 }
2975
2976 static int fix_item_offset(struct btrfs_trans_handle *trans,
2977                            struct btrfs_root *root,
2978                            struct btrfs_path *path)
2979 {
2980         struct extent_buffer *buf;
2981         int i;
2982         int ret = 0;
2983
2984         /* We should only get this for leaves */
2985         BUG_ON(path->lowest_level);
2986         buf = path->nodes[0];
2987 again:
2988         for (i = 0; i < btrfs_header_nritems(buf); i++) {
2989                 unsigned int shift = 0, offset;
2990
2991                 if (i == 0 && btrfs_item_end_nr(buf, i) !=
2992                     BTRFS_LEAF_DATA_SIZE(root)) {
2993                         if (btrfs_item_end_nr(buf, i) >
2994                             BTRFS_LEAF_DATA_SIZE(root)) {
2995                                 ret = delete_bogus_item(trans, root, path,
2996                                                         buf, i);
2997                                 if (!ret)
2998                                         goto again;
2999                                 fprintf(stderr, "item is off the end of the "
3000                                         "leaf, can't fix\n");
3001                                 ret = -EIO;
3002                                 break;
3003                         }
3004                         shift = BTRFS_LEAF_DATA_SIZE(root) -
3005                                 btrfs_item_end_nr(buf, i);
3006                 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
3007                            btrfs_item_offset_nr(buf, i - 1)) {
3008                         if (btrfs_item_end_nr(buf, i) >
3009                             btrfs_item_offset_nr(buf, i - 1)) {
3010                                 ret = delete_bogus_item(trans, root, path,
3011                                                         buf, i);
3012                                 if (!ret)
3013                                         goto again;
3014                                 fprintf(stderr, "items overlap, can't fix\n");
3015                                 ret = -EIO;
3016                                 break;
3017                         }
3018                         shift = btrfs_item_offset_nr(buf, i - 1) -
3019                                 btrfs_item_end_nr(buf, i);
3020                 }
3021                 if (!shift)
3022                         continue;
3023
3024                 printf("Shifting item nr %d by %u bytes in block %llu\n",
3025                        i, shift, (unsigned long long)buf->start);
3026                 offset = btrfs_item_offset_nr(buf, i);
3027                 memmove_extent_buffer(buf,
3028                                       btrfs_leaf_data(buf) + offset + shift,
3029                                       btrfs_leaf_data(buf) + offset,
3030                                       btrfs_item_size_nr(buf, i));
3031                 btrfs_set_item_offset(buf, btrfs_item_nr(i),
3032                                       offset + shift);
3033                 btrfs_mark_buffer_dirty(buf);
3034         }
3035
3036         /*
3037          * We may have moved things, in which case we want to exit so we don't
3038          * write those changes out.  Once we have proper abort functionality in
3039          * progs this can be changed to something nicer.
3040          */
3041         BUG_ON(ret);
3042         return ret;
3043 }
3044
3045 /*
3046  * Attempt to fix basic block failures.  If we can't fix it for whatever reason
3047  * then just return -EIO.
3048  */
3049 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
3050                                 struct btrfs_root *root,
3051                                 struct extent_buffer *buf,
3052                                 enum btrfs_tree_block_status status)
3053 {
3054         struct ulist *roots;
3055         struct ulist_node *node;
3056         struct btrfs_root *search_root;
3057         struct btrfs_path *path;
3058         struct ulist_iterator iter;
3059         struct btrfs_key root_key, key;
3060         int ret;
3061
3062         if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
3063             status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3064                 return -EIO;
3065
3066         path = btrfs_alloc_path();
3067         if (!path)
3068                 return -EIO;
3069
3070         ret = btrfs_find_all_roots(trans, root->fs_info, buf->start,
3071                                    0, &roots);
3072         if (ret) {
3073                 btrfs_free_path(path);
3074                 return -EIO;
3075         }
3076
3077         ULIST_ITER_INIT(&iter);
3078         while ((node = ulist_next(roots, &iter))) {
3079                 root_key.objectid = node->val;
3080                 root_key.type = BTRFS_ROOT_ITEM_KEY;
3081                 root_key.offset = (u64)-1;
3082
3083                 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
3084                 if (IS_ERR(root)) {
3085                         ret = -EIO;
3086                         break;
3087                 }
3088
3089                 record_root_in_trans(trans, search_root);
3090
3091                 path->lowest_level = btrfs_header_level(buf);
3092                 path->skip_check_block = 1;
3093                 if (path->lowest_level)
3094                         btrfs_node_key_to_cpu(buf, &key, 0);
3095                 else
3096                         btrfs_item_key_to_cpu(buf, &key, 0);
3097                 ret = btrfs_search_slot(trans, search_root, &key, path, 0, 1);
3098                 if (ret) {
3099                         ret = -EIO;
3100                         break;
3101                 }
3102                 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
3103                         ret = fix_key_order(trans, search_root, path);
3104                 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3105                         ret = fix_item_offset(trans, search_root, path);
3106                 if (ret)
3107                         break;
3108                 btrfs_release_path(path);
3109         }
3110         ulist_free(roots);
3111         btrfs_free_path(path);
3112         return ret;
3113 }
3114
3115 static int check_block(struct btrfs_trans_handle *trans,
3116                        struct btrfs_root *root,
3117                        struct cache_tree *extent_cache,
3118                        struct extent_buffer *buf, u64 flags)
3119 {
3120         struct extent_record *rec;
3121         struct cache_extent *cache;
3122         struct btrfs_key key;
3123         enum btrfs_tree_block_status status;
3124         int ret = 0;
3125         int level;
3126
3127         cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
3128         if (!cache)
3129                 return 1;
3130         rec = container_of(cache, struct extent_record, cache);
3131         rec->generation = btrfs_header_generation(buf);
3132
3133         level = btrfs_header_level(buf);
3134         if (btrfs_header_nritems(buf) > 0) {
3135
3136                 if (level == 0)
3137                         btrfs_item_key_to_cpu(buf, &key, 0);
3138                 else
3139                         btrfs_node_key_to_cpu(buf, &key, 0);
3140
3141                 rec->info_objectid = key.objectid;
3142         }
3143         rec->info_level = level;
3144
3145         if (btrfs_is_leaf(buf))
3146                 status = btrfs_check_leaf(root, &rec->parent_key, buf);
3147         else
3148                 status = btrfs_check_node(root, &rec->parent_key, buf);
3149
3150         if (status != BTRFS_TREE_BLOCK_CLEAN) {
3151                 if (repair)
3152                         status = try_to_fix_bad_block(trans, root, buf,
3153                                                       status);
3154                 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3155                         ret = -EIO;
3156                         fprintf(stderr, "bad block %llu\n",
3157                                 (unsigned long long)buf->start);
3158                 } else {
3159                         /*
3160                          * Signal to callers we need to start the scan over
3161                          * again since we'll have cow'ed blocks.
3162                          */
3163                         ret = -EAGAIN;
3164                 }
3165         } else {
3166                 rec->content_checked = 1;
3167                 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3168                         rec->owner_ref_checked = 1;
3169                 else {
3170                         ret = check_owner_ref(root, rec, buf);
3171                         if (!ret)
3172                                 rec->owner_ref_checked = 1;
3173                 }
3174         }
3175         if (!ret)
3176                 maybe_free_extent_rec(extent_cache, rec);
3177         return ret;
3178 }
3179
3180 static struct tree_backref *find_tree_backref(struct extent_record *rec,
3181                                                 u64 parent, u64 root)
3182 {
3183         struct list_head *cur = rec->backrefs.next;
3184         struct extent_backref *node;
3185         struct tree_backref *back;
3186
3187         while(cur != &rec->backrefs) {
3188                 node = list_entry(cur, struct extent_backref, list);
3189                 cur = cur->next;
3190                 if (node->is_data)
3191                         continue;
3192                 back = (struct tree_backref *)node;
3193                 if (parent > 0) {
3194                         if (!node->full_backref)
3195                                 continue;
3196                         if (parent == back->parent)
3197                                 return back;
3198                 } else {
3199                         if (node->full_backref)
3200                                 continue;
3201                         if (back->root == root)
3202                                 return back;
3203                 }
3204         }
3205         return NULL;
3206 }
3207
3208 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
3209                                                 u64 parent, u64 root)
3210 {
3211         struct tree_backref *ref = malloc(sizeof(*ref));
3212         memset(&ref->node, 0, sizeof(ref->node));
3213         if (parent > 0) {
3214                 ref->parent = parent;
3215                 ref->node.full_backref = 1;
3216         } else {
3217                 ref->root = root;
3218                 ref->node.full_backref = 0;
3219         }
3220         list_add_tail(&ref->node.list, &rec->backrefs);
3221
3222         return ref;
3223 }
3224
3225 static struct data_backref *find_data_backref(struct extent_record *rec,
3226                                                 u64 parent, u64 root,
3227                                                 u64 owner, u64 offset,
3228                                                 int found_ref,
3229                                                 u64 disk_bytenr, u64 bytes)
3230 {
3231         struct list_head *cur = rec->backrefs.next;
3232         struct extent_backref *node;
3233         struct data_backref *back;
3234
3235         while(cur != &rec->backrefs) {
3236                 node = list_entry(cur, struct extent_backref, list);
3237                 cur = cur->next;
3238                 if (!node->is_data)
3239                         continue;
3240                 back = (struct data_backref *)node;
3241                 if (parent > 0) {
3242                         if (!node->full_backref)
3243                                 continue;
3244                         if (parent == back->parent)
3245                                 return back;
3246                 } else {
3247                         if (node->full_backref)
3248                                 continue;
3249                         if (back->root == root && back->owner == owner &&
3250                             back->offset == offset) {
3251                                 if (found_ref && node->found_ref &&
3252                                     (back->bytes != bytes ||
3253                                     back->disk_bytenr != disk_bytenr))
3254                                         continue;
3255                                 return back;
3256                         }
3257                 }
3258         }
3259         return NULL;
3260 }
3261
3262 static struct data_backref *alloc_data_backref(struct extent_record *rec,
3263                                                 u64 parent, u64 root,
3264                                                 u64 owner, u64 offset,
3265                                                 u64 max_size)
3266 {
3267         struct data_backref *ref = malloc(sizeof(*ref));
3268         memset(&ref->node, 0, sizeof(ref->node));
3269         ref->node.is_data = 1;
3270
3271         if (parent > 0) {
3272                 ref->parent = parent;
3273                 ref->owner = 0;
3274                 ref->offset = 0;
3275                 ref->node.full_backref = 1;
3276         } else {
3277                 ref->root = root;
3278                 ref->owner = owner;
3279                 ref->offset = offset;
3280                 ref->node.full_backref = 0;
3281         }
3282         ref->bytes = max_size;
3283         ref->found_ref = 0;
3284         ref->num_refs = 0;
3285         list_add_tail(&ref->node.list, &rec->backrefs);
3286         if (max_size > rec->max_size)
3287                 rec->max_size = max_size;
3288         return ref;
3289 }
3290
3291 static int add_extent_rec(struct cache_tree *extent_cache,
3292                           struct btrfs_key *parent_key, u64 parent_gen,
3293                           u64 start, u64 nr, u64 extent_item_refs,
3294                           int is_root, int inc_ref, int set_checked,
3295                           int metadata, int extent_rec, u64 max_size)
3296 {
3297         struct extent_record *rec;
3298         struct cache_extent *cache;
3299         int ret = 0;
3300         int dup = 0;
3301
3302         cache = lookup_cache_extent(extent_cache, start, nr);
3303         if (cache) {
3304                 rec = container_of(cache, struct extent_record, cache);
3305                 if (inc_ref)
3306                         rec->refs++;
3307                 if (rec->nr == 1)
3308                         rec->nr = max(nr, max_size);
3309
3310                 /*
3311                  * We need to make sure to reset nr to whatever the extent
3312                  * record says was the real size, this way we can compare it to
3313                  * the backrefs.
3314                  */
3315                 if (extent_rec) {
3316                         if (start != rec->start || rec->found_rec) {
3317                                 struct extent_record *tmp;
3318
3319                                 dup = 1;
3320                                 if (list_empty(&rec->list))
3321                                         list_add_tail(&rec->list,
3322                                                       &duplicate_extents);
3323
3324                                 /*
3325                                  * We have to do this song and dance in case we
3326                                  * find an extent record that falls inside of
3327                                  * our current extent record but does not have
3328                                  * the same objectid.
3329                                  */
3330                                 tmp = malloc(sizeof(*tmp));
3331                                 if (!tmp)
3332                                         return -ENOMEM;
3333                                 tmp->start = start;
3334                                 tmp->max_size = max_size;
3335                                 tmp->nr = nr;
3336                                 tmp->found_rec = 1;
3337                                 tmp->metadata = metadata;
3338                                 tmp->extent_item_refs = extent_item_refs;
3339                                 INIT_LIST_HEAD(&tmp->list);
3340                                 list_add_tail(&tmp->list, &rec->dups);
3341                                 rec->num_duplicates++;
3342                         } else {
3343                                 rec->nr = nr;
3344                                 rec->found_rec = 1;
3345                         }
3346                 }
3347
3348                 if (extent_item_refs && !dup) {
3349                         if (rec->extent_item_refs) {
3350                                 fprintf(stderr, "block %llu rec "
3351                                         "extent_item_refs %llu, passed %llu\n",
3352                                         (unsigned long long)start,
3353                                         (unsigned long long)
3354                                                         rec->extent_item_refs,
3355                                         (unsigned long long)extent_item_refs);
3356                         }
3357                         rec->extent_item_refs = extent_item_refs;
3358                 }
3359                 if (is_root)
3360                         rec->is_root = 1;
3361                 if (set_checked) {
3362                         rec->content_checked = 1;
3363                         rec->owner_ref_checked = 1;
3364                 }
3365
3366                 if (parent_key)
3367                         btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3368                 if (parent_gen)
3369                         rec->parent_generation = parent_gen;
3370
3371                 if (rec->max_size < max_size)
3372                         rec->max_size = max_size;
3373
3374                 maybe_free_extent_rec(extent_cache, rec);
3375                 return ret;
3376         }
3377         rec = malloc(sizeof(*rec));
3378         rec->start = start;
3379         rec->max_size = max_size;
3380         rec->nr = max(nr, max_size);
3381         rec->found_rec = !!extent_rec;
3382         rec->content_checked = 0;
3383         rec->owner_ref_checked = 0;
3384         rec->num_duplicates = 0;
3385         rec->metadata = metadata;
3386         INIT_LIST_HEAD(&rec->backrefs);
3387         INIT_LIST_HEAD(&rec->dups);
3388         INIT_LIST_HEAD(&rec->list);
3389
3390         if (is_root)
3391                 rec->is_root = 1;
3392         else
3393                 rec->is_root = 0;
3394
3395         if (inc_ref)
3396                 rec->refs = 1;
3397         else
3398                 rec->refs = 0;
3399
3400         if (extent_item_refs)
3401                 rec->extent_item_refs = extent_item_refs;
3402         else
3403                 rec->extent_item_refs = 0;
3404
3405         if (parent_key)
3406                 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3407         else
3408                 memset(&rec->parent_key, 0, sizeof(*parent_key));
3409
3410         if (parent_gen)
3411                 rec->parent_generation = parent_gen;
3412         else
3413                 rec->parent_generation = 0;
3414
3415         rec->cache.start = start;
3416         rec->cache.size = nr;
3417         ret = insert_cache_extent(extent_cache, &rec->cache);
3418         BUG_ON(ret);
3419         bytes_used += nr;
3420         if (set_checked) {
3421                 rec->content_checked = 1;
3422                 rec->owner_ref_checked = 1;
3423         }
3424         return ret;
3425 }
3426
3427 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
3428                             u64 parent, u64 root, int found_ref)
3429 {
3430         struct extent_record *rec;
3431         struct tree_backref *back;
3432         struct cache_extent *cache;
3433
3434         cache = lookup_cache_extent(extent_cache, bytenr, 1);
3435         if (!cache) {
3436                 add_extent_rec(extent_cache, NULL, 0, bytenr,
3437                                1, 0, 0, 0, 0, 1, 0, 0);
3438                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3439                 if (!cache)
3440                         abort();
3441         }
3442
3443         rec = container_of(cache, struct extent_record, cache);
3444         if (rec->start != bytenr) {
3445                 abort();
3446         }
3447
3448         back = find_tree_backref(rec, parent, root);
3449         if (!back)
3450                 back = alloc_tree_backref(rec, parent, root);
3451
3452         if (found_ref) {
3453                 if (back->node.found_ref) {
3454                         fprintf(stderr, "Extent back ref already exists "
3455                                 "for %llu parent %llu root %llu \n",
3456                                 (unsigned long long)bytenr,
3457                                 (unsigned long long)parent,
3458                                 (unsigned long long)root);
3459                 }
3460                 back->node.found_ref = 1;
3461         } else {
3462                 if (back->node.found_extent_tree) {
3463                         fprintf(stderr, "Extent back ref already exists "
3464                                 "for %llu parent %llu root %llu \n",
3465                                 (unsigned long long)bytenr,
3466                                 (unsigned long long)parent,
3467                                 (unsigned long long)root);
3468                 }
3469                 back->node.found_extent_tree = 1;
3470         }
3471         maybe_free_extent_rec(extent_cache, rec);
3472         return 0;
3473 }
3474
3475 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
3476                             u64 parent, u64 root, u64 owner, u64 offset,
3477                             u32 num_refs, int found_ref, u64 max_size)
3478 {
3479         struct extent_record *rec;
3480         struct data_backref *back;
3481         struct cache_extent *cache;
3482
3483         cache = lookup_cache_extent(extent_cache, bytenr, 1);
3484         if (!cache) {
3485                 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
3486                                0, 0, max_size);
3487                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3488                 if (!cache)
3489                         abort();
3490         }
3491
3492         rec = container_of(cache, struct extent_record, cache);
3493         if (rec->max_size < max_size)
3494                 rec->max_size = max_size;
3495
3496         /*
3497          * If found_ref is set then max_size is the real size and must match the
3498          * existing refs.  So if we have already found a ref then we need to
3499          * make sure that this ref matches the existing one, otherwise we need
3500          * to add a new backref so we can notice that the backrefs don't match
3501          * and we need to figure out who is telling the truth.  This is to
3502          * account for that awful fsync bug I introduced where we'd end up with
3503          * a btrfs_file_extent_item that would have its length include multiple
3504          * prealloc extents or point inside of a prealloc extent.
3505          */
3506         back = find_data_backref(rec, parent, root, owner, offset, found_ref,
3507                                  bytenr, max_size);
3508         if (!back)
3509                 back = alloc_data_backref(rec, parent, root, owner, offset,
3510                                           max_size);
3511
3512         if (found_ref) {
3513                 BUG_ON(num_refs != 1);
3514                 if (back->node.found_ref)
3515                         BUG_ON(back->bytes != max_size);
3516                 back->node.found_ref = 1;
3517                 back->found_ref += 1;
3518                 back->bytes = max_size;
3519                 back->disk_bytenr = bytenr;
3520                 rec->refs += 1;
3521                 rec->content_checked = 1;
3522                 rec->owner_ref_checked = 1;
3523         } else {
3524                 if (back->node.found_extent_tree) {
3525                         fprintf(stderr, "Extent back ref already exists "
3526                                 "for %llu parent %llu root %llu "
3527                                 "owner %llu offset %llu num_refs %lu\n",
3528                                 (unsigned long long)bytenr,
3529                                 (unsigned long long)parent,
3530                                 (unsigned long long)root,
3531                                 (unsigned long long)owner,
3532                                 (unsigned long long)offset,
3533                                 (unsigned long)num_refs);
3534                 }
3535                 back->num_refs = num_refs;
3536                 back->node.found_extent_tree = 1;
3537         }
3538         maybe_free_extent_rec(extent_cache, rec);
3539         return 0;
3540 }
3541
3542 static int add_pending(struct cache_tree *pending,
3543                        struct cache_tree *seen, u64 bytenr, u32 size)
3544 {
3545         int ret;
3546         ret = add_cache_extent(seen, bytenr, size);
3547         if (ret)
3548                 return ret;
3549         add_cache_extent(pending, bytenr, size);
3550         return 0;
3551 }
3552
3553 static int pick_next_pending(struct cache_tree *pending,
3554                         struct cache_tree *reada,
3555                         struct cache_tree *nodes,
3556                         u64 last, struct block_info *bits, int bits_nr,
3557                         int *reada_bits)
3558 {
3559         unsigned long node_start = last;
3560         struct cache_extent *cache;
3561         int ret;
3562
3563         cache = search_cache_extent(reada, 0);
3564         if (cache) {
3565                 bits[0].start = cache->start;
3566                 bits[0].size = cache->size;
3567                 *reada_bits = 1;
3568                 return 1;
3569         }
3570         *reada_bits = 0;
3571         if (node_start > 32768)
3572                 node_start -= 32768;
3573
3574         cache = search_cache_extent(nodes, node_start);
3575         if (!cache)
3576                 cache = search_cache_extent(nodes, 0);
3577
3578         if (!cache) {
3579                  cache = search_cache_extent(pending, 0);
3580                  if (!cache)
3581                          return 0;
3582                  ret = 0;
3583                  do {
3584                          bits[ret].start = cache->start;
3585                          bits[ret].size = cache->size;
3586                          cache = next_cache_extent(cache);
3587                          ret++;
3588                  } while (cache && ret < bits_nr);
3589                  return ret;
3590         }
3591
3592         ret = 0;
3593         do {
3594                 bits[ret].start = cache->start;
3595                 bits[ret].size = cache->size;
3596                 cache = next_cache_extent(cache);
3597                 ret++;
3598         } while (cache && ret < bits_nr);
3599
3600         if (bits_nr - ret > 8) {
3601                 u64 lookup = bits[0].start + bits[0].size;
3602                 struct cache_extent *next;
3603                 next = search_cache_extent(pending, lookup);
3604                 while(next) {
3605                         if (next->start - lookup > 32768)
3606                                 break;
3607                         bits[ret].start = next->start;
3608                         bits[ret].size = next->size;
3609                         lookup = next->start + next->size;
3610                         ret++;
3611                         if (ret == bits_nr)
3612                                 break;
3613                         next = next_cache_extent(next);
3614                         if (!next)
3615                                 break;
3616                 }
3617         }
3618         return ret;
3619 }
3620
3621 static void free_chunk_record(struct cache_extent *cache)
3622 {
3623         struct chunk_record *rec;
3624
3625         rec = container_of(cache, struct chunk_record, cache);
3626         list_del_init(&rec->list);
3627         list_del_init(&rec->dextents);
3628         free(rec);
3629 }
3630
3631 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
3632 {
3633         cache_tree_free_extents(chunk_cache, free_chunk_record);
3634 }
3635
3636 static void free_device_record(struct rb_node *node)
3637 {
3638         struct device_record *rec;
3639
3640         rec = container_of(node, struct device_record, node);
3641         free(rec);
3642 }
3643
3644 FREE_RB_BASED_TREE(device_cache, free_device_record);
3645
3646 int insert_block_group_record(struct block_group_tree *tree,
3647                               struct block_group_record *bg_rec)
3648 {
3649         int ret;
3650
3651         ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3652         if (ret)
3653                 return ret;
3654
3655         list_add_tail(&bg_rec->list, &tree->block_groups);
3656         return 0;
3657 }
3658
3659 static void free_block_group_record(struct cache_extent *cache)
3660 {
3661         struct block_group_record *rec;
3662
3663         rec = container_of(cache, struct block_group_record, cache);
3664         list_del_init(&rec->list);
3665         free(rec);
3666 }
3667
3668 void free_block_group_tree(struct block_group_tree *tree)
3669 {
3670         cache_tree_free_extents(&tree->tree, free_block_group_record);
3671 }
3672
3673 int insert_device_extent_record(struct device_extent_tree *tree,
3674                                 struct device_extent_record *de_rec)
3675 {
3676         int ret;
3677
3678         /*
3679          * Device extent is a bit different from the other extents, because
3680          * the extents which belong to the different devices may have the
3681          * same start and size, so we need use the special extent cache
3682          * search/insert functions.
3683          */
3684         ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3685         if (ret)
3686                 return ret;
3687
3688         list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3689         list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3690         return 0;
3691 }
3692
3693 static void free_device_extent_record(struct cache_extent *cache)
3694 {
3695         struct device_extent_record *rec;
3696
3697         rec = container_of(cache, struct device_extent_record, cache);
3698         if (!list_empty(&rec->chunk_list))
3699                 list_del_init(&rec->chunk_list);
3700         if (!list_empty(&rec->device_list))
3701                 list_del_init(&rec->device_list);
3702         free(rec);
3703 }
3704
3705 void free_device_extent_tree(struct device_extent_tree *tree)
3706 {
3707         cache_tree_free_extents(&tree->tree, free_device_extent_record);
3708 }
3709
3710 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3711 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3712                                  struct extent_buffer *leaf, int slot)
3713 {
3714         struct btrfs_extent_ref_v0 *ref0;
3715         struct btrfs_key key;
3716
3717         btrfs_item_key_to_cpu(leaf, &key, slot);
3718         ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3719         if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3720                 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3721         } else {
3722                 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3723                                  0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3724         }
3725         return 0;
3726 }
3727 #endif
3728
3729 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3730                                             struct btrfs_key *key,
3731                                             int slot)
3732 {
3733         struct btrfs_chunk *ptr;
3734         struct chunk_record *rec;
3735         int num_stripes, i;
3736
3737         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3738         num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3739
3740         rec = malloc(btrfs_chunk_record_size(num_stripes));
3741         if (!rec) {
3742                 fprintf(stderr, "memory allocation failed\n");
3743                 exit(-1);
3744         }
3745
3746         memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3747
3748         INIT_LIST_HEAD(&rec->list);
3749         INIT_LIST_HEAD(&rec->dextents);
3750         rec->bg_rec = NULL;
3751
3752         rec->cache.start = key->offset;
3753         rec->cache.size = btrfs_chunk_length(leaf, ptr);
3754
3755         rec->generation = btrfs_header_generation(leaf);
3756
3757         rec->objectid = key->objectid;
3758         rec->type = key->type;
3759         rec->offset = key->offset;
3760
3761         rec->length = rec->cache.size;
3762         rec->owner = btrfs_chunk_owner(leaf, ptr);
3763         rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3764         rec->type_flags = btrfs_chunk_type(leaf, ptr);
3765         rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3766         rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3767         rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3768         rec->num_stripes = num_stripes;
3769         rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3770
3771         for (i = 0; i < rec->num_stripes; ++i) {
3772                 rec->stripes[i].devid =
3773                         btrfs_stripe_devid_nr(leaf, ptr, i);
3774                 rec->stripes[i].offset =
3775                         btrfs_stripe_offset_nr(leaf, ptr, i);
3776                 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3777                                 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3778                                 BTRFS_UUID_SIZE);
3779         }
3780
3781         return rec;
3782 }
3783
3784 static int process_chunk_item(struct cache_tree *chunk_cache,
3785                               struct btrfs_key *key, struct extent_buffer *eb,
3786                               int slot)
3787 {
3788         struct chunk_record *rec;
3789         int ret = 0;
3790
3791         rec = btrfs_new_chunk_record(eb, key, slot);
3792         ret = insert_cache_extent(chunk_cache, &rec->cache);
3793         if (ret) {
3794                 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3795                         rec->offset, rec->length);
3796                 free(rec);
3797         }
3798
3799         return ret;
3800 }
3801
3802 static int process_device_item(struct rb_root *dev_cache,
3803                 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3804 {
3805         struct btrfs_dev_item *ptr;
3806         struct device_record *rec;
3807         int ret = 0;
3808
3809         ptr = btrfs_item_ptr(eb,
3810                 slot, struct btrfs_dev_item);
3811
3812         rec = malloc(sizeof(*rec));
3813         if (!rec) {
3814                 fprintf(stderr, "memory allocation failed\n");
3815                 return -ENOMEM;
3816         }
3817
3818         rec->devid = key->offset;
3819         rec->generation = btrfs_header_generation(eb);
3820
3821         rec->objectid = key->objectid;
3822         rec->type = key->type;
3823         rec->offset = key->offset;
3824
3825         rec->devid = btrfs_device_id(eb, ptr);
3826         rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3827         rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3828
3829         ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3830         if (ret) {
3831                 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3832                 free(rec);
3833         }
3834
3835         return ret;
3836 }
3837
3838 struct block_group_record *
3839 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3840                              int slot)
3841 {
3842         struct btrfs_block_group_item *ptr;
3843         struct block_group_record *rec;
3844
3845         rec = malloc(sizeof(*rec));
3846         if (!rec) {
3847                 fprintf(stderr, "memory allocation failed\n");
3848                 exit(-1);
3849         }
3850         memset(rec, 0, sizeof(*rec));
3851
3852         rec->cache.start = key->objectid;
3853         rec->cache.size = key->offset;
3854
3855         rec->generation = btrfs_header_generation(leaf);
3856
3857         rec->objectid = key->objectid;
3858         rec->type = key->type;
3859         rec->offset = key->offset;
3860
3861         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3862         rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3863
3864         INIT_LIST_HEAD(&rec->list);
3865
3866         return rec;
3867 }
3868
3869 static int process_block_group_item(struct block_group_tree *block_group_cache,
3870                                     struct btrfs_key *key,
3871                                     struct extent_buffer *eb, int slot)
3872 {
3873         struct block_group_record *rec;
3874         int ret = 0;
3875
3876         rec = btrfs_new_block_group_record(eb, key, slot);
3877         ret = insert_block_group_record(block_group_cache, rec);
3878         if (ret) {
3879                 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3880                         rec->objectid, rec->offset);
3881                 free(rec);
3882         }
3883
3884         return ret;
3885 }
3886
3887 struct device_extent_record *
3888 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3889                                struct btrfs_key *key, int slot)
3890 {
3891         struct device_extent_record *rec;
3892         struct btrfs_dev_extent *ptr;
3893
3894         rec = malloc(sizeof(*rec));
3895         if (!rec) {
3896                 fprintf(stderr, "memory allocation failed\n");
3897                 exit(-1);
3898         }
3899         memset(rec, 0, sizeof(*rec));
3900
3901         rec->cache.objectid = key->objectid;
3902         rec->cache.start = key->offset;
3903
3904         rec->generation = btrfs_header_generation(leaf);
3905
3906         rec->objectid = key->objectid;
3907         rec->type = key->type;
3908         rec->offset = key->offset;
3909
3910         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3911         rec->chunk_objecteid =
3912                 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3913         rec->chunk_offset =
3914                 btrfs_dev_extent_chunk_offset(leaf, ptr);
3915         rec->length = btrfs_dev_extent_length(leaf, ptr);
3916         rec->cache.size = rec->length;
3917
3918         INIT_LIST_HEAD(&rec->chunk_list);
3919         INIT_LIST_HEAD(&rec->device_list);
3920
3921         return rec;
3922 }
3923
3924 static int
3925 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3926                            struct btrfs_key *key, struct extent_buffer *eb,
3927                            int slot)
3928 {
3929         struct device_extent_record *rec;
3930         int ret;
3931
3932         rec = btrfs_new_device_extent_record(eb, key, slot);
3933         ret = insert_device_extent_record(dev_extent_cache, rec);
3934         if (ret) {
3935                 fprintf(stderr,
3936                         "Device extent[%llu, %llu, %llu] existed.\n",
3937                         rec->objectid, rec->offset, rec->length);
3938                 free(rec);
3939         }
3940
3941         return ret;
3942 }
3943
3944 static int process_extent_item(struct btrfs_root *root,
3945                                struct cache_tree *extent_cache,
3946                                struct extent_buffer *eb, int slot)
3947 {
3948         struct btrfs_extent_item *ei;
3949         struct btrfs_extent_inline_ref *iref;
3950         struct btrfs_extent_data_ref *dref;
3951         struct btrfs_shared_data_ref *sref;
3952         struct btrfs_key key;
3953         unsigned long end;
3954         unsigned long ptr;
3955         int type;
3956         u32 item_size = btrfs_item_size_nr(eb, slot);
3957         u64 refs = 0;
3958         u64 offset;
3959         u64 num_bytes;
3960         int metadata = 0;
3961
3962         btrfs_item_key_to_cpu(eb, &key, slot);
3963
3964         if (key.type == BTRFS_METADATA_ITEM_KEY) {
3965                 metadata = 1;
3966                 num_bytes = root->leafsize;
3967         } else {
3968                 num_bytes = key.offset;
3969         }
3970
3971         if (item_size < sizeof(*ei)) {
3972 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3973                 struct btrfs_extent_item_v0 *ei0;
3974                 BUG_ON(item_size != sizeof(*ei0));
3975                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3976                 refs = btrfs_extent_refs_v0(eb, ei0);
3977 #else
3978                 BUG();
3979 #endif
3980                 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3981                                       num_bytes, refs, 0, 0, 0, metadata, 1,
3982                                       num_bytes);
3983         }
3984
3985         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3986         refs = btrfs_extent_refs(eb, ei);
3987
3988         add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3989                        refs, 0, 0, 0, metadata, 1, num_bytes);
3990
3991         ptr = (unsigned long)(ei + 1);
3992         if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3993             key.type == BTRFS_EXTENT_ITEM_KEY)
3994                 ptr += sizeof(struct btrfs_tree_block_info);
3995
3996         end = (unsigned long)ei + item_size;
3997         while (ptr < end) {
3998                 iref = (struct btrfs_extent_inline_ref *)ptr;
3999                 type = btrfs_extent_inline_ref_type(eb, iref);
4000                 offset = btrfs_extent_inline_ref_offset(eb, iref);
4001                 switch (type) {
4002                 case BTRFS_TREE_BLOCK_REF_KEY:
4003                         add_tree_backref(extent_cache, key.objectid,
4004                                          0, offset, 0);
4005                         break;
4006                 case BTRFS_SHARED_BLOCK_REF_KEY:
4007                         add_tree_backref(extent_cache, key.objectid,
4008                                          offset, 0, 0);
4009                         break;
4010                 case BTRFS_EXTENT_DATA_REF_KEY:
4011                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
4012                         add_data_backref(extent_cache, key.objectid, 0,
4013                                         btrfs_extent_data_ref_root(eb, dref),
4014                                         btrfs_extent_data_ref_objectid(eb,
4015                                                                        dref),
4016                                         btrfs_extent_data_ref_offset(eb, dref),
4017                                         btrfs_extent_data_ref_count(eb, dref),
4018                                         0, num_bytes);
4019                         break;
4020                 case BTRFS_SHARED_DATA_REF_KEY:
4021                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
4022                         add_data_backref(extent_cache, key.objectid, offset,
4023                                         0, 0, 0,
4024                                         btrfs_shared_data_ref_count(eb, sref),
4025                                         0, num_bytes);
4026                         break;
4027                 default:
4028                         fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
4029                                 key.objectid, key.type, num_bytes);
4030                         goto out;
4031                 }
4032                 ptr += btrfs_extent_inline_ref_size(type);
4033         }
4034         WARN_ON(ptr > end);
4035 out:
4036         return 0;
4037 }
4038
4039 static int check_cache_range(struct btrfs_root *root,
4040                              struct btrfs_block_group_cache *cache,
4041                              u64 offset, u64 bytes)
4042 {
4043         struct btrfs_free_space *entry;
4044         u64 *logical;
4045         u64 bytenr;
4046         int stripe_len;
4047         int i, nr, ret;
4048
4049         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4050                 bytenr = btrfs_sb_offset(i);
4051                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
4052                                        cache->key.objectid, bytenr, 0,
4053                                        &logical, &nr, &stripe_len);
4054                 if (ret)
4055                         return ret;
4056
4057                 while (nr--) {
4058                         if (logical[nr] + stripe_len <= offset)
4059                                 continue;
4060                         if (offset + bytes <= logical[nr])
4061                                 continue;
4062                         if (logical[nr] == offset) {
4063                                 if (stripe_len >= bytes) {
4064                                         kfree(logical);
4065                                         return 0;
4066                                 }
4067                                 bytes -= stripe_len;
4068                                 offset += stripe_len;
4069                         } else if (logical[nr] < offset) {
4070                                 if (logical[nr] + stripe_len >=
4071                                     offset + bytes) {
4072                                         kfree(logical);
4073                                         return 0;
4074                                 }
4075                                 bytes = (offset + bytes) -
4076                                         (logical[nr] + stripe_len);
4077                                 offset = logical[nr] + stripe_len;
4078                         } else {
4079                                 /*
4080                                  * Could be tricky, the super may land in the
4081                                  * middle of the area we're checking.  First
4082                                  * check the easiest case, it's at the end.
4083                                  */
4084                                 if (logical[nr] + stripe_len >=
4085                                     bytes + offset) {
4086                                         bytes = logical[nr] - offset;
4087                                         continue;
4088                                 }
4089
4090                                 /* Check the left side */
4091                                 ret = check_cache_range(root, cache,
4092                                                         offset,
4093                                                         logical[nr] - offset);
4094                                 if (ret) {
4095                                         kfree(logical);
4096                                         return ret;
4097                                 }
4098
4099                                 /* Now we continue with the right side */
4100                                 bytes = (offset + bytes) -
4101                                         (logical[nr] + stripe_len);
4102                                 offset = logical[nr] + stripe_len;
4103                         }
4104                 }
4105
4106                 kfree(logical);
4107         }
4108
4109         entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
4110         if (!entry) {
4111                 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
4112                         offset, offset+bytes);
4113                 return -EINVAL;
4114         }
4115
4116         if (entry->offset != offset) {
4117                 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
4118                         entry->offset);
4119                 return -EINVAL;
4120         }
4121
4122         if (entry->bytes != bytes) {
4123                 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
4124                         bytes, entry->bytes, offset);
4125                 return -EINVAL;
4126         }
4127
4128         unlink_free_space(cache->free_space_ctl, entry);
4129         free(entry);
4130         return 0;
4131 }
4132
4133 static int verify_space_cache(struct btrfs_root *root,
4134                               struct btrfs_block_group_cache *cache)
4135 {
4136         struct btrfs_path *path;
4137         struct extent_buffer *leaf;
4138         struct btrfs_key key;
4139         u64 last;
4140         int ret = 0;
4141
4142         path = btrfs_alloc_path();
4143         if (!path)
4144                 return -ENOMEM;
4145
4146         root = root->fs_info->extent_root;
4147
4148         last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
4149
4150         key.objectid = last;
4151         key.offset = 0;
4152         key.type = BTRFS_EXTENT_ITEM_KEY;
4153
4154         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4155         if (ret < 0)
4156                 goto out;
4157         ret = 0;
4158         while (1) {
4159                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4160                         ret = btrfs_next_leaf(root, path);
4161                         if (ret < 0)
4162                                 goto out;
4163                         if (ret > 0) {
4164                                 ret = 0;
4165                                 break;
4166                         }
4167                 }
4168                 leaf = path->nodes[0];
4169                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4170                 if (key.objectid >= cache->key.offset + cache->key.objectid)
4171                         break;
4172                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
4173                     key.type != BTRFS_METADATA_ITEM_KEY) {
4174                         path->slots[0]++;
4175                         continue;
4176                 }
4177
4178                 if (last == key.objectid) {
4179                         if (key.type == BTRFS_EXTENT_ITEM_KEY)
4180                                 last = key.objectid + key.offset;
4181                         else
4182                                 last = key.objectid + root->leafsize;
4183                         path->slots[0]++;
4184                         continue;
4185                 }
4186
4187                 ret = check_cache_range(root, cache, last,
4188                                         key.objectid - last);
4189                 if (ret)
4190                         break;
4191                 if (key.type == BTRFS_EXTENT_ITEM_KEY)
4192                         last = key.objectid + key.offset;
4193                 else
4194                         last = key.objectid + root->leafsize;
4195                 path->slots[0]++;
4196         }
4197
4198         if (last < cache->key.objectid + cache->key.offset)
4199                 ret = check_cache_range(root, cache, last,
4200                                         cache->key.objectid +
4201                                         cache->key.offset - last);
4202
4203 out:
4204         btrfs_free_path(path);
4205
4206         if (!ret &&
4207             !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
4208                 fprintf(stderr, "There are still entries left in the space "
4209                         "cache\n");
4210                 ret = -EINVAL;
4211         }
4212
4213         return ret;
4214 }
4215
4216 static int check_space_cache(struct btrfs_root *root)
4217 {
4218         struct btrfs_block_group_cache *cache;
4219         u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
4220         int ret;
4221         int error = 0;
4222
4223         if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
4224             btrfs_super_generation(root->fs_info->super_copy) !=
4225             btrfs_super_cache_generation(root->fs_info->super_copy)) {
4226                 printf("cache and super generation don't match, space cache "
4227                        "will be invalidated\n");
4228                 return 0;
4229         }
4230
4231         while (1) {
4232                 cache = btrfs_lookup_first_block_group(root->fs_info, start);
4233                 if (!cache)
4234                         break;
4235
4236                 start = cache->key.objectid + cache->key.offset;
4237                 if (!cache->free_space_ctl) {
4238                         if (btrfs_init_free_space_ctl(cache,
4239                                                       root->sectorsize)) {
4240                                 ret = -ENOMEM;
4241                                 break;
4242                         }
4243                 } else {
4244                         btrfs_remove_free_space_cache(cache);
4245                 }
4246
4247                 ret = load_free_space_cache(root->fs_info, cache);
4248                 if (!ret)
4249                         continue;
4250
4251                 ret = verify_space_cache(root, cache);
4252                 if (ret) {
4253                         fprintf(stderr, "cache appears valid but isnt %Lu\n",
4254                                 cache->key.objectid);
4255                         error++;
4256                 }
4257         }
4258
4259         return error ? -EINVAL : 0;
4260 }
4261
4262 static int read_extent_data(struct btrfs_root *root, char *data,
4263                         u64 logical, u64 *len, int mirror)
4264 {
4265         u64 offset = 0;
4266         struct btrfs_multi_bio *multi = NULL;
4267         struct btrfs_fs_info *info = root->fs_info;
4268         struct btrfs_device *device;
4269         int ret = 0;
4270         u64 max_len = *len;
4271
4272         ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
4273                               &multi, mirror, NULL);
4274         if (ret) {
4275                 fprintf(stderr, "Couldn't map the block %llu\n",
4276                                 logical + offset);
4277                 goto err;
4278         }
4279         device = multi->stripes[0].dev;
4280
4281         if (device->fd == 0)
4282                 goto err;
4283         if (*len > max_len)
4284                 *len = max_len;
4285
4286         ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
4287         if (ret != *len)
4288                 ret = -EIO;
4289         else
4290                 ret = 0;
4291 err:
4292         kfree(multi);
4293         return ret;
4294 }
4295
4296 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
4297                         u64 num_bytes, unsigned long leaf_offset,
4298                         struct extent_buffer *eb) {
4299
4300         u64 offset = 0;
4301         u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4302         char *data;
4303         unsigned long csum_offset;
4304         u32 csum;
4305         u32 csum_expected;
4306         u64 read_len;
4307         u64 data_checked = 0;
4308         u64 tmp;
4309         int ret = 0;
4310         int mirror;
4311         int num_copies;
4312
4313         if (num_bytes % root->sectorsize)
4314                 return -EINVAL;
4315
4316         data = malloc(num_bytes);
4317         if (!data)
4318                 return -ENOMEM;
4319
4320         while (offset < num_bytes) {
4321                 mirror = 0;
4322 again:
4323                 read_len = num_bytes - offset;
4324                 /* read as much space once a time */
4325                 ret = read_extent_data(root, data + offset,
4326                                 bytenr + offset, &read_len, mirror);
4327                 if (ret)
4328                         goto out;
4329                 data_checked = 0;
4330                 /* verify every 4k data's checksum */
4331                 while (data_checked < read_len) {
4332                         csum = ~(u32)0;
4333                         tmp = offset + data_checked;
4334
4335                         csum = btrfs_csum_data(NULL, (char *)data + tmp,
4336                                                csum, root->sectorsize);
4337                         btrfs_csum_final(csum, (char *)&csum);
4338
4339                         csum_offset = leaf_offset +
4340                                  tmp / root->sectorsize * csum_size;
4341                         read_extent_buffer(eb, (char *)&csum_expected,
4342                                            csum_offset, csum_size);
4343                         /* try another mirror */
4344                         if (csum != csum_expected) {
4345                                 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
4346                                                 mirror, bytenr + tmp,
4347                                                 csum, csum_expected);
4348                                 num_copies = btrfs_num_copies(
4349                                                 &root->fs_info->mapping_tree,
4350                                                 bytenr, num_bytes);
4351                                 if (mirror < num_copies - 1) {
4352                                         mirror += 1;
4353                                         goto again;
4354                                 }
4355                         }
4356                         data_checked += root->sectorsize;
4357                 }
4358                 offset += read_len;
4359         }
4360 out:
4361         free(data);
4362         return ret;
4363 }
4364
4365 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
4366                                u64 num_bytes)
4367 {
4368         struct btrfs_path *path;
4369         struct extent_buffer *leaf;
4370         struct btrfs_key key;
4371         int ret;
4372
4373         path = btrfs_alloc_path();
4374         if (!path) {
4375                 fprintf(stderr, "Error allocing path\n");
4376                 return -ENOMEM;
4377         }
4378
4379         key.objectid = bytenr;
4380         key.type = BTRFS_EXTENT_ITEM_KEY;
4381         key.offset = (u64)-1;
4382
4383 again:
4384         ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
4385                                 0, 0);
4386         if (ret < 0) {
4387                 fprintf(stderr, "Error looking up extent record %d\n", ret);
4388                 btrfs_free_path(path);
4389                 return ret;
4390         } else if (ret) {
4391                 if (path->slots[0] > 0) {
4392                         path->slots[0]--;
4393                 } else {
4394                         ret = btrfs_prev_leaf(root, path);
4395                         if (ret < 0) {
4396                                 goto out;
4397                         } else if (ret > 0) {
4398                                 ret = 0;
4399                                 goto out;
4400                         }
4401                 }
4402         }
4403
4404         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4405
4406         /*
4407          * Block group items come before extent items if they have the same
4408          * bytenr, so walk back one more just in case.  Dear future traveler,
4409          * first congrats on mastering time travel.  Now if it's not too much
4410          * trouble could you go back to 2006 and tell Chris to make the
4411          * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
4412          * EXTENT_ITEM_KEY please?
4413          */
4414         while (key.type > BTRFS_EXTENT_ITEM_KEY) {
4415                 if (path->slots[0] > 0) {
4416                         path->slots[0]--;
4417                 } else {
4418                         ret = btrfs_prev_leaf(root, path);
4419                         if (ret < 0) {
4420                                 goto out;
4421                         } else if (ret > 0) {
4422                                 ret = 0;
4423                                 goto out;
4424                         }
4425                 }
4426                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4427         }
4428
4429         while (num_bytes) {
4430                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4431                         ret = btrfs_next_leaf(root, path);
4432                         if (ret < 0) {
4433                                 fprintf(stderr, "Error going to next leaf "
4434                                         "%d\n", ret);
4435                                 btrfs_free_path(path);
4436                                 return ret;
4437                         } else if (ret) {
4438                                 break;
4439                         }
4440                 }
4441                 leaf = path->nodes[0];
4442                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4443                 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
4444                         path->slots[0]++;
4445                         continue;
4446                 }
4447                 if (key.objectid + key.offset < bytenr) {
4448                         path->slots[0]++;
4449                         continue;
4450                 }
4451                 if (key.objectid > bytenr + num_bytes)
4452                         break;
4453
4454                 if (key.objectid == bytenr) {
4455                         if (key.offset >= num_bytes) {
4456                                 num_bytes = 0;
4457                                 break;
4458                         }
4459                         num_bytes -= key.offset;
4460                         bytenr += key.offset;
4461                 } else if (key.objectid < bytenr) {
4462                         if (key.objectid + key.offset >= bytenr + num_bytes) {
4463                                 num_bytes = 0;
4464                                 break;
4465                         }
4466                         num_bytes = (bytenr + num_bytes) -
4467                                 (key.objectid + key.offset);
4468                         bytenr = key.objectid + key.offset;
4469                 } else {
4470                         if (key.objectid + key.offset < bytenr + num_bytes) {
4471                                 u64 new_start = key.objectid + key.offset;
4472                                 u64 new_bytes = bytenr + num_bytes - new_start;
4473
4474                                 /*
4475                                  * Weird case, the extent is in the middle of
4476                                  * our range, we'll have to search one side
4477                                  * and then the other.  Not sure if this happens
4478                                  * in real life, but no harm in coding it up
4479                                  * anyway just in case.
4480                                  */
4481                                 btrfs_release_path(path);
4482                                 ret = check_extent_exists(root, new_start,
4483                                                           new_bytes);
4484                                 if (ret) {
4485                                         fprintf(stderr, "Right section didn't "
4486                                                 "have a record\n");
4487                                         break;
4488                                 }
4489                                 num_bytes = key.objectid - bytenr;
4490                                 goto again;
4491                         }
4492                         num_bytes = key.objectid - bytenr;
4493                 }
4494                 path->slots[0]++;
4495         }
4496         ret = 0;
4497
4498 out:
4499         if (num_bytes && !ret) {
4500                 fprintf(stderr, "There are no extents for csum range "
4501                         "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
4502                 ret = 1;
4503         }
4504
4505         btrfs_free_path(path);
4506         return ret;
4507 }
4508
4509 static int check_csums(struct btrfs_root *root)
4510 {
4511         struct btrfs_path *path;
4512         struct extent_buffer *leaf;
4513         struct btrfs_key key;
4514         u64 offset = 0, num_bytes = 0;
4515         u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4516         int errors = 0;
4517         int ret;
4518         u64 data_len;
4519         unsigned long leaf_offset;
4520
4521         root = root->fs_info->csum_root;
4522         if (!extent_buffer_uptodate(root->node)) {
4523                 fprintf(stderr, "No valid csum tree found\n");
4524                 return -ENOENT;
4525         }
4526
4527         key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
4528         key.type = BTRFS_EXTENT_CSUM_KEY;
4529         key.offset = 0;
4530
4531         path = btrfs_alloc_path();
4532         if (!path)
4533                 return -ENOMEM;
4534
4535         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4536         if (ret < 0) {
4537                 fprintf(stderr, "Error searching csum tree %d\n", ret);
4538                 btrfs_free_path(path);
4539                 return ret;
4540         }
4541
4542         if (ret > 0 && path->slots[0])
4543                 path->slots[0]--;
4544         ret = 0;
4545
4546         while (1) {
4547                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4548                         ret = btrfs_next_leaf(root, path);
4549                         if (ret < 0) {
4550                                 fprintf(stderr, "Error going to next leaf "
4551                                         "%d\n", ret);
4552                                 break;
4553                         }
4554                         if (ret)
4555                                 break;
4556                 }
4557                 leaf = path->nodes[0];
4558
4559                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4560                 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
4561                         path->slots[0]++;
4562                         continue;
4563                 }
4564
4565                 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
4566                               csum_size) * root->sectorsize;
4567                 if (!check_data_csum)
4568                         goto skip_csum_check;
4569                 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
4570                 ret = check_extent_csums(root, key.offset, data_len,
4571                                          leaf_offset, leaf);
4572                 if (ret)
4573                         break;
4574 skip_csum_check:
4575                 if (!num_bytes) {
4576                         offset = key.offset;
4577                 } else if (key.offset != offset + num_bytes) {
4578                         ret = check_extent_exists(root, offset, num_bytes);
4579                         if (ret) {
4580                                 fprintf(stderr, "Csum exists for %Lu-%Lu but "
4581                                         "there is no extent record\n",
4582                                         offset, offset+num_bytes);
4583                                 errors++;
4584                         }
4585                         offset = key.offset;
4586                         num_bytes = 0;
4587                 }
4588                 num_bytes += data_len;
4589                 path->slots[0]++;
4590         }
4591
4592         btrfs_free_path(path);
4593         return errors;
4594 }
4595
4596 static int is_dropped_key(struct btrfs_key *key,
4597                           struct btrfs_key *drop_key) {
4598         if (key->objectid < drop_key->objectid)
4599                 return 1;
4600         else if (key->objectid == drop_key->objectid) {
4601                 if (key->type < drop_key->type)
4602                         return 1;
4603                 else if (key->type == drop_key->type) {
4604                         if (key->offset < drop_key->offset)
4605                                 return 1;
4606                 }
4607         }
4608         return 0;
4609 }
4610
4611 static int run_next_block(struct btrfs_trans_handle *trans,
4612                           struct btrfs_root *root,
4613                           struct block_info *bits,
4614                           int bits_nr,
4615                           u64 *last,
4616                           struct cache_tree *pending,
4617                           struct cache_tree *seen,
4618                           struct cache_tree *reada,
4619                           struct cache_tree *nodes,
4620                           struct cache_tree *extent_cache,
4621                           struct cache_tree *chunk_cache,
4622                           struct rb_root *dev_cache,
4623                           struct block_group_tree *block_group_cache,
4624                           struct device_extent_tree *dev_extent_cache,
4625                           struct btrfs_root_item *ri)
4626 {
4627         struct extent_buffer *buf;
4628         u64 bytenr;
4629         u32 size;
4630         u64 parent;
4631         u64 owner;
4632         u64 flags;
4633         u64 ptr;
4634         u64 gen = 0;
4635         int ret = 0;
4636         int i;
4637         int nritems;
4638         struct btrfs_key key;
4639         struct cache_extent *cache;
4640         int reada_bits;
4641
4642         nritems = pick_next_pending(pending, reada, nodes, *last, bits,
4643                                     bits_nr, &reada_bits);
4644         if (nritems == 0)
4645                 return 1;
4646
4647         if (!reada_bits) {
4648                 for(i = 0; i < nritems; i++) {
4649                         ret = add_cache_extent(reada, bits[i].start,
4650                                                bits[i].size);
4651                         if (ret == -EEXIST)
4652                                 continue;
4653
4654                         /* fixme, get the parent transid */
4655                         readahead_tree_block(root, bits[i].start,
4656                                              bits[i].size, 0);
4657                 }
4658         }
4659         *last = bits[0].start;
4660         bytenr = bits[0].start;
4661         size = bits[0].size;
4662
4663         cache = lookup_cache_extent(pending, bytenr, size);
4664         if (cache) {
4665                 remove_cache_extent(pending, cache);
4666                 free(cache);
4667         }
4668         cache = lookup_cache_extent(reada, bytenr, size);
4669         if (cache) {
4670                 remove_cache_extent(reada, cache);
4671                 free(cache);
4672         }
4673         cache = lookup_cache_extent(nodes, bytenr, size);
4674         if (cache) {
4675                 remove_cache_extent(nodes, cache);
4676                 free(cache);
4677         }
4678         cache = lookup_cache_extent(extent_cache, bytenr, size);
4679         if (cache) {
4680                 struct extent_record *rec;
4681
4682                 rec = container_of(cache, struct extent_record, cache);
4683                 gen = rec->parent_generation;
4684         }
4685
4686         /* fixme, get the real parent transid */
4687         buf = read_tree_block(root, bytenr, size, gen);
4688         if (!extent_buffer_uptodate(buf)) {
4689                 record_bad_block_io(root->fs_info,
4690                                     extent_cache, bytenr, size);
4691                 goto out;
4692         }
4693
4694         nritems = btrfs_header_nritems(buf);
4695
4696         /*
4697          * FIXME, this only works only if we don't have any full
4698          * backref mode.
4699          */
4700         if (!init_extent_tree) {
4701                 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
4702                                        btrfs_header_level(buf), 1, NULL,
4703                                        &flags);
4704                 if (ret < 0)
4705                         flags = 0;
4706         } else {
4707                 flags = 0;
4708         }
4709
4710         if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4711                 parent = bytenr;
4712                 owner = 0;
4713         } else {
4714                 parent = 0;
4715                 owner = btrfs_header_owner(buf);
4716         }
4717
4718         ret = check_block(trans, root, extent_cache, buf, flags);
4719         if (ret)
4720                 goto out;
4721
4722         if (btrfs_is_leaf(buf)) {
4723                 btree_space_waste += btrfs_leaf_free_space(root, buf);
4724                 for (i = 0; i < nritems; i++) {
4725                         struct btrfs_file_extent_item *fi;
4726                         btrfs_item_key_to_cpu(buf, &key, i);
4727                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
4728                                 process_extent_item(root, extent_cache, buf,
4729                                                     i);
4730                                 continue;
4731                         }
4732                         if (key.type == BTRFS_METADATA_ITEM_KEY) {
4733                                 process_extent_item(root, extent_cache, buf,
4734                                                     i);
4735                                 continue;
4736                         }
4737                         if (key.type == BTRFS_EXTENT_CSUM_KEY) {
4738                                 total_csum_bytes +=
4739                                         btrfs_item_size_nr(buf, i);
4740                                 continue;
4741                         }
4742                         if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4743                                 process_chunk_item(chunk_cache, &key, buf, i);
4744                                 continue;
4745                         }
4746                         if (key.type == BTRFS_DEV_ITEM_KEY) {
4747                                 process_device_item(dev_cache, &key, buf, i);
4748                                 continue;
4749                         }
4750                         if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4751                                 process_block_group_item(block_group_cache,
4752                                         &key, buf, i);
4753                                 continue;
4754                         }
4755                         if (key.type == BTRFS_DEV_EXTENT_KEY) {
4756                                 process_device_extent_item(dev_extent_cache,
4757                                         &key, buf, i);
4758                                 continue;
4759
4760                         }
4761                         if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
4762 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4763                                 process_extent_ref_v0(extent_cache, buf, i);
4764 #else
4765                                 BUG();
4766 #endif
4767                                 continue;
4768                         }
4769
4770                         if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
4771                                 add_tree_backref(extent_cache, key.objectid, 0,
4772                                                  key.offset, 0);
4773                                 continue;
4774                         }
4775                         if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
4776                                 add_tree_backref(extent_cache, key.objectid,
4777                                                  key.offset, 0, 0);
4778                                 continue;
4779                         }
4780                         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
4781                                 struct btrfs_extent_data_ref *ref;
4782                                 ref = btrfs_item_ptr(buf, i,
4783                                                 struct btrfs_extent_data_ref);
4784                                 add_data_backref(extent_cache,
4785                                         key.objectid, 0,
4786                                         btrfs_extent_data_ref_root(buf, ref),
4787                                         btrfs_extent_data_ref_objectid(buf,
4788                                                                        ref),
4789                                         btrfs_extent_data_ref_offset(buf, ref),
4790                                         btrfs_extent_data_ref_count(buf, ref),
4791                                         0, root->sectorsize);
4792                                 continue;
4793                         }
4794                         if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4795                                 struct btrfs_shared_data_ref *ref;
4796                                 ref = btrfs_item_ptr(buf, i,
4797                                                 struct btrfs_shared_data_ref);
4798                                 add_data_backref(extent_cache,
4799                                         key.objectid, key.offset, 0, 0, 0,
4800                                         btrfs_shared_data_ref_count(buf, ref),
4801                                         0, root->sectorsize);
4802                                 continue;
4803                         }
4804                         if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4805                                 struct bad_item *bad;
4806
4807                                 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4808                                         continue;
4809                                 if (!owner)
4810                                         continue;
4811                                 bad = malloc(sizeof(struct bad_item));
4812                                 if (!bad)
4813                                         continue;
4814                                 INIT_LIST_HEAD(&bad->list);
4815                                 memcpy(&bad->key, &key,
4816                                        sizeof(struct btrfs_key));
4817                                 bad->root_id = owner;
4818                                 list_add_tail(&bad->list, &delete_items);
4819                                 continue;
4820                         }
4821                         if (key.type != BTRFS_EXTENT_DATA_KEY)
4822                                 continue;
4823                         fi = btrfs_item_ptr(buf, i,
4824                                             struct btrfs_file_extent_item);
4825                         if (btrfs_file_extent_type(buf, fi) ==
4826                             BTRFS_FILE_EXTENT_INLINE)
4827                                 continue;
4828                         if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4829                                 continue;
4830
4831                         data_bytes_allocated +=
4832                                 btrfs_file_extent_disk_num_bytes(buf, fi);
4833                         if (data_bytes_allocated < root->sectorsize) {
4834                                 abort();
4835                         }
4836                         data_bytes_referenced +=
4837                                 btrfs_file_extent_num_bytes(buf, fi);
4838                         add_data_backref(extent_cache,
4839                                 btrfs_file_extent_disk_bytenr(buf, fi),
4840                                 parent, owner, key.objectid, key.offset -
4841                                 btrfs_file_extent_offset(buf, fi), 1, 1,
4842                                 btrfs_file_extent_disk_num_bytes(buf, fi));
4843                 }
4844         } else {
4845                 int level;
4846                 struct btrfs_key first_key;
4847
4848                 first_key.objectid = 0;
4849
4850                 if (nritems > 0)
4851                         btrfs_item_key_to_cpu(buf, &first_key, 0);
4852                 level = btrfs_header_level(buf);
4853                 for (i = 0; i < nritems; i++) {
4854                         ptr = btrfs_node_blockptr(buf, i);
4855                         size = btrfs_level_size(root, level - 1);
4856                         btrfs_node_key_to_cpu(buf, &key, i);
4857                         if (ri != NULL) {
4858                                 struct btrfs_key drop_key;
4859                                 btrfs_disk_key_to_cpu(&drop_key,
4860                                                       &ri->drop_progress);
4861                                 if ((level == ri->drop_level)
4862                                     && is_dropped_key(&key, &drop_key)) {
4863                                         continue;
4864                                 }
4865                         }
4866                         ret = add_extent_rec(extent_cache, &key,
4867                                              btrfs_node_ptr_generation(buf, i),
4868                                              ptr, size, 0, 0, 1, 0, 1, 0,
4869                                              size);
4870                         BUG_ON(ret);
4871
4872                         add_tree_backref(extent_cache, ptr, parent, owner, 1);
4873
4874                         if (level > 1) {
4875                                 add_pending(nodes, seen, ptr, size);
4876                         } else {
4877                                 add_pending(pending, seen, ptr, size);
4878                         }
4879                 }
4880                 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4881                                       nritems) * sizeof(struct btrfs_key_ptr);
4882         }
4883         total_btree_bytes += buf->len;
4884         if (fs_root_objectid(btrfs_header_owner(buf)))
4885                 total_fs_tree_bytes += buf->len;
4886         if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4887                 total_extent_tree_bytes += buf->len;
4888         if (!found_old_backref &&
4889             btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4890             btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4891             !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4892                 found_old_backref = 1;
4893 out:
4894         free_extent_buffer(buf);
4895         return ret;
4896 }
4897
4898 static int add_root_to_pending(struct extent_buffer *buf,
4899                                struct cache_tree *extent_cache,
4900                                struct cache_tree *pending,
4901                                struct cache_tree *seen,
4902                                struct cache_tree *nodes,
4903                                struct btrfs_key *root_key)
4904 {
4905         if (btrfs_header_level(buf) > 0)
4906                 add_pending(nodes, seen, buf->start, buf->len);
4907         else
4908                 add_pending(pending, seen, buf->start, buf->len);
4909         add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4910                        0, 1, 1, 0, 1, 0, buf->len);
4911
4912         if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4913             btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4914                 add_tree_backref(extent_cache, buf->start, buf->start,
4915                                  0, 1);
4916         else
4917                 add_tree_backref(extent_cache, buf->start, 0,
4918                                  root_key->objectid, 1);
4919         return 0;
4920 }
4921
4922 /* as we fix the tree, we might be deleting blocks that
4923  * we're tracking for repair.  This hook makes sure we
4924  * remove any backrefs for blocks as we are fixing them.
4925  */
4926 static int free_extent_hook(struct btrfs_trans_handle *trans,
4927                             struct btrfs_root *root,
4928                             u64 bytenr, u64 num_bytes, u64 parent,
4929                             u64 root_objectid, u64 owner, u64 offset,
4930                             int refs_to_drop)
4931 {
4932         struct extent_record *rec;
4933         struct cache_extent *cache;
4934         int is_data;
4935         struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4936
4937         is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4938         cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4939         if (!cache)
4940                 return 0;
4941
4942         rec = container_of(cache, struct extent_record, cache);
4943         if (is_data) {
4944                 struct data_backref *back;
4945                 back = find_data_backref(rec, parent, root_objectid, owner,
4946                                          offset, 1, bytenr, num_bytes);
4947                 if (!back)
4948                         goto out;
4949                 if (back->node.found_ref) {
4950                         back->found_ref -= refs_to_drop;
4951                         if (rec->refs)
4952                                 rec->refs -= refs_to_drop;
4953                 }
4954                 if (back->node.found_extent_tree) {
4955                         back->num_refs -= refs_to_drop;
4956                         if (rec->extent_item_refs)
4957                                 rec->extent_item_refs -= refs_to_drop;
4958                 }
4959                 if (back->found_ref == 0)
4960                         back->node.found_ref = 0;
4961                 if (back->num_refs == 0)
4962                         back->node.found_extent_tree = 0;
4963
4964                 if (!back->node.found_extent_tree && back->node.found_ref) {
4965                         list_del(&back->node.list);
4966                         free(back);
4967                 }
4968         } else {
4969                 struct tree_backref *back;
4970                 back = find_tree_backref(rec, parent, root_objectid);
4971                 if (!back)
4972                         goto out;
4973                 if (back->node.found_ref) {
4974                         if (rec->refs)
4975                                 rec->refs--;
4976                         back->node.found_ref = 0;
4977                 }
4978                 if (back->node.found_extent_tree) {
4979                         if (rec->extent_item_refs)
4980                                 rec->extent_item_refs--;
4981                         back->node.found_extent_tree = 0;
4982                 }
4983                 if (!back->node.found_extent_tree && back->node.found_ref) {
4984                         list_del(&back->node.list);
4985                         free(back);
4986                 }
4987         }
4988         maybe_free_extent_rec(extent_cache, rec);
4989 out:
4990         return 0;
4991 }
4992
4993 static int delete_extent_records(struct btrfs_trans_handle *trans,
4994                                  struct btrfs_root *root,
4995                                  struct btrfs_path *path,
4996                                  u64 bytenr, u64 new_len)
4997 {
4998         struct btrfs_key key;
4999         struct btrfs_key found_key;
5000         struct extent_buffer *leaf;
5001         int ret;
5002         int slot;
5003
5004
5005         key.objectid = bytenr;
5006         key.type = (u8)-1;
5007         key.offset = (u64)-1;
5008
5009         while(1) {
5010                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
5011                                         &key, path, 0, 1);
5012                 if (ret < 0)
5013                         break;
5014
5015                 if (ret > 0) {
5016                         ret = 0;
5017                         if (path->slots[0] == 0)
5018                                 break;
5019                         path->slots[0]--;
5020                 }
5021                 ret = 0;
5022
5023                 leaf = path->nodes[0];
5024                 slot = path->slots[0];
5025
5026                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5027                 if (found_key.objectid != bytenr)
5028                         break;
5029
5030                 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
5031                     found_key.type != BTRFS_METADATA_ITEM_KEY &&
5032                     found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5033                     found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
5034                     found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
5035                     found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
5036                     found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
5037                         btrfs_release_path(path);
5038                         if (found_key.type == 0) {
5039                                 if (found_key.offset == 0)
5040                                         break;
5041                                 key.offset = found_key.offset - 1;
5042                                 key.type = found_key.type;
5043                         }
5044                         key.type = found_key.type - 1;
5045                         key.offset = (u64)-1;
5046                         continue;
5047                 }
5048
5049                 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
5050                         found_key.objectid, found_key.type, found_key.offset);
5051
5052                 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
5053                 if (ret)
5054                         break;
5055                 btrfs_release_path(path);
5056
5057                 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5058                     found_key.type == BTRFS_METADATA_ITEM_KEY) {
5059                         u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
5060                                 found_key.offset : root->leafsize;
5061
5062                         ret = btrfs_update_block_group(trans, root, bytenr,
5063                                                        bytes, 0, 0);
5064                         if (ret)
5065                                 break;
5066                 }
5067         }
5068
5069         btrfs_release_path(path);
5070         return ret;
5071 }
5072
5073 /*
5074  * for a single backref, this will allocate a new extent
5075  * and add the backref to it.
5076  */
5077 static int record_extent(struct btrfs_trans_handle *trans,
5078                          struct btrfs_fs_info *info,
5079                          struct btrfs_path *path,
5080                          struct extent_record *rec,
5081                          struct extent_backref *back,
5082                          int allocated, u64 flags)
5083 {
5084         int ret;
5085         struct btrfs_root *extent_root = info->extent_root;
5086         struct extent_buffer *leaf;
5087         struct btrfs_key ins_key;
5088         struct btrfs_extent_item *ei;
5089         struct tree_backref *tback;
5090         struct data_backref *dback;
5091         struct btrfs_tree_block_info *bi;
5092
5093         if (!back->is_data)
5094                 rec->max_size = max_t(u64, rec->max_size,
5095                                     info->extent_root->leafsize);
5096
5097         if (!allocated) {
5098                 u32 item_size = sizeof(*ei);
5099
5100                 if (!back->is_data)
5101                         item_size += sizeof(*bi);
5102
5103                 ins_key.objectid = rec->start;
5104                 ins_key.offset = rec->max_size;
5105                 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
5106
5107                 ret = btrfs_insert_empty_item(trans, extent_root, path,
5108                                         &ins_key, item_size);
5109                 if (ret)
5110                         goto fail;
5111
5112                 leaf = path->nodes[0];
5113                 ei = btrfs_item_ptr(leaf, path->slots[0],
5114                                     struct btrfs_extent_item);
5115
5116                 btrfs_set_extent_refs(leaf, ei, 0);
5117                 btrfs_set_extent_generation(leaf, ei, rec->generation);
5118
5119                 if (back->is_data) {
5120                         btrfs_set_extent_flags(leaf, ei,
5121                                                BTRFS_EXTENT_FLAG_DATA);
5122                 } else {
5123                         struct btrfs_disk_key copy_key;;
5124
5125                         tback = (struct tree_backref *)back;
5126                         bi = (struct btrfs_tree_block_info *)(ei + 1);
5127                         memset_extent_buffer(leaf, 0, (unsigned long)bi,
5128                                              sizeof(*bi));
5129
5130                         btrfs_set_disk_key_objectid(&copy_key,
5131                                                     rec->info_objectid);
5132                         btrfs_set_disk_key_type(&copy_key, 0);
5133                         btrfs_set_disk_key_offset(&copy_key, 0);
5134
5135                         btrfs_set_tree_block_level(leaf, bi, rec->info_level);
5136                         btrfs_set_tree_block_key(leaf, bi, &copy_key);
5137
5138                         btrfs_set_extent_flags(leaf, ei,
5139                                                BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
5140                 }
5141
5142                 btrfs_mark_buffer_dirty(leaf);
5143                 ret = btrfs_update_block_group(trans, extent_root, rec->start,
5144                                                rec->max_size, 1, 0);
5145                 if (ret)
5146                         goto fail;
5147                 btrfs_release_path(path);
5148         }
5149
5150         if (back->is_data) {
5151                 u64 parent;
5152                 int i;
5153
5154                 dback = (struct data_backref *)back;
5155                 if (back->full_backref)
5156                         parent = dback->parent;
5157                 else
5158                         parent = 0;
5159
5160                 for (i = 0; i < dback->found_ref; i++) {
5161                         /* if parent != 0, we're doing a full backref
5162                          * passing BTRFS_FIRST_FREE_OBJECTID as the owner
5163                          * just makes the backref allocator create a data
5164                          * backref
5165                          */
5166                         ret = btrfs_inc_extent_ref(trans, info->extent_root,
5167                                                    rec->start, rec->max_size,
5168                                                    parent,
5169                                                    dback->root,
5170                                                    parent ?
5171                                                    BTRFS_FIRST_FREE_OBJECTID :
5172                                                    dback->owner,
5173                                                    dback->offset);
5174                         if (ret)
5175                                 break;
5176                 }
5177                 fprintf(stderr, "adding new data backref"
5178                                 " on %llu %s %llu owner %llu"
5179                                 " offset %llu found %d\n",
5180                                 (unsigned long long)rec->start,
5181                                 back->full_backref ?
5182                                 "parent" : "root",
5183                                 back->full_backref ?
5184                                 (unsigned long long)parent :
5185                                 (unsigned long long)dback->root,
5186                                 (unsigned long long)dback->owner,
5187                                 (unsigned long long)dback->offset,
5188                                 dback->found_ref);
5189         } else {
5190                 u64 parent;
5191
5192                 tback = (struct tree_backref *)back;
5193                 if (back->full_backref)
5194                         parent = tback->parent;
5195                 else
5196                         parent = 0;
5197
5198                 ret = btrfs_inc_extent_ref(trans, info->extent_root,
5199                                            rec->start, rec->max_size,
5200                                            parent, tback->root, 0, 0);
5201                 fprintf(stderr, "adding new tree backref on "
5202                         "start %llu len %llu parent %llu root %llu\n",
5203                         rec->start, rec->max_size, tback->parent, tback->root);
5204         }
5205         if (ret)
5206                 goto fail;
5207 fail:
5208         btrfs_release_path(path);
5209         return ret;
5210 }
5211
5212 struct extent_entry {
5213         u64 bytenr;
5214         u64 bytes;
5215         int count;
5216         int broken;
5217         struct list_head list;
5218 };
5219
5220 static struct extent_entry *find_entry(struct list_head *entries,
5221                                        u64 bytenr, u64 bytes)
5222 {
5223         struct extent_entry *entry = NULL;
5224
5225         list_for_each_entry(entry, entries, list) {
5226                 if (entry->bytenr == bytenr && entry->bytes == bytes)
5227                         return entry;
5228         }
5229
5230         return NULL;
5231 }
5232
5233 static struct extent_entry *find_most_right_entry(struct list_head *entries)
5234 {
5235         struct extent_entry *entry, *best = NULL, *prev = NULL;
5236
5237         list_for_each_entry(entry, entries, list) {
5238                 if (!prev) {
5239                         prev = entry;
5240                         continue;
5241                 }
5242
5243                 /*
5244                  * If there are as many broken entries as entries then we know
5245                  * not to trust this particular entry.
5246                  */
5247                 if (entry->broken == entry->count)
5248                         continue;
5249
5250                 /*
5251                  * If our current entry == best then we can't be sure our best
5252                  * is really the best, so we need to keep searching.
5253                  */
5254                 if (best && best->count == entry->count) {
5255                         prev = entry;
5256                         best = NULL;
5257                         continue;
5258                 }
5259
5260                 /* Prev == entry, not good enough, have to keep searching */
5261                 if (!prev->broken && prev->count == entry->count)
5262                         continue;
5263
5264                 if (!best)
5265                         best = (prev->count > entry->count) ? prev : entry;
5266                 else if (best->count < entry->count)
5267                         best = entry;
5268                 prev = entry;
5269         }
5270
5271         return best;
5272 }
5273
5274 static int repair_ref(struct btrfs_trans_handle *trans,
5275                       struct btrfs_fs_info *info, struct btrfs_path *path,
5276                       struct data_backref *dback, struct extent_entry *entry)
5277 {
5278         struct btrfs_root *root;
5279         struct btrfs_file_extent_item *fi;
5280         struct extent_buffer *leaf;
5281         struct btrfs_key key;
5282         u64 bytenr, bytes;
5283         int ret;
5284
5285         key.objectid = dback->root;
5286         key.type = BTRFS_ROOT_ITEM_KEY;
5287         key.offset = (u64)-1;
5288         root = btrfs_read_fs_root(info, &key);
5289         if (IS_ERR(root)) {
5290                 fprintf(stderr, "Couldn't find root for our ref\n");
5291                 return -EINVAL;
5292         }
5293
5294         /*
5295          * The backref points to the original offset of the extent if it was
5296          * split, so we need to search down to the offset we have and then walk
5297          * forward until we find the backref we're looking for.
5298          */
5299         key.objectid = dback->owner;
5300         key.type = BTRFS_EXTENT_DATA_KEY;
5301         key.offset = dback->offset;
5302         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5303         if (ret < 0) {
5304                 fprintf(stderr, "Error looking up ref %d\n", ret);
5305                 return ret;
5306         }
5307
5308         while (1) {
5309                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5310                         ret = btrfs_next_leaf(root, path);
5311                         if (ret) {
5312                                 fprintf(stderr, "Couldn't find our ref, next\n");
5313                                 return -EINVAL;
5314                         }
5315                 }
5316                 leaf = path->nodes[0];
5317                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5318                 if (key.objectid != dback->owner ||
5319                     key.type != BTRFS_EXTENT_DATA_KEY) {
5320                         fprintf(stderr, "Couldn't find our ref, search\n");
5321                         return -EINVAL;
5322                 }
5323                 fi = btrfs_item_ptr(leaf, path->slots[0],
5324                                     struct btrfs_file_extent_item);
5325                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5326                 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5327
5328                 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
5329                         break;
5330                 path->slots[0]++;
5331         }
5332
5333         btrfs_release_path(path);
5334
5335         /*
5336          * Have to make sure that this root gets updated when we commit the
5337          * transaction
5338          */
5339         record_root_in_trans(trans, root);
5340
5341         /*
5342          * Ok we have the key of the file extent we want to fix, now we can cow
5343          * down to the thing and fix it.
5344          */
5345         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5346         if (ret < 0) {
5347                 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
5348                         key.objectid, key.type, key.offset, ret);
5349                 return ret;
5350         }
5351         if (ret > 0) {
5352                 fprintf(stderr, "Well that's odd, we just found this key "
5353                         "[%Lu, %u, %Lu]\n", key.objectid, key.type,
5354                         key.offset);
5355                 return -EINVAL;
5356         }
5357         leaf = path->nodes[0];
5358         fi = btrfs_item_ptr(leaf, path->slots[0],
5359                             struct btrfs_file_extent_item);
5360
5361         if (btrfs_file_extent_compression(leaf, fi) &&
5362             dback->disk_bytenr != entry->bytenr) {
5363                 fprintf(stderr, "Ref doesn't match the record start and is "
5364                         "compressed, please take a btrfs-image of this file "
5365                         "system and send it to a btrfs developer so they can "
5366                         "complete this functionality for bytenr %Lu\n",
5367                         dback->disk_bytenr);
5368                 return -EINVAL;
5369         }
5370
5371         if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
5372                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5373         } else if (dback->disk_bytenr > entry->bytenr) {
5374                 u64 off_diff, offset;
5375
5376                 off_diff = dback->disk_bytenr - entry->bytenr;
5377                 offset = btrfs_file_extent_offset(leaf, fi);
5378                 if (dback->disk_bytenr + offset +
5379                     btrfs_file_extent_num_bytes(leaf, fi) >
5380                     entry->bytenr + entry->bytes) {
5381                         fprintf(stderr, "Ref is past the entry end, please "
5382                                 "take a btrfs-image of this file system and "
5383                                 "send it to a btrfs developer, ref %Lu\n",
5384                                 dback->disk_bytenr);
5385                         return -EINVAL;
5386                 }
5387                 offset += off_diff;
5388                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5389                 btrfs_set_file_extent_offset(leaf, fi, offset);
5390         } else if (dback->disk_bytenr < entry->bytenr) {
5391                 u64 offset;
5392
5393                 offset = btrfs_file_extent_offset(leaf, fi);
5394                 if (dback->disk_bytenr + offset < entry->bytenr) {
5395                         fprintf(stderr, "Ref is before the entry start, please"
5396                                 " take a btrfs-image of this file system and "
5397                                 "send it to a btrfs developer, ref %Lu\n",
5398                                 dback->disk_bytenr);
5399                         return -EINVAL;
5400                 }
5401
5402                 offset += dback->disk_bytenr;
5403                 offset -= entry->bytenr;
5404                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5405                 btrfs_set_file_extent_offset(leaf, fi, offset);
5406         }
5407
5408         btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
5409
5410         /*
5411          * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
5412          * only do this if we aren't using compression, otherwise it's a
5413          * trickier case.
5414          */
5415         if (!btrfs_file_extent_compression(leaf, fi))
5416                 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
5417         else
5418                 printf("ram bytes may be wrong?\n");
5419         btrfs_mark_buffer_dirty(leaf);
5420         btrfs_release_path(path);
5421         return 0;
5422 }
5423
5424 static int verify_backrefs(struct btrfs_trans_handle *trans,
5425                            struct btrfs_fs_info *info, struct btrfs_path *path,
5426                            struct extent_record *rec)
5427 {
5428         struct extent_backref *back;
5429         struct data_backref *dback;
5430         struct extent_entry *entry, *best = NULL;
5431         LIST_HEAD(entries);
5432         int nr_entries = 0;
5433         int broken_entries = 0;
5434         int ret = 0;
5435         short mismatch = 0;
5436
5437         /*
5438          * Metadata is easy and the backrefs should always agree on bytenr and
5439          * size, if not we've got bigger issues.
5440          */
5441         if (rec->metadata)
5442                 return 0;
5443
5444         list_for_each_entry(back, &rec->backrefs, list) {
5445                 if (back->full_backref || !back->is_data)
5446                         continue;
5447
5448                 dback = (struct data_backref *)back;
5449
5450                 /*
5451                  * We only pay attention to backrefs that we found a real
5452                  * backref for.
5453                  */
5454                 if (dback->found_ref == 0)
5455                         continue;
5456
5457                 /*
5458                  * For now we only catch when the bytes don't match, not the
5459                  * bytenr.  We can easily do this at the same time, but I want
5460                  * to have a fs image to test on before we just add repair
5461                  * functionality willy-nilly so we know we won't screw up the
5462                  * repair.
5463                  */
5464
5465                 entry = find_entry(&entries, dback->disk_bytenr,
5466                                    dback->bytes);
5467                 if (!entry) {
5468                         entry = malloc(sizeof(struct extent_entry));
5469                         if (!entry) {
5470                                 ret = -ENOMEM;
5471                                 goto out;
5472                         }
5473                         memset(entry, 0, sizeof(*entry));
5474                         entry->bytenr = dback->disk_bytenr;
5475                         entry->bytes = dback->bytes;
5476                         list_add_tail(&entry->list, &entries);
5477                         nr_entries++;
5478                 }
5479
5480                 /*
5481                  * If we only have on entry we may think the entries agree when
5482                  * in reality they don't so we have to do some extra checking.
5483                  */
5484                 if (dback->disk_bytenr != rec->start ||
5485                     dback->bytes != rec->nr || back->broken)
5486                         mismatch = 1;
5487
5488                 if (back->broken) {
5489                         entry->broken++;
5490                         broken_entries++;
5491                 }
5492
5493                 entry->count++;
5494         }
5495
5496         /* Yay all the backrefs agree, carry on good sir */
5497         if (nr_entries <= 1 && !mismatch)
5498                 goto out;
5499
5500         fprintf(stderr, "attempting to repair backref discrepency for bytenr "
5501                 "%Lu\n", rec->start);
5502
5503         /*
5504          * First we want to see if the backrefs can agree amongst themselves who
5505          * is right, so figure out which one of the entries has the highest
5506          * count.
5507          */
5508         best = find_most_right_entry(&entries);
5509
5510         /*
5511          * Ok so we may have an even split between what the backrefs think, so
5512          * this is where we use the extent ref to see what it thinks.
5513          */
5514         if (!best) {
5515                 entry = find_entry(&entries, rec->start, rec->nr);
5516                 if (!entry && (!broken_entries || !rec->found_rec)) {
5517                         fprintf(stderr, "Backrefs don't agree with each other "
5518                                 "and extent record doesn't agree with anybody,"
5519                                 " so we can't fix bytenr %Lu bytes %Lu\n",
5520                                 rec->start, rec->nr);
5521                         ret = -EINVAL;
5522                         goto out;
5523                 } else if (!entry) {
5524                         /*
5525                          * Ok our backrefs were broken, we'll assume this is the
5526                          * correct value and add an entry for this range.
5527                          */
5528                         entry = malloc(sizeof(struct extent_entry));
5529                         if (!entry) {
5530                                 ret = -ENOMEM;
5531                                 goto out;
5532                         }
5533                         memset(entry, 0, sizeof(*entry));
5534                         entry->bytenr = rec->start;
5535                         entry->bytes = rec->nr;
5536                         list_add_tail(&entry->list, &entries);
5537                         nr_entries++;
5538                 }
5539                 entry->count++;
5540                 best = find_most_right_entry(&entries);
5541                 if (!best) {
5542                         fprintf(stderr, "Backrefs and extent record evenly "
5543                                 "split on who is right, this is going to "
5544                                 "require user input to fix bytenr %Lu bytes "
5545                                 "%Lu\n", rec->start, rec->nr);
5546                         ret = -EINVAL;
5547                         goto out;
5548                 }
5549         }
5550
5551         /*
5552          * I don't think this can happen currently as we'll abort() if we catch
5553          * this case higher up, but in case somebody removes that we still can't
5554          * deal with it properly here yet, so just bail out of that's the case.
5555          */
5556         if (best->bytenr != rec->start) {
5557                 fprintf(stderr, "Extent start and backref starts don't match, "
5558                         "please use btrfs-image on this file system and send "
5559                         "it to a btrfs developer so they can make fsck fix "
5560                         "this particular case.  bytenr is %Lu, bytes is %Lu\n",
5561                         rec->start, rec->nr);
5562                 ret = -EINVAL;
5563                 goto out;
5564         }
5565
5566         /*
5567          * Ok great we all agreed on an extent record, let's go find the real
5568          * references and fix up the ones that don't match.
5569          */
5570         list_for_each_entry(back, &rec->backrefs, list) {
5571                 if (back->full_backref || !back->is_data)
5572                         continue;
5573
5574                 dback = (struct data_backref *)back;
5575
5576                 /*
5577                  * Still ignoring backrefs that don't have a real ref attached
5578                  * to them.
5579                  */
5580                 if (dback->found_ref == 0)
5581                         continue;
5582
5583                 if (dback->bytes == best->bytes &&
5584                     dback->disk_bytenr == best->bytenr)
5585                         continue;
5586
5587                 ret = repair_ref(trans, info, path, dback, best);
5588                 if (ret)
5589                         goto out;
5590         }
5591
5592         /*
5593          * Ok we messed with the actual refs, which means we need to drop our
5594          * entire cache and go back and rescan.  I know this is a huge pain and
5595          * adds a lot of extra work, but it's the only way to be safe.  Once all
5596          * the backrefs agree we may not need to do anything to the extent
5597          * record itself.
5598          */
5599         ret = -EAGAIN;
5600 out:
5601         while (!list_empty(&entries)) {
5602                 entry = list_entry(entries.next, struct extent_entry, list);
5603                 list_del_init(&entry->list);
5604                 free(entry);
5605         }
5606         return ret;
5607 }
5608
5609 static int process_duplicates(struct btrfs_root *root,
5610                               struct cache_tree *extent_cache,
5611                               struct extent_record *rec)
5612 {
5613         struct extent_record *good, *tmp;
5614         struct cache_extent *cache;
5615         int ret;
5616
5617         /*
5618          * If we found a extent record for this extent then return, or if we
5619          * have more than one duplicate we are likely going to need to delete
5620          * something.
5621          */
5622         if (rec->found_rec || rec->num_duplicates > 1)
5623                 return 0;
5624
5625         /* Shouldn't happen but just in case */
5626         BUG_ON(!rec->num_duplicates);
5627
5628         /*
5629          * So this happens if we end up with a backref that doesn't match the
5630          * actual extent entry.  So either the backref is bad or the extent
5631          * entry is bad.  Either way we want to have the extent_record actually
5632          * reflect what we found in the extent_tree, so we need to take the
5633          * duplicate out and use that as the extent_record since the only way we
5634          * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
5635          */
5636         remove_cache_extent(extent_cache, &rec->cache);
5637
5638         good = list_entry(rec->dups.next, struct extent_record, list);
5639         list_del_init(&good->list);
5640         INIT_LIST_HEAD(&good->backrefs);
5641         INIT_LIST_HEAD(&good->dups);
5642         good->cache.start = good->start;
5643         good->cache.size = good->nr;
5644         good->content_checked = 0;
5645         good->owner_ref_checked = 0;
5646         good->num_duplicates = 0;
5647         good->refs = rec->refs;
5648         list_splice_init(&rec->backrefs, &good->backrefs);
5649         while (1) {
5650                 cache = lookup_cache_extent(extent_cache, good->start,
5651                                             good->nr);
5652                 if (!cache)
5653                         break;
5654                 tmp = container_of(cache, struct extent_record, cache);
5655
5656                 /*
5657                  * If we find another overlapping extent and it's found_rec is
5658                  * set then it's a duplicate and we need to try and delete
5659                  * something.
5660                  */
5661                 if (tmp->found_rec || tmp->num_duplicates > 0) {
5662                         if (list_empty(&good->list))
5663                                 list_add_tail(&good->list,
5664                                               &duplicate_extents);
5665                         good->num_duplicates += tmp->num_duplicates + 1;
5666                         list_splice_init(&tmp->dups, &good->dups);
5667                         list_del_init(&tmp->list);
5668                         list_add_tail(&tmp->list, &good->dups);
5669                         remove_cache_extent(extent_cache, &tmp->cache);
5670                         continue;
5671                 }
5672
5673                 /*
5674                  * Ok we have another non extent item backed extent rec, so lets
5675                  * just add it to this extent and carry on like we did above.
5676                  */
5677                 good->refs += tmp->refs;
5678                 list_splice_init(&tmp->backrefs, &good->backrefs);
5679                 remove_cache_extent(extent_cache, &tmp->cache);
5680                 free(tmp);
5681         }
5682         ret = insert_cache_extent(extent_cache, &good->cache);
5683         BUG_ON(ret);
5684         free(rec);
5685         return good->num_duplicates ? 0 : 1;
5686 }
5687
5688 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
5689                                     struct btrfs_root *root,
5690                                     struct extent_record *rec)
5691 {
5692         LIST_HEAD(delete_list);
5693         struct btrfs_path *path;
5694         struct extent_record *tmp, *good, *n;
5695         int nr_del = 0;
5696         int ret = 0;
5697         struct btrfs_key key;
5698
5699         path = btrfs_alloc_path();
5700         if (!path) {
5701                 ret = -ENOMEM;
5702                 goto out;
5703         }
5704
5705         good = rec;
5706         /* Find the record that covers all of the duplicates. */
5707         list_for_each_entry(tmp, &rec->dups, list) {
5708                 if (good->start < tmp->start)
5709                         continue;
5710                 if (good->nr > tmp->nr)
5711                         continue;
5712
5713                 if (tmp->start + tmp->nr < good->start + good->nr) {
5714                         fprintf(stderr, "Ok we have overlapping extents that "
5715                                 "aren't completely covered by eachother, this "
5716                                 "is going to require more careful thought.  "
5717                                 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
5718                                 tmp->start, tmp->nr, good->start, good->nr);
5719                         abort();
5720                 }
5721                 good = tmp;
5722         }
5723
5724         if (good != rec)
5725                 list_add_tail(&rec->list, &delete_list);
5726
5727         list_for_each_entry_safe(tmp, n, &rec->dups, list) {
5728                 if (tmp == good)
5729                         continue;
5730                 list_move_tail(&tmp->list, &delete_list);
5731         }
5732
5733         root = root->fs_info->extent_root;
5734         list_for_each_entry(tmp, &delete_list, list) {
5735                 if (tmp->found_rec == 0)
5736                         continue;
5737                 key.objectid = tmp->start;
5738                 key.type = BTRFS_EXTENT_ITEM_KEY;
5739                 key.offset = tmp->nr;
5740
5741                 /* Shouldn't happen but just in case */
5742                 if (tmp->metadata) {
5743                         fprintf(stderr, "Well this shouldn't happen, extent "
5744                                 "record overlaps but is metadata? "
5745                                 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
5746                         abort();
5747                 }
5748
5749                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5750                 if (ret) {
5751                         if (ret > 0)
5752                                 ret = -EINVAL;
5753                         goto out;
5754                 }
5755                 ret = btrfs_del_item(trans, root, path);
5756                 if (ret)
5757                         goto out;
5758                 btrfs_release_path(path);
5759                 nr_del++;
5760         }
5761
5762 out:
5763         while (!list_empty(&delete_list)) {
5764                 tmp = list_entry(delete_list.next, struct extent_record, list);
5765                 list_del_init(&tmp->list);
5766                 if (tmp == rec)
5767                         continue;
5768                 free(tmp);
5769         }
5770
5771         while (!list_empty(&rec->dups)) {
5772                 tmp = list_entry(rec->dups.next, struct extent_record, list);
5773                 list_del_init(&tmp->list);
5774                 free(tmp);
5775         }
5776
5777         btrfs_free_path(path);
5778
5779         if (!ret && !nr_del)
5780                 rec->num_duplicates = 0;
5781
5782         return ret ? ret : nr_del;
5783 }
5784
5785 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5786                                   struct btrfs_fs_info *info,
5787                                   struct btrfs_path *path,
5788                                   struct cache_tree *extent_cache,
5789                                   struct extent_record *rec)
5790 {
5791         struct btrfs_root *root;
5792         struct extent_backref *back;
5793         struct data_backref *dback;
5794         struct cache_extent *cache;
5795         struct btrfs_file_extent_item *fi;
5796         struct btrfs_key key;
5797         u64 bytenr, bytes;
5798         int ret;
5799
5800         list_for_each_entry(back, &rec->backrefs, list) {
5801                 /* Don't care about full backrefs (poor unloved backrefs) */
5802                 if (back->full_backref || !back->is_data)
5803                         continue;
5804
5805                 dback = (struct data_backref *)back;
5806
5807                 /* We found this one, we don't need to do a lookup */
5808                 if (dback->found_ref)
5809                         continue;
5810
5811                 key.objectid = dback->root;
5812                 key.type = BTRFS_ROOT_ITEM_KEY;
5813                 key.offset = (u64)-1;
5814
5815                 root = btrfs_read_fs_root(info, &key);
5816
5817                 /* No root, definitely a bad ref, skip */
5818                 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5819                         continue;
5820                 /* Other err, exit */
5821                 if (IS_ERR(root))
5822                         return PTR_ERR(root);
5823
5824                 key.objectid = dback->owner;
5825                 key.type = BTRFS_EXTENT_DATA_KEY;
5826                 key.offset = dback->offset;
5827                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5828                 if (ret) {
5829                         btrfs_release_path(path);
5830                         if (ret < 0)
5831                                 return ret;
5832                         /* Didn't find it, we can carry on */
5833                         ret = 0;
5834                         continue;
5835                 }
5836
5837                 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5838                                     struct btrfs_file_extent_item);
5839                 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5840                 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5841                 btrfs_release_path(path);
5842                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5843                 if (cache) {
5844                         struct extent_record *tmp;
5845                         tmp = container_of(cache, struct extent_record, cache);
5846
5847                         /*
5848                          * If we found an extent record for the bytenr for this
5849                          * particular backref then we can't add it to our
5850                          * current extent record.  We only want to add backrefs
5851                          * that don't have a corresponding extent item in the
5852                          * extent tree since they likely belong to this record
5853                          * and we need to fix it if it doesn't match bytenrs.
5854                          */
5855                         if  (tmp->found_rec)
5856                                 continue;
5857                 }
5858
5859                 dback->found_ref += 1;
5860                 dback->disk_bytenr = bytenr;
5861                 dback->bytes = bytes;
5862
5863                 /*
5864                  * Set this so the verify backref code knows not to trust the
5865                  * values in this backref.
5866                  */
5867                 back->broken = 1;
5868         }
5869
5870         return 0;
5871 }
5872
5873 /*
5874  * when an incorrect extent item is found, this will delete
5875  * all of the existing entries for it and recreate them
5876  * based on what the tree scan found.
5877  */
5878 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5879                              struct btrfs_fs_info *info,
5880                              struct cache_tree *extent_cache,
5881                              struct extent_record *rec)
5882 {
5883         int ret;
5884         struct btrfs_path *path;
5885         struct list_head *cur = rec->backrefs.next;
5886         struct cache_extent *cache;
5887         struct extent_backref *back;
5888         int allocated = 0;
5889         u64 flags = 0;
5890
5891         /*
5892          * remember our flags for recreating the extent.
5893          * FIXME, if we have cleared extent tree, we can not
5894          * lookup extent info in extent tree.
5895          */
5896         if (!init_extent_tree) {
5897                 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5898                                         rec->start, rec->max_size,
5899                                         rec->metadata, NULL, &flags);
5900                 if (ret < 0)
5901                         flags = 0;
5902         } else {
5903                 flags = 0;
5904         }
5905
5906         path = btrfs_alloc_path();
5907         if (!path)
5908                 return -ENOMEM;
5909
5910         if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5911                 /*
5912                  * Sometimes the backrefs themselves are so broken they don't
5913                  * get attached to any meaningful rec, so first go back and
5914                  * check any of our backrefs that we couldn't find and throw
5915                  * them into the list if we find the backref so that
5916                  * verify_backrefs can figure out what to do.
5917                  */
5918                 ret = find_possible_backrefs(trans, info, path, extent_cache,
5919                                              rec);
5920                 if (ret < 0)
5921                         goto out;
5922         }
5923
5924         /* step one, make sure all of the backrefs agree */
5925         ret = verify_backrefs(trans, info, path, rec);
5926         if (ret < 0)
5927                 goto out;
5928
5929         /* step two, delete all the existing records */
5930         ret = delete_extent_records(trans, info->extent_root, path,
5931                                     rec->start, rec->max_size);
5932
5933         if (ret < 0)
5934                 goto out;
5935
5936         /* was this block corrupt?  If so, don't add references to it */
5937         cache = lookup_cache_extent(info->corrupt_blocks,
5938                                     rec->start, rec->max_size);
5939         if (cache) {
5940                 ret = 0;
5941                 goto out;
5942         }
5943
5944         /* step three, recreate all the refs we did find */
5945         while(cur != &rec->backrefs) {
5946                 back = list_entry(cur, struct extent_backref, list);
5947                 cur = cur->next;
5948
5949                 /*
5950                  * if we didn't find any references, don't create a
5951                  * new extent record
5952                  */
5953                 if (!back->found_ref)
5954                         continue;
5955
5956                 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5957                 allocated = 1;
5958
5959                 if (ret)
5960                         goto out;
5961         }
5962 out:
5963         btrfs_free_path(path);
5964         return ret;
5965 }
5966
5967 /* right now we only prune from the extent allocation tree */
5968 static int prune_one_block(struct btrfs_trans_handle *trans,
5969                            struct btrfs_fs_info *info,
5970                            struct btrfs_corrupt_block *corrupt)
5971 {
5972         int ret;
5973         struct btrfs_path path;
5974         struct extent_buffer *eb;
5975         u64 found;
5976         int slot;
5977         int nritems;
5978         int level = corrupt->level + 1;
5979
5980         btrfs_init_path(&path);
5981 again:
5982         /* we want to stop at the parent to our busted block */
5983         path.lowest_level = level;
5984
5985         ret = btrfs_search_slot(trans, info->extent_root,
5986                                 &corrupt->key, &path, -1, 1);
5987
5988         if (ret < 0)
5989                 goto out;
5990
5991         eb = path.nodes[level];
5992         if (!eb) {
5993                 ret = -ENOENT;
5994                 goto out;
5995         }
5996
5997         /*
5998          * hopefully the search gave us the block we want to prune,
5999          * lets try that first
6000          */
6001         slot = path.slots[level];
6002         found =  btrfs_node_blockptr(eb, slot);
6003         if (found == corrupt->cache.start)
6004                 goto del_ptr;
6005
6006         nritems = btrfs_header_nritems(eb);
6007
6008         /* the search failed, lets scan this node and hope we find it */
6009         for (slot = 0; slot < nritems; slot++) {
6010                 found =  btrfs_node_blockptr(eb, slot);
6011                 if (found == corrupt->cache.start)
6012                         goto del_ptr;
6013         }
6014         /*
6015          * we couldn't find the bad block.  TODO, search all the nodes for pointers
6016          * to this block
6017          */
6018         if (eb == info->extent_root->node) {
6019                 ret = -ENOENT;
6020                 goto out;
6021         } else {
6022                 level++;
6023                 btrfs_release_path(&path);
6024                 goto again;
6025         }
6026
6027 del_ptr:
6028         printk("deleting pointer to block %Lu\n", corrupt->cache.start);
6029         ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
6030
6031 out:
6032         btrfs_release_path(&path);
6033         return ret;
6034 }
6035
6036 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
6037                                 struct btrfs_fs_info *info)
6038 {
6039         struct cache_extent *cache;
6040         struct btrfs_corrupt_block *corrupt;
6041
6042         cache = search_cache_extent(info->corrupt_blocks, 0);
6043         while (1) {
6044                 if (!cache)
6045                         break;
6046                 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
6047                 prune_one_block(trans, info, corrupt);
6048                 cache = next_cache_extent(cache);
6049         }
6050         return 0;
6051 }
6052
6053 static void free_corrupt_block(struct cache_extent *cache)
6054 {
6055         struct btrfs_corrupt_block *corrupt;
6056
6057         corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
6058         free(corrupt);
6059 }
6060
6061 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
6062
6063 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
6064 {
6065         struct btrfs_block_group_cache *cache;
6066         u64 start, end;
6067         int ret;
6068
6069         while (1) {
6070                 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
6071                                             &start, &end, EXTENT_DIRTY);
6072                 if (ret)
6073                         break;
6074                 clear_extent_dirty(&fs_info->free_space_cache, start, end,
6075                                    GFP_NOFS);
6076         }
6077
6078         start = 0;
6079         while (1) {
6080                 cache = btrfs_lookup_first_block_group(fs_info, start);
6081                 if (!cache)
6082                         break;
6083                 if (cache->cached)
6084                         cache->cached = 0;
6085                 start = cache->key.objectid + cache->key.offset;
6086         }
6087 }
6088
6089 static int check_extent_refs(struct btrfs_trans_handle *trans,
6090                              struct btrfs_root *root,
6091                              struct cache_tree *extent_cache)
6092 {
6093         struct extent_record *rec;
6094         struct cache_extent *cache;
6095         int err = 0;
6096         int ret = 0;
6097         int fixed = 0;
6098         int had_dups = 0;
6099
6100         if (repair) {
6101                 /*
6102                  * if we're doing a repair, we have to make sure
6103                  * we don't allocate from the problem extents.
6104                  * In the worst case, this will be all the
6105                  * extents in the FS
6106                  */
6107                 cache = search_cache_extent(extent_cache, 0);
6108                 while(cache) {
6109                         rec = container_of(cache, struct extent_record, cache);
6110                         btrfs_pin_extent(root->fs_info,
6111                                          rec->start, rec->max_size);
6112                         cache = next_cache_extent(cache);
6113                 }
6114
6115                 /* pin down all the corrupted blocks too */
6116                 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
6117                 while(cache) {
6118                         btrfs_pin_extent(root->fs_info,
6119                                          cache->start, cache->size);
6120                         cache = next_cache_extent(cache);
6121                 }
6122                 prune_corrupt_blocks(trans, root->fs_info);
6123                 reset_cached_block_groups(root->fs_info);
6124         }
6125
6126         /*
6127          * We need to delete any duplicate entries we find first otherwise we
6128          * could mess up the extent tree when we have backrefs that actually
6129          * belong to a different extent item and not the weird duplicate one.
6130          */
6131         while (repair && !list_empty(&duplicate_extents)) {
6132                 rec = list_entry(duplicate_extents.next, struct extent_record,
6133                                  list);
6134                 list_del_init(&rec->list);
6135
6136                 /* Sometimes we can find a backref before we find an actual
6137                  * extent, so we need to process it a little bit to see if there
6138                  * truly are multiple EXTENT_ITEM_KEY's for the same range, or
6139                  * if this is a backref screwup.  If we need to delete stuff
6140                  * process_duplicates() will return 0, otherwise it will return
6141                  * 1 and we
6142                  */
6143                 if (process_duplicates(root, extent_cache, rec))
6144                         continue;
6145                 ret = delete_duplicate_records(trans, root, rec);
6146                 if (ret < 0)
6147                         return ret;
6148                 /*
6149                  * delete_duplicate_records will return the number of entries
6150                  * deleted, so if it's greater than 0 then we know we actually
6151                  * did something and we need to remove.
6152                  */
6153                 if (ret)
6154                         had_dups = 1;
6155         }
6156
6157         if (had_dups)
6158                 return -EAGAIN;
6159
6160         while(1) {
6161                 fixed = 0;
6162                 cache = search_cache_extent(extent_cache, 0);
6163                 if (!cache)
6164                         break;
6165                 rec = container_of(cache, struct extent_record, cache);
6166                 if (rec->num_duplicates) {
6167                         fprintf(stderr, "extent item %llu has multiple extent "
6168                                 "items\n", (unsigned long long)rec->start);
6169                         err = 1;
6170                 }
6171
6172                 if (rec->refs != rec->extent_item_refs) {
6173                         fprintf(stderr, "ref mismatch on [%llu %llu] ",
6174                                 (unsigned long long)rec->start,
6175                                 (unsigned long long)rec->nr);
6176                         fprintf(stderr, "extent item %llu, found %llu\n",
6177                                 (unsigned long long)rec->extent_item_refs,
6178                                 (unsigned long long)rec->refs);
6179                         if (!fixed && repair) {
6180                                 ret = fixup_extent_refs(trans, root->fs_info,
6181                                                         extent_cache, rec);
6182                                 if (ret)
6183                                         goto repair_abort;
6184                                 fixed = 1;
6185                         }
6186                         err = 1;
6187
6188                 }
6189                 if (all_backpointers_checked(rec, 1)) {
6190                         fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
6191                                 (unsigned long long)rec->start,
6192                                 (unsigned long long)rec->nr);
6193
6194                         if (!fixed && repair) {
6195                                 ret = fixup_extent_refs(trans, root->fs_info,
6196                                                         extent_cache, rec);
6197                                 if (ret)
6198                                         goto repair_abort;
6199                                 fixed = 1;
6200                         }
6201
6202                         err = 1;
6203                 }
6204                 if (!rec->owner_ref_checked) {
6205                         fprintf(stderr, "owner ref check failed [%llu %llu]\n",
6206                                 (unsigned long long)rec->start,
6207                                 (unsigned long long)rec->nr);
6208                         if (!fixed && repair) {
6209                                 ret = fixup_extent_refs(trans, root->fs_info,
6210                                                         extent_cache, rec);
6211                                 if (ret)
6212                                         goto repair_abort;
6213                                 fixed = 1;
6214                         }
6215                         err = 1;
6216                 }
6217
6218                 remove_cache_extent(extent_cache, cache);
6219                 free_all_extent_backrefs(rec);
6220                 free(rec);
6221         }
6222 repair_abort:
6223         if (repair) {
6224                 if (ret && ret != -EAGAIN) {
6225                         fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
6226                         exit(1);
6227                 } else if (!ret) {
6228                         btrfs_fix_block_accounting(trans, root);
6229                 }
6230                 if (err)
6231                         fprintf(stderr, "repaired damaged extent references\n");
6232                 return ret;
6233         }
6234         return err;
6235 }
6236
6237 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
6238 {
6239         u64 stripe_size;
6240
6241         if (type & BTRFS_BLOCK_GROUP_RAID0) {
6242                 stripe_size = length;
6243                 stripe_size /= num_stripes;
6244         } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6245                 stripe_size = length * 2;
6246                 stripe_size /= num_stripes;
6247         } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
6248                 stripe_size = length;
6249                 stripe_size /= (num_stripes - 1);
6250         } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
6251                 stripe_size = length;
6252                 stripe_size /= (num_stripes - 2);
6253         } else {
6254                 stripe_size = length;
6255         }
6256         return stripe_size;
6257 }
6258
6259 static int check_chunk_refs(struct chunk_record *chunk_rec,
6260                             struct block_group_tree *block_group_cache,
6261                             struct device_extent_tree *dev_extent_cache,
6262                             int silent)
6263 {
6264         struct cache_extent *block_group_item;
6265         struct block_group_record *block_group_rec;
6266         struct cache_extent *dev_extent_item;
6267         struct device_extent_record *dev_extent_rec;
6268         u64 devid;
6269         u64 offset;
6270         u64 length;
6271         int i;
6272         int ret = 0;
6273
6274         block_group_item = lookup_cache_extent(&block_group_cache->tree,
6275                                                chunk_rec->offset,
6276                                                chunk_rec->length);
6277         if (block_group_item) {
6278                 block_group_rec = container_of(block_group_item,
6279                                                struct block_group_record,
6280                                                cache);
6281                 if (chunk_rec->length != block_group_rec->offset ||
6282                     chunk_rec->offset != block_group_rec->objectid ||
6283                     chunk_rec->type_flags != block_group_rec->flags) {
6284                         if (!silent)
6285                                 fprintf(stderr,
6286                                         "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
6287                                         chunk_rec->objectid,
6288                                         chunk_rec->type,
6289                                         chunk_rec->offset,
6290                                         chunk_rec->length,
6291                                         chunk_rec->offset,
6292                                         chunk_rec->type_flags,
6293                                         block_group_rec->objectid,
6294                                         block_group_rec->type,
6295                                         block_group_rec->offset,
6296                                         block_group_rec->offset,
6297                                         block_group_rec->objectid,
6298                                         block_group_rec->flags);
6299                         ret = -1;
6300                 } else {
6301                         list_del_init(&block_group_rec->list);
6302                         chunk_rec->bg_rec = block_group_rec;
6303                 }
6304         } else {
6305                 if (!silent)
6306                         fprintf(stderr,
6307                                 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
6308                                 chunk_rec->objectid,
6309                                 chunk_rec->type,
6310                                 chunk_rec->offset,
6311                                 chunk_rec->length,
6312                                 chunk_rec->offset,
6313                                 chunk_rec->type_flags);
6314                 ret = -1;
6315         }
6316
6317         length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
6318                                     chunk_rec->num_stripes);
6319         for (i = 0; i < chunk_rec->num_stripes; ++i) {
6320                 devid = chunk_rec->stripes[i].devid;
6321                 offset = chunk_rec->stripes[i].offset;
6322                 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
6323                                                        devid, offset, length);
6324                 if (dev_extent_item) {
6325                         dev_extent_rec = container_of(dev_extent_item,
6326                                                 struct device_extent_record,
6327                                                 cache);
6328                         if (dev_extent_rec->objectid != devid ||
6329                             dev_extent_rec->offset != offset ||
6330                             dev_extent_rec->chunk_offset != chunk_rec->offset ||
6331                             dev_extent_rec->length != length) {
6332                                 if (!silent)
6333                                         fprintf(stderr,
6334                                                 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
6335                                                 chunk_rec->objectid,
6336                                                 chunk_rec->type,
6337                                                 chunk_rec->offset,
6338                                                 chunk_rec->stripes[i].devid,
6339                                                 chunk_rec->stripes[i].offset,
6340                                                 dev_extent_rec->objectid,
6341                                                 dev_extent_rec->offset,
6342                                                 dev_extent_rec->length);
6343                                 ret = -1;
6344                         } else {
6345                                 list_move(&dev_extent_rec->chunk_list,
6346                                           &chunk_rec->dextents);
6347                         }
6348                 } else {
6349                         if (!silent)
6350                                 fprintf(stderr,
6351                                         "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
6352                                         chunk_rec->objectid,
6353                                         chunk_rec->type,
6354                                         chunk_rec->offset,
6355                                         chunk_rec->stripes[i].devid,
6356                                         chunk_rec->stripes[i].offset);
6357                         ret = -1;
6358                 }
6359         }
6360         return ret;
6361 }
6362
6363 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
6364 int check_chunks(struct cache_tree *chunk_cache,
6365                  struct block_group_tree *block_group_cache,
6366                  struct device_extent_tree *dev_extent_cache,
6367                  struct list_head *good, struct list_head *bad, int silent)
6368 {
6369         struct cache_extent *chunk_item;
6370         struct chunk_record *chunk_rec;
6371         struct block_group_record *bg_rec;
6372         struct device_extent_record *dext_rec;
6373         int err;
6374         int ret = 0;
6375
6376         chunk_item = first_cache_extent(chunk_cache);
6377         while (chunk_item) {
6378                 chunk_rec = container_of(chunk_item, struct chunk_record,
6379                                          cache);
6380                 err = check_chunk_refs(chunk_rec, block_group_cache,
6381                                        dev_extent_cache, silent);
6382                 if (err) {
6383                         ret = err;
6384                         if (bad)
6385                                 list_add_tail(&chunk_rec->list, bad);
6386                 } else {
6387                         if (good)
6388                                 list_add_tail(&chunk_rec->list, good);
6389                 }
6390
6391                 chunk_item = next_cache_extent(chunk_item);
6392         }
6393
6394         list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
6395                 if (!silent)
6396                         fprintf(stderr,
6397                                 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
6398                                 bg_rec->objectid,
6399                                 bg_rec->offset,
6400                                 bg_rec->flags);
6401                 if (!ret)
6402                         ret = 1;
6403         }
6404
6405         list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
6406                             chunk_list) {
6407                 if (!silent)
6408                         fprintf(stderr,
6409                                 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
6410                                 dext_rec->objectid,
6411                                 dext_rec->offset,
6412                                 dext_rec->length);
6413                 if (!ret)
6414                         ret = 1;
6415         }
6416         return ret;
6417 }
6418
6419
6420 static int check_device_used(struct device_record *dev_rec,
6421                              struct device_extent_tree *dext_cache)
6422 {
6423         struct cache_extent *cache;
6424         struct device_extent_record *dev_extent_rec;
6425         u64 total_byte = 0;
6426
6427         cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
6428         while (cache) {
6429                 dev_extent_rec = container_of(cache,
6430                                               struct device_extent_record,
6431                                               cache);
6432                 if (dev_extent_rec->objectid != dev_rec->devid)
6433                         break;
6434
6435                 list_del_init(&dev_extent_rec->device_list);
6436                 total_byte += dev_extent_rec->length;
6437                 cache = next_cache_extent(cache);
6438         }
6439
6440         if (total_byte != dev_rec->byte_used) {
6441                 fprintf(stderr,
6442                         "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
6443                         total_byte, dev_rec->byte_used, dev_rec->objectid,
6444                         dev_rec->type, dev_rec->offset);
6445                 return -1;
6446         } else {
6447                 return 0;
6448         }
6449 }
6450
6451 /* check btrfs_dev_item -> btrfs_dev_extent */
6452 static int check_devices(struct rb_root *dev_cache,
6453                          struct device_extent_tree *dev_extent_cache)
6454 {
6455         struct rb_node *dev_node;
6456         struct device_record *dev_rec;
6457         struct device_extent_record *dext_rec;
6458         int err;
6459         int ret = 0;
6460
6461         dev_node = rb_first(dev_cache);
6462         while (dev_node) {
6463                 dev_rec = container_of(dev_node, struct device_record, node);
6464                 err = check_device_used(dev_rec, dev_extent_cache);
6465                 if (err)
6466                         ret = err;
6467
6468                 dev_node = rb_next(dev_node);
6469         }
6470         list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
6471                             device_list) {
6472                 fprintf(stderr,
6473                         "Device extent[%llu, %llu, %llu] didn't find its device.\n",
6474                         dext_rec->objectid, dext_rec->offset, dext_rec->length);
6475                 if (!ret)
6476                         ret = 1;
6477         }
6478         return ret;
6479 }
6480
6481 static int check_chunks_and_extents(struct btrfs_root *root)
6482 {
6483         struct rb_root dev_cache;
6484         struct cache_tree chunk_cache;
6485         struct block_group_tree block_group_cache;
6486         struct device_extent_tree dev_extent_cache;
6487         struct cache_tree extent_cache;
6488         struct cache_tree seen;
6489         struct cache_tree pending;
6490         struct cache_tree reada;
6491         struct cache_tree nodes;
6492         struct cache_tree corrupt_blocks;
6493         struct btrfs_path path;
6494         struct btrfs_key key;
6495         struct btrfs_key found_key;
6496         int ret, err = 0;
6497         u64 last = 0;
6498         struct block_info *bits;
6499         int bits_nr;
6500         struct extent_buffer *leaf;
6501         struct btrfs_trans_handle *trans = NULL;
6502         int slot;
6503         struct btrfs_root_item ri;
6504         struct list_head dropping_trees;
6505
6506         dev_cache = RB_ROOT;
6507         cache_tree_init(&chunk_cache);
6508         block_group_tree_init(&block_group_cache);
6509         device_extent_tree_init(&dev_extent_cache);
6510
6511         cache_tree_init(&extent_cache);
6512         cache_tree_init(&seen);
6513         cache_tree_init(&pending);
6514         cache_tree_init(&nodes);
6515         cache_tree_init(&reada);
6516         cache_tree_init(&corrupt_blocks);
6517         INIT_LIST_HEAD(&dropping_trees);
6518
6519         if (repair) {
6520                 trans = btrfs_start_transaction(root, 1);
6521                 if (IS_ERR(trans)) {
6522                         fprintf(stderr, "Error starting transaction\n");
6523                         return PTR_ERR(trans);
6524                 }
6525                 root->fs_info->fsck_extent_cache = &extent_cache;
6526                 root->fs_info->free_extent_hook = free_extent_hook;
6527                 root->fs_info->corrupt_blocks = &corrupt_blocks;
6528         }
6529
6530         bits_nr = 1024;
6531         bits = malloc(bits_nr * sizeof(struct block_info));
6532         if (!bits) {
6533                 perror("malloc");
6534                 exit(1);
6535         }
6536
6537 again:
6538         add_root_to_pending(root->fs_info->tree_root->node,
6539                             &extent_cache, &pending, &seen, &nodes,
6540                             &root->fs_info->tree_root->root_key);
6541
6542         add_root_to_pending(root->fs_info->chunk_root->node,
6543                             &extent_cache, &pending, &seen, &nodes,
6544                             &root->fs_info->chunk_root->root_key);
6545
6546         btrfs_init_path(&path);
6547         key.offset = 0;
6548         key.objectid = 0;
6549         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
6550         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
6551                                         &key, &path, 0, 0);
6552         if (ret < 0)
6553                 goto out;
6554         while(1) {
6555                 leaf = path.nodes[0];
6556                 slot = path.slots[0];
6557                 if (slot >= btrfs_header_nritems(path.nodes[0])) {
6558                         ret = btrfs_next_leaf(root, &path);
6559                         if (ret != 0)
6560                                 break;
6561                         leaf = path.nodes[0];
6562                         slot = path.slots[0];
6563                 }
6564                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
6565                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
6566                         unsigned long offset;
6567                         struct extent_buffer *buf;
6568
6569                         offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
6570                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
6571                         if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
6572                                 buf = read_tree_block(root->fs_info->tree_root,
6573                                                       btrfs_root_bytenr(&ri),
6574                                                       btrfs_level_size(root,
6575                                                       btrfs_root_level(&ri)),
6576                                                       0);
6577                                 if (!buf) {
6578                                         ret = -EIO;
6579                                         goto out;
6580                                 }
6581                                 add_root_to_pending(buf, &extent_cache,
6582                                                     &pending, &seen, &nodes,
6583                                                     &found_key);
6584                                 free_extent_buffer(buf);
6585                         } else {
6586                                 struct dropping_root_item_record *dri_rec;
6587                                 dri_rec = malloc(sizeof(*dri_rec));
6588                                 if (!dri_rec) {
6589                                         perror("malloc");
6590                                         exit(1);
6591                                 }
6592                                 memcpy(&dri_rec->ri, &ri, sizeof(ri));
6593                                 memcpy(&dri_rec->found_key, &found_key,
6594                                        sizeof(found_key));
6595                                 list_add_tail(&dri_rec->list, &dropping_trees);
6596                         }
6597                 }
6598                 path.slots[0]++;
6599         }
6600         btrfs_release_path(&path);
6601         while (1) {
6602                 ret = run_next_block(trans, root, bits, bits_nr, &last,
6603                                      &pending, &seen, &reada, &nodes,
6604                                      &extent_cache, &chunk_cache, &dev_cache,
6605                                      &block_group_cache, &dev_extent_cache,
6606                                      NULL);
6607                 if (ret != 0)
6608                         break;
6609         }
6610
6611         while (!list_empty(&dropping_trees)) {
6612                 struct dropping_root_item_record *rec;
6613                 struct extent_buffer *buf;
6614                 rec = list_entry(dropping_trees.next,
6615                                  struct dropping_root_item_record, list);
6616                 last = 0;
6617                 if (!bits) {
6618                         perror("realloc");
6619                         exit(1);
6620                 }
6621                 buf = read_tree_block(root->fs_info->tree_root,
6622                                       btrfs_root_bytenr(&rec->ri),
6623                                       btrfs_level_size(root,
6624                                       btrfs_root_level(&rec->ri)), 0);
6625                 if (!buf) {
6626                         ret = -EIO;
6627                         goto out;
6628                 }
6629                 add_root_to_pending(buf, &extent_cache, &pending,
6630                                     &seen, &nodes, &rec->found_key);
6631                 while (1) {
6632                         ret = run_next_block(trans, root, bits, bits_nr, &last,
6633                                              &pending, &seen, &reada,
6634                                              &nodes, &extent_cache,
6635                                              &chunk_cache, &dev_cache,
6636                                              &block_group_cache,
6637                                              &dev_extent_cache,
6638                                              &rec->ri);
6639                         if (ret != 0)
6640                                 break;
6641                 }
6642                 free_extent_buffer(buf);
6643                 list_del(&rec->list);
6644                 free(rec);
6645         }
6646
6647         if (ret >= 0)
6648                 ret = check_extent_refs(trans, root, &extent_cache);
6649         if (ret == -EAGAIN) {
6650                 ret = btrfs_commit_transaction(trans, root);
6651                 if (ret)
6652                         goto out;
6653
6654                 trans = btrfs_start_transaction(root, 1);
6655                 if (IS_ERR(trans)) {
6656                         ret = PTR_ERR(trans);
6657                         goto out;
6658                 }
6659
6660                 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6661                 free_extent_cache_tree(&seen);
6662                 free_extent_cache_tree(&pending);
6663                 free_extent_cache_tree(&reada);
6664                 free_extent_cache_tree(&nodes);
6665                 free_chunk_cache_tree(&chunk_cache);
6666                 free_block_group_tree(&block_group_cache);
6667                 free_device_cache_tree(&dev_cache);
6668                 free_device_extent_tree(&dev_extent_cache);
6669                 free_extent_record_cache(root->fs_info, &extent_cache);
6670                 goto again;
6671         }
6672
6673         err = check_chunks(&chunk_cache, &block_group_cache,
6674                            &dev_extent_cache, NULL, NULL, 0);
6675         if (err && !ret)
6676                 ret = err;
6677
6678         err = check_devices(&dev_cache, &dev_extent_cache);
6679         if (err && !ret)
6680                 ret = err;
6681
6682 out:
6683         if (trans) {
6684                 err = btrfs_commit_transaction(trans, root);
6685                 if (!ret)
6686                         ret = err;
6687         }
6688         if (repair) {
6689                 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6690                 root->fs_info->fsck_extent_cache = NULL;
6691                 root->fs_info->free_extent_hook = NULL;
6692                 root->fs_info->corrupt_blocks = NULL;
6693         }
6694         free(bits);
6695         free_chunk_cache_tree(&chunk_cache);
6696         free_device_cache_tree(&dev_cache);
6697         free_block_group_tree(&block_group_cache);
6698         free_device_extent_tree(&dev_extent_cache);
6699         free_extent_cache_tree(&seen);
6700         free_extent_cache_tree(&pending);
6701         free_extent_cache_tree(&reada);
6702         free_extent_cache_tree(&nodes);
6703         return ret;
6704 }
6705
6706 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
6707                            struct btrfs_root *root, int overwrite)
6708 {
6709         struct extent_buffer *c;
6710         struct extent_buffer *old = root->node;
6711         int level;
6712         int ret;
6713         struct btrfs_disk_key disk_key = {0,0,0};
6714
6715         level = 0;
6716
6717         if (overwrite) {
6718                 c = old;
6719                 extent_buffer_get(c);
6720                 goto init;
6721         }
6722         c = btrfs_alloc_free_block(trans, root,
6723                                    btrfs_level_size(root, 0),
6724                                    root->root_key.objectid,
6725                                    &disk_key, level, 0, 0);
6726         if (IS_ERR(c)) {
6727                 c = old;
6728                 extent_buffer_get(c);
6729                 overwrite = 1;
6730         }
6731 init:
6732         memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
6733         btrfs_set_header_level(c, level);
6734         btrfs_set_header_bytenr(c, c->start);
6735         btrfs_set_header_generation(c, trans->transid);
6736         btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
6737         btrfs_set_header_owner(c, root->root_key.objectid);
6738
6739         write_extent_buffer(c, root->fs_info->fsid,
6740                             btrfs_header_fsid(), BTRFS_FSID_SIZE);
6741
6742         write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
6743                             btrfs_header_chunk_tree_uuid(c),
6744                             BTRFS_UUID_SIZE);
6745
6746         btrfs_mark_buffer_dirty(c);
6747         /*
6748          * this case can happen in the following case:
6749          *
6750          * 1.overwrite previous root.
6751          *
6752          * 2.reinit reloc data root, this is because we skip pin
6753          * down reloc data tree before which means we can allocate
6754          * same block bytenr here.
6755          */
6756         if (old->start == c->start) {
6757                 btrfs_set_root_generation(&root->root_item,
6758                                           trans->transid);
6759                 root->root_item.level = btrfs_header_level(root->node);
6760                 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6761                                         &root->root_key, &root->root_item);
6762                 if (ret) {
6763                         free_extent_buffer(c);
6764                         return ret;
6765                 }
6766         }
6767         free_extent_buffer(old);
6768         root->node = c;
6769         add_root_to_dirty_list(root);
6770         return 0;
6771 }
6772
6773 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
6774                                 struct extent_buffer *eb, int tree_root)
6775 {
6776         struct extent_buffer *tmp;
6777         struct btrfs_root_item *ri;
6778         struct btrfs_key key;
6779         u64 bytenr;
6780         u32 leafsize;
6781         int level = btrfs_header_level(eb);
6782         int nritems;
6783         int ret;
6784         int i;
6785
6786         /*
6787          * If we have pinned this block before, don't pin it again.
6788          * This can not only avoid forever loop with broken filesystem
6789          * but also give us some speedups.
6790          */
6791         if (test_range_bit(&fs_info->pinned_extents, eb->start,
6792                            eb->start + eb->len - 1, EXTENT_DIRTY, 0))
6793                 return 0;
6794
6795         btrfs_pin_extent(fs_info, eb->start, eb->len);
6796
6797         leafsize = btrfs_super_leafsize(fs_info->super_copy);
6798         nritems = btrfs_header_nritems(eb);
6799         for (i = 0; i < nritems; i++) {
6800                 if (level == 0) {
6801                         btrfs_item_key_to_cpu(eb, &key, i);
6802                         if (key.type != BTRFS_ROOT_ITEM_KEY)
6803                                 continue;
6804                         /* Skip the extent root and reloc roots */
6805                         if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6806                             key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6807                             key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6808                                 continue;
6809                         ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6810                         bytenr = btrfs_disk_root_bytenr(eb, ri);
6811
6812                         /*
6813                          * If at any point we start needing the real root we
6814                          * will have to build a stump root for the root we are
6815                          * in, but for now this doesn't actually use the root so
6816                          * just pass in extent_root.
6817                          */
6818                         tmp = read_tree_block(fs_info->extent_root, bytenr,
6819                                               leafsize, 0);
6820                         if (!tmp) {
6821                                 fprintf(stderr, "Error reading root block\n");
6822                                 return -EIO;
6823                         }
6824                         ret = pin_down_tree_blocks(fs_info, tmp, 0);
6825                         free_extent_buffer(tmp);
6826                         if (ret)
6827                                 return ret;
6828                 } else {
6829                         bytenr = btrfs_node_blockptr(eb, i);
6830
6831                         /* If we aren't the tree root don't read the block */
6832                         if (level == 1 && !tree_root) {
6833                                 btrfs_pin_extent(fs_info, bytenr, leafsize);
6834                                 continue;
6835                         }
6836
6837                         tmp = read_tree_block(fs_info->extent_root, bytenr,
6838                                               leafsize, 0);
6839                         if (!tmp) {
6840                                 fprintf(stderr, "Error reading tree block\n");
6841                                 return -EIO;
6842                         }
6843                         ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6844                         free_extent_buffer(tmp);
6845                         if (ret)
6846                                 return ret;
6847                 }
6848         }
6849
6850         return 0;
6851 }
6852
6853 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6854 {
6855         int ret;
6856
6857         ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6858         if (ret)
6859                 return ret;
6860
6861         return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6862 }
6863
6864 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6865 {
6866         struct btrfs_block_group_cache *cache;
6867         struct btrfs_path *path;
6868         struct extent_buffer *leaf;
6869         struct btrfs_chunk *chunk;
6870         struct btrfs_key key;
6871         int ret;
6872         u64 start;
6873
6874         path = btrfs_alloc_path();
6875         if (!path)
6876                 return -ENOMEM;
6877
6878         key.objectid = 0;
6879         key.type = BTRFS_CHUNK_ITEM_KEY;
6880         key.offset = 0;
6881
6882         ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6883         if (ret < 0) {
6884                 btrfs_free_path(path);
6885                 return ret;
6886         }
6887
6888         /*
6889          * We do this in case the block groups were screwed up and had alloc
6890          * bits that aren't actually set on the chunks.  This happens with
6891          * restored images every time and could happen in real life I guess.
6892          */
6893         fs_info->avail_data_alloc_bits = 0;
6894         fs_info->avail_metadata_alloc_bits = 0;
6895         fs_info->avail_system_alloc_bits = 0;
6896
6897         /* First we need to create the in-memory block groups */
6898         while (1) {
6899                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6900                         ret = btrfs_next_leaf(fs_info->chunk_root, path);
6901                         if (ret < 0) {
6902                                 btrfs_free_path(path);
6903                                 return ret;
6904                         }
6905                         if (ret) {
6906                                 ret = 0;
6907                                 break;
6908                         }
6909                 }
6910                 leaf = path->nodes[0];
6911                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6912                 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6913                         path->slots[0]++;
6914                         continue;
6915                 }
6916
6917                 chunk = btrfs_item_ptr(leaf, path->slots[0],
6918                                        struct btrfs_chunk);
6919                 btrfs_add_block_group(fs_info, 0,
6920                                       btrfs_chunk_type(leaf, chunk),
6921                                       key.objectid, key.offset,
6922                                       btrfs_chunk_length(leaf, chunk));
6923                 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6924                                  key.offset + btrfs_chunk_length(leaf, chunk),
6925                                  GFP_NOFS);
6926                 path->slots[0]++;
6927         }
6928         start = 0;
6929         while (1) {
6930                 cache = btrfs_lookup_first_block_group(fs_info, start);
6931                 if (!cache)
6932                         break;
6933                 cache->cached = 1;
6934                 start = cache->key.objectid + cache->key.offset;
6935         }
6936
6937         btrfs_free_path(path);
6938         return 0;
6939 }
6940
6941 static int reset_balance(struct btrfs_trans_handle *trans,
6942                          struct btrfs_fs_info *fs_info)
6943 {
6944         struct btrfs_root *root = fs_info->tree_root;
6945         struct btrfs_path *path;
6946         struct extent_buffer *leaf;
6947         struct btrfs_key key;
6948         int del_slot, del_nr = 0;
6949         int ret;
6950         int found = 0;
6951
6952         path = btrfs_alloc_path();
6953         if (!path)
6954                 return -ENOMEM;
6955
6956         key.objectid = BTRFS_BALANCE_OBJECTID;
6957         key.type = BTRFS_BALANCE_ITEM_KEY;
6958         key.offset = 0;
6959
6960         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6961         if (ret) {
6962                 if (ret > 0)
6963                         ret = 0;
6964                 if (!ret)
6965                         goto reinit_data_reloc;
6966                 else
6967                         goto out;
6968         }
6969
6970         ret = btrfs_del_item(trans, root, path);
6971         if (ret)
6972                 goto out;
6973         btrfs_release_path(path);
6974
6975         key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6976         key.type = BTRFS_ROOT_ITEM_KEY;
6977         key.offset = 0;
6978
6979         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6980         if (ret < 0)
6981                 goto out;
6982         while (1) {
6983                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6984                         if (!found)
6985                                 break;
6986
6987                         if (del_nr) {
6988                                 ret = btrfs_del_items(trans, root, path,
6989                                                       del_slot, del_nr);
6990                                 del_nr = 0;
6991                                 if (ret)
6992                                         goto out;
6993                         }
6994                         key.offset++;
6995                         btrfs_release_path(path);
6996
6997                         found = 0;
6998                         ret = btrfs_search_slot(trans, root, &key, path,
6999                                                 -1, 1);
7000                         if (ret < 0)
7001                                 goto out;
7002                         continue;
7003                 }
7004                 found = 1;
7005                 leaf = path->nodes[0];
7006                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7007                 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
7008                         break;
7009                 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7010                         path->slots[0]++;
7011                         continue;
7012                 }
7013                 if (!del_nr) {
7014                         del_slot = path->slots[0];
7015                         del_nr = 1;
7016                 } else {
7017                         del_nr++;
7018                 }
7019                 path->slots[0]++;
7020         }
7021
7022         if (del_nr) {
7023                 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
7024                 if (ret)
7025                         goto out;
7026         }
7027         btrfs_release_path(path);
7028
7029 reinit_data_reloc:
7030         key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7031         key.type = BTRFS_ROOT_ITEM_KEY;
7032         key.offset = (u64)-1;
7033         root = btrfs_read_fs_root(fs_info, &key);
7034         if (IS_ERR(root)) {
7035                 fprintf(stderr, "Error reading data reloc tree\n");
7036                 return PTR_ERR(root);
7037         }
7038         record_root_in_trans(trans, root);
7039         ret = btrfs_fsck_reinit_root(trans, root, 0);
7040         if (ret)
7041                 goto out;
7042         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
7043 out:
7044         btrfs_free_path(path);
7045         return ret;
7046 }
7047
7048 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
7049                               struct btrfs_fs_info *fs_info)
7050 {
7051         u64 start = 0;
7052         int ret;
7053
7054         /*
7055          * The only reason we don't do this is because right now we're just
7056          * walking the trees we find and pinning down their bytes, we don't look
7057          * at any of the leaves.  In order to do mixed groups we'd have to check
7058          * the leaves of any fs roots and pin down the bytes for any file
7059          * extents we find.  Not hard but why do it if we don't have to?
7060          */
7061         if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
7062                 fprintf(stderr, "We don't support re-initing the extent tree "
7063                         "for mixed block groups yet, please notify a btrfs "
7064                         "developer you want to do this so they can add this "
7065                         "functionality.\n");
7066                 return -EINVAL;
7067         }
7068
7069         /*
7070          * first we need to walk all of the trees except the extent tree and pin
7071          * down the bytes that are in use so we don't overwrite any existing
7072          * metadata.
7073          */
7074         ret = pin_metadata_blocks(fs_info);
7075         if (ret) {
7076                 fprintf(stderr, "error pinning down used bytes\n");
7077                 return ret;
7078         }
7079
7080         /*
7081          * Need to drop all the block groups since we're going to recreate all
7082          * of them again.
7083          */
7084         btrfs_free_block_groups(fs_info);
7085         ret = reset_block_groups(fs_info);
7086         if (ret) {
7087                 fprintf(stderr, "error resetting the block groups\n");
7088                 return ret;
7089         }
7090
7091         /* Ok we can allocate now, reinit the extent root */
7092         ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
7093         if (ret) {
7094                 fprintf(stderr, "extent root initialization failed\n");
7095                 /*
7096                  * When the transaction code is updated we should end the
7097                  * transaction, but for now progs only knows about commit so
7098                  * just return an error.
7099                  */
7100                 return ret;
7101         }
7102
7103         /*
7104          * Now we have all the in-memory block groups setup so we can make
7105          * allocations properly, and the metadata we care about is safe since we
7106          * pinned all of it above.
7107          */
7108         while (1) {
7109                 struct btrfs_block_group_cache *cache;
7110
7111                 cache = btrfs_lookup_first_block_group(fs_info, start);
7112                 if (!cache)
7113                         break;
7114                 start = cache->key.objectid + cache->key.offset;
7115                 ret = btrfs_insert_item(trans, fs_info->extent_root,
7116                                         &cache->key, &cache->item,
7117                                         sizeof(cache->item));
7118                 if (ret) {
7119                         fprintf(stderr, "Error adding block group\n");
7120                         return ret;
7121                 }
7122                 btrfs_extent_post_op(trans, fs_info->extent_root);
7123         }
7124
7125         ret = reset_balance(trans, fs_info);
7126         if (ret)
7127                 fprintf(stderr, "error reseting the pending balance\n");
7128
7129         return ret;
7130 }
7131
7132 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
7133 {
7134         struct btrfs_path *path;
7135         struct btrfs_trans_handle *trans;
7136         struct btrfs_key key;
7137         int ret;
7138
7139         printf("Recowing metadata block %llu\n", eb->start);
7140         key.objectid = btrfs_header_owner(eb);
7141         key.type = BTRFS_ROOT_ITEM_KEY;
7142         key.offset = (u64)-1;
7143
7144         root = btrfs_read_fs_root(root->fs_info, &key);
7145         if (IS_ERR(root)) {
7146                 fprintf(stderr, "Couldn't find owner root %llu\n",
7147                         key.objectid);
7148                 return PTR_ERR(root);
7149         }
7150
7151         path = btrfs_alloc_path();
7152         if (!path)
7153                 return -ENOMEM;
7154
7155         trans = btrfs_start_transaction(root, 1);
7156         if (IS_ERR(trans)) {
7157                 btrfs_free_path(path);
7158                 return PTR_ERR(trans);
7159         }
7160
7161         path->lowest_level = btrfs_header_level(eb);
7162         if (path->lowest_level)
7163                 btrfs_node_key_to_cpu(eb, &key, 0);
7164         else
7165                 btrfs_item_key_to_cpu(eb, &key, 0);
7166
7167         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7168         btrfs_commit_transaction(trans, root);
7169         btrfs_free_path(path);
7170         return ret;
7171 }
7172
7173 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
7174 {
7175         struct btrfs_path *path;
7176         struct btrfs_trans_handle *trans;
7177         struct btrfs_key key;
7178         int ret;
7179
7180         printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
7181                bad->key.type, bad->key.offset);
7182         key.objectid = bad->root_id;
7183         key.type = BTRFS_ROOT_ITEM_KEY;
7184         key.offset = (u64)-1;
7185
7186         root = btrfs_read_fs_root(root->fs_info, &key);
7187         if (IS_ERR(root)) {
7188                 fprintf(stderr, "Couldn't find owner root %llu\n",
7189                         key.objectid);
7190                 return PTR_ERR(root);
7191         }
7192
7193         path = btrfs_alloc_path();
7194         if (!path)
7195                 return -ENOMEM;
7196
7197         trans = btrfs_start_transaction(root, 1);
7198         if (IS_ERR(trans)) {
7199                 btrfs_free_path(path);
7200                 return PTR_ERR(trans);
7201         }
7202
7203         ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
7204         if (ret) {
7205                 if (ret > 0)
7206                         ret = 0;
7207                 goto out;
7208         }
7209         ret = btrfs_del_item(trans, root, path);
7210 out:
7211         btrfs_commit_transaction(trans, root);
7212         btrfs_free_path(path);
7213         return ret;
7214 }
7215
7216 static int zero_log_tree(struct btrfs_root *root)
7217 {
7218         struct btrfs_trans_handle *trans;
7219         int ret;
7220
7221         trans = btrfs_start_transaction(root, 1);
7222         if (IS_ERR(trans)) {
7223                 ret = PTR_ERR(trans);
7224                 return ret;
7225         }
7226         btrfs_set_super_log_root(root->fs_info->super_copy, 0);
7227         btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
7228         ret = btrfs_commit_transaction(trans, root);
7229         return ret;
7230 }
7231
7232 static int populate_csum(struct btrfs_trans_handle *trans,
7233                          struct btrfs_root *csum_root, char *buf, u64 start,
7234                          u64 len)
7235 {
7236         u64 offset = 0;
7237         u64 sectorsize;
7238         int ret = 0;
7239
7240         while (offset < len) {
7241                 sectorsize = csum_root->sectorsize;
7242                 ret = read_extent_data(csum_root, buf, start + offset,
7243                                        &sectorsize, 0);
7244                 if (ret)
7245                         break;
7246                 ret = btrfs_csum_file_block(trans, csum_root, start + len,
7247                                             start + offset, buf, sectorsize);
7248                 if (ret)
7249                         break;
7250                 offset += sectorsize;
7251         }
7252         return ret;
7253 }
7254
7255 static int fill_csum_tree(struct btrfs_trans_handle *trans,
7256                           struct btrfs_root *csum_root)
7257 {
7258         struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
7259         struct btrfs_path *path;
7260         struct btrfs_extent_item *ei;
7261         struct extent_buffer *leaf;
7262         char *buf;
7263         struct btrfs_key key;
7264         int ret;
7265
7266         path = btrfs_alloc_path();
7267         if (!path)
7268                 return -ENOMEM;
7269
7270         key.objectid = 0;
7271         key.type = BTRFS_EXTENT_ITEM_KEY;
7272         key.offset = 0;
7273
7274         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
7275         if (ret < 0) {
7276                 btrfs_free_path(path);
7277                 return ret;
7278         }
7279
7280         buf = malloc(csum_root->sectorsize);
7281         if (!buf) {
7282                 btrfs_free_path(path);
7283                 return -ENOMEM;
7284         }
7285
7286         while (1) {
7287                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7288                         ret = btrfs_next_leaf(extent_root, path);
7289                         if (ret < 0)
7290                                 break;
7291                         if (ret) {
7292                                 ret = 0;
7293                                 break;
7294                         }
7295                 }
7296                 leaf = path->nodes[0];
7297
7298                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7299                 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7300                         path->slots[0]++;
7301                         continue;
7302                 }
7303
7304                 ei = btrfs_item_ptr(leaf, path->slots[0],
7305                                     struct btrfs_extent_item);
7306                 if (!(btrfs_extent_flags(leaf, ei) &
7307                       BTRFS_EXTENT_FLAG_DATA)) {
7308                         path->slots[0]++;
7309                         continue;
7310                 }
7311
7312                 ret = populate_csum(trans, csum_root, buf, key.objectid,
7313                                     key.offset);
7314                 if (ret)
7315                         break;
7316                 path->slots[0]++;
7317         }
7318
7319         btrfs_free_path(path);
7320         free(buf);
7321         return ret;
7322 }
7323
7324 struct root_item_info {
7325         /* level of the root */
7326         u8 level;
7327         /* number of nodes at this level, must be 1 for a root */
7328         int node_count;
7329         u64 bytenr;
7330         u64 gen;
7331         struct cache_extent cache_extent;
7332 };
7333
7334 static struct cache_tree *roots_info_cache = NULL;
7335
7336 static void free_roots_info_cache(void)
7337 {
7338         if (!roots_info_cache)
7339                 return;
7340
7341         while (!cache_tree_empty(roots_info_cache)) {
7342                 struct cache_extent *entry;
7343                 struct root_item_info *rii;
7344
7345                 entry = first_cache_extent(roots_info_cache);
7346                 remove_cache_extent(roots_info_cache, entry);
7347                 rii = container_of(entry, struct root_item_info, cache_extent);
7348                 free(rii);
7349         }
7350
7351         free(roots_info_cache);
7352         roots_info_cache = NULL;
7353 }
7354
7355 static int build_roots_info_cache(struct btrfs_fs_info *info)
7356 {
7357         int ret = 0;
7358         struct btrfs_key key;
7359         struct extent_buffer *leaf;
7360         struct btrfs_path *path;
7361
7362         if (!roots_info_cache) {
7363                 roots_info_cache = malloc(sizeof(*roots_info_cache));
7364                 if (!roots_info_cache)
7365                         return -ENOMEM;
7366                 cache_tree_init(roots_info_cache);
7367         }
7368
7369         path = btrfs_alloc_path();
7370         if (!path)
7371                 return -ENOMEM;
7372
7373         key.objectid = 0;
7374         key.type = BTRFS_EXTENT_ITEM_KEY;
7375         key.offset = 0;
7376
7377         ret = btrfs_search_slot(NULL, info->extent_root, &key, path, 0, 0);
7378         if (ret < 0)
7379                 goto out;
7380         leaf = path->nodes[0];
7381
7382         while (1) {
7383                 struct btrfs_key found_key;
7384                 struct btrfs_extent_item *ei;
7385                 struct btrfs_extent_inline_ref *iref;
7386                 int slot = path->slots[0];
7387                 int type;
7388                 u64 flags;
7389                 u64 root_id;
7390                 u8 level;
7391                 struct cache_extent *entry;
7392                 struct root_item_info *rii;
7393
7394                 if (slot >= btrfs_header_nritems(leaf)) {
7395                         ret = btrfs_next_leaf(info->extent_root, path);
7396                         if (ret < 0) {
7397                                 break;
7398                         } else if (ret) {
7399                                 ret = 0;
7400                                 break;
7401                         }
7402                         leaf = path->nodes[0];
7403                         slot = path->slots[0];
7404                 }
7405
7406                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7407
7408                 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
7409                     found_key.type != BTRFS_METADATA_ITEM_KEY)
7410                         goto next;
7411
7412                 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
7413                 flags = btrfs_extent_flags(leaf, ei);
7414
7415                 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
7416                     !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
7417                         goto next;
7418
7419                 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
7420                         iref = (struct btrfs_extent_inline_ref *)(ei + 1);
7421                         level = found_key.offset;
7422                 } else {
7423                         struct btrfs_tree_block_info *info;
7424
7425                         info = (struct btrfs_tree_block_info *)(ei + 1);
7426                         iref = (struct btrfs_extent_inline_ref *)(info + 1);
7427                         level = btrfs_tree_block_level(leaf, info);
7428                 }
7429
7430                 /*
7431                  * For a root extent, it must be of the following type and the
7432                  * first (and only one) iref in the item.
7433                  */
7434                 type = btrfs_extent_inline_ref_type(leaf, iref);
7435                 if (type != BTRFS_TREE_BLOCK_REF_KEY)
7436                         goto next;
7437
7438                 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
7439                 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
7440                 if (!entry) {
7441                         rii = malloc(sizeof(struct root_item_info));
7442                         if (!rii) {
7443                                 ret = -ENOMEM;
7444                                 goto out;
7445                         }
7446                         rii->cache_extent.start = root_id;
7447                         rii->cache_extent.size = 1;
7448                         rii->level = (u8)-1;
7449                         entry = &rii->cache_extent;
7450                         ret = insert_cache_extent(roots_info_cache, entry);
7451                         ASSERT(ret == 0);
7452                 } else {
7453                         rii = container_of(entry, struct root_item_info,
7454                                            cache_extent);
7455                 }
7456
7457                 ASSERT(rii->cache_extent.start == root_id);
7458                 ASSERT(rii->cache_extent.size == 1);
7459
7460                 if (level > rii->level || rii->level == (u8)-1) {
7461                         rii->level = level;
7462                         rii->bytenr = found_key.objectid;
7463                         rii->gen = btrfs_extent_generation(leaf, ei);
7464                         rii->node_count = 1;
7465                 } else if (level == rii->level) {
7466                         rii->node_count++;
7467                 }
7468 next:
7469                 path->slots[0]++;
7470         }
7471
7472 out:
7473         btrfs_free_path(path);
7474
7475         return ret;
7476 }
7477
7478 static int maybe_repair_root_item(struct btrfs_fs_info *info,
7479                                   struct btrfs_path *path,
7480                                   const struct btrfs_key *root_key,
7481                                   const int read_only_mode)
7482 {
7483         const u64 root_id = root_key->objectid;
7484         struct cache_extent *entry;
7485         struct root_item_info *rii;
7486         struct btrfs_root_item ri;
7487         unsigned long offset;
7488
7489         entry = lookup_cache_extent(roots_info_cache, root_id, 1);
7490         if (!entry) {
7491                 fprintf(stderr,
7492                         "Error: could not find extent items for root %llu\n",
7493                         root_key->objectid);
7494                 return -ENOENT;
7495         }
7496
7497         rii = container_of(entry, struct root_item_info, cache_extent);
7498         ASSERT(rii->cache_extent.start == root_id);
7499         ASSERT(rii->cache_extent.size == 1);
7500
7501         if (rii->node_count != 1) {
7502                 fprintf(stderr,
7503                         "Error: could not find btree root extent for root %llu\n",
7504                         root_id);
7505                 return -ENOENT;
7506         }
7507
7508         offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
7509         read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
7510
7511         if (btrfs_root_bytenr(&ri) != rii->bytenr ||
7512             btrfs_root_level(&ri) != rii->level ||
7513             btrfs_root_generation(&ri) != rii->gen) {
7514
7515                 /*
7516                  * If we're in repair mode but our caller told us to not update
7517                  * the root item, i.e. just check if it needs to be updated, don't
7518                  * print this message, since the caller will call us again shortly
7519                  * for the same root item without read only mode (the caller will
7520                  * open a transaction first).
7521                  */
7522                 if (!(read_only_mode && repair))
7523                         fprintf(stderr,
7524                                 "%sroot item for root %llu,"
7525                                 " current bytenr %llu, current gen %llu, current level %u,"
7526                                 " new bytenr %llu, new gen %llu, new level %u\n",
7527                                 (read_only_mode ? "" : "fixing "),
7528                                 root_id,
7529                                 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
7530                                 btrfs_root_level(&ri),
7531                                 rii->bytenr, rii->gen, rii->level);
7532
7533                 if (btrfs_root_generation(&ri) > rii->gen) {
7534                         fprintf(stderr,
7535                                 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
7536                                 root_id, btrfs_root_generation(&ri), rii->gen);
7537                         return -EINVAL;
7538                 }
7539
7540                 if (!read_only_mode) {
7541                         btrfs_set_root_bytenr(&ri, rii->bytenr);
7542                         btrfs_set_root_level(&ri, rii->level);
7543                         btrfs_set_root_generation(&ri, rii->gen);
7544                         write_extent_buffer(path->nodes[0], &ri,
7545                                             offset, sizeof(ri));
7546                 }
7547
7548                 return 1;
7549         }
7550
7551         return 0;
7552 }
7553
7554 /*
7555  * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
7556  * caused read-only snapshots to be corrupted if they were created at a moment
7557  * when the source subvolume/snapshot had orphan items. The issue was that the
7558  * on-disk root items became incorrect, referring to the pre orphan cleanup root
7559  * node instead of the post orphan cleanup root node.
7560  * So this function, and its callees, just detects and fixes those cases. Even
7561  * though the regression was for read-only snapshots, this function applies to
7562  * any snapshot/subvolume root.
7563  * This must be run before any other repair code - not doing it so, makes other
7564  * repair code delete or modify backrefs in the extent tree for example, which
7565  * will result in an inconsistent fs after repairing the root items.
7566  */
7567 static int repair_root_items(struct btrfs_fs_info *info)
7568 {
7569         struct btrfs_path *path = NULL;
7570         struct btrfs_key key;
7571         struct extent_buffer *leaf;
7572         struct btrfs_trans_handle *trans = NULL;
7573         int ret = 0;
7574         int bad_roots = 0;
7575         int need_trans = 0;
7576
7577         ret = build_roots_info_cache(info);
7578         if (ret)
7579                 goto out;
7580
7581         path = btrfs_alloc_path();
7582         if (!path) {
7583                 ret = -ENOMEM;
7584                 goto out;
7585         }
7586
7587         key.objectid = BTRFS_FIRST_FREE_OBJECTID;
7588         key.type = BTRFS_ROOT_ITEM_KEY;
7589         key.offset = 0;
7590
7591 again:
7592         /*
7593          * Avoid opening and committing transactions if a leaf doesn't have
7594          * any root items that need to be fixed, so that we avoid rotating
7595          * backup roots unnecessarily.
7596          */
7597         if (need_trans) {
7598                 trans = btrfs_start_transaction(info->tree_root, 1);
7599                 if (IS_ERR(trans)) {
7600                         ret = PTR_ERR(trans);
7601                         goto out;
7602                 }
7603         }
7604
7605         ret = btrfs_search_slot(trans, info->tree_root, &key, path,
7606                                 0, trans ? 1 : 0);
7607         if (ret < 0)
7608                 goto out;
7609         leaf = path->nodes[0];
7610
7611         while (1) {
7612                 struct btrfs_key found_key;
7613
7614                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7615                         int no_more_keys = find_next_key(path, &key);
7616
7617                         btrfs_release_path(path);
7618                         if (trans) {
7619                                 ret = btrfs_commit_transaction(trans,
7620                                                                info->tree_root);
7621                                 trans = NULL;
7622                                 if (ret < 0)
7623                                         goto out;
7624                         }
7625                         need_trans = 0;
7626                         if (no_more_keys)
7627                                 break;
7628                         goto again;
7629                 }
7630
7631                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7632
7633                 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
7634                         goto next;
7635
7636                 ret = maybe_repair_root_item(info, path, &found_key,
7637                                              trans ? 0 : 1);
7638                 if (ret < 0)
7639                         goto out;
7640                 if (ret) {
7641                         if (!trans && repair) {
7642                                 need_trans = 1;
7643                                 key = found_key;
7644                                 btrfs_release_path(path);
7645                                 goto again;
7646                         }
7647                         bad_roots++;
7648                 }
7649 next:
7650                 path->slots[0]++;
7651         }
7652         ret = 0;
7653 out:
7654         free_roots_info_cache();
7655         if (path)
7656                 btrfs_free_path(path);
7657         if (ret < 0)
7658                 return ret;
7659
7660         return bad_roots;
7661 }
7662
7663 static struct option long_options[] = {
7664         { "super", 1, NULL, 's' },
7665         { "repair", 0, NULL, 0 },
7666         { "init-csum-tree", 0, NULL, 0 },
7667         { "init-extent-tree", 0, NULL, 0 },
7668         { "check-data-csum", 0, NULL, 0 },
7669         { "backup", 0, NULL, 0 },
7670         { "subvol-extents", 1, NULL, 'E' },
7671         { "qgroup-report", 0, NULL, 'Q' },
7672         { NULL, 0, NULL, 0}
7673 };
7674
7675 const char * const cmd_check_usage[] = {
7676         "btrfs check [options] <device>",
7677         "Check an unmounted btrfs filesystem.",
7678         "",
7679         "-s|--super <superblock>     use this superblock copy",
7680         "-b|--backup                 use the backup root copy",
7681         "--repair                    try to repair the filesystem",
7682         "--init-csum-tree            create a new CRC tree",
7683         "--init-extent-tree          create a new extent tree",
7684         "--check-data-csum           verify checkums of data blocks",
7685         "--qgroup-report             print a report on qgroup consistency",
7686         "--subvol-extents <subvolid> print subvolume extents and sharing state",
7687         NULL
7688 };
7689
7690 int cmd_check(int argc, char **argv)
7691 {
7692         struct cache_tree root_cache;
7693         struct btrfs_root *root;
7694         struct btrfs_fs_info *info;
7695         u64 bytenr = 0;
7696         u64 subvolid = 0;
7697         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
7698         int ret;
7699         u64 num;
7700         int option_index = 0;
7701         int init_csum_tree = 0;
7702         int qgroup_report = 0;
7703         enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_EXCLUSIVE;
7704
7705         while(1) {
7706                 int c;
7707                 c = getopt_long(argc, argv, "as:b", long_options,
7708                                 &option_index);
7709                 if (c < 0)
7710                         break;
7711                 switch(c) {
7712                         case 'a': /* ignored */ break;
7713                         case 'b':
7714                                 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
7715                                 break;
7716                         case 's':
7717                                 num = arg_strtou64(optarg);
7718                                 if (num >= BTRFS_SUPER_MIRROR_MAX) {
7719                                         fprintf(stderr,
7720                                                 "ERROR: super mirror should be less than: %d\n",
7721                                                 BTRFS_SUPER_MIRROR_MAX);
7722                                         exit(1);
7723                                 }
7724                                 bytenr = btrfs_sb_offset(((int)num));
7725                                 printf("using SB copy %llu, bytenr %llu\n", num,
7726                                        (unsigned long long)bytenr);
7727                                 break;
7728                         case 'Q':
7729                                 qgroup_report = 1;
7730                                 break;
7731                         case 'E':
7732                                 subvolid = arg_strtou64(optarg);
7733                                 break;
7734                         case '?':
7735                         case 'h':
7736                                 usage(cmd_check_usage);
7737                 }
7738                 if (option_index == 1) {
7739                         printf("enabling repair mode\n");
7740                         repair = 1;
7741                         ctree_flags |= OPEN_CTREE_WRITES;
7742                 } else if (option_index == 2) {
7743                         printf("Creating a new CRC tree\n");
7744                         init_csum_tree = 1;
7745                         repair = 1;
7746                         ctree_flags |= OPEN_CTREE_WRITES;
7747                 } else if (option_index == 3) {
7748                         init_extent_tree = 1;
7749                         ctree_flags |= (OPEN_CTREE_WRITES |
7750                                         OPEN_CTREE_NO_BLOCK_GROUPS);
7751                         repair = 1;
7752                 } else if (option_index == 4) {
7753                         check_data_csum = 1;
7754                 }
7755         }
7756         argc = argc - optind;
7757
7758         if (check_argc_exact(argc, 1))
7759                 usage(cmd_check_usage);
7760
7761         radix_tree_init();
7762         cache_tree_init(&root_cache);
7763
7764         if((ret = check_mounted(argv[optind])) < 0) {
7765                 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
7766                 goto err_out;
7767         } else if(ret) {
7768                 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
7769                 ret = -EBUSY;
7770                 goto err_out;
7771         }
7772
7773         /* only allow partial opening under repair mode */
7774         if (repair)
7775                 ctree_flags |= OPEN_CTREE_PARTIAL;
7776
7777         info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
7778         if (!info) {
7779                 fprintf(stderr, "Couldn't open file system\n");
7780                 ret = -EIO;
7781                 goto err_out;
7782         }
7783
7784         root = info->fs_root;
7785
7786         ret = repair_root_items(info);
7787         if (ret < 0)
7788                 goto close_out;
7789         if (repair) {
7790                 fprintf(stderr, "Fixed %d roots.\n", ret);
7791                 ret = 0;
7792         } else if (ret > 0) {
7793                 fprintf(stderr,
7794                        "Found %d roots with an outdated root item.\n",
7795                        ret);
7796                 fprintf(stderr,
7797                         "Please run a filesystem check with the option --repair to fix them.\n");
7798                 ret = 1;
7799                 goto close_out;
7800         }
7801
7802         /*
7803          * repair mode will force us to commit transaction which
7804          * will make us fail to load log tree when mounting.
7805          */
7806         if (repair && btrfs_super_log_root(info->super_copy)) {
7807                 ret = ask_user("repair mode will force to clear out log tree, Are you sure?");
7808                 if (!ret) {
7809                         ret = 1;
7810                         goto close_out;
7811                 }
7812                 ret = zero_log_tree(root);
7813                 if (ret) {
7814                         fprintf(stderr, "fail to zero log tree\n");
7815                         goto close_out;
7816                 }
7817         }
7818
7819         uuid_unparse(info->super_copy->fsid, uuidbuf);
7820         if (qgroup_report) {
7821                 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
7822                        uuidbuf);
7823                 ret = qgroup_verify_all(info);
7824                 if (ret == 0)
7825                         print_qgroup_report(1);
7826                 goto close_out;
7827         }
7828         if (subvolid) {
7829                 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
7830                        subvolid, argv[optind], uuidbuf);
7831                 ret = print_extent_state(info, subvolid);
7832                 goto close_out;
7833         }
7834         printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
7835
7836         if (!extent_buffer_uptodate(info->tree_root->node) ||
7837             !extent_buffer_uptodate(info->dev_root->node) ||
7838             !extent_buffer_uptodate(info->chunk_root->node)) {
7839                 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7840                 ret = -EIO;
7841                 goto close_out;
7842         }
7843
7844         if (init_extent_tree || init_csum_tree) {
7845                 struct btrfs_trans_handle *trans;
7846
7847                 trans = btrfs_start_transaction(info->extent_root, 0);
7848                 if (IS_ERR(trans)) {
7849                         fprintf(stderr, "Error starting transaction\n");
7850                         ret = PTR_ERR(trans);
7851                         goto close_out;
7852                 }
7853
7854                 if (init_extent_tree) {
7855                         printf("Creating a new extent tree\n");
7856                         ret = reinit_extent_tree(trans, info);
7857                         if (ret)
7858                                 goto close_out;
7859                 }
7860
7861                 if (init_csum_tree) {
7862                         fprintf(stderr, "Reinit crc root\n");
7863                         ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
7864                         if (ret) {
7865                                 fprintf(stderr, "crc root initialization failed\n");
7866                                 ret = -EIO;
7867                                 goto close_out;
7868                         }
7869
7870                         ret = fill_csum_tree(trans, info->csum_root);
7871                         if (ret) {
7872                                 fprintf(stderr, "crc refilling failed\n");
7873                                 return -EIO;
7874                         }
7875                 }
7876                 /*
7877                  * Ok now we commit and run the normal fsck, which will add
7878                  * extent entries for all of the items it finds.
7879                  */
7880                 ret = btrfs_commit_transaction(trans, info->extent_root);
7881                 if (ret)
7882                         goto close_out;
7883         }
7884         if (!extent_buffer_uptodate(info->extent_root->node)) {
7885                 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7886                 ret = -EIO;
7887                 goto close_out;
7888         }
7889         if (!extent_buffer_uptodate(info->csum_root->node)) {
7890                 fprintf(stderr, "Checksum root corrupted, rerun with --init-csum-tree option\n");
7891                 ret = -EIO;
7892                 goto close_out;
7893         }
7894
7895         fprintf(stderr, "checking extents\n");
7896         ret = check_chunks_and_extents(root);
7897         if (ret)
7898                 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
7899
7900         fprintf(stderr, "checking free space cache\n");
7901         ret = check_space_cache(root);
7902         if (ret)
7903                 goto out;
7904
7905         /*
7906          * We used to have to have these hole extents in between our real
7907          * extents so if we don't have this flag set we need to make sure there
7908          * are no gaps in the file extents for inodes, otherwise we can just
7909          * ignore it when this happens.
7910          */
7911         no_holes = btrfs_fs_incompat(root->fs_info,
7912                                      BTRFS_FEATURE_INCOMPAT_NO_HOLES);
7913         fprintf(stderr, "checking fs roots\n");
7914         ret = check_fs_roots(root, &root_cache);
7915         if (ret)
7916                 goto out;
7917
7918         fprintf(stderr, "checking csums\n");
7919         ret = check_csums(root);
7920         if (ret)
7921                 goto out;
7922
7923         fprintf(stderr, "checking root refs\n");
7924         ret = check_root_refs(root, &root_cache);
7925         if (ret)
7926                 goto out;
7927
7928         while (repair && !list_empty(&root->fs_info->recow_ebs)) {
7929                 struct extent_buffer *eb;
7930
7931                 eb = list_first_entry(&root->fs_info->recow_ebs,
7932                                       struct extent_buffer, recow);
7933                 list_del_init(&eb->recow);
7934                 ret = recow_extent_buffer(root, eb);
7935                 if (ret)
7936                         break;
7937         }
7938
7939         while (!list_empty(&delete_items)) {
7940                 struct bad_item *bad;
7941
7942                 bad = list_first_entry(&delete_items, struct bad_item, list);
7943                 list_del_init(&bad->list);
7944                 if (repair)
7945                         ret = delete_bad_item(root, bad);
7946                 free(bad);
7947         }
7948
7949         if (info->quota_enabled) {
7950                 int err;
7951                 fprintf(stderr, "checking quota groups\n");
7952                 err = qgroup_verify_all(info);
7953                 if (err)
7954                         goto out;
7955         }
7956
7957         if (!list_empty(&root->fs_info->recow_ebs)) {
7958                 fprintf(stderr, "Transid errors in file system\n");
7959                 ret = 1;
7960         }
7961 out:
7962         print_qgroup_report(0);
7963         if (found_old_backref) { /*
7964                  * there was a disk format change when mixed
7965                  * backref was in testing tree. The old format
7966                  * existed about one week.
7967                  */
7968                 printf("\n * Found old mixed backref format. "
7969                        "The old format is not supported! *"
7970                        "\n * Please mount the FS in readonly mode, "
7971                        "backup data and re-format the FS. *\n\n");
7972                 ret = 1;
7973         }
7974         printf("found %llu bytes used err is %d\n",
7975                (unsigned long long)bytes_used, ret);
7976         printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
7977         printf("total tree bytes: %llu\n",
7978                (unsigned long long)total_btree_bytes);
7979         printf("total fs tree bytes: %llu\n",
7980                (unsigned long long)total_fs_tree_bytes);
7981         printf("total extent tree bytes: %llu\n",
7982                (unsigned long long)total_extent_tree_bytes);
7983         printf("btree space waste bytes: %llu\n",
7984                (unsigned long long)btree_space_waste);
7985         printf("file data blocks allocated: %llu\n referenced %llu\n",
7986                 (unsigned long long)data_bytes_allocated,
7987                 (unsigned long long)data_bytes_referenced);
7988         printf("%s\n", BTRFS_BUILD_VERSION);
7989
7990         free_root_recs_tree(&root_cache);
7991 close_out:
7992         close_ctree(root);
7993 err_out:
7994         return ret;
7995 }