btrfs-progs: Free resources when returning error from cmd_subvol_create()
[platform/upstream/btrfs-progs.git] / cmds-check.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE 1
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <getopt.h>
29 #include <uuid/uuid.h>
30 #include "kerncompat.h"
31 #include "ctree.h"
32 #include "volumes.h"
33 #include "repair.h"
34 #include "disk-io.h"
35 #include "print-tree.h"
36 #include "transaction.h"
37 #include "list.h"
38 #include "version.h"
39 #include "utils.h"
40 #include "commands.h"
41
42 static u64 bytes_used = 0;
43 static u64 total_csum_bytes = 0;
44 static u64 total_btree_bytes = 0;
45 static u64 total_fs_tree_bytes = 0;
46 static u64 btree_space_waste = 0;
47 static u64 data_bytes_allocated = 0;
48 static u64 data_bytes_referenced = 0;
49 static int found_old_backref = 0;
50
51 struct extent_backref {
52         struct list_head list;
53         unsigned int is_data:1;
54         unsigned int found_extent_tree:1;
55         unsigned int full_backref:1;
56         unsigned int found_ref:1;
57 };
58
59 struct data_backref {
60         struct extent_backref node;
61         union {
62                 u64 parent;
63                 u64 root;
64         };
65         u64 owner;
66         u64 offset;
67         u32 num_refs;
68         u32 found_ref;
69 };
70
71 struct tree_backref {
72         struct extent_backref node;
73         union {
74                 u64 parent;
75                 u64 root;
76         };
77 };
78
79 struct extent_record {
80         struct list_head backrefs;
81         struct cache_extent cache;
82         struct btrfs_disk_key parent_key;
83         u64 start;
84         u64 max_size;
85         u64 nr;
86         u64 refs;
87         u64 extent_item_refs;
88         u64 generation;
89         u64 info_objectid;
90         u8 info_level;
91         unsigned int content_checked:1;
92         unsigned int owner_ref_checked:1;
93         unsigned int is_root:1;
94 };
95
96 struct inode_backref {
97         struct list_head list;
98         unsigned int found_dir_item:1;
99         unsigned int found_dir_index:1;
100         unsigned int found_inode_ref:1;
101         unsigned int filetype:8;
102         int errors;
103         unsigned int ref_type;
104         u64 dir;
105         u64 index;
106         u16 namelen;
107         char name[0];
108 };
109
110 #define REF_ERR_NO_DIR_ITEM             (1 << 0)
111 #define REF_ERR_NO_DIR_INDEX            (1 << 1)
112 #define REF_ERR_NO_INODE_REF            (1 << 2)
113 #define REF_ERR_DUP_DIR_ITEM            (1 << 3)
114 #define REF_ERR_DUP_DIR_INDEX           (1 << 4)
115 #define REF_ERR_DUP_INODE_REF           (1 << 5)
116 #define REF_ERR_INDEX_UNMATCH           (1 << 6)
117 #define REF_ERR_FILETYPE_UNMATCH        (1 << 7)
118 #define REF_ERR_NAME_TOO_LONG           (1 << 8) // 100
119 #define REF_ERR_NO_ROOT_REF             (1 << 9)
120 #define REF_ERR_NO_ROOT_BACKREF         (1 << 10)
121 #define REF_ERR_DUP_ROOT_REF            (1 << 11)
122 #define REF_ERR_DUP_ROOT_BACKREF        (1 << 12)
123
124 struct inode_record {
125         struct list_head backrefs;
126         unsigned int checked:1;
127         unsigned int merging:1;
128         unsigned int found_inode_item:1;
129         unsigned int found_dir_item:1;
130         unsigned int found_file_extent:1;
131         unsigned int found_csum_item:1;
132         unsigned int some_csum_missing:1;
133         unsigned int nodatasum:1;
134         int errors;
135
136         u64 ino;
137         u32 nlink;
138         u32 imode;
139         u64 isize;
140         u64 nbytes;
141
142         u32 found_link;
143         u64 found_size;
144         u64 extent_start;
145         u64 extent_end;
146         u64 first_extent_gap;
147
148         u32 refs;
149 };
150
151 #define I_ERR_NO_INODE_ITEM             (1 << 0)
152 #define I_ERR_NO_ORPHAN_ITEM            (1 << 1)
153 #define I_ERR_DUP_INODE_ITEM            (1 << 2)
154 #define I_ERR_DUP_DIR_INDEX             (1 << 3)
155 #define I_ERR_ODD_DIR_ITEM              (1 << 4)
156 #define I_ERR_ODD_FILE_EXTENT           (1 << 5)
157 #define I_ERR_BAD_FILE_EXTENT           (1 << 6)
158 #define I_ERR_FILE_EXTENT_OVERLAP       (1 << 7)
159 #define I_ERR_FILE_EXTENT_DISCOUNT      (1 << 8) // 100
160 #define I_ERR_DIR_ISIZE_WRONG           (1 << 9)
161 #define I_ERR_FILE_NBYTES_WRONG         (1 << 10) // 400
162 #define I_ERR_ODD_CSUM_ITEM             (1 << 11)
163 #define I_ERR_SOME_CSUM_MISSING         (1 << 12)
164 #define I_ERR_LINK_COUNT_WRONG          (1 << 13)
165
166 struct root_backref {
167         struct list_head list;
168         unsigned int found_dir_item:1;
169         unsigned int found_dir_index:1;
170         unsigned int found_back_ref:1;
171         unsigned int found_forward_ref:1;
172         unsigned int reachable:1;
173         int errors;
174         u64 ref_root;
175         u64 dir;
176         u64 index;
177         u16 namelen;
178         char name[0];
179 };
180
181 struct root_record {
182         struct list_head backrefs;
183         struct cache_extent cache;
184         unsigned int found_root_item:1;
185         u64 objectid;
186         u32 found_ref;
187 };
188
189 struct ptr_node {
190         struct cache_extent cache;
191         void *data;
192 };
193
194 struct shared_node {
195         struct cache_extent cache;
196         struct cache_tree root_cache;
197         struct cache_tree inode_cache;
198         struct inode_record *current;
199         u32 refs;
200 };
201
202 struct block_info {
203         u64 start;
204         u32 size;
205 };
206
207 struct walk_control {
208         struct cache_tree shared;
209         struct shared_node *nodes[BTRFS_MAX_LEVEL];
210         int active_node;
211         int root_level;
212 };
213
214 static u8 imode_to_type(u32 imode)
215 {
216 #define S_SHIFT 12
217         static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
218                 [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
219                 [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
220                 [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
221                 [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
222                 [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
223                 [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
224                 [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
225         };
226
227         return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
228 #undef S_SHIFT
229 }
230
231 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
232 {
233         struct inode_record *rec;
234         struct inode_backref *backref;
235         struct inode_backref *orig;
236         size_t size;
237
238         rec = malloc(sizeof(*rec));
239         memcpy(rec, orig_rec, sizeof(*rec));
240         rec->refs = 1;
241         INIT_LIST_HEAD(&rec->backrefs);
242
243         list_for_each_entry(orig, &orig_rec->backrefs, list) {
244                 size = sizeof(*orig) + orig->namelen + 1;
245                 backref = malloc(size);
246                 memcpy(backref, orig, size);
247                 list_add_tail(&backref->list, &rec->backrefs);
248         }
249         return rec;
250 }
251
252 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
253                                           u64 ino, int mod)
254 {
255         struct ptr_node *node;
256         struct cache_extent *cache;
257         struct inode_record *rec = NULL;
258         int ret;
259
260         cache = find_cache_extent(inode_cache, ino, 1);
261         if (cache) {
262                 node = container_of(cache, struct ptr_node, cache);
263                 rec = node->data;
264                 if (mod && rec->refs > 1) {
265                         node->data = clone_inode_rec(rec);
266                         rec->refs--;
267                         rec = node->data;
268                 }
269         } else if (mod) {
270                 rec = calloc(1, sizeof(*rec));
271                 rec->ino = ino;
272                 rec->extent_start = (u64)-1;
273                 rec->first_extent_gap = (u64)-1;
274                 rec->refs = 1;
275                 INIT_LIST_HEAD(&rec->backrefs);
276
277                 node = malloc(sizeof(*node));
278                 node->cache.start = ino;
279                 node->cache.size = 1;
280                 node->data = rec;
281
282                 if (ino == BTRFS_FREE_INO_OBJECTID)
283                         rec->found_link = 1;
284
285                 ret = insert_existing_cache_extent(inode_cache, &node->cache);
286                 BUG_ON(ret);
287         }
288         return rec;
289 }
290
291 static void free_inode_rec(struct inode_record *rec)
292 {
293         struct inode_backref *backref;
294
295         if (--rec->refs > 0)
296                 return;
297
298         while (!list_empty(&rec->backrefs)) {
299                 backref = list_entry(rec->backrefs.next,
300                                      struct inode_backref, list);
301                 list_del(&backref->list);
302                 free(backref);
303         }
304         free(rec);
305 }
306
307 static int can_free_inode_rec(struct inode_record *rec)
308 {
309         if (!rec->errors && rec->checked && rec->found_inode_item &&
310             rec->nlink == rec->found_link && list_empty(&rec->backrefs))
311                 return 1;
312         return 0;
313 }
314
315 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
316                                  struct inode_record *rec)
317 {
318         struct cache_extent *cache;
319         struct inode_backref *tmp, *backref;
320         struct ptr_node *node;
321         unsigned char filetype;
322
323         if (!rec->found_inode_item)
324                 return;
325
326         filetype = imode_to_type(rec->imode);
327         list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
328                 if (backref->found_dir_item && backref->found_dir_index) {
329                         if (backref->filetype != filetype)
330                                 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
331                         if (!backref->errors && backref->found_inode_ref) {
332                                 list_del(&backref->list);
333                                 free(backref);
334                         }
335                 }
336         }
337
338         if (!rec->checked || rec->merging)
339                 return;
340
341         if (S_ISDIR(rec->imode)) {
342                 if (rec->found_size != rec->isize)
343                         rec->errors |= I_ERR_DIR_ISIZE_WRONG;
344                 if (rec->found_file_extent)
345                         rec->errors |= I_ERR_ODD_FILE_EXTENT;
346         } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
347                 if (rec->found_dir_item)
348                         rec->errors |= I_ERR_ODD_DIR_ITEM;
349                 if (rec->found_size != rec->nbytes)
350                         rec->errors |= I_ERR_FILE_NBYTES_WRONG;
351                 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
352                         rec->first_extent_gap = 0;
353                 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
354                     rec->first_extent_gap < rec->isize))
355                         rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
356         }
357
358         if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
359                 if (rec->found_csum_item && rec->nodatasum)
360                         rec->errors |= I_ERR_ODD_CSUM_ITEM;
361                 if (rec->some_csum_missing && !rec->nodatasum)
362                         rec->errors |= I_ERR_SOME_CSUM_MISSING;
363         }
364
365         BUG_ON(rec->refs != 1);
366         if (can_free_inode_rec(rec)) {
367                 cache = find_cache_extent(inode_cache, rec->ino, 1);
368                 node = container_of(cache, struct ptr_node, cache);
369                 BUG_ON(node->data != rec);
370                 remove_cache_extent(inode_cache, &node->cache);
371                 free(node);
372                 free_inode_rec(rec);
373         }
374 }
375
376 static int check_orphan_item(struct btrfs_root *root, u64 ino)
377 {
378         struct btrfs_path path;
379         struct btrfs_key key;
380         int ret;
381
382         key.objectid = BTRFS_ORPHAN_OBJECTID;
383         key.type = BTRFS_ORPHAN_ITEM_KEY;
384         key.offset = ino;
385
386         btrfs_init_path(&path);
387         ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
388         btrfs_release_path(root, &path);
389         if (ret > 0)
390                 ret = -ENOENT;
391         return ret;
392 }
393
394 static int process_inode_item(struct extent_buffer *eb,
395                               int slot, struct btrfs_key *key,
396                               struct shared_node *active_node)
397 {
398         struct inode_record *rec;
399         struct btrfs_inode_item *item;
400
401         rec = active_node->current;
402         BUG_ON(rec->ino != key->objectid || rec->refs > 1);
403         if (rec->found_inode_item) {
404                 rec->errors |= I_ERR_DUP_INODE_ITEM;
405                 return 1;
406         }
407         item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
408         rec->nlink = btrfs_inode_nlink(eb, item);
409         rec->isize = btrfs_inode_size(eb, item);
410         rec->nbytes = btrfs_inode_nbytes(eb, item);
411         rec->imode = btrfs_inode_mode(eb, item);
412         if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
413                 rec->nodatasum = 1;
414         rec->found_inode_item = 1;
415         if (rec->nlink == 0)
416                 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
417         maybe_free_inode_rec(&active_node->inode_cache, rec);
418         return 0;
419 }
420
421 static struct inode_backref *get_inode_backref(struct inode_record *rec,
422                                                 const char *name,
423                                                 int namelen, u64 dir)
424 {
425         struct inode_backref *backref;
426
427         list_for_each_entry(backref, &rec->backrefs, list) {
428                 if (backref->dir != dir || backref->namelen != namelen)
429                         continue;
430                 if (memcmp(name, backref->name, namelen))
431                         continue;
432                 return backref;
433         }
434
435         backref = malloc(sizeof(*backref) + namelen + 1);
436         memset(backref, 0, sizeof(*backref));
437         backref->dir = dir;
438         backref->namelen = namelen;
439         memcpy(backref->name, name, namelen);
440         backref->name[namelen] = '\0';
441         list_add_tail(&backref->list, &rec->backrefs);
442         return backref;
443 }
444
445 static int add_inode_backref(struct cache_tree *inode_cache,
446                              u64 ino, u64 dir, u64 index,
447                              const char *name, int namelen,
448                              int filetype, int itemtype, int errors)
449 {
450         struct inode_record *rec;
451         struct inode_backref *backref;
452
453         rec = get_inode_rec(inode_cache, ino, 1);
454         backref = get_inode_backref(rec, name, namelen, dir);
455         if (errors)
456                 backref->errors |= errors;
457         if (itemtype == BTRFS_DIR_INDEX_KEY) {
458                 if (backref->found_dir_index)
459                         backref->errors |= REF_ERR_DUP_DIR_INDEX;
460                 if (backref->found_inode_ref && backref->index != index)
461                         backref->errors |= REF_ERR_INDEX_UNMATCH;
462                 if (backref->found_dir_item && backref->filetype != filetype)
463                         backref->errors |= REF_ERR_FILETYPE_UNMATCH;
464
465                 backref->index = index;
466                 backref->filetype = filetype;
467                 backref->found_dir_index = 1;
468         } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
469                 rec->found_link++;
470                 if (backref->found_dir_item)
471                         backref->errors |= REF_ERR_DUP_DIR_ITEM;
472                 if (backref->found_dir_index && backref->filetype != filetype)
473                         backref->errors |= REF_ERR_FILETYPE_UNMATCH;
474
475                 backref->filetype = filetype;
476                 backref->found_dir_item = 1;
477         } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
478                    (itemtype == BTRFS_INODE_EXTREF_KEY)) {
479                 if (backref->found_inode_ref)
480                         backref->errors |= REF_ERR_DUP_INODE_REF;
481                 if (backref->found_dir_index && backref->index != index)
482                         backref->errors |= REF_ERR_INDEX_UNMATCH;
483
484                 backref->ref_type = itemtype;
485                 backref->index = index;
486                 backref->found_inode_ref = 1;
487         } else {
488                 BUG_ON(1);
489         }
490
491         maybe_free_inode_rec(inode_cache, rec);
492         return 0;
493 }
494
495 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
496                             struct cache_tree *dst_cache)
497 {
498         struct inode_backref *backref;
499         u32 dir_count = 0;
500
501         dst->merging = 1;
502         list_for_each_entry(backref, &src->backrefs, list) {
503                 if (backref->found_dir_index) {
504                         add_inode_backref(dst_cache, dst->ino, backref->dir,
505                                         backref->index, backref->name,
506                                         backref->namelen, backref->filetype,
507                                         BTRFS_DIR_INDEX_KEY, backref->errors);
508                 }
509                 if (backref->found_dir_item) {
510                         dir_count++;
511                         add_inode_backref(dst_cache, dst->ino,
512                                         backref->dir, 0, backref->name,
513                                         backref->namelen, backref->filetype,
514                                         BTRFS_DIR_ITEM_KEY, backref->errors);
515                 }
516                 if (backref->found_inode_ref) {
517                         add_inode_backref(dst_cache, dst->ino,
518                                         backref->dir, backref->index,
519                                         backref->name, backref->namelen, 0,
520                                         backref->ref_type, backref->errors);
521                 }
522         }
523
524         if (src->found_dir_item)
525                 dst->found_dir_item = 1;
526         if (src->found_file_extent)
527                 dst->found_file_extent = 1;
528         if (src->found_csum_item)
529                 dst->found_csum_item = 1;
530         if (src->some_csum_missing)
531                 dst->some_csum_missing = 1;
532         if (dst->first_extent_gap > src->first_extent_gap)
533                 dst->first_extent_gap = src->first_extent_gap;
534
535         BUG_ON(src->found_link < dir_count);
536         dst->found_link += src->found_link - dir_count;
537         dst->found_size += src->found_size;
538         if (src->extent_start != (u64)-1) {
539                 if (dst->extent_start == (u64)-1) {
540                         dst->extent_start = src->extent_start;
541                         dst->extent_end = src->extent_end;
542                 } else {
543                         if (dst->extent_end > src->extent_start)
544                                 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
545                         else if (dst->extent_end < src->extent_start &&
546                                  dst->extent_end < dst->first_extent_gap)
547                                 dst->first_extent_gap = dst->extent_end;
548                         if (dst->extent_end < src->extent_end)
549                                 dst->extent_end = src->extent_end;
550                 }
551         }
552
553         dst->errors |= src->errors;
554         if (src->found_inode_item) {
555                 if (!dst->found_inode_item) {
556                         dst->nlink = src->nlink;
557                         dst->isize = src->isize;
558                         dst->nbytes = src->nbytes;
559                         dst->imode = src->imode;
560                         dst->nodatasum = src->nodatasum;
561                         dst->found_inode_item = 1;
562                 } else {
563                         dst->errors |= I_ERR_DUP_INODE_ITEM;
564                 }
565         }
566         dst->merging = 0;
567
568         return 0;
569 }
570
571 static int splice_shared_node(struct shared_node *src_node,
572                               struct shared_node *dst_node)
573 {
574         struct cache_extent *cache;
575         struct ptr_node *node, *ins;
576         struct cache_tree *src, *dst;
577         struct inode_record *rec, *conflict;
578         u64 current_ino = 0;
579         int splice = 0;
580         int ret;
581
582         if (--src_node->refs == 0)
583                 splice = 1;
584         if (src_node->current)
585                 current_ino = src_node->current->ino;
586
587         src = &src_node->root_cache;
588         dst = &dst_node->root_cache;
589 again:
590         cache = find_first_cache_extent(src, 0);
591         while (cache) {
592                 node = container_of(cache, struct ptr_node, cache);
593                 rec = node->data;
594                 cache = next_cache_extent(cache);
595
596                 if (splice) {
597                         remove_cache_extent(src, &node->cache);
598                         ins = node;
599                 } else {
600                         ins = malloc(sizeof(*ins));
601                         ins->cache.start = node->cache.start;
602                         ins->cache.size = node->cache.size;
603                         ins->data = rec;
604                         rec->refs++;
605                 }
606                 ret = insert_existing_cache_extent(dst, &ins->cache);
607                 if (ret == -EEXIST) {
608                         conflict = get_inode_rec(dst, rec->ino, 1);
609                         merge_inode_recs(rec, conflict, dst);
610                         if (rec->checked) {
611                                 conflict->checked = 1;
612                                 if (dst_node->current == conflict)
613                                         dst_node->current = NULL;
614                         }
615                         maybe_free_inode_rec(dst, conflict);
616                         free_inode_rec(rec);
617                         free(ins);
618                 } else {
619                         BUG_ON(ret);
620                 }
621         }
622
623         if (src == &src_node->root_cache) {
624                 src = &src_node->inode_cache;
625                 dst = &dst_node->inode_cache;
626                 goto again;
627         }
628
629         if (current_ino > 0 && (!dst_node->current ||
630             current_ino > dst_node->current->ino)) {
631                 if (dst_node->current) {
632                         dst_node->current->checked = 1;
633                         maybe_free_inode_rec(dst, dst_node->current);
634                 }
635                 dst_node->current = get_inode_rec(dst, current_ino, 1);
636         }
637         return 0;
638 }
639
640 static void free_inode_recs(struct cache_tree *inode_cache)
641 {
642         struct cache_extent *cache;
643         struct ptr_node *node;
644         struct inode_record *rec;
645
646         while (1) {
647                 cache = find_first_cache_extent(inode_cache, 0);
648                 if (!cache)
649                         break;
650                 node = container_of(cache, struct ptr_node, cache);
651                 rec = node->data;
652                 remove_cache_extent(inode_cache, &node->cache);
653                 free(node);
654                 free_inode_rec(rec);
655         }
656 }
657
658 static struct shared_node *find_shared_node(struct cache_tree *shared,
659                                             u64 bytenr)
660 {
661         struct cache_extent *cache;
662         struct shared_node *node;
663
664         cache = find_cache_extent(shared, bytenr, 1);
665         if (cache) {
666                 node = container_of(cache, struct shared_node, cache);
667                 return node;
668         }
669         return NULL;
670 }
671
672 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
673 {
674         int ret;
675         struct shared_node *node;
676
677         node = calloc(1, sizeof(*node));
678         node->cache.start = bytenr;
679         node->cache.size = 1;
680         cache_tree_init(&node->root_cache);
681         cache_tree_init(&node->inode_cache);
682         node->refs = refs;
683
684         ret = insert_existing_cache_extent(shared, &node->cache);
685         BUG_ON(ret);
686         return 0;
687 }
688
689 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
690                              struct walk_control *wc, int level)
691 {
692         struct shared_node *node;
693         struct shared_node *dest;
694
695         if (level == wc->active_node)
696                 return 0;
697
698         BUG_ON(wc->active_node <= level);
699         node = find_shared_node(&wc->shared, bytenr);
700         if (!node) {
701                 add_shared_node(&wc->shared, bytenr, refs);
702                 node = find_shared_node(&wc->shared, bytenr);
703                 wc->nodes[level] = node;
704                 wc->active_node = level;
705                 return 0;
706         }
707
708         if (wc->root_level == wc->active_node &&
709             btrfs_root_refs(&root->root_item) == 0) {
710                 if (--node->refs == 0) {
711                         free_inode_recs(&node->root_cache);
712                         free_inode_recs(&node->inode_cache);
713                         remove_cache_extent(&wc->shared, &node->cache);
714                         free(node);
715                 }
716                 return 1;
717         }
718
719         dest = wc->nodes[wc->active_node];
720         splice_shared_node(node, dest);
721         if (node->refs == 0) {
722                 remove_cache_extent(&wc->shared, &node->cache);
723                 free(node);
724         }
725         return 1;
726 }
727
728 static int leave_shared_node(struct btrfs_root *root,
729                              struct walk_control *wc, int level)
730 {
731         struct shared_node *node;
732         struct shared_node *dest;
733         int i;
734
735         if (level == wc->root_level)
736                 return 0;
737
738         for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
739                 if (wc->nodes[i])
740                         break;
741         }
742         BUG_ON(i >= BTRFS_MAX_LEVEL);
743
744         node = wc->nodes[wc->active_node];
745         wc->nodes[wc->active_node] = NULL;
746         wc->active_node = i;
747
748         dest = wc->nodes[wc->active_node];
749         if (wc->active_node < wc->root_level ||
750             btrfs_root_refs(&root->root_item) > 0) {
751                 BUG_ON(node->refs <= 1);
752                 splice_shared_node(node, dest);
753         } else {
754                 BUG_ON(node->refs < 2);
755                 node->refs--;
756         }
757         return 0;
758 }
759
760 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
761                          u64 child_root_id)
762 {
763         struct btrfs_path path;
764         struct btrfs_key key;
765         struct extent_buffer *leaf;
766         int has_parent = 0;
767         int ret;
768
769         btrfs_init_path(&path);
770
771         key.objectid = parent_root_id;
772         key.type = BTRFS_ROOT_REF_KEY;
773         key.offset = child_root_id;
774         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
775                                 0, 0);
776         BUG_ON(ret < 0);
777         btrfs_release_path(root, &path);
778         if (!ret)
779                 return 1;
780
781         key.objectid = child_root_id;
782         key.type = BTRFS_ROOT_BACKREF_KEY;
783         key.offset = 0;
784         ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
785                                 0, 0);
786         BUG_ON(ret <= 0);
787
788         while (1) {
789                 leaf = path.nodes[0];
790                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
791                         ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
792                         BUG_ON(ret < 0);
793
794                         if (ret > 0)
795                                 break;
796                         leaf = path.nodes[0];
797                 }
798
799                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
800                 if (key.objectid != child_root_id ||
801                     key.type != BTRFS_ROOT_BACKREF_KEY)
802                         break;
803
804                 has_parent = 1;
805
806                 if (key.offset == parent_root_id) {
807                         btrfs_release_path(root, &path);
808                         return 1;
809                 }
810
811                 path.slots[0]++;
812         }
813
814         btrfs_release_path(root, &path);
815         return has_parent? 0 : -1;
816 }
817
818 static int process_dir_item(struct btrfs_root *root,
819                             struct extent_buffer *eb,
820                             int slot, struct btrfs_key *key,
821                             struct shared_node *active_node)
822 {
823         u32 total;
824         u32 cur = 0;
825         u32 len;
826         u32 name_len;
827         u32 data_len;
828         int error;
829         int nritems = 0;
830         int filetype;
831         struct btrfs_dir_item *di;
832         struct inode_record *rec;
833         struct cache_tree *root_cache;
834         struct cache_tree *inode_cache;
835         struct btrfs_key location;
836         char namebuf[BTRFS_NAME_LEN];
837
838         root_cache = &active_node->root_cache;
839         inode_cache = &active_node->inode_cache;
840         rec = active_node->current;
841         rec->found_dir_item = 1;
842
843         di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
844         total = btrfs_item_size_nr(eb, slot);
845         while (cur < total) {
846                 nritems++;
847                 btrfs_dir_item_key_to_cpu(eb, di, &location);
848                 name_len = btrfs_dir_name_len(eb, di);
849                 data_len = btrfs_dir_data_len(eb, di);
850                 filetype = btrfs_dir_type(eb, di);
851
852                 rec->found_size += name_len;
853                 if (name_len <= BTRFS_NAME_LEN) {
854                         len = name_len;
855                         error = 0;
856                 } else {
857                         len = BTRFS_NAME_LEN;
858                         error = REF_ERR_NAME_TOO_LONG;
859                 }
860                 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
861
862                 if (location.type == BTRFS_INODE_ITEM_KEY) {
863                         add_inode_backref(inode_cache, location.objectid,
864                                           key->objectid, key->offset, namebuf,
865                                           len, filetype, key->type, error);
866                 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
867                         add_inode_backref(root_cache, location.objectid,
868                                           key->objectid, key->offset,
869                                           namebuf, len, filetype,
870                                           key->type, error);
871                 } else {
872                         fprintf(stderr, "warning line %d\n", __LINE__);
873                 }
874
875                 len = sizeof(*di) + name_len + data_len;
876                 di = (struct btrfs_dir_item *)((char *)di + len);
877                 cur += len;
878         }
879         if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
880                 rec->errors |= I_ERR_DUP_DIR_INDEX;
881
882         return 0;
883 }
884
885 static int process_inode_ref(struct extent_buffer *eb,
886                              int slot, struct btrfs_key *key,
887                              struct shared_node *active_node)
888 {
889         u32 total;
890         u32 cur = 0;
891         u32 len;
892         u32 name_len;
893         u64 index;
894         int error;
895         struct cache_tree *inode_cache;
896         struct btrfs_inode_ref *ref;
897         char namebuf[BTRFS_NAME_LEN];
898
899         inode_cache = &active_node->inode_cache;
900
901         ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
902         total = btrfs_item_size_nr(eb, slot);
903         while (cur < total) {
904                 name_len = btrfs_inode_ref_name_len(eb, ref);
905                 index = btrfs_inode_ref_index(eb, ref);
906                 if (name_len <= BTRFS_NAME_LEN) {
907                         len = name_len;
908                         error = 0;
909                 } else {
910                         len = BTRFS_NAME_LEN;
911                         error = REF_ERR_NAME_TOO_LONG;
912                 }
913                 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
914                 add_inode_backref(inode_cache, key->objectid, key->offset,
915                                   index, namebuf, len, 0, key->type, error);
916
917                 len = sizeof(*ref) + name_len;
918                 ref = (struct btrfs_inode_ref *)((char *)ref + len);
919                 cur += len;
920         }
921         return 0;
922 }
923
924 static int process_inode_extref(struct extent_buffer *eb,
925                                 int slot, struct btrfs_key *key,
926                                 struct shared_node *active_node)
927 {
928         u32 total;
929         u32 cur = 0;
930         u32 len;
931         u32 name_len;
932         u64 index;
933         u64 parent;
934         int error;
935         struct cache_tree *inode_cache;
936         struct btrfs_inode_extref *extref;
937         char namebuf[BTRFS_NAME_LEN];
938
939         inode_cache = &active_node->inode_cache;
940
941         extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
942         total = btrfs_item_size_nr(eb, slot);
943         while (cur < total) {
944                 name_len = btrfs_inode_extref_name_len(eb, extref);
945                 index = btrfs_inode_extref_index(eb, extref);
946                 parent = btrfs_inode_extref_parent(eb, extref);
947                 if (name_len <= BTRFS_NAME_LEN) {
948                         len = name_len;
949                         error = 0;
950                 } else {
951                         len = BTRFS_NAME_LEN;
952                         error = REF_ERR_NAME_TOO_LONG;
953                 }
954                 read_extent_buffer(eb, namebuf,
955                                    (unsigned long)(extref + 1), len);
956                 add_inode_backref(inode_cache, key->objectid, parent,
957                                   index, namebuf, len, 0, key->type, error);
958
959                 len = sizeof(*extref) + name_len;
960                 extref = (struct btrfs_inode_extref *)((char *)extref + len);
961                 cur += len;
962         }
963         return 0;
964
965 }
966
967 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
968 {
969         struct btrfs_key key;
970         struct btrfs_path path;
971         struct extent_buffer *leaf;
972         int ret ;
973         size_t size;
974         u64 found = 0;
975         u64 csum_end;
976         u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
977
978         btrfs_init_path(&path);
979
980         key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
981         key.offset = start;
982         key.type = BTRFS_EXTENT_CSUM_KEY;
983
984         ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
985                                 &key, &path, 0, 0);
986         BUG_ON(ret < 0);
987         if (ret > 0 && path.slots[0] > 0) {
988                 leaf = path.nodes[0];
989                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
990                 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
991                     key.type == BTRFS_EXTENT_CSUM_KEY)
992                         path.slots[0]--;
993         }
994
995         while (len > 0) {
996                 leaf = path.nodes[0];
997                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
998                         ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
999                         BUG_ON(ret < 0);
1000                         if (ret > 0)
1001                                 break;
1002                         leaf = path.nodes[0];
1003                 }
1004
1005                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1006                 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1007                     key.type != BTRFS_EXTENT_CSUM_KEY)
1008                         break;
1009
1010                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1011                 if (key.offset >= start + len)
1012                         break;
1013
1014                 if (key.offset > start)
1015                         start = key.offset;
1016
1017                 size = btrfs_item_size_nr(leaf, path.slots[0]);
1018                 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1019                 if (csum_end > start) {
1020                         size = min(csum_end - start, len);
1021                         len -= size;
1022                         start += size;
1023                         found += size;
1024                 }
1025
1026                 path.slots[0]++;
1027         }
1028         btrfs_release_path(root->fs_info->csum_root, &path);
1029         return found;
1030 }
1031
1032 static int process_file_extent(struct btrfs_root *root,
1033                                 struct extent_buffer *eb,
1034                                 int slot, struct btrfs_key *key,
1035                                 struct shared_node *active_node)
1036 {
1037         struct inode_record *rec;
1038         struct btrfs_file_extent_item *fi;
1039         u64 num_bytes = 0;
1040         u64 disk_bytenr = 0;
1041         u64 extent_offset = 0;
1042         u64 mask = root->sectorsize - 1;
1043         int extent_type;
1044
1045         rec = active_node->current;
1046         BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1047         rec->found_file_extent = 1;
1048
1049         if (rec->extent_start == (u64)-1) {
1050                 rec->extent_start = key->offset;
1051                 rec->extent_end = key->offset;
1052         }
1053
1054         if (rec->extent_end > key->offset)
1055                 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1056         else if (rec->extent_end < key->offset &&
1057                  rec->extent_end < rec->first_extent_gap)
1058                 rec->first_extent_gap = rec->extent_end;
1059
1060         fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1061         extent_type = btrfs_file_extent_type(eb, fi);
1062
1063         if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1064                 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1065                 if (num_bytes == 0)
1066                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1067                 rec->found_size += num_bytes;
1068                 num_bytes = (num_bytes + mask) & ~mask;
1069         } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1070                    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1071                 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1072                 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1073                 extent_offset = btrfs_file_extent_offset(eb, fi);
1074                 if (num_bytes == 0 || (num_bytes & mask))
1075                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1076                 if (num_bytes + extent_offset >
1077                     btrfs_file_extent_ram_bytes(eb, fi))
1078                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1079                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1080                     (btrfs_file_extent_compression(eb, fi) ||
1081                      btrfs_file_extent_encryption(eb, fi) ||
1082                      btrfs_file_extent_other_encoding(eb, fi)))
1083                         rec->errors |= I_ERR_BAD_FILE_EXTENT;
1084                 if (disk_bytenr > 0)
1085                         rec->found_size += num_bytes;
1086         } else {
1087                 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1088         }
1089         rec->extent_end = key->offset + num_bytes;
1090
1091         if (disk_bytenr > 0) {
1092                 u64 found;
1093                 if (btrfs_file_extent_compression(eb, fi))
1094                         num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1095                 else
1096                         disk_bytenr += extent_offset;
1097
1098                 found = count_csum_range(root, disk_bytenr, num_bytes);
1099                 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1100                         if (found > 0)
1101                                 rec->found_csum_item = 1;
1102                         if (found < num_bytes)
1103                                 rec->some_csum_missing = 1;
1104                 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1105                         if (found > 0)
1106                                 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1107                 }
1108         }
1109         return 0;
1110 }
1111
1112 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1113                             struct walk_control *wc)
1114 {
1115         struct btrfs_key key;
1116         u32 nritems;
1117         int i;
1118         int ret = 0;
1119         struct cache_tree *inode_cache;
1120         struct shared_node *active_node;
1121
1122         if (wc->root_level == wc->active_node &&
1123             btrfs_root_refs(&root->root_item) == 0)
1124                 return 0;
1125
1126         active_node = wc->nodes[wc->active_node];
1127         inode_cache = &active_node->inode_cache;
1128         nritems = btrfs_header_nritems(eb);
1129         for (i = 0; i < nritems; i++) {
1130                 btrfs_item_key_to_cpu(eb, &key, i);
1131
1132                 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1133                         continue;
1134
1135                 if (active_node->current == NULL ||
1136                     active_node->current->ino < key.objectid) {
1137                         if (active_node->current) {
1138                                 active_node->current->checked = 1;
1139                                 maybe_free_inode_rec(inode_cache,
1140                                                      active_node->current);
1141                         }
1142                         active_node->current = get_inode_rec(inode_cache,
1143                                                              key.objectid, 1);
1144                 }
1145                 switch (key.type) {
1146                 case BTRFS_DIR_ITEM_KEY:
1147                 case BTRFS_DIR_INDEX_KEY:
1148                         ret = process_dir_item(root, eb, i, &key, active_node);
1149                         break;
1150                 case BTRFS_INODE_REF_KEY:
1151                         ret = process_inode_ref(eb, i, &key, active_node);
1152                         break;
1153                 case BTRFS_INODE_EXTREF_KEY:
1154                         ret = process_inode_extref(eb, i, &key, active_node);
1155                         break;
1156                 case BTRFS_INODE_ITEM_KEY:
1157                         ret = process_inode_item(eb, i, &key, active_node);
1158                         break;
1159                 case BTRFS_EXTENT_DATA_KEY:
1160                         ret = process_file_extent(root, eb, i, &key,
1161                                                   active_node);
1162                         break;
1163                 default:
1164                         break;
1165                 };
1166         }
1167         return ret;
1168 }
1169
1170 static void reada_walk_down(struct btrfs_root *root,
1171                             struct extent_buffer *node, int slot)
1172 {
1173         u64 bytenr;
1174         u64 ptr_gen;
1175         u32 nritems;
1176         u32 blocksize;
1177         int i;
1178         int ret;
1179         int level;
1180
1181         level = btrfs_header_level(node);
1182         if (level != 1)
1183                 return;
1184
1185         nritems = btrfs_header_nritems(node);
1186         blocksize = btrfs_level_size(root, level - 1);
1187         for (i = slot; i < nritems; i++) {
1188                 bytenr = btrfs_node_blockptr(node, i);
1189                 ptr_gen = btrfs_node_ptr_generation(node, i);
1190                 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1191                 if (ret)
1192                         break;
1193         }
1194 }
1195
1196 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1197                           struct walk_control *wc, int *level)
1198 {
1199         u64 bytenr;
1200         u64 ptr_gen;
1201         struct extent_buffer *next;
1202         struct extent_buffer *cur;
1203         u32 blocksize;
1204         int ret;
1205         u64 refs;
1206
1207         WARN_ON(*level < 0);
1208         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1209         ret = btrfs_lookup_extent_info(NULL, root,
1210                                        path->nodes[*level]->start,
1211                                        path->nodes[*level]->len, &refs, NULL);
1212         if (ret < 0)
1213                 goto out;
1214
1215         if (refs > 1) {
1216                 ret = enter_shared_node(root, path->nodes[*level]->start,
1217                                         refs, wc, *level);
1218                 if (ret > 0)
1219                         goto out;
1220         }
1221
1222         while (*level >= 0) {
1223                 WARN_ON(*level < 0);
1224                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1225                 cur = path->nodes[*level];
1226
1227                 if (btrfs_header_level(cur) != *level)
1228                         WARN_ON(1);
1229
1230                 if (path->slots[*level] >= btrfs_header_nritems(cur))
1231                         break;
1232                 if (*level == 0) {
1233                         ret = process_one_leaf(root, cur, wc);
1234                         break;
1235                 }
1236                 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1237                 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1238                 blocksize = btrfs_level_size(root, *level - 1);
1239                 ret = btrfs_lookup_extent_info(NULL, root, bytenr, blocksize,
1240                                                &refs, NULL);
1241                 if (ret < 0)
1242                         refs = 0;
1243
1244                 if (refs > 1) {
1245                         ret = enter_shared_node(root, bytenr, refs,
1246                                                 wc, *level - 1);
1247                         if (ret > 0) {
1248                                 path->slots[*level]++;
1249                                 continue;
1250                         }
1251                 }
1252
1253                 next = btrfs_find_tree_block(root, bytenr, blocksize);
1254                 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1255                         free_extent_buffer(next);
1256                         reada_walk_down(root, cur, path->slots[*level]);
1257                         next = read_tree_block(root, bytenr, blocksize,
1258                                                ptr_gen);
1259                 }
1260
1261                 *level = *level - 1;
1262                 free_extent_buffer(path->nodes[*level]);
1263                 path->nodes[*level] = next;
1264                 path->slots[*level] = 0;
1265         }
1266 out:
1267         path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1268         return 0;
1269 }
1270
1271 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1272                         struct walk_control *wc, int *level)
1273 {
1274         int i;
1275         struct extent_buffer *leaf;
1276
1277         for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1278                 leaf = path->nodes[i];
1279                 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1280                         path->slots[i]++;
1281                         *level = i;
1282                         return 0;
1283                 } else {
1284                         free_extent_buffer(path->nodes[*level]);
1285                         path->nodes[*level] = NULL;
1286                         BUG_ON(*level > wc->active_node);
1287                         if (*level == wc->active_node)
1288                                 leave_shared_node(root, wc, *level);
1289                         *level = i + 1;
1290                 }
1291         }
1292         return 1;
1293 }
1294
1295 static int check_root_dir(struct inode_record *rec)
1296 {
1297         struct inode_backref *backref;
1298         int ret = -1;
1299
1300         if (!rec->found_inode_item || rec->errors)
1301                 goto out;
1302         if (rec->nlink != 1 || rec->found_link != 0)
1303                 goto out;
1304         if (list_empty(&rec->backrefs))
1305                 goto out;
1306         backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1307         if (!backref->found_inode_ref)
1308                 goto out;
1309         if (backref->index != 0 || backref->namelen != 2 ||
1310             memcmp(backref->name, "..", 2))
1311                 goto out;
1312         if (backref->found_dir_index || backref->found_dir_item)
1313                 goto out;
1314         ret = 0;
1315 out:
1316         return ret;
1317 }
1318
1319 static int check_inode_recs(struct btrfs_root *root,
1320                             struct cache_tree *inode_cache)
1321 {
1322         struct cache_extent *cache;
1323         struct ptr_node *node;
1324         struct inode_record *rec;
1325         struct inode_backref *backref;
1326         int ret;
1327         u64 error = 0;
1328         u64 root_dirid = btrfs_root_dirid(&root->root_item);
1329
1330         if (btrfs_root_refs(&root->root_item) == 0) {
1331                 if (!cache_tree_empty(inode_cache))
1332                         fprintf(stderr, "warning line %d\n", __LINE__);
1333                 return 0;
1334         }
1335
1336         rec = get_inode_rec(inode_cache, root_dirid, 0);
1337         if (rec) {
1338                 ret = check_root_dir(rec);
1339                 if (ret) {
1340                         fprintf(stderr, "root %llu root dir %llu error\n",
1341                                 (unsigned long long)root->root_key.objectid,
1342                                 (unsigned long long)root_dirid);
1343                         error++;
1344                 }
1345         } else {
1346                 fprintf(stderr, "root %llu root dir %llu not found\n",
1347                         (unsigned long long)root->root_key.objectid,
1348                         (unsigned long long)root_dirid);
1349         }
1350
1351         while (1) {
1352                 cache = find_first_cache_extent(inode_cache, 0);
1353                 if (!cache)
1354                         break;
1355                 node = container_of(cache, struct ptr_node, cache);
1356                 rec = node->data;
1357                 remove_cache_extent(inode_cache, &node->cache);
1358                 free(node);
1359                 if (rec->ino == root_dirid ||
1360                     rec->ino == BTRFS_ORPHAN_OBJECTID) {
1361                         free_inode_rec(rec);
1362                         continue;
1363                 }
1364
1365                 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1366                         ret = check_orphan_item(root, rec->ino);
1367                         if (ret == 0)
1368                                 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1369                         if (can_free_inode_rec(rec)) {
1370                                 free_inode_rec(rec);
1371                                 continue;
1372                         }
1373                 }
1374
1375                 error++;
1376                 if (!rec->found_inode_item)
1377                         rec->errors |= I_ERR_NO_INODE_ITEM;
1378                 if (rec->found_link != rec->nlink)
1379                         rec->errors |= I_ERR_LINK_COUNT_WRONG;
1380                 fprintf(stderr, "root %llu inode %llu errors %x\n",
1381                         (unsigned long long) root->root_key.objectid,
1382                         (unsigned long long) rec->ino, rec->errors);
1383                 list_for_each_entry(backref, &rec->backrefs, list) {
1384                         if (!backref->found_dir_item)
1385                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
1386                         if (!backref->found_dir_index)
1387                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
1388                         if (!backref->found_inode_ref)
1389                                 backref->errors |= REF_ERR_NO_INODE_REF;
1390                         fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1391                                 " namelen %u name %s filetype %d error %x\n",
1392                                 (unsigned long long)backref->dir,
1393                                 (unsigned long long)backref->index,
1394                                 backref->namelen, backref->name,
1395                                 backref->filetype, backref->errors);
1396                 }
1397                 free_inode_rec(rec);
1398         }
1399         return (error > 0) ? -1 : 0;
1400 }
1401
1402 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1403                                         u64 objectid)
1404 {
1405         struct cache_extent *cache;
1406         struct root_record *rec = NULL;
1407         int ret;
1408
1409         cache = find_cache_extent(root_cache, objectid, 1);
1410         if (cache) {
1411                 rec = container_of(cache, struct root_record, cache);
1412         } else {
1413                 rec = calloc(1, sizeof(*rec));
1414                 rec->objectid = objectid;
1415                 INIT_LIST_HEAD(&rec->backrefs);
1416                 rec->cache.start = objectid;
1417                 rec->cache.size = 1;
1418
1419                 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1420                 BUG_ON(ret);
1421         }
1422         return rec;
1423 }
1424
1425 static struct root_backref *get_root_backref(struct root_record *rec,
1426                                              u64 ref_root, u64 dir, u64 index,
1427                                              const char *name, int namelen)
1428 {
1429         struct root_backref *backref;
1430
1431         list_for_each_entry(backref, &rec->backrefs, list) {
1432                 if (backref->ref_root != ref_root || backref->dir != dir ||
1433                     backref->namelen != namelen)
1434                         continue;
1435                 if (memcmp(name, backref->name, namelen))
1436                         continue;
1437                 return backref;
1438         }
1439
1440         backref = malloc(sizeof(*backref) + namelen + 1);
1441         memset(backref, 0, sizeof(*backref));
1442         backref->ref_root = ref_root;
1443         backref->dir = dir;
1444         backref->index = index;
1445         backref->namelen = namelen;
1446         memcpy(backref->name, name, namelen);
1447         backref->name[namelen] = '\0';
1448         list_add_tail(&backref->list, &rec->backrefs);
1449         return backref;
1450 }
1451
1452 static void free_root_recs(struct cache_tree *root_cache)
1453 {
1454         struct cache_extent *cache;
1455         struct root_record *rec;
1456         struct root_backref *backref;
1457
1458         while (1) {
1459                 cache = find_first_cache_extent(root_cache, 0);
1460                 if (!cache)
1461                         break;
1462                 rec = container_of(cache, struct root_record, cache);
1463                 remove_cache_extent(root_cache, &rec->cache);
1464
1465                 while (!list_empty(&rec->backrefs)) {
1466                         backref = list_entry(rec->backrefs.next,
1467                                              struct root_backref, list);
1468                         list_del(&backref->list);
1469                         free(backref);
1470                 }
1471                 kfree(rec);
1472         }
1473 }
1474
1475 static int add_root_backref(struct cache_tree *root_cache,
1476                             u64 root_id, u64 ref_root, u64 dir, u64 index,
1477                             const char *name, int namelen,
1478                             int item_type, int errors)
1479 {
1480         struct root_record *rec;
1481         struct root_backref *backref;
1482
1483         rec = get_root_rec(root_cache, root_id);
1484         backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1485
1486         backref->errors |= errors;
1487
1488         if (item_type != BTRFS_DIR_ITEM_KEY) {
1489                 if (backref->found_dir_index || backref->found_back_ref ||
1490                     backref->found_forward_ref) {
1491                         if (backref->index != index)
1492                                 backref->errors |= REF_ERR_INDEX_UNMATCH;
1493                 } else {
1494                         backref->index = index;
1495                 }
1496         }
1497
1498         if (item_type == BTRFS_DIR_ITEM_KEY) {
1499                 backref->found_dir_item = 1;
1500                 backref->reachable = 1;
1501                 rec->found_ref++;
1502         } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1503                 backref->found_dir_index = 1;
1504         } else if (item_type == BTRFS_ROOT_REF_KEY) {
1505                 if (backref->found_forward_ref)
1506                         backref->errors |= REF_ERR_DUP_ROOT_REF;
1507                 backref->found_forward_ref = 1;
1508         } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1509                 if (backref->found_back_ref)
1510                         backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1511                 backref->found_back_ref = 1;
1512         } else {
1513                 BUG_ON(1);
1514         }
1515
1516         return 0;
1517 }
1518
1519 static int merge_root_recs(struct btrfs_root *root,
1520                            struct cache_tree *src_cache,
1521                            struct cache_tree *dst_cache)
1522 {
1523         struct cache_extent *cache;
1524         struct ptr_node *node;
1525         struct inode_record *rec;
1526         struct inode_backref *backref;
1527
1528         if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1529                 free_inode_recs(src_cache);
1530                 return 0;
1531         }
1532
1533         while (1) {
1534                 cache = find_first_cache_extent(src_cache, 0);
1535                 if (!cache)
1536                         break;
1537                 node = container_of(cache, struct ptr_node, cache);
1538                 rec = node->data;
1539                 remove_cache_extent(src_cache, &node->cache);
1540                 free(node);
1541
1542                 if (!is_child_root(root, root->objectid, rec->ino))
1543                         goto skip;
1544
1545                 list_for_each_entry(backref, &rec->backrefs, list) {
1546                         BUG_ON(backref->found_inode_ref);
1547                         if (backref->found_dir_item)
1548                                 add_root_backref(dst_cache, rec->ino,
1549                                         root->root_key.objectid, backref->dir,
1550                                         backref->index, backref->name,
1551                                         backref->namelen, BTRFS_DIR_ITEM_KEY,
1552                                         backref->errors);
1553                         if (backref->found_dir_index)
1554                                 add_root_backref(dst_cache, rec->ino,
1555                                         root->root_key.objectid, backref->dir,
1556                                         backref->index, backref->name,
1557                                         backref->namelen, BTRFS_DIR_INDEX_KEY,
1558                                         backref->errors);
1559                 }
1560 skip:
1561                 free_inode_rec(rec);
1562         }
1563         return 0;
1564 }
1565
1566 static int check_root_refs(struct btrfs_root *root,
1567                            struct cache_tree *root_cache)
1568 {
1569         struct root_record *rec;
1570         struct root_record *ref_root;
1571         struct root_backref *backref;
1572         struct cache_extent *cache;
1573         int loop = 1;
1574         int ret;
1575         int error;
1576         int errors = 0;
1577
1578         rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1579         rec->found_ref = 1;
1580
1581         /* fixme: this can not detect circular references */
1582         while (loop) {
1583                 loop = 0;
1584                 cache = find_first_cache_extent(root_cache, 0);
1585                 while (1) {
1586                         if (!cache)
1587                                 break;
1588                         rec = container_of(cache, struct root_record, cache);
1589                         cache = next_cache_extent(cache);
1590
1591                         if (rec->found_ref == 0)
1592                                 continue;
1593
1594                         list_for_each_entry(backref, &rec->backrefs, list) {
1595                                 if (!backref->reachable)
1596                                         continue;
1597
1598                                 ref_root = get_root_rec(root_cache,
1599                                                         backref->ref_root);
1600                                 if (ref_root->found_ref > 0)
1601                                         continue;
1602
1603                                 backref->reachable = 0;
1604                                 rec->found_ref--;
1605                                 if (rec->found_ref == 0)
1606                                         loop = 1;
1607                         }
1608                 }
1609         }
1610
1611         cache = find_first_cache_extent(root_cache, 0);
1612         while (1) {
1613                 if (!cache)
1614                         break;
1615                 rec = container_of(cache, struct root_record, cache);
1616                 cache = next_cache_extent(cache);
1617
1618                 if (rec->found_ref == 0 &&
1619                     rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1620                     rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1621                         ret = check_orphan_item(root->fs_info->tree_root,
1622                                                 rec->objectid);
1623                         if (ret == 0)
1624                                 continue;
1625                         errors++;
1626                         fprintf(stderr, "fs tree %llu not referenced\n",
1627                                 (unsigned long long)rec->objectid);
1628                 }
1629
1630                 error = 0;
1631                 if (rec->found_ref > 0 && !rec->found_root_item)
1632                         error = 1;
1633                 list_for_each_entry(backref, &rec->backrefs, list) {
1634                         if (!backref->found_dir_item)
1635                                 backref->errors |= REF_ERR_NO_DIR_ITEM;
1636                         if (!backref->found_dir_index)
1637                                 backref->errors |= REF_ERR_NO_DIR_INDEX;
1638                         if (!backref->found_back_ref)
1639                                 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1640                         if (!backref->found_forward_ref)
1641                                 backref->errors |= REF_ERR_NO_ROOT_REF;
1642                         if (backref->reachable && backref->errors)
1643                                 error = 1;
1644                 }
1645                 if (!error)
1646                         continue;
1647
1648                 errors++;
1649                 fprintf(stderr, "fs tree %llu refs %u %s\n",
1650                         (unsigned long long)rec->objectid, rec->found_ref,
1651                          rec->found_root_item ? "" : "not found");
1652
1653                 list_for_each_entry(backref, &rec->backrefs, list) {
1654                         if (!backref->reachable)
1655                                 continue;
1656                         if (!backref->errors && rec->found_root_item)
1657                                 continue;
1658                         fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1659                                 " index %llu namelen %u name %s error %x\n",
1660                                 (unsigned long long)backref->ref_root,
1661                                 (unsigned long long)backref->dir,
1662                                 (unsigned long long)backref->index,
1663                                 backref->namelen, backref->name,
1664                                 backref->errors);
1665                 }
1666         }
1667         return errors > 0 ? 1 : 0;
1668 }
1669
1670 static int process_root_ref(struct extent_buffer *eb, int slot,
1671                             struct btrfs_key *key,
1672                             struct cache_tree *root_cache)
1673 {
1674         u64 dirid;
1675         u64 index;
1676         u32 len;
1677         u32 name_len;
1678         struct btrfs_root_ref *ref;
1679         char namebuf[BTRFS_NAME_LEN];
1680         int error;
1681
1682         ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1683
1684         dirid = btrfs_root_ref_dirid(eb, ref);
1685         index = btrfs_root_ref_sequence(eb, ref);
1686         name_len = btrfs_root_ref_name_len(eb, ref);
1687
1688         if (name_len <= BTRFS_NAME_LEN) {
1689                 len = name_len;
1690                 error = 0;
1691         } else {
1692                 len = BTRFS_NAME_LEN;
1693                 error = REF_ERR_NAME_TOO_LONG;
1694         }
1695         read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1696
1697         if (key->type == BTRFS_ROOT_REF_KEY) {
1698                 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1699                                  index, namebuf, len, key->type, error);
1700         } else {
1701                 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1702                                  index, namebuf, len, key->type, error);
1703         }
1704         return 0;
1705 }
1706
1707 static int check_fs_root(struct btrfs_root *root,
1708                          struct cache_tree *root_cache,
1709                          struct walk_control *wc)
1710 {
1711         int ret = 0;
1712         int wret;
1713         int level;
1714         struct btrfs_path path;
1715         struct shared_node root_node;
1716         struct root_record *rec;
1717         struct btrfs_root_item *root_item = &root->root_item;
1718
1719         if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1720                 rec = get_root_rec(root_cache, root->root_key.objectid);
1721                 if (btrfs_root_refs(root_item) > 0)
1722                         rec->found_root_item = 1;
1723         }
1724
1725         btrfs_init_path(&path);
1726         memset(&root_node, 0, sizeof(root_node));
1727         cache_tree_init(&root_node.root_cache);
1728         cache_tree_init(&root_node.inode_cache);
1729
1730         level = btrfs_header_level(root->node);
1731         memset(wc->nodes, 0, sizeof(wc->nodes));
1732         wc->nodes[level] = &root_node;
1733         wc->active_node = level;
1734         wc->root_level = level;
1735
1736         if (btrfs_root_refs(root_item) > 0 ||
1737             btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1738                 path.nodes[level] = root->node;
1739                 extent_buffer_get(root->node);
1740                 path.slots[level] = 0;
1741         } else {
1742                 struct btrfs_key key;
1743                 struct btrfs_disk_key found_key;
1744
1745                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1746                 level = root_item->drop_level;
1747                 path.lowest_level = level;
1748                 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1749                 BUG_ON(wret < 0);
1750                 btrfs_node_key(path.nodes[level], &found_key,
1751                                 path.slots[level]);
1752                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1753                                         sizeof(found_key)));
1754         }
1755
1756         while (1) {
1757                 wret = walk_down_tree(root, &path, wc, &level);
1758                 if (wret < 0)
1759                         ret = wret;
1760                 if (wret != 0)
1761                         break;
1762
1763                 wret = walk_up_tree(root, &path, wc, &level);
1764                 if (wret < 0)
1765                         ret = wret;
1766                 if (wret != 0)
1767                         break;
1768         }
1769         btrfs_release_path(root, &path);
1770
1771         merge_root_recs(root, &root_node.root_cache, root_cache);
1772
1773         if (root_node.current) {
1774                 root_node.current->checked = 1;
1775                 maybe_free_inode_rec(&root_node.inode_cache,
1776                                 root_node.current);
1777         }
1778
1779         ret = check_inode_recs(root, &root_node.inode_cache);
1780         return ret;
1781 }
1782
1783 static int fs_root_objectid(u64 objectid)
1784 {
1785         if (objectid == BTRFS_FS_TREE_OBJECTID ||
1786             objectid == BTRFS_TREE_RELOC_OBJECTID ||
1787             objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1788             (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1789              objectid <= BTRFS_LAST_FREE_OBJECTID))
1790                 return 1;
1791         return 0;
1792 }
1793
1794 static int check_fs_roots(struct btrfs_root *root,
1795                           struct cache_tree *root_cache)
1796 {
1797         struct btrfs_path path;
1798         struct btrfs_key key;
1799         struct walk_control wc;
1800         struct extent_buffer *leaf;
1801         struct btrfs_root *tmp_root;
1802         struct btrfs_root *tree_root = root->fs_info->tree_root;
1803         int ret;
1804         int err = 0;
1805
1806         memset(&wc, 0, sizeof(wc));
1807         cache_tree_init(&wc.shared);
1808         btrfs_init_path(&path);
1809
1810         key.offset = 0;
1811         key.objectid = 0;
1812         key.type = BTRFS_ROOT_ITEM_KEY;
1813         ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1814         BUG_ON(ret < 0);
1815         while (1) {
1816                 leaf = path.nodes[0];
1817                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1818                         ret = btrfs_next_leaf(tree_root, &path);
1819                         if (ret != 0)
1820                                 break;
1821                         leaf = path.nodes[0];
1822                 }
1823                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1824                 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1825                     fs_root_objectid(key.objectid)) {
1826                         tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1827                                                                &key);
1828                         if (IS_ERR(tmp_root)) {
1829                                 err = 1;
1830                                 goto next;
1831                         }
1832                         ret = check_fs_root(tmp_root, root_cache, &wc);
1833                         if (ret)
1834                                 err = 1;
1835                         btrfs_free_fs_root(root->fs_info, tmp_root);
1836                 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1837                            key.type == BTRFS_ROOT_BACKREF_KEY) {
1838                         process_root_ref(leaf, path.slots[0], &key,
1839                                          root_cache);
1840                 }
1841 next:
1842                 path.slots[0]++;
1843         }
1844         btrfs_release_path(tree_root, &path);
1845
1846         if (!cache_tree_empty(&wc.shared))
1847                 fprintf(stderr, "warning line %d\n", __LINE__);
1848
1849         return err;
1850 }
1851
1852 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1853 {
1854         struct list_head *cur = rec->backrefs.next;
1855         struct extent_backref *back;
1856         struct tree_backref *tback;
1857         struct data_backref *dback;
1858         u64 found = 0;
1859         int err = 0;
1860
1861         while(cur != &rec->backrefs) {
1862                 back = list_entry(cur, struct extent_backref, list);
1863                 cur = cur->next;
1864                 if (!back->found_extent_tree) {
1865                         err = 1;
1866                         if (!print_errs)
1867                                 goto out;
1868                         if (back->is_data) {
1869                                 dback = (struct data_backref *)back;
1870                                 fprintf(stderr, "Backref %llu %s %llu"
1871                                         " owner %llu offset %llu num_refs %lu"
1872                                         " not found in extent tree\n",
1873                                         (unsigned long long)rec->start,
1874                                         back->full_backref ?
1875                                         "parent" : "root",
1876                                         back->full_backref ?
1877                                         (unsigned long long)dback->parent:
1878                                         (unsigned long long)dback->root,
1879                                         (unsigned long long)dback->owner,
1880                                         (unsigned long long)dback->offset,
1881                                         (unsigned long)dback->num_refs);
1882                         } else {
1883                                 tback = (struct tree_backref *)back;
1884                                 fprintf(stderr, "Backref %llu parent %llu"
1885                                         " root %llu not found in extent tree\n",
1886                                         (unsigned long long)rec->start,
1887                                         (unsigned long long)tback->parent,
1888                                         (unsigned long long)tback->root);
1889                         }
1890                 }
1891                 if (!back->is_data && !back->found_ref) {
1892                         err = 1;
1893                         if (!print_errs)
1894                                 goto out;
1895                         tback = (struct tree_backref *)back;
1896                         fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1897                                 (unsigned long long)rec->start,
1898                                 back->full_backref ? "parent" : "root",
1899                                 back->full_backref ?
1900                                 (unsigned long long)tback->parent :
1901                                 (unsigned long long)tback->root, back);
1902                 }
1903                 if (back->is_data) {
1904                         dback = (struct data_backref *)back;
1905                         if (dback->found_ref != dback->num_refs) {
1906                                 err = 1;
1907                                 if (!print_errs)
1908                                         goto out;
1909                                 fprintf(stderr, "Incorrect local backref count"
1910                                         " on %llu %s %llu owner %llu"
1911                                         " offset %llu found %u wanted %u back %p\n",
1912                                         (unsigned long long)rec->start,
1913                                         back->full_backref ?
1914                                         "parent" : "root",
1915                                         back->full_backref ? 
1916                                         (unsigned long long)dback->parent:
1917                                         (unsigned long long)dback->root,
1918                                         (unsigned long long)dback->owner,
1919                                         (unsigned long long)dback->offset,
1920                                         dback->found_ref, dback->num_refs, back);
1921                         }
1922                 }
1923                 if (!back->is_data) {
1924                         found += 1;
1925                 } else {
1926                         dback = (struct data_backref *)back;
1927                         found += dback->found_ref;
1928                 }
1929         }
1930         if (found != rec->refs) {
1931                 err = 1;
1932                 if (!print_errs)
1933                         goto out;
1934                 fprintf(stderr, "Incorrect global backref count "
1935                         "on %llu found %llu wanted %llu\n",
1936                         (unsigned long long)rec->start,
1937                         (unsigned long long)found,
1938                         (unsigned long long)rec->refs);
1939         }
1940 out:
1941         return err;
1942 }
1943
1944 static int free_all_extent_backrefs(struct extent_record *rec)
1945 {
1946         struct extent_backref *back;
1947         struct list_head *cur;
1948         while (!list_empty(&rec->backrefs)) {
1949                 cur = rec->backrefs.next;
1950                 back = list_entry(cur, struct extent_backref, list);
1951                 list_del(cur);
1952                 free(back);
1953         }
1954         return 0;
1955 }
1956
1957 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
1958                                  struct extent_record *rec)
1959 {
1960         if (rec->content_checked && rec->owner_ref_checked &&
1961             rec->extent_item_refs == rec->refs && rec->refs > 0 &&
1962             !all_backpointers_checked(rec, 0)) {
1963                 remove_cache_extent(extent_cache, &rec->cache);
1964                 free_all_extent_backrefs(rec);
1965                 free(rec);
1966         }
1967         return 0;
1968 }
1969
1970 static int check_owner_ref(struct btrfs_root *root,
1971                             struct extent_record *rec,
1972                             struct extent_buffer *buf)
1973 {
1974         struct extent_backref *node;
1975         struct tree_backref *back;
1976         struct btrfs_root *ref_root;
1977         struct btrfs_key key;
1978         struct btrfs_path path;
1979         struct extent_buffer *parent;
1980         int level;
1981         int found = 0;
1982         int ret;
1983
1984         list_for_each_entry(node, &rec->backrefs, list) {
1985                 if (node->is_data)
1986                         continue;
1987                 if (!node->found_ref)
1988                         continue;
1989                 if (node->full_backref)
1990                         continue;
1991                 back = (struct tree_backref *)node;
1992                 if (btrfs_header_owner(buf) == back->root)
1993                         return 0;
1994         }
1995         BUG_ON(rec->is_root);
1996
1997         /* try to find the block by search corresponding fs tree */
1998         key.objectid = btrfs_header_owner(buf);
1999         key.type = BTRFS_ROOT_ITEM_KEY;
2000         key.offset = (u64)-1;
2001
2002         ref_root = btrfs_read_fs_root(root->fs_info, &key);
2003         if (IS_ERR(ref_root))
2004                 return 1;
2005
2006         level = btrfs_header_level(buf);
2007         if (level == 0)
2008                 btrfs_item_key_to_cpu(buf, &key, 0);
2009         else
2010                 btrfs_node_key_to_cpu(buf, &key, 0);
2011
2012         btrfs_init_path(&path);
2013         path.lowest_level = level + 1;
2014         ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2015         if (ret < 0)
2016                 return 0;
2017
2018         parent = path.nodes[level + 1];
2019         if (parent && buf->start == btrfs_node_blockptr(parent,
2020                                                         path.slots[level + 1]))
2021                 found = 1;
2022
2023         btrfs_release_path(ref_root, &path);
2024         return found ? 0 : 1;
2025 }
2026
2027 static int is_extent_tree_record(struct extent_record *rec)
2028 {
2029         struct list_head *cur = rec->backrefs.next;
2030         struct extent_backref *node;
2031         struct tree_backref *back;
2032         int is_extent = 0;
2033
2034         while(cur != &rec->backrefs) {
2035                 node = list_entry(cur, struct extent_backref, list);
2036                 cur = cur->next;
2037                 if (node->is_data)
2038                         return 0;
2039                 back = (struct tree_backref *)node;
2040                 if (node->full_backref)
2041                         return 0;
2042                 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2043                         is_extent = 1;
2044         }
2045         return is_extent;
2046 }
2047
2048
2049 static int record_bad_block_io(struct btrfs_fs_info *info,
2050                                struct cache_tree *extent_cache,
2051                                u64 start, u64 len)
2052 {
2053         struct extent_record *rec;
2054         struct cache_extent *cache;
2055         struct btrfs_key key;
2056
2057         cache = find_cache_extent(extent_cache, start, len);
2058         if (!cache)
2059                 return 0;
2060
2061         rec = container_of(cache, struct extent_record, cache);
2062         if (!is_extent_tree_record(rec))
2063                 return 0;
2064
2065         btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2066         return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2067 }
2068
2069 static int check_block(struct btrfs_root *root,
2070                        struct cache_tree *extent_cache,
2071                        struct extent_buffer *buf, u64 flags)
2072 {
2073         struct extent_record *rec;
2074         struct cache_extent *cache;
2075         struct btrfs_key key;
2076         int ret = 1;
2077         int level;
2078
2079         cache = find_cache_extent(extent_cache, buf->start, buf->len);
2080         if (!cache)
2081                 return 1;
2082         rec = container_of(cache, struct extent_record, cache);
2083         rec->generation = btrfs_header_generation(buf);
2084
2085         level = btrfs_header_level(buf);
2086         if (btrfs_header_nritems(buf) > 0) {
2087
2088                 if (level == 0)
2089                         btrfs_item_key_to_cpu(buf, &key, 0);
2090                 else
2091                         btrfs_node_key_to_cpu(buf, &key, 0);
2092
2093                 rec->info_objectid = key.objectid;
2094         }
2095         rec->info_level = level;
2096
2097         if (btrfs_is_leaf(buf))
2098                 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2099         else
2100                 ret = btrfs_check_node(root, &rec->parent_key, buf);
2101
2102         if (ret) {
2103                 fprintf(stderr, "bad block %llu\n",
2104                         (unsigned long long)buf->start);
2105         } else {
2106                 rec->content_checked = 1;
2107                 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2108                         rec->owner_ref_checked = 1;
2109                 else {
2110                         ret = check_owner_ref(root, rec, buf);
2111                         if (!ret)
2112                                 rec->owner_ref_checked = 1;
2113                 }
2114         }
2115         if (!ret)
2116                 maybe_free_extent_rec(extent_cache, rec);
2117         return ret;
2118 }
2119
2120 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2121                                                 u64 parent, u64 root)
2122 {
2123         struct list_head *cur = rec->backrefs.next;
2124         struct extent_backref *node;
2125         struct tree_backref *back;
2126
2127         while(cur != &rec->backrefs) {
2128                 node = list_entry(cur, struct extent_backref, list);
2129                 cur = cur->next;
2130                 if (node->is_data)
2131                         continue;
2132                 back = (struct tree_backref *)node;
2133                 if (parent > 0) {
2134                         if (!node->full_backref)
2135                                 continue;
2136                         if (parent == back->parent)
2137                                 return back;
2138                 } else {
2139                         if (node->full_backref)
2140                                 continue;
2141                         if (back->root == root)
2142                                 return back;
2143                 }
2144         }
2145         return NULL;
2146 }
2147
2148 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2149                                                 u64 parent, u64 root)
2150 {
2151         struct tree_backref *ref = malloc(sizeof(*ref));
2152         memset(&ref->node, 0, sizeof(ref->node));
2153         if (parent > 0) {
2154                 ref->parent = parent;
2155                 ref->node.full_backref = 1;
2156         } else {
2157                 ref->root = root;
2158                 ref->node.full_backref = 0;
2159         }
2160         list_add_tail(&ref->node.list, &rec->backrefs);
2161
2162         return ref;
2163 }
2164
2165 static struct data_backref *find_data_backref(struct extent_record *rec,
2166                                                 u64 parent, u64 root,
2167                                                 u64 owner, u64 offset)
2168 {
2169         struct list_head *cur = rec->backrefs.next;
2170         struct extent_backref *node;
2171         struct data_backref *back;
2172
2173         while(cur != &rec->backrefs) {
2174                 node = list_entry(cur, struct extent_backref, list);
2175                 cur = cur->next;
2176                 if (!node->is_data)
2177                         continue;
2178                 back = (struct data_backref *)node;
2179                 if (parent > 0) {
2180                         if (!node->full_backref)
2181                                 continue;
2182                         if (parent == back->parent)
2183                                 return back;
2184                 } else {
2185                         if (node->full_backref)
2186                                 continue;
2187                         if (back->root == root && back->owner == owner &&
2188                             back->offset == offset)
2189                                 return back;
2190                 }
2191         }
2192         return NULL;
2193 }
2194
2195 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2196                                                 u64 parent, u64 root,
2197                                                 u64 owner, u64 offset,
2198                                                 u64 max_size)
2199 {
2200         struct data_backref *ref = malloc(sizeof(*ref));
2201         memset(&ref->node, 0, sizeof(ref->node));
2202         ref->node.is_data = 1;
2203
2204         if (parent > 0) {
2205                 ref->parent = parent;
2206                 ref->owner = 0;
2207                 ref->offset = 0;
2208                 ref->node.full_backref = 1;
2209         } else {
2210                 ref->root = root;
2211                 ref->owner = owner;
2212                 ref->offset = offset;
2213                 ref->node.full_backref = 0;
2214         }
2215         ref->found_ref = 0;
2216         ref->num_refs = 0;
2217         list_add_tail(&ref->node.list, &rec->backrefs);
2218         if (max_size > rec->max_size)
2219                 rec->max_size = max_size;
2220         return ref;
2221 }
2222
2223 static int add_extent_rec(struct cache_tree *extent_cache,
2224                           struct btrfs_key *parent_key,
2225                           u64 start, u64 nr, u64 extent_item_refs,
2226                           int is_root, int inc_ref, int set_checked,
2227                           u64 max_size)
2228 {
2229         struct extent_record *rec;
2230         struct cache_extent *cache;
2231         int ret = 0;
2232
2233         cache = find_cache_extent(extent_cache, start, nr);
2234         if (cache) {
2235                 rec = container_of(cache, struct extent_record, cache);
2236                 if (inc_ref)
2237                         rec->refs++;
2238                 if (rec->nr == 1)
2239                         rec->nr = max(nr, max_size);
2240
2241                 if (start != rec->start) {
2242                         fprintf(stderr, "warning, start mismatch %llu %llu\n",
2243                                 (unsigned long long)rec->start,
2244                                 (unsigned long long)start);
2245                         ret = 1;
2246                 }
2247                 if (extent_item_refs) {
2248                         if (rec->extent_item_refs) {
2249                                 fprintf(stderr, "block %llu rec "
2250                                         "extent_item_refs %llu, passed %llu\n",
2251                                         (unsigned long long)start,
2252                                         (unsigned long long)
2253                                                         rec->extent_item_refs,
2254                                         (unsigned long long)extent_item_refs);
2255                         }
2256                         rec->extent_item_refs = extent_item_refs;
2257                 }
2258                 if (is_root)
2259                         rec->is_root = 1;
2260                 if (set_checked) {
2261                         rec->content_checked = 1;
2262                         rec->owner_ref_checked = 1;
2263                 }
2264
2265                 if (parent_key)
2266                         btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2267
2268                 if (rec->max_size < max_size)
2269                         rec->max_size = max_size;
2270
2271                 maybe_free_extent_rec(extent_cache, rec);
2272                 return ret;
2273         }
2274         rec = malloc(sizeof(*rec));
2275         rec->start = start;
2276         rec->max_size = max_size;
2277         rec->nr = max(nr, max_size);
2278         rec->content_checked = 0;
2279         rec->owner_ref_checked = 0;
2280         INIT_LIST_HEAD(&rec->backrefs);
2281
2282         if (is_root)
2283                 rec->is_root = 1;
2284         else
2285                 rec->is_root = 0;
2286
2287         if (inc_ref)
2288                 rec->refs = 1;
2289         else
2290                 rec->refs = 0;
2291
2292         if (extent_item_refs)
2293                 rec->extent_item_refs = extent_item_refs;
2294         else
2295                 rec->extent_item_refs = 0;
2296
2297         if (parent_key)
2298                 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2299         else
2300                 memset(&rec->parent_key, 0, sizeof(*parent_key));
2301
2302         rec->cache.start = start;
2303         rec->cache.size = nr;
2304         ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2305         BUG_ON(ret);
2306         bytes_used += nr;
2307         if (set_checked) {
2308                 rec->content_checked = 1;
2309                 rec->owner_ref_checked = 1;
2310         }
2311         return ret;
2312 }
2313
2314 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2315                             u64 parent, u64 root, int found_ref)
2316 {
2317         struct extent_record *rec;
2318         struct tree_backref *back;
2319         struct cache_extent *cache;
2320
2321         cache = find_cache_extent(extent_cache, bytenr, 1);
2322         if (!cache) {
2323                 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0, 0);
2324                 cache = find_cache_extent(extent_cache, bytenr, 1);
2325                 if (!cache)
2326                         abort();
2327         }
2328
2329         rec = container_of(cache, struct extent_record, cache);
2330         if (rec->start != bytenr) {
2331                 abort();
2332         }
2333
2334         back = find_tree_backref(rec, parent, root);
2335         if (!back)
2336                 back = alloc_tree_backref(rec, parent, root);
2337
2338         if (found_ref) {
2339                 if (back->node.found_ref) {
2340                         fprintf(stderr, "Extent back ref already exists "
2341                                 "for %llu parent %llu root %llu \n",
2342                                 (unsigned long long)bytenr,
2343                                 (unsigned long long)parent,
2344                                 (unsigned long long)root);
2345                 }
2346                 back->node.found_ref = 1;
2347         } else {
2348                 if (back->node.found_extent_tree) {
2349                         fprintf(stderr, "Extent back ref already exists "
2350                                 "for %llu parent %llu root %llu \n",
2351                                 (unsigned long long)bytenr,
2352                                 (unsigned long long)parent,
2353                                 (unsigned long long)root);
2354                 }
2355                 back->node.found_extent_tree = 1;
2356         }
2357         return 0;
2358 }
2359
2360 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2361                             u64 parent, u64 root, u64 owner, u64 offset,
2362                             u32 num_refs, int found_ref, u64 max_size)
2363 {
2364         struct extent_record *rec;
2365         struct data_backref *back;
2366         struct cache_extent *cache;
2367
2368         cache = find_cache_extent(extent_cache, bytenr, 1);
2369         if (!cache) {
2370                 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2371                                max_size);
2372                 cache = find_cache_extent(extent_cache, bytenr, 1);
2373                 if (!cache)
2374                         abort();
2375         }
2376
2377         rec = container_of(cache, struct extent_record, cache);
2378         if (rec->start != bytenr) {
2379                 abort();
2380         }
2381         if (rec->max_size < max_size)
2382                 rec->max_size = max_size;
2383
2384         back = find_data_backref(rec, parent, root, owner, offset);
2385         if (!back)
2386                 back = alloc_data_backref(rec, parent, root, owner, offset,
2387                                           max_size);
2388
2389         if (found_ref) {
2390                 BUG_ON(num_refs != 1);
2391                 back->node.found_ref = 1;
2392                 back->found_ref += 1;
2393         } else {
2394                 if (back->node.found_extent_tree) {
2395                         fprintf(stderr, "Extent back ref already exists "
2396                                 "for %llu parent %llu root %llu"
2397                                 "owner %llu offset %llu num_refs %lu\n",
2398                                 (unsigned long long)bytenr,
2399                                 (unsigned long long)parent,
2400                                 (unsigned long long)root,
2401                                 (unsigned long long)owner,
2402                                 (unsigned long long)offset,
2403                                 (unsigned long)num_refs);
2404                 }
2405                 back->num_refs = num_refs;
2406                 back->node.found_extent_tree = 1;
2407         }
2408         return 0;
2409 }
2410
2411 static int add_pending(struct cache_tree *pending,
2412                        struct cache_tree *seen, u64 bytenr, u32 size)
2413 {
2414         int ret;
2415         ret = insert_cache_extent(seen, bytenr, size);
2416         if (ret)
2417                 return ret;
2418         insert_cache_extent(pending, bytenr, size);
2419         return 0;
2420 }
2421
2422 static int pick_next_pending(struct cache_tree *pending,
2423                         struct cache_tree *reada,
2424                         struct cache_tree *nodes,
2425                         u64 last, struct block_info *bits, int bits_nr,
2426                         int *reada_bits)
2427 {
2428         unsigned long node_start = last;
2429         struct cache_extent *cache;
2430         int ret;
2431
2432         cache = find_first_cache_extent(reada, 0);
2433         if (cache) {
2434                 bits[0].start = cache->start;
2435                 bits[1].size = cache->size;
2436                 *reada_bits = 1;
2437                 return 1;
2438         }
2439         *reada_bits = 0;
2440         if (node_start > 32768)
2441                 node_start -= 32768;
2442
2443         cache = find_first_cache_extent(nodes, node_start);
2444         if (!cache)
2445                 cache = find_first_cache_extent(nodes, 0);
2446
2447         if (!cache) {
2448                  cache = find_first_cache_extent(pending, 0);
2449                  if (!cache)
2450                          return 0;
2451                  ret = 0;
2452                  do {
2453                          bits[ret].start = cache->start;
2454                          bits[ret].size = cache->size;
2455                          cache = next_cache_extent(cache);
2456                          ret++;
2457                  } while (cache && ret < bits_nr);
2458                  return ret;
2459         }
2460
2461         ret = 0;
2462         do {
2463                 bits[ret].start = cache->start;
2464                 bits[ret].size = cache->size;
2465                 cache = next_cache_extent(cache);
2466                 ret++;
2467         } while (cache && ret < bits_nr);
2468
2469         if (bits_nr - ret > 8) {
2470                 u64 lookup = bits[0].start + bits[0].size;
2471                 struct cache_extent *next;
2472                 next = find_first_cache_extent(pending, lookup);
2473                 while(next) {
2474                         if (next->start - lookup > 32768)
2475                                 break;
2476                         bits[ret].start = next->start;
2477                         bits[ret].size = next->size;
2478                         lookup = next->start + next->size;
2479                         ret++;
2480                         if (ret == bits_nr)
2481                                 break;
2482                         next = next_cache_extent(next);
2483                         if (!next)
2484                                 break;
2485                 }
2486         }
2487         return ret;
2488 }
2489
2490 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2491 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2492                                  struct extent_buffer *leaf, int slot)
2493 {
2494         struct btrfs_extent_ref_v0 *ref0;
2495         struct btrfs_key key;
2496
2497         btrfs_item_key_to_cpu(leaf, &key, slot);
2498         ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2499         if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2500                 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2501         } else {
2502                 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2503                                  0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2504         }
2505         return 0;
2506 }
2507 #endif
2508
2509 static int process_extent_item(struct cache_tree *extent_cache,
2510                                struct extent_buffer *eb, int slot)
2511 {
2512         struct btrfs_extent_item *ei;
2513         struct btrfs_extent_inline_ref *iref;
2514         struct btrfs_extent_data_ref *dref;
2515         struct btrfs_shared_data_ref *sref;
2516         struct btrfs_key key;
2517         unsigned long end;
2518         unsigned long ptr;
2519         int type;
2520         u32 item_size = btrfs_item_size_nr(eb, slot);
2521         u64 refs = 0;
2522         u64 offset;
2523
2524         btrfs_item_key_to_cpu(eb, &key, slot);
2525
2526         if (item_size < sizeof(*ei)) {
2527 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2528                 struct btrfs_extent_item_v0 *ei0;
2529                 BUG_ON(item_size != sizeof(*ei0));
2530                 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2531                 refs = btrfs_extent_refs_v0(eb, ei0);
2532 #else
2533                 BUG();
2534 #endif
2535                 return add_extent_rec(extent_cache, NULL, key.objectid,
2536                                       key.offset, refs, 0, 0, 0, key.offset);
2537         }
2538
2539         ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2540         refs = btrfs_extent_refs(eb, ei);
2541
2542         add_extent_rec(extent_cache, NULL, key.objectid, key.offset,
2543                        refs, 0, 0, 0, key.offset);
2544
2545         ptr = (unsigned long)(ei + 1);
2546         if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2547                 ptr += sizeof(struct btrfs_tree_block_info);
2548
2549         end = (unsigned long)ei + item_size;
2550         while (ptr < end) {
2551                 iref = (struct btrfs_extent_inline_ref *)ptr;
2552                 type = btrfs_extent_inline_ref_type(eb, iref);
2553                 offset = btrfs_extent_inline_ref_offset(eb, iref);
2554                 switch (type) {
2555                 case BTRFS_TREE_BLOCK_REF_KEY:
2556                         add_tree_backref(extent_cache, key.objectid,
2557                                          0, offset, 0);
2558                         break;
2559                 case BTRFS_SHARED_BLOCK_REF_KEY:
2560                         add_tree_backref(extent_cache, key.objectid,
2561                                          offset, 0, 0);
2562                         break;
2563                 case BTRFS_EXTENT_DATA_REF_KEY:
2564                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2565                         add_data_backref(extent_cache, key.objectid, 0,
2566                                         btrfs_extent_data_ref_root(eb, dref),
2567                                         btrfs_extent_data_ref_objectid(eb,
2568                                                                        dref),
2569                                         btrfs_extent_data_ref_offset(eb, dref),
2570                                         btrfs_extent_data_ref_count(eb, dref),
2571                                         0, key.offset);
2572                         break;
2573                 case BTRFS_SHARED_DATA_REF_KEY:
2574                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
2575                         add_data_backref(extent_cache, key.objectid, offset,
2576                                         0, 0, 0,
2577                                         btrfs_shared_data_ref_count(eb, sref),
2578                                         0, key.offset);
2579                         break;
2580                 default:
2581                         fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2582                                 key.objectid, key.type, key.offset);
2583                         goto out;
2584                 }
2585                 ptr += btrfs_extent_inline_ref_size(type);
2586         }
2587         WARN_ON(ptr > end);
2588 out:
2589         return 0;
2590 }
2591
2592 static int run_next_block(struct btrfs_root *root,
2593                           struct block_info *bits,
2594                           int bits_nr,
2595                           u64 *last,
2596                           struct cache_tree *pending,
2597                           struct cache_tree *seen,
2598                           struct cache_tree *reada,
2599                           struct cache_tree *nodes,
2600                           struct cache_tree *extent_cache)
2601 {
2602         struct extent_buffer *buf;
2603         u64 bytenr;
2604         u32 size;
2605         u64 parent;
2606         u64 owner;
2607         u64 flags;
2608         int ret;
2609         int i;
2610         int nritems;
2611         struct btrfs_key key;
2612         struct cache_extent *cache;
2613         int reada_bits;
2614
2615         ret = pick_next_pending(pending, reada, nodes, *last, bits,
2616                                 bits_nr, &reada_bits);
2617         if (ret == 0) {
2618                 return 1;
2619         }
2620         if (!reada_bits) {
2621                 for(i = 0; i < ret; i++) {
2622                         insert_cache_extent(reada, bits[i].start,
2623                                             bits[i].size);
2624
2625                         /* fixme, get the parent transid */
2626                         readahead_tree_block(root, bits[i].start,
2627                                              bits[i].size, 0);
2628                 }
2629         }
2630         *last = bits[0].start;
2631         bytenr = bits[0].start;
2632         size = bits[0].size;
2633
2634         cache = find_cache_extent(pending, bytenr, size);
2635         if (cache) {
2636                 remove_cache_extent(pending, cache);
2637                 free(cache);
2638         }
2639         cache = find_cache_extent(reada, bytenr, size);
2640         if (cache) {
2641                 remove_cache_extent(reada, cache);
2642                 free(cache);
2643         }
2644         cache = find_cache_extent(nodes, bytenr, size);
2645         if (cache) {
2646                 remove_cache_extent(nodes, cache);
2647                 free(cache);
2648         }
2649
2650         /* fixme, get the real parent transid */
2651         buf = read_tree_block(root, bytenr, size, 0);
2652         if (!extent_buffer_uptodate(buf)) {
2653                 record_bad_block_io(root->fs_info,
2654                                     extent_cache, bytenr, size);
2655                 goto out;
2656         }
2657
2658         nritems = btrfs_header_nritems(buf);
2659
2660         ret = btrfs_lookup_extent_info(NULL, root, bytenr, size, NULL, &flags);
2661         if (ret < 0)
2662                 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
2663
2664         if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
2665                 parent = bytenr;
2666                 owner = 0;
2667         } else {
2668                 parent = 0;
2669                 owner = btrfs_header_owner(buf);
2670         }
2671
2672         ret = check_block(root, extent_cache, buf, flags);
2673         if (ret)
2674                 goto out;
2675
2676         if (btrfs_is_leaf(buf)) {
2677                 btree_space_waste += btrfs_leaf_free_space(root, buf);
2678                 for (i = 0; i < nritems; i++) {
2679                         struct btrfs_file_extent_item *fi;
2680                         btrfs_item_key_to_cpu(buf, &key, i);
2681                         if (key.type == BTRFS_EXTENT_ITEM_KEY) {
2682                                 process_extent_item(extent_cache, buf, i);
2683                                 continue;
2684                         }
2685                         if (key.type == BTRFS_EXTENT_CSUM_KEY) {
2686                                 total_csum_bytes +=
2687                                         btrfs_item_size_nr(buf, i);
2688                                 continue;
2689                         }
2690                         if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2691                                 continue;
2692                         }
2693                         if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
2694 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2695                                 process_extent_ref_v0(extent_cache, buf, i);
2696 #else
2697                                 BUG();
2698 #endif
2699                                 continue;
2700                         }
2701
2702                         if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
2703                                 add_tree_backref(extent_cache, key.objectid, 0,
2704                                                  key.offset, 0);
2705                                 continue;
2706                         }
2707                         if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
2708                                 add_tree_backref(extent_cache, key.objectid,
2709                                                  key.offset, 0, 0);
2710                                 continue;
2711                         }
2712                         if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
2713                                 struct btrfs_extent_data_ref *ref;
2714                                 ref = btrfs_item_ptr(buf, i,
2715                                                 struct btrfs_extent_data_ref);
2716                                 add_data_backref(extent_cache,
2717                                         key.objectid, 0,
2718                                         btrfs_extent_data_ref_root(buf, ref),
2719                                         btrfs_extent_data_ref_objectid(buf,
2720                                                                        ref),
2721                                         btrfs_extent_data_ref_offset(buf, ref),
2722                                         btrfs_extent_data_ref_count(buf, ref),
2723                                         0, root->sectorsize);
2724                                 continue;
2725                         }
2726                         if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
2727                                 struct btrfs_shared_data_ref *ref;
2728                                 ref = btrfs_item_ptr(buf, i,
2729                                                 struct btrfs_shared_data_ref);
2730                                 add_data_backref(extent_cache,
2731                                         key.objectid, key.offset, 0, 0, 0, 
2732                                         btrfs_shared_data_ref_count(buf, ref),
2733                                         0, root->sectorsize);
2734                                 continue;
2735                         }
2736                         if (key.type != BTRFS_EXTENT_DATA_KEY)
2737                                 continue;
2738                         fi = btrfs_item_ptr(buf, i,
2739                                             struct btrfs_file_extent_item);
2740                         if (btrfs_file_extent_type(buf, fi) ==
2741                             BTRFS_FILE_EXTENT_INLINE)
2742                                 continue;
2743                         if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
2744                                 continue;
2745
2746                         data_bytes_allocated +=
2747                                 btrfs_file_extent_disk_num_bytes(buf, fi);
2748                         if (data_bytes_allocated < root->sectorsize) {
2749                                 abort();
2750                         }
2751                         data_bytes_referenced +=
2752                                 btrfs_file_extent_num_bytes(buf, fi);
2753                         ret = add_extent_rec(extent_cache, NULL,
2754                                    btrfs_file_extent_disk_bytenr(buf, fi),
2755                                    btrfs_file_extent_disk_num_bytes(buf, fi),
2756                                    0, 0, 1, 1,
2757                                    btrfs_file_extent_disk_num_bytes(buf, fi));
2758                         add_data_backref(extent_cache,
2759                                 btrfs_file_extent_disk_bytenr(buf, fi),
2760                                 parent, owner, key.objectid, key.offset -
2761                                 btrfs_file_extent_offset(buf, fi), 1, 1,
2762                                 btrfs_file_extent_disk_num_bytes(buf, fi));
2763                         BUG_ON(ret);
2764                 }
2765         } else {
2766                 int level;
2767                 struct btrfs_key first_key;
2768
2769                 first_key.objectid = 0;
2770
2771                 if (nritems > 0)
2772                         btrfs_item_key_to_cpu(buf, &first_key, 0);
2773                 level = btrfs_header_level(buf);
2774                 for (i = 0; i < nritems; i++) {
2775                         u64 ptr = btrfs_node_blockptr(buf, i);
2776                         u32 size = btrfs_level_size(root, level - 1);
2777                         btrfs_node_key_to_cpu(buf, &key, i);
2778                         ret = add_extent_rec(extent_cache, &key,
2779                                              ptr, size, 0, 0, 1, 0, size);
2780                         BUG_ON(ret);
2781
2782                         add_tree_backref(extent_cache, ptr, parent, owner, 1);
2783
2784                         if (level > 1) {
2785                                 add_pending(nodes, seen, ptr, size);
2786                         } else {
2787                                 add_pending(pending, seen, ptr, size);
2788                         }
2789                 }
2790                 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
2791                                       nritems) * sizeof(struct btrfs_key_ptr);
2792         }
2793         total_btree_bytes += buf->len;
2794         if (fs_root_objectid(btrfs_header_owner(buf)))
2795                 total_fs_tree_bytes += buf->len;
2796         if (!found_old_backref &&
2797             btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
2798             btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
2799             !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
2800                 found_old_backref = 1;
2801 out:
2802         free_extent_buffer(buf);
2803         return 0;
2804 }
2805
2806 static int add_root_to_pending(struct extent_buffer *buf,
2807                                struct cache_tree *extent_cache,
2808                                struct cache_tree *pending,
2809                                struct cache_tree *seen,
2810                                struct cache_tree *nodes,
2811                                struct btrfs_key *root_key)
2812 {
2813         if (btrfs_header_level(buf) > 0)
2814                 add_pending(nodes, seen, buf->start, buf->len);
2815         else
2816                 add_pending(pending, seen, buf->start, buf->len);
2817         add_extent_rec(extent_cache, NULL, buf->start, buf->len,
2818                        0, 1, 1, 0, buf->len);
2819
2820         if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
2821             btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2822                 add_tree_backref(extent_cache, buf->start, buf->start,
2823                                  0, 1);
2824         else
2825                 add_tree_backref(extent_cache, buf->start, 0,
2826                                  root_key->objectid, 1);
2827         return 0;
2828 }
2829
2830 /* as we fix the tree, we might be deleting blocks that
2831  * we're tracking for repair.  This hook makes sure we
2832  * remove any backrefs for blocks as we are fixing them.
2833  */
2834 static int free_extent_hook(struct btrfs_trans_handle *trans,
2835                             struct btrfs_root *root,
2836                             u64 bytenr, u64 num_bytes, u64 parent,
2837                             u64 root_objectid, u64 owner, u64 offset,
2838                             int refs_to_drop)
2839 {
2840         struct extent_record *rec;
2841         struct cache_extent *cache;
2842         int is_data;
2843         struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
2844
2845         is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
2846         cache = find_cache_extent(extent_cache, bytenr, num_bytes);
2847         if (!cache)
2848                 return 0;
2849
2850         rec = container_of(cache, struct extent_record, cache);
2851         if (is_data) {
2852                 struct data_backref *back;
2853                 back = find_data_backref(rec, parent, root_objectid, owner,
2854                                          offset);
2855                 if (!back)
2856                         goto out;
2857                 if (back->node.found_ref) {
2858                         back->found_ref -= refs_to_drop;
2859                         if (rec->refs)
2860                                 rec->refs -= refs_to_drop;
2861                 }
2862                 if (back->node.found_extent_tree) {
2863                         back->num_refs -= refs_to_drop;
2864                         if (rec->extent_item_refs)
2865                                 rec->extent_item_refs -= refs_to_drop;
2866                 }
2867                 if (back->found_ref == 0)
2868                         back->node.found_ref = 0;
2869                 if (back->num_refs == 0)
2870                         back->node.found_extent_tree = 0;
2871
2872                 if (!back->node.found_extent_tree && back->node.found_ref) {
2873                         list_del(&back->node.list);
2874                         free(back);
2875                 }
2876         } else {
2877                 struct tree_backref *back;
2878                 back = find_tree_backref(rec, parent, root_objectid);
2879                 if (!back)
2880                         goto out;
2881                 if (back->node.found_ref) {
2882                         if (rec->refs)
2883                                 rec->refs--;
2884                         back->node.found_ref = 0;
2885                 }
2886                 if (back->node.found_extent_tree) {
2887                         if (rec->extent_item_refs)
2888                                 rec->extent_item_refs--;
2889                         back->node.found_extent_tree = 0;
2890                 }
2891                 if (!back->node.found_extent_tree && back->node.found_ref) {
2892                         list_del(&back->node.list);
2893                         free(back);
2894                 }
2895         }
2896         maybe_free_extent_rec(extent_cache, rec);
2897 out:
2898         return 0;
2899 }
2900
2901 static int delete_extent_records(struct btrfs_trans_handle *trans,
2902                                  struct btrfs_root *root,
2903                                  struct btrfs_path *path,
2904                                  u64 bytenr, u64 new_len)
2905 {
2906         struct btrfs_key key;
2907         struct btrfs_key found_key;
2908         struct extent_buffer *leaf;
2909         int ret;
2910         int slot;
2911
2912
2913         key.objectid = bytenr;
2914         key.type = (u8)-1;
2915         key.offset = (u64)-1;
2916
2917         while(1) {
2918                 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
2919                                         &key, path, 0, 1);
2920                 if (ret < 0)
2921                         break;
2922
2923                 if (ret > 0) {
2924                         ret = 0;
2925                         if (path->slots[0] == 0)
2926                                 break;
2927                         path->slots[0]--;
2928                 }
2929                 ret = 0;
2930
2931                 leaf = path->nodes[0];
2932                 slot = path->slots[0];
2933
2934                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2935                 if (found_key.objectid != bytenr)
2936                         break;
2937
2938                 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
2939                     found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2940                     found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
2941                     found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
2942                     found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
2943                     found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
2944                         btrfs_release_path(NULL, path);
2945                         if (found_key.type == 0) {
2946                                 if (found_key.offset == 0)
2947                                         break;
2948                                 key.offset = found_key.offset - 1;
2949                                 key.type = found_key.type;
2950                         }
2951                         key.type = found_key.type - 1;
2952                         key.offset = (u64)-1;
2953                         continue;
2954                 }
2955
2956                 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
2957                         found_key.objectid, found_key.type, found_key.offset);
2958
2959                 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
2960                 if (ret)
2961                         break;
2962                 btrfs_release_path(NULL, path);
2963
2964                 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2965                         ret = btrfs_update_block_group(trans, root, bytenr,
2966                                                        found_key.offset, 0, 0);
2967                         if (ret)
2968                                 break;
2969                 }
2970         }
2971
2972         btrfs_release_path(NULL, path);
2973         return ret;
2974 }
2975
2976 /*
2977  * for a single backref, this will allocate a new extent
2978  * and add the backref to it.
2979  */
2980 static int record_extent(struct btrfs_trans_handle *trans,
2981                          struct btrfs_fs_info *info,
2982                          struct btrfs_path *path,
2983                          struct extent_record *rec,
2984                          struct extent_backref *back,
2985                          int allocated, u64 flags)
2986 {
2987         int ret;
2988         struct btrfs_root *extent_root = info->extent_root;
2989         struct extent_buffer *leaf;
2990         struct btrfs_key ins_key;
2991         struct btrfs_extent_item *ei;
2992         struct tree_backref *tback;
2993         struct data_backref *dback;
2994         struct btrfs_tree_block_info *bi;
2995
2996         if (!back->is_data)
2997                 rec->max_size = max_t(u64, rec->max_size,
2998                                     info->extent_root->leafsize);
2999
3000         if (!allocated) {
3001                 u32 item_size = sizeof(*ei);
3002
3003                 if (!back->is_data)
3004                         item_size += sizeof(*bi);
3005
3006                 ins_key.objectid = rec->start;
3007                 ins_key.offset = rec->max_size;
3008                 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3009
3010                 ret = btrfs_insert_empty_item(trans, extent_root, path,
3011                                         &ins_key, item_size);
3012                 if (ret)
3013                         goto fail;
3014
3015                 leaf = path->nodes[0];
3016                 ei = btrfs_item_ptr(leaf, path->slots[0],
3017                                     struct btrfs_extent_item);
3018
3019                 btrfs_set_extent_refs(leaf, ei, 0);
3020                 btrfs_set_extent_generation(leaf, ei, rec->generation);
3021
3022                 if (back->is_data) {
3023                         btrfs_set_extent_flags(leaf, ei,
3024                                                BTRFS_EXTENT_FLAG_DATA);
3025                 } else {
3026                         struct btrfs_disk_key copy_key;;
3027
3028                         tback = (struct tree_backref *)back;
3029                         bi = (struct btrfs_tree_block_info *)(ei + 1);
3030                         memset_extent_buffer(leaf, 0, (unsigned long)bi,
3031                                              sizeof(*bi));
3032                         memset(&copy_key, 0, sizeof(copy_key));
3033
3034                         copy_key.objectid = le64_to_cpu(rec->info_objectid);
3035                         btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3036                         btrfs_set_tree_block_key(leaf, bi, &copy_key);
3037
3038                         btrfs_set_extent_flags(leaf, ei,
3039                                                BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3040                 }
3041
3042                 btrfs_mark_buffer_dirty(leaf);
3043                 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3044                                                rec->max_size, 1, 0);
3045                 if (ret)
3046                         goto fail;
3047                 btrfs_release_path(NULL, path);
3048         }
3049
3050         if (back->is_data) {
3051                 u64 parent;
3052                 int i;
3053
3054                 dback = (struct data_backref *)back;
3055                 if (back->full_backref)
3056                         parent = dback->parent;
3057                 else
3058                         parent = 0;
3059
3060                 for (i = 0; i < dback->found_ref; i++) {
3061                         /* if parent != 0, we're doing a full backref
3062                          * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3063                          * just makes the backref allocator create a data
3064                          * backref
3065                          */
3066                         ret = btrfs_inc_extent_ref(trans, info->extent_root,
3067                                                    rec->start, rec->max_size,
3068                                                    parent,
3069                                                    dback->root,
3070                                                    parent ?
3071                                                    BTRFS_FIRST_FREE_OBJECTID :
3072                                                    dback->owner,
3073                                                    dback->offset);
3074                         if (ret)
3075                                 break;
3076                 }
3077                 fprintf(stderr, "adding new data backref"
3078                                 " on %llu %s %llu owner %llu"
3079                                 " offset %llu found %d\n",
3080                                 (unsigned long long)rec->start,
3081                                 back->full_backref ?
3082                                 "parent" : "root",
3083                                 back->full_backref ?
3084                                 (unsigned long long)parent :
3085                                 (unsigned long long)dback->root,
3086                                 (unsigned long long)dback->owner,
3087                                 (unsigned long long)dback->offset,
3088                                 dback->found_ref);
3089         } else {
3090                 u64 parent;
3091
3092                 tback = (struct tree_backref *)back;
3093                 if (back->full_backref)
3094                         parent = tback->parent;
3095                 else
3096                         parent = 0;
3097
3098                 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3099                                            rec->start, rec->max_size,
3100                                            parent, tback->root, 0, 0);
3101                 fprintf(stderr, "adding new tree backref on "
3102                         "start %llu len %llu parent %llu root %llu\n",
3103                         rec->start, rec->max_size, tback->parent, tback->root);
3104         }
3105         if (ret)
3106                 goto fail;
3107 fail:
3108         btrfs_release_path(NULL, path);
3109         return ret;
3110 }
3111
3112 /*
3113  * when an incorrect extent item is found, this will delete
3114  * all of the existing entries for it and recreate them
3115  * based on what the tree scan found.
3116  */
3117 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3118                              struct btrfs_fs_info *info,
3119                              struct extent_record *rec)
3120 {
3121         int ret;
3122         struct btrfs_path *path;
3123         struct list_head *cur = rec->backrefs.next;
3124         struct cache_extent *cache;
3125         struct extent_backref *back;
3126         int allocated = 0;
3127         u64 flags = 0;
3128
3129         /* remember our flags for recreating the extent */
3130         ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3131                                        rec->max_size, NULL, &flags);
3132         if (ret < 0)
3133                 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3134
3135         path = btrfs_alloc_path();
3136
3137         /* step one, delete all the existing records */
3138         ret = delete_extent_records(trans, info->extent_root, path,
3139                                     rec->start, rec->max_size);
3140
3141         if (ret < 0)
3142                 goto out;
3143
3144         /* was this block corrupt?  If so, don't add references to it */
3145         cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3146         if (cache) {
3147                 ret = 0;
3148                 goto out;
3149         }
3150
3151         /* step two, recreate all the refs we did find */
3152         while(cur != &rec->backrefs) {
3153                 back = list_entry(cur, struct extent_backref, list);
3154                 cur = cur->next;
3155
3156                 /*
3157                  * if we didn't find any references, don't create a
3158                  * new extent record
3159                  */
3160                 if (!back->found_ref)
3161                         continue;
3162
3163                 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3164                 allocated = 1;
3165
3166                 if (ret)
3167                         goto out;
3168         }
3169 out:
3170         btrfs_free_path(path);
3171         return ret;
3172 }
3173
3174 /* right now we only prune from the extent allocation tree */
3175 static int prune_one_block(struct btrfs_trans_handle *trans,
3176                            struct btrfs_fs_info *info,
3177                            struct btrfs_corrupt_block *corrupt)
3178 {
3179         int ret;
3180         struct btrfs_path path;
3181         struct extent_buffer *eb;
3182         u64 found;
3183         int slot;
3184         int nritems;
3185         int level = corrupt->level + 1;
3186
3187         btrfs_init_path(&path);
3188 again:
3189         /* we want to stop at the parent to our busted block */
3190         path.lowest_level = level;
3191
3192         ret = btrfs_search_slot(trans, info->extent_root,
3193                                 &corrupt->key, &path, -1, 1);
3194
3195         if (ret < 0)
3196                 goto out;
3197
3198         eb = path.nodes[level];
3199         if (!eb) {
3200                 ret = -ENOENT;
3201                 goto out;
3202         }
3203
3204         /*
3205          * hopefully the search gave us the block we want to prune,
3206          * lets try that first
3207          */
3208         slot = path.slots[level];
3209         found =  btrfs_node_blockptr(eb, slot);
3210         if (found == corrupt->cache.start)
3211                 goto del_ptr;
3212
3213         nritems = btrfs_header_nritems(eb);
3214
3215         /* the search failed, lets scan this node and hope we find it */
3216         for (slot = 0; slot < nritems; slot++) {
3217                 found =  btrfs_node_blockptr(eb, slot);
3218                 if (found == corrupt->cache.start)
3219                         goto del_ptr;
3220         }
3221         /*
3222          * we couldn't find the bad block.  TODO, search all the nodes for pointers
3223          * to this block
3224          */
3225         if (eb == info->extent_root->node) {
3226                 ret = -ENOENT;
3227                 goto out;
3228         } else {
3229                 level++;
3230                 btrfs_release_path(NULL, &path);
3231                 goto again;
3232         }
3233
3234 del_ptr:
3235         printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3236         ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3237
3238 out:
3239         btrfs_release_path(NULL, &path);
3240         return ret;
3241 }
3242
3243 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3244                                 struct btrfs_fs_info *info)
3245 {
3246         struct cache_extent *cache;
3247         struct btrfs_corrupt_block *corrupt;
3248
3249         cache = find_first_cache_extent(info->corrupt_blocks, 0);
3250         while (1) {
3251                 if (!cache)
3252                         break;
3253                 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3254                 prune_one_block(trans, info, corrupt);
3255                 cache = next_cache_extent(cache);
3256         }
3257         return 0;
3258 }
3259
3260 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3261 {
3262         struct cache_extent *cache;
3263         struct btrfs_corrupt_block *corrupt;
3264
3265         while (1) {
3266                 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3267                 if (!cache)
3268                         break;
3269                 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3270                 remove_cache_extent(info->corrupt_blocks, cache);
3271                 free(corrupt);
3272         }
3273 }
3274
3275 static int check_block_group(struct btrfs_trans_handle *trans,
3276                               struct btrfs_fs_info *info,
3277                               struct map_lookup *map,
3278                               int *reinit)
3279 {
3280         struct btrfs_key key;
3281         struct btrfs_path path;
3282         int ret;
3283
3284         key.objectid = map->ce.start;
3285         key.offset = map->ce.size;
3286         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3287
3288         btrfs_init_path(&path);
3289         ret = btrfs_search_slot(NULL, info->extent_root,
3290                                 &key, &path, 0, 0);
3291         btrfs_release_path(NULL, &path);
3292         if (ret <= 0)
3293                 goto out;
3294
3295         ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3296                                BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3297                                key.objectid, key.offset);
3298         *reinit = 1;
3299 out:
3300         return ret;
3301 }
3302
3303 static int check_block_groups(struct btrfs_trans_handle *trans,
3304                               struct btrfs_fs_info *info, int *reinit)
3305 {
3306         struct cache_extent *ce;
3307         struct map_lookup *map;
3308         struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3309
3310         /* this isn't quite working */
3311         return 0;
3312
3313         ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3314         while (1) {
3315                 if (!ce)
3316                         break;
3317                 map = container_of(ce, struct map_lookup, ce);
3318                 check_block_group(trans, info, map, reinit);
3319                 ce = next_cache_extent(ce);
3320         }
3321         return 0;
3322 }
3323
3324 static int check_extent_refs(struct btrfs_trans_handle *trans,
3325                              struct btrfs_root *root,
3326                              struct cache_tree *extent_cache, int repair)
3327 {
3328         struct extent_record *rec;
3329         struct cache_extent *cache;
3330         int err = 0;
3331         int ret = 0;
3332         int fixed = 0;
3333         int reinit = 0;
3334
3335         if (repair) {
3336                 /*
3337                  * if we're doing a repair, we have to make sure
3338                  * we don't allocate from the problem extents.
3339                  * In the worst case, this will be all the
3340                  * extents in the FS
3341                  */
3342                 cache = find_first_cache_extent(extent_cache, 0);
3343                 while(cache) {
3344                         rec = container_of(cache, struct extent_record, cache);
3345                         btrfs_pin_extent(root->fs_info,
3346                                          rec->start, rec->max_size);
3347                         cache = next_cache_extent(cache);
3348                 }
3349
3350                 /* pin down all the corrupted blocks too */
3351                 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3352                 while(cache) {
3353                         rec = container_of(cache, struct extent_record, cache);
3354                         btrfs_pin_extent(root->fs_info,
3355                                          rec->start, rec->max_size);
3356                         cache = next_cache_extent(cache);
3357                 }
3358                 prune_corrupt_blocks(trans, root->fs_info);
3359                 check_block_groups(trans, root->fs_info, &reinit);
3360                 if (reinit)
3361                         btrfs_read_block_groups(root->fs_info->extent_root);
3362         }
3363         while(1) {
3364                 fixed = 0;
3365                 cache = find_first_cache_extent(extent_cache, 0);
3366                 if (!cache)
3367                         break;
3368                 rec = container_of(cache, struct extent_record, cache);
3369                 if (rec->refs != rec->extent_item_refs) {
3370                         fprintf(stderr, "ref mismatch on [%llu %llu] ",
3371                                 (unsigned long long)rec->start,
3372                                 (unsigned long long)rec->nr);
3373                         fprintf(stderr, "extent item %llu, found %llu\n",
3374                                 (unsigned long long)rec->extent_item_refs,
3375                                 (unsigned long long)rec->refs);
3376                         if (!fixed && repair) {
3377                                 ret = fixup_extent_refs(trans, root->fs_info, rec);
3378                                 if (ret)
3379                                         goto repair_abort;
3380                                 fixed = 1;
3381                         }
3382                         err = 1;
3383
3384                 }
3385                 if (all_backpointers_checked(rec, 1)) {
3386                         fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3387                                 (unsigned long long)rec->start,
3388                                 (unsigned long long)rec->nr);
3389
3390                         if (!fixed && repair) {
3391                                 ret = fixup_extent_refs(trans, root->fs_info, rec);
3392                                 if (ret)
3393                                         goto repair_abort;
3394                                 fixed = 1;
3395                         }
3396
3397                         err = 1;
3398                 }
3399                 if (!rec->owner_ref_checked) {
3400                         fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3401                                 (unsigned long long)rec->start,
3402                                 (unsigned long long)rec->nr);
3403                         if (!fixed && repair) {
3404                                 ret = fixup_extent_refs(trans, root->fs_info, rec);
3405                                 if (ret)
3406                                         goto repair_abort;
3407                                 fixed = 1;
3408                         }
3409                         err = 1;
3410                 }
3411
3412                 remove_cache_extent(extent_cache, cache);
3413                 free_all_extent_backrefs(rec);
3414                 free(rec);
3415         }
3416 repair_abort:
3417         if (repair) {
3418                 if (ret) {
3419                         fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3420                         exit(1);
3421                 } else {
3422                         btrfs_fix_block_accounting(trans, root);
3423                 }
3424                 if (err)
3425                         fprintf(stderr, "repaired damaged extent references\n");
3426                 return ret;
3427         }
3428         return err;
3429 }
3430
3431 static int check_extents(struct btrfs_trans_handle *trans,
3432                          struct btrfs_root *root, int repair)
3433 {
3434         struct cache_tree extent_cache;
3435         struct cache_tree seen;
3436         struct cache_tree pending;
3437         struct cache_tree reada;
3438         struct cache_tree nodes;
3439         struct cache_tree corrupt_blocks;
3440         struct btrfs_path path;
3441         struct btrfs_key key;
3442         struct btrfs_key found_key;
3443         int ret;
3444         u64 last = 0;
3445         struct block_info *bits;
3446         int bits_nr;
3447         struct extent_buffer *leaf;
3448         int slot;
3449         struct btrfs_root_item ri;
3450
3451         cache_tree_init(&extent_cache);
3452         cache_tree_init(&seen);
3453         cache_tree_init(&pending);
3454         cache_tree_init(&nodes);
3455         cache_tree_init(&reada);
3456         cache_tree_init(&corrupt_blocks);
3457
3458         if (repair) {
3459                 root->fs_info->fsck_extent_cache = &extent_cache;
3460                 root->fs_info->free_extent_hook = free_extent_hook;
3461                 root->fs_info->corrupt_blocks = &corrupt_blocks;
3462         }
3463
3464         bits_nr = 1024;
3465         bits = malloc(bits_nr * sizeof(struct block_info));
3466         if (!bits) {
3467                 perror("malloc");
3468                 exit(1);
3469         }
3470
3471         add_root_to_pending(root->fs_info->tree_root->node,
3472                             &extent_cache, &pending, &seen, &nodes,
3473                             &root->fs_info->tree_root->root_key);
3474
3475         add_root_to_pending(root->fs_info->chunk_root->node,
3476                             &extent_cache, &pending, &seen, &nodes,
3477                             &root->fs_info->chunk_root->root_key);
3478
3479         btrfs_init_path(&path);
3480         key.offset = 0;
3481         key.objectid = 0;
3482         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3483         ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3484                                         &key, &path, 0, 0);
3485         BUG_ON(ret < 0);
3486         while(1) {
3487                 leaf = path.nodes[0];
3488                 slot = path.slots[0];
3489                 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3490                         ret = btrfs_next_leaf(root, &path);
3491                         if (ret != 0)
3492                                 break;
3493                         leaf = path.nodes[0];
3494                         slot = path.slots[0];
3495                 }
3496                 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3497                 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3498                         unsigned long offset;
3499                         struct extent_buffer *buf;
3500
3501                         offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3502                         read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3503                         buf = read_tree_block(root->fs_info->tree_root,
3504                                               btrfs_root_bytenr(&ri),
3505                                               btrfs_level_size(root,
3506                                                btrfs_root_level(&ri)), 0);
3507                         add_root_to_pending(buf, &extent_cache, &pending,
3508                                             &seen, &nodes, &found_key);
3509                         free_extent_buffer(buf);
3510                 }
3511                 path.slots[0]++;
3512         }
3513         btrfs_release_path(root, &path);
3514         while(1) {
3515                 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3516                                      &seen, &reada, &nodes, &extent_cache);
3517                 if (ret != 0)
3518                         break;
3519         }
3520         ret = check_extent_refs(trans, root, &extent_cache, repair);
3521
3522         if (repair) {
3523                 free_corrupt_blocks(root->fs_info);
3524                 root->fs_info->fsck_extent_cache = NULL;
3525                 root->fs_info->free_extent_hook = NULL;
3526                 root->fs_info->corrupt_blocks = NULL;
3527         }
3528
3529         free(bits);
3530         return ret;
3531 }
3532
3533 static struct option long_options[] = {
3534         { "super", 1, NULL, 's' },
3535         { "repair", 0, NULL, 0 },
3536         { "init-csum-tree", 0, NULL, 0 },
3537         { "init-extent-tree", 0, NULL, 0 },
3538         { 0, 0, 0, 0}
3539 };
3540
3541 const char * const cmd_check_usage[] = {
3542         "btrfs check [options] <device>",
3543         "Check an unmounted btrfs filesystem.",
3544         "",
3545         "-s|--super <superblock>     use this superblock copy",
3546         "--repair                    try to repair the filesystem",
3547         "--init-csum-tree            create a new CRC tree",
3548         "--init-extent-tree          create a new extent tree",
3549         NULL
3550 };
3551
3552 int cmd_check(int argc, char **argv)
3553 {
3554         struct cache_tree root_cache;
3555         struct btrfs_root *root;
3556         struct btrfs_fs_info *info;
3557         struct btrfs_trans_handle *trans = NULL;
3558         u64 bytenr = 0;
3559         char uuidbuf[37];
3560         int ret;
3561         int num;
3562         int repair = 0;
3563         int option_index = 0;
3564         int init_csum_tree = 0;
3565         int rw = 0;
3566
3567         while(1) {
3568                 int c;
3569                 c = getopt_long(argc, argv, "as:", long_options,
3570                                 &option_index);
3571                 if (c < 0)
3572                         break;
3573                 switch(c) {
3574                         case 'a': /* ignored */ break;
3575                         case 's':
3576                                 num = atol(optarg);
3577                                 bytenr = btrfs_sb_offset(num);
3578                                 printf("using SB copy %d, bytenr %llu\n", num,
3579                                        (unsigned long long)bytenr);
3580                                 break;
3581                         case '?':
3582                         case 'h':
3583                                 usage(cmd_check_usage);
3584                 }
3585                 if (option_index == 1) {
3586                         printf("enabling repair mode\n");
3587                         repair = 1;
3588                         rw = 1;
3589                 } else if (option_index == 2) {
3590                         printf("Creating a new CRC tree\n");
3591                         init_csum_tree = 1;
3592                         rw = 1;
3593                 }
3594
3595         }
3596         argc = argc - optind;
3597
3598         if (argc != 1)
3599                 usage(cmd_check_usage);
3600
3601         radix_tree_init();
3602         cache_tree_init(&root_cache);
3603
3604         if((ret = check_mounted(argv[optind])) < 0) {
3605                 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
3606                 return ret;
3607         } else if(ret) {
3608                 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
3609                 return -EBUSY;
3610         }
3611
3612         info = open_ctree_fs_info(argv[optind], bytenr, rw, 1);
3613         if (info == NULL)
3614                 return 1;
3615
3616         uuid_unparse(info->super_copy.fsid, uuidbuf);
3617         printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
3618
3619         if (!extent_buffer_uptodate(info->tree_root->node) ||
3620             !extent_buffer_uptodate(info->dev_root->node) ||
3621             !extent_buffer_uptodate(info->extent_root->node) ||
3622             !extent_buffer_uptodate(info->chunk_root->node)) {
3623                 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
3624                 return -EIO;
3625         }
3626
3627         root = info->fs_root;
3628
3629         fprintf(stderr, "checking extents\n");
3630         if (rw)
3631                 trans = btrfs_start_transaction(root, 1);
3632
3633         if (init_csum_tree) {
3634                 fprintf(stderr, "Reinit crc root\n");
3635                 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
3636                 if (ret) {
3637                         fprintf(stderr, "crc root initialization failed\n");
3638                         return -EIO;
3639                 }
3640                 goto out;
3641         }
3642         ret = check_extents(trans, root, repair);
3643         if (ret)
3644                 fprintf(stderr, "Errors found in extent allocation tree\n");
3645
3646         fprintf(stderr, "checking fs roots\n");
3647         ret = check_fs_roots(root, &root_cache);
3648         if (ret)
3649                 goto out;
3650
3651         fprintf(stderr, "checking root refs\n");
3652         ret = check_root_refs(root, &root_cache);
3653 out:
3654         free_root_recs(&root_cache);
3655         if (rw) {
3656                 ret = btrfs_commit_transaction(trans, root);
3657                 if (ret)
3658                         exit(1);
3659         }
3660         close_ctree(root);
3661
3662         if (found_old_backref) { /*
3663                  * there was a disk format change when mixed
3664                  * backref was in testing tree. The old format
3665                  * existed about one week.
3666                  */
3667                 printf("\n * Found old mixed backref format. "
3668                        "The old format is not supported! *"
3669                        "\n * Please mount the FS in readonly mode, "
3670                        "backup data and re-format the FS. *\n\n");
3671                 ret = 1;
3672         }
3673         printf("found %llu bytes used err is %d\n",
3674                (unsigned long long)bytes_used, ret);
3675         printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
3676         printf("total tree bytes: %llu\n",
3677                (unsigned long long)total_btree_bytes);
3678         printf("total fs tree bytes: %llu\n",
3679                (unsigned long long)total_fs_tree_bytes);
3680         printf("btree space waste bytes: %llu\n",
3681                (unsigned long long)btree_space_waste);
3682         printf("file data blocks allocated: %llu\n referenced %llu\n",
3683                 (unsigned long long)data_bytes_allocated,
3684                 (unsigned long long)data_bytes_referenced);
3685         printf("%s\n", BTRFS_BUILD_VERSION);
3686         return ret;
3687 }