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