btrfs-progs: print root dir verbose error in fsck
[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                         print_inode_error(root, rec);
1988                         error++;
1989                 }
1990         } else {
1991                 if (repair) {
1992                         struct btrfs_trans_handle *trans;
1993
1994                         trans = btrfs_start_transaction(root, 1);
1995                         if (IS_ERR(trans)) {
1996                                 err = PTR_ERR(trans);
1997                                 return err;
1998                         }
1999
2000                         fprintf(stderr,
2001                                 "root %llu missing its root dir, recreating\n",
2002                                 (unsigned long long)root->objectid);
2003
2004                         ret = btrfs_make_root_dir(trans, root, root_dirid);
2005                         BUG_ON(ret);
2006
2007                         btrfs_commit_transaction(trans, root);
2008                         return -EAGAIN;
2009                 }
2010
2011                 fprintf(stderr, "root %llu root dir %llu not found\n",
2012                         (unsigned long long)root->root_key.objectid,
2013                         (unsigned long long)root_dirid);
2014         }
2015
2016         while (1) {
2017                 cache = search_cache_extent(inode_cache, 0);
2018                 if (!cache)
2019                         break;
2020                 node = container_of(cache, struct ptr_node, cache);
2021                 rec = node->data;
2022                 remove_cache_extent(inode_cache, &node->cache);
2023                 free(node);
2024                 if (rec->ino == root_dirid ||
2025                     rec->ino == BTRFS_ORPHAN_OBJECTID) {
2026                         free_inode_rec(rec);
2027                         continue;
2028                 }
2029
2030                 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
2031                         ret = check_orphan_item(root, rec->ino);
2032                         if (ret == 0)
2033                                 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2034                         if (can_free_inode_rec(rec)) {
2035                                 free_inode_rec(rec);
2036                                 continue;
2037                         }
2038                 }
2039
2040                 if (repair) {
2041                         ret = try_repair_inode(root, rec);
2042                         if (ret == 0 && can_free_inode_rec(rec)) {
2043                                 free_inode_rec(rec);
2044                                 continue;
2045                         }
2046                         ret = 0;
2047                 }
2048
2049                 error++;
2050                 if (!rec->found_inode_item)
2051                         rec->errors |= I_ERR_NO_INODE_ITEM;
2052                 if (rec->found_link != rec->nlink)
2053                         rec->errors |= I_ERR_LINK_COUNT_WRONG;
2054                 print_inode_error(root, rec);
2055                 list_for_each_entry(backref, &rec->backrefs, list) {
2056                         if (!backref->found_dir_item)
2057                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
2058                         if (!backref->found_dir_index)
2059                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
2060                         if (!backref->found_inode_ref)
2061                                 backref->errors |= REF_ERR_NO_INODE_REF;
2062                         fprintf(stderr, "\tunresolved ref dir %llu index %llu"
2063                                 " namelen %u name %s filetype %d errors %x",
2064                                 (unsigned long long)backref->dir,
2065                                 (unsigned long long)backref->index,
2066                                 backref->namelen, backref->name,
2067                                 backref->filetype, backref->errors);
2068                         print_ref_error(backref->errors);
2069                 }
2070                 free_inode_rec(rec);
2071         }
2072         return (error > 0) ? -1 : 0;
2073 }
2074
2075 static struct root_record *get_root_rec(struct cache_tree *root_cache,
2076                                         u64 objectid)
2077 {
2078         struct cache_extent *cache;
2079         struct root_record *rec = NULL;
2080         int ret;
2081
2082         cache = lookup_cache_extent(root_cache, objectid, 1);
2083         if (cache) {
2084                 rec = container_of(cache, struct root_record, cache);
2085         } else {
2086                 rec = calloc(1, sizeof(*rec));
2087                 rec->objectid = objectid;
2088                 INIT_LIST_HEAD(&rec->backrefs);
2089                 rec->cache.start = objectid;
2090                 rec->cache.size = 1;
2091
2092                 ret = insert_cache_extent(root_cache, &rec->cache);
2093                 BUG_ON(ret);
2094         }
2095         return rec;
2096 }
2097
2098 static struct root_backref *get_root_backref(struct root_record *rec,
2099                                              u64 ref_root, u64 dir, u64 index,
2100                                              const char *name, int namelen)
2101 {
2102         struct root_backref *backref;
2103
2104         list_for_each_entry(backref, &rec->backrefs, list) {
2105                 if (backref->ref_root != ref_root || backref->dir != dir ||
2106                     backref->namelen != namelen)
2107                         continue;
2108                 if (memcmp(name, backref->name, namelen))
2109                         continue;
2110                 return backref;
2111         }
2112
2113         backref = malloc(sizeof(*backref) + namelen + 1);
2114         memset(backref, 0, sizeof(*backref));
2115         backref->ref_root = ref_root;
2116         backref->dir = dir;
2117         backref->index = index;
2118         backref->namelen = namelen;
2119         memcpy(backref->name, name, namelen);
2120         backref->name[namelen] = '\0';
2121         list_add_tail(&backref->list, &rec->backrefs);
2122         return backref;
2123 }
2124
2125 static void free_root_record(struct cache_extent *cache)
2126 {
2127         struct root_record *rec;
2128         struct root_backref *backref;
2129
2130         rec = container_of(cache, struct root_record, cache);
2131         while (!list_empty(&rec->backrefs)) {
2132                 backref = list_entry(rec->backrefs.next,
2133                                      struct root_backref, list);
2134                 list_del(&backref->list);
2135                 free(backref);
2136         }
2137
2138         kfree(rec);
2139 }
2140
2141 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
2142
2143 static int add_root_backref(struct cache_tree *root_cache,
2144                             u64 root_id, u64 ref_root, u64 dir, u64 index,
2145                             const char *name, int namelen,
2146                             int item_type, int errors)
2147 {
2148         struct root_record *rec;
2149         struct root_backref *backref;
2150
2151         rec = get_root_rec(root_cache, root_id);
2152         backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
2153
2154         backref->errors |= errors;
2155
2156         if (item_type != BTRFS_DIR_ITEM_KEY) {
2157                 if (backref->found_dir_index || backref->found_back_ref ||
2158                     backref->found_forward_ref) {
2159                         if (backref->index != index)
2160                                 backref->errors |= REF_ERR_INDEX_UNMATCH;
2161                 } else {
2162                         backref->index = index;
2163                 }
2164         }
2165
2166         if (item_type == BTRFS_DIR_ITEM_KEY) {
2167                 if (backref->found_forward_ref)
2168                         rec->found_ref++;
2169                 backref->found_dir_item = 1;
2170         } else if (item_type == BTRFS_DIR_INDEX_KEY) {
2171                 backref->found_dir_index = 1;
2172         } else if (item_type == BTRFS_ROOT_REF_KEY) {
2173                 if (backref->found_forward_ref)
2174                         backref->errors |= REF_ERR_DUP_ROOT_REF;
2175                 else if (backref->found_dir_item)
2176                         rec->found_ref++;
2177                 backref->found_forward_ref = 1;
2178         } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
2179                 if (backref->found_back_ref)
2180                         backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
2181                 backref->found_back_ref = 1;
2182         } else {
2183                 BUG_ON(1);
2184         }
2185
2186         if (backref->found_forward_ref && backref->found_dir_item)
2187                 backref->reachable = 1;
2188         return 0;
2189 }
2190
2191 static int merge_root_recs(struct btrfs_root *root,
2192                            struct cache_tree *src_cache,
2193                            struct cache_tree *dst_cache)
2194 {
2195         struct cache_extent *cache;
2196         struct ptr_node *node;
2197         struct inode_record *rec;
2198         struct inode_backref *backref;
2199         int ret = 0;
2200
2201         if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2202                 free_inode_recs_tree(src_cache);
2203                 return 0;
2204         }
2205
2206         while (1) {
2207                 cache = search_cache_extent(src_cache, 0);
2208                 if (!cache)
2209                         break;
2210                 node = container_of(cache, struct ptr_node, cache);
2211                 rec = node->data;
2212                 remove_cache_extent(src_cache, &node->cache);
2213                 free(node);
2214
2215                 ret = is_child_root(root, root->objectid, rec->ino);
2216                 if (ret < 0)
2217                         break;
2218                 else if (ret == 0)
2219                         goto skip;
2220
2221                 list_for_each_entry(backref, &rec->backrefs, list) {
2222                         BUG_ON(backref->found_inode_ref);
2223                         if (backref->found_dir_item)
2224                                 add_root_backref(dst_cache, rec->ino,
2225                                         root->root_key.objectid, backref->dir,
2226                                         backref->index, backref->name,
2227                                         backref->namelen, BTRFS_DIR_ITEM_KEY,
2228                                         backref->errors);
2229                         if (backref->found_dir_index)
2230                                 add_root_backref(dst_cache, rec->ino,
2231                                         root->root_key.objectid, backref->dir,
2232                                         backref->index, backref->name,
2233                                         backref->namelen, BTRFS_DIR_INDEX_KEY,
2234                                         backref->errors);
2235                 }
2236 skip:
2237                 free_inode_rec(rec);
2238         }
2239         if (ret < 0)
2240                 return ret;
2241         return 0;
2242 }
2243
2244 static int check_root_refs(struct btrfs_root *root,
2245                            struct cache_tree *root_cache)
2246 {
2247         struct root_record *rec;
2248         struct root_record *ref_root;
2249         struct root_backref *backref;
2250         struct cache_extent *cache;
2251         int loop = 1;
2252         int ret;
2253         int error;
2254         int errors = 0;
2255
2256         rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
2257         rec->found_ref = 1;
2258
2259         /* fixme: this can not detect circular references */
2260         while (loop) {
2261                 loop = 0;
2262                 cache = search_cache_extent(root_cache, 0);
2263                 while (1) {
2264                         if (!cache)
2265                                 break;
2266                         rec = container_of(cache, struct root_record, cache);
2267                         cache = next_cache_extent(cache);
2268
2269                         if (rec->found_ref == 0)
2270                                 continue;
2271
2272                         list_for_each_entry(backref, &rec->backrefs, list) {
2273                                 if (!backref->reachable)
2274                                         continue;
2275
2276                                 ref_root = get_root_rec(root_cache,
2277                                                         backref->ref_root);
2278                                 if (ref_root->found_ref > 0)
2279                                         continue;
2280
2281                                 backref->reachable = 0;
2282                                 rec->found_ref--;
2283                                 if (rec->found_ref == 0)
2284                                         loop = 1;
2285                         }
2286                 }
2287         }
2288
2289         cache = search_cache_extent(root_cache, 0);
2290         while (1) {
2291                 if (!cache)
2292                         break;
2293                 rec = container_of(cache, struct root_record, cache);
2294                 cache = next_cache_extent(cache);
2295
2296                 if (rec->found_ref == 0 &&
2297                     rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
2298                     rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
2299                         ret = check_orphan_item(root->fs_info->tree_root,
2300                                                 rec->objectid);
2301                         if (ret == 0)
2302                                 continue;
2303
2304                         /*
2305                          * If we don't have a root item then we likely just have
2306                          * a dir item in a snapshot for this root but no actual
2307                          * ref key or anything so it's meaningless.
2308                          */
2309                         if (!rec->found_root_item)
2310                                 continue;
2311                         errors++;
2312                         fprintf(stderr, "fs tree %llu not referenced\n",
2313                                 (unsigned long long)rec->objectid);
2314                 }
2315
2316                 error = 0;
2317                 if (rec->found_ref > 0 && !rec->found_root_item)
2318                         error = 1;
2319                 list_for_each_entry(backref, &rec->backrefs, list) {
2320                         if (!backref->found_dir_item)
2321                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
2322                         if (!backref->found_dir_index)
2323                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
2324                         if (!backref->found_back_ref)
2325                                 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
2326                         if (!backref->found_forward_ref)
2327                                 backref->errors |= REF_ERR_NO_ROOT_REF;
2328                         if (backref->reachable && backref->errors)
2329                                 error = 1;
2330                 }
2331                 if (!error)
2332                         continue;
2333
2334                 errors++;
2335                 fprintf(stderr, "fs tree %llu refs %u %s\n",
2336                         (unsigned long long)rec->objectid, rec->found_ref,
2337                          rec->found_root_item ? "" : "not found");
2338
2339                 list_for_each_entry(backref, &rec->backrefs, list) {
2340                         if (!backref->reachable)
2341                                 continue;
2342                         if (!backref->errors && rec->found_root_item)
2343                                 continue;
2344                         fprintf(stderr, "\tunresolved ref root %llu dir %llu"
2345                                 " index %llu namelen %u name %s errors %x\n",
2346                                 (unsigned long long)backref->ref_root,
2347                                 (unsigned long long)backref->dir,
2348                                 (unsigned long long)backref->index,
2349                                 backref->namelen, backref->name,
2350                                 backref->errors);
2351                         print_ref_error(backref->errors);
2352                 }
2353         }
2354         return errors > 0 ? 1 : 0;
2355 }
2356
2357 static int process_root_ref(struct extent_buffer *eb, int slot,
2358                             struct btrfs_key *key,
2359                             struct cache_tree *root_cache)
2360 {
2361         u64 dirid;
2362         u64 index;
2363         u32 len;
2364         u32 name_len;
2365         struct btrfs_root_ref *ref;
2366         char namebuf[BTRFS_NAME_LEN];
2367         int error;
2368
2369         ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
2370
2371         dirid = btrfs_root_ref_dirid(eb, ref);
2372         index = btrfs_root_ref_sequence(eb, ref);
2373         name_len = btrfs_root_ref_name_len(eb, ref);
2374
2375         if (name_len <= BTRFS_NAME_LEN) {
2376                 len = name_len;
2377                 error = 0;
2378         } else {
2379                 len = BTRFS_NAME_LEN;
2380                 error = REF_ERR_NAME_TOO_LONG;
2381         }
2382         read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
2383
2384         if (key->type == BTRFS_ROOT_REF_KEY) {
2385                 add_root_backref(root_cache, key->offset, key->objectid, dirid,
2386                                  index, namebuf, len, key->type, error);
2387         } else {
2388                 add_root_backref(root_cache, key->objectid, key->offset, dirid,
2389                                  index, namebuf, len, key->type, error);
2390         }
2391         return 0;
2392 }
2393
2394 static int check_fs_root(struct btrfs_root *root,
2395                          struct cache_tree *root_cache,
2396                          struct walk_control *wc)
2397 {
2398         int ret = 0;
2399         int err = 0;
2400         int wret;
2401         int level;
2402         struct btrfs_path path;
2403         struct shared_node root_node;
2404         struct root_record *rec;
2405         struct btrfs_root_item *root_item = &root->root_item;
2406         enum btrfs_tree_block_status status;
2407
2408         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2409                 rec = get_root_rec(root_cache, root->root_key.objectid);
2410                 if (btrfs_root_refs(root_item) > 0)
2411                         rec->found_root_item = 1;
2412         }
2413
2414         btrfs_init_path(&path);
2415         memset(&root_node, 0, sizeof(root_node));
2416         cache_tree_init(&root_node.root_cache);
2417         cache_tree_init(&root_node.inode_cache);
2418
2419         level = btrfs_header_level(root->node);
2420         memset(wc->nodes, 0, sizeof(wc->nodes));
2421         wc->nodes[level] = &root_node;
2422         wc->active_node = level;
2423         wc->root_level = level;
2424
2425         /* We may not have checked the root block, lets do that now */
2426         if (btrfs_is_leaf(root->node))
2427                 status = btrfs_check_leaf(root, NULL, root->node);
2428         else
2429                 status = btrfs_check_node(root, NULL, root->node);
2430         if (status != BTRFS_TREE_BLOCK_CLEAN)
2431                 return -EIO;
2432
2433         if (btrfs_root_refs(root_item) > 0 ||
2434             btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2435                 path.nodes[level] = root->node;
2436                 extent_buffer_get(root->node);
2437                 path.slots[level] = 0;
2438         } else {
2439                 struct btrfs_key key;
2440                 struct btrfs_disk_key found_key;
2441
2442                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2443                 level = root_item->drop_level;
2444                 path.lowest_level = level;
2445                 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2446                 if (wret < 0)
2447                         goto skip_walking;
2448                 btrfs_node_key(path.nodes[level], &found_key,
2449                                 path.slots[level]);
2450                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2451                                         sizeof(found_key)));
2452         }
2453
2454         while (1) {
2455                 wret = walk_down_tree(root, &path, wc, &level);
2456                 if (wret < 0)
2457                         ret = wret;
2458                 if (wret != 0)
2459                         break;
2460
2461                 wret = walk_up_tree(root, &path, wc, &level);
2462                 if (wret < 0)
2463                         ret = wret;
2464                 if (wret != 0)
2465                         break;
2466         }
2467 skip_walking:
2468         btrfs_release_path(&path);
2469
2470         err = merge_root_recs(root, &root_node.root_cache, root_cache);
2471         if (err < 0)
2472                 ret = err;
2473
2474         if (root_node.current) {
2475                 root_node.current->checked = 1;
2476                 maybe_free_inode_rec(&root_node.inode_cache,
2477                                 root_node.current);
2478         }
2479
2480         err = check_inode_recs(root, &root_node.inode_cache);
2481         if (!ret)
2482                 ret = err;
2483         return ret;
2484 }
2485
2486 static int fs_root_objectid(u64 objectid)
2487 {
2488         if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
2489             objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2490                 return 1;
2491         return is_fstree(objectid);
2492 }
2493
2494 static int check_fs_roots(struct btrfs_root *root,
2495                           struct cache_tree *root_cache)
2496 {
2497         struct btrfs_path path;
2498         struct btrfs_key key;
2499         struct walk_control wc;
2500         struct extent_buffer *leaf, *tree_node;
2501         struct btrfs_root *tmp_root;
2502         struct btrfs_root *tree_root = root->fs_info->tree_root;
2503         int ret;
2504         int err = 0;
2505
2506         /*
2507          * Just in case we made any changes to the extent tree that weren't
2508          * reflected into the free space cache yet.
2509          */
2510         if (repair)
2511                 reset_cached_block_groups(root->fs_info);
2512         memset(&wc, 0, sizeof(wc));
2513         cache_tree_init(&wc.shared);
2514         btrfs_init_path(&path);
2515
2516 again:
2517         key.offset = 0;
2518         key.objectid = 0;
2519         key.type = BTRFS_ROOT_ITEM_KEY;
2520         ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2521         if (ret < 0) {
2522                 err = 1;
2523                 goto out;
2524         }
2525         tree_node = tree_root->node;
2526         while (1) {
2527                 if (tree_node != tree_root->node) {
2528                         free_root_recs_tree(root_cache);
2529                         btrfs_release_path(&path);
2530                         goto again;
2531                 }
2532                 leaf = path.nodes[0];
2533                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2534                         ret = btrfs_next_leaf(tree_root, &path);
2535                         if (ret) {
2536                                 if (ret < 0)
2537                                         err = 1;
2538                                 break;
2539                         }
2540                         leaf = path.nodes[0];
2541                 }
2542                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2543                 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2544                     fs_root_objectid(key.objectid)) {
2545                         if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2546                                 tmp_root = btrfs_read_fs_root_no_cache(
2547                                                 root->fs_info, &key);
2548                         } else {
2549                                 key.offset = (u64)-1;
2550                                 tmp_root = btrfs_read_fs_root(
2551                                                 root->fs_info, &key);
2552                         }
2553                         if (IS_ERR(tmp_root)) {
2554                                 err = 1;
2555                                 goto next;
2556                         }
2557                         ret = check_fs_root(tmp_root, root_cache, &wc);
2558                         if (ret == -EAGAIN) {
2559                                 free_root_recs_tree(root_cache);
2560                                 btrfs_release_path(&path);
2561                                 goto again;
2562                         }
2563                         if (ret)
2564                                 err = 1;
2565                         if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
2566                                 btrfs_free_fs_root(tmp_root);
2567                 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2568                            key.type == BTRFS_ROOT_BACKREF_KEY) {
2569                         process_root_ref(leaf, path.slots[0], &key,
2570                                          root_cache);
2571                 }
2572 next:
2573                 path.slots[0]++;
2574         }
2575 out:
2576         btrfs_release_path(&path);
2577         if (err)
2578                 free_extent_cache_tree(&wc.shared);
2579         if (!cache_tree_empty(&wc.shared))
2580                 fprintf(stderr, "warning line %d\n", __LINE__);
2581
2582         return err;
2583 }
2584
2585 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2586 {
2587         struct list_head *cur = rec->backrefs.next;
2588         struct extent_backref *back;
2589         struct tree_backref *tback;
2590         struct data_backref *dback;
2591         u64 found = 0;
2592         int err = 0;
2593
2594         while(cur != &rec->backrefs) {
2595                 back = list_entry(cur, struct extent_backref, list);
2596                 cur = cur->next;
2597                 if (!back->found_extent_tree) {
2598                         err = 1;
2599                         if (!print_errs)
2600                                 goto out;
2601                         if (back->is_data) {
2602                                 dback = (struct data_backref *)back;
2603                                 fprintf(stderr, "Backref %llu %s %llu"
2604                                         " owner %llu offset %llu num_refs %lu"
2605                                         " not found in extent tree\n",
2606                                         (unsigned long long)rec->start,
2607                                         back->full_backref ?
2608                                         "parent" : "root",
2609                                         back->full_backref ?
2610                                         (unsigned long long)dback->parent:
2611                                         (unsigned long long)dback->root,
2612                                         (unsigned long long)dback->owner,
2613                                         (unsigned long long)dback->offset,
2614                                         (unsigned long)dback->num_refs);
2615                         } else {
2616                                 tback = (struct tree_backref *)back;
2617                                 fprintf(stderr, "Backref %llu parent %llu"
2618                                         " root %llu not found in extent tree\n",
2619                                         (unsigned long long)rec->start,
2620                                         (unsigned long long)tback->parent,
2621                                         (unsigned long long)tback->root);
2622                         }
2623                 }
2624                 if (!back->is_data && !back->found_ref) {
2625                         err = 1;
2626                         if (!print_errs)
2627                                 goto out;
2628                         tback = (struct tree_backref *)back;
2629                         fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2630                                 (unsigned long long)rec->start,
2631                                 back->full_backref ? "parent" : "root",
2632                                 back->full_backref ?
2633                                 (unsigned long long)tback->parent :
2634                                 (unsigned long long)tback->root, back);
2635                 }
2636                 if (back->is_data) {
2637                         dback = (struct data_backref *)back;
2638                         if (dback->found_ref != dback->num_refs) {
2639                                 err = 1;
2640                                 if (!print_errs)
2641                                         goto out;
2642                                 fprintf(stderr, "Incorrect local backref count"
2643                                         " on %llu %s %llu owner %llu"
2644                                         " offset %llu found %u wanted %u back %p\n",
2645                                         (unsigned long long)rec->start,
2646                                         back->full_backref ?
2647                                         "parent" : "root",
2648                                         back->full_backref ?
2649                                         (unsigned long long)dback->parent:
2650                                         (unsigned long long)dback->root,
2651                                         (unsigned long long)dback->owner,
2652                                         (unsigned long long)dback->offset,
2653                                         dback->found_ref, dback->num_refs, back);
2654                         }
2655                         if (dback->disk_bytenr != rec->start) {
2656                                 err = 1;
2657                                 if (!print_errs)
2658                                         goto out;
2659                                 fprintf(stderr, "Backref disk bytenr does not"
2660                                         " match extent record, bytenr=%llu, "
2661                                         "ref bytenr=%llu\n",
2662                                         (unsigned long long)rec->start,
2663                                         (unsigned long long)dback->disk_bytenr);
2664                         }
2665
2666                         if (dback->bytes != rec->nr) {
2667                                 err = 1;
2668                                 if (!print_errs)
2669                                         goto out;
2670                                 fprintf(stderr, "Backref bytes do not match "
2671                                         "extent backref, bytenr=%llu, ref "
2672                                         "bytes=%llu, backref bytes=%llu\n",
2673                                         (unsigned long long)rec->start,
2674                                         (unsigned long long)rec->nr,
2675                                         (unsigned long long)dback->bytes);
2676                         }
2677                 }
2678                 if (!back->is_data) {
2679                         found += 1;
2680                 } else {
2681                         dback = (struct data_backref *)back;
2682                         found += dback->found_ref;
2683                 }
2684         }
2685         if (found != rec->refs) {
2686                 err = 1;
2687                 if (!print_errs)
2688                         goto out;
2689                 fprintf(stderr, "Incorrect global backref count "
2690                         "on %llu found %llu wanted %llu\n",
2691                         (unsigned long long)rec->start,
2692                         (unsigned long long)found,
2693                         (unsigned long long)rec->refs);
2694         }
2695 out:
2696         return err;
2697 }
2698
2699 static int free_all_extent_backrefs(struct extent_record *rec)
2700 {
2701         struct extent_backref *back;
2702         struct list_head *cur;
2703         while (!list_empty(&rec->backrefs)) {
2704                 cur = rec->backrefs.next;
2705                 back = list_entry(cur, struct extent_backref, list);
2706                 list_del(cur);
2707                 free(back);
2708         }
2709         return 0;
2710 }
2711
2712 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2713                                      struct cache_tree *extent_cache)
2714 {
2715         struct cache_extent *cache;
2716         struct extent_record *rec;
2717
2718         while (1) {
2719                 cache = first_cache_extent(extent_cache);
2720                 if (!cache)
2721                         break;
2722                 rec = container_of(cache, struct extent_record, cache);
2723                 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2724                 remove_cache_extent(extent_cache, cache);
2725                 free_all_extent_backrefs(rec);
2726                 free(rec);
2727         }
2728 }
2729
2730 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2731                                  struct extent_record *rec)
2732 {
2733         if (rec->content_checked && rec->owner_ref_checked &&
2734             rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2735             rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2736                 remove_cache_extent(extent_cache, &rec->cache);
2737                 free_all_extent_backrefs(rec);
2738                 list_del_init(&rec->list);
2739                 free(rec);
2740         }
2741         return 0;
2742 }
2743
2744 static int check_owner_ref(struct btrfs_root *root,
2745                             struct extent_record *rec,
2746                             struct extent_buffer *buf)
2747 {
2748         struct extent_backref *node;
2749         struct tree_backref *back;
2750         struct btrfs_root *ref_root;
2751         struct btrfs_key key;
2752         struct btrfs_path path;
2753         struct extent_buffer *parent;
2754         int level;
2755         int found = 0;
2756         int ret;
2757
2758         list_for_each_entry(node, &rec->backrefs, list) {
2759                 if (node->is_data)
2760                         continue;
2761                 if (!node->found_ref)
2762                         continue;
2763                 if (node->full_backref)
2764                         continue;
2765                 back = (struct tree_backref *)node;
2766                 if (btrfs_header_owner(buf) == back->root)
2767                         return 0;
2768         }
2769         BUG_ON(rec->is_root);
2770
2771         /* try to find the block by search corresponding fs tree */
2772         key.objectid = btrfs_header_owner(buf);
2773         key.type = BTRFS_ROOT_ITEM_KEY;
2774         key.offset = (u64)-1;
2775
2776         ref_root = btrfs_read_fs_root(root->fs_info, &key);
2777         if (IS_ERR(ref_root))
2778                 return 1;
2779
2780         level = btrfs_header_level(buf);
2781         if (level == 0)
2782                 btrfs_item_key_to_cpu(buf, &key, 0);
2783         else
2784                 btrfs_node_key_to_cpu(buf, &key, 0);
2785
2786         btrfs_init_path(&path);
2787         path.lowest_level = level + 1;
2788         ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2789         if (ret < 0)
2790                 return 0;
2791
2792         parent = path.nodes[level + 1];
2793         if (parent && buf->start == btrfs_node_blockptr(parent,
2794                                                         path.slots[level + 1]))
2795                 found = 1;
2796
2797         btrfs_release_path(&path);
2798         return found ? 0 : 1;
2799 }
2800
2801 static int is_extent_tree_record(struct extent_record *rec)
2802 {
2803         struct list_head *cur = rec->backrefs.next;
2804         struct extent_backref *node;
2805         struct tree_backref *back;
2806         int is_extent = 0;
2807
2808         while(cur != &rec->backrefs) {
2809                 node = list_entry(cur, struct extent_backref, list);
2810                 cur = cur->next;
2811                 if (node->is_data)
2812                         return 0;
2813                 back = (struct tree_backref *)node;
2814                 if (node->full_backref)
2815                         return 0;
2816                 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2817                         is_extent = 1;
2818         }
2819         return is_extent;
2820 }
2821
2822
2823 static int record_bad_block_io(struct btrfs_fs_info *info,
2824                                struct cache_tree *extent_cache,
2825                                u64 start, u64 len)
2826 {
2827         struct extent_record *rec;
2828         struct cache_extent *cache;
2829         struct btrfs_key key;
2830
2831         cache = lookup_cache_extent(extent_cache, start, len);
2832         if (!cache)
2833                 return 0;
2834
2835         rec = container_of(cache, struct extent_record, cache);
2836         if (!is_extent_tree_record(rec))
2837                 return 0;
2838
2839         btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2840         return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2841 }
2842
2843 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2844                        struct extent_buffer *buf, int slot)
2845 {
2846         if (btrfs_header_level(buf)) {
2847                 struct btrfs_key_ptr ptr1, ptr2;
2848
2849                 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2850                                    sizeof(struct btrfs_key_ptr));
2851                 read_extent_buffer(buf, &ptr2,
2852                                    btrfs_node_key_ptr_offset(slot + 1),
2853                                    sizeof(struct btrfs_key_ptr));
2854                 write_extent_buffer(buf, &ptr1,
2855                                     btrfs_node_key_ptr_offset(slot + 1),
2856                                     sizeof(struct btrfs_key_ptr));
2857                 write_extent_buffer(buf, &ptr2,
2858                                     btrfs_node_key_ptr_offset(slot),
2859                                     sizeof(struct btrfs_key_ptr));
2860                 if (slot == 0) {
2861                         struct btrfs_disk_key key;
2862                         btrfs_node_key(buf, &key, 0);
2863                         btrfs_fixup_low_keys(root, path, &key,
2864                                              btrfs_header_level(buf) + 1);
2865                 }
2866         } else {
2867                 struct btrfs_item *item1, *item2;
2868                 struct btrfs_key k1, k2;
2869                 char *item1_data, *item2_data;
2870                 u32 item1_offset, item2_offset, item1_size, item2_size;
2871
2872                 item1 = btrfs_item_nr(slot);
2873                 item2 = btrfs_item_nr(slot + 1);
2874                 btrfs_item_key_to_cpu(buf, &k1, slot);
2875                 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2876                 item1_offset = btrfs_item_offset(buf, item1);
2877                 item2_offset = btrfs_item_offset(buf, item2);
2878                 item1_size = btrfs_item_size(buf, item1);
2879                 item2_size = btrfs_item_size(buf, item2);
2880
2881                 item1_data = malloc(item1_size);
2882                 if (!item1_data)
2883                         return -ENOMEM;
2884                 item2_data = malloc(item2_size);
2885                 if (!item2_data) {
2886                         free(item1_data);
2887                         return -ENOMEM;
2888                 }
2889
2890                 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2891                 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2892
2893                 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2894                 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2895                 free(item1_data);
2896                 free(item2_data);
2897
2898                 btrfs_set_item_offset(buf, item1, item2_offset);
2899                 btrfs_set_item_offset(buf, item2, item1_offset);
2900                 btrfs_set_item_size(buf, item1, item2_size);
2901                 btrfs_set_item_size(buf, item2, item1_size);
2902
2903                 path->slots[0] = slot;
2904                 btrfs_set_item_key_unsafe(root, path, &k2);
2905                 path->slots[0] = slot + 1;
2906                 btrfs_set_item_key_unsafe(root, path, &k1);
2907         }
2908         return 0;
2909 }
2910
2911 static int fix_key_order(struct btrfs_trans_handle *trans,
2912                          struct btrfs_root *root,
2913                          struct btrfs_path *path)
2914 {
2915         struct extent_buffer *buf;
2916         struct btrfs_key k1, k2;
2917         int i;
2918         int level = path->lowest_level;
2919         int ret;
2920
2921         buf = path->nodes[level];
2922         for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2923                 if (level) {
2924                         btrfs_node_key_to_cpu(buf, &k1, i);
2925                         btrfs_node_key_to_cpu(buf, &k2, i + 1);
2926                 } else {
2927                         btrfs_item_key_to_cpu(buf, &k1, i);
2928                         btrfs_item_key_to_cpu(buf, &k2, i + 1);
2929                 }
2930                 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2931                         continue;
2932                 ret = swap_values(root, path, buf, i);
2933                 if (ret)
2934                         break;
2935                 btrfs_mark_buffer_dirty(buf);
2936                 i = 0;
2937         }
2938         return ret;
2939 }
2940
2941 static int delete_bogus_item(struct btrfs_trans_handle *trans,
2942                              struct btrfs_root *root,
2943                              struct btrfs_path *path,
2944                              struct extent_buffer *buf, int slot)
2945 {
2946         struct btrfs_key key;
2947         int nritems = btrfs_header_nritems(buf);
2948
2949         btrfs_item_key_to_cpu(buf, &key, slot);
2950
2951         /* These are all the keys we can deal with missing. */
2952         if (key.type != BTRFS_DIR_INDEX_KEY &&
2953             key.type != BTRFS_EXTENT_ITEM_KEY &&
2954             key.type != BTRFS_METADATA_ITEM_KEY &&
2955             key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2956             key.type != BTRFS_EXTENT_DATA_REF_KEY)
2957                 return -1;
2958
2959         printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
2960                (unsigned long long)key.objectid, key.type,
2961                (unsigned long long)key.offset, slot, buf->start);
2962         memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
2963                               btrfs_item_nr_offset(slot + 1),
2964                               sizeof(struct btrfs_item) *
2965                               (nritems - slot - 1));
2966         btrfs_set_header_nritems(buf, nritems - 1);
2967         if (slot == 0) {
2968                 struct btrfs_disk_key disk_key;
2969
2970                 btrfs_item_key(buf, &disk_key, 0);
2971                 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2972         }
2973         btrfs_mark_buffer_dirty(buf);
2974         return 0;
2975 }
2976
2977 static int fix_item_offset(struct btrfs_trans_handle *trans,
2978                            struct btrfs_root *root,
2979                            struct btrfs_path *path)
2980 {
2981         struct extent_buffer *buf;
2982         int i;
2983         int ret = 0;
2984
2985         /* We should only get this for leaves */
2986         BUG_ON(path->lowest_level);
2987         buf = path->nodes[0];
2988 again:
2989         for (i = 0; i < btrfs_header_nritems(buf); i++) {
2990                 unsigned int shift = 0, offset;
2991
2992                 if (i == 0 && btrfs_item_end_nr(buf, i) !=
2993                     BTRFS_LEAF_DATA_SIZE(root)) {
2994                         if (btrfs_item_end_nr(buf, i) >
2995                             BTRFS_LEAF_DATA_SIZE(root)) {
2996                                 ret = delete_bogus_item(trans, root, path,
2997                                                         buf, i);
2998                                 if (!ret)
2999                                         goto again;
3000                                 fprintf(stderr, "item is off the end of the "
3001                                         "leaf, can't fix\n");
3002                                 ret = -EIO;
3003                                 break;
3004                         }
3005                         shift = BTRFS_LEAF_DATA_SIZE(root) -
3006                                 btrfs_item_end_nr(buf, i);
3007                 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
3008                            btrfs_item_offset_nr(buf, i - 1)) {
3009                         if (btrfs_item_end_nr(buf, i) >
3010                             btrfs_item_offset_nr(buf, i - 1)) {
3011                                 ret = delete_bogus_item(trans, root, path,
3012                                                         buf, i);
3013                                 if (!ret)
3014                                         goto again;
3015                                 fprintf(stderr, "items overlap, can't fix\n");
3016                                 ret = -EIO;
3017                                 break;
3018                         }
3019                         shift = btrfs_item_offset_nr(buf, i - 1) -
3020                                 btrfs_item_end_nr(buf, i);
3021                 }
3022                 if (!shift)
3023                         continue;
3024
3025                 printf("Shifting item nr %d by %u bytes in block %llu\n",
3026                        i, shift, (unsigned long long)buf->start);
3027                 offset = btrfs_item_offset_nr(buf, i);
3028                 memmove_extent_buffer(buf,
3029                                       btrfs_leaf_data(buf) + offset + shift,
3030                                       btrfs_leaf_data(buf) + offset,
3031                                       btrfs_item_size_nr(buf, i));
3032                 btrfs_set_item_offset(buf, btrfs_item_nr(i),
3033                                       offset + shift);
3034                 btrfs_mark_buffer_dirty(buf);
3035         }
3036
3037         /*
3038          * We may have moved things, in which case we want to exit so we don't
3039          * write those changes out.  Once we have proper abort functionality in
3040          * progs this can be changed to something nicer.
3041          */
3042         BUG_ON(ret);
3043         return ret;
3044 }
3045
3046 /*
3047  * Attempt to fix basic block failures.  If we can't fix it for whatever reason
3048  * then just return -EIO.
3049  */
3050 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
3051                                 struct btrfs_root *root,
3052                                 struct extent_buffer *buf,
3053                                 enum btrfs_tree_block_status status)
3054 {
3055         struct ulist *roots;
3056         struct ulist_node *node;
3057         struct btrfs_root *search_root;
3058         struct btrfs_path *path;
3059         struct ulist_iterator iter;
3060         struct btrfs_key root_key, key;
3061         int ret;
3062
3063         if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
3064             status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3065                 return -EIO;
3066
3067         path = btrfs_alloc_path();
3068         if (!path)
3069                 return -EIO;
3070
3071         ret = btrfs_find_all_roots(trans, root->fs_info, buf->start,
3072                                    0, &roots);
3073         if (ret) {
3074                 btrfs_free_path(path);
3075                 return -EIO;
3076         }
3077
3078         ULIST_ITER_INIT(&iter);
3079         while ((node = ulist_next(roots, &iter))) {
3080                 root_key.objectid = node->val;
3081                 root_key.type = BTRFS_ROOT_ITEM_KEY;
3082                 root_key.offset = (u64)-1;
3083
3084                 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
3085                 if (IS_ERR(root)) {
3086                         ret = -EIO;
3087                         break;
3088                 }
3089
3090                 record_root_in_trans(trans, search_root);
3091
3092                 path->lowest_level = btrfs_header_level(buf);
3093                 path->skip_check_block = 1;
3094                 if (path->lowest_level)
3095                         btrfs_node_key_to_cpu(buf, &key, 0);
3096                 else
3097                         btrfs_item_key_to_cpu(buf, &key, 0);
3098                 ret = btrfs_search_slot(trans, search_root, &key, path, 0, 1);
3099                 if (ret) {
3100                         ret = -EIO;
3101                         break;
3102                 }
3103                 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
3104                         ret = fix_key_order(trans, search_root, path);
3105                 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3106                         ret = fix_item_offset(trans, search_root, path);
3107                 if (ret)
3108                         break;
3109                 btrfs_release_path(path);
3110         }
3111         ulist_free(roots);
3112         btrfs_free_path(path);
3113         return ret;
3114 }
3115
3116 static int check_block(struct btrfs_trans_handle *trans,
3117                        struct btrfs_root *root,
3118                        struct cache_tree *extent_cache,
3119                        struct extent_buffer *buf, u64 flags)
3120 {
3121         struct extent_record *rec;
3122         struct cache_extent *cache;
3123         struct btrfs_key key;
3124         enum btrfs_tree_block_status status;
3125         int ret = 0;
3126         int level;
3127
3128         cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
3129         if (!cache)
3130                 return 1;
3131         rec = container_of(cache, struct extent_record, cache);
3132         rec->generation = btrfs_header_generation(buf);
3133
3134         level = btrfs_header_level(buf);
3135         if (btrfs_header_nritems(buf) > 0) {
3136
3137                 if (level == 0)
3138                         btrfs_item_key_to_cpu(buf, &key, 0);
3139                 else
3140                         btrfs_node_key_to_cpu(buf, &key, 0);
3141
3142                 rec->info_objectid = key.objectid;
3143         }
3144         rec->info_level = level;
3145
3146         if (btrfs_is_leaf(buf))
3147                 status = btrfs_check_leaf(root, &rec->parent_key, buf);
3148         else
3149                 status = btrfs_check_node(root, &rec->parent_key, buf);
3150
3151         if (status != BTRFS_TREE_BLOCK_CLEAN) {
3152                 if (repair)
3153                         status = try_to_fix_bad_block(trans, root, buf,
3154                                                       status);
3155                 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3156                         ret = -EIO;
3157                         fprintf(stderr, "bad block %llu\n",
3158                                 (unsigned long long)buf->start);
3159                 } else {
3160                         /*
3161                          * Signal to callers we need to start the scan over
3162                          * again since we'll have cow'ed blocks.
3163                          */
3164                         ret = -EAGAIN;
3165                 }
3166         } else {
3167                 rec->content_checked = 1;
3168                 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3169                         rec->owner_ref_checked = 1;
3170                 else {
3171                         ret = check_owner_ref(root, rec, buf);
3172                         if (!ret)
3173                                 rec->owner_ref_checked = 1;
3174                 }
3175         }
3176         if (!ret)
3177                 maybe_free_extent_rec(extent_cache, rec);
3178         return ret;
3179 }
3180
3181 static struct tree_backref *find_tree_backref(struct extent_record *rec,
3182                                                 u64 parent, u64 root)
3183 {
3184         struct list_head *cur = rec->backrefs.next;
3185         struct extent_backref *node;
3186         struct tree_backref *back;
3187
3188         while(cur != &rec->backrefs) {
3189                 node = list_entry(cur, struct extent_backref, list);
3190                 cur = cur->next;
3191                 if (node->is_data)
3192                         continue;
3193                 back = (struct tree_backref *)node;
3194                 if (parent > 0) {
3195                         if (!node->full_backref)
3196                                 continue;
3197                         if (parent == back->parent)
3198                                 return back;
3199                 } else {
3200                         if (node->full_backref)
3201                                 continue;
3202                         if (back->root == root)
3203                                 return back;
3204                 }
3205         }
3206         return NULL;
3207 }
3208
3209 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
3210                                                 u64 parent, u64 root)
3211 {
3212         struct tree_backref *ref = malloc(sizeof(*ref));
3213         memset(&ref->node, 0, sizeof(ref->node));
3214         if (parent > 0) {
3215                 ref->parent = parent;
3216                 ref->node.full_backref = 1;
3217         } else {
3218                 ref->root = root;
3219                 ref->node.full_backref = 0;
3220         }
3221         list_add_tail(&ref->node.list, &rec->backrefs);
3222
3223         return ref;
3224 }
3225
3226 static struct data_backref *find_data_backref(struct extent_record *rec,
3227                                                 u64 parent, u64 root,
3228                                                 u64 owner, u64 offset,
3229                                                 int found_ref,
3230                                                 u64 disk_bytenr, u64 bytes)
3231 {
3232         struct list_head *cur = rec->backrefs.next;
3233         struct extent_backref *node;
3234         struct data_backref *back;
3235
3236         while(cur != &rec->backrefs) {
3237                 node = list_entry(cur, struct extent_backref, list);
3238                 cur = cur->next;
3239                 if (!node->is_data)
3240                         continue;
3241                 back = (struct data_backref *)node;
3242                 if (parent > 0) {
3243                         if (!node->full_backref)
3244                                 continue;
3245                         if (parent == back->parent)
3246                                 return back;
3247                 } else {
3248                         if (node->full_backref)
3249                                 continue;
3250                         if (back->root == root && back->owner == owner &&
3251                             back->offset == offset) {
3252                                 if (found_ref && node->found_ref &&
3253                                     (back->bytes != bytes ||
3254                                     back->disk_bytenr != disk_bytenr))
3255                                         continue;
3256                                 return back;
3257                         }
3258                 }
3259         }
3260         return NULL;
3261 }
3262
3263 static struct data_backref *alloc_data_backref(struct extent_record *rec,
3264                                                 u64 parent, u64 root,
3265                                                 u64 owner, u64 offset,
3266                                                 u64 max_size)
3267 {
3268         struct data_backref *ref = malloc(sizeof(*ref));
3269         memset(&ref->node, 0, sizeof(ref->node));
3270         ref->node.is_data = 1;
3271
3272         if (parent > 0) {
3273                 ref->parent = parent;
3274                 ref->owner = 0;
3275                 ref->offset = 0;
3276                 ref->node.full_backref = 1;
3277         } else {
3278                 ref->root = root;
3279                 ref->owner = owner;
3280                 ref->offset = offset;
3281                 ref->node.full_backref = 0;
3282         }
3283         ref->bytes = max_size;
3284         ref->found_ref = 0;
3285         ref->num_refs = 0;
3286         list_add_tail(&ref->node.list, &rec->backrefs);
3287         if (max_size > rec->max_size)
3288                 rec->max_size = max_size;
3289         return ref;
3290 }
3291
3292 static int add_extent_rec(struct cache_tree *extent_cache,
3293                           struct btrfs_key *parent_key, u64 parent_gen,
3294                           u64 start, u64 nr, u64 extent_item_refs,
3295                           int is_root, int inc_ref, int set_checked,
3296                           int metadata, int extent_rec, u64 max_size)
3297 {
3298         struct extent_record *rec;
3299         struct cache_extent *cache;
3300         int ret = 0;
3301         int dup = 0;
3302
3303         cache = lookup_cache_extent(extent_cache, start, nr);
3304         if (cache) {
3305                 rec = container_of(cache, struct extent_record, cache);
3306                 if (inc_ref)
3307                         rec->refs++;
3308                 if (rec->nr == 1)
3309                         rec->nr = max(nr, max_size);
3310
3311                 /*
3312                  * We need to make sure to reset nr to whatever the extent
3313                  * record says was the real size, this way we can compare it to
3314                  * the backrefs.
3315                  */
3316                 if (extent_rec) {
3317                         if (start != rec->start || rec->found_rec) {
3318                                 struct extent_record *tmp;
3319
3320                                 dup = 1;
3321                                 if (list_empty(&rec->list))
3322                                         list_add_tail(&rec->list,
3323                                                       &duplicate_extents);
3324
3325                                 /*
3326                                  * We have to do this song and dance in case we
3327                                  * find an extent record that falls inside of
3328                                  * our current extent record but does not have
3329                                  * the same objectid.
3330                                  */
3331                                 tmp = malloc(sizeof(*tmp));
3332                                 if (!tmp)
3333                                         return -ENOMEM;
3334                                 tmp->start = start;
3335                                 tmp->max_size = max_size;
3336                                 tmp->nr = nr;
3337                                 tmp->found_rec = 1;
3338                                 tmp->metadata = metadata;
3339                                 tmp->extent_item_refs = extent_item_refs;
3340                                 INIT_LIST_HEAD(&tmp->list);
3341                                 list_add_tail(&tmp->list, &rec->dups);
3342                                 rec->num_duplicates++;
3343                         } else {
3344                                 rec->nr = nr;
3345                                 rec->found_rec = 1;
3346                         }
3347                 }
3348
3349                 if (extent_item_refs && !dup) {
3350                         if (rec->extent_item_refs) {
3351                                 fprintf(stderr, "block %llu rec "
3352                                         "extent_item_refs %llu, passed %llu\n",
3353                                         (unsigned long long)start,
3354                                         (unsigned long long)
3355                                                         rec->extent_item_refs,
3356                                         (unsigned long long)extent_item_refs);
3357                         }
3358                         rec->extent_item_refs = extent_item_refs;
3359                 }
3360                 if (is_root)
3361                         rec->is_root = 1;
3362                 if (set_checked) {
3363                         rec->content_checked = 1;
3364                         rec->owner_ref_checked = 1;
3365                 }
3366
3367                 if (parent_key)
3368                         btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3369                 if (parent_gen)
3370                         rec->parent_generation = parent_gen;
3371
3372                 if (rec->max_size < max_size)
3373                         rec->max_size = max_size;
3374
3375                 maybe_free_extent_rec(extent_cache, rec);
3376                 return ret;
3377         }
3378         rec = malloc(sizeof(*rec));
3379         rec->start = start;
3380         rec->max_size = max_size;
3381         rec->nr = max(nr, max_size);
3382         rec->found_rec = !!extent_rec;
3383         rec->content_checked = 0;
3384         rec->owner_ref_checked = 0;
3385         rec->num_duplicates = 0;
3386         rec->metadata = metadata;
3387         INIT_LIST_HEAD(&rec->backrefs);
3388         INIT_LIST_HEAD(&rec->dups);
3389         INIT_LIST_HEAD(&rec->list);
3390
3391         if (is_root)
3392                 rec->is_root = 1;
3393         else
3394                 rec->is_root = 0;
3395
3396         if (inc_ref)
3397                 rec->refs = 1;
3398         else
3399                 rec->refs = 0;
3400
3401         if (extent_item_refs)
3402                 rec->extent_item_refs = extent_item_refs;
3403         else
3404                 rec->extent_item_refs = 0;
3405
3406         if (parent_key)
3407                 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3408         else
3409                 memset(&rec->parent_key, 0, sizeof(*parent_key));
3410
3411         if (parent_gen)
3412                 rec->parent_generation = parent_gen;
3413         else
3414                 rec->parent_generation = 0;
3415
3416         rec->cache.start = start;
3417         rec->cache.size = nr;
3418         ret = insert_cache_extent(extent_cache, &rec->cache);
3419         BUG_ON(ret);
3420         bytes_used += nr;
3421         if (set_checked) {
3422                 rec->content_checked = 1;
3423                 rec->owner_ref_checked = 1;
3424         }
3425         return ret;
3426 }
3427
3428 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
3429                             u64 parent, u64 root, int found_ref)
3430 {
3431         struct extent_record *rec;
3432         struct tree_backref *back;
3433         struct cache_extent *cache;
3434
3435         cache = lookup_cache_extent(extent_cache, bytenr, 1);
3436         if (!cache) {
3437                 add_extent_rec(extent_cache, NULL, 0, bytenr,
3438                                1, 0, 0, 0, 0, 1, 0, 0);
3439                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3440                 if (!cache)
3441                         abort();
3442         }
3443
3444         rec = container_of(cache, struct extent_record, cache);
3445         if (rec->start != bytenr) {
3446                 abort();
3447         }
3448
3449         back = find_tree_backref(rec, parent, root);
3450         if (!back)
3451                 back = alloc_tree_backref(rec, parent, root);
3452
3453         if (found_ref) {
3454                 if (back->node.found_ref) {
3455                         fprintf(stderr, "Extent back ref already exists "
3456                                 "for %llu parent %llu root %llu \n",
3457                                 (unsigned long long)bytenr,
3458                                 (unsigned long long)parent,
3459                                 (unsigned long long)root);
3460                 }
3461                 back->node.found_ref = 1;
3462         } else {
3463                 if (back->node.found_extent_tree) {
3464                         fprintf(stderr, "Extent back ref already exists "
3465                                 "for %llu parent %llu root %llu \n",
3466                                 (unsigned long long)bytenr,
3467                                 (unsigned long long)parent,
3468                                 (unsigned long long)root);
3469                 }
3470                 back->node.found_extent_tree = 1;
3471         }
3472         maybe_free_extent_rec(extent_cache, rec);
3473         return 0;
3474 }
3475
3476 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
3477                             u64 parent, u64 root, u64 owner, u64 offset,
3478                             u32 num_refs, int found_ref, u64 max_size)
3479 {
3480         struct extent_record *rec;
3481         struct data_backref *back;
3482         struct cache_extent *cache;
3483
3484         cache = lookup_cache_extent(extent_cache, bytenr, 1);
3485         if (!cache) {
3486                 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
3487                                0, 0, max_size);
3488                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3489                 if (!cache)
3490                         abort();
3491         }
3492
3493         rec = container_of(cache, struct extent_record, cache);
3494         if (rec->max_size < max_size)
3495                 rec->max_size = max_size;
3496
3497         /*
3498          * If found_ref is set then max_size is the real size and must match the
3499          * existing refs.  So if we have already found a ref then we need to
3500          * make sure that this ref matches the existing one, otherwise we need
3501          * to add a new backref so we can notice that the backrefs don't match
3502          * and we need to figure out who is telling the truth.  This is to
3503          * account for that awful fsync bug I introduced where we'd end up with
3504          * a btrfs_file_extent_item that would have its length include multiple
3505          * prealloc extents or point inside of a prealloc extent.
3506          */
3507         back = find_data_backref(rec, parent, root, owner, offset, found_ref,
3508                                  bytenr, max_size);
3509         if (!back)
3510                 back = alloc_data_backref(rec, parent, root, owner, offset,
3511                                           max_size);
3512
3513         if (found_ref) {
3514                 BUG_ON(num_refs != 1);
3515                 if (back->node.found_ref)
3516                         BUG_ON(back->bytes != max_size);
3517                 back->node.found_ref = 1;
3518                 back->found_ref += 1;
3519                 back->bytes = max_size;
3520                 back->disk_bytenr = bytenr;
3521                 rec->refs += 1;
3522                 rec->content_checked = 1;
3523                 rec->owner_ref_checked = 1;
3524         } else {
3525                 if (back->node.found_extent_tree) {
3526                         fprintf(stderr, "Extent back ref already exists "
3527                                 "for %llu parent %llu root %llu "
3528                                 "owner %llu offset %llu num_refs %lu\n",
3529                                 (unsigned long long)bytenr,
3530                                 (unsigned long long)parent,
3531                                 (unsigned long long)root,
3532                                 (unsigned long long)owner,
3533                                 (unsigned long long)offset,
3534                                 (unsigned long)num_refs);
3535                 }
3536                 back->num_refs = num_refs;
3537                 back->node.found_extent_tree = 1;
3538         }
3539         maybe_free_extent_rec(extent_cache, rec);
3540         return 0;
3541 }
3542
3543 static int add_pending(struct cache_tree *pending,
3544                        struct cache_tree *seen, u64 bytenr, u32 size)
3545 {
3546         int ret;
3547         ret = add_cache_extent(seen, bytenr, size);
3548         if (ret)
3549                 return ret;
3550         add_cache_extent(pending, bytenr, size);
3551         return 0;
3552 }
3553
3554 static int pick_next_pending(struct cache_tree *pending,
3555                         struct cache_tree *reada,
3556                         struct cache_tree *nodes,
3557                         u64 last, struct block_info *bits, int bits_nr,
3558                         int *reada_bits)
3559 {
3560         unsigned long node_start = last;
3561         struct cache_extent *cache;
3562         int ret;
3563
3564         cache = search_cache_extent(reada, 0);
3565         if (cache) {
3566                 bits[0].start = cache->start;
3567                 bits[0].size = cache->size;
3568                 *reada_bits = 1;
3569                 return 1;
3570         }
3571         *reada_bits = 0;
3572         if (node_start > 32768)
3573                 node_start -= 32768;
3574
3575         cache = search_cache_extent(nodes, node_start);
3576         if (!cache)
3577                 cache = search_cache_extent(nodes, 0);
3578
3579         if (!cache) {
3580                  cache = search_cache_extent(pending, 0);
3581                  if (!cache)
3582                          return 0;
3583                  ret = 0;
3584                  do {
3585                          bits[ret].start = cache->start;
3586                          bits[ret].size = cache->size;
3587                          cache = next_cache_extent(cache);
3588                          ret++;
3589                  } while (cache && ret < bits_nr);
3590                  return ret;
3591         }
3592
3593         ret = 0;
3594         do {
3595                 bits[ret].start = cache->start;
3596                 bits[ret].size = cache->size;
3597                 cache = next_cache_extent(cache);
3598                 ret++;
3599         } while (cache && ret < bits_nr);
3600
3601         if (bits_nr - ret > 8) {
3602                 u64 lookup = bits[0].start + bits[0].size;
3603                 struct cache_extent *next;
3604                 next = search_cache_extent(pending, lookup);
3605                 while(next) {
3606                         if (next->start - lookup > 32768)
3607                                 break;
3608                         bits[ret].start = next->start;
3609                         bits[ret].size = next->size;
3610                         lookup = next->start + next->size;
3611                         ret++;
3612                         if (ret == bits_nr)
3613                                 break;
3614                         next = next_cache_extent(next);
3615                         if (!next)
3616                                 break;
3617                 }
3618         }
3619         return ret;
3620 }
3621
3622 static void free_chunk_record(struct cache_extent *cache)
3623 {
3624         struct chunk_record *rec;
3625
3626         rec = container_of(cache, struct chunk_record, cache);
3627         list_del_init(&rec->list);
3628         list_del_init(&rec->dextents);
3629         free(rec);
3630 }
3631
3632 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
3633 {
3634         cache_tree_free_extents(chunk_cache, free_chunk_record);
3635 }
3636
3637 static void free_device_record(struct rb_node *node)
3638 {
3639         struct device_record *rec;
3640
3641         rec = container_of(node, struct device_record, node);
3642         free(rec);
3643 }
3644
3645 FREE_RB_BASED_TREE(device_cache, free_device_record);
3646
3647 int insert_block_group_record(struct block_group_tree *tree,
3648                               struct block_group_record *bg_rec)
3649 {
3650         int ret;
3651
3652         ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3653         if (ret)
3654                 return ret;
3655
3656         list_add_tail(&bg_rec->list, &tree->block_groups);
3657         return 0;
3658 }
3659
3660 static void free_block_group_record(struct cache_extent *cache)
3661 {
3662         struct block_group_record *rec;
3663
3664         rec = container_of(cache, struct block_group_record, cache);
3665         list_del_init(&rec->list);
3666         free(rec);
3667 }
3668
3669 void free_block_group_tree(struct block_group_tree *tree)
3670 {
3671         cache_tree_free_extents(&tree->tree, free_block_group_record);
3672 }
3673
3674 int insert_device_extent_record(struct device_extent_tree *tree,
3675                                 struct device_extent_record *de_rec)
3676 {
3677         int ret;
3678
3679         /*
3680          * Device extent is a bit different from the other extents, because
3681          * the extents which belong to the different devices may have the
3682          * same start and size, so we need use the special extent cache
3683          * search/insert functions.
3684          */
3685         ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3686         if (ret)
3687                 return ret;
3688
3689         list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3690         list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3691         return 0;
3692 }
3693
3694 static void free_device_extent_record(struct cache_extent *cache)
3695 {
3696         struct device_extent_record *rec;
3697
3698         rec = container_of(cache, struct device_extent_record, cache);
3699         if (!list_empty(&rec->chunk_list))
3700                 list_del_init(&rec->chunk_list);
3701         if (!list_empty(&rec->device_list))
3702                 list_del_init(&rec->device_list);
3703         free(rec);
3704 }
3705
3706 void free_device_extent_tree(struct device_extent_tree *tree)
3707 {
3708         cache_tree_free_extents(&tree->tree, free_device_extent_record);
3709 }
3710
3711 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3712 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3713                                  struct extent_buffer *leaf, int slot)
3714 {
3715         struct btrfs_extent_ref_v0 *ref0;
3716         struct btrfs_key key;
3717
3718         btrfs_item_key_to_cpu(leaf, &key, slot);
3719         ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3720         if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3721                 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3722         } else {
3723                 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3724                                  0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3725         }
3726         return 0;
3727 }
3728 #endif
3729
3730 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3731                                             struct btrfs_key *key,
3732                                             int slot)
3733 {
3734         struct btrfs_chunk *ptr;
3735         struct chunk_record *rec;
3736         int num_stripes, i;
3737
3738         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3739         num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3740
3741         rec = malloc(btrfs_chunk_record_size(num_stripes));
3742         if (!rec) {
3743                 fprintf(stderr, "memory allocation failed\n");
3744                 exit(-1);
3745         }
3746
3747         memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3748
3749         INIT_LIST_HEAD(&rec->list);
3750         INIT_LIST_HEAD(&rec->dextents);
3751         rec->bg_rec = NULL;
3752
3753         rec->cache.start = key->offset;
3754         rec->cache.size = btrfs_chunk_length(leaf, ptr);
3755
3756         rec->generation = btrfs_header_generation(leaf);
3757
3758         rec->objectid = key->objectid;
3759         rec->type = key->type;
3760         rec->offset = key->offset;
3761
3762         rec->length = rec->cache.size;
3763         rec->owner = btrfs_chunk_owner(leaf, ptr);
3764         rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3765         rec->type_flags = btrfs_chunk_type(leaf, ptr);
3766         rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3767         rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3768         rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3769         rec->num_stripes = num_stripes;
3770         rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3771
3772         for (i = 0; i < rec->num_stripes; ++i) {
3773                 rec->stripes[i].devid =
3774                         btrfs_stripe_devid_nr(leaf, ptr, i);
3775                 rec->stripes[i].offset =
3776                         btrfs_stripe_offset_nr(leaf, ptr, i);
3777                 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3778                                 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3779                                 BTRFS_UUID_SIZE);
3780         }
3781
3782         return rec;
3783 }
3784
3785 static int process_chunk_item(struct cache_tree *chunk_cache,
3786                               struct btrfs_key *key, struct extent_buffer *eb,
3787                               int slot)
3788 {
3789         struct chunk_record *rec;
3790         int ret = 0;
3791
3792         rec = btrfs_new_chunk_record(eb, key, slot);
3793         ret = insert_cache_extent(chunk_cache, &rec->cache);
3794         if (ret) {
3795                 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3796                         rec->offset, rec->length);
3797                 free(rec);
3798         }
3799
3800         return ret;
3801 }
3802
3803 static int process_device_item(struct rb_root *dev_cache,
3804                 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3805 {
3806         struct btrfs_dev_item *ptr;
3807         struct device_record *rec;
3808         int ret = 0;
3809
3810         ptr = btrfs_item_ptr(eb,
3811                 slot, struct btrfs_dev_item);
3812
3813         rec = malloc(sizeof(*rec));
3814         if (!rec) {
3815                 fprintf(stderr, "memory allocation failed\n");
3816                 return -ENOMEM;
3817         }
3818
3819         rec->devid = key->offset;
3820         rec->generation = btrfs_header_generation(eb);
3821
3822         rec->objectid = key->objectid;
3823         rec->type = key->type;
3824         rec->offset = key->offset;
3825
3826         rec->devid = btrfs_device_id(eb, ptr);
3827         rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3828         rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3829
3830         ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3831         if (ret) {
3832                 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3833                 free(rec);
3834         }
3835
3836         return ret;
3837 }
3838
3839 struct block_group_record *
3840 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3841                              int slot)
3842 {
3843         struct btrfs_block_group_item *ptr;
3844         struct block_group_record *rec;
3845
3846         rec = malloc(sizeof(*rec));
3847         if (!rec) {
3848                 fprintf(stderr, "memory allocation failed\n");
3849                 exit(-1);
3850         }
3851         memset(rec, 0, sizeof(*rec));
3852
3853         rec->cache.start = key->objectid;
3854         rec->cache.size = key->offset;
3855
3856         rec->generation = btrfs_header_generation(leaf);
3857
3858         rec->objectid = key->objectid;
3859         rec->type = key->type;
3860         rec->offset = key->offset;
3861
3862         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3863         rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3864
3865         INIT_LIST_HEAD(&rec->list);
3866
3867         return rec;
3868 }
3869
3870 static int process_block_group_item(struct block_group_tree *block_group_cache,
3871                                     struct btrfs_key *key,
3872                                     struct extent_buffer *eb, int slot)
3873 {
3874         struct block_group_record *rec;
3875         int ret = 0;
3876
3877         rec = btrfs_new_block_group_record(eb, key, slot);
3878         ret = insert_block_group_record(block_group_cache, rec);
3879         if (ret) {
3880                 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3881                         rec->objectid, rec->offset);
3882                 free(rec);
3883         }
3884
3885         return ret;
3886 }
3887
3888 struct device_extent_record *
3889 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3890                                struct btrfs_key *key, int slot)
3891 {
3892         struct device_extent_record *rec;
3893         struct btrfs_dev_extent *ptr;
3894
3895         rec = malloc(sizeof(*rec));
3896         if (!rec) {
3897                 fprintf(stderr, "memory allocation failed\n");
3898                 exit(-1);
3899         }
3900         memset(rec, 0, sizeof(*rec));
3901
3902         rec->cache.objectid = key->objectid;
3903         rec->cache.start = key->offset;
3904
3905         rec->generation = btrfs_header_generation(leaf);
3906
3907         rec->objectid = key->objectid;
3908         rec->type = key->type;
3909         rec->offset = key->offset;
3910
3911         ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3912         rec->chunk_objecteid =
3913                 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3914         rec->chunk_offset =
3915                 btrfs_dev_extent_chunk_offset(leaf, ptr);
3916         rec->length = btrfs_dev_extent_length(leaf, ptr);
3917         rec->cache.size = rec->length;
3918
3919         INIT_LIST_HEAD(&rec->chunk_list);
3920         INIT_LIST_HEAD(&rec->device_list);
3921
3922         return rec;
3923 }
3924
3925 static int
3926 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3927                            struct btrfs_key *key, struct extent_buffer *eb,
3928                            int slot)
3929 {
3930         struct device_extent_record *rec;
3931         int ret;
3932
3933         rec = btrfs_new_device_extent_record(eb, key, slot);
3934         ret = insert_device_extent_record(dev_extent_cache, rec);
3935         if (ret) {
3936                 fprintf(stderr,
3937                         "Device extent[%llu, %llu, %llu] existed.\n",
3938                         rec->objectid, rec->offset, rec->length);
3939                 free(rec);
3940         }
3941
3942         return ret;
3943 }
3944
3945 static int process_extent_item(struct btrfs_root *root,
3946                                struct cache_tree *extent_cache,
3947                                struct extent_buffer *eb, int slot)
3948 {
3949         struct btrfs_extent_item *ei;
3950         struct btrfs_extent_inline_ref *iref;
3951         struct btrfs_extent_data_ref *dref;
3952         struct btrfs_shared_data_ref *sref;
3953         struct btrfs_key key;
3954         unsigned long end;
3955         unsigned long ptr;
3956         int type;
3957         u32 item_size = btrfs_item_size_nr(eb, slot);
3958         u64 refs = 0;
3959         u64 offset;
3960         u64 num_bytes;
3961         int metadata = 0;
3962
3963         btrfs_item_key_to_cpu(eb, &key, slot);
3964
3965         if (key.type == BTRFS_METADATA_ITEM_KEY) {
3966                 metadata = 1;
3967                 num_bytes = root->leafsize;
3968         } else {
3969                 num_bytes = key.offset;
3970         }
3971
3972         if (item_size < sizeof(*ei)) {
3973 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3974                 struct btrfs_extent_item_v0 *ei0;
3975                 BUG_ON(item_size != sizeof(*ei0));
3976                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3977                 refs = btrfs_extent_refs_v0(eb, ei0);
3978 #else
3979                 BUG();
3980 #endif
3981                 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3982                                       num_bytes, refs, 0, 0, 0, metadata, 1,
3983                                       num_bytes);
3984         }
3985
3986         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3987         refs = btrfs_extent_refs(eb, ei);
3988
3989         add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3990                        refs, 0, 0, 0, metadata, 1, num_bytes);
3991
3992         ptr = (unsigned long)(ei + 1);
3993         if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3994             key.type == BTRFS_EXTENT_ITEM_KEY)
3995                 ptr += sizeof(struct btrfs_tree_block_info);
3996
3997         end = (unsigned long)ei + item_size;
3998         while (ptr < end) {
3999                 iref = (struct btrfs_extent_inline_ref *)ptr;
4000                 type = btrfs_extent_inline_ref_type(eb, iref);
4001                 offset = btrfs_extent_inline_ref_offset(eb, iref);
4002                 switch (type) {
4003                 case BTRFS_TREE_BLOCK_REF_KEY:
4004                         add_tree_backref(extent_cache, key.objectid,
4005                                          0, offset, 0);
4006                         break;
4007                 case BTRFS_SHARED_BLOCK_REF_KEY:
4008                         add_tree_backref(extent_cache, key.objectid,
4009                                          offset, 0, 0);
4010                         break;
4011                 case BTRFS_EXTENT_DATA_REF_KEY:
4012                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
4013                         add_data_backref(extent_cache, key.objectid, 0,
4014                                         btrfs_extent_data_ref_root(eb, dref),
4015                                         btrfs_extent_data_ref_objectid(eb,
4016                                                                        dref),
4017                                         btrfs_extent_data_ref_offset(eb, dref),
4018                                         btrfs_extent_data_ref_count(eb, dref),
4019                                         0, num_bytes);
4020                         break;
4021                 case BTRFS_SHARED_DATA_REF_KEY:
4022                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
4023                         add_data_backref(extent_cache, key.objectid, offset,
4024                                         0, 0, 0,
4025                                         btrfs_shared_data_ref_count(eb, sref),
4026                                         0, num_bytes);
4027                         break;
4028                 default:
4029                         fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
4030                                 key.objectid, key.type, num_bytes);
4031                         goto out;
4032                 }
4033                 ptr += btrfs_extent_inline_ref_size(type);
4034         }
4035         WARN_ON(ptr > end);
4036 out:
4037         return 0;
4038 }
4039
4040 static int check_cache_range(struct btrfs_root *root,
4041                              struct btrfs_block_group_cache *cache,
4042                              u64 offset, u64 bytes)
4043 {
4044         struct btrfs_free_space *entry;
4045         u64 *logical;
4046         u64 bytenr;
4047         int stripe_len;
4048         int i, nr, ret;
4049
4050         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4051                 bytenr = btrfs_sb_offset(i);
4052                 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
4053                                        cache->key.objectid, bytenr, 0,
4054                                        &logical, &nr, &stripe_len);
4055                 if (ret)
4056                         return ret;
4057
4058                 while (nr--) {
4059                         if (logical[nr] + stripe_len <= offset)
4060                                 continue;
4061                         if (offset + bytes <= logical[nr])
4062                                 continue;
4063                         if (logical[nr] == offset) {
4064                                 if (stripe_len >= bytes) {
4065                                         kfree(logical);
4066                                         return 0;
4067                                 }
4068                                 bytes -= stripe_len;
4069                                 offset += stripe_len;
4070                         } else if (logical[nr] < offset) {
4071                                 if (logical[nr] + stripe_len >=
4072                                     offset + bytes) {
4073                                         kfree(logical);
4074                                         return 0;
4075                                 }
4076                                 bytes = (offset + bytes) -
4077                                         (logical[nr] + stripe_len);
4078                                 offset = logical[nr] + stripe_len;
4079                         } else {
4080                                 /*
4081                                  * Could be tricky, the super may land in the
4082                                  * middle of the area we're checking.  First
4083                                  * check the easiest case, it's at the end.
4084                                  */
4085                                 if (logical[nr] + stripe_len >=
4086                                     bytes + offset) {
4087                                         bytes = logical[nr] - offset;
4088                                         continue;
4089                                 }
4090
4091                                 /* Check the left side */
4092                                 ret = check_cache_range(root, cache,
4093                                                         offset,
4094                                                         logical[nr] - offset);
4095                                 if (ret) {
4096                                         kfree(logical);
4097                                         return ret;
4098                                 }
4099
4100                                 /* Now we continue with the right side */
4101                                 bytes = (offset + bytes) -
4102                                         (logical[nr] + stripe_len);
4103                                 offset = logical[nr] + stripe_len;
4104                         }
4105                 }
4106
4107                 kfree(logical);
4108         }
4109
4110         entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
4111         if (!entry) {
4112                 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
4113                         offset, offset+bytes);
4114                 return -EINVAL;
4115         }
4116
4117         if (entry->offset != offset) {
4118                 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
4119                         entry->offset);
4120                 return -EINVAL;
4121         }
4122
4123         if (entry->bytes != bytes) {
4124                 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
4125                         bytes, entry->bytes, offset);
4126                 return -EINVAL;
4127         }
4128
4129         unlink_free_space(cache->free_space_ctl, entry);
4130         free(entry);
4131         return 0;
4132 }
4133
4134 static int verify_space_cache(struct btrfs_root *root,
4135                               struct btrfs_block_group_cache *cache)
4136 {
4137         struct btrfs_path *path;
4138         struct extent_buffer *leaf;
4139         struct btrfs_key key;
4140         u64 last;
4141         int ret = 0;
4142
4143         path = btrfs_alloc_path();
4144         if (!path)
4145                 return -ENOMEM;
4146
4147         root = root->fs_info->extent_root;
4148
4149         last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
4150
4151         key.objectid = last;
4152         key.offset = 0;
4153         key.type = BTRFS_EXTENT_ITEM_KEY;
4154
4155         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4156         if (ret < 0)
4157                 goto out;
4158         ret = 0;
4159         while (1) {
4160                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4161                         ret = btrfs_next_leaf(root, path);
4162                         if (ret < 0)
4163                                 goto out;
4164                         if (ret > 0) {
4165                                 ret = 0;
4166                                 break;
4167                         }
4168                 }
4169                 leaf = path->nodes[0];
4170                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4171                 if (key.objectid >= cache->key.offset + cache->key.objectid)
4172                         break;
4173                 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
4174                     key.type != BTRFS_METADATA_ITEM_KEY) {
4175                         path->slots[0]++;
4176                         continue;
4177                 }
4178
4179                 if (last == key.objectid) {
4180                         if (key.type == BTRFS_EXTENT_ITEM_KEY)
4181                                 last = key.objectid + key.offset;
4182                         else
4183                                 last = key.objectid + root->leafsize;
4184                         path->slots[0]++;
4185                         continue;
4186                 }
4187
4188                 ret = check_cache_range(root, cache, last,
4189                                         key.objectid - last);
4190                 if (ret)
4191                         break;
4192                 if (key.type == BTRFS_EXTENT_ITEM_KEY)
4193                         last = key.objectid + key.offset;
4194                 else
4195                         last = key.objectid + root->leafsize;
4196                 path->slots[0]++;
4197         }
4198
4199         if (last < cache->key.objectid + cache->key.offset)
4200                 ret = check_cache_range(root, cache, last,
4201                                         cache->key.objectid +
4202                                         cache->key.offset - last);
4203
4204 out:
4205         btrfs_free_path(path);
4206
4207         if (!ret &&
4208             !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
4209                 fprintf(stderr, "There are still entries left in the space "
4210                         "cache\n");
4211                 ret = -EINVAL;
4212         }
4213
4214         return ret;
4215 }
4216
4217 static int check_space_cache(struct btrfs_root *root)
4218 {
4219         struct btrfs_block_group_cache *cache;
4220         u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
4221         int ret;
4222         int error = 0;
4223
4224         if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
4225             btrfs_super_generation(root->fs_info->super_copy) !=
4226             btrfs_super_cache_generation(root->fs_info->super_copy)) {
4227                 printf("cache and super generation don't match, space cache "
4228                        "will be invalidated\n");
4229                 return 0;
4230         }
4231
4232         while (1) {
4233                 cache = btrfs_lookup_first_block_group(root->fs_info, start);
4234                 if (!cache)
4235                         break;
4236
4237                 start = cache->key.objectid + cache->key.offset;
4238                 if (!cache->free_space_ctl) {
4239                         if (btrfs_init_free_space_ctl(cache,
4240                                                       root->sectorsize)) {
4241                                 ret = -ENOMEM;
4242                                 break;
4243                         }
4244                 } else {
4245                         btrfs_remove_free_space_cache(cache);
4246                 }
4247
4248                 ret = load_free_space_cache(root->fs_info, cache);
4249                 if (!ret)
4250                         continue;
4251
4252                 ret = verify_space_cache(root, cache);
4253                 if (ret) {
4254                         fprintf(stderr, "cache appears valid but isnt %Lu\n",
4255                                 cache->key.objectid);
4256                         error++;
4257                 }
4258         }
4259
4260         return error ? -EINVAL : 0;
4261 }
4262
4263 static int read_extent_data(struct btrfs_root *root, char *data,
4264                         u64 logical, u64 *len, int mirror)
4265 {
4266         u64 offset = 0;
4267         struct btrfs_multi_bio *multi = NULL;
4268         struct btrfs_fs_info *info = root->fs_info;
4269         struct btrfs_device *device;
4270         int ret = 0;
4271         u64 max_len = *len;
4272
4273         ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
4274                               &multi, mirror, NULL);
4275         if (ret) {
4276                 fprintf(stderr, "Couldn't map the block %llu\n",
4277                                 logical + offset);
4278                 goto err;
4279         }
4280         device = multi->stripes[0].dev;
4281
4282         if (device->fd == 0)
4283                 goto err;
4284         if (*len > max_len)
4285                 *len = max_len;
4286
4287         ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
4288         if (ret != *len)
4289                 ret = -EIO;
4290         else
4291                 ret = 0;
4292 err:
4293         kfree(multi);
4294         return ret;
4295 }
4296
4297 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
4298                         u64 num_bytes, unsigned long leaf_offset,
4299                         struct extent_buffer *eb) {
4300
4301         u64 offset = 0;
4302         u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4303         char *data;
4304         unsigned long csum_offset;
4305         u32 csum;
4306         u32 csum_expected;
4307         u64 read_len;
4308         u64 data_checked = 0;
4309         u64 tmp;
4310         int ret = 0;
4311         int mirror;
4312         int num_copies;
4313
4314         if (num_bytes % root->sectorsize)
4315                 return -EINVAL;
4316
4317         data = malloc(num_bytes);
4318         if (!data)
4319                 return -ENOMEM;
4320
4321         while (offset < num_bytes) {
4322                 mirror = 0;
4323 again:
4324                 read_len = num_bytes - offset;
4325                 /* read as much space once a time */
4326                 ret = read_extent_data(root, data + offset,
4327                                 bytenr + offset, &read_len, mirror);
4328                 if (ret)
4329                         goto out;
4330                 data_checked = 0;
4331                 /* verify every 4k data's checksum */
4332                 while (data_checked < read_len) {
4333                         csum = ~(u32)0;
4334                         tmp = offset + data_checked;
4335
4336                         csum = btrfs_csum_data(NULL, (char *)data + tmp,
4337                                                csum, root->sectorsize);
4338                         btrfs_csum_final(csum, (char *)&csum);
4339
4340                         csum_offset = leaf_offset +
4341                                  tmp / root->sectorsize * csum_size;
4342                         read_extent_buffer(eb, (char *)&csum_expected,
4343                                            csum_offset, csum_size);
4344                         /* try another mirror */
4345                         if (csum != csum_expected) {
4346                                 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
4347                                                 mirror, bytenr + tmp,
4348                                                 csum, csum_expected);
4349                                 num_copies = btrfs_num_copies(
4350                                                 &root->fs_info->mapping_tree,
4351                                                 bytenr, num_bytes);
4352                                 if (mirror < num_copies - 1) {
4353                                         mirror += 1;
4354                                         goto again;
4355                                 }
4356                         }
4357                         data_checked += root->sectorsize;
4358                 }
4359                 offset += read_len;
4360         }
4361 out:
4362         free(data);
4363         return ret;
4364 }
4365
4366 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
4367                                u64 num_bytes)
4368 {
4369         struct btrfs_path *path;
4370         struct extent_buffer *leaf;
4371         struct btrfs_key key;
4372         int ret;
4373
4374         path = btrfs_alloc_path();
4375         if (!path) {
4376                 fprintf(stderr, "Error allocing path\n");
4377                 return -ENOMEM;
4378         }
4379
4380         key.objectid = bytenr;
4381         key.type = BTRFS_EXTENT_ITEM_KEY;
4382         key.offset = (u64)-1;
4383
4384 again:
4385         ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
4386                                 0, 0);
4387         if (ret < 0) {
4388                 fprintf(stderr, "Error looking up extent record %d\n", ret);
4389                 btrfs_free_path(path);
4390                 return ret;
4391         } else if (ret) {
4392                 if (path->slots[0] > 0) {
4393                         path->slots[0]--;
4394                 } else {
4395                         ret = btrfs_prev_leaf(root, path);
4396                         if (ret < 0) {
4397                                 goto out;
4398                         } else if (ret > 0) {
4399                                 ret = 0;
4400                                 goto out;
4401                         }
4402                 }
4403         }
4404
4405         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4406
4407         /*
4408          * Block group items come before extent items if they have the same
4409          * bytenr, so walk back one more just in case.  Dear future traveler,
4410          * first congrats on mastering time travel.  Now if it's not too much
4411          * trouble could you go back to 2006 and tell Chris to make the
4412          * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
4413          * EXTENT_ITEM_KEY please?
4414          */
4415         while (key.type > BTRFS_EXTENT_ITEM_KEY) {
4416                 if (path->slots[0] > 0) {
4417                         path->slots[0]--;
4418                 } else {
4419                         ret = btrfs_prev_leaf(root, path);
4420                         if (ret < 0) {
4421                                 goto out;
4422                         } else if (ret > 0) {
4423                                 ret = 0;
4424                                 goto out;
4425                         }
4426                 }
4427                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4428         }
4429
4430         while (num_bytes) {
4431                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4432                         ret = btrfs_next_leaf(root, path);
4433                         if (ret < 0) {
4434                                 fprintf(stderr, "Error going to next leaf "
4435                                         "%d\n", ret);
4436                                 btrfs_free_path(path);
4437                                 return ret;
4438                         } else if (ret) {
4439                                 break;
4440                         }
4441                 }
4442                 leaf = path->nodes[0];
4443                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4444                 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
4445                         path->slots[0]++;
4446                         continue;
4447                 }
4448                 if (key.objectid + key.offset < bytenr) {
4449                         path->slots[0]++;
4450                         continue;
4451                 }
4452                 if (key.objectid > bytenr + num_bytes)
4453                         break;
4454
4455                 if (key.objectid == bytenr) {
4456                         if (key.offset >= num_bytes) {
4457                                 num_bytes = 0;
4458                                 break;
4459                         }
4460                         num_bytes -= key.offset;
4461                         bytenr += key.offset;
4462                 } else if (key.objectid < bytenr) {
4463                         if (key.objectid + key.offset >= bytenr + num_bytes) {
4464                                 num_bytes = 0;
4465                                 break;
4466                         }
4467                         num_bytes = (bytenr + num_bytes) -
4468                                 (key.objectid + key.offset);
4469                         bytenr = key.objectid + key.offset;
4470                 } else {
4471                         if (key.objectid + key.offset < bytenr + num_bytes) {
4472                                 u64 new_start = key.objectid + key.offset;
4473                                 u64 new_bytes = bytenr + num_bytes - new_start;
4474
4475                                 /*
4476                                  * Weird case, the extent is in the middle of
4477                                  * our range, we'll have to search one side
4478                                  * and then the other.  Not sure if this happens
4479                                  * in real life, but no harm in coding it up
4480                                  * anyway just in case.
4481                                  */
4482                                 btrfs_release_path(path);
4483                                 ret = check_extent_exists(root, new_start,
4484                                                           new_bytes);
4485                                 if (ret) {
4486                                         fprintf(stderr, "Right section didn't "
4487                                                 "have a record\n");
4488                                         break;
4489                                 }
4490                                 num_bytes = key.objectid - bytenr;
4491                                 goto again;
4492                         }
4493                         num_bytes = key.objectid - bytenr;
4494                 }
4495                 path->slots[0]++;
4496         }
4497         ret = 0;
4498
4499 out:
4500         if (num_bytes && !ret) {
4501                 fprintf(stderr, "There are no extents for csum range "
4502                         "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
4503                 ret = 1;
4504         }
4505
4506         btrfs_free_path(path);
4507         return ret;
4508 }
4509
4510 static int check_csums(struct btrfs_root *root)
4511 {
4512         struct btrfs_path *path;
4513         struct extent_buffer *leaf;
4514         struct btrfs_key key;
4515         u64 offset = 0, num_bytes = 0;
4516         u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4517         int errors = 0;
4518         int ret;
4519         u64 data_len;
4520         unsigned long leaf_offset;
4521
4522         root = root->fs_info->csum_root;
4523         if (!extent_buffer_uptodate(root->node)) {
4524                 fprintf(stderr, "No valid csum tree found\n");
4525                 return -ENOENT;
4526         }
4527
4528         key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
4529         key.type = BTRFS_EXTENT_CSUM_KEY;
4530         key.offset = 0;
4531
4532         path = btrfs_alloc_path();
4533         if (!path)
4534                 return -ENOMEM;
4535
4536         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4537         if (ret < 0) {
4538                 fprintf(stderr, "Error searching csum tree %d\n", ret);
4539                 btrfs_free_path(path);
4540                 return ret;
4541         }
4542
4543         if (ret > 0 && path->slots[0])
4544                 path->slots[0]--;
4545         ret = 0;
4546
4547         while (1) {
4548                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4549                         ret = btrfs_next_leaf(root, path);
4550                         if (ret < 0) {
4551                                 fprintf(stderr, "Error going to next leaf "
4552                                         "%d\n", ret);
4553                                 break;
4554                         }
4555                         if (ret)
4556                                 break;
4557                 }
4558                 leaf = path->nodes[0];
4559
4560                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4561                 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
4562                         path->slots[0]++;
4563                         continue;
4564                 }
4565
4566                 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
4567                               csum_size) * root->sectorsize;
4568                 if (!check_data_csum)
4569                         goto skip_csum_check;
4570                 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
4571                 ret = check_extent_csums(root, key.offset, data_len,
4572                                          leaf_offset, leaf);
4573                 if (ret)
4574                         break;
4575 skip_csum_check:
4576                 if (!num_bytes) {
4577                         offset = key.offset;
4578                 } else if (key.offset != offset + num_bytes) {
4579                         ret = check_extent_exists(root, offset, num_bytes);
4580                         if (ret) {
4581                                 fprintf(stderr, "Csum exists for %Lu-%Lu but "
4582                                         "there is no extent record\n",
4583                                         offset, offset+num_bytes);
4584                                 errors++;
4585                         }
4586                         offset = key.offset;
4587                         num_bytes = 0;
4588                 }
4589                 num_bytes += data_len;
4590                 path->slots[0]++;
4591         }
4592
4593         btrfs_free_path(path);
4594         return errors;
4595 }
4596
4597 static int is_dropped_key(struct btrfs_key *key,
4598                           struct btrfs_key *drop_key) {
4599         if (key->objectid < drop_key->objectid)
4600                 return 1;
4601         else if (key->objectid == drop_key->objectid) {
4602                 if (key->type < drop_key->type)
4603                         return 1;
4604                 else if (key->type == drop_key->type) {
4605                         if (key->offset < drop_key->offset)
4606                                 return 1;
4607                 }
4608         }
4609         return 0;
4610 }
4611
4612 static int run_next_block(struct btrfs_trans_handle *trans,
4613                           struct btrfs_root *root,
4614                           struct block_info *bits,
4615                           int bits_nr,
4616                           u64 *last,
4617                           struct cache_tree *pending,
4618                           struct cache_tree *seen,
4619                           struct cache_tree *reada,
4620                           struct cache_tree *nodes,
4621                           struct cache_tree *extent_cache,
4622                           struct cache_tree *chunk_cache,
4623                           struct rb_root *dev_cache,
4624                           struct block_group_tree *block_group_cache,
4625                           struct device_extent_tree *dev_extent_cache,
4626                           struct btrfs_root_item *ri)
4627 {
4628         struct extent_buffer *buf;
4629         u64 bytenr;
4630         u32 size;
4631         u64 parent;
4632         u64 owner;
4633         u64 flags;
4634         u64 ptr;
4635         u64 gen = 0;
4636         int ret = 0;
4637         int i;
4638         int nritems;
4639         struct btrfs_key key;
4640         struct cache_extent *cache;
4641         int reada_bits;
4642
4643         nritems = pick_next_pending(pending, reada, nodes, *last, bits,
4644                                     bits_nr, &reada_bits);
4645         if (nritems == 0)
4646                 return 1;
4647
4648         if (!reada_bits) {
4649                 for(i = 0; i < nritems; i++) {
4650                         ret = add_cache_extent(reada, bits[i].start,
4651                                                bits[i].size);
4652                         if (ret == -EEXIST)
4653                                 continue;
4654
4655                         /* fixme, get the parent transid */
4656                         readahead_tree_block(root, bits[i].start,
4657                                              bits[i].size, 0);
4658                 }
4659         }
4660         *last = bits[0].start;
4661         bytenr = bits[0].start;
4662         size = bits[0].size;
4663
4664         cache = lookup_cache_extent(pending, bytenr, size);
4665         if (cache) {
4666                 remove_cache_extent(pending, cache);
4667                 free(cache);
4668         }
4669         cache = lookup_cache_extent(reada, bytenr, size);
4670         if (cache) {
4671                 remove_cache_extent(reada, cache);
4672                 free(cache);
4673         }
4674         cache = lookup_cache_extent(nodes, bytenr, size);
4675         if (cache) {
4676                 remove_cache_extent(nodes, cache);
4677                 free(cache);
4678         }
4679         cache = lookup_cache_extent(extent_cache, bytenr, size);
4680         if (cache) {
4681                 struct extent_record *rec;
4682
4683                 rec = container_of(cache, struct extent_record, cache);
4684                 gen = rec->parent_generation;
4685         }
4686
4687         /* fixme, get the real parent transid */
4688         buf = read_tree_block(root, bytenr, size, gen);
4689         if (!extent_buffer_uptodate(buf)) {
4690                 record_bad_block_io(root->fs_info,
4691                                     extent_cache, bytenr, size);
4692                 goto out;
4693         }
4694
4695         nritems = btrfs_header_nritems(buf);
4696
4697         /*
4698          * FIXME, this only works only if we don't have any full
4699          * backref mode.
4700          */
4701         if (!init_extent_tree) {
4702                 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
4703                                        btrfs_header_level(buf), 1, NULL,
4704                                        &flags);
4705                 if (ret < 0)
4706                         flags = 0;
4707         } else {
4708                 flags = 0;
4709         }
4710
4711         if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4712                 parent = bytenr;
4713                 owner = 0;
4714         } else {
4715                 parent = 0;
4716                 owner = btrfs_header_owner(buf);
4717         }
4718
4719         ret = check_block(trans, root, extent_cache, buf, flags);
4720         if (ret)
4721                 goto out;
4722
4723         if (btrfs_is_leaf(buf)) {
4724                 btree_space_waste += btrfs_leaf_free_space(root, buf);
4725                 for (i = 0; i < nritems; i++) {
4726                         struct btrfs_file_extent_item *fi;
4727                         btrfs_item_key_to_cpu(buf, &key, i);
4728                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
4729                                 process_extent_item(root, extent_cache, buf,
4730                                                     i);
4731                                 continue;
4732                         }
4733                         if (key.type == BTRFS_METADATA_ITEM_KEY) {
4734                                 process_extent_item(root, extent_cache, buf,
4735                                                     i);
4736                                 continue;
4737                         }
4738                         if (key.type == BTRFS_EXTENT_CSUM_KEY) {
4739                                 total_csum_bytes +=
4740                                         btrfs_item_size_nr(buf, i);
4741                                 continue;
4742                         }
4743                         if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4744                                 process_chunk_item(chunk_cache, &key, buf, i);
4745                                 continue;
4746                         }
4747                         if (key.type == BTRFS_DEV_ITEM_KEY) {
4748                                 process_device_item(dev_cache, &key, buf, i);
4749                                 continue;
4750                         }
4751                         if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4752                                 process_block_group_item(block_group_cache,
4753                                         &key, buf, i);
4754                                 continue;
4755                         }
4756                         if (key.type == BTRFS_DEV_EXTENT_KEY) {
4757                                 process_device_extent_item(dev_extent_cache,
4758                                         &key, buf, i);
4759                                 continue;
4760
4761                         }
4762                         if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
4763 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4764                                 process_extent_ref_v0(extent_cache, buf, i);
4765 #else
4766                                 BUG();
4767 #endif
4768                                 continue;
4769                         }
4770
4771                         if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
4772                                 add_tree_backref(extent_cache, key.objectid, 0,
4773                                                  key.offset, 0);
4774                                 continue;
4775                         }
4776                         if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
4777                                 add_tree_backref(extent_cache, key.objectid,
4778                                                  key.offset, 0, 0);
4779                                 continue;
4780                         }
4781                         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
4782                                 struct btrfs_extent_data_ref *ref;
4783                                 ref = btrfs_item_ptr(buf, i,
4784                                                 struct btrfs_extent_data_ref);
4785                                 add_data_backref(extent_cache,
4786                                         key.objectid, 0,
4787                                         btrfs_extent_data_ref_root(buf, ref),
4788                                         btrfs_extent_data_ref_objectid(buf,
4789                                                                        ref),
4790                                         btrfs_extent_data_ref_offset(buf, ref),
4791                                         btrfs_extent_data_ref_count(buf, ref),
4792                                         0, root->sectorsize);
4793                                 continue;
4794                         }
4795                         if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4796                                 struct btrfs_shared_data_ref *ref;
4797                                 ref = btrfs_item_ptr(buf, i,
4798                                                 struct btrfs_shared_data_ref);
4799                                 add_data_backref(extent_cache,
4800                                         key.objectid, key.offset, 0, 0, 0,
4801                                         btrfs_shared_data_ref_count(buf, ref),
4802                                         0, root->sectorsize);
4803                                 continue;
4804                         }
4805                         if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4806                                 struct bad_item *bad;
4807
4808                                 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4809                                         continue;
4810                                 if (!owner)
4811                                         continue;
4812                                 bad = malloc(sizeof(struct bad_item));
4813                                 if (!bad)
4814                                         continue;
4815                                 INIT_LIST_HEAD(&bad->list);
4816                                 memcpy(&bad->key, &key,
4817                                        sizeof(struct btrfs_key));
4818                                 bad->root_id = owner;
4819                                 list_add_tail(&bad->list, &delete_items);
4820                                 continue;
4821                         }
4822                         if (key.type != BTRFS_EXTENT_DATA_KEY)
4823                                 continue;
4824                         fi = btrfs_item_ptr(buf, i,
4825                                             struct btrfs_file_extent_item);
4826                         if (btrfs_file_extent_type(buf, fi) ==
4827                             BTRFS_FILE_EXTENT_INLINE)
4828                                 continue;
4829                         if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4830                                 continue;
4831
4832                         data_bytes_allocated +=
4833                                 btrfs_file_extent_disk_num_bytes(buf, fi);
4834                         if (data_bytes_allocated < root->sectorsize) {
4835                                 abort();
4836                         }
4837                         data_bytes_referenced +=
4838                                 btrfs_file_extent_num_bytes(buf, fi);
4839                         add_data_backref(extent_cache,
4840                                 btrfs_file_extent_disk_bytenr(buf, fi),
4841                                 parent, owner, key.objectid, key.offset -
4842                                 btrfs_file_extent_offset(buf, fi), 1, 1,
4843                                 btrfs_file_extent_disk_num_bytes(buf, fi));
4844                 }
4845         } else {
4846                 int level;
4847                 struct btrfs_key first_key;
4848
4849                 first_key.objectid = 0;
4850
4851                 if (nritems > 0)
4852                         btrfs_item_key_to_cpu(buf, &first_key, 0);
4853                 level = btrfs_header_level(buf);
4854                 for (i = 0; i < nritems; i++) {
4855                         ptr = btrfs_node_blockptr(buf, i);
4856                         size = btrfs_level_size(root, level - 1);
4857                         btrfs_node_key_to_cpu(buf, &key, i);
4858                         if (ri != NULL) {
4859                                 struct btrfs_key drop_key;
4860                                 btrfs_disk_key_to_cpu(&drop_key,
4861                                                       &ri->drop_progress);
4862                                 if ((level == ri->drop_level)
4863                                     && is_dropped_key(&key, &drop_key)) {
4864                                         continue;
4865                                 }
4866                         }
4867                         ret = add_extent_rec(extent_cache, &key,
4868                                              btrfs_node_ptr_generation(buf, i),
4869                                              ptr, size, 0, 0, 1, 0, 1, 0,
4870                                              size);
4871                         BUG_ON(ret);
4872
4873                         add_tree_backref(extent_cache, ptr, parent, owner, 1);
4874
4875                         if (level > 1) {
4876                                 add_pending(nodes, seen, ptr, size);
4877                         } else {
4878                                 add_pending(pending, seen, ptr, size);
4879                         }
4880                 }
4881                 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4882                                       nritems) * sizeof(struct btrfs_key_ptr);
4883         }
4884         total_btree_bytes += buf->len;
4885         if (fs_root_objectid(btrfs_header_owner(buf)))
4886                 total_fs_tree_bytes += buf->len;
4887         if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4888                 total_extent_tree_bytes += buf->len;
4889         if (!found_old_backref &&
4890             btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4891             btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4892             !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4893                 found_old_backref = 1;
4894 out:
4895         free_extent_buffer(buf);
4896         return ret;
4897 }
4898
4899 static int add_root_to_pending(struct extent_buffer *buf,
4900                                struct cache_tree *extent_cache,
4901                                struct cache_tree *pending,
4902                                struct cache_tree *seen,
4903                                struct cache_tree *nodes,
4904                                struct btrfs_key *root_key)
4905 {
4906         if (btrfs_header_level(buf) > 0)
4907                 add_pending(nodes, seen, buf->start, buf->len);
4908         else
4909                 add_pending(pending, seen, buf->start, buf->len);
4910         add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4911                        0, 1, 1, 0, 1, 0, buf->len);
4912
4913         if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4914             btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4915                 add_tree_backref(extent_cache, buf->start, buf->start,
4916                                  0, 1);
4917         else
4918                 add_tree_backref(extent_cache, buf->start, 0,
4919                                  root_key->objectid, 1);
4920         return 0;
4921 }
4922
4923 /* as we fix the tree, we might be deleting blocks that
4924  * we're tracking for repair.  This hook makes sure we
4925  * remove any backrefs for blocks as we are fixing them.
4926  */
4927 static int free_extent_hook(struct btrfs_trans_handle *trans,
4928                             struct btrfs_root *root,
4929                             u64 bytenr, u64 num_bytes, u64 parent,
4930                             u64 root_objectid, u64 owner, u64 offset,
4931                             int refs_to_drop)
4932 {
4933         struct extent_record *rec;
4934         struct cache_extent *cache;
4935         int is_data;
4936         struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4937
4938         is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4939         cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4940         if (!cache)
4941                 return 0;
4942
4943         rec = container_of(cache, struct extent_record, cache);
4944         if (is_data) {
4945                 struct data_backref *back;
4946                 back = find_data_backref(rec, parent, root_objectid, owner,
4947                                          offset, 1, bytenr, num_bytes);
4948                 if (!back)
4949                         goto out;
4950                 if (back->node.found_ref) {
4951                         back->found_ref -= refs_to_drop;
4952                         if (rec->refs)
4953                                 rec->refs -= refs_to_drop;
4954                 }
4955                 if (back->node.found_extent_tree) {
4956                         back->num_refs -= refs_to_drop;
4957                         if (rec->extent_item_refs)
4958                                 rec->extent_item_refs -= refs_to_drop;
4959                 }
4960                 if (back->found_ref == 0)
4961                         back->node.found_ref = 0;
4962                 if (back->num_refs == 0)
4963                         back->node.found_extent_tree = 0;
4964
4965                 if (!back->node.found_extent_tree && back->node.found_ref) {
4966                         list_del(&back->node.list);
4967                         free(back);
4968                 }
4969         } else {
4970                 struct tree_backref *back;
4971                 back = find_tree_backref(rec, parent, root_objectid);
4972                 if (!back)
4973                         goto out;
4974                 if (back->node.found_ref) {
4975                         if (rec->refs)
4976                                 rec->refs--;
4977                         back->node.found_ref = 0;
4978                 }
4979                 if (back->node.found_extent_tree) {
4980                         if (rec->extent_item_refs)
4981                                 rec->extent_item_refs--;
4982                         back->node.found_extent_tree = 0;
4983                 }
4984                 if (!back->node.found_extent_tree && back->node.found_ref) {
4985                         list_del(&back->node.list);
4986                         free(back);
4987                 }
4988         }
4989         maybe_free_extent_rec(extent_cache, rec);
4990 out:
4991         return 0;
4992 }
4993
4994 static int delete_extent_records(struct btrfs_trans_handle *trans,
4995                                  struct btrfs_root *root,
4996                                  struct btrfs_path *path,
4997                                  u64 bytenr, u64 new_len)
4998 {
4999         struct btrfs_key key;
5000         struct btrfs_key found_key;
5001         struct extent_buffer *leaf;
5002         int ret;
5003         int slot;
5004
5005
5006         key.objectid = bytenr;
5007         key.type = (u8)-1;
5008         key.offset = (u64)-1;
5009
5010         while(1) {
5011                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
5012                                         &key, path, 0, 1);
5013                 if (ret < 0)
5014                         break;
5015
5016                 if (ret > 0) {
5017                         ret = 0;
5018                         if (path->slots[0] == 0)
5019                                 break;
5020                         path->slots[0]--;
5021                 }
5022                 ret = 0;
5023
5024                 leaf = path->nodes[0];
5025                 slot = path->slots[0];
5026
5027                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5028                 if (found_key.objectid != bytenr)
5029                         break;
5030
5031                 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
5032                     found_key.type != BTRFS_METADATA_ITEM_KEY &&
5033                     found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5034                     found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
5035                     found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
5036                     found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
5037                     found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
5038                         btrfs_release_path(path);
5039                         if (found_key.type == 0) {
5040                                 if (found_key.offset == 0)
5041                                         break;
5042                                 key.offset = found_key.offset - 1;
5043                                 key.type = found_key.type;
5044                         }
5045                         key.type = found_key.type - 1;
5046                         key.offset = (u64)-1;
5047                         continue;
5048                 }
5049
5050                 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
5051                         found_key.objectid, found_key.type, found_key.offset);
5052
5053                 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
5054                 if (ret)
5055                         break;
5056                 btrfs_release_path(path);
5057
5058                 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5059                     found_key.type == BTRFS_METADATA_ITEM_KEY) {
5060                         u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
5061                                 found_key.offset : root->leafsize;
5062
5063                         ret = btrfs_update_block_group(trans, root, bytenr,
5064                                                        bytes, 0, 0);
5065                         if (ret)
5066                                 break;
5067                 }
5068         }
5069
5070         btrfs_release_path(path);
5071         return ret;
5072 }
5073
5074 /*
5075  * for a single backref, this will allocate a new extent
5076  * and add the backref to it.
5077  */
5078 static int record_extent(struct btrfs_trans_handle *trans,
5079                          struct btrfs_fs_info *info,
5080                          struct btrfs_path *path,
5081                          struct extent_record *rec,
5082                          struct extent_backref *back,
5083                          int allocated, u64 flags)
5084 {
5085         int ret;
5086         struct btrfs_root *extent_root = info->extent_root;
5087         struct extent_buffer *leaf;
5088         struct btrfs_key ins_key;
5089         struct btrfs_extent_item *ei;
5090         struct tree_backref *tback;
5091         struct data_backref *dback;
5092         struct btrfs_tree_block_info *bi;
5093
5094         if (!back->is_data)
5095                 rec->max_size = max_t(u64, rec->max_size,
5096                                     info->extent_root->leafsize);
5097
5098         if (!allocated) {
5099                 u32 item_size = sizeof(*ei);
5100
5101                 if (!back->is_data)
5102                         item_size += sizeof(*bi);
5103
5104                 ins_key.objectid = rec->start;
5105                 ins_key.offset = rec->max_size;
5106                 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
5107
5108                 ret = btrfs_insert_empty_item(trans, extent_root, path,
5109                                         &ins_key, item_size);
5110                 if (ret)
5111                         goto fail;
5112
5113                 leaf = path->nodes[0];
5114                 ei = btrfs_item_ptr(leaf, path->slots[0],
5115                                     struct btrfs_extent_item);
5116
5117                 btrfs_set_extent_refs(leaf, ei, 0);
5118                 btrfs_set_extent_generation(leaf, ei, rec->generation);
5119
5120                 if (back->is_data) {
5121                         btrfs_set_extent_flags(leaf, ei,
5122                                                BTRFS_EXTENT_FLAG_DATA);
5123                 } else {
5124                         struct btrfs_disk_key copy_key;;
5125
5126                         tback = (struct tree_backref *)back;
5127                         bi = (struct btrfs_tree_block_info *)(ei + 1);
5128                         memset_extent_buffer(leaf, 0, (unsigned long)bi,
5129                                              sizeof(*bi));
5130
5131                         btrfs_set_disk_key_objectid(&copy_key,
5132                                                     rec->info_objectid);
5133                         btrfs_set_disk_key_type(&copy_key, 0);
5134                         btrfs_set_disk_key_offset(&copy_key, 0);
5135
5136                         btrfs_set_tree_block_level(leaf, bi, rec->info_level);
5137                         btrfs_set_tree_block_key(leaf, bi, &copy_key);
5138
5139                         btrfs_set_extent_flags(leaf, ei,
5140                                                BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
5141                 }
5142
5143                 btrfs_mark_buffer_dirty(leaf);
5144                 ret = btrfs_update_block_group(trans, extent_root, rec->start,
5145                                                rec->max_size, 1, 0);
5146                 if (ret)
5147                         goto fail;
5148                 btrfs_release_path(path);
5149         }
5150
5151         if (back->is_data) {
5152                 u64 parent;
5153                 int i;
5154
5155                 dback = (struct data_backref *)back;
5156                 if (back->full_backref)
5157                         parent = dback->parent;
5158                 else
5159                         parent = 0;
5160
5161                 for (i = 0; i < dback->found_ref; i++) {
5162                         /* if parent != 0, we're doing a full backref
5163                          * passing BTRFS_FIRST_FREE_OBJECTID as the owner
5164                          * just makes the backref allocator create a data
5165                          * backref
5166                          */
5167                         ret = btrfs_inc_extent_ref(trans, info->extent_root,
5168                                                    rec->start, rec->max_size,
5169                                                    parent,
5170                                                    dback->root,
5171                                                    parent ?
5172                                                    BTRFS_FIRST_FREE_OBJECTID :
5173                                                    dback->owner,
5174                                                    dback->offset);
5175                         if (ret)
5176                                 break;
5177                 }
5178                 fprintf(stderr, "adding new data backref"
5179                                 " on %llu %s %llu owner %llu"
5180                                 " offset %llu found %d\n",
5181                                 (unsigned long long)rec->start,
5182                                 back->full_backref ?
5183                                 "parent" : "root",
5184                                 back->full_backref ?
5185                                 (unsigned long long)parent :
5186                                 (unsigned long long)dback->root,
5187                                 (unsigned long long)dback->owner,
5188                                 (unsigned long long)dback->offset,
5189                                 dback->found_ref);
5190         } else {
5191                 u64 parent;
5192
5193                 tback = (struct tree_backref *)back;
5194                 if (back->full_backref)
5195                         parent = tback->parent;
5196                 else
5197                         parent = 0;
5198
5199                 ret = btrfs_inc_extent_ref(trans, info->extent_root,
5200                                            rec->start, rec->max_size,
5201                                            parent, tback->root, 0, 0);
5202                 fprintf(stderr, "adding new tree backref on "
5203                         "start %llu len %llu parent %llu root %llu\n",
5204                         rec->start, rec->max_size, tback->parent, tback->root);
5205         }
5206         if (ret)
5207                 goto fail;
5208 fail:
5209         btrfs_release_path(path);
5210         return ret;
5211 }
5212
5213 struct extent_entry {
5214         u64 bytenr;
5215         u64 bytes;
5216         int count;
5217         int broken;
5218         struct list_head list;
5219 };
5220
5221 static struct extent_entry *find_entry(struct list_head *entries,
5222                                        u64 bytenr, u64 bytes)
5223 {
5224         struct extent_entry *entry = NULL;
5225
5226         list_for_each_entry(entry, entries, list) {
5227                 if (entry->bytenr == bytenr && entry->bytes == bytes)
5228                         return entry;
5229         }
5230
5231         return NULL;
5232 }
5233
5234 static struct extent_entry *find_most_right_entry(struct list_head *entries)
5235 {
5236         struct extent_entry *entry, *best = NULL, *prev = NULL;
5237
5238         list_for_each_entry(entry, entries, list) {
5239                 if (!prev) {
5240                         prev = entry;
5241                         continue;
5242                 }
5243
5244                 /*
5245                  * If there are as many broken entries as entries then we know
5246                  * not to trust this particular entry.
5247                  */
5248                 if (entry->broken == entry->count)
5249                         continue;
5250
5251                 /*
5252                  * If our current entry == best then we can't be sure our best
5253                  * is really the best, so we need to keep searching.
5254                  */
5255                 if (best && best->count == entry->count) {
5256                         prev = entry;
5257                         best = NULL;
5258                         continue;
5259                 }
5260
5261                 /* Prev == entry, not good enough, have to keep searching */
5262                 if (!prev->broken && prev->count == entry->count)
5263                         continue;
5264
5265                 if (!best)
5266                         best = (prev->count > entry->count) ? prev : entry;
5267                 else if (best->count < entry->count)
5268                         best = entry;
5269                 prev = entry;
5270         }
5271
5272         return best;
5273 }
5274
5275 static int repair_ref(struct btrfs_trans_handle *trans,
5276                       struct btrfs_fs_info *info, struct btrfs_path *path,
5277                       struct data_backref *dback, struct extent_entry *entry)
5278 {
5279         struct btrfs_root *root;
5280         struct btrfs_file_extent_item *fi;
5281         struct extent_buffer *leaf;
5282         struct btrfs_key key;
5283         u64 bytenr, bytes;
5284         int ret;
5285
5286         key.objectid = dback->root;
5287         key.type = BTRFS_ROOT_ITEM_KEY;
5288         key.offset = (u64)-1;
5289         root = btrfs_read_fs_root(info, &key);
5290         if (IS_ERR(root)) {
5291                 fprintf(stderr, "Couldn't find root for our ref\n");
5292                 return -EINVAL;
5293         }
5294
5295         /*
5296          * The backref points to the original offset of the extent if it was
5297          * split, so we need to search down to the offset we have and then walk
5298          * forward until we find the backref we're looking for.
5299          */
5300         key.objectid = dback->owner;
5301         key.type = BTRFS_EXTENT_DATA_KEY;
5302         key.offset = dback->offset;
5303         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5304         if (ret < 0) {
5305                 fprintf(stderr, "Error looking up ref %d\n", ret);
5306                 return ret;
5307         }
5308
5309         while (1) {
5310                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5311                         ret = btrfs_next_leaf(root, path);
5312                         if (ret) {
5313                                 fprintf(stderr, "Couldn't find our ref, next\n");
5314                                 return -EINVAL;
5315                         }
5316                 }
5317                 leaf = path->nodes[0];
5318                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5319                 if (key.objectid != dback->owner ||
5320                     key.type != BTRFS_EXTENT_DATA_KEY) {
5321                         fprintf(stderr, "Couldn't find our ref, search\n");
5322                         return -EINVAL;
5323                 }
5324                 fi = btrfs_item_ptr(leaf, path->slots[0],
5325                                     struct btrfs_file_extent_item);
5326                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5327                 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5328
5329                 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
5330                         break;
5331                 path->slots[0]++;
5332         }
5333
5334         btrfs_release_path(path);
5335
5336         /*
5337          * Have to make sure that this root gets updated when we commit the
5338          * transaction
5339          */
5340         record_root_in_trans(trans, root);
5341
5342         /*
5343          * Ok we have the key of the file extent we want to fix, now we can cow
5344          * down to the thing and fix it.
5345          */
5346         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5347         if (ret < 0) {
5348                 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
5349                         key.objectid, key.type, key.offset, ret);
5350                 return ret;
5351         }
5352         if (ret > 0) {
5353                 fprintf(stderr, "Well that's odd, we just found this key "
5354                         "[%Lu, %u, %Lu]\n", key.objectid, key.type,
5355                         key.offset);
5356                 return -EINVAL;
5357         }
5358         leaf = path->nodes[0];
5359         fi = btrfs_item_ptr(leaf, path->slots[0],
5360                             struct btrfs_file_extent_item);
5361
5362         if (btrfs_file_extent_compression(leaf, fi) &&
5363             dback->disk_bytenr != entry->bytenr) {
5364                 fprintf(stderr, "Ref doesn't match the record start and is "
5365                         "compressed, please take a btrfs-image of this file "
5366                         "system and send it to a btrfs developer so they can "
5367                         "complete this functionality for bytenr %Lu\n",
5368                         dback->disk_bytenr);
5369                 return -EINVAL;
5370         }
5371
5372         if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
5373                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5374         } else if (dback->disk_bytenr > entry->bytenr) {
5375                 u64 off_diff, offset;
5376
5377                 off_diff = dback->disk_bytenr - entry->bytenr;
5378                 offset = btrfs_file_extent_offset(leaf, fi);
5379                 if (dback->disk_bytenr + offset +
5380                     btrfs_file_extent_num_bytes(leaf, fi) >
5381                     entry->bytenr + entry->bytes) {
5382                         fprintf(stderr, "Ref is past the entry end, please "
5383                                 "take a btrfs-image of this file system and "
5384                                 "send it to a btrfs developer, ref %Lu\n",
5385                                 dback->disk_bytenr);
5386                         return -EINVAL;
5387                 }
5388                 offset += off_diff;
5389                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5390                 btrfs_set_file_extent_offset(leaf, fi, offset);
5391         } else if (dback->disk_bytenr < entry->bytenr) {
5392                 u64 offset;
5393
5394                 offset = btrfs_file_extent_offset(leaf, fi);
5395                 if (dback->disk_bytenr + offset < entry->bytenr) {
5396                         fprintf(stderr, "Ref is before the entry start, please"
5397                                 " take a btrfs-image of this file system and "
5398                                 "send it to a btrfs developer, ref %Lu\n",
5399                                 dback->disk_bytenr);
5400                         return -EINVAL;
5401                 }
5402
5403                 offset += dback->disk_bytenr;
5404                 offset -= entry->bytenr;
5405                 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5406                 btrfs_set_file_extent_offset(leaf, fi, offset);
5407         }
5408
5409         btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
5410
5411         /*
5412          * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
5413          * only do this if we aren't using compression, otherwise it's a
5414          * trickier case.
5415          */
5416         if (!btrfs_file_extent_compression(leaf, fi))
5417                 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
5418         else
5419                 printf("ram bytes may be wrong?\n");
5420         btrfs_mark_buffer_dirty(leaf);
5421         btrfs_release_path(path);
5422         return 0;
5423 }
5424
5425 static int verify_backrefs(struct btrfs_trans_handle *trans,
5426                            struct btrfs_fs_info *info, struct btrfs_path *path,
5427                            struct extent_record *rec)
5428 {
5429         struct extent_backref *back;
5430         struct data_backref *dback;
5431         struct extent_entry *entry, *best = NULL;
5432         LIST_HEAD(entries);
5433         int nr_entries = 0;
5434         int broken_entries = 0;
5435         int ret = 0;
5436         short mismatch = 0;
5437
5438         /*
5439          * Metadata is easy and the backrefs should always agree on bytenr and
5440          * size, if not we've got bigger issues.
5441          */
5442         if (rec->metadata)
5443                 return 0;
5444
5445         list_for_each_entry(back, &rec->backrefs, list) {
5446                 if (back->full_backref || !back->is_data)
5447                         continue;
5448
5449                 dback = (struct data_backref *)back;
5450
5451                 /*
5452                  * We only pay attention to backrefs that we found a real
5453                  * backref for.
5454                  */
5455                 if (dback->found_ref == 0)
5456                         continue;
5457
5458                 /*
5459                  * For now we only catch when the bytes don't match, not the
5460                  * bytenr.  We can easily do this at the same time, but I want
5461                  * to have a fs image to test on before we just add repair
5462                  * functionality willy-nilly so we know we won't screw up the
5463                  * repair.
5464                  */
5465
5466                 entry = find_entry(&entries, dback->disk_bytenr,
5467                                    dback->bytes);
5468                 if (!entry) {
5469                         entry = malloc(sizeof(struct extent_entry));
5470                         if (!entry) {
5471                                 ret = -ENOMEM;
5472                                 goto out;
5473                         }
5474                         memset(entry, 0, sizeof(*entry));
5475                         entry->bytenr = dback->disk_bytenr;
5476                         entry->bytes = dback->bytes;
5477                         list_add_tail(&entry->list, &entries);
5478                         nr_entries++;
5479                 }
5480
5481                 /*
5482                  * If we only have on entry we may think the entries agree when
5483                  * in reality they don't so we have to do some extra checking.
5484                  */
5485                 if (dback->disk_bytenr != rec->start ||
5486                     dback->bytes != rec->nr || back->broken)
5487                         mismatch = 1;
5488
5489                 if (back->broken) {
5490                         entry->broken++;
5491                         broken_entries++;
5492                 }
5493
5494                 entry->count++;
5495         }
5496
5497         /* Yay all the backrefs agree, carry on good sir */
5498         if (nr_entries <= 1 && !mismatch)
5499                 goto out;
5500
5501         fprintf(stderr, "attempting to repair backref discrepency for bytenr "
5502                 "%Lu\n", rec->start);
5503
5504         /*
5505          * First we want to see if the backrefs can agree amongst themselves who
5506          * is right, so figure out which one of the entries has the highest
5507          * count.
5508          */
5509         best = find_most_right_entry(&entries);
5510
5511         /*
5512          * Ok so we may have an even split between what the backrefs think, so
5513          * this is where we use the extent ref to see what it thinks.
5514          */
5515         if (!best) {
5516                 entry = find_entry(&entries, rec->start, rec->nr);
5517                 if (!entry && (!broken_entries || !rec->found_rec)) {
5518                         fprintf(stderr, "Backrefs don't agree with each other "
5519                                 "and extent record doesn't agree with anybody,"
5520                                 " so we can't fix bytenr %Lu bytes %Lu\n",
5521                                 rec->start, rec->nr);
5522                         ret = -EINVAL;
5523                         goto out;
5524                 } else if (!entry) {
5525                         /*
5526                          * Ok our backrefs were broken, we'll assume this is the
5527                          * correct value and add an entry for this range.
5528                          */
5529                         entry = malloc(sizeof(struct extent_entry));
5530                         if (!entry) {
5531                                 ret = -ENOMEM;
5532                                 goto out;
5533                         }
5534                         memset(entry, 0, sizeof(*entry));
5535                         entry->bytenr = rec->start;
5536                         entry->bytes = rec->nr;
5537                         list_add_tail(&entry->list, &entries);
5538                         nr_entries++;
5539                 }
5540                 entry->count++;
5541                 best = find_most_right_entry(&entries);
5542                 if (!best) {
5543                         fprintf(stderr, "Backrefs and extent record evenly "
5544                                 "split on who is right, this is going to "
5545                                 "require user input to fix bytenr %Lu bytes "
5546                                 "%Lu\n", rec->start, rec->nr);
5547                         ret = -EINVAL;
5548                         goto out;
5549                 }
5550         }
5551
5552         /*
5553          * I don't think this can happen currently as we'll abort() if we catch
5554          * this case higher up, but in case somebody removes that we still can't
5555          * deal with it properly here yet, so just bail out of that's the case.
5556          */
5557         if (best->bytenr != rec->start) {
5558                 fprintf(stderr, "Extent start and backref starts don't match, "
5559                         "please use btrfs-image on this file system and send "
5560                         "it to a btrfs developer so they can make fsck fix "
5561                         "this particular case.  bytenr is %Lu, bytes is %Lu\n",
5562                         rec->start, rec->nr);
5563                 ret = -EINVAL;
5564                 goto out;
5565         }
5566
5567         /*
5568          * Ok great we all agreed on an extent record, let's go find the real
5569          * references and fix up the ones that don't match.
5570          */
5571         list_for_each_entry(back, &rec->backrefs, list) {
5572                 if (back->full_backref || !back->is_data)
5573                         continue;
5574
5575                 dback = (struct data_backref *)back;
5576
5577                 /*
5578                  * Still ignoring backrefs that don't have a real ref attached
5579                  * to them.
5580                  */
5581                 if (dback->found_ref == 0)
5582                         continue;
5583
5584                 if (dback->bytes == best->bytes &&
5585                     dback->disk_bytenr == best->bytenr)
5586                         continue;
5587
5588                 ret = repair_ref(trans, info, path, dback, best);
5589                 if (ret)
5590                         goto out;
5591         }
5592
5593         /*
5594          * Ok we messed with the actual refs, which means we need to drop our
5595          * entire cache and go back and rescan.  I know this is a huge pain and
5596          * adds a lot of extra work, but it's the only way to be safe.  Once all
5597          * the backrefs agree we may not need to do anything to the extent
5598          * record itself.
5599          */
5600         ret = -EAGAIN;
5601 out:
5602         while (!list_empty(&entries)) {
5603                 entry = list_entry(entries.next, struct extent_entry, list);
5604                 list_del_init(&entry->list);
5605                 free(entry);
5606         }
5607         return ret;
5608 }
5609
5610 static int process_duplicates(struct btrfs_root *root,
5611                               struct cache_tree *extent_cache,
5612                               struct extent_record *rec)
5613 {
5614         struct extent_record *good, *tmp;
5615         struct cache_extent *cache;
5616         int ret;
5617
5618         /*
5619          * If we found a extent record for this extent then return, or if we
5620          * have more than one duplicate we are likely going to need to delete
5621          * something.
5622          */
5623         if (rec->found_rec || rec->num_duplicates > 1)
5624                 return 0;
5625
5626         /* Shouldn't happen but just in case */
5627         BUG_ON(!rec->num_duplicates);
5628
5629         /*
5630          * So this happens if we end up with a backref that doesn't match the
5631          * actual extent entry.  So either the backref is bad or the extent
5632          * entry is bad.  Either way we want to have the extent_record actually
5633          * reflect what we found in the extent_tree, so we need to take the
5634          * duplicate out and use that as the extent_record since the only way we
5635          * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
5636          */
5637         remove_cache_extent(extent_cache, &rec->cache);
5638
5639         good = list_entry(rec->dups.next, struct extent_record, list);
5640         list_del_init(&good->list);
5641         INIT_LIST_HEAD(&good->backrefs);
5642         INIT_LIST_HEAD(&good->dups);
5643         good->cache.start = good->start;
5644         good->cache.size = good->nr;
5645         good->content_checked = 0;
5646         good->owner_ref_checked = 0;
5647         good->num_duplicates = 0;
5648         good->refs = rec->refs;
5649         list_splice_init(&rec->backrefs, &good->backrefs);
5650         while (1) {
5651                 cache = lookup_cache_extent(extent_cache, good->start,
5652                                             good->nr);
5653                 if (!cache)
5654                         break;
5655                 tmp = container_of(cache, struct extent_record, cache);
5656
5657                 /*
5658                  * If we find another overlapping extent and it's found_rec is
5659                  * set then it's a duplicate and we need to try and delete
5660                  * something.
5661                  */
5662                 if (tmp->found_rec || tmp->num_duplicates > 0) {
5663                         if (list_empty(&good->list))
5664                                 list_add_tail(&good->list,
5665                                               &duplicate_extents);
5666                         good->num_duplicates += tmp->num_duplicates + 1;
5667                         list_splice_init(&tmp->dups, &good->dups);
5668                         list_del_init(&tmp->list);
5669                         list_add_tail(&tmp->list, &good->dups);
5670                         remove_cache_extent(extent_cache, &tmp->cache);
5671                         continue;
5672                 }
5673
5674                 /*
5675                  * Ok we have another non extent item backed extent rec, so lets
5676                  * just add it to this extent and carry on like we did above.
5677                  */
5678                 good->refs += tmp->refs;
5679                 list_splice_init(&tmp->backrefs, &good->backrefs);
5680                 remove_cache_extent(extent_cache, &tmp->cache);
5681                 free(tmp);
5682         }
5683         ret = insert_cache_extent(extent_cache, &good->cache);
5684         BUG_ON(ret);
5685         free(rec);
5686         return good->num_duplicates ? 0 : 1;
5687 }
5688
5689 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
5690                                     struct btrfs_root *root,
5691                                     struct extent_record *rec)
5692 {
5693         LIST_HEAD(delete_list);
5694         struct btrfs_path *path;
5695         struct extent_record *tmp, *good, *n;
5696         int nr_del = 0;
5697         int ret = 0;
5698         struct btrfs_key key;
5699
5700         path = btrfs_alloc_path();
5701         if (!path) {
5702                 ret = -ENOMEM;
5703                 goto out;
5704         }
5705
5706         good = rec;
5707         /* Find the record that covers all of the duplicates. */
5708         list_for_each_entry(tmp, &rec->dups, list) {
5709                 if (good->start < tmp->start)
5710                         continue;
5711                 if (good->nr > tmp->nr)
5712                         continue;
5713
5714                 if (tmp->start + tmp->nr < good->start + good->nr) {
5715                         fprintf(stderr, "Ok we have overlapping extents that "
5716                                 "aren't completely covered by eachother, this "
5717                                 "is going to require more careful thought.  "
5718                                 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
5719                                 tmp->start, tmp->nr, good->start, good->nr);
5720                         abort();
5721                 }
5722                 good = tmp;
5723         }
5724
5725         if (good != rec)
5726                 list_add_tail(&rec->list, &delete_list);
5727
5728         list_for_each_entry_safe(tmp, n, &rec->dups, list) {
5729                 if (tmp == good)
5730                         continue;
5731                 list_move_tail(&tmp->list, &delete_list);
5732         }
5733
5734         root = root->fs_info->extent_root;
5735         list_for_each_entry(tmp, &delete_list, list) {
5736                 if (tmp->found_rec == 0)
5737                         continue;
5738                 key.objectid = tmp->start;
5739                 key.type = BTRFS_EXTENT_ITEM_KEY;
5740                 key.offset = tmp->nr;
5741
5742                 /* Shouldn't happen but just in case */
5743                 if (tmp->metadata) {
5744                         fprintf(stderr, "Well this shouldn't happen, extent "
5745                                 "record overlaps but is metadata? "
5746                                 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
5747                         abort();
5748                 }
5749
5750                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5751                 if (ret) {
5752                         if (ret > 0)
5753                                 ret = -EINVAL;
5754                         goto out;
5755                 }
5756                 ret = btrfs_del_item(trans, root, path);
5757                 if (ret)
5758                         goto out;
5759                 btrfs_release_path(path);
5760                 nr_del++;
5761         }
5762
5763 out:
5764         while (!list_empty(&delete_list)) {
5765                 tmp = list_entry(delete_list.next, struct extent_record, list);
5766                 list_del_init(&tmp->list);
5767                 if (tmp == rec)
5768                         continue;
5769                 free(tmp);
5770         }
5771
5772         while (!list_empty(&rec->dups)) {
5773                 tmp = list_entry(rec->dups.next, struct extent_record, list);
5774                 list_del_init(&tmp->list);
5775                 free(tmp);
5776         }
5777
5778         btrfs_free_path(path);
5779
5780         if (!ret && !nr_del)
5781                 rec->num_duplicates = 0;
5782
5783         return ret ? ret : nr_del;
5784 }
5785
5786 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5787                                   struct btrfs_fs_info *info,
5788                                   struct btrfs_path *path,
5789                                   struct cache_tree *extent_cache,
5790                                   struct extent_record *rec)
5791 {
5792         struct btrfs_root *root;
5793         struct extent_backref *back;
5794         struct data_backref *dback;
5795         struct cache_extent *cache;
5796         struct btrfs_file_extent_item *fi;
5797         struct btrfs_key key;
5798         u64 bytenr, bytes;
5799         int ret;
5800
5801         list_for_each_entry(back, &rec->backrefs, list) {
5802                 /* Don't care about full backrefs (poor unloved backrefs) */
5803                 if (back->full_backref || !back->is_data)
5804                         continue;
5805
5806                 dback = (struct data_backref *)back;
5807
5808                 /* We found this one, we don't need to do a lookup */
5809                 if (dback->found_ref)
5810                         continue;
5811
5812                 key.objectid = dback->root;
5813                 key.type = BTRFS_ROOT_ITEM_KEY;
5814                 key.offset = (u64)-1;
5815
5816                 root = btrfs_read_fs_root(info, &key);
5817
5818                 /* No root, definitely a bad ref, skip */
5819                 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5820                         continue;
5821                 /* Other err, exit */
5822                 if (IS_ERR(root))
5823                         return PTR_ERR(root);
5824
5825                 key.objectid = dback->owner;
5826                 key.type = BTRFS_EXTENT_DATA_KEY;
5827                 key.offset = dback->offset;
5828                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5829                 if (ret) {
5830                         btrfs_release_path(path);
5831                         if (ret < 0)
5832                                 return ret;
5833                         /* Didn't find it, we can carry on */
5834                         ret = 0;
5835                         continue;
5836                 }
5837
5838                 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5839                                     struct btrfs_file_extent_item);
5840                 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5841                 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5842                 btrfs_release_path(path);
5843                 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5844                 if (cache) {
5845                         struct extent_record *tmp;
5846                         tmp = container_of(cache, struct extent_record, cache);
5847
5848                         /*
5849                          * If we found an extent record for the bytenr for this
5850                          * particular backref then we can't add it to our
5851                          * current extent record.  We only want to add backrefs
5852                          * that don't have a corresponding extent item in the
5853                          * extent tree since they likely belong to this record
5854                          * and we need to fix it if it doesn't match bytenrs.
5855                          */
5856                         if  (tmp->found_rec)
5857                                 continue;
5858                 }
5859
5860                 dback->found_ref += 1;
5861                 dback->disk_bytenr = bytenr;
5862                 dback->bytes = bytes;
5863
5864                 /*
5865                  * Set this so the verify backref code knows not to trust the
5866                  * values in this backref.
5867                  */
5868                 back->broken = 1;
5869         }
5870
5871         return 0;
5872 }
5873
5874 /*
5875  * when an incorrect extent item is found, this will delete
5876  * all of the existing entries for it and recreate them
5877  * based on what the tree scan found.
5878  */
5879 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5880                              struct btrfs_fs_info *info,
5881                              struct cache_tree *extent_cache,
5882                              struct extent_record *rec)
5883 {
5884         int ret;
5885         struct btrfs_path *path;
5886         struct list_head *cur = rec->backrefs.next;
5887         struct cache_extent *cache;
5888         struct extent_backref *back;
5889         int allocated = 0;
5890         u64 flags = 0;
5891
5892         /*
5893          * remember our flags for recreating the extent.
5894          * FIXME, if we have cleared extent tree, we can not
5895          * lookup extent info in extent tree.
5896          */
5897         if (!init_extent_tree) {
5898                 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5899                                         rec->start, rec->max_size,
5900                                         rec->metadata, NULL, &flags);
5901                 if (ret < 0)
5902                         flags = 0;
5903         } else {
5904                 flags = 0;
5905         }
5906
5907         path = btrfs_alloc_path();
5908         if (!path)
5909                 return -ENOMEM;
5910
5911         if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5912                 /*
5913                  * Sometimes the backrefs themselves are so broken they don't
5914                  * get attached to any meaningful rec, so first go back and
5915                  * check any of our backrefs that we couldn't find and throw
5916                  * them into the list if we find the backref so that
5917                  * verify_backrefs can figure out what to do.
5918                  */
5919                 ret = find_possible_backrefs(trans, info, path, extent_cache,
5920                                              rec);
5921                 if (ret < 0)
5922                         goto out;
5923         }
5924
5925         /* step one, make sure all of the backrefs agree */
5926         ret = verify_backrefs(trans, info, path, rec);
5927         if (ret < 0)
5928                 goto out;
5929
5930         /* step two, delete all the existing records */
5931         ret = delete_extent_records(trans, info->extent_root, path,
5932                                     rec->start, rec->max_size);
5933
5934         if (ret < 0)
5935                 goto out;
5936
5937         /* was this block corrupt?  If so, don't add references to it */
5938         cache = lookup_cache_extent(info->corrupt_blocks,
5939                                     rec->start, rec->max_size);
5940         if (cache) {
5941                 ret = 0;
5942                 goto out;
5943         }
5944
5945         /* step three, recreate all the refs we did find */
5946         while(cur != &rec->backrefs) {
5947                 back = list_entry(cur, struct extent_backref, list);
5948                 cur = cur->next;
5949
5950                 /*
5951                  * if we didn't find any references, don't create a
5952                  * new extent record
5953                  */
5954                 if (!back->found_ref)
5955                         continue;
5956
5957                 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5958                 allocated = 1;
5959
5960                 if (ret)
5961                         goto out;
5962         }
5963 out:
5964         btrfs_free_path(path);
5965         return ret;
5966 }
5967
5968 /* right now we only prune from the extent allocation tree */
5969 static int prune_one_block(struct btrfs_trans_handle *trans,
5970                            struct btrfs_fs_info *info,
5971                            struct btrfs_corrupt_block *corrupt)
5972 {
5973         int ret;
5974         struct btrfs_path path;
5975         struct extent_buffer *eb;
5976         u64 found;
5977         int slot;
5978         int nritems;
5979         int level = corrupt->level + 1;
5980
5981         btrfs_init_path(&path);
5982 again:
5983         /* we want to stop at the parent to our busted block */
5984         path.lowest_level = level;
5985
5986         ret = btrfs_search_slot(trans, info->extent_root,
5987                                 &corrupt->key, &path, -1, 1);
5988
5989         if (ret < 0)
5990                 goto out;
5991
5992         eb = path.nodes[level];
5993         if (!eb) {
5994                 ret = -ENOENT;
5995                 goto out;
5996         }
5997
5998         /*
5999          * hopefully the search gave us the block we want to prune,
6000          * lets try that first
6001          */
6002         slot = path.slots[level];
6003         found =  btrfs_node_blockptr(eb, slot);
6004         if (found == corrupt->cache.start)
6005                 goto del_ptr;
6006
6007         nritems = btrfs_header_nritems(eb);
6008
6009         /* the search failed, lets scan this node and hope we find it */
6010         for (slot = 0; slot < nritems; slot++) {
6011                 found =  btrfs_node_blockptr(eb, slot);
6012                 if (found == corrupt->cache.start)
6013                         goto del_ptr;
6014         }
6015         /*
6016          * we couldn't find the bad block.  TODO, search all the nodes for pointers
6017          * to this block
6018          */
6019         if (eb == info->extent_root->node) {
6020                 ret = -ENOENT;
6021                 goto out;
6022         } else {
6023                 level++;
6024                 btrfs_release_path(&path);
6025                 goto again;
6026         }
6027
6028 del_ptr:
6029         printk("deleting pointer to block %Lu\n", corrupt->cache.start);
6030         ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
6031
6032 out:
6033         btrfs_release_path(&path);
6034         return ret;
6035 }
6036
6037 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
6038                                 struct btrfs_fs_info *info)
6039 {
6040         struct cache_extent *cache;
6041         struct btrfs_corrupt_block *corrupt;
6042
6043         cache = search_cache_extent(info->corrupt_blocks, 0);
6044         while (1) {
6045                 if (!cache)
6046                         break;
6047                 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
6048                 prune_one_block(trans, info, corrupt);
6049                 cache = next_cache_extent(cache);
6050         }
6051         return 0;
6052 }
6053
6054 static void free_corrupt_block(struct cache_extent *cache)
6055 {
6056         struct btrfs_corrupt_block *corrupt;
6057
6058         corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
6059         free(corrupt);
6060 }
6061
6062 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
6063
6064 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
6065 {
6066         struct btrfs_block_group_cache *cache;
6067         u64 start, end;
6068         int ret;
6069
6070         while (1) {
6071                 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
6072                                             &start, &end, EXTENT_DIRTY);
6073                 if (ret)
6074                         break;
6075                 clear_extent_dirty(&fs_info->free_space_cache, start, end,
6076                                    GFP_NOFS);
6077         }
6078
6079         start = 0;
6080         while (1) {
6081                 cache = btrfs_lookup_first_block_group(fs_info, start);
6082                 if (!cache)
6083                         break;
6084                 if (cache->cached)
6085                         cache->cached = 0;
6086                 start = cache->key.objectid + cache->key.offset;
6087         }
6088 }
6089
6090 static int check_extent_refs(struct btrfs_trans_handle *trans,
6091                              struct btrfs_root *root,
6092                              struct cache_tree *extent_cache)
6093 {
6094         struct extent_record *rec;
6095         struct cache_extent *cache;
6096         int err = 0;
6097         int ret = 0;
6098         int fixed = 0;
6099         int had_dups = 0;
6100
6101         if (repair) {
6102                 /*
6103                  * if we're doing a repair, we have to make sure
6104                  * we don't allocate from the problem extents.
6105                  * In the worst case, this will be all the
6106                  * extents in the FS
6107                  */
6108                 cache = search_cache_extent(extent_cache, 0);
6109                 while(cache) {
6110                         rec = container_of(cache, struct extent_record, cache);
6111                         btrfs_pin_extent(root->fs_info,
6112                                          rec->start, rec->max_size);
6113                         cache = next_cache_extent(cache);
6114                 }
6115
6116                 /* pin down all the corrupted blocks too */
6117                 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
6118                 while(cache) {
6119                         btrfs_pin_extent(root->fs_info,
6120                                          cache->start, cache->size);
6121                         cache = next_cache_extent(cache);
6122                 }
6123                 prune_corrupt_blocks(trans, root->fs_info);
6124                 reset_cached_block_groups(root->fs_info);
6125         }
6126
6127         /*
6128          * We need to delete any duplicate entries we find first otherwise we
6129          * could mess up the extent tree when we have backrefs that actually
6130          * belong to a different extent item and not the weird duplicate one.
6131          */
6132         while (repair && !list_empty(&duplicate_extents)) {
6133                 rec = list_entry(duplicate_extents.next, struct extent_record,
6134                                  list);
6135                 list_del_init(&rec->list);
6136
6137                 /* Sometimes we can find a backref before we find an actual
6138                  * extent, so we need to process it a little bit to see if there
6139                  * truly are multiple EXTENT_ITEM_KEY's for the same range, or
6140                  * if this is a backref screwup.  If we need to delete stuff
6141                  * process_duplicates() will return 0, otherwise it will return
6142                  * 1 and we
6143                  */
6144                 if (process_duplicates(root, extent_cache, rec))
6145                         continue;
6146                 ret = delete_duplicate_records(trans, root, rec);
6147                 if (ret < 0)
6148                         return ret;
6149                 /*
6150                  * delete_duplicate_records will return the number of entries
6151                  * deleted, so if it's greater than 0 then we know we actually
6152                  * did something and we need to remove.
6153                  */
6154                 if (ret)
6155                         had_dups = 1;
6156         }
6157
6158         if (had_dups)
6159                 return -EAGAIN;
6160
6161         while(1) {
6162                 fixed = 0;
6163                 cache = search_cache_extent(extent_cache, 0);
6164                 if (!cache)
6165                         break;
6166                 rec = container_of(cache, struct extent_record, cache);
6167                 if (rec->num_duplicates) {
6168                         fprintf(stderr, "extent item %llu has multiple extent "
6169                                 "items\n", (unsigned long long)rec->start);
6170                         err = 1;
6171                 }
6172
6173                 if (rec->refs != rec->extent_item_refs) {
6174                         fprintf(stderr, "ref mismatch on [%llu %llu] ",
6175                                 (unsigned long long)rec->start,
6176                                 (unsigned long long)rec->nr);
6177                         fprintf(stderr, "extent item %llu, found %llu\n",
6178                                 (unsigned long long)rec->extent_item_refs,
6179                                 (unsigned long long)rec->refs);
6180                         if (!fixed && repair) {
6181                                 ret = fixup_extent_refs(trans, root->fs_info,
6182                                                         extent_cache, rec);
6183                                 if (ret)
6184                                         goto repair_abort;
6185                                 fixed = 1;
6186                         }
6187                         err = 1;
6188
6189                 }
6190                 if (all_backpointers_checked(rec, 1)) {
6191                         fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
6192                                 (unsigned long long)rec->start,
6193                                 (unsigned long long)rec->nr);
6194
6195                         if (!fixed && repair) {
6196                                 ret = fixup_extent_refs(trans, root->fs_info,
6197                                                         extent_cache, rec);
6198                                 if (ret)
6199                                         goto repair_abort;
6200                                 fixed = 1;
6201                         }
6202
6203                         err = 1;
6204                 }
6205                 if (!rec->owner_ref_checked) {
6206                         fprintf(stderr, "owner ref check failed [%llu %llu]\n",
6207                                 (unsigned long long)rec->start,
6208                                 (unsigned long long)rec->nr);
6209                         if (!fixed && repair) {
6210                                 ret = fixup_extent_refs(trans, root->fs_info,
6211                                                         extent_cache, rec);
6212                                 if (ret)
6213                                         goto repair_abort;
6214                                 fixed = 1;
6215                         }
6216                         err = 1;
6217                 }
6218
6219                 remove_cache_extent(extent_cache, cache);
6220                 free_all_extent_backrefs(rec);
6221                 free(rec);
6222         }
6223 repair_abort:
6224         if (repair) {
6225                 if (ret && ret != -EAGAIN) {
6226                         fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
6227                         exit(1);
6228                 } else if (!ret) {
6229                         btrfs_fix_block_accounting(trans, root);
6230                 }
6231                 if (err)
6232                         fprintf(stderr, "repaired damaged extent references\n");
6233                 return ret;
6234         }
6235         return err;
6236 }
6237
6238 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
6239 {
6240         u64 stripe_size;
6241
6242         if (type & BTRFS_BLOCK_GROUP_RAID0) {
6243                 stripe_size = length;
6244                 stripe_size /= num_stripes;
6245         } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6246                 stripe_size = length * 2;
6247                 stripe_size /= num_stripes;
6248         } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
6249                 stripe_size = length;
6250                 stripe_size /= (num_stripes - 1);
6251         } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
6252                 stripe_size = length;
6253                 stripe_size /= (num_stripes - 2);
6254         } else {
6255                 stripe_size = length;
6256         }
6257         return stripe_size;
6258 }
6259
6260 static int check_chunk_refs(struct chunk_record *chunk_rec,
6261                             struct block_group_tree *block_group_cache,
6262                             struct device_extent_tree *dev_extent_cache,
6263                             int silent)
6264 {
6265         struct cache_extent *block_group_item;
6266         struct block_group_record *block_group_rec;
6267         struct cache_extent *dev_extent_item;
6268         struct device_extent_record *dev_extent_rec;
6269         u64 devid;
6270         u64 offset;
6271         u64 length;
6272         int i;
6273         int ret = 0;
6274
6275         block_group_item = lookup_cache_extent(&block_group_cache->tree,
6276                                                chunk_rec->offset,
6277                                                chunk_rec->length);
6278         if (block_group_item) {
6279                 block_group_rec = container_of(block_group_item,
6280                                                struct block_group_record,
6281                                                cache);
6282                 if (chunk_rec->length != block_group_rec->offset ||
6283                     chunk_rec->offset != block_group_rec->objectid ||
6284                     chunk_rec->type_flags != block_group_rec->flags) {
6285                         if (!silent)
6286                                 fprintf(stderr,
6287                                         "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
6288                                         chunk_rec->objectid,
6289                                         chunk_rec->type,
6290                                         chunk_rec->offset,
6291                                         chunk_rec->length,
6292                                         chunk_rec->offset,
6293                                         chunk_rec->type_flags,
6294                                         block_group_rec->objectid,
6295                                         block_group_rec->type,
6296                                         block_group_rec->offset,
6297                                         block_group_rec->offset,
6298                                         block_group_rec->objectid,
6299                                         block_group_rec->flags);
6300                         ret = -1;
6301                 } else {
6302                         list_del_init(&block_group_rec->list);
6303                         chunk_rec->bg_rec = block_group_rec;
6304                 }
6305         } else {
6306                 if (!silent)
6307                         fprintf(stderr,
6308                                 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
6309                                 chunk_rec->objectid,
6310                                 chunk_rec->type,
6311                                 chunk_rec->offset,
6312                                 chunk_rec->length,
6313                                 chunk_rec->offset,
6314                                 chunk_rec->type_flags);
6315                 ret = -1;
6316         }
6317
6318         length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
6319                                     chunk_rec->num_stripes);
6320         for (i = 0; i < chunk_rec->num_stripes; ++i) {
6321                 devid = chunk_rec->stripes[i].devid;
6322                 offset = chunk_rec->stripes[i].offset;
6323                 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
6324                                                        devid, offset, length);
6325                 if (dev_extent_item) {
6326                         dev_extent_rec = container_of(dev_extent_item,
6327                                                 struct device_extent_record,
6328                                                 cache);
6329                         if (dev_extent_rec->objectid != devid ||
6330                             dev_extent_rec->offset != offset ||
6331                             dev_extent_rec->chunk_offset != chunk_rec->offset ||
6332                             dev_extent_rec->length != length) {
6333                                 if (!silent)
6334                                         fprintf(stderr,
6335                                                 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
6336                                                 chunk_rec->objectid,
6337                                                 chunk_rec->type,
6338                                                 chunk_rec->offset,
6339                                                 chunk_rec->stripes[i].devid,
6340                                                 chunk_rec->stripes[i].offset,
6341                                                 dev_extent_rec->objectid,
6342                                                 dev_extent_rec->offset,
6343                                                 dev_extent_rec->length);
6344                                 ret = -1;
6345                         } else {
6346                                 list_move(&dev_extent_rec->chunk_list,
6347                                           &chunk_rec->dextents);
6348                         }
6349                 } else {
6350                         if (!silent)
6351                                 fprintf(stderr,
6352                                         "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
6353                                         chunk_rec->objectid,
6354                                         chunk_rec->type,
6355                                         chunk_rec->offset,
6356                                         chunk_rec->stripes[i].devid,
6357                                         chunk_rec->stripes[i].offset);
6358                         ret = -1;
6359                 }
6360         }
6361         return ret;
6362 }
6363
6364 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
6365 int check_chunks(struct cache_tree *chunk_cache,
6366                  struct block_group_tree *block_group_cache,
6367                  struct device_extent_tree *dev_extent_cache,
6368                  struct list_head *good, struct list_head *bad, int silent)
6369 {
6370         struct cache_extent *chunk_item;
6371         struct chunk_record *chunk_rec;
6372         struct block_group_record *bg_rec;
6373         struct device_extent_record *dext_rec;
6374         int err;
6375         int ret = 0;
6376
6377         chunk_item = first_cache_extent(chunk_cache);
6378         while (chunk_item) {
6379                 chunk_rec = container_of(chunk_item, struct chunk_record,
6380                                          cache);
6381                 err = check_chunk_refs(chunk_rec, block_group_cache,
6382                                        dev_extent_cache, silent);
6383                 if (err) {
6384                         ret = err;
6385                         if (bad)
6386                                 list_add_tail(&chunk_rec->list, bad);
6387                 } else {
6388                         if (good)
6389                                 list_add_tail(&chunk_rec->list, good);
6390                 }
6391
6392                 chunk_item = next_cache_extent(chunk_item);
6393         }
6394
6395         list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
6396                 if (!silent)
6397                         fprintf(stderr,
6398                                 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
6399                                 bg_rec->objectid,
6400                                 bg_rec->offset,
6401                                 bg_rec->flags);
6402                 if (!ret)
6403                         ret = 1;
6404         }
6405
6406         list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
6407                             chunk_list) {
6408                 if (!silent)
6409                         fprintf(stderr,
6410                                 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
6411                                 dext_rec->objectid,
6412                                 dext_rec->offset,
6413                                 dext_rec->length);
6414                 if (!ret)
6415                         ret = 1;
6416         }
6417         return ret;
6418 }
6419
6420
6421 static int check_device_used(struct device_record *dev_rec,
6422                              struct device_extent_tree *dext_cache)
6423 {
6424         struct cache_extent *cache;
6425         struct device_extent_record *dev_extent_rec;
6426         u64 total_byte = 0;
6427
6428         cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
6429         while (cache) {
6430                 dev_extent_rec = container_of(cache,
6431                                               struct device_extent_record,
6432                                               cache);
6433                 if (dev_extent_rec->objectid != dev_rec->devid)
6434                         break;
6435
6436                 list_del_init(&dev_extent_rec->device_list);
6437                 total_byte += dev_extent_rec->length;
6438                 cache = next_cache_extent(cache);
6439         }
6440
6441         if (total_byte != dev_rec->byte_used) {
6442                 fprintf(stderr,
6443                         "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
6444                         total_byte, dev_rec->byte_used, dev_rec->objectid,
6445                         dev_rec->type, dev_rec->offset);
6446                 return -1;
6447         } else {
6448                 return 0;
6449         }
6450 }
6451
6452 /* check btrfs_dev_item -> btrfs_dev_extent */
6453 static int check_devices(struct rb_root *dev_cache,
6454                          struct device_extent_tree *dev_extent_cache)
6455 {
6456         struct rb_node *dev_node;
6457         struct device_record *dev_rec;
6458         struct device_extent_record *dext_rec;
6459         int err;
6460         int ret = 0;
6461
6462         dev_node = rb_first(dev_cache);
6463         while (dev_node) {
6464                 dev_rec = container_of(dev_node, struct device_record, node);
6465                 err = check_device_used(dev_rec, dev_extent_cache);
6466                 if (err)
6467                         ret = err;
6468
6469                 dev_node = rb_next(dev_node);
6470         }
6471         list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
6472                             device_list) {
6473                 fprintf(stderr,
6474                         "Device extent[%llu, %llu, %llu] didn't find its device.\n",
6475                         dext_rec->objectid, dext_rec->offset, dext_rec->length);
6476                 if (!ret)
6477                         ret = 1;
6478         }
6479         return ret;
6480 }
6481
6482 static int check_chunks_and_extents(struct btrfs_root *root)
6483 {
6484         struct rb_root dev_cache;
6485         struct cache_tree chunk_cache;
6486         struct block_group_tree block_group_cache;
6487         struct device_extent_tree dev_extent_cache;
6488         struct cache_tree extent_cache;
6489         struct cache_tree seen;
6490         struct cache_tree pending;
6491         struct cache_tree reada;
6492         struct cache_tree nodes;
6493         struct cache_tree corrupt_blocks;
6494         struct btrfs_path path;
6495         struct btrfs_key key;
6496         struct btrfs_key found_key;
6497         int ret, err = 0;
6498         u64 last = 0;
6499         struct block_info *bits;
6500         int bits_nr;
6501         struct extent_buffer *leaf;
6502         struct btrfs_trans_handle *trans = NULL;
6503         int slot;
6504         struct btrfs_root_item ri;
6505         struct list_head dropping_trees;
6506
6507         dev_cache = RB_ROOT;
6508         cache_tree_init(&chunk_cache);
6509         block_group_tree_init(&block_group_cache);
6510         device_extent_tree_init(&dev_extent_cache);
6511
6512         cache_tree_init(&extent_cache);
6513         cache_tree_init(&seen);
6514         cache_tree_init(&pending);
6515         cache_tree_init(&nodes);
6516         cache_tree_init(&reada);
6517         cache_tree_init(&corrupt_blocks);
6518         INIT_LIST_HEAD(&dropping_trees);
6519
6520         if (repair) {
6521                 trans = btrfs_start_transaction(root, 1);
6522                 if (IS_ERR(trans)) {
6523                         fprintf(stderr, "Error starting transaction\n");
6524                         return PTR_ERR(trans);
6525                 }
6526                 root->fs_info->fsck_extent_cache = &extent_cache;
6527                 root->fs_info->free_extent_hook = free_extent_hook;
6528                 root->fs_info->corrupt_blocks = &corrupt_blocks;
6529         }
6530
6531         bits_nr = 1024;
6532         bits = malloc(bits_nr * sizeof(struct block_info));
6533         if (!bits) {
6534                 perror("malloc");
6535                 exit(1);
6536         }
6537
6538 again:
6539         add_root_to_pending(root->fs_info->tree_root->node,
6540                             &extent_cache, &pending, &seen, &nodes,
6541                             &root->fs_info->tree_root->root_key);
6542
6543         add_root_to_pending(root->fs_info->chunk_root->node,
6544                             &extent_cache, &pending, &seen, &nodes,
6545                             &root->fs_info->chunk_root->root_key);
6546
6547         btrfs_init_path(&path);
6548         key.offset = 0;
6549         key.objectid = 0;
6550         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
6551         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
6552                                         &key, &path, 0, 0);
6553         if (ret < 0)
6554                 goto out;
6555         while(1) {
6556                 leaf = path.nodes[0];
6557                 slot = path.slots[0];
6558                 if (slot >= btrfs_header_nritems(path.nodes[0])) {
6559                         ret = btrfs_next_leaf(root, &path);
6560                         if (ret != 0)
6561                                 break;
6562                         leaf = path.nodes[0];
6563                         slot = path.slots[0];
6564                 }
6565                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
6566                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
6567                         unsigned long offset;
6568                         struct extent_buffer *buf;
6569
6570                         offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
6571                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
6572                         if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
6573                                 buf = read_tree_block(root->fs_info->tree_root,
6574                                                       btrfs_root_bytenr(&ri),
6575                                                       btrfs_level_size(root,
6576                                                       btrfs_root_level(&ri)),
6577                                                       0);
6578                                 if (!buf) {
6579                                         ret = -EIO;
6580                                         goto out;
6581                                 }
6582                                 add_root_to_pending(buf, &extent_cache,
6583                                                     &pending, &seen, &nodes,
6584                                                     &found_key);
6585                                 free_extent_buffer(buf);
6586                         } else {
6587                                 struct dropping_root_item_record *dri_rec;
6588                                 dri_rec = malloc(sizeof(*dri_rec));
6589                                 if (!dri_rec) {
6590                                         perror("malloc");
6591                                         exit(1);
6592                                 }
6593                                 memcpy(&dri_rec->ri, &ri, sizeof(ri));
6594                                 memcpy(&dri_rec->found_key, &found_key,
6595                                        sizeof(found_key));
6596                                 list_add_tail(&dri_rec->list, &dropping_trees);
6597                         }
6598                 }
6599                 path.slots[0]++;
6600         }
6601         btrfs_release_path(&path);
6602         while (1) {
6603                 ret = run_next_block(trans, root, bits, bits_nr, &last,
6604                                      &pending, &seen, &reada, &nodes,
6605                                      &extent_cache, &chunk_cache, &dev_cache,
6606                                      &block_group_cache, &dev_extent_cache,
6607                                      NULL);
6608                 if (ret != 0)
6609                         break;
6610         }
6611
6612         while (!list_empty(&dropping_trees)) {
6613                 struct dropping_root_item_record *rec;
6614                 struct extent_buffer *buf;
6615                 rec = list_entry(dropping_trees.next,
6616                                  struct dropping_root_item_record, list);
6617                 last = 0;
6618                 if (!bits) {
6619                         perror("realloc");
6620                         exit(1);
6621                 }
6622                 buf = read_tree_block(root->fs_info->tree_root,
6623                                       btrfs_root_bytenr(&rec->ri),
6624                                       btrfs_level_size(root,
6625                                       btrfs_root_level(&rec->ri)), 0);
6626                 if (!buf) {
6627                         ret = -EIO;
6628                         goto out;
6629                 }
6630                 add_root_to_pending(buf, &extent_cache, &pending,
6631                                     &seen, &nodes, &rec->found_key);
6632                 while (1) {
6633                         ret = run_next_block(trans, root, bits, bits_nr, &last,
6634                                              &pending, &seen, &reada,
6635                                              &nodes, &extent_cache,
6636                                              &chunk_cache, &dev_cache,
6637                                              &block_group_cache,
6638                                              &dev_extent_cache,
6639                                              &rec->ri);
6640                         if (ret != 0)
6641                                 break;
6642                 }
6643                 free_extent_buffer(buf);
6644                 list_del(&rec->list);
6645                 free(rec);
6646         }
6647
6648         if (ret >= 0)
6649                 ret = check_extent_refs(trans, root, &extent_cache);
6650         if (ret == -EAGAIN) {
6651                 ret = btrfs_commit_transaction(trans, root);
6652                 if (ret)
6653                         goto out;
6654
6655                 trans = btrfs_start_transaction(root, 1);
6656                 if (IS_ERR(trans)) {
6657                         ret = PTR_ERR(trans);
6658                         goto out;
6659                 }
6660
6661                 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6662                 free_extent_cache_tree(&seen);
6663                 free_extent_cache_tree(&pending);
6664                 free_extent_cache_tree(&reada);
6665                 free_extent_cache_tree(&nodes);
6666                 free_chunk_cache_tree(&chunk_cache);
6667                 free_block_group_tree(&block_group_cache);
6668                 free_device_cache_tree(&dev_cache);
6669                 free_device_extent_tree(&dev_extent_cache);
6670                 free_extent_record_cache(root->fs_info, &extent_cache);
6671                 goto again;
6672         }
6673
6674         err = check_chunks(&chunk_cache, &block_group_cache,
6675                            &dev_extent_cache, NULL, NULL, 0);
6676         if (err && !ret)
6677                 ret = err;
6678
6679         err = check_devices(&dev_cache, &dev_extent_cache);
6680         if (err && !ret)
6681                 ret = err;
6682
6683 out:
6684         if (trans) {
6685                 err = btrfs_commit_transaction(trans, root);
6686                 if (!ret)
6687                         ret = err;
6688         }
6689         if (repair) {
6690                 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6691                 root->fs_info->fsck_extent_cache = NULL;
6692                 root->fs_info->free_extent_hook = NULL;
6693                 root->fs_info->corrupt_blocks = NULL;
6694         }
6695         free(bits);
6696         free_chunk_cache_tree(&chunk_cache);
6697         free_device_cache_tree(&dev_cache);
6698         free_block_group_tree(&block_group_cache);
6699         free_device_extent_tree(&dev_extent_cache);
6700         free_extent_cache_tree(&seen);
6701         free_extent_cache_tree(&pending);
6702         free_extent_cache_tree(&reada);
6703         free_extent_cache_tree(&nodes);
6704         return ret;
6705 }
6706
6707 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
6708                            struct btrfs_root *root, int overwrite)
6709 {
6710         struct extent_buffer *c;
6711         struct extent_buffer *old = root->node;
6712         int level;
6713         int ret;
6714         struct btrfs_disk_key disk_key = {0,0,0};
6715
6716         level = 0;
6717
6718         if (overwrite) {
6719                 c = old;
6720                 extent_buffer_get(c);
6721                 goto init;
6722         }
6723         c = btrfs_alloc_free_block(trans, root,
6724                                    btrfs_level_size(root, 0),
6725                                    root->root_key.objectid,
6726                                    &disk_key, level, 0, 0);
6727         if (IS_ERR(c)) {
6728                 c = old;
6729                 extent_buffer_get(c);
6730                 overwrite = 1;
6731         }
6732 init:
6733         memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
6734         btrfs_set_header_level(c, level);
6735         btrfs_set_header_bytenr(c, c->start);
6736         btrfs_set_header_generation(c, trans->transid);
6737         btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
6738         btrfs_set_header_owner(c, root->root_key.objectid);
6739
6740         write_extent_buffer(c, root->fs_info->fsid,
6741                             btrfs_header_fsid(), BTRFS_FSID_SIZE);
6742
6743         write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
6744                             btrfs_header_chunk_tree_uuid(c),
6745                             BTRFS_UUID_SIZE);
6746
6747         btrfs_mark_buffer_dirty(c);
6748         /*
6749          * this case can happen in the following case:
6750          *
6751          * 1.overwrite previous root.
6752          *
6753          * 2.reinit reloc data root, this is because we skip pin
6754          * down reloc data tree before which means we can allocate
6755          * same block bytenr here.
6756          */
6757         if (old->start == c->start) {
6758                 btrfs_set_root_generation(&root->root_item,
6759                                           trans->transid);
6760                 root->root_item.level = btrfs_header_level(root->node);
6761                 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6762                                         &root->root_key, &root->root_item);
6763                 if (ret) {
6764                         free_extent_buffer(c);
6765                         return ret;
6766                 }
6767         }
6768         free_extent_buffer(old);
6769         root->node = c;
6770         add_root_to_dirty_list(root);
6771         return 0;
6772 }
6773
6774 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
6775                                 struct extent_buffer *eb, int tree_root)
6776 {
6777         struct extent_buffer *tmp;
6778         struct btrfs_root_item *ri;
6779         struct btrfs_key key;
6780         u64 bytenr;
6781         u32 leafsize;
6782         int level = btrfs_header_level(eb);
6783         int nritems;
6784         int ret;
6785         int i;
6786
6787         /*
6788          * If we have pinned this block before, don't pin it again.
6789          * This can not only avoid forever loop with broken filesystem
6790          * but also give us some speedups.
6791          */
6792         if (test_range_bit(&fs_info->pinned_extents, eb->start,
6793                            eb->start + eb->len - 1, EXTENT_DIRTY, 0))
6794                 return 0;
6795
6796         btrfs_pin_extent(fs_info, eb->start, eb->len);
6797
6798         leafsize = btrfs_super_leafsize(fs_info->super_copy);
6799         nritems = btrfs_header_nritems(eb);
6800         for (i = 0; i < nritems; i++) {
6801                 if (level == 0) {
6802                         btrfs_item_key_to_cpu(eb, &key, i);
6803                         if (key.type != BTRFS_ROOT_ITEM_KEY)
6804                                 continue;
6805                         /* Skip the extent root and reloc roots */
6806                         if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6807                             key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6808                             key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6809                                 continue;
6810                         ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6811                         bytenr = btrfs_disk_root_bytenr(eb, ri);
6812
6813                         /*
6814                          * If at any point we start needing the real root we
6815                          * will have to build a stump root for the root we are
6816                          * in, but for now this doesn't actually use the root so
6817                          * just pass in extent_root.
6818                          */
6819                         tmp = read_tree_block(fs_info->extent_root, bytenr,
6820                                               leafsize, 0);
6821                         if (!tmp) {
6822                                 fprintf(stderr, "Error reading root block\n");
6823                                 return -EIO;
6824                         }
6825                         ret = pin_down_tree_blocks(fs_info, tmp, 0);
6826                         free_extent_buffer(tmp);
6827                         if (ret)
6828                                 return ret;
6829                 } else {
6830                         bytenr = btrfs_node_blockptr(eb, i);
6831
6832                         /* If we aren't the tree root don't read the block */
6833                         if (level == 1 && !tree_root) {
6834                                 btrfs_pin_extent(fs_info, bytenr, leafsize);
6835                                 continue;
6836                         }
6837
6838                         tmp = read_tree_block(fs_info->extent_root, bytenr,
6839                                               leafsize, 0);
6840                         if (!tmp) {
6841                                 fprintf(stderr, "Error reading tree block\n");
6842                                 return -EIO;
6843                         }
6844                         ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6845                         free_extent_buffer(tmp);
6846                         if (ret)
6847                                 return ret;
6848                 }
6849         }
6850
6851         return 0;
6852 }
6853
6854 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6855 {
6856         int ret;
6857
6858         ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6859         if (ret)
6860                 return ret;
6861
6862         return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6863 }
6864
6865 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6866 {
6867         struct btrfs_block_group_cache *cache;
6868         struct btrfs_path *path;
6869         struct extent_buffer *leaf;
6870         struct btrfs_chunk *chunk;
6871         struct btrfs_key key;
6872         int ret;
6873         u64 start;
6874
6875         path = btrfs_alloc_path();
6876         if (!path)
6877                 return -ENOMEM;
6878
6879         key.objectid = 0;
6880         key.type = BTRFS_CHUNK_ITEM_KEY;
6881         key.offset = 0;
6882
6883         ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6884         if (ret < 0) {
6885                 btrfs_free_path(path);
6886                 return ret;
6887         }
6888
6889         /*
6890          * We do this in case the block groups were screwed up and had alloc
6891          * bits that aren't actually set on the chunks.  This happens with
6892          * restored images every time and could happen in real life I guess.
6893          */
6894         fs_info->avail_data_alloc_bits = 0;
6895         fs_info->avail_metadata_alloc_bits = 0;
6896         fs_info->avail_system_alloc_bits = 0;
6897
6898         /* First we need to create the in-memory block groups */
6899         while (1) {
6900                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6901                         ret = btrfs_next_leaf(fs_info->chunk_root, path);
6902                         if (ret < 0) {
6903                                 btrfs_free_path(path);
6904                                 return ret;
6905                         }
6906                         if (ret) {
6907                                 ret = 0;
6908                                 break;
6909                         }
6910                 }
6911                 leaf = path->nodes[0];
6912                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6913                 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6914                         path->slots[0]++;
6915                         continue;
6916                 }
6917
6918                 chunk = btrfs_item_ptr(leaf, path->slots[0],
6919                                        struct btrfs_chunk);
6920                 btrfs_add_block_group(fs_info, 0,
6921                                       btrfs_chunk_type(leaf, chunk),
6922                                       key.objectid, key.offset,
6923                                       btrfs_chunk_length(leaf, chunk));
6924                 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6925                                  key.offset + btrfs_chunk_length(leaf, chunk),
6926                                  GFP_NOFS);
6927                 path->slots[0]++;
6928         }
6929         start = 0;
6930         while (1) {
6931                 cache = btrfs_lookup_first_block_group(fs_info, start);
6932                 if (!cache)
6933                         break;
6934                 cache->cached = 1;
6935                 start = cache->key.objectid + cache->key.offset;
6936         }
6937
6938         btrfs_free_path(path);
6939         return 0;
6940 }
6941
6942 static int reset_balance(struct btrfs_trans_handle *trans,
6943                          struct btrfs_fs_info *fs_info)
6944 {
6945         struct btrfs_root *root = fs_info->tree_root;
6946         struct btrfs_path *path;
6947         struct extent_buffer *leaf;
6948         struct btrfs_key key;
6949         int del_slot, del_nr = 0;
6950         int ret;
6951         int found = 0;
6952
6953         path = btrfs_alloc_path();
6954         if (!path)
6955                 return -ENOMEM;
6956
6957         key.objectid = BTRFS_BALANCE_OBJECTID;
6958         key.type = BTRFS_BALANCE_ITEM_KEY;
6959         key.offset = 0;
6960
6961         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6962         if (ret) {
6963                 if (ret > 0)
6964                         ret = 0;
6965                 if (!ret)
6966                         goto reinit_data_reloc;
6967                 else
6968                         goto out;
6969         }
6970
6971         ret = btrfs_del_item(trans, root, path);
6972         if (ret)
6973                 goto out;
6974         btrfs_release_path(path);
6975
6976         key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6977         key.type = BTRFS_ROOT_ITEM_KEY;
6978         key.offset = 0;
6979
6980         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6981         if (ret < 0)
6982                 goto out;
6983         while (1) {
6984                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6985                         if (!found)
6986                                 break;
6987
6988                         if (del_nr) {
6989                                 ret = btrfs_del_items(trans, root, path,
6990                                                       del_slot, del_nr);
6991                                 del_nr = 0;
6992                                 if (ret)
6993                                         goto out;
6994                         }
6995                         key.offset++;
6996                         btrfs_release_path(path);
6997
6998                         found = 0;
6999                         ret = btrfs_search_slot(trans, root, &key, path,
7000                                                 -1, 1);
7001                         if (ret < 0)
7002                                 goto out;
7003                         continue;
7004                 }
7005                 found = 1;
7006                 leaf = path->nodes[0];
7007                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7008                 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
7009                         break;
7010                 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7011                         path->slots[0]++;
7012                         continue;
7013                 }
7014                 if (!del_nr) {
7015                         del_slot = path->slots[0];
7016                         del_nr = 1;
7017                 } else {
7018                         del_nr++;
7019                 }
7020                 path->slots[0]++;
7021         }
7022
7023         if (del_nr) {
7024                 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
7025                 if (ret)
7026                         goto out;
7027         }
7028         btrfs_release_path(path);
7029
7030 reinit_data_reloc:
7031         key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7032         key.type = BTRFS_ROOT_ITEM_KEY;
7033         key.offset = (u64)-1;
7034         root = btrfs_read_fs_root(fs_info, &key);
7035         if (IS_ERR(root)) {
7036                 fprintf(stderr, "Error reading data reloc tree\n");
7037                 return PTR_ERR(root);
7038         }
7039         record_root_in_trans(trans, root);
7040         ret = btrfs_fsck_reinit_root(trans, root, 0);
7041         if (ret)
7042                 goto out;
7043         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
7044 out:
7045         btrfs_free_path(path);
7046         return ret;
7047 }
7048
7049 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
7050                               struct btrfs_fs_info *fs_info)
7051 {
7052         u64 start = 0;
7053         int ret;
7054
7055         /*
7056          * The only reason we don't do this is because right now we're just
7057          * walking the trees we find and pinning down their bytes, we don't look
7058          * at any of the leaves.  In order to do mixed groups we'd have to check
7059          * the leaves of any fs roots and pin down the bytes for any file
7060          * extents we find.  Not hard but why do it if we don't have to?
7061          */
7062         if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
7063                 fprintf(stderr, "We don't support re-initing the extent tree "
7064                         "for mixed block groups yet, please notify a btrfs "
7065                         "developer you want to do this so they can add this "
7066                         "functionality.\n");
7067                 return -EINVAL;
7068         }
7069
7070         /*
7071          * first we need to walk all of the trees except the extent tree and pin
7072          * down the bytes that are in use so we don't overwrite any existing
7073          * metadata.
7074          */
7075         ret = pin_metadata_blocks(fs_info);
7076         if (ret) {
7077                 fprintf(stderr, "error pinning down used bytes\n");
7078                 return ret;
7079         }
7080
7081         /*
7082          * Need to drop all the block groups since we're going to recreate all
7083          * of them again.
7084          */
7085         btrfs_free_block_groups(fs_info);
7086         ret = reset_block_groups(fs_info);
7087         if (ret) {
7088                 fprintf(stderr, "error resetting the block groups\n");
7089                 return ret;
7090         }
7091
7092         /* Ok we can allocate now, reinit the extent root */
7093         ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
7094         if (ret) {
7095                 fprintf(stderr, "extent root initialization failed\n");
7096                 /*
7097                  * When the transaction code is updated we should end the
7098                  * transaction, but for now progs only knows about commit so
7099                  * just return an error.
7100                  */
7101                 return ret;
7102         }
7103
7104         /*
7105          * Now we have all the in-memory block groups setup so we can make
7106          * allocations properly, and the metadata we care about is safe since we
7107          * pinned all of it above.
7108          */
7109         while (1) {
7110                 struct btrfs_block_group_cache *cache;
7111
7112                 cache = btrfs_lookup_first_block_group(fs_info, start);
7113                 if (!cache)
7114                         break;
7115                 start = cache->key.objectid + cache->key.offset;
7116                 ret = btrfs_insert_item(trans, fs_info->extent_root,
7117                                         &cache->key, &cache->item,
7118                                         sizeof(cache->item));
7119                 if (ret) {
7120                         fprintf(stderr, "Error adding block group\n");
7121                         return ret;
7122                 }
7123                 btrfs_extent_post_op(trans, fs_info->extent_root);
7124         }
7125
7126         ret = reset_balance(trans, fs_info);
7127         if (ret)
7128                 fprintf(stderr, "error reseting the pending balance\n");
7129
7130         return ret;
7131 }
7132
7133 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
7134 {
7135         struct btrfs_path *path;
7136         struct btrfs_trans_handle *trans;
7137         struct btrfs_key key;
7138         int ret;
7139
7140         printf("Recowing metadata block %llu\n", eb->start);
7141         key.objectid = btrfs_header_owner(eb);
7142         key.type = BTRFS_ROOT_ITEM_KEY;
7143         key.offset = (u64)-1;
7144
7145         root = btrfs_read_fs_root(root->fs_info, &key);
7146         if (IS_ERR(root)) {
7147                 fprintf(stderr, "Couldn't find owner root %llu\n",
7148                         key.objectid);
7149                 return PTR_ERR(root);
7150         }
7151
7152         path = btrfs_alloc_path();
7153         if (!path)
7154                 return -ENOMEM;
7155
7156         trans = btrfs_start_transaction(root, 1);
7157         if (IS_ERR(trans)) {
7158                 btrfs_free_path(path);
7159                 return PTR_ERR(trans);
7160         }
7161
7162         path->lowest_level = btrfs_header_level(eb);
7163         if (path->lowest_level)
7164                 btrfs_node_key_to_cpu(eb, &key, 0);
7165         else
7166                 btrfs_item_key_to_cpu(eb, &key, 0);
7167
7168         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7169         btrfs_commit_transaction(trans, root);
7170         btrfs_free_path(path);
7171         return ret;
7172 }
7173
7174 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
7175 {
7176         struct btrfs_path *path;
7177         struct btrfs_trans_handle *trans;
7178         struct btrfs_key key;
7179         int ret;
7180
7181         printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
7182                bad->key.type, bad->key.offset);
7183         key.objectid = bad->root_id;
7184         key.type = BTRFS_ROOT_ITEM_KEY;
7185         key.offset = (u64)-1;
7186
7187         root = btrfs_read_fs_root(root->fs_info, &key);
7188         if (IS_ERR(root)) {
7189                 fprintf(stderr, "Couldn't find owner root %llu\n",
7190                         key.objectid);
7191                 return PTR_ERR(root);
7192         }
7193
7194         path = btrfs_alloc_path();
7195         if (!path)
7196                 return -ENOMEM;
7197
7198         trans = btrfs_start_transaction(root, 1);
7199         if (IS_ERR(trans)) {
7200                 btrfs_free_path(path);
7201                 return PTR_ERR(trans);
7202         }
7203
7204         ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
7205         if (ret) {
7206                 if (ret > 0)
7207                         ret = 0;
7208                 goto out;
7209         }
7210         ret = btrfs_del_item(trans, root, path);
7211 out:
7212         btrfs_commit_transaction(trans, root);
7213         btrfs_free_path(path);
7214         return ret;
7215 }
7216
7217 static int zero_log_tree(struct btrfs_root *root)
7218 {
7219         struct btrfs_trans_handle *trans;
7220         int ret;
7221
7222         trans = btrfs_start_transaction(root, 1);
7223         if (IS_ERR(trans)) {
7224                 ret = PTR_ERR(trans);
7225                 return ret;
7226         }
7227         btrfs_set_super_log_root(root->fs_info->super_copy, 0);
7228         btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
7229         ret = btrfs_commit_transaction(trans, root);
7230         return ret;
7231 }
7232
7233 static int populate_csum(struct btrfs_trans_handle *trans,
7234                          struct btrfs_root *csum_root, char *buf, u64 start,
7235                          u64 len)
7236 {
7237         u64 offset = 0;
7238         u64 sectorsize;
7239         int ret = 0;
7240
7241         while (offset < len) {
7242                 sectorsize = csum_root->sectorsize;
7243                 ret = read_extent_data(csum_root, buf, start + offset,
7244                                        &sectorsize, 0);
7245                 if (ret)
7246                         break;
7247                 ret = btrfs_csum_file_block(trans, csum_root, start + len,
7248                                             start + offset, buf, sectorsize);
7249                 if (ret)
7250                         break;
7251                 offset += sectorsize;
7252         }
7253         return ret;
7254 }
7255
7256 static int fill_csum_tree(struct btrfs_trans_handle *trans,
7257                           struct btrfs_root *csum_root)
7258 {
7259         struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
7260         struct btrfs_path *path;
7261         struct btrfs_extent_item *ei;
7262         struct extent_buffer *leaf;
7263         char *buf;
7264         struct btrfs_key key;
7265         int ret;
7266
7267         path = btrfs_alloc_path();
7268         if (!path)
7269                 return -ENOMEM;
7270
7271         key.objectid = 0;
7272         key.type = BTRFS_EXTENT_ITEM_KEY;
7273         key.offset = 0;
7274
7275         ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
7276         if (ret < 0) {
7277                 btrfs_free_path(path);
7278                 return ret;
7279         }
7280
7281         buf = malloc(csum_root->sectorsize);
7282         if (!buf) {
7283                 btrfs_free_path(path);
7284                 return -ENOMEM;
7285         }
7286
7287         while (1) {
7288                 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7289                         ret = btrfs_next_leaf(extent_root, path);
7290                         if (ret < 0)
7291                                 break;
7292                         if (ret) {
7293                                 ret = 0;
7294                                 break;
7295                         }
7296                 }
7297                 leaf = path->nodes[0];
7298
7299                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7300                 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7301                         path->slots[0]++;
7302                         continue;
7303                 }
7304
7305                 ei = btrfs_item_ptr(leaf, path->slots[0],
7306                                     struct btrfs_extent_item);
7307                 if (!(btrfs_extent_flags(leaf, ei) &
7308                       BTRFS_EXTENT_FLAG_DATA)) {
7309                         path->slots[0]++;
7310                         continue;
7311                 }
7312
7313                 ret = populate_csum(trans, csum_root, buf, key.objectid,
7314                                     key.offset);
7315                 if (ret)
7316                         break;
7317                 path->slots[0]++;
7318         }
7319
7320         btrfs_free_path(path);
7321         free(buf);
7322         return ret;
7323 }
7324
7325 struct root_item_info {
7326         /* level of the root */
7327         u8 level;
7328         /* number of nodes at this level, must be 1 for a root */
7329         int node_count;
7330         u64 bytenr;
7331         u64 gen;
7332         struct cache_extent cache_extent;
7333 };
7334
7335 static struct cache_tree *roots_info_cache = NULL;
7336
7337 static void free_roots_info_cache(void)
7338 {
7339         if (!roots_info_cache)
7340                 return;
7341
7342         while (!cache_tree_empty(roots_info_cache)) {
7343                 struct cache_extent *entry;
7344                 struct root_item_info *rii;
7345
7346                 entry = first_cache_extent(roots_info_cache);
7347                 remove_cache_extent(roots_info_cache, entry);
7348                 rii = container_of(entry, struct root_item_info, cache_extent);
7349                 free(rii);
7350         }
7351
7352         free(roots_info_cache);
7353         roots_info_cache = NULL;
7354 }
7355
7356 static int build_roots_info_cache(struct btrfs_fs_info *info)
7357 {
7358         int ret = 0;
7359         struct btrfs_key key;
7360         struct extent_buffer *leaf;
7361         struct btrfs_path *path;
7362
7363         if (!roots_info_cache) {
7364                 roots_info_cache = malloc(sizeof(*roots_info_cache));
7365                 if (!roots_info_cache)
7366                         return -ENOMEM;
7367                 cache_tree_init(roots_info_cache);
7368         }
7369
7370         path = btrfs_alloc_path();
7371         if (!path)
7372                 return -ENOMEM;
7373
7374         key.objectid = 0;
7375         key.type = BTRFS_EXTENT_ITEM_KEY;
7376         key.offset = 0;
7377
7378         ret = btrfs_search_slot(NULL, info->extent_root, &key, path, 0, 0);
7379         if (ret < 0)
7380                 goto out;
7381         leaf = path->nodes[0];
7382
7383         while (1) {
7384                 struct btrfs_key found_key;
7385                 struct btrfs_extent_item *ei;
7386                 struct btrfs_extent_inline_ref *iref;
7387                 int slot = path->slots[0];
7388                 int type;
7389                 u64 flags;
7390                 u64 root_id;
7391                 u8 level;
7392                 struct cache_extent *entry;
7393                 struct root_item_info *rii;
7394
7395                 if (slot >= btrfs_header_nritems(leaf)) {
7396                         ret = btrfs_next_leaf(info->extent_root, path);
7397                         if (ret < 0) {
7398                                 break;
7399                         } else if (ret) {
7400                                 ret = 0;
7401                                 break;
7402                         }
7403                         leaf = path->nodes[0];
7404                         slot = path->slots[0];
7405                 }
7406
7407                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7408
7409                 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
7410                     found_key.type != BTRFS_METADATA_ITEM_KEY)
7411                         goto next;
7412
7413                 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
7414                 flags = btrfs_extent_flags(leaf, ei);
7415
7416                 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
7417                     !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
7418                         goto next;
7419
7420                 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
7421                         iref = (struct btrfs_extent_inline_ref *)(ei + 1);
7422                         level = found_key.offset;
7423                 } else {
7424                         struct btrfs_tree_block_info *info;
7425
7426                         info = (struct btrfs_tree_block_info *)(ei + 1);
7427                         iref = (struct btrfs_extent_inline_ref *)(info + 1);
7428                         level = btrfs_tree_block_level(leaf, info);
7429                 }
7430
7431                 /*
7432                  * For a root extent, it must be of the following type and the
7433                  * first (and only one) iref in the item.
7434                  */
7435                 type = btrfs_extent_inline_ref_type(leaf, iref);
7436                 if (type != BTRFS_TREE_BLOCK_REF_KEY)
7437                         goto next;
7438
7439                 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
7440                 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
7441                 if (!entry) {
7442                         rii = malloc(sizeof(struct root_item_info));
7443                         if (!rii) {
7444                                 ret = -ENOMEM;
7445                                 goto out;
7446                         }
7447                         rii->cache_extent.start = root_id;
7448                         rii->cache_extent.size = 1;
7449                         rii->level = (u8)-1;
7450                         entry = &rii->cache_extent;
7451                         ret = insert_cache_extent(roots_info_cache, entry);
7452                         ASSERT(ret == 0);
7453                 } else {
7454                         rii = container_of(entry, struct root_item_info,
7455                                            cache_extent);
7456                 }
7457
7458                 ASSERT(rii->cache_extent.start == root_id);
7459                 ASSERT(rii->cache_extent.size == 1);
7460
7461                 if (level > rii->level || rii->level == (u8)-1) {
7462                         rii->level = level;
7463                         rii->bytenr = found_key.objectid;
7464                         rii->gen = btrfs_extent_generation(leaf, ei);
7465                         rii->node_count = 1;
7466                 } else if (level == rii->level) {
7467                         rii->node_count++;
7468                 }
7469 next:
7470                 path->slots[0]++;
7471         }
7472
7473 out:
7474         btrfs_free_path(path);
7475
7476         return ret;
7477 }
7478
7479 static int maybe_repair_root_item(struct btrfs_fs_info *info,
7480                                   struct btrfs_path *path,
7481                                   const struct btrfs_key *root_key,
7482                                   const int read_only_mode)
7483 {
7484         const u64 root_id = root_key->objectid;
7485         struct cache_extent *entry;
7486         struct root_item_info *rii;
7487         struct btrfs_root_item ri;
7488         unsigned long offset;
7489
7490         entry = lookup_cache_extent(roots_info_cache, root_id, 1);
7491         if (!entry) {
7492                 fprintf(stderr,
7493                         "Error: could not find extent items for root %llu\n",
7494                         root_key->objectid);
7495                 return -ENOENT;
7496         }
7497
7498         rii = container_of(entry, struct root_item_info, cache_extent);
7499         ASSERT(rii->cache_extent.start == root_id);
7500         ASSERT(rii->cache_extent.size == 1);
7501
7502         if (rii->node_count != 1) {
7503                 fprintf(stderr,
7504                         "Error: could not find btree root extent for root %llu\n",
7505                         root_id);
7506                 return -ENOENT;
7507         }
7508
7509         offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
7510         read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
7511
7512         if (btrfs_root_bytenr(&ri) != rii->bytenr ||
7513             btrfs_root_level(&ri) != rii->level ||
7514             btrfs_root_generation(&ri) != rii->gen) {
7515
7516                 /*
7517                  * If we're in repair mode but our caller told us to not update
7518                  * the root item, i.e. just check if it needs to be updated, don't
7519                  * print this message, since the caller will call us again shortly
7520                  * for the same root item without read only mode (the caller will
7521                  * open a transaction first).
7522                  */
7523                 if (!(read_only_mode && repair))
7524                         fprintf(stderr,
7525                                 "%sroot item for root %llu,"
7526                                 " current bytenr %llu, current gen %llu, current level %u,"
7527                                 " new bytenr %llu, new gen %llu, new level %u\n",
7528                                 (read_only_mode ? "" : "fixing "),
7529                                 root_id,
7530                                 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
7531                                 btrfs_root_level(&ri),
7532                                 rii->bytenr, rii->gen, rii->level);
7533
7534                 if (btrfs_root_generation(&ri) > rii->gen) {
7535                         fprintf(stderr,
7536                                 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
7537                                 root_id, btrfs_root_generation(&ri), rii->gen);
7538                         return -EINVAL;
7539                 }
7540
7541                 if (!read_only_mode) {
7542                         btrfs_set_root_bytenr(&ri, rii->bytenr);
7543                         btrfs_set_root_level(&ri, rii->level);
7544                         btrfs_set_root_generation(&ri, rii->gen);
7545                         write_extent_buffer(path->nodes[0], &ri,
7546                                             offset, sizeof(ri));
7547                 }
7548
7549                 return 1;
7550         }
7551
7552         return 0;
7553 }
7554
7555 /*
7556  * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
7557  * caused read-only snapshots to be corrupted if they were created at a moment
7558  * when the source subvolume/snapshot had orphan items. The issue was that the
7559  * on-disk root items became incorrect, referring to the pre orphan cleanup root
7560  * node instead of the post orphan cleanup root node.
7561  * So this function, and its callees, just detects and fixes those cases. Even
7562  * though the regression was for read-only snapshots, this function applies to
7563  * any snapshot/subvolume root.
7564  * This must be run before any other repair code - not doing it so, makes other
7565  * repair code delete or modify backrefs in the extent tree for example, which
7566  * will result in an inconsistent fs after repairing the root items.
7567  */
7568 static int repair_root_items(struct btrfs_fs_info *info)
7569 {
7570         struct btrfs_path *path = NULL;
7571         struct btrfs_key key;
7572         struct extent_buffer *leaf;
7573         struct btrfs_trans_handle *trans = NULL;
7574         int ret = 0;
7575         int bad_roots = 0;
7576         int need_trans = 0;
7577
7578         ret = build_roots_info_cache(info);
7579         if (ret)
7580                 goto out;
7581
7582         path = btrfs_alloc_path();
7583         if (!path) {
7584                 ret = -ENOMEM;
7585                 goto out;
7586         }
7587
7588         key.objectid = BTRFS_FIRST_FREE_OBJECTID;
7589         key.type = BTRFS_ROOT_ITEM_KEY;
7590         key.offset = 0;
7591
7592 again:
7593         /*
7594          * Avoid opening and committing transactions if a leaf doesn't have
7595          * any root items that need to be fixed, so that we avoid rotating
7596          * backup roots unnecessarily.
7597          */
7598         if (need_trans) {
7599                 trans = btrfs_start_transaction(info->tree_root, 1);
7600                 if (IS_ERR(trans)) {
7601                         ret = PTR_ERR(trans);
7602                         goto out;
7603                 }
7604         }
7605
7606         ret = btrfs_search_slot(trans, info->tree_root, &key, path,
7607                                 0, trans ? 1 : 0);
7608         if (ret < 0)
7609                 goto out;
7610         leaf = path->nodes[0];
7611
7612         while (1) {
7613                 struct btrfs_key found_key;
7614
7615                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7616                         int no_more_keys = find_next_key(path, &key);
7617
7618                         btrfs_release_path(path);
7619                         if (trans) {
7620                                 ret = btrfs_commit_transaction(trans,
7621                                                                info->tree_root);
7622                                 trans = NULL;
7623                                 if (ret < 0)
7624                                         goto out;
7625                         }
7626                         need_trans = 0;
7627                         if (no_more_keys)
7628                                 break;
7629                         goto again;
7630                 }
7631
7632                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7633
7634                 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
7635                         goto next;
7636
7637                 ret = maybe_repair_root_item(info, path, &found_key,
7638                                              trans ? 0 : 1);
7639                 if (ret < 0)
7640                         goto out;
7641                 if (ret) {
7642                         if (!trans && repair) {
7643                                 need_trans = 1;
7644                                 key = found_key;
7645                                 btrfs_release_path(path);
7646                                 goto again;
7647                         }
7648                         bad_roots++;
7649                 }
7650 next:
7651                 path->slots[0]++;
7652         }
7653         ret = 0;
7654 out:
7655         free_roots_info_cache();
7656         if (path)
7657                 btrfs_free_path(path);
7658         if (ret < 0)
7659                 return ret;
7660
7661         return bad_roots;
7662 }
7663
7664 static struct option long_options[] = {
7665         { "super", 1, NULL, 's' },
7666         { "repair", 0, NULL, 0 },
7667         { "init-csum-tree", 0, NULL, 0 },
7668         { "init-extent-tree", 0, NULL, 0 },
7669         { "check-data-csum", 0, NULL, 0 },
7670         { "backup", 0, NULL, 0 },
7671         { "subvol-extents", 1, NULL, 'E' },
7672         { "qgroup-report", 0, NULL, 'Q' },
7673         { NULL, 0, NULL, 0}
7674 };
7675
7676 const char * const cmd_check_usage[] = {
7677         "btrfs check [options] <device>",
7678         "Check an unmounted btrfs filesystem.",
7679         "",
7680         "-s|--super <superblock>     use this superblock copy",
7681         "-b|--backup                 use the backup root copy",
7682         "--repair                    try to repair the filesystem",
7683         "--init-csum-tree            create a new CRC tree",
7684         "--init-extent-tree          create a new extent tree",
7685         "--check-data-csum           verify checkums of data blocks",
7686         "--qgroup-report             print a report on qgroup consistency",
7687         "--subvol-extents <subvolid> print subvolume extents and sharing state",
7688         NULL
7689 };
7690
7691 int cmd_check(int argc, char **argv)
7692 {
7693         struct cache_tree root_cache;
7694         struct btrfs_root *root;
7695         struct btrfs_fs_info *info;
7696         u64 bytenr = 0;
7697         u64 subvolid = 0;
7698         char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
7699         int ret;
7700         u64 num;
7701         int option_index = 0;
7702         int init_csum_tree = 0;
7703         int qgroup_report = 0;
7704         enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_EXCLUSIVE;
7705
7706         while(1) {
7707                 int c;
7708                 c = getopt_long(argc, argv, "as:b", long_options,
7709                                 &option_index);
7710                 if (c < 0)
7711                         break;
7712                 switch(c) {
7713                         case 'a': /* ignored */ break;
7714                         case 'b':
7715                                 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
7716                                 break;
7717                         case 's':
7718                                 num = arg_strtou64(optarg);
7719                                 if (num >= BTRFS_SUPER_MIRROR_MAX) {
7720                                         fprintf(stderr,
7721                                                 "ERROR: super mirror should be less than: %d\n",
7722                                                 BTRFS_SUPER_MIRROR_MAX);
7723                                         exit(1);
7724                                 }
7725                                 bytenr = btrfs_sb_offset(((int)num));
7726                                 printf("using SB copy %llu, bytenr %llu\n", num,
7727                                        (unsigned long long)bytenr);
7728                                 break;
7729                         case 'Q':
7730                                 qgroup_report = 1;
7731                                 break;
7732                         case 'E':
7733                                 subvolid = arg_strtou64(optarg);
7734                                 break;
7735                         case '?':
7736                         case 'h':
7737                                 usage(cmd_check_usage);
7738                 }
7739                 if (option_index == 1) {
7740                         printf("enabling repair mode\n");
7741                         repair = 1;
7742                         ctree_flags |= OPEN_CTREE_WRITES;
7743                 } else if (option_index == 2) {
7744                         printf("Creating a new CRC tree\n");
7745                         init_csum_tree = 1;
7746                         repair = 1;
7747                         ctree_flags |= OPEN_CTREE_WRITES;
7748                 } else if (option_index == 3) {
7749                         init_extent_tree = 1;
7750                         ctree_flags |= (OPEN_CTREE_WRITES |
7751                                         OPEN_CTREE_NO_BLOCK_GROUPS);
7752                         repair = 1;
7753                 } else if (option_index == 4) {
7754                         check_data_csum = 1;
7755                 }
7756         }
7757         argc = argc - optind;
7758
7759         if (check_argc_exact(argc, 1))
7760                 usage(cmd_check_usage);
7761
7762         radix_tree_init();
7763         cache_tree_init(&root_cache);
7764
7765         if((ret = check_mounted(argv[optind])) < 0) {
7766                 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
7767                 goto err_out;
7768         } else if(ret) {
7769                 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
7770                 ret = -EBUSY;
7771                 goto err_out;
7772         }
7773
7774         /* only allow partial opening under repair mode */
7775         if (repair)
7776                 ctree_flags |= OPEN_CTREE_PARTIAL;
7777
7778         info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
7779         if (!info) {
7780                 fprintf(stderr, "Couldn't open file system\n");
7781                 ret = -EIO;
7782                 goto err_out;
7783         }
7784
7785         root = info->fs_root;
7786
7787         ret = repair_root_items(info);
7788         if (ret < 0)
7789                 goto close_out;
7790         if (repair) {
7791                 fprintf(stderr, "Fixed %d roots.\n", ret);
7792                 ret = 0;
7793         } else if (ret > 0) {
7794                 fprintf(stderr,
7795                        "Found %d roots with an outdated root item.\n",
7796                        ret);
7797                 fprintf(stderr,
7798                         "Please run a filesystem check with the option --repair to fix them.\n");
7799                 ret = 1;
7800                 goto close_out;
7801         }
7802
7803         /*
7804          * repair mode will force us to commit transaction which
7805          * will make us fail to load log tree when mounting.
7806          */
7807         if (repair && btrfs_super_log_root(info->super_copy)) {
7808                 ret = ask_user("repair mode will force to clear out log tree, Are you sure?");
7809                 if (!ret) {
7810                         ret = 1;
7811                         goto close_out;
7812                 }
7813                 ret = zero_log_tree(root);
7814                 if (ret) {
7815                         fprintf(stderr, "fail to zero log tree\n");
7816                         goto close_out;
7817                 }
7818         }
7819
7820         uuid_unparse(info->super_copy->fsid, uuidbuf);
7821         if (qgroup_report) {
7822                 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
7823                        uuidbuf);
7824                 ret = qgroup_verify_all(info);
7825                 if (ret == 0)
7826                         print_qgroup_report(1);
7827                 goto close_out;
7828         }
7829         if (subvolid) {
7830                 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
7831                        subvolid, argv[optind], uuidbuf);
7832                 ret = print_extent_state(info, subvolid);
7833                 goto close_out;
7834         }
7835         printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
7836
7837         if (!extent_buffer_uptodate(info->tree_root->node) ||
7838             !extent_buffer_uptodate(info->dev_root->node) ||
7839             !extent_buffer_uptodate(info->chunk_root->node)) {
7840                 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7841                 ret = -EIO;
7842                 goto close_out;
7843         }
7844
7845         if (init_extent_tree || init_csum_tree) {
7846                 struct btrfs_trans_handle *trans;
7847
7848                 trans = btrfs_start_transaction(info->extent_root, 0);
7849                 if (IS_ERR(trans)) {
7850                         fprintf(stderr, "Error starting transaction\n");
7851                         ret = PTR_ERR(trans);
7852                         goto close_out;
7853                 }
7854
7855                 if (init_extent_tree) {
7856                         printf("Creating a new extent tree\n");
7857                         ret = reinit_extent_tree(trans, info);
7858                         if (ret)
7859                                 goto close_out;
7860                 }
7861
7862                 if (init_csum_tree) {
7863                         fprintf(stderr, "Reinit crc root\n");
7864                         ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
7865                         if (ret) {
7866                                 fprintf(stderr, "crc root initialization failed\n");
7867                                 ret = -EIO;
7868                                 goto close_out;
7869                         }
7870
7871                         ret = fill_csum_tree(trans, info->csum_root);
7872                         if (ret) {
7873                                 fprintf(stderr, "crc refilling failed\n");
7874                                 return -EIO;
7875                         }
7876                 }
7877                 /*
7878                  * Ok now we commit and run the normal fsck, which will add
7879                  * extent entries for all of the items it finds.
7880                  */
7881                 ret = btrfs_commit_transaction(trans, info->extent_root);
7882                 if (ret)
7883                         goto close_out;
7884         }
7885         if (!extent_buffer_uptodate(info->extent_root->node)) {
7886                 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7887                 ret = -EIO;
7888                 goto close_out;
7889         }
7890         if (!extent_buffer_uptodate(info->csum_root->node)) {
7891                 fprintf(stderr, "Checksum root corrupted, rerun with --init-csum-tree option\n");
7892                 ret = -EIO;
7893                 goto close_out;
7894         }
7895
7896         fprintf(stderr, "checking extents\n");
7897         ret = check_chunks_and_extents(root);
7898         if (ret)
7899                 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
7900
7901         fprintf(stderr, "checking free space cache\n");
7902         ret = check_space_cache(root);
7903         if (ret)
7904                 goto out;
7905
7906         /*
7907          * We used to have to have these hole extents in between our real
7908          * extents so if we don't have this flag set we need to make sure there
7909          * are no gaps in the file extents for inodes, otherwise we can just
7910          * ignore it when this happens.
7911          */
7912         no_holes = btrfs_fs_incompat(root->fs_info,
7913                                      BTRFS_FEATURE_INCOMPAT_NO_HOLES);
7914         fprintf(stderr, "checking fs roots\n");
7915         ret = check_fs_roots(root, &root_cache);
7916         if (ret)
7917                 goto out;
7918
7919         fprintf(stderr, "checking csums\n");
7920         ret = check_csums(root);
7921         if (ret)
7922                 goto out;
7923
7924         fprintf(stderr, "checking root refs\n");
7925         ret = check_root_refs(root, &root_cache);
7926         if (ret)
7927                 goto out;
7928
7929         while (repair && !list_empty(&root->fs_info->recow_ebs)) {
7930                 struct extent_buffer *eb;
7931
7932                 eb = list_first_entry(&root->fs_info->recow_ebs,
7933                                       struct extent_buffer, recow);
7934                 list_del_init(&eb->recow);
7935                 ret = recow_extent_buffer(root, eb);
7936                 if (ret)
7937                         break;
7938         }
7939
7940         while (!list_empty(&delete_items)) {
7941                 struct bad_item *bad;
7942
7943                 bad = list_first_entry(&delete_items, struct bad_item, list);
7944                 list_del_init(&bad->list);
7945                 if (repair)
7946                         ret = delete_bad_item(root, bad);
7947                 free(bad);
7948         }
7949
7950         if (info->quota_enabled) {
7951                 int err;
7952                 fprintf(stderr, "checking quota groups\n");
7953                 err = qgroup_verify_all(info);
7954                 if (err)
7955                         goto out;
7956         }
7957
7958         if (!list_empty(&root->fs_info->recow_ebs)) {
7959                 fprintf(stderr, "Transid errors in file system\n");
7960                 ret = 1;
7961         }
7962 out:
7963         print_qgroup_report(0);
7964         if (found_old_backref) { /*
7965                  * there was a disk format change when mixed
7966                  * backref was in testing tree. The old format
7967                  * existed about one week.
7968                  */
7969                 printf("\n * Found old mixed backref format. "
7970                        "The old format is not supported! *"
7971                        "\n * Please mount the FS in readonly mode, "
7972                        "backup data and re-format the FS. *\n\n");
7973                 ret = 1;
7974         }
7975         printf("found %llu bytes used err is %d\n",
7976                (unsigned long long)bytes_used, ret);
7977         printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
7978         printf("total tree bytes: %llu\n",
7979                (unsigned long long)total_btree_bytes);
7980         printf("total fs tree bytes: %llu\n",
7981                (unsigned long long)total_fs_tree_bytes);
7982         printf("total extent tree bytes: %llu\n",
7983                (unsigned long long)total_extent_tree_bytes);
7984         printf("btree space waste bytes: %llu\n",
7985                (unsigned long long)btree_space_waste);
7986         printf("file data blocks allocated: %llu\n referenced %llu\n",
7987                 (unsigned long long)data_bytes_allocated,
7988                 (unsigned long long)data_bytes_referenced);
7989         printf("%s\n", BTRFS_BUILD_VERSION);
7990
7991         free_root_recs_tree(&root_cache);
7992 close_out:
7993         close_ctree(root);
7994 err_out:
7995         return ret;
7996 }