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.
23 #include <sys/types.h>
27 #include <uuid/uuid.h>
32 #include "print-tree.h"
33 #include "transaction.h"
36 #include "free-space-cache.h"
38 #include "qgroup-verify.h"
39 #include "rbtree-utils.h"
43 static u64 bytes_used = 0;
44 static u64 total_csum_bytes = 0;
45 static u64 total_btree_bytes = 0;
46 static u64 total_fs_tree_bytes = 0;
47 static u64 total_extent_tree_bytes = 0;
48 static u64 btree_space_waste = 0;
49 static u64 data_bytes_allocated = 0;
50 static u64 data_bytes_referenced = 0;
51 static int found_old_backref = 0;
52 static LIST_HEAD(duplicate_extents);
53 static LIST_HEAD(delete_items);
54 static int repair = 0;
55 static int no_holes = 0;
56 static int init_extent_tree = 0;
57 static int check_data_csum = 0;
59 struct extent_backref {
60 struct list_head list;
61 unsigned int is_data:1;
62 unsigned int found_extent_tree:1;
63 unsigned int full_backref:1;
64 unsigned int found_ref:1;
65 unsigned int broken:1;
69 struct extent_backref node;
84 * Much like data_backref, just removed the undetermined members
85 * and change it to use list_head.
86 * Stored in the root->orphan_data_extents list
88 struct orphan_data_extent {
89 struct list_head list;
98 struct extent_backref node;
105 struct extent_record {
106 struct list_head backrefs;
107 struct list_head dups;
108 struct list_head list;
109 struct cache_extent cache;
110 struct btrfs_disk_key parent_key;
115 u64 extent_item_refs;
117 u64 parent_generation;
121 unsigned int found_rec:1;
122 unsigned int content_checked:1;
123 unsigned int owner_ref_checked:1;
124 unsigned int is_root:1;
125 unsigned int metadata:1;
126 unsigned int flag_block_full_backref:1;
129 struct inode_backref {
130 struct list_head list;
131 unsigned int found_dir_item:1;
132 unsigned int found_dir_index:1;
133 unsigned int found_inode_ref:1;
134 unsigned int filetype:8;
136 unsigned int ref_type;
143 struct root_item_record {
144 struct list_head list;
150 struct btrfs_key drop_key;
153 #define REF_ERR_NO_DIR_ITEM (1 << 0)
154 #define REF_ERR_NO_DIR_INDEX (1 << 1)
155 #define REF_ERR_NO_INODE_REF (1 << 2)
156 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
157 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
158 #define REF_ERR_DUP_INODE_REF (1 << 5)
159 #define REF_ERR_INDEX_UNMATCH (1 << 6)
160 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
161 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
162 #define REF_ERR_NO_ROOT_REF (1 << 9)
163 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
164 #define REF_ERR_DUP_ROOT_REF (1 << 11)
165 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
167 struct file_extent_hole {
173 /* Compatible function to allow reuse of old codes */
174 static u64 first_extent_gap(struct rb_root *holes)
176 struct file_extent_hole *hole;
178 if (RB_EMPTY_ROOT(holes))
181 hole = rb_entry(rb_first(holes), struct file_extent_hole, node);
185 int compare_hole(struct rb_node *node1, struct rb_node *node2)
187 struct file_extent_hole *hole1;
188 struct file_extent_hole *hole2;
190 hole1 = rb_entry(node1, struct file_extent_hole, node);
191 hole2 = rb_entry(node2, struct file_extent_hole, node);
193 if (hole1->start > hole2->start)
195 if (hole1->start < hole2->start)
197 /* Now hole1->start == hole2->start */
198 if (hole1->len >= hole2->len)
200 * Hole 1 will be merge center
201 * Same hole will be merged later
204 /* Hole 2 will be merge center */
209 * Add a hole to the record
211 * This will do hole merge for copy_file_extent_holes(),
212 * which will ensure there won't be continuous holes.
214 static int add_file_extent_hole(struct rb_root *holes,
217 struct file_extent_hole *hole;
218 struct file_extent_hole *prev = NULL;
219 struct file_extent_hole *next = NULL;
221 hole = malloc(sizeof(*hole));
226 /* Since compare will not return 0, no -EEXIST will happen */
227 rb_insert(holes, &hole->node, compare_hole);
229 /* simple merge with previous hole */
230 if (rb_prev(&hole->node))
231 prev = rb_entry(rb_prev(&hole->node), struct file_extent_hole,
233 if (prev && prev->start + prev->len >= hole->start) {
234 hole->len = hole->start + hole->len - prev->start;
235 hole->start = prev->start;
236 rb_erase(&prev->node, holes);
241 /* iterate merge with next holes */
243 if (!rb_next(&hole->node))
245 next = rb_entry(rb_next(&hole->node), struct file_extent_hole,
247 if (hole->start + hole->len >= next->start) {
248 if (hole->start + hole->len <= next->start + next->len)
249 hole->len = next->start + next->len -
251 rb_erase(&next->node, holes);
260 static int compare_hole_range(struct rb_node *node, void *data)
262 struct file_extent_hole *hole;
265 hole = (struct file_extent_hole *)data;
268 hole = rb_entry(node, struct file_extent_hole, node);
269 if (start < hole->start)
271 if (start >= hole->start && start < hole->start + hole->len)
277 * Delete a hole in the record
279 * This will do the hole split and is much restrict than add.
281 static int del_file_extent_hole(struct rb_root *holes,
284 struct file_extent_hole *hole;
285 struct file_extent_hole tmp;
286 struct file_extent_hole prev;
287 struct file_extent_hole next;
288 struct rb_node *node;
295 node = rb_search(holes, &tmp, compare_hole_range, NULL);
298 hole = rb_entry(node, struct file_extent_hole, node);
299 if (start + len > hole->start + hole->len)
303 * Now there will be no overflap, delete the hole and re-add the
304 * split(s) if they exists.
306 if (start > hole->start) {
307 prev.start = hole->start;
308 prev.len = start - hole->start;
311 if (hole->start + hole->len > start + len) {
312 next.start = start + len;
313 next.len = hole->start + hole->len - start - len;
316 rb_erase(node, holes);
319 ret = add_file_extent_hole(holes, prev.start, prev.len);
324 ret = add_file_extent_hole(holes, next.start, next.len);
331 static int copy_file_extent_holes(struct rb_root *dst,
334 struct file_extent_hole *hole;
335 struct rb_node *node;
338 node = rb_first(src);
340 hole = rb_entry(node, struct file_extent_hole, node);
341 ret = add_file_extent_hole(dst, hole->start, hole->len);
344 node = rb_next(node);
349 static void free_file_extent_holes(struct rb_root *holes)
351 struct rb_node *node;
352 struct file_extent_hole *hole;
354 node = rb_first(holes);
356 hole = rb_entry(node, struct file_extent_hole, node);
357 rb_erase(node, holes);
359 node = rb_first(holes);
363 struct inode_record {
364 struct list_head backrefs;
365 unsigned int checked:1;
366 unsigned int merging:1;
367 unsigned int found_inode_item:1;
368 unsigned int found_dir_item:1;
369 unsigned int found_file_extent:1;
370 unsigned int found_csum_item:1;
371 unsigned int some_csum_missing:1;
372 unsigned int nodatasum:1;
385 struct rb_root holes;
390 #define I_ERR_NO_INODE_ITEM (1 << 0)
391 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
392 #define I_ERR_DUP_INODE_ITEM (1 << 2)
393 #define I_ERR_DUP_DIR_INDEX (1 << 3)
394 #define I_ERR_ODD_DIR_ITEM (1 << 4)
395 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
396 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
397 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
398 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
399 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
400 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
401 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
402 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
403 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
405 struct root_backref {
406 struct list_head list;
407 unsigned int found_dir_item:1;
408 unsigned int found_dir_index:1;
409 unsigned int found_back_ref:1;
410 unsigned int found_forward_ref:1;
411 unsigned int reachable:1;
421 struct list_head backrefs;
422 struct cache_extent cache;
423 unsigned int found_root_item:1;
429 struct cache_extent cache;
434 struct cache_extent cache;
435 struct cache_tree root_cache;
436 struct cache_tree inode_cache;
437 struct inode_record *current;
446 struct walk_control {
447 struct cache_tree shared;
448 struct shared_node *nodes[BTRFS_MAX_LEVEL];
454 struct btrfs_key key;
456 struct list_head list;
459 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
461 static void record_root_in_trans(struct btrfs_trans_handle *trans,
462 struct btrfs_root *root)
464 if (root->last_trans != trans->transid) {
465 root->track_dirty = 1;
466 root->last_trans = trans->transid;
467 root->commit_root = root->node;
468 extent_buffer_get(root->node);
472 static u8 imode_to_type(u32 imode)
475 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
476 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
477 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
478 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
479 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
480 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
481 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
482 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
485 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
489 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
491 struct device_record *rec1;
492 struct device_record *rec2;
494 rec1 = rb_entry(node1, struct device_record, node);
495 rec2 = rb_entry(node2, struct device_record, node);
496 if (rec1->devid > rec2->devid)
498 else if (rec1->devid < rec2->devid)
504 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
506 struct inode_record *rec;
507 struct inode_backref *backref;
508 struct inode_backref *orig;
511 rec = malloc(sizeof(*rec));
512 memcpy(rec, orig_rec, sizeof(*rec));
514 INIT_LIST_HEAD(&rec->backrefs);
516 list_for_each_entry(orig, &orig_rec->backrefs, list) {
517 size = sizeof(*orig) + orig->namelen + 1;
518 backref = malloc(size);
519 memcpy(backref, orig, size);
520 list_add_tail(&backref->list, &rec->backrefs);
525 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
527 u64 root_objectid = root->root_key.objectid;
528 int errors = rec->errors;
532 /* reloc root errors, we print its corresponding fs root objectid*/
533 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
534 root_objectid = root->root_key.offset;
535 fprintf(stderr, "reloc");
537 fprintf(stderr, "root %llu inode %llu errors %x",
538 (unsigned long long) root_objectid,
539 (unsigned long long) rec->ino, rec->errors);
541 if (errors & I_ERR_NO_INODE_ITEM)
542 fprintf(stderr, ", no inode item");
543 if (errors & I_ERR_NO_ORPHAN_ITEM)
544 fprintf(stderr, ", no orphan item");
545 if (errors & I_ERR_DUP_INODE_ITEM)
546 fprintf(stderr, ", dup inode item");
547 if (errors & I_ERR_DUP_DIR_INDEX)
548 fprintf(stderr, ", dup dir index");
549 if (errors & I_ERR_ODD_DIR_ITEM)
550 fprintf(stderr, ", odd dir item");
551 if (errors & I_ERR_ODD_FILE_EXTENT)
552 fprintf(stderr, ", odd file extent");
553 if (errors & I_ERR_BAD_FILE_EXTENT)
554 fprintf(stderr, ", bad file extent");
555 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
556 fprintf(stderr, ", file extent overlap");
557 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
558 fprintf(stderr, ", file extent discount");
559 if (errors & I_ERR_DIR_ISIZE_WRONG)
560 fprintf(stderr, ", dir isize wrong");
561 if (errors & I_ERR_FILE_NBYTES_WRONG)
562 fprintf(stderr, ", nbytes wrong");
563 if (errors & I_ERR_ODD_CSUM_ITEM)
564 fprintf(stderr, ", odd csum item");
565 if (errors & I_ERR_SOME_CSUM_MISSING)
566 fprintf(stderr, ", some csum missing");
567 if (errors & I_ERR_LINK_COUNT_WRONG)
568 fprintf(stderr, ", link count wrong");
569 fprintf(stderr, "\n");
570 /* Print the holes if needed */
571 if (errors & I_ERR_FILE_EXTENT_DISCOUNT) {
572 struct file_extent_hole *hole;
573 struct rb_node *node;
575 node = rb_first(&rec->holes);
576 fprintf(stderr, "Found file extent holes:\n");
578 hole = rb_entry(node, struct file_extent_hole, node);
579 fprintf(stderr, "\tstart: %llu, len:%llu\n",
580 hole->start, hole->len);
581 node = rb_next(node);
586 static void print_ref_error(int errors)
588 if (errors & REF_ERR_NO_DIR_ITEM)
589 fprintf(stderr, ", no dir item");
590 if (errors & REF_ERR_NO_DIR_INDEX)
591 fprintf(stderr, ", no dir index");
592 if (errors & REF_ERR_NO_INODE_REF)
593 fprintf(stderr, ", no inode ref");
594 if (errors & REF_ERR_DUP_DIR_ITEM)
595 fprintf(stderr, ", dup dir item");
596 if (errors & REF_ERR_DUP_DIR_INDEX)
597 fprintf(stderr, ", dup dir index");
598 if (errors & REF_ERR_DUP_INODE_REF)
599 fprintf(stderr, ", dup inode ref");
600 if (errors & REF_ERR_INDEX_UNMATCH)
601 fprintf(stderr, ", index unmatch");
602 if (errors & REF_ERR_FILETYPE_UNMATCH)
603 fprintf(stderr, ", filetype unmatch");
604 if (errors & REF_ERR_NAME_TOO_LONG)
605 fprintf(stderr, ", name too long");
606 if (errors & REF_ERR_NO_ROOT_REF)
607 fprintf(stderr, ", no root ref");
608 if (errors & REF_ERR_NO_ROOT_BACKREF)
609 fprintf(stderr, ", no root backref");
610 if (errors & REF_ERR_DUP_ROOT_REF)
611 fprintf(stderr, ", dup root ref");
612 if (errors & REF_ERR_DUP_ROOT_BACKREF)
613 fprintf(stderr, ", dup root backref");
614 fprintf(stderr, "\n");
617 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
620 struct ptr_node *node;
621 struct cache_extent *cache;
622 struct inode_record *rec = NULL;
625 cache = lookup_cache_extent(inode_cache, ino, 1);
627 node = container_of(cache, struct ptr_node, cache);
629 if (mod && rec->refs > 1) {
630 node->data = clone_inode_rec(rec);
635 rec = calloc(1, sizeof(*rec));
637 rec->extent_start = (u64)-1;
639 INIT_LIST_HEAD(&rec->backrefs);
640 rec->holes = RB_ROOT;
642 node = malloc(sizeof(*node));
643 node->cache.start = ino;
644 node->cache.size = 1;
647 if (ino == BTRFS_FREE_INO_OBJECTID)
650 ret = insert_cache_extent(inode_cache, &node->cache);
656 static void free_inode_rec(struct inode_record *rec)
658 struct inode_backref *backref;
663 while (!list_empty(&rec->backrefs)) {
664 backref = list_entry(rec->backrefs.next,
665 struct inode_backref, list);
666 list_del(&backref->list);
669 free_file_extent_holes(&rec->holes);
673 static int can_free_inode_rec(struct inode_record *rec)
675 if (!rec->errors && rec->checked && rec->found_inode_item &&
676 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
681 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
682 struct inode_record *rec)
684 struct cache_extent *cache;
685 struct inode_backref *tmp, *backref;
686 struct ptr_node *node;
687 unsigned char filetype;
689 if (!rec->found_inode_item)
692 filetype = imode_to_type(rec->imode);
693 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
694 if (backref->found_dir_item && backref->found_dir_index) {
695 if (backref->filetype != filetype)
696 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
697 if (!backref->errors && backref->found_inode_ref) {
698 list_del(&backref->list);
704 if (!rec->checked || rec->merging)
707 if (S_ISDIR(rec->imode)) {
708 if (rec->found_size != rec->isize)
709 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
710 if (rec->found_file_extent)
711 rec->errors |= I_ERR_ODD_FILE_EXTENT;
712 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
713 if (rec->found_dir_item)
714 rec->errors |= I_ERR_ODD_DIR_ITEM;
715 if (rec->found_size != rec->nbytes)
716 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
717 if (rec->nlink > 0 && !no_holes &&
718 (rec->extent_end < rec->isize ||
719 first_extent_gap(&rec->holes) < rec->isize))
720 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
723 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
724 if (rec->found_csum_item && rec->nodatasum)
725 rec->errors |= I_ERR_ODD_CSUM_ITEM;
726 if (rec->some_csum_missing && !rec->nodatasum)
727 rec->errors |= I_ERR_SOME_CSUM_MISSING;
730 BUG_ON(rec->refs != 1);
731 if (can_free_inode_rec(rec)) {
732 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
733 node = container_of(cache, struct ptr_node, cache);
734 BUG_ON(node->data != rec);
735 remove_cache_extent(inode_cache, &node->cache);
741 static int check_orphan_item(struct btrfs_root *root, u64 ino)
743 struct btrfs_path path;
744 struct btrfs_key key;
747 key.objectid = BTRFS_ORPHAN_OBJECTID;
748 key.type = BTRFS_ORPHAN_ITEM_KEY;
751 btrfs_init_path(&path);
752 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
753 btrfs_release_path(&path);
759 static int process_inode_item(struct extent_buffer *eb,
760 int slot, struct btrfs_key *key,
761 struct shared_node *active_node)
763 struct inode_record *rec;
764 struct btrfs_inode_item *item;
766 rec = active_node->current;
767 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
768 if (rec->found_inode_item) {
769 rec->errors |= I_ERR_DUP_INODE_ITEM;
772 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
773 rec->nlink = btrfs_inode_nlink(eb, item);
774 rec->isize = btrfs_inode_size(eb, item);
775 rec->nbytes = btrfs_inode_nbytes(eb, item);
776 rec->imode = btrfs_inode_mode(eb, item);
777 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
779 rec->found_inode_item = 1;
781 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
782 maybe_free_inode_rec(&active_node->inode_cache, rec);
786 static struct inode_backref *get_inode_backref(struct inode_record *rec,
788 int namelen, u64 dir)
790 struct inode_backref *backref;
792 list_for_each_entry(backref, &rec->backrefs, list) {
793 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
795 if (backref->dir != dir || backref->namelen != namelen)
797 if (memcmp(name, backref->name, namelen))
802 backref = malloc(sizeof(*backref) + namelen + 1);
803 memset(backref, 0, sizeof(*backref));
805 backref->namelen = namelen;
806 memcpy(backref->name, name, namelen);
807 backref->name[namelen] = '\0';
808 list_add_tail(&backref->list, &rec->backrefs);
812 static int add_inode_backref(struct cache_tree *inode_cache,
813 u64 ino, u64 dir, u64 index,
814 const char *name, int namelen,
815 int filetype, int itemtype, int errors)
817 struct inode_record *rec;
818 struct inode_backref *backref;
820 rec = get_inode_rec(inode_cache, ino, 1);
821 backref = get_inode_backref(rec, name, namelen, dir);
823 backref->errors |= errors;
824 if (itemtype == BTRFS_DIR_INDEX_KEY) {
825 if (backref->found_dir_index)
826 backref->errors |= REF_ERR_DUP_DIR_INDEX;
827 if (backref->found_inode_ref && backref->index != index)
828 backref->errors |= REF_ERR_INDEX_UNMATCH;
829 if (backref->found_dir_item && backref->filetype != filetype)
830 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
832 backref->index = index;
833 backref->filetype = filetype;
834 backref->found_dir_index = 1;
835 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
837 if (backref->found_dir_item)
838 backref->errors |= REF_ERR_DUP_DIR_ITEM;
839 if (backref->found_dir_index && backref->filetype != filetype)
840 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
842 backref->filetype = filetype;
843 backref->found_dir_item = 1;
844 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
845 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
846 if (backref->found_inode_ref)
847 backref->errors |= REF_ERR_DUP_INODE_REF;
848 if (backref->found_dir_index && backref->index != index)
849 backref->errors |= REF_ERR_INDEX_UNMATCH;
851 backref->index = index;
853 backref->ref_type = itemtype;
854 backref->found_inode_ref = 1;
859 maybe_free_inode_rec(inode_cache, rec);
863 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
864 struct cache_tree *dst_cache)
866 struct inode_backref *backref;
871 list_for_each_entry(backref, &src->backrefs, list) {
872 if (backref->found_dir_index) {
873 add_inode_backref(dst_cache, dst->ino, backref->dir,
874 backref->index, backref->name,
875 backref->namelen, backref->filetype,
876 BTRFS_DIR_INDEX_KEY, backref->errors);
878 if (backref->found_dir_item) {
880 add_inode_backref(dst_cache, dst->ino,
881 backref->dir, 0, backref->name,
882 backref->namelen, backref->filetype,
883 BTRFS_DIR_ITEM_KEY, backref->errors);
885 if (backref->found_inode_ref) {
886 add_inode_backref(dst_cache, dst->ino,
887 backref->dir, backref->index,
888 backref->name, backref->namelen, 0,
889 backref->ref_type, backref->errors);
893 if (src->found_dir_item)
894 dst->found_dir_item = 1;
895 if (src->found_file_extent)
896 dst->found_file_extent = 1;
897 if (src->found_csum_item)
898 dst->found_csum_item = 1;
899 if (src->some_csum_missing)
900 dst->some_csum_missing = 1;
901 if (first_extent_gap(&dst->holes) > first_extent_gap(&src->holes)) {
902 ret = copy_file_extent_holes(&dst->holes, &src->holes);
907 BUG_ON(src->found_link < dir_count);
908 dst->found_link += src->found_link - dir_count;
909 dst->found_size += src->found_size;
910 if (src->extent_start != (u64)-1) {
911 if (dst->extent_start == (u64)-1) {
912 dst->extent_start = src->extent_start;
913 dst->extent_end = src->extent_end;
915 if (dst->extent_end > src->extent_start)
916 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
917 else if (dst->extent_end < src->extent_start) {
918 ret = add_file_extent_hole(&dst->holes,
920 src->extent_start - dst->extent_end);
922 if (dst->extent_end < src->extent_end)
923 dst->extent_end = src->extent_end;
927 dst->errors |= src->errors;
928 if (src->found_inode_item) {
929 if (!dst->found_inode_item) {
930 dst->nlink = src->nlink;
931 dst->isize = src->isize;
932 dst->nbytes = src->nbytes;
933 dst->imode = src->imode;
934 dst->nodatasum = src->nodatasum;
935 dst->found_inode_item = 1;
937 dst->errors |= I_ERR_DUP_INODE_ITEM;
945 static int splice_shared_node(struct shared_node *src_node,
946 struct shared_node *dst_node)
948 struct cache_extent *cache;
949 struct ptr_node *node, *ins;
950 struct cache_tree *src, *dst;
951 struct inode_record *rec, *conflict;
956 if (--src_node->refs == 0)
958 if (src_node->current)
959 current_ino = src_node->current->ino;
961 src = &src_node->root_cache;
962 dst = &dst_node->root_cache;
964 cache = search_cache_extent(src, 0);
966 node = container_of(cache, struct ptr_node, cache);
968 cache = next_cache_extent(cache);
971 remove_cache_extent(src, &node->cache);
974 ins = malloc(sizeof(*ins));
975 ins->cache.start = node->cache.start;
976 ins->cache.size = node->cache.size;
980 ret = insert_cache_extent(dst, &ins->cache);
981 if (ret == -EEXIST) {
982 conflict = get_inode_rec(dst, rec->ino, 1);
983 merge_inode_recs(rec, conflict, dst);
985 conflict->checked = 1;
986 if (dst_node->current == conflict)
987 dst_node->current = NULL;
989 maybe_free_inode_rec(dst, conflict);
997 if (src == &src_node->root_cache) {
998 src = &src_node->inode_cache;
999 dst = &dst_node->inode_cache;
1003 if (current_ino > 0 && (!dst_node->current ||
1004 current_ino > dst_node->current->ino)) {
1005 if (dst_node->current) {
1006 dst_node->current->checked = 1;
1007 maybe_free_inode_rec(dst, dst_node->current);
1009 dst_node->current = get_inode_rec(dst, current_ino, 1);
1014 static void free_inode_ptr(struct cache_extent *cache)
1016 struct ptr_node *node;
1017 struct inode_record *rec;
1019 node = container_of(cache, struct ptr_node, cache);
1021 free_inode_rec(rec);
1025 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
1027 static struct shared_node *find_shared_node(struct cache_tree *shared,
1030 struct cache_extent *cache;
1031 struct shared_node *node;
1033 cache = lookup_cache_extent(shared, bytenr, 1);
1035 node = container_of(cache, struct shared_node, cache);
1041 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
1044 struct shared_node *node;
1046 node = calloc(1, sizeof(*node));
1047 node->cache.start = bytenr;
1048 node->cache.size = 1;
1049 cache_tree_init(&node->root_cache);
1050 cache_tree_init(&node->inode_cache);
1053 ret = insert_cache_extent(shared, &node->cache);
1058 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
1059 struct walk_control *wc, int level)
1061 struct shared_node *node;
1062 struct shared_node *dest;
1064 if (level == wc->active_node)
1067 BUG_ON(wc->active_node <= level);
1068 node = find_shared_node(&wc->shared, bytenr);
1070 add_shared_node(&wc->shared, bytenr, refs);
1071 node = find_shared_node(&wc->shared, bytenr);
1072 wc->nodes[level] = node;
1073 wc->active_node = level;
1077 if (wc->root_level == wc->active_node &&
1078 btrfs_root_refs(&root->root_item) == 0) {
1079 if (--node->refs == 0) {
1080 free_inode_recs_tree(&node->root_cache);
1081 free_inode_recs_tree(&node->inode_cache);
1082 remove_cache_extent(&wc->shared, &node->cache);
1088 dest = wc->nodes[wc->active_node];
1089 splice_shared_node(node, dest);
1090 if (node->refs == 0) {
1091 remove_cache_extent(&wc->shared, &node->cache);
1097 static int leave_shared_node(struct btrfs_root *root,
1098 struct walk_control *wc, int level)
1100 struct shared_node *node;
1101 struct shared_node *dest;
1104 if (level == wc->root_level)
1107 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
1111 BUG_ON(i >= BTRFS_MAX_LEVEL);
1113 node = wc->nodes[wc->active_node];
1114 wc->nodes[wc->active_node] = NULL;
1115 wc->active_node = i;
1117 dest = wc->nodes[wc->active_node];
1118 if (wc->active_node < wc->root_level ||
1119 btrfs_root_refs(&root->root_item) > 0) {
1120 BUG_ON(node->refs <= 1);
1121 splice_shared_node(node, dest);
1123 BUG_ON(node->refs < 2);
1132 * 1 - if the root with id child_root_id is a child of root parent_root_id
1133 * 0 - if the root child_root_id isn't a child of the root parent_root_id but
1134 * has other root(s) as parent(s)
1135 * 2 - if the root child_root_id doesn't have any parent roots
1137 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
1140 struct btrfs_path path;
1141 struct btrfs_key key;
1142 struct extent_buffer *leaf;
1146 btrfs_init_path(&path);
1148 key.objectid = parent_root_id;
1149 key.type = BTRFS_ROOT_REF_KEY;
1150 key.offset = child_root_id;
1151 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1155 btrfs_release_path(&path);
1159 key.objectid = child_root_id;
1160 key.type = BTRFS_ROOT_BACKREF_KEY;
1162 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1168 leaf = path.nodes[0];
1169 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1170 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
1173 leaf = path.nodes[0];
1176 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1177 if (key.objectid != child_root_id ||
1178 key.type != BTRFS_ROOT_BACKREF_KEY)
1183 if (key.offset == parent_root_id) {
1184 btrfs_release_path(&path);
1191 btrfs_release_path(&path);
1194 return has_parent ? 0 : 2;
1197 static int process_dir_item(struct btrfs_root *root,
1198 struct extent_buffer *eb,
1199 int slot, struct btrfs_key *key,
1200 struct shared_node *active_node)
1210 struct btrfs_dir_item *di;
1211 struct inode_record *rec;
1212 struct cache_tree *root_cache;
1213 struct cache_tree *inode_cache;
1214 struct btrfs_key location;
1215 char namebuf[BTRFS_NAME_LEN];
1217 root_cache = &active_node->root_cache;
1218 inode_cache = &active_node->inode_cache;
1219 rec = active_node->current;
1220 rec->found_dir_item = 1;
1222 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1223 total = btrfs_item_size_nr(eb, slot);
1224 while (cur < total) {
1226 btrfs_dir_item_key_to_cpu(eb, di, &location);
1227 name_len = btrfs_dir_name_len(eb, di);
1228 data_len = btrfs_dir_data_len(eb, di);
1229 filetype = btrfs_dir_type(eb, di);
1231 rec->found_size += name_len;
1232 if (name_len <= BTRFS_NAME_LEN) {
1236 len = BTRFS_NAME_LEN;
1237 error = REF_ERR_NAME_TOO_LONG;
1239 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
1241 if (location.type == BTRFS_INODE_ITEM_KEY) {
1242 add_inode_backref(inode_cache, location.objectid,
1243 key->objectid, key->offset, namebuf,
1244 len, filetype, key->type, error);
1245 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
1246 add_inode_backref(root_cache, location.objectid,
1247 key->objectid, key->offset,
1248 namebuf, len, filetype,
1251 fprintf(stderr, "invalid location in dir item %u\n",
1253 add_inode_backref(inode_cache, BTRFS_MULTIPLE_OBJECTIDS,
1254 key->objectid, key->offset, namebuf,
1255 len, filetype, key->type, error);
1258 len = sizeof(*di) + name_len + data_len;
1259 di = (struct btrfs_dir_item *)((char *)di + len);
1262 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1263 rec->errors |= I_ERR_DUP_DIR_INDEX;
1268 static int process_inode_ref(struct extent_buffer *eb,
1269 int slot, struct btrfs_key *key,
1270 struct shared_node *active_node)
1278 struct cache_tree *inode_cache;
1279 struct btrfs_inode_ref *ref;
1280 char namebuf[BTRFS_NAME_LEN];
1282 inode_cache = &active_node->inode_cache;
1284 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1285 total = btrfs_item_size_nr(eb, slot);
1286 while (cur < total) {
1287 name_len = btrfs_inode_ref_name_len(eb, ref);
1288 index = btrfs_inode_ref_index(eb, ref);
1289 if (name_len <= BTRFS_NAME_LEN) {
1293 len = BTRFS_NAME_LEN;
1294 error = REF_ERR_NAME_TOO_LONG;
1296 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1297 add_inode_backref(inode_cache, key->objectid, key->offset,
1298 index, namebuf, len, 0, key->type, error);
1300 len = sizeof(*ref) + name_len;
1301 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1307 static int process_inode_extref(struct extent_buffer *eb,
1308 int slot, struct btrfs_key *key,
1309 struct shared_node *active_node)
1318 struct cache_tree *inode_cache;
1319 struct btrfs_inode_extref *extref;
1320 char namebuf[BTRFS_NAME_LEN];
1322 inode_cache = &active_node->inode_cache;
1324 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1325 total = btrfs_item_size_nr(eb, slot);
1326 while (cur < total) {
1327 name_len = btrfs_inode_extref_name_len(eb, extref);
1328 index = btrfs_inode_extref_index(eb, extref);
1329 parent = btrfs_inode_extref_parent(eb, extref);
1330 if (name_len <= BTRFS_NAME_LEN) {
1334 len = BTRFS_NAME_LEN;
1335 error = REF_ERR_NAME_TOO_LONG;
1337 read_extent_buffer(eb, namebuf,
1338 (unsigned long)(extref + 1), len);
1339 add_inode_backref(inode_cache, key->objectid, parent,
1340 index, namebuf, len, 0, key->type, error);
1342 len = sizeof(*extref) + name_len;
1343 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1350 static int count_csum_range(struct btrfs_root *root, u64 start,
1351 u64 len, u64 *found)
1353 struct btrfs_key key;
1354 struct btrfs_path path;
1355 struct extent_buffer *leaf;
1360 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1362 btrfs_init_path(&path);
1364 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1366 key.type = BTRFS_EXTENT_CSUM_KEY;
1368 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1372 if (ret > 0 && path.slots[0] > 0) {
1373 leaf = path.nodes[0];
1374 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1375 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1376 key.type == BTRFS_EXTENT_CSUM_KEY)
1381 leaf = path.nodes[0];
1382 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1383 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1388 leaf = path.nodes[0];
1391 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1392 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1393 key.type != BTRFS_EXTENT_CSUM_KEY)
1396 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1397 if (key.offset >= start + len)
1400 if (key.offset > start)
1403 size = btrfs_item_size_nr(leaf, path.slots[0]);
1404 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1405 if (csum_end > start) {
1406 size = min(csum_end - start, len);
1415 btrfs_release_path(&path);
1421 static int process_file_extent(struct btrfs_root *root,
1422 struct extent_buffer *eb,
1423 int slot, struct btrfs_key *key,
1424 struct shared_node *active_node)
1426 struct inode_record *rec;
1427 struct btrfs_file_extent_item *fi;
1429 u64 disk_bytenr = 0;
1430 u64 extent_offset = 0;
1431 u64 mask = root->sectorsize - 1;
1435 rec = active_node->current;
1436 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1437 rec->found_file_extent = 1;
1439 if (rec->extent_start == (u64)-1) {
1440 rec->extent_start = key->offset;
1441 rec->extent_end = key->offset;
1444 if (rec->extent_end > key->offset)
1445 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1446 else if (rec->extent_end < key->offset) {
1447 ret = add_file_extent_hole(&rec->holes, rec->extent_end,
1448 key->offset - rec->extent_end);
1453 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1454 extent_type = btrfs_file_extent_type(eb, fi);
1456 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1457 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1459 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1460 rec->found_size += num_bytes;
1461 num_bytes = (num_bytes + mask) & ~mask;
1462 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1463 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1464 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1465 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1466 extent_offset = btrfs_file_extent_offset(eb, fi);
1467 if (num_bytes == 0 || (num_bytes & mask))
1468 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1469 if (num_bytes + extent_offset >
1470 btrfs_file_extent_ram_bytes(eb, fi))
1471 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1472 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1473 (btrfs_file_extent_compression(eb, fi) ||
1474 btrfs_file_extent_encryption(eb, fi) ||
1475 btrfs_file_extent_other_encoding(eb, fi)))
1476 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1477 if (disk_bytenr > 0)
1478 rec->found_size += num_bytes;
1480 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1482 rec->extent_end = key->offset + num_bytes;
1484 if (disk_bytenr > 0) {
1486 if (btrfs_file_extent_compression(eb, fi))
1487 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1489 disk_bytenr += extent_offset;
1491 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1494 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1496 rec->found_csum_item = 1;
1497 if (found < num_bytes)
1498 rec->some_csum_missing = 1;
1499 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1501 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1507 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1508 struct walk_control *wc)
1510 struct btrfs_key key;
1514 struct cache_tree *inode_cache;
1515 struct shared_node *active_node;
1517 if (wc->root_level == wc->active_node &&
1518 btrfs_root_refs(&root->root_item) == 0)
1521 active_node = wc->nodes[wc->active_node];
1522 inode_cache = &active_node->inode_cache;
1523 nritems = btrfs_header_nritems(eb);
1524 for (i = 0; i < nritems; i++) {
1525 btrfs_item_key_to_cpu(eb, &key, i);
1527 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1529 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1532 if (active_node->current == NULL ||
1533 active_node->current->ino < key.objectid) {
1534 if (active_node->current) {
1535 active_node->current->checked = 1;
1536 maybe_free_inode_rec(inode_cache,
1537 active_node->current);
1539 active_node->current = get_inode_rec(inode_cache,
1543 case BTRFS_DIR_ITEM_KEY:
1544 case BTRFS_DIR_INDEX_KEY:
1545 ret = process_dir_item(root, eb, i, &key, active_node);
1547 case BTRFS_INODE_REF_KEY:
1548 ret = process_inode_ref(eb, i, &key, active_node);
1550 case BTRFS_INODE_EXTREF_KEY:
1551 ret = process_inode_extref(eb, i, &key, active_node);
1553 case BTRFS_INODE_ITEM_KEY:
1554 ret = process_inode_item(eb, i, &key, active_node);
1556 case BTRFS_EXTENT_DATA_KEY:
1557 ret = process_file_extent(root, eb, i, &key,
1567 static void reada_walk_down(struct btrfs_root *root,
1568 struct extent_buffer *node, int slot)
1577 level = btrfs_header_level(node);
1581 nritems = btrfs_header_nritems(node);
1582 blocksize = btrfs_level_size(root, level - 1);
1583 for (i = slot; i < nritems; i++) {
1584 bytenr = btrfs_node_blockptr(node, i);
1585 ptr_gen = btrfs_node_ptr_generation(node, i);
1586 readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1591 * Check the child node/leaf by the following condition:
1592 * 1. the first item key of the node/leaf should be the same with the one
1594 * 2. block in parent node should match the child node/leaf.
1595 * 3. generation of parent node and child's header should be consistent.
1597 * Or the child node/leaf pointed by the key in parent is not valid.
1599 * We hope to check leaf owner too, but since subvol may share leaves,
1600 * which makes leaf owner check not so strong, key check should be
1601 * sufficient enough for that case.
1603 static int check_child_node(struct btrfs_root *root,
1604 struct extent_buffer *parent, int slot,
1605 struct extent_buffer *child)
1607 struct btrfs_key parent_key;
1608 struct btrfs_key child_key;
1611 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1612 if (btrfs_header_level(child) == 0)
1613 btrfs_item_key_to_cpu(child, &child_key, 0);
1615 btrfs_node_key_to_cpu(child, &child_key, 0);
1617 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
1620 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
1621 parent_key.objectid, parent_key.type, parent_key.offset,
1622 child_key.objectid, child_key.type, child_key.offset);
1624 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
1626 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
1627 btrfs_node_blockptr(parent, slot),
1628 btrfs_header_bytenr(child));
1630 if (btrfs_node_ptr_generation(parent, slot) !=
1631 btrfs_header_generation(child)) {
1633 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
1634 btrfs_header_generation(child),
1635 btrfs_node_ptr_generation(parent, slot));
1640 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1641 struct walk_control *wc, int *level)
1643 enum btrfs_tree_block_status status;
1646 struct extent_buffer *next;
1647 struct extent_buffer *cur;
1652 WARN_ON(*level < 0);
1653 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1654 ret = btrfs_lookup_extent_info(NULL, root,
1655 path->nodes[*level]->start,
1656 *level, 1, &refs, NULL);
1663 ret = enter_shared_node(root, path->nodes[*level]->start,
1671 while (*level >= 0) {
1672 WARN_ON(*level < 0);
1673 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1674 cur = path->nodes[*level];
1676 if (btrfs_header_level(cur) != *level)
1679 if (path->slots[*level] >= btrfs_header_nritems(cur))
1682 ret = process_one_leaf(root, cur, wc);
1687 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1688 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1689 blocksize = btrfs_level_size(root, *level - 1);
1690 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1696 ret = enter_shared_node(root, bytenr, refs,
1699 path->slots[*level]++;
1704 next = btrfs_find_tree_block(root, bytenr, blocksize);
1705 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1706 free_extent_buffer(next);
1707 reada_walk_down(root, cur, path->slots[*level]);
1708 next = read_tree_block(root, bytenr, blocksize,
1711 struct btrfs_key node_key;
1713 btrfs_node_key_to_cpu(path->nodes[*level],
1715 path->slots[*level]);
1716 btrfs_add_corrupt_extent_record(root->fs_info,
1718 path->nodes[*level]->start,
1719 root->leafsize, *level);
1725 ret = check_child_node(root, cur, path->slots[*level], next);
1731 if (btrfs_is_leaf(next))
1732 status = btrfs_check_leaf(root, NULL, next);
1734 status = btrfs_check_node(root, NULL, next);
1735 if (status != BTRFS_TREE_BLOCK_CLEAN) {
1736 free_extent_buffer(next);
1741 *level = *level - 1;
1742 free_extent_buffer(path->nodes[*level]);
1743 path->nodes[*level] = next;
1744 path->slots[*level] = 0;
1747 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1751 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1752 struct walk_control *wc, int *level)
1755 struct extent_buffer *leaf;
1757 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1758 leaf = path->nodes[i];
1759 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1764 free_extent_buffer(path->nodes[*level]);
1765 path->nodes[*level] = NULL;
1766 BUG_ON(*level > wc->active_node);
1767 if (*level == wc->active_node)
1768 leave_shared_node(root, wc, *level);
1775 static int check_root_dir(struct inode_record *rec)
1777 struct inode_backref *backref;
1780 if (!rec->found_inode_item || rec->errors)
1782 if (rec->nlink != 1 || rec->found_link != 0)
1784 if (list_empty(&rec->backrefs))
1786 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1787 if (!backref->found_inode_ref)
1789 if (backref->index != 0 || backref->namelen != 2 ||
1790 memcmp(backref->name, "..", 2))
1792 if (backref->found_dir_index || backref->found_dir_item)
1799 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1800 struct btrfs_root *root, struct btrfs_path *path,
1801 struct inode_record *rec)
1803 struct btrfs_inode_item *ei;
1804 struct btrfs_key key;
1807 key.objectid = rec->ino;
1808 key.type = BTRFS_INODE_ITEM_KEY;
1809 key.offset = (u64)-1;
1811 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1815 if (!path->slots[0]) {
1822 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1823 if (key.objectid != rec->ino) {
1828 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1829 struct btrfs_inode_item);
1830 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1831 btrfs_mark_buffer_dirty(path->nodes[0]);
1832 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1833 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1834 root->root_key.objectid);
1836 btrfs_release_path(path);
1840 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1841 struct btrfs_root *root,
1842 struct btrfs_path *path,
1843 struct inode_record *rec)
1847 ret = btrfs_add_orphan_item(trans, root, path, rec->ino);
1848 btrfs_release_path(path);
1850 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1854 static int add_missing_dir_index(struct btrfs_root *root,
1855 struct cache_tree *inode_cache,
1856 struct inode_record *rec,
1857 struct inode_backref *backref)
1859 struct btrfs_path *path;
1860 struct btrfs_trans_handle *trans;
1861 struct btrfs_dir_item *dir_item;
1862 struct extent_buffer *leaf;
1863 struct btrfs_key key;
1864 struct btrfs_disk_key disk_key;
1865 struct inode_record *dir_rec;
1866 unsigned long name_ptr;
1867 u32 data_size = sizeof(*dir_item) + backref->namelen;
1870 path = btrfs_alloc_path();
1874 trans = btrfs_start_transaction(root, 1);
1875 if (IS_ERR(trans)) {
1876 btrfs_free_path(path);
1877 return PTR_ERR(trans);
1880 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
1881 (unsigned long long)rec->ino);
1882 key.objectid = backref->dir;
1883 key.type = BTRFS_DIR_INDEX_KEY;
1884 key.offset = backref->index;
1886 ret = btrfs_insert_empty_item(trans, root, path, &key, data_size);
1889 leaf = path->nodes[0];
1890 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
1892 disk_key.objectid = cpu_to_le64(rec->ino);
1893 disk_key.type = BTRFS_INODE_ITEM_KEY;
1894 disk_key.offset = 0;
1896 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
1897 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
1898 btrfs_set_dir_data_len(leaf, dir_item, 0);
1899 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
1900 name_ptr = (unsigned long)(dir_item + 1);
1901 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
1902 btrfs_mark_buffer_dirty(leaf);
1903 btrfs_free_path(path);
1904 btrfs_commit_transaction(trans, root);
1906 backref->found_dir_index = 1;
1907 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
1910 dir_rec->found_size += backref->namelen;
1911 if (dir_rec->found_size == dir_rec->isize &&
1912 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
1913 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1914 if (dir_rec->found_size != dir_rec->isize)
1915 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
1920 static int delete_dir_index(struct btrfs_root *root,
1921 struct cache_tree *inode_cache,
1922 struct inode_record *rec,
1923 struct inode_backref *backref)
1925 struct btrfs_trans_handle *trans;
1926 struct btrfs_dir_item *di;
1927 struct btrfs_path *path;
1930 path = btrfs_alloc_path();
1934 trans = btrfs_start_transaction(root, 1);
1935 if (IS_ERR(trans)) {
1936 btrfs_free_path(path);
1937 return PTR_ERR(trans);
1941 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
1942 (unsigned long long)backref->dir,
1943 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
1944 (unsigned long long)root->objectid);
1946 di = btrfs_lookup_dir_index(trans, root, path, backref->dir,
1947 backref->name, backref->namelen,
1948 backref->index, -1);
1951 btrfs_free_path(path);
1952 btrfs_commit_transaction(trans, root);
1959 ret = btrfs_del_item(trans, root, path);
1961 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1963 btrfs_free_path(path);
1964 btrfs_commit_transaction(trans, root);
1968 static int create_inode_item(struct btrfs_root *root,
1969 struct inode_record *rec,
1970 struct inode_backref *backref, int root_dir)
1972 struct btrfs_trans_handle *trans;
1973 struct btrfs_inode_item inode_item;
1974 time_t now = time(NULL);
1977 trans = btrfs_start_transaction(root, 1);
1978 if (IS_ERR(trans)) {
1979 ret = PTR_ERR(trans);
1983 fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
1984 "be incomplete, please check permissions and content after "
1985 "the fsck completes.\n", (unsigned long long)root->objectid,
1986 (unsigned long long)rec->ino);
1988 memset(&inode_item, 0, sizeof(inode_item));
1989 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
1991 btrfs_set_stack_inode_nlink(&inode_item, 1);
1993 btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
1994 btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
1995 if (rec->found_dir_item) {
1996 if (rec->found_file_extent)
1997 fprintf(stderr, "root %llu inode %llu has both a dir "
1998 "item and extents, unsure if it is a dir or a "
1999 "regular file so setting it as a directory\n",
2000 (unsigned long long)root->objectid,
2001 (unsigned long long)rec->ino);
2002 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
2003 btrfs_set_stack_inode_size(&inode_item, rec->found_size);
2004 } else if (!rec->found_dir_item) {
2005 btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
2006 btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
2008 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
2009 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
2010 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
2011 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
2012 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
2013 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
2014 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
2015 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
2017 ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
2019 btrfs_commit_transaction(trans, root);
2023 static int repair_inode_backrefs(struct btrfs_root *root,
2024 struct inode_record *rec,
2025 struct cache_tree *inode_cache,
2028 struct inode_backref *tmp, *backref;
2029 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2033 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2034 if (!delete && rec->ino == root_dirid) {
2035 if (!rec->found_inode_item) {
2036 ret = create_inode_item(root, rec, backref, 1);
2043 /* Index 0 for root dir's are special, don't mess with it */
2044 if (rec->ino == root_dirid && backref->index == 0)
2048 ((backref->found_dir_index && !backref->found_inode_ref) ||
2049 (backref->found_dir_index && backref->found_inode_ref &&
2050 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
2051 ret = delete_dir_index(root, inode_cache, rec, backref);
2055 list_del(&backref->list);
2059 if (!delete && !backref->found_dir_index &&
2060 backref->found_dir_item && backref->found_inode_ref) {
2061 ret = add_missing_dir_index(root, inode_cache, rec,
2066 if (backref->found_dir_item &&
2067 backref->found_dir_index &&
2068 backref->found_dir_index) {
2069 if (!backref->errors &&
2070 backref->found_inode_ref) {
2071 list_del(&backref->list);
2077 if (!delete && (!backref->found_dir_index &&
2078 !backref->found_dir_item &&
2079 backref->found_inode_ref)) {
2080 struct btrfs_trans_handle *trans;
2081 struct btrfs_key location;
2083 ret = check_dir_conflict(root, backref->name,
2089 * let nlink fixing routine to handle it,
2090 * which can do it better.
2095 location.objectid = rec->ino;
2096 location.type = BTRFS_INODE_ITEM_KEY;
2097 location.offset = 0;
2099 trans = btrfs_start_transaction(root, 1);
2100 if (IS_ERR(trans)) {
2101 ret = PTR_ERR(trans);
2104 fprintf(stderr, "adding missing dir index/item pair "
2106 (unsigned long long)rec->ino);
2107 ret = btrfs_insert_dir_item(trans, root, backref->name,
2109 backref->dir, &location,
2110 imode_to_type(rec->imode),
2113 btrfs_commit_transaction(trans, root);
2117 if (!delete && (backref->found_inode_ref &&
2118 backref->found_dir_index &&
2119 backref->found_dir_item &&
2120 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
2121 !rec->found_inode_item)) {
2122 ret = create_inode_item(root, rec, backref, 0);
2129 return ret ? ret : repaired;
2133 * To determine the file type for nlink/inode_item repair
2135 * Return 0 if file type is found and BTRFS_FT_* is stored into type.
2136 * Return -ENOENT if file type is not found.
2138 static int find_file_type(struct inode_record *rec, u8 *type)
2140 struct inode_backref *backref;
2142 /* For inode item recovered case */
2143 if (rec->found_inode_item) {
2144 *type = imode_to_type(rec->imode);
2148 list_for_each_entry(backref, &rec->backrefs, list) {
2149 if (backref->found_dir_index || backref->found_dir_item) {
2150 *type = backref->filetype;
2158 * To determine the file name for nlink repair
2160 * Return 0 if file name is found, set name and namelen.
2161 * Return -ENOENT if file name is not found.
2163 static int find_file_name(struct inode_record *rec,
2164 char *name, int *namelen)
2166 struct inode_backref *backref;
2168 list_for_each_entry(backref, &rec->backrefs, list) {
2169 if (backref->found_dir_index || backref->found_dir_item ||
2170 backref->found_inode_ref) {
2171 memcpy(name, backref->name, backref->namelen);
2172 *namelen = backref->namelen;
2179 /* Reset the nlink of the inode to the correct one */
2180 static int reset_nlink(struct btrfs_trans_handle *trans,
2181 struct btrfs_root *root,
2182 struct btrfs_path *path,
2183 struct inode_record *rec)
2185 struct inode_backref *backref;
2186 struct inode_backref *tmp;
2187 struct btrfs_key key;
2188 struct btrfs_inode_item *inode_item;
2191 /* We don't believe this either, reset it and iterate backref */
2192 rec->found_link = 0;
2194 /* Remove all backref including the valid ones */
2195 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2196 ret = btrfs_unlink(trans, root, rec->ino, backref->dir,
2197 backref->index, backref->name,
2198 backref->namelen, 0);
2202 /* remove invalid backref, so it won't be added back */
2203 if (!(backref->found_dir_index &&
2204 backref->found_dir_item &&
2205 backref->found_inode_ref)) {
2206 list_del(&backref->list);
2213 /* Set nlink to 0 */
2214 key.objectid = rec->ino;
2215 key.type = BTRFS_INODE_ITEM_KEY;
2217 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2224 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2225 struct btrfs_inode_item);
2226 btrfs_set_inode_nlink(path->nodes[0], inode_item, 0);
2227 btrfs_mark_buffer_dirty(path->nodes[0]);
2228 btrfs_release_path(path);
2231 * Add back valid inode_ref/dir_item/dir_index,
2232 * add_link() will handle the nlink inc, so new nlink must be correct
2234 list_for_each_entry(backref, &rec->backrefs, list) {
2235 ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
2236 backref->name, backref->namelen,
2237 backref->ref_type, &backref->index, 1);
2242 btrfs_release_path(path);
2246 static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
2247 struct btrfs_root *root,
2248 struct btrfs_path *path,
2249 struct inode_record *rec)
2251 char *dir_name = "lost+found";
2252 char namebuf[BTRFS_NAME_LEN] = {0};
2257 int name_recovered = 0;
2258 int type_recovered = 0;
2262 * Get file name and type first before these invalid inode ref
2263 * are deleted by remove_all_invalid_backref()
2265 name_recovered = !find_file_name(rec, namebuf, &namelen);
2266 type_recovered = !find_file_type(rec, &type);
2268 if (!name_recovered) {
2269 printf("Can't get file name for inode %llu, using '%llu' as fallback\n",
2270 rec->ino, rec->ino);
2271 namelen = count_digits(rec->ino);
2272 sprintf(namebuf, "%llu", rec->ino);
2275 if (!type_recovered) {
2276 printf("Can't get file type for inode %llu, using FILE as fallback\n",
2278 type = BTRFS_FT_REG_FILE;
2282 ret = reset_nlink(trans, root, path, rec);
2285 "Failed to reset nlink for inode %llu: %s\n",
2286 rec->ino, strerror(-ret));
2290 if (rec->found_link == 0) {
2291 lost_found_ino = root->highest_inode;
2292 if (lost_found_ino >= BTRFS_LAST_FREE_OBJECTID) {
2297 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
2298 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
2301 fprintf(stderr, "Failed to create '%s' dir: %s",
2302 dir_name, strerror(-ret));
2305 ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
2306 namebuf, namelen, type, NULL, 1);
2307 if (ret == -EEXIST) {
2309 * Conflicting file name, add ".INO" as suffix * +1 for '.'
2311 if (namelen + count_digits(rec->ino) + 1 >
2316 snprintf(namebuf + namelen, BTRFS_NAME_LEN - namelen,
2318 namelen += count_digits(rec->ino) + 1;
2319 ret = btrfs_add_link(trans, root, rec->ino,
2320 lost_found_ino, namebuf,
2321 namelen, type, NULL, 1);
2325 "Failed to link the inode %llu to %s dir: %s",
2326 rec->ino, dir_name, strerror(-ret));
2330 * Just increase the found_link, don't actually add the
2331 * backref. This will make things easier and this inode
2332 * record will be freed after the repair is done.
2333 * So fsck will not report problem about this inode.
2336 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
2337 namelen, namebuf, dir_name);
2339 rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
2340 printf("Fixed the nlink of inode %llu\n", rec->ino);
2342 btrfs_release_path(path);
2347 * Check if there is any normal(reg or prealloc) file extent for given
2349 * This is used to determine the file type when neither its dir_index/item or
2350 * inode_item exists.
2352 * This will *NOT* report error, if any error happens, just consider it does
2353 * not have any normal file extent.
2355 static int find_normal_file_extent(struct btrfs_root *root, u64 ino)
2357 struct btrfs_path *path;
2358 struct btrfs_key key;
2359 struct btrfs_key found_key;
2360 struct btrfs_file_extent_item *fi;
2364 path = btrfs_alloc_path();
2368 key.type = BTRFS_EXTENT_DATA_KEY;
2371 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2376 if (ret && path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2377 ret = btrfs_next_leaf(root, path);
2384 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2386 if (found_key.objectid != ino ||
2387 found_key.type != BTRFS_EXTENT_DATA_KEY)
2389 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
2390 struct btrfs_file_extent_item);
2391 type = btrfs_file_extent_type(path->nodes[0], fi);
2392 if (type != BTRFS_FILE_EXTENT_INLINE) {
2398 btrfs_free_path(path);
2402 static u32 btrfs_type_to_imode(u8 type)
2404 static u32 imode_by_btrfs_type[] = {
2405 [BTRFS_FT_REG_FILE] = S_IFREG,
2406 [BTRFS_FT_DIR] = S_IFDIR,
2407 [BTRFS_FT_CHRDEV] = S_IFCHR,
2408 [BTRFS_FT_BLKDEV] = S_IFBLK,
2409 [BTRFS_FT_FIFO] = S_IFIFO,
2410 [BTRFS_FT_SOCK] = S_IFSOCK,
2411 [BTRFS_FT_SYMLINK] = S_IFLNK,
2414 return imode_by_btrfs_type[(type)];
2417 static int repair_inode_no_item(struct btrfs_trans_handle *trans,
2418 struct btrfs_root *root,
2419 struct btrfs_path *path,
2420 struct inode_record *rec)
2424 int type_recovered = 0;
2429 * 1. salvage data from existing file extent and
2430 * punch hole to keep fi ext consistent.
2431 * 2. salvage data from extent tree
2433 printf("Trying to rebuild inode:%llu\n", rec->ino);
2435 type_recovered = !find_file_type(rec, &filetype);
2438 * Try to determine inode type if type not found.
2440 * For found regular file extent, it must be FILE.
2441 * For found dir_item/index, it must be DIR.
2443 * For undetermined one, use FILE as fallback.
2446 * 1. If found extent belong to it in extent tree, it must be FILE
2447 * Need extra hook in extent tree scan.
2448 * 2. If found backref(inode_index/item is already handled) to it,
2450 * Need new inode-inode ref structure to allow search for that.
2452 if (!type_recovered) {
2453 if (rec->found_file_extent &&
2454 find_normal_file_extent(root, rec->ino)) {
2456 filetype = BTRFS_FT_REG_FILE;
2457 } else if (rec->found_dir_item) {
2459 filetype = BTRFS_FT_DIR;
2461 printf("Can't determint the filetype for inode %llu, assume it is a normal file\n",
2464 filetype = BTRFS_FT_REG_FILE;
2468 ret = btrfs_new_inode(trans, root, rec->ino,
2469 mode | btrfs_type_to_imode(filetype));
2474 * Here inode rebuild is done, we only rebuild the inode item,
2475 * don't repair the nlink(like move to lost+found).
2476 * That is the job of nlink repair.
2478 * We just fill the record and return
2480 rec->found_dir_item = 1;
2481 rec->imode = mode | btrfs_type_to_imode(filetype);
2483 rec->errors &= ~I_ERR_NO_INODE_ITEM;
2484 /* Ensure the inode_nlinks repair function will be called */
2485 rec->errors |= I_ERR_LINK_COUNT_WRONG;
2490 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
2492 struct btrfs_trans_handle *trans;
2493 struct btrfs_path *path;
2496 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |
2497 I_ERR_NO_ORPHAN_ITEM |
2498 I_ERR_LINK_COUNT_WRONG |
2499 I_ERR_NO_INODE_ITEM)))
2502 path = btrfs_alloc_path();
2507 * For nlink repair, it may create a dir and add link, so
2508 * 2 for parent(256)'s dir_index and dir_item
2509 * 2 for lost+found dir's inode_item and inode_ref
2510 * 1 for the new inode_ref of the file
2511 * 2 for lost+found dir's dir_index and dir_item for the file
2513 trans = btrfs_start_transaction(root, 7);
2514 if (IS_ERR(trans)) {
2515 btrfs_free_path(path);
2516 return PTR_ERR(trans);
2519 if (rec->errors & I_ERR_NO_INODE_ITEM)
2520 ret = repair_inode_no_item(trans, root, path, rec);
2521 if (!ret && rec->errors & I_ERR_DIR_ISIZE_WRONG)
2522 ret = repair_inode_isize(trans, root, path, rec);
2523 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
2524 ret = repair_inode_orphan_item(trans, root, path, rec);
2525 if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
2526 ret = repair_inode_nlinks(trans, root, path, rec);
2527 btrfs_commit_transaction(trans, root);
2528 btrfs_free_path(path);
2532 static int check_inode_recs(struct btrfs_root *root,
2533 struct cache_tree *inode_cache)
2535 struct cache_extent *cache;
2536 struct ptr_node *node;
2537 struct inode_record *rec;
2538 struct inode_backref *backref;
2543 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2545 if (btrfs_root_refs(&root->root_item) == 0) {
2546 if (!cache_tree_empty(inode_cache))
2547 fprintf(stderr, "warning line %d\n", __LINE__);
2552 * We need to record the highest inode number for later 'lost+found'
2554 * We must select a ino not used/refered by any existing inode, or
2555 * 'lost+found' ino may be a missing ino in a corrupted leaf,
2556 * this may cause 'lost+found' dir has wrong nlinks.
2558 cache = last_cache_extent(inode_cache);
2560 node = container_of(cache, struct ptr_node, cache);
2562 if (rec->ino > root->highest_inode)
2563 root->highest_inode = rec->ino;
2567 * We need to repair backrefs first because we could change some of the
2568 * errors in the inode recs.
2570 * We also need to go through and delete invalid backrefs first and then
2571 * add the correct ones second. We do this because we may get EEXIST
2572 * when adding back the correct index because we hadn't yet deleted the
2575 * For example, if we were missing a dir index then the directories
2576 * isize would be wrong, so if we fixed the isize to what we thought it
2577 * would be and then fixed the backref we'd still have a invalid fs, so
2578 * we need to add back the dir index and then check to see if the isize
2583 if (stage == 3 && !err)
2586 cache = search_cache_extent(inode_cache, 0);
2587 while (repair && cache) {
2588 node = container_of(cache, struct ptr_node, cache);
2590 cache = next_cache_extent(cache);
2592 /* Need to free everything up and rescan */
2594 remove_cache_extent(inode_cache, &node->cache);
2596 free_inode_rec(rec);
2600 if (list_empty(&rec->backrefs))
2603 ret = repair_inode_backrefs(root, rec, inode_cache,
2617 rec = get_inode_rec(inode_cache, root_dirid, 0);
2619 ret = check_root_dir(rec);
2621 fprintf(stderr, "root %llu root dir %llu error\n",
2622 (unsigned long long)root->root_key.objectid,
2623 (unsigned long long)root_dirid);
2624 print_inode_error(root, rec);
2629 struct btrfs_trans_handle *trans;
2631 trans = btrfs_start_transaction(root, 1);
2632 if (IS_ERR(trans)) {
2633 err = PTR_ERR(trans);
2638 "root %llu missing its root dir, recreating\n",
2639 (unsigned long long)root->objectid);
2641 ret = btrfs_make_root_dir(trans, root, root_dirid);
2644 btrfs_commit_transaction(trans, root);
2648 fprintf(stderr, "root %llu root dir %llu not found\n",
2649 (unsigned long long)root->root_key.objectid,
2650 (unsigned long long)root_dirid);
2654 cache = search_cache_extent(inode_cache, 0);
2657 node = container_of(cache, struct ptr_node, cache);
2659 remove_cache_extent(inode_cache, &node->cache);
2661 if (rec->ino == root_dirid ||
2662 rec->ino == BTRFS_ORPHAN_OBJECTID) {
2663 free_inode_rec(rec);
2667 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
2668 ret = check_orphan_item(root, rec->ino);
2670 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2671 if (can_free_inode_rec(rec)) {
2672 free_inode_rec(rec);
2677 if (!rec->found_inode_item)
2678 rec->errors |= I_ERR_NO_INODE_ITEM;
2679 if (rec->found_link != rec->nlink)
2680 rec->errors |= I_ERR_LINK_COUNT_WRONG;
2682 ret = try_repair_inode(root, rec);
2683 if (ret == 0 && can_free_inode_rec(rec)) {
2684 free_inode_rec(rec);
2690 if (!(repair && ret == 0))
2692 print_inode_error(root, rec);
2693 list_for_each_entry(backref, &rec->backrefs, list) {
2694 if (!backref->found_dir_item)
2695 backref->errors |= REF_ERR_NO_DIR_ITEM;
2696 if (!backref->found_dir_index)
2697 backref->errors |= REF_ERR_NO_DIR_INDEX;
2698 if (!backref->found_inode_ref)
2699 backref->errors |= REF_ERR_NO_INODE_REF;
2700 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
2701 " namelen %u name %s filetype %d errors %x",
2702 (unsigned long long)backref->dir,
2703 (unsigned long long)backref->index,
2704 backref->namelen, backref->name,
2705 backref->filetype, backref->errors);
2706 print_ref_error(backref->errors);
2708 free_inode_rec(rec);
2710 return (error > 0) ? -1 : 0;
2713 static struct root_record *get_root_rec(struct cache_tree *root_cache,
2716 struct cache_extent *cache;
2717 struct root_record *rec = NULL;
2720 cache = lookup_cache_extent(root_cache, objectid, 1);
2722 rec = container_of(cache, struct root_record, cache);
2724 rec = calloc(1, sizeof(*rec));
2725 rec->objectid = objectid;
2726 INIT_LIST_HEAD(&rec->backrefs);
2727 rec->cache.start = objectid;
2728 rec->cache.size = 1;
2730 ret = insert_cache_extent(root_cache, &rec->cache);
2736 static struct root_backref *get_root_backref(struct root_record *rec,
2737 u64 ref_root, u64 dir, u64 index,
2738 const char *name, int namelen)
2740 struct root_backref *backref;
2742 list_for_each_entry(backref, &rec->backrefs, list) {
2743 if (backref->ref_root != ref_root || backref->dir != dir ||
2744 backref->namelen != namelen)
2746 if (memcmp(name, backref->name, namelen))
2751 backref = malloc(sizeof(*backref) + namelen + 1);
2752 memset(backref, 0, sizeof(*backref));
2753 backref->ref_root = ref_root;
2755 backref->index = index;
2756 backref->namelen = namelen;
2757 memcpy(backref->name, name, namelen);
2758 backref->name[namelen] = '\0';
2759 list_add_tail(&backref->list, &rec->backrefs);
2763 static void free_root_record(struct cache_extent *cache)
2765 struct root_record *rec;
2766 struct root_backref *backref;
2768 rec = container_of(cache, struct root_record, cache);
2769 while (!list_empty(&rec->backrefs)) {
2770 backref = list_entry(rec->backrefs.next,
2771 struct root_backref, list);
2772 list_del(&backref->list);
2779 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
2781 static int add_root_backref(struct cache_tree *root_cache,
2782 u64 root_id, u64 ref_root, u64 dir, u64 index,
2783 const char *name, int namelen,
2784 int item_type, int errors)
2786 struct root_record *rec;
2787 struct root_backref *backref;
2789 rec = get_root_rec(root_cache, root_id);
2790 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
2792 backref->errors |= errors;
2794 if (item_type != BTRFS_DIR_ITEM_KEY) {
2795 if (backref->found_dir_index || backref->found_back_ref ||
2796 backref->found_forward_ref) {
2797 if (backref->index != index)
2798 backref->errors |= REF_ERR_INDEX_UNMATCH;
2800 backref->index = index;
2804 if (item_type == BTRFS_DIR_ITEM_KEY) {
2805 if (backref->found_forward_ref)
2807 backref->found_dir_item = 1;
2808 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
2809 backref->found_dir_index = 1;
2810 } else if (item_type == BTRFS_ROOT_REF_KEY) {
2811 if (backref->found_forward_ref)
2812 backref->errors |= REF_ERR_DUP_ROOT_REF;
2813 else if (backref->found_dir_item)
2815 backref->found_forward_ref = 1;
2816 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
2817 if (backref->found_back_ref)
2818 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
2819 backref->found_back_ref = 1;
2824 if (backref->found_forward_ref && backref->found_dir_item)
2825 backref->reachable = 1;
2829 static int merge_root_recs(struct btrfs_root *root,
2830 struct cache_tree *src_cache,
2831 struct cache_tree *dst_cache)
2833 struct cache_extent *cache;
2834 struct ptr_node *node;
2835 struct inode_record *rec;
2836 struct inode_backref *backref;
2839 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2840 free_inode_recs_tree(src_cache);
2845 cache = search_cache_extent(src_cache, 0);
2848 node = container_of(cache, struct ptr_node, cache);
2850 remove_cache_extent(src_cache, &node->cache);
2853 ret = is_child_root(root, root->objectid, rec->ino);
2859 list_for_each_entry(backref, &rec->backrefs, list) {
2860 BUG_ON(backref->found_inode_ref);
2861 if (backref->found_dir_item)
2862 add_root_backref(dst_cache, rec->ino,
2863 root->root_key.objectid, backref->dir,
2864 backref->index, backref->name,
2865 backref->namelen, BTRFS_DIR_ITEM_KEY,
2867 if (backref->found_dir_index)
2868 add_root_backref(dst_cache, rec->ino,
2869 root->root_key.objectid, backref->dir,
2870 backref->index, backref->name,
2871 backref->namelen, BTRFS_DIR_INDEX_KEY,
2875 free_inode_rec(rec);
2882 static int check_root_refs(struct btrfs_root *root,
2883 struct cache_tree *root_cache)
2885 struct root_record *rec;
2886 struct root_record *ref_root;
2887 struct root_backref *backref;
2888 struct cache_extent *cache;
2894 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
2897 /* fixme: this can not detect circular references */
2900 cache = search_cache_extent(root_cache, 0);
2904 rec = container_of(cache, struct root_record, cache);
2905 cache = next_cache_extent(cache);
2907 if (rec->found_ref == 0)
2910 list_for_each_entry(backref, &rec->backrefs, list) {
2911 if (!backref->reachable)
2914 ref_root = get_root_rec(root_cache,
2916 if (ref_root->found_ref > 0)
2919 backref->reachable = 0;
2921 if (rec->found_ref == 0)
2927 cache = search_cache_extent(root_cache, 0);
2931 rec = container_of(cache, struct root_record, cache);
2932 cache = next_cache_extent(cache);
2934 if (rec->found_ref == 0 &&
2935 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
2936 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
2937 ret = check_orphan_item(root->fs_info->tree_root,
2943 * If we don't have a root item then we likely just have
2944 * a dir item in a snapshot for this root but no actual
2945 * ref key or anything so it's meaningless.
2947 if (!rec->found_root_item)
2950 fprintf(stderr, "fs tree %llu not referenced\n",
2951 (unsigned long long)rec->objectid);
2955 if (rec->found_ref > 0 && !rec->found_root_item)
2957 list_for_each_entry(backref, &rec->backrefs, list) {
2958 if (!backref->found_dir_item)
2959 backref->errors |= REF_ERR_NO_DIR_ITEM;
2960 if (!backref->found_dir_index)
2961 backref->errors |= REF_ERR_NO_DIR_INDEX;
2962 if (!backref->found_back_ref)
2963 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
2964 if (!backref->found_forward_ref)
2965 backref->errors |= REF_ERR_NO_ROOT_REF;
2966 if (backref->reachable && backref->errors)
2973 fprintf(stderr, "fs tree %llu refs %u %s\n",
2974 (unsigned long long)rec->objectid, rec->found_ref,
2975 rec->found_root_item ? "" : "not found");
2977 list_for_each_entry(backref, &rec->backrefs, list) {
2978 if (!backref->reachable)
2980 if (!backref->errors && rec->found_root_item)
2982 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
2983 " index %llu namelen %u name %s errors %x\n",
2984 (unsigned long long)backref->ref_root,
2985 (unsigned long long)backref->dir,
2986 (unsigned long long)backref->index,
2987 backref->namelen, backref->name,
2989 print_ref_error(backref->errors);
2992 return errors > 0 ? 1 : 0;
2995 static int process_root_ref(struct extent_buffer *eb, int slot,
2996 struct btrfs_key *key,
2997 struct cache_tree *root_cache)
3003 struct btrfs_root_ref *ref;
3004 char namebuf[BTRFS_NAME_LEN];
3007 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
3009 dirid = btrfs_root_ref_dirid(eb, ref);
3010 index = btrfs_root_ref_sequence(eb, ref);
3011 name_len = btrfs_root_ref_name_len(eb, ref);
3013 if (name_len <= BTRFS_NAME_LEN) {
3017 len = BTRFS_NAME_LEN;
3018 error = REF_ERR_NAME_TOO_LONG;
3020 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
3022 if (key->type == BTRFS_ROOT_REF_KEY) {
3023 add_root_backref(root_cache, key->offset, key->objectid, dirid,
3024 index, namebuf, len, key->type, error);
3026 add_root_backref(root_cache, key->objectid, key->offset, dirid,
3027 index, namebuf, len, key->type, error);
3032 static void free_corrupt_block(struct cache_extent *cache)
3034 struct btrfs_corrupt_block *corrupt;
3036 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3040 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
3043 * Repair the btree of the given root.
3045 * The fix is to remove the node key in corrupt_blocks cache_tree.
3046 * and rebalance the tree.
3047 * After the fix, the btree should be writeable.
3049 static int repair_btree(struct btrfs_root *root,
3050 struct cache_tree *corrupt_blocks)
3052 struct btrfs_trans_handle *trans;
3053 struct btrfs_path *path;
3054 struct btrfs_corrupt_block *corrupt;
3055 struct cache_extent *cache;
3056 struct btrfs_key key;
3061 if (cache_tree_empty(corrupt_blocks))
3064 path = btrfs_alloc_path();
3068 trans = btrfs_start_transaction(root, 1);
3069 if (IS_ERR(trans)) {
3070 ret = PTR_ERR(trans);
3071 fprintf(stderr, "Error starting transaction: %s\n",
3075 cache = first_cache_extent(corrupt_blocks);
3077 corrupt = container_of(cache, struct btrfs_corrupt_block,
3079 level = corrupt->level;
3080 path->lowest_level = level;
3081 key.objectid = corrupt->key.objectid;
3082 key.type = corrupt->key.type;
3083 key.offset = corrupt->key.offset;
3086 * Here we don't want to do any tree balance, since it may
3087 * cause a balance with corrupted brother leaf/node,
3088 * so ins_len set to 0 here.
3089 * Balance will be done after all corrupt node/leaf is deleted.
3091 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3094 offset = btrfs_node_blockptr(path->nodes[level],
3095 path->slots[level]);
3097 /* Remove the ptr */
3098 ret = btrfs_del_ptr(trans, root, path, level,
3099 path->slots[level]);
3103 * Remove the corresponding extent
3104 * return value is not concerned.
3106 btrfs_release_path(path);
3107 ret = btrfs_free_extent(trans, root, offset, root->nodesize,
3108 0, root->root_key.objectid,
3110 cache = next_cache_extent(cache);
3113 /* Balance the btree using btrfs_search_slot() */
3114 cache = first_cache_extent(corrupt_blocks);
3116 corrupt = container_of(cache, struct btrfs_corrupt_block,
3118 memcpy(&key, &corrupt->key, sizeof(key));
3119 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3122 /* return will always >0 since it won't find the item */
3124 btrfs_release_path(path);
3125 cache = next_cache_extent(cache);
3128 btrfs_commit_transaction(trans, root);
3130 btrfs_free_path(path);
3134 static void print_orphan_data_extents(struct list_head *orphan_extents,
3137 struct orphan_data_extent *orphan;
3139 if (list_empty(orphan_extents))
3141 printf("The following data extent is lost in tree %llu:\n",
3143 list_for_each_entry(orphan, orphan_extents, list) {
3144 printf("\tinode: %llu, offset:%llu, disk_bytenr: %llu, disk_len: %llu\n",
3145 orphan->objectid, orphan->offset, orphan->disk_bytenr,
3150 static void free_orphan_data_extents(struct list_head *orphan_extents)
3152 struct orphan_data_extent *orphan;
3154 while (!list_empty(orphan_extents)) {
3155 orphan = list_entry(orphan_extents->next,
3156 struct orphan_data_extent, list);
3157 list_del(&orphan->list);
3162 static int check_fs_root(struct btrfs_root *root,
3163 struct cache_tree *root_cache,
3164 struct walk_control *wc)
3170 struct btrfs_path path;
3171 struct shared_node root_node;
3172 struct root_record *rec;
3173 struct btrfs_root_item *root_item = &root->root_item;
3174 struct cache_tree corrupt_blocks;
3175 enum btrfs_tree_block_status status;
3178 * Reuse the corrupt_block cache tree to record corrupted tree block
3180 * Unlike the usage in extent tree check, here we do it in a per
3181 * fs/subvol tree base.
3183 cache_tree_init(&corrupt_blocks);
3184 root->fs_info->corrupt_blocks = &corrupt_blocks;
3185 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
3186 rec = get_root_rec(root_cache, root->root_key.objectid);
3187 if (btrfs_root_refs(root_item) > 0)
3188 rec->found_root_item = 1;
3191 btrfs_init_path(&path);
3192 memset(&root_node, 0, sizeof(root_node));
3193 cache_tree_init(&root_node.root_cache);
3194 cache_tree_init(&root_node.inode_cache);
3196 level = btrfs_header_level(root->node);
3197 memset(wc->nodes, 0, sizeof(wc->nodes));
3198 wc->nodes[level] = &root_node;
3199 wc->active_node = level;
3200 wc->root_level = level;
3202 /* We may not have checked the root block, lets do that now */
3203 if (btrfs_is_leaf(root->node))
3204 status = btrfs_check_leaf(root, NULL, root->node);
3206 status = btrfs_check_node(root, NULL, root->node);
3207 if (status != BTRFS_TREE_BLOCK_CLEAN)
3210 if (btrfs_root_refs(root_item) > 0 ||
3211 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3212 path.nodes[level] = root->node;
3213 extent_buffer_get(root->node);
3214 path.slots[level] = 0;
3216 struct btrfs_key key;
3217 struct btrfs_disk_key found_key;
3219 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3220 level = root_item->drop_level;
3221 path.lowest_level = level;
3222 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3225 btrfs_node_key(path.nodes[level], &found_key,
3227 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3228 sizeof(found_key)));
3232 wret = walk_down_tree(root, &path, wc, &level);
3238 wret = walk_up_tree(root, &path, wc, &level);
3245 btrfs_release_path(&path);
3247 if (!cache_tree_empty(&corrupt_blocks)) {
3248 struct cache_extent *cache;
3249 struct btrfs_corrupt_block *corrupt;
3251 printf("The following tree block(s) is corrupted in tree %llu:\n",
3252 root->root_key.objectid);
3253 cache = first_cache_extent(&corrupt_blocks);
3255 corrupt = container_of(cache,
3256 struct btrfs_corrupt_block,
3258 printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n",
3259 cache->start, corrupt->level,
3260 corrupt->key.objectid, corrupt->key.type,
3261 corrupt->key.offset);
3262 cache = next_cache_extent(cache);
3265 printf("Try to repair the btree for root %llu\n",
3266 root->root_key.objectid);
3267 ret = repair_btree(root, &corrupt_blocks);
3269 fprintf(stderr, "Failed to repair btree: %s\n",
3272 printf("Btree for root %llu is fixed\n",
3273 root->root_key.objectid);
3277 err = merge_root_recs(root, &root_node.root_cache, root_cache);
3281 if (root_node.current) {
3282 root_node.current->checked = 1;
3283 maybe_free_inode_rec(&root_node.inode_cache,
3287 err = check_inode_recs(root, &root_node.inode_cache);
3291 free_corrupt_blocks_tree(&corrupt_blocks);
3292 root->fs_info->corrupt_blocks = NULL;
3293 print_orphan_data_extents(&root->orphan_data_extents, root->objectid);
3294 free_orphan_data_extents(&root->orphan_data_extents);
3298 static int fs_root_objectid(u64 objectid)
3300 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
3301 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3303 return is_fstree(objectid);
3306 static int check_fs_roots(struct btrfs_root *root,
3307 struct cache_tree *root_cache)
3309 struct btrfs_path path;
3310 struct btrfs_key key;
3311 struct walk_control wc;
3312 struct extent_buffer *leaf, *tree_node;
3313 struct btrfs_root *tmp_root;
3314 struct btrfs_root *tree_root = root->fs_info->tree_root;
3319 * Just in case we made any changes to the extent tree that weren't
3320 * reflected into the free space cache yet.
3323 reset_cached_block_groups(root->fs_info);
3324 memset(&wc, 0, sizeof(wc));
3325 cache_tree_init(&wc.shared);
3326 btrfs_init_path(&path);
3331 key.type = BTRFS_ROOT_ITEM_KEY;
3332 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
3337 tree_node = tree_root->node;
3339 if (tree_node != tree_root->node) {
3340 free_root_recs_tree(root_cache);
3341 btrfs_release_path(&path);
3344 leaf = path.nodes[0];
3345 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
3346 ret = btrfs_next_leaf(tree_root, &path);
3352 leaf = path.nodes[0];
3354 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
3355 if (key.type == BTRFS_ROOT_ITEM_KEY &&
3356 fs_root_objectid(key.objectid)) {
3357 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
3358 tmp_root = btrfs_read_fs_root_no_cache(
3359 root->fs_info, &key);
3361 key.offset = (u64)-1;
3362 tmp_root = btrfs_read_fs_root(
3363 root->fs_info, &key);
3365 if (IS_ERR(tmp_root)) {
3369 ret = check_fs_root(tmp_root, root_cache, &wc);
3370 if (ret == -EAGAIN) {
3371 free_root_recs_tree(root_cache);
3372 btrfs_release_path(&path);
3377 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
3378 btrfs_free_fs_root(tmp_root);
3379 } else if (key.type == BTRFS_ROOT_REF_KEY ||
3380 key.type == BTRFS_ROOT_BACKREF_KEY) {
3381 process_root_ref(leaf, path.slots[0], &key,
3388 btrfs_release_path(&path);
3390 free_extent_cache_tree(&wc.shared);
3391 if (!cache_tree_empty(&wc.shared))
3392 fprintf(stderr, "warning line %d\n", __LINE__);
3397 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
3399 struct list_head *cur = rec->backrefs.next;
3400 struct extent_backref *back;
3401 struct tree_backref *tback;
3402 struct data_backref *dback;
3406 while(cur != &rec->backrefs) {
3407 back = list_entry(cur, struct extent_backref, list);
3409 if (!back->found_extent_tree) {
3413 if (back->is_data) {
3414 dback = (struct data_backref *)back;
3415 fprintf(stderr, "Backref %llu %s %llu"
3416 " owner %llu offset %llu num_refs %lu"
3417 " not found in extent tree\n",
3418 (unsigned long long)rec->start,
3419 back->full_backref ?
3421 back->full_backref ?
3422 (unsigned long long)dback->parent:
3423 (unsigned long long)dback->root,
3424 (unsigned long long)dback->owner,
3425 (unsigned long long)dback->offset,
3426 (unsigned long)dback->num_refs);
3428 tback = (struct tree_backref *)back;
3429 fprintf(stderr, "Backref %llu parent %llu"
3430 " root %llu not found in extent tree\n",
3431 (unsigned long long)rec->start,
3432 (unsigned long long)tback->parent,
3433 (unsigned long long)tback->root);
3436 if (!back->is_data && !back->found_ref) {
3440 tback = (struct tree_backref *)back;
3441 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
3442 (unsigned long long)rec->start,
3443 back->full_backref ? "parent" : "root",
3444 back->full_backref ?
3445 (unsigned long long)tback->parent :
3446 (unsigned long long)tback->root, back);
3448 if (back->is_data) {
3449 dback = (struct data_backref *)back;
3450 if (dback->found_ref != dback->num_refs) {
3454 fprintf(stderr, "Incorrect local backref count"
3455 " on %llu %s %llu owner %llu"
3456 " offset %llu found %u wanted %u back %p\n",
3457 (unsigned long long)rec->start,
3458 back->full_backref ?
3460 back->full_backref ?
3461 (unsigned long long)dback->parent:
3462 (unsigned long long)dback->root,
3463 (unsigned long long)dback->owner,
3464 (unsigned long long)dback->offset,
3465 dback->found_ref, dback->num_refs, back);
3467 if (dback->disk_bytenr != rec->start) {
3471 fprintf(stderr, "Backref disk bytenr does not"
3472 " match extent record, bytenr=%llu, "
3473 "ref bytenr=%llu\n",
3474 (unsigned long long)rec->start,
3475 (unsigned long long)dback->disk_bytenr);
3478 if (dback->bytes != rec->nr) {
3482 fprintf(stderr, "Backref bytes do not match "
3483 "extent backref, bytenr=%llu, ref "
3484 "bytes=%llu, backref bytes=%llu\n",
3485 (unsigned long long)rec->start,
3486 (unsigned long long)rec->nr,
3487 (unsigned long long)dback->bytes);
3490 if (!back->is_data) {
3493 dback = (struct data_backref *)back;
3494 found += dback->found_ref;
3497 if (found != rec->refs) {
3501 fprintf(stderr, "Incorrect global backref count "
3502 "on %llu found %llu wanted %llu\n",
3503 (unsigned long long)rec->start,
3504 (unsigned long long)found,
3505 (unsigned long long)rec->refs);
3511 static int free_all_extent_backrefs(struct extent_record *rec)
3513 struct extent_backref *back;
3514 struct list_head *cur;
3515 while (!list_empty(&rec->backrefs)) {
3516 cur = rec->backrefs.next;
3517 back = list_entry(cur, struct extent_backref, list);
3524 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
3525 struct cache_tree *extent_cache)
3527 struct cache_extent *cache;
3528 struct extent_record *rec;
3531 cache = first_cache_extent(extent_cache);
3534 rec = container_of(cache, struct extent_record, cache);
3535 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
3536 remove_cache_extent(extent_cache, cache);
3537 free_all_extent_backrefs(rec);
3542 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
3543 struct extent_record *rec)
3545 if (rec->content_checked && rec->owner_ref_checked &&
3546 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
3547 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
3548 remove_cache_extent(extent_cache, &rec->cache);
3549 free_all_extent_backrefs(rec);
3550 list_del_init(&rec->list);
3556 static int check_owner_ref(struct btrfs_root *root,
3557 struct extent_record *rec,
3558 struct extent_buffer *buf)
3560 struct extent_backref *node;
3561 struct tree_backref *back;
3562 struct btrfs_root *ref_root;
3563 struct btrfs_key key;
3564 struct btrfs_path path;
3565 struct extent_buffer *parent;
3570 list_for_each_entry(node, &rec->backrefs, list) {
3573 if (!node->found_ref)
3575 if (node->full_backref)
3577 back = (struct tree_backref *)node;
3578 if (btrfs_header_owner(buf) == back->root)
3581 BUG_ON(rec->is_root);
3583 /* try to find the block by search corresponding fs tree */
3584 key.objectid = btrfs_header_owner(buf);
3585 key.type = BTRFS_ROOT_ITEM_KEY;
3586 key.offset = (u64)-1;
3588 ref_root = btrfs_read_fs_root(root->fs_info, &key);
3589 if (IS_ERR(ref_root))
3592 level = btrfs_header_level(buf);
3594 btrfs_item_key_to_cpu(buf, &key, 0);
3596 btrfs_node_key_to_cpu(buf, &key, 0);
3598 btrfs_init_path(&path);
3599 path.lowest_level = level + 1;
3600 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
3604 parent = path.nodes[level + 1];
3605 if (parent && buf->start == btrfs_node_blockptr(parent,
3606 path.slots[level + 1]))
3609 btrfs_release_path(&path);
3610 return found ? 0 : 1;
3613 static int is_extent_tree_record(struct extent_record *rec)
3615 struct list_head *cur = rec->backrefs.next;
3616 struct extent_backref *node;
3617 struct tree_backref *back;
3620 while(cur != &rec->backrefs) {
3621 node = list_entry(cur, struct extent_backref, list);
3625 back = (struct tree_backref *)node;
3626 if (node->full_backref)
3628 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
3635 static int record_bad_block_io(struct btrfs_fs_info *info,
3636 struct cache_tree *extent_cache,
3639 struct extent_record *rec;
3640 struct cache_extent *cache;
3641 struct btrfs_key key;
3643 cache = lookup_cache_extent(extent_cache, start, len);
3647 rec = container_of(cache, struct extent_record, cache);
3648 if (!is_extent_tree_record(rec))
3651 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
3652 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
3655 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
3656 struct extent_buffer *buf, int slot)
3658 if (btrfs_header_level(buf)) {
3659 struct btrfs_key_ptr ptr1, ptr2;
3661 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
3662 sizeof(struct btrfs_key_ptr));
3663 read_extent_buffer(buf, &ptr2,
3664 btrfs_node_key_ptr_offset(slot + 1),
3665 sizeof(struct btrfs_key_ptr));
3666 write_extent_buffer(buf, &ptr1,
3667 btrfs_node_key_ptr_offset(slot + 1),
3668 sizeof(struct btrfs_key_ptr));
3669 write_extent_buffer(buf, &ptr2,
3670 btrfs_node_key_ptr_offset(slot),
3671 sizeof(struct btrfs_key_ptr));
3673 struct btrfs_disk_key key;
3674 btrfs_node_key(buf, &key, 0);
3675 btrfs_fixup_low_keys(root, path, &key,
3676 btrfs_header_level(buf) + 1);
3679 struct btrfs_item *item1, *item2;
3680 struct btrfs_key k1, k2;
3681 char *item1_data, *item2_data;
3682 u32 item1_offset, item2_offset, item1_size, item2_size;
3684 item1 = btrfs_item_nr(slot);
3685 item2 = btrfs_item_nr(slot + 1);
3686 btrfs_item_key_to_cpu(buf, &k1, slot);
3687 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
3688 item1_offset = btrfs_item_offset(buf, item1);
3689 item2_offset = btrfs_item_offset(buf, item2);
3690 item1_size = btrfs_item_size(buf, item1);
3691 item2_size = btrfs_item_size(buf, item2);
3693 item1_data = malloc(item1_size);
3696 item2_data = malloc(item2_size);
3702 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
3703 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
3705 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
3706 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
3710 btrfs_set_item_offset(buf, item1, item2_offset);
3711 btrfs_set_item_offset(buf, item2, item1_offset);
3712 btrfs_set_item_size(buf, item1, item2_size);
3713 btrfs_set_item_size(buf, item2, item1_size);
3715 path->slots[0] = slot;
3716 btrfs_set_item_key_unsafe(root, path, &k2);
3717 path->slots[0] = slot + 1;
3718 btrfs_set_item_key_unsafe(root, path, &k1);
3723 static int fix_key_order(struct btrfs_trans_handle *trans,
3724 struct btrfs_root *root,
3725 struct btrfs_path *path)
3727 struct extent_buffer *buf;
3728 struct btrfs_key k1, k2;
3730 int level = path->lowest_level;
3733 buf = path->nodes[level];
3734 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
3736 btrfs_node_key_to_cpu(buf, &k1, i);
3737 btrfs_node_key_to_cpu(buf, &k2, i + 1);
3739 btrfs_item_key_to_cpu(buf, &k1, i);
3740 btrfs_item_key_to_cpu(buf, &k2, i + 1);
3742 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
3744 ret = swap_values(root, path, buf, i);
3747 btrfs_mark_buffer_dirty(buf);
3753 static int delete_bogus_item(struct btrfs_trans_handle *trans,
3754 struct btrfs_root *root,
3755 struct btrfs_path *path,
3756 struct extent_buffer *buf, int slot)
3758 struct btrfs_key key;
3759 int nritems = btrfs_header_nritems(buf);
3761 btrfs_item_key_to_cpu(buf, &key, slot);
3763 /* These are all the keys we can deal with missing. */
3764 if (key.type != BTRFS_DIR_INDEX_KEY &&
3765 key.type != BTRFS_EXTENT_ITEM_KEY &&
3766 key.type != BTRFS_METADATA_ITEM_KEY &&
3767 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3768 key.type != BTRFS_EXTENT_DATA_REF_KEY)
3771 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
3772 (unsigned long long)key.objectid, key.type,
3773 (unsigned long long)key.offset, slot, buf->start);
3774 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
3775 btrfs_item_nr_offset(slot + 1),
3776 sizeof(struct btrfs_item) *
3777 (nritems - slot - 1));
3778 btrfs_set_header_nritems(buf, nritems - 1);
3780 struct btrfs_disk_key disk_key;
3782 btrfs_item_key(buf, &disk_key, 0);
3783 btrfs_fixup_low_keys(root, path, &disk_key, 1);
3785 btrfs_mark_buffer_dirty(buf);
3789 static int fix_item_offset(struct btrfs_trans_handle *trans,
3790 struct btrfs_root *root,
3791 struct btrfs_path *path)
3793 struct extent_buffer *buf;
3797 /* We should only get this for leaves */
3798 BUG_ON(path->lowest_level);
3799 buf = path->nodes[0];
3801 for (i = 0; i < btrfs_header_nritems(buf); i++) {
3802 unsigned int shift = 0, offset;
3804 if (i == 0 && btrfs_item_end_nr(buf, i) !=
3805 BTRFS_LEAF_DATA_SIZE(root)) {
3806 if (btrfs_item_end_nr(buf, i) >
3807 BTRFS_LEAF_DATA_SIZE(root)) {
3808 ret = delete_bogus_item(trans, root, path,
3812 fprintf(stderr, "item is off the end of the "
3813 "leaf, can't fix\n");
3817 shift = BTRFS_LEAF_DATA_SIZE(root) -
3818 btrfs_item_end_nr(buf, i);
3819 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
3820 btrfs_item_offset_nr(buf, i - 1)) {
3821 if (btrfs_item_end_nr(buf, i) >
3822 btrfs_item_offset_nr(buf, i - 1)) {
3823 ret = delete_bogus_item(trans, root, path,
3827 fprintf(stderr, "items overlap, can't fix\n");
3831 shift = btrfs_item_offset_nr(buf, i - 1) -
3832 btrfs_item_end_nr(buf, i);
3837 printf("Shifting item nr %d by %u bytes in block %llu\n",
3838 i, shift, (unsigned long long)buf->start);
3839 offset = btrfs_item_offset_nr(buf, i);
3840 memmove_extent_buffer(buf,
3841 btrfs_leaf_data(buf) + offset + shift,
3842 btrfs_leaf_data(buf) + offset,
3843 btrfs_item_size_nr(buf, i));
3844 btrfs_set_item_offset(buf, btrfs_item_nr(i),
3846 btrfs_mark_buffer_dirty(buf);
3850 * We may have moved things, in which case we want to exit so we don't
3851 * write those changes out. Once we have proper abort functionality in
3852 * progs this can be changed to something nicer.
3859 * Attempt to fix basic block failures. If we can't fix it for whatever reason
3860 * then just return -EIO.
3862 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
3863 struct btrfs_root *root,
3864 struct extent_buffer *buf,
3865 enum btrfs_tree_block_status status)
3867 struct ulist *roots;
3868 struct ulist_node *node;
3869 struct btrfs_root *search_root;
3870 struct btrfs_path *path;
3871 struct ulist_iterator iter;
3872 struct btrfs_key root_key, key;
3875 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
3876 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3879 path = btrfs_alloc_path();
3883 ret = btrfs_find_all_roots(trans, root->fs_info, buf->start,
3886 btrfs_free_path(path);
3890 ULIST_ITER_INIT(&iter);
3891 while ((node = ulist_next(roots, &iter))) {
3892 root_key.objectid = node->val;
3893 root_key.type = BTRFS_ROOT_ITEM_KEY;
3894 root_key.offset = (u64)-1;
3896 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
3902 record_root_in_trans(trans, search_root);
3904 path->lowest_level = btrfs_header_level(buf);
3905 path->skip_check_block = 1;
3906 if (path->lowest_level)
3907 btrfs_node_key_to_cpu(buf, &key, 0);
3909 btrfs_item_key_to_cpu(buf, &key, 0);
3910 ret = btrfs_search_slot(trans, search_root, &key, path, 0, 1);
3915 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
3916 ret = fix_key_order(trans, search_root, path);
3917 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
3918 ret = fix_item_offset(trans, search_root, path);
3921 btrfs_release_path(path);
3924 btrfs_free_path(path);
3928 static int check_block(struct btrfs_trans_handle *trans,
3929 struct btrfs_root *root,
3930 struct cache_tree *extent_cache,
3931 struct extent_buffer *buf, u64 flags)
3933 struct extent_record *rec;
3934 struct cache_extent *cache;
3935 struct btrfs_key key;
3936 enum btrfs_tree_block_status status;
3940 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
3943 rec = container_of(cache, struct extent_record, cache);
3944 rec->generation = btrfs_header_generation(buf);
3946 level = btrfs_header_level(buf);
3947 if (btrfs_header_nritems(buf) > 0) {
3950 btrfs_item_key_to_cpu(buf, &key, 0);
3952 btrfs_node_key_to_cpu(buf, &key, 0);
3954 rec->info_objectid = key.objectid;
3956 rec->info_level = level;
3958 if (btrfs_is_leaf(buf))
3959 status = btrfs_check_leaf(root, &rec->parent_key, buf);
3961 status = btrfs_check_node(root, &rec->parent_key, buf);
3963 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3965 status = try_to_fix_bad_block(trans, root, buf,
3967 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3969 fprintf(stderr, "bad block %llu\n",
3970 (unsigned long long)buf->start);
3973 * Signal to callers we need to start the scan over
3974 * again since we'll have cow'ed blocks.
3979 rec->content_checked = 1;
3980 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3981 rec->owner_ref_checked = 1;
3983 ret = check_owner_ref(root, rec, buf);
3985 rec->owner_ref_checked = 1;
3989 maybe_free_extent_rec(extent_cache, rec);
3993 static struct tree_backref *find_tree_backref(struct extent_record *rec,
3994 u64 parent, u64 root)
3996 struct list_head *cur = rec->backrefs.next;
3997 struct extent_backref *node;
3998 struct tree_backref *back;
4000 while(cur != &rec->backrefs) {
4001 node = list_entry(cur, struct extent_backref, list);
4005 back = (struct tree_backref *)node;
4007 if (!node->full_backref)
4009 if (parent == back->parent)
4012 if (node->full_backref)
4014 if (back->root == root)
4021 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
4022 u64 parent, u64 root)
4024 struct tree_backref *ref = malloc(sizeof(*ref));
4025 memset(&ref->node, 0, sizeof(ref->node));
4027 ref->parent = parent;
4028 ref->node.full_backref = 1;
4031 ref->node.full_backref = 0;
4033 list_add_tail(&ref->node.list, &rec->backrefs);
4038 static struct data_backref *find_data_backref(struct extent_record *rec,
4039 u64 parent, u64 root,
4040 u64 owner, u64 offset,
4042 u64 disk_bytenr, u64 bytes)
4044 struct list_head *cur = rec->backrefs.next;
4045 struct extent_backref *node;
4046 struct data_backref *back;
4048 while(cur != &rec->backrefs) {
4049 node = list_entry(cur, struct extent_backref, list);
4053 back = (struct data_backref *)node;
4055 if (!node->full_backref)
4057 if (parent == back->parent)
4060 if (node->full_backref)
4062 if (back->root == root && back->owner == owner &&
4063 back->offset == offset) {
4064 if (found_ref && node->found_ref &&
4065 (back->bytes != bytes ||
4066 back->disk_bytenr != disk_bytenr))
4075 static struct data_backref *alloc_data_backref(struct extent_record *rec,
4076 u64 parent, u64 root,
4077 u64 owner, u64 offset,
4080 struct data_backref *ref = malloc(sizeof(*ref));
4081 memset(&ref->node, 0, sizeof(ref->node));
4082 ref->node.is_data = 1;
4085 ref->parent = parent;
4088 ref->node.full_backref = 1;
4092 ref->offset = offset;
4093 ref->node.full_backref = 0;
4095 ref->bytes = max_size;
4098 list_add_tail(&ref->node.list, &rec->backrefs);
4099 if (max_size > rec->max_size)
4100 rec->max_size = max_size;
4104 static int add_extent_rec(struct cache_tree *extent_cache,
4105 struct btrfs_key *parent_key, u64 parent_gen,
4106 u64 start, u64 nr, u64 extent_item_refs,
4107 int is_root, int inc_ref, int set_checked,
4108 int metadata, int extent_rec, u64 max_size)
4110 struct extent_record *rec;
4111 struct cache_extent *cache;
4115 cache = lookup_cache_extent(extent_cache, start, nr);
4117 rec = container_of(cache, struct extent_record, cache);
4121 rec->nr = max(nr, max_size);
4124 * We need to make sure to reset nr to whatever the extent
4125 * record says was the real size, this way we can compare it to
4129 if (start != rec->start || rec->found_rec) {
4130 struct extent_record *tmp;
4133 if (list_empty(&rec->list))
4134 list_add_tail(&rec->list,
4135 &duplicate_extents);
4138 * We have to do this song and dance in case we
4139 * find an extent record that falls inside of
4140 * our current extent record but does not have
4141 * the same objectid.
4143 tmp = malloc(sizeof(*tmp));
4147 tmp->max_size = max_size;
4150 tmp->metadata = metadata;
4151 tmp->extent_item_refs = extent_item_refs;
4152 INIT_LIST_HEAD(&tmp->list);
4153 list_add_tail(&tmp->list, &rec->dups);
4154 rec->num_duplicates++;
4161 if (extent_item_refs && !dup) {
4162 if (rec->extent_item_refs) {
4163 fprintf(stderr, "block %llu rec "
4164 "extent_item_refs %llu, passed %llu\n",
4165 (unsigned long long)start,
4166 (unsigned long long)
4167 rec->extent_item_refs,
4168 (unsigned long long)extent_item_refs);
4170 rec->extent_item_refs = extent_item_refs;
4175 rec->content_checked = 1;
4176 rec->owner_ref_checked = 1;
4180 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
4182 rec->parent_generation = parent_gen;
4184 if (rec->max_size < max_size)
4185 rec->max_size = max_size;
4187 maybe_free_extent_rec(extent_cache, rec);
4190 rec = malloc(sizeof(*rec));
4192 rec->max_size = max_size;
4193 rec->nr = max(nr, max_size);
4194 rec->found_rec = !!extent_rec;
4195 rec->content_checked = 0;
4196 rec->owner_ref_checked = 0;
4197 rec->num_duplicates = 0;
4198 rec->metadata = metadata;
4199 INIT_LIST_HEAD(&rec->backrefs);
4200 INIT_LIST_HEAD(&rec->dups);
4201 INIT_LIST_HEAD(&rec->list);
4213 if (extent_item_refs)
4214 rec->extent_item_refs = extent_item_refs;
4216 rec->extent_item_refs = 0;
4219 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
4221 memset(&rec->parent_key, 0, sizeof(*parent_key));
4224 rec->parent_generation = parent_gen;
4226 rec->parent_generation = 0;
4228 rec->cache.start = start;
4229 rec->cache.size = nr;
4230 ret = insert_cache_extent(extent_cache, &rec->cache);
4234 rec->content_checked = 1;
4235 rec->owner_ref_checked = 1;
4240 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
4241 u64 parent, u64 root, int found_ref)
4243 struct extent_record *rec;
4244 struct tree_backref *back;
4245 struct cache_extent *cache;
4247 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4249 add_extent_rec(extent_cache, NULL, 0, bytenr,
4250 1, 0, 0, 0, 0, 1, 0, 0);
4251 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4256 rec = container_of(cache, struct extent_record, cache);
4257 if (rec->start != bytenr) {
4261 back = find_tree_backref(rec, parent, root);
4263 back = alloc_tree_backref(rec, parent, root);
4266 if (back->node.found_ref) {
4267 fprintf(stderr, "Extent back ref already exists "
4268 "for %llu parent %llu root %llu \n",
4269 (unsigned long long)bytenr,
4270 (unsigned long long)parent,
4271 (unsigned long long)root);
4273 back->node.found_ref = 1;
4275 if (back->node.found_extent_tree) {
4276 fprintf(stderr, "Extent back ref already exists "
4277 "for %llu parent %llu root %llu \n",
4278 (unsigned long long)bytenr,
4279 (unsigned long long)parent,
4280 (unsigned long long)root);
4282 back->node.found_extent_tree = 1;
4284 maybe_free_extent_rec(extent_cache, rec);
4288 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
4289 u64 parent, u64 root, u64 owner, u64 offset,
4290 u32 num_refs, int found_ref, u64 max_size)
4292 struct extent_record *rec;
4293 struct data_backref *back;
4294 struct cache_extent *cache;
4296 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4298 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
4300 cache = lookup_cache_extent(extent_cache, bytenr, 1);
4305 rec = container_of(cache, struct extent_record, cache);
4306 if (rec->max_size < max_size)
4307 rec->max_size = max_size;
4310 * If found_ref is set then max_size is the real size and must match the
4311 * existing refs. So if we have already found a ref then we need to
4312 * make sure that this ref matches the existing one, otherwise we need
4313 * to add a new backref so we can notice that the backrefs don't match
4314 * and we need to figure out who is telling the truth. This is to
4315 * account for that awful fsync bug I introduced where we'd end up with
4316 * a btrfs_file_extent_item that would have its length include multiple
4317 * prealloc extents or point inside of a prealloc extent.
4319 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
4322 back = alloc_data_backref(rec, parent, root, owner, offset,
4326 BUG_ON(num_refs != 1);
4327 if (back->node.found_ref)
4328 BUG_ON(back->bytes != max_size);
4329 back->node.found_ref = 1;
4330 back->found_ref += 1;
4331 back->bytes = max_size;
4332 back->disk_bytenr = bytenr;
4334 rec->content_checked = 1;
4335 rec->owner_ref_checked = 1;
4337 if (back->node.found_extent_tree) {
4338 fprintf(stderr, "Extent back ref already exists "
4339 "for %llu parent %llu root %llu "
4340 "owner %llu offset %llu num_refs %lu\n",
4341 (unsigned long long)bytenr,
4342 (unsigned long long)parent,
4343 (unsigned long long)root,
4344 (unsigned long long)owner,
4345 (unsigned long long)offset,
4346 (unsigned long)num_refs);
4348 back->num_refs = num_refs;
4349 back->node.found_extent_tree = 1;
4351 maybe_free_extent_rec(extent_cache, rec);
4355 static int add_pending(struct cache_tree *pending,
4356 struct cache_tree *seen, u64 bytenr, u32 size)
4359 ret = add_cache_extent(seen, bytenr, size);
4362 add_cache_extent(pending, bytenr, size);
4366 static int pick_next_pending(struct cache_tree *pending,
4367 struct cache_tree *reada,
4368 struct cache_tree *nodes,
4369 u64 last, struct block_info *bits, int bits_nr,
4372 unsigned long node_start = last;
4373 struct cache_extent *cache;
4376 cache = search_cache_extent(reada, 0);
4378 bits[0].start = cache->start;
4379 bits[0].size = cache->size;
4384 if (node_start > 32768)
4385 node_start -= 32768;
4387 cache = search_cache_extent(nodes, node_start);
4389 cache = search_cache_extent(nodes, 0);
4392 cache = search_cache_extent(pending, 0);
4397 bits[ret].start = cache->start;
4398 bits[ret].size = cache->size;
4399 cache = next_cache_extent(cache);
4401 } while (cache && ret < bits_nr);
4407 bits[ret].start = cache->start;
4408 bits[ret].size = cache->size;
4409 cache = next_cache_extent(cache);
4411 } while (cache && ret < bits_nr);
4413 if (bits_nr - ret > 8) {
4414 u64 lookup = bits[0].start + bits[0].size;
4415 struct cache_extent *next;
4416 next = search_cache_extent(pending, lookup);
4418 if (next->start - lookup > 32768)
4420 bits[ret].start = next->start;
4421 bits[ret].size = next->size;
4422 lookup = next->start + next->size;
4426 next = next_cache_extent(next);
4434 static void free_chunk_record(struct cache_extent *cache)
4436 struct chunk_record *rec;
4438 rec = container_of(cache, struct chunk_record, cache);
4439 list_del_init(&rec->list);
4440 list_del_init(&rec->dextents);
4444 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
4446 cache_tree_free_extents(chunk_cache, free_chunk_record);
4449 static void free_device_record(struct rb_node *node)
4451 struct device_record *rec;
4453 rec = container_of(node, struct device_record, node);
4457 FREE_RB_BASED_TREE(device_cache, free_device_record);
4459 int insert_block_group_record(struct block_group_tree *tree,
4460 struct block_group_record *bg_rec)
4464 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
4468 list_add_tail(&bg_rec->list, &tree->block_groups);
4472 static void free_block_group_record(struct cache_extent *cache)
4474 struct block_group_record *rec;
4476 rec = container_of(cache, struct block_group_record, cache);
4477 list_del_init(&rec->list);
4481 void free_block_group_tree(struct block_group_tree *tree)
4483 cache_tree_free_extents(&tree->tree, free_block_group_record);
4486 int insert_device_extent_record(struct device_extent_tree *tree,
4487 struct device_extent_record *de_rec)
4492 * Device extent is a bit different from the other extents, because
4493 * the extents which belong to the different devices may have the
4494 * same start and size, so we need use the special extent cache
4495 * search/insert functions.
4497 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
4501 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
4502 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
4506 static void free_device_extent_record(struct cache_extent *cache)
4508 struct device_extent_record *rec;
4510 rec = container_of(cache, struct device_extent_record, cache);
4511 if (!list_empty(&rec->chunk_list))
4512 list_del_init(&rec->chunk_list);
4513 if (!list_empty(&rec->device_list))
4514 list_del_init(&rec->device_list);
4518 void free_device_extent_tree(struct device_extent_tree *tree)
4520 cache_tree_free_extents(&tree->tree, free_device_extent_record);
4523 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4524 static int process_extent_ref_v0(struct cache_tree *extent_cache,
4525 struct extent_buffer *leaf, int slot)
4527 struct btrfs_extent_ref_v0 *ref0;
4528 struct btrfs_key key;
4530 btrfs_item_key_to_cpu(leaf, &key, slot);
4531 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
4532 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
4533 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
4535 add_data_backref(extent_cache, key.objectid, key.offset, 0,
4536 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
4542 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
4543 struct btrfs_key *key,
4546 struct btrfs_chunk *ptr;
4547 struct chunk_record *rec;
4550 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
4551 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
4553 rec = malloc(btrfs_chunk_record_size(num_stripes));
4555 fprintf(stderr, "memory allocation failed\n");
4559 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
4561 INIT_LIST_HEAD(&rec->list);
4562 INIT_LIST_HEAD(&rec->dextents);
4565 rec->cache.start = key->offset;
4566 rec->cache.size = btrfs_chunk_length(leaf, ptr);
4568 rec->generation = btrfs_header_generation(leaf);
4570 rec->objectid = key->objectid;
4571 rec->type = key->type;
4572 rec->offset = key->offset;
4574 rec->length = rec->cache.size;
4575 rec->owner = btrfs_chunk_owner(leaf, ptr);
4576 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
4577 rec->type_flags = btrfs_chunk_type(leaf, ptr);
4578 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
4579 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
4580 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
4581 rec->num_stripes = num_stripes;
4582 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
4584 for (i = 0; i < rec->num_stripes; ++i) {
4585 rec->stripes[i].devid =
4586 btrfs_stripe_devid_nr(leaf, ptr, i);
4587 rec->stripes[i].offset =
4588 btrfs_stripe_offset_nr(leaf, ptr, i);
4589 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
4590 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
4597 static int process_chunk_item(struct cache_tree *chunk_cache,
4598 struct btrfs_key *key, struct extent_buffer *eb,
4601 struct chunk_record *rec;
4604 rec = btrfs_new_chunk_record(eb, key, slot);
4605 ret = insert_cache_extent(chunk_cache, &rec->cache);
4607 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
4608 rec->offset, rec->length);
4615 static int process_device_item(struct rb_root *dev_cache,
4616 struct btrfs_key *key, struct extent_buffer *eb, int slot)
4618 struct btrfs_dev_item *ptr;
4619 struct device_record *rec;
4622 ptr = btrfs_item_ptr(eb,
4623 slot, struct btrfs_dev_item);
4625 rec = malloc(sizeof(*rec));
4627 fprintf(stderr, "memory allocation failed\n");
4631 rec->devid = key->offset;
4632 rec->generation = btrfs_header_generation(eb);
4634 rec->objectid = key->objectid;
4635 rec->type = key->type;
4636 rec->offset = key->offset;
4638 rec->devid = btrfs_device_id(eb, ptr);
4639 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
4640 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
4642 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
4644 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
4651 struct block_group_record *
4652 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
4655 struct btrfs_block_group_item *ptr;
4656 struct block_group_record *rec;
4658 rec = malloc(sizeof(*rec));
4660 fprintf(stderr, "memory allocation failed\n");
4663 memset(rec, 0, sizeof(*rec));
4665 rec->cache.start = key->objectid;
4666 rec->cache.size = key->offset;
4668 rec->generation = btrfs_header_generation(leaf);
4670 rec->objectid = key->objectid;
4671 rec->type = key->type;
4672 rec->offset = key->offset;
4674 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
4675 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
4677 INIT_LIST_HEAD(&rec->list);
4682 static int process_block_group_item(struct block_group_tree *block_group_cache,
4683 struct btrfs_key *key,
4684 struct extent_buffer *eb, int slot)
4686 struct block_group_record *rec;
4689 rec = btrfs_new_block_group_record(eb, key, slot);
4690 ret = insert_block_group_record(block_group_cache, rec);
4692 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
4693 rec->objectid, rec->offset);
4700 struct device_extent_record *
4701 btrfs_new_device_extent_record(struct extent_buffer *leaf,
4702 struct btrfs_key *key, int slot)
4704 struct device_extent_record *rec;
4705 struct btrfs_dev_extent *ptr;
4707 rec = malloc(sizeof(*rec));
4709 fprintf(stderr, "memory allocation failed\n");
4712 memset(rec, 0, sizeof(*rec));
4714 rec->cache.objectid = key->objectid;
4715 rec->cache.start = key->offset;
4717 rec->generation = btrfs_header_generation(leaf);
4719 rec->objectid = key->objectid;
4720 rec->type = key->type;
4721 rec->offset = key->offset;
4723 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
4724 rec->chunk_objecteid =
4725 btrfs_dev_extent_chunk_objectid(leaf, ptr);
4727 btrfs_dev_extent_chunk_offset(leaf, ptr);
4728 rec->length = btrfs_dev_extent_length(leaf, ptr);
4729 rec->cache.size = rec->length;
4731 INIT_LIST_HEAD(&rec->chunk_list);
4732 INIT_LIST_HEAD(&rec->device_list);
4738 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
4739 struct btrfs_key *key, struct extent_buffer *eb,
4742 struct device_extent_record *rec;
4745 rec = btrfs_new_device_extent_record(eb, key, slot);
4746 ret = insert_device_extent_record(dev_extent_cache, rec);
4749 "Device extent[%llu, %llu, %llu] existed.\n",
4750 rec->objectid, rec->offset, rec->length);
4757 static int process_extent_item(struct btrfs_root *root,
4758 struct cache_tree *extent_cache,
4759 struct extent_buffer *eb, int slot)
4761 struct btrfs_extent_item *ei;
4762 struct btrfs_extent_inline_ref *iref;
4763 struct btrfs_extent_data_ref *dref;
4764 struct btrfs_shared_data_ref *sref;
4765 struct btrfs_key key;
4769 u32 item_size = btrfs_item_size_nr(eb, slot);
4775 btrfs_item_key_to_cpu(eb, &key, slot);
4777 if (key.type == BTRFS_METADATA_ITEM_KEY) {
4779 num_bytes = root->leafsize;
4781 num_bytes = key.offset;
4784 if (item_size < sizeof(*ei)) {
4785 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4786 struct btrfs_extent_item_v0 *ei0;
4787 BUG_ON(item_size != sizeof(*ei0));
4788 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
4789 refs = btrfs_extent_refs_v0(eb, ei0);
4793 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
4794 num_bytes, refs, 0, 0, 0, metadata, 1,
4798 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
4799 refs = btrfs_extent_refs(eb, ei);
4801 add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
4802 refs, 0, 0, 0, metadata, 1, num_bytes);
4804 ptr = (unsigned long)(ei + 1);
4805 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
4806 key.type == BTRFS_EXTENT_ITEM_KEY)
4807 ptr += sizeof(struct btrfs_tree_block_info);
4809 end = (unsigned long)ei + item_size;
4811 iref = (struct btrfs_extent_inline_ref *)ptr;
4812 type = btrfs_extent_inline_ref_type(eb, iref);
4813 offset = btrfs_extent_inline_ref_offset(eb, iref);
4815 case BTRFS_TREE_BLOCK_REF_KEY:
4816 add_tree_backref(extent_cache, key.objectid,
4819 case BTRFS_SHARED_BLOCK_REF_KEY:
4820 add_tree_backref(extent_cache, key.objectid,
4823 case BTRFS_EXTENT_DATA_REF_KEY:
4824 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
4825 add_data_backref(extent_cache, key.objectid, 0,
4826 btrfs_extent_data_ref_root(eb, dref),
4827 btrfs_extent_data_ref_objectid(eb,
4829 btrfs_extent_data_ref_offset(eb, dref),
4830 btrfs_extent_data_ref_count(eb, dref),
4833 case BTRFS_SHARED_DATA_REF_KEY:
4834 sref = (struct btrfs_shared_data_ref *)(iref + 1);
4835 add_data_backref(extent_cache, key.objectid, offset,
4837 btrfs_shared_data_ref_count(eb, sref),
4841 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
4842 key.objectid, key.type, num_bytes);
4845 ptr += btrfs_extent_inline_ref_size(type);
4852 static int check_cache_range(struct btrfs_root *root,
4853 struct btrfs_block_group_cache *cache,
4854 u64 offset, u64 bytes)
4856 struct btrfs_free_space *entry;
4862 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
4863 bytenr = btrfs_sb_offset(i);
4864 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
4865 cache->key.objectid, bytenr, 0,
4866 &logical, &nr, &stripe_len);
4871 if (logical[nr] + stripe_len <= offset)
4873 if (offset + bytes <= logical[nr])
4875 if (logical[nr] == offset) {
4876 if (stripe_len >= bytes) {
4880 bytes -= stripe_len;
4881 offset += stripe_len;
4882 } else if (logical[nr] < offset) {
4883 if (logical[nr] + stripe_len >=
4888 bytes = (offset + bytes) -
4889 (logical[nr] + stripe_len);
4890 offset = logical[nr] + stripe_len;
4893 * Could be tricky, the super may land in the
4894 * middle of the area we're checking. First
4895 * check the easiest case, it's at the end.
4897 if (logical[nr] + stripe_len >=
4899 bytes = logical[nr] - offset;
4903 /* Check the left side */
4904 ret = check_cache_range(root, cache,
4906 logical[nr] - offset);
4912 /* Now we continue with the right side */
4913 bytes = (offset + bytes) -
4914 (logical[nr] + stripe_len);
4915 offset = logical[nr] + stripe_len;
4922 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
4924 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
4925 offset, offset+bytes);
4929 if (entry->offset != offset) {
4930 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
4935 if (entry->bytes != bytes) {
4936 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
4937 bytes, entry->bytes, offset);
4941 unlink_free_space(cache->free_space_ctl, entry);
4946 static int verify_space_cache(struct btrfs_root *root,
4947 struct btrfs_block_group_cache *cache)
4949 struct btrfs_path *path;
4950 struct extent_buffer *leaf;
4951 struct btrfs_key key;
4955 path = btrfs_alloc_path();
4959 root = root->fs_info->extent_root;
4961 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
4963 key.objectid = last;
4965 key.type = BTRFS_EXTENT_ITEM_KEY;
4967 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4972 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4973 ret = btrfs_next_leaf(root, path);
4981 leaf = path->nodes[0];
4982 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4983 if (key.objectid >= cache->key.offset + cache->key.objectid)
4985 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
4986 key.type != BTRFS_METADATA_ITEM_KEY) {
4991 if (last == key.objectid) {
4992 if (key.type == BTRFS_EXTENT_ITEM_KEY)
4993 last = key.objectid + key.offset;
4995 last = key.objectid + root->leafsize;
5000 ret = check_cache_range(root, cache, last,
5001 key.objectid - last);
5004 if (key.type == BTRFS_EXTENT_ITEM_KEY)
5005 last = key.objectid + key.offset;
5007 last = key.objectid + root->leafsize;
5011 if (last < cache->key.objectid + cache->key.offset)
5012 ret = check_cache_range(root, cache, last,
5013 cache->key.objectid +
5014 cache->key.offset - last);
5017 btrfs_free_path(path);
5020 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
5021 fprintf(stderr, "There are still entries left in the space "
5029 static int check_space_cache(struct btrfs_root *root)
5031 struct btrfs_block_group_cache *cache;
5032 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
5036 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
5037 btrfs_super_generation(root->fs_info->super_copy) !=
5038 btrfs_super_cache_generation(root->fs_info->super_copy)) {
5039 printf("cache and super generation don't match, space cache "
5040 "will be invalidated\n");
5045 cache = btrfs_lookup_first_block_group(root->fs_info, start);
5049 start = cache->key.objectid + cache->key.offset;
5050 if (!cache->free_space_ctl) {
5051 if (btrfs_init_free_space_ctl(cache,
5052 root->sectorsize)) {
5057 btrfs_remove_free_space_cache(cache);
5060 ret = load_free_space_cache(root->fs_info, cache);
5064 ret = verify_space_cache(root, cache);
5066 fprintf(stderr, "cache appears valid but isnt %Lu\n",
5067 cache->key.objectid);
5072 return error ? -EINVAL : 0;
5075 static int read_extent_data(struct btrfs_root *root, char *data,
5076 u64 logical, u64 *len, int mirror)
5079 struct btrfs_multi_bio *multi = NULL;
5080 struct btrfs_fs_info *info = root->fs_info;
5081 struct btrfs_device *device;
5085 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
5086 &multi, mirror, NULL);
5088 fprintf(stderr, "Couldn't map the block %llu\n",
5092 device = multi->stripes[0].dev;
5094 if (device->fd == 0)
5099 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
5109 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
5110 u64 num_bytes, unsigned long leaf_offset,
5111 struct extent_buffer *eb) {
5114 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
5116 unsigned long csum_offset;
5120 u64 data_checked = 0;
5126 if (num_bytes % root->sectorsize)
5129 data = malloc(num_bytes);
5133 while (offset < num_bytes) {
5136 read_len = num_bytes - offset;
5137 /* read as much space once a time */
5138 ret = read_extent_data(root, data + offset,
5139 bytenr + offset, &read_len, mirror);
5143 /* verify every 4k data's checksum */
5144 while (data_checked < read_len) {
5146 tmp = offset + data_checked;
5148 csum = btrfs_csum_data(NULL, (char *)data + tmp,
5149 csum, root->sectorsize);
5150 btrfs_csum_final(csum, (char *)&csum);
5152 csum_offset = leaf_offset +
5153 tmp / root->sectorsize * csum_size;
5154 read_extent_buffer(eb, (char *)&csum_expected,
5155 csum_offset, csum_size);
5156 /* try another mirror */
5157 if (csum != csum_expected) {
5158 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
5159 mirror, bytenr + tmp,
5160 csum, csum_expected);
5161 num_copies = btrfs_num_copies(
5162 &root->fs_info->mapping_tree,
5164 if (mirror < num_copies - 1) {
5169 data_checked += root->sectorsize;
5178 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
5181 struct btrfs_path *path;
5182 struct extent_buffer *leaf;
5183 struct btrfs_key key;
5186 path = btrfs_alloc_path();
5188 fprintf(stderr, "Error allocing path\n");
5192 key.objectid = bytenr;
5193 key.type = BTRFS_EXTENT_ITEM_KEY;
5194 key.offset = (u64)-1;
5197 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
5200 fprintf(stderr, "Error looking up extent record %d\n", ret);
5201 btrfs_free_path(path);
5204 if (path->slots[0] > 0) {
5207 ret = btrfs_prev_leaf(root, path);
5210 } else if (ret > 0) {
5217 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5220 * Block group items come before extent items if they have the same
5221 * bytenr, so walk back one more just in case. Dear future traveler,
5222 * first congrats on mastering time travel. Now if it's not too much
5223 * trouble could you go back to 2006 and tell Chris to make the
5224 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
5225 * EXTENT_ITEM_KEY please?
5227 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
5228 if (path->slots[0] > 0) {
5231 ret = btrfs_prev_leaf(root, path);
5234 } else if (ret > 0) {
5239 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
5243 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5244 ret = btrfs_next_leaf(root, path);
5246 fprintf(stderr, "Error going to next leaf "
5248 btrfs_free_path(path);
5254 leaf = path->nodes[0];
5255 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5256 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
5260 if (key.objectid + key.offset < bytenr) {
5264 if (key.objectid > bytenr + num_bytes)
5267 if (key.objectid == bytenr) {
5268 if (key.offset >= num_bytes) {
5272 num_bytes -= key.offset;
5273 bytenr += key.offset;
5274 } else if (key.objectid < bytenr) {
5275 if (key.objectid + key.offset >= bytenr + num_bytes) {
5279 num_bytes = (bytenr + num_bytes) -
5280 (key.objectid + key.offset);
5281 bytenr = key.objectid + key.offset;
5283 if (key.objectid + key.offset < bytenr + num_bytes) {
5284 u64 new_start = key.objectid + key.offset;
5285 u64 new_bytes = bytenr + num_bytes - new_start;
5288 * Weird case, the extent is in the middle of
5289 * our range, we'll have to search one side
5290 * and then the other. Not sure if this happens
5291 * in real life, but no harm in coding it up
5292 * anyway just in case.
5294 btrfs_release_path(path);
5295 ret = check_extent_exists(root, new_start,
5298 fprintf(stderr, "Right section didn't "
5302 num_bytes = key.objectid - bytenr;
5305 num_bytes = key.objectid - bytenr;
5312 if (num_bytes && !ret) {
5313 fprintf(stderr, "There are no extents for csum range "
5314 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
5318 btrfs_free_path(path);
5322 static int check_csums(struct btrfs_root *root)
5324 struct btrfs_path *path;
5325 struct extent_buffer *leaf;
5326 struct btrfs_key key;
5327 u64 offset = 0, num_bytes = 0;
5328 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
5332 unsigned long leaf_offset;
5334 root = root->fs_info->csum_root;
5335 if (!extent_buffer_uptodate(root->node)) {
5336 fprintf(stderr, "No valid csum tree found\n");
5340 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
5341 key.type = BTRFS_EXTENT_CSUM_KEY;
5344 path = btrfs_alloc_path();
5348 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5350 fprintf(stderr, "Error searching csum tree %d\n", ret);
5351 btrfs_free_path(path);
5355 if (ret > 0 && path->slots[0])
5360 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5361 ret = btrfs_next_leaf(root, path);
5363 fprintf(stderr, "Error going to next leaf "
5370 leaf = path->nodes[0];
5372 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5373 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
5378 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
5379 csum_size) * root->sectorsize;
5380 if (!check_data_csum)
5381 goto skip_csum_check;
5382 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
5383 ret = check_extent_csums(root, key.offset, data_len,
5389 offset = key.offset;
5390 } else if (key.offset != offset + num_bytes) {
5391 ret = check_extent_exists(root, offset, num_bytes);
5393 fprintf(stderr, "Csum exists for %Lu-%Lu but "
5394 "there is no extent record\n",
5395 offset, offset+num_bytes);
5398 offset = key.offset;
5401 num_bytes += data_len;
5405 btrfs_free_path(path);
5409 static int is_dropped_key(struct btrfs_key *key,
5410 struct btrfs_key *drop_key) {
5411 if (key->objectid < drop_key->objectid)
5413 else if (key->objectid == drop_key->objectid) {
5414 if (key->type < drop_key->type)
5416 else if (key->type == drop_key->type) {
5417 if (key->offset < drop_key->offset)
5424 static int calc_extent_flag(struct btrfs_root *root,
5425 struct cache_tree *extent_cache,
5426 struct extent_buffer *buf,
5427 struct root_item_record *ri,
5431 int nritems = btrfs_header_nritems(buf);
5432 struct btrfs_key key;
5433 struct extent_record *rec;
5434 struct cache_extent *cache;
5435 struct data_backref *dback;
5436 struct tree_backref *tback;
5437 struct extent_buffer *new_buf;
5447 * Except file/reloc tree, we can not have
5450 if (ri->objectid < BTRFS_FIRST_FREE_OBJECTID)
5455 if (buf->start == ri->bytenr)
5457 if (btrfs_is_leaf(buf)) {
5459 * we are searching from original root, world
5460 * peace is achieved, we use normal backref.
5462 owner = btrfs_header_owner(buf);
5463 if (owner == ri->objectid)
5466 * we check every eb here, and if any of
5467 * eb dosen't have original root refers
5468 * to this eb, we set full backref flag for
5469 * this extent, otherwise normal backref.
5471 for (i = 0; i < nritems; i++) {
5472 struct btrfs_file_extent_item *fi;
5473 btrfs_item_key_to_cpu(buf, &key, i);
5475 if (key.type != BTRFS_EXTENT_DATA_KEY)
5477 fi = btrfs_item_ptr(buf, i,
5478 struct btrfs_file_extent_item);
5479 if (btrfs_file_extent_type(buf, fi) ==
5480 BTRFS_FILE_EXTENT_INLINE)
5482 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
5484 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
5485 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5488 offset = btrfs_file_extent_offset(buf, fi);
5489 rec = container_of(cache, struct extent_record, cache);
5490 dback = find_data_backref(rec, 0, ri->objectid, owner,
5491 key.offset - offset, 1, bytenr, bytenr);
5497 level = btrfs_header_level(buf);
5498 for (i = 0; i < nritems; i++) {
5499 ptr = btrfs_node_blockptr(buf, i);
5500 size = btrfs_level_size(root, level);
5502 new_buf = read_tree_block(root, ptr, size, 0);
5503 if (!extent_buffer_uptodate(new_buf)) {
5504 free_extent_buffer(new_buf);
5509 * we are searching from origin root, world
5510 * peace is achieved, we use normal backref.
5512 owner = btrfs_header_owner(new_buf);
5513 free_extent_buffer(new_buf);
5514 if (owner == ri->objectid)
5517 cache = lookup_cache_extent(extent_cache, ptr, size);
5520 rec = container_of(cache, struct extent_record, cache);
5521 tback = find_tree_backref(rec, 0, owner);
5529 cache = lookup_cache_extent(extent_cache, buf->start, 1);
5530 /* we have added this extent before */
5532 rec = container_of(cache, struct extent_record, cache);
5533 rec->flag_block_full_backref = 0;
5536 *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5537 cache = lookup_cache_extent(extent_cache, buf->start, 1);
5538 /* we have added this extent before */
5540 rec = container_of(cache, struct extent_record, cache);
5541 rec->flag_block_full_backref = 1;
5545 static int run_next_block(struct btrfs_trans_handle *trans,
5546 struct btrfs_root *root,
5547 struct block_info *bits,
5550 struct cache_tree *pending,
5551 struct cache_tree *seen,
5552 struct cache_tree *reada,
5553 struct cache_tree *nodes,
5554 struct cache_tree *extent_cache,
5555 struct cache_tree *chunk_cache,
5556 struct rb_root *dev_cache,
5557 struct block_group_tree *block_group_cache,
5558 struct device_extent_tree *dev_extent_cache,
5559 struct root_item_record *ri)
5561 struct extent_buffer *buf;
5572 struct btrfs_key key;
5573 struct cache_extent *cache;
5576 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
5577 bits_nr, &reada_bits);
5582 for(i = 0; i < nritems; i++) {
5583 ret = add_cache_extent(reada, bits[i].start,
5588 /* fixme, get the parent transid */
5589 readahead_tree_block(root, bits[i].start,
5593 *last = bits[0].start;
5594 bytenr = bits[0].start;
5595 size = bits[0].size;
5597 cache = lookup_cache_extent(pending, bytenr, size);
5599 remove_cache_extent(pending, cache);
5602 cache = lookup_cache_extent(reada, bytenr, size);
5604 remove_cache_extent(reada, cache);
5607 cache = lookup_cache_extent(nodes, bytenr, size);
5609 remove_cache_extent(nodes, cache);
5612 cache = lookup_cache_extent(extent_cache, bytenr, size);
5614 struct extent_record *rec;
5616 rec = container_of(cache, struct extent_record, cache);
5617 gen = rec->parent_generation;
5620 /* fixme, get the real parent transid */
5621 buf = read_tree_block(root, bytenr, size, gen);
5622 if (!extent_buffer_uptodate(buf)) {
5623 record_bad_block_io(root->fs_info,
5624 extent_cache, bytenr, size);
5628 nritems = btrfs_header_nritems(buf);
5631 * FIXME, this only works only if we don't have any full
5634 if (!init_extent_tree) {
5635 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
5636 btrfs_header_level(buf), 1, NULL,
5642 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
5647 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5652 owner = btrfs_header_owner(buf);
5655 ret = check_block(trans, root, extent_cache, buf, flags);
5659 if (btrfs_is_leaf(buf)) {
5660 btree_space_waste += btrfs_leaf_free_space(root, buf);
5661 for (i = 0; i < nritems; i++) {
5662 struct btrfs_file_extent_item *fi;
5663 btrfs_item_key_to_cpu(buf, &key, i);
5664 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
5665 process_extent_item(root, extent_cache, buf,
5669 if (key.type == BTRFS_METADATA_ITEM_KEY) {
5670 process_extent_item(root, extent_cache, buf,
5674 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
5676 btrfs_item_size_nr(buf, i);
5679 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
5680 process_chunk_item(chunk_cache, &key, buf, i);
5683 if (key.type == BTRFS_DEV_ITEM_KEY) {
5684 process_device_item(dev_cache, &key, buf, i);
5687 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5688 process_block_group_item(block_group_cache,
5692 if (key.type == BTRFS_DEV_EXTENT_KEY) {
5693 process_device_extent_item(dev_extent_cache,
5698 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
5699 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5700 process_extent_ref_v0(extent_cache, buf, i);
5707 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
5708 add_tree_backref(extent_cache, key.objectid, 0,
5712 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
5713 add_tree_backref(extent_cache, key.objectid,
5717 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
5718 struct btrfs_extent_data_ref *ref;
5719 ref = btrfs_item_ptr(buf, i,
5720 struct btrfs_extent_data_ref);
5721 add_data_backref(extent_cache,
5723 btrfs_extent_data_ref_root(buf, ref),
5724 btrfs_extent_data_ref_objectid(buf,
5726 btrfs_extent_data_ref_offset(buf, ref),
5727 btrfs_extent_data_ref_count(buf, ref),
5728 0, root->sectorsize);
5731 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
5732 struct btrfs_shared_data_ref *ref;
5733 ref = btrfs_item_ptr(buf, i,
5734 struct btrfs_shared_data_ref);
5735 add_data_backref(extent_cache,
5736 key.objectid, key.offset, 0, 0, 0,
5737 btrfs_shared_data_ref_count(buf, ref),
5738 0, root->sectorsize);
5741 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
5742 struct bad_item *bad;
5744 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
5748 bad = malloc(sizeof(struct bad_item));
5751 INIT_LIST_HEAD(&bad->list);
5752 memcpy(&bad->key, &key,
5753 sizeof(struct btrfs_key));
5754 bad->root_id = owner;
5755 list_add_tail(&bad->list, &delete_items);
5758 if (key.type != BTRFS_EXTENT_DATA_KEY)
5760 fi = btrfs_item_ptr(buf, i,
5761 struct btrfs_file_extent_item);
5762 if (btrfs_file_extent_type(buf, fi) ==
5763 BTRFS_FILE_EXTENT_INLINE)
5765 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
5768 data_bytes_allocated +=
5769 btrfs_file_extent_disk_num_bytes(buf, fi);
5770 if (data_bytes_allocated < root->sectorsize) {
5773 data_bytes_referenced +=
5774 btrfs_file_extent_num_bytes(buf, fi);
5775 add_data_backref(extent_cache,
5776 btrfs_file_extent_disk_bytenr(buf, fi),
5777 parent, owner, key.objectid, key.offset -
5778 btrfs_file_extent_offset(buf, fi), 1, 1,
5779 btrfs_file_extent_disk_num_bytes(buf, fi));
5783 struct btrfs_key first_key;
5785 first_key.objectid = 0;
5788 btrfs_item_key_to_cpu(buf, &first_key, 0);
5789 level = btrfs_header_level(buf);
5790 for (i = 0; i < nritems; i++) {
5791 ptr = btrfs_node_blockptr(buf, i);
5792 size = btrfs_level_size(root, level - 1);
5793 btrfs_node_key_to_cpu(buf, &key, i);
5795 if ((level == ri->drop_level)
5796 && is_dropped_key(&key, &ri->drop_key)) {
5800 ret = add_extent_rec(extent_cache, &key,
5801 btrfs_node_ptr_generation(buf, i),
5802 ptr, size, 0, 0, 1, 0, 1, 0,
5806 add_tree_backref(extent_cache, ptr, parent, owner, 1);
5809 add_pending(nodes, seen, ptr, size);
5811 add_pending(pending, seen, ptr, size);
5814 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
5815 nritems) * sizeof(struct btrfs_key_ptr);
5817 total_btree_bytes += buf->len;
5818 if (fs_root_objectid(btrfs_header_owner(buf)))
5819 total_fs_tree_bytes += buf->len;
5820 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
5821 total_extent_tree_bytes += buf->len;
5822 if (!found_old_backref &&
5823 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
5824 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
5825 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
5826 found_old_backref = 1;
5828 free_extent_buffer(buf);
5832 static int add_root_to_pending(struct extent_buffer *buf,
5833 struct cache_tree *extent_cache,
5834 struct cache_tree *pending,
5835 struct cache_tree *seen,
5836 struct cache_tree *nodes,
5839 if (btrfs_header_level(buf) > 0)
5840 add_pending(nodes, seen, buf->start, buf->len);
5842 add_pending(pending, seen, buf->start, buf->len);
5843 add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
5844 0, 1, 1, 0, 1, 0, buf->len);
5846 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
5847 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
5848 add_tree_backref(extent_cache, buf->start, buf->start,
5851 add_tree_backref(extent_cache, buf->start, 0, objectid, 1);
5855 /* as we fix the tree, we might be deleting blocks that
5856 * we're tracking for repair. This hook makes sure we
5857 * remove any backrefs for blocks as we are fixing them.
5859 static int free_extent_hook(struct btrfs_trans_handle *trans,
5860 struct btrfs_root *root,
5861 u64 bytenr, u64 num_bytes, u64 parent,
5862 u64 root_objectid, u64 owner, u64 offset,
5865 struct extent_record *rec;
5866 struct cache_extent *cache;
5868 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
5870 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
5871 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
5875 rec = container_of(cache, struct extent_record, cache);
5877 struct data_backref *back;
5878 back = find_data_backref(rec, parent, root_objectid, owner,
5879 offset, 1, bytenr, num_bytes);
5882 if (back->node.found_ref) {
5883 back->found_ref -= refs_to_drop;
5885 rec->refs -= refs_to_drop;
5887 if (back->node.found_extent_tree) {
5888 back->num_refs -= refs_to_drop;
5889 if (rec->extent_item_refs)
5890 rec->extent_item_refs -= refs_to_drop;
5892 if (back->found_ref == 0)
5893 back->node.found_ref = 0;
5894 if (back->num_refs == 0)
5895 back->node.found_extent_tree = 0;
5897 if (!back->node.found_extent_tree && back->node.found_ref) {
5898 list_del(&back->node.list);
5902 struct tree_backref *back;
5903 back = find_tree_backref(rec, parent, root_objectid);
5906 if (back->node.found_ref) {
5909 back->node.found_ref = 0;
5911 if (back->node.found_extent_tree) {
5912 if (rec->extent_item_refs)
5913 rec->extent_item_refs--;
5914 back->node.found_extent_tree = 0;
5916 if (!back->node.found_extent_tree && back->node.found_ref) {
5917 list_del(&back->node.list);
5921 maybe_free_extent_rec(extent_cache, rec);
5926 static int delete_extent_records(struct btrfs_trans_handle *trans,
5927 struct btrfs_root *root,
5928 struct btrfs_path *path,
5929 u64 bytenr, u64 new_len)
5931 struct btrfs_key key;
5932 struct btrfs_key found_key;
5933 struct extent_buffer *leaf;
5938 key.objectid = bytenr;
5940 key.offset = (u64)-1;
5943 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
5950 if (path->slots[0] == 0)
5956 leaf = path->nodes[0];
5957 slot = path->slots[0];
5959 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5960 if (found_key.objectid != bytenr)
5963 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
5964 found_key.type != BTRFS_METADATA_ITEM_KEY &&
5965 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5966 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
5967 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
5968 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
5969 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
5970 btrfs_release_path(path);
5971 if (found_key.type == 0) {
5972 if (found_key.offset == 0)
5974 key.offset = found_key.offset - 1;
5975 key.type = found_key.type;
5977 key.type = found_key.type - 1;
5978 key.offset = (u64)-1;
5982 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
5983 found_key.objectid, found_key.type, found_key.offset);
5985 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
5988 btrfs_release_path(path);
5990 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5991 found_key.type == BTRFS_METADATA_ITEM_KEY) {
5992 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
5993 found_key.offset : root->leafsize;
5995 ret = btrfs_update_block_group(trans, root, bytenr,
6002 btrfs_release_path(path);
6007 * for a single backref, this will allocate a new extent
6008 * and add the backref to it.
6010 static int record_extent(struct btrfs_trans_handle *trans,
6011 struct btrfs_fs_info *info,
6012 struct btrfs_path *path,
6013 struct extent_record *rec,
6014 struct extent_backref *back,
6015 int allocated, u64 flags)
6018 struct btrfs_root *extent_root = info->extent_root;
6019 struct extent_buffer *leaf;
6020 struct btrfs_key ins_key;
6021 struct btrfs_extent_item *ei;
6022 struct tree_backref *tback;
6023 struct data_backref *dback;
6024 struct btrfs_tree_block_info *bi;
6027 rec->max_size = max_t(u64, rec->max_size,
6028 info->extent_root->leafsize);
6031 u32 item_size = sizeof(*ei);
6034 item_size += sizeof(*bi);
6036 ins_key.objectid = rec->start;
6037 ins_key.offset = rec->max_size;
6038 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
6040 ret = btrfs_insert_empty_item(trans, extent_root, path,
6041 &ins_key, item_size);
6045 leaf = path->nodes[0];
6046 ei = btrfs_item_ptr(leaf, path->slots[0],
6047 struct btrfs_extent_item);
6049 btrfs_set_extent_refs(leaf, ei, 0);
6050 btrfs_set_extent_generation(leaf, ei, rec->generation);
6052 if (back->is_data) {
6053 btrfs_set_extent_flags(leaf, ei,
6054 BTRFS_EXTENT_FLAG_DATA);
6056 struct btrfs_disk_key copy_key;;
6058 tback = (struct tree_backref *)back;
6059 bi = (struct btrfs_tree_block_info *)(ei + 1);
6060 memset_extent_buffer(leaf, 0, (unsigned long)bi,
6063 btrfs_set_disk_key_objectid(©_key,
6064 rec->info_objectid);
6065 btrfs_set_disk_key_type(©_key, 0);
6066 btrfs_set_disk_key_offset(©_key, 0);
6068 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
6069 btrfs_set_tree_block_key(leaf, bi, ©_key);
6071 btrfs_set_extent_flags(leaf, ei,
6072 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
6075 btrfs_mark_buffer_dirty(leaf);
6076 ret = btrfs_update_block_group(trans, extent_root, rec->start,
6077 rec->max_size, 1, 0);
6080 btrfs_release_path(path);
6083 if (back->is_data) {
6087 dback = (struct data_backref *)back;
6088 if (back->full_backref)
6089 parent = dback->parent;
6093 for (i = 0; i < dback->found_ref; i++) {
6094 /* if parent != 0, we're doing a full backref
6095 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
6096 * just makes the backref allocator create a data
6099 ret = btrfs_inc_extent_ref(trans, info->extent_root,
6100 rec->start, rec->max_size,
6104 BTRFS_FIRST_FREE_OBJECTID :
6110 fprintf(stderr, "adding new data backref"
6111 " on %llu %s %llu owner %llu"
6112 " offset %llu found %d\n",
6113 (unsigned long long)rec->start,
6114 back->full_backref ?
6116 back->full_backref ?
6117 (unsigned long long)parent :
6118 (unsigned long long)dback->root,
6119 (unsigned long long)dback->owner,
6120 (unsigned long long)dback->offset,
6125 tback = (struct tree_backref *)back;
6126 if (back->full_backref)
6127 parent = tback->parent;
6131 ret = btrfs_inc_extent_ref(trans, info->extent_root,
6132 rec->start, rec->max_size,
6133 parent, tback->root, 0, 0);
6134 fprintf(stderr, "adding new tree backref on "
6135 "start %llu len %llu parent %llu root %llu\n",
6136 rec->start, rec->max_size, tback->parent, tback->root);
6141 btrfs_release_path(path);
6145 struct extent_entry {
6150 struct list_head list;
6153 static struct extent_entry *find_entry(struct list_head *entries,
6154 u64 bytenr, u64 bytes)
6156 struct extent_entry *entry = NULL;
6158 list_for_each_entry(entry, entries, list) {
6159 if (entry->bytenr == bytenr && entry->bytes == bytes)
6166 static struct extent_entry *find_most_right_entry(struct list_head *entries)
6168 struct extent_entry *entry, *best = NULL, *prev = NULL;
6170 list_for_each_entry(entry, entries, list) {
6177 * If there are as many broken entries as entries then we know
6178 * not to trust this particular entry.
6180 if (entry->broken == entry->count)
6184 * If our current entry == best then we can't be sure our best
6185 * is really the best, so we need to keep searching.
6187 if (best && best->count == entry->count) {
6193 /* Prev == entry, not good enough, have to keep searching */
6194 if (!prev->broken && prev->count == entry->count)
6198 best = (prev->count > entry->count) ? prev : entry;
6199 else if (best->count < entry->count)
6207 static int repair_ref(struct btrfs_trans_handle *trans,
6208 struct btrfs_fs_info *info, struct btrfs_path *path,
6209 struct data_backref *dback, struct extent_entry *entry)
6211 struct btrfs_root *root;
6212 struct btrfs_file_extent_item *fi;
6213 struct extent_buffer *leaf;
6214 struct btrfs_key key;
6218 key.objectid = dback->root;
6219 key.type = BTRFS_ROOT_ITEM_KEY;
6220 key.offset = (u64)-1;
6221 root = btrfs_read_fs_root(info, &key);
6223 fprintf(stderr, "Couldn't find root for our ref\n");
6228 * The backref points to the original offset of the extent if it was
6229 * split, so we need to search down to the offset we have and then walk
6230 * forward until we find the backref we're looking for.
6232 key.objectid = dback->owner;
6233 key.type = BTRFS_EXTENT_DATA_KEY;
6234 key.offset = dback->offset;
6235 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6237 fprintf(stderr, "Error looking up ref %d\n", ret);
6242 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6243 ret = btrfs_next_leaf(root, path);
6245 fprintf(stderr, "Couldn't find our ref, next\n");
6249 leaf = path->nodes[0];
6250 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6251 if (key.objectid != dback->owner ||
6252 key.type != BTRFS_EXTENT_DATA_KEY) {
6253 fprintf(stderr, "Couldn't find our ref, search\n");
6256 fi = btrfs_item_ptr(leaf, path->slots[0],
6257 struct btrfs_file_extent_item);
6258 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6259 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
6261 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
6266 btrfs_release_path(path);
6269 * Have to make sure that this root gets updated when we commit the
6272 record_root_in_trans(trans, root);
6275 * Ok we have the key of the file extent we want to fix, now we can cow
6276 * down to the thing and fix it.
6278 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6280 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
6281 key.objectid, key.type, key.offset, ret);
6285 fprintf(stderr, "Well that's odd, we just found this key "
6286 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
6290 leaf = path->nodes[0];
6291 fi = btrfs_item_ptr(leaf, path->slots[0],
6292 struct btrfs_file_extent_item);
6294 if (btrfs_file_extent_compression(leaf, fi) &&
6295 dback->disk_bytenr != entry->bytenr) {
6296 fprintf(stderr, "Ref doesn't match the record start and is "
6297 "compressed, please take a btrfs-image of this file "
6298 "system and send it to a btrfs developer so they can "
6299 "complete this functionality for bytenr %Lu\n",
6300 dback->disk_bytenr);
6304 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
6305 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
6306 } else if (dback->disk_bytenr > entry->bytenr) {
6307 u64 off_diff, offset;
6309 off_diff = dback->disk_bytenr - entry->bytenr;
6310 offset = btrfs_file_extent_offset(leaf, fi);
6311 if (dback->disk_bytenr + offset +
6312 btrfs_file_extent_num_bytes(leaf, fi) >
6313 entry->bytenr + entry->bytes) {
6314 fprintf(stderr, "Ref is past the entry end, please "
6315 "take a btrfs-image of this file system and "
6316 "send it to a btrfs developer, ref %Lu\n",
6317 dback->disk_bytenr);
6321 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
6322 btrfs_set_file_extent_offset(leaf, fi, offset);
6323 } else if (dback->disk_bytenr < entry->bytenr) {
6326 offset = btrfs_file_extent_offset(leaf, fi);
6327 if (dback->disk_bytenr + offset < entry->bytenr) {
6328 fprintf(stderr, "Ref is before the entry start, please"
6329 " take a btrfs-image of this file system and "
6330 "send it to a btrfs developer, ref %Lu\n",
6331 dback->disk_bytenr);
6335 offset += dback->disk_bytenr;
6336 offset -= entry->bytenr;
6337 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
6338 btrfs_set_file_extent_offset(leaf, fi, offset);
6341 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
6344 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
6345 * only do this if we aren't using compression, otherwise it's a
6348 if (!btrfs_file_extent_compression(leaf, fi))
6349 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
6351 printf("ram bytes may be wrong?\n");
6352 btrfs_mark_buffer_dirty(leaf);
6353 btrfs_release_path(path);
6357 static int verify_backrefs(struct btrfs_trans_handle *trans,
6358 struct btrfs_fs_info *info, struct btrfs_path *path,
6359 struct extent_record *rec)
6361 struct extent_backref *back;
6362 struct data_backref *dback;
6363 struct extent_entry *entry, *best = NULL;
6366 int broken_entries = 0;
6371 * Metadata is easy and the backrefs should always agree on bytenr and
6372 * size, if not we've got bigger issues.
6377 list_for_each_entry(back, &rec->backrefs, list) {
6378 if (back->full_backref || !back->is_data)
6381 dback = (struct data_backref *)back;
6384 * We only pay attention to backrefs that we found a real
6387 if (dback->found_ref == 0)
6391 * For now we only catch when the bytes don't match, not the
6392 * bytenr. We can easily do this at the same time, but I want
6393 * to have a fs image to test on before we just add repair
6394 * functionality willy-nilly so we know we won't screw up the
6398 entry = find_entry(&entries, dback->disk_bytenr,
6401 entry = malloc(sizeof(struct extent_entry));
6406 memset(entry, 0, sizeof(*entry));
6407 entry->bytenr = dback->disk_bytenr;
6408 entry->bytes = dback->bytes;
6409 list_add_tail(&entry->list, &entries);
6414 * If we only have on entry we may think the entries agree when
6415 * in reality they don't so we have to do some extra checking.
6417 if (dback->disk_bytenr != rec->start ||
6418 dback->bytes != rec->nr || back->broken)
6429 /* Yay all the backrefs agree, carry on good sir */
6430 if (nr_entries <= 1 && !mismatch)
6433 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
6434 "%Lu\n", rec->start);
6437 * First we want to see if the backrefs can agree amongst themselves who
6438 * is right, so figure out which one of the entries has the highest
6441 best = find_most_right_entry(&entries);
6444 * Ok so we may have an even split between what the backrefs think, so
6445 * this is where we use the extent ref to see what it thinks.
6448 entry = find_entry(&entries, rec->start, rec->nr);
6449 if (!entry && (!broken_entries || !rec->found_rec)) {
6450 fprintf(stderr, "Backrefs don't agree with each other "
6451 "and extent record doesn't agree with anybody,"
6452 " so we can't fix bytenr %Lu bytes %Lu\n",
6453 rec->start, rec->nr);
6456 } else if (!entry) {
6458 * Ok our backrefs were broken, we'll assume this is the
6459 * correct value and add an entry for this range.
6461 entry = malloc(sizeof(struct extent_entry));
6466 memset(entry, 0, sizeof(*entry));
6467 entry->bytenr = rec->start;
6468 entry->bytes = rec->nr;
6469 list_add_tail(&entry->list, &entries);
6473 best = find_most_right_entry(&entries);
6475 fprintf(stderr, "Backrefs and extent record evenly "
6476 "split on who is right, this is going to "
6477 "require user input to fix bytenr %Lu bytes "
6478 "%Lu\n", rec->start, rec->nr);
6485 * I don't think this can happen currently as we'll abort() if we catch
6486 * this case higher up, but in case somebody removes that we still can't
6487 * deal with it properly here yet, so just bail out of that's the case.
6489 if (best->bytenr != rec->start) {
6490 fprintf(stderr, "Extent start and backref starts don't match, "
6491 "please use btrfs-image on this file system and send "
6492 "it to a btrfs developer so they can make fsck fix "
6493 "this particular case. bytenr is %Lu, bytes is %Lu\n",
6494 rec->start, rec->nr);
6500 * Ok great we all agreed on an extent record, let's go find the real
6501 * references and fix up the ones that don't match.
6503 list_for_each_entry(back, &rec->backrefs, list) {
6504 if (back->full_backref || !back->is_data)
6507 dback = (struct data_backref *)back;
6510 * Still ignoring backrefs that don't have a real ref attached
6513 if (dback->found_ref == 0)
6516 if (dback->bytes == best->bytes &&
6517 dback->disk_bytenr == best->bytenr)
6520 ret = repair_ref(trans, info, path, dback, best);
6526 * Ok we messed with the actual refs, which means we need to drop our
6527 * entire cache and go back and rescan. I know this is a huge pain and
6528 * adds a lot of extra work, but it's the only way to be safe. Once all
6529 * the backrefs agree we may not need to do anything to the extent
6534 while (!list_empty(&entries)) {
6535 entry = list_entry(entries.next, struct extent_entry, list);
6536 list_del_init(&entry->list);
6542 static int process_duplicates(struct btrfs_root *root,
6543 struct cache_tree *extent_cache,
6544 struct extent_record *rec)
6546 struct extent_record *good, *tmp;
6547 struct cache_extent *cache;
6551 * If we found a extent record for this extent then return, or if we
6552 * have more than one duplicate we are likely going to need to delete
6555 if (rec->found_rec || rec->num_duplicates > 1)
6558 /* Shouldn't happen but just in case */
6559 BUG_ON(!rec->num_duplicates);
6562 * So this happens if we end up with a backref that doesn't match the
6563 * actual extent entry. So either the backref is bad or the extent
6564 * entry is bad. Either way we want to have the extent_record actually
6565 * reflect what we found in the extent_tree, so we need to take the
6566 * duplicate out and use that as the extent_record since the only way we
6567 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
6569 remove_cache_extent(extent_cache, &rec->cache);
6571 good = list_entry(rec->dups.next, struct extent_record, list);
6572 list_del_init(&good->list);
6573 INIT_LIST_HEAD(&good->backrefs);
6574 INIT_LIST_HEAD(&good->dups);
6575 good->cache.start = good->start;
6576 good->cache.size = good->nr;
6577 good->content_checked = 0;
6578 good->owner_ref_checked = 0;
6579 good->num_duplicates = 0;
6580 good->refs = rec->refs;
6581 list_splice_init(&rec->backrefs, &good->backrefs);
6583 cache = lookup_cache_extent(extent_cache, good->start,
6587 tmp = container_of(cache, struct extent_record, cache);
6590 * If we find another overlapping extent and it's found_rec is
6591 * set then it's a duplicate and we need to try and delete
6594 if (tmp->found_rec || tmp->num_duplicates > 0) {
6595 if (list_empty(&good->list))
6596 list_add_tail(&good->list,
6597 &duplicate_extents);
6598 good->num_duplicates += tmp->num_duplicates + 1;
6599 list_splice_init(&tmp->dups, &good->dups);
6600 list_del_init(&tmp->list);
6601 list_add_tail(&tmp->list, &good->dups);
6602 remove_cache_extent(extent_cache, &tmp->cache);
6607 * Ok we have another non extent item backed extent rec, so lets
6608 * just add it to this extent and carry on like we did above.
6610 good->refs += tmp->refs;
6611 list_splice_init(&tmp->backrefs, &good->backrefs);
6612 remove_cache_extent(extent_cache, &tmp->cache);
6615 ret = insert_cache_extent(extent_cache, &good->cache);
6618 return good->num_duplicates ? 0 : 1;
6621 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
6622 struct btrfs_root *root,
6623 struct extent_record *rec)
6625 LIST_HEAD(delete_list);
6626 struct btrfs_path *path;
6627 struct extent_record *tmp, *good, *n;
6630 struct btrfs_key key;
6632 path = btrfs_alloc_path();
6639 /* Find the record that covers all of the duplicates. */
6640 list_for_each_entry(tmp, &rec->dups, list) {
6641 if (good->start < tmp->start)
6643 if (good->nr > tmp->nr)
6646 if (tmp->start + tmp->nr < good->start + good->nr) {
6647 fprintf(stderr, "Ok we have overlapping extents that "
6648 "aren't completely covered by eachother, this "
6649 "is going to require more careful thought. "
6650 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
6651 tmp->start, tmp->nr, good->start, good->nr);
6658 list_add_tail(&rec->list, &delete_list);
6660 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
6663 list_move_tail(&tmp->list, &delete_list);
6666 root = root->fs_info->extent_root;
6667 list_for_each_entry(tmp, &delete_list, list) {
6668 if (tmp->found_rec == 0)
6670 key.objectid = tmp->start;
6671 key.type = BTRFS_EXTENT_ITEM_KEY;
6672 key.offset = tmp->nr;
6674 /* Shouldn't happen but just in case */
6675 if (tmp->metadata) {
6676 fprintf(stderr, "Well this shouldn't happen, extent "
6677 "record overlaps but is metadata? "
6678 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
6682 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6688 ret = btrfs_del_item(trans, root, path);
6691 btrfs_release_path(path);
6696 while (!list_empty(&delete_list)) {
6697 tmp = list_entry(delete_list.next, struct extent_record, list);
6698 list_del_init(&tmp->list);
6704 while (!list_empty(&rec->dups)) {
6705 tmp = list_entry(rec->dups.next, struct extent_record, list);
6706 list_del_init(&tmp->list);
6710 btrfs_free_path(path);
6712 if (!ret && !nr_del)
6713 rec->num_duplicates = 0;
6715 return ret ? ret : nr_del;
6718 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
6719 struct btrfs_fs_info *info,
6720 struct btrfs_path *path,
6721 struct cache_tree *extent_cache,
6722 struct extent_record *rec)
6724 struct btrfs_root *root;
6725 struct extent_backref *back;
6726 struct data_backref *dback;
6727 struct cache_extent *cache;
6728 struct btrfs_file_extent_item *fi;
6729 struct btrfs_key key;
6733 list_for_each_entry(back, &rec->backrefs, list) {
6734 /* Don't care about full backrefs (poor unloved backrefs) */
6735 if (back->full_backref || !back->is_data)
6738 dback = (struct data_backref *)back;
6740 /* We found this one, we don't need to do a lookup */
6741 if (dback->found_ref)
6744 key.objectid = dback->root;
6745 key.type = BTRFS_ROOT_ITEM_KEY;
6746 key.offset = (u64)-1;
6748 root = btrfs_read_fs_root(info, &key);
6750 /* No root, definitely a bad ref, skip */
6751 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
6753 /* Other err, exit */
6755 return PTR_ERR(root);
6757 key.objectid = dback->owner;
6758 key.type = BTRFS_EXTENT_DATA_KEY;
6759 key.offset = dback->offset;
6760 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6762 btrfs_release_path(path);
6765 /* Didn't find it, we can carry on */
6770 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
6771 struct btrfs_file_extent_item);
6772 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
6773 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
6774 btrfs_release_path(path);
6775 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6777 struct extent_record *tmp;
6778 tmp = container_of(cache, struct extent_record, cache);
6781 * If we found an extent record for the bytenr for this
6782 * particular backref then we can't add it to our
6783 * current extent record. We only want to add backrefs
6784 * that don't have a corresponding extent item in the
6785 * extent tree since they likely belong to this record
6786 * and we need to fix it if it doesn't match bytenrs.
6792 dback->found_ref += 1;
6793 dback->disk_bytenr = bytenr;
6794 dback->bytes = bytes;
6797 * Set this so the verify backref code knows not to trust the
6798 * values in this backref.
6807 * Record orphan data ref into corresponding root.
6809 * Return 0 if the extent item contains data ref and recorded.
6810 * Return 1 if the extent item contains no useful data ref
6811 * On that case, it may contains only shared_dataref or metadata backref
6812 * or the file extent exists(this should be handled by the extent bytenr
6814 * Return <0 if something goes wrong.
6816 static int record_orphan_data_extents(struct btrfs_fs_info *fs_info,
6817 struct extent_record *rec)
6819 struct btrfs_key key;
6820 struct btrfs_root *dest_root;
6821 struct extent_backref *back;
6822 struct data_backref *dback;
6823 struct orphan_data_extent *orphan;
6824 struct btrfs_path *path;
6825 int recorded_data_ref = 0;
6830 path = btrfs_alloc_path();
6833 list_for_each_entry(back, &rec->backrefs, list) {
6834 if (back->full_backref || !back->is_data ||
6835 !back->found_extent_tree)
6837 dback = (struct data_backref *)back;
6838 if (dback->found_ref)
6840 key.objectid = dback->root;
6841 key.type = BTRFS_ROOT_ITEM_KEY;
6842 key.offset = (u64)-1;
6844 dest_root = btrfs_read_fs_root(fs_info, &key);
6846 /* For non-exist root we just skip it */
6847 if (IS_ERR(dest_root) || !dest_root)
6850 key.objectid = dback->owner;
6851 key.type = BTRFS_EXTENT_DATA_KEY;
6852 key.offset = dback->offset;
6854 ret = btrfs_search_slot(NULL, dest_root, &key, path, 0, 0);
6856 * For ret < 0, it's OK since the fs-tree may be corrupted,
6857 * we need to record it for inode/file extent rebuild.
6858 * For ret > 0, we record it only for file extent rebuild.
6859 * For ret == 0, the file extent exists but only bytenr
6860 * mismatch, let the original bytenr fix routine to handle,
6866 orphan = malloc(sizeof(*orphan));
6871 INIT_LIST_HEAD(&orphan->list);
6872 orphan->root = dback->root;
6873 orphan->objectid = dback->owner;
6874 orphan->offset = dback->offset;
6875 orphan->disk_bytenr = rec->cache.start;
6876 orphan->disk_len = rec->cache.size;
6877 list_add(&dest_root->orphan_data_extents, &orphan->list);
6878 recorded_data_ref = 1;
6881 btrfs_free_path(path);
6883 return !recorded_data_ref;
6889 * when an incorrect extent item is found, this will delete
6890 * all of the existing entries for it and recreate them
6891 * based on what the tree scan found.
6893 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
6894 struct btrfs_fs_info *info,
6895 struct cache_tree *extent_cache,
6896 struct extent_record *rec)
6899 struct btrfs_path *path;
6900 struct list_head *cur = rec->backrefs.next;
6901 struct cache_extent *cache;
6902 struct extent_backref *back;
6907 * remember our flags for recreating the extent.
6908 * FIXME, if we have cleared extent tree, we can not
6909 * lookup extent info in extent tree.
6911 if (!init_extent_tree) {
6912 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
6913 rec->start, rec->max_size,
6914 rec->metadata, NULL, &flags);
6918 if (rec->flag_block_full_backref)
6919 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6922 path = btrfs_alloc_path();
6926 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
6928 * Sometimes the backrefs themselves are so broken they don't
6929 * get attached to any meaningful rec, so first go back and
6930 * check any of our backrefs that we couldn't find and throw
6931 * them into the list if we find the backref so that
6932 * verify_backrefs can figure out what to do.
6934 ret = find_possible_backrefs(trans, info, path, extent_cache,
6940 /* step one, make sure all of the backrefs agree */
6941 ret = verify_backrefs(trans, info, path, rec);
6945 /* step two, delete all the existing records */
6946 ret = delete_extent_records(trans, info->extent_root, path,
6947 rec->start, rec->max_size);
6952 /* was this block corrupt? If so, don't add references to it */
6953 cache = lookup_cache_extent(info->corrupt_blocks,
6954 rec->start, rec->max_size);
6960 /* step three, recreate all the refs we did find */
6961 while(cur != &rec->backrefs) {
6962 back = list_entry(cur, struct extent_backref, list);
6966 * if we didn't find any references, don't create a
6969 if (!back->found_ref)
6972 ret = record_extent(trans, info, path, rec, back, allocated, flags);
6979 btrfs_free_path(path);
6983 /* right now we only prune from the extent allocation tree */
6984 static int prune_one_block(struct btrfs_trans_handle *trans,
6985 struct btrfs_fs_info *info,
6986 struct btrfs_corrupt_block *corrupt)
6989 struct btrfs_path path;
6990 struct extent_buffer *eb;
6994 int level = corrupt->level + 1;
6996 btrfs_init_path(&path);
6998 /* we want to stop at the parent to our busted block */
6999 path.lowest_level = level;
7001 ret = btrfs_search_slot(trans, info->extent_root,
7002 &corrupt->key, &path, -1, 1);
7007 eb = path.nodes[level];
7014 * hopefully the search gave us the block we want to prune,
7015 * lets try that first
7017 slot = path.slots[level];
7018 found = btrfs_node_blockptr(eb, slot);
7019 if (found == corrupt->cache.start)
7022 nritems = btrfs_header_nritems(eb);
7024 /* the search failed, lets scan this node and hope we find it */
7025 for (slot = 0; slot < nritems; slot++) {
7026 found = btrfs_node_blockptr(eb, slot);
7027 if (found == corrupt->cache.start)
7031 * we couldn't find the bad block. TODO, search all the nodes for pointers
7034 if (eb == info->extent_root->node) {
7039 btrfs_release_path(&path);
7044 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
7045 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
7048 btrfs_release_path(&path);
7052 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
7053 struct btrfs_fs_info *info)
7055 struct cache_extent *cache;
7056 struct btrfs_corrupt_block *corrupt;
7058 cache = search_cache_extent(info->corrupt_blocks, 0);
7062 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
7063 prune_one_block(trans, info, corrupt);
7064 cache = next_cache_extent(cache);
7069 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
7071 struct btrfs_block_group_cache *cache;
7076 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
7077 &start, &end, EXTENT_DIRTY);
7080 clear_extent_dirty(&fs_info->free_space_cache, start, end,
7086 cache = btrfs_lookup_first_block_group(fs_info, start);
7091 start = cache->key.objectid + cache->key.offset;
7095 static int check_extent_refs(struct btrfs_trans_handle *trans,
7096 struct btrfs_root *root,
7097 struct cache_tree *extent_cache)
7099 struct extent_record *rec;
7100 struct cache_extent *cache;
7109 * if we're doing a repair, we have to make sure
7110 * we don't allocate from the problem extents.
7111 * In the worst case, this will be all the
7114 cache = search_cache_extent(extent_cache, 0);
7116 rec = container_of(cache, struct extent_record, cache);
7117 btrfs_pin_extent(root->fs_info,
7118 rec->start, rec->max_size);
7119 cache = next_cache_extent(cache);
7122 /* pin down all the corrupted blocks too */
7123 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
7125 btrfs_pin_extent(root->fs_info,
7126 cache->start, cache->size);
7127 cache = next_cache_extent(cache);
7129 prune_corrupt_blocks(trans, root->fs_info);
7130 reset_cached_block_groups(root->fs_info);
7134 * We need to delete any duplicate entries we find first otherwise we
7135 * could mess up the extent tree when we have backrefs that actually
7136 * belong to a different extent item and not the weird duplicate one.
7138 while (repair && !list_empty(&duplicate_extents)) {
7139 rec = list_entry(duplicate_extents.next, struct extent_record,
7141 list_del_init(&rec->list);
7143 /* Sometimes we can find a backref before we find an actual
7144 * extent, so we need to process it a little bit to see if there
7145 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
7146 * if this is a backref screwup. If we need to delete stuff
7147 * process_duplicates() will return 0, otherwise it will return
7150 if (process_duplicates(root, extent_cache, rec))
7152 ret = delete_duplicate_records(trans, root, rec);
7156 * delete_duplicate_records will return the number of entries
7157 * deleted, so if it's greater than 0 then we know we actually
7158 * did something and we need to remove.
7170 cache = search_cache_extent(extent_cache, 0);
7173 rec = container_of(cache, struct extent_record, cache);
7174 if (rec->num_duplicates) {
7175 fprintf(stderr, "extent item %llu has multiple extent "
7176 "items\n", (unsigned long long)rec->start);
7180 if (rec->refs != rec->extent_item_refs) {
7181 fprintf(stderr, "ref mismatch on [%llu %llu] ",
7182 (unsigned long long)rec->start,
7183 (unsigned long long)rec->nr);
7184 fprintf(stderr, "extent item %llu, found %llu\n",
7185 (unsigned long long)rec->extent_item_refs,
7186 (unsigned long long)rec->refs);
7187 ret = record_orphan_data_extents(root->fs_info, rec);
7194 * we can't use the extent to repair file
7195 * extent, let the fallback method handle it.
7197 if (!fixed && repair) {
7198 ret = fixup_extent_refs(trans,
7209 if (all_backpointers_checked(rec, 1)) {
7210 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
7211 (unsigned long long)rec->start,
7212 (unsigned long long)rec->nr);
7214 if (!fixed && !recorded && repair) {
7215 ret = fixup_extent_refs(trans, root->fs_info,
7223 if (!rec->owner_ref_checked) {
7224 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
7225 (unsigned long long)rec->start,
7226 (unsigned long long)rec->nr);
7227 if (!fixed && !recorded && repair) {
7228 ret = fixup_extent_refs(trans, root->fs_info,
7237 remove_cache_extent(extent_cache, cache);
7238 free_all_extent_backrefs(rec);
7243 if (ret && ret != -EAGAIN) {
7244 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
7247 btrfs_fix_block_accounting(trans, root);
7250 fprintf(stderr, "repaired damaged extent references\n");
7256 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
7260 if (type & BTRFS_BLOCK_GROUP_RAID0) {
7261 stripe_size = length;
7262 stripe_size /= num_stripes;
7263 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
7264 stripe_size = length * 2;
7265 stripe_size /= num_stripes;
7266 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
7267 stripe_size = length;
7268 stripe_size /= (num_stripes - 1);
7269 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
7270 stripe_size = length;
7271 stripe_size /= (num_stripes - 2);
7273 stripe_size = length;
7279 * Check the chunk with its block group/dev list ref:
7280 * Return 0 if all refs seems valid.
7281 * Return 1 if part of refs seems valid, need later check for rebuild ref
7282 * like missing block group and needs to search extent tree to rebuild them.
7283 * Return -1 if essential refs are missing and unable to rebuild.
7285 static int check_chunk_refs(struct chunk_record *chunk_rec,
7286 struct block_group_tree *block_group_cache,
7287 struct device_extent_tree *dev_extent_cache,
7290 struct cache_extent *block_group_item;
7291 struct block_group_record *block_group_rec;
7292 struct cache_extent *dev_extent_item;
7293 struct device_extent_record *dev_extent_rec;
7300 block_group_item = lookup_cache_extent(&block_group_cache->tree,
7303 if (block_group_item) {
7304 block_group_rec = container_of(block_group_item,
7305 struct block_group_record,
7307 if (chunk_rec->length != block_group_rec->offset ||
7308 chunk_rec->offset != block_group_rec->objectid ||
7309 chunk_rec->type_flags != block_group_rec->flags) {
7312 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
7313 chunk_rec->objectid,
7318 chunk_rec->type_flags,
7319 block_group_rec->objectid,
7320 block_group_rec->type,
7321 block_group_rec->offset,
7322 block_group_rec->offset,
7323 block_group_rec->objectid,
7324 block_group_rec->flags);
7327 list_del_init(&block_group_rec->list);
7328 chunk_rec->bg_rec = block_group_rec;
7333 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
7334 chunk_rec->objectid,
7339 chunk_rec->type_flags);
7343 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
7344 chunk_rec->num_stripes);
7345 for (i = 0; i < chunk_rec->num_stripes; ++i) {
7346 devid = chunk_rec->stripes[i].devid;
7347 offset = chunk_rec->stripes[i].offset;
7348 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
7349 devid, offset, length);
7350 if (dev_extent_item) {
7351 dev_extent_rec = container_of(dev_extent_item,
7352 struct device_extent_record,
7354 if (dev_extent_rec->objectid != devid ||
7355 dev_extent_rec->offset != offset ||
7356 dev_extent_rec->chunk_offset != chunk_rec->offset ||
7357 dev_extent_rec->length != length) {
7360 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
7361 chunk_rec->objectid,
7364 chunk_rec->stripes[i].devid,
7365 chunk_rec->stripes[i].offset,
7366 dev_extent_rec->objectid,
7367 dev_extent_rec->offset,
7368 dev_extent_rec->length);
7371 list_move(&dev_extent_rec->chunk_list,
7372 &chunk_rec->dextents);
7377 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
7378 chunk_rec->objectid,
7381 chunk_rec->stripes[i].devid,
7382 chunk_rec->stripes[i].offset);
7389 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
7390 int check_chunks(struct cache_tree *chunk_cache,
7391 struct block_group_tree *block_group_cache,
7392 struct device_extent_tree *dev_extent_cache,
7393 struct list_head *good, struct list_head *bad,
7394 struct list_head *rebuild, int silent)
7396 struct cache_extent *chunk_item;
7397 struct chunk_record *chunk_rec;
7398 struct block_group_record *bg_rec;
7399 struct device_extent_record *dext_rec;
7403 chunk_item = first_cache_extent(chunk_cache);
7404 while (chunk_item) {
7405 chunk_rec = container_of(chunk_item, struct chunk_record,
7407 err = check_chunk_refs(chunk_rec, block_group_cache,
7408 dev_extent_cache, silent);
7411 if (err == 0 && good)
7412 list_add_tail(&chunk_rec->list, good);
7413 if (err > 0 && rebuild)
7414 list_add_tail(&chunk_rec->list, rebuild);
7416 list_add_tail(&chunk_rec->list, bad);
7417 chunk_item = next_cache_extent(chunk_item);
7420 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
7423 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
7431 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
7435 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
7446 static int check_device_used(struct device_record *dev_rec,
7447 struct device_extent_tree *dext_cache)
7449 struct cache_extent *cache;
7450 struct device_extent_record *dev_extent_rec;
7453 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
7455 dev_extent_rec = container_of(cache,
7456 struct device_extent_record,
7458 if (dev_extent_rec->objectid != dev_rec->devid)
7461 list_del_init(&dev_extent_rec->device_list);
7462 total_byte += dev_extent_rec->length;
7463 cache = next_cache_extent(cache);
7466 if (total_byte != dev_rec->byte_used) {
7468 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
7469 total_byte, dev_rec->byte_used, dev_rec->objectid,
7470 dev_rec->type, dev_rec->offset);
7477 /* check btrfs_dev_item -> btrfs_dev_extent */
7478 static int check_devices(struct rb_root *dev_cache,
7479 struct device_extent_tree *dev_extent_cache)
7481 struct rb_node *dev_node;
7482 struct device_record *dev_rec;
7483 struct device_extent_record *dext_rec;
7487 dev_node = rb_first(dev_cache);
7489 dev_rec = container_of(dev_node, struct device_record, node);
7490 err = check_device_used(dev_rec, dev_extent_cache);
7494 dev_node = rb_next(dev_node);
7496 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
7499 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
7500 dext_rec->objectid, dext_rec->offset, dext_rec->length);
7507 static int add_root_item_to_list(struct list_head *head,
7508 u64 objectid, u64 bytenr,
7509 u8 level, u8 drop_level,
7510 int level_size, struct btrfs_key *drop_key)
7513 struct root_item_record *ri_rec;
7514 ri_rec = malloc(sizeof(*ri_rec));
7517 ri_rec->bytenr = bytenr;
7518 ri_rec->objectid = objectid;
7519 ri_rec->level = level;
7520 ri_rec->level_size = level_size;
7521 ri_rec->drop_level = drop_level;
7523 memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
7524 list_add_tail(&ri_rec->list, head);
7529 static int deal_root_from_list(struct list_head *list,
7530 struct btrfs_trans_handle *trans,
7531 struct btrfs_root *root,
7532 struct block_info *bits,
7534 struct cache_tree *pending,
7535 struct cache_tree *seen,
7536 struct cache_tree *reada,
7537 struct cache_tree *nodes,
7538 struct cache_tree *extent_cache,
7539 struct cache_tree *chunk_cache,
7540 struct rb_root *dev_cache,
7541 struct block_group_tree *block_group_cache,
7542 struct device_extent_tree *dev_extent_cache)
7547 while (!list_empty(list)) {
7548 struct root_item_record *rec;
7549 struct extent_buffer *buf;
7550 rec = list_entry(list->next,
7551 struct root_item_record, list);
7553 buf = read_tree_block(root->fs_info->tree_root,
7554 rec->bytenr, rec->level_size, 0);
7555 if (!extent_buffer_uptodate(buf)) {
7556 free_extent_buffer(buf);
7560 add_root_to_pending(buf, extent_cache, pending,
7561 seen, nodes, rec->objectid);
7563 * To rebuild extent tree, we need deal with snapshot
7564 * one by one, otherwise we deal with node firstly which
7565 * can maximize readahead.
7567 if (!init_extent_tree && !rec->drop_level)
7570 ret = run_next_block(trans, root, bits, bits_nr, &last,
7571 pending, seen, reada,
7572 nodes, extent_cache,
7573 chunk_cache, dev_cache,
7575 dev_extent_cache, rec);
7580 free_extent_buffer(buf);
7581 list_del(&rec->list);
7585 ret = run_next_block(trans, root, bits, bits_nr, &last,
7586 pending, seen, reada,
7587 nodes, extent_cache,
7588 chunk_cache, dev_cache,
7590 dev_extent_cache, NULL);
7600 static int check_chunks_and_extents(struct btrfs_root *root)
7602 struct rb_root dev_cache;
7603 struct cache_tree chunk_cache;
7604 struct block_group_tree block_group_cache;
7605 struct device_extent_tree dev_extent_cache;
7606 struct cache_tree extent_cache;
7607 struct cache_tree seen;
7608 struct cache_tree pending;
7609 struct cache_tree reada;
7610 struct cache_tree nodes;
7611 struct cache_tree corrupt_blocks;
7612 struct btrfs_path path;
7613 struct btrfs_key key;
7614 struct btrfs_key found_key;
7616 struct block_info *bits;
7618 struct extent_buffer *leaf;
7619 struct btrfs_trans_handle *trans = NULL;
7621 struct btrfs_root_item ri;
7622 struct list_head dropping_trees;
7623 struct list_head normal_trees;
7624 struct btrfs_root *root1;
7629 dev_cache = RB_ROOT;
7630 cache_tree_init(&chunk_cache);
7631 block_group_tree_init(&block_group_cache);
7632 device_extent_tree_init(&dev_extent_cache);
7634 cache_tree_init(&extent_cache);
7635 cache_tree_init(&seen);
7636 cache_tree_init(&pending);
7637 cache_tree_init(&nodes);
7638 cache_tree_init(&reada);
7639 cache_tree_init(&corrupt_blocks);
7640 INIT_LIST_HEAD(&dropping_trees);
7641 INIT_LIST_HEAD(&normal_trees);
7644 trans = btrfs_start_transaction(root, 1);
7645 if (IS_ERR(trans)) {
7646 fprintf(stderr, "Error starting transaction\n");
7647 return PTR_ERR(trans);
7649 root->fs_info->fsck_extent_cache = &extent_cache;
7650 root->fs_info->free_extent_hook = free_extent_hook;
7651 root->fs_info->corrupt_blocks = &corrupt_blocks;
7655 bits = malloc(bits_nr * sizeof(struct block_info));
7662 root1 = root->fs_info->tree_root;
7663 level = btrfs_header_level(root1->node);
7664 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
7665 root1->node->start, level, 0,
7666 btrfs_level_size(root1, level), NULL);
7669 root1 = root->fs_info->chunk_root;
7670 level = btrfs_header_level(root1->node);
7671 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
7672 root1->node->start, level, 0,
7673 btrfs_level_size(root1, level), NULL);
7676 btrfs_init_path(&path);
7679 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
7680 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
7685 leaf = path.nodes[0];
7686 slot = path.slots[0];
7687 if (slot >= btrfs_header_nritems(path.nodes[0])) {
7688 ret = btrfs_next_leaf(root, &path);
7691 leaf = path.nodes[0];
7692 slot = path.slots[0];
7694 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
7695 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
7696 unsigned long offset;
7698 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
7699 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
7700 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
7701 level = btrfs_root_level(&ri);
7702 level_size = btrfs_level_size(root, level);
7703 ret = add_root_item_to_list(&normal_trees,
7705 btrfs_root_bytenr(&ri), level,
7706 0, level_size, NULL);
7710 level = btrfs_root_level(&ri);
7711 level_size = btrfs_level_size(root, level);
7712 objectid = found_key.objectid;
7713 btrfs_disk_key_to_cpu(&found_key,
7715 ret = add_root_item_to_list(&dropping_trees,
7717 btrfs_root_bytenr(&ri),
7718 level, ri.drop_level,
7719 level_size, &found_key);
7726 btrfs_release_path(&path);
7727 ret = deal_root_from_list(&normal_trees, trans, root,
7728 bits, bits_nr, &pending, &seen,
7729 &reada, &nodes, &extent_cache,
7730 &chunk_cache, &dev_cache, &block_group_cache,
7734 ret = deal_root_from_list(&dropping_trees, trans, root,
7735 bits, bits_nr, &pending, &seen,
7736 &reada, &nodes, &extent_cache,
7737 &chunk_cache, &dev_cache, &block_group_cache,
7742 ret = check_extent_refs(trans, root, &extent_cache);
7743 if (ret == -EAGAIN) {
7744 ret = btrfs_commit_transaction(trans, root);
7748 trans = btrfs_start_transaction(root, 1);
7749 if (IS_ERR(trans)) {
7750 ret = PTR_ERR(trans);
7754 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
7755 free_extent_cache_tree(&seen);
7756 free_extent_cache_tree(&pending);
7757 free_extent_cache_tree(&reada);
7758 free_extent_cache_tree(&nodes);
7759 free_chunk_cache_tree(&chunk_cache);
7760 free_block_group_tree(&block_group_cache);
7761 free_device_cache_tree(&dev_cache);
7762 free_device_extent_tree(&dev_extent_cache);
7763 free_extent_record_cache(root->fs_info, &extent_cache);
7767 err = check_chunks(&chunk_cache, &block_group_cache,
7768 &dev_extent_cache, NULL, NULL, NULL, 0);
7772 err = check_devices(&dev_cache, &dev_extent_cache);
7778 err = btrfs_commit_transaction(trans, root);
7783 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
7784 root->fs_info->fsck_extent_cache = NULL;
7785 root->fs_info->free_extent_hook = NULL;
7786 root->fs_info->corrupt_blocks = NULL;
7789 free_chunk_cache_tree(&chunk_cache);
7790 free_device_cache_tree(&dev_cache);
7791 free_block_group_tree(&block_group_cache);
7792 free_device_extent_tree(&dev_extent_cache);
7793 free_extent_cache_tree(&seen);
7794 free_extent_cache_tree(&pending);
7795 free_extent_cache_tree(&reada);
7796 free_extent_cache_tree(&nodes);
7800 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
7801 struct btrfs_root *root, int overwrite)
7803 struct extent_buffer *c;
7804 struct extent_buffer *old = root->node;
7807 struct btrfs_disk_key disk_key = {0,0,0};
7813 extent_buffer_get(c);
7816 c = btrfs_alloc_free_block(trans, root,
7817 btrfs_level_size(root, 0),
7818 root->root_key.objectid,
7819 &disk_key, level, 0, 0);
7822 extent_buffer_get(c);
7826 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
7827 btrfs_set_header_level(c, level);
7828 btrfs_set_header_bytenr(c, c->start);
7829 btrfs_set_header_generation(c, trans->transid);
7830 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
7831 btrfs_set_header_owner(c, root->root_key.objectid);
7833 write_extent_buffer(c, root->fs_info->fsid,
7834 btrfs_header_fsid(), BTRFS_FSID_SIZE);
7836 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
7837 btrfs_header_chunk_tree_uuid(c),
7840 btrfs_mark_buffer_dirty(c);
7842 * this case can happen in the following case:
7844 * 1.overwrite previous root.
7846 * 2.reinit reloc data root, this is because we skip pin
7847 * down reloc data tree before which means we can allocate
7848 * same block bytenr here.
7850 if (old->start == c->start) {
7851 btrfs_set_root_generation(&root->root_item,
7853 root->root_item.level = btrfs_header_level(root->node);
7854 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7855 &root->root_key, &root->root_item);
7857 free_extent_buffer(c);
7861 free_extent_buffer(old);
7863 add_root_to_dirty_list(root);
7867 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
7868 struct extent_buffer *eb, int tree_root)
7870 struct extent_buffer *tmp;
7871 struct btrfs_root_item *ri;
7872 struct btrfs_key key;
7875 int level = btrfs_header_level(eb);
7881 * If we have pinned this block before, don't pin it again.
7882 * This can not only avoid forever loop with broken filesystem
7883 * but also give us some speedups.
7885 if (test_range_bit(&fs_info->pinned_extents, eb->start,
7886 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
7889 btrfs_pin_extent(fs_info, eb->start, eb->len);
7891 leafsize = btrfs_super_leafsize(fs_info->super_copy);
7892 nritems = btrfs_header_nritems(eb);
7893 for (i = 0; i < nritems; i++) {
7895 btrfs_item_key_to_cpu(eb, &key, i);
7896 if (key.type != BTRFS_ROOT_ITEM_KEY)
7898 /* Skip the extent root and reloc roots */
7899 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
7900 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
7901 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
7903 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
7904 bytenr = btrfs_disk_root_bytenr(eb, ri);
7907 * If at any point we start needing the real root we
7908 * will have to build a stump root for the root we are
7909 * in, but for now this doesn't actually use the root so
7910 * just pass in extent_root.
7912 tmp = read_tree_block(fs_info->extent_root, bytenr,
7915 fprintf(stderr, "Error reading root block\n");
7918 ret = pin_down_tree_blocks(fs_info, tmp, 0);
7919 free_extent_buffer(tmp);
7923 bytenr = btrfs_node_blockptr(eb, i);
7925 /* If we aren't the tree root don't read the block */
7926 if (level == 1 && !tree_root) {
7927 btrfs_pin_extent(fs_info, bytenr, leafsize);
7931 tmp = read_tree_block(fs_info->extent_root, bytenr,
7934 fprintf(stderr, "Error reading tree block\n");
7937 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
7938 free_extent_buffer(tmp);
7947 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
7951 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
7955 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
7958 static int reset_block_groups(struct btrfs_fs_info *fs_info)
7960 struct btrfs_block_group_cache *cache;
7961 struct btrfs_path *path;
7962 struct extent_buffer *leaf;
7963 struct btrfs_chunk *chunk;
7964 struct btrfs_key key;
7968 path = btrfs_alloc_path();
7973 key.type = BTRFS_CHUNK_ITEM_KEY;
7976 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
7978 btrfs_free_path(path);
7983 * We do this in case the block groups were screwed up and had alloc
7984 * bits that aren't actually set on the chunks. This happens with
7985 * restored images every time and could happen in real life I guess.
7987 fs_info->avail_data_alloc_bits = 0;
7988 fs_info->avail_metadata_alloc_bits = 0;
7989 fs_info->avail_system_alloc_bits = 0;
7991 /* First we need to create the in-memory block groups */
7993 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7994 ret = btrfs_next_leaf(fs_info->chunk_root, path);
7996 btrfs_free_path(path);
8004 leaf = path->nodes[0];
8005 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8006 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
8011 chunk = btrfs_item_ptr(leaf, path->slots[0],
8012 struct btrfs_chunk);
8013 btrfs_add_block_group(fs_info, 0,
8014 btrfs_chunk_type(leaf, chunk),
8015 key.objectid, key.offset,
8016 btrfs_chunk_length(leaf, chunk));
8017 set_extent_dirty(&fs_info->free_space_cache, key.offset,
8018 key.offset + btrfs_chunk_length(leaf, chunk),
8024 cache = btrfs_lookup_first_block_group(fs_info, start);
8028 start = cache->key.objectid + cache->key.offset;
8031 btrfs_free_path(path);
8035 static int reset_balance(struct btrfs_trans_handle *trans,
8036 struct btrfs_fs_info *fs_info)
8038 struct btrfs_root *root = fs_info->tree_root;
8039 struct btrfs_path *path;
8040 struct extent_buffer *leaf;
8041 struct btrfs_key key;
8042 int del_slot, del_nr = 0;
8046 path = btrfs_alloc_path();
8050 key.objectid = BTRFS_BALANCE_OBJECTID;
8051 key.type = BTRFS_BALANCE_ITEM_KEY;
8054 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8059 goto reinit_data_reloc;
8064 ret = btrfs_del_item(trans, root, path);
8067 btrfs_release_path(path);
8069 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
8070 key.type = BTRFS_ROOT_ITEM_KEY;
8073 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8077 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
8082 ret = btrfs_del_items(trans, root, path,
8089 btrfs_release_path(path);
8092 ret = btrfs_search_slot(trans, root, &key, path,
8099 leaf = path->nodes[0];
8100 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8101 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
8103 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
8108 del_slot = path->slots[0];
8117 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
8121 btrfs_release_path(path);
8124 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
8125 key.type = BTRFS_ROOT_ITEM_KEY;
8126 key.offset = (u64)-1;
8127 root = btrfs_read_fs_root(fs_info, &key);
8129 fprintf(stderr, "Error reading data reloc tree\n");
8130 ret = PTR_ERR(root);
8133 record_root_in_trans(trans, root);
8134 ret = btrfs_fsck_reinit_root(trans, root, 0);
8137 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
8139 btrfs_free_path(path);
8143 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
8144 struct btrfs_fs_info *fs_info)
8150 * The only reason we don't do this is because right now we're just
8151 * walking the trees we find and pinning down their bytes, we don't look
8152 * at any of the leaves. In order to do mixed groups we'd have to check
8153 * the leaves of any fs roots and pin down the bytes for any file
8154 * extents we find. Not hard but why do it if we don't have to?
8156 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
8157 fprintf(stderr, "We don't support re-initing the extent tree "
8158 "for mixed block groups yet, please notify a btrfs "
8159 "developer you want to do this so they can add this "
8160 "functionality.\n");
8165 * first we need to walk all of the trees except the extent tree and pin
8166 * down the bytes that are in use so we don't overwrite any existing
8169 ret = pin_metadata_blocks(fs_info);
8171 fprintf(stderr, "error pinning down used bytes\n");
8176 * Need to drop all the block groups since we're going to recreate all
8179 btrfs_free_block_groups(fs_info);
8180 ret = reset_block_groups(fs_info);
8182 fprintf(stderr, "error resetting the block groups\n");
8186 /* Ok we can allocate now, reinit the extent root */
8187 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
8189 fprintf(stderr, "extent root initialization failed\n");
8191 * When the transaction code is updated we should end the
8192 * transaction, but for now progs only knows about commit so
8193 * just return an error.
8199 * Now we have all the in-memory block groups setup so we can make
8200 * allocations properly, and the metadata we care about is safe since we
8201 * pinned all of it above.
8204 struct btrfs_block_group_cache *cache;
8206 cache = btrfs_lookup_first_block_group(fs_info, start);
8209 start = cache->key.objectid + cache->key.offset;
8210 ret = btrfs_insert_item(trans, fs_info->extent_root,
8211 &cache->key, &cache->item,
8212 sizeof(cache->item));
8214 fprintf(stderr, "Error adding block group\n");
8217 btrfs_extent_post_op(trans, fs_info->extent_root);
8220 ret = reset_balance(trans, fs_info);
8222 fprintf(stderr, "error reseting the pending balance\n");
8227 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
8229 struct btrfs_path *path;
8230 struct btrfs_trans_handle *trans;
8231 struct btrfs_key key;
8234 printf("Recowing metadata block %llu\n", eb->start);
8235 key.objectid = btrfs_header_owner(eb);
8236 key.type = BTRFS_ROOT_ITEM_KEY;
8237 key.offset = (u64)-1;
8239 root = btrfs_read_fs_root(root->fs_info, &key);
8241 fprintf(stderr, "Couldn't find owner root %llu\n",
8243 return PTR_ERR(root);
8246 path = btrfs_alloc_path();
8250 trans = btrfs_start_transaction(root, 1);
8251 if (IS_ERR(trans)) {
8252 btrfs_free_path(path);
8253 return PTR_ERR(trans);
8256 path->lowest_level = btrfs_header_level(eb);
8257 if (path->lowest_level)
8258 btrfs_node_key_to_cpu(eb, &key, 0);
8260 btrfs_item_key_to_cpu(eb, &key, 0);
8262 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
8263 btrfs_commit_transaction(trans, root);
8264 btrfs_free_path(path);
8268 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
8270 struct btrfs_path *path;
8271 struct btrfs_trans_handle *trans;
8272 struct btrfs_key key;
8275 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
8276 bad->key.type, bad->key.offset);
8277 key.objectid = bad->root_id;
8278 key.type = BTRFS_ROOT_ITEM_KEY;
8279 key.offset = (u64)-1;
8281 root = btrfs_read_fs_root(root->fs_info, &key);
8283 fprintf(stderr, "Couldn't find owner root %llu\n",
8285 return PTR_ERR(root);
8288 path = btrfs_alloc_path();
8292 trans = btrfs_start_transaction(root, 1);
8293 if (IS_ERR(trans)) {
8294 btrfs_free_path(path);
8295 return PTR_ERR(trans);
8298 ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
8304 ret = btrfs_del_item(trans, root, path);
8306 btrfs_commit_transaction(trans, root);
8307 btrfs_free_path(path);
8311 static int zero_log_tree(struct btrfs_root *root)
8313 struct btrfs_trans_handle *trans;
8316 trans = btrfs_start_transaction(root, 1);
8317 if (IS_ERR(trans)) {
8318 ret = PTR_ERR(trans);
8321 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
8322 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
8323 ret = btrfs_commit_transaction(trans, root);
8327 static int populate_csum(struct btrfs_trans_handle *trans,
8328 struct btrfs_root *csum_root, char *buf, u64 start,
8335 while (offset < len) {
8336 sectorsize = csum_root->sectorsize;
8337 ret = read_extent_data(csum_root, buf, start + offset,
8341 ret = btrfs_csum_file_block(trans, csum_root, start + len,
8342 start + offset, buf, sectorsize);
8345 offset += sectorsize;
8350 static int fill_csum_tree(struct btrfs_trans_handle *trans,
8351 struct btrfs_root *csum_root)
8353 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
8354 struct btrfs_path *path;
8355 struct btrfs_extent_item *ei;
8356 struct extent_buffer *leaf;
8358 struct btrfs_key key;
8361 path = btrfs_alloc_path();
8366 key.type = BTRFS_EXTENT_ITEM_KEY;
8369 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
8371 btrfs_free_path(path);
8375 buf = malloc(csum_root->sectorsize);
8377 btrfs_free_path(path);
8382 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
8383 ret = btrfs_next_leaf(extent_root, path);
8391 leaf = path->nodes[0];
8393 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8394 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
8399 ei = btrfs_item_ptr(leaf, path->slots[0],
8400 struct btrfs_extent_item);
8401 if (!(btrfs_extent_flags(leaf, ei) &
8402 BTRFS_EXTENT_FLAG_DATA)) {
8407 ret = populate_csum(trans, csum_root, buf, key.objectid,
8414 btrfs_free_path(path);
8419 struct root_item_info {
8420 /* level of the root */
8422 /* number of nodes at this level, must be 1 for a root */
8426 struct cache_extent cache_extent;
8429 static struct cache_tree *roots_info_cache = NULL;
8431 static void free_roots_info_cache(void)
8433 if (!roots_info_cache)
8436 while (!cache_tree_empty(roots_info_cache)) {
8437 struct cache_extent *entry;
8438 struct root_item_info *rii;
8440 entry = first_cache_extent(roots_info_cache);
8443 remove_cache_extent(roots_info_cache, entry);
8444 rii = container_of(entry, struct root_item_info, cache_extent);
8448 free(roots_info_cache);
8449 roots_info_cache = NULL;
8452 static int build_roots_info_cache(struct btrfs_fs_info *info)
8455 struct btrfs_key key;
8456 struct extent_buffer *leaf;
8457 struct btrfs_path *path;
8459 if (!roots_info_cache) {
8460 roots_info_cache = malloc(sizeof(*roots_info_cache));
8461 if (!roots_info_cache)
8463 cache_tree_init(roots_info_cache);
8466 path = btrfs_alloc_path();
8471 key.type = BTRFS_EXTENT_ITEM_KEY;
8474 ret = btrfs_search_slot(NULL, info->extent_root, &key, path, 0, 0);
8477 leaf = path->nodes[0];
8480 struct btrfs_key found_key;
8481 struct btrfs_extent_item *ei;
8482 struct btrfs_extent_inline_ref *iref;
8483 int slot = path->slots[0];
8488 struct cache_extent *entry;
8489 struct root_item_info *rii;
8491 if (slot >= btrfs_header_nritems(leaf)) {
8492 ret = btrfs_next_leaf(info->extent_root, path);
8499 leaf = path->nodes[0];
8500 slot = path->slots[0];
8503 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8505 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
8506 found_key.type != BTRFS_METADATA_ITEM_KEY)
8509 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
8510 flags = btrfs_extent_flags(leaf, ei);
8512 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
8513 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
8516 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
8517 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
8518 level = found_key.offset;
8520 struct btrfs_tree_block_info *info;
8522 info = (struct btrfs_tree_block_info *)(ei + 1);
8523 iref = (struct btrfs_extent_inline_ref *)(info + 1);
8524 level = btrfs_tree_block_level(leaf, info);
8528 * For a root extent, it must be of the following type and the
8529 * first (and only one) iref in the item.
8531 type = btrfs_extent_inline_ref_type(leaf, iref);
8532 if (type != BTRFS_TREE_BLOCK_REF_KEY)
8535 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
8536 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
8538 rii = malloc(sizeof(struct root_item_info));
8543 rii->cache_extent.start = root_id;
8544 rii->cache_extent.size = 1;
8545 rii->level = (u8)-1;
8546 entry = &rii->cache_extent;
8547 ret = insert_cache_extent(roots_info_cache, entry);
8550 rii = container_of(entry, struct root_item_info,
8554 ASSERT(rii->cache_extent.start == root_id);
8555 ASSERT(rii->cache_extent.size == 1);
8557 if (level > rii->level || rii->level == (u8)-1) {
8559 rii->bytenr = found_key.objectid;
8560 rii->gen = btrfs_extent_generation(leaf, ei);
8561 rii->node_count = 1;
8562 } else if (level == rii->level) {
8570 btrfs_free_path(path);
8575 static int maybe_repair_root_item(struct btrfs_fs_info *info,
8576 struct btrfs_path *path,
8577 const struct btrfs_key *root_key,
8578 const int read_only_mode)
8580 const u64 root_id = root_key->objectid;
8581 struct cache_extent *entry;
8582 struct root_item_info *rii;
8583 struct btrfs_root_item ri;
8584 unsigned long offset;
8586 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
8589 "Error: could not find extent items for root %llu\n",
8590 root_key->objectid);
8594 rii = container_of(entry, struct root_item_info, cache_extent);
8595 ASSERT(rii->cache_extent.start == root_id);
8596 ASSERT(rii->cache_extent.size == 1);
8598 if (rii->node_count != 1) {
8600 "Error: could not find btree root extent for root %llu\n",
8605 offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
8606 read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
8608 if (btrfs_root_bytenr(&ri) != rii->bytenr ||
8609 btrfs_root_level(&ri) != rii->level ||
8610 btrfs_root_generation(&ri) != rii->gen) {
8613 * If we're in repair mode but our caller told us to not update
8614 * the root item, i.e. just check if it needs to be updated, don't
8615 * print this message, since the caller will call us again shortly
8616 * for the same root item without read only mode (the caller will
8617 * open a transaction first).
8619 if (!(read_only_mode && repair))
8621 "%sroot item for root %llu,"
8622 " current bytenr %llu, current gen %llu, current level %u,"
8623 " new bytenr %llu, new gen %llu, new level %u\n",
8624 (read_only_mode ? "" : "fixing "),
8626 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
8627 btrfs_root_level(&ri),
8628 rii->bytenr, rii->gen, rii->level);
8630 if (btrfs_root_generation(&ri) > rii->gen) {
8632 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
8633 root_id, btrfs_root_generation(&ri), rii->gen);
8637 if (!read_only_mode) {
8638 btrfs_set_root_bytenr(&ri, rii->bytenr);
8639 btrfs_set_root_level(&ri, rii->level);
8640 btrfs_set_root_generation(&ri, rii->gen);
8641 write_extent_buffer(path->nodes[0], &ri,
8642 offset, sizeof(ri));
8652 * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
8653 * caused read-only snapshots to be corrupted if they were created at a moment
8654 * when the source subvolume/snapshot had orphan items. The issue was that the
8655 * on-disk root items became incorrect, referring to the pre orphan cleanup root
8656 * node instead of the post orphan cleanup root node.
8657 * So this function, and its callees, just detects and fixes those cases. Even
8658 * though the regression was for read-only snapshots, this function applies to
8659 * any snapshot/subvolume root.
8660 * This must be run before any other repair code - not doing it so, makes other
8661 * repair code delete or modify backrefs in the extent tree for example, which
8662 * will result in an inconsistent fs after repairing the root items.
8664 static int repair_root_items(struct btrfs_fs_info *info)
8666 struct btrfs_path *path = NULL;
8667 struct btrfs_key key;
8668 struct extent_buffer *leaf;
8669 struct btrfs_trans_handle *trans = NULL;
8674 ret = build_roots_info_cache(info);
8678 path = btrfs_alloc_path();
8684 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
8685 key.type = BTRFS_ROOT_ITEM_KEY;
8690 * Avoid opening and committing transactions if a leaf doesn't have
8691 * any root items that need to be fixed, so that we avoid rotating
8692 * backup roots unnecessarily.
8695 trans = btrfs_start_transaction(info->tree_root, 1);
8696 if (IS_ERR(trans)) {
8697 ret = PTR_ERR(trans);
8702 ret = btrfs_search_slot(trans, info->tree_root, &key, path,
8706 leaf = path->nodes[0];
8709 struct btrfs_key found_key;
8711 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
8712 int no_more_keys = find_next_key(path, &key);
8714 btrfs_release_path(path);
8716 ret = btrfs_commit_transaction(trans,
8728 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8730 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
8733 ret = maybe_repair_root_item(info, path, &found_key,
8738 if (!trans && repair) {
8741 btrfs_release_path(path);
8751 free_roots_info_cache();
8753 btrfs_free_path(path);
8760 const char * const cmd_check_usage[] = {
8761 "btrfs check [options] <device>",
8762 "Check an unmounted btrfs filesystem.",
8764 "-s|--super <superblock> use this superblock copy",
8765 "-b|--backup use the backup root copy",
8766 "--repair try to repair the filesystem",
8767 "--init-csum-tree create a new CRC tree",
8768 "--init-extent-tree create a new extent tree",
8769 "--check-data-csum verify checkums of data blocks",
8770 "--qgroup-report print a report on qgroup consistency",
8771 "--subvol-extents <subvolid> print subvolume extents and sharing state",
8772 "--tree-root <bytenr> use the given bytenr for the tree root",
8776 int cmd_check(int argc, char **argv)
8778 struct cache_tree root_cache;
8779 struct btrfs_root *root;
8780 struct btrfs_fs_info *info;
8783 u64 tree_root_bytenr = 0;
8784 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
8787 int init_csum_tree = 0;
8789 int qgroup_report = 0;
8790 enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_EXCLUSIVE;
8794 int option_index = 0;
8795 enum { OPT_REPAIR = 257, OPT_INIT_CSUM, OPT_INIT_EXTENT,
8796 OPT_CHECK_CSUM, OPT_READONLY };
8797 static const struct option long_options[] = {
8798 { "super", 1, NULL, 's' },
8799 { "repair", 0, NULL, OPT_REPAIR },
8800 { "readonly", 0, NULL, OPT_READONLY },
8801 { "init-csum-tree", 0, NULL, OPT_INIT_CSUM },
8802 { "init-extent-tree", 0, NULL, OPT_INIT_EXTENT },
8803 { "check-data-csum", 0, NULL, OPT_CHECK_CSUM },
8804 { "backup", 0, NULL, 'b' },
8805 { "subvol-extents", 1, NULL, 'E' },
8806 { "qgroup-report", 0, NULL, 'Q' },
8807 { "tree-root", 1, NULL, 'r' },
8811 c = getopt_long(argc, argv, "as:br:", long_options,
8816 case 'a': /* ignored */ break;
8818 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
8821 num = arg_strtou64(optarg);
8822 if (num >= BTRFS_SUPER_MIRROR_MAX) {
8824 "ERROR: super mirror should be less than: %d\n",
8825 BTRFS_SUPER_MIRROR_MAX);
8828 bytenr = btrfs_sb_offset(((int)num));
8829 printf("using SB copy %llu, bytenr %llu\n", num,
8830 (unsigned long long)bytenr);
8836 subvolid = arg_strtou64(optarg);
8839 tree_root_bytenr = arg_strtou64(optarg);
8843 usage(cmd_check_usage);
8845 printf("enabling repair mode\n");
8847 ctree_flags |= OPEN_CTREE_WRITES;
8853 printf("Creating a new CRC tree\n");
8856 ctree_flags |= OPEN_CTREE_WRITES;
8858 case OPT_INIT_EXTENT:
8859 init_extent_tree = 1;
8860 ctree_flags |= (OPEN_CTREE_WRITES |
8861 OPEN_CTREE_NO_BLOCK_GROUPS);
8864 case OPT_CHECK_CSUM:
8865 check_data_csum = 1;
8869 argc = argc - optind;
8871 if (check_argc_exact(argc, 1))
8872 usage(cmd_check_usage);
8874 /* This check is the only reason for --readonly to exist */
8875 if (readonly && repair) {
8876 fprintf(stderr, "Repair options are not compatible with --readonly\n");
8881 cache_tree_init(&root_cache);
8883 if((ret = check_mounted(argv[optind])) < 0) {
8884 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
8887 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
8892 /* only allow partial opening under repair mode */
8894 ctree_flags |= OPEN_CTREE_PARTIAL;
8896 info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
8899 fprintf(stderr, "Couldn't open file system\n");
8904 root = info->fs_root;
8907 * repair mode will force us to commit transaction which
8908 * will make us fail to load log tree when mounting.
8910 if (repair && btrfs_super_log_root(info->super_copy)) {
8911 ret = ask_user("repair mode will force to clear out log tree, Are you sure?");
8916 ret = zero_log_tree(root);
8918 fprintf(stderr, "fail to zero log tree\n");
8923 uuid_unparse(info->super_copy->fsid, uuidbuf);
8924 if (qgroup_report) {
8925 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
8927 ret = qgroup_verify_all(info);
8929 print_qgroup_report(1);
8933 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
8934 subvolid, argv[optind], uuidbuf);
8935 ret = print_extent_state(info, subvolid);
8938 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
8940 if (!extent_buffer_uptodate(info->tree_root->node) ||
8941 !extent_buffer_uptodate(info->dev_root->node) ||
8942 !extent_buffer_uptodate(info->chunk_root->node)) {
8943 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
8948 if (init_extent_tree || init_csum_tree) {
8949 struct btrfs_trans_handle *trans;
8951 trans = btrfs_start_transaction(info->extent_root, 0);
8952 if (IS_ERR(trans)) {
8953 fprintf(stderr, "Error starting transaction\n");
8954 ret = PTR_ERR(trans);
8958 if (init_extent_tree) {
8959 printf("Creating a new extent tree\n");
8960 ret = reinit_extent_tree(trans, info);
8965 if (init_csum_tree) {
8966 fprintf(stderr, "Reinit crc root\n");
8967 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
8969 fprintf(stderr, "crc root initialization failed\n");
8974 ret = fill_csum_tree(trans, info->csum_root);
8976 fprintf(stderr, "crc refilling failed\n");
8981 * Ok now we commit and run the normal fsck, which will add
8982 * extent entries for all of the items it finds.
8984 ret = btrfs_commit_transaction(trans, info->extent_root);
8988 if (!extent_buffer_uptodate(info->extent_root->node)) {
8989 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
8993 if (!extent_buffer_uptodate(info->csum_root->node)) {
8994 fprintf(stderr, "Checksum root corrupted, rerun with --init-csum-tree option\n");
8999 fprintf(stderr, "checking extents\n");
9000 ret = check_chunks_and_extents(root);
9002 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
9004 ret = repair_root_items(info);
9008 fprintf(stderr, "Fixed %d roots.\n", ret);
9010 } else if (ret > 0) {
9012 "Found %d roots with an outdated root item.\n",
9015 "Please run a filesystem check with the option --repair to fix them.\n");
9020 fprintf(stderr, "checking free space cache\n");
9021 ret = check_space_cache(root);
9026 * We used to have to have these hole extents in between our real
9027 * extents so if we don't have this flag set we need to make sure there
9028 * are no gaps in the file extents for inodes, otherwise we can just
9029 * ignore it when this happens.
9031 no_holes = btrfs_fs_incompat(root->fs_info,
9032 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
9033 fprintf(stderr, "checking fs roots\n");
9034 ret = check_fs_roots(root, &root_cache);
9038 fprintf(stderr, "checking csums\n");
9039 ret = check_csums(root);
9043 fprintf(stderr, "checking root refs\n");
9044 ret = check_root_refs(root, &root_cache);
9048 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
9049 struct extent_buffer *eb;
9051 eb = list_first_entry(&root->fs_info->recow_ebs,
9052 struct extent_buffer, recow);
9053 list_del_init(&eb->recow);
9054 ret = recow_extent_buffer(root, eb);
9059 while (!list_empty(&delete_items)) {
9060 struct bad_item *bad;
9062 bad = list_first_entry(&delete_items, struct bad_item, list);
9063 list_del_init(&bad->list);
9065 ret = delete_bad_item(root, bad);
9069 if (info->quota_enabled) {
9071 fprintf(stderr, "checking quota groups\n");
9072 err = qgroup_verify_all(info);
9077 if (!list_empty(&root->fs_info->recow_ebs)) {
9078 fprintf(stderr, "Transid errors in file system\n");
9082 print_qgroup_report(0);
9083 if (found_old_backref) { /*
9084 * there was a disk format change when mixed
9085 * backref was in testing tree. The old format
9086 * existed about one week.
9088 printf("\n * Found old mixed backref format. "
9089 "The old format is not supported! *"
9090 "\n * Please mount the FS in readonly mode, "
9091 "backup data and re-format the FS. *\n\n");
9094 printf("found %llu bytes used err is %d\n",
9095 (unsigned long long)bytes_used, ret);
9096 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
9097 printf("total tree bytes: %llu\n",
9098 (unsigned long long)total_btree_bytes);
9099 printf("total fs tree bytes: %llu\n",
9100 (unsigned long long)total_fs_tree_bytes);
9101 printf("total extent tree bytes: %llu\n",
9102 (unsigned long long)total_extent_tree_bytes);
9103 printf("btree space waste bytes: %llu\n",
9104 (unsigned long long)btree_space_waste);
9105 printf("file data blocks allocated: %llu\n referenced %llu\n",
9106 (unsigned long long)data_bytes_allocated,
9107 (unsigned long long)data_bytes_referenced);
9108 printf("%s\n", PACKAGE_STRING);
9110 free_root_recs_tree(&root_cache);