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 "task-utils.h"
34 #include "transaction.h"
37 #include "free-space-cache.h"
38 #include "free-space-tree.h"
40 #include "qgroup-verify.h"
41 #include "rbtree-utils.h"
43 #include "kernel-shared/ulist.h"
46 #include "check/original.h"
52 TASK_NOTHING, /* have to be the last element */
57 enum task_position tp;
59 struct task_info *info;
62 static u64 bytes_used = 0;
63 static u64 total_csum_bytes = 0;
64 static u64 total_btree_bytes = 0;
65 static u64 total_fs_tree_bytes = 0;
66 static u64 total_extent_tree_bytes = 0;
67 static u64 btree_space_waste = 0;
68 static u64 data_bytes_allocated = 0;
69 static u64 data_bytes_referenced = 0;
70 static LIST_HEAD(duplicate_extents);
71 static LIST_HEAD(delete_items);
72 static int no_holes = 0;
73 static int init_extent_tree = 0;
74 static int check_data_csum = 0;
75 static struct btrfs_fs_info *global_info;
76 static struct task_ctx ctx = { 0 };
77 static struct cache_tree *roots_info_cache = NULL;
79 enum btrfs_check_mode {
83 CHECK_MODE_DEFAULT = CHECK_MODE_ORIGINAL
86 static enum btrfs_check_mode check_mode = CHECK_MODE_DEFAULT;
88 #define ROOT_DIR_ERROR (1<<1) /* bad ROOT_DIR */
89 #define DIR_ITEM_MISSING (1<<2) /* DIR_ITEM not found */
90 #define DIR_ITEM_MISMATCH (1<<3) /* DIR_ITEM found but not match */
91 #define INODE_REF_MISSING (1<<4) /* INODE_REF/INODE_EXTREF not found */
92 #define INODE_ITEM_MISSING (1<<5) /* INODE_ITEM not found */
93 #define INODE_ITEM_MISMATCH (1<<6) /* INODE_ITEM found but not match */
94 #define FILE_EXTENT_ERROR (1<<7) /* bad FILE_EXTENT */
95 #define ODD_CSUM_ITEM (1<<8) /* CSUM_ITEM error */
96 #define CSUM_ITEM_MISSING (1<<9) /* CSUM_ITEM not found */
97 #define LINK_COUNT_ERROR (1<<10) /* INODE_ITEM nlink count error */
98 #define NBYTES_ERROR (1<<11) /* INODE_ITEM nbytes count error */
99 #define ISIZE_ERROR (1<<12) /* INODE_ITEM size count error */
100 #define ORPHAN_ITEM (1<<13) /* INODE_ITEM no reference */
101 #define NO_INODE_ITEM (1<<14) /* no inode_item */
102 #define LAST_ITEM (1<<15) /* Complete this tree traversal */
103 #define ROOT_REF_MISSING (1<<16) /* ROOT_REF not found */
104 #define ROOT_REF_MISMATCH (1<<17) /* ROOT_REF found but not match */
105 #define DIR_INDEX_MISSING (1<<18) /* INODE_INDEX not found */
106 #define DIR_INDEX_MISMATCH (1<<19) /* INODE_INDEX found but not match */
107 #define DIR_COUNT_AGAIN (1<<20) /* DIR isize should be recalculated */
108 #define BG_ACCOUNTING_ERROR (1<<21) /* Block group accounting error */
110 static int compare_data_backref(struct rb_node *node1, struct rb_node *node2)
112 struct extent_backref *ext1 = rb_node_to_extent_backref(node1);
113 struct extent_backref *ext2 = rb_node_to_extent_backref(node2);
114 struct data_backref *back1 = to_data_backref(ext1);
115 struct data_backref *back2 = to_data_backref(ext2);
117 WARN_ON(!ext1->is_data);
118 WARN_ON(!ext2->is_data);
120 /* parent and root are a union, so this covers both */
121 if (back1->parent > back2->parent)
123 if (back1->parent < back2->parent)
126 /* This is a full backref and the parents match. */
127 if (back1->node.full_backref)
130 if (back1->owner > back2->owner)
132 if (back1->owner < back2->owner)
135 if (back1->offset > back2->offset)
137 if (back1->offset < back2->offset)
140 if (back1->found_ref && back2->found_ref) {
141 if (back1->disk_bytenr > back2->disk_bytenr)
143 if (back1->disk_bytenr < back2->disk_bytenr)
146 if (back1->bytes > back2->bytes)
148 if (back1->bytes < back2->bytes)
155 static int compare_tree_backref(struct rb_node *node1, struct rb_node *node2)
157 struct extent_backref *ext1 = rb_node_to_extent_backref(node1);
158 struct extent_backref *ext2 = rb_node_to_extent_backref(node2);
159 struct tree_backref *back1 = to_tree_backref(ext1);
160 struct tree_backref *back2 = to_tree_backref(ext2);
162 WARN_ON(ext1->is_data);
163 WARN_ON(ext2->is_data);
165 /* parent and root are a union, so this covers both */
166 if (back1->parent > back2->parent)
168 if (back1->parent < back2->parent)
174 static int compare_extent_backref(struct rb_node *node1, struct rb_node *node2)
176 struct extent_backref *ext1 = rb_node_to_extent_backref(node1);
177 struct extent_backref *ext2 = rb_node_to_extent_backref(node2);
179 if (ext1->is_data > ext2->is_data)
182 if (ext1->is_data < ext2->is_data)
185 if (ext1->full_backref > ext2->full_backref)
187 if (ext1->full_backref < ext2->full_backref)
191 return compare_data_backref(node1, node2);
193 return compare_tree_backref(node1, node2);
197 * Error bit for low memory mode check.
199 * Currently no caller cares about it yet. Just internal use for error
202 #define BACKREF_MISSING (1 << 0) /* Backref missing in extent tree */
203 #define BACKREF_MISMATCH (1 << 1) /* Backref exists but does not match */
204 #define BYTES_UNALIGNED (1 << 2) /* Some bytes are not aligned */
205 #define REFERENCER_MISSING (1 << 3) /* Referencer not found */
206 #define REFERENCER_MISMATCH (1 << 4) /* Referenceer found but does not match */
207 #define CROSSING_STRIPE_BOUNDARY (1 << 4) /* For kernel scrub workaround */
208 #define ITEM_SIZE_MISMATCH (1 << 5) /* Bad item size */
209 #define UNKNOWN_TYPE (1 << 6) /* Unknown type */
210 #define ACCOUNTING_MISMATCH (1 << 7) /* Used space accounting error */
211 #define CHUNK_TYPE_MISMATCH (1 << 8)
213 static void *print_status_check(void *p)
215 struct task_ctx *priv = p;
216 const char work_indicator[] = { '.', 'o', 'O', 'o' };
218 static char *task_position_string[] = {
220 "checking free space cache",
224 task_period_start(priv->info, 1000 /* 1s */);
226 if (priv->tp == TASK_NOTHING)
230 printf("%s [%c]\r", task_position_string[priv->tp],
231 work_indicator[count % 4]);
234 task_period_wait(priv->info);
239 static int print_status_return(void *p)
247 static enum btrfs_check_mode parse_check_mode(const char *str)
249 if (strcmp(str, "lowmem") == 0)
250 return CHECK_MODE_LOWMEM;
251 if (strcmp(str, "orig") == 0)
252 return CHECK_MODE_ORIGINAL;
253 if (strcmp(str, "original") == 0)
254 return CHECK_MODE_ORIGINAL;
256 return CHECK_MODE_UNKNOWN;
259 /* Compatible function to allow reuse of old codes */
260 static u64 first_extent_gap(struct rb_root *holes)
262 struct file_extent_hole *hole;
264 if (RB_EMPTY_ROOT(holes))
267 hole = rb_entry(rb_first(holes), struct file_extent_hole, node);
271 static int compare_hole(struct rb_node *node1, struct rb_node *node2)
273 struct file_extent_hole *hole1;
274 struct file_extent_hole *hole2;
276 hole1 = rb_entry(node1, struct file_extent_hole, node);
277 hole2 = rb_entry(node2, struct file_extent_hole, node);
279 if (hole1->start > hole2->start)
281 if (hole1->start < hole2->start)
283 /* Now hole1->start == hole2->start */
284 if (hole1->len >= hole2->len)
286 * Hole 1 will be merge center
287 * Same hole will be merged later
290 /* Hole 2 will be merge center */
295 * Add a hole to the record
297 * This will do hole merge for copy_file_extent_holes(),
298 * which will ensure there won't be continuous holes.
300 static int add_file_extent_hole(struct rb_root *holes,
303 struct file_extent_hole *hole;
304 struct file_extent_hole *prev = NULL;
305 struct file_extent_hole *next = NULL;
307 hole = malloc(sizeof(*hole));
312 /* Since compare will not return 0, no -EEXIST will happen */
313 rb_insert(holes, &hole->node, compare_hole);
315 /* simple merge with previous hole */
316 if (rb_prev(&hole->node))
317 prev = rb_entry(rb_prev(&hole->node), struct file_extent_hole,
319 if (prev && prev->start + prev->len >= hole->start) {
320 hole->len = hole->start + hole->len - prev->start;
321 hole->start = prev->start;
322 rb_erase(&prev->node, holes);
327 /* iterate merge with next holes */
329 if (!rb_next(&hole->node))
331 next = rb_entry(rb_next(&hole->node), struct file_extent_hole,
333 if (hole->start + hole->len >= next->start) {
334 if (hole->start + hole->len <= next->start + next->len)
335 hole->len = next->start + next->len -
337 rb_erase(&next->node, holes);
346 static int compare_hole_range(struct rb_node *node, void *data)
348 struct file_extent_hole *hole;
351 hole = (struct file_extent_hole *)data;
354 hole = rb_entry(node, struct file_extent_hole, node);
355 if (start < hole->start)
357 if (start >= hole->start && start < hole->start + hole->len)
363 * Delete a hole in the record
365 * This will do the hole split and is much restrict than add.
367 static int del_file_extent_hole(struct rb_root *holes,
370 struct file_extent_hole *hole;
371 struct file_extent_hole tmp;
376 struct rb_node *node;
383 node = rb_search(holes, &tmp, compare_hole_range, NULL);
386 hole = rb_entry(node, struct file_extent_hole, node);
387 if (start + len > hole->start + hole->len)
391 * Now there will be no overlap, delete the hole and re-add the
392 * split(s) if they exists.
394 if (start > hole->start) {
395 prev_start = hole->start;
396 prev_len = start - hole->start;
399 if (hole->start + hole->len > start + len) {
400 next_start = start + len;
401 next_len = hole->start + hole->len - start - len;
404 rb_erase(node, holes);
407 ret = add_file_extent_hole(holes, prev_start, prev_len);
412 ret = add_file_extent_hole(holes, next_start, next_len);
419 static int copy_file_extent_holes(struct rb_root *dst,
422 struct file_extent_hole *hole;
423 struct rb_node *node;
426 node = rb_first(src);
428 hole = rb_entry(node, struct file_extent_hole, node);
429 ret = add_file_extent_hole(dst, hole->start, hole->len);
432 node = rb_next(node);
437 static void free_file_extent_holes(struct rb_root *holes)
439 struct rb_node *node;
440 struct file_extent_hole *hole;
442 node = rb_first(holes);
444 hole = rb_entry(node, struct file_extent_hole, node);
445 rb_erase(node, holes);
447 node = rb_first(holes);
451 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
453 static void record_root_in_trans(struct btrfs_trans_handle *trans,
454 struct btrfs_root *root)
456 if (root->last_trans != trans->transid) {
457 root->track_dirty = 1;
458 root->last_trans = trans->transid;
459 root->commit_root = root->node;
460 extent_buffer_get(root->node);
464 static u8 imode_to_type(u32 imode)
467 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
468 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
469 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
470 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
471 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
472 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
473 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
474 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
477 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
481 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
483 struct device_record *rec1;
484 struct device_record *rec2;
486 rec1 = rb_entry(node1, struct device_record, node);
487 rec2 = rb_entry(node2, struct device_record, node);
488 if (rec1->devid > rec2->devid)
490 else if (rec1->devid < rec2->devid)
496 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
498 struct inode_record *rec;
499 struct inode_backref *backref;
500 struct inode_backref *orig;
501 struct inode_backref *tmp;
502 struct orphan_data_extent *src_orphan;
503 struct orphan_data_extent *dst_orphan;
508 rec = malloc(sizeof(*rec));
510 return ERR_PTR(-ENOMEM);
511 memcpy(rec, orig_rec, sizeof(*rec));
513 INIT_LIST_HEAD(&rec->backrefs);
514 INIT_LIST_HEAD(&rec->orphan_extents);
515 rec->holes = RB_ROOT;
517 list_for_each_entry(orig, &orig_rec->backrefs, list) {
518 size = sizeof(*orig) + orig->namelen + 1;
519 backref = malloc(size);
524 memcpy(backref, orig, size);
525 list_add_tail(&backref->list, &rec->backrefs);
527 list_for_each_entry(src_orphan, &orig_rec->orphan_extents, list) {
528 dst_orphan = malloc(sizeof(*dst_orphan));
533 memcpy(dst_orphan, src_orphan, sizeof(*src_orphan));
534 list_add_tail(&dst_orphan->list, &rec->orphan_extents);
536 ret = copy_file_extent_holes(&rec->holes, &orig_rec->holes);
543 rb = rb_first(&rec->holes);
545 struct file_extent_hole *hole;
547 hole = rb_entry(rb, struct file_extent_hole, node);
553 if (!list_empty(&rec->backrefs))
554 list_for_each_entry_safe(orig, tmp, &rec->backrefs, list) {
555 list_del(&orig->list);
559 if (!list_empty(&rec->orphan_extents))
560 list_for_each_entry_safe(orig, tmp, &rec->orphan_extents, list) {
561 list_del(&orig->list);
570 static void print_orphan_data_extents(struct list_head *orphan_extents,
573 struct orphan_data_extent *orphan;
575 if (list_empty(orphan_extents))
577 printf("The following data extent is lost in tree %llu:\n",
579 list_for_each_entry(orphan, orphan_extents, list) {
580 printf("\tinode: %llu, offset:%llu, disk_bytenr: %llu, disk_len: %llu\n",
581 orphan->objectid, orphan->offset, orphan->disk_bytenr,
586 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
588 u64 root_objectid = root->root_key.objectid;
589 int errors = rec->errors;
593 /* reloc root errors, we print its corresponding fs root objectid*/
594 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
595 root_objectid = root->root_key.offset;
596 fprintf(stderr, "reloc");
598 fprintf(stderr, "root %llu inode %llu errors %x",
599 (unsigned long long) root_objectid,
600 (unsigned long long) rec->ino, rec->errors);
602 if (errors & I_ERR_NO_INODE_ITEM)
603 fprintf(stderr, ", no inode item");
604 if (errors & I_ERR_NO_ORPHAN_ITEM)
605 fprintf(stderr, ", no orphan item");
606 if (errors & I_ERR_DUP_INODE_ITEM)
607 fprintf(stderr, ", dup inode item");
608 if (errors & I_ERR_DUP_DIR_INDEX)
609 fprintf(stderr, ", dup dir index");
610 if (errors & I_ERR_ODD_DIR_ITEM)
611 fprintf(stderr, ", odd dir item");
612 if (errors & I_ERR_ODD_FILE_EXTENT)
613 fprintf(stderr, ", odd file extent");
614 if (errors & I_ERR_BAD_FILE_EXTENT)
615 fprintf(stderr, ", bad file extent");
616 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
617 fprintf(stderr, ", file extent overlap");
618 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
619 fprintf(stderr, ", file extent discount");
620 if (errors & I_ERR_DIR_ISIZE_WRONG)
621 fprintf(stderr, ", dir isize wrong");
622 if (errors & I_ERR_FILE_NBYTES_WRONG)
623 fprintf(stderr, ", nbytes wrong");
624 if (errors & I_ERR_ODD_CSUM_ITEM)
625 fprintf(stderr, ", odd csum item");
626 if (errors & I_ERR_SOME_CSUM_MISSING)
627 fprintf(stderr, ", some csum missing");
628 if (errors & I_ERR_LINK_COUNT_WRONG)
629 fprintf(stderr, ", link count wrong");
630 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
631 fprintf(stderr, ", orphan file extent");
632 fprintf(stderr, "\n");
633 /* Print the orphan extents if needed */
634 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
635 print_orphan_data_extents(&rec->orphan_extents, root->objectid);
637 /* Print the holes if needed */
638 if (errors & I_ERR_FILE_EXTENT_DISCOUNT) {
639 struct file_extent_hole *hole;
640 struct rb_node *node;
643 node = rb_first(&rec->holes);
644 fprintf(stderr, "Found file extent holes:\n");
647 hole = rb_entry(node, struct file_extent_hole, node);
648 fprintf(stderr, "\tstart: %llu, len: %llu\n",
649 hole->start, hole->len);
650 node = rb_next(node);
653 fprintf(stderr, "\tstart: 0, len: %llu\n",
655 root->fs_info->sectorsize));
659 static void print_ref_error(int errors)
661 if (errors & REF_ERR_NO_DIR_ITEM)
662 fprintf(stderr, ", no dir item");
663 if (errors & REF_ERR_NO_DIR_INDEX)
664 fprintf(stderr, ", no dir index");
665 if (errors & REF_ERR_NO_INODE_REF)
666 fprintf(stderr, ", no inode ref");
667 if (errors & REF_ERR_DUP_DIR_ITEM)
668 fprintf(stderr, ", dup dir item");
669 if (errors & REF_ERR_DUP_DIR_INDEX)
670 fprintf(stderr, ", dup dir index");
671 if (errors & REF_ERR_DUP_INODE_REF)
672 fprintf(stderr, ", dup inode ref");
673 if (errors & REF_ERR_INDEX_UNMATCH)
674 fprintf(stderr, ", index mismatch");
675 if (errors & REF_ERR_FILETYPE_UNMATCH)
676 fprintf(stderr, ", filetype mismatch");
677 if (errors & REF_ERR_NAME_TOO_LONG)
678 fprintf(stderr, ", name too long");
679 if (errors & REF_ERR_NO_ROOT_REF)
680 fprintf(stderr, ", no root ref");
681 if (errors & REF_ERR_NO_ROOT_BACKREF)
682 fprintf(stderr, ", no root backref");
683 if (errors & REF_ERR_DUP_ROOT_REF)
684 fprintf(stderr, ", dup root ref");
685 if (errors & REF_ERR_DUP_ROOT_BACKREF)
686 fprintf(stderr, ", dup root backref");
687 fprintf(stderr, "\n");
690 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
693 struct ptr_node *node;
694 struct cache_extent *cache;
695 struct inode_record *rec = NULL;
698 cache = lookup_cache_extent(inode_cache, ino, 1);
700 node = container_of(cache, struct ptr_node, cache);
702 if (mod && rec->refs > 1) {
703 node->data = clone_inode_rec(rec);
704 if (IS_ERR(node->data))
710 rec = calloc(1, sizeof(*rec));
712 return ERR_PTR(-ENOMEM);
714 rec->extent_start = (u64)-1;
716 INIT_LIST_HEAD(&rec->backrefs);
717 INIT_LIST_HEAD(&rec->orphan_extents);
718 rec->holes = RB_ROOT;
720 node = malloc(sizeof(*node));
723 return ERR_PTR(-ENOMEM);
725 node->cache.start = ino;
726 node->cache.size = 1;
729 if (ino == BTRFS_FREE_INO_OBJECTID)
732 ret = insert_cache_extent(inode_cache, &node->cache);
734 return ERR_PTR(-EEXIST);
739 static void free_orphan_data_extents(struct list_head *orphan_extents)
741 struct orphan_data_extent *orphan;
743 while (!list_empty(orphan_extents)) {
744 orphan = list_entry(orphan_extents->next,
745 struct orphan_data_extent, list);
746 list_del(&orphan->list);
751 static void free_inode_rec(struct inode_record *rec)
753 struct inode_backref *backref;
758 while (!list_empty(&rec->backrefs)) {
759 backref = to_inode_backref(rec->backrefs.next);
760 list_del(&backref->list);
763 free_orphan_data_extents(&rec->orphan_extents);
764 free_file_extent_holes(&rec->holes);
768 static int can_free_inode_rec(struct inode_record *rec)
770 if (!rec->errors && rec->checked && rec->found_inode_item &&
771 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
776 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
777 struct inode_record *rec)
779 struct cache_extent *cache;
780 struct inode_backref *tmp, *backref;
781 struct ptr_node *node;
784 if (!rec->found_inode_item)
787 filetype = imode_to_type(rec->imode);
788 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
789 if (backref->found_dir_item && backref->found_dir_index) {
790 if (backref->filetype != filetype)
791 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
792 if (!backref->errors && backref->found_inode_ref &&
793 rec->nlink == rec->found_link) {
794 list_del(&backref->list);
800 if (!rec->checked || rec->merging)
803 if (S_ISDIR(rec->imode)) {
804 if (rec->found_size != rec->isize)
805 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
806 if (rec->found_file_extent)
807 rec->errors |= I_ERR_ODD_FILE_EXTENT;
808 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
809 if (rec->found_dir_item)
810 rec->errors |= I_ERR_ODD_DIR_ITEM;
811 if (rec->found_size != rec->nbytes)
812 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
813 if (rec->nlink > 0 && !no_holes &&
814 (rec->extent_end < rec->isize ||
815 first_extent_gap(&rec->holes) < rec->isize))
816 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
819 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
820 if (rec->found_csum_item && rec->nodatasum)
821 rec->errors |= I_ERR_ODD_CSUM_ITEM;
822 if (rec->some_csum_missing && !rec->nodatasum)
823 rec->errors |= I_ERR_SOME_CSUM_MISSING;
826 BUG_ON(rec->refs != 1);
827 if (can_free_inode_rec(rec)) {
828 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
829 node = container_of(cache, struct ptr_node, cache);
830 BUG_ON(node->data != rec);
831 remove_cache_extent(inode_cache, &node->cache);
837 static int check_orphan_item(struct btrfs_root *root, u64 ino)
839 struct btrfs_path path;
840 struct btrfs_key key;
843 key.objectid = BTRFS_ORPHAN_OBJECTID;
844 key.type = BTRFS_ORPHAN_ITEM_KEY;
847 btrfs_init_path(&path);
848 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
849 btrfs_release_path(&path);
855 static int process_inode_item(struct extent_buffer *eb,
856 int slot, struct btrfs_key *key,
857 struct shared_node *active_node)
859 struct inode_record *rec;
860 struct btrfs_inode_item *item;
862 rec = active_node->current;
863 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
864 if (rec->found_inode_item) {
865 rec->errors |= I_ERR_DUP_INODE_ITEM;
868 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
869 rec->nlink = btrfs_inode_nlink(eb, item);
870 rec->isize = btrfs_inode_size(eb, item);
871 rec->nbytes = btrfs_inode_nbytes(eb, item);
872 rec->imode = btrfs_inode_mode(eb, item);
873 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
875 rec->found_inode_item = 1;
877 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
878 maybe_free_inode_rec(&active_node->inode_cache, rec);
882 static struct inode_backref *get_inode_backref(struct inode_record *rec,
884 int namelen, u64 dir)
886 struct inode_backref *backref;
888 list_for_each_entry(backref, &rec->backrefs, list) {
889 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
891 if (backref->dir != dir || backref->namelen != namelen)
893 if (memcmp(name, backref->name, namelen))
898 backref = malloc(sizeof(*backref) + namelen + 1);
901 memset(backref, 0, sizeof(*backref));
903 backref->namelen = namelen;
904 memcpy(backref->name, name, namelen);
905 backref->name[namelen] = '\0';
906 list_add_tail(&backref->list, &rec->backrefs);
910 static int add_inode_backref(struct cache_tree *inode_cache,
911 u64 ino, u64 dir, u64 index,
912 const char *name, int namelen,
913 u8 filetype, u8 itemtype, int errors)
915 struct inode_record *rec;
916 struct inode_backref *backref;
918 rec = get_inode_rec(inode_cache, ino, 1);
920 backref = get_inode_backref(rec, name, namelen, dir);
923 backref->errors |= errors;
924 if (itemtype == BTRFS_DIR_INDEX_KEY) {
925 if (backref->found_dir_index)
926 backref->errors |= REF_ERR_DUP_DIR_INDEX;
927 if (backref->found_inode_ref && backref->index != index)
928 backref->errors |= REF_ERR_INDEX_UNMATCH;
929 if (backref->found_dir_item && backref->filetype != filetype)
930 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
932 backref->index = index;
933 backref->filetype = filetype;
934 backref->found_dir_index = 1;
935 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
937 if (backref->found_dir_item)
938 backref->errors |= REF_ERR_DUP_DIR_ITEM;
939 if (backref->found_dir_index && backref->filetype != filetype)
940 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
942 backref->filetype = filetype;
943 backref->found_dir_item = 1;
944 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
945 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
946 if (backref->found_inode_ref)
947 backref->errors |= REF_ERR_DUP_INODE_REF;
948 if (backref->found_dir_index && backref->index != index)
949 backref->errors |= REF_ERR_INDEX_UNMATCH;
951 backref->index = index;
953 backref->ref_type = itemtype;
954 backref->found_inode_ref = 1;
959 maybe_free_inode_rec(inode_cache, rec);
963 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
964 struct cache_tree *dst_cache)
966 struct inode_backref *backref;
971 list_for_each_entry(backref, &src->backrefs, list) {
972 if (backref->found_dir_index) {
973 add_inode_backref(dst_cache, dst->ino, backref->dir,
974 backref->index, backref->name,
975 backref->namelen, backref->filetype,
976 BTRFS_DIR_INDEX_KEY, backref->errors);
978 if (backref->found_dir_item) {
980 add_inode_backref(dst_cache, dst->ino,
981 backref->dir, 0, backref->name,
982 backref->namelen, backref->filetype,
983 BTRFS_DIR_ITEM_KEY, backref->errors);
985 if (backref->found_inode_ref) {
986 add_inode_backref(dst_cache, dst->ino,
987 backref->dir, backref->index,
988 backref->name, backref->namelen, 0,
989 backref->ref_type, backref->errors);
993 if (src->found_dir_item)
994 dst->found_dir_item = 1;
995 if (src->found_file_extent)
996 dst->found_file_extent = 1;
997 if (src->found_csum_item)
998 dst->found_csum_item = 1;
999 if (src->some_csum_missing)
1000 dst->some_csum_missing = 1;
1001 if (first_extent_gap(&dst->holes) > first_extent_gap(&src->holes)) {
1002 ret = copy_file_extent_holes(&dst->holes, &src->holes);
1007 BUG_ON(src->found_link < dir_count);
1008 dst->found_link += src->found_link - dir_count;
1009 dst->found_size += src->found_size;
1010 if (src->extent_start != (u64)-1) {
1011 if (dst->extent_start == (u64)-1) {
1012 dst->extent_start = src->extent_start;
1013 dst->extent_end = src->extent_end;
1015 if (dst->extent_end > src->extent_start)
1016 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1017 else if (dst->extent_end < src->extent_start) {
1018 ret = add_file_extent_hole(&dst->holes,
1020 src->extent_start - dst->extent_end);
1022 if (dst->extent_end < src->extent_end)
1023 dst->extent_end = src->extent_end;
1027 dst->errors |= src->errors;
1028 if (src->found_inode_item) {
1029 if (!dst->found_inode_item) {
1030 dst->nlink = src->nlink;
1031 dst->isize = src->isize;
1032 dst->nbytes = src->nbytes;
1033 dst->imode = src->imode;
1034 dst->nodatasum = src->nodatasum;
1035 dst->found_inode_item = 1;
1037 dst->errors |= I_ERR_DUP_INODE_ITEM;
1045 static int splice_shared_node(struct shared_node *src_node,
1046 struct shared_node *dst_node)
1048 struct cache_extent *cache;
1049 struct ptr_node *node, *ins;
1050 struct cache_tree *src, *dst;
1051 struct inode_record *rec, *conflict;
1052 u64 current_ino = 0;
1056 if (--src_node->refs == 0)
1058 if (src_node->current)
1059 current_ino = src_node->current->ino;
1061 src = &src_node->root_cache;
1062 dst = &dst_node->root_cache;
1064 cache = search_cache_extent(src, 0);
1066 node = container_of(cache, struct ptr_node, cache);
1068 cache = next_cache_extent(cache);
1071 remove_cache_extent(src, &node->cache);
1074 ins = malloc(sizeof(*ins));
1076 ins->cache.start = node->cache.start;
1077 ins->cache.size = node->cache.size;
1081 ret = insert_cache_extent(dst, &ins->cache);
1082 if (ret == -EEXIST) {
1083 conflict = get_inode_rec(dst, rec->ino, 1);
1084 BUG_ON(IS_ERR(conflict));
1085 merge_inode_recs(rec, conflict, dst);
1087 conflict->checked = 1;
1088 if (dst_node->current == conflict)
1089 dst_node->current = NULL;
1091 maybe_free_inode_rec(dst, conflict);
1092 free_inode_rec(rec);
1099 if (src == &src_node->root_cache) {
1100 src = &src_node->inode_cache;
1101 dst = &dst_node->inode_cache;
1105 if (current_ino > 0 && (!dst_node->current ||
1106 current_ino > dst_node->current->ino)) {
1107 if (dst_node->current) {
1108 dst_node->current->checked = 1;
1109 maybe_free_inode_rec(dst, dst_node->current);
1111 dst_node->current = get_inode_rec(dst, current_ino, 1);
1112 BUG_ON(IS_ERR(dst_node->current));
1117 static void free_inode_ptr(struct cache_extent *cache)
1119 struct ptr_node *node;
1120 struct inode_record *rec;
1122 node = container_of(cache, struct ptr_node, cache);
1124 free_inode_rec(rec);
1128 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
1130 static struct shared_node *find_shared_node(struct cache_tree *shared,
1133 struct cache_extent *cache;
1134 struct shared_node *node;
1136 cache = lookup_cache_extent(shared, bytenr, 1);
1138 node = container_of(cache, struct shared_node, cache);
1144 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
1147 struct shared_node *node;
1149 node = calloc(1, sizeof(*node));
1152 node->cache.start = bytenr;
1153 node->cache.size = 1;
1154 cache_tree_init(&node->root_cache);
1155 cache_tree_init(&node->inode_cache);
1158 ret = insert_cache_extent(shared, &node->cache);
1163 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
1164 struct walk_control *wc, int level)
1166 struct shared_node *node;
1167 struct shared_node *dest;
1170 if (level == wc->active_node)
1173 BUG_ON(wc->active_node <= level);
1174 node = find_shared_node(&wc->shared, bytenr);
1176 ret = add_shared_node(&wc->shared, bytenr, refs);
1178 node = find_shared_node(&wc->shared, bytenr);
1179 wc->nodes[level] = node;
1180 wc->active_node = level;
1184 if (wc->root_level == wc->active_node &&
1185 btrfs_root_refs(&root->root_item) == 0) {
1186 if (--node->refs == 0) {
1187 free_inode_recs_tree(&node->root_cache);
1188 free_inode_recs_tree(&node->inode_cache);
1189 remove_cache_extent(&wc->shared, &node->cache);
1195 dest = wc->nodes[wc->active_node];
1196 splice_shared_node(node, dest);
1197 if (node->refs == 0) {
1198 remove_cache_extent(&wc->shared, &node->cache);
1204 static int leave_shared_node(struct btrfs_root *root,
1205 struct walk_control *wc, int level)
1207 struct shared_node *node;
1208 struct shared_node *dest;
1211 if (level == wc->root_level)
1214 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
1218 BUG_ON(i >= BTRFS_MAX_LEVEL);
1220 node = wc->nodes[wc->active_node];
1221 wc->nodes[wc->active_node] = NULL;
1222 wc->active_node = i;
1224 dest = wc->nodes[wc->active_node];
1225 if (wc->active_node < wc->root_level ||
1226 btrfs_root_refs(&root->root_item) > 0) {
1227 BUG_ON(node->refs <= 1);
1228 splice_shared_node(node, dest);
1230 BUG_ON(node->refs < 2);
1239 * 1 - if the root with id child_root_id is a child of root parent_root_id
1240 * 0 - if the root child_root_id isn't a child of the root parent_root_id but
1241 * has other root(s) as parent(s)
1242 * 2 - if the root child_root_id doesn't have any parent roots
1244 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
1247 struct btrfs_path path;
1248 struct btrfs_key key;
1249 struct extent_buffer *leaf;
1253 btrfs_init_path(&path);
1255 key.objectid = parent_root_id;
1256 key.type = BTRFS_ROOT_REF_KEY;
1257 key.offset = child_root_id;
1258 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1262 btrfs_release_path(&path);
1266 key.objectid = child_root_id;
1267 key.type = BTRFS_ROOT_BACKREF_KEY;
1269 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1275 leaf = path.nodes[0];
1276 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1277 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
1280 leaf = path.nodes[0];
1283 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1284 if (key.objectid != child_root_id ||
1285 key.type != BTRFS_ROOT_BACKREF_KEY)
1290 if (key.offset == parent_root_id) {
1291 btrfs_release_path(&path);
1298 btrfs_release_path(&path);
1301 return has_parent ? 0 : 2;
1304 static int process_dir_item(struct extent_buffer *eb,
1305 int slot, struct btrfs_key *key,
1306 struct shared_node *active_node)
1316 struct btrfs_dir_item *di;
1317 struct inode_record *rec;
1318 struct cache_tree *root_cache;
1319 struct cache_tree *inode_cache;
1320 struct btrfs_key location;
1321 char namebuf[BTRFS_NAME_LEN];
1323 root_cache = &active_node->root_cache;
1324 inode_cache = &active_node->inode_cache;
1325 rec = active_node->current;
1326 rec->found_dir_item = 1;
1328 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1329 total = btrfs_item_size_nr(eb, slot);
1330 while (cur < total) {
1332 btrfs_dir_item_key_to_cpu(eb, di, &location);
1333 name_len = btrfs_dir_name_len(eb, di);
1334 data_len = btrfs_dir_data_len(eb, di);
1335 filetype = btrfs_dir_type(eb, di);
1337 rec->found_size += name_len;
1338 if (cur + sizeof(*di) + name_len > total ||
1339 name_len > BTRFS_NAME_LEN) {
1340 error = REF_ERR_NAME_TOO_LONG;
1342 if (cur + sizeof(*di) > total)
1344 len = min_t(u32, total - cur - sizeof(*di),
1351 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
1353 if (key->type == BTRFS_DIR_ITEM_KEY &&
1354 key->offset != btrfs_name_hash(namebuf, len)) {
1355 rec->errors |= I_ERR_ODD_DIR_ITEM;
1356 error("DIR_ITEM[%llu %llu] name %s namelen %u filetype %u mismatch with its hash, wanted %llu have %llu",
1357 key->objectid, key->offset, namebuf, len, filetype,
1358 key->offset, btrfs_name_hash(namebuf, len));
1361 if (location.type == BTRFS_INODE_ITEM_KEY) {
1362 add_inode_backref(inode_cache, location.objectid,
1363 key->objectid, key->offset, namebuf,
1364 len, filetype, key->type, error);
1365 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
1366 add_inode_backref(root_cache, location.objectid,
1367 key->objectid, key->offset,
1368 namebuf, len, filetype,
1372 "unknown location type %d in DIR_ITEM[%llu %llu]\n",
1373 location.type, key->objectid, key->offset);
1374 add_inode_backref(inode_cache, BTRFS_MULTIPLE_OBJECTIDS,
1375 key->objectid, key->offset, namebuf,
1376 len, filetype, key->type, error);
1379 len = sizeof(*di) + name_len + data_len;
1380 di = (struct btrfs_dir_item *)((char *)di + len);
1383 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1384 rec->errors |= I_ERR_DUP_DIR_INDEX;
1389 static int process_inode_ref(struct extent_buffer *eb,
1390 int slot, struct btrfs_key *key,
1391 struct shared_node *active_node)
1399 struct cache_tree *inode_cache;
1400 struct btrfs_inode_ref *ref;
1401 char namebuf[BTRFS_NAME_LEN];
1403 inode_cache = &active_node->inode_cache;
1405 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1406 total = btrfs_item_size_nr(eb, slot);
1407 while (cur < total) {
1408 name_len = btrfs_inode_ref_name_len(eb, ref);
1409 index = btrfs_inode_ref_index(eb, ref);
1411 /* inode_ref + namelen should not cross item boundary */
1412 if (cur + sizeof(*ref) + name_len > total ||
1413 name_len > BTRFS_NAME_LEN) {
1414 if (total < cur + sizeof(*ref))
1417 /* Still try to read out the remaining part */
1418 len = min_t(u32, total - cur - sizeof(*ref),
1420 error = REF_ERR_NAME_TOO_LONG;
1426 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1427 add_inode_backref(inode_cache, key->objectid, key->offset,
1428 index, namebuf, len, 0, key->type, error);
1430 len = sizeof(*ref) + name_len;
1431 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1437 static int process_inode_extref(struct extent_buffer *eb,
1438 int slot, struct btrfs_key *key,
1439 struct shared_node *active_node)
1448 struct cache_tree *inode_cache;
1449 struct btrfs_inode_extref *extref;
1450 char namebuf[BTRFS_NAME_LEN];
1452 inode_cache = &active_node->inode_cache;
1454 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1455 total = btrfs_item_size_nr(eb, slot);
1456 while (cur < total) {
1457 name_len = btrfs_inode_extref_name_len(eb, extref);
1458 index = btrfs_inode_extref_index(eb, extref);
1459 parent = btrfs_inode_extref_parent(eb, extref);
1460 if (name_len <= BTRFS_NAME_LEN) {
1464 len = BTRFS_NAME_LEN;
1465 error = REF_ERR_NAME_TOO_LONG;
1467 read_extent_buffer(eb, namebuf,
1468 (unsigned long)(extref + 1), len);
1469 add_inode_backref(inode_cache, key->objectid, parent,
1470 index, namebuf, len, 0, key->type, error);
1472 len = sizeof(*extref) + name_len;
1473 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1480 static int count_csum_range(struct btrfs_root *root, u64 start,
1481 u64 len, u64 *found)
1483 struct btrfs_key key;
1484 struct btrfs_path path;
1485 struct extent_buffer *leaf;
1490 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1492 btrfs_init_path(&path);
1494 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1496 key.type = BTRFS_EXTENT_CSUM_KEY;
1498 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1502 if (ret > 0 && path.slots[0] > 0) {
1503 leaf = path.nodes[0];
1504 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1505 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1506 key.type == BTRFS_EXTENT_CSUM_KEY)
1511 leaf = path.nodes[0];
1512 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1513 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1518 leaf = path.nodes[0];
1521 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1522 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1523 key.type != BTRFS_EXTENT_CSUM_KEY)
1526 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1527 if (key.offset >= start + len)
1530 if (key.offset > start)
1533 size = btrfs_item_size_nr(leaf, path.slots[0]);
1534 csum_end = key.offset + (size / csum_size) *
1535 root->fs_info->sectorsize;
1536 if (csum_end > start) {
1537 size = min(csum_end - start, len);
1546 btrfs_release_path(&path);
1552 static int process_file_extent(struct btrfs_root *root,
1553 struct extent_buffer *eb,
1554 int slot, struct btrfs_key *key,
1555 struct shared_node *active_node)
1557 struct inode_record *rec;
1558 struct btrfs_file_extent_item *fi;
1560 u64 disk_bytenr = 0;
1561 u64 extent_offset = 0;
1562 u64 mask = root->fs_info->sectorsize - 1;
1566 rec = active_node->current;
1567 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1568 rec->found_file_extent = 1;
1570 if (rec->extent_start == (u64)-1) {
1571 rec->extent_start = key->offset;
1572 rec->extent_end = key->offset;
1575 if (rec->extent_end > key->offset)
1576 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1577 else if (rec->extent_end < key->offset) {
1578 ret = add_file_extent_hole(&rec->holes, rec->extent_end,
1579 key->offset - rec->extent_end);
1584 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1585 extent_type = btrfs_file_extent_type(eb, fi);
1587 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1588 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1590 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1591 rec->found_size += num_bytes;
1592 num_bytes = (num_bytes + mask) & ~mask;
1593 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1594 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1595 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1596 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1597 extent_offset = btrfs_file_extent_offset(eb, fi);
1598 if (num_bytes == 0 || (num_bytes & mask))
1599 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1600 if (num_bytes + extent_offset >
1601 btrfs_file_extent_ram_bytes(eb, fi))
1602 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1603 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1604 (btrfs_file_extent_compression(eb, fi) ||
1605 btrfs_file_extent_encryption(eb, fi) ||
1606 btrfs_file_extent_other_encoding(eb, fi)))
1607 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1608 if (disk_bytenr > 0)
1609 rec->found_size += num_bytes;
1611 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1613 rec->extent_end = key->offset + num_bytes;
1616 * The data reloc tree will copy full extents into its inode and then
1617 * copy the corresponding csums. Because the extent it copied could be
1618 * a preallocated extent that hasn't been written to yet there may be no
1619 * csums to copy, ergo we won't have csums for our file extent. This is
1620 * ok so just don't bother checking csums if the inode belongs to the
1623 if (disk_bytenr > 0 &&
1624 btrfs_header_owner(eb) != BTRFS_DATA_RELOC_TREE_OBJECTID) {
1626 if (btrfs_file_extent_compression(eb, fi))
1627 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1629 disk_bytenr += extent_offset;
1631 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1634 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1636 rec->found_csum_item = 1;
1637 if (found < num_bytes)
1638 rec->some_csum_missing = 1;
1639 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1641 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1647 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1648 struct walk_control *wc)
1650 struct btrfs_key key;
1654 struct cache_tree *inode_cache;
1655 struct shared_node *active_node;
1657 if (wc->root_level == wc->active_node &&
1658 btrfs_root_refs(&root->root_item) == 0)
1661 active_node = wc->nodes[wc->active_node];
1662 inode_cache = &active_node->inode_cache;
1663 nritems = btrfs_header_nritems(eb);
1664 for (i = 0; i < nritems; i++) {
1665 btrfs_item_key_to_cpu(eb, &key, i);
1667 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1669 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1672 if (active_node->current == NULL ||
1673 active_node->current->ino < key.objectid) {
1674 if (active_node->current) {
1675 active_node->current->checked = 1;
1676 maybe_free_inode_rec(inode_cache,
1677 active_node->current);
1679 active_node->current = get_inode_rec(inode_cache,
1681 BUG_ON(IS_ERR(active_node->current));
1684 case BTRFS_DIR_ITEM_KEY:
1685 case BTRFS_DIR_INDEX_KEY:
1686 ret = process_dir_item(eb, i, &key, active_node);
1688 case BTRFS_INODE_REF_KEY:
1689 ret = process_inode_ref(eb, i, &key, active_node);
1691 case BTRFS_INODE_EXTREF_KEY:
1692 ret = process_inode_extref(eb, i, &key, active_node);
1694 case BTRFS_INODE_ITEM_KEY:
1695 ret = process_inode_item(eb, i, &key, active_node);
1697 case BTRFS_EXTENT_DATA_KEY:
1698 ret = process_file_extent(root, eb, i, &key,
1709 u64 bytenr[BTRFS_MAX_LEVEL];
1710 u64 refs[BTRFS_MAX_LEVEL];
1711 int need_check[BTRFS_MAX_LEVEL];
1712 /* field for checking all trees */
1713 int checked[BTRFS_MAX_LEVEL];
1714 /* the corresponding extent should be marked as full backref or not */
1715 int full_backref[BTRFS_MAX_LEVEL];
1718 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
1719 struct extent_buffer *eb, struct node_refs *nrefs,
1720 u64 level, int check_all);
1721 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
1722 unsigned int ext_ref);
1725 * Returns >0 Found error, not fatal, should continue
1726 * Returns <0 Fatal error, must exit the whole check
1727 * Returns 0 No errors found
1729 static int process_one_leaf_v2(struct btrfs_root *root, struct btrfs_path *path,
1730 struct node_refs *nrefs, int *level, int ext_ref)
1732 struct extent_buffer *cur = path->nodes[0];
1733 struct btrfs_key key;
1737 int root_level = btrfs_header_level(root->node);
1739 int ret = 0; /* Final return value */
1740 int err = 0; /* Positive error bitmap */
1742 cur_bytenr = cur->start;
1744 /* skip to first inode item or the first inode number change */
1745 nritems = btrfs_header_nritems(cur);
1746 for (i = 0; i < nritems; i++) {
1747 btrfs_item_key_to_cpu(cur, &key, i);
1749 first_ino = key.objectid;
1750 if (key.type == BTRFS_INODE_ITEM_KEY ||
1751 (first_ino && first_ino != key.objectid))
1755 path->slots[0] = nritems;
1761 err |= check_inode_item(root, path, ext_ref);
1763 /* modify cur since check_inode_item may change path */
1764 cur = path->nodes[0];
1766 if (err & LAST_ITEM)
1769 /* still have inode items in thie leaf */
1770 if (cur->start == cur_bytenr)
1774 * we have switched to another leaf, above nodes may
1775 * have changed, here walk down the path, if a node
1776 * or leaf is shared, check whether we can skip this
1779 for (i = root_level; i >= 0; i--) {
1780 if (path->nodes[i]->start == nrefs->bytenr[i])
1783 ret = update_nodes_refs(root, path->nodes[i]->start,
1784 path->nodes[i], nrefs, i, 0);
1788 if (!nrefs->need_check[i]) {
1794 for (i = 0; i < *level; i++) {
1795 free_extent_buffer(path->nodes[i]);
1796 path->nodes[i] = NULL;
1805 static void reada_walk_down(struct btrfs_root *root,
1806 struct extent_buffer *node, int slot)
1808 struct btrfs_fs_info *fs_info = root->fs_info;
1815 level = btrfs_header_level(node);
1819 nritems = btrfs_header_nritems(node);
1820 for (i = slot; i < nritems; i++) {
1821 bytenr = btrfs_node_blockptr(node, i);
1822 ptr_gen = btrfs_node_ptr_generation(node, i);
1823 readahead_tree_block(fs_info, bytenr, ptr_gen);
1828 * Check the child node/leaf by the following condition:
1829 * 1. the first item key of the node/leaf should be the same with the one
1831 * 2. block in parent node should match the child node/leaf.
1832 * 3. generation of parent node and child's header should be consistent.
1834 * Or the child node/leaf pointed by the key in parent is not valid.
1836 * We hope to check leaf owner too, but since subvol may share leaves,
1837 * which makes leaf owner check not so strong, key check should be
1838 * sufficient enough for that case.
1840 static int check_child_node(struct extent_buffer *parent, int slot,
1841 struct extent_buffer *child)
1843 struct btrfs_key parent_key;
1844 struct btrfs_key child_key;
1847 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1848 if (btrfs_header_level(child) == 0)
1849 btrfs_item_key_to_cpu(child, &child_key, 0);
1851 btrfs_node_key_to_cpu(child, &child_key, 0);
1853 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
1856 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
1857 parent_key.objectid, parent_key.type, parent_key.offset,
1858 child_key.objectid, child_key.type, child_key.offset);
1860 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
1862 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
1863 btrfs_node_blockptr(parent, slot),
1864 btrfs_header_bytenr(child));
1866 if (btrfs_node_ptr_generation(parent, slot) !=
1867 btrfs_header_generation(child)) {
1869 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
1870 btrfs_header_generation(child),
1871 btrfs_node_ptr_generation(parent, slot));
1877 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
1878 * in every fs or file tree check. Here we find its all root ids, and only check
1879 * it in the fs or file tree which has the smallest root id.
1881 static int need_check(struct btrfs_root *root, struct ulist *roots)
1883 struct rb_node *node;
1884 struct ulist_node *u;
1887 * @roots can be empty if it belongs to tree reloc tree
1888 * In that case, we should always check the leaf, as we can't use
1889 * the tree owner to ensure some other root will check it.
1891 if (roots->nnodes == 1 || roots->nnodes == 0)
1894 node = rb_first(&roots->root);
1895 u = rb_entry(node, struct ulist_node, rb_node);
1897 * current root id is not smallest, we skip it and let it be checked
1898 * in the fs or file tree who hash the smallest root id.
1900 if (root->objectid != u->val)
1906 static int calc_extent_flag_v2(struct btrfs_root *root, struct extent_buffer *eb,
1909 struct btrfs_root *extent_root = root->fs_info->extent_root;
1910 struct btrfs_root_item *ri = &root->root_item;
1911 struct btrfs_extent_inline_ref *iref;
1912 struct btrfs_extent_item *ei;
1913 struct btrfs_key key;
1914 struct btrfs_path *path = NULL;
1925 * Except file/reloc tree, we can not have FULL BACKREF MODE
1927 if (root->objectid < BTRFS_FIRST_FREE_OBJECTID)
1931 if (eb->start == btrfs_root_bytenr(ri))
1934 if (btrfs_header_flag(eb, BTRFS_HEADER_FLAG_RELOC))
1937 owner = btrfs_header_owner(eb);
1938 if (owner == root->objectid)
1941 path = btrfs_alloc_path();
1945 key.objectid = btrfs_header_bytenr(eb);
1947 key.offset = (u64)-1;
1949 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1956 ret = btrfs_previous_extent_item(extent_root, path,
1962 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1964 eb = path->nodes[0];
1965 slot = path->slots[0];
1966 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
1968 flags = btrfs_extent_flags(eb, ei);
1969 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
1972 ptr = (unsigned long)(ei + 1);
1973 end = (unsigned long)ei + btrfs_item_size_nr(eb, slot);
1975 if (key.type == BTRFS_EXTENT_ITEM_KEY)
1976 ptr += sizeof(struct btrfs_tree_block_info);
1979 /* Reached extent item ends normally */
1983 /* Beyond extent item end, wrong item size */
1985 error("extent item at bytenr %llu slot %d has wrong size",
1990 iref = (struct btrfs_extent_inline_ref *)ptr;
1991 offset = btrfs_extent_inline_ref_offset(eb, iref);
1992 type = btrfs_extent_inline_ref_type(eb, iref);
1994 if (type == BTRFS_TREE_BLOCK_REF_KEY && offset == owner)
1996 ptr += btrfs_extent_inline_ref_size(type);
2000 *flags_ret &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
2004 *flags_ret |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
2006 btrfs_free_path(path);
2011 * for a tree node or leaf, we record its reference count, so later if we still
2012 * process this node or leaf, don't need to compute its reference count again.
2014 * @bytenr if @bytenr == (u64)-1, only update nrefs->full_backref[level]
2016 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
2017 struct extent_buffer *eb, struct node_refs *nrefs,
2018 u64 level, int check_all)
2020 struct ulist *roots;
2023 int root_level = btrfs_header_level(root->node);
2027 if (nrefs->bytenr[level] == bytenr)
2030 if (bytenr != (u64)-1) {
2031 /* the return value of this function seems a mistake */
2032 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2033 level, 1, &refs, &flags);
2035 if (ret < 0 && !check_all)
2038 nrefs->bytenr[level] = bytenr;
2039 nrefs->refs[level] = refs;
2040 nrefs->full_backref[level] = 0;
2041 nrefs->checked[level] = 0;
2044 ret = btrfs_find_all_roots(NULL, root->fs_info, bytenr,
2049 check = need_check(root, roots);
2051 nrefs->need_check[level] = check;
2054 nrefs->need_check[level] = 1;
2056 if (level == root_level) {
2057 nrefs->need_check[level] = 1;
2060 * The node refs may have not been
2061 * updated if upper needs checking (the
2062 * lowest root_objectid) the node can
2065 nrefs->need_check[level] =
2066 nrefs->need_check[level + 1];
2072 if (check_all && eb) {
2073 calc_extent_flag_v2(root, eb, &flags);
2074 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2075 nrefs->full_backref[level] = 1;
2082 * @level if @level == -1 means extent data item
2083 * else normal treeblocl.
2085 static int should_check_extent_strictly(struct btrfs_root *root,
2086 struct node_refs *nrefs, int level)
2088 int root_level = btrfs_header_level(root->node);
2090 if (level > root_level || level < -1)
2092 if (level == root_level)
2095 * if the upper node is marked full backref, it should contain shared
2096 * backref of the parent (except owner == root->objectid).
2098 while (++level <= root_level)
2099 if (nrefs->refs[level] > 1)
2105 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
2106 struct walk_control *wc, int *level,
2107 struct node_refs *nrefs)
2109 enum btrfs_tree_block_status status;
2112 struct btrfs_fs_info *fs_info = root->fs_info;
2113 struct extent_buffer *next;
2114 struct extent_buffer *cur;
2118 WARN_ON(*level < 0);
2119 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2121 if (path->nodes[*level]->start == nrefs->bytenr[*level]) {
2122 refs = nrefs->refs[*level];
2125 ret = btrfs_lookup_extent_info(NULL, root,
2126 path->nodes[*level]->start,
2127 *level, 1, &refs, NULL);
2132 nrefs->bytenr[*level] = path->nodes[*level]->start;
2133 nrefs->refs[*level] = refs;
2137 ret = enter_shared_node(root, path->nodes[*level]->start,
2145 while (*level >= 0) {
2146 WARN_ON(*level < 0);
2147 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2148 cur = path->nodes[*level];
2150 if (btrfs_header_level(cur) != *level)
2153 if (path->slots[*level] >= btrfs_header_nritems(cur))
2156 ret = process_one_leaf(root, cur, wc);
2161 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2162 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2164 if (bytenr == nrefs->bytenr[*level - 1]) {
2165 refs = nrefs->refs[*level - 1];
2167 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2168 *level - 1, 1, &refs, NULL);
2172 nrefs->bytenr[*level - 1] = bytenr;
2173 nrefs->refs[*level - 1] = refs;
2178 ret = enter_shared_node(root, bytenr, refs,
2181 path->slots[*level]++;
2186 next = btrfs_find_tree_block(fs_info, bytenr, fs_info->nodesize);
2187 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2188 free_extent_buffer(next);
2189 reada_walk_down(root, cur, path->slots[*level]);
2190 next = read_tree_block(root->fs_info, bytenr, ptr_gen);
2191 if (!extent_buffer_uptodate(next)) {
2192 struct btrfs_key node_key;
2194 btrfs_node_key_to_cpu(path->nodes[*level],
2196 path->slots[*level]);
2197 btrfs_add_corrupt_extent_record(root->fs_info,
2199 path->nodes[*level]->start,
2200 root->fs_info->nodesize,
2207 ret = check_child_node(cur, path->slots[*level], next);
2209 free_extent_buffer(next);
2214 if (btrfs_is_leaf(next))
2215 status = btrfs_check_leaf(root, NULL, next);
2217 status = btrfs_check_node(root, NULL, next);
2218 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2219 free_extent_buffer(next);
2224 *level = *level - 1;
2225 free_extent_buffer(path->nodes[*level]);
2226 path->nodes[*level] = next;
2227 path->slots[*level] = 0;
2230 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2234 static int fs_root_objectid(u64 objectid);
2237 * Update global fs information.
2239 static void account_bytes(struct btrfs_root *root, struct btrfs_path *path,
2243 struct extent_buffer *eb = path->nodes[level];
2245 total_btree_bytes += eb->len;
2246 if (fs_root_objectid(root->objectid))
2247 total_fs_tree_bytes += eb->len;
2248 if (btrfs_header_owner(eb) == BTRFS_EXTENT_TREE_OBJECTID)
2249 total_extent_tree_bytes += eb->len;
2252 btree_space_waste += btrfs_leaf_free_space(root, eb);
2254 free_nrs = (BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) -
2255 btrfs_header_nritems(eb));
2256 btree_space_waste += free_nrs * sizeof(struct btrfs_key_ptr);
2261 * This function only handles BACKREF_MISSING,
2262 * If corresponding extent item exists, increase the ref, else insert an extent
2265 * Returns error bits after repair.
2267 static int repair_tree_block_ref(struct btrfs_trans_handle *trans,
2268 struct btrfs_root *root,
2269 struct extent_buffer *node,
2270 struct node_refs *nrefs, int level, int err)
2272 struct btrfs_fs_info *fs_info = root->fs_info;
2273 struct btrfs_root *extent_root = fs_info->extent_root;
2274 struct btrfs_path path;
2275 struct btrfs_extent_item *ei;
2276 struct btrfs_tree_block_info *bi;
2277 struct btrfs_key key;
2278 struct extent_buffer *eb;
2279 u32 size = sizeof(*ei);
2280 u32 node_size = root->fs_info->nodesize;
2281 int insert_extent = 0;
2282 int skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
2283 int root_level = btrfs_header_level(root->node);
2288 u64 flags = BTRFS_EXTENT_FLAG_TREE_BLOCK;
2291 if ((err & BACKREF_MISSING) == 0)
2294 WARN_ON(level > BTRFS_MAX_LEVEL);
2297 btrfs_init_path(&path);
2298 bytenr = btrfs_header_bytenr(node);
2299 owner = btrfs_header_owner(node);
2300 generation = btrfs_header_generation(node);
2302 key.objectid = bytenr;
2304 key.offset = (u64)-1;
2306 /* Search for the extent item */
2307 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
2313 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
2317 /* calculate if the extent item flag is full backref or not */
2318 if (nrefs->full_backref[level] != 0)
2319 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
2321 /* insert an extent item */
2322 if (insert_extent) {
2323 struct btrfs_disk_key copy_key;
2325 generation = btrfs_header_generation(node);
2327 if (level < root_level && nrefs->full_backref[level + 1] &&
2328 owner != root->objectid) {
2329 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
2332 key.objectid = bytenr;
2333 if (!skinny_metadata) {
2334 key.type = BTRFS_EXTENT_ITEM_KEY;
2335 key.offset = node_size;
2336 size += sizeof(*bi);
2338 key.type = BTRFS_METADATA_ITEM_KEY;
2342 btrfs_release_path(&path);
2343 ret = btrfs_insert_empty_item(trans, extent_root, &path, &key,
2349 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
2351 btrfs_set_extent_refs(eb, ei, 0);
2352 btrfs_set_extent_generation(eb, ei, generation);
2353 btrfs_set_extent_flags(eb, ei, flags);
2355 if (!skinny_metadata) {
2356 bi = (struct btrfs_tree_block_info *)(ei + 1);
2357 memset_extent_buffer(eb, 0, (unsigned long)bi,
2359 btrfs_set_disk_key_objectid(©_key, root->objectid);
2360 btrfs_set_disk_key_type(©_key, 0);
2361 btrfs_set_disk_key_offset(©_key, 0);
2363 btrfs_set_tree_block_level(eb, bi, level);
2364 btrfs_set_tree_block_key(eb, bi, ©_key);
2366 btrfs_mark_buffer_dirty(eb);
2367 printf("Added an extent item [%llu %u]\n", bytenr, node_size);
2368 btrfs_update_block_group(extent_root, bytenr, node_size, 1, 0);
2370 nrefs->refs[level] = 0;
2371 nrefs->full_backref[level] =
2372 flags & BTRFS_BLOCK_FLAG_FULL_BACKREF;
2373 btrfs_release_path(&path);
2376 if (level < root_level && nrefs->full_backref[level + 1] &&
2377 owner != root->objectid)
2378 parent = nrefs->bytenr[level + 1];
2380 /* increase the ref */
2381 ret = btrfs_inc_extent_ref(trans, extent_root, bytenr, node_size,
2382 parent, root->objectid, level, 0);
2384 nrefs->refs[level]++;
2386 btrfs_release_path(&path);
2389 "failed to repair tree block ref start %llu root %llu due to %s",
2390 bytenr, root->objectid, strerror(-ret));
2392 printf("Added one tree block ref start %llu %s %llu\n",
2393 bytenr, parent ? "parent" : "root",
2394 parent ? parent : root->objectid);
2395 err &= ~BACKREF_MISSING;
2401 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
2402 unsigned int ext_ref);
2403 static int check_tree_block_ref(struct btrfs_root *root,
2404 struct extent_buffer *eb, u64 bytenr,
2405 int level, u64 owner, struct node_refs *nrefs);
2406 static int check_leaf_items(struct btrfs_trans_handle *trans,
2407 struct btrfs_root *root, struct btrfs_path *path,
2408 struct node_refs *nrefs, int account_bytes);
2411 * @trans just for lowmem repair mode
2412 * @check all if not 0 then check all tree block backrefs and items
2413 * 0 then just check relationship of items in fs tree(s)
2415 * Returns >0 Found error, should continue
2416 * Returns <0 Fatal error, must exit the whole check
2417 * Returns 0 No errors found
2419 static int walk_down_tree_v2(struct btrfs_trans_handle *trans,
2420 struct btrfs_root *root, struct btrfs_path *path,
2421 int *level, struct node_refs *nrefs, int ext_ref,
2425 enum btrfs_tree_block_status status;
2428 struct btrfs_fs_info *fs_info = root->fs_info;
2429 struct extent_buffer *next;
2430 struct extent_buffer *cur;
2434 int account_file_data = 0;
2436 WARN_ON(*level < 0);
2437 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2439 ret = update_nodes_refs(root, btrfs_header_bytenr(path->nodes[*level]),
2440 path->nodes[*level], nrefs, *level, check_all);
2444 while (*level >= 0) {
2445 WARN_ON(*level < 0);
2446 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2447 cur = path->nodes[*level];
2448 bytenr = btrfs_header_bytenr(cur);
2449 check = nrefs->need_check[*level];
2451 if (btrfs_header_level(cur) != *level)
2454 * Update bytes accounting and check tree block ref
2455 * NOTE: Doing accounting and check before checking nritems
2456 * is necessary because of empty node/leaf.
2458 if ((check_all && !nrefs->checked[*level]) ||
2459 (!check_all && nrefs->need_check[*level])) {
2460 ret = check_tree_block_ref(root, cur,
2461 btrfs_header_bytenr(cur), btrfs_header_level(cur),
2462 btrfs_header_owner(cur), nrefs);
2465 ret = repair_tree_block_ref(trans, root,
2466 path->nodes[*level], nrefs, *level, ret);
2469 if (check_all && nrefs->need_check[*level] &&
2470 nrefs->refs[*level]) {
2471 account_bytes(root, path, *level);
2472 account_file_data = 1;
2474 nrefs->checked[*level] = 1;
2477 if (path->slots[*level] >= btrfs_header_nritems(cur))
2480 /* Don't forgot to check leaf/node validation */
2482 /* skip duplicate check */
2483 if (check || !check_all) {
2484 ret = btrfs_check_leaf(root, NULL, cur);
2485 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2493 ret = process_one_leaf_v2(root, path, nrefs,
2496 ret = check_leaf_items(trans, root, path,
2497 nrefs, account_file_data);
2501 if (check || !check_all) {
2502 ret = btrfs_check_node(root, NULL, cur);
2503 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2510 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2511 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2513 ret = update_nodes_refs(root, bytenr, NULL, nrefs, *level - 1,
2518 * check all trees in check_chunks_and_extent_v2
2519 * check shared node once in check_fs_roots
2521 if (!check_all && !nrefs->need_check[*level - 1]) {
2522 path->slots[*level]++;
2526 next = btrfs_find_tree_block(fs_info, bytenr, fs_info->nodesize);
2527 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2528 free_extent_buffer(next);
2529 reada_walk_down(root, cur, path->slots[*level]);
2530 next = read_tree_block(fs_info, bytenr, ptr_gen);
2531 if (!extent_buffer_uptodate(next)) {
2532 struct btrfs_key node_key;
2534 btrfs_node_key_to_cpu(path->nodes[*level],
2536 path->slots[*level]);
2537 btrfs_add_corrupt_extent_record(fs_info,
2538 &node_key, path->nodes[*level]->start,
2539 fs_info->nodesize, *level);
2545 ret = check_child_node(cur, path->slots[*level], next);
2550 if (btrfs_is_leaf(next))
2551 status = btrfs_check_leaf(root, NULL, next);
2553 status = btrfs_check_node(root, NULL, next);
2554 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2555 free_extent_buffer(next);
2560 *level = *level - 1;
2561 free_extent_buffer(path->nodes[*level]);
2562 path->nodes[*level] = next;
2563 path->slots[*level] = 0;
2564 account_file_data = 0;
2566 update_nodes_refs(root, (u64)-1, next, nrefs, *level, check_all);
2571 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
2572 struct walk_control *wc, int *level)
2575 struct extent_buffer *leaf;
2577 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2578 leaf = path->nodes[i];
2579 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2584 free_extent_buffer(path->nodes[*level]);
2585 path->nodes[*level] = NULL;
2586 BUG_ON(*level > wc->active_node);
2587 if (*level == wc->active_node)
2588 leave_shared_node(root, wc, *level);
2595 static int walk_up_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2599 struct extent_buffer *leaf;
2601 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2602 leaf = path->nodes[i];
2603 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2608 free_extent_buffer(path->nodes[*level]);
2609 path->nodes[*level] = NULL;
2616 static int check_root_dir(struct inode_record *rec)
2618 struct inode_backref *backref;
2621 if (!rec->found_inode_item || rec->errors)
2623 if (rec->nlink != 1 || rec->found_link != 0)
2625 if (list_empty(&rec->backrefs))
2627 backref = to_inode_backref(rec->backrefs.next);
2628 if (!backref->found_inode_ref)
2630 if (backref->index != 0 || backref->namelen != 2 ||
2631 memcmp(backref->name, "..", 2))
2633 if (backref->found_dir_index || backref->found_dir_item)
2640 static int repair_inode_isize(struct btrfs_trans_handle *trans,
2641 struct btrfs_root *root, struct btrfs_path *path,
2642 struct inode_record *rec)
2644 struct btrfs_inode_item *ei;
2645 struct btrfs_key key;
2648 key.objectid = rec->ino;
2649 key.type = BTRFS_INODE_ITEM_KEY;
2650 key.offset = (u64)-1;
2652 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2656 if (!path->slots[0]) {
2663 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2664 if (key.objectid != rec->ino) {
2669 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2670 struct btrfs_inode_item);
2671 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
2672 btrfs_mark_buffer_dirty(path->nodes[0]);
2673 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2674 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
2675 root->root_key.objectid);
2677 btrfs_release_path(path);
2681 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
2682 struct btrfs_root *root,
2683 struct btrfs_path *path,
2684 struct inode_record *rec)
2688 ret = btrfs_add_orphan_item(trans, root, path, rec->ino);
2689 btrfs_release_path(path);
2691 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2695 static int repair_inode_nbytes(struct btrfs_trans_handle *trans,
2696 struct btrfs_root *root,
2697 struct btrfs_path *path,
2698 struct inode_record *rec)
2700 struct btrfs_inode_item *ei;
2701 struct btrfs_key key;
2704 key.objectid = rec->ino;
2705 key.type = BTRFS_INODE_ITEM_KEY;
2708 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2715 /* Since ret == 0, no need to check anything */
2716 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2717 struct btrfs_inode_item);
2718 btrfs_set_inode_nbytes(path->nodes[0], ei, rec->found_size);
2719 btrfs_mark_buffer_dirty(path->nodes[0]);
2720 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
2721 printf("reset nbytes for ino %llu root %llu\n",
2722 rec->ino, root->root_key.objectid);
2724 btrfs_release_path(path);
2728 static int add_missing_dir_index(struct btrfs_root *root,
2729 struct cache_tree *inode_cache,
2730 struct inode_record *rec,
2731 struct inode_backref *backref)
2733 struct btrfs_path path;
2734 struct btrfs_trans_handle *trans;
2735 struct btrfs_dir_item *dir_item;
2736 struct extent_buffer *leaf;
2737 struct btrfs_key key;
2738 struct btrfs_disk_key disk_key;
2739 struct inode_record *dir_rec;
2740 unsigned long name_ptr;
2741 u32 data_size = sizeof(*dir_item) + backref->namelen;
2744 trans = btrfs_start_transaction(root, 1);
2746 return PTR_ERR(trans);
2748 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
2749 (unsigned long long)rec->ino);
2751 btrfs_init_path(&path);
2752 key.objectid = backref->dir;
2753 key.type = BTRFS_DIR_INDEX_KEY;
2754 key.offset = backref->index;
2755 ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
2758 leaf = path.nodes[0];
2759 dir_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_dir_item);
2761 disk_key.objectid = cpu_to_le64(rec->ino);
2762 disk_key.type = BTRFS_INODE_ITEM_KEY;
2763 disk_key.offset = 0;
2765 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
2766 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
2767 btrfs_set_dir_data_len(leaf, dir_item, 0);
2768 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
2769 name_ptr = (unsigned long)(dir_item + 1);
2770 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
2771 btrfs_mark_buffer_dirty(leaf);
2772 btrfs_release_path(&path);
2773 btrfs_commit_transaction(trans, root);
2775 backref->found_dir_index = 1;
2776 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
2777 BUG_ON(IS_ERR(dir_rec));
2780 dir_rec->found_size += backref->namelen;
2781 if (dir_rec->found_size == dir_rec->isize &&
2782 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
2783 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2784 if (dir_rec->found_size != dir_rec->isize)
2785 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
2790 static int delete_dir_index(struct btrfs_root *root,
2791 struct inode_backref *backref)
2793 struct btrfs_trans_handle *trans;
2794 struct btrfs_dir_item *di;
2795 struct btrfs_path path;
2798 trans = btrfs_start_transaction(root, 1);
2800 return PTR_ERR(trans);
2802 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
2803 (unsigned long long)backref->dir,
2804 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
2805 (unsigned long long)root->objectid);
2807 btrfs_init_path(&path);
2808 di = btrfs_lookup_dir_index(trans, root, &path, backref->dir,
2809 backref->name, backref->namelen,
2810 backref->index, -1);
2813 btrfs_release_path(&path);
2814 btrfs_commit_transaction(trans, root);
2821 ret = btrfs_del_item(trans, root, &path);
2823 ret = btrfs_delete_one_dir_name(trans, root, &path, di);
2825 btrfs_release_path(&path);
2826 btrfs_commit_transaction(trans, root);
2830 static int __create_inode_item(struct btrfs_trans_handle *trans,
2831 struct btrfs_root *root, u64 ino, u64 size,
2832 u64 nbytes, u64 nlink, u32 mode)
2834 struct btrfs_inode_item ii;
2835 time_t now = time(NULL);
2838 btrfs_set_stack_inode_size(&ii, size);
2839 btrfs_set_stack_inode_nbytes(&ii, nbytes);
2840 btrfs_set_stack_inode_nlink(&ii, nlink);
2841 btrfs_set_stack_inode_mode(&ii, mode);
2842 btrfs_set_stack_inode_generation(&ii, trans->transid);
2843 btrfs_set_stack_timespec_nsec(&ii.atime, 0);
2844 btrfs_set_stack_timespec_sec(&ii.ctime, now);
2845 btrfs_set_stack_timespec_nsec(&ii.ctime, 0);
2846 btrfs_set_stack_timespec_sec(&ii.mtime, now);
2847 btrfs_set_stack_timespec_nsec(&ii.mtime, 0);
2848 btrfs_set_stack_timespec_sec(&ii.otime, 0);
2849 btrfs_set_stack_timespec_nsec(&ii.otime, 0);
2851 ret = btrfs_insert_inode(trans, root, ino, &ii);
2854 warning("root %llu inode %llu recreating inode item, this may "
2855 "be incomplete, please check permissions and content after "
2856 "the fsck completes.\n", (unsigned long long)root->objectid,
2857 (unsigned long long)ino);
2862 static int create_inode_item_lowmem(struct btrfs_trans_handle *trans,
2863 struct btrfs_root *root, u64 ino,
2866 u32 mode = (filetype == BTRFS_FT_DIR ? S_IFDIR : S_IFREG) | 0755;
2868 return __create_inode_item(trans, root, ino, 0, 0, 0, mode);
2871 static int create_inode_item(struct btrfs_root *root,
2872 struct inode_record *rec, int root_dir)
2874 struct btrfs_trans_handle *trans;
2880 trans = btrfs_start_transaction(root, 1);
2881 if (IS_ERR(trans)) {
2882 ret = PTR_ERR(trans);
2886 nlink = root_dir ? 1 : rec->found_link;
2887 if (rec->found_dir_item) {
2888 if (rec->found_file_extent)
2889 fprintf(stderr, "root %llu inode %llu has both a dir "
2890 "item and extents, unsure if it is a dir or a "
2891 "regular file so setting it as a directory\n",
2892 (unsigned long long)root->objectid,
2893 (unsigned long long)rec->ino);
2894 mode = S_IFDIR | 0755;
2895 size = rec->found_size;
2896 } else if (!rec->found_dir_item) {
2897 size = rec->extent_end;
2898 mode = S_IFREG | 0755;
2901 ret = __create_inode_item(trans, root, rec->ino, size, rec->nbytes,
2903 btrfs_commit_transaction(trans, root);
2907 static int repair_inode_backrefs(struct btrfs_root *root,
2908 struct inode_record *rec,
2909 struct cache_tree *inode_cache,
2912 struct inode_backref *tmp, *backref;
2913 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2917 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2918 if (!delete && rec->ino == root_dirid) {
2919 if (!rec->found_inode_item) {
2920 ret = create_inode_item(root, rec, 1);
2927 /* Index 0 for root dir's are special, don't mess with it */
2928 if (rec->ino == root_dirid && backref->index == 0)
2932 ((backref->found_dir_index && !backref->found_inode_ref) ||
2933 (backref->found_dir_index && backref->found_inode_ref &&
2934 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
2935 ret = delete_dir_index(root, backref);
2939 list_del(&backref->list);
2944 if (!delete && !backref->found_dir_index &&
2945 backref->found_dir_item && backref->found_inode_ref) {
2946 ret = add_missing_dir_index(root, inode_cache, rec,
2951 if (backref->found_dir_item &&
2952 backref->found_dir_index) {
2953 if (!backref->errors &&
2954 backref->found_inode_ref) {
2955 list_del(&backref->list);
2962 if (!delete && (!backref->found_dir_index &&
2963 !backref->found_dir_item &&
2964 backref->found_inode_ref)) {
2965 struct btrfs_trans_handle *trans;
2966 struct btrfs_key location;
2968 ret = check_dir_conflict(root, backref->name,
2974 * let nlink fixing routine to handle it,
2975 * which can do it better.
2980 location.objectid = rec->ino;
2981 location.type = BTRFS_INODE_ITEM_KEY;
2982 location.offset = 0;
2984 trans = btrfs_start_transaction(root, 1);
2985 if (IS_ERR(trans)) {
2986 ret = PTR_ERR(trans);
2989 fprintf(stderr, "adding missing dir index/item pair "
2991 (unsigned long long)rec->ino);
2992 ret = btrfs_insert_dir_item(trans, root, backref->name,
2994 backref->dir, &location,
2995 imode_to_type(rec->imode),
2998 btrfs_commit_transaction(trans, root);
3002 if (!delete && (backref->found_inode_ref &&
3003 backref->found_dir_index &&
3004 backref->found_dir_item &&
3005 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
3006 !rec->found_inode_item)) {
3007 ret = create_inode_item(root, rec, 0);
3014 return ret ? ret : repaired;
3018 * To determine the file type for nlink/inode_item repair
3020 * Return 0 if file type is found and BTRFS_FT_* is stored into type.
3021 * Return -ENOENT if file type is not found.
3023 static int find_file_type(struct inode_record *rec, u8 *type)
3025 struct inode_backref *backref;
3027 /* For inode item recovered case */
3028 if (rec->found_inode_item) {
3029 *type = imode_to_type(rec->imode);
3033 list_for_each_entry(backref, &rec->backrefs, list) {
3034 if (backref->found_dir_index || backref->found_dir_item) {
3035 *type = backref->filetype;
3043 * To determine the file name for nlink repair
3045 * Return 0 if file name is found, set name and namelen.
3046 * Return -ENOENT if file name is not found.
3048 static int find_file_name(struct inode_record *rec,
3049 char *name, int *namelen)
3051 struct inode_backref *backref;
3053 list_for_each_entry(backref, &rec->backrefs, list) {
3054 if (backref->found_dir_index || backref->found_dir_item ||
3055 backref->found_inode_ref) {
3056 memcpy(name, backref->name, backref->namelen);
3057 *namelen = backref->namelen;
3064 /* Reset the nlink of the inode to the correct one */
3065 static int reset_nlink(struct btrfs_trans_handle *trans,
3066 struct btrfs_root *root,
3067 struct btrfs_path *path,
3068 struct inode_record *rec)
3070 struct inode_backref *backref;
3071 struct inode_backref *tmp;
3072 struct btrfs_key key;
3073 struct btrfs_inode_item *inode_item;
3076 /* We don't believe this either, reset it and iterate backref */
3077 rec->found_link = 0;
3079 /* Remove all backref including the valid ones */
3080 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
3081 ret = btrfs_unlink(trans, root, rec->ino, backref->dir,
3082 backref->index, backref->name,
3083 backref->namelen, 0);
3087 /* remove invalid backref, so it won't be added back */
3088 if (!(backref->found_dir_index &&
3089 backref->found_dir_item &&
3090 backref->found_inode_ref)) {
3091 list_del(&backref->list);
3098 /* Set nlink to 0 */
3099 key.objectid = rec->ino;
3100 key.type = BTRFS_INODE_ITEM_KEY;
3102 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3109 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3110 struct btrfs_inode_item);
3111 btrfs_set_inode_nlink(path->nodes[0], inode_item, 0);
3112 btrfs_mark_buffer_dirty(path->nodes[0]);
3113 btrfs_release_path(path);
3116 * Add back valid inode_ref/dir_item/dir_index,
3117 * add_link() will handle the nlink inc, so new nlink must be correct
3119 list_for_each_entry(backref, &rec->backrefs, list) {
3120 ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
3121 backref->name, backref->namelen,
3122 backref->filetype, &backref->index, 1, 0);
3127 btrfs_release_path(path);
3131 static int get_highest_inode(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root,
3133 struct btrfs_path *path,
3136 struct btrfs_key key, found_key;
3139 btrfs_init_path(path);
3140 key.objectid = BTRFS_LAST_FREE_OBJECTID;
3142 key.type = BTRFS_INODE_ITEM_KEY;
3143 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3145 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
3146 path->slots[0] - 1);
3147 *highest_ino = found_key.objectid;
3150 if (*highest_ino >= BTRFS_LAST_FREE_OBJECTID)
3152 btrfs_release_path(path);
3157 * Link inode to dir 'lost+found'. Increase @ref_count.
3159 * Returns 0 means success.
3160 * Returns <0 means failure.
3162 static int link_inode_to_lostfound(struct btrfs_trans_handle *trans,
3163 struct btrfs_root *root,
3164 struct btrfs_path *path,
3165 u64 ino, char *namebuf, u32 name_len,
3166 u8 filetype, u64 *ref_count)
3168 char *dir_name = "lost+found";
3173 btrfs_release_path(path);
3174 ret = get_highest_inode(trans, root, path, &lost_found_ino);
3179 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
3180 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
3183 error("failed to create '%s' dir: %s", dir_name, strerror(-ret));
3186 ret = btrfs_add_link(trans, root, ino, lost_found_ino,
3187 namebuf, name_len, filetype, NULL, 1, 0);
3189 * Add ".INO" suffix several times to handle case where
3190 * "FILENAME.INO" is already taken by another file.
3192 while (ret == -EEXIST) {
3194 * Conflicting file name, add ".INO" as suffix * +1 for '.'
3196 if (name_len + count_digits(ino) + 1 > BTRFS_NAME_LEN) {
3200 snprintf(namebuf + name_len, BTRFS_NAME_LEN - name_len,
3202 name_len += count_digits(ino) + 1;
3203 ret = btrfs_add_link(trans, root, ino, lost_found_ino, namebuf,
3204 name_len, filetype, NULL, 1, 0);
3207 error("failed to link the inode %llu to %s dir: %s",
3208 ino, dir_name, strerror(-ret));
3213 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
3214 name_len, namebuf, dir_name);
3216 btrfs_release_path(path);
3218 error("failed to move file '%.*s' to '%s' dir", name_len,
3223 static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
3224 struct btrfs_root *root,
3225 struct btrfs_path *path,
3226 struct inode_record *rec)
3228 char namebuf[BTRFS_NAME_LEN] = {0};
3231 int name_recovered = 0;
3232 int type_recovered = 0;
3236 * Get file name and type first before these invalid inode ref
3237 * are deleted by remove_all_invalid_backref()
3239 name_recovered = !find_file_name(rec, namebuf, &namelen);
3240 type_recovered = !find_file_type(rec, &type);
3242 if (!name_recovered) {
3243 printf("Can't get file name for inode %llu, using '%llu' as fallback\n",
3244 rec->ino, rec->ino);
3245 namelen = count_digits(rec->ino);
3246 sprintf(namebuf, "%llu", rec->ino);
3249 if (!type_recovered) {
3250 printf("Can't get file type for inode %llu, using FILE as fallback\n",
3252 type = BTRFS_FT_REG_FILE;
3256 ret = reset_nlink(trans, root, path, rec);
3259 "Failed to reset nlink for inode %llu: %s\n",
3260 rec->ino, strerror(-ret));
3264 if (rec->found_link == 0) {
3265 ret = link_inode_to_lostfound(trans, root, path, rec->ino,
3266 namebuf, namelen, type,
3267 (u64 *)&rec->found_link);
3271 printf("Fixed the nlink of inode %llu\n", rec->ino);
3274 * Clear the flag anyway, or we will loop forever for the same inode
3275 * as it will not be removed from the bad inode list and the dead loop
3278 rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
3279 btrfs_release_path(path);
3284 * Check if there is any normal(reg or prealloc) file extent for given
3286 * This is used to determine the file type when neither its dir_index/item or
3287 * inode_item exists.
3289 * This will *NOT* report error, if any error happens, just consider it does
3290 * not have any normal file extent.
3292 static int find_normal_file_extent(struct btrfs_root *root, u64 ino)
3294 struct btrfs_path path;
3295 struct btrfs_key key;
3296 struct btrfs_key found_key;
3297 struct btrfs_file_extent_item *fi;
3301 btrfs_init_path(&path);
3303 key.type = BTRFS_EXTENT_DATA_KEY;
3306 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3311 if (ret && path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
3312 ret = btrfs_next_leaf(root, &path);
3319 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
3321 if (found_key.objectid != ino ||
3322 found_key.type != BTRFS_EXTENT_DATA_KEY)
3324 fi = btrfs_item_ptr(path.nodes[0], path.slots[0],
3325 struct btrfs_file_extent_item);
3326 type = btrfs_file_extent_type(path.nodes[0], fi);
3327 if (type != BTRFS_FILE_EXTENT_INLINE) {
3333 btrfs_release_path(&path);
3337 static u32 btrfs_type_to_imode(u8 type)
3339 static u32 imode_by_btrfs_type[] = {
3340 [BTRFS_FT_REG_FILE] = S_IFREG,
3341 [BTRFS_FT_DIR] = S_IFDIR,
3342 [BTRFS_FT_CHRDEV] = S_IFCHR,
3343 [BTRFS_FT_BLKDEV] = S_IFBLK,
3344 [BTRFS_FT_FIFO] = S_IFIFO,
3345 [BTRFS_FT_SOCK] = S_IFSOCK,
3346 [BTRFS_FT_SYMLINK] = S_IFLNK,
3349 return imode_by_btrfs_type[(type)];
3352 static int repair_inode_no_item(struct btrfs_trans_handle *trans,
3353 struct btrfs_root *root,
3354 struct btrfs_path *path,
3355 struct inode_record *rec)
3359 int type_recovered = 0;
3362 printf("Trying to rebuild inode:%llu\n", rec->ino);
3364 type_recovered = !find_file_type(rec, &filetype);
3367 * Try to determine inode type if type not found.
3369 * For found regular file extent, it must be FILE.
3370 * For found dir_item/index, it must be DIR.
3372 * For undetermined one, use FILE as fallback.
3375 * 1. If found backref(inode_index/item is already handled) to it,
3377 * Need new inode-inode ref structure to allow search for that.
3379 if (!type_recovered) {
3380 if (rec->found_file_extent &&
3381 find_normal_file_extent(root, rec->ino)) {
3383 filetype = BTRFS_FT_REG_FILE;
3384 } else if (rec->found_dir_item) {
3386 filetype = BTRFS_FT_DIR;
3387 } else if (!list_empty(&rec->orphan_extents)) {
3389 filetype = BTRFS_FT_REG_FILE;
3391 printf("Can't determine the filetype for inode %llu, assume it is a normal file\n",
3394 filetype = BTRFS_FT_REG_FILE;
3398 ret = btrfs_new_inode(trans, root, rec->ino,
3399 mode | btrfs_type_to_imode(filetype));
3404 * Here inode rebuild is done, we only rebuild the inode item,
3405 * don't repair the nlink(like move to lost+found).
3406 * That is the job of nlink repair.
3408 * We just fill the record and return
3410 rec->found_dir_item = 1;
3411 rec->imode = mode | btrfs_type_to_imode(filetype);
3413 rec->errors &= ~I_ERR_NO_INODE_ITEM;
3414 /* Ensure the inode_nlinks repair function will be called */
3415 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3420 static int repair_inode_orphan_extent(struct btrfs_trans_handle *trans,
3421 struct btrfs_root *root,
3422 struct btrfs_path *path,
3423 struct inode_record *rec)
3425 struct orphan_data_extent *orphan;
3426 struct orphan_data_extent *tmp;
3429 list_for_each_entry_safe(orphan, tmp, &rec->orphan_extents, list) {
3431 * Check for conflicting file extents
3433 * Here we don't know whether the extents is compressed or not,
3434 * so we can only assume it not compressed nor data offset,
3435 * and use its disk_len as extent length.
3437 ret = btrfs_get_extent(NULL, root, path, orphan->objectid,
3438 orphan->offset, orphan->disk_len, 0);
3439 btrfs_release_path(path);
3444 "orphan extent (%llu, %llu) conflicts, delete the orphan\n",
3445 orphan->disk_bytenr, orphan->disk_len);
3446 ret = btrfs_free_extent(trans,
3447 root->fs_info->extent_root,
3448 orphan->disk_bytenr, orphan->disk_len,
3449 0, root->objectid, orphan->objectid,
3454 ret = btrfs_insert_file_extent(trans, root, orphan->objectid,
3455 orphan->offset, orphan->disk_bytenr,
3456 orphan->disk_len, orphan->disk_len);
3460 /* Update file size info */
3461 rec->found_size += orphan->disk_len;
3462 if (rec->found_size == rec->nbytes)
3463 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
3465 /* Update the file extent hole info too */
3466 ret = del_file_extent_hole(&rec->holes, orphan->offset,
3470 if (RB_EMPTY_ROOT(&rec->holes))
3471 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3473 list_del(&orphan->list);
3476 rec->errors &= ~I_ERR_FILE_EXTENT_ORPHAN;
3481 static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
3482 struct btrfs_root *root,
3483 struct btrfs_path *path,
3484 struct inode_record *rec)
3486 struct rb_node *node;
3487 struct file_extent_hole *hole;
3491 node = rb_first(&rec->holes);
3495 hole = rb_entry(node, struct file_extent_hole, node);
3496 ret = btrfs_punch_hole(trans, root, rec->ino,
3497 hole->start, hole->len);
3500 ret = del_file_extent_hole(&rec->holes, hole->start,
3504 if (RB_EMPTY_ROOT(&rec->holes))
3505 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3506 node = rb_first(&rec->holes);
3508 /* special case for a file losing all its file extent */
3510 ret = btrfs_punch_hole(trans, root, rec->ino, 0,
3511 round_up(rec->isize,
3512 root->fs_info->sectorsize));
3516 printf("Fixed discount file extents for inode: %llu in root: %llu\n",
3517 rec->ino, root->objectid);
3522 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
3524 struct btrfs_trans_handle *trans;
3525 struct btrfs_path path;
3528 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |
3529 I_ERR_NO_ORPHAN_ITEM |
3530 I_ERR_LINK_COUNT_WRONG |
3531 I_ERR_NO_INODE_ITEM |
3532 I_ERR_FILE_EXTENT_ORPHAN |
3533 I_ERR_FILE_EXTENT_DISCOUNT|
3534 I_ERR_FILE_NBYTES_WRONG)))
3538 * For nlink repair, it may create a dir and add link, so
3539 * 2 for parent(256)'s dir_index and dir_item
3540 * 2 for lost+found dir's inode_item and inode_ref
3541 * 1 for the new inode_ref of the file
3542 * 2 for lost+found dir's dir_index and dir_item for the file
3544 trans = btrfs_start_transaction(root, 7);
3546 return PTR_ERR(trans);
3548 btrfs_init_path(&path);
3549 if (rec->errors & I_ERR_NO_INODE_ITEM)
3550 ret = repair_inode_no_item(trans, root, &path, rec);
3551 if (!ret && rec->errors & I_ERR_FILE_EXTENT_ORPHAN)
3552 ret = repair_inode_orphan_extent(trans, root, &path, rec);
3553 if (!ret && rec->errors & I_ERR_FILE_EXTENT_DISCOUNT)
3554 ret = repair_inode_discount_extent(trans, root, &path, rec);
3555 if (!ret && rec->errors & I_ERR_DIR_ISIZE_WRONG)
3556 ret = repair_inode_isize(trans, root, &path, rec);
3557 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
3558 ret = repair_inode_orphan_item(trans, root, &path, rec);
3559 if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
3560 ret = repair_inode_nlinks(trans, root, &path, rec);
3561 if (!ret && rec->errors & I_ERR_FILE_NBYTES_WRONG)
3562 ret = repair_inode_nbytes(trans, root, &path, rec);
3563 btrfs_commit_transaction(trans, root);
3564 btrfs_release_path(&path);
3568 static int check_inode_recs(struct btrfs_root *root,
3569 struct cache_tree *inode_cache)
3571 struct cache_extent *cache;
3572 struct ptr_node *node;
3573 struct inode_record *rec;
3574 struct inode_backref *backref;
3579 u64 root_dirid = btrfs_root_dirid(&root->root_item);
3581 if (btrfs_root_refs(&root->root_item) == 0) {
3582 if (!cache_tree_empty(inode_cache))
3583 fprintf(stderr, "warning line %d\n", __LINE__);
3588 * We need to repair backrefs first because we could change some of the
3589 * errors in the inode recs.
3591 * We also need to go through and delete invalid backrefs first and then
3592 * add the correct ones second. We do this because we may get EEXIST
3593 * when adding back the correct index because we hadn't yet deleted the
3596 * For example, if we were missing a dir index then the directories
3597 * isize would be wrong, so if we fixed the isize to what we thought it
3598 * would be and then fixed the backref we'd still have a invalid fs, so
3599 * we need to add back the dir index and then check to see if the isize
3604 if (stage == 3 && !err)
3607 cache = search_cache_extent(inode_cache, 0);
3608 while (repair && cache) {
3609 node = container_of(cache, struct ptr_node, cache);
3611 cache = next_cache_extent(cache);
3613 /* Need to free everything up and rescan */
3615 remove_cache_extent(inode_cache, &node->cache);
3617 free_inode_rec(rec);
3621 if (list_empty(&rec->backrefs))
3624 ret = repair_inode_backrefs(root, rec, inode_cache,
3638 rec = get_inode_rec(inode_cache, root_dirid, 0);
3639 BUG_ON(IS_ERR(rec));
3641 ret = check_root_dir(rec);
3643 fprintf(stderr, "root %llu root dir %llu error\n",
3644 (unsigned long long)root->root_key.objectid,
3645 (unsigned long long)root_dirid);
3646 print_inode_error(root, rec);
3651 struct btrfs_trans_handle *trans;
3653 trans = btrfs_start_transaction(root, 1);
3654 if (IS_ERR(trans)) {
3655 err = PTR_ERR(trans);
3660 "root %llu missing its root dir, recreating\n",
3661 (unsigned long long)root->objectid);
3663 ret = btrfs_make_root_dir(trans, root, root_dirid);
3666 btrfs_commit_transaction(trans, root);
3670 fprintf(stderr, "root %llu root dir %llu not found\n",
3671 (unsigned long long)root->root_key.objectid,
3672 (unsigned long long)root_dirid);
3676 cache = search_cache_extent(inode_cache, 0);
3679 node = container_of(cache, struct ptr_node, cache);
3681 remove_cache_extent(inode_cache, &node->cache);
3683 if (rec->ino == root_dirid ||
3684 rec->ino == BTRFS_ORPHAN_OBJECTID) {
3685 free_inode_rec(rec);
3689 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
3690 ret = check_orphan_item(root, rec->ino);
3692 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
3693 if (can_free_inode_rec(rec)) {
3694 free_inode_rec(rec);
3699 if (!rec->found_inode_item)
3700 rec->errors |= I_ERR_NO_INODE_ITEM;
3701 if (rec->found_link != rec->nlink)
3702 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3704 ret = try_repair_inode(root, rec);
3705 if (ret == 0 && can_free_inode_rec(rec)) {
3706 free_inode_rec(rec);
3712 if (!(repair && ret == 0))
3714 print_inode_error(root, rec);
3715 list_for_each_entry(backref, &rec->backrefs, list) {
3716 if (!backref->found_dir_item)
3717 backref->errors |= REF_ERR_NO_DIR_ITEM;
3718 if (!backref->found_dir_index)
3719 backref->errors |= REF_ERR_NO_DIR_INDEX;
3720 if (!backref->found_inode_ref)
3721 backref->errors |= REF_ERR_NO_INODE_REF;
3722 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
3723 " namelen %u name %s filetype %d errors %x",
3724 (unsigned long long)backref->dir,
3725 (unsigned long long)backref->index,
3726 backref->namelen, backref->name,
3727 backref->filetype, backref->errors);
3728 print_ref_error(backref->errors);
3730 free_inode_rec(rec);
3732 return (error > 0) ? -1 : 0;
3735 static struct root_record *get_root_rec(struct cache_tree *root_cache,
3738 struct cache_extent *cache;
3739 struct root_record *rec = NULL;
3742 cache = lookup_cache_extent(root_cache, objectid, 1);
3744 rec = container_of(cache, struct root_record, cache);
3746 rec = calloc(1, sizeof(*rec));
3748 return ERR_PTR(-ENOMEM);
3749 rec->objectid = objectid;
3750 INIT_LIST_HEAD(&rec->backrefs);
3751 rec->cache.start = objectid;
3752 rec->cache.size = 1;
3754 ret = insert_cache_extent(root_cache, &rec->cache);
3756 return ERR_PTR(-EEXIST);
3761 static struct root_backref *get_root_backref(struct root_record *rec,
3762 u64 ref_root, u64 dir, u64 index,
3763 const char *name, int namelen)
3765 struct root_backref *backref;
3767 list_for_each_entry(backref, &rec->backrefs, list) {
3768 if (backref->ref_root != ref_root || backref->dir != dir ||
3769 backref->namelen != namelen)
3771 if (memcmp(name, backref->name, namelen))
3776 backref = calloc(1, sizeof(*backref) + namelen + 1);
3779 backref->ref_root = ref_root;
3781 backref->index = index;
3782 backref->namelen = namelen;
3783 memcpy(backref->name, name, namelen);
3784 backref->name[namelen] = '\0';
3785 list_add_tail(&backref->list, &rec->backrefs);
3789 static void free_root_record(struct cache_extent *cache)
3791 struct root_record *rec;
3792 struct root_backref *backref;
3794 rec = container_of(cache, struct root_record, cache);
3795 while (!list_empty(&rec->backrefs)) {
3796 backref = to_root_backref(rec->backrefs.next);
3797 list_del(&backref->list);
3804 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
3806 static int add_root_backref(struct cache_tree *root_cache,
3807 u64 root_id, u64 ref_root, u64 dir, u64 index,
3808 const char *name, int namelen,
3809 int item_type, int errors)
3811 struct root_record *rec;
3812 struct root_backref *backref;
3814 rec = get_root_rec(root_cache, root_id);
3815 BUG_ON(IS_ERR(rec));
3816 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
3819 backref->errors |= errors;
3821 if (item_type != BTRFS_DIR_ITEM_KEY) {
3822 if (backref->found_dir_index || backref->found_back_ref ||
3823 backref->found_forward_ref) {
3824 if (backref->index != index)
3825 backref->errors |= REF_ERR_INDEX_UNMATCH;
3827 backref->index = index;
3831 if (item_type == BTRFS_DIR_ITEM_KEY) {
3832 if (backref->found_forward_ref)
3834 backref->found_dir_item = 1;
3835 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
3836 backref->found_dir_index = 1;
3837 } else if (item_type == BTRFS_ROOT_REF_KEY) {
3838 if (backref->found_forward_ref)
3839 backref->errors |= REF_ERR_DUP_ROOT_REF;
3840 else if (backref->found_dir_item)
3842 backref->found_forward_ref = 1;
3843 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
3844 if (backref->found_back_ref)
3845 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
3846 backref->found_back_ref = 1;
3851 if (backref->found_forward_ref && backref->found_dir_item)
3852 backref->reachable = 1;
3856 static int merge_root_recs(struct btrfs_root *root,
3857 struct cache_tree *src_cache,
3858 struct cache_tree *dst_cache)
3860 struct cache_extent *cache;
3861 struct ptr_node *node;
3862 struct inode_record *rec;
3863 struct inode_backref *backref;
3866 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
3867 free_inode_recs_tree(src_cache);
3872 cache = search_cache_extent(src_cache, 0);
3875 node = container_of(cache, struct ptr_node, cache);
3877 remove_cache_extent(src_cache, &node->cache);
3880 ret = is_child_root(root, root->objectid, rec->ino);
3886 list_for_each_entry(backref, &rec->backrefs, list) {
3887 BUG_ON(backref->found_inode_ref);
3888 if (backref->found_dir_item)
3889 add_root_backref(dst_cache, rec->ino,
3890 root->root_key.objectid, backref->dir,
3891 backref->index, backref->name,
3892 backref->namelen, BTRFS_DIR_ITEM_KEY,
3894 if (backref->found_dir_index)
3895 add_root_backref(dst_cache, rec->ino,
3896 root->root_key.objectid, backref->dir,
3897 backref->index, backref->name,
3898 backref->namelen, BTRFS_DIR_INDEX_KEY,
3902 free_inode_rec(rec);
3909 static int check_root_refs(struct btrfs_root *root,
3910 struct cache_tree *root_cache)
3912 struct root_record *rec;
3913 struct root_record *ref_root;
3914 struct root_backref *backref;
3915 struct cache_extent *cache;
3921 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
3922 BUG_ON(IS_ERR(rec));
3925 /* fixme: this can not detect circular references */
3928 cache = search_cache_extent(root_cache, 0);
3932 rec = container_of(cache, struct root_record, cache);
3933 cache = next_cache_extent(cache);
3935 if (rec->found_ref == 0)
3938 list_for_each_entry(backref, &rec->backrefs, list) {
3939 if (!backref->reachable)
3942 ref_root = get_root_rec(root_cache,
3944 BUG_ON(IS_ERR(ref_root));
3945 if (ref_root->found_ref > 0)
3948 backref->reachable = 0;
3950 if (rec->found_ref == 0)
3956 cache = search_cache_extent(root_cache, 0);
3960 rec = container_of(cache, struct root_record, cache);
3961 cache = next_cache_extent(cache);
3963 if (rec->found_ref == 0 &&
3964 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3965 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
3966 ret = check_orphan_item(root->fs_info->tree_root,
3972 * If we don't have a root item then we likely just have
3973 * a dir item in a snapshot for this root but no actual
3974 * ref key or anything so it's meaningless.
3976 if (!rec->found_root_item)
3979 fprintf(stderr, "fs tree %llu not referenced\n",
3980 (unsigned long long)rec->objectid);
3984 if (rec->found_ref > 0 && !rec->found_root_item)
3986 list_for_each_entry(backref, &rec->backrefs, list) {
3987 if (!backref->found_dir_item)
3988 backref->errors |= REF_ERR_NO_DIR_ITEM;
3989 if (!backref->found_dir_index)
3990 backref->errors |= REF_ERR_NO_DIR_INDEX;
3991 if (!backref->found_back_ref)
3992 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
3993 if (!backref->found_forward_ref)
3994 backref->errors |= REF_ERR_NO_ROOT_REF;
3995 if (backref->reachable && backref->errors)
4002 fprintf(stderr, "fs tree %llu refs %u %s\n",
4003 (unsigned long long)rec->objectid, rec->found_ref,
4004 rec->found_root_item ? "" : "not found");
4006 list_for_each_entry(backref, &rec->backrefs, list) {
4007 if (!backref->reachable)
4009 if (!backref->errors && rec->found_root_item)
4011 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
4012 " index %llu namelen %u name %s errors %x\n",
4013 (unsigned long long)backref->ref_root,
4014 (unsigned long long)backref->dir,
4015 (unsigned long long)backref->index,
4016 backref->namelen, backref->name,
4018 print_ref_error(backref->errors);
4021 return errors > 0 ? 1 : 0;
4024 static int process_root_ref(struct extent_buffer *eb, int slot,
4025 struct btrfs_key *key,
4026 struct cache_tree *root_cache)
4032 struct btrfs_root_ref *ref;
4033 char namebuf[BTRFS_NAME_LEN];
4036 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
4038 dirid = btrfs_root_ref_dirid(eb, ref);
4039 index = btrfs_root_ref_sequence(eb, ref);
4040 name_len = btrfs_root_ref_name_len(eb, ref);
4042 if (name_len <= BTRFS_NAME_LEN) {
4046 len = BTRFS_NAME_LEN;
4047 error = REF_ERR_NAME_TOO_LONG;
4049 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
4051 if (key->type == BTRFS_ROOT_REF_KEY) {
4052 add_root_backref(root_cache, key->offset, key->objectid, dirid,
4053 index, namebuf, len, key->type, error);
4055 add_root_backref(root_cache, key->objectid, key->offset, dirid,
4056 index, namebuf, len, key->type, error);
4061 static void free_corrupt_block(struct cache_extent *cache)
4063 struct btrfs_corrupt_block *corrupt;
4065 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
4069 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
4072 * Repair the btree of the given root.
4074 * The fix is to remove the node key in corrupt_blocks cache_tree.
4075 * and rebalance the tree.
4076 * After the fix, the btree should be writeable.
4078 static int repair_btree(struct btrfs_root *root,
4079 struct cache_tree *corrupt_blocks)
4081 struct btrfs_trans_handle *trans;
4082 struct btrfs_path path;
4083 struct btrfs_corrupt_block *corrupt;
4084 struct cache_extent *cache;
4085 struct btrfs_key key;
4090 if (cache_tree_empty(corrupt_blocks))
4093 trans = btrfs_start_transaction(root, 1);
4094 if (IS_ERR(trans)) {
4095 ret = PTR_ERR(trans);
4096 fprintf(stderr, "Error starting transaction: %s\n",
4100 btrfs_init_path(&path);
4101 cache = first_cache_extent(corrupt_blocks);
4103 corrupt = container_of(cache, struct btrfs_corrupt_block,
4105 level = corrupt->level;
4106 path.lowest_level = level;
4107 key.objectid = corrupt->key.objectid;
4108 key.type = corrupt->key.type;
4109 key.offset = corrupt->key.offset;
4112 * Here we don't want to do any tree balance, since it may
4113 * cause a balance with corrupted brother leaf/node,
4114 * so ins_len set to 0 here.
4115 * Balance will be done after all corrupt node/leaf is deleted.
4117 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
4120 offset = btrfs_node_blockptr(path.nodes[level],
4123 /* Remove the ptr */
4124 ret = btrfs_del_ptr(root, &path, level, path.slots[level]);
4128 * Remove the corresponding extent
4129 * return value is not concerned.
4131 btrfs_release_path(&path);
4132 ret = btrfs_free_extent(trans, root, offset,
4133 root->fs_info->nodesize, 0,
4134 root->root_key.objectid, level - 1, 0);
4135 cache = next_cache_extent(cache);
4138 /* Balance the btree using btrfs_search_slot() */
4139 cache = first_cache_extent(corrupt_blocks);
4141 corrupt = container_of(cache, struct btrfs_corrupt_block,
4143 memcpy(&key, &corrupt->key, sizeof(key));
4144 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
4147 /* return will always >0 since it won't find the item */
4149 btrfs_release_path(&path);
4150 cache = next_cache_extent(cache);
4153 btrfs_commit_transaction(trans, root);
4154 btrfs_release_path(&path);
4158 static int check_fs_root(struct btrfs_root *root,
4159 struct cache_tree *root_cache,
4160 struct walk_control *wc)
4166 struct btrfs_path path;
4167 struct shared_node root_node;
4168 struct root_record *rec;
4169 struct btrfs_root_item *root_item = &root->root_item;
4170 struct cache_tree corrupt_blocks;
4171 struct orphan_data_extent *orphan;
4172 struct orphan_data_extent *tmp;
4173 enum btrfs_tree_block_status status;
4174 struct node_refs nrefs;
4177 * Reuse the corrupt_block cache tree to record corrupted tree block
4179 * Unlike the usage in extent tree check, here we do it in a per
4180 * fs/subvol tree base.
4182 cache_tree_init(&corrupt_blocks);
4183 root->fs_info->corrupt_blocks = &corrupt_blocks;
4185 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
4186 rec = get_root_rec(root_cache, root->root_key.objectid);
4187 BUG_ON(IS_ERR(rec));
4188 if (btrfs_root_refs(root_item) > 0)
4189 rec->found_root_item = 1;
4192 btrfs_init_path(&path);
4193 memset(&root_node, 0, sizeof(root_node));
4194 cache_tree_init(&root_node.root_cache);
4195 cache_tree_init(&root_node.inode_cache);
4196 memset(&nrefs, 0, sizeof(nrefs));
4198 /* Move the orphan extent record to corresponding inode_record */
4199 list_for_each_entry_safe(orphan, tmp,
4200 &root->orphan_data_extents, list) {
4201 struct inode_record *inode;
4203 inode = get_inode_rec(&root_node.inode_cache, orphan->objectid,
4205 BUG_ON(IS_ERR(inode));
4206 inode->errors |= I_ERR_FILE_EXTENT_ORPHAN;
4207 list_move(&orphan->list, &inode->orphan_extents);
4210 level = btrfs_header_level(root->node);
4211 memset(wc->nodes, 0, sizeof(wc->nodes));
4212 wc->nodes[level] = &root_node;
4213 wc->active_node = level;
4214 wc->root_level = level;
4216 /* We may not have checked the root block, lets do that now */
4217 if (btrfs_is_leaf(root->node))
4218 status = btrfs_check_leaf(root, NULL, root->node);
4220 status = btrfs_check_node(root, NULL, root->node);
4221 if (status != BTRFS_TREE_BLOCK_CLEAN)
4224 if (btrfs_root_refs(root_item) > 0 ||
4225 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4226 path.nodes[level] = root->node;
4227 extent_buffer_get(root->node);
4228 path.slots[level] = 0;
4230 struct btrfs_key key;
4231 struct btrfs_disk_key found_key;
4233 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
4234 level = root_item->drop_level;
4235 path.lowest_level = level;
4236 if (level > btrfs_header_level(root->node) ||
4237 level >= BTRFS_MAX_LEVEL) {
4238 error("ignoring invalid drop level: %u", level);
4241 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4244 btrfs_node_key(path.nodes[level], &found_key,
4246 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
4247 sizeof(found_key)));
4251 wret = walk_down_tree(root, &path, wc, &level, &nrefs);
4257 wret = walk_up_tree(root, &path, wc, &level);
4264 btrfs_release_path(&path);
4266 if (!cache_tree_empty(&corrupt_blocks)) {
4267 struct cache_extent *cache;
4268 struct btrfs_corrupt_block *corrupt;
4270 printf("The following tree block(s) is corrupted in tree %llu:\n",
4271 root->root_key.objectid);
4272 cache = first_cache_extent(&corrupt_blocks);
4274 corrupt = container_of(cache,
4275 struct btrfs_corrupt_block,
4277 printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n",
4278 cache->start, corrupt->level,
4279 corrupt->key.objectid, corrupt->key.type,
4280 corrupt->key.offset);
4281 cache = next_cache_extent(cache);
4284 printf("Try to repair the btree for root %llu\n",
4285 root->root_key.objectid);
4286 ret = repair_btree(root, &corrupt_blocks);
4288 fprintf(stderr, "Failed to repair btree: %s\n",
4291 printf("Btree for root %llu is fixed\n",
4292 root->root_key.objectid);
4296 err = merge_root_recs(root, &root_node.root_cache, root_cache);
4300 if (root_node.current) {
4301 root_node.current->checked = 1;
4302 maybe_free_inode_rec(&root_node.inode_cache,
4306 err = check_inode_recs(root, &root_node.inode_cache);
4310 free_corrupt_blocks_tree(&corrupt_blocks);
4311 root->fs_info->corrupt_blocks = NULL;
4312 free_orphan_data_extents(&root->orphan_data_extents);
4316 static int fs_root_objectid(u64 objectid)
4318 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
4319 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
4321 return is_fstree(objectid);
4324 static int check_fs_roots(struct btrfs_fs_info *fs_info,
4325 struct cache_tree *root_cache)
4327 struct btrfs_path path;
4328 struct btrfs_key key;
4329 struct walk_control wc;
4330 struct extent_buffer *leaf, *tree_node;
4331 struct btrfs_root *tmp_root;
4332 struct btrfs_root *tree_root = fs_info->tree_root;
4336 if (ctx.progress_enabled) {
4337 ctx.tp = TASK_FS_ROOTS;
4338 task_start(ctx.info);
4342 * Just in case we made any changes to the extent tree that weren't
4343 * reflected into the free space cache yet.
4346 reset_cached_block_groups(fs_info);
4347 memset(&wc, 0, sizeof(wc));
4348 cache_tree_init(&wc.shared);
4349 btrfs_init_path(&path);
4354 key.type = BTRFS_ROOT_ITEM_KEY;
4355 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4360 tree_node = tree_root->node;
4362 if (tree_node != tree_root->node) {
4363 free_root_recs_tree(root_cache);
4364 btrfs_release_path(&path);
4367 leaf = path.nodes[0];
4368 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
4369 ret = btrfs_next_leaf(tree_root, &path);
4375 leaf = path.nodes[0];
4377 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
4378 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4379 fs_root_objectid(key.objectid)) {
4380 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4381 tmp_root = btrfs_read_fs_root_no_cache(
4384 key.offset = (u64)-1;
4385 tmp_root = btrfs_read_fs_root(
4388 if (IS_ERR(tmp_root)) {
4392 ret = check_fs_root(tmp_root, root_cache, &wc);
4393 if (ret == -EAGAIN) {
4394 free_root_recs_tree(root_cache);
4395 btrfs_release_path(&path);
4400 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4401 btrfs_free_fs_root(tmp_root);
4402 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4403 key.type == BTRFS_ROOT_BACKREF_KEY) {
4404 process_root_ref(leaf, path.slots[0], &key,
4411 btrfs_release_path(&path);
4413 free_extent_cache_tree(&wc.shared);
4414 if (!cache_tree_empty(&wc.shared))
4415 fprintf(stderr, "warning line %d\n", __LINE__);
4417 task_stop(ctx.info);
4423 * Find the @index according by @ino and name.
4424 * Notice:time efficiency is O(N)
4426 * @root: the root of the fs/file tree
4427 * @index_ret: the index as return value
4428 * @namebuf: the name to match
4429 * @name_len: the length of name to match
4430 * @file_type: the file_type of INODE_ITEM to match
4432 * Returns 0 if found and *@index_ret will be modified with right value
4433 * Returns< 0 not found and *@index_ret will be (u64)-1
4435 static int find_dir_index(struct btrfs_root *root, u64 dirid, u64 location_id,
4436 u64 *index_ret, char *namebuf, u32 name_len,
4439 struct btrfs_path path;
4440 struct extent_buffer *node;
4441 struct btrfs_dir_item *di;
4442 struct btrfs_key key;
4443 struct btrfs_key location;
4444 char name[BTRFS_NAME_LEN] = {0};
4456 /* search from the last index */
4457 key.objectid = dirid;
4458 key.offset = (u64)-1;
4459 key.type = BTRFS_DIR_INDEX_KEY;
4461 btrfs_init_path(&path);
4462 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4467 ret = btrfs_previous_item(root, &path, dirid, BTRFS_DIR_INDEX_KEY);
4470 *index_ret = (64)-1;
4473 /* Check whether inode_id/filetype/name match */
4474 node = path.nodes[0];
4475 slot = path.slots[0];
4476 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4477 total = btrfs_item_size_nr(node, slot);
4478 while (cur < total) {
4480 len = btrfs_dir_name_len(node, di);
4481 data_len = btrfs_dir_data_len(node, di);
4483 btrfs_dir_item_key_to_cpu(node, di, &location);
4484 if (location.objectid != location_id ||
4485 location.type != BTRFS_INODE_ITEM_KEY ||
4486 location.offset != 0)
4489 filetype = btrfs_dir_type(node, di);
4490 if (file_type != filetype)
4493 if (len > BTRFS_NAME_LEN)
4494 len = BTRFS_NAME_LEN;
4496 read_extent_buffer(node, name, (unsigned long)(di + 1), len);
4497 if (len != name_len || strncmp(namebuf, name, len))
4500 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
4501 *index_ret = key.offset;
4505 len += sizeof(*di) + data_len;
4506 di = (struct btrfs_dir_item *)((char *)di + len);
4512 btrfs_release_path(&path);
4517 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
4518 * INODE_REF/INODE_EXTREF match.
4520 * @root: the root of the fs/file tree
4521 * @key: the key of the DIR_ITEM/DIR_INDEX, key->offset will be right
4522 * value while find index
4523 * @location_key: location key of the struct btrfs_dir_item to match
4524 * @name: the name to match
4525 * @namelen: the length of name
4526 * @file_type: the type of file to math
4528 * Return 0 if no error occurred.
4529 * Return DIR_ITEM_MISSING/DIR_INDEX_MISSING if couldn't find
4530 * DIR_ITEM/DIR_INDEX
4531 * Return DIR_ITEM_MISMATCH/DIR_INDEX_MISMATCH if INODE_REF/INODE_EXTREF
4532 * and DIR_ITEM/DIR_INDEX mismatch
4534 static int find_dir_item(struct btrfs_root *root, struct btrfs_key *key,
4535 struct btrfs_key *location_key, char *name,
4536 u32 namelen, u8 file_type)
4538 struct btrfs_path path;
4539 struct extent_buffer *node;
4540 struct btrfs_dir_item *di;
4541 struct btrfs_key location;
4542 char namebuf[BTRFS_NAME_LEN] = {0};
4551 /* get the index by traversing all index */
4552 if (key->type == BTRFS_DIR_INDEX_KEY && key->offset == (u64)-1) {
4553 ret = find_dir_index(root, key->objectid,
4554 location_key->objectid, &key->offset,
4555 name, namelen, file_type);
4557 ret = DIR_INDEX_MISSING;
4561 btrfs_init_path(&path);
4562 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4564 ret = key->type == BTRFS_DIR_ITEM_KEY ? DIR_ITEM_MISSING :
4569 /* Check whether inode_id/filetype/name match */
4570 node = path.nodes[0];
4571 slot = path.slots[0];
4572 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4573 total = btrfs_item_size_nr(node, slot);
4574 while (cur < total) {
4575 ret = key->type == BTRFS_DIR_ITEM_KEY ?
4576 DIR_ITEM_MISMATCH : DIR_INDEX_MISMATCH;
4578 len = btrfs_dir_name_len(node, di);
4579 data_len = btrfs_dir_data_len(node, di);
4581 btrfs_dir_item_key_to_cpu(node, di, &location);
4582 if (location.objectid != location_key->objectid ||
4583 location.type != location_key->type ||
4584 location.offset != location_key->offset)
4587 filetype = btrfs_dir_type(node, di);
4588 if (file_type != filetype)
4591 if (len > BTRFS_NAME_LEN) {
4592 len = BTRFS_NAME_LEN;
4593 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
4595 key->type == BTRFS_DIR_ITEM_KEY ?
4596 "DIR_ITEM" : "DIR_INDEX",
4597 key->objectid, key->offset, len);
4599 read_extent_buffer(node, namebuf, (unsigned long)(di + 1),
4601 if (len != namelen || strncmp(namebuf, name, len))
4607 len += sizeof(*di) + data_len;
4608 di = (struct btrfs_dir_item *)((char *)di + len);
4613 btrfs_release_path(&path);
4618 * Prints inode ref error message
4620 static void print_inode_ref_err(struct btrfs_root *root, struct btrfs_key *key,
4621 u64 index, const char *namebuf, int name_len,
4622 u8 filetype, int err)
4627 /* root dir error */
4628 if (key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
4630 "root %llu root dir shouldn't have INODE REF[%llu %llu] name %s",
4631 root->objectid, key->objectid, key->offset, namebuf);
4636 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING))
4637 error("root %llu DIR ITEM[%llu %llu] %s name %s filetype %u",
4638 root->objectid, key->offset,
4639 btrfs_name_hash(namebuf, name_len),
4640 err & DIR_ITEM_MISMATCH ? "mismatch" : "missing",
4642 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING))
4643 error("root %llu DIR INDEX[%llu %llu] %s name %s filetype %u",
4644 root->objectid, key->offset, index,
4645 err & DIR_ITEM_MISMATCH ? "mismatch" : "missing",
4650 * Insert the missing inode item.
4652 * Returns 0 means success.
4653 * Returns <0 means error.
4655 static int repair_inode_item_missing(struct btrfs_root *root, u64 ino,
4658 struct btrfs_key key;
4659 struct btrfs_trans_handle *trans;
4660 struct btrfs_path path;
4664 key.type = BTRFS_INODE_ITEM_KEY;
4667 btrfs_init_path(&path);
4668 trans = btrfs_start_transaction(root, 1);
4669 if (IS_ERR(trans)) {
4674 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
4675 if (ret < 0 || !ret)
4678 /* insert inode item */
4679 create_inode_item_lowmem(trans, root, ino, filetype);
4682 btrfs_commit_transaction(trans, root);
4685 error("failed to repair root %llu INODE ITEM[%llu] missing",
4686 root->objectid, ino);
4687 btrfs_release_path(&path);
4692 * The ternary means dir item, dir index and relative inode ref.
4693 * The function handles errs: INODE_MISSING, DIR_INDEX_MISSING
4694 * DIR_INDEX_MISMATCH, DIR_ITEM_MISSING, DIR_ITEM_MISMATCH by the follow
4696 * If two of three is missing or mismatched, delete the existing one.
4697 * If one of three is missing or mismatched, add the missing one.
4699 * returns 0 means success.
4700 * returns not 0 means on error;
4702 int repair_ternary_lowmem(struct btrfs_root *root, u64 dir_ino, u64 ino,
4703 u64 index, char *name, int name_len, u8 filetype,
4706 struct btrfs_trans_handle *trans;
4711 * stage shall be one of following valild values:
4712 * 0: Fine, nothing to do.
4713 * 1: One of three is wrong, so add missing one.
4714 * 2: Two of three is wrong, so delete existed one.
4716 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING))
4718 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING))
4720 if (err & (INODE_REF_MISSING))
4723 /* stage must be smllarer than 3 */
4726 trans = btrfs_start_transaction(root, 1);
4728 ret = btrfs_unlink(trans, root, ino, dir_ino, index, name,
4733 ret = btrfs_add_link(trans, root, ino, dir_ino, name, name_len,
4734 filetype, &index, 1, 1);
4738 btrfs_commit_transaction(trans, root);
4741 error("fail to repair inode %llu name %s filetype %u",
4742 ino, name, filetype);
4744 printf("%s ref/dir_item of inode %llu name %s filetype %u\n",
4745 stage == 2 ? "Delete" : "Add",
4746 ino, name, filetype);
4752 * Traverse the given INODE_REF and call find_dir_item() to find related
4753 * DIR_ITEM/DIR_INDEX.
4755 * @root: the root of the fs/file tree
4756 * @ref_key: the key of the INODE_REF
4757 * @path the path provides node and slot
4758 * @refs: the count of INODE_REF
4759 * @mode: the st_mode of INODE_ITEM
4760 * @name_ret: returns with the first ref's name
4761 * @name_len_ret: len of the name_ret
4763 * Return 0 if no error occurred.
4765 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4766 struct btrfs_path *path, char *name_ret,
4767 u32 *namelen_ret, u64 *refs_ret, int mode)
4769 struct btrfs_key key;
4770 struct btrfs_key location;
4771 struct btrfs_inode_ref *ref;
4772 struct extent_buffer *node;
4773 char namebuf[BTRFS_NAME_LEN] = {0};
4783 int need_research = 0;
4791 /* since after repair, path and the dir item may be changed */
4792 if (need_research) {
4794 btrfs_release_path(path);
4795 ret = btrfs_search_slot(NULL, root, ref_key, path, 0, 0);
4796 /* the item was deleted, let path point to the last checked item */
4798 if (path->slots[0] == 0)
4799 btrfs_prev_leaf(root, path);
4807 location.objectid = ref_key->objectid;
4808 location.type = BTRFS_INODE_ITEM_KEY;
4809 location.offset = 0;
4810 node = path->nodes[0];
4811 slot = path->slots[0];
4813 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
4814 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4815 total = btrfs_item_size_nr(node, slot);
4818 /* Update inode ref count */
4821 index = btrfs_inode_ref_index(node, ref);
4822 name_len = btrfs_inode_ref_name_len(node, ref);
4824 if (name_len <= BTRFS_NAME_LEN) {
4827 len = BTRFS_NAME_LEN;
4828 warning("root %llu INODE_REF[%llu %llu] name too long",
4829 root->objectid, ref_key->objectid, ref_key->offset);
4832 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
4834 /* copy the first name found to name_ret */
4835 if (refs == 1 && name_ret) {
4836 memcpy(name_ret, namebuf, len);
4840 /* Check root dir ref */
4841 if (ref_key->objectid == BTRFS_FIRST_FREE_OBJECTID) {
4842 if (index != 0 || len != strlen("..") ||
4843 strncmp("..", namebuf, len) ||
4844 ref_key->offset != BTRFS_FIRST_FREE_OBJECTID) {
4845 /* set err bits then repair will delete the ref */
4846 err |= DIR_INDEX_MISSING;
4847 err |= DIR_ITEM_MISSING;
4852 /* Find related DIR_INDEX */
4853 key.objectid = ref_key->offset;
4854 key.type = BTRFS_DIR_INDEX_KEY;
4856 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
4857 imode_to_type(mode));
4859 /* Find related dir_item */
4860 key.objectid = ref_key->offset;
4861 key.type = BTRFS_DIR_ITEM_KEY;
4862 key.offset = btrfs_name_hash(namebuf, len);
4863 tmp_err |= find_dir_item(root, &key, &location, namebuf, len,
4864 imode_to_type(mode));
4866 if (tmp_err && repair) {
4867 ret = repair_ternary_lowmem(root, ref_key->offset,
4868 ref_key->objectid, index, namebuf,
4869 name_len, imode_to_type(mode),
4876 print_inode_ref_err(root, ref_key, index, namebuf, name_len,
4877 imode_to_type(mode), tmp_err);
4879 len = sizeof(*ref) + name_len;
4880 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4891 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
4892 * DIR_ITEM/DIR_INDEX.
4894 * @root: the root of the fs/file tree
4895 * @ref_key: the key of the INODE_EXTREF
4896 * @refs: the count of INODE_EXTREF
4897 * @mode: the st_mode of INODE_ITEM
4899 * Return 0 if no error occurred.
4901 static int check_inode_extref(struct btrfs_root *root,
4902 struct btrfs_key *ref_key,
4903 struct extent_buffer *node, int slot, u64 *refs,
4906 struct btrfs_key key;
4907 struct btrfs_key location;
4908 struct btrfs_inode_extref *extref;
4909 char namebuf[BTRFS_NAME_LEN] = {0};
4919 location.objectid = ref_key->objectid;
4920 location.type = BTRFS_INODE_ITEM_KEY;
4921 location.offset = 0;
4923 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4924 total = btrfs_item_size_nr(node, slot);
4927 /* update inode ref count */
4929 name_len = btrfs_inode_extref_name_len(node, extref);
4930 index = btrfs_inode_extref_index(node, extref);
4931 parent = btrfs_inode_extref_parent(node, extref);
4932 if (name_len <= BTRFS_NAME_LEN) {
4935 len = BTRFS_NAME_LEN;
4936 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
4937 root->objectid, ref_key->objectid, ref_key->offset);
4939 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
4941 /* Check root dir ref name */
4942 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4943 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
4944 root->objectid, ref_key->objectid, ref_key->offset,
4946 err |= ROOT_DIR_ERROR;
4949 /* find related dir_index */
4950 key.objectid = parent;
4951 key.type = BTRFS_DIR_INDEX_KEY;
4953 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
4956 /* find related dir_item */
4957 key.objectid = parent;
4958 key.type = BTRFS_DIR_ITEM_KEY;
4959 key.offset = btrfs_name_hash(namebuf, len);
4960 ret = find_dir_item(root, &key, &location, namebuf, len, mode);
4963 len = sizeof(*extref) + name_len;
4964 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4974 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
4975 * DIR_ITEM/DIR_INDEX match.
4976 * Return with @index_ret.
4978 * @root: the root of the fs/file tree
4979 * @key: the key of the INODE_REF/INODE_EXTREF
4980 * @name: the name in the INODE_REF/INODE_EXTREF
4981 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4982 * @index_ret: the index in the INODE_REF/INODE_EXTREF,
4983 * value (64)-1 means do not check index
4984 * @ext_ref: the EXTENDED_IREF feature
4986 * Return 0 if no error occurred.
4987 * Return >0 for error bitmap
4989 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
4990 char *name, int namelen, u64 *index_ret,
4991 unsigned int ext_ref)
4993 struct btrfs_path path;
4994 struct btrfs_inode_ref *ref;
4995 struct btrfs_inode_extref *extref;
4996 struct extent_buffer *node;
4997 char ref_namebuf[BTRFS_NAME_LEN] = {0};
5010 btrfs_init_path(&path);
5011 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
5013 ret = INODE_REF_MISSING;
5017 node = path.nodes[0];
5018 slot = path.slots[0];
5020 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
5021 total = btrfs_item_size_nr(node, slot);
5023 /* Iterate all entry of INODE_REF */
5024 while (cur < total) {
5025 ret = INODE_REF_MISSING;
5027 ref_namelen = btrfs_inode_ref_name_len(node, ref);
5028 ref_index = btrfs_inode_ref_index(node, ref);
5029 if (*index_ret != (u64)-1 && *index_ret != ref_index)
5032 if (cur + sizeof(*ref) + ref_namelen > total ||
5033 ref_namelen > BTRFS_NAME_LEN) {
5034 warning("root %llu INODE %s[%llu %llu] name too long",
5036 key->type == BTRFS_INODE_REF_KEY ?
5038 key->objectid, key->offset);
5040 if (cur + sizeof(*ref) > total)
5042 len = min_t(u32, total - cur - sizeof(*ref),
5048 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
5051 if (len != namelen || strncmp(ref_namebuf, name, len))
5054 *index_ret = ref_index;
5058 len = sizeof(*ref) + ref_namelen;
5059 ref = (struct btrfs_inode_ref *)((char *)ref + len);
5064 /* Skip if not support EXTENDED_IREF feature */
5068 btrfs_release_path(&path);
5069 btrfs_init_path(&path);
5071 dir_id = key->offset;
5072 key->type = BTRFS_INODE_EXTREF_KEY;
5073 key->offset = btrfs_extref_hash(dir_id, name, namelen);
5075 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
5077 ret = INODE_REF_MISSING;
5081 node = path.nodes[0];
5082 slot = path.slots[0];
5084 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
5086 total = btrfs_item_size_nr(node, slot);
5088 /* Iterate all entry of INODE_EXTREF */
5089 while (cur < total) {
5090 ret = INODE_REF_MISSING;
5092 ref_namelen = btrfs_inode_extref_name_len(node, extref);
5093 ref_index = btrfs_inode_extref_index(node, extref);
5094 parent = btrfs_inode_extref_parent(node, extref);
5095 if (*index_ret != (u64)-1 && *index_ret != ref_index)
5098 if (parent != dir_id)
5101 if (ref_namelen <= BTRFS_NAME_LEN) {
5104 len = BTRFS_NAME_LEN;
5105 warning("root %llu INODE %s[%llu %llu] name too long",
5107 key->type == BTRFS_INODE_REF_KEY ?
5109 key->objectid, key->offset);
5111 read_extent_buffer(node, ref_namebuf,
5112 (unsigned long)(extref + 1), len);
5114 if (len != namelen || strncmp(ref_namebuf, name, len))
5117 *index_ret = ref_index;
5122 len = sizeof(*extref) + ref_namelen;
5123 extref = (struct btrfs_inode_extref *)((char *)extref + len);
5128 btrfs_release_path(&path);
5132 static void print_dir_item_err(struct btrfs_root *root, struct btrfs_key *key,
5133 u64 ino, u64 index, const char *namebuf,
5134 int name_len, u8 filetype, int err)
5136 if (err & (DIR_ITEM_MISMATCH | DIR_ITEM_MISSING)) {
5137 error("root %llu DIR ITEM[%llu %llu] name %s filetype %d %s",
5138 root->objectid, key->objectid, key->offset, namebuf,
5140 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
5143 if (err & (DIR_INDEX_MISMATCH | DIR_INDEX_MISSING)) {
5144 error("root %llu DIR INDEX[%llu %llu] name %s filetype %d %s",
5145 root->objectid, key->objectid, index, namebuf, filetype,
5146 err & DIR_ITEM_MISMATCH ? "mismath" : "missing");
5149 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH)) {
5151 "root %llu INODE_ITEM[%llu] index %llu name %s filetype %d %s",
5152 root->objectid, ino, index, namebuf, filetype,
5153 err & INODE_ITEM_MISMATCH ? "mismath" : "missing");
5156 if (err & INODE_REF_MISSING)
5158 "root %llu INODE REF[%llu, %llu] name %s filetype %u missing",
5159 root->objectid, ino, key->objectid, namebuf, filetype);
5164 * Call repair_inode_item_missing and repair_ternary_lowmem to repair
5166 * Returns error after repair
5168 static int repair_dir_item(struct btrfs_root *root, u64 dirid, u64 ino,
5169 u64 index, u8 filetype, char *namebuf, u32 name_len,
5174 if (err & INODE_ITEM_MISSING) {
5175 ret = repair_inode_item_missing(root, ino, filetype);
5177 err &= ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING);
5180 if (err & ~(INODE_ITEM_MISMATCH | INODE_ITEM_MISSING)) {
5181 ret = repair_ternary_lowmem(root, dirid, ino, index, namebuf,
5182 name_len, filetype, err);
5184 err &= ~(DIR_INDEX_MISMATCH | DIR_INDEX_MISSING);
5185 err &= ~(DIR_ITEM_MISMATCH | DIR_ITEM_MISSING);
5186 err &= ~(INODE_REF_MISSING);
5192 static int __count_dir_isize(struct btrfs_root *root, u64 ino, int type,
5195 struct btrfs_key key;
5196 struct btrfs_path path;
5198 struct btrfs_dir_item *di;
5208 key.offset = (u64)-1;
5210 btrfs_init_path(&path);
5211 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
5216 /* if found, go to spacial case */
5221 ret = btrfs_previous_item(root, &path, ino, type);
5229 di = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_dir_item);
5231 total = btrfs_item_size_nr(path.nodes[0], path.slots[0]);
5233 while (cur < total) {
5234 len = btrfs_dir_name_len(path.nodes[0], di);
5235 if (len > BTRFS_NAME_LEN)
5236 len = BTRFS_NAME_LEN;
5239 len += btrfs_dir_data_len(path.nodes[0], di);
5241 di = (struct btrfs_dir_item *)((char *)di + len);
5247 btrfs_release_path(&path);
5251 static int count_dir_isize(struct btrfs_root *root, u64 ino, u64 *size)
5258 ret = __count_dir_isize(root, ino, BTRFS_DIR_ITEM_KEY, &item_size);
5262 ret = __count_dir_isize(root, ino, BTRFS_DIR_INDEX_KEY, &index_size);
5266 *size = item_size + index_size;
5270 error("failed to count root %llu INODE[%llu] root size",
5271 root->objectid, ino);
5276 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
5277 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
5279 * @root: the root of the fs/file tree
5280 * @key: the key of the INODE_REF/INODE_EXTREF
5282 * @size: the st_size of the INODE_ITEM
5283 * @ext_ref: the EXTENDED_IREF feature
5285 * Return 0 if no error occurred.
5286 * Return DIR_COUNT_AGAIN if the isize of the inode should be recalculated.
5288 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *di_key,
5289 struct btrfs_path *path, u64 *size,
5290 unsigned int ext_ref)
5292 struct btrfs_dir_item *di;
5293 struct btrfs_inode_item *ii;
5294 struct btrfs_key key;
5295 struct btrfs_key location;
5296 struct extent_buffer *node;
5298 char namebuf[BTRFS_NAME_LEN] = {0};
5310 int need_research = 0;
5313 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
5314 * ignore index check.
5316 if (di_key->type == BTRFS_DIR_INDEX_KEY)
5317 index = di_key->offset;
5324 /* since after repair, path and the dir item may be changed */
5325 if (need_research) {
5327 err |= DIR_COUNT_AGAIN;
5328 btrfs_release_path(path);
5329 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
5330 /* the item was deleted, let path point the last checked item */
5332 if (path->slots[0] == 0)
5333 btrfs_prev_leaf(root, path);
5341 node = path->nodes[0];
5342 slot = path->slots[0];
5344 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
5345 total = btrfs_item_size_nr(node, slot);
5346 memset(namebuf, 0, sizeof(namebuf) / sizeof(*namebuf));
5348 while (cur < total) {
5349 data_len = btrfs_dir_data_len(node, di);
5352 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
5354 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
5355 di_key->objectid, di_key->offset, data_len);
5357 name_len = btrfs_dir_name_len(node, di);
5358 if (name_len <= BTRFS_NAME_LEN) {
5361 len = BTRFS_NAME_LEN;
5362 warning("root %llu %s[%llu %llu] name too long",
5364 di_key->type == BTRFS_DIR_ITEM_KEY ? "DIR_ITEM" : "DIR_INDEX",
5365 di_key->objectid, di_key->offset);
5367 (*size) += name_len;
5368 read_extent_buffer(node, namebuf, (unsigned long)(di + 1),
5370 filetype = btrfs_dir_type(node, di);
5372 if (di_key->type == BTRFS_DIR_ITEM_KEY &&
5373 di_key->offset != btrfs_name_hash(namebuf, len)) {
5375 error("root %llu DIR_ITEM[%llu %llu] name %s namelen %u filetype %u mismatch with its hash, wanted %llu have %llu",
5376 root->objectid, di_key->objectid, di_key->offset,
5377 namebuf, len, filetype, di_key->offset,
5378 btrfs_name_hash(namebuf, len));
5381 btrfs_dir_item_key_to_cpu(node, di, &location);
5382 /* Ignore related ROOT_ITEM check */
5383 if (location.type == BTRFS_ROOT_ITEM_KEY)
5386 btrfs_release_path(path);
5387 /* Check relative INODE_ITEM(existence/filetype) */
5388 ret = btrfs_search_slot(NULL, root, &location, path, 0, 0);
5390 tmp_err |= INODE_ITEM_MISSING;
5394 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
5395 struct btrfs_inode_item);
5396 mode = btrfs_inode_mode(path->nodes[0], ii);
5397 if (imode_to_type(mode) != filetype) {
5398 tmp_err |= INODE_ITEM_MISMATCH;
5402 /* Check relative INODE_REF/INODE_EXTREF */
5403 key.objectid = location.objectid;
5404 key.type = BTRFS_INODE_REF_KEY;
5405 key.offset = di_key->objectid;
5406 tmp_err |= find_inode_ref(root, &key, namebuf, len,
5409 /* check relative INDEX/ITEM */
5410 key.objectid = di_key->objectid;
5411 if (key.type == BTRFS_DIR_ITEM_KEY) {
5412 key.type = BTRFS_DIR_INDEX_KEY;
5415 key.type = BTRFS_DIR_ITEM_KEY;
5416 key.offset = btrfs_name_hash(namebuf, name_len);
5419 tmp_err |= find_dir_item(root, &key, &location, namebuf,
5420 name_len, filetype);
5421 /* find_dir_item may find index */
5422 if (key.type == BTRFS_DIR_INDEX_KEY)
5426 if (tmp_err && repair) {
5427 ret = repair_dir_item(root, di_key->objectid,
5428 location.objectid, index,
5429 imode_to_type(mode), namebuf,
5431 if (ret != tmp_err) {
5436 btrfs_release_path(path);
5437 print_dir_item_err(root, di_key, location.objectid, index,
5438 namebuf, name_len, filetype, tmp_err);
5440 len = sizeof(*di) + name_len + data_len;
5441 di = (struct btrfs_dir_item *)((char *)di + len);
5444 if (di_key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
5445 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
5446 root->objectid, di_key->objectid,
5453 btrfs_release_path(path);
5454 ret = btrfs_search_slot(NULL, root, di_key, path, 0, 0);
5456 err |= ret > 0 ? -ENOENT : ret;
5461 * Wrapper function of btrfs_punch_hole.
5463 * Returns 0 means success.
5464 * Returns not 0 means error.
5466 static int punch_extent_hole(struct btrfs_root *root, u64 ino, u64 start,
5469 struct btrfs_trans_handle *trans;
5472 trans = btrfs_start_transaction(root, 1);
5474 return PTR_ERR(trans);
5476 ret = btrfs_punch_hole(trans, root, ino, start, len);
5478 error("failed to add hole [%llu, %llu] in inode [%llu]",
5481 printf("Add a hole [%llu, %llu] in inode [%llu]\n", start, len,
5484 btrfs_commit_transaction(trans, root);
5489 * Check file extent datasum/hole, update the size of the file extents,
5490 * check and update the last offset of the file extent.
5492 * @root: the root of fs/file tree.
5493 * @fkey: the key of the file extent.
5494 * @nodatasum: INODE_NODATASUM feature.
5495 * @size: the sum of all EXTENT_DATA items size for this inode.
5496 * @end: the offset of the last extent.
5498 * Return 0 if no error occurred.
5500 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
5501 struct extent_buffer *node, int slot,
5502 unsigned int nodatasum, u64 *size, u64 *end)
5504 struct btrfs_file_extent_item *fi;
5507 u64 extent_num_bytes;
5509 u64 csum_found; /* In byte size, sectorsize aligned */
5510 u64 search_start; /* Logical range start we search for csum */
5511 u64 search_len; /* Logical range len we search for csum */
5512 unsigned int extent_type;
5513 unsigned int is_hole;
5518 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
5520 /* Check inline extent */
5521 extent_type = btrfs_file_extent_type(node, fi);
5522 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
5523 struct btrfs_item *e = btrfs_item_nr(slot);
5524 u32 item_inline_len;
5526 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
5527 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
5528 compressed = btrfs_file_extent_compression(node, fi);
5529 if (extent_num_bytes == 0) {
5531 "root %llu EXTENT_DATA[%llu %llu] has empty inline extent",
5532 root->objectid, fkey->objectid, fkey->offset);
5533 err |= FILE_EXTENT_ERROR;
5535 if (!compressed && extent_num_bytes != item_inline_len) {
5537 "root %llu EXTENT_DATA[%llu %llu] wrong inline size, have: %llu, expected: %u",
5538 root->objectid, fkey->objectid, fkey->offset,
5539 extent_num_bytes, item_inline_len);
5540 err |= FILE_EXTENT_ERROR;
5542 *end += extent_num_bytes;
5543 *size += extent_num_bytes;
5547 /* Check extent type */
5548 if (extent_type != BTRFS_FILE_EXTENT_REG &&
5549 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
5550 err |= FILE_EXTENT_ERROR;
5551 error("root %llu EXTENT_DATA[%llu %llu] type bad",
5552 root->objectid, fkey->objectid, fkey->offset);
5556 /* Check REG_EXTENT/PREALLOC_EXTENT */
5557 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
5558 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
5559 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
5560 extent_offset = btrfs_file_extent_offset(node, fi);
5561 compressed = btrfs_file_extent_compression(node, fi);
5562 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
5565 * Check EXTENT_DATA csum
5567 * For plain (uncompressed) extent, we should only check the range
5568 * we're referring to, as it's possible that part of prealloc extent
5569 * has been written, and has csum:
5571 * |<--- Original large preallocated extent A ---->|
5572 * |<- Prealloc File Extent ->|<- Regular Extent ->|
5575 * For compressed extent, we should check the whole range.
5578 search_start = disk_bytenr + extent_offset;
5579 search_len = extent_num_bytes;
5581 search_start = disk_bytenr;
5582 search_len = disk_num_bytes;
5584 ret = count_csum_range(root, search_start, search_len, &csum_found);
5585 if (csum_found > 0 && nodatasum) {
5586 err |= ODD_CSUM_ITEM;
5587 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
5588 root->objectid, fkey->objectid, fkey->offset);
5589 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
5590 !is_hole && (ret < 0 || csum_found < search_len)) {
5591 err |= CSUM_ITEM_MISSING;
5592 error("root %llu EXTENT_DATA[%llu %llu] csum missing, have: %llu, expected: %llu",
5593 root->objectid, fkey->objectid, fkey->offset,
5594 csum_found, search_len);
5595 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC && csum_found > 0) {
5596 err |= ODD_CSUM_ITEM;
5597 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have csum, but has: %llu",
5598 root->objectid, fkey->objectid, fkey->offset, csum_found);
5601 /* Check EXTENT_DATA hole */
5602 if (!no_holes && *end != fkey->offset) {
5604 ret = punch_extent_hole(root, fkey->objectid,
5605 *end, fkey->offset - *end);
5606 if (!repair || ret) {
5607 err |= FILE_EXTENT_ERROR;
5609 "root %llu EXTENT_DATA[%llu %llu] gap exists, expected: EXTENT_DATA[%llu %llu]",
5610 root->objectid, fkey->objectid, fkey->offset,
5611 fkey->objectid, *end);
5615 *end += extent_num_bytes;
5617 *size += extent_num_bytes;
5623 * Set inode item nbytes to @nbytes
5625 * Returns 0 on success
5626 * Returns != 0 on error
5628 static int repair_inode_nbytes_lowmem(struct btrfs_root *root,
5629 struct btrfs_path *path,
5630 u64 ino, u64 nbytes)
5632 struct btrfs_trans_handle *trans;
5633 struct btrfs_inode_item *ii;
5634 struct btrfs_key key;
5635 struct btrfs_key research_key;
5639 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
5642 key.type = BTRFS_INODE_ITEM_KEY;
5645 trans = btrfs_start_transaction(root, 1);
5646 if (IS_ERR(trans)) {
5647 ret = PTR_ERR(trans);
5652 btrfs_release_path(path);
5653 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5661 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
5662 struct btrfs_inode_item);
5663 btrfs_set_inode_nbytes(path->nodes[0], ii, nbytes);
5664 btrfs_mark_buffer_dirty(path->nodes[0]);
5666 btrfs_commit_transaction(trans, root);
5669 error("failed to set nbytes in inode %llu root %llu",
5670 ino, root->root_key.objectid);
5672 printf("Set nbytes in inode item %llu root %llu\n to %llu", ino,
5673 root->root_key.objectid, nbytes);
5676 btrfs_release_path(path);
5677 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
5684 * Set directory inode isize to @isize.
5686 * Returns 0 on success.
5687 * Returns != 0 on error.
5689 static int repair_dir_isize_lowmem(struct btrfs_root *root,
5690 struct btrfs_path *path,
5693 struct btrfs_trans_handle *trans;
5694 struct btrfs_inode_item *ii;
5695 struct btrfs_key key;
5696 struct btrfs_key research_key;
5700 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
5703 key.type = BTRFS_INODE_ITEM_KEY;
5706 trans = btrfs_start_transaction(root, 1);
5707 if (IS_ERR(trans)) {
5708 ret = PTR_ERR(trans);
5713 btrfs_release_path(path);
5714 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5722 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
5723 struct btrfs_inode_item);
5724 btrfs_set_inode_size(path->nodes[0], ii, isize);
5725 btrfs_mark_buffer_dirty(path->nodes[0]);
5727 btrfs_commit_transaction(trans, root);
5730 error("failed to set isize in inode %llu root %llu",
5731 ino, root->root_key.objectid);
5733 printf("Set isize in inode %llu root %llu to %llu\n",
5734 ino, root->root_key.objectid, isize);
5736 btrfs_release_path(path);
5737 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
5744 * Wrapper function for btrfs_add_orphan_item().
5746 * Returns 0 on success.
5747 * Returns != 0 on error.
5749 static int repair_inode_orphan_item_lowmem(struct btrfs_root *root,
5750 struct btrfs_path *path, u64 ino)
5752 struct btrfs_trans_handle *trans;
5753 struct btrfs_key research_key;
5757 btrfs_item_key_to_cpu(path->nodes[0], &research_key, path->slots[0]);
5759 trans = btrfs_start_transaction(root, 1);
5760 if (IS_ERR(trans)) {
5761 ret = PTR_ERR(trans);
5766 btrfs_release_path(path);
5767 ret = btrfs_add_orphan_item(trans, root, path, ino);
5769 btrfs_commit_transaction(trans, root);
5772 error("failed to add inode %llu as orphan item root %llu",
5773 ino, root->root_key.objectid);
5775 printf("Added inode %llu as orphan item root %llu\n",
5776 ino, root->root_key.objectid);
5778 btrfs_release_path(path);
5779 ret = btrfs_search_slot(NULL, root, &research_key, path, 0, 0);
5785 /* Set inode_item nlink to @ref_count.
5786 * If @ref_count == 0, move it to "lost+found" and increase @ref_count.
5788 * Returns 0 on success
5790 static int repair_inode_nlinks_lowmem(struct btrfs_root *root,
5791 struct btrfs_path *path, u64 ino,
5792 const char *name, u32 namelen,
5793 u64 ref_count, u8 filetype, u64 *nlink)
5795 struct btrfs_trans_handle *trans;
5796 struct btrfs_inode_item *ii;
5797 struct btrfs_key key;
5798 struct btrfs_key old_key;
5799 char namebuf[BTRFS_NAME_LEN] = {0};
5805 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
5807 if (name && namelen) {
5808 ASSERT(namelen <= BTRFS_NAME_LEN);
5809 memcpy(namebuf, name, namelen);
5812 sprintf(namebuf, "%llu", ino);
5813 name_len = count_digits(ino);
5814 printf("Can't find file name for inode %llu, use %s instead\n",
5818 trans = btrfs_start_transaction(root, 1);
5819 if (IS_ERR(trans)) {
5820 ret = PTR_ERR(trans);
5824 btrfs_release_path(path);
5825 /* if refs is 0, put it into lostfound */
5826 if (ref_count == 0) {
5827 ret = link_inode_to_lostfound(trans, root, path, ino, namebuf,
5828 name_len, filetype, &ref_count);
5833 /* reset inode_item's nlink to ref_count */
5835 key.type = BTRFS_INODE_ITEM_KEY;
5838 btrfs_release_path(path);
5839 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5845 ii = btrfs_item_ptr(path->nodes[0], path->slots[0],
5846 struct btrfs_inode_item);
5847 btrfs_set_inode_nlink(path->nodes[0], ii, ref_count);
5848 btrfs_mark_buffer_dirty(path->nodes[0]);
5853 btrfs_commit_transaction(trans, root);
5857 "fail to repair nlink of inode %llu root %llu name %s filetype %u",
5858 root->objectid, ino, namebuf, filetype);
5860 printf("Fixed nlink of inode %llu root %llu name %s filetype %u\n",
5861 root->objectid, ino, namebuf, filetype);
5864 btrfs_release_path(path);
5865 ret2 = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
5872 * Check INODE_ITEM and related ITEMs (the same inode number)
5873 * 1. check link count
5874 * 2. check inode ref/extref
5875 * 3. check dir item/index
5877 * @ext_ref: the EXTENDED_IREF feature
5879 * Return 0 if no error occurred.
5880 * Return >0 for error or hit the traversal is done(by error bitmap)
5882 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
5883 unsigned int ext_ref)
5885 struct extent_buffer *node;
5886 struct btrfs_inode_item *ii;
5887 struct btrfs_key key;
5888 struct btrfs_key last_key;
5897 u64 extent_size = 0;
5899 unsigned int nodatasum;
5903 char namebuf[BTRFS_NAME_LEN] = {0};
5906 node = path->nodes[0];
5907 slot = path->slots[0];
5909 btrfs_item_key_to_cpu(node, &key, slot);
5910 inode_id = key.objectid;
5912 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
5913 ret = btrfs_next_item(root, path);
5919 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
5920 isize = btrfs_inode_size(node, ii);
5921 nbytes = btrfs_inode_nbytes(node, ii);
5922 mode = btrfs_inode_mode(node, ii);
5923 dir = imode_to_type(mode) == BTRFS_FT_DIR;
5924 nlink = btrfs_inode_nlink(node, ii);
5925 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
5928 btrfs_item_key_to_cpu(path->nodes[0], &last_key, path->slots[0]);
5929 ret = btrfs_next_item(root, path);
5931 /* out will fill 'err' rusing current statistics */
5933 } else if (ret > 0) {
5938 node = path->nodes[0];
5939 slot = path->slots[0];
5940 btrfs_item_key_to_cpu(node, &key, slot);
5941 if (key.objectid != inode_id)
5945 case BTRFS_INODE_REF_KEY:
5946 ret = check_inode_ref(root, &key, path, namebuf,
5947 &name_len, &refs, mode);
5950 case BTRFS_INODE_EXTREF_KEY:
5951 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
5952 warning("root %llu EXTREF[%llu %llu] isn't supported",
5953 root->objectid, key.objectid,
5955 ret = check_inode_extref(root, &key, node, slot, &refs,
5959 case BTRFS_DIR_ITEM_KEY:
5960 case BTRFS_DIR_INDEX_KEY:
5962 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
5963 root->objectid, inode_id,
5964 imode_to_type(mode), key.objectid,
5967 ret = check_dir_item(root, &key, path, &size, ext_ref);
5970 case BTRFS_EXTENT_DATA_KEY:
5972 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
5973 root->objectid, inode_id, key.objectid,
5976 ret = check_file_extent(root, &key, node, slot,
5977 nodatasum, &extent_size,
5981 case BTRFS_XATTR_ITEM_KEY:
5984 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
5985 key.objectid, key.type, key.offset);
5990 if (err & LAST_ITEM) {
5991 btrfs_release_path(path);
5992 ret = btrfs_search_slot(NULL, root, &last_key, path, 0, 0);
5997 /* verify INODE_ITEM nlink/isize/nbytes */
5999 if (repair && (err & DIR_COUNT_AGAIN)) {
6000 err &= ~DIR_COUNT_AGAIN;
6001 count_dir_isize(root, inode_id, &size);
6004 if ((nlink != 1 || refs != 1) && repair) {
6005 ret = repair_inode_nlinks_lowmem(root, path, inode_id,
6006 namebuf, name_len, refs, imode_to_type(mode),
6011 err |= LINK_COUNT_ERROR;
6012 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
6013 root->objectid, inode_id, nlink);
6017 * Just a warning, as dir inode nbytes is just an
6018 * instructive value.
6020 if (!IS_ALIGNED(nbytes, root->fs_info->nodesize)) {
6021 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
6022 root->objectid, inode_id,
6023 root->fs_info->nodesize);
6026 if (isize != size) {
6028 ret = repair_dir_isize_lowmem(root, path,
6030 if (!repair || ret) {
6033 "root %llu DIR INODE [%llu] size %llu not equal to %llu",
6034 root->objectid, inode_id, isize, size);
6038 if (nlink != refs) {
6040 ret = repair_inode_nlinks_lowmem(root, path,
6041 inode_id, namebuf, name_len, refs,
6042 imode_to_type(mode), &nlink);
6043 if (!repair || ret) {
6044 err |= LINK_COUNT_ERROR;
6046 "root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
6047 root->objectid, inode_id, nlink, refs);
6049 } else if (!nlink) {
6051 ret = repair_inode_orphan_item_lowmem(root,
6053 if (!repair || ret) {
6055 error("root %llu INODE[%llu] is orphan item",
6056 root->objectid, inode_id);
6060 if (!nbytes && !no_holes && extent_end < isize) {
6062 ret = punch_extent_hole(root, inode_id,
6063 extent_end, isize - extent_end);
6064 if (!repair || ret) {
6065 err |= NBYTES_ERROR;
6067 "root %llu INODE[%llu] size %llu should have a file extent hole",
6068 root->objectid, inode_id, isize);
6072 if (nbytes != extent_size) {
6074 ret = repair_inode_nbytes_lowmem(root, path,
6075 inode_id, extent_size);
6076 if (!repair || ret) {
6077 err |= NBYTES_ERROR;
6079 "root %llu INODE[%llu] nbytes %llu not equal to extent_size %llu",
6080 root->objectid, inode_id, nbytes,
6086 if (err & LAST_ITEM)
6087 btrfs_next_item(root, path);
6092 * Insert the missing inode item and inode ref.
6094 * Normal INODE_ITEM_MISSING and INODE_REF_MISSING are handled in backref * dir.
6095 * Root dir should be handled specially because root dir is the root of fs.
6097 * returns err (>0 or 0) after repair
6099 static int repair_fs_first_inode(struct btrfs_root *root, int err)
6101 struct btrfs_trans_handle *trans;
6102 struct btrfs_key key;
6103 struct btrfs_path path;
6104 int filetype = BTRFS_FT_DIR;
6107 btrfs_init_path(&path);
6109 if (err & INODE_REF_MISSING) {
6110 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6111 key.type = BTRFS_INODE_REF_KEY;
6112 key.offset = BTRFS_FIRST_FREE_OBJECTID;
6114 trans = btrfs_start_transaction(root, 1);
6115 if (IS_ERR(trans)) {
6116 ret = PTR_ERR(trans);
6120 btrfs_release_path(&path);
6121 ret = btrfs_search_slot(trans, root, &key, &path, 1, 1);
6125 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
6126 BTRFS_FIRST_FREE_OBJECTID,
6127 BTRFS_FIRST_FREE_OBJECTID, 0);
6131 printf("Add INODE_REF[%llu %llu] name %s\n",
6132 BTRFS_FIRST_FREE_OBJECTID, BTRFS_FIRST_FREE_OBJECTID,
6134 err &= ~INODE_REF_MISSING;
6137 error("fail to insert first inode's ref");
6138 btrfs_commit_transaction(trans, root);
6141 if (err & INODE_ITEM_MISSING) {
6142 ret = repair_inode_item_missing(root,
6143 BTRFS_FIRST_FREE_OBJECTID, filetype);
6146 err &= ~INODE_ITEM_MISSING;
6150 error("fail to repair first inode");
6151 btrfs_release_path(&path);
6156 * check first root dir's inode_item and inode_ref
6158 * returns 0 means no error
6159 * returns >0 means error
6160 * returns <0 means fatal error
6162 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
6164 struct btrfs_path path;
6165 struct btrfs_key key;
6166 struct btrfs_inode_item *ii;
6172 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
6173 key.type = BTRFS_INODE_ITEM_KEY;
6176 /* For root being dropped, we don't need to check first inode */
6177 if (btrfs_root_refs(&root->root_item) == 0 &&
6178 btrfs_disk_key_objectid(&root->root_item.drop_progress) >=
6179 BTRFS_FIRST_FREE_OBJECTID)
6182 btrfs_init_path(&path);
6183 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6188 err |= INODE_ITEM_MISSING;
6190 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
6191 struct btrfs_inode_item);
6192 mode = btrfs_inode_mode(path.nodes[0], ii);
6193 if (imode_to_type(mode) != BTRFS_FT_DIR)
6194 err |= INODE_ITEM_MISMATCH;
6197 /* lookup first inode ref */
6198 key.offset = BTRFS_FIRST_FREE_OBJECTID;
6199 key.type = BTRFS_INODE_REF_KEY;
6200 /* special index value */
6203 ret = find_inode_ref(root, &key, "..", strlen(".."), &index, ext_ref);
6209 btrfs_release_path(&path);
6212 err = repair_fs_first_inode(root, err);
6214 if (err & (INODE_ITEM_MISSING | INODE_ITEM_MISMATCH))
6215 error("root dir INODE_ITEM is %s",
6216 err & INODE_ITEM_MISMATCH ? "mismatch" : "missing");
6217 if (err & INODE_REF_MISSING)
6218 error("root dir INODE_REF is missing");
6220 return ret < 0 ? ret : err;
6223 static struct tree_backref *find_tree_backref(struct extent_record *rec,
6224 u64 parent, u64 root)
6226 struct rb_node *node;
6227 struct tree_backref *back = NULL;
6228 struct tree_backref match = {
6235 match.parent = parent;
6236 match.node.full_backref = 1;
6241 node = rb_search(&rec->backref_tree, &match.node.node,
6242 (rb_compare_keys)compare_extent_backref, NULL);
6244 back = to_tree_backref(rb_node_to_extent_backref(node));
6249 static struct data_backref *find_data_backref(struct extent_record *rec,
6250 u64 parent, u64 root,
6251 u64 owner, u64 offset,
6253 u64 disk_bytenr, u64 bytes)
6255 struct rb_node *node;
6256 struct data_backref *back = NULL;
6257 struct data_backref match = {
6264 .found_ref = found_ref,
6265 .disk_bytenr = disk_bytenr,
6269 match.parent = parent;
6270 match.node.full_backref = 1;
6275 node = rb_search(&rec->backref_tree, &match.node.node,
6276 (rb_compare_keys)compare_extent_backref, NULL);
6278 back = to_data_backref(rb_node_to_extent_backref(node));
6283 * This function calls walk_down_tree_v2 and walk_up_tree_v2 to check tree
6284 * blocks and integrity of fs tree items.
6286 * @root: the root of the tree to be checked.
6287 * @ext_ref feature EXTENDED_IREF is enable or not.
6288 * @account if NOT 0 means check the tree (including tree)'s treeblocks.
6289 * otherwise means check fs tree(s) items relationship and
6290 * @root MUST be a fs tree root.
6291 * Returns 0 represents OK.
6292 * Returns not 0 represents error.
6294 static int check_btrfs_root(struct btrfs_trans_handle *trans,
6295 struct btrfs_root *root, unsigned int ext_ref,
6299 struct btrfs_path path;
6300 struct node_refs nrefs;
6301 struct btrfs_root_item *root_item = &root->root_item;
6306 memset(&nrefs, 0, sizeof(nrefs));
6309 * We need to manually check the first inode item (256)
6310 * As the following traversal function will only start from
6311 * the first inode item in the leaf, if inode item (256) is
6312 * missing we will skip it forever.
6314 ret = check_fs_first_inode(root, ext_ref);
6320 level = btrfs_header_level(root->node);
6321 btrfs_init_path(&path);
6323 if (btrfs_root_refs(root_item) > 0 ||
6324 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
6325 path.nodes[level] = root->node;
6326 path.slots[level] = 0;
6327 extent_buffer_get(root->node);
6329 struct btrfs_key key;
6331 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6332 level = root_item->drop_level;
6333 path.lowest_level = level;
6334 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6341 ret = walk_down_tree_v2(trans, root, &path, &level, &nrefs,
6342 ext_ref, check_all);
6346 /* if ret is negative, walk shall stop */
6352 ret = walk_up_tree_v2(root, &path, &level);
6354 /* Normal exit, reset ret to err */
6361 btrfs_release_path(&path);
6366 * Iterate all items in the tree and call check_inode_item() to check.
6368 * @root: the root of the tree to be checked.
6369 * @ext_ref: the EXTENDED_IREF feature
6371 * Return 0 if no error found.
6372 * Return <0 for error.
6374 static int check_fs_root_v2(struct btrfs_root *root, unsigned int ext_ref)
6376 reset_cached_block_groups(root->fs_info);
6377 return check_btrfs_root(NULL, root, ext_ref, 0);
6381 * Find the relative ref for root_ref and root_backref.
6383 * @root: the root of the root tree.
6384 * @ref_key: the key of the root ref.
6386 * Return 0 if no error occurred.
6388 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
6389 struct extent_buffer *node, int slot)
6391 struct btrfs_path path;
6392 struct btrfs_key key;
6393 struct btrfs_root_ref *ref;
6394 struct btrfs_root_ref *backref;
6395 char ref_name[BTRFS_NAME_LEN] = {0};
6396 char backref_name[BTRFS_NAME_LEN] = {0};
6402 u32 backref_namelen;
6407 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
6408 ref_dirid = btrfs_root_ref_dirid(node, ref);
6409 ref_seq = btrfs_root_ref_sequence(node, ref);
6410 ref_namelen = btrfs_root_ref_name_len(node, ref);
6412 if (ref_namelen <= BTRFS_NAME_LEN) {
6415 len = BTRFS_NAME_LEN;
6416 warning("%s[%llu %llu] ref_name too long",
6417 ref_key->type == BTRFS_ROOT_REF_KEY ?
6418 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
6421 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
6423 /* Find relative root_ref */
6424 key.objectid = ref_key->offset;
6425 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
6426 key.offset = ref_key->objectid;
6428 btrfs_init_path(&path);
6429 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6431 err |= ROOT_REF_MISSING;
6432 error("%s[%llu %llu] couldn't find relative ref",
6433 ref_key->type == BTRFS_ROOT_REF_KEY ?
6434 "ROOT_REF" : "ROOT_BACKREF",
6435 ref_key->objectid, ref_key->offset);
6439 backref = btrfs_item_ptr(path.nodes[0], path.slots[0],
6440 struct btrfs_root_ref);
6441 backref_dirid = btrfs_root_ref_dirid(path.nodes[0], backref);
6442 backref_seq = btrfs_root_ref_sequence(path.nodes[0], backref);
6443 backref_namelen = btrfs_root_ref_name_len(path.nodes[0], backref);
6445 if (backref_namelen <= BTRFS_NAME_LEN) {
6446 len = backref_namelen;
6448 len = BTRFS_NAME_LEN;
6449 warning("%s[%llu %llu] ref_name too long",
6450 key.type == BTRFS_ROOT_REF_KEY ?
6451 "ROOT_REF" : "ROOT_BACKREF",
6452 key.objectid, key.offset);
6454 read_extent_buffer(path.nodes[0], backref_name,
6455 (unsigned long)(backref + 1), len);
6457 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
6458 ref_namelen != backref_namelen ||
6459 strncmp(ref_name, backref_name, len)) {
6460 err |= ROOT_REF_MISMATCH;
6461 error("%s[%llu %llu] mismatch relative ref",
6462 ref_key->type == BTRFS_ROOT_REF_KEY ?
6463 "ROOT_REF" : "ROOT_BACKREF",
6464 ref_key->objectid, ref_key->offset);
6467 btrfs_release_path(&path);
6472 * Check all fs/file tree in low_memory mode.
6474 * 1. for fs tree root item, call check_fs_root_v2()
6475 * 2. for fs tree root ref/backref, call check_root_ref()
6477 * Return 0 if no error occurred.
6479 static int check_fs_roots_v2(struct btrfs_fs_info *fs_info)
6481 struct btrfs_root *tree_root = fs_info->tree_root;
6482 struct btrfs_root *cur_root = NULL;
6483 struct btrfs_path path;
6484 struct btrfs_key key;
6485 struct extent_buffer *node;
6486 unsigned int ext_ref;
6491 ext_ref = btrfs_fs_incompat(fs_info, EXTENDED_IREF);
6493 btrfs_init_path(&path);
6494 key.objectid = BTRFS_FS_TREE_OBJECTID;
6496 key.type = BTRFS_ROOT_ITEM_KEY;
6498 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
6502 } else if (ret > 0) {
6508 node = path.nodes[0];
6509 slot = path.slots[0];
6510 btrfs_item_key_to_cpu(node, &key, slot);
6511 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
6513 if (key.type == BTRFS_ROOT_ITEM_KEY &&
6514 fs_root_objectid(key.objectid)) {
6515 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
6516 cur_root = btrfs_read_fs_root_no_cache(fs_info,
6519 key.offset = (u64)-1;
6520 cur_root = btrfs_read_fs_root(fs_info, &key);
6523 if (IS_ERR(cur_root)) {
6524 error("Fail to read fs/subvol tree: %lld",
6530 ret = check_fs_root_v2(cur_root, ext_ref);
6533 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
6534 btrfs_free_fs_root(cur_root);
6535 } else if (key.type == BTRFS_ROOT_REF_KEY ||
6536 key.type == BTRFS_ROOT_BACKREF_KEY) {
6537 ret = check_root_ref(tree_root, &key, node, slot);
6541 ret = btrfs_next_item(tree_root, &path);
6551 btrfs_release_path(&path);
6555 static int do_check_fs_roots(struct btrfs_fs_info *fs_info,
6556 struct cache_tree *root_cache)
6560 if (!ctx.progress_enabled)
6561 fprintf(stderr, "checking fs roots\n");
6562 if (check_mode == CHECK_MODE_LOWMEM)
6563 ret = check_fs_roots_v2(fs_info);
6565 ret = check_fs_roots(fs_info, root_cache);
6570 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
6572 struct extent_backref *back, *tmp;
6573 struct tree_backref *tback;
6574 struct data_backref *dback;
6578 rbtree_postorder_for_each_entry_safe(back, tmp,
6579 &rec->backref_tree, node) {
6580 if (!back->found_extent_tree) {
6584 if (back->is_data) {
6585 dback = to_data_backref(back);
6586 fprintf(stderr, "Data backref %llu %s %llu"
6587 " owner %llu offset %llu num_refs %lu"
6588 " not found in extent tree\n",
6589 (unsigned long long)rec->start,
6590 back->full_backref ?
6592 back->full_backref ?
6593 (unsigned long long)dback->parent:
6594 (unsigned long long)dback->root,
6595 (unsigned long long)dback->owner,
6596 (unsigned long long)dback->offset,
6597 (unsigned long)dback->num_refs);
6599 tback = to_tree_backref(back);
6600 fprintf(stderr, "Tree backref %llu parent %llu"
6601 " root %llu not found in extent tree\n",
6602 (unsigned long long)rec->start,
6603 (unsigned long long)tback->parent,
6604 (unsigned long long)tback->root);
6607 if (!back->is_data && !back->found_ref) {
6611 tback = to_tree_backref(back);
6612 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
6613 (unsigned long long)rec->start,
6614 back->full_backref ? "parent" : "root",
6615 back->full_backref ?
6616 (unsigned long long)tback->parent :
6617 (unsigned long long)tback->root, back);
6619 if (back->is_data) {
6620 dback = to_data_backref(back);
6621 if (dback->found_ref != dback->num_refs) {
6625 fprintf(stderr, "Incorrect local backref count"
6626 " on %llu %s %llu owner %llu"
6627 " offset %llu found %u wanted %u back %p\n",
6628 (unsigned long long)rec->start,
6629 back->full_backref ?
6631 back->full_backref ?
6632 (unsigned long long)dback->parent:
6633 (unsigned long long)dback->root,
6634 (unsigned long long)dback->owner,
6635 (unsigned long long)dback->offset,
6636 dback->found_ref, dback->num_refs, back);
6638 if (dback->disk_bytenr != rec->start) {
6642 fprintf(stderr, "Backref disk bytenr does not"
6643 " match extent record, bytenr=%llu, "
6644 "ref bytenr=%llu\n",
6645 (unsigned long long)rec->start,
6646 (unsigned long long)dback->disk_bytenr);
6649 if (dback->bytes != rec->nr) {
6653 fprintf(stderr, "Backref bytes do not match "
6654 "extent backref, bytenr=%llu, ref "
6655 "bytes=%llu, backref bytes=%llu\n",
6656 (unsigned long long)rec->start,
6657 (unsigned long long)rec->nr,
6658 (unsigned long long)dback->bytes);
6661 if (!back->is_data) {
6664 dback = to_data_backref(back);
6665 found += dback->found_ref;
6668 if (found != rec->refs) {
6672 fprintf(stderr, "Incorrect global backref count "
6673 "on %llu found %llu wanted %llu\n",
6674 (unsigned long long)rec->start,
6675 (unsigned long long)found,
6676 (unsigned long long)rec->refs);
6682 static void __free_one_backref(struct rb_node *node)
6684 struct extent_backref *back = rb_node_to_extent_backref(node);
6689 static void free_all_extent_backrefs(struct extent_record *rec)
6691 rb_free_nodes(&rec->backref_tree, __free_one_backref);
6694 static void free_extent_record_cache(struct cache_tree *extent_cache)
6696 struct cache_extent *cache;
6697 struct extent_record *rec;
6700 cache = first_cache_extent(extent_cache);
6703 rec = container_of(cache, struct extent_record, cache);
6704 remove_cache_extent(extent_cache, cache);
6705 free_all_extent_backrefs(rec);
6710 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
6711 struct extent_record *rec)
6713 if (rec->content_checked && rec->owner_ref_checked &&
6714 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
6715 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0) &&
6716 !rec->bad_full_backref && !rec->crossing_stripes &&
6717 !rec->wrong_chunk_type) {
6718 remove_cache_extent(extent_cache, &rec->cache);
6719 free_all_extent_backrefs(rec);
6720 list_del_init(&rec->list);
6726 static int check_owner_ref(struct btrfs_root *root,
6727 struct extent_record *rec,
6728 struct extent_buffer *buf)
6730 struct extent_backref *node, *tmp;
6731 struct tree_backref *back;
6732 struct btrfs_root *ref_root;
6733 struct btrfs_key key;
6734 struct btrfs_path path;
6735 struct extent_buffer *parent;
6740 rbtree_postorder_for_each_entry_safe(node, tmp,
6741 &rec->backref_tree, node) {
6744 if (!node->found_ref)
6746 if (node->full_backref)
6748 back = to_tree_backref(node);
6749 if (btrfs_header_owner(buf) == back->root)
6752 BUG_ON(rec->is_root);
6754 /* try to find the block by search corresponding fs tree */
6755 key.objectid = btrfs_header_owner(buf);
6756 key.type = BTRFS_ROOT_ITEM_KEY;
6757 key.offset = (u64)-1;
6759 ref_root = btrfs_read_fs_root(root->fs_info, &key);
6760 if (IS_ERR(ref_root))
6763 level = btrfs_header_level(buf);
6765 btrfs_item_key_to_cpu(buf, &key, 0);
6767 btrfs_node_key_to_cpu(buf, &key, 0);
6769 btrfs_init_path(&path);
6770 path.lowest_level = level + 1;
6771 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
6775 parent = path.nodes[level + 1];
6776 if (parent && buf->start == btrfs_node_blockptr(parent,
6777 path.slots[level + 1]))
6780 btrfs_release_path(&path);
6781 return found ? 0 : 1;
6784 static int is_extent_tree_record(struct extent_record *rec)
6786 struct extent_backref *node, *tmp;
6787 struct tree_backref *back;
6790 rbtree_postorder_for_each_entry_safe(node, tmp,
6791 &rec->backref_tree, node) {
6794 back = to_tree_backref(node);
6795 if (node->full_backref)
6797 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
6804 static int record_bad_block_io(struct btrfs_fs_info *info,
6805 struct cache_tree *extent_cache,
6808 struct extent_record *rec;
6809 struct cache_extent *cache;
6810 struct btrfs_key key;
6812 cache = lookup_cache_extent(extent_cache, start, len);
6816 rec = container_of(cache, struct extent_record, cache);
6817 if (!is_extent_tree_record(rec))
6820 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
6821 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
6824 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
6825 struct extent_buffer *buf, int slot)
6827 if (btrfs_header_level(buf)) {
6828 struct btrfs_key_ptr ptr1, ptr2;
6830 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
6831 sizeof(struct btrfs_key_ptr));
6832 read_extent_buffer(buf, &ptr2,
6833 btrfs_node_key_ptr_offset(slot + 1),
6834 sizeof(struct btrfs_key_ptr));
6835 write_extent_buffer(buf, &ptr1,
6836 btrfs_node_key_ptr_offset(slot + 1),
6837 sizeof(struct btrfs_key_ptr));
6838 write_extent_buffer(buf, &ptr2,
6839 btrfs_node_key_ptr_offset(slot),
6840 sizeof(struct btrfs_key_ptr));
6842 struct btrfs_disk_key key;
6843 btrfs_node_key(buf, &key, 0);
6844 btrfs_fixup_low_keys(root, path, &key,
6845 btrfs_header_level(buf) + 1);
6848 struct btrfs_item *item1, *item2;
6849 struct btrfs_key k1, k2;
6850 char *item1_data, *item2_data;
6851 u32 item1_offset, item2_offset, item1_size, item2_size;
6853 item1 = btrfs_item_nr(slot);
6854 item2 = btrfs_item_nr(slot + 1);
6855 btrfs_item_key_to_cpu(buf, &k1, slot);
6856 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
6857 item1_offset = btrfs_item_offset(buf, item1);
6858 item2_offset = btrfs_item_offset(buf, item2);
6859 item1_size = btrfs_item_size(buf, item1);
6860 item2_size = btrfs_item_size(buf, item2);
6862 item1_data = malloc(item1_size);
6865 item2_data = malloc(item2_size);
6871 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
6872 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
6874 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
6875 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
6879 btrfs_set_item_offset(buf, item1, item2_offset);
6880 btrfs_set_item_offset(buf, item2, item1_offset);
6881 btrfs_set_item_size(buf, item1, item2_size);
6882 btrfs_set_item_size(buf, item2, item1_size);
6884 path->slots[0] = slot;
6885 btrfs_set_item_key_unsafe(root, path, &k2);
6886 path->slots[0] = slot + 1;
6887 btrfs_set_item_key_unsafe(root, path, &k1);
6892 static int fix_key_order(struct btrfs_root *root, struct btrfs_path *path)
6894 struct extent_buffer *buf;
6895 struct btrfs_key k1, k2;
6897 int level = path->lowest_level;
6900 buf = path->nodes[level];
6901 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
6903 btrfs_node_key_to_cpu(buf, &k1, i);
6904 btrfs_node_key_to_cpu(buf, &k2, i + 1);
6906 btrfs_item_key_to_cpu(buf, &k1, i);
6907 btrfs_item_key_to_cpu(buf, &k2, i + 1);
6909 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
6911 ret = swap_values(root, path, buf, i);
6914 btrfs_mark_buffer_dirty(buf);
6920 static int delete_bogus_item(struct btrfs_root *root,
6921 struct btrfs_path *path,
6922 struct extent_buffer *buf, int slot)
6924 struct btrfs_key key;
6925 int nritems = btrfs_header_nritems(buf);
6927 btrfs_item_key_to_cpu(buf, &key, slot);
6929 /* These are all the keys we can deal with missing. */
6930 if (key.type != BTRFS_DIR_INDEX_KEY &&
6931 key.type != BTRFS_EXTENT_ITEM_KEY &&
6932 key.type != BTRFS_METADATA_ITEM_KEY &&
6933 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
6934 key.type != BTRFS_EXTENT_DATA_REF_KEY)
6937 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
6938 (unsigned long long)key.objectid, key.type,
6939 (unsigned long long)key.offset, slot, buf->start);
6940 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
6941 btrfs_item_nr_offset(slot + 1),
6942 sizeof(struct btrfs_item) *
6943 (nritems - slot - 1));
6944 btrfs_set_header_nritems(buf, nritems - 1);
6946 struct btrfs_disk_key disk_key;
6948 btrfs_item_key(buf, &disk_key, 0);
6949 btrfs_fixup_low_keys(root, path, &disk_key, 1);
6951 btrfs_mark_buffer_dirty(buf);
6955 static int fix_item_offset(struct btrfs_root *root, struct btrfs_path *path)
6957 struct extent_buffer *buf;
6961 /* We should only get this for leaves */
6962 BUG_ON(path->lowest_level);
6963 buf = path->nodes[0];
6965 for (i = 0; i < btrfs_header_nritems(buf); i++) {
6966 unsigned int shift = 0, offset;
6968 if (i == 0 && btrfs_item_end_nr(buf, i) !=
6969 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
6970 if (btrfs_item_end_nr(buf, i) >
6971 BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
6972 ret = delete_bogus_item(root, path, buf, i);
6975 fprintf(stderr, "item is off the end of the "
6976 "leaf, can't fix\n");
6980 shift = BTRFS_LEAF_DATA_SIZE(root->fs_info) -
6981 btrfs_item_end_nr(buf, i);
6982 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
6983 btrfs_item_offset_nr(buf, i - 1)) {
6984 if (btrfs_item_end_nr(buf, i) >
6985 btrfs_item_offset_nr(buf, i - 1)) {
6986 ret = delete_bogus_item(root, path, buf, i);
6989 fprintf(stderr, "items overlap, can't fix\n");
6993 shift = btrfs_item_offset_nr(buf, i - 1) -
6994 btrfs_item_end_nr(buf, i);
6999 printf("Shifting item nr %d by %u bytes in block %llu\n",
7000 i, shift, (unsigned long long)buf->start);
7001 offset = btrfs_item_offset_nr(buf, i);
7002 memmove_extent_buffer(buf,
7003 btrfs_leaf_data(buf) + offset + shift,
7004 btrfs_leaf_data(buf) + offset,
7005 btrfs_item_size_nr(buf, i));
7006 btrfs_set_item_offset(buf, btrfs_item_nr(i),
7008 btrfs_mark_buffer_dirty(buf);
7012 * We may have moved things, in which case we want to exit so we don't
7013 * write those changes out. Once we have proper abort functionality in
7014 * progs this can be changed to something nicer.
7021 * Attempt to fix basic block failures. If we can't fix it for whatever reason
7022 * then just return -EIO.
7024 static int try_to_fix_bad_block(struct btrfs_root *root,
7025 struct extent_buffer *buf,
7026 enum btrfs_tree_block_status status)
7028 struct btrfs_trans_handle *trans;
7029 struct ulist *roots;
7030 struct ulist_node *node;
7031 struct btrfs_root *search_root;
7032 struct btrfs_path path;
7033 struct ulist_iterator iter;
7034 struct btrfs_key root_key, key;
7037 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
7038 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
7041 ret = btrfs_find_all_roots(NULL, root->fs_info, buf->start, 0, &roots);
7045 btrfs_init_path(&path);
7046 ULIST_ITER_INIT(&iter);
7047 while ((node = ulist_next(roots, &iter))) {
7048 root_key.objectid = node->val;
7049 root_key.type = BTRFS_ROOT_ITEM_KEY;
7050 root_key.offset = (u64)-1;
7052 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
7059 trans = btrfs_start_transaction(search_root, 0);
7060 if (IS_ERR(trans)) {
7061 ret = PTR_ERR(trans);
7065 path.lowest_level = btrfs_header_level(buf);
7066 path.skip_check_block = 1;
7067 if (path.lowest_level)
7068 btrfs_node_key_to_cpu(buf, &key, 0);
7070 btrfs_item_key_to_cpu(buf, &key, 0);
7071 ret = btrfs_search_slot(trans, search_root, &key, &path, 0, 1);
7074 btrfs_commit_transaction(trans, search_root);
7077 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
7078 ret = fix_key_order(search_root, &path);
7079 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
7080 ret = fix_item_offset(search_root, &path);
7082 btrfs_commit_transaction(trans, search_root);
7085 btrfs_release_path(&path);
7086 btrfs_commit_transaction(trans, search_root);
7089 btrfs_release_path(&path);
7093 static int check_block(struct btrfs_root *root,
7094 struct cache_tree *extent_cache,
7095 struct extent_buffer *buf, u64 flags)
7097 struct extent_record *rec;
7098 struct cache_extent *cache;
7099 struct btrfs_key key;
7100 enum btrfs_tree_block_status status;
7104 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
7107 rec = container_of(cache, struct extent_record, cache);
7108 rec->generation = btrfs_header_generation(buf);
7110 level = btrfs_header_level(buf);
7111 if (btrfs_header_nritems(buf) > 0) {
7114 btrfs_item_key_to_cpu(buf, &key, 0);
7116 btrfs_node_key_to_cpu(buf, &key, 0);
7118 rec->info_objectid = key.objectid;
7120 rec->info_level = level;
7122 if (btrfs_is_leaf(buf))
7123 status = btrfs_check_leaf(root, &rec->parent_key, buf);
7125 status = btrfs_check_node(root, &rec->parent_key, buf);
7127 if (status != BTRFS_TREE_BLOCK_CLEAN) {
7129 status = try_to_fix_bad_block(root, buf, status);
7130 if (status != BTRFS_TREE_BLOCK_CLEAN) {
7132 fprintf(stderr, "bad block %llu\n",
7133 (unsigned long long)buf->start);
7136 * Signal to callers we need to start the scan over
7137 * again since we'll have cowed blocks.
7142 rec->content_checked = 1;
7143 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7144 rec->owner_ref_checked = 1;
7146 ret = check_owner_ref(root, rec, buf);
7148 rec->owner_ref_checked = 1;
7152 maybe_free_extent_rec(extent_cache, rec);
7157 static struct tree_backref *find_tree_backref(struct extent_record *rec,
7158 u64 parent, u64 root)
7160 struct list_head *cur = rec->backrefs.next;
7161 struct extent_backref *node;
7162 struct tree_backref *back;
7164 while(cur != &rec->backrefs) {
7165 node = to_extent_backref(cur);
7169 back = to_tree_backref(node);
7171 if (!node->full_backref)
7173 if (parent == back->parent)
7176 if (node->full_backref)
7178 if (back->root == root)
7186 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
7187 u64 parent, u64 root)
7189 struct tree_backref *ref = malloc(sizeof(*ref));
7193 memset(&ref->node, 0, sizeof(ref->node));
7195 ref->parent = parent;
7196 ref->node.full_backref = 1;
7199 ref->node.full_backref = 0;
7206 static struct data_backref *find_data_backref(struct extent_record *rec,
7207 u64 parent, u64 root,
7208 u64 owner, u64 offset,
7210 u64 disk_bytenr, u64 bytes)
7212 struct list_head *cur = rec->backrefs.next;
7213 struct extent_backref *node;
7214 struct data_backref *back;
7216 while(cur != &rec->backrefs) {
7217 node = to_extent_backref(cur);
7221 back = to_data_backref(node);
7223 if (!node->full_backref)
7225 if (parent == back->parent)
7228 if (node->full_backref)
7230 if (back->root == root && back->owner == owner &&
7231 back->offset == offset) {
7232 if (found_ref && node->found_ref &&
7233 (back->bytes != bytes ||
7234 back->disk_bytenr != disk_bytenr))
7244 static struct data_backref *alloc_data_backref(struct extent_record *rec,
7245 u64 parent, u64 root,
7246 u64 owner, u64 offset,
7249 struct data_backref *ref = malloc(sizeof(*ref));
7253 memset(&ref->node, 0, sizeof(ref->node));
7254 ref->node.is_data = 1;
7257 ref->parent = parent;
7260 ref->node.full_backref = 1;
7264 ref->offset = offset;
7265 ref->node.full_backref = 0;
7267 ref->bytes = max_size;
7270 if (max_size > rec->max_size)
7271 rec->max_size = max_size;
7275 /* Check if the type of extent matches with its chunk */
7276 static void check_extent_type(struct extent_record *rec)
7278 struct btrfs_block_group_cache *bg_cache;
7280 bg_cache = btrfs_lookup_first_block_group(global_info, rec->start);
7284 /* data extent, check chunk directly*/
7285 if (!rec->metadata) {
7286 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
7287 rec->wrong_chunk_type = 1;
7291 /* metadata extent, check the obvious case first */
7292 if (!(bg_cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
7293 BTRFS_BLOCK_GROUP_METADATA))) {
7294 rec->wrong_chunk_type = 1;
7299 * Check SYSTEM extent, as it's also marked as metadata, we can only
7300 * make sure it's a SYSTEM extent by its backref
7302 if (!RB_EMPTY_ROOT(&rec->backref_tree)) {
7303 struct extent_backref *node;
7304 struct tree_backref *tback;
7307 node = rb_node_to_extent_backref(rb_first(&rec->backref_tree));
7308 if (node->is_data) {
7309 /* tree block shouldn't have data backref */
7310 rec->wrong_chunk_type = 1;
7313 tback = container_of(node, struct tree_backref, node);
7315 if (tback->root == BTRFS_CHUNK_TREE_OBJECTID)
7316 bg_type = BTRFS_BLOCK_GROUP_SYSTEM;
7318 bg_type = BTRFS_BLOCK_GROUP_METADATA;
7319 if (!(bg_cache->flags & bg_type))
7320 rec->wrong_chunk_type = 1;
7325 * Allocate a new extent record, fill default values from @tmpl and insert int
7326 * @extent_cache. Caller is supposed to make sure the [start,nr) is not in
7327 * the cache, otherwise it fails.
7329 static int add_extent_rec_nolookup(struct cache_tree *extent_cache,
7330 struct extent_record *tmpl)
7332 struct extent_record *rec;
7335 BUG_ON(tmpl->max_size == 0);
7336 rec = malloc(sizeof(*rec));
7339 rec->start = tmpl->start;
7340 rec->max_size = tmpl->max_size;
7341 rec->nr = max(tmpl->nr, tmpl->max_size);
7342 rec->found_rec = tmpl->found_rec;
7343 rec->content_checked = tmpl->content_checked;
7344 rec->owner_ref_checked = tmpl->owner_ref_checked;
7345 rec->num_duplicates = 0;
7346 rec->metadata = tmpl->metadata;
7347 rec->flag_block_full_backref = FLAG_UNSET;
7348 rec->bad_full_backref = 0;
7349 rec->crossing_stripes = 0;
7350 rec->wrong_chunk_type = 0;
7351 rec->is_root = tmpl->is_root;
7352 rec->refs = tmpl->refs;
7353 rec->extent_item_refs = tmpl->extent_item_refs;
7354 rec->parent_generation = tmpl->parent_generation;
7355 INIT_LIST_HEAD(&rec->backrefs);
7356 INIT_LIST_HEAD(&rec->dups);
7357 INIT_LIST_HEAD(&rec->list);
7358 rec->backref_tree = RB_ROOT;
7359 memcpy(&rec->parent_key, &tmpl->parent_key, sizeof(tmpl->parent_key));
7360 rec->cache.start = tmpl->start;
7361 rec->cache.size = tmpl->nr;
7362 ret = insert_cache_extent(extent_cache, &rec->cache);
7367 bytes_used += rec->nr;
7370 rec->crossing_stripes = check_crossing_stripes(global_info,
7371 rec->start, global_info->nodesize);
7372 check_extent_type(rec);
7377 * Lookup and modify an extent, some values of @tmpl are interpreted verbatim,
7379 * - refs - if found, increase refs
7380 * - is_root - if found, set
7381 * - content_checked - if found, set
7382 * - owner_ref_checked - if found, set
7384 * If not found, create a new one, initialize and insert.
7386 static int add_extent_rec(struct cache_tree *extent_cache,
7387 struct extent_record *tmpl)
7389 struct extent_record *rec;
7390 struct cache_extent *cache;
7394 cache = lookup_cache_extent(extent_cache, tmpl->start, tmpl->nr);
7396 rec = container_of(cache, struct extent_record, cache);
7400 rec->nr = max(tmpl->nr, tmpl->max_size);
7403 * We need to make sure to reset nr to whatever the extent
7404 * record says was the real size, this way we can compare it to
7407 if (tmpl->found_rec) {
7408 if (tmpl->start != rec->start || rec->found_rec) {
7409 struct extent_record *tmp;
7412 if (list_empty(&rec->list))
7413 list_add_tail(&rec->list,
7414 &duplicate_extents);
7417 * We have to do this song and dance in case we
7418 * find an extent record that falls inside of
7419 * our current extent record but does not have
7420 * the same objectid.
7422 tmp = malloc(sizeof(*tmp));
7425 tmp->start = tmpl->start;
7426 tmp->max_size = tmpl->max_size;
7429 tmp->metadata = tmpl->metadata;
7430 tmp->extent_item_refs = tmpl->extent_item_refs;
7431 INIT_LIST_HEAD(&tmp->list);
7432 list_add_tail(&tmp->list, &rec->dups);
7433 rec->num_duplicates++;
7440 if (tmpl->extent_item_refs && !dup) {
7441 if (rec->extent_item_refs) {
7442 fprintf(stderr, "block %llu rec "
7443 "extent_item_refs %llu, passed %llu\n",
7444 (unsigned long long)tmpl->start,
7445 (unsigned long long)
7446 rec->extent_item_refs,
7447 (unsigned long long)tmpl->extent_item_refs);
7449 rec->extent_item_refs = tmpl->extent_item_refs;
7453 if (tmpl->content_checked)
7454 rec->content_checked = 1;
7455 if (tmpl->owner_ref_checked)
7456 rec->owner_ref_checked = 1;
7457 memcpy(&rec->parent_key, &tmpl->parent_key,
7458 sizeof(tmpl->parent_key));
7459 if (tmpl->parent_generation)
7460 rec->parent_generation = tmpl->parent_generation;
7461 if (rec->max_size < tmpl->max_size)
7462 rec->max_size = tmpl->max_size;
7465 * A metadata extent can't cross stripe_len boundary, otherwise
7466 * kernel scrub won't be able to handle it.
7467 * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
7471 rec->crossing_stripes = check_crossing_stripes(
7472 global_info, rec->start,
7473 global_info->nodesize);
7474 check_extent_type(rec);
7475 maybe_free_extent_rec(extent_cache, rec);
7479 ret = add_extent_rec_nolookup(extent_cache, tmpl);
7484 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
7485 u64 parent, u64 root, int found_ref)
7487 struct extent_record *rec;
7488 struct tree_backref *back;
7489 struct cache_extent *cache;
7491 bool insert = false;
7493 cache = lookup_cache_extent(extent_cache, bytenr, 1);
7495 struct extent_record tmpl;
7497 memset(&tmpl, 0, sizeof(tmpl));
7498 tmpl.start = bytenr;
7503 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
7507 /* really a bug in cache_extent implement now */
7508 cache = lookup_cache_extent(extent_cache, bytenr, 1);
7513 rec = container_of(cache, struct extent_record, cache);
7514 if (rec->start != bytenr) {
7516 * Several cause, from unaligned bytenr to over lapping extents
7521 back = find_tree_backref(rec, parent, root);
7523 back = alloc_tree_backref(rec, parent, root);
7530 if (back->node.found_ref) {
7531 fprintf(stderr, "Extent back ref already exists "
7532 "for %llu parent %llu root %llu \n",
7533 (unsigned long long)bytenr,
7534 (unsigned long long)parent,
7535 (unsigned long long)root);
7537 back->node.found_ref = 1;
7539 if (back->node.found_extent_tree) {
7540 fprintf(stderr, "Extent back ref already exists "
7541 "for %llu parent %llu root %llu \n",
7542 (unsigned long long)bytenr,
7543 (unsigned long long)parent,
7544 (unsigned long long)root);
7546 back->node.found_extent_tree = 1;
7549 WARN_ON(rb_insert(&rec->backref_tree, &back->node.node,
7550 compare_extent_backref));
7551 check_extent_type(rec);
7552 maybe_free_extent_rec(extent_cache, rec);
7556 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
7557 u64 parent, u64 root, u64 owner, u64 offset,
7558 u32 num_refs, int found_ref, u64 max_size)
7560 struct extent_record *rec;
7561 struct data_backref *back;
7562 struct cache_extent *cache;
7564 bool insert = false;
7566 cache = lookup_cache_extent(extent_cache, bytenr, 1);
7568 struct extent_record tmpl;
7570 memset(&tmpl, 0, sizeof(tmpl));
7571 tmpl.start = bytenr;
7573 tmpl.max_size = max_size;
7575 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
7579 cache = lookup_cache_extent(extent_cache, bytenr, 1);
7584 rec = container_of(cache, struct extent_record, cache);
7585 if (rec->max_size < max_size)
7586 rec->max_size = max_size;
7589 * If found_ref is set then max_size is the real size and must match the
7590 * existing refs. So if we have already found a ref then we need to
7591 * make sure that this ref matches the existing one, otherwise we need
7592 * to add a new backref so we can notice that the backrefs don't match
7593 * and we need to figure out who is telling the truth. This is to
7594 * account for that awful fsync bug I introduced where we'd end up with
7595 * a btrfs_file_extent_item that would have its length include multiple
7596 * prealloc extents or point inside of a prealloc extent.
7598 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
7601 back = alloc_data_backref(rec, parent, root, owner, offset,
7608 BUG_ON(num_refs != 1);
7609 if (back->node.found_ref)
7610 BUG_ON(back->bytes != max_size);
7611 back->node.found_ref = 1;
7612 back->found_ref += 1;
7613 if (back->bytes != max_size || back->disk_bytenr != bytenr) {
7614 back->bytes = max_size;
7615 back->disk_bytenr = bytenr;
7617 /* Need to reinsert if not already in the tree */
7619 rb_erase(&back->node.node, &rec->backref_tree);
7624 rec->content_checked = 1;
7625 rec->owner_ref_checked = 1;
7627 if (back->node.found_extent_tree) {
7628 fprintf(stderr, "Extent back ref already exists "
7629 "for %llu parent %llu root %llu "
7630 "owner %llu offset %llu num_refs %lu\n",
7631 (unsigned long long)bytenr,
7632 (unsigned long long)parent,
7633 (unsigned long long)root,
7634 (unsigned long long)owner,
7635 (unsigned long long)offset,
7636 (unsigned long)num_refs);
7638 back->num_refs = num_refs;
7639 back->node.found_extent_tree = 1;
7642 WARN_ON(rb_insert(&rec->backref_tree, &back->node.node,
7643 compare_extent_backref));
7645 maybe_free_extent_rec(extent_cache, rec);
7649 static int add_pending(struct cache_tree *pending,
7650 struct cache_tree *seen, u64 bytenr, u32 size)
7653 ret = add_cache_extent(seen, bytenr, size);
7656 add_cache_extent(pending, bytenr, size);
7660 static int pick_next_pending(struct cache_tree *pending,
7661 struct cache_tree *reada,
7662 struct cache_tree *nodes,
7663 u64 last, struct block_info *bits, int bits_nr,
7666 unsigned long node_start = last;
7667 struct cache_extent *cache;
7670 cache = search_cache_extent(reada, 0);
7672 bits[0].start = cache->start;
7673 bits[0].size = cache->size;
7678 if (node_start > 32768)
7679 node_start -= 32768;
7681 cache = search_cache_extent(nodes, node_start);
7683 cache = search_cache_extent(nodes, 0);
7686 cache = search_cache_extent(pending, 0);
7691 bits[ret].start = cache->start;
7692 bits[ret].size = cache->size;
7693 cache = next_cache_extent(cache);
7695 } while (cache && ret < bits_nr);
7701 bits[ret].start = cache->start;
7702 bits[ret].size = cache->size;
7703 cache = next_cache_extent(cache);
7705 } while (cache && ret < bits_nr);
7707 if (bits_nr - ret > 8) {
7708 u64 lookup = bits[0].start + bits[0].size;
7709 struct cache_extent *next;
7710 next = search_cache_extent(pending, lookup);
7712 if (next->start - lookup > 32768)
7714 bits[ret].start = next->start;
7715 bits[ret].size = next->size;
7716 lookup = next->start + next->size;
7720 next = next_cache_extent(next);
7728 static void free_chunk_record(struct cache_extent *cache)
7730 struct chunk_record *rec;
7732 rec = container_of(cache, struct chunk_record, cache);
7733 list_del_init(&rec->list);
7734 list_del_init(&rec->dextents);
7738 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
7740 cache_tree_free_extents(chunk_cache, free_chunk_record);
7743 static void free_device_record(struct rb_node *node)
7745 struct device_record *rec;
7747 rec = container_of(node, struct device_record, node);
7751 FREE_RB_BASED_TREE(device_cache, free_device_record);
7753 int insert_block_group_record(struct block_group_tree *tree,
7754 struct block_group_record *bg_rec)
7758 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
7762 list_add_tail(&bg_rec->list, &tree->block_groups);
7766 static void free_block_group_record(struct cache_extent *cache)
7768 struct block_group_record *rec;
7770 rec = container_of(cache, struct block_group_record, cache);
7771 list_del_init(&rec->list);
7775 void free_block_group_tree(struct block_group_tree *tree)
7777 cache_tree_free_extents(&tree->tree, free_block_group_record);
7780 int insert_device_extent_record(struct device_extent_tree *tree,
7781 struct device_extent_record *de_rec)
7786 * Device extent is a bit different from the other extents, because
7787 * the extents which belong to the different devices may have the
7788 * same start and size, so we need use the special extent cache
7789 * search/insert functions.
7791 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
7795 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
7796 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
7800 static void free_device_extent_record(struct cache_extent *cache)
7802 struct device_extent_record *rec;
7804 rec = container_of(cache, struct device_extent_record, cache);
7805 if (!list_empty(&rec->chunk_list))
7806 list_del_init(&rec->chunk_list);
7807 if (!list_empty(&rec->device_list))
7808 list_del_init(&rec->device_list);
7812 void free_device_extent_tree(struct device_extent_tree *tree)
7814 cache_tree_free_extents(&tree->tree, free_device_extent_record);
7817 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7818 static int process_extent_ref_v0(struct cache_tree *extent_cache,
7819 struct extent_buffer *leaf, int slot)
7821 struct btrfs_extent_ref_v0 *ref0;
7822 struct btrfs_key key;
7825 btrfs_item_key_to_cpu(leaf, &key, slot);
7826 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
7827 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
7828 ret = add_tree_backref(extent_cache, key.objectid, key.offset,
7831 ret = add_data_backref(extent_cache, key.objectid, key.offset,
7832 0, 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
7838 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
7839 struct btrfs_key *key,
7842 struct btrfs_chunk *ptr;
7843 struct chunk_record *rec;
7846 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
7847 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
7849 rec = calloc(1, btrfs_chunk_record_size(num_stripes));
7851 fprintf(stderr, "memory allocation failed\n");
7855 INIT_LIST_HEAD(&rec->list);
7856 INIT_LIST_HEAD(&rec->dextents);
7859 rec->cache.start = key->offset;
7860 rec->cache.size = btrfs_chunk_length(leaf, ptr);
7862 rec->generation = btrfs_header_generation(leaf);
7864 rec->objectid = key->objectid;
7865 rec->type = key->type;
7866 rec->offset = key->offset;
7868 rec->length = rec->cache.size;
7869 rec->owner = btrfs_chunk_owner(leaf, ptr);
7870 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
7871 rec->type_flags = btrfs_chunk_type(leaf, ptr);
7872 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
7873 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
7874 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
7875 rec->num_stripes = num_stripes;
7876 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
7878 for (i = 0; i < rec->num_stripes; ++i) {
7879 rec->stripes[i].devid =
7880 btrfs_stripe_devid_nr(leaf, ptr, i);
7881 rec->stripes[i].offset =
7882 btrfs_stripe_offset_nr(leaf, ptr, i);
7883 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
7884 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
7891 static int process_chunk_item(struct cache_tree *chunk_cache,
7892 struct btrfs_key *key, struct extent_buffer *eb,
7895 struct chunk_record *rec;
7896 struct btrfs_chunk *chunk;
7899 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
7901 * Do extra check for this chunk item,
7903 * It's still possible one can craft a leaf with CHUNK_ITEM, with
7904 * wrong onwer(3) out of chunk tree, to pass both chunk tree check
7905 * and owner<->key_type check.
7907 ret = btrfs_check_chunk_valid(global_info, eb, chunk, slot,
7910 error("chunk(%llu, %llu) is not valid, ignore it",
7911 key->offset, btrfs_chunk_length(eb, chunk));
7914 rec = btrfs_new_chunk_record(eb, key, slot);
7915 ret = insert_cache_extent(chunk_cache, &rec->cache);
7917 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
7918 rec->offset, rec->length);
7925 static int process_device_item(struct rb_root *dev_cache,
7926 struct btrfs_key *key, struct extent_buffer *eb, int slot)
7928 struct btrfs_dev_item *ptr;
7929 struct device_record *rec;
7932 ptr = btrfs_item_ptr(eb,
7933 slot, struct btrfs_dev_item);
7935 rec = malloc(sizeof(*rec));
7937 fprintf(stderr, "memory allocation failed\n");
7941 rec->devid = key->offset;
7942 rec->generation = btrfs_header_generation(eb);
7944 rec->objectid = key->objectid;
7945 rec->type = key->type;
7946 rec->offset = key->offset;
7948 rec->devid = btrfs_device_id(eb, ptr);
7949 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
7950 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
7952 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
7954 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
7961 struct block_group_record *
7962 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
7965 struct btrfs_block_group_item *ptr;
7966 struct block_group_record *rec;
7968 rec = calloc(1, sizeof(*rec));
7970 fprintf(stderr, "memory allocation failed\n");
7974 rec->cache.start = key->objectid;
7975 rec->cache.size = key->offset;
7977 rec->generation = btrfs_header_generation(leaf);
7979 rec->objectid = key->objectid;
7980 rec->type = key->type;
7981 rec->offset = key->offset;
7983 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
7984 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
7986 INIT_LIST_HEAD(&rec->list);
7991 static int process_block_group_item(struct block_group_tree *block_group_cache,
7992 struct btrfs_key *key,
7993 struct extent_buffer *eb, int slot)
7995 struct block_group_record *rec;
7998 rec = btrfs_new_block_group_record(eb, key, slot);
7999 ret = insert_block_group_record(block_group_cache, rec);
8001 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
8002 rec->objectid, rec->offset);
8009 struct device_extent_record *
8010 btrfs_new_device_extent_record(struct extent_buffer *leaf,
8011 struct btrfs_key *key, int slot)
8013 struct device_extent_record *rec;
8014 struct btrfs_dev_extent *ptr;
8016 rec = calloc(1, sizeof(*rec));
8018 fprintf(stderr, "memory allocation failed\n");
8022 rec->cache.objectid = key->objectid;
8023 rec->cache.start = key->offset;
8025 rec->generation = btrfs_header_generation(leaf);
8027 rec->objectid = key->objectid;
8028 rec->type = key->type;
8029 rec->offset = key->offset;
8031 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
8032 rec->chunk_objecteid =
8033 btrfs_dev_extent_chunk_objectid(leaf, ptr);
8035 btrfs_dev_extent_chunk_offset(leaf, ptr);
8036 rec->length = btrfs_dev_extent_length(leaf, ptr);
8037 rec->cache.size = rec->length;
8039 INIT_LIST_HEAD(&rec->chunk_list);
8040 INIT_LIST_HEAD(&rec->device_list);
8046 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
8047 struct btrfs_key *key, struct extent_buffer *eb,
8050 struct device_extent_record *rec;
8053 rec = btrfs_new_device_extent_record(eb, key, slot);
8054 ret = insert_device_extent_record(dev_extent_cache, rec);
8057 "Device extent[%llu, %llu, %llu] existed.\n",
8058 rec->objectid, rec->offset, rec->length);
8065 static int process_extent_item(struct btrfs_root *root,
8066 struct cache_tree *extent_cache,
8067 struct extent_buffer *eb, int slot)
8069 struct btrfs_extent_item *ei;
8070 struct btrfs_extent_inline_ref *iref;
8071 struct btrfs_extent_data_ref *dref;
8072 struct btrfs_shared_data_ref *sref;
8073 struct btrfs_key key;
8074 struct extent_record tmpl;
8079 u32 item_size = btrfs_item_size_nr(eb, slot);
8085 btrfs_item_key_to_cpu(eb, &key, slot);
8087 if (key.type == BTRFS_METADATA_ITEM_KEY) {
8089 num_bytes = root->fs_info->nodesize;
8091 num_bytes = key.offset;
8094 if (!IS_ALIGNED(key.objectid, root->fs_info->sectorsize)) {
8095 error("ignoring invalid extent, bytenr %llu is not aligned to %u",
8096 key.objectid, root->fs_info->sectorsize);
8099 if (item_size < sizeof(*ei)) {
8100 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
8101 struct btrfs_extent_item_v0 *ei0;
8102 if (item_size != sizeof(*ei0)) {
8104 "invalid extent item format: ITEM[%llu %u %llu] leaf: %llu slot: %d",
8105 key.objectid, key.type, key.offset,
8106 btrfs_header_bytenr(eb), slot);
8109 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
8110 refs = btrfs_extent_refs_v0(eb, ei0);
8114 memset(&tmpl, 0, sizeof(tmpl));
8115 tmpl.start = key.objectid;
8116 tmpl.nr = num_bytes;
8117 tmpl.extent_item_refs = refs;
8118 tmpl.metadata = metadata;
8120 tmpl.max_size = num_bytes;
8122 return add_extent_rec(extent_cache, &tmpl);
8125 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
8126 refs = btrfs_extent_refs(eb, ei);
8127 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
8131 if (metadata && num_bytes != root->fs_info->nodesize) {
8132 error("ignore invalid metadata extent, length %llu does not equal to %u",
8133 num_bytes, root->fs_info->nodesize);
8136 if (!metadata && !IS_ALIGNED(num_bytes, root->fs_info->sectorsize)) {
8137 error("ignore invalid data extent, length %llu is not aligned to %u",
8138 num_bytes, root->fs_info->sectorsize);
8142 memset(&tmpl, 0, sizeof(tmpl));
8143 tmpl.start = key.objectid;
8144 tmpl.nr = num_bytes;
8145 tmpl.extent_item_refs = refs;
8146 tmpl.metadata = metadata;
8148 tmpl.max_size = num_bytes;
8149 add_extent_rec(extent_cache, &tmpl);
8151 ptr = (unsigned long)(ei + 1);
8152 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
8153 key.type == BTRFS_EXTENT_ITEM_KEY)
8154 ptr += sizeof(struct btrfs_tree_block_info);
8156 end = (unsigned long)ei + item_size;
8158 iref = (struct btrfs_extent_inline_ref *)ptr;
8159 type = btrfs_extent_inline_ref_type(eb, iref);
8160 offset = btrfs_extent_inline_ref_offset(eb, iref);
8162 case BTRFS_TREE_BLOCK_REF_KEY:
8163 ret = add_tree_backref(extent_cache, key.objectid,
8167 "add_tree_backref failed (extent items tree block): %s",
8170 case BTRFS_SHARED_BLOCK_REF_KEY:
8171 ret = add_tree_backref(extent_cache, key.objectid,
8175 "add_tree_backref failed (extent items shared block): %s",
8178 case BTRFS_EXTENT_DATA_REF_KEY:
8179 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
8180 add_data_backref(extent_cache, key.objectid, 0,
8181 btrfs_extent_data_ref_root(eb, dref),
8182 btrfs_extent_data_ref_objectid(eb,
8184 btrfs_extent_data_ref_offset(eb, dref),
8185 btrfs_extent_data_ref_count(eb, dref),
8188 case BTRFS_SHARED_DATA_REF_KEY:
8189 sref = (struct btrfs_shared_data_ref *)(iref + 1);
8190 add_data_backref(extent_cache, key.objectid, offset,
8192 btrfs_shared_data_ref_count(eb, sref),
8196 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
8197 key.objectid, key.type, num_bytes);
8200 ptr += btrfs_extent_inline_ref_size(type);
8207 static int check_cache_range(struct btrfs_root *root,
8208 struct btrfs_block_group_cache *cache,
8209 u64 offset, u64 bytes)
8211 struct btrfs_free_space *entry;
8217 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
8218 bytenr = btrfs_sb_offset(i);
8219 ret = btrfs_rmap_block(root->fs_info,
8220 cache->key.objectid, bytenr, 0,
8221 &logical, &nr, &stripe_len);
8226 if (logical[nr] + stripe_len <= offset)
8228 if (offset + bytes <= logical[nr])
8230 if (logical[nr] == offset) {
8231 if (stripe_len >= bytes) {
8235 bytes -= stripe_len;
8236 offset += stripe_len;
8237 } else if (logical[nr] < offset) {
8238 if (logical[nr] + stripe_len >=
8243 bytes = (offset + bytes) -
8244 (logical[nr] + stripe_len);
8245 offset = logical[nr] + stripe_len;
8248 * Could be tricky, the super may land in the
8249 * middle of the area we're checking. First
8250 * check the easiest case, it's at the end.
8252 if (logical[nr] + stripe_len >=
8254 bytes = logical[nr] - offset;
8258 /* Check the left side */
8259 ret = check_cache_range(root, cache,
8261 logical[nr] - offset);
8267 /* Now we continue with the right side */
8268 bytes = (offset + bytes) -
8269 (logical[nr] + stripe_len);
8270 offset = logical[nr] + stripe_len;
8277 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
8279 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
8280 offset, offset+bytes);
8284 if (entry->offset != offset) {
8285 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
8290 if (entry->bytes != bytes) {
8291 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
8292 bytes, entry->bytes, offset);
8296 unlink_free_space(cache->free_space_ctl, entry);
8301 static int verify_space_cache(struct btrfs_root *root,
8302 struct btrfs_block_group_cache *cache)
8304 struct btrfs_path path;
8305 struct extent_buffer *leaf;
8306 struct btrfs_key key;
8310 root = root->fs_info->extent_root;
8312 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
8314 btrfs_init_path(&path);
8315 key.objectid = last;
8317 key.type = BTRFS_EXTENT_ITEM_KEY;
8318 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
8323 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
8324 ret = btrfs_next_leaf(root, &path);
8332 leaf = path.nodes[0];
8333 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
8334 if (key.objectid >= cache->key.offset + cache->key.objectid)
8336 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
8337 key.type != BTRFS_METADATA_ITEM_KEY) {
8342 if (last == key.objectid) {
8343 if (key.type == BTRFS_EXTENT_ITEM_KEY)
8344 last = key.objectid + key.offset;
8346 last = key.objectid + root->fs_info->nodesize;
8351 ret = check_cache_range(root, cache, last,
8352 key.objectid - last);
8355 if (key.type == BTRFS_EXTENT_ITEM_KEY)
8356 last = key.objectid + key.offset;
8358 last = key.objectid + root->fs_info->nodesize;
8362 if (last < cache->key.objectid + cache->key.offset)
8363 ret = check_cache_range(root, cache, last,
8364 cache->key.objectid +
8365 cache->key.offset - last);
8368 btrfs_release_path(&path);
8371 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
8372 fprintf(stderr, "There are still entries left in the space "
8380 static int check_space_cache(struct btrfs_root *root)
8382 struct btrfs_block_group_cache *cache;
8383 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
8387 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
8388 btrfs_super_generation(root->fs_info->super_copy) !=
8389 btrfs_super_cache_generation(root->fs_info->super_copy)) {
8390 printf("cache and super generation don't match, space cache "
8391 "will be invalidated\n");
8395 if (ctx.progress_enabled) {
8396 ctx.tp = TASK_FREE_SPACE;
8397 task_start(ctx.info);
8401 cache = btrfs_lookup_first_block_group(root->fs_info, start);
8405 start = cache->key.objectid + cache->key.offset;
8406 if (!cache->free_space_ctl) {
8407 if (btrfs_init_free_space_ctl(cache,
8408 root->fs_info->sectorsize)) {
8413 btrfs_remove_free_space_cache(cache);
8416 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) {
8417 ret = exclude_super_stripes(root, cache);
8419 fprintf(stderr, "could not exclude super stripes: %s\n",
8424 ret = load_free_space_tree(root->fs_info, cache);
8425 free_excluded_extents(root, cache);
8427 fprintf(stderr, "could not load free space tree: %s\n",
8434 ret = load_free_space_cache(root->fs_info, cache);
8439 ret = verify_space_cache(root, cache);
8441 fprintf(stderr, "cache appears valid but isn't %Lu\n",
8442 cache->key.objectid);
8447 task_stop(ctx.info);
8449 return error ? -EINVAL : 0;
8452 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
8453 u64 num_bytes, unsigned long leaf_offset,
8454 struct extent_buffer *eb) {
8456 struct btrfs_fs_info *fs_info = root->fs_info;
8458 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
8460 unsigned long csum_offset;
8464 u64 data_checked = 0;
8470 if (num_bytes % fs_info->sectorsize)
8473 data = malloc(num_bytes);
8477 while (offset < num_bytes) {
8480 read_len = num_bytes - offset;
8481 /* read as much space once a time */
8482 ret = read_extent_data(fs_info, data + offset,
8483 bytenr + offset, &read_len, mirror);
8487 /* verify every 4k data's checksum */
8488 while (data_checked < read_len) {
8490 tmp = offset + data_checked;
8492 csum = btrfs_csum_data((char *)data + tmp,
8493 csum, fs_info->sectorsize);
8494 btrfs_csum_final(csum, (u8 *)&csum);
8496 csum_offset = leaf_offset +
8497 tmp / fs_info->sectorsize * csum_size;
8498 read_extent_buffer(eb, (char *)&csum_expected,
8499 csum_offset, csum_size);
8500 /* try another mirror */
8501 if (csum != csum_expected) {
8502 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
8503 mirror, bytenr + tmp,
8504 csum, csum_expected);
8505 num_copies = btrfs_num_copies(root->fs_info,
8507 if (mirror < num_copies - 1) {
8512 data_checked += fs_info->sectorsize;
8521 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
8524 struct btrfs_path path;
8525 struct extent_buffer *leaf;
8526 struct btrfs_key key;
8529 btrfs_init_path(&path);
8530 key.objectid = bytenr;
8531 key.type = BTRFS_EXTENT_ITEM_KEY;
8532 key.offset = (u64)-1;
8535 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, &path,
8538 fprintf(stderr, "Error looking up extent record %d\n", ret);
8539 btrfs_release_path(&path);
8542 if (path.slots[0] > 0) {
8545 ret = btrfs_prev_leaf(root, &path);
8548 } else if (ret > 0) {
8555 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
8558 * Block group items come before extent items if they have the same
8559 * bytenr, so walk back one more just in case. Dear future traveller,
8560 * first congrats on mastering time travel. Now if it's not too much
8561 * trouble could you go back to 2006 and tell Chris to make the
8562 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
8563 * EXTENT_ITEM_KEY please?
8565 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
8566 if (path.slots[0] > 0) {
8569 ret = btrfs_prev_leaf(root, &path);
8572 } else if (ret > 0) {
8577 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
8581 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
8582 ret = btrfs_next_leaf(root, &path);
8584 fprintf(stderr, "Error going to next leaf "
8586 btrfs_release_path(&path);
8592 leaf = path.nodes[0];
8593 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
8594 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
8598 if (key.objectid + key.offset < bytenr) {
8602 if (key.objectid > bytenr + num_bytes)
8605 if (key.objectid == bytenr) {
8606 if (key.offset >= num_bytes) {
8610 num_bytes -= key.offset;
8611 bytenr += key.offset;
8612 } else if (key.objectid < bytenr) {
8613 if (key.objectid + key.offset >= bytenr + num_bytes) {
8617 num_bytes = (bytenr + num_bytes) -
8618 (key.objectid + key.offset);
8619 bytenr = key.objectid + key.offset;
8621 if (key.objectid + key.offset < bytenr + num_bytes) {
8622 u64 new_start = key.objectid + key.offset;
8623 u64 new_bytes = bytenr + num_bytes - new_start;
8626 * Weird case, the extent is in the middle of
8627 * our range, we'll have to search one side
8628 * and then the other. Not sure if this happens
8629 * in real life, but no harm in coding it up
8630 * anyway just in case.
8632 btrfs_release_path(&path);
8633 ret = check_extent_exists(root, new_start,
8636 fprintf(stderr, "Right section didn't "
8640 num_bytes = key.objectid - bytenr;
8643 num_bytes = key.objectid - bytenr;
8650 if (num_bytes && !ret) {
8651 fprintf(stderr, "There are no extents for csum range "
8652 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
8656 btrfs_release_path(&path);
8660 static int check_csums(struct btrfs_root *root)
8662 struct btrfs_path path;
8663 struct extent_buffer *leaf;
8664 struct btrfs_key key;
8665 u64 offset = 0, num_bytes = 0;
8666 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
8670 unsigned long leaf_offset;
8672 root = root->fs_info->csum_root;
8673 if (!extent_buffer_uptodate(root->node)) {
8674 fprintf(stderr, "No valid csum tree found\n");
8678 btrfs_init_path(&path);
8679 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
8680 key.type = BTRFS_EXTENT_CSUM_KEY;
8682 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
8684 fprintf(stderr, "Error searching csum tree %d\n", ret);
8685 btrfs_release_path(&path);
8689 if (ret > 0 && path.slots[0])
8694 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
8695 ret = btrfs_next_leaf(root, &path);
8697 fprintf(stderr, "Error going to next leaf "
8704 leaf = path.nodes[0];
8706 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
8707 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
8712 data_len = (btrfs_item_size_nr(leaf, path.slots[0]) /
8713 csum_size) * root->fs_info->sectorsize;
8714 if (!check_data_csum)
8715 goto skip_csum_check;
8716 leaf_offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
8717 ret = check_extent_csums(root, key.offset, data_len,
8723 offset = key.offset;
8724 } else if (key.offset != offset + num_bytes) {
8725 ret = check_extent_exists(root, offset, num_bytes);
8727 fprintf(stderr, "Csum exists for %Lu-%Lu but "
8728 "there is no extent record\n",
8729 offset, offset+num_bytes);
8732 offset = key.offset;
8735 num_bytes += data_len;
8739 btrfs_release_path(&path);
8743 static int is_dropped_key(struct btrfs_key *key,
8744 struct btrfs_key *drop_key) {
8745 if (key->objectid < drop_key->objectid)
8747 else if (key->objectid == drop_key->objectid) {
8748 if (key->type < drop_key->type)
8750 else if (key->type == drop_key->type) {
8751 if (key->offset < drop_key->offset)
8759 * Here are the rules for FULL_BACKREF.
8761 * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
8762 * 2) If btrfs_header_owner(buf) no longer points to buf then we have
8764 * 3) We cowed the block walking down a reloc tree. This is impossible to tell
8765 * if it happened after the relocation occurred since we'll have dropped the
8766 * reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
8767 * have no real way to know for sure.
8769 * We process the blocks one root at a time, and we start from the lowest root
8770 * objectid and go to the highest. So we can just lookup the owner backref for
8771 * the record and if we don't find it then we know it doesn't exist and we have
8774 * FIXME: if we ever start reclaiming root objectid's then we need to fix this
8775 * assumption and simply indicate that we _think_ that the FULL BACKREF needs to
8776 * be set or not and then we can check later once we've gathered all the refs.
8778 static int calc_extent_flag(struct cache_tree *extent_cache,
8779 struct extent_buffer *buf,
8780 struct root_item_record *ri,
8783 struct extent_record *rec;
8784 struct cache_extent *cache;
8785 struct tree_backref *tback;
8788 cache = lookup_cache_extent(extent_cache, buf->start, 1);
8789 /* we have added this extent before */
8793 rec = container_of(cache, struct extent_record, cache);
8796 * Except file/reloc tree, we can not have
8799 if (ri->objectid < BTRFS_FIRST_FREE_OBJECTID)
8804 if (buf->start == ri->bytenr)
8807 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
8810 owner = btrfs_header_owner(buf);
8811 if (owner == ri->objectid)
8814 tback = find_tree_backref(rec, 0, owner);
8819 if (rec->flag_block_full_backref != FLAG_UNSET &&
8820 rec->flag_block_full_backref != 0)
8821 rec->bad_full_backref = 1;
8824 *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8825 if (rec->flag_block_full_backref != FLAG_UNSET &&
8826 rec->flag_block_full_backref != 1)
8827 rec->bad_full_backref = 1;
8831 static void report_mismatch_key_root(u8 key_type, u64 rootid)
8833 fprintf(stderr, "Invalid key type(");
8834 print_key_type(stderr, 0, key_type);
8835 fprintf(stderr, ") found in root(");
8836 print_objectid(stderr, rootid, 0);
8837 fprintf(stderr, ")\n");
8841 * Check if the key is valid with its extent buffer.
8843 * This is a early check in case invalid key exists in a extent buffer
8844 * This is not comprehensive yet, but should prevent wrong key/item passed
8847 static int check_type_with_root(u64 rootid, u8 key_type)
8850 /* Only valid in chunk tree */
8851 case BTRFS_DEV_ITEM_KEY:
8852 case BTRFS_CHUNK_ITEM_KEY:
8853 if (rootid != BTRFS_CHUNK_TREE_OBJECTID)
8856 /* valid in csum and log tree */
8857 case BTRFS_CSUM_TREE_OBJECTID:
8858 if (!(rootid == BTRFS_TREE_LOG_OBJECTID ||
8862 case BTRFS_EXTENT_ITEM_KEY:
8863 case BTRFS_METADATA_ITEM_KEY:
8864 case BTRFS_BLOCK_GROUP_ITEM_KEY:
8865 if (rootid != BTRFS_EXTENT_TREE_OBJECTID)
8868 case BTRFS_ROOT_ITEM_KEY:
8869 if (rootid != BTRFS_ROOT_TREE_OBJECTID)
8872 case BTRFS_DEV_EXTENT_KEY:
8873 if (rootid != BTRFS_DEV_TREE_OBJECTID)
8879 report_mismatch_key_root(key_type, rootid);
8883 static int run_next_block(struct btrfs_root *root,
8884 struct block_info *bits,
8887 struct cache_tree *pending,
8888 struct cache_tree *seen,
8889 struct cache_tree *reada,
8890 struct cache_tree *nodes,
8891 struct cache_tree *extent_cache,
8892 struct cache_tree *chunk_cache,
8893 struct rb_root *dev_cache,
8894 struct block_group_tree *block_group_cache,
8895 struct device_extent_tree *dev_extent_cache,
8896 struct root_item_record *ri)
8898 struct btrfs_fs_info *fs_info = root->fs_info;
8899 struct extent_buffer *buf;
8900 struct extent_record *rec = NULL;
8911 struct btrfs_key key;
8912 struct cache_extent *cache;
8915 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
8916 bits_nr, &reada_bits);
8921 for(i = 0; i < nritems; i++) {
8922 ret = add_cache_extent(reada, bits[i].start,
8927 /* fixme, get the parent transid */
8928 readahead_tree_block(fs_info, bits[i].start, 0);
8931 *last = bits[0].start;
8932 bytenr = bits[0].start;
8933 size = bits[0].size;
8935 cache = lookup_cache_extent(pending, bytenr, size);
8937 remove_cache_extent(pending, cache);
8940 cache = lookup_cache_extent(reada, bytenr, size);
8942 remove_cache_extent(reada, cache);
8945 cache = lookup_cache_extent(nodes, bytenr, size);
8947 remove_cache_extent(nodes, cache);
8950 cache = lookup_cache_extent(extent_cache, bytenr, size);
8952 rec = container_of(cache, struct extent_record, cache);
8953 gen = rec->parent_generation;
8956 /* fixme, get the real parent transid */
8957 buf = read_tree_block(root->fs_info, bytenr, gen);
8958 if (!extent_buffer_uptodate(buf)) {
8959 record_bad_block_io(root->fs_info,
8960 extent_cache, bytenr, size);
8964 nritems = btrfs_header_nritems(buf);
8967 if (!init_extent_tree) {
8968 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
8969 btrfs_header_level(buf), 1, NULL,
8972 ret = calc_extent_flag(extent_cache, buf, ri, &flags);
8974 fprintf(stderr, "Couldn't calc extent flags\n");
8975 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8980 ret = calc_extent_flag(extent_cache, buf, ri, &flags);
8982 fprintf(stderr, "Couldn't calc extent flags\n");
8983 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8987 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
8989 ri->objectid != BTRFS_TREE_RELOC_OBJECTID &&
8990 ri->objectid == btrfs_header_owner(buf)) {
8992 * Ok we got to this block from it's original owner and
8993 * we have FULL_BACKREF set. Relocation can leave
8994 * converted blocks over so this is altogether possible,
8995 * however it's not possible if the generation > the
8996 * last snapshot, so check for this case.
8998 if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC) &&
8999 btrfs_header_generation(buf) > ri->last_snapshot) {
9000 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
9001 rec->bad_full_backref = 1;
9006 (ri->objectid == BTRFS_TREE_RELOC_OBJECTID ||
9007 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
9008 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
9009 rec->bad_full_backref = 1;
9013 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
9014 rec->flag_block_full_backref = 1;
9018 rec->flag_block_full_backref = 0;
9020 owner = btrfs_header_owner(buf);
9023 ret = check_block(root, extent_cache, buf, flags);
9027 if (btrfs_is_leaf(buf)) {
9028 btree_space_waste += btrfs_leaf_free_space(root, buf);
9029 for (i = 0; i < nritems; i++) {
9030 struct btrfs_file_extent_item *fi;
9031 btrfs_item_key_to_cpu(buf, &key, i);
9033 * Check key type against the leaf owner.
9034 * Could filter quite a lot of early error if
9037 if (check_type_with_root(btrfs_header_owner(buf),
9039 fprintf(stderr, "ignoring invalid key\n");
9042 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
9043 process_extent_item(root, extent_cache, buf,
9047 if (key.type == BTRFS_METADATA_ITEM_KEY) {
9048 process_extent_item(root, extent_cache, buf,
9052 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
9054 btrfs_item_size_nr(buf, i);
9057 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
9058 process_chunk_item(chunk_cache, &key, buf, i);
9061 if (key.type == BTRFS_DEV_ITEM_KEY) {
9062 process_device_item(dev_cache, &key, buf, i);
9065 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
9066 process_block_group_item(block_group_cache,
9070 if (key.type == BTRFS_DEV_EXTENT_KEY) {
9071 process_device_extent_item(dev_extent_cache,
9076 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
9077 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
9078 process_extent_ref_v0(extent_cache, buf, i);
9085 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
9086 ret = add_tree_backref(extent_cache,
9087 key.objectid, 0, key.offset, 0);
9090 "add_tree_backref failed (leaf tree block): %s",
9094 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
9095 ret = add_tree_backref(extent_cache,
9096 key.objectid, key.offset, 0, 0);
9099 "add_tree_backref failed (leaf shared block): %s",
9103 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
9104 struct btrfs_extent_data_ref *ref;
9105 ref = btrfs_item_ptr(buf, i,
9106 struct btrfs_extent_data_ref);
9107 add_data_backref(extent_cache,
9109 btrfs_extent_data_ref_root(buf, ref),
9110 btrfs_extent_data_ref_objectid(buf,
9112 btrfs_extent_data_ref_offset(buf, ref),
9113 btrfs_extent_data_ref_count(buf, ref),
9114 0, root->fs_info->sectorsize);
9117 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
9118 struct btrfs_shared_data_ref *ref;
9119 ref = btrfs_item_ptr(buf, i,
9120 struct btrfs_shared_data_ref);
9121 add_data_backref(extent_cache,
9122 key.objectid, key.offset, 0, 0, 0,
9123 btrfs_shared_data_ref_count(buf, ref),
9124 0, root->fs_info->sectorsize);
9127 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
9128 struct bad_item *bad;
9130 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
9134 bad = malloc(sizeof(struct bad_item));
9137 INIT_LIST_HEAD(&bad->list);
9138 memcpy(&bad->key, &key,
9139 sizeof(struct btrfs_key));
9140 bad->root_id = owner;
9141 list_add_tail(&bad->list, &delete_items);
9144 if (key.type != BTRFS_EXTENT_DATA_KEY)
9146 fi = btrfs_item_ptr(buf, i,
9147 struct btrfs_file_extent_item);
9148 if (btrfs_file_extent_type(buf, fi) ==
9149 BTRFS_FILE_EXTENT_INLINE)
9151 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
9154 data_bytes_allocated +=
9155 btrfs_file_extent_disk_num_bytes(buf, fi);
9156 if (data_bytes_allocated < root->fs_info->sectorsize) {
9159 data_bytes_referenced +=
9160 btrfs_file_extent_num_bytes(buf, fi);
9161 add_data_backref(extent_cache,
9162 btrfs_file_extent_disk_bytenr(buf, fi),
9163 parent, owner, key.objectid, key.offset -
9164 btrfs_file_extent_offset(buf, fi), 1, 1,
9165 btrfs_file_extent_disk_num_bytes(buf, fi));
9169 struct btrfs_key first_key;
9171 first_key.objectid = 0;
9174 btrfs_item_key_to_cpu(buf, &first_key, 0);
9175 level = btrfs_header_level(buf);
9176 for (i = 0; i < nritems; i++) {
9177 struct extent_record tmpl;
9179 ptr = btrfs_node_blockptr(buf, i);
9180 size = root->fs_info->nodesize;
9181 btrfs_node_key_to_cpu(buf, &key, i);
9183 if ((level == ri->drop_level)
9184 && is_dropped_key(&key, &ri->drop_key)) {
9189 memset(&tmpl, 0, sizeof(tmpl));
9190 btrfs_cpu_key_to_disk(&tmpl.parent_key, &key);
9191 tmpl.parent_generation = btrfs_node_ptr_generation(buf, i);
9196 tmpl.max_size = size;
9197 ret = add_extent_rec(extent_cache, &tmpl);
9201 ret = add_tree_backref(extent_cache, ptr, parent,
9205 "add_tree_backref failed (non-leaf block): %s",
9211 add_pending(nodes, seen, ptr, size);
9213 add_pending(pending, seen, ptr, size);
9216 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(fs_info) -
9217 nritems) * sizeof(struct btrfs_key_ptr);
9219 total_btree_bytes += buf->len;
9220 if (fs_root_objectid(btrfs_header_owner(buf)))
9221 total_fs_tree_bytes += buf->len;
9222 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
9223 total_extent_tree_bytes += buf->len;
9225 free_extent_buffer(buf);
9229 static int add_root_to_pending(struct extent_buffer *buf,
9230 struct cache_tree *extent_cache,
9231 struct cache_tree *pending,
9232 struct cache_tree *seen,
9233 struct cache_tree *nodes,
9236 struct extent_record tmpl;
9239 if (btrfs_header_level(buf) > 0)
9240 add_pending(nodes, seen, buf->start, buf->len);
9242 add_pending(pending, seen, buf->start, buf->len);
9244 memset(&tmpl, 0, sizeof(tmpl));
9245 tmpl.start = buf->start;
9250 tmpl.max_size = buf->len;
9251 add_extent_rec(extent_cache, &tmpl);
9253 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
9254 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
9255 ret = add_tree_backref(extent_cache, buf->start, buf->start,
9258 ret = add_tree_backref(extent_cache, buf->start, 0, objectid,
9263 /* as we fix the tree, we might be deleting blocks that
9264 * we're tracking for repair. This hook makes sure we
9265 * remove any backrefs for blocks as we are fixing them.
9267 static int free_extent_hook(struct btrfs_trans_handle *trans,
9268 struct btrfs_root *root,
9269 u64 bytenr, u64 num_bytes, u64 parent,
9270 u64 root_objectid, u64 owner, u64 offset,
9273 struct extent_record *rec;
9274 struct cache_extent *cache;
9276 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
9278 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
9279 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
9283 rec = container_of(cache, struct extent_record, cache);
9285 struct data_backref *back;
9286 back = find_data_backref(rec, parent, root_objectid, owner,
9287 offset, 1, bytenr, num_bytes);
9290 if (back->node.found_ref) {
9291 back->found_ref -= refs_to_drop;
9293 rec->refs -= refs_to_drop;
9295 if (back->node.found_extent_tree) {
9296 back->num_refs -= refs_to_drop;
9297 if (rec->extent_item_refs)
9298 rec->extent_item_refs -= refs_to_drop;
9300 if (back->found_ref == 0)
9301 back->node.found_ref = 0;
9302 if (back->num_refs == 0)
9303 back->node.found_extent_tree = 0;
9305 if (!back->node.found_extent_tree && back->node.found_ref) {
9306 rb_erase(&back->node.node, &rec->backref_tree);
9310 struct tree_backref *back;
9311 back = find_tree_backref(rec, parent, root_objectid);
9314 if (back->node.found_ref) {
9317 back->node.found_ref = 0;
9319 if (back->node.found_extent_tree) {
9320 if (rec->extent_item_refs)
9321 rec->extent_item_refs--;
9322 back->node.found_extent_tree = 0;
9324 if (!back->node.found_extent_tree && back->node.found_ref) {
9325 rb_erase(&back->node.node, &rec->backref_tree);
9329 maybe_free_extent_rec(extent_cache, rec);
9334 static int delete_extent_records(struct btrfs_trans_handle *trans,
9335 struct btrfs_root *root,
9336 struct btrfs_path *path,
9339 struct btrfs_key key;
9340 struct btrfs_key found_key;
9341 struct extent_buffer *leaf;
9346 key.objectid = bytenr;
9348 key.offset = (u64)-1;
9351 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
9358 if (path->slots[0] == 0)
9364 leaf = path->nodes[0];
9365 slot = path->slots[0];
9367 btrfs_item_key_to_cpu(leaf, &found_key, slot);
9368 if (found_key.objectid != bytenr)
9371 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
9372 found_key.type != BTRFS_METADATA_ITEM_KEY &&
9373 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
9374 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
9375 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
9376 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
9377 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
9378 btrfs_release_path(path);
9379 if (found_key.type == 0) {
9380 if (found_key.offset == 0)
9382 key.offset = found_key.offset - 1;
9383 key.type = found_key.type;
9385 key.type = found_key.type - 1;
9386 key.offset = (u64)-1;
9390 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
9391 found_key.objectid, found_key.type, found_key.offset);
9393 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
9396 btrfs_release_path(path);
9398 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
9399 found_key.type == BTRFS_METADATA_ITEM_KEY) {
9400 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
9401 found_key.offset : root->fs_info->nodesize;
9403 ret = btrfs_update_block_group(root, bytenr,
9410 btrfs_release_path(path);
9415 * for a single backref, this will allocate a new extent
9416 * and add the backref to it.
9418 static int record_extent(struct btrfs_trans_handle *trans,
9419 struct btrfs_fs_info *info,
9420 struct btrfs_path *path,
9421 struct extent_record *rec,
9422 struct extent_backref *back,
9423 int allocated, u64 flags)
9426 struct btrfs_root *extent_root = info->extent_root;
9427 struct extent_buffer *leaf;
9428 struct btrfs_key ins_key;
9429 struct btrfs_extent_item *ei;
9430 struct data_backref *dback;
9431 struct btrfs_tree_block_info *bi;
9434 rec->max_size = max_t(u64, rec->max_size,
9438 u32 item_size = sizeof(*ei);
9441 item_size += sizeof(*bi);
9443 ins_key.objectid = rec->start;
9444 ins_key.offset = rec->max_size;
9445 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
9447 ret = btrfs_insert_empty_item(trans, extent_root, path,
9448 &ins_key, item_size);
9452 leaf = path->nodes[0];
9453 ei = btrfs_item_ptr(leaf, path->slots[0],
9454 struct btrfs_extent_item);
9456 btrfs_set_extent_refs(leaf, ei, 0);
9457 btrfs_set_extent_generation(leaf, ei, rec->generation);
9459 if (back->is_data) {
9460 btrfs_set_extent_flags(leaf, ei,
9461 BTRFS_EXTENT_FLAG_DATA);
9463 struct btrfs_disk_key copy_key;;
9465 bi = (struct btrfs_tree_block_info *)(ei + 1);
9466 memset_extent_buffer(leaf, 0, (unsigned long)bi,
9469 btrfs_set_disk_key_objectid(©_key,
9470 rec->info_objectid);
9471 btrfs_set_disk_key_type(©_key, 0);
9472 btrfs_set_disk_key_offset(©_key, 0);
9474 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
9475 btrfs_set_tree_block_key(leaf, bi, ©_key);
9477 btrfs_set_extent_flags(leaf, ei,
9478 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
9481 btrfs_mark_buffer_dirty(leaf);
9482 ret = btrfs_update_block_group(extent_root, rec->start,
9483 rec->max_size, 1, 0);
9486 btrfs_release_path(path);
9489 if (back->is_data) {
9493 dback = to_data_backref(back);
9494 if (back->full_backref)
9495 parent = dback->parent;
9499 for (i = 0; i < dback->found_ref; i++) {
9500 /* if parent != 0, we're doing a full backref
9501 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
9502 * just makes the backref allocator create a data
9505 ret = btrfs_inc_extent_ref(trans, info->extent_root,
9506 rec->start, rec->max_size,
9510 BTRFS_FIRST_FREE_OBJECTID :
9516 fprintf(stderr, "adding new data backref"
9517 " on %llu %s %llu owner %llu"
9518 " offset %llu found %d\n",
9519 (unsigned long long)rec->start,
9520 back->full_backref ?
9522 back->full_backref ?
9523 (unsigned long long)parent :
9524 (unsigned long long)dback->root,
9525 (unsigned long long)dback->owner,
9526 (unsigned long long)dback->offset,
9530 struct tree_backref *tback;
9532 tback = to_tree_backref(back);
9533 if (back->full_backref)
9534 parent = tback->parent;
9538 ret = btrfs_inc_extent_ref(trans, info->extent_root,
9539 rec->start, rec->max_size,
9540 parent, tback->root, 0, 0);
9541 fprintf(stderr, "adding new tree backref on "
9542 "start %llu len %llu parent %llu root %llu\n",
9543 rec->start, rec->max_size, parent, tback->root);
9546 btrfs_release_path(path);
9550 static struct extent_entry *find_entry(struct list_head *entries,
9551 u64 bytenr, u64 bytes)
9553 struct extent_entry *entry = NULL;
9555 list_for_each_entry(entry, entries, list) {
9556 if (entry->bytenr == bytenr && entry->bytes == bytes)
9563 static struct extent_entry *find_most_right_entry(struct list_head *entries)
9565 struct extent_entry *entry, *best = NULL, *prev = NULL;
9567 list_for_each_entry(entry, entries, list) {
9569 * If there are as many broken entries as entries then we know
9570 * not to trust this particular entry.
9572 if (entry->broken == entry->count)
9576 * Special case, when there are only two entries and 'best' is
9586 * If our current entry == best then we can't be sure our best
9587 * is really the best, so we need to keep searching.
9589 if (best && best->count == entry->count) {
9595 /* Prev == entry, not good enough, have to keep searching */
9596 if (!prev->broken && prev->count == entry->count)
9600 best = (prev->count > entry->count) ? prev : entry;
9601 else if (best->count < entry->count)
9609 static int repair_ref(struct btrfs_fs_info *info, struct btrfs_path *path,
9610 struct data_backref *dback, struct extent_entry *entry)
9612 struct btrfs_trans_handle *trans;
9613 struct btrfs_root *root;
9614 struct btrfs_file_extent_item *fi;
9615 struct extent_buffer *leaf;
9616 struct btrfs_key key;
9620 key.objectid = dback->root;
9621 key.type = BTRFS_ROOT_ITEM_KEY;
9622 key.offset = (u64)-1;
9623 root = btrfs_read_fs_root(info, &key);
9625 fprintf(stderr, "Couldn't find root for our ref\n");
9630 * The backref points to the original offset of the extent if it was
9631 * split, so we need to search down to the offset we have and then walk
9632 * forward until we find the backref we're looking for.
9634 key.objectid = dback->owner;
9635 key.type = BTRFS_EXTENT_DATA_KEY;
9636 key.offset = dback->offset;
9637 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
9639 fprintf(stderr, "Error looking up ref %d\n", ret);
9644 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
9645 ret = btrfs_next_leaf(root, path);
9647 fprintf(stderr, "Couldn't find our ref, next\n");
9651 leaf = path->nodes[0];
9652 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
9653 if (key.objectid != dback->owner ||
9654 key.type != BTRFS_EXTENT_DATA_KEY) {
9655 fprintf(stderr, "Couldn't find our ref, search\n");
9658 fi = btrfs_item_ptr(leaf, path->slots[0],
9659 struct btrfs_file_extent_item);
9660 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
9661 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
9663 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
9668 btrfs_release_path(path);
9670 trans = btrfs_start_transaction(root, 1);
9672 return PTR_ERR(trans);
9675 * Ok we have the key of the file extent we want to fix, now we can cow
9676 * down to the thing and fix it.
9678 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
9680 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
9681 key.objectid, key.type, key.offset, ret);
9685 fprintf(stderr, "Well that's odd, we just found this key "
9686 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
9691 leaf = path->nodes[0];
9692 fi = btrfs_item_ptr(leaf, path->slots[0],
9693 struct btrfs_file_extent_item);
9695 if (btrfs_file_extent_compression(leaf, fi) &&
9696 dback->disk_bytenr != entry->bytenr) {
9697 fprintf(stderr, "Ref doesn't match the record start and is "
9698 "compressed, please take a btrfs-image of this file "
9699 "system and send it to a btrfs developer so they can "
9700 "complete this functionality for bytenr %Lu\n",
9701 dback->disk_bytenr);
9706 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
9707 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
9708 } else if (dback->disk_bytenr > entry->bytenr) {
9709 u64 off_diff, offset;
9711 off_diff = dback->disk_bytenr - entry->bytenr;
9712 offset = btrfs_file_extent_offset(leaf, fi);
9713 if (dback->disk_bytenr + offset +
9714 btrfs_file_extent_num_bytes(leaf, fi) >
9715 entry->bytenr + entry->bytes) {
9716 fprintf(stderr, "Ref is past the entry end, please "
9717 "take a btrfs-image of this file system and "
9718 "send it to a btrfs developer, ref %Lu\n",
9719 dback->disk_bytenr);
9724 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
9725 btrfs_set_file_extent_offset(leaf, fi, offset);
9726 } else if (dback->disk_bytenr < entry->bytenr) {
9729 offset = btrfs_file_extent_offset(leaf, fi);
9730 if (dback->disk_bytenr + offset < entry->bytenr) {
9731 fprintf(stderr, "Ref is before the entry start, please"
9732 " take a btrfs-image of this file system and "
9733 "send it to a btrfs developer, ref %Lu\n",
9734 dback->disk_bytenr);
9739 offset += dback->disk_bytenr;
9740 offset -= entry->bytenr;
9741 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
9742 btrfs_set_file_extent_offset(leaf, fi, offset);
9745 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
9748 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
9749 * only do this if we aren't using compression, otherwise it's a
9752 if (!btrfs_file_extent_compression(leaf, fi))
9753 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
9755 printf("ram bytes may be wrong?\n");
9756 btrfs_mark_buffer_dirty(leaf);
9758 err = btrfs_commit_transaction(trans, root);
9759 btrfs_release_path(path);
9760 return ret ? ret : err;
9763 static int verify_backrefs(struct btrfs_fs_info *info, struct btrfs_path *path,
9764 struct extent_record *rec)
9766 struct extent_backref *back, *tmp;
9767 struct data_backref *dback;
9768 struct extent_entry *entry, *best = NULL;
9771 int broken_entries = 0;
9776 * Metadata is easy and the backrefs should always agree on bytenr and
9777 * size, if not we've got bigger issues.
9782 rbtree_postorder_for_each_entry_safe(back, tmp,
9783 &rec->backref_tree, node) {
9784 if (back->full_backref || !back->is_data)
9787 dback = to_data_backref(back);
9790 * We only pay attention to backrefs that we found a real
9793 if (dback->found_ref == 0)
9797 * For now we only catch when the bytes don't match, not the
9798 * bytenr. We can easily do this at the same time, but I want
9799 * to have a fs image to test on before we just add repair
9800 * functionality willy-nilly so we know we won't screw up the
9804 entry = find_entry(&entries, dback->disk_bytenr,
9807 entry = malloc(sizeof(struct extent_entry));
9812 memset(entry, 0, sizeof(*entry));
9813 entry->bytenr = dback->disk_bytenr;
9814 entry->bytes = dback->bytes;
9815 list_add_tail(&entry->list, &entries);
9820 * If we only have on entry we may think the entries agree when
9821 * in reality they don't so we have to do some extra checking.
9823 if (dback->disk_bytenr != rec->start ||
9824 dback->bytes != rec->nr || back->broken)
9835 /* Yay all the backrefs agree, carry on good sir */
9836 if (nr_entries <= 1 && !mismatch)
9839 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
9840 "%Lu\n", rec->start);
9843 * First we want to see if the backrefs can agree amongst themselves who
9844 * is right, so figure out which one of the entries has the highest
9847 best = find_most_right_entry(&entries);
9850 * Ok so we may have an even split between what the backrefs think, so
9851 * this is where we use the extent ref to see what it thinks.
9854 entry = find_entry(&entries, rec->start, rec->nr);
9855 if (!entry && (!broken_entries || !rec->found_rec)) {
9856 fprintf(stderr, "Backrefs don't agree with each other "
9857 "and extent record doesn't agree with anybody,"
9858 " so we can't fix bytenr %Lu bytes %Lu\n",
9859 rec->start, rec->nr);
9862 } else if (!entry) {
9864 * Ok our backrefs were broken, we'll assume this is the
9865 * correct value and add an entry for this range.
9867 entry = malloc(sizeof(struct extent_entry));
9872 memset(entry, 0, sizeof(*entry));
9873 entry->bytenr = rec->start;
9874 entry->bytes = rec->nr;
9875 list_add_tail(&entry->list, &entries);
9879 best = find_most_right_entry(&entries);
9881 fprintf(stderr, "Backrefs and extent record evenly "
9882 "split on who is right, this is going to "
9883 "require user input to fix bytenr %Lu bytes "
9884 "%Lu\n", rec->start, rec->nr);
9891 * I don't think this can happen currently as we'll abort() if we catch
9892 * this case higher up, but in case somebody removes that we still can't
9893 * deal with it properly here yet, so just bail out of that's the case.
9895 if (best->bytenr != rec->start) {
9896 fprintf(stderr, "Extent start and backref starts don't match, "
9897 "please use btrfs-image on this file system and send "
9898 "it to a btrfs developer so they can make fsck fix "
9899 "this particular case. bytenr is %Lu, bytes is %Lu\n",
9900 rec->start, rec->nr);
9906 * Ok great we all agreed on an extent record, let's go find the real
9907 * references and fix up the ones that don't match.
9909 rbtree_postorder_for_each_entry_safe(back, tmp,
9910 &rec->backref_tree, node) {
9911 if (back->full_backref || !back->is_data)
9914 dback = to_data_backref(back);
9917 * Still ignoring backrefs that don't have a real ref attached
9920 if (dback->found_ref == 0)
9923 if (dback->bytes == best->bytes &&
9924 dback->disk_bytenr == best->bytenr)
9927 ret = repair_ref(info, path, dback, best);
9933 * Ok we messed with the actual refs, which means we need to drop our
9934 * entire cache and go back and rescan. I know this is a huge pain and
9935 * adds a lot of extra work, but it's the only way to be safe. Once all
9936 * the backrefs agree we may not need to do anything to the extent
9941 while (!list_empty(&entries)) {
9942 entry = list_entry(entries.next, struct extent_entry, list);
9943 list_del_init(&entry->list);
9949 static int process_duplicates(struct cache_tree *extent_cache,
9950 struct extent_record *rec)
9952 struct extent_record *good, *tmp;
9953 struct cache_extent *cache;
9957 * If we found a extent record for this extent then return, or if we
9958 * have more than one duplicate we are likely going to need to delete
9961 if (rec->found_rec || rec->num_duplicates > 1)
9964 /* Shouldn't happen but just in case */
9965 BUG_ON(!rec->num_duplicates);
9968 * So this happens if we end up with a backref that doesn't match the
9969 * actual extent entry. So either the backref is bad or the extent
9970 * entry is bad. Either way we want to have the extent_record actually
9971 * reflect what we found in the extent_tree, so we need to take the
9972 * duplicate out and use that as the extent_record since the only way we
9973 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
9975 remove_cache_extent(extent_cache, &rec->cache);
9977 good = to_extent_record(rec->dups.next);
9978 list_del_init(&good->list);
9979 INIT_LIST_HEAD(&good->backrefs);
9980 INIT_LIST_HEAD(&good->dups);
9981 good->cache.start = good->start;
9982 good->cache.size = good->nr;
9983 good->content_checked = 0;
9984 good->owner_ref_checked = 0;
9985 good->num_duplicates = 0;
9986 good->refs = rec->refs;
9987 list_splice_init(&rec->backrefs, &good->backrefs);
9989 cache = lookup_cache_extent(extent_cache, good->start,
9993 tmp = container_of(cache, struct extent_record, cache);
9996 * If we find another overlapping extent and it's found_rec is
9997 * set then it's a duplicate and we need to try and delete
10000 if (tmp->found_rec || tmp->num_duplicates > 0) {
10001 if (list_empty(&good->list))
10002 list_add_tail(&good->list,
10003 &duplicate_extents);
10004 good->num_duplicates += tmp->num_duplicates + 1;
10005 list_splice_init(&tmp->dups, &good->dups);
10006 list_del_init(&tmp->list);
10007 list_add_tail(&tmp->list, &good->dups);
10008 remove_cache_extent(extent_cache, &tmp->cache);
10013 * Ok we have another non extent item backed extent rec, so lets
10014 * just add it to this extent and carry on like we did above.
10016 good->refs += tmp->refs;
10017 list_splice_init(&tmp->backrefs, &good->backrefs);
10018 remove_cache_extent(extent_cache, &tmp->cache);
10021 ret = insert_cache_extent(extent_cache, &good->cache);
10024 return good->num_duplicates ? 0 : 1;
10027 static int delete_duplicate_records(struct btrfs_root *root,
10028 struct extent_record *rec)
10030 struct btrfs_trans_handle *trans;
10031 LIST_HEAD(delete_list);
10032 struct btrfs_path path;
10033 struct extent_record *tmp, *good, *n;
10036 struct btrfs_key key;
10038 btrfs_init_path(&path);
10041 /* Find the record that covers all of the duplicates. */
10042 list_for_each_entry(tmp, &rec->dups, list) {
10043 if (good->start < tmp->start)
10045 if (good->nr > tmp->nr)
10048 if (tmp->start + tmp->nr < good->start + good->nr) {
10049 fprintf(stderr, "Ok we have overlapping extents that "
10050 "aren't completely covered by each other, this "
10051 "is going to require more careful thought. "
10052 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
10053 tmp->start, tmp->nr, good->start, good->nr);
10060 list_add_tail(&rec->list, &delete_list);
10062 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
10065 list_move_tail(&tmp->list, &delete_list);
10068 root = root->fs_info->extent_root;
10069 trans = btrfs_start_transaction(root, 1);
10070 if (IS_ERR(trans)) {
10071 ret = PTR_ERR(trans);
10075 list_for_each_entry(tmp, &delete_list, list) {
10076 if (tmp->found_rec == 0)
10078 key.objectid = tmp->start;
10079 key.type = BTRFS_EXTENT_ITEM_KEY;
10080 key.offset = tmp->nr;
10082 /* Shouldn't happen but just in case */
10083 if (tmp->metadata) {
10084 fprintf(stderr, "Well this shouldn't happen, extent "
10085 "record overlaps but is metadata? "
10086 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
10090 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
10096 ret = btrfs_del_item(trans, root, &path);
10099 btrfs_release_path(&path);
10102 err = btrfs_commit_transaction(trans, root);
10106 while (!list_empty(&delete_list)) {
10107 tmp = to_extent_record(delete_list.next);
10108 list_del_init(&tmp->list);
10114 while (!list_empty(&rec->dups)) {
10115 tmp = to_extent_record(rec->dups.next);
10116 list_del_init(&tmp->list);
10120 btrfs_release_path(&path);
10122 if (!ret && !nr_del)
10123 rec->num_duplicates = 0;
10125 return ret ? ret : nr_del;
10128 static int find_possible_backrefs(struct btrfs_fs_info *info,
10129 struct btrfs_path *path,
10130 struct cache_tree *extent_cache,
10131 struct extent_record *rec)
10133 struct btrfs_root *root;
10134 struct extent_backref *back, *tmp;
10135 struct data_backref *dback;
10136 struct cache_extent *cache;
10137 struct btrfs_file_extent_item *fi;
10138 struct btrfs_key key;
10142 rbtree_postorder_for_each_entry_safe(back, tmp,
10143 &rec->backref_tree, node) {
10144 /* Don't care about full backrefs (poor unloved backrefs) */
10145 if (back->full_backref || !back->is_data)
10148 dback = to_data_backref(back);
10150 /* We found this one, we don't need to do a lookup */
10151 if (dback->found_ref)
10154 key.objectid = dback->root;
10155 key.type = BTRFS_ROOT_ITEM_KEY;
10156 key.offset = (u64)-1;
10158 root = btrfs_read_fs_root(info, &key);
10160 /* No root, definitely a bad ref, skip */
10161 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
10163 /* Other err, exit */
10165 return PTR_ERR(root);
10167 key.objectid = dback->owner;
10168 key.type = BTRFS_EXTENT_DATA_KEY;
10169 key.offset = dback->offset;
10170 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
10172 btrfs_release_path(path);
10175 /* Didn't find it, we can carry on */
10180 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
10181 struct btrfs_file_extent_item);
10182 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
10183 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
10184 btrfs_release_path(path);
10185 cache = lookup_cache_extent(extent_cache, bytenr, 1);
10187 struct extent_record *tmp;
10188 tmp = container_of(cache, struct extent_record, cache);
10191 * If we found an extent record for the bytenr for this
10192 * particular backref then we can't add it to our
10193 * current extent record. We only want to add backrefs
10194 * that don't have a corresponding extent item in the
10195 * extent tree since they likely belong to this record
10196 * and we need to fix it if it doesn't match bytenrs.
10198 if (tmp->found_rec)
10202 dback->found_ref += 1;
10203 dback->disk_bytenr = bytenr;
10204 dback->bytes = bytes;
10207 * Set this so the verify backref code knows not to trust the
10208 * values in this backref.
10217 * Record orphan data ref into corresponding root.
10219 * Return 0 if the extent item contains data ref and recorded.
10220 * Return 1 if the extent item contains no useful data ref
10221 * On that case, it may contains only shared_dataref or metadata backref
10222 * or the file extent exists(this should be handled by the extent bytenr
10223 * recovery routine)
10224 * Return <0 if something goes wrong.
10226 static int record_orphan_data_extents(struct btrfs_fs_info *fs_info,
10227 struct extent_record *rec)
10229 struct btrfs_key key;
10230 struct btrfs_root *dest_root;
10231 struct extent_backref *back, *tmp;
10232 struct data_backref *dback;
10233 struct orphan_data_extent *orphan;
10234 struct btrfs_path path;
10235 int recorded_data_ref = 0;
10240 btrfs_init_path(&path);
10241 rbtree_postorder_for_each_entry_safe(back, tmp,
10242 &rec->backref_tree, node) {
10243 if (back->full_backref || !back->is_data ||
10244 !back->found_extent_tree)
10246 dback = to_data_backref(back);
10247 if (dback->found_ref)
10249 key.objectid = dback->root;
10250 key.type = BTRFS_ROOT_ITEM_KEY;
10251 key.offset = (u64)-1;
10253 dest_root = btrfs_read_fs_root(fs_info, &key);
10255 /* For non-exist root we just skip it */
10256 if (IS_ERR(dest_root) || !dest_root)
10259 key.objectid = dback->owner;
10260 key.type = BTRFS_EXTENT_DATA_KEY;
10261 key.offset = dback->offset;
10263 ret = btrfs_search_slot(NULL, dest_root, &key, &path, 0, 0);
10264 btrfs_release_path(&path);
10266 * For ret < 0, it's OK since the fs-tree may be corrupted,
10267 * we need to record it for inode/file extent rebuild.
10268 * For ret > 0, we record it only for file extent rebuild.
10269 * For ret == 0, the file extent exists but only bytenr
10270 * mismatch, let the original bytenr fix routine to handle,
10276 orphan = malloc(sizeof(*orphan));
10281 INIT_LIST_HEAD(&orphan->list);
10282 orphan->root = dback->root;
10283 orphan->objectid = dback->owner;
10284 orphan->offset = dback->offset;
10285 orphan->disk_bytenr = rec->cache.start;
10286 orphan->disk_len = rec->cache.size;
10287 list_add(&dest_root->orphan_data_extents, &orphan->list);
10288 recorded_data_ref = 1;
10291 btrfs_release_path(&path);
10293 return !recorded_data_ref;
10299 * when an incorrect extent item is found, this will delete
10300 * all of the existing entries for it and recreate them
10301 * based on what the tree scan found.
10303 static int fixup_extent_refs(struct btrfs_fs_info *info,
10304 struct cache_tree *extent_cache,
10305 struct extent_record *rec)
10307 struct btrfs_trans_handle *trans = NULL;
10309 struct btrfs_path path;
10310 struct cache_extent *cache;
10311 struct extent_backref *back, *tmp;
10315 if (rec->flag_block_full_backref)
10316 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
10318 btrfs_init_path(&path);
10319 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
10321 * Sometimes the backrefs themselves are so broken they don't
10322 * get attached to any meaningful rec, so first go back and
10323 * check any of our backrefs that we couldn't find and throw
10324 * them into the list if we find the backref so that
10325 * verify_backrefs can figure out what to do.
10327 ret = find_possible_backrefs(info, &path, extent_cache, rec);
10332 /* step one, make sure all of the backrefs agree */
10333 ret = verify_backrefs(info, &path, rec);
10337 trans = btrfs_start_transaction(info->extent_root, 1);
10338 if (IS_ERR(trans)) {
10339 ret = PTR_ERR(trans);
10343 /* step two, delete all the existing records */
10344 ret = delete_extent_records(trans, info->extent_root, &path,
10350 /* was this block corrupt? If so, don't add references to it */
10351 cache = lookup_cache_extent(info->corrupt_blocks,
10352 rec->start, rec->max_size);
10358 /* step three, recreate all the refs we did find */
10359 rbtree_postorder_for_each_entry_safe(back, tmp,
10360 &rec->backref_tree, node) {
10362 * if we didn't find any references, don't create a
10363 * new extent record
10365 if (!back->found_ref)
10368 rec->bad_full_backref = 0;
10369 ret = record_extent(trans, info, &path, rec, back, allocated, flags);
10377 int err = btrfs_commit_transaction(trans, info->extent_root);
10383 fprintf(stderr, "Repaired extent references for %llu\n",
10384 (unsigned long long)rec->start);
10386 btrfs_release_path(&path);
10390 static int fixup_extent_flags(struct btrfs_fs_info *fs_info,
10391 struct extent_record *rec)
10393 struct btrfs_trans_handle *trans;
10394 struct btrfs_root *root = fs_info->extent_root;
10395 struct btrfs_path path;
10396 struct btrfs_extent_item *ei;
10397 struct btrfs_key key;
10401 key.objectid = rec->start;
10402 if (rec->metadata) {
10403 key.type = BTRFS_METADATA_ITEM_KEY;
10404 key.offset = rec->info_level;
10406 key.type = BTRFS_EXTENT_ITEM_KEY;
10407 key.offset = rec->max_size;
10410 trans = btrfs_start_transaction(root, 0);
10412 return PTR_ERR(trans);
10414 btrfs_init_path(&path);
10415 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
10417 btrfs_release_path(&path);
10418 btrfs_commit_transaction(trans, root);
10421 fprintf(stderr, "Didn't find extent for %llu\n",
10422 (unsigned long long)rec->start);
10423 btrfs_release_path(&path);
10424 btrfs_commit_transaction(trans, root);
10428 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
10429 struct btrfs_extent_item);
10430 flags = btrfs_extent_flags(path.nodes[0], ei);
10431 if (rec->flag_block_full_backref) {
10432 fprintf(stderr, "setting full backref on %llu\n",
10433 (unsigned long long)key.objectid);
10434 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
10436 fprintf(stderr, "clearing full backref on %llu\n",
10437 (unsigned long long)key.objectid);
10438 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
10440 btrfs_set_extent_flags(path.nodes[0], ei, flags);
10441 btrfs_mark_buffer_dirty(path.nodes[0]);
10442 btrfs_release_path(&path);
10443 ret = btrfs_commit_transaction(trans, root);
10445 fprintf(stderr, "Repaired extent flags for %llu\n",
10446 (unsigned long long)rec->start);
10451 /* right now we only prune from the extent allocation tree */
10452 static int prune_one_block(struct btrfs_trans_handle *trans,
10453 struct btrfs_fs_info *info,
10454 struct btrfs_corrupt_block *corrupt)
10457 struct btrfs_path path;
10458 struct extent_buffer *eb;
10462 int level = corrupt->level + 1;
10464 btrfs_init_path(&path);
10466 /* we want to stop at the parent to our busted block */
10467 path.lowest_level = level;
10469 ret = btrfs_search_slot(trans, info->extent_root,
10470 &corrupt->key, &path, -1, 1);
10475 eb = path.nodes[level];
10482 * hopefully the search gave us the block we want to prune,
10483 * lets try that first
10485 slot = path.slots[level];
10486 found = btrfs_node_blockptr(eb, slot);
10487 if (found == corrupt->cache.start)
10490 nritems = btrfs_header_nritems(eb);
10492 /* the search failed, lets scan this node and hope we find it */
10493 for (slot = 0; slot < nritems; slot++) {
10494 found = btrfs_node_blockptr(eb, slot);
10495 if (found == corrupt->cache.start)
10499 * we couldn't find the bad block. TODO, search all the nodes for pointers
10502 if (eb == info->extent_root->node) {
10507 btrfs_release_path(&path);
10512 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
10513 ret = btrfs_del_ptr(info->extent_root, &path, level, slot);
10516 btrfs_release_path(&path);
10520 static int prune_corrupt_blocks(struct btrfs_fs_info *info)
10522 struct btrfs_trans_handle *trans = NULL;
10523 struct cache_extent *cache;
10524 struct btrfs_corrupt_block *corrupt;
10527 cache = search_cache_extent(info->corrupt_blocks, 0);
10531 trans = btrfs_start_transaction(info->extent_root, 1);
10533 return PTR_ERR(trans);
10535 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
10536 prune_one_block(trans, info, corrupt);
10537 remove_cache_extent(info->corrupt_blocks, cache);
10540 return btrfs_commit_transaction(trans, info->extent_root);
10544 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
10546 struct btrfs_block_group_cache *cache;
10551 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
10552 &start, &end, EXTENT_DIRTY);
10555 clear_extent_dirty(&fs_info->free_space_cache, start, end);
10560 cache = btrfs_lookup_first_block_group(fs_info, start);
10565 start = cache->key.objectid + cache->key.offset;
10569 static int check_extent_refs(struct btrfs_root *root,
10570 struct cache_tree *extent_cache)
10572 struct extent_record *rec;
10573 struct cache_extent *cache;
10580 * if we're doing a repair, we have to make sure
10581 * we don't allocate from the problem extents.
10582 * In the worst case, this will be all the
10583 * extents in the FS
10585 cache = search_cache_extent(extent_cache, 0);
10587 rec = container_of(cache, struct extent_record, cache);
10588 set_extent_dirty(root->fs_info->excluded_extents,
10590 rec->start + rec->max_size - 1);
10591 cache = next_cache_extent(cache);
10594 /* pin down all the corrupted blocks too */
10595 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
10597 set_extent_dirty(root->fs_info->excluded_extents,
10599 cache->start + cache->size - 1);
10600 cache = next_cache_extent(cache);
10602 prune_corrupt_blocks(root->fs_info);
10603 reset_cached_block_groups(root->fs_info);
10606 reset_cached_block_groups(root->fs_info);
10609 * We need to delete any duplicate entries we find first otherwise we
10610 * could mess up the extent tree when we have backrefs that actually
10611 * belong to a different extent item and not the weird duplicate one.
10613 while (repair && !list_empty(&duplicate_extents)) {
10614 rec = to_extent_record(duplicate_extents.next);
10615 list_del_init(&rec->list);
10617 /* Sometimes we can find a backref before we find an actual
10618 * extent, so we need to process it a little bit to see if there
10619 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
10620 * if this is a backref screwup. If we need to delete stuff
10621 * process_duplicates() will return 0, otherwise it will return
10624 if (process_duplicates(extent_cache, rec))
10626 ret = delete_duplicate_records(root, rec);
10630 * delete_duplicate_records will return the number of entries
10631 * deleted, so if it's greater than 0 then we know we actually
10632 * did something and we need to remove.
10645 cache = search_cache_extent(extent_cache, 0);
10648 rec = container_of(cache, struct extent_record, cache);
10649 if (rec->num_duplicates) {
10650 fprintf(stderr, "extent item %llu has multiple extent "
10651 "items\n", (unsigned long long)rec->start);
10655 if (rec->refs != rec->extent_item_refs) {
10656 fprintf(stderr, "ref mismatch on [%llu %llu] ",
10657 (unsigned long long)rec->start,
10658 (unsigned long long)rec->nr);
10659 fprintf(stderr, "extent item %llu, found %llu\n",
10660 (unsigned long long)rec->extent_item_refs,
10661 (unsigned long long)rec->refs);
10662 ret = record_orphan_data_extents(root->fs_info, rec);
10668 if (all_backpointers_checked(rec, 1)) {
10669 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
10670 (unsigned long long)rec->start,
10671 (unsigned long long)rec->nr);
10675 if (!rec->owner_ref_checked) {
10676 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
10677 (unsigned long long)rec->start,
10678 (unsigned long long)rec->nr);
10683 if (repair && fix) {
10684 ret = fixup_extent_refs(root->fs_info, extent_cache, rec);
10690 if (rec->bad_full_backref) {
10691 fprintf(stderr, "bad full backref, on [%llu]\n",
10692 (unsigned long long)rec->start);
10694 ret = fixup_extent_flags(root->fs_info, rec);
10702 * Although it's not a extent ref's problem, we reuse this
10703 * routine for error reporting.
10704 * No repair function yet.
10706 if (rec->crossing_stripes) {
10708 "bad metadata [%llu, %llu) crossing stripe boundary\n",
10709 rec->start, rec->start + rec->max_size);
10713 if (rec->wrong_chunk_type) {
10715 "bad extent [%llu, %llu), type mismatch with chunk\n",
10716 rec->start, rec->start + rec->max_size);
10721 remove_cache_extent(extent_cache, cache);
10722 free_all_extent_backrefs(rec);
10723 if (!init_extent_tree && repair && (!cur_err || fix))
10724 clear_extent_dirty(root->fs_info->excluded_extents,
10726 rec->start + rec->max_size - 1);
10731 if (ret && ret != -EAGAIN) {
10732 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
10735 struct btrfs_trans_handle *trans;
10737 root = root->fs_info->extent_root;
10738 trans = btrfs_start_transaction(root, 1);
10739 if (IS_ERR(trans)) {
10740 ret = PTR_ERR(trans);
10744 ret = btrfs_fix_block_accounting(trans, root);
10747 ret = btrfs_commit_transaction(trans, root);
10759 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
10763 if (type & BTRFS_BLOCK_GROUP_RAID0) {
10764 stripe_size = length;
10765 stripe_size /= num_stripes;
10766 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
10767 stripe_size = length * 2;
10768 stripe_size /= num_stripes;
10769 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
10770 stripe_size = length;
10771 stripe_size /= (num_stripes - 1);
10772 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
10773 stripe_size = length;
10774 stripe_size /= (num_stripes - 2);
10776 stripe_size = length;
10778 return stripe_size;
10782 * Check the chunk with its block group/dev list ref:
10783 * Return 0 if all refs seems valid.
10784 * Return 1 if part of refs seems valid, need later check for rebuild ref
10785 * like missing block group and needs to search extent tree to rebuild them.
10786 * Return -1 if essential refs are missing and unable to rebuild.
10788 static int check_chunk_refs(struct chunk_record *chunk_rec,
10789 struct block_group_tree *block_group_cache,
10790 struct device_extent_tree *dev_extent_cache,
10793 struct cache_extent *block_group_item;
10794 struct block_group_record *block_group_rec;
10795 struct cache_extent *dev_extent_item;
10796 struct device_extent_record *dev_extent_rec;
10800 int metadump_v2 = 0;
10804 block_group_item = lookup_cache_extent(&block_group_cache->tree,
10806 chunk_rec->length);
10807 if (block_group_item) {
10808 block_group_rec = container_of(block_group_item,
10809 struct block_group_record,
10811 if (chunk_rec->length != block_group_rec->offset ||
10812 chunk_rec->offset != block_group_rec->objectid ||
10814 chunk_rec->type_flags != block_group_rec->flags)) {
10817 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
10818 chunk_rec->objectid,
10823 chunk_rec->type_flags,
10824 block_group_rec->objectid,
10825 block_group_rec->type,
10826 block_group_rec->offset,
10827 block_group_rec->offset,
10828 block_group_rec->objectid,
10829 block_group_rec->flags);
10832 list_del_init(&block_group_rec->list);
10833 chunk_rec->bg_rec = block_group_rec;
10838 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
10839 chunk_rec->objectid,
10844 chunk_rec->type_flags);
10851 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
10852 chunk_rec->num_stripes);
10853 for (i = 0; i < chunk_rec->num_stripes; ++i) {
10854 devid = chunk_rec->stripes[i].devid;
10855 offset = chunk_rec->stripes[i].offset;
10856 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
10857 devid, offset, length);
10858 if (dev_extent_item) {
10859 dev_extent_rec = container_of(dev_extent_item,
10860 struct device_extent_record,
10862 if (dev_extent_rec->objectid != devid ||
10863 dev_extent_rec->offset != offset ||
10864 dev_extent_rec->chunk_offset != chunk_rec->offset ||
10865 dev_extent_rec->length != length) {
10868 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
10869 chunk_rec->objectid,
10872 chunk_rec->stripes[i].devid,
10873 chunk_rec->stripes[i].offset,
10874 dev_extent_rec->objectid,
10875 dev_extent_rec->offset,
10876 dev_extent_rec->length);
10879 list_move(&dev_extent_rec->chunk_list,
10880 &chunk_rec->dextents);
10885 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
10886 chunk_rec->objectid,
10889 chunk_rec->stripes[i].devid,
10890 chunk_rec->stripes[i].offset);
10897 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
10898 int check_chunks(struct cache_tree *chunk_cache,
10899 struct block_group_tree *block_group_cache,
10900 struct device_extent_tree *dev_extent_cache,
10901 struct list_head *good, struct list_head *bad,
10902 struct list_head *rebuild, int silent)
10904 struct cache_extent *chunk_item;
10905 struct chunk_record *chunk_rec;
10906 struct block_group_record *bg_rec;
10907 struct device_extent_record *dext_rec;
10911 chunk_item = first_cache_extent(chunk_cache);
10912 while (chunk_item) {
10913 chunk_rec = container_of(chunk_item, struct chunk_record,
10915 err = check_chunk_refs(chunk_rec, block_group_cache,
10916 dev_extent_cache, silent);
10919 if (err == 0 && good)
10920 list_add_tail(&chunk_rec->list, good);
10921 if (err > 0 && rebuild)
10922 list_add_tail(&chunk_rec->list, rebuild);
10923 if (err < 0 && bad)
10924 list_add_tail(&chunk_rec->list, bad);
10925 chunk_item = next_cache_extent(chunk_item);
10928 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
10931 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
10939 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
10943 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
10944 dext_rec->objectid,
10954 static int check_device_used(struct device_record *dev_rec,
10955 struct device_extent_tree *dext_cache)
10957 struct cache_extent *cache;
10958 struct device_extent_record *dev_extent_rec;
10959 u64 total_byte = 0;
10961 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
10963 dev_extent_rec = container_of(cache,
10964 struct device_extent_record,
10966 if (dev_extent_rec->objectid != dev_rec->devid)
10969 list_del_init(&dev_extent_rec->device_list);
10970 total_byte += dev_extent_rec->length;
10971 cache = next_cache_extent(cache);
10974 if (total_byte != dev_rec->byte_used) {
10976 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
10977 total_byte, dev_rec->byte_used, dev_rec->objectid,
10978 dev_rec->type, dev_rec->offset);
10986 * Extra (optional) check for dev_item size to report possbile problem on a new
10989 static void check_dev_size_alignment(u64 devid, u64 total_bytes, u32 sectorsize)
10991 if (!IS_ALIGNED(total_bytes, sectorsize)) {
10993 "unaligned total_bytes detected for devid %llu, have %llu should be aligned to %u",
10994 devid, total_bytes, sectorsize);
10996 "this is OK for older kernel, but may cause kernel warning for newer kernels");
10997 warning("this can be fixed by 'btrfs rescue fix-device-size'");
11002 * Unlike device size alignment check above, some super total_bytes check
11003 * failure can lead to mount failure for newer kernel.
11005 * So this function will return the error for a fatal super total_bytes problem.
11007 static bool is_super_size_valid(struct btrfs_fs_info *fs_info)
11009 struct btrfs_device *dev;
11010 struct list_head *dev_list = &fs_info->fs_devices->devices;
11011 u64 total_bytes = 0;
11012 u64 super_bytes = btrfs_super_total_bytes(fs_info->super_copy);
11014 list_for_each_entry(dev, dev_list, dev_list)
11015 total_bytes += dev->total_bytes;
11017 /* Important check, which can cause unmountable fs */
11018 if (super_bytes < total_bytes) {
11019 error("super total bytes %llu smaller than real device(s) size %llu",
11020 super_bytes, total_bytes);
11021 error("mounting this fs may fail for newer kernels");
11022 error("this can be fixed by 'btrfs rescue fix-device-size'");
11027 * Optional check, just to make everything aligned and match with each
11030 * For a btrfs-image restored fs, we don't need to check it anyway.
11032 if (btrfs_super_flags(fs_info->super_copy) &
11033 (BTRFS_SUPER_FLAG_METADUMP | BTRFS_SUPER_FLAG_METADUMP_V2))
11035 if (!IS_ALIGNED(super_bytes, fs_info->sectorsize) ||
11036 !IS_ALIGNED(total_bytes, fs_info->sectorsize) ||
11037 super_bytes != total_bytes) {
11038 warning("minor unaligned/mismatch device size detected");
11040 "recommended to use 'btrfs rescue fix-device-size' to fix it");
11045 /* check btrfs_dev_item -> btrfs_dev_extent */
11046 static int check_devices(struct rb_root *dev_cache,
11047 struct device_extent_tree *dev_extent_cache)
11049 struct rb_node *dev_node;
11050 struct device_record *dev_rec;
11051 struct device_extent_record *dext_rec;
11055 dev_node = rb_first(dev_cache);
11057 dev_rec = container_of(dev_node, struct device_record, node);
11058 err = check_device_used(dev_rec, dev_extent_cache);
11062 check_dev_size_alignment(dev_rec->devid, dev_rec->total_byte,
11063 global_info->sectorsize);
11064 dev_node = rb_next(dev_node);
11066 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
11069 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
11070 dext_rec->objectid, dext_rec->offset, dext_rec->length);
11077 static int add_root_item_to_list(struct list_head *head,
11078 u64 objectid, u64 bytenr, u64 last_snapshot,
11079 u8 level, u8 drop_level,
11080 struct btrfs_key *drop_key)
11083 struct root_item_record *ri_rec;
11084 ri_rec = malloc(sizeof(*ri_rec));
11087 ri_rec->bytenr = bytenr;
11088 ri_rec->objectid = objectid;
11089 ri_rec->level = level;
11090 ri_rec->drop_level = drop_level;
11091 ri_rec->last_snapshot = last_snapshot;
11093 memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
11094 list_add_tail(&ri_rec->list, head);
11099 static void free_root_item_list(struct list_head *list)
11101 struct root_item_record *ri_rec;
11103 while (!list_empty(list)) {
11104 ri_rec = list_first_entry(list, struct root_item_record,
11106 list_del_init(&ri_rec->list);
11111 static int deal_root_from_list(struct list_head *list,
11112 struct btrfs_root *root,
11113 struct block_info *bits,
11115 struct cache_tree *pending,
11116 struct cache_tree *seen,
11117 struct cache_tree *reada,
11118 struct cache_tree *nodes,
11119 struct cache_tree *extent_cache,
11120 struct cache_tree *chunk_cache,
11121 struct rb_root *dev_cache,
11122 struct block_group_tree *block_group_cache,
11123 struct device_extent_tree *dev_extent_cache)
11128 while (!list_empty(list)) {
11129 struct root_item_record *rec;
11130 struct extent_buffer *buf;
11131 rec = list_entry(list->next,
11132 struct root_item_record, list);
11134 buf = read_tree_block(root->fs_info, rec->bytenr, 0);
11135 if (!extent_buffer_uptodate(buf)) {
11136 free_extent_buffer(buf);
11140 ret = add_root_to_pending(buf, extent_cache, pending,
11141 seen, nodes, rec->objectid);
11145 * To rebuild extent tree, we need deal with snapshot
11146 * one by one, otherwise we deal with node firstly which
11147 * can maximize readahead.
11150 ret = run_next_block(root, bits, bits_nr, &last,
11151 pending, seen, reada, nodes,
11152 extent_cache, chunk_cache,
11153 dev_cache, block_group_cache,
11154 dev_extent_cache, rec);
11158 free_extent_buffer(buf);
11159 list_del(&rec->list);
11165 ret = run_next_block(root, bits, bits_nr, &last, pending, seen,
11166 reada, nodes, extent_cache, chunk_cache,
11167 dev_cache, block_group_cache,
11168 dev_extent_cache, NULL);
11178 static int check_chunks_and_extents(struct btrfs_fs_info *fs_info)
11180 struct rb_root dev_cache;
11181 struct cache_tree chunk_cache;
11182 struct block_group_tree block_group_cache;
11183 struct device_extent_tree dev_extent_cache;
11184 struct cache_tree extent_cache;
11185 struct cache_tree seen;
11186 struct cache_tree pending;
11187 struct cache_tree reada;
11188 struct cache_tree nodes;
11189 struct extent_io_tree excluded_extents;
11190 struct cache_tree corrupt_blocks;
11191 struct btrfs_path path;
11192 struct btrfs_key key;
11193 struct btrfs_key found_key;
11195 struct block_info *bits;
11197 struct extent_buffer *leaf;
11199 struct btrfs_root_item ri;
11200 struct list_head dropping_trees;
11201 struct list_head normal_trees;
11202 struct btrfs_root *root1;
11203 struct btrfs_root *root;
11207 root = fs_info->fs_root;
11208 dev_cache = RB_ROOT;
11209 cache_tree_init(&chunk_cache);
11210 block_group_tree_init(&block_group_cache);
11211 device_extent_tree_init(&dev_extent_cache);
11213 cache_tree_init(&extent_cache);
11214 cache_tree_init(&seen);
11215 cache_tree_init(&pending);
11216 cache_tree_init(&nodes);
11217 cache_tree_init(&reada);
11218 cache_tree_init(&corrupt_blocks);
11219 extent_io_tree_init(&excluded_extents);
11220 INIT_LIST_HEAD(&dropping_trees);
11221 INIT_LIST_HEAD(&normal_trees);
11224 fs_info->excluded_extents = &excluded_extents;
11225 fs_info->fsck_extent_cache = &extent_cache;
11226 fs_info->free_extent_hook = free_extent_hook;
11227 fs_info->corrupt_blocks = &corrupt_blocks;
11231 bits = malloc(bits_nr * sizeof(struct block_info));
11237 if (ctx.progress_enabled) {
11238 ctx.tp = TASK_EXTENTS;
11239 task_start(ctx.info);
11243 root1 = fs_info->tree_root;
11244 level = btrfs_header_level(root1->node);
11245 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
11246 root1->node->start, 0, level, 0, NULL);
11249 root1 = fs_info->chunk_root;
11250 level = btrfs_header_level(root1->node);
11251 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
11252 root1->node->start, 0, level, 0, NULL);
11255 btrfs_init_path(&path);
11258 key.type = BTRFS_ROOT_ITEM_KEY;
11259 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, &path, 0, 0);
11263 leaf = path.nodes[0];
11264 slot = path.slots[0];
11265 if (slot >= btrfs_header_nritems(path.nodes[0])) {
11266 ret = btrfs_next_leaf(root, &path);
11269 leaf = path.nodes[0];
11270 slot = path.slots[0];
11272 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
11273 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
11274 unsigned long offset;
11277 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
11278 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
11279 last_snapshot = btrfs_root_last_snapshot(&ri);
11280 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
11281 level = btrfs_root_level(&ri);
11282 ret = add_root_item_to_list(&normal_trees,
11283 found_key.objectid,
11284 btrfs_root_bytenr(&ri),
11285 last_snapshot, level,
11290 level = btrfs_root_level(&ri);
11291 objectid = found_key.objectid;
11292 btrfs_disk_key_to_cpu(&found_key,
11293 &ri.drop_progress);
11294 ret = add_root_item_to_list(&dropping_trees,
11296 btrfs_root_bytenr(&ri),
11297 last_snapshot, level,
11298 ri.drop_level, &found_key);
11305 btrfs_release_path(&path);
11308 * check_block can return -EAGAIN if it fixes something, please keep
11309 * this in mind when dealing with return values from these functions, if
11310 * we get -EAGAIN we want to fall through and restart the loop.
11312 ret = deal_root_from_list(&normal_trees, root, bits, bits_nr, &pending,
11313 &seen, &reada, &nodes, &extent_cache,
11314 &chunk_cache, &dev_cache, &block_group_cache,
11315 &dev_extent_cache);
11317 if (ret == -EAGAIN)
11321 ret = deal_root_from_list(&dropping_trees, root, bits, bits_nr,
11322 &pending, &seen, &reada, &nodes,
11323 &extent_cache, &chunk_cache, &dev_cache,
11324 &block_group_cache, &dev_extent_cache);
11326 if (ret == -EAGAIN)
11331 ret = check_chunks(&chunk_cache, &block_group_cache,
11332 &dev_extent_cache, NULL, NULL, NULL, 0);
11334 if (ret == -EAGAIN)
11339 ret = check_extent_refs(root, &extent_cache);
11341 if (ret == -EAGAIN)
11346 ret = check_devices(&dev_cache, &dev_extent_cache);
11351 task_stop(ctx.info);
11353 free_corrupt_blocks_tree(fs_info->corrupt_blocks);
11354 extent_io_tree_cleanup(&excluded_extents);
11355 fs_info->fsck_extent_cache = NULL;
11356 fs_info->free_extent_hook = NULL;
11357 fs_info->corrupt_blocks = NULL;
11358 fs_info->excluded_extents = NULL;
11361 free_chunk_cache_tree(&chunk_cache);
11362 free_device_cache_tree(&dev_cache);
11363 free_block_group_tree(&block_group_cache);
11364 free_device_extent_tree(&dev_extent_cache);
11365 free_extent_cache_tree(&seen);
11366 free_extent_cache_tree(&pending);
11367 free_extent_cache_tree(&reada);
11368 free_extent_cache_tree(&nodes);
11369 free_root_item_list(&normal_trees);
11370 free_root_item_list(&dropping_trees);
11373 free_corrupt_blocks_tree(fs_info->corrupt_blocks);
11374 free_extent_cache_tree(&seen);
11375 free_extent_cache_tree(&pending);
11376 free_extent_cache_tree(&reada);
11377 free_extent_cache_tree(&nodes);
11378 free_chunk_cache_tree(&chunk_cache);
11379 free_block_group_tree(&block_group_cache);
11380 free_device_cache_tree(&dev_cache);
11381 free_device_extent_tree(&dev_extent_cache);
11382 free_extent_record_cache(&extent_cache);
11383 free_root_item_list(&normal_trees);
11384 free_root_item_list(&dropping_trees);
11385 extent_io_tree_cleanup(&excluded_extents);
11389 static int check_extent_inline_ref(struct extent_buffer *eb,
11390 struct btrfs_key *key, struct btrfs_extent_inline_ref *iref)
11393 u8 type = btrfs_extent_inline_ref_type(eb, iref);
11396 case BTRFS_TREE_BLOCK_REF_KEY:
11397 case BTRFS_EXTENT_DATA_REF_KEY:
11398 case BTRFS_SHARED_BLOCK_REF_KEY:
11399 case BTRFS_SHARED_DATA_REF_KEY:
11403 error("extent[%llu %u %llu] has unknown ref type: %d",
11404 key->objectid, key->type, key->offset, type);
11405 ret = UNKNOWN_TYPE;
11413 * Check backrefs of a tree block given by @bytenr or @eb.
11415 * @root: the root containing the @bytenr or @eb
11416 * @eb: tree block extent buffer, can be NULL
11417 * @bytenr: bytenr of the tree block to search
11418 * @level: tree level of the tree block
11419 * @owner: owner of the tree block
11421 * Return >0 for any error found and output error message
11422 * Return 0 for no error found
11424 static int check_tree_block_ref(struct btrfs_root *root,
11425 struct extent_buffer *eb, u64 bytenr,
11426 int level, u64 owner, struct node_refs *nrefs)
11428 struct btrfs_key key;
11429 struct btrfs_root *extent_root = root->fs_info->extent_root;
11430 struct btrfs_path path;
11431 struct btrfs_extent_item *ei;
11432 struct btrfs_extent_inline_ref *iref;
11433 struct extent_buffer *leaf;
11438 int root_level = btrfs_header_level(root->node);
11440 u32 nodesize = root->fs_info->nodesize;
11449 btrfs_init_path(&path);
11450 key.objectid = bytenr;
11451 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
11452 key.type = BTRFS_METADATA_ITEM_KEY;
11454 key.type = BTRFS_EXTENT_ITEM_KEY;
11455 key.offset = (u64)-1;
11457 /* Search for the backref in extent tree */
11458 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11460 err |= BACKREF_MISSING;
11463 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
11465 err |= BACKREF_MISSING;
11469 leaf = path.nodes[0];
11470 slot = path.slots[0];
11471 btrfs_item_key_to_cpu(leaf, &key, slot);
11473 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
11475 if (key.type == BTRFS_METADATA_ITEM_KEY) {
11476 skinny_level = (int)key.offset;
11477 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
11479 struct btrfs_tree_block_info *info;
11481 info = (struct btrfs_tree_block_info *)(ei + 1);
11482 skinny_level = btrfs_tree_block_level(leaf, info);
11483 iref = (struct btrfs_extent_inline_ref *)(info + 1);
11492 * Due to the feature of shared tree blocks, if the upper node
11493 * is a fs root or shared node, the extent of checked node may
11494 * not be updated until the next CoW.
11497 strict = should_check_extent_strictly(root, nrefs,
11499 if (!(btrfs_extent_flags(leaf, ei) &
11500 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
11502 "extent[%llu %u] backref type mismatch, missing bit: %llx",
11503 key.objectid, nodesize,
11504 BTRFS_EXTENT_FLAG_TREE_BLOCK);
11505 err = BACKREF_MISMATCH;
11507 header_gen = btrfs_header_generation(eb);
11508 extent_gen = btrfs_extent_generation(leaf, ei);
11509 if (header_gen != extent_gen) {
11511 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
11512 key.objectid, nodesize, header_gen,
11514 err = BACKREF_MISMATCH;
11516 if (level != skinny_level) {
11518 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
11519 key.objectid, nodesize, level, skinny_level);
11520 err = BACKREF_MISMATCH;
11522 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
11524 "extent[%llu %u] is referred by other roots than %llu",
11525 key.objectid, nodesize, root->objectid);
11526 err = BACKREF_MISMATCH;
11531 * Iterate the extent/metadata item to find the exact backref
11533 item_size = btrfs_item_size_nr(leaf, slot);
11534 ptr = (unsigned long)iref;
11535 end = (unsigned long)ei + item_size;
11537 while (ptr < end) {
11538 iref = (struct btrfs_extent_inline_ref *)ptr;
11539 type = btrfs_extent_inline_ref_type(leaf, iref);
11540 offset = btrfs_extent_inline_ref_offset(leaf, iref);
11542 ret = check_extent_inline_ref(leaf, &key, iref);
11547 if (type == BTRFS_TREE_BLOCK_REF_KEY) {
11548 if (offset == root->objectid)
11550 if (!strict && owner == offset)
11552 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
11554 * Backref of tree reloc root points to itself, no need
11555 * to check backref any more.
11557 * This may be an error of loop backref, but extent tree
11558 * checker should have already handled it.
11559 * Here we only need to avoid infinite iteration.
11561 if (offset == bytenr) {
11565 * Check if the backref points to valid
11568 found_ref = !check_tree_block_ref( root, NULL,
11569 offset, level + 1, owner,
11576 ptr += btrfs_extent_inline_ref_size(type);
11580 * Inlined extent item doesn't have what we need, check
11581 * TREE_BLOCK_REF_KEY
11584 btrfs_release_path(&path);
11585 key.objectid = bytenr;
11586 key.type = BTRFS_TREE_BLOCK_REF_KEY;
11587 key.offset = root->objectid;
11589 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11594 * Finally check SHARED BLOCK REF, any found will be good
11595 * Here we're not doing comprehensive extent backref checking,
11596 * only need to ensure there is some extent referring to this
11600 btrfs_release_path(&path);
11601 key.objectid = bytenr;
11602 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
11603 key.offset = (u64)-1;
11605 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11607 err |= BACKREF_MISSING;
11610 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
11612 err |= BACKREF_MISSING;
11618 err |= BACKREF_MISSING;
11620 btrfs_release_path(&path);
11621 if (nrefs && strict &&
11622 level < root_level && nrefs->full_backref[level + 1])
11623 parent = nrefs->bytenr[level + 1];
11624 if (eb && (err & BACKREF_MISSING))
11626 "extent[%llu %u] backref lost (owner: %llu, level: %u) %s %llu",
11627 bytenr, nodesize, owner, level,
11628 parent ? "parent" : "root",
11629 parent ? parent : root->objectid);
11634 * If @err contains BACKREF_MISSING then add extent of the
11635 * file_extent_data_item.
11637 * Returns error bits after reapir.
11639 static int repair_extent_data_item(struct btrfs_trans_handle *trans,
11640 struct btrfs_root *root,
11641 struct btrfs_path *pathp,
11642 struct node_refs *nrefs,
11645 struct btrfs_file_extent_item *fi;
11646 struct btrfs_key fi_key;
11647 struct btrfs_key key;
11648 struct btrfs_extent_item *ei;
11649 struct btrfs_path path;
11650 struct btrfs_root *extent_root = root->fs_info->extent_root;
11651 struct extent_buffer *eb;
11663 eb = pathp->nodes[0];
11664 slot = pathp->slots[0];
11665 btrfs_item_key_to_cpu(eb, &fi_key, slot);
11666 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
11668 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
11669 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
11672 file_offset = fi_key.offset;
11673 generation = btrfs_file_extent_generation(eb, fi);
11674 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
11675 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
11676 extent_offset = btrfs_file_extent_offset(eb, fi);
11677 offset = file_offset - extent_offset;
11679 /* now repair only adds backref */
11680 if ((err & BACKREF_MISSING) == 0)
11683 /* search extent item */
11684 key.objectid = disk_bytenr;
11685 key.type = BTRFS_EXTENT_ITEM_KEY;
11686 key.offset = num_bytes;
11688 btrfs_init_path(&path);
11689 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11695 /* insert an extent item */
11697 key.objectid = disk_bytenr;
11698 key.type = BTRFS_EXTENT_ITEM_KEY;
11699 key.offset = num_bytes;
11700 size = sizeof(*ei);
11702 btrfs_release_path(&path);
11703 ret = btrfs_insert_empty_item(trans, extent_root, &path, &key,
11707 eb = path.nodes[0];
11708 ei = btrfs_item_ptr(eb, path.slots[0], struct btrfs_extent_item);
11710 btrfs_set_extent_refs(eb, ei, 0);
11711 btrfs_set_extent_generation(eb, ei, generation);
11712 btrfs_set_extent_flags(eb, ei, BTRFS_EXTENT_FLAG_DATA);
11714 btrfs_mark_buffer_dirty(eb);
11715 ret = btrfs_update_block_group(extent_root, disk_bytenr,
11717 btrfs_release_path(&path);
11720 if (nrefs->full_backref[0])
11721 parent = btrfs_header_bytenr(eb);
11725 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, parent,
11727 parent ? BTRFS_FIRST_FREE_OBJECTID : fi_key.objectid,
11731 "failed to increase extent data backref[%llu %llu] root %llu",
11732 disk_bytenr, num_bytes, root->objectid);
11735 printf("Add one extent data backref [%llu %llu]\n",
11736 disk_bytenr, num_bytes);
11739 err &= ~BACKREF_MISSING;
11742 error("can't repair root %llu extent data item[%llu %llu]",
11743 root->objectid, disk_bytenr, num_bytes);
11748 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
11750 * Return >0 any error found and output error message
11751 * Return 0 for no error found
11753 static int check_extent_data_item(struct btrfs_root *root,
11754 struct btrfs_path *pathp,
11755 struct node_refs *nrefs, int account_bytes)
11757 struct btrfs_file_extent_item *fi;
11758 struct extent_buffer *eb = pathp->nodes[0];
11759 struct btrfs_path path;
11760 struct btrfs_root *extent_root = root->fs_info->extent_root;
11761 struct btrfs_key fi_key;
11762 struct btrfs_key dbref_key;
11763 struct extent_buffer *leaf;
11764 struct btrfs_extent_item *ei;
11765 struct btrfs_extent_inline_ref *iref;
11766 struct btrfs_extent_data_ref *dref;
11769 u64 disk_num_bytes;
11770 u64 extent_num_bytes;
11777 int found_dbackref = 0;
11778 int slot = pathp->slots[0];
11783 btrfs_item_key_to_cpu(eb, &fi_key, slot);
11784 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
11786 /* Nothing to check for hole and inline data extents */
11787 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
11788 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
11791 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
11792 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
11793 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
11794 offset = btrfs_file_extent_offset(eb, fi);
11796 /* Check unaligned disk_num_bytes and num_bytes */
11797 if (!IS_ALIGNED(disk_num_bytes, root->fs_info->sectorsize)) {
11799 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
11800 fi_key.objectid, fi_key.offset, disk_num_bytes,
11801 root->fs_info->sectorsize);
11802 err |= BYTES_UNALIGNED;
11803 } else if (account_bytes) {
11804 data_bytes_allocated += disk_num_bytes;
11806 if (!IS_ALIGNED(extent_num_bytes, root->fs_info->sectorsize)) {
11808 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
11809 fi_key.objectid, fi_key.offset, extent_num_bytes,
11810 root->fs_info->sectorsize);
11811 err |= BYTES_UNALIGNED;
11812 } else if (account_bytes) {
11813 data_bytes_referenced += extent_num_bytes;
11815 owner = btrfs_header_owner(eb);
11817 /* Check the extent item of the file extent in extent tree */
11818 btrfs_init_path(&path);
11819 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
11820 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
11821 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
11823 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
11827 leaf = path.nodes[0];
11828 slot = path.slots[0];
11829 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
11831 extent_flags = btrfs_extent_flags(leaf, ei);
11833 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
11835 "extent[%llu %llu] backref type mismatch, wanted bit: %llx",
11836 disk_bytenr, disk_num_bytes,
11837 BTRFS_EXTENT_FLAG_DATA);
11838 err |= BACKREF_MISMATCH;
11841 /* Check data backref inside that extent item */
11842 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
11843 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
11844 ptr = (unsigned long)iref;
11845 end = (unsigned long)ei + item_size;
11846 strict = should_check_extent_strictly(root, nrefs, -1);
11848 while (ptr < end) {
11852 bool match = false;
11854 iref = (struct btrfs_extent_inline_ref *)ptr;
11855 type = btrfs_extent_inline_ref_type(leaf, iref);
11856 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
11858 ret = check_extent_inline_ref(leaf, &dbref_key, iref);
11863 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
11864 ref_root = btrfs_extent_data_ref_root(leaf, dref);
11865 ref_objectid = btrfs_extent_data_ref_objectid(leaf, dref);
11866 ref_offset = btrfs_extent_data_ref_offset(leaf, dref);
11868 if (ref_objectid == fi_key.objectid &&
11869 ref_offset == fi_key.offset - offset)
11871 if (ref_root == root->objectid && match)
11872 found_dbackref = 1;
11873 else if (!strict && owner == ref_root && match)
11874 found_dbackref = 1;
11875 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
11876 found_dbackref = !check_tree_block_ref(root, NULL,
11877 btrfs_extent_inline_ref_offset(leaf, iref),
11881 if (found_dbackref)
11883 ptr += btrfs_extent_inline_ref_size(type);
11886 if (!found_dbackref) {
11887 btrfs_release_path(&path);
11889 /* Didn't find inlined data backref, try EXTENT_DATA_REF_KEY */
11890 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
11891 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
11892 dbref_key.offset = hash_extent_data_ref(root->objectid,
11893 fi_key.objectid, fi_key.offset - offset);
11895 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
11896 &dbref_key, &path, 0, 0);
11898 found_dbackref = 1;
11902 btrfs_release_path(&path);
11905 * Neither inlined nor EXTENT_DATA_REF found, try
11906 * SHARED_DATA_REF as last chance.
11908 dbref_key.objectid = disk_bytenr;
11909 dbref_key.type = BTRFS_SHARED_DATA_REF_KEY;
11910 dbref_key.offset = eb->start;
11912 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
11913 &dbref_key, &path, 0, 0);
11915 found_dbackref = 1;
11921 if (!found_dbackref)
11922 err |= BACKREF_MISSING;
11923 btrfs_release_path(&path);
11924 if (err & BACKREF_MISSING) {
11925 error("data extent[%llu %llu] backref lost",
11926 disk_bytenr, disk_num_bytes);
11932 * Get real tree block level for the case like shared block
11933 * Return >= 0 as tree level
11934 * Return <0 for error
11936 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
11938 struct extent_buffer *eb;
11939 struct btrfs_path path;
11940 struct btrfs_key key;
11941 struct btrfs_extent_item *ei;
11948 /* Search extent tree for extent generation and level */
11949 key.objectid = bytenr;
11950 key.type = BTRFS_METADATA_ITEM_KEY;
11951 key.offset = (u64)-1;
11953 btrfs_init_path(&path);
11954 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
11957 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
11965 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
11966 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
11967 struct btrfs_extent_item);
11968 flags = btrfs_extent_flags(path.nodes[0], ei);
11969 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
11974 /* Get transid for later read_tree_block() check */
11975 transid = btrfs_extent_generation(path.nodes[0], ei);
11977 /* Get backref level as one source */
11978 if (key.type == BTRFS_METADATA_ITEM_KEY) {
11979 backref_level = key.offset;
11981 struct btrfs_tree_block_info *info;
11983 info = (struct btrfs_tree_block_info *)(ei + 1);
11984 backref_level = btrfs_tree_block_level(path.nodes[0], info);
11986 btrfs_release_path(&path);
11988 /* Get level from tree block as an alternative source */
11989 eb = read_tree_block(fs_info, bytenr, transid);
11990 if (!extent_buffer_uptodate(eb)) {
11991 free_extent_buffer(eb);
11994 header_level = btrfs_header_level(eb);
11995 free_extent_buffer(eb);
11997 if (header_level != backref_level)
11999 return header_level;
12002 btrfs_release_path(&path);
12007 * Check if a tree block backref is valid (points to a valid tree block)
12008 * if level == -1, level will be resolved
12009 * Return >0 for any error found and print error message
12011 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
12012 u64 bytenr, int level)
12014 struct btrfs_root *root;
12015 struct btrfs_key key;
12016 struct btrfs_path path;
12017 struct extent_buffer *eb;
12018 struct extent_buffer *node;
12019 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
12023 /* Query level for level == -1 special case */
12025 level = query_tree_block_level(fs_info, bytenr);
12027 err |= REFERENCER_MISSING;
12031 key.objectid = root_id;
12032 key.type = BTRFS_ROOT_ITEM_KEY;
12033 key.offset = (u64)-1;
12035 root = btrfs_read_fs_root(fs_info, &key);
12036 if (IS_ERR(root)) {
12037 err |= REFERENCER_MISSING;
12041 /* Read out the tree block to get item/node key */
12042 eb = read_tree_block(fs_info, bytenr, 0);
12043 if (!extent_buffer_uptodate(eb)) {
12044 err |= REFERENCER_MISSING;
12045 free_extent_buffer(eb);
12049 /* Empty tree, no need to check key */
12050 if (!btrfs_header_nritems(eb) && !level) {
12051 free_extent_buffer(eb);
12056 btrfs_node_key_to_cpu(eb, &key, 0);
12058 btrfs_item_key_to_cpu(eb, &key, 0);
12060 free_extent_buffer(eb);
12062 btrfs_init_path(&path);
12063 path.lowest_level = level;
12064 /* Search with the first key, to ensure we can reach it */
12065 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
12067 err |= REFERENCER_MISSING;
12071 node = path.nodes[level];
12072 if (btrfs_header_bytenr(node) != bytenr) {
12074 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
12075 bytenr, nodesize, bytenr,
12076 btrfs_header_bytenr(node));
12077 err |= REFERENCER_MISMATCH;
12079 if (btrfs_header_level(node) != level) {
12081 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
12082 bytenr, nodesize, level,
12083 btrfs_header_level(node));
12084 err |= REFERENCER_MISMATCH;
12088 btrfs_release_path(&path);
12090 if (err & REFERENCER_MISSING) {
12092 error("extent [%llu %d] lost referencer (owner: %llu)",
12093 bytenr, nodesize, root_id);
12096 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
12097 bytenr, nodesize, root_id, level);
12104 * Check if tree block @eb is tree reloc root.
12105 * Return 0 if it's not or any problem happens
12106 * Return 1 if it's a tree reloc root
12108 static int is_tree_reloc_root(struct btrfs_fs_info *fs_info,
12109 struct extent_buffer *eb)
12111 struct btrfs_root *tree_reloc_root;
12112 struct btrfs_key key;
12113 u64 bytenr = btrfs_header_bytenr(eb);
12114 u64 owner = btrfs_header_owner(eb);
12117 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
12118 key.offset = owner;
12119 key.type = BTRFS_ROOT_ITEM_KEY;
12121 tree_reloc_root = btrfs_read_fs_root_no_cache(fs_info, &key);
12122 if (IS_ERR(tree_reloc_root))
12125 if (bytenr == btrfs_header_bytenr(tree_reloc_root->node))
12127 btrfs_free_fs_root(tree_reloc_root);
12132 * Check referencer for shared block backref
12133 * If level == -1, this function will resolve the level.
12135 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
12136 u64 parent, u64 bytenr, int level)
12138 struct extent_buffer *eb;
12140 int found_parent = 0;
12143 eb = read_tree_block(fs_info, parent, 0);
12144 if (!extent_buffer_uptodate(eb))
12148 level = query_tree_block_level(fs_info, bytenr);
12152 /* It's possible it's a tree reloc root */
12153 if (parent == bytenr) {
12154 if (is_tree_reloc_root(fs_info, eb))
12159 if (level + 1 != btrfs_header_level(eb))
12162 nr = btrfs_header_nritems(eb);
12163 for (i = 0; i < nr; i++) {
12164 if (bytenr == btrfs_node_blockptr(eb, i)) {
12170 free_extent_buffer(eb);
12171 if (!found_parent) {
12173 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
12174 bytenr, fs_info->nodesize, parent, level);
12175 return REFERENCER_MISSING;
12181 * Check referencer for normal (inlined) data ref
12182 * If len == 0, it will be resolved by searching in extent tree
12184 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
12185 u64 root_id, u64 objectid, u64 offset,
12186 u64 bytenr, u64 len, u32 count)
12188 struct btrfs_root *root;
12189 struct btrfs_root *extent_root = fs_info->extent_root;
12190 struct btrfs_key key;
12191 struct btrfs_path path;
12192 struct extent_buffer *leaf;
12193 struct btrfs_file_extent_item *fi;
12194 u32 found_count = 0;
12199 key.objectid = bytenr;
12200 key.type = BTRFS_EXTENT_ITEM_KEY;
12201 key.offset = (u64)-1;
12203 btrfs_init_path(&path);
12204 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
12207 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
12210 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
12211 if (key.objectid != bytenr ||
12212 key.type != BTRFS_EXTENT_ITEM_KEY)
12215 btrfs_release_path(&path);
12217 key.objectid = root_id;
12218 key.type = BTRFS_ROOT_ITEM_KEY;
12219 key.offset = (u64)-1;
12220 btrfs_init_path(&path);
12222 root = btrfs_read_fs_root(fs_info, &key);
12226 key.objectid = objectid;
12227 key.type = BTRFS_EXTENT_DATA_KEY;
12229 * It can be nasty as data backref offset is
12230 * file offset - file extent offset, which is smaller or
12231 * equal to original backref offset. The only special case is
12232 * overflow. So we need to special check and do further search.
12234 key.offset = offset & (1ULL << 63) ? 0 : offset;
12236 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
12241 * Search afterwards to get correct one
12242 * NOTE: As we must do a comprehensive check on the data backref to
12243 * make sure the dref count also matches, we must iterate all file
12244 * extents for that inode.
12247 leaf = path.nodes[0];
12248 slot = path.slots[0];
12250 if (slot >= btrfs_header_nritems(leaf) ||
12251 btrfs_header_owner(leaf) != root_id)
12253 btrfs_item_key_to_cpu(leaf, &key, slot);
12254 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
12256 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
12258 * Except normal disk bytenr and disk num bytes, we still
12259 * need to do extra check on dbackref offset as
12260 * dbackref offset = file_offset - file_extent_offset
12262 * Also, we must check the leaf owner.
12263 * In case of shared tree blocks (snapshots) we can inherit
12264 * leaves from source snapshot.
12265 * In that case, reference from source snapshot should not
12268 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
12269 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
12270 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
12271 offset && btrfs_header_owner(leaf) == root_id)
12275 ret = btrfs_next_item(root, &path);
12280 btrfs_release_path(&path);
12281 if (found_count != count) {
12283 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
12284 bytenr, len, root_id, objectid, offset, count, found_count);
12285 return REFERENCER_MISSING;
12291 * Check if the referencer of a shared data backref exists
12293 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
12294 u64 parent, u64 bytenr)
12296 struct extent_buffer *eb;
12297 struct btrfs_key key;
12298 struct btrfs_file_extent_item *fi;
12300 int found_parent = 0;
12303 eb = read_tree_block(fs_info, parent, 0);
12304 if (!extent_buffer_uptodate(eb))
12307 nr = btrfs_header_nritems(eb);
12308 for (i = 0; i < nr; i++) {
12309 btrfs_item_key_to_cpu(eb, &key, i);
12310 if (key.type != BTRFS_EXTENT_DATA_KEY)
12313 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
12314 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
12317 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
12324 free_extent_buffer(eb);
12325 if (!found_parent) {
12326 error("shared extent %llu referencer lost (parent: %llu)",
12328 return REFERENCER_MISSING;
12334 * Only delete backref if REFERENCER_MISSING now
12336 * Returns <0 the extent was deleted
12337 * Returns >0 the backref was deleted but extent still exists, returned value
12338 * means error after repair
12339 * Returns 0 nothing happened
12341 static int repair_extent_item(struct btrfs_trans_handle *trans,
12342 struct btrfs_root *root, struct btrfs_path *path,
12343 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
12344 u64 owner, u64 offset, int err)
12346 struct btrfs_key old_key;
12350 btrfs_item_key_to_cpu(path->nodes[0], &old_key, path->slots[0]);
12352 if (err & (REFERENCER_MISSING | REFERENCER_MISMATCH)) {
12353 /* delete the backref */
12354 ret = btrfs_free_extent(trans, root->fs_info->fs_root, bytenr,
12355 num_bytes, parent, root_objectid, owner, offset);
12358 err &= ~REFERENCER_MISSING;
12359 printf("Delete backref in extent [%llu %llu]\n",
12360 bytenr, num_bytes);
12362 error("fail to delete backref in extent [%llu %llu]",
12363 bytenr, num_bytes);
12367 /* btrfs_free_extent may delete the extent */
12368 btrfs_release_path(path);
12369 ret = btrfs_search_slot(NULL, root, &old_key, path, 0, 0);
12379 * This function will check a given extent item, including its backref and
12380 * itself (like crossing stripe boundary and type)
12382 * Since we don't use extent_record anymore, introduce new error bit
12384 static int check_extent_item(struct btrfs_trans_handle *trans,
12385 struct btrfs_fs_info *fs_info,
12386 struct btrfs_path *path)
12388 struct btrfs_extent_item *ei;
12389 struct btrfs_extent_inline_ref *iref;
12390 struct btrfs_extent_data_ref *dref;
12391 struct extent_buffer *eb = path->nodes[0];
12394 int slot = path->slots[0];
12396 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
12397 u32 item_size = btrfs_item_size_nr(eb, slot);
12407 struct btrfs_key key;
12411 btrfs_item_key_to_cpu(eb, &key, slot);
12412 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
12413 bytes_used += key.offset;
12414 num_bytes = key.offset;
12416 bytes_used += nodesize;
12417 num_bytes = nodesize;
12420 if (item_size < sizeof(*ei)) {
12422 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
12423 * old thing when on disk format is still un-determined.
12424 * No need to care about it anymore
12426 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
12430 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
12431 flags = btrfs_extent_flags(eb, ei);
12433 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
12435 if (metadata && check_crossing_stripes(global_info, key.objectid,
12437 error("bad metadata [%llu, %llu) crossing stripe boundary",
12438 key.objectid, key.objectid + nodesize);
12439 err |= CROSSING_STRIPE_BOUNDARY;
12442 ptr = (unsigned long)(ei + 1);
12444 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
12445 /* Old EXTENT_ITEM metadata */
12446 struct btrfs_tree_block_info *info;
12448 info = (struct btrfs_tree_block_info *)ptr;
12449 level = btrfs_tree_block_level(eb, info);
12450 ptr += sizeof(struct btrfs_tree_block_info);
12452 /* New METADATA_ITEM */
12453 level = key.offset;
12455 end = (unsigned long)ei + item_size;
12458 /* Reached extent item end normally */
12462 /* Beyond extent item end, wrong item size */
12464 err |= ITEM_SIZE_MISMATCH;
12465 error("extent item at bytenr %llu slot %d has wrong size",
12474 /* Now check every backref in this extent item */
12475 iref = (struct btrfs_extent_inline_ref *)ptr;
12476 type = btrfs_extent_inline_ref_type(eb, iref);
12477 offset = btrfs_extent_inline_ref_offset(eb, iref);
12479 case BTRFS_TREE_BLOCK_REF_KEY:
12480 root_objectid = offset;
12482 ret = check_tree_block_backref(fs_info, offset, key.objectid,
12486 case BTRFS_SHARED_BLOCK_REF_KEY:
12488 ret = check_shared_block_backref(fs_info, offset, key.objectid,
12492 case BTRFS_EXTENT_DATA_REF_KEY:
12493 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
12494 root_objectid = btrfs_extent_data_ref_root(eb, dref);
12495 owner = btrfs_extent_data_ref_objectid(eb, dref);
12496 owner_offset = btrfs_extent_data_ref_offset(eb, dref);
12497 ret = check_extent_data_backref(fs_info, root_objectid, owner,
12498 owner_offset, key.objectid, key.offset,
12499 btrfs_extent_data_ref_count(eb, dref));
12502 case BTRFS_SHARED_DATA_REF_KEY:
12504 ret = check_shared_data_backref(fs_info, offset, key.objectid);
12508 error("extent[%llu %d %llu] has unknown ref type: %d",
12509 key.objectid, key.type, key.offset, type);
12510 ret = UNKNOWN_TYPE;
12515 if (err && repair) {
12516 ret = repair_extent_item(trans, fs_info->extent_root, path,
12517 key.objectid, num_bytes, parent, root_objectid,
12518 owner, owner_offset, ret);
12527 ptr += btrfs_extent_inline_ref_size(type);
12535 * Check if a dev extent item is referred correctly by its chunk
12537 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
12538 struct extent_buffer *eb, int slot)
12540 struct btrfs_root *chunk_root = fs_info->chunk_root;
12541 struct btrfs_dev_extent *ptr;
12542 struct btrfs_path path;
12543 struct btrfs_key chunk_key;
12544 struct btrfs_key devext_key;
12545 struct btrfs_chunk *chunk;
12546 struct extent_buffer *l;
12550 int found_chunk = 0;
12553 btrfs_item_key_to_cpu(eb, &devext_key, slot);
12554 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
12555 length = btrfs_dev_extent_length(eb, ptr);
12557 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
12558 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
12559 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
12561 btrfs_init_path(&path);
12562 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
12567 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
12568 ret = btrfs_check_chunk_valid(fs_info, l, chunk, path.slots[0],
12573 if (btrfs_stripe_length(fs_info, l, chunk) != length)
12576 num_stripes = btrfs_chunk_num_stripes(l, chunk);
12577 for (i = 0; i < num_stripes; i++) {
12578 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
12579 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
12581 if (devid == devext_key.objectid &&
12582 offset == devext_key.offset) {
12588 btrfs_release_path(&path);
12589 if (!found_chunk) {
12591 "device extent[%llu, %llu, %llu] did not find the related chunk",
12592 devext_key.objectid, devext_key.offset, length);
12593 return REFERENCER_MISSING;
12599 * Check if the used space is correct with the dev item
12601 static int check_dev_item(struct btrfs_fs_info *fs_info,
12602 struct extent_buffer *eb, int slot)
12604 struct btrfs_root *dev_root = fs_info->dev_root;
12605 struct btrfs_dev_item *dev_item;
12606 struct btrfs_path path;
12607 struct btrfs_key key;
12608 struct btrfs_dev_extent *ptr;
12615 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
12616 dev_id = btrfs_device_id(eb, dev_item);
12617 used = btrfs_device_bytes_used(eb, dev_item);
12618 total_bytes = btrfs_device_total_bytes(eb, dev_item);
12620 key.objectid = dev_id;
12621 key.type = BTRFS_DEV_EXTENT_KEY;
12624 btrfs_init_path(&path);
12625 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
12627 btrfs_item_key_to_cpu(eb, &key, slot);
12628 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
12629 key.objectid, key.type, key.offset);
12630 btrfs_release_path(&path);
12631 return REFERENCER_MISSING;
12634 /* Iterate dev_extents to calculate the used space of a device */
12636 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0]))
12639 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
12640 if (key.objectid > dev_id)
12642 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
12645 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
12646 struct btrfs_dev_extent);
12647 total += btrfs_dev_extent_length(path.nodes[0], ptr);
12649 ret = btrfs_next_item(dev_root, &path);
12653 btrfs_release_path(&path);
12655 if (used != total) {
12656 btrfs_item_key_to_cpu(eb, &key, slot);
12658 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
12659 total, used, BTRFS_ROOT_TREE_OBJECTID,
12660 BTRFS_DEV_EXTENT_KEY, dev_id);
12661 return ACCOUNTING_MISMATCH;
12663 check_dev_size_alignment(dev_id, total_bytes, fs_info->sectorsize);
12669 * Check a block group item with its referener (chunk) and its used space
12670 * with extent/metadata item
12672 static int check_block_group_item(struct btrfs_fs_info *fs_info,
12673 struct extent_buffer *eb, int slot)
12675 struct btrfs_root *extent_root = fs_info->extent_root;
12676 struct btrfs_root *chunk_root = fs_info->chunk_root;
12677 struct btrfs_block_group_item *bi;
12678 struct btrfs_block_group_item bg_item;
12679 struct btrfs_path path;
12680 struct btrfs_key bg_key;
12681 struct btrfs_key chunk_key;
12682 struct btrfs_key extent_key;
12683 struct btrfs_chunk *chunk;
12684 struct extent_buffer *leaf;
12685 struct btrfs_extent_item *ei;
12686 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
12694 btrfs_item_key_to_cpu(eb, &bg_key, slot);
12695 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
12696 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
12697 used = btrfs_block_group_used(&bg_item);
12698 bg_flags = btrfs_block_group_flags(&bg_item);
12700 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
12701 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
12702 chunk_key.offset = bg_key.objectid;
12704 btrfs_init_path(&path);
12705 /* Search for the referencer chunk */
12706 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
12709 "block group[%llu %llu] did not find the related chunk item",
12710 bg_key.objectid, bg_key.offset);
12711 err |= REFERENCER_MISSING;
12713 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
12714 struct btrfs_chunk);
12715 if (btrfs_chunk_length(path.nodes[0], chunk) !=
12718 "block group[%llu %llu] related chunk item length does not match",
12719 bg_key.objectid, bg_key.offset);
12720 err |= REFERENCER_MISMATCH;
12723 btrfs_release_path(&path);
12725 /* Search from the block group bytenr */
12726 extent_key.objectid = bg_key.objectid;
12727 extent_key.type = 0;
12728 extent_key.offset = 0;
12730 btrfs_init_path(&path);
12731 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
12735 /* Iterate extent tree to account used space */
12737 leaf = path.nodes[0];
12739 /* Search slot can point to the last item beyond leaf nritems */
12740 if (path.slots[0] >= btrfs_header_nritems(leaf))
12743 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
12744 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
12747 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
12748 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
12750 if (extent_key.objectid < bg_key.objectid)
12753 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
12756 total += extent_key.offset;
12758 ei = btrfs_item_ptr(leaf, path.slots[0],
12759 struct btrfs_extent_item);
12760 flags = btrfs_extent_flags(leaf, ei);
12761 if (flags & BTRFS_EXTENT_FLAG_DATA) {
12762 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
12764 "bad extent[%llu, %llu) type mismatch with chunk",
12765 extent_key.objectid,
12766 extent_key.objectid + extent_key.offset);
12767 err |= CHUNK_TYPE_MISMATCH;
12769 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
12770 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
12771 BTRFS_BLOCK_GROUP_METADATA))) {
12773 "bad extent[%llu, %llu) type mismatch with chunk",
12774 extent_key.objectid,
12775 extent_key.objectid + nodesize);
12776 err |= CHUNK_TYPE_MISMATCH;
12780 ret = btrfs_next_item(extent_root, &path);
12786 btrfs_release_path(&path);
12788 if (total != used) {
12790 "block group[%llu %llu] used %llu but extent items used %llu",
12791 bg_key.objectid, bg_key.offset, used, total);
12792 err |= BG_ACCOUNTING_ERROR;
12798 * Add block group item to the extent tree if @err contains REFERENCER_MISSING.
12799 * FIXME: We still need to repair error of dev_item.
12801 * Returns error after repair.
12803 static int repair_chunk_item(struct btrfs_trans_handle *trans,
12804 struct btrfs_root *chunk_root,
12805 struct btrfs_path *path, int err)
12807 struct btrfs_chunk *chunk;
12808 struct btrfs_key chunk_key;
12809 struct extent_buffer *eb = path->nodes[0];
12811 int slot = path->slots[0];
12815 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
12816 if (chunk_key.type != BTRFS_CHUNK_ITEM_KEY)
12818 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
12819 type = btrfs_chunk_type(path->nodes[0], chunk);
12820 length = btrfs_chunk_length(eb, chunk);
12822 if (err & REFERENCER_MISSING) {
12823 ret = btrfs_make_block_group(trans, chunk_root->fs_info, 0,
12824 type, chunk_key.offset, length);
12826 error("fail to add block group item[%llu %llu]",
12827 chunk_key.offset, length);
12830 err &= ~REFERENCER_MISSING;
12831 printf("Added block group item[%llu %llu]\n",
12832 chunk_key.offset, length);
12841 * Check a chunk item.
12842 * Including checking all referred dev_extents and block group
12844 static int check_chunk_item(struct btrfs_fs_info *fs_info,
12845 struct extent_buffer *eb, int slot)
12847 struct btrfs_root *extent_root = fs_info->extent_root;
12848 struct btrfs_root *dev_root = fs_info->dev_root;
12849 struct btrfs_path path;
12850 struct btrfs_key chunk_key;
12851 struct btrfs_key bg_key;
12852 struct btrfs_key devext_key;
12853 struct btrfs_chunk *chunk;
12854 struct extent_buffer *leaf;
12855 struct btrfs_block_group_item *bi;
12856 struct btrfs_block_group_item bg_item;
12857 struct btrfs_dev_extent *ptr;
12869 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
12870 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
12871 length = btrfs_chunk_length(eb, chunk);
12872 chunk_end = chunk_key.offset + length;
12873 ret = btrfs_check_chunk_valid(fs_info, eb, chunk, slot,
12876 error("chunk[%llu %llu) is invalid", chunk_key.offset,
12878 err |= BYTES_UNALIGNED | UNKNOWN_TYPE;
12881 type = btrfs_chunk_type(eb, chunk);
12883 bg_key.objectid = chunk_key.offset;
12884 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
12885 bg_key.offset = length;
12887 btrfs_init_path(&path);
12888 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
12891 "chunk[%llu %llu) did not find the related block group item",
12892 chunk_key.offset, chunk_end);
12893 err |= REFERENCER_MISSING;
12895 leaf = path.nodes[0];
12896 bi = btrfs_item_ptr(leaf, path.slots[0],
12897 struct btrfs_block_group_item);
12898 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
12900 if (btrfs_block_group_flags(&bg_item) != type) {
12902 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
12903 chunk_key.offset, chunk_end, type,
12904 btrfs_block_group_flags(&bg_item));
12905 err |= REFERENCER_MISSING;
12909 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
12910 stripe_len = btrfs_stripe_length(fs_info, eb, chunk);
12911 for (i = 0; i < num_stripes; i++) {
12912 btrfs_release_path(&path);
12913 btrfs_init_path(&path);
12914 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
12915 devext_key.type = BTRFS_DEV_EXTENT_KEY;
12916 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
12918 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
12921 goto not_match_dev;
12923 leaf = path.nodes[0];
12924 ptr = btrfs_item_ptr(leaf, path.slots[0],
12925 struct btrfs_dev_extent);
12926 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
12927 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
12928 if (objectid != chunk_key.objectid ||
12929 offset != chunk_key.offset ||
12930 btrfs_dev_extent_length(leaf, ptr) != stripe_len)
12931 goto not_match_dev;
12934 err |= BACKREF_MISSING;
12936 "chunk[%llu %llu) stripe %d did not find the related dev extent",
12937 chunk_key.objectid, chunk_end, i);
12940 btrfs_release_path(&path);
12945 static int delete_extent_tree_item(struct btrfs_trans_handle *trans,
12946 struct btrfs_root *root,
12947 struct btrfs_path *path)
12949 struct btrfs_key key;
12952 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
12953 btrfs_release_path(path);
12954 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
12960 ret = btrfs_del_item(trans, root, path);
12964 if (path->slots[0] == 0)
12965 btrfs_prev_leaf(root, path);
12970 error("failed to delete root %llu item[%llu, %u, %llu]",
12971 root->objectid, key.objectid, key.type, key.offset);
12973 printf("Deleted root %llu item[%llu, %u, %llu]\n",
12974 root->objectid, key.objectid, key.type, key.offset);
12979 * Main entry function to check known items and update related accounting info
12981 static int check_leaf_items(struct btrfs_trans_handle *trans,
12982 struct btrfs_root *root, struct btrfs_path *path,
12983 struct node_refs *nrefs, int account_bytes)
12985 struct btrfs_fs_info *fs_info = root->fs_info;
12986 struct btrfs_key key;
12987 struct extent_buffer *eb;
12990 struct btrfs_extent_data_ref *dref;
12995 eb = path->nodes[0];
12996 slot = path->slots[0];
12997 if (slot >= btrfs_header_nritems(eb)) {
12999 error("empty leaf [%llu %u] root %llu", eb->start,
13000 root->fs_info->nodesize, root->objectid);
13006 btrfs_item_key_to_cpu(eb, &key, slot);
13010 case BTRFS_EXTENT_DATA_KEY:
13011 ret = check_extent_data_item(root, path, nrefs, account_bytes);
13013 ret = repair_extent_data_item(trans, root, path, nrefs,
13017 case BTRFS_BLOCK_GROUP_ITEM_KEY:
13018 ret = check_block_group_item(fs_info, eb, slot);
13020 ret & REFERENCER_MISSING)
13021 ret = delete_extent_tree_item(trans, root, path);
13024 case BTRFS_DEV_ITEM_KEY:
13025 ret = check_dev_item(fs_info, eb, slot);
13028 case BTRFS_CHUNK_ITEM_KEY:
13029 ret = check_chunk_item(fs_info, eb, slot);
13031 ret = repair_chunk_item(trans, root, path, ret);
13034 case BTRFS_DEV_EXTENT_KEY:
13035 ret = check_dev_extent_item(fs_info, eb, slot);
13038 case BTRFS_EXTENT_ITEM_KEY:
13039 case BTRFS_METADATA_ITEM_KEY:
13040 ret = check_extent_item(trans, fs_info, path);
13043 case BTRFS_EXTENT_CSUM_KEY:
13044 total_csum_bytes += btrfs_item_size_nr(eb, slot);
13047 case BTRFS_TREE_BLOCK_REF_KEY:
13048 ret = check_tree_block_backref(fs_info, key.offset,
13051 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
13052 ret = delete_extent_tree_item(trans, root, path);
13055 case BTRFS_EXTENT_DATA_REF_KEY:
13056 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
13057 ret = check_extent_data_backref(fs_info,
13058 btrfs_extent_data_ref_root(eb, dref),
13059 btrfs_extent_data_ref_objectid(eb, dref),
13060 btrfs_extent_data_ref_offset(eb, dref),
13062 btrfs_extent_data_ref_count(eb, dref));
13064 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
13065 ret = delete_extent_tree_item(trans, root, path);
13068 case BTRFS_SHARED_BLOCK_REF_KEY:
13069 ret = check_shared_block_backref(fs_info, key.offset,
13072 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
13073 ret = delete_extent_tree_item(trans, root, path);
13076 case BTRFS_SHARED_DATA_REF_KEY:
13077 ret = check_shared_data_backref(fs_info, key.offset,
13080 ret & (REFERENCER_MISMATCH | REFERENCER_MISSING))
13081 ret = delete_extent_tree_item(trans, root, path);
13095 * Low memory usage version check_chunks_and_extents.
13097 static int check_chunks_and_extents_v2(struct btrfs_fs_info *fs_info)
13099 struct btrfs_trans_handle *trans = NULL;
13100 struct btrfs_path path;
13101 struct btrfs_key old_key;
13102 struct btrfs_key key;
13103 struct btrfs_root *root1;
13104 struct btrfs_root *root;
13105 struct btrfs_root *cur_root;
13109 root = fs_info->fs_root;
13112 trans = btrfs_start_transaction(fs_info->extent_root, 1);
13113 if (IS_ERR(trans)) {
13114 error("failed to start transaction before check");
13115 return PTR_ERR(trans);
13119 root1 = root->fs_info->chunk_root;
13120 ret = check_btrfs_root(trans, root1, 0, 1);
13123 root1 = root->fs_info->tree_root;
13124 ret = check_btrfs_root(trans, root1, 0, 1);
13127 btrfs_init_path(&path);
13128 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
13130 key.type = BTRFS_ROOT_ITEM_KEY;
13132 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
13134 error("cannot find extent tree in tree_root");
13139 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
13140 if (key.type != BTRFS_ROOT_ITEM_KEY)
13143 key.offset = (u64)-1;
13145 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
13146 cur_root = btrfs_read_fs_root_no_cache(root->fs_info,
13149 cur_root = btrfs_read_fs_root(root->fs_info, &key);
13150 if (IS_ERR(cur_root) || !cur_root) {
13151 error("failed to read tree: %lld", key.objectid);
13155 ret = check_btrfs_root(trans, cur_root, 0, 1);
13158 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
13159 btrfs_free_fs_root(cur_root);
13161 btrfs_release_path(&path);
13162 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
13163 &old_key, &path, 0, 0);
13167 ret = btrfs_next_item(root1, &path);
13173 /* if repair, update block accounting */
13175 ret = btrfs_fix_block_accounting(trans, root);
13179 err &= ~BG_ACCOUNTING_ERROR;
13183 btrfs_commit_transaction(trans, root->fs_info->extent_root);
13185 btrfs_release_path(&path);
13190 static int do_check_chunks_and_extents(struct btrfs_fs_info *fs_info)
13194 if (!ctx.progress_enabled)
13195 fprintf(stderr, "checking extents\n");
13196 if (check_mode == CHECK_MODE_LOWMEM)
13197 ret = check_chunks_and_extents_v2(fs_info);
13199 ret = check_chunks_and_extents(fs_info);
13201 /* Also repair device size related problems */
13202 if (repair && !ret) {
13203 ret = btrfs_fix_device_and_super_size(fs_info);
13210 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
13211 struct btrfs_root *root, int overwrite)
13213 struct extent_buffer *c;
13214 struct extent_buffer *old = root->node;
13217 struct btrfs_disk_key disk_key = {0,0,0};
13223 extent_buffer_get(c);
13226 c = btrfs_alloc_free_block(trans, root,
13227 root->fs_info->nodesize,
13228 root->root_key.objectid,
13229 &disk_key, level, 0, 0);
13232 extent_buffer_get(c);
13236 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
13237 btrfs_set_header_level(c, level);
13238 btrfs_set_header_bytenr(c, c->start);
13239 btrfs_set_header_generation(c, trans->transid);
13240 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
13241 btrfs_set_header_owner(c, root->root_key.objectid);
13243 write_extent_buffer(c, root->fs_info->fsid,
13244 btrfs_header_fsid(), BTRFS_FSID_SIZE);
13246 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
13247 btrfs_header_chunk_tree_uuid(c),
13250 btrfs_mark_buffer_dirty(c);
13252 * this case can happen in the following case:
13254 * 1.overwrite previous root.
13256 * 2.reinit reloc data root, this is because we skip pin
13257 * down reloc data tree before which means we can allocate
13258 * same block bytenr here.
13260 if (old->start == c->start) {
13261 btrfs_set_root_generation(&root->root_item,
13263 root->root_item.level = btrfs_header_level(root->node);
13264 ret = btrfs_update_root(trans, root->fs_info->tree_root,
13265 &root->root_key, &root->root_item);
13267 free_extent_buffer(c);
13271 free_extent_buffer(old);
13273 add_root_to_dirty_list(root);
13277 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
13278 struct extent_buffer *eb, int tree_root)
13280 struct extent_buffer *tmp;
13281 struct btrfs_root_item *ri;
13282 struct btrfs_key key;
13284 int level = btrfs_header_level(eb);
13290 * If we have pinned this block before, don't pin it again.
13291 * This can not only avoid forever loop with broken filesystem
13292 * but also give us some speedups.
13294 if (test_range_bit(&fs_info->pinned_extents, eb->start,
13295 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
13298 btrfs_pin_extent(fs_info, eb->start, eb->len);
13300 nritems = btrfs_header_nritems(eb);
13301 for (i = 0; i < nritems; i++) {
13303 btrfs_item_key_to_cpu(eb, &key, i);
13304 if (key.type != BTRFS_ROOT_ITEM_KEY)
13306 /* Skip the extent root and reloc roots */
13307 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
13308 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
13309 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
13311 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
13312 bytenr = btrfs_disk_root_bytenr(eb, ri);
13315 * If at any point we start needing the real root we
13316 * will have to build a stump root for the root we are
13317 * in, but for now this doesn't actually use the root so
13318 * just pass in extent_root.
13320 tmp = read_tree_block(fs_info, bytenr, 0);
13321 if (!extent_buffer_uptodate(tmp)) {
13322 fprintf(stderr, "Error reading root block\n");
13325 ret = pin_down_tree_blocks(fs_info, tmp, 0);
13326 free_extent_buffer(tmp);
13330 bytenr = btrfs_node_blockptr(eb, i);
13332 /* If we aren't the tree root don't read the block */
13333 if (level == 1 && !tree_root) {
13334 btrfs_pin_extent(fs_info, bytenr,
13335 fs_info->nodesize);
13339 tmp = read_tree_block(fs_info, bytenr, 0);
13340 if (!extent_buffer_uptodate(tmp)) {
13341 fprintf(stderr, "Error reading tree block\n");
13344 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
13345 free_extent_buffer(tmp);
13354 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
13358 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
13362 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
13365 static int reset_block_groups(struct btrfs_fs_info *fs_info)
13367 struct btrfs_block_group_cache *cache;
13368 struct btrfs_path path;
13369 struct extent_buffer *leaf;
13370 struct btrfs_chunk *chunk;
13371 struct btrfs_key key;
13375 btrfs_init_path(&path);
13377 key.type = BTRFS_CHUNK_ITEM_KEY;
13379 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, &path, 0, 0);
13381 btrfs_release_path(&path);
13386 * We do this in case the block groups were screwed up and had alloc
13387 * bits that aren't actually set on the chunks. This happens with
13388 * restored images every time and could happen in real life I guess.
13390 fs_info->avail_data_alloc_bits = 0;
13391 fs_info->avail_metadata_alloc_bits = 0;
13392 fs_info->avail_system_alloc_bits = 0;
13394 /* First we need to create the in-memory block groups */
13396 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
13397 ret = btrfs_next_leaf(fs_info->chunk_root, &path);
13399 btrfs_release_path(&path);
13407 leaf = path.nodes[0];
13408 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
13409 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
13414 chunk = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_chunk);
13415 btrfs_add_block_group(fs_info, 0,
13416 btrfs_chunk_type(leaf, chunk), key.offset,
13417 btrfs_chunk_length(leaf, chunk));
13418 set_extent_dirty(&fs_info->free_space_cache, key.offset,
13419 key.offset + btrfs_chunk_length(leaf, chunk));
13424 cache = btrfs_lookup_first_block_group(fs_info, start);
13428 start = cache->key.objectid + cache->key.offset;
13431 btrfs_release_path(&path);
13435 static int reset_balance(struct btrfs_trans_handle *trans,
13436 struct btrfs_fs_info *fs_info)
13438 struct btrfs_root *root = fs_info->tree_root;
13439 struct btrfs_path path;
13440 struct extent_buffer *leaf;
13441 struct btrfs_key key;
13442 int del_slot, del_nr = 0;
13446 btrfs_init_path(&path);
13447 key.objectid = BTRFS_BALANCE_OBJECTID;
13448 key.type = BTRFS_BALANCE_ITEM_KEY;
13450 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
13455 goto reinit_data_reloc;
13460 ret = btrfs_del_item(trans, root, &path);
13463 btrfs_release_path(&path);
13465 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
13466 key.type = BTRFS_ROOT_ITEM_KEY;
13468 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
13472 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
13477 ret = btrfs_del_items(trans, root, &path,
13484 btrfs_release_path(&path);
13487 ret = btrfs_search_slot(trans, root, &key, &path,
13494 leaf = path.nodes[0];
13495 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
13496 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
13498 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
13503 del_slot = path.slots[0];
13512 ret = btrfs_del_items(trans, root, &path, del_slot, del_nr);
13516 btrfs_release_path(&path);
13519 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
13520 key.type = BTRFS_ROOT_ITEM_KEY;
13521 key.offset = (u64)-1;
13522 root = btrfs_read_fs_root(fs_info, &key);
13523 if (IS_ERR(root)) {
13524 fprintf(stderr, "Error reading data reloc tree\n");
13525 ret = PTR_ERR(root);
13528 record_root_in_trans(trans, root);
13529 ret = btrfs_fsck_reinit_root(trans, root, 0);
13532 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
13534 btrfs_release_path(&path);
13538 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
13539 struct btrfs_fs_info *fs_info)
13545 * The only reason we don't do this is because right now we're just
13546 * walking the trees we find and pinning down their bytes, we don't look
13547 * at any of the leaves. In order to do mixed groups we'd have to check
13548 * the leaves of any fs roots and pin down the bytes for any file
13549 * extents we find. Not hard but why do it if we don't have to?
13551 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
13552 fprintf(stderr, "We don't support re-initing the extent tree "
13553 "for mixed block groups yet, please notify a btrfs "
13554 "developer you want to do this so they can add this "
13555 "functionality.\n");
13560 * first we need to walk all of the trees except the extent tree and pin
13561 * down the bytes that are in use so we don't overwrite any existing
13564 ret = pin_metadata_blocks(fs_info);
13566 fprintf(stderr, "error pinning down used bytes\n");
13571 * Need to drop all the block groups since we're going to recreate all
13574 btrfs_free_block_groups(fs_info);
13575 ret = reset_block_groups(fs_info);
13577 fprintf(stderr, "error resetting the block groups\n");
13581 /* Ok we can allocate now, reinit the extent root */
13582 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
13584 fprintf(stderr, "extent root initialization failed\n");
13586 * When the transaction code is updated we should end the
13587 * transaction, but for now progs only knows about commit so
13588 * just return an error.
13594 * Now we have all the in-memory block groups setup so we can make
13595 * allocations properly, and the metadata we care about is safe since we
13596 * pinned all of it above.
13599 struct btrfs_block_group_cache *cache;
13601 cache = btrfs_lookup_first_block_group(fs_info, start);
13604 start = cache->key.objectid + cache->key.offset;
13605 ret = btrfs_insert_item(trans, fs_info->extent_root,
13606 &cache->key, &cache->item,
13607 sizeof(cache->item));
13609 fprintf(stderr, "Error adding block group\n");
13612 btrfs_extent_post_op(trans, fs_info->extent_root);
13615 ret = reset_balance(trans, fs_info);
13617 fprintf(stderr, "error resetting the pending balance\n");
13622 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
13624 struct btrfs_path path;
13625 struct btrfs_trans_handle *trans;
13626 struct btrfs_key key;
13629 printf("Recowing metadata block %llu\n", eb->start);
13630 key.objectid = btrfs_header_owner(eb);
13631 key.type = BTRFS_ROOT_ITEM_KEY;
13632 key.offset = (u64)-1;
13634 root = btrfs_read_fs_root(root->fs_info, &key);
13635 if (IS_ERR(root)) {
13636 fprintf(stderr, "Couldn't find owner root %llu\n",
13638 return PTR_ERR(root);
13641 trans = btrfs_start_transaction(root, 1);
13643 return PTR_ERR(trans);
13645 btrfs_init_path(&path);
13646 path.lowest_level = btrfs_header_level(eb);
13647 if (path.lowest_level)
13648 btrfs_node_key_to_cpu(eb, &key, 0);
13650 btrfs_item_key_to_cpu(eb, &key, 0);
13652 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
13653 btrfs_commit_transaction(trans, root);
13654 btrfs_release_path(&path);
13658 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
13660 struct btrfs_path path;
13661 struct btrfs_trans_handle *trans;
13662 struct btrfs_key key;
13665 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
13666 bad->key.type, bad->key.offset);
13667 key.objectid = bad->root_id;
13668 key.type = BTRFS_ROOT_ITEM_KEY;
13669 key.offset = (u64)-1;
13671 root = btrfs_read_fs_root(root->fs_info, &key);
13672 if (IS_ERR(root)) {
13673 fprintf(stderr, "Couldn't find owner root %llu\n",
13675 return PTR_ERR(root);
13678 trans = btrfs_start_transaction(root, 1);
13680 return PTR_ERR(trans);
13682 btrfs_init_path(&path);
13683 ret = btrfs_search_slot(trans, root, &bad->key, &path, -1, 1);
13689 ret = btrfs_del_item(trans, root, &path);
13691 btrfs_commit_transaction(trans, root);
13692 btrfs_release_path(&path);
13696 static int zero_log_tree(struct btrfs_root *root)
13698 struct btrfs_trans_handle *trans;
13701 trans = btrfs_start_transaction(root, 1);
13702 if (IS_ERR(trans)) {
13703 ret = PTR_ERR(trans);
13706 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
13707 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
13708 ret = btrfs_commit_transaction(trans, root);
13712 static int populate_csum(struct btrfs_trans_handle *trans,
13713 struct btrfs_root *csum_root, char *buf, u64 start,
13716 struct btrfs_fs_info *fs_info = csum_root->fs_info;
13721 while (offset < len) {
13722 sectorsize = fs_info->sectorsize;
13723 ret = read_extent_data(fs_info, buf, start + offset,
13727 ret = btrfs_csum_file_block(trans, csum_root, start + len,
13728 start + offset, buf, sectorsize);
13731 offset += sectorsize;
13736 static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
13737 struct btrfs_root *csum_root,
13738 struct btrfs_root *cur_root)
13740 struct btrfs_path path;
13741 struct btrfs_key key;
13742 struct extent_buffer *node;
13743 struct btrfs_file_extent_item *fi;
13750 buf = malloc(cur_root->fs_info->sectorsize);
13754 btrfs_init_path(&path);
13758 ret = btrfs_search_slot(NULL, cur_root, &key, &path, 0, 0);
13761 /* Iterate all regular file extents and fill its csum */
13763 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
13765 if (key.type != BTRFS_EXTENT_DATA_KEY)
13767 node = path.nodes[0];
13768 slot = path.slots[0];
13769 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
13770 if (btrfs_file_extent_type(node, fi) != BTRFS_FILE_EXTENT_REG)
13772 start = btrfs_file_extent_disk_bytenr(node, fi);
13773 len = btrfs_file_extent_disk_num_bytes(node, fi);
13775 ret = populate_csum(trans, csum_root, buf, start, len);
13776 if (ret == -EEXIST)
13782 * TODO: if next leaf is corrupted, jump to nearest next valid
13785 ret = btrfs_next_item(cur_root, &path);
13795 btrfs_release_path(&path);
13800 static int fill_csum_tree_from_fs(struct btrfs_trans_handle *trans,
13801 struct btrfs_root *csum_root)
13803 struct btrfs_fs_info *fs_info = csum_root->fs_info;
13804 struct btrfs_path path;
13805 struct btrfs_root *tree_root = fs_info->tree_root;
13806 struct btrfs_root *cur_root;
13807 struct extent_buffer *node;
13808 struct btrfs_key key;
13812 btrfs_init_path(&path);
13813 key.objectid = BTRFS_FS_TREE_OBJECTID;
13815 key.type = BTRFS_ROOT_ITEM_KEY;
13816 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
13825 node = path.nodes[0];
13826 slot = path.slots[0];
13827 btrfs_item_key_to_cpu(node, &key, slot);
13828 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
13830 if (key.type != BTRFS_ROOT_ITEM_KEY)
13832 if (!is_fstree(key.objectid))
13834 key.offset = (u64)-1;
13836 cur_root = btrfs_read_fs_root(fs_info, &key);
13837 if (IS_ERR(cur_root) || !cur_root) {
13838 fprintf(stderr, "Fail to read fs/subvol tree: %lld\n",
13842 ret = fill_csum_tree_from_one_fs_root(trans, csum_root,
13847 ret = btrfs_next_item(tree_root, &path);
13857 btrfs_release_path(&path);
13861 static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans,
13862 struct btrfs_root *csum_root)
13864 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
13865 struct btrfs_path path;
13866 struct btrfs_extent_item *ei;
13867 struct extent_buffer *leaf;
13869 struct btrfs_key key;
13872 btrfs_init_path(&path);
13874 key.type = BTRFS_EXTENT_ITEM_KEY;
13876 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
13878 btrfs_release_path(&path);
13882 buf = malloc(csum_root->fs_info->sectorsize);
13884 btrfs_release_path(&path);
13889 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
13890 ret = btrfs_next_leaf(extent_root, &path);
13898 leaf = path.nodes[0];
13900 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
13901 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
13906 ei = btrfs_item_ptr(leaf, path.slots[0],
13907 struct btrfs_extent_item);
13908 if (!(btrfs_extent_flags(leaf, ei) &
13909 BTRFS_EXTENT_FLAG_DATA)) {
13914 ret = populate_csum(trans, csum_root, buf, key.objectid,
13921 btrfs_release_path(&path);
13927 * Recalculate the csum and put it into the csum tree.
13929 * Extent tree init will wipe out all the extent info, so in that case, we
13930 * can't depend on extent tree, but use fs tree. If search_fs_tree is set, we
13931 * will use fs/subvol trees to init the csum tree.
13933 static int fill_csum_tree(struct btrfs_trans_handle *trans,
13934 struct btrfs_root *csum_root,
13935 int search_fs_tree)
13937 if (search_fs_tree)
13938 return fill_csum_tree_from_fs(trans, csum_root);
13940 return fill_csum_tree_from_extent(trans, csum_root);
13943 static void free_roots_info_cache(void)
13945 if (!roots_info_cache)
13948 while (!cache_tree_empty(roots_info_cache)) {
13949 struct cache_extent *entry;
13950 struct root_item_info *rii;
13952 entry = first_cache_extent(roots_info_cache);
13955 remove_cache_extent(roots_info_cache, entry);
13956 rii = container_of(entry, struct root_item_info, cache_extent);
13960 free(roots_info_cache);
13961 roots_info_cache = NULL;
13964 static int build_roots_info_cache(struct btrfs_fs_info *info)
13967 struct btrfs_key key;
13968 struct extent_buffer *leaf;
13969 struct btrfs_path path;
13971 if (!roots_info_cache) {
13972 roots_info_cache = malloc(sizeof(*roots_info_cache));
13973 if (!roots_info_cache)
13975 cache_tree_init(roots_info_cache);
13978 btrfs_init_path(&path);
13980 key.type = BTRFS_EXTENT_ITEM_KEY;
13982 ret = btrfs_search_slot(NULL, info->extent_root, &key, &path, 0, 0);
13985 leaf = path.nodes[0];
13988 struct btrfs_key found_key;
13989 struct btrfs_extent_item *ei;
13990 struct btrfs_extent_inline_ref *iref;
13991 int slot = path.slots[0];
13996 struct cache_extent *entry;
13997 struct root_item_info *rii;
13999 if (slot >= btrfs_header_nritems(leaf)) {
14000 ret = btrfs_next_leaf(info->extent_root, &path);
14007 leaf = path.nodes[0];
14008 slot = path.slots[0];
14011 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
14013 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
14014 found_key.type != BTRFS_METADATA_ITEM_KEY)
14017 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
14018 flags = btrfs_extent_flags(leaf, ei);
14020 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
14021 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
14024 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
14025 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
14026 level = found_key.offset;
14028 struct btrfs_tree_block_info *binfo;
14030 binfo = (struct btrfs_tree_block_info *)(ei + 1);
14031 iref = (struct btrfs_extent_inline_ref *)(binfo + 1);
14032 level = btrfs_tree_block_level(leaf, binfo);
14036 * For a root extent, it must be of the following type and the
14037 * first (and only one) iref in the item.
14039 type = btrfs_extent_inline_ref_type(leaf, iref);
14040 if (type != BTRFS_TREE_BLOCK_REF_KEY)
14043 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
14044 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
14046 rii = malloc(sizeof(struct root_item_info));
14051 rii->cache_extent.start = root_id;
14052 rii->cache_extent.size = 1;
14053 rii->level = (u8)-1;
14054 entry = &rii->cache_extent;
14055 ret = insert_cache_extent(roots_info_cache, entry);
14058 rii = container_of(entry, struct root_item_info,
14062 ASSERT(rii->cache_extent.start == root_id);
14063 ASSERT(rii->cache_extent.size == 1);
14065 if (level > rii->level || rii->level == (u8)-1) {
14066 rii->level = level;
14067 rii->bytenr = found_key.objectid;
14068 rii->gen = btrfs_extent_generation(leaf, ei);
14069 rii->node_count = 1;
14070 } else if (level == rii->level) {
14078 btrfs_release_path(&path);
14083 static int maybe_repair_root_item(struct btrfs_path *path,
14084 const struct btrfs_key *root_key,
14085 const int read_only_mode)
14087 const u64 root_id = root_key->objectid;
14088 struct cache_extent *entry;
14089 struct root_item_info *rii;
14090 struct btrfs_root_item ri;
14091 unsigned long offset;
14093 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
14096 "Error: could not find extent items for root %llu\n",
14097 root_key->objectid);
14101 rii = container_of(entry, struct root_item_info, cache_extent);
14102 ASSERT(rii->cache_extent.start == root_id);
14103 ASSERT(rii->cache_extent.size == 1);
14105 if (rii->node_count != 1) {
14107 "Error: could not find btree root extent for root %llu\n",
14112 offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
14113 read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
14115 if (btrfs_root_bytenr(&ri) != rii->bytenr ||
14116 btrfs_root_level(&ri) != rii->level ||
14117 btrfs_root_generation(&ri) != rii->gen) {
14120 * If we're in repair mode but our caller told us to not update
14121 * the root item, i.e. just check if it needs to be updated, don't
14122 * print this message, since the caller will call us again shortly
14123 * for the same root item without read only mode (the caller will
14124 * open a transaction first).
14126 if (!(read_only_mode && repair))
14128 "%sroot item for root %llu,"
14129 " current bytenr %llu, current gen %llu, current level %u,"
14130 " new bytenr %llu, new gen %llu, new level %u\n",
14131 (read_only_mode ? "" : "fixing "),
14133 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
14134 btrfs_root_level(&ri),
14135 rii->bytenr, rii->gen, rii->level);
14137 if (btrfs_root_generation(&ri) > rii->gen) {
14139 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
14140 root_id, btrfs_root_generation(&ri), rii->gen);
14144 if (!read_only_mode) {
14145 btrfs_set_root_bytenr(&ri, rii->bytenr);
14146 btrfs_set_root_level(&ri, rii->level);
14147 btrfs_set_root_generation(&ri, rii->gen);
14148 write_extent_buffer(path->nodes[0], &ri,
14149 offset, sizeof(ri));
14159 * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
14160 * caused read-only snapshots to be corrupted if they were created at a moment
14161 * when the source subvolume/snapshot had orphan items. The issue was that the
14162 * on-disk root items became incorrect, referring to the pre orphan cleanup root
14163 * node instead of the post orphan cleanup root node.
14164 * So this function, and its callees, just detects and fixes those cases. Even
14165 * though the regression was for read-only snapshots, this function applies to
14166 * any snapshot/subvolume root.
14167 * This must be run before any other repair code - not doing it so, makes other
14168 * repair code delete or modify backrefs in the extent tree for example, which
14169 * will result in an inconsistent fs after repairing the root items.
14171 static int repair_root_items(struct btrfs_fs_info *info)
14173 struct btrfs_path path;
14174 struct btrfs_key key;
14175 struct extent_buffer *leaf;
14176 struct btrfs_trans_handle *trans = NULL;
14179 int need_trans = 0;
14181 btrfs_init_path(&path);
14183 ret = build_roots_info_cache(info);
14187 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
14188 key.type = BTRFS_ROOT_ITEM_KEY;
14193 * Avoid opening and committing transactions if a leaf doesn't have
14194 * any root items that need to be fixed, so that we avoid rotating
14195 * backup roots unnecessarily.
14198 trans = btrfs_start_transaction(info->tree_root, 1);
14199 if (IS_ERR(trans)) {
14200 ret = PTR_ERR(trans);
14205 ret = btrfs_search_slot(trans, info->tree_root, &key, &path,
14209 leaf = path.nodes[0];
14212 struct btrfs_key found_key;
14214 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
14215 int no_more_keys = find_next_key(&path, &key);
14217 btrfs_release_path(&path);
14219 ret = btrfs_commit_transaction(trans,
14231 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
14233 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
14235 if (found_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
14238 ret = maybe_repair_root_item(&path, &found_key, trans ? 0 : 1);
14242 if (!trans && repair) {
14245 btrfs_release_path(&path);
14255 free_roots_info_cache();
14256 btrfs_release_path(&path);
14258 btrfs_commit_transaction(trans, info->tree_root);
14265 static int clear_free_space_cache(struct btrfs_fs_info *fs_info)
14267 struct btrfs_trans_handle *trans;
14268 struct btrfs_block_group_cache *bg_cache;
14272 /* Clear all free space cache inodes and its extent data */
14274 bg_cache = btrfs_lookup_first_block_group(fs_info, current);
14277 ret = btrfs_clear_free_space_cache(fs_info, bg_cache);
14280 current = bg_cache->key.objectid + bg_cache->key.offset;
14283 /* Don't forget to set cache_generation to -1 */
14284 trans = btrfs_start_transaction(fs_info->tree_root, 0);
14285 if (IS_ERR(trans)) {
14286 error("failed to update super block cache generation");
14287 return PTR_ERR(trans);
14289 btrfs_set_super_cache_generation(fs_info->super_copy, (u64)-1);
14290 btrfs_commit_transaction(trans, fs_info->tree_root);
14295 static int do_clear_free_space_cache(struct btrfs_fs_info *fs_info,
14300 if (clear_version == 1) {
14301 if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
14303 "free space cache v2 detected, use --clear-space-cache v2");
14307 printf("Clearing free space cache\n");
14308 ret = clear_free_space_cache(fs_info);
14310 error("failed to clear free space cache");
14313 printf("Free space cache cleared\n");
14315 } else if (clear_version == 2) {
14316 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
14317 printf("no free space cache v2 to clear\n");
14321 printf("Clear free space cache v2\n");
14322 ret = btrfs_clear_free_space_tree(fs_info);
14324 error("failed to clear free space cache v2: %d", ret);
14327 printf("free space cache v2 cleared\n");
14334 const char * const cmd_check_usage[] = {
14335 "btrfs check [options] <device>",
14336 "Check structural integrity of a filesystem (unmounted).",
14337 "Check structural integrity of an unmounted filesystem. Verify internal",
14338 "trees' consistency and item connectivity. In the repair mode try to",
14339 "fix the problems found. ",
14340 "WARNING: the repair mode is considered dangerous",
14342 "-s|--super <superblock> use this superblock copy",
14343 "-b|--backup use the first valid backup root copy",
14344 "--force skip mount checks, repair is not possible",
14345 "--repair try to repair the filesystem",
14346 "--readonly run in read-only mode (default)",
14347 "--init-csum-tree create a new CRC tree",
14348 "--init-extent-tree create a new extent tree",
14349 "--mode <MODE> allows choice of memory/IO trade-offs",
14350 " where MODE is one of:",
14351 " original - read inodes and extents to memory (requires",
14352 " more memory, does less IO)",
14353 " lowmem - try to use less memory but read blocks again",
14355 "--check-data-csum verify checksums of data blocks",
14356 "-Q|--qgroup-report print a report on qgroup consistency",
14357 "-E|--subvol-extents <subvolid>",
14358 " print subvolume extents and sharing state",
14359 "-r|--tree-root <bytenr> use the given bytenr for the tree root",
14360 "--chunk-root <bytenr> use the given bytenr for the chunk tree root",
14361 "-p|--progress indicate progress",
14362 "--clear-space-cache v1|v2 clear space cache for v1 or v2",
14366 int cmd_check(int argc, char **argv)
14368 struct cache_tree root_cache;
14369 struct btrfs_root *root;
14370 struct btrfs_fs_info *info;
14373 u64 tree_root_bytenr = 0;
14374 u64 chunk_root_bytenr = 0;
14375 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
14379 int init_csum_tree = 0;
14381 int clear_space_cache = 0;
14382 int qgroup_report = 0;
14383 int qgroups_repaired = 0;
14384 unsigned ctree_flags = OPEN_CTREE_EXCLUSIVE;
14389 enum { GETOPT_VAL_REPAIR = 257, GETOPT_VAL_INIT_CSUM,
14390 GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
14391 GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE,
14392 GETOPT_VAL_MODE, GETOPT_VAL_CLEAR_SPACE_CACHE,
14393 GETOPT_VAL_FORCE };
14394 static const struct option long_options[] = {
14395 { "super", required_argument, NULL, 's' },
14396 { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
14397 { "readonly", no_argument, NULL, GETOPT_VAL_READONLY },
14398 { "init-csum-tree", no_argument, NULL,
14399 GETOPT_VAL_INIT_CSUM },
14400 { "init-extent-tree", no_argument, NULL,
14401 GETOPT_VAL_INIT_EXTENT },
14402 { "check-data-csum", no_argument, NULL,
14403 GETOPT_VAL_CHECK_CSUM },
14404 { "backup", no_argument, NULL, 'b' },
14405 { "subvol-extents", required_argument, NULL, 'E' },
14406 { "qgroup-report", no_argument, NULL, 'Q' },
14407 { "tree-root", required_argument, NULL, 'r' },
14408 { "chunk-root", required_argument, NULL,
14409 GETOPT_VAL_CHUNK_TREE },
14410 { "progress", no_argument, NULL, 'p' },
14411 { "mode", required_argument, NULL,
14413 { "clear-space-cache", required_argument, NULL,
14414 GETOPT_VAL_CLEAR_SPACE_CACHE},
14415 { "force", no_argument, NULL, GETOPT_VAL_FORCE },
14416 { NULL, 0, NULL, 0}
14419 c = getopt_long(argc, argv, "as:br:pEQ", long_options, NULL);
14423 case 'a': /* ignored */ break;
14425 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
14428 num = arg_strtou64(optarg);
14429 if (num >= BTRFS_SUPER_MIRROR_MAX) {
14431 "super mirror should be less than %d",
14432 BTRFS_SUPER_MIRROR_MAX);
14435 bytenr = btrfs_sb_offset(((int)num));
14436 printf("using SB copy %llu, bytenr %llu\n", num,
14437 (unsigned long long)bytenr);
14443 subvolid = arg_strtou64(optarg);
14446 tree_root_bytenr = arg_strtou64(optarg);
14448 case GETOPT_VAL_CHUNK_TREE:
14449 chunk_root_bytenr = arg_strtou64(optarg);
14452 ctx.progress_enabled = true;
14456 usage(cmd_check_usage);
14457 case GETOPT_VAL_REPAIR:
14458 printf("enabling repair mode\n");
14460 ctree_flags |= OPEN_CTREE_WRITES;
14462 case GETOPT_VAL_READONLY:
14465 case GETOPT_VAL_INIT_CSUM:
14466 printf("Creating a new CRC tree\n");
14467 init_csum_tree = 1;
14469 ctree_flags |= OPEN_CTREE_WRITES;
14471 case GETOPT_VAL_INIT_EXTENT:
14472 init_extent_tree = 1;
14473 ctree_flags |= (OPEN_CTREE_WRITES |
14474 OPEN_CTREE_NO_BLOCK_GROUPS);
14477 case GETOPT_VAL_CHECK_CSUM:
14478 check_data_csum = 1;
14480 case GETOPT_VAL_MODE:
14481 check_mode = parse_check_mode(optarg);
14482 if (check_mode == CHECK_MODE_UNKNOWN) {
14483 error("unknown mode: %s", optarg);
14487 case GETOPT_VAL_CLEAR_SPACE_CACHE:
14488 if (strcmp(optarg, "v1") == 0) {
14489 clear_space_cache = 1;
14490 } else if (strcmp(optarg, "v2") == 0) {
14491 clear_space_cache = 2;
14492 ctree_flags |= OPEN_CTREE_INVALIDATE_FST;
14495 "invalid argument to --clear-space-cache, must be v1 or v2");
14498 ctree_flags |= OPEN_CTREE_WRITES;
14500 case GETOPT_VAL_FORCE:
14506 if (check_argc_exact(argc - optind, 1))
14507 usage(cmd_check_usage);
14509 if (ctx.progress_enabled) {
14510 ctx.tp = TASK_NOTHING;
14511 ctx.info = task_init(print_status_check, print_status_return, &ctx);
14514 /* This check is the only reason for --readonly to exist */
14515 if (readonly && repair) {
14516 error("repair options are not compatible with --readonly");
14521 * experimental and dangerous
14523 if (repair && check_mode == CHECK_MODE_LOWMEM)
14524 warning("low-memory mode repair support is only partial");
14527 cache_tree_init(&root_cache);
14529 ret = check_mounted(argv[optind]);
14532 error("could not check mount status: %s",
14538 "%s is currently mounted, use --force if you really intend to check the filesystem",
14546 error("repair and --force is not yet supported");
14553 "cannot check mount status of %s, the filesystem could be mounted, continuing because of --force",
14557 "filesystem mounted, continuing because of --force");
14559 /* A block device is mounted in exclusive mode by kernel */
14560 ctree_flags &= ~OPEN_CTREE_EXCLUSIVE;
14563 /* only allow partial opening under repair mode */
14565 ctree_flags |= OPEN_CTREE_PARTIAL;
14567 info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
14568 chunk_root_bytenr, ctree_flags);
14570 error("cannot open file system");
14576 global_info = info;
14577 root = info->fs_root;
14578 uuid_unparse(info->super_copy->fsid, uuidbuf);
14580 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
14583 * Check the bare minimum before starting anything else that could rely
14584 * on it, namely the tree roots, any local consistency checks
14586 if (!extent_buffer_uptodate(info->tree_root->node) ||
14587 !extent_buffer_uptodate(info->dev_root->node) ||
14588 !extent_buffer_uptodate(info->chunk_root->node)) {
14589 error("critical roots corrupted, unable to check the filesystem");
14595 if (clear_space_cache) {
14596 ret = do_clear_free_space_cache(info, clear_space_cache);
14602 * repair mode will force us to commit transaction which
14603 * will make us fail to load log tree when mounting.
14605 if (repair && btrfs_super_log_root(info->super_copy)) {
14606 ret = ask_user("repair mode will force to clear out log tree, are you sure?");
14612 ret = zero_log_tree(root);
14615 error("failed to zero log tree: %d", ret);
14620 if (qgroup_report) {
14621 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
14623 ret = qgroup_verify_all(info);
14630 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
14631 subvolid, argv[optind], uuidbuf);
14632 ret = print_extent_state(info, subvolid);
14637 if (init_extent_tree || init_csum_tree) {
14638 struct btrfs_trans_handle *trans;
14640 trans = btrfs_start_transaction(info->extent_root, 0);
14641 if (IS_ERR(trans)) {
14642 error("error starting transaction");
14643 ret = PTR_ERR(trans);
14648 if (init_extent_tree) {
14649 printf("Creating a new extent tree\n");
14650 ret = reinit_extent_tree(trans, info);
14656 if (init_csum_tree) {
14657 printf("Reinitialize checksum tree\n");
14658 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
14660 error("checksum tree initialization failed: %d",
14667 ret = fill_csum_tree(trans, info->csum_root,
14671 error("checksum tree refilling failed: %d", ret);
14676 * Ok now we commit and run the normal fsck, which will add
14677 * extent entries for all of the items it finds.
14679 ret = btrfs_commit_transaction(trans, info->extent_root);
14684 if (!extent_buffer_uptodate(info->extent_root->node)) {
14685 error("critical: extent_root, unable to check the filesystem");
14690 if (!extent_buffer_uptodate(info->csum_root->node)) {
14691 error("critical: csum_root, unable to check the filesystem");
14697 if (!init_extent_tree) {
14698 ret = repair_root_items(info);
14701 error("failed to repair root items: %s", strerror(-ret));
14705 fprintf(stderr, "Fixed %d roots.\n", ret);
14707 } else if (ret > 0) {
14709 "Found %d roots with an outdated root item.\n",
14712 "Please run a filesystem check with the option --repair to fix them.\n");
14719 ret = do_check_chunks_and_extents(info);
14723 "errors found in extent allocation tree or chunk allocation");
14725 /* Only re-check super size after we checked and repaired the fs */
14726 err |= !is_super_size_valid(info);
14728 if (!ctx.progress_enabled) {
14729 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
14730 fprintf(stderr, "checking free space tree\n");
14732 fprintf(stderr, "checking free space cache\n");
14734 ret = check_space_cache(root);
14737 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
14738 error("errors found in free space tree");
14740 error("errors found in free space cache");
14745 * We used to have to have these hole extents in between our real
14746 * extents so if we don't have this flag set we need to make sure there
14747 * are no gaps in the file extents for inodes, otherwise we can just
14748 * ignore it when this happens.
14750 no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
14751 ret = do_check_fs_roots(info, &root_cache);
14754 error("errors found in fs roots");
14758 fprintf(stderr, "checking csums\n");
14759 ret = check_csums(root);
14762 error("errors found in csum tree");
14766 fprintf(stderr, "checking root refs\n");
14767 /* For low memory mode, check_fs_roots_v2 handles root refs */
14768 if (check_mode != CHECK_MODE_LOWMEM) {
14769 ret = check_root_refs(root, &root_cache);
14772 error("errors found in root refs");
14777 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
14778 struct extent_buffer *eb;
14780 eb = list_first_entry(&root->fs_info->recow_ebs,
14781 struct extent_buffer, recow);
14782 list_del_init(&eb->recow);
14783 ret = recow_extent_buffer(root, eb);
14786 error("fails to fix transid errors");
14791 while (!list_empty(&delete_items)) {
14792 struct bad_item *bad;
14794 bad = list_first_entry(&delete_items, struct bad_item, list);
14795 list_del_init(&bad->list);
14797 ret = delete_bad_item(root, bad);
14803 if (info->quota_enabled) {
14804 fprintf(stderr, "checking quota groups\n");
14805 ret = qgroup_verify_all(info);
14808 error("failed to check quota groups");
14812 ret = repair_qgroups(info, &qgroups_repaired);
14815 error("failed to repair quota groups");
14821 if (!list_empty(&root->fs_info->recow_ebs)) {
14822 error("transid errors in file system");
14827 printf("found %llu bytes used, ",
14828 (unsigned long long)bytes_used);
14830 printf("error(s) found\n");
14832 printf("no error found\n");
14833 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
14834 printf("total tree bytes: %llu\n",
14835 (unsigned long long)total_btree_bytes);
14836 printf("total fs tree bytes: %llu\n",
14837 (unsigned long long)total_fs_tree_bytes);
14838 printf("total extent tree bytes: %llu\n",
14839 (unsigned long long)total_extent_tree_bytes);
14840 printf("btree space waste bytes: %llu\n",
14841 (unsigned long long)btree_space_waste);
14842 printf("file data blocks allocated: %llu\n referenced %llu\n",
14843 (unsigned long long)data_bytes_allocated,
14844 (unsigned long long)data_bytes_referenced);
14846 free_qgroup_counts();
14847 free_root_recs_tree(&root_cache);
14851 if (ctx.progress_enabled)
14852 task_deinit(ctx.info);