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