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