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