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 LIST_HEAD(delete_items);
53 static int repair = 0;
54 static int no_holes = 0;
55 static int init_extent_tree = 0;
57 struct extent_backref {
58 struct list_head list;
59 unsigned int is_data:1;
60 unsigned int found_extent_tree:1;
61 unsigned int full_backref:1;
62 unsigned int found_ref:1;
63 unsigned int broken:1;
67 struct extent_backref node;
82 struct extent_backref node;
89 struct extent_record {
90 struct list_head backrefs;
91 struct list_head dups;
92 struct list_head list;
93 struct cache_extent cache;
94 struct btrfs_disk_key parent_key;
101 u64 parent_generation;
105 unsigned int found_rec:1;
106 unsigned int content_checked:1;
107 unsigned int owner_ref_checked:1;
108 unsigned int is_root:1;
109 unsigned int metadata:1;
112 struct inode_backref {
113 struct list_head list;
114 unsigned int found_dir_item:1;
115 unsigned int found_dir_index:1;
116 unsigned int found_inode_ref:1;
117 unsigned int filetype:8;
119 unsigned int ref_type;
126 struct dropping_root_item_record {
127 struct list_head list;
128 struct btrfs_root_item ri;
129 struct btrfs_key found_key;
132 #define REF_ERR_NO_DIR_ITEM (1 << 0)
133 #define REF_ERR_NO_DIR_INDEX (1 << 1)
134 #define REF_ERR_NO_INODE_REF (1 << 2)
135 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
136 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
137 #define REF_ERR_DUP_INODE_REF (1 << 5)
138 #define REF_ERR_INDEX_UNMATCH (1 << 6)
139 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
140 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
141 #define REF_ERR_NO_ROOT_REF (1 << 9)
142 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
143 #define REF_ERR_DUP_ROOT_REF (1 << 11)
144 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
146 struct inode_record {
147 struct list_head backrefs;
148 unsigned int checked:1;
149 unsigned int merging:1;
150 unsigned int found_inode_item:1;
151 unsigned int found_dir_item:1;
152 unsigned int found_file_extent:1;
153 unsigned int found_csum_item:1;
154 unsigned int some_csum_missing:1;
155 unsigned int nodatasum:1;
168 u64 first_extent_gap;
173 #define I_ERR_NO_INODE_ITEM (1 << 0)
174 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
175 #define I_ERR_DUP_INODE_ITEM (1 << 2)
176 #define I_ERR_DUP_DIR_INDEX (1 << 3)
177 #define I_ERR_ODD_DIR_ITEM (1 << 4)
178 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
179 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
180 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
181 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
182 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
183 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
184 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
185 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
186 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
188 struct root_backref {
189 struct list_head list;
190 unsigned int found_dir_item:1;
191 unsigned int found_dir_index:1;
192 unsigned int found_back_ref:1;
193 unsigned int found_forward_ref:1;
194 unsigned int reachable:1;
204 struct list_head backrefs;
205 struct cache_extent cache;
206 unsigned int found_root_item:1;
212 struct cache_extent cache;
217 struct cache_extent cache;
218 struct cache_tree root_cache;
219 struct cache_tree inode_cache;
220 struct inode_record *current;
229 struct walk_control {
230 struct cache_tree shared;
231 struct shared_node *nodes[BTRFS_MAX_LEVEL];
237 struct btrfs_key key;
239 struct list_head list;
242 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
244 static u8 imode_to_type(u32 imode)
247 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
248 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
249 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
250 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
251 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
252 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
253 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
254 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
257 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
261 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
263 struct device_record *rec1;
264 struct device_record *rec2;
266 rec1 = rb_entry(node1, struct device_record, node);
267 rec2 = rb_entry(node2, struct device_record, node);
268 if (rec1->devid > rec2->devid)
270 else if (rec1->devid < rec2->devid)
276 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
278 struct inode_record *rec;
279 struct inode_backref *backref;
280 struct inode_backref *orig;
283 rec = malloc(sizeof(*rec));
284 memcpy(rec, orig_rec, sizeof(*rec));
286 INIT_LIST_HEAD(&rec->backrefs);
288 list_for_each_entry(orig, &orig_rec->backrefs, list) {
289 size = sizeof(*orig) + orig->namelen + 1;
290 backref = malloc(size);
291 memcpy(backref, orig, size);
292 list_add_tail(&backref->list, &rec->backrefs);
297 static void print_inode_error(int errors)
299 if (errors & I_ERR_NO_INODE_ITEM)
300 fprintf(stderr, ", no inode item");
301 if (errors & I_ERR_NO_ORPHAN_ITEM)
302 fprintf(stderr, ", no orphan item");
303 if (errors & I_ERR_DUP_INODE_ITEM)
304 fprintf(stderr, ", dup inode item");
305 if (errors & I_ERR_DUP_DIR_INDEX)
306 fprintf(stderr, ", dup dir index");
307 if (errors & I_ERR_ODD_DIR_ITEM)
308 fprintf(stderr, ", odd dir item");
309 if (errors & I_ERR_ODD_FILE_EXTENT)
310 fprintf(stderr, ", odd file extent");
311 if (errors & I_ERR_BAD_FILE_EXTENT)
312 fprintf(stderr, ", bad file extent");
313 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
314 fprintf(stderr, ", file extent overlap");
315 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
316 fprintf(stderr, ", file extent discount");
317 if (errors & I_ERR_DIR_ISIZE_WRONG)
318 fprintf(stderr, ", dir isize wrong");
319 if (errors & I_ERR_FILE_NBYTES_WRONG)
320 fprintf(stderr, ", nbytes wrong");
321 if (errors & I_ERR_ODD_CSUM_ITEM)
322 fprintf(stderr, ", odd csum item");
323 if (errors & I_ERR_SOME_CSUM_MISSING)
324 fprintf(stderr, ", some csum missing");
325 if (errors & I_ERR_LINK_COUNT_WRONG)
326 fprintf(stderr, ", link count wrong");
327 fprintf(stderr, "\n");
330 static void print_ref_error(int errors)
332 if (errors & REF_ERR_NO_DIR_ITEM)
333 fprintf(stderr, ", no dir item");
334 if (errors & REF_ERR_NO_DIR_INDEX)
335 fprintf(stderr, ", no dir index");
336 if (errors & REF_ERR_NO_INODE_REF)
337 fprintf(stderr, ", no inode ref");
338 if (errors & REF_ERR_DUP_DIR_ITEM)
339 fprintf(stderr, ", dup dir item");
340 if (errors & REF_ERR_DUP_DIR_INDEX)
341 fprintf(stderr, ", dup dir index");
342 if (errors & REF_ERR_DUP_INODE_REF)
343 fprintf(stderr, ", dup inode ref");
344 if (errors & REF_ERR_INDEX_UNMATCH)
345 fprintf(stderr, ", index unmatch");
346 if (errors & REF_ERR_FILETYPE_UNMATCH)
347 fprintf(stderr, ", filetype unmatch");
348 if (errors & REF_ERR_NAME_TOO_LONG)
349 fprintf(stderr, ", name too long");
350 if (errors & REF_ERR_NO_ROOT_REF)
351 fprintf(stderr, ", no root ref");
352 if (errors & REF_ERR_NO_ROOT_BACKREF)
353 fprintf(stderr, ", no root backref");
354 if (errors & REF_ERR_DUP_ROOT_REF)
355 fprintf(stderr, ", dup root ref");
356 if (errors & REF_ERR_DUP_ROOT_BACKREF)
357 fprintf(stderr, ", dup root backref");
358 fprintf(stderr, "\n");
361 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
364 struct ptr_node *node;
365 struct cache_extent *cache;
366 struct inode_record *rec = NULL;
369 cache = lookup_cache_extent(inode_cache, ino, 1);
371 node = container_of(cache, struct ptr_node, cache);
373 if (mod && rec->refs > 1) {
374 node->data = clone_inode_rec(rec);
379 rec = calloc(1, sizeof(*rec));
381 rec->extent_start = (u64)-1;
382 rec->first_extent_gap = (u64)-1;
384 INIT_LIST_HEAD(&rec->backrefs);
386 node = malloc(sizeof(*node));
387 node->cache.start = ino;
388 node->cache.size = 1;
391 if (ino == BTRFS_FREE_INO_OBJECTID)
394 ret = insert_cache_extent(inode_cache, &node->cache);
400 static void free_inode_rec(struct inode_record *rec)
402 struct inode_backref *backref;
407 while (!list_empty(&rec->backrefs)) {
408 backref = list_entry(rec->backrefs.next,
409 struct inode_backref, list);
410 list_del(&backref->list);
416 static int can_free_inode_rec(struct inode_record *rec)
418 if (!rec->errors && rec->checked && rec->found_inode_item &&
419 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
424 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
425 struct inode_record *rec)
427 struct cache_extent *cache;
428 struct inode_backref *tmp, *backref;
429 struct ptr_node *node;
430 unsigned char filetype;
432 if (!rec->found_inode_item)
435 filetype = imode_to_type(rec->imode);
436 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
437 if (backref->found_dir_item && backref->found_dir_index) {
438 if (backref->filetype != filetype)
439 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
440 if (!backref->errors && backref->found_inode_ref) {
441 list_del(&backref->list);
447 if (!rec->checked || rec->merging)
450 if (S_ISDIR(rec->imode)) {
451 if (rec->found_size != rec->isize)
452 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
453 if (rec->found_file_extent)
454 rec->errors |= I_ERR_ODD_FILE_EXTENT;
455 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
456 if (rec->found_dir_item)
457 rec->errors |= I_ERR_ODD_DIR_ITEM;
458 if (rec->found_size != rec->nbytes)
459 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
460 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
461 rec->first_extent_gap = 0;
462 if (rec->nlink > 0 && !no_holes &&
463 (rec->extent_end < rec->isize ||
464 rec->first_extent_gap < rec->isize))
465 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
468 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
469 if (rec->found_csum_item && rec->nodatasum)
470 rec->errors |= I_ERR_ODD_CSUM_ITEM;
471 if (rec->some_csum_missing && !rec->nodatasum)
472 rec->errors |= I_ERR_SOME_CSUM_MISSING;
475 BUG_ON(rec->refs != 1);
476 if (can_free_inode_rec(rec)) {
477 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
478 node = container_of(cache, struct ptr_node, cache);
479 BUG_ON(node->data != rec);
480 remove_cache_extent(inode_cache, &node->cache);
486 static int check_orphan_item(struct btrfs_root *root, u64 ino)
488 struct btrfs_path path;
489 struct btrfs_key key;
492 key.objectid = BTRFS_ORPHAN_OBJECTID;
493 key.type = BTRFS_ORPHAN_ITEM_KEY;
496 btrfs_init_path(&path);
497 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
498 btrfs_release_path(&path);
504 static int process_inode_item(struct extent_buffer *eb,
505 int slot, struct btrfs_key *key,
506 struct shared_node *active_node)
508 struct inode_record *rec;
509 struct btrfs_inode_item *item;
511 rec = active_node->current;
512 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
513 if (rec->found_inode_item) {
514 rec->errors |= I_ERR_DUP_INODE_ITEM;
517 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
518 rec->nlink = btrfs_inode_nlink(eb, item);
519 rec->isize = btrfs_inode_size(eb, item);
520 rec->nbytes = btrfs_inode_nbytes(eb, item);
521 rec->imode = btrfs_inode_mode(eb, item);
522 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
524 rec->found_inode_item = 1;
526 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
527 maybe_free_inode_rec(&active_node->inode_cache, rec);
531 static struct inode_backref *get_inode_backref(struct inode_record *rec,
533 int namelen, u64 dir)
535 struct inode_backref *backref;
537 list_for_each_entry(backref, &rec->backrefs, list) {
538 if (backref->dir != dir || backref->namelen != namelen)
540 if (memcmp(name, backref->name, namelen))
545 backref = malloc(sizeof(*backref) + namelen + 1);
546 memset(backref, 0, sizeof(*backref));
548 backref->namelen = namelen;
549 memcpy(backref->name, name, namelen);
550 backref->name[namelen] = '\0';
551 list_add_tail(&backref->list, &rec->backrefs);
555 static int add_inode_backref(struct cache_tree *inode_cache,
556 u64 ino, u64 dir, u64 index,
557 const char *name, int namelen,
558 int filetype, int itemtype, int errors)
560 struct inode_record *rec;
561 struct inode_backref *backref;
563 rec = get_inode_rec(inode_cache, ino, 1);
564 backref = get_inode_backref(rec, name, namelen, dir);
566 backref->errors |= errors;
567 if (itemtype == BTRFS_DIR_INDEX_KEY) {
568 if (backref->found_dir_index)
569 backref->errors |= REF_ERR_DUP_DIR_INDEX;
570 if (backref->found_inode_ref && backref->index != index)
571 backref->errors |= REF_ERR_INDEX_UNMATCH;
572 if (backref->found_dir_item && backref->filetype != filetype)
573 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
575 backref->index = index;
576 backref->filetype = filetype;
577 backref->found_dir_index = 1;
578 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
580 if (backref->found_dir_item)
581 backref->errors |= REF_ERR_DUP_DIR_ITEM;
582 if (backref->found_dir_index && backref->filetype != filetype)
583 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
585 backref->filetype = filetype;
586 backref->found_dir_item = 1;
587 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
588 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
589 if (backref->found_inode_ref)
590 backref->errors |= REF_ERR_DUP_INODE_REF;
591 if (backref->found_dir_index && backref->index != index)
592 backref->errors |= REF_ERR_INDEX_UNMATCH;
594 backref->ref_type = itemtype;
595 backref->index = index;
596 backref->found_inode_ref = 1;
601 maybe_free_inode_rec(inode_cache, rec);
605 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
606 struct cache_tree *dst_cache)
608 struct inode_backref *backref;
612 list_for_each_entry(backref, &src->backrefs, list) {
613 if (backref->found_dir_index) {
614 add_inode_backref(dst_cache, dst->ino, backref->dir,
615 backref->index, backref->name,
616 backref->namelen, backref->filetype,
617 BTRFS_DIR_INDEX_KEY, backref->errors);
619 if (backref->found_dir_item) {
621 add_inode_backref(dst_cache, dst->ino,
622 backref->dir, 0, backref->name,
623 backref->namelen, backref->filetype,
624 BTRFS_DIR_ITEM_KEY, backref->errors);
626 if (backref->found_inode_ref) {
627 add_inode_backref(dst_cache, dst->ino,
628 backref->dir, backref->index,
629 backref->name, backref->namelen, 0,
630 backref->ref_type, backref->errors);
634 if (src->found_dir_item)
635 dst->found_dir_item = 1;
636 if (src->found_file_extent)
637 dst->found_file_extent = 1;
638 if (src->found_csum_item)
639 dst->found_csum_item = 1;
640 if (src->some_csum_missing)
641 dst->some_csum_missing = 1;
642 if (dst->first_extent_gap > src->first_extent_gap)
643 dst->first_extent_gap = src->first_extent_gap;
645 BUG_ON(src->found_link < dir_count);
646 dst->found_link += src->found_link - dir_count;
647 dst->found_size += src->found_size;
648 if (src->extent_start != (u64)-1) {
649 if (dst->extent_start == (u64)-1) {
650 dst->extent_start = src->extent_start;
651 dst->extent_end = src->extent_end;
653 if (dst->extent_end > src->extent_start)
654 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
655 else if (dst->extent_end < src->extent_start &&
656 dst->extent_end < dst->first_extent_gap)
657 dst->first_extent_gap = dst->extent_end;
658 if (dst->extent_end < src->extent_end)
659 dst->extent_end = src->extent_end;
663 dst->errors |= src->errors;
664 if (src->found_inode_item) {
665 if (!dst->found_inode_item) {
666 dst->nlink = src->nlink;
667 dst->isize = src->isize;
668 dst->nbytes = src->nbytes;
669 dst->imode = src->imode;
670 dst->nodatasum = src->nodatasum;
671 dst->found_inode_item = 1;
673 dst->errors |= I_ERR_DUP_INODE_ITEM;
681 static int splice_shared_node(struct shared_node *src_node,
682 struct shared_node *dst_node)
684 struct cache_extent *cache;
685 struct ptr_node *node, *ins;
686 struct cache_tree *src, *dst;
687 struct inode_record *rec, *conflict;
692 if (--src_node->refs == 0)
694 if (src_node->current)
695 current_ino = src_node->current->ino;
697 src = &src_node->root_cache;
698 dst = &dst_node->root_cache;
700 cache = search_cache_extent(src, 0);
702 node = container_of(cache, struct ptr_node, cache);
704 cache = next_cache_extent(cache);
707 remove_cache_extent(src, &node->cache);
710 ins = malloc(sizeof(*ins));
711 ins->cache.start = node->cache.start;
712 ins->cache.size = node->cache.size;
716 ret = insert_cache_extent(dst, &ins->cache);
717 if (ret == -EEXIST) {
718 conflict = get_inode_rec(dst, rec->ino, 1);
719 merge_inode_recs(rec, conflict, dst);
721 conflict->checked = 1;
722 if (dst_node->current == conflict)
723 dst_node->current = NULL;
725 maybe_free_inode_rec(dst, conflict);
733 if (src == &src_node->root_cache) {
734 src = &src_node->inode_cache;
735 dst = &dst_node->inode_cache;
739 if (current_ino > 0 && (!dst_node->current ||
740 current_ino > dst_node->current->ino)) {
741 if (dst_node->current) {
742 dst_node->current->checked = 1;
743 maybe_free_inode_rec(dst, dst_node->current);
745 dst_node->current = get_inode_rec(dst, current_ino, 1);
750 static void free_inode_ptr(struct cache_extent *cache)
752 struct ptr_node *node;
753 struct inode_record *rec;
755 node = container_of(cache, struct ptr_node, cache);
761 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
763 static struct shared_node *find_shared_node(struct cache_tree *shared,
766 struct cache_extent *cache;
767 struct shared_node *node;
769 cache = lookup_cache_extent(shared, bytenr, 1);
771 node = container_of(cache, struct shared_node, cache);
777 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
780 struct shared_node *node;
782 node = calloc(1, sizeof(*node));
783 node->cache.start = bytenr;
784 node->cache.size = 1;
785 cache_tree_init(&node->root_cache);
786 cache_tree_init(&node->inode_cache);
789 ret = insert_cache_extent(shared, &node->cache);
794 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
795 struct walk_control *wc, int level)
797 struct shared_node *node;
798 struct shared_node *dest;
800 if (level == wc->active_node)
803 BUG_ON(wc->active_node <= level);
804 node = find_shared_node(&wc->shared, bytenr);
806 add_shared_node(&wc->shared, bytenr, refs);
807 node = find_shared_node(&wc->shared, bytenr);
808 wc->nodes[level] = node;
809 wc->active_node = level;
813 if (wc->root_level == wc->active_node &&
814 btrfs_root_refs(&root->root_item) == 0) {
815 if (--node->refs == 0) {
816 free_inode_recs_tree(&node->root_cache);
817 free_inode_recs_tree(&node->inode_cache);
818 remove_cache_extent(&wc->shared, &node->cache);
824 dest = wc->nodes[wc->active_node];
825 splice_shared_node(node, dest);
826 if (node->refs == 0) {
827 remove_cache_extent(&wc->shared, &node->cache);
833 static int leave_shared_node(struct btrfs_root *root,
834 struct walk_control *wc, int level)
836 struct shared_node *node;
837 struct shared_node *dest;
840 if (level == wc->root_level)
843 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
847 BUG_ON(i >= BTRFS_MAX_LEVEL);
849 node = wc->nodes[wc->active_node];
850 wc->nodes[wc->active_node] = NULL;
853 dest = wc->nodes[wc->active_node];
854 if (wc->active_node < wc->root_level ||
855 btrfs_root_refs(&root->root_item) > 0) {
856 BUG_ON(node->refs <= 1);
857 splice_shared_node(node, dest);
859 BUG_ON(node->refs < 2);
865 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
868 struct btrfs_path path;
869 struct btrfs_key key;
870 struct extent_buffer *leaf;
874 btrfs_init_path(&path);
876 key.objectid = parent_root_id;
877 key.type = BTRFS_ROOT_REF_KEY;
878 key.offset = child_root_id;
879 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
882 btrfs_release_path(&path);
886 key.objectid = child_root_id;
887 key.type = BTRFS_ROOT_BACKREF_KEY;
889 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
894 leaf = path.nodes[0];
895 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
896 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
901 leaf = path.nodes[0];
904 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
905 if (key.objectid != child_root_id ||
906 key.type != BTRFS_ROOT_BACKREF_KEY)
911 if (key.offset == parent_root_id) {
912 btrfs_release_path(&path);
919 btrfs_release_path(&path);
920 return has_parent? 0 : -1;
923 static int process_dir_item(struct btrfs_root *root,
924 struct extent_buffer *eb,
925 int slot, struct btrfs_key *key,
926 struct shared_node *active_node)
936 struct btrfs_dir_item *di;
937 struct inode_record *rec;
938 struct cache_tree *root_cache;
939 struct cache_tree *inode_cache;
940 struct btrfs_key location;
941 char namebuf[BTRFS_NAME_LEN];
943 root_cache = &active_node->root_cache;
944 inode_cache = &active_node->inode_cache;
945 rec = active_node->current;
946 rec->found_dir_item = 1;
948 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
949 total = btrfs_item_size_nr(eb, slot);
950 while (cur < total) {
952 btrfs_dir_item_key_to_cpu(eb, di, &location);
953 name_len = btrfs_dir_name_len(eb, di);
954 data_len = btrfs_dir_data_len(eb, di);
955 filetype = btrfs_dir_type(eb, di);
957 rec->found_size += name_len;
958 if (name_len <= BTRFS_NAME_LEN) {
962 len = BTRFS_NAME_LEN;
963 error = REF_ERR_NAME_TOO_LONG;
965 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
967 if (location.type == BTRFS_INODE_ITEM_KEY) {
968 add_inode_backref(inode_cache, location.objectid,
969 key->objectid, key->offset, namebuf,
970 len, filetype, key->type, error);
971 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
972 add_inode_backref(root_cache, location.objectid,
973 key->objectid, key->offset,
974 namebuf, len, filetype,
977 fprintf(stderr, "warning line %d\n", __LINE__);
980 len = sizeof(*di) + name_len + data_len;
981 di = (struct btrfs_dir_item *)((char *)di + len);
984 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
985 rec->errors |= I_ERR_DUP_DIR_INDEX;
990 static int process_inode_ref(struct extent_buffer *eb,
991 int slot, struct btrfs_key *key,
992 struct shared_node *active_node)
1000 struct cache_tree *inode_cache;
1001 struct btrfs_inode_ref *ref;
1002 char namebuf[BTRFS_NAME_LEN];
1004 inode_cache = &active_node->inode_cache;
1006 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1007 total = btrfs_item_size_nr(eb, slot);
1008 while (cur < total) {
1009 name_len = btrfs_inode_ref_name_len(eb, ref);
1010 index = btrfs_inode_ref_index(eb, ref);
1011 if (name_len <= BTRFS_NAME_LEN) {
1015 len = BTRFS_NAME_LEN;
1016 error = REF_ERR_NAME_TOO_LONG;
1018 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1019 add_inode_backref(inode_cache, key->objectid, key->offset,
1020 index, namebuf, len, 0, key->type, error);
1022 len = sizeof(*ref) + name_len;
1023 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1029 static int process_inode_extref(struct extent_buffer *eb,
1030 int slot, struct btrfs_key *key,
1031 struct shared_node *active_node)
1040 struct cache_tree *inode_cache;
1041 struct btrfs_inode_extref *extref;
1042 char namebuf[BTRFS_NAME_LEN];
1044 inode_cache = &active_node->inode_cache;
1046 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1047 total = btrfs_item_size_nr(eb, slot);
1048 while (cur < total) {
1049 name_len = btrfs_inode_extref_name_len(eb, extref);
1050 index = btrfs_inode_extref_index(eb, extref);
1051 parent = btrfs_inode_extref_parent(eb, extref);
1052 if (name_len <= BTRFS_NAME_LEN) {
1056 len = BTRFS_NAME_LEN;
1057 error = REF_ERR_NAME_TOO_LONG;
1059 read_extent_buffer(eb, namebuf,
1060 (unsigned long)(extref + 1), len);
1061 add_inode_backref(inode_cache, key->objectid, parent,
1062 index, namebuf, len, 0, key->type, error);
1064 len = sizeof(*extref) + name_len;
1065 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1072 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
1074 struct btrfs_key key;
1075 struct btrfs_path path;
1076 struct extent_buffer *leaf;
1081 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1083 btrfs_init_path(&path);
1085 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1087 key.type = BTRFS_EXTENT_CSUM_KEY;
1089 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1092 if (ret > 0 && path.slots[0] > 0) {
1093 leaf = path.nodes[0];
1094 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1095 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1096 key.type == BTRFS_EXTENT_CSUM_KEY)
1101 leaf = path.nodes[0];
1102 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1103 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1107 leaf = path.nodes[0];
1110 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1111 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1112 key.type != BTRFS_EXTENT_CSUM_KEY)
1115 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1116 if (key.offset >= start + len)
1119 if (key.offset > start)
1122 size = btrfs_item_size_nr(leaf, path.slots[0]);
1123 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1124 if (csum_end > start) {
1125 size = min(csum_end - start, len);
1133 btrfs_release_path(&path);
1137 static int process_file_extent(struct btrfs_root *root,
1138 struct extent_buffer *eb,
1139 int slot, struct btrfs_key *key,
1140 struct shared_node *active_node)
1142 struct inode_record *rec;
1143 struct btrfs_file_extent_item *fi;
1145 u64 disk_bytenr = 0;
1146 u64 extent_offset = 0;
1147 u64 mask = root->sectorsize - 1;
1150 rec = active_node->current;
1151 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1152 rec->found_file_extent = 1;
1154 if (rec->extent_start == (u64)-1) {
1155 rec->extent_start = key->offset;
1156 rec->extent_end = key->offset;
1159 if (rec->extent_end > key->offset)
1160 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1161 else if (rec->extent_end < key->offset &&
1162 rec->extent_end < rec->first_extent_gap)
1163 rec->first_extent_gap = rec->extent_end;
1165 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1166 extent_type = btrfs_file_extent_type(eb, fi);
1168 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1169 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1171 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1172 rec->found_size += num_bytes;
1173 num_bytes = (num_bytes + mask) & ~mask;
1174 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1175 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1176 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1177 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1178 extent_offset = btrfs_file_extent_offset(eb, fi);
1179 if (num_bytes == 0 || (num_bytes & mask))
1180 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1181 if (num_bytes + extent_offset >
1182 btrfs_file_extent_ram_bytes(eb, fi))
1183 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1184 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1185 (btrfs_file_extent_compression(eb, fi) ||
1186 btrfs_file_extent_encryption(eb, fi) ||
1187 btrfs_file_extent_other_encoding(eb, fi)))
1188 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1189 if (disk_bytenr > 0)
1190 rec->found_size += num_bytes;
1192 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1194 rec->extent_end = key->offset + num_bytes;
1196 if (disk_bytenr > 0) {
1198 if (btrfs_file_extent_compression(eb, fi))
1199 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1201 disk_bytenr += extent_offset;
1203 found = count_csum_range(root, disk_bytenr, num_bytes);
1204 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1206 rec->found_csum_item = 1;
1207 if (found < num_bytes)
1208 rec->some_csum_missing = 1;
1209 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1211 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1217 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1218 struct walk_control *wc)
1220 struct btrfs_key key;
1225 struct cache_tree *inode_cache;
1226 struct shared_node *active_node;
1228 if (wc->root_level == wc->active_node &&
1229 btrfs_root_refs(&root->root_item) == 0)
1232 active_node = wc->nodes[wc->active_node];
1233 inode_cache = &active_node->inode_cache;
1234 nritems = btrfs_header_nritems(eb);
1235 for (i = 0; i < nritems; i++) {
1236 btrfs_item_key_to_cpu(eb, &key, i);
1238 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1240 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1243 if (active_node->current == NULL ||
1244 active_node->current->ino < key.objectid) {
1245 if (active_node->current) {
1246 active_node->current->checked = 1;
1247 maybe_free_inode_rec(inode_cache,
1248 active_node->current);
1250 active_node->current = get_inode_rec(inode_cache,
1254 case BTRFS_DIR_ITEM_KEY:
1255 case BTRFS_DIR_INDEX_KEY:
1256 ret = process_dir_item(root, eb, i, &key, active_node);
1258 case BTRFS_INODE_REF_KEY:
1259 ret = process_inode_ref(eb, i, &key, active_node);
1261 case BTRFS_INODE_EXTREF_KEY:
1262 ret = process_inode_extref(eb, i, &key, active_node);
1264 case BTRFS_INODE_ITEM_KEY:
1265 ret = process_inode_item(eb, i, &key, active_node);
1267 case BTRFS_EXTENT_DATA_KEY:
1268 ret = process_file_extent(root, eb, i, &key,
1280 static void reada_walk_down(struct btrfs_root *root,
1281 struct extent_buffer *node, int slot)
1291 level = btrfs_header_level(node);
1295 nritems = btrfs_header_nritems(node);
1296 blocksize = btrfs_level_size(root, level - 1);
1297 for (i = slot; i < nritems; i++) {
1298 bytenr = btrfs_node_blockptr(node, i);
1299 ptr_gen = btrfs_node_ptr_generation(node, i);
1300 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1306 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1307 struct walk_control *wc, int *level)
1311 struct extent_buffer *next;
1312 struct extent_buffer *cur;
1317 WARN_ON(*level < 0);
1318 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1319 ret = btrfs_lookup_extent_info(NULL, root,
1320 path->nodes[*level]->start,
1321 *level, 1, &refs, NULL);
1328 ret = enter_shared_node(root, path->nodes[*level]->start,
1336 while (*level >= 0) {
1337 WARN_ON(*level < 0);
1338 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1339 cur = path->nodes[*level];
1341 if (btrfs_header_level(cur) != *level)
1344 if (path->slots[*level] >= btrfs_header_nritems(cur))
1347 ret = process_one_leaf(root, cur, wc);
1350 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1351 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1352 blocksize = btrfs_level_size(root, *level - 1);
1353 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1359 ret = enter_shared_node(root, bytenr, refs,
1362 path->slots[*level]++;
1367 next = btrfs_find_tree_block(root, bytenr, blocksize);
1368 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1369 free_extent_buffer(next);
1370 reada_walk_down(root, cur, path->slots[*level]);
1371 next = read_tree_block(root, bytenr, blocksize,
1379 *level = *level - 1;
1380 free_extent_buffer(path->nodes[*level]);
1381 path->nodes[*level] = next;
1382 path->slots[*level] = 0;
1385 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1389 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1390 struct walk_control *wc, int *level)
1393 struct extent_buffer *leaf;
1395 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1396 leaf = path->nodes[i];
1397 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1402 free_extent_buffer(path->nodes[*level]);
1403 path->nodes[*level] = NULL;
1404 BUG_ON(*level > wc->active_node);
1405 if (*level == wc->active_node)
1406 leave_shared_node(root, wc, *level);
1413 static int check_root_dir(struct inode_record *rec)
1415 struct inode_backref *backref;
1418 if (!rec->found_inode_item || rec->errors)
1420 if (rec->nlink != 1 || rec->found_link != 0)
1422 if (list_empty(&rec->backrefs))
1424 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1425 if (!backref->found_inode_ref)
1427 if (backref->index != 0 || backref->namelen != 2 ||
1428 memcmp(backref->name, "..", 2))
1430 if (backref->found_dir_index || backref->found_dir_item)
1437 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1438 struct btrfs_root *root, struct btrfs_path *path,
1439 struct inode_record *rec)
1441 struct btrfs_inode_item *ei;
1442 struct btrfs_key key;
1445 key.objectid = rec->ino;
1446 key.type = BTRFS_INODE_ITEM_KEY;
1447 key.offset = (u64)-1;
1449 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1453 if (!path->slots[0]) {
1460 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1461 if (key.objectid != rec->ino) {
1466 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1467 struct btrfs_inode_item);
1468 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1469 btrfs_mark_buffer_dirty(path->nodes[0]);
1470 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1471 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1472 root->root_key.objectid);
1474 btrfs_release_path(path);
1478 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1479 struct btrfs_root *root,
1480 struct btrfs_path *path,
1481 struct inode_record *rec)
1483 struct btrfs_key key;
1486 key.objectid = BTRFS_ORPHAN_OBJECTID;
1487 key.type = BTRFS_ORPHAN_ITEM_KEY;
1488 key.offset = rec->ino;
1490 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1491 btrfs_release_path(path);
1493 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1497 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1499 struct btrfs_trans_handle *trans;
1500 struct btrfs_path *path;
1503 /* So far we just fix dir isize wrong */
1504 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1507 path = btrfs_alloc_path();
1511 trans = btrfs_start_transaction(root, 1);
1512 if (IS_ERR(trans)) {
1513 btrfs_free_path(path);
1514 return PTR_ERR(trans);
1517 if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
1518 ret = repair_inode_isize(trans, root, path, rec);
1519 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
1520 ret = repair_inode_orphan_item(trans, root, path, rec);
1521 btrfs_commit_transaction(trans, root);
1522 btrfs_free_path(path);
1526 static int check_inode_recs(struct btrfs_root *root,
1527 struct cache_tree *inode_cache)
1529 struct cache_extent *cache;
1530 struct ptr_node *node;
1531 struct inode_record *rec;
1532 struct inode_backref *backref;
1535 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1537 if (btrfs_root_refs(&root->root_item) == 0) {
1538 if (!cache_tree_empty(inode_cache))
1539 fprintf(stderr, "warning line %d\n", __LINE__);
1543 rec = get_inode_rec(inode_cache, root_dirid, 0);
1545 ret = check_root_dir(rec);
1547 fprintf(stderr, "root %llu root dir %llu error\n",
1548 (unsigned long long)root->root_key.objectid,
1549 (unsigned long long)root_dirid);
1553 fprintf(stderr, "root %llu root dir %llu not found\n",
1554 (unsigned long long)root->root_key.objectid,
1555 (unsigned long long)root_dirid);
1559 cache = search_cache_extent(inode_cache, 0);
1562 node = container_of(cache, struct ptr_node, cache);
1564 remove_cache_extent(inode_cache, &node->cache);
1566 if (rec->ino == root_dirid ||
1567 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1568 free_inode_rec(rec);
1572 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1573 ret = check_orphan_item(root, rec->ino);
1575 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1576 if (can_free_inode_rec(rec)) {
1577 free_inode_rec(rec);
1583 ret = try_repair_inode(root, rec);
1584 if (ret == 0 && can_free_inode_rec(rec)) {
1585 free_inode_rec(rec);
1592 if (!rec->found_inode_item)
1593 rec->errors |= I_ERR_NO_INODE_ITEM;
1594 if (rec->found_link != rec->nlink)
1595 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1596 fprintf(stderr, "root %llu inode %llu errors %x",
1597 (unsigned long long) root->root_key.objectid,
1598 (unsigned long long) rec->ino, rec->errors);
1599 print_inode_error(rec->errors);
1600 list_for_each_entry(backref, &rec->backrefs, list) {
1601 if (!backref->found_dir_item)
1602 backref->errors |= REF_ERR_NO_DIR_ITEM;
1603 if (!backref->found_dir_index)
1604 backref->errors |= REF_ERR_NO_DIR_INDEX;
1605 if (!backref->found_inode_ref)
1606 backref->errors |= REF_ERR_NO_INODE_REF;
1607 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1608 " namelen %u name %s filetype %d errors %x",
1609 (unsigned long long)backref->dir,
1610 (unsigned long long)backref->index,
1611 backref->namelen, backref->name,
1612 backref->filetype, backref->errors);
1613 print_ref_error(backref->errors);
1615 free_inode_rec(rec);
1617 return (error > 0) ? -1 : 0;
1620 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1623 struct cache_extent *cache;
1624 struct root_record *rec = NULL;
1627 cache = lookup_cache_extent(root_cache, objectid, 1);
1629 rec = container_of(cache, struct root_record, cache);
1631 rec = calloc(1, sizeof(*rec));
1632 rec->objectid = objectid;
1633 INIT_LIST_HEAD(&rec->backrefs);
1634 rec->cache.start = objectid;
1635 rec->cache.size = 1;
1637 ret = insert_cache_extent(root_cache, &rec->cache);
1643 static struct root_backref *get_root_backref(struct root_record *rec,
1644 u64 ref_root, u64 dir, u64 index,
1645 const char *name, int namelen)
1647 struct root_backref *backref;
1649 list_for_each_entry(backref, &rec->backrefs, list) {
1650 if (backref->ref_root != ref_root || backref->dir != dir ||
1651 backref->namelen != namelen)
1653 if (memcmp(name, backref->name, namelen))
1658 backref = malloc(sizeof(*backref) + namelen + 1);
1659 memset(backref, 0, sizeof(*backref));
1660 backref->ref_root = ref_root;
1662 backref->index = index;
1663 backref->namelen = namelen;
1664 memcpy(backref->name, name, namelen);
1665 backref->name[namelen] = '\0';
1666 list_add_tail(&backref->list, &rec->backrefs);
1670 static void free_root_record(struct cache_extent *cache)
1672 struct root_record *rec;
1673 struct root_backref *backref;
1675 rec = container_of(cache, struct root_record, cache);
1676 while (!list_empty(&rec->backrefs)) {
1677 backref = list_entry(rec->backrefs.next,
1678 struct root_backref, list);
1679 list_del(&backref->list);
1686 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
1688 static int add_root_backref(struct cache_tree *root_cache,
1689 u64 root_id, u64 ref_root, u64 dir, u64 index,
1690 const char *name, int namelen,
1691 int item_type, int errors)
1693 struct root_record *rec;
1694 struct root_backref *backref;
1696 rec = get_root_rec(root_cache, root_id);
1697 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1699 backref->errors |= errors;
1701 if (item_type != BTRFS_DIR_ITEM_KEY) {
1702 if (backref->found_dir_index || backref->found_back_ref ||
1703 backref->found_forward_ref) {
1704 if (backref->index != index)
1705 backref->errors |= REF_ERR_INDEX_UNMATCH;
1707 backref->index = index;
1711 if (item_type == BTRFS_DIR_ITEM_KEY) {
1712 if (backref->found_forward_ref)
1714 backref->found_dir_item = 1;
1715 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1716 backref->found_dir_index = 1;
1717 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1718 if (backref->found_forward_ref)
1719 backref->errors |= REF_ERR_DUP_ROOT_REF;
1720 else if (backref->found_dir_item)
1722 backref->found_forward_ref = 1;
1723 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1724 if (backref->found_back_ref)
1725 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1726 backref->found_back_ref = 1;
1731 if (backref->found_forward_ref && backref->found_dir_item)
1732 backref->reachable = 1;
1736 static int merge_root_recs(struct btrfs_root *root,
1737 struct cache_tree *src_cache,
1738 struct cache_tree *dst_cache)
1740 struct cache_extent *cache;
1741 struct ptr_node *node;
1742 struct inode_record *rec;
1743 struct inode_backref *backref;
1745 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1746 free_inode_recs_tree(src_cache);
1751 cache = search_cache_extent(src_cache, 0);
1754 node = container_of(cache, struct ptr_node, cache);
1756 remove_cache_extent(src_cache, &node->cache);
1759 if (!is_child_root(root, root->objectid, rec->ino))
1762 list_for_each_entry(backref, &rec->backrefs, list) {
1763 BUG_ON(backref->found_inode_ref);
1764 if (backref->found_dir_item)
1765 add_root_backref(dst_cache, rec->ino,
1766 root->root_key.objectid, backref->dir,
1767 backref->index, backref->name,
1768 backref->namelen, BTRFS_DIR_ITEM_KEY,
1770 if (backref->found_dir_index)
1771 add_root_backref(dst_cache, rec->ino,
1772 root->root_key.objectid, backref->dir,
1773 backref->index, backref->name,
1774 backref->namelen, BTRFS_DIR_INDEX_KEY,
1778 free_inode_rec(rec);
1783 static int check_root_refs(struct btrfs_root *root,
1784 struct cache_tree *root_cache)
1786 struct root_record *rec;
1787 struct root_record *ref_root;
1788 struct root_backref *backref;
1789 struct cache_extent *cache;
1795 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1798 /* fixme: this can not detect circular references */
1801 cache = search_cache_extent(root_cache, 0);
1805 rec = container_of(cache, struct root_record, cache);
1806 cache = next_cache_extent(cache);
1808 if (rec->found_ref == 0)
1811 list_for_each_entry(backref, &rec->backrefs, list) {
1812 if (!backref->reachable)
1815 ref_root = get_root_rec(root_cache,
1817 if (ref_root->found_ref > 0)
1820 backref->reachable = 0;
1822 if (rec->found_ref == 0)
1828 cache = search_cache_extent(root_cache, 0);
1832 rec = container_of(cache, struct root_record, cache);
1833 cache = next_cache_extent(cache);
1835 if (rec->found_ref == 0 &&
1836 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1837 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1838 ret = check_orphan_item(root->fs_info->tree_root,
1844 * If we don't have a root item then we likely just have
1845 * a dir item in a snapshot for this root but no actual
1846 * ref key or anything so it's meaningless.
1848 if (!rec->found_root_item)
1851 fprintf(stderr, "fs tree %llu not referenced\n",
1852 (unsigned long long)rec->objectid);
1856 if (rec->found_ref > 0 && !rec->found_root_item)
1858 list_for_each_entry(backref, &rec->backrefs, list) {
1859 if (!backref->found_dir_item)
1860 backref->errors |= REF_ERR_NO_DIR_ITEM;
1861 if (!backref->found_dir_index)
1862 backref->errors |= REF_ERR_NO_DIR_INDEX;
1863 if (!backref->found_back_ref)
1864 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1865 if (!backref->found_forward_ref)
1866 backref->errors |= REF_ERR_NO_ROOT_REF;
1867 if (backref->reachable && backref->errors)
1874 fprintf(stderr, "fs tree %llu refs %u %s\n",
1875 (unsigned long long)rec->objectid, rec->found_ref,
1876 rec->found_root_item ? "" : "not found");
1878 list_for_each_entry(backref, &rec->backrefs, list) {
1879 if (!backref->reachable)
1881 if (!backref->errors && rec->found_root_item)
1883 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1884 " index %llu namelen %u name %s errors %x\n",
1885 (unsigned long long)backref->ref_root,
1886 (unsigned long long)backref->dir,
1887 (unsigned long long)backref->index,
1888 backref->namelen, backref->name,
1890 print_ref_error(backref->errors);
1893 return errors > 0 ? 1 : 0;
1896 static int process_root_ref(struct extent_buffer *eb, int slot,
1897 struct btrfs_key *key,
1898 struct cache_tree *root_cache)
1904 struct btrfs_root_ref *ref;
1905 char namebuf[BTRFS_NAME_LEN];
1908 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1910 dirid = btrfs_root_ref_dirid(eb, ref);
1911 index = btrfs_root_ref_sequence(eb, ref);
1912 name_len = btrfs_root_ref_name_len(eb, ref);
1914 if (name_len <= BTRFS_NAME_LEN) {
1918 len = BTRFS_NAME_LEN;
1919 error = REF_ERR_NAME_TOO_LONG;
1921 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1923 if (key->type == BTRFS_ROOT_REF_KEY) {
1924 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1925 index, namebuf, len, key->type, error);
1927 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1928 index, namebuf, len, key->type, error);
1933 static int check_fs_root(struct btrfs_root *root,
1934 struct cache_tree *root_cache,
1935 struct walk_control *wc)
1940 struct btrfs_path path;
1941 struct shared_node root_node;
1942 struct root_record *rec;
1943 struct btrfs_root_item *root_item = &root->root_item;
1945 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1946 rec = get_root_rec(root_cache, root->root_key.objectid);
1947 if (btrfs_root_refs(root_item) > 0)
1948 rec->found_root_item = 1;
1951 btrfs_init_path(&path);
1952 memset(&root_node, 0, sizeof(root_node));
1953 cache_tree_init(&root_node.root_cache);
1954 cache_tree_init(&root_node.inode_cache);
1956 level = btrfs_header_level(root->node);
1957 memset(wc->nodes, 0, sizeof(wc->nodes));
1958 wc->nodes[level] = &root_node;
1959 wc->active_node = level;
1960 wc->root_level = level;
1962 if (btrfs_root_refs(root_item) > 0 ||
1963 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1964 path.nodes[level] = root->node;
1965 extent_buffer_get(root->node);
1966 path.slots[level] = 0;
1968 struct btrfs_key key;
1969 struct btrfs_disk_key found_key;
1971 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1972 level = root_item->drop_level;
1973 path.lowest_level = level;
1974 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1976 btrfs_node_key(path.nodes[level], &found_key,
1978 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1979 sizeof(found_key)));
1983 wret = walk_down_tree(root, &path, wc, &level);
1989 wret = walk_up_tree(root, &path, wc, &level);
1995 btrfs_release_path(&path);
1997 merge_root_recs(root, &root_node.root_cache, root_cache);
1999 if (root_node.current) {
2000 root_node.current->checked = 1;
2001 maybe_free_inode_rec(&root_node.inode_cache,
2005 ret = check_inode_recs(root, &root_node.inode_cache);
2009 static int fs_root_objectid(u64 objectid)
2011 if (objectid == BTRFS_FS_TREE_OBJECTID ||
2012 objectid == BTRFS_TREE_RELOC_OBJECTID ||
2013 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
2014 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
2015 objectid <= BTRFS_LAST_FREE_OBJECTID))
2020 static int check_fs_roots(struct btrfs_root *root,
2021 struct cache_tree *root_cache)
2023 struct btrfs_path path;
2024 struct btrfs_key key;
2025 struct walk_control wc;
2026 struct extent_buffer *leaf;
2027 struct btrfs_root *tmp_root;
2028 struct btrfs_root *tree_root = root->fs_info->tree_root;
2033 * Just in case we made any changes to the extent tree that weren't
2034 * reflected into the free space cache yet.
2037 reset_cached_block_groups(root->fs_info);
2038 memset(&wc, 0, sizeof(wc));
2039 cache_tree_init(&wc.shared);
2040 btrfs_init_path(&path);
2044 key.type = BTRFS_ROOT_ITEM_KEY;
2045 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2048 leaf = path.nodes[0];
2049 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2050 ret = btrfs_next_leaf(tree_root, &path);
2053 leaf = path.nodes[0];
2055 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2056 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2057 fs_root_objectid(key.objectid)) {
2058 key.offset = (u64)-1;
2059 tmp_root = btrfs_read_fs_root(root->fs_info, &key);
2060 if (IS_ERR(tmp_root)) {
2064 ret = check_fs_root(tmp_root, root_cache, &wc);
2067 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2068 key.type == BTRFS_ROOT_BACKREF_KEY) {
2069 process_root_ref(leaf, path.slots[0], &key,
2075 btrfs_release_path(&path);
2077 if (!cache_tree_empty(&wc.shared))
2078 fprintf(stderr, "warning line %d\n", __LINE__);
2083 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2085 struct list_head *cur = rec->backrefs.next;
2086 struct extent_backref *back;
2087 struct tree_backref *tback;
2088 struct data_backref *dback;
2092 while(cur != &rec->backrefs) {
2093 back = list_entry(cur, struct extent_backref, list);
2095 if (!back->found_extent_tree) {
2099 if (back->is_data) {
2100 dback = (struct data_backref *)back;
2101 fprintf(stderr, "Backref %llu %s %llu"
2102 " owner %llu offset %llu num_refs %lu"
2103 " not found in extent tree\n",
2104 (unsigned long long)rec->start,
2105 back->full_backref ?
2107 back->full_backref ?
2108 (unsigned long long)dback->parent:
2109 (unsigned long long)dback->root,
2110 (unsigned long long)dback->owner,
2111 (unsigned long long)dback->offset,
2112 (unsigned long)dback->num_refs);
2114 tback = (struct tree_backref *)back;
2115 fprintf(stderr, "Backref %llu parent %llu"
2116 " root %llu not found in extent tree\n",
2117 (unsigned long long)rec->start,
2118 (unsigned long long)tback->parent,
2119 (unsigned long long)tback->root);
2122 if (!back->is_data && !back->found_ref) {
2126 tback = (struct tree_backref *)back;
2127 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2128 (unsigned long long)rec->start,
2129 back->full_backref ? "parent" : "root",
2130 back->full_backref ?
2131 (unsigned long long)tback->parent :
2132 (unsigned long long)tback->root, back);
2134 if (back->is_data) {
2135 dback = (struct data_backref *)back;
2136 if (dback->found_ref != dback->num_refs) {
2140 fprintf(stderr, "Incorrect local backref count"
2141 " on %llu %s %llu owner %llu"
2142 " offset %llu found %u wanted %u back %p\n",
2143 (unsigned long long)rec->start,
2144 back->full_backref ?
2146 back->full_backref ?
2147 (unsigned long long)dback->parent:
2148 (unsigned long long)dback->root,
2149 (unsigned long long)dback->owner,
2150 (unsigned long long)dback->offset,
2151 dback->found_ref, dback->num_refs, back);
2153 if (dback->disk_bytenr != rec->start) {
2157 fprintf(stderr, "Backref disk bytenr does not"
2158 " match extent record, bytenr=%llu, "
2159 "ref bytenr=%llu\n",
2160 (unsigned long long)rec->start,
2161 (unsigned long long)dback->disk_bytenr);
2164 if (dback->bytes != rec->nr) {
2168 fprintf(stderr, "Backref bytes do not match "
2169 "extent backref, bytenr=%llu, ref "
2170 "bytes=%llu, backref bytes=%llu\n",
2171 (unsigned long long)rec->start,
2172 (unsigned long long)rec->nr,
2173 (unsigned long long)dback->bytes);
2176 if (!back->is_data) {
2179 dback = (struct data_backref *)back;
2180 found += dback->found_ref;
2183 if (found != rec->refs) {
2187 fprintf(stderr, "Incorrect global backref count "
2188 "on %llu found %llu wanted %llu\n",
2189 (unsigned long long)rec->start,
2190 (unsigned long long)found,
2191 (unsigned long long)rec->refs);
2197 static int free_all_extent_backrefs(struct extent_record *rec)
2199 struct extent_backref *back;
2200 struct list_head *cur;
2201 while (!list_empty(&rec->backrefs)) {
2202 cur = rec->backrefs.next;
2203 back = list_entry(cur, struct extent_backref, list);
2210 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2211 struct cache_tree *extent_cache)
2213 struct cache_extent *cache;
2214 struct extent_record *rec;
2217 cache = first_cache_extent(extent_cache);
2220 rec = container_of(cache, struct extent_record, cache);
2221 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2222 remove_cache_extent(extent_cache, cache);
2223 free_all_extent_backrefs(rec);
2228 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2229 struct extent_record *rec)
2231 if (rec->content_checked && rec->owner_ref_checked &&
2232 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2233 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2234 remove_cache_extent(extent_cache, &rec->cache);
2235 free_all_extent_backrefs(rec);
2236 list_del_init(&rec->list);
2242 static int check_owner_ref(struct btrfs_root *root,
2243 struct extent_record *rec,
2244 struct extent_buffer *buf)
2246 struct extent_backref *node;
2247 struct tree_backref *back;
2248 struct btrfs_root *ref_root;
2249 struct btrfs_key key;
2250 struct btrfs_path path;
2251 struct extent_buffer *parent;
2256 list_for_each_entry(node, &rec->backrefs, list) {
2259 if (!node->found_ref)
2261 if (node->full_backref)
2263 back = (struct tree_backref *)node;
2264 if (btrfs_header_owner(buf) == back->root)
2267 BUG_ON(rec->is_root);
2269 /* try to find the block by search corresponding fs tree */
2270 key.objectid = btrfs_header_owner(buf);
2271 key.type = BTRFS_ROOT_ITEM_KEY;
2272 key.offset = (u64)-1;
2274 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2275 if (IS_ERR(ref_root))
2278 level = btrfs_header_level(buf);
2280 btrfs_item_key_to_cpu(buf, &key, 0);
2282 btrfs_node_key_to_cpu(buf, &key, 0);
2284 btrfs_init_path(&path);
2285 path.lowest_level = level + 1;
2286 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2290 parent = path.nodes[level + 1];
2291 if (parent && buf->start == btrfs_node_blockptr(parent,
2292 path.slots[level + 1]))
2295 btrfs_release_path(&path);
2296 return found ? 0 : 1;
2299 static int is_extent_tree_record(struct extent_record *rec)
2301 struct list_head *cur = rec->backrefs.next;
2302 struct extent_backref *node;
2303 struct tree_backref *back;
2306 while(cur != &rec->backrefs) {
2307 node = list_entry(cur, struct extent_backref, list);
2311 back = (struct tree_backref *)node;
2312 if (node->full_backref)
2314 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2321 static int record_bad_block_io(struct btrfs_fs_info *info,
2322 struct cache_tree *extent_cache,
2325 struct extent_record *rec;
2326 struct cache_extent *cache;
2327 struct btrfs_key key;
2329 cache = lookup_cache_extent(extent_cache, start, len);
2333 rec = container_of(cache, struct extent_record, cache);
2334 if (!is_extent_tree_record(rec))
2337 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2338 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2341 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2342 struct extent_buffer *buf, int slot)
2344 if (btrfs_header_level(buf)) {
2345 struct btrfs_key_ptr ptr1, ptr2;
2347 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2348 sizeof(struct btrfs_key_ptr));
2349 read_extent_buffer(buf, &ptr2,
2350 btrfs_node_key_ptr_offset(slot + 1),
2351 sizeof(struct btrfs_key_ptr));
2352 write_extent_buffer(buf, &ptr1,
2353 btrfs_node_key_ptr_offset(slot + 1),
2354 sizeof(struct btrfs_key_ptr));
2355 write_extent_buffer(buf, &ptr2,
2356 btrfs_node_key_ptr_offset(slot),
2357 sizeof(struct btrfs_key_ptr));
2359 struct btrfs_disk_key key;
2360 btrfs_node_key(buf, &key, 0);
2361 btrfs_fixup_low_keys(root, path, &key,
2362 btrfs_header_level(buf) + 1);
2365 struct btrfs_item *item1, *item2;
2366 struct btrfs_key k1, k2;
2367 char *item1_data, *item2_data;
2368 u32 item1_offset, item2_offset, item1_size, item2_size;
2370 item1 = btrfs_item_nr(slot);
2371 item2 = btrfs_item_nr(slot + 1);
2372 btrfs_item_key_to_cpu(buf, &k1, slot);
2373 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2374 item1_offset = btrfs_item_offset(buf, item1);
2375 item2_offset = btrfs_item_offset(buf, item2);
2376 item1_size = btrfs_item_size(buf, item1);
2377 item2_size = btrfs_item_size(buf, item2);
2379 item1_data = malloc(item1_size);
2382 item2_data = malloc(item2_size);
2388 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2389 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2391 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2392 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2396 btrfs_set_item_offset(buf, item1, item2_offset);
2397 btrfs_set_item_offset(buf, item2, item1_offset);
2398 btrfs_set_item_size(buf, item1, item2_size);
2399 btrfs_set_item_size(buf, item2, item1_size);
2401 path->slots[0] = slot;
2402 btrfs_set_item_key_unsafe(root, path, &k2);
2403 path->slots[0] = slot + 1;
2404 btrfs_set_item_key_unsafe(root, path, &k1);
2410 * Attempt to fix basic block failures. Currently we only handle bad key
2411 * orders, we will cycle through the keys and swap them if necessary.
2413 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
2414 struct btrfs_root *root,
2415 struct extent_buffer *buf,
2416 struct btrfs_disk_key *parent_key,
2417 enum btrfs_tree_block_status status)
2419 struct btrfs_path *path;
2420 struct btrfs_key k1, k2;
2424 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
2427 k1.objectid = btrfs_header_owner(buf);
2428 k1.type = BTRFS_ROOT_ITEM_KEY;
2429 k1.offset = (u64)-1;
2431 root = btrfs_read_fs_root(root->fs_info, &k1);
2435 path = btrfs_alloc_path();
2439 path->lowest_level = btrfs_header_level(buf);
2440 path->skip_check_block = 1;
2441 if (btrfs_header_level(buf))
2442 btrfs_node_key_to_cpu(buf, &k1, 0);
2444 btrfs_item_key_to_cpu(buf, &k1, 0);
2446 ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
2448 btrfs_free_path(path);
2452 buf = path->nodes[0];
2453 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2454 if (btrfs_header_level(buf)) {
2455 btrfs_node_key_to_cpu(buf, &k1, i);
2456 btrfs_node_key_to_cpu(buf, &k2, i + 1);
2458 btrfs_item_key_to_cpu(buf, &k1, i);
2459 btrfs_item_key_to_cpu(buf, &k2, i + 1);
2461 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2463 ret = swap_values(root, path, buf, i);
2466 btrfs_mark_buffer_dirty(buf);
2470 btrfs_free_path(path);
2474 static int check_block(struct btrfs_trans_handle *trans,
2475 struct btrfs_root *root,
2476 struct cache_tree *extent_cache,
2477 struct extent_buffer *buf, u64 flags)
2479 struct extent_record *rec;
2480 struct cache_extent *cache;
2481 struct btrfs_key key;
2482 enum btrfs_tree_block_status status;
2486 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2489 rec = container_of(cache, struct extent_record, cache);
2490 rec->generation = btrfs_header_generation(buf);
2492 level = btrfs_header_level(buf);
2493 if (btrfs_header_nritems(buf) > 0) {
2496 btrfs_item_key_to_cpu(buf, &key, 0);
2498 btrfs_node_key_to_cpu(buf, &key, 0);
2500 rec->info_objectid = key.objectid;
2502 rec->info_level = level;
2504 if (btrfs_is_leaf(buf))
2505 status = btrfs_check_leaf(root, &rec->parent_key, buf);
2507 status = btrfs_check_node(root, &rec->parent_key, buf);
2509 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2511 status = try_to_fix_bad_block(trans, root, buf,
2514 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2516 fprintf(stderr, "bad block %llu\n",
2517 (unsigned long long)buf->start);
2520 * Signal to callers we need to start the scan over
2521 * again since we'll have cow'ed blocks.
2526 rec->content_checked = 1;
2527 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2528 rec->owner_ref_checked = 1;
2530 ret = check_owner_ref(root, rec, buf);
2532 rec->owner_ref_checked = 1;
2536 maybe_free_extent_rec(extent_cache, rec);
2540 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2541 u64 parent, u64 root)
2543 struct list_head *cur = rec->backrefs.next;
2544 struct extent_backref *node;
2545 struct tree_backref *back;
2547 while(cur != &rec->backrefs) {
2548 node = list_entry(cur, struct extent_backref, list);
2552 back = (struct tree_backref *)node;
2554 if (!node->full_backref)
2556 if (parent == back->parent)
2559 if (node->full_backref)
2561 if (back->root == root)
2568 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2569 u64 parent, u64 root)
2571 struct tree_backref *ref = malloc(sizeof(*ref));
2572 memset(&ref->node, 0, sizeof(ref->node));
2574 ref->parent = parent;
2575 ref->node.full_backref = 1;
2578 ref->node.full_backref = 0;
2580 list_add_tail(&ref->node.list, &rec->backrefs);
2585 static struct data_backref *find_data_backref(struct extent_record *rec,
2586 u64 parent, u64 root,
2587 u64 owner, u64 offset,
2589 u64 disk_bytenr, u64 bytes)
2591 struct list_head *cur = rec->backrefs.next;
2592 struct extent_backref *node;
2593 struct data_backref *back;
2595 while(cur != &rec->backrefs) {
2596 node = list_entry(cur, struct extent_backref, list);
2600 back = (struct data_backref *)node;
2602 if (!node->full_backref)
2604 if (parent == back->parent)
2607 if (node->full_backref)
2609 if (back->root == root && back->owner == owner &&
2610 back->offset == offset) {
2611 if (found_ref && node->found_ref &&
2612 (back->bytes != bytes ||
2613 back->disk_bytenr != disk_bytenr))
2622 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2623 u64 parent, u64 root,
2624 u64 owner, u64 offset,
2627 struct data_backref *ref = malloc(sizeof(*ref));
2628 memset(&ref->node, 0, sizeof(ref->node));
2629 ref->node.is_data = 1;
2632 ref->parent = parent;
2635 ref->node.full_backref = 1;
2639 ref->offset = offset;
2640 ref->node.full_backref = 0;
2642 ref->bytes = max_size;
2645 list_add_tail(&ref->node.list, &rec->backrefs);
2646 if (max_size > rec->max_size)
2647 rec->max_size = max_size;
2651 static int add_extent_rec(struct cache_tree *extent_cache,
2652 struct btrfs_key *parent_key, u64 parent_gen,
2653 u64 start, u64 nr, u64 extent_item_refs,
2654 int is_root, int inc_ref, int set_checked,
2655 int metadata, int extent_rec, u64 max_size)
2657 struct extent_record *rec;
2658 struct cache_extent *cache;
2662 cache = lookup_cache_extent(extent_cache, start, nr);
2664 rec = container_of(cache, struct extent_record, cache);
2668 rec->nr = max(nr, max_size);
2671 * We need to make sure to reset nr to whatever the extent
2672 * record says was the real size, this way we can compare it to
2676 if (start != rec->start || rec->found_rec) {
2677 struct extent_record *tmp;
2680 if (list_empty(&rec->list))
2681 list_add_tail(&rec->list,
2682 &duplicate_extents);
2685 * We have to do this song and dance in case we
2686 * find an extent record that falls inside of
2687 * our current extent record but does not have
2688 * the same objectid.
2690 tmp = malloc(sizeof(*tmp));
2694 tmp->max_size = max_size;
2697 tmp->metadata = metadata;
2698 tmp->extent_item_refs = extent_item_refs;
2699 INIT_LIST_HEAD(&tmp->list);
2700 list_add_tail(&tmp->list, &rec->dups);
2701 rec->num_duplicates++;
2708 if (extent_item_refs && !dup) {
2709 if (rec->extent_item_refs) {
2710 fprintf(stderr, "block %llu rec "
2711 "extent_item_refs %llu, passed %llu\n",
2712 (unsigned long long)start,
2713 (unsigned long long)
2714 rec->extent_item_refs,
2715 (unsigned long long)extent_item_refs);
2717 rec->extent_item_refs = extent_item_refs;
2722 rec->content_checked = 1;
2723 rec->owner_ref_checked = 1;
2727 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2729 rec->parent_generation = parent_gen;
2731 if (rec->max_size < max_size)
2732 rec->max_size = max_size;
2734 maybe_free_extent_rec(extent_cache, rec);
2737 rec = malloc(sizeof(*rec));
2739 rec->max_size = max_size;
2740 rec->nr = max(nr, max_size);
2741 rec->found_rec = !!extent_rec;
2742 rec->content_checked = 0;
2743 rec->owner_ref_checked = 0;
2744 rec->num_duplicates = 0;
2745 rec->metadata = metadata;
2746 INIT_LIST_HEAD(&rec->backrefs);
2747 INIT_LIST_HEAD(&rec->dups);
2748 INIT_LIST_HEAD(&rec->list);
2760 if (extent_item_refs)
2761 rec->extent_item_refs = extent_item_refs;
2763 rec->extent_item_refs = 0;
2766 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2768 memset(&rec->parent_key, 0, sizeof(*parent_key));
2771 rec->parent_generation = parent_gen;
2773 rec->parent_generation = 0;
2775 rec->cache.start = start;
2776 rec->cache.size = nr;
2777 ret = insert_cache_extent(extent_cache, &rec->cache);
2781 rec->content_checked = 1;
2782 rec->owner_ref_checked = 1;
2787 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2788 u64 parent, u64 root, int found_ref)
2790 struct extent_record *rec;
2791 struct tree_backref *back;
2792 struct cache_extent *cache;
2794 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2796 add_extent_rec(extent_cache, NULL, 0, bytenr,
2797 1, 0, 0, 0, 0, 1, 0, 0);
2798 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2803 rec = container_of(cache, struct extent_record, cache);
2804 if (rec->start != bytenr) {
2808 back = find_tree_backref(rec, parent, root);
2810 back = alloc_tree_backref(rec, parent, root);
2813 if (back->node.found_ref) {
2814 fprintf(stderr, "Extent back ref already exists "
2815 "for %llu parent %llu root %llu \n",
2816 (unsigned long long)bytenr,
2817 (unsigned long long)parent,
2818 (unsigned long long)root);
2820 back->node.found_ref = 1;
2822 if (back->node.found_extent_tree) {
2823 fprintf(stderr, "Extent back ref already exists "
2824 "for %llu parent %llu root %llu \n",
2825 (unsigned long long)bytenr,
2826 (unsigned long long)parent,
2827 (unsigned long long)root);
2829 back->node.found_extent_tree = 1;
2834 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2835 u64 parent, u64 root, u64 owner, u64 offset,
2836 u32 num_refs, int found_ref, u64 max_size)
2838 struct extent_record *rec;
2839 struct data_backref *back;
2840 struct cache_extent *cache;
2842 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2844 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
2846 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2851 rec = container_of(cache, struct extent_record, cache);
2852 if (rec->max_size < max_size)
2853 rec->max_size = max_size;
2856 * If found_ref is set then max_size is the real size and must match the
2857 * existing refs. So if we have already found a ref then we need to
2858 * make sure that this ref matches the existing one, otherwise we need
2859 * to add a new backref so we can notice that the backrefs don't match
2860 * and we need to figure out who is telling the truth. This is to
2861 * account for that awful fsync bug I introduced where we'd end up with
2862 * a btrfs_file_extent_item that would have its length include multiple
2863 * prealloc extents or point inside of a prealloc extent.
2865 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2868 back = alloc_data_backref(rec, parent, root, owner, offset,
2872 BUG_ON(num_refs != 1);
2873 if (back->node.found_ref)
2874 BUG_ON(back->bytes != max_size);
2875 back->node.found_ref = 1;
2876 back->found_ref += 1;
2877 back->bytes = max_size;
2878 back->disk_bytenr = bytenr;
2880 rec->content_checked = 1;
2881 rec->owner_ref_checked = 1;
2883 if (back->node.found_extent_tree) {
2884 fprintf(stderr, "Extent back ref already exists "
2885 "for %llu parent %llu root %llu "
2886 "owner %llu offset %llu num_refs %lu\n",
2887 (unsigned long long)bytenr,
2888 (unsigned long long)parent,
2889 (unsigned long long)root,
2890 (unsigned long long)owner,
2891 (unsigned long long)offset,
2892 (unsigned long)num_refs);
2894 back->num_refs = num_refs;
2895 back->node.found_extent_tree = 1;
2900 static int add_pending(struct cache_tree *pending,
2901 struct cache_tree *seen, u64 bytenr, u32 size)
2904 ret = add_cache_extent(seen, bytenr, size);
2907 add_cache_extent(pending, bytenr, size);
2911 static int pick_next_pending(struct cache_tree *pending,
2912 struct cache_tree *reada,
2913 struct cache_tree *nodes,
2914 u64 last, struct block_info *bits, int bits_nr,
2917 unsigned long node_start = last;
2918 struct cache_extent *cache;
2921 cache = search_cache_extent(reada, 0);
2923 bits[0].start = cache->start;
2924 bits[0].size = cache->size;
2929 if (node_start > 32768)
2930 node_start -= 32768;
2932 cache = search_cache_extent(nodes, node_start);
2934 cache = search_cache_extent(nodes, 0);
2937 cache = search_cache_extent(pending, 0);
2942 bits[ret].start = cache->start;
2943 bits[ret].size = cache->size;
2944 cache = next_cache_extent(cache);
2946 } while (cache && ret < bits_nr);
2952 bits[ret].start = cache->start;
2953 bits[ret].size = cache->size;
2954 cache = next_cache_extent(cache);
2956 } while (cache && ret < bits_nr);
2958 if (bits_nr - ret > 8) {
2959 u64 lookup = bits[0].start + bits[0].size;
2960 struct cache_extent *next;
2961 next = search_cache_extent(pending, lookup);
2963 if (next->start - lookup > 32768)
2965 bits[ret].start = next->start;
2966 bits[ret].size = next->size;
2967 lookup = next->start + next->size;
2971 next = next_cache_extent(next);
2979 static void free_chunk_record(struct cache_extent *cache)
2981 struct chunk_record *rec;
2983 rec = container_of(cache, struct chunk_record, cache);
2987 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
2989 cache_tree_free_extents(chunk_cache, free_chunk_record);
2992 static void free_device_record(struct rb_node *node)
2994 struct device_record *rec;
2996 rec = container_of(node, struct device_record, node);
3000 FREE_RB_BASED_TREE(device_cache, free_device_record);
3002 int insert_block_group_record(struct block_group_tree *tree,
3003 struct block_group_record *bg_rec)
3007 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3011 list_add_tail(&bg_rec->list, &tree->block_groups);
3015 static void free_block_group_record(struct cache_extent *cache)
3017 struct block_group_record *rec;
3019 rec = container_of(cache, struct block_group_record, cache);
3023 void free_block_group_tree(struct block_group_tree *tree)
3025 cache_tree_free_extents(&tree->tree, free_block_group_record);
3028 int insert_device_extent_record(struct device_extent_tree *tree,
3029 struct device_extent_record *de_rec)
3034 * Device extent is a bit different from the other extents, because
3035 * the extents which belong to the different devices may have the
3036 * same start and size, so we need use the special extent cache
3037 * search/insert functions.
3039 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3043 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3044 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3048 static void free_device_extent_record(struct cache_extent *cache)
3050 struct device_extent_record *rec;
3052 rec = container_of(cache, struct device_extent_record, cache);
3056 void free_device_extent_tree(struct device_extent_tree *tree)
3058 cache_tree_free_extents(&tree->tree, free_device_extent_record);
3061 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3062 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3063 struct extent_buffer *leaf, int slot)
3065 struct btrfs_extent_ref_v0 *ref0;
3066 struct btrfs_key key;
3068 btrfs_item_key_to_cpu(leaf, &key, slot);
3069 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3070 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3071 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3073 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3074 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3080 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3081 struct btrfs_key *key,
3084 struct btrfs_chunk *ptr;
3085 struct chunk_record *rec;
3088 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3089 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3091 rec = malloc(btrfs_chunk_record_size(num_stripes));
3093 fprintf(stderr, "memory allocation failed\n");
3097 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3099 INIT_LIST_HEAD(&rec->list);
3100 INIT_LIST_HEAD(&rec->dextents);
3103 rec->cache.start = key->offset;
3104 rec->cache.size = btrfs_chunk_length(leaf, ptr);
3106 rec->generation = btrfs_header_generation(leaf);
3108 rec->objectid = key->objectid;
3109 rec->type = key->type;
3110 rec->offset = key->offset;
3112 rec->length = rec->cache.size;
3113 rec->owner = btrfs_chunk_owner(leaf, ptr);
3114 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3115 rec->type_flags = btrfs_chunk_type(leaf, ptr);
3116 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3117 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3118 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3119 rec->num_stripes = num_stripes;
3120 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3122 for (i = 0; i < rec->num_stripes; ++i) {
3123 rec->stripes[i].devid =
3124 btrfs_stripe_devid_nr(leaf, ptr, i);
3125 rec->stripes[i].offset =
3126 btrfs_stripe_offset_nr(leaf, ptr, i);
3127 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3128 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3135 static int process_chunk_item(struct cache_tree *chunk_cache,
3136 struct btrfs_key *key, struct extent_buffer *eb,
3139 struct chunk_record *rec;
3142 rec = btrfs_new_chunk_record(eb, key, slot);
3143 ret = insert_cache_extent(chunk_cache, &rec->cache);
3145 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3146 rec->offset, rec->length);
3153 static int process_device_item(struct rb_root *dev_cache,
3154 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3156 struct btrfs_dev_item *ptr;
3157 struct device_record *rec;
3160 ptr = btrfs_item_ptr(eb,
3161 slot, struct btrfs_dev_item);
3163 rec = malloc(sizeof(*rec));
3165 fprintf(stderr, "memory allocation failed\n");
3169 rec->devid = key->offset;
3170 rec->generation = btrfs_header_generation(eb);
3172 rec->objectid = key->objectid;
3173 rec->type = key->type;
3174 rec->offset = key->offset;
3176 rec->devid = btrfs_device_id(eb, ptr);
3177 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3178 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3180 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3182 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3189 struct block_group_record *
3190 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3193 struct btrfs_block_group_item *ptr;
3194 struct block_group_record *rec;
3196 rec = malloc(sizeof(*rec));
3198 fprintf(stderr, "memory allocation failed\n");
3201 memset(rec, 0, sizeof(*rec));
3203 rec->cache.start = key->objectid;
3204 rec->cache.size = key->offset;
3206 rec->generation = btrfs_header_generation(leaf);
3208 rec->objectid = key->objectid;
3209 rec->type = key->type;
3210 rec->offset = key->offset;
3212 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3213 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3215 INIT_LIST_HEAD(&rec->list);
3220 static int process_block_group_item(struct block_group_tree *block_group_cache,
3221 struct btrfs_key *key,
3222 struct extent_buffer *eb, int slot)
3224 struct block_group_record *rec;
3227 rec = btrfs_new_block_group_record(eb, key, slot);
3228 ret = insert_block_group_record(block_group_cache, rec);
3230 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3231 rec->objectid, rec->offset);
3238 struct device_extent_record *
3239 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3240 struct btrfs_key *key, int slot)
3242 struct device_extent_record *rec;
3243 struct btrfs_dev_extent *ptr;
3245 rec = malloc(sizeof(*rec));
3247 fprintf(stderr, "memory allocation failed\n");
3250 memset(rec, 0, sizeof(*rec));
3252 rec->cache.objectid = key->objectid;
3253 rec->cache.start = key->offset;
3255 rec->generation = btrfs_header_generation(leaf);
3257 rec->objectid = key->objectid;
3258 rec->type = key->type;
3259 rec->offset = key->offset;
3261 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3262 rec->chunk_objecteid =
3263 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3265 btrfs_dev_extent_chunk_offset(leaf, ptr);
3266 rec->length = btrfs_dev_extent_length(leaf, ptr);
3267 rec->cache.size = rec->length;
3269 INIT_LIST_HEAD(&rec->chunk_list);
3270 INIT_LIST_HEAD(&rec->device_list);
3276 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3277 struct btrfs_key *key, struct extent_buffer *eb,
3280 struct device_extent_record *rec;
3283 rec = btrfs_new_device_extent_record(eb, key, slot);
3284 ret = insert_device_extent_record(dev_extent_cache, rec);
3287 "Device extent[%llu, %llu, %llu] existed.\n",
3288 rec->objectid, rec->offset, rec->length);
3295 static int process_extent_item(struct btrfs_root *root,
3296 struct cache_tree *extent_cache,
3297 struct extent_buffer *eb, int slot)
3299 struct btrfs_extent_item *ei;
3300 struct btrfs_extent_inline_ref *iref;
3301 struct btrfs_extent_data_ref *dref;
3302 struct btrfs_shared_data_ref *sref;
3303 struct btrfs_key key;
3307 u32 item_size = btrfs_item_size_nr(eb, slot);
3313 btrfs_item_key_to_cpu(eb, &key, slot);
3315 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3317 num_bytes = root->leafsize;
3319 num_bytes = key.offset;
3322 if (item_size < sizeof(*ei)) {
3323 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3324 struct btrfs_extent_item_v0 *ei0;
3325 BUG_ON(item_size != sizeof(*ei0));
3326 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3327 refs = btrfs_extent_refs_v0(eb, ei0);
3331 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3332 num_bytes, refs, 0, 0, 0, metadata, 1,
3336 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3337 refs = btrfs_extent_refs(eb, ei);
3339 add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3340 refs, 0, 0, 0, metadata, 1, num_bytes);
3342 ptr = (unsigned long)(ei + 1);
3343 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3344 key.type == BTRFS_EXTENT_ITEM_KEY)
3345 ptr += sizeof(struct btrfs_tree_block_info);
3347 end = (unsigned long)ei + item_size;
3349 iref = (struct btrfs_extent_inline_ref *)ptr;
3350 type = btrfs_extent_inline_ref_type(eb, iref);
3351 offset = btrfs_extent_inline_ref_offset(eb, iref);
3353 case BTRFS_TREE_BLOCK_REF_KEY:
3354 add_tree_backref(extent_cache, key.objectid,
3357 case BTRFS_SHARED_BLOCK_REF_KEY:
3358 add_tree_backref(extent_cache, key.objectid,
3361 case BTRFS_EXTENT_DATA_REF_KEY:
3362 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3363 add_data_backref(extent_cache, key.objectid, 0,
3364 btrfs_extent_data_ref_root(eb, dref),
3365 btrfs_extent_data_ref_objectid(eb,
3367 btrfs_extent_data_ref_offset(eb, dref),
3368 btrfs_extent_data_ref_count(eb, dref),
3371 case BTRFS_SHARED_DATA_REF_KEY:
3372 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3373 add_data_backref(extent_cache, key.objectid, offset,
3375 btrfs_shared_data_ref_count(eb, sref),
3379 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3380 key.objectid, key.type, num_bytes);
3383 ptr += btrfs_extent_inline_ref_size(type);
3390 static int check_cache_range(struct btrfs_root *root,
3391 struct btrfs_block_group_cache *cache,
3392 u64 offset, u64 bytes)
3394 struct btrfs_free_space *entry;
3400 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3401 bytenr = btrfs_sb_offset(i);
3402 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3403 cache->key.objectid, bytenr, 0,
3404 &logical, &nr, &stripe_len);
3409 if (logical[nr] + stripe_len <= offset)
3411 if (offset + bytes <= logical[nr])
3413 if (logical[nr] == offset) {
3414 if (stripe_len >= bytes) {
3418 bytes -= stripe_len;
3419 offset += stripe_len;
3420 } else if (logical[nr] < offset) {
3421 if (logical[nr] + stripe_len >=
3426 bytes = (offset + bytes) -
3427 (logical[nr] + stripe_len);
3428 offset = logical[nr] + stripe_len;
3431 * Could be tricky, the super may land in the
3432 * middle of the area we're checking. First
3433 * check the easiest case, it's at the end.
3435 if (logical[nr] + stripe_len >=
3437 bytes = logical[nr] - offset;
3441 /* Check the left side */
3442 ret = check_cache_range(root, cache,
3444 logical[nr] - offset);
3450 /* Now we continue with the right side */
3451 bytes = (offset + bytes) -
3452 (logical[nr] + stripe_len);
3453 offset = logical[nr] + stripe_len;
3460 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3462 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3463 offset, offset+bytes);
3467 if (entry->offset != offset) {
3468 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3473 if (entry->bytes != bytes) {
3474 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3475 bytes, entry->bytes, offset);
3479 unlink_free_space(cache->free_space_ctl, entry);
3484 static int verify_space_cache(struct btrfs_root *root,
3485 struct btrfs_block_group_cache *cache)
3487 struct btrfs_path *path;
3488 struct extent_buffer *leaf;
3489 struct btrfs_key key;
3493 path = btrfs_alloc_path();
3497 root = root->fs_info->extent_root;
3499 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
3501 key.objectid = last;
3503 key.type = BTRFS_EXTENT_ITEM_KEY;
3505 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3510 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3511 ret = btrfs_next_leaf(root, path);
3519 leaf = path->nodes[0];
3520 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3521 if (key.objectid >= cache->key.offset + cache->key.objectid)
3523 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3524 key.type != BTRFS_METADATA_ITEM_KEY) {
3529 if (last == key.objectid) {
3530 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3531 last = key.objectid + key.offset;
3533 last = key.objectid + root->leafsize;
3538 ret = check_cache_range(root, cache, last,
3539 key.objectid - last);
3542 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3543 last = key.objectid + key.offset;
3545 last = key.objectid + root->leafsize;
3549 if (last < cache->key.objectid + cache->key.offset)
3550 ret = check_cache_range(root, cache, last,
3551 cache->key.objectid +
3552 cache->key.offset - last);
3555 btrfs_free_path(path);
3558 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
3559 fprintf(stderr, "There are still entries left in the space "
3567 static int check_space_cache(struct btrfs_root *root)
3569 struct btrfs_block_group_cache *cache;
3570 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
3574 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
3575 btrfs_super_generation(root->fs_info->super_copy) !=
3576 btrfs_super_cache_generation(root->fs_info->super_copy)) {
3577 printf("cache and super generation don't match, space cache "
3578 "will be invalidated\n");
3583 cache = btrfs_lookup_first_block_group(root->fs_info, start);
3587 start = cache->key.objectid + cache->key.offset;
3588 if (!cache->free_space_ctl) {
3589 if (btrfs_init_free_space_ctl(cache,
3590 root->sectorsize)) {
3595 btrfs_remove_free_space_cache(cache);
3598 ret = load_free_space_cache(root->fs_info, cache);
3602 ret = verify_space_cache(root, cache);
3604 fprintf(stderr, "cache appears valid but isnt %Lu\n",
3605 cache->key.objectid);
3610 return error ? -EINVAL : 0;
3613 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
3616 struct btrfs_path *path;
3617 struct extent_buffer *leaf;
3618 struct btrfs_key key;
3621 path = btrfs_alloc_path();
3623 fprintf(stderr, "Error allocing path\n");
3627 key.objectid = bytenr;
3628 key.type = BTRFS_EXTENT_ITEM_KEY;
3633 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
3636 fprintf(stderr, "Error looking up extent record %d\n", ret);
3637 btrfs_free_path(path);
3643 btrfs_prev_leaf(root, path);
3646 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3649 * Block group items come before extent items if they have the same
3650 * bytenr, so walk back one more just in case. Dear future traveler,
3651 * first congrats on mastering time travel. Now if it's not too much
3652 * trouble could you go back to 2006 and tell Chris to make the
3653 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
3655 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3659 btrfs_prev_leaf(root, path);
3663 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3664 ret = btrfs_next_leaf(root, path);
3666 fprintf(stderr, "Error going to next leaf "
3668 btrfs_free_path(path);
3674 leaf = path->nodes[0];
3675 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3676 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3680 if (key.objectid + key.offset < bytenr) {
3684 if (key.objectid > bytenr + num_bytes)
3687 if (key.objectid == bytenr) {
3688 if (key.offset >= num_bytes) {
3692 num_bytes -= key.offset;
3693 bytenr += key.offset;
3694 } else if (key.objectid < bytenr) {
3695 if (key.objectid + key.offset >= bytenr + num_bytes) {
3699 num_bytes = (bytenr + num_bytes) -
3700 (key.objectid + key.offset);
3701 bytenr = key.objectid + key.offset;
3703 if (key.objectid + key.offset < bytenr + num_bytes) {
3704 u64 new_start = key.objectid + key.offset;
3705 u64 new_bytes = bytenr + num_bytes - new_start;
3708 * Weird case, the extent is in the middle of
3709 * our range, we'll have to search one side
3710 * and then the other. Not sure if this happens
3711 * in real life, but no harm in coding it up
3712 * anyway just in case.
3714 btrfs_release_path(path);
3715 ret = check_extent_exists(root, new_start,
3718 fprintf(stderr, "Right section didn't "
3722 num_bytes = key.objectid - bytenr;
3725 num_bytes = key.objectid - bytenr;
3732 fprintf(stderr, "There are no extents for csum range "
3733 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3737 btrfs_free_path(path);
3741 static int check_csums(struct btrfs_root *root)
3743 struct btrfs_path *path;
3744 struct extent_buffer *leaf;
3745 struct btrfs_key key;
3746 u64 offset = 0, num_bytes = 0;
3747 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3751 root = root->fs_info->csum_root;
3753 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3754 key.type = BTRFS_EXTENT_CSUM_KEY;
3757 path = btrfs_alloc_path();
3761 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3763 fprintf(stderr, "Error searching csum tree %d\n", ret);
3764 btrfs_free_path(path);
3768 if (ret > 0 && path->slots[0])
3773 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3774 ret = btrfs_next_leaf(root, path);
3776 fprintf(stderr, "Error going to next leaf "
3783 leaf = path->nodes[0];
3785 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3786 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3792 offset = key.offset;
3793 } else if (key.offset != offset + num_bytes) {
3794 ret = check_extent_exists(root, offset, num_bytes);
3796 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3797 "there is no extent record\n",
3798 offset, offset+num_bytes);
3801 offset = key.offset;
3805 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3806 csum_size) * root->sectorsize;
3810 btrfs_free_path(path);
3814 static int is_dropped_key(struct btrfs_key *key,
3815 struct btrfs_key *drop_key) {
3816 if (key->objectid < drop_key->objectid)
3818 else if (key->objectid == drop_key->objectid) {
3819 if (key->type < drop_key->type)
3821 else if (key->type == drop_key->type) {
3822 if (key->offset < drop_key->offset)
3829 static int run_next_block(struct btrfs_trans_handle *trans,
3830 struct btrfs_root *root,
3831 struct block_info *bits,
3834 struct cache_tree *pending,
3835 struct cache_tree *seen,
3836 struct cache_tree *reada,
3837 struct cache_tree *nodes,
3838 struct cache_tree *extent_cache,
3839 struct cache_tree *chunk_cache,
3840 struct rb_root *dev_cache,
3841 struct block_group_tree *block_group_cache,
3842 struct device_extent_tree *dev_extent_cache,
3843 struct btrfs_root_item *ri)
3845 struct extent_buffer *buf;
3856 struct btrfs_key key;
3857 struct cache_extent *cache;
3860 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
3861 bits_nr, &reada_bits);
3866 for(i = 0; i < nritems; i++) {
3867 ret = add_cache_extent(reada, bits[i].start,
3872 /* fixme, get the parent transid */
3873 readahead_tree_block(root, bits[i].start,
3877 *last = bits[0].start;
3878 bytenr = bits[0].start;
3879 size = bits[0].size;
3881 cache = lookup_cache_extent(pending, bytenr, size);
3883 remove_cache_extent(pending, cache);
3886 cache = lookup_cache_extent(reada, bytenr, size);
3888 remove_cache_extent(reada, cache);
3891 cache = lookup_cache_extent(nodes, bytenr, size);
3893 remove_cache_extent(nodes, cache);
3896 cache = lookup_cache_extent(extent_cache, bytenr, size);
3898 struct extent_record *rec;
3900 rec = container_of(cache, struct extent_record, cache);
3901 gen = rec->parent_generation;
3904 /* fixme, get the real parent transid */
3905 buf = read_tree_block(root, bytenr, size, gen);
3906 if (!extent_buffer_uptodate(buf)) {
3907 record_bad_block_io(root->fs_info,
3908 extent_cache, bytenr, size);
3912 nritems = btrfs_header_nritems(buf);
3915 * FIXME, this only works only if we don't have any full
3918 if (!init_extent_tree) {
3919 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3920 btrfs_header_level(buf), 1, NULL,
3928 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3933 owner = btrfs_header_owner(buf);
3936 ret = check_block(trans, root, extent_cache, buf, flags);
3940 if (btrfs_is_leaf(buf)) {
3941 btree_space_waste += btrfs_leaf_free_space(root, buf);
3942 for (i = 0; i < nritems; i++) {
3943 struct btrfs_file_extent_item *fi;
3944 btrfs_item_key_to_cpu(buf, &key, i);
3945 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3946 process_extent_item(root, extent_cache, buf,
3950 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3951 process_extent_item(root, extent_cache, buf,
3955 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3957 btrfs_item_size_nr(buf, i);
3960 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3961 process_chunk_item(chunk_cache, &key, buf, i);
3964 if (key.type == BTRFS_DEV_ITEM_KEY) {
3965 process_device_item(dev_cache, &key, buf, i);
3968 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3969 process_block_group_item(block_group_cache,
3973 if (key.type == BTRFS_DEV_EXTENT_KEY) {
3974 process_device_extent_item(dev_extent_cache,
3979 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3980 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3981 process_extent_ref_v0(extent_cache, buf, i);
3988 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3989 add_tree_backref(extent_cache, key.objectid, 0,
3993 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3994 add_tree_backref(extent_cache, key.objectid,
3998 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3999 struct btrfs_extent_data_ref *ref;
4000 ref = btrfs_item_ptr(buf, i,
4001 struct btrfs_extent_data_ref);
4002 add_data_backref(extent_cache,
4004 btrfs_extent_data_ref_root(buf, ref),
4005 btrfs_extent_data_ref_objectid(buf,
4007 btrfs_extent_data_ref_offset(buf, ref),
4008 btrfs_extent_data_ref_count(buf, ref),
4009 0, root->sectorsize);
4012 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4013 struct btrfs_shared_data_ref *ref;
4014 ref = btrfs_item_ptr(buf, i,
4015 struct btrfs_shared_data_ref);
4016 add_data_backref(extent_cache,
4017 key.objectid, key.offset, 0, 0, 0,
4018 btrfs_shared_data_ref_count(buf, ref),
4019 0, root->sectorsize);
4022 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4023 struct bad_item *bad;
4025 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4029 bad = malloc(sizeof(struct bad_item));
4032 INIT_LIST_HEAD(&bad->list);
4033 memcpy(&bad->key, &key,
4034 sizeof(struct btrfs_key));
4035 bad->root_id = owner;
4036 list_add_tail(&bad->list, &delete_items);
4039 if (key.type != BTRFS_EXTENT_DATA_KEY)
4041 fi = btrfs_item_ptr(buf, i,
4042 struct btrfs_file_extent_item);
4043 if (btrfs_file_extent_type(buf, fi) ==
4044 BTRFS_FILE_EXTENT_INLINE)
4046 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4049 data_bytes_allocated +=
4050 btrfs_file_extent_disk_num_bytes(buf, fi);
4051 if (data_bytes_allocated < root->sectorsize) {
4054 data_bytes_referenced +=
4055 btrfs_file_extent_num_bytes(buf, fi);
4056 add_data_backref(extent_cache,
4057 btrfs_file_extent_disk_bytenr(buf, fi),
4058 parent, owner, key.objectid, key.offset -
4059 btrfs_file_extent_offset(buf, fi), 1, 1,
4060 btrfs_file_extent_disk_num_bytes(buf, fi));
4064 struct btrfs_key first_key;
4066 first_key.objectid = 0;
4069 btrfs_item_key_to_cpu(buf, &first_key, 0);
4070 level = btrfs_header_level(buf);
4071 for (i = 0; i < nritems; i++) {
4072 ptr = btrfs_node_blockptr(buf, i);
4073 size = btrfs_level_size(root, level - 1);
4074 btrfs_node_key_to_cpu(buf, &key, i);
4076 struct btrfs_key drop_key;
4077 btrfs_disk_key_to_cpu(&drop_key,
4078 &ri->drop_progress);
4079 if ((level == ri->drop_level)
4080 && is_dropped_key(&key, &drop_key)) {
4084 ret = add_extent_rec(extent_cache, &key,
4085 btrfs_node_ptr_generation(buf, i),
4086 ptr, size, 0, 0, 1, 0, 1, 0,
4090 add_tree_backref(extent_cache, ptr, parent, owner, 1);
4093 add_pending(nodes, seen, ptr, size);
4095 add_pending(pending, seen, ptr, size);
4098 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4099 nritems) * sizeof(struct btrfs_key_ptr);
4101 total_btree_bytes += buf->len;
4102 if (fs_root_objectid(btrfs_header_owner(buf)))
4103 total_fs_tree_bytes += buf->len;
4104 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4105 total_extent_tree_bytes += buf->len;
4106 if (!found_old_backref &&
4107 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4108 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4109 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4110 found_old_backref = 1;
4112 free_extent_buffer(buf);
4116 static int add_root_to_pending(struct extent_buffer *buf,
4117 struct cache_tree *extent_cache,
4118 struct cache_tree *pending,
4119 struct cache_tree *seen,
4120 struct cache_tree *nodes,
4121 struct btrfs_key *root_key)
4123 if (btrfs_header_level(buf) > 0)
4124 add_pending(nodes, seen, buf->start, buf->len);
4126 add_pending(pending, seen, buf->start, buf->len);
4127 add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4128 0, 1, 1, 0, 1, 0, buf->len);
4130 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4131 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4132 add_tree_backref(extent_cache, buf->start, buf->start,
4135 add_tree_backref(extent_cache, buf->start, 0,
4136 root_key->objectid, 1);
4140 /* as we fix the tree, we might be deleting blocks that
4141 * we're tracking for repair. This hook makes sure we
4142 * remove any backrefs for blocks as we are fixing them.
4144 static int free_extent_hook(struct btrfs_trans_handle *trans,
4145 struct btrfs_root *root,
4146 u64 bytenr, u64 num_bytes, u64 parent,
4147 u64 root_objectid, u64 owner, u64 offset,
4150 struct extent_record *rec;
4151 struct cache_extent *cache;
4153 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4155 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4156 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4160 rec = container_of(cache, struct extent_record, cache);
4162 struct data_backref *back;
4163 back = find_data_backref(rec, parent, root_objectid, owner,
4164 offset, 1, bytenr, num_bytes);
4167 if (back->node.found_ref) {
4168 back->found_ref -= refs_to_drop;
4170 rec->refs -= refs_to_drop;
4172 if (back->node.found_extent_tree) {
4173 back->num_refs -= refs_to_drop;
4174 if (rec->extent_item_refs)
4175 rec->extent_item_refs -= refs_to_drop;
4177 if (back->found_ref == 0)
4178 back->node.found_ref = 0;
4179 if (back->num_refs == 0)
4180 back->node.found_extent_tree = 0;
4182 if (!back->node.found_extent_tree && back->node.found_ref) {
4183 list_del(&back->node.list);
4187 struct tree_backref *back;
4188 back = find_tree_backref(rec, parent, root_objectid);
4191 if (back->node.found_ref) {
4194 back->node.found_ref = 0;
4196 if (back->node.found_extent_tree) {
4197 if (rec->extent_item_refs)
4198 rec->extent_item_refs--;
4199 back->node.found_extent_tree = 0;
4201 if (!back->node.found_extent_tree && back->node.found_ref) {
4202 list_del(&back->node.list);
4206 maybe_free_extent_rec(extent_cache, rec);
4211 static int delete_extent_records(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
4213 struct btrfs_path *path,
4214 u64 bytenr, u64 new_len)
4216 struct btrfs_key key;
4217 struct btrfs_key found_key;
4218 struct extent_buffer *leaf;
4223 key.objectid = bytenr;
4225 key.offset = (u64)-1;
4228 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
4235 if (path->slots[0] == 0)
4241 leaf = path->nodes[0];
4242 slot = path->slots[0];
4244 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4245 if (found_key.objectid != bytenr)
4248 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
4249 found_key.type != BTRFS_METADATA_ITEM_KEY &&
4250 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
4251 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
4252 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
4253 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
4254 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
4255 btrfs_release_path(path);
4256 if (found_key.type == 0) {
4257 if (found_key.offset == 0)
4259 key.offset = found_key.offset - 1;
4260 key.type = found_key.type;
4262 key.type = found_key.type - 1;
4263 key.offset = (u64)-1;
4267 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4268 found_key.objectid, found_key.type, found_key.offset);
4270 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4273 btrfs_release_path(path);
4275 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4276 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4277 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4278 found_key.offset : root->leafsize;
4280 ret = btrfs_update_block_group(trans, root, bytenr,
4287 btrfs_release_path(path);
4292 * for a single backref, this will allocate a new extent
4293 * and add the backref to it.
4295 static int record_extent(struct btrfs_trans_handle *trans,
4296 struct btrfs_fs_info *info,
4297 struct btrfs_path *path,
4298 struct extent_record *rec,
4299 struct extent_backref *back,
4300 int allocated, u64 flags)
4303 struct btrfs_root *extent_root = info->extent_root;
4304 struct extent_buffer *leaf;
4305 struct btrfs_key ins_key;
4306 struct btrfs_extent_item *ei;
4307 struct tree_backref *tback;
4308 struct data_backref *dback;
4309 struct btrfs_tree_block_info *bi;
4312 rec->max_size = max_t(u64, rec->max_size,
4313 info->extent_root->leafsize);
4316 u32 item_size = sizeof(*ei);
4319 item_size += sizeof(*bi);
4321 ins_key.objectid = rec->start;
4322 ins_key.offset = rec->max_size;
4323 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4325 ret = btrfs_insert_empty_item(trans, extent_root, path,
4326 &ins_key, item_size);
4330 leaf = path->nodes[0];
4331 ei = btrfs_item_ptr(leaf, path->slots[0],
4332 struct btrfs_extent_item);
4334 btrfs_set_extent_refs(leaf, ei, 0);
4335 btrfs_set_extent_generation(leaf, ei, rec->generation);
4337 if (back->is_data) {
4338 btrfs_set_extent_flags(leaf, ei,
4339 BTRFS_EXTENT_FLAG_DATA);
4341 struct btrfs_disk_key copy_key;;
4343 tback = (struct tree_backref *)back;
4344 bi = (struct btrfs_tree_block_info *)(ei + 1);
4345 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4348 btrfs_set_disk_key_objectid(©_key,
4349 rec->info_objectid);
4350 btrfs_set_disk_key_type(©_key, 0);
4351 btrfs_set_disk_key_offset(©_key, 0);
4353 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
4354 btrfs_set_tree_block_key(leaf, bi, ©_key);
4356 btrfs_set_extent_flags(leaf, ei,
4357 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
4360 btrfs_mark_buffer_dirty(leaf);
4361 ret = btrfs_update_block_group(trans, extent_root, rec->start,
4362 rec->max_size, 1, 0);
4365 btrfs_release_path(path);
4368 if (back->is_data) {
4372 dback = (struct data_backref *)back;
4373 if (back->full_backref)
4374 parent = dback->parent;
4378 for (i = 0; i < dback->found_ref; i++) {
4379 /* if parent != 0, we're doing a full backref
4380 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
4381 * just makes the backref allocator create a data
4384 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4385 rec->start, rec->max_size,
4389 BTRFS_FIRST_FREE_OBJECTID :
4395 fprintf(stderr, "adding new data backref"
4396 " on %llu %s %llu owner %llu"
4397 " offset %llu found %d\n",
4398 (unsigned long long)rec->start,
4399 back->full_backref ?
4401 back->full_backref ?
4402 (unsigned long long)parent :
4403 (unsigned long long)dback->root,
4404 (unsigned long long)dback->owner,
4405 (unsigned long long)dback->offset,
4410 tback = (struct tree_backref *)back;
4411 if (back->full_backref)
4412 parent = tback->parent;
4416 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4417 rec->start, rec->max_size,
4418 parent, tback->root, 0, 0);
4419 fprintf(stderr, "adding new tree backref on "
4420 "start %llu len %llu parent %llu root %llu\n",
4421 rec->start, rec->max_size, tback->parent, tback->root);
4426 btrfs_release_path(path);
4430 struct extent_entry {
4435 struct list_head list;
4438 static struct extent_entry *find_entry(struct list_head *entries,
4439 u64 bytenr, u64 bytes)
4441 struct extent_entry *entry = NULL;
4443 list_for_each_entry(entry, entries, list) {
4444 if (entry->bytenr == bytenr && entry->bytes == bytes)
4451 static struct extent_entry *find_most_right_entry(struct list_head *entries)
4453 struct extent_entry *entry, *best = NULL, *prev = NULL;
4455 list_for_each_entry(entry, entries, list) {
4462 * If there are as many broken entries as entries then we know
4463 * not to trust this particular entry.
4465 if (entry->broken == entry->count)
4469 * If our current entry == best then we can't be sure our best
4470 * is really the best, so we need to keep searching.
4472 if (best && best->count == entry->count) {
4478 /* Prev == entry, not good enough, have to keep searching */
4479 if (!prev->broken && prev->count == entry->count)
4483 best = (prev->count > entry->count) ? prev : entry;
4484 else if (best->count < entry->count)
4492 static int repair_ref(struct btrfs_trans_handle *trans,
4493 struct btrfs_fs_info *info, struct btrfs_path *path,
4494 struct data_backref *dback, struct extent_entry *entry)
4496 struct btrfs_root *root;
4497 struct btrfs_file_extent_item *fi;
4498 struct extent_buffer *leaf;
4499 struct btrfs_key key;
4503 key.objectid = dback->root;
4504 key.type = BTRFS_ROOT_ITEM_KEY;
4505 key.offset = (u64)-1;
4506 root = btrfs_read_fs_root(info, &key);
4508 fprintf(stderr, "Couldn't find root for our ref\n");
4513 * The backref points to the original offset of the extent if it was
4514 * split, so we need to search down to the offset we have and then walk
4515 * forward until we find the backref we're looking for.
4517 key.objectid = dback->owner;
4518 key.type = BTRFS_EXTENT_DATA_KEY;
4519 key.offset = dback->offset;
4520 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4522 fprintf(stderr, "Error looking up ref %d\n", ret);
4527 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4528 ret = btrfs_next_leaf(root, path);
4530 fprintf(stderr, "Couldn't find our ref, next\n");
4534 leaf = path->nodes[0];
4535 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4536 if (key.objectid != dback->owner ||
4537 key.type != BTRFS_EXTENT_DATA_KEY) {
4538 fprintf(stderr, "Couldn't find our ref, search\n");
4541 fi = btrfs_item_ptr(leaf, path->slots[0],
4542 struct btrfs_file_extent_item);
4543 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4544 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4546 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
4551 btrfs_release_path(path);
4554 * Have to make sure that this root gets updated when we commit the
4557 root->track_dirty = 1;
4558 if (root->last_trans != trans->transid) {
4559 root->last_trans = trans->transid;
4560 root->commit_root = root->node;
4561 extent_buffer_get(root->node);
4565 * Ok we have the key of the file extent we want to fix, now we can cow
4566 * down to the thing and fix it.
4568 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4570 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
4571 key.objectid, key.type, key.offset, ret);
4575 fprintf(stderr, "Well that's odd, we just found this key "
4576 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
4580 leaf = path->nodes[0];
4581 fi = btrfs_item_ptr(leaf, path->slots[0],
4582 struct btrfs_file_extent_item);
4584 if (btrfs_file_extent_compression(leaf, fi) &&
4585 dback->disk_bytenr != entry->bytenr) {
4586 fprintf(stderr, "Ref doesn't match the record start and is "
4587 "compressed, please take a btrfs-image of this file "
4588 "system and send it to a btrfs developer so they can "
4589 "complete this functionality for bytenr %Lu\n",
4590 dback->disk_bytenr);
4594 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
4595 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4596 } else if (dback->disk_bytenr > entry->bytenr) {
4597 u64 off_diff, offset;
4599 off_diff = dback->disk_bytenr - entry->bytenr;
4600 offset = btrfs_file_extent_offset(leaf, fi);
4601 if (dback->disk_bytenr + offset +
4602 btrfs_file_extent_num_bytes(leaf, fi) >
4603 entry->bytenr + entry->bytes) {
4604 fprintf(stderr, "Ref is past the entry end, please "
4605 "take a btrfs-image of this file system and "
4606 "send it to a btrfs developer, ref %Lu\n",
4607 dback->disk_bytenr);
4611 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4612 btrfs_set_file_extent_offset(leaf, fi, offset);
4613 } else if (dback->disk_bytenr < entry->bytenr) {
4616 offset = btrfs_file_extent_offset(leaf, fi);
4617 if (dback->disk_bytenr + offset < entry->bytenr) {
4618 fprintf(stderr, "Ref is before the entry start, please"
4619 " take a btrfs-image of this file system and "
4620 "send it to a btrfs developer, ref %Lu\n",
4621 dback->disk_bytenr);
4625 offset += dback->disk_bytenr;
4626 offset -= entry->bytenr;
4627 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4628 btrfs_set_file_extent_offset(leaf, fi, offset);
4631 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
4634 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
4635 * only do this if we aren't using compression, otherwise it's a
4638 if (!btrfs_file_extent_compression(leaf, fi))
4639 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
4641 printf("ram bytes may be wrong?\n");
4642 btrfs_mark_buffer_dirty(leaf);
4643 btrfs_release_path(path);
4647 static int verify_backrefs(struct btrfs_trans_handle *trans,
4648 struct btrfs_fs_info *info, struct btrfs_path *path,
4649 struct extent_record *rec)
4651 struct extent_backref *back;
4652 struct data_backref *dback;
4653 struct extent_entry *entry, *best = NULL;
4656 int broken_entries = 0;
4661 * Metadata is easy and the backrefs should always agree on bytenr and
4662 * size, if not we've got bigger issues.
4667 list_for_each_entry(back, &rec->backrefs, list) {
4668 dback = (struct data_backref *)back;
4670 * We only pay attention to backrefs that we found a real
4673 if (dback->found_ref == 0)
4675 if (back->full_backref)
4679 * For now we only catch when the bytes don't match, not the
4680 * bytenr. We can easily do this at the same time, but I want
4681 * to have a fs image to test on before we just add repair
4682 * functionality willy-nilly so we know we won't screw up the
4686 entry = find_entry(&entries, dback->disk_bytenr,
4689 entry = malloc(sizeof(struct extent_entry));
4694 memset(entry, 0, sizeof(*entry));
4695 entry->bytenr = dback->disk_bytenr;
4696 entry->bytes = dback->bytes;
4697 list_add_tail(&entry->list, &entries);
4702 * If we only have on entry we may think the entries agree when
4703 * in reality they don't so we have to do some extra checking.
4705 if (dback->disk_bytenr != rec->start ||
4706 dback->bytes != rec->nr || back->broken)
4717 /* Yay all the backrefs agree, carry on good sir */
4718 if (nr_entries <= 1 && !mismatch)
4721 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
4722 "%Lu\n", rec->start);
4725 * First we want to see if the backrefs can agree amongst themselves who
4726 * is right, so figure out which one of the entries has the highest
4729 best = find_most_right_entry(&entries);
4732 * Ok so we may have an even split between what the backrefs think, so
4733 * this is where we use the extent ref to see what it thinks.
4736 entry = find_entry(&entries, rec->start, rec->nr);
4737 if (!entry && (!broken_entries || !rec->found_rec)) {
4738 fprintf(stderr, "Backrefs don't agree with each other "
4739 "and extent record doesn't agree with anybody,"
4740 " so we can't fix bytenr %Lu bytes %Lu\n",
4741 rec->start, rec->nr);
4744 } else if (!entry) {
4746 * Ok our backrefs were broken, we'll assume this is the
4747 * correct value and add an entry for this range.
4749 entry = malloc(sizeof(struct extent_entry));
4754 memset(entry, 0, sizeof(*entry));
4755 entry->bytenr = rec->start;
4756 entry->bytes = rec->nr;
4757 list_add_tail(&entry->list, &entries);
4761 best = find_most_right_entry(&entries);
4763 fprintf(stderr, "Backrefs and extent record evenly "
4764 "split on who is right, this is going to "
4765 "require user input to fix bytenr %Lu bytes "
4766 "%Lu\n", rec->start, rec->nr);
4773 * I don't think this can happen currently as we'll abort() if we catch
4774 * this case higher up, but in case somebody removes that we still can't
4775 * deal with it properly here yet, so just bail out of that's the case.
4777 if (best->bytenr != rec->start) {
4778 fprintf(stderr, "Extent start and backref starts don't match, "
4779 "please use btrfs-image on this file system and send "
4780 "it to a btrfs developer so they can make fsck fix "
4781 "this particular case. bytenr is %Lu, bytes is %Lu\n",
4782 rec->start, rec->nr);
4788 * Ok great we all agreed on an extent record, let's go find the real
4789 * references and fix up the ones that don't match.
4791 list_for_each_entry(back, &rec->backrefs, list) {
4792 dback = (struct data_backref *)back;
4795 * Still ignoring backrefs that don't have a real ref attached
4798 if (dback->found_ref == 0)
4800 if (back->full_backref)
4803 if (dback->bytes == best->bytes &&
4804 dback->disk_bytenr == best->bytenr)
4807 ret = repair_ref(trans, info, path, dback, best);
4813 * Ok we messed with the actual refs, which means we need to drop our
4814 * entire cache and go back and rescan. I know this is a huge pain and
4815 * adds a lot of extra work, but it's the only way to be safe. Once all
4816 * the backrefs agree we may not need to do anything to the extent
4821 while (!list_empty(&entries)) {
4822 entry = list_entry(entries.next, struct extent_entry, list);
4823 list_del_init(&entry->list);
4829 static int process_duplicates(struct btrfs_root *root,
4830 struct cache_tree *extent_cache,
4831 struct extent_record *rec)
4833 struct extent_record *good, *tmp;
4834 struct cache_extent *cache;
4838 * If we found a extent record for this extent then return, or if we
4839 * have more than one duplicate we are likely going to need to delete
4842 if (rec->found_rec || rec->num_duplicates > 1)
4845 /* Shouldn't happen but just in case */
4846 BUG_ON(!rec->num_duplicates);
4849 * So this happens if we end up with a backref that doesn't match the
4850 * actual extent entry. So either the backref is bad or the extent
4851 * entry is bad. Either way we want to have the extent_record actually
4852 * reflect what we found in the extent_tree, so we need to take the
4853 * duplicate out and use that as the extent_record since the only way we
4854 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
4856 remove_cache_extent(extent_cache, &rec->cache);
4858 good = list_entry(rec->dups.next, struct extent_record, list);
4859 list_del_init(&good->list);
4860 INIT_LIST_HEAD(&good->backrefs);
4861 INIT_LIST_HEAD(&good->dups);
4862 good->cache.start = good->start;
4863 good->cache.size = good->nr;
4864 good->content_checked = 0;
4865 good->owner_ref_checked = 0;
4866 good->num_duplicates = 0;
4867 good->refs = rec->refs;
4868 list_splice_init(&rec->backrefs, &good->backrefs);
4870 cache = lookup_cache_extent(extent_cache, good->start,
4874 tmp = container_of(cache, struct extent_record, cache);
4877 * If we find another overlapping extent and it's found_rec is
4878 * set then it's a duplicate and we need to try and delete
4881 if (tmp->found_rec || tmp->num_duplicates > 0) {
4882 if (list_empty(&good->list))
4883 list_add_tail(&good->list,
4884 &duplicate_extents);
4885 good->num_duplicates += tmp->num_duplicates + 1;
4886 list_splice_init(&tmp->dups, &good->dups);
4887 list_del_init(&tmp->list);
4888 list_add_tail(&tmp->list, &good->dups);
4889 remove_cache_extent(extent_cache, &tmp->cache);
4894 * Ok we have another non extent item backed extent rec, so lets
4895 * just add it to this extent and carry on like we did above.
4897 good->refs += tmp->refs;
4898 list_splice_init(&tmp->backrefs, &good->backrefs);
4899 remove_cache_extent(extent_cache, &tmp->cache);
4902 ret = insert_cache_extent(extent_cache, &good->cache);
4905 return good->num_duplicates ? 0 : 1;
4908 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
4909 struct btrfs_root *root,
4910 struct extent_record *rec)
4912 LIST_HEAD(delete_list);
4913 struct btrfs_path *path;
4914 struct extent_record *tmp, *good, *n;
4917 struct btrfs_key key;
4919 path = btrfs_alloc_path();
4926 /* Find the record that covers all of the duplicates. */
4927 list_for_each_entry(tmp, &rec->dups, list) {
4928 if (good->start < tmp->start)
4930 if (good->nr > tmp->nr)
4933 if (tmp->start + tmp->nr < good->start + good->nr) {
4934 fprintf(stderr, "Ok we have overlapping extents that "
4935 "aren't completely covered by eachother, this "
4936 "is going to require more careful thought. "
4937 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
4938 tmp->start, tmp->nr, good->start, good->nr);
4945 list_add_tail(&rec->list, &delete_list);
4947 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
4950 list_move_tail(&tmp->list, &delete_list);
4953 root = root->fs_info->extent_root;
4954 list_for_each_entry(tmp, &delete_list, list) {
4955 if (tmp->found_rec == 0)
4957 key.objectid = tmp->start;
4958 key.type = BTRFS_EXTENT_ITEM_KEY;
4959 key.offset = tmp->nr;
4961 /* Shouldn't happen but just in case */
4962 if (tmp->metadata) {
4963 fprintf(stderr, "Well this shouldn't happen, extent "
4964 "record overlaps but is metadata? "
4965 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
4969 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4975 ret = btrfs_del_item(trans, root, path);
4978 btrfs_release_path(path);
4983 while (!list_empty(&delete_list)) {
4984 tmp = list_entry(delete_list.next, struct extent_record, list);
4985 list_del_init(&tmp->list);
4991 while (!list_empty(&rec->dups)) {
4992 tmp = list_entry(rec->dups.next, struct extent_record, list);
4993 list_del_init(&tmp->list);
4997 btrfs_free_path(path);
4999 if (!ret && !nr_del)
5000 rec->num_duplicates = 0;
5002 return ret ? ret : nr_del;
5005 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5006 struct btrfs_fs_info *info,
5007 struct btrfs_path *path,
5008 struct cache_tree *extent_cache,
5009 struct extent_record *rec)
5011 struct btrfs_root *root;
5012 struct extent_backref *back;
5013 struct data_backref *dback;
5014 struct cache_extent *cache;
5015 struct btrfs_file_extent_item *fi;
5016 struct btrfs_key key;
5020 list_for_each_entry(back, &rec->backrefs, list) {
5021 dback = (struct data_backref *)back;
5023 /* We found this one, we don't need to do a lookup */
5024 if (dback->found_ref)
5026 /* Don't care about full backrefs (poor unloved backrefs) */
5027 if (back->full_backref)
5029 key.objectid = dback->root;
5030 key.type = BTRFS_ROOT_ITEM_KEY;
5031 key.offset = (u64)-1;
5033 root = btrfs_read_fs_root(info, &key);
5035 /* No root, definitely a bad ref, skip */
5036 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5038 /* Other err, exit */
5040 return PTR_ERR(root);
5042 key.objectid = dback->owner;
5043 key.type = BTRFS_EXTENT_DATA_KEY;
5044 key.offset = dback->offset;
5045 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5047 btrfs_release_path(path);
5050 /* Didn't find it, we can carry on */
5055 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5056 struct btrfs_file_extent_item);
5057 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5058 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5059 btrfs_release_path(path);
5060 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5062 struct extent_record *tmp;
5063 tmp = container_of(cache, struct extent_record, cache);
5066 * If we found an extent record for the bytenr for this
5067 * particular backref then we can't add it to our
5068 * current extent record. We only want to add backrefs
5069 * that don't have a corresponding extent item in the
5070 * extent tree since they likely belong to this record
5071 * and we need to fix it if it doesn't match bytenrs.
5077 dback->found_ref += 1;
5078 dback->disk_bytenr = bytenr;
5079 dback->bytes = bytes;
5082 * Set this so the verify backref code knows not to trust the
5083 * values in this backref.
5092 * when an incorrect extent item is found, this will delete
5093 * all of the existing entries for it and recreate them
5094 * based on what the tree scan found.
5096 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5097 struct btrfs_fs_info *info,
5098 struct cache_tree *extent_cache,
5099 struct extent_record *rec)
5102 struct btrfs_path *path;
5103 struct list_head *cur = rec->backrefs.next;
5104 struct cache_extent *cache;
5105 struct extent_backref *back;
5110 * remember our flags for recreating the extent.
5111 * FIXME, if we have cleared extent tree, we can not
5112 * lookup extent info in extent tree.
5114 if (!init_extent_tree) {
5115 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5116 rec->start, rec->max_size,
5117 rec->metadata, NULL, &flags);
5124 path = btrfs_alloc_path();
5128 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5130 * Sometimes the backrefs themselves are so broken they don't
5131 * get attached to any meaningful rec, so first go back and
5132 * check any of our backrefs that we couldn't find and throw
5133 * them into the list if we find the backref so that
5134 * verify_backrefs can figure out what to do.
5136 ret = find_possible_backrefs(trans, info, path, extent_cache,
5142 /* step one, make sure all of the backrefs agree */
5143 ret = verify_backrefs(trans, info, path, rec);
5147 /* step two, delete all the existing records */
5148 ret = delete_extent_records(trans, info->extent_root, path,
5149 rec->start, rec->max_size);
5154 /* was this block corrupt? If so, don't add references to it */
5155 cache = lookup_cache_extent(info->corrupt_blocks,
5156 rec->start, rec->max_size);
5162 /* step three, recreate all the refs we did find */
5163 while(cur != &rec->backrefs) {
5164 back = list_entry(cur, struct extent_backref, list);
5168 * if we didn't find any references, don't create a
5171 if (!back->found_ref)
5174 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5181 btrfs_free_path(path);
5185 /* right now we only prune from the extent allocation tree */
5186 static int prune_one_block(struct btrfs_trans_handle *trans,
5187 struct btrfs_fs_info *info,
5188 struct btrfs_corrupt_block *corrupt)
5191 struct btrfs_path path;
5192 struct extent_buffer *eb;
5196 int level = corrupt->level + 1;
5198 btrfs_init_path(&path);
5200 /* we want to stop at the parent to our busted block */
5201 path.lowest_level = level;
5203 ret = btrfs_search_slot(trans, info->extent_root,
5204 &corrupt->key, &path, -1, 1);
5209 eb = path.nodes[level];
5216 * hopefully the search gave us the block we want to prune,
5217 * lets try that first
5219 slot = path.slots[level];
5220 found = btrfs_node_blockptr(eb, slot);
5221 if (found == corrupt->cache.start)
5224 nritems = btrfs_header_nritems(eb);
5226 /* the search failed, lets scan this node and hope we find it */
5227 for (slot = 0; slot < nritems; slot++) {
5228 found = btrfs_node_blockptr(eb, slot);
5229 if (found == corrupt->cache.start)
5233 * we couldn't find the bad block. TODO, search all the nodes for pointers
5236 if (eb == info->extent_root->node) {
5241 btrfs_release_path(&path);
5246 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
5247 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
5250 btrfs_release_path(&path);
5254 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
5255 struct btrfs_fs_info *info)
5257 struct cache_extent *cache;
5258 struct btrfs_corrupt_block *corrupt;
5260 cache = search_cache_extent(info->corrupt_blocks, 0);
5264 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5265 prune_one_block(trans, info, corrupt);
5266 cache = next_cache_extent(cache);
5271 static void free_corrupt_block(struct cache_extent *cache)
5273 struct btrfs_corrupt_block *corrupt;
5275 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5279 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5281 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5283 struct btrfs_block_group_cache *cache;
5288 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5289 &start, &end, EXTENT_DIRTY);
5292 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5298 cache = btrfs_lookup_first_block_group(fs_info, start);
5303 start = cache->key.objectid + cache->key.offset;
5307 static int check_extent_refs(struct btrfs_trans_handle *trans,
5308 struct btrfs_root *root,
5309 struct cache_tree *extent_cache)
5311 struct extent_record *rec;
5312 struct cache_extent *cache;
5320 * if we're doing a repair, we have to make sure
5321 * we don't allocate from the problem extents.
5322 * In the worst case, this will be all the
5325 cache = search_cache_extent(extent_cache, 0);
5327 rec = container_of(cache, struct extent_record, cache);
5328 btrfs_pin_extent(root->fs_info,
5329 rec->start, rec->max_size);
5330 cache = next_cache_extent(cache);
5333 /* pin down all the corrupted blocks too */
5334 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5336 btrfs_pin_extent(root->fs_info,
5337 cache->start, cache->size);
5338 cache = next_cache_extent(cache);
5340 prune_corrupt_blocks(trans, root->fs_info);
5341 reset_cached_block_groups(root->fs_info);
5345 * We need to delete any duplicate entries we find first otherwise we
5346 * could mess up the extent tree when we have backrefs that actually
5347 * belong to a different extent item and not the weird duplicate one.
5349 while (repair && !list_empty(&duplicate_extents)) {
5350 rec = list_entry(duplicate_extents.next, struct extent_record,
5352 list_del_init(&rec->list);
5354 /* Sometimes we can find a backref before we find an actual
5355 * extent, so we need to process it a little bit to see if there
5356 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
5357 * if this is a backref screwup. If we need to delete stuff
5358 * process_duplicates() will return 0, otherwise it will return
5361 if (process_duplicates(root, extent_cache, rec))
5363 ret = delete_duplicate_records(trans, root, rec);
5367 * delete_duplicate_records will return the number of entries
5368 * deleted, so if it's greater than 0 then we know we actually
5369 * did something and we need to remove.
5380 cache = search_cache_extent(extent_cache, 0);
5383 rec = container_of(cache, struct extent_record, cache);
5384 if (rec->num_duplicates) {
5385 fprintf(stderr, "extent item %llu has multiple extent "
5386 "items\n", (unsigned long long)rec->start);
5390 if (rec->refs != rec->extent_item_refs) {
5391 fprintf(stderr, "ref mismatch on [%llu %llu] ",
5392 (unsigned long long)rec->start,
5393 (unsigned long long)rec->nr);
5394 fprintf(stderr, "extent item %llu, found %llu\n",
5395 (unsigned long long)rec->extent_item_refs,
5396 (unsigned long long)rec->refs);
5397 if (!fixed && repair) {
5398 ret = fixup_extent_refs(trans, root->fs_info,
5407 if (all_backpointers_checked(rec, 1)) {
5408 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
5409 (unsigned long long)rec->start,
5410 (unsigned long long)rec->nr);
5412 if (!fixed && repair) {
5413 ret = fixup_extent_refs(trans, root->fs_info,
5422 if (!rec->owner_ref_checked) {
5423 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
5424 (unsigned long long)rec->start,
5425 (unsigned long long)rec->nr);
5426 if (!fixed && repair) {
5427 ret = fixup_extent_refs(trans, root->fs_info,
5436 remove_cache_extent(extent_cache, cache);
5437 free_all_extent_backrefs(rec);
5442 if (ret && ret != -EAGAIN) {
5443 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
5446 btrfs_fix_block_accounting(trans, root);
5449 fprintf(stderr, "repaired damaged extent references\n");
5455 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
5459 if (type & BTRFS_BLOCK_GROUP_RAID0) {
5460 stripe_size = length;
5461 stripe_size /= num_stripes;
5462 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
5463 stripe_size = length * 2;
5464 stripe_size /= num_stripes;
5465 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
5466 stripe_size = length;
5467 stripe_size /= (num_stripes - 1);
5468 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
5469 stripe_size = length;
5470 stripe_size /= (num_stripes - 2);
5472 stripe_size = length;
5477 static int check_chunk_refs(struct chunk_record *chunk_rec,
5478 struct block_group_tree *block_group_cache,
5479 struct device_extent_tree *dev_extent_cache,
5482 struct cache_extent *block_group_item;
5483 struct block_group_record *block_group_rec;
5484 struct cache_extent *dev_extent_item;
5485 struct device_extent_record *dev_extent_rec;
5492 block_group_item = lookup_cache_extent(&block_group_cache->tree,
5495 if (block_group_item) {
5496 block_group_rec = container_of(block_group_item,
5497 struct block_group_record,
5499 if (chunk_rec->length != block_group_rec->offset ||
5500 chunk_rec->offset != block_group_rec->objectid ||
5501 chunk_rec->type_flags != block_group_rec->flags) {
5504 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
5505 chunk_rec->objectid,
5510 chunk_rec->type_flags,
5511 block_group_rec->objectid,
5512 block_group_rec->type,
5513 block_group_rec->offset,
5514 block_group_rec->offset,
5515 block_group_rec->objectid,
5516 block_group_rec->flags);
5519 list_del_init(&block_group_rec->list);
5520 chunk_rec->bg_rec = block_group_rec;
5525 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
5526 chunk_rec->objectid,
5531 chunk_rec->type_flags);
5535 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
5536 chunk_rec->num_stripes);
5537 for (i = 0; i < chunk_rec->num_stripes; ++i) {
5538 devid = chunk_rec->stripes[i].devid;
5539 offset = chunk_rec->stripes[i].offset;
5540 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
5541 devid, offset, length);
5542 if (dev_extent_item) {
5543 dev_extent_rec = container_of(dev_extent_item,
5544 struct device_extent_record,
5546 if (dev_extent_rec->objectid != devid ||
5547 dev_extent_rec->offset != offset ||
5548 dev_extent_rec->chunk_offset != chunk_rec->offset ||
5549 dev_extent_rec->length != length) {
5552 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
5553 chunk_rec->objectid,
5556 chunk_rec->stripes[i].devid,
5557 chunk_rec->stripes[i].offset,
5558 dev_extent_rec->objectid,
5559 dev_extent_rec->offset,
5560 dev_extent_rec->length);
5563 list_move(&dev_extent_rec->chunk_list,
5564 &chunk_rec->dextents);
5569 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
5570 chunk_rec->objectid,
5573 chunk_rec->stripes[i].devid,
5574 chunk_rec->stripes[i].offset);
5581 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
5582 int check_chunks(struct cache_tree *chunk_cache,
5583 struct block_group_tree *block_group_cache,
5584 struct device_extent_tree *dev_extent_cache,
5585 struct list_head *good, struct list_head *bad, int silent)
5587 struct cache_extent *chunk_item;
5588 struct chunk_record *chunk_rec;
5589 struct block_group_record *bg_rec;
5590 struct device_extent_record *dext_rec;
5594 chunk_item = first_cache_extent(chunk_cache);
5595 while (chunk_item) {
5596 chunk_rec = container_of(chunk_item, struct chunk_record,
5598 err = check_chunk_refs(chunk_rec, block_group_cache,
5599 dev_extent_cache, silent);
5603 list_add_tail(&chunk_rec->list, bad);
5606 list_add_tail(&chunk_rec->list, good);
5609 chunk_item = next_cache_extent(chunk_item);
5612 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
5615 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
5623 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
5627 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
5638 static int check_device_used(struct device_record *dev_rec,
5639 struct device_extent_tree *dext_cache)
5641 struct cache_extent *cache;
5642 struct device_extent_record *dev_extent_rec;
5645 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
5647 dev_extent_rec = container_of(cache,
5648 struct device_extent_record,
5650 if (dev_extent_rec->objectid != dev_rec->devid)
5653 list_del(&dev_extent_rec->device_list);
5654 total_byte += dev_extent_rec->length;
5655 cache = next_cache_extent(cache);
5658 if (total_byte != dev_rec->byte_used) {
5660 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
5661 total_byte, dev_rec->byte_used, dev_rec->objectid,
5662 dev_rec->type, dev_rec->offset);
5669 /* check btrfs_dev_item -> btrfs_dev_extent */
5670 static int check_devices(struct rb_root *dev_cache,
5671 struct device_extent_tree *dev_extent_cache)
5673 struct rb_node *dev_node;
5674 struct device_record *dev_rec;
5675 struct device_extent_record *dext_rec;
5679 dev_node = rb_first(dev_cache);
5681 dev_rec = container_of(dev_node, struct device_record, node);
5682 err = check_device_used(dev_rec, dev_extent_cache);
5686 dev_node = rb_next(dev_node);
5688 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
5691 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
5692 dext_rec->objectid, dext_rec->offset, dext_rec->length);
5699 static int check_chunks_and_extents(struct btrfs_root *root)
5701 struct rb_root dev_cache;
5702 struct cache_tree chunk_cache;
5703 struct block_group_tree block_group_cache;
5704 struct device_extent_tree dev_extent_cache;
5705 struct cache_tree extent_cache;
5706 struct cache_tree seen;
5707 struct cache_tree pending;
5708 struct cache_tree reada;
5709 struct cache_tree nodes;
5710 struct cache_tree corrupt_blocks;
5711 struct btrfs_path path;
5712 struct btrfs_key key;
5713 struct btrfs_key found_key;
5716 struct block_info *bits;
5718 struct extent_buffer *leaf;
5719 struct btrfs_trans_handle *trans = NULL;
5721 struct btrfs_root_item ri;
5722 struct list_head dropping_trees;
5724 dev_cache = RB_ROOT;
5725 cache_tree_init(&chunk_cache);
5726 block_group_tree_init(&block_group_cache);
5727 device_extent_tree_init(&dev_extent_cache);
5729 cache_tree_init(&extent_cache);
5730 cache_tree_init(&seen);
5731 cache_tree_init(&pending);
5732 cache_tree_init(&nodes);
5733 cache_tree_init(&reada);
5734 cache_tree_init(&corrupt_blocks);
5735 INIT_LIST_HEAD(&dropping_trees);
5738 trans = btrfs_start_transaction(root, 1);
5739 if (IS_ERR(trans)) {
5740 fprintf(stderr, "Error starting transaction\n");
5741 return PTR_ERR(trans);
5743 root->fs_info->fsck_extent_cache = &extent_cache;
5744 root->fs_info->free_extent_hook = free_extent_hook;
5745 root->fs_info->corrupt_blocks = &corrupt_blocks;
5749 bits = malloc(bits_nr * sizeof(struct block_info));
5756 add_root_to_pending(root->fs_info->tree_root->node,
5757 &extent_cache, &pending, &seen, &nodes,
5758 &root->fs_info->tree_root->root_key);
5760 add_root_to_pending(root->fs_info->chunk_root->node,
5761 &extent_cache, &pending, &seen, &nodes,
5762 &root->fs_info->chunk_root->root_key);
5764 btrfs_init_path(&path);
5767 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
5768 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
5772 leaf = path.nodes[0];
5773 slot = path.slots[0];
5774 if (slot >= btrfs_header_nritems(path.nodes[0])) {
5775 ret = btrfs_next_leaf(root, &path);
5778 leaf = path.nodes[0];
5779 slot = path.slots[0];
5781 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
5782 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
5783 unsigned long offset;
5784 struct extent_buffer *buf;
5786 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5787 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5788 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
5789 buf = read_tree_block(root->fs_info->tree_root,
5790 btrfs_root_bytenr(&ri),
5791 btrfs_level_size(root,
5792 btrfs_root_level(&ri)),
5798 add_root_to_pending(buf, &extent_cache,
5799 &pending, &seen, &nodes,
5801 free_extent_buffer(buf);
5803 struct dropping_root_item_record *dri_rec;
5804 dri_rec = malloc(sizeof(*dri_rec));
5809 memcpy(&dri_rec->ri, &ri, sizeof(ri));
5810 memcpy(&dri_rec->found_key, &found_key,
5812 list_add_tail(&dri_rec->list, &dropping_trees);
5817 btrfs_release_path(&path);
5819 ret = run_next_block(trans, root, bits, bits_nr, &last,
5820 &pending, &seen, &reada, &nodes,
5821 &extent_cache, &chunk_cache, &dev_cache,
5822 &block_group_cache, &dev_extent_cache,
5828 while (!list_empty(&dropping_trees)) {
5829 struct dropping_root_item_record *rec;
5830 struct extent_buffer *buf;
5831 rec = list_entry(dropping_trees.next,
5832 struct dropping_root_item_record, list);
5838 buf = read_tree_block(root->fs_info->tree_root,
5839 btrfs_root_bytenr(&rec->ri),
5840 btrfs_level_size(root,
5841 btrfs_root_level(&rec->ri)), 0);
5846 add_root_to_pending(buf, &extent_cache, &pending,
5847 &seen, &nodes, &rec->found_key);
5849 ret = run_next_block(trans, root, bits, bits_nr, &last,
5850 &pending, &seen, &reada,
5851 &nodes, &extent_cache,
5852 &chunk_cache, &dev_cache,
5859 free_extent_buffer(buf);
5860 list_del(&rec->list);
5865 ret = check_extent_refs(trans, root, &extent_cache);
5866 if (ret == -EAGAIN) {
5867 ret = btrfs_commit_transaction(trans, root);
5871 trans = btrfs_start_transaction(root, 1);
5872 if (IS_ERR(trans)) {
5873 ret = PTR_ERR(trans);
5877 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5878 free_extent_cache_tree(&seen);
5879 free_extent_cache_tree(&pending);
5880 free_extent_cache_tree(&reada);
5881 free_extent_cache_tree(&nodes);
5882 free_extent_record_cache(root->fs_info, &extent_cache);
5886 err = check_chunks(&chunk_cache, &block_group_cache,
5887 &dev_extent_cache, NULL, NULL, 0);
5891 err = check_devices(&dev_cache, &dev_extent_cache);
5896 err = btrfs_commit_transaction(trans, root);
5902 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5903 root->fs_info->fsck_extent_cache = NULL;
5904 root->fs_info->free_extent_hook = NULL;
5905 root->fs_info->corrupt_blocks = NULL;
5908 free_chunk_cache_tree(&chunk_cache);
5909 free_device_cache_tree(&dev_cache);
5910 free_block_group_tree(&block_group_cache);
5911 free_device_extent_tree(&dev_extent_cache);
5912 free_extent_cache_tree(&seen);
5913 free_extent_cache_tree(&pending);
5914 free_extent_cache_tree(&reada);
5915 free_extent_cache_tree(&nodes);
5919 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
5920 struct btrfs_root *root, int overwrite)
5922 struct extent_buffer *c;
5923 struct extent_buffer *old = root->node;
5926 struct btrfs_disk_key disk_key = {0,0,0};
5932 extent_buffer_get(c);
5935 c = btrfs_alloc_free_block(trans, root,
5936 btrfs_level_size(root, 0),
5937 root->root_key.objectid,
5938 &disk_key, level, 0, 0);
5941 extent_buffer_get(c);
5945 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
5946 btrfs_set_header_level(c, level);
5947 btrfs_set_header_bytenr(c, c->start);
5948 btrfs_set_header_generation(c, trans->transid);
5949 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
5950 btrfs_set_header_owner(c, root->root_key.objectid);
5952 write_extent_buffer(c, root->fs_info->fsid,
5953 btrfs_header_fsid(), BTRFS_FSID_SIZE);
5955 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
5956 btrfs_header_chunk_tree_uuid(c),
5959 btrfs_mark_buffer_dirty(c);
5961 * this case can happen in the following case:
5963 * 1.overwrite previous root.
5965 * 2.reinit reloc data root, this is because we skip pin
5966 * down reloc data tree before which means we can allocate
5967 * same block bytenr here.
5969 if (old->start == c->start) {
5970 btrfs_set_root_generation(&root->root_item,
5972 root->root_item.level = btrfs_header_level(root->node);
5973 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5974 &root->root_key, &root->root_item);
5976 free_extent_buffer(c);
5980 free_extent_buffer(old);
5982 add_root_to_dirty_list(root);
5986 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
5987 struct extent_buffer *eb, int tree_root)
5989 struct extent_buffer *tmp;
5990 struct btrfs_root_item *ri;
5991 struct btrfs_key key;
5994 int level = btrfs_header_level(eb);
5999 btrfs_pin_extent(fs_info, eb->start, eb->len);
6001 leafsize = btrfs_super_leafsize(fs_info->super_copy);
6002 nritems = btrfs_header_nritems(eb);
6003 for (i = 0; i < nritems; i++) {
6005 btrfs_item_key_to_cpu(eb, &key, i);
6006 if (key.type != BTRFS_ROOT_ITEM_KEY)
6008 /* Skip the extent root and reloc roots */
6009 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6010 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6011 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6013 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6014 bytenr = btrfs_disk_root_bytenr(eb, ri);
6017 * If at any point we start needing the real root we
6018 * will have to build a stump root for the root we are
6019 * in, but for now this doesn't actually use the root so
6020 * just pass in extent_root.
6022 tmp = read_tree_block(fs_info->extent_root, bytenr,
6025 fprintf(stderr, "Error reading root block\n");
6028 ret = pin_down_tree_blocks(fs_info, tmp, 0);
6029 free_extent_buffer(tmp);
6033 bytenr = btrfs_node_blockptr(eb, i);
6035 /* If we aren't the tree root don't read the block */
6036 if (level == 1 && !tree_root) {
6037 btrfs_pin_extent(fs_info, bytenr, leafsize);
6041 tmp = read_tree_block(fs_info->extent_root, bytenr,
6044 fprintf(stderr, "Error reading tree block\n");
6047 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6048 free_extent_buffer(tmp);
6057 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6061 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6065 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6068 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6070 struct btrfs_block_group_cache *cache;
6071 struct btrfs_path *path;
6072 struct extent_buffer *leaf;
6073 struct btrfs_chunk *chunk;
6074 struct btrfs_key key;
6078 path = btrfs_alloc_path();
6083 key.type = BTRFS_CHUNK_ITEM_KEY;
6086 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6088 btrfs_free_path(path);
6093 * We do this in case the block groups were screwed up and had alloc
6094 * bits that aren't actually set on the chunks. This happens with
6095 * restored images every time and could happen in real life I guess.
6097 fs_info->avail_data_alloc_bits = 0;
6098 fs_info->avail_metadata_alloc_bits = 0;
6099 fs_info->avail_system_alloc_bits = 0;
6101 /* First we need to create the in-memory block groups */
6103 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6104 ret = btrfs_next_leaf(fs_info->chunk_root, path);
6106 btrfs_free_path(path);
6114 leaf = path->nodes[0];
6115 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6116 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6121 chunk = btrfs_item_ptr(leaf, path->slots[0],
6122 struct btrfs_chunk);
6123 btrfs_add_block_group(fs_info, 0,
6124 btrfs_chunk_type(leaf, chunk),
6125 key.objectid, key.offset,
6126 btrfs_chunk_length(leaf, chunk));
6127 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6128 key.offset + btrfs_chunk_length(leaf, chunk),
6134 cache = btrfs_lookup_first_block_group(fs_info, start);
6138 start = cache->key.objectid + cache->key.offset;
6141 btrfs_free_path(path);
6145 static int reset_balance(struct btrfs_trans_handle *trans,
6146 struct btrfs_fs_info *fs_info)
6148 struct btrfs_root *root = fs_info->tree_root;
6149 struct btrfs_path *path;
6150 struct extent_buffer *leaf;
6151 struct btrfs_key key;
6152 int del_slot, del_nr = 0;
6156 path = btrfs_alloc_path();
6160 key.objectid = BTRFS_BALANCE_OBJECTID;
6161 key.type = BTRFS_BALANCE_ITEM_KEY;
6164 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6169 goto reinit_data_reloc;
6174 ret = btrfs_del_item(trans, root, path);
6177 btrfs_release_path(path);
6179 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6180 key.type = BTRFS_ROOT_ITEM_KEY;
6183 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6187 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6192 ret = btrfs_del_items(trans, root, path,
6199 btrfs_release_path(path);
6202 ret = btrfs_search_slot(trans, root, &key, path,
6209 leaf = path->nodes[0];
6210 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6211 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
6213 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6218 del_slot = path->slots[0];
6227 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
6231 btrfs_release_path(path);
6234 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6235 key.type = BTRFS_ROOT_ITEM_KEY;
6236 key.offset = (u64)-1;
6237 root = btrfs_read_fs_root(fs_info, &key);
6239 fprintf(stderr, "Error reading data reloc tree\n");
6240 return PTR_ERR(root);
6242 root->track_dirty = 1;
6243 if (root->last_trans != trans->transid) {
6244 root->last_trans = trans->transid;
6245 root->commit_root = root->node;
6246 extent_buffer_get(root->node);
6248 ret = btrfs_fsck_reinit_root(trans, root, 0);
6251 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
6253 btrfs_free_path(path);
6257 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
6258 struct btrfs_fs_info *fs_info)
6264 * The only reason we don't do this is because right now we're just
6265 * walking the trees we find and pinning down their bytes, we don't look
6266 * at any of the leaves. In order to do mixed groups we'd have to check
6267 * the leaves of any fs roots and pin down the bytes for any file
6268 * extents we find. Not hard but why do it if we don't have to?
6270 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
6271 fprintf(stderr, "We don't support re-initing the extent tree "
6272 "for mixed block groups yet, please notify a btrfs "
6273 "developer you want to do this so they can add this "
6274 "functionality.\n");
6279 * first we need to walk all of the trees except the extent tree and pin
6280 * down the bytes that are in use so we don't overwrite any existing
6283 ret = pin_metadata_blocks(fs_info);
6285 fprintf(stderr, "error pinning down used bytes\n");
6290 * Need to drop all the block groups since we're going to recreate all
6293 btrfs_free_block_groups(fs_info);
6294 ret = reset_block_groups(fs_info);
6296 fprintf(stderr, "error resetting the block groups\n");
6300 /* Ok we can allocate now, reinit the extent root */
6301 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
6303 fprintf(stderr, "extent root initialization failed\n");
6305 * When the transaction code is updated we should end the
6306 * transaction, but for now progs only knows about commit so
6307 * just return an error.
6313 * Now we have all the in-memory block groups setup so we can make
6314 * allocations properly, and the metadata we care about is safe since we
6315 * pinned all of it above.
6318 struct btrfs_block_group_cache *cache;
6320 cache = btrfs_lookup_first_block_group(fs_info, start);
6323 start = cache->key.objectid + cache->key.offset;
6324 ret = btrfs_insert_item(trans, fs_info->extent_root,
6325 &cache->key, &cache->item,
6326 sizeof(cache->item));
6328 fprintf(stderr, "Error adding block group\n");
6331 btrfs_extent_post_op(trans, fs_info->extent_root);
6334 ret = reset_balance(trans, fs_info);
6336 fprintf(stderr, "error reseting the pending balance\n");
6341 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
6343 struct btrfs_path *path;
6344 struct btrfs_trans_handle *trans;
6345 struct btrfs_key key;
6348 printf("Recowing metadata block %llu\n", eb->start);
6349 key.objectid = btrfs_header_owner(eb);
6350 key.type = BTRFS_ROOT_ITEM_KEY;
6351 key.offset = (u64)-1;
6353 root = btrfs_read_fs_root(root->fs_info, &key);
6355 fprintf(stderr, "Couldn't find owner root %llu\n",
6357 return PTR_ERR(root);
6360 path = btrfs_alloc_path();
6364 trans = btrfs_start_transaction(root, 1);
6365 if (IS_ERR(trans)) {
6366 btrfs_free_path(path);
6367 return PTR_ERR(trans);
6370 path->lowest_level = btrfs_header_level(eb);
6371 if (path->lowest_level)
6372 btrfs_node_key_to_cpu(eb, &key, 0);
6374 btrfs_item_key_to_cpu(eb, &key, 0);
6376 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6377 btrfs_commit_transaction(trans, root);
6378 btrfs_free_path(path);
6382 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
6384 struct btrfs_path *path;
6385 struct btrfs_trans_handle *trans;
6386 struct btrfs_key key;
6389 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
6390 bad->key.type, bad->key.offset);
6391 key.objectid = bad->root_id;
6392 key.type = BTRFS_ROOT_ITEM_KEY;
6393 key.offset = (u64)-1;
6395 root = btrfs_read_fs_root(root->fs_info, &key);
6397 fprintf(stderr, "Couldn't find owner root %llu\n",
6399 return PTR_ERR(root);
6402 path = btrfs_alloc_path();
6406 trans = btrfs_start_transaction(root, 1);
6407 if (IS_ERR(trans)) {
6408 btrfs_free_path(path);
6409 return PTR_ERR(trans);
6412 ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
6418 ret = btrfs_del_item(trans, root, path);
6420 btrfs_commit_transaction(trans, root);
6421 btrfs_free_path(path);
6425 static struct option long_options[] = {
6426 { "super", 1, NULL, 's' },
6427 { "repair", 0, NULL, 0 },
6428 { "init-csum-tree", 0, NULL, 0 },
6429 { "init-extent-tree", 0, NULL, 0 },
6430 { "backup", 0, NULL, 0 },
6434 const char * const cmd_check_usage[] = {
6435 "btrfs check [options] <device>",
6436 "Check an unmounted btrfs filesystem.",
6438 "-s|--super <superblock> use this superblock copy",
6439 "-b|--backup use the backup root copy",
6440 "--repair try to repair the filesystem",
6441 "--init-csum-tree create a new CRC tree",
6442 "--init-extent-tree create a new extent tree",
6446 int cmd_check(int argc, char **argv)
6448 struct cache_tree root_cache;
6449 struct btrfs_root *root;
6450 struct btrfs_fs_info *info;
6452 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
6455 int option_index = 0;
6456 int init_csum_tree = 0;
6457 enum btrfs_open_ctree_flags ctree_flags =
6458 OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
6462 c = getopt_long(argc, argv, "as:b", long_options,
6467 case 'a': /* ignored */ break;
6469 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6472 num = arg_strtou64(optarg);
6473 if (num >= BTRFS_SUPER_MIRROR_MAX) {
6475 "ERROR: super mirror should be less than: %d\n",
6476 BTRFS_SUPER_MIRROR_MAX);
6479 bytenr = btrfs_sb_offset(((int)num));
6480 printf("using SB copy %llu, bytenr %llu\n", num,
6481 (unsigned long long)bytenr);
6485 usage(cmd_check_usage);
6487 if (option_index == 1) {
6488 printf("enabling repair mode\n");
6490 ctree_flags |= OPEN_CTREE_WRITES;
6491 } else if (option_index == 2) {
6492 printf("Creating a new CRC tree\n");
6495 ctree_flags |= OPEN_CTREE_WRITES;
6496 } else if (option_index == 3) {
6497 init_extent_tree = 1;
6498 ctree_flags |= (OPEN_CTREE_WRITES |
6499 OPEN_CTREE_NO_BLOCK_GROUPS);
6504 argc = argc - optind;
6507 usage(cmd_check_usage);
6510 cache_tree_init(&root_cache);
6512 if((ret = check_mounted(argv[optind])) < 0) {
6513 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6516 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6521 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6523 fprintf(stderr, "Couldn't open file system\n");
6528 root = info->fs_root;
6529 uuid_unparse(info->super_copy->fsid, uuidbuf);
6530 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6532 if (!extent_buffer_uptodate(info->tree_root->node) ||
6533 !extent_buffer_uptodate(info->dev_root->node) ||
6534 !extent_buffer_uptodate(info->chunk_root->node)) {
6535 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6540 if (init_extent_tree || init_csum_tree) {
6541 struct btrfs_trans_handle *trans;
6543 trans = btrfs_start_transaction(info->extent_root, 0);
6544 if (IS_ERR(trans)) {
6545 fprintf(stderr, "Error starting transaction\n");
6546 ret = PTR_ERR(trans);
6550 if (init_extent_tree) {
6551 printf("Creating a new extent tree\n");
6552 ret = reinit_extent_tree(trans, info);
6557 if (init_csum_tree) {
6558 fprintf(stderr, "Reinit crc root\n");
6559 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6561 fprintf(stderr, "crc root initialization failed\n");
6567 * Ok now we commit and run the normal fsck, which will add
6568 * extent entries for all of the items it finds.
6570 ret = btrfs_commit_transaction(trans, info->extent_root);
6574 if (!extent_buffer_uptodate(info->extent_root->node)) {
6575 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6580 fprintf(stderr, "checking extents\n");
6581 ret = check_chunks_and_extents(root);
6583 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6585 fprintf(stderr, "checking free space cache\n");
6586 ret = check_space_cache(root);
6591 * We used to have to have these hole extents in between our real
6592 * extents so if we don't have this flag set we need to make sure there
6593 * are no gaps in the file extents for inodes, otherwise we can just
6594 * ignore it when this happens.
6596 no_holes = btrfs_fs_incompat(root->fs_info,
6597 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
6598 fprintf(stderr, "checking fs roots\n");
6599 ret = check_fs_roots(root, &root_cache);
6603 fprintf(stderr, "checking csums\n");
6604 ret = check_csums(root);
6608 fprintf(stderr, "checking root refs\n");
6609 ret = check_root_refs(root, &root_cache);
6613 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
6614 struct extent_buffer *eb;
6616 eb = list_first_entry(&root->fs_info->recow_ebs,
6617 struct extent_buffer, recow);
6618 ret = recow_extent_buffer(root, eb);
6623 while (!list_empty(&delete_items)) {
6624 struct bad_item *bad;
6626 bad = list_first_entry(&delete_items, struct bad_item, list);
6627 list_del_init(&bad->list);
6629 ret = delete_bad_item(root, bad);
6633 if (!list_empty(&root->fs_info->recow_ebs)) {
6634 fprintf(stderr, "Transid errors in file system\n");
6638 if (found_old_backref) { /*
6639 * there was a disk format change when mixed
6640 * backref was in testing tree. The old format
6641 * existed about one week.
6643 printf("\n * Found old mixed backref format. "
6644 "The old format is not supported! *"
6645 "\n * Please mount the FS in readonly mode, "
6646 "backup data and re-format the FS. *\n\n");
6649 printf("found %llu bytes used err is %d\n",
6650 (unsigned long long)bytes_used, ret);
6651 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
6652 printf("total tree bytes: %llu\n",
6653 (unsigned long long)total_btree_bytes);
6654 printf("total fs tree bytes: %llu\n",
6655 (unsigned long long)total_fs_tree_bytes);
6656 printf("total extent tree bytes: %llu\n",
6657 (unsigned long long)total_extent_tree_bytes);
6658 printf("btree space waste bytes: %llu\n",
6659 (unsigned long long)btree_space_waste);
6660 printf("file data blocks allocated: %llu\n referenced %llu\n",
6661 (unsigned long long)data_bytes_allocated,
6662 (unsigned long long)data_bytes_referenced);
6663 printf("%s\n", BTRFS_BUILD_VERSION);
6665 free_root_recs_tree(&root_cache);