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"
50 TASK_NOTHING, /* have to be the last element */
55 enum task_position tp;
57 struct task_info *info;
60 static u64 bytes_used = 0;
61 static u64 total_csum_bytes = 0;
62 static u64 total_btree_bytes = 0;
63 static u64 total_fs_tree_bytes = 0;
64 static u64 total_extent_tree_bytes = 0;
65 static u64 btree_space_waste = 0;
66 static u64 data_bytes_allocated = 0;
67 static u64 data_bytes_referenced = 0;
68 static int found_old_backref = 0;
69 static LIST_HEAD(duplicate_extents);
70 static LIST_HEAD(delete_items);
71 static int no_holes = 0;
72 static int init_extent_tree = 0;
73 static int check_data_csum = 0;
74 static struct btrfs_fs_info *global_info;
75 static struct task_ctx ctx = { 0 };
76 static struct cache_tree *roots_info_cache = NULL;
78 enum btrfs_check_mode {
82 CHECK_MODE_DEFAULT = CHECK_MODE_ORIGINAL
85 static enum btrfs_check_mode check_mode = CHECK_MODE_DEFAULT;
87 struct extent_backref {
88 struct list_head list;
89 unsigned int is_data:1;
90 unsigned int found_extent_tree:1;
91 unsigned int full_backref:1;
92 unsigned int found_ref:1;
93 unsigned int broken:1;
96 static inline struct extent_backref* to_extent_backref(struct list_head *entry)
98 return list_entry(entry, struct extent_backref, list);
101 struct data_backref {
102 struct extent_backref node;
116 #define ROOT_DIR_ERROR (1<<1) /* bad ROOT_DIR */
117 #define DIR_ITEM_MISSING (1<<2) /* DIR_ITEM not found */
118 #define DIR_ITEM_MISMATCH (1<<3) /* DIR_ITEM found but not match */
119 #define INODE_REF_MISSING (1<<4) /* INODE_REF/INODE_EXTREF not found */
120 #define INODE_ITEM_MISSING (1<<5) /* INODE_ITEM not found */
121 #define INODE_ITEM_MISMATCH (1<<6) /* INODE_ITEM found but not match */
122 #define FILE_EXTENT_ERROR (1<<7) /* bad FILE_EXTENT */
123 #define ODD_CSUM_ITEM (1<<8) /* CSUM_ITEM error */
124 #define CSUM_ITEM_MISSING (1<<9) /* CSUM_ITEM not found */
125 #define LINK_COUNT_ERROR (1<<10) /* INODE_ITEM nlink count error */
126 #define NBYTES_ERROR (1<<11) /* INODE_ITEM nbytes count error */
127 #define ISIZE_ERROR (1<<12) /* INODE_ITEM size count error */
128 #define ORPHAN_ITEM (1<<13) /* INODE_ITEM no reference */
129 #define NO_INODE_ITEM (1<<14) /* no inode_item */
130 #define LAST_ITEM (1<<15) /* Complete this tree traversal */
131 #define ROOT_REF_MISSING (1<<16) /* ROOT_REF not found */
132 #define ROOT_REF_MISMATCH (1<<17) /* ROOT_REF found but not match */
134 static inline struct data_backref* to_data_backref(struct extent_backref *back)
136 return container_of(back, struct data_backref, node);
140 * Much like data_backref, just removed the undetermined members
141 * and change it to use list_head.
142 * During extent scan, it is stored in root->orphan_data_extent.
143 * During fs tree scan, it is then moved to inode_rec->orphan_data_extents.
145 struct orphan_data_extent {
146 struct list_head list;
154 struct tree_backref {
155 struct extent_backref node;
162 static inline struct tree_backref* to_tree_backref(struct extent_backref *back)
164 return container_of(back, struct tree_backref, node);
167 /* Explicit initialization for extent_record::flag_block_full_backref */
168 enum { FLAG_UNSET = 2 };
170 struct extent_record {
171 struct list_head backrefs;
172 struct list_head dups;
173 struct list_head list;
174 struct cache_extent cache;
175 struct btrfs_disk_key parent_key;
180 u64 extent_item_refs;
182 u64 parent_generation;
186 unsigned int flag_block_full_backref:2;
187 unsigned int found_rec:1;
188 unsigned int content_checked:1;
189 unsigned int owner_ref_checked:1;
190 unsigned int is_root:1;
191 unsigned int metadata:1;
192 unsigned int bad_full_backref:1;
193 unsigned int crossing_stripes:1;
194 unsigned int wrong_chunk_type:1;
197 static inline struct extent_record* to_extent_record(struct list_head *entry)
199 return container_of(entry, struct extent_record, list);
202 struct inode_backref {
203 struct list_head list;
204 unsigned int found_dir_item:1;
205 unsigned int found_dir_index:1;
206 unsigned int found_inode_ref:1;
216 static inline struct inode_backref* to_inode_backref(struct list_head *entry)
218 return list_entry(entry, struct inode_backref, list);
221 struct root_item_record {
222 struct list_head list;
229 struct btrfs_key drop_key;
232 #define REF_ERR_NO_DIR_ITEM (1 << 0)
233 #define REF_ERR_NO_DIR_INDEX (1 << 1)
234 #define REF_ERR_NO_INODE_REF (1 << 2)
235 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
236 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
237 #define REF_ERR_DUP_INODE_REF (1 << 5)
238 #define REF_ERR_INDEX_UNMATCH (1 << 6)
239 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
240 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
241 #define REF_ERR_NO_ROOT_REF (1 << 9)
242 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
243 #define REF_ERR_DUP_ROOT_REF (1 << 11)
244 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
246 struct file_extent_hole {
252 struct inode_record {
253 struct list_head backrefs;
254 unsigned int checked:1;
255 unsigned int merging:1;
256 unsigned int found_inode_item:1;
257 unsigned int found_dir_item:1;
258 unsigned int found_file_extent:1;
259 unsigned int found_csum_item:1;
260 unsigned int some_csum_missing:1;
261 unsigned int nodatasum:1;
274 struct rb_root holes;
275 struct list_head orphan_extents;
280 #define I_ERR_NO_INODE_ITEM (1 << 0)
281 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
282 #define I_ERR_DUP_INODE_ITEM (1 << 2)
283 #define I_ERR_DUP_DIR_INDEX (1 << 3)
284 #define I_ERR_ODD_DIR_ITEM (1 << 4)
285 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
286 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
287 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
288 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
289 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
290 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
291 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
292 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
293 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
294 #define I_ERR_FILE_EXTENT_ORPHAN (1 << 14)
296 struct root_backref {
297 struct list_head list;
298 unsigned int found_dir_item:1;
299 unsigned int found_dir_index:1;
300 unsigned int found_back_ref:1;
301 unsigned int found_forward_ref:1;
302 unsigned int reachable:1;
311 static inline struct root_backref* to_root_backref(struct list_head *entry)
313 return list_entry(entry, struct root_backref, list);
317 struct list_head backrefs;
318 struct cache_extent cache;
319 unsigned int found_root_item:1;
325 struct cache_extent cache;
330 struct cache_extent cache;
331 struct cache_tree root_cache;
332 struct cache_tree inode_cache;
333 struct inode_record *current;
342 struct walk_control {
343 struct cache_tree shared;
344 struct shared_node *nodes[BTRFS_MAX_LEVEL];
350 struct btrfs_key key;
352 struct list_head list;
355 struct extent_entry {
360 struct list_head list;
363 struct root_item_info {
364 /* level of the root */
366 /* number of nodes at this level, must be 1 for a root */
370 struct cache_extent cache_extent;
374 * Error bit for low memory mode check.
376 * Currently no caller cares about it yet. Just internal use for error
379 #define BACKREF_MISSING (1 << 0) /* Backref missing in extent tree */
380 #define BACKREF_MISMATCH (1 << 1) /* Backref exists but does not match */
381 #define BYTES_UNALIGNED (1 << 2) /* Some bytes are not aligned */
382 #define REFERENCER_MISSING (1 << 3) /* Referencer not found */
383 #define REFERENCER_MISMATCH (1 << 4) /* Referenceer found but does not match */
384 #define CROSSING_STRIPE_BOUNDARY (1 << 4) /* For kernel scrub workaround */
385 #define ITEM_SIZE_MISMATCH (1 << 5) /* Bad item size */
386 #define UNKNOWN_TYPE (1 << 6) /* Unknown type */
387 #define ACCOUNTING_MISMATCH (1 << 7) /* Used space accounting error */
388 #define CHUNK_TYPE_MISMATCH (1 << 8)
390 static void *print_status_check(void *p)
392 struct task_ctx *priv = p;
393 const char work_indicator[] = { '.', 'o', 'O', 'o' };
395 static char *task_position_string[] = {
397 "checking free space cache",
401 task_period_start(priv->info, 1000 /* 1s */);
403 if (priv->tp == TASK_NOTHING)
407 printf("%s [%c]\r", task_position_string[priv->tp],
408 work_indicator[count % 4]);
411 task_period_wait(priv->info);
416 static int print_status_return(void *p)
424 static enum btrfs_check_mode parse_check_mode(const char *str)
426 if (strcmp(str, "lowmem") == 0)
427 return CHECK_MODE_LOWMEM;
428 if (strcmp(str, "orig") == 0)
429 return CHECK_MODE_ORIGINAL;
430 if (strcmp(str, "original") == 0)
431 return CHECK_MODE_ORIGINAL;
433 return CHECK_MODE_UNKNOWN;
436 /* Compatible function to allow reuse of old codes */
437 static u64 first_extent_gap(struct rb_root *holes)
439 struct file_extent_hole *hole;
441 if (RB_EMPTY_ROOT(holes))
444 hole = rb_entry(rb_first(holes), struct file_extent_hole, node);
448 static int compare_hole(struct rb_node *node1, struct rb_node *node2)
450 struct file_extent_hole *hole1;
451 struct file_extent_hole *hole2;
453 hole1 = rb_entry(node1, struct file_extent_hole, node);
454 hole2 = rb_entry(node2, struct file_extent_hole, node);
456 if (hole1->start > hole2->start)
458 if (hole1->start < hole2->start)
460 /* Now hole1->start == hole2->start */
461 if (hole1->len >= hole2->len)
463 * Hole 1 will be merge center
464 * Same hole will be merged later
467 /* Hole 2 will be merge center */
472 * Add a hole to the record
474 * This will do hole merge for copy_file_extent_holes(),
475 * which will ensure there won't be continuous holes.
477 static int add_file_extent_hole(struct rb_root *holes,
480 struct file_extent_hole *hole;
481 struct file_extent_hole *prev = NULL;
482 struct file_extent_hole *next = NULL;
484 hole = malloc(sizeof(*hole));
489 /* Since compare will not return 0, no -EEXIST will happen */
490 rb_insert(holes, &hole->node, compare_hole);
492 /* simple merge with previous hole */
493 if (rb_prev(&hole->node))
494 prev = rb_entry(rb_prev(&hole->node), struct file_extent_hole,
496 if (prev && prev->start + prev->len >= hole->start) {
497 hole->len = hole->start + hole->len - prev->start;
498 hole->start = prev->start;
499 rb_erase(&prev->node, holes);
504 /* iterate merge with next holes */
506 if (!rb_next(&hole->node))
508 next = rb_entry(rb_next(&hole->node), struct file_extent_hole,
510 if (hole->start + hole->len >= next->start) {
511 if (hole->start + hole->len <= next->start + next->len)
512 hole->len = next->start + next->len -
514 rb_erase(&next->node, holes);
523 static int compare_hole_range(struct rb_node *node, void *data)
525 struct file_extent_hole *hole;
528 hole = (struct file_extent_hole *)data;
531 hole = rb_entry(node, struct file_extent_hole, node);
532 if (start < hole->start)
534 if (start >= hole->start && start < hole->start + hole->len)
540 * Delete a hole in the record
542 * This will do the hole split and is much restrict than add.
544 static int del_file_extent_hole(struct rb_root *holes,
547 struct file_extent_hole *hole;
548 struct file_extent_hole tmp;
553 struct rb_node *node;
560 node = rb_search(holes, &tmp, compare_hole_range, NULL);
563 hole = rb_entry(node, struct file_extent_hole, node);
564 if (start + len > hole->start + hole->len)
568 * Now there will be no overlap, delete the hole and re-add the
569 * split(s) if they exists.
571 if (start > hole->start) {
572 prev_start = hole->start;
573 prev_len = start - hole->start;
576 if (hole->start + hole->len > start + len) {
577 next_start = start + len;
578 next_len = hole->start + hole->len - start - len;
581 rb_erase(node, holes);
584 ret = add_file_extent_hole(holes, prev_start, prev_len);
589 ret = add_file_extent_hole(holes, next_start, next_len);
596 static int copy_file_extent_holes(struct rb_root *dst,
599 struct file_extent_hole *hole;
600 struct rb_node *node;
603 node = rb_first(src);
605 hole = rb_entry(node, struct file_extent_hole, node);
606 ret = add_file_extent_hole(dst, hole->start, hole->len);
609 node = rb_next(node);
614 static void free_file_extent_holes(struct rb_root *holes)
616 struct rb_node *node;
617 struct file_extent_hole *hole;
619 node = rb_first(holes);
621 hole = rb_entry(node, struct file_extent_hole, node);
622 rb_erase(node, holes);
624 node = rb_first(holes);
628 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
630 static void record_root_in_trans(struct btrfs_trans_handle *trans,
631 struct btrfs_root *root)
633 if (root->last_trans != trans->transid) {
634 root->track_dirty = 1;
635 root->last_trans = trans->transid;
636 root->commit_root = root->node;
637 extent_buffer_get(root->node);
641 static u8 imode_to_type(u32 imode)
644 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
645 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
646 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
647 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
648 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
649 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
650 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
651 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
654 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
658 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
660 struct device_record *rec1;
661 struct device_record *rec2;
663 rec1 = rb_entry(node1, struct device_record, node);
664 rec2 = rb_entry(node2, struct device_record, node);
665 if (rec1->devid > rec2->devid)
667 else if (rec1->devid < rec2->devid)
673 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
675 struct inode_record *rec;
676 struct inode_backref *backref;
677 struct inode_backref *orig;
678 struct inode_backref *tmp;
679 struct orphan_data_extent *src_orphan;
680 struct orphan_data_extent *dst_orphan;
685 rec = malloc(sizeof(*rec));
687 return ERR_PTR(-ENOMEM);
688 memcpy(rec, orig_rec, sizeof(*rec));
690 INIT_LIST_HEAD(&rec->backrefs);
691 INIT_LIST_HEAD(&rec->orphan_extents);
692 rec->holes = RB_ROOT;
694 list_for_each_entry(orig, &orig_rec->backrefs, list) {
695 size = sizeof(*orig) + orig->namelen + 1;
696 backref = malloc(size);
701 memcpy(backref, orig, size);
702 list_add_tail(&backref->list, &rec->backrefs);
704 list_for_each_entry(src_orphan, &orig_rec->orphan_extents, list) {
705 dst_orphan = malloc(sizeof(*dst_orphan));
710 memcpy(dst_orphan, src_orphan, sizeof(*src_orphan));
711 list_add_tail(&dst_orphan->list, &rec->orphan_extents);
713 ret = copy_file_extent_holes(&rec->holes, &orig_rec->holes);
720 rb = rb_first(&rec->holes);
722 struct file_extent_hole *hole;
724 hole = rb_entry(rb, struct file_extent_hole, node);
730 if (!list_empty(&rec->backrefs))
731 list_for_each_entry_safe(orig, tmp, &rec->backrefs, list) {
732 list_del(&orig->list);
736 if (!list_empty(&rec->orphan_extents))
737 list_for_each_entry_safe(orig, tmp, &rec->orphan_extents, list) {
738 list_del(&orig->list);
747 static void print_orphan_data_extents(struct list_head *orphan_extents,
750 struct orphan_data_extent *orphan;
752 if (list_empty(orphan_extents))
754 printf("The following data extent is lost in tree %llu:\n",
756 list_for_each_entry(orphan, orphan_extents, list) {
757 printf("\tinode: %llu, offset:%llu, disk_bytenr: %llu, disk_len: %llu\n",
758 orphan->objectid, orphan->offset, orphan->disk_bytenr,
763 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
765 u64 root_objectid = root->root_key.objectid;
766 int errors = rec->errors;
770 /* reloc root errors, we print its corresponding fs root objectid*/
771 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
772 root_objectid = root->root_key.offset;
773 fprintf(stderr, "reloc");
775 fprintf(stderr, "root %llu inode %llu errors %x",
776 (unsigned long long) root_objectid,
777 (unsigned long long) rec->ino, rec->errors);
779 if (errors & I_ERR_NO_INODE_ITEM)
780 fprintf(stderr, ", no inode item");
781 if (errors & I_ERR_NO_ORPHAN_ITEM)
782 fprintf(stderr, ", no orphan item");
783 if (errors & I_ERR_DUP_INODE_ITEM)
784 fprintf(stderr, ", dup inode item");
785 if (errors & I_ERR_DUP_DIR_INDEX)
786 fprintf(stderr, ", dup dir index");
787 if (errors & I_ERR_ODD_DIR_ITEM)
788 fprintf(stderr, ", odd dir item");
789 if (errors & I_ERR_ODD_FILE_EXTENT)
790 fprintf(stderr, ", odd file extent");
791 if (errors & I_ERR_BAD_FILE_EXTENT)
792 fprintf(stderr, ", bad file extent");
793 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
794 fprintf(stderr, ", file extent overlap");
795 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
796 fprintf(stderr, ", file extent discount");
797 if (errors & I_ERR_DIR_ISIZE_WRONG)
798 fprintf(stderr, ", dir isize wrong");
799 if (errors & I_ERR_FILE_NBYTES_WRONG)
800 fprintf(stderr, ", nbytes wrong");
801 if (errors & I_ERR_ODD_CSUM_ITEM)
802 fprintf(stderr, ", odd csum item");
803 if (errors & I_ERR_SOME_CSUM_MISSING)
804 fprintf(stderr, ", some csum missing");
805 if (errors & I_ERR_LINK_COUNT_WRONG)
806 fprintf(stderr, ", link count wrong");
807 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
808 fprintf(stderr, ", orphan file extent");
809 fprintf(stderr, "\n");
810 /* Print the orphan extents if needed */
811 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
812 print_orphan_data_extents(&rec->orphan_extents, root->objectid);
814 /* Print the holes if needed */
815 if (errors & I_ERR_FILE_EXTENT_DISCOUNT) {
816 struct file_extent_hole *hole;
817 struct rb_node *node;
820 node = rb_first(&rec->holes);
821 fprintf(stderr, "Found file extent holes:\n");
824 hole = rb_entry(node, struct file_extent_hole, node);
825 fprintf(stderr, "\tstart: %llu, len: %llu\n",
826 hole->start, hole->len);
827 node = rb_next(node);
830 fprintf(stderr, "\tstart: 0, len: %llu\n",
831 round_up(rec->isize, root->sectorsize));
835 static void print_ref_error(int errors)
837 if (errors & REF_ERR_NO_DIR_ITEM)
838 fprintf(stderr, ", no dir item");
839 if (errors & REF_ERR_NO_DIR_INDEX)
840 fprintf(stderr, ", no dir index");
841 if (errors & REF_ERR_NO_INODE_REF)
842 fprintf(stderr, ", no inode ref");
843 if (errors & REF_ERR_DUP_DIR_ITEM)
844 fprintf(stderr, ", dup dir item");
845 if (errors & REF_ERR_DUP_DIR_INDEX)
846 fprintf(stderr, ", dup dir index");
847 if (errors & REF_ERR_DUP_INODE_REF)
848 fprintf(stderr, ", dup inode ref");
849 if (errors & REF_ERR_INDEX_UNMATCH)
850 fprintf(stderr, ", index mismatch");
851 if (errors & REF_ERR_FILETYPE_UNMATCH)
852 fprintf(stderr, ", filetype mismatch");
853 if (errors & REF_ERR_NAME_TOO_LONG)
854 fprintf(stderr, ", name too long");
855 if (errors & REF_ERR_NO_ROOT_REF)
856 fprintf(stderr, ", no root ref");
857 if (errors & REF_ERR_NO_ROOT_BACKREF)
858 fprintf(stderr, ", no root backref");
859 if (errors & REF_ERR_DUP_ROOT_REF)
860 fprintf(stderr, ", dup root ref");
861 if (errors & REF_ERR_DUP_ROOT_BACKREF)
862 fprintf(stderr, ", dup root backref");
863 fprintf(stderr, "\n");
866 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
869 struct ptr_node *node;
870 struct cache_extent *cache;
871 struct inode_record *rec = NULL;
874 cache = lookup_cache_extent(inode_cache, ino, 1);
876 node = container_of(cache, struct ptr_node, cache);
878 if (mod && rec->refs > 1) {
879 node->data = clone_inode_rec(rec);
880 if (IS_ERR(node->data))
886 rec = calloc(1, sizeof(*rec));
888 return ERR_PTR(-ENOMEM);
890 rec->extent_start = (u64)-1;
892 INIT_LIST_HEAD(&rec->backrefs);
893 INIT_LIST_HEAD(&rec->orphan_extents);
894 rec->holes = RB_ROOT;
896 node = malloc(sizeof(*node));
899 return ERR_PTR(-ENOMEM);
901 node->cache.start = ino;
902 node->cache.size = 1;
905 if (ino == BTRFS_FREE_INO_OBJECTID)
908 ret = insert_cache_extent(inode_cache, &node->cache);
910 return ERR_PTR(-EEXIST);
915 static void free_orphan_data_extents(struct list_head *orphan_extents)
917 struct orphan_data_extent *orphan;
919 while (!list_empty(orphan_extents)) {
920 orphan = list_entry(orphan_extents->next,
921 struct orphan_data_extent, list);
922 list_del(&orphan->list);
927 static void free_inode_rec(struct inode_record *rec)
929 struct inode_backref *backref;
934 while (!list_empty(&rec->backrefs)) {
935 backref = to_inode_backref(rec->backrefs.next);
936 list_del(&backref->list);
939 free_orphan_data_extents(&rec->orphan_extents);
940 free_file_extent_holes(&rec->holes);
944 static int can_free_inode_rec(struct inode_record *rec)
946 if (!rec->errors && rec->checked && rec->found_inode_item &&
947 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
952 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
953 struct inode_record *rec)
955 struct cache_extent *cache;
956 struct inode_backref *tmp, *backref;
957 struct ptr_node *node;
960 if (!rec->found_inode_item)
963 filetype = imode_to_type(rec->imode);
964 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
965 if (backref->found_dir_item && backref->found_dir_index) {
966 if (backref->filetype != filetype)
967 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
968 if (!backref->errors && backref->found_inode_ref &&
969 rec->nlink == rec->found_link) {
970 list_del(&backref->list);
976 if (!rec->checked || rec->merging)
979 if (S_ISDIR(rec->imode)) {
980 if (rec->found_size != rec->isize)
981 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
982 if (rec->found_file_extent)
983 rec->errors |= I_ERR_ODD_FILE_EXTENT;
984 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
985 if (rec->found_dir_item)
986 rec->errors |= I_ERR_ODD_DIR_ITEM;
987 if (rec->found_size != rec->nbytes)
988 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
989 if (rec->nlink > 0 && !no_holes &&
990 (rec->extent_end < rec->isize ||
991 first_extent_gap(&rec->holes) < rec->isize))
992 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
995 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
996 if (rec->found_csum_item && rec->nodatasum)
997 rec->errors |= I_ERR_ODD_CSUM_ITEM;
998 if (rec->some_csum_missing && !rec->nodatasum)
999 rec->errors |= I_ERR_SOME_CSUM_MISSING;
1002 BUG_ON(rec->refs != 1);
1003 if (can_free_inode_rec(rec)) {
1004 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
1005 node = container_of(cache, struct ptr_node, cache);
1006 BUG_ON(node->data != rec);
1007 remove_cache_extent(inode_cache, &node->cache);
1009 free_inode_rec(rec);
1013 static int check_orphan_item(struct btrfs_root *root, u64 ino)
1015 struct btrfs_path path;
1016 struct btrfs_key key;
1019 key.objectid = BTRFS_ORPHAN_OBJECTID;
1020 key.type = BTRFS_ORPHAN_ITEM_KEY;
1023 btrfs_init_path(&path);
1024 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1025 btrfs_release_path(&path);
1031 static int process_inode_item(struct extent_buffer *eb,
1032 int slot, struct btrfs_key *key,
1033 struct shared_node *active_node)
1035 struct inode_record *rec;
1036 struct btrfs_inode_item *item;
1038 rec = active_node->current;
1039 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1040 if (rec->found_inode_item) {
1041 rec->errors |= I_ERR_DUP_INODE_ITEM;
1044 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
1045 rec->nlink = btrfs_inode_nlink(eb, item);
1046 rec->isize = btrfs_inode_size(eb, item);
1047 rec->nbytes = btrfs_inode_nbytes(eb, item);
1048 rec->imode = btrfs_inode_mode(eb, item);
1049 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
1051 rec->found_inode_item = 1;
1052 if (rec->nlink == 0)
1053 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
1054 maybe_free_inode_rec(&active_node->inode_cache, rec);
1058 static struct inode_backref *get_inode_backref(struct inode_record *rec,
1060 int namelen, u64 dir)
1062 struct inode_backref *backref;
1064 list_for_each_entry(backref, &rec->backrefs, list) {
1065 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
1067 if (backref->dir != dir || backref->namelen != namelen)
1069 if (memcmp(name, backref->name, namelen))
1074 backref = malloc(sizeof(*backref) + namelen + 1);
1077 memset(backref, 0, sizeof(*backref));
1079 backref->namelen = namelen;
1080 memcpy(backref->name, name, namelen);
1081 backref->name[namelen] = '\0';
1082 list_add_tail(&backref->list, &rec->backrefs);
1086 static int add_inode_backref(struct cache_tree *inode_cache,
1087 u64 ino, u64 dir, u64 index,
1088 const char *name, int namelen,
1089 u8 filetype, u8 itemtype, int errors)
1091 struct inode_record *rec;
1092 struct inode_backref *backref;
1094 rec = get_inode_rec(inode_cache, ino, 1);
1095 BUG_ON(IS_ERR(rec));
1096 backref = get_inode_backref(rec, name, namelen, dir);
1099 backref->errors |= errors;
1100 if (itemtype == BTRFS_DIR_INDEX_KEY) {
1101 if (backref->found_dir_index)
1102 backref->errors |= REF_ERR_DUP_DIR_INDEX;
1103 if (backref->found_inode_ref && backref->index != index)
1104 backref->errors |= REF_ERR_INDEX_UNMATCH;
1105 if (backref->found_dir_item && backref->filetype != filetype)
1106 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
1108 backref->index = index;
1109 backref->filetype = filetype;
1110 backref->found_dir_index = 1;
1111 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
1113 if (backref->found_dir_item)
1114 backref->errors |= REF_ERR_DUP_DIR_ITEM;
1115 if (backref->found_dir_index && backref->filetype != filetype)
1116 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
1118 backref->filetype = filetype;
1119 backref->found_dir_item = 1;
1120 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
1121 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
1122 if (backref->found_inode_ref)
1123 backref->errors |= REF_ERR_DUP_INODE_REF;
1124 if (backref->found_dir_index && backref->index != index)
1125 backref->errors |= REF_ERR_INDEX_UNMATCH;
1127 backref->index = index;
1129 backref->ref_type = itemtype;
1130 backref->found_inode_ref = 1;
1135 maybe_free_inode_rec(inode_cache, rec);
1139 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
1140 struct cache_tree *dst_cache)
1142 struct inode_backref *backref;
1147 list_for_each_entry(backref, &src->backrefs, list) {
1148 if (backref->found_dir_index) {
1149 add_inode_backref(dst_cache, dst->ino, backref->dir,
1150 backref->index, backref->name,
1151 backref->namelen, backref->filetype,
1152 BTRFS_DIR_INDEX_KEY, backref->errors);
1154 if (backref->found_dir_item) {
1156 add_inode_backref(dst_cache, dst->ino,
1157 backref->dir, 0, backref->name,
1158 backref->namelen, backref->filetype,
1159 BTRFS_DIR_ITEM_KEY, backref->errors);
1161 if (backref->found_inode_ref) {
1162 add_inode_backref(dst_cache, dst->ino,
1163 backref->dir, backref->index,
1164 backref->name, backref->namelen, 0,
1165 backref->ref_type, backref->errors);
1169 if (src->found_dir_item)
1170 dst->found_dir_item = 1;
1171 if (src->found_file_extent)
1172 dst->found_file_extent = 1;
1173 if (src->found_csum_item)
1174 dst->found_csum_item = 1;
1175 if (src->some_csum_missing)
1176 dst->some_csum_missing = 1;
1177 if (first_extent_gap(&dst->holes) > first_extent_gap(&src->holes)) {
1178 ret = copy_file_extent_holes(&dst->holes, &src->holes);
1183 BUG_ON(src->found_link < dir_count);
1184 dst->found_link += src->found_link - dir_count;
1185 dst->found_size += src->found_size;
1186 if (src->extent_start != (u64)-1) {
1187 if (dst->extent_start == (u64)-1) {
1188 dst->extent_start = src->extent_start;
1189 dst->extent_end = src->extent_end;
1191 if (dst->extent_end > src->extent_start)
1192 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1193 else if (dst->extent_end < src->extent_start) {
1194 ret = add_file_extent_hole(&dst->holes,
1196 src->extent_start - dst->extent_end);
1198 if (dst->extent_end < src->extent_end)
1199 dst->extent_end = src->extent_end;
1203 dst->errors |= src->errors;
1204 if (src->found_inode_item) {
1205 if (!dst->found_inode_item) {
1206 dst->nlink = src->nlink;
1207 dst->isize = src->isize;
1208 dst->nbytes = src->nbytes;
1209 dst->imode = src->imode;
1210 dst->nodatasum = src->nodatasum;
1211 dst->found_inode_item = 1;
1213 dst->errors |= I_ERR_DUP_INODE_ITEM;
1221 static int splice_shared_node(struct shared_node *src_node,
1222 struct shared_node *dst_node)
1224 struct cache_extent *cache;
1225 struct ptr_node *node, *ins;
1226 struct cache_tree *src, *dst;
1227 struct inode_record *rec, *conflict;
1228 u64 current_ino = 0;
1232 if (--src_node->refs == 0)
1234 if (src_node->current)
1235 current_ino = src_node->current->ino;
1237 src = &src_node->root_cache;
1238 dst = &dst_node->root_cache;
1240 cache = search_cache_extent(src, 0);
1242 node = container_of(cache, struct ptr_node, cache);
1244 cache = next_cache_extent(cache);
1247 remove_cache_extent(src, &node->cache);
1250 ins = malloc(sizeof(*ins));
1252 ins->cache.start = node->cache.start;
1253 ins->cache.size = node->cache.size;
1257 ret = insert_cache_extent(dst, &ins->cache);
1258 if (ret == -EEXIST) {
1259 conflict = get_inode_rec(dst, rec->ino, 1);
1260 BUG_ON(IS_ERR(conflict));
1261 merge_inode_recs(rec, conflict, dst);
1263 conflict->checked = 1;
1264 if (dst_node->current == conflict)
1265 dst_node->current = NULL;
1267 maybe_free_inode_rec(dst, conflict);
1268 free_inode_rec(rec);
1275 if (src == &src_node->root_cache) {
1276 src = &src_node->inode_cache;
1277 dst = &dst_node->inode_cache;
1281 if (current_ino > 0 && (!dst_node->current ||
1282 current_ino > dst_node->current->ino)) {
1283 if (dst_node->current) {
1284 dst_node->current->checked = 1;
1285 maybe_free_inode_rec(dst, dst_node->current);
1287 dst_node->current = get_inode_rec(dst, current_ino, 1);
1288 BUG_ON(IS_ERR(dst_node->current));
1293 static void free_inode_ptr(struct cache_extent *cache)
1295 struct ptr_node *node;
1296 struct inode_record *rec;
1298 node = container_of(cache, struct ptr_node, cache);
1300 free_inode_rec(rec);
1304 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
1306 static struct shared_node *find_shared_node(struct cache_tree *shared,
1309 struct cache_extent *cache;
1310 struct shared_node *node;
1312 cache = lookup_cache_extent(shared, bytenr, 1);
1314 node = container_of(cache, struct shared_node, cache);
1320 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
1323 struct shared_node *node;
1325 node = calloc(1, sizeof(*node));
1328 node->cache.start = bytenr;
1329 node->cache.size = 1;
1330 cache_tree_init(&node->root_cache);
1331 cache_tree_init(&node->inode_cache);
1334 ret = insert_cache_extent(shared, &node->cache);
1339 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
1340 struct walk_control *wc, int level)
1342 struct shared_node *node;
1343 struct shared_node *dest;
1346 if (level == wc->active_node)
1349 BUG_ON(wc->active_node <= level);
1350 node = find_shared_node(&wc->shared, bytenr);
1352 ret = add_shared_node(&wc->shared, bytenr, refs);
1354 node = find_shared_node(&wc->shared, bytenr);
1355 wc->nodes[level] = node;
1356 wc->active_node = level;
1360 if (wc->root_level == wc->active_node &&
1361 btrfs_root_refs(&root->root_item) == 0) {
1362 if (--node->refs == 0) {
1363 free_inode_recs_tree(&node->root_cache);
1364 free_inode_recs_tree(&node->inode_cache);
1365 remove_cache_extent(&wc->shared, &node->cache);
1371 dest = wc->nodes[wc->active_node];
1372 splice_shared_node(node, dest);
1373 if (node->refs == 0) {
1374 remove_cache_extent(&wc->shared, &node->cache);
1380 static int leave_shared_node(struct btrfs_root *root,
1381 struct walk_control *wc, int level)
1383 struct shared_node *node;
1384 struct shared_node *dest;
1387 if (level == wc->root_level)
1390 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
1394 BUG_ON(i >= BTRFS_MAX_LEVEL);
1396 node = wc->nodes[wc->active_node];
1397 wc->nodes[wc->active_node] = NULL;
1398 wc->active_node = i;
1400 dest = wc->nodes[wc->active_node];
1401 if (wc->active_node < wc->root_level ||
1402 btrfs_root_refs(&root->root_item) > 0) {
1403 BUG_ON(node->refs <= 1);
1404 splice_shared_node(node, dest);
1406 BUG_ON(node->refs < 2);
1415 * 1 - if the root with id child_root_id is a child of root parent_root_id
1416 * 0 - if the root child_root_id isn't a child of the root parent_root_id but
1417 * has other root(s) as parent(s)
1418 * 2 - if the root child_root_id doesn't have any parent roots
1420 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
1423 struct btrfs_path path;
1424 struct btrfs_key key;
1425 struct extent_buffer *leaf;
1429 btrfs_init_path(&path);
1431 key.objectid = parent_root_id;
1432 key.type = BTRFS_ROOT_REF_KEY;
1433 key.offset = child_root_id;
1434 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1438 btrfs_release_path(&path);
1442 key.objectid = child_root_id;
1443 key.type = BTRFS_ROOT_BACKREF_KEY;
1445 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1451 leaf = path.nodes[0];
1452 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1453 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
1456 leaf = path.nodes[0];
1459 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1460 if (key.objectid != child_root_id ||
1461 key.type != BTRFS_ROOT_BACKREF_KEY)
1466 if (key.offset == parent_root_id) {
1467 btrfs_release_path(&path);
1474 btrfs_release_path(&path);
1477 return has_parent ? 0 : 2;
1480 static int process_dir_item(struct btrfs_root *root,
1481 struct extent_buffer *eb,
1482 int slot, struct btrfs_key *key,
1483 struct shared_node *active_node)
1493 struct btrfs_dir_item *di;
1494 struct inode_record *rec;
1495 struct cache_tree *root_cache;
1496 struct cache_tree *inode_cache;
1497 struct btrfs_key location;
1498 char namebuf[BTRFS_NAME_LEN];
1500 root_cache = &active_node->root_cache;
1501 inode_cache = &active_node->inode_cache;
1502 rec = active_node->current;
1503 rec->found_dir_item = 1;
1505 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
1506 total = btrfs_item_size_nr(eb, slot);
1507 while (cur < total) {
1509 btrfs_dir_item_key_to_cpu(eb, di, &location);
1510 name_len = btrfs_dir_name_len(eb, di);
1511 data_len = btrfs_dir_data_len(eb, di);
1512 filetype = btrfs_dir_type(eb, di);
1514 rec->found_size += name_len;
1515 if (name_len <= BTRFS_NAME_LEN) {
1519 len = BTRFS_NAME_LEN;
1520 error = REF_ERR_NAME_TOO_LONG;
1522 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
1524 if (location.type == BTRFS_INODE_ITEM_KEY) {
1525 add_inode_backref(inode_cache, location.objectid,
1526 key->objectid, key->offset, namebuf,
1527 len, filetype, key->type, error);
1528 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
1529 add_inode_backref(root_cache, location.objectid,
1530 key->objectid, key->offset,
1531 namebuf, len, filetype,
1534 fprintf(stderr, "invalid location in dir item %u\n",
1536 add_inode_backref(inode_cache, BTRFS_MULTIPLE_OBJECTIDS,
1537 key->objectid, key->offset, namebuf,
1538 len, filetype, key->type, error);
1541 len = sizeof(*di) + name_len + data_len;
1542 di = (struct btrfs_dir_item *)((char *)di + len);
1545 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1546 rec->errors |= I_ERR_DUP_DIR_INDEX;
1551 static int process_inode_ref(struct extent_buffer *eb,
1552 int slot, struct btrfs_key *key,
1553 struct shared_node *active_node)
1561 struct cache_tree *inode_cache;
1562 struct btrfs_inode_ref *ref;
1563 char namebuf[BTRFS_NAME_LEN];
1565 inode_cache = &active_node->inode_cache;
1567 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1568 total = btrfs_item_size_nr(eb, slot);
1569 while (cur < total) {
1570 name_len = btrfs_inode_ref_name_len(eb, ref);
1571 index = btrfs_inode_ref_index(eb, ref);
1572 if (name_len <= BTRFS_NAME_LEN) {
1576 len = BTRFS_NAME_LEN;
1577 error = REF_ERR_NAME_TOO_LONG;
1579 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1580 add_inode_backref(inode_cache, key->objectid, key->offset,
1581 index, namebuf, len, 0, key->type, error);
1583 len = sizeof(*ref) + name_len;
1584 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1590 static int process_inode_extref(struct extent_buffer *eb,
1591 int slot, struct btrfs_key *key,
1592 struct shared_node *active_node)
1601 struct cache_tree *inode_cache;
1602 struct btrfs_inode_extref *extref;
1603 char namebuf[BTRFS_NAME_LEN];
1605 inode_cache = &active_node->inode_cache;
1607 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1608 total = btrfs_item_size_nr(eb, slot);
1609 while (cur < total) {
1610 name_len = btrfs_inode_extref_name_len(eb, extref);
1611 index = btrfs_inode_extref_index(eb, extref);
1612 parent = btrfs_inode_extref_parent(eb, extref);
1613 if (name_len <= BTRFS_NAME_LEN) {
1617 len = BTRFS_NAME_LEN;
1618 error = REF_ERR_NAME_TOO_LONG;
1620 read_extent_buffer(eb, namebuf,
1621 (unsigned long)(extref + 1), len);
1622 add_inode_backref(inode_cache, key->objectid, parent,
1623 index, namebuf, len, 0, key->type, error);
1625 len = sizeof(*extref) + name_len;
1626 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1633 static int count_csum_range(struct btrfs_root *root, u64 start,
1634 u64 len, u64 *found)
1636 struct btrfs_key key;
1637 struct btrfs_path path;
1638 struct extent_buffer *leaf;
1643 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1645 btrfs_init_path(&path);
1647 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1649 key.type = BTRFS_EXTENT_CSUM_KEY;
1651 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1655 if (ret > 0 && path.slots[0] > 0) {
1656 leaf = path.nodes[0];
1657 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1658 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1659 key.type == BTRFS_EXTENT_CSUM_KEY)
1664 leaf = path.nodes[0];
1665 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1666 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1671 leaf = path.nodes[0];
1674 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1675 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1676 key.type != BTRFS_EXTENT_CSUM_KEY)
1679 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1680 if (key.offset >= start + len)
1683 if (key.offset > start)
1686 size = btrfs_item_size_nr(leaf, path.slots[0]);
1687 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1688 if (csum_end > start) {
1689 size = min(csum_end - start, len);
1698 btrfs_release_path(&path);
1704 static int process_file_extent(struct btrfs_root *root,
1705 struct extent_buffer *eb,
1706 int slot, struct btrfs_key *key,
1707 struct shared_node *active_node)
1709 struct inode_record *rec;
1710 struct btrfs_file_extent_item *fi;
1712 u64 disk_bytenr = 0;
1713 u64 extent_offset = 0;
1714 u64 mask = root->sectorsize - 1;
1718 rec = active_node->current;
1719 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1720 rec->found_file_extent = 1;
1722 if (rec->extent_start == (u64)-1) {
1723 rec->extent_start = key->offset;
1724 rec->extent_end = key->offset;
1727 if (rec->extent_end > key->offset)
1728 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1729 else if (rec->extent_end < key->offset) {
1730 ret = add_file_extent_hole(&rec->holes, rec->extent_end,
1731 key->offset - rec->extent_end);
1736 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1737 extent_type = btrfs_file_extent_type(eb, fi);
1739 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1740 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1742 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1743 rec->found_size += num_bytes;
1744 num_bytes = (num_bytes + mask) & ~mask;
1745 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1746 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1747 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1748 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1749 extent_offset = btrfs_file_extent_offset(eb, fi);
1750 if (num_bytes == 0 || (num_bytes & mask))
1751 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1752 if (num_bytes + extent_offset >
1753 btrfs_file_extent_ram_bytes(eb, fi))
1754 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1755 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1756 (btrfs_file_extent_compression(eb, fi) ||
1757 btrfs_file_extent_encryption(eb, fi) ||
1758 btrfs_file_extent_other_encoding(eb, fi)))
1759 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1760 if (disk_bytenr > 0)
1761 rec->found_size += num_bytes;
1763 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1765 rec->extent_end = key->offset + num_bytes;
1768 * The data reloc tree will copy full extents into its inode and then
1769 * copy the corresponding csums. Because the extent it copied could be
1770 * a preallocated extent that hasn't been written to yet there may be no
1771 * csums to copy, ergo we won't have csums for our file extent. This is
1772 * ok so just don't bother checking csums if the inode belongs to the
1775 if (disk_bytenr > 0 &&
1776 btrfs_header_owner(eb) != BTRFS_DATA_RELOC_TREE_OBJECTID) {
1778 if (btrfs_file_extent_compression(eb, fi))
1779 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1781 disk_bytenr += extent_offset;
1783 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1786 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1788 rec->found_csum_item = 1;
1789 if (found < num_bytes)
1790 rec->some_csum_missing = 1;
1791 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1793 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1799 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1800 struct walk_control *wc)
1802 struct btrfs_key key;
1806 struct cache_tree *inode_cache;
1807 struct shared_node *active_node;
1809 if (wc->root_level == wc->active_node &&
1810 btrfs_root_refs(&root->root_item) == 0)
1813 active_node = wc->nodes[wc->active_node];
1814 inode_cache = &active_node->inode_cache;
1815 nritems = btrfs_header_nritems(eb);
1816 for (i = 0; i < nritems; i++) {
1817 btrfs_item_key_to_cpu(eb, &key, i);
1819 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1821 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1824 if (active_node->current == NULL ||
1825 active_node->current->ino < key.objectid) {
1826 if (active_node->current) {
1827 active_node->current->checked = 1;
1828 maybe_free_inode_rec(inode_cache,
1829 active_node->current);
1831 active_node->current = get_inode_rec(inode_cache,
1833 BUG_ON(IS_ERR(active_node->current));
1836 case BTRFS_DIR_ITEM_KEY:
1837 case BTRFS_DIR_INDEX_KEY:
1838 ret = process_dir_item(root, eb, i, &key, active_node);
1840 case BTRFS_INODE_REF_KEY:
1841 ret = process_inode_ref(eb, i, &key, active_node);
1843 case BTRFS_INODE_EXTREF_KEY:
1844 ret = process_inode_extref(eb, i, &key, active_node);
1846 case BTRFS_INODE_ITEM_KEY:
1847 ret = process_inode_item(eb, i, &key, active_node);
1849 case BTRFS_EXTENT_DATA_KEY:
1850 ret = process_file_extent(root, eb, i, &key,
1861 u64 bytenr[BTRFS_MAX_LEVEL];
1862 u64 refs[BTRFS_MAX_LEVEL];
1863 int need_check[BTRFS_MAX_LEVEL];
1866 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
1867 struct node_refs *nrefs, u64 level);
1868 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
1869 unsigned int ext_ref);
1871 static int process_one_leaf_v2(struct btrfs_root *root, struct btrfs_path *path,
1872 struct node_refs *nrefs, int *level, int ext_ref)
1874 struct extent_buffer *cur = path->nodes[0];
1875 struct btrfs_key key;
1879 int root_level = btrfs_header_level(root->node);
1881 int ret = 0; /* Final return value */
1882 int err = 0; /* Positive error bitmap */
1884 cur_bytenr = cur->start;
1886 /* skip to first inode item or the first inode number change */
1887 nritems = btrfs_header_nritems(cur);
1888 for (i = 0; i < nritems; i++) {
1889 btrfs_item_key_to_cpu(cur, &key, i);
1891 first_ino = key.objectid;
1892 if (key.type == BTRFS_INODE_ITEM_KEY ||
1893 (first_ino && first_ino != key.objectid))
1897 path->slots[0] = nritems;
1903 err |= check_inode_item(root, path, ext_ref);
1905 if (err & LAST_ITEM)
1908 /* still have inode items in thie leaf */
1909 if (cur->start == cur_bytenr)
1913 * we have switched to another leaf, above nodes may
1914 * have changed, here walk down the path, if a node
1915 * or leaf is shared, check whether we can skip this
1918 for (i = root_level; i >= 0; i--) {
1919 if (path->nodes[i]->start == nrefs->bytenr[i])
1922 ret = update_nodes_refs(root,
1923 path->nodes[i]->start,
1928 if (!nrefs->need_check[i]) {
1934 for (i = 0; i < *level; i++) {
1935 free_extent_buffer(path->nodes[i]);
1936 path->nodes[i] = NULL;
1941 * Convert any error bitmap to -EIO, as we should avoid
1942 * mixing positive and negative return value to represent
1950 static void reada_walk_down(struct btrfs_root *root,
1951 struct extent_buffer *node, int slot)
1960 level = btrfs_header_level(node);
1964 nritems = btrfs_header_nritems(node);
1965 blocksize = root->nodesize;
1966 for (i = slot; i < nritems; i++) {
1967 bytenr = btrfs_node_blockptr(node, i);
1968 ptr_gen = btrfs_node_ptr_generation(node, i);
1969 readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1974 * Check the child node/leaf by the following condition:
1975 * 1. the first item key of the node/leaf should be the same with the one
1977 * 2. block in parent node should match the child node/leaf.
1978 * 3. generation of parent node and child's header should be consistent.
1980 * Or the child node/leaf pointed by the key in parent is not valid.
1982 * We hope to check leaf owner too, but since subvol may share leaves,
1983 * which makes leaf owner check not so strong, key check should be
1984 * sufficient enough for that case.
1986 static int check_child_node(struct btrfs_root *root,
1987 struct extent_buffer *parent, int slot,
1988 struct extent_buffer *child)
1990 struct btrfs_key parent_key;
1991 struct btrfs_key child_key;
1994 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1995 if (btrfs_header_level(child) == 0)
1996 btrfs_item_key_to_cpu(child, &child_key, 0);
1998 btrfs_node_key_to_cpu(child, &child_key, 0);
2000 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
2003 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
2004 parent_key.objectid, parent_key.type, parent_key.offset,
2005 child_key.objectid, child_key.type, child_key.offset);
2007 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
2009 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
2010 btrfs_node_blockptr(parent, slot),
2011 btrfs_header_bytenr(child));
2013 if (btrfs_node_ptr_generation(parent, slot) !=
2014 btrfs_header_generation(child)) {
2016 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
2017 btrfs_header_generation(child),
2018 btrfs_node_ptr_generation(parent, slot));
2024 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
2025 * in every fs or file tree check. Here we find its all root ids, and only check
2026 * it in the fs or file tree which has the smallest root id.
2028 static int need_check(struct btrfs_root *root, struct ulist *roots)
2030 struct rb_node *node;
2031 struct ulist_node *u;
2033 if (roots->nnodes == 1)
2036 node = rb_first(&roots->root);
2037 u = rb_entry(node, struct ulist_node, rb_node);
2039 * current root id is not smallest, we skip it and let it be checked
2040 * in the fs or file tree who hash the smallest root id.
2042 if (root->objectid != u->val)
2049 * for a tree node or leaf, we record its reference count, so later if we still
2050 * process this node or leaf, don't need to compute its reference count again.
2052 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
2053 struct node_refs *nrefs, u64 level)
2057 struct ulist *roots;
2059 if (nrefs->bytenr[level] != bytenr) {
2060 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2061 level, 1, &refs, NULL);
2065 nrefs->bytenr[level] = bytenr;
2066 nrefs->refs[level] = refs;
2068 ret = btrfs_find_all_roots(NULL, root->fs_info, bytenr,
2073 check = need_check(root, roots);
2075 nrefs->need_check[level] = check;
2077 nrefs->need_check[level] = 1;
2084 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
2085 struct walk_control *wc, int *level,
2086 struct node_refs *nrefs)
2088 enum btrfs_tree_block_status status;
2091 struct extent_buffer *next;
2092 struct extent_buffer *cur;
2097 WARN_ON(*level < 0);
2098 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2100 if (path->nodes[*level]->start == nrefs->bytenr[*level]) {
2101 refs = nrefs->refs[*level];
2104 ret = btrfs_lookup_extent_info(NULL, root,
2105 path->nodes[*level]->start,
2106 *level, 1, &refs, NULL);
2111 nrefs->bytenr[*level] = path->nodes[*level]->start;
2112 nrefs->refs[*level] = refs;
2116 ret = enter_shared_node(root, path->nodes[*level]->start,
2124 while (*level >= 0) {
2125 WARN_ON(*level < 0);
2126 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2127 cur = path->nodes[*level];
2129 if (btrfs_header_level(cur) != *level)
2132 if (path->slots[*level] >= btrfs_header_nritems(cur))
2135 ret = process_one_leaf(root, cur, wc);
2140 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2141 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2142 blocksize = root->nodesize;
2144 if (bytenr == nrefs->bytenr[*level - 1]) {
2145 refs = nrefs->refs[*level - 1];
2147 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2148 *level - 1, 1, &refs, NULL);
2152 nrefs->bytenr[*level - 1] = bytenr;
2153 nrefs->refs[*level - 1] = refs;
2158 ret = enter_shared_node(root, bytenr, refs,
2161 path->slots[*level]++;
2166 next = btrfs_find_tree_block(root, bytenr, blocksize);
2167 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2168 free_extent_buffer(next);
2169 reada_walk_down(root, cur, path->slots[*level]);
2170 next = read_tree_block(root, bytenr, blocksize,
2172 if (!extent_buffer_uptodate(next)) {
2173 struct btrfs_key node_key;
2175 btrfs_node_key_to_cpu(path->nodes[*level],
2177 path->slots[*level]);
2178 btrfs_add_corrupt_extent_record(root->fs_info,
2180 path->nodes[*level]->start,
2181 root->nodesize, *level);
2187 ret = check_child_node(root, cur, path->slots[*level], next);
2193 if (btrfs_is_leaf(next))
2194 status = btrfs_check_leaf(root, NULL, next);
2196 status = btrfs_check_node(root, NULL, next);
2197 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2198 free_extent_buffer(next);
2203 *level = *level - 1;
2204 free_extent_buffer(path->nodes[*level]);
2205 path->nodes[*level] = next;
2206 path->slots[*level] = 0;
2209 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2213 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
2214 unsigned int ext_ref);
2216 static int walk_down_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2217 int *level, struct node_refs *nrefs, int ext_ref)
2219 enum btrfs_tree_block_status status;
2222 struct extent_buffer *next;
2223 struct extent_buffer *cur;
2227 WARN_ON(*level < 0);
2228 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2230 ret = update_nodes_refs(root, path->nodes[*level]->start,
2235 while (*level >= 0) {
2236 WARN_ON(*level < 0);
2237 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2238 cur = path->nodes[*level];
2240 if (btrfs_header_level(cur) != *level)
2243 if (path->slots[*level] >= btrfs_header_nritems(cur))
2245 /* Don't forgot to check leaf/node validation */
2247 ret = btrfs_check_leaf(root, NULL, cur);
2248 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2252 ret = process_one_leaf_v2(root, path, nrefs,
2256 ret = btrfs_check_node(root, NULL, cur);
2257 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2262 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2263 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2264 blocksize = root->nodesize;
2266 ret = update_nodes_refs(root, bytenr, nrefs, *level - 1);
2269 if (!nrefs->need_check[*level - 1]) {
2270 path->slots[*level]++;
2274 next = btrfs_find_tree_block(root, bytenr, blocksize);
2275 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2276 free_extent_buffer(next);
2277 reada_walk_down(root, cur, path->slots[*level]);
2278 next = read_tree_block(root, bytenr, blocksize,
2280 if (!extent_buffer_uptodate(next)) {
2281 struct btrfs_key node_key;
2283 btrfs_node_key_to_cpu(path->nodes[*level],
2285 path->slots[*level]);
2286 btrfs_add_corrupt_extent_record(root->fs_info,
2288 path->nodes[*level]->start,
2289 root->nodesize, *level);
2295 ret = check_child_node(root, cur, path->slots[*level], next);
2299 if (btrfs_is_leaf(next))
2300 status = btrfs_check_leaf(root, NULL, next);
2302 status = btrfs_check_node(root, NULL, next);
2303 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2304 free_extent_buffer(next);
2309 *level = *level - 1;
2310 free_extent_buffer(path->nodes[*level]);
2311 path->nodes[*level] = next;
2312 path->slots[*level] = 0;
2317 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
2318 struct walk_control *wc, int *level)
2321 struct extent_buffer *leaf;
2323 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2324 leaf = path->nodes[i];
2325 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2330 free_extent_buffer(path->nodes[*level]);
2331 path->nodes[*level] = NULL;
2332 BUG_ON(*level > wc->active_node);
2333 if (*level == wc->active_node)
2334 leave_shared_node(root, wc, *level);
2341 static int walk_up_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2345 struct extent_buffer *leaf;
2347 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2348 leaf = path->nodes[i];
2349 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2354 free_extent_buffer(path->nodes[*level]);
2355 path->nodes[*level] = NULL;
2362 static int check_root_dir(struct inode_record *rec)
2364 struct inode_backref *backref;
2367 if (!rec->found_inode_item || rec->errors)
2369 if (rec->nlink != 1 || rec->found_link != 0)
2371 if (list_empty(&rec->backrefs))
2373 backref = to_inode_backref(rec->backrefs.next);
2374 if (!backref->found_inode_ref)
2376 if (backref->index != 0 || backref->namelen != 2 ||
2377 memcmp(backref->name, "..", 2))
2379 if (backref->found_dir_index || backref->found_dir_item)
2386 static int repair_inode_isize(struct btrfs_trans_handle *trans,
2387 struct btrfs_root *root, struct btrfs_path *path,
2388 struct inode_record *rec)
2390 struct btrfs_inode_item *ei;
2391 struct btrfs_key key;
2394 key.objectid = rec->ino;
2395 key.type = BTRFS_INODE_ITEM_KEY;
2396 key.offset = (u64)-1;
2398 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2402 if (!path->slots[0]) {
2409 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2410 if (key.objectid != rec->ino) {
2415 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2416 struct btrfs_inode_item);
2417 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
2418 btrfs_mark_buffer_dirty(path->nodes[0]);
2419 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2420 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
2421 root->root_key.objectid);
2423 btrfs_release_path(path);
2427 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
2428 struct btrfs_root *root,
2429 struct btrfs_path *path,
2430 struct inode_record *rec)
2434 ret = btrfs_add_orphan_item(trans, root, path, rec->ino);
2435 btrfs_release_path(path);
2437 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2441 static int repair_inode_nbytes(struct btrfs_trans_handle *trans,
2442 struct btrfs_root *root,
2443 struct btrfs_path *path,
2444 struct inode_record *rec)
2446 struct btrfs_inode_item *ei;
2447 struct btrfs_key key;
2450 key.objectid = rec->ino;
2451 key.type = BTRFS_INODE_ITEM_KEY;
2454 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2461 /* Since ret == 0, no need to check anything */
2462 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2463 struct btrfs_inode_item);
2464 btrfs_set_inode_nbytes(path->nodes[0], ei, rec->found_size);
2465 btrfs_mark_buffer_dirty(path->nodes[0]);
2466 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
2467 printf("reset nbytes for ino %llu root %llu\n",
2468 rec->ino, root->root_key.objectid);
2470 btrfs_release_path(path);
2474 static int add_missing_dir_index(struct btrfs_root *root,
2475 struct cache_tree *inode_cache,
2476 struct inode_record *rec,
2477 struct inode_backref *backref)
2479 struct btrfs_path path;
2480 struct btrfs_trans_handle *trans;
2481 struct btrfs_dir_item *dir_item;
2482 struct extent_buffer *leaf;
2483 struct btrfs_key key;
2484 struct btrfs_disk_key disk_key;
2485 struct inode_record *dir_rec;
2486 unsigned long name_ptr;
2487 u32 data_size = sizeof(*dir_item) + backref->namelen;
2490 trans = btrfs_start_transaction(root, 1);
2492 return PTR_ERR(trans);
2494 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
2495 (unsigned long long)rec->ino);
2497 btrfs_init_path(&path);
2498 key.objectid = backref->dir;
2499 key.type = BTRFS_DIR_INDEX_KEY;
2500 key.offset = backref->index;
2501 ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
2504 leaf = path.nodes[0];
2505 dir_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_dir_item);
2507 disk_key.objectid = cpu_to_le64(rec->ino);
2508 disk_key.type = BTRFS_INODE_ITEM_KEY;
2509 disk_key.offset = 0;
2511 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
2512 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
2513 btrfs_set_dir_data_len(leaf, dir_item, 0);
2514 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
2515 name_ptr = (unsigned long)(dir_item + 1);
2516 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
2517 btrfs_mark_buffer_dirty(leaf);
2518 btrfs_release_path(&path);
2519 btrfs_commit_transaction(trans, root);
2521 backref->found_dir_index = 1;
2522 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
2523 BUG_ON(IS_ERR(dir_rec));
2526 dir_rec->found_size += backref->namelen;
2527 if (dir_rec->found_size == dir_rec->isize &&
2528 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
2529 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2530 if (dir_rec->found_size != dir_rec->isize)
2531 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
2536 static int delete_dir_index(struct btrfs_root *root,
2537 struct cache_tree *inode_cache,
2538 struct inode_record *rec,
2539 struct inode_backref *backref)
2541 struct btrfs_trans_handle *trans;
2542 struct btrfs_dir_item *di;
2543 struct btrfs_path path;
2546 trans = btrfs_start_transaction(root, 1);
2548 return PTR_ERR(trans);
2550 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
2551 (unsigned long long)backref->dir,
2552 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
2553 (unsigned long long)root->objectid);
2555 btrfs_init_path(&path);
2556 di = btrfs_lookup_dir_index(trans, root, &path, backref->dir,
2557 backref->name, backref->namelen,
2558 backref->index, -1);
2561 btrfs_release_path(&path);
2562 btrfs_commit_transaction(trans, root);
2569 ret = btrfs_del_item(trans, root, &path);
2571 ret = btrfs_delete_one_dir_name(trans, root, &path, di);
2573 btrfs_release_path(&path);
2574 btrfs_commit_transaction(trans, root);
2578 static int create_inode_item(struct btrfs_root *root,
2579 struct inode_record *rec,
2580 struct inode_backref *backref, int root_dir)
2582 struct btrfs_trans_handle *trans;
2583 struct btrfs_inode_item inode_item;
2584 time_t now = time(NULL);
2587 trans = btrfs_start_transaction(root, 1);
2588 if (IS_ERR(trans)) {
2589 ret = PTR_ERR(trans);
2593 fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
2594 "be incomplete, please check permissions and content after "
2595 "the fsck completes.\n", (unsigned long long)root->objectid,
2596 (unsigned long long)rec->ino);
2598 memset(&inode_item, 0, sizeof(inode_item));
2599 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
2601 btrfs_set_stack_inode_nlink(&inode_item, 1);
2603 btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
2604 btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
2605 if (rec->found_dir_item) {
2606 if (rec->found_file_extent)
2607 fprintf(stderr, "root %llu inode %llu has both a dir "
2608 "item and extents, unsure if it is a dir or a "
2609 "regular file so setting it as a directory\n",
2610 (unsigned long long)root->objectid,
2611 (unsigned long long)rec->ino);
2612 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
2613 btrfs_set_stack_inode_size(&inode_item, rec->found_size);
2614 } else if (!rec->found_dir_item) {
2615 btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
2616 btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
2618 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
2619 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
2620 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
2621 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
2622 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
2623 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
2624 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
2625 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
2627 ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
2629 btrfs_commit_transaction(trans, root);
2633 static int repair_inode_backrefs(struct btrfs_root *root,
2634 struct inode_record *rec,
2635 struct cache_tree *inode_cache,
2638 struct inode_backref *tmp, *backref;
2639 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2643 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2644 if (!delete && rec->ino == root_dirid) {
2645 if (!rec->found_inode_item) {
2646 ret = create_inode_item(root, rec, backref, 1);
2653 /* Index 0 for root dir's are special, don't mess with it */
2654 if (rec->ino == root_dirid && backref->index == 0)
2658 ((backref->found_dir_index && !backref->found_inode_ref) ||
2659 (backref->found_dir_index && backref->found_inode_ref &&
2660 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
2661 ret = delete_dir_index(root, inode_cache, rec, backref);
2665 list_del(&backref->list);
2669 if (!delete && !backref->found_dir_index &&
2670 backref->found_dir_item && backref->found_inode_ref) {
2671 ret = add_missing_dir_index(root, inode_cache, rec,
2676 if (backref->found_dir_item &&
2677 backref->found_dir_index &&
2678 backref->found_dir_index) {
2679 if (!backref->errors &&
2680 backref->found_inode_ref) {
2681 list_del(&backref->list);
2687 if (!delete && (!backref->found_dir_index &&
2688 !backref->found_dir_item &&
2689 backref->found_inode_ref)) {
2690 struct btrfs_trans_handle *trans;
2691 struct btrfs_key location;
2693 ret = check_dir_conflict(root, backref->name,
2699 * let nlink fixing routine to handle it,
2700 * which can do it better.
2705 location.objectid = rec->ino;
2706 location.type = BTRFS_INODE_ITEM_KEY;
2707 location.offset = 0;
2709 trans = btrfs_start_transaction(root, 1);
2710 if (IS_ERR(trans)) {
2711 ret = PTR_ERR(trans);
2714 fprintf(stderr, "adding missing dir index/item pair "
2716 (unsigned long long)rec->ino);
2717 ret = btrfs_insert_dir_item(trans, root, backref->name,
2719 backref->dir, &location,
2720 imode_to_type(rec->imode),
2723 btrfs_commit_transaction(trans, root);
2727 if (!delete && (backref->found_inode_ref &&
2728 backref->found_dir_index &&
2729 backref->found_dir_item &&
2730 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
2731 !rec->found_inode_item)) {
2732 ret = create_inode_item(root, rec, backref, 0);
2739 return ret ? ret : repaired;
2743 * To determine the file type for nlink/inode_item repair
2745 * Return 0 if file type is found and BTRFS_FT_* is stored into type.
2746 * Return -ENOENT if file type is not found.
2748 static int find_file_type(struct inode_record *rec, u8 *type)
2750 struct inode_backref *backref;
2752 /* For inode item recovered case */
2753 if (rec->found_inode_item) {
2754 *type = imode_to_type(rec->imode);
2758 list_for_each_entry(backref, &rec->backrefs, list) {
2759 if (backref->found_dir_index || backref->found_dir_item) {
2760 *type = backref->filetype;
2768 * To determine the file name for nlink repair
2770 * Return 0 if file name is found, set name and namelen.
2771 * Return -ENOENT if file name is not found.
2773 static int find_file_name(struct inode_record *rec,
2774 char *name, int *namelen)
2776 struct inode_backref *backref;
2778 list_for_each_entry(backref, &rec->backrefs, list) {
2779 if (backref->found_dir_index || backref->found_dir_item ||
2780 backref->found_inode_ref) {
2781 memcpy(name, backref->name, backref->namelen);
2782 *namelen = backref->namelen;
2789 /* Reset the nlink of the inode to the correct one */
2790 static int reset_nlink(struct btrfs_trans_handle *trans,
2791 struct btrfs_root *root,
2792 struct btrfs_path *path,
2793 struct inode_record *rec)
2795 struct inode_backref *backref;
2796 struct inode_backref *tmp;
2797 struct btrfs_key key;
2798 struct btrfs_inode_item *inode_item;
2801 /* We don't believe this either, reset it and iterate backref */
2802 rec->found_link = 0;
2804 /* Remove all backref including the valid ones */
2805 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2806 ret = btrfs_unlink(trans, root, rec->ino, backref->dir,
2807 backref->index, backref->name,
2808 backref->namelen, 0);
2812 /* remove invalid backref, so it won't be added back */
2813 if (!(backref->found_dir_index &&
2814 backref->found_dir_item &&
2815 backref->found_inode_ref)) {
2816 list_del(&backref->list);
2823 /* Set nlink to 0 */
2824 key.objectid = rec->ino;
2825 key.type = BTRFS_INODE_ITEM_KEY;
2827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2834 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2835 struct btrfs_inode_item);
2836 btrfs_set_inode_nlink(path->nodes[0], inode_item, 0);
2837 btrfs_mark_buffer_dirty(path->nodes[0]);
2838 btrfs_release_path(path);
2841 * Add back valid inode_ref/dir_item/dir_index,
2842 * add_link() will handle the nlink inc, so new nlink must be correct
2844 list_for_each_entry(backref, &rec->backrefs, list) {
2845 ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
2846 backref->name, backref->namelen,
2847 backref->filetype, &backref->index, 1);
2852 btrfs_release_path(path);
2856 static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
2857 struct btrfs_root *root,
2858 struct btrfs_path *path,
2859 struct inode_record *rec)
2861 char *dir_name = "lost+found";
2862 char namebuf[BTRFS_NAME_LEN] = {0};
2867 int name_recovered = 0;
2868 int type_recovered = 0;
2872 * Get file name and type first before these invalid inode ref
2873 * are deleted by remove_all_invalid_backref()
2875 name_recovered = !find_file_name(rec, namebuf, &namelen);
2876 type_recovered = !find_file_type(rec, &type);
2878 if (!name_recovered) {
2879 printf("Can't get file name for inode %llu, using '%llu' as fallback\n",
2880 rec->ino, rec->ino);
2881 namelen = count_digits(rec->ino);
2882 sprintf(namebuf, "%llu", rec->ino);
2885 if (!type_recovered) {
2886 printf("Can't get file type for inode %llu, using FILE as fallback\n",
2888 type = BTRFS_FT_REG_FILE;
2892 ret = reset_nlink(trans, root, path, rec);
2895 "Failed to reset nlink for inode %llu: %s\n",
2896 rec->ino, strerror(-ret));
2900 if (rec->found_link == 0) {
2901 lost_found_ino = root->highest_inode;
2902 if (lost_found_ino >= BTRFS_LAST_FREE_OBJECTID) {
2907 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
2908 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
2911 fprintf(stderr, "Failed to create '%s' dir: %s\n",
2912 dir_name, strerror(-ret));
2915 ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
2916 namebuf, namelen, type, NULL, 1);
2918 * Add ".INO" suffix several times to handle case where
2919 * "FILENAME.INO" is already taken by another file.
2921 while (ret == -EEXIST) {
2923 * Conflicting file name, add ".INO" as suffix * +1 for '.'
2925 if (namelen + count_digits(rec->ino) + 1 >
2930 snprintf(namebuf + namelen, BTRFS_NAME_LEN - namelen,
2932 namelen += count_digits(rec->ino) + 1;
2933 ret = btrfs_add_link(trans, root, rec->ino,
2934 lost_found_ino, namebuf,
2935 namelen, type, NULL, 1);
2939 "Failed to link the inode %llu to %s dir: %s\n",
2940 rec->ino, dir_name, strerror(-ret));
2944 * Just increase the found_link, don't actually add the
2945 * backref. This will make things easier and this inode
2946 * record will be freed after the repair is done.
2947 * So fsck will not report problem about this inode.
2950 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
2951 namelen, namebuf, dir_name);
2953 printf("Fixed the nlink of inode %llu\n", rec->ino);
2956 * Clear the flag anyway, or we will loop forever for the same inode
2957 * as it will not be removed from the bad inode list and the dead loop
2960 rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
2961 btrfs_release_path(path);
2966 * Check if there is any normal(reg or prealloc) file extent for given
2968 * This is used to determine the file type when neither its dir_index/item or
2969 * inode_item exists.
2971 * This will *NOT* report error, if any error happens, just consider it does
2972 * not have any normal file extent.
2974 static int find_normal_file_extent(struct btrfs_root *root, u64 ino)
2976 struct btrfs_path path;
2977 struct btrfs_key key;
2978 struct btrfs_key found_key;
2979 struct btrfs_file_extent_item *fi;
2983 btrfs_init_path(&path);
2985 key.type = BTRFS_EXTENT_DATA_KEY;
2988 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2993 if (ret && path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
2994 ret = btrfs_next_leaf(root, &path);
3001 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
3003 if (found_key.objectid != ino ||
3004 found_key.type != BTRFS_EXTENT_DATA_KEY)
3006 fi = btrfs_item_ptr(path.nodes[0], path.slots[0],
3007 struct btrfs_file_extent_item);
3008 type = btrfs_file_extent_type(path.nodes[0], fi);
3009 if (type != BTRFS_FILE_EXTENT_INLINE) {
3015 btrfs_release_path(&path);
3019 static u32 btrfs_type_to_imode(u8 type)
3021 static u32 imode_by_btrfs_type[] = {
3022 [BTRFS_FT_REG_FILE] = S_IFREG,
3023 [BTRFS_FT_DIR] = S_IFDIR,
3024 [BTRFS_FT_CHRDEV] = S_IFCHR,
3025 [BTRFS_FT_BLKDEV] = S_IFBLK,
3026 [BTRFS_FT_FIFO] = S_IFIFO,
3027 [BTRFS_FT_SOCK] = S_IFSOCK,
3028 [BTRFS_FT_SYMLINK] = S_IFLNK,
3031 return imode_by_btrfs_type[(type)];
3034 static int repair_inode_no_item(struct btrfs_trans_handle *trans,
3035 struct btrfs_root *root,
3036 struct btrfs_path *path,
3037 struct inode_record *rec)
3041 int type_recovered = 0;
3044 printf("Trying to rebuild inode:%llu\n", rec->ino);
3046 type_recovered = !find_file_type(rec, &filetype);
3049 * Try to determine inode type if type not found.
3051 * For found regular file extent, it must be FILE.
3052 * For found dir_item/index, it must be DIR.
3054 * For undetermined one, use FILE as fallback.
3057 * 1. If found backref(inode_index/item is already handled) to it,
3059 * Need new inode-inode ref structure to allow search for that.
3061 if (!type_recovered) {
3062 if (rec->found_file_extent &&
3063 find_normal_file_extent(root, rec->ino)) {
3065 filetype = BTRFS_FT_REG_FILE;
3066 } else if (rec->found_dir_item) {
3068 filetype = BTRFS_FT_DIR;
3069 } else if (!list_empty(&rec->orphan_extents)) {
3071 filetype = BTRFS_FT_REG_FILE;
3073 printf("Can't determine the filetype for inode %llu, assume it is a normal file\n",
3076 filetype = BTRFS_FT_REG_FILE;
3080 ret = btrfs_new_inode(trans, root, rec->ino,
3081 mode | btrfs_type_to_imode(filetype));
3086 * Here inode rebuild is done, we only rebuild the inode item,
3087 * don't repair the nlink(like move to lost+found).
3088 * That is the job of nlink repair.
3090 * We just fill the record and return
3092 rec->found_dir_item = 1;
3093 rec->imode = mode | btrfs_type_to_imode(filetype);
3095 rec->errors &= ~I_ERR_NO_INODE_ITEM;
3096 /* Ensure the inode_nlinks repair function will be called */
3097 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3102 static int repair_inode_orphan_extent(struct btrfs_trans_handle *trans,
3103 struct btrfs_root *root,
3104 struct btrfs_path *path,
3105 struct inode_record *rec)
3107 struct orphan_data_extent *orphan;
3108 struct orphan_data_extent *tmp;
3111 list_for_each_entry_safe(orphan, tmp, &rec->orphan_extents, list) {
3113 * Check for conflicting file extents
3115 * Here we don't know whether the extents is compressed or not,
3116 * so we can only assume it not compressed nor data offset,
3117 * and use its disk_len as extent length.
3119 ret = btrfs_get_extent(NULL, root, path, orphan->objectid,
3120 orphan->offset, orphan->disk_len, 0);
3121 btrfs_release_path(path);
3126 "orphan extent (%llu, %llu) conflicts, delete the orphan\n",
3127 orphan->disk_bytenr, orphan->disk_len);
3128 ret = btrfs_free_extent(trans,
3129 root->fs_info->extent_root,
3130 orphan->disk_bytenr, orphan->disk_len,
3131 0, root->objectid, orphan->objectid,
3136 ret = btrfs_insert_file_extent(trans, root, orphan->objectid,
3137 orphan->offset, orphan->disk_bytenr,
3138 orphan->disk_len, orphan->disk_len);
3142 /* Update file size info */
3143 rec->found_size += orphan->disk_len;
3144 if (rec->found_size == rec->nbytes)
3145 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
3147 /* Update the file extent hole info too */
3148 ret = del_file_extent_hole(&rec->holes, orphan->offset,
3152 if (RB_EMPTY_ROOT(&rec->holes))
3153 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3155 list_del(&orphan->list);
3158 rec->errors &= ~I_ERR_FILE_EXTENT_ORPHAN;
3163 static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
3164 struct btrfs_root *root,
3165 struct btrfs_path *path,
3166 struct inode_record *rec)
3168 struct rb_node *node;
3169 struct file_extent_hole *hole;
3173 node = rb_first(&rec->holes);
3177 hole = rb_entry(node, struct file_extent_hole, node);
3178 ret = btrfs_punch_hole(trans, root, rec->ino,
3179 hole->start, hole->len);
3182 ret = del_file_extent_hole(&rec->holes, hole->start,
3186 if (RB_EMPTY_ROOT(&rec->holes))
3187 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3188 node = rb_first(&rec->holes);
3190 /* special case for a file losing all its file extent */
3192 ret = btrfs_punch_hole(trans, root, rec->ino, 0,
3193 round_up(rec->isize, root->sectorsize));
3197 printf("Fixed discount file extents for inode: %llu in root: %llu\n",
3198 rec->ino, root->objectid);
3203 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
3205 struct btrfs_trans_handle *trans;
3206 struct btrfs_path path;
3209 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |
3210 I_ERR_NO_ORPHAN_ITEM |
3211 I_ERR_LINK_COUNT_WRONG |
3212 I_ERR_NO_INODE_ITEM |
3213 I_ERR_FILE_EXTENT_ORPHAN |
3214 I_ERR_FILE_EXTENT_DISCOUNT|
3215 I_ERR_FILE_NBYTES_WRONG)))
3219 * For nlink repair, it may create a dir and add link, so
3220 * 2 for parent(256)'s dir_index and dir_item
3221 * 2 for lost+found dir's inode_item and inode_ref
3222 * 1 for the new inode_ref of the file
3223 * 2 for lost+found dir's dir_index and dir_item for the file
3225 trans = btrfs_start_transaction(root, 7);
3227 return PTR_ERR(trans);
3229 btrfs_init_path(&path);
3230 if (rec->errors & I_ERR_NO_INODE_ITEM)
3231 ret = repair_inode_no_item(trans, root, &path, rec);
3232 if (!ret && rec->errors & I_ERR_FILE_EXTENT_ORPHAN)
3233 ret = repair_inode_orphan_extent(trans, root, &path, rec);
3234 if (!ret && rec->errors & I_ERR_FILE_EXTENT_DISCOUNT)
3235 ret = repair_inode_discount_extent(trans, root, &path, rec);
3236 if (!ret && rec->errors & I_ERR_DIR_ISIZE_WRONG)
3237 ret = repair_inode_isize(trans, root, &path, rec);
3238 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
3239 ret = repair_inode_orphan_item(trans, root, &path, rec);
3240 if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
3241 ret = repair_inode_nlinks(trans, root, &path, rec);
3242 if (!ret && rec->errors & I_ERR_FILE_NBYTES_WRONG)
3243 ret = repair_inode_nbytes(trans, root, &path, rec);
3244 btrfs_commit_transaction(trans, root);
3245 btrfs_release_path(&path);
3249 static int check_inode_recs(struct btrfs_root *root,
3250 struct cache_tree *inode_cache)
3252 struct cache_extent *cache;
3253 struct ptr_node *node;
3254 struct inode_record *rec;
3255 struct inode_backref *backref;
3260 u64 root_dirid = btrfs_root_dirid(&root->root_item);
3262 if (btrfs_root_refs(&root->root_item) == 0) {
3263 if (!cache_tree_empty(inode_cache))
3264 fprintf(stderr, "warning line %d\n", __LINE__);
3269 * We need to record the highest inode number for later 'lost+found'
3271 * We must select an ino not used/referred by any existing inode, or
3272 * 'lost+found' ino may be a missing ino in a corrupted leaf,
3273 * this may cause 'lost+found' dir has wrong nlinks.
3275 cache = last_cache_extent(inode_cache);
3277 node = container_of(cache, struct ptr_node, cache);
3279 if (rec->ino > root->highest_inode)
3280 root->highest_inode = rec->ino;
3284 * We need to repair backrefs first because we could change some of the
3285 * errors in the inode recs.
3287 * We also need to go through and delete invalid backrefs first and then
3288 * add the correct ones second. We do this because we may get EEXIST
3289 * when adding back the correct index because we hadn't yet deleted the
3292 * For example, if we were missing a dir index then the directories
3293 * isize would be wrong, so if we fixed the isize to what we thought it
3294 * would be and then fixed the backref we'd still have a invalid fs, so
3295 * we need to add back the dir index and then check to see if the isize
3300 if (stage == 3 && !err)
3303 cache = search_cache_extent(inode_cache, 0);
3304 while (repair && cache) {
3305 node = container_of(cache, struct ptr_node, cache);
3307 cache = next_cache_extent(cache);
3309 /* Need to free everything up and rescan */
3311 remove_cache_extent(inode_cache, &node->cache);
3313 free_inode_rec(rec);
3317 if (list_empty(&rec->backrefs))
3320 ret = repair_inode_backrefs(root, rec, inode_cache,
3334 rec = get_inode_rec(inode_cache, root_dirid, 0);
3335 BUG_ON(IS_ERR(rec));
3337 ret = check_root_dir(rec);
3339 fprintf(stderr, "root %llu root dir %llu error\n",
3340 (unsigned long long)root->root_key.objectid,
3341 (unsigned long long)root_dirid);
3342 print_inode_error(root, rec);
3347 struct btrfs_trans_handle *trans;
3349 trans = btrfs_start_transaction(root, 1);
3350 if (IS_ERR(trans)) {
3351 err = PTR_ERR(trans);
3356 "root %llu missing its root dir, recreating\n",
3357 (unsigned long long)root->objectid);
3359 ret = btrfs_make_root_dir(trans, root, root_dirid);
3362 btrfs_commit_transaction(trans, root);
3366 fprintf(stderr, "root %llu root dir %llu not found\n",
3367 (unsigned long long)root->root_key.objectid,
3368 (unsigned long long)root_dirid);
3372 cache = search_cache_extent(inode_cache, 0);
3375 node = container_of(cache, struct ptr_node, cache);
3377 remove_cache_extent(inode_cache, &node->cache);
3379 if (rec->ino == root_dirid ||
3380 rec->ino == BTRFS_ORPHAN_OBJECTID) {
3381 free_inode_rec(rec);
3385 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
3386 ret = check_orphan_item(root, rec->ino);
3388 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
3389 if (can_free_inode_rec(rec)) {
3390 free_inode_rec(rec);
3395 if (!rec->found_inode_item)
3396 rec->errors |= I_ERR_NO_INODE_ITEM;
3397 if (rec->found_link != rec->nlink)
3398 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3400 ret = try_repair_inode(root, rec);
3401 if (ret == 0 && can_free_inode_rec(rec)) {
3402 free_inode_rec(rec);
3408 if (!(repair && ret == 0))
3410 print_inode_error(root, rec);
3411 list_for_each_entry(backref, &rec->backrefs, list) {
3412 if (!backref->found_dir_item)
3413 backref->errors |= REF_ERR_NO_DIR_ITEM;
3414 if (!backref->found_dir_index)
3415 backref->errors |= REF_ERR_NO_DIR_INDEX;
3416 if (!backref->found_inode_ref)
3417 backref->errors |= REF_ERR_NO_INODE_REF;
3418 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
3419 " namelen %u name %s filetype %d errors %x",
3420 (unsigned long long)backref->dir,
3421 (unsigned long long)backref->index,
3422 backref->namelen, backref->name,
3423 backref->filetype, backref->errors);
3424 print_ref_error(backref->errors);
3426 free_inode_rec(rec);
3428 return (error > 0) ? -1 : 0;
3431 static struct root_record *get_root_rec(struct cache_tree *root_cache,
3434 struct cache_extent *cache;
3435 struct root_record *rec = NULL;
3438 cache = lookup_cache_extent(root_cache, objectid, 1);
3440 rec = container_of(cache, struct root_record, cache);
3442 rec = calloc(1, sizeof(*rec));
3444 return ERR_PTR(-ENOMEM);
3445 rec->objectid = objectid;
3446 INIT_LIST_HEAD(&rec->backrefs);
3447 rec->cache.start = objectid;
3448 rec->cache.size = 1;
3450 ret = insert_cache_extent(root_cache, &rec->cache);
3452 return ERR_PTR(-EEXIST);
3457 static struct root_backref *get_root_backref(struct root_record *rec,
3458 u64 ref_root, u64 dir, u64 index,
3459 const char *name, int namelen)
3461 struct root_backref *backref;
3463 list_for_each_entry(backref, &rec->backrefs, list) {
3464 if (backref->ref_root != ref_root || backref->dir != dir ||
3465 backref->namelen != namelen)
3467 if (memcmp(name, backref->name, namelen))
3472 backref = calloc(1, sizeof(*backref) + namelen + 1);
3475 backref->ref_root = ref_root;
3477 backref->index = index;
3478 backref->namelen = namelen;
3479 memcpy(backref->name, name, namelen);
3480 backref->name[namelen] = '\0';
3481 list_add_tail(&backref->list, &rec->backrefs);
3485 static void free_root_record(struct cache_extent *cache)
3487 struct root_record *rec;
3488 struct root_backref *backref;
3490 rec = container_of(cache, struct root_record, cache);
3491 while (!list_empty(&rec->backrefs)) {
3492 backref = to_root_backref(rec->backrefs.next);
3493 list_del(&backref->list);
3500 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
3502 static int add_root_backref(struct cache_tree *root_cache,
3503 u64 root_id, u64 ref_root, u64 dir, u64 index,
3504 const char *name, int namelen,
3505 int item_type, int errors)
3507 struct root_record *rec;
3508 struct root_backref *backref;
3510 rec = get_root_rec(root_cache, root_id);
3511 BUG_ON(IS_ERR(rec));
3512 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
3515 backref->errors |= errors;
3517 if (item_type != BTRFS_DIR_ITEM_KEY) {
3518 if (backref->found_dir_index || backref->found_back_ref ||
3519 backref->found_forward_ref) {
3520 if (backref->index != index)
3521 backref->errors |= REF_ERR_INDEX_UNMATCH;
3523 backref->index = index;
3527 if (item_type == BTRFS_DIR_ITEM_KEY) {
3528 if (backref->found_forward_ref)
3530 backref->found_dir_item = 1;
3531 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
3532 backref->found_dir_index = 1;
3533 } else if (item_type == BTRFS_ROOT_REF_KEY) {
3534 if (backref->found_forward_ref)
3535 backref->errors |= REF_ERR_DUP_ROOT_REF;
3536 else if (backref->found_dir_item)
3538 backref->found_forward_ref = 1;
3539 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
3540 if (backref->found_back_ref)
3541 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
3542 backref->found_back_ref = 1;
3547 if (backref->found_forward_ref && backref->found_dir_item)
3548 backref->reachable = 1;
3552 static int merge_root_recs(struct btrfs_root *root,
3553 struct cache_tree *src_cache,
3554 struct cache_tree *dst_cache)
3556 struct cache_extent *cache;
3557 struct ptr_node *node;
3558 struct inode_record *rec;
3559 struct inode_backref *backref;
3562 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
3563 free_inode_recs_tree(src_cache);
3568 cache = search_cache_extent(src_cache, 0);
3571 node = container_of(cache, struct ptr_node, cache);
3573 remove_cache_extent(src_cache, &node->cache);
3576 ret = is_child_root(root, root->objectid, rec->ino);
3582 list_for_each_entry(backref, &rec->backrefs, list) {
3583 BUG_ON(backref->found_inode_ref);
3584 if (backref->found_dir_item)
3585 add_root_backref(dst_cache, rec->ino,
3586 root->root_key.objectid, backref->dir,
3587 backref->index, backref->name,
3588 backref->namelen, BTRFS_DIR_ITEM_KEY,
3590 if (backref->found_dir_index)
3591 add_root_backref(dst_cache, rec->ino,
3592 root->root_key.objectid, backref->dir,
3593 backref->index, backref->name,
3594 backref->namelen, BTRFS_DIR_INDEX_KEY,
3598 free_inode_rec(rec);
3605 static int check_root_refs(struct btrfs_root *root,
3606 struct cache_tree *root_cache)
3608 struct root_record *rec;
3609 struct root_record *ref_root;
3610 struct root_backref *backref;
3611 struct cache_extent *cache;
3617 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
3618 BUG_ON(IS_ERR(rec));
3621 /* fixme: this can not detect circular references */
3624 cache = search_cache_extent(root_cache, 0);
3628 rec = container_of(cache, struct root_record, cache);
3629 cache = next_cache_extent(cache);
3631 if (rec->found_ref == 0)
3634 list_for_each_entry(backref, &rec->backrefs, list) {
3635 if (!backref->reachable)
3638 ref_root = get_root_rec(root_cache,
3640 BUG_ON(IS_ERR(ref_root));
3641 if (ref_root->found_ref > 0)
3644 backref->reachable = 0;
3646 if (rec->found_ref == 0)
3652 cache = search_cache_extent(root_cache, 0);
3656 rec = container_of(cache, struct root_record, cache);
3657 cache = next_cache_extent(cache);
3659 if (rec->found_ref == 0 &&
3660 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3661 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
3662 ret = check_orphan_item(root->fs_info->tree_root,
3668 * If we don't have a root item then we likely just have
3669 * a dir item in a snapshot for this root but no actual
3670 * ref key or anything so it's meaningless.
3672 if (!rec->found_root_item)
3675 fprintf(stderr, "fs tree %llu not referenced\n",
3676 (unsigned long long)rec->objectid);
3680 if (rec->found_ref > 0 && !rec->found_root_item)
3682 list_for_each_entry(backref, &rec->backrefs, list) {
3683 if (!backref->found_dir_item)
3684 backref->errors |= REF_ERR_NO_DIR_ITEM;
3685 if (!backref->found_dir_index)
3686 backref->errors |= REF_ERR_NO_DIR_INDEX;
3687 if (!backref->found_back_ref)
3688 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
3689 if (!backref->found_forward_ref)
3690 backref->errors |= REF_ERR_NO_ROOT_REF;
3691 if (backref->reachable && backref->errors)
3698 fprintf(stderr, "fs tree %llu refs %u %s\n",
3699 (unsigned long long)rec->objectid, rec->found_ref,
3700 rec->found_root_item ? "" : "not found");
3702 list_for_each_entry(backref, &rec->backrefs, list) {
3703 if (!backref->reachable)
3705 if (!backref->errors && rec->found_root_item)
3707 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
3708 " index %llu namelen %u name %s errors %x\n",
3709 (unsigned long long)backref->ref_root,
3710 (unsigned long long)backref->dir,
3711 (unsigned long long)backref->index,
3712 backref->namelen, backref->name,
3714 print_ref_error(backref->errors);
3717 return errors > 0 ? 1 : 0;
3720 static int process_root_ref(struct extent_buffer *eb, int slot,
3721 struct btrfs_key *key,
3722 struct cache_tree *root_cache)
3728 struct btrfs_root_ref *ref;
3729 char namebuf[BTRFS_NAME_LEN];
3732 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
3734 dirid = btrfs_root_ref_dirid(eb, ref);
3735 index = btrfs_root_ref_sequence(eb, ref);
3736 name_len = btrfs_root_ref_name_len(eb, ref);
3738 if (name_len <= BTRFS_NAME_LEN) {
3742 len = BTRFS_NAME_LEN;
3743 error = REF_ERR_NAME_TOO_LONG;
3745 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
3747 if (key->type == BTRFS_ROOT_REF_KEY) {
3748 add_root_backref(root_cache, key->offset, key->objectid, dirid,
3749 index, namebuf, len, key->type, error);
3751 add_root_backref(root_cache, key->objectid, key->offset, dirid,
3752 index, namebuf, len, key->type, error);
3757 static void free_corrupt_block(struct cache_extent *cache)
3759 struct btrfs_corrupt_block *corrupt;
3761 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3765 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
3768 * Repair the btree of the given root.
3770 * The fix is to remove the node key in corrupt_blocks cache_tree.
3771 * and rebalance the tree.
3772 * After the fix, the btree should be writeable.
3774 static int repair_btree(struct btrfs_root *root,
3775 struct cache_tree *corrupt_blocks)
3777 struct btrfs_trans_handle *trans;
3778 struct btrfs_path path;
3779 struct btrfs_corrupt_block *corrupt;
3780 struct cache_extent *cache;
3781 struct btrfs_key key;
3786 if (cache_tree_empty(corrupt_blocks))
3789 trans = btrfs_start_transaction(root, 1);
3790 if (IS_ERR(trans)) {
3791 ret = PTR_ERR(trans);
3792 fprintf(stderr, "Error starting transaction: %s\n",
3796 btrfs_init_path(&path);
3797 cache = first_cache_extent(corrupt_blocks);
3799 corrupt = container_of(cache, struct btrfs_corrupt_block,
3801 level = corrupt->level;
3802 path.lowest_level = level;
3803 key.objectid = corrupt->key.objectid;
3804 key.type = corrupt->key.type;
3805 key.offset = corrupt->key.offset;
3808 * Here we don't want to do any tree balance, since it may
3809 * cause a balance with corrupted brother leaf/node,
3810 * so ins_len set to 0 here.
3811 * Balance will be done after all corrupt node/leaf is deleted.
3813 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
3816 offset = btrfs_node_blockptr(path.nodes[level],
3819 /* Remove the ptr */
3820 ret = btrfs_del_ptr(trans, root, &path, level,
3825 * Remove the corresponding extent
3826 * return value is not concerned.
3828 btrfs_release_path(&path);
3829 ret = btrfs_free_extent(trans, root, offset, root->nodesize,
3830 0, root->root_key.objectid,
3832 cache = next_cache_extent(cache);
3835 /* Balance the btree using btrfs_search_slot() */
3836 cache = first_cache_extent(corrupt_blocks);
3838 corrupt = container_of(cache, struct btrfs_corrupt_block,
3840 memcpy(&key, &corrupt->key, sizeof(key));
3841 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
3844 /* return will always >0 since it won't find the item */
3846 btrfs_release_path(&path);
3847 cache = next_cache_extent(cache);
3850 btrfs_commit_transaction(trans, root);
3851 btrfs_release_path(&path);
3855 static int check_fs_root(struct btrfs_root *root,
3856 struct cache_tree *root_cache,
3857 struct walk_control *wc)
3863 struct btrfs_path path;
3864 struct shared_node root_node;
3865 struct root_record *rec;
3866 struct btrfs_root_item *root_item = &root->root_item;
3867 struct cache_tree corrupt_blocks;
3868 struct orphan_data_extent *orphan;
3869 struct orphan_data_extent *tmp;
3870 enum btrfs_tree_block_status status;
3871 struct node_refs nrefs;
3874 * Reuse the corrupt_block cache tree to record corrupted tree block
3876 * Unlike the usage in extent tree check, here we do it in a per
3877 * fs/subvol tree base.
3879 cache_tree_init(&corrupt_blocks);
3880 root->fs_info->corrupt_blocks = &corrupt_blocks;
3882 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
3883 rec = get_root_rec(root_cache, root->root_key.objectid);
3884 BUG_ON(IS_ERR(rec));
3885 if (btrfs_root_refs(root_item) > 0)
3886 rec->found_root_item = 1;
3889 btrfs_init_path(&path);
3890 memset(&root_node, 0, sizeof(root_node));
3891 cache_tree_init(&root_node.root_cache);
3892 cache_tree_init(&root_node.inode_cache);
3893 memset(&nrefs, 0, sizeof(nrefs));
3895 /* Move the orphan extent record to corresponding inode_record */
3896 list_for_each_entry_safe(orphan, tmp,
3897 &root->orphan_data_extents, list) {
3898 struct inode_record *inode;
3900 inode = get_inode_rec(&root_node.inode_cache, orphan->objectid,
3902 BUG_ON(IS_ERR(inode));
3903 inode->errors |= I_ERR_FILE_EXTENT_ORPHAN;
3904 list_move(&orphan->list, &inode->orphan_extents);
3907 level = btrfs_header_level(root->node);
3908 memset(wc->nodes, 0, sizeof(wc->nodes));
3909 wc->nodes[level] = &root_node;
3910 wc->active_node = level;
3911 wc->root_level = level;
3913 /* We may not have checked the root block, lets do that now */
3914 if (btrfs_is_leaf(root->node))
3915 status = btrfs_check_leaf(root, NULL, root->node);
3917 status = btrfs_check_node(root, NULL, root->node);
3918 if (status != BTRFS_TREE_BLOCK_CLEAN)
3921 if (btrfs_root_refs(root_item) > 0 ||
3922 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3923 path.nodes[level] = root->node;
3924 extent_buffer_get(root->node);
3925 path.slots[level] = 0;
3927 struct btrfs_key key;
3928 struct btrfs_disk_key found_key;
3930 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3931 level = root_item->drop_level;
3932 path.lowest_level = level;
3933 if (level > btrfs_header_level(root->node) ||
3934 level >= BTRFS_MAX_LEVEL) {
3935 error("ignoring invalid drop level: %u", level);
3938 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3941 btrfs_node_key(path.nodes[level], &found_key,
3943 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3944 sizeof(found_key)));
3948 wret = walk_down_tree(root, &path, wc, &level, &nrefs);
3954 wret = walk_up_tree(root, &path, wc, &level);
3961 btrfs_release_path(&path);
3963 if (!cache_tree_empty(&corrupt_blocks)) {
3964 struct cache_extent *cache;
3965 struct btrfs_corrupt_block *corrupt;
3967 printf("The following tree block(s) is corrupted in tree %llu:\n",
3968 root->root_key.objectid);
3969 cache = first_cache_extent(&corrupt_blocks);
3971 corrupt = container_of(cache,
3972 struct btrfs_corrupt_block,
3974 printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n",
3975 cache->start, corrupt->level,
3976 corrupt->key.objectid, corrupt->key.type,
3977 corrupt->key.offset);
3978 cache = next_cache_extent(cache);
3981 printf("Try to repair the btree for root %llu\n",
3982 root->root_key.objectid);
3983 ret = repair_btree(root, &corrupt_blocks);
3985 fprintf(stderr, "Failed to repair btree: %s\n",
3988 printf("Btree for root %llu is fixed\n",
3989 root->root_key.objectid);
3993 err = merge_root_recs(root, &root_node.root_cache, root_cache);
3997 if (root_node.current) {
3998 root_node.current->checked = 1;
3999 maybe_free_inode_rec(&root_node.inode_cache,
4003 err = check_inode_recs(root, &root_node.inode_cache);
4007 free_corrupt_blocks_tree(&corrupt_blocks);
4008 root->fs_info->corrupt_blocks = NULL;
4009 free_orphan_data_extents(&root->orphan_data_extents);
4013 static int fs_root_objectid(u64 objectid)
4015 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
4016 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
4018 return is_fstree(objectid);
4021 static int check_fs_roots(struct btrfs_root *root,
4022 struct cache_tree *root_cache)
4024 struct btrfs_path path;
4025 struct btrfs_key key;
4026 struct walk_control wc;
4027 struct extent_buffer *leaf, *tree_node;
4028 struct btrfs_root *tmp_root;
4029 struct btrfs_root *tree_root = root->fs_info->tree_root;
4033 if (ctx.progress_enabled) {
4034 ctx.tp = TASK_FS_ROOTS;
4035 task_start(ctx.info);
4039 * Just in case we made any changes to the extent tree that weren't
4040 * reflected into the free space cache yet.
4043 reset_cached_block_groups(root->fs_info);
4044 memset(&wc, 0, sizeof(wc));
4045 cache_tree_init(&wc.shared);
4046 btrfs_init_path(&path);
4051 key.type = BTRFS_ROOT_ITEM_KEY;
4052 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4057 tree_node = tree_root->node;
4059 if (tree_node != tree_root->node) {
4060 free_root_recs_tree(root_cache);
4061 btrfs_release_path(&path);
4064 leaf = path.nodes[0];
4065 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
4066 ret = btrfs_next_leaf(tree_root, &path);
4072 leaf = path.nodes[0];
4074 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
4075 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4076 fs_root_objectid(key.objectid)) {
4077 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4078 tmp_root = btrfs_read_fs_root_no_cache(
4079 root->fs_info, &key);
4081 key.offset = (u64)-1;
4082 tmp_root = btrfs_read_fs_root(
4083 root->fs_info, &key);
4085 if (IS_ERR(tmp_root)) {
4089 ret = check_fs_root(tmp_root, root_cache, &wc);
4090 if (ret == -EAGAIN) {
4091 free_root_recs_tree(root_cache);
4092 btrfs_release_path(&path);
4097 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4098 btrfs_free_fs_root(tmp_root);
4099 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4100 key.type == BTRFS_ROOT_BACKREF_KEY) {
4101 process_root_ref(leaf, path.slots[0], &key,
4108 btrfs_release_path(&path);
4110 free_extent_cache_tree(&wc.shared);
4111 if (!cache_tree_empty(&wc.shared))
4112 fprintf(stderr, "warning line %d\n", __LINE__);
4114 task_stop(ctx.info);
4120 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
4121 * INODE_REF/INODE_EXTREF match.
4123 * @root: the root of the fs/file tree
4124 * @ref_key: the key of the INODE_REF/INODE_EXTREF
4125 * @key: the key of the DIR_ITEM/DIR_INDEX
4126 * @index: the index in the INODE_REF/INODE_EXTREF, be used to
4127 * distinguish root_dir between normal dir/file
4128 * @name: the name in the INODE_REF/INODE_EXTREF
4129 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4130 * @mode: the st_mode of INODE_ITEM
4132 * Return 0 if no error occurred.
4133 * Return ROOT_DIR_ERROR if found DIR_ITEM/DIR_INDEX for root_dir.
4134 * Return DIR_ITEM_MISSING if couldn't find DIR_ITEM/DIR_INDEX for normal
4136 * Return DIR_ITEM_MISMATCH if INODE_REF/INODE_EXTREF and DIR_ITEM/DIR_INDEX
4137 * not match for normal dir/file.
4139 static int find_dir_item(struct btrfs_root *root, struct btrfs_key *ref_key,
4140 struct btrfs_key *key, u64 index, char *name,
4141 u32 namelen, u32 mode)
4143 struct btrfs_path path;
4144 struct extent_buffer *node;
4145 struct btrfs_dir_item *di;
4146 struct btrfs_key location;
4147 char namebuf[BTRFS_NAME_LEN] = {0};
4157 btrfs_init_path(&path);
4158 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4160 ret = DIR_ITEM_MISSING;
4164 /* Process root dir and goto out*/
4167 ret = ROOT_DIR_ERROR;
4169 "root %llu INODE %s[%llu %llu] ROOT_DIR shouldn't have %s",
4171 ref_key->type == BTRFS_INODE_REF_KEY ?
4173 ref_key->objectid, ref_key->offset,
4174 key->type == BTRFS_DIR_ITEM_KEY ?
4175 "DIR_ITEM" : "DIR_INDEX");
4183 /* Process normal file/dir */
4185 ret = DIR_ITEM_MISSING;
4187 "root %llu INODE %s[%llu %llu] doesn't have related %s[%llu %llu] namelen %u filename %s filetype %d",
4189 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4190 ref_key->objectid, ref_key->offset,
4191 key->type == BTRFS_DIR_ITEM_KEY ?
4192 "DIR_ITEM" : "DIR_INDEX",
4193 key->objectid, key->offset, namelen, name,
4194 imode_to_type(mode));
4198 /* Check whether inode_id/filetype/name match */
4199 node = path.nodes[0];
4200 slot = path.slots[0];
4201 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4202 total = btrfs_item_size_nr(node, slot);
4203 while (cur < total) {
4204 ret = DIR_ITEM_MISMATCH;
4205 name_len = btrfs_dir_name_len(node, di);
4206 data_len = btrfs_dir_data_len(node, di);
4208 btrfs_dir_item_key_to_cpu(node, di, &location);
4209 if (location.objectid != ref_key->objectid ||
4210 location.type != BTRFS_INODE_ITEM_KEY ||
4211 location.offset != 0)
4214 filetype = btrfs_dir_type(node, di);
4215 if (imode_to_type(mode) != filetype)
4218 if (name_len <= BTRFS_NAME_LEN) {
4221 len = BTRFS_NAME_LEN;
4222 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
4224 key->type == BTRFS_DIR_ITEM_KEY ?
4225 "DIR_ITEM" : "DIR_INDEX",
4226 key->objectid, key->offset, name_len);
4228 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4229 if (len != namelen || strncmp(namebuf, name, len))
4235 len = sizeof(*di) + name_len + data_len;
4236 di = (struct btrfs_dir_item *)((char *)di + len);
4239 if (ret == DIR_ITEM_MISMATCH)
4241 "root %llu INODE %s[%llu %llu] and %s[%llu %llu] mismatch namelen %u filename %s filetype %d",
4243 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4244 ref_key->objectid, ref_key->offset,
4245 key->type == BTRFS_DIR_ITEM_KEY ?
4246 "DIR_ITEM" : "DIR_INDEX",
4247 key->objectid, key->offset, namelen, name,
4248 imode_to_type(mode));
4250 btrfs_release_path(&path);
4255 * Traverse the given INODE_REF and call find_dir_item() to find related
4256 * DIR_ITEM/DIR_INDEX.
4258 * @root: the root of the fs/file tree
4259 * @ref_key: the key of the INODE_REF
4260 * @refs: the count of INODE_REF
4261 * @mode: the st_mode of INODE_ITEM
4263 * Return 0 if no error occurred.
4265 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4266 struct extent_buffer *node, int slot, u64 *refs,
4269 struct btrfs_key key;
4270 struct btrfs_inode_ref *ref;
4271 char namebuf[BTRFS_NAME_LEN] = {0};
4279 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4280 total = btrfs_item_size_nr(node, slot);
4283 /* Update inode ref count */
4286 index = btrfs_inode_ref_index(node, ref);
4287 name_len = btrfs_inode_ref_name_len(node, ref);
4288 if (name_len <= BTRFS_NAME_LEN) {
4291 len = BTRFS_NAME_LEN;
4292 warning("root %llu INODE_REF[%llu %llu] name too long",
4293 root->objectid, ref_key->objectid, ref_key->offset);
4296 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
4298 /* Check root dir ref name */
4299 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4300 error("root %llu INODE_REF[%llu %llu] ROOT_DIR name shouldn't be %s",
4301 root->objectid, ref_key->objectid, ref_key->offset,
4303 err |= ROOT_DIR_ERROR;
4306 /* Find related DIR_INDEX */
4307 key.objectid = ref_key->offset;
4308 key.type = BTRFS_DIR_INDEX_KEY;
4310 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4313 /* Find related dir_item */
4314 key.objectid = ref_key->offset;
4315 key.type = BTRFS_DIR_ITEM_KEY;
4316 key.offset = btrfs_name_hash(namebuf, len);
4317 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4320 len = sizeof(*ref) + name_len;
4321 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4330 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
4331 * DIR_ITEM/DIR_INDEX.
4333 * @root: the root of the fs/file tree
4334 * @ref_key: the key of the INODE_EXTREF
4335 * @refs: the count of INODE_EXTREF
4336 * @mode: the st_mode of INODE_ITEM
4338 * Return 0 if no error occurred.
4340 static int check_inode_extref(struct btrfs_root *root,
4341 struct btrfs_key *ref_key,
4342 struct extent_buffer *node, int slot, u64 *refs,
4345 struct btrfs_key key;
4346 struct btrfs_inode_extref *extref;
4347 char namebuf[BTRFS_NAME_LEN] = {0};
4357 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4358 total = btrfs_item_size_nr(node, slot);
4361 /* update inode ref count */
4363 name_len = btrfs_inode_extref_name_len(node, extref);
4364 index = btrfs_inode_extref_index(node, extref);
4365 parent = btrfs_inode_extref_parent(node, extref);
4366 if (name_len <= BTRFS_NAME_LEN) {
4369 len = BTRFS_NAME_LEN;
4370 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
4371 root->objectid, ref_key->objectid, ref_key->offset);
4373 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
4375 /* Check root dir ref name */
4376 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4377 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
4378 root->objectid, ref_key->objectid, ref_key->offset,
4380 err |= ROOT_DIR_ERROR;
4383 /* find related dir_index */
4384 key.objectid = parent;
4385 key.type = BTRFS_DIR_INDEX_KEY;
4387 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4390 /* find related dir_item */
4391 key.objectid = parent;
4392 key.type = BTRFS_DIR_ITEM_KEY;
4393 key.offset = btrfs_name_hash(namebuf, len);
4394 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4397 len = sizeof(*extref) + name_len;
4398 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4408 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
4409 * DIR_ITEM/DIR_INDEX match.
4411 * @root: the root of the fs/file tree
4412 * @key: the key of the INODE_REF/INODE_EXTREF
4413 * @name: the name in the INODE_REF/INODE_EXTREF
4414 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4415 * @index: the index in the INODE_REF/INODE_EXTREF, for DIR_ITEM set index
4417 * @ext_ref: the EXTENDED_IREF feature
4419 * Return 0 if no error occurred.
4420 * Return >0 for error bitmap
4422 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
4423 char *name, int namelen, u64 index,
4424 unsigned int ext_ref)
4426 struct btrfs_path path;
4427 struct btrfs_inode_ref *ref;
4428 struct btrfs_inode_extref *extref;
4429 struct extent_buffer *node;
4430 char ref_namebuf[BTRFS_NAME_LEN] = {0};
4441 btrfs_init_path(&path);
4442 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4444 ret = INODE_REF_MISSING;
4448 node = path.nodes[0];
4449 slot = path.slots[0];
4451 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4452 total = btrfs_item_size_nr(node, slot);
4454 /* Iterate all entry of INODE_REF */
4455 while (cur < total) {
4456 ret = INODE_REF_MISSING;
4458 ref_namelen = btrfs_inode_ref_name_len(node, ref);
4459 ref_index = btrfs_inode_ref_index(node, ref);
4460 if (index != (u64)-1 && index != ref_index)
4463 if (ref_namelen <= BTRFS_NAME_LEN) {
4466 len = BTRFS_NAME_LEN;
4467 warning("root %llu INODE %s[%llu %llu] name too long",
4469 key->type == BTRFS_INODE_REF_KEY ?
4471 key->objectid, key->offset);
4473 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
4476 if (len != namelen || strncmp(ref_namebuf, name, len))
4482 len = sizeof(*ref) + ref_namelen;
4483 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4488 /* Skip if not support EXTENDED_IREF feature */
4492 btrfs_release_path(&path);
4493 btrfs_init_path(&path);
4495 dir_id = key->offset;
4496 key->type = BTRFS_INODE_EXTREF_KEY;
4497 key->offset = btrfs_extref_hash(dir_id, name, namelen);
4499 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4501 ret = INODE_REF_MISSING;
4505 node = path.nodes[0];
4506 slot = path.slots[0];
4508 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4510 total = btrfs_item_size_nr(node, slot);
4512 /* Iterate all entry of INODE_EXTREF */
4513 while (cur < total) {
4514 ret = INODE_REF_MISSING;
4516 ref_namelen = btrfs_inode_extref_name_len(node, extref);
4517 ref_index = btrfs_inode_extref_index(node, extref);
4518 parent = btrfs_inode_extref_parent(node, extref);
4519 if (index != (u64)-1 && index != ref_index)
4522 if (parent != dir_id)
4525 if (ref_namelen <= BTRFS_NAME_LEN) {
4528 len = BTRFS_NAME_LEN;
4529 warning("Warning: root %llu INODE %s[%llu %llu] name too long\n",
4531 key->type == BTRFS_INODE_REF_KEY ?
4533 key->objectid, key->offset);
4535 read_extent_buffer(node, ref_namebuf,
4536 (unsigned long)(extref + 1), len);
4538 if (len != namelen || strncmp(ref_namebuf, name, len))
4545 len = sizeof(*extref) + ref_namelen;
4546 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4551 btrfs_release_path(&path);
4556 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
4557 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
4559 * @root: the root of the fs/file tree
4560 * @key: the key of the INODE_REF/INODE_EXTREF
4561 * @size: the st_size of the INODE_ITEM
4562 * @ext_ref: the EXTENDED_IREF feature
4564 * Return 0 if no error occurred.
4566 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *key,
4567 struct extent_buffer *node, int slot, u64 *size,
4568 unsigned int ext_ref)
4570 struct btrfs_dir_item *di;
4571 struct btrfs_inode_item *ii;
4572 struct btrfs_path path;
4573 struct btrfs_key location;
4574 char namebuf[BTRFS_NAME_LEN] = {0};
4587 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
4588 * ignore index check.
4590 index = (key->type == BTRFS_DIR_INDEX_KEY) ? key->offset : (u64)-1;
4592 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4593 total = btrfs_item_size_nr(node, slot);
4595 while (cur < total) {
4596 data_len = btrfs_dir_data_len(node, di);
4598 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
4599 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4600 "DIR_ITEM" : "DIR_INDEX",
4601 key->objectid, key->offset, data_len);
4603 name_len = btrfs_dir_name_len(node, di);
4604 if (name_len <= BTRFS_NAME_LEN) {
4607 len = BTRFS_NAME_LEN;
4608 warning("root %llu %s[%llu %llu] name too long",
4610 key->type == BTRFS_DIR_ITEM_KEY ?
4611 "DIR_ITEM" : "DIR_INDEX",
4612 key->objectid, key->offset);
4614 (*size) += name_len;
4616 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4617 filetype = btrfs_dir_type(node, di);
4619 btrfs_init_path(&path);
4620 btrfs_dir_item_key_to_cpu(node, di, &location);
4622 /* Ignore related ROOT_ITEM check */
4623 if (location.type == BTRFS_ROOT_ITEM_KEY)
4626 /* Check relative INODE_ITEM(existence/filetype) */
4627 ret = btrfs_search_slot(NULL, root, &location, &path, 0, 0);
4629 err |= INODE_ITEM_MISSING;
4630 error("root %llu %s[%llu %llu] couldn't find relative INODE_ITEM[%llu] namelen %u filename %s filetype %x",
4631 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4632 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4633 key->offset, location.objectid, name_len,
4638 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
4639 struct btrfs_inode_item);
4640 mode = btrfs_inode_mode(path.nodes[0], ii);
4642 if (imode_to_type(mode) != filetype) {
4643 err |= INODE_ITEM_MISMATCH;
4644 error("root %llu %s[%llu %llu] relative INODE_ITEM filetype mismatch namelen %u filename %s filetype %d",
4645 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4646 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4647 key->offset, name_len, namebuf, filetype);
4650 /* Check relative INODE_REF/INODE_EXTREF */
4651 location.type = BTRFS_INODE_REF_KEY;
4652 location.offset = key->objectid;
4653 ret = find_inode_ref(root, &location, namebuf, len,
4656 if (ret & INODE_REF_MISSING)
4657 error("root %llu %s[%llu %llu] relative INODE_REF missing namelen %u filename %s filetype %d",
4658 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4659 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4660 key->offset, name_len, namebuf, filetype);
4663 btrfs_release_path(&path);
4664 len = sizeof(*di) + name_len + data_len;
4665 di = (struct btrfs_dir_item *)((char *)di + len);
4668 if (key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
4669 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
4670 root->objectid, key->objectid, key->offset);
4679 * Check file extent datasum/hole, update the size of the file extents,
4680 * check and update the last offset of the file extent.
4682 * @root: the root of fs/file tree.
4683 * @fkey: the key of the file extent.
4684 * @nodatasum: INODE_NODATASUM feature.
4685 * @size: the sum of all EXTENT_DATA items size for this inode.
4686 * @end: the offset of the last extent.
4688 * Return 0 if no error occurred.
4690 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
4691 struct extent_buffer *node, int slot,
4692 unsigned int nodatasum, u64 *size, u64 *end)
4694 struct btrfs_file_extent_item *fi;
4697 u64 extent_num_bytes;
4699 unsigned int extent_type;
4700 unsigned int is_hole;
4704 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
4706 extent_type = btrfs_file_extent_type(node, fi);
4707 /* Skip if file extent is inline */
4708 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4709 struct btrfs_item *e = btrfs_item_nr(slot);
4710 u32 item_inline_len;
4712 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
4713 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
4714 if (extent_num_bytes == 0 ||
4715 extent_num_bytes != item_inline_len)
4716 err |= FILE_EXTENT_ERROR;
4717 *size += extent_num_bytes;
4721 /* Check extent type */
4722 if (extent_type != BTRFS_FILE_EXTENT_REG &&
4723 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
4724 err |= FILE_EXTENT_ERROR;
4725 error("root %llu EXTENT_DATA[%llu %llu] type bad",
4726 root->objectid, fkey->objectid, fkey->offset);
4730 /* Check REG_EXTENT/PREALLOC_EXTENT */
4731 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
4732 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
4733 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
4734 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
4736 /* Check EXTENT_DATA datasum */
4737 ret = count_csum_range(root, disk_bytenr, disk_num_bytes, &found);
4738 if (found > 0 && nodatasum) {
4739 err |= ODD_CSUM_ITEM;
4740 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
4741 root->objectid, fkey->objectid, fkey->offset);
4742 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
4744 (ret < 0 || found == 0 || found < disk_num_bytes)) {
4745 err |= CSUM_ITEM_MISSING;
4746 error("root %llu EXTENT_DATA[%llu %llu] datasum missing",
4747 root->objectid, fkey->objectid, fkey->offset);
4748 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC && found > 0) {
4749 err |= ODD_CSUM_ITEM;
4750 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have datasum",
4751 root->objectid, fkey->objectid, fkey->offset);
4754 /* Check EXTENT_DATA hole */
4755 if (no_holes && is_hole) {
4756 err |= FILE_EXTENT_ERROR;
4757 error("root %llu EXTENT_DATA[%llu %llu] shouldn't be hole",
4758 root->objectid, fkey->objectid, fkey->offset);
4759 } else if (!no_holes && *end != fkey->offset) {
4760 err |= FILE_EXTENT_ERROR;
4761 error("root %llu EXTENT_DATA[%llu %llu] interrupt",
4762 root->objectid, fkey->objectid, fkey->offset);
4765 *end += extent_num_bytes;
4767 *size += extent_num_bytes;
4773 * Check INODE_ITEM and related ITEMs (the same inode number)
4774 * 1. check link count
4775 * 2. check inode ref/extref
4776 * 3. check dir item/index
4778 * @ext_ref: the EXTENDED_IREF feature
4780 * Return 0 if no error occurred.
4781 * Return >0 for error or hit the traversal is done(by error bitmap)
4783 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
4784 unsigned int ext_ref)
4786 struct extent_buffer *node;
4787 struct btrfs_inode_item *ii;
4788 struct btrfs_key key;
4797 u64 extent_size = 0;
4799 unsigned int nodatasum;
4804 node = path->nodes[0];
4805 slot = path->slots[0];
4807 btrfs_item_key_to_cpu(node, &key, slot);
4808 inode_id = key.objectid;
4810 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
4811 ret = btrfs_next_item(root, path);
4817 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
4818 isize = btrfs_inode_size(node, ii);
4819 nbytes = btrfs_inode_nbytes(node, ii);
4820 mode = btrfs_inode_mode(node, ii);
4821 dir = imode_to_type(mode) == BTRFS_FT_DIR;
4822 nlink = btrfs_inode_nlink(node, ii);
4823 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
4826 ret = btrfs_next_item(root, path);
4828 /* out will fill 'err' rusing current statistics */
4830 } else if (ret > 0) {
4835 node = path->nodes[0];
4836 slot = path->slots[0];
4837 btrfs_item_key_to_cpu(node, &key, slot);
4838 if (key.objectid != inode_id)
4842 case BTRFS_INODE_REF_KEY:
4843 ret = check_inode_ref(root, &key, node, slot, &refs,
4847 case BTRFS_INODE_EXTREF_KEY:
4848 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
4849 warning("root %llu EXTREF[%llu %llu] isn't supported",
4850 root->objectid, key.objectid,
4852 ret = check_inode_extref(root, &key, node, slot, &refs,
4856 case BTRFS_DIR_ITEM_KEY:
4857 case BTRFS_DIR_INDEX_KEY:
4859 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
4860 root->objectid, inode_id,
4861 imode_to_type(mode), key.objectid,
4864 ret = check_dir_item(root, &key, node, slot, &size,
4868 case BTRFS_EXTENT_DATA_KEY:
4870 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
4871 root->objectid, inode_id, key.objectid,
4874 ret = check_file_extent(root, &key, node, slot,
4875 nodatasum, &extent_size,
4879 case BTRFS_XATTR_ITEM_KEY:
4882 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
4883 key.objectid, key.type, key.offset);
4888 /* verify INODE_ITEM nlink/isize/nbytes */
4891 err |= LINK_COUNT_ERROR;
4892 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
4893 root->objectid, inode_id, nlink);
4897 * Just a warning, as dir inode nbytes is just an
4898 * instructive value.
4900 if (!IS_ALIGNED(nbytes, root->nodesize)) {
4901 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
4902 root->objectid, inode_id, root->nodesize);
4905 if (isize != size) {
4907 error("root %llu DIR INODE [%llu] size(%llu) not equal to %llu",
4908 root->objectid, inode_id, isize, size);
4911 if (nlink != refs) {
4912 err |= LINK_COUNT_ERROR;
4913 error("root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
4914 root->objectid, inode_id, nlink, refs);
4915 } else if (!nlink) {
4919 if (!nbytes && !no_holes && extent_end < isize) {
4920 err |= NBYTES_ERROR;
4921 error("root %llu INODE[%llu] size (%llu) should have a file extent hole",
4922 root->objectid, inode_id, isize);
4925 if (nbytes != extent_size) {
4926 err |= NBYTES_ERROR;
4927 error("root %llu INODE[%llu] nbytes(%llu) not equal to extent_size(%llu)",
4928 root->objectid, inode_id, nbytes, extent_size);
4935 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
4937 struct btrfs_path *path;
4938 struct btrfs_key key;
4942 path = btrfs_alloc_path();
4944 key.type = BTRFS_INODE_ITEM_KEY;
4947 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4952 err |= INODE_ITEM_MISSING;
4955 err |= check_inode_item(root, path, ext_ref);
4959 btrfs_free_path(path);
4964 * Iterate all item on the tree and call check_inode_item() to check.
4966 * @root: the root of the tree to be checked.
4967 * @ext_ref: the EXTENDED_IREF feature
4969 * Return 0 if no error found.
4970 * Return <0 for error.
4972 static int check_fs_root_v2(struct btrfs_root *root, unsigned int ext_ref)
4974 struct btrfs_path *path;
4975 struct node_refs nrefs;
4976 struct btrfs_root_item *root_item = &root->root_item;
4981 * We need to manually check the first inode item(256)
4982 * As the following traversal function will only start from
4983 * the first inode item in the leaf, if inode item(256) is missing
4984 * we will just skip it forever.
4986 ret = check_fs_first_inode(root, ext_ref);
4990 path = btrfs_alloc_path();
4994 memset(&nrefs, 0, sizeof(nrefs));
4995 level = btrfs_header_level(root->node);
4997 if (btrfs_root_refs(root_item) > 0 ||
4998 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4999 path->nodes[level] = root->node;
5000 path->slots[level] = 0;
5001 extent_buffer_get(root->node);
5003 struct btrfs_key key;
5005 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5006 level = root_item->drop_level;
5007 path->lowest_level = level;
5008 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5015 wret = walk_down_tree_v2(root, path, &level, &nrefs, ext_ref);
5021 wret = walk_up_tree_v2(root, path, &level);
5029 btrfs_free_path(path);
5034 * Find the relative ref for root_ref and root_backref.
5036 * @root: the root of the root tree.
5037 * @ref_key: the key of the root ref.
5039 * Return 0 if no error occurred.
5041 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
5042 struct extent_buffer *node, int slot)
5044 struct btrfs_path *path;
5045 struct btrfs_key key;
5046 struct btrfs_root_ref *ref;
5047 struct btrfs_root_ref *backref;
5048 char ref_name[BTRFS_NAME_LEN] = {0};
5049 char backref_name[BTRFS_NAME_LEN] = {0};
5055 u32 backref_namelen;
5060 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
5061 ref_dirid = btrfs_root_ref_dirid(node, ref);
5062 ref_seq = btrfs_root_ref_sequence(node, ref);
5063 ref_namelen = btrfs_root_ref_name_len(node, ref);
5065 if (ref_namelen <= BTRFS_NAME_LEN) {
5068 len = BTRFS_NAME_LEN;
5069 warning("%s[%llu %llu] ref_name too long",
5070 ref_key->type == BTRFS_ROOT_REF_KEY ?
5071 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
5074 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
5076 /* Find relative root_ref */
5077 key.objectid = ref_key->offset;
5078 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
5079 key.offset = ref_key->objectid;
5081 path = btrfs_alloc_path();
5082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5084 err |= ROOT_REF_MISSING;
5085 error("%s[%llu %llu] couldn't find relative ref",
5086 ref_key->type == BTRFS_ROOT_REF_KEY ?
5087 "ROOT_REF" : "ROOT_BACKREF",
5088 ref_key->objectid, ref_key->offset);
5092 backref = btrfs_item_ptr(path->nodes[0], path->slots[0],
5093 struct btrfs_root_ref);
5094 backref_dirid = btrfs_root_ref_dirid(path->nodes[0], backref);
5095 backref_seq = btrfs_root_ref_sequence(path->nodes[0], backref);
5096 backref_namelen = btrfs_root_ref_name_len(path->nodes[0], backref);
5098 if (backref_namelen <= BTRFS_NAME_LEN) {
5099 len = backref_namelen;
5101 len = BTRFS_NAME_LEN;
5102 warning("%s[%llu %llu] ref_name too long",
5103 key.type == BTRFS_ROOT_REF_KEY ?
5104 "ROOT_REF" : "ROOT_BACKREF",
5105 key.objectid, key.offset);
5107 read_extent_buffer(path->nodes[0], backref_name,
5108 (unsigned long)(backref + 1), len);
5110 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
5111 ref_namelen != backref_namelen ||
5112 strncmp(ref_name, backref_name, len)) {
5113 err |= ROOT_REF_MISMATCH;
5114 error("%s[%llu %llu] mismatch relative ref",
5115 ref_key->type == BTRFS_ROOT_REF_KEY ?
5116 "ROOT_REF" : "ROOT_BACKREF",
5117 ref_key->objectid, ref_key->offset);
5120 btrfs_free_path(path);
5125 * Check all fs/file tree in low_memory mode.
5127 * 1. for fs tree root item, call check_fs_root_v2()
5128 * 2. for fs tree root ref/backref, call check_root_ref()
5130 * Return 0 if no error occurred.
5132 static int check_fs_roots_v2(struct btrfs_fs_info *fs_info)
5134 struct btrfs_root *tree_root = fs_info->tree_root;
5135 struct btrfs_root *cur_root = NULL;
5136 struct btrfs_path *path;
5137 struct btrfs_key key;
5138 struct extent_buffer *node;
5139 unsigned int ext_ref;
5144 ext_ref = btrfs_fs_incompat(fs_info,
5145 BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF);
5147 path = btrfs_alloc_path();
5151 key.objectid = BTRFS_FS_TREE_OBJECTID;
5153 key.type = BTRFS_ROOT_ITEM_KEY;
5155 ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
5159 } else if (ret > 0) {
5165 node = path->nodes[0];
5166 slot = path->slots[0];
5167 btrfs_item_key_to_cpu(node, &key, slot);
5168 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
5170 if (key.type == BTRFS_ROOT_ITEM_KEY &&
5171 fs_root_objectid(key.objectid)) {
5172 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
5173 cur_root = btrfs_read_fs_root_no_cache(fs_info,
5176 key.offset = (u64)-1;
5177 cur_root = btrfs_read_fs_root(fs_info, &key);
5180 if (IS_ERR(cur_root)) {
5181 error("Fail to read fs/subvol tree: %lld",
5187 ret = check_fs_root_v2(cur_root, ext_ref);
5190 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5191 btrfs_free_fs_root(cur_root);
5192 } else if (key.type == BTRFS_ROOT_REF_KEY ||
5193 key.type == BTRFS_ROOT_BACKREF_KEY) {
5194 ret = check_root_ref(tree_root, &key, node, slot);
5198 ret = btrfs_next_item(tree_root, path);
5208 btrfs_free_path(path);
5212 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
5214 struct list_head *cur = rec->backrefs.next;
5215 struct extent_backref *back;
5216 struct tree_backref *tback;
5217 struct data_backref *dback;
5221 while(cur != &rec->backrefs) {
5222 back = to_extent_backref(cur);
5224 if (!back->found_extent_tree) {
5228 if (back->is_data) {
5229 dback = to_data_backref(back);
5230 fprintf(stderr, "Backref %llu %s %llu"
5231 " owner %llu offset %llu num_refs %lu"
5232 " not found in extent tree\n",
5233 (unsigned long long)rec->start,
5234 back->full_backref ?
5236 back->full_backref ?
5237 (unsigned long long)dback->parent:
5238 (unsigned long long)dback->root,
5239 (unsigned long long)dback->owner,
5240 (unsigned long long)dback->offset,
5241 (unsigned long)dback->num_refs);
5243 tback = to_tree_backref(back);
5244 fprintf(stderr, "Backref %llu parent %llu"
5245 " root %llu not found in extent tree\n",
5246 (unsigned long long)rec->start,
5247 (unsigned long long)tback->parent,
5248 (unsigned long long)tback->root);
5251 if (!back->is_data && !back->found_ref) {
5255 tback = to_tree_backref(back);
5256 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
5257 (unsigned long long)rec->start,
5258 back->full_backref ? "parent" : "root",
5259 back->full_backref ?
5260 (unsigned long long)tback->parent :
5261 (unsigned long long)tback->root, back);
5263 if (back->is_data) {
5264 dback = to_data_backref(back);
5265 if (dback->found_ref != dback->num_refs) {
5269 fprintf(stderr, "Incorrect local backref count"
5270 " on %llu %s %llu owner %llu"
5271 " offset %llu found %u wanted %u back %p\n",
5272 (unsigned long long)rec->start,
5273 back->full_backref ?
5275 back->full_backref ?
5276 (unsigned long long)dback->parent:
5277 (unsigned long long)dback->root,
5278 (unsigned long long)dback->owner,
5279 (unsigned long long)dback->offset,
5280 dback->found_ref, dback->num_refs, back);
5282 if (dback->disk_bytenr != rec->start) {
5286 fprintf(stderr, "Backref disk bytenr does not"
5287 " match extent record, bytenr=%llu, "
5288 "ref bytenr=%llu\n",
5289 (unsigned long long)rec->start,
5290 (unsigned long long)dback->disk_bytenr);
5293 if (dback->bytes != rec->nr) {
5297 fprintf(stderr, "Backref bytes do not match "
5298 "extent backref, bytenr=%llu, ref "
5299 "bytes=%llu, backref bytes=%llu\n",
5300 (unsigned long long)rec->start,
5301 (unsigned long long)rec->nr,
5302 (unsigned long long)dback->bytes);
5305 if (!back->is_data) {
5308 dback = to_data_backref(back);
5309 found += dback->found_ref;
5312 if (found != rec->refs) {
5316 fprintf(stderr, "Incorrect global backref count "
5317 "on %llu found %llu wanted %llu\n",
5318 (unsigned long long)rec->start,
5319 (unsigned long long)found,
5320 (unsigned long long)rec->refs);
5326 static int free_all_extent_backrefs(struct extent_record *rec)
5328 struct extent_backref *back;
5329 struct list_head *cur;
5330 while (!list_empty(&rec->backrefs)) {
5331 cur = rec->backrefs.next;
5332 back = to_extent_backref(cur);
5339 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
5340 struct cache_tree *extent_cache)
5342 struct cache_extent *cache;
5343 struct extent_record *rec;
5346 cache = first_cache_extent(extent_cache);
5349 rec = container_of(cache, struct extent_record, cache);
5350 remove_cache_extent(extent_cache, cache);
5351 free_all_extent_backrefs(rec);
5356 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
5357 struct extent_record *rec)
5359 if (rec->content_checked && rec->owner_ref_checked &&
5360 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
5361 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0) &&
5362 !rec->bad_full_backref && !rec->crossing_stripes &&
5363 !rec->wrong_chunk_type) {
5364 remove_cache_extent(extent_cache, &rec->cache);
5365 free_all_extent_backrefs(rec);
5366 list_del_init(&rec->list);
5372 static int check_owner_ref(struct btrfs_root *root,
5373 struct extent_record *rec,
5374 struct extent_buffer *buf)
5376 struct extent_backref *node;
5377 struct tree_backref *back;
5378 struct btrfs_root *ref_root;
5379 struct btrfs_key key;
5380 struct btrfs_path path;
5381 struct extent_buffer *parent;
5386 list_for_each_entry(node, &rec->backrefs, list) {
5389 if (!node->found_ref)
5391 if (node->full_backref)
5393 back = to_tree_backref(node);
5394 if (btrfs_header_owner(buf) == back->root)
5397 BUG_ON(rec->is_root);
5399 /* try to find the block by search corresponding fs tree */
5400 key.objectid = btrfs_header_owner(buf);
5401 key.type = BTRFS_ROOT_ITEM_KEY;
5402 key.offset = (u64)-1;
5404 ref_root = btrfs_read_fs_root(root->fs_info, &key);
5405 if (IS_ERR(ref_root))
5408 level = btrfs_header_level(buf);
5410 btrfs_item_key_to_cpu(buf, &key, 0);
5412 btrfs_node_key_to_cpu(buf, &key, 0);
5414 btrfs_init_path(&path);
5415 path.lowest_level = level + 1;
5416 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
5420 parent = path.nodes[level + 1];
5421 if (parent && buf->start == btrfs_node_blockptr(parent,
5422 path.slots[level + 1]))
5425 btrfs_release_path(&path);
5426 return found ? 0 : 1;
5429 static int is_extent_tree_record(struct extent_record *rec)
5431 struct list_head *cur = rec->backrefs.next;
5432 struct extent_backref *node;
5433 struct tree_backref *back;
5436 while(cur != &rec->backrefs) {
5437 node = to_extent_backref(cur);
5441 back = to_tree_backref(node);
5442 if (node->full_backref)
5444 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
5451 static int record_bad_block_io(struct btrfs_fs_info *info,
5452 struct cache_tree *extent_cache,
5455 struct extent_record *rec;
5456 struct cache_extent *cache;
5457 struct btrfs_key key;
5459 cache = lookup_cache_extent(extent_cache, start, len);
5463 rec = container_of(cache, struct extent_record, cache);
5464 if (!is_extent_tree_record(rec))
5467 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
5468 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
5471 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
5472 struct extent_buffer *buf, int slot)
5474 if (btrfs_header_level(buf)) {
5475 struct btrfs_key_ptr ptr1, ptr2;
5477 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
5478 sizeof(struct btrfs_key_ptr));
5479 read_extent_buffer(buf, &ptr2,
5480 btrfs_node_key_ptr_offset(slot + 1),
5481 sizeof(struct btrfs_key_ptr));
5482 write_extent_buffer(buf, &ptr1,
5483 btrfs_node_key_ptr_offset(slot + 1),
5484 sizeof(struct btrfs_key_ptr));
5485 write_extent_buffer(buf, &ptr2,
5486 btrfs_node_key_ptr_offset(slot),
5487 sizeof(struct btrfs_key_ptr));
5489 struct btrfs_disk_key key;
5490 btrfs_node_key(buf, &key, 0);
5491 btrfs_fixup_low_keys(root, path, &key,
5492 btrfs_header_level(buf) + 1);
5495 struct btrfs_item *item1, *item2;
5496 struct btrfs_key k1, k2;
5497 char *item1_data, *item2_data;
5498 u32 item1_offset, item2_offset, item1_size, item2_size;
5500 item1 = btrfs_item_nr(slot);
5501 item2 = btrfs_item_nr(slot + 1);
5502 btrfs_item_key_to_cpu(buf, &k1, slot);
5503 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
5504 item1_offset = btrfs_item_offset(buf, item1);
5505 item2_offset = btrfs_item_offset(buf, item2);
5506 item1_size = btrfs_item_size(buf, item1);
5507 item2_size = btrfs_item_size(buf, item2);
5509 item1_data = malloc(item1_size);
5512 item2_data = malloc(item2_size);
5518 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
5519 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
5521 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
5522 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
5526 btrfs_set_item_offset(buf, item1, item2_offset);
5527 btrfs_set_item_offset(buf, item2, item1_offset);
5528 btrfs_set_item_size(buf, item1, item2_size);
5529 btrfs_set_item_size(buf, item2, item1_size);
5531 path->slots[0] = slot;
5532 btrfs_set_item_key_unsafe(root, path, &k2);
5533 path->slots[0] = slot + 1;
5534 btrfs_set_item_key_unsafe(root, path, &k1);
5539 static int fix_key_order(struct btrfs_trans_handle *trans,
5540 struct btrfs_root *root,
5541 struct btrfs_path *path)
5543 struct extent_buffer *buf;
5544 struct btrfs_key k1, k2;
5546 int level = path->lowest_level;
5549 buf = path->nodes[level];
5550 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
5552 btrfs_node_key_to_cpu(buf, &k1, i);
5553 btrfs_node_key_to_cpu(buf, &k2, i + 1);
5555 btrfs_item_key_to_cpu(buf, &k1, i);
5556 btrfs_item_key_to_cpu(buf, &k2, i + 1);
5558 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
5560 ret = swap_values(root, path, buf, i);
5563 btrfs_mark_buffer_dirty(buf);
5569 static int delete_bogus_item(struct btrfs_trans_handle *trans,
5570 struct btrfs_root *root,
5571 struct btrfs_path *path,
5572 struct extent_buffer *buf, int slot)
5574 struct btrfs_key key;
5575 int nritems = btrfs_header_nritems(buf);
5577 btrfs_item_key_to_cpu(buf, &key, slot);
5579 /* These are all the keys we can deal with missing. */
5580 if (key.type != BTRFS_DIR_INDEX_KEY &&
5581 key.type != BTRFS_EXTENT_ITEM_KEY &&
5582 key.type != BTRFS_METADATA_ITEM_KEY &&
5583 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5584 key.type != BTRFS_EXTENT_DATA_REF_KEY)
5587 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
5588 (unsigned long long)key.objectid, key.type,
5589 (unsigned long long)key.offset, slot, buf->start);
5590 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
5591 btrfs_item_nr_offset(slot + 1),
5592 sizeof(struct btrfs_item) *
5593 (nritems - slot - 1));
5594 btrfs_set_header_nritems(buf, nritems - 1);
5596 struct btrfs_disk_key disk_key;
5598 btrfs_item_key(buf, &disk_key, 0);
5599 btrfs_fixup_low_keys(root, path, &disk_key, 1);
5601 btrfs_mark_buffer_dirty(buf);
5605 static int fix_item_offset(struct btrfs_trans_handle *trans,
5606 struct btrfs_root *root,
5607 struct btrfs_path *path)
5609 struct extent_buffer *buf;
5613 /* We should only get this for leaves */
5614 BUG_ON(path->lowest_level);
5615 buf = path->nodes[0];
5617 for (i = 0; i < btrfs_header_nritems(buf); i++) {
5618 unsigned int shift = 0, offset;
5620 if (i == 0 && btrfs_item_end_nr(buf, i) !=
5621 BTRFS_LEAF_DATA_SIZE(root)) {
5622 if (btrfs_item_end_nr(buf, i) >
5623 BTRFS_LEAF_DATA_SIZE(root)) {
5624 ret = delete_bogus_item(trans, root, path,
5628 fprintf(stderr, "item is off the end of the "
5629 "leaf, can't fix\n");
5633 shift = BTRFS_LEAF_DATA_SIZE(root) -
5634 btrfs_item_end_nr(buf, i);
5635 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
5636 btrfs_item_offset_nr(buf, i - 1)) {
5637 if (btrfs_item_end_nr(buf, i) >
5638 btrfs_item_offset_nr(buf, i - 1)) {
5639 ret = delete_bogus_item(trans, root, path,
5643 fprintf(stderr, "items overlap, can't fix\n");
5647 shift = btrfs_item_offset_nr(buf, i - 1) -
5648 btrfs_item_end_nr(buf, i);
5653 printf("Shifting item nr %d by %u bytes in block %llu\n",
5654 i, shift, (unsigned long long)buf->start);
5655 offset = btrfs_item_offset_nr(buf, i);
5656 memmove_extent_buffer(buf,
5657 btrfs_leaf_data(buf) + offset + shift,
5658 btrfs_leaf_data(buf) + offset,
5659 btrfs_item_size_nr(buf, i));
5660 btrfs_set_item_offset(buf, btrfs_item_nr(i),
5662 btrfs_mark_buffer_dirty(buf);
5666 * We may have moved things, in which case we want to exit so we don't
5667 * write those changes out. Once we have proper abort functionality in
5668 * progs this can be changed to something nicer.
5675 * Attempt to fix basic block failures. If we can't fix it for whatever reason
5676 * then just return -EIO.
5678 static int try_to_fix_bad_block(struct btrfs_root *root,
5679 struct extent_buffer *buf,
5680 enum btrfs_tree_block_status status)
5682 struct btrfs_trans_handle *trans;
5683 struct ulist *roots;
5684 struct ulist_node *node;
5685 struct btrfs_root *search_root;
5686 struct btrfs_path path;
5687 struct ulist_iterator iter;
5688 struct btrfs_key root_key, key;
5691 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
5692 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5695 ret = btrfs_find_all_roots(NULL, root->fs_info, buf->start, 0, &roots);
5699 btrfs_init_path(&path);
5700 ULIST_ITER_INIT(&iter);
5701 while ((node = ulist_next(roots, &iter))) {
5702 root_key.objectid = node->val;
5703 root_key.type = BTRFS_ROOT_ITEM_KEY;
5704 root_key.offset = (u64)-1;
5706 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
5713 trans = btrfs_start_transaction(search_root, 0);
5714 if (IS_ERR(trans)) {
5715 ret = PTR_ERR(trans);
5719 path.lowest_level = btrfs_header_level(buf);
5720 path.skip_check_block = 1;
5721 if (path.lowest_level)
5722 btrfs_node_key_to_cpu(buf, &key, 0);
5724 btrfs_item_key_to_cpu(buf, &key, 0);
5725 ret = btrfs_search_slot(trans, search_root, &key, &path, 0, 1);
5728 btrfs_commit_transaction(trans, search_root);
5731 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
5732 ret = fix_key_order(trans, search_root, &path);
5733 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5734 ret = fix_item_offset(trans, search_root, &path);
5736 btrfs_commit_transaction(trans, search_root);
5739 btrfs_release_path(&path);
5740 btrfs_commit_transaction(trans, search_root);
5743 btrfs_release_path(&path);
5747 static int check_block(struct btrfs_root *root,
5748 struct cache_tree *extent_cache,
5749 struct extent_buffer *buf, u64 flags)
5751 struct extent_record *rec;
5752 struct cache_extent *cache;
5753 struct btrfs_key key;
5754 enum btrfs_tree_block_status status;
5758 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
5761 rec = container_of(cache, struct extent_record, cache);
5762 rec->generation = btrfs_header_generation(buf);
5764 level = btrfs_header_level(buf);
5765 if (btrfs_header_nritems(buf) > 0) {
5768 btrfs_item_key_to_cpu(buf, &key, 0);
5770 btrfs_node_key_to_cpu(buf, &key, 0);
5772 rec->info_objectid = key.objectid;
5774 rec->info_level = level;
5776 if (btrfs_is_leaf(buf))
5777 status = btrfs_check_leaf(root, &rec->parent_key, buf);
5779 status = btrfs_check_node(root, &rec->parent_key, buf);
5781 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5783 status = try_to_fix_bad_block(root, buf, status);
5784 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5786 fprintf(stderr, "bad block %llu\n",
5787 (unsigned long long)buf->start);
5790 * Signal to callers we need to start the scan over
5791 * again since we'll have cowed blocks.
5796 rec->content_checked = 1;
5797 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5798 rec->owner_ref_checked = 1;
5800 ret = check_owner_ref(root, rec, buf);
5802 rec->owner_ref_checked = 1;
5806 maybe_free_extent_rec(extent_cache, rec);
5810 static struct tree_backref *find_tree_backref(struct extent_record *rec,
5811 u64 parent, u64 root)
5813 struct list_head *cur = rec->backrefs.next;
5814 struct extent_backref *node;
5815 struct tree_backref *back;
5817 while(cur != &rec->backrefs) {
5818 node = to_extent_backref(cur);
5822 back = to_tree_backref(node);
5824 if (!node->full_backref)
5826 if (parent == back->parent)
5829 if (node->full_backref)
5831 if (back->root == root)
5838 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
5839 u64 parent, u64 root)
5841 struct tree_backref *ref = malloc(sizeof(*ref));
5845 memset(&ref->node, 0, sizeof(ref->node));
5847 ref->parent = parent;
5848 ref->node.full_backref = 1;
5851 ref->node.full_backref = 0;
5853 list_add_tail(&ref->node.list, &rec->backrefs);
5858 static struct data_backref *find_data_backref(struct extent_record *rec,
5859 u64 parent, u64 root,
5860 u64 owner, u64 offset,
5862 u64 disk_bytenr, u64 bytes)
5864 struct list_head *cur = rec->backrefs.next;
5865 struct extent_backref *node;
5866 struct data_backref *back;
5868 while(cur != &rec->backrefs) {
5869 node = to_extent_backref(cur);
5873 back = to_data_backref(node);
5875 if (!node->full_backref)
5877 if (parent == back->parent)
5880 if (node->full_backref)
5882 if (back->root == root && back->owner == owner &&
5883 back->offset == offset) {
5884 if (found_ref && node->found_ref &&
5885 (back->bytes != bytes ||
5886 back->disk_bytenr != disk_bytenr))
5895 static struct data_backref *alloc_data_backref(struct extent_record *rec,
5896 u64 parent, u64 root,
5897 u64 owner, u64 offset,
5900 struct data_backref *ref = malloc(sizeof(*ref));
5904 memset(&ref->node, 0, sizeof(ref->node));
5905 ref->node.is_data = 1;
5908 ref->parent = parent;
5911 ref->node.full_backref = 1;
5915 ref->offset = offset;
5916 ref->node.full_backref = 0;
5918 ref->bytes = max_size;
5921 list_add_tail(&ref->node.list, &rec->backrefs);
5922 if (max_size > rec->max_size)
5923 rec->max_size = max_size;
5927 /* Check if the type of extent matches with its chunk */
5928 static void check_extent_type(struct extent_record *rec)
5930 struct btrfs_block_group_cache *bg_cache;
5932 bg_cache = btrfs_lookup_first_block_group(global_info, rec->start);
5936 /* data extent, check chunk directly*/
5937 if (!rec->metadata) {
5938 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
5939 rec->wrong_chunk_type = 1;
5943 /* metadata extent, check the obvious case first */
5944 if (!(bg_cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
5945 BTRFS_BLOCK_GROUP_METADATA))) {
5946 rec->wrong_chunk_type = 1;
5951 * Check SYSTEM extent, as it's also marked as metadata, we can only
5952 * make sure it's a SYSTEM extent by its backref
5954 if (!list_empty(&rec->backrefs)) {
5955 struct extent_backref *node;
5956 struct tree_backref *tback;
5959 node = to_extent_backref(rec->backrefs.next);
5960 if (node->is_data) {
5961 /* tree block shouldn't have data backref */
5962 rec->wrong_chunk_type = 1;
5965 tback = container_of(node, struct tree_backref, node);
5967 if (tback->root == BTRFS_CHUNK_TREE_OBJECTID)
5968 bg_type = BTRFS_BLOCK_GROUP_SYSTEM;
5970 bg_type = BTRFS_BLOCK_GROUP_METADATA;
5971 if (!(bg_cache->flags & bg_type))
5972 rec->wrong_chunk_type = 1;
5977 * Allocate a new extent record, fill default values from @tmpl and insert int
5978 * @extent_cache. Caller is supposed to make sure the [start,nr) is not in
5979 * the cache, otherwise it fails.
5981 static int add_extent_rec_nolookup(struct cache_tree *extent_cache,
5982 struct extent_record *tmpl)
5984 struct extent_record *rec;
5987 rec = malloc(sizeof(*rec));
5990 rec->start = tmpl->start;
5991 rec->max_size = tmpl->max_size;
5992 rec->nr = max(tmpl->nr, tmpl->max_size);
5993 rec->found_rec = tmpl->found_rec;
5994 rec->content_checked = tmpl->content_checked;
5995 rec->owner_ref_checked = tmpl->owner_ref_checked;
5996 rec->num_duplicates = 0;
5997 rec->metadata = tmpl->metadata;
5998 rec->flag_block_full_backref = FLAG_UNSET;
5999 rec->bad_full_backref = 0;
6000 rec->crossing_stripes = 0;
6001 rec->wrong_chunk_type = 0;
6002 rec->is_root = tmpl->is_root;
6003 rec->refs = tmpl->refs;
6004 rec->extent_item_refs = tmpl->extent_item_refs;
6005 rec->parent_generation = tmpl->parent_generation;
6006 INIT_LIST_HEAD(&rec->backrefs);
6007 INIT_LIST_HEAD(&rec->dups);
6008 INIT_LIST_HEAD(&rec->list);
6009 memcpy(&rec->parent_key, &tmpl->parent_key, sizeof(tmpl->parent_key));
6010 rec->cache.start = tmpl->start;
6011 rec->cache.size = tmpl->nr;
6012 ret = insert_cache_extent(extent_cache, &rec->cache);
6017 bytes_used += rec->nr;
6020 rec->crossing_stripes = check_crossing_stripes(global_info,
6021 rec->start, global_info->tree_root->nodesize);
6022 check_extent_type(rec);
6027 * Lookup and modify an extent, some values of @tmpl are interpreted verbatim,
6029 * - refs - if found, increase refs
6030 * - is_root - if found, set
6031 * - content_checked - if found, set
6032 * - owner_ref_checked - if found, set
6034 * If not found, create a new one, initialize and insert.
6036 static int add_extent_rec(struct cache_tree *extent_cache,
6037 struct extent_record *tmpl)
6039 struct extent_record *rec;
6040 struct cache_extent *cache;
6044 cache = lookup_cache_extent(extent_cache, tmpl->start, tmpl->nr);
6046 rec = container_of(cache, struct extent_record, cache);
6050 rec->nr = max(tmpl->nr, tmpl->max_size);
6053 * We need to make sure to reset nr to whatever the extent
6054 * record says was the real size, this way we can compare it to
6057 if (tmpl->found_rec) {
6058 if (tmpl->start != rec->start || rec->found_rec) {
6059 struct extent_record *tmp;
6062 if (list_empty(&rec->list))
6063 list_add_tail(&rec->list,
6064 &duplicate_extents);
6067 * We have to do this song and dance in case we
6068 * find an extent record that falls inside of
6069 * our current extent record but does not have
6070 * the same objectid.
6072 tmp = malloc(sizeof(*tmp));
6075 tmp->start = tmpl->start;
6076 tmp->max_size = tmpl->max_size;
6079 tmp->metadata = tmpl->metadata;
6080 tmp->extent_item_refs = tmpl->extent_item_refs;
6081 INIT_LIST_HEAD(&tmp->list);
6082 list_add_tail(&tmp->list, &rec->dups);
6083 rec->num_duplicates++;
6090 if (tmpl->extent_item_refs && !dup) {
6091 if (rec->extent_item_refs) {
6092 fprintf(stderr, "block %llu rec "
6093 "extent_item_refs %llu, passed %llu\n",
6094 (unsigned long long)tmpl->start,
6095 (unsigned long long)
6096 rec->extent_item_refs,
6097 (unsigned long long)tmpl->extent_item_refs);
6099 rec->extent_item_refs = tmpl->extent_item_refs;
6103 if (tmpl->content_checked)
6104 rec->content_checked = 1;
6105 if (tmpl->owner_ref_checked)
6106 rec->owner_ref_checked = 1;
6107 memcpy(&rec->parent_key, &tmpl->parent_key,
6108 sizeof(tmpl->parent_key));
6109 if (tmpl->parent_generation)
6110 rec->parent_generation = tmpl->parent_generation;
6111 if (rec->max_size < tmpl->max_size)
6112 rec->max_size = tmpl->max_size;
6115 * A metadata extent can't cross stripe_len boundary, otherwise
6116 * kernel scrub won't be able to handle it.
6117 * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
6121 rec->crossing_stripes = check_crossing_stripes(
6122 global_info, rec->start,
6123 global_info->tree_root->nodesize);
6124 check_extent_type(rec);
6125 maybe_free_extent_rec(extent_cache, rec);
6129 ret = add_extent_rec_nolookup(extent_cache, tmpl);
6134 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
6135 u64 parent, u64 root, int found_ref)
6137 struct extent_record *rec;
6138 struct tree_backref *back;
6139 struct cache_extent *cache;
6142 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6144 struct extent_record tmpl;
6146 memset(&tmpl, 0, sizeof(tmpl));
6147 tmpl.start = bytenr;
6151 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6155 /* really a bug in cache_extent implement now */
6156 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6161 rec = container_of(cache, struct extent_record, cache);
6162 if (rec->start != bytenr) {
6164 * Several cause, from unaligned bytenr to over lapping extents
6169 back = find_tree_backref(rec, parent, root);
6171 back = alloc_tree_backref(rec, parent, root);
6177 if (back->node.found_ref) {
6178 fprintf(stderr, "Extent back ref already exists "
6179 "for %llu parent %llu root %llu \n",
6180 (unsigned long long)bytenr,
6181 (unsigned long long)parent,
6182 (unsigned long long)root);
6184 back->node.found_ref = 1;
6186 if (back->node.found_extent_tree) {
6187 fprintf(stderr, "Extent back ref already exists "
6188 "for %llu parent %llu root %llu \n",
6189 (unsigned long long)bytenr,
6190 (unsigned long long)parent,
6191 (unsigned long long)root);
6193 back->node.found_extent_tree = 1;
6195 check_extent_type(rec);
6196 maybe_free_extent_rec(extent_cache, rec);
6200 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
6201 u64 parent, u64 root, u64 owner, u64 offset,
6202 u32 num_refs, int found_ref, u64 max_size)
6204 struct extent_record *rec;
6205 struct data_backref *back;
6206 struct cache_extent *cache;
6209 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6211 struct extent_record tmpl;
6213 memset(&tmpl, 0, sizeof(tmpl));
6214 tmpl.start = bytenr;
6216 tmpl.max_size = max_size;
6218 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6222 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6227 rec = container_of(cache, struct extent_record, cache);
6228 if (rec->max_size < max_size)
6229 rec->max_size = max_size;
6232 * If found_ref is set then max_size is the real size and must match the
6233 * existing refs. So if we have already found a ref then we need to
6234 * make sure that this ref matches the existing one, otherwise we need
6235 * to add a new backref so we can notice that the backrefs don't match
6236 * and we need to figure out who is telling the truth. This is to
6237 * account for that awful fsync bug I introduced where we'd end up with
6238 * a btrfs_file_extent_item that would have its length include multiple
6239 * prealloc extents or point inside of a prealloc extent.
6241 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
6244 back = alloc_data_backref(rec, parent, root, owner, offset,
6250 BUG_ON(num_refs != 1);
6251 if (back->node.found_ref)
6252 BUG_ON(back->bytes != max_size);
6253 back->node.found_ref = 1;
6254 back->found_ref += 1;
6255 back->bytes = max_size;
6256 back->disk_bytenr = bytenr;
6258 rec->content_checked = 1;
6259 rec->owner_ref_checked = 1;
6261 if (back->node.found_extent_tree) {
6262 fprintf(stderr, "Extent back ref already exists "
6263 "for %llu parent %llu root %llu "
6264 "owner %llu offset %llu num_refs %lu\n",
6265 (unsigned long long)bytenr,
6266 (unsigned long long)parent,
6267 (unsigned long long)root,
6268 (unsigned long long)owner,
6269 (unsigned long long)offset,
6270 (unsigned long)num_refs);
6272 back->num_refs = num_refs;
6273 back->node.found_extent_tree = 1;
6275 maybe_free_extent_rec(extent_cache, rec);
6279 static int add_pending(struct cache_tree *pending,
6280 struct cache_tree *seen, u64 bytenr, u32 size)
6283 ret = add_cache_extent(seen, bytenr, size);
6286 add_cache_extent(pending, bytenr, size);
6290 static int pick_next_pending(struct cache_tree *pending,
6291 struct cache_tree *reada,
6292 struct cache_tree *nodes,
6293 u64 last, struct block_info *bits, int bits_nr,
6296 unsigned long node_start = last;
6297 struct cache_extent *cache;
6300 cache = search_cache_extent(reada, 0);
6302 bits[0].start = cache->start;
6303 bits[0].size = cache->size;
6308 if (node_start > 32768)
6309 node_start -= 32768;
6311 cache = search_cache_extent(nodes, node_start);
6313 cache = search_cache_extent(nodes, 0);
6316 cache = search_cache_extent(pending, 0);
6321 bits[ret].start = cache->start;
6322 bits[ret].size = cache->size;
6323 cache = next_cache_extent(cache);
6325 } while (cache && ret < bits_nr);
6331 bits[ret].start = cache->start;
6332 bits[ret].size = cache->size;
6333 cache = next_cache_extent(cache);
6335 } while (cache && ret < bits_nr);
6337 if (bits_nr - ret > 8) {
6338 u64 lookup = bits[0].start + bits[0].size;
6339 struct cache_extent *next;
6340 next = search_cache_extent(pending, lookup);
6342 if (next->start - lookup > 32768)
6344 bits[ret].start = next->start;
6345 bits[ret].size = next->size;
6346 lookup = next->start + next->size;
6350 next = next_cache_extent(next);
6358 static void free_chunk_record(struct cache_extent *cache)
6360 struct chunk_record *rec;
6362 rec = container_of(cache, struct chunk_record, cache);
6363 list_del_init(&rec->list);
6364 list_del_init(&rec->dextents);
6368 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
6370 cache_tree_free_extents(chunk_cache, free_chunk_record);
6373 static void free_device_record(struct rb_node *node)
6375 struct device_record *rec;
6377 rec = container_of(node, struct device_record, node);
6381 FREE_RB_BASED_TREE(device_cache, free_device_record);
6383 int insert_block_group_record(struct block_group_tree *tree,
6384 struct block_group_record *bg_rec)
6388 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
6392 list_add_tail(&bg_rec->list, &tree->block_groups);
6396 static void free_block_group_record(struct cache_extent *cache)
6398 struct block_group_record *rec;
6400 rec = container_of(cache, struct block_group_record, cache);
6401 list_del_init(&rec->list);
6405 void free_block_group_tree(struct block_group_tree *tree)
6407 cache_tree_free_extents(&tree->tree, free_block_group_record);
6410 int insert_device_extent_record(struct device_extent_tree *tree,
6411 struct device_extent_record *de_rec)
6416 * Device extent is a bit different from the other extents, because
6417 * the extents which belong to the different devices may have the
6418 * same start and size, so we need use the special extent cache
6419 * search/insert functions.
6421 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
6425 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
6426 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
6430 static void free_device_extent_record(struct cache_extent *cache)
6432 struct device_extent_record *rec;
6434 rec = container_of(cache, struct device_extent_record, cache);
6435 if (!list_empty(&rec->chunk_list))
6436 list_del_init(&rec->chunk_list);
6437 if (!list_empty(&rec->device_list))
6438 list_del_init(&rec->device_list);
6442 void free_device_extent_tree(struct device_extent_tree *tree)
6444 cache_tree_free_extents(&tree->tree, free_device_extent_record);
6447 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6448 static int process_extent_ref_v0(struct cache_tree *extent_cache,
6449 struct extent_buffer *leaf, int slot)
6451 struct btrfs_extent_ref_v0 *ref0;
6452 struct btrfs_key key;
6455 btrfs_item_key_to_cpu(leaf, &key, slot);
6456 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
6457 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
6458 ret = add_tree_backref(extent_cache, key.objectid, key.offset,
6461 ret = add_data_backref(extent_cache, key.objectid, key.offset,
6462 0, 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
6468 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
6469 struct btrfs_key *key,
6472 struct btrfs_chunk *ptr;
6473 struct chunk_record *rec;
6476 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6477 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
6479 rec = calloc(1, btrfs_chunk_record_size(num_stripes));
6481 fprintf(stderr, "memory allocation failed\n");
6485 INIT_LIST_HEAD(&rec->list);
6486 INIT_LIST_HEAD(&rec->dextents);
6489 rec->cache.start = key->offset;
6490 rec->cache.size = btrfs_chunk_length(leaf, ptr);
6492 rec->generation = btrfs_header_generation(leaf);
6494 rec->objectid = key->objectid;
6495 rec->type = key->type;
6496 rec->offset = key->offset;
6498 rec->length = rec->cache.size;
6499 rec->owner = btrfs_chunk_owner(leaf, ptr);
6500 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
6501 rec->type_flags = btrfs_chunk_type(leaf, ptr);
6502 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
6503 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
6504 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
6505 rec->num_stripes = num_stripes;
6506 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
6508 for (i = 0; i < rec->num_stripes; ++i) {
6509 rec->stripes[i].devid =
6510 btrfs_stripe_devid_nr(leaf, ptr, i);
6511 rec->stripes[i].offset =
6512 btrfs_stripe_offset_nr(leaf, ptr, i);
6513 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
6514 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
6521 static int process_chunk_item(struct cache_tree *chunk_cache,
6522 struct btrfs_key *key, struct extent_buffer *eb,
6525 struct chunk_record *rec;
6526 struct btrfs_chunk *chunk;
6529 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
6531 * Do extra check for this chunk item,
6533 * It's still possible one can craft a leaf with CHUNK_ITEM, with
6534 * wrong onwer(3) out of chunk tree, to pass both chunk tree check
6535 * and owner<->key_type check.
6537 ret = btrfs_check_chunk_valid(global_info->tree_root, eb, chunk, slot,
6540 error("chunk(%llu, %llu) is not valid, ignore it",
6541 key->offset, btrfs_chunk_length(eb, chunk));
6544 rec = btrfs_new_chunk_record(eb, key, slot);
6545 ret = insert_cache_extent(chunk_cache, &rec->cache);
6547 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
6548 rec->offset, rec->length);
6555 static int process_device_item(struct rb_root *dev_cache,
6556 struct btrfs_key *key, struct extent_buffer *eb, int slot)
6558 struct btrfs_dev_item *ptr;
6559 struct device_record *rec;
6562 ptr = btrfs_item_ptr(eb,
6563 slot, struct btrfs_dev_item);
6565 rec = malloc(sizeof(*rec));
6567 fprintf(stderr, "memory allocation failed\n");
6571 rec->devid = key->offset;
6572 rec->generation = btrfs_header_generation(eb);
6574 rec->objectid = key->objectid;
6575 rec->type = key->type;
6576 rec->offset = key->offset;
6578 rec->devid = btrfs_device_id(eb, ptr);
6579 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
6580 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
6582 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
6584 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
6591 struct block_group_record *
6592 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
6595 struct btrfs_block_group_item *ptr;
6596 struct block_group_record *rec;
6598 rec = calloc(1, sizeof(*rec));
6600 fprintf(stderr, "memory allocation failed\n");
6604 rec->cache.start = key->objectid;
6605 rec->cache.size = key->offset;
6607 rec->generation = btrfs_header_generation(leaf);
6609 rec->objectid = key->objectid;
6610 rec->type = key->type;
6611 rec->offset = key->offset;
6613 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
6614 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
6616 INIT_LIST_HEAD(&rec->list);
6621 static int process_block_group_item(struct block_group_tree *block_group_cache,
6622 struct btrfs_key *key,
6623 struct extent_buffer *eb, int slot)
6625 struct block_group_record *rec;
6628 rec = btrfs_new_block_group_record(eb, key, slot);
6629 ret = insert_block_group_record(block_group_cache, rec);
6631 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
6632 rec->objectid, rec->offset);
6639 struct device_extent_record *
6640 btrfs_new_device_extent_record(struct extent_buffer *leaf,
6641 struct btrfs_key *key, int slot)
6643 struct device_extent_record *rec;
6644 struct btrfs_dev_extent *ptr;
6646 rec = calloc(1, sizeof(*rec));
6648 fprintf(stderr, "memory allocation failed\n");
6652 rec->cache.objectid = key->objectid;
6653 rec->cache.start = key->offset;
6655 rec->generation = btrfs_header_generation(leaf);
6657 rec->objectid = key->objectid;
6658 rec->type = key->type;
6659 rec->offset = key->offset;
6661 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
6662 rec->chunk_objecteid =
6663 btrfs_dev_extent_chunk_objectid(leaf, ptr);
6665 btrfs_dev_extent_chunk_offset(leaf, ptr);
6666 rec->length = btrfs_dev_extent_length(leaf, ptr);
6667 rec->cache.size = rec->length;
6669 INIT_LIST_HEAD(&rec->chunk_list);
6670 INIT_LIST_HEAD(&rec->device_list);
6676 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
6677 struct btrfs_key *key, struct extent_buffer *eb,
6680 struct device_extent_record *rec;
6683 rec = btrfs_new_device_extent_record(eb, key, slot);
6684 ret = insert_device_extent_record(dev_extent_cache, rec);
6687 "Device extent[%llu, %llu, %llu] existed.\n",
6688 rec->objectid, rec->offset, rec->length);
6695 static int process_extent_item(struct btrfs_root *root,
6696 struct cache_tree *extent_cache,
6697 struct extent_buffer *eb, int slot)
6699 struct btrfs_extent_item *ei;
6700 struct btrfs_extent_inline_ref *iref;
6701 struct btrfs_extent_data_ref *dref;
6702 struct btrfs_shared_data_ref *sref;
6703 struct btrfs_key key;
6704 struct extent_record tmpl;
6709 u32 item_size = btrfs_item_size_nr(eb, slot);
6715 btrfs_item_key_to_cpu(eb, &key, slot);
6717 if (key.type == BTRFS_METADATA_ITEM_KEY) {
6719 num_bytes = root->nodesize;
6721 num_bytes = key.offset;
6724 if (!IS_ALIGNED(key.objectid, root->sectorsize)) {
6725 error("ignoring invalid extent, bytenr %llu is not aligned to %u",
6726 key.objectid, root->sectorsize);
6729 if (item_size < sizeof(*ei)) {
6730 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6731 struct btrfs_extent_item_v0 *ei0;
6732 BUG_ON(item_size != sizeof(*ei0));
6733 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
6734 refs = btrfs_extent_refs_v0(eb, ei0);
6738 memset(&tmpl, 0, sizeof(tmpl));
6739 tmpl.start = key.objectid;
6740 tmpl.nr = num_bytes;
6741 tmpl.extent_item_refs = refs;
6742 tmpl.metadata = metadata;
6744 tmpl.max_size = num_bytes;
6746 return add_extent_rec(extent_cache, &tmpl);
6749 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
6750 refs = btrfs_extent_refs(eb, ei);
6751 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
6755 if (metadata && num_bytes != root->nodesize) {
6756 error("ignore invalid metadata extent, length %llu does not equal to %u",
6757 num_bytes, root->nodesize);
6760 if (!metadata && !IS_ALIGNED(num_bytes, root->sectorsize)) {
6761 error("ignore invalid data extent, length %llu is not aligned to %u",
6762 num_bytes, root->sectorsize);
6766 memset(&tmpl, 0, sizeof(tmpl));
6767 tmpl.start = key.objectid;
6768 tmpl.nr = num_bytes;
6769 tmpl.extent_item_refs = refs;
6770 tmpl.metadata = metadata;
6772 tmpl.max_size = num_bytes;
6773 add_extent_rec(extent_cache, &tmpl);
6775 ptr = (unsigned long)(ei + 1);
6776 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
6777 key.type == BTRFS_EXTENT_ITEM_KEY)
6778 ptr += sizeof(struct btrfs_tree_block_info);
6780 end = (unsigned long)ei + item_size;
6782 iref = (struct btrfs_extent_inline_ref *)ptr;
6783 type = btrfs_extent_inline_ref_type(eb, iref);
6784 offset = btrfs_extent_inline_ref_offset(eb, iref);
6786 case BTRFS_TREE_BLOCK_REF_KEY:
6787 ret = add_tree_backref(extent_cache, key.objectid,
6790 error("add_tree_backref failed: %s",
6793 case BTRFS_SHARED_BLOCK_REF_KEY:
6794 ret = add_tree_backref(extent_cache, key.objectid,
6797 error("add_tree_backref failed: %s",
6800 case BTRFS_EXTENT_DATA_REF_KEY:
6801 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
6802 add_data_backref(extent_cache, key.objectid, 0,
6803 btrfs_extent_data_ref_root(eb, dref),
6804 btrfs_extent_data_ref_objectid(eb,
6806 btrfs_extent_data_ref_offset(eb, dref),
6807 btrfs_extent_data_ref_count(eb, dref),
6810 case BTRFS_SHARED_DATA_REF_KEY:
6811 sref = (struct btrfs_shared_data_ref *)(iref + 1);
6812 add_data_backref(extent_cache, key.objectid, offset,
6814 btrfs_shared_data_ref_count(eb, sref),
6818 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
6819 key.objectid, key.type, num_bytes);
6822 ptr += btrfs_extent_inline_ref_size(type);
6829 static int check_cache_range(struct btrfs_root *root,
6830 struct btrfs_block_group_cache *cache,
6831 u64 offset, u64 bytes)
6833 struct btrfs_free_space *entry;
6839 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
6840 bytenr = btrfs_sb_offset(i);
6841 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
6842 cache->key.objectid, bytenr, 0,
6843 &logical, &nr, &stripe_len);
6848 if (logical[nr] + stripe_len <= offset)
6850 if (offset + bytes <= logical[nr])
6852 if (logical[nr] == offset) {
6853 if (stripe_len >= bytes) {
6857 bytes -= stripe_len;
6858 offset += stripe_len;
6859 } else if (logical[nr] < offset) {
6860 if (logical[nr] + stripe_len >=
6865 bytes = (offset + bytes) -
6866 (logical[nr] + stripe_len);
6867 offset = logical[nr] + stripe_len;
6870 * Could be tricky, the super may land in the
6871 * middle of the area we're checking. First
6872 * check the easiest case, it's at the end.
6874 if (logical[nr] + stripe_len >=
6876 bytes = logical[nr] - offset;
6880 /* Check the left side */
6881 ret = check_cache_range(root, cache,
6883 logical[nr] - offset);
6889 /* Now we continue with the right side */
6890 bytes = (offset + bytes) -
6891 (logical[nr] + stripe_len);
6892 offset = logical[nr] + stripe_len;
6899 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
6901 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
6902 offset, offset+bytes);
6906 if (entry->offset != offset) {
6907 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
6912 if (entry->bytes != bytes) {
6913 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
6914 bytes, entry->bytes, offset);
6918 unlink_free_space(cache->free_space_ctl, entry);
6923 static int verify_space_cache(struct btrfs_root *root,
6924 struct btrfs_block_group_cache *cache)
6926 struct btrfs_path path;
6927 struct extent_buffer *leaf;
6928 struct btrfs_key key;
6932 root = root->fs_info->extent_root;
6934 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
6936 btrfs_init_path(&path);
6937 key.objectid = last;
6939 key.type = BTRFS_EXTENT_ITEM_KEY;
6940 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6945 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
6946 ret = btrfs_next_leaf(root, &path);
6954 leaf = path.nodes[0];
6955 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
6956 if (key.objectid >= cache->key.offset + cache->key.objectid)
6958 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
6959 key.type != BTRFS_METADATA_ITEM_KEY) {
6964 if (last == key.objectid) {
6965 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6966 last = key.objectid + key.offset;
6968 last = key.objectid + root->nodesize;
6973 ret = check_cache_range(root, cache, last,
6974 key.objectid - last);
6977 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6978 last = key.objectid + key.offset;
6980 last = key.objectid + root->nodesize;
6984 if (last < cache->key.objectid + cache->key.offset)
6985 ret = check_cache_range(root, cache, last,
6986 cache->key.objectid +
6987 cache->key.offset - last);
6990 btrfs_release_path(&path);
6993 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
6994 fprintf(stderr, "There are still entries left in the space "
7002 static int check_space_cache(struct btrfs_root *root)
7004 struct btrfs_block_group_cache *cache;
7005 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
7009 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
7010 btrfs_super_generation(root->fs_info->super_copy) !=
7011 btrfs_super_cache_generation(root->fs_info->super_copy)) {
7012 printf("cache and super generation don't match, space cache "
7013 "will be invalidated\n");
7017 if (ctx.progress_enabled) {
7018 ctx.tp = TASK_FREE_SPACE;
7019 task_start(ctx.info);
7023 cache = btrfs_lookup_first_block_group(root->fs_info, start);
7027 start = cache->key.objectid + cache->key.offset;
7028 if (!cache->free_space_ctl) {
7029 if (btrfs_init_free_space_ctl(cache,
7030 root->sectorsize)) {
7035 btrfs_remove_free_space_cache(cache);
7038 if (btrfs_fs_compat_ro(root->fs_info,
7039 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
7040 ret = exclude_super_stripes(root, cache);
7042 fprintf(stderr, "could not exclude super stripes: %s\n",
7047 ret = load_free_space_tree(root->fs_info, cache);
7048 free_excluded_extents(root, cache);
7050 fprintf(stderr, "could not load free space tree: %s\n",
7057 ret = load_free_space_cache(root->fs_info, cache);
7062 ret = verify_space_cache(root, cache);
7064 fprintf(stderr, "cache appears valid but isn't %Lu\n",
7065 cache->key.objectid);
7070 task_stop(ctx.info);
7072 return error ? -EINVAL : 0;
7075 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
7076 u64 num_bytes, unsigned long leaf_offset,
7077 struct extent_buffer *eb) {
7080 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7082 unsigned long csum_offset;
7086 u64 data_checked = 0;
7092 if (num_bytes % root->sectorsize)
7095 data = malloc(num_bytes);
7099 while (offset < num_bytes) {
7102 read_len = num_bytes - offset;
7103 /* read as much space once a time */
7104 ret = read_extent_data(root, data + offset,
7105 bytenr + offset, &read_len, mirror);
7109 /* verify every 4k data's checksum */
7110 while (data_checked < read_len) {
7112 tmp = offset + data_checked;
7114 csum = btrfs_csum_data(NULL, (char *)data + tmp,
7115 csum, root->sectorsize);
7116 btrfs_csum_final(csum, (u8 *)&csum);
7118 csum_offset = leaf_offset +
7119 tmp / root->sectorsize * csum_size;
7120 read_extent_buffer(eb, (char *)&csum_expected,
7121 csum_offset, csum_size);
7122 /* try another mirror */
7123 if (csum != csum_expected) {
7124 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
7125 mirror, bytenr + tmp,
7126 csum, csum_expected);
7127 num_copies = btrfs_num_copies(
7128 &root->fs_info->mapping_tree,
7130 if (mirror < num_copies - 1) {
7135 data_checked += root->sectorsize;
7144 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
7147 struct btrfs_path path;
7148 struct extent_buffer *leaf;
7149 struct btrfs_key key;
7152 btrfs_init_path(&path);
7153 key.objectid = bytenr;
7154 key.type = BTRFS_EXTENT_ITEM_KEY;
7155 key.offset = (u64)-1;
7158 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, &path,
7161 fprintf(stderr, "Error looking up extent record %d\n", ret);
7162 btrfs_release_path(&path);
7165 if (path.slots[0] > 0) {
7168 ret = btrfs_prev_leaf(root, &path);
7171 } else if (ret > 0) {
7178 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7181 * Block group items come before extent items if they have the same
7182 * bytenr, so walk back one more just in case. Dear future traveller,
7183 * first congrats on mastering time travel. Now if it's not too much
7184 * trouble could you go back to 2006 and tell Chris to make the
7185 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
7186 * EXTENT_ITEM_KEY please?
7188 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
7189 if (path.slots[0] > 0) {
7192 ret = btrfs_prev_leaf(root, &path);
7195 } else if (ret > 0) {
7200 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7204 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7205 ret = btrfs_next_leaf(root, &path);
7207 fprintf(stderr, "Error going to next leaf "
7209 btrfs_release_path(&path);
7215 leaf = path.nodes[0];
7216 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7217 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7221 if (key.objectid + key.offset < bytenr) {
7225 if (key.objectid > bytenr + num_bytes)
7228 if (key.objectid == bytenr) {
7229 if (key.offset >= num_bytes) {
7233 num_bytes -= key.offset;
7234 bytenr += key.offset;
7235 } else if (key.objectid < bytenr) {
7236 if (key.objectid + key.offset >= bytenr + num_bytes) {
7240 num_bytes = (bytenr + num_bytes) -
7241 (key.objectid + key.offset);
7242 bytenr = key.objectid + key.offset;
7244 if (key.objectid + key.offset < bytenr + num_bytes) {
7245 u64 new_start = key.objectid + key.offset;
7246 u64 new_bytes = bytenr + num_bytes - new_start;
7249 * Weird case, the extent is in the middle of
7250 * our range, we'll have to search one side
7251 * and then the other. Not sure if this happens
7252 * in real life, but no harm in coding it up
7253 * anyway just in case.
7255 btrfs_release_path(&path);
7256 ret = check_extent_exists(root, new_start,
7259 fprintf(stderr, "Right section didn't "
7263 num_bytes = key.objectid - bytenr;
7266 num_bytes = key.objectid - bytenr;
7273 if (num_bytes && !ret) {
7274 fprintf(stderr, "There are no extents for csum range "
7275 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
7279 btrfs_release_path(&path);
7283 static int check_csums(struct btrfs_root *root)
7285 struct btrfs_path path;
7286 struct extent_buffer *leaf;
7287 struct btrfs_key key;
7288 u64 offset = 0, num_bytes = 0;
7289 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7293 unsigned long leaf_offset;
7295 root = root->fs_info->csum_root;
7296 if (!extent_buffer_uptodate(root->node)) {
7297 fprintf(stderr, "No valid csum tree found\n");
7301 btrfs_init_path(&path);
7302 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
7303 key.type = BTRFS_EXTENT_CSUM_KEY;
7305 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
7307 fprintf(stderr, "Error searching csum tree %d\n", ret);
7308 btrfs_release_path(&path);
7312 if (ret > 0 && path.slots[0])
7317 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7318 ret = btrfs_next_leaf(root, &path);
7320 fprintf(stderr, "Error going to next leaf "
7327 leaf = path.nodes[0];
7329 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7330 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
7335 data_len = (btrfs_item_size_nr(leaf, path.slots[0]) /
7336 csum_size) * root->sectorsize;
7337 if (!check_data_csum)
7338 goto skip_csum_check;
7339 leaf_offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
7340 ret = check_extent_csums(root, key.offset, data_len,
7346 offset = key.offset;
7347 } else if (key.offset != offset + num_bytes) {
7348 ret = check_extent_exists(root, offset, num_bytes);
7350 fprintf(stderr, "Csum exists for %Lu-%Lu but "
7351 "there is no extent record\n",
7352 offset, offset+num_bytes);
7355 offset = key.offset;
7358 num_bytes += data_len;
7362 btrfs_release_path(&path);
7366 static int is_dropped_key(struct btrfs_key *key,
7367 struct btrfs_key *drop_key) {
7368 if (key->objectid < drop_key->objectid)
7370 else if (key->objectid == drop_key->objectid) {
7371 if (key->type < drop_key->type)
7373 else if (key->type == drop_key->type) {
7374 if (key->offset < drop_key->offset)
7382 * Here are the rules for FULL_BACKREF.
7384 * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
7385 * 2) If btrfs_header_owner(buf) no longer points to buf then we have
7387 * 3) We cowed the block walking down a reloc tree. This is impossible to tell
7388 * if it happened after the relocation occurred since we'll have dropped the
7389 * reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
7390 * have no real way to know for sure.
7392 * We process the blocks one root at a time, and we start from the lowest root
7393 * objectid and go to the highest. So we can just lookup the owner backref for
7394 * the record and if we don't find it then we know it doesn't exist and we have
7397 * FIXME: if we ever start reclaiming root objectid's then we need to fix this
7398 * assumption and simply indicate that we _think_ that the FULL BACKREF needs to
7399 * be set or not and then we can check later once we've gathered all the refs.
7401 static int calc_extent_flag(struct btrfs_root *root,
7402 struct cache_tree *extent_cache,
7403 struct extent_buffer *buf,
7404 struct root_item_record *ri,
7407 struct extent_record *rec;
7408 struct cache_extent *cache;
7409 struct tree_backref *tback;
7412 cache = lookup_cache_extent(extent_cache, buf->start, 1);
7413 /* we have added this extent before */
7417 rec = container_of(cache, struct extent_record, cache);
7420 * Except file/reloc tree, we can not have
7423 if (ri->objectid < BTRFS_FIRST_FREE_OBJECTID)
7428 if (buf->start == ri->bytenr)
7431 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7434 owner = btrfs_header_owner(buf);
7435 if (owner == ri->objectid)
7438 tback = find_tree_backref(rec, 0, owner);
7443 if (rec->flag_block_full_backref != FLAG_UNSET &&
7444 rec->flag_block_full_backref != 0)
7445 rec->bad_full_backref = 1;
7448 *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7449 if (rec->flag_block_full_backref != FLAG_UNSET &&
7450 rec->flag_block_full_backref != 1)
7451 rec->bad_full_backref = 1;
7455 static void report_mismatch_key_root(u8 key_type, u64 rootid)
7457 fprintf(stderr, "Invalid key type(");
7458 print_key_type(stderr, 0, key_type);
7459 fprintf(stderr, ") found in root(");
7460 print_objectid(stderr, rootid, 0);
7461 fprintf(stderr, ")\n");
7465 * Check if the key is valid with its extent buffer.
7467 * This is a early check in case invalid key exists in a extent buffer
7468 * This is not comprehensive yet, but should prevent wrong key/item passed
7471 static int check_type_with_root(u64 rootid, u8 key_type)
7474 /* Only valid in chunk tree */
7475 case BTRFS_DEV_ITEM_KEY:
7476 case BTRFS_CHUNK_ITEM_KEY:
7477 if (rootid != BTRFS_CHUNK_TREE_OBJECTID)
7480 /* valid in csum and log tree */
7481 case BTRFS_CSUM_TREE_OBJECTID:
7482 if (!(rootid == BTRFS_TREE_LOG_OBJECTID ||
7486 case BTRFS_EXTENT_ITEM_KEY:
7487 case BTRFS_METADATA_ITEM_KEY:
7488 case BTRFS_BLOCK_GROUP_ITEM_KEY:
7489 if (rootid != BTRFS_EXTENT_TREE_OBJECTID)
7492 case BTRFS_ROOT_ITEM_KEY:
7493 if (rootid != BTRFS_ROOT_TREE_OBJECTID)
7496 case BTRFS_DEV_EXTENT_KEY:
7497 if (rootid != BTRFS_DEV_TREE_OBJECTID)
7503 report_mismatch_key_root(key_type, rootid);
7507 static int run_next_block(struct btrfs_root *root,
7508 struct block_info *bits,
7511 struct cache_tree *pending,
7512 struct cache_tree *seen,
7513 struct cache_tree *reada,
7514 struct cache_tree *nodes,
7515 struct cache_tree *extent_cache,
7516 struct cache_tree *chunk_cache,
7517 struct rb_root *dev_cache,
7518 struct block_group_tree *block_group_cache,
7519 struct device_extent_tree *dev_extent_cache,
7520 struct root_item_record *ri)
7522 struct extent_buffer *buf;
7523 struct extent_record *rec = NULL;
7534 struct btrfs_key key;
7535 struct cache_extent *cache;
7538 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
7539 bits_nr, &reada_bits);
7544 for(i = 0; i < nritems; i++) {
7545 ret = add_cache_extent(reada, bits[i].start,
7550 /* fixme, get the parent transid */
7551 readahead_tree_block(root, bits[i].start,
7555 *last = bits[0].start;
7556 bytenr = bits[0].start;
7557 size = bits[0].size;
7559 cache = lookup_cache_extent(pending, bytenr, size);
7561 remove_cache_extent(pending, cache);
7564 cache = lookup_cache_extent(reada, bytenr, size);
7566 remove_cache_extent(reada, cache);
7569 cache = lookup_cache_extent(nodes, bytenr, size);
7571 remove_cache_extent(nodes, cache);
7574 cache = lookup_cache_extent(extent_cache, bytenr, size);
7576 rec = container_of(cache, struct extent_record, cache);
7577 gen = rec->parent_generation;
7580 /* fixme, get the real parent transid */
7581 buf = read_tree_block(root, bytenr, size, gen);
7582 if (!extent_buffer_uptodate(buf)) {
7583 record_bad_block_io(root->fs_info,
7584 extent_cache, bytenr, size);
7588 nritems = btrfs_header_nritems(buf);
7591 if (!init_extent_tree) {
7592 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
7593 btrfs_header_level(buf), 1, NULL,
7596 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7598 fprintf(stderr, "Couldn't calc extent flags\n");
7599 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7604 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7606 fprintf(stderr, "Couldn't calc extent flags\n");
7607 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7611 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7613 ri->objectid != BTRFS_TREE_RELOC_OBJECTID &&
7614 ri->objectid == btrfs_header_owner(buf)) {
7616 * Ok we got to this block from it's original owner and
7617 * we have FULL_BACKREF set. Relocation can leave
7618 * converted blocks over so this is altogether possible,
7619 * however it's not possible if the generation > the
7620 * last snapshot, so check for this case.
7622 if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC) &&
7623 btrfs_header_generation(buf) > ri->last_snapshot) {
7624 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
7625 rec->bad_full_backref = 1;
7630 (ri->objectid == BTRFS_TREE_RELOC_OBJECTID ||
7631 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
7632 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7633 rec->bad_full_backref = 1;
7637 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7638 rec->flag_block_full_backref = 1;
7642 rec->flag_block_full_backref = 0;
7644 owner = btrfs_header_owner(buf);
7647 ret = check_block(root, extent_cache, buf, flags);
7651 if (btrfs_is_leaf(buf)) {
7652 btree_space_waste += btrfs_leaf_free_space(root, buf);
7653 for (i = 0; i < nritems; i++) {
7654 struct btrfs_file_extent_item *fi;
7655 btrfs_item_key_to_cpu(buf, &key, i);
7657 * Check key type against the leaf owner.
7658 * Could filter quite a lot of early error if
7661 if (check_type_with_root(btrfs_header_owner(buf),
7663 fprintf(stderr, "ignoring invalid key\n");
7666 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
7667 process_extent_item(root, extent_cache, buf,
7671 if (key.type == BTRFS_METADATA_ITEM_KEY) {
7672 process_extent_item(root, extent_cache, buf,
7676 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
7678 btrfs_item_size_nr(buf, i);
7681 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
7682 process_chunk_item(chunk_cache, &key, buf, i);
7685 if (key.type == BTRFS_DEV_ITEM_KEY) {
7686 process_device_item(dev_cache, &key, buf, i);
7689 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7690 process_block_group_item(block_group_cache,
7694 if (key.type == BTRFS_DEV_EXTENT_KEY) {
7695 process_device_extent_item(dev_extent_cache,
7700 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
7701 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7702 process_extent_ref_v0(extent_cache, buf, i);
7709 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
7710 ret = add_tree_backref(extent_cache,
7711 key.objectid, 0, key.offset, 0);
7713 error("add_tree_backref failed: %s",
7717 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
7718 ret = add_tree_backref(extent_cache,
7719 key.objectid, key.offset, 0, 0);
7721 error("add_tree_backref failed: %s",
7725 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
7726 struct btrfs_extent_data_ref *ref;
7727 ref = btrfs_item_ptr(buf, i,
7728 struct btrfs_extent_data_ref);
7729 add_data_backref(extent_cache,
7731 btrfs_extent_data_ref_root(buf, ref),
7732 btrfs_extent_data_ref_objectid(buf,
7734 btrfs_extent_data_ref_offset(buf, ref),
7735 btrfs_extent_data_ref_count(buf, ref),
7736 0, root->sectorsize);
7739 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
7740 struct btrfs_shared_data_ref *ref;
7741 ref = btrfs_item_ptr(buf, i,
7742 struct btrfs_shared_data_ref);
7743 add_data_backref(extent_cache,
7744 key.objectid, key.offset, 0, 0, 0,
7745 btrfs_shared_data_ref_count(buf, ref),
7746 0, root->sectorsize);
7749 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
7750 struct bad_item *bad;
7752 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
7756 bad = malloc(sizeof(struct bad_item));
7759 INIT_LIST_HEAD(&bad->list);
7760 memcpy(&bad->key, &key,
7761 sizeof(struct btrfs_key));
7762 bad->root_id = owner;
7763 list_add_tail(&bad->list, &delete_items);
7766 if (key.type != BTRFS_EXTENT_DATA_KEY)
7768 fi = btrfs_item_ptr(buf, i,
7769 struct btrfs_file_extent_item);
7770 if (btrfs_file_extent_type(buf, fi) ==
7771 BTRFS_FILE_EXTENT_INLINE)
7773 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
7776 data_bytes_allocated +=
7777 btrfs_file_extent_disk_num_bytes(buf, fi);
7778 if (data_bytes_allocated < root->sectorsize) {
7781 data_bytes_referenced +=
7782 btrfs_file_extent_num_bytes(buf, fi);
7783 add_data_backref(extent_cache,
7784 btrfs_file_extent_disk_bytenr(buf, fi),
7785 parent, owner, key.objectid, key.offset -
7786 btrfs_file_extent_offset(buf, fi), 1, 1,
7787 btrfs_file_extent_disk_num_bytes(buf, fi));
7791 struct btrfs_key first_key;
7793 first_key.objectid = 0;
7796 btrfs_item_key_to_cpu(buf, &first_key, 0);
7797 level = btrfs_header_level(buf);
7798 for (i = 0; i < nritems; i++) {
7799 struct extent_record tmpl;
7801 ptr = btrfs_node_blockptr(buf, i);
7802 size = root->nodesize;
7803 btrfs_node_key_to_cpu(buf, &key, i);
7805 if ((level == ri->drop_level)
7806 && is_dropped_key(&key, &ri->drop_key)) {
7811 memset(&tmpl, 0, sizeof(tmpl));
7812 btrfs_cpu_key_to_disk(&tmpl.parent_key, &key);
7813 tmpl.parent_generation = btrfs_node_ptr_generation(buf, i);
7818 tmpl.max_size = size;
7819 ret = add_extent_rec(extent_cache, &tmpl);
7823 ret = add_tree_backref(extent_cache, ptr, parent,
7826 error("add_tree_backref failed: %s",
7832 add_pending(nodes, seen, ptr, size);
7834 add_pending(pending, seen, ptr, size);
7837 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
7838 nritems) * sizeof(struct btrfs_key_ptr);
7840 total_btree_bytes += buf->len;
7841 if (fs_root_objectid(btrfs_header_owner(buf)))
7842 total_fs_tree_bytes += buf->len;
7843 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
7844 total_extent_tree_bytes += buf->len;
7845 if (!found_old_backref &&
7846 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
7847 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
7848 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7849 found_old_backref = 1;
7851 free_extent_buffer(buf);
7855 static int add_root_to_pending(struct extent_buffer *buf,
7856 struct cache_tree *extent_cache,
7857 struct cache_tree *pending,
7858 struct cache_tree *seen,
7859 struct cache_tree *nodes,
7862 struct extent_record tmpl;
7865 if (btrfs_header_level(buf) > 0)
7866 add_pending(nodes, seen, buf->start, buf->len);
7868 add_pending(pending, seen, buf->start, buf->len);
7870 memset(&tmpl, 0, sizeof(tmpl));
7871 tmpl.start = buf->start;
7876 tmpl.max_size = buf->len;
7877 add_extent_rec(extent_cache, &tmpl);
7879 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
7880 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
7881 ret = add_tree_backref(extent_cache, buf->start, buf->start,
7884 ret = add_tree_backref(extent_cache, buf->start, 0, objectid,
7889 /* as we fix the tree, we might be deleting blocks that
7890 * we're tracking for repair. This hook makes sure we
7891 * remove any backrefs for blocks as we are fixing them.
7893 static int free_extent_hook(struct btrfs_trans_handle *trans,
7894 struct btrfs_root *root,
7895 u64 bytenr, u64 num_bytes, u64 parent,
7896 u64 root_objectid, u64 owner, u64 offset,
7899 struct extent_record *rec;
7900 struct cache_extent *cache;
7902 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
7904 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
7905 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
7909 rec = container_of(cache, struct extent_record, cache);
7911 struct data_backref *back;
7912 back = find_data_backref(rec, parent, root_objectid, owner,
7913 offset, 1, bytenr, num_bytes);
7916 if (back->node.found_ref) {
7917 back->found_ref -= refs_to_drop;
7919 rec->refs -= refs_to_drop;
7921 if (back->node.found_extent_tree) {
7922 back->num_refs -= refs_to_drop;
7923 if (rec->extent_item_refs)
7924 rec->extent_item_refs -= refs_to_drop;
7926 if (back->found_ref == 0)
7927 back->node.found_ref = 0;
7928 if (back->num_refs == 0)
7929 back->node.found_extent_tree = 0;
7931 if (!back->node.found_extent_tree && back->node.found_ref) {
7932 list_del(&back->node.list);
7936 struct tree_backref *back;
7937 back = find_tree_backref(rec, parent, root_objectid);
7940 if (back->node.found_ref) {
7943 back->node.found_ref = 0;
7945 if (back->node.found_extent_tree) {
7946 if (rec->extent_item_refs)
7947 rec->extent_item_refs--;
7948 back->node.found_extent_tree = 0;
7950 if (!back->node.found_extent_tree && back->node.found_ref) {
7951 list_del(&back->node.list);
7955 maybe_free_extent_rec(extent_cache, rec);
7960 static int delete_extent_records(struct btrfs_trans_handle *trans,
7961 struct btrfs_root *root,
7962 struct btrfs_path *path,
7963 u64 bytenr, u64 new_len)
7965 struct btrfs_key key;
7966 struct btrfs_key found_key;
7967 struct extent_buffer *leaf;
7972 key.objectid = bytenr;
7974 key.offset = (u64)-1;
7977 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
7984 if (path->slots[0] == 0)
7990 leaf = path->nodes[0];
7991 slot = path->slots[0];
7993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7994 if (found_key.objectid != bytenr)
7997 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
7998 found_key.type != BTRFS_METADATA_ITEM_KEY &&
7999 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
8000 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
8001 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
8002 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
8003 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
8004 btrfs_release_path(path);
8005 if (found_key.type == 0) {
8006 if (found_key.offset == 0)
8008 key.offset = found_key.offset - 1;
8009 key.type = found_key.type;
8011 key.type = found_key.type - 1;
8012 key.offset = (u64)-1;
8016 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
8017 found_key.objectid, found_key.type, found_key.offset);
8019 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
8022 btrfs_release_path(path);
8024 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
8025 found_key.type == BTRFS_METADATA_ITEM_KEY) {
8026 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
8027 found_key.offset : root->nodesize;
8029 ret = btrfs_update_block_group(trans, root, bytenr,
8036 btrfs_release_path(path);
8041 * for a single backref, this will allocate a new extent
8042 * and add the backref to it.
8044 static int record_extent(struct btrfs_trans_handle *trans,
8045 struct btrfs_fs_info *info,
8046 struct btrfs_path *path,
8047 struct extent_record *rec,
8048 struct extent_backref *back,
8049 int allocated, u64 flags)
8052 struct btrfs_root *extent_root = info->extent_root;
8053 struct extent_buffer *leaf;
8054 struct btrfs_key ins_key;
8055 struct btrfs_extent_item *ei;
8056 struct data_backref *dback;
8057 struct btrfs_tree_block_info *bi;
8060 rec->max_size = max_t(u64, rec->max_size,
8061 info->extent_root->nodesize);
8064 u32 item_size = sizeof(*ei);
8067 item_size += sizeof(*bi);
8069 ins_key.objectid = rec->start;
8070 ins_key.offset = rec->max_size;
8071 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
8073 ret = btrfs_insert_empty_item(trans, extent_root, path,
8074 &ins_key, item_size);
8078 leaf = path->nodes[0];
8079 ei = btrfs_item_ptr(leaf, path->slots[0],
8080 struct btrfs_extent_item);
8082 btrfs_set_extent_refs(leaf, ei, 0);
8083 btrfs_set_extent_generation(leaf, ei, rec->generation);
8085 if (back->is_data) {
8086 btrfs_set_extent_flags(leaf, ei,
8087 BTRFS_EXTENT_FLAG_DATA);
8089 struct btrfs_disk_key copy_key;;
8091 bi = (struct btrfs_tree_block_info *)(ei + 1);
8092 memset_extent_buffer(leaf, 0, (unsigned long)bi,
8095 btrfs_set_disk_key_objectid(©_key,
8096 rec->info_objectid);
8097 btrfs_set_disk_key_type(©_key, 0);
8098 btrfs_set_disk_key_offset(©_key, 0);
8100 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
8101 btrfs_set_tree_block_key(leaf, bi, ©_key);
8103 btrfs_set_extent_flags(leaf, ei,
8104 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
8107 btrfs_mark_buffer_dirty(leaf);
8108 ret = btrfs_update_block_group(trans, extent_root, rec->start,
8109 rec->max_size, 1, 0);
8112 btrfs_release_path(path);
8115 if (back->is_data) {
8119 dback = to_data_backref(back);
8120 if (back->full_backref)
8121 parent = dback->parent;
8125 for (i = 0; i < dback->found_ref; i++) {
8126 /* if parent != 0, we're doing a full backref
8127 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
8128 * just makes the backref allocator create a data
8131 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8132 rec->start, rec->max_size,
8136 BTRFS_FIRST_FREE_OBJECTID :
8142 fprintf(stderr, "adding new data backref"
8143 " on %llu %s %llu owner %llu"
8144 " offset %llu found %d\n",
8145 (unsigned long long)rec->start,
8146 back->full_backref ?
8148 back->full_backref ?
8149 (unsigned long long)parent :
8150 (unsigned long long)dback->root,
8151 (unsigned long long)dback->owner,
8152 (unsigned long long)dback->offset,
8156 struct tree_backref *tback;
8158 tback = to_tree_backref(back);
8159 if (back->full_backref)
8160 parent = tback->parent;
8164 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8165 rec->start, rec->max_size,
8166 parent, tback->root, 0, 0);
8167 fprintf(stderr, "adding new tree backref on "
8168 "start %llu len %llu parent %llu root %llu\n",
8169 rec->start, rec->max_size, parent, tback->root);
8172 btrfs_release_path(path);
8176 static struct extent_entry *find_entry(struct list_head *entries,
8177 u64 bytenr, u64 bytes)
8179 struct extent_entry *entry = NULL;
8181 list_for_each_entry(entry, entries, list) {
8182 if (entry->bytenr == bytenr && entry->bytes == bytes)
8189 static struct extent_entry *find_most_right_entry(struct list_head *entries)
8191 struct extent_entry *entry, *best = NULL, *prev = NULL;
8193 list_for_each_entry(entry, entries, list) {
8195 * If there are as many broken entries as entries then we know
8196 * not to trust this particular entry.
8198 if (entry->broken == entry->count)
8202 * Special case, when there are only two entries and 'best' is
8212 * If our current entry == best then we can't be sure our best
8213 * is really the best, so we need to keep searching.
8215 if (best && best->count == entry->count) {
8221 /* Prev == entry, not good enough, have to keep searching */
8222 if (!prev->broken && prev->count == entry->count)
8226 best = (prev->count > entry->count) ? prev : entry;
8227 else if (best->count < entry->count)
8235 static int repair_ref(struct btrfs_fs_info *info, struct btrfs_path *path,
8236 struct data_backref *dback, struct extent_entry *entry)
8238 struct btrfs_trans_handle *trans;
8239 struct btrfs_root *root;
8240 struct btrfs_file_extent_item *fi;
8241 struct extent_buffer *leaf;
8242 struct btrfs_key key;
8246 key.objectid = dback->root;
8247 key.type = BTRFS_ROOT_ITEM_KEY;
8248 key.offset = (u64)-1;
8249 root = btrfs_read_fs_root(info, &key);
8251 fprintf(stderr, "Couldn't find root for our ref\n");
8256 * The backref points to the original offset of the extent if it was
8257 * split, so we need to search down to the offset we have and then walk
8258 * forward until we find the backref we're looking for.
8260 key.objectid = dback->owner;
8261 key.type = BTRFS_EXTENT_DATA_KEY;
8262 key.offset = dback->offset;
8263 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8265 fprintf(stderr, "Error looking up ref %d\n", ret);
8270 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
8271 ret = btrfs_next_leaf(root, path);
8273 fprintf(stderr, "Couldn't find our ref, next\n");
8277 leaf = path->nodes[0];
8278 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8279 if (key.objectid != dback->owner ||
8280 key.type != BTRFS_EXTENT_DATA_KEY) {
8281 fprintf(stderr, "Couldn't find our ref, search\n");
8284 fi = btrfs_item_ptr(leaf, path->slots[0],
8285 struct btrfs_file_extent_item);
8286 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
8287 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
8289 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
8294 btrfs_release_path(path);
8296 trans = btrfs_start_transaction(root, 1);
8298 return PTR_ERR(trans);
8301 * Ok we have the key of the file extent we want to fix, now we can cow
8302 * down to the thing and fix it.
8304 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
8306 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
8307 key.objectid, key.type, key.offset, ret);
8311 fprintf(stderr, "Well that's odd, we just found this key "
8312 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
8317 leaf = path->nodes[0];
8318 fi = btrfs_item_ptr(leaf, path->slots[0],
8319 struct btrfs_file_extent_item);
8321 if (btrfs_file_extent_compression(leaf, fi) &&
8322 dback->disk_bytenr != entry->bytenr) {
8323 fprintf(stderr, "Ref doesn't match the record start and is "
8324 "compressed, please take a btrfs-image of this file "
8325 "system and send it to a btrfs developer so they can "
8326 "complete this functionality for bytenr %Lu\n",
8327 dback->disk_bytenr);
8332 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
8333 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8334 } else if (dback->disk_bytenr > entry->bytenr) {
8335 u64 off_diff, offset;
8337 off_diff = dback->disk_bytenr - entry->bytenr;
8338 offset = btrfs_file_extent_offset(leaf, fi);
8339 if (dback->disk_bytenr + offset +
8340 btrfs_file_extent_num_bytes(leaf, fi) >
8341 entry->bytenr + entry->bytes) {
8342 fprintf(stderr, "Ref is past the entry end, please "
8343 "take a btrfs-image of this file system and "
8344 "send it to a btrfs developer, ref %Lu\n",
8345 dback->disk_bytenr);
8350 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8351 btrfs_set_file_extent_offset(leaf, fi, offset);
8352 } else if (dback->disk_bytenr < entry->bytenr) {
8355 offset = btrfs_file_extent_offset(leaf, fi);
8356 if (dback->disk_bytenr + offset < entry->bytenr) {
8357 fprintf(stderr, "Ref is before the entry start, please"
8358 " take a btrfs-image of this file system and "
8359 "send it to a btrfs developer, ref %Lu\n",
8360 dback->disk_bytenr);
8365 offset += dback->disk_bytenr;
8366 offset -= entry->bytenr;
8367 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8368 btrfs_set_file_extent_offset(leaf, fi, offset);
8371 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
8374 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
8375 * only do this if we aren't using compression, otherwise it's a
8378 if (!btrfs_file_extent_compression(leaf, fi))
8379 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
8381 printf("ram bytes may be wrong?\n");
8382 btrfs_mark_buffer_dirty(leaf);
8384 err = btrfs_commit_transaction(trans, root);
8385 btrfs_release_path(path);
8386 return ret ? ret : err;
8389 static int verify_backrefs(struct btrfs_fs_info *info, struct btrfs_path *path,
8390 struct extent_record *rec)
8392 struct extent_backref *back;
8393 struct data_backref *dback;
8394 struct extent_entry *entry, *best = NULL;
8397 int broken_entries = 0;
8402 * Metadata is easy and the backrefs should always agree on bytenr and
8403 * size, if not we've got bigger issues.
8408 list_for_each_entry(back, &rec->backrefs, list) {
8409 if (back->full_backref || !back->is_data)
8412 dback = to_data_backref(back);
8415 * We only pay attention to backrefs that we found a real
8418 if (dback->found_ref == 0)
8422 * For now we only catch when the bytes don't match, not the
8423 * bytenr. We can easily do this at the same time, but I want
8424 * to have a fs image to test on before we just add repair
8425 * functionality willy-nilly so we know we won't screw up the
8429 entry = find_entry(&entries, dback->disk_bytenr,
8432 entry = malloc(sizeof(struct extent_entry));
8437 memset(entry, 0, sizeof(*entry));
8438 entry->bytenr = dback->disk_bytenr;
8439 entry->bytes = dback->bytes;
8440 list_add_tail(&entry->list, &entries);
8445 * If we only have on entry we may think the entries agree when
8446 * in reality they don't so we have to do some extra checking.
8448 if (dback->disk_bytenr != rec->start ||
8449 dback->bytes != rec->nr || back->broken)
8460 /* Yay all the backrefs agree, carry on good sir */
8461 if (nr_entries <= 1 && !mismatch)
8464 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
8465 "%Lu\n", rec->start);
8468 * First we want to see if the backrefs can agree amongst themselves who
8469 * is right, so figure out which one of the entries has the highest
8472 best = find_most_right_entry(&entries);
8475 * Ok so we may have an even split between what the backrefs think, so
8476 * this is where we use the extent ref to see what it thinks.
8479 entry = find_entry(&entries, rec->start, rec->nr);
8480 if (!entry && (!broken_entries || !rec->found_rec)) {
8481 fprintf(stderr, "Backrefs don't agree with each other "
8482 "and extent record doesn't agree with anybody,"
8483 " so we can't fix bytenr %Lu bytes %Lu\n",
8484 rec->start, rec->nr);
8487 } else if (!entry) {
8489 * Ok our backrefs were broken, we'll assume this is the
8490 * correct value and add an entry for this range.
8492 entry = malloc(sizeof(struct extent_entry));
8497 memset(entry, 0, sizeof(*entry));
8498 entry->bytenr = rec->start;
8499 entry->bytes = rec->nr;
8500 list_add_tail(&entry->list, &entries);
8504 best = find_most_right_entry(&entries);
8506 fprintf(stderr, "Backrefs and extent record evenly "
8507 "split on who is right, this is going to "
8508 "require user input to fix bytenr %Lu bytes "
8509 "%Lu\n", rec->start, rec->nr);
8516 * I don't think this can happen currently as we'll abort() if we catch
8517 * this case higher up, but in case somebody removes that we still can't
8518 * deal with it properly here yet, so just bail out of that's the case.
8520 if (best->bytenr != rec->start) {
8521 fprintf(stderr, "Extent start and backref starts don't match, "
8522 "please use btrfs-image on this file system and send "
8523 "it to a btrfs developer so they can make fsck fix "
8524 "this particular case. bytenr is %Lu, bytes is %Lu\n",
8525 rec->start, rec->nr);
8531 * Ok great we all agreed on an extent record, let's go find the real
8532 * references and fix up the ones that don't match.
8534 list_for_each_entry(back, &rec->backrefs, list) {
8535 if (back->full_backref || !back->is_data)
8538 dback = to_data_backref(back);
8541 * Still ignoring backrefs that don't have a real ref attached
8544 if (dback->found_ref == 0)
8547 if (dback->bytes == best->bytes &&
8548 dback->disk_bytenr == best->bytenr)
8551 ret = repair_ref(info, path, dback, best);
8557 * Ok we messed with the actual refs, which means we need to drop our
8558 * entire cache and go back and rescan. I know this is a huge pain and
8559 * adds a lot of extra work, but it's the only way to be safe. Once all
8560 * the backrefs agree we may not need to do anything to the extent
8565 while (!list_empty(&entries)) {
8566 entry = list_entry(entries.next, struct extent_entry, list);
8567 list_del_init(&entry->list);
8573 static int process_duplicates(struct btrfs_root *root,
8574 struct cache_tree *extent_cache,
8575 struct extent_record *rec)
8577 struct extent_record *good, *tmp;
8578 struct cache_extent *cache;
8582 * If we found a extent record for this extent then return, or if we
8583 * have more than one duplicate we are likely going to need to delete
8586 if (rec->found_rec || rec->num_duplicates > 1)
8589 /* Shouldn't happen but just in case */
8590 BUG_ON(!rec->num_duplicates);
8593 * So this happens if we end up with a backref that doesn't match the
8594 * actual extent entry. So either the backref is bad or the extent
8595 * entry is bad. Either way we want to have the extent_record actually
8596 * reflect what we found in the extent_tree, so we need to take the
8597 * duplicate out and use that as the extent_record since the only way we
8598 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
8600 remove_cache_extent(extent_cache, &rec->cache);
8602 good = to_extent_record(rec->dups.next);
8603 list_del_init(&good->list);
8604 INIT_LIST_HEAD(&good->backrefs);
8605 INIT_LIST_HEAD(&good->dups);
8606 good->cache.start = good->start;
8607 good->cache.size = good->nr;
8608 good->content_checked = 0;
8609 good->owner_ref_checked = 0;
8610 good->num_duplicates = 0;
8611 good->refs = rec->refs;
8612 list_splice_init(&rec->backrefs, &good->backrefs);
8614 cache = lookup_cache_extent(extent_cache, good->start,
8618 tmp = container_of(cache, struct extent_record, cache);
8621 * If we find another overlapping extent and it's found_rec is
8622 * set then it's a duplicate and we need to try and delete
8625 if (tmp->found_rec || tmp->num_duplicates > 0) {
8626 if (list_empty(&good->list))
8627 list_add_tail(&good->list,
8628 &duplicate_extents);
8629 good->num_duplicates += tmp->num_duplicates + 1;
8630 list_splice_init(&tmp->dups, &good->dups);
8631 list_del_init(&tmp->list);
8632 list_add_tail(&tmp->list, &good->dups);
8633 remove_cache_extent(extent_cache, &tmp->cache);
8638 * Ok we have another non extent item backed extent rec, so lets
8639 * just add it to this extent and carry on like we did above.
8641 good->refs += tmp->refs;
8642 list_splice_init(&tmp->backrefs, &good->backrefs);
8643 remove_cache_extent(extent_cache, &tmp->cache);
8646 ret = insert_cache_extent(extent_cache, &good->cache);
8649 return good->num_duplicates ? 0 : 1;
8652 static int delete_duplicate_records(struct btrfs_root *root,
8653 struct extent_record *rec)
8655 struct btrfs_trans_handle *trans;
8656 LIST_HEAD(delete_list);
8657 struct btrfs_path path;
8658 struct extent_record *tmp, *good, *n;
8661 struct btrfs_key key;
8663 btrfs_init_path(&path);
8666 /* Find the record that covers all of the duplicates. */
8667 list_for_each_entry(tmp, &rec->dups, list) {
8668 if (good->start < tmp->start)
8670 if (good->nr > tmp->nr)
8673 if (tmp->start + tmp->nr < good->start + good->nr) {
8674 fprintf(stderr, "Ok we have overlapping extents that "
8675 "aren't completely covered by each other, this "
8676 "is going to require more careful thought. "
8677 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
8678 tmp->start, tmp->nr, good->start, good->nr);
8685 list_add_tail(&rec->list, &delete_list);
8687 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
8690 list_move_tail(&tmp->list, &delete_list);
8693 root = root->fs_info->extent_root;
8694 trans = btrfs_start_transaction(root, 1);
8695 if (IS_ERR(trans)) {
8696 ret = PTR_ERR(trans);
8700 list_for_each_entry(tmp, &delete_list, list) {
8701 if (tmp->found_rec == 0)
8703 key.objectid = tmp->start;
8704 key.type = BTRFS_EXTENT_ITEM_KEY;
8705 key.offset = tmp->nr;
8707 /* Shouldn't happen but just in case */
8708 if (tmp->metadata) {
8709 fprintf(stderr, "Well this shouldn't happen, extent "
8710 "record overlaps but is metadata? "
8711 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
8715 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
8721 ret = btrfs_del_item(trans, root, &path);
8724 btrfs_release_path(&path);
8727 err = btrfs_commit_transaction(trans, root);
8731 while (!list_empty(&delete_list)) {
8732 tmp = to_extent_record(delete_list.next);
8733 list_del_init(&tmp->list);
8739 while (!list_empty(&rec->dups)) {
8740 tmp = to_extent_record(rec->dups.next);
8741 list_del_init(&tmp->list);
8745 btrfs_release_path(&path);
8747 if (!ret && !nr_del)
8748 rec->num_duplicates = 0;
8750 return ret ? ret : nr_del;
8753 static int find_possible_backrefs(struct btrfs_fs_info *info,
8754 struct btrfs_path *path,
8755 struct cache_tree *extent_cache,
8756 struct extent_record *rec)
8758 struct btrfs_root *root;
8759 struct extent_backref *back;
8760 struct data_backref *dback;
8761 struct cache_extent *cache;
8762 struct btrfs_file_extent_item *fi;
8763 struct btrfs_key key;
8767 list_for_each_entry(back, &rec->backrefs, list) {
8768 /* Don't care about full backrefs (poor unloved backrefs) */
8769 if (back->full_backref || !back->is_data)
8772 dback = to_data_backref(back);
8774 /* We found this one, we don't need to do a lookup */
8775 if (dback->found_ref)
8778 key.objectid = dback->root;
8779 key.type = BTRFS_ROOT_ITEM_KEY;
8780 key.offset = (u64)-1;
8782 root = btrfs_read_fs_root(info, &key);
8784 /* No root, definitely a bad ref, skip */
8785 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
8787 /* Other err, exit */
8789 return PTR_ERR(root);
8791 key.objectid = dback->owner;
8792 key.type = BTRFS_EXTENT_DATA_KEY;
8793 key.offset = dback->offset;
8794 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8796 btrfs_release_path(path);
8799 /* Didn't find it, we can carry on */
8804 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
8805 struct btrfs_file_extent_item);
8806 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
8807 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
8808 btrfs_release_path(path);
8809 cache = lookup_cache_extent(extent_cache, bytenr, 1);
8811 struct extent_record *tmp;
8812 tmp = container_of(cache, struct extent_record, cache);
8815 * If we found an extent record for the bytenr for this
8816 * particular backref then we can't add it to our
8817 * current extent record. We only want to add backrefs
8818 * that don't have a corresponding extent item in the
8819 * extent tree since they likely belong to this record
8820 * and we need to fix it if it doesn't match bytenrs.
8826 dback->found_ref += 1;
8827 dback->disk_bytenr = bytenr;
8828 dback->bytes = bytes;
8831 * Set this so the verify backref code knows not to trust the
8832 * values in this backref.
8841 * Record orphan data ref into corresponding root.
8843 * Return 0 if the extent item contains data ref and recorded.
8844 * Return 1 if the extent item contains no useful data ref
8845 * On that case, it may contains only shared_dataref or metadata backref
8846 * or the file extent exists(this should be handled by the extent bytenr
8848 * Return <0 if something goes wrong.
8850 static int record_orphan_data_extents(struct btrfs_fs_info *fs_info,
8851 struct extent_record *rec)
8853 struct btrfs_key key;
8854 struct btrfs_root *dest_root;
8855 struct extent_backref *back;
8856 struct data_backref *dback;
8857 struct orphan_data_extent *orphan;
8858 struct btrfs_path path;
8859 int recorded_data_ref = 0;
8864 btrfs_init_path(&path);
8865 list_for_each_entry(back, &rec->backrefs, list) {
8866 if (back->full_backref || !back->is_data ||
8867 !back->found_extent_tree)
8869 dback = to_data_backref(back);
8870 if (dback->found_ref)
8872 key.objectid = dback->root;
8873 key.type = BTRFS_ROOT_ITEM_KEY;
8874 key.offset = (u64)-1;
8876 dest_root = btrfs_read_fs_root(fs_info, &key);
8878 /* For non-exist root we just skip it */
8879 if (IS_ERR(dest_root) || !dest_root)
8882 key.objectid = dback->owner;
8883 key.type = BTRFS_EXTENT_DATA_KEY;
8884 key.offset = dback->offset;
8886 ret = btrfs_search_slot(NULL, dest_root, &key, &path, 0, 0);
8887 btrfs_release_path(&path);
8889 * For ret < 0, it's OK since the fs-tree may be corrupted,
8890 * we need to record it for inode/file extent rebuild.
8891 * For ret > 0, we record it only for file extent rebuild.
8892 * For ret == 0, the file extent exists but only bytenr
8893 * mismatch, let the original bytenr fix routine to handle,
8899 orphan = malloc(sizeof(*orphan));
8904 INIT_LIST_HEAD(&orphan->list);
8905 orphan->root = dback->root;
8906 orphan->objectid = dback->owner;
8907 orphan->offset = dback->offset;
8908 orphan->disk_bytenr = rec->cache.start;
8909 orphan->disk_len = rec->cache.size;
8910 list_add(&dest_root->orphan_data_extents, &orphan->list);
8911 recorded_data_ref = 1;
8914 btrfs_release_path(&path);
8916 return !recorded_data_ref;
8922 * when an incorrect extent item is found, this will delete
8923 * all of the existing entries for it and recreate them
8924 * based on what the tree scan found.
8926 static int fixup_extent_refs(struct btrfs_fs_info *info,
8927 struct cache_tree *extent_cache,
8928 struct extent_record *rec)
8930 struct btrfs_trans_handle *trans = NULL;
8932 struct btrfs_path path;
8933 struct list_head *cur = rec->backrefs.next;
8934 struct cache_extent *cache;
8935 struct extent_backref *back;
8939 if (rec->flag_block_full_backref)
8940 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8942 btrfs_init_path(&path);
8943 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
8945 * Sometimes the backrefs themselves are so broken they don't
8946 * get attached to any meaningful rec, so first go back and
8947 * check any of our backrefs that we couldn't find and throw
8948 * them into the list if we find the backref so that
8949 * verify_backrefs can figure out what to do.
8951 ret = find_possible_backrefs(info, &path, extent_cache, rec);
8956 /* step one, make sure all of the backrefs agree */
8957 ret = verify_backrefs(info, &path, rec);
8961 trans = btrfs_start_transaction(info->extent_root, 1);
8962 if (IS_ERR(trans)) {
8963 ret = PTR_ERR(trans);
8967 /* step two, delete all the existing records */
8968 ret = delete_extent_records(trans, info->extent_root, &path,
8969 rec->start, rec->max_size);
8974 /* was this block corrupt? If so, don't add references to it */
8975 cache = lookup_cache_extent(info->corrupt_blocks,
8976 rec->start, rec->max_size);
8982 /* step three, recreate all the refs we did find */
8983 while(cur != &rec->backrefs) {
8984 back = to_extent_backref(cur);
8988 * if we didn't find any references, don't create a
8991 if (!back->found_ref)
8994 rec->bad_full_backref = 0;
8995 ret = record_extent(trans, info, &path, rec, back, allocated, flags);
9003 int err = btrfs_commit_transaction(trans, info->extent_root);
9008 btrfs_release_path(&path);
9012 static int fixup_extent_flags(struct btrfs_fs_info *fs_info,
9013 struct extent_record *rec)
9015 struct btrfs_trans_handle *trans;
9016 struct btrfs_root *root = fs_info->extent_root;
9017 struct btrfs_path path;
9018 struct btrfs_extent_item *ei;
9019 struct btrfs_key key;
9023 key.objectid = rec->start;
9024 if (rec->metadata) {
9025 key.type = BTRFS_METADATA_ITEM_KEY;
9026 key.offset = rec->info_level;
9028 key.type = BTRFS_EXTENT_ITEM_KEY;
9029 key.offset = rec->max_size;
9032 trans = btrfs_start_transaction(root, 0);
9034 return PTR_ERR(trans);
9036 btrfs_init_path(&path);
9037 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
9039 btrfs_release_path(&path);
9040 btrfs_commit_transaction(trans, root);
9043 fprintf(stderr, "Didn't find extent for %llu\n",
9044 (unsigned long long)rec->start);
9045 btrfs_release_path(&path);
9046 btrfs_commit_transaction(trans, root);
9050 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
9051 struct btrfs_extent_item);
9052 flags = btrfs_extent_flags(path.nodes[0], ei);
9053 if (rec->flag_block_full_backref) {
9054 fprintf(stderr, "setting full backref on %llu\n",
9055 (unsigned long long)key.objectid);
9056 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
9058 fprintf(stderr, "clearing full backref on %llu\n",
9059 (unsigned long long)key.objectid);
9060 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
9062 btrfs_set_extent_flags(path.nodes[0], ei, flags);
9063 btrfs_mark_buffer_dirty(path.nodes[0]);
9064 btrfs_release_path(&path);
9065 return btrfs_commit_transaction(trans, root);
9068 /* right now we only prune from the extent allocation tree */
9069 static int prune_one_block(struct btrfs_trans_handle *trans,
9070 struct btrfs_fs_info *info,
9071 struct btrfs_corrupt_block *corrupt)
9074 struct btrfs_path path;
9075 struct extent_buffer *eb;
9079 int level = corrupt->level + 1;
9081 btrfs_init_path(&path);
9083 /* we want to stop at the parent to our busted block */
9084 path.lowest_level = level;
9086 ret = btrfs_search_slot(trans, info->extent_root,
9087 &corrupt->key, &path, -1, 1);
9092 eb = path.nodes[level];
9099 * hopefully the search gave us the block we want to prune,
9100 * lets try that first
9102 slot = path.slots[level];
9103 found = btrfs_node_blockptr(eb, slot);
9104 if (found == corrupt->cache.start)
9107 nritems = btrfs_header_nritems(eb);
9109 /* the search failed, lets scan this node and hope we find it */
9110 for (slot = 0; slot < nritems; slot++) {
9111 found = btrfs_node_blockptr(eb, slot);
9112 if (found == corrupt->cache.start)
9116 * we couldn't find the bad block. TODO, search all the nodes for pointers
9119 if (eb == info->extent_root->node) {
9124 btrfs_release_path(&path);
9129 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
9130 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
9133 btrfs_release_path(&path);
9137 static int prune_corrupt_blocks(struct btrfs_fs_info *info)
9139 struct btrfs_trans_handle *trans = NULL;
9140 struct cache_extent *cache;
9141 struct btrfs_corrupt_block *corrupt;
9144 cache = search_cache_extent(info->corrupt_blocks, 0);
9148 trans = btrfs_start_transaction(info->extent_root, 1);
9150 return PTR_ERR(trans);
9152 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
9153 prune_one_block(trans, info, corrupt);
9154 remove_cache_extent(info->corrupt_blocks, cache);
9157 return btrfs_commit_transaction(trans, info->extent_root);
9161 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
9163 struct btrfs_block_group_cache *cache;
9168 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
9169 &start, &end, EXTENT_DIRTY);
9172 clear_extent_dirty(&fs_info->free_space_cache, start, end,
9178 cache = btrfs_lookup_first_block_group(fs_info, start);
9183 start = cache->key.objectid + cache->key.offset;
9187 static int check_extent_refs(struct btrfs_root *root,
9188 struct cache_tree *extent_cache)
9190 struct extent_record *rec;
9191 struct cache_extent *cache;
9200 * if we're doing a repair, we have to make sure
9201 * we don't allocate from the problem extents.
9202 * In the worst case, this will be all the
9205 cache = search_cache_extent(extent_cache, 0);
9207 rec = container_of(cache, struct extent_record, cache);
9208 set_extent_dirty(root->fs_info->excluded_extents,
9210 rec->start + rec->max_size - 1,
9212 cache = next_cache_extent(cache);
9215 /* pin down all the corrupted blocks too */
9216 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
9218 set_extent_dirty(root->fs_info->excluded_extents,
9220 cache->start + cache->size - 1,
9222 cache = next_cache_extent(cache);
9224 prune_corrupt_blocks(root->fs_info);
9225 reset_cached_block_groups(root->fs_info);
9228 reset_cached_block_groups(root->fs_info);
9231 * We need to delete any duplicate entries we find first otherwise we
9232 * could mess up the extent tree when we have backrefs that actually
9233 * belong to a different extent item and not the weird duplicate one.
9235 while (repair && !list_empty(&duplicate_extents)) {
9236 rec = to_extent_record(duplicate_extents.next);
9237 list_del_init(&rec->list);
9239 /* Sometimes we can find a backref before we find an actual
9240 * extent, so we need to process it a little bit to see if there
9241 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
9242 * if this is a backref screwup. If we need to delete stuff
9243 * process_duplicates() will return 0, otherwise it will return
9246 if (process_duplicates(root, extent_cache, rec))
9248 ret = delete_duplicate_records(root, rec);
9252 * delete_duplicate_records will return the number of entries
9253 * deleted, so if it's greater than 0 then we know we actually
9254 * did something and we need to remove.
9268 cache = search_cache_extent(extent_cache, 0);
9271 rec = container_of(cache, struct extent_record, cache);
9272 if (rec->num_duplicates) {
9273 fprintf(stderr, "extent item %llu has multiple extent "
9274 "items\n", (unsigned long long)rec->start);
9279 if (rec->refs != rec->extent_item_refs) {
9280 fprintf(stderr, "ref mismatch on [%llu %llu] ",
9281 (unsigned long long)rec->start,
9282 (unsigned long long)rec->nr);
9283 fprintf(stderr, "extent item %llu, found %llu\n",
9284 (unsigned long long)rec->extent_item_refs,
9285 (unsigned long long)rec->refs);
9286 ret = record_orphan_data_extents(root->fs_info, rec);
9293 * we can't use the extent to repair file
9294 * extent, let the fallback method handle it.
9296 if (!fixed && repair) {
9297 ret = fixup_extent_refs(
9308 if (all_backpointers_checked(rec, 1)) {
9309 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
9310 (unsigned long long)rec->start,
9311 (unsigned long long)rec->nr);
9313 if (!fixed && !recorded && repair) {
9314 ret = fixup_extent_refs(root->fs_info,
9323 if (!rec->owner_ref_checked) {
9324 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
9325 (unsigned long long)rec->start,
9326 (unsigned long long)rec->nr);
9327 if (!fixed && !recorded && repair) {
9328 ret = fixup_extent_refs(root->fs_info,
9337 if (rec->bad_full_backref) {
9338 fprintf(stderr, "bad full backref, on [%llu]\n",
9339 (unsigned long long)rec->start);
9341 ret = fixup_extent_flags(root->fs_info, rec);
9350 * Although it's not a extent ref's problem, we reuse this
9351 * routine for error reporting.
9352 * No repair function yet.
9354 if (rec->crossing_stripes) {
9356 "bad metadata [%llu, %llu) crossing stripe boundary\n",
9357 rec->start, rec->start + rec->max_size);
9362 if (rec->wrong_chunk_type) {
9364 "bad extent [%llu, %llu), type mismatch with chunk\n",
9365 rec->start, rec->start + rec->max_size);
9370 remove_cache_extent(extent_cache, cache);
9371 free_all_extent_backrefs(rec);
9372 if (!init_extent_tree && repair && (!cur_err || fixed))
9373 clear_extent_dirty(root->fs_info->excluded_extents,
9375 rec->start + rec->max_size - 1,
9381 if (ret && ret != -EAGAIN) {
9382 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
9385 struct btrfs_trans_handle *trans;
9387 root = root->fs_info->extent_root;
9388 trans = btrfs_start_transaction(root, 1);
9389 if (IS_ERR(trans)) {
9390 ret = PTR_ERR(trans);
9394 btrfs_fix_block_accounting(trans, root);
9395 ret = btrfs_commit_transaction(trans, root);
9400 fprintf(stderr, "repaired damaged extent references\n");
9406 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
9410 if (type & BTRFS_BLOCK_GROUP_RAID0) {
9411 stripe_size = length;
9412 stripe_size /= num_stripes;
9413 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
9414 stripe_size = length * 2;
9415 stripe_size /= num_stripes;
9416 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
9417 stripe_size = length;
9418 stripe_size /= (num_stripes - 1);
9419 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
9420 stripe_size = length;
9421 stripe_size /= (num_stripes - 2);
9423 stripe_size = length;
9429 * Check the chunk with its block group/dev list ref:
9430 * Return 0 if all refs seems valid.
9431 * Return 1 if part of refs seems valid, need later check for rebuild ref
9432 * like missing block group and needs to search extent tree to rebuild them.
9433 * Return -1 if essential refs are missing and unable to rebuild.
9435 static int check_chunk_refs(struct chunk_record *chunk_rec,
9436 struct block_group_tree *block_group_cache,
9437 struct device_extent_tree *dev_extent_cache,
9440 struct cache_extent *block_group_item;
9441 struct block_group_record *block_group_rec;
9442 struct cache_extent *dev_extent_item;
9443 struct device_extent_record *dev_extent_rec;
9447 int metadump_v2 = 0;
9451 block_group_item = lookup_cache_extent(&block_group_cache->tree,
9454 if (block_group_item) {
9455 block_group_rec = container_of(block_group_item,
9456 struct block_group_record,
9458 if (chunk_rec->length != block_group_rec->offset ||
9459 chunk_rec->offset != block_group_rec->objectid ||
9461 chunk_rec->type_flags != block_group_rec->flags)) {
9464 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
9465 chunk_rec->objectid,
9470 chunk_rec->type_flags,
9471 block_group_rec->objectid,
9472 block_group_rec->type,
9473 block_group_rec->offset,
9474 block_group_rec->offset,
9475 block_group_rec->objectid,
9476 block_group_rec->flags);
9479 list_del_init(&block_group_rec->list);
9480 chunk_rec->bg_rec = block_group_rec;
9485 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
9486 chunk_rec->objectid,
9491 chunk_rec->type_flags);
9498 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
9499 chunk_rec->num_stripes);
9500 for (i = 0; i < chunk_rec->num_stripes; ++i) {
9501 devid = chunk_rec->stripes[i].devid;
9502 offset = chunk_rec->stripes[i].offset;
9503 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
9504 devid, offset, length);
9505 if (dev_extent_item) {
9506 dev_extent_rec = container_of(dev_extent_item,
9507 struct device_extent_record,
9509 if (dev_extent_rec->objectid != devid ||
9510 dev_extent_rec->offset != offset ||
9511 dev_extent_rec->chunk_offset != chunk_rec->offset ||
9512 dev_extent_rec->length != length) {
9515 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
9516 chunk_rec->objectid,
9519 chunk_rec->stripes[i].devid,
9520 chunk_rec->stripes[i].offset,
9521 dev_extent_rec->objectid,
9522 dev_extent_rec->offset,
9523 dev_extent_rec->length);
9526 list_move(&dev_extent_rec->chunk_list,
9527 &chunk_rec->dextents);
9532 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
9533 chunk_rec->objectid,
9536 chunk_rec->stripes[i].devid,
9537 chunk_rec->stripes[i].offset);
9544 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
9545 int check_chunks(struct cache_tree *chunk_cache,
9546 struct block_group_tree *block_group_cache,
9547 struct device_extent_tree *dev_extent_cache,
9548 struct list_head *good, struct list_head *bad,
9549 struct list_head *rebuild, int silent)
9551 struct cache_extent *chunk_item;
9552 struct chunk_record *chunk_rec;
9553 struct block_group_record *bg_rec;
9554 struct device_extent_record *dext_rec;
9558 chunk_item = first_cache_extent(chunk_cache);
9559 while (chunk_item) {
9560 chunk_rec = container_of(chunk_item, struct chunk_record,
9562 err = check_chunk_refs(chunk_rec, block_group_cache,
9563 dev_extent_cache, silent);
9566 if (err == 0 && good)
9567 list_add_tail(&chunk_rec->list, good);
9568 if (err > 0 && rebuild)
9569 list_add_tail(&chunk_rec->list, rebuild);
9571 list_add_tail(&chunk_rec->list, bad);
9572 chunk_item = next_cache_extent(chunk_item);
9575 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
9578 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
9586 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
9590 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
9601 static int check_device_used(struct device_record *dev_rec,
9602 struct device_extent_tree *dext_cache)
9604 struct cache_extent *cache;
9605 struct device_extent_record *dev_extent_rec;
9608 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
9610 dev_extent_rec = container_of(cache,
9611 struct device_extent_record,
9613 if (dev_extent_rec->objectid != dev_rec->devid)
9616 list_del_init(&dev_extent_rec->device_list);
9617 total_byte += dev_extent_rec->length;
9618 cache = next_cache_extent(cache);
9621 if (total_byte != dev_rec->byte_used) {
9623 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
9624 total_byte, dev_rec->byte_used, dev_rec->objectid,
9625 dev_rec->type, dev_rec->offset);
9632 /* check btrfs_dev_item -> btrfs_dev_extent */
9633 static int check_devices(struct rb_root *dev_cache,
9634 struct device_extent_tree *dev_extent_cache)
9636 struct rb_node *dev_node;
9637 struct device_record *dev_rec;
9638 struct device_extent_record *dext_rec;
9642 dev_node = rb_first(dev_cache);
9644 dev_rec = container_of(dev_node, struct device_record, node);
9645 err = check_device_used(dev_rec, dev_extent_cache);
9649 dev_node = rb_next(dev_node);
9651 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
9654 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
9655 dext_rec->objectid, dext_rec->offset, dext_rec->length);
9662 static int add_root_item_to_list(struct list_head *head,
9663 u64 objectid, u64 bytenr, u64 last_snapshot,
9664 u8 level, u8 drop_level,
9665 int level_size, struct btrfs_key *drop_key)
9668 struct root_item_record *ri_rec;
9669 ri_rec = malloc(sizeof(*ri_rec));
9672 ri_rec->bytenr = bytenr;
9673 ri_rec->objectid = objectid;
9674 ri_rec->level = level;
9675 ri_rec->level_size = level_size;
9676 ri_rec->drop_level = drop_level;
9677 ri_rec->last_snapshot = last_snapshot;
9679 memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
9680 list_add_tail(&ri_rec->list, head);
9685 static void free_root_item_list(struct list_head *list)
9687 struct root_item_record *ri_rec;
9689 while (!list_empty(list)) {
9690 ri_rec = list_first_entry(list, struct root_item_record,
9692 list_del_init(&ri_rec->list);
9697 static int deal_root_from_list(struct list_head *list,
9698 struct btrfs_root *root,
9699 struct block_info *bits,
9701 struct cache_tree *pending,
9702 struct cache_tree *seen,
9703 struct cache_tree *reada,
9704 struct cache_tree *nodes,
9705 struct cache_tree *extent_cache,
9706 struct cache_tree *chunk_cache,
9707 struct rb_root *dev_cache,
9708 struct block_group_tree *block_group_cache,
9709 struct device_extent_tree *dev_extent_cache)
9714 while (!list_empty(list)) {
9715 struct root_item_record *rec;
9716 struct extent_buffer *buf;
9717 rec = list_entry(list->next,
9718 struct root_item_record, list);
9720 buf = read_tree_block(root->fs_info->tree_root,
9721 rec->bytenr, rec->level_size, 0);
9722 if (!extent_buffer_uptodate(buf)) {
9723 free_extent_buffer(buf);
9727 ret = add_root_to_pending(buf, extent_cache, pending,
9728 seen, nodes, rec->objectid);
9732 * To rebuild extent tree, we need deal with snapshot
9733 * one by one, otherwise we deal with node firstly which
9734 * can maximize readahead.
9737 ret = run_next_block(root, bits, bits_nr, &last,
9738 pending, seen, reada, nodes,
9739 extent_cache, chunk_cache,
9740 dev_cache, block_group_cache,
9741 dev_extent_cache, rec);
9745 free_extent_buffer(buf);
9746 list_del(&rec->list);
9752 ret = run_next_block(root, bits, bits_nr, &last, pending, seen,
9753 reada, nodes, extent_cache, chunk_cache,
9754 dev_cache, block_group_cache,
9755 dev_extent_cache, NULL);
9765 static int check_chunks_and_extents(struct btrfs_root *root)
9767 struct rb_root dev_cache;
9768 struct cache_tree chunk_cache;
9769 struct block_group_tree block_group_cache;
9770 struct device_extent_tree dev_extent_cache;
9771 struct cache_tree extent_cache;
9772 struct cache_tree seen;
9773 struct cache_tree pending;
9774 struct cache_tree reada;
9775 struct cache_tree nodes;
9776 struct extent_io_tree excluded_extents;
9777 struct cache_tree corrupt_blocks;
9778 struct btrfs_path path;
9779 struct btrfs_key key;
9780 struct btrfs_key found_key;
9782 struct block_info *bits;
9784 struct extent_buffer *leaf;
9786 struct btrfs_root_item ri;
9787 struct list_head dropping_trees;
9788 struct list_head normal_trees;
9789 struct btrfs_root *root1;
9794 dev_cache = RB_ROOT;
9795 cache_tree_init(&chunk_cache);
9796 block_group_tree_init(&block_group_cache);
9797 device_extent_tree_init(&dev_extent_cache);
9799 cache_tree_init(&extent_cache);
9800 cache_tree_init(&seen);
9801 cache_tree_init(&pending);
9802 cache_tree_init(&nodes);
9803 cache_tree_init(&reada);
9804 cache_tree_init(&corrupt_blocks);
9805 extent_io_tree_init(&excluded_extents);
9806 INIT_LIST_HEAD(&dropping_trees);
9807 INIT_LIST_HEAD(&normal_trees);
9810 root->fs_info->excluded_extents = &excluded_extents;
9811 root->fs_info->fsck_extent_cache = &extent_cache;
9812 root->fs_info->free_extent_hook = free_extent_hook;
9813 root->fs_info->corrupt_blocks = &corrupt_blocks;
9817 bits = malloc(bits_nr * sizeof(struct block_info));
9823 if (ctx.progress_enabled) {
9824 ctx.tp = TASK_EXTENTS;
9825 task_start(ctx.info);
9829 root1 = root->fs_info->tree_root;
9830 level = btrfs_header_level(root1->node);
9831 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9832 root1->node->start, 0, level, 0,
9833 root1->nodesize, NULL);
9836 root1 = root->fs_info->chunk_root;
9837 level = btrfs_header_level(root1->node);
9838 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9839 root1->node->start, 0, level, 0,
9840 root1->nodesize, NULL);
9843 btrfs_init_path(&path);
9846 key.type = BTRFS_ROOT_ITEM_KEY;
9847 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
9852 leaf = path.nodes[0];
9853 slot = path.slots[0];
9854 if (slot >= btrfs_header_nritems(path.nodes[0])) {
9855 ret = btrfs_next_leaf(root, &path);
9858 leaf = path.nodes[0];
9859 slot = path.slots[0];
9861 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
9862 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
9863 unsigned long offset;
9866 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
9867 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
9868 last_snapshot = btrfs_root_last_snapshot(&ri);
9869 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
9870 level = btrfs_root_level(&ri);
9871 level_size = root->nodesize;
9872 ret = add_root_item_to_list(&normal_trees,
9874 btrfs_root_bytenr(&ri),
9875 last_snapshot, level,
9876 0, level_size, NULL);
9880 level = btrfs_root_level(&ri);
9881 level_size = root->nodesize;
9882 objectid = found_key.objectid;
9883 btrfs_disk_key_to_cpu(&found_key,
9885 ret = add_root_item_to_list(&dropping_trees,
9887 btrfs_root_bytenr(&ri),
9888 last_snapshot, level,
9890 level_size, &found_key);
9897 btrfs_release_path(&path);
9900 * check_block can return -EAGAIN if it fixes something, please keep
9901 * this in mind when dealing with return values from these functions, if
9902 * we get -EAGAIN we want to fall through and restart the loop.
9904 ret = deal_root_from_list(&normal_trees, root, bits, bits_nr, &pending,
9905 &seen, &reada, &nodes, &extent_cache,
9906 &chunk_cache, &dev_cache, &block_group_cache,
9913 ret = deal_root_from_list(&dropping_trees, root, bits, bits_nr,
9914 &pending, &seen, &reada, &nodes,
9915 &extent_cache, &chunk_cache, &dev_cache,
9916 &block_group_cache, &dev_extent_cache);
9923 ret = check_chunks(&chunk_cache, &block_group_cache,
9924 &dev_extent_cache, NULL, NULL, NULL, 0);
9931 ret = check_extent_refs(root, &extent_cache);
9938 ret = check_devices(&dev_cache, &dev_extent_cache);
9943 task_stop(ctx.info);
9945 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9946 extent_io_tree_cleanup(&excluded_extents);
9947 root->fs_info->fsck_extent_cache = NULL;
9948 root->fs_info->free_extent_hook = NULL;
9949 root->fs_info->corrupt_blocks = NULL;
9950 root->fs_info->excluded_extents = NULL;
9953 free_chunk_cache_tree(&chunk_cache);
9954 free_device_cache_tree(&dev_cache);
9955 free_block_group_tree(&block_group_cache);
9956 free_device_extent_tree(&dev_extent_cache);
9957 free_extent_cache_tree(&seen);
9958 free_extent_cache_tree(&pending);
9959 free_extent_cache_tree(&reada);
9960 free_extent_cache_tree(&nodes);
9963 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9964 free_extent_cache_tree(&seen);
9965 free_extent_cache_tree(&pending);
9966 free_extent_cache_tree(&reada);
9967 free_extent_cache_tree(&nodes);
9968 free_chunk_cache_tree(&chunk_cache);
9969 free_block_group_tree(&block_group_cache);
9970 free_device_cache_tree(&dev_cache);
9971 free_device_extent_tree(&dev_extent_cache);
9972 free_extent_record_cache(root->fs_info, &extent_cache);
9973 free_root_item_list(&normal_trees);
9974 free_root_item_list(&dropping_trees);
9975 extent_io_tree_cleanup(&excluded_extents);
9980 * Check backrefs of a tree block given by @bytenr or @eb.
9982 * @root: the root containing the @bytenr or @eb
9983 * @eb: tree block extent buffer, can be NULL
9984 * @bytenr: bytenr of the tree block to search
9985 * @level: tree level of the tree block
9986 * @owner: owner of the tree block
9988 * Return >0 for any error found and output error message
9989 * Return 0 for no error found
9991 static int check_tree_block_ref(struct btrfs_root *root,
9992 struct extent_buffer *eb, u64 bytenr,
9993 int level, u64 owner)
9995 struct btrfs_key key;
9996 struct btrfs_root *extent_root = root->fs_info->extent_root;
9997 struct btrfs_path path;
9998 struct btrfs_extent_item *ei;
9999 struct btrfs_extent_inline_ref *iref;
10000 struct extent_buffer *leaf;
10006 u32 nodesize = root->nodesize;
10013 btrfs_init_path(&path);
10014 key.objectid = bytenr;
10015 if (btrfs_fs_incompat(root->fs_info,
10016 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA))
10017 key.type = BTRFS_METADATA_ITEM_KEY;
10019 key.type = BTRFS_EXTENT_ITEM_KEY;
10020 key.offset = (u64)-1;
10022 /* Search for the backref in extent tree */
10023 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10025 err |= BACKREF_MISSING;
10028 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10030 err |= BACKREF_MISSING;
10034 leaf = path.nodes[0];
10035 slot = path.slots[0];
10036 btrfs_item_key_to_cpu(leaf, &key, slot);
10038 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10040 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10041 skinny_level = (int)key.offset;
10042 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10044 struct btrfs_tree_block_info *info;
10046 info = (struct btrfs_tree_block_info *)(ei + 1);
10047 skinny_level = btrfs_tree_block_level(leaf, info);
10048 iref = (struct btrfs_extent_inline_ref *)(info + 1);
10055 if (!(btrfs_extent_flags(leaf, ei) &
10056 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10058 "extent[%llu %u] backref type mismatch, missing bit: %llx",
10059 key.objectid, nodesize,
10060 BTRFS_EXTENT_FLAG_TREE_BLOCK);
10061 err = BACKREF_MISMATCH;
10063 header_gen = btrfs_header_generation(eb);
10064 extent_gen = btrfs_extent_generation(leaf, ei);
10065 if (header_gen != extent_gen) {
10067 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
10068 key.objectid, nodesize, header_gen,
10070 err = BACKREF_MISMATCH;
10072 if (level != skinny_level) {
10074 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
10075 key.objectid, nodesize, level, skinny_level);
10076 err = BACKREF_MISMATCH;
10078 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
10080 "extent[%llu %u] is referred by other roots than %llu",
10081 key.objectid, nodesize, root->objectid);
10082 err = BACKREF_MISMATCH;
10087 * Iterate the extent/metadata item to find the exact backref
10089 item_size = btrfs_item_size_nr(leaf, slot);
10090 ptr = (unsigned long)iref;
10091 end = (unsigned long)ei + item_size;
10092 while (ptr < end) {
10093 iref = (struct btrfs_extent_inline_ref *)ptr;
10094 type = btrfs_extent_inline_ref_type(leaf, iref);
10095 offset = btrfs_extent_inline_ref_offset(leaf, iref);
10097 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
10098 (offset == root->objectid || offset == owner)) {
10100 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
10101 /* Check if the backref points to valid referencer */
10102 found_ref = !check_tree_block_ref(root, NULL, offset,
10108 ptr += btrfs_extent_inline_ref_size(type);
10112 * Inlined extent item doesn't have what we need, check
10113 * TREE_BLOCK_REF_KEY
10116 btrfs_release_path(&path);
10117 key.objectid = bytenr;
10118 key.type = BTRFS_TREE_BLOCK_REF_KEY;
10119 key.offset = root->objectid;
10121 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10126 err |= BACKREF_MISSING;
10128 btrfs_release_path(&path);
10129 if (eb && (err & BACKREF_MISSING))
10130 error("extent[%llu %u] backref lost (owner: %llu, level: %u)",
10131 bytenr, nodesize, owner, level);
10136 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
10138 * Return >0 any error found and output error message
10139 * Return 0 for no error found
10141 static int check_extent_data_item(struct btrfs_root *root,
10142 struct extent_buffer *eb, int slot)
10144 struct btrfs_file_extent_item *fi;
10145 struct btrfs_path path;
10146 struct btrfs_root *extent_root = root->fs_info->extent_root;
10147 struct btrfs_key fi_key;
10148 struct btrfs_key dbref_key;
10149 struct extent_buffer *leaf;
10150 struct btrfs_extent_item *ei;
10151 struct btrfs_extent_inline_ref *iref;
10152 struct btrfs_extent_data_ref *dref;
10154 u64 file_extent_gen;
10156 u64 disk_num_bytes;
10157 u64 extent_num_bytes;
10165 int found_dbackref = 0;
10169 btrfs_item_key_to_cpu(eb, &fi_key, slot);
10170 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
10171 file_extent_gen = btrfs_file_extent_generation(eb, fi);
10173 /* Nothing to check for hole and inline data extents */
10174 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
10175 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
10178 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
10179 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
10180 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
10182 /* Check unaligned disk_num_bytes and num_bytes */
10183 if (!IS_ALIGNED(disk_num_bytes, root->sectorsize)) {
10185 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
10186 fi_key.objectid, fi_key.offset, disk_num_bytes,
10188 err |= BYTES_UNALIGNED;
10190 data_bytes_allocated += disk_num_bytes;
10192 if (!IS_ALIGNED(extent_num_bytes, root->sectorsize)) {
10194 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
10195 fi_key.objectid, fi_key.offset, extent_num_bytes,
10197 err |= BYTES_UNALIGNED;
10199 data_bytes_referenced += extent_num_bytes;
10201 owner = btrfs_header_owner(eb);
10203 /* Check the extent item of the file extent in extent tree */
10204 btrfs_init_path(&path);
10205 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10206 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
10207 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
10209 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
10211 err |= BACKREF_MISSING;
10215 leaf = path.nodes[0];
10216 slot = path.slots[0];
10217 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10219 extent_flags = btrfs_extent_flags(leaf, ei);
10220 extent_gen = btrfs_extent_generation(leaf, ei);
10222 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
10224 "extent[%llu %llu] backref type mismatch, wanted bit: %llx",
10225 disk_bytenr, disk_num_bytes,
10226 BTRFS_EXTENT_FLAG_DATA);
10227 err |= BACKREF_MISMATCH;
10230 if (file_extent_gen < extent_gen) {
10232 "extent[%llu %llu] backref generation mismatch, wanted: <=%llu, have: %llu",
10233 disk_bytenr, disk_num_bytes, file_extent_gen,
10235 err |= BACKREF_MISMATCH;
10238 /* Check data backref inside that extent item */
10239 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
10240 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10241 ptr = (unsigned long)iref;
10242 end = (unsigned long)ei + item_size;
10243 while (ptr < end) {
10244 iref = (struct btrfs_extent_inline_ref *)ptr;
10245 type = btrfs_extent_inline_ref_type(leaf, iref);
10246 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10248 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
10249 ref_root = btrfs_extent_data_ref_root(leaf, dref);
10250 if (ref_root == owner || ref_root == root->objectid)
10251 found_dbackref = 1;
10252 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
10253 found_dbackref = !check_tree_block_ref(root, NULL,
10254 btrfs_extent_inline_ref_offset(leaf, iref),
10258 if (found_dbackref)
10260 ptr += btrfs_extent_inline_ref_size(type);
10263 /* Didn't found inlined data backref, try EXTENT_DATA_REF_KEY */
10264 if (!found_dbackref) {
10265 btrfs_release_path(&path);
10267 btrfs_init_path(&path);
10268 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10269 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
10270 dbref_key.offset = hash_extent_data_ref(root->objectid,
10271 fi_key.objectid, fi_key.offset);
10273 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
10274 &dbref_key, &path, 0, 0);
10276 found_dbackref = 1;
10279 if (!found_dbackref)
10280 err |= BACKREF_MISSING;
10282 btrfs_release_path(&path);
10283 if (err & BACKREF_MISSING) {
10284 error("data extent[%llu %llu] backref lost",
10285 disk_bytenr, disk_num_bytes);
10291 * Get real tree block level for the case like shared block
10292 * Return >= 0 as tree level
10293 * Return <0 for error
10295 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
10297 struct extent_buffer *eb;
10298 struct btrfs_path path;
10299 struct btrfs_key key;
10300 struct btrfs_extent_item *ei;
10303 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10308 /* Search extent tree for extent generation and level */
10309 key.objectid = bytenr;
10310 key.type = BTRFS_METADATA_ITEM_KEY;
10311 key.offset = (u64)-1;
10313 btrfs_init_path(&path);
10314 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
10317 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
10325 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10326 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
10327 struct btrfs_extent_item);
10328 flags = btrfs_extent_flags(path.nodes[0], ei);
10329 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10334 /* Get transid for later read_tree_block() check */
10335 transid = btrfs_extent_generation(path.nodes[0], ei);
10337 /* Get backref level as one source */
10338 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10339 backref_level = key.offset;
10341 struct btrfs_tree_block_info *info;
10343 info = (struct btrfs_tree_block_info *)(ei + 1);
10344 backref_level = btrfs_tree_block_level(path.nodes[0], info);
10346 btrfs_release_path(&path);
10348 /* Get level from tree block as an alternative source */
10349 eb = read_tree_block_fs_info(fs_info, bytenr, nodesize, transid);
10350 if (!extent_buffer_uptodate(eb)) {
10351 free_extent_buffer(eb);
10354 header_level = btrfs_header_level(eb);
10355 free_extent_buffer(eb);
10357 if (header_level != backref_level)
10359 return header_level;
10362 btrfs_release_path(&path);
10367 * Check if a tree block backref is valid (points to a valid tree block)
10368 * if level == -1, level will be resolved
10369 * Return >0 for any error found and print error message
10371 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
10372 u64 bytenr, int level)
10374 struct btrfs_root *root;
10375 struct btrfs_key key;
10376 struct btrfs_path path;
10377 struct extent_buffer *eb;
10378 struct extent_buffer *node;
10379 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10383 /* Query level for level == -1 special case */
10385 level = query_tree_block_level(fs_info, bytenr);
10387 err |= REFERENCER_MISSING;
10391 key.objectid = root_id;
10392 key.type = BTRFS_ROOT_ITEM_KEY;
10393 key.offset = (u64)-1;
10395 root = btrfs_read_fs_root(fs_info, &key);
10396 if (IS_ERR(root)) {
10397 err |= REFERENCER_MISSING;
10401 /* Read out the tree block to get item/node key */
10402 eb = read_tree_block(root, bytenr, root->nodesize, 0);
10403 if (!extent_buffer_uptodate(eb)) {
10404 err |= REFERENCER_MISSING;
10405 free_extent_buffer(eb);
10409 /* Empty tree, no need to check key */
10410 if (!btrfs_header_nritems(eb) && !level) {
10411 free_extent_buffer(eb);
10416 btrfs_node_key_to_cpu(eb, &key, 0);
10418 btrfs_item_key_to_cpu(eb, &key, 0);
10420 free_extent_buffer(eb);
10422 btrfs_init_path(&path);
10423 path.lowest_level = level;
10424 /* Search with the first key, to ensure we can reach it */
10425 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10427 err |= REFERENCER_MISSING;
10431 node = path.nodes[level];
10432 if (btrfs_header_bytenr(node) != bytenr) {
10434 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
10435 bytenr, nodesize, bytenr,
10436 btrfs_header_bytenr(node));
10437 err |= REFERENCER_MISMATCH;
10439 if (btrfs_header_level(node) != level) {
10441 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
10442 bytenr, nodesize, level,
10443 btrfs_header_level(node));
10444 err |= REFERENCER_MISMATCH;
10448 btrfs_release_path(&path);
10450 if (err & REFERENCER_MISSING) {
10452 error("extent [%llu %d] lost referencer (owner: %llu)",
10453 bytenr, nodesize, root_id);
10456 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
10457 bytenr, nodesize, root_id, level);
10464 * Check referencer for shared block backref
10465 * If level == -1, this function will resolve the level.
10467 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
10468 u64 parent, u64 bytenr, int level)
10470 struct extent_buffer *eb;
10471 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10473 int found_parent = 0;
10476 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10477 if (!extent_buffer_uptodate(eb))
10481 level = query_tree_block_level(fs_info, bytenr);
10485 if (level + 1 != btrfs_header_level(eb))
10488 nr = btrfs_header_nritems(eb);
10489 for (i = 0; i < nr; i++) {
10490 if (bytenr == btrfs_node_blockptr(eb, i)) {
10496 free_extent_buffer(eb);
10497 if (!found_parent) {
10499 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
10500 bytenr, nodesize, parent, level);
10501 return REFERENCER_MISSING;
10507 * Check referencer for normal (inlined) data ref
10508 * If len == 0, it will be resolved by searching in extent tree
10510 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
10511 u64 root_id, u64 objectid, u64 offset,
10512 u64 bytenr, u64 len, u32 count)
10514 struct btrfs_root *root;
10515 struct btrfs_root *extent_root = fs_info->extent_root;
10516 struct btrfs_key key;
10517 struct btrfs_path path;
10518 struct extent_buffer *leaf;
10519 struct btrfs_file_extent_item *fi;
10520 u32 found_count = 0;
10525 key.objectid = bytenr;
10526 key.type = BTRFS_EXTENT_ITEM_KEY;
10527 key.offset = (u64)-1;
10529 btrfs_init_path(&path);
10530 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10533 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10536 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10537 if (key.objectid != bytenr ||
10538 key.type != BTRFS_EXTENT_ITEM_KEY)
10541 btrfs_release_path(&path);
10543 key.objectid = root_id;
10544 key.type = BTRFS_ROOT_ITEM_KEY;
10545 key.offset = (u64)-1;
10546 btrfs_init_path(&path);
10548 root = btrfs_read_fs_root(fs_info, &key);
10552 key.objectid = objectid;
10553 key.type = BTRFS_EXTENT_DATA_KEY;
10555 * It can be nasty as data backref offset is
10556 * file offset - file extent offset, which is smaller or
10557 * equal to original backref offset. The only special case is
10558 * overflow. So we need to special check and do further search.
10560 key.offset = offset & (1ULL << 63) ? 0 : offset;
10562 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10567 * Search afterwards to get correct one
10568 * NOTE: As we must do a comprehensive check on the data backref to
10569 * make sure the dref count also matches, we must iterate all file
10570 * extents for that inode.
10573 leaf = path.nodes[0];
10574 slot = path.slots[0];
10576 btrfs_item_key_to_cpu(leaf, &key, slot);
10577 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
10579 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
10581 * Except normal disk bytenr and disk num bytes, we still
10582 * need to do extra check on dbackref offset as
10583 * dbackref offset = file_offset - file_extent_offset
10585 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
10586 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
10587 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
10591 ret = btrfs_next_item(root, &path);
10596 btrfs_release_path(&path);
10597 if (found_count != count) {
10599 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
10600 bytenr, len, root_id, objectid, offset, count, found_count);
10601 return REFERENCER_MISSING;
10607 * Check if the referencer of a shared data backref exists
10609 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
10610 u64 parent, u64 bytenr)
10612 struct extent_buffer *eb;
10613 struct btrfs_key key;
10614 struct btrfs_file_extent_item *fi;
10615 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10617 int found_parent = 0;
10620 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10621 if (!extent_buffer_uptodate(eb))
10624 nr = btrfs_header_nritems(eb);
10625 for (i = 0; i < nr; i++) {
10626 btrfs_item_key_to_cpu(eb, &key, i);
10627 if (key.type != BTRFS_EXTENT_DATA_KEY)
10630 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
10631 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
10634 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
10641 free_extent_buffer(eb);
10642 if (!found_parent) {
10643 error("shared extent %llu referencer lost (parent: %llu)",
10645 return REFERENCER_MISSING;
10651 * This function will check a given extent item, including its backref and
10652 * itself (like crossing stripe boundary and type)
10654 * Since we don't use extent_record anymore, introduce new error bit
10656 static int check_extent_item(struct btrfs_fs_info *fs_info,
10657 struct extent_buffer *eb, int slot)
10659 struct btrfs_extent_item *ei;
10660 struct btrfs_extent_inline_ref *iref;
10661 struct btrfs_extent_data_ref *dref;
10665 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10666 u32 item_size = btrfs_item_size_nr(eb, slot);
10671 struct btrfs_key key;
10675 btrfs_item_key_to_cpu(eb, &key, slot);
10676 if (key.type == BTRFS_EXTENT_ITEM_KEY)
10677 bytes_used += key.offset;
10679 bytes_used += nodesize;
10681 if (item_size < sizeof(*ei)) {
10683 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
10684 * old thing when on disk format is still un-determined.
10685 * No need to care about it anymore
10687 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
10691 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
10692 flags = btrfs_extent_flags(eb, ei);
10694 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
10696 if (metadata && check_crossing_stripes(global_info, key.objectid,
10698 error("bad metadata [%llu, %llu) crossing stripe boundary",
10699 key.objectid, key.objectid + nodesize);
10700 err |= CROSSING_STRIPE_BOUNDARY;
10703 ptr = (unsigned long)(ei + 1);
10705 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
10706 /* Old EXTENT_ITEM metadata */
10707 struct btrfs_tree_block_info *info;
10709 info = (struct btrfs_tree_block_info *)ptr;
10710 level = btrfs_tree_block_level(eb, info);
10711 ptr += sizeof(struct btrfs_tree_block_info);
10713 /* New METADATA_ITEM */
10714 level = key.offset;
10716 end = (unsigned long)ei + item_size;
10719 err |= ITEM_SIZE_MISMATCH;
10723 /* Now check every backref in this extent item */
10725 iref = (struct btrfs_extent_inline_ref *)ptr;
10726 type = btrfs_extent_inline_ref_type(eb, iref);
10727 offset = btrfs_extent_inline_ref_offset(eb, iref);
10729 case BTRFS_TREE_BLOCK_REF_KEY:
10730 ret = check_tree_block_backref(fs_info, offset, key.objectid,
10734 case BTRFS_SHARED_BLOCK_REF_KEY:
10735 ret = check_shared_block_backref(fs_info, offset, key.objectid,
10739 case BTRFS_EXTENT_DATA_REF_KEY:
10740 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10741 ret = check_extent_data_backref(fs_info,
10742 btrfs_extent_data_ref_root(eb, dref),
10743 btrfs_extent_data_ref_objectid(eb, dref),
10744 btrfs_extent_data_ref_offset(eb, dref),
10745 key.objectid, key.offset,
10746 btrfs_extent_data_ref_count(eb, dref));
10749 case BTRFS_SHARED_DATA_REF_KEY:
10750 ret = check_shared_data_backref(fs_info, offset, key.objectid);
10754 error("extent[%llu %d %llu] has unknown ref type: %d",
10755 key.objectid, key.type, key.offset, type);
10756 err |= UNKNOWN_TYPE;
10760 ptr += btrfs_extent_inline_ref_size(type);
10769 * Check if a dev extent item is referred correctly by its chunk
10771 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
10772 struct extent_buffer *eb, int slot)
10774 struct btrfs_root *chunk_root = fs_info->chunk_root;
10775 struct btrfs_dev_extent *ptr;
10776 struct btrfs_path path;
10777 struct btrfs_key chunk_key;
10778 struct btrfs_key devext_key;
10779 struct btrfs_chunk *chunk;
10780 struct extent_buffer *l;
10784 int found_chunk = 0;
10787 btrfs_item_key_to_cpu(eb, &devext_key, slot);
10788 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
10789 length = btrfs_dev_extent_length(eb, ptr);
10791 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
10792 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10793 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
10795 btrfs_init_path(&path);
10796 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10801 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
10802 if (btrfs_chunk_length(l, chunk) != length)
10805 num_stripes = btrfs_chunk_num_stripes(l, chunk);
10806 for (i = 0; i < num_stripes; i++) {
10807 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
10808 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
10810 if (devid == devext_key.objectid &&
10811 offset == devext_key.offset) {
10817 btrfs_release_path(&path);
10818 if (!found_chunk) {
10820 "device extent[%llu, %llu, %llu] did not find the related chunk",
10821 devext_key.objectid, devext_key.offset, length);
10822 return REFERENCER_MISSING;
10828 * Check if the used space is correct with the dev item
10830 static int check_dev_item(struct btrfs_fs_info *fs_info,
10831 struct extent_buffer *eb, int slot)
10833 struct btrfs_root *dev_root = fs_info->dev_root;
10834 struct btrfs_dev_item *dev_item;
10835 struct btrfs_path path;
10836 struct btrfs_key key;
10837 struct btrfs_dev_extent *ptr;
10843 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
10844 dev_id = btrfs_device_id(eb, dev_item);
10845 used = btrfs_device_bytes_used(eb, dev_item);
10847 key.objectid = dev_id;
10848 key.type = BTRFS_DEV_EXTENT_KEY;
10851 btrfs_init_path(&path);
10852 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
10854 btrfs_item_key_to_cpu(eb, &key, slot);
10855 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
10856 key.objectid, key.type, key.offset);
10857 btrfs_release_path(&path);
10858 return REFERENCER_MISSING;
10861 /* Iterate dev_extents to calculate the used space of a device */
10863 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10865 if (key.objectid > dev_id)
10867 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
10870 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
10871 struct btrfs_dev_extent);
10872 total += btrfs_dev_extent_length(path.nodes[0], ptr);
10874 ret = btrfs_next_item(dev_root, &path);
10878 btrfs_release_path(&path);
10880 if (used != total) {
10881 btrfs_item_key_to_cpu(eb, &key, slot);
10883 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
10884 total, used, BTRFS_ROOT_TREE_OBJECTID,
10885 BTRFS_DEV_EXTENT_KEY, dev_id);
10886 return ACCOUNTING_MISMATCH;
10892 * Check a block group item with its referener (chunk) and its used space
10893 * with extent/metadata item
10895 static int check_block_group_item(struct btrfs_fs_info *fs_info,
10896 struct extent_buffer *eb, int slot)
10898 struct btrfs_root *extent_root = fs_info->extent_root;
10899 struct btrfs_root *chunk_root = fs_info->chunk_root;
10900 struct btrfs_block_group_item *bi;
10901 struct btrfs_block_group_item bg_item;
10902 struct btrfs_path path;
10903 struct btrfs_key bg_key;
10904 struct btrfs_key chunk_key;
10905 struct btrfs_key extent_key;
10906 struct btrfs_chunk *chunk;
10907 struct extent_buffer *leaf;
10908 struct btrfs_extent_item *ei;
10909 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10917 btrfs_item_key_to_cpu(eb, &bg_key, slot);
10918 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
10919 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
10920 used = btrfs_block_group_used(&bg_item);
10921 bg_flags = btrfs_block_group_flags(&bg_item);
10923 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
10924 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10925 chunk_key.offset = bg_key.objectid;
10927 btrfs_init_path(&path);
10928 /* Search for the referencer chunk */
10929 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10932 "block group[%llu %llu] did not find the related chunk item",
10933 bg_key.objectid, bg_key.offset);
10934 err |= REFERENCER_MISSING;
10936 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
10937 struct btrfs_chunk);
10938 if (btrfs_chunk_length(path.nodes[0], chunk) !=
10941 "block group[%llu %llu] related chunk item length does not match",
10942 bg_key.objectid, bg_key.offset);
10943 err |= REFERENCER_MISMATCH;
10946 btrfs_release_path(&path);
10948 /* Search from the block group bytenr */
10949 extent_key.objectid = bg_key.objectid;
10950 extent_key.type = 0;
10951 extent_key.offset = 0;
10953 btrfs_init_path(&path);
10954 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
10958 /* Iterate extent tree to account used space */
10960 leaf = path.nodes[0];
10961 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
10962 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
10965 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
10966 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
10968 if (extent_key.objectid < bg_key.objectid)
10971 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
10974 total += extent_key.offset;
10976 ei = btrfs_item_ptr(leaf, path.slots[0],
10977 struct btrfs_extent_item);
10978 flags = btrfs_extent_flags(leaf, ei);
10979 if (flags & BTRFS_EXTENT_FLAG_DATA) {
10980 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
10982 "bad extent[%llu, %llu) type mismatch with chunk",
10983 extent_key.objectid,
10984 extent_key.objectid + extent_key.offset);
10985 err |= CHUNK_TYPE_MISMATCH;
10987 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
10988 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
10989 BTRFS_BLOCK_GROUP_METADATA))) {
10991 "bad extent[%llu, %llu) type mismatch with chunk",
10992 extent_key.objectid,
10993 extent_key.objectid + nodesize);
10994 err |= CHUNK_TYPE_MISMATCH;
10998 ret = btrfs_next_item(extent_root, &path);
11004 btrfs_release_path(&path);
11006 if (total != used) {
11008 "block group[%llu %llu] used %llu but extent items used %llu",
11009 bg_key.objectid, bg_key.offset, used, total);
11010 err |= ACCOUNTING_MISMATCH;
11016 * Check a chunk item.
11017 * Including checking all referred dev_extents and block group
11019 static int check_chunk_item(struct btrfs_fs_info *fs_info,
11020 struct extent_buffer *eb, int slot)
11022 struct btrfs_root *extent_root = fs_info->extent_root;
11023 struct btrfs_root *dev_root = fs_info->dev_root;
11024 struct btrfs_path path;
11025 struct btrfs_key chunk_key;
11026 struct btrfs_key bg_key;
11027 struct btrfs_key devext_key;
11028 struct btrfs_chunk *chunk;
11029 struct extent_buffer *leaf;
11030 struct btrfs_block_group_item *bi;
11031 struct btrfs_block_group_item bg_item;
11032 struct btrfs_dev_extent *ptr;
11033 u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
11045 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
11046 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
11047 length = btrfs_chunk_length(eb, chunk);
11048 chunk_end = chunk_key.offset + length;
11049 if (!IS_ALIGNED(length, sectorsize)) {
11050 error("chunk[%llu %llu) not aligned to %u",
11051 chunk_key.offset, chunk_end, sectorsize);
11052 err |= BYTES_UNALIGNED;
11056 type = btrfs_chunk_type(eb, chunk);
11057 profile = type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
11058 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
11059 error("chunk[%llu %llu) has no chunk type",
11060 chunk_key.offset, chunk_end);
11061 err |= UNKNOWN_TYPE;
11063 if (profile && (profile & (profile - 1))) {
11064 error("chunk[%llu %llu) multiple profiles detected: %llx",
11065 chunk_key.offset, chunk_end, profile);
11066 err |= UNKNOWN_TYPE;
11069 bg_key.objectid = chunk_key.offset;
11070 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
11071 bg_key.offset = length;
11073 btrfs_init_path(&path);
11074 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
11077 "chunk[%llu %llu) did not find the related block group item",
11078 chunk_key.offset, chunk_end);
11079 err |= REFERENCER_MISSING;
11081 leaf = path.nodes[0];
11082 bi = btrfs_item_ptr(leaf, path.slots[0],
11083 struct btrfs_block_group_item);
11084 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
11086 if (btrfs_block_group_flags(&bg_item) != type) {
11088 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
11089 chunk_key.offset, chunk_end, type,
11090 btrfs_block_group_flags(&bg_item));
11091 err |= REFERENCER_MISSING;
11095 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
11096 for (i = 0; i < num_stripes; i++) {
11097 btrfs_release_path(&path);
11098 btrfs_init_path(&path);
11099 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
11100 devext_key.type = BTRFS_DEV_EXTENT_KEY;
11101 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
11103 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
11106 goto not_match_dev;
11108 leaf = path.nodes[0];
11109 ptr = btrfs_item_ptr(leaf, path.slots[0],
11110 struct btrfs_dev_extent);
11111 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
11112 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
11113 if (objectid != chunk_key.objectid ||
11114 offset != chunk_key.offset ||
11115 btrfs_dev_extent_length(leaf, ptr) != length)
11116 goto not_match_dev;
11119 err |= BACKREF_MISSING;
11121 "chunk[%llu %llu) stripe %d did not find the related dev extent",
11122 chunk_key.objectid, chunk_end, i);
11125 btrfs_release_path(&path);
11131 * Main entry function to check known items and update related accounting info
11133 static int check_leaf_items(struct btrfs_root *root, struct extent_buffer *eb)
11135 struct btrfs_fs_info *fs_info = root->fs_info;
11136 struct btrfs_key key;
11139 struct btrfs_extent_data_ref *dref;
11144 btrfs_item_key_to_cpu(eb, &key, slot);
11148 case BTRFS_EXTENT_DATA_KEY:
11149 ret = check_extent_data_item(root, eb, slot);
11152 case BTRFS_BLOCK_GROUP_ITEM_KEY:
11153 ret = check_block_group_item(fs_info, eb, slot);
11156 case BTRFS_DEV_ITEM_KEY:
11157 ret = check_dev_item(fs_info, eb, slot);
11160 case BTRFS_CHUNK_ITEM_KEY:
11161 ret = check_chunk_item(fs_info, eb, slot);
11164 case BTRFS_DEV_EXTENT_KEY:
11165 ret = check_dev_extent_item(fs_info, eb, slot);
11168 case BTRFS_EXTENT_ITEM_KEY:
11169 case BTRFS_METADATA_ITEM_KEY:
11170 ret = check_extent_item(fs_info, eb, slot);
11173 case BTRFS_EXTENT_CSUM_KEY:
11174 total_csum_bytes += btrfs_item_size_nr(eb, slot);
11176 case BTRFS_TREE_BLOCK_REF_KEY:
11177 ret = check_tree_block_backref(fs_info, key.offset,
11181 case BTRFS_EXTENT_DATA_REF_KEY:
11182 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
11183 ret = check_extent_data_backref(fs_info,
11184 btrfs_extent_data_ref_root(eb, dref),
11185 btrfs_extent_data_ref_objectid(eb, dref),
11186 btrfs_extent_data_ref_offset(eb, dref),
11188 btrfs_extent_data_ref_count(eb, dref));
11191 case BTRFS_SHARED_BLOCK_REF_KEY:
11192 ret = check_shared_block_backref(fs_info, key.offset,
11196 case BTRFS_SHARED_DATA_REF_KEY:
11197 ret = check_shared_data_backref(fs_info, key.offset,
11205 if (++slot < btrfs_header_nritems(eb))
11212 * Helper function for later fs/subvol tree check. To determine if a tree
11213 * block should be checked.
11214 * This function will ensure only the direct referencer with lowest rootid to
11215 * check a fs/subvolume tree block.
11217 * Backref check at extent tree would detect errors like missing subvolume
11218 * tree, so we can do aggressive check to reduce duplicated checks.
11220 static int should_check(struct btrfs_root *root, struct extent_buffer *eb)
11222 struct btrfs_root *extent_root = root->fs_info->extent_root;
11223 struct btrfs_key key;
11224 struct btrfs_path path;
11225 struct extent_buffer *leaf;
11227 struct btrfs_extent_item *ei;
11233 struct btrfs_extent_inline_ref *iref;
11236 btrfs_init_path(&path);
11237 key.objectid = btrfs_header_bytenr(eb);
11238 key.type = BTRFS_METADATA_ITEM_KEY;
11239 key.offset = (u64)-1;
11242 * Any failure in backref resolving means we can't determine
11243 * whom the tree block belongs to.
11244 * So in that case, we need to check that tree block
11246 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11250 ret = btrfs_previous_extent_item(extent_root, &path,
11251 btrfs_header_bytenr(eb));
11255 leaf = path.nodes[0];
11256 slot = path.slots[0];
11257 btrfs_item_key_to_cpu(leaf, &key, slot);
11258 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
11260 if (key.type == BTRFS_METADATA_ITEM_KEY) {
11261 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
11263 struct btrfs_tree_block_info *info;
11265 info = (struct btrfs_tree_block_info *)(ei + 1);
11266 iref = (struct btrfs_extent_inline_ref *)(info + 1);
11269 item_size = btrfs_item_size_nr(leaf, slot);
11270 ptr = (unsigned long)iref;
11271 end = (unsigned long)ei + item_size;
11272 while (ptr < end) {
11273 iref = (struct btrfs_extent_inline_ref *)ptr;
11274 type = btrfs_extent_inline_ref_type(leaf, iref);
11275 offset = btrfs_extent_inline_ref_offset(leaf, iref);
11278 * We only check the tree block if current root is
11279 * the lowest referencer of it.
11281 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
11282 offset < root->objectid) {
11283 btrfs_release_path(&path);
11287 ptr += btrfs_extent_inline_ref_size(type);
11290 * Normally we should also check keyed tree block ref, but that may be
11291 * very time consuming. Inlined ref should already make us skip a lot
11292 * of refs now. So skip search keyed tree block ref.
11296 btrfs_release_path(&path);
11301 * Traversal function for tree block. We will do:
11302 * 1) Skip shared fs/subvolume tree blocks
11303 * 2) Update related bytes accounting
11304 * 3) Pre-order traversal
11306 static int traverse_tree_block(struct btrfs_root *root,
11307 struct extent_buffer *node)
11309 struct extent_buffer *eb;
11310 struct btrfs_key key;
11311 struct btrfs_key drop_key;
11319 * Skip shared fs/subvolume tree block, in that case they will
11320 * be checked by referencer with lowest rootid
11322 if (is_fstree(root->objectid) && !should_check(root, node))
11325 /* Update bytes accounting */
11326 total_btree_bytes += node->len;
11327 if (fs_root_objectid(btrfs_header_owner(node)))
11328 total_fs_tree_bytes += node->len;
11329 if (btrfs_header_owner(node) == BTRFS_EXTENT_TREE_OBJECTID)
11330 total_extent_tree_bytes += node->len;
11331 if (!found_old_backref &&
11332 btrfs_header_owner(node) == BTRFS_TREE_RELOC_OBJECTID &&
11333 btrfs_header_backref_rev(node) == BTRFS_MIXED_BACKREF_REV &&
11334 !btrfs_header_flag(node, BTRFS_HEADER_FLAG_RELOC))
11335 found_old_backref = 1;
11337 /* pre-order tranversal, check itself first */
11338 level = btrfs_header_level(node);
11339 ret = check_tree_block_ref(root, node, btrfs_header_bytenr(node),
11340 btrfs_header_level(node),
11341 btrfs_header_owner(node));
11345 "check %s failed root %llu bytenr %llu level %d, force continue check",
11346 level ? "node":"leaf", root->objectid,
11347 btrfs_header_bytenr(node), btrfs_header_level(node));
11350 btree_space_waste += btrfs_leaf_free_space(root, node);
11351 ret = check_leaf_items(root, node);
11356 nr = btrfs_header_nritems(node);
11357 btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
11358 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) - nr) *
11359 sizeof(struct btrfs_key_ptr);
11361 /* Then check all its children */
11362 for (i = 0; i < nr; i++) {
11363 u64 blocknr = btrfs_node_blockptr(node, i);
11365 btrfs_node_key_to_cpu(node, &key, i);
11366 if (level == root->root_item.drop_level &&
11367 is_dropped_key(&key, &drop_key))
11371 * As a btrfs tree has most 8 levels (0..7), so it's quite safe
11372 * to call the function itself.
11374 eb = read_tree_block(root, blocknr, root->nodesize, 0);
11375 if (extent_buffer_uptodate(eb)) {
11376 ret = traverse_tree_block(root, eb);
11379 free_extent_buffer(eb);
11386 * Low memory usage version check_chunks_and_extents.
11388 static int check_chunks_and_extents_v2(struct btrfs_root *root)
11390 struct btrfs_path path;
11391 struct btrfs_key key;
11392 struct btrfs_root *root1;
11393 struct btrfs_root *cur_root;
11397 root1 = root->fs_info->chunk_root;
11398 ret = traverse_tree_block(root1, root1->node);
11401 root1 = root->fs_info->tree_root;
11402 ret = traverse_tree_block(root1, root1->node);
11405 btrfs_init_path(&path);
11406 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
11408 key.type = BTRFS_ROOT_ITEM_KEY;
11410 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
11412 error("cannot find extent treet in tree_root");
11417 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
11418 if (key.type != BTRFS_ROOT_ITEM_KEY)
11420 key.offset = (u64)-1;
11422 cur_root = btrfs_read_fs_root(root->fs_info, &key);
11423 if (IS_ERR(cur_root) || !cur_root) {
11424 error("failed to read tree: %lld", key.objectid);
11428 ret = traverse_tree_block(cur_root, cur_root->node);
11432 ret = btrfs_next_item(root1, &path);
11438 btrfs_release_path(&path);
11442 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
11443 struct btrfs_root *root, int overwrite)
11445 struct extent_buffer *c;
11446 struct extent_buffer *old = root->node;
11449 struct btrfs_disk_key disk_key = {0,0,0};
11455 extent_buffer_get(c);
11458 c = btrfs_alloc_free_block(trans, root,
11460 root->root_key.objectid,
11461 &disk_key, level, 0, 0);
11464 extent_buffer_get(c);
11468 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
11469 btrfs_set_header_level(c, level);
11470 btrfs_set_header_bytenr(c, c->start);
11471 btrfs_set_header_generation(c, trans->transid);
11472 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
11473 btrfs_set_header_owner(c, root->root_key.objectid);
11475 write_extent_buffer(c, root->fs_info->fsid,
11476 btrfs_header_fsid(), BTRFS_FSID_SIZE);
11478 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
11479 btrfs_header_chunk_tree_uuid(c),
11482 btrfs_mark_buffer_dirty(c);
11484 * this case can happen in the following case:
11486 * 1.overwrite previous root.
11488 * 2.reinit reloc data root, this is because we skip pin
11489 * down reloc data tree before which means we can allocate
11490 * same block bytenr here.
11492 if (old->start == c->start) {
11493 btrfs_set_root_generation(&root->root_item,
11495 root->root_item.level = btrfs_header_level(root->node);
11496 ret = btrfs_update_root(trans, root->fs_info->tree_root,
11497 &root->root_key, &root->root_item);
11499 free_extent_buffer(c);
11503 free_extent_buffer(old);
11505 add_root_to_dirty_list(root);
11509 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
11510 struct extent_buffer *eb, int tree_root)
11512 struct extent_buffer *tmp;
11513 struct btrfs_root_item *ri;
11514 struct btrfs_key key;
11517 int level = btrfs_header_level(eb);
11523 * If we have pinned this block before, don't pin it again.
11524 * This can not only avoid forever loop with broken filesystem
11525 * but also give us some speedups.
11527 if (test_range_bit(&fs_info->pinned_extents, eb->start,
11528 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
11531 btrfs_pin_extent(fs_info, eb->start, eb->len);
11533 nodesize = btrfs_super_nodesize(fs_info->super_copy);
11534 nritems = btrfs_header_nritems(eb);
11535 for (i = 0; i < nritems; i++) {
11537 btrfs_item_key_to_cpu(eb, &key, i);
11538 if (key.type != BTRFS_ROOT_ITEM_KEY)
11540 /* Skip the extent root and reloc roots */
11541 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
11542 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
11543 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
11545 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
11546 bytenr = btrfs_disk_root_bytenr(eb, ri);
11549 * If at any point we start needing the real root we
11550 * will have to build a stump root for the root we are
11551 * in, but for now this doesn't actually use the root so
11552 * just pass in extent_root.
11554 tmp = read_tree_block(fs_info->extent_root, bytenr,
11556 if (!extent_buffer_uptodate(tmp)) {
11557 fprintf(stderr, "Error reading root block\n");
11560 ret = pin_down_tree_blocks(fs_info, tmp, 0);
11561 free_extent_buffer(tmp);
11565 bytenr = btrfs_node_blockptr(eb, i);
11567 /* If we aren't the tree root don't read the block */
11568 if (level == 1 && !tree_root) {
11569 btrfs_pin_extent(fs_info, bytenr, nodesize);
11573 tmp = read_tree_block(fs_info->extent_root, bytenr,
11575 if (!extent_buffer_uptodate(tmp)) {
11576 fprintf(stderr, "Error reading tree block\n");
11579 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
11580 free_extent_buffer(tmp);
11589 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
11593 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
11597 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
11600 static int reset_block_groups(struct btrfs_fs_info *fs_info)
11602 struct btrfs_block_group_cache *cache;
11603 struct btrfs_path path;
11604 struct extent_buffer *leaf;
11605 struct btrfs_chunk *chunk;
11606 struct btrfs_key key;
11610 btrfs_init_path(&path);
11612 key.type = BTRFS_CHUNK_ITEM_KEY;
11614 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, &path, 0, 0);
11616 btrfs_release_path(&path);
11621 * We do this in case the block groups were screwed up and had alloc
11622 * bits that aren't actually set on the chunks. This happens with
11623 * restored images every time and could happen in real life I guess.
11625 fs_info->avail_data_alloc_bits = 0;
11626 fs_info->avail_metadata_alloc_bits = 0;
11627 fs_info->avail_system_alloc_bits = 0;
11629 /* First we need to create the in-memory block groups */
11631 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11632 ret = btrfs_next_leaf(fs_info->chunk_root, &path);
11634 btrfs_release_path(&path);
11642 leaf = path.nodes[0];
11643 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11644 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
11649 chunk = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_chunk);
11650 btrfs_add_block_group(fs_info, 0,
11651 btrfs_chunk_type(leaf, chunk),
11652 key.objectid, key.offset,
11653 btrfs_chunk_length(leaf, chunk));
11654 set_extent_dirty(&fs_info->free_space_cache, key.offset,
11655 key.offset + btrfs_chunk_length(leaf, chunk),
11661 cache = btrfs_lookup_first_block_group(fs_info, start);
11665 start = cache->key.objectid + cache->key.offset;
11668 btrfs_release_path(&path);
11672 static int reset_balance(struct btrfs_trans_handle *trans,
11673 struct btrfs_fs_info *fs_info)
11675 struct btrfs_root *root = fs_info->tree_root;
11676 struct btrfs_path path;
11677 struct extent_buffer *leaf;
11678 struct btrfs_key key;
11679 int del_slot, del_nr = 0;
11683 btrfs_init_path(&path);
11684 key.objectid = BTRFS_BALANCE_OBJECTID;
11685 key.type = BTRFS_BALANCE_ITEM_KEY;
11687 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11692 goto reinit_data_reloc;
11697 ret = btrfs_del_item(trans, root, &path);
11700 btrfs_release_path(&path);
11702 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
11703 key.type = BTRFS_ROOT_ITEM_KEY;
11705 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11709 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11714 ret = btrfs_del_items(trans, root, &path,
11721 btrfs_release_path(&path);
11724 ret = btrfs_search_slot(trans, root, &key, &path,
11731 leaf = path.nodes[0];
11732 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11733 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
11735 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
11740 del_slot = path.slots[0];
11749 ret = btrfs_del_items(trans, root, &path, del_slot, del_nr);
11753 btrfs_release_path(&path);
11756 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
11757 key.type = BTRFS_ROOT_ITEM_KEY;
11758 key.offset = (u64)-1;
11759 root = btrfs_read_fs_root(fs_info, &key);
11760 if (IS_ERR(root)) {
11761 fprintf(stderr, "Error reading data reloc tree\n");
11762 ret = PTR_ERR(root);
11765 record_root_in_trans(trans, root);
11766 ret = btrfs_fsck_reinit_root(trans, root, 0);
11769 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
11771 btrfs_release_path(&path);
11775 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
11776 struct btrfs_fs_info *fs_info)
11782 * The only reason we don't do this is because right now we're just
11783 * walking the trees we find and pinning down their bytes, we don't look
11784 * at any of the leaves. In order to do mixed groups we'd have to check
11785 * the leaves of any fs roots and pin down the bytes for any file
11786 * extents we find. Not hard but why do it if we don't have to?
11788 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
11789 fprintf(stderr, "We don't support re-initing the extent tree "
11790 "for mixed block groups yet, please notify a btrfs "
11791 "developer you want to do this so they can add this "
11792 "functionality.\n");
11797 * first we need to walk all of the trees except the extent tree and pin
11798 * down the bytes that are in use so we don't overwrite any existing
11801 ret = pin_metadata_blocks(fs_info);
11803 fprintf(stderr, "error pinning down used bytes\n");
11808 * Need to drop all the block groups since we're going to recreate all
11811 btrfs_free_block_groups(fs_info);
11812 ret = reset_block_groups(fs_info);
11814 fprintf(stderr, "error resetting the block groups\n");
11818 /* Ok we can allocate now, reinit the extent root */
11819 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
11821 fprintf(stderr, "extent root initialization failed\n");
11823 * When the transaction code is updated we should end the
11824 * transaction, but for now progs only knows about commit so
11825 * just return an error.
11831 * Now we have all the in-memory block groups setup so we can make
11832 * allocations properly, and the metadata we care about is safe since we
11833 * pinned all of it above.
11836 struct btrfs_block_group_cache *cache;
11838 cache = btrfs_lookup_first_block_group(fs_info, start);
11841 start = cache->key.objectid + cache->key.offset;
11842 ret = btrfs_insert_item(trans, fs_info->extent_root,
11843 &cache->key, &cache->item,
11844 sizeof(cache->item));
11846 fprintf(stderr, "Error adding block group\n");
11849 btrfs_extent_post_op(trans, fs_info->extent_root);
11852 ret = reset_balance(trans, fs_info);
11854 fprintf(stderr, "error resetting the pending balance\n");
11859 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
11861 struct btrfs_path path;
11862 struct btrfs_trans_handle *trans;
11863 struct btrfs_key key;
11866 printf("Recowing metadata block %llu\n", eb->start);
11867 key.objectid = btrfs_header_owner(eb);
11868 key.type = BTRFS_ROOT_ITEM_KEY;
11869 key.offset = (u64)-1;
11871 root = btrfs_read_fs_root(root->fs_info, &key);
11872 if (IS_ERR(root)) {
11873 fprintf(stderr, "Couldn't find owner root %llu\n",
11875 return PTR_ERR(root);
11878 trans = btrfs_start_transaction(root, 1);
11880 return PTR_ERR(trans);
11882 btrfs_init_path(&path);
11883 path.lowest_level = btrfs_header_level(eb);
11884 if (path.lowest_level)
11885 btrfs_node_key_to_cpu(eb, &key, 0);
11887 btrfs_item_key_to_cpu(eb, &key, 0);
11889 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
11890 btrfs_commit_transaction(trans, root);
11891 btrfs_release_path(&path);
11895 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
11897 struct btrfs_path path;
11898 struct btrfs_trans_handle *trans;
11899 struct btrfs_key key;
11902 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
11903 bad->key.type, bad->key.offset);
11904 key.objectid = bad->root_id;
11905 key.type = BTRFS_ROOT_ITEM_KEY;
11906 key.offset = (u64)-1;
11908 root = btrfs_read_fs_root(root->fs_info, &key);
11909 if (IS_ERR(root)) {
11910 fprintf(stderr, "Couldn't find owner root %llu\n",
11912 return PTR_ERR(root);
11915 trans = btrfs_start_transaction(root, 1);
11917 return PTR_ERR(trans);
11919 btrfs_init_path(&path);
11920 ret = btrfs_search_slot(trans, root, &bad->key, &path, -1, 1);
11926 ret = btrfs_del_item(trans, root, &path);
11928 btrfs_commit_transaction(trans, root);
11929 btrfs_release_path(&path);
11933 static int zero_log_tree(struct btrfs_root *root)
11935 struct btrfs_trans_handle *trans;
11938 trans = btrfs_start_transaction(root, 1);
11939 if (IS_ERR(trans)) {
11940 ret = PTR_ERR(trans);
11943 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
11944 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
11945 ret = btrfs_commit_transaction(trans, root);
11949 static int populate_csum(struct btrfs_trans_handle *trans,
11950 struct btrfs_root *csum_root, char *buf, u64 start,
11957 while (offset < len) {
11958 sectorsize = csum_root->sectorsize;
11959 ret = read_extent_data(csum_root, buf, start + offset,
11963 ret = btrfs_csum_file_block(trans, csum_root, start + len,
11964 start + offset, buf, sectorsize);
11967 offset += sectorsize;
11972 static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
11973 struct btrfs_root *csum_root,
11974 struct btrfs_root *cur_root)
11976 struct btrfs_path path;
11977 struct btrfs_key key;
11978 struct extent_buffer *node;
11979 struct btrfs_file_extent_item *fi;
11986 buf = malloc(cur_root->fs_info->csum_root->sectorsize);
11990 btrfs_init_path(&path);
11994 ret = btrfs_search_slot(NULL, cur_root, &key, &path, 0, 0);
11997 /* Iterate all regular file extents and fill its csum */
11999 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
12001 if (key.type != BTRFS_EXTENT_DATA_KEY)
12003 node = path.nodes[0];
12004 slot = path.slots[0];
12005 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
12006 if (btrfs_file_extent_type(node, fi) != BTRFS_FILE_EXTENT_REG)
12008 start = btrfs_file_extent_disk_bytenr(node, fi);
12009 len = btrfs_file_extent_disk_num_bytes(node, fi);
12011 ret = populate_csum(trans, csum_root, buf, start, len);
12012 if (ret == -EEXIST)
12018 * TODO: if next leaf is corrupted, jump to nearest next valid
12021 ret = btrfs_next_item(cur_root, &path);
12031 btrfs_release_path(&path);
12036 static int fill_csum_tree_from_fs(struct btrfs_trans_handle *trans,
12037 struct btrfs_root *csum_root)
12039 struct btrfs_fs_info *fs_info = csum_root->fs_info;
12040 struct btrfs_path path;
12041 struct btrfs_root *tree_root = fs_info->tree_root;
12042 struct btrfs_root *cur_root;
12043 struct extent_buffer *node;
12044 struct btrfs_key key;
12048 btrfs_init_path(&path);
12049 key.objectid = BTRFS_FS_TREE_OBJECTID;
12051 key.type = BTRFS_ROOT_ITEM_KEY;
12052 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
12061 node = path.nodes[0];
12062 slot = path.slots[0];
12063 btrfs_item_key_to_cpu(node, &key, slot);
12064 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
12066 if (key.type != BTRFS_ROOT_ITEM_KEY)
12068 if (!is_fstree(key.objectid))
12070 key.offset = (u64)-1;
12072 cur_root = btrfs_read_fs_root(fs_info, &key);
12073 if (IS_ERR(cur_root) || !cur_root) {
12074 fprintf(stderr, "Fail to read fs/subvol tree: %lld\n",
12078 ret = fill_csum_tree_from_one_fs_root(trans, csum_root,
12083 ret = btrfs_next_item(tree_root, &path);
12093 btrfs_release_path(&path);
12097 static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans,
12098 struct btrfs_root *csum_root)
12100 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
12101 struct btrfs_path path;
12102 struct btrfs_extent_item *ei;
12103 struct extent_buffer *leaf;
12105 struct btrfs_key key;
12108 btrfs_init_path(&path);
12110 key.type = BTRFS_EXTENT_ITEM_KEY;
12112 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
12114 btrfs_release_path(&path);
12118 buf = malloc(csum_root->sectorsize);
12120 btrfs_release_path(&path);
12125 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
12126 ret = btrfs_next_leaf(extent_root, &path);
12134 leaf = path.nodes[0];
12136 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
12137 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
12142 ei = btrfs_item_ptr(leaf, path.slots[0],
12143 struct btrfs_extent_item);
12144 if (!(btrfs_extent_flags(leaf, ei) &
12145 BTRFS_EXTENT_FLAG_DATA)) {
12150 ret = populate_csum(trans, csum_root, buf, key.objectid,
12157 btrfs_release_path(&path);
12163 * Recalculate the csum and put it into the csum tree.
12165 * Extent tree init will wipe out all the extent info, so in that case, we
12166 * can't depend on extent tree, but use fs tree. If search_fs_tree is set, we
12167 * will use fs/subvol trees to init the csum tree.
12169 static int fill_csum_tree(struct btrfs_trans_handle *trans,
12170 struct btrfs_root *csum_root,
12171 int search_fs_tree)
12173 if (search_fs_tree)
12174 return fill_csum_tree_from_fs(trans, csum_root);
12176 return fill_csum_tree_from_extent(trans, csum_root);
12179 static void free_roots_info_cache(void)
12181 if (!roots_info_cache)
12184 while (!cache_tree_empty(roots_info_cache)) {
12185 struct cache_extent *entry;
12186 struct root_item_info *rii;
12188 entry = first_cache_extent(roots_info_cache);
12191 remove_cache_extent(roots_info_cache, entry);
12192 rii = container_of(entry, struct root_item_info, cache_extent);
12196 free(roots_info_cache);
12197 roots_info_cache = NULL;
12200 static int build_roots_info_cache(struct btrfs_fs_info *info)
12203 struct btrfs_key key;
12204 struct extent_buffer *leaf;
12205 struct btrfs_path path;
12207 if (!roots_info_cache) {
12208 roots_info_cache = malloc(sizeof(*roots_info_cache));
12209 if (!roots_info_cache)
12211 cache_tree_init(roots_info_cache);
12214 btrfs_init_path(&path);
12216 key.type = BTRFS_EXTENT_ITEM_KEY;
12218 ret = btrfs_search_slot(NULL, info->extent_root, &key, &path, 0, 0);
12221 leaf = path.nodes[0];
12224 struct btrfs_key found_key;
12225 struct btrfs_extent_item *ei;
12226 struct btrfs_extent_inline_ref *iref;
12227 int slot = path.slots[0];
12232 struct cache_extent *entry;
12233 struct root_item_info *rii;
12235 if (slot >= btrfs_header_nritems(leaf)) {
12236 ret = btrfs_next_leaf(info->extent_root, &path);
12243 leaf = path.nodes[0];
12244 slot = path.slots[0];
12247 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12249 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
12250 found_key.type != BTRFS_METADATA_ITEM_KEY)
12253 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
12254 flags = btrfs_extent_flags(leaf, ei);
12256 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
12257 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
12260 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
12261 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
12262 level = found_key.offset;
12264 struct btrfs_tree_block_info *binfo;
12266 binfo = (struct btrfs_tree_block_info *)(ei + 1);
12267 iref = (struct btrfs_extent_inline_ref *)(binfo + 1);
12268 level = btrfs_tree_block_level(leaf, binfo);
12272 * For a root extent, it must be of the following type and the
12273 * first (and only one) iref in the item.
12275 type = btrfs_extent_inline_ref_type(leaf, iref);
12276 if (type != BTRFS_TREE_BLOCK_REF_KEY)
12279 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
12280 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12282 rii = malloc(sizeof(struct root_item_info));
12287 rii->cache_extent.start = root_id;
12288 rii->cache_extent.size = 1;
12289 rii->level = (u8)-1;
12290 entry = &rii->cache_extent;
12291 ret = insert_cache_extent(roots_info_cache, entry);
12294 rii = container_of(entry, struct root_item_info,
12298 ASSERT(rii->cache_extent.start == root_id);
12299 ASSERT(rii->cache_extent.size == 1);
12301 if (level > rii->level || rii->level == (u8)-1) {
12302 rii->level = level;
12303 rii->bytenr = found_key.objectid;
12304 rii->gen = btrfs_extent_generation(leaf, ei);
12305 rii->node_count = 1;
12306 } else if (level == rii->level) {
12314 btrfs_release_path(&path);
12319 static int maybe_repair_root_item(struct btrfs_fs_info *info,
12320 struct btrfs_path *path,
12321 const struct btrfs_key *root_key,
12322 const int read_only_mode)
12324 const u64 root_id = root_key->objectid;
12325 struct cache_extent *entry;
12326 struct root_item_info *rii;
12327 struct btrfs_root_item ri;
12328 unsigned long offset;
12330 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12333 "Error: could not find extent items for root %llu\n",
12334 root_key->objectid);
12338 rii = container_of(entry, struct root_item_info, cache_extent);
12339 ASSERT(rii->cache_extent.start == root_id);
12340 ASSERT(rii->cache_extent.size == 1);
12342 if (rii->node_count != 1) {
12344 "Error: could not find btree root extent for root %llu\n",
12349 offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
12350 read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
12352 if (btrfs_root_bytenr(&ri) != rii->bytenr ||
12353 btrfs_root_level(&ri) != rii->level ||
12354 btrfs_root_generation(&ri) != rii->gen) {
12357 * If we're in repair mode but our caller told us to not update
12358 * the root item, i.e. just check if it needs to be updated, don't
12359 * print this message, since the caller will call us again shortly
12360 * for the same root item without read only mode (the caller will
12361 * open a transaction first).
12363 if (!(read_only_mode && repair))
12365 "%sroot item for root %llu,"
12366 " current bytenr %llu, current gen %llu, current level %u,"
12367 " new bytenr %llu, new gen %llu, new level %u\n",
12368 (read_only_mode ? "" : "fixing "),
12370 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
12371 btrfs_root_level(&ri),
12372 rii->bytenr, rii->gen, rii->level);
12374 if (btrfs_root_generation(&ri) > rii->gen) {
12376 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
12377 root_id, btrfs_root_generation(&ri), rii->gen);
12381 if (!read_only_mode) {
12382 btrfs_set_root_bytenr(&ri, rii->bytenr);
12383 btrfs_set_root_level(&ri, rii->level);
12384 btrfs_set_root_generation(&ri, rii->gen);
12385 write_extent_buffer(path->nodes[0], &ri,
12386 offset, sizeof(ri));
12396 * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
12397 * caused read-only snapshots to be corrupted if they were created at a moment
12398 * when the source subvolume/snapshot had orphan items. The issue was that the
12399 * on-disk root items became incorrect, referring to the pre orphan cleanup root
12400 * node instead of the post orphan cleanup root node.
12401 * So this function, and its callees, just detects and fixes those cases. Even
12402 * though the regression was for read-only snapshots, this function applies to
12403 * any snapshot/subvolume root.
12404 * This must be run before any other repair code - not doing it so, makes other
12405 * repair code delete or modify backrefs in the extent tree for example, which
12406 * will result in an inconsistent fs after repairing the root items.
12408 static int repair_root_items(struct btrfs_fs_info *info)
12410 struct btrfs_path path;
12411 struct btrfs_key key;
12412 struct extent_buffer *leaf;
12413 struct btrfs_trans_handle *trans = NULL;
12416 int need_trans = 0;
12418 btrfs_init_path(&path);
12420 ret = build_roots_info_cache(info);
12424 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
12425 key.type = BTRFS_ROOT_ITEM_KEY;
12430 * Avoid opening and committing transactions if a leaf doesn't have
12431 * any root items that need to be fixed, so that we avoid rotating
12432 * backup roots unnecessarily.
12435 trans = btrfs_start_transaction(info->tree_root, 1);
12436 if (IS_ERR(trans)) {
12437 ret = PTR_ERR(trans);
12442 ret = btrfs_search_slot(trans, info->tree_root, &key, &path,
12446 leaf = path.nodes[0];
12449 struct btrfs_key found_key;
12451 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
12452 int no_more_keys = find_next_key(&path, &key);
12454 btrfs_release_path(&path);
12456 ret = btrfs_commit_transaction(trans,
12468 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12470 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
12472 if (found_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
12475 ret = maybe_repair_root_item(info, &path, &found_key,
12480 if (!trans && repair) {
12483 btrfs_release_path(&path);
12493 free_roots_info_cache();
12494 btrfs_release_path(&path);
12496 btrfs_commit_transaction(trans, info->tree_root);
12503 static int clear_free_space_cache(struct btrfs_fs_info *fs_info)
12505 struct btrfs_trans_handle *trans;
12506 struct btrfs_block_group_cache *bg_cache;
12510 /* Clear all free space cache inodes and its extent data */
12512 bg_cache = btrfs_lookup_first_block_group(fs_info, current);
12515 ret = btrfs_clear_free_space_cache(fs_info, bg_cache);
12518 current = bg_cache->key.objectid + bg_cache->key.offset;
12521 /* Don't forget to set cache_generation to -1 */
12522 trans = btrfs_start_transaction(fs_info->tree_root, 0);
12523 if (IS_ERR(trans)) {
12524 error("failed to update super block cache generation");
12525 return PTR_ERR(trans);
12527 btrfs_set_super_cache_generation(fs_info->super_copy, (u64)-1);
12528 btrfs_commit_transaction(trans, fs_info->tree_root);
12533 const char * const cmd_check_usage[] = {
12534 "btrfs check [options] <device>",
12535 "Check structural integrity of a filesystem (unmounted).",
12536 "Check structural integrity of an unmounted filesystem. Verify internal",
12537 "trees' consistency and item connectivity. In the repair mode try to",
12538 "fix the problems found. ",
12539 "WARNING: the repair mode is considered dangerous",
12541 "-s|--super <superblock> use this superblock copy",
12542 "-b|--backup use the first valid backup root copy",
12543 "--repair try to repair the filesystem",
12544 "--readonly run in read-only mode (default)",
12545 "--init-csum-tree create a new CRC tree",
12546 "--init-extent-tree create a new extent tree",
12547 "--mode <MODE> allows choice of memory/IO trade-offs",
12548 " where MODE is one of:",
12549 " original - read inodes and extents to memory (requires",
12550 " more memory, does less IO)",
12551 " lowmem - try to use less memory but read blocks again",
12553 "--check-data-csum verify checksums of data blocks",
12554 "-Q|--qgroup-report print a report on qgroup consistency",
12555 "-E|--subvol-extents <subvolid>",
12556 " print subvolume extents and sharing state",
12557 "-r|--tree-root <bytenr> use the given bytenr for the tree root",
12558 "--chunk-root <bytenr> use the given bytenr for the chunk tree root",
12559 "-p|--progress indicate progress",
12560 "--clear-space-cache v1|v2 clear space cache for v1 or v2",
12564 int cmd_check(int argc, char **argv)
12566 struct cache_tree root_cache;
12567 struct btrfs_root *root;
12568 struct btrfs_fs_info *info;
12571 u64 tree_root_bytenr = 0;
12572 u64 chunk_root_bytenr = 0;
12573 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
12577 int init_csum_tree = 0;
12579 int clear_space_cache = 0;
12580 int qgroup_report = 0;
12581 int qgroups_repaired = 0;
12582 unsigned ctree_flags = OPEN_CTREE_EXCLUSIVE;
12586 enum { GETOPT_VAL_REPAIR = 257, GETOPT_VAL_INIT_CSUM,
12587 GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
12588 GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE,
12589 GETOPT_VAL_MODE, GETOPT_VAL_CLEAR_SPACE_CACHE };
12590 static const struct option long_options[] = {
12591 { "super", required_argument, NULL, 's' },
12592 { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
12593 { "readonly", no_argument, NULL, GETOPT_VAL_READONLY },
12594 { "init-csum-tree", no_argument, NULL,
12595 GETOPT_VAL_INIT_CSUM },
12596 { "init-extent-tree", no_argument, NULL,
12597 GETOPT_VAL_INIT_EXTENT },
12598 { "check-data-csum", no_argument, NULL,
12599 GETOPT_VAL_CHECK_CSUM },
12600 { "backup", no_argument, NULL, 'b' },
12601 { "subvol-extents", required_argument, NULL, 'E' },
12602 { "qgroup-report", no_argument, NULL, 'Q' },
12603 { "tree-root", required_argument, NULL, 'r' },
12604 { "chunk-root", required_argument, NULL,
12605 GETOPT_VAL_CHUNK_TREE },
12606 { "progress", no_argument, NULL, 'p' },
12607 { "mode", required_argument, NULL,
12609 { "clear-space-cache", required_argument, NULL,
12610 GETOPT_VAL_CLEAR_SPACE_CACHE},
12611 { NULL, 0, NULL, 0}
12614 c = getopt_long(argc, argv, "as:br:p", long_options, NULL);
12618 case 'a': /* ignored */ break;
12620 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
12623 num = arg_strtou64(optarg);
12624 if (num >= BTRFS_SUPER_MIRROR_MAX) {
12626 "super mirror should be less than %d",
12627 BTRFS_SUPER_MIRROR_MAX);
12630 bytenr = btrfs_sb_offset(((int)num));
12631 printf("using SB copy %llu, bytenr %llu\n", num,
12632 (unsigned long long)bytenr);
12638 subvolid = arg_strtou64(optarg);
12641 tree_root_bytenr = arg_strtou64(optarg);
12643 case GETOPT_VAL_CHUNK_TREE:
12644 chunk_root_bytenr = arg_strtou64(optarg);
12647 ctx.progress_enabled = true;
12651 usage(cmd_check_usage);
12652 case GETOPT_VAL_REPAIR:
12653 printf("enabling repair mode\n");
12655 ctree_flags |= OPEN_CTREE_WRITES;
12657 case GETOPT_VAL_READONLY:
12660 case GETOPT_VAL_INIT_CSUM:
12661 printf("Creating a new CRC tree\n");
12662 init_csum_tree = 1;
12664 ctree_flags |= OPEN_CTREE_WRITES;
12666 case GETOPT_VAL_INIT_EXTENT:
12667 init_extent_tree = 1;
12668 ctree_flags |= (OPEN_CTREE_WRITES |
12669 OPEN_CTREE_NO_BLOCK_GROUPS);
12672 case GETOPT_VAL_CHECK_CSUM:
12673 check_data_csum = 1;
12675 case GETOPT_VAL_MODE:
12676 check_mode = parse_check_mode(optarg);
12677 if (check_mode == CHECK_MODE_UNKNOWN) {
12678 error("unknown mode: %s", optarg);
12682 case GETOPT_VAL_CLEAR_SPACE_CACHE:
12683 if (strcmp(optarg, "v1") == 0) {
12684 clear_space_cache = 1;
12685 } else if (strcmp(optarg, "v2") == 0) {
12686 clear_space_cache = 2;
12687 ctree_flags |= OPEN_CTREE_INVALIDATE_FST;
12690 "invalid argument to --clear-space-cache, must be v1 or v2");
12693 ctree_flags |= OPEN_CTREE_WRITES;
12698 if (check_argc_exact(argc - optind, 1))
12699 usage(cmd_check_usage);
12701 if (ctx.progress_enabled) {
12702 ctx.tp = TASK_NOTHING;
12703 ctx.info = task_init(print_status_check, print_status_return, &ctx);
12706 /* This check is the only reason for --readonly to exist */
12707 if (readonly && repair) {
12708 error("repair options are not compatible with --readonly");
12713 * Not supported yet
12715 if (repair && check_mode == CHECK_MODE_LOWMEM) {
12716 error("low memory mode doesn't support repair yet");
12721 cache_tree_init(&root_cache);
12723 if((ret = check_mounted(argv[optind])) < 0) {
12724 error("could not check mount status: %s", strerror(-ret));
12728 error("%s is currently mounted, aborting", argv[optind]);
12734 /* only allow partial opening under repair mode */
12736 ctree_flags |= OPEN_CTREE_PARTIAL;
12738 info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
12739 chunk_root_bytenr, ctree_flags);
12741 error("cannot open file system");
12747 global_info = info;
12748 root = info->fs_root;
12749 if (clear_space_cache == 1) {
12750 if (btrfs_fs_compat_ro(info,
12751 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
12753 "free space cache v2 detected, use --clear-space-cache v2");
12757 printf("Clearing free space cache\n");
12758 ret = clear_free_space_cache(info);
12760 error("failed to clear free space cache");
12763 printf("Free space cache cleared\n");
12766 } else if (clear_space_cache == 2) {
12767 if (!btrfs_fs_compat_ro(info,
12768 BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
12769 printf("no free space cache v2 to clear\n");
12773 printf("Clear free space cache v2\n");
12774 ret = btrfs_clear_free_space_tree(info);
12776 error("failed to clear free space cache v2: %d", ret);
12779 printf("free space cache v2 cleared\n");
12785 * repair mode will force us to commit transaction which
12786 * will make us fail to load log tree when mounting.
12788 if (repair && btrfs_super_log_root(info->super_copy)) {
12789 ret = ask_user("repair mode will force to clear out log tree, are you sure?");
12795 ret = zero_log_tree(root);
12798 error("failed to zero log tree: %d", ret);
12803 uuid_unparse(info->super_copy->fsid, uuidbuf);
12804 if (qgroup_report) {
12805 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
12807 ret = qgroup_verify_all(info);
12814 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
12815 subvolid, argv[optind], uuidbuf);
12816 ret = print_extent_state(info, subvolid);
12820 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
12822 if (!extent_buffer_uptodate(info->tree_root->node) ||
12823 !extent_buffer_uptodate(info->dev_root->node) ||
12824 !extent_buffer_uptodate(info->chunk_root->node)) {
12825 error("critical roots corrupted, unable to check the filesystem");
12831 if (init_extent_tree || init_csum_tree) {
12832 struct btrfs_trans_handle *trans;
12834 trans = btrfs_start_transaction(info->extent_root, 0);
12835 if (IS_ERR(trans)) {
12836 error("error starting transaction");
12837 ret = PTR_ERR(trans);
12842 if (init_extent_tree) {
12843 printf("Creating a new extent tree\n");
12844 ret = reinit_extent_tree(trans, info);
12850 if (init_csum_tree) {
12851 printf("Reinitialize checksum tree\n");
12852 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
12854 error("checksum tree initialization failed: %d",
12861 ret = fill_csum_tree(trans, info->csum_root,
12865 error("checksum tree refilling failed: %d", ret);
12870 * Ok now we commit and run the normal fsck, which will add
12871 * extent entries for all of the items it finds.
12873 ret = btrfs_commit_transaction(trans, info->extent_root);
12878 if (!extent_buffer_uptodate(info->extent_root->node)) {
12879 error("critical: extent_root, unable to check the filesystem");
12884 if (!extent_buffer_uptodate(info->csum_root->node)) {
12885 error("critical: csum_root, unable to check the filesystem");
12891 if (!ctx.progress_enabled)
12892 fprintf(stderr, "checking extents\n");
12893 if (check_mode == CHECK_MODE_LOWMEM)
12894 ret = check_chunks_and_extents_v2(root);
12896 ret = check_chunks_and_extents(root);
12900 "errors found in extent allocation tree or chunk allocation");
12902 ret = repair_root_items(info);
12907 fprintf(stderr, "Fixed %d roots.\n", ret);
12909 } else if (ret > 0) {
12911 "Found %d roots with an outdated root item.\n",
12914 "Please run a filesystem check with the option --repair to fix them.\n");
12920 if (!ctx.progress_enabled) {
12921 if (btrfs_fs_compat_ro(info, BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE))
12922 fprintf(stderr, "checking free space tree\n");
12924 fprintf(stderr, "checking free space cache\n");
12926 ret = check_space_cache(root);
12932 * We used to have to have these hole extents in between our real
12933 * extents so if we don't have this flag set we need to make sure there
12934 * are no gaps in the file extents for inodes, otherwise we can just
12935 * ignore it when this happens.
12937 no_holes = btrfs_fs_incompat(root->fs_info,
12938 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
12939 if (!ctx.progress_enabled)
12940 fprintf(stderr, "checking fs roots\n");
12941 if (check_mode == CHECK_MODE_LOWMEM)
12942 ret = check_fs_roots_v2(root->fs_info);
12944 ret = check_fs_roots(root, &root_cache);
12949 fprintf(stderr, "checking csums\n");
12950 ret = check_csums(root);
12955 fprintf(stderr, "checking root refs\n");
12956 /* For low memory mode, check_fs_roots_v2 handles root refs */
12957 if (check_mode != CHECK_MODE_LOWMEM) {
12958 ret = check_root_refs(root, &root_cache);
12964 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
12965 struct extent_buffer *eb;
12967 eb = list_first_entry(&root->fs_info->recow_ebs,
12968 struct extent_buffer, recow);
12969 list_del_init(&eb->recow);
12970 ret = recow_extent_buffer(root, eb);
12976 while (!list_empty(&delete_items)) {
12977 struct bad_item *bad;
12979 bad = list_first_entry(&delete_items, struct bad_item, list);
12980 list_del_init(&bad->list);
12982 ret = delete_bad_item(root, bad);
12988 if (info->quota_enabled) {
12989 fprintf(stderr, "checking quota groups\n");
12990 ret = qgroup_verify_all(info);
12995 ret = repair_qgroups(info, &qgroups_repaired);
13002 if (!list_empty(&root->fs_info->recow_ebs)) {
13003 error("transid errors in file system");
13008 if (found_old_backref) { /*
13009 * there was a disk format change when mixed
13010 * backref was in testing tree. The old format
13011 * existed about one week.
13013 printf("\n * Found old mixed backref format. "
13014 "The old format is not supported! *"
13015 "\n * Please mount the FS in readonly mode, "
13016 "backup data and re-format the FS. *\n\n");
13019 printf("found %llu bytes used err is %d\n",
13020 (unsigned long long)bytes_used, ret);
13021 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
13022 printf("total tree bytes: %llu\n",
13023 (unsigned long long)total_btree_bytes);
13024 printf("total fs tree bytes: %llu\n",
13025 (unsigned long long)total_fs_tree_bytes);
13026 printf("total extent tree bytes: %llu\n",
13027 (unsigned long long)total_extent_tree_bytes);
13028 printf("btree space waste bytes: %llu\n",
13029 (unsigned long long)btree_space_waste);
13030 printf("file data blocks allocated: %llu\n referenced %llu\n",
13031 (unsigned long long)data_bytes_allocated,
13032 (unsigned long long)data_bytes_referenced);
13034 free_qgroup_counts();
13035 free_root_recs_tree(&root_cache);
13039 if (ctx.progress_enabled)
13040 task_deinit(ctx.info);