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