2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 500
25 #include <sys/types.h>
29 #include <uuid/uuid.h>
34 #include "print-tree.h"
35 #include "transaction.h"
39 #include "free-space-cache.h"
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 total_extent_tree_bytes = 0;
47 static u64 btree_space_waste = 0;
48 static u64 data_bytes_allocated = 0;
49 static u64 data_bytes_referenced = 0;
50 static int found_old_backref = 0;
51 static LIST_HEAD(duplicate_extents);
52 static int repair = 0;
54 struct extent_backref {
55 struct list_head list;
56 unsigned int is_data:1;
57 unsigned int found_extent_tree:1;
58 unsigned int full_backref:1;
59 unsigned int found_ref:1;
60 unsigned int broken:1;
64 struct extent_backref node;
79 struct extent_backref node;
86 struct extent_record {
87 struct list_head backrefs;
88 struct list_head dups;
89 struct list_head list;
90 struct cache_extent cache;
91 struct btrfs_disk_key parent_key;
92 unsigned int found_rec;
102 unsigned int content_checked:1;
103 unsigned int owner_ref_checked:1;
104 unsigned int is_root:1;
105 unsigned int metadata:1;
108 struct inode_backref {
109 struct list_head list;
110 unsigned int found_dir_item:1;
111 unsigned int found_dir_index:1;
112 unsigned int found_inode_ref:1;
113 unsigned int filetype:8;
115 unsigned int ref_type;
122 struct dropping_root_item_record {
123 struct list_head list;
124 struct btrfs_root_item ri;
125 struct btrfs_key found_key;
128 #define REF_ERR_NO_DIR_ITEM (1 << 0)
129 #define REF_ERR_NO_DIR_INDEX (1 << 1)
130 #define REF_ERR_NO_INODE_REF (1 << 2)
131 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
132 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
133 #define REF_ERR_DUP_INODE_REF (1 << 5)
134 #define REF_ERR_INDEX_UNMATCH (1 << 6)
135 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
136 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
137 #define REF_ERR_NO_ROOT_REF (1 << 9)
138 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
139 #define REF_ERR_DUP_ROOT_REF (1 << 11)
140 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
142 struct inode_record {
143 struct list_head backrefs;
144 unsigned int checked:1;
145 unsigned int merging:1;
146 unsigned int found_inode_item:1;
147 unsigned int found_dir_item:1;
148 unsigned int found_file_extent:1;
149 unsigned int found_csum_item:1;
150 unsigned int some_csum_missing:1;
151 unsigned int nodatasum:1;
164 u64 first_extent_gap;
169 #define I_ERR_NO_INODE_ITEM (1 << 0)
170 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
171 #define I_ERR_DUP_INODE_ITEM (1 << 2)
172 #define I_ERR_DUP_DIR_INDEX (1 << 3)
173 #define I_ERR_ODD_DIR_ITEM (1 << 4)
174 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
175 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
176 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
177 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
178 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
179 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
180 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
181 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
182 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
184 struct root_backref {
185 struct list_head list;
186 unsigned int found_dir_item:1;
187 unsigned int found_dir_index:1;
188 unsigned int found_back_ref:1;
189 unsigned int found_forward_ref:1;
190 unsigned int reachable:1;
200 struct list_head backrefs;
201 struct cache_extent cache;
202 unsigned int found_root_item:1;
208 struct cache_extent cache;
213 struct cache_extent cache;
214 struct cache_tree root_cache;
215 struct cache_tree inode_cache;
216 struct inode_record *current;
225 struct walk_control {
226 struct cache_tree shared;
227 struct shared_node *nodes[BTRFS_MAX_LEVEL];
232 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
234 static u8 imode_to_type(u32 imode)
237 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
238 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
239 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
240 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
241 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
242 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
243 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
244 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
247 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
251 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
253 struct device_record *rec1;
254 struct device_record *rec2;
256 rec1 = rb_entry(node1, struct device_record, node);
257 rec2 = rb_entry(node2, struct device_record, node);
258 if (rec1->devid > rec2->devid)
260 else if (rec1->devid < rec2->devid)
266 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
268 struct inode_record *rec;
269 struct inode_backref *backref;
270 struct inode_backref *orig;
273 rec = malloc(sizeof(*rec));
274 memcpy(rec, orig_rec, sizeof(*rec));
276 INIT_LIST_HEAD(&rec->backrefs);
278 list_for_each_entry(orig, &orig_rec->backrefs, list) {
279 size = sizeof(*orig) + orig->namelen + 1;
280 backref = malloc(size);
281 memcpy(backref, orig, size);
282 list_add_tail(&backref->list, &rec->backrefs);
287 static void print_inode_error(int errors)
289 if (errors & I_ERR_NO_INODE_ITEM)
290 fprintf(stderr, ", no inode item");
291 if (errors & I_ERR_NO_ORPHAN_ITEM)
292 fprintf(stderr, ", no orphan item");
293 if (errors & I_ERR_DUP_INODE_ITEM)
294 fprintf(stderr, ", dup inode item");
295 if (errors & I_ERR_DUP_DIR_INDEX)
296 fprintf(stderr, ", dup dir index");
297 if (errors & I_ERR_ODD_DIR_ITEM)
298 fprintf(stderr, ", odd dir item");
299 if (errors & I_ERR_ODD_FILE_EXTENT)
300 fprintf(stderr, ", odd file extent");
301 if (errors & I_ERR_BAD_FILE_EXTENT)
302 fprintf(stderr, ", bad file extent");
303 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
304 fprintf(stderr, ", file extent overlap");
305 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
306 fprintf(stderr, ", file extent discount");
307 if (errors & I_ERR_DIR_ISIZE_WRONG)
308 fprintf(stderr, ", dir isize wrong");
309 if (errors & I_ERR_FILE_NBYTES_WRONG)
310 fprintf(stderr, ", nbytes wrong");
311 if (errors & I_ERR_ODD_CSUM_ITEM)
312 fprintf(stderr, ", odd csum item");
313 if (errors & I_ERR_SOME_CSUM_MISSING)
314 fprintf(stderr, ", some csum missing");
315 if (errors & I_ERR_LINK_COUNT_WRONG)
316 fprintf(stderr, ", link count wrong");
317 fprintf(stderr, "\n");
320 static void print_ref_error(int errors)
322 if (errors & REF_ERR_NO_DIR_ITEM)
323 fprintf(stderr, ", no dir item");
324 if (errors & REF_ERR_NO_DIR_INDEX)
325 fprintf(stderr, ", no dir index");
326 if (errors & REF_ERR_NO_INODE_REF)
327 fprintf(stderr, ", no inode ref");
328 if (errors & REF_ERR_DUP_DIR_ITEM)
329 fprintf(stderr, ", dup dir item");
330 if (errors & REF_ERR_DUP_DIR_INDEX)
331 fprintf(stderr, ", dup dir index");
332 if (errors & REF_ERR_DUP_INODE_REF)
333 fprintf(stderr, ", dup inode ref");
334 if (errors & REF_ERR_INDEX_UNMATCH)
335 fprintf(stderr, ", index unmatch");
336 if (errors & REF_ERR_FILETYPE_UNMATCH)
337 fprintf(stderr, ", filetype unmatch");
338 if (errors & REF_ERR_NAME_TOO_LONG)
339 fprintf(stderr, ", name too long");
340 if (errors & REF_ERR_NO_ROOT_REF)
341 fprintf(stderr, ", no root ref");
342 if (errors & REF_ERR_NO_ROOT_BACKREF)
343 fprintf(stderr, ", no root backref");
344 if (errors & REF_ERR_DUP_ROOT_REF)
345 fprintf(stderr, ", dup root ref");
346 if (errors & REF_ERR_DUP_ROOT_BACKREF)
347 fprintf(stderr, ", dup root backref");
348 fprintf(stderr, "\n");
351 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
354 struct ptr_node *node;
355 struct cache_extent *cache;
356 struct inode_record *rec = NULL;
359 cache = lookup_cache_extent(inode_cache, ino, 1);
361 node = container_of(cache, struct ptr_node, cache);
363 if (mod && rec->refs > 1) {
364 node->data = clone_inode_rec(rec);
369 rec = calloc(1, sizeof(*rec));
371 rec->extent_start = (u64)-1;
372 rec->first_extent_gap = (u64)-1;
374 INIT_LIST_HEAD(&rec->backrefs);
376 node = malloc(sizeof(*node));
377 node->cache.start = ino;
378 node->cache.size = 1;
381 if (ino == BTRFS_FREE_INO_OBJECTID)
384 ret = insert_cache_extent(inode_cache, &node->cache);
390 static void free_inode_rec(struct inode_record *rec)
392 struct inode_backref *backref;
397 while (!list_empty(&rec->backrefs)) {
398 backref = list_entry(rec->backrefs.next,
399 struct inode_backref, list);
400 list_del(&backref->list);
406 static int can_free_inode_rec(struct inode_record *rec)
408 if (!rec->errors && rec->checked && rec->found_inode_item &&
409 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
414 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
415 struct inode_record *rec)
417 struct cache_extent *cache;
418 struct inode_backref *tmp, *backref;
419 struct ptr_node *node;
420 unsigned char filetype;
422 if (!rec->found_inode_item)
425 filetype = imode_to_type(rec->imode);
426 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
427 if (backref->found_dir_item && backref->found_dir_index) {
428 if (backref->filetype != filetype)
429 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
430 if (!backref->errors && backref->found_inode_ref) {
431 list_del(&backref->list);
437 if (!rec->checked || rec->merging)
440 if (S_ISDIR(rec->imode)) {
441 if (rec->found_size != rec->isize)
442 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
443 if (rec->found_file_extent)
444 rec->errors |= I_ERR_ODD_FILE_EXTENT;
445 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
446 if (rec->found_dir_item)
447 rec->errors |= I_ERR_ODD_DIR_ITEM;
448 if (rec->found_size != rec->nbytes)
449 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
450 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
451 rec->first_extent_gap = 0;
452 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
453 rec->first_extent_gap < rec->isize))
454 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
457 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
458 if (rec->found_csum_item && rec->nodatasum)
459 rec->errors |= I_ERR_ODD_CSUM_ITEM;
460 if (rec->some_csum_missing && !rec->nodatasum)
461 rec->errors |= I_ERR_SOME_CSUM_MISSING;
464 BUG_ON(rec->refs != 1);
465 if (can_free_inode_rec(rec)) {
466 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
467 node = container_of(cache, struct ptr_node, cache);
468 BUG_ON(node->data != rec);
469 remove_cache_extent(inode_cache, &node->cache);
475 static int check_orphan_item(struct btrfs_root *root, u64 ino)
477 struct btrfs_path path;
478 struct btrfs_key key;
481 key.objectid = BTRFS_ORPHAN_OBJECTID;
482 key.type = BTRFS_ORPHAN_ITEM_KEY;
485 btrfs_init_path(&path);
486 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
487 btrfs_release_path(&path);
493 static int process_inode_item(struct extent_buffer *eb,
494 int slot, struct btrfs_key *key,
495 struct shared_node *active_node)
497 struct inode_record *rec;
498 struct btrfs_inode_item *item;
500 rec = active_node->current;
501 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
502 if (rec->found_inode_item) {
503 rec->errors |= I_ERR_DUP_INODE_ITEM;
506 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
507 rec->nlink = btrfs_inode_nlink(eb, item);
508 rec->isize = btrfs_inode_size(eb, item);
509 rec->nbytes = btrfs_inode_nbytes(eb, item);
510 rec->imode = btrfs_inode_mode(eb, item);
511 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
513 rec->found_inode_item = 1;
515 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
516 maybe_free_inode_rec(&active_node->inode_cache, rec);
520 static struct inode_backref *get_inode_backref(struct inode_record *rec,
522 int namelen, u64 dir)
524 struct inode_backref *backref;
526 list_for_each_entry(backref, &rec->backrefs, list) {
527 if (backref->dir != dir || backref->namelen != namelen)
529 if (memcmp(name, backref->name, namelen))
534 backref = malloc(sizeof(*backref) + namelen + 1);
535 memset(backref, 0, sizeof(*backref));
537 backref->namelen = namelen;
538 memcpy(backref->name, name, namelen);
539 backref->name[namelen] = '\0';
540 list_add_tail(&backref->list, &rec->backrefs);
544 static int add_inode_backref(struct cache_tree *inode_cache,
545 u64 ino, u64 dir, u64 index,
546 const char *name, int namelen,
547 int filetype, int itemtype, int errors)
549 struct inode_record *rec;
550 struct inode_backref *backref;
552 rec = get_inode_rec(inode_cache, ino, 1);
553 backref = get_inode_backref(rec, name, namelen, dir);
555 backref->errors |= errors;
556 if (itemtype == BTRFS_DIR_INDEX_KEY) {
557 if (backref->found_dir_index)
558 backref->errors |= REF_ERR_DUP_DIR_INDEX;
559 if (backref->found_inode_ref && backref->index != index)
560 backref->errors |= REF_ERR_INDEX_UNMATCH;
561 if (backref->found_dir_item && backref->filetype != filetype)
562 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
564 backref->index = index;
565 backref->filetype = filetype;
566 backref->found_dir_index = 1;
567 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
569 if (backref->found_dir_item)
570 backref->errors |= REF_ERR_DUP_DIR_ITEM;
571 if (backref->found_dir_index && backref->filetype != filetype)
572 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
574 backref->filetype = filetype;
575 backref->found_dir_item = 1;
576 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
577 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
578 if (backref->found_inode_ref)
579 backref->errors |= REF_ERR_DUP_INODE_REF;
580 if (backref->found_dir_index && backref->index != index)
581 backref->errors |= REF_ERR_INDEX_UNMATCH;
583 backref->ref_type = itemtype;
584 backref->index = index;
585 backref->found_inode_ref = 1;
590 maybe_free_inode_rec(inode_cache, rec);
594 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
595 struct cache_tree *dst_cache)
597 struct inode_backref *backref;
601 list_for_each_entry(backref, &src->backrefs, list) {
602 if (backref->found_dir_index) {
603 add_inode_backref(dst_cache, dst->ino, backref->dir,
604 backref->index, backref->name,
605 backref->namelen, backref->filetype,
606 BTRFS_DIR_INDEX_KEY, backref->errors);
608 if (backref->found_dir_item) {
610 add_inode_backref(dst_cache, dst->ino,
611 backref->dir, 0, backref->name,
612 backref->namelen, backref->filetype,
613 BTRFS_DIR_ITEM_KEY, backref->errors);
615 if (backref->found_inode_ref) {
616 add_inode_backref(dst_cache, dst->ino,
617 backref->dir, backref->index,
618 backref->name, backref->namelen, 0,
619 backref->ref_type, backref->errors);
623 if (src->found_dir_item)
624 dst->found_dir_item = 1;
625 if (src->found_file_extent)
626 dst->found_file_extent = 1;
627 if (src->found_csum_item)
628 dst->found_csum_item = 1;
629 if (src->some_csum_missing)
630 dst->some_csum_missing = 1;
631 if (dst->first_extent_gap > src->first_extent_gap)
632 dst->first_extent_gap = src->first_extent_gap;
634 BUG_ON(src->found_link < dir_count);
635 dst->found_link += src->found_link - dir_count;
636 dst->found_size += src->found_size;
637 if (src->extent_start != (u64)-1) {
638 if (dst->extent_start == (u64)-1) {
639 dst->extent_start = src->extent_start;
640 dst->extent_end = src->extent_end;
642 if (dst->extent_end > src->extent_start)
643 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
644 else if (dst->extent_end < src->extent_start &&
645 dst->extent_end < dst->first_extent_gap)
646 dst->first_extent_gap = dst->extent_end;
647 if (dst->extent_end < src->extent_end)
648 dst->extent_end = src->extent_end;
652 dst->errors |= src->errors;
653 if (src->found_inode_item) {
654 if (!dst->found_inode_item) {
655 dst->nlink = src->nlink;
656 dst->isize = src->isize;
657 dst->nbytes = src->nbytes;
658 dst->imode = src->imode;
659 dst->nodatasum = src->nodatasum;
660 dst->found_inode_item = 1;
662 dst->errors |= I_ERR_DUP_INODE_ITEM;
670 static int splice_shared_node(struct shared_node *src_node,
671 struct shared_node *dst_node)
673 struct cache_extent *cache;
674 struct ptr_node *node, *ins;
675 struct cache_tree *src, *dst;
676 struct inode_record *rec, *conflict;
681 if (--src_node->refs == 0)
683 if (src_node->current)
684 current_ino = src_node->current->ino;
686 src = &src_node->root_cache;
687 dst = &dst_node->root_cache;
689 cache = search_cache_extent(src, 0);
691 node = container_of(cache, struct ptr_node, cache);
693 cache = next_cache_extent(cache);
696 remove_cache_extent(src, &node->cache);
699 ins = malloc(sizeof(*ins));
700 ins->cache.start = node->cache.start;
701 ins->cache.size = node->cache.size;
705 ret = insert_cache_extent(dst, &ins->cache);
706 if (ret == -EEXIST) {
707 conflict = get_inode_rec(dst, rec->ino, 1);
708 merge_inode_recs(rec, conflict, dst);
710 conflict->checked = 1;
711 if (dst_node->current == conflict)
712 dst_node->current = NULL;
714 maybe_free_inode_rec(dst, conflict);
722 if (src == &src_node->root_cache) {
723 src = &src_node->inode_cache;
724 dst = &dst_node->inode_cache;
728 if (current_ino > 0 && (!dst_node->current ||
729 current_ino > dst_node->current->ino)) {
730 if (dst_node->current) {
731 dst_node->current->checked = 1;
732 maybe_free_inode_rec(dst, dst_node->current);
734 dst_node->current = get_inode_rec(dst, current_ino, 1);
739 static void free_inode_ptr(struct cache_extent *cache)
741 struct ptr_node *node;
742 struct inode_record *rec;
744 node = container_of(cache, struct ptr_node, cache);
750 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
752 static struct shared_node *find_shared_node(struct cache_tree *shared,
755 struct cache_extent *cache;
756 struct shared_node *node;
758 cache = lookup_cache_extent(shared, bytenr, 1);
760 node = container_of(cache, struct shared_node, cache);
766 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
769 struct shared_node *node;
771 node = calloc(1, sizeof(*node));
772 node->cache.start = bytenr;
773 node->cache.size = 1;
774 cache_tree_init(&node->root_cache);
775 cache_tree_init(&node->inode_cache);
778 ret = insert_cache_extent(shared, &node->cache);
783 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
784 struct walk_control *wc, int level)
786 struct shared_node *node;
787 struct shared_node *dest;
789 if (level == wc->active_node)
792 BUG_ON(wc->active_node <= level);
793 node = find_shared_node(&wc->shared, bytenr);
795 add_shared_node(&wc->shared, bytenr, refs);
796 node = find_shared_node(&wc->shared, bytenr);
797 wc->nodes[level] = node;
798 wc->active_node = level;
802 if (wc->root_level == wc->active_node &&
803 btrfs_root_refs(&root->root_item) == 0) {
804 if (--node->refs == 0) {
805 free_inode_recs_tree(&node->root_cache);
806 free_inode_recs_tree(&node->inode_cache);
807 remove_cache_extent(&wc->shared, &node->cache);
813 dest = wc->nodes[wc->active_node];
814 splice_shared_node(node, dest);
815 if (node->refs == 0) {
816 remove_cache_extent(&wc->shared, &node->cache);
822 static int leave_shared_node(struct btrfs_root *root,
823 struct walk_control *wc, int level)
825 struct shared_node *node;
826 struct shared_node *dest;
829 if (level == wc->root_level)
832 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
836 BUG_ON(i >= BTRFS_MAX_LEVEL);
838 node = wc->nodes[wc->active_node];
839 wc->nodes[wc->active_node] = NULL;
842 dest = wc->nodes[wc->active_node];
843 if (wc->active_node < wc->root_level ||
844 btrfs_root_refs(&root->root_item) > 0) {
845 BUG_ON(node->refs <= 1);
846 splice_shared_node(node, dest);
848 BUG_ON(node->refs < 2);
854 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
857 struct btrfs_path path;
858 struct btrfs_key key;
859 struct extent_buffer *leaf;
863 btrfs_init_path(&path);
865 key.objectid = parent_root_id;
866 key.type = BTRFS_ROOT_REF_KEY;
867 key.offset = child_root_id;
868 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
871 btrfs_release_path(&path);
875 key.objectid = child_root_id;
876 key.type = BTRFS_ROOT_BACKREF_KEY;
878 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
883 leaf = path.nodes[0];
884 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
885 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
890 leaf = path.nodes[0];
893 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
894 if (key.objectid != child_root_id ||
895 key.type != BTRFS_ROOT_BACKREF_KEY)
900 if (key.offset == parent_root_id) {
901 btrfs_release_path(&path);
908 btrfs_release_path(&path);
909 return has_parent? 0 : -1;
912 static int process_dir_item(struct btrfs_root *root,
913 struct extent_buffer *eb,
914 int slot, struct btrfs_key *key,
915 struct shared_node *active_node)
925 struct btrfs_dir_item *di;
926 struct inode_record *rec;
927 struct cache_tree *root_cache;
928 struct cache_tree *inode_cache;
929 struct btrfs_key location;
930 char namebuf[BTRFS_NAME_LEN];
932 root_cache = &active_node->root_cache;
933 inode_cache = &active_node->inode_cache;
934 rec = active_node->current;
935 rec->found_dir_item = 1;
937 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
938 total = btrfs_item_size_nr(eb, slot);
939 while (cur < total) {
941 btrfs_dir_item_key_to_cpu(eb, di, &location);
942 name_len = btrfs_dir_name_len(eb, di);
943 data_len = btrfs_dir_data_len(eb, di);
944 filetype = btrfs_dir_type(eb, di);
946 rec->found_size += name_len;
947 if (name_len <= BTRFS_NAME_LEN) {
951 len = BTRFS_NAME_LEN;
952 error = REF_ERR_NAME_TOO_LONG;
954 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
956 if (location.type == BTRFS_INODE_ITEM_KEY) {
957 add_inode_backref(inode_cache, location.objectid,
958 key->objectid, key->offset, namebuf,
959 len, filetype, key->type, error);
960 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
961 add_inode_backref(root_cache, location.objectid,
962 key->objectid, key->offset,
963 namebuf, len, filetype,
966 fprintf(stderr, "warning line %d\n", __LINE__);
969 len = sizeof(*di) + name_len + data_len;
970 di = (struct btrfs_dir_item *)((char *)di + len);
973 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
974 rec->errors |= I_ERR_DUP_DIR_INDEX;
979 static int process_inode_ref(struct extent_buffer *eb,
980 int slot, struct btrfs_key *key,
981 struct shared_node *active_node)
989 struct cache_tree *inode_cache;
990 struct btrfs_inode_ref *ref;
991 char namebuf[BTRFS_NAME_LEN];
993 inode_cache = &active_node->inode_cache;
995 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
996 total = btrfs_item_size_nr(eb, slot);
997 while (cur < total) {
998 name_len = btrfs_inode_ref_name_len(eb, ref);
999 index = btrfs_inode_ref_index(eb, ref);
1000 if (name_len <= BTRFS_NAME_LEN) {
1004 len = BTRFS_NAME_LEN;
1005 error = REF_ERR_NAME_TOO_LONG;
1007 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1008 add_inode_backref(inode_cache, key->objectid, key->offset,
1009 index, namebuf, len, 0, key->type, error);
1011 len = sizeof(*ref) + name_len;
1012 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1018 static int process_inode_extref(struct extent_buffer *eb,
1019 int slot, struct btrfs_key *key,
1020 struct shared_node *active_node)
1029 struct cache_tree *inode_cache;
1030 struct btrfs_inode_extref *extref;
1031 char namebuf[BTRFS_NAME_LEN];
1033 inode_cache = &active_node->inode_cache;
1035 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1036 total = btrfs_item_size_nr(eb, slot);
1037 while (cur < total) {
1038 name_len = btrfs_inode_extref_name_len(eb, extref);
1039 index = btrfs_inode_extref_index(eb, extref);
1040 parent = btrfs_inode_extref_parent(eb, extref);
1041 if (name_len <= BTRFS_NAME_LEN) {
1045 len = BTRFS_NAME_LEN;
1046 error = REF_ERR_NAME_TOO_LONG;
1048 read_extent_buffer(eb, namebuf,
1049 (unsigned long)(extref + 1), len);
1050 add_inode_backref(inode_cache, key->objectid, parent,
1051 index, namebuf, len, 0, key->type, error);
1053 len = sizeof(*extref) + name_len;
1054 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1061 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
1063 struct btrfs_key key;
1064 struct btrfs_path path;
1065 struct extent_buffer *leaf;
1070 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1072 btrfs_init_path(&path);
1074 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1076 key.type = BTRFS_EXTENT_CSUM_KEY;
1078 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1081 if (ret > 0 && path.slots[0] > 0) {
1082 leaf = path.nodes[0];
1083 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1084 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1085 key.type == BTRFS_EXTENT_CSUM_KEY)
1090 leaf = path.nodes[0];
1091 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1092 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1096 leaf = path.nodes[0];
1099 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1100 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1101 key.type != BTRFS_EXTENT_CSUM_KEY)
1104 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1105 if (key.offset >= start + len)
1108 if (key.offset > start)
1111 size = btrfs_item_size_nr(leaf, path.slots[0]);
1112 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1113 if (csum_end > start) {
1114 size = min(csum_end - start, len);
1122 btrfs_release_path(&path);
1126 static int process_file_extent(struct btrfs_root *root,
1127 struct extent_buffer *eb,
1128 int slot, struct btrfs_key *key,
1129 struct shared_node *active_node)
1131 struct inode_record *rec;
1132 struct btrfs_file_extent_item *fi;
1134 u64 disk_bytenr = 0;
1135 u64 extent_offset = 0;
1136 u64 mask = root->sectorsize - 1;
1139 rec = active_node->current;
1140 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1141 rec->found_file_extent = 1;
1143 if (rec->extent_start == (u64)-1) {
1144 rec->extent_start = key->offset;
1145 rec->extent_end = key->offset;
1148 if (rec->extent_end > key->offset)
1149 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1150 else if (rec->extent_end < key->offset &&
1151 rec->extent_end < rec->first_extent_gap)
1152 rec->first_extent_gap = rec->extent_end;
1154 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1155 extent_type = btrfs_file_extent_type(eb, fi);
1157 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1158 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1160 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1161 rec->found_size += num_bytes;
1162 num_bytes = (num_bytes + mask) & ~mask;
1163 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1164 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1165 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1166 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1167 extent_offset = btrfs_file_extent_offset(eb, fi);
1168 if (num_bytes == 0 || (num_bytes & mask))
1169 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1170 if (num_bytes + extent_offset >
1171 btrfs_file_extent_ram_bytes(eb, fi))
1172 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1173 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1174 (btrfs_file_extent_compression(eb, fi) ||
1175 btrfs_file_extent_encryption(eb, fi) ||
1176 btrfs_file_extent_other_encoding(eb, fi)))
1177 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1178 if (disk_bytenr > 0)
1179 rec->found_size += num_bytes;
1181 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1183 rec->extent_end = key->offset + num_bytes;
1185 if (disk_bytenr > 0) {
1187 if (btrfs_file_extent_compression(eb, fi))
1188 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1190 disk_bytenr += extent_offset;
1192 found = count_csum_range(root, disk_bytenr, num_bytes);
1193 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1195 rec->found_csum_item = 1;
1196 if (found < num_bytes)
1197 rec->some_csum_missing = 1;
1198 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1200 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1206 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1207 struct walk_control *wc)
1209 struct btrfs_key key;
1213 struct cache_tree *inode_cache;
1214 struct shared_node *active_node;
1216 if (wc->root_level == wc->active_node &&
1217 btrfs_root_refs(&root->root_item) == 0)
1220 active_node = wc->nodes[wc->active_node];
1221 inode_cache = &active_node->inode_cache;
1222 nritems = btrfs_header_nritems(eb);
1223 for (i = 0; i < nritems; i++) {
1224 btrfs_item_key_to_cpu(eb, &key, i);
1226 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1229 if (active_node->current == NULL ||
1230 active_node->current->ino < key.objectid) {
1231 if (active_node->current) {
1232 active_node->current->checked = 1;
1233 maybe_free_inode_rec(inode_cache,
1234 active_node->current);
1236 active_node->current = get_inode_rec(inode_cache,
1240 case BTRFS_DIR_ITEM_KEY:
1241 case BTRFS_DIR_INDEX_KEY:
1242 ret = process_dir_item(root, eb, i, &key, active_node);
1244 case BTRFS_INODE_REF_KEY:
1245 ret = process_inode_ref(eb, i, &key, active_node);
1247 case BTRFS_INODE_EXTREF_KEY:
1248 ret = process_inode_extref(eb, i, &key, active_node);
1250 case BTRFS_INODE_ITEM_KEY:
1251 ret = process_inode_item(eb, i, &key, active_node);
1253 case BTRFS_EXTENT_DATA_KEY:
1254 ret = process_file_extent(root, eb, i, &key,
1264 static void reada_walk_down(struct btrfs_root *root,
1265 struct extent_buffer *node, int slot)
1275 level = btrfs_header_level(node);
1279 nritems = btrfs_header_nritems(node);
1280 blocksize = btrfs_level_size(root, level - 1);
1281 for (i = slot; i < nritems; i++) {
1282 bytenr = btrfs_node_blockptr(node, i);
1283 ptr_gen = btrfs_node_ptr_generation(node, i);
1284 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1290 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1291 struct walk_control *wc, int *level)
1295 struct extent_buffer *next;
1296 struct extent_buffer *cur;
1301 WARN_ON(*level < 0);
1302 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1303 ret = btrfs_lookup_extent_info(NULL, root,
1304 path->nodes[*level]->start,
1305 *level, 1, &refs, NULL);
1312 ret = enter_shared_node(root, path->nodes[*level]->start,
1320 while (*level >= 0) {
1321 WARN_ON(*level < 0);
1322 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1323 cur = path->nodes[*level];
1325 if (btrfs_header_level(cur) != *level)
1328 if (path->slots[*level] >= btrfs_header_nritems(cur))
1331 ret = process_one_leaf(root, cur, wc);
1334 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1335 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1336 blocksize = btrfs_level_size(root, *level - 1);
1337 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1343 ret = enter_shared_node(root, bytenr, refs,
1346 path->slots[*level]++;
1351 next = btrfs_find_tree_block(root, bytenr, blocksize);
1352 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1353 free_extent_buffer(next);
1354 reada_walk_down(root, cur, path->slots[*level]);
1355 next = read_tree_block(root, bytenr, blocksize,
1363 *level = *level - 1;
1364 free_extent_buffer(path->nodes[*level]);
1365 path->nodes[*level] = next;
1366 path->slots[*level] = 0;
1369 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1373 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1374 struct walk_control *wc, int *level)
1377 struct extent_buffer *leaf;
1379 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1380 leaf = path->nodes[i];
1381 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1386 free_extent_buffer(path->nodes[*level]);
1387 path->nodes[*level] = NULL;
1388 BUG_ON(*level > wc->active_node);
1389 if (*level == wc->active_node)
1390 leave_shared_node(root, wc, *level);
1397 static int check_root_dir(struct inode_record *rec)
1399 struct inode_backref *backref;
1402 if (!rec->found_inode_item || rec->errors)
1404 if (rec->nlink != 1 || rec->found_link != 0)
1406 if (list_empty(&rec->backrefs))
1408 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1409 if (!backref->found_inode_ref)
1411 if (backref->index != 0 || backref->namelen != 2 ||
1412 memcmp(backref->name, "..", 2))
1414 if (backref->found_dir_index || backref->found_dir_item)
1421 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1423 struct btrfs_trans_handle *trans;
1424 struct btrfs_path *path;
1425 struct btrfs_inode_item *ei;
1426 struct btrfs_key key;
1429 /* So far we just fix dir isize wrong */
1430 if (!(rec->errors & I_ERR_DIR_ISIZE_WRONG))
1433 path = btrfs_alloc_path();
1437 trans = btrfs_start_transaction(root, 1);
1438 if (IS_ERR(trans)) {
1439 btrfs_free_path(path);
1440 return PTR_ERR(trans);
1443 key.objectid = rec->ino;
1444 key.type = BTRFS_INODE_ITEM_KEY;
1445 key.offset = (u64)-1;
1447 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1451 if (!path->slots[0]) {
1458 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1459 if (key.objectid != rec->ino) {
1464 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1465 struct btrfs_inode_item);
1466 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1467 btrfs_mark_buffer_dirty(path->nodes[0]);
1468 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1469 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1470 root->root_key.objectid);
1472 btrfs_commit_transaction(trans, root);
1473 btrfs_free_path(path);
1477 static int check_inode_recs(struct btrfs_root *root,
1478 struct cache_tree *inode_cache)
1480 struct cache_extent *cache;
1481 struct ptr_node *node;
1482 struct inode_record *rec;
1483 struct inode_backref *backref;
1486 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1488 if (btrfs_root_refs(&root->root_item) == 0) {
1489 if (!cache_tree_empty(inode_cache))
1490 fprintf(stderr, "warning line %d\n", __LINE__);
1494 rec = get_inode_rec(inode_cache, root_dirid, 0);
1496 ret = check_root_dir(rec);
1498 fprintf(stderr, "root %llu root dir %llu error\n",
1499 (unsigned long long)root->root_key.objectid,
1500 (unsigned long long)root_dirid);
1504 fprintf(stderr, "root %llu root dir %llu not found\n",
1505 (unsigned long long)root->root_key.objectid,
1506 (unsigned long long)root_dirid);
1510 cache = search_cache_extent(inode_cache, 0);
1513 node = container_of(cache, struct ptr_node, cache);
1515 remove_cache_extent(inode_cache, &node->cache);
1517 if (rec->ino == root_dirid ||
1518 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1519 free_inode_rec(rec);
1523 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1524 ret = check_orphan_item(root, rec->ino);
1526 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1527 if (can_free_inode_rec(rec)) {
1528 free_inode_rec(rec);
1534 ret = try_repair_inode(root, rec);
1535 if (ret == 0 && can_free_inode_rec(rec)) {
1536 free_inode_rec(rec);
1543 if (!rec->found_inode_item)
1544 rec->errors |= I_ERR_NO_INODE_ITEM;
1545 if (rec->found_link != rec->nlink)
1546 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1547 fprintf(stderr, "root %llu inode %llu errors %x",
1548 (unsigned long long) root->root_key.objectid,
1549 (unsigned long long) rec->ino, rec->errors);
1550 print_inode_error(rec->errors);
1551 list_for_each_entry(backref, &rec->backrefs, list) {
1552 if (!backref->found_dir_item)
1553 backref->errors |= REF_ERR_NO_DIR_ITEM;
1554 if (!backref->found_dir_index)
1555 backref->errors |= REF_ERR_NO_DIR_INDEX;
1556 if (!backref->found_inode_ref)
1557 backref->errors |= REF_ERR_NO_INODE_REF;
1558 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1559 " namelen %u name %s filetype %d error %x",
1560 (unsigned long long)backref->dir,
1561 (unsigned long long)backref->index,
1562 backref->namelen, backref->name,
1563 backref->filetype, backref->errors);
1564 print_ref_error(backref->errors);
1566 free_inode_rec(rec);
1568 return (error > 0) ? -1 : 0;
1571 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1574 struct cache_extent *cache;
1575 struct root_record *rec = NULL;
1578 cache = lookup_cache_extent(root_cache, objectid, 1);
1580 rec = container_of(cache, struct root_record, cache);
1582 rec = calloc(1, sizeof(*rec));
1583 rec->objectid = objectid;
1584 INIT_LIST_HEAD(&rec->backrefs);
1585 rec->cache.start = objectid;
1586 rec->cache.size = 1;
1588 ret = insert_cache_extent(root_cache, &rec->cache);
1594 static struct root_backref *get_root_backref(struct root_record *rec,
1595 u64 ref_root, u64 dir, u64 index,
1596 const char *name, int namelen)
1598 struct root_backref *backref;
1600 list_for_each_entry(backref, &rec->backrefs, list) {
1601 if (backref->ref_root != ref_root || backref->dir != dir ||
1602 backref->namelen != namelen)
1604 if (memcmp(name, backref->name, namelen))
1609 backref = malloc(sizeof(*backref) + namelen + 1);
1610 memset(backref, 0, sizeof(*backref));
1611 backref->ref_root = ref_root;
1613 backref->index = index;
1614 backref->namelen = namelen;
1615 memcpy(backref->name, name, namelen);
1616 backref->name[namelen] = '\0';
1617 list_add_tail(&backref->list, &rec->backrefs);
1621 static void free_root_record(struct cache_extent *cache)
1623 struct root_record *rec;
1624 struct root_backref *backref;
1626 rec = container_of(cache, struct root_record, cache);
1627 while (!list_empty(&rec->backrefs)) {
1628 backref = list_entry(rec->backrefs.next,
1629 struct root_backref, list);
1630 list_del(&backref->list);
1637 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
1639 static int add_root_backref(struct cache_tree *root_cache,
1640 u64 root_id, u64 ref_root, u64 dir, u64 index,
1641 const char *name, int namelen,
1642 int item_type, int errors)
1644 struct root_record *rec;
1645 struct root_backref *backref;
1647 rec = get_root_rec(root_cache, root_id);
1648 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1650 backref->errors |= errors;
1652 if (item_type != BTRFS_DIR_ITEM_KEY) {
1653 if (backref->found_dir_index || backref->found_back_ref ||
1654 backref->found_forward_ref) {
1655 if (backref->index != index)
1656 backref->errors |= REF_ERR_INDEX_UNMATCH;
1658 backref->index = index;
1662 if (item_type == BTRFS_DIR_ITEM_KEY) {
1663 if (backref->found_forward_ref)
1665 backref->found_dir_item = 1;
1666 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1667 backref->found_dir_index = 1;
1668 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1669 if (backref->found_forward_ref)
1670 backref->errors |= REF_ERR_DUP_ROOT_REF;
1671 else if (backref->found_dir_item)
1673 backref->found_forward_ref = 1;
1674 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1675 if (backref->found_back_ref)
1676 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1677 backref->found_back_ref = 1;
1682 if (backref->found_forward_ref && backref->found_dir_item)
1683 backref->reachable = 1;
1687 static int merge_root_recs(struct btrfs_root *root,
1688 struct cache_tree *src_cache,
1689 struct cache_tree *dst_cache)
1691 struct cache_extent *cache;
1692 struct ptr_node *node;
1693 struct inode_record *rec;
1694 struct inode_backref *backref;
1696 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1697 free_inode_recs_tree(src_cache);
1702 cache = search_cache_extent(src_cache, 0);
1705 node = container_of(cache, struct ptr_node, cache);
1707 remove_cache_extent(src_cache, &node->cache);
1710 if (!is_child_root(root, root->objectid, rec->ino))
1713 list_for_each_entry(backref, &rec->backrefs, list) {
1714 BUG_ON(backref->found_inode_ref);
1715 if (backref->found_dir_item)
1716 add_root_backref(dst_cache, rec->ino,
1717 root->root_key.objectid, backref->dir,
1718 backref->index, backref->name,
1719 backref->namelen, BTRFS_DIR_ITEM_KEY,
1721 if (backref->found_dir_index)
1722 add_root_backref(dst_cache, rec->ino,
1723 root->root_key.objectid, backref->dir,
1724 backref->index, backref->name,
1725 backref->namelen, BTRFS_DIR_INDEX_KEY,
1729 free_inode_rec(rec);
1734 static int check_root_refs(struct btrfs_root *root,
1735 struct cache_tree *root_cache)
1737 struct root_record *rec;
1738 struct root_record *ref_root;
1739 struct root_backref *backref;
1740 struct cache_extent *cache;
1746 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1749 /* fixme: this can not detect circular references */
1752 cache = search_cache_extent(root_cache, 0);
1756 rec = container_of(cache, struct root_record, cache);
1757 cache = next_cache_extent(cache);
1759 if (rec->found_ref == 0)
1762 list_for_each_entry(backref, &rec->backrefs, list) {
1763 if (!backref->reachable)
1766 ref_root = get_root_rec(root_cache,
1768 if (ref_root->found_ref > 0)
1771 backref->reachable = 0;
1773 if (rec->found_ref == 0)
1779 cache = search_cache_extent(root_cache, 0);
1783 rec = container_of(cache, struct root_record, cache);
1784 cache = next_cache_extent(cache);
1786 if (rec->found_ref == 0 &&
1787 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1788 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1789 ret = check_orphan_item(root->fs_info->tree_root,
1795 * If we don't have a root item then we likely just have
1796 * a dir item in a snapshot for this root but no actual
1797 * ref key or anything so it's meaningless.
1799 if (!rec->found_root_item)
1802 fprintf(stderr, "fs tree %llu not referenced\n",
1803 (unsigned long long)rec->objectid);
1807 if (rec->found_ref > 0 && !rec->found_root_item)
1809 list_for_each_entry(backref, &rec->backrefs, list) {
1810 if (!backref->found_dir_item)
1811 backref->errors |= REF_ERR_NO_DIR_ITEM;
1812 if (!backref->found_dir_index)
1813 backref->errors |= REF_ERR_NO_DIR_INDEX;
1814 if (!backref->found_back_ref)
1815 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1816 if (!backref->found_forward_ref)
1817 backref->errors |= REF_ERR_NO_ROOT_REF;
1818 if (backref->reachable && backref->errors)
1825 fprintf(stderr, "fs tree %llu refs %u %s\n",
1826 (unsigned long long)rec->objectid, rec->found_ref,
1827 rec->found_root_item ? "" : "not found");
1829 list_for_each_entry(backref, &rec->backrefs, list) {
1830 if (!backref->reachable)
1832 if (!backref->errors && rec->found_root_item)
1834 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1835 " index %llu namelen %u name %s error %x\n",
1836 (unsigned long long)backref->ref_root,
1837 (unsigned long long)backref->dir,
1838 (unsigned long long)backref->index,
1839 backref->namelen, backref->name,
1843 return errors > 0 ? 1 : 0;
1846 static int process_root_ref(struct extent_buffer *eb, int slot,
1847 struct btrfs_key *key,
1848 struct cache_tree *root_cache)
1854 struct btrfs_root_ref *ref;
1855 char namebuf[BTRFS_NAME_LEN];
1858 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1860 dirid = btrfs_root_ref_dirid(eb, ref);
1861 index = btrfs_root_ref_sequence(eb, ref);
1862 name_len = btrfs_root_ref_name_len(eb, ref);
1864 if (name_len <= BTRFS_NAME_LEN) {
1868 len = BTRFS_NAME_LEN;
1869 error = REF_ERR_NAME_TOO_LONG;
1871 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1873 if (key->type == BTRFS_ROOT_REF_KEY) {
1874 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1875 index, namebuf, len, key->type, error);
1877 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1878 index, namebuf, len, key->type, error);
1883 static int check_fs_root(struct btrfs_root *root,
1884 struct cache_tree *root_cache,
1885 struct walk_control *wc)
1890 struct btrfs_path path;
1891 struct shared_node root_node;
1892 struct root_record *rec;
1893 struct btrfs_root_item *root_item = &root->root_item;
1895 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1896 rec = get_root_rec(root_cache, root->root_key.objectid);
1897 if (btrfs_root_refs(root_item) > 0)
1898 rec->found_root_item = 1;
1901 btrfs_init_path(&path);
1902 memset(&root_node, 0, sizeof(root_node));
1903 cache_tree_init(&root_node.root_cache);
1904 cache_tree_init(&root_node.inode_cache);
1906 level = btrfs_header_level(root->node);
1907 memset(wc->nodes, 0, sizeof(wc->nodes));
1908 wc->nodes[level] = &root_node;
1909 wc->active_node = level;
1910 wc->root_level = level;
1912 if (btrfs_root_refs(root_item) > 0 ||
1913 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1914 path.nodes[level] = root->node;
1915 extent_buffer_get(root->node);
1916 path.slots[level] = 0;
1918 struct btrfs_key key;
1919 struct btrfs_disk_key found_key;
1921 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1922 level = root_item->drop_level;
1923 path.lowest_level = level;
1924 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1926 btrfs_node_key(path.nodes[level], &found_key,
1928 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1929 sizeof(found_key)));
1933 wret = walk_down_tree(root, &path, wc, &level);
1939 wret = walk_up_tree(root, &path, wc, &level);
1945 btrfs_release_path(&path);
1947 merge_root_recs(root, &root_node.root_cache, root_cache);
1949 if (root_node.current) {
1950 root_node.current->checked = 1;
1951 maybe_free_inode_rec(&root_node.inode_cache,
1955 ret = check_inode_recs(root, &root_node.inode_cache);
1959 static int fs_root_objectid(u64 objectid)
1961 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1962 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1963 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1964 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1965 objectid <= BTRFS_LAST_FREE_OBJECTID))
1970 static int check_fs_roots(struct btrfs_root *root,
1971 struct cache_tree *root_cache)
1973 struct btrfs_path path;
1974 struct btrfs_key key;
1975 struct walk_control wc;
1976 struct extent_buffer *leaf;
1977 struct btrfs_root *tmp_root;
1978 struct btrfs_root *tree_root = root->fs_info->tree_root;
1983 * Just in case we made any changes to the extent tree that weren't
1984 * reflected into the free space cache yet.
1987 reset_cached_block_groups(root->fs_info);
1988 memset(&wc, 0, sizeof(wc));
1989 cache_tree_init(&wc.shared);
1990 btrfs_init_path(&path);
1994 key.type = BTRFS_ROOT_ITEM_KEY;
1995 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1998 leaf = path.nodes[0];
1999 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2000 ret = btrfs_next_leaf(tree_root, &path);
2003 leaf = path.nodes[0];
2005 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2006 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2007 fs_root_objectid(key.objectid)) {
2008 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
2010 if (IS_ERR(tmp_root)) {
2014 ret = check_fs_root(tmp_root, root_cache, &wc);
2017 btrfs_free_fs_root(tmp_root);
2018 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2019 key.type == BTRFS_ROOT_BACKREF_KEY) {
2020 process_root_ref(leaf, path.slots[0], &key,
2026 btrfs_release_path(&path);
2028 if (!cache_tree_empty(&wc.shared))
2029 fprintf(stderr, "warning line %d\n", __LINE__);
2034 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2036 struct list_head *cur = rec->backrefs.next;
2037 struct extent_backref *back;
2038 struct tree_backref *tback;
2039 struct data_backref *dback;
2043 while(cur != &rec->backrefs) {
2044 back = list_entry(cur, struct extent_backref, list);
2046 if (!back->found_extent_tree) {
2050 if (back->is_data) {
2051 dback = (struct data_backref *)back;
2052 fprintf(stderr, "Backref %llu %s %llu"
2053 " owner %llu offset %llu num_refs %lu"
2054 " not found in extent tree\n",
2055 (unsigned long long)rec->start,
2056 back->full_backref ?
2058 back->full_backref ?
2059 (unsigned long long)dback->parent:
2060 (unsigned long long)dback->root,
2061 (unsigned long long)dback->owner,
2062 (unsigned long long)dback->offset,
2063 (unsigned long)dback->num_refs);
2065 tback = (struct tree_backref *)back;
2066 fprintf(stderr, "Backref %llu parent %llu"
2067 " root %llu not found in extent tree\n",
2068 (unsigned long long)rec->start,
2069 (unsigned long long)tback->parent,
2070 (unsigned long long)tback->root);
2073 if (!back->is_data && !back->found_ref) {
2077 tback = (struct tree_backref *)back;
2078 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2079 (unsigned long long)rec->start,
2080 back->full_backref ? "parent" : "root",
2081 back->full_backref ?
2082 (unsigned long long)tback->parent :
2083 (unsigned long long)tback->root, back);
2085 if (back->is_data) {
2086 dback = (struct data_backref *)back;
2087 if (dback->found_ref != dback->num_refs) {
2091 fprintf(stderr, "Incorrect local backref count"
2092 " on %llu %s %llu owner %llu"
2093 " offset %llu found %u wanted %u back %p\n",
2094 (unsigned long long)rec->start,
2095 back->full_backref ?
2097 back->full_backref ?
2098 (unsigned long long)dback->parent:
2099 (unsigned long long)dback->root,
2100 (unsigned long long)dback->owner,
2101 (unsigned long long)dback->offset,
2102 dback->found_ref, dback->num_refs, back);
2104 if (dback->disk_bytenr != rec->start) {
2108 fprintf(stderr, "Backref disk bytenr does not"
2109 " match extent record, bytenr=%llu, "
2110 "ref bytenr=%llu\n",
2111 (unsigned long long)rec->start,
2112 (unsigned long long)dback->disk_bytenr);
2115 if (dback->bytes != rec->nr) {
2119 fprintf(stderr, "Backref bytes do not match "
2120 "extent backref, bytenr=%llu, ref "
2121 "bytes=%llu, backref bytes=%llu\n",
2122 (unsigned long long)rec->start,
2123 (unsigned long long)rec->nr,
2124 (unsigned long long)dback->bytes);
2127 if (!back->is_data) {
2130 dback = (struct data_backref *)back;
2131 found += dback->found_ref;
2134 if (found != rec->refs) {
2138 fprintf(stderr, "Incorrect global backref count "
2139 "on %llu found %llu wanted %llu\n",
2140 (unsigned long long)rec->start,
2141 (unsigned long long)found,
2142 (unsigned long long)rec->refs);
2148 static int free_all_extent_backrefs(struct extent_record *rec)
2150 struct extent_backref *back;
2151 struct list_head *cur;
2152 while (!list_empty(&rec->backrefs)) {
2153 cur = rec->backrefs.next;
2154 back = list_entry(cur, struct extent_backref, list);
2161 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2162 struct cache_tree *extent_cache)
2164 struct cache_extent *cache;
2165 struct extent_record *rec;
2168 cache = first_cache_extent(extent_cache);
2171 rec = container_of(cache, struct extent_record, cache);
2172 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2173 remove_cache_extent(extent_cache, cache);
2174 free_all_extent_backrefs(rec);
2179 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2180 struct extent_record *rec)
2182 if (rec->content_checked && rec->owner_ref_checked &&
2183 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2184 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2185 remove_cache_extent(extent_cache, &rec->cache);
2186 free_all_extent_backrefs(rec);
2187 list_del_init(&rec->list);
2193 static int check_owner_ref(struct btrfs_root *root,
2194 struct extent_record *rec,
2195 struct extent_buffer *buf)
2197 struct extent_backref *node;
2198 struct tree_backref *back;
2199 struct btrfs_root *ref_root;
2200 struct btrfs_key key;
2201 struct btrfs_path path;
2202 struct extent_buffer *parent;
2207 list_for_each_entry(node, &rec->backrefs, list) {
2210 if (!node->found_ref)
2212 if (node->full_backref)
2214 back = (struct tree_backref *)node;
2215 if (btrfs_header_owner(buf) == back->root)
2218 BUG_ON(rec->is_root);
2220 /* try to find the block by search corresponding fs tree */
2221 key.objectid = btrfs_header_owner(buf);
2222 key.type = BTRFS_ROOT_ITEM_KEY;
2223 key.offset = (u64)-1;
2225 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2226 if (IS_ERR(ref_root))
2229 level = btrfs_header_level(buf);
2231 btrfs_item_key_to_cpu(buf, &key, 0);
2233 btrfs_node_key_to_cpu(buf, &key, 0);
2235 btrfs_init_path(&path);
2236 path.lowest_level = level + 1;
2237 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2241 parent = path.nodes[level + 1];
2242 if (parent && buf->start == btrfs_node_blockptr(parent,
2243 path.slots[level + 1]))
2246 btrfs_release_path(&path);
2247 return found ? 0 : 1;
2250 static int is_extent_tree_record(struct extent_record *rec)
2252 struct list_head *cur = rec->backrefs.next;
2253 struct extent_backref *node;
2254 struct tree_backref *back;
2257 while(cur != &rec->backrefs) {
2258 node = list_entry(cur, struct extent_backref, list);
2262 back = (struct tree_backref *)node;
2263 if (node->full_backref)
2265 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2272 static int record_bad_block_io(struct btrfs_fs_info *info,
2273 struct cache_tree *extent_cache,
2276 struct extent_record *rec;
2277 struct cache_extent *cache;
2278 struct btrfs_key key;
2280 cache = lookup_cache_extent(extent_cache, start, len);
2284 rec = container_of(cache, struct extent_record, cache);
2285 if (!is_extent_tree_record(rec))
2288 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2289 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2292 static int check_block(struct btrfs_root *root,
2293 struct cache_tree *extent_cache,
2294 struct extent_buffer *buf, u64 flags)
2296 struct extent_record *rec;
2297 struct cache_extent *cache;
2298 struct btrfs_key key;
2302 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2305 rec = container_of(cache, struct extent_record, cache);
2306 rec->generation = btrfs_header_generation(buf);
2308 level = btrfs_header_level(buf);
2309 if (btrfs_header_nritems(buf) > 0) {
2312 btrfs_item_key_to_cpu(buf, &key, 0);
2314 btrfs_node_key_to_cpu(buf, &key, 0);
2316 rec->info_objectid = key.objectid;
2318 rec->info_level = level;
2320 if (btrfs_is_leaf(buf))
2321 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2323 ret = btrfs_check_node(root, &rec->parent_key, buf);
2326 fprintf(stderr, "bad block %llu\n",
2327 (unsigned long long)buf->start);
2329 rec->content_checked = 1;
2330 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2331 rec->owner_ref_checked = 1;
2333 ret = check_owner_ref(root, rec, buf);
2335 rec->owner_ref_checked = 1;
2339 maybe_free_extent_rec(extent_cache, rec);
2343 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2344 u64 parent, u64 root)
2346 struct list_head *cur = rec->backrefs.next;
2347 struct extent_backref *node;
2348 struct tree_backref *back;
2350 while(cur != &rec->backrefs) {
2351 node = list_entry(cur, struct extent_backref, list);
2355 back = (struct tree_backref *)node;
2357 if (!node->full_backref)
2359 if (parent == back->parent)
2362 if (node->full_backref)
2364 if (back->root == root)
2371 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2372 u64 parent, u64 root)
2374 struct tree_backref *ref = malloc(sizeof(*ref));
2375 memset(&ref->node, 0, sizeof(ref->node));
2377 ref->parent = parent;
2378 ref->node.full_backref = 1;
2381 ref->node.full_backref = 0;
2383 list_add_tail(&ref->node.list, &rec->backrefs);
2388 static struct data_backref *find_data_backref(struct extent_record *rec,
2389 u64 parent, u64 root,
2390 u64 owner, u64 offset,
2392 u64 disk_bytenr, u64 bytes)
2394 struct list_head *cur = rec->backrefs.next;
2395 struct extent_backref *node;
2396 struct data_backref *back;
2398 while(cur != &rec->backrefs) {
2399 node = list_entry(cur, struct extent_backref, list);
2403 back = (struct data_backref *)node;
2405 if (!node->full_backref)
2407 if (parent == back->parent)
2410 if (node->full_backref)
2412 if (back->root == root && back->owner == owner &&
2413 back->offset == offset) {
2414 if (found_ref && node->found_ref &&
2415 (back->bytes != bytes ||
2416 back->disk_bytenr != disk_bytenr))
2425 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2426 u64 parent, u64 root,
2427 u64 owner, u64 offset,
2430 struct data_backref *ref = malloc(sizeof(*ref));
2431 memset(&ref->node, 0, sizeof(ref->node));
2432 ref->node.is_data = 1;
2435 ref->parent = parent;
2438 ref->node.full_backref = 1;
2442 ref->offset = offset;
2443 ref->node.full_backref = 0;
2445 ref->bytes = max_size;
2448 list_add_tail(&ref->node.list, &rec->backrefs);
2449 if (max_size > rec->max_size)
2450 rec->max_size = max_size;
2454 static int add_extent_rec(struct cache_tree *extent_cache,
2455 struct btrfs_key *parent_key,
2456 u64 start, u64 nr, u64 extent_item_refs,
2457 int is_root, int inc_ref, int set_checked,
2458 int metadata, int extent_rec, u64 max_size)
2460 struct extent_record *rec;
2461 struct cache_extent *cache;
2465 cache = lookup_cache_extent(extent_cache, start, nr);
2467 rec = container_of(cache, struct extent_record, cache);
2471 rec->nr = max(nr, max_size);
2474 * We need to make sure to reset nr to whatever the extent
2475 * record says was the real size, this way we can compare it to
2479 if (start != rec->start || rec->found_rec) {
2480 struct extent_record *tmp;
2483 if (list_empty(&rec->list))
2484 list_add_tail(&rec->list,
2485 &duplicate_extents);
2488 * We have to do this song and dance in case we
2489 * find an extent record that falls inside of
2490 * our current extent record but does not have
2491 * the same objectid.
2493 tmp = malloc(sizeof(*tmp));
2497 tmp->max_size = max_size;
2500 tmp->metadata = metadata;
2501 tmp->extent_item_refs = extent_item_refs;
2502 INIT_LIST_HEAD(&tmp->list);
2503 list_add_tail(&tmp->list, &rec->dups);
2504 rec->num_duplicates++;
2511 if (extent_item_refs && !dup) {
2512 if (rec->extent_item_refs) {
2513 fprintf(stderr, "block %llu rec "
2514 "extent_item_refs %llu, passed %llu\n",
2515 (unsigned long long)start,
2516 (unsigned long long)
2517 rec->extent_item_refs,
2518 (unsigned long long)extent_item_refs);
2520 rec->extent_item_refs = extent_item_refs;
2525 rec->content_checked = 1;
2526 rec->owner_ref_checked = 1;
2530 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2532 if (rec->max_size < max_size)
2533 rec->max_size = max_size;
2535 maybe_free_extent_rec(extent_cache, rec);
2538 rec = malloc(sizeof(*rec));
2540 rec->max_size = max_size;
2541 rec->nr = max(nr, max_size);
2542 rec->found_rec = extent_rec;
2543 rec->content_checked = 0;
2544 rec->owner_ref_checked = 0;
2545 rec->num_duplicates = 0;
2546 rec->metadata = metadata;
2547 INIT_LIST_HEAD(&rec->backrefs);
2548 INIT_LIST_HEAD(&rec->dups);
2549 INIT_LIST_HEAD(&rec->list);
2561 if (extent_item_refs)
2562 rec->extent_item_refs = extent_item_refs;
2564 rec->extent_item_refs = 0;
2567 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2569 memset(&rec->parent_key, 0, sizeof(*parent_key));
2571 rec->cache.start = start;
2572 rec->cache.size = nr;
2573 ret = insert_cache_extent(extent_cache, &rec->cache);
2577 rec->content_checked = 1;
2578 rec->owner_ref_checked = 1;
2583 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2584 u64 parent, u64 root, int found_ref)
2586 struct extent_record *rec;
2587 struct tree_backref *back;
2588 struct cache_extent *cache;
2590 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2592 add_extent_rec(extent_cache, NULL, bytenr,
2593 1, 0, 0, 0, 0, 1, 0, 0);
2594 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2599 rec = container_of(cache, struct extent_record, cache);
2600 if (rec->start != bytenr) {
2604 back = find_tree_backref(rec, parent, root);
2606 back = alloc_tree_backref(rec, parent, root);
2609 if (back->node.found_ref) {
2610 fprintf(stderr, "Extent back ref already exists "
2611 "for %llu parent %llu root %llu \n",
2612 (unsigned long long)bytenr,
2613 (unsigned long long)parent,
2614 (unsigned long long)root);
2616 back->node.found_ref = 1;
2618 if (back->node.found_extent_tree) {
2619 fprintf(stderr, "Extent back ref already exists "
2620 "for %llu parent %llu root %llu \n",
2621 (unsigned long long)bytenr,
2622 (unsigned long long)parent,
2623 (unsigned long long)root);
2625 back->node.found_extent_tree = 1;
2630 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2631 u64 parent, u64 root, u64 owner, u64 offset,
2632 u32 num_refs, int found_ref, u64 max_size)
2634 struct extent_record *rec;
2635 struct data_backref *back;
2636 struct cache_extent *cache;
2638 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2640 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2642 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2647 rec = container_of(cache, struct extent_record, cache);
2648 if (rec->max_size < max_size)
2649 rec->max_size = max_size;
2652 * If found_ref is set then max_size is the real size and must match the
2653 * existing refs. So if we have already found a ref then we need to
2654 * make sure that this ref matches the existing one, otherwise we need
2655 * to add a new backref so we can notice that the backrefs don't match
2656 * and we need to figure out who is telling the truth. This is to
2657 * account for that awful fsync bug I introduced where we'd end up with
2658 * a btrfs_file_extent_item that would have its length include multiple
2659 * prealloc extents or point inside of a prealloc extent.
2661 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2664 back = alloc_data_backref(rec, parent, root, owner, offset,
2668 BUG_ON(num_refs != 1);
2669 if (back->node.found_ref)
2670 BUG_ON(back->bytes != max_size);
2671 back->node.found_ref = 1;
2672 back->found_ref += 1;
2673 back->bytes = max_size;
2674 back->disk_bytenr = bytenr;
2676 rec->content_checked = 1;
2677 rec->owner_ref_checked = 1;
2679 if (back->node.found_extent_tree) {
2680 fprintf(stderr, "Extent back ref already exists "
2681 "for %llu parent %llu root %llu"
2682 "owner %llu offset %llu num_refs %lu\n",
2683 (unsigned long long)bytenr,
2684 (unsigned long long)parent,
2685 (unsigned long long)root,
2686 (unsigned long long)owner,
2687 (unsigned long long)offset,
2688 (unsigned long)num_refs);
2690 back->num_refs = num_refs;
2691 back->node.found_extent_tree = 1;
2696 static int add_pending(struct cache_tree *pending,
2697 struct cache_tree *seen, u64 bytenr, u32 size)
2700 ret = add_cache_extent(seen, bytenr, size);
2703 add_cache_extent(pending, bytenr, size);
2707 static int pick_next_pending(struct cache_tree *pending,
2708 struct cache_tree *reada,
2709 struct cache_tree *nodes,
2710 u64 last, struct block_info *bits, int bits_nr,
2713 unsigned long node_start = last;
2714 struct cache_extent *cache;
2717 cache = search_cache_extent(reada, 0);
2719 bits[0].start = cache->start;
2720 bits[1].size = cache->size;
2725 if (node_start > 32768)
2726 node_start -= 32768;
2728 cache = search_cache_extent(nodes, node_start);
2730 cache = search_cache_extent(nodes, 0);
2733 cache = search_cache_extent(pending, 0);
2738 bits[ret].start = cache->start;
2739 bits[ret].size = cache->size;
2740 cache = next_cache_extent(cache);
2742 } while (cache && ret < bits_nr);
2748 bits[ret].start = cache->start;
2749 bits[ret].size = cache->size;
2750 cache = next_cache_extent(cache);
2752 } while (cache && ret < bits_nr);
2754 if (bits_nr - ret > 8) {
2755 u64 lookup = bits[0].start + bits[0].size;
2756 struct cache_extent *next;
2757 next = search_cache_extent(pending, lookup);
2759 if (next->start - lookup > 32768)
2761 bits[ret].start = next->start;
2762 bits[ret].size = next->size;
2763 lookup = next->start + next->size;
2767 next = next_cache_extent(next);
2775 static void free_chunk_record(struct cache_extent *cache)
2777 struct chunk_record *rec;
2779 rec = container_of(cache, struct chunk_record, cache);
2783 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
2785 cache_tree_free_extents(chunk_cache, free_chunk_record);
2788 static void free_device_record(struct rb_node *node)
2790 struct device_record *rec;
2792 rec = container_of(node, struct device_record, node);
2796 FREE_RB_BASED_TREE(device_cache, free_device_record);
2798 int insert_block_group_record(struct block_group_tree *tree,
2799 struct block_group_record *bg_rec)
2803 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
2807 list_add_tail(&bg_rec->list, &tree->block_groups);
2811 static void free_block_group_record(struct cache_extent *cache)
2813 struct block_group_record *rec;
2815 rec = container_of(cache, struct block_group_record, cache);
2819 void free_block_group_tree(struct block_group_tree *tree)
2821 cache_tree_free_extents(&tree->tree, free_block_group_record);
2824 int insert_device_extent_record(struct device_extent_tree *tree,
2825 struct device_extent_record *de_rec)
2830 * Device extent is a bit different from the other extents, because
2831 * the extents which belong to the different devices may have the
2832 * same start and size, so we need use the special extent cache
2833 * search/insert functions.
2835 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
2839 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
2840 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
2844 static void free_device_extent_record(struct cache_extent *cache)
2846 struct device_extent_record *rec;
2848 rec = container_of(cache, struct device_extent_record, cache);
2852 void free_device_extent_tree(struct device_extent_tree *tree)
2854 cache_tree_free_extents(&tree->tree, free_device_extent_record);
2857 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2858 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2859 struct extent_buffer *leaf, int slot)
2861 struct btrfs_extent_ref_v0 *ref0;
2862 struct btrfs_key key;
2864 btrfs_item_key_to_cpu(leaf, &key, slot);
2865 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2866 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2867 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2869 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2870 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2876 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
2877 struct btrfs_key *key,
2880 struct btrfs_chunk *ptr;
2881 struct chunk_record *rec;
2884 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2885 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
2887 rec = malloc(btrfs_chunk_record_size(num_stripes));
2889 fprintf(stderr, "memory allocation failed\n");
2893 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
2895 INIT_LIST_HEAD(&rec->list);
2896 INIT_LIST_HEAD(&rec->dextents);
2899 rec->cache.start = key->offset;
2900 rec->cache.size = btrfs_chunk_length(leaf, ptr);
2902 rec->generation = btrfs_header_generation(leaf);
2904 rec->objectid = key->objectid;
2905 rec->type = key->type;
2906 rec->offset = key->offset;
2908 rec->length = rec->cache.size;
2909 rec->owner = btrfs_chunk_owner(leaf, ptr);
2910 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
2911 rec->type_flags = btrfs_chunk_type(leaf, ptr);
2912 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
2913 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
2914 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
2915 rec->num_stripes = num_stripes;
2916 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
2918 for (i = 0; i < rec->num_stripes; ++i) {
2919 rec->stripes[i].devid =
2920 btrfs_stripe_devid_nr(leaf, ptr, i);
2921 rec->stripes[i].offset =
2922 btrfs_stripe_offset_nr(leaf, ptr, i);
2923 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
2924 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
2931 static int process_chunk_item(struct cache_tree *chunk_cache,
2932 struct btrfs_key *key, struct extent_buffer *eb,
2935 struct chunk_record *rec;
2938 rec = btrfs_new_chunk_record(eb, key, slot);
2939 ret = insert_cache_extent(chunk_cache, &rec->cache);
2941 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
2942 rec->offset, rec->length);
2949 static int process_device_item(struct rb_root *dev_cache,
2950 struct btrfs_key *key, struct extent_buffer *eb, int slot)
2952 struct btrfs_dev_item *ptr;
2953 struct device_record *rec;
2956 ptr = btrfs_item_ptr(eb,
2957 slot, struct btrfs_dev_item);
2959 rec = malloc(sizeof(*rec));
2961 fprintf(stderr, "memory allocation failed\n");
2965 rec->devid = key->offset;
2966 rec->generation = btrfs_header_generation(eb);
2968 rec->objectid = key->objectid;
2969 rec->type = key->type;
2970 rec->offset = key->offset;
2972 rec->devid = btrfs_device_id(eb, ptr);
2973 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
2974 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
2976 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
2978 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
2985 struct block_group_record *
2986 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
2989 struct btrfs_block_group_item *ptr;
2990 struct block_group_record *rec;
2992 rec = malloc(sizeof(*rec));
2994 fprintf(stderr, "memory allocation failed\n");
2997 memset(rec, 0, sizeof(*rec));
2999 rec->cache.start = key->objectid;
3000 rec->cache.size = key->offset;
3002 rec->generation = btrfs_header_generation(leaf);
3004 rec->objectid = key->objectid;
3005 rec->type = key->type;
3006 rec->offset = key->offset;
3008 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3009 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3011 INIT_LIST_HEAD(&rec->list);
3016 static int process_block_group_item(struct block_group_tree *block_group_cache,
3017 struct btrfs_key *key,
3018 struct extent_buffer *eb, int slot)
3020 struct block_group_record *rec;
3023 rec = btrfs_new_block_group_record(eb, key, slot);
3024 ret = insert_block_group_record(block_group_cache, rec);
3026 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3027 rec->objectid, rec->offset);
3034 struct device_extent_record *
3035 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3036 struct btrfs_key *key, int slot)
3038 struct device_extent_record *rec;
3039 struct btrfs_dev_extent *ptr;
3041 rec = malloc(sizeof(*rec));
3043 fprintf(stderr, "memory allocation failed\n");
3046 memset(rec, 0, sizeof(*rec));
3048 rec->cache.objectid = key->objectid;
3049 rec->cache.start = key->offset;
3051 rec->generation = btrfs_header_generation(leaf);
3053 rec->objectid = key->objectid;
3054 rec->type = key->type;
3055 rec->offset = key->offset;
3057 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3058 rec->chunk_objecteid =
3059 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3061 btrfs_dev_extent_chunk_offset(leaf, ptr);
3062 rec->length = btrfs_dev_extent_length(leaf, ptr);
3063 rec->cache.size = rec->length;
3065 INIT_LIST_HEAD(&rec->chunk_list);
3066 INIT_LIST_HEAD(&rec->device_list);
3072 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3073 struct btrfs_key *key, struct extent_buffer *eb,
3076 struct device_extent_record *rec;
3079 rec = btrfs_new_device_extent_record(eb, key, slot);
3080 ret = insert_device_extent_record(dev_extent_cache, rec);
3083 "Device extent[%llu, %llu, %llu] existed.\n",
3084 rec->objectid, rec->offset, rec->length);
3091 static int process_extent_item(struct btrfs_root *root,
3092 struct cache_tree *extent_cache,
3093 struct extent_buffer *eb, int slot)
3095 struct btrfs_extent_item *ei;
3096 struct btrfs_extent_inline_ref *iref;
3097 struct btrfs_extent_data_ref *dref;
3098 struct btrfs_shared_data_ref *sref;
3099 struct btrfs_key key;
3103 u32 item_size = btrfs_item_size_nr(eb, slot);
3109 btrfs_item_key_to_cpu(eb, &key, slot);
3111 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3113 num_bytes = root->leafsize;
3115 num_bytes = key.offset;
3118 if (item_size < sizeof(*ei)) {
3119 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3120 struct btrfs_extent_item_v0 *ei0;
3121 BUG_ON(item_size != sizeof(*ei0));
3122 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3123 refs = btrfs_extent_refs_v0(eb, ei0);
3127 return add_extent_rec(extent_cache, NULL, key.objectid,
3128 num_bytes, refs, 0, 0, 0, metadata, 1,
3132 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3133 refs = btrfs_extent_refs(eb, ei);
3135 add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
3136 refs, 0, 0, 0, metadata, 1, num_bytes);
3138 ptr = (unsigned long)(ei + 1);
3139 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3140 key.type == BTRFS_EXTENT_ITEM_KEY)
3141 ptr += sizeof(struct btrfs_tree_block_info);
3143 end = (unsigned long)ei + item_size;
3145 iref = (struct btrfs_extent_inline_ref *)ptr;
3146 type = btrfs_extent_inline_ref_type(eb, iref);
3147 offset = btrfs_extent_inline_ref_offset(eb, iref);
3149 case BTRFS_TREE_BLOCK_REF_KEY:
3150 add_tree_backref(extent_cache, key.objectid,
3153 case BTRFS_SHARED_BLOCK_REF_KEY:
3154 add_tree_backref(extent_cache, key.objectid,
3157 case BTRFS_EXTENT_DATA_REF_KEY:
3158 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3159 add_data_backref(extent_cache, key.objectid, 0,
3160 btrfs_extent_data_ref_root(eb, dref),
3161 btrfs_extent_data_ref_objectid(eb,
3163 btrfs_extent_data_ref_offset(eb, dref),
3164 btrfs_extent_data_ref_count(eb, dref),
3167 case BTRFS_SHARED_DATA_REF_KEY:
3168 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3169 add_data_backref(extent_cache, key.objectid, offset,
3171 btrfs_shared_data_ref_count(eb, sref),
3175 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3176 key.objectid, key.type, num_bytes);
3179 ptr += btrfs_extent_inline_ref_size(type);
3186 static int check_cache_range(struct btrfs_root *root,
3187 struct btrfs_block_group_cache *cache,
3188 u64 offset, u64 bytes)
3190 struct btrfs_free_space *entry;
3196 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3197 bytenr = btrfs_sb_offset(i);
3198 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3199 cache->key.objectid, bytenr, 0,
3200 &logical, &nr, &stripe_len);
3205 if (logical[nr] + stripe_len <= offset)
3207 if (offset + bytes <= logical[nr])
3209 if (logical[nr] == offset) {
3210 if (stripe_len >= bytes) {
3214 bytes -= stripe_len;
3215 offset += stripe_len;
3216 } else if (logical[nr] < offset) {
3217 if (logical[nr] + stripe_len >=
3222 bytes = (offset + bytes) -
3223 (logical[nr] + stripe_len);
3224 offset = logical[nr] + stripe_len;
3227 * Could be tricky, the super may land in the
3228 * middle of the area we're checking. First
3229 * check the easiest case, it's at the end.
3231 if (logical[nr] + stripe_len >=
3233 bytes = logical[nr] - offset;
3237 /* Check the left side */
3238 ret = check_cache_range(root, cache,
3240 logical[nr] - offset);
3246 /* Now we continue with the right side */
3247 bytes = (offset + bytes) -
3248 (logical[nr] + stripe_len);
3249 offset = logical[nr] + stripe_len;
3256 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3258 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3259 offset, offset+bytes);
3263 if (entry->offset != offset) {
3264 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3269 if (entry->bytes != bytes) {
3270 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3271 bytes, entry->bytes, offset);
3275 unlink_free_space(cache->free_space_ctl, entry);
3280 static int verify_space_cache(struct btrfs_root *root,
3281 struct btrfs_block_group_cache *cache)
3283 struct btrfs_path *path;
3284 struct extent_buffer *leaf;
3285 struct btrfs_key key;
3289 path = btrfs_alloc_path();
3293 root = root->fs_info->extent_root;
3295 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
3297 key.objectid = last;
3299 key.type = BTRFS_EXTENT_ITEM_KEY;
3301 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3306 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3307 ret = btrfs_next_leaf(root, path);
3315 leaf = path->nodes[0];
3316 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3317 if (key.objectid >= cache->key.offset + cache->key.objectid)
3319 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3320 key.type != BTRFS_METADATA_ITEM_KEY) {
3325 if (last == key.objectid) {
3326 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3327 last = key.objectid + key.offset;
3329 last = key.objectid + root->leafsize;
3334 ret = check_cache_range(root, cache, last,
3335 key.objectid - last);
3338 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3339 last = key.objectid + key.offset;
3341 last = key.objectid + root->leafsize;
3345 if (last < cache->key.objectid + cache->key.offset)
3346 ret = check_cache_range(root, cache, last,
3347 cache->key.objectid +
3348 cache->key.offset - last);
3351 btrfs_free_path(path);
3354 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
3355 fprintf(stderr, "There are still entries left in the space "
3363 static int check_space_cache(struct btrfs_root *root)
3365 struct btrfs_block_group_cache *cache;
3366 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
3370 if (btrfs_super_generation(root->fs_info->super_copy) !=
3371 btrfs_super_cache_generation(root->fs_info->super_copy)) {
3372 printf("cache and super generation don't match, space cache "
3373 "will be invalidated\n");
3378 cache = btrfs_lookup_first_block_group(root->fs_info, start);
3382 start = cache->key.objectid + cache->key.offset;
3383 if (!cache->free_space_ctl) {
3384 if (btrfs_init_free_space_ctl(cache,
3385 root->sectorsize)) {
3390 btrfs_remove_free_space_cache(cache);
3393 ret = load_free_space_cache(root->fs_info, cache);
3397 ret = verify_space_cache(root, cache);
3399 fprintf(stderr, "cache appears valid but isnt %Lu\n",
3400 cache->key.objectid);
3405 return error ? -EINVAL : 0;
3408 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
3411 struct btrfs_path *path;
3412 struct extent_buffer *leaf;
3413 struct btrfs_key key;
3416 path = btrfs_alloc_path();
3418 fprintf(stderr, "Error allocing path\n");
3422 key.objectid = bytenr;
3423 key.type = BTRFS_EXTENT_ITEM_KEY;
3428 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
3431 fprintf(stderr, "Error looking up extent record %d\n", ret);
3432 btrfs_free_path(path);
3438 btrfs_prev_leaf(root, path);
3441 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3444 * Block group items come before extent items if they have the same
3445 * bytenr, so walk back one more just in case. Dear future traveler,
3446 * first congrats on mastering time travel. Now if it's not too much
3447 * trouble could you go back to 2006 and tell Chris to make the
3448 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
3450 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3454 btrfs_prev_leaf(root, path);
3458 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3459 ret = btrfs_next_leaf(root, path);
3461 fprintf(stderr, "Error going to next leaf "
3463 btrfs_free_path(path);
3469 leaf = path->nodes[0];
3470 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3471 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3475 if (key.objectid + key.offset < bytenr) {
3479 if (key.objectid > bytenr + num_bytes)
3482 if (key.objectid == bytenr) {
3483 if (key.offset >= num_bytes) {
3487 num_bytes -= key.offset;
3488 bytenr += key.offset;
3489 } else if (key.objectid < bytenr) {
3490 if (key.objectid + key.offset >= bytenr + num_bytes) {
3494 num_bytes = (bytenr + num_bytes) -
3495 (key.objectid + key.offset);
3496 bytenr = key.objectid + key.offset;
3498 if (key.objectid + key.offset < bytenr + num_bytes) {
3499 u64 new_start = key.objectid + key.offset;
3500 u64 new_bytes = bytenr + num_bytes - new_start;
3503 * Weird case, the extent is in the middle of
3504 * our range, we'll have to search one side
3505 * and then the other. Not sure if this happens
3506 * in real life, but no harm in coding it up
3507 * anyway just in case.
3509 btrfs_release_path(path);
3510 ret = check_extent_exists(root, new_start,
3513 fprintf(stderr, "Right section didn't "
3517 num_bytes = key.objectid - bytenr;
3520 num_bytes = key.objectid - bytenr;
3527 fprintf(stderr, "There are no extents for csum range "
3528 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3532 btrfs_free_path(path);
3536 static int check_csums(struct btrfs_root *root)
3538 struct btrfs_path *path;
3539 struct extent_buffer *leaf;
3540 struct btrfs_key key;
3541 u64 offset = 0, num_bytes = 0;
3542 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3546 root = root->fs_info->csum_root;
3548 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3549 key.type = BTRFS_EXTENT_CSUM_KEY;
3552 path = btrfs_alloc_path();
3556 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3558 fprintf(stderr, "Error searching csum tree %d\n", ret);
3559 btrfs_free_path(path);
3563 if (ret > 0 && path->slots[0])
3568 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3569 ret = btrfs_next_leaf(root, path);
3571 fprintf(stderr, "Error going to next leaf "
3578 leaf = path->nodes[0];
3580 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3581 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3587 offset = key.offset;
3588 } else if (key.offset != offset + num_bytes) {
3589 ret = check_extent_exists(root, offset, num_bytes);
3591 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3592 "there is no extent record\n",
3593 offset, offset+num_bytes);
3596 offset = key.offset;
3600 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3601 csum_size) * root->sectorsize;
3605 btrfs_free_path(path);
3609 static int is_dropped_key(struct btrfs_key *key,
3610 struct btrfs_key *drop_key) {
3611 if (key->objectid < drop_key->objectid)
3613 else if (key->objectid == drop_key->objectid) {
3614 if (key->type < drop_key->type)
3616 else if (key->type == drop_key->type) {
3617 if (key->offset < drop_key->offset)
3624 static int run_next_block(struct btrfs_root *root,
3625 struct block_info *bits,
3628 struct cache_tree *pending,
3629 struct cache_tree *seen,
3630 struct cache_tree *reada,
3631 struct cache_tree *nodes,
3632 struct cache_tree *extent_cache,
3633 struct cache_tree *chunk_cache,
3634 struct rb_root *dev_cache,
3635 struct block_group_tree *block_group_cache,
3636 struct device_extent_tree *dev_extent_cache,
3637 struct btrfs_root_item *ri)
3639 struct extent_buffer *buf;
3649 struct btrfs_key key;
3650 struct cache_extent *cache;
3653 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
3654 bits_nr, &reada_bits);
3659 for(i = 0; i < nritems; i++) {
3660 ret = add_cache_extent(reada, bits[i].start,
3665 /* fixme, get the parent transid */
3666 readahead_tree_block(root, bits[i].start,
3670 *last = bits[0].start;
3671 bytenr = bits[0].start;
3672 size = bits[0].size;
3674 cache = lookup_cache_extent(pending, bytenr, size);
3676 remove_cache_extent(pending, cache);
3679 cache = lookup_cache_extent(reada, bytenr, size);
3681 remove_cache_extent(reada, cache);
3684 cache = lookup_cache_extent(nodes, bytenr, size);
3686 remove_cache_extent(nodes, cache);
3689 cache = lookup_cache_extent(seen, bytenr, size);
3691 remove_cache_extent(seen, cache);
3695 /* fixme, get the real parent transid */
3696 buf = read_tree_block(root, bytenr, size, 0);
3697 if (!extent_buffer_uptodate(buf)) {
3698 record_bad_block_io(root->fs_info,
3699 extent_cache, bytenr, size);
3703 nritems = btrfs_header_nritems(buf);
3705 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3706 btrfs_header_level(buf), 1, NULL,
3709 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3711 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3716 owner = btrfs_header_owner(buf);
3719 ret = check_block(root, extent_cache, buf, flags);
3723 if (btrfs_is_leaf(buf)) {
3724 btree_space_waste += btrfs_leaf_free_space(root, buf);
3725 for (i = 0; i < nritems; i++) {
3726 struct btrfs_file_extent_item *fi;
3727 btrfs_item_key_to_cpu(buf, &key, i);
3728 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3729 process_extent_item(root, extent_cache, buf,
3733 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3734 process_extent_item(root, extent_cache, buf,
3738 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3740 btrfs_item_size_nr(buf, i);
3743 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3744 process_chunk_item(chunk_cache, &key, buf, i);
3747 if (key.type == BTRFS_DEV_ITEM_KEY) {
3748 process_device_item(dev_cache, &key, buf, i);
3751 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3752 process_block_group_item(block_group_cache,
3756 if (key.type == BTRFS_DEV_EXTENT_KEY) {
3757 process_device_extent_item(dev_extent_cache,
3762 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3763 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3764 process_extent_ref_v0(extent_cache, buf, i);
3771 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3772 add_tree_backref(extent_cache, key.objectid, 0,
3776 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3777 add_tree_backref(extent_cache, key.objectid,
3781 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3782 struct btrfs_extent_data_ref *ref;
3783 ref = btrfs_item_ptr(buf, i,
3784 struct btrfs_extent_data_ref);
3785 add_data_backref(extent_cache,
3787 btrfs_extent_data_ref_root(buf, ref),
3788 btrfs_extent_data_ref_objectid(buf,
3790 btrfs_extent_data_ref_offset(buf, ref),
3791 btrfs_extent_data_ref_count(buf, ref),
3792 0, root->sectorsize);
3795 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3796 struct btrfs_shared_data_ref *ref;
3797 ref = btrfs_item_ptr(buf, i,
3798 struct btrfs_shared_data_ref);
3799 add_data_backref(extent_cache,
3800 key.objectid, key.offset, 0, 0, 0,
3801 btrfs_shared_data_ref_count(buf, ref),
3802 0, root->sectorsize);
3805 if (key.type != BTRFS_EXTENT_DATA_KEY)
3807 fi = btrfs_item_ptr(buf, i,
3808 struct btrfs_file_extent_item);
3809 if (btrfs_file_extent_type(buf, fi) ==
3810 BTRFS_FILE_EXTENT_INLINE)
3812 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3815 data_bytes_allocated +=
3816 btrfs_file_extent_disk_num_bytes(buf, fi);
3817 if (data_bytes_allocated < root->sectorsize) {
3820 data_bytes_referenced +=
3821 btrfs_file_extent_num_bytes(buf, fi);
3822 add_data_backref(extent_cache,
3823 btrfs_file_extent_disk_bytenr(buf, fi),
3824 parent, owner, key.objectid, key.offset -
3825 btrfs_file_extent_offset(buf, fi), 1, 1,
3826 btrfs_file_extent_disk_num_bytes(buf, fi));
3831 struct btrfs_key first_key;
3833 first_key.objectid = 0;
3836 btrfs_item_key_to_cpu(buf, &first_key, 0);
3837 level = btrfs_header_level(buf);
3838 for (i = 0; i < nritems; i++) {
3839 ptr = btrfs_node_blockptr(buf, i);
3840 size = btrfs_level_size(root, level - 1);
3841 btrfs_node_key_to_cpu(buf, &key, i);
3843 struct btrfs_key drop_key;
3844 btrfs_disk_key_to_cpu(&drop_key,
3845 &ri->drop_progress);
3846 if ((level == ri->drop_level)
3847 && is_dropped_key(&key, &drop_key)) {
3851 ret = add_extent_rec(extent_cache, &key,
3852 ptr, size, 0, 0, 1, 0, 1, 0,
3856 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3859 add_pending(nodes, seen, ptr, size);
3861 add_pending(pending, seen, ptr, size);
3864 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3865 nritems) * sizeof(struct btrfs_key_ptr);
3867 total_btree_bytes += buf->len;
3868 if (fs_root_objectid(btrfs_header_owner(buf)))
3869 total_fs_tree_bytes += buf->len;
3870 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3871 total_extent_tree_bytes += buf->len;
3872 if (!found_old_backref &&
3873 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3874 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3875 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3876 found_old_backref = 1;
3878 free_extent_buffer(buf);
3882 static int add_root_to_pending(struct extent_buffer *buf,
3883 struct cache_tree *extent_cache,
3884 struct cache_tree *pending,
3885 struct cache_tree *seen,
3886 struct cache_tree *nodes,
3887 struct btrfs_key *root_key)
3889 if (btrfs_header_level(buf) > 0)
3890 add_pending(nodes, seen, buf->start, buf->len);
3892 add_pending(pending, seen, buf->start, buf->len);
3893 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3894 0, 1, 1, 0, 1, 0, buf->len);
3896 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3897 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3898 add_tree_backref(extent_cache, buf->start, buf->start,
3901 add_tree_backref(extent_cache, buf->start, 0,
3902 root_key->objectid, 1);
3906 /* as we fix the tree, we might be deleting blocks that
3907 * we're tracking for repair. This hook makes sure we
3908 * remove any backrefs for blocks as we are fixing them.
3910 static int free_extent_hook(struct btrfs_trans_handle *trans,
3911 struct btrfs_root *root,
3912 u64 bytenr, u64 num_bytes, u64 parent,
3913 u64 root_objectid, u64 owner, u64 offset,
3916 struct extent_record *rec;
3917 struct cache_extent *cache;
3919 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3921 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3922 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
3926 rec = container_of(cache, struct extent_record, cache);
3928 struct data_backref *back;
3929 back = find_data_backref(rec, parent, root_objectid, owner,
3930 offset, 1, bytenr, num_bytes);
3933 if (back->node.found_ref) {
3934 back->found_ref -= refs_to_drop;
3936 rec->refs -= refs_to_drop;
3938 if (back->node.found_extent_tree) {
3939 back->num_refs -= refs_to_drop;
3940 if (rec->extent_item_refs)
3941 rec->extent_item_refs -= refs_to_drop;
3943 if (back->found_ref == 0)
3944 back->node.found_ref = 0;
3945 if (back->num_refs == 0)
3946 back->node.found_extent_tree = 0;
3948 if (!back->node.found_extent_tree && back->node.found_ref) {
3949 list_del(&back->node.list);
3953 struct tree_backref *back;
3954 back = find_tree_backref(rec, parent, root_objectid);
3957 if (back->node.found_ref) {
3960 back->node.found_ref = 0;
3962 if (back->node.found_extent_tree) {
3963 if (rec->extent_item_refs)
3964 rec->extent_item_refs--;
3965 back->node.found_extent_tree = 0;
3967 if (!back->node.found_extent_tree && back->node.found_ref) {
3968 list_del(&back->node.list);
3972 maybe_free_extent_rec(extent_cache, rec);
3977 static int delete_extent_records(struct btrfs_trans_handle *trans,
3978 struct btrfs_root *root,
3979 struct btrfs_path *path,
3980 u64 bytenr, u64 new_len)
3982 struct btrfs_key key;
3983 struct btrfs_key found_key;
3984 struct extent_buffer *leaf;
3989 key.objectid = bytenr;
3991 key.offset = (u64)-1;
3994 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
4001 if (path->slots[0] == 0)
4007 leaf = path->nodes[0];
4008 slot = path->slots[0];
4010 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4011 if (found_key.objectid != bytenr)
4014 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
4015 found_key.type != BTRFS_METADATA_ITEM_KEY &&
4016 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
4017 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
4018 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
4019 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
4020 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
4021 btrfs_release_path(path);
4022 if (found_key.type == 0) {
4023 if (found_key.offset == 0)
4025 key.offset = found_key.offset - 1;
4026 key.type = found_key.type;
4028 key.type = found_key.type - 1;
4029 key.offset = (u64)-1;
4033 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4034 found_key.objectid, found_key.type, found_key.offset);
4036 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4039 btrfs_release_path(path);
4041 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4042 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4043 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4044 found_key.offset : root->leafsize;
4046 ret = btrfs_update_block_group(trans, root, bytenr,
4053 btrfs_release_path(path);
4058 * for a single backref, this will allocate a new extent
4059 * and add the backref to it.
4061 static int record_extent(struct btrfs_trans_handle *trans,
4062 struct btrfs_fs_info *info,
4063 struct btrfs_path *path,
4064 struct extent_record *rec,
4065 struct extent_backref *back,
4066 int allocated, u64 flags)
4069 struct btrfs_root *extent_root = info->extent_root;
4070 struct extent_buffer *leaf;
4071 struct btrfs_key ins_key;
4072 struct btrfs_extent_item *ei;
4073 struct tree_backref *tback;
4074 struct data_backref *dback;
4075 struct btrfs_tree_block_info *bi;
4078 rec->max_size = max_t(u64, rec->max_size,
4079 info->extent_root->leafsize);
4082 u32 item_size = sizeof(*ei);
4085 item_size += sizeof(*bi);
4087 ins_key.objectid = rec->start;
4088 ins_key.offset = rec->max_size;
4089 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4091 ret = btrfs_insert_empty_item(trans, extent_root, path,
4092 &ins_key, item_size);
4096 leaf = path->nodes[0];
4097 ei = btrfs_item_ptr(leaf, path->slots[0],
4098 struct btrfs_extent_item);
4100 btrfs_set_extent_refs(leaf, ei, 0);
4101 btrfs_set_extent_generation(leaf, ei, rec->generation);
4103 if (back->is_data) {
4104 btrfs_set_extent_flags(leaf, ei,
4105 BTRFS_EXTENT_FLAG_DATA);
4107 struct btrfs_disk_key copy_key;;
4109 tback = (struct tree_backref *)back;
4110 bi = (struct btrfs_tree_block_info *)(ei + 1);
4111 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4114 btrfs_set_disk_key_objectid(©_key,
4115 rec->info_objectid);
4116 btrfs_set_disk_key_type(©_key, 0);
4117 btrfs_set_disk_key_offset(©_key, 0);
4119 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
4120 btrfs_set_tree_block_key(leaf, bi, ©_key);
4122 btrfs_set_extent_flags(leaf, ei,
4123 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
4126 btrfs_mark_buffer_dirty(leaf);
4127 ret = btrfs_update_block_group(trans, extent_root, rec->start,
4128 rec->max_size, 1, 0);
4131 btrfs_release_path(path);
4134 if (back->is_data) {
4138 dback = (struct data_backref *)back;
4139 if (back->full_backref)
4140 parent = dback->parent;
4144 for (i = 0; i < dback->found_ref; i++) {
4145 /* if parent != 0, we're doing a full backref
4146 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
4147 * just makes the backref allocator create a data
4150 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4151 rec->start, rec->max_size,
4155 BTRFS_FIRST_FREE_OBJECTID :
4161 fprintf(stderr, "adding new data backref"
4162 " on %llu %s %llu owner %llu"
4163 " offset %llu found %d\n",
4164 (unsigned long long)rec->start,
4165 back->full_backref ?
4167 back->full_backref ?
4168 (unsigned long long)parent :
4169 (unsigned long long)dback->root,
4170 (unsigned long long)dback->owner,
4171 (unsigned long long)dback->offset,
4176 tback = (struct tree_backref *)back;
4177 if (back->full_backref)
4178 parent = tback->parent;
4182 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4183 rec->start, rec->max_size,
4184 parent, tback->root, 0, 0);
4185 fprintf(stderr, "adding new tree backref on "
4186 "start %llu len %llu parent %llu root %llu\n",
4187 rec->start, rec->max_size, tback->parent, tback->root);
4192 btrfs_release_path(path);
4196 struct extent_entry {
4201 struct list_head list;
4204 static struct extent_entry *find_entry(struct list_head *entries,
4205 u64 bytenr, u64 bytes)
4207 struct extent_entry *entry = NULL;
4209 list_for_each_entry(entry, entries, list) {
4210 if (entry->bytenr == bytenr && entry->bytes == bytes)
4217 static struct extent_entry *find_most_right_entry(struct list_head *entries)
4219 struct extent_entry *entry, *best = NULL, *prev = NULL;
4221 list_for_each_entry(entry, entries, list) {
4228 * If there are as many broken entries as entries then we know
4229 * not to trust this particular entry.
4231 if (entry->broken == entry->count)
4235 * If our current entry == best then we can't be sure our best
4236 * is really the best, so we need to keep searching.
4238 if (best && best->count == entry->count) {
4244 /* Prev == entry, not good enough, have to keep searching */
4245 if (!prev->broken && prev->count == entry->count)
4249 best = (prev->count > entry->count) ? prev : entry;
4250 else if (best->count < entry->count)
4258 static int repair_ref(struct btrfs_trans_handle *trans,
4259 struct btrfs_fs_info *info, struct btrfs_path *path,
4260 struct data_backref *dback, struct extent_entry *entry)
4262 struct btrfs_root *root;
4263 struct btrfs_file_extent_item *fi;
4264 struct extent_buffer *leaf;
4265 struct btrfs_key key;
4269 key.objectid = dback->root;
4270 key.type = BTRFS_ROOT_ITEM_KEY;
4271 key.offset = (u64)-1;
4272 root = btrfs_read_fs_root(info, &key);
4274 fprintf(stderr, "Couldn't find root for our ref\n");
4279 * The backref points to the original offset of the extent if it was
4280 * split, so we need to search down to the offset we have and then walk
4281 * forward until we find the backref we're looking for.
4283 key.objectid = dback->owner;
4284 key.type = BTRFS_EXTENT_DATA_KEY;
4285 key.offset = dback->offset;
4286 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4288 fprintf(stderr, "Error looking up ref %d\n", ret);
4293 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4294 ret = btrfs_next_leaf(root, path);
4296 fprintf(stderr, "Couldn't find our ref, next\n");
4300 leaf = path->nodes[0];
4301 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4302 if (key.objectid != dback->owner ||
4303 key.type != BTRFS_EXTENT_DATA_KEY) {
4304 fprintf(stderr, "Couldn't find our ref, search\n");
4307 fi = btrfs_item_ptr(leaf, path->slots[0],
4308 struct btrfs_file_extent_item);
4309 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4310 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4312 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
4317 btrfs_release_path(path);
4320 * Have to make sure that this root gets updated when we commit the
4323 root->track_dirty = 1;
4324 if (root->last_trans != trans->transid) {
4325 root->last_trans = trans->transid;
4326 root->commit_root = root->node;
4327 extent_buffer_get(root->node);
4331 * Ok we have the key of the file extent we want to fix, now we can cow
4332 * down to the thing and fix it.
4334 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4336 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
4337 key.objectid, key.type, key.offset, ret);
4341 fprintf(stderr, "Well that's odd, we just found this key "
4342 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
4346 leaf = path->nodes[0];
4347 fi = btrfs_item_ptr(leaf, path->slots[0],
4348 struct btrfs_file_extent_item);
4350 if (btrfs_file_extent_compression(leaf, fi) &&
4351 dback->disk_bytenr != entry->bytenr) {
4352 fprintf(stderr, "Ref doesn't match the record start and is "
4353 "compressed, please take a btrfs-image of this file "
4354 "system and send it to a btrfs developer so they can "
4355 "complete this functionality for bytenr %Lu\n",
4356 dback->disk_bytenr);
4360 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
4361 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4362 } else if (dback->disk_bytenr > entry->bytenr) {
4363 u64 off_diff, offset;
4365 off_diff = dback->disk_bytenr - entry->bytenr;
4366 offset = btrfs_file_extent_offset(leaf, fi);
4367 if (dback->disk_bytenr + offset +
4368 btrfs_file_extent_num_bytes(leaf, fi) >
4369 entry->bytenr + entry->bytes) {
4370 fprintf(stderr, "Ref is past the entry end, please "
4371 "take a btrfs-image of this file system and "
4372 "send it to a btrfs developer, ref %Lu\n",
4373 dback->disk_bytenr);
4377 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4378 btrfs_set_file_extent_offset(leaf, fi, offset);
4379 } else if (dback->disk_bytenr < entry->bytenr) {
4382 offset = btrfs_file_extent_offset(leaf, fi);
4383 if (dback->disk_bytenr + offset < entry->bytenr) {
4384 fprintf(stderr, "Ref is before the entry start, please"
4385 " take a btrfs-image of this file system and "
4386 "send it to a btrfs developer, ref %Lu\n",
4387 dback->disk_bytenr);
4391 offset += dback->disk_bytenr;
4392 offset -= entry->bytenr;
4393 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4394 btrfs_set_file_extent_offset(leaf, fi, offset);
4397 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
4400 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
4401 * only do this if we aren't using compression, otherwise it's a
4404 if (!btrfs_file_extent_compression(leaf, fi))
4405 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
4407 printf("ram bytes may be wrong?\n");
4408 btrfs_mark_buffer_dirty(leaf);
4409 btrfs_release_path(path);
4413 static int verify_backrefs(struct btrfs_trans_handle *trans,
4414 struct btrfs_fs_info *info, struct btrfs_path *path,
4415 struct extent_record *rec)
4417 struct extent_backref *back;
4418 struct data_backref *dback;
4419 struct extent_entry *entry, *best = NULL;
4422 int broken_entries = 0;
4427 * Metadata is easy and the backrefs should always agree on bytenr and
4428 * size, if not we've got bigger issues.
4433 list_for_each_entry(back, &rec->backrefs, list) {
4434 dback = (struct data_backref *)back;
4436 * We only pay attention to backrefs that we found a real
4439 if (dback->found_ref == 0)
4441 if (back->full_backref)
4445 * For now we only catch when the bytes don't match, not the
4446 * bytenr. We can easily do this at the same time, but I want
4447 * to have a fs image to test on before we just add repair
4448 * functionality willy-nilly so we know we won't screw up the
4452 entry = find_entry(&entries, dback->disk_bytenr,
4455 entry = malloc(sizeof(struct extent_entry));
4460 memset(entry, 0, sizeof(*entry));
4461 entry->bytenr = dback->disk_bytenr;
4462 entry->bytes = dback->bytes;
4463 list_add_tail(&entry->list, &entries);
4468 * If we only have on entry we may think the entries agree when
4469 * in reality they don't so we have to do some extra checking.
4471 if (dback->disk_bytenr != rec->start ||
4472 dback->bytes != rec->nr || back->broken)
4483 /* Yay all the backrefs agree, carry on good sir */
4484 if (nr_entries <= 1 && !mismatch)
4487 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
4488 "%Lu\n", rec->start);
4491 * First we want to see if the backrefs can agree amongst themselves who
4492 * is right, so figure out which one of the entries has the highest
4495 best = find_most_right_entry(&entries);
4498 * Ok so we may have an even split between what the backrefs think, so
4499 * this is where we use the extent ref to see what it thinks.
4502 entry = find_entry(&entries, rec->start, rec->nr);
4503 if (!entry && (!broken_entries || !rec->found_rec)) {
4504 fprintf(stderr, "Backrefs don't agree with eachother "
4505 "and extent record doesn't agree with anybody,"
4506 " so we can't fix bytenr %Lu bytes %Lu\n",
4507 rec->start, rec->nr);
4510 } else if (!entry) {
4512 * Ok our backrefs were broken, we'll assume this is the
4513 * correct value and add an entry for this range.
4515 entry = malloc(sizeof(struct extent_entry));
4520 memset(entry, 0, sizeof(*entry));
4521 entry->bytenr = rec->start;
4522 entry->bytes = rec->nr;
4523 list_add_tail(&entry->list, &entries);
4527 best = find_most_right_entry(&entries);
4529 fprintf(stderr, "Backrefs and extent record evenly "
4530 "split on who is right, this is going to "
4531 "require user input to fix bytenr %Lu bytes "
4532 "%Lu\n", rec->start, rec->nr);
4539 * I don't think this can happen currently as we'll abort() if we catch
4540 * this case higher up, but in case somebody removes that we still can't
4541 * deal with it properly here yet, so just bail out of that's the case.
4543 if (best->bytenr != rec->start) {
4544 fprintf(stderr, "Extent start and backref starts don't match, "
4545 "please use btrfs-image on this file system and send "
4546 "it to a btrfs developer so they can make fsck fix "
4547 "this particular case. bytenr is %Lu, bytes is %Lu\n",
4548 rec->start, rec->nr);
4554 * Ok great we all agreed on an extent record, let's go find the real
4555 * references and fix up the ones that don't match.
4557 list_for_each_entry(back, &rec->backrefs, list) {
4558 dback = (struct data_backref *)back;
4561 * Still ignoring backrefs that don't have a real ref attached
4564 if (dback->found_ref == 0)
4566 if (back->full_backref)
4569 if (dback->bytes == best->bytes &&
4570 dback->disk_bytenr == best->bytenr)
4573 ret = repair_ref(trans, info, path, dback, best);
4579 * Ok we messed with the actual refs, which means we need to drop our
4580 * entire cache and go back and rescan. I know this is a huge pain and
4581 * adds a lot of extra work, but it's the only way to be safe. Once all
4582 * the backrefs agree we may not need to do anything to the extent
4587 while (!list_empty(&entries)) {
4588 entry = list_entry(entries.next, struct extent_entry, list);
4589 list_del_init(&entry->list);
4595 static int process_duplicates(struct btrfs_root *root,
4596 struct cache_tree *extent_cache,
4597 struct extent_record *rec)
4599 struct extent_record *good, *tmp;
4600 struct cache_extent *cache;
4604 * If we found a extent record for this extent then return, or if we
4605 * have more than one duplicate we are likely going to need to delete
4608 if (rec->found_rec || rec->num_duplicates > 1)
4611 /* Shouldn't happen but just in case */
4612 BUG_ON(!rec->num_duplicates);
4615 * So this happens if we end up with a backref that doesn't match the
4616 * actual extent entry. So either the backref is bad or the extent
4617 * entry is bad. Either way we want to have the extent_record actually
4618 * reflect what we found in the extent_tree, so we need to take the
4619 * duplicate out and use that as the extent_record since the only way we
4620 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
4622 remove_cache_extent(extent_cache, &rec->cache);
4624 good = list_entry(rec->dups.next, struct extent_record, list);
4625 list_del_init(&good->list);
4626 INIT_LIST_HEAD(&good->backrefs);
4627 INIT_LIST_HEAD(&good->dups);
4628 good->cache.start = good->start;
4629 good->cache.size = good->nr;
4630 good->content_checked = 0;
4631 good->owner_ref_checked = 0;
4632 good->num_duplicates = 0;
4633 good->refs = rec->refs;
4634 list_splice_init(&rec->backrefs, &good->backrefs);
4636 cache = lookup_cache_extent(extent_cache, good->start,
4640 tmp = container_of(cache, struct extent_record, cache);
4643 * If we find another overlapping extent and it's found_rec is
4644 * set then it's a duplicate and we need to try and delete
4647 if (tmp->found_rec || tmp->num_duplicates > 0) {
4648 if (list_empty(&good->list))
4649 list_add_tail(&good->list,
4650 &duplicate_extents);
4651 good->num_duplicates += tmp->num_duplicates + 1;
4652 list_splice_init(&tmp->dups, &good->dups);
4653 list_del_init(&tmp->list);
4654 list_add_tail(&tmp->list, &good->dups);
4655 remove_cache_extent(extent_cache, &tmp->cache);
4660 * Ok we have another non extent item backed extent rec, so lets
4661 * just add it to this extent and carry on like we did above.
4663 good->refs += tmp->refs;
4664 list_splice_init(&tmp->backrefs, &good->backrefs);
4665 remove_cache_extent(extent_cache, &tmp->cache);
4668 ret = insert_cache_extent(extent_cache, &good->cache);
4671 return good->num_duplicates ? 0 : 1;
4674 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
4675 struct btrfs_root *root,
4676 struct extent_record *rec)
4678 LIST_HEAD(delete_list);
4679 struct btrfs_path *path;
4680 struct extent_record *tmp, *good, *n;
4683 struct btrfs_key key;
4685 path = btrfs_alloc_path();
4692 /* Find the record that covers all of the duplicates. */
4693 list_for_each_entry(tmp, &rec->dups, list) {
4694 if (good->start < tmp->start)
4696 if (good->nr > tmp->nr)
4699 if (tmp->start + tmp->nr < good->start + good->nr) {
4700 fprintf(stderr, "Ok we have overlapping extents that "
4701 "aren't completely covered by eachother, this "
4702 "is going to require more careful thought. "
4703 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
4704 tmp->start, tmp->nr, good->start, good->nr);
4711 list_add_tail(&rec->list, &delete_list);
4713 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
4716 list_move_tail(&tmp->list, &delete_list);
4719 root = root->fs_info->extent_root;
4720 list_for_each_entry(tmp, &delete_list, list) {
4721 if (tmp->found_rec == 0)
4723 key.objectid = tmp->start;
4724 key.type = BTRFS_EXTENT_ITEM_KEY;
4725 key.offset = tmp->nr;
4727 /* Shouldn't happen but just in case */
4728 if (tmp->metadata) {
4729 fprintf(stderr, "Well this shouldn't happen, extent "
4730 "record overlaps but is metadata? "
4731 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
4735 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4741 ret = btrfs_del_item(trans, root, path);
4744 btrfs_release_path(path);
4749 while (!list_empty(&delete_list)) {
4750 tmp = list_entry(delete_list.next, struct extent_record, list);
4751 list_del_init(&tmp->list);
4757 while (!list_empty(&rec->dups)) {
4758 tmp = list_entry(rec->dups.next, struct extent_record, list);
4759 list_del_init(&tmp->list);
4763 btrfs_free_path(path);
4765 if (!ret && !nr_del)
4766 rec->num_duplicates = 0;
4768 return ret ? ret : nr_del;
4771 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
4772 struct btrfs_fs_info *info,
4773 struct btrfs_path *path,
4774 struct cache_tree *extent_cache,
4775 struct extent_record *rec)
4777 struct btrfs_root *root;
4778 struct extent_backref *back;
4779 struct data_backref *dback;
4780 struct cache_extent *cache;
4781 struct btrfs_file_extent_item *fi;
4782 struct btrfs_key key;
4786 list_for_each_entry(back, &rec->backrefs, list) {
4787 dback = (struct data_backref *)back;
4789 /* We found this one, we don't need to do a lookup */
4790 if (dback->found_ref)
4792 /* Don't care about full backrefs (poor unloved backrefs) */
4793 if (back->full_backref)
4795 key.objectid = dback->root;
4796 key.type = BTRFS_ROOT_ITEM_KEY;
4797 key.offset = (u64)-1;
4799 root = btrfs_read_fs_root(info, &key);
4801 /* No root, definitely a bad ref, skip */
4802 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
4804 /* Other err, exit */
4806 return PTR_ERR(root);
4808 key.objectid = dback->owner;
4809 key.type = BTRFS_EXTENT_DATA_KEY;
4810 key.offset = dback->offset;
4811 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4813 btrfs_release_path(path);
4816 /* Didn't find it, we can carry on */
4821 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4822 struct btrfs_file_extent_item);
4823 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
4824 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
4825 btrfs_release_path(path);
4826 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4828 struct extent_record *tmp;
4829 tmp = container_of(cache, struct extent_record, cache);
4832 * If we found an extent record for the bytenr for this
4833 * particular backref then we can't add it to our
4834 * current extent record. We only want to add backrefs
4835 * that don't have a corresponding extent item in the
4836 * extent tree since they likely belong to this record
4837 * and we need to fix it if it doesn't match bytenrs.
4843 dback->found_ref += 1;
4844 dback->disk_bytenr = bytenr;
4845 dback->bytes = bytes;
4848 * Set this so the verify backref code knows not to trust the
4849 * values in this backref.
4858 * when an incorrect extent item is found, this will delete
4859 * all of the existing entries for it and recreate them
4860 * based on what the tree scan found.
4862 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
4863 struct btrfs_fs_info *info,
4864 struct cache_tree *extent_cache,
4865 struct extent_record *rec)
4868 struct btrfs_path *path;
4869 struct list_head *cur = rec->backrefs.next;
4870 struct cache_extent *cache;
4871 struct extent_backref *back;
4875 /* remember our flags for recreating the extent */
4876 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
4877 rec->max_size, rec->metadata, NULL,
4880 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4882 path = btrfs_alloc_path();
4886 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
4888 * Sometimes the backrefs themselves are so broken they don't
4889 * get attached to any meaningful rec, so first go back and
4890 * check any of our backrefs that we couldn't find and throw
4891 * them into the list if we find the backref so that
4892 * verify_backrefs can figure out what to do.
4894 ret = find_possible_backrefs(trans, info, path, extent_cache,
4900 /* step one, make sure all of the backrefs agree */
4901 ret = verify_backrefs(trans, info, path, rec);
4905 /* step two, delete all the existing records */
4906 ret = delete_extent_records(trans, info->extent_root, path,
4907 rec->start, rec->max_size);
4912 /* was this block corrupt? If so, don't add references to it */
4913 cache = lookup_cache_extent(info->corrupt_blocks,
4914 rec->start, rec->max_size);
4920 /* step three, recreate all the refs we did find */
4921 while(cur != &rec->backrefs) {
4922 back = list_entry(cur, struct extent_backref, list);
4926 * if we didn't find any references, don't create a
4929 if (!back->found_ref)
4932 ret = record_extent(trans, info, path, rec, back, allocated, flags);
4939 btrfs_free_path(path);
4943 /* right now we only prune from the extent allocation tree */
4944 static int prune_one_block(struct btrfs_trans_handle *trans,
4945 struct btrfs_fs_info *info,
4946 struct btrfs_corrupt_block *corrupt)
4949 struct btrfs_path path;
4950 struct extent_buffer *eb;
4954 int level = corrupt->level + 1;
4956 btrfs_init_path(&path);
4958 /* we want to stop at the parent to our busted block */
4959 path.lowest_level = level;
4961 ret = btrfs_search_slot(trans, info->extent_root,
4962 &corrupt->key, &path, -1, 1);
4967 eb = path.nodes[level];
4974 * hopefully the search gave us the block we want to prune,
4975 * lets try that first
4977 slot = path.slots[level];
4978 found = btrfs_node_blockptr(eb, slot);
4979 if (found == corrupt->cache.start)
4982 nritems = btrfs_header_nritems(eb);
4984 /* the search failed, lets scan this node and hope we find it */
4985 for (slot = 0; slot < nritems; slot++) {
4986 found = btrfs_node_blockptr(eb, slot);
4987 if (found == corrupt->cache.start)
4991 * we couldn't find the bad block. TODO, search all the nodes for pointers
4994 if (eb == info->extent_root->node) {
4999 btrfs_release_path(&path);
5004 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
5005 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
5008 btrfs_release_path(&path);
5012 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
5013 struct btrfs_fs_info *info)
5015 struct cache_extent *cache;
5016 struct btrfs_corrupt_block *corrupt;
5018 cache = search_cache_extent(info->corrupt_blocks, 0);
5022 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5023 prune_one_block(trans, info, corrupt);
5024 cache = next_cache_extent(cache);
5029 static void free_corrupt_block(struct cache_extent *cache)
5031 struct btrfs_corrupt_block *corrupt;
5033 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5037 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5039 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5041 struct btrfs_block_group_cache *cache;
5046 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5047 &start, &end, EXTENT_DIRTY);
5050 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5056 cache = btrfs_lookup_first_block_group(fs_info, start);
5061 start = cache->key.objectid + cache->key.offset;
5065 static int check_extent_refs(struct btrfs_trans_handle *trans,
5066 struct btrfs_root *root,
5067 struct cache_tree *extent_cache)
5069 struct extent_record *rec;
5070 struct cache_extent *cache;
5078 * if we're doing a repair, we have to make sure
5079 * we don't allocate from the problem extents.
5080 * In the worst case, this will be all the
5083 cache = search_cache_extent(extent_cache, 0);
5085 rec = container_of(cache, struct extent_record, cache);
5086 btrfs_pin_extent(root->fs_info,
5087 rec->start, rec->max_size);
5088 cache = next_cache_extent(cache);
5091 /* pin down all the corrupted blocks too */
5092 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5094 btrfs_pin_extent(root->fs_info,
5095 cache->start, cache->size);
5096 cache = next_cache_extent(cache);
5098 prune_corrupt_blocks(trans, root->fs_info);
5099 reset_cached_block_groups(root->fs_info);
5103 * We need to delete any duplicate entries we find first otherwise we
5104 * could mess up the extent tree when we have backrefs that actually
5105 * belong to a different extent item and not the weird duplicate one.
5107 while (repair && !list_empty(&duplicate_extents)) {
5108 rec = list_entry(duplicate_extents.next, struct extent_record,
5110 list_del_init(&rec->list);
5112 /* Sometimes we can find a backref before we find an actual
5113 * extent, so we need to process it a little bit to see if there
5114 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
5115 * if this is a backref screwup. If we need to delete stuff
5116 * process_duplicates() will return 0, otherwise it will return
5119 if (process_duplicates(root, extent_cache, rec))
5121 ret = delete_duplicate_records(trans, root, rec);
5125 * delete_duplicate_records will return the number of entries
5126 * deleted, so if it's greater than 0 then we know we actually
5127 * did something and we need to remove.
5138 cache = search_cache_extent(extent_cache, 0);
5141 rec = container_of(cache, struct extent_record, cache);
5142 if (rec->num_duplicates) {
5143 fprintf(stderr, "extent item %llu has multiple extent "
5144 "items\n", (unsigned long long)rec->start);
5148 if (rec->refs != rec->extent_item_refs) {
5149 fprintf(stderr, "ref mismatch on [%llu %llu] ",
5150 (unsigned long long)rec->start,
5151 (unsigned long long)rec->nr);
5152 fprintf(stderr, "extent item %llu, found %llu\n",
5153 (unsigned long long)rec->extent_item_refs,
5154 (unsigned long long)rec->refs);
5155 if (!fixed && repair) {
5156 ret = fixup_extent_refs(trans, root->fs_info,
5165 if (all_backpointers_checked(rec, 1)) {
5166 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
5167 (unsigned long long)rec->start,
5168 (unsigned long long)rec->nr);
5170 if (!fixed && repair) {
5171 ret = fixup_extent_refs(trans, root->fs_info,
5180 if (!rec->owner_ref_checked) {
5181 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
5182 (unsigned long long)rec->start,
5183 (unsigned long long)rec->nr);
5184 if (!fixed && repair) {
5185 ret = fixup_extent_refs(trans, root->fs_info,
5194 remove_cache_extent(extent_cache, cache);
5195 free_all_extent_backrefs(rec);
5200 if (ret && ret != -EAGAIN) {
5201 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
5204 btrfs_fix_block_accounting(trans, root);
5207 fprintf(stderr, "repaired damaged extent references\n");
5213 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
5217 if (type & BTRFS_BLOCK_GROUP_RAID0) {
5218 stripe_size = length;
5219 stripe_size /= num_stripes;
5220 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
5221 stripe_size = length * 2;
5222 stripe_size /= num_stripes;
5223 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
5224 stripe_size = length;
5225 stripe_size /= (num_stripes - 1);
5226 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
5227 stripe_size = length;
5228 stripe_size /= (num_stripes - 2);
5230 stripe_size = length;
5235 static int check_chunk_refs(struct chunk_record *chunk_rec,
5236 struct block_group_tree *block_group_cache,
5237 struct device_extent_tree *dev_extent_cache,
5240 struct cache_extent *block_group_item;
5241 struct block_group_record *block_group_rec;
5242 struct cache_extent *dev_extent_item;
5243 struct device_extent_record *dev_extent_rec;
5250 block_group_item = lookup_cache_extent(&block_group_cache->tree,
5253 if (block_group_item) {
5254 block_group_rec = container_of(block_group_item,
5255 struct block_group_record,
5257 if (chunk_rec->length != block_group_rec->offset ||
5258 chunk_rec->offset != block_group_rec->objectid ||
5259 chunk_rec->type_flags != block_group_rec->flags) {
5262 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
5263 chunk_rec->objectid,
5268 chunk_rec->type_flags,
5269 block_group_rec->objectid,
5270 block_group_rec->type,
5271 block_group_rec->offset,
5272 block_group_rec->offset,
5273 block_group_rec->objectid,
5274 block_group_rec->flags);
5277 list_del_init(&block_group_rec->list);
5278 chunk_rec->bg_rec = block_group_rec;
5283 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
5284 chunk_rec->objectid,
5289 chunk_rec->type_flags);
5293 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
5294 chunk_rec->num_stripes);
5295 for (i = 0; i < chunk_rec->num_stripes; ++i) {
5296 devid = chunk_rec->stripes[i].devid;
5297 offset = chunk_rec->stripes[i].offset;
5298 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
5299 devid, offset, length);
5300 if (dev_extent_item) {
5301 dev_extent_rec = container_of(dev_extent_item,
5302 struct device_extent_record,
5304 if (dev_extent_rec->objectid != devid ||
5305 dev_extent_rec->offset != offset ||
5306 dev_extent_rec->chunk_offset != chunk_rec->offset ||
5307 dev_extent_rec->length != length) {
5310 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
5311 chunk_rec->objectid,
5314 chunk_rec->stripes[i].devid,
5315 chunk_rec->stripes[i].offset,
5316 dev_extent_rec->objectid,
5317 dev_extent_rec->offset,
5318 dev_extent_rec->length);
5321 list_move(&dev_extent_rec->chunk_list,
5322 &chunk_rec->dextents);
5327 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
5328 chunk_rec->objectid,
5331 chunk_rec->stripes[i].devid,
5332 chunk_rec->stripes[i].offset);
5339 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
5340 int check_chunks(struct cache_tree *chunk_cache,
5341 struct block_group_tree *block_group_cache,
5342 struct device_extent_tree *dev_extent_cache,
5343 struct list_head *good, struct list_head *bad, int silent)
5345 struct cache_extent *chunk_item;
5346 struct chunk_record *chunk_rec;
5347 struct block_group_record *bg_rec;
5348 struct device_extent_record *dext_rec;
5352 chunk_item = first_cache_extent(chunk_cache);
5353 while (chunk_item) {
5354 chunk_rec = container_of(chunk_item, struct chunk_record,
5356 err = check_chunk_refs(chunk_rec, block_group_cache,
5357 dev_extent_cache, silent);
5361 list_add_tail(&chunk_rec->list, bad);
5364 list_add_tail(&chunk_rec->list, good);
5367 chunk_item = next_cache_extent(chunk_item);
5370 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
5373 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
5381 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
5385 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
5396 static int check_device_used(struct device_record *dev_rec,
5397 struct device_extent_tree *dext_cache)
5399 struct cache_extent *cache;
5400 struct device_extent_record *dev_extent_rec;
5403 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
5405 dev_extent_rec = container_of(cache,
5406 struct device_extent_record,
5408 if (dev_extent_rec->objectid != dev_rec->devid)
5411 list_del(&dev_extent_rec->device_list);
5412 total_byte += dev_extent_rec->length;
5413 cache = next_cache_extent(cache);
5416 if (total_byte != dev_rec->byte_used) {
5418 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
5419 total_byte, dev_rec->byte_used, dev_rec->objectid,
5420 dev_rec->type, dev_rec->offset);
5427 /* check btrfs_dev_item -> btrfs_dev_extent */
5428 static int check_devices(struct rb_root *dev_cache,
5429 struct device_extent_tree *dev_extent_cache)
5431 struct rb_node *dev_node;
5432 struct device_record *dev_rec;
5433 struct device_extent_record *dext_rec;
5437 dev_node = rb_first(dev_cache);
5439 dev_rec = container_of(dev_node, struct device_record, node);
5440 err = check_device_used(dev_rec, dev_extent_cache);
5444 dev_node = rb_next(dev_node);
5446 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
5449 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
5450 dext_rec->objectid, dext_rec->offset, dext_rec->length);
5457 static int check_chunks_and_extents(struct btrfs_root *root)
5459 struct rb_root dev_cache;
5460 struct cache_tree chunk_cache;
5461 struct block_group_tree block_group_cache;
5462 struct device_extent_tree dev_extent_cache;
5463 struct cache_tree extent_cache;
5464 struct cache_tree seen;
5465 struct cache_tree pending;
5466 struct cache_tree reada;
5467 struct cache_tree nodes;
5468 struct cache_tree corrupt_blocks;
5469 struct btrfs_path path;
5470 struct btrfs_key key;
5471 struct btrfs_key found_key;
5474 struct block_info *bits;
5476 struct extent_buffer *leaf;
5477 struct btrfs_trans_handle *trans = NULL;
5479 struct btrfs_root_item ri;
5480 struct list_head dropping_trees;
5482 dev_cache = RB_ROOT;
5483 cache_tree_init(&chunk_cache);
5484 block_group_tree_init(&block_group_cache);
5485 device_extent_tree_init(&dev_extent_cache);
5487 cache_tree_init(&extent_cache);
5488 cache_tree_init(&seen);
5489 cache_tree_init(&pending);
5490 cache_tree_init(&nodes);
5491 cache_tree_init(&reada);
5492 cache_tree_init(&corrupt_blocks);
5493 INIT_LIST_HEAD(&dropping_trees);
5496 trans = btrfs_start_transaction(root, 1);
5497 if (IS_ERR(trans)) {
5498 fprintf(stderr, "Error starting transaction\n");
5499 return PTR_ERR(trans);
5501 root->fs_info->fsck_extent_cache = &extent_cache;
5502 root->fs_info->free_extent_hook = free_extent_hook;
5503 root->fs_info->corrupt_blocks = &corrupt_blocks;
5507 bits = malloc(bits_nr * sizeof(struct block_info));
5514 add_root_to_pending(root->fs_info->tree_root->node,
5515 &extent_cache, &pending, &seen, &nodes,
5516 &root->fs_info->tree_root->root_key);
5518 add_root_to_pending(root->fs_info->chunk_root->node,
5519 &extent_cache, &pending, &seen, &nodes,
5520 &root->fs_info->chunk_root->root_key);
5522 btrfs_init_path(&path);
5525 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
5526 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
5530 leaf = path.nodes[0];
5531 slot = path.slots[0];
5532 if (slot >= btrfs_header_nritems(path.nodes[0])) {
5533 ret = btrfs_next_leaf(root, &path);
5536 leaf = path.nodes[0];
5537 slot = path.slots[0];
5539 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
5540 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
5541 unsigned long offset;
5542 struct extent_buffer *buf;
5544 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5545 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5546 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
5547 buf = read_tree_block(root->fs_info->tree_root,
5548 btrfs_root_bytenr(&ri),
5549 btrfs_level_size(root,
5550 btrfs_root_level(&ri)),
5552 add_root_to_pending(buf, &extent_cache,
5553 &pending, &seen, &nodes,
5555 free_extent_buffer(buf);
5557 struct dropping_root_item_record *dri_rec;
5558 dri_rec = malloc(sizeof(*dri_rec));
5563 memcpy(&dri_rec->ri, &ri, sizeof(ri));
5564 memcpy(&dri_rec->found_key, &found_key,
5566 list_add_tail(&dri_rec->list, &dropping_trees);
5571 btrfs_release_path(&path);
5573 ret = run_next_block(root, bits, bits_nr, &last, &pending,
5574 &seen, &reada, &nodes, &extent_cache,
5575 &chunk_cache, &dev_cache,
5576 &block_group_cache, &dev_extent_cache,
5582 while (!list_empty(&dropping_trees)) {
5583 struct dropping_root_item_record *rec;
5584 struct extent_buffer *buf;
5585 rec = list_entry(dropping_trees.next,
5586 struct dropping_root_item_record, list);
5592 buf = read_tree_block(root->fs_info->tree_root,
5593 btrfs_root_bytenr(&rec->ri),
5594 btrfs_level_size(root,
5595 btrfs_root_level(&rec->ri)), 0);
5596 add_root_to_pending(buf, &extent_cache, &pending,
5597 &seen, &nodes, &rec->found_key);
5599 ret = run_next_block(root, bits, bits_nr, &last,
5600 &pending, &seen, &reada,
5601 &nodes, &extent_cache,
5602 &chunk_cache, &dev_cache,
5609 free_extent_buffer(buf);
5610 list_del(&rec->list);
5614 ret = check_extent_refs(trans, root, &extent_cache);
5615 if (ret == -EAGAIN) {
5616 ret = btrfs_commit_transaction(trans, root);
5620 trans = btrfs_start_transaction(root, 1);
5621 if (IS_ERR(trans)) {
5622 ret = PTR_ERR(trans);
5626 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5627 free_extent_cache_tree(&seen);
5628 free_extent_cache_tree(&pending);
5629 free_extent_cache_tree(&reada);
5630 free_extent_cache_tree(&nodes);
5631 free_extent_record_cache(root->fs_info, &extent_cache);
5635 err = check_chunks(&chunk_cache, &block_group_cache,
5636 &dev_extent_cache, NULL, NULL, 0);
5640 err = check_devices(&dev_cache, &dev_extent_cache);
5645 err = btrfs_commit_transaction(trans, root);
5651 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5652 root->fs_info->fsck_extent_cache = NULL;
5653 root->fs_info->free_extent_hook = NULL;
5654 root->fs_info->corrupt_blocks = NULL;
5657 free_chunk_cache_tree(&chunk_cache);
5658 free_device_cache_tree(&dev_cache);
5659 free_block_group_tree(&block_group_cache);
5660 free_device_extent_tree(&dev_extent_cache);
5664 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
5665 struct btrfs_root *root, int overwrite)
5667 struct extent_buffer *c;
5668 struct extent_buffer *old = root->node;
5670 struct btrfs_disk_key disk_key = {0,0,0};
5676 extent_buffer_get(c);
5679 c = btrfs_alloc_free_block(trans, root,
5680 btrfs_level_size(root, 0),
5681 root->root_key.objectid,
5682 &disk_key, level, 0, 0);
5685 extent_buffer_get(c);
5688 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
5689 btrfs_set_header_level(c, level);
5690 btrfs_set_header_bytenr(c, c->start);
5691 btrfs_set_header_generation(c, trans->transid);
5692 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
5693 btrfs_set_header_owner(c, root->root_key.objectid);
5695 write_extent_buffer(c, root->fs_info->fsid,
5696 btrfs_header_fsid(), BTRFS_FSID_SIZE);
5698 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
5699 (unsigned long)btrfs_header_chunk_tree_uuid(c),
5702 btrfs_mark_buffer_dirty(c);
5704 free_extent_buffer(old);
5706 add_root_to_dirty_list(root);
5710 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
5711 struct extent_buffer *eb, int tree_root)
5713 struct extent_buffer *tmp;
5714 struct btrfs_root_item *ri;
5715 struct btrfs_key key;
5718 int level = btrfs_header_level(eb);
5723 btrfs_pin_extent(fs_info, eb->start, eb->len);
5725 leafsize = btrfs_super_leafsize(fs_info->super_copy);
5726 nritems = btrfs_header_nritems(eb);
5727 for (i = 0; i < nritems; i++) {
5729 btrfs_item_key_to_cpu(eb, &key, i);
5730 if (key.type != BTRFS_ROOT_ITEM_KEY)
5732 /* Skip the extent root and reloc roots */
5733 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5734 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
5735 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
5737 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
5738 bytenr = btrfs_disk_root_bytenr(eb, ri);
5741 * If at any point we start needing the real root we
5742 * will have to build a stump root for the root we are
5743 * in, but for now this doesn't actually use the root so
5744 * just pass in extent_root.
5746 tmp = read_tree_block(fs_info->extent_root, bytenr,
5749 fprintf(stderr, "Error reading root block\n");
5752 ret = pin_down_tree_blocks(fs_info, tmp, 0);
5753 free_extent_buffer(tmp);
5757 bytenr = btrfs_node_blockptr(eb, i);
5759 /* If we aren't the tree root don't read the block */
5760 if (level == 1 && !tree_root) {
5761 btrfs_pin_extent(fs_info, bytenr, leafsize);
5765 tmp = read_tree_block(fs_info->extent_root, bytenr,
5768 fprintf(stderr, "Error reading tree block\n");
5771 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
5772 free_extent_buffer(tmp);
5781 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
5785 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
5789 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
5792 static int reset_block_groups(struct btrfs_fs_info *fs_info)
5794 struct btrfs_path *path;
5795 struct extent_buffer *leaf;
5796 struct btrfs_chunk *chunk;
5797 struct btrfs_key key;
5800 path = btrfs_alloc_path();
5805 key.type = BTRFS_CHUNK_ITEM_KEY;
5808 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
5810 btrfs_free_path(path);
5815 * We do this in case the block groups were screwed up and had alloc
5816 * bits that aren't actually set on the chunks. This happens with
5817 * restored images every time and could happen in real life I guess.
5819 fs_info->avail_data_alloc_bits = 0;
5820 fs_info->avail_metadata_alloc_bits = 0;
5821 fs_info->avail_system_alloc_bits = 0;
5823 /* First we need to create the in-memory block groups */
5825 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5826 ret = btrfs_next_leaf(fs_info->chunk_root, path);
5828 btrfs_free_path(path);
5836 leaf = path->nodes[0];
5837 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5838 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
5843 chunk = btrfs_item_ptr(leaf, path->slots[0],
5844 struct btrfs_chunk);
5845 btrfs_add_block_group(fs_info, 0,
5846 btrfs_chunk_type(leaf, chunk),
5847 key.objectid, key.offset,
5848 btrfs_chunk_length(leaf, chunk));
5852 btrfs_free_path(path);
5856 static int reset_balance(struct btrfs_trans_handle *trans,
5857 struct btrfs_fs_info *fs_info)
5859 struct btrfs_root *root = fs_info->tree_root;
5860 struct btrfs_path *path;
5861 struct extent_buffer *leaf;
5862 struct btrfs_key key;
5863 int del_slot, del_nr = 0;
5867 path = btrfs_alloc_path();
5871 key.objectid = BTRFS_BALANCE_OBJECTID;
5872 key.type = BTRFS_BALANCE_ITEM_KEY;
5875 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5882 ret = btrfs_del_item(trans, root, path);
5885 btrfs_release_path(path);
5887 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5888 key.type = BTRFS_ROOT_ITEM_KEY;
5891 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5895 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5900 ret = btrfs_del_items(trans, root, path,
5907 btrfs_release_path(path);
5910 ret = btrfs_search_slot(trans, root, &key, path,
5917 leaf = path->nodes[0];
5918 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5919 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
5921 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5926 del_slot = path->slots[0];
5935 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
5939 btrfs_release_path(path);
5941 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5942 key.type = BTRFS_ROOT_ITEM_KEY;
5943 key.offset = (u64)-1;
5944 root = btrfs_read_fs_root(fs_info, &key);
5946 fprintf(stderr, "Error reading data reloc tree\n");
5947 return PTR_ERR(root);
5949 root->track_dirty = 1;
5950 if (root->last_trans != trans->transid) {
5951 root->last_trans = trans->transid;
5952 root->commit_root = root->node;
5953 extent_buffer_get(root->node);
5955 ret = btrfs_fsck_reinit_root(trans, root, 0);
5957 btrfs_free_path(path);
5961 static int reinit_extent_tree(struct btrfs_fs_info *fs_info)
5963 struct btrfs_trans_handle *trans;
5968 * The only reason we don't do this is because right now we're just
5969 * walking the trees we find and pinning down their bytes, we don't look
5970 * at any of the leaves. In order to do mixed groups we'd have to check
5971 * the leaves of any fs roots and pin down the bytes for any file
5972 * extents we find. Not hard but why do it if we don't have to?
5974 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
5975 fprintf(stderr, "We don't support re-initing the extent tree "
5976 "for mixed block groups yet, please notify a btrfs "
5977 "developer you want to do this so they can add this "
5978 "functionality.\n");
5982 trans = btrfs_start_transaction(fs_info->extent_root, 1);
5983 if (IS_ERR(trans)) {
5984 fprintf(stderr, "Error starting transaction\n");
5985 return PTR_ERR(trans);
5989 * first we need to walk all of the trees except the extent tree and pin
5990 * down the bytes that are in use so we don't overwrite any existing
5993 ret = pin_metadata_blocks(fs_info);
5995 fprintf(stderr, "error pinning down used bytes\n");
6000 * Need to drop all the block groups since we're going to recreate all
6003 btrfs_free_block_groups(fs_info);
6004 ret = reset_block_groups(fs_info);
6006 fprintf(stderr, "error resetting the block groups\n");
6010 /* Ok we can allocate now, reinit the extent root */
6011 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 1);
6013 fprintf(stderr, "extent root initialization failed\n");
6015 * When the transaction code is updated we should end the
6016 * transaction, but for now progs only knows about commit so
6017 * just return an error.
6022 ret = reset_balance(trans, fs_info);
6024 fprintf(stderr, "error reseting the pending balance\n");
6029 * Now we have all the in-memory block groups setup so we can make
6030 * allocations properly, and the metadata we care about is safe since we
6031 * pinned all of it above.
6034 struct btrfs_block_group_cache *cache;
6036 cache = btrfs_lookup_first_block_group(fs_info, start);
6039 start = cache->key.objectid + cache->key.offset;
6040 ret = btrfs_insert_item(trans, fs_info->extent_root,
6041 &cache->key, &cache->item,
6042 sizeof(cache->item));
6044 fprintf(stderr, "Error adding block group\n");
6047 btrfs_extent_post_op(trans, fs_info->extent_root);
6051 * Ok now we commit and run the normal fsck, which will add extent
6052 * entries for all of the items it finds.
6054 return btrfs_commit_transaction(trans, fs_info->extent_root);
6057 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
6059 struct btrfs_path *path;
6060 struct btrfs_trans_handle *trans;
6061 struct btrfs_key key;
6064 printf("Recowing metadata block %llu\n", eb->start);
6065 key.objectid = btrfs_header_owner(eb);
6066 key.type = BTRFS_ROOT_ITEM_KEY;
6067 key.offset = (u64)-1;
6069 root = btrfs_read_fs_root(root->fs_info, &key);
6071 fprintf(stderr, "Couldn't find owner root %llu\n",
6073 return PTR_ERR(root);
6076 path = btrfs_alloc_path();
6080 trans = btrfs_start_transaction(root, 1);
6081 if (IS_ERR(trans)) {
6082 btrfs_free_path(path);
6083 return PTR_ERR(trans);
6086 path->lowest_level = btrfs_header_level(eb);
6087 if (path->lowest_level)
6088 btrfs_node_key_to_cpu(eb, &key, 0);
6090 btrfs_item_key_to_cpu(eb, &key, 0);
6092 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6093 btrfs_commit_transaction(trans, root);
6094 btrfs_free_path(path);
6098 static struct option long_options[] = {
6099 { "super", 1, NULL, 's' },
6100 { "repair", 0, NULL, 0 },
6101 { "init-csum-tree", 0, NULL, 0 },
6102 { "init-extent-tree", 0, NULL, 0 },
6103 { "backup", 0, NULL, 0 },
6107 const char * const cmd_check_usage[] = {
6108 "btrfs check [options] <device>",
6109 "Check an unmounted btrfs filesystem.",
6111 "-s|--super <superblock> use this superblock copy",
6112 "-b|--backup use the backup root copy",
6113 "--repair try to repair the filesystem",
6114 "--init-csum-tree create a new CRC tree",
6115 "--init-extent-tree create a new extent tree",
6119 int cmd_check(int argc, char **argv)
6121 struct cache_tree root_cache;
6122 struct btrfs_root *root;
6123 struct btrfs_fs_info *info;
6128 int option_index = 0;
6129 int init_csum_tree = 0;
6130 int init_extent_tree = 0;
6131 enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_PARTIAL;
6135 c = getopt_long(argc, argv, "as:b", long_options,
6140 case 'a': /* ignored */ break;
6142 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6146 bytenr = btrfs_sb_offset(num);
6147 printf("using SB copy %d, bytenr %llu\n", num,
6148 (unsigned long long)bytenr);
6152 usage(cmd_check_usage);
6154 if (option_index == 1) {
6155 printf("enabling repair mode\n");
6157 ctree_flags |= OPEN_CTREE_WRITES;
6158 } else if (option_index == 2) {
6159 printf("Creating a new CRC tree\n");
6161 ctree_flags |= OPEN_CTREE_WRITES;
6162 } else if (option_index == 3) {
6163 init_extent_tree = 1;
6164 ctree_flags |= (OPEN_CTREE_WRITES |
6165 OPEN_CTREE_NO_BLOCK_GROUPS);
6170 argc = argc - optind;
6173 usage(cmd_check_usage);
6176 cache_tree_init(&root_cache);
6178 if((ret = check_mounted(argv[optind])) < 0) {
6179 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6182 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6186 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6188 fprintf(stderr, "Couldn't open file system\n");
6192 uuid_unparse(info->super_copy->fsid, uuidbuf);
6193 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6195 if (!extent_buffer_uptodate(info->tree_root->node) ||
6196 !extent_buffer_uptodate(info->dev_root->node) ||
6197 !extent_buffer_uptodate(info->extent_root->node) ||
6198 !extent_buffer_uptodate(info->chunk_root->node)) {
6199 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6203 root = info->fs_root;
6205 if (init_extent_tree) {
6206 printf("Creating a new extent tree\n");
6207 ret = reinit_extent_tree(info);
6211 fprintf(stderr, "checking extents\n");
6212 if (init_csum_tree) {
6213 struct btrfs_trans_handle *trans;
6215 fprintf(stderr, "Reinit crc root\n");
6216 trans = btrfs_start_transaction(info->csum_root, 1);
6217 if (IS_ERR(trans)) {
6218 fprintf(stderr, "Error starting transaction\n");
6219 return PTR_ERR(trans);
6222 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6224 fprintf(stderr, "crc root initialization failed\n");
6228 ret = btrfs_commit_transaction(trans, info->csum_root);
6233 ret = check_chunks_and_extents(root);
6235 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6237 fprintf(stderr, "checking free space cache\n");
6238 ret = check_space_cache(root);
6242 fprintf(stderr, "checking fs roots\n");
6243 ret = check_fs_roots(root, &root_cache);
6247 fprintf(stderr, "checking csums\n");
6248 ret = check_csums(root);
6252 fprintf(stderr, "checking root refs\n");
6253 ret = check_root_refs(root, &root_cache);
6257 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
6258 struct extent_buffer *eb;
6260 eb = list_first_entry(&root->fs_info->recow_ebs,
6261 struct extent_buffer, recow);
6262 ret = recow_extent_buffer(root, eb);
6267 if (!list_empty(&root->fs_info->recow_ebs)) {
6268 fprintf(stderr, "Transid errors in file system\n");
6272 free_root_recs_tree(&root_cache);
6275 if (found_old_backref) { /*
6276 * there was a disk format change when mixed
6277 * backref was in testing tree. The old format
6278 * existed about one week.
6280 printf("\n * Found old mixed backref format. "
6281 "The old format is not supported! *"
6282 "\n * Please mount the FS in readonly mode, "
6283 "backup data and re-format the FS. *\n\n");
6286 printf("found %llu bytes used err is %d\n",
6287 (unsigned long long)bytes_used, ret);
6288 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
6289 printf("total tree bytes: %llu\n",
6290 (unsigned long long)total_btree_bytes);
6291 printf("total fs tree bytes: %llu\n",
6292 (unsigned long long)total_fs_tree_bytes);
6293 printf("total extent tree bytes: %llu\n",
6294 (unsigned long long)total_extent_tree_bytes);
6295 printf("btree space waste bytes: %llu\n",
6296 (unsigned long long)btree_space_waste);
6297 printf("file data blocks allocated: %llu\n referenced %llu\n",
6298 (unsigned long long)data_bytes_allocated,
6299 (unsigned long long)data_bytes_referenced);
6300 printf("%s\n", BTRFS_BUILD_VERSION);