2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
23 #include <sys/types.h>
27 #include <uuid/uuid.h>
32 #include "print-tree.h"
33 #include "task-utils.h"
34 #include "transaction.h"
37 #include "free-space-cache.h"
38 #include "free-space-tree.h"
40 #include "qgroup-verify.h"
41 #include "rbtree-utils.h"
43 #include "kernel-shared/ulist.h"
51 TASK_NOTHING, /* have to be the last element */
56 enum task_position tp;
58 struct task_info *info;
61 static u64 bytes_used = 0;
62 static u64 total_csum_bytes = 0;
63 static u64 total_btree_bytes = 0;
64 static u64 total_fs_tree_bytes = 0;
65 static u64 total_extent_tree_bytes = 0;
66 static u64 btree_space_waste = 0;
67 static u64 data_bytes_allocated = 0;
68 static u64 data_bytes_referenced = 0;
69 static int found_old_backref = 0;
70 static LIST_HEAD(duplicate_extents);
71 static LIST_HEAD(delete_items);
72 static int no_holes = 0;
73 static int init_extent_tree = 0;
74 static int check_data_csum = 0;
75 static struct btrfs_fs_info *global_info;
76 static struct task_ctx ctx = { 0 };
77 static struct cache_tree *roots_info_cache = NULL;
79 enum btrfs_check_mode {
83 CHECK_MODE_DEFAULT = CHECK_MODE_ORIGINAL
86 static enum btrfs_check_mode check_mode = CHECK_MODE_DEFAULT;
88 struct extent_backref {
89 struct list_head list;
90 unsigned int is_data:1;
91 unsigned int found_extent_tree:1;
92 unsigned int full_backref:1;
93 unsigned int found_ref:1;
94 unsigned int broken:1;
97 static inline struct extent_backref* to_extent_backref(struct list_head *entry)
99 return list_entry(entry, struct extent_backref, list);
102 struct data_backref {
103 struct extent_backref node;
117 #define ROOT_DIR_ERROR (1<<1) /* bad ROOT_DIR */
118 #define DIR_ITEM_MISSING (1<<2) /* DIR_ITEM not found */
119 #define DIR_ITEM_MISMATCH (1<<3) /* DIR_ITEM found but not match */
120 #define INODE_REF_MISSING (1<<4) /* INODE_REF/INODE_EXTREF not found */
121 #define INODE_ITEM_MISSING (1<<5) /* INODE_ITEM not found */
122 #define INODE_ITEM_MISMATCH (1<<6) /* INODE_ITEM found but not match */
123 #define FILE_EXTENT_ERROR (1<<7) /* bad FILE_EXTENT */
124 #define ODD_CSUM_ITEM (1<<8) /* CSUM_ITEM error */
125 #define CSUM_ITEM_MISSING (1<<9) /* CSUM_ITEM not found */
126 #define LINK_COUNT_ERROR (1<<10) /* INODE_ITEM nlink count error */
127 #define NBYTES_ERROR (1<<11) /* INODE_ITEM nbytes count error */
128 #define ISIZE_ERROR (1<<12) /* INODE_ITEM size count error */
129 #define ORPHAN_ITEM (1<<13) /* INODE_ITEM no reference */
130 #define NO_INODE_ITEM (1<<14) /* no inode_item */
131 #define LAST_ITEM (1<<15) /* Complete this tree traversal */
132 #define ROOT_REF_MISSING (1<<16) /* ROOT_REF not found */
133 #define ROOT_REF_MISMATCH (1<<17) /* ROOT_REF found but not match */
135 static inline struct data_backref* to_data_backref(struct extent_backref *back)
137 return container_of(back, struct data_backref, node);
141 * Much like data_backref, just removed the undetermined members
142 * and change it to use list_head.
143 * During extent scan, it is stored in root->orphan_data_extent.
144 * During fs tree scan, it is then moved to inode_rec->orphan_data_extents.
146 struct orphan_data_extent {
147 struct list_head list;
155 struct tree_backref {
156 struct extent_backref node;
163 static inline struct tree_backref* to_tree_backref(struct extent_backref *back)
165 return container_of(back, struct tree_backref, node);
168 /* Explicit initialization for extent_record::flag_block_full_backref */
169 enum { FLAG_UNSET = 2 };
171 struct extent_record {
172 struct list_head backrefs;
173 struct list_head dups;
174 struct list_head list;
175 struct cache_extent cache;
176 struct btrfs_disk_key parent_key;
181 u64 extent_item_refs;
183 u64 parent_generation;
187 unsigned int flag_block_full_backref:2;
188 unsigned int found_rec:1;
189 unsigned int content_checked:1;
190 unsigned int owner_ref_checked:1;
191 unsigned int is_root:1;
192 unsigned int metadata:1;
193 unsigned int bad_full_backref:1;
194 unsigned int crossing_stripes:1;
195 unsigned int wrong_chunk_type:1;
198 static inline struct extent_record* to_extent_record(struct list_head *entry)
200 return container_of(entry, struct extent_record, list);
203 struct inode_backref {
204 struct list_head list;
205 unsigned int found_dir_item:1;
206 unsigned int found_dir_index:1;
207 unsigned int found_inode_ref:1;
217 static inline struct inode_backref* to_inode_backref(struct list_head *entry)
219 return list_entry(entry, struct inode_backref, list);
222 struct root_item_record {
223 struct list_head list;
230 struct btrfs_key drop_key;
233 #define REF_ERR_NO_DIR_ITEM (1 << 0)
234 #define REF_ERR_NO_DIR_INDEX (1 << 1)
235 #define REF_ERR_NO_INODE_REF (1 << 2)
236 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
237 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
238 #define REF_ERR_DUP_INODE_REF (1 << 5)
239 #define REF_ERR_INDEX_UNMATCH (1 << 6)
240 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
241 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
242 #define REF_ERR_NO_ROOT_REF (1 << 9)
243 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
244 #define REF_ERR_DUP_ROOT_REF (1 << 11)
245 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
247 struct file_extent_hole {
253 struct inode_record {
254 struct list_head backrefs;
255 unsigned int checked:1;
256 unsigned int merging:1;
257 unsigned int found_inode_item:1;
258 unsigned int found_dir_item:1;
259 unsigned int found_file_extent:1;
260 unsigned int found_csum_item:1;
261 unsigned int some_csum_missing:1;
262 unsigned int nodatasum:1;
275 struct rb_root holes;
276 struct list_head orphan_extents;
281 #define I_ERR_NO_INODE_ITEM (1 << 0)
282 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
283 #define I_ERR_DUP_INODE_ITEM (1 << 2)
284 #define I_ERR_DUP_DIR_INDEX (1 << 3)
285 #define I_ERR_ODD_DIR_ITEM (1 << 4)
286 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
287 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
288 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
289 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
290 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
291 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
292 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
293 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
294 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
295 #define I_ERR_FILE_EXTENT_ORPHAN (1 << 14)
297 struct root_backref {
298 struct list_head list;
299 unsigned int found_dir_item:1;
300 unsigned int found_dir_index:1;
301 unsigned int found_back_ref:1;
302 unsigned int found_forward_ref:1;
303 unsigned int reachable:1;
312 static inline struct root_backref* to_root_backref(struct list_head *entry)
314 return list_entry(entry, struct root_backref, list);
318 struct list_head backrefs;
319 struct cache_extent cache;
320 unsigned int found_root_item:1;
326 struct cache_extent cache;
331 struct cache_extent cache;
332 struct cache_tree root_cache;
333 struct cache_tree inode_cache;
334 struct inode_record *current;
343 struct walk_control {
344 struct cache_tree shared;
345 struct shared_node *nodes[BTRFS_MAX_LEVEL];
351 struct btrfs_key key;
353 struct list_head list;
356 struct extent_entry {
361 struct list_head list;
364 struct root_item_info {
365 /* level of the root */
367 /* number of nodes at this level, must be 1 for a root */
371 struct cache_extent cache_extent;
375 * Error bit for low memory mode check.
377 * Currently no caller cares about it yet. Just internal use for error
380 #define BACKREF_MISSING (1 << 0) /* Backref missing in extent tree */
381 #define BACKREF_MISMATCH (1 << 1) /* Backref exists but does not match */
382 #define BYTES_UNALIGNED (1 << 2) /* Some bytes are not aligned */
383 #define REFERENCER_MISSING (1 << 3) /* Referencer not found */
384 #define REFERENCER_MISMATCH (1 << 4) /* Referenceer found but does not match */
385 #define CROSSING_STRIPE_BOUNDARY (1 << 4) /* For kernel scrub workaround */
386 #define ITEM_SIZE_MISMATCH (1 << 5) /* Bad item size */
387 #define UNKNOWN_TYPE (1 << 6) /* Unknown type */
388 #define ACCOUNTING_MISMATCH (1 << 7) /* Used space accounting error */
389 #define CHUNK_TYPE_MISMATCH (1 << 8)
391 static void *print_status_check(void *p)
393 struct task_ctx *priv = p;
394 const char work_indicator[] = { '.', 'o', 'O', 'o' };
396 static char *task_position_string[] = {
398 "checking free space cache",
402 task_period_start(priv->info, 1000 /* 1s */);
404 if (priv->tp == TASK_NOTHING)
408 printf("%s [%c]\r", task_position_string[priv->tp],
409 work_indicator[count % 4]);
412 task_period_wait(priv->info);
417 static int print_status_return(void *p)
425 static enum btrfs_check_mode parse_check_mode(const char *str)
427 if (strcmp(str, "lowmem") == 0)
428 return CHECK_MODE_LOWMEM;
429 if (strcmp(str, "orig") == 0)
430 return CHECK_MODE_ORIGINAL;
431 if (strcmp(str, "original") == 0)
432 return CHECK_MODE_ORIGINAL;
434 return CHECK_MODE_UNKNOWN;
437 /* Compatible function to allow reuse of old codes */
438 static u64 first_extent_gap(struct rb_root *holes)
440 struct file_extent_hole *hole;
442 if (RB_EMPTY_ROOT(holes))
445 hole = rb_entry(rb_first(holes), struct file_extent_hole, node);
449 static int compare_hole(struct rb_node *node1, struct rb_node *node2)
451 struct file_extent_hole *hole1;
452 struct file_extent_hole *hole2;
454 hole1 = rb_entry(node1, struct file_extent_hole, node);
455 hole2 = rb_entry(node2, struct file_extent_hole, node);
457 if (hole1->start > hole2->start)
459 if (hole1->start < hole2->start)
461 /* Now hole1->start == hole2->start */
462 if (hole1->len >= hole2->len)
464 * Hole 1 will be merge center
465 * Same hole will be merged later
468 /* Hole 2 will be merge center */
473 * Add a hole to the record
475 * This will do hole merge for copy_file_extent_holes(),
476 * which will ensure there won't be continuous holes.
478 static int add_file_extent_hole(struct rb_root *holes,
481 struct file_extent_hole *hole;
482 struct file_extent_hole *prev = NULL;
483 struct file_extent_hole *next = NULL;
485 hole = malloc(sizeof(*hole));
490 /* Since compare will not return 0, no -EEXIST will happen */
491 rb_insert(holes, &hole->node, compare_hole);
493 /* simple merge with previous hole */
494 if (rb_prev(&hole->node))
495 prev = rb_entry(rb_prev(&hole->node), struct file_extent_hole,
497 if (prev && prev->start + prev->len >= hole->start) {
498 hole->len = hole->start + hole->len - prev->start;
499 hole->start = prev->start;
500 rb_erase(&prev->node, holes);
505 /* iterate merge with next holes */
507 if (!rb_next(&hole->node))
509 next = rb_entry(rb_next(&hole->node), struct file_extent_hole,
511 if (hole->start + hole->len >= next->start) {
512 if (hole->start + hole->len <= next->start + next->len)
513 hole->len = next->start + next->len -
515 rb_erase(&next->node, holes);
524 static int compare_hole_range(struct rb_node *node, void *data)
526 struct file_extent_hole *hole;
529 hole = (struct file_extent_hole *)data;
532 hole = rb_entry(node, struct file_extent_hole, node);
533 if (start < hole->start)
535 if (start >= hole->start && start < hole->start + hole->len)
541 * Delete a hole in the record
543 * This will do the hole split and is much restrict than add.
545 static int del_file_extent_hole(struct rb_root *holes,
548 struct file_extent_hole *hole;
549 struct file_extent_hole tmp;
554 struct rb_node *node;
561 node = rb_search(holes, &tmp, compare_hole_range, NULL);
564 hole = rb_entry(node, struct file_extent_hole, node);
565 if (start + len > hole->start + hole->len)
569 * Now there will be no overlap, delete the hole and re-add the
570 * split(s) if they exists.
572 if (start > hole->start) {
573 prev_start = hole->start;
574 prev_len = start - hole->start;
577 if (hole->start + hole->len > start + len) {
578 next_start = start + len;
579 next_len = hole->start + hole->len - start - len;
582 rb_erase(node, holes);
585 ret = add_file_extent_hole(holes, prev_start, prev_len);
590 ret = add_file_extent_hole(holes, next_start, next_len);
597 static int copy_file_extent_holes(struct rb_root *dst,
600 struct file_extent_hole *hole;
601 struct rb_node *node;
604 node = rb_first(src);
606 hole = rb_entry(node, struct file_extent_hole, node);
607 ret = add_file_extent_hole(dst, hole->start, hole->len);
610 node = rb_next(node);
615 static void free_file_extent_holes(struct rb_root *holes)
617 struct rb_node *node;
618 struct file_extent_hole *hole;
620 node = rb_first(holes);
622 hole = rb_entry(node, struct file_extent_hole, node);
623 rb_erase(node, holes);
625 node = rb_first(holes);
629 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
631 static void record_root_in_trans(struct btrfs_trans_handle *trans,
632 struct btrfs_root *root)
634 if (root->last_trans != trans->transid) {
635 root->track_dirty = 1;
636 root->last_trans = trans->transid;
637 root->commit_root = root->node;
638 extent_buffer_get(root->node);
642 static u8 imode_to_type(u32 imode)
645 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
646 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
647 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
648 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
649 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
650 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
651 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
652 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
655 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
659 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
661 struct device_record *rec1;
662 struct device_record *rec2;
664 rec1 = rb_entry(node1, struct device_record, node);
665 rec2 = rb_entry(node2, struct device_record, node);
666 if (rec1->devid > rec2->devid)
668 else if (rec1->devid < rec2->devid)
674 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
676 struct inode_record *rec;
677 struct inode_backref *backref;
678 struct inode_backref *orig;
679 struct inode_backref *tmp;
680 struct orphan_data_extent *src_orphan;
681 struct orphan_data_extent *dst_orphan;
686 rec = malloc(sizeof(*rec));
688 return ERR_PTR(-ENOMEM);
689 memcpy(rec, orig_rec, sizeof(*rec));
691 INIT_LIST_HEAD(&rec->backrefs);
692 INIT_LIST_HEAD(&rec->orphan_extents);
693 rec->holes = RB_ROOT;
695 list_for_each_entry(orig, &orig_rec->backrefs, list) {
696 size = sizeof(*orig) + orig->namelen + 1;
697 backref = malloc(size);
702 memcpy(backref, orig, size);
703 list_add_tail(&backref->list, &rec->backrefs);
705 list_for_each_entry(src_orphan, &orig_rec->orphan_extents, list) {
706 dst_orphan = malloc(sizeof(*dst_orphan));
711 memcpy(dst_orphan, src_orphan, sizeof(*src_orphan));
712 list_add_tail(&dst_orphan->list, &rec->orphan_extents);
714 ret = copy_file_extent_holes(&rec->holes, &orig_rec->holes);
721 rb = rb_first(&rec->holes);
723 struct file_extent_hole *hole;
725 hole = rb_entry(rb, struct file_extent_hole, node);
731 if (!list_empty(&rec->backrefs))
732 list_for_each_entry_safe(orig, tmp, &rec->backrefs, list) {
733 list_del(&orig->list);
737 if (!list_empty(&rec->orphan_extents))
738 list_for_each_entry_safe(orig, tmp, &rec->orphan_extents, list) {
739 list_del(&orig->list);
748 static void print_orphan_data_extents(struct list_head *orphan_extents,
751 struct orphan_data_extent *orphan;
753 if (list_empty(orphan_extents))
755 printf("The following data extent is lost in tree %llu:\n",
757 list_for_each_entry(orphan, orphan_extents, list) {
758 printf("\tinode: %llu, offset:%llu, disk_bytenr: %llu, disk_len: %llu\n",
759 orphan->objectid, orphan->offset, orphan->disk_bytenr,
764 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
766 u64 root_objectid = root->root_key.objectid;
767 int errors = rec->errors;
771 /* reloc root errors, we print its corresponding fs root objectid*/
772 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
773 root_objectid = root->root_key.offset;
774 fprintf(stderr, "reloc");
776 fprintf(stderr, "root %llu inode %llu errors %x",
777 (unsigned long long) root_objectid,
778 (unsigned long long) rec->ino, rec->errors);
780 if (errors & I_ERR_NO_INODE_ITEM)
781 fprintf(stderr, ", no inode item");
782 if (errors & I_ERR_NO_ORPHAN_ITEM)
783 fprintf(stderr, ", no orphan item");
784 if (errors & I_ERR_DUP_INODE_ITEM)
785 fprintf(stderr, ", dup inode item");
786 if (errors & I_ERR_DUP_DIR_INDEX)
787 fprintf(stderr, ", dup dir index");
788 if (errors & I_ERR_ODD_DIR_ITEM)
789 fprintf(stderr, ", odd dir item");
790 if (errors & I_ERR_ODD_FILE_EXTENT)
791 fprintf(stderr, ", odd file extent");
792 if (errors & I_ERR_BAD_FILE_EXTENT)
793 fprintf(stderr, ", bad file extent");
794 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
795 fprintf(stderr, ", file extent overlap");
796 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
797 fprintf(stderr, ", file extent discount");
798 if (errors & I_ERR_DIR_ISIZE_WRONG)
799 fprintf(stderr, ", dir isize wrong");
800 if (errors & I_ERR_FILE_NBYTES_WRONG)
801 fprintf(stderr, ", nbytes wrong");
802 if (errors & I_ERR_ODD_CSUM_ITEM)
803 fprintf(stderr, ", odd csum item");
804 if (errors & I_ERR_SOME_CSUM_MISSING)
805 fprintf(stderr, ", some csum missing");
806 if (errors & I_ERR_LINK_COUNT_WRONG)
807 fprintf(stderr, ", link count wrong");
808 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
809 fprintf(stderr, ", orphan file extent");
810 fprintf(stderr, "\n");
811 /* Print the orphan extents if needed */
812 if (errors & I_ERR_FILE_EXTENT_ORPHAN)
813 print_orphan_data_extents(&rec->orphan_extents, root->objectid);
815 /* Print the holes if needed */
816 if (errors & I_ERR_FILE_EXTENT_DISCOUNT) {
817 struct file_extent_hole *hole;
818 struct rb_node *node;
821 node = rb_first(&rec->holes);
822 fprintf(stderr, "Found file extent holes:\n");
825 hole = rb_entry(node, struct file_extent_hole, node);
826 fprintf(stderr, "\tstart: %llu, len: %llu\n",
827 hole->start, hole->len);
828 node = rb_next(node);
831 fprintf(stderr, "\tstart: 0, len: %llu\n",
832 round_up(rec->isize, root->sectorsize));
836 static void print_ref_error(int errors)
838 if (errors & REF_ERR_NO_DIR_ITEM)
839 fprintf(stderr, ", no dir item");
840 if (errors & REF_ERR_NO_DIR_INDEX)
841 fprintf(stderr, ", no dir index");
842 if (errors & REF_ERR_NO_INODE_REF)
843 fprintf(stderr, ", no inode ref");
844 if (errors & REF_ERR_DUP_DIR_ITEM)
845 fprintf(stderr, ", dup dir item");
846 if (errors & REF_ERR_DUP_DIR_INDEX)
847 fprintf(stderr, ", dup dir index");
848 if (errors & REF_ERR_DUP_INODE_REF)
849 fprintf(stderr, ", dup inode ref");
850 if (errors & REF_ERR_INDEX_UNMATCH)
851 fprintf(stderr, ", index mismatch");
852 if (errors & REF_ERR_FILETYPE_UNMATCH)
853 fprintf(stderr, ", filetype mismatch");
854 if (errors & REF_ERR_NAME_TOO_LONG)
855 fprintf(stderr, ", name too long");
856 if (errors & REF_ERR_NO_ROOT_REF)
857 fprintf(stderr, ", no root ref");
858 if (errors & REF_ERR_NO_ROOT_BACKREF)
859 fprintf(stderr, ", no root backref");
860 if (errors & REF_ERR_DUP_ROOT_REF)
861 fprintf(stderr, ", dup root ref");
862 if (errors & REF_ERR_DUP_ROOT_BACKREF)
863 fprintf(stderr, ", dup root backref");
864 fprintf(stderr, "\n");
867 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
870 struct ptr_node *node;
871 struct cache_extent *cache;
872 struct inode_record *rec = NULL;
875 cache = lookup_cache_extent(inode_cache, ino, 1);
877 node = container_of(cache, struct ptr_node, cache);
879 if (mod && rec->refs > 1) {
880 node->data = clone_inode_rec(rec);
881 if (IS_ERR(node->data))
887 rec = calloc(1, sizeof(*rec));
889 return ERR_PTR(-ENOMEM);
891 rec->extent_start = (u64)-1;
893 INIT_LIST_HEAD(&rec->backrefs);
894 INIT_LIST_HEAD(&rec->orphan_extents);
895 rec->holes = RB_ROOT;
897 node = malloc(sizeof(*node));
900 return ERR_PTR(-ENOMEM);
902 node->cache.start = ino;
903 node->cache.size = 1;
906 if (ino == BTRFS_FREE_INO_OBJECTID)
909 ret = insert_cache_extent(inode_cache, &node->cache);
911 return ERR_PTR(-EEXIST);
916 static void free_orphan_data_extents(struct list_head *orphan_extents)
918 struct orphan_data_extent *orphan;
920 while (!list_empty(orphan_extents)) {
921 orphan = list_entry(orphan_extents->next,
922 struct orphan_data_extent, list);
923 list_del(&orphan->list);
928 static void free_inode_rec(struct inode_record *rec)
930 struct inode_backref *backref;
935 while (!list_empty(&rec->backrefs)) {
936 backref = to_inode_backref(rec->backrefs.next);
937 list_del(&backref->list);
940 free_orphan_data_extents(&rec->orphan_extents);
941 free_file_extent_holes(&rec->holes);
945 static int can_free_inode_rec(struct inode_record *rec)
947 if (!rec->errors && rec->checked && rec->found_inode_item &&
948 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
953 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
954 struct inode_record *rec)
956 struct cache_extent *cache;
957 struct inode_backref *tmp, *backref;
958 struct ptr_node *node;
961 if (!rec->found_inode_item)
964 filetype = imode_to_type(rec->imode);
965 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
966 if (backref->found_dir_item && backref->found_dir_index) {
967 if (backref->filetype != filetype)
968 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
969 if (!backref->errors && backref->found_inode_ref &&
970 rec->nlink == rec->found_link) {
971 list_del(&backref->list);
977 if (!rec->checked || rec->merging)
980 if (S_ISDIR(rec->imode)) {
981 if (rec->found_size != rec->isize)
982 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
983 if (rec->found_file_extent)
984 rec->errors |= I_ERR_ODD_FILE_EXTENT;
985 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
986 if (rec->found_dir_item)
987 rec->errors |= I_ERR_ODD_DIR_ITEM;
988 if (rec->found_size != rec->nbytes)
989 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
990 if (rec->nlink > 0 && !no_holes &&
991 (rec->extent_end < rec->isize ||
992 first_extent_gap(&rec->holes) < rec->isize))
993 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
996 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
997 if (rec->found_csum_item && rec->nodatasum)
998 rec->errors |= I_ERR_ODD_CSUM_ITEM;
999 if (rec->some_csum_missing && !rec->nodatasum)
1000 rec->errors |= I_ERR_SOME_CSUM_MISSING;
1003 BUG_ON(rec->refs != 1);
1004 if (can_free_inode_rec(rec)) {
1005 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
1006 node = container_of(cache, struct ptr_node, cache);
1007 BUG_ON(node->data != rec);
1008 remove_cache_extent(inode_cache, &node->cache);
1010 free_inode_rec(rec);
1014 static int check_orphan_item(struct btrfs_root *root, u64 ino)
1016 struct btrfs_path path;
1017 struct btrfs_key key;
1020 key.objectid = BTRFS_ORPHAN_OBJECTID;
1021 key.type = BTRFS_ORPHAN_ITEM_KEY;
1024 btrfs_init_path(&path);
1025 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1026 btrfs_release_path(&path);
1032 static int process_inode_item(struct extent_buffer *eb,
1033 int slot, struct btrfs_key *key,
1034 struct shared_node *active_node)
1036 struct inode_record *rec;
1037 struct btrfs_inode_item *item;
1039 rec = active_node->current;
1040 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1041 if (rec->found_inode_item) {
1042 rec->errors |= I_ERR_DUP_INODE_ITEM;
1045 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
1046 rec->nlink = btrfs_inode_nlink(eb, item);
1047 rec->isize = btrfs_inode_size(eb, item);
1048 rec->nbytes = btrfs_inode_nbytes(eb, item);
1049 rec->imode = btrfs_inode_mode(eb, item);
1050 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
1052 rec->found_inode_item = 1;
1053 if (rec->nlink == 0)
1054 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
1055 maybe_free_inode_rec(&active_node->inode_cache, rec);
1059 static struct inode_backref *get_inode_backref(struct inode_record *rec,
1061 int namelen, u64 dir)
1063 struct inode_backref *backref;
1065 list_for_each_entry(backref, &rec->backrefs, list) {
1066 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
1068 if (backref->dir != dir || backref->namelen != namelen)
1070 if (memcmp(name, backref->name, namelen))
1075 backref = malloc(sizeof(*backref) + namelen + 1);
1078 memset(backref, 0, sizeof(*backref));
1080 backref->namelen = namelen;
1081 memcpy(backref->name, name, namelen);
1082 backref->name[namelen] = '\0';
1083 list_add_tail(&backref->list, &rec->backrefs);
1087 static int add_inode_backref(struct cache_tree *inode_cache,
1088 u64 ino, u64 dir, u64 index,
1089 const char *name, int namelen,
1090 u8 filetype, u8 itemtype, int errors)
1092 struct inode_record *rec;
1093 struct inode_backref *backref;
1095 rec = get_inode_rec(inode_cache, ino, 1);
1096 BUG_ON(IS_ERR(rec));
1097 backref = get_inode_backref(rec, name, namelen, dir);
1100 backref->errors |= errors;
1101 if (itemtype == BTRFS_DIR_INDEX_KEY) {
1102 if (backref->found_dir_index)
1103 backref->errors |= REF_ERR_DUP_DIR_INDEX;
1104 if (backref->found_inode_ref && backref->index != index)
1105 backref->errors |= REF_ERR_INDEX_UNMATCH;
1106 if (backref->found_dir_item && backref->filetype != filetype)
1107 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
1109 backref->index = index;
1110 backref->filetype = filetype;
1111 backref->found_dir_index = 1;
1112 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
1114 if (backref->found_dir_item)
1115 backref->errors |= REF_ERR_DUP_DIR_ITEM;
1116 if (backref->found_dir_index && backref->filetype != filetype)
1117 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
1119 backref->filetype = filetype;
1120 backref->found_dir_item = 1;
1121 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
1122 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
1123 if (backref->found_inode_ref)
1124 backref->errors |= REF_ERR_DUP_INODE_REF;
1125 if (backref->found_dir_index && backref->index != index)
1126 backref->errors |= REF_ERR_INDEX_UNMATCH;
1128 backref->index = index;
1130 backref->ref_type = itemtype;
1131 backref->found_inode_ref = 1;
1136 maybe_free_inode_rec(inode_cache, rec);
1140 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
1141 struct cache_tree *dst_cache)
1143 struct inode_backref *backref;
1148 list_for_each_entry(backref, &src->backrefs, list) {
1149 if (backref->found_dir_index) {
1150 add_inode_backref(dst_cache, dst->ino, backref->dir,
1151 backref->index, backref->name,
1152 backref->namelen, backref->filetype,
1153 BTRFS_DIR_INDEX_KEY, backref->errors);
1155 if (backref->found_dir_item) {
1157 add_inode_backref(dst_cache, dst->ino,
1158 backref->dir, 0, backref->name,
1159 backref->namelen, backref->filetype,
1160 BTRFS_DIR_ITEM_KEY, backref->errors);
1162 if (backref->found_inode_ref) {
1163 add_inode_backref(dst_cache, dst->ino,
1164 backref->dir, backref->index,
1165 backref->name, backref->namelen, 0,
1166 backref->ref_type, backref->errors);
1170 if (src->found_dir_item)
1171 dst->found_dir_item = 1;
1172 if (src->found_file_extent)
1173 dst->found_file_extent = 1;
1174 if (src->found_csum_item)
1175 dst->found_csum_item = 1;
1176 if (src->some_csum_missing)
1177 dst->some_csum_missing = 1;
1178 if (first_extent_gap(&dst->holes) > first_extent_gap(&src->holes)) {
1179 ret = copy_file_extent_holes(&dst->holes, &src->holes);
1184 BUG_ON(src->found_link < dir_count);
1185 dst->found_link += src->found_link - dir_count;
1186 dst->found_size += src->found_size;
1187 if (src->extent_start != (u64)-1) {
1188 if (dst->extent_start == (u64)-1) {
1189 dst->extent_start = src->extent_start;
1190 dst->extent_end = src->extent_end;
1192 if (dst->extent_end > src->extent_start)
1193 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1194 else if (dst->extent_end < src->extent_start) {
1195 ret = add_file_extent_hole(&dst->holes,
1197 src->extent_start - dst->extent_end);
1199 if (dst->extent_end < src->extent_end)
1200 dst->extent_end = src->extent_end;
1204 dst->errors |= src->errors;
1205 if (src->found_inode_item) {
1206 if (!dst->found_inode_item) {
1207 dst->nlink = src->nlink;
1208 dst->isize = src->isize;
1209 dst->nbytes = src->nbytes;
1210 dst->imode = src->imode;
1211 dst->nodatasum = src->nodatasum;
1212 dst->found_inode_item = 1;
1214 dst->errors |= I_ERR_DUP_INODE_ITEM;
1222 static int splice_shared_node(struct shared_node *src_node,
1223 struct shared_node *dst_node)
1225 struct cache_extent *cache;
1226 struct ptr_node *node, *ins;
1227 struct cache_tree *src, *dst;
1228 struct inode_record *rec, *conflict;
1229 u64 current_ino = 0;
1233 if (--src_node->refs == 0)
1235 if (src_node->current)
1236 current_ino = src_node->current->ino;
1238 src = &src_node->root_cache;
1239 dst = &dst_node->root_cache;
1241 cache = search_cache_extent(src, 0);
1243 node = container_of(cache, struct ptr_node, cache);
1245 cache = next_cache_extent(cache);
1248 remove_cache_extent(src, &node->cache);
1251 ins = malloc(sizeof(*ins));
1253 ins->cache.start = node->cache.start;
1254 ins->cache.size = node->cache.size;
1258 ret = insert_cache_extent(dst, &ins->cache);
1259 if (ret == -EEXIST) {
1260 conflict = get_inode_rec(dst, rec->ino, 1);
1261 BUG_ON(IS_ERR(conflict));
1262 merge_inode_recs(rec, conflict, dst);
1264 conflict->checked = 1;
1265 if (dst_node->current == conflict)
1266 dst_node->current = NULL;
1268 maybe_free_inode_rec(dst, conflict);
1269 free_inode_rec(rec);
1276 if (src == &src_node->root_cache) {
1277 src = &src_node->inode_cache;
1278 dst = &dst_node->inode_cache;
1282 if (current_ino > 0 && (!dst_node->current ||
1283 current_ino > dst_node->current->ino)) {
1284 if (dst_node->current) {
1285 dst_node->current->checked = 1;
1286 maybe_free_inode_rec(dst, dst_node->current);
1288 dst_node->current = get_inode_rec(dst, current_ino, 1);
1289 BUG_ON(IS_ERR(dst_node->current));
1294 static void free_inode_ptr(struct cache_extent *cache)
1296 struct ptr_node *node;
1297 struct inode_record *rec;
1299 node = container_of(cache, struct ptr_node, cache);
1301 free_inode_rec(rec);
1305 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
1307 static struct shared_node *find_shared_node(struct cache_tree *shared,
1310 struct cache_extent *cache;
1311 struct shared_node *node;
1313 cache = lookup_cache_extent(shared, bytenr, 1);
1315 node = container_of(cache, struct shared_node, cache);
1321 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
1324 struct shared_node *node;
1326 node = calloc(1, sizeof(*node));
1329 node->cache.start = bytenr;
1330 node->cache.size = 1;
1331 cache_tree_init(&node->root_cache);
1332 cache_tree_init(&node->inode_cache);
1335 ret = insert_cache_extent(shared, &node->cache);
1340 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
1341 struct walk_control *wc, int level)
1343 struct shared_node *node;
1344 struct shared_node *dest;
1347 if (level == wc->active_node)
1350 BUG_ON(wc->active_node <= level);
1351 node = find_shared_node(&wc->shared, bytenr);
1353 ret = add_shared_node(&wc->shared, bytenr, refs);
1355 node = find_shared_node(&wc->shared, bytenr);
1356 wc->nodes[level] = node;
1357 wc->active_node = level;
1361 if (wc->root_level == wc->active_node &&
1362 btrfs_root_refs(&root->root_item) == 0) {
1363 if (--node->refs == 0) {
1364 free_inode_recs_tree(&node->root_cache);
1365 free_inode_recs_tree(&node->inode_cache);
1366 remove_cache_extent(&wc->shared, &node->cache);
1372 dest = wc->nodes[wc->active_node];
1373 splice_shared_node(node, dest);
1374 if (node->refs == 0) {
1375 remove_cache_extent(&wc->shared, &node->cache);
1381 static int leave_shared_node(struct btrfs_root *root,
1382 struct walk_control *wc, int level)
1384 struct shared_node *node;
1385 struct shared_node *dest;
1388 if (level == wc->root_level)
1391 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
1395 BUG_ON(i >= BTRFS_MAX_LEVEL);
1397 node = wc->nodes[wc->active_node];
1398 wc->nodes[wc->active_node] = NULL;
1399 wc->active_node = i;
1401 dest = wc->nodes[wc->active_node];
1402 if (wc->active_node < wc->root_level ||
1403 btrfs_root_refs(&root->root_item) > 0) {
1404 BUG_ON(node->refs <= 1);
1405 splice_shared_node(node, dest);
1407 BUG_ON(node->refs < 2);
1416 * 1 - if the root with id child_root_id is a child of root parent_root_id
1417 * 0 - if the root child_root_id isn't a child of the root parent_root_id but
1418 * has other root(s) as parent(s)
1419 * 2 - if the root child_root_id doesn't have any parent roots
1421 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
1424 struct btrfs_path path;
1425 struct btrfs_key key;
1426 struct extent_buffer *leaf;
1430 btrfs_init_path(&path);
1432 key.objectid = parent_root_id;
1433 key.type = BTRFS_ROOT_REF_KEY;
1434 key.offset = child_root_id;
1435 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1439 btrfs_release_path(&path);
1443 key.objectid = child_root_id;
1444 key.type = BTRFS_ROOT_BACKREF_KEY;
1446 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
1452 leaf = path.nodes[0];
1453 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1454 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
1457 leaf = path.nodes[0];
1460 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1461 if (key.objectid != child_root_id ||
1462 key.type != BTRFS_ROOT_BACKREF_KEY)
1467 if (key.offset == parent_root_id) {
1468 btrfs_release_path(&path);
1475 btrfs_release_path(&path);
1478 return has_parent ? 0 : 2;
1481 static int process_dir_item(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(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 extent_buffer *parent, int slot,
1987 struct extent_buffer *child)
1989 struct btrfs_key parent_key;
1990 struct btrfs_key child_key;
1993 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1994 if (btrfs_header_level(child) == 0)
1995 btrfs_item_key_to_cpu(child, &child_key, 0);
1997 btrfs_node_key_to_cpu(child, &child_key, 0);
1999 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
2002 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
2003 parent_key.objectid, parent_key.type, parent_key.offset,
2004 child_key.objectid, child_key.type, child_key.offset);
2006 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
2008 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
2009 btrfs_node_blockptr(parent, slot),
2010 btrfs_header_bytenr(child));
2012 if (btrfs_node_ptr_generation(parent, slot) !=
2013 btrfs_header_generation(child)) {
2015 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
2016 btrfs_header_generation(child),
2017 btrfs_node_ptr_generation(parent, slot));
2023 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
2024 * in every fs or file tree check. Here we find its all root ids, and only check
2025 * it in the fs or file tree which has the smallest root id.
2027 static int need_check(struct btrfs_root *root, struct ulist *roots)
2029 struct rb_node *node;
2030 struct ulist_node *u;
2032 if (roots->nnodes == 1)
2035 node = rb_first(&roots->root);
2036 u = rb_entry(node, struct ulist_node, rb_node);
2038 * current root id is not smallest, we skip it and let it be checked
2039 * in the fs or file tree who hash the smallest root id.
2041 if (root->objectid != u->val)
2048 * for a tree node or leaf, we record its reference count, so later if we still
2049 * process this node or leaf, don't need to compute its reference count again.
2051 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
2052 struct node_refs *nrefs, u64 level)
2056 struct ulist *roots;
2058 if (nrefs->bytenr[level] != bytenr) {
2059 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2060 level, 1, &refs, NULL);
2064 nrefs->bytenr[level] = bytenr;
2065 nrefs->refs[level] = refs;
2067 ret = btrfs_find_all_roots(NULL, root->fs_info, bytenr,
2072 check = need_check(root, roots);
2074 nrefs->need_check[level] = check;
2076 nrefs->need_check[level] = 1;
2083 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
2084 struct walk_control *wc, int *level,
2085 struct node_refs *nrefs)
2087 enum btrfs_tree_block_status status;
2090 struct extent_buffer *next;
2091 struct extent_buffer *cur;
2096 WARN_ON(*level < 0);
2097 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2099 if (path->nodes[*level]->start == nrefs->bytenr[*level]) {
2100 refs = nrefs->refs[*level];
2103 ret = btrfs_lookup_extent_info(NULL, root,
2104 path->nodes[*level]->start,
2105 *level, 1, &refs, NULL);
2110 nrefs->bytenr[*level] = path->nodes[*level]->start;
2111 nrefs->refs[*level] = refs;
2115 ret = enter_shared_node(root, path->nodes[*level]->start,
2123 while (*level >= 0) {
2124 WARN_ON(*level < 0);
2125 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2126 cur = path->nodes[*level];
2128 if (btrfs_header_level(cur) != *level)
2131 if (path->slots[*level] >= btrfs_header_nritems(cur))
2134 ret = process_one_leaf(root, cur, wc);
2139 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2140 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2141 blocksize = root->nodesize;
2143 if (bytenr == nrefs->bytenr[*level - 1]) {
2144 refs = nrefs->refs[*level - 1];
2146 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2147 *level - 1, 1, &refs, NULL);
2151 nrefs->bytenr[*level - 1] = bytenr;
2152 nrefs->refs[*level - 1] = refs;
2157 ret = enter_shared_node(root, bytenr, refs,
2160 path->slots[*level]++;
2165 next = btrfs_find_tree_block(root, bytenr, blocksize);
2166 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2167 free_extent_buffer(next);
2168 reada_walk_down(root, cur, path->slots[*level]);
2169 next = read_tree_block(root, bytenr, blocksize,
2171 if (!extent_buffer_uptodate(next)) {
2172 struct btrfs_key node_key;
2174 btrfs_node_key_to_cpu(path->nodes[*level],
2176 path->slots[*level]);
2177 btrfs_add_corrupt_extent_record(root->fs_info,
2179 path->nodes[*level]->start,
2180 root->nodesize, *level);
2186 ret = check_child_node(cur, path->slots[*level], next);
2192 if (btrfs_is_leaf(next))
2193 status = btrfs_check_leaf(root, NULL, next);
2195 status = btrfs_check_node(root, NULL, next);
2196 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2197 free_extent_buffer(next);
2202 *level = *level - 1;
2203 free_extent_buffer(path->nodes[*level]);
2204 path->nodes[*level] = next;
2205 path->slots[*level] = 0;
2208 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2212 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
2213 unsigned int ext_ref);
2215 static int walk_down_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2216 int *level, struct node_refs *nrefs, int ext_ref)
2218 enum btrfs_tree_block_status status;
2221 struct extent_buffer *next;
2222 struct extent_buffer *cur;
2226 WARN_ON(*level < 0);
2227 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2229 ret = update_nodes_refs(root, path->nodes[*level]->start,
2234 while (*level >= 0) {
2235 WARN_ON(*level < 0);
2236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2237 cur = path->nodes[*level];
2239 if (btrfs_header_level(cur) != *level)
2242 if (path->slots[*level] >= btrfs_header_nritems(cur))
2244 /* Don't forgot to check leaf/node validation */
2246 ret = btrfs_check_leaf(root, NULL, cur);
2247 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2251 ret = process_one_leaf_v2(root, path, nrefs,
2255 ret = btrfs_check_node(root, NULL, cur);
2256 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2261 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2262 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2263 blocksize = root->nodesize;
2265 ret = update_nodes_refs(root, bytenr, nrefs, *level - 1);
2268 if (!nrefs->need_check[*level - 1]) {
2269 path->slots[*level]++;
2273 next = btrfs_find_tree_block(root, bytenr, blocksize);
2274 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2275 free_extent_buffer(next);
2276 reada_walk_down(root, cur, path->slots[*level]);
2277 next = read_tree_block(root, bytenr, blocksize,
2279 if (!extent_buffer_uptodate(next)) {
2280 struct btrfs_key node_key;
2282 btrfs_node_key_to_cpu(path->nodes[*level],
2284 path->slots[*level]);
2285 btrfs_add_corrupt_extent_record(root->fs_info,
2287 path->nodes[*level]->start,
2288 root->nodesize, *level);
2294 ret = check_child_node(cur, path->slots[*level], next);
2298 if (btrfs_is_leaf(next))
2299 status = btrfs_check_leaf(root, NULL, next);
2301 status = btrfs_check_node(root, NULL, next);
2302 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2303 free_extent_buffer(next);
2308 *level = *level - 1;
2309 free_extent_buffer(path->nodes[*level]);
2310 path->nodes[*level] = next;
2311 path->slots[*level] = 0;
2316 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
2317 struct walk_control *wc, int *level)
2320 struct extent_buffer *leaf;
2322 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2323 leaf = path->nodes[i];
2324 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2329 free_extent_buffer(path->nodes[*level]);
2330 path->nodes[*level] = NULL;
2331 BUG_ON(*level > wc->active_node);
2332 if (*level == wc->active_node)
2333 leave_shared_node(root, wc, *level);
2340 static int walk_up_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2344 struct extent_buffer *leaf;
2346 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2347 leaf = path->nodes[i];
2348 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2353 free_extent_buffer(path->nodes[*level]);
2354 path->nodes[*level] = NULL;
2361 static int check_root_dir(struct inode_record *rec)
2363 struct inode_backref *backref;
2366 if (!rec->found_inode_item || rec->errors)
2368 if (rec->nlink != 1 || rec->found_link != 0)
2370 if (list_empty(&rec->backrefs))
2372 backref = to_inode_backref(rec->backrefs.next);
2373 if (!backref->found_inode_ref)
2375 if (backref->index != 0 || backref->namelen != 2 ||
2376 memcmp(backref->name, "..", 2))
2378 if (backref->found_dir_index || backref->found_dir_item)
2385 static int repair_inode_isize(struct btrfs_trans_handle *trans,
2386 struct btrfs_root *root, struct btrfs_path *path,
2387 struct inode_record *rec)
2389 struct btrfs_inode_item *ei;
2390 struct btrfs_key key;
2393 key.objectid = rec->ino;
2394 key.type = BTRFS_INODE_ITEM_KEY;
2395 key.offset = (u64)-1;
2397 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2401 if (!path->slots[0]) {
2408 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2409 if (key.objectid != rec->ino) {
2414 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2415 struct btrfs_inode_item);
2416 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
2417 btrfs_mark_buffer_dirty(path->nodes[0]);
2418 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2419 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
2420 root->root_key.objectid);
2422 btrfs_release_path(path);
2426 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
2427 struct btrfs_root *root,
2428 struct btrfs_path *path,
2429 struct inode_record *rec)
2433 ret = btrfs_add_orphan_item(trans, root, path, rec->ino);
2434 btrfs_release_path(path);
2436 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2440 static int repair_inode_nbytes(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root,
2442 struct btrfs_path *path,
2443 struct inode_record *rec)
2445 struct btrfs_inode_item *ei;
2446 struct btrfs_key key;
2449 key.objectid = rec->ino;
2450 key.type = BTRFS_INODE_ITEM_KEY;
2453 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2460 /* Since ret == 0, no need to check anything */
2461 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2462 struct btrfs_inode_item);
2463 btrfs_set_inode_nbytes(path->nodes[0], ei, rec->found_size);
2464 btrfs_mark_buffer_dirty(path->nodes[0]);
2465 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
2466 printf("reset nbytes for ino %llu root %llu\n",
2467 rec->ino, root->root_key.objectid);
2469 btrfs_release_path(path);
2473 static int add_missing_dir_index(struct btrfs_root *root,
2474 struct cache_tree *inode_cache,
2475 struct inode_record *rec,
2476 struct inode_backref *backref)
2478 struct btrfs_path path;
2479 struct btrfs_trans_handle *trans;
2480 struct btrfs_dir_item *dir_item;
2481 struct extent_buffer *leaf;
2482 struct btrfs_key key;
2483 struct btrfs_disk_key disk_key;
2484 struct inode_record *dir_rec;
2485 unsigned long name_ptr;
2486 u32 data_size = sizeof(*dir_item) + backref->namelen;
2489 trans = btrfs_start_transaction(root, 1);
2491 return PTR_ERR(trans);
2493 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
2494 (unsigned long long)rec->ino);
2496 btrfs_init_path(&path);
2497 key.objectid = backref->dir;
2498 key.type = BTRFS_DIR_INDEX_KEY;
2499 key.offset = backref->index;
2500 ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
2503 leaf = path.nodes[0];
2504 dir_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_dir_item);
2506 disk_key.objectid = cpu_to_le64(rec->ino);
2507 disk_key.type = BTRFS_INODE_ITEM_KEY;
2508 disk_key.offset = 0;
2510 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
2511 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
2512 btrfs_set_dir_data_len(leaf, dir_item, 0);
2513 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
2514 name_ptr = (unsigned long)(dir_item + 1);
2515 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
2516 btrfs_mark_buffer_dirty(leaf);
2517 btrfs_release_path(&path);
2518 btrfs_commit_transaction(trans, root);
2520 backref->found_dir_index = 1;
2521 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
2522 BUG_ON(IS_ERR(dir_rec));
2525 dir_rec->found_size += backref->namelen;
2526 if (dir_rec->found_size == dir_rec->isize &&
2527 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
2528 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2529 if (dir_rec->found_size != dir_rec->isize)
2530 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
2535 static int delete_dir_index(struct btrfs_root *root,
2536 struct cache_tree *inode_cache,
2537 struct inode_record *rec,
2538 struct inode_backref *backref)
2540 struct btrfs_trans_handle *trans;
2541 struct btrfs_dir_item *di;
2542 struct btrfs_path path;
2545 trans = btrfs_start_transaction(root, 1);
2547 return PTR_ERR(trans);
2549 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
2550 (unsigned long long)backref->dir,
2551 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
2552 (unsigned long long)root->objectid);
2554 btrfs_init_path(&path);
2555 di = btrfs_lookup_dir_index(trans, root, &path, backref->dir,
2556 backref->name, backref->namelen,
2557 backref->index, -1);
2560 btrfs_release_path(&path);
2561 btrfs_commit_transaction(trans, root);
2568 ret = btrfs_del_item(trans, root, &path);
2570 ret = btrfs_delete_one_dir_name(trans, root, &path, di);
2572 btrfs_release_path(&path);
2573 btrfs_commit_transaction(trans, root);
2577 static int create_inode_item(struct btrfs_root *root,
2578 struct inode_record *rec,
2579 struct inode_backref *backref, int root_dir)
2581 struct btrfs_trans_handle *trans;
2582 struct btrfs_inode_item inode_item;
2583 time_t now = time(NULL);
2586 trans = btrfs_start_transaction(root, 1);
2587 if (IS_ERR(trans)) {
2588 ret = PTR_ERR(trans);
2592 fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
2593 "be incomplete, please check permissions and content after "
2594 "the fsck completes.\n", (unsigned long long)root->objectid,
2595 (unsigned long long)rec->ino);
2597 memset(&inode_item, 0, sizeof(inode_item));
2598 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
2600 btrfs_set_stack_inode_nlink(&inode_item, 1);
2602 btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
2603 btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
2604 if (rec->found_dir_item) {
2605 if (rec->found_file_extent)
2606 fprintf(stderr, "root %llu inode %llu has both a dir "
2607 "item and extents, unsure if it is a dir or a "
2608 "regular file so setting it as a directory\n",
2609 (unsigned long long)root->objectid,
2610 (unsigned long long)rec->ino);
2611 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
2612 btrfs_set_stack_inode_size(&inode_item, rec->found_size);
2613 } else if (!rec->found_dir_item) {
2614 btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
2615 btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
2617 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
2618 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
2619 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
2620 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
2621 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
2622 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
2623 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
2624 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
2626 ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
2628 btrfs_commit_transaction(trans, root);
2632 static int repair_inode_backrefs(struct btrfs_root *root,
2633 struct inode_record *rec,
2634 struct cache_tree *inode_cache,
2637 struct inode_backref *tmp, *backref;
2638 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2642 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2643 if (!delete && rec->ino == root_dirid) {
2644 if (!rec->found_inode_item) {
2645 ret = create_inode_item(root, rec, backref, 1);
2652 /* Index 0 for root dir's are special, don't mess with it */
2653 if (rec->ino == root_dirid && backref->index == 0)
2657 ((backref->found_dir_index && !backref->found_inode_ref) ||
2658 (backref->found_dir_index && backref->found_inode_ref &&
2659 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
2660 ret = delete_dir_index(root, inode_cache, rec, backref);
2664 list_del(&backref->list);
2668 if (!delete && !backref->found_dir_index &&
2669 backref->found_dir_item && backref->found_inode_ref) {
2670 ret = add_missing_dir_index(root, inode_cache, rec,
2675 if (backref->found_dir_item &&
2676 backref->found_dir_index &&
2677 backref->found_dir_index) {
2678 if (!backref->errors &&
2679 backref->found_inode_ref) {
2680 list_del(&backref->list);
2686 if (!delete && (!backref->found_dir_index &&
2687 !backref->found_dir_item &&
2688 backref->found_inode_ref)) {
2689 struct btrfs_trans_handle *trans;
2690 struct btrfs_key location;
2692 ret = check_dir_conflict(root, backref->name,
2698 * let nlink fixing routine to handle it,
2699 * which can do it better.
2704 location.objectid = rec->ino;
2705 location.type = BTRFS_INODE_ITEM_KEY;
2706 location.offset = 0;
2708 trans = btrfs_start_transaction(root, 1);
2709 if (IS_ERR(trans)) {
2710 ret = PTR_ERR(trans);
2713 fprintf(stderr, "adding missing dir index/item pair "
2715 (unsigned long long)rec->ino);
2716 ret = btrfs_insert_dir_item(trans, root, backref->name,
2718 backref->dir, &location,
2719 imode_to_type(rec->imode),
2722 btrfs_commit_transaction(trans, root);
2726 if (!delete && (backref->found_inode_ref &&
2727 backref->found_dir_index &&
2728 backref->found_dir_item &&
2729 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
2730 !rec->found_inode_item)) {
2731 ret = create_inode_item(root, rec, backref, 0);
2738 return ret ? ret : repaired;
2742 * To determine the file type for nlink/inode_item repair
2744 * Return 0 if file type is found and BTRFS_FT_* is stored into type.
2745 * Return -ENOENT if file type is not found.
2747 static int find_file_type(struct inode_record *rec, u8 *type)
2749 struct inode_backref *backref;
2751 /* For inode item recovered case */
2752 if (rec->found_inode_item) {
2753 *type = imode_to_type(rec->imode);
2757 list_for_each_entry(backref, &rec->backrefs, list) {
2758 if (backref->found_dir_index || backref->found_dir_item) {
2759 *type = backref->filetype;
2767 * To determine the file name for nlink repair
2769 * Return 0 if file name is found, set name and namelen.
2770 * Return -ENOENT if file name is not found.
2772 static int find_file_name(struct inode_record *rec,
2773 char *name, int *namelen)
2775 struct inode_backref *backref;
2777 list_for_each_entry(backref, &rec->backrefs, list) {
2778 if (backref->found_dir_index || backref->found_dir_item ||
2779 backref->found_inode_ref) {
2780 memcpy(name, backref->name, backref->namelen);
2781 *namelen = backref->namelen;
2788 /* Reset the nlink of the inode to the correct one */
2789 static int reset_nlink(struct btrfs_trans_handle *trans,
2790 struct btrfs_root *root,
2791 struct btrfs_path *path,
2792 struct inode_record *rec)
2794 struct inode_backref *backref;
2795 struct inode_backref *tmp;
2796 struct btrfs_key key;
2797 struct btrfs_inode_item *inode_item;
2800 /* We don't believe this either, reset it and iterate backref */
2801 rec->found_link = 0;
2803 /* Remove all backref including the valid ones */
2804 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2805 ret = btrfs_unlink(trans, root, rec->ino, backref->dir,
2806 backref->index, backref->name,
2807 backref->namelen, 0);
2811 /* remove invalid backref, so it won't be added back */
2812 if (!(backref->found_dir_index &&
2813 backref->found_dir_item &&
2814 backref->found_inode_ref)) {
2815 list_del(&backref->list);
2822 /* Set nlink to 0 */
2823 key.objectid = rec->ino;
2824 key.type = BTRFS_INODE_ITEM_KEY;
2826 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2833 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2834 struct btrfs_inode_item);
2835 btrfs_set_inode_nlink(path->nodes[0], inode_item, 0);
2836 btrfs_mark_buffer_dirty(path->nodes[0]);
2837 btrfs_release_path(path);
2840 * Add back valid inode_ref/dir_item/dir_index,
2841 * add_link() will handle the nlink inc, so new nlink must be correct
2843 list_for_each_entry(backref, &rec->backrefs, list) {
2844 ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
2845 backref->name, backref->namelen,
2846 backref->filetype, &backref->index, 1);
2851 btrfs_release_path(path);
2855 static int get_highest_inode(struct btrfs_trans_handle *trans,
2856 struct btrfs_root *root,
2857 struct btrfs_path *path,
2860 struct btrfs_key key, found_key;
2863 btrfs_init_path(path);
2864 key.objectid = BTRFS_LAST_FREE_OBJECTID;
2866 key.type = BTRFS_INODE_ITEM_KEY;
2867 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2869 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2870 path->slots[0] - 1);
2871 *highest_ino = found_key.objectid;
2874 if (*highest_ino >= BTRFS_LAST_FREE_OBJECTID)
2876 btrfs_release_path(path);
2880 static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
2881 struct btrfs_root *root,
2882 struct btrfs_path *path,
2883 struct inode_record *rec)
2885 char *dir_name = "lost+found";
2886 char namebuf[BTRFS_NAME_LEN] = {0};
2891 int name_recovered = 0;
2892 int type_recovered = 0;
2896 * Get file name and type first before these invalid inode ref
2897 * are deleted by remove_all_invalid_backref()
2899 name_recovered = !find_file_name(rec, namebuf, &namelen);
2900 type_recovered = !find_file_type(rec, &type);
2902 if (!name_recovered) {
2903 printf("Can't get file name for inode %llu, using '%llu' as fallback\n",
2904 rec->ino, rec->ino);
2905 namelen = count_digits(rec->ino);
2906 sprintf(namebuf, "%llu", rec->ino);
2909 if (!type_recovered) {
2910 printf("Can't get file type for inode %llu, using FILE as fallback\n",
2912 type = BTRFS_FT_REG_FILE;
2916 ret = reset_nlink(trans, root, path, rec);
2919 "Failed to reset nlink for inode %llu: %s\n",
2920 rec->ino, strerror(-ret));
2924 if (rec->found_link == 0) {
2925 ret = get_highest_inode(trans, root, path, &lost_found_ino);
2929 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
2930 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
2933 fprintf(stderr, "Failed to create '%s' dir: %s\n",
2934 dir_name, strerror(-ret));
2937 ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
2938 namebuf, namelen, type, NULL, 1);
2940 * Add ".INO" suffix several times to handle case where
2941 * "FILENAME.INO" is already taken by another file.
2943 while (ret == -EEXIST) {
2945 * Conflicting file name, add ".INO" as suffix * +1 for '.'
2947 if (namelen + count_digits(rec->ino) + 1 >
2952 snprintf(namebuf + namelen, BTRFS_NAME_LEN - namelen,
2954 namelen += count_digits(rec->ino) + 1;
2955 ret = btrfs_add_link(trans, root, rec->ino,
2956 lost_found_ino, namebuf,
2957 namelen, type, NULL, 1);
2961 "Failed to link the inode %llu to %s dir: %s\n",
2962 rec->ino, dir_name, strerror(-ret));
2966 * Just increase the found_link, don't actually add the
2967 * backref. This will make things easier and this inode
2968 * record will be freed after the repair is done.
2969 * So fsck will not report problem about this inode.
2972 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
2973 namelen, namebuf, dir_name);
2975 printf("Fixed the nlink of inode %llu\n", rec->ino);
2978 * Clear the flag anyway, or we will loop forever for the same inode
2979 * as it will not be removed from the bad inode list and the dead loop
2982 rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
2983 btrfs_release_path(path);
2988 * Check if there is any normal(reg or prealloc) file extent for given
2990 * This is used to determine the file type when neither its dir_index/item or
2991 * inode_item exists.
2993 * This will *NOT* report error, if any error happens, just consider it does
2994 * not have any normal file extent.
2996 static int find_normal_file_extent(struct btrfs_root *root, u64 ino)
2998 struct btrfs_path path;
2999 struct btrfs_key key;
3000 struct btrfs_key found_key;
3001 struct btrfs_file_extent_item *fi;
3005 btrfs_init_path(&path);
3007 key.type = BTRFS_EXTENT_DATA_KEY;
3010 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3015 if (ret && path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
3016 ret = btrfs_next_leaf(root, &path);
3023 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
3025 if (found_key.objectid != ino ||
3026 found_key.type != BTRFS_EXTENT_DATA_KEY)
3028 fi = btrfs_item_ptr(path.nodes[0], path.slots[0],
3029 struct btrfs_file_extent_item);
3030 type = btrfs_file_extent_type(path.nodes[0], fi);
3031 if (type != BTRFS_FILE_EXTENT_INLINE) {
3037 btrfs_release_path(&path);
3041 static u32 btrfs_type_to_imode(u8 type)
3043 static u32 imode_by_btrfs_type[] = {
3044 [BTRFS_FT_REG_FILE] = S_IFREG,
3045 [BTRFS_FT_DIR] = S_IFDIR,
3046 [BTRFS_FT_CHRDEV] = S_IFCHR,
3047 [BTRFS_FT_BLKDEV] = S_IFBLK,
3048 [BTRFS_FT_FIFO] = S_IFIFO,
3049 [BTRFS_FT_SOCK] = S_IFSOCK,
3050 [BTRFS_FT_SYMLINK] = S_IFLNK,
3053 return imode_by_btrfs_type[(type)];
3056 static int repair_inode_no_item(struct btrfs_trans_handle *trans,
3057 struct btrfs_root *root,
3058 struct btrfs_path *path,
3059 struct inode_record *rec)
3063 int type_recovered = 0;
3066 printf("Trying to rebuild inode:%llu\n", rec->ino);
3068 type_recovered = !find_file_type(rec, &filetype);
3071 * Try to determine inode type if type not found.
3073 * For found regular file extent, it must be FILE.
3074 * For found dir_item/index, it must be DIR.
3076 * For undetermined one, use FILE as fallback.
3079 * 1. If found backref(inode_index/item is already handled) to it,
3081 * Need new inode-inode ref structure to allow search for that.
3083 if (!type_recovered) {
3084 if (rec->found_file_extent &&
3085 find_normal_file_extent(root, rec->ino)) {
3087 filetype = BTRFS_FT_REG_FILE;
3088 } else if (rec->found_dir_item) {
3090 filetype = BTRFS_FT_DIR;
3091 } else if (!list_empty(&rec->orphan_extents)) {
3093 filetype = BTRFS_FT_REG_FILE;
3095 printf("Can't determine the filetype for inode %llu, assume it is a normal file\n",
3098 filetype = BTRFS_FT_REG_FILE;
3102 ret = btrfs_new_inode(trans, root, rec->ino,
3103 mode | btrfs_type_to_imode(filetype));
3108 * Here inode rebuild is done, we only rebuild the inode item,
3109 * don't repair the nlink(like move to lost+found).
3110 * That is the job of nlink repair.
3112 * We just fill the record and return
3114 rec->found_dir_item = 1;
3115 rec->imode = mode | btrfs_type_to_imode(filetype);
3117 rec->errors &= ~I_ERR_NO_INODE_ITEM;
3118 /* Ensure the inode_nlinks repair function will be called */
3119 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3124 static int repair_inode_orphan_extent(struct btrfs_trans_handle *trans,
3125 struct btrfs_root *root,
3126 struct btrfs_path *path,
3127 struct inode_record *rec)
3129 struct orphan_data_extent *orphan;
3130 struct orphan_data_extent *tmp;
3133 list_for_each_entry_safe(orphan, tmp, &rec->orphan_extents, list) {
3135 * Check for conflicting file extents
3137 * Here we don't know whether the extents is compressed or not,
3138 * so we can only assume it not compressed nor data offset,
3139 * and use its disk_len as extent length.
3141 ret = btrfs_get_extent(NULL, root, path, orphan->objectid,
3142 orphan->offset, orphan->disk_len, 0);
3143 btrfs_release_path(path);
3148 "orphan extent (%llu, %llu) conflicts, delete the orphan\n",
3149 orphan->disk_bytenr, orphan->disk_len);
3150 ret = btrfs_free_extent(trans,
3151 root->fs_info->extent_root,
3152 orphan->disk_bytenr, orphan->disk_len,
3153 0, root->objectid, orphan->objectid,
3158 ret = btrfs_insert_file_extent(trans, root, orphan->objectid,
3159 orphan->offset, orphan->disk_bytenr,
3160 orphan->disk_len, orphan->disk_len);
3164 /* Update file size info */
3165 rec->found_size += orphan->disk_len;
3166 if (rec->found_size == rec->nbytes)
3167 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
3169 /* Update the file extent hole info too */
3170 ret = del_file_extent_hole(&rec->holes, orphan->offset,
3174 if (RB_EMPTY_ROOT(&rec->holes))
3175 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3177 list_del(&orphan->list);
3180 rec->errors &= ~I_ERR_FILE_EXTENT_ORPHAN;
3185 static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root,
3187 struct btrfs_path *path,
3188 struct inode_record *rec)
3190 struct rb_node *node;
3191 struct file_extent_hole *hole;
3195 node = rb_first(&rec->holes);
3199 hole = rb_entry(node, struct file_extent_hole, node);
3200 ret = btrfs_punch_hole(trans, root, rec->ino,
3201 hole->start, hole->len);
3204 ret = del_file_extent_hole(&rec->holes, hole->start,
3208 if (RB_EMPTY_ROOT(&rec->holes))
3209 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3210 node = rb_first(&rec->holes);
3212 /* special case for a file losing all its file extent */
3214 ret = btrfs_punch_hole(trans, root, rec->ino, 0,
3215 round_up(rec->isize, root->sectorsize));
3219 printf("Fixed discount file extents for inode: %llu in root: %llu\n",
3220 rec->ino, root->objectid);
3225 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
3227 struct btrfs_trans_handle *trans;
3228 struct btrfs_path path;
3231 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |
3232 I_ERR_NO_ORPHAN_ITEM |
3233 I_ERR_LINK_COUNT_WRONG |
3234 I_ERR_NO_INODE_ITEM |
3235 I_ERR_FILE_EXTENT_ORPHAN |
3236 I_ERR_FILE_EXTENT_DISCOUNT|
3237 I_ERR_FILE_NBYTES_WRONG)))
3241 * For nlink repair, it may create a dir and add link, so
3242 * 2 for parent(256)'s dir_index and dir_item
3243 * 2 for lost+found dir's inode_item and inode_ref
3244 * 1 for the new inode_ref of the file
3245 * 2 for lost+found dir's dir_index and dir_item for the file
3247 trans = btrfs_start_transaction(root, 7);
3249 return PTR_ERR(trans);
3251 btrfs_init_path(&path);
3252 if (rec->errors & I_ERR_NO_INODE_ITEM)
3253 ret = repair_inode_no_item(trans, root, &path, rec);
3254 if (!ret && rec->errors & I_ERR_FILE_EXTENT_ORPHAN)
3255 ret = repair_inode_orphan_extent(trans, root, &path, rec);
3256 if (!ret && rec->errors & I_ERR_FILE_EXTENT_DISCOUNT)
3257 ret = repair_inode_discount_extent(trans, root, &path, rec);
3258 if (!ret && rec->errors & I_ERR_DIR_ISIZE_WRONG)
3259 ret = repair_inode_isize(trans, root, &path, rec);
3260 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
3261 ret = repair_inode_orphan_item(trans, root, &path, rec);
3262 if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
3263 ret = repair_inode_nlinks(trans, root, &path, rec);
3264 if (!ret && rec->errors & I_ERR_FILE_NBYTES_WRONG)
3265 ret = repair_inode_nbytes(trans, root, &path, rec);
3266 btrfs_commit_transaction(trans, root);
3267 btrfs_release_path(&path);
3271 static int check_inode_recs(struct btrfs_root *root,
3272 struct cache_tree *inode_cache)
3274 struct cache_extent *cache;
3275 struct ptr_node *node;
3276 struct inode_record *rec;
3277 struct inode_backref *backref;
3282 u64 root_dirid = btrfs_root_dirid(&root->root_item);
3284 if (btrfs_root_refs(&root->root_item) == 0) {
3285 if (!cache_tree_empty(inode_cache))
3286 fprintf(stderr, "warning line %d\n", __LINE__);
3291 * We need to repair backrefs first because we could change some of the
3292 * errors in the inode recs.
3294 * We also need to go through and delete invalid backrefs first and then
3295 * add the correct ones second. We do this because we may get EEXIST
3296 * when adding back the correct index because we hadn't yet deleted the
3299 * For example, if we were missing a dir index then the directories
3300 * isize would be wrong, so if we fixed the isize to what we thought it
3301 * would be and then fixed the backref we'd still have a invalid fs, so
3302 * we need to add back the dir index and then check to see if the isize
3307 if (stage == 3 && !err)
3310 cache = search_cache_extent(inode_cache, 0);
3311 while (repair && cache) {
3312 node = container_of(cache, struct ptr_node, cache);
3314 cache = next_cache_extent(cache);
3316 /* Need to free everything up and rescan */
3318 remove_cache_extent(inode_cache, &node->cache);
3320 free_inode_rec(rec);
3324 if (list_empty(&rec->backrefs))
3327 ret = repair_inode_backrefs(root, rec, inode_cache,
3341 rec = get_inode_rec(inode_cache, root_dirid, 0);
3342 BUG_ON(IS_ERR(rec));
3344 ret = check_root_dir(rec);
3346 fprintf(stderr, "root %llu root dir %llu error\n",
3347 (unsigned long long)root->root_key.objectid,
3348 (unsigned long long)root_dirid);
3349 print_inode_error(root, rec);
3354 struct btrfs_trans_handle *trans;
3356 trans = btrfs_start_transaction(root, 1);
3357 if (IS_ERR(trans)) {
3358 err = PTR_ERR(trans);
3363 "root %llu missing its root dir, recreating\n",
3364 (unsigned long long)root->objectid);
3366 ret = btrfs_make_root_dir(trans, root, root_dirid);
3369 btrfs_commit_transaction(trans, root);
3373 fprintf(stderr, "root %llu root dir %llu not found\n",
3374 (unsigned long long)root->root_key.objectid,
3375 (unsigned long long)root_dirid);
3379 cache = search_cache_extent(inode_cache, 0);
3382 node = container_of(cache, struct ptr_node, cache);
3384 remove_cache_extent(inode_cache, &node->cache);
3386 if (rec->ino == root_dirid ||
3387 rec->ino == BTRFS_ORPHAN_OBJECTID) {
3388 free_inode_rec(rec);
3392 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
3393 ret = check_orphan_item(root, rec->ino);
3395 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
3396 if (can_free_inode_rec(rec)) {
3397 free_inode_rec(rec);
3402 if (!rec->found_inode_item)
3403 rec->errors |= I_ERR_NO_INODE_ITEM;
3404 if (rec->found_link != rec->nlink)
3405 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3407 ret = try_repair_inode(root, rec);
3408 if (ret == 0 && can_free_inode_rec(rec)) {
3409 free_inode_rec(rec);
3415 if (!(repair && ret == 0))
3417 print_inode_error(root, rec);
3418 list_for_each_entry(backref, &rec->backrefs, list) {
3419 if (!backref->found_dir_item)
3420 backref->errors |= REF_ERR_NO_DIR_ITEM;
3421 if (!backref->found_dir_index)
3422 backref->errors |= REF_ERR_NO_DIR_INDEX;
3423 if (!backref->found_inode_ref)
3424 backref->errors |= REF_ERR_NO_INODE_REF;
3425 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
3426 " namelen %u name %s filetype %d errors %x",
3427 (unsigned long long)backref->dir,
3428 (unsigned long long)backref->index,
3429 backref->namelen, backref->name,
3430 backref->filetype, backref->errors);
3431 print_ref_error(backref->errors);
3433 free_inode_rec(rec);
3435 return (error > 0) ? -1 : 0;
3438 static struct root_record *get_root_rec(struct cache_tree *root_cache,
3441 struct cache_extent *cache;
3442 struct root_record *rec = NULL;
3445 cache = lookup_cache_extent(root_cache, objectid, 1);
3447 rec = container_of(cache, struct root_record, cache);
3449 rec = calloc(1, sizeof(*rec));
3451 return ERR_PTR(-ENOMEM);
3452 rec->objectid = objectid;
3453 INIT_LIST_HEAD(&rec->backrefs);
3454 rec->cache.start = objectid;
3455 rec->cache.size = 1;
3457 ret = insert_cache_extent(root_cache, &rec->cache);
3459 return ERR_PTR(-EEXIST);
3464 static struct root_backref *get_root_backref(struct root_record *rec,
3465 u64 ref_root, u64 dir, u64 index,
3466 const char *name, int namelen)
3468 struct root_backref *backref;
3470 list_for_each_entry(backref, &rec->backrefs, list) {
3471 if (backref->ref_root != ref_root || backref->dir != dir ||
3472 backref->namelen != namelen)
3474 if (memcmp(name, backref->name, namelen))
3479 backref = calloc(1, sizeof(*backref) + namelen + 1);
3482 backref->ref_root = ref_root;
3484 backref->index = index;
3485 backref->namelen = namelen;
3486 memcpy(backref->name, name, namelen);
3487 backref->name[namelen] = '\0';
3488 list_add_tail(&backref->list, &rec->backrefs);
3492 static void free_root_record(struct cache_extent *cache)
3494 struct root_record *rec;
3495 struct root_backref *backref;
3497 rec = container_of(cache, struct root_record, cache);
3498 while (!list_empty(&rec->backrefs)) {
3499 backref = to_root_backref(rec->backrefs.next);
3500 list_del(&backref->list);
3507 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
3509 static int add_root_backref(struct cache_tree *root_cache,
3510 u64 root_id, u64 ref_root, u64 dir, u64 index,
3511 const char *name, int namelen,
3512 int item_type, int errors)
3514 struct root_record *rec;
3515 struct root_backref *backref;
3517 rec = get_root_rec(root_cache, root_id);
3518 BUG_ON(IS_ERR(rec));
3519 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
3522 backref->errors |= errors;
3524 if (item_type != BTRFS_DIR_ITEM_KEY) {
3525 if (backref->found_dir_index || backref->found_back_ref ||
3526 backref->found_forward_ref) {
3527 if (backref->index != index)
3528 backref->errors |= REF_ERR_INDEX_UNMATCH;
3530 backref->index = index;
3534 if (item_type == BTRFS_DIR_ITEM_KEY) {
3535 if (backref->found_forward_ref)
3537 backref->found_dir_item = 1;
3538 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
3539 backref->found_dir_index = 1;
3540 } else if (item_type == BTRFS_ROOT_REF_KEY) {
3541 if (backref->found_forward_ref)
3542 backref->errors |= REF_ERR_DUP_ROOT_REF;
3543 else if (backref->found_dir_item)
3545 backref->found_forward_ref = 1;
3546 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
3547 if (backref->found_back_ref)
3548 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
3549 backref->found_back_ref = 1;
3554 if (backref->found_forward_ref && backref->found_dir_item)
3555 backref->reachable = 1;
3559 static int merge_root_recs(struct btrfs_root *root,
3560 struct cache_tree *src_cache,
3561 struct cache_tree *dst_cache)
3563 struct cache_extent *cache;
3564 struct ptr_node *node;
3565 struct inode_record *rec;
3566 struct inode_backref *backref;
3569 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
3570 free_inode_recs_tree(src_cache);
3575 cache = search_cache_extent(src_cache, 0);
3578 node = container_of(cache, struct ptr_node, cache);
3580 remove_cache_extent(src_cache, &node->cache);
3583 ret = is_child_root(root, root->objectid, rec->ino);
3589 list_for_each_entry(backref, &rec->backrefs, list) {
3590 BUG_ON(backref->found_inode_ref);
3591 if (backref->found_dir_item)
3592 add_root_backref(dst_cache, rec->ino,
3593 root->root_key.objectid, backref->dir,
3594 backref->index, backref->name,
3595 backref->namelen, BTRFS_DIR_ITEM_KEY,
3597 if (backref->found_dir_index)
3598 add_root_backref(dst_cache, rec->ino,
3599 root->root_key.objectid, backref->dir,
3600 backref->index, backref->name,
3601 backref->namelen, BTRFS_DIR_INDEX_KEY,
3605 free_inode_rec(rec);
3612 static int check_root_refs(struct btrfs_root *root,
3613 struct cache_tree *root_cache)
3615 struct root_record *rec;
3616 struct root_record *ref_root;
3617 struct root_backref *backref;
3618 struct cache_extent *cache;
3624 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
3625 BUG_ON(IS_ERR(rec));
3628 /* fixme: this can not detect circular references */
3631 cache = search_cache_extent(root_cache, 0);
3635 rec = container_of(cache, struct root_record, cache);
3636 cache = next_cache_extent(cache);
3638 if (rec->found_ref == 0)
3641 list_for_each_entry(backref, &rec->backrefs, list) {
3642 if (!backref->reachable)
3645 ref_root = get_root_rec(root_cache,
3647 BUG_ON(IS_ERR(ref_root));
3648 if (ref_root->found_ref > 0)
3651 backref->reachable = 0;
3653 if (rec->found_ref == 0)
3659 cache = search_cache_extent(root_cache, 0);
3663 rec = container_of(cache, struct root_record, cache);
3664 cache = next_cache_extent(cache);
3666 if (rec->found_ref == 0 &&
3667 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3668 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
3669 ret = check_orphan_item(root->fs_info->tree_root,
3675 * If we don't have a root item then we likely just have
3676 * a dir item in a snapshot for this root but no actual
3677 * ref key or anything so it's meaningless.
3679 if (!rec->found_root_item)
3682 fprintf(stderr, "fs tree %llu not referenced\n",
3683 (unsigned long long)rec->objectid);
3687 if (rec->found_ref > 0 && !rec->found_root_item)
3689 list_for_each_entry(backref, &rec->backrefs, list) {
3690 if (!backref->found_dir_item)
3691 backref->errors |= REF_ERR_NO_DIR_ITEM;
3692 if (!backref->found_dir_index)
3693 backref->errors |= REF_ERR_NO_DIR_INDEX;
3694 if (!backref->found_back_ref)
3695 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
3696 if (!backref->found_forward_ref)
3697 backref->errors |= REF_ERR_NO_ROOT_REF;
3698 if (backref->reachable && backref->errors)
3705 fprintf(stderr, "fs tree %llu refs %u %s\n",
3706 (unsigned long long)rec->objectid, rec->found_ref,
3707 rec->found_root_item ? "" : "not found");
3709 list_for_each_entry(backref, &rec->backrefs, list) {
3710 if (!backref->reachable)
3712 if (!backref->errors && rec->found_root_item)
3714 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
3715 " index %llu namelen %u name %s errors %x\n",
3716 (unsigned long long)backref->ref_root,
3717 (unsigned long long)backref->dir,
3718 (unsigned long long)backref->index,
3719 backref->namelen, backref->name,
3721 print_ref_error(backref->errors);
3724 return errors > 0 ? 1 : 0;
3727 static int process_root_ref(struct extent_buffer *eb, int slot,
3728 struct btrfs_key *key,
3729 struct cache_tree *root_cache)
3735 struct btrfs_root_ref *ref;
3736 char namebuf[BTRFS_NAME_LEN];
3739 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
3741 dirid = btrfs_root_ref_dirid(eb, ref);
3742 index = btrfs_root_ref_sequence(eb, ref);
3743 name_len = btrfs_root_ref_name_len(eb, ref);
3745 if (name_len <= BTRFS_NAME_LEN) {
3749 len = BTRFS_NAME_LEN;
3750 error = REF_ERR_NAME_TOO_LONG;
3752 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
3754 if (key->type == BTRFS_ROOT_REF_KEY) {
3755 add_root_backref(root_cache, key->offset, key->objectid, dirid,
3756 index, namebuf, len, key->type, error);
3758 add_root_backref(root_cache, key->objectid, key->offset, dirid,
3759 index, namebuf, len, key->type, error);
3764 static void free_corrupt_block(struct cache_extent *cache)
3766 struct btrfs_corrupt_block *corrupt;
3768 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3772 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
3775 * Repair the btree of the given root.
3777 * The fix is to remove the node key in corrupt_blocks cache_tree.
3778 * and rebalance the tree.
3779 * After the fix, the btree should be writeable.
3781 static int repair_btree(struct btrfs_root *root,
3782 struct cache_tree *corrupt_blocks)
3784 struct btrfs_trans_handle *trans;
3785 struct btrfs_path path;
3786 struct btrfs_corrupt_block *corrupt;
3787 struct cache_extent *cache;
3788 struct btrfs_key key;
3793 if (cache_tree_empty(corrupt_blocks))
3796 trans = btrfs_start_transaction(root, 1);
3797 if (IS_ERR(trans)) {
3798 ret = PTR_ERR(trans);
3799 fprintf(stderr, "Error starting transaction: %s\n",
3803 btrfs_init_path(&path);
3804 cache = first_cache_extent(corrupt_blocks);
3806 corrupt = container_of(cache, struct btrfs_corrupt_block,
3808 level = corrupt->level;
3809 path.lowest_level = level;
3810 key.objectid = corrupt->key.objectid;
3811 key.type = corrupt->key.type;
3812 key.offset = corrupt->key.offset;
3815 * Here we don't want to do any tree balance, since it may
3816 * cause a balance with corrupted brother leaf/node,
3817 * so ins_len set to 0 here.
3818 * Balance will be done after all corrupt node/leaf is deleted.
3820 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
3823 offset = btrfs_node_blockptr(path.nodes[level],
3826 /* Remove the ptr */
3827 ret = btrfs_del_ptr(root, &path, level, path.slots[level]);
3831 * Remove the corresponding extent
3832 * return value is not concerned.
3834 btrfs_release_path(&path);
3835 ret = btrfs_free_extent(trans, root, offset, root->nodesize,
3836 0, root->root_key.objectid,
3838 cache = next_cache_extent(cache);
3841 /* Balance the btree using btrfs_search_slot() */
3842 cache = first_cache_extent(corrupt_blocks);
3844 corrupt = container_of(cache, struct btrfs_corrupt_block,
3846 memcpy(&key, &corrupt->key, sizeof(key));
3847 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
3850 /* return will always >0 since it won't find the item */
3852 btrfs_release_path(&path);
3853 cache = next_cache_extent(cache);
3856 btrfs_commit_transaction(trans, root);
3857 btrfs_release_path(&path);
3861 static int check_fs_root(struct btrfs_root *root,
3862 struct cache_tree *root_cache,
3863 struct walk_control *wc)
3869 struct btrfs_path path;
3870 struct shared_node root_node;
3871 struct root_record *rec;
3872 struct btrfs_root_item *root_item = &root->root_item;
3873 struct cache_tree corrupt_blocks;
3874 struct orphan_data_extent *orphan;
3875 struct orphan_data_extent *tmp;
3876 enum btrfs_tree_block_status status;
3877 struct node_refs nrefs;
3880 * Reuse the corrupt_block cache tree to record corrupted tree block
3882 * Unlike the usage in extent tree check, here we do it in a per
3883 * fs/subvol tree base.
3885 cache_tree_init(&corrupt_blocks);
3886 root->fs_info->corrupt_blocks = &corrupt_blocks;
3888 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
3889 rec = get_root_rec(root_cache, root->root_key.objectid);
3890 BUG_ON(IS_ERR(rec));
3891 if (btrfs_root_refs(root_item) > 0)
3892 rec->found_root_item = 1;
3895 btrfs_init_path(&path);
3896 memset(&root_node, 0, sizeof(root_node));
3897 cache_tree_init(&root_node.root_cache);
3898 cache_tree_init(&root_node.inode_cache);
3899 memset(&nrefs, 0, sizeof(nrefs));
3901 /* Move the orphan extent record to corresponding inode_record */
3902 list_for_each_entry_safe(orphan, tmp,
3903 &root->orphan_data_extents, list) {
3904 struct inode_record *inode;
3906 inode = get_inode_rec(&root_node.inode_cache, orphan->objectid,
3908 BUG_ON(IS_ERR(inode));
3909 inode->errors |= I_ERR_FILE_EXTENT_ORPHAN;
3910 list_move(&orphan->list, &inode->orphan_extents);
3913 level = btrfs_header_level(root->node);
3914 memset(wc->nodes, 0, sizeof(wc->nodes));
3915 wc->nodes[level] = &root_node;
3916 wc->active_node = level;
3917 wc->root_level = level;
3919 /* We may not have checked the root block, lets do that now */
3920 if (btrfs_is_leaf(root->node))
3921 status = btrfs_check_leaf(root, NULL, root->node);
3923 status = btrfs_check_node(root, NULL, root->node);
3924 if (status != BTRFS_TREE_BLOCK_CLEAN)
3927 if (btrfs_root_refs(root_item) > 0 ||
3928 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3929 path.nodes[level] = root->node;
3930 extent_buffer_get(root->node);
3931 path.slots[level] = 0;
3933 struct btrfs_key key;
3934 struct btrfs_disk_key found_key;
3936 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3937 level = root_item->drop_level;
3938 path.lowest_level = level;
3939 if (level > btrfs_header_level(root->node) ||
3940 level >= BTRFS_MAX_LEVEL) {
3941 error("ignoring invalid drop level: %u", level);
3944 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3947 btrfs_node_key(path.nodes[level], &found_key,
3949 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3950 sizeof(found_key)));
3954 wret = walk_down_tree(root, &path, wc, &level, &nrefs);
3960 wret = walk_up_tree(root, &path, wc, &level);
3967 btrfs_release_path(&path);
3969 if (!cache_tree_empty(&corrupt_blocks)) {
3970 struct cache_extent *cache;
3971 struct btrfs_corrupt_block *corrupt;
3973 printf("The following tree block(s) is corrupted in tree %llu:\n",
3974 root->root_key.objectid);
3975 cache = first_cache_extent(&corrupt_blocks);
3977 corrupt = container_of(cache,
3978 struct btrfs_corrupt_block,
3980 printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n",
3981 cache->start, corrupt->level,
3982 corrupt->key.objectid, corrupt->key.type,
3983 corrupt->key.offset);
3984 cache = next_cache_extent(cache);
3987 printf("Try to repair the btree for root %llu\n",
3988 root->root_key.objectid);
3989 ret = repair_btree(root, &corrupt_blocks);
3991 fprintf(stderr, "Failed to repair btree: %s\n",
3994 printf("Btree for root %llu is fixed\n",
3995 root->root_key.objectid);
3999 err = merge_root_recs(root, &root_node.root_cache, root_cache);
4003 if (root_node.current) {
4004 root_node.current->checked = 1;
4005 maybe_free_inode_rec(&root_node.inode_cache,
4009 err = check_inode_recs(root, &root_node.inode_cache);
4013 free_corrupt_blocks_tree(&corrupt_blocks);
4014 root->fs_info->corrupt_blocks = NULL;
4015 free_orphan_data_extents(&root->orphan_data_extents);
4019 static int fs_root_objectid(u64 objectid)
4021 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
4022 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
4024 return is_fstree(objectid);
4027 static int check_fs_roots(struct btrfs_root *root,
4028 struct cache_tree *root_cache)
4030 struct btrfs_path path;
4031 struct btrfs_key key;
4032 struct walk_control wc;
4033 struct extent_buffer *leaf, *tree_node;
4034 struct btrfs_root *tmp_root;
4035 struct btrfs_root *tree_root = root->fs_info->tree_root;
4039 if (ctx.progress_enabled) {
4040 ctx.tp = TASK_FS_ROOTS;
4041 task_start(ctx.info);
4045 * Just in case we made any changes to the extent tree that weren't
4046 * reflected into the free space cache yet.
4049 reset_cached_block_groups(root->fs_info);
4050 memset(&wc, 0, sizeof(wc));
4051 cache_tree_init(&wc.shared);
4052 btrfs_init_path(&path);
4057 key.type = BTRFS_ROOT_ITEM_KEY;
4058 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4063 tree_node = tree_root->node;
4065 if (tree_node != tree_root->node) {
4066 free_root_recs_tree(root_cache);
4067 btrfs_release_path(&path);
4070 leaf = path.nodes[0];
4071 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
4072 ret = btrfs_next_leaf(tree_root, &path);
4078 leaf = path.nodes[0];
4080 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
4081 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4082 fs_root_objectid(key.objectid)) {
4083 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4084 tmp_root = btrfs_read_fs_root_no_cache(
4085 root->fs_info, &key);
4087 key.offset = (u64)-1;
4088 tmp_root = btrfs_read_fs_root(
4089 root->fs_info, &key);
4091 if (IS_ERR(tmp_root)) {
4095 ret = check_fs_root(tmp_root, root_cache, &wc);
4096 if (ret == -EAGAIN) {
4097 free_root_recs_tree(root_cache);
4098 btrfs_release_path(&path);
4103 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4104 btrfs_free_fs_root(tmp_root);
4105 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4106 key.type == BTRFS_ROOT_BACKREF_KEY) {
4107 process_root_ref(leaf, path.slots[0], &key,
4114 btrfs_release_path(&path);
4116 free_extent_cache_tree(&wc.shared);
4117 if (!cache_tree_empty(&wc.shared))
4118 fprintf(stderr, "warning line %d\n", __LINE__);
4120 task_stop(ctx.info);
4126 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
4127 * INODE_REF/INODE_EXTREF match.
4129 * @root: the root of the fs/file tree
4130 * @ref_key: the key of the INODE_REF/INODE_EXTREF
4131 * @key: the key of the DIR_ITEM/DIR_INDEX
4132 * @index: the index in the INODE_REF/INODE_EXTREF, be used to
4133 * distinguish root_dir between normal dir/file
4134 * @name: the name in the INODE_REF/INODE_EXTREF
4135 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4136 * @mode: the st_mode of INODE_ITEM
4138 * Return 0 if no error occurred.
4139 * Return ROOT_DIR_ERROR if found DIR_ITEM/DIR_INDEX for root_dir.
4140 * Return DIR_ITEM_MISSING if couldn't find DIR_ITEM/DIR_INDEX for normal
4142 * Return DIR_ITEM_MISMATCH if INODE_REF/INODE_EXTREF and DIR_ITEM/DIR_INDEX
4143 * not match for normal dir/file.
4145 static int find_dir_item(struct btrfs_root *root, struct btrfs_key *ref_key,
4146 struct btrfs_key *key, u64 index, char *name,
4147 u32 namelen, u32 mode)
4149 struct btrfs_path path;
4150 struct extent_buffer *node;
4151 struct btrfs_dir_item *di;
4152 struct btrfs_key location;
4153 char namebuf[BTRFS_NAME_LEN] = {0};
4163 btrfs_init_path(&path);
4164 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4166 ret = DIR_ITEM_MISSING;
4170 /* Process root dir and goto out*/
4173 ret = ROOT_DIR_ERROR;
4175 "root %llu INODE %s[%llu %llu] ROOT_DIR shouldn't have %s",
4177 ref_key->type == BTRFS_INODE_REF_KEY ?
4179 ref_key->objectid, ref_key->offset,
4180 key->type == BTRFS_DIR_ITEM_KEY ?
4181 "DIR_ITEM" : "DIR_INDEX");
4189 /* Process normal file/dir */
4191 ret = DIR_ITEM_MISSING;
4193 "root %llu INODE %s[%llu %llu] doesn't have related %s[%llu %llu] namelen %u filename %s filetype %d",
4195 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4196 ref_key->objectid, ref_key->offset,
4197 key->type == BTRFS_DIR_ITEM_KEY ?
4198 "DIR_ITEM" : "DIR_INDEX",
4199 key->objectid, key->offset, namelen, name,
4200 imode_to_type(mode));
4204 /* Check whether inode_id/filetype/name match */
4205 node = path.nodes[0];
4206 slot = path.slots[0];
4207 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4208 total = btrfs_item_size_nr(node, slot);
4209 while (cur < total) {
4210 ret = DIR_ITEM_MISMATCH;
4211 name_len = btrfs_dir_name_len(node, di);
4212 data_len = btrfs_dir_data_len(node, di);
4214 btrfs_dir_item_key_to_cpu(node, di, &location);
4215 if (location.objectid != ref_key->objectid ||
4216 location.type != BTRFS_INODE_ITEM_KEY ||
4217 location.offset != 0)
4220 filetype = btrfs_dir_type(node, di);
4221 if (imode_to_type(mode) != filetype)
4224 if (name_len <= BTRFS_NAME_LEN) {
4227 len = BTRFS_NAME_LEN;
4228 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
4230 key->type == BTRFS_DIR_ITEM_KEY ?
4231 "DIR_ITEM" : "DIR_INDEX",
4232 key->objectid, key->offset, name_len);
4234 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4235 if (len != namelen || strncmp(namebuf, name, len))
4241 len = sizeof(*di) + name_len + data_len;
4242 di = (struct btrfs_dir_item *)((char *)di + len);
4245 if (ret == DIR_ITEM_MISMATCH)
4247 "root %llu INODE %s[%llu %llu] and %s[%llu %llu] mismatch namelen %u filename %s filetype %d",
4249 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4250 ref_key->objectid, ref_key->offset,
4251 key->type == BTRFS_DIR_ITEM_KEY ?
4252 "DIR_ITEM" : "DIR_INDEX",
4253 key->objectid, key->offset, namelen, name,
4254 imode_to_type(mode));
4256 btrfs_release_path(&path);
4261 * Traverse the given INODE_REF and call find_dir_item() to find related
4262 * DIR_ITEM/DIR_INDEX.
4264 * @root: the root of the fs/file tree
4265 * @ref_key: the key of the INODE_REF
4266 * @refs: the count of INODE_REF
4267 * @mode: the st_mode of INODE_ITEM
4269 * Return 0 if no error occurred.
4271 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4272 struct extent_buffer *node, int slot, u64 *refs,
4275 struct btrfs_key key;
4276 struct btrfs_inode_ref *ref;
4277 char namebuf[BTRFS_NAME_LEN] = {0};
4285 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4286 total = btrfs_item_size_nr(node, slot);
4289 /* Update inode ref count */
4292 index = btrfs_inode_ref_index(node, ref);
4293 name_len = btrfs_inode_ref_name_len(node, ref);
4294 if (name_len <= BTRFS_NAME_LEN) {
4297 len = BTRFS_NAME_LEN;
4298 warning("root %llu INODE_REF[%llu %llu] name too long",
4299 root->objectid, ref_key->objectid, ref_key->offset);
4302 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
4304 /* Check root dir ref name */
4305 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4306 error("root %llu INODE_REF[%llu %llu] ROOT_DIR name shouldn't be %s",
4307 root->objectid, ref_key->objectid, ref_key->offset,
4309 err |= ROOT_DIR_ERROR;
4312 /* Find related DIR_INDEX */
4313 key.objectid = ref_key->offset;
4314 key.type = BTRFS_DIR_INDEX_KEY;
4316 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4319 /* Find related dir_item */
4320 key.objectid = ref_key->offset;
4321 key.type = BTRFS_DIR_ITEM_KEY;
4322 key.offset = btrfs_name_hash(namebuf, len);
4323 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4326 len = sizeof(*ref) + name_len;
4327 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4336 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
4337 * DIR_ITEM/DIR_INDEX.
4339 * @root: the root of the fs/file tree
4340 * @ref_key: the key of the INODE_EXTREF
4341 * @refs: the count of INODE_EXTREF
4342 * @mode: the st_mode of INODE_ITEM
4344 * Return 0 if no error occurred.
4346 static int check_inode_extref(struct btrfs_root *root,
4347 struct btrfs_key *ref_key,
4348 struct extent_buffer *node, int slot, u64 *refs,
4351 struct btrfs_key key;
4352 struct btrfs_inode_extref *extref;
4353 char namebuf[BTRFS_NAME_LEN] = {0};
4363 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4364 total = btrfs_item_size_nr(node, slot);
4367 /* update inode ref count */
4369 name_len = btrfs_inode_extref_name_len(node, extref);
4370 index = btrfs_inode_extref_index(node, extref);
4371 parent = btrfs_inode_extref_parent(node, extref);
4372 if (name_len <= BTRFS_NAME_LEN) {
4375 len = BTRFS_NAME_LEN;
4376 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
4377 root->objectid, ref_key->objectid, ref_key->offset);
4379 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
4381 /* Check root dir ref name */
4382 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4383 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
4384 root->objectid, ref_key->objectid, ref_key->offset,
4386 err |= ROOT_DIR_ERROR;
4389 /* find related dir_index */
4390 key.objectid = parent;
4391 key.type = BTRFS_DIR_INDEX_KEY;
4393 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4396 /* find related dir_item */
4397 key.objectid = parent;
4398 key.type = BTRFS_DIR_ITEM_KEY;
4399 key.offset = btrfs_name_hash(namebuf, len);
4400 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4403 len = sizeof(*extref) + name_len;
4404 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4414 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
4415 * DIR_ITEM/DIR_INDEX match.
4417 * @root: the root of the fs/file tree
4418 * @key: the key of the INODE_REF/INODE_EXTREF
4419 * @name: the name in the INODE_REF/INODE_EXTREF
4420 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4421 * @index: the index in the INODE_REF/INODE_EXTREF, for DIR_ITEM set index
4423 * @ext_ref: the EXTENDED_IREF feature
4425 * Return 0 if no error occurred.
4426 * Return >0 for error bitmap
4428 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
4429 char *name, int namelen, u64 index,
4430 unsigned int ext_ref)
4432 struct btrfs_path path;
4433 struct btrfs_inode_ref *ref;
4434 struct btrfs_inode_extref *extref;
4435 struct extent_buffer *node;
4436 char ref_namebuf[BTRFS_NAME_LEN] = {0};
4447 btrfs_init_path(&path);
4448 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4450 ret = INODE_REF_MISSING;
4454 node = path.nodes[0];
4455 slot = path.slots[0];
4457 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4458 total = btrfs_item_size_nr(node, slot);
4460 /* Iterate all entry of INODE_REF */
4461 while (cur < total) {
4462 ret = INODE_REF_MISSING;
4464 ref_namelen = btrfs_inode_ref_name_len(node, ref);
4465 ref_index = btrfs_inode_ref_index(node, ref);
4466 if (index != (u64)-1 && index != ref_index)
4469 if (ref_namelen <= BTRFS_NAME_LEN) {
4472 len = BTRFS_NAME_LEN;
4473 warning("root %llu INODE %s[%llu %llu] name too long",
4475 key->type == BTRFS_INODE_REF_KEY ?
4477 key->objectid, key->offset);
4479 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
4482 if (len != namelen || strncmp(ref_namebuf, name, len))
4488 len = sizeof(*ref) + ref_namelen;
4489 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4494 /* Skip if not support EXTENDED_IREF feature */
4498 btrfs_release_path(&path);
4499 btrfs_init_path(&path);
4501 dir_id = key->offset;
4502 key->type = BTRFS_INODE_EXTREF_KEY;
4503 key->offset = btrfs_extref_hash(dir_id, name, namelen);
4505 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4507 ret = INODE_REF_MISSING;
4511 node = path.nodes[0];
4512 slot = path.slots[0];
4514 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4516 total = btrfs_item_size_nr(node, slot);
4518 /* Iterate all entry of INODE_EXTREF */
4519 while (cur < total) {
4520 ret = INODE_REF_MISSING;
4522 ref_namelen = btrfs_inode_extref_name_len(node, extref);
4523 ref_index = btrfs_inode_extref_index(node, extref);
4524 parent = btrfs_inode_extref_parent(node, extref);
4525 if (index != (u64)-1 && index != ref_index)
4528 if (parent != dir_id)
4531 if (ref_namelen <= BTRFS_NAME_LEN) {
4534 len = BTRFS_NAME_LEN;
4535 warning("root %llu INODE %s[%llu %llu] name too long",
4537 key->type == BTRFS_INODE_REF_KEY ?
4539 key->objectid, key->offset);
4541 read_extent_buffer(node, ref_namebuf,
4542 (unsigned long)(extref + 1), len);
4544 if (len != namelen || strncmp(ref_namebuf, name, len))
4551 len = sizeof(*extref) + ref_namelen;
4552 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4557 btrfs_release_path(&path);
4562 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
4563 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
4565 * @root: the root of the fs/file tree
4566 * @key: the key of the INODE_REF/INODE_EXTREF
4567 * @size: the st_size of the INODE_ITEM
4568 * @ext_ref: the EXTENDED_IREF feature
4570 * Return 0 if no error occurred.
4572 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *key,
4573 struct extent_buffer *node, int slot, u64 *size,
4574 unsigned int ext_ref)
4576 struct btrfs_dir_item *di;
4577 struct btrfs_inode_item *ii;
4578 struct btrfs_path path;
4579 struct btrfs_key location;
4580 char namebuf[BTRFS_NAME_LEN] = {0};
4593 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
4594 * ignore index check.
4596 index = (key->type == BTRFS_DIR_INDEX_KEY) ? key->offset : (u64)-1;
4598 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4599 total = btrfs_item_size_nr(node, slot);
4601 while (cur < total) {
4602 data_len = btrfs_dir_data_len(node, di);
4604 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
4605 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4606 "DIR_ITEM" : "DIR_INDEX",
4607 key->objectid, key->offset, data_len);
4609 name_len = btrfs_dir_name_len(node, di);
4610 if (name_len <= BTRFS_NAME_LEN) {
4613 len = BTRFS_NAME_LEN;
4614 warning("root %llu %s[%llu %llu] name too long",
4616 key->type == BTRFS_DIR_ITEM_KEY ?
4617 "DIR_ITEM" : "DIR_INDEX",
4618 key->objectid, key->offset);
4620 (*size) += name_len;
4622 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4623 filetype = btrfs_dir_type(node, di);
4625 btrfs_init_path(&path);
4626 btrfs_dir_item_key_to_cpu(node, di, &location);
4628 /* Ignore related ROOT_ITEM check */
4629 if (location.type == BTRFS_ROOT_ITEM_KEY)
4632 /* Check relative INODE_ITEM(existence/filetype) */
4633 ret = btrfs_search_slot(NULL, root, &location, &path, 0, 0);
4635 err |= INODE_ITEM_MISSING;
4636 error("root %llu %s[%llu %llu] couldn't find relative INODE_ITEM[%llu] namelen %u filename %s filetype %x",
4637 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4638 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4639 key->offset, location.objectid, name_len,
4644 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
4645 struct btrfs_inode_item);
4646 mode = btrfs_inode_mode(path.nodes[0], ii);
4648 if (imode_to_type(mode) != filetype) {
4649 err |= INODE_ITEM_MISMATCH;
4650 error("root %llu %s[%llu %llu] relative INODE_ITEM filetype mismatch namelen %u filename %s filetype %d",
4651 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4652 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4653 key->offset, name_len, namebuf, filetype);
4656 /* Check relative INODE_REF/INODE_EXTREF */
4657 location.type = BTRFS_INODE_REF_KEY;
4658 location.offset = key->objectid;
4659 ret = find_inode_ref(root, &location, namebuf, len,
4662 if (ret & INODE_REF_MISSING)
4663 error("root %llu %s[%llu %llu] relative INODE_REF missing namelen %u filename %s filetype %d",
4664 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4665 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4666 key->offset, name_len, namebuf, filetype);
4669 btrfs_release_path(&path);
4670 len = sizeof(*di) + name_len + data_len;
4671 di = (struct btrfs_dir_item *)((char *)di + len);
4674 if (key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
4675 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
4676 root->objectid, key->objectid, key->offset);
4685 * Check file extent datasum/hole, update the size of the file extents,
4686 * check and update the last offset of the file extent.
4688 * @root: the root of fs/file tree.
4689 * @fkey: the key of the file extent.
4690 * @nodatasum: INODE_NODATASUM feature.
4691 * @size: the sum of all EXTENT_DATA items size for this inode.
4692 * @end: the offset of the last extent.
4694 * Return 0 if no error occurred.
4696 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
4697 struct extent_buffer *node, int slot,
4698 unsigned int nodatasum, u64 *size, u64 *end)
4700 struct btrfs_file_extent_item *fi;
4703 u64 extent_num_bytes;
4705 unsigned int extent_type;
4706 unsigned int is_hole;
4710 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
4712 extent_type = btrfs_file_extent_type(node, fi);
4713 /* Skip if file extent is inline */
4714 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4715 struct btrfs_item *e = btrfs_item_nr(slot);
4716 u32 item_inline_len;
4718 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
4719 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
4720 if (extent_num_bytes == 0 ||
4721 extent_num_bytes != item_inline_len)
4722 err |= FILE_EXTENT_ERROR;
4723 *size += extent_num_bytes;
4727 /* Check extent type */
4728 if (extent_type != BTRFS_FILE_EXTENT_REG &&
4729 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
4730 err |= FILE_EXTENT_ERROR;
4731 error("root %llu EXTENT_DATA[%llu %llu] type bad",
4732 root->objectid, fkey->objectid, fkey->offset);
4736 /* Check REG_EXTENT/PREALLOC_EXTENT */
4737 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
4738 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
4739 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
4740 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
4742 /* Check EXTENT_DATA datasum */
4743 ret = count_csum_range(root, disk_bytenr, disk_num_bytes, &found);
4744 if (found > 0 && nodatasum) {
4745 err |= ODD_CSUM_ITEM;
4746 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
4747 root->objectid, fkey->objectid, fkey->offset);
4748 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
4750 (ret < 0 || found == 0 || found < disk_num_bytes)) {
4751 err |= CSUM_ITEM_MISSING;
4752 error("root %llu EXTENT_DATA[%llu %llu] datasum missing",
4753 root->objectid, fkey->objectid, fkey->offset);
4754 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC && found > 0) {
4755 err |= ODD_CSUM_ITEM;
4756 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have datasum",
4757 root->objectid, fkey->objectid, fkey->offset);
4760 /* Check EXTENT_DATA hole */
4761 if (no_holes && is_hole) {
4762 err |= FILE_EXTENT_ERROR;
4763 error("root %llu EXTENT_DATA[%llu %llu] shouldn't be hole",
4764 root->objectid, fkey->objectid, fkey->offset);
4765 } else if (!no_holes && *end != fkey->offset) {
4766 err |= FILE_EXTENT_ERROR;
4767 error("root %llu EXTENT_DATA[%llu %llu] interrupt",
4768 root->objectid, fkey->objectid, fkey->offset);
4771 *end += extent_num_bytes;
4773 *size += extent_num_bytes;
4779 * Check INODE_ITEM and related ITEMs (the same inode number)
4780 * 1. check link count
4781 * 2. check inode ref/extref
4782 * 3. check dir item/index
4784 * @ext_ref: the EXTENDED_IREF feature
4786 * Return 0 if no error occurred.
4787 * Return >0 for error or hit the traversal is done(by error bitmap)
4789 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
4790 unsigned int ext_ref)
4792 struct extent_buffer *node;
4793 struct btrfs_inode_item *ii;
4794 struct btrfs_key key;
4803 u64 extent_size = 0;
4805 unsigned int nodatasum;
4810 node = path->nodes[0];
4811 slot = path->slots[0];
4813 btrfs_item_key_to_cpu(node, &key, slot);
4814 inode_id = key.objectid;
4816 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
4817 ret = btrfs_next_item(root, path);
4823 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
4824 isize = btrfs_inode_size(node, ii);
4825 nbytes = btrfs_inode_nbytes(node, ii);
4826 mode = btrfs_inode_mode(node, ii);
4827 dir = imode_to_type(mode) == BTRFS_FT_DIR;
4828 nlink = btrfs_inode_nlink(node, ii);
4829 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
4832 ret = btrfs_next_item(root, path);
4834 /* out will fill 'err' rusing current statistics */
4836 } else if (ret > 0) {
4841 node = path->nodes[0];
4842 slot = path->slots[0];
4843 btrfs_item_key_to_cpu(node, &key, slot);
4844 if (key.objectid != inode_id)
4848 case BTRFS_INODE_REF_KEY:
4849 ret = check_inode_ref(root, &key, node, slot, &refs,
4853 case BTRFS_INODE_EXTREF_KEY:
4854 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
4855 warning("root %llu EXTREF[%llu %llu] isn't supported",
4856 root->objectid, key.objectid,
4858 ret = check_inode_extref(root, &key, node, slot, &refs,
4862 case BTRFS_DIR_ITEM_KEY:
4863 case BTRFS_DIR_INDEX_KEY:
4865 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
4866 root->objectid, inode_id,
4867 imode_to_type(mode), key.objectid,
4870 ret = check_dir_item(root, &key, node, slot, &size,
4874 case BTRFS_EXTENT_DATA_KEY:
4876 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
4877 root->objectid, inode_id, key.objectid,
4880 ret = check_file_extent(root, &key, node, slot,
4881 nodatasum, &extent_size,
4885 case BTRFS_XATTR_ITEM_KEY:
4888 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
4889 key.objectid, key.type, key.offset);
4894 /* verify INODE_ITEM nlink/isize/nbytes */
4897 err |= LINK_COUNT_ERROR;
4898 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
4899 root->objectid, inode_id, nlink);
4903 * Just a warning, as dir inode nbytes is just an
4904 * instructive value.
4906 if (!IS_ALIGNED(nbytes, root->nodesize)) {
4907 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
4908 root->objectid, inode_id, root->nodesize);
4911 if (isize != size) {
4913 error("root %llu DIR INODE [%llu] size(%llu) not equal to %llu",
4914 root->objectid, inode_id, isize, size);
4917 if (nlink != refs) {
4918 err |= LINK_COUNT_ERROR;
4919 error("root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
4920 root->objectid, inode_id, nlink, refs);
4921 } else if (!nlink) {
4925 if (!nbytes && !no_holes && extent_end < isize) {
4926 err |= NBYTES_ERROR;
4927 error("root %llu INODE[%llu] size (%llu) should have a file extent hole",
4928 root->objectid, inode_id, isize);
4931 if (nbytes != extent_size) {
4932 err |= NBYTES_ERROR;
4933 error("root %llu INODE[%llu] nbytes(%llu) not equal to extent_size(%llu)",
4934 root->objectid, inode_id, nbytes, extent_size);
4941 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
4943 struct btrfs_path path;
4944 struct btrfs_key key;
4948 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4949 key.type = BTRFS_INODE_ITEM_KEY;
4952 /* For root being dropped, we don't need to check first inode */
4953 if (btrfs_root_refs(&root->root_item) == 0 &&
4954 btrfs_disk_key_objectid(&root->root_item.drop_progress) >=
4958 btrfs_init_path(&path);
4960 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4965 err |= INODE_ITEM_MISSING;
4968 err |= check_inode_item(root, &path, ext_ref);
4973 btrfs_release_path(&path);
4978 * Iterate all item on the tree and call check_inode_item() to check.
4980 * @root: the root of the tree to be checked.
4981 * @ext_ref: the EXTENDED_IREF feature
4983 * Return 0 if no error found.
4984 * Return <0 for error.
4986 static int check_fs_root_v2(struct btrfs_root *root, unsigned int ext_ref)
4988 struct btrfs_path path;
4989 struct node_refs nrefs;
4990 struct btrfs_root_item *root_item = &root->root_item;
4995 * We need to manually check the first inode item(256)
4996 * As the following traversal function will only start from
4997 * the first inode item in the leaf, if inode item(256) is missing
4998 * we will just skip it forever.
5000 ret = check_fs_first_inode(root, ext_ref);
5004 memset(&nrefs, 0, sizeof(nrefs));
5005 level = btrfs_header_level(root->node);
5006 btrfs_init_path(&path);
5008 if (btrfs_root_refs(root_item) > 0 ||
5009 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5010 path.nodes[level] = root->node;
5011 path.slots[level] = 0;
5012 extent_buffer_get(root->node);
5014 struct btrfs_key key;
5016 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5017 level = root_item->drop_level;
5018 path.lowest_level = level;
5019 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
5026 wret = walk_down_tree_v2(root, &path, &level, &nrefs, ext_ref);
5032 wret = walk_up_tree_v2(root, &path, &level);
5040 btrfs_release_path(&path);
5045 * Find the relative ref for root_ref and root_backref.
5047 * @root: the root of the root tree.
5048 * @ref_key: the key of the root ref.
5050 * Return 0 if no error occurred.
5052 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
5053 struct extent_buffer *node, int slot)
5055 struct btrfs_path path;
5056 struct btrfs_key key;
5057 struct btrfs_root_ref *ref;
5058 struct btrfs_root_ref *backref;
5059 char ref_name[BTRFS_NAME_LEN] = {0};
5060 char backref_name[BTRFS_NAME_LEN] = {0};
5066 u32 backref_namelen;
5071 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
5072 ref_dirid = btrfs_root_ref_dirid(node, ref);
5073 ref_seq = btrfs_root_ref_sequence(node, ref);
5074 ref_namelen = btrfs_root_ref_name_len(node, ref);
5076 if (ref_namelen <= BTRFS_NAME_LEN) {
5079 len = BTRFS_NAME_LEN;
5080 warning("%s[%llu %llu] ref_name too long",
5081 ref_key->type == BTRFS_ROOT_REF_KEY ?
5082 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
5085 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
5087 /* Find relative root_ref */
5088 key.objectid = ref_key->offset;
5089 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
5090 key.offset = ref_key->objectid;
5092 btrfs_init_path(&path);
5093 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
5095 err |= ROOT_REF_MISSING;
5096 error("%s[%llu %llu] couldn't find relative ref",
5097 ref_key->type == BTRFS_ROOT_REF_KEY ?
5098 "ROOT_REF" : "ROOT_BACKREF",
5099 ref_key->objectid, ref_key->offset);
5103 backref = btrfs_item_ptr(path.nodes[0], path.slots[0],
5104 struct btrfs_root_ref);
5105 backref_dirid = btrfs_root_ref_dirid(path.nodes[0], backref);
5106 backref_seq = btrfs_root_ref_sequence(path.nodes[0], backref);
5107 backref_namelen = btrfs_root_ref_name_len(path.nodes[0], backref);
5109 if (backref_namelen <= BTRFS_NAME_LEN) {
5110 len = backref_namelen;
5112 len = BTRFS_NAME_LEN;
5113 warning("%s[%llu %llu] ref_name too long",
5114 key.type == BTRFS_ROOT_REF_KEY ?
5115 "ROOT_REF" : "ROOT_BACKREF",
5116 key.objectid, key.offset);
5118 read_extent_buffer(path.nodes[0], backref_name,
5119 (unsigned long)(backref + 1), len);
5121 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
5122 ref_namelen != backref_namelen ||
5123 strncmp(ref_name, backref_name, len)) {
5124 err |= ROOT_REF_MISMATCH;
5125 error("%s[%llu %llu] mismatch relative ref",
5126 ref_key->type == BTRFS_ROOT_REF_KEY ?
5127 "ROOT_REF" : "ROOT_BACKREF",
5128 ref_key->objectid, ref_key->offset);
5131 btrfs_release_path(&path);
5136 * Check all fs/file tree in low_memory mode.
5138 * 1. for fs tree root item, call check_fs_root_v2()
5139 * 2. for fs tree root ref/backref, call check_root_ref()
5141 * Return 0 if no error occurred.
5143 static int check_fs_roots_v2(struct btrfs_fs_info *fs_info)
5145 struct btrfs_root *tree_root = fs_info->tree_root;
5146 struct btrfs_root *cur_root = NULL;
5147 struct btrfs_path path;
5148 struct btrfs_key key;
5149 struct extent_buffer *node;
5150 unsigned int ext_ref;
5155 ext_ref = btrfs_fs_incompat(fs_info, EXTENDED_IREF);
5157 btrfs_init_path(&path);
5158 key.objectid = BTRFS_FS_TREE_OBJECTID;
5160 key.type = BTRFS_ROOT_ITEM_KEY;
5162 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
5166 } else if (ret > 0) {
5172 node = path.nodes[0];
5173 slot = path.slots[0];
5174 btrfs_item_key_to_cpu(node, &key, slot);
5175 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
5177 if (key.type == BTRFS_ROOT_ITEM_KEY &&
5178 fs_root_objectid(key.objectid)) {
5179 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
5180 cur_root = btrfs_read_fs_root_no_cache(fs_info,
5183 key.offset = (u64)-1;
5184 cur_root = btrfs_read_fs_root(fs_info, &key);
5187 if (IS_ERR(cur_root)) {
5188 error("Fail to read fs/subvol tree: %lld",
5194 ret = check_fs_root_v2(cur_root, ext_ref);
5197 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5198 btrfs_free_fs_root(cur_root);
5199 } else if (key.type == BTRFS_ROOT_REF_KEY ||
5200 key.type == BTRFS_ROOT_BACKREF_KEY) {
5201 ret = check_root_ref(tree_root, &key, node, slot);
5205 ret = btrfs_next_item(tree_root, &path);
5215 btrfs_release_path(&path);
5219 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
5221 struct list_head *cur = rec->backrefs.next;
5222 struct extent_backref *back;
5223 struct tree_backref *tback;
5224 struct data_backref *dback;
5228 while(cur != &rec->backrefs) {
5229 back = to_extent_backref(cur);
5231 if (!back->found_extent_tree) {
5235 if (back->is_data) {
5236 dback = to_data_backref(back);
5237 fprintf(stderr, "Backref %llu %s %llu"
5238 " owner %llu offset %llu num_refs %lu"
5239 " not found in extent tree\n",
5240 (unsigned long long)rec->start,
5241 back->full_backref ?
5243 back->full_backref ?
5244 (unsigned long long)dback->parent:
5245 (unsigned long long)dback->root,
5246 (unsigned long long)dback->owner,
5247 (unsigned long long)dback->offset,
5248 (unsigned long)dback->num_refs);
5250 tback = to_tree_backref(back);
5251 fprintf(stderr, "Backref %llu parent %llu"
5252 " root %llu not found in extent tree\n",
5253 (unsigned long long)rec->start,
5254 (unsigned long long)tback->parent,
5255 (unsigned long long)tback->root);
5258 if (!back->is_data && !back->found_ref) {
5262 tback = to_tree_backref(back);
5263 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
5264 (unsigned long long)rec->start,
5265 back->full_backref ? "parent" : "root",
5266 back->full_backref ?
5267 (unsigned long long)tback->parent :
5268 (unsigned long long)tback->root, back);
5270 if (back->is_data) {
5271 dback = to_data_backref(back);
5272 if (dback->found_ref != dback->num_refs) {
5276 fprintf(stderr, "Incorrect local backref count"
5277 " on %llu %s %llu owner %llu"
5278 " offset %llu found %u wanted %u back %p\n",
5279 (unsigned long long)rec->start,
5280 back->full_backref ?
5282 back->full_backref ?
5283 (unsigned long long)dback->parent:
5284 (unsigned long long)dback->root,
5285 (unsigned long long)dback->owner,
5286 (unsigned long long)dback->offset,
5287 dback->found_ref, dback->num_refs, back);
5289 if (dback->disk_bytenr != rec->start) {
5293 fprintf(stderr, "Backref disk bytenr does not"
5294 " match extent record, bytenr=%llu, "
5295 "ref bytenr=%llu\n",
5296 (unsigned long long)rec->start,
5297 (unsigned long long)dback->disk_bytenr);
5300 if (dback->bytes != rec->nr) {
5304 fprintf(stderr, "Backref bytes do not match "
5305 "extent backref, bytenr=%llu, ref "
5306 "bytes=%llu, backref bytes=%llu\n",
5307 (unsigned long long)rec->start,
5308 (unsigned long long)rec->nr,
5309 (unsigned long long)dback->bytes);
5312 if (!back->is_data) {
5315 dback = to_data_backref(back);
5316 found += dback->found_ref;
5319 if (found != rec->refs) {
5323 fprintf(stderr, "Incorrect global backref count "
5324 "on %llu found %llu wanted %llu\n",
5325 (unsigned long long)rec->start,
5326 (unsigned long long)found,
5327 (unsigned long long)rec->refs);
5333 static int free_all_extent_backrefs(struct extent_record *rec)
5335 struct extent_backref *back;
5336 struct list_head *cur;
5337 while (!list_empty(&rec->backrefs)) {
5338 cur = rec->backrefs.next;
5339 back = to_extent_backref(cur);
5346 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
5347 struct cache_tree *extent_cache)
5349 struct cache_extent *cache;
5350 struct extent_record *rec;
5353 cache = first_cache_extent(extent_cache);
5356 rec = container_of(cache, struct extent_record, cache);
5357 remove_cache_extent(extent_cache, cache);
5358 free_all_extent_backrefs(rec);
5363 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
5364 struct extent_record *rec)
5366 if (rec->content_checked && rec->owner_ref_checked &&
5367 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
5368 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0) &&
5369 !rec->bad_full_backref && !rec->crossing_stripes &&
5370 !rec->wrong_chunk_type) {
5371 remove_cache_extent(extent_cache, &rec->cache);
5372 free_all_extent_backrefs(rec);
5373 list_del_init(&rec->list);
5379 static int check_owner_ref(struct btrfs_root *root,
5380 struct extent_record *rec,
5381 struct extent_buffer *buf)
5383 struct extent_backref *node;
5384 struct tree_backref *back;
5385 struct btrfs_root *ref_root;
5386 struct btrfs_key key;
5387 struct btrfs_path path;
5388 struct extent_buffer *parent;
5393 list_for_each_entry(node, &rec->backrefs, list) {
5396 if (!node->found_ref)
5398 if (node->full_backref)
5400 back = to_tree_backref(node);
5401 if (btrfs_header_owner(buf) == back->root)
5404 BUG_ON(rec->is_root);
5406 /* try to find the block by search corresponding fs tree */
5407 key.objectid = btrfs_header_owner(buf);
5408 key.type = BTRFS_ROOT_ITEM_KEY;
5409 key.offset = (u64)-1;
5411 ref_root = btrfs_read_fs_root(root->fs_info, &key);
5412 if (IS_ERR(ref_root))
5415 level = btrfs_header_level(buf);
5417 btrfs_item_key_to_cpu(buf, &key, 0);
5419 btrfs_node_key_to_cpu(buf, &key, 0);
5421 btrfs_init_path(&path);
5422 path.lowest_level = level + 1;
5423 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
5427 parent = path.nodes[level + 1];
5428 if (parent && buf->start == btrfs_node_blockptr(parent,
5429 path.slots[level + 1]))
5432 btrfs_release_path(&path);
5433 return found ? 0 : 1;
5436 static int is_extent_tree_record(struct extent_record *rec)
5438 struct list_head *cur = rec->backrefs.next;
5439 struct extent_backref *node;
5440 struct tree_backref *back;
5443 while(cur != &rec->backrefs) {
5444 node = to_extent_backref(cur);
5448 back = to_tree_backref(node);
5449 if (node->full_backref)
5451 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
5458 static int record_bad_block_io(struct btrfs_fs_info *info,
5459 struct cache_tree *extent_cache,
5462 struct extent_record *rec;
5463 struct cache_extent *cache;
5464 struct btrfs_key key;
5466 cache = lookup_cache_extent(extent_cache, start, len);
5470 rec = container_of(cache, struct extent_record, cache);
5471 if (!is_extent_tree_record(rec))
5474 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
5475 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
5478 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
5479 struct extent_buffer *buf, int slot)
5481 if (btrfs_header_level(buf)) {
5482 struct btrfs_key_ptr ptr1, ptr2;
5484 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
5485 sizeof(struct btrfs_key_ptr));
5486 read_extent_buffer(buf, &ptr2,
5487 btrfs_node_key_ptr_offset(slot + 1),
5488 sizeof(struct btrfs_key_ptr));
5489 write_extent_buffer(buf, &ptr1,
5490 btrfs_node_key_ptr_offset(slot + 1),
5491 sizeof(struct btrfs_key_ptr));
5492 write_extent_buffer(buf, &ptr2,
5493 btrfs_node_key_ptr_offset(slot),
5494 sizeof(struct btrfs_key_ptr));
5496 struct btrfs_disk_key key;
5497 btrfs_node_key(buf, &key, 0);
5498 btrfs_fixup_low_keys(root, path, &key,
5499 btrfs_header_level(buf) + 1);
5502 struct btrfs_item *item1, *item2;
5503 struct btrfs_key k1, k2;
5504 char *item1_data, *item2_data;
5505 u32 item1_offset, item2_offset, item1_size, item2_size;
5507 item1 = btrfs_item_nr(slot);
5508 item2 = btrfs_item_nr(slot + 1);
5509 btrfs_item_key_to_cpu(buf, &k1, slot);
5510 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
5511 item1_offset = btrfs_item_offset(buf, item1);
5512 item2_offset = btrfs_item_offset(buf, item2);
5513 item1_size = btrfs_item_size(buf, item1);
5514 item2_size = btrfs_item_size(buf, item2);
5516 item1_data = malloc(item1_size);
5519 item2_data = malloc(item2_size);
5525 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
5526 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
5528 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
5529 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
5533 btrfs_set_item_offset(buf, item1, item2_offset);
5534 btrfs_set_item_offset(buf, item2, item1_offset);
5535 btrfs_set_item_size(buf, item1, item2_size);
5536 btrfs_set_item_size(buf, item2, item1_size);
5538 path->slots[0] = slot;
5539 btrfs_set_item_key_unsafe(root, path, &k2);
5540 path->slots[0] = slot + 1;
5541 btrfs_set_item_key_unsafe(root, path, &k1);
5546 static int fix_key_order(struct btrfs_trans_handle *trans,
5547 struct btrfs_root *root,
5548 struct btrfs_path *path)
5550 struct extent_buffer *buf;
5551 struct btrfs_key k1, k2;
5553 int level = path->lowest_level;
5556 buf = path->nodes[level];
5557 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
5559 btrfs_node_key_to_cpu(buf, &k1, i);
5560 btrfs_node_key_to_cpu(buf, &k2, i + 1);
5562 btrfs_item_key_to_cpu(buf, &k1, i);
5563 btrfs_item_key_to_cpu(buf, &k2, i + 1);
5565 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
5567 ret = swap_values(root, path, buf, i);
5570 btrfs_mark_buffer_dirty(buf);
5576 static int delete_bogus_item(struct btrfs_trans_handle *trans,
5577 struct btrfs_root *root,
5578 struct btrfs_path *path,
5579 struct extent_buffer *buf, int slot)
5581 struct btrfs_key key;
5582 int nritems = btrfs_header_nritems(buf);
5584 btrfs_item_key_to_cpu(buf, &key, slot);
5586 /* These are all the keys we can deal with missing. */
5587 if (key.type != BTRFS_DIR_INDEX_KEY &&
5588 key.type != BTRFS_EXTENT_ITEM_KEY &&
5589 key.type != BTRFS_METADATA_ITEM_KEY &&
5590 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5591 key.type != BTRFS_EXTENT_DATA_REF_KEY)
5594 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
5595 (unsigned long long)key.objectid, key.type,
5596 (unsigned long long)key.offset, slot, buf->start);
5597 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
5598 btrfs_item_nr_offset(slot + 1),
5599 sizeof(struct btrfs_item) *
5600 (nritems - slot - 1));
5601 btrfs_set_header_nritems(buf, nritems - 1);
5603 struct btrfs_disk_key disk_key;
5605 btrfs_item_key(buf, &disk_key, 0);
5606 btrfs_fixup_low_keys(root, path, &disk_key, 1);
5608 btrfs_mark_buffer_dirty(buf);
5612 static int fix_item_offset(struct btrfs_trans_handle *trans,
5613 struct btrfs_root *root,
5614 struct btrfs_path *path)
5616 struct extent_buffer *buf;
5620 /* We should only get this for leaves */
5621 BUG_ON(path->lowest_level);
5622 buf = path->nodes[0];
5624 for (i = 0; i < btrfs_header_nritems(buf); i++) {
5625 unsigned int shift = 0, offset;
5627 if (i == 0 && btrfs_item_end_nr(buf, i) !=
5628 BTRFS_LEAF_DATA_SIZE(root)) {
5629 if (btrfs_item_end_nr(buf, i) >
5630 BTRFS_LEAF_DATA_SIZE(root)) {
5631 ret = delete_bogus_item(trans, root, path,
5635 fprintf(stderr, "item is off the end of the "
5636 "leaf, can't fix\n");
5640 shift = BTRFS_LEAF_DATA_SIZE(root) -
5641 btrfs_item_end_nr(buf, i);
5642 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
5643 btrfs_item_offset_nr(buf, i - 1)) {
5644 if (btrfs_item_end_nr(buf, i) >
5645 btrfs_item_offset_nr(buf, i - 1)) {
5646 ret = delete_bogus_item(trans, root, path,
5650 fprintf(stderr, "items overlap, can't fix\n");
5654 shift = btrfs_item_offset_nr(buf, i - 1) -
5655 btrfs_item_end_nr(buf, i);
5660 printf("Shifting item nr %d by %u bytes in block %llu\n",
5661 i, shift, (unsigned long long)buf->start);
5662 offset = btrfs_item_offset_nr(buf, i);
5663 memmove_extent_buffer(buf,
5664 btrfs_leaf_data(buf) + offset + shift,
5665 btrfs_leaf_data(buf) + offset,
5666 btrfs_item_size_nr(buf, i));
5667 btrfs_set_item_offset(buf, btrfs_item_nr(i),
5669 btrfs_mark_buffer_dirty(buf);
5673 * We may have moved things, in which case we want to exit so we don't
5674 * write those changes out. Once we have proper abort functionality in
5675 * progs this can be changed to something nicer.
5682 * Attempt to fix basic block failures. If we can't fix it for whatever reason
5683 * then just return -EIO.
5685 static int try_to_fix_bad_block(struct btrfs_root *root,
5686 struct extent_buffer *buf,
5687 enum btrfs_tree_block_status status)
5689 struct btrfs_trans_handle *trans;
5690 struct ulist *roots;
5691 struct ulist_node *node;
5692 struct btrfs_root *search_root;
5693 struct btrfs_path path;
5694 struct ulist_iterator iter;
5695 struct btrfs_key root_key, key;
5698 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
5699 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5702 ret = btrfs_find_all_roots(NULL, root->fs_info, buf->start, 0, &roots);
5706 btrfs_init_path(&path);
5707 ULIST_ITER_INIT(&iter);
5708 while ((node = ulist_next(roots, &iter))) {
5709 root_key.objectid = node->val;
5710 root_key.type = BTRFS_ROOT_ITEM_KEY;
5711 root_key.offset = (u64)-1;
5713 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
5720 trans = btrfs_start_transaction(search_root, 0);
5721 if (IS_ERR(trans)) {
5722 ret = PTR_ERR(trans);
5726 path.lowest_level = btrfs_header_level(buf);
5727 path.skip_check_block = 1;
5728 if (path.lowest_level)
5729 btrfs_node_key_to_cpu(buf, &key, 0);
5731 btrfs_item_key_to_cpu(buf, &key, 0);
5732 ret = btrfs_search_slot(trans, search_root, &key, &path, 0, 1);
5735 btrfs_commit_transaction(trans, search_root);
5738 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
5739 ret = fix_key_order(trans, search_root, &path);
5740 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5741 ret = fix_item_offset(trans, search_root, &path);
5743 btrfs_commit_transaction(trans, search_root);
5746 btrfs_release_path(&path);
5747 btrfs_commit_transaction(trans, search_root);
5750 btrfs_release_path(&path);
5754 static int check_block(struct btrfs_root *root,
5755 struct cache_tree *extent_cache,
5756 struct extent_buffer *buf, u64 flags)
5758 struct extent_record *rec;
5759 struct cache_extent *cache;
5760 struct btrfs_key key;
5761 enum btrfs_tree_block_status status;
5765 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
5768 rec = container_of(cache, struct extent_record, cache);
5769 rec->generation = btrfs_header_generation(buf);
5771 level = btrfs_header_level(buf);
5772 if (btrfs_header_nritems(buf) > 0) {
5775 btrfs_item_key_to_cpu(buf, &key, 0);
5777 btrfs_node_key_to_cpu(buf, &key, 0);
5779 rec->info_objectid = key.objectid;
5781 rec->info_level = level;
5783 if (btrfs_is_leaf(buf))
5784 status = btrfs_check_leaf(root, &rec->parent_key, buf);
5786 status = btrfs_check_node(root, &rec->parent_key, buf);
5788 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5790 status = try_to_fix_bad_block(root, buf, status);
5791 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5793 fprintf(stderr, "bad block %llu\n",
5794 (unsigned long long)buf->start);
5797 * Signal to callers we need to start the scan over
5798 * again since we'll have cowed blocks.
5803 rec->content_checked = 1;
5804 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5805 rec->owner_ref_checked = 1;
5807 ret = check_owner_ref(root, rec, buf);
5809 rec->owner_ref_checked = 1;
5813 maybe_free_extent_rec(extent_cache, rec);
5817 static struct tree_backref *find_tree_backref(struct extent_record *rec,
5818 u64 parent, u64 root)
5820 struct list_head *cur = rec->backrefs.next;
5821 struct extent_backref *node;
5822 struct tree_backref *back;
5824 while(cur != &rec->backrefs) {
5825 node = to_extent_backref(cur);
5829 back = to_tree_backref(node);
5831 if (!node->full_backref)
5833 if (parent == back->parent)
5836 if (node->full_backref)
5838 if (back->root == root)
5845 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
5846 u64 parent, u64 root)
5848 struct tree_backref *ref = malloc(sizeof(*ref));
5852 memset(&ref->node, 0, sizeof(ref->node));
5854 ref->parent = parent;
5855 ref->node.full_backref = 1;
5858 ref->node.full_backref = 0;
5860 list_add_tail(&ref->node.list, &rec->backrefs);
5865 static struct data_backref *find_data_backref(struct extent_record *rec,
5866 u64 parent, u64 root,
5867 u64 owner, u64 offset,
5869 u64 disk_bytenr, u64 bytes)
5871 struct list_head *cur = rec->backrefs.next;
5872 struct extent_backref *node;
5873 struct data_backref *back;
5875 while(cur != &rec->backrefs) {
5876 node = to_extent_backref(cur);
5880 back = to_data_backref(node);
5882 if (!node->full_backref)
5884 if (parent == back->parent)
5887 if (node->full_backref)
5889 if (back->root == root && back->owner == owner &&
5890 back->offset == offset) {
5891 if (found_ref && node->found_ref &&
5892 (back->bytes != bytes ||
5893 back->disk_bytenr != disk_bytenr))
5902 static struct data_backref *alloc_data_backref(struct extent_record *rec,
5903 u64 parent, u64 root,
5904 u64 owner, u64 offset,
5907 struct data_backref *ref = malloc(sizeof(*ref));
5911 memset(&ref->node, 0, sizeof(ref->node));
5912 ref->node.is_data = 1;
5915 ref->parent = parent;
5918 ref->node.full_backref = 1;
5922 ref->offset = offset;
5923 ref->node.full_backref = 0;
5925 ref->bytes = max_size;
5928 list_add_tail(&ref->node.list, &rec->backrefs);
5929 if (max_size > rec->max_size)
5930 rec->max_size = max_size;
5934 /* Check if the type of extent matches with its chunk */
5935 static void check_extent_type(struct extent_record *rec)
5937 struct btrfs_block_group_cache *bg_cache;
5939 bg_cache = btrfs_lookup_first_block_group(global_info, rec->start);
5943 /* data extent, check chunk directly*/
5944 if (!rec->metadata) {
5945 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
5946 rec->wrong_chunk_type = 1;
5950 /* metadata extent, check the obvious case first */
5951 if (!(bg_cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
5952 BTRFS_BLOCK_GROUP_METADATA))) {
5953 rec->wrong_chunk_type = 1;
5958 * Check SYSTEM extent, as it's also marked as metadata, we can only
5959 * make sure it's a SYSTEM extent by its backref
5961 if (!list_empty(&rec->backrefs)) {
5962 struct extent_backref *node;
5963 struct tree_backref *tback;
5966 node = to_extent_backref(rec->backrefs.next);
5967 if (node->is_data) {
5968 /* tree block shouldn't have data backref */
5969 rec->wrong_chunk_type = 1;
5972 tback = container_of(node, struct tree_backref, node);
5974 if (tback->root == BTRFS_CHUNK_TREE_OBJECTID)
5975 bg_type = BTRFS_BLOCK_GROUP_SYSTEM;
5977 bg_type = BTRFS_BLOCK_GROUP_METADATA;
5978 if (!(bg_cache->flags & bg_type))
5979 rec->wrong_chunk_type = 1;
5984 * Allocate a new extent record, fill default values from @tmpl and insert int
5985 * @extent_cache. Caller is supposed to make sure the [start,nr) is not in
5986 * the cache, otherwise it fails.
5988 static int add_extent_rec_nolookup(struct cache_tree *extent_cache,
5989 struct extent_record *tmpl)
5991 struct extent_record *rec;
5994 rec = malloc(sizeof(*rec));
5997 rec->start = tmpl->start;
5998 rec->max_size = tmpl->max_size;
5999 rec->nr = max(tmpl->nr, tmpl->max_size);
6000 rec->found_rec = tmpl->found_rec;
6001 rec->content_checked = tmpl->content_checked;
6002 rec->owner_ref_checked = tmpl->owner_ref_checked;
6003 rec->num_duplicates = 0;
6004 rec->metadata = tmpl->metadata;
6005 rec->flag_block_full_backref = FLAG_UNSET;
6006 rec->bad_full_backref = 0;
6007 rec->crossing_stripes = 0;
6008 rec->wrong_chunk_type = 0;
6009 rec->is_root = tmpl->is_root;
6010 rec->refs = tmpl->refs;
6011 rec->extent_item_refs = tmpl->extent_item_refs;
6012 rec->parent_generation = tmpl->parent_generation;
6013 INIT_LIST_HEAD(&rec->backrefs);
6014 INIT_LIST_HEAD(&rec->dups);
6015 INIT_LIST_HEAD(&rec->list);
6016 memcpy(&rec->parent_key, &tmpl->parent_key, sizeof(tmpl->parent_key));
6017 rec->cache.start = tmpl->start;
6018 rec->cache.size = tmpl->nr;
6019 ret = insert_cache_extent(extent_cache, &rec->cache);
6024 bytes_used += rec->nr;
6027 rec->crossing_stripes = check_crossing_stripes(global_info,
6028 rec->start, global_info->tree_root->nodesize);
6029 check_extent_type(rec);
6034 * Lookup and modify an extent, some values of @tmpl are interpreted verbatim,
6036 * - refs - if found, increase refs
6037 * - is_root - if found, set
6038 * - content_checked - if found, set
6039 * - owner_ref_checked - if found, set
6041 * If not found, create a new one, initialize and insert.
6043 static int add_extent_rec(struct cache_tree *extent_cache,
6044 struct extent_record *tmpl)
6046 struct extent_record *rec;
6047 struct cache_extent *cache;
6051 cache = lookup_cache_extent(extent_cache, tmpl->start, tmpl->nr);
6053 rec = container_of(cache, struct extent_record, cache);
6057 rec->nr = max(tmpl->nr, tmpl->max_size);
6060 * We need to make sure to reset nr to whatever the extent
6061 * record says was the real size, this way we can compare it to
6064 if (tmpl->found_rec) {
6065 if (tmpl->start != rec->start || rec->found_rec) {
6066 struct extent_record *tmp;
6069 if (list_empty(&rec->list))
6070 list_add_tail(&rec->list,
6071 &duplicate_extents);
6074 * We have to do this song and dance in case we
6075 * find an extent record that falls inside of
6076 * our current extent record but does not have
6077 * the same objectid.
6079 tmp = malloc(sizeof(*tmp));
6082 tmp->start = tmpl->start;
6083 tmp->max_size = tmpl->max_size;
6086 tmp->metadata = tmpl->metadata;
6087 tmp->extent_item_refs = tmpl->extent_item_refs;
6088 INIT_LIST_HEAD(&tmp->list);
6089 list_add_tail(&tmp->list, &rec->dups);
6090 rec->num_duplicates++;
6097 if (tmpl->extent_item_refs && !dup) {
6098 if (rec->extent_item_refs) {
6099 fprintf(stderr, "block %llu rec "
6100 "extent_item_refs %llu, passed %llu\n",
6101 (unsigned long long)tmpl->start,
6102 (unsigned long long)
6103 rec->extent_item_refs,
6104 (unsigned long long)tmpl->extent_item_refs);
6106 rec->extent_item_refs = tmpl->extent_item_refs;
6110 if (tmpl->content_checked)
6111 rec->content_checked = 1;
6112 if (tmpl->owner_ref_checked)
6113 rec->owner_ref_checked = 1;
6114 memcpy(&rec->parent_key, &tmpl->parent_key,
6115 sizeof(tmpl->parent_key));
6116 if (tmpl->parent_generation)
6117 rec->parent_generation = tmpl->parent_generation;
6118 if (rec->max_size < tmpl->max_size)
6119 rec->max_size = tmpl->max_size;
6122 * A metadata extent can't cross stripe_len boundary, otherwise
6123 * kernel scrub won't be able to handle it.
6124 * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
6128 rec->crossing_stripes = check_crossing_stripes(
6129 global_info, rec->start,
6130 global_info->tree_root->nodesize);
6131 check_extent_type(rec);
6132 maybe_free_extent_rec(extent_cache, rec);
6136 ret = add_extent_rec_nolookup(extent_cache, tmpl);
6141 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
6142 u64 parent, u64 root, int found_ref)
6144 struct extent_record *rec;
6145 struct tree_backref *back;
6146 struct cache_extent *cache;
6149 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6151 struct extent_record tmpl;
6153 memset(&tmpl, 0, sizeof(tmpl));
6154 tmpl.start = bytenr;
6158 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6162 /* really a bug in cache_extent implement now */
6163 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6168 rec = container_of(cache, struct extent_record, cache);
6169 if (rec->start != bytenr) {
6171 * Several cause, from unaligned bytenr to over lapping extents
6176 back = find_tree_backref(rec, parent, root);
6178 back = alloc_tree_backref(rec, parent, root);
6184 if (back->node.found_ref) {
6185 fprintf(stderr, "Extent back ref already exists "
6186 "for %llu parent %llu root %llu \n",
6187 (unsigned long long)bytenr,
6188 (unsigned long long)parent,
6189 (unsigned long long)root);
6191 back->node.found_ref = 1;
6193 if (back->node.found_extent_tree) {
6194 fprintf(stderr, "Extent back ref already exists "
6195 "for %llu parent %llu root %llu \n",
6196 (unsigned long long)bytenr,
6197 (unsigned long long)parent,
6198 (unsigned long long)root);
6200 back->node.found_extent_tree = 1;
6202 check_extent_type(rec);
6203 maybe_free_extent_rec(extent_cache, rec);
6207 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
6208 u64 parent, u64 root, u64 owner, u64 offset,
6209 u32 num_refs, int found_ref, u64 max_size)
6211 struct extent_record *rec;
6212 struct data_backref *back;
6213 struct cache_extent *cache;
6216 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6218 struct extent_record tmpl;
6220 memset(&tmpl, 0, sizeof(tmpl));
6221 tmpl.start = bytenr;
6223 tmpl.max_size = max_size;
6225 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6229 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6234 rec = container_of(cache, struct extent_record, cache);
6235 if (rec->max_size < max_size)
6236 rec->max_size = max_size;
6239 * If found_ref is set then max_size is the real size and must match the
6240 * existing refs. So if we have already found a ref then we need to
6241 * make sure that this ref matches the existing one, otherwise we need
6242 * to add a new backref so we can notice that the backrefs don't match
6243 * and we need to figure out who is telling the truth. This is to
6244 * account for that awful fsync bug I introduced where we'd end up with
6245 * a btrfs_file_extent_item that would have its length include multiple
6246 * prealloc extents or point inside of a prealloc extent.
6248 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
6251 back = alloc_data_backref(rec, parent, root, owner, offset,
6257 BUG_ON(num_refs != 1);
6258 if (back->node.found_ref)
6259 BUG_ON(back->bytes != max_size);
6260 back->node.found_ref = 1;
6261 back->found_ref += 1;
6262 back->bytes = max_size;
6263 back->disk_bytenr = bytenr;
6265 rec->content_checked = 1;
6266 rec->owner_ref_checked = 1;
6268 if (back->node.found_extent_tree) {
6269 fprintf(stderr, "Extent back ref already exists "
6270 "for %llu parent %llu root %llu "
6271 "owner %llu offset %llu num_refs %lu\n",
6272 (unsigned long long)bytenr,
6273 (unsigned long long)parent,
6274 (unsigned long long)root,
6275 (unsigned long long)owner,
6276 (unsigned long long)offset,
6277 (unsigned long)num_refs);
6279 back->num_refs = num_refs;
6280 back->node.found_extent_tree = 1;
6282 maybe_free_extent_rec(extent_cache, rec);
6286 static int add_pending(struct cache_tree *pending,
6287 struct cache_tree *seen, u64 bytenr, u32 size)
6290 ret = add_cache_extent(seen, bytenr, size);
6293 add_cache_extent(pending, bytenr, size);
6297 static int pick_next_pending(struct cache_tree *pending,
6298 struct cache_tree *reada,
6299 struct cache_tree *nodes,
6300 u64 last, struct block_info *bits, int bits_nr,
6303 unsigned long node_start = last;
6304 struct cache_extent *cache;
6307 cache = search_cache_extent(reada, 0);
6309 bits[0].start = cache->start;
6310 bits[0].size = cache->size;
6315 if (node_start > 32768)
6316 node_start -= 32768;
6318 cache = search_cache_extent(nodes, node_start);
6320 cache = search_cache_extent(nodes, 0);
6323 cache = search_cache_extent(pending, 0);
6328 bits[ret].start = cache->start;
6329 bits[ret].size = cache->size;
6330 cache = next_cache_extent(cache);
6332 } while (cache && ret < bits_nr);
6338 bits[ret].start = cache->start;
6339 bits[ret].size = cache->size;
6340 cache = next_cache_extent(cache);
6342 } while (cache && ret < bits_nr);
6344 if (bits_nr - ret > 8) {
6345 u64 lookup = bits[0].start + bits[0].size;
6346 struct cache_extent *next;
6347 next = search_cache_extent(pending, lookup);
6349 if (next->start - lookup > 32768)
6351 bits[ret].start = next->start;
6352 bits[ret].size = next->size;
6353 lookup = next->start + next->size;
6357 next = next_cache_extent(next);
6365 static void free_chunk_record(struct cache_extent *cache)
6367 struct chunk_record *rec;
6369 rec = container_of(cache, struct chunk_record, cache);
6370 list_del_init(&rec->list);
6371 list_del_init(&rec->dextents);
6375 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
6377 cache_tree_free_extents(chunk_cache, free_chunk_record);
6380 static void free_device_record(struct rb_node *node)
6382 struct device_record *rec;
6384 rec = container_of(node, struct device_record, node);
6388 FREE_RB_BASED_TREE(device_cache, free_device_record);
6390 int insert_block_group_record(struct block_group_tree *tree,
6391 struct block_group_record *bg_rec)
6395 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
6399 list_add_tail(&bg_rec->list, &tree->block_groups);
6403 static void free_block_group_record(struct cache_extent *cache)
6405 struct block_group_record *rec;
6407 rec = container_of(cache, struct block_group_record, cache);
6408 list_del_init(&rec->list);
6412 void free_block_group_tree(struct block_group_tree *tree)
6414 cache_tree_free_extents(&tree->tree, free_block_group_record);
6417 int insert_device_extent_record(struct device_extent_tree *tree,
6418 struct device_extent_record *de_rec)
6423 * Device extent is a bit different from the other extents, because
6424 * the extents which belong to the different devices may have the
6425 * same start and size, so we need use the special extent cache
6426 * search/insert functions.
6428 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
6432 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
6433 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
6437 static void free_device_extent_record(struct cache_extent *cache)
6439 struct device_extent_record *rec;
6441 rec = container_of(cache, struct device_extent_record, cache);
6442 if (!list_empty(&rec->chunk_list))
6443 list_del_init(&rec->chunk_list);
6444 if (!list_empty(&rec->device_list))
6445 list_del_init(&rec->device_list);
6449 void free_device_extent_tree(struct device_extent_tree *tree)
6451 cache_tree_free_extents(&tree->tree, free_device_extent_record);
6454 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6455 static int process_extent_ref_v0(struct cache_tree *extent_cache,
6456 struct extent_buffer *leaf, int slot)
6458 struct btrfs_extent_ref_v0 *ref0;
6459 struct btrfs_key key;
6462 btrfs_item_key_to_cpu(leaf, &key, slot);
6463 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
6464 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
6465 ret = add_tree_backref(extent_cache, key.objectid, key.offset,
6468 ret = add_data_backref(extent_cache, key.objectid, key.offset,
6469 0, 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
6475 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
6476 struct btrfs_key *key,
6479 struct btrfs_chunk *ptr;
6480 struct chunk_record *rec;
6483 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6484 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
6486 rec = calloc(1, btrfs_chunk_record_size(num_stripes));
6488 fprintf(stderr, "memory allocation failed\n");
6492 INIT_LIST_HEAD(&rec->list);
6493 INIT_LIST_HEAD(&rec->dextents);
6496 rec->cache.start = key->offset;
6497 rec->cache.size = btrfs_chunk_length(leaf, ptr);
6499 rec->generation = btrfs_header_generation(leaf);
6501 rec->objectid = key->objectid;
6502 rec->type = key->type;
6503 rec->offset = key->offset;
6505 rec->length = rec->cache.size;
6506 rec->owner = btrfs_chunk_owner(leaf, ptr);
6507 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
6508 rec->type_flags = btrfs_chunk_type(leaf, ptr);
6509 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
6510 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
6511 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
6512 rec->num_stripes = num_stripes;
6513 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
6515 for (i = 0; i < rec->num_stripes; ++i) {
6516 rec->stripes[i].devid =
6517 btrfs_stripe_devid_nr(leaf, ptr, i);
6518 rec->stripes[i].offset =
6519 btrfs_stripe_offset_nr(leaf, ptr, i);
6520 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
6521 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
6528 static int process_chunk_item(struct cache_tree *chunk_cache,
6529 struct btrfs_key *key, struct extent_buffer *eb,
6532 struct chunk_record *rec;
6533 struct btrfs_chunk *chunk;
6536 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
6538 * Do extra check for this chunk item,
6540 * It's still possible one can craft a leaf with CHUNK_ITEM, with
6541 * wrong onwer(3) out of chunk tree, to pass both chunk tree check
6542 * and owner<->key_type check.
6544 ret = btrfs_check_chunk_valid(global_info->tree_root, eb, chunk, slot,
6547 error("chunk(%llu, %llu) is not valid, ignore it",
6548 key->offset, btrfs_chunk_length(eb, chunk));
6551 rec = btrfs_new_chunk_record(eb, key, slot);
6552 ret = insert_cache_extent(chunk_cache, &rec->cache);
6554 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
6555 rec->offset, rec->length);
6562 static int process_device_item(struct rb_root *dev_cache,
6563 struct btrfs_key *key, struct extent_buffer *eb, int slot)
6565 struct btrfs_dev_item *ptr;
6566 struct device_record *rec;
6569 ptr = btrfs_item_ptr(eb,
6570 slot, struct btrfs_dev_item);
6572 rec = malloc(sizeof(*rec));
6574 fprintf(stderr, "memory allocation failed\n");
6578 rec->devid = key->offset;
6579 rec->generation = btrfs_header_generation(eb);
6581 rec->objectid = key->objectid;
6582 rec->type = key->type;
6583 rec->offset = key->offset;
6585 rec->devid = btrfs_device_id(eb, ptr);
6586 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
6587 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
6589 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
6591 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
6598 struct block_group_record *
6599 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
6602 struct btrfs_block_group_item *ptr;
6603 struct block_group_record *rec;
6605 rec = calloc(1, sizeof(*rec));
6607 fprintf(stderr, "memory allocation failed\n");
6611 rec->cache.start = key->objectid;
6612 rec->cache.size = key->offset;
6614 rec->generation = btrfs_header_generation(leaf);
6616 rec->objectid = key->objectid;
6617 rec->type = key->type;
6618 rec->offset = key->offset;
6620 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
6621 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
6623 INIT_LIST_HEAD(&rec->list);
6628 static int process_block_group_item(struct block_group_tree *block_group_cache,
6629 struct btrfs_key *key,
6630 struct extent_buffer *eb, int slot)
6632 struct block_group_record *rec;
6635 rec = btrfs_new_block_group_record(eb, key, slot);
6636 ret = insert_block_group_record(block_group_cache, rec);
6638 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
6639 rec->objectid, rec->offset);
6646 struct device_extent_record *
6647 btrfs_new_device_extent_record(struct extent_buffer *leaf,
6648 struct btrfs_key *key, int slot)
6650 struct device_extent_record *rec;
6651 struct btrfs_dev_extent *ptr;
6653 rec = calloc(1, sizeof(*rec));
6655 fprintf(stderr, "memory allocation failed\n");
6659 rec->cache.objectid = key->objectid;
6660 rec->cache.start = key->offset;
6662 rec->generation = btrfs_header_generation(leaf);
6664 rec->objectid = key->objectid;
6665 rec->type = key->type;
6666 rec->offset = key->offset;
6668 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
6669 rec->chunk_objecteid =
6670 btrfs_dev_extent_chunk_objectid(leaf, ptr);
6672 btrfs_dev_extent_chunk_offset(leaf, ptr);
6673 rec->length = btrfs_dev_extent_length(leaf, ptr);
6674 rec->cache.size = rec->length;
6676 INIT_LIST_HEAD(&rec->chunk_list);
6677 INIT_LIST_HEAD(&rec->device_list);
6683 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
6684 struct btrfs_key *key, struct extent_buffer *eb,
6687 struct device_extent_record *rec;
6690 rec = btrfs_new_device_extent_record(eb, key, slot);
6691 ret = insert_device_extent_record(dev_extent_cache, rec);
6694 "Device extent[%llu, %llu, %llu] existed.\n",
6695 rec->objectid, rec->offset, rec->length);
6702 static int process_extent_item(struct btrfs_root *root,
6703 struct cache_tree *extent_cache,
6704 struct extent_buffer *eb, int slot)
6706 struct btrfs_extent_item *ei;
6707 struct btrfs_extent_inline_ref *iref;
6708 struct btrfs_extent_data_ref *dref;
6709 struct btrfs_shared_data_ref *sref;
6710 struct btrfs_key key;
6711 struct extent_record tmpl;
6716 u32 item_size = btrfs_item_size_nr(eb, slot);
6722 btrfs_item_key_to_cpu(eb, &key, slot);
6724 if (key.type == BTRFS_METADATA_ITEM_KEY) {
6726 num_bytes = root->nodesize;
6728 num_bytes = key.offset;
6731 if (!IS_ALIGNED(key.objectid, root->sectorsize)) {
6732 error("ignoring invalid extent, bytenr %llu is not aligned to %u",
6733 key.objectid, root->sectorsize);
6736 if (item_size < sizeof(*ei)) {
6737 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6738 struct btrfs_extent_item_v0 *ei0;
6739 BUG_ON(item_size != sizeof(*ei0));
6740 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
6741 refs = btrfs_extent_refs_v0(eb, ei0);
6745 memset(&tmpl, 0, sizeof(tmpl));
6746 tmpl.start = key.objectid;
6747 tmpl.nr = num_bytes;
6748 tmpl.extent_item_refs = refs;
6749 tmpl.metadata = metadata;
6751 tmpl.max_size = num_bytes;
6753 return add_extent_rec(extent_cache, &tmpl);
6756 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
6757 refs = btrfs_extent_refs(eb, ei);
6758 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
6762 if (metadata && num_bytes != root->nodesize) {
6763 error("ignore invalid metadata extent, length %llu does not equal to %u",
6764 num_bytes, root->nodesize);
6767 if (!metadata && !IS_ALIGNED(num_bytes, root->sectorsize)) {
6768 error("ignore invalid data extent, length %llu is not aligned to %u",
6769 num_bytes, root->sectorsize);
6773 memset(&tmpl, 0, sizeof(tmpl));
6774 tmpl.start = key.objectid;
6775 tmpl.nr = num_bytes;
6776 tmpl.extent_item_refs = refs;
6777 tmpl.metadata = metadata;
6779 tmpl.max_size = num_bytes;
6780 add_extent_rec(extent_cache, &tmpl);
6782 ptr = (unsigned long)(ei + 1);
6783 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
6784 key.type == BTRFS_EXTENT_ITEM_KEY)
6785 ptr += sizeof(struct btrfs_tree_block_info);
6787 end = (unsigned long)ei + item_size;
6789 iref = (struct btrfs_extent_inline_ref *)ptr;
6790 type = btrfs_extent_inline_ref_type(eb, iref);
6791 offset = btrfs_extent_inline_ref_offset(eb, iref);
6793 case BTRFS_TREE_BLOCK_REF_KEY:
6794 ret = add_tree_backref(extent_cache, key.objectid,
6797 error("add_tree_backref failed: %s",
6800 case BTRFS_SHARED_BLOCK_REF_KEY:
6801 ret = add_tree_backref(extent_cache, key.objectid,
6804 error("add_tree_backref failed: %s",
6807 case BTRFS_EXTENT_DATA_REF_KEY:
6808 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
6809 add_data_backref(extent_cache, key.objectid, 0,
6810 btrfs_extent_data_ref_root(eb, dref),
6811 btrfs_extent_data_ref_objectid(eb,
6813 btrfs_extent_data_ref_offset(eb, dref),
6814 btrfs_extent_data_ref_count(eb, dref),
6817 case BTRFS_SHARED_DATA_REF_KEY:
6818 sref = (struct btrfs_shared_data_ref *)(iref + 1);
6819 add_data_backref(extent_cache, key.objectid, offset,
6821 btrfs_shared_data_ref_count(eb, sref),
6825 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
6826 key.objectid, key.type, num_bytes);
6829 ptr += btrfs_extent_inline_ref_size(type);
6836 static int check_cache_range(struct btrfs_root *root,
6837 struct btrfs_block_group_cache *cache,
6838 u64 offset, u64 bytes)
6840 struct btrfs_free_space *entry;
6846 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
6847 bytenr = btrfs_sb_offset(i);
6848 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
6849 cache->key.objectid, bytenr, 0,
6850 &logical, &nr, &stripe_len);
6855 if (logical[nr] + stripe_len <= offset)
6857 if (offset + bytes <= logical[nr])
6859 if (logical[nr] == offset) {
6860 if (stripe_len >= bytes) {
6864 bytes -= stripe_len;
6865 offset += stripe_len;
6866 } else if (logical[nr] < offset) {
6867 if (logical[nr] + stripe_len >=
6872 bytes = (offset + bytes) -
6873 (logical[nr] + stripe_len);
6874 offset = logical[nr] + stripe_len;
6877 * Could be tricky, the super may land in the
6878 * middle of the area we're checking. First
6879 * check the easiest case, it's at the end.
6881 if (logical[nr] + stripe_len >=
6883 bytes = logical[nr] - offset;
6887 /* Check the left side */
6888 ret = check_cache_range(root, cache,
6890 logical[nr] - offset);
6896 /* Now we continue with the right side */
6897 bytes = (offset + bytes) -
6898 (logical[nr] + stripe_len);
6899 offset = logical[nr] + stripe_len;
6906 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
6908 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
6909 offset, offset+bytes);
6913 if (entry->offset != offset) {
6914 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
6919 if (entry->bytes != bytes) {
6920 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
6921 bytes, entry->bytes, offset);
6925 unlink_free_space(cache->free_space_ctl, entry);
6930 static int verify_space_cache(struct btrfs_root *root,
6931 struct btrfs_block_group_cache *cache)
6933 struct btrfs_path path;
6934 struct extent_buffer *leaf;
6935 struct btrfs_key key;
6939 root = root->fs_info->extent_root;
6941 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
6943 btrfs_init_path(&path);
6944 key.objectid = last;
6946 key.type = BTRFS_EXTENT_ITEM_KEY;
6947 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6952 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
6953 ret = btrfs_next_leaf(root, &path);
6961 leaf = path.nodes[0];
6962 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
6963 if (key.objectid >= cache->key.offset + cache->key.objectid)
6965 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
6966 key.type != BTRFS_METADATA_ITEM_KEY) {
6971 if (last == key.objectid) {
6972 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6973 last = key.objectid + key.offset;
6975 last = key.objectid + root->nodesize;
6980 ret = check_cache_range(root, cache, last,
6981 key.objectid - last);
6984 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6985 last = key.objectid + key.offset;
6987 last = key.objectid + root->nodesize;
6991 if (last < cache->key.objectid + cache->key.offset)
6992 ret = check_cache_range(root, cache, last,
6993 cache->key.objectid +
6994 cache->key.offset - last);
6997 btrfs_release_path(&path);
7000 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
7001 fprintf(stderr, "There are still entries left in the space "
7009 static int check_space_cache(struct btrfs_root *root)
7011 struct btrfs_block_group_cache *cache;
7012 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
7016 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
7017 btrfs_super_generation(root->fs_info->super_copy) !=
7018 btrfs_super_cache_generation(root->fs_info->super_copy)) {
7019 printf("cache and super generation don't match, space cache "
7020 "will be invalidated\n");
7024 if (ctx.progress_enabled) {
7025 ctx.tp = TASK_FREE_SPACE;
7026 task_start(ctx.info);
7030 cache = btrfs_lookup_first_block_group(root->fs_info, start);
7034 start = cache->key.objectid + cache->key.offset;
7035 if (!cache->free_space_ctl) {
7036 if (btrfs_init_free_space_ctl(cache,
7037 root->sectorsize)) {
7042 btrfs_remove_free_space_cache(cache);
7045 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) {
7046 ret = exclude_super_stripes(root, cache);
7048 fprintf(stderr, "could not exclude super stripes: %s\n",
7053 ret = load_free_space_tree(root->fs_info, cache);
7054 free_excluded_extents(root, cache);
7056 fprintf(stderr, "could not load free space tree: %s\n",
7063 ret = load_free_space_cache(root->fs_info, cache);
7068 ret = verify_space_cache(root, cache);
7070 fprintf(stderr, "cache appears valid but isn't %Lu\n",
7071 cache->key.objectid);
7076 task_stop(ctx.info);
7078 return error ? -EINVAL : 0;
7081 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
7082 u64 num_bytes, unsigned long leaf_offset,
7083 struct extent_buffer *eb) {
7086 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7088 unsigned long csum_offset;
7092 u64 data_checked = 0;
7098 if (num_bytes % root->sectorsize)
7101 data = malloc(num_bytes);
7105 while (offset < num_bytes) {
7108 read_len = num_bytes - offset;
7109 /* read as much space once a time */
7110 ret = read_extent_data(root, data + offset,
7111 bytenr + offset, &read_len, mirror);
7115 /* verify every 4k data's checksum */
7116 while (data_checked < read_len) {
7118 tmp = offset + data_checked;
7120 csum = btrfs_csum_data((char *)data + tmp,
7121 csum, root->sectorsize);
7122 btrfs_csum_final(csum, (u8 *)&csum);
7124 csum_offset = leaf_offset +
7125 tmp / root->sectorsize * csum_size;
7126 read_extent_buffer(eb, (char *)&csum_expected,
7127 csum_offset, csum_size);
7128 /* try another mirror */
7129 if (csum != csum_expected) {
7130 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
7131 mirror, bytenr + tmp,
7132 csum, csum_expected);
7133 num_copies = btrfs_num_copies(
7134 &root->fs_info->mapping_tree,
7136 if (mirror < num_copies - 1) {
7141 data_checked += root->sectorsize;
7150 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
7153 struct btrfs_path path;
7154 struct extent_buffer *leaf;
7155 struct btrfs_key key;
7158 btrfs_init_path(&path);
7159 key.objectid = bytenr;
7160 key.type = BTRFS_EXTENT_ITEM_KEY;
7161 key.offset = (u64)-1;
7164 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, &path,
7167 fprintf(stderr, "Error looking up extent record %d\n", ret);
7168 btrfs_release_path(&path);
7171 if (path.slots[0] > 0) {
7174 ret = btrfs_prev_leaf(root, &path);
7177 } else if (ret > 0) {
7184 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7187 * Block group items come before extent items if they have the same
7188 * bytenr, so walk back one more just in case. Dear future traveller,
7189 * first congrats on mastering time travel. Now if it's not too much
7190 * trouble could you go back to 2006 and tell Chris to make the
7191 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
7192 * EXTENT_ITEM_KEY please?
7194 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
7195 if (path.slots[0] > 0) {
7198 ret = btrfs_prev_leaf(root, &path);
7201 } else if (ret > 0) {
7206 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7210 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7211 ret = btrfs_next_leaf(root, &path);
7213 fprintf(stderr, "Error going to next leaf "
7215 btrfs_release_path(&path);
7221 leaf = path.nodes[0];
7222 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7223 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7227 if (key.objectid + key.offset < bytenr) {
7231 if (key.objectid > bytenr + num_bytes)
7234 if (key.objectid == bytenr) {
7235 if (key.offset >= num_bytes) {
7239 num_bytes -= key.offset;
7240 bytenr += key.offset;
7241 } else if (key.objectid < bytenr) {
7242 if (key.objectid + key.offset >= bytenr + num_bytes) {
7246 num_bytes = (bytenr + num_bytes) -
7247 (key.objectid + key.offset);
7248 bytenr = key.objectid + key.offset;
7250 if (key.objectid + key.offset < bytenr + num_bytes) {
7251 u64 new_start = key.objectid + key.offset;
7252 u64 new_bytes = bytenr + num_bytes - new_start;
7255 * Weird case, the extent is in the middle of
7256 * our range, we'll have to search one side
7257 * and then the other. Not sure if this happens
7258 * in real life, but no harm in coding it up
7259 * anyway just in case.
7261 btrfs_release_path(&path);
7262 ret = check_extent_exists(root, new_start,
7265 fprintf(stderr, "Right section didn't "
7269 num_bytes = key.objectid - bytenr;
7272 num_bytes = key.objectid - bytenr;
7279 if (num_bytes && !ret) {
7280 fprintf(stderr, "There are no extents for csum range "
7281 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
7285 btrfs_release_path(&path);
7289 static int check_csums(struct btrfs_root *root)
7291 struct btrfs_path path;
7292 struct extent_buffer *leaf;
7293 struct btrfs_key key;
7294 u64 offset = 0, num_bytes = 0;
7295 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7299 unsigned long leaf_offset;
7301 root = root->fs_info->csum_root;
7302 if (!extent_buffer_uptodate(root->node)) {
7303 fprintf(stderr, "No valid csum tree found\n");
7307 btrfs_init_path(&path);
7308 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
7309 key.type = BTRFS_EXTENT_CSUM_KEY;
7311 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
7313 fprintf(stderr, "Error searching csum tree %d\n", ret);
7314 btrfs_release_path(&path);
7318 if (ret > 0 && path.slots[0])
7323 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7324 ret = btrfs_next_leaf(root, &path);
7326 fprintf(stderr, "Error going to next leaf "
7333 leaf = path.nodes[0];
7335 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7336 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
7341 data_len = (btrfs_item_size_nr(leaf, path.slots[0]) /
7342 csum_size) * root->sectorsize;
7343 if (!check_data_csum)
7344 goto skip_csum_check;
7345 leaf_offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
7346 ret = check_extent_csums(root, key.offset, data_len,
7352 offset = key.offset;
7353 } else if (key.offset != offset + num_bytes) {
7354 ret = check_extent_exists(root, offset, num_bytes);
7356 fprintf(stderr, "Csum exists for %Lu-%Lu but "
7357 "there is no extent record\n",
7358 offset, offset+num_bytes);
7361 offset = key.offset;
7364 num_bytes += data_len;
7368 btrfs_release_path(&path);
7372 static int is_dropped_key(struct btrfs_key *key,
7373 struct btrfs_key *drop_key) {
7374 if (key->objectid < drop_key->objectid)
7376 else if (key->objectid == drop_key->objectid) {
7377 if (key->type < drop_key->type)
7379 else if (key->type == drop_key->type) {
7380 if (key->offset < drop_key->offset)
7388 * Here are the rules for FULL_BACKREF.
7390 * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
7391 * 2) If btrfs_header_owner(buf) no longer points to buf then we have
7393 * 3) We cowed the block walking down a reloc tree. This is impossible to tell
7394 * if it happened after the relocation occurred since we'll have dropped the
7395 * reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
7396 * have no real way to know for sure.
7398 * We process the blocks one root at a time, and we start from the lowest root
7399 * objectid and go to the highest. So we can just lookup the owner backref for
7400 * the record and if we don't find it then we know it doesn't exist and we have
7403 * FIXME: if we ever start reclaiming root objectid's then we need to fix this
7404 * assumption and simply indicate that we _think_ that the FULL BACKREF needs to
7405 * be set or not and then we can check later once we've gathered all the refs.
7407 static int calc_extent_flag(struct btrfs_root *root,
7408 struct cache_tree *extent_cache,
7409 struct extent_buffer *buf,
7410 struct root_item_record *ri,
7413 struct extent_record *rec;
7414 struct cache_extent *cache;
7415 struct tree_backref *tback;
7418 cache = lookup_cache_extent(extent_cache, buf->start, 1);
7419 /* we have added this extent before */
7423 rec = container_of(cache, struct extent_record, cache);
7426 * Except file/reloc tree, we can not have
7429 if (ri->objectid < BTRFS_FIRST_FREE_OBJECTID)
7434 if (buf->start == ri->bytenr)
7437 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7440 owner = btrfs_header_owner(buf);
7441 if (owner == ri->objectid)
7444 tback = find_tree_backref(rec, 0, owner);
7449 if (rec->flag_block_full_backref != FLAG_UNSET &&
7450 rec->flag_block_full_backref != 0)
7451 rec->bad_full_backref = 1;
7454 *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7455 if (rec->flag_block_full_backref != FLAG_UNSET &&
7456 rec->flag_block_full_backref != 1)
7457 rec->bad_full_backref = 1;
7461 static void report_mismatch_key_root(u8 key_type, u64 rootid)
7463 fprintf(stderr, "Invalid key type(");
7464 print_key_type(stderr, 0, key_type);
7465 fprintf(stderr, ") found in root(");
7466 print_objectid(stderr, rootid, 0);
7467 fprintf(stderr, ")\n");
7471 * Check if the key is valid with its extent buffer.
7473 * This is a early check in case invalid key exists in a extent buffer
7474 * This is not comprehensive yet, but should prevent wrong key/item passed
7477 static int check_type_with_root(u64 rootid, u8 key_type)
7480 /* Only valid in chunk tree */
7481 case BTRFS_DEV_ITEM_KEY:
7482 case BTRFS_CHUNK_ITEM_KEY:
7483 if (rootid != BTRFS_CHUNK_TREE_OBJECTID)
7486 /* valid in csum and log tree */
7487 case BTRFS_CSUM_TREE_OBJECTID:
7488 if (!(rootid == BTRFS_TREE_LOG_OBJECTID ||
7492 case BTRFS_EXTENT_ITEM_KEY:
7493 case BTRFS_METADATA_ITEM_KEY:
7494 case BTRFS_BLOCK_GROUP_ITEM_KEY:
7495 if (rootid != BTRFS_EXTENT_TREE_OBJECTID)
7498 case BTRFS_ROOT_ITEM_KEY:
7499 if (rootid != BTRFS_ROOT_TREE_OBJECTID)
7502 case BTRFS_DEV_EXTENT_KEY:
7503 if (rootid != BTRFS_DEV_TREE_OBJECTID)
7509 report_mismatch_key_root(key_type, rootid);
7513 static int run_next_block(struct btrfs_root *root,
7514 struct block_info *bits,
7517 struct cache_tree *pending,
7518 struct cache_tree *seen,
7519 struct cache_tree *reada,
7520 struct cache_tree *nodes,
7521 struct cache_tree *extent_cache,
7522 struct cache_tree *chunk_cache,
7523 struct rb_root *dev_cache,
7524 struct block_group_tree *block_group_cache,
7525 struct device_extent_tree *dev_extent_cache,
7526 struct root_item_record *ri)
7528 struct extent_buffer *buf;
7529 struct extent_record *rec = NULL;
7540 struct btrfs_key key;
7541 struct cache_extent *cache;
7544 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
7545 bits_nr, &reada_bits);
7550 for(i = 0; i < nritems; i++) {
7551 ret = add_cache_extent(reada, bits[i].start,
7556 /* fixme, get the parent transid */
7557 readahead_tree_block(root, bits[i].start,
7561 *last = bits[0].start;
7562 bytenr = bits[0].start;
7563 size = bits[0].size;
7565 cache = lookup_cache_extent(pending, bytenr, size);
7567 remove_cache_extent(pending, cache);
7570 cache = lookup_cache_extent(reada, bytenr, size);
7572 remove_cache_extent(reada, cache);
7575 cache = lookup_cache_extent(nodes, bytenr, size);
7577 remove_cache_extent(nodes, cache);
7580 cache = lookup_cache_extent(extent_cache, bytenr, size);
7582 rec = container_of(cache, struct extent_record, cache);
7583 gen = rec->parent_generation;
7586 /* fixme, get the real parent transid */
7587 buf = read_tree_block(root, bytenr, size, gen);
7588 if (!extent_buffer_uptodate(buf)) {
7589 record_bad_block_io(root->fs_info,
7590 extent_cache, bytenr, size);
7594 nritems = btrfs_header_nritems(buf);
7597 if (!init_extent_tree) {
7598 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
7599 btrfs_header_level(buf), 1, NULL,
7602 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7604 fprintf(stderr, "Couldn't calc extent flags\n");
7605 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7610 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7612 fprintf(stderr, "Couldn't calc extent flags\n");
7613 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7617 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7619 ri->objectid != BTRFS_TREE_RELOC_OBJECTID &&
7620 ri->objectid == btrfs_header_owner(buf)) {
7622 * Ok we got to this block from it's original owner and
7623 * we have FULL_BACKREF set. Relocation can leave
7624 * converted blocks over so this is altogether possible,
7625 * however it's not possible if the generation > the
7626 * last snapshot, so check for this case.
7628 if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC) &&
7629 btrfs_header_generation(buf) > ri->last_snapshot) {
7630 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
7631 rec->bad_full_backref = 1;
7636 (ri->objectid == BTRFS_TREE_RELOC_OBJECTID ||
7637 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
7638 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7639 rec->bad_full_backref = 1;
7643 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7644 rec->flag_block_full_backref = 1;
7648 rec->flag_block_full_backref = 0;
7650 owner = btrfs_header_owner(buf);
7653 ret = check_block(root, extent_cache, buf, flags);
7657 if (btrfs_is_leaf(buf)) {
7658 btree_space_waste += btrfs_leaf_free_space(root, buf);
7659 for (i = 0; i < nritems; i++) {
7660 struct btrfs_file_extent_item *fi;
7661 btrfs_item_key_to_cpu(buf, &key, i);
7663 * Check key type against the leaf owner.
7664 * Could filter quite a lot of early error if
7667 if (check_type_with_root(btrfs_header_owner(buf),
7669 fprintf(stderr, "ignoring invalid key\n");
7672 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
7673 process_extent_item(root, extent_cache, buf,
7677 if (key.type == BTRFS_METADATA_ITEM_KEY) {
7678 process_extent_item(root, extent_cache, buf,
7682 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
7684 btrfs_item_size_nr(buf, i);
7687 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
7688 process_chunk_item(chunk_cache, &key, buf, i);
7691 if (key.type == BTRFS_DEV_ITEM_KEY) {
7692 process_device_item(dev_cache, &key, buf, i);
7695 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7696 process_block_group_item(block_group_cache,
7700 if (key.type == BTRFS_DEV_EXTENT_KEY) {
7701 process_device_extent_item(dev_extent_cache,
7706 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
7707 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7708 process_extent_ref_v0(extent_cache, buf, i);
7715 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
7716 ret = add_tree_backref(extent_cache,
7717 key.objectid, 0, key.offset, 0);
7719 error("add_tree_backref failed: %s",
7723 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
7724 ret = add_tree_backref(extent_cache,
7725 key.objectid, key.offset, 0, 0);
7727 error("add_tree_backref failed: %s",
7731 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
7732 struct btrfs_extent_data_ref *ref;
7733 ref = btrfs_item_ptr(buf, i,
7734 struct btrfs_extent_data_ref);
7735 add_data_backref(extent_cache,
7737 btrfs_extent_data_ref_root(buf, ref),
7738 btrfs_extent_data_ref_objectid(buf,
7740 btrfs_extent_data_ref_offset(buf, ref),
7741 btrfs_extent_data_ref_count(buf, ref),
7742 0, root->sectorsize);
7745 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
7746 struct btrfs_shared_data_ref *ref;
7747 ref = btrfs_item_ptr(buf, i,
7748 struct btrfs_shared_data_ref);
7749 add_data_backref(extent_cache,
7750 key.objectid, key.offset, 0, 0, 0,
7751 btrfs_shared_data_ref_count(buf, ref),
7752 0, root->sectorsize);
7755 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
7756 struct bad_item *bad;
7758 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
7762 bad = malloc(sizeof(struct bad_item));
7765 INIT_LIST_HEAD(&bad->list);
7766 memcpy(&bad->key, &key,
7767 sizeof(struct btrfs_key));
7768 bad->root_id = owner;
7769 list_add_tail(&bad->list, &delete_items);
7772 if (key.type != BTRFS_EXTENT_DATA_KEY)
7774 fi = btrfs_item_ptr(buf, i,
7775 struct btrfs_file_extent_item);
7776 if (btrfs_file_extent_type(buf, fi) ==
7777 BTRFS_FILE_EXTENT_INLINE)
7779 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
7782 data_bytes_allocated +=
7783 btrfs_file_extent_disk_num_bytes(buf, fi);
7784 if (data_bytes_allocated < root->sectorsize) {
7787 data_bytes_referenced +=
7788 btrfs_file_extent_num_bytes(buf, fi);
7789 add_data_backref(extent_cache,
7790 btrfs_file_extent_disk_bytenr(buf, fi),
7791 parent, owner, key.objectid, key.offset -
7792 btrfs_file_extent_offset(buf, fi), 1, 1,
7793 btrfs_file_extent_disk_num_bytes(buf, fi));
7797 struct btrfs_key first_key;
7799 first_key.objectid = 0;
7802 btrfs_item_key_to_cpu(buf, &first_key, 0);
7803 level = btrfs_header_level(buf);
7804 for (i = 0; i < nritems; i++) {
7805 struct extent_record tmpl;
7807 ptr = btrfs_node_blockptr(buf, i);
7808 size = root->nodesize;
7809 btrfs_node_key_to_cpu(buf, &key, i);
7811 if ((level == ri->drop_level)
7812 && is_dropped_key(&key, &ri->drop_key)) {
7817 memset(&tmpl, 0, sizeof(tmpl));
7818 btrfs_cpu_key_to_disk(&tmpl.parent_key, &key);
7819 tmpl.parent_generation = btrfs_node_ptr_generation(buf, i);
7824 tmpl.max_size = size;
7825 ret = add_extent_rec(extent_cache, &tmpl);
7829 ret = add_tree_backref(extent_cache, ptr, parent,
7832 error("add_tree_backref failed: %s",
7838 add_pending(nodes, seen, ptr, size);
7840 add_pending(pending, seen, ptr, size);
7843 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
7844 nritems) * sizeof(struct btrfs_key_ptr);
7846 total_btree_bytes += buf->len;
7847 if (fs_root_objectid(btrfs_header_owner(buf)))
7848 total_fs_tree_bytes += buf->len;
7849 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
7850 total_extent_tree_bytes += buf->len;
7851 if (!found_old_backref &&
7852 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
7853 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
7854 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7855 found_old_backref = 1;
7857 free_extent_buffer(buf);
7861 static int add_root_to_pending(struct extent_buffer *buf,
7862 struct cache_tree *extent_cache,
7863 struct cache_tree *pending,
7864 struct cache_tree *seen,
7865 struct cache_tree *nodes,
7868 struct extent_record tmpl;
7871 if (btrfs_header_level(buf) > 0)
7872 add_pending(nodes, seen, buf->start, buf->len);
7874 add_pending(pending, seen, buf->start, buf->len);
7876 memset(&tmpl, 0, sizeof(tmpl));
7877 tmpl.start = buf->start;
7882 tmpl.max_size = buf->len;
7883 add_extent_rec(extent_cache, &tmpl);
7885 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
7886 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
7887 ret = add_tree_backref(extent_cache, buf->start, buf->start,
7890 ret = add_tree_backref(extent_cache, buf->start, 0, objectid,
7895 /* as we fix the tree, we might be deleting blocks that
7896 * we're tracking for repair. This hook makes sure we
7897 * remove any backrefs for blocks as we are fixing them.
7899 static int free_extent_hook(struct btrfs_trans_handle *trans,
7900 struct btrfs_root *root,
7901 u64 bytenr, u64 num_bytes, u64 parent,
7902 u64 root_objectid, u64 owner, u64 offset,
7905 struct extent_record *rec;
7906 struct cache_extent *cache;
7908 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
7910 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
7911 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
7915 rec = container_of(cache, struct extent_record, cache);
7917 struct data_backref *back;
7918 back = find_data_backref(rec, parent, root_objectid, owner,
7919 offset, 1, bytenr, num_bytes);
7922 if (back->node.found_ref) {
7923 back->found_ref -= refs_to_drop;
7925 rec->refs -= refs_to_drop;
7927 if (back->node.found_extent_tree) {
7928 back->num_refs -= refs_to_drop;
7929 if (rec->extent_item_refs)
7930 rec->extent_item_refs -= refs_to_drop;
7932 if (back->found_ref == 0)
7933 back->node.found_ref = 0;
7934 if (back->num_refs == 0)
7935 back->node.found_extent_tree = 0;
7937 if (!back->node.found_extent_tree && back->node.found_ref) {
7938 list_del(&back->node.list);
7942 struct tree_backref *back;
7943 back = find_tree_backref(rec, parent, root_objectid);
7946 if (back->node.found_ref) {
7949 back->node.found_ref = 0;
7951 if (back->node.found_extent_tree) {
7952 if (rec->extent_item_refs)
7953 rec->extent_item_refs--;
7954 back->node.found_extent_tree = 0;
7956 if (!back->node.found_extent_tree && back->node.found_ref) {
7957 list_del(&back->node.list);
7961 maybe_free_extent_rec(extent_cache, rec);
7966 static int delete_extent_records(struct btrfs_trans_handle *trans,
7967 struct btrfs_root *root,
7968 struct btrfs_path *path,
7971 struct btrfs_key key;
7972 struct btrfs_key found_key;
7973 struct extent_buffer *leaf;
7978 key.objectid = bytenr;
7980 key.offset = (u64)-1;
7983 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
7990 if (path->slots[0] == 0)
7996 leaf = path->nodes[0];
7997 slot = path->slots[0];
7999 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8000 if (found_key.objectid != bytenr)
8003 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
8004 found_key.type != BTRFS_METADATA_ITEM_KEY &&
8005 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
8006 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
8007 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
8008 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
8009 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
8010 btrfs_release_path(path);
8011 if (found_key.type == 0) {
8012 if (found_key.offset == 0)
8014 key.offset = found_key.offset - 1;
8015 key.type = found_key.type;
8017 key.type = found_key.type - 1;
8018 key.offset = (u64)-1;
8022 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
8023 found_key.objectid, found_key.type, found_key.offset);
8025 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
8028 btrfs_release_path(path);
8030 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
8031 found_key.type == BTRFS_METADATA_ITEM_KEY) {
8032 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
8033 found_key.offset : root->nodesize;
8035 ret = btrfs_update_block_group(trans, root, bytenr,
8042 btrfs_release_path(path);
8047 * for a single backref, this will allocate a new extent
8048 * and add the backref to it.
8050 static int record_extent(struct btrfs_trans_handle *trans,
8051 struct btrfs_fs_info *info,
8052 struct btrfs_path *path,
8053 struct extent_record *rec,
8054 struct extent_backref *back,
8055 int allocated, u64 flags)
8058 struct btrfs_root *extent_root = info->extent_root;
8059 struct extent_buffer *leaf;
8060 struct btrfs_key ins_key;
8061 struct btrfs_extent_item *ei;
8062 struct data_backref *dback;
8063 struct btrfs_tree_block_info *bi;
8066 rec->max_size = max_t(u64, rec->max_size,
8067 info->extent_root->nodesize);
8070 u32 item_size = sizeof(*ei);
8073 item_size += sizeof(*bi);
8075 ins_key.objectid = rec->start;
8076 ins_key.offset = rec->max_size;
8077 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
8079 ret = btrfs_insert_empty_item(trans, extent_root, path,
8080 &ins_key, item_size);
8084 leaf = path->nodes[0];
8085 ei = btrfs_item_ptr(leaf, path->slots[0],
8086 struct btrfs_extent_item);
8088 btrfs_set_extent_refs(leaf, ei, 0);
8089 btrfs_set_extent_generation(leaf, ei, rec->generation);
8091 if (back->is_data) {
8092 btrfs_set_extent_flags(leaf, ei,
8093 BTRFS_EXTENT_FLAG_DATA);
8095 struct btrfs_disk_key copy_key;;
8097 bi = (struct btrfs_tree_block_info *)(ei + 1);
8098 memset_extent_buffer(leaf, 0, (unsigned long)bi,
8101 btrfs_set_disk_key_objectid(©_key,
8102 rec->info_objectid);
8103 btrfs_set_disk_key_type(©_key, 0);
8104 btrfs_set_disk_key_offset(©_key, 0);
8106 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
8107 btrfs_set_tree_block_key(leaf, bi, ©_key);
8109 btrfs_set_extent_flags(leaf, ei,
8110 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
8113 btrfs_mark_buffer_dirty(leaf);
8114 ret = btrfs_update_block_group(trans, extent_root, rec->start,
8115 rec->max_size, 1, 0);
8118 btrfs_release_path(path);
8121 if (back->is_data) {
8125 dback = to_data_backref(back);
8126 if (back->full_backref)
8127 parent = dback->parent;
8131 for (i = 0; i < dback->found_ref; i++) {
8132 /* if parent != 0, we're doing a full backref
8133 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
8134 * just makes the backref allocator create a data
8137 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8138 rec->start, rec->max_size,
8142 BTRFS_FIRST_FREE_OBJECTID :
8148 fprintf(stderr, "adding new data backref"
8149 " on %llu %s %llu owner %llu"
8150 " offset %llu found %d\n",
8151 (unsigned long long)rec->start,
8152 back->full_backref ?
8154 back->full_backref ?
8155 (unsigned long long)parent :
8156 (unsigned long long)dback->root,
8157 (unsigned long long)dback->owner,
8158 (unsigned long long)dback->offset,
8162 struct tree_backref *tback;
8164 tback = to_tree_backref(back);
8165 if (back->full_backref)
8166 parent = tback->parent;
8170 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8171 rec->start, rec->max_size,
8172 parent, tback->root, 0, 0);
8173 fprintf(stderr, "adding new tree backref on "
8174 "start %llu len %llu parent %llu root %llu\n",
8175 rec->start, rec->max_size, parent, tback->root);
8178 btrfs_release_path(path);
8182 static struct extent_entry *find_entry(struct list_head *entries,
8183 u64 bytenr, u64 bytes)
8185 struct extent_entry *entry = NULL;
8187 list_for_each_entry(entry, entries, list) {
8188 if (entry->bytenr == bytenr && entry->bytes == bytes)
8195 static struct extent_entry *find_most_right_entry(struct list_head *entries)
8197 struct extent_entry *entry, *best = NULL, *prev = NULL;
8199 list_for_each_entry(entry, entries, list) {
8201 * If there are as many broken entries as entries then we know
8202 * not to trust this particular entry.
8204 if (entry->broken == entry->count)
8208 * Special case, when there are only two entries and 'best' is
8218 * If our current entry == best then we can't be sure our best
8219 * is really the best, so we need to keep searching.
8221 if (best && best->count == entry->count) {
8227 /* Prev == entry, not good enough, have to keep searching */
8228 if (!prev->broken && prev->count == entry->count)
8232 best = (prev->count > entry->count) ? prev : entry;
8233 else if (best->count < entry->count)
8241 static int repair_ref(struct btrfs_fs_info *info, struct btrfs_path *path,
8242 struct data_backref *dback, struct extent_entry *entry)
8244 struct btrfs_trans_handle *trans;
8245 struct btrfs_root *root;
8246 struct btrfs_file_extent_item *fi;
8247 struct extent_buffer *leaf;
8248 struct btrfs_key key;
8252 key.objectid = dback->root;
8253 key.type = BTRFS_ROOT_ITEM_KEY;
8254 key.offset = (u64)-1;
8255 root = btrfs_read_fs_root(info, &key);
8257 fprintf(stderr, "Couldn't find root for our ref\n");
8262 * The backref points to the original offset of the extent if it was
8263 * split, so we need to search down to the offset we have and then walk
8264 * forward until we find the backref we're looking for.
8266 key.objectid = dback->owner;
8267 key.type = BTRFS_EXTENT_DATA_KEY;
8268 key.offset = dback->offset;
8269 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8271 fprintf(stderr, "Error looking up ref %d\n", ret);
8276 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
8277 ret = btrfs_next_leaf(root, path);
8279 fprintf(stderr, "Couldn't find our ref, next\n");
8283 leaf = path->nodes[0];
8284 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8285 if (key.objectid != dback->owner ||
8286 key.type != BTRFS_EXTENT_DATA_KEY) {
8287 fprintf(stderr, "Couldn't find our ref, search\n");
8290 fi = btrfs_item_ptr(leaf, path->slots[0],
8291 struct btrfs_file_extent_item);
8292 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
8293 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
8295 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
8300 btrfs_release_path(path);
8302 trans = btrfs_start_transaction(root, 1);
8304 return PTR_ERR(trans);
8307 * Ok we have the key of the file extent we want to fix, now we can cow
8308 * down to the thing and fix it.
8310 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
8312 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
8313 key.objectid, key.type, key.offset, ret);
8317 fprintf(stderr, "Well that's odd, we just found this key "
8318 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
8323 leaf = path->nodes[0];
8324 fi = btrfs_item_ptr(leaf, path->slots[0],
8325 struct btrfs_file_extent_item);
8327 if (btrfs_file_extent_compression(leaf, fi) &&
8328 dback->disk_bytenr != entry->bytenr) {
8329 fprintf(stderr, "Ref doesn't match the record start and is "
8330 "compressed, please take a btrfs-image of this file "
8331 "system and send it to a btrfs developer so they can "
8332 "complete this functionality for bytenr %Lu\n",
8333 dback->disk_bytenr);
8338 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
8339 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8340 } else if (dback->disk_bytenr > entry->bytenr) {
8341 u64 off_diff, offset;
8343 off_diff = dback->disk_bytenr - entry->bytenr;
8344 offset = btrfs_file_extent_offset(leaf, fi);
8345 if (dback->disk_bytenr + offset +
8346 btrfs_file_extent_num_bytes(leaf, fi) >
8347 entry->bytenr + entry->bytes) {
8348 fprintf(stderr, "Ref is past the entry end, please "
8349 "take a btrfs-image of this file system and "
8350 "send it to a btrfs developer, ref %Lu\n",
8351 dback->disk_bytenr);
8356 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8357 btrfs_set_file_extent_offset(leaf, fi, offset);
8358 } else if (dback->disk_bytenr < entry->bytenr) {
8361 offset = btrfs_file_extent_offset(leaf, fi);
8362 if (dback->disk_bytenr + offset < entry->bytenr) {
8363 fprintf(stderr, "Ref is before the entry start, please"
8364 " take a btrfs-image of this file system and "
8365 "send it to a btrfs developer, ref %Lu\n",
8366 dback->disk_bytenr);
8371 offset += dback->disk_bytenr;
8372 offset -= entry->bytenr;
8373 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8374 btrfs_set_file_extent_offset(leaf, fi, offset);
8377 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
8380 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
8381 * only do this if we aren't using compression, otherwise it's a
8384 if (!btrfs_file_extent_compression(leaf, fi))
8385 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
8387 printf("ram bytes may be wrong?\n");
8388 btrfs_mark_buffer_dirty(leaf);
8390 err = btrfs_commit_transaction(trans, root);
8391 btrfs_release_path(path);
8392 return ret ? ret : err;
8395 static int verify_backrefs(struct btrfs_fs_info *info, struct btrfs_path *path,
8396 struct extent_record *rec)
8398 struct extent_backref *back;
8399 struct data_backref *dback;
8400 struct extent_entry *entry, *best = NULL;
8403 int broken_entries = 0;
8408 * Metadata is easy and the backrefs should always agree on bytenr and
8409 * size, if not we've got bigger issues.
8414 list_for_each_entry(back, &rec->backrefs, list) {
8415 if (back->full_backref || !back->is_data)
8418 dback = to_data_backref(back);
8421 * We only pay attention to backrefs that we found a real
8424 if (dback->found_ref == 0)
8428 * For now we only catch when the bytes don't match, not the
8429 * bytenr. We can easily do this at the same time, but I want
8430 * to have a fs image to test on before we just add repair
8431 * functionality willy-nilly so we know we won't screw up the
8435 entry = find_entry(&entries, dback->disk_bytenr,
8438 entry = malloc(sizeof(struct extent_entry));
8443 memset(entry, 0, sizeof(*entry));
8444 entry->bytenr = dback->disk_bytenr;
8445 entry->bytes = dback->bytes;
8446 list_add_tail(&entry->list, &entries);
8451 * If we only have on entry we may think the entries agree when
8452 * in reality they don't so we have to do some extra checking.
8454 if (dback->disk_bytenr != rec->start ||
8455 dback->bytes != rec->nr || back->broken)
8466 /* Yay all the backrefs agree, carry on good sir */
8467 if (nr_entries <= 1 && !mismatch)
8470 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
8471 "%Lu\n", rec->start);
8474 * First we want to see if the backrefs can agree amongst themselves who
8475 * is right, so figure out which one of the entries has the highest
8478 best = find_most_right_entry(&entries);
8481 * Ok so we may have an even split between what the backrefs think, so
8482 * this is where we use the extent ref to see what it thinks.
8485 entry = find_entry(&entries, rec->start, rec->nr);
8486 if (!entry && (!broken_entries || !rec->found_rec)) {
8487 fprintf(stderr, "Backrefs don't agree with each other "
8488 "and extent record doesn't agree with anybody,"
8489 " so we can't fix bytenr %Lu bytes %Lu\n",
8490 rec->start, rec->nr);
8493 } else if (!entry) {
8495 * Ok our backrefs were broken, we'll assume this is the
8496 * correct value and add an entry for this range.
8498 entry = malloc(sizeof(struct extent_entry));
8503 memset(entry, 0, sizeof(*entry));
8504 entry->bytenr = rec->start;
8505 entry->bytes = rec->nr;
8506 list_add_tail(&entry->list, &entries);
8510 best = find_most_right_entry(&entries);
8512 fprintf(stderr, "Backrefs and extent record evenly "
8513 "split on who is right, this is going to "
8514 "require user input to fix bytenr %Lu bytes "
8515 "%Lu\n", rec->start, rec->nr);
8522 * I don't think this can happen currently as we'll abort() if we catch
8523 * this case higher up, but in case somebody removes that we still can't
8524 * deal with it properly here yet, so just bail out of that's the case.
8526 if (best->bytenr != rec->start) {
8527 fprintf(stderr, "Extent start and backref starts don't match, "
8528 "please use btrfs-image on this file system and send "
8529 "it to a btrfs developer so they can make fsck fix "
8530 "this particular case. bytenr is %Lu, bytes is %Lu\n",
8531 rec->start, rec->nr);
8537 * Ok great we all agreed on an extent record, let's go find the real
8538 * references and fix up the ones that don't match.
8540 list_for_each_entry(back, &rec->backrefs, list) {
8541 if (back->full_backref || !back->is_data)
8544 dback = to_data_backref(back);
8547 * Still ignoring backrefs that don't have a real ref attached
8550 if (dback->found_ref == 0)
8553 if (dback->bytes == best->bytes &&
8554 dback->disk_bytenr == best->bytenr)
8557 ret = repair_ref(info, path, dback, best);
8563 * Ok we messed with the actual refs, which means we need to drop our
8564 * entire cache and go back and rescan. I know this is a huge pain and
8565 * adds a lot of extra work, but it's the only way to be safe. Once all
8566 * the backrefs agree we may not need to do anything to the extent
8571 while (!list_empty(&entries)) {
8572 entry = list_entry(entries.next, struct extent_entry, list);
8573 list_del_init(&entry->list);
8579 static int process_duplicates(struct btrfs_root *root,
8580 struct cache_tree *extent_cache,
8581 struct extent_record *rec)
8583 struct extent_record *good, *tmp;
8584 struct cache_extent *cache;
8588 * If we found a extent record for this extent then return, or if we
8589 * have more than one duplicate we are likely going to need to delete
8592 if (rec->found_rec || rec->num_duplicates > 1)
8595 /* Shouldn't happen but just in case */
8596 BUG_ON(!rec->num_duplicates);
8599 * So this happens if we end up with a backref that doesn't match the
8600 * actual extent entry. So either the backref is bad or the extent
8601 * entry is bad. Either way we want to have the extent_record actually
8602 * reflect what we found in the extent_tree, so we need to take the
8603 * duplicate out and use that as the extent_record since the only way we
8604 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
8606 remove_cache_extent(extent_cache, &rec->cache);
8608 good = to_extent_record(rec->dups.next);
8609 list_del_init(&good->list);
8610 INIT_LIST_HEAD(&good->backrefs);
8611 INIT_LIST_HEAD(&good->dups);
8612 good->cache.start = good->start;
8613 good->cache.size = good->nr;
8614 good->content_checked = 0;
8615 good->owner_ref_checked = 0;
8616 good->num_duplicates = 0;
8617 good->refs = rec->refs;
8618 list_splice_init(&rec->backrefs, &good->backrefs);
8620 cache = lookup_cache_extent(extent_cache, good->start,
8624 tmp = container_of(cache, struct extent_record, cache);
8627 * If we find another overlapping extent and it's found_rec is
8628 * set then it's a duplicate and we need to try and delete
8631 if (tmp->found_rec || tmp->num_duplicates > 0) {
8632 if (list_empty(&good->list))
8633 list_add_tail(&good->list,
8634 &duplicate_extents);
8635 good->num_duplicates += tmp->num_duplicates + 1;
8636 list_splice_init(&tmp->dups, &good->dups);
8637 list_del_init(&tmp->list);
8638 list_add_tail(&tmp->list, &good->dups);
8639 remove_cache_extent(extent_cache, &tmp->cache);
8644 * Ok we have another non extent item backed extent rec, so lets
8645 * just add it to this extent and carry on like we did above.
8647 good->refs += tmp->refs;
8648 list_splice_init(&tmp->backrefs, &good->backrefs);
8649 remove_cache_extent(extent_cache, &tmp->cache);
8652 ret = insert_cache_extent(extent_cache, &good->cache);
8655 return good->num_duplicates ? 0 : 1;
8658 static int delete_duplicate_records(struct btrfs_root *root,
8659 struct extent_record *rec)
8661 struct btrfs_trans_handle *trans;
8662 LIST_HEAD(delete_list);
8663 struct btrfs_path path;
8664 struct extent_record *tmp, *good, *n;
8667 struct btrfs_key key;
8669 btrfs_init_path(&path);
8672 /* Find the record that covers all of the duplicates. */
8673 list_for_each_entry(tmp, &rec->dups, list) {
8674 if (good->start < tmp->start)
8676 if (good->nr > tmp->nr)
8679 if (tmp->start + tmp->nr < good->start + good->nr) {
8680 fprintf(stderr, "Ok we have overlapping extents that "
8681 "aren't completely covered by each other, this "
8682 "is going to require more careful thought. "
8683 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
8684 tmp->start, tmp->nr, good->start, good->nr);
8691 list_add_tail(&rec->list, &delete_list);
8693 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
8696 list_move_tail(&tmp->list, &delete_list);
8699 root = root->fs_info->extent_root;
8700 trans = btrfs_start_transaction(root, 1);
8701 if (IS_ERR(trans)) {
8702 ret = PTR_ERR(trans);
8706 list_for_each_entry(tmp, &delete_list, list) {
8707 if (tmp->found_rec == 0)
8709 key.objectid = tmp->start;
8710 key.type = BTRFS_EXTENT_ITEM_KEY;
8711 key.offset = tmp->nr;
8713 /* Shouldn't happen but just in case */
8714 if (tmp->metadata) {
8715 fprintf(stderr, "Well this shouldn't happen, extent "
8716 "record overlaps but is metadata? "
8717 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
8721 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
8727 ret = btrfs_del_item(trans, root, &path);
8730 btrfs_release_path(&path);
8733 err = btrfs_commit_transaction(trans, root);
8737 while (!list_empty(&delete_list)) {
8738 tmp = to_extent_record(delete_list.next);
8739 list_del_init(&tmp->list);
8745 while (!list_empty(&rec->dups)) {
8746 tmp = to_extent_record(rec->dups.next);
8747 list_del_init(&tmp->list);
8751 btrfs_release_path(&path);
8753 if (!ret && !nr_del)
8754 rec->num_duplicates = 0;
8756 return ret ? ret : nr_del;
8759 static int find_possible_backrefs(struct btrfs_fs_info *info,
8760 struct btrfs_path *path,
8761 struct cache_tree *extent_cache,
8762 struct extent_record *rec)
8764 struct btrfs_root *root;
8765 struct extent_backref *back;
8766 struct data_backref *dback;
8767 struct cache_extent *cache;
8768 struct btrfs_file_extent_item *fi;
8769 struct btrfs_key key;
8773 list_for_each_entry(back, &rec->backrefs, list) {
8774 /* Don't care about full backrefs (poor unloved backrefs) */
8775 if (back->full_backref || !back->is_data)
8778 dback = to_data_backref(back);
8780 /* We found this one, we don't need to do a lookup */
8781 if (dback->found_ref)
8784 key.objectid = dback->root;
8785 key.type = BTRFS_ROOT_ITEM_KEY;
8786 key.offset = (u64)-1;
8788 root = btrfs_read_fs_root(info, &key);
8790 /* No root, definitely a bad ref, skip */
8791 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
8793 /* Other err, exit */
8795 return PTR_ERR(root);
8797 key.objectid = dback->owner;
8798 key.type = BTRFS_EXTENT_DATA_KEY;
8799 key.offset = dback->offset;
8800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8802 btrfs_release_path(path);
8805 /* Didn't find it, we can carry on */
8810 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
8811 struct btrfs_file_extent_item);
8812 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
8813 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
8814 btrfs_release_path(path);
8815 cache = lookup_cache_extent(extent_cache, bytenr, 1);
8817 struct extent_record *tmp;
8818 tmp = container_of(cache, struct extent_record, cache);
8821 * If we found an extent record for the bytenr for this
8822 * particular backref then we can't add it to our
8823 * current extent record. We only want to add backrefs
8824 * that don't have a corresponding extent item in the
8825 * extent tree since they likely belong to this record
8826 * and we need to fix it if it doesn't match bytenrs.
8832 dback->found_ref += 1;
8833 dback->disk_bytenr = bytenr;
8834 dback->bytes = bytes;
8837 * Set this so the verify backref code knows not to trust the
8838 * values in this backref.
8847 * Record orphan data ref into corresponding root.
8849 * Return 0 if the extent item contains data ref and recorded.
8850 * Return 1 if the extent item contains no useful data ref
8851 * On that case, it may contains only shared_dataref or metadata backref
8852 * or the file extent exists(this should be handled by the extent bytenr
8854 * Return <0 if something goes wrong.
8856 static int record_orphan_data_extents(struct btrfs_fs_info *fs_info,
8857 struct extent_record *rec)
8859 struct btrfs_key key;
8860 struct btrfs_root *dest_root;
8861 struct extent_backref *back;
8862 struct data_backref *dback;
8863 struct orphan_data_extent *orphan;
8864 struct btrfs_path path;
8865 int recorded_data_ref = 0;
8870 btrfs_init_path(&path);
8871 list_for_each_entry(back, &rec->backrefs, list) {
8872 if (back->full_backref || !back->is_data ||
8873 !back->found_extent_tree)
8875 dback = to_data_backref(back);
8876 if (dback->found_ref)
8878 key.objectid = dback->root;
8879 key.type = BTRFS_ROOT_ITEM_KEY;
8880 key.offset = (u64)-1;
8882 dest_root = btrfs_read_fs_root(fs_info, &key);
8884 /* For non-exist root we just skip it */
8885 if (IS_ERR(dest_root) || !dest_root)
8888 key.objectid = dback->owner;
8889 key.type = BTRFS_EXTENT_DATA_KEY;
8890 key.offset = dback->offset;
8892 ret = btrfs_search_slot(NULL, dest_root, &key, &path, 0, 0);
8893 btrfs_release_path(&path);
8895 * For ret < 0, it's OK since the fs-tree may be corrupted,
8896 * we need to record it for inode/file extent rebuild.
8897 * For ret > 0, we record it only for file extent rebuild.
8898 * For ret == 0, the file extent exists but only bytenr
8899 * mismatch, let the original bytenr fix routine to handle,
8905 orphan = malloc(sizeof(*orphan));
8910 INIT_LIST_HEAD(&orphan->list);
8911 orphan->root = dback->root;
8912 orphan->objectid = dback->owner;
8913 orphan->offset = dback->offset;
8914 orphan->disk_bytenr = rec->cache.start;
8915 orphan->disk_len = rec->cache.size;
8916 list_add(&dest_root->orphan_data_extents, &orphan->list);
8917 recorded_data_ref = 1;
8920 btrfs_release_path(&path);
8922 return !recorded_data_ref;
8928 * when an incorrect extent item is found, this will delete
8929 * all of the existing entries for it and recreate them
8930 * based on what the tree scan found.
8932 static int fixup_extent_refs(struct btrfs_fs_info *info,
8933 struct cache_tree *extent_cache,
8934 struct extent_record *rec)
8936 struct btrfs_trans_handle *trans = NULL;
8938 struct btrfs_path path;
8939 struct list_head *cur = rec->backrefs.next;
8940 struct cache_extent *cache;
8941 struct extent_backref *back;
8945 if (rec->flag_block_full_backref)
8946 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8948 btrfs_init_path(&path);
8949 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
8951 * Sometimes the backrefs themselves are so broken they don't
8952 * get attached to any meaningful rec, so first go back and
8953 * check any of our backrefs that we couldn't find and throw
8954 * them into the list if we find the backref so that
8955 * verify_backrefs can figure out what to do.
8957 ret = find_possible_backrefs(info, &path, extent_cache, rec);
8962 /* step one, make sure all of the backrefs agree */
8963 ret = verify_backrefs(info, &path, rec);
8967 trans = btrfs_start_transaction(info->extent_root, 1);
8968 if (IS_ERR(trans)) {
8969 ret = PTR_ERR(trans);
8973 /* step two, delete all the existing records */
8974 ret = delete_extent_records(trans, info->extent_root, &path,
8980 /* was this block corrupt? If so, don't add references to it */
8981 cache = lookup_cache_extent(info->corrupt_blocks,
8982 rec->start, rec->max_size);
8988 /* step three, recreate all the refs we did find */
8989 while(cur != &rec->backrefs) {
8990 back = to_extent_backref(cur);
8994 * if we didn't find any references, don't create a
8997 if (!back->found_ref)
9000 rec->bad_full_backref = 0;
9001 ret = record_extent(trans, info, &path, rec, back, allocated, flags);
9009 int err = btrfs_commit_transaction(trans, info->extent_root);
9015 fprintf(stderr, "Repaired extent references for %llu\n",
9016 (unsigned long long)rec->start);
9018 btrfs_release_path(&path);
9022 static int fixup_extent_flags(struct btrfs_fs_info *fs_info,
9023 struct extent_record *rec)
9025 struct btrfs_trans_handle *trans;
9026 struct btrfs_root *root = fs_info->extent_root;
9027 struct btrfs_path path;
9028 struct btrfs_extent_item *ei;
9029 struct btrfs_key key;
9033 key.objectid = rec->start;
9034 if (rec->metadata) {
9035 key.type = BTRFS_METADATA_ITEM_KEY;
9036 key.offset = rec->info_level;
9038 key.type = BTRFS_EXTENT_ITEM_KEY;
9039 key.offset = rec->max_size;
9042 trans = btrfs_start_transaction(root, 0);
9044 return PTR_ERR(trans);
9046 btrfs_init_path(&path);
9047 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
9049 btrfs_release_path(&path);
9050 btrfs_commit_transaction(trans, root);
9053 fprintf(stderr, "Didn't find extent for %llu\n",
9054 (unsigned long long)rec->start);
9055 btrfs_release_path(&path);
9056 btrfs_commit_transaction(trans, root);
9060 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
9061 struct btrfs_extent_item);
9062 flags = btrfs_extent_flags(path.nodes[0], ei);
9063 if (rec->flag_block_full_backref) {
9064 fprintf(stderr, "setting full backref on %llu\n",
9065 (unsigned long long)key.objectid);
9066 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
9068 fprintf(stderr, "clearing full backref on %llu\n",
9069 (unsigned long long)key.objectid);
9070 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
9072 btrfs_set_extent_flags(path.nodes[0], ei, flags);
9073 btrfs_mark_buffer_dirty(path.nodes[0]);
9074 btrfs_release_path(&path);
9075 ret = btrfs_commit_transaction(trans, root);
9077 fprintf(stderr, "Repaired extent flags for %llu\n",
9078 (unsigned long long)rec->start);
9083 /* right now we only prune from the extent allocation tree */
9084 static int prune_one_block(struct btrfs_trans_handle *trans,
9085 struct btrfs_fs_info *info,
9086 struct btrfs_corrupt_block *corrupt)
9089 struct btrfs_path path;
9090 struct extent_buffer *eb;
9094 int level = corrupt->level + 1;
9096 btrfs_init_path(&path);
9098 /* we want to stop at the parent to our busted block */
9099 path.lowest_level = level;
9101 ret = btrfs_search_slot(trans, info->extent_root,
9102 &corrupt->key, &path, -1, 1);
9107 eb = path.nodes[level];
9114 * hopefully the search gave us the block we want to prune,
9115 * lets try that first
9117 slot = path.slots[level];
9118 found = btrfs_node_blockptr(eb, slot);
9119 if (found == corrupt->cache.start)
9122 nritems = btrfs_header_nritems(eb);
9124 /* the search failed, lets scan this node and hope we find it */
9125 for (slot = 0; slot < nritems; slot++) {
9126 found = btrfs_node_blockptr(eb, slot);
9127 if (found == corrupt->cache.start)
9131 * we couldn't find the bad block. TODO, search all the nodes for pointers
9134 if (eb == info->extent_root->node) {
9139 btrfs_release_path(&path);
9144 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
9145 ret = btrfs_del_ptr(info->extent_root, &path, level, slot);
9148 btrfs_release_path(&path);
9152 static int prune_corrupt_blocks(struct btrfs_fs_info *info)
9154 struct btrfs_trans_handle *trans = NULL;
9155 struct cache_extent *cache;
9156 struct btrfs_corrupt_block *corrupt;
9159 cache = search_cache_extent(info->corrupt_blocks, 0);
9163 trans = btrfs_start_transaction(info->extent_root, 1);
9165 return PTR_ERR(trans);
9167 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
9168 prune_one_block(trans, info, corrupt);
9169 remove_cache_extent(info->corrupt_blocks, cache);
9172 return btrfs_commit_transaction(trans, info->extent_root);
9176 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
9178 struct btrfs_block_group_cache *cache;
9183 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
9184 &start, &end, EXTENT_DIRTY);
9187 clear_extent_dirty(&fs_info->free_space_cache, start, end);
9192 cache = btrfs_lookup_first_block_group(fs_info, start);
9197 start = cache->key.objectid + cache->key.offset;
9201 static int check_extent_refs(struct btrfs_root *root,
9202 struct cache_tree *extent_cache)
9204 struct extent_record *rec;
9205 struct cache_extent *cache;
9211 * if we're doing a repair, we have to make sure
9212 * we don't allocate from the problem extents.
9213 * In the worst case, this will be all the
9216 cache = search_cache_extent(extent_cache, 0);
9218 rec = container_of(cache, struct extent_record, cache);
9219 set_extent_dirty(root->fs_info->excluded_extents,
9221 rec->start + rec->max_size - 1);
9222 cache = next_cache_extent(cache);
9225 /* pin down all the corrupted blocks too */
9226 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
9228 set_extent_dirty(root->fs_info->excluded_extents,
9230 cache->start + cache->size - 1);
9231 cache = next_cache_extent(cache);
9233 prune_corrupt_blocks(root->fs_info);
9234 reset_cached_block_groups(root->fs_info);
9237 reset_cached_block_groups(root->fs_info);
9240 * We need to delete any duplicate entries we find first otherwise we
9241 * could mess up the extent tree when we have backrefs that actually
9242 * belong to a different extent item and not the weird duplicate one.
9244 while (repair && !list_empty(&duplicate_extents)) {
9245 rec = to_extent_record(duplicate_extents.next);
9246 list_del_init(&rec->list);
9248 /* Sometimes we can find a backref before we find an actual
9249 * extent, so we need to process it a little bit to see if there
9250 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
9251 * if this is a backref screwup. If we need to delete stuff
9252 * process_duplicates() will return 0, otherwise it will return
9255 if (process_duplicates(root, extent_cache, rec))
9257 ret = delete_duplicate_records(root, rec);
9261 * delete_duplicate_records will return the number of entries
9262 * deleted, so if it's greater than 0 then we know we actually
9263 * did something and we need to remove.
9276 cache = search_cache_extent(extent_cache, 0);
9279 rec = container_of(cache, struct extent_record, cache);
9280 if (rec->num_duplicates) {
9281 fprintf(stderr, "extent item %llu has multiple extent "
9282 "items\n", (unsigned long long)rec->start);
9286 if (rec->refs != rec->extent_item_refs) {
9287 fprintf(stderr, "ref mismatch on [%llu %llu] ",
9288 (unsigned long long)rec->start,
9289 (unsigned long long)rec->nr);
9290 fprintf(stderr, "extent item %llu, found %llu\n",
9291 (unsigned long long)rec->extent_item_refs,
9292 (unsigned long long)rec->refs);
9293 ret = record_orphan_data_extents(root->fs_info, rec);
9299 if (all_backpointers_checked(rec, 1)) {
9300 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
9301 (unsigned long long)rec->start,
9302 (unsigned long long)rec->nr);
9306 if (!rec->owner_ref_checked) {
9307 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
9308 (unsigned long long)rec->start,
9309 (unsigned long long)rec->nr);
9314 if (repair && fix) {
9315 ret = fixup_extent_refs(root->fs_info, extent_cache, rec);
9321 if (rec->bad_full_backref) {
9322 fprintf(stderr, "bad full backref, on [%llu]\n",
9323 (unsigned long long)rec->start);
9325 ret = fixup_extent_flags(root->fs_info, rec);
9333 * Although it's not a extent ref's problem, we reuse this
9334 * routine for error reporting.
9335 * No repair function yet.
9337 if (rec->crossing_stripes) {
9339 "bad metadata [%llu, %llu) crossing stripe boundary\n",
9340 rec->start, rec->start + rec->max_size);
9344 if (rec->wrong_chunk_type) {
9346 "bad extent [%llu, %llu), type mismatch with chunk\n",
9347 rec->start, rec->start + rec->max_size);
9351 remove_cache_extent(extent_cache, cache);
9352 free_all_extent_backrefs(rec);
9353 if (!init_extent_tree && repair && (!cur_err || fix))
9354 clear_extent_dirty(root->fs_info->excluded_extents,
9356 rec->start + rec->max_size - 1);
9361 if (ret && ret != -EAGAIN) {
9362 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
9365 struct btrfs_trans_handle *trans;
9367 root = root->fs_info->extent_root;
9368 trans = btrfs_start_transaction(root, 1);
9369 if (IS_ERR(trans)) {
9370 ret = PTR_ERR(trans);
9374 btrfs_fix_block_accounting(trans, root);
9375 ret = btrfs_commit_transaction(trans, root);
9384 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
9388 if (type & BTRFS_BLOCK_GROUP_RAID0) {
9389 stripe_size = length;
9390 stripe_size /= num_stripes;
9391 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
9392 stripe_size = length * 2;
9393 stripe_size /= num_stripes;
9394 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
9395 stripe_size = length;
9396 stripe_size /= (num_stripes - 1);
9397 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
9398 stripe_size = length;
9399 stripe_size /= (num_stripes - 2);
9401 stripe_size = length;
9407 * Check the chunk with its block group/dev list ref:
9408 * Return 0 if all refs seems valid.
9409 * Return 1 if part of refs seems valid, need later check for rebuild ref
9410 * like missing block group and needs to search extent tree to rebuild them.
9411 * Return -1 if essential refs are missing and unable to rebuild.
9413 static int check_chunk_refs(struct chunk_record *chunk_rec,
9414 struct block_group_tree *block_group_cache,
9415 struct device_extent_tree *dev_extent_cache,
9418 struct cache_extent *block_group_item;
9419 struct block_group_record *block_group_rec;
9420 struct cache_extent *dev_extent_item;
9421 struct device_extent_record *dev_extent_rec;
9425 int metadump_v2 = 0;
9429 block_group_item = lookup_cache_extent(&block_group_cache->tree,
9432 if (block_group_item) {
9433 block_group_rec = container_of(block_group_item,
9434 struct block_group_record,
9436 if (chunk_rec->length != block_group_rec->offset ||
9437 chunk_rec->offset != block_group_rec->objectid ||
9439 chunk_rec->type_flags != block_group_rec->flags)) {
9442 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
9443 chunk_rec->objectid,
9448 chunk_rec->type_flags,
9449 block_group_rec->objectid,
9450 block_group_rec->type,
9451 block_group_rec->offset,
9452 block_group_rec->offset,
9453 block_group_rec->objectid,
9454 block_group_rec->flags);
9457 list_del_init(&block_group_rec->list);
9458 chunk_rec->bg_rec = block_group_rec;
9463 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
9464 chunk_rec->objectid,
9469 chunk_rec->type_flags);
9476 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
9477 chunk_rec->num_stripes);
9478 for (i = 0; i < chunk_rec->num_stripes; ++i) {
9479 devid = chunk_rec->stripes[i].devid;
9480 offset = chunk_rec->stripes[i].offset;
9481 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
9482 devid, offset, length);
9483 if (dev_extent_item) {
9484 dev_extent_rec = container_of(dev_extent_item,
9485 struct device_extent_record,
9487 if (dev_extent_rec->objectid != devid ||
9488 dev_extent_rec->offset != offset ||
9489 dev_extent_rec->chunk_offset != chunk_rec->offset ||
9490 dev_extent_rec->length != length) {
9493 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
9494 chunk_rec->objectid,
9497 chunk_rec->stripes[i].devid,
9498 chunk_rec->stripes[i].offset,
9499 dev_extent_rec->objectid,
9500 dev_extent_rec->offset,
9501 dev_extent_rec->length);
9504 list_move(&dev_extent_rec->chunk_list,
9505 &chunk_rec->dextents);
9510 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
9511 chunk_rec->objectid,
9514 chunk_rec->stripes[i].devid,
9515 chunk_rec->stripes[i].offset);
9522 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
9523 int check_chunks(struct cache_tree *chunk_cache,
9524 struct block_group_tree *block_group_cache,
9525 struct device_extent_tree *dev_extent_cache,
9526 struct list_head *good, struct list_head *bad,
9527 struct list_head *rebuild, int silent)
9529 struct cache_extent *chunk_item;
9530 struct chunk_record *chunk_rec;
9531 struct block_group_record *bg_rec;
9532 struct device_extent_record *dext_rec;
9536 chunk_item = first_cache_extent(chunk_cache);
9537 while (chunk_item) {
9538 chunk_rec = container_of(chunk_item, struct chunk_record,
9540 err = check_chunk_refs(chunk_rec, block_group_cache,
9541 dev_extent_cache, silent);
9544 if (err == 0 && good)
9545 list_add_tail(&chunk_rec->list, good);
9546 if (err > 0 && rebuild)
9547 list_add_tail(&chunk_rec->list, rebuild);
9549 list_add_tail(&chunk_rec->list, bad);
9550 chunk_item = next_cache_extent(chunk_item);
9553 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
9556 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
9564 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
9568 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
9579 static int check_device_used(struct device_record *dev_rec,
9580 struct device_extent_tree *dext_cache)
9582 struct cache_extent *cache;
9583 struct device_extent_record *dev_extent_rec;
9586 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
9588 dev_extent_rec = container_of(cache,
9589 struct device_extent_record,
9591 if (dev_extent_rec->objectid != dev_rec->devid)
9594 list_del_init(&dev_extent_rec->device_list);
9595 total_byte += dev_extent_rec->length;
9596 cache = next_cache_extent(cache);
9599 if (total_byte != dev_rec->byte_used) {
9601 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
9602 total_byte, dev_rec->byte_used, dev_rec->objectid,
9603 dev_rec->type, dev_rec->offset);
9610 /* check btrfs_dev_item -> btrfs_dev_extent */
9611 static int check_devices(struct rb_root *dev_cache,
9612 struct device_extent_tree *dev_extent_cache)
9614 struct rb_node *dev_node;
9615 struct device_record *dev_rec;
9616 struct device_extent_record *dext_rec;
9620 dev_node = rb_first(dev_cache);
9622 dev_rec = container_of(dev_node, struct device_record, node);
9623 err = check_device_used(dev_rec, dev_extent_cache);
9627 dev_node = rb_next(dev_node);
9629 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
9632 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
9633 dext_rec->objectid, dext_rec->offset, dext_rec->length);
9640 static int add_root_item_to_list(struct list_head *head,
9641 u64 objectid, u64 bytenr, u64 last_snapshot,
9642 u8 level, u8 drop_level,
9643 int level_size, struct btrfs_key *drop_key)
9646 struct root_item_record *ri_rec;
9647 ri_rec = malloc(sizeof(*ri_rec));
9650 ri_rec->bytenr = bytenr;
9651 ri_rec->objectid = objectid;
9652 ri_rec->level = level;
9653 ri_rec->level_size = level_size;
9654 ri_rec->drop_level = drop_level;
9655 ri_rec->last_snapshot = last_snapshot;
9657 memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
9658 list_add_tail(&ri_rec->list, head);
9663 static void free_root_item_list(struct list_head *list)
9665 struct root_item_record *ri_rec;
9667 while (!list_empty(list)) {
9668 ri_rec = list_first_entry(list, struct root_item_record,
9670 list_del_init(&ri_rec->list);
9675 static int deal_root_from_list(struct list_head *list,
9676 struct btrfs_root *root,
9677 struct block_info *bits,
9679 struct cache_tree *pending,
9680 struct cache_tree *seen,
9681 struct cache_tree *reada,
9682 struct cache_tree *nodes,
9683 struct cache_tree *extent_cache,
9684 struct cache_tree *chunk_cache,
9685 struct rb_root *dev_cache,
9686 struct block_group_tree *block_group_cache,
9687 struct device_extent_tree *dev_extent_cache)
9692 while (!list_empty(list)) {
9693 struct root_item_record *rec;
9694 struct extent_buffer *buf;
9695 rec = list_entry(list->next,
9696 struct root_item_record, list);
9698 buf = read_tree_block(root->fs_info->tree_root,
9699 rec->bytenr, rec->level_size, 0);
9700 if (!extent_buffer_uptodate(buf)) {
9701 free_extent_buffer(buf);
9705 ret = add_root_to_pending(buf, extent_cache, pending,
9706 seen, nodes, rec->objectid);
9710 * To rebuild extent tree, we need deal with snapshot
9711 * one by one, otherwise we deal with node firstly which
9712 * can maximize readahead.
9715 ret = run_next_block(root, bits, bits_nr, &last,
9716 pending, seen, reada, nodes,
9717 extent_cache, chunk_cache,
9718 dev_cache, block_group_cache,
9719 dev_extent_cache, rec);
9723 free_extent_buffer(buf);
9724 list_del(&rec->list);
9730 ret = run_next_block(root, bits, bits_nr, &last, pending, seen,
9731 reada, nodes, extent_cache, chunk_cache,
9732 dev_cache, block_group_cache,
9733 dev_extent_cache, NULL);
9743 static int check_chunks_and_extents(struct btrfs_root *root)
9745 struct rb_root dev_cache;
9746 struct cache_tree chunk_cache;
9747 struct block_group_tree block_group_cache;
9748 struct device_extent_tree dev_extent_cache;
9749 struct cache_tree extent_cache;
9750 struct cache_tree seen;
9751 struct cache_tree pending;
9752 struct cache_tree reada;
9753 struct cache_tree nodes;
9754 struct extent_io_tree excluded_extents;
9755 struct cache_tree corrupt_blocks;
9756 struct btrfs_path path;
9757 struct btrfs_key key;
9758 struct btrfs_key found_key;
9760 struct block_info *bits;
9762 struct extent_buffer *leaf;
9764 struct btrfs_root_item ri;
9765 struct list_head dropping_trees;
9766 struct list_head normal_trees;
9767 struct btrfs_root *root1;
9772 dev_cache = RB_ROOT;
9773 cache_tree_init(&chunk_cache);
9774 block_group_tree_init(&block_group_cache);
9775 device_extent_tree_init(&dev_extent_cache);
9777 cache_tree_init(&extent_cache);
9778 cache_tree_init(&seen);
9779 cache_tree_init(&pending);
9780 cache_tree_init(&nodes);
9781 cache_tree_init(&reada);
9782 cache_tree_init(&corrupt_blocks);
9783 extent_io_tree_init(&excluded_extents);
9784 INIT_LIST_HEAD(&dropping_trees);
9785 INIT_LIST_HEAD(&normal_trees);
9788 root->fs_info->excluded_extents = &excluded_extents;
9789 root->fs_info->fsck_extent_cache = &extent_cache;
9790 root->fs_info->free_extent_hook = free_extent_hook;
9791 root->fs_info->corrupt_blocks = &corrupt_blocks;
9795 bits = malloc(bits_nr * sizeof(struct block_info));
9801 if (ctx.progress_enabled) {
9802 ctx.tp = TASK_EXTENTS;
9803 task_start(ctx.info);
9807 root1 = root->fs_info->tree_root;
9808 level = btrfs_header_level(root1->node);
9809 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9810 root1->node->start, 0, level, 0,
9811 root1->nodesize, NULL);
9814 root1 = root->fs_info->chunk_root;
9815 level = btrfs_header_level(root1->node);
9816 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9817 root1->node->start, 0, level, 0,
9818 root1->nodesize, NULL);
9821 btrfs_init_path(&path);
9824 key.type = BTRFS_ROOT_ITEM_KEY;
9825 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
9830 leaf = path.nodes[0];
9831 slot = path.slots[0];
9832 if (slot >= btrfs_header_nritems(path.nodes[0])) {
9833 ret = btrfs_next_leaf(root, &path);
9836 leaf = path.nodes[0];
9837 slot = path.slots[0];
9839 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
9840 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
9841 unsigned long offset;
9844 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
9845 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
9846 last_snapshot = btrfs_root_last_snapshot(&ri);
9847 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
9848 level = btrfs_root_level(&ri);
9849 level_size = root->nodesize;
9850 ret = add_root_item_to_list(&normal_trees,
9852 btrfs_root_bytenr(&ri),
9853 last_snapshot, level,
9854 0, level_size, NULL);
9858 level = btrfs_root_level(&ri);
9859 level_size = root->nodesize;
9860 objectid = found_key.objectid;
9861 btrfs_disk_key_to_cpu(&found_key,
9863 ret = add_root_item_to_list(&dropping_trees,
9865 btrfs_root_bytenr(&ri),
9866 last_snapshot, level,
9868 level_size, &found_key);
9875 btrfs_release_path(&path);
9878 * check_block can return -EAGAIN if it fixes something, please keep
9879 * this in mind when dealing with return values from these functions, if
9880 * we get -EAGAIN we want to fall through and restart the loop.
9882 ret = deal_root_from_list(&normal_trees, root, bits, bits_nr, &pending,
9883 &seen, &reada, &nodes, &extent_cache,
9884 &chunk_cache, &dev_cache, &block_group_cache,
9891 ret = deal_root_from_list(&dropping_trees, root, bits, bits_nr,
9892 &pending, &seen, &reada, &nodes,
9893 &extent_cache, &chunk_cache, &dev_cache,
9894 &block_group_cache, &dev_extent_cache);
9901 ret = check_chunks(&chunk_cache, &block_group_cache,
9902 &dev_extent_cache, NULL, NULL, NULL, 0);
9909 ret = check_extent_refs(root, &extent_cache);
9916 ret = check_devices(&dev_cache, &dev_extent_cache);
9921 task_stop(ctx.info);
9923 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9924 extent_io_tree_cleanup(&excluded_extents);
9925 root->fs_info->fsck_extent_cache = NULL;
9926 root->fs_info->free_extent_hook = NULL;
9927 root->fs_info->corrupt_blocks = NULL;
9928 root->fs_info->excluded_extents = NULL;
9931 free_chunk_cache_tree(&chunk_cache);
9932 free_device_cache_tree(&dev_cache);
9933 free_block_group_tree(&block_group_cache);
9934 free_device_extent_tree(&dev_extent_cache);
9935 free_extent_cache_tree(&seen);
9936 free_extent_cache_tree(&pending);
9937 free_extent_cache_tree(&reada);
9938 free_extent_cache_tree(&nodes);
9941 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9942 free_extent_cache_tree(&seen);
9943 free_extent_cache_tree(&pending);
9944 free_extent_cache_tree(&reada);
9945 free_extent_cache_tree(&nodes);
9946 free_chunk_cache_tree(&chunk_cache);
9947 free_block_group_tree(&block_group_cache);
9948 free_device_cache_tree(&dev_cache);
9949 free_device_extent_tree(&dev_extent_cache);
9950 free_extent_record_cache(root->fs_info, &extent_cache);
9951 free_root_item_list(&normal_trees);
9952 free_root_item_list(&dropping_trees);
9953 extent_io_tree_cleanup(&excluded_extents);
9958 * Check backrefs of a tree block given by @bytenr or @eb.
9960 * @root: the root containing the @bytenr or @eb
9961 * @eb: tree block extent buffer, can be NULL
9962 * @bytenr: bytenr of the tree block to search
9963 * @level: tree level of the tree block
9964 * @owner: owner of the tree block
9966 * Return >0 for any error found and output error message
9967 * Return 0 for no error found
9969 static int check_tree_block_ref(struct btrfs_root *root,
9970 struct extent_buffer *eb, u64 bytenr,
9971 int level, u64 owner)
9973 struct btrfs_key key;
9974 struct btrfs_root *extent_root = root->fs_info->extent_root;
9975 struct btrfs_path path;
9976 struct btrfs_extent_item *ei;
9977 struct btrfs_extent_inline_ref *iref;
9978 struct extent_buffer *leaf;
9984 u32 nodesize = root->nodesize;
9987 int tree_reloc_root = 0;
9992 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
9993 btrfs_header_bytenr(root->node) == bytenr)
9994 tree_reloc_root = 1;
9996 btrfs_init_path(&path);
9997 key.objectid = bytenr;
9998 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
9999 key.type = BTRFS_METADATA_ITEM_KEY;
10001 key.type = BTRFS_EXTENT_ITEM_KEY;
10002 key.offset = (u64)-1;
10004 /* Search for the backref in extent tree */
10005 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10007 err |= BACKREF_MISSING;
10010 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10012 err |= BACKREF_MISSING;
10016 leaf = path.nodes[0];
10017 slot = path.slots[0];
10018 btrfs_item_key_to_cpu(leaf, &key, slot);
10020 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10022 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10023 skinny_level = (int)key.offset;
10024 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10026 struct btrfs_tree_block_info *info;
10028 info = (struct btrfs_tree_block_info *)(ei + 1);
10029 skinny_level = btrfs_tree_block_level(leaf, info);
10030 iref = (struct btrfs_extent_inline_ref *)(info + 1);
10037 if (!(btrfs_extent_flags(leaf, ei) &
10038 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10040 "extent[%llu %u] backref type mismatch, missing bit: %llx",
10041 key.objectid, nodesize,
10042 BTRFS_EXTENT_FLAG_TREE_BLOCK);
10043 err = BACKREF_MISMATCH;
10045 header_gen = btrfs_header_generation(eb);
10046 extent_gen = btrfs_extent_generation(leaf, ei);
10047 if (header_gen != extent_gen) {
10049 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
10050 key.objectid, nodesize, header_gen,
10052 err = BACKREF_MISMATCH;
10054 if (level != skinny_level) {
10056 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
10057 key.objectid, nodesize, level, skinny_level);
10058 err = BACKREF_MISMATCH;
10060 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
10062 "extent[%llu %u] is referred by other roots than %llu",
10063 key.objectid, nodesize, root->objectid);
10064 err = BACKREF_MISMATCH;
10069 * Iterate the extent/metadata item to find the exact backref
10071 item_size = btrfs_item_size_nr(leaf, slot);
10072 ptr = (unsigned long)iref;
10073 end = (unsigned long)ei + item_size;
10074 while (ptr < end) {
10075 iref = (struct btrfs_extent_inline_ref *)ptr;
10076 type = btrfs_extent_inline_ref_type(leaf, iref);
10077 offset = btrfs_extent_inline_ref_offset(leaf, iref);
10079 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
10080 (offset == root->objectid || offset == owner)) {
10082 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
10084 * Backref of tree reloc root points to itself, no need
10085 * to check backref any more.
10087 if (tree_reloc_root)
10090 /* Check if the backref points to valid referencer */
10091 found_ref = !check_tree_block_ref(root, NULL,
10092 offset, level + 1, owner);
10097 ptr += btrfs_extent_inline_ref_size(type);
10101 * Inlined extent item doesn't have what we need, check
10102 * TREE_BLOCK_REF_KEY
10105 btrfs_release_path(&path);
10106 key.objectid = bytenr;
10107 key.type = BTRFS_TREE_BLOCK_REF_KEY;
10108 key.offset = root->objectid;
10110 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10115 err |= BACKREF_MISSING;
10117 btrfs_release_path(&path);
10118 if (eb && (err & BACKREF_MISSING))
10119 error("extent[%llu %u] backref lost (owner: %llu, level: %u)",
10120 bytenr, nodesize, owner, level);
10125 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
10127 * Return >0 any error found and output error message
10128 * Return 0 for no error found
10130 static int check_extent_data_item(struct btrfs_root *root,
10131 struct extent_buffer *eb, int slot)
10133 struct btrfs_file_extent_item *fi;
10134 struct btrfs_path path;
10135 struct btrfs_root *extent_root = root->fs_info->extent_root;
10136 struct btrfs_key fi_key;
10137 struct btrfs_key dbref_key;
10138 struct extent_buffer *leaf;
10139 struct btrfs_extent_item *ei;
10140 struct btrfs_extent_inline_ref *iref;
10141 struct btrfs_extent_data_ref *dref;
10144 u64 disk_num_bytes;
10145 u64 extent_num_bytes;
10152 int found_dbackref = 0;
10156 btrfs_item_key_to_cpu(eb, &fi_key, slot);
10157 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
10159 /* Nothing to check for hole and inline data extents */
10160 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
10161 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
10164 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
10165 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
10166 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
10168 /* Check unaligned disk_num_bytes and num_bytes */
10169 if (!IS_ALIGNED(disk_num_bytes, root->sectorsize)) {
10171 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
10172 fi_key.objectid, fi_key.offset, disk_num_bytes,
10174 err |= BYTES_UNALIGNED;
10176 data_bytes_allocated += disk_num_bytes;
10178 if (!IS_ALIGNED(extent_num_bytes, root->sectorsize)) {
10180 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
10181 fi_key.objectid, fi_key.offset, extent_num_bytes,
10183 err |= BYTES_UNALIGNED;
10185 data_bytes_referenced += extent_num_bytes;
10187 owner = btrfs_header_owner(eb);
10189 /* Check the extent item of the file extent in extent tree */
10190 btrfs_init_path(&path);
10191 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10192 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
10193 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
10195 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
10197 err |= BACKREF_MISSING;
10201 leaf = path.nodes[0];
10202 slot = path.slots[0];
10203 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10205 extent_flags = btrfs_extent_flags(leaf, ei);
10207 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
10209 "extent[%llu %llu] backref type mismatch, wanted bit: %llx",
10210 disk_bytenr, disk_num_bytes,
10211 BTRFS_EXTENT_FLAG_DATA);
10212 err |= BACKREF_MISMATCH;
10215 /* Check data backref inside that extent item */
10216 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
10217 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10218 ptr = (unsigned long)iref;
10219 end = (unsigned long)ei + item_size;
10220 while (ptr < end) {
10221 iref = (struct btrfs_extent_inline_ref *)ptr;
10222 type = btrfs_extent_inline_ref_type(leaf, iref);
10223 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10225 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
10226 ref_root = btrfs_extent_data_ref_root(leaf, dref);
10227 if (ref_root == owner || ref_root == root->objectid)
10228 found_dbackref = 1;
10229 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
10230 found_dbackref = !check_tree_block_ref(root, NULL,
10231 btrfs_extent_inline_ref_offset(leaf, iref),
10235 if (found_dbackref)
10237 ptr += btrfs_extent_inline_ref_size(type);
10240 /* Didn't found inlined data backref, try EXTENT_DATA_REF_KEY */
10241 if (!found_dbackref) {
10242 btrfs_release_path(&path);
10244 btrfs_init_path(&path);
10245 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10246 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
10247 dbref_key.offset = hash_extent_data_ref(root->objectid,
10248 fi_key.objectid, fi_key.offset);
10250 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
10251 &dbref_key, &path, 0, 0);
10253 found_dbackref = 1;
10256 if (!found_dbackref)
10257 err |= BACKREF_MISSING;
10259 btrfs_release_path(&path);
10260 if (err & BACKREF_MISSING) {
10261 error("data extent[%llu %llu] backref lost",
10262 disk_bytenr, disk_num_bytes);
10268 * Get real tree block level for the case like shared block
10269 * Return >= 0 as tree level
10270 * Return <0 for error
10272 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
10274 struct extent_buffer *eb;
10275 struct btrfs_path path;
10276 struct btrfs_key key;
10277 struct btrfs_extent_item *ei;
10280 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10285 /* Search extent tree for extent generation and level */
10286 key.objectid = bytenr;
10287 key.type = BTRFS_METADATA_ITEM_KEY;
10288 key.offset = (u64)-1;
10290 btrfs_init_path(&path);
10291 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
10294 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
10302 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10303 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
10304 struct btrfs_extent_item);
10305 flags = btrfs_extent_flags(path.nodes[0], ei);
10306 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10311 /* Get transid for later read_tree_block() check */
10312 transid = btrfs_extent_generation(path.nodes[0], ei);
10314 /* Get backref level as one source */
10315 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10316 backref_level = key.offset;
10318 struct btrfs_tree_block_info *info;
10320 info = (struct btrfs_tree_block_info *)(ei + 1);
10321 backref_level = btrfs_tree_block_level(path.nodes[0], info);
10323 btrfs_release_path(&path);
10325 /* Get level from tree block as an alternative source */
10326 eb = read_tree_block_fs_info(fs_info, bytenr, nodesize, transid);
10327 if (!extent_buffer_uptodate(eb)) {
10328 free_extent_buffer(eb);
10331 header_level = btrfs_header_level(eb);
10332 free_extent_buffer(eb);
10334 if (header_level != backref_level)
10336 return header_level;
10339 btrfs_release_path(&path);
10344 * Check if a tree block backref is valid (points to a valid tree block)
10345 * if level == -1, level will be resolved
10346 * Return >0 for any error found and print error message
10348 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
10349 u64 bytenr, int level)
10351 struct btrfs_root *root;
10352 struct btrfs_key key;
10353 struct btrfs_path path;
10354 struct extent_buffer *eb;
10355 struct extent_buffer *node;
10356 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10360 /* Query level for level == -1 special case */
10362 level = query_tree_block_level(fs_info, bytenr);
10364 err |= REFERENCER_MISSING;
10368 key.objectid = root_id;
10369 key.type = BTRFS_ROOT_ITEM_KEY;
10370 key.offset = (u64)-1;
10372 root = btrfs_read_fs_root(fs_info, &key);
10373 if (IS_ERR(root)) {
10374 err |= REFERENCER_MISSING;
10378 /* Read out the tree block to get item/node key */
10379 eb = read_tree_block(root, bytenr, root->nodesize, 0);
10380 if (!extent_buffer_uptodate(eb)) {
10381 err |= REFERENCER_MISSING;
10382 free_extent_buffer(eb);
10386 /* Empty tree, no need to check key */
10387 if (!btrfs_header_nritems(eb) && !level) {
10388 free_extent_buffer(eb);
10393 btrfs_node_key_to_cpu(eb, &key, 0);
10395 btrfs_item_key_to_cpu(eb, &key, 0);
10397 free_extent_buffer(eb);
10399 btrfs_init_path(&path);
10400 path.lowest_level = level;
10401 /* Search with the first key, to ensure we can reach it */
10402 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10404 err |= REFERENCER_MISSING;
10408 node = path.nodes[level];
10409 if (btrfs_header_bytenr(node) != bytenr) {
10411 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
10412 bytenr, nodesize, bytenr,
10413 btrfs_header_bytenr(node));
10414 err |= REFERENCER_MISMATCH;
10416 if (btrfs_header_level(node) != level) {
10418 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
10419 bytenr, nodesize, level,
10420 btrfs_header_level(node));
10421 err |= REFERENCER_MISMATCH;
10425 btrfs_release_path(&path);
10427 if (err & REFERENCER_MISSING) {
10429 error("extent [%llu %d] lost referencer (owner: %llu)",
10430 bytenr, nodesize, root_id);
10433 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
10434 bytenr, nodesize, root_id, level);
10441 * Check if tree block @eb is tree reloc root.
10442 * Return 0 if it's not or any problem happens
10443 * Return 1 if it's a tree reloc root
10445 static int is_tree_reloc_root(struct btrfs_fs_info *fs_info,
10446 struct extent_buffer *eb)
10448 struct btrfs_root *tree_reloc_root;
10449 struct btrfs_key key;
10450 u64 bytenr = btrfs_header_bytenr(eb);
10451 u64 owner = btrfs_header_owner(eb);
10454 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
10455 key.offset = owner;
10456 key.type = BTRFS_ROOT_ITEM_KEY;
10458 tree_reloc_root = btrfs_read_fs_root_no_cache(fs_info, &key);
10459 if (IS_ERR(tree_reloc_root))
10462 if (bytenr == btrfs_header_bytenr(tree_reloc_root->node))
10464 btrfs_free_fs_root(tree_reloc_root);
10469 * Check referencer for shared block backref
10470 * If level == -1, this function will resolve the level.
10472 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
10473 u64 parent, u64 bytenr, int level)
10475 struct extent_buffer *eb;
10476 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10478 int found_parent = 0;
10481 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10482 if (!extent_buffer_uptodate(eb))
10486 level = query_tree_block_level(fs_info, bytenr);
10490 /* It's possible it's a tree reloc root */
10491 if (parent == bytenr) {
10492 if (is_tree_reloc_root(fs_info, eb))
10497 if (level + 1 != btrfs_header_level(eb))
10500 nr = btrfs_header_nritems(eb);
10501 for (i = 0; i < nr; i++) {
10502 if (bytenr == btrfs_node_blockptr(eb, i)) {
10508 free_extent_buffer(eb);
10509 if (!found_parent) {
10511 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
10512 bytenr, nodesize, parent, level);
10513 return REFERENCER_MISSING;
10519 * Check referencer for normal (inlined) data ref
10520 * If len == 0, it will be resolved by searching in extent tree
10522 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
10523 u64 root_id, u64 objectid, u64 offset,
10524 u64 bytenr, u64 len, u32 count)
10526 struct btrfs_root *root;
10527 struct btrfs_root *extent_root = fs_info->extent_root;
10528 struct btrfs_key key;
10529 struct btrfs_path path;
10530 struct extent_buffer *leaf;
10531 struct btrfs_file_extent_item *fi;
10532 u32 found_count = 0;
10537 key.objectid = bytenr;
10538 key.type = BTRFS_EXTENT_ITEM_KEY;
10539 key.offset = (u64)-1;
10541 btrfs_init_path(&path);
10542 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10545 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10548 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10549 if (key.objectid != bytenr ||
10550 key.type != BTRFS_EXTENT_ITEM_KEY)
10553 btrfs_release_path(&path);
10555 key.objectid = root_id;
10556 key.type = BTRFS_ROOT_ITEM_KEY;
10557 key.offset = (u64)-1;
10558 btrfs_init_path(&path);
10560 root = btrfs_read_fs_root(fs_info, &key);
10564 key.objectid = objectid;
10565 key.type = BTRFS_EXTENT_DATA_KEY;
10567 * It can be nasty as data backref offset is
10568 * file offset - file extent offset, which is smaller or
10569 * equal to original backref offset. The only special case is
10570 * overflow. So we need to special check and do further search.
10572 key.offset = offset & (1ULL << 63) ? 0 : offset;
10574 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10579 * Search afterwards to get correct one
10580 * NOTE: As we must do a comprehensive check on the data backref to
10581 * make sure the dref count also matches, we must iterate all file
10582 * extents for that inode.
10585 leaf = path.nodes[0];
10586 slot = path.slots[0];
10588 btrfs_item_key_to_cpu(leaf, &key, slot);
10589 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
10591 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
10593 * Except normal disk bytenr and disk num bytes, we still
10594 * need to do extra check on dbackref offset as
10595 * dbackref offset = file_offset - file_extent_offset
10597 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
10598 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
10599 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
10603 ret = btrfs_next_item(root, &path);
10608 btrfs_release_path(&path);
10609 if (found_count != count) {
10611 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
10612 bytenr, len, root_id, objectid, offset, count, found_count);
10613 return REFERENCER_MISSING;
10619 * Check if the referencer of a shared data backref exists
10621 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
10622 u64 parent, u64 bytenr)
10624 struct extent_buffer *eb;
10625 struct btrfs_key key;
10626 struct btrfs_file_extent_item *fi;
10627 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10629 int found_parent = 0;
10632 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10633 if (!extent_buffer_uptodate(eb))
10636 nr = btrfs_header_nritems(eb);
10637 for (i = 0; i < nr; i++) {
10638 btrfs_item_key_to_cpu(eb, &key, i);
10639 if (key.type != BTRFS_EXTENT_DATA_KEY)
10642 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
10643 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
10646 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
10653 free_extent_buffer(eb);
10654 if (!found_parent) {
10655 error("shared extent %llu referencer lost (parent: %llu)",
10657 return REFERENCER_MISSING;
10663 * This function will check a given extent item, including its backref and
10664 * itself (like crossing stripe boundary and type)
10666 * Since we don't use extent_record anymore, introduce new error bit
10668 static int check_extent_item(struct btrfs_fs_info *fs_info,
10669 struct extent_buffer *eb, int slot)
10671 struct btrfs_extent_item *ei;
10672 struct btrfs_extent_inline_ref *iref;
10673 struct btrfs_extent_data_ref *dref;
10677 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10678 u32 item_size = btrfs_item_size_nr(eb, slot);
10683 struct btrfs_key key;
10687 btrfs_item_key_to_cpu(eb, &key, slot);
10688 if (key.type == BTRFS_EXTENT_ITEM_KEY)
10689 bytes_used += key.offset;
10691 bytes_used += nodesize;
10693 if (item_size < sizeof(*ei)) {
10695 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
10696 * old thing when on disk format is still un-determined.
10697 * No need to care about it anymore
10699 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
10703 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
10704 flags = btrfs_extent_flags(eb, ei);
10706 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
10708 if (metadata && check_crossing_stripes(global_info, key.objectid,
10710 error("bad metadata [%llu, %llu) crossing stripe boundary",
10711 key.objectid, key.objectid + nodesize);
10712 err |= CROSSING_STRIPE_BOUNDARY;
10715 ptr = (unsigned long)(ei + 1);
10717 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
10718 /* Old EXTENT_ITEM metadata */
10719 struct btrfs_tree_block_info *info;
10721 info = (struct btrfs_tree_block_info *)ptr;
10722 level = btrfs_tree_block_level(eb, info);
10723 ptr += sizeof(struct btrfs_tree_block_info);
10725 /* New METADATA_ITEM */
10726 level = key.offset;
10728 end = (unsigned long)ei + item_size;
10731 err |= ITEM_SIZE_MISMATCH;
10735 /* Now check every backref in this extent item */
10737 iref = (struct btrfs_extent_inline_ref *)ptr;
10738 type = btrfs_extent_inline_ref_type(eb, iref);
10739 offset = btrfs_extent_inline_ref_offset(eb, iref);
10741 case BTRFS_TREE_BLOCK_REF_KEY:
10742 ret = check_tree_block_backref(fs_info, offset, key.objectid,
10746 case BTRFS_SHARED_BLOCK_REF_KEY:
10747 ret = check_shared_block_backref(fs_info, offset, key.objectid,
10751 case BTRFS_EXTENT_DATA_REF_KEY:
10752 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10753 ret = check_extent_data_backref(fs_info,
10754 btrfs_extent_data_ref_root(eb, dref),
10755 btrfs_extent_data_ref_objectid(eb, dref),
10756 btrfs_extent_data_ref_offset(eb, dref),
10757 key.objectid, key.offset,
10758 btrfs_extent_data_ref_count(eb, dref));
10761 case BTRFS_SHARED_DATA_REF_KEY:
10762 ret = check_shared_data_backref(fs_info, offset, key.objectid);
10766 error("extent[%llu %d %llu] has unknown ref type: %d",
10767 key.objectid, key.type, key.offset, type);
10768 err |= UNKNOWN_TYPE;
10772 ptr += btrfs_extent_inline_ref_size(type);
10781 * Check if a dev extent item is referred correctly by its chunk
10783 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
10784 struct extent_buffer *eb, int slot)
10786 struct btrfs_root *chunk_root = fs_info->chunk_root;
10787 struct btrfs_dev_extent *ptr;
10788 struct btrfs_path path;
10789 struct btrfs_key chunk_key;
10790 struct btrfs_key devext_key;
10791 struct btrfs_chunk *chunk;
10792 struct extent_buffer *l;
10796 int found_chunk = 0;
10799 btrfs_item_key_to_cpu(eb, &devext_key, slot);
10800 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
10801 length = btrfs_dev_extent_length(eb, ptr);
10803 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
10804 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10805 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
10807 btrfs_init_path(&path);
10808 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10813 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
10814 if (btrfs_chunk_length(l, chunk) != length)
10817 num_stripes = btrfs_chunk_num_stripes(l, chunk);
10818 for (i = 0; i < num_stripes; i++) {
10819 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
10820 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
10822 if (devid == devext_key.objectid &&
10823 offset == devext_key.offset) {
10829 btrfs_release_path(&path);
10830 if (!found_chunk) {
10832 "device extent[%llu, %llu, %llu] did not find the related chunk",
10833 devext_key.objectid, devext_key.offset, length);
10834 return REFERENCER_MISSING;
10840 * Check if the used space is correct with the dev item
10842 static int check_dev_item(struct btrfs_fs_info *fs_info,
10843 struct extent_buffer *eb, int slot)
10845 struct btrfs_root *dev_root = fs_info->dev_root;
10846 struct btrfs_dev_item *dev_item;
10847 struct btrfs_path path;
10848 struct btrfs_key key;
10849 struct btrfs_dev_extent *ptr;
10855 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
10856 dev_id = btrfs_device_id(eb, dev_item);
10857 used = btrfs_device_bytes_used(eb, dev_item);
10859 key.objectid = dev_id;
10860 key.type = BTRFS_DEV_EXTENT_KEY;
10863 btrfs_init_path(&path);
10864 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
10866 btrfs_item_key_to_cpu(eb, &key, slot);
10867 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
10868 key.objectid, key.type, key.offset);
10869 btrfs_release_path(&path);
10870 return REFERENCER_MISSING;
10873 /* Iterate dev_extents to calculate the used space of a device */
10875 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10877 if (key.objectid > dev_id)
10879 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
10882 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
10883 struct btrfs_dev_extent);
10884 total += btrfs_dev_extent_length(path.nodes[0], ptr);
10886 ret = btrfs_next_item(dev_root, &path);
10890 btrfs_release_path(&path);
10892 if (used != total) {
10893 btrfs_item_key_to_cpu(eb, &key, slot);
10895 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
10896 total, used, BTRFS_ROOT_TREE_OBJECTID,
10897 BTRFS_DEV_EXTENT_KEY, dev_id);
10898 return ACCOUNTING_MISMATCH;
10904 * Check a block group item with its referener (chunk) and its used space
10905 * with extent/metadata item
10907 static int check_block_group_item(struct btrfs_fs_info *fs_info,
10908 struct extent_buffer *eb, int slot)
10910 struct btrfs_root *extent_root = fs_info->extent_root;
10911 struct btrfs_root *chunk_root = fs_info->chunk_root;
10912 struct btrfs_block_group_item *bi;
10913 struct btrfs_block_group_item bg_item;
10914 struct btrfs_path path;
10915 struct btrfs_key bg_key;
10916 struct btrfs_key chunk_key;
10917 struct btrfs_key extent_key;
10918 struct btrfs_chunk *chunk;
10919 struct extent_buffer *leaf;
10920 struct btrfs_extent_item *ei;
10921 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10929 btrfs_item_key_to_cpu(eb, &bg_key, slot);
10930 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
10931 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
10932 used = btrfs_block_group_used(&bg_item);
10933 bg_flags = btrfs_block_group_flags(&bg_item);
10935 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
10936 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10937 chunk_key.offset = bg_key.objectid;
10939 btrfs_init_path(&path);
10940 /* Search for the referencer chunk */
10941 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10944 "block group[%llu %llu] did not find the related chunk item",
10945 bg_key.objectid, bg_key.offset);
10946 err |= REFERENCER_MISSING;
10948 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
10949 struct btrfs_chunk);
10950 if (btrfs_chunk_length(path.nodes[0], chunk) !=
10953 "block group[%llu %llu] related chunk item length does not match",
10954 bg_key.objectid, bg_key.offset);
10955 err |= REFERENCER_MISMATCH;
10958 btrfs_release_path(&path);
10960 /* Search from the block group bytenr */
10961 extent_key.objectid = bg_key.objectid;
10962 extent_key.type = 0;
10963 extent_key.offset = 0;
10965 btrfs_init_path(&path);
10966 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
10970 /* Iterate extent tree to account used space */
10972 leaf = path.nodes[0];
10973 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
10974 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
10977 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
10978 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
10980 if (extent_key.objectid < bg_key.objectid)
10983 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
10986 total += extent_key.offset;
10988 ei = btrfs_item_ptr(leaf, path.slots[0],
10989 struct btrfs_extent_item);
10990 flags = btrfs_extent_flags(leaf, ei);
10991 if (flags & BTRFS_EXTENT_FLAG_DATA) {
10992 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
10994 "bad extent[%llu, %llu) type mismatch with chunk",
10995 extent_key.objectid,
10996 extent_key.objectid + extent_key.offset);
10997 err |= CHUNK_TYPE_MISMATCH;
10999 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
11000 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
11001 BTRFS_BLOCK_GROUP_METADATA))) {
11003 "bad extent[%llu, %llu) type mismatch with chunk",
11004 extent_key.objectid,
11005 extent_key.objectid + nodesize);
11006 err |= CHUNK_TYPE_MISMATCH;
11010 ret = btrfs_next_item(extent_root, &path);
11016 btrfs_release_path(&path);
11018 if (total != used) {
11020 "block group[%llu %llu] used %llu but extent items used %llu",
11021 bg_key.objectid, bg_key.offset, used, total);
11022 err |= ACCOUNTING_MISMATCH;
11028 * Check a chunk item.
11029 * Including checking all referred dev_extents and block group
11031 static int check_chunk_item(struct btrfs_fs_info *fs_info,
11032 struct extent_buffer *eb, int slot)
11034 struct btrfs_root *extent_root = fs_info->extent_root;
11035 struct btrfs_root *dev_root = fs_info->dev_root;
11036 struct btrfs_path path;
11037 struct btrfs_key chunk_key;
11038 struct btrfs_key bg_key;
11039 struct btrfs_key devext_key;
11040 struct btrfs_chunk *chunk;
11041 struct extent_buffer *leaf;
11042 struct btrfs_block_group_item *bi;
11043 struct btrfs_block_group_item bg_item;
11044 struct btrfs_dev_extent *ptr;
11045 u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
11057 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
11058 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
11059 length = btrfs_chunk_length(eb, chunk);
11060 chunk_end = chunk_key.offset + length;
11061 if (!IS_ALIGNED(length, sectorsize)) {
11062 error("chunk[%llu %llu) not aligned to %u",
11063 chunk_key.offset, chunk_end, sectorsize);
11064 err |= BYTES_UNALIGNED;
11068 type = btrfs_chunk_type(eb, chunk);
11069 profile = type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
11070 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
11071 error("chunk[%llu %llu) has no chunk type",
11072 chunk_key.offset, chunk_end);
11073 err |= UNKNOWN_TYPE;
11075 if (profile && (profile & (profile - 1))) {
11076 error("chunk[%llu %llu) multiple profiles detected: %llx",
11077 chunk_key.offset, chunk_end, profile);
11078 err |= UNKNOWN_TYPE;
11081 bg_key.objectid = chunk_key.offset;
11082 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
11083 bg_key.offset = length;
11085 btrfs_init_path(&path);
11086 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
11089 "chunk[%llu %llu) did not find the related block group item",
11090 chunk_key.offset, chunk_end);
11091 err |= REFERENCER_MISSING;
11093 leaf = path.nodes[0];
11094 bi = btrfs_item_ptr(leaf, path.slots[0],
11095 struct btrfs_block_group_item);
11096 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
11098 if (btrfs_block_group_flags(&bg_item) != type) {
11100 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
11101 chunk_key.offset, chunk_end, type,
11102 btrfs_block_group_flags(&bg_item));
11103 err |= REFERENCER_MISSING;
11107 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
11108 for (i = 0; i < num_stripes; i++) {
11109 btrfs_release_path(&path);
11110 btrfs_init_path(&path);
11111 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
11112 devext_key.type = BTRFS_DEV_EXTENT_KEY;
11113 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
11115 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
11118 goto not_match_dev;
11120 leaf = path.nodes[0];
11121 ptr = btrfs_item_ptr(leaf, path.slots[0],
11122 struct btrfs_dev_extent);
11123 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
11124 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
11125 if (objectid != chunk_key.objectid ||
11126 offset != chunk_key.offset ||
11127 btrfs_dev_extent_length(leaf, ptr) != length)
11128 goto not_match_dev;
11131 err |= BACKREF_MISSING;
11133 "chunk[%llu %llu) stripe %d did not find the related dev extent",
11134 chunk_key.objectid, chunk_end, i);
11137 btrfs_release_path(&path);
11143 * Main entry function to check known items and update related accounting info
11145 static int check_leaf_items(struct btrfs_root *root, struct extent_buffer *eb)
11147 struct btrfs_fs_info *fs_info = root->fs_info;
11148 struct btrfs_key key;
11151 struct btrfs_extent_data_ref *dref;
11156 btrfs_item_key_to_cpu(eb, &key, slot);
11160 case BTRFS_EXTENT_DATA_KEY:
11161 ret = check_extent_data_item(root, eb, slot);
11164 case BTRFS_BLOCK_GROUP_ITEM_KEY:
11165 ret = check_block_group_item(fs_info, eb, slot);
11168 case BTRFS_DEV_ITEM_KEY:
11169 ret = check_dev_item(fs_info, eb, slot);
11172 case BTRFS_CHUNK_ITEM_KEY:
11173 ret = check_chunk_item(fs_info, eb, slot);
11176 case BTRFS_DEV_EXTENT_KEY:
11177 ret = check_dev_extent_item(fs_info, eb, slot);
11180 case BTRFS_EXTENT_ITEM_KEY:
11181 case BTRFS_METADATA_ITEM_KEY:
11182 ret = check_extent_item(fs_info, eb, slot);
11185 case BTRFS_EXTENT_CSUM_KEY:
11186 total_csum_bytes += btrfs_item_size_nr(eb, slot);
11188 case BTRFS_TREE_BLOCK_REF_KEY:
11189 ret = check_tree_block_backref(fs_info, key.offset,
11193 case BTRFS_EXTENT_DATA_REF_KEY:
11194 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
11195 ret = check_extent_data_backref(fs_info,
11196 btrfs_extent_data_ref_root(eb, dref),
11197 btrfs_extent_data_ref_objectid(eb, dref),
11198 btrfs_extent_data_ref_offset(eb, dref),
11200 btrfs_extent_data_ref_count(eb, dref));
11203 case BTRFS_SHARED_BLOCK_REF_KEY:
11204 ret = check_shared_block_backref(fs_info, key.offset,
11208 case BTRFS_SHARED_DATA_REF_KEY:
11209 ret = check_shared_data_backref(fs_info, key.offset,
11217 if (++slot < btrfs_header_nritems(eb))
11224 * Helper function for later fs/subvol tree check. To determine if a tree
11225 * block should be checked.
11226 * This function will ensure only the direct referencer with lowest rootid to
11227 * check a fs/subvolume tree block.
11229 * Backref check at extent tree would detect errors like missing subvolume
11230 * tree, so we can do aggressive check to reduce duplicated checks.
11232 static int should_check(struct btrfs_root *root, struct extent_buffer *eb)
11234 struct btrfs_root *extent_root = root->fs_info->extent_root;
11235 struct btrfs_key key;
11236 struct btrfs_path path;
11237 struct extent_buffer *leaf;
11239 struct btrfs_extent_item *ei;
11245 struct btrfs_extent_inline_ref *iref;
11248 btrfs_init_path(&path);
11249 key.objectid = btrfs_header_bytenr(eb);
11250 key.type = BTRFS_METADATA_ITEM_KEY;
11251 key.offset = (u64)-1;
11254 * Any failure in backref resolving means we can't determine
11255 * whom the tree block belongs to.
11256 * So in that case, we need to check that tree block
11258 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11262 ret = btrfs_previous_extent_item(extent_root, &path,
11263 btrfs_header_bytenr(eb));
11267 leaf = path.nodes[0];
11268 slot = path.slots[0];
11269 btrfs_item_key_to_cpu(leaf, &key, slot);
11270 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
11272 if (key.type == BTRFS_METADATA_ITEM_KEY) {
11273 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
11275 struct btrfs_tree_block_info *info;
11277 info = (struct btrfs_tree_block_info *)(ei + 1);
11278 iref = (struct btrfs_extent_inline_ref *)(info + 1);
11281 item_size = btrfs_item_size_nr(leaf, slot);
11282 ptr = (unsigned long)iref;
11283 end = (unsigned long)ei + item_size;
11284 while (ptr < end) {
11285 iref = (struct btrfs_extent_inline_ref *)ptr;
11286 type = btrfs_extent_inline_ref_type(leaf, iref);
11287 offset = btrfs_extent_inline_ref_offset(leaf, iref);
11290 * We only check the tree block if current root is
11291 * the lowest referencer of it.
11293 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
11294 offset < root->objectid) {
11295 btrfs_release_path(&path);
11299 ptr += btrfs_extent_inline_ref_size(type);
11302 * Normally we should also check keyed tree block ref, but that may be
11303 * very time consuming. Inlined ref should already make us skip a lot
11304 * of refs now. So skip search keyed tree block ref.
11308 btrfs_release_path(&path);
11313 * Traversal function for tree block. We will do:
11314 * 1) Skip shared fs/subvolume tree blocks
11315 * 2) Update related bytes accounting
11316 * 3) Pre-order traversal
11318 static int traverse_tree_block(struct btrfs_root *root,
11319 struct extent_buffer *node)
11321 struct extent_buffer *eb;
11322 struct btrfs_key key;
11323 struct btrfs_key drop_key;
11331 * Skip shared fs/subvolume tree block, in that case they will
11332 * be checked by referencer with lowest rootid
11334 if (is_fstree(root->objectid) && !should_check(root, node))
11337 /* Update bytes accounting */
11338 total_btree_bytes += node->len;
11339 if (fs_root_objectid(btrfs_header_owner(node)))
11340 total_fs_tree_bytes += node->len;
11341 if (btrfs_header_owner(node) == BTRFS_EXTENT_TREE_OBJECTID)
11342 total_extent_tree_bytes += node->len;
11343 if (!found_old_backref &&
11344 btrfs_header_owner(node) == BTRFS_TREE_RELOC_OBJECTID &&
11345 btrfs_header_backref_rev(node) == BTRFS_MIXED_BACKREF_REV &&
11346 !btrfs_header_flag(node, BTRFS_HEADER_FLAG_RELOC))
11347 found_old_backref = 1;
11349 /* pre-order tranversal, check itself first */
11350 level = btrfs_header_level(node);
11351 ret = check_tree_block_ref(root, node, btrfs_header_bytenr(node),
11352 btrfs_header_level(node),
11353 btrfs_header_owner(node));
11357 "check %s failed root %llu bytenr %llu level %d, force continue check",
11358 level ? "node":"leaf", root->objectid,
11359 btrfs_header_bytenr(node), btrfs_header_level(node));
11362 btree_space_waste += btrfs_leaf_free_space(root, node);
11363 ret = check_leaf_items(root, node);
11368 nr = btrfs_header_nritems(node);
11369 btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
11370 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) - nr) *
11371 sizeof(struct btrfs_key_ptr);
11373 /* Then check all its children */
11374 for (i = 0; i < nr; i++) {
11375 u64 blocknr = btrfs_node_blockptr(node, i);
11377 btrfs_node_key_to_cpu(node, &key, i);
11378 if (level == root->root_item.drop_level &&
11379 is_dropped_key(&key, &drop_key))
11383 * As a btrfs tree has most 8 levels (0..7), so it's quite safe
11384 * to call the function itself.
11386 eb = read_tree_block(root, blocknr, root->nodesize, 0);
11387 if (extent_buffer_uptodate(eb)) {
11388 ret = traverse_tree_block(root, eb);
11391 free_extent_buffer(eb);
11398 * Low memory usage version check_chunks_and_extents.
11400 static int check_chunks_and_extents_v2(struct btrfs_root *root)
11402 struct btrfs_path path;
11403 struct btrfs_key key;
11404 struct btrfs_root *root1;
11405 struct btrfs_root *cur_root;
11409 root1 = root->fs_info->chunk_root;
11410 ret = traverse_tree_block(root1, root1->node);
11413 root1 = root->fs_info->tree_root;
11414 ret = traverse_tree_block(root1, root1->node);
11417 btrfs_init_path(&path);
11418 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
11420 key.type = BTRFS_ROOT_ITEM_KEY;
11422 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
11424 error("cannot find extent treet in tree_root");
11429 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
11430 if (key.type != BTRFS_ROOT_ITEM_KEY)
11432 key.offset = (u64)-1;
11434 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
11435 cur_root = btrfs_read_fs_root_no_cache(root->fs_info,
11438 cur_root = btrfs_read_fs_root(root->fs_info, &key);
11439 if (IS_ERR(cur_root) || !cur_root) {
11440 error("failed to read tree: %lld", key.objectid);
11444 ret = traverse_tree_block(cur_root, cur_root->node);
11447 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
11448 btrfs_free_fs_root(cur_root);
11450 ret = btrfs_next_item(root1, &path);
11456 btrfs_release_path(&path);
11460 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
11461 struct btrfs_root *root, int overwrite)
11463 struct extent_buffer *c;
11464 struct extent_buffer *old = root->node;
11467 struct btrfs_disk_key disk_key = {0,0,0};
11473 extent_buffer_get(c);
11476 c = btrfs_alloc_free_block(trans, root,
11478 root->root_key.objectid,
11479 &disk_key, level, 0, 0);
11482 extent_buffer_get(c);
11486 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
11487 btrfs_set_header_level(c, level);
11488 btrfs_set_header_bytenr(c, c->start);
11489 btrfs_set_header_generation(c, trans->transid);
11490 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
11491 btrfs_set_header_owner(c, root->root_key.objectid);
11493 write_extent_buffer(c, root->fs_info->fsid,
11494 btrfs_header_fsid(), BTRFS_FSID_SIZE);
11496 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
11497 btrfs_header_chunk_tree_uuid(c),
11500 btrfs_mark_buffer_dirty(c);
11502 * this case can happen in the following case:
11504 * 1.overwrite previous root.
11506 * 2.reinit reloc data root, this is because we skip pin
11507 * down reloc data tree before which means we can allocate
11508 * same block bytenr here.
11510 if (old->start == c->start) {
11511 btrfs_set_root_generation(&root->root_item,
11513 root->root_item.level = btrfs_header_level(root->node);
11514 ret = btrfs_update_root(trans, root->fs_info->tree_root,
11515 &root->root_key, &root->root_item);
11517 free_extent_buffer(c);
11521 free_extent_buffer(old);
11523 add_root_to_dirty_list(root);
11527 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
11528 struct extent_buffer *eb, int tree_root)
11530 struct extent_buffer *tmp;
11531 struct btrfs_root_item *ri;
11532 struct btrfs_key key;
11535 int level = btrfs_header_level(eb);
11541 * If we have pinned this block before, don't pin it again.
11542 * This can not only avoid forever loop with broken filesystem
11543 * but also give us some speedups.
11545 if (test_range_bit(&fs_info->pinned_extents, eb->start,
11546 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
11549 btrfs_pin_extent(fs_info, eb->start, eb->len);
11551 nodesize = btrfs_super_nodesize(fs_info->super_copy);
11552 nritems = btrfs_header_nritems(eb);
11553 for (i = 0; i < nritems; i++) {
11555 btrfs_item_key_to_cpu(eb, &key, i);
11556 if (key.type != BTRFS_ROOT_ITEM_KEY)
11558 /* Skip the extent root and reloc roots */
11559 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
11560 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
11561 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
11563 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
11564 bytenr = btrfs_disk_root_bytenr(eb, ri);
11567 * If at any point we start needing the real root we
11568 * will have to build a stump root for the root we are
11569 * in, but for now this doesn't actually use the root so
11570 * just pass in extent_root.
11572 tmp = read_tree_block(fs_info->extent_root, bytenr,
11574 if (!extent_buffer_uptodate(tmp)) {
11575 fprintf(stderr, "Error reading root block\n");
11578 ret = pin_down_tree_blocks(fs_info, tmp, 0);
11579 free_extent_buffer(tmp);
11583 bytenr = btrfs_node_blockptr(eb, i);
11585 /* If we aren't the tree root don't read the block */
11586 if (level == 1 && !tree_root) {
11587 btrfs_pin_extent(fs_info, bytenr, nodesize);
11591 tmp = read_tree_block(fs_info->extent_root, bytenr,
11593 if (!extent_buffer_uptodate(tmp)) {
11594 fprintf(stderr, "Error reading tree block\n");
11597 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
11598 free_extent_buffer(tmp);
11607 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
11611 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
11615 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
11618 static int reset_block_groups(struct btrfs_fs_info *fs_info)
11620 struct btrfs_block_group_cache *cache;
11621 struct btrfs_path path;
11622 struct extent_buffer *leaf;
11623 struct btrfs_chunk *chunk;
11624 struct btrfs_key key;
11628 btrfs_init_path(&path);
11630 key.type = BTRFS_CHUNK_ITEM_KEY;
11632 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, &path, 0, 0);
11634 btrfs_release_path(&path);
11639 * We do this in case the block groups were screwed up and had alloc
11640 * bits that aren't actually set on the chunks. This happens with
11641 * restored images every time and could happen in real life I guess.
11643 fs_info->avail_data_alloc_bits = 0;
11644 fs_info->avail_metadata_alloc_bits = 0;
11645 fs_info->avail_system_alloc_bits = 0;
11647 /* First we need to create the in-memory block groups */
11649 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11650 ret = btrfs_next_leaf(fs_info->chunk_root, &path);
11652 btrfs_release_path(&path);
11660 leaf = path.nodes[0];
11661 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11662 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
11667 chunk = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_chunk);
11668 btrfs_add_block_group(fs_info, 0,
11669 btrfs_chunk_type(leaf, chunk),
11670 key.objectid, key.offset,
11671 btrfs_chunk_length(leaf, chunk));
11672 set_extent_dirty(&fs_info->free_space_cache, key.offset,
11673 key.offset + btrfs_chunk_length(leaf, chunk));
11678 cache = btrfs_lookup_first_block_group(fs_info, start);
11682 start = cache->key.objectid + cache->key.offset;
11685 btrfs_release_path(&path);
11689 static int reset_balance(struct btrfs_trans_handle *trans,
11690 struct btrfs_fs_info *fs_info)
11692 struct btrfs_root *root = fs_info->tree_root;
11693 struct btrfs_path path;
11694 struct extent_buffer *leaf;
11695 struct btrfs_key key;
11696 int del_slot, del_nr = 0;
11700 btrfs_init_path(&path);
11701 key.objectid = BTRFS_BALANCE_OBJECTID;
11702 key.type = BTRFS_BALANCE_ITEM_KEY;
11704 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11709 goto reinit_data_reloc;
11714 ret = btrfs_del_item(trans, root, &path);
11717 btrfs_release_path(&path);
11719 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
11720 key.type = BTRFS_ROOT_ITEM_KEY;
11722 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11726 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11731 ret = btrfs_del_items(trans, root, &path,
11738 btrfs_release_path(&path);
11741 ret = btrfs_search_slot(trans, root, &key, &path,
11748 leaf = path.nodes[0];
11749 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11750 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
11752 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
11757 del_slot = path.slots[0];
11766 ret = btrfs_del_items(trans, root, &path, del_slot, del_nr);
11770 btrfs_release_path(&path);
11773 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
11774 key.type = BTRFS_ROOT_ITEM_KEY;
11775 key.offset = (u64)-1;
11776 root = btrfs_read_fs_root(fs_info, &key);
11777 if (IS_ERR(root)) {
11778 fprintf(stderr, "Error reading data reloc tree\n");
11779 ret = PTR_ERR(root);
11782 record_root_in_trans(trans, root);
11783 ret = btrfs_fsck_reinit_root(trans, root, 0);
11786 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
11788 btrfs_release_path(&path);
11792 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
11793 struct btrfs_fs_info *fs_info)
11799 * The only reason we don't do this is because right now we're just
11800 * walking the trees we find and pinning down their bytes, we don't look
11801 * at any of the leaves. In order to do mixed groups we'd have to check
11802 * the leaves of any fs roots and pin down the bytes for any file
11803 * extents we find. Not hard but why do it if we don't have to?
11805 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
11806 fprintf(stderr, "We don't support re-initing the extent tree "
11807 "for mixed block groups yet, please notify a btrfs "
11808 "developer you want to do this so they can add this "
11809 "functionality.\n");
11814 * first we need to walk all of the trees except the extent tree and pin
11815 * down the bytes that are in use so we don't overwrite any existing
11818 ret = pin_metadata_blocks(fs_info);
11820 fprintf(stderr, "error pinning down used bytes\n");
11825 * Need to drop all the block groups since we're going to recreate all
11828 btrfs_free_block_groups(fs_info);
11829 ret = reset_block_groups(fs_info);
11831 fprintf(stderr, "error resetting the block groups\n");
11835 /* Ok we can allocate now, reinit the extent root */
11836 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
11838 fprintf(stderr, "extent root initialization failed\n");
11840 * When the transaction code is updated we should end the
11841 * transaction, but for now progs only knows about commit so
11842 * just return an error.
11848 * Now we have all the in-memory block groups setup so we can make
11849 * allocations properly, and the metadata we care about is safe since we
11850 * pinned all of it above.
11853 struct btrfs_block_group_cache *cache;
11855 cache = btrfs_lookup_first_block_group(fs_info, start);
11858 start = cache->key.objectid + cache->key.offset;
11859 ret = btrfs_insert_item(trans, fs_info->extent_root,
11860 &cache->key, &cache->item,
11861 sizeof(cache->item));
11863 fprintf(stderr, "Error adding block group\n");
11866 btrfs_extent_post_op(trans, fs_info->extent_root);
11869 ret = reset_balance(trans, fs_info);
11871 fprintf(stderr, "error resetting the pending balance\n");
11876 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
11878 struct btrfs_path path;
11879 struct btrfs_trans_handle *trans;
11880 struct btrfs_key key;
11883 printf("Recowing metadata block %llu\n", eb->start);
11884 key.objectid = btrfs_header_owner(eb);
11885 key.type = BTRFS_ROOT_ITEM_KEY;
11886 key.offset = (u64)-1;
11888 root = btrfs_read_fs_root(root->fs_info, &key);
11889 if (IS_ERR(root)) {
11890 fprintf(stderr, "Couldn't find owner root %llu\n",
11892 return PTR_ERR(root);
11895 trans = btrfs_start_transaction(root, 1);
11897 return PTR_ERR(trans);
11899 btrfs_init_path(&path);
11900 path.lowest_level = btrfs_header_level(eb);
11901 if (path.lowest_level)
11902 btrfs_node_key_to_cpu(eb, &key, 0);
11904 btrfs_item_key_to_cpu(eb, &key, 0);
11906 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
11907 btrfs_commit_transaction(trans, root);
11908 btrfs_release_path(&path);
11912 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
11914 struct btrfs_path path;
11915 struct btrfs_trans_handle *trans;
11916 struct btrfs_key key;
11919 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
11920 bad->key.type, bad->key.offset);
11921 key.objectid = bad->root_id;
11922 key.type = BTRFS_ROOT_ITEM_KEY;
11923 key.offset = (u64)-1;
11925 root = btrfs_read_fs_root(root->fs_info, &key);
11926 if (IS_ERR(root)) {
11927 fprintf(stderr, "Couldn't find owner root %llu\n",
11929 return PTR_ERR(root);
11932 trans = btrfs_start_transaction(root, 1);
11934 return PTR_ERR(trans);
11936 btrfs_init_path(&path);
11937 ret = btrfs_search_slot(trans, root, &bad->key, &path, -1, 1);
11943 ret = btrfs_del_item(trans, root, &path);
11945 btrfs_commit_transaction(trans, root);
11946 btrfs_release_path(&path);
11950 static int zero_log_tree(struct btrfs_root *root)
11952 struct btrfs_trans_handle *trans;
11955 trans = btrfs_start_transaction(root, 1);
11956 if (IS_ERR(trans)) {
11957 ret = PTR_ERR(trans);
11960 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
11961 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
11962 ret = btrfs_commit_transaction(trans, root);
11966 static int populate_csum(struct btrfs_trans_handle *trans,
11967 struct btrfs_root *csum_root, char *buf, u64 start,
11974 while (offset < len) {
11975 sectorsize = csum_root->sectorsize;
11976 ret = read_extent_data(csum_root, buf, start + offset,
11980 ret = btrfs_csum_file_block(trans, csum_root, start + len,
11981 start + offset, buf, sectorsize);
11984 offset += sectorsize;
11989 static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
11990 struct btrfs_root *csum_root,
11991 struct btrfs_root *cur_root)
11993 struct btrfs_path path;
11994 struct btrfs_key key;
11995 struct extent_buffer *node;
11996 struct btrfs_file_extent_item *fi;
12003 buf = malloc(cur_root->fs_info->csum_root->sectorsize);
12007 btrfs_init_path(&path);
12011 ret = btrfs_search_slot(NULL, cur_root, &key, &path, 0, 0);
12014 /* Iterate all regular file extents and fill its csum */
12016 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
12018 if (key.type != BTRFS_EXTENT_DATA_KEY)
12020 node = path.nodes[0];
12021 slot = path.slots[0];
12022 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
12023 if (btrfs_file_extent_type(node, fi) != BTRFS_FILE_EXTENT_REG)
12025 start = btrfs_file_extent_disk_bytenr(node, fi);
12026 len = btrfs_file_extent_disk_num_bytes(node, fi);
12028 ret = populate_csum(trans, csum_root, buf, start, len);
12029 if (ret == -EEXIST)
12035 * TODO: if next leaf is corrupted, jump to nearest next valid
12038 ret = btrfs_next_item(cur_root, &path);
12048 btrfs_release_path(&path);
12053 static int fill_csum_tree_from_fs(struct btrfs_trans_handle *trans,
12054 struct btrfs_root *csum_root)
12056 struct btrfs_fs_info *fs_info = csum_root->fs_info;
12057 struct btrfs_path path;
12058 struct btrfs_root *tree_root = fs_info->tree_root;
12059 struct btrfs_root *cur_root;
12060 struct extent_buffer *node;
12061 struct btrfs_key key;
12065 btrfs_init_path(&path);
12066 key.objectid = BTRFS_FS_TREE_OBJECTID;
12068 key.type = BTRFS_ROOT_ITEM_KEY;
12069 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
12078 node = path.nodes[0];
12079 slot = path.slots[0];
12080 btrfs_item_key_to_cpu(node, &key, slot);
12081 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
12083 if (key.type != BTRFS_ROOT_ITEM_KEY)
12085 if (!is_fstree(key.objectid))
12087 key.offset = (u64)-1;
12089 cur_root = btrfs_read_fs_root(fs_info, &key);
12090 if (IS_ERR(cur_root) || !cur_root) {
12091 fprintf(stderr, "Fail to read fs/subvol tree: %lld\n",
12095 ret = fill_csum_tree_from_one_fs_root(trans, csum_root,
12100 ret = btrfs_next_item(tree_root, &path);
12110 btrfs_release_path(&path);
12114 static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans,
12115 struct btrfs_root *csum_root)
12117 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
12118 struct btrfs_path path;
12119 struct btrfs_extent_item *ei;
12120 struct extent_buffer *leaf;
12122 struct btrfs_key key;
12125 btrfs_init_path(&path);
12127 key.type = BTRFS_EXTENT_ITEM_KEY;
12129 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
12131 btrfs_release_path(&path);
12135 buf = malloc(csum_root->sectorsize);
12137 btrfs_release_path(&path);
12142 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
12143 ret = btrfs_next_leaf(extent_root, &path);
12151 leaf = path.nodes[0];
12153 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
12154 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
12159 ei = btrfs_item_ptr(leaf, path.slots[0],
12160 struct btrfs_extent_item);
12161 if (!(btrfs_extent_flags(leaf, ei) &
12162 BTRFS_EXTENT_FLAG_DATA)) {
12167 ret = populate_csum(trans, csum_root, buf, key.objectid,
12174 btrfs_release_path(&path);
12180 * Recalculate the csum and put it into the csum tree.
12182 * Extent tree init will wipe out all the extent info, so in that case, we
12183 * can't depend on extent tree, but use fs tree. If search_fs_tree is set, we
12184 * will use fs/subvol trees to init the csum tree.
12186 static int fill_csum_tree(struct btrfs_trans_handle *trans,
12187 struct btrfs_root *csum_root,
12188 int search_fs_tree)
12190 if (search_fs_tree)
12191 return fill_csum_tree_from_fs(trans, csum_root);
12193 return fill_csum_tree_from_extent(trans, csum_root);
12196 static void free_roots_info_cache(void)
12198 if (!roots_info_cache)
12201 while (!cache_tree_empty(roots_info_cache)) {
12202 struct cache_extent *entry;
12203 struct root_item_info *rii;
12205 entry = first_cache_extent(roots_info_cache);
12208 remove_cache_extent(roots_info_cache, entry);
12209 rii = container_of(entry, struct root_item_info, cache_extent);
12213 free(roots_info_cache);
12214 roots_info_cache = NULL;
12217 static int build_roots_info_cache(struct btrfs_fs_info *info)
12220 struct btrfs_key key;
12221 struct extent_buffer *leaf;
12222 struct btrfs_path path;
12224 if (!roots_info_cache) {
12225 roots_info_cache = malloc(sizeof(*roots_info_cache));
12226 if (!roots_info_cache)
12228 cache_tree_init(roots_info_cache);
12231 btrfs_init_path(&path);
12233 key.type = BTRFS_EXTENT_ITEM_KEY;
12235 ret = btrfs_search_slot(NULL, info->extent_root, &key, &path, 0, 0);
12238 leaf = path.nodes[0];
12241 struct btrfs_key found_key;
12242 struct btrfs_extent_item *ei;
12243 struct btrfs_extent_inline_ref *iref;
12244 int slot = path.slots[0];
12249 struct cache_extent *entry;
12250 struct root_item_info *rii;
12252 if (slot >= btrfs_header_nritems(leaf)) {
12253 ret = btrfs_next_leaf(info->extent_root, &path);
12260 leaf = path.nodes[0];
12261 slot = path.slots[0];
12264 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12266 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
12267 found_key.type != BTRFS_METADATA_ITEM_KEY)
12270 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
12271 flags = btrfs_extent_flags(leaf, ei);
12273 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
12274 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
12277 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
12278 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
12279 level = found_key.offset;
12281 struct btrfs_tree_block_info *binfo;
12283 binfo = (struct btrfs_tree_block_info *)(ei + 1);
12284 iref = (struct btrfs_extent_inline_ref *)(binfo + 1);
12285 level = btrfs_tree_block_level(leaf, binfo);
12289 * For a root extent, it must be of the following type and the
12290 * first (and only one) iref in the item.
12292 type = btrfs_extent_inline_ref_type(leaf, iref);
12293 if (type != BTRFS_TREE_BLOCK_REF_KEY)
12296 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
12297 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12299 rii = malloc(sizeof(struct root_item_info));
12304 rii->cache_extent.start = root_id;
12305 rii->cache_extent.size = 1;
12306 rii->level = (u8)-1;
12307 entry = &rii->cache_extent;
12308 ret = insert_cache_extent(roots_info_cache, entry);
12311 rii = container_of(entry, struct root_item_info,
12315 ASSERT(rii->cache_extent.start == root_id);
12316 ASSERT(rii->cache_extent.size == 1);
12318 if (level > rii->level || rii->level == (u8)-1) {
12319 rii->level = level;
12320 rii->bytenr = found_key.objectid;
12321 rii->gen = btrfs_extent_generation(leaf, ei);
12322 rii->node_count = 1;
12323 } else if (level == rii->level) {
12331 btrfs_release_path(&path);
12336 static int maybe_repair_root_item(struct btrfs_fs_info *info,
12337 struct btrfs_path *path,
12338 const struct btrfs_key *root_key,
12339 const int read_only_mode)
12341 const u64 root_id = root_key->objectid;
12342 struct cache_extent *entry;
12343 struct root_item_info *rii;
12344 struct btrfs_root_item ri;
12345 unsigned long offset;
12347 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12350 "Error: could not find extent items for root %llu\n",
12351 root_key->objectid);
12355 rii = container_of(entry, struct root_item_info, cache_extent);
12356 ASSERT(rii->cache_extent.start == root_id);
12357 ASSERT(rii->cache_extent.size == 1);
12359 if (rii->node_count != 1) {
12361 "Error: could not find btree root extent for root %llu\n",
12366 offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
12367 read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
12369 if (btrfs_root_bytenr(&ri) != rii->bytenr ||
12370 btrfs_root_level(&ri) != rii->level ||
12371 btrfs_root_generation(&ri) != rii->gen) {
12374 * If we're in repair mode but our caller told us to not update
12375 * the root item, i.e. just check if it needs to be updated, don't
12376 * print this message, since the caller will call us again shortly
12377 * for the same root item without read only mode (the caller will
12378 * open a transaction first).
12380 if (!(read_only_mode && repair))
12382 "%sroot item for root %llu,"
12383 " current bytenr %llu, current gen %llu, current level %u,"
12384 " new bytenr %llu, new gen %llu, new level %u\n",
12385 (read_only_mode ? "" : "fixing "),
12387 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
12388 btrfs_root_level(&ri),
12389 rii->bytenr, rii->gen, rii->level);
12391 if (btrfs_root_generation(&ri) > rii->gen) {
12393 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
12394 root_id, btrfs_root_generation(&ri), rii->gen);
12398 if (!read_only_mode) {
12399 btrfs_set_root_bytenr(&ri, rii->bytenr);
12400 btrfs_set_root_level(&ri, rii->level);
12401 btrfs_set_root_generation(&ri, rii->gen);
12402 write_extent_buffer(path->nodes[0], &ri,
12403 offset, sizeof(ri));
12413 * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
12414 * caused read-only snapshots to be corrupted if they were created at a moment
12415 * when the source subvolume/snapshot had orphan items. The issue was that the
12416 * on-disk root items became incorrect, referring to the pre orphan cleanup root
12417 * node instead of the post orphan cleanup root node.
12418 * So this function, and its callees, just detects and fixes those cases. Even
12419 * though the regression was for read-only snapshots, this function applies to
12420 * any snapshot/subvolume root.
12421 * This must be run before any other repair code - not doing it so, makes other
12422 * repair code delete or modify backrefs in the extent tree for example, which
12423 * will result in an inconsistent fs after repairing the root items.
12425 static int repair_root_items(struct btrfs_fs_info *info)
12427 struct btrfs_path path;
12428 struct btrfs_key key;
12429 struct extent_buffer *leaf;
12430 struct btrfs_trans_handle *trans = NULL;
12433 int need_trans = 0;
12435 btrfs_init_path(&path);
12437 ret = build_roots_info_cache(info);
12441 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
12442 key.type = BTRFS_ROOT_ITEM_KEY;
12447 * Avoid opening and committing transactions if a leaf doesn't have
12448 * any root items that need to be fixed, so that we avoid rotating
12449 * backup roots unnecessarily.
12452 trans = btrfs_start_transaction(info->tree_root, 1);
12453 if (IS_ERR(trans)) {
12454 ret = PTR_ERR(trans);
12459 ret = btrfs_search_slot(trans, info->tree_root, &key, &path,
12463 leaf = path.nodes[0];
12466 struct btrfs_key found_key;
12468 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
12469 int no_more_keys = find_next_key(&path, &key);
12471 btrfs_release_path(&path);
12473 ret = btrfs_commit_transaction(trans,
12485 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12487 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
12489 if (found_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
12492 ret = maybe_repair_root_item(info, &path, &found_key,
12497 if (!trans && repair) {
12500 btrfs_release_path(&path);
12510 free_roots_info_cache();
12511 btrfs_release_path(&path);
12513 btrfs_commit_transaction(trans, info->tree_root);
12520 static int clear_free_space_cache(struct btrfs_fs_info *fs_info)
12522 struct btrfs_trans_handle *trans;
12523 struct btrfs_block_group_cache *bg_cache;
12527 /* Clear all free space cache inodes and its extent data */
12529 bg_cache = btrfs_lookup_first_block_group(fs_info, current);
12532 ret = btrfs_clear_free_space_cache(fs_info, bg_cache);
12535 current = bg_cache->key.objectid + bg_cache->key.offset;
12538 /* Don't forget to set cache_generation to -1 */
12539 trans = btrfs_start_transaction(fs_info->tree_root, 0);
12540 if (IS_ERR(trans)) {
12541 error("failed to update super block cache generation");
12542 return PTR_ERR(trans);
12544 btrfs_set_super_cache_generation(fs_info->super_copy, (u64)-1);
12545 btrfs_commit_transaction(trans, fs_info->tree_root);
12550 const char * const cmd_check_usage[] = {
12551 "btrfs check [options] <device>",
12552 "Check structural integrity of a filesystem (unmounted).",
12553 "Check structural integrity of an unmounted filesystem. Verify internal",
12554 "trees' consistency and item connectivity. In the repair mode try to",
12555 "fix the problems found. ",
12556 "WARNING: the repair mode is considered dangerous",
12558 "-s|--super <superblock> use this superblock copy",
12559 "-b|--backup use the first valid backup root copy",
12560 "--repair try to repair the filesystem",
12561 "--readonly run in read-only mode (default)",
12562 "--init-csum-tree create a new CRC tree",
12563 "--init-extent-tree create a new extent tree",
12564 "--mode <MODE> allows choice of memory/IO trade-offs",
12565 " where MODE is one of:",
12566 " original - read inodes and extents to memory (requires",
12567 " more memory, does less IO)",
12568 " lowmem - try to use less memory but read blocks again",
12570 "--check-data-csum verify checksums of data blocks",
12571 "-Q|--qgroup-report print a report on qgroup consistency",
12572 "-E|--subvol-extents <subvolid>",
12573 " print subvolume extents and sharing state",
12574 "-r|--tree-root <bytenr> use the given bytenr for the tree root",
12575 "--chunk-root <bytenr> use the given bytenr for the chunk tree root",
12576 "-p|--progress indicate progress",
12577 "--clear-space-cache v1|v2 clear space cache for v1 or v2",
12581 int cmd_check(int argc, char **argv)
12583 struct cache_tree root_cache;
12584 struct btrfs_root *root;
12585 struct btrfs_fs_info *info;
12588 u64 tree_root_bytenr = 0;
12589 u64 chunk_root_bytenr = 0;
12590 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
12594 int init_csum_tree = 0;
12596 int clear_space_cache = 0;
12597 int qgroup_report = 0;
12598 int qgroups_repaired = 0;
12599 unsigned ctree_flags = OPEN_CTREE_EXCLUSIVE;
12603 enum { GETOPT_VAL_REPAIR = 257, GETOPT_VAL_INIT_CSUM,
12604 GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
12605 GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE,
12606 GETOPT_VAL_MODE, GETOPT_VAL_CLEAR_SPACE_CACHE };
12607 static const struct option long_options[] = {
12608 { "super", required_argument, NULL, 's' },
12609 { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
12610 { "readonly", no_argument, NULL, GETOPT_VAL_READONLY },
12611 { "init-csum-tree", no_argument, NULL,
12612 GETOPT_VAL_INIT_CSUM },
12613 { "init-extent-tree", no_argument, NULL,
12614 GETOPT_VAL_INIT_EXTENT },
12615 { "check-data-csum", no_argument, NULL,
12616 GETOPT_VAL_CHECK_CSUM },
12617 { "backup", no_argument, NULL, 'b' },
12618 { "subvol-extents", required_argument, NULL, 'E' },
12619 { "qgroup-report", no_argument, NULL, 'Q' },
12620 { "tree-root", required_argument, NULL, 'r' },
12621 { "chunk-root", required_argument, NULL,
12622 GETOPT_VAL_CHUNK_TREE },
12623 { "progress", no_argument, NULL, 'p' },
12624 { "mode", required_argument, NULL,
12626 { "clear-space-cache", required_argument, NULL,
12627 GETOPT_VAL_CLEAR_SPACE_CACHE},
12628 { NULL, 0, NULL, 0}
12631 c = getopt_long(argc, argv, "as:br:p", long_options, NULL);
12635 case 'a': /* ignored */ break;
12637 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
12640 num = arg_strtou64(optarg);
12641 if (num >= BTRFS_SUPER_MIRROR_MAX) {
12643 "super mirror should be less than %d",
12644 BTRFS_SUPER_MIRROR_MAX);
12647 bytenr = btrfs_sb_offset(((int)num));
12648 printf("using SB copy %llu, bytenr %llu\n", num,
12649 (unsigned long long)bytenr);
12655 subvolid = arg_strtou64(optarg);
12658 tree_root_bytenr = arg_strtou64(optarg);
12660 case GETOPT_VAL_CHUNK_TREE:
12661 chunk_root_bytenr = arg_strtou64(optarg);
12664 ctx.progress_enabled = true;
12668 usage(cmd_check_usage);
12669 case GETOPT_VAL_REPAIR:
12670 printf("enabling repair mode\n");
12672 ctree_flags |= OPEN_CTREE_WRITES;
12674 case GETOPT_VAL_READONLY:
12677 case GETOPT_VAL_INIT_CSUM:
12678 printf("Creating a new CRC tree\n");
12679 init_csum_tree = 1;
12681 ctree_flags |= OPEN_CTREE_WRITES;
12683 case GETOPT_VAL_INIT_EXTENT:
12684 init_extent_tree = 1;
12685 ctree_flags |= (OPEN_CTREE_WRITES |
12686 OPEN_CTREE_NO_BLOCK_GROUPS);
12689 case GETOPT_VAL_CHECK_CSUM:
12690 check_data_csum = 1;
12692 case GETOPT_VAL_MODE:
12693 check_mode = parse_check_mode(optarg);
12694 if (check_mode == CHECK_MODE_UNKNOWN) {
12695 error("unknown mode: %s", optarg);
12699 case GETOPT_VAL_CLEAR_SPACE_CACHE:
12700 if (strcmp(optarg, "v1") == 0) {
12701 clear_space_cache = 1;
12702 } else if (strcmp(optarg, "v2") == 0) {
12703 clear_space_cache = 2;
12704 ctree_flags |= OPEN_CTREE_INVALIDATE_FST;
12707 "invalid argument to --clear-space-cache, must be v1 or v2");
12710 ctree_flags |= OPEN_CTREE_WRITES;
12715 if (check_argc_exact(argc - optind, 1))
12716 usage(cmd_check_usage);
12718 if (ctx.progress_enabled) {
12719 ctx.tp = TASK_NOTHING;
12720 ctx.info = task_init(print_status_check, print_status_return, &ctx);
12723 /* This check is the only reason for --readonly to exist */
12724 if (readonly && repair) {
12725 error("repair options are not compatible with --readonly");
12730 * Not supported yet
12732 if (repair && check_mode == CHECK_MODE_LOWMEM) {
12733 error("low memory mode doesn't support repair yet");
12738 cache_tree_init(&root_cache);
12740 if((ret = check_mounted(argv[optind])) < 0) {
12741 error("could not check mount status: %s", strerror(-ret));
12745 error("%s is currently mounted, aborting", argv[optind]);
12751 /* only allow partial opening under repair mode */
12753 ctree_flags |= OPEN_CTREE_PARTIAL;
12755 info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
12756 chunk_root_bytenr, ctree_flags);
12758 error("cannot open file system");
12764 global_info = info;
12765 root = info->fs_root;
12766 if (clear_space_cache == 1) {
12767 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) {
12769 "free space cache v2 detected, use --clear-space-cache v2");
12773 printf("Clearing free space cache\n");
12774 ret = clear_free_space_cache(info);
12776 error("failed to clear free space cache");
12779 printf("Free space cache cleared\n");
12782 } else if (clear_space_cache == 2) {
12783 if (!btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) {
12784 printf("no free space cache v2 to clear\n");
12788 printf("Clear free space cache v2\n");
12789 ret = btrfs_clear_free_space_tree(info);
12791 error("failed to clear free space cache v2: %d", ret);
12794 printf("free space cache v2 cleared\n");
12800 * repair mode will force us to commit transaction which
12801 * will make us fail to load log tree when mounting.
12803 if (repair && btrfs_super_log_root(info->super_copy)) {
12804 ret = ask_user("repair mode will force to clear out log tree, are you sure?");
12810 ret = zero_log_tree(root);
12813 error("failed to zero log tree: %d", ret);
12818 uuid_unparse(info->super_copy->fsid, uuidbuf);
12819 if (qgroup_report) {
12820 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
12822 ret = qgroup_verify_all(info);
12829 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
12830 subvolid, argv[optind], uuidbuf);
12831 ret = print_extent_state(info, subvolid);
12835 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
12837 if (!extent_buffer_uptodate(info->tree_root->node) ||
12838 !extent_buffer_uptodate(info->dev_root->node) ||
12839 !extent_buffer_uptodate(info->chunk_root->node)) {
12840 error("critical roots corrupted, unable to check the filesystem");
12846 if (init_extent_tree || init_csum_tree) {
12847 struct btrfs_trans_handle *trans;
12849 trans = btrfs_start_transaction(info->extent_root, 0);
12850 if (IS_ERR(trans)) {
12851 error("error starting transaction");
12852 ret = PTR_ERR(trans);
12857 if (init_extent_tree) {
12858 printf("Creating a new extent tree\n");
12859 ret = reinit_extent_tree(trans, info);
12865 if (init_csum_tree) {
12866 printf("Reinitialize checksum tree\n");
12867 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
12869 error("checksum tree initialization failed: %d",
12876 ret = fill_csum_tree(trans, info->csum_root,
12880 error("checksum tree refilling failed: %d", ret);
12885 * Ok now we commit and run the normal fsck, which will add
12886 * extent entries for all of the items it finds.
12888 ret = btrfs_commit_transaction(trans, info->extent_root);
12893 if (!extent_buffer_uptodate(info->extent_root->node)) {
12894 error("critical: extent_root, unable to check the filesystem");
12899 if (!extent_buffer_uptodate(info->csum_root->node)) {
12900 error("critical: csum_root, unable to check the filesystem");
12906 if (!ctx.progress_enabled)
12907 fprintf(stderr, "checking extents\n");
12908 if (check_mode == CHECK_MODE_LOWMEM)
12909 ret = check_chunks_and_extents_v2(root);
12911 ret = check_chunks_and_extents(root);
12915 "errors found in extent allocation tree or chunk allocation");
12917 ret = repair_root_items(info);
12922 fprintf(stderr, "Fixed %d roots.\n", ret);
12924 } else if (ret > 0) {
12926 "Found %d roots with an outdated root item.\n",
12929 "Please run a filesystem check with the option --repair to fix them.\n");
12935 if (!ctx.progress_enabled) {
12936 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
12937 fprintf(stderr, "checking free space tree\n");
12939 fprintf(stderr, "checking free space cache\n");
12941 ret = check_space_cache(root);
12947 * We used to have to have these hole extents in between our real
12948 * extents so if we don't have this flag set we need to make sure there
12949 * are no gaps in the file extents for inodes, otherwise we can just
12950 * ignore it when this happens.
12952 no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
12953 if (!ctx.progress_enabled)
12954 fprintf(stderr, "checking fs roots\n");
12955 if (check_mode == CHECK_MODE_LOWMEM)
12956 ret = check_fs_roots_v2(root->fs_info);
12958 ret = check_fs_roots(root, &root_cache);
12963 fprintf(stderr, "checking csums\n");
12964 ret = check_csums(root);
12969 fprintf(stderr, "checking root refs\n");
12970 /* For low memory mode, check_fs_roots_v2 handles root refs */
12971 if (check_mode != CHECK_MODE_LOWMEM) {
12972 ret = check_root_refs(root, &root_cache);
12978 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
12979 struct extent_buffer *eb;
12981 eb = list_first_entry(&root->fs_info->recow_ebs,
12982 struct extent_buffer, recow);
12983 list_del_init(&eb->recow);
12984 ret = recow_extent_buffer(root, eb);
12990 while (!list_empty(&delete_items)) {
12991 struct bad_item *bad;
12993 bad = list_first_entry(&delete_items, struct bad_item, list);
12994 list_del_init(&bad->list);
12996 ret = delete_bad_item(root, bad);
13002 if (info->quota_enabled) {
13003 fprintf(stderr, "checking quota groups\n");
13004 ret = qgroup_verify_all(info);
13009 ret = repair_qgroups(info, &qgroups_repaired);
13016 if (!list_empty(&root->fs_info->recow_ebs)) {
13017 error("transid errors in file system");
13022 if (found_old_backref) { /*
13023 * there was a disk format change when mixed
13024 * backref was in testing tree. The old format
13025 * existed about one week.
13027 printf("\n * Found old mixed backref format. "
13028 "The old format is not supported! *"
13029 "\n * Please mount the FS in readonly mode, "
13030 "backup data and re-format the FS. *\n\n");
13033 printf("found %llu bytes used err is %d\n",
13034 (unsigned long long)bytes_used, ret);
13035 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
13036 printf("total tree bytes: %llu\n",
13037 (unsigned long long)total_btree_bytes);
13038 printf("total fs tree bytes: %llu\n",
13039 (unsigned long long)total_fs_tree_bytes);
13040 printf("total extent tree bytes: %llu\n",
13041 (unsigned long long)total_extent_tree_bytes);
13042 printf("btree space waste bytes: %llu\n",
13043 (unsigned long long)btree_space_waste);
13044 printf("file data blocks allocated: %llu\n referenced %llu\n",
13045 (unsigned long long)data_bytes_allocated,
13046 (unsigned long long)data_bytes_referenced);
13048 free_qgroup_counts();
13049 free_root_recs_tree(&root_cache);
13053 if (ctx.progress_enabled)
13054 task_deinit(ctx.info);