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 btrfs_root *root,
1987 struct extent_buffer *parent, int slot,
1988 struct extent_buffer *child)
1990 struct btrfs_key parent_key;
1991 struct btrfs_key child_key;
1994 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1995 if (btrfs_header_level(child) == 0)
1996 btrfs_item_key_to_cpu(child, &child_key, 0);
1998 btrfs_node_key_to_cpu(child, &child_key, 0);
2000 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
2003 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
2004 parent_key.objectid, parent_key.type, parent_key.offset,
2005 child_key.objectid, child_key.type, child_key.offset);
2007 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
2009 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
2010 btrfs_node_blockptr(parent, slot),
2011 btrfs_header_bytenr(child));
2013 if (btrfs_node_ptr_generation(parent, slot) !=
2014 btrfs_header_generation(child)) {
2016 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
2017 btrfs_header_generation(child),
2018 btrfs_node_ptr_generation(parent, slot));
2024 * for a tree node or leaf, if it's shared, indeed we don't need to iterate it
2025 * in every fs or file tree check. Here we find its all root ids, and only check
2026 * it in the fs or file tree which has the smallest root id.
2028 static int need_check(struct btrfs_root *root, struct ulist *roots)
2030 struct rb_node *node;
2031 struct ulist_node *u;
2033 if (roots->nnodes == 1)
2036 node = rb_first(&roots->root);
2037 u = rb_entry(node, struct ulist_node, rb_node);
2039 * current root id is not smallest, we skip it and let it be checked
2040 * in the fs or file tree who hash the smallest root id.
2042 if (root->objectid != u->val)
2049 * for a tree node or leaf, we record its reference count, so later if we still
2050 * process this node or leaf, don't need to compute its reference count again.
2052 static int update_nodes_refs(struct btrfs_root *root, u64 bytenr,
2053 struct node_refs *nrefs, u64 level)
2057 struct ulist *roots;
2059 if (nrefs->bytenr[level] != bytenr) {
2060 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2061 level, 1, &refs, NULL);
2065 nrefs->bytenr[level] = bytenr;
2066 nrefs->refs[level] = refs;
2068 ret = btrfs_find_all_roots(NULL, root->fs_info, bytenr,
2073 check = need_check(root, roots);
2075 nrefs->need_check[level] = check;
2077 nrefs->need_check[level] = 1;
2084 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
2085 struct walk_control *wc, int *level,
2086 struct node_refs *nrefs)
2088 enum btrfs_tree_block_status status;
2091 struct extent_buffer *next;
2092 struct extent_buffer *cur;
2097 WARN_ON(*level < 0);
2098 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2100 if (path->nodes[*level]->start == nrefs->bytenr[*level]) {
2101 refs = nrefs->refs[*level];
2104 ret = btrfs_lookup_extent_info(NULL, root,
2105 path->nodes[*level]->start,
2106 *level, 1, &refs, NULL);
2111 nrefs->bytenr[*level] = path->nodes[*level]->start;
2112 nrefs->refs[*level] = refs;
2116 ret = enter_shared_node(root, path->nodes[*level]->start,
2124 while (*level >= 0) {
2125 WARN_ON(*level < 0);
2126 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2127 cur = path->nodes[*level];
2129 if (btrfs_header_level(cur) != *level)
2132 if (path->slots[*level] >= btrfs_header_nritems(cur))
2135 ret = process_one_leaf(root, cur, wc);
2140 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2141 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2142 blocksize = root->nodesize;
2144 if (bytenr == nrefs->bytenr[*level - 1]) {
2145 refs = nrefs->refs[*level - 1];
2147 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
2148 *level - 1, 1, &refs, NULL);
2152 nrefs->bytenr[*level - 1] = bytenr;
2153 nrefs->refs[*level - 1] = refs;
2158 ret = enter_shared_node(root, bytenr, refs,
2161 path->slots[*level]++;
2166 next = btrfs_find_tree_block(root, bytenr, blocksize);
2167 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2168 free_extent_buffer(next);
2169 reada_walk_down(root, cur, path->slots[*level]);
2170 next = read_tree_block(root, bytenr, blocksize,
2172 if (!extent_buffer_uptodate(next)) {
2173 struct btrfs_key node_key;
2175 btrfs_node_key_to_cpu(path->nodes[*level],
2177 path->slots[*level]);
2178 btrfs_add_corrupt_extent_record(root->fs_info,
2180 path->nodes[*level]->start,
2181 root->nodesize, *level);
2187 ret = check_child_node(root, cur, path->slots[*level], next);
2193 if (btrfs_is_leaf(next))
2194 status = btrfs_check_leaf(root, NULL, next);
2196 status = btrfs_check_node(root, NULL, next);
2197 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2198 free_extent_buffer(next);
2203 *level = *level - 1;
2204 free_extent_buffer(path->nodes[*level]);
2205 path->nodes[*level] = next;
2206 path->slots[*level] = 0;
2209 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
2213 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
2214 unsigned int ext_ref);
2216 static int walk_down_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2217 int *level, struct node_refs *nrefs, int ext_ref)
2219 enum btrfs_tree_block_status status;
2222 struct extent_buffer *next;
2223 struct extent_buffer *cur;
2227 WARN_ON(*level < 0);
2228 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2230 ret = update_nodes_refs(root, path->nodes[*level]->start,
2235 while (*level >= 0) {
2236 WARN_ON(*level < 0);
2237 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2238 cur = path->nodes[*level];
2240 if (btrfs_header_level(cur) != *level)
2243 if (path->slots[*level] >= btrfs_header_nritems(cur))
2245 /* Don't forgot to check leaf/node validation */
2247 ret = btrfs_check_leaf(root, NULL, cur);
2248 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2252 ret = process_one_leaf_v2(root, path, nrefs,
2256 ret = btrfs_check_node(root, NULL, cur);
2257 if (ret != BTRFS_TREE_BLOCK_CLEAN) {
2262 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2263 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2264 blocksize = root->nodesize;
2266 ret = update_nodes_refs(root, bytenr, nrefs, *level - 1);
2269 if (!nrefs->need_check[*level - 1]) {
2270 path->slots[*level]++;
2274 next = btrfs_find_tree_block(root, bytenr, blocksize);
2275 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2276 free_extent_buffer(next);
2277 reada_walk_down(root, cur, path->slots[*level]);
2278 next = read_tree_block(root, bytenr, blocksize,
2280 if (!extent_buffer_uptodate(next)) {
2281 struct btrfs_key node_key;
2283 btrfs_node_key_to_cpu(path->nodes[*level],
2285 path->slots[*level]);
2286 btrfs_add_corrupt_extent_record(root->fs_info,
2288 path->nodes[*level]->start,
2289 root->nodesize, *level);
2295 ret = check_child_node(root, cur, path->slots[*level], next);
2299 if (btrfs_is_leaf(next))
2300 status = btrfs_check_leaf(root, NULL, next);
2302 status = btrfs_check_node(root, NULL, next);
2303 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2304 free_extent_buffer(next);
2309 *level = *level - 1;
2310 free_extent_buffer(path->nodes[*level]);
2311 path->nodes[*level] = next;
2312 path->slots[*level] = 0;
2317 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
2318 struct walk_control *wc, int *level)
2321 struct extent_buffer *leaf;
2323 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2324 leaf = path->nodes[i];
2325 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2330 free_extent_buffer(path->nodes[*level]);
2331 path->nodes[*level] = NULL;
2332 BUG_ON(*level > wc->active_node);
2333 if (*level == wc->active_node)
2334 leave_shared_node(root, wc, *level);
2341 static int walk_up_tree_v2(struct btrfs_root *root, struct btrfs_path *path,
2345 struct extent_buffer *leaf;
2347 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2348 leaf = path->nodes[i];
2349 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
2354 free_extent_buffer(path->nodes[*level]);
2355 path->nodes[*level] = NULL;
2362 static int check_root_dir(struct inode_record *rec)
2364 struct inode_backref *backref;
2367 if (!rec->found_inode_item || rec->errors)
2369 if (rec->nlink != 1 || rec->found_link != 0)
2371 if (list_empty(&rec->backrefs))
2373 backref = to_inode_backref(rec->backrefs.next);
2374 if (!backref->found_inode_ref)
2376 if (backref->index != 0 || backref->namelen != 2 ||
2377 memcmp(backref->name, "..", 2))
2379 if (backref->found_dir_index || backref->found_dir_item)
2386 static int repair_inode_isize(struct btrfs_trans_handle *trans,
2387 struct btrfs_root *root, struct btrfs_path *path,
2388 struct inode_record *rec)
2390 struct btrfs_inode_item *ei;
2391 struct btrfs_key key;
2394 key.objectid = rec->ino;
2395 key.type = BTRFS_INODE_ITEM_KEY;
2396 key.offset = (u64)-1;
2398 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2402 if (!path->slots[0]) {
2409 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2410 if (key.objectid != rec->ino) {
2415 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2416 struct btrfs_inode_item);
2417 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
2418 btrfs_mark_buffer_dirty(path->nodes[0]);
2419 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2420 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
2421 root->root_key.objectid);
2423 btrfs_release_path(path);
2427 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
2428 struct btrfs_root *root,
2429 struct btrfs_path *path,
2430 struct inode_record *rec)
2434 ret = btrfs_add_orphan_item(trans, root, path, rec->ino);
2435 btrfs_release_path(path);
2437 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
2441 static int repair_inode_nbytes(struct btrfs_trans_handle *trans,
2442 struct btrfs_root *root,
2443 struct btrfs_path *path,
2444 struct inode_record *rec)
2446 struct btrfs_inode_item *ei;
2447 struct btrfs_key key;
2450 key.objectid = rec->ino;
2451 key.type = BTRFS_INODE_ITEM_KEY;
2454 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2461 /* Since ret == 0, no need to check anything */
2462 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
2463 struct btrfs_inode_item);
2464 btrfs_set_inode_nbytes(path->nodes[0], ei, rec->found_size);
2465 btrfs_mark_buffer_dirty(path->nodes[0]);
2466 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
2467 printf("reset nbytes for ino %llu root %llu\n",
2468 rec->ino, root->root_key.objectid);
2470 btrfs_release_path(path);
2474 static int add_missing_dir_index(struct btrfs_root *root,
2475 struct cache_tree *inode_cache,
2476 struct inode_record *rec,
2477 struct inode_backref *backref)
2479 struct btrfs_path path;
2480 struct btrfs_trans_handle *trans;
2481 struct btrfs_dir_item *dir_item;
2482 struct extent_buffer *leaf;
2483 struct btrfs_key key;
2484 struct btrfs_disk_key disk_key;
2485 struct inode_record *dir_rec;
2486 unsigned long name_ptr;
2487 u32 data_size = sizeof(*dir_item) + backref->namelen;
2490 trans = btrfs_start_transaction(root, 1);
2492 return PTR_ERR(trans);
2494 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
2495 (unsigned long long)rec->ino);
2497 btrfs_init_path(&path);
2498 key.objectid = backref->dir;
2499 key.type = BTRFS_DIR_INDEX_KEY;
2500 key.offset = backref->index;
2501 ret = btrfs_insert_empty_item(trans, root, &path, &key, data_size);
2504 leaf = path.nodes[0];
2505 dir_item = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_dir_item);
2507 disk_key.objectid = cpu_to_le64(rec->ino);
2508 disk_key.type = BTRFS_INODE_ITEM_KEY;
2509 disk_key.offset = 0;
2511 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
2512 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
2513 btrfs_set_dir_data_len(leaf, dir_item, 0);
2514 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
2515 name_ptr = (unsigned long)(dir_item + 1);
2516 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
2517 btrfs_mark_buffer_dirty(leaf);
2518 btrfs_release_path(&path);
2519 btrfs_commit_transaction(trans, root);
2521 backref->found_dir_index = 1;
2522 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
2523 BUG_ON(IS_ERR(dir_rec));
2526 dir_rec->found_size += backref->namelen;
2527 if (dir_rec->found_size == dir_rec->isize &&
2528 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
2529 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
2530 if (dir_rec->found_size != dir_rec->isize)
2531 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
2536 static int delete_dir_index(struct btrfs_root *root,
2537 struct cache_tree *inode_cache,
2538 struct inode_record *rec,
2539 struct inode_backref *backref)
2541 struct btrfs_trans_handle *trans;
2542 struct btrfs_dir_item *di;
2543 struct btrfs_path path;
2546 trans = btrfs_start_transaction(root, 1);
2548 return PTR_ERR(trans);
2550 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
2551 (unsigned long long)backref->dir,
2552 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
2553 (unsigned long long)root->objectid);
2555 btrfs_init_path(&path);
2556 di = btrfs_lookup_dir_index(trans, root, &path, backref->dir,
2557 backref->name, backref->namelen,
2558 backref->index, -1);
2561 btrfs_release_path(&path);
2562 btrfs_commit_transaction(trans, root);
2569 ret = btrfs_del_item(trans, root, &path);
2571 ret = btrfs_delete_one_dir_name(trans, root, &path, di);
2573 btrfs_release_path(&path);
2574 btrfs_commit_transaction(trans, root);
2578 static int create_inode_item(struct btrfs_root *root,
2579 struct inode_record *rec,
2580 struct inode_backref *backref, int root_dir)
2582 struct btrfs_trans_handle *trans;
2583 struct btrfs_inode_item inode_item;
2584 time_t now = time(NULL);
2587 trans = btrfs_start_transaction(root, 1);
2588 if (IS_ERR(trans)) {
2589 ret = PTR_ERR(trans);
2593 fprintf(stderr, "root %llu inode %llu recreating inode item, this may "
2594 "be incomplete, please check permissions and content after "
2595 "the fsck completes.\n", (unsigned long long)root->objectid,
2596 (unsigned long long)rec->ino);
2598 memset(&inode_item, 0, sizeof(inode_item));
2599 btrfs_set_stack_inode_generation(&inode_item, trans->transid);
2601 btrfs_set_stack_inode_nlink(&inode_item, 1);
2603 btrfs_set_stack_inode_nlink(&inode_item, rec->found_link);
2604 btrfs_set_stack_inode_nbytes(&inode_item, rec->found_size);
2605 if (rec->found_dir_item) {
2606 if (rec->found_file_extent)
2607 fprintf(stderr, "root %llu inode %llu has both a dir "
2608 "item and extents, unsure if it is a dir or a "
2609 "regular file so setting it as a directory\n",
2610 (unsigned long long)root->objectid,
2611 (unsigned long long)rec->ino);
2612 btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
2613 btrfs_set_stack_inode_size(&inode_item, rec->found_size);
2614 } else if (!rec->found_dir_item) {
2615 btrfs_set_stack_inode_size(&inode_item, rec->extent_end);
2616 btrfs_set_stack_inode_mode(&inode_item, S_IFREG | 0755);
2618 btrfs_set_stack_timespec_sec(&inode_item.atime, now);
2619 btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
2620 btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
2621 btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
2622 btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
2623 btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
2624 btrfs_set_stack_timespec_sec(&inode_item.otime, 0);
2625 btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
2627 ret = btrfs_insert_inode(trans, root, rec->ino, &inode_item);
2629 btrfs_commit_transaction(trans, root);
2633 static int repair_inode_backrefs(struct btrfs_root *root,
2634 struct inode_record *rec,
2635 struct cache_tree *inode_cache,
2638 struct inode_backref *tmp, *backref;
2639 u64 root_dirid = btrfs_root_dirid(&root->root_item);
2643 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2644 if (!delete && rec->ino == root_dirid) {
2645 if (!rec->found_inode_item) {
2646 ret = create_inode_item(root, rec, backref, 1);
2653 /* Index 0 for root dir's are special, don't mess with it */
2654 if (rec->ino == root_dirid && backref->index == 0)
2658 ((backref->found_dir_index && !backref->found_inode_ref) ||
2659 (backref->found_dir_index && backref->found_inode_ref &&
2660 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
2661 ret = delete_dir_index(root, inode_cache, rec, backref);
2665 list_del(&backref->list);
2669 if (!delete && !backref->found_dir_index &&
2670 backref->found_dir_item && backref->found_inode_ref) {
2671 ret = add_missing_dir_index(root, inode_cache, rec,
2676 if (backref->found_dir_item &&
2677 backref->found_dir_index &&
2678 backref->found_dir_index) {
2679 if (!backref->errors &&
2680 backref->found_inode_ref) {
2681 list_del(&backref->list);
2687 if (!delete && (!backref->found_dir_index &&
2688 !backref->found_dir_item &&
2689 backref->found_inode_ref)) {
2690 struct btrfs_trans_handle *trans;
2691 struct btrfs_key location;
2693 ret = check_dir_conflict(root, backref->name,
2699 * let nlink fixing routine to handle it,
2700 * which can do it better.
2705 location.objectid = rec->ino;
2706 location.type = BTRFS_INODE_ITEM_KEY;
2707 location.offset = 0;
2709 trans = btrfs_start_transaction(root, 1);
2710 if (IS_ERR(trans)) {
2711 ret = PTR_ERR(trans);
2714 fprintf(stderr, "adding missing dir index/item pair "
2716 (unsigned long long)rec->ino);
2717 ret = btrfs_insert_dir_item(trans, root, backref->name,
2719 backref->dir, &location,
2720 imode_to_type(rec->imode),
2723 btrfs_commit_transaction(trans, root);
2727 if (!delete && (backref->found_inode_ref &&
2728 backref->found_dir_index &&
2729 backref->found_dir_item &&
2730 !(backref->errors & REF_ERR_INDEX_UNMATCH) &&
2731 !rec->found_inode_item)) {
2732 ret = create_inode_item(root, rec, backref, 0);
2739 return ret ? ret : repaired;
2743 * To determine the file type for nlink/inode_item repair
2745 * Return 0 if file type is found and BTRFS_FT_* is stored into type.
2746 * Return -ENOENT if file type is not found.
2748 static int find_file_type(struct inode_record *rec, u8 *type)
2750 struct inode_backref *backref;
2752 /* For inode item recovered case */
2753 if (rec->found_inode_item) {
2754 *type = imode_to_type(rec->imode);
2758 list_for_each_entry(backref, &rec->backrefs, list) {
2759 if (backref->found_dir_index || backref->found_dir_item) {
2760 *type = backref->filetype;
2768 * To determine the file name for nlink repair
2770 * Return 0 if file name is found, set name and namelen.
2771 * Return -ENOENT if file name is not found.
2773 static int find_file_name(struct inode_record *rec,
2774 char *name, int *namelen)
2776 struct inode_backref *backref;
2778 list_for_each_entry(backref, &rec->backrefs, list) {
2779 if (backref->found_dir_index || backref->found_dir_item ||
2780 backref->found_inode_ref) {
2781 memcpy(name, backref->name, backref->namelen);
2782 *namelen = backref->namelen;
2789 /* Reset the nlink of the inode to the correct one */
2790 static int reset_nlink(struct btrfs_trans_handle *trans,
2791 struct btrfs_root *root,
2792 struct btrfs_path *path,
2793 struct inode_record *rec)
2795 struct inode_backref *backref;
2796 struct inode_backref *tmp;
2797 struct btrfs_key key;
2798 struct btrfs_inode_item *inode_item;
2801 /* We don't believe this either, reset it and iterate backref */
2802 rec->found_link = 0;
2804 /* Remove all backref including the valid ones */
2805 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
2806 ret = btrfs_unlink(trans, root, rec->ino, backref->dir,
2807 backref->index, backref->name,
2808 backref->namelen, 0);
2812 /* remove invalid backref, so it won't be added back */
2813 if (!(backref->found_dir_index &&
2814 backref->found_dir_item &&
2815 backref->found_inode_ref)) {
2816 list_del(&backref->list);
2823 /* Set nlink to 0 */
2824 key.objectid = rec->ino;
2825 key.type = BTRFS_INODE_ITEM_KEY;
2827 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2834 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2835 struct btrfs_inode_item);
2836 btrfs_set_inode_nlink(path->nodes[0], inode_item, 0);
2837 btrfs_mark_buffer_dirty(path->nodes[0]);
2838 btrfs_release_path(path);
2841 * Add back valid inode_ref/dir_item/dir_index,
2842 * add_link() will handle the nlink inc, so new nlink must be correct
2844 list_for_each_entry(backref, &rec->backrefs, list) {
2845 ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
2846 backref->name, backref->namelen,
2847 backref->filetype, &backref->index, 1);
2852 btrfs_release_path(path);
2856 static int get_highest_inode(struct btrfs_trans_handle *trans,
2857 struct btrfs_root *root,
2858 struct btrfs_path *path,
2861 struct btrfs_key key, found_key;
2864 btrfs_init_path(path);
2865 key.objectid = BTRFS_LAST_FREE_OBJECTID;
2867 key.type = BTRFS_INODE_ITEM_KEY;
2868 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2870 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2871 path->slots[0] - 1);
2872 *highest_ino = found_key.objectid;
2875 if (*highest_ino >= BTRFS_LAST_FREE_OBJECTID)
2877 btrfs_release_path(path);
2881 static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
2882 struct btrfs_root *root,
2883 struct btrfs_path *path,
2884 struct inode_record *rec)
2886 char *dir_name = "lost+found";
2887 char namebuf[BTRFS_NAME_LEN] = {0};
2892 int name_recovered = 0;
2893 int type_recovered = 0;
2897 * Get file name and type first before these invalid inode ref
2898 * are deleted by remove_all_invalid_backref()
2900 name_recovered = !find_file_name(rec, namebuf, &namelen);
2901 type_recovered = !find_file_type(rec, &type);
2903 if (!name_recovered) {
2904 printf("Can't get file name for inode %llu, using '%llu' as fallback\n",
2905 rec->ino, rec->ino);
2906 namelen = count_digits(rec->ino);
2907 sprintf(namebuf, "%llu", rec->ino);
2910 if (!type_recovered) {
2911 printf("Can't get file type for inode %llu, using FILE as fallback\n",
2913 type = BTRFS_FT_REG_FILE;
2917 ret = reset_nlink(trans, root, path, rec);
2920 "Failed to reset nlink for inode %llu: %s\n",
2921 rec->ino, strerror(-ret));
2925 if (rec->found_link == 0) {
2926 ret = get_highest_inode(trans, root, path, &lost_found_ino);
2930 ret = btrfs_mkdir(trans, root, dir_name, strlen(dir_name),
2931 BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
2934 fprintf(stderr, "Failed to create '%s' dir: %s\n",
2935 dir_name, strerror(-ret));
2938 ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
2939 namebuf, namelen, type, NULL, 1);
2941 * Add ".INO" suffix several times to handle case where
2942 * "FILENAME.INO" is already taken by another file.
2944 while (ret == -EEXIST) {
2946 * Conflicting file name, add ".INO" as suffix * +1 for '.'
2948 if (namelen + count_digits(rec->ino) + 1 >
2953 snprintf(namebuf + namelen, BTRFS_NAME_LEN - namelen,
2955 namelen += count_digits(rec->ino) + 1;
2956 ret = btrfs_add_link(trans, root, rec->ino,
2957 lost_found_ino, namebuf,
2958 namelen, type, NULL, 1);
2962 "Failed to link the inode %llu to %s dir: %s\n",
2963 rec->ino, dir_name, strerror(-ret));
2967 * Just increase the found_link, don't actually add the
2968 * backref. This will make things easier and this inode
2969 * record will be freed after the repair is done.
2970 * So fsck will not report problem about this inode.
2973 printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
2974 namelen, namebuf, dir_name);
2976 printf("Fixed the nlink of inode %llu\n", rec->ino);
2979 * Clear the flag anyway, or we will loop forever for the same inode
2980 * as it will not be removed from the bad inode list and the dead loop
2983 rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
2984 btrfs_release_path(path);
2989 * Check if there is any normal(reg or prealloc) file extent for given
2991 * This is used to determine the file type when neither its dir_index/item or
2992 * inode_item exists.
2994 * This will *NOT* report error, if any error happens, just consider it does
2995 * not have any normal file extent.
2997 static int find_normal_file_extent(struct btrfs_root *root, u64 ino)
2999 struct btrfs_path path;
3000 struct btrfs_key key;
3001 struct btrfs_key found_key;
3002 struct btrfs_file_extent_item *fi;
3006 btrfs_init_path(&path);
3008 key.type = BTRFS_EXTENT_DATA_KEY;
3011 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3016 if (ret && path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
3017 ret = btrfs_next_leaf(root, &path);
3024 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
3026 if (found_key.objectid != ino ||
3027 found_key.type != BTRFS_EXTENT_DATA_KEY)
3029 fi = btrfs_item_ptr(path.nodes[0], path.slots[0],
3030 struct btrfs_file_extent_item);
3031 type = btrfs_file_extent_type(path.nodes[0], fi);
3032 if (type != BTRFS_FILE_EXTENT_INLINE) {
3038 btrfs_release_path(&path);
3042 static u32 btrfs_type_to_imode(u8 type)
3044 static u32 imode_by_btrfs_type[] = {
3045 [BTRFS_FT_REG_FILE] = S_IFREG,
3046 [BTRFS_FT_DIR] = S_IFDIR,
3047 [BTRFS_FT_CHRDEV] = S_IFCHR,
3048 [BTRFS_FT_BLKDEV] = S_IFBLK,
3049 [BTRFS_FT_FIFO] = S_IFIFO,
3050 [BTRFS_FT_SOCK] = S_IFSOCK,
3051 [BTRFS_FT_SYMLINK] = S_IFLNK,
3054 return imode_by_btrfs_type[(type)];
3057 static int repair_inode_no_item(struct btrfs_trans_handle *trans,
3058 struct btrfs_root *root,
3059 struct btrfs_path *path,
3060 struct inode_record *rec)
3064 int type_recovered = 0;
3067 printf("Trying to rebuild inode:%llu\n", rec->ino);
3069 type_recovered = !find_file_type(rec, &filetype);
3072 * Try to determine inode type if type not found.
3074 * For found regular file extent, it must be FILE.
3075 * For found dir_item/index, it must be DIR.
3077 * For undetermined one, use FILE as fallback.
3080 * 1. If found backref(inode_index/item is already handled) to it,
3082 * Need new inode-inode ref structure to allow search for that.
3084 if (!type_recovered) {
3085 if (rec->found_file_extent &&
3086 find_normal_file_extent(root, rec->ino)) {
3088 filetype = BTRFS_FT_REG_FILE;
3089 } else if (rec->found_dir_item) {
3091 filetype = BTRFS_FT_DIR;
3092 } else if (!list_empty(&rec->orphan_extents)) {
3094 filetype = BTRFS_FT_REG_FILE;
3096 printf("Can't determine the filetype for inode %llu, assume it is a normal file\n",
3099 filetype = BTRFS_FT_REG_FILE;
3103 ret = btrfs_new_inode(trans, root, rec->ino,
3104 mode | btrfs_type_to_imode(filetype));
3109 * Here inode rebuild is done, we only rebuild the inode item,
3110 * don't repair the nlink(like move to lost+found).
3111 * That is the job of nlink repair.
3113 * We just fill the record and return
3115 rec->found_dir_item = 1;
3116 rec->imode = mode | btrfs_type_to_imode(filetype);
3118 rec->errors &= ~I_ERR_NO_INODE_ITEM;
3119 /* Ensure the inode_nlinks repair function will be called */
3120 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3125 static int repair_inode_orphan_extent(struct btrfs_trans_handle *trans,
3126 struct btrfs_root *root,
3127 struct btrfs_path *path,
3128 struct inode_record *rec)
3130 struct orphan_data_extent *orphan;
3131 struct orphan_data_extent *tmp;
3134 list_for_each_entry_safe(orphan, tmp, &rec->orphan_extents, list) {
3136 * Check for conflicting file extents
3138 * Here we don't know whether the extents is compressed or not,
3139 * so we can only assume it not compressed nor data offset,
3140 * and use its disk_len as extent length.
3142 ret = btrfs_get_extent(NULL, root, path, orphan->objectid,
3143 orphan->offset, orphan->disk_len, 0);
3144 btrfs_release_path(path);
3149 "orphan extent (%llu, %llu) conflicts, delete the orphan\n",
3150 orphan->disk_bytenr, orphan->disk_len);
3151 ret = btrfs_free_extent(trans,
3152 root->fs_info->extent_root,
3153 orphan->disk_bytenr, orphan->disk_len,
3154 0, root->objectid, orphan->objectid,
3159 ret = btrfs_insert_file_extent(trans, root, orphan->objectid,
3160 orphan->offset, orphan->disk_bytenr,
3161 orphan->disk_len, orphan->disk_len);
3165 /* Update file size info */
3166 rec->found_size += orphan->disk_len;
3167 if (rec->found_size == rec->nbytes)
3168 rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
3170 /* Update the file extent hole info too */
3171 ret = del_file_extent_hole(&rec->holes, orphan->offset,
3175 if (RB_EMPTY_ROOT(&rec->holes))
3176 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3178 list_del(&orphan->list);
3181 rec->errors &= ~I_ERR_FILE_EXTENT_ORPHAN;
3186 static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
3187 struct btrfs_root *root,
3188 struct btrfs_path *path,
3189 struct inode_record *rec)
3191 struct rb_node *node;
3192 struct file_extent_hole *hole;
3196 node = rb_first(&rec->holes);
3200 hole = rb_entry(node, struct file_extent_hole, node);
3201 ret = btrfs_punch_hole(trans, root, rec->ino,
3202 hole->start, hole->len);
3205 ret = del_file_extent_hole(&rec->holes, hole->start,
3209 if (RB_EMPTY_ROOT(&rec->holes))
3210 rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
3211 node = rb_first(&rec->holes);
3213 /* special case for a file losing all its file extent */
3215 ret = btrfs_punch_hole(trans, root, rec->ino, 0,
3216 round_up(rec->isize, root->sectorsize));
3220 printf("Fixed discount file extents for inode: %llu in root: %llu\n",
3221 rec->ino, root->objectid);
3226 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
3228 struct btrfs_trans_handle *trans;
3229 struct btrfs_path path;
3232 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG |
3233 I_ERR_NO_ORPHAN_ITEM |
3234 I_ERR_LINK_COUNT_WRONG |
3235 I_ERR_NO_INODE_ITEM |
3236 I_ERR_FILE_EXTENT_ORPHAN |
3237 I_ERR_FILE_EXTENT_DISCOUNT|
3238 I_ERR_FILE_NBYTES_WRONG)))
3242 * For nlink repair, it may create a dir and add link, so
3243 * 2 for parent(256)'s dir_index and dir_item
3244 * 2 for lost+found dir's inode_item and inode_ref
3245 * 1 for the new inode_ref of the file
3246 * 2 for lost+found dir's dir_index and dir_item for the file
3248 trans = btrfs_start_transaction(root, 7);
3250 return PTR_ERR(trans);
3252 btrfs_init_path(&path);
3253 if (rec->errors & I_ERR_NO_INODE_ITEM)
3254 ret = repair_inode_no_item(trans, root, &path, rec);
3255 if (!ret && rec->errors & I_ERR_FILE_EXTENT_ORPHAN)
3256 ret = repair_inode_orphan_extent(trans, root, &path, rec);
3257 if (!ret && rec->errors & I_ERR_FILE_EXTENT_DISCOUNT)
3258 ret = repair_inode_discount_extent(trans, root, &path, rec);
3259 if (!ret && rec->errors & I_ERR_DIR_ISIZE_WRONG)
3260 ret = repair_inode_isize(trans, root, &path, rec);
3261 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
3262 ret = repair_inode_orphan_item(trans, root, &path, rec);
3263 if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
3264 ret = repair_inode_nlinks(trans, root, &path, rec);
3265 if (!ret && rec->errors & I_ERR_FILE_NBYTES_WRONG)
3266 ret = repair_inode_nbytes(trans, root, &path, rec);
3267 btrfs_commit_transaction(trans, root);
3268 btrfs_release_path(&path);
3272 static int check_inode_recs(struct btrfs_root *root,
3273 struct cache_tree *inode_cache)
3275 struct cache_extent *cache;
3276 struct ptr_node *node;
3277 struct inode_record *rec;
3278 struct inode_backref *backref;
3283 u64 root_dirid = btrfs_root_dirid(&root->root_item);
3285 if (btrfs_root_refs(&root->root_item) == 0) {
3286 if (!cache_tree_empty(inode_cache))
3287 fprintf(stderr, "warning line %d\n", __LINE__);
3292 * We need to repair backrefs first because we could change some of the
3293 * errors in the inode recs.
3295 * We also need to go through and delete invalid backrefs first and then
3296 * add the correct ones second. We do this because we may get EEXIST
3297 * when adding back the correct index because we hadn't yet deleted the
3300 * For example, if we were missing a dir index then the directories
3301 * isize would be wrong, so if we fixed the isize to what we thought it
3302 * would be and then fixed the backref we'd still have a invalid fs, so
3303 * we need to add back the dir index and then check to see if the isize
3308 if (stage == 3 && !err)
3311 cache = search_cache_extent(inode_cache, 0);
3312 while (repair && cache) {
3313 node = container_of(cache, struct ptr_node, cache);
3315 cache = next_cache_extent(cache);
3317 /* Need to free everything up and rescan */
3319 remove_cache_extent(inode_cache, &node->cache);
3321 free_inode_rec(rec);
3325 if (list_empty(&rec->backrefs))
3328 ret = repair_inode_backrefs(root, rec, inode_cache,
3342 rec = get_inode_rec(inode_cache, root_dirid, 0);
3343 BUG_ON(IS_ERR(rec));
3345 ret = check_root_dir(rec);
3347 fprintf(stderr, "root %llu root dir %llu error\n",
3348 (unsigned long long)root->root_key.objectid,
3349 (unsigned long long)root_dirid);
3350 print_inode_error(root, rec);
3355 struct btrfs_trans_handle *trans;
3357 trans = btrfs_start_transaction(root, 1);
3358 if (IS_ERR(trans)) {
3359 err = PTR_ERR(trans);
3364 "root %llu missing its root dir, recreating\n",
3365 (unsigned long long)root->objectid);
3367 ret = btrfs_make_root_dir(trans, root, root_dirid);
3370 btrfs_commit_transaction(trans, root);
3374 fprintf(stderr, "root %llu root dir %llu not found\n",
3375 (unsigned long long)root->root_key.objectid,
3376 (unsigned long long)root_dirid);
3380 cache = search_cache_extent(inode_cache, 0);
3383 node = container_of(cache, struct ptr_node, cache);
3385 remove_cache_extent(inode_cache, &node->cache);
3387 if (rec->ino == root_dirid ||
3388 rec->ino == BTRFS_ORPHAN_OBJECTID) {
3389 free_inode_rec(rec);
3393 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
3394 ret = check_orphan_item(root, rec->ino);
3396 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
3397 if (can_free_inode_rec(rec)) {
3398 free_inode_rec(rec);
3403 if (!rec->found_inode_item)
3404 rec->errors |= I_ERR_NO_INODE_ITEM;
3405 if (rec->found_link != rec->nlink)
3406 rec->errors |= I_ERR_LINK_COUNT_WRONG;
3408 ret = try_repair_inode(root, rec);
3409 if (ret == 0 && can_free_inode_rec(rec)) {
3410 free_inode_rec(rec);
3416 if (!(repair && ret == 0))
3418 print_inode_error(root, rec);
3419 list_for_each_entry(backref, &rec->backrefs, list) {
3420 if (!backref->found_dir_item)
3421 backref->errors |= REF_ERR_NO_DIR_ITEM;
3422 if (!backref->found_dir_index)
3423 backref->errors |= REF_ERR_NO_DIR_INDEX;
3424 if (!backref->found_inode_ref)
3425 backref->errors |= REF_ERR_NO_INODE_REF;
3426 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
3427 " namelen %u name %s filetype %d errors %x",
3428 (unsigned long long)backref->dir,
3429 (unsigned long long)backref->index,
3430 backref->namelen, backref->name,
3431 backref->filetype, backref->errors);
3432 print_ref_error(backref->errors);
3434 free_inode_rec(rec);
3436 return (error > 0) ? -1 : 0;
3439 static struct root_record *get_root_rec(struct cache_tree *root_cache,
3442 struct cache_extent *cache;
3443 struct root_record *rec = NULL;
3446 cache = lookup_cache_extent(root_cache, objectid, 1);
3448 rec = container_of(cache, struct root_record, cache);
3450 rec = calloc(1, sizeof(*rec));
3452 return ERR_PTR(-ENOMEM);
3453 rec->objectid = objectid;
3454 INIT_LIST_HEAD(&rec->backrefs);
3455 rec->cache.start = objectid;
3456 rec->cache.size = 1;
3458 ret = insert_cache_extent(root_cache, &rec->cache);
3460 return ERR_PTR(-EEXIST);
3465 static struct root_backref *get_root_backref(struct root_record *rec,
3466 u64 ref_root, u64 dir, u64 index,
3467 const char *name, int namelen)
3469 struct root_backref *backref;
3471 list_for_each_entry(backref, &rec->backrefs, list) {
3472 if (backref->ref_root != ref_root || backref->dir != dir ||
3473 backref->namelen != namelen)
3475 if (memcmp(name, backref->name, namelen))
3480 backref = calloc(1, sizeof(*backref) + namelen + 1);
3483 backref->ref_root = ref_root;
3485 backref->index = index;
3486 backref->namelen = namelen;
3487 memcpy(backref->name, name, namelen);
3488 backref->name[namelen] = '\0';
3489 list_add_tail(&backref->list, &rec->backrefs);
3493 static void free_root_record(struct cache_extent *cache)
3495 struct root_record *rec;
3496 struct root_backref *backref;
3498 rec = container_of(cache, struct root_record, cache);
3499 while (!list_empty(&rec->backrefs)) {
3500 backref = to_root_backref(rec->backrefs.next);
3501 list_del(&backref->list);
3508 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
3510 static int add_root_backref(struct cache_tree *root_cache,
3511 u64 root_id, u64 ref_root, u64 dir, u64 index,
3512 const char *name, int namelen,
3513 int item_type, int errors)
3515 struct root_record *rec;
3516 struct root_backref *backref;
3518 rec = get_root_rec(root_cache, root_id);
3519 BUG_ON(IS_ERR(rec));
3520 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
3523 backref->errors |= errors;
3525 if (item_type != BTRFS_DIR_ITEM_KEY) {
3526 if (backref->found_dir_index || backref->found_back_ref ||
3527 backref->found_forward_ref) {
3528 if (backref->index != index)
3529 backref->errors |= REF_ERR_INDEX_UNMATCH;
3531 backref->index = index;
3535 if (item_type == BTRFS_DIR_ITEM_KEY) {
3536 if (backref->found_forward_ref)
3538 backref->found_dir_item = 1;
3539 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
3540 backref->found_dir_index = 1;
3541 } else if (item_type == BTRFS_ROOT_REF_KEY) {
3542 if (backref->found_forward_ref)
3543 backref->errors |= REF_ERR_DUP_ROOT_REF;
3544 else if (backref->found_dir_item)
3546 backref->found_forward_ref = 1;
3547 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
3548 if (backref->found_back_ref)
3549 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
3550 backref->found_back_ref = 1;
3555 if (backref->found_forward_ref && backref->found_dir_item)
3556 backref->reachable = 1;
3560 static int merge_root_recs(struct btrfs_root *root,
3561 struct cache_tree *src_cache,
3562 struct cache_tree *dst_cache)
3564 struct cache_extent *cache;
3565 struct ptr_node *node;
3566 struct inode_record *rec;
3567 struct inode_backref *backref;
3570 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
3571 free_inode_recs_tree(src_cache);
3576 cache = search_cache_extent(src_cache, 0);
3579 node = container_of(cache, struct ptr_node, cache);
3581 remove_cache_extent(src_cache, &node->cache);
3584 ret = is_child_root(root, root->objectid, rec->ino);
3590 list_for_each_entry(backref, &rec->backrefs, list) {
3591 BUG_ON(backref->found_inode_ref);
3592 if (backref->found_dir_item)
3593 add_root_backref(dst_cache, rec->ino,
3594 root->root_key.objectid, backref->dir,
3595 backref->index, backref->name,
3596 backref->namelen, BTRFS_DIR_ITEM_KEY,
3598 if (backref->found_dir_index)
3599 add_root_backref(dst_cache, rec->ino,
3600 root->root_key.objectid, backref->dir,
3601 backref->index, backref->name,
3602 backref->namelen, BTRFS_DIR_INDEX_KEY,
3606 free_inode_rec(rec);
3613 static int check_root_refs(struct btrfs_root *root,
3614 struct cache_tree *root_cache)
3616 struct root_record *rec;
3617 struct root_record *ref_root;
3618 struct root_backref *backref;
3619 struct cache_extent *cache;
3625 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
3626 BUG_ON(IS_ERR(rec));
3629 /* fixme: this can not detect circular references */
3632 cache = search_cache_extent(root_cache, 0);
3636 rec = container_of(cache, struct root_record, cache);
3637 cache = next_cache_extent(cache);
3639 if (rec->found_ref == 0)
3642 list_for_each_entry(backref, &rec->backrefs, list) {
3643 if (!backref->reachable)
3646 ref_root = get_root_rec(root_cache,
3648 BUG_ON(IS_ERR(ref_root));
3649 if (ref_root->found_ref > 0)
3652 backref->reachable = 0;
3654 if (rec->found_ref == 0)
3660 cache = search_cache_extent(root_cache, 0);
3664 rec = container_of(cache, struct root_record, cache);
3665 cache = next_cache_extent(cache);
3667 if (rec->found_ref == 0 &&
3668 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
3669 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
3670 ret = check_orphan_item(root->fs_info->tree_root,
3676 * If we don't have a root item then we likely just have
3677 * a dir item in a snapshot for this root but no actual
3678 * ref key or anything so it's meaningless.
3680 if (!rec->found_root_item)
3683 fprintf(stderr, "fs tree %llu not referenced\n",
3684 (unsigned long long)rec->objectid);
3688 if (rec->found_ref > 0 && !rec->found_root_item)
3690 list_for_each_entry(backref, &rec->backrefs, list) {
3691 if (!backref->found_dir_item)
3692 backref->errors |= REF_ERR_NO_DIR_ITEM;
3693 if (!backref->found_dir_index)
3694 backref->errors |= REF_ERR_NO_DIR_INDEX;
3695 if (!backref->found_back_ref)
3696 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
3697 if (!backref->found_forward_ref)
3698 backref->errors |= REF_ERR_NO_ROOT_REF;
3699 if (backref->reachable && backref->errors)
3706 fprintf(stderr, "fs tree %llu refs %u %s\n",
3707 (unsigned long long)rec->objectid, rec->found_ref,
3708 rec->found_root_item ? "" : "not found");
3710 list_for_each_entry(backref, &rec->backrefs, list) {
3711 if (!backref->reachable)
3713 if (!backref->errors && rec->found_root_item)
3715 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
3716 " index %llu namelen %u name %s errors %x\n",
3717 (unsigned long long)backref->ref_root,
3718 (unsigned long long)backref->dir,
3719 (unsigned long long)backref->index,
3720 backref->namelen, backref->name,
3722 print_ref_error(backref->errors);
3725 return errors > 0 ? 1 : 0;
3728 static int process_root_ref(struct extent_buffer *eb, int slot,
3729 struct btrfs_key *key,
3730 struct cache_tree *root_cache)
3736 struct btrfs_root_ref *ref;
3737 char namebuf[BTRFS_NAME_LEN];
3740 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
3742 dirid = btrfs_root_ref_dirid(eb, ref);
3743 index = btrfs_root_ref_sequence(eb, ref);
3744 name_len = btrfs_root_ref_name_len(eb, ref);
3746 if (name_len <= BTRFS_NAME_LEN) {
3750 len = BTRFS_NAME_LEN;
3751 error = REF_ERR_NAME_TOO_LONG;
3753 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
3755 if (key->type == BTRFS_ROOT_REF_KEY) {
3756 add_root_backref(root_cache, key->offset, key->objectid, dirid,
3757 index, namebuf, len, key->type, error);
3759 add_root_backref(root_cache, key->objectid, key->offset, dirid,
3760 index, namebuf, len, key->type, error);
3765 static void free_corrupt_block(struct cache_extent *cache)
3767 struct btrfs_corrupt_block *corrupt;
3769 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3773 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
3776 * Repair the btree of the given root.
3778 * The fix is to remove the node key in corrupt_blocks cache_tree.
3779 * and rebalance the tree.
3780 * After the fix, the btree should be writeable.
3782 static int repair_btree(struct btrfs_root *root,
3783 struct cache_tree *corrupt_blocks)
3785 struct btrfs_trans_handle *trans;
3786 struct btrfs_path path;
3787 struct btrfs_corrupt_block *corrupt;
3788 struct cache_extent *cache;
3789 struct btrfs_key key;
3794 if (cache_tree_empty(corrupt_blocks))
3797 trans = btrfs_start_transaction(root, 1);
3798 if (IS_ERR(trans)) {
3799 ret = PTR_ERR(trans);
3800 fprintf(stderr, "Error starting transaction: %s\n",
3804 btrfs_init_path(&path);
3805 cache = first_cache_extent(corrupt_blocks);
3807 corrupt = container_of(cache, struct btrfs_corrupt_block,
3809 level = corrupt->level;
3810 path.lowest_level = level;
3811 key.objectid = corrupt->key.objectid;
3812 key.type = corrupt->key.type;
3813 key.offset = corrupt->key.offset;
3816 * Here we don't want to do any tree balance, since it may
3817 * cause a balance with corrupted brother leaf/node,
3818 * so ins_len set to 0 here.
3819 * Balance will be done after all corrupt node/leaf is deleted.
3821 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
3824 offset = btrfs_node_blockptr(path.nodes[level],
3827 /* Remove the ptr */
3828 ret = btrfs_del_ptr(root, &path, level, path.slots[level]);
3832 * Remove the corresponding extent
3833 * return value is not concerned.
3835 btrfs_release_path(&path);
3836 ret = btrfs_free_extent(trans, root, offset, root->nodesize,
3837 0, root->root_key.objectid,
3839 cache = next_cache_extent(cache);
3842 /* Balance the btree using btrfs_search_slot() */
3843 cache = first_cache_extent(corrupt_blocks);
3845 corrupt = container_of(cache, struct btrfs_corrupt_block,
3847 memcpy(&key, &corrupt->key, sizeof(key));
3848 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
3851 /* return will always >0 since it won't find the item */
3853 btrfs_release_path(&path);
3854 cache = next_cache_extent(cache);
3857 btrfs_commit_transaction(trans, root);
3858 btrfs_release_path(&path);
3862 static int check_fs_root(struct btrfs_root *root,
3863 struct cache_tree *root_cache,
3864 struct walk_control *wc)
3870 struct btrfs_path path;
3871 struct shared_node root_node;
3872 struct root_record *rec;
3873 struct btrfs_root_item *root_item = &root->root_item;
3874 struct cache_tree corrupt_blocks;
3875 struct orphan_data_extent *orphan;
3876 struct orphan_data_extent *tmp;
3877 enum btrfs_tree_block_status status;
3878 struct node_refs nrefs;
3881 * Reuse the corrupt_block cache tree to record corrupted tree block
3883 * Unlike the usage in extent tree check, here we do it in a per
3884 * fs/subvol tree base.
3886 cache_tree_init(&corrupt_blocks);
3887 root->fs_info->corrupt_blocks = &corrupt_blocks;
3889 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
3890 rec = get_root_rec(root_cache, root->root_key.objectid);
3891 BUG_ON(IS_ERR(rec));
3892 if (btrfs_root_refs(root_item) > 0)
3893 rec->found_root_item = 1;
3896 btrfs_init_path(&path);
3897 memset(&root_node, 0, sizeof(root_node));
3898 cache_tree_init(&root_node.root_cache);
3899 cache_tree_init(&root_node.inode_cache);
3900 memset(&nrefs, 0, sizeof(nrefs));
3902 /* Move the orphan extent record to corresponding inode_record */
3903 list_for_each_entry_safe(orphan, tmp,
3904 &root->orphan_data_extents, list) {
3905 struct inode_record *inode;
3907 inode = get_inode_rec(&root_node.inode_cache, orphan->objectid,
3909 BUG_ON(IS_ERR(inode));
3910 inode->errors |= I_ERR_FILE_EXTENT_ORPHAN;
3911 list_move(&orphan->list, &inode->orphan_extents);
3914 level = btrfs_header_level(root->node);
3915 memset(wc->nodes, 0, sizeof(wc->nodes));
3916 wc->nodes[level] = &root_node;
3917 wc->active_node = level;
3918 wc->root_level = level;
3920 /* We may not have checked the root block, lets do that now */
3921 if (btrfs_is_leaf(root->node))
3922 status = btrfs_check_leaf(root, NULL, root->node);
3924 status = btrfs_check_node(root, NULL, root->node);
3925 if (status != BTRFS_TREE_BLOCK_CLEAN)
3928 if (btrfs_root_refs(root_item) > 0 ||
3929 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3930 path.nodes[level] = root->node;
3931 extent_buffer_get(root->node);
3932 path.slots[level] = 0;
3934 struct btrfs_key key;
3935 struct btrfs_disk_key found_key;
3937 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3938 level = root_item->drop_level;
3939 path.lowest_level = level;
3940 if (level > btrfs_header_level(root->node) ||
3941 level >= BTRFS_MAX_LEVEL) {
3942 error("ignoring invalid drop level: %u", level);
3945 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
3948 btrfs_node_key(path.nodes[level], &found_key,
3950 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3951 sizeof(found_key)));
3955 wret = walk_down_tree(root, &path, wc, &level, &nrefs);
3961 wret = walk_up_tree(root, &path, wc, &level);
3968 btrfs_release_path(&path);
3970 if (!cache_tree_empty(&corrupt_blocks)) {
3971 struct cache_extent *cache;
3972 struct btrfs_corrupt_block *corrupt;
3974 printf("The following tree block(s) is corrupted in tree %llu:\n",
3975 root->root_key.objectid);
3976 cache = first_cache_extent(&corrupt_blocks);
3978 corrupt = container_of(cache,
3979 struct btrfs_corrupt_block,
3981 printf("\ttree block bytenr: %llu, level: %d, node key: (%llu, %u, %llu)\n",
3982 cache->start, corrupt->level,
3983 corrupt->key.objectid, corrupt->key.type,
3984 corrupt->key.offset);
3985 cache = next_cache_extent(cache);
3988 printf("Try to repair the btree for root %llu\n",
3989 root->root_key.objectid);
3990 ret = repair_btree(root, &corrupt_blocks);
3992 fprintf(stderr, "Failed to repair btree: %s\n",
3995 printf("Btree for root %llu is fixed\n",
3996 root->root_key.objectid);
4000 err = merge_root_recs(root, &root_node.root_cache, root_cache);
4004 if (root_node.current) {
4005 root_node.current->checked = 1;
4006 maybe_free_inode_rec(&root_node.inode_cache,
4010 err = check_inode_recs(root, &root_node.inode_cache);
4014 free_corrupt_blocks_tree(&corrupt_blocks);
4015 root->fs_info->corrupt_blocks = NULL;
4016 free_orphan_data_extents(&root->orphan_data_extents);
4020 static int fs_root_objectid(u64 objectid)
4022 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
4023 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
4025 return is_fstree(objectid);
4028 static int check_fs_roots(struct btrfs_root *root,
4029 struct cache_tree *root_cache)
4031 struct btrfs_path path;
4032 struct btrfs_key key;
4033 struct walk_control wc;
4034 struct extent_buffer *leaf, *tree_node;
4035 struct btrfs_root *tmp_root;
4036 struct btrfs_root *tree_root = root->fs_info->tree_root;
4040 if (ctx.progress_enabled) {
4041 ctx.tp = TASK_FS_ROOTS;
4042 task_start(ctx.info);
4046 * Just in case we made any changes to the extent tree that weren't
4047 * reflected into the free space cache yet.
4050 reset_cached_block_groups(root->fs_info);
4051 memset(&wc, 0, sizeof(wc));
4052 cache_tree_init(&wc.shared);
4053 btrfs_init_path(&path);
4058 key.type = BTRFS_ROOT_ITEM_KEY;
4059 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
4064 tree_node = tree_root->node;
4066 if (tree_node != tree_root->node) {
4067 free_root_recs_tree(root_cache);
4068 btrfs_release_path(&path);
4071 leaf = path.nodes[0];
4072 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
4073 ret = btrfs_next_leaf(tree_root, &path);
4079 leaf = path.nodes[0];
4081 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
4082 if (key.type == BTRFS_ROOT_ITEM_KEY &&
4083 fs_root_objectid(key.objectid)) {
4084 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
4085 tmp_root = btrfs_read_fs_root_no_cache(
4086 root->fs_info, &key);
4088 key.offset = (u64)-1;
4089 tmp_root = btrfs_read_fs_root(
4090 root->fs_info, &key);
4092 if (IS_ERR(tmp_root)) {
4096 ret = check_fs_root(tmp_root, root_cache, &wc);
4097 if (ret == -EAGAIN) {
4098 free_root_recs_tree(root_cache);
4099 btrfs_release_path(&path);
4104 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
4105 btrfs_free_fs_root(tmp_root);
4106 } else if (key.type == BTRFS_ROOT_REF_KEY ||
4107 key.type == BTRFS_ROOT_BACKREF_KEY) {
4108 process_root_ref(leaf, path.slots[0], &key,
4115 btrfs_release_path(&path);
4117 free_extent_cache_tree(&wc.shared);
4118 if (!cache_tree_empty(&wc.shared))
4119 fprintf(stderr, "warning line %d\n", __LINE__);
4121 task_stop(ctx.info);
4127 * Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
4128 * INODE_REF/INODE_EXTREF match.
4130 * @root: the root of the fs/file tree
4131 * @ref_key: the key of the INODE_REF/INODE_EXTREF
4132 * @key: the key of the DIR_ITEM/DIR_INDEX
4133 * @index: the index in the INODE_REF/INODE_EXTREF, be used to
4134 * distinguish root_dir between normal dir/file
4135 * @name: the name in the INODE_REF/INODE_EXTREF
4136 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4137 * @mode: the st_mode of INODE_ITEM
4139 * Return 0 if no error occurred.
4140 * Return ROOT_DIR_ERROR if found DIR_ITEM/DIR_INDEX for root_dir.
4141 * Return DIR_ITEM_MISSING if couldn't find DIR_ITEM/DIR_INDEX for normal
4143 * Return DIR_ITEM_MISMATCH if INODE_REF/INODE_EXTREF and DIR_ITEM/DIR_INDEX
4144 * not match for normal dir/file.
4146 static int find_dir_item(struct btrfs_root *root, struct btrfs_key *ref_key,
4147 struct btrfs_key *key, u64 index, char *name,
4148 u32 namelen, u32 mode)
4150 struct btrfs_path path;
4151 struct extent_buffer *node;
4152 struct btrfs_dir_item *di;
4153 struct btrfs_key location;
4154 char namebuf[BTRFS_NAME_LEN] = {0};
4164 btrfs_init_path(&path);
4165 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4167 ret = DIR_ITEM_MISSING;
4171 /* Process root dir and goto out*/
4174 ret = ROOT_DIR_ERROR;
4176 "root %llu INODE %s[%llu %llu] ROOT_DIR shouldn't have %s",
4178 ref_key->type == BTRFS_INODE_REF_KEY ?
4180 ref_key->objectid, ref_key->offset,
4181 key->type == BTRFS_DIR_ITEM_KEY ?
4182 "DIR_ITEM" : "DIR_INDEX");
4190 /* Process normal file/dir */
4192 ret = DIR_ITEM_MISSING;
4194 "root %llu INODE %s[%llu %llu] doesn't have related %s[%llu %llu] namelen %u filename %s filetype %d",
4196 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4197 ref_key->objectid, ref_key->offset,
4198 key->type == BTRFS_DIR_ITEM_KEY ?
4199 "DIR_ITEM" : "DIR_INDEX",
4200 key->objectid, key->offset, namelen, name,
4201 imode_to_type(mode));
4205 /* Check whether inode_id/filetype/name match */
4206 node = path.nodes[0];
4207 slot = path.slots[0];
4208 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4209 total = btrfs_item_size_nr(node, slot);
4210 while (cur < total) {
4211 ret = DIR_ITEM_MISMATCH;
4212 name_len = btrfs_dir_name_len(node, di);
4213 data_len = btrfs_dir_data_len(node, di);
4215 btrfs_dir_item_key_to_cpu(node, di, &location);
4216 if (location.objectid != ref_key->objectid ||
4217 location.type != BTRFS_INODE_ITEM_KEY ||
4218 location.offset != 0)
4221 filetype = btrfs_dir_type(node, di);
4222 if (imode_to_type(mode) != filetype)
4225 if (name_len <= BTRFS_NAME_LEN) {
4228 len = BTRFS_NAME_LEN;
4229 warning("root %llu %s[%llu %llu] name too long %u, trimmed",
4231 key->type == BTRFS_DIR_ITEM_KEY ?
4232 "DIR_ITEM" : "DIR_INDEX",
4233 key->objectid, key->offset, name_len);
4235 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4236 if (len != namelen || strncmp(namebuf, name, len))
4242 len = sizeof(*di) + name_len + data_len;
4243 di = (struct btrfs_dir_item *)((char *)di + len);
4246 if (ret == DIR_ITEM_MISMATCH)
4248 "root %llu INODE %s[%llu %llu] and %s[%llu %llu] mismatch namelen %u filename %s filetype %d",
4250 ref_key->type == BTRFS_INODE_REF_KEY ? "REF" : "EXTREF",
4251 ref_key->objectid, ref_key->offset,
4252 key->type == BTRFS_DIR_ITEM_KEY ?
4253 "DIR_ITEM" : "DIR_INDEX",
4254 key->objectid, key->offset, namelen, name,
4255 imode_to_type(mode));
4257 btrfs_release_path(&path);
4262 * Traverse the given INODE_REF and call find_dir_item() to find related
4263 * DIR_ITEM/DIR_INDEX.
4265 * @root: the root of the fs/file tree
4266 * @ref_key: the key of the INODE_REF
4267 * @refs: the count of INODE_REF
4268 * @mode: the st_mode of INODE_ITEM
4270 * Return 0 if no error occurred.
4272 static int check_inode_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
4273 struct extent_buffer *node, int slot, u64 *refs,
4276 struct btrfs_key key;
4277 struct btrfs_inode_ref *ref;
4278 char namebuf[BTRFS_NAME_LEN] = {0};
4286 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4287 total = btrfs_item_size_nr(node, slot);
4290 /* Update inode ref count */
4293 index = btrfs_inode_ref_index(node, ref);
4294 name_len = btrfs_inode_ref_name_len(node, ref);
4295 if (name_len <= BTRFS_NAME_LEN) {
4298 len = BTRFS_NAME_LEN;
4299 warning("root %llu INODE_REF[%llu %llu] name too long",
4300 root->objectid, ref_key->objectid, ref_key->offset);
4303 read_extent_buffer(node, namebuf, (unsigned long)(ref + 1), len);
4305 /* Check root dir ref name */
4306 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4307 error("root %llu INODE_REF[%llu %llu] ROOT_DIR name shouldn't be %s",
4308 root->objectid, ref_key->objectid, ref_key->offset,
4310 err |= ROOT_DIR_ERROR;
4313 /* Find related DIR_INDEX */
4314 key.objectid = ref_key->offset;
4315 key.type = BTRFS_DIR_INDEX_KEY;
4317 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4320 /* Find related dir_item */
4321 key.objectid = ref_key->offset;
4322 key.type = BTRFS_DIR_ITEM_KEY;
4323 key.offset = btrfs_name_hash(namebuf, len);
4324 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4327 len = sizeof(*ref) + name_len;
4328 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4337 * Traverse the given INODE_EXTREF and call find_dir_item() to find related
4338 * DIR_ITEM/DIR_INDEX.
4340 * @root: the root of the fs/file tree
4341 * @ref_key: the key of the INODE_EXTREF
4342 * @refs: the count of INODE_EXTREF
4343 * @mode: the st_mode of INODE_ITEM
4345 * Return 0 if no error occurred.
4347 static int check_inode_extref(struct btrfs_root *root,
4348 struct btrfs_key *ref_key,
4349 struct extent_buffer *node, int slot, u64 *refs,
4352 struct btrfs_key key;
4353 struct btrfs_inode_extref *extref;
4354 char namebuf[BTRFS_NAME_LEN] = {0};
4364 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4365 total = btrfs_item_size_nr(node, slot);
4368 /* update inode ref count */
4370 name_len = btrfs_inode_extref_name_len(node, extref);
4371 index = btrfs_inode_extref_index(node, extref);
4372 parent = btrfs_inode_extref_parent(node, extref);
4373 if (name_len <= BTRFS_NAME_LEN) {
4376 len = BTRFS_NAME_LEN;
4377 warning("root %llu INODE_EXTREF[%llu %llu] name too long",
4378 root->objectid, ref_key->objectid, ref_key->offset);
4380 read_extent_buffer(node, namebuf, (unsigned long)(extref + 1), len);
4382 /* Check root dir ref name */
4383 if (index == 0 && strncmp(namebuf, "..", name_len)) {
4384 error("root %llu INODE_EXTREF[%llu %llu] ROOT_DIR name shouldn't be %s",
4385 root->objectid, ref_key->objectid, ref_key->offset,
4387 err |= ROOT_DIR_ERROR;
4390 /* find related dir_index */
4391 key.objectid = parent;
4392 key.type = BTRFS_DIR_INDEX_KEY;
4394 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4397 /* find related dir_item */
4398 key.objectid = parent;
4399 key.type = BTRFS_DIR_ITEM_KEY;
4400 key.offset = btrfs_name_hash(namebuf, len);
4401 ret = find_dir_item(root, ref_key, &key, index, namebuf, len, mode);
4404 len = sizeof(*extref) + name_len;
4405 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4415 * Find INODE_REF/INODE_EXTREF for the given key and check it with the specified
4416 * DIR_ITEM/DIR_INDEX match.
4418 * @root: the root of the fs/file tree
4419 * @key: the key of the INODE_REF/INODE_EXTREF
4420 * @name: the name in the INODE_REF/INODE_EXTREF
4421 * @namelen: the length of name in the INODE_REF/INODE_EXTREF
4422 * @index: the index in the INODE_REF/INODE_EXTREF, for DIR_ITEM set index
4424 * @ext_ref: the EXTENDED_IREF feature
4426 * Return 0 if no error occurred.
4427 * Return >0 for error bitmap
4429 static int find_inode_ref(struct btrfs_root *root, struct btrfs_key *key,
4430 char *name, int namelen, u64 index,
4431 unsigned int ext_ref)
4433 struct btrfs_path path;
4434 struct btrfs_inode_ref *ref;
4435 struct btrfs_inode_extref *extref;
4436 struct extent_buffer *node;
4437 char ref_namebuf[BTRFS_NAME_LEN] = {0};
4448 btrfs_init_path(&path);
4449 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4451 ret = INODE_REF_MISSING;
4455 node = path.nodes[0];
4456 slot = path.slots[0];
4458 ref = btrfs_item_ptr(node, slot, struct btrfs_inode_ref);
4459 total = btrfs_item_size_nr(node, slot);
4461 /* Iterate all entry of INODE_REF */
4462 while (cur < total) {
4463 ret = INODE_REF_MISSING;
4465 ref_namelen = btrfs_inode_ref_name_len(node, ref);
4466 ref_index = btrfs_inode_ref_index(node, ref);
4467 if (index != (u64)-1 && index != ref_index)
4470 if (ref_namelen <= BTRFS_NAME_LEN) {
4473 len = BTRFS_NAME_LEN;
4474 warning("root %llu INODE %s[%llu %llu] name too long",
4476 key->type == BTRFS_INODE_REF_KEY ?
4478 key->objectid, key->offset);
4480 read_extent_buffer(node, ref_namebuf, (unsigned long)(ref + 1),
4483 if (len != namelen || strncmp(ref_namebuf, name, len))
4489 len = sizeof(*ref) + ref_namelen;
4490 ref = (struct btrfs_inode_ref *)((char *)ref + len);
4495 /* Skip if not support EXTENDED_IREF feature */
4499 btrfs_release_path(&path);
4500 btrfs_init_path(&path);
4502 dir_id = key->offset;
4503 key->type = BTRFS_INODE_EXTREF_KEY;
4504 key->offset = btrfs_extref_hash(dir_id, name, namelen);
4506 ret = btrfs_search_slot(NULL, root, key, &path, 0, 0);
4508 ret = INODE_REF_MISSING;
4512 node = path.nodes[0];
4513 slot = path.slots[0];
4515 extref = btrfs_item_ptr(node, slot, struct btrfs_inode_extref);
4517 total = btrfs_item_size_nr(node, slot);
4519 /* Iterate all entry of INODE_EXTREF */
4520 while (cur < total) {
4521 ret = INODE_REF_MISSING;
4523 ref_namelen = btrfs_inode_extref_name_len(node, extref);
4524 ref_index = btrfs_inode_extref_index(node, extref);
4525 parent = btrfs_inode_extref_parent(node, extref);
4526 if (index != (u64)-1 && index != ref_index)
4529 if (parent != dir_id)
4532 if (ref_namelen <= BTRFS_NAME_LEN) {
4535 len = BTRFS_NAME_LEN;
4536 warning("root %llu INODE %s[%llu %llu] name too long",
4538 key->type == BTRFS_INODE_REF_KEY ?
4540 key->objectid, key->offset);
4542 read_extent_buffer(node, ref_namebuf,
4543 (unsigned long)(extref + 1), len);
4545 if (len != namelen || strncmp(ref_namebuf, name, len))
4552 len = sizeof(*extref) + ref_namelen;
4553 extref = (struct btrfs_inode_extref *)((char *)extref + len);
4558 btrfs_release_path(&path);
4563 * Traverse the given DIR_ITEM/DIR_INDEX and check related INODE_ITEM and
4564 * call find_inode_ref() to check related INODE_REF/INODE_EXTREF.
4566 * @root: the root of the fs/file tree
4567 * @key: the key of the INODE_REF/INODE_EXTREF
4568 * @size: the st_size of the INODE_ITEM
4569 * @ext_ref: the EXTENDED_IREF feature
4571 * Return 0 if no error occurred.
4573 static int check_dir_item(struct btrfs_root *root, struct btrfs_key *key,
4574 struct extent_buffer *node, int slot, u64 *size,
4575 unsigned int ext_ref)
4577 struct btrfs_dir_item *di;
4578 struct btrfs_inode_item *ii;
4579 struct btrfs_path path;
4580 struct btrfs_key location;
4581 char namebuf[BTRFS_NAME_LEN] = {0};
4594 * For DIR_ITEM set index to (u64)-1, so that find_inode_ref
4595 * ignore index check.
4597 index = (key->type == BTRFS_DIR_INDEX_KEY) ? key->offset : (u64)-1;
4599 di = btrfs_item_ptr(node, slot, struct btrfs_dir_item);
4600 total = btrfs_item_size_nr(node, slot);
4602 while (cur < total) {
4603 data_len = btrfs_dir_data_len(node, di);
4605 error("root %llu %s[%llu %llu] data_len shouldn't be %u",
4606 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4607 "DIR_ITEM" : "DIR_INDEX",
4608 key->objectid, key->offset, data_len);
4610 name_len = btrfs_dir_name_len(node, di);
4611 if (name_len <= BTRFS_NAME_LEN) {
4614 len = BTRFS_NAME_LEN;
4615 warning("root %llu %s[%llu %llu] name too long",
4617 key->type == BTRFS_DIR_ITEM_KEY ?
4618 "DIR_ITEM" : "DIR_INDEX",
4619 key->objectid, key->offset);
4621 (*size) += name_len;
4623 read_extent_buffer(node, namebuf, (unsigned long)(di + 1), len);
4624 filetype = btrfs_dir_type(node, di);
4626 btrfs_init_path(&path);
4627 btrfs_dir_item_key_to_cpu(node, di, &location);
4629 /* Ignore related ROOT_ITEM check */
4630 if (location.type == BTRFS_ROOT_ITEM_KEY)
4633 /* Check relative INODE_ITEM(existence/filetype) */
4634 ret = btrfs_search_slot(NULL, root, &location, &path, 0, 0);
4636 err |= INODE_ITEM_MISSING;
4637 error("root %llu %s[%llu %llu] couldn't find relative INODE_ITEM[%llu] namelen %u filename %s filetype %x",
4638 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4639 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4640 key->offset, location.objectid, name_len,
4645 ii = btrfs_item_ptr(path.nodes[0], path.slots[0],
4646 struct btrfs_inode_item);
4647 mode = btrfs_inode_mode(path.nodes[0], ii);
4649 if (imode_to_type(mode) != filetype) {
4650 err |= INODE_ITEM_MISMATCH;
4651 error("root %llu %s[%llu %llu] relative INODE_ITEM filetype mismatch namelen %u filename %s filetype %d",
4652 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4653 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4654 key->offset, name_len, namebuf, filetype);
4657 /* Check relative INODE_REF/INODE_EXTREF */
4658 location.type = BTRFS_INODE_REF_KEY;
4659 location.offset = key->objectid;
4660 ret = find_inode_ref(root, &location, namebuf, len,
4663 if (ret & INODE_REF_MISSING)
4664 error("root %llu %s[%llu %llu] relative INODE_REF missing namelen %u filename %s filetype %d",
4665 root->objectid, key->type == BTRFS_DIR_ITEM_KEY ?
4666 "DIR_ITEM" : "DIR_INDEX", key->objectid,
4667 key->offset, name_len, namebuf, filetype);
4670 btrfs_release_path(&path);
4671 len = sizeof(*di) + name_len + data_len;
4672 di = (struct btrfs_dir_item *)((char *)di + len);
4675 if (key->type == BTRFS_DIR_INDEX_KEY && cur < total) {
4676 error("root %llu DIR_INDEX[%llu %llu] should contain only one entry",
4677 root->objectid, key->objectid, key->offset);
4686 * Check file extent datasum/hole, update the size of the file extents,
4687 * check and update the last offset of the file extent.
4689 * @root: the root of fs/file tree.
4690 * @fkey: the key of the file extent.
4691 * @nodatasum: INODE_NODATASUM feature.
4692 * @size: the sum of all EXTENT_DATA items size for this inode.
4693 * @end: the offset of the last extent.
4695 * Return 0 if no error occurred.
4697 static int check_file_extent(struct btrfs_root *root, struct btrfs_key *fkey,
4698 struct extent_buffer *node, int slot,
4699 unsigned int nodatasum, u64 *size, u64 *end)
4701 struct btrfs_file_extent_item *fi;
4704 u64 extent_num_bytes;
4706 unsigned int extent_type;
4707 unsigned int is_hole;
4711 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
4713 extent_type = btrfs_file_extent_type(node, fi);
4714 /* Skip if file extent is inline */
4715 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4716 struct btrfs_item *e = btrfs_item_nr(slot);
4717 u32 item_inline_len;
4719 item_inline_len = btrfs_file_extent_inline_item_len(node, e);
4720 extent_num_bytes = btrfs_file_extent_inline_len(node, slot, fi);
4721 if (extent_num_bytes == 0 ||
4722 extent_num_bytes != item_inline_len)
4723 err |= FILE_EXTENT_ERROR;
4724 *size += extent_num_bytes;
4728 /* Check extent type */
4729 if (extent_type != BTRFS_FILE_EXTENT_REG &&
4730 extent_type != BTRFS_FILE_EXTENT_PREALLOC) {
4731 err |= FILE_EXTENT_ERROR;
4732 error("root %llu EXTENT_DATA[%llu %llu] type bad",
4733 root->objectid, fkey->objectid, fkey->offset);
4737 /* Check REG_EXTENT/PREALLOC_EXTENT */
4738 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
4739 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
4740 extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
4741 is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
4743 /* Check EXTENT_DATA datasum */
4744 ret = count_csum_range(root, disk_bytenr, disk_num_bytes, &found);
4745 if (found > 0 && nodatasum) {
4746 err |= ODD_CSUM_ITEM;
4747 error("root %llu EXTENT_DATA[%llu %llu] nodatasum shouldn't have datasum",
4748 root->objectid, fkey->objectid, fkey->offset);
4749 } else if (extent_type == BTRFS_FILE_EXTENT_REG && !nodatasum &&
4751 (ret < 0 || found == 0 || found < disk_num_bytes)) {
4752 err |= CSUM_ITEM_MISSING;
4753 error("root %llu EXTENT_DATA[%llu %llu] datasum missing",
4754 root->objectid, fkey->objectid, fkey->offset);
4755 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC && found > 0) {
4756 err |= ODD_CSUM_ITEM;
4757 error("root %llu EXTENT_DATA[%llu %llu] prealloc shouldn't have datasum",
4758 root->objectid, fkey->objectid, fkey->offset);
4761 /* Check EXTENT_DATA hole */
4762 if (no_holes && is_hole) {
4763 err |= FILE_EXTENT_ERROR;
4764 error("root %llu EXTENT_DATA[%llu %llu] shouldn't be hole",
4765 root->objectid, fkey->objectid, fkey->offset);
4766 } else if (!no_holes && *end != fkey->offset) {
4767 err |= FILE_EXTENT_ERROR;
4768 error("root %llu EXTENT_DATA[%llu %llu] interrupt",
4769 root->objectid, fkey->objectid, fkey->offset);
4772 *end += extent_num_bytes;
4774 *size += extent_num_bytes;
4780 * Check INODE_ITEM and related ITEMs (the same inode number)
4781 * 1. check link count
4782 * 2. check inode ref/extref
4783 * 3. check dir item/index
4785 * @ext_ref: the EXTENDED_IREF feature
4787 * Return 0 if no error occurred.
4788 * Return >0 for error or hit the traversal is done(by error bitmap)
4790 static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path,
4791 unsigned int ext_ref)
4793 struct extent_buffer *node;
4794 struct btrfs_inode_item *ii;
4795 struct btrfs_key key;
4804 u64 extent_size = 0;
4806 unsigned int nodatasum;
4811 node = path->nodes[0];
4812 slot = path->slots[0];
4814 btrfs_item_key_to_cpu(node, &key, slot);
4815 inode_id = key.objectid;
4817 if (inode_id == BTRFS_ORPHAN_OBJECTID) {
4818 ret = btrfs_next_item(root, path);
4824 ii = btrfs_item_ptr(node, slot, struct btrfs_inode_item);
4825 isize = btrfs_inode_size(node, ii);
4826 nbytes = btrfs_inode_nbytes(node, ii);
4827 mode = btrfs_inode_mode(node, ii);
4828 dir = imode_to_type(mode) == BTRFS_FT_DIR;
4829 nlink = btrfs_inode_nlink(node, ii);
4830 nodatasum = btrfs_inode_flags(node, ii) & BTRFS_INODE_NODATASUM;
4833 ret = btrfs_next_item(root, path);
4835 /* out will fill 'err' rusing current statistics */
4837 } else if (ret > 0) {
4842 node = path->nodes[0];
4843 slot = path->slots[0];
4844 btrfs_item_key_to_cpu(node, &key, slot);
4845 if (key.objectid != inode_id)
4849 case BTRFS_INODE_REF_KEY:
4850 ret = check_inode_ref(root, &key, node, slot, &refs,
4854 case BTRFS_INODE_EXTREF_KEY:
4855 if (key.type == BTRFS_INODE_EXTREF_KEY && !ext_ref)
4856 warning("root %llu EXTREF[%llu %llu] isn't supported",
4857 root->objectid, key.objectid,
4859 ret = check_inode_extref(root, &key, node, slot, &refs,
4863 case BTRFS_DIR_ITEM_KEY:
4864 case BTRFS_DIR_INDEX_KEY:
4866 warning("root %llu INODE[%llu] mode %u shouldn't have DIR_INDEX[%llu %llu]",
4867 root->objectid, inode_id,
4868 imode_to_type(mode), key.objectid,
4871 ret = check_dir_item(root, &key, node, slot, &size,
4875 case BTRFS_EXTENT_DATA_KEY:
4877 warning("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]",
4878 root->objectid, inode_id, key.objectid,
4881 ret = check_file_extent(root, &key, node, slot,
4882 nodatasum, &extent_size,
4886 case BTRFS_XATTR_ITEM_KEY:
4889 error("ITEM[%llu %u %llu] UNKNOWN TYPE",
4890 key.objectid, key.type, key.offset);
4895 /* verify INODE_ITEM nlink/isize/nbytes */
4898 err |= LINK_COUNT_ERROR;
4899 error("root %llu DIR INODE[%llu] shouldn't have more than one link(%llu)",
4900 root->objectid, inode_id, nlink);
4904 * Just a warning, as dir inode nbytes is just an
4905 * instructive value.
4907 if (!IS_ALIGNED(nbytes, root->nodesize)) {
4908 warning("root %llu DIR INODE[%llu] nbytes should be aligned to %u",
4909 root->objectid, inode_id, root->nodesize);
4912 if (isize != size) {
4914 error("root %llu DIR INODE [%llu] size(%llu) not equal to %llu",
4915 root->objectid, inode_id, isize, size);
4918 if (nlink != refs) {
4919 err |= LINK_COUNT_ERROR;
4920 error("root %llu INODE[%llu] nlink(%llu) not equal to inode_refs(%llu)",
4921 root->objectid, inode_id, nlink, refs);
4922 } else if (!nlink) {
4926 if (!nbytes && !no_holes && extent_end < isize) {
4927 err |= NBYTES_ERROR;
4928 error("root %llu INODE[%llu] size (%llu) should have a file extent hole",
4929 root->objectid, inode_id, isize);
4932 if (nbytes != extent_size) {
4933 err |= NBYTES_ERROR;
4934 error("root %llu INODE[%llu] nbytes(%llu) not equal to extent_size(%llu)",
4935 root->objectid, inode_id, nbytes, extent_size);
4942 static int check_fs_first_inode(struct btrfs_root *root, unsigned int ext_ref)
4944 struct btrfs_path path;
4945 struct btrfs_key key;
4949 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
4950 key.type = BTRFS_INODE_ITEM_KEY;
4953 /* For root being dropped, we don't need to check first inode */
4954 if (btrfs_root_refs(&root->root_item) == 0 &&
4955 btrfs_disk_key_objectid(&root->root_item.drop_progress) >=
4959 btrfs_init_path(&path);
4961 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
4966 err |= INODE_ITEM_MISSING;
4969 err |= check_inode_item(root, &path, ext_ref);
4974 btrfs_release_path(&path);
4979 * Iterate all item on the tree and call check_inode_item() to check.
4981 * @root: the root of the tree to be checked.
4982 * @ext_ref: the EXTENDED_IREF feature
4984 * Return 0 if no error found.
4985 * Return <0 for error.
4987 static int check_fs_root_v2(struct btrfs_root *root, unsigned int ext_ref)
4989 struct btrfs_path path;
4990 struct node_refs nrefs;
4991 struct btrfs_root_item *root_item = &root->root_item;
4996 * We need to manually check the first inode item(256)
4997 * As the following traversal function will only start from
4998 * the first inode item in the leaf, if inode item(256) is missing
4999 * we will just skip it forever.
5001 ret = check_fs_first_inode(root, ext_ref);
5005 memset(&nrefs, 0, sizeof(nrefs));
5006 level = btrfs_header_level(root->node);
5007 btrfs_init_path(&path);
5009 if (btrfs_root_refs(root_item) > 0 ||
5010 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5011 path.nodes[level] = root->node;
5012 path.slots[level] = 0;
5013 extent_buffer_get(root->node);
5015 struct btrfs_key key;
5017 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5018 level = root_item->drop_level;
5019 path.lowest_level = level;
5020 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
5027 wret = walk_down_tree_v2(root, &path, &level, &nrefs, ext_ref);
5033 wret = walk_up_tree_v2(root, &path, &level);
5041 btrfs_release_path(&path);
5046 * Find the relative ref for root_ref and root_backref.
5048 * @root: the root of the root tree.
5049 * @ref_key: the key of the root ref.
5051 * Return 0 if no error occurred.
5053 static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
5054 struct extent_buffer *node, int slot)
5056 struct btrfs_path path;
5057 struct btrfs_key key;
5058 struct btrfs_root_ref *ref;
5059 struct btrfs_root_ref *backref;
5060 char ref_name[BTRFS_NAME_LEN] = {0};
5061 char backref_name[BTRFS_NAME_LEN] = {0};
5067 u32 backref_namelen;
5072 ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
5073 ref_dirid = btrfs_root_ref_dirid(node, ref);
5074 ref_seq = btrfs_root_ref_sequence(node, ref);
5075 ref_namelen = btrfs_root_ref_name_len(node, ref);
5077 if (ref_namelen <= BTRFS_NAME_LEN) {
5080 len = BTRFS_NAME_LEN;
5081 warning("%s[%llu %llu] ref_name too long",
5082 ref_key->type == BTRFS_ROOT_REF_KEY ?
5083 "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
5086 read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
5088 /* Find relative root_ref */
5089 key.objectid = ref_key->offset;
5090 key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
5091 key.offset = ref_key->objectid;
5093 btrfs_init_path(&path);
5094 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
5096 err |= ROOT_REF_MISSING;
5097 error("%s[%llu %llu] couldn't find relative ref",
5098 ref_key->type == BTRFS_ROOT_REF_KEY ?
5099 "ROOT_REF" : "ROOT_BACKREF",
5100 ref_key->objectid, ref_key->offset);
5104 backref = btrfs_item_ptr(path.nodes[0], path.slots[0],
5105 struct btrfs_root_ref);
5106 backref_dirid = btrfs_root_ref_dirid(path.nodes[0], backref);
5107 backref_seq = btrfs_root_ref_sequence(path.nodes[0], backref);
5108 backref_namelen = btrfs_root_ref_name_len(path.nodes[0], backref);
5110 if (backref_namelen <= BTRFS_NAME_LEN) {
5111 len = backref_namelen;
5113 len = BTRFS_NAME_LEN;
5114 warning("%s[%llu %llu] ref_name too long",
5115 key.type == BTRFS_ROOT_REF_KEY ?
5116 "ROOT_REF" : "ROOT_BACKREF",
5117 key.objectid, key.offset);
5119 read_extent_buffer(path.nodes[0], backref_name,
5120 (unsigned long)(backref + 1), len);
5122 if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
5123 ref_namelen != backref_namelen ||
5124 strncmp(ref_name, backref_name, len)) {
5125 err |= ROOT_REF_MISMATCH;
5126 error("%s[%llu %llu] mismatch relative ref",
5127 ref_key->type == BTRFS_ROOT_REF_KEY ?
5128 "ROOT_REF" : "ROOT_BACKREF",
5129 ref_key->objectid, ref_key->offset);
5132 btrfs_release_path(&path);
5137 * Check all fs/file tree in low_memory mode.
5139 * 1. for fs tree root item, call check_fs_root_v2()
5140 * 2. for fs tree root ref/backref, call check_root_ref()
5142 * Return 0 if no error occurred.
5144 static int check_fs_roots_v2(struct btrfs_fs_info *fs_info)
5146 struct btrfs_root *tree_root = fs_info->tree_root;
5147 struct btrfs_root *cur_root = NULL;
5148 struct btrfs_path path;
5149 struct btrfs_key key;
5150 struct extent_buffer *node;
5151 unsigned int ext_ref;
5156 ext_ref = btrfs_fs_incompat(fs_info, EXTENDED_IREF);
5158 btrfs_init_path(&path);
5159 key.objectid = BTRFS_FS_TREE_OBJECTID;
5161 key.type = BTRFS_ROOT_ITEM_KEY;
5163 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
5167 } else if (ret > 0) {
5173 node = path.nodes[0];
5174 slot = path.slots[0];
5175 btrfs_item_key_to_cpu(node, &key, slot);
5176 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
5178 if (key.type == BTRFS_ROOT_ITEM_KEY &&
5179 fs_root_objectid(key.objectid)) {
5180 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
5181 cur_root = btrfs_read_fs_root_no_cache(fs_info,
5184 key.offset = (u64)-1;
5185 cur_root = btrfs_read_fs_root(fs_info, &key);
5188 if (IS_ERR(cur_root)) {
5189 error("Fail to read fs/subvol tree: %lld",
5195 ret = check_fs_root_v2(cur_root, ext_ref);
5198 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
5199 btrfs_free_fs_root(cur_root);
5200 } else if (key.type == BTRFS_ROOT_REF_KEY ||
5201 key.type == BTRFS_ROOT_BACKREF_KEY) {
5202 ret = check_root_ref(tree_root, &key, node, slot);
5206 ret = btrfs_next_item(tree_root, &path);
5216 btrfs_release_path(&path);
5220 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
5222 struct list_head *cur = rec->backrefs.next;
5223 struct extent_backref *back;
5224 struct tree_backref *tback;
5225 struct data_backref *dback;
5229 while(cur != &rec->backrefs) {
5230 back = to_extent_backref(cur);
5232 if (!back->found_extent_tree) {
5236 if (back->is_data) {
5237 dback = to_data_backref(back);
5238 fprintf(stderr, "Backref %llu %s %llu"
5239 " owner %llu offset %llu num_refs %lu"
5240 " not found in extent tree\n",
5241 (unsigned long long)rec->start,
5242 back->full_backref ?
5244 back->full_backref ?
5245 (unsigned long long)dback->parent:
5246 (unsigned long long)dback->root,
5247 (unsigned long long)dback->owner,
5248 (unsigned long long)dback->offset,
5249 (unsigned long)dback->num_refs);
5251 tback = to_tree_backref(back);
5252 fprintf(stderr, "Backref %llu parent %llu"
5253 " root %llu not found in extent tree\n",
5254 (unsigned long long)rec->start,
5255 (unsigned long long)tback->parent,
5256 (unsigned long long)tback->root);
5259 if (!back->is_data && !back->found_ref) {
5263 tback = to_tree_backref(back);
5264 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
5265 (unsigned long long)rec->start,
5266 back->full_backref ? "parent" : "root",
5267 back->full_backref ?
5268 (unsigned long long)tback->parent :
5269 (unsigned long long)tback->root, back);
5271 if (back->is_data) {
5272 dback = to_data_backref(back);
5273 if (dback->found_ref != dback->num_refs) {
5277 fprintf(stderr, "Incorrect local backref count"
5278 " on %llu %s %llu owner %llu"
5279 " offset %llu found %u wanted %u back %p\n",
5280 (unsigned long long)rec->start,
5281 back->full_backref ?
5283 back->full_backref ?
5284 (unsigned long long)dback->parent:
5285 (unsigned long long)dback->root,
5286 (unsigned long long)dback->owner,
5287 (unsigned long long)dback->offset,
5288 dback->found_ref, dback->num_refs, back);
5290 if (dback->disk_bytenr != rec->start) {
5294 fprintf(stderr, "Backref disk bytenr does not"
5295 " match extent record, bytenr=%llu, "
5296 "ref bytenr=%llu\n",
5297 (unsigned long long)rec->start,
5298 (unsigned long long)dback->disk_bytenr);
5301 if (dback->bytes != rec->nr) {
5305 fprintf(stderr, "Backref bytes do not match "
5306 "extent backref, bytenr=%llu, ref "
5307 "bytes=%llu, backref bytes=%llu\n",
5308 (unsigned long long)rec->start,
5309 (unsigned long long)rec->nr,
5310 (unsigned long long)dback->bytes);
5313 if (!back->is_data) {
5316 dback = to_data_backref(back);
5317 found += dback->found_ref;
5320 if (found != rec->refs) {
5324 fprintf(stderr, "Incorrect global backref count "
5325 "on %llu found %llu wanted %llu\n",
5326 (unsigned long long)rec->start,
5327 (unsigned long long)found,
5328 (unsigned long long)rec->refs);
5334 static int free_all_extent_backrefs(struct extent_record *rec)
5336 struct extent_backref *back;
5337 struct list_head *cur;
5338 while (!list_empty(&rec->backrefs)) {
5339 cur = rec->backrefs.next;
5340 back = to_extent_backref(cur);
5347 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
5348 struct cache_tree *extent_cache)
5350 struct cache_extent *cache;
5351 struct extent_record *rec;
5354 cache = first_cache_extent(extent_cache);
5357 rec = container_of(cache, struct extent_record, cache);
5358 remove_cache_extent(extent_cache, cache);
5359 free_all_extent_backrefs(rec);
5364 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
5365 struct extent_record *rec)
5367 if (rec->content_checked && rec->owner_ref_checked &&
5368 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
5369 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0) &&
5370 !rec->bad_full_backref && !rec->crossing_stripes &&
5371 !rec->wrong_chunk_type) {
5372 remove_cache_extent(extent_cache, &rec->cache);
5373 free_all_extent_backrefs(rec);
5374 list_del_init(&rec->list);
5380 static int check_owner_ref(struct btrfs_root *root,
5381 struct extent_record *rec,
5382 struct extent_buffer *buf)
5384 struct extent_backref *node;
5385 struct tree_backref *back;
5386 struct btrfs_root *ref_root;
5387 struct btrfs_key key;
5388 struct btrfs_path path;
5389 struct extent_buffer *parent;
5394 list_for_each_entry(node, &rec->backrefs, list) {
5397 if (!node->found_ref)
5399 if (node->full_backref)
5401 back = to_tree_backref(node);
5402 if (btrfs_header_owner(buf) == back->root)
5405 BUG_ON(rec->is_root);
5407 /* try to find the block by search corresponding fs tree */
5408 key.objectid = btrfs_header_owner(buf);
5409 key.type = BTRFS_ROOT_ITEM_KEY;
5410 key.offset = (u64)-1;
5412 ref_root = btrfs_read_fs_root(root->fs_info, &key);
5413 if (IS_ERR(ref_root))
5416 level = btrfs_header_level(buf);
5418 btrfs_item_key_to_cpu(buf, &key, 0);
5420 btrfs_node_key_to_cpu(buf, &key, 0);
5422 btrfs_init_path(&path);
5423 path.lowest_level = level + 1;
5424 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
5428 parent = path.nodes[level + 1];
5429 if (parent && buf->start == btrfs_node_blockptr(parent,
5430 path.slots[level + 1]))
5433 btrfs_release_path(&path);
5434 return found ? 0 : 1;
5437 static int is_extent_tree_record(struct extent_record *rec)
5439 struct list_head *cur = rec->backrefs.next;
5440 struct extent_backref *node;
5441 struct tree_backref *back;
5444 while(cur != &rec->backrefs) {
5445 node = to_extent_backref(cur);
5449 back = to_tree_backref(node);
5450 if (node->full_backref)
5452 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
5459 static int record_bad_block_io(struct btrfs_fs_info *info,
5460 struct cache_tree *extent_cache,
5463 struct extent_record *rec;
5464 struct cache_extent *cache;
5465 struct btrfs_key key;
5467 cache = lookup_cache_extent(extent_cache, start, len);
5471 rec = container_of(cache, struct extent_record, cache);
5472 if (!is_extent_tree_record(rec))
5475 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
5476 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
5479 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
5480 struct extent_buffer *buf, int slot)
5482 if (btrfs_header_level(buf)) {
5483 struct btrfs_key_ptr ptr1, ptr2;
5485 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
5486 sizeof(struct btrfs_key_ptr));
5487 read_extent_buffer(buf, &ptr2,
5488 btrfs_node_key_ptr_offset(slot + 1),
5489 sizeof(struct btrfs_key_ptr));
5490 write_extent_buffer(buf, &ptr1,
5491 btrfs_node_key_ptr_offset(slot + 1),
5492 sizeof(struct btrfs_key_ptr));
5493 write_extent_buffer(buf, &ptr2,
5494 btrfs_node_key_ptr_offset(slot),
5495 sizeof(struct btrfs_key_ptr));
5497 struct btrfs_disk_key key;
5498 btrfs_node_key(buf, &key, 0);
5499 btrfs_fixup_low_keys(root, path, &key,
5500 btrfs_header_level(buf) + 1);
5503 struct btrfs_item *item1, *item2;
5504 struct btrfs_key k1, k2;
5505 char *item1_data, *item2_data;
5506 u32 item1_offset, item2_offset, item1_size, item2_size;
5508 item1 = btrfs_item_nr(slot);
5509 item2 = btrfs_item_nr(slot + 1);
5510 btrfs_item_key_to_cpu(buf, &k1, slot);
5511 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
5512 item1_offset = btrfs_item_offset(buf, item1);
5513 item2_offset = btrfs_item_offset(buf, item2);
5514 item1_size = btrfs_item_size(buf, item1);
5515 item2_size = btrfs_item_size(buf, item2);
5517 item1_data = malloc(item1_size);
5520 item2_data = malloc(item2_size);
5526 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
5527 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
5529 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
5530 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
5534 btrfs_set_item_offset(buf, item1, item2_offset);
5535 btrfs_set_item_offset(buf, item2, item1_offset);
5536 btrfs_set_item_size(buf, item1, item2_size);
5537 btrfs_set_item_size(buf, item2, item1_size);
5539 path->slots[0] = slot;
5540 btrfs_set_item_key_unsafe(root, path, &k2);
5541 path->slots[0] = slot + 1;
5542 btrfs_set_item_key_unsafe(root, path, &k1);
5547 static int fix_key_order(struct btrfs_trans_handle *trans,
5548 struct btrfs_root *root,
5549 struct btrfs_path *path)
5551 struct extent_buffer *buf;
5552 struct btrfs_key k1, k2;
5554 int level = path->lowest_level;
5557 buf = path->nodes[level];
5558 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
5560 btrfs_node_key_to_cpu(buf, &k1, i);
5561 btrfs_node_key_to_cpu(buf, &k2, i + 1);
5563 btrfs_item_key_to_cpu(buf, &k1, i);
5564 btrfs_item_key_to_cpu(buf, &k2, i + 1);
5566 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
5568 ret = swap_values(root, path, buf, i);
5571 btrfs_mark_buffer_dirty(buf);
5577 static int delete_bogus_item(struct btrfs_trans_handle *trans,
5578 struct btrfs_root *root,
5579 struct btrfs_path *path,
5580 struct extent_buffer *buf, int slot)
5582 struct btrfs_key key;
5583 int nritems = btrfs_header_nritems(buf);
5585 btrfs_item_key_to_cpu(buf, &key, slot);
5587 /* These are all the keys we can deal with missing. */
5588 if (key.type != BTRFS_DIR_INDEX_KEY &&
5589 key.type != BTRFS_EXTENT_ITEM_KEY &&
5590 key.type != BTRFS_METADATA_ITEM_KEY &&
5591 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
5592 key.type != BTRFS_EXTENT_DATA_REF_KEY)
5595 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
5596 (unsigned long long)key.objectid, key.type,
5597 (unsigned long long)key.offset, slot, buf->start);
5598 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
5599 btrfs_item_nr_offset(slot + 1),
5600 sizeof(struct btrfs_item) *
5601 (nritems - slot - 1));
5602 btrfs_set_header_nritems(buf, nritems - 1);
5604 struct btrfs_disk_key disk_key;
5606 btrfs_item_key(buf, &disk_key, 0);
5607 btrfs_fixup_low_keys(root, path, &disk_key, 1);
5609 btrfs_mark_buffer_dirty(buf);
5613 static int fix_item_offset(struct btrfs_trans_handle *trans,
5614 struct btrfs_root *root,
5615 struct btrfs_path *path)
5617 struct extent_buffer *buf;
5621 /* We should only get this for leaves */
5622 BUG_ON(path->lowest_level);
5623 buf = path->nodes[0];
5625 for (i = 0; i < btrfs_header_nritems(buf); i++) {
5626 unsigned int shift = 0, offset;
5628 if (i == 0 && btrfs_item_end_nr(buf, i) !=
5629 BTRFS_LEAF_DATA_SIZE(root)) {
5630 if (btrfs_item_end_nr(buf, i) >
5631 BTRFS_LEAF_DATA_SIZE(root)) {
5632 ret = delete_bogus_item(trans, root, path,
5636 fprintf(stderr, "item is off the end of the "
5637 "leaf, can't fix\n");
5641 shift = BTRFS_LEAF_DATA_SIZE(root) -
5642 btrfs_item_end_nr(buf, i);
5643 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
5644 btrfs_item_offset_nr(buf, i - 1)) {
5645 if (btrfs_item_end_nr(buf, i) >
5646 btrfs_item_offset_nr(buf, i - 1)) {
5647 ret = delete_bogus_item(trans, root, path,
5651 fprintf(stderr, "items overlap, can't fix\n");
5655 shift = btrfs_item_offset_nr(buf, i - 1) -
5656 btrfs_item_end_nr(buf, i);
5661 printf("Shifting item nr %d by %u bytes in block %llu\n",
5662 i, shift, (unsigned long long)buf->start);
5663 offset = btrfs_item_offset_nr(buf, i);
5664 memmove_extent_buffer(buf,
5665 btrfs_leaf_data(buf) + offset + shift,
5666 btrfs_leaf_data(buf) + offset,
5667 btrfs_item_size_nr(buf, i));
5668 btrfs_set_item_offset(buf, btrfs_item_nr(i),
5670 btrfs_mark_buffer_dirty(buf);
5674 * We may have moved things, in which case we want to exit so we don't
5675 * write those changes out. Once we have proper abort functionality in
5676 * progs this can be changed to something nicer.
5683 * Attempt to fix basic block failures. If we can't fix it for whatever reason
5684 * then just return -EIO.
5686 static int try_to_fix_bad_block(struct btrfs_root *root,
5687 struct extent_buffer *buf,
5688 enum btrfs_tree_block_status status)
5690 struct btrfs_trans_handle *trans;
5691 struct ulist *roots;
5692 struct ulist_node *node;
5693 struct btrfs_root *search_root;
5694 struct btrfs_path path;
5695 struct ulist_iterator iter;
5696 struct btrfs_key root_key, key;
5699 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
5700 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5703 ret = btrfs_find_all_roots(NULL, root->fs_info, buf->start, 0, &roots);
5707 btrfs_init_path(&path);
5708 ULIST_ITER_INIT(&iter);
5709 while ((node = ulist_next(roots, &iter))) {
5710 root_key.objectid = node->val;
5711 root_key.type = BTRFS_ROOT_ITEM_KEY;
5712 root_key.offset = (u64)-1;
5714 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
5721 trans = btrfs_start_transaction(search_root, 0);
5722 if (IS_ERR(trans)) {
5723 ret = PTR_ERR(trans);
5727 path.lowest_level = btrfs_header_level(buf);
5728 path.skip_check_block = 1;
5729 if (path.lowest_level)
5730 btrfs_node_key_to_cpu(buf, &key, 0);
5732 btrfs_item_key_to_cpu(buf, &key, 0);
5733 ret = btrfs_search_slot(trans, search_root, &key, &path, 0, 1);
5736 btrfs_commit_transaction(trans, search_root);
5739 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
5740 ret = fix_key_order(trans, search_root, &path);
5741 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
5742 ret = fix_item_offset(trans, search_root, &path);
5744 btrfs_commit_transaction(trans, search_root);
5747 btrfs_release_path(&path);
5748 btrfs_commit_transaction(trans, search_root);
5751 btrfs_release_path(&path);
5755 static int check_block(struct btrfs_root *root,
5756 struct cache_tree *extent_cache,
5757 struct extent_buffer *buf, u64 flags)
5759 struct extent_record *rec;
5760 struct cache_extent *cache;
5761 struct btrfs_key key;
5762 enum btrfs_tree_block_status status;
5766 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
5769 rec = container_of(cache, struct extent_record, cache);
5770 rec->generation = btrfs_header_generation(buf);
5772 level = btrfs_header_level(buf);
5773 if (btrfs_header_nritems(buf) > 0) {
5776 btrfs_item_key_to_cpu(buf, &key, 0);
5778 btrfs_node_key_to_cpu(buf, &key, 0);
5780 rec->info_objectid = key.objectid;
5782 rec->info_level = level;
5784 if (btrfs_is_leaf(buf))
5785 status = btrfs_check_leaf(root, &rec->parent_key, buf);
5787 status = btrfs_check_node(root, &rec->parent_key, buf);
5789 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5791 status = try_to_fix_bad_block(root, buf, status);
5792 if (status != BTRFS_TREE_BLOCK_CLEAN) {
5794 fprintf(stderr, "bad block %llu\n",
5795 (unsigned long long)buf->start);
5798 * Signal to callers we need to start the scan over
5799 * again since we'll have cowed blocks.
5804 rec->content_checked = 1;
5805 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5806 rec->owner_ref_checked = 1;
5808 ret = check_owner_ref(root, rec, buf);
5810 rec->owner_ref_checked = 1;
5814 maybe_free_extent_rec(extent_cache, rec);
5818 static struct tree_backref *find_tree_backref(struct extent_record *rec,
5819 u64 parent, u64 root)
5821 struct list_head *cur = rec->backrefs.next;
5822 struct extent_backref *node;
5823 struct tree_backref *back;
5825 while(cur != &rec->backrefs) {
5826 node = to_extent_backref(cur);
5830 back = to_tree_backref(node);
5832 if (!node->full_backref)
5834 if (parent == back->parent)
5837 if (node->full_backref)
5839 if (back->root == root)
5846 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
5847 u64 parent, u64 root)
5849 struct tree_backref *ref = malloc(sizeof(*ref));
5853 memset(&ref->node, 0, sizeof(ref->node));
5855 ref->parent = parent;
5856 ref->node.full_backref = 1;
5859 ref->node.full_backref = 0;
5861 list_add_tail(&ref->node.list, &rec->backrefs);
5866 static struct data_backref *find_data_backref(struct extent_record *rec,
5867 u64 parent, u64 root,
5868 u64 owner, u64 offset,
5870 u64 disk_bytenr, u64 bytes)
5872 struct list_head *cur = rec->backrefs.next;
5873 struct extent_backref *node;
5874 struct data_backref *back;
5876 while(cur != &rec->backrefs) {
5877 node = to_extent_backref(cur);
5881 back = to_data_backref(node);
5883 if (!node->full_backref)
5885 if (parent == back->parent)
5888 if (node->full_backref)
5890 if (back->root == root && back->owner == owner &&
5891 back->offset == offset) {
5892 if (found_ref && node->found_ref &&
5893 (back->bytes != bytes ||
5894 back->disk_bytenr != disk_bytenr))
5903 static struct data_backref *alloc_data_backref(struct extent_record *rec,
5904 u64 parent, u64 root,
5905 u64 owner, u64 offset,
5908 struct data_backref *ref = malloc(sizeof(*ref));
5912 memset(&ref->node, 0, sizeof(ref->node));
5913 ref->node.is_data = 1;
5916 ref->parent = parent;
5919 ref->node.full_backref = 1;
5923 ref->offset = offset;
5924 ref->node.full_backref = 0;
5926 ref->bytes = max_size;
5929 list_add_tail(&ref->node.list, &rec->backrefs);
5930 if (max_size > rec->max_size)
5931 rec->max_size = max_size;
5935 /* Check if the type of extent matches with its chunk */
5936 static void check_extent_type(struct extent_record *rec)
5938 struct btrfs_block_group_cache *bg_cache;
5940 bg_cache = btrfs_lookup_first_block_group(global_info, rec->start);
5944 /* data extent, check chunk directly*/
5945 if (!rec->metadata) {
5946 if (!(bg_cache->flags & BTRFS_BLOCK_GROUP_DATA))
5947 rec->wrong_chunk_type = 1;
5951 /* metadata extent, check the obvious case first */
5952 if (!(bg_cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
5953 BTRFS_BLOCK_GROUP_METADATA))) {
5954 rec->wrong_chunk_type = 1;
5959 * Check SYSTEM extent, as it's also marked as metadata, we can only
5960 * make sure it's a SYSTEM extent by its backref
5962 if (!list_empty(&rec->backrefs)) {
5963 struct extent_backref *node;
5964 struct tree_backref *tback;
5967 node = to_extent_backref(rec->backrefs.next);
5968 if (node->is_data) {
5969 /* tree block shouldn't have data backref */
5970 rec->wrong_chunk_type = 1;
5973 tback = container_of(node, struct tree_backref, node);
5975 if (tback->root == BTRFS_CHUNK_TREE_OBJECTID)
5976 bg_type = BTRFS_BLOCK_GROUP_SYSTEM;
5978 bg_type = BTRFS_BLOCK_GROUP_METADATA;
5979 if (!(bg_cache->flags & bg_type))
5980 rec->wrong_chunk_type = 1;
5985 * Allocate a new extent record, fill default values from @tmpl and insert int
5986 * @extent_cache. Caller is supposed to make sure the [start,nr) is not in
5987 * the cache, otherwise it fails.
5989 static int add_extent_rec_nolookup(struct cache_tree *extent_cache,
5990 struct extent_record *tmpl)
5992 struct extent_record *rec;
5995 rec = malloc(sizeof(*rec));
5998 rec->start = tmpl->start;
5999 rec->max_size = tmpl->max_size;
6000 rec->nr = max(tmpl->nr, tmpl->max_size);
6001 rec->found_rec = tmpl->found_rec;
6002 rec->content_checked = tmpl->content_checked;
6003 rec->owner_ref_checked = tmpl->owner_ref_checked;
6004 rec->num_duplicates = 0;
6005 rec->metadata = tmpl->metadata;
6006 rec->flag_block_full_backref = FLAG_UNSET;
6007 rec->bad_full_backref = 0;
6008 rec->crossing_stripes = 0;
6009 rec->wrong_chunk_type = 0;
6010 rec->is_root = tmpl->is_root;
6011 rec->refs = tmpl->refs;
6012 rec->extent_item_refs = tmpl->extent_item_refs;
6013 rec->parent_generation = tmpl->parent_generation;
6014 INIT_LIST_HEAD(&rec->backrefs);
6015 INIT_LIST_HEAD(&rec->dups);
6016 INIT_LIST_HEAD(&rec->list);
6017 memcpy(&rec->parent_key, &tmpl->parent_key, sizeof(tmpl->parent_key));
6018 rec->cache.start = tmpl->start;
6019 rec->cache.size = tmpl->nr;
6020 ret = insert_cache_extent(extent_cache, &rec->cache);
6025 bytes_used += rec->nr;
6028 rec->crossing_stripes = check_crossing_stripes(global_info,
6029 rec->start, global_info->tree_root->nodesize);
6030 check_extent_type(rec);
6035 * Lookup and modify an extent, some values of @tmpl are interpreted verbatim,
6037 * - refs - if found, increase refs
6038 * - is_root - if found, set
6039 * - content_checked - if found, set
6040 * - owner_ref_checked - if found, set
6042 * If not found, create a new one, initialize and insert.
6044 static int add_extent_rec(struct cache_tree *extent_cache,
6045 struct extent_record *tmpl)
6047 struct extent_record *rec;
6048 struct cache_extent *cache;
6052 cache = lookup_cache_extent(extent_cache, tmpl->start, tmpl->nr);
6054 rec = container_of(cache, struct extent_record, cache);
6058 rec->nr = max(tmpl->nr, tmpl->max_size);
6061 * We need to make sure to reset nr to whatever the extent
6062 * record says was the real size, this way we can compare it to
6065 if (tmpl->found_rec) {
6066 if (tmpl->start != rec->start || rec->found_rec) {
6067 struct extent_record *tmp;
6070 if (list_empty(&rec->list))
6071 list_add_tail(&rec->list,
6072 &duplicate_extents);
6075 * We have to do this song and dance in case we
6076 * find an extent record that falls inside of
6077 * our current extent record but does not have
6078 * the same objectid.
6080 tmp = malloc(sizeof(*tmp));
6083 tmp->start = tmpl->start;
6084 tmp->max_size = tmpl->max_size;
6087 tmp->metadata = tmpl->metadata;
6088 tmp->extent_item_refs = tmpl->extent_item_refs;
6089 INIT_LIST_HEAD(&tmp->list);
6090 list_add_tail(&tmp->list, &rec->dups);
6091 rec->num_duplicates++;
6098 if (tmpl->extent_item_refs && !dup) {
6099 if (rec->extent_item_refs) {
6100 fprintf(stderr, "block %llu rec "
6101 "extent_item_refs %llu, passed %llu\n",
6102 (unsigned long long)tmpl->start,
6103 (unsigned long long)
6104 rec->extent_item_refs,
6105 (unsigned long long)tmpl->extent_item_refs);
6107 rec->extent_item_refs = tmpl->extent_item_refs;
6111 if (tmpl->content_checked)
6112 rec->content_checked = 1;
6113 if (tmpl->owner_ref_checked)
6114 rec->owner_ref_checked = 1;
6115 memcpy(&rec->parent_key, &tmpl->parent_key,
6116 sizeof(tmpl->parent_key));
6117 if (tmpl->parent_generation)
6118 rec->parent_generation = tmpl->parent_generation;
6119 if (rec->max_size < tmpl->max_size)
6120 rec->max_size = tmpl->max_size;
6123 * A metadata extent can't cross stripe_len boundary, otherwise
6124 * kernel scrub won't be able to handle it.
6125 * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
6129 rec->crossing_stripes = check_crossing_stripes(
6130 global_info, rec->start,
6131 global_info->tree_root->nodesize);
6132 check_extent_type(rec);
6133 maybe_free_extent_rec(extent_cache, rec);
6137 ret = add_extent_rec_nolookup(extent_cache, tmpl);
6142 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
6143 u64 parent, u64 root, int found_ref)
6145 struct extent_record *rec;
6146 struct tree_backref *back;
6147 struct cache_extent *cache;
6150 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6152 struct extent_record tmpl;
6154 memset(&tmpl, 0, sizeof(tmpl));
6155 tmpl.start = bytenr;
6159 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6163 /* really a bug in cache_extent implement now */
6164 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6169 rec = container_of(cache, struct extent_record, cache);
6170 if (rec->start != bytenr) {
6172 * Several cause, from unaligned bytenr to over lapping extents
6177 back = find_tree_backref(rec, parent, root);
6179 back = alloc_tree_backref(rec, parent, root);
6185 if (back->node.found_ref) {
6186 fprintf(stderr, "Extent back ref already exists "
6187 "for %llu parent %llu root %llu \n",
6188 (unsigned long long)bytenr,
6189 (unsigned long long)parent,
6190 (unsigned long long)root);
6192 back->node.found_ref = 1;
6194 if (back->node.found_extent_tree) {
6195 fprintf(stderr, "Extent back ref already exists "
6196 "for %llu parent %llu root %llu \n",
6197 (unsigned long long)bytenr,
6198 (unsigned long long)parent,
6199 (unsigned long long)root);
6201 back->node.found_extent_tree = 1;
6203 check_extent_type(rec);
6204 maybe_free_extent_rec(extent_cache, rec);
6208 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
6209 u64 parent, u64 root, u64 owner, u64 offset,
6210 u32 num_refs, int found_ref, u64 max_size)
6212 struct extent_record *rec;
6213 struct data_backref *back;
6214 struct cache_extent *cache;
6217 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6219 struct extent_record tmpl;
6221 memset(&tmpl, 0, sizeof(tmpl));
6222 tmpl.start = bytenr;
6224 tmpl.max_size = max_size;
6226 ret = add_extent_rec_nolookup(extent_cache, &tmpl);
6230 cache = lookup_cache_extent(extent_cache, bytenr, 1);
6235 rec = container_of(cache, struct extent_record, cache);
6236 if (rec->max_size < max_size)
6237 rec->max_size = max_size;
6240 * If found_ref is set then max_size is the real size and must match the
6241 * existing refs. So if we have already found a ref then we need to
6242 * make sure that this ref matches the existing one, otherwise we need
6243 * to add a new backref so we can notice that the backrefs don't match
6244 * and we need to figure out who is telling the truth. This is to
6245 * account for that awful fsync bug I introduced where we'd end up with
6246 * a btrfs_file_extent_item that would have its length include multiple
6247 * prealloc extents or point inside of a prealloc extent.
6249 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
6252 back = alloc_data_backref(rec, parent, root, owner, offset,
6258 BUG_ON(num_refs != 1);
6259 if (back->node.found_ref)
6260 BUG_ON(back->bytes != max_size);
6261 back->node.found_ref = 1;
6262 back->found_ref += 1;
6263 back->bytes = max_size;
6264 back->disk_bytenr = bytenr;
6266 rec->content_checked = 1;
6267 rec->owner_ref_checked = 1;
6269 if (back->node.found_extent_tree) {
6270 fprintf(stderr, "Extent back ref already exists "
6271 "for %llu parent %llu root %llu "
6272 "owner %llu offset %llu num_refs %lu\n",
6273 (unsigned long long)bytenr,
6274 (unsigned long long)parent,
6275 (unsigned long long)root,
6276 (unsigned long long)owner,
6277 (unsigned long long)offset,
6278 (unsigned long)num_refs);
6280 back->num_refs = num_refs;
6281 back->node.found_extent_tree = 1;
6283 maybe_free_extent_rec(extent_cache, rec);
6287 static int add_pending(struct cache_tree *pending,
6288 struct cache_tree *seen, u64 bytenr, u32 size)
6291 ret = add_cache_extent(seen, bytenr, size);
6294 add_cache_extent(pending, bytenr, size);
6298 static int pick_next_pending(struct cache_tree *pending,
6299 struct cache_tree *reada,
6300 struct cache_tree *nodes,
6301 u64 last, struct block_info *bits, int bits_nr,
6304 unsigned long node_start = last;
6305 struct cache_extent *cache;
6308 cache = search_cache_extent(reada, 0);
6310 bits[0].start = cache->start;
6311 bits[0].size = cache->size;
6316 if (node_start > 32768)
6317 node_start -= 32768;
6319 cache = search_cache_extent(nodes, node_start);
6321 cache = search_cache_extent(nodes, 0);
6324 cache = search_cache_extent(pending, 0);
6329 bits[ret].start = cache->start;
6330 bits[ret].size = cache->size;
6331 cache = next_cache_extent(cache);
6333 } while (cache && ret < bits_nr);
6339 bits[ret].start = cache->start;
6340 bits[ret].size = cache->size;
6341 cache = next_cache_extent(cache);
6343 } while (cache && ret < bits_nr);
6345 if (bits_nr - ret > 8) {
6346 u64 lookup = bits[0].start + bits[0].size;
6347 struct cache_extent *next;
6348 next = search_cache_extent(pending, lookup);
6350 if (next->start - lookup > 32768)
6352 bits[ret].start = next->start;
6353 bits[ret].size = next->size;
6354 lookup = next->start + next->size;
6358 next = next_cache_extent(next);
6366 static void free_chunk_record(struct cache_extent *cache)
6368 struct chunk_record *rec;
6370 rec = container_of(cache, struct chunk_record, cache);
6371 list_del_init(&rec->list);
6372 list_del_init(&rec->dextents);
6376 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
6378 cache_tree_free_extents(chunk_cache, free_chunk_record);
6381 static void free_device_record(struct rb_node *node)
6383 struct device_record *rec;
6385 rec = container_of(node, struct device_record, node);
6389 FREE_RB_BASED_TREE(device_cache, free_device_record);
6391 int insert_block_group_record(struct block_group_tree *tree,
6392 struct block_group_record *bg_rec)
6396 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
6400 list_add_tail(&bg_rec->list, &tree->block_groups);
6404 static void free_block_group_record(struct cache_extent *cache)
6406 struct block_group_record *rec;
6408 rec = container_of(cache, struct block_group_record, cache);
6409 list_del_init(&rec->list);
6413 void free_block_group_tree(struct block_group_tree *tree)
6415 cache_tree_free_extents(&tree->tree, free_block_group_record);
6418 int insert_device_extent_record(struct device_extent_tree *tree,
6419 struct device_extent_record *de_rec)
6424 * Device extent is a bit different from the other extents, because
6425 * the extents which belong to the different devices may have the
6426 * same start and size, so we need use the special extent cache
6427 * search/insert functions.
6429 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
6433 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
6434 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
6438 static void free_device_extent_record(struct cache_extent *cache)
6440 struct device_extent_record *rec;
6442 rec = container_of(cache, struct device_extent_record, cache);
6443 if (!list_empty(&rec->chunk_list))
6444 list_del_init(&rec->chunk_list);
6445 if (!list_empty(&rec->device_list))
6446 list_del_init(&rec->device_list);
6450 void free_device_extent_tree(struct device_extent_tree *tree)
6452 cache_tree_free_extents(&tree->tree, free_device_extent_record);
6455 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6456 static int process_extent_ref_v0(struct cache_tree *extent_cache,
6457 struct extent_buffer *leaf, int slot)
6459 struct btrfs_extent_ref_v0 *ref0;
6460 struct btrfs_key key;
6463 btrfs_item_key_to_cpu(leaf, &key, slot);
6464 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
6465 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
6466 ret = add_tree_backref(extent_cache, key.objectid, key.offset,
6469 ret = add_data_backref(extent_cache, key.objectid, key.offset,
6470 0, 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
6476 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
6477 struct btrfs_key *key,
6480 struct btrfs_chunk *ptr;
6481 struct chunk_record *rec;
6484 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6485 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
6487 rec = calloc(1, btrfs_chunk_record_size(num_stripes));
6489 fprintf(stderr, "memory allocation failed\n");
6493 INIT_LIST_HEAD(&rec->list);
6494 INIT_LIST_HEAD(&rec->dextents);
6497 rec->cache.start = key->offset;
6498 rec->cache.size = btrfs_chunk_length(leaf, ptr);
6500 rec->generation = btrfs_header_generation(leaf);
6502 rec->objectid = key->objectid;
6503 rec->type = key->type;
6504 rec->offset = key->offset;
6506 rec->length = rec->cache.size;
6507 rec->owner = btrfs_chunk_owner(leaf, ptr);
6508 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
6509 rec->type_flags = btrfs_chunk_type(leaf, ptr);
6510 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
6511 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
6512 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
6513 rec->num_stripes = num_stripes;
6514 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
6516 for (i = 0; i < rec->num_stripes; ++i) {
6517 rec->stripes[i].devid =
6518 btrfs_stripe_devid_nr(leaf, ptr, i);
6519 rec->stripes[i].offset =
6520 btrfs_stripe_offset_nr(leaf, ptr, i);
6521 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
6522 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
6529 static int process_chunk_item(struct cache_tree *chunk_cache,
6530 struct btrfs_key *key, struct extent_buffer *eb,
6533 struct chunk_record *rec;
6534 struct btrfs_chunk *chunk;
6537 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
6539 * Do extra check for this chunk item,
6541 * It's still possible one can craft a leaf with CHUNK_ITEM, with
6542 * wrong onwer(3) out of chunk tree, to pass both chunk tree check
6543 * and owner<->key_type check.
6545 ret = btrfs_check_chunk_valid(global_info->tree_root, eb, chunk, slot,
6548 error("chunk(%llu, %llu) is not valid, ignore it",
6549 key->offset, btrfs_chunk_length(eb, chunk));
6552 rec = btrfs_new_chunk_record(eb, key, slot);
6553 ret = insert_cache_extent(chunk_cache, &rec->cache);
6555 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
6556 rec->offset, rec->length);
6563 static int process_device_item(struct rb_root *dev_cache,
6564 struct btrfs_key *key, struct extent_buffer *eb, int slot)
6566 struct btrfs_dev_item *ptr;
6567 struct device_record *rec;
6570 ptr = btrfs_item_ptr(eb,
6571 slot, struct btrfs_dev_item);
6573 rec = malloc(sizeof(*rec));
6575 fprintf(stderr, "memory allocation failed\n");
6579 rec->devid = key->offset;
6580 rec->generation = btrfs_header_generation(eb);
6582 rec->objectid = key->objectid;
6583 rec->type = key->type;
6584 rec->offset = key->offset;
6586 rec->devid = btrfs_device_id(eb, ptr);
6587 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
6588 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
6590 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
6592 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
6599 struct block_group_record *
6600 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
6603 struct btrfs_block_group_item *ptr;
6604 struct block_group_record *rec;
6606 rec = calloc(1, sizeof(*rec));
6608 fprintf(stderr, "memory allocation failed\n");
6612 rec->cache.start = key->objectid;
6613 rec->cache.size = key->offset;
6615 rec->generation = btrfs_header_generation(leaf);
6617 rec->objectid = key->objectid;
6618 rec->type = key->type;
6619 rec->offset = key->offset;
6621 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
6622 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
6624 INIT_LIST_HEAD(&rec->list);
6629 static int process_block_group_item(struct block_group_tree *block_group_cache,
6630 struct btrfs_key *key,
6631 struct extent_buffer *eb, int slot)
6633 struct block_group_record *rec;
6636 rec = btrfs_new_block_group_record(eb, key, slot);
6637 ret = insert_block_group_record(block_group_cache, rec);
6639 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
6640 rec->objectid, rec->offset);
6647 struct device_extent_record *
6648 btrfs_new_device_extent_record(struct extent_buffer *leaf,
6649 struct btrfs_key *key, int slot)
6651 struct device_extent_record *rec;
6652 struct btrfs_dev_extent *ptr;
6654 rec = calloc(1, sizeof(*rec));
6656 fprintf(stderr, "memory allocation failed\n");
6660 rec->cache.objectid = key->objectid;
6661 rec->cache.start = key->offset;
6663 rec->generation = btrfs_header_generation(leaf);
6665 rec->objectid = key->objectid;
6666 rec->type = key->type;
6667 rec->offset = key->offset;
6669 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
6670 rec->chunk_objecteid =
6671 btrfs_dev_extent_chunk_objectid(leaf, ptr);
6673 btrfs_dev_extent_chunk_offset(leaf, ptr);
6674 rec->length = btrfs_dev_extent_length(leaf, ptr);
6675 rec->cache.size = rec->length;
6677 INIT_LIST_HEAD(&rec->chunk_list);
6678 INIT_LIST_HEAD(&rec->device_list);
6684 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
6685 struct btrfs_key *key, struct extent_buffer *eb,
6688 struct device_extent_record *rec;
6691 rec = btrfs_new_device_extent_record(eb, key, slot);
6692 ret = insert_device_extent_record(dev_extent_cache, rec);
6695 "Device extent[%llu, %llu, %llu] existed.\n",
6696 rec->objectid, rec->offset, rec->length);
6703 static int process_extent_item(struct btrfs_root *root,
6704 struct cache_tree *extent_cache,
6705 struct extent_buffer *eb, int slot)
6707 struct btrfs_extent_item *ei;
6708 struct btrfs_extent_inline_ref *iref;
6709 struct btrfs_extent_data_ref *dref;
6710 struct btrfs_shared_data_ref *sref;
6711 struct btrfs_key key;
6712 struct extent_record tmpl;
6717 u32 item_size = btrfs_item_size_nr(eb, slot);
6723 btrfs_item_key_to_cpu(eb, &key, slot);
6725 if (key.type == BTRFS_METADATA_ITEM_KEY) {
6727 num_bytes = root->nodesize;
6729 num_bytes = key.offset;
6732 if (!IS_ALIGNED(key.objectid, root->sectorsize)) {
6733 error("ignoring invalid extent, bytenr %llu is not aligned to %u",
6734 key.objectid, root->sectorsize);
6737 if (item_size < sizeof(*ei)) {
6738 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
6739 struct btrfs_extent_item_v0 *ei0;
6740 BUG_ON(item_size != sizeof(*ei0));
6741 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
6742 refs = btrfs_extent_refs_v0(eb, ei0);
6746 memset(&tmpl, 0, sizeof(tmpl));
6747 tmpl.start = key.objectid;
6748 tmpl.nr = num_bytes;
6749 tmpl.extent_item_refs = refs;
6750 tmpl.metadata = metadata;
6752 tmpl.max_size = num_bytes;
6754 return add_extent_rec(extent_cache, &tmpl);
6757 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
6758 refs = btrfs_extent_refs(eb, ei);
6759 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
6763 if (metadata && num_bytes != root->nodesize) {
6764 error("ignore invalid metadata extent, length %llu does not equal to %u",
6765 num_bytes, root->nodesize);
6768 if (!metadata && !IS_ALIGNED(num_bytes, root->sectorsize)) {
6769 error("ignore invalid data extent, length %llu is not aligned to %u",
6770 num_bytes, root->sectorsize);
6774 memset(&tmpl, 0, sizeof(tmpl));
6775 tmpl.start = key.objectid;
6776 tmpl.nr = num_bytes;
6777 tmpl.extent_item_refs = refs;
6778 tmpl.metadata = metadata;
6780 tmpl.max_size = num_bytes;
6781 add_extent_rec(extent_cache, &tmpl);
6783 ptr = (unsigned long)(ei + 1);
6784 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
6785 key.type == BTRFS_EXTENT_ITEM_KEY)
6786 ptr += sizeof(struct btrfs_tree_block_info);
6788 end = (unsigned long)ei + item_size;
6790 iref = (struct btrfs_extent_inline_ref *)ptr;
6791 type = btrfs_extent_inline_ref_type(eb, iref);
6792 offset = btrfs_extent_inline_ref_offset(eb, iref);
6794 case BTRFS_TREE_BLOCK_REF_KEY:
6795 ret = add_tree_backref(extent_cache, key.objectid,
6798 error("add_tree_backref failed: %s",
6801 case BTRFS_SHARED_BLOCK_REF_KEY:
6802 ret = add_tree_backref(extent_cache, key.objectid,
6805 error("add_tree_backref failed: %s",
6808 case BTRFS_EXTENT_DATA_REF_KEY:
6809 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
6810 add_data_backref(extent_cache, key.objectid, 0,
6811 btrfs_extent_data_ref_root(eb, dref),
6812 btrfs_extent_data_ref_objectid(eb,
6814 btrfs_extent_data_ref_offset(eb, dref),
6815 btrfs_extent_data_ref_count(eb, dref),
6818 case BTRFS_SHARED_DATA_REF_KEY:
6819 sref = (struct btrfs_shared_data_ref *)(iref + 1);
6820 add_data_backref(extent_cache, key.objectid, offset,
6822 btrfs_shared_data_ref_count(eb, sref),
6826 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
6827 key.objectid, key.type, num_bytes);
6830 ptr += btrfs_extent_inline_ref_size(type);
6837 static int check_cache_range(struct btrfs_root *root,
6838 struct btrfs_block_group_cache *cache,
6839 u64 offset, u64 bytes)
6841 struct btrfs_free_space *entry;
6847 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
6848 bytenr = btrfs_sb_offset(i);
6849 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
6850 cache->key.objectid, bytenr, 0,
6851 &logical, &nr, &stripe_len);
6856 if (logical[nr] + stripe_len <= offset)
6858 if (offset + bytes <= logical[nr])
6860 if (logical[nr] == offset) {
6861 if (stripe_len >= bytes) {
6865 bytes -= stripe_len;
6866 offset += stripe_len;
6867 } else if (logical[nr] < offset) {
6868 if (logical[nr] + stripe_len >=
6873 bytes = (offset + bytes) -
6874 (logical[nr] + stripe_len);
6875 offset = logical[nr] + stripe_len;
6878 * Could be tricky, the super may land in the
6879 * middle of the area we're checking. First
6880 * check the easiest case, it's at the end.
6882 if (logical[nr] + stripe_len >=
6884 bytes = logical[nr] - offset;
6888 /* Check the left side */
6889 ret = check_cache_range(root, cache,
6891 logical[nr] - offset);
6897 /* Now we continue with the right side */
6898 bytes = (offset + bytes) -
6899 (logical[nr] + stripe_len);
6900 offset = logical[nr] + stripe_len;
6907 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
6909 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
6910 offset, offset+bytes);
6914 if (entry->offset != offset) {
6915 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
6920 if (entry->bytes != bytes) {
6921 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
6922 bytes, entry->bytes, offset);
6926 unlink_free_space(cache->free_space_ctl, entry);
6931 static int verify_space_cache(struct btrfs_root *root,
6932 struct btrfs_block_group_cache *cache)
6934 struct btrfs_path path;
6935 struct extent_buffer *leaf;
6936 struct btrfs_key key;
6940 root = root->fs_info->extent_root;
6942 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
6944 btrfs_init_path(&path);
6945 key.objectid = last;
6947 key.type = BTRFS_EXTENT_ITEM_KEY;
6948 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
6953 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
6954 ret = btrfs_next_leaf(root, &path);
6962 leaf = path.nodes[0];
6963 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
6964 if (key.objectid >= cache->key.offset + cache->key.objectid)
6966 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
6967 key.type != BTRFS_METADATA_ITEM_KEY) {
6972 if (last == key.objectid) {
6973 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6974 last = key.objectid + key.offset;
6976 last = key.objectid + root->nodesize;
6981 ret = check_cache_range(root, cache, last,
6982 key.objectid - last);
6985 if (key.type == BTRFS_EXTENT_ITEM_KEY)
6986 last = key.objectid + key.offset;
6988 last = key.objectid + root->nodesize;
6992 if (last < cache->key.objectid + cache->key.offset)
6993 ret = check_cache_range(root, cache, last,
6994 cache->key.objectid +
6995 cache->key.offset - last);
6998 btrfs_release_path(&path);
7001 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
7002 fprintf(stderr, "There are still entries left in the space "
7010 static int check_space_cache(struct btrfs_root *root)
7012 struct btrfs_block_group_cache *cache;
7013 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
7017 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
7018 btrfs_super_generation(root->fs_info->super_copy) !=
7019 btrfs_super_cache_generation(root->fs_info->super_copy)) {
7020 printf("cache and super generation don't match, space cache "
7021 "will be invalidated\n");
7025 if (ctx.progress_enabled) {
7026 ctx.tp = TASK_FREE_SPACE;
7027 task_start(ctx.info);
7031 cache = btrfs_lookup_first_block_group(root->fs_info, start);
7035 start = cache->key.objectid + cache->key.offset;
7036 if (!cache->free_space_ctl) {
7037 if (btrfs_init_free_space_ctl(cache,
7038 root->sectorsize)) {
7043 btrfs_remove_free_space_cache(cache);
7046 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) {
7047 ret = exclude_super_stripes(root, cache);
7049 fprintf(stderr, "could not exclude super stripes: %s\n",
7054 ret = load_free_space_tree(root->fs_info, cache);
7055 free_excluded_extents(root, cache);
7057 fprintf(stderr, "could not load free space tree: %s\n",
7064 ret = load_free_space_cache(root->fs_info, cache);
7069 ret = verify_space_cache(root, cache);
7071 fprintf(stderr, "cache appears valid but isn't %Lu\n",
7072 cache->key.objectid);
7077 task_stop(ctx.info);
7079 return error ? -EINVAL : 0;
7082 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
7083 u64 num_bytes, unsigned long leaf_offset,
7084 struct extent_buffer *eb) {
7087 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7089 unsigned long csum_offset;
7093 u64 data_checked = 0;
7099 if (num_bytes % root->sectorsize)
7102 data = malloc(num_bytes);
7106 while (offset < num_bytes) {
7109 read_len = num_bytes - offset;
7110 /* read as much space once a time */
7111 ret = read_extent_data(root, data + offset,
7112 bytenr + offset, &read_len, mirror);
7116 /* verify every 4k data's checksum */
7117 while (data_checked < read_len) {
7119 tmp = offset + data_checked;
7121 csum = btrfs_csum_data((char *)data + tmp,
7122 csum, root->sectorsize);
7123 btrfs_csum_final(csum, (u8 *)&csum);
7125 csum_offset = leaf_offset +
7126 tmp / root->sectorsize * csum_size;
7127 read_extent_buffer(eb, (char *)&csum_expected,
7128 csum_offset, csum_size);
7129 /* try another mirror */
7130 if (csum != csum_expected) {
7131 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
7132 mirror, bytenr + tmp,
7133 csum, csum_expected);
7134 num_copies = btrfs_num_copies(
7135 &root->fs_info->mapping_tree,
7137 if (mirror < num_copies - 1) {
7142 data_checked += root->sectorsize;
7151 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
7154 struct btrfs_path path;
7155 struct extent_buffer *leaf;
7156 struct btrfs_key key;
7159 btrfs_init_path(&path);
7160 key.objectid = bytenr;
7161 key.type = BTRFS_EXTENT_ITEM_KEY;
7162 key.offset = (u64)-1;
7165 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, &path,
7168 fprintf(stderr, "Error looking up extent record %d\n", ret);
7169 btrfs_release_path(&path);
7172 if (path.slots[0] > 0) {
7175 ret = btrfs_prev_leaf(root, &path);
7178 } else if (ret > 0) {
7185 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7188 * Block group items come before extent items if they have the same
7189 * bytenr, so walk back one more just in case. Dear future traveller,
7190 * first congrats on mastering time travel. Now if it's not too much
7191 * trouble could you go back to 2006 and tell Chris to make the
7192 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
7193 * EXTENT_ITEM_KEY please?
7195 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
7196 if (path.slots[0] > 0) {
7199 ret = btrfs_prev_leaf(root, &path);
7202 } else if (ret > 0) {
7207 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
7211 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7212 ret = btrfs_next_leaf(root, &path);
7214 fprintf(stderr, "Error going to next leaf "
7216 btrfs_release_path(&path);
7222 leaf = path.nodes[0];
7223 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7224 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7228 if (key.objectid + key.offset < bytenr) {
7232 if (key.objectid > bytenr + num_bytes)
7235 if (key.objectid == bytenr) {
7236 if (key.offset >= num_bytes) {
7240 num_bytes -= key.offset;
7241 bytenr += key.offset;
7242 } else if (key.objectid < bytenr) {
7243 if (key.objectid + key.offset >= bytenr + num_bytes) {
7247 num_bytes = (bytenr + num_bytes) -
7248 (key.objectid + key.offset);
7249 bytenr = key.objectid + key.offset;
7251 if (key.objectid + key.offset < bytenr + num_bytes) {
7252 u64 new_start = key.objectid + key.offset;
7253 u64 new_bytes = bytenr + num_bytes - new_start;
7256 * Weird case, the extent is in the middle of
7257 * our range, we'll have to search one side
7258 * and then the other. Not sure if this happens
7259 * in real life, but no harm in coding it up
7260 * anyway just in case.
7262 btrfs_release_path(&path);
7263 ret = check_extent_exists(root, new_start,
7266 fprintf(stderr, "Right section didn't "
7270 num_bytes = key.objectid - bytenr;
7273 num_bytes = key.objectid - bytenr;
7280 if (num_bytes && !ret) {
7281 fprintf(stderr, "There are no extents for csum range "
7282 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
7286 btrfs_release_path(&path);
7290 static int check_csums(struct btrfs_root *root)
7292 struct btrfs_path path;
7293 struct extent_buffer *leaf;
7294 struct btrfs_key key;
7295 u64 offset = 0, num_bytes = 0;
7296 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7300 unsigned long leaf_offset;
7302 root = root->fs_info->csum_root;
7303 if (!extent_buffer_uptodate(root->node)) {
7304 fprintf(stderr, "No valid csum tree found\n");
7308 btrfs_init_path(&path);
7309 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
7310 key.type = BTRFS_EXTENT_CSUM_KEY;
7312 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
7314 fprintf(stderr, "Error searching csum tree %d\n", ret);
7315 btrfs_release_path(&path);
7319 if (ret > 0 && path.slots[0])
7324 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
7325 ret = btrfs_next_leaf(root, &path);
7327 fprintf(stderr, "Error going to next leaf "
7334 leaf = path.nodes[0];
7336 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
7337 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
7342 data_len = (btrfs_item_size_nr(leaf, path.slots[0]) /
7343 csum_size) * root->sectorsize;
7344 if (!check_data_csum)
7345 goto skip_csum_check;
7346 leaf_offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
7347 ret = check_extent_csums(root, key.offset, data_len,
7353 offset = key.offset;
7354 } else if (key.offset != offset + num_bytes) {
7355 ret = check_extent_exists(root, offset, num_bytes);
7357 fprintf(stderr, "Csum exists for %Lu-%Lu but "
7358 "there is no extent record\n",
7359 offset, offset+num_bytes);
7362 offset = key.offset;
7365 num_bytes += data_len;
7369 btrfs_release_path(&path);
7373 static int is_dropped_key(struct btrfs_key *key,
7374 struct btrfs_key *drop_key) {
7375 if (key->objectid < drop_key->objectid)
7377 else if (key->objectid == drop_key->objectid) {
7378 if (key->type < drop_key->type)
7380 else if (key->type == drop_key->type) {
7381 if (key->offset < drop_key->offset)
7389 * Here are the rules for FULL_BACKREF.
7391 * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
7392 * 2) If btrfs_header_owner(buf) no longer points to buf then we have
7394 * 3) We cowed the block walking down a reloc tree. This is impossible to tell
7395 * if it happened after the relocation occurred since we'll have dropped the
7396 * reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
7397 * have no real way to know for sure.
7399 * We process the blocks one root at a time, and we start from the lowest root
7400 * objectid and go to the highest. So we can just lookup the owner backref for
7401 * the record and if we don't find it then we know it doesn't exist and we have
7404 * FIXME: if we ever start reclaiming root objectid's then we need to fix this
7405 * assumption and simply indicate that we _think_ that the FULL BACKREF needs to
7406 * be set or not and then we can check later once we've gathered all the refs.
7408 static int calc_extent_flag(struct btrfs_root *root,
7409 struct cache_tree *extent_cache,
7410 struct extent_buffer *buf,
7411 struct root_item_record *ri,
7414 struct extent_record *rec;
7415 struct cache_extent *cache;
7416 struct tree_backref *tback;
7419 cache = lookup_cache_extent(extent_cache, buf->start, 1);
7420 /* we have added this extent before */
7424 rec = container_of(cache, struct extent_record, cache);
7427 * Except file/reloc tree, we can not have
7430 if (ri->objectid < BTRFS_FIRST_FREE_OBJECTID)
7435 if (buf->start == ri->bytenr)
7438 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7441 owner = btrfs_header_owner(buf);
7442 if (owner == ri->objectid)
7445 tback = find_tree_backref(rec, 0, owner);
7450 if (rec->flag_block_full_backref != FLAG_UNSET &&
7451 rec->flag_block_full_backref != 0)
7452 rec->bad_full_backref = 1;
7455 *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7456 if (rec->flag_block_full_backref != FLAG_UNSET &&
7457 rec->flag_block_full_backref != 1)
7458 rec->bad_full_backref = 1;
7462 static void report_mismatch_key_root(u8 key_type, u64 rootid)
7464 fprintf(stderr, "Invalid key type(");
7465 print_key_type(stderr, 0, key_type);
7466 fprintf(stderr, ") found in root(");
7467 print_objectid(stderr, rootid, 0);
7468 fprintf(stderr, ")\n");
7472 * Check if the key is valid with its extent buffer.
7474 * This is a early check in case invalid key exists in a extent buffer
7475 * This is not comprehensive yet, but should prevent wrong key/item passed
7478 static int check_type_with_root(u64 rootid, u8 key_type)
7481 /* Only valid in chunk tree */
7482 case BTRFS_DEV_ITEM_KEY:
7483 case BTRFS_CHUNK_ITEM_KEY:
7484 if (rootid != BTRFS_CHUNK_TREE_OBJECTID)
7487 /* valid in csum and log tree */
7488 case BTRFS_CSUM_TREE_OBJECTID:
7489 if (!(rootid == BTRFS_TREE_LOG_OBJECTID ||
7493 case BTRFS_EXTENT_ITEM_KEY:
7494 case BTRFS_METADATA_ITEM_KEY:
7495 case BTRFS_BLOCK_GROUP_ITEM_KEY:
7496 if (rootid != BTRFS_EXTENT_TREE_OBJECTID)
7499 case BTRFS_ROOT_ITEM_KEY:
7500 if (rootid != BTRFS_ROOT_TREE_OBJECTID)
7503 case BTRFS_DEV_EXTENT_KEY:
7504 if (rootid != BTRFS_DEV_TREE_OBJECTID)
7510 report_mismatch_key_root(key_type, rootid);
7514 static int run_next_block(struct btrfs_root *root,
7515 struct block_info *bits,
7518 struct cache_tree *pending,
7519 struct cache_tree *seen,
7520 struct cache_tree *reada,
7521 struct cache_tree *nodes,
7522 struct cache_tree *extent_cache,
7523 struct cache_tree *chunk_cache,
7524 struct rb_root *dev_cache,
7525 struct block_group_tree *block_group_cache,
7526 struct device_extent_tree *dev_extent_cache,
7527 struct root_item_record *ri)
7529 struct extent_buffer *buf;
7530 struct extent_record *rec = NULL;
7541 struct btrfs_key key;
7542 struct cache_extent *cache;
7545 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
7546 bits_nr, &reada_bits);
7551 for(i = 0; i < nritems; i++) {
7552 ret = add_cache_extent(reada, bits[i].start,
7557 /* fixme, get the parent transid */
7558 readahead_tree_block(root, bits[i].start,
7562 *last = bits[0].start;
7563 bytenr = bits[0].start;
7564 size = bits[0].size;
7566 cache = lookup_cache_extent(pending, bytenr, size);
7568 remove_cache_extent(pending, cache);
7571 cache = lookup_cache_extent(reada, bytenr, size);
7573 remove_cache_extent(reada, cache);
7576 cache = lookup_cache_extent(nodes, bytenr, size);
7578 remove_cache_extent(nodes, cache);
7581 cache = lookup_cache_extent(extent_cache, bytenr, size);
7583 rec = container_of(cache, struct extent_record, cache);
7584 gen = rec->parent_generation;
7587 /* fixme, get the real parent transid */
7588 buf = read_tree_block(root, bytenr, size, gen);
7589 if (!extent_buffer_uptodate(buf)) {
7590 record_bad_block_io(root->fs_info,
7591 extent_cache, bytenr, size);
7595 nritems = btrfs_header_nritems(buf);
7598 if (!init_extent_tree) {
7599 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
7600 btrfs_header_level(buf), 1, NULL,
7603 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7605 fprintf(stderr, "Couldn't calc extent flags\n");
7606 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7611 ret = calc_extent_flag(root, extent_cache, buf, ri, &flags);
7613 fprintf(stderr, "Couldn't calc extent flags\n");
7614 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7618 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7620 ri->objectid != BTRFS_TREE_RELOC_OBJECTID &&
7621 ri->objectid == btrfs_header_owner(buf)) {
7623 * Ok we got to this block from it's original owner and
7624 * we have FULL_BACKREF set. Relocation can leave
7625 * converted blocks over so this is altogether possible,
7626 * however it's not possible if the generation > the
7627 * last snapshot, so check for this case.
7629 if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC) &&
7630 btrfs_header_generation(buf) > ri->last_snapshot) {
7631 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
7632 rec->bad_full_backref = 1;
7637 (ri->objectid == BTRFS_TREE_RELOC_OBJECTID ||
7638 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
7639 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7640 rec->bad_full_backref = 1;
7644 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7645 rec->flag_block_full_backref = 1;
7649 rec->flag_block_full_backref = 0;
7651 owner = btrfs_header_owner(buf);
7654 ret = check_block(root, extent_cache, buf, flags);
7658 if (btrfs_is_leaf(buf)) {
7659 btree_space_waste += btrfs_leaf_free_space(root, buf);
7660 for (i = 0; i < nritems; i++) {
7661 struct btrfs_file_extent_item *fi;
7662 btrfs_item_key_to_cpu(buf, &key, i);
7664 * Check key type against the leaf owner.
7665 * Could filter quite a lot of early error if
7668 if (check_type_with_root(btrfs_header_owner(buf),
7670 fprintf(stderr, "ignoring invalid key\n");
7673 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
7674 process_extent_item(root, extent_cache, buf,
7678 if (key.type == BTRFS_METADATA_ITEM_KEY) {
7679 process_extent_item(root, extent_cache, buf,
7683 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
7685 btrfs_item_size_nr(buf, i);
7688 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
7689 process_chunk_item(chunk_cache, &key, buf, i);
7692 if (key.type == BTRFS_DEV_ITEM_KEY) {
7693 process_device_item(dev_cache, &key, buf, i);
7696 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7697 process_block_group_item(block_group_cache,
7701 if (key.type == BTRFS_DEV_EXTENT_KEY) {
7702 process_device_extent_item(dev_extent_cache,
7707 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
7708 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
7709 process_extent_ref_v0(extent_cache, buf, i);
7716 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
7717 ret = add_tree_backref(extent_cache,
7718 key.objectid, 0, key.offset, 0);
7720 error("add_tree_backref failed: %s",
7724 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
7725 ret = add_tree_backref(extent_cache,
7726 key.objectid, key.offset, 0, 0);
7728 error("add_tree_backref failed: %s",
7732 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
7733 struct btrfs_extent_data_ref *ref;
7734 ref = btrfs_item_ptr(buf, i,
7735 struct btrfs_extent_data_ref);
7736 add_data_backref(extent_cache,
7738 btrfs_extent_data_ref_root(buf, ref),
7739 btrfs_extent_data_ref_objectid(buf,
7741 btrfs_extent_data_ref_offset(buf, ref),
7742 btrfs_extent_data_ref_count(buf, ref),
7743 0, root->sectorsize);
7746 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
7747 struct btrfs_shared_data_ref *ref;
7748 ref = btrfs_item_ptr(buf, i,
7749 struct btrfs_shared_data_ref);
7750 add_data_backref(extent_cache,
7751 key.objectid, key.offset, 0, 0, 0,
7752 btrfs_shared_data_ref_count(buf, ref),
7753 0, root->sectorsize);
7756 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
7757 struct bad_item *bad;
7759 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
7763 bad = malloc(sizeof(struct bad_item));
7766 INIT_LIST_HEAD(&bad->list);
7767 memcpy(&bad->key, &key,
7768 sizeof(struct btrfs_key));
7769 bad->root_id = owner;
7770 list_add_tail(&bad->list, &delete_items);
7773 if (key.type != BTRFS_EXTENT_DATA_KEY)
7775 fi = btrfs_item_ptr(buf, i,
7776 struct btrfs_file_extent_item);
7777 if (btrfs_file_extent_type(buf, fi) ==
7778 BTRFS_FILE_EXTENT_INLINE)
7780 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
7783 data_bytes_allocated +=
7784 btrfs_file_extent_disk_num_bytes(buf, fi);
7785 if (data_bytes_allocated < root->sectorsize) {
7788 data_bytes_referenced +=
7789 btrfs_file_extent_num_bytes(buf, fi);
7790 add_data_backref(extent_cache,
7791 btrfs_file_extent_disk_bytenr(buf, fi),
7792 parent, owner, key.objectid, key.offset -
7793 btrfs_file_extent_offset(buf, fi), 1, 1,
7794 btrfs_file_extent_disk_num_bytes(buf, fi));
7798 struct btrfs_key first_key;
7800 first_key.objectid = 0;
7803 btrfs_item_key_to_cpu(buf, &first_key, 0);
7804 level = btrfs_header_level(buf);
7805 for (i = 0; i < nritems; i++) {
7806 struct extent_record tmpl;
7808 ptr = btrfs_node_blockptr(buf, i);
7809 size = root->nodesize;
7810 btrfs_node_key_to_cpu(buf, &key, i);
7812 if ((level == ri->drop_level)
7813 && is_dropped_key(&key, &ri->drop_key)) {
7818 memset(&tmpl, 0, sizeof(tmpl));
7819 btrfs_cpu_key_to_disk(&tmpl.parent_key, &key);
7820 tmpl.parent_generation = btrfs_node_ptr_generation(buf, i);
7825 tmpl.max_size = size;
7826 ret = add_extent_rec(extent_cache, &tmpl);
7830 ret = add_tree_backref(extent_cache, ptr, parent,
7833 error("add_tree_backref failed: %s",
7839 add_pending(nodes, seen, ptr, size);
7841 add_pending(pending, seen, ptr, size);
7844 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
7845 nritems) * sizeof(struct btrfs_key_ptr);
7847 total_btree_bytes += buf->len;
7848 if (fs_root_objectid(btrfs_header_owner(buf)))
7849 total_fs_tree_bytes += buf->len;
7850 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
7851 total_extent_tree_bytes += buf->len;
7852 if (!found_old_backref &&
7853 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
7854 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
7855 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
7856 found_old_backref = 1;
7858 free_extent_buffer(buf);
7862 static int add_root_to_pending(struct extent_buffer *buf,
7863 struct cache_tree *extent_cache,
7864 struct cache_tree *pending,
7865 struct cache_tree *seen,
7866 struct cache_tree *nodes,
7869 struct extent_record tmpl;
7872 if (btrfs_header_level(buf) > 0)
7873 add_pending(nodes, seen, buf->start, buf->len);
7875 add_pending(pending, seen, buf->start, buf->len);
7877 memset(&tmpl, 0, sizeof(tmpl));
7878 tmpl.start = buf->start;
7883 tmpl.max_size = buf->len;
7884 add_extent_rec(extent_cache, &tmpl);
7886 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
7887 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
7888 ret = add_tree_backref(extent_cache, buf->start, buf->start,
7891 ret = add_tree_backref(extent_cache, buf->start, 0, objectid,
7896 /* as we fix the tree, we might be deleting blocks that
7897 * we're tracking for repair. This hook makes sure we
7898 * remove any backrefs for blocks as we are fixing them.
7900 static int free_extent_hook(struct btrfs_trans_handle *trans,
7901 struct btrfs_root *root,
7902 u64 bytenr, u64 num_bytes, u64 parent,
7903 u64 root_objectid, u64 owner, u64 offset,
7906 struct extent_record *rec;
7907 struct cache_extent *cache;
7909 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
7911 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
7912 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
7916 rec = container_of(cache, struct extent_record, cache);
7918 struct data_backref *back;
7919 back = find_data_backref(rec, parent, root_objectid, owner,
7920 offset, 1, bytenr, num_bytes);
7923 if (back->node.found_ref) {
7924 back->found_ref -= refs_to_drop;
7926 rec->refs -= refs_to_drop;
7928 if (back->node.found_extent_tree) {
7929 back->num_refs -= refs_to_drop;
7930 if (rec->extent_item_refs)
7931 rec->extent_item_refs -= refs_to_drop;
7933 if (back->found_ref == 0)
7934 back->node.found_ref = 0;
7935 if (back->num_refs == 0)
7936 back->node.found_extent_tree = 0;
7938 if (!back->node.found_extent_tree && back->node.found_ref) {
7939 list_del(&back->node.list);
7943 struct tree_backref *back;
7944 back = find_tree_backref(rec, parent, root_objectid);
7947 if (back->node.found_ref) {
7950 back->node.found_ref = 0;
7952 if (back->node.found_extent_tree) {
7953 if (rec->extent_item_refs)
7954 rec->extent_item_refs--;
7955 back->node.found_extent_tree = 0;
7957 if (!back->node.found_extent_tree && back->node.found_ref) {
7958 list_del(&back->node.list);
7962 maybe_free_extent_rec(extent_cache, rec);
7967 static int delete_extent_records(struct btrfs_trans_handle *trans,
7968 struct btrfs_root *root,
7969 struct btrfs_path *path,
7972 struct btrfs_key key;
7973 struct btrfs_key found_key;
7974 struct extent_buffer *leaf;
7979 key.objectid = bytenr;
7981 key.offset = (u64)-1;
7984 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
7991 if (path->slots[0] == 0)
7997 leaf = path->nodes[0];
7998 slot = path->slots[0];
8000 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8001 if (found_key.objectid != bytenr)
8004 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
8005 found_key.type != BTRFS_METADATA_ITEM_KEY &&
8006 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
8007 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
8008 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
8009 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
8010 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
8011 btrfs_release_path(path);
8012 if (found_key.type == 0) {
8013 if (found_key.offset == 0)
8015 key.offset = found_key.offset - 1;
8016 key.type = found_key.type;
8018 key.type = found_key.type - 1;
8019 key.offset = (u64)-1;
8023 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
8024 found_key.objectid, found_key.type, found_key.offset);
8026 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
8029 btrfs_release_path(path);
8031 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
8032 found_key.type == BTRFS_METADATA_ITEM_KEY) {
8033 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
8034 found_key.offset : root->nodesize;
8036 ret = btrfs_update_block_group(trans, root, bytenr,
8043 btrfs_release_path(path);
8048 * for a single backref, this will allocate a new extent
8049 * and add the backref to it.
8051 static int record_extent(struct btrfs_trans_handle *trans,
8052 struct btrfs_fs_info *info,
8053 struct btrfs_path *path,
8054 struct extent_record *rec,
8055 struct extent_backref *back,
8056 int allocated, u64 flags)
8059 struct btrfs_root *extent_root = info->extent_root;
8060 struct extent_buffer *leaf;
8061 struct btrfs_key ins_key;
8062 struct btrfs_extent_item *ei;
8063 struct data_backref *dback;
8064 struct btrfs_tree_block_info *bi;
8067 rec->max_size = max_t(u64, rec->max_size,
8068 info->extent_root->nodesize);
8071 u32 item_size = sizeof(*ei);
8074 item_size += sizeof(*bi);
8076 ins_key.objectid = rec->start;
8077 ins_key.offset = rec->max_size;
8078 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
8080 ret = btrfs_insert_empty_item(trans, extent_root, path,
8081 &ins_key, item_size);
8085 leaf = path->nodes[0];
8086 ei = btrfs_item_ptr(leaf, path->slots[0],
8087 struct btrfs_extent_item);
8089 btrfs_set_extent_refs(leaf, ei, 0);
8090 btrfs_set_extent_generation(leaf, ei, rec->generation);
8092 if (back->is_data) {
8093 btrfs_set_extent_flags(leaf, ei,
8094 BTRFS_EXTENT_FLAG_DATA);
8096 struct btrfs_disk_key copy_key;;
8098 bi = (struct btrfs_tree_block_info *)(ei + 1);
8099 memset_extent_buffer(leaf, 0, (unsigned long)bi,
8102 btrfs_set_disk_key_objectid(©_key,
8103 rec->info_objectid);
8104 btrfs_set_disk_key_type(©_key, 0);
8105 btrfs_set_disk_key_offset(©_key, 0);
8107 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
8108 btrfs_set_tree_block_key(leaf, bi, ©_key);
8110 btrfs_set_extent_flags(leaf, ei,
8111 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
8114 btrfs_mark_buffer_dirty(leaf);
8115 ret = btrfs_update_block_group(trans, extent_root, rec->start,
8116 rec->max_size, 1, 0);
8119 btrfs_release_path(path);
8122 if (back->is_data) {
8126 dback = to_data_backref(back);
8127 if (back->full_backref)
8128 parent = dback->parent;
8132 for (i = 0; i < dback->found_ref; i++) {
8133 /* if parent != 0, we're doing a full backref
8134 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
8135 * just makes the backref allocator create a data
8138 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8139 rec->start, rec->max_size,
8143 BTRFS_FIRST_FREE_OBJECTID :
8149 fprintf(stderr, "adding new data backref"
8150 " on %llu %s %llu owner %llu"
8151 " offset %llu found %d\n",
8152 (unsigned long long)rec->start,
8153 back->full_backref ?
8155 back->full_backref ?
8156 (unsigned long long)parent :
8157 (unsigned long long)dback->root,
8158 (unsigned long long)dback->owner,
8159 (unsigned long long)dback->offset,
8163 struct tree_backref *tback;
8165 tback = to_tree_backref(back);
8166 if (back->full_backref)
8167 parent = tback->parent;
8171 ret = btrfs_inc_extent_ref(trans, info->extent_root,
8172 rec->start, rec->max_size,
8173 parent, tback->root, 0, 0);
8174 fprintf(stderr, "adding new tree backref on "
8175 "start %llu len %llu parent %llu root %llu\n",
8176 rec->start, rec->max_size, parent, tback->root);
8179 btrfs_release_path(path);
8183 static struct extent_entry *find_entry(struct list_head *entries,
8184 u64 bytenr, u64 bytes)
8186 struct extent_entry *entry = NULL;
8188 list_for_each_entry(entry, entries, list) {
8189 if (entry->bytenr == bytenr && entry->bytes == bytes)
8196 static struct extent_entry *find_most_right_entry(struct list_head *entries)
8198 struct extent_entry *entry, *best = NULL, *prev = NULL;
8200 list_for_each_entry(entry, entries, list) {
8202 * If there are as many broken entries as entries then we know
8203 * not to trust this particular entry.
8205 if (entry->broken == entry->count)
8209 * Special case, when there are only two entries and 'best' is
8219 * If our current entry == best then we can't be sure our best
8220 * is really the best, so we need to keep searching.
8222 if (best && best->count == entry->count) {
8228 /* Prev == entry, not good enough, have to keep searching */
8229 if (!prev->broken && prev->count == entry->count)
8233 best = (prev->count > entry->count) ? prev : entry;
8234 else if (best->count < entry->count)
8242 static int repair_ref(struct btrfs_fs_info *info, struct btrfs_path *path,
8243 struct data_backref *dback, struct extent_entry *entry)
8245 struct btrfs_trans_handle *trans;
8246 struct btrfs_root *root;
8247 struct btrfs_file_extent_item *fi;
8248 struct extent_buffer *leaf;
8249 struct btrfs_key key;
8253 key.objectid = dback->root;
8254 key.type = BTRFS_ROOT_ITEM_KEY;
8255 key.offset = (u64)-1;
8256 root = btrfs_read_fs_root(info, &key);
8258 fprintf(stderr, "Couldn't find root for our ref\n");
8263 * The backref points to the original offset of the extent if it was
8264 * split, so we need to search down to the offset we have and then walk
8265 * forward until we find the backref we're looking for.
8267 key.objectid = dback->owner;
8268 key.type = BTRFS_EXTENT_DATA_KEY;
8269 key.offset = dback->offset;
8270 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8272 fprintf(stderr, "Error looking up ref %d\n", ret);
8277 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
8278 ret = btrfs_next_leaf(root, path);
8280 fprintf(stderr, "Couldn't find our ref, next\n");
8284 leaf = path->nodes[0];
8285 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
8286 if (key.objectid != dback->owner ||
8287 key.type != BTRFS_EXTENT_DATA_KEY) {
8288 fprintf(stderr, "Couldn't find our ref, search\n");
8291 fi = btrfs_item_ptr(leaf, path->slots[0],
8292 struct btrfs_file_extent_item);
8293 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
8294 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
8296 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
8301 btrfs_release_path(path);
8303 trans = btrfs_start_transaction(root, 1);
8305 return PTR_ERR(trans);
8308 * Ok we have the key of the file extent we want to fix, now we can cow
8309 * down to the thing and fix it.
8311 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
8313 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
8314 key.objectid, key.type, key.offset, ret);
8318 fprintf(stderr, "Well that's odd, we just found this key "
8319 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
8324 leaf = path->nodes[0];
8325 fi = btrfs_item_ptr(leaf, path->slots[0],
8326 struct btrfs_file_extent_item);
8328 if (btrfs_file_extent_compression(leaf, fi) &&
8329 dback->disk_bytenr != entry->bytenr) {
8330 fprintf(stderr, "Ref doesn't match the record start and is "
8331 "compressed, please take a btrfs-image of this file "
8332 "system and send it to a btrfs developer so they can "
8333 "complete this functionality for bytenr %Lu\n",
8334 dback->disk_bytenr);
8339 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
8340 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8341 } else if (dback->disk_bytenr > entry->bytenr) {
8342 u64 off_diff, offset;
8344 off_diff = dback->disk_bytenr - entry->bytenr;
8345 offset = btrfs_file_extent_offset(leaf, fi);
8346 if (dback->disk_bytenr + offset +
8347 btrfs_file_extent_num_bytes(leaf, fi) >
8348 entry->bytenr + entry->bytes) {
8349 fprintf(stderr, "Ref is past the entry end, please "
8350 "take a btrfs-image of this file system and "
8351 "send it to a btrfs developer, ref %Lu\n",
8352 dback->disk_bytenr);
8357 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8358 btrfs_set_file_extent_offset(leaf, fi, offset);
8359 } else if (dback->disk_bytenr < entry->bytenr) {
8362 offset = btrfs_file_extent_offset(leaf, fi);
8363 if (dback->disk_bytenr + offset < entry->bytenr) {
8364 fprintf(stderr, "Ref is before the entry start, please"
8365 " take a btrfs-image of this file system and "
8366 "send it to a btrfs developer, ref %Lu\n",
8367 dback->disk_bytenr);
8372 offset += dback->disk_bytenr;
8373 offset -= entry->bytenr;
8374 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
8375 btrfs_set_file_extent_offset(leaf, fi, offset);
8378 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
8381 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
8382 * only do this if we aren't using compression, otherwise it's a
8385 if (!btrfs_file_extent_compression(leaf, fi))
8386 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
8388 printf("ram bytes may be wrong?\n");
8389 btrfs_mark_buffer_dirty(leaf);
8391 err = btrfs_commit_transaction(trans, root);
8392 btrfs_release_path(path);
8393 return ret ? ret : err;
8396 static int verify_backrefs(struct btrfs_fs_info *info, struct btrfs_path *path,
8397 struct extent_record *rec)
8399 struct extent_backref *back;
8400 struct data_backref *dback;
8401 struct extent_entry *entry, *best = NULL;
8404 int broken_entries = 0;
8409 * Metadata is easy and the backrefs should always agree on bytenr and
8410 * size, if not we've got bigger issues.
8415 list_for_each_entry(back, &rec->backrefs, list) {
8416 if (back->full_backref || !back->is_data)
8419 dback = to_data_backref(back);
8422 * We only pay attention to backrefs that we found a real
8425 if (dback->found_ref == 0)
8429 * For now we only catch when the bytes don't match, not the
8430 * bytenr. We can easily do this at the same time, but I want
8431 * to have a fs image to test on before we just add repair
8432 * functionality willy-nilly so we know we won't screw up the
8436 entry = find_entry(&entries, dback->disk_bytenr,
8439 entry = malloc(sizeof(struct extent_entry));
8444 memset(entry, 0, sizeof(*entry));
8445 entry->bytenr = dback->disk_bytenr;
8446 entry->bytes = dback->bytes;
8447 list_add_tail(&entry->list, &entries);
8452 * If we only have on entry we may think the entries agree when
8453 * in reality they don't so we have to do some extra checking.
8455 if (dback->disk_bytenr != rec->start ||
8456 dback->bytes != rec->nr || back->broken)
8467 /* Yay all the backrefs agree, carry on good sir */
8468 if (nr_entries <= 1 && !mismatch)
8471 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
8472 "%Lu\n", rec->start);
8475 * First we want to see if the backrefs can agree amongst themselves who
8476 * is right, so figure out which one of the entries has the highest
8479 best = find_most_right_entry(&entries);
8482 * Ok so we may have an even split between what the backrefs think, so
8483 * this is where we use the extent ref to see what it thinks.
8486 entry = find_entry(&entries, rec->start, rec->nr);
8487 if (!entry && (!broken_entries || !rec->found_rec)) {
8488 fprintf(stderr, "Backrefs don't agree with each other "
8489 "and extent record doesn't agree with anybody,"
8490 " so we can't fix bytenr %Lu bytes %Lu\n",
8491 rec->start, rec->nr);
8494 } else if (!entry) {
8496 * Ok our backrefs were broken, we'll assume this is the
8497 * correct value and add an entry for this range.
8499 entry = malloc(sizeof(struct extent_entry));
8504 memset(entry, 0, sizeof(*entry));
8505 entry->bytenr = rec->start;
8506 entry->bytes = rec->nr;
8507 list_add_tail(&entry->list, &entries);
8511 best = find_most_right_entry(&entries);
8513 fprintf(stderr, "Backrefs and extent record evenly "
8514 "split on who is right, this is going to "
8515 "require user input to fix bytenr %Lu bytes "
8516 "%Lu\n", rec->start, rec->nr);
8523 * I don't think this can happen currently as we'll abort() if we catch
8524 * this case higher up, but in case somebody removes that we still can't
8525 * deal with it properly here yet, so just bail out of that's the case.
8527 if (best->bytenr != rec->start) {
8528 fprintf(stderr, "Extent start and backref starts don't match, "
8529 "please use btrfs-image on this file system and send "
8530 "it to a btrfs developer so they can make fsck fix "
8531 "this particular case. bytenr is %Lu, bytes is %Lu\n",
8532 rec->start, rec->nr);
8538 * Ok great we all agreed on an extent record, let's go find the real
8539 * references and fix up the ones that don't match.
8541 list_for_each_entry(back, &rec->backrefs, list) {
8542 if (back->full_backref || !back->is_data)
8545 dback = to_data_backref(back);
8548 * Still ignoring backrefs that don't have a real ref attached
8551 if (dback->found_ref == 0)
8554 if (dback->bytes == best->bytes &&
8555 dback->disk_bytenr == best->bytenr)
8558 ret = repair_ref(info, path, dback, best);
8564 * Ok we messed with the actual refs, which means we need to drop our
8565 * entire cache and go back and rescan. I know this is a huge pain and
8566 * adds a lot of extra work, but it's the only way to be safe. Once all
8567 * the backrefs agree we may not need to do anything to the extent
8572 while (!list_empty(&entries)) {
8573 entry = list_entry(entries.next, struct extent_entry, list);
8574 list_del_init(&entry->list);
8580 static int process_duplicates(struct btrfs_root *root,
8581 struct cache_tree *extent_cache,
8582 struct extent_record *rec)
8584 struct extent_record *good, *tmp;
8585 struct cache_extent *cache;
8589 * If we found a extent record for this extent then return, or if we
8590 * have more than one duplicate we are likely going to need to delete
8593 if (rec->found_rec || rec->num_duplicates > 1)
8596 /* Shouldn't happen but just in case */
8597 BUG_ON(!rec->num_duplicates);
8600 * So this happens if we end up with a backref that doesn't match the
8601 * actual extent entry. So either the backref is bad or the extent
8602 * entry is bad. Either way we want to have the extent_record actually
8603 * reflect what we found in the extent_tree, so we need to take the
8604 * duplicate out and use that as the extent_record since the only way we
8605 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
8607 remove_cache_extent(extent_cache, &rec->cache);
8609 good = to_extent_record(rec->dups.next);
8610 list_del_init(&good->list);
8611 INIT_LIST_HEAD(&good->backrefs);
8612 INIT_LIST_HEAD(&good->dups);
8613 good->cache.start = good->start;
8614 good->cache.size = good->nr;
8615 good->content_checked = 0;
8616 good->owner_ref_checked = 0;
8617 good->num_duplicates = 0;
8618 good->refs = rec->refs;
8619 list_splice_init(&rec->backrefs, &good->backrefs);
8621 cache = lookup_cache_extent(extent_cache, good->start,
8625 tmp = container_of(cache, struct extent_record, cache);
8628 * If we find another overlapping extent and it's found_rec is
8629 * set then it's a duplicate and we need to try and delete
8632 if (tmp->found_rec || tmp->num_duplicates > 0) {
8633 if (list_empty(&good->list))
8634 list_add_tail(&good->list,
8635 &duplicate_extents);
8636 good->num_duplicates += tmp->num_duplicates + 1;
8637 list_splice_init(&tmp->dups, &good->dups);
8638 list_del_init(&tmp->list);
8639 list_add_tail(&tmp->list, &good->dups);
8640 remove_cache_extent(extent_cache, &tmp->cache);
8645 * Ok we have another non extent item backed extent rec, so lets
8646 * just add it to this extent and carry on like we did above.
8648 good->refs += tmp->refs;
8649 list_splice_init(&tmp->backrefs, &good->backrefs);
8650 remove_cache_extent(extent_cache, &tmp->cache);
8653 ret = insert_cache_extent(extent_cache, &good->cache);
8656 return good->num_duplicates ? 0 : 1;
8659 static int delete_duplicate_records(struct btrfs_root *root,
8660 struct extent_record *rec)
8662 struct btrfs_trans_handle *trans;
8663 LIST_HEAD(delete_list);
8664 struct btrfs_path path;
8665 struct extent_record *tmp, *good, *n;
8668 struct btrfs_key key;
8670 btrfs_init_path(&path);
8673 /* Find the record that covers all of the duplicates. */
8674 list_for_each_entry(tmp, &rec->dups, list) {
8675 if (good->start < tmp->start)
8677 if (good->nr > tmp->nr)
8680 if (tmp->start + tmp->nr < good->start + good->nr) {
8681 fprintf(stderr, "Ok we have overlapping extents that "
8682 "aren't completely covered by each other, this "
8683 "is going to require more careful thought. "
8684 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
8685 tmp->start, tmp->nr, good->start, good->nr);
8692 list_add_tail(&rec->list, &delete_list);
8694 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
8697 list_move_tail(&tmp->list, &delete_list);
8700 root = root->fs_info->extent_root;
8701 trans = btrfs_start_transaction(root, 1);
8702 if (IS_ERR(trans)) {
8703 ret = PTR_ERR(trans);
8707 list_for_each_entry(tmp, &delete_list, list) {
8708 if (tmp->found_rec == 0)
8710 key.objectid = tmp->start;
8711 key.type = BTRFS_EXTENT_ITEM_KEY;
8712 key.offset = tmp->nr;
8714 /* Shouldn't happen but just in case */
8715 if (tmp->metadata) {
8716 fprintf(stderr, "Well this shouldn't happen, extent "
8717 "record overlaps but is metadata? "
8718 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
8722 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
8728 ret = btrfs_del_item(trans, root, &path);
8731 btrfs_release_path(&path);
8734 err = btrfs_commit_transaction(trans, root);
8738 while (!list_empty(&delete_list)) {
8739 tmp = to_extent_record(delete_list.next);
8740 list_del_init(&tmp->list);
8746 while (!list_empty(&rec->dups)) {
8747 tmp = to_extent_record(rec->dups.next);
8748 list_del_init(&tmp->list);
8752 btrfs_release_path(&path);
8754 if (!ret && !nr_del)
8755 rec->num_duplicates = 0;
8757 return ret ? ret : nr_del;
8760 static int find_possible_backrefs(struct btrfs_fs_info *info,
8761 struct btrfs_path *path,
8762 struct cache_tree *extent_cache,
8763 struct extent_record *rec)
8765 struct btrfs_root *root;
8766 struct extent_backref *back;
8767 struct data_backref *dback;
8768 struct cache_extent *cache;
8769 struct btrfs_file_extent_item *fi;
8770 struct btrfs_key key;
8774 list_for_each_entry(back, &rec->backrefs, list) {
8775 /* Don't care about full backrefs (poor unloved backrefs) */
8776 if (back->full_backref || !back->is_data)
8779 dback = to_data_backref(back);
8781 /* We found this one, we don't need to do a lookup */
8782 if (dback->found_ref)
8785 key.objectid = dback->root;
8786 key.type = BTRFS_ROOT_ITEM_KEY;
8787 key.offset = (u64)-1;
8789 root = btrfs_read_fs_root(info, &key);
8791 /* No root, definitely a bad ref, skip */
8792 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
8794 /* Other err, exit */
8796 return PTR_ERR(root);
8798 key.objectid = dback->owner;
8799 key.type = BTRFS_EXTENT_DATA_KEY;
8800 key.offset = dback->offset;
8801 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8803 btrfs_release_path(path);
8806 /* Didn't find it, we can carry on */
8811 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
8812 struct btrfs_file_extent_item);
8813 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
8814 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
8815 btrfs_release_path(path);
8816 cache = lookup_cache_extent(extent_cache, bytenr, 1);
8818 struct extent_record *tmp;
8819 tmp = container_of(cache, struct extent_record, cache);
8822 * If we found an extent record for the bytenr for this
8823 * particular backref then we can't add it to our
8824 * current extent record. We only want to add backrefs
8825 * that don't have a corresponding extent item in the
8826 * extent tree since they likely belong to this record
8827 * and we need to fix it if it doesn't match bytenrs.
8833 dback->found_ref += 1;
8834 dback->disk_bytenr = bytenr;
8835 dback->bytes = bytes;
8838 * Set this so the verify backref code knows not to trust the
8839 * values in this backref.
8848 * Record orphan data ref into corresponding root.
8850 * Return 0 if the extent item contains data ref and recorded.
8851 * Return 1 if the extent item contains no useful data ref
8852 * On that case, it may contains only shared_dataref or metadata backref
8853 * or the file extent exists(this should be handled by the extent bytenr
8855 * Return <0 if something goes wrong.
8857 static int record_orphan_data_extents(struct btrfs_fs_info *fs_info,
8858 struct extent_record *rec)
8860 struct btrfs_key key;
8861 struct btrfs_root *dest_root;
8862 struct extent_backref *back;
8863 struct data_backref *dback;
8864 struct orphan_data_extent *orphan;
8865 struct btrfs_path path;
8866 int recorded_data_ref = 0;
8871 btrfs_init_path(&path);
8872 list_for_each_entry(back, &rec->backrefs, list) {
8873 if (back->full_backref || !back->is_data ||
8874 !back->found_extent_tree)
8876 dback = to_data_backref(back);
8877 if (dback->found_ref)
8879 key.objectid = dback->root;
8880 key.type = BTRFS_ROOT_ITEM_KEY;
8881 key.offset = (u64)-1;
8883 dest_root = btrfs_read_fs_root(fs_info, &key);
8885 /* For non-exist root we just skip it */
8886 if (IS_ERR(dest_root) || !dest_root)
8889 key.objectid = dback->owner;
8890 key.type = BTRFS_EXTENT_DATA_KEY;
8891 key.offset = dback->offset;
8893 ret = btrfs_search_slot(NULL, dest_root, &key, &path, 0, 0);
8894 btrfs_release_path(&path);
8896 * For ret < 0, it's OK since the fs-tree may be corrupted,
8897 * we need to record it for inode/file extent rebuild.
8898 * For ret > 0, we record it only for file extent rebuild.
8899 * For ret == 0, the file extent exists but only bytenr
8900 * mismatch, let the original bytenr fix routine to handle,
8906 orphan = malloc(sizeof(*orphan));
8911 INIT_LIST_HEAD(&orphan->list);
8912 orphan->root = dback->root;
8913 orphan->objectid = dback->owner;
8914 orphan->offset = dback->offset;
8915 orphan->disk_bytenr = rec->cache.start;
8916 orphan->disk_len = rec->cache.size;
8917 list_add(&dest_root->orphan_data_extents, &orphan->list);
8918 recorded_data_ref = 1;
8921 btrfs_release_path(&path);
8923 return !recorded_data_ref;
8929 * when an incorrect extent item is found, this will delete
8930 * all of the existing entries for it and recreate them
8931 * based on what the tree scan found.
8933 static int fixup_extent_refs(struct btrfs_fs_info *info,
8934 struct cache_tree *extent_cache,
8935 struct extent_record *rec)
8937 struct btrfs_trans_handle *trans = NULL;
8939 struct btrfs_path path;
8940 struct list_head *cur = rec->backrefs.next;
8941 struct cache_extent *cache;
8942 struct extent_backref *back;
8946 if (rec->flag_block_full_backref)
8947 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
8949 btrfs_init_path(&path);
8950 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
8952 * Sometimes the backrefs themselves are so broken they don't
8953 * get attached to any meaningful rec, so first go back and
8954 * check any of our backrefs that we couldn't find and throw
8955 * them into the list if we find the backref so that
8956 * verify_backrefs can figure out what to do.
8958 ret = find_possible_backrefs(info, &path, extent_cache, rec);
8963 /* step one, make sure all of the backrefs agree */
8964 ret = verify_backrefs(info, &path, rec);
8968 trans = btrfs_start_transaction(info->extent_root, 1);
8969 if (IS_ERR(trans)) {
8970 ret = PTR_ERR(trans);
8974 /* step two, delete all the existing records */
8975 ret = delete_extent_records(trans, info->extent_root, &path,
8981 /* was this block corrupt? If so, don't add references to it */
8982 cache = lookup_cache_extent(info->corrupt_blocks,
8983 rec->start, rec->max_size);
8989 /* step three, recreate all the refs we did find */
8990 while(cur != &rec->backrefs) {
8991 back = to_extent_backref(cur);
8995 * if we didn't find any references, don't create a
8998 if (!back->found_ref)
9001 rec->bad_full_backref = 0;
9002 ret = record_extent(trans, info, &path, rec, back, allocated, flags);
9010 int err = btrfs_commit_transaction(trans, info->extent_root);
9016 fprintf(stderr, "Repaired extent references for %llu\n",
9017 (unsigned long long)rec->start);
9019 btrfs_release_path(&path);
9023 static int fixup_extent_flags(struct btrfs_fs_info *fs_info,
9024 struct extent_record *rec)
9026 struct btrfs_trans_handle *trans;
9027 struct btrfs_root *root = fs_info->extent_root;
9028 struct btrfs_path path;
9029 struct btrfs_extent_item *ei;
9030 struct btrfs_key key;
9034 key.objectid = rec->start;
9035 if (rec->metadata) {
9036 key.type = BTRFS_METADATA_ITEM_KEY;
9037 key.offset = rec->info_level;
9039 key.type = BTRFS_EXTENT_ITEM_KEY;
9040 key.offset = rec->max_size;
9043 trans = btrfs_start_transaction(root, 0);
9045 return PTR_ERR(trans);
9047 btrfs_init_path(&path);
9048 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
9050 btrfs_release_path(&path);
9051 btrfs_commit_transaction(trans, root);
9054 fprintf(stderr, "Didn't find extent for %llu\n",
9055 (unsigned long long)rec->start);
9056 btrfs_release_path(&path);
9057 btrfs_commit_transaction(trans, root);
9061 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
9062 struct btrfs_extent_item);
9063 flags = btrfs_extent_flags(path.nodes[0], ei);
9064 if (rec->flag_block_full_backref) {
9065 fprintf(stderr, "setting full backref on %llu\n",
9066 (unsigned long long)key.objectid);
9067 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
9069 fprintf(stderr, "clearing full backref on %llu\n",
9070 (unsigned long long)key.objectid);
9071 flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
9073 btrfs_set_extent_flags(path.nodes[0], ei, flags);
9074 btrfs_mark_buffer_dirty(path.nodes[0]);
9075 btrfs_release_path(&path);
9076 ret = btrfs_commit_transaction(trans, root);
9078 fprintf(stderr, "Repaired extent flags for %llu\n",
9079 (unsigned long long)rec->start);
9084 /* right now we only prune from the extent allocation tree */
9085 static int prune_one_block(struct btrfs_trans_handle *trans,
9086 struct btrfs_fs_info *info,
9087 struct btrfs_corrupt_block *corrupt)
9090 struct btrfs_path path;
9091 struct extent_buffer *eb;
9095 int level = corrupt->level + 1;
9097 btrfs_init_path(&path);
9099 /* we want to stop at the parent to our busted block */
9100 path.lowest_level = level;
9102 ret = btrfs_search_slot(trans, info->extent_root,
9103 &corrupt->key, &path, -1, 1);
9108 eb = path.nodes[level];
9115 * hopefully the search gave us the block we want to prune,
9116 * lets try that first
9118 slot = path.slots[level];
9119 found = btrfs_node_blockptr(eb, slot);
9120 if (found == corrupt->cache.start)
9123 nritems = btrfs_header_nritems(eb);
9125 /* the search failed, lets scan this node and hope we find it */
9126 for (slot = 0; slot < nritems; slot++) {
9127 found = btrfs_node_blockptr(eb, slot);
9128 if (found == corrupt->cache.start)
9132 * we couldn't find the bad block. TODO, search all the nodes for pointers
9135 if (eb == info->extent_root->node) {
9140 btrfs_release_path(&path);
9145 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
9146 ret = btrfs_del_ptr(info->extent_root, &path, level, slot);
9149 btrfs_release_path(&path);
9153 static int prune_corrupt_blocks(struct btrfs_fs_info *info)
9155 struct btrfs_trans_handle *trans = NULL;
9156 struct cache_extent *cache;
9157 struct btrfs_corrupt_block *corrupt;
9160 cache = search_cache_extent(info->corrupt_blocks, 0);
9164 trans = btrfs_start_transaction(info->extent_root, 1);
9166 return PTR_ERR(trans);
9168 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
9169 prune_one_block(trans, info, corrupt);
9170 remove_cache_extent(info->corrupt_blocks, cache);
9173 return btrfs_commit_transaction(trans, info->extent_root);
9177 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
9179 struct btrfs_block_group_cache *cache;
9184 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
9185 &start, &end, EXTENT_DIRTY);
9188 clear_extent_dirty(&fs_info->free_space_cache, start, end);
9193 cache = btrfs_lookup_first_block_group(fs_info, start);
9198 start = cache->key.objectid + cache->key.offset;
9202 static int check_extent_refs(struct btrfs_root *root,
9203 struct cache_tree *extent_cache)
9205 struct extent_record *rec;
9206 struct cache_extent *cache;
9212 * if we're doing a repair, we have to make sure
9213 * we don't allocate from the problem extents.
9214 * In the worst case, this will be all the
9217 cache = search_cache_extent(extent_cache, 0);
9219 rec = container_of(cache, struct extent_record, cache);
9220 set_extent_dirty(root->fs_info->excluded_extents,
9222 rec->start + rec->max_size - 1);
9223 cache = next_cache_extent(cache);
9226 /* pin down all the corrupted blocks too */
9227 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
9229 set_extent_dirty(root->fs_info->excluded_extents,
9231 cache->start + cache->size - 1);
9232 cache = next_cache_extent(cache);
9234 prune_corrupt_blocks(root->fs_info);
9235 reset_cached_block_groups(root->fs_info);
9238 reset_cached_block_groups(root->fs_info);
9241 * We need to delete any duplicate entries we find first otherwise we
9242 * could mess up the extent tree when we have backrefs that actually
9243 * belong to a different extent item and not the weird duplicate one.
9245 while (repair && !list_empty(&duplicate_extents)) {
9246 rec = to_extent_record(duplicate_extents.next);
9247 list_del_init(&rec->list);
9249 /* Sometimes we can find a backref before we find an actual
9250 * extent, so we need to process it a little bit to see if there
9251 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
9252 * if this is a backref screwup. If we need to delete stuff
9253 * process_duplicates() will return 0, otherwise it will return
9256 if (process_duplicates(root, extent_cache, rec))
9258 ret = delete_duplicate_records(root, rec);
9262 * delete_duplicate_records will return the number of entries
9263 * deleted, so if it's greater than 0 then we know we actually
9264 * did something and we need to remove.
9277 cache = search_cache_extent(extent_cache, 0);
9280 rec = container_of(cache, struct extent_record, cache);
9281 if (rec->num_duplicates) {
9282 fprintf(stderr, "extent item %llu has multiple extent "
9283 "items\n", (unsigned long long)rec->start);
9287 if (rec->refs != rec->extent_item_refs) {
9288 fprintf(stderr, "ref mismatch on [%llu %llu] ",
9289 (unsigned long long)rec->start,
9290 (unsigned long long)rec->nr);
9291 fprintf(stderr, "extent item %llu, found %llu\n",
9292 (unsigned long long)rec->extent_item_refs,
9293 (unsigned long long)rec->refs);
9294 ret = record_orphan_data_extents(root->fs_info, rec);
9300 if (all_backpointers_checked(rec, 1)) {
9301 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
9302 (unsigned long long)rec->start,
9303 (unsigned long long)rec->nr);
9307 if (!rec->owner_ref_checked) {
9308 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
9309 (unsigned long long)rec->start,
9310 (unsigned long long)rec->nr);
9315 if (repair && fix) {
9316 ret = fixup_extent_refs(root->fs_info, extent_cache, rec);
9322 if (rec->bad_full_backref) {
9323 fprintf(stderr, "bad full backref, on [%llu]\n",
9324 (unsigned long long)rec->start);
9326 ret = fixup_extent_flags(root->fs_info, rec);
9334 * Although it's not a extent ref's problem, we reuse this
9335 * routine for error reporting.
9336 * No repair function yet.
9338 if (rec->crossing_stripes) {
9340 "bad metadata [%llu, %llu) crossing stripe boundary\n",
9341 rec->start, rec->start + rec->max_size);
9345 if (rec->wrong_chunk_type) {
9347 "bad extent [%llu, %llu), type mismatch with chunk\n",
9348 rec->start, rec->start + rec->max_size);
9352 remove_cache_extent(extent_cache, cache);
9353 free_all_extent_backrefs(rec);
9354 if (!init_extent_tree && repair && (!cur_err || fix))
9355 clear_extent_dirty(root->fs_info->excluded_extents,
9357 rec->start + rec->max_size - 1);
9362 if (ret && ret != -EAGAIN) {
9363 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
9366 struct btrfs_trans_handle *trans;
9368 root = root->fs_info->extent_root;
9369 trans = btrfs_start_transaction(root, 1);
9370 if (IS_ERR(trans)) {
9371 ret = PTR_ERR(trans);
9375 btrfs_fix_block_accounting(trans, root);
9376 ret = btrfs_commit_transaction(trans, root);
9385 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
9389 if (type & BTRFS_BLOCK_GROUP_RAID0) {
9390 stripe_size = length;
9391 stripe_size /= num_stripes;
9392 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
9393 stripe_size = length * 2;
9394 stripe_size /= num_stripes;
9395 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
9396 stripe_size = length;
9397 stripe_size /= (num_stripes - 1);
9398 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
9399 stripe_size = length;
9400 stripe_size /= (num_stripes - 2);
9402 stripe_size = length;
9408 * Check the chunk with its block group/dev list ref:
9409 * Return 0 if all refs seems valid.
9410 * Return 1 if part of refs seems valid, need later check for rebuild ref
9411 * like missing block group and needs to search extent tree to rebuild them.
9412 * Return -1 if essential refs are missing and unable to rebuild.
9414 static int check_chunk_refs(struct chunk_record *chunk_rec,
9415 struct block_group_tree *block_group_cache,
9416 struct device_extent_tree *dev_extent_cache,
9419 struct cache_extent *block_group_item;
9420 struct block_group_record *block_group_rec;
9421 struct cache_extent *dev_extent_item;
9422 struct device_extent_record *dev_extent_rec;
9426 int metadump_v2 = 0;
9430 block_group_item = lookup_cache_extent(&block_group_cache->tree,
9433 if (block_group_item) {
9434 block_group_rec = container_of(block_group_item,
9435 struct block_group_record,
9437 if (chunk_rec->length != block_group_rec->offset ||
9438 chunk_rec->offset != block_group_rec->objectid ||
9440 chunk_rec->type_flags != block_group_rec->flags)) {
9443 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
9444 chunk_rec->objectid,
9449 chunk_rec->type_flags,
9450 block_group_rec->objectid,
9451 block_group_rec->type,
9452 block_group_rec->offset,
9453 block_group_rec->offset,
9454 block_group_rec->objectid,
9455 block_group_rec->flags);
9458 list_del_init(&block_group_rec->list);
9459 chunk_rec->bg_rec = block_group_rec;
9464 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
9465 chunk_rec->objectid,
9470 chunk_rec->type_flags);
9477 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
9478 chunk_rec->num_stripes);
9479 for (i = 0; i < chunk_rec->num_stripes; ++i) {
9480 devid = chunk_rec->stripes[i].devid;
9481 offset = chunk_rec->stripes[i].offset;
9482 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
9483 devid, offset, length);
9484 if (dev_extent_item) {
9485 dev_extent_rec = container_of(dev_extent_item,
9486 struct device_extent_record,
9488 if (dev_extent_rec->objectid != devid ||
9489 dev_extent_rec->offset != offset ||
9490 dev_extent_rec->chunk_offset != chunk_rec->offset ||
9491 dev_extent_rec->length != length) {
9494 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
9495 chunk_rec->objectid,
9498 chunk_rec->stripes[i].devid,
9499 chunk_rec->stripes[i].offset,
9500 dev_extent_rec->objectid,
9501 dev_extent_rec->offset,
9502 dev_extent_rec->length);
9505 list_move(&dev_extent_rec->chunk_list,
9506 &chunk_rec->dextents);
9511 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
9512 chunk_rec->objectid,
9515 chunk_rec->stripes[i].devid,
9516 chunk_rec->stripes[i].offset);
9523 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
9524 int check_chunks(struct cache_tree *chunk_cache,
9525 struct block_group_tree *block_group_cache,
9526 struct device_extent_tree *dev_extent_cache,
9527 struct list_head *good, struct list_head *bad,
9528 struct list_head *rebuild, int silent)
9530 struct cache_extent *chunk_item;
9531 struct chunk_record *chunk_rec;
9532 struct block_group_record *bg_rec;
9533 struct device_extent_record *dext_rec;
9537 chunk_item = first_cache_extent(chunk_cache);
9538 while (chunk_item) {
9539 chunk_rec = container_of(chunk_item, struct chunk_record,
9541 err = check_chunk_refs(chunk_rec, block_group_cache,
9542 dev_extent_cache, silent);
9545 if (err == 0 && good)
9546 list_add_tail(&chunk_rec->list, good);
9547 if (err > 0 && rebuild)
9548 list_add_tail(&chunk_rec->list, rebuild);
9550 list_add_tail(&chunk_rec->list, bad);
9551 chunk_item = next_cache_extent(chunk_item);
9554 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
9557 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
9565 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
9569 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
9580 static int check_device_used(struct device_record *dev_rec,
9581 struct device_extent_tree *dext_cache)
9583 struct cache_extent *cache;
9584 struct device_extent_record *dev_extent_rec;
9587 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
9589 dev_extent_rec = container_of(cache,
9590 struct device_extent_record,
9592 if (dev_extent_rec->objectid != dev_rec->devid)
9595 list_del_init(&dev_extent_rec->device_list);
9596 total_byte += dev_extent_rec->length;
9597 cache = next_cache_extent(cache);
9600 if (total_byte != dev_rec->byte_used) {
9602 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
9603 total_byte, dev_rec->byte_used, dev_rec->objectid,
9604 dev_rec->type, dev_rec->offset);
9611 /* check btrfs_dev_item -> btrfs_dev_extent */
9612 static int check_devices(struct rb_root *dev_cache,
9613 struct device_extent_tree *dev_extent_cache)
9615 struct rb_node *dev_node;
9616 struct device_record *dev_rec;
9617 struct device_extent_record *dext_rec;
9621 dev_node = rb_first(dev_cache);
9623 dev_rec = container_of(dev_node, struct device_record, node);
9624 err = check_device_used(dev_rec, dev_extent_cache);
9628 dev_node = rb_next(dev_node);
9630 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
9633 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
9634 dext_rec->objectid, dext_rec->offset, dext_rec->length);
9641 static int add_root_item_to_list(struct list_head *head,
9642 u64 objectid, u64 bytenr, u64 last_snapshot,
9643 u8 level, u8 drop_level,
9644 int level_size, struct btrfs_key *drop_key)
9647 struct root_item_record *ri_rec;
9648 ri_rec = malloc(sizeof(*ri_rec));
9651 ri_rec->bytenr = bytenr;
9652 ri_rec->objectid = objectid;
9653 ri_rec->level = level;
9654 ri_rec->level_size = level_size;
9655 ri_rec->drop_level = drop_level;
9656 ri_rec->last_snapshot = last_snapshot;
9658 memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
9659 list_add_tail(&ri_rec->list, head);
9664 static void free_root_item_list(struct list_head *list)
9666 struct root_item_record *ri_rec;
9668 while (!list_empty(list)) {
9669 ri_rec = list_first_entry(list, struct root_item_record,
9671 list_del_init(&ri_rec->list);
9676 static int deal_root_from_list(struct list_head *list,
9677 struct btrfs_root *root,
9678 struct block_info *bits,
9680 struct cache_tree *pending,
9681 struct cache_tree *seen,
9682 struct cache_tree *reada,
9683 struct cache_tree *nodes,
9684 struct cache_tree *extent_cache,
9685 struct cache_tree *chunk_cache,
9686 struct rb_root *dev_cache,
9687 struct block_group_tree *block_group_cache,
9688 struct device_extent_tree *dev_extent_cache)
9693 while (!list_empty(list)) {
9694 struct root_item_record *rec;
9695 struct extent_buffer *buf;
9696 rec = list_entry(list->next,
9697 struct root_item_record, list);
9699 buf = read_tree_block(root->fs_info->tree_root,
9700 rec->bytenr, rec->level_size, 0);
9701 if (!extent_buffer_uptodate(buf)) {
9702 free_extent_buffer(buf);
9706 ret = add_root_to_pending(buf, extent_cache, pending,
9707 seen, nodes, rec->objectid);
9711 * To rebuild extent tree, we need deal with snapshot
9712 * one by one, otherwise we deal with node firstly which
9713 * can maximize readahead.
9716 ret = run_next_block(root, bits, bits_nr, &last,
9717 pending, seen, reada, nodes,
9718 extent_cache, chunk_cache,
9719 dev_cache, block_group_cache,
9720 dev_extent_cache, rec);
9724 free_extent_buffer(buf);
9725 list_del(&rec->list);
9731 ret = run_next_block(root, bits, bits_nr, &last, pending, seen,
9732 reada, nodes, extent_cache, chunk_cache,
9733 dev_cache, block_group_cache,
9734 dev_extent_cache, NULL);
9744 static int check_chunks_and_extents(struct btrfs_root *root)
9746 struct rb_root dev_cache;
9747 struct cache_tree chunk_cache;
9748 struct block_group_tree block_group_cache;
9749 struct device_extent_tree dev_extent_cache;
9750 struct cache_tree extent_cache;
9751 struct cache_tree seen;
9752 struct cache_tree pending;
9753 struct cache_tree reada;
9754 struct cache_tree nodes;
9755 struct extent_io_tree excluded_extents;
9756 struct cache_tree corrupt_blocks;
9757 struct btrfs_path path;
9758 struct btrfs_key key;
9759 struct btrfs_key found_key;
9761 struct block_info *bits;
9763 struct extent_buffer *leaf;
9765 struct btrfs_root_item ri;
9766 struct list_head dropping_trees;
9767 struct list_head normal_trees;
9768 struct btrfs_root *root1;
9773 dev_cache = RB_ROOT;
9774 cache_tree_init(&chunk_cache);
9775 block_group_tree_init(&block_group_cache);
9776 device_extent_tree_init(&dev_extent_cache);
9778 cache_tree_init(&extent_cache);
9779 cache_tree_init(&seen);
9780 cache_tree_init(&pending);
9781 cache_tree_init(&nodes);
9782 cache_tree_init(&reada);
9783 cache_tree_init(&corrupt_blocks);
9784 extent_io_tree_init(&excluded_extents);
9785 INIT_LIST_HEAD(&dropping_trees);
9786 INIT_LIST_HEAD(&normal_trees);
9789 root->fs_info->excluded_extents = &excluded_extents;
9790 root->fs_info->fsck_extent_cache = &extent_cache;
9791 root->fs_info->free_extent_hook = free_extent_hook;
9792 root->fs_info->corrupt_blocks = &corrupt_blocks;
9796 bits = malloc(bits_nr * sizeof(struct block_info));
9802 if (ctx.progress_enabled) {
9803 ctx.tp = TASK_EXTENTS;
9804 task_start(ctx.info);
9808 root1 = root->fs_info->tree_root;
9809 level = btrfs_header_level(root1->node);
9810 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9811 root1->node->start, 0, level, 0,
9812 root1->nodesize, NULL);
9815 root1 = root->fs_info->chunk_root;
9816 level = btrfs_header_level(root1->node);
9817 ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
9818 root1->node->start, 0, level, 0,
9819 root1->nodesize, NULL);
9822 btrfs_init_path(&path);
9825 key.type = BTRFS_ROOT_ITEM_KEY;
9826 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
9831 leaf = path.nodes[0];
9832 slot = path.slots[0];
9833 if (slot >= btrfs_header_nritems(path.nodes[0])) {
9834 ret = btrfs_next_leaf(root, &path);
9837 leaf = path.nodes[0];
9838 slot = path.slots[0];
9840 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
9841 if (found_key.type == BTRFS_ROOT_ITEM_KEY) {
9842 unsigned long offset;
9845 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
9846 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
9847 last_snapshot = btrfs_root_last_snapshot(&ri);
9848 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
9849 level = btrfs_root_level(&ri);
9850 level_size = root->nodesize;
9851 ret = add_root_item_to_list(&normal_trees,
9853 btrfs_root_bytenr(&ri),
9854 last_snapshot, level,
9855 0, level_size, NULL);
9859 level = btrfs_root_level(&ri);
9860 level_size = root->nodesize;
9861 objectid = found_key.objectid;
9862 btrfs_disk_key_to_cpu(&found_key,
9864 ret = add_root_item_to_list(&dropping_trees,
9866 btrfs_root_bytenr(&ri),
9867 last_snapshot, level,
9869 level_size, &found_key);
9876 btrfs_release_path(&path);
9879 * check_block can return -EAGAIN if it fixes something, please keep
9880 * this in mind when dealing with return values from these functions, if
9881 * we get -EAGAIN we want to fall through and restart the loop.
9883 ret = deal_root_from_list(&normal_trees, root, bits, bits_nr, &pending,
9884 &seen, &reada, &nodes, &extent_cache,
9885 &chunk_cache, &dev_cache, &block_group_cache,
9892 ret = deal_root_from_list(&dropping_trees, root, bits, bits_nr,
9893 &pending, &seen, &reada, &nodes,
9894 &extent_cache, &chunk_cache, &dev_cache,
9895 &block_group_cache, &dev_extent_cache);
9902 ret = check_chunks(&chunk_cache, &block_group_cache,
9903 &dev_extent_cache, NULL, NULL, NULL, 0);
9910 ret = check_extent_refs(root, &extent_cache);
9917 ret = check_devices(&dev_cache, &dev_extent_cache);
9922 task_stop(ctx.info);
9924 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9925 extent_io_tree_cleanup(&excluded_extents);
9926 root->fs_info->fsck_extent_cache = NULL;
9927 root->fs_info->free_extent_hook = NULL;
9928 root->fs_info->corrupt_blocks = NULL;
9929 root->fs_info->excluded_extents = NULL;
9932 free_chunk_cache_tree(&chunk_cache);
9933 free_device_cache_tree(&dev_cache);
9934 free_block_group_tree(&block_group_cache);
9935 free_device_extent_tree(&dev_extent_cache);
9936 free_extent_cache_tree(&seen);
9937 free_extent_cache_tree(&pending);
9938 free_extent_cache_tree(&reada);
9939 free_extent_cache_tree(&nodes);
9942 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
9943 free_extent_cache_tree(&seen);
9944 free_extent_cache_tree(&pending);
9945 free_extent_cache_tree(&reada);
9946 free_extent_cache_tree(&nodes);
9947 free_chunk_cache_tree(&chunk_cache);
9948 free_block_group_tree(&block_group_cache);
9949 free_device_cache_tree(&dev_cache);
9950 free_device_extent_tree(&dev_extent_cache);
9951 free_extent_record_cache(root->fs_info, &extent_cache);
9952 free_root_item_list(&normal_trees);
9953 free_root_item_list(&dropping_trees);
9954 extent_io_tree_cleanup(&excluded_extents);
9959 * Check backrefs of a tree block given by @bytenr or @eb.
9961 * @root: the root containing the @bytenr or @eb
9962 * @eb: tree block extent buffer, can be NULL
9963 * @bytenr: bytenr of the tree block to search
9964 * @level: tree level of the tree block
9965 * @owner: owner of the tree block
9967 * Return >0 for any error found and output error message
9968 * Return 0 for no error found
9970 static int check_tree_block_ref(struct btrfs_root *root,
9971 struct extent_buffer *eb, u64 bytenr,
9972 int level, u64 owner)
9974 struct btrfs_key key;
9975 struct btrfs_root *extent_root = root->fs_info->extent_root;
9976 struct btrfs_path path;
9977 struct btrfs_extent_item *ei;
9978 struct btrfs_extent_inline_ref *iref;
9979 struct extent_buffer *leaf;
9985 u32 nodesize = root->nodesize;
9988 int tree_reloc_root = 0;
9993 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID &&
9994 btrfs_header_bytenr(root->node) == bytenr)
9995 tree_reloc_root = 1;
9997 btrfs_init_path(&path);
9998 key.objectid = bytenr;
9999 if (btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
10000 key.type = BTRFS_METADATA_ITEM_KEY;
10002 key.type = BTRFS_EXTENT_ITEM_KEY;
10003 key.offset = (u64)-1;
10005 /* Search for the backref in extent tree */
10006 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10008 err |= BACKREF_MISSING;
10011 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10013 err |= BACKREF_MISSING;
10017 leaf = path.nodes[0];
10018 slot = path.slots[0];
10019 btrfs_item_key_to_cpu(leaf, &key, slot);
10021 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10023 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10024 skinny_level = (int)key.offset;
10025 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10027 struct btrfs_tree_block_info *info;
10029 info = (struct btrfs_tree_block_info *)(ei + 1);
10030 skinny_level = btrfs_tree_block_level(leaf, info);
10031 iref = (struct btrfs_extent_inline_ref *)(info + 1);
10038 if (!(btrfs_extent_flags(leaf, ei) &
10039 BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10041 "extent[%llu %u] backref type mismatch, missing bit: %llx",
10042 key.objectid, nodesize,
10043 BTRFS_EXTENT_FLAG_TREE_BLOCK);
10044 err = BACKREF_MISMATCH;
10046 header_gen = btrfs_header_generation(eb);
10047 extent_gen = btrfs_extent_generation(leaf, ei);
10048 if (header_gen != extent_gen) {
10050 "extent[%llu %u] backref generation mismatch, wanted: %llu, have: %llu",
10051 key.objectid, nodesize, header_gen,
10053 err = BACKREF_MISMATCH;
10055 if (level != skinny_level) {
10057 "extent[%llu %u] level mismatch, wanted: %u, have: %u",
10058 key.objectid, nodesize, level, skinny_level);
10059 err = BACKREF_MISMATCH;
10061 if (!is_fstree(owner) && btrfs_extent_refs(leaf, ei) != 1) {
10063 "extent[%llu %u] is referred by other roots than %llu",
10064 key.objectid, nodesize, root->objectid);
10065 err = BACKREF_MISMATCH;
10070 * Iterate the extent/metadata item to find the exact backref
10072 item_size = btrfs_item_size_nr(leaf, slot);
10073 ptr = (unsigned long)iref;
10074 end = (unsigned long)ei + item_size;
10075 while (ptr < end) {
10076 iref = (struct btrfs_extent_inline_ref *)ptr;
10077 type = btrfs_extent_inline_ref_type(leaf, iref);
10078 offset = btrfs_extent_inline_ref_offset(leaf, iref);
10080 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
10081 (offset == root->objectid || offset == owner)) {
10083 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
10085 * Backref of tree reloc root points to itself, no need
10086 * to check backref any more.
10088 if (tree_reloc_root)
10091 /* Check if the backref points to valid referencer */
10092 found_ref = !check_tree_block_ref(root, NULL,
10093 offset, level + 1, owner);
10098 ptr += btrfs_extent_inline_ref_size(type);
10102 * Inlined extent item doesn't have what we need, check
10103 * TREE_BLOCK_REF_KEY
10106 btrfs_release_path(&path);
10107 key.objectid = bytenr;
10108 key.type = BTRFS_TREE_BLOCK_REF_KEY;
10109 key.offset = root->objectid;
10111 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10116 err |= BACKREF_MISSING;
10118 btrfs_release_path(&path);
10119 if (eb && (err & BACKREF_MISSING))
10120 error("extent[%llu %u] backref lost (owner: %llu, level: %u)",
10121 bytenr, nodesize, owner, level);
10126 * Check EXTENT_DATA item, mainly for its dbackref in extent tree
10128 * Return >0 any error found and output error message
10129 * Return 0 for no error found
10131 static int check_extent_data_item(struct btrfs_root *root,
10132 struct extent_buffer *eb, int slot)
10134 struct btrfs_file_extent_item *fi;
10135 struct btrfs_path path;
10136 struct btrfs_root *extent_root = root->fs_info->extent_root;
10137 struct btrfs_key fi_key;
10138 struct btrfs_key dbref_key;
10139 struct extent_buffer *leaf;
10140 struct btrfs_extent_item *ei;
10141 struct btrfs_extent_inline_ref *iref;
10142 struct btrfs_extent_data_ref *dref;
10145 u64 disk_num_bytes;
10146 u64 extent_num_bytes;
10153 int found_dbackref = 0;
10157 btrfs_item_key_to_cpu(eb, &fi_key, slot);
10158 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
10160 /* Nothing to check for hole and inline data extents */
10161 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE ||
10162 btrfs_file_extent_disk_bytenr(eb, fi) == 0)
10165 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
10166 disk_num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
10167 extent_num_bytes = btrfs_file_extent_num_bytes(eb, fi);
10169 /* Check unaligned disk_num_bytes and num_bytes */
10170 if (!IS_ALIGNED(disk_num_bytes, root->sectorsize)) {
10172 "file extent [%llu, %llu] has unaligned disk num bytes: %llu, should be aligned to %u",
10173 fi_key.objectid, fi_key.offset, disk_num_bytes,
10175 err |= BYTES_UNALIGNED;
10177 data_bytes_allocated += disk_num_bytes;
10179 if (!IS_ALIGNED(extent_num_bytes, root->sectorsize)) {
10181 "file extent [%llu, %llu] has unaligned num bytes: %llu, should be aligned to %u",
10182 fi_key.objectid, fi_key.offset, extent_num_bytes,
10184 err |= BYTES_UNALIGNED;
10186 data_bytes_referenced += extent_num_bytes;
10188 owner = btrfs_header_owner(eb);
10190 /* Check the extent item of the file extent in extent tree */
10191 btrfs_init_path(&path);
10192 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10193 dbref_key.type = BTRFS_EXTENT_ITEM_KEY;
10194 dbref_key.offset = btrfs_file_extent_disk_num_bytes(eb, fi);
10196 ret = btrfs_search_slot(NULL, extent_root, &dbref_key, &path, 0, 0);
10198 err |= BACKREF_MISSING;
10202 leaf = path.nodes[0];
10203 slot = path.slots[0];
10204 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
10206 extent_flags = btrfs_extent_flags(leaf, ei);
10208 if (!(extent_flags & BTRFS_EXTENT_FLAG_DATA)) {
10210 "extent[%llu %llu] backref type mismatch, wanted bit: %llx",
10211 disk_bytenr, disk_num_bytes,
10212 BTRFS_EXTENT_FLAG_DATA);
10213 err |= BACKREF_MISMATCH;
10216 /* Check data backref inside that extent item */
10217 item_size = btrfs_item_size_nr(leaf, path.slots[0]);
10218 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
10219 ptr = (unsigned long)iref;
10220 end = (unsigned long)ei + item_size;
10221 while (ptr < end) {
10222 iref = (struct btrfs_extent_inline_ref *)ptr;
10223 type = btrfs_extent_inline_ref_type(leaf, iref);
10224 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10226 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
10227 ref_root = btrfs_extent_data_ref_root(leaf, dref);
10228 if (ref_root == owner || ref_root == root->objectid)
10229 found_dbackref = 1;
10230 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
10231 found_dbackref = !check_tree_block_ref(root, NULL,
10232 btrfs_extent_inline_ref_offset(leaf, iref),
10236 if (found_dbackref)
10238 ptr += btrfs_extent_inline_ref_size(type);
10241 /* Didn't found inlined data backref, try EXTENT_DATA_REF_KEY */
10242 if (!found_dbackref) {
10243 btrfs_release_path(&path);
10245 btrfs_init_path(&path);
10246 dbref_key.objectid = btrfs_file_extent_disk_bytenr(eb, fi);
10247 dbref_key.type = BTRFS_EXTENT_DATA_REF_KEY;
10248 dbref_key.offset = hash_extent_data_ref(root->objectid,
10249 fi_key.objectid, fi_key.offset);
10251 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
10252 &dbref_key, &path, 0, 0);
10254 found_dbackref = 1;
10257 if (!found_dbackref)
10258 err |= BACKREF_MISSING;
10260 btrfs_release_path(&path);
10261 if (err & BACKREF_MISSING) {
10262 error("data extent[%llu %llu] backref lost",
10263 disk_bytenr, disk_num_bytes);
10269 * Get real tree block level for the case like shared block
10270 * Return >= 0 as tree level
10271 * Return <0 for error
10273 static int query_tree_block_level(struct btrfs_fs_info *fs_info, u64 bytenr)
10275 struct extent_buffer *eb;
10276 struct btrfs_path path;
10277 struct btrfs_key key;
10278 struct btrfs_extent_item *ei;
10281 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10286 /* Search extent tree for extent generation and level */
10287 key.objectid = bytenr;
10288 key.type = BTRFS_METADATA_ITEM_KEY;
10289 key.offset = (u64)-1;
10291 btrfs_init_path(&path);
10292 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, &path, 0, 0);
10295 ret = btrfs_previous_extent_item(fs_info->extent_root, &path, bytenr);
10303 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10304 ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
10305 struct btrfs_extent_item);
10306 flags = btrfs_extent_flags(path.nodes[0], ei);
10307 if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
10312 /* Get transid for later read_tree_block() check */
10313 transid = btrfs_extent_generation(path.nodes[0], ei);
10315 /* Get backref level as one source */
10316 if (key.type == BTRFS_METADATA_ITEM_KEY) {
10317 backref_level = key.offset;
10319 struct btrfs_tree_block_info *info;
10321 info = (struct btrfs_tree_block_info *)(ei + 1);
10322 backref_level = btrfs_tree_block_level(path.nodes[0], info);
10324 btrfs_release_path(&path);
10326 /* Get level from tree block as an alternative source */
10327 eb = read_tree_block_fs_info(fs_info, bytenr, nodesize, transid);
10328 if (!extent_buffer_uptodate(eb)) {
10329 free_extent_buffer(eb);
10332 header_level = btrfs_header_level(eb);
10333 free_extent_buffer(eb);
10335 if (header_level != backref_level)
10337 return header_level;
10340 btrfs_release_path(&path);
10345 * Check if a tree block backref is valid (points to a valid tree block)
10346 * if level == -1, level will be resolved
10347 * Return >0 for any error found and print error message
10349 static int check_tree_block_backref(struct btrfs_fs_info *fs_info, u64 root_id,
10350 u64 bytenr, int level)
10352 struct btrfs_root *root;
10353 struct btrfs_key key;
10354 struct btrfs_path path;
10355 struct extent_buffer *eb;
10356 struct extent_buffer *node;
10357 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10361 /* Query level for level == -1 special case */
10363 level = query_tree_block_level(fs_info, bytenr);
10365 err |= REFERENCER_MISSING;
10369 key.objectid = root_id;
10370 key.type = BTRFS_ROOT_ITEM_KEY;
10371 key.offset = (u64)-1;
10373 root = btrfs_read_fs_root(fs_info, &key);
10374 if (IS_ERR(root)) {
10375 err |= REFERENCER_MISSING;
10379 /* Read out the tree block to get item/node key */
10380 eb = read_tree_block(root, bytenr, root->nodesize, 0);
10381 if (!extent_buffer_uptodate(eb)) {
10382 err |= REFERENCER_MISSING;
10383 free_extent_buffer(eb);
10387 /* Empty tree, no need to check key */
10388 if (!btrfs_header_nritems(eb) && !level) {
10389 free_extent_buffer(eb);
10394 btrfs_node_key_to_cpu(eb, &key, 0);
10396 btrfs_item_key_to_cpu(eb, &key, 0);
10398 free_extent_buffer(eb);
10400 btrfs_init_path(&path);
10401 path.lowest_level = level;
10402 /* Search with the first key, to ensure we can reach it */
10403 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10405 err |= REFERENCER_MISSING;
10409 node = path.nodes[level];
10410 if (btrfs_header_bytenr(node) != bytenr) {
10412 "extent [%llu %d] referencer bytenr mismatch, wanted: %llu, have: %llu",
10413 bytenr, nodesize, bytenr,
10414 btrfs_header_bytenr(node));
10415 err |= REFERENCER_MISMATCH;
10417 if (btrfs_header_level(node) != level) {
10419 "extent [%llu %d] referencer level mismatch, wanted: %d, have: %d",
10420 bytenr, nodesize, level,
10421 btrfs_header_level(node));
10422 err |= REFERENCER_MISMATCH;
10426 btrfs_release_path(&path);
10428 if (err & REFERENCER_MISSING) {
10430 error("extent [%llu %d] lost referencer (owner: %llu)",
10431 bytenr, nodesize, root_id);
10434 "extent [%llu %d] lost referencer (owner: %llu, level: %u)",
10435 bytenr, nodesize, root_id, level);
10442 * Check if tree block @eb is tree reloc root.
10443 * Return 0 if it's not or any problem happens
10444 * Return 1 if it's a tree reloc root
10446 static int is_tree_reloc_root(struct btrfs_fs_info *fs_info,
10447 struct extent_buffer *eb)
10449 struct btrfs_root *tree_reloc_root;
10450 struct btrfs_key key;
10451 u64 bytenr = btrfs_header_bytenr(eb);
10452 u64 owner = btrfs_header_owner(eb);
10455 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
10456 key.offset = owner;
10457 key.type = BTRFS_ROOT_ITEM_KEY;
10459 tree_reloc_root = btrfs_read_fs_root_no_cache(fs_info, &key);
10460 if (IS_ERR(tree_reloc_root))
10463 if (bytenr == btrfs_header_bytenr(tree_reloc_root->node))
10465 btrfs_free_fs_root(tree_reloc_root);
10470 * Check referencer for shared block backref
10471 * If level == -1, this function will resolve the level.
10473 static int check_shared_block_backref(struct btrfs_fs_info *fs_info,
10474 u64 parent, u64 bytenr, int level)
10476 struct extent_buffer *eb;
10477 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10479 int found_parent = 0;
10482 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10483 if (!extent_buffer_uptodate(eb))
10487 level = query_tree_block_level(fs_info, bytenr);
10491 /* It's possible it's a tree reloc root */
10492 if (parent == bytenr) {
10493 if (is_tree_reloc_root(fs_info, eb))
10498 if (level + 1 != btrfs_header_level(eb))
10501 nr = btrfs_header_nritems(eb);
10502 for (i = 0; i < nr; i++) {
10503 if (bytenr == btrfs_node_blockptr(eb, i)) {
10509 free_extent_buffer(eb);
10510 if (!found_parent) {
10512 "shared extent[%llu %u] lost its parent (parent: %llu, level: %u)",
10513 bytenr, nodesize, parent, level);
10514 return REFERENCER_MISSING;
10520 * Check referencer for normal (inlined) data ref
10521 * If len == 0, it will be resolved by searching in extent tree
10523 static int check_extent_data_backref(struct btrfs_fs_info *fs_info,
10524 u64 root_id, u64 objectid, u64 offset,
10525 u64 bytenr, u64 len, u32 count)
10527 struct btrfs_root *root;
10528 struct btrfs_root *extent_root = fs_info->extent_root;
10529 struct btrfs_key key;
10530 struct btrfs_path path;
10531 struct extent_buffer *leaf;
10532 struct btrfs_file_extent_item *fi;
10533 u32 found_count = 0;
10538 key.objectid = bytenr;
10539 key.type = BTRFS_EXTENT_ITEM_KEY;
10540 key.offset = (u64)-1;
10542 btrfs_init_path(&path);
10543 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
10546 ret = btrfs_previous_extent_item(extent_root, &path, bytenr);
10549 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10550 if (key.objectid != bytenr ||
10551 key.type != BTRFS_EXTENT_ITEM_KEY)
10554 btrfs_release_path(&path);
10556 key.objectid = root_id;
10557 key.type = BTRFS_ROOT_ITEM_KEY;
10558 key.offset = (u64)-1;
10559 btrfs_init_path(&path);
10561 root = btrfs_read_fs_root(fs_info, &key);
10565 key.objectid = objectid;
10566 key.type = BTRFS_EXTENT_DATA_KEY;
10568 * It can be nasty as data backref offset is
10569 * file offset - file extent offset, which is smaller or
10570 * equal to original backref offset. The only special case is
10571 * overflow. So we need to special check and do further search.
10573 key.offset = offset & (1ULL << 63) ? 0 : offset;
10575 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
10580 * Search afterwards to get correct one
10581 * NOTE: As we must do a comprehensive check on the data backref to
10582 * make sure the dref count also matches, we must iterate all file
10583 * extents for that inode.
10586 leaf = path.nodes[0];
10587 slot = path.slots[0];
10589 btrfs_item_key_to_cpu(leaf, &key, slot);
10590 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
10592 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
10594 * Except normal disk bytenr and disk num bytes, we still
10595 * need to do extra check on dbackref offset as
10596 * dbackref offset = file_offset - file_extent_offset
10598 if (btrfs_file_extent_disk_bytenr(leaf, fi) == bytenr &&
10599 btrfs_file_extent_disk_num_bytes(leaf, fi) == len &&
10600 (u64)(key.offset - btrfs_file_extent_offset(leaf, fi)) ==
10604 ret = btrfs_next_item(root, &path);
10609 btrfs_release_path(&path);
10610 if (found_count != count) {
10612 "extent[%llu, %llu] referencer count mismatch (root: %llu, owner: %llu, offset: %llu) wanted: %u, have: %u",
10613 bytenr, len, root_id, objectid, offset, count, found_count);
10614 return REFERENCER_MISSING;
10620 * Check if the referencer of a shared data backref exists
10622 static int check_shared_data_backref(struct btrfs_fs_info *fs_info,
10623 u64 parent, u64 bytenr)
10625 struct extent_buffer *eb;
10626 struct btrfs_key key;
10627 struct btrfs_file_extent_item *fi;
10628 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10630 int found_parent = 0;
10633 eb = read_tree_block_fs_info(fs_info, parent, nodesize, 0);
10634 if (!extent_buffer_uptodate(eb))
10637 nr = btrfs_header_nritems(eb);
10638 for (i = 0; i < nr; i++) {
10639 btrfs_item_key_to_cpu(eb, &key, i);
10640 if (key.type != BTRFS_EXTENT_DATA_KEY)
10643 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
10644 if (btrfs_file_extent_type(eb, fi) == BTRFS_FILE_EXTENT_INLINE)
10647 if (btrfs_file_extent_disk_bytenr(eb, fi) == bytenr) {
10654 free_extent_buffer(eb);
10655 if (!found_parent) {
10656 error("shared extent %llu referencer lost (parent: %llu)",
10658 return REFERENCER_MISSING;
10664 * This function will check a given extent item, including its backref and
10665 * itself (like crossing stripe boundary and type)
10667 * Since we don't use extent_record anymore, introduce new error bit
10669 static int check_extent_item(struct btrfs_fs_info *fs_info,
10670 struct extent_buffer *eb, int slot)
10672 struct btrfs_extent_item *ei;
10673 struct btrfs_extent_inline_ref *iref;
10674 struct btrfs_extent_data_ref *dref;
10678 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10679 u32 item_size = btrfs_item_size_nr(eb, slot);
10684 struct btrfs_key key;
10688 btrfs_item_key_to_cpu(eb, &key, slot);
10689 if (key.type == BTRFS_EXTENT_ITEM_KEY)
10690 bytes_used += key.offset;
10692 bytes_used += nodesize;
10694 if (item_size < sizeof(*ei)) {
10696 * COMPAT_EXTENT_TREE_V0 case, but it's already a super
10697 * old thing when on disk format is still un-determined.
10698 * No need to care about it anymore
10700 error("unsupported COMPAT_EXTENT_TREE_V0 detected");
10704 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
10705 flags = btrfs_extent_flags(eb, ei);
10707 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)
10709 if (metadata && check_crossing_stripes(global_info, key.objectid,
10711 error("bad metadata [%llu, %llu) crossing stripe boundary",
10712 key.objectid, key.objectid + nodesize);
10713 err |= CROSSING_STRIPE_BOUNDARY;
10716 ptr = (unsigned long)(ei + 1);
10718 if (metadata && key.type == BTRFS_EXTENT_ITEM_KEY) {
10719 /* Old EXTENT_ITEM metadata */
10720 struct btrfs_tree_block_info *info;
10722 info = (struct btrfs_tree_block_info *)ptr;
10723 level = btrfs_tree_block_level(eb, info);
10724 ptr += sizeof(struct btrfs_tree_block_info);
10726 /* New METADATA_ITEM */
10727 level = key.offset;
10729 end = (unsigned long)ei + item_size;
10732 err |= ITEM_SIZE_MISMATCH;
10736 /* Now check every backref in this extent item */
10738 iref = (struct btrfs_extent_inline_ref *)ptr;
10739 type = btrfs_extent_inline_ref_type(eb, iref);
10740 offset = btrfs_extent_inline_ref_offset(eb, iref);
10742 case BTRFS_TREE_BLOCK_REF_KEY:
10743 ret = check_tree_block_backref(fs_info, offset, key.objectid,
10747 case BTRFS_SHARED_BLOCK_REF_KEY:
10748 ret = check_shared_block_backref(fs_info, offset, key.objectid,
10752 case BTRFS_EXTENT_DATA_REF_KEY:
10753 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
10754 ret = check_extent_data_backref(fs_info,
10755 btrfs_extent_data_ref_root(eb, dref),
10756 btrfs_extent_data_ref_objectid(eb, dref),
10757 btrfs_extent_data_ref_offset(eb, dref),
10758 key.objectid, key.offset,
10759 btrfs_extent_data_ref_count(eb, dref));
10762 case BTRFS_SHARED_DATA_REF_KEY:
10763 ret = check_shared_data_backref(fs_info, offset, key.objectid);
10767 error("extent[%llu %d %llu] has unknown ref type: %d",
10768 key.objectid, key.type, key.offset, type);
10769 err |= UNKNOWN_TYPE;
10773 ptr += btrfs_extent_inline_ref_size(type);
10782 * Check if a dev extent item is referred correctly by its chunk
10784 static int check_dev_extent_item(struct btrfs_fs_info *fs_info,
10785 struct extent_buffer *eb, int slot)
10787 struct btrfs_root *chunk_root = fs_info->chunk_root;
10788 struct btrfs_dev_extent *ptr;
10789 struct btrfs_path path;
10790 struct btrfs_key chunk_key;
10791 struct btrfs_key devext_key;
10792 struct btrfs_chunk *chunk;
10793 struct extent_buffer *l;
10797 int found_chunk = 0;
10800 btrfs_item_key_to_cpu(eb, &devext_key, slot);
10801 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_extent);
10802 length = btrfs_dev_extent_length(eb, ptr);
10804 chunk_key.objectid = btrfs_dev_extent_chunk_objectid(eb, ptr);
10805 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10806 chunk_key.offset = btrfs_dev_extent_chunk_offset(eb, ptr);
10808 btrfs_init_path(&path);
10809 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10814 chunk = btrfs_item_ptr(l, path.slots[0], struct btrfs_chunk);
10815 if (btrfs_chunk_length(l, chunk) != length)
10818 num_stripes = btrfs_chunk_num_stripes(l, chunk);
10819 for (i = 0; i < num_stripes; i++) {
10820 u64 devid = btrfs_stripe_devid_nr(l, chunk, i);
10821 u64 offset = btrfs_stripe_offset_nr(l, chunk, i);
10823 if (devid == devext_key.objectid &&
10824 offset == devext_key.offset) {
10830 btrfs_release_path(&path);
10831 if (!found_chunk) {
10833 "device extent[%llu, %llu, %llu] did not find the related chunk",
10834 devext_key.objectid, devext_key.offset, length);
10835 return REFERENCER_MISSING;
10841 * Check if the used space is correct with the dev item
10843 static int check_dev_item(struct btrfs_fs_info *fs_info,
10844 struct extent_buffer *eb, int slot)
10846 struct btrfs_root *dev_root = fs_info->dev_root;
10847 struct btrfs_dev_item *dev_item;
10848 struct btrfs_path path;
10849 struct btrfs_key key;
10850 struct btrfs_dev_extent *ptr;
10856 dev_item = btrfs_item_ptr(eb, slot, struct btrfs_dev_item);
10857 dev_id = btrfs_device_id(eb, dev_item);
10858 used = btrfs_device_bytes_used(eb, dev_item);
10860 key.objectid = dev_id;
10861 key.type = BTRFS_DEV_EXTENT_KEY;
10864 btrfs_init_path(&path);
10865 ret = btrfs_search_slot(NULL, dev_root, &key, &path, 0, 0);
10867 btrfs_item_key_to_cpu(eb, &key, slot);
10868 error("cannot find any related dev extent for dev[%llu, %u, %llu]",
10869 key.objectid, key.type, key.offset);
10870 btrfs_release_path(&path);
10871 return REFERENCER_MISSING;
10874 /* Iterate dev_extents to calculate the used space of a device */
10876 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
10878 if (key.objectid > dev_id)
10880 if (key.type != BTRFS_DEV_EXTENT_KEY || key.objectid != dev_id)
10883 ptr = btrfs_item_ptr(path.nodes[0], path.slots[0],
10884 struct btrfs_dev_extent);
10885 total += btrfs_dev_extent_length(path.nodes[0], ptr);
10887 ret = btrfs_next_item(dev_root, &path);
10891 btrfs_release_path(&path);
10893 if (used != total) {
10894 btrfs_item_key_to_cpu(eb, &key, slot);
10896 "Dev extent's total-byte %llu is not equal to bytes-used %llu in dev[%llu, %u, %llu]",
10897 total, used, BTRFS_ROOT_TREE_OBJECTID,
10898 BTRFS_DEV_EXTENT_KEY, dev_id);
10899 return ACCOUNTING_MISMATCH;
10905 * Check a block group item with its referener (chunk) and its used space
10906 * with extent/metadata item
10908 static int check_block_group_item(struct btrfs_fs_info *fs_info,
10909 struct extent_buffer *eb, int slot)
10911 struct btrfs_root *extent_root = fs_info->extent_root;
10912 struct btrfs_root *chunk_root = fs_info->chunk_root;
10913 struct btrfs_block_group_item *bi;
10914 struct btrfs_block_group_item bg_item;
10915 struct btrfs_path path;
10916 struct btrfs_key bg_key;
10917 struct btrfs_key chunk_key;
10918 struct btrfs_key extent_key;
10919 struct btrfs_chunk *chunk;
10920 struct extent_buffer *leaf;
10921 struct btrfs_extent_item *ei;
10922 u32 nodesize = btrfs_super_nodesize(fs_info->super_copy);
10930 btrfs_item_key_to_cpu(eb, &bg_key, slot);
10931 bi = btrfs_item_ptr(eb, slot, struct btrfs_block_group_item);
10932 read_extent_buffer(eb, &bg_item, (unsigned long)bi, sizeof(bg_item));
10933 used = btrfs_block_group_used(&bg_item);
10934 bg_flags = btrfs_block_group_flags(&bg_item);
10936 chunk_key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
10937 chunk_key.type = BTRFS_CHUNK_ITEM_KEY;
10938 chunk_key.offset = bg_key.objectid;
10940 btrfs_init_path(&path);
10941 /* Search for the referencer chunk */
10942 ret = btrfs_search_slot(NULL, chunk_root, &chunk_key, &path, 0, 0);
10945 "block group[%llu %llu] did not find the related chunk item",
10946 bg_key.objectid, bg_key.offset);
10947 err |= REFERENCER_MISSING;
10949 chunk = btrfs_item_ptr(path.nodes[0], path.slots[0],
10950 struct btrfs_chunk);
10951 if (btrfs_chunk_length(path.nodes[0], chunk) !=
10954 "block group[%llu %llu] related chunk item length does not match",
10955 bg_key.objectid, bg_key.offset);
10956 err |= REFERENCER_MISMATCH;
10959 btrfs_release_path(&path);
10961 /* Search from the block group bytenr */
10962 extent_key.objectid = bg_key.objectid;
10963 extent_key.type = 0;
10964 extent_key.offset = 0;
10966 btrfs_init_path(&path);
10967 ret = btrfs_search_slot(NULL, extent_root, &extent_key, &path, 0, 0);
10971 /* Iterate extent tree to account used space */
10973 leaf = path.nodes[0];
10974 btrfs_item_key_to_cpu(leaf, &extent_key, path.slots[0]);
10975 if (extent_key.objectid >= bg_key.objectid + bg_key.offset)
10978 if (extent_key.type != BTRFS_METADATA_ITEM_KEY &&
10979 extent_key.type != BTRFS_EXTENT_ITEM_KEY)
10981 if (extent_key.objectid < bg_key.objectid)
10984 if (extent_key.type == BTRFS_METADATA_ITEM_KEY)
10987 total += extent_key.offset;
10989 ei = btrfs_item_ptr(leaf, path.slots[0],
10990 struct btrfs_extent_item);
10991 flags = btrfs_extent_flags(leaf, ei);
10992 if (flags & BTRFS_EXTENT_FLAG_DATA) {
10993 if (!(bg_flags & BTRFS_BLOCK_GROUP_DATA)) {
10995 "bad extent[%llu, %llu) type mismatch with chunk",
10996 extent_key.objectid,
10997 extent_key.objectid + extent_key.offset);
10998 err |= CHUNK_TYPE_MISMATCH;
11000 } else if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
11001 if (!(bg_flags & (BTRFS_BLOCK_GROUP_SYSTEM |
11002 BTRFS_BLOCK_GROUP_METADATA))) {
11004 "bad extent[%llu, %llu) type mismatch with chunk",
11005 extent_key.objectid,
11006 extent_key.objectid + nodesize);
11007 err |= CHUNK_TYPE_MISMATCH;
11011 ret = btrfs_next_item(extent_root, &path);
11017 btrfs_release_path(&path);
11019 if (total != used) {
11021 "block group[%llu %llu] used %llu but extent items used %llu",
11022 bg_key.objectid, bg_key.offset, used, total);
11023 err |= ACCOUNTING_MISMATCH;
11029 * Check a chunk item.
11030 * Including checking all referred dev_extents and block group
11032 static int check_chunk_item(struct btrfs_fs_info *fs_info,
11033 struct extent_buffer *eb, int slot)
11035 struct btrfs_root *extent_root = fs_info->extent_root;
11036 struct btrfs_root *dev_root = fs_info->dev_root;
11037 struct btrfs_path path;
11038 struct btrfs_key chunk_key;
11039 struct btrfs_key bg_key;
11040 struct btrfs_key devext_key;
11041 struct btrfs_chunk *chunk;
11042 struct extent_buffer *leaf;
11043 struct btrfs_block_group_item *bi;
11044 struct btrfs_block_group_item bg_item;
11045 struct btrfs_dev_extent *ptr;
11046 u32 sectorsize = btrfs_super_sectorsize(fs_info->super_copy);
11058 btrfs_item_key_to_cpu(eb, &chunk_key, slot);
11059 chunk = btrfs_item_ptr(eb, slot, struct btrfs_chunk);
11060 length = btrfs_chunk_length(eb, chunk);
11061 chunk_end = chunk_key.offset + length;
11062 if (!IS_ALIGNED(length, sectorsize)) {
11063 error("chunk[%llu %llu) not aligned to %u",
11064 chunk_key.offset, chunk_end, sectorsize);
11065 err |= BYTES_UNALIGNED;
11069 type = btrfs_chunk_type(eb, chunk);
11070 profile = type & BTRFS_BLOCK_GROUP_PROFILE_MASK;
11071 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
11072 error("chunk[%llu %llu) has no chunk type",
11073 chunk_key.offset, chunk_end);
11074 err |= UNKNOWN_TYPE;
11076 if (profile && (profile & (profile - 1))) {
11077 error("chunk[%llu %llu) multiple profiles detected: %llx",
11078 chunk_key.offset, chunk_end, profile);
11079 err |= UNKNOWN_TYPE;
11082 bg_key.objectid = chunk_key.offset;
11083 bg_key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
11084 bg_key.offset = length;
11086 btrfs_init_path(&path);
11087 ret = btrfs_search_slot(NULL, extent_root, &bg_key, &path, 0, 0);
11090 "chunk[%llu %llu) did not find the related block group item",
11091 chunk_key.offset, chunk_end);
11092 err |= REFERENCER_MISSING;
11094 leaf = path.nodes[0];
11095 bi = btrfs_item_ptr(leaf, path.slots[0],
11096 struct btrfs_block_group_item);
11097 read_extent_buffer(leaf, &bg_item, (unsigned long)bi,
11099 if (btrfs_block_group_flags(&bg_item) != type) {
11101 "chunk[%llu %llu) related block group item flags mismatch, wanted: %llu, have: %llu",
11102 chunk_key.offset, chunk_end, type,
11103 btrfs_block_group_flags(&bg_item));
11104 err |= REFERENCER_MISSING;
11108 num_stripes = btrfs_chunk_num_stripes(eb, chunk);
11109 for (i = 0; i < num_stripes; i++) {
11110 btrfs_release_path(&path);
11111 btrfs_init_path(&path);
11112 devext_key.objectid = btrfs_stripe_devid_nr(eb, chunk, i);
11113 devext_key.type = BTRFS_DEV_EXTENT_KEY;
11114 devext_key.offset = btrfs_stripe_offset_nr(eb, chunk, i);
11116 ret = btrfs_search_slot(NULL, dev_root, &devext_key, &path,
11119 goto not_match_dev;
11121 leaf = path.nodes[0];
11122 ptr = btrfs_item_ptr(leaf, path.slots[0],
11123 struct btrfs_dev_extent);
11124 objectid = btrfs_dev_extent_chunk_objectid(leaf, ptr);
11125 offset = btrfs_dev_extent_chunk_offset(leaf, ptr);
11126 if (objectid != chunk_key.objectid ||
11127 offset != chunk_key.offset ||
11128 btrfs_dev_extent_length(leaf, ptr) != length)
11129 goto not_match_dev;
11132 err |= BACKREF_MISSING;
11134 "chunk[%llu %llu) stripe %d did not find the related dev extent",
11135 chunk_key.objectid, chunk_end, i);
11138 btrfs_release_path(&path);
11144 * Main entry function to check known items and update related accounting info
11146 static int check_leaf_items(struct btrfs_root *root, struct extent_buffer *eb)
11148 struct btrfs_fs_info *fs_info = root->fs_info;
11149 struct btrfs_key key;
11152 struct btrfs_extent_data_ref *dref;
11157 btrfs_item_key_to_cpu(eb, &key, slot);
11161 case BTRFS_EXTENT_DATA_KEY:
11162 ret = check_extent_data_item(root, eb, slot);
11165 case BTRFS_BLOCK_GROUP_ITEM_KEY:
11166 ret = check_block_group_item(fs_info, eb, slot);
11169 case BTRFS_DEV_ITEM_KEY:
11170 ret = check_dev_item(fs_info, eb, slot);
11173 case BTRFS_CHUNK_ITEM_KEY:
11174 ret = check_chunk_item(fs_info, eb, slot);
11177 case BTRFS_DEV_EXTENT_KEY:
11178 ret = check_dev_extent_item(fs_info, eb, slot);
11181 case BTRFS_EXTENT_ITEM_KEY:
11182 case BTRFS_METADATA_ITEM_KEY:
11183 ret = check_extent_item(fs_info, eb, slot);
11186 case BTRFS_EXTENT_CSUM_KEY:
11187 total_csum_bytes += btrfs_item_size_nr(eb, slot);
11189 case BTRFS_TREE_BLOCK_REF_KEY:
11190 ret = check_tree_block_backref(fs_info, key.offset,
11194 case BTRFS_EXTENT_DATA_REF_KEY:
11195 dref = btrfs_item_ptr(eb, slot, struct btrfs_extent_data_ref);
11196 ret = check_extent_data_backref(fs_info,
11197 btrfs_extent_data_ref_root(eb, dref),
11198 btrfs_extent_data_ref_objectid(eb, dref),
11199 btrfs_extent_data_ref_offset(eb, dref),
11201 btrfs_extent_data_ref_count(eb, dref));
11204 case BTRFS_SHARED_BLOCK_REF_KEY:
11205 ret = check_shared_block_backref(fs_info, key.offset,
11209 case BTRFS_SHARED_DATA_REF_KEY:
11210 ret = check_shared_data_backref(fs_info, key.offset,
11218 if (++slot < btrfs_header_nritems(eb))
11225 * Helper function for later fs/subvol tree check. To determine if a tree
11226 * block should be checked.
11227 * This function will ensure only the direct referencer with lowest rootid to
11228 * check a fs/subvolume tree block.
11230 * Backref check at extent tree would detect errors like missing subvolume
11231 * tree, so we can do aggressive check to reduce duplicated checks.
11233 static int should_check(struct btrfs_root *root, struct extent_buffer *eb)
11235 struct btrfs_root *extent_root = root->fs_info->extent_root;
11236 struct btrfs_key key;
11237 struct btrfs_path path;
11238 struct extent_buffer *leaf;
11240 struct btrfs_extent_item *ei;
11246 struct btrfs_extent_inline_ref *iref;
11249 btrfs_init_path(&path);
11250 key.objectid = btrfs_header_bytenr(eb);
11251 key.type = BTRFS_METADATA_ITEM_KEY;
11252 key.offset = (u64)-1;
11255 * Any failure in backref resolving means we can't determine
11256 * whom the tree block belongs to.
11257 * So in that case, we need to check that tree block
11259 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
11263 ret = btrfs_previous_extent_item(extent_root, &path,
11264 btrfs_header_bytenr(eb));
11268 leaf = path.nodes[0];
11269 slot = path.slots[0];
11270 btrfs_item_key_to_cpu(leaf, &key, slot);
11271 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
11273 if (key.type == BTRFS_METADATA_ITEM_KEY) {
11274 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
11276 struct btrfs_tree_block_info *info;
11278 info = (struct btrfs_tree_block_info *)(ei + 1);
11279 iref = (struct btrfs_extent_inline_ref *)(info + 1);
11282 item_size = btrfs_item_size_nr(leaf, slot);
11283 ptr = (unsigned long)iref;
11284 end = (unsigned long)ei + item_size;
11285 while (ptr < end) {
11286 iref = (struct btrfs_extent_inline_ref *)ptr;
11287 type = btrfs_extent_inline_ref_type(leaf, iref);
11288 offset = btrfs_extent_inline_ref_offset(leaf, iref);
11291 * We only check the tree block if current root is
11292 * the lowest referencer of it.
11294 if (type == BTRFS_TREE_BLOCK_REF_KEY &&
11295 offset < root->objectid) {
11296 btrfs_release_path(&path);
11300 ptr += btrfs_extent_inline_ref_size(type);
11303 * Normally we should also check keyed tree block ref, but that may be
11304 * very time consuming. Inlined ref should already make us skip a lot
11305 * of refs now. So skip search keyed tree block ref.
11309 btrfs_release_path(&path);
11314 * Traversal function for tree block. We will do:
11315 * 1) Skip shared fs/subvolume tree blocks
11316 * 2) Update related bytes accounting
11317 * 3) Pre-order traversal
11319 static int traverse_tree_block(struct btrfs_root *root,
11320 struct extent_buffer *node)
11322 struct extent_buffer *eb;
11323 struct btrfs_key key;
11324 struct btrfs_key drop_key;
11332 * Skip shared fs/subvolume tree block, in that case they will
11333 * be checked by referencer with lowest rootid
11335 if (is_fstree(root->objectid) && !should_check(root, node))
11338 /* Update bytes accounting */
11339 total_btree_bytes += node->len;
11340 if (fs_root_objectid(btrfs_header_owner(node)))
11341 total_fs_tree_bytes += node->len;
11342 if (btrfs_header_owner(node) == BTRFS_EXTENT_TREE_OBJECTID)
11343 total_extent_tree_bytes += node->len;
11344 if (!found_old_backref &&
11345 btrfs_header_owner(node) == BTRFS_TREE_RELOC_OBJECTID &&
11346 btrfs_header_backref_rev(node) == BTRFS_MIXED_BACKREF_REV &&
11347 !btrfs_header_flag(node, BTRFS_HEADER_FLAG_RELOC))
11348 found_old_backref = 1;
11350 /* pre-order tranversal, check itself first */
11351 level = btrfs_header_level(node);
11352 ret = check_tree_block_ref(root, node, btrfs_header_bytenr(node),
11353 btrfs_header_level(node),
11354 btrfs_header_owner(node));
11358 "check %s failed root %llu bytenr %llu level %d, force continue check",
11359 level ? "node":"leaf", root->objectid,
11360 btrfs_header_bytenr(node), btrfs_header_level(node));
11363 btree_space_waste += btrfs_leaf_free_space(root, node);
11364 ret = check_leaf_items(root, node);
11369 nr = btrfs_header_nritems(node);
11370 btrfs_disk_key_to_cpu(&drop_key, &root->root_item.drop_progress);
11371 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) - nr) *
11372 sizeof(struct btrfs_key_ptr);
11374 /* Then check all its children */
11375 for (i = 0; i < nr; i++) {
11376 u64 blocknr = btrfs_node_blockptr(node, i);
11378 btrfs_node_key_to_cpu(node, &key, i);
11379 if (level == root->root_item.drop_level &&
11380 is_dropped_key(&key, &drop_key))
11384 * As a btrfs tree has most 8 levels (0..7), so it's quite safe
11385 * to call the function itself.
11387 eb = read_tree_block(root, blocknr, root->nodesize, 0);
11388 if (extent_buffer_uptodate(eb)) {
11389 ret = traverse_tree_block(root, eb);
11392 free_extent_buffer(eb);
11399 * Low memory usage version check_chunks_and_extents.
11401 static int check_chunks_and_extents_v2(struct btrfs_root *root)
11403 struct btrfs_path path;
11404 struct btrfs_key key;
11405 struct btrfs_root *root1;
11406 struct btrfs_root *cur_root;
11410 root1 = root->fs_info->chunk_root;
11411 ret = traverse_tree_block(root1, root1->node);
11414 root1 = root->fs_info->tree_root;
11415 ret = traverse_tree_block(root1, root1->node);
11418 btrfs_init_path(&path);
11419 key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
11421 key.type = BTRFS_ROOT_ITEM_KEY;
11423 ret = btrfs_search_slot(NULL, root1, &key, &path, 0, 0);
11425 error("cannot find extent treet in tree_root");
11430 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
11431 if (key.type != BTRFS_ROOT_ITEM_KEY)
11433 key.offset = (u64)-1;
11435 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
11436 cur_root = btrfs_read_fs_root_no_cache(root->fs_info,
11439 cur_root = btrfs_read_fs_root(root->fs_info, &key);
11440 if (IS_ERR(cur_root) || !cur_root) {
11441 error("failed to read tree: %lld", key.objectid);
11445 ret = traverse_tree_block(cur_root, cur_root->node);
11448 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
11449 btrfs_free_fs_root(cur_root);
11451 ret = btrfs_next_item(root1, &path);
11457 btrfs_release_path(&path);
11461 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
11462 struct btrfs_root *root, int overwrite)
11464 struct extent_buffer *c;
11465 struct extent_buffer *old = root->node;
11468 struct btrfs_disk_key disk_key = {0,0,0};
11474 extent_buffer_get(c);
11477 c = btrfs_alloc_free_block(trans, root,
11479 root->root_key.objectid,
11480 &disk_key, level, 0, 0);
11483 extent_buffer_get(c);
11487 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
11488 btrfs_set_header_level(c, level);
11489 btrfs_set_header_bytenr(c, c->start);
11490 btrfs_set_header_generation(c, trans->transid);
11491 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
11492 btrfs_set_header_owner(c, root->root_key.objectid);
11494 write_extent_buffer(c, root->fs_info->fsid,
11495 btrfs_header_fsid(), BTRFS_FSID_SIZE);
11497 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
11498 btrfs_header_chunk_tree_uuid(c),
11501 btrfs_mark_buffer_dirty(c);
11503 * this case can happen in the following case:
11505 * 1.overwrite previous root.
11507 * 2.reinit reloc data root, this is because we skip pin
11508 * down reloc data tree before which means we can allocate
11509 * same block bytenr here.
11511 if (old->start == c->start) {
11512 btrfs_set_root_generation(&root->root_item,
11514 root->root_item.level = btrfs_header_level(root->node);
11515 ret = btrfs_update_root(trans, root->fs_info->tree_root,
11516 &root->root_key, &root->root_item);
11518 free_extent_buffer(c);
11522 free_extent_buffer(old);
11524 add_root_to_dirty_list(root);
11528 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
11529 struct extent_buffer *eb, int tree_root)
11531 struct extent_buffer *tmp;
11532 struct btrfs_root_item *ri;
11533 struct btrfs_key key;
11536 int level = btrfs_header_level(eb);
11542 * If we have pinned this block before, don't pin it again.
11543 * This can not only avoid forever loop with broken filesystem
11544 * but also give us some speedups.
11546 if (test_range_bit(&fs_info->pinned_extents, eb->start,
11547 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
11550 btrfs_pin_extent(fs_info, eb->start, eb->len);
11552 nodesize = btrfs_super_nodesize(fs_info->super_copy);
11553 nritems = btrfs_header_nritems(eb);
11554 for (i = 0; i < nritems; i++) {
11556 btrfs_item_key_to_cpu(eb, &key, i);
11557 if (key.type != BTRFS_ROOT_ITEM_KEY)
11559 /* Skip the extent root and reloc roots */
11560 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
11561 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
11562 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
11564 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
11565 bytenr = btrfs_disk_root_bytenr(eb, ri);
11568 * If at any point we start needing the real root we
11569 * will have to build a stump root for the root we are
11570 * in, but for now this doesn't actually use the root so
11571 * just pass in extent_root.
11573 tmp = read_tree_block(fs_info->extent_root, bytenr,
11575 if (!extent_buffer_uptodate(tmp)) {
11576 fprintf(stderr, "Error reading root block\n");
11579 ret = pin_down_tree_blocks(fs_info, tmp, 0);
11580 free_extent_buffer(tmp);
11584 bytenr = btrfs_node_blockptr(eb, i);
11586 /* If we aren't the tree root don't read the block */
11587 if (level == 1 && !tree_root) {
11588 btrfs_pin_extent(fs_info, bytenr, nodesize);
11592 tmp = read_tree_block(fs_info->extent_root, bytenr,
11594 if (!extent_buffer_uptodate(tmp)) {
11595 fprintf(stderr, "Error reading tree block\n");
11598 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
11599 free_extent_buffer(tmp);
11608 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
11612 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
11616 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
11619 static int reset_block_groups(struct btrfs_fs_info *fs_info)
11621 struct btrfs_block_group_cache *cache;
11622 struct btrfs_path path;
11623 struct extent_buffer *leaf;
11624 struct btrfs_chunk *chunk;
11625 struct btrfs_key key;
11629 btrfs_init_path(&path);
11631 key.type = BTRFS_CHUNK_ITEM_KEY;
11633 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, &path, 0, 0);
11635 btrfs_release_path(&path);
11640 * We do this in case the block groups were screwed up and had alloc
11641 * bits that aren't actually set on the chunks. This happens with
11642 * restored images every time and could happen in real life I guess.
11644 fs_info->avail_data_alloc_bits = 0;
11645 fs_info->avail_metadata_alloc_bits = 0;
11646 fs_info->avail_system_alloc_bits = 0;
11648 /* First we need to create the in-memory block groups */
11650 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11651 ret = btrfs_next_leaf(fs_info->chunk_root, &path);
11653 btrfs_release_path(&path);
11661 leaf = path.nodes[0];
11662 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11663 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
11668 chunk = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_chunk);
11669 btrfs_add_block_group(fs_info, 0,
11670 btrfs_chunk_type(leaf, chunk),
11671 key.objectid, key.offset,
11672 btrfs_chunk_length(leaf, chunk));
11673 set_extent_dirty(&fs_info->free_space_cache, key.offset,
11674 key.offset + btrfs_chunk_length(leaf, chunk));
11679 cache = btrfs_lookup_first_block_group(fs_info, start);
11683 start = cache->key.objectid + cache->key.offset;
11686 btrfs_release_path(&path);
11690 static int reset_balance(struct btrfs_trans_handle *trans,
11691 struct btrfs_fs_info *fs_info)
11693 struct btrfs_root *root = fs_info->tree_root;
11694 struct btrfs_path path;
11695 struct extent_buffer *leaf;
11696 struct btrfs_key key;
11697 int del_slot, del_nr = 0;
11701 btrfs_init_path(&path);
11702 key.objectid = BTRFS_BALANCE_OBJECTID;
11703 key.type = BTRFS_BALANCE_ITEM_KEY;
11705 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11710 goto reinit_data_reloc;
11715 ret = btrfs_del_item(trans, root, &path);
11718 btrfs_release_path(&path);
11720 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
11721 key.type = BTRFS_ROOT_ITEM_KEY;
11723 ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
11727 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
11732 ret = btrfs_del_items(trans, root, &path,
11739 btrfs_release_path(&path);
11742 ret = btrfs_search_slot(trans, root, &key, &path,
11749 leaf = path.nodes[0];
11750 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
11751 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
11753 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
11758 del_slot = path.slots[0];
11767 ret = btrfs_del_items(trans, root, &path, del_slot, del_nr);
11771 btrfs_release_path(&path);
11774 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
11775 key.type = BTRFS_ROOT_ITEM_KEY;
11776 key.offset = (u64)-1;
11777 root = btrfs_read_fs_root(fs_info, &key);
11778 if (IS_ERR(root)) {
11779 fprintf(stderr, "Error reading data reloc tree\n");
11780 ret = PTR_ERR(root);
11783 record_root_in_trans(trans, root);
11784 ret = btrfs_fsck_reinit_root(trans, root, 0);
11787 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
11789 btrfs_release_path(&path);
11793 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
11794 struct btrfs_fs_info *fs_info)
11800 * The only reason we don't do this is because right now we're just
11801 * walking the trees we find and pinning down their bytes, we don't look
11802 * at any of the leaves. In order to do mixed groups we'd have to check
11803 * the leaves of any fs roots and pin down the bytes for any file
11804 * extents we find. Not hard but why do it if we don't have to?
11806 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
11807 fprintf(stderr, "We don't support re-initing the extent tree "
11808 "for mixed block groups yet, please notify a btrfs "
11809 "developer you want to do this so they can add this "
11810 "functionality.\n");
11815 * first we need to walk all of the trees except the extent tree and pin
11816 * down the bytes that are in use so we don't overwrite any existing
11819 ret = pin_metadata_blocks(fs_info);
11821 fprintf(stderr, "error pinning down used bytes\n");
11826 * Need to drop all the block groups since we're going to recreate all
11829 btrfs_free_block_groups(fs_info);
11830 ret = reset_block_groups(fs_info);
11832 fprintf(stderr, "error resetting the block groups\n");
11836 /* Ok we can allocate now, reinit the extent root */
11837 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
11839 fprintf(stderr, "extent root initialization failed\n");
11841 * When the transaction code is updated we should end the
11842 * transaction, but for now progs only knows about commit so
11843 * just return an error.
11849 * Now we have all the in-memory block groups setup so we can make
11850 * allocations properly, and the metadata we care about is safe since we
11851 * pinned all of it above.
11854 struct btrfs_block_group_cache *cache;
11856 cache = btrfs_lookup_first_block_group(fs_info, start);
11859 start = cache->key.objectid + cache->key.offset;
11860 ret = btrfs_insert_item(trans, fs_info->extent_root,
11861 &cache->key, &cache->item,
11862 sizeof(cache->item));
11864 fprintf(stderr, "Error adding block group\n");
11867 btrfs_extent_post_op(trans, fs_info->extent_root);
11870 ret = reset_balance(trans, fs_info);
11872 fprintf(stderr, "error resetting the pending balance\n");
11877 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
11879 struct btrfs_path path;
11880 struct btrfs_trans_handle *trans;
11881 struct btrfs_key key;
11884 printf("Recowing metadata block %llu\n", eb->start);
11885 key.objectid = btrfs_header_owner(eb);
11886 key.type = BTRFS_ROOT_ITEM_KEY;
11887 key.offset = (u64)-1;
11889 root = btrfs_read_fs_root(root->fs_info, &key);
11890 if (IS_ERR(root)) {
11891 fprintf(stderr, "Couldn't find owner root %llu\n",
11893 return PTR_ERR(root);
11896 trans = btrfs_start_transaction(root, 1);
11898 return PTR_ERR(trans);
11900 btrfs_init_path(&path);
11901 path.lowest_level = btrfs_header_level(eb);
11902 if (path.lowest_level)
11903 btrfs_node_key_to_cpu(eb, &key, 0);
11905 btrfs_item_key_to_cpu(eb, &key, 0);
11907 ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
11908 btrfs_commit_transaction(trans, root);
11909 btrfs_release_path(&path);
11913 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
11915 struct btrfs_path path;
11916 struct btrfs_trans_handle *trans;
11917 struct btrfs_key key;
11920 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
11921 bad->key.type, bad->key.offset);
11922 key.objectid = bad->root_id;
11923 key.type = BTRFS_ROOT_ITEM_KEY;
11924 key.offset = (u64)-1;
11926 root = btrfs_read_fs_root(root->fs_info, &key);
11927 if (IS_ERR(root)) {
11928 fprintf(stderr, "Couldn't find owner root %llu\n",
11930 return PTR_ERR(root);
11933 trans = btrfs_start_transaction(root, 1);
11935 return PTR_ERR(trans);
11937 btrfs_init_path(&path);
11938 ret = btrfs_search_slot(trans, root, &bad->key, &path, -1, 1);
11944 ret = btrfs_del_item(trans, root, &path);
11946 btrfs_commit_transaction(trans, root);
11947 btrfs_release_path(&path);
11951 static int zero_log_tree(struct btrfs_root *root)
11953 struct btrfs_trans_handle *trans;
11956 trans = btrfs_start_transaction(root, 1);
11957 if (IS_ERR(trans)) {
11958 ret = PTR_ERR(trans);
11961 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
11962 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
11963 ret = btrfs_commit_transaction(trans, root);
11967 static int populate_csum(struct btrfs_trans_handle *trans,
11968 struct btrfs_root *csum_root, char *buf, u64 start,
11975 while (offset < len) {
11976 sectorsize = csum_root->sectorsize;
11977 ret = read_extent_data(csum_root, buf, start + offset,
11981 ret = btrfs_csum_file_block(trans, csum_root, start + len,
11982 start + offset, buf, sectorsize);
11985 offset += sectorsize;
11990 static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
11991 struct btrfs_root *csum_root,
11992 struct btrfs_root *cur_root)
11994 struct btrfs_path path;
11995 struct btrfs_key key;
11996 struct extent_buffer *node;
11997 struct btrfs_file_extent_item *fi;
12004 buf = malloc(cur_root->fs_info->csum_root->sectorsize);
12008 btrfs_init_path(&path);
12012 ret = btrfs_search_slot(NULL, cur_root, &key, &path, 0, 0);
12015 /* Iterate all regular file extents and fill its csum */
12017 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
12019 if (key.type != BTRFS_EXTENT_DATA_KEY)
12021 node = path.nodes[0];
12022 slot = path.slots[0];
12023 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
12024 if (btrfs_file_extent_type(node, fi) != BTRFS_FILE_EXTENT_REG)
12026 start = btrfs_file_extent_disk_bytenr(node, fi);
12027 len = btrfs_file_extent_disk_num_bytes(node, fi);
12029 ret = populate_csum(trans, csum_root, buf, start, len);
12030 if (ret == -EEXIST)
12036 * TODO: if next leaf is corrupted, jump to nearest next valid
12039 ret = btrfs_next_item(cur_root, &path);
12049 btrfs_release_path(&path);
12054 static int fill_csum_tree_from_fs(struct btrfs_trans_handle *trans,
12055 struct btrfs_root *csum_root)
12057 struct btrfs_fs_info *fs_info = csum_root->fs_info;
12058 struct btrfs_path path;
12059 struct btrfs_root *tree_root = fs_info->tree_root;
12060 struct btrfs_root *cur_root;
12061 struct extent_buffer *node;
12062 struct btrfs_key key;
12066 btrfs_init_path(&path);
12067 key.objectid = BTRFS_FS_TREE_OBJECTID;
12069 key.type = BTRFS_ROOT_ITEM_KEY;
12070 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
12079 node = path.nodes[0];
12080 slot = path.slots[0];
12081 btrfs_item_key_to_cpu(node, &key, slot);
12082 if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
12084 if (key.type != BTRFS_ROOT_ITEM_KEY)
12086 if (!is_fstree(key.objectid))
12088 key.offset = (u64)-1;
12090 cur_root = btrfs_read_fs_root(fs_info, &key);
12091 if (IS_ERR(cur_root) || !cur_root) {
12092 fprintf(stderr, "Fail to read fs/subvol tree: %lld\n",
12096 ret = fill_csum_tree_from_one_fs_root(trans, csum_root,
12101 ret = btrfs_next_item(tree_root, &path);
12111 btrfs_release_path(&path);
12115 static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans,
12116 struct btrfs_root *csum_root)
12118 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
12119 struct btrfs_path path;
12120 struct btrfs_extent_item *ei;
12121 struct extent_buffer *leaf;
12123 struct btrfs_key key;
12126 btrfs_init_path(&path);
12128 key.type = BTRFS_EXTENT_ITEM_KEY;
12130 ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
12132 btrfs_release_path(&path);
12136 buf = malloc(csum_root->sectorsize);
12138 btrfs_release_path(&path);
12143 if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
12144 ret = btrfs_next_leaf(extent_root, &path);
12152 leaf = path.nodes[0];
12154 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
12155 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
12160 ei = btrfs_item_ptr(leaf, path.slots[0],
12161 struct btrfs_extent_item);
12162 if (!(btrfs_extent_flags(leaf, ei) &
12163 BTRFS_EXTENT_FLAG_DATA)) {
12168 ret = populate_csum(trans, csum_root, buf, key.objectid,
12175 btrfs_release_path(&path);
12181 * Recalculate the csum and put it into the csum tree.
12183 * Extent tree init will wipe out all the extent info, so in that case, we
12184 * can't depend on extent tree, but use fs tree. If search_fs_tree is set, we
12185 * will use fs/subvol trees to init the csum tree.
12187 static int fill_csum_tree(struct btrfs_trans_handle *trans,
12188 struct btrfs_root *csum_root,
12189 int search_fs_tree)
12191 if (search_fs_tree)
12192 return fill_csum_tree_from_fs(trans, csum_root);
12194 return fill_csum_tree_from_extent(trans, csum_root);
12197 static void free_roots_info_cache(void)
12199 if (!roots_info_cache)
12202 while (!cache_tree_empty(roots_info_cache)) {
12203 struct cache_extent *entry;
12204 struct root_item_info *rii;
12206 entry = first_cache_extent(roots_info_cache);
12209 remove_cache_extent(roots_info_cache, entry);
12210 rii = container_of(entry, struct root_item_info, cache_extent);
12214 free(roots_info_cache);
12215 roots_info_cache = NULL;
12218 static int build_roots_info_cache(struct btrfs_fs_info *info)
12221 struct btrfs_key key;
12222 struct extent_buffer *leaf;
12223 struct btrfs_path path;
12225 if (!roots_info_cache) {
12226 roots_info_cache = malloc(sizeof(*roots_info_cache));
12227 if (!roots_info_cache)
12229 cache_tree_init(roots_info_cache);
12232 btrfs_init_path(&path);
12234 key.type = BTRFS_EXTENT_ITEM_KEY;
12236 ret = btrfs_search_slot(NULL, info->extent_root, &key, &path, 0, 0);
12239 leaf = path.nodes[0];
12242 struct btrfs_key found_key;
12243 struct btrfs_extent_item *ei;
12244 struct btrfs_extent_inline_ref *iref;
12245 int slot = path.slots[0];
12250 struct cache_extent *entry;
12251 struct root_item_info *rii;
12253 if (slot >= btrfs_header_nritems(leaf)) {
12254 ret = btrfs_next_leaf(info->extent_root, &path);
12261 leaf = path.nodes[0];
12262 slot = path.slots[0];
12265 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12267 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
12268 found_key.type != BTRFS_METADATA_ITEM_KEY)
12271 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
12272 flags = btrfs_extent_flags(leaf, ei);
12274 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
12275 !(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK))
12278 if (found_key.type == BTRFS_METADATA_ITEM_KEY) {
12279 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
12280 level = found_key.offset;
12282 struct btrfs_tree_block_info *binfo;
12284 binfo = (struct btrfs_tree_block_info *)(ei + 1);
12285 iref = (struct btrfs_extent_inline_ref *)(binfo + 1);
12286 level = btrfs_tree_block_level(leaf, binfo);
12290 * For a root extent, it must be of the following type and the
12291 * first (and only one) iref in the item.
12293 type = btrfs_extent_inline_ref_type(leaf, iref);
12294 if (type != BTRFS_TREE_BLOCK_REF_KEY)
12297 root_id = btrfs_extent_inline_ref_offset(leaf, iref);
12298 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12300 rii = malloc(sizeof(struct root_item_info));
12305 rii->cache_extent.start = root_id;
12306 rii->cache_extent.size = 1;
12307 rii->level = (u8)-1;
12308 entry = &rii->cache_extent;
12309 ret = insert_cache_extent(roots_info_cache, entry);
12312 rii = container_of(entry, struct root_item_info,
12316 ASSERT(rii->cache_extent.start == root_id);
12317 ASSERT(rii->cache_extent.size == 1);
12319 if (level > rii->level || rii->level == (u8)-1) {
12320 rii->level = level;
12321 rii->bytenr = found_key.objectid;
12322 rii->gen = btrfs_extent_generation(leaf, ei);
12323 rii->node_count = 1;
12324 } else if (level == rii->level) {
12332 btrfs_release_path(&path);
12337 static int maybe_repair_root_item(struct btrfs_fs_info *info,
12338 struct btrfs_path *path,
12339 const struct btrfs_key *root_key,
12340 const int read_only_mode)
12342 const u64 root_id = root_key->objectid;
12343 struct cache_extent *entry;
12344 struct root_item_info *rii;
12345 struct btrfs_root_item ri;
12346 unsigned long offset;
12348 entry = lookup_cache_extent(roots_info_cache, root_id, 1);
12351 "Error: could not find extent items for root %llu\n",
12352 root_key->objectid);
12356 rii = container_of(entry, struct root_item_info, cache_extent);
12357 ASSERT(rii->cache_extent.start == root_id);
12358 ASSERT(rii->cache_extent.size == 1);
12360 if (rii->node_count != 1) {
12362 "Error: could not find btree root extent for root %llu\n",
12367 offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
12368 read_extent_buffer(path->nodes[0], &ri, offset, sizeof(ri));
12370 if (btrfs_root_bytenr(&ri) != rii->bytenr ||
12371 btrfs_root_level(&ri) != rii->level ||
12372 btrfs_root_generation(&ri) != rii->gen) {
12375 * If we're in repair mode but our caller told us to not update
12376 * the root item, i.e. just check if it needs to be updated, don't
12377 * print this message, since the caller will call us again shortly
12378 * for the same root item without read only mode (the caller will
12379 * open a transaction first).
12381 if (!(read_only_mode && repair))
12383 "%sroot item for root %llu,"
12384 " current bytenr %llu, current gen %llu, current level %u,"
12385 " new bytenr %llu, new gen %llu, new level %u\n",
12386 (read_only_mode ? "" : "fixing "),
12388 btrfs_root_bytenr(&ri), btrfs_root_generation(&ri),
12389 btrfs_root_level(&ri),
12390 rii->bytenr, rii->gen, rii->level);
12392 if (btrfs_root_generation(&ri) > rii->gen) {
12394 "root %llu has a root item with a more recent gen (%llu) compared to the found root node (%llu)\n",
12395 root_id, btrfs_root_generation(&ri), rii->gen);
12399 if (!read_only_mode) {
12400 btrfs_set_root_bytenr(&ri, rii->bytenr);
12401 btrfs_set_root_level(&ri, rii->level);
12402 btrfs_set_root_generation(&ri, rii->gen);
12403 write_extent_buffer(path->nodes[0], &ri,
12404 offset, sizeof(ri));
12414 * A regression introduced in the 3.17 kernel (more specifically in 3.17-rc2),
12415 * caused read-only snapshots to be corrupted if they were created at a moment
12416 * when the source subvolume/snapshot had orphan items. The issue was that the
12417 * on-disk root items became incorrect, referring to the pre orphan cleanup root
12418 * node instead of the post orphan cleanup root node.
12419 * So this function, and its callees, just detects and fixes those cases. Even
12420 * though the regression was for read-only snapshots, this function applies to
12421 * any snapshot/subvolume root.
12422 * This must be run before any other repair code - not doing it so, makes other
12423 * repair code delete or modify backrefs in the extent tree for example, which
12424 * will result in an inconsistent fs after repairing the root items.
12426 static int repair_root_items(struct btrfs_fs_info *info)
12428 struct btrfs_path path;
12429 struct btrfs_key key;
12430 struct extent_buffer *leaf;
12431 struct btrfs_trans_handle *trans = NULL;
12434 int need_trans = 0;
12436 btrfs_init_path(&path);
12438 ret = build_roots_info_cache(info);
12442 key.objectid = BTRFS_FIRST_FREE_OBJECTID;
12443 key.type = BTRFS_ROOT_ITEM_KEY;
12448 * Avoid opening and committing transactions if a leaf doesn't have
12449 * any root items that need to be fixed, so that we avoid rotating
12450 * backup roots unnecessarily.
12453 trans = btrfs_start_transaction(info->tree_root, 1);
12454 if (IS_ERR(trans)) {
12455 ret = PTR_ERR(trans);
12460 ret = btrfs_search_slot(trans, info->tree_root, &key, &path,
12464 leaf = path.nodes[0];
12467 struct btrfs_key found_key;
12469 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
12470 int no_more_keys = find_next_key(&path, &key);
12472 btrfs_release_path(&path);
12474 ret = btrfs_commit_transaction(trans,
12486 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
12488 if (found_key.type != BTRFS_ROOT_ITEM_KEY)
12490 if (found_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
12493 ret = maybe_repair_root_item(info, &path, &found_key,
12498 if (!trans && repair) {
12501 btrfs_release_path(&path);
12511 free_roots_info_cache();
12512 btrfs_release_path(&path);
12514 btrfs_commit_transaction(trans, info->tree_root);
12521 static int clear_free_space_cache(struct btrfs_fs_info *fs_info)
12523 struct btrfs_trans_handle *trans;
12524 struct btrfs_block_group_cache *bg_cache;
12528 /* Clear all free space cache inodes and its extent data */
12530 bg_cache = btrfs_lookup_first_block_group(fs_info, current);
12533 ret = btrfs_clear_free_space_cache(fs_info, bg_cache);
12536 current = bg_cache->key.objectid + bg_cache->key.offset;
12539 /* Don't forget to set cache_generation to -1 */
12540 trans = btrfs_start_transaction(fs_info->tree_root, 0);
12541 if (IS_ERR(trans)) {
12542 error("failed to update super block cache generation");
12543 return PTR_ERR(trans);
12545 btrfs_set_super_cache_generation(fs_info->super_copy, (u64)-1);
12546 btrfs_commit_transaction(trans, fs_info->tree_root);
12551 const char * const cmd_check_usage[] = {
12552 "btrfs check [options] <device>",
12553 "Check structural integrity of a filesystem (unmounted).",
12554 "Check structural integrity of an unmounted filesystem. Verify internal",
12555 "trees' consistency and item connectivity. In the repair mode try to",
12556 "fix the problems found. ",
12557 "WARNING: the repair mode is considered dangerous",
12559 "-s|--super <superblock> use this superblock copy",
12560 "-b|--backup use the first valid backup root copy",
12561 "--repair try to repair the filesystem",
12562 "--readonly run in read-only mode (default)",
12563 "--init-csum-tree create a new CRC tree",
12564 "--init-extent-tree create a new extent tree",
12565 "--mode <MODE> allows choice of memory/IO trade-offs",
12566 " where MODE is one of:",
12567 " original - read inodes and extents to memory (requires",
12568 " more memory, does less IO)",
12569 " lowmem - try to use less memory but read blocks again",
12571 "--check-data-csum verify checksums of data blocks",
12572 "-Q|--qgroup-report print a report on qgroup consistency",
12573 "-E|--subvol-extents <subvolid>",
12574 " print subvolume extents and sharing state",
12575 "-r|--tree-root <bytenr> use the given bytenr for the tree root",
12576 "--chunk-root <bytenr> use the given bytenr for the chunk tree root",
12577 "-p|--progress indicate progress",
12578 "--clear-space-cache v1|v2 clear space cache for v1 or v2",
12582 int cmd_check(int argc, char **argv)
12584 struct cache_tree root_cache;
12585 struct btrfs_root *root;
12586 struct btrfs_fs_info *info;
12589 u64 tree_root_bytenr = 0;
12590 u64 chunk_root_bytenr = 0;
12591 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
12595 int init_csum_tree = 0;
12597 int clear_space_cache = 0;
12598 int qgroup_report = 0;
12599 int qgroups_repaired = 0;
12600 unsigned ctree_flags = OPEN_CTREE_EXCLUSIVE;
12604 enum { GETOPT_VAL_REPAIR = 257, GETOPT_VAL_INIT_CSUM,
12605 GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
12606 GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE,
12607 GETOPT_VAL_MODE, GETOPT_VAL_CLEAR_SPACE_CACHE };
12608 static const struct option long_options[] = {
12609 { "super", required_argument, NULL, 's' },
12610 { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
12611 { "readonly", no_argument, NULL, GETOPT_VAL_READONLY },
12612 { "init-csum-tree", no_argument, NULL,
12613 GETOPT_VAL_INIT_CSUM },
12614 { "init-extent-tree", no_argument, NULL,
12615 GETOPT_VAL_INIT_EXTENT },
12616 { "check-data-csum", no_argument, NULL,
12617 GETOPT_VAL_CHECK_CSUM },
12618 { "backup", no_argument, NULL, 'b' },
12619 { "subvol-extents", required_argument, NULL, 'E' },
12620 { "qgroup-report", no_argument, NULL, 'Q' },
12621 { "tree-root", required_argument, NULL, 'r' },
12622 { "chunk-root", required_argument, NULL,
12623 GETOPT_VAL_CHUNK_TREE },
12624 { "progress", no_argument, NULL, 'p' },
12625 { "mode", required_argument, NULL,
12627 { "clear-space-cache", required_argument, NULL,
12628 GETOPT_VAL_CLEAR_SPACE_CACHE},
12629 { NULL, 0, NULL, 0}
12632 c = getopt_long(argc, argv, "as:br:p", long_options, NULL);
12636 case 'a': /* ignored */ break;
12638 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
12641 num = arg_strtou64(optarg);
12642 if (num >= BTRFS_SUPER_MIRROR_MAX) {
12644 "super mirror should be less than %d",
12645 BTRFS_SUPER_MIRROR_MAX);
12648 bytenr = btrfs_sb_offset(((int)num));
12649 printf("using SB copy %llu, bytenr %llu\n", num,
12650 (unsigned long long)bytenr);
12656 subvolid = arg_strtou64(optarg);
12659 tree_root_bytenr = arg_strtou64(optarg);
12661 case GETOPT_VAL_CHUNK_TREE:
12662 chunk_root_bytenr = arg_strtou64(optarg);
12665 ctx.progress_enabled = true;
12669 usage(cmd_check_usage);
12670 case GETOPT_VAL_REPAIR:
12671 printf("enabling repair mode\n");
12673 ctree_flags |= OPEN_CTREE_WRITES;
12675 case GETOPT_VAL_READONLY:
12678 case GETOPT_VAL_INIT_CSUM:
12679 printf("Creating a new CRC tree\n");
12680 init_csum_tree = 1;
12682 ctree_flags |= OPEN_CTREE_WRITES;
12684 case GETOPT_VAL_INIT_EXTENT:
12685 init_extent_tree = 1;
12686 ctree_flags |= (OPEN_CTREE_WRITES |
12687 OPEN_CTREE_NO_BLOCK_GROUPS);
12690 case GETOPT_VAL_CHECK_CSUM:
12691 check_data_csum = 1;
12693 case GETOPT_VAL_MODE:
12694 check_mode = parse_check_mode(optarg);
12695 if (check_mode == CHECK_MODE_UNKNOWN) {
12696 error("unknown mode: %s", optarg);
12700 case GETOPT_VAL_CLEAR_SPACE_CACHE:
12701 if (strcmp(optarg, "v1") == 0) {
12702 clear_space_cache = 1;
12703 } else if (strcmp(optarg, "v2") == 0) {
12704 clear_space_cache = 2;
12705 ctree_flags |= OPEN_CTREE_INVALIDATE_FST;
12708 "invalid argument to --clear-space-cache, must be v1 or v2");
12711 ctree_flags |= OPEN_CTREE_WRITES;
12716 if (check_argc_exact(argc - optind, 1))
12717 usage(cmd_check_usage);
12719 if (ctx.progress_enabled) {
12720 ctx.tp = TASK_NOTHING;
12721 ctx.info = task_init(print_status_check, print_status_return, &ctx);
12724 /* This check is the only reason for --readonly to exist */
12725 if (readonly && repair) {
12726 error("repair options are not compatible with --readonly");
12731 * Not supported yet
12733 if (repair && check_mode == CHECK_MODE_LOWMEM) {
12734 error("low memory mode doesn't support repair yet");
12739 cache_tree_init(&root_cache);
12741 if((ret = check_mounted(argv[optind])) < 0) {
12742 error("could not check mount status: %s", strerror(-ret));
12746 error("%s is currently mounted, aborting", argv[optind]);
12752 /* only allow partial opening under repair mode */
12754 ctree_flags |= OPEN_CTREE_PARTIAL;
12756 info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
12757 chunk_root_bytenr, ctree_flags);
12759 error("cannot open file system");
12765 global_info = info;
12766 root = info->fs_root;
12767 if (clear_space_cache == 1) {
12768 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) {
12770 "free space cache v2 detected, use --clear-space-cache v2");
12774 printf("Clearing free space cache\n");
12775 ret = clear_free_space_cache(info);
12777 error("failed to clear free space cache");
12780 printf("Free space cache cleared\n");
12783 } else if (clear_space_cache == 2) {
12784 if (!btrfs_fs_compat_ro(info, FREE_SPACE_TREE)) {
12785 printf("no free space cache v2 to clear\n");
12789 printf("Clear free space cache v2\n");
12790 ret = btrfs_clear_free_space_tree(info);
12792 error("failed to clear free space cache v2: %d", ret);
12795 printf("free space cache v2 cleared\n");
12801 * repair mode will force us to commit transaction which
12802 * will make us fail to load log tree when mounting.
12804 if (repair && btrfs_super_log_root(info->super_copy)) {
12805 ret = ask_user("repair mode will force to clear out log tree, are you sure?");
12811 ret = zero_log_tree(root);
12814 error("failed to zero log tree: %d", ret);
12819 uuid_unparse(info->super_copy->fsid, uuidbuf);
12820 if (qgroup_report) {
12821 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
12823 ret = qgroup_verify_all(info);
12830 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
12831 subvolid, argv[optind], uuidbuf);
12832 ret = print_extent_state(info, subvolid);
12836 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
12838 if (!extent_buffer_uptodate(info->tree_root->node) ||
12839 !extent_buffer_uptodate(info->dev_root->node) ||
12840 !extent_buffer_uptodate(info->chunk_root->node)) {
12841 error("critical roots corrupted, unable to check the filesystem");
12847 if (init_extent_tree || init_csum_tree) {
12848 struct btrfs_trans_handle *trans;
12850 trans = btrfs_start_transaction(info->extent_root, 0);
12851 if (IS_ERR(trans)) {
12852 error("error starting transaction");
12853 ret = PTR_ERR(trans);
12858 if (init_extent_tree) {
12859 printf("Creating a new extent tree\n");
12860 ret = reinit_extent_tree(trans, info);
12866 if (init_csum_tree) {
12867 printf("Reinitialize checksum tree\n");
12868 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
12870 error("checksum tree initialization failed: %d",
12877 ret = fill_csum_tree(trans, info->csum_root,
12881 error("checksum tree refilling failed: %d", ret);
12886 * Ok now we commit and run the normal fsck, which will add
12887 * extent entries for all of the items it finds.
12889 ret = btrfs_commit_transaction(trans, info->extent_root);
12894 if (!extent_buffer_uptodate(info->extent_root->node)) {
12895 error("critical: extent_root, unable to check the filesystem");
12900 if (!extent_buffer_uptodate(info->csum_root->node)) {
12901 error("critical: csum_root, unable to check the filesystem");
12907 if (!ctx.progress_enabled)
12908 fprintf(stderr, "checking extents\n");
12909 if (check_mode == CHECK_MODE_LOWMEM)
12910 ret = check_chunks_and_extents_v2(root);
12912 ret = check_chunks_and_extents(root);
12916 "errors found in extent allocation tree or chunk allocation");
12918 ret = repair_root_items(info);
12923 fprintf(stderr, "Fixed %d roots.\n", ret);
12925 } else if (ret > 0) {
12927 "Found %d roots with an outdated root item.\n",
12930 "Please run a filesystem check with the option --repair to fix them.\n");
12936 if (!ctx.progress_enabled) {
12937 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
12938 fprintf(stderr, "checking free space tree\n");
12940 fprintf(stderr, "checking free space cache\n");
12942 ret = check_space_cache(root);
12948 * We used to have to have these hole extents in between our real
12949 * extents so if we don't have this flag set we need to make sure there
12950 * are no gaps in the file extents for inodes, otherwise we can just
12951 * ignore it when this happens.
12953 no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES);
12954 if (!ctx.progress_enabled)
12955 fprintf(stderr, "checking fs roots\n");
12956 if (check_mode == CHECK_MODE_LOWMEM)
12957 ret = check_fs_roots_v2(root->fs_info);
12959 ret = check_fs_roots(root, &root_cache);
12964 fprintf(stderr, "checking csums\n");
12965 ret = check_csums(root);
12970 fprintf(stderr, "checking root refs\n");
12971 /* For low memory mode, check_fs_roots_v2 handles root refs */
12972 if (check_mode != CHECK_MODE_LOWMEM) {
12973 ret = check_root_refs(root, &root_cache);
12979 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
12980 struct extent_buffer *eb;
12982 eb = list_first_entry(&root->fs_info->recow_ebs,
12983 struct extent_buffer, recow);
12984 list_del_init(&eb->recow);
12985 ret = recow_extent_buffer(root, eb);
12991 while (!list_empty(&delete_items)) {
12992 struct bad_item *bad;
12994 bad = list_first_entry(&delete_items, struct bad_item, list);
12995 list_del_init(&bad->list);
12997 ret = delete_bad_item(root, bad);
13003 if (info->quota_enabled) {
13004 fprintf(stderr, "checking quota groups\n");
13005 ret = qgroup_verify_all(info);
13010 ret = repair_qgroups(info, &qgroups_repaired);
13017 if (!list_empty(&root->fs_info->recow_ebs)) {
13018 error("transid errors in file system");
13023 if (found_old_backref) { /*
13024 * there was a disk format change when mixed
13025 * backref was in testing tree. The old format
13026 * existed about one week.
13028 printf("\n * Found old mixed backref format. "
13029 "The old format is not supported! *"
13030 "\n * Please mount the FS in readonly mode, "
13031 "backup data and re-format the FS. *\n\n");
13034 printf("found %llu bytes used err is %d\n",
13035 (unsigned long long)bytes_used, ret);
13036 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
13037 printf("total tree bytes: %llu\n",
13038 (unsigned long long)total_btree_bytes);
13039 printf("total fs tree bytes: %llu\n",
13040 (unsigned long long)total_fs_tree_bytes);
13041 printf("total extent tree bytes: %llu\n",
13042 (unsigned long long)total_extent_tree_bytes);
13043 printf("btree space waste bytes: %llu\n",
13044 (unsigned long long)btree_space_waste);
13045 printf("file data blocks allocated: %llu\n referenced %llu\n",
13046 (unsigned long long)data_bytes_allocated,
13047 (unsigned long long)data_bytes_referenced);
13049 free_qgroup_counts();
13050 free_root_recs_tree(&root_cache);
13054 if (ctx.progress_enabled)
13055 task_deinit(ctx.info);