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 #define REF_ERR_NO_DIR_ITEM (1 << 0)
123 #define REF_ERR_NO_DIR_INDEX (1 << 1)
124 #define REF_ERR_NO_INODE_REF (1 << 2)
125 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
126 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
127 #define REF_ERR_DUP_INODE_REF (1 << 5)
128 #define REF_ERR_INDEX_UNMATCH (1 << 6)
129 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
130 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
131 #define REF_ERR_NO_ROOT_REF (1 << 9)
132 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
133 #define REF_ERR_DUP_ROOT_REF (1 << 11)
134 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
136 struct inode_record {
137 struct list_head backrefs;
138 unsigned int checked:1;
139 unsigned int merging:1;
140 unsigned int found_inode_item:1;
141 unsigned int found_dir_item:1;
142 unsigned int found_file_extent:1;
143 unsigned int found_csum_item:1;
144 unsigned int some_csum_missing:1;
145 unsigned int nodatasum:1;
158 u64 first_extent_gap;
163 #define I_ERR_NO_INODE_ITEM (1 << 0)
164 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
165 #define I_ERR_DUP_INODE_ITEM (1 << 2)
166 #define I_ERR_DUP_DIR_INDEX (1 << 3)
167 #define I_ERR_ODD_DIR_ITEM (1 << 4)
168 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
169 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
170 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
171 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
172 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
173 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
174 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
175 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
176 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
178 struct root_backref {
179 struct list_head list;
180 unsigned int found_dir_item:1;
181 unsigned int found_dir_index:1;
182 unsigned int found_back_ref:1;
183 unsigned int found_forward_ref:1;
184 unsigned int reachable:1;
194 struct list_head backrefs;
195 struct cache_extent cache;
196 unsigned int found_root_item:1;
202 struct cache_extent cache;
207 struct cache_extent cache;
208 struct cache_tree root_cache;
209 struct cache_tree inode_cache;
210 struct inode_record *current;
219 struct walk_control {
220 struct cache_tree shared;
221 struct shared_node *nodes[BTRFS_MAX_LEVEL];
226 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
228 static u8 imode_to_type(u32 imode)
231 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
232 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
233 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
234 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
235 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
236 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
237 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
238 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
241 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
245 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
247 struct device_record *rec1;
248 struct device_record *rec2;
250 rec1 = rb_entry(node1, struct device_record, node);
251 rec2 = rb_entry(node2, struct device_record, node);
252 if (rec1->devid > rec2->devid)
254 else if (rec1->devid < rec2->devid)
260 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
262 struct inode_record *rec;
263 struct inode_backref *backref;
264 struct inode_backref *orig;
267 rec = malloc(sizeof(*rec));
268 memcpy(rec, orig_rec, sizeof(*rec));
270 INIT_LIST_HEAD(&rec->backrefs);
272 list_for_each_entry(orig, &orig_rec->backrefs, list) {
273 size = sizeof(*orig) + orig->namelen + 1;
274 backref = malloc(size);
275 memcpy(backref, orig, size);
276 list_add_tail(&backref->list, &rec->backrefs);
281 static void print_inode_error(int errors)
283 if (errors & I_ERR_NO_INODE_ITEM)
284 fprintf(stderr, ", no inode item");
285 if (errors & I_ERR_NO_ORPHAN_ITEM)
286 fprintf(stderr, ", no orphan item");
287 if (errors & I_ERR_DUP_INODE_ITEM)
288 fprintf(stderr, ", dup inode item");
289 if (errors & I_ERR_DUP_DIR_INDEX)
290 fprintf(stderr, ", dup dir index");
291 if (errors & I_ERR_ODD_DIR_ITEM)
292 fprintf(stderr, ", odd dir item");
293 if (errors & I_ERR_ODD_FILE_EXTENT)
294 fprintf(stderr, ", odd file extent");
295 if (errors & I_ERR_BAD_FILE_EXTENT)
296 fprintf(stderr, ", bad file extent");
297 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
298 fprintf(stderr, ", file extent overlap");
299 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
300 fprintf(stderr, ", file extent discount");
301 if (errors & I_ERR_DIR_ISIZE_WRONG)
302 fprintf(stderr, ", dir isize wrong");
303 if (errors & I_ERR_FILE_NBYTES_WRONG)
304 fprintf(stderr, ", nbytes wrong");
305 if (errors & I_ERR_ODD_CSUM_ITEM)
306 fprintf(stderr, ", odd csum item");
307 if (errors & I_ERR_SOME_CSUM_MISSING)
308 fprintf(stderr, ", some csum missing");
309 if (errors & I_ERR_LINK_COUNT_WRONG)
310 fprintf(stderr, ", link count wrong");
311 fprintf(stderr, "\n");
314 static void print_ref_error(int errors)
316 if (errors & REF_ERR_NO_DIR_ITEM)
317 fprintf(stderr, ", no dir item");
318 if (errors & REF_ERR_NO_DIR_INDEX)
319 fprintf(stderr, ", no dir index");
320 if (errors & REF_ERR_NO_INODE_REF)
321 fprintf(stderr, ", no inode ref");
322 if (errors & REF_ERR_DUP_DIR_ITEM)
323 fprintf(stderr, ", dup dir item");
324 if (errors & REF_ERR_DUP_DIR_INDEX)
325 fprintf(stderr, ", dup dir index");
326 if (errors & REF_ERR_DUP_INODE_REF)
327 fprintf(stderr, ", dup inode ref");
328 if (errors & REF_ERR_INDEX_UNMATCH)
329 fprintf(stderr, ", index unmatch");
330 if (errors & REF_ERR_FILETYPE_UNMATCH)
331 fprintf(stderr, ", filetype unmatch");
332 if (errors & REF_ERR_NAME_TOO_LONG)
333 fprintf(stderr, ", name too long");
334 if (errors & REF_ERR_NO_ROOT_REF)
335 fprintf(stderr, ", no root ref");
336 if (errors & REF_ERR_NO_ROOT_BACKREF)
337 fprintf(stderr, ", no root backref");
338 if (errors & REF_ERR_DUP_ROOT_REF)
339 fprintf(stderr, ", dup root ref");
340 if (errors & REF_ERR_DUP_ROOT_BACKREF)
341 fprintf(stderr, ", dup root backref");
342 fprintf(stderr, "\n");
345 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
348 struct ptr_node *node;
349 struct cache_extent *cache;
350 struct inode_record *rec = NULL;
353 cache = lookup_cache_extent(inode_cache, ino, 1);
355 node = container_of(cache, struct ptr_node, cache);
357 if (mod && rec->refs > 1) {
358 node->data = clone_inode_rec(rec);
363 rec = calloc(1, sizeof(*rec));
365 rec->extent_start = (u64)-1;
366 rec->first_extent_gap = (u64)-1;
368 INIT_LIST_HEAD(&rec->backrefs);
370 node = malloc(sizeof(*node));
371 node->cache.start = ino;
372 node->cache.size = 1;
375 if (ino == BTRFS_FREE_INO_OBJECTID)
378 ret = insert_cache_extent(inode_cache, &node->cache);
384 static void free_inode_rec(struct inode_record *rec)
386 struct inode_backref *backref;
391 while (!list_empty(&rec->backrefs)) {
392 backref = list_entry(rec->backrefs.next,
393 struct inode_backref, list);
394 list_del(&backref->list);
400 static int can_free_inode_rec(struct inode_record *rec)
402 if (!rec->errors && rec->checked && rec->found_inode_item &&
403 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
408 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
409 struct inode_record *rec)
411 struct cache_extent *cache;
412 struct inode_backref *tmp, *backref;
413 struct ptr_node *node;
414 unsigned char filetype;
416 if (!rec->found_inode_item)
419 filetype = imode_to_type(rec->imode);
420 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
421 if (backref->found_dir_item && backref->found_dir_index) {
422 if (backref->filetype != filetype)
423 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
424 if (!backref->errors && backref->found_inode_ref) {
425 list_del(&backref->list);
431 if (!rec->checked || rec->merging)
434 if (S_ISDIR(rec->imode)) {
435 if (rec->found_size != rec->isize)
436 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
437 if (rec->found_file_extent)
438 rec->errors |= I_ERR_ODD_FILE_EXTENT;
439 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
440 if (rec->found_dir_item)
441 rec->errors |= I_ERR_ODD_DIR_ITEM;
442 if (rec->found_size != rec->nbytes)
443 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
444 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
445 rec->first_extent_gap = 0;
446 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
447 rec->first_extent_gap < rec->isize))
448 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
451 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
452 if (rec->found_csum_item && rec->nodatasum)
453 rec->errors |= I_ERR_ODD_CSUM_ITEM;
454 if (rec->some_csum_missing && !rec->nodatasum)
455 rec->errors |= I_ERR_SOME_CSUM_MISSING;
458 BUG_ON(rec->refs != 1);
459 if (can_free_inode_rec(rec)) {
460 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
461 node = container_of(cache, struct ptr_node, cache);
462 BUG_ON(node->data != rec);
463 remove_cache_extent(inode_cache, &node->cache);
469 static int check_orphan_item(struct btrfs_root *root, u64 ino)
471 struct btrfs_path path;
472 struct btrfs_key key;
475 key.objectid = BTRFS_ORPHAN_OBJECTID;
476 key.type = BTRFS_ORPHAN_ITEM_KEY;
479 btrfs_init_path(&path);
480 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
481 btrfs_release_path(&path);
487 static int process_inode_item(struct extent_buffer *eb,
488 int slot, struct btrfs_key *key,
489 struct shared_node *active_node)
491 struct inode_record *rec;
492 struct btrfs_inode_item *item;
494 rec = active_node->current;
495 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
496 if (rec->found_inode_item) {
497 rec->errors |= I_ERR_DUP_INODE_ITEM;
500 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
501 rec->nlink = btrfs_inode_nlink(eb, item);
502 rec->isize = btrfs_inode_size(eb, item);
503 rec->nbytes = btrfs_inode_nbytes(eb, item);
504 rec->imode = btrfs_inode_mode(eb, item);
505 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
507 rec->found_inode_item = 1;
509 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
510 maybe_free_inode_rec(&active_node->inode_cache, rec);
514 static struct inode_backref *get_inode_backref(struct inode_record *rec,
516 int namelen, u64 dir)
518 struct inode_backref *backref;
520 list_for_each_entry(backref, &rec->backrefs, list) {
521 if (backref->dir != dir || backref->namelen != namelen)
523 if (memcmp(name, backref->name, namelen))
528 backref = malloc(sizeof(*backref) + namelen + 1);
529 memset(backref, 0, sizeof(*backref));
531 backref->namelen = namelen;
532 memcpy(backref->name, name, namelen);
533 backref->name[namelen] = '\0';
534 list_add_tail(&backref->list, &rec->backrefs);
538 static int add_inode_backref(struct cache_tree *inode_cache,
539 u64 ino, u64 dir, u64 index,
540 const char *name, int namelen,
541 int filetype, int itemtype, int errors)
543 struct inode_record *rec;
544 struct inode_backref *backref;
546 rec = get_inode_rec(inode_cache, ino, 1);
547 backref = get_inode_backref(rec, name, namelen, dir);
549 backref->errors |= errors;
550 if (itemtype == BTRFS_DIR_INDEX_KEY) {
551 if (backref->found_dir_index)
552 backref->errors |= REF_ERR_DUP_DIR_INDEX;
553 if (backref->found_inode_ref && backref->index != index)
554 backref->errors |= REF_ERR_INDEX_UNMATCH;
555 if (backref->found_dir_item && backref->filetype != filetype)
556 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
558 backref->index = index;
559 backref->filetype = filetype;
560 backref->found_dir_index = 1;
561 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
563 if (backref->found_dir_item)
564 backref->errors |= REF_ERR_DUP_DIR_ITEM;
565 if (backref->found_dir_index && backref->filetype != filetype)
566 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
568 backref->filetype = filetype;
569 backref->found_dir_item = 1;
570 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
571 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
572 if (backref->found_inode_ref)
573 backref->errors |= REF_ERR_DUP_INODE_REF;
574 if (backref->found_dir_index && backref->index != index)
575 backref->errors |= REF_ERR_INDEX_UNMATCH;
577 backref->ref_type = itemtype;
578 backref->index = index;
579 backref->found_inode_ref = 1;
584 maybe_free_inode_rec(inode_cache, rec);
588 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
589 struct cache_tree *dst_cache)
591 struct inode_backref *backref;
595 list_for_each_entry(backref, &src->backrefs, list) {
596 if (backref->found_dir_index) {
597 add_inode_backref(dst_cache, dst->ino, backref->dir,
598 backref->index, backref->name,
599 backref->namelen, backref->filetype,
600 BTRFS_DIR_INDEX_KEY, backref->errors);
602 if (backref->found_dir_item) {
604 add_inode_backref(dst_cache, dst->ino,
605 backref->dir, 0, backref->name,
606 backref->namelen, backref->filetype,
607 BTRFS_DIR_ITEM_KEY, backref->errors);
609 if (backref->found_inode_ref) {
610 add_inode_backref(dst_cache, dst->ino,
611 backref->dir, backref->index,
612 backref->name, backref->namelen, 0,
613 backref->ref_type, backref->errors);
617 if (src->found_dir_item)
618 dst->found_dir_item = 1;
619 if (src->found_file_extent)
620 dst->found_file_extent = 1;
621 if (src->found_csum_item)
622 dst->found_csum_item = 1;
623 if (src->some_csum_missing)
624 dst->some_csum_missing = 1;
625 if (dst->first_extent_gap > src->first_extent_gap)
626 dst->first_extent_gap = src->first_extent_gap;
628 BUG_ON(src->found_link < dir_count);
629 dst->found_link += src->found_link - dir_count;
630 dst->found_size += src->found_size;
631 if (src->extent_start != (u64)-1) {
632 if (dst->extent_start == (u64)-1) {
633 dst->extent_start = src->extent_start;
634 dst->extent_end = src->extent_end;
636 if (dst->extent_end > src->extent_start)
637 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
638 else if (dst->extent_end < src->extent_start &&
639 dst->extent_end < dst->first_extent_gap)
640 dst->first_extent_gap = dst->extent_end;
641 if (dst->extent_end < src->extent_end)
642 dst->extent_end = src->extent_end;
646 dst->errors |= src->errors;
647 if (src->found_inode_item) {
648 if (!dst->found_inode_item) {
649 dst->nlink = src->nlink;
650 dst->isize = src->isize;
651 dst->nbytes = src->nbytes;
652 dst->imode = src->imode;
653 dst->nodatasum = src->nodatasum;
654 dst->found_inode_item = 1;
656 dst->errors |= I_ERR_DUP_INODE_ITEM;
664 static int splice_shared_node(struct shared_node *src_node,
665 struct shared_node *dst_node)
667 struct cache_extent *cache;
668 struct ptr_node *node, *ins;
669 struct cache_tree *src, *dst;
670 struct inode_record *rec, *conflict;
675 if (--src_node->refs == 0)
677 if (src_node->current)
678 current_ino = src_node->current->ino;
680 src = &src_node->root_cache;
681 dst = &dst_node->root_cache;
683 cache = search_cache_extent(src, 0);
685 node = container_of(cache, struct ptr_node, cache);
687 cache = next_cache_extent(cache);
690 remove_cache_extent(src, &node->cache);
693 ins = malloc(sizeof(*ins));
694 ins->cache.start = node->cache.start;
695 ins->cache.size = node->cache.size;
699 ret = insert_cache_extent(dst, &ins->cache);
700 if (ret == -EEXIST) {
701 conflict = get_inode_rec(dst, rec->ino, 1);
702 merge_inode_recs(rec, conflict, dst);
704 conflict->checked = 1;
705 if (dst_node->current == conflict)
706 dst_node->current = NULL;
708 maybe_free_inode_rec(dst, conflict);
716 if (src == &src_node->root_cache) {
717 src = &src_node->inode_cache;
718 dst = &dst_node->inode_cache;
722 if (current_ino > 0 && (!dst_node->current ||
723 current_ino > dst_node->current->ino)) {
724 if (dst_node->current) {
725 dst_node->current->checked = 1;
726 maybe_free_inode_rec(dst, dst_node->current);
728 dst_node->current = get_inode_rec(dst, current_ino, 1);
733 static void free_inode_ptr(struct cache_extent *cache)
735 struct ptr_node *node;
736 struct inode_record *rec;
738 node = container_of(cache, struct ptr_node, cache);
744 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
746 static struct shared_node *find_shared_node(struct cache_tree *shared,
749 struct cache_extent *cache;
750 struct shared_node *node;
752 cache = lookup_cache_extent(shared, bytenr, 1);
754 node = container_of(cache, struct shared_node, cache);
760 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
763 struct shared_node *node;
765 node = calloc(1, sizeof(*node));
766 node->cache.start = bytenr;
767 node->cache.size = 1;
768 cache_tree_init(&node->root_cache);
769 cache_tree_init(&node->inode_cache);
772 ret = insert_cache_extent(shared, &node->cache);
777 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
778 struct walk_control *wc, int level)
780 struct shared_node *node;
781 struct shared_node *dest;
783 if (level == wc->active_node)
786 BUG_ON(wc->active_node <= level);
787 node = find_shared_node(&wc->shared, bytenr);
789 add_shared_node(&wc->shared, bytenr, refs);
790 node = find_shared_node(&wc->shared, bytenr);
791 wc->nodes[level] = node;
792 wc->active_node = level;
796 if (wc->root_level == wc->active_node &&
797 btrfs_root_refs(&root->root_item) == 0) {
798 if (--node->refs == 0) {
799 free_inode_recs_tree(&node->root_cache);
800 free_inode_recs_tree(&node->inode_cache);
801 remove_cache_extent(&wc->shared, &node->cache);
807 dest = wc->nodes[wc->active_node];
808 splice_shared_node(node, dest);
809 if (node->refs == 0) {
810 remove_cache_extent(&wc->shared, &node->cache);
816 static int leave_shared_node(struct btrfs_root *root,
817 struct walk_control *wc, int level)
819 struct shared_node *node;
820 struct shared_node *dest;
823 if (level == wc->root_level)
826 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
830 BUG_ON(i >= BTRFS_MAX_LEVEL);
832 node = wc->nodes[wc->active_node];
833 wc->nodes[wc->active_node] = NULL;
836 dest = wc->nodes[wc->active_node];
837 if (wc->active_node < wc->root_level ||
838 btrfs_root_refs(&root->root_item) > 0) {
839 BUG_ON(node->refs <= 1);
840 splice_shared_node(node, dest);
842 BUG_ON(node->refs < 2);
848 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
851 struct btrfs_path path;
852 struct btrfs_key key;
853 struct extent_buffer *leaf;
857 btrfs_init_path(&path);
859 key.objectid = parent_root_id;
860 key.type = BTRFS_ROOT_REF_KEY;
861 key.offset = child_root_id;
862 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
865 btrfs_release_path(&path);
869 key.objectid = child_root_id;
870 key.type = BTRFS_ROOT_BACKREF_KEY;
872 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
877 leaf = path.nodes[0];
878 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
879 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
884 leaf = path.nodes[0];
887 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
888 if (key.objectid != child_root_id ||
889 key.type != BTRFS_ROOT_BACKREF_KEY)
894 if (key.offset == parent_root_id) {
895 btrfs_release_path(&path);
902 btrfs_release_path(&path);
903 return has_parent? 0 : -1;
906 static int process_dir_item(struct btrfs_root *root,
907 struct extent_buffer *eb,
908 int slot, struct btrfs_key *key,
909 struct shared_node *active_node)
919 struct btrfs_dir_item *di;
920 struct inode_record *rec;
921 struct cache_tree *root_cache;
922 struct cache_tree *inode_cache;
923 struct btrfs_key location;
924 char namebuf[BTRFS_NAME_LEN];
926 root_cache = &active_node->root_cache;
927 inode_cache = &active_node->inode_cache;
928 rec = active_node->current;
929 rec->found_dir_item = 1;
931 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
932 total = btrfs_item_size_nr(eb, slot);
933 while (cur < total) {
935 btrfs_dir_item_key_to_cpu(eb, di, &location);
936 name_len = btrfs_dir_name_len(eb, di);
937 data_len = btrfs_dir_data_len(eb, di);
938 filetype = btrfs_dir_type(eb, di);
940 rec->found_size += name_len;
941 if (name_len <= BTRFS_NAME_LEN) {
945 len = BTRFS_NAME_LEN;
946 error = REF_ERR_NAME_TOO_LONG;
948 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
950 if (location.type == BTRFS_INODE_ITEM_KEY) {
951 add_inode_backref(inode_cache, location.objectid,
952 key->objectid, key->offset, namebuf,
953 len, filetype, key->type, error);
954 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
955 add_inode_backref(root_cache, location.objectid,
956 key->objectid, key->offset,
957 namebuf, len, filetype,
960 fprintf(stderr, "warning line %d\n", __LINE__);
963 len = sizeof(*di) + name_len + data_len;
964 di = (struct btrfs_dir_item *)((char *)di + len);
967 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
968 rec->errors |= I_ERR_DUP_DIR_INDEX;
973 static int process_inode_ref(struct extent_buffer *eb,
974 int slot, struct btrfs_key *key,
975 struct shared_node *active_node)
983 struct cache_tree *inode_cache;
984 struct btrfs_inode_ref *ref;
985 char namebuf[BTRFS_NAME_LEN];
987 inode_cache = &active_node->inode_cache;
989 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
990 total = btrfs_item_size_nr(eb, slot);
991 while (cur < total) {
992 name_len = btrfs_inode_ref_name_len(eb, ref);
993 index = btrfs_inode_ref_index(eb, ref);
994 if (name_len <= BTRFS_NAME_LEN) {
998 len = BTRFS_NAME_LEN;
999 error = REF_ERR_NAME_TOO_LONG;
1001 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1002 add_inode_backref(inode_cache, key->objectid, key->offset,
1003 index, namebuf, len, 0, key->type, error);
1005 len = sizeof(*ref) + name_len;
1006 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1012 static int process_inode_extref(struct extent_buffer *eb,
1013 int slot, struct btrfs_key *key,
1014 struct shared_node *active_node)
1023 struct cache_tree *inode_cache;
1024 struct btrfs_inode_extref *extref;
1025 char namebuf[BTRFS_NAME_LEN];
1027 inode_cache = &active_node->inode_cache;
1029 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1030 total = btrfs_item_size_nr(eb, slot);
1031 while (cur < total) {
1032 name_len = btrfs_inode_extref_name_len(eb, extref);
1033 index = btrfs_inode_extref_index(eb, extref);
1034 parent = btrfs_inode_extref_parent(eb, extref);
1035 if (name_len <= BTRFS_NAME_LEN) {
1039 len = BTRFS_NAME_LEN;
1040 error = REF_ERR_NAME_TOO_LONG;
1042 read_extent_buffer(eb, namebuf,
1043 (unsigned long)(extref + 1), len);
1044 add_inode_backref(inode_cache, key->objectid, parent,
1045 index, namebuf, len, 0, key->type, error);
1047 len = sizeof(*extref) + name_len;
1048 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1055 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
1057 struct btrfs_key key;
1058 struct btrfs_path path;
1059 struct extent_buffer *leaf;
1064 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1066 btrfs_init_path(&path);
1068 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1070 key.type = BTRFS_EXTENT_CSUM_KEY;
1072 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1075 if (ret > 0 && path.slots[0] > 0) {
1076 leaf = path.nodes[0];
1077 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1078 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1079 key.type == BTRFS_EXTENT_CSUM_KEY)
1084 leaf = path.nodes[0];
1085 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1086 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1090 leaf = path.nodes[0];
1093 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1094 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1095 key.type != BTRFS_EXTENT_CSUM_KEY)
1098 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1099 if (key.offset >= start + len)
1102 if (key.offset > start)
1105 size = btrfs_item_size_nr(leaf, path.slots[0]);
1106 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1107 if (csum_end > start) {
1108 size = min(csum_end - start, len);
1116 btrfs_release_path(&path);
1120 static int process_file_extent(struct btrfs_root *root,
1121 struct extent_buffer *eb,
1122 int slot, struct btrfs_key *key,
1123 struct shared_node *active_node)
1125 struct inode_record *rec;
1126 struct btrfs_file_extent_item *fi;
1128 u64 disk_bytenr = 0;
1129 u64 extent_offset = 0;
1130 u64 mask = root->sectorsize - 1;
1133 rec = active_node->current;
1134 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1135 rec->found_file_extent = 1;
1137 if (rec->extent_start == (u64)-1) {
1138 rec->extent_start = key->offset;
1139 rec->extent_end = key->offset;
1142 if (rec->extent_end > key->offset)
1143 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1144 else if (rec->extent_end < key->offset &&
1145 rec->extent_end < rec->first_extent_gap)
1146 rec->first_extent_gap = rec->extent_end;
1148 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1149 extent_type = btrfs_file_extent_type(eb, fi);
1151 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1152 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1154 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1155 rec->found_size += num_bytes;
1156 num_bytes = (num_bytes + mask) & ~mask;
1157 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1158 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1159 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1160 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1161 extent_offset = btrfs_file_extent_offset(eb, fi);
1162 if (num_bytes == 0 || (num_bytes & mask))
1163 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1164 if (num_bytes + extent_offset >
1165 btrfs_file_extent_ram_bytes(eb, fi))
1166 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1167 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1168 (btrfs_file_extent_compression(eb, fi) ||
1169 btrfs_file_extent_encryption(eb, fi) ||
1170 btrfs_file_extent_other_encoding(eb, fi)))
1171 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1172 if (disk_bytenr > 0)
1173 rec->found_size += num_bytes;
1175 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1177 rec->extent_end = key->offset + num_bytes;
1179 if (disk_bytenr > 0) {
1181 if (btrfs_file_extent_compression(eb, fi))
1182 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1184 disk_bytenr += extent_offset;
1186 found = count_csum_range(root, disk_bytenr, num_bytes);
1187 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1189 rec->found_csum_item = 1;
1190 if (found < num_bytes)
1191 rec->some_csum_missing = 1;
1192 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1194 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1200 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1201 struct walk_control *wc)
1203 struct btrfs_key key;
1207 struct cache_tree *inode_cache;
1208 struct shared_node *active_node;
1210 if (wc->root_level == wc->active_node &&
1211 btrfs_root_refs(&root->root_item) == 0)
1214 active_node = wc->nodes[wc->active_node];
1215 inode_cache = &active_node->inode_cache;
1216 nritems = btrfs_header_nritems(eb);
1217 for (i = 0; i < nritems; i++) {
1218 btrfs_item_key_to_cpu(eb, &key, i);
1220 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1223 if (active_node->current == NULL ||
1224 active_node->current->ino < key.objectid) {
1225 if (active_node->current) {
1226 active_node->current->checked = 1;
1227 maybe_free_inode_rec(inode_cache,
1228 active_node->current);
1230 active_node->current = get_inode_rec(inode_cache,
1234 case BTRFS_DIR_ITEM_KEY:
1235 case BTRFS_DIR_INDEX_KEY:
1236 ret = process_dir_item(root, eb, i, &key, active_node);
1238 case BTRFS_INODE_REF_KEY:
1239 ret = process_inode_ref(eb, i, &key, active_node);
1241 case BTRFS_INODE_EXTREF_KEY:
1242 ret = process_inode_extref(eb, i, &key, active_node);
1244 case BTRFS_INODE_ITEM_KEY:
1245 ret = process_inode_item(eb, i, &key, active_node);
1247 case BTRFS_EXTENT_DATA_KEY:
1248 ret = process_file_extent(root, eb, i, &key,
1258 static void reada_walk_down(struct btrfs_root *root,
1259 struct extent_buffer *node, int slot)
1269 level = btrfs_header_level(node);
1273 nritems = btrfs_header_nritems(node);
1274 blocksize = btrfs_level_size(root, level - 1);
1275 for (i = slot; i < nritems; i++) {
1276 bytenr = btrfs_node_blockptr(node, i);
1277 ptr_gen = btrfs_node_ptr_generation(node, i);
1278 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1284 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1285 struct walk_control *wc, int *level)
1289 struct extent_buffer *next;
1290 struct extent_buffer *cur;
1295 WARN_ON(*level < 0);
1296 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1297 ret = btrfs_lookup_extent_info(NULL, root,
1298 path->nodes[*level]->start,
1299 *level, 1, &refs, NULL);
1306 ret = enter_shared_node(root, path->nodes[*level]->start,
1314 while (*level >= 0) {
1315 WARN_ON(*level < 0);
1316 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1317 cur = path->nodes[*level];
1319 if (btrfs_header_level(cur) != *level)
1322 if (path->slots[*level] >= btrfs_header_nritems(cur))
1325 ret = process_one_leaf(root, cur, wc);
1328 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1329 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1330 blocksize = btrfs_level_size(root, *level - 1);
1331 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1337 ret = enter_shared_node(root, bytenr, refs,
1340 path->slots[*level]++;
1345 next = btrfs_find_tree_block(root, bytenr, blocksize);
1346 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1347 free_extent_buffer(next);
1348 reada_walk_down(root, cur, path->slots[*level]);
1349 next = read_tree_block(root, bytenr, blocksize,
1357 *level = *level - 1;
1358 free_extent_buffer(path->nodes[*level]);
1359 path->nodes[*level] = next;
1360 path->slots[*level] = 0;
1363 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1367 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1368 struct walk_control *wc, int *level)
1371 struct extent_buffer *leaf;
1373 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1374 leaf = path->nodes[i];
1375 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1380 free_extent_buffer(path->nodes[*level]);
1381 path->nodes[*level] = NULL;
1382 BUG_ON(*level > wc->active_node);
1383 if (*level == wc->active_node)
1384 leave_shared_node(root, wc, *level);
1391 static int check_root_dir(struct inode_record *rec)
1393 struct inode_backref *backref;
1396 if (!rec->found_inode_item || rec->errors)
1398 if (rec->nlink != 1 || rec->found_link != 0)
1400 if (list_empty(&rec->backrefs))
1402 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1403 if (!backref->found_inode_ref)
1405 if (backref->index != 0 || backref->namelen != 2 ||
1406 memcmp(backref->name, "..", 2))
1408 if (backref->found_dir_index || backref->found_dir_item)
1415 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1417 struct btrfs_trans_handle *trans;
1418 struct btrfs_path *path;
1419 struct btrfs_inode_item *ei;
1420 struct btrfs_key key;
1423 /* So far we just fix dir isize wrong */
1424 if (!(rec->errors & I_ERR_DIR_ISIZE_WRONG))
1427 path = btrfs_alloc_path();
1431 trans = btrfs_start_transaction(root, 1);
1432 if (IS_ERR(trans)) {
1433 btrfs_free_path(path);
1434 return PTR_ERR(trans);
1437 key.objectid = rec->ino;
1438 key.type = BTRFS_INODE_ITEM_KEY;
1439 key.offset = (u64)-1;
1441 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1445 if (!path->slots[0]) {
1452 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1453 if (key.objectid != rec->ino) {
1458 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1459 struct btrfs_inode_item);
1460 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1461 btrfs_mark_buffer_dirty(path->nodes[0]);
1462 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1463 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1464 root->root_key.objectid);
1466 btrfs_commit_transaction(trans, root);
1467 btrfs_free_path(path);
1471 static int check_inode_recs(struct btrfs_root *root,
1472 struct cache_tree *inode_cache)
1474 struct cache_extent *cache;
1475 struct ptr_node *node;
1476 struct inode_record *rec;
1477 struct inode_backref *backref;
1480 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1482 if (btrfs_root_refs(&root->root_item) == 0) {
1483 if (!cache_tree_empty(inode_cache))
1484 fprintf(stderr, "warning line %d\n", __LINE__);
1488 rec = get_inode_rec(inode_cache, root_dirid, 0);
1490 ret = check_root_dir(rec);
1492 fprintf(stderr, "root %llu root dir %llu error\n",
1493 (unsigned long long)root->root_key.objectid,
1494 (unsigned long long)root_dirid);
1498 fprintf(stderr, "root %llu root dir %llu not found\n",
1499 (unsigned long long)root->root_key.objectid,
1500 (unsigned long long)root_dirid);
1504 cache = search_cache_extent(inode_cache, 0);
1507 node = container_of(cache, struct ptr_node, cache);
1509 remove_cache_extent(inode_cache, &node->cache);
1511 if (rec->ino == root_dirid ||
1512 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1513 free_inode_rec(rec);
1517 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1518 ret = check_orphan_item(root, rec->ino);
1520 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1521 if (can_free_inode_rec(rec)) {
1522 free_inode_rec(rec);
1528 ret = try_repair_inode(root, rec);
1529 if (ret == 0 && can_free_inode_rec(rec)) {
1530 free_inode_rec(rec);
1537 if (!rec->found_inode_item)
1538 rec->errors |= I_ERR_NO_INODE_ITEM;
1539 if (rec->found_link != rec->nlink)
1540 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1541 fprintf(stderr, "root %llu inode %llu errors %x",
1542 (unsigned long long) root->root_key.objectid,
1543 (unsigned long long) rec->ino, rec->errors);
1544 print_inode_error(rec->errors);
1545 list_for_each_entry(backref, &rec->backrefs, list) {
1546 if (!backref->found_dir_item)
1547 backref->errors |= REF_ERR_NO_DIR_ITEM;
1548 if (!backref->found_dir_index)
1549 backref->errors |= REF_ERR_NO_DIR_INDEX;
1550 if (!backref->found_inode_ref)
1551 backref->errors |= REF_ERR_NO_INODE_REF;
1552 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1553 " namelen %u name %s filetype %d error %x",
1554 (unsigned long long)backref->dir,
1555 (unsigned long long)backref->index,
1556 backref->namelen, backref->name,
1557 backref->filetype, backref->errors);
1558 print_ref_error(backref->errors);
1560 free_inode_rec(rec);
1562 return (error > 0) ? -1 : 0;
1565 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1568 struct cache_extent *cache;
1569 struct root_record *rec = NULL;
1572 cache = lookup_cache_extent(root_cache, objectid, 1);
1574 rec = container_of(cache, struct root_record, cache);
1576 rec = calloc(1, sizeof(*rec));
1577 rec->objectid = objectid;
1578 INIT_LIST_HEAD(&rec->backrefs);
1579 rec->cache.start = objectid;
1580 rec->cache.size = 1;
1582 ret = insert_cache_extent(root_cache, &rec->cache);
1588 static struct root_backref *get_root_backref(struct root_record *rec,
1589 u64 ref_root, u64 dir, u64 index,
1590 const char *name, int namelen)
1592 struct root_backref *backref;
1594 list_for_each_entry(backref, &rec->backrefs, list) {
1595 if (backref->ref_root != ref_root || backref->dir != dir ||
1596 backref->namelen != namelen)
1598 if (memcmp(name, backref->name, namelen))
1603 backref = malloc(sizeof(*backref) + namelen + 1);
1604 memset(backref, 0, sizeof(*backref));
1605 backref->ref_root = ref_root;
1607 backref->index = index;
1608 backref->namelen = namelen;
1609 memcpy(backref->name, name, namelen);
1610 backref->name[namelen] = '\0';
1611 list_add_tail(&backref->list, &rec->backrefs);
1615 static void free_root_record(struct cache_extent *cache)
1617 struct root_record *rec;
1618 struct root_backref *backref;
1620 rec = container_of(cache, struct root_record, cache);
1621 while (!list_empty(&rec->backrefs)) {
1622 backref = list_entry(rec->backrefs.next,
1623 struct root_backref, list);
1624 list_del(&backref->list);
1631 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
1633 static int add_root_backref(struct cache_tree *root_cache,
1634 u64 root_id, u64 ref_root, u64 dir, u64 index,
1635 const char *name, int namelen,
1636 int item_type, int errors)
1638 struct root_record *rec;
1639 struct root_backref *backref;
1641 rec = get_root_rec(root_cache, root_id);
1642 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1644 backref->errors |= errors;
1646 if (item_type != BTRFS_DIR_ITEM_KEY) {
1647 if (backref->found_dir_index || backref->found_back_ref ||
1648 backref->found_forward_ref) {
1649 if (backref->index != index)
1650 backref->errors |= REF_ERR_INDEX_UNMATCH;
1652 backref->index = index;
1656 if (item_type == BTRFS_DIR_ITEM_KEY) {
1657 if (backref->found_forward_ref)
1659 backref->found_dir_item = 1;
1660 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1661 backref->found_dir_index = 1;
1662 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1663 if (backref->found_forward_ref)
1664 backref->errors |= REF_ERR_DUP_ROOT_REF;
1665 else if (backref->found_dir_item)
1667 backref->found_forward_ref = 1;
1668 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1669 if (backref->found_back_ref)
1670 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1671 backref->found_back_ref = 1;
1676 if (backref->found_forward_ref && backref->found_dir_item)
1677 backref->reachable = 1;
1681 static int merge_root_recs(struct btrfs_root *root,
1682 struct cache_tree *src_cache,
1683 struct cache_tree *dst_cache)
1685 struct cache_extent *cache;
1686 struct ptr_node *node;
1687 struct inode_record *rec;
1688 struct inode_backref *backref;
1690 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1691 free_inode_recs_tree(src_cache);
1696 cache = search_cache_extent(src_cache, 0);
1699 node = container_of(cache, struct ptr_node, cache);
1701 remove_cache_extent(src_cache, &node->cache);
1704 if (!is_child_root(root, root->objectid, rec->ino))
1707 list_for_each_entry(backref, &rec->backrefs, list) {
1708 BUG_ON(backref->found_inode_ref);
1709 if (backref->found_dir_item)
1710 add_root_backref(dst_cache, rec->ino,
1711 root->root_key.objectid, backref->dir,
1712 backref->index, backref->name,
1713 backref->namelen, BTRFS_DIR_ITEM_KEY,
1715 if (backref->found_dir_index)
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_INDEX_KEY,
1723 free_inode_rec(rec);
1728 static int check_root_refs(struct btrfs_root *root,
1729 struct cache_tree *root_cache)
1731 struct root_record *rec;
1732 struct root_record *ref_root;
1733 struct root_backref *backref;
1734 struct cache_extent *cache;
1740 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1743 /* fixme: this can not detect circular references */
1746 cache = search_cache_extent(root_cache, 0);
1750 rec = container_of(cache, struct root_record, cache);
1751 cache = next_cache_extent(cache);
1753 if (rec->found_ref == 0)
1756 list_for_each_entry(backref, &rec->backrefs, list) {
1757 if (!backref->reachable)
1760 ref_root = get_root_rec(root_cache,
1762 if (ref_root->found_ref > 0)
1765 backref->reachable = 0;
1767 if (rec->found_ref == 0)
1773 cache = search_cache_extent(root_cache, 0);
1777 rec = container_of(cache, struct root_record, cache);
1778 cache = next_cache_extent(cache);
1780 if (rec->found_ref == 0 &&
1781 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1782 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1783 ret = check_orphan_item(root->fs_info->tree_root,
1789 * If we don't have a root item then we likely just have
1790 * a dir item in a snapshot for this root but no actual
1791 * ref key or anything so it's meaningless.
1793 if (!rec->found_root_item)
1796 fprintf(stderr, "fs tree %llu not referenced\n",
1797 (unsigned long long)rec->objectid);
1801 if (rec->found_ref > 0 && !rec->found_root_item)
1803 list_for_each_entry(backref, &rec->backrefs, list) {
1804 if (!backref->found_dir_item)
1805 backref->errors |= REF_ERR_NO_DIR_ITEM;
1806 if (!backref->found_dir_index)
1807 backref->errors |= REF_ERR_NO_DIR_INDEX;
1808 if (!backref->found_back_ref)
1809 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1810 if (!backref->found_forward_ref)
1811 backref->errors |= REF_ERR_NO_ROOT_REF;
1812 if (backref->reachable && backref->errors)
1819 fprintf(stderr, "fs tree %llu refs %u %s\n",
1820 (unsigned long long)rec->objectid, rec->found_ref,
1821 rec->found_root_item ? "" : "not found");
1823 list_for_each_entry(backref, &rec->backrefs, list) {
1824 if (!backref->reachable)
1826 if (!backref->errors && rec->found_root_item)
1828 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1829 " index %llu namelen %u name %s error %x\n",
1830 (unsigned long long)backref->ref_root,
1831 (unsigned long long)backref->dir,
1832 (unsigned long long)backref->index,
1833 backref->namelen, backref->name,
1837 return errors > 0 ? 1 : 0;
1840 static int process_root_ref(struct extent_buffer *eb, int slot,
1841 struct btrfs_key *key,
1842 struct cache_tree *root_cache)
1848 struct btrfs_root_ref *ref;
1849 char namebuf[BTRFS_NAME_LEN];
1852 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1854 dirid = btrfs_root_ref_dirid(eb, ref);
1855 index = btrfs_root_ref_sequence(eb, ref);
1856 name_len = btrfs_root_ref_name_len(eb, ref);
1858 if (name_len <= BTRFS_NAME_LEN) {
1862 len = BTRFS_NAME_LEN;
1863 error = REF_ERR_NAME_TOO_LONG;
1865 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1867 if (key->type == BTRFS_ROOT_REF_KEY) {
1868 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1869 index, namebuf, len, key->type, error);
1871 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1872 index, namebuf, len, key->type, error);
1877 static int check_fs_root(struct btrfs_root *root,
1878 struct cache_tree *root_cache,
1879 struct walk_control *wc)
1884 struct btrfs_path path;
1885 struct shared_node root_node;
1886 struct root_record *rec;
1887 struct btrfs_root_item *root_item = &root->root_item;
1889 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1890 rec = get_root_rec(root_cache, root->root_key.objectid);
1891 if (btrfs_root_refs(root_item) > 0)
1892 rec->found_root_item = 1;
1895 btrfs_init_path(&path);
1896 memset(&root_node, 0, sizeof(root_node));
1897 cache_tree_init(&root_node.root_cache);
1898 cache_tree_init(&root_node.inode_cache);
1900 level = btrfs_header_level(root->node);
1901 memset(wc->nodes, 0, sizeof(wc->nodes));
1902 wc->nodes[level] = &root_node;
1903 wc->active_node = level;
1904 wc->root_level = level;
1906 if (btrfs_root_refs(root_item) > 0 ||
1907 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1908 path.nodes[level] = root->node;
1909 extent_buffer_get(root->node);
1910 path.slots[level] = 0;
1912 struct btrfs_key key;
1913 struct btrfs_disk_key found_key;
1915 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1916 level = root_item->drop_level;
1917 path.lowest_level = level;
1918 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1920 btrfs_node_key(path.nodes[level], &found_key,
1922 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1923 sizeof(found_key)));
1927 wret = walk_down_tree(root, &path, wc, &level);
1933 wret = walk_up_tree(root, &path, wc, &level);
1939 btrfs_release_path(&path);
1941 merge_root_recs(root, &root_node.root_cache, root_cache);
1943 if (root_node.current) {
1944 root_node.current->checked = 1;
1945 maybe_free_inode_rec(&root_node.inode_cache,
1949 ret = check_inode_recs(root, &root_node.inode_cache);
1953 static int fs_root_objectid(u64 objectid)
1955 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1956 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1957 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1958 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1959 objectid <= BTRFS_LAST_FREE_OBJECTID))
1964 static int check_fs_roots(struct btrfs_root *root,
1965 struct cache_tree *root_cache)
1967 struct btrfs_path path;
1968 struct btrfs_key key;
1969 struct walk_control wc;
1970 struct extent_buffer *leaf;
1971 struct btrfs_root *tmp_root;
1972 struct btrfs_root *tree_root = root->fs_info->tree_root;
1977 * Just in case we made any changes to the extent tree that weren't
1978 * reflected into the free space cache yet.
1981 reset_cached_block_groups(root->fs_info);
1982 memset(&wc, 0, sizeof(wc));
1983 cache_tree_init(&wc.shared);
1984 btrfs_init_path(&path);
1988 key.type = BTRFS_ROOT_ITEM_KEY;
1989 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1992 leaf = path.nodes[0];
1993 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1994 ret = btrfs_next_leaf(tree_root, &path);
1997 leaf = path.nodes[0];
1999 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2000 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2001 fs_root_objectid(key.objectid)) {
2002 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
2004 if (IS_ERR(tmp_root)) {
2008 ret = check_fs_root(tmp_root, root_cache, &wc);
2011 btrfs_free_fs_root(tmp_root);
2012 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2013 key.type == BTRFS_ROOT_BACKREF_KEY) {
2014 process_root_ref(leaf, path.slots[0], &key,
2020 btrfs_release_path(&path);
2022 if (!cache_tree_empty(&wc.shared))
2023 fprintf(stderr, "warning line %d\n", __LINE__);
2028 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2030 struct list_head *cur = rec->backrefs.next;
2031 struct extent_backref *back;
2032 struct tree_backref *tback;
2033 struct data_backref *dback;
2037 while(cur != &rec->backrefs) {
2038 back = list_entry(cur, struct extent_backref, list);
2040 if (!back->found_extent_tree) {
2044 if (back->is_data) {
2045 dback = (struct data_backref *)back;
2046 fprintf(stderr, "Backref %llu %s %llu"
2047 " owner %llu offset %llu num_refs %lu"
2048 " not found in extent tree\n",
2049 (unsigned long long)rec->start,
2050 back->full_backref ?
2052 back->full_backref ?
2053 (unsigned long long)dback->parent:
2054 (unsigned long long)dback->root,
2055 (unsigned long long)dback->owner,
2056 (unsigned long long)dback->offset,
2057 (unsigned long)dback->num_refs);
2059 tback = (struct tree_backref *)back;
2060 fprintf(stderr, "Backref %llu parent %llu"
2061 " root %llu not found in extent tree\n",
2062 (unsigned long long)rec->start,
2063 (unsigned long long)tback->parent,
2064 (unsigned long long)tback->root);
2067 if (!back->is_data && !back->found_ref) {
2071 tback = (struct tree_backref *)back;
2072 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2073 (unsigned long long)rec->start,
2074 back->full_backref ? "parent" : "root",
2075 back->full_backref ?
2076 (unsigned long long)tback->parent :
2077 (unsigned long long)tback->root, back);
2079 if (back->is_data) {
2080 dback = (struct data_backref *)back;
2081 if (dback->found_ref != dback->num_refs) {
2085 fprintf(stderr, "Incorrect local backref count"
2086 " on %llu %s %llu owner %llu"
2087 " offset %llu found %u wanted %u back %p\n",
2088 (unsigned long long)rec->start,
2089 back->full_backref ?
2091 back->full_backref ?
2092 (unsigned long long)dback->parent:
2093 (unsigned long long)dback->root,
2094 (unsigned long long)dback->owner,
2095 (unsigned long long)dback->offset,
2096 dback->found_ref, dback->num_refs, back);
2098 if (dback->disk_bytenr != rec->start) {
2102 fprintf(stderr, "Backref disk bytenr does not"
2103 " match extent record, bytenr=%llu, "
2104 "ref bytenr=%llu\n",
2105 (unsigned long long)rec->start,
2106 (unsigned long long)dback->disk_bytenr);
2109 if (dback->bytes != rec->nr) {
2113 fprintf(stderr, "Backref bytes do not match "
2114 "extent backref, bytenr=%llu, ref "
2115 "bytes=%llu, backref bytes=%llu\n",
2116 (unsigned long long)rec->start,
2117 (unsigned long long)rec->nr,
2118 (unsigned long long)dback->bytes);
2121 if (!back->is_data) {
2124 dback = (struct data_backref *)back;
2125 found += dback->found_ref;
2128 if (found != rec->refs) {
2132 fprintf(stderr, "Incorrect global backref count "
2133 "on %llu found %llu wanted %llu\n",
2134 (unsigned long long)rec->start,
2135 (unsigned long long)found,
2136 (unsigned long long)rec->refs);
2142 static int free_all_extent_backrefs(struct extent_record *rec)
2144 struct extent_backref *back;
2145 struct list_head *cur;
2146 while (!list_empty(&rec->backrefs)) {
2147 cur = rec->backrefs.next;
2148 back = list_entry(cur, struct extent_backref, list);
2155 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2156 struct cache_tree *extent_cache)
2158 struct cache_extent *cache;
2159 struct extent_record *rec;
2162 cache = first_cache_extent(extent_cache);
2165 rec = container_of(cache, struct extent_record, cache);
2166 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2167 remove_cache_extent(extent_cache, cache);
2168 free_all_extent_backrefs(rec);
2173 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2174 struct extent_record *rec)
2176 if (rec->content_checked && rec->owner_ref_checked &&
2177 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2178 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2179 remove_cache_extent(extent_cache, &rec->cache);
2180 free_all_extent_backrefs(rec);
2181 list_del_init(&rec->list);
2187 static int check_owner_ref(struct btrfs_root *root,
2188 struct extent_record *rec,
2189 struct extent_buffer *buf)
2191 struct extent_backref *node;
2192 struct tree_backref *back;
2193 struct btrfs_root *ref_root;
2194 struct btrfs_key key;
2195 struct btrfs_path path;
2196 struct extent_buffer *parent;
2201 list_for_each_entry(node, &rec->backrefs, list) {
2204 if (!node->found_ref)
2206 if (node->full_backref)
2208 back = (struct tree_backref *)node;
2209 if (btrfs_header_owner(buf) == back->root)
2212 BUG_ON(rec->is_root);
2214 /* try to find the block by search corresponding fs tree */
2215 key.objectid = btrfs_header_owner(buf);
2216 key.type = BTRFS_ROOT_ITEM_KEY;
2217 key.offset = (u64)-1;
2219 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2220 if (IS_ERR(ref_root))
2223 level = btrfs_header_level(buf);
2225 btrfs_item_key_to_cpu(buf, &key, 0);
2227 btrfs_node_key_to_cpu(buf, &key, 0);
2229 btrfs_init_path(&path);
2230 path.lowest_level = level + 1;
2231 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2235 parent = path.nodes[level + 1];
2236 if (parent && buf->start == btrfs_node_blockptr(parent,
2237 path.slots[level + 1]))
2240 btrfs_release_path(&path);
2241 return found ? 0 : 1;
2244 static int is_extent_tree_record(struct extent_record *rec)
2246 struct list_head *cur = rec->backrefs.next;
2247 struct extent_backref *node;
2248 struct tree_backref *back;
2251 while(cur != &rec->backrefs) {
2252 node = list_entry(cur, struct extent_backref, list);
2256 back = (struct tree_backref *)node;
2257 if (node->full_backref)
2259 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2266 static int record_bad_block_io(struct btrfs_fs_info *info,
2267 struct cache_tree *extent_cache,
2270 struct extent_record *rec;
2271 struct cache_extent *cache;
2272 struct btrfs_key key;
2274 cache = lookup_cache_extent(extent_cache, start, len);
2278 rec = container_of(cache, struct extent_record, cache);
2279 if (!is_extent_tree_record(rec))
2282 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2283 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2286 static int check_block(struct btrfs_root *root,
2287 struct cache_tree *extent_cache,
2288 struct extent_buffer *buf, u64 flags)
2290 struct extent_record *rec;
2291 struct cache_extent *cache;
2292 struct btrfs_key key;
2296 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2299 rec = container_of(cache, struct extent_record, cache);
2300 rec->generation = btrfs_header_generation(buf);
2302 level = btrfs_header_level(buf);
2303 if (btrfs_header_nritems(buf) > 0) {
2306 btrfs_item_key_to_cpu(buf, &key, 0);
2308 btrfs_node_key_to_cpu(buf, &key, 0);
2310 rec->info_objectid = key.objectid;
2312 rec->info_level = level;
2314 if (btrfs_is_leaf(buf))
2315 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2317 ret = btrfs_check_node(root, &rec->parent_key, buf);
2320 fprintf(stderr, "bad block %llu\n",
2321 (unsigned long long)buf->start);
2323 rec->content_checked = 1;
2324 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2325 rec->owner_ref_checked = 1;
2327 ret = check_owner_ref(root, rec, buf);
2329 rec->owner_ref_checked = 1;
2333 maybe_free_extent_rec(extent_cache, rec);
2337 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2338 u64 parent, u64 root)
2340 struct list_head *cur = rec->backrefs.next;
2341 struct extent_backref *node;
2342 struct tree_backref *back;
2344 while(cur != &rec->backrefs) {
2345 node = list_entry(cur, struct extent_backref, list);
2349 back = (struct tree_backref *)node;
2351 if (!node->full_backref)
2353 if (parent == back->parent)
2356 if (node->full_backref)
2358 if (back->root == root)
2365 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2366 u64 parent, u64 root)
2368 struct tree_backref *ref = malloc(sizeof(*ref));
2369 memset(&ref->node, 0, sizeof(ref->node));
2371 ref->parent = parent;
2372 ref->node.full_backref = 1;
2375 ref->node.full_backref = 0;
2377 list_add_tail(&ref->node.list, &rec->backrefs);
2382 static struct data_backref *find_data_backref(struct extent_record *rec,
2383 u64 parent, u64 root,
2384 u64 owner, u64 offset,
2386 u64 disk_bytenr, u64 bytes)
2388 struct list_head *cur = rec->backrefs.next;
2389 struct extent_backref *node;
2390 struct data_backref *back;
2392 while(cur != &rec->backrefs) {
2393 node = list_entry(cur, struct extent_backref, list);
2397 back = (struct data_backref *)node;
2399 if (!node->full_backref)
2401 if (parent == back->parent)
2404 if (node->full_backref)
2406 if (back->root == root && back->owner == owner &&
2407 back->offset == offset) {
2408 if (found_ref && node->found_ref &&
2409 (back->bytes != bytes ||
2410 back->disk_bytenr != disk_bytenr))
2419 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2420 u64 parent, u64 root,
2421 u64 owner, u64 offset,
2424 struct data_backref *ref = malloc(sizeof(*ref));
2425 memset(&ref->node, 0, sizeof(ref->node));
2426 ref->node.is_data = 1;
2429 ref->parent = parent;
2432 ref->node.full_backref = 1;
2436 ref->offset = offset;
2437 ref->node.full_backref = 0;
2439 ref->bytes = max_size;
2442 list_add_tail(&ref->node.list, &rec->backrefs);
2443 if (max_size > rec->max_size)
2444 rec->max_size = max_size;
2448 static int add_extent_rec(struct cache_tree *extent_cache,
2449 struct btrfs_key *parent_key,
2450 u64 start, u64 nr, u64 extent_item_refs,
2451 int is_root, int inc_ref, int set_checked,
2452 int metadata, int extent_rec, u64 max_size)
2454 struct extent_record *rec;
2455 struct cache_extent *cache;
2459 cache = lookup_cache_extent(extent_cache, start, nr);
2461 rec = container_of(cache, struct extent_record, cache);
2465 rec->nr = max(nr, max_size);
2468 * We need to make sure to reset nr to whatever the extent
2469 * record says was the real size, this way we can compare it to
2473 if (start != rec->start || rec->found_rec) {
2474 struct extent_record *tmp;
2477 if (list_empty(&rec->list))
2478 list_add_tail(&rec->list,
2479 &duplicate_extents);
2482 * We have to do this song and dance in case we
2483 * find an extent record that falls inside of
2484 * our current extent record but does not have
2485 * the same objectid.
2487 tmp = malloc(sizeof(*tmp));
2491 tmp->max_size = max_size;
2494 tmp->metadata = metadata;
2495 tmp->extent_item_refs = extent_item_refs;
2496 INIT_LIST_HEAD(&tmp->list);
2497 list_add_tail(&tmp->list, &rec->dups);
2498 rec->num_duplicates++;
2505 if (extent_item_refs && !dup) {
2506 if (rec->extent_item_refs) {
2507 fprintf(stderr, "block %llu rec "
2508 "extent_item_refs %llu, passed %llu\n",
2509 (unsigned long long)start,
2510 (unsigned long long)
2511 rec->extent_item_refs,
2512 (unsigned long long)extent_item_refs);
2514 rec->extent_item_refs = extent_item_refs;
2519 rec->content_checked = 1;
2520 rec->owner_ref_checked = 1;
2524 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2526 if (rec->max_size < max_size)
2527 rec->max_size = max_size;
2529 maybe_free_extent_rec(extent_cache, rec);
2532 rec = malloc(sizeof(*rec));
2534 rec->max_size = max_size;
2535 rec->nr = max(nr, max_size);
2536 rec->found_rec = extent_rec;
2537 rec->content_checked = 0;
2538 rec->owner_ref_checked = 0;
2539 rec->num_duplicates = 0;
2540 rec->metadata = metadata;
2541 INIT_LIST_HEAD(&rec->backrefs);
2542 INIT_LIST_HEAD(&rec->dups);
2543 INIT_LIST_HEAD(&rec->list);
2555 if (extent_item_refs)
2556 rec->extent_item_refs = extent_item_refs;
2558 rec->extent_item_refs = 0;
2561 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2563 memset(&rec->parent_key, 0, sizeof(*parent_key));
2565 rec->cache.start = start;
2566 rec->cache.size = nr;
2567 ret = insert_cache_extent(extent_cache, &rec->cache);
2571 rec->content_checked = 1;
2572 rec->owner_ref_checked = 1;
2577 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2578 u64 parent, u64 root, int found_ref)
2580 struct extent_record *rec;
2581 struct tree_backref *back;
2582 struct cache_extent *cache;
2584 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2586 add_extent_rec(extent_cache, NULL, bytenr,
2587 1, 0, 0, 0, 0, 1, 0, 0);
2588 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2593 rec = container_of(cache, struct extent_record, cache);
2594 if (rec->start != bytenr) {
2598 back = find_tree_backref(rec, parent, root);
2600 back = alloc_tree_backref(rec, parent, root);
2603 if (back->node.found_ref) {
2604 fprintf(stderr, "Extent back ref already exists "
2605 "for %llu parent %llu root %llu \n",
2606 (unsigned long long)bytenr,
2607 (unsigned long long)parent,
2608 (unsigned long long)root);
2610 back->node.found_ref = 1;
2612 if (back->node.found_extent_tree) {
2613 fprintf(stderr, "Extent back ref already exists "
2614 "for %llu parent %llu root %llu \n",
2615 (unsigned long long)bytenr,
2616 (unsigned long long)parent,
2617 (unsigned long long)root);
2619 back->node.found_extent_tree = 1;
2624 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2625 u64 parent, u64 root, u64 owner, u64 offset,
2626 u32 num_refs, int found_ref, u64 max_size)
2628 struct extent_record *rec;
2629 struct data_backref *back;
2630 struct cache_extent *cache;
2632 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2634 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2636 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2641 rec = container_of(cache, struct extent_record, cache);
2642 if (rec->max_size < max_size)
2643 rec->max_size = max_size;
2646 * If found_ref is set then max_size is the real size and must match the
2647 * existing refs. So if we have already found a ref then we need to
2648 * make sure that this ref matches the existing one, otherwise we need
2649 * to add a new backref so we can notice that the backrefs don't match
2650 * and we need to figure out who is telling the truth. This is to
2651 * account for that awful fsync bug I introduced where we'd end up with
2652 * a btrfs_file_extent_item that would have its length include multiple
2653 * prealloc extents or point inside of a prealloc extent.
2655 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2658 back = alloc_data_backref(rec, parent, root, owner, offset,
2662 BUG_ON(num_refs != 1);
2663 if (back->node.found_ref)
2664 BUG_ON(back->bytes != max_size);
2665 back->node.found_ref = 1;
2666 back->found_ref += 1;
2667 back->bytes = max_size;
2668 back->disk_bytenr = bytenr;
2670 rec->content_checked = 1;
2671 rec->owner_ref_checked = 1;
2673 if (back->node.found_extent_tree) {
2674 fprintf(stderr, "Extent back ref already exists "
2675 "for %llu parent %llu root %llu"
2676 "owner %llu offset %llu num_refs %lu\n",
2677 (unsigned long long)bytenr,
2678 (unsigned long long)parent,
2679 (unsigned long long)root,
2680 (unsigned long long)owner,
2681 (unsigned long long)offset,
2682 (unsigned long)num_refs);
2684 back->num_refs = num_refs;
2685 back->node.found_extent_tree = 1;
2690 static int add_pending(struct cache_tree *pending,
2691 struct cache_tree *seen, u64 bytenr, u32 size)
2694 ret = add_cache_extent(seen, bytenr, size);
2697 add_cache_extent(pending, bytenr, size);
2701 static int pick_next_pending(struct cache_tree *pending,
2702 struct cache_tree *reada,
2703 struct cache_tree *nodes,
2704 u64 last, struct block_info *bits, int bits_nr,
2707 unsigned long node_start = last;
2708 struct cache_extent *cache;
2711 cache = search_cache_extent(reada, 0);
2713 bits[0].start = cache->start;
2714 bits[1].size = cache->size;
2719 if (node_start > 32768)
2720 node_start -= 32768;
2722 cache = search_cache_extent(nodes, node_start);
2724 cache = search_cache_extent(nodes, 0);
2727 cache = search_cache_extent(pending, 0);
2732 bits[ret].start = cache->start;
2733 bits[ret].size = cache->size;
2734 cache = next_cache_extent(cache);
2736 } while (cache && ret < bits_nr);
2742 bits[ret].start = cache->start;
2743 bits[ret].size = cache->size;
2744 cache = next_cache_extent(cache);
2746 } while (cache && ret < bits_nr);
2748 if (bits_nr - ret > 8) {
2749 u64 lookup = bits[0].start + bits[0].size;
2750 struct cache_extent *next;
2751 next = search_cache_extent(pending, lookup);
2753 if (next->start - lookup > 32768)
2755 bits[ret].start = next->start;
2756 bits[ret].size = next->size;
2757 lookup = next->start + next->size;
2761 next = next_cache_extent(next);
2769 static void free_chunk_record(struct cache_extent *cache)
2771 struct chunk_record *rec;
2773 rec = container_of(cache, struct chunk_record, cache);
2777 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
2779 cache_tree_free_extents(chunk_cache, free_chunk_record);
2782 static void free_device_record(struct rb_node *node)
2784 struct device_record *rec;
2786 rec = container_of(node, struct device_record, node);
2790 FREE_RB_BASED_TREE(device_cache, free_device_record);
2792 int insert_block_group_record(struct block_group_tree *tree,
2793 struct block_group_record *bg_rec)
2797 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
2801 list_add_tail(&bg_rec->list, &tree->block_groups);
2805 static void free_block_group_record(struct cache_extent *cache)
2807 struct block_group_record *rec;
2809 rec = container_of(cache, struct block_group_record, cache);
2813 void free_block_group_tree(struct block_group_tree *tree)
2815 cache_tree_free_extents(&tree->tree, free_block_group_record);
2818 int insert_device_extent_record(struct device_extent_tree *tree,
2819 struct device_extent_record *de_rec)
2824 * Device extent is a bit different from the other extents, because
2825 * the extents which belong to the different devices may have the
2826 * same start and size, so we need use the special extent cache
2827 * search/insert functions.
2829 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
2833 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
2834 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
2838 static void free_device_extent_record(struct cache_extent *cache)
2840 struct device_extent_record *rec;
2842 rec = container_of(cache, struct device_extent_record, cache);
2846 void free_device_extent_tree(struct device_extent_tree *tree)
2848 cache_tree_free_extents(&tree->tree, free_device_extent_record);
2851 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2852 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2853 struct extent_buffer *leaf, int slot)
2855 struct btrfs_extent_ref_v0 *ref0;
2856 struct btrfs_key key;
2858 btrfs_item_key_to_cpu(leaf, &key, slot);
2859 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2860 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2861 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2863 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2864 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2870 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
2871 struct btrfs_key *key,
2874 struct btrfs_chunk *ptr;
2875 struct chunk_record *rec;
2878 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2879 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
2881 rec = malloc(btrfs_chunk_record_size(num_stripes));
2883 fprintf(stderr, "memory allocation failed\n");
2887 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
2889 INIT_LIST_HEAD(&rec->list);
2890 INIT_LIST_HEAD(&rec->dextents);
2893 rec->cache.start = key->offset;
2894 rec->cache.size = btrfs_chunk_length(leaf, ptr);
2896 rec->generation = btrfs_header_generation(leaf);
2898 rec->objectid = key->objectid;
2899 rec->type = key->type;
2900 rec->offset = key->offset;
2902 rec->length = rec->cache.size;
2903 rec->owner = btrfs_chunk_owner(leaf, ptr);
2904 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
2905 rec->type_flags = btrfs_chunk_type(leaf, ptr);
2906 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
2907 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
2908 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
2909 rec->num_stripes = num_stripes;
2910 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
2912 for (i = 0; i < rec->num_stripes; ++i) {
2913 rec->stripes[i].devid =
2914 btrfs_stripe_devid_nr(leaf, ptr, i);
2915 rec->stripes[i].offset =
2916 btrfs_stripe_offset_nr(leaf, ptr, i);
2917 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
2918 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
2925 static int process_chunk_item(struct cache_tree *chunk_cache,
2926 struct btrfs_key *key, struct extent_buffer *eb,
2929 struct chunk_record *rec;
2932 rec = btrfs_new_chunk_record(eb, key, slot);
2933 ret = insert_cache_extent(chunk_cache, &rec->cache);
2935 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
2936 rec->offset, rec->length);
2943 static int process_device_item(struct rb_root *dev_cache,
2944 struct btrfs_key *key, struct extent_buffer *eb, int slot)
2946 struct btrfs_dev_item *ptr;
2947 struct device_record *rec;
2950 ptr = btrfs_item_ptr(eb,
2951 slot, struct btrfs_dev_item);
2953 rec = malloc(sizeof(*rec));
2955 fprintf(stderr, "memory allocation failed\n");
2959 rec->devid = key->offset;
2960 rec->generation = btrfs_header_generation(eb);
2962 rec->objectid = key->objectid;
2963 rec->type = key->type;
2964 rec->offset = key->offset;
2966 rec->devid = btrfs_device_id(eb, ptr);
2967 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
2968 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
2970 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
2972 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
2979 struct block_group_record *
2980 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
2983 struct btrfs_block_group_item *ptr;
2984 struct block_group_record *rec;
2986 rec = malloc(sizeof(*rec));
2988 fprintf(stderr, "memory allocation failed\n");
2991 memset(rec, 0, sizeof(*rec));
2993 rec->cache.start = key->objectid;
2994 rec->cache.size = key->offset;
2996 rec->generation = btrfs_header_generation(leaf);
2998 rec->objectid = key->objectid;
2999 rec->type = key->type;
3000 rec->offset = key->offset;
3002 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3003 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3005 INIT_LIST_HEAD(&rec->list);
3010 static int process_block_group_item(struct block_group_tree *block_group_cache,
3011 struct btrfs_key *key,
3012 struct extent_buffer *eb, int slot)
3014 struct block_group_record *rec;
3017 rec = btrfs_new_block_group_record(eb, key, slot);
3018 ret = insert_block_group_record(block_group_cache, rec);
3020 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3021 rec->objectid, rec->offset);
3028 struct device_extent_record *
3029 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3030 struct btrfs_key *key, int slot)
3032 struct device_extent_record *rec;
3033 struct btrfs_dev_extent *ptr;
3035 rec = malloc(sizeof(*rec));
3037 fprintf(stderr, "memory allocation failed\n");
3040 memset(rec, 0, sizeof(*rec));
3042 rec->cache.objectid = key->objectid;
3043 rec->cache.start = key->offset;
3045 rec->generation = btrfs_header_generation(leaf);
3047 rec->objectid = key->objectid;
3048 rec->type = key->type;
3049 rec->offset = key->offset;
3051 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3052 rec->chunk_objecteid =
3053 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3055 btrfs_dev_extent_chunk_offset(leaf, ptr);
3056 rec->length = btrfs_dev_extent_length(leaf, ptr);
3057 rec->cache.size = rec->length;
3059 INIT_LIST_HEAD(&rec->chunk_list);
3060 INIT_LIST_HEAD(&rec->device_list);
3066 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3067 struct btrfs_key *key, struct extent_buffer *eb,
3070 struct device_extent_record *rec;
3073 rec = btrfs_new_device_extent_record(eb, key, slot);
3074 ret = insert_device_extent_record(dev_extent_cache, rec);
3077 "Device extent[%llu, %llu, %llu] existed.\n",
3078 rec->objectid, rec->offset, rec->length);
3085 static int process_extent_item(struct btrfs_root *root,
3086 struct cache_tree *extent_cache,
3087 struct extent_buffer *eb, int slot)
3089 struct btrfs_extent_item *ei;
3090 struct btrfs_extent_inline_ref *iref;
3091 struct btrfs_extent_data_ref *dref;
3092 struct btrfs_shared_data_ref *sref;
3093 struct btrfs_key key;
3097 u32 item_size = btrfs_item_size_nr(eb, slot);
3103 btrfs_item_key_to_cpu(eb, &key, slot);
3105 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3107 num_bytes = root->leafsize;
3109 num_bytes = key.offset;
3112 if (item_size < sizeof(*ei)) {
3113 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3114 struct btrfs_extent_item_v0 *ei0;
3115 BUG_ON(item_size != sizeof(*ei0));
3116 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3117 refs = btrfs_extent_refs_v0(eb, ei0);
3121 return add_extent_rec(extent_cache, NULL, key.objectid,
3122 num_bytes, refs, 0, 0, 0, metadata, 1,
3126 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3127 refs = btrfs_extent_refs(eb, ei);
3129 add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
3130 refs, 0, 0, 0, metadata, 1, num_bytes);
3132 ptr = (unsigned long)(ei + 1);
3133 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3134 key.type == BTRFS_EXTENT_ITEM_KEY)
3135 ptr += sizeof(struct btrfs_tree_block_info);
3137 end = (unsigned long)ei + item_size;
3139 iref = (struct btrfs_extent_inline_ref *)ptr;
3140 type = btrfs_extent_inline_ref_type(eb, iref);
3141 offset = btrfs_extent_inline_ref_offset(eb, iref);
3143 case BTRFS_TREE_BLOCK_REF_KEY:
3144 add_tree_backref(extent_cache, key.objectid,
3147 case BTRFS_SHARED_BLOCK_REF_KEY:
3148 add_tree_backref(extent_cache, key.objectid,
3151 case BTRFS_EXTENT_DATA_REF_KEY:
3152 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3153 add_data_backref(extent_cache, key.objectid, 0,
3154 btrfs_extent_data_ref_root(eb, dref),
3155 btrfs_extent_data_ref_objectid(eb,
3157 btrfs_extent_data_ref_offset(eb, dref),
3158 btrfs_extent_data_ref_count(eb, dref),
3161 case BTRFS_SHARED_DATA_REF_KEY:
3162 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3163 add_data_backref(extent_cache, key.objectid, offset,
3165 btrfs_shared_data_ref_count(eb, sref),
3169 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3170 key.objectid, key.type, num_bytes);
3173 ptr += btrfs_extent_inline_ref_size(type);
3180 static int check_cache_range(struct btrfs_root *root,
3181 struct btrfs_block_group_cache *cache,
3182 u64 offset, u64 bytes)
3184 struct btrfs_free_space *entry;
3190 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3191 bytenr = btrfs_sb_offset(i);
3192 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3193 cache->key.objectid, bytenr, 0,
3194 &logical, &nr, &stripe_len);
3199 if (logical[nr] + stripe_len <= offset)
3201 if (offset + bytes <= logical[nr])
3203 if (logical[nr] == offset) {
3204 if (stripe_len >= bytes) {
3208 bytes -= stripe_len;
3209 offset += stripe_len;
3210 } else if (logical[nr] < offset) {
3211 if (logical[nr] + stripe_len >=
3216 bytes = (offset + bytes) -
3217 (logical[nr] + stripe_len);
3218 offset = logical[nr] + stripe_len;
3221 * Could be tricky, the super may land in the
3222 * middle of the area we're checking. First
3223 * check the easiest case, it's at the end.
3225 if (logical[nr] + stripe_len >=
3227 bytes = logical[nr] - offset;
3231 /* Check the left side */
3232 ret = check_cache_range(root, cache,
3234 logical[nr] - offset);
3240 /* Now we continue with the right side */
3241 bytes = (offset + bytes) -
3242 (logical[nr] + stripe_len);
3243 offset = logical[nr] + stripe_len;
3250 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3252 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3253 offset, offset+bytes);
3257 if (entry->offset != offset) {
3258 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3263 if (entry->bytes != bytes) {
3264 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3265 bytes, entry->bytes, offset);
3269 unlink_free_space(cache->free_space_ctl, entry);
3274 static int verify_space_cache(struct btrfs_root *root,
3275 struct btrfs_block_group_cache *cache)
3277 struct btrfs_path *path;
3278 struct extent_buffer *leaf;
3279 struct btrfs_key key;
3283 path = btrfs_alloc_path();
3287 root = root->fs_info->extent_root;
3289 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
3291 key.objectid = last;
3293 key.type = BTRFS_EXTENT_ITEM_KEY;
3295 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3300 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3301 ret = btrfs_next_leaf(root, path);
3309 leaf = path->nodes[0];
3310 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3311 if (key.objectid >= cache->key.offset + cache->key.objectid)
3313 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3314 key.type != BTRFS_METADATA_ITEM_KEY) {
3319 if (last == key.objectid) {
3320 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3321 last = key.objectid + key.offset;
3323 last = key.objectid + root->leafsize;
3328 ret = check_cache_range(root, cache, last,
3329 key.objectid - last);
3332 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3333 last = key.objectid + key.offset;
3335 last = key.objectid + root->leafsize;
3339 if (last < cache->key.objectid + cache->key.offset)
3340 ret = check_cache_range(root, cache, last,
3341 cache->key.objectid +
3342 cache->key.offset - last);
3345 btrfs_free_path(path);
3348 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
3349 fprintf(stderr, "There are still entries left in the space "
3357 static int check_space_cache(struct btrfs_root *root)
3359 struct btrfs_block_group_cache *cache;
3360 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
3364 if (btrfs_super_generation(root->fs_info->super_copy) !=
3365 btrfs_super_cache_generation(root->fs_info->super_copy)) {
3366 printf("cache and super generation don't match, space cache "
3367 "will be invalidated\n");
3372 cache = btrfs_lookup_first_block_group(root->fs_info, start);
3376 start = cache->key.objectid + cache->key.offset;
3377 if (!cache->free_space_ctl) {
3378 if (btrfs_init_free_space_ctl(cache,
3379 root->sectorsize)) {
3384 btrfs_remove_free_space_cache(cache);
3387 ret = load_free_space_cache(root->fs_info, cache);
3391 ret = verify_space_cache(root, cache);
3393 fprintf(stderr, "cache appears valid but isnt %Lu\n",
3394 cache->key.objectid);
3399 return error ? -EINVAL : 0;
3402 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
3405 struct btrfs_path *path;
3406 struct extent_buffer *leaf;
3407 struct btrfs_key key;
3410 path = btrfs_alloc_path();
3412 fprintf(stderr, "Error allocing path\n");
3416 key.objectid = bytenr;
3417 key.type = BTRFS_EXTENT_ITEM_KEY;
3422 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
3425 fprintf(stderr, "Error looking up extent record %d\n", ret);
3426 btrfs_free_path(path);
3432 btrfs_prev_leaf(root, path);
3435 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3438 * Block group items come before extent items if they have the same
3439 * bytenr, so walk back one more just in case. Dear future traveler,
3440 * first congrats on mastering time travel. Now if it's not too much
3441 * trouble could you go back to 2006 and tell Chris to make the
3442 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
3444 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3448 btrfs_prev_leaf(root, path);
3452 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3453 ret = btrfs_next_leaf(root, path);
3455 fprintf(stderr, "Error going to next leaf "
3457 btrfs_free_path(path);
3463 leaf = path->nodes[0];
3464 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3465 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3469 if (key.objectid + key.offset < bytenr) {
3473 if (key.objectid > bytenr + num_bytes)
3476 if (key.objectid == bytenr) {
3477 if (key.offset >= num_bytes) {
3481 num_bytes -= key.offset;
3482 bytenr += key.offset;
3483 } else if (key.objectid < bytenr) {
3484 if (key.objectid + key.offset >= bytenr + num_bytes) {
3488 num_bytes = (bytenr + num_bytes) -
3489 (key.objectid + key.offset);
3490 bytenr = key.objectid + key.offset;
3492 if (key.objectid + key.offset < bytenr + num_bytes) {
3493 u64 new_start = key.objectid + key.offset;
3494 u64 new_bytes = bytenr + num_bytes - new_start;
3497 * Weird case, the extent is in the middle of
3498 * our range, we'll have to search one side
3499 * and then the other. Not sure if this happens
3500 * in real life, but no harm in coding it up
3501 * anyway just in case.
3503 btrfs_release_path(path);
3504 ret = check_extent_exists(root, new_start,
3507 fprintf(stderr, "Right section didn't "
3511 num_bytes = key.objectid - bytenr;
3514 num_bytes = key.objectid - bytenr;
3521 fprintf(stderr, "There are no extents for csum range "
3522 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3526 btrfs_free_path(path);
3530 static int check_csums(struct btrfs_root *root)
3532 struct btrfs_path *path;
3533 struct extent_buffer *leaf;
3534 struct btrfs_key key;
3535 u64 offset = 0, num_bytes = 0;
3536 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3540 root = root->fs_info->csum_root;
3542 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3543 key.type = BTRFS_EXTENT_CSUM_KEY;
3546 path = btrfs_alloc_path();
3550 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3552 fprintf(stderr, "Error searching csum tree %d\n", ret);
3553 btrfs_free_path(path);
3557 if (ret > 0 && path->slots[0])
3562 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3563 ret = btrfs_next_leaf(root, path);
3565 fprintf(stderr, "Error going to next leaf "
3572 leaf = path->nodes[0];
3574 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3575 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3581 offset = key.offset;
3582 } else if (key.offset != offset + num_bytes) {
3583 ret = check_extent_exists(root, offset, num_bytes);
3585 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3586 "there is no extent record\n",
3587 offset, offset+num_bytes);
3590 offset = key.offset;
3594 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3595 csum_size) * root->sectorsize;
3599 btrfs_free_path(path);
3603 static int run_next_block(struct btrfs_root *root,
3604 struct block_info *bits,
3607 struct cache_tree *pending,
3608 struct cache_tree *seen,
3609 struct cache_tree *reada,
3610 struct cache_tree *nodes,
3611 struct cache_tree *extent_cache,
3612 struct cache_tree *chunk_cache,
3613 struct rb_root *dev_cache,
3614 struct block_group_tree *block_group_cache,
3615 struct device_extent_tree *dev_extent_cache)
3617 struct extent_buffer *buf;
3627 struct btrfs_key key;
3628 struct cache_extent *cache;
3631 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
3632 bits_nr, &reada_bits);
3637 for(i = 0; i < nritems; i++) {
3638 ret = add_cache_extent(reada, bits[i].start,
3643 /* fixme, get the parent transid */
3644 readahead_tree_block(root, bits[i].start,
3648 *last = bits[0].start;
3649 bytenr = bits[0].start;
3650 size = bits[0].size;
3652 cache = lookup_cache_extent(pending, bytenr, size);
3654 remove_cache_extent(pending, cache);
3657 cache = lookup_cache_extent(reada, bytenr, size);
3659 remove_cache_extent(reada, cache);
3662 cache = lookup_cache_extent(nodes, bytenr, size);
3664 remove_cache_extent(nodes, cache);
3667 cache = lookup_cache_extent(seen, bytenr, size);
3669 remove_cache_extent(seen, cache);
3673 /* fixme, get the real parent transid */
3674 buf = read_tree_block(root, bytenr, size, 0);
3675 if (!extent_buffer_uptodate(buf)) {
3676 record_bad_block_io(root->fs_info,
3677 extent_cache, bytenr, size);
3681 nritems = btrfs_header_nritems(buf);
3683 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3684 btrfs_header_level(buf), 1, NULL,
3687 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3689 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3694 owner = btrfs_header_owner(buf);
3697 ret = check_block(root, extent_cache, buf, flags);
3701 if (btrfs_is_leaf(buf)) {
3702 btree_space_waste += btrfs_leaf_free_space(root, buf);
3703 for (i = 0; i < nritems; i++) {
3704 struct btrfs_file_extent_item *fi;
3705 btrfs_item_key_to_cpu(buf, &key, i);
3706 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3707 process_extent_item(root, extent_cache, buf,
3711 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3712 process_extent_item(root, extent_cache, buf,
3716 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3718 btrfs_item_size_nr(buf, i);
3721 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3722 process_chunk_item(chunk_cache, &key, buf, i);
3725 if (key.type == BTRFS_DEV_ITEM_KEY) {
3726 process_device_item(dev_cache, &key, buf, i);
3729 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3730 process_block_group_item(block_group_cache,
3734 if (key.type == BTRFS_DEV_EXTENT_KEY) {
3735 process_device_extent_item(dev_extent_cache,
3740 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3741 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3742 process_extent_ref_v0(extent_cache, buf, i);
3749 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3750 add_tree_backref(extent_cache, key.objectid, 0,
3754 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3755 add_tree_backref(extent_cache, key.objectid,
3759 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3760 struct btrfs_extent_data_ref *ref;
3761 ref = btrfs_item_ptr(buf, i,
3762 struct btrfs_extent_data_ref);
3763 add_data_backref(extent_cache,
3765 btrfs_extent_data_ref_root(buf, ref),
3766 btrfs_extent_data_ref_objectid(buf,
3768 btrfs_extent_data_ref_offset(buf, ref),
3769 btrfs_extent_data_ref_count(buf, ref),
3770 0, root->sectorsize);
3773 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3774 struct btrfs_shared_data_ref *ref;
3775 ref = btrfs_item_ptr(buf, i,
3776 struct btrfs_shared_data_ref);
3777 add_data_backref(extent_cache,
3778 key.objectid, key.offset, 0, 0, 0,
3779 btrfs_shared_data_ref_count(buf, ref),
3780 0, root->sectorsize);
3783 if (key.type != BTRFS_EXTENT_DATA_KEY)
3785 fi = btrfs_item_ptr(buf, i,
3786 struct btrfs_file_extent_item);
3787 if (btrfs_file_extent_type(buf, fi) ==
3788 BTRFS_FILE_EXTENT_INLINE)
3790 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3793 data_bytes_allocated +=
3794 btrfs_file_extent_disk_num_bytes(buf, fi);
3795 if (data_bytes_allocated < root->sectorsize) {
3798 data_bytes_referenced +=
3799 btrfs_file_extent_num_bytes(buf, fi);
3800 add_data_backref(extent_cache,
3801 btrfs_file_extent_disk_bytenr(buf, fi),
3802 parent, owner, key.objectid, key.offset -
3803 btrfs_file_extent_offset(buf, fi), 1, 1,
3804 btrfs_file_extent_disk_num_bytes(buf, fi));
3809 struct btrfs_key first_key;
3811 first_key.objectid = 0;
3814 btrfs_item_key_to_cpu(buf, &first_key, 0);
3815 level = btrfs_header_level(buf);
3816 for (i = 0; i < nritems; i++) {
3817 ptr = btrfs_node_blockptr(buf, i);
3818 size = btrfs_level_size(root, level - 1);
3819 btrfs_node_key_to_cpu(buf, &key, i);
3820 ret = add_extent_rec(extent_cache, &key,
3821 ptr, size, 0, 0, 1, 0, 1, 0,
3825 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3828 add_pending(nodes, seen, ptr, size);
3830 add_pending(pending, seen, ptr, size);
3833 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3834 nritems) * sizeof(struct btrfs_key_ptr);
3836 total_btree_bytes += buf->len;
3837 if (fs_root_objectid(btrfs_header_owner(buf)))
3838 total_fs_tree_bytes += buf->len;
3839 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3840 total_extent_tree_bytes += buf->len;
3841 if (!found_old_backref &&
3842 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3843 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3844 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3845 found_old_backref = 1;
3847 free_extent_buffer(buf);
3851 static int add_root_to_pending(struct extent_buffer *buf,
3852 struct cache_tree *extent_cache,
3853 struct cache_tree *pending,
3854 struct cache_tree *seen,
3855 struct cache_tree *nodes,
3856 struct btrfs_key *root_key)
3858 if (btrfs_header_level(buf) > 0)
3859 add_pending(nodes, seen, buf->start, buf->len);
3861 add_pending(pending, seen, buf->start, buf->len);
3862 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3863 0, 1, 1, 0, 1, 0, buf->len);
3865 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3866 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3867 add_tree_backref(extent_cache, buf->start, buf->start,
3870 add_tree_backref(extent_cache, buf->start, 0,
3871 root_key->objectid, 1);
3875 /* as we fix the tree, we might be deleting blocks that
3876 * we're tracking for repair. This hook makes sure we
3877 * remove any backrefs for blocks as we are fixing them.
3879 static int free_extent_hook(struct btrfs_trans_handle *trans,
3880 struct btrfs_root *root,
3881 u64 bytenr, u64 num_bytes, u64 parent,
3882 u64 root_objectid, u64 owner, u64 offset,
3885 struct extent_record *rec;
3886 struct cache_extent *cache;
3888 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3890 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3891 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
3895 rec = container_of(cache, struct extent_record, cache);
3897 struct data_backref *back;
3898 back = find_data_backref(rec, parent, root_objectid, owner,
3899 offset, 1, bytenr, num_bytes);
3902 if (back->node.found_ref) {
3903 back->found_ref -= refs_to_drop;
3905 rec->refs -= refs_to_drop;
3907 if (back->node.found_extent_tree) {
3908 back->num_refs -= refs_to_drop;
3909 if (rec->extent_item_refs)
3910 rec->extent_item_refs -= refs_to_drop;
3912 if (back->found_ref == 0)
3913 back->node.found_ref = 0;
3914 if (back->num_refs == 0)
3915 back->node.found_extent_tree = 0;
3917 if (!back->node.found_extent_tree && back->node.found_ref) {
3918 list_del(&back->node.list);
3922 struct tree_backref *back;
3923 back = find_tree_backref(rec, parent, root_objectid);
3926 if (back->node.found_ref) {
3929 back->node.found_ref = 0;
3931 if (back->node.found_extent_tree) {
3932 if (rec->extent_item_refs)
3933 rec->extent_item_refs--;
3934 back->node.found_extent_tree = 0;
3936 if (!back->node.found_extent_tree && back->node.found_ref) {
3937 list_del(&back->node.list);
3941 maybe_free_extent_rec(extent_cache, rec);
3946 static int delete_extent_records(struct btrfs_trans_handle *trans,
3947 struct btrfs_root *root,
3948 struct btrfs_path *path,
3949 u64 bytenr, u64 new_len)
3951 struct btrfs_key key;
3952 struct btrfs_key found_key;
3953 struct extent_buffer *leaf;
3958 key.objectid = bytenr;
3960 key.offset = (u64)-1;
3963 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3970 if (path->slots[0] == 0)
3976 leaf = path->nodes[0];
3977 slot = path->slots[0];
3979 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3980 if (found_key.objectid != bytenr)
3983 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3984 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3985 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3986 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3987 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3988 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3989 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3990 btrfs_release_path(path);
3991 if (found_key.type == 0) {
3992 if (found_key.offset == 0)
3994 key.offset = found_key.offset - 1;
3995 key.type = found_key.type;
3997 key.type = found_key.type - 1;
3998 key.offset = (u64)-1;
4002 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4003 found_key.objectid, found_key.type, found_key.offset);
4005 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4008 btrfs_release_path(path);
4010 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4011 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4012 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4013 found_key.offset : root->leafsize;
4015 ret = btrfs_update_block_group(trans, root, bytenr,
4022 btrfs_release_path(path);
4027 * for a single backref, this will allocate a new extent
4028 * and add the backref to it.
4030 static int record_extent(struct btrfs_trans_handle *trans,
4031 struct btrfs_fs_info *info,
4032 struct btrfs_path *path,
4033 struct extent_record *rec,
4034 struct extent_backref *back,
4035 int allocated, u64 flags)
4038 struct btrfs_root *extent_root = info->extent_root;
4039 struct extent_buffer *leaf;
4040 struct btrfs_key ins_key;
4041 struct btrfs_extent_item *ei;
4042 struct tree_backref *tback;
4043 struct data_backref *dback;
4044 struct btrfs_tree_block_info *bi;
4047 rec->max_size = max_t(u64, rec->max_size,
4048 info->extent_root->leafsize);
4051 u32 item_size = sizeof(*ei);
4054 item_size += sizeof(*bi);
4056 ins_key.objectid = rec->start;
4057 ins_key.offset = rec->max_size;
4058 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4060 ret = btrfs_insert_empty_item(trans, extent_root, path,
4061 &ins_key, item_size);
4065 leaf = path->nodes[0];
4066 ei = btrfs_item_ptr(leaf, path->slots[0],
4067 struct btrfs_extent_item);
4069 btrfs_set_extent_refs(leaf, ei, 0);
4070 btrfs_set_extent_generation(leaf, ei, rec->generation);
4072 if (back->is_data) {
4073 btrfs_set_extent_flags(leaf, ei,
4074 BTRFS_EXTENT_FLAG_DATA);
4076 struct btrfs_disk_key copy_key;;
4078 tback = (struct tree_backref *)back;
4079 bi = (struct btrfs_tree_block_info *)(ei + 1);
4080 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4083 btrfs_set_disk_key_objectid(©_key,
4084 rec->info_objectid);
4085 btrfs_set_disk_key_type(©_key, 0);
4086 btrfs_set_disk_key_offset(©_key, 0);
4088 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
4089 btrfs_set_tree_block_key(leaf, bi, ©_key);
4091 btrfs_set_extent_flags(leaf, ei,
4092 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
4095 btrfs_mark_buffer_dirty(leaf);
4096 ret = btrfs_update_block_group(trans, extent_root, rec->start,
4097 rec->max_size, 1, 0);
4100 btrfs_release_path(path);
4103 if (back->is_data) {
4107 dback = (struct data_backref *)back;
4108 if (back->full_backref)
4109 parent = dback->parent;
4113 for (i = 0; i < dback->found_ref; i++) {
4114 /* if parent != 0, we're doing a full backref
4115 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
4116 * just makes the backref allocator create a data
4119 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4120 rec->start, rec->max_size,
4124 BTRFS_FIRST_FREE_OBJECTID :
4130 fprintf(stderr, "adding new data backref"
4131 " on %llu %s %llu owner %llu"
4132 " offset %llu found %d\n",
4133 (unsigned long long)rec->start,
4134 back->full_backref ?
4136 back->full_backref ?
4137 (unsigned long long)parent :
4138 (unsigned long long)dback->root,
4139 (unsigned long long)dback->owner,
4140 (unsigned long long)dback->offset,
4145 tback = (struct tree_backref *)back;
4146 if (back->full_backref)
4147 parent = tback->parent;
4151 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4152 rec->start, rec->max_size,
4153 parent, tback->root, 0, 0);
4154 fprintf(stderr, "adding new tree backref on "
4155 "start %llu len %llu parent %llu root %llu\n",
4156 rec->start, rec->max_size, tback->parent, tback->root);
4161 btrfs_release_path(path);
4165 struct extent_entry {
4170 struct list_head list;
4173 static struct extent_entry *find_entry(struct list_head *entries,
4174 u64 bytenr, u64 bytes)
4176 struct extent_entry *entry = NULL;
4178 list_for_each_entry(entry, entries, list) {
4179 if (entry->bytenr == bytenr && entry->bytes == bytes)
4186 static struct extent_entry *find_most_right_entry(struct list_head *entries)
4188 struct extent_entry *entry, *best = NULL, *prev = NULL;
4190 list_for_each_entry(entry, entries, list) {
4197 * If there are as many broken entries as entries then we know
4198 * not to trust this particular entry.
4200 if (entry->broken == entry->count)
4204 * If our current entry == best then we can't be sure our best
4205 * is really the best, so we need to keep searching.
4207 if (best && best->count == entry->count) {
4213 /* Prev == entry, not good enough, have to keep searching */
4214 if (!prev->broken && prev->count == entry->count)
4218 best = (prev->count > entry->count) ? prev : entry;
4219 else if (best->count < entry->count)
4227 static int repair_ref(struct btrfs_trans_handle *trans,
4228 struct btrfs_fs_info *info, struct btrfs_path *path,
4229 struct data_backref *dback, struct extent_entry *entry)
4231 struct btrfs_root *root;
4232 struct btrfs_file_extent_item *fi;
4233 struct extent_buffer *leaf;
4234 struct btrfs_key key;
4238 key.objectid = dback->root;
4239 key.type = BTRFS_ROOT_ITEM_KEY;
4240 key.offset = (u64)-1;
4241 root = btrfs_read_fs_root(info, &key);
4243 fprintf(stderr, "Couldn't find root for our ref\n");
4248 * The backref points to the original offset of the extent if it was
4249 * split, so we need to search down to the offset we have and then walk
4250 * forward until we find the backref we're looking for.
4252 key.objectid = dback->owner;
4253 key.type = BTRFS_EXTENT_DATA_KEY;
4254 key.offset = dback->offset;
4255 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4257 fprintf(stderr, "Error looking up ref %d\n", ret);
4262 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4263 ret = btrfs_next_leaf(root, path);
4265 fprintf(stderr, "Couldn't find our ref, next\n");
4269 leaf = path->nodes[0];
4270 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4271 if (key.objectid != dback->owner ||
4272 key.type != BTRFS_EXTENT_DATA_KEY) {
4273 fprintf(stderr, "Couldn't find our ref, search\n");
4276 fi = btrfs_item_ptr(leaf, path->slots[0],
4277 struct btrfs_file_extent_item);
4278 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4279 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4281 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
4286 btrfs_release_path(path);
4289 * Have to make sure that this root gets updated when we commit the
4292 root->track_dirty = 1;
4293 if (root->last_trans != trans->transid) {
4294 root->last_trans = trans->transid;
4295 root->commit_root = root->node;
4296 extent_buffer_get(root->node);
4300 * Ok we have the key of the file extent we want to fix, now we can cow
4301 * down to the thing and fix it.
4303 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4305 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
4306 key.objectid, key.type, key.offset, ret);
4310 fprintf(stderr, "Well that's odd, we just found this key "
4311 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
4315 leaf = path->nodes[0];
4316 fi = btrfs_item_ptr(leaf, path->slots[0],
4317 struct btrfs_file_extent_item);
4319 if (btrfs_file_extent_compression(leaf, fi) &&
4320 dback->disk_bytenr != entry->bytenr) {
4321 fprintf(stderr, "Ref doesn't match the record start and is "
4322 "compressed, please take a btrfs-image of this file "
4323 "system and send it to a btrfs developer so they can "
4324 "complete this functionality for bytenr %Lu\n",
4325 dback->disk_bytenr);
4329 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
4330 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4331 } else if (dback->disk_bytenr > entry->bytenr) {
4332 u64 off_diff, offset;
4334 off_diff = dback->disk_bytenr - entry->bytenr;
4335 offset = btrfs_file_extent_offset(leaf, fi);
4336 if (dback->disk_bytenr + offset +
4337 btrfs_file_extent_num_bytes(leaf, fi) >
4338 entry->bytenr + entry->bytes) {
4339 fprintf(stderr, "Ref is past the entry end, please "
4340 "take a btrfs-image of this file system and "
4341 "send it to a btrfs developer, ref %Lu\n",
4342 dback->disk_bytenr);
4346 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4347 btrfs_set_file_extent_offset(leaf, fi, offset);
4348 } else if (dback->disk_bytenr < entry->bytenr) {
4351 offset = btrfs_file_extent_offset(leaf, fi);
4352 if (dback->disk_bytenr + offset < entry->bytenr) {
4353 fprintf(stderr, "Ref is before the entry start, please"
4354 " take a btrfs-image of this file system and "
4355 "send it to a btrfs developer, ref %Lu\n",
4356 dback->disk_bytenr);
4360 offset += dback->disk_bytenr;
4361 offset -= entry->bytenr;
4362 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4363 btrfs_set_file_extent_offset(leaf, fi, offset);
4366 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
4369 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
4370 * only do this if we aren't using compression, otherwise it's a
4373 if (!btrfs_file_extent_compression(leaf, fi))
4374 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
4376 printf("ram bytes may be wrong?\n");
4377 btrfs_mark_buffer_dirty(leaf);
4378 btrfs_release_path(path);
4382 static int verify_backrefs(struct btrfs_trans_handle *trans,
4383 struct btrfs_fs_info *info, struct btrfs_path *path,
4384 struct extent_record *rec)
4386 struct extent_backref *back;
4387 struct data_backref *dback;
4388 struct extent_entry *entry, *best = NULL;
4391 int broken_entries = 0;
4396 * Metadata is easy and the backrefs should always agree on bytenr and
4397 * size, if not we've got bigger issues.
4402 list_for_each_entry(back, &rec->backrefs, list) {
4403 dback = (struct data_backref *)back;
4405 * We only pay attention to backrefs that we found a real
4408 if (dback->found_ref == 0)
4410 if (back->full_backref)
4414 * For now we only catch when the bytes don't match, not the
4415 * bytenr. We can easily do this at the same time, but I want
4416 * to have a fs image to test on before we just add repair
4417 * functionality willy-nilly so we know we won't screw up the
4421 entry = find_entry(&entries, dback->disk_bytenr,
4424 entry = malloc(sizeof(struct extent_entry));
4429 memset(entry, 0, sizeof(*entry));
4430 entry->bytenr = dback->disk_bytenr;
4431 entry->bytes = dback->bytes;
4432 list_add_tail(&entry->list, &entries);
4437 * If we only have on entry we may think the entries agree when
4438 * in reality they don't so we have to do some extra checking.
4440 if (dback->disk_bytenr != rec->start ||
4441 dback->bytes != rec->nr || back->broken)
4452 /* Yay all the backrefs agree, carry on good sir */
4453 if (nr_entries <= 1 && !mismatch)
4456 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
4457 "%Lu\n", rec->start);
4460 * First we want to see if the backrefs can agree amongst themselves who
4461 * is right, so figure out which one of the entries has the highest
4464 best = find_most_right_entry(&entries);
4467 * Ok so we may have an even split between what the backrefs think, so
4468 * this is where we use the extent ref to see what it thinks.
4471 entry = find_entry(&entries, rec->start, rec->nr);
4472 if (!entry && (!broken_entries || !rec->found_rec)) {
4473 fprintf(stderr, "Backrefs don't agree with eachother "
4474 "and extent record doesn't agree with anybody,"
4475 " so we can't fix bytenr %Lu bytes %Lu\n",
4476 rec->start, rec->nr);
4479 } else if (!entry) {
4481 * Ok our backrefs were broken, we'll assume this is the
4482 * correct value and add an entry for this range.
4484 entry = malloc(sizeof(struct extent_entry));
4489 memset(entry, 0, sizeof(*entry));
4490 entry->bytenr = rec->start;
4491 entry->bytes = rec->nr;
4492 list_add_tail(&entry->list, &entries);
4496 best = find_most_right_entry(&entries);
4498 fprintf(stderr, "Backrefs and extent record evenly "
4499 "split on who is right, this is going to "
4500 "require user input to fix bytenr %Lu bytes "
4501 "%Lu\n", rec->start, rec->nr);
4508 * I don't think this can happen currently as we'll abort() if we catch
4509 * this case higher up, but in case somebody removes that we still can't
4510 * deal with it properly here yet, so just bail out of that's the case.
4512 if (best->bytenr != rec->start) {
4513 fprintf(stderr, "Extent start and backref starts don't match, "
4514 "please use btrfs-image on this file system and send "
4515 "it to a btrfs developer so they can make fsck fix "
4516 "this particular case. bytenr is %Lu, bytes is %Lu\n",
4517 rec->start, rec->nr);
4523 * Ok great we all agreed on an extent record, let's go find the real
4524 * references and fix up the ones that don't match.
4526 list_for_each_entry(back, &rec->backrefs, list) {
4527 dback = (struct data_backref *)back;
4530 * Still ignoring backrefs that don't have a real ref attached
4533 if (dback->found_ref == 0)
4535 if (back->full_backref)
4538 if (dback->bytes == best->bytes &&
4539 dback->disk_bytenr == best->bytenr)
4542 ret = repair_ref(trans, info, path, dback, best);
4548 * Ok we messed with the actual refs, which means we need to drop our
4549 * entire cache and go back and rescan. I know this is a huge pain and
4550 * adds a lot of extra work, but it's the only way to be safe. Once all
4551 * the backrefs agree we may not need to do anything to the extent
4556 while (!list_empty(&entries)) {
4557 entry = list_entry(entries.next, struct extent_entry, list);
4558 list_del_init(&entry->list);
4564 static int process_duplicates(struct btrfs_root *root,
4565 struct cache_tree *extent_cache,
4566 struct extent_record *rec)
4568 struct extent_record *good, *tmp;
4569 struct cache_extent *cache;
4573 * If we found a extent record for this extent then return, or if we
4574 * have more than one duplicate we are likely going to need to delete
4577 if (rec->found_rec || rec->num_duplicates > 1)
4580 /* Shouldn't happen but just in case */
4581 BUG_ON(!rec->num_duplicates);
4584 * So this happens if we end up with a backref that doesn't match the
4585 * actual extent entry. So either the backref is bad or the extent
4586 * entry is bad. Either way we want to have the extent_record actually
4587 * reflect what we found in the extent_tree, so we need to take the
4588 * duplicate out and use that as the extent_record since the only way we
4589 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
4591 remove_cache_extent(extent_cache, &rec->cache);
4593 good = list_entry(rec->dups.next, struct extent_record, list);
4594 list_del_init(&good->list);
4595 INIT_LIST_HEAD(&good->backrefs);
4596 INIT_LIST_HEAD(&good->dups);
4597 good->cache.start = good->start;
4598 good->cache.size = good->nr;
4599 good->content_checked = 0;
4600 good->owner_ref_checked = 0;
4601 good->num_duplicates = 0;
4602 good->refs = rec->refs;
4603 list_splice_init(&rec->backrefs, &good->backrefs);
4605 cache = lookup_cache_extent(extent_cache, good->start,
4609 tmp = container_of(cache, struct extent_record, cache);
4612 * If we find another overlapping extent and it's found_rec is
4613 * set then it's a duplicate and we need to try and delete
4616 if (tmp->found_rec || tmp->num_duplicates > 0) {
4617 if (list_empty(&good->list))
4618 list_add_tail(&good->list,
4619 &duplicate_extents);
4620 good->num_duplicates += tmp->num_duplicates + 1;
4621 list_splice_init(&tmp->dups, &good->dups);
4622 list_del_init(&tmp->list);
4623 list_add_tail(&tmp->list, &good->dups);
4624 remove_cache_extent(extent_cache, &tmp->cache);
4629 * Ok we have another non extent item backed extent rec, so lets
4630 * just add it to this extent and carry on like we did above.
4632 good->refs += tmp->refs;
4633 list_splice_init(&tmp->backrefs, &good->backrefs);
4634 remove_cache_extent(extent_cache, &tmp->cache);
4637 ret = insert_cache_extent(extent_cache, &good->cache);
4640 return good->num_duplicates ? 0 : 1;
4643 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root,
4645 struct extent_record *rec)
4647 LIST_HEAD(delete_list);
4648 struct btrfs_path *path;
4649 struct extent_record *tmp, *good, *n;
4652 struct btrfs_key key;
4654 path = btrfs_alloc_path();
4661 /* Find the record that covers all of the duplicates. */
4662 list_for_each_entry(tmp, &rec->dups, list) {
4663 if (good->start < tmp->start)
4665 if (good->nr > tmp->nr)
4668 if (tmp->start + tmp->nr < good->start + good->nr) {
4669 fprintf(stderr, "Ok we have overlapping extents that "
4670 "aren't completely covered by eachother, this "
4671 "is going to require more careful thought. "
4672 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
4673 tmp->start, tmp->nr, good->start, good->nr);
4680 list_add_tail(&rec->list, &delete_list);
4682 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
4685 list_move_tail(&tmp->list, &delete_list);
4688 root = root->fs_info->extent_root;
4689 list_for_each_entry(tmp, &delete_list, list) {
4690 if (tmp->found_rec == 0)
4692 key.objectid = tmp->start;
4693 key.type = BTRFS_EXTENT_ITEM_KEY;
4694 key.offset = tmp->nr;
4696 /* Shouldn't happen but just in case */
4697 if (tmp->metadata) {
4698 fprintf(stderr, "Well this shouldn't happen, extent "
4699 "record overlaps but is metadata? "
4700 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
4704 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4710 ret = btrfs_del_item(trans, root, path);
4713 btrfs_release_path(path);
4718 while (!list_empty(&delete_list)) {
4719 tmp = list_entry(delete_list.next, struct extent_record, list);
4720 list_del_init(&tmp->list);
4726 while (!list_empty(&rec->dups)) {
4727 tmp = list_entry(rec->dups.next, struct extent_record, list);
4728 list_del_init(&tmp->list);
4732 btrfs_free_path(path);
4734 if (!ret && !nr_del)
4735 rec->num_duplicates = 0;
4737 return ret ? ret : nr_del;
4740 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
4741 struct btrfs_fs_info *info,
4742 struct btrfs_path *path,
4743 struct cache_tree *extent_cache,
4744 struct extent_record *rec)
4746 struct btrfs_root *root;
4747 struct extent_backref *back;
4748 struct data_backref *dback;
4749 struct cache_extent *cache;
4750 struct btrfs_file_extent_item *fi;
4751 struct btrfs_key key;
4755 list_for_each_entry(back, &rec->backrefs, list) {
4756 dback = (struct data_backref *)back;
4758 /* We found this one, we don't need to do a lookup */
4759 if (dback->found_ref)
4761 /* Don't care about full backrefs (poor unloved backrefs) */
4762 if (back->full_backref)
4764 key.objectid = dback->root;
4765 key.type = BTRFS_ROOT_ITEM_KEY;
4766 key.offset = (u64)-1;
4768 root = btrfs_read_fs_root(info, &key);
4770 /* No root, definitely a bad ref, skip */
4771 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
4773 /* Other err, exit */
4775 return PTR_ERR(root);
4777 key.objectid = dback->owner;
4778 key.type = BTRFS_EXTENT_DATA_KEY;
4779 key.offset = dback->offset;
4780 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4782 btrfs_release_path(path);
4785 /* Didn't find it, we can carry on */
4790 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
4791 struct btrfs_file_extent_item);
4792 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
4793 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
4794 btrfs_release_path(path);
4795 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4797 struct extent_record *tmp;
4798 tmp = container_of(cache, struct extent_record, cache);
4801 * If we found an extent record for the bytenr for this
4802 * particular backref then we can't add it to our
4803 * current extent record. We only want to add backrefs
4804 * that don't have a corresponding extent item in the
4805 * extent tree since they likely belong to this record
4806 * and we need to fix it if it doesn't match bytenrs.
4812 dback->found_ref += 1;
4813 dback->disk_bytenr = bytenr;
4814 dback->bytes = bytes;
4817 * Set this so the verify backref code knows not to trust the
4818 * values in this backref.
4827 * when an incorrect extent item is found, this will delete
4828 * all of the existing entries for it and recreate them
4829 * based on what the tree scan found.
4831 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
4832 struct btrfs_fs_info *info,
4833 struct cache_tree *extent_cache,
4834 struct extent_record *rec)
4837 struct btrfs_path *path;
4838 struct list_head *cur = rec->backrefs.next;
4839 struct cache_extent *cache;
4840 struct extent_backref *back;
4844 /* remember our flags for recreating the extent */
4845 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
4846 rec->max_size, rec->metadata, NULL,
4849 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4851 path = btrfs_alloc_path();
4855 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
4857 * Sometimes the backrefs themselves are so broken they don't
4858 * get attached to any meaningful rec, so first go back and
4859 * check any of our backrefs that we couldn't find and throw
4860 * them into the list if we find the backref so that
4861 * verify_backrefs can figure out what to do.
4863 ret = find_possible_backrefs(trans, info, path, extent_cache,
4869 /* step one, make sure all of the backrefs agree */
4870 ret = verify_backrefs(trans, info, path, rec);
4874 /* step two, delete all the existing records */
4875 ret = delete_extent_records(trans, info->extent_root, path,
4876 rec->start, rec->max_size);
4881 /* was this block corrupt? If so, don't add references to it */
4882 cache = lookup_cache_extent(info->corrupt_blocks,
4883 rec->start, rec->max_size);
4889 /* step three, recreate all the refs we did find */
4890 while(cur != &rec->backrefs) {
4891 back = list_entry(cur, struct extent_backref, list);
4895 * if we didn't find any references, don't create a
4898 if (!back->found_ref)
4901 ret = record_extent(trans, info, path, rec, back, allocated, flags);
4908 btrfs_free_path(path);
4912 /* right now we only prune from the extent allocation tree */
4913 static int prune_one_block(struct btrfs_trans_handle *trans,
4914 struct btrfs_fs_info *info,
4915 struct btrfs_corrupt_block *corrupt)
4918 struct btrfs_path path;
4919 struct extent_buffer *eb;
4923 int level = corrupt->level + 1;
4925 btrfs_init_path(&path);
4927 /* we want to stop at the parent to our busted block */
4928 path.lowest_level = level;
4930 ret = btrfs_search_slot(trans, info->extent_root,
4931 &corrupt->key, &path, -1, 1);
4936 eb = path.nodes[level];
4943 * hopefully the search gave us the block we want to prune,
4944 * lets try that first
4946 slot = path.slots[level];
4947 found = btrfs_node_blockptr(eb, slot);
4948 if (found == corrupt->cache.start)
4951 nritems = btrfs_header_nritems(eb);
4953 /* the search failed, lets scan this node and hope we find it */
4954 for (slot = 0; slot < nritems; slot++) {
4955 found = btrfs_node_blockptr(eb, slot);
4956 if (found == corrupt->cache.start)
4960 * we couldn't find the bad block. TODO, search all the nodes for pointers
4963 if (eb == info->extent_root->node) {
4968 btrfs_release_path(&path);
4973 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
4974 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
4977 btrfs_release_path(&path);
4981 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
4982 struct btrfs_fs_info *info)
4984 struct cache_extent *cache;
4985 struct btrfs_corrupt_block *corrupt;
4987 cache = search_cache_extent(info->corrupt_blocks, 0);
4991 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
4992 prune_one_block(trans, info, corrupt);
4993 cache = next_cache_extent(cache);
4998 static void free_corrupt_block(struct cache_extent *cache)
5000 struct btrfs_corrupt_block *corrupt;
5002 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5006 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5008 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5010 struct btrfs_block_group_cache *cache;
5015 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5016 &start, &end, EXTENT_DIRTY);
5019 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5025 cache = btrfs_lookup_first_block_group(fs_info, start);
5030 start = cache->key.objectid + cache->key.offset;
5034 static int check_extent_refs(struct btrfs_trans_handle *trans,
5035 struct btrfs_root *root,
5036 struct cache_tree *extent_cache)
5038 struct extent_record *rec;
5039 struct cache_extent *cache;
5047 * if we're doing a repair, we have to make sure
5048 * we don't allocate from the problem extents.
5049 * In the worst case, this will be all the
5052 cache = search_cache_extent(extent_cache, 0);
5054 rec = container_of(cache, struct extent_record, cache);
5055 btrfs_pin_extent(root->fs_info,
5056 rec->start, rec->max_size);
5057 cache = next_cache_extent(cache);
5060 /* pin down all the corrupted blocks too */
5061 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5063 btrfs_pin_extent(root->fs_info,
5064 cache->start, cache->size);
5065 cache = next_cache_extent(cache);
5067 prune_corrupt_blocks(trans, root->fs_info);
5068 reset_cached_block_groups(root->fs_info);
5072 * We need to delete any duplicate entries we find first otherwise we
5073 * could mess up the extent tree when we have backrefs that actually
5074 * belong to a different extent item and not the weird duplicate one.
5076 while (repair && !list_empty(&duplicate_extents)) {
5077 rec = list_entry(duplicate_extents.next, struct extent_record,
5079 list_del_init(&rec->list);
5081 /* Sometimes we can find a backref before we find an actual
5082 * extent, so we need to process it a little bit to see if there
5083 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
5084 * if this is a backref screwup. If we need to delete stuff
5085 * process_duplicates() will return 0, otherwise it will return
5088 if (process_duplicates(root, extent_cache, rec))
5090 ret = delete_duplicate_records(trans, root, rec);
5094 * delete_duplicate_records will return the number of entries
5095 * deleted, so if it's greater than 0 then we know we actually
5096 * did something and we need to remove.
5107 cache = search_cache_extent(extent_cache, 0);
5110 rec = container_of(cache, struct extent_record, cache);
5111 if (rec->num_duplicates) {
5112 fprintf(stderr, "extent item %llu has multiple extent "
5113 "items\n", (unsigned long long)rec->start);
5117 if (rec->refs != rec->extent_item_refs) {
5118 fprintf(stderr, "ref mismatch on [%llu %llu] ",
5119 (unsigned long long)rec->start,
5120 (unsigned long long)rec->nr);
5121 fprintf(stderr, "extent item %llu, found %llu\n",
5122 (unsigned long long)rec->extent_item_refs,
5123 (unsigned long long)rec->refs);
5124 if (!fixed && repair) {
5125 ret = fixup_extent_refs(trans, root->fs_info,
5134 if (all_backpointers_checked(rec, 1)) {
5135 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
5136 (unsigned long long)rec->start,
5137 (unsigned long long)rec->nr);
5139 if (!fixed && repair) {
5140 ret = fixup_extent_refs(trans, root->fs_info,
5149 if (!rec->owner_ref_checked) {
5150 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
5151 (unsigned long long)rec->start,
5152 (unsigned long long)rec->nr);
5153 if (!fixed && repair) {
5154 ret = fixup_extent_refs(trans, root->fs_info,
5163 remove_cache_extent(extent_cache, cache);
5164 free_all_extent_backrefs(rec);
5169 if (ret && ret != -EAGAIN) {
5170 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
5173 btrfs_fix_block_accounting(trans, root);
5176 fprintf(stderr, "repaired damaged extent references\n");
5182 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
5186 if (type & BTRFS_BLOCK_GROUP_RAID0) {
5187 stripe_size = length;
5188 stripe_size /= num_stripes;
5189 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
5190 stripe_size = length * 2;
5191 stripe_size /= num_stripes;
5192 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
5193 stripe_size = length;
5194 stripe_size /= (num_stripes - 1);
5195 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
5196 stripe_size = length;
5197 stripe_size /= (num_stripes - 2);
5199 stripe_size = length;
5204 static int check_chunk_refs(struct chunk_record *chunk_rec,
5205 struct block_group_tree *block_group_cache,
5206 struct device_extent_tree *dev_extent_cache,
5209 struct cache_extent *block_group_item;
5210 struct block_group_record *block_group_rec;
5211 struct cache_extent *dev_extent_item;
5212 struct device_extent_record *dev_extent_rec;
5219 block_group_item = lookup_cache_extent(&block_group_cache->tree,
5222 if (block_group_item) {
5223 block_group_rec = container_of(block_group_item,
5224 struct block_group_record,
5226 if (chunk_rec->length != block_group_rec->offset ||
5227 chunk_rec->offset != block_group_rec->objectid ||
5228 chunk_rec->type_flags != block_group_rec->flags) {
5231 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
5232 chunk_rec->objectid,
5237 chunk_rec->type_flags,
5238 block_group_rec->objectid,
5239 block_group_rec->type,
5240 block_group_rec->offset,
5241 block_group_rec->offset,
5242 block_group_rec->objectid,
5243 block_group_rec->flags);
5246 list_del_init(&block_group_rec->list);
5247 chunk_rec->bg_rec = block_group_rec;
5252 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
5253 chunk_rec->objectid,
5258 chunk_rec->type_flags);
5262 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
5263 chunk_rec->num_stripes);
5264 for (i = 0; i < chunk_rec->num_stripes; ++i) {
5265 devid = chunk_rec->stripes[i].devid;
5266 offset = chunk_rec->stripes[i].offset;
5267 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
5268 devid, offset, length);
5269 if (dev_extent_item) {
5270 dev_extent_rec = container_of(dev_extent_item,
5271 struct device_extent_record,
5273 if (dev_extent_rec->objectid != devid ||
5274 dev_extent_rec->offset != offset ||
5275 dev_extent_rec->chunk_offset != chunk_rec->offset ||
5276 dev_extent_rec->length != length) {
5279 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
5280 chunk_rec->objectid,
5283 chunk_rec->stripes[i].devid,
5284 chunk_rec->stripes[i].offset,
5285 dev_extent_rec->objectid,
5286 dev_extent_rec->offset,
5287 dev_extent_rec->length);
5290 list_move(&dev_extent_rec->chunk_list,
5291 &chunk_rec->dextents);
5296 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
5297 chunk_rec->objectid,
5300 chunk_rec->stripes[i].devid,
5301 chunk_rec->stripes[i].offset);
5308 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
5309 int check_chunks(struct cache_tree *chunk_cache,
5310 struct block_group_tree *block_group_cache,
5311 struct device_extent_tree *dev_extent_cache,
5312 struct list_head *good, struct list_head *bad, int silent)
5314 struct cache_extent *chunk_item;
5315 struct chunk_record *chunk_rec;
5316 struct block_group_record *bg_rec;
5317 struct device_extent_record *dext_rec;
5321 chunk_item = first_cache_extent(chunk_cache);
5322 while (chunk_item) {
5323 chunk_rec = container_of(chunk_item, struct chunk_record,
5325 err = check_chunk_refs(chunk_rec, block_group_cache,
5326 dev_extent_cache, silent);
5330 list_add_tail(&chunk_rec->list, bad);
5333 list_add_tail(&chunk_rec->list, good);
5336 chunk_item = next_cache_extent(chunk_item);
5339 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
5342 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
5350 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
5354 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
5365 static int check_device_used(struct device_record *dev_rec,
5366 struct device_extent_tree *dext_cache)
5368 struct cache_extent *cache;
5369 struct device_extent_record *dev_extent_rec;
5372 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
5374 dev_extent_rec = container_of(cache,
5375 struct device_extent_record,
5377 if (dev_extent_rec->objectid != dev_rec->devid)
5380 list_del(&dev_extent_rec->device_list);
5381 total_byte += dev_extent_rec->length;
5382 cache = next_cache_extent(cache);
5385 if (total_byte != dev_rec->byte_used) {
5387 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
5388 total_byte, dev_rec->byte_used, dev_rec->objectid,
5389 dev_rec->type, dev_rec->offset);
5396 /* check btrfs_dev_item -> btrfs_dev_extent */
5397 static int check_devices(struct rb_root *dev_cache,
5398 struct device_extent_tree *dev_extent_cache)
5400 struct rb_node *dev_node;
5401 struct device_record *dev_rec;
5402 struct device_extent_record *dext_rec;
5406 dev_node = rb_first(dev_cache);
5408 dev_rec = container_of(dev_node, struct device_record, node);
5409 err = check_device_used(dev_rec, dev_extent_cache);
5413 dev_node = rb_next(dev_node);
5415 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
5418 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
5419 dext_rec->objectid, dext_rec->offset, dext_rec->length);
5426 static int check_chunks_and_extents(struct btrfs_root *root)
5428 struct rb_root dev_cache;
5429 struct cache_tree chunk_cache;
5430 struct block_group_tree block_group_cache;
5431 struct device_extent_tree dev_extent_cache;
5432 struct cache_tree extent_cache;
5433 struct cache_tree seen;
5434 struct cache_tree pending;
5435 struct cache_tree reada;
5436 struct cache_tree nodes;
5437 struct cache_tree corrupt_blocks;
5438 struct btrfs_path path;
5439 struct btrfs_key key;
5440 struct btrfs_key found_key;
5443 struct block_info *bits;
5445 struct extent_buffer *leaf;
5446 struct btrfs_trans_handle *trans = NULL;
5448 struct btrfs_root_item ri;
5450 dev_cache = RB_ROOT;
5451 cache_tree_init(&chunk_cache);
5452 block_group_tree_init(&block_group_cache);
5453 device_extent_tree_init(&dev_extent_cache);
5455 cache_tree_init(&extent_cache);
5456 cache_tree_init(&seen);
5457 cache_tree_init(&pending);
5458 cache_tree_init(&nodes);
5459 cache_tree_init(&reada);
5460 cache_tree_init(&corrupt_blocks);
5463 trans = btrfs_start_transaction(root, 1);
5464 if (IS_ERR(trans)) {
5465 fprintf(stderr, "Error starting transaction\n");
5466 return PTR_ERR(trans);
5468 root->fs_info->fsck_extent_cache = &extent_cache;
5469 root->fs_info->free_extent_hook = free_extent_hook;
5470 root->fs_info->corrupt_blocks = &corrupt_blocks;
5474 bits = malloc(bits_nr * sizeof(struct block_info));
5481 add_root_to_pending(root->fs_info->tree_root->node,
5482 &extent_cache, &pending, &seen, &nodes,
5483 &root->fs_info->tree_root->root_key);
5485 add_root_to_pending(root->fs_info->chunk_root->node,
5486 &extent_cache, &pending, &seen, &nodes,
5487 &root->fs_info->chunk_root->root_key);
5489 btrfs_init_path(&path);
5492 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
5493 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
5497 leaf = path.nodes[0];
5498 slot = path.slots[0];
5499 if (slot >= btrfs_header_nritems(path.nodes[0])) {
5500 ret = btrfs_next_leaf(root, &path);
5503 leaf = path.nodes[0];
5504 slot = path.slots[0];
5506 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
5507 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
5508 unsigned long offset;
5509 struct extent_buffer *buf;
5511 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5512 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5513 buf = read_tree_block(root->fs_info->tree_root,
5514 btrfs_root_bytenr(&ri),
5515 btrfs_level_size(root,
5516 btrfs_root_level(&ri)), 0);
5517 add_root_to_pending(buf, &extent_cache, &pending,
5518 &seen, &nodes, &found_key);
5519 free_extent_buffer(buf);
5523 btrfs_release_path(&path);
5525 ret = run_next_block(root, bits, bits_nr, &last, &pending,
5526 &seen, &reada, &nodes, &extent_cache,
5527 &chunk_cache, &dev_cache,
5528 &block_group_cache, &dev_extent_cache);
5533 ret = check_extent_refs(trans, root, &extent_cache);
5534 if (ret == -EAGAIN) {
5535 ret = btrfs_commit_transaction(trans, root);
5539 trans = btrfs_start_transaction(root, 1);
5540 if (IS_ERR(trans)) {
5541 ret = PTR_ERR(trans);
5545 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5546 free_extent_cache_tree(&seen);
5547 free_extent_cache_tree(&pending);
5548 free_extent_cache_tree(&reada);
5549 free_extent_cache_tree(&nodes);
5550 free_extent_record_cache(root->fs_info, &extent_cache);
5554 err = check_chunks(&chunk_cache, &block_group_cache,
5555 &dev_extent_cache, NULL, NULL, 0);
5559 err = check_devices(&dev_cache, &dev_extent_cache);
5564 err = btrfs_commit_transaction(trans, root);
5570 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5571 root->fs_info->fsck_extent_cache = NULL;
5572 root->fs_info->free_extent_hook = NULL;
5573 root->fs_info->corrupt_blocks = NULL;
5576 free_chunk_cache_tree(&chunk_cache);
5577 free_device_cache_tree(&dev_cache);
5578 free_block_group_tree(&block_group_cache);
5579 free_device_extent_tree(&dev_extent_cache);
5583 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
5584 struct btrfs_root *root, int overwrite)
5586 struct extent_buffer *c;
5587 struct extent_buffer *old = root->node;
5589 struct btrfs_disk_key disk_key = {0,0,0};
5595 extent_buffer_get(c);
5598 c = btrfs_alloc_free_block(trans, root,
5599 btrfs_level_size(root, 0),
5600 root->root_key.objectid,
5601 &disk_key, level, 0, 0);
5604 extent_buffer_get(c);
5607 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
5608 btrfs_set_header_level(c, level);
5609 btrfs_set_header_bytenr(c, c->start);
5610 btrfs_set_header_generation(c, trans->transid);
5611 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
5612 btrfs_set_header_owner(c, root->root_key.objectid);
5614 write_extent_buffer(c, root->fs_info->fsid,
5615 btrfs_header_fsid(), BTRFS_FSID_SIZE);
5617 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
5618 (unsigned long)btrfs_header_chunk_tree_uuid(c),
5621 btrfs_mark_buffer_dirty(c);
5623 free_extent_buffer(old);
5625 add_root_to_dirty_list(root);
5629 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
5630 struct extent_buffer *eb, int tree_root)
5632 struct extent_buffer *tmp;
5633 struct btrfs_root_item *ri;
5634 struct btrfs_key key;
5637 int level = btrfs_header_level(eb);
5642 btrfs_pin_extent(fs_info, eb->start, eb->len);
5644 leafsize = btrfs_super_leafsize(fs_info->super_copy);
5645 nritems = btrfs_header_nritems(eb);
5646 for (i = 0; i < nritems; i++) {
5648 btrfs_item_key_to_cpu(eb, &key, i);
5649 if (key.type != BTRFS_ROOT_ITEM_KEY)
5651 /* Skip the extent root and reloc roots */
5652 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
5653 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
5654 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
5656 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
5657 bytenr = btrfs_disk_root_bytenr(eb, ri);
5660 * If at any point we start needing the real root we
5661 * will have to build a stump root for the root we are
5662 * in, but for now this doesn't actually use the root so
5663 * just pass in extent_root.
5665 tmp = read_tree_block(fs_info->extent_root, bytenr,
5668 fprintf(stderr, "Error reading root block\n");
5671 ret = pin_down_tree_blocks(fs_info, tmp, 0);
5672 free_extent_buffer(tmp);
5676 bytenr = btrfs_node_blockptr(eb, i);
5678 /* If we aren't the tree root don't read the block */
5679 if (level == 1 && !tree_root) {
5680 btrfs_pin_extent(fs_info, bytenr, leafsize);
5684 tmp = read_tree_block(fs_info->extent_root, bytenr,
5687 fprintf(stderr, "Error reading tree block\n");
5690 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
5691 free_extent_buffer(tmp);
5700 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
5704 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
5708 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
5711 static int reset_block_groups(struct btrfs_fs_info *fs_info)
5713 struct btrfs_path *path;
5714 struct extent_buffer *leaf;
5715 struct btrfs_chunk *chunk;
5716 struct btrfs_key key;
5719 path = btrfs_alloc_path();
5724 key.type = BTRFS_CHUNK_ITEM_KEY;
5727 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
5729 btrfs_free_path(path);
5734 * We do this in case the block groups were screwed up and had alloc
5735 * bits that aren't actually set on the chunks. This happens with
5736 * restored images every time and could happen in real life I guess.
5738 fs_info->avail_data_alloc_bits = 0;
5739 fs_info->avail_metadata_alloc_bits = 0;
5740 fs_info->avail_system_alloc_bits = 0;
5742 /* First we need to create the in-memory block groups */
5744 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5745 ret = btrfs_next_leaf(fs_info->chunk_root, path);
5747 btrfs_free_path(path);
5755 leaf = path->nodes[0];
5756 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5757 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
5762 chunk = btrfs_item_ptr(leaf, path->slots[0],
5763 struct btrfs_chunk);
5764 btrfs_add_block_group(fs_info, 0,
5765 btrfs_chunk_type(leaf, chunk),
5766 key.objectid, key.offset,
5767 btrfs_chunk_length(leaf, chunk));
5771 btrfs_free_path(path);
5775 static int reset_balance(struct btrfs_trans_handle *trans,
5776 struct btrfs_fs_info *fs_info)
5778 struct btrfs_root *root = fs_info->tree_root;
5779 struct btrfs_path *path;
5780 struct extent_buffer *leaf;
5781 struct btrfs_key key;
5782 int del_slot, del_nr = 0;
5786 path = btrfs_alloc_path();
5790 key.objectid = BTRFS_BALANCE_OBJECTID;
5791 key.type = BTRFS_BALANCE_ITEM_KEY;
5794 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5801 ret = btrfs_del_item(trans, root, path);
5804 btrfs_release_path(path);
5806 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5807 key.type = BTRFS_ROOT_ITEM_KEY;
5810 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5814 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5819 ret = btrfs_del_items(trans, root, path,
5826 btrfs_release_path(path);
5829 ret = btrfs_search_slot(trans, root, &key, path,
5836 leaf = path->nodes[0];
5837 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5838 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
5840 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5845 del_slot = path->slots[0];
5854 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
5858 btrfs_release_path(path);
5860 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5861 key.type = BTRFS_ROOT_ITEM_KEY;
5862 key.offset = (u64)-1;
5863 root = btrfs_read_fs_root(fs_info, &key);
5865 fprintf(stderr, "Error reading data reloc tree\n");
5866 return PTR_ERR(root);
5868 root->track_dirty = 1;
5869 if (root->last_trans != trans->transid) {
5870 root->last_trans = trans->transid;
5871 root->commit_root = root->node;
5872 extent_buffer_get(root->node);
5874 ret = btrfs_fsck_reinit_root(trans, root, 0);
5876 btrfs_free_path(path);
5880 static int reinit_extent_tree(struct btrfs_fs_info *fs_info)
5882 struct btrfs_trans_handle *trans;
5887 * The only reason we don't do this is because right now we're just
5888 * walking the trees we find and pinning down their bytes, we don't look
5889 * at any of the leaves. In order to do mixed groups we'd have to check
5890 * the leaves of any fs roots and pin down the bytes for any file
5891 * extents we find. Not hard but why do it if we don't have to?
5893 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
5894 fprintf(stderr, "We don't support re-initing the extent tree "
5895 "for mixed block groups yet, please notify a btrfs "
5896 "developer you want to do this so they can add this "
5897 "functionality.\n");
5901 trans = btrfs_start_transaction(fs_info->extent_root, 1);
5902 if (IS_ERR(trans)) {
5903 fprintf(stderr, "Error starting transaction\n");
5904 return PTR_ERR(trans);
5908 * first we need to walk all of the trees except the extent tree and pin
5909 * down the bytes that are in use so we don't overwrite any existing
5912 ret = pin_metadata_blocks(fs_info);
5914 fprintf(stderr, "error pinning down used bytes\n");
5919 * Need to drop all the block groups since we're going to recreate all
5922 btrfs_free_block_groups(fs_info);
5923 ret = reset_block_groups(fs_info);
5925 fprintf(stderr, "error resetting the block groups\n");
5929 /* Ok we can allocate now, reinit the extent root */
5930 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 1);
5932 fprintf(stderr, "extent root initialization failed\n");
5934 * When the transaction code is updated we should end the
5935 * transaction, but for now progs only knows about commit so
5936 * just return an error.
5941 ret = reset_balance(trans, fs_info);
5943 fprintf(stderr, "error reseting the pending balance\n");
5948 * Now we have all the in-memory block groups setup so we can make
5949 * allocations properly, and the metadata we care about is safe since we
5950 * pinned all of it above.
5953 struct btrfs_block_group_cache *cache;
5955 cache = btrfs_lookup_first_block_group(fs_info, start);
5958 start = cache->key.objectid + cache->key.offset;
5959 ret = btrfs_insert_item(trans, fs_info->extent_root,
5960 &cache->key, &cache->item,
5961 sizeof(cache->item));
5963 fprintf(stderr, "Error adding block group\n");
5966 btrfs_extent_post_op(trans, fs_info->extent_root);
5970 * Ok now we commit and run the normal fsck, which will add extent
5971 * entries for all of the items it finds.
5973 return btrfs_commit_transaction(trans, fs_info->extent_root);
5976 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
5978 struct btrfs_path *path;
5979 struct btrfs_trans_handle *trans;
5980 struct btrfs_key key;
5983 printf("Recowing metadata block %llu\n", eb->start);
5984 key.objectid = btrfs_header_owner(eb);
5985 key.type = BTRFS_ROOT_ITEM_KEY;
5986 key.offset = (u64)-1;
5988 root = btrfs_read_fs_root(root->fs_info, &key);
5990 fprintf(stderr, "Couldn't find owner root %llu\n",
5992 return PTR_ERR(root);
5995 path = btrfs_alloc_path();
5999 trans = btrfs_start_transaction(root, 1);
6000 if (IS_ERR(trans)) {
6001 btrfs_free_path(path);
6002 return PTR_ERR(trans);
6005 path->lowest_level = btrfs_header_level(eb);
6006 if (path->lowest_level)
6007 btrfs_node_key_to_cpu(eb, &key, 0);
6009 btrfs_item_key_to_cpu(eb, &key, 0);
6011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6012 btrfs_commit_transaction(trans, root);
6013 btrfs_free_path(path);
6017 static struct option long_options[] = {
6018 { "super", 1, NULL, 's' },
6019 { "repair", 0, NULL, 0 },
6020 { "init-csum-tree", 0, NULL, 0 },
6021 { "init-extent-tree", 0, NULL, 0 },
6022 { "backup", 0, NULL, 0 },
6026 const char * const cmd_check_usage[] = {
6027 "btrfs check [options] <device>",
6028 "Check an unmounted btrfs filesystem.",
6030 "-s|--super <superblock> use this superblock copy",
6031 "-b|--backup use the backup root copy",
6032 "--repair try to repair the filesystem",
6033 "--init-csum-tree create a new CRC tree",
6034 "--init-extent-tree create a new extent tree",
6038 int cmd_check(int argc, char **argv)
6040 struct cache_tree root_cache;
6041 struct btrfs_root *root;
6042 struct btrfs_fs_info *info;
6047 int option_index = 0;
6048 int init_csum_tree = 0;
6049 int init_extent_tree = 0;
6050 enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_PARTIAL;
6054 c = getopt_long(argc, argv, "as:b", long_options,
6059 case 'a': /* ignored */ break;
6061 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6065 bytenr = btrfs_sb_offset(num);
6066 printf("using SB copy %d, bytenr %llu\n", num,
6067 (unsigned long long)bytenr);
6071 usage(cmd_check_usage);
6073 if (option_index == 1) {
6074 printf("enabling repair mode\n");
6076 ctree_flags |= OPEN_CTREE_WRITES;
6077 } else if (option_index == 2) {
6078 printf("Creating a new CRC tree\n");
6080 ctree_flags |= OPEN_CTREE_WRITES;
6081 } else if (option_index == 3) {
6082 init_extent_tree = 1;
6083 ctree_flags |= (OPEN_CTREE_WRITES |
6084 OPEN_CTREE_NO_BLOCK_GROUPS);
6089 argc = argc - optind;
6092 usage(cmd_check_usage);
6095 cache_tree_init(&root_cache);
6097 if((ret = check_mounted(argv[optind])) < 0) {
6098 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6101 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6105 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6107 fprintf(stderr, "Couldn't open file system\n");
6111 uuid_unparse(info->super_copy->fsid, uuidbuf);
6112 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6114 if (!extent_buffer_uptodate(info->tree_root->node) ||
6115 !extent_buffer_uptodate(info->dev_root->node) ||
6116 !extent_buffer_uptodate(info->extent_root->node) ||
6117 !extent_buffer_uptodate(info->chunk_root->node)) {
6118 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6122 root = info->fs_root;
6124 if (init_extent_tree) {
6125 printf("Creating a new extent tree\n");
6126 ret = reinit_extent_tree(info);
6130 fprintf(stderr, "checking extents\n");
6131 if (init_csum_tree) {
6132 struct btrfs_trans_handle *trans;
6134 fprintf(stderr, "Reinit crc root\n");
6135 trans = btrfs_start_transaction(info->csum_root, 1);
6136 if (IS_ERR(trans)) {
6137 fprintf(stderr, "Error starting transaction\n");
6138 return PTR_ERR(trans);
6141 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6143 fprintf(stderr, "crc root initialization failed\n");
6147 ret = btrfs_commit_transaction(trans, info->csum_root);
6152 ret = check_chunks_and_extents(root);
6154 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6156 fprintf(stderr, "checking free space cache\n");
6157 ret = check_space_cache(root);
6161 fprintf(stderr, "checking fs roots\n");
6162 ret = check_fs_roots(root, &root_cache);
6166 fprintf(stderr, "checking csums\n");
6167 ret = check_csums(root);
6171 fprintf(stderr, "checking root refs\n");
6172 ret = check_root_refs(root, &root_cache);
6176 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
6177 struct extent_buffer *eb;
6179 eb = list_first_entry(&root->fs_info->recow_ebs,
6180 struct extent_buffer, recow);
6181 ret = recow_extent_buffer(root, eb);
6186 if (!list_empty(&root->fs_info->recow_ebs)) {
6187 fprintf(stderr, "Transid errors in file system\n");
6191 free_root_recs_tree(&root_cache);
6194 if (found_old_backref) { /*
6195 * there was a disk format change when mixed
6196 * backref was in testing tree. The old format
6197 * existed about one week.
6199 printf("\n * Found old mixed backref format. "
6200 "The old format is not supported! *"
6201 "\n * Please mount the FS in readonly mode, "
6202 "backup data and re-format the FS. *\n\n");
6205 printf("found %llu bytes used err is %d\n",
6206 (unsigned long long)bytes_used, ret);
6207 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
6208 printf("total tree bytes: %llu\n",
6209 (unsigned long long)total_btree_bytes);
6210 printf("total fs tree bytes: %llu\n",
6211 (unsigned long long)total_fs_tree_bytes);
6212 printf("total extent tree bytes: %llu\n",
6213 (unsigned long long)total_extent_tree_bytes);
6214 printf("btree space waste bytes: %llu\n",
6215 (unsigned long long)btree_space_waste);
6216 printf("file data blocks allocated: %llu\n referenced %llu\n",
6217 (unsigned long long)data_bytes_allocated,
6218 (unsigned long long)data_bytes_referenced);
6219 printf("%s\n", BTRFS_BUILD_VERSION);