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.
19 #define _XOPEN_SOURCE 500
25 #include <sys/types.h>
29 #include <uuid/uuid.h>
34 #include "print-tree.h"
35 #include "transaction.h"
39 #include "free-space-cache.h"
41 #include "qgroup-verify.h"
42 #include "rbtree-utils.h"
46 static u64 bytes_used = 0;
47 static u64 total_csum_bytes = 0;
48 static u64 total_btree_bytes = 0;
49 static u64 total_fs_tree_bytes = 0;
50 static u64 total_extent_tree_bytes = 0;
51 static u64 btree_space_waste = 0;
52 static u64 data_bytes_allocated = 0;
53 static u64 data_bytes_referenced = 0;
54 static int found_old_backref = 0;
55 static LIST_HEAD(duplicate_extents);
56 static LIST_HEAD(delete_items);
57 static int repair = 0;
58 static int no_holes = 0;
59 static int init_extent_tree = 0;
60 static int check_data_csum = 0;
62 struct extent_backref {
63 struct list_head list;
64 unsigned int is_data:1;
65 unsigned int found_extent_tree:1;
66 unsigned int full_backref:1;
67 unsigned int found_ref:1;
68 unsigned int broken:1;
72 struct extent_backref node;
87 struct extent_backref node;
94 struct extent_record {
95 struct list_head backrefs;
96 struct list_head dups;
97 struct list_head list;
98 struct cache_extent cache;
99 struct btrfs_disk_key parent_key;
104 u64 extent_item_refs;
106 u64 parent_generation;
110 unsigned int found_rec:1;
111 unsigned int content_checked:1;
112 unsigned int owner_ref_checked:1;
113 unsigned int is_root:1;
114 unsigned int metadata:1;
117 struct inode_backref {
118 struct list_head list;
119 unsigned int found_dir_item:1;
120 unsigned int found_dir_index:1;
121 unsigned int found_inode_ref:1;
122 unsigned int filetype:8;
124 unsigned int ref_type;
131 struct dropping_root_item_record {
132 struct list_head list;
133 struct btrfs_root_item ri;
134 struct btrfs_key found_key;
137 #define REF_ERR_NO_DIR_ITEM (1 << 0)
138 #define REF_ERR_NO_DIR_INDEX (1 << 1)
139 #define REF_ERR_NO_INODE_REF (1 << 2)
140 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
141 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
142 #define REF_ERR_DUP_INODE_REF (1 << 5)
143 #define REF_ERR_INDEX_UNMATCH (1 << 6)
144 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
145 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
146 #define REF_ERR_NO_ROOT_REF (1 << 9)
147 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
148 #define REF_ERR_DUP_ROOT_REF (1 << 11)
149 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
151 struct inode_record {
152 struct list_head backrefs;
153 unsigned int checked:1;
154 unsigned int merging:1;
155 unsigned int found_inode_item:1;
156 unsigned int found_dir_item:1;
157 unsigned int found_file_extent:1;
158 unsigned int found_csum_item:1;
159 unsigned int some_csum_missing:1;
160 unsigned int nodatasum:1;
173 u64 first_extent_gap;
178 #define I_ERR_NO_INODE_ITEM (1 << 0)
179 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
180 #define I_ERR_DUP_INODE_ITEM (1 << 2)
181 #define I_ERR_DUP_DIR_INDEX (1 << 3)
182 #define I_ERR_ODD_DIR_ITEM (1 << 4)
183 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
184 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
185 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
186 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
187 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
188 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
189 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
190 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
191 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
193 struct root_backref {
194 struct list_head list;
195 unsigned int found_dir_item:1;
196 unsigned int found_dir_index:1;
197 unsigned int found_back_ref:1;
198 unsigned int found_forward_ref:1;
199 unsigned int reachable:1;
209 struct list_head backrefs;
210 struct cache_extent cache;
211 unsigned int found_root_item:1;
217 struct cache_extent cache;
222 struct cache_extent cache;
223 struct cache_tree root_cache;
224 struct cache_tree inode_cache;
225 struct inode_record *current;
234 struct walk_control {
235 struct cache_tree shared;
236 struct shared_node *nodes[BTRFS_MAX_LEVEL];
242 struct btrfs_key key;
244 struct list_head list;
247 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
249 static void record_root_in_trans(struct btrfs_trans_handle *trans,
250 struct btrfs_root *root)
252 if (root->last_trans != trans->transid) {
253 root->track_dirty = 1;
254 root->last_trans = trans->transid;
255 root->commit_root = root->node;
256 extent_buffer_get(root->node);
260 static u8 imode_to_type(u32 imode)
263 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
264 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
265 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
266 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
267 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
268 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
269 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
270 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
273 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
277 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
279 struct device_record *rec1;
280 struct device_record *rec2;
282 rec1 = rb_entry(node1, struct device_record, node);
283 rec2 = rb_entry(node2, struct device_record, node);
284 if (rec1->devid > rec2->devid)
286 else if (rec1->devid < rec2->devid)
292 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
294 struct inode_record *rec;
295 struct inode_backref *backref;
296 struct inode_backref *orig;
299 rec = malloc(sizeof(*rec));
300 memcpy(rec, orig_rec, sizeof(*rec));
302 INIT_LIST_HEAD(&rec->backrefs);
304 list_for_each_entry(orig, &orig_rec->backrefs, list) {
305 size = sizeof(*orig) + orig->namelen + 1;
306 backref = malloc(size);
307 memcpy(backref, orig, size);
308 list_add_tail(&backref->list, &rec->backrefs);
313 static void print_inode_error(struct btrfs_root *root, struct inode_record *rec)
315 u64 root_objectid = root->root_key.objectid;
316 int errors = rec->errors;
320 /* reloc root errors, we print its corresponding fs root objectid*/
321 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
322 root_objectid = root->root_key.offset;
323 fprintf(stderr, "reloc");
325 fprintf(stderr, "root %llu inode %llu errors %x",
326 (unsigned long long) root_objectid,
327 (unsigned long long) rec->ino, rec->errors);
329 if (errors & I_ERR_NO_INODE_ITEM)
330 fprintf(stderr, ", no inode item");
331 if (errors & I_ERR_NO_ORPHAN_ITEM)
332 fprintf(stderr, ", no orphan item");
333 if (errors & I_ERR_DUP_INODE_ITEM)
334 fprintf(stderr, ", dup inode item");
335 if (errors & I_ERR_DUP_DIR_INDEX)
336 fprintf(stderr, ", dup dir index");
337 if (errors & I_ERR_ODD_DIR_ITEM)
338 fprintf(stderr, ", odd dir item");
339 if (errors & I_ERR_ODD_FILE_EXTENT)
340 fprintf(stderr, ", odd file extent");
341 if (errors & I_ERR_BAD_FILE_EXTENT)
342 fprintf(stderr, ", bad file extent");
343 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
344 fprintf(stderr, ", file extent overlap");
345 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
346 fprintf(stderr, ", file extent discount");
347 if (errors & I_ERR_DIR_ISIZE_WRONG)
348 fprintf(stderr, ", dir isize wrong");
349 if (errors & I_ERR_FILE_NBYTES_WRONG)
350 fprintf(stderr, ", nbytes wrong");
351 if (errors & I_ERR_ODD_CSUM_ITEM)
352 fprintf(stderr, ", odd csum item");
353 if (errors & I_ERR_SOME_CSUM_MISSING)
354 fprintf(stderr, ", some csum missing");
355 if (errors & I_ERR_LINK_COUNT_WRONG)
356 fprintf(stderr, ", link count wrong");
357 fprintf(stderr, "\n");
360 static void print_ref_error(int errors)
362 if (errors & REF_ERR_NO_DIR_ITEM)
363 fprintf(stderr, ", no dir item");
364 if (errors & REF_ERR_NO_DIR_INDEX)
365 fprintf(stderr, ", no dir index");
366 if (errors & REF_ERR_NO_INODE_REF)
367 fprintf(stderr, ", no inode ref");
368 if (errors & REF_ERR_DUP_DIR_ITEM)
369 fprintf(stderr, ", dup dir item");
370 if (errors & REF_ERR_DUP_DIR_INDEX)
371 fprintf(stderr, ", dup dir index");
372 if (errors & REF_ERR_DUP_INODE_REF)
373 fprintf(stderr, ", dup inode ref");
374 if (errors & REF_ERR_INDEX_UNMATCH)
375 fprintf(stderr, ", index unmatch");
376 if (errors & REF_ERR_FILETYPE_UNMATCH)
377 fprintf(stderr, ", filetype unmatch");
378 if (errors & REF_ERR_NAME_TOO_LONG)
379 fprintf(stderr, ", name too long");
380 if (errors & REF_ERR_NO_ROOT_REF)
381 fprintf(stderr, ", no root ref");
382 if (errors & REF_ERR_NO_ROOT_BACKREF)
383 fprintf(stderr, ", no root backref");
384 if (errors & REF_ERR_DUP_ROOT_REF)
385 fprintf(stderr, ", dup root ref");
386 if (errors & REF_ERR_DUP_ROOT_BACKREF)
387 fprintf(stderr, ", dup root backref");
388 fprintf(stderr, "\n");
391 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
394 struct ptr_node *node;
395 struct cache_extent *cache;
396 struct inode_record *rec = NULL;
399 cache = lookup_cache_extent(inode_cache, ino, 1);
401 node = container_of(cache, struct ptr_node, cache);
403 if (mod && rec->refs > 1) {
404 node->data = clone_inode_rec(rec);
409 rec = calloc(1, sizeof(*rec));
411 rec->extent_start = (u64)-1;
412 rec->first_extent_gap = (u64)-1;
414 INIT_LIST_HEAD(&rec->backrefs);
416 node = malloc(sizeof(*node));
417 node->cache.start = ino;
418 node->cache.size = 1;
421 if (ino == BTRFS_FREE_INO_OBJECTID)
424 ret = insert_cache_extent(inode_cache, &node->cache);
430 static void free_inode_rec(struct inode_record *rec)
432 struct inode_backref *backref;
437 while (!list_empty(&rec->backrefs)) {
438 backref = list_entry(rec->backrefs.next,
439 struct inode_backref, list);
440 list_del(&backref->list);
446 static int can_free_inode_rec(struct inode_record *rec)
448 if (!rec->errors && rec->checked && rec->found_inode_item &&
449 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
454 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
455 struct inode_record *rec)
457 struct cache_extent *cache;
458 struct inode_backref *tmp, *backref;
459 struct ptr_node *node;
460 unsigned char filetype;
462 if (!rec->found_inode_item)
465 filetype = imode_to_type(rec->imode);
466 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
467 if (backref->found_dir_item && backref->found_dir_index) {
468 if (backref->filetype != filetype)
469 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
470 if (!backref->errors && backref->found_inode_ref) {
471 list_del(&backref->list);
477 if (!rec->checked || rec->merging)
480 if (S_ISDIR(rec->imode)) {
481 if (rec->found_size != rec->isize)
482 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
483 if (rec->found_file_extent)
484 rec->errors |= I_ERR_ODD_FILE_EXTENT;
485 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
486 if (rec->found_dir_item)
487 rec->errors |= I_ERR_ODD_DIR_ITEM;
488 if (rec->found_size != rec->nbytes)
489 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
490 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
491 rec->first_extent_gap = 0;
492 if (rec->nlink > 0 && !no_holes &&
493 (rec->extent_end < rec->isize ||
494 rec->first_extent_gap < rec->isize))
495 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
498 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
499 if (rec->found_csum_item && rec->nodatasum)
500 rec->errors |= I_ERR_ODD_CSUM_ITEM;
501 if (rec->some_csum_missing && !rec->nodatasum)
502 rec->errors |= I_ERR_SOME_CSUM_MISSING;
505 BUG_ON(rec->refs != 1);
506 if (can_free_inode_rec(rec)) {
507 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
508 node = container_of(cache, struct ptr_node, cache);
509 BUG_ON(node->data != rec);
510 remove_cache_extent(inode_cache, &node->cache);
516 static int check_orphan_item(struct btrfs_root *root, u64 ino)
518 struct btrfs_path path;
519 struct btrfs_key key;
522 key.objectid = BTRFS_ORPHAN_OBJECTID;
523 key.type = BTRFS_ORPHAN_ITEM_KEY;
526 btrfs_init_path(&path);
527 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
528 btrfs_release_path(&path);
534 static int process_inode_item(struct extent_buffer *eb,
535 int slot, struct btrfs_key *key,
536 struct shared_node *active_node)
538 struct inode_record *rec;
539 struct btrfs_inode_item *item;
541 rec = active_node->current;
542 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
543 if (rec->found_inode_item) {
544 rec->errors |= I_ERR_DUP_INODE_ITEM;
547 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
548 rec->nlink = btrfs_inode_nlink(eb, item);
549 rec->isize = btrfs_inode_size(eb, item);
550 rec->nbytes = btrfs_inode_nbytes(eb, item);
551 rec->imode = btrfs_inode_mode(eb, item);
552 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
554 rec->found_inode_item = 1;
556 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
557 maybe_free_inode_rec(&active_node->inode_cache, rec);
561 static struct inode_backref *get_inode_backref(struct inode_record *rec,
563 int namelen, u64 dir)
565 struct inode_backref *backref;
567 list_for_each_entry(backref, &rec->backrefs, list) {
568 if (rec->ino == BTRFS_MULTIPLE_OBJECTIDS)
570 if (backref->dir != dir || backref->namelen != namelen)
572 if (memcmp(name, backref->name, namelen))
577 backref = malloc(sizeof(*backref) + namelen + 1);
578 memset(backref, 0, sizeof(*backref));
580 backref->namelen = namelen;
581 memcpy(backref->name, name, namelen);
582 backref->name[namelen] = '\0';
583 list_add_tail(&backref->list, &rec->backrefs);
587 static int add_inode_backref(struct cache_tree *inode_cache,
588 u64 ino, u64 dir, u64 index,
589 const char *name, int namelen,
590 int filetype, int itemtype, int errors)
592 struct inode_record *rec;
593 struct inode_backref *backref;
595 rec = get_inode_rec(inode_cache, ino, 1);
596 backref = get_inode_backref(rec, name, namelen, dir);
598 backref->errors |= errors;
599 if (itemtype == BTRFS_DIR_INDEX_KEY) {
600 if (backref->found_dir_index)
601 backref->errors |= REF_ERR_DUP_DIR_INDEX;
602 if (backref->found_inode_ref && backref->index != index)
603 backref->errors |= REF_ERR_INDEX_UNMATCH;
604 if (backref->found_dir_item && backref->filetype != filetype)
605 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
607 backref->index = index;
608 backref->filetype = filetype;
609 backref->found_dir_index = 1;
610 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
612 if (backref->found_dir_item)
613 backref->errors |= REF_ERR_DUP_DIR_ITEM;
614 if (backref->found_dir_index && backref->filetype != filetype)
615 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
617 backref->filetype = filetype;
618 backref->found_dir_item = 1;
619 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
620 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
621 if (backref->found_inode_ref)
622 backref->errors |= REF_ERR_DUP_INODE_REF;
623 if (backref->found_dir_index && backref->index != index)
624 backref->errors |= REF_ERR_INDEX_UNMATCH;
626 backref->index = index;
628 backref->ref_type = itemtype;
629 backref->found_inode_ref = 1;
634 maybe_free_inode_rec(inode_cache, rec);
638 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
639 struct cache_tree *dst_cache)
641 struct inode_backref *backref;
645 list_for_each_entry(backref, &src->backrefs, list) {
646 if (backref->found_dir_index) {
647 add_inode_backref(dst_cache, dst->ino, backref->dir,
648 backref->index, backref->name,
649 backref->namelen, backref->filetype,
650 BTRFS_DIR_INDEX_KEY, backref->errors);
652 if (backref->found_dir_item) {
654 add_inode_backref(dst_cache, dst->ino,
655 backref->dir, 0, backref->name,
656 backref->namelen, backref->filetype,
657 BTRFS_DIR_ITEM_KEY, backref->errors);
659 if (backref->found_inode_ref) {
660 add_inode_backref(dst_cache, dst->ino,
661 backref->dir, backref->index,
662 backref->name, backref->namelen, 0,
663 backref->ref_type, backref->errors);
667 if (src->found_dir_item)
668 dst->found_dir_item = 1;
669 if (src->found_file_extent)
670 dst->found_file_extent = 1;
671 if (src->found_csum_item)
672 dst->found_csum_item = 1;
673 if (src->some_csum_missing)
674 dst->some_csum_missing = 1;
675 if (dst->first_extent_gap > src->first_extent_gap)
676 dst->first_extent_gap = src->first_extent_gap;
678 BUG_ON(src->found_link < dir_count);
679 dst->found_link += src->found_link - dir_count;
680 dst->found_size += src->found_size;
681 if (src->extent_start != (u64)-1) {
682 if (dst->extent_start == (u64)-1) {
683 dst->extent_start = src->extent_start;
684 dst->extent_end = src->extent_end;
686 if (dst->extent_end > src->extent_start)
687 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
688 else if (dst->extent_end < src->extent_start &&
689 dst->extent_end < dst->first_extent_gap)
690 dst->first_extent_gap = dst->extent_end;
691 if (dst->extent_end < src->extent_end)
692 dst->extent_end = src->extent_end;
696 dst->errors |= src->errors;
697 if (src->found_inode_item) {
698 if (!dst->found_inode_item) {
699 dst->nlink = src->nlink;
700 dst->isize = src->isize;
701 dst->nbytes = src->nbytes;
702 dst->imode = src->imode;
703 dst->nodatasum = src->nodatasum;
704 dst->found_inode_item = 1;
706 dst->errors |= I_ERR_DUP_INODE_ITEM;
714 static int splice_shared_node(struct shared_node *src_node,
715 struct shared_node *dst_node)
717 struct cache_extent *cache;
718 struct ptr_node *node, *ins;
719 struct cache_tree *src, *dst;
720 struct inode_record *rec, *conflict;
725 if (--src_node->refs == 0)
727 if (src_node->current)
728 current_ino = src_node->current->ino;
730 src = &src_node->root_cache;
731 dst = &dst_node->root_cache;
733 cache = search_cache_extent(src, 0);
735 node = container_of(cache, struct ptr_node, cache);
737 cache = next_cache_extent(cache);
740 remove_cache_extent(src, &node->cache);
743 ins = malloc(sizeof(*ins));
744 ins->cache.start = node->cache.start;
745 ins->cache.size = node->cache.size;
749 ret = insert_cache_extent(dst, &ins->cache);
750 if (ret == -EEXIST) {
751 conflict = get_inode_rec(dst, rec->ino, 1);
752 merge_inode_recs(rec, conflict, dst);
754 conflict->checked = 1;
755 if (dst_node->current == conflict)
756 dst_node->current = NULL;
758 maybe_free_inode_rec(dst, conflict);
766 if (src == &src_node->root_cache) {
767 src = &src_node->inode_cache;
768 dst = &dst_node->inode_cache;
772 if (current_ino > 0 && (!dst_node->current ||
773 current_ino > dst_node->current->ino)) {
774 if (dst_node->current) {
775 dst_node->current->checked = 1;
776 maybe_free_inode_rec(dst, dst_node->current);
778 dst_node->current = get_inode_rec(dst, current_ino, 1);
783 static void free_inode_ptr(struct cache_extent *cache)
785 struct ptr_node *node;
786 struct inode_record *rec;
788 node = container_of(cache, struct ptr_node, cache);
794 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
796 static struct shared_node *find_shared_node(struct cache_tree *shared,
799 struct cache_extent *cache;
800 struct shared_node *node;
802 cache = lookup_cache_extent(shared, bytenr, 1);
804 node = container_of(cache, struct shared_node, cache);
810 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
813 struct shared_node *node;
815 node = calloc(1, sizeof(*node));
816 node->cache.start = bytenr;
817 node->cache.size = 1;
818 cache_tree_init(&node->root_cache);
819 cache_tree_init(&node->inode_cache);
822 ret = insert_cache_extent(shared, &node->cache);
827 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
828 struct walk_control *wc, int level)
830 struct shared_node *node;
831 struct shared_node *dest;
833 if (level == wc->active_node)
836 BUG_ON(wc->active_node <= level);
837 node = find_shared_node(&wc->shared, bytenr);
839 add_shared_node(&wc->shared, bytenr, refs);
840 node = find_shared_node(&wc->shared, bytenr);
841 wc->nodes[level] = node;
842 wc->active_node = level;
846 if (wc->root_level == wc->active_node &&
847 btrfs_root_refs(&root->root_item) == 0) {
848 if (--node->refs == 0) {
849 free_inode_recs_tree(&node->root_cache);
850 free_inode_recs_tree(&node->inode_cache);
851 remove_cache_extent(&wc->shared, &node->cache);
857 dest = wc->nodes[wc->active_node];
858 splice_shared_node(node, dest);
859 if (node->refs == 0) {
860 remove_cache_extent(&wc->shared, &node->cache);
866 static int leave_shared_node(struct btrfs_root *root,
867 struct walk_control *wc, int level)
869 struct shared_node *node;
870 struct shared_node *dest;
873 if (level == wc->root_level)
876 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
880 BUG_ON(i >= BTRFS_MAX_LEVEL);
882 node = wc->nodes[wc->active_node];
883 wc->nodes[wc->active_node] = NULL;
886 dest = wc->nodes[wc->active_node];
887 if (wc->active_node < wc->root_level ||
888 btrfs_root_refs(&root->root_item) > 0) {
889 BUG_ON(node->refs <= 1);
890 splice_shared_node(node, dest);
892 BUG_ON(node->refs < 2);
898 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
901 struct btrfs_path path;
902 struct btrfs_key key;
903 struct extent_buffer *leaf;
907 btrfs_init_path(&path);
909 key.objectid = parent_root_id;
910 key.type = BTRFS_ROOT_REF_KEY;
911 key.offset = child_root_id;
912 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
916 btrfs_release_path(&path);
920 key.objectid = child_root_id;
921 key.type = BTRFS_ROOT_BACKREF_KEY;
923 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
929 leaf = path.nodes[0];
930 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
931 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
934 leaf = path.nodes[0];
937 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
938 if (key.objectid != child_root_id ||
939 key.type != BTRFS_ROOT_BACKREF_KEY)
944 if (key.offset == parent_root_id) {
945 btrfs_release_path(&path);
952 btrfs_release_path(&path);
955 return has_parent? 0 : -1;
958 static int process_dir_item(struct btrfs_root *root,
959 struct extent_buffer *eb,
960 int slot, struct btrfs_key *key,
961 struct shared_node *active_node)
971 struct btrfs_dir_item *di;
972 struct inode_record *rec;
973 struct cache_tree *root_cache;
974 struct cache_tree *inode_cache;
975 struct btrfs_key location;
976 char namebuf[BTRFS_NAME_LEN];
978 root_cache = &active_node->root_cache;
979 inode_cache = &active_node->inode_cache;
980 rec = active_node->current;
981 rec->found_dir_item = 1;
983 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
984 total = btrfs_item_size_nr(eb, slot);
985 while (cur < total) {
987 btrfs_dir_item_key_to_cpu(eb, di, &location);
988 name_len = btrfs_dir_name_len(eb, di);
989 data_len = btrfs_dir_data_len(eb, di);
990 filetype = btrfs_dir_type(eb, di);
992 rec->found_size += name_len;
993 if (name_len <= BTRFS_NAME_LEN) {
997 len = BTRFS_NAME_LEN;
998 error = REF_ERR_NAME_TOO_LONG;
1000 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
1002 if (location.type == BTRFS_INODE_ITEM_KEY) {
1003 add_inode_backref(inode_cache, location.objectid,
1004 key->objectid, key->offset, namebuf,
1005 len, filetype, key->type, error);
1006 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
1007 add_inode_backref(root_cache, location.objectid,
1008 key->objectid, key->offset,
1009 namebuf, len, filetype,
1012 fprintf(stderr, "invalid location in dir item %u\n",
1014 add_inode_backref(inode_cache, BTRFS_MULTIPLE_OBJECTIDS,
1015 key->objectid, key->offset, namebuf,
1016 len, filetype, key->type, error);
1019 len = sizeof(*di) + name_len + data_len;
1020 di = (struct btrfs_dir_item *)((char *)di + len);
1023 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1024 rec->errors |= I_ERR_DUP_DIR_INDEX;
1029 static int process_inode_ref(struct extent_buffer *eb,
1030 int slot, struct btrfs_key *key,
1031 struct shared_node *active_node)
1039 struct cache_tree *inode_cache;
1040 struct btrfs_inode_ref *ref;
1041 char namebuf[BTRFS_NAME_LEN];
1043 inode_cache = &active_node->inode_cache;
1045 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1046 total = btrfs_item_size_nr(eb, slot);
1047 while (cur < total) {
1048 name_len = btrfs_inode_ref_name_len(eb, ref);
1049 index = btrfs_inode_ref_index(eb, ref);
1050 if (name_len <= BTRFS_NAME_LEN) {
1054 len = BTRFS_NAME_LEN;
1055 error = REF_ERR_NAME_TOO_LONG;
1057 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1058 add_inode_backref(inode_cache, key->objectid, key->offset,
1059 index, namebuf, len, 0, key->type, error);
1061 len = sizeof(*ref) + name_len;
1062 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1068 static int process_inode_extref(struct extent_buffer *eb,
1069 int slot, struct btrfs_key *key,
1070 struct shared_node *active_node)
1079 struct cache_tree *inode_cache;
1080 struct btrfs_inode_extref *extref;
1081 char namebuf[BTRFS_NAME_LEN];
1083 inode_cache = &active_node->inode_cache;
1085 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1086 total = btrfs_item_size_nr(eb, slot);
1087 while (cur < total) {
1088 name_len = btrfs_inode_extref_name_len(eb, extref);
1089 index = btrfs_inode_extref_index(eb, extref);
1090 parent = btrfs_inode_extref_parent(eb, extref);
1091 if (name_len <= BTRFS_NAME_LEN) {
1095 len = BTRFS_NAME_LEN;
1096 error = REF_ERR_NAME_TOO_LONG;
1098 read_extent_buffer(eb, namebuf,
1099 (unsigned long)(extref + 1), len);
1100 add_inode_backref(inode_cache, key->objectid, parent,
1101 index, namebuf, len, 0, key->type, error);
1103 len = sizeof(*extref) + name_len;
1104 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1111 static int count_csum_range(struct btrfs_root *root, u64 start,
1112 u64 len, u64 *found)
1114 struct btrfs_key key;
1115 struct btrfs_path path;
1116 struct extent_buffer *leaf;
1121 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1123 btrfs_init_path(&path);
1125 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1127 key.type = BTRFS_EXTENT_CSUM_KEY;
1129 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1133 if (ret > 0 && path.slots[0] > 0) {
1134 leaf = path.nodes[0];
1135 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1136 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1137 key.type == BTRFS_EXTENT_CSUM_KEY)
1142 leaf = path.nodes[0];
1143 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1144 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1149 leaf = path.nodes[0];
1152 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1153 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1154 key.type != BTRFS_EXTENT_CSUM_KEY)
1157 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1158 if (key.offset >= start + len)
1161 if (key.offset > start)
1164 size = btrfs_item_size_nr(leaf, path.slots[0]);
1165 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1166 if (csum_end > start) {
1167 size = min(csum_end - start, len);
1178 btrfs_release_path(&path);
1182 static int process_file_extent(struct btrfs_root *root,
1183 struct extent_buffer *eb,
1184 int slot, struct btrfs_key *key,
1185 struct shared_node *active_node)
1187 struct inode_record *rec;
1188 struct btrfs_file_extent_item *fi;
1190 u64 disk_bytenr = 0;
1191 u64 extent_offset = 0;
1192 u64 mask = root->sectorsize - 1;
1196 rec = active_node->current;
1197 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1198 rec->found_file_extent = 1;
1200 if (rec->extent_start == (u64)-1) {
1201 rec->extent_start = key->offset;
1202 rec->extent_end = key->offset;
1205 if (rec->extent_end > key->offset)
1206 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1207 else if (rec->extent_end < key->offset &&
1208 rec->extent_end < rec->first_extent_gap)
1209 rec->first_extent_gap = rec->extent_end;
1211 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1212 extent_type = btrfs_file_extent_type(eb, fi);
1214 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1215 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1217 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1218 rec->found_size += num_bytes;
1219 num_bytes = (num_bytes + mask) & ~mask;
1220 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1221 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1222 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1223 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1224 extent_offset = btrfs_file_extent_offset(eb, fi);
1225 if (num_bytes == 0 || (num_bytes & mask))
1226 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1227 if (num_bytes + extent_offset >
1228 btrfs_file_extent_ram_bytes(eb, fi))
1229 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1230 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1231 (btrfs_file_extent_compression(eb, fi) ||
1232 btrfs_file_extent_encryption(eb, fi) ||
1233 btrfs_file_extent_other_encoding(eb, fi)))
1234 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1235 if (disk_bytenr > 0)
1236 rec->found_size += num_bytes;
1238 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1240 rec->extent_end = key->offset + num_bytes;
1242 if (disk_bytenr > 0) {
1244 if (btrfs_file_extent_compression(eb, fi))
1245 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1247 disk_bytenr += extent_offset;
1249 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1252 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1254 rec->found_csum_item = 1;
1255 if (found < num_bytes)
1256 rec->some_csum_missing = 1;
1257 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1259 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1265 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1266 struct walk_control *wc)
1268 struct btrfs_key key;
1272 struct cache_tree *inode_cache;
1273 struct shared_node *active_node;
1275 if (wc->root_level == wc->active_node &&
1276 btrfs_root_refs(&root->root_item) == 0)
1279 active_node = wc->nodes[wc->active_node];
1280 inode_cache = &active_node->inode_cache;
1281 nritems = btrfs_header_nritems(eb);
1282 for (i = 0; i < nritems; i++) {
1283 btrfs_item_key_to_cpu(eb, &key, i);
1285 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1287 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1290 if (active_node->current == NULL ||
1291 active_node->current->ino < key.objectid) {
1292 if (active_node->current) {
1293 active_node->current->checked = 1;
1294 maybe_free_inode_rec(inode_cache,
1295 active_node->current);
1297 active_node->current = get_inode_rec(inode_cache,
1301 case BTRFS_DIR_ITEM_KEY:
1302 case BTRFS_DIR_INDEX_KEY:
1303 ret = process_dir_item(root, eb, i, &key, active_node);
1305 case BTRFS_INODE_REF_KEY:
1306 ret = process_inode_ref(eb, i, &key, active_node);
1308 case BTRFS_INODE_EXTREF_KEY:
1309 ret = process_inode_extref(eb, i, &key, active_node);
1311 case BTRFS_INODE_ITEM_KEY:
1312 ret = process_inode_item(eb, i, &key, active_node);
1314 case BTRFS_EXTENT_DATA_KEY:
1315 ret = process_file_extent(root, eb, i, &key,
1325 static void reada_walk_down(struct btrfs_root *root,
1326 struct extent_buffer *node, int slot)
1335 level = btrfs_header_level(node);
1339 nritems = btrfs_header_nritems(node);
1340 blocksize = btrfs_level_size(root, level - 1);
1341 for (i = slot; i < nritems; i++) {
1342 bytenr = btrfs_node_blockptr(node, i);
1343 ptr_gen = btrfs_node_ptr_generation(node, i);
1344 readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1349 * Check the child node/leaf by the following condition:
1350 * 1. the first item key of the node/leaf should be the same with the one
1352 * 2. block in parent node should match the child node/leaf.
1353 * 3. generation of parent node and child's header should be consistent.
1355 * Or the child node/leaf pointed by the key in parent is not valid.
1357 * We hope to check leaf owner too, but since subvol may share leaves,
1358 * which makes leaf owner check not so strong, key check should be
1359 * sufficient enough for that case.
1361 static int check_child_node(struct btrfs_root *root,
1362 struct extent_buffer *parent, int slot,
1363 struct extent_buffer *child)
1365 struct btrfs_key parent_key;
1366 struct btrfs_key child_key;
1369 btrfs_node_key_to_cpu(parent, &parent_key, slot);
1370 if (btrfs_header_level(child) == 0)
1371 btrfs_item_key_to_cpu(child, &child_key, 0);
1373 btrfs_node_key_to_cpu(child, &child_key, 0);
1375 if (memcmp(&parent_key, &child_key, sizeof(parent_key))) {
1378 "Wrong key of child node/leaf, wanted: (%llu, %u, %llu), have: (%llu, %u, %llu)\n",
1379 parent_key.objectid, parent_key.type, parent_key.offset,
1380 child_key.objectid, child_key.type, child_key.offset);
1382 if (btrfs_header_bytenr(child) != btrfs_node_blockptr(parent, slot)) {
1384 fprintf(stderr, "Wrong block of child node/leaf, wanted: %llu, have: %llu\n",
1385 btrfs_node_blockptr(parent, slot),
1386 btrfs_header_bytenr(child));
1388 if (btrfs_node_ptr_generation(parent, slot) !=
1389 btrfs_header_generation(child)) {
1391 fprintf(stderr, "Wrong generation of child node/leaf, wanted: %llu, have: %llu\n",
1392 btrfs_header_generation(child),
1393 btrfs_node_ptr_generation(parent, slot));
1398 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1399 struct walk_control *wc, int *level)
1401 enum btrfs_tree_block_status status;
1404 struct extent_buffer *next;
1405 struct extent_buffer *cur;
1410 WARN_ON(*level < 0);
1411 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1412 ret = btrfs_lookup_extent_info(NULL, root,
1413 path->nodes[*level]->start,
1414 *level, 1, &refs, NULL);
1421 ret = enter_shared_node(root, path->nodes[*level]->start,
1429 while (*level >= 0) {
1430 WARN_ON(*level < 0);
1431 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1432 cur = path->nodes[*level];
1434 if (btrfs_header_level(cur) != *level)
1437 if (path->slots[*level] >= btrfs_header_nritems(cur))
1440 ret = process_one_leaf(root, cur, wc);
1445 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1446 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1447 blocksize = btrfs_level_size(root, *level - 1);
1448 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1454 ret = enter_shared_node(root, bytenr, refs,
1457 path->slots[*level]++;
1462 next = btrfs_find_tree_block(root, bytenr, blocksize);
1463 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1464 free_extent_buffer(next);
1465 reada_walk_down(root, cur, path->slots[*level]);
1466 next = read_tree_block(root, bytenr, blocksize,
1474 ret = check_child_node(root, cur, path->slots[*level], next);
1480 if (btrfs_is_leaf(next))
1481 status = btrfs_check_leaf(root, NULL, next);
1483 status = btrfs_check_node(root, NULL, next);
1484 if (status != BTRFS_TREE_BLOCK_CLEAN) {
1485 free_extent_buffer(next);
1490 *level = *level - 1;
1491 free_extent_buffer(path->nodes[*level]);
1492 path->nodes[*level] = next;
1493 path->slots[*level] = 0;
1496 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1500 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1501 struct walk_control *wc, int *level)
1504 struct extent_buffer *leaf;
1506 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1507 leaf = path->nodes[i];
1508 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1513 free_extent_buffer(path->nodes[*level]);
1514 path->nodes[*level] = NULL;
1515 BUG_ON(*level > wc->active_node);
1516 if (*level == wc->active_node)
1517 leave_shared_node(root, wc, *level);
1524 static int check_root_dir(struct inode_record *rec)
1526 struct inode_backref *backref;
1529 if (!rec->found_inode_item || rec->errors)
1531 if (rec->nlink != 1 || rec->found_link != 0)
1533 if (list_empty(&rec->backrefs))
1535 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1536 if (!backref->found_inode_ref)
1538 if (backref->index != 0 || backref->namelen != 2 ||
1539 memcmp(backref->name, "..", 2))
1541 if (backref->found_dir_index || backref->found_dir_item)
1548 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1549 struct btrfs_root *root, struct btrfs_path *path,
1550 struct inode_record *rec)
1552 struct btrfs_inode_item *ei;
1553 struct btrfs_key key;
1556 key.objectid = rec->ino;
1557 key.type = BTRFS_INODE_ITEM_KEY;
1558 key.offset = (u64)-1;
1560 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1564 if (!path->slots[0]) {
1571 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1572 if (key.objectid != rec->ino) {
1577 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1578 struct btrfs_inode_item);
1579 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1580 btrfs_mark_buffer_dirty(path->nodes[0]);
1581 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1582 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1583 root->root_key.objectid);
1585 btrfs_release_path(path);
1589 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1590 struct btrfs_root *root,
1591 struct btrfs_path *path,
1592 struct inode_record *rec)
1594 struct btrfs_key key;
1597 key.objectid = BTRFS_ORPHAN_OBJECTID;
1598 key.type = BTRFS_ORPHAN_ITEM_KEY;
1599 key.offset = rec->ino;
1601 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1602 btrfs_release_path(path);
1604 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1608 static int add_missing_dir_index(struct btrfs_root *root,
1609 struct cache_tree *inode_cache,
1610 struct inode_record *rec,
1611 struct inode_backref *backref)
1613 struct btrfs_path *path;
1614 struct btrfs_trans_handle *trans;
1615 struct btrfs_dir_item *dir_item;
1616 struct extent_buffer *leaf;
1617 struct btrfs_key key;
1618 struct btrfs_disk_key disk_key;
1619 struct inode_record *dir_rec;
1620 unsigned long name_ptr;
1621 u32 data_size = sizeof(*dir_item) + backref->namelen;
1624 path = btrfs_alloc_path();
1628 trans = btrfs_start_transaction(root, 1);
1629 if (IS_ERR(trans)) {
1630 btrfs_free_path(path);
1631 return PTR_ERR(trans);
1634 fprintf(stderr, "repairing missing dir index item for inode %llu\n",
1635 (unsigned long long)rec->ino);
1636 key.objectid = backref->dir;
1637 key.type = BTRFS_DIR_INDEX_KEY;
1638 key.offset = backref->index;
1640 ret = btrfs_insert_empty_item(trans, root, path, &key, data_size);
1643 leaf = path->nodes[0];
1644 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
1646 disk_key.objectid = cpu_to_le64(rec->ino);
1647 disk_key.type = BTRFS_INODE_ITEM_KEY;
1648 disk_key.offset = 0;
1650 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
1651 btrfs_set_dir_type(leaf, dir_item, imode_to_type(rec->imode));
1652 btrfs_set_dir_data_len(leaf, dir_item, 0);
1653 btrfs_set_dir_name_len(leaf, dir_item, backref->namelen);
1654 name_ptr = (unsigned long)(dir_item + 1);
1655 write_extent_buffer(leaf, backref->name, name_ptr, backref->namelen);
1656 btrfs_mark_buffer_dirty(leaf);
1657 btrfs_free_path(path);
1658 btrfs_commit_transaction(trans, root);
1660 backref->found_dir_index = 1;
1661 dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
1664 dir_rec->found_size += backref->namelen;
1665 if (dir_rec->found_size == dir_rec->isize &&
1666 (dir_rec->errors & I_ERR_DIR_ISIZE_WRONG))
1667 dir_rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1668 if (dir_rec->found_size != dir_rec->isize)
1669 dir_rec->errors |= I_ERR_DIR_ISIZE_WRONG;
1674 static int delete_dir_index(struct btrfs_root *root,
1675 struct cache_tree *inode_cache,
1676 struct inode_record *rec,
1677 struct inode_backref *backref)
1679 struct btrfs_trans_handle *trans;
1680 struct btrfs_dir_item *di;
1681 struct btrfs_path *path;
1684 path = btrfs_alloc_path();
1688 trans = btrfs_start_transaction(root, 1);
1689 if (IS_ERR(trans)) {
1690 btrfs_free_path(path);
1691 return PTR_ERR(trans);
1695 fprintf(stderr, "Deleting bad dir index [%llu,%u,%llu] root %llu\n",
1696 (unsigned long long)backref->dir,
1697 BTRFS_DIR_INDEX_KEY, (unsigned long long)backref->index,
1698 (unsigned long long)root->objectid);
1700 di = btrfs_lookup_dir_index(trans, root, path, backref->dir,
1701 backref->name, backref->namelen,
1702 backref->index, -1);
1705 btrfs_free_path(path);
1706 btrfs_commit_transaction(trans, root);
1713 ret = btrfs_del_item(trans, root, path);
1715 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1717 btrfs_free_path(path);
1718 btrfs_commit_transaction(trans, root);
1722 static int repair_inode_backrefs(struct btrfs_root *root,
1723 struct inode_record *rec,
1724 struct cache_tree *inode_cache,
1727 struct inode_backref *tmp, *backref;
1728 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1732 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
1733 /* Index 0 for root dir's are special, don't mess with it */
1734 if (rec->ino == root_dirid && backref->index == 0)
1738 ((backref->found_dir_index && !backref->found_inode_ref) ||
1739 (backref->found_dir_index && backref->found_inode_ref &&
1740 (backref->errors & REF_ERR_INDEX_UNMATCH)))) {
1741 ret = delete_dir_index(root, inode_cache, rec, backref);
1745 list_del(&backref->list);
1749 if (!delete && !backref->found_dir_index &&
1750 backref->found_dir_item && backref->found_inode_ref) {
1751 ret = add_missing_dir_index(root, inode_cache, rec,
1756 if (backref->found_dir_item &&
1757 backref->found_dir_index &&
1758 backref->found_dir_index) {
1759 if (!backref->errors &&
1760 backref->found_inode_ref) {
1761 list_del(&backref->list);
1768 return ret ? ret : repaired;
1771 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1773 struct btrfs_trans_handle *trans;
1774 struct btrfs_path *path;
1777 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1780 path = btrfs_alloc_path();
1784 trans = btrfs_start_transaction(root, 1);
1785 if (IS_ERR(trans)) {
1786 btrfs_free_path(path);
1787 return PTR_ERR(trans);
1790 if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
1791 ret = repair_inode_isize(trans, root, path, rec);
1792 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
1793 ret = repair_inode_orphan_item(trans, root, path, rec);
1794 btrfs_commit_transaction(trans, root);
1795 btrfs_free_path(path);
1799 static int check_inode_recs(struct btrfs_root *root,
1800 struct cache_tree *inode_cache)
1802 struct cache_extent *cache;
1803 struct ptr_node *node;
1804 struct inode_record *rec;
1805 struct inode_backref *backref;
1810 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1812 if (btrfs_root_refs(&root->root_item) == 0) {
1813 if (!cache_tree_empty(inode_cache))
1814 fprintf(stderr, "warning line %d\n", __LINE__);
1819 * We need to repair backrefs first because we could change some of the
1820 * errors in the inode recs.
1822 * We also need to go through and delete invalid backrefs first and then
1823 * add the correct ones second. We do this because we may get EEXIST
1824 * when adding back the correct index because we hadn't yet deleted the
1827 * For example, if we were missing a dir index then the directories
1828 * isize would be wrong, so if we fixed the isize to what we thought it
1829 * would be and then fixed the backref we'd still have a invalid fs, so
1830 * we need to add back the dir index and then check to see if the isize
1835 if (stage == 3 && !err)
1838 cache = search_cache_extent(inode_cache, 0);
1839 while (repair && cache) {
1840 node = container_of(cache, struct ptr_node, cache);
1842 cache = next_cache_extent(cache);
1844 /* Need to free everything up and rescan */
1846 remove_cache_extent(inode_cache, &node->cache);
1848 free_inode_rec(rec);
1852 if (list_empty(&rec->backrefs))
1855 ret = repair_inode_backrefs(root, rec, inode_cache,
1869 rec = get_inode_rec(inode_cache, root_dirid, 0);
1871 ret = check_root_dir(rec);
1873 fprintf(stderr, "root %llu root dir %llu error\n",
1874 (unsigned long long)root->root_key.objectid,
1875 (unsigned long long)root_dirid);
1879 fprintf(stderr, "root %llu root dir %llu not found\n",
1880 (unsigned long long)root->root_key.objectid,
1881 (unsigned long long)root_dirid);
1885 cache = search_cache_extent(inode_cache, 0);
1888 node = container_of(cache, struct ptr_node, cache);
1890 remove_cache_extent(inode_cache, &node->cache);
1892 if (rec->ino == root_dirid ||
1893 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1894 free_inode_rec(rec);
1898 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1899 ret = check_orphan_item(root, rec->ino);
1901 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1902 if (can_free_inode_rec(rec)) {
1903 free_inode_rec(rec);
1909 ret = try_repair_inode(root, rec);
1910 if (ret == 0 && can_free_inode_rec(rec)) {
1911 free_inode_rec(rec);
1918 if (!rec->found_inode_item)
1919 rec->errors |= I_ERR_NO_INODE_ITEM;
1920 if (rec->found_link != rec->nlink)
1921 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1922 print_inode_error(root, rec);
1923 list_for_each_entry(backref, &rec->backrefs, list) {
1924 if (!backref->found_dir_item)
1925 backref->errors |= REF_ERR_NO_DIR_ITEM;
1926 if (!backref->found_dir_index)
1927 backref->errors |= REF_ERR_NO_DIR_INDEX;
1928 if (!backref->found_inode_ref)
1929 backref->errors |= REF_ERR_NO_INODE_REF;
1930 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1931 " namelen %u name %s filetype %d errors %x",
1932 (unsigned long long)backref->dir,
1933 (unsigned long long)backref->index,
1934 backref->namelen, backref->name,
1935 backref->filetype, backref->errors);
1936 print_ref_error(backref->errors);
1938 free_inode_rec(rec);
1940 return (error > 0) ? -1 : 0;
1943 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1946 struct cache_extent *cache;
1947 struct root_record *rec = NULL;
1950 cache = lookup_cache_extent(root_cache, objectid, 1);
1952 rec = container_of(cache, struct root_record, cache);
1954 rec = calloc(1, sizeof(*rec));
1955 rec->objectid = objectid;
1956 INIT_LIST_HEAD(&rec->backrefs);
1957 rec->cache.start = objectid;
1958 rec->cache.size = 1;
1960 ret = insert_cache_extent(root_cache, &rec->cache);
1966 static struct root_backref *get_root_backref(struct root_record *rec,
1967 u64 ref_root, u64 dir, u64 index,
1968 const char *name, int namelen)
1970 struct root_backref *backref;
1972 list_for_each_entry(backref, &rec->backrefs, list) {
1973 if (backref->ref_root != ref_root || backref->dir != dir ||
1974 backref->namelen != namelen)
1976 if (memcmp(name, backref->name, namelen))
1981 backref = malloc(sizeof(*backref) + namelen + 1);
1982 memset(backref, 0, sizeof(*backref));
1983 backref->ref_root = ref_root;
1985 backref->index = index;
1986 backref->namelen = namelen;
1987 memcpy(backref->name, name, namelen);
1988 backref->name[namelen] = '\0';
1989 list_add_tail(&backref->list, &rec->backrefs);
1993 static void free_root_record(struct cache_extent *cache)
1995 struct root_record *rec;
1996 struct root_backref *backref;
1998 rec = container_of(cache, struct root_record, cache);
1999 while (!list_empty(&rec->backrefs)) {
2000 backref = list_entry(rec->backrefs.next,
2001 struct root_backref, list);
2002 list_del(&backref->list);
2009 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
2011 static int add_root_backref(struct cache_tree *root_cache,
2012 u64 root_id, u64 ref_root, u64 dir, u64 index,
2013 const char *name, int namelen,
2014 int item_type, int errors)
2016 struct root_record *rec;
2017 struct root_backref *backref;
2019 rec = get_root_rec(root_cache, root_id);
2020 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
2022 backref->errors |= errors;
2024 if (item_type != BTRFS_DIR_ITEM_KEY) {
2025 if (backref->found_dir_index || backref->found_back_ref ||
2026 backref->found_forward_ref) {
2027 if (backref->index != index)
2028 backref->errors |= REF_ERR_INDEX_UNMATCH;
2030 backref->index = index;
2034 if (item_type == BTRFS_DIR_ITEM_KEY) {
2035 if (backref->found_forward_ref)
2037 backref->found_dir_item = 1;
2038 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
2039 backref->found_dir_index = 1;
2040 } else if (item_type == BTRFS_ROOT_REF_KEY) {
2041 if (backref->found_forward_ref)
2042 backref->errors |= REF_ERR_DUP_ROOT_REF;
2043 else if (backref->found_dir_item)
2045 backref->found_forward_ref = 1;
2046 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
2047 if (backref->found_back_ref)
2048 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
2049 backref->found_back_ref = 1;
2054 if (backref->found_forward_ref && backref->found_dir_item)
2055 backref->reachable = 1;
2059 static int merge_root_recs(struct btrfs_root *root,
2060 struct cache_tree *src_cache,
2061 struct cache_tree *dst_cache)
2063 struct cache_extent *cache;
2064 struct ptr_node *node;
2065 struct inode_record *rec;
2066 struct inode_backref *backref;
2069 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2070 free_inode_recs_tree(src_cache);
2075 cache = search_cache_extent(src_cache, 0);
2078 node = container_of(cache, struct ptr_node, cache);
2080 remove_cache_extent(src_cache, &node->cache);
2083 ret = is_child_root(root, root->objectid, rec->ino);
2089 list_for_each_entry(backref, &rec->backrefs, list) {
2090 BUG_ON(backref->found_inode_ref);
2091 if (backref->found_dir_item)
2092 add_root_backref(dst_cache, rec->ino,
2093 root->root_key.objectid, backref->dir,
2094 backref->index, backref->name,
2095 backref->namelen, BTRFS_DIR_ITEM_KEY,
2097 if (backref->found_dir_index)
2098 add_root_backref(dst_cache, rec->ino,
2099 root->root_key.objectid, backref->dir,
2100 backref->index, backref->name,
2101 backref->namelen, BTRFS_DIR_INDEX_KEY,
2105 free_inode_rec(rec);
2112 static int check_root_refs(struct btrfs_root *root,
2113 struct cache_tree *root_cache)
2115 struct root_record *rec;
2116 struct root_record *ref_root;
2117 struct root_backref *backref;
2118 struct cache_extent *cache;
2124 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
2127 /* fixme: this can not detect circular references */
2130 cache = search_cache_extent(root_cache, 0);
2134 rec = container_of(cache, struct root_record, cache);
2135 cache = next_cache_extent(cache);
2137 if (rec->found_ref == 0)
2140 list_for_each_entry(backref, &rec->backrefs, list) {
2141 if (!backref->reachable)
2144 ref_root = get_root_rec(root_cache,
2146 if (ref_root->found_ref > 0)
2149 backref->reachable = 0;
2151 if (rec->found_ref == 0)
2157 cache = search_cache_extent(root_cache, 0);
2161 rec = container_of(cache, struct root_record, cache);
2162 cache = next_cache_extent(cache);
2164 if (rec->found_ref == 0 &&
2165 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
2166 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
2167 ret = check_orphan_item(root->fs_info->tree_root,
2173 * If we don't have a root item then we likely just have
2174 * a dir item in a snapshot for this root but no actual
2175 * ref key or anything so it's meaningless.
2177 if (!rec->found_root_item)
2180 fprintf(stderr, "fs tree %llu not referenced\n",
2181 (unsigned long long)rec->objectid);
2185 if (rec->found_ref > 0 && !rec->found_root_item)
2187 list_for_each_entry(backref, &rec->backrefs, list) {
2188 if (!backref->found_dir_item)
2189 backref->errors |= REF_ERR_NO_DIR_ITEM;
2190 if (!backref->found_dir_index)
2191 backref->errors |= REF_ERR_NO_DIR_INDEX;
2192 if (!backref->found_back_ref)
2193 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
2194 if (!backref->found_forward_ref)
2195 backref->errors |= REF_ERR_NO_ROOT_REF;
2196 if (backref->reachable && backref->errors)
2203 fprintf(stderr, "fs tree %llu refs %u %s\n",
2204 (unsigned long long)rec->objectid, rec->found_ref,
2205 rec->found_root_item ? "" : "not found");
2207 list_for_each_entry(backref, &rec->backrefs, list) {
2208 if (!backref->reachable)
2210 if (!backref->errors && rec->found_root_item)
2212 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
2213 " index %llu namelen %u name %s errors %x\n",
2214 (unsigned long long)backref->ref_root,
2215 (unsigned long long)backref->dir,
2216 (unsigned long long)backref->index,
2217 backref->namelen, backref->name,
2219 print_ref_error(backref->errors);
2222 return errors > 0 ? 1 : 0;
2225 static int process_root_ref(struct extent_buffer *eb, int slot,
2226 struct btrfs_key *key,
2227 struct cache_tree *root_cache)
2233 struct btrfs_root_ref *ref;
2234 char namebuf[BTRFS_NAME_LEN];
2237 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
2239 dirid = btrfs_root_ref_dirid(eb, ref);
2240 index = btrfs_root_ref_sequence(eb, ref);
2241 name_len = btrfs_root_ref_name_len(eb, ref);
2243 if (name_len <= BTRFS_NAME_LEN) {
2247 len = BTRFS_NAME_LEN;
2248 error = REF_ERR_NAME_TOO_LONG;
2250 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
2252 if (key->type == BTRFS_ROOT_REF_KEY) {
2253 add_root_backref(root_cache, key->offset, key->objectid, dirid,
2254 index, namebuf, len, key->type, error);
2256 add_root_backref(root_cache, key->objectid, key->offset, dirid,
2257 index, namebuf, len, key->type, error);
2262 static int check_fs_root(struct btrfs_root *root,
2263 struct cache_tree *root_cache,
2264 struct walk_control *wc)
2270 struct btrfs_path path;
2271 struct shared_node root_node;
2272 struct root_record *rec;
2273 struct btrfs_root_item *root_item = &root->root_item;
2274 enum btrfs_tree_block_status status;
2276 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2277 rec = get_root_rec(root_cache, root->root_key.objectid);
2278 if (btrfs_root_refs(root_item) > 0)
2279 rec->found_root_item = 1;
2282 btrfs_init_path(&path);
2283 memset(&root_node, 0, sizeof(root_node));
2284 cache_tree_init(&root_node.root_cache);
2285 cache_tree_init(&root_node.inode_cache);
2287 level = btrfs_header_level(root->node);
2288 memset(wc->nodes, 0, sizeof(wc->nodes));
2289 wc->nodes[level] = &root_node;
2290 wc->active_node = level;
2291 wc->root_level = level;
2293 /* We may not have checked the root block, lets do that now */
2294 if (btrfs_is_leaf(root->node))
2295 status = btrfs_check_leaf(root, NULL, root->node);
2297 status = btrfs_check_node(root, NULL, root->node);
2298 if (status != BTRFS_TREE_BLOCK_CLEAN)
2301 if (btrfs_root_refs(root_item) > 0 ||
2302 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2303 path.nodes[level] = root->node;
2304 extent_buffer_get(root->node);
2305 path.slots[level] = 0;
2307 struct btrfs_key key;
2308 struct btrfs_disk_key found_key;
2310 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2311 level = root_item->drop_level;
2312 path.lowest_level = level;
2313 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2316 btrfs_node_key(path.nodes[level], &found_key,
2318 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2319 sizeof(found_key)));
2323 wret = walk_down_tree(root, &path, wc, &level);
2329 wret = walk_up_tree(root, &path, wc, &level);
2336 btrfs_release_path(&path);
2338 err = merge_root_recs(root, &root_node.root_cache, root_cache);
2342 if (root_node.current) {
2343 root_node.current->checked = 1;
2344 maybe_free_inode_rec(&root_node.inode_cache,
2348 err = check_inode_recs(root, &root_node.inode_cache);
2354 static int fs_root_objectid(u64 objectid)
2356 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
2357 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2359 return is_fstree(objectid);
2362 static int check_fs_roots(struct btrfs_root *root,
2363 struct cache_tree *root_cache)
2365 struct btrfs_path path;
2366 struct btrfs_key key;
2367 struct walk_control wc;
2368 struct extent_buffer *leaf, *tree_node;
2369 struct btrfs_root *tmp_root;
2370 struct btrfs_root *tree_root = root->fs_info->tree_root;
2375 * Just in case we made any changes to the extent tree that weren't
2376 * reflected into the free space cache yet.
2379 reset_cached_block_groups(root->fs_info);
2380 memset(&wc, 0, sizeof(wc));
2381 cache_tree_init(&wc.shared);
2382 btrfs_init_path(&path);
2387 key.type = BTRFS_ROOT_ITEM_KEY;
2388 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2393 tree_node = tree_root->node;
2395 if (tree_node != tree_root->node) {
2396 free_root_recs_tree(root_cache);
2397 btrfs_release_path(&path);
2400 leaf = path.nodes[0];
2401 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2402 ret = btrfs_next_leaf(tree_root, &path);
2408 leaf = path.nodes[0];
2410 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2411 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2412 fs_root_objectid(key.objectid)) {
2413 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
2414 tmp_root = btrfs_read_fs_root_no_cache(
2415 root->fs_info, &key);
2417 key.offset = (u64)-1;
2418 tmp_root = btrfs_read_fs_root(
2419 root->fs_info, &key);
2421 if (IS_ERR(tmp_root)) {
2425 ret = check_fs_root(tmp_root, root_cache, &wc);
2426 if (ret == -EAGAIN) {
2427 free_root_recs_tree(root_cache);
2428 btrfs_release_path(&path);
2433 if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
2434 btrfs_free_fs_root(tmp_root);
2435 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2436 key.type == BTRFS_ROOT_BACKREF_KEY) {
2437 process_root_ref(leaf, path.slots[0], &key,
2444 btrfs_release_path(&path);
2446 free_extent_cache_tree(&wc.shared);
2447 if (!cache_tree_empty(&wc.shared))
2448 fprintf(stderr, "warning line %d\n", __LINE__);
2453 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2455 struct list_head *cur = rec->backrefs.next;
2456 struct extent_backref *back;
2457 struct tree_backref *tback;
2458 struct data_backref *dback;
2462 while(cur != &rec->backrefs) {
2463 back = list_entry(cur, struct extent_backref, list);
2465 if (!back->found_extent_tree) {
2469 if (back->is_data) {
2470 dback = (struct data_backref *)back;
2471 fprintf(stderr, "Backref %llu %s %llu"
2472 " owner %llu offset %llu num_refs %lu"
2473 " not found in extent tree\n",
2474 (unsigned long long)rec->start,
2475 back->full_backref ?
2477 back->full_backref ?
2478 (unsigned long long)dback->parent:
2479 (unsigned long long)dback->root,
2480 (unsigned long long)dback->owner,
2481 (unsigned long long)dback->offset,
2482 (unsigned long)dback->num_refs);
2484 tback = (struct tree_backref *)back;
2485 fprintf(stderr, "Backref %llu parent %llu"
2486 " root %llu not found in extent tree\n",
2487 (unsigned long long)rec->start,
2488 (unsigned long long)tback->parent,
2489 (unsigned long long)tback->root);
2492 if (!back->is_data && !back->found_ref) {
2496 tback = (struct tree_backref *)back;
2497 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2498 (unsigned long long)rec->start,
2499 back->full_backref ? "parent" : "root",
2500 back->full_backref ?
2501 (unsigned long long)tback->parent :
2502 (unsigned long long)tback->root, back);
2504 if (back->is_data) {
2505 dback = (struct data_backref *)back;
2506 if (dback->found_ref != dback->num_refs) {
2510 fprintf(stderr, "Incorrect local backref count"
2511 " on %llu %s %llu owner %llu"
2512 " offset %llu found %u wanted %u back %p\n",
2513 (unsigned long long)rec->start,
2514 back->full_backref ?
2516 back->full_backref ?
2517 (unsigned long long)dback->parent:
2518 (unsigned long long)dback->root,
2519 (unsigned long long)dback->owner,
2520 (unsigned long long)dback->offset,
2521 dback->found_ref, dback->num_refs, back);
2523 if (dback->disk_bytenr != rec->start) {
2527 fprintf(stderr, "Backref disk bytenr does not"
2528 " match extent record, bytenr=%llu, "
2529 "ref bytenr=%llu\n",
2530 (unsigned long long)rec->start,
2531 (unsigned long long)dback->disk_bytenr);
2534 if (dback->bytes != rec->nr) {
2538 fprintf(stderr, "Backref bytes do not match "
2539 "extent backref, bytenr=%llu, ref "
2540 "bytes=%llu, backref bytes=%llu\n",
2541 (unsigned long long)rec->start,
2542 (unsigned long long)rec->nr,
2543 (unsigned long long)dback->bytes);
2546 if (!back->is_data) {
2549 dback = (struct data_backref *)back;
2550 found += dback->found_ref;
2553 if (found != rec->refs) {
2557 fprintf(stderr, "Incorrect global backref count "
2558 "on %llu found %llu wanted %llu\n",
2559 (unsigned long long)rec->start,
2560 (unsigned long long)found,
2561 (unsigned long long)rec->refs);
2567 static int free_all_extent_backrefs(struct extent_record *rec)
2569 struct extent_backref *back;
2570 struct list_head *cur;
2571 while (!list_empty(&rec->backrefs)) {
2572 cur = rec->backrefs.next;
2573 back = list_entry(cur, struct extent_backref, list);
2580 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2581 struct cache_tree *extent_cache)
2583 struct cache_extent *cache;
2584 struct extent_record *rec;
2587 cache = first_cache_extent(extent_cache);
2590 rec = container_of(cache, struct extent_record, cache);
2591 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2592 remove_cache_extent(extent_cache, cache);
2593 free_all_extent_backrefs(rec);
2598 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2599 struct extent_record *rec)
2601 if (rec->content_checked && rec->owner_ref_checked &&
2602 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2603 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2604 remove_cache_extent(extent_cache, &rec->cache);
2605 free_all_extent_backrefs(rec);
2606 list_del_init(&rec->list);
2612 static int check_owner_ref(struct btrfs_root *root,
2613 struct extent_record *rec,
2614 struct extent_buffer *buf)
2616 struct extent_backref *node;
2617 struct tree_backref *back;
2618 struct btrfs_root *ref_root;
2619 struct btrfs_key key;
2620 struct btrfs_path path;
2621 struct extent_buffer *parent;
2626 list_for_each_entry(node, &rec->backrefs, list) {
2629 if (!node->found_ref)
2631 if (node->full_backref)
2633 back = (struct tree_backref *)node;
2634 if (btrfs_header_owner(buf) == back->root)
2637 BUG_ON(rec->is_root);
2639 /* try to find the block by search corresponding fs tree */
2640 key.objectid = btrfs_header_owner(buf);
2641 key.type = BTRFS_ROOT_ITEM_KEY;
2642 key.offset = (u64)-1;
2644 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2645 if (IS_ERR(ref_root))
2648 level = btrfs_header_level(buf);
2650 btrfs_item_key_to_cpu(buf, &key, 0);
2652 btrfs_node_key_to_cpu(buf, &key, 0);
2654 btrfs_init_path(&path);
2655 path.lowest_level = level + 1;
2656 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2660 parent = path.nodes[level + 1];
2661 if (parent && buf->start == btrfs_node_blockptr(parent,
2662 path.slots[level + 1]))
2665 btrfs_release_path(&path);
2666 return found ? 0 : 1;
2669 static int is_extent_tree_record(struct extent_record *rec)
2671 struct list_head *cur = rec->backrefs.next;
2672 struct extent_backref *node;
2673 struct tree_backref *back;
2676 while(cur != &rec->backrefs) {
2677 node = list_entry(cur, struct extent_backref, list);
2681 back = (struct tree_backref *)node;
2682 if (node->full_backref)
2684 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2691 static int record_bad_block_io(struct btrfs_fs_info *info,
2692 struct cache_tree *extent_cache,
2695 struct extent_record *rec;
2696 struct cache_extent *cache;
2697 struct btrfs_key key;
2699 cache = lookup_cache_extent(extent_cache, start, len);
2703 rec = container_of(cache, struct extent_record, cache);
2704 if (!is_extent_tree_record(rec))
2707 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2708 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2711 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2712 struct extent_buffer *buf, int slot)
2714 if (btrfs_header_level(buf)) {
2715 struct btrfs_key_ptr ptr1, ptr2;
2717 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2718 sizeof(struct btrfs_key_ptr));
2719 read_extent_buffer(buf, &ptr2,
2720 btrfs_node_key_ptr_offset(slot + 1),
2721 sizeof(struct btrfs_key_ptr));
2722 write_extent_buffer(buf, &ptr1,
2723 btrfs_node_key_ptr_offset(slot + 1),
2724 sizeof(struct btrfs_key_ptr));
2725 write_extent_buffer(buf, &ptr2,
2726 btrfs_node_key_ptr_offset(slot),
2727 sizeof(struct btrfs_key_ptr));
2729 struct btrfs_disk_key key;
2730 btrfs_node_key(buf, &key, 0);
2731 btrfs_fixup_low_keys(root, path, &key,
2732 btrfs_header_level(buf) + 1);
2735 struct btrfs_item *item1, *item2;
2736 struct btrfs_key k1, k2;
2737 char *item1_data, *item2_data;
2738 u32 item1_offset, item2_offset, item1_size, item2_size;
2740 item1 = btrfs_item_nr(slot);
2741 item2 = btrfs_item_nr(slot + 1);
2742 btrfs_item_key_to_cpu(buf, &k1, slot);
2743 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2744 item1_offset = btrfs_item_offset(buf, item1);
2745 item2_offset = btrfs_item_offset(buf, item2);
2746 item1_size = btrfs_item_size(buf, item1);
2747 item2_size = btrfs_item_size(buf, item2);
2749 item1_data = malloc(item1_size);
2752 item2_data = malloc(item2_size);
2758 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2759 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2761 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2762 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2766 btrfs_set_item_offset(buf, item1, item2_offset);
2767 btrfs_set_item_offset(buf, item2, item1_offset);
2768 btrfs_set_item_size(buf, item1, item2_size);
2769 btrfs_set_item_size(buf, item2, item1_size);
2771 path->slots[0] = slot;
2772 btrfs_set_item_key_unsafe(root, path, &k2);
2773 path->slots[0] = slot + 1;
2774 btrfs_set_item_key_unsafe(root, path, &k1);
2779 static int fix_key_order(struct btrfs_trans_handle *trans,
2780 struct btrfs_root *root,
2781 struct btrfs_path *path)
2783 struct extent_buffer *buf;
2784 struct btrfs_key k1, k2;
2786 int level = path->lowest_level;
2789 buf = path->nodes[level];
2790 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2792 btrfs_node_key_to_cpu(buf, &k1, i);
2793 btrfs_node_key_to_cpu(buf, &k2, i + 1);
2795 btrfs_item_key_to_cpu(buf, &k1, i);
2796 btrfs_item_key_to_cpu(buf, &k2, i + 1);
2798 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2800 ret = swap_values(root, path, buf, i);
2803 btrfs_mark_buffer_dirty(buf);
2809 static int delete_bogus_item(struct btrfs_trans_handle *trans,
2810 struct btrfs_root *root,
2811 struct btrfs_path *path,
2812 struct extent_buffer *buf, int slot)
2814 struct btrfs_key key;
2815 int nritems = btrfs_header_nritems(buf);
2817 btrfs_item_key_to_cpu(buf, &key, slot);
2819 /* These are all the keys we can deal with missing. */
2820 if (key.type != BTRFS_DIR_INDEX_KEY &&
2821 key.type != BTRFS_EXTENT_ITEM_KEY &&
2822 key.type != BTRFS_METADATA_ITEM_KEY &&
2823 key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2824 key.type != BTRFS_EXTENT_DATA_REF_KEY)
2827 printf("Deleting bogus item [%llu,%u,%llu] at slot %d on block %llu\n",
2828 (unsigned long long)key.objectid, key.type,
2829 (unsigned long long)key.offset, slot, buf->start);
2830 memmove_extent_buffer(buf, btrfs_item_nr_offset(slot),
2831 btrfs_item_nr_offset(slot + 1),
2832 sizeof(struct btrfs_item) *
2833 (nritems - slot - 1));
2834 btrfs_set_header_nritems(buf, nritems - 1);
2836 struct btrfs_disk_key disk_key;
2838 btrfs_item_key(buf, &disk_key, 0);
2839 btrfs_fixup_low_keys(root, path, &disk_key, 1);
2841 btrfs_mark_buffer_dirty(buf);
2845 static int fix_item_offset(struct btrfs_trans_handle *trans,
2846 struct btrfs_root *root,
2847 struct btrfs_path *path)
2849 struct extent_buffer *buf;
2853 /* We should only get this for leaves */
2854 BUG_ON(path->lowest_level);
2855 buf = path->nodes[0];
2857 for (i = 0; i < btrfs_header_nritems(buf); i++) {
2858 unsigned int shift = 0, offset;
2860 if (i == 0 && btrfs_item_end_nr(buf, i) !=
2861 BTRFS_LEAF_DATA_SIZE(root)) {
2862 if (btrfs_item_end_nr(buf, i) >
2863 BTRFS_LEAF_DATA_SIZE(root)) {
2864 ret = delete_bogus_item(trans, root, path,
2868 fprintf(stderr, "item is off the end of the "
2869 "leaf, can't fix\n");
2873 shift = BTRFS_LEAF_DATA_SIZE(root) -
2874 btrfs_item_end_nr(buf, i);
2875 } else if (i > 0 && btrfs_item_end_nr(buf, i) !=
2876 btrfs_item_offset_nr(buf, i - 1)) {
2877 if (btrfs_item_end_nr(buf, i) >
2878 btrfs_item_offset_nr(buf, i - 1)) {
2879 ret = delete_bogus_item(trans, root, path,
2883 fprintf(stderr, "items overlap, can't fix\n");
2887 shift = btrfs_item_offset_nr(buf, i - 1) -
2888 btrfs_item_end_nr(buf, i);
2893 printf("Shifting item nr %d by %u bytes in block %llu\n",
2894 i, shift, (unsigned long long)buf->start);
2895 offset = btrfs_item_offset_nr(buf, i);
2896 memmove_extent_buffer(buf,
2897 btrfs_leaf_data(buf) + offset + shift,
2898 btrfs_leaf_data(buf) + offset,
2899 btrfs_item_size_nr(buf, i));
2900 btrfs_set_item_offset(buf, btrfs_item_nr(i),
2902 btrfs_mark_buffer_dirty(buf);
2906 * We may have moved things, in which case we want to exit so we don't
2907 * write those changes out. Once we have proper abort functionality in
2908 * progs this can be changed to something nicer.
2915 * Attempt to fix basic block failures. If we can't fix it for whatever reason
2916 * then just return -EIO.
2918 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
2919 struct btrfs_root *root,
2920 struct extent_buffer *buf,
2921 enum btrfs_tree_block_status status)
2923 struct ulist *roots;
2924 struct ulist_node *node;
2925 struct btrfs_root *search_root;
2926 struct btrfs_path *path;
2927 struct ulist_iterator iter;
2928 struct btrfs_key root_key, key;
2931 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER &&
2932 status != BTRFS_TREE_BLOCK_INVALID_OFFSETS)
2935 path = btrfs_alloc_path();
2939 ret = btrfs_find_all_roots(trans, root->fs_info, buf->start,
2942 btrfs_free_path(path);
2946 ULIST_ITER_INIT(&iter);
2947 while ((node = ulist_next(roots, &iter))) {
2948 root_key.objectid = node->val;
2949 root_key.type = BTRFS_ROOT_ITEM_KEY;
2950 root_key.offset = (u64)-1;
2952 search_root = btrfs_read_fs_root(root->fs_info, &root_key);
2958 record_root_in_trans(trans, search_root);
2960 path->lowest_level = btrfs_header_level(buf);
2961 path->skip_check_block = 1;
2962 if (path->lowest_level)
2963 btrfs_node_key_to_cpu(buf, &key, 0);
2965 btrfs_item_key_to_cpu(buf, &key, 0);
2966 ret = btrfs_search_slot(trans, search_root, &key, path, 0, 1);
2971 if (status == BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
2972 ret = fix_key_order(trans, search_root, path);
2973 else if (status == BTRFS_TREE_BLOCK_INVALID_OFFSETS)
2974 ret = fix_item_offset(trans, search_root, path);
2977 btrfs_release_path(path);
2980 btrfs_free_path(path);
2984 static int check_block(struct btrfs_trans_handle *trans,
2985 struct btrfs_root *root,
2986 struct cache_tree *extent_cache,
2987 struct extent_buffer *buf, u64 flags)
2989 struct extent_record *rec;
2990 struct cache_extent *cache;
2991 struct btrfs_key key;
2992 enum btrfs_tree_block_status status;
2996 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2999 rec = container_of(cache, struct extent_record, cache);
3000 rec->generation = btrfs_header_generation(buf);
3002 level = btrfs_header_level(buf);
3003 if (btrfs_header_nritems(buf) > 0) {
3006 btrfs_item_key_to_cpu(buf, &key, 0);
3008 btrfs_node_key_to_cpu(buf, &key, 0);
3010 rec->info_objectid = key.objectid;
3012 rec->info_level = level;
3014 if (btrfs_is_leaf(buf))
3015 status = btrfs_check_leaf(root, &rec->parent_key, buf);
3017 status = btrfs_check_node(root, &rec->parent_key, buf);
3019 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3021 status = try_to_fix_bad_block(trans, root, buf,
3023 if (status != BTRFS_TREE_BLOCK_CLEAN) {
3025 fprintf(stderr, "bad block %llu\n",
3026 (unsigned long long)buf->start);
3029 * Signal to callers we need to start the scan over
3030 * again since we'll have cow'ed blocks.
3035 rec->content_checked = 1;
3036 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3037 rec->owner_ref_checked = 1;
3039 ret = check_owner_ref(root, rec, buf);
3041 rec->owner_ref_checked = 1;
3045 maybe_free_extent_rec(extent_cache, rec);
3049 static struct tree_backref *find_tree_backref(struct extent_record *rec,
3050 u64 parent, u64 root)
3052 struct list_head *cur = rec->backrefs.next;
3053 struct extent_backref *node;
3054 struct tree_backref *back;
3056 while(cur != &rec->backrefs) {
3057 node = list_entry(cur, struct extent_backref, list);
3061 back = (struct tree_backref *)node;
3063 if (!node->full_backref)
3065 if (parent == back->parent)
3068 if (node->full_backref)
3070 if (back->root == root)
3077 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
3078 u64 parent, u64 root)
3080 struct tree_backref *ref = malloc(sizeof(*ref));
3081 memset(&ref->node, 0, sizeof(ref->node));
3083 ref->parent = parent;
3084 ref->node.full_backref = 1;
3087 ref->node.full_backref = 0;
3089 list_add_tail(&ref->node.list, &rec->backrefs);
3094 static struct data_backref *find_data_backref(struct extent_record *rec,
3095 u64 parent, u64 root,
3096 u64 owner, u64 offset,
3098 u64 disk_bytenr, u64 bytes)
3100 struct list_head *cur = rec->backrefs.next;
3101 struct extent_backref *node;
3102 struct data_backref *back;
3104 while(cur != &rec->backrefs) {
3105 node = list_entry(cur, struct extent_backref, list);
3109 back = (struct data_backref *)node;
3111 if (!node->full_backref)
3113 if (parent == back->parent)
3116 if (node->full_backref)
3118 if (back->root == root && back->owner == owner &&
3119 back->offset == offset) {
3120 if (found_ref && node->found_ref &&
3121 (back->bytes != bytes ||
3122 back->disk_bytenr != disk_bytenr))
3131 static struct data_backref *alloc_data_backref(struct extent_record *rec,
3132 u64 parent, u64 root,
3133 u64 owner, u64 offset,
3136 struct data_backref *ref = malloc(sizeof(*ref));
3137 memset(&ref->node, 0, sizeof(ref->node));
3138 ref->node.is_data = 1;
3141 ref->parent = parent;
3144 ref->node.full_backref = 1;
3148 ref->offset = offset;
3149 ref->node.full_backref = 0;
3151 ref->bytes = max_size;
3154 list_add_tail(&ref->node.list, &rec->backrefs);
3155 if (max_size > rec->max_size)
3156 rec->max_size = max_size;
3160 static int add_extent_rec(struct cache_tree *extent_cache,
3161 struct btrfs_key *parent_key, u64 parent_gen,
3162 u64 start, u64 nr, u64 extent_item_refs,
3163 int is_root, int inc_ref, int set_checked,
3164 int metadata, int extent_rec, u64 max_size)
3166 struct extent_record *rec;
3167 struct cache_extent *cache;
3171 cache = lookup_cache_extent(extent_cache, start, nr);
3173 rec = container_of(cache, struct extent_record, cache);
3177 rec->nr = max(nr, max_size);
3180 * We need to make sure to reset nr to whatever the extent
3181 * record says was the real size, this way we can compare it to
3185 if (start != rec->start || rec->found_rec) {
3186 struct extent_record *tmp;
3189 if (list_empty(&rec->list))
3190 list_add_tail(&rec->list,
3191 &duplicate_extents);
3194 * We have to do this song and dance in case we
3195 * find an extent record that falls inside of
3196 * our current extent record but does not have
3197 * the same objectid.
3199 tmp = malloc(sizeof(*tmp));
3203 tmp->max_size = max_size;
3206 tmp->metadata = metadata;
3207 tmp->extent_item_refs = extent_item_refs;
3208 INIT_LIST_HEAD(&tmp->list);
3209 list_add_tail(&tmp->list, &rec->dups);
3210 rec->num_duplicates++;
3217 if (extent_item_refs && !dup) {
3218 if (rec->extent_item_refs) {
3219 fprintf(stderr, "block %llu rec "
3220 "extent_item_refs %llu, passed %llu\n",
3221 (unsigned long long)start,
3222 (unsigned long long)
3223 rec->extent_item_refs,
3224 (unsigned long long)extent_item_refs);
3226 rec->extent_item_refs = extent_item_refs;
3231 rec->content_checked = 1;
3232 rec->owner_ref_checked = 1;
3236 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3238 rec->parent_generation = parent_gen;
3240 if (rec->max_size < max_size)
3241 rec->max_size = max_size;
3243 maybe_free_extent_rec(extent_cache, rec);
3246 rec = malloc(sizeof(*rec));
3248 rec->max_size = max_size;
3249 rec->nr = max(nr, max_size);
3250 rec->found_rec = !!extent_rec;
3251 rec->content_checked = 0;
3252 rec->owner_ref_checked = 0;
3253 rec->num_duplicates = 0;
3254 rec->metadata = metadata;
3255 INIT_LIST_HEAD(&rec->backrefs);
3256 INIT_LIST_HEAD(&rec->dups);
3257 INIT_LIST_HEAD(&rec->list);
3269 if (extent_item_refs)
3270 rec->extent_item_refs = extent_item_refs;
3272 rec->extent_item_refs = 0;
3275 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
3277 memset(&rec->parent_key, 0, sizeof(*parent_key));
3280 rec->parent_generation = parent_gen;
3282 rec->parent_generation = 0;
3284 rec->cache.start = start;
3285 rec->cache.size = nr;
3286 ret = insert_cache_extent(extent_cache, &rec->cache);
3290 rec->content_checked = 1;
3291 rec->owner_ref_checked = 1;
3296 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
3297 u64 parent, u64 root, int found_ref)
3299 struct extent_record *rec;
3300 struct tree_backref *back;
3301 struct cache_extent *cache;
3303 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3305 add_extent_rec(extent_cache, NULL, 0, bytenr,
3306 1, 0, 0, 0, 0, 1, 0, 0);
3307 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3312 rec = container_of(cache, struct extent_record, cache);
3313 if (rec->start != bytenr) {
3317 back = find_tree_backref(rec, parent, root);
3319 back = alloc_tree_backref(rec, parent, root);
3322 if (back->node.found_ref) {
3323 fprintf(stderr, "Extent back ref already exists "
3324 "for %llu parent %llu root %llu \n",
3325 (unsigned long long)bytenr,
3326 (unsigned long long)parent,
3327 (unsigned long long)root);
3329 back->node.found_ref = 1;
3331 if (back->node.found_extent_tree) {
3332 fprintf(stderr, "Extent back ref already exists "
3333 "for %llu parent %llu root %llu \n",
3334 (unsigned long long)bytenr,
3335 (unsigned long long)parent,
3336 (unsigned long long)root);
3338 back->node.found_extent_tree = 1;
3340 maybe_free_extent_rec(extent_cache, rec);
3344 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
3345 u64 parent, u64 root, u64 owner, u64 offset,
3346 u32 num_refs, int found_ref, u64 max_size)
3348 struct extent_record *rec;
3349 struct data_backref *back;
3350 struct cache_extent *cache;
3352 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3354 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
3356 cache = lookup_cache_extent(extent_cache, bytenr, 1);
3361 rec = container_of(cache, struct extent_record, cache);
3362 if (rec->max_size < max_size)
3363 rec->max_size = max_size;
3366 * If found_ref is set then max_size is the real size and must match the
3367 * existing refs. So if we have already found a ref then we need to
3368 * make sure that this ref matches the existing one, otherwise we need
3369 * to add a new backref so we can notice that the backrefs don't match
3370 * and we need to figure out who is telling the truth. This is to
3371 * account for that awful fsync bug I introduced where we'd end up with
3372 * a btrfs_file_extent_item that would have its length include multiple
3373 * prealloc extents or point inside of a prealloc extent.
3375 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
3378 back = alloc_data_backref(rec, parent, root, owner, offset,
3382 BUG_ON(num_refs != 1);
3383 if (back->node.found_ref)
3384 BUG_ON(back->bytes != max_size);
3385 back->node.found_ref = 1;
3386 back->found_ref += 1;
3387 back->bytes = max_size;
3388 back->disk_bytenr = bytenr;
3390 rec->content_checked = 1;
3391 rec->owner_ref_checked = 1;
3393 if (back->node.found_extent_tree) {
3394 fprintf(stderr, "Extent back ref already exists "
3395 "for %llu parent %llu root %llu "
3396 "owner %llu offset %llu num_refs %lu\n",
3397 (unsigned long long)bytenr,
3398 (unsigned long long)parent,
3399 (unsigned long long)root,
3400 (unsigned long long)owner,
3401 (unsigned long long)offset,
3402 (unsigned long)num_refs);
3404 back->num_refs = num_refs;
3405 back->node.found_extent_tree = 1;
3407 maybe_free_extent_rec(extent_cache, rec);
3411 static int add_pending(struct cache_tree *pending,
3412 struct cache_tree *seen, u64 bytenr, u32 size)
3415 ret = add_cache_extent(seen, bytenr, size);
3418 add_cache_extent(pending, bytenr, size);
3422 static int pick_next_pending(struct cache_tree *pending,
3423 struct cache_tree *reada,
3424 struct cache_tree *nodes,
3425 u64 last, struct block_info *bits, int bits_nr,
3428 unsigned long node_start = last;
3429 struct cache_extent *cache;
3432 cache = search_cache_extent(reada, 0);
3434 bits[0].start = cache->start;
3435 bits[0].size = cache->size;
3440 if (node_start > 32768)
3441 node_start -= 32768;
3443 cache = search_cache_extent(nodes, node_start);
3445 cache = search_cache_extent(nodes, 0);
3448 cache = search_cache_extent(pending, 0);
3453 bits[ret].start = cache->start;
3454 bits[ret].size = cache->size;
3455 cache = next_cache_extent(cache);
3457 } while (cache && ret < bits_nr);
3463 bits[ret].start = cache->start;
3464 bits[ret].size = cache->size;
3465 cache = next_cache_extent(cache);
3467 } while (cache && ret < bits_nr);
3469 if (bits_nr - ret > 8) {
3470 u64 lookup = bits[0].start + bits[0].size;
3471 struct cache_extent *next;
3472 next = search_cache_extent(pending, lookup);
3474 if (next->start - lookup > 32768)
3476 bits[ret].start = next->start;
3477 bits[ret].size = next->size;
3478 lookup = next->start + next->size;
3482 next = next_cache_extent(next);
3490 static void free_chunk_record(struct cache_extent *cache)
3492 struct chunk_record *rec;
3494 rec = container_of(cache, struct chunk_record, cache);
3495 list_del_init(&rec->list);
3496 list_del_init(&rec->dextents);
3500 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
3502 cache_tree_free_extents(chunk_cache, free_chunk_record);
3505 static void free_device_record(struct rb_node *node)
3507 struct device_record *rec;
3509 rec = container_of(node, struct device_record, node);
3513 FREE_RB_BASED_TREE(device_cache, free_device_record);
3515 int insert_block_group_record(struct block_group_tree *tree,
3516 struct block_group_record *bg_rec)
3520 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3524 list_add_tail(&bg_rec->list, &tree->block_groups);
3528 static void free_block_group_record(struct cache_extent *cache)
3530 struct block_group_record *rec;
3532 rec = container_of(cache, struct block_group_record, cache);
3533 list_del_init(&rec->list);
3537 void free_block_group_tree(struct block_group_tree *tree)
3539 cache_tree_free_extents(&tree->tree, free_block_group_record);
3542 int insert_device_extent_record(struct device_extent_tree *tree,
3543 struct device_extent_record *de_rec)
3548 * Device extent is a bit different from the other extents, because
3549 * the extents which belong to the different devices may have the
3550 * same start and size, so we need use the special extent cache
3551 * search/insert functions.
3553 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3557 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3558 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3562 static void free_device_extent_record(struct cache_extent *cache)
3564 struct device_extent_record *rec;
3566 rec = container_of(cache, struct device_extent_record, cache);
3567 if (!list_empty(&rec->chunk_list))
3568 list_del_init(&rec->chunk_list);
3569 if (!list_empty(&rec->device_list))
3570 list_del_init(&rec->device_list);
3574 void free_device_extent_tree(struct device_extent_tree *tree)
3576 cache_tree_free_extents(&tree->tree, free_device_extent_record);
3579 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3580 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3581 struct extent_buffer *leaf, int slot)
3583 struct btrfs_extent_ref_v0 *ref0;
3584 struct btrfs_key key;
3586 btrfs_item_key_to_cpu(leaf, &key, slot);
3587 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3588 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3589 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3591 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3592 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3598 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3599 struct btrfs_key *key,
3602 struct btrfs_chunk *ptr;
3603 struct chunk_record *rec;
3606 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3607 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3609 rec = malloc(btrfs_chunk_record_size(num_stripes));
3611 fprintf(stderr, "memory allocation failed\n");
3615 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3617 INIT_LIST_HEAD(&rec->list);
3618 INIT_LIST_HEAD(&rec->dextents);
3621 rec->cache.start = key->offset;
3622 rec->cache.size = btrfs_chunk_length(leaf, ptr);
3624 rec->generation = btrfs_header_generation(leaf);
3626 rec->objectid = key->objectid;
3627 rec->type = key->type;
3628 rec->offset = key->offset;
3630 rec->length = rec->cache.size;
3631 rec->owner = btrfs_chunk_owner(leaf, ptr);
3632 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3633 rec->type_flags = btrfs_chunk_type(leaf, ptr);
3634 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3635 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3636 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3637 rec->num_stripes = num_stripes;
3638 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3640 for (i = 0; i < rec->num_stripes; ++i) {
3641 rec->stripes[i].devid =
3642 btrfs_stripe_devid_nr(leaf, ptr, i);
3643 rec->stripes[i].offset =
3644 btrfs_stripe_offset_nr(leaf, ptr, i);
3645 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3646 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3653 static int process_chunk_item(struct cache_tree *chunk_cache,
3654 struct btrfs_key *key, struct extent_buffer *eb,
3657 struct chunk_record *rec;
3660 rec = btrfs_new_chunk_record(eb, key, slot);
3661 ret = insert_cache_extent(chunk_cache, &rec->cache);
3663 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3664 rec->offset, rec->length);
3671 static int process_device_item(struct rb_root *dev_cache,
3672 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3674 struct btrfs_dev_item *ptr;
3675 struct device_record *rec;
3678 ptr = btrfs_item_ptr(eb,
3679 slot, struct btrfs_dev_item);
3681 rec = malloc(sizeof(*rec));
3683 fprintf(stderr, "memory allocation failed\n");
3687 rec->devid = key->offset;
3688 rec->generation = btrfs_header_generation(eb);
3690 rec->objectid = key->objectid;
3691 rec->type = key->type;
3692 rec->offset = key->offset;
3694 rec->devid = btrfs_device_id(eb, ptr);
3695 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3696 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3698 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3700 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3707 struct block_group_record *
3708 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3711 struct btrfs_block_group_item *ptr;
3712 struct block_group_record *rec;
3714 rec = malloc(sizeof(*rec));
3716 fprintf(stderr, "memory allocation failed\n");
3719 memset(rec, 0, sizeof(*rec));
3721 rec->cache.start = key->objectid;
3722 rec->cache.size = key->offset;
3724 rec->generation = btrfs_header_generation(leaf);
3726 rec->objectid = key->objectid;
3727 rec->type = key->type;
3728 rec->offset = key->offset;
3730 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3731 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3733 INIT_LIST_HEAD(&rec->list);
3738 static int process_block_group_item(struct block_group_tree *block_group_cache,
3739 struct btrfs_key *key,
3740 struct extent_buffer *eb, int slot)
3742 struct block_group_record *rec;
3745 rec = btrfs_new_block_group_record(eb, key, slot);
3746 ret = insert_block_group_record(block_group_cache, rec);
3748 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3749 rec->objectid, rec->offset);
3756 struct device_extent_record *
3757 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3758 struct btrfs_key *key, int slot)
3760 struct device_extent_record *rec;
3761 struct btrfs_dev_extent *ptr;
3763 rec = malloc(sizeof(*rec));
3765 fprintf(stderr, "memory allocation failed\n");
3768 memset(rec, 0, sizeof(*rec));
3770 rec->cache.objectid = key->objectid;
3771 rec->cache.start = key->offset;
3773 rec->generation = btrfs_header_generation(leaf);
3775 rec->objectid = key->objectid;
3776 rec->type = key->type;
3777 rec->offset = key->offset;
3779 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3780 rec->chunk_objecteid =
3781 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3783 btrfs_dev_extent_chunk_offset(leaf, ptr);
3784 rec->length = btrfs_dev_extent_length(leaf, ptr);
3785 rec->cache.size = rec->length;
3787 INIT_LIST_HEAD(&rec->chunk_list);
3788 INIT_LIST_HEAD(&rec->device_list);
3794 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3795 struct btrfs_key *key, struct extent_buffer *eb,
3798 struct device_extent_record *rec;
3801 rec = btrfs_new_device_extent_record(eb, key, slot);
3802 ret = insert_device_extent_record(dev_extent_cache, rec);
3805 "Device extent[%llu, %llu, %llu] existed.\n",
3806 rec->objectid, rec->offset, rec->length);
3813 static int process_extent_item(struct btrfs_root *root,
3814 struct cache_tree *extent_cache,
3815 struct extent_buffer *eb, int slot)
3817 struct btrfs_extent_item *ei;
3818 struct btrfs_extent_inline_ref *iref;
3819 struct btrfs_extent_data_ref *dref;
3820 struct btrfs_shared_data_ref *sref;
3821 struct btrfs_key key;
3825 u32 item_size = btrfs_item_size_nr(eb, slot);
3831 btrfs_item_key_to_cpu(eb, &key, slot);
3833 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3835 num_bytes = root->leafsize;
3837 num_bytes = key.offset;
3840 if (item_size < sizeof(*ei)) {
3841 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3842 struct btrfs_extent_item_v0 *ei0;
3843 BUG_ON(item_size != sizeof(*ei0));
3844 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3845 refs = btrfs_extent_refs_v0(eb, ei0);
3849 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3850 num_bytes, refs, 0, 0, 0, metadata, 1,
3854 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3855 refs = btrfs_extent_refs(eb, ei);
3857 add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3858 refs, 0, 0, 0, metadata, 1, num_bytes);
3860 ptr = (unsigned long)(ei + 1);
3861 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3862 key.type == BTRFS_EXTENT_ITEM_KEY)
3863 ptr += sizeof(struct btrfs_tree_block_info);
3865 end = (unsigned long)ei + item_size;
3867 iref = (struct btrfs_extent_inline_ref *)ptr;
3868 type = btrfs_extent_inline_ref_type(eb, iref);
3869 offset = btrfs_extent_inline_ref_offset(eb, iref);
3871 case BTRFS_TREE_BLOCK_REF_KEY:
3872 add_tree_backref(extent_cache, key.objectid,
3875 case BTRFS_SHARED_BLOCK_REF_KEY:
3876 add_tree_backref(extent_cache, key.objectid,
3879 case BTRFS_EXTENT_DATA_REF_KEY:
3880 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3881 add_data_backref(extent_cache, key.objectid, 0,
3882 btrfs_extent_data_ref_root(eb, dref),
3883 btrfs_extent_data_ref_objectid(eb,
3885 btrfs_extent_data_ref_offset(eb, dref),
3886 btrfs_extent_data_ref_count(eb, dref),
3889 case BTRFS_SHARED_DATA_REF_KEY:
3890 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3891 add_data_backref(extent_cache, key.objectid, offset,
3893 btrfs_shared_data_ref_count(eb, sref),
3897 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3898 key.objectid, key.type, num_bytes);
3901 ptr += btrfs_extent_inline_ref_size(type);
3908 static int check_cache_range(struct btrfs_root *root,
3909 struct btrfs_block_group_cache *cache,
3910 u64 offset, u64 bytes)
3912 struct btrfs_free_space *entry;
3918 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3919 bytenr = btrfs_sb_offset(i);
3920 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3921 cache->key.objectid, bytenr, 0,
3922 &logical, &nr, &stripe_len);
3927 if (logical[nr] + stripe_len <= offset)
3929 if (offset + bytes <= logical[nr])
3931 if (logical[nr] == offset) {
3932 if (stripe_len >= bytes) {
3936 bytes -= stripe_len;
3937 offset += stripe_len;
3938 } else if (logical[nr] < offset) {
3939 if (logical[nr] + stripe_len >=
3944 bytes = (offset + bytes) -
3945 (logical[nr] + stripe_len);
3946 offset = logical[nr] + stripe_len;
3949 * Could be tricky, the super may land in the
3950 * middle of the area we're checking. First
3951 * check the easiest case, it's at the end.
3953 if (logical[nr] + stripe_len >=
3955 bytes = logical[nr] - offset;
3959 /* Check the left side */
3960 ret = check_cache_range(root, cache,
3962 logical[nr] - offset);
3968 /* Now we continue with the right side */
3969 bytes = (offset + bytes) -
3970 (logical[nr] + stripe_len);
3971 offset = logical[nr] + stripe_len;
3978 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3980 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3981 offset, offset+bytes);
3985 if (entry->offset != offset) {
3986 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3991 if (entry->bytes != bytes) {
3992 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3993 bytes, entry->bytes, offset);
3997 unlink_free_space(cache->free_space_ctl, entry);
4002 static int verify_space_cache(struct btrfs_root *root,
4003 struct btrfs_block_group_cache *cache)
4005 struct btrfs_path *path;
4006 struct extent_buffer *leaf;
4007 struct btrfs_key key;
4011 path = btrfs_alloc_path();
4015 root = root->fs_info->extent_root;
4017 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
4019 key.objectid = last;
4021 key.type = BTRFS_EXTENT_ITEM_KEY;
4023 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4028 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4029 ret = btrfs_next_leaf(root, path);
4037 leaf = path->nodes[0];
4038 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4039 if (key.objectid >= cache->key.offset + cache->key.objectid)
4041 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
4042 key.type != BTRFS_METADATA_ITEM_KEY) {
4047 if (last == key.objectid) {
4048 if (key.type == BTRFS_EXTENT_ITEM_KEY)
4049 last = key.objectid + key.offset;
4051 last = key.objectid + root->leafsize;
4056 ret = check_cache_range(root, cache, last,
4057 key.objectid - last);
4060 if (key.type == BTRFS_EXTENT_ITEM_KEY)
4061 last = key.objectid + key.offset;
4063 last = key.objectid + root->leafsize;
4067 if (last < cache->key.objectid + cache->key.offset)
4068 ret = check_cache_range(root, cache, last,
4069 cache->key.objectid +
4070 cache->key.offset - last);
4073 btrfs_free_path(path);
4076 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
4077 fprintf(stderr, "There are still entries left in the space "
4085 static int check_space_cache(struct btrfs_root *root)
4087 struct btrfs_block_group_cache *cache;
4088 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
4092 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
4093 btrfs_super_generation(root->fs_info->super_copy) !=
4094 btrfs_super_cache_generation(root->fs_info->super_copy)) {
4095 printf("cache and super generation don't match, space cache "
4096 "will be invalidated\n");
4101 cache = btrfs_lookup_first_block_group(root->fs_info, start);
4105 start = cache->key.objectid + cache->key.offset;
4106 if (!cache->free_space_ctl) {
4107 if (btrfs_init_free_space_ctl(cache,
4108 root->sectorsize)) {
4113 btrfs_remove_free_space_cache(cache);
4116 ret = load_free_space_cache(root->fs_info, cache);
4120 ret = verify_space_cache(root, cache);
4122 fprintf(stderr, "cache appears valid but isnt %Lu\n",
4123 cache->key.objectid);
4128 return error ? -EINVAL : 0;
4131 static int read_extent_data(struct btrfs_root *root, char *data,
4132 u64 logical, u64 *len, int mirror)
4135 struct btrfs_multi_bio *multi = NULL;
4136 struct btrfs_fs_info *info = root->fs_info;
4137 struct btrfs_device *device;
4141 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
4142 &multi, mirror, NULL);
4144 fprintf(stderr, "Couldn't map the block %llu\n",
4148 device = multi->stripes[0].dev;
4150 if (device->fd == 0)
4155 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
4165 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
4166 u64 num_bytes, unsigned long leaf_offset,
4167 struct extent_buffer *eb) {
4170 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4172 unsigned long csum_offset;
4176 u64 data_checked = 0;
4182 if (num_bytes % root->sectorsize)
4185 data = malloc(num_bytes);
4189 while (offset < num_bytes) {
4192 read_len = num_bytes - offset;
4193 /* read as much space once a time */
4194 ret = read_extent_data(root, data + offset,
4195 bytenr + offset, &read_len, mirror);
4199 /* verify every 4k data's checksum */
4200 while (data_checked < read_len) {
4202 tmp = offset + data_checked;
4204 csum = btrfs_csum_data(NULL, (char *)data + tmp,
4205 csum, root->sectorsize);
4206 btrfs_csum_final(csum, (char *)&csum);
4208 csum_offset = leaf_offset +
4209 tmp / root->sectorsize * csum_size;
4210 read_extent_buffer(eb, (char *)&csum_expected,
4211 csum_offset, csum_size);
4212 /* try another mirror */
4213 if (csum != csum_expected) {
4214 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
4215 mirror, bytenr + tmp,
4216 csum, csum_expected);
4217 num_copies = btrfs_num_copies(
4218 &root->fs_info->mapping_tree,
4220 if (mirror < num_copies - 1) {
4225 data_checked += root->sectorsize;
4234 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
4237 struct btrfs_path *path;
4238 struct extent_buffer *leaf;
4239 struct btrfs_key key;
4242 path = btrfs_alloc_path();
4244 fprintf(stderr, "Error allocing path\n");
4248 key.objectid = bytenr;
4249 key.type = BTRFS_EXTENT_ITEM_KEY;
4250 key.offset = (u64)-1;
4253 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
4256 fprintf(stderr, "Error looking up extent record %d\n", ret);
4257 btrfs_free_path(path);
4260 if (path->slots[0] > 0) {
4263 ret = btrfs_prev_leaf(root, path);
4266 } else if (ret > 0) {
4273 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4276 * Block group items come before extent items if they have the same
4277 * bytenr, so walk back one more just in case. Dear future traveler,
4278 * first congrats on mastering time travel. Now if it's not too much
4279 * trouble could you go back to 2006 and tell Chris to make the
4280 * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
4281 * EXTENT_ITEM_KEY please?
4283 while (key.type > BTRFS_EXTENT_ITEM_KEY) {
4284 if (path->slots[0] > 0) {
4287 ret = btrfs_prev_leaf(root, path);
4290 } else if (ret > 0) {
4295 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4299 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4300 ret = btrfs_next_leaf(root, path);
4302 fprintf(stderr, "Error going to next leaf "
4304 btrfs_free_path(path);
4310 leaf = path->nodes[0];
4311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4312 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
4316 if (key.objectid + key.offset < bytenr) {
4320 if (key.objectid > bytenr + num_bytes)
4323 if (key.objectid == bytenr) {
4324 if (key.offset >= num_bytes) {
4328 num_bytes -= key.offset;
4329 bytenr += key.offset;
4330 } else if (key.objectid < bytenr) {
4331 if (key.objectid + key.offset >= bytenr + num_bytes) {
4335 num_bytes = (bytenr + num_bytes) -
4336 (key.objectid + key.offset);
4337 bytenr = key.objectid + key.offset;
4339 if (key.objectid + key.offset < bytenr + num_bytes) {
4340 u64 new_start = key.objectid + key.offset;
4341 u64 new_bytes = bytenr + num_bytes - new_start;
4344 * Weird case, the extent is in the middle of
4345 * our range, we'll have to search one side
4346 * and then the other. Not sure if this happens
4347 * in real life, but no harm in coding it up
4348 * anyway just in case.
4350 btrfs_release_path(path);
4351 ret = check_extent_exists(root, new_start,
4354 fprintf(stderr, "Right section didn't "
4358 num_bytes = key.objectid - bytenr;
4361 num_bytes = key.objectid - bytenr;
4368 if (num_bytes && !ret) {
4369 fprintf(stderr, "There are no extents for csum range "
4370 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
4374 btrfs_free_path(path);
4378 static int check_csums(struct btrfs_root *root)
4380 struct btrfs_path *path;
4381 struct extent_buffer *leaf;
4382 struct btrfs_key key;
4383 u64 offset = 0, num_bytes = 0;
4384 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
4388 unsigned long leaf_offset;
4390 root = root->fs_info->csum_root;
4391 if (!extent_buffer_uptodate(root->node)) {
4392 fprintf(stderr, "No valid csum tree found\n");
4396 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
4397 key.type = BTRFS_EXTENT_CSUM_KEY;
4400 path = btrfs_alloc_path();
4404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4406 fprintf(stderr, "Error searching csum tree %d\n", ret);
4407 btrfs_free_path(path);
4411 if (ret > 0 && path->slots[0])
4416 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4417 ret = btrfs_next_leaf(root, path);
4419 fprintf(stderr, "Error going to next leaf "
4426 leaf = path->nodes[0];
4428 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4429 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
4434 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
4435 csum_size) * root->sectorsize;
4436 if (!check_data_csum)
4437 goto skip_csum_check;
4438 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
4439 ret = check_extent_csums(root, key.offset, data_len,
4445 offset = key.offset;
4446 } else if (key.offset != offset + num_bytes) {
4447 ret = check_extent_exists(root, offset, num_bytes);
4449 fprintf(stderr, "Csum exists for %Lu-%Lu but "
4450 "there is no extent record\n",
4451 offset, offset+num_bytes);
4454 offset = key.offset;
4457 num_bytes += data_len;
4461 btrfs_free_path(path);
4465 static int is_dropped_key(struct btrfs_key *key,
4466 struct btrfs_key *drop_key) {
4467 if (key->objectid < drop_key->objectid)
4469 else if (key->objectid == drop_key->objectid) {
4470 if (key->type < drop_key->type)
4472 else if (key->type == drop_key->type) {
4473 if (key->offset < drop_key->offset)
4480 static int run_next_block(struct btrfs_trans_handle *trans,
4481 struct btrfs_root *root,
4482 struct block_info *bits,
4485 struct cache_tree *pending,
4486 struct cache_tree *seen,
4487 struct cache_tree *reada,
4488 struct cache_tree *nodes,
4489 struct cache_tree *extent_cache,
4490 struct cache_tree *chunk_cache,
4491 struct rb_root *dev_cache,
4492 struct block_group_tree *block_group_cache,
4493 struct device_extent_tree *dev_extent_cache,
4494 struct btrfs_root_item *ri)
4496 struct extent_buffer *buf;
4507 struct btrfs_key key;
4508 struct cache_extent *cache;
4511 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
4512 bits_nr, &reada_bits);
4517 for(i = 0; i < nritems; i++) {
4518 ret = add_cache_extent(reada, bits[i].start,
4523 /* fixme, get the parent transid */
4524 readahead_tree_block(root, bits[i].start,
4528 *last = bits[0].start;
4529 bytenr = bits[0].start;
4530 size = bits[0].size;
4532 cache = lookup_cache_extent(pending, bytenr, size);
4534 remove_cache_extent(pending, cache);
4537 cache = lookup_cache_extent(reada, bytenr, size);
4539 remove_cache_extent(reada, cache);
4542 cache = lookup_cache_extent(nodes, bytenr, size);
4544 remove_cache_extent(nodes, cache);
4547 cache = lookup_cache_extent(extent_cache, bytenr, size);
4549 struct extent_record *rec;
4551 rec = container_of(cache, struct extent_record, cache);
4552 gen = rec->parent_generation;
4555 /* fixme, get the real parent transid */
4556 buf = read_tree_block(root, bytenr, size, gen);
4557 if (!extent_buffer_uptodate(buf)) {
4558 record_bad_block_io(root->fs_info,
4559 extent_cache, bytenr, size);
4563 nritems = btrfs_header_nritems(buf);
4566 * FIXME, this only works only if we don't have any full
4569 if (!init_extent_tree) {
4570 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
4571 btrfs_header_level(buf), 1, NULL,
4579 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4584 owner = btrfs_header_owner(buf);
4587 ret = check_block(trans, root, extent_cache, buf, flags);
4591 if (btrfs_is_leaf(buf)) {
4592 btree_space_waste += btrfs_leaf_free_space(root, buf);
4593 for (i = 0; i < nritems; i++) {
4594 struct btrfs_file_extent_item *fi;
4595 btrfs_item_key_to_cpu(buf, &key, i);
4596 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
4597 process_extent_item(root, extent_cache, buf,
4601 if (key.type == BTRFS_METADATA_ITEM_KEY) {
4602 process_extent_item(root, extent_cache, buf,
4606 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
4608 btrfs_item_size_nr(buf, i);
4611 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4612 process_chunk_item(chunk_cache, &key, buf, i);
4615 if (key.type == BTRFS_DEV_ITEM_KEY) {
4616 process_device_item(dev_cache, &key, buf, i);
4619 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4620 process_block_group_item(block_group_cache,
4624 if (key.type == BTRFS_DEV_EXTENT_KEY) {
4625 process_device_extent_item(dev_extent_cache,
4630 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
4631 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4632 process_extent_ref_v0(extent_cache, buf, i);
4639 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
4640 add_tree_backref(extent_cache, key.objectid, 0,
4644 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
4645 add_tree_backref(extent_cache, key.objectid,
4649 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
4650 struct btrfs_extent_data_ref *ref;
4651 ref = btrfs_item_ptr(buf, i,
4652 struct btrfs_extent_data_ref);
4653 add_data_backref(extent_cache,
4655 btrfs_extent_data_ref_root(buf, ref),
4656 btrfs_extent_data_ref_objectid(buf,
4658 btrfs_extent_data_ref_offset(buf, ref),
4659 btrfs_extent_data_ref_count(buf, ref),
4660 0, root->sectorsize);
4663 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4664 struct btrfs_shared_data_ref *ref;
4665 ref = btrfs_item_ptr(buf, i,
4666 struct btrfs_shared_data_ref);
4667 add_data_backref(extent_cache,
4668 key.objectid, key.offset, 0, 0, 0,
4669 btrfs_shared_data_ref_count(buf, ref),
4670 0, root->sectorsize);
4673 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4674 struct bad_item *bad;
4676 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4680 bad = malloc(sizeof(struct bad_item));
4683 INIT_LIST_HEAD(&bad->list);
4684 memcpy(&bad->key, &key,
4685 sizeof(struct btrfs_key));
4686 bad->root_id = owner;
4687 list_add_tail(&bad->list, &delete_items);
4690 if (key.type != BTRFS_EXTENT_DATA_KEY)
4692 fi = btrfs_item_ptr(buf, i,
4693 struct btrfs_file_extent_item);
4694 if (btrfs_file_extent_type(buf, fi) ==
4695 BTRFS_FILE_EXTENT_INLINE)
4697 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4700 data_bytes_allocated +=
4701 btrfs_file_extent_disk_num_bytes(buf, fi);
4702 if (data_bytes_allocated < root->sectorsize) {
4705 data_bytes_referenced +=
4706 btrfs_file_extent_num_bytes(buf, fi);
4707 add_data_backref(extent_cache,
4708 btrfs_file_extent_disk_bytenr(buf, fi),
4709 parent, owner, key.objectid, key.offset -
4710 btrfs_file_extent_offset(buf, fi), 1, 1,
4711 btrfs_file_extent_disk_num_bytes(buf, fi));
4715 struct btrfs_key first_key;
4717 first_key.objectid = 0;
4720 btrfs_item_key_to_cpu(buf, &first_key, 0);
4721 level = btrfs_header_level(buf);
4722 for (i = 0; i < nritems; i++) {
4723 ptr = btrfs_node_blockptr(buf, i);
4724 size = btrfs_level_size(root, level - 1);
4725 btrfs_node_key_to_cpu(buf, &key, i);
4727 struct btrfs_key drop_key;
4728 btrfs_disk_key_to_cpu(&drop_key,
4729 &ri->drop_progress);
4730 if ((level == ri->drop_level)
4731 && is_dropped_key(&key, &drop_key)) {
4735 ret = add_extent_rec(extent_cache, &key,
4736 btrfs_node_ptr_generation(buf, i),
4737 ptr, size, 0, 0, 1, 0, 1, 0,
4741 add_tree_backref(extent_cache, ptr, parent, owner, 1);
4744 add_pending(nodes, seen, ptr, size);
4746 add_pending(pending, seen, ptr, size);
4749 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4750 nritems) * sizeof(struct btrfs_key_ptr);
4752 total_btree_bytes += buf->len;
4753 if (fs_root_objectid(btrfs_header_owner(buf)))
4754 total_fs_tree_bytes += buf->len;
4755 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4756 total_extent_tree_bytes += buf->len;
4757 if (!found_old_backref &&
4758 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4759 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4760 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4761 found_old_backref = 1;
4763 free_extent_buffer(buf);
4767 static int add_root_to_pending(struct extent_buffer *buf,
4768 struct cache_tree *extent_cache,
4769 struct cache_tree *pending,
4770 struct cache_tree *seen,
4771 struct cache_tree *nodes,
4772 struct btrfs_key *root_key)
4774 if (btrfs_header_level(buf) > 0)
4775 add_pending(nodes, seen, buf->start, buf->len);
4777 add_pending(pending, seen, buf->start, buf->len);
4778 add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4779 0, 1, 1, 0, 1, 0, buf->len);
4781 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4782 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4783 add_tree_backref(extent_cache, buf->start, buf->start,
4786 add_tree_backref(extent_cache, buf->start, 0,
4787 root_key->objectid, 1);
4791 /* as we fix the tree, we might be deleting blocks that
4792 * we're tracking for repair. This hook makes sure we
4793 * remove any backrefs for blocks as we are fixing them.
4795 static int free_extent_hook(struct btrfs_trans_handle *trans,
4796 struct btrfs_root *root,
4797 u64 bytenr, u64 num_bytes, u64 parent,
4798 u64 root_objectid, u64 owner, u64 offset,
4801 struct extent_record *rec;
4802 struct cache_extent *cache;
4804 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4806 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4807 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4811 rec = container_of(cache, struct extent_record, cache);
4813 struct data_backref *back;
4814 back = find_data_backref(rec, parent, root_objectid, owner,
4815 offset, 1, bytenr, num_bytes);
4818 if (back->node.found_ref) {
4819 back->found_ref -= refs_to_drop;
4821 rec->refs -= refs_to_drop;
4823 if (back->node.found_extent_tree) {
4824 back->num_refs -= refs_to_drop;
4825 if (rec->extent_item_refs)
4826 rec->extent_item_refs -= refs_to_drop;
4828 if (back->found_ref == 0)
4829 back->node.found_ref = 0;
4830 if (back->num_refs == 0)
4831 back->node.found_extent_tree = 0;
4833 if (!back->node.found_extent_tree && back->node.found_ref) {
4834 list_del(&back->node.list);
4838 struct tree_backref *back;
4839 back = find_tree_backref(rec, parent, root_objectid);
4842 if (back->node.found_ref) {
4845 back->node.found_ref = 0;
4847 if (back->node.found_extent_tree) {
4848 if (rec->extent_item_refs)
4849 rec->extent_item_refs--;
4850 back->node.found_extent_tree = 0;
4852 if (!back->node.found_extent_tree && back->node.found_ref) {
4853 list_del(&back->node.list);
4857 maybe_free_extent_rec(extent_cache, rec);
4862 static int delete_extent_records(struct btrfs_trans_handle *trans,
4863 struct btrfs_root *root,
4864 struct btrfs_path *path,
4865 u64 bytenr, u64 new_len)
4867 struct btrfs_key key;
4868 struct btrfs_key found_key;
4869 struct extent_buffer *leaf;
4874 key.objectid = bytenr;
4876 key.offset = (u64)-1;
4879 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
4886 if (path->slots[0] == 0)
4892 leaf = path->nodes[0];
4893 slot = path->slots[0];
4895 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4896 if (found_key.objectid != bytenr)
4899 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
4900 found_key.type != BTRFS_METADATA_ITEM_KEY &&
4901 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
4902 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
4903 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
4904 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
4905 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
4906 btrfs_release_path(path);
4907 if (found_key.type == 0) {
4908 if (found_key.offset == 0)
4910 key.offset = found_key.offset - 1;
4911 key.type = found_key.type;
4913 key.type = found_key.type - 1;
4914 key.offset = (u64)-1;
4918 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4919 found_key.objectid, found_key.type, found_key.offset);
4921 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4924 btrfs_release_path(path);
4926 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4927 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4928 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4929 found_key.offset : root->leafsize;
4931 ret = btrfs_update_block_group(trans, root, bytenr,
4938 btrfs_release_path(path);
4943 * for a single backref, this will allocate a new extent
4944 * and add the backref to it.
4946 static int record_extent(struct btrfs_trans_handle *trans,
4947 struct btrfs_fs_info *info,
4948 struct btrfs_path *path,
4949 struct extent_record *rec,
4950 struct extent_backref *back,
4951 int allocated, u64 flags)
4954 struct btrfs_root *extent_root = info->extent_root;
4955 struct extent_buffer *leaf;
4956 struct btrfs_key ins_key;
4957 struct btrfs_extent_item *ei;
4958 struct tree_backref *tback;
4959 struct data_backref *dback;
4960 struct btrfs_tree_block_info *bi;
4963 rec->max_size = max_t(u64, rec->max_size,
4964 info->extent_root->leafsize);
4967 u32 item_size = sizeof(*ei);
4970 item_size += sizeof(*bi);
4972 ins_key.objectid = rec->start;
4973 ins_key.offset = rec->max_size;
4974 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4976 ret = btrfs_insert_empty_item(trans, extent_root, path,
4977 &ins_key, item_size);
4981 leaf = path->nodes[0];
4982 ei = btrfs_item_ptr(leaf, path->slots[0],
4983 struct btrfs_extent_item);
4985 btrfs_set_extent_refs(leaf, ei, 0);
4986 btrfs_set_extent_generation(leaf, ei, rec->generation);
4988 if (back->is_data) {
4989 btrfs_set_extent_flags(leaf, ei,
4990 BTRFS_EXTENT_FLAG_DATA);
4992 struct btrfs_disk_key copy_key;;
4994 tback = (struct tree_backref *)back;
4995 bi = (struct btrfs_tree_block_info *)(ei + 1);
4996 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4999 btrfs_set_disk_key_objectid(©_key,
5000 rec->info_objectid);
5001 btrfs_set_disk_key_type(©_key, 0);
5002 btrfs_set_disk_key_offset(©_key, 0);
5004 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
5005 btrfs_set_tree_block_key(leaf, bi, ©_key);
5007 btrfs_set_extent_flags(leaf, ei,
5008 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
5011 btrfs_mark_buffer_dirty(leaf);
5012 ret = btrfs_update_block_group(trans, extent_root, rec->start,
5013 rec->max_size, 1, 0);
5016 btrfs_release_path(path);
5019 if (back->is_data) {
5023 dback = (struct data_backref *)back;
5024 if (back->full_backref)
5025 parent = dback->parent;
5029 for (i = 0; i < dback->found_ref; i++) {
5030 /* if parent != 0, we're doing a full backref
5031 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
5032 * just makes the backref allocator create a data
5035 ret = btrfs_inc_extent_ref(trans, info->extent_root,
5036 rec->start, rec->max_size,
5040 BTRFS_FIRST_FREE_OBJECTID :
5046 fprintf(stderr, "adding new data backref"
5047 " on %llu %s %llu owner %llu"
5048 " offset %llu found %d\n",
5049 (unsigned long long)rec->start,
5050 back->full_backref ?
5052 back->full_backref ?
5053 (unsigned long long)parent :
5054 (unsigned long long)dback->root,
5055 (unsigned long long)dback->owner,
5056 (unsigned long long)dback->offset,
5061 tback = (struct tree_backref *)back;
5062 if (back->full_backref)
5063 parent = tback->parent;
5067 ret = btrfs_inc_extent_ref(trans, info->extent_root,
5068 rec->start, rec->max_size,
5069 parent, tback->root, 0, 0);
5070 fprintf(stderr, "adding new tree backref on "
5071 "start %llu len %llu parent %llu root %llu\n",
5072 rec->start, rec->max_size, tback->parent, tback->root);
5077 btrfs_release_path(path);
5081 struct extent_entry {
5086 struct list_head list;
5089 static struct extent_entry *find_entry(struct list_head *entries,
5090 u64 bytenr, u64 bytes)
5092 struct extent_entry *entry = NULL;
5094 list_for_each_entry(entry, entries, list) {
5095 if (entry->bytenr == bytenr && entry->bytes == bytes)
5102 static struct extent_entry *find_most_right_entry(struct list_head *entries)
5104 struct extent_entry *entry, *best = NULL, *prev = NULL;
5106 list_for_each_entry(entry, entries, list) {
5113 * If there are as many broken entries as entries then we know
5114 * not to trust this particular entry.
5116 if (entry->broken == entry->count)
5120 * If our current entry == best then we can't be sure our best
5121 * is really the best, so we need to keep searching.
5123 if (best && best->count == entry->count) {
5129 /* Prev == entry, not good enough, have to keep searching */
5130 if (!prev->broken && prev->count == entry->count)
5134 best = (prev->count > entry->count) ? prev : entry;
5135 else if (best->count < entry->count)
5143 static int repair_ref(struct btrfs_trans_handle *trans,
5144 struct btrfs_fs_info *info, struct btrfs_path *path,
5145 struct data_backref *dback, struct extent_entry *entry)
5147 struct btrfs_root *root;
5148 struct btrfs_file_extent_item *fi;
5149 struct extent_buffer *leaf;
5150 struct btrfs_key key;
5154 key.objectid = dback->root;
5155 key.type = BTRFS_ROOT_ITEM_KEY;
5156 key.offset = (u64)-1;
5157 root = btrfs_read_fs_root(info, &key);
5159 fprintf(stderr, "Couldn't find root for our ref\n");
5164 * The backref points to the original offset of the extent if it was
5165 * split, so we need to search down to the offset we have and then walk
5166 * forward until we find the backref we're looking for.
5168 key.objectid = dback->owner;
5169 key.type = BTRFS_EXTENT_DATA_KEY;
5170 key.offset = dback->offset;
5171 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5173 fprintf(stderr, "Error looking up ref %d\n", ret);
5178 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
5179 ret = btrfs_next_leaf(root, path);
5181 fprintf(stderr, "Couldn't find our ref, next\n");
5185 leaf = path->nodes[0];
5186 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5187 if (key.objectid != dback->owner ||
5188 key.type != BTRFS_EXTENT_DATA_KEY) {
5189 fprintf(stderr, "Couldn't find our ref, search\n");
5192 fi = btrfs_item_ptr(leaf, path->slots[0],
5193 struct btrfs_file_extent_item);
5194 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5195 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5197 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
5202 btrfs_release_path(path);
5205 * Have to make sure that this root gets updated when we commit the
5208 record_root_in_trans(trans, root);
5211 * Ok we have the key of the file extent we want to fix, now we can cow
5212 * down to the thing and fix it.
5214 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5216 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
5217 key.objectid, key.type, key.offset, ret);
5221 fprintf(stderr, "Well that's odd, we just found this key "
5222 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
5226 leaf = path->nodes[0];
5227 fi = btrfs_item_ptr(leaf, path->slots[0],
5228 struct btrfs_file_extent_item);
5230 if (btrfs_file_extent_compression(leaf, fi) &&
5231 dback->disk_bytenr != entry->bytenr) {
5232 fprintf(stderr, "Ref doesn't match the record start and is "
5233 "compressed, please take a btrfs-image of this file "
5234 "system and send it to a btrfs developer so they can "
5235 "complete this functionality for bytenr %Lu\n",
5236 dback->disk_bytenr);
5240 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
5241 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5242 } else if (dback->disk_bytenr > entry->bytenr) {
5243 u64 off_diff, offset;
5245 off_diff = dback->disk_bytenr - entry->bytenr;
5246 offset = btrfs_file_extent_offset(leaf, fi);
5247 if (dback->disk_bytenr + offset +
5248 btrfs_file_extent_num_bytes(leaf, fi) >
5249 entry->bytenr + entry->bytes) {
5250 fprintf(stderr, "Ref is past the entry end, please "
5251 "take a btrfs-image of this file system and "
5252 "send it to a btrfs developer, ref %Lu\n",
5253 dback->disk_bytenr);
5257 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5258 btrfs_set_file_extent_offset(leaf, fi, offset);
5259 } else if (dback->disk_bytenr < entry->bytenr) {
5262 offset = btrfs_file_extent_offset(leaf, fi);
5263 if (dback->disk_bytenr + offset < entry->bytenr) {
5264 fprintf(stderr, "Ref is before the entry start, please"
5265 " take a btrfs-image of this file system and "
5266 "send it to a btrfs developer, ref %Lu\n",
5267 dback->disk_bytenr);
5271 offset += dback->disk_bytenr;
5272 offset -= entry->bytenr;
5273 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
5274 btrfs_set_file_extent_offset(leaf, fi, offset);
5277 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
5280 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
5281 * only do this if we aren't using compression, otherwise it's a
5284 if (!btrfs_file_extent_compression(leaf, fi))
5285 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
5287 printf("ram bytes may be wrong?\n");
5288 btrfs_mark_buffer_dirty(leaf);
5289 btrfs_release_path(path);
5293 static int verify_backrefs(struct btrfs_trans_handle *trans,
5294 struct btrfs_fs_info *info, struct btrfs_path *path,
5295 struct extent_record *rec)
5297 struct extent_backref *back;
5298 struct data_backref *dback;
5299 struct extent_entry *entry, *best = NULL;
5302 int broken_entries = 0;
5307 * Metadata is easy and the backrefs should always agree on bytenr and
5308 * size, if not we've got bigger issues.
5313 list_for_each_entry(back, &rec->backrefs, list) {
5314 if (back->full_backref || !back->is_data)
5317 dback = (struct data_backref *)back;
5320 * We only pay attention to backrefs that we found a real
5323 if (dback->found_ref == 0)
5327 * For now we only catch when the bytes don't match, not the
5328 * bytenr. We can easily do this at the same time, but I want
5329 * to have a fs image to test on before we just add repair
5330 * functionality willy-nilly so we know we won't screw up the
5334 entry = find_entry(&entries, dback->disk_bytenr,
5337 entry = malloc(sizeof(struct extent_entry));
5342 memset(entry, 0, sizeof(*entry));
5343 entry->bytenr = dback->disk_bytenr;
5344 entry->bytes = dback->bytes;
5345 list_add_tail(&entry->list, &entries);
5350 * If we only have on entry we may think the entries agree when
5351 * in reality they don't so we have to do some extra checking.
5353 if (dback->disk_bytenr != rec->start ||
5354 dback->bytes != rec->nr || back->broken)
5365 /* Yay all the backrefs agree, carry on good sir */
5366 if (nr_entries <= 1 && !mismatch)
5369 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
5370 "%Lu\n", rec->start);
5373 * First we want to see if the backrefs can agree amongst themselves who
5374 * is right, so figure out which one of the entries has the highest
5377 best = find_most_right_entry(&entries);
5380 * Ok so we may have an even split between what the backrefs think, so
5381 * this is where we use the extent ref to see what it thinks.
5384 entry = find_entry(&entries, rec->start, rec->nr);
5385 if (!entry && (!broken_entries || !rec->found_rec)) {
5386 fprintf(stderr, "Backrefs don't agree with each other "
5387 "and extent record doesn't agree with anybody,"
5388 " so we can't fix bytenr %Lu bytes %Lu\n",
5389 rec->start, rec->nr);
5392 } else if (!entry) {
5394 * Ok our backrefs were broken, we'll assume this is the
5395 * correct value and add an entry for this range.
5397 entry = malloc(sizeof(struct extent_entry));
5402 memset(entry, 0, sizeof(*entry));
5403 entry->bytenr = rec->start;
5404 entry->bytes = rec->nr;
5405 list_add_tail(&entry->list, &entries);
5409 best = find_most_right_entry(&entries);
5411 fprintf(stderr, "Backrefs and extent record evenly "
5412 "split on who is right, this is going to "
5413 "require user input to fix bytenr %Lu bytes "
5414 "%Lu\n", rec->start, rec->nr);
5421 * I don't think this can happen currently as we'll abort() if we catch
5422 * this case higher up, but in case somebody removes that we still can't
5423 * deal with it properly here yet, so just bail out of that's the case.
5425 if (best->bytenr != rec->start) {
5426 fprintf(stderr, "Extent start and backref starts don't match, "
5427 "please use btrfs-image on this file system and send "
5428 "it to a btrfs developer so they can make fsck fix "
5429 "this particular case. bytenr is %Lu, bytes is %Lu\n",
5430 rec->start, rec->nr);
5436 * Ok great we all agreed on an extent record, let's go find the real
5437 * references and fix up the ones that don't match.
5439 list_for_each_entry(back, &rec->backrefs, list) {
5440 if (back->full_backref || !back->is_data)
5443 dback = (struct data_backref *)back;
5446 * Still ignoring backrefs that don't have a real ref attached
5449 if (dback->found_ref == 0)
5452 if (dback->bytes == best->bytes &&
5453 dback->disk_bytenr == best->bytenr)
5456 ret = repair_ref(trans, info, path, dback, best);
5462 * Ok we messed with the actual refs, which means we need to drop our
5463 * entire cache and go back and rescan. I know this is a huge pain and
5464 * adds a lot of extra work, but it's the only way to be safe. Once all
5465 * the backrefs agree we may not need to do anything to the extent
5470 while (!list_empty(&entries)) {
5471 entry = list_entry(entries.next, struct extent_entry, list);
5472 list_del_init(&entry->list);
5478 static int process_duplicates(struct btrfs_root *root,
5479 struct cache_tree *extent_cache,
5480 struct extent_record *rec)
5482 struct extent_record *good, *tmp;
5483 struct cache_extent *cache;
5487 * If we found a extent record for this extent then return, or if we
5488 * have more than one duplicate we are likely going to need to delete
5491 if (rec->found_rec || rec->num_duplicates > 1)
5494 /* Shouldn't happen but just in case */
5495 BUG_ON(!rec->num_duplicates);
5498 * So this happens if we end up with a backref that doesn't match the
5499 * actual extent entry. So either the backref is bad or the extent
5500 * entry is bad. Either way we want to have the extent_record actually
5501 * reflect what we found in the extent_tree, so we need to take the
5502 * duplicate out and use that as the extent_record since the only way we
5503 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
5505 remove_cache_extent(extent_cache, &rec->cache);
5507 good = list_entry(rec->dups.next, struct extent_record, list);
5508 list_del_init(&good->list);
5509 INIT_LIST_HEAD(&good->backrefs);
5510 INIT_LIST_HEAD(&good->dups);
5511 good->cache.start = good->start;
5512 good->cache.size = good->nr;
5513 good->content_checked = 0;
5514 good->owner_ref_checked = 0;
5515 good->num_duplicates = 0;
5516 good->refs = rec->refs;
5517 list_splice_init(&rec->backrefs, &good->backrefs);
5519 cache = lookup_cache_extent(extent_cache, good->start,
5523 tmp = container_of(cache, struct extent_record, cache);
5526 * If we find another overlapping extent and it's found_rec is
5527 * set then it's a duplicate and we need to try and delete
5530 if (tmp->found_rec || tmp->num_duplicates > 0) {
5531 if (list_empty(&good->list))
5532 list_add_tail(&good->list,
5533 &duplicate_extents);
5534 good->num_duplicates += tmp->num_duplicates + 1;
5535 list_splice_init(&tmp->dups, &good->dups);
5536 list_del_init(&tmp->list);
5537 list_add_tail(&tmp->list, &good->dups);
5538 remove_cache_extent(extent_cache, &tmp->cache);
5543 * Ok we have another non extent item backed extent rec, so lets
5544 * just add it to this extent and carry on like we did above.
5546 good->refs += tmp->refs;
5547 list_splice_init(&tmp->backrefs, &good->backrefs);
5548 remove_cache_extent(extent_cache, &tmp->cache);
5551 ret = insert_cache_extent(extent_cache, &good->cache);
5554 return good->num_duplicates ? 0 : 1;
5557 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
5558 struct btrfs_root *root,
5559 struct extent_record *rec)
5561 LIST_HEAD(delete_list);
5562 struct btrfs_path *path;
5563 struct extent_record *tmp, *good, *n;
5566 struct btrfs_key key;
5568 path = btrfs_alloc_path();
5575 /* Find the record that covers all of the duplicates. */
5576 list_for_each_entry(tmp, &rec->dups, list) {
5577 if (good->start < tmp->start)
5579 if (good->nr > tmp->nr)
5582 if (tmp->start + tmp->nr < good->start + good->nr) {
5583 fprintf(stderr, "Ok we have overlapping extents that "
5584 "aren't completely covered by eachother, this "
5585 "is going to require more careful thought. "
5586 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
5587 tmp->start, tmp->nr, good->start, good->nr);
5594 list_add_tail(&rec->list, &delete_list);
5596 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
5599 list_move_tail(&tmp->list, &delete_list);
5602 root = root->fs_info->extent_root;
5603 list_for_each_entry(tmp, &delete_list, list) {
5604 if (tmp->found_rec == 0)
5606 key.objectid = tmp->start;
5607 key.type = BTRFS_EXTENT_ITEM_KEY;
5608 key.offset = tmp->nr;
5610 /* Shouldn't happen but just in case */
5611 if (tmp->metadata) {
5612 fprintf(stderr, "Well this shouldn't happen, extent "
5613 "record overlaps but is metadata? "
5614 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
5618 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5624 ret = btrfs_del_item(trans, root, path);
5627 btrfs_release_path(path);
5632 while (!list_empty(&delete_list)) {
5633 tmp = list_entry(delete_list.next, struct extent_record, list);
5634 list_del_init(&tmp->list);
5640 while (!list_empty(&rec->dups)) {
5641 tmp = list_entry(rec->dups.next, struct extent_record, list);
5642 list_del_init(&tmp->list);
5646 btrfs_free_path(path);
5648 if (!ret && !nr_del)
5649 rec->num_duplicates = 0;
5651 return ret ? ret : nr_del;
5654 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5655 struct btrfs_fs_info *info,
5656 struct btrfs_path *path,
5657 struct cache_tree *extent_cache,
5658 struct extent_record *rec)
5660 struct btrfs_root *root;
5661 struct extent_backref *back;
5662 struct data_backref *dback;
5663 struct cache_extent *cache;
5664 struct btrfs_file_extent_item *fi;
5665 struct btrfs_key key;
5669 list_for_each_entry(back, &rec->backrefs, list) {
5670 /* Don't care about full backrefs (poor unloved backrefs) */
5671 if (back->full_backref || !back->is_data)
5674 dback = (struct data_backref *)back;
5676 /* We found this one, we don't need to do a lookup */
5677 if (dback->found_ref)
5680 key.objectid = dback->root;
5681 key.type = BTRFS_ROOT_ITEM_KEY;
5682 key.offset = (u64)-1;
5684 root = btrfs_read_fs_root(info, &key);
5686 /* No root, definitely a bad ref, skip */
5687 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5689 /* Other err, exit */
5691 return PTR_ERR(root);
5693 key.objectid = dback->owner;
5694 key.type = BTRFS_EXTENT_DATA_KEY;
5695 key.offset = dback->offset;
5696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5698 btrfs_release_path(path);
5701 /* Didn't find it, we can carry on */
5706 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5707 struct btrfs_file_extent_item);
5708 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5709 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5710 btrfs_release_path(path);
5711 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5713 struct extent_record *tmp;
5714 tmp = container_of(cache, struct extent_record, cache);
5717 * If we found an extent record for the bytenr for this
5718 * particular backref then we can't add it to our
5719 * current extent record. We only want to add backrefs
5720 * that don't have a corresponding extent item in the
5721 * extent tree since they likely belong to this record
5722 * and we need to fix it if it doesn't match bytenrs.
5728 dback->found_ref += 1;
5729 dback->disk_bytenr = bytenr;
5730 dback->bytes = bytes;
5733 * Set this so the verify backref code knows not to trust the
5734 * values in this backref.
5743 * when an incorrect extent item is found, this will delete
5744 * all of the existing entries for it and recreate them
5745 * based on what the tree scan found.
5747 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5748 struct btrfs_fs_info *info,
5749 struct cache_tree *extent_cache,
5750 struct extent_record *rec)
5753 struct btrfs_path *path;
5754 struct list_head *cur = rec->backrefs.next;
5755 struct cache_extent *cache;
5756 struct extent_backref *back;
5761 * remember our flags for recreating the extent.
5762 * FIXME, if we have cleared extent tree, we can not
5763 * lookup extent info in extent tree.
5765 if (!init_extent_tree) {
5766 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5767 rec->start, rec->max_size,
5768 rec->metadata, NULL, &flags);
5775 path = btrfs_alloc_path();
5779 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5781 * Sometimes the backrefs themselves are so broken they don't
5782 * get attached to any meaningful rec, so first go back and
5783 * check any of our backrefs that we couldn't find and throw
5784 * them into the list if we find the backref so that
5785 * verify_backrefs can figure out what to do.
5787 ret = find_possible_backrefs(trans, info, path, extent_cache,
5793 /* step one, make sure all of the backrefs agree */
5794 ret = verify_backrefs(trans, info, path, rec);
5798 /* step two, delete all the existing records */
5799 ret = delete_extent_records(trans, info->extent_root, path,
5800 rec->start, rec->max_size);
5805 /* was this block corrupt? If so, don't add references to it */
5806 cache = lookup_cache_extent(info->corrupt_blocks,
5807 rec->start, rec->max_size);
5813 /* step three, recreate all the refs we did find */
5814 while(cur != &rec->backrefs) {
5815 back = list_entry(cur, struct extent_backref, list);
5819 * if we didn't find any references, don't create a
5822 if (!back->found_ref)
5825 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5832 btrfs_free_path(path);
5836 /* right now we only prune from the extent allocation tree */
5837 static int prune_one_block(struct btrfs_trans_handle *trans,
5838 struct btrfs_fs_info *info,
5839 struct btrfs_corrupt_block *corrupt)
5842 struct btrfs_path path;
5843 struct extent_buffer *eb;
5847 int level = corrupt->level + 1;
5849 btrfs_init_path(&path);
5851 /* we want to stop at the parent to our busted block */
5852 path.lowest_level = level;
5854 ret = btrfs_search_slot(trans, info->extent_root,
5855 &corrupt->key, &path, -1, 1);
5860 eb = path.nodes[level];
5867 * hopefully the search gave us the block we want to prune,
5868 * lets try that first
5870 slot = path.slots[level];
5871 found = btrfs_node_blockptr(eb, slot);
5872 if (found == corrupt->cache.start)
5875 nritems = btrfs_header_nritems(eb);
5877 /* the search failed, lets scan this node and hope we find it */
5878 for (slot = 0; slot < nritems; slot++) {
5879 found = btrfs_node_blockptr(eb, slot);
5880 if (found == corrupt->cache.start)
5884 * we couldn't find the bad block. TODO, search all the nodes for pointers
5887 if (eb == info->extent_root->node) {
5892 btrfs_release_path(&path);
5897 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
5898 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
5901 btrfs_release_path(&path);
5905 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
5906 struct btrfs_fs_info *info)
5908 struct cache_extent *cache;
5909 struct btrfs_corrupt_block *corrupt;
5911 cache = search_cache_extent(info->corrupt_blocks, 0);
5915 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5916 prune_one_block(trans, info, corrupt);
5917 cache = next_cache_extent(cache);
5922 static void free_corrupt_block(struct cache_extent *cache)
5924 struct btrfs_corrupt_block *corrupt;
5926 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5930 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5932 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5934 struct btrfs_block_group_cache *cache;
5939 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5940 &start, &end, EXTENT_DIRTY);
5943 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5949 cache = btrfs_lookup_first_block_group(fs_info, start);
5954 start = cache->key.objectid + cache->key.offset;
5958 static int check_extent_refs(struct btrfs_trans_handle *trans,
5959 struct btrfs_root *root,
5960 struct cache_tree *extent_cache)
5962 struct extent_record *rec;
5963 struct cache_extent *cache;
5971 * if we're doing a repair, we have to make sure
5972 * we don't allocate from the problem extents.
5973 * In the worst case, this will be all the
5976 cache = search_cache_extent(extent_cache, 0);
5978 rec = container_of(cache, struct extent_record, cache);
5979 btrfs_pin_extent(root->fs_info,
5980 rec->start, rec->max_size);
5981 cache = next_cache_extent(cache);
5984 /* pin down all the corrupted blocks too */
5985 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5987 btrfs_pin_extent(root->fs_info,
5988 cache->start, cache->size);
5989 cache = next_cache_extent(cache);
5991 prune_corrupt_blocks(trans, root->fs_info);
5992 reset_cached_block_groups(root->fs_info);
5996 * We need to delete any duplicate entries we find first otherwise we
5997 * could mess up the extent tree when we have backrefs that actually
5998 * belong to a different extent item and not the weird duplicate one.
6000 while (repair && !list_empty(&duplicate_extents)) {
6001 rec = list_entry(duplicate_extents.next, struct extent_record,
6003 list_del_init(&rec->list);
6005 /* Sometimes we can find a backref before we find an actual
6006 * extent, so we need to process it a little bit to see if there
6007 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
6008 * if this is a backref screwup. If we need to delete stuff
6009 * process_duplicates() will return 0, otherwise it will return
6012 if (process_duplicates(root, extent_cache, rec))
6014 ret = delete_duplicate_records(trans, root, rec);
6018 * delete_duplicate_records will return the number of entries
6019 * deleted, so if it's greater than 0 then we know we actually
6020 * did something and we need to remove.
6031 cache = search_cache_extent(extent_cache, 0);
6034 rec = container_of(cache, struct extent_record, cache);
6035 if (rec->num_duplicates) {
6036 fprintf(stderr, "extent item %llu has multiple extent "
6037 "items\n", (unsigned long long)rec->start);
6041 if (rec->refs != rec->extent_item_refs) {
6042 fprintf(stderr, "ref mismatch on [%llu %llu] ",
6043 (unsigned long long)rec->start,
6044 (unsigned long long)rec->nr);
6045 fprintf(stderr, "extent item %llu, found %llu\n",
6046 (unsigned long long)rec->extent_item_refs,
6047 (unsigned long long)rec->refs);
6048 if (!fixed && repair) {
6049 ret = fixup_extent_refs(trans, root->fs_info,
6058 if (all_backpointers_checked(rec, 1)) {
6059 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
6060 (unsigned long long)rec->start,
6061 (unsigned long long)rec->nr);
6063 if (!fixed && repair) {
6064 ret = fixup_extent_refs(trans, root->fs_info,
6073 if (!rec->owner_ref_checked) {
6074 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
6075 (unsigned long long)rec->start,
6076 (unsigned long long)rec->nr);
6077 if (!fixed && repair) {
6078 ret = fixup_extent_refs(trans, root->fs_info,
6087 remove_cache_extent(extent_cache, cache);
6088 free_all_extent_backrefs(rec);
6093 if (ret && ret != -EAGAIN) {
6094 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
6097 btrfs_fix_block_accounting(trans, root);
6100 fprintf(stderr, "repaired damaged extent references\n");
6106 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
6110 if (type & BTRFS_BLOCK_GROUP_RAID0) {
6111 stripe_size = length;
6112 stripe_size /= num_stripes;
6113 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
6114 stripe_size = length * 2;
6115 stripe_size /= num_stripes;
6116 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
6117 stripe_size = length;
6118 stripe_size /= (num_stripes - 1);
6119 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
6120 stripe_size = length;
6121 stripe_size /= (num_stripes - 2);
6123 stripe_size = length;
6128 static int check_chunk_refs(struct chunk_record *chunk_rec,
6129 struct block_group_tree *block_group_cache,
6130 struct device_extent_tree *dev_extent_cache,
6133 struct cache_extent *block_group_item;
6134 struct block_group_record *block_group_rec;
6135 struct cache_extent *dev_extent_item;
6136 struct device_extent_record *dev_extent_rec;
6143 block_group_item = lookup_cache_extent(&block_group_cache->tree,
6146 if (block_group_item) {
6147 block_group_rec = container_of(block_group_item,
6148 struct block_group_record,
6150 if (chunk_rec->length != block_group_rec->offset ||
6151 chunk_rec->offset != block_group_rec->objectid ||
6152 chunk_rec->type_flags != block_group_rec->flags) {
6155 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
6156 chunk_rec->objectid,
6161 chunk_rec->type_flags,
6162 block_group_rec->objectid,
6163 block_group_rec->type,
6164 block_group_rec->offset,
6165 block_group_rec->offset,
6166 block_group_rec->objectid,
6167 block_group_rec->flags);
6170 list_del_init(&block_group_rec->list);
6171 chunk_rec->bg_rec = block_group_rec;
6176 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
6177 chunk_rec->objectid,
6182 chunk_rec->type_flags);
6186 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
6187 chunk_rec->num_stripes);
6188 for (i = 0; i < chunk_rec->num_stripes; ++i) {
6189 devid = chunk_rec->stripes[i].devid;
6190 offset = chunk_rec->stripes[i].offset;
6191 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
6192 devid, offset, length);
6193 if (dev_extent_item) {
6194 dev_extent_rec = container_of(dev_extent_item,
6195 struct device_extent_record,
6197 if (dev_extent_rec->objectid != devid ||
6198 dev_extent_rec->offset != offset ||
6199 dev_extent_rec->chunk_offset != chunk_rec->offset ||
6200 dev_extent_rec->length != length) {
6203 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
6204 chunk_rec->objectid,
6207 chunk_rec->stripes[i].devid,
6208 chunk_rec->stripes[i].offset,
6209 dev_extent_rec->objectid,
6210 dev_extent_rec->offset,
6211 dev_extent_rec->length);
6214 list_move(&dev_extent_rec->chunk_list,
6215 &chunk_rec->dextents);
6220 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
6221 chunk_rec->objectid,
6224 chunk_rec->stripes[i].devid,
6225 chunk_rec->stripes[i].offset);
6232 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
6233 int check_chunks(struct cache_tree *chunk_cache,
6234 struct block_group_tree *block_group_cache,
6235 struct device_extent_tree *dev_extent_cache,
6236 struct list_head *good, struct list_head *bad, int silent)
6238 struct cache_extent *chunk_item;
6239 struct chunk_record *chunk_rec;
6240 struct block_group_record *bg_rec;
6241 struct device_extent_record *dext_rec;
6245 chunk_item = first_cache_extent(chunk_cache);
6246 while (chunk_item) {
6247 chunk_rec = container_of(chunk_item, struct chunk_record,
6249 err = check_chunk_refs(chunk_rec, block_group_cache,
6250 dev_extent_cache, silent);
6254 list_add_tail(&chunk_rec->list, bad);
6257 list_add_tail(&chunk_rec->list, good);
6260 chunk_item = next_cache_extent(chunk_item);
6263 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
6266 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
6274 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
6278 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
6289 static int check_device_used(struct device_record *dev_rec,
6290 struct device_extent_tree *dext_cache)
6292 struct cache_extent *cache;
6293 struct device_extent_record *dev_extent_rec;
6296 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
6298 dev_extent_rec = container_of(cache,
6299 struct device_extent_record,
6301 if (dev_extent_rec->objectid != dev_rec->devid)
6304 list_del_init(&dev_extent_rec->device_list);
6305 total_byte += dev_extent_rec->length;
6306 cache = next_cache_extent(cache);
6309 if (total_byte != dev_rec->byte_used) {
6311 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
6312 total_byte, dev_rec->byte_used, dev_rec->objectid,
6313 dev_rec->type, dev_rec->offset);
6320 /* check btrfs_dev_item -> btrfs_dev_extent */
6321 static int check_devices(struct rb_root *dev_cache,
6322 struct device_extent_tree *dev_extent_cache)
6324 struct rb_node *dev_node;
6325 struct device_record *dev_rec;
6326 struct device_extent_record *dext_rec;
6330 dev_node = rb_first(dev_cache);
6332 dev_rec = container_of(dev_node, struct device_record, node);
6333 err = check_device_used(dev_rec, dev_extent_cache);
6337 dev_node = rb_next(dev_node);
6339 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
6342 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
6343 dext_rec->objectid, dext_rec->offset, dext_rec->length);
6350 static int check_chunks_and_extents(struct btrfs_root *root)
6352 struct rb_root dev_cache;
6353 struct cache_tree chunk_cache;
6354 struct block_group_tree block_group_cache;
6355 struct device_extent_tree dev_extent_cache;
6356 struct cache_tree extent_cache;
6357 struct cache_tree seen;
6358 struct cache_tree pending;
6359 struct cache_tree reada;
6360 struct cache_tree nodes;
6361 struct cache_tree corrupt_blocks;
6362 struct btrfs_path path;
6363 struct btrfs_key key;
6364 struct btrfs_key found_key;
6367 struct block_info *bits;
6369 struct extent_buffer *leaf;
6370 struct btrfs_trans_handle *trans = NULL;
6372 struct btrfs_root_item ri;
6373 struct list_head dropping_trees;
6375 dev_cache = RB_ROOT;
6376 cache_tree_init(&chunk_cache);
6377 block_group_tree_init(&block_group_cache);
6378 device_extent_tree_init(&dev_extent_cache);
6380 cache_tree_init(&extent_cache);
6381 cache_tree_init(&seen);
6382 cache_tree_init(&pending);
6383 cache_tree_init(&nodes);
6384 cache_tree_init(&reada);
6385 cache_tree_init(&corrupt_blocks);
6386 INIT_LIST_HEAD(&dropping_trees);
6389 trans = btrfs_start_transaction(root, 1);
6390 if (IS_ERR(trans)) {
6391 fprintf(stderr, "Error starting transaction\n");
6392 return PTR_ERR(trans);
6394 root->fs_info->fsck_extent_cache = &extent_cache;
6395 root->fs_info->free_extent_hook = free_extent_hook;
6396 root->fs_info->corrupt_blocks = &corrupt_blocks;
6400 bits = malloc(bits_nr * sizeof(struct block_info));
6407 add_root_to_pending(root->fs_info->tree_root->node,
6408 &extent_cache, &pending, &seen, &nodes,
6409 &root->fs_info->tree_root->root_key);
6411 add_root_to_pending(root->fs_info->chunk_root->node,
6412 &extent_cache, &pending, &seen, &nodes,
6413 &root->fs_info->chunk_root->root_key);
6415 btrfs_init_path(&path);
6418 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
6419 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
6424 leaf = path.nodes[0];
6425 slot = path.slots[0];
6426 if (slot >= btrfs_header_nritems(path.nodes[0])) {
6427 ret = btrfs_next_leaf(root, &path);
6430 leaf = path.nodes[0];
6431 slot = path.slots[0];
6433 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
6434 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
6435 unsigned long offset;
6436 struct extent_buffer *buf;
6438 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
6439 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
6440 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
6441 buf = read_tree_block(root->fs_info->tree_root,
6442 btrfs_root_bytenr(&ri),
6443 btrfs_level_size(root,
6444 btrfs_root_level(&ri)),
6450 add_root_to_pending(buf, &extent_cache,
6451 &pending, &seen, &nodes,
6453 free_extent_buffer(buf);
6455 struct dropping_root_item_record *dri_rec;
6456 dri_rec = malloc(sizeof(*dri_rec));
6461 memcpy(&dri_rec->ri, &ri, sizeof(ri));
6462 memcpy(&dri_rec->found_key, &found_key,
6464 list_add_tail(&dri_rec->list, &dropping_trees);
6469 btrfs_release_path(&path);
6471 ret = run_next_block(trans, root, bits, bits_nr, &last,
6472 &pending, &seen, &reada, &nodes,
6473 &extent_cache, &chunk_cache, &dev_cache,
6474 &block_group_cache, &dev_extent_cache,
6480 while (!list_empty(&dropping_trees)) {
6481 struct dropping_root_item_record *rec;
6482 struct extent_buffer *buf;
6483 rec = list_entry(dropping_trees.next,
6484 struct dropping_root_item_record, list);
6490 buf = read_tree_block(root->fs_info->tree_root,
6491 btrfs_root_bytenr(&rec->ri),
6492 btrfs_level_size(root,
6493 btrfs_root_level(&rec->ri)), 0);
6498 add_root_to_pending(buf, &extent_cache, &pending,
6499 &seen, &nodes, &rec->found_key);
6501 ret = run_next_block(trans, root, bits, bits_nr, &last,
6502 &pending, &seen, &reada,
6503 &nodes, &extent_cache,
6504 &chunk_cache, &dev_cache,
6511 free_extent_buffer(buf);
6512 list_del(&rec->list);
6517 ret = check_extent_refs(trans, root, &extent_cache);
6518 if (ret == -EAGAIN) {
6519 ret = btrfs_commit_transaction(trans, root);
6523 trans = btrfs_start_transaction(root, 1);
6524 if (IS_ERR(trans)) {
6525 ret = PTR_ERR(trans);
6529 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6530 free_extent_cache_tree(&seen);
6531 free_extent_cache_tree(&pending);
6532 free_extent_cache_tree(&reada);
6533 free_extent_cache_tree(&nodes);
6534 free_chunk_cache_tree(&chunk_cache);
6535 free_block_group_tree(&block_group_cache);
6536 free_device_cache_tree(&dev_cache);
6537 free_device_extent_tree(&dev_extent_cache);
6538 free_extent_record_cache(root->fs_info, &extent_cache);
6542 err = check_chunks(&chunk_cache, &block_group_cache,
6543 &dev_extent_cache, NULL, NULL, 0);
6547 err = check_devices(&dev_cache, &dev_extent_cache);
6553 err = btrfs_commit_transaction(trans, root);
6558 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6559 root->fs_info->fsck_extent_cache = NULL;
6560 root->fs_info->free_extent_hook = NULL;
6561 root->fs_info->corrupt_blocks = NULL;
6564 free_chunk_cache_tree(&chunk_cache);
6565 free_device_cache_tree(&dev_cache);
6566 free_block_group_tree(&block_group_cache);
6567 free_device_extent_tree(&dev_extent_cache);
6568 free_extent_cache_tree(&seen);
6569 free_extent_cache_tree(&pending);
6570 free_extent_cache_tree(&reada);
6571 free_extent_cache_tree(&nodes);
6575 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
6576 struct btrfs_root *root, int overwrite)
6578 struct extent_buffer *c;
6579 struct extent_buffer *old = root->node;
6582 struct btrfs_disk_key disk_key = {0,0,0};
6588 extent_buffer_get(c);
6591 c = btrfs_alloc_free_block(trans, root,
6592 btrfs_level_size(root, 0),
6593 root->root_key.objectid,
6594 &disk_key, level, 0, 0);
6597 extent_buffer_get(c);
6601 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
6602 btrfs_set_header_level(c, level);
6603 btrfs_set_header_bytenr(c, c->start);
6604 btrfs_set_header_generation(c, trans->transid);
6605 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
6606 btrfs_set_header_owner(c, root->root_key.objectid);
6608 write_extent_buffer(c, root->fs_info->fsid,
6609 btrfs_header_fsid(), BTRFS_FSID_SIZE);
6611 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
6612 btrfs_header_chunk_tree_uuid(c),
6615 btrfs_mark_buffer_dirty(c);
6617 * this case can happen in the following case:
6619 * 1.overwrite previous root.
6621 * 2.reinit reloc data root, this is because we skip pin
6622 * down reloc data tree before which means we can allocate
6623 * same block bytenr here.
6625 if (old->start == c->start) {
6626 btrfs_set_root_generation(&root->root_item,
6628 root->root_item.level = btrfs_header_level(root->node);
6629 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6630 &root->root_key, &root->root_item);
6632 free_extent_buffer(c);
6636 free_extent_buffer(old);
6638 add_root_to_dirty_list(root);
6642 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
6643 struct extent_buffer *eb, int tree_root)
6645 struct extent_buffer *tmp;
6646 struct btrfs_root_item *ri;
6647 struct btrfs_key key;
6650 int level = btrfs_header_level(eb);
6656 * If we have pinned this block before, don't pin it again.
6657 * This can not only avoid forever loop with broken filesystem
6658 * but also give us some speedups.
6660 if (test_range_bit(&fs_info->pinned_extents, eb->start,
6661 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
6664 btrfs_pin_extent(fs_info, eb->start, eb->len);
6666 leafsize = btrfs_super_leafsize(fs_info->super_copy);
6667 nritems = btrfs_header_nritems(eb);
6668 for (i = 0; i < nritems; i++) {
6670 btrfs_item_key_to_cpu(eb, &key, i);
6671 if (key.type != BTRFS_ROOT_ITEM_KEY)
6673 /* Skip the extent root and reloc roots */
6674 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6675 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6676 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6678 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6679 bytenr = btrfs_disk_root_bytenr(eb, ri);
6682 * If at any point we start needing the real root we
6683 * will have to build a stump root for the root we are
6684 * in, but for now this doesn't actually use the root so
6685 * just pass in extent_root.
6687 tmp = read_tree_block(fs_info->extent_root, bytenr,
6690 fprintf(stderr, "Error reading root block\n");
6693 ret = pin_down_tree_blocks(fs_info, tmp, 0);
6694 free_extent_buffer(tmp);
6698 bytenr = btrfs_node_blockptr(eb, i);
6700 /* If we aren't the tree root don't read the block */
6701 if (level == 1 && !tree_root) {
6702 btrfs_pin_extent(fs_info, bytenr, leafsize);
6706 tmp = read_tree_block(fs_info->extent_root, bytenr,
6709 fprintf(stderr, "Error reading tree block\n");
6712 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6713 free_extent_buffer(tmp);
6722 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6726 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6730 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6733 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6735 struct btrfs_block_group_cache *cache;
6736 struct btrfs_path *path;
6737 struct extent_buffer *leaf;
6738 struct btrfs_chunk *chunk;
6739 struct btrfs_key key;
6743 path = btrfs_alloc_path();
6748 key.type = BTRFS_CHUNK_ITEM_KEY;
6751 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6753 btrfs_free_path(path);
6758 * We do this in case the block groups were screwed up and had alloc
6759 * bits that aren't actually set on the chunks. This happens with
6760 * restored images every time and could happen in real life I guess.
6762 fs_info->avail_data_alloc_bits = 0;
6763 fs_info->avail_metadata_alloc_bits = 0;
6764 fs_info->avail_system_alloc_bits = 0;
6766 /* First we need to create the in-memory block groups */
6768 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6769 ret = btrfs_next_leaf(fs_info->chunk_root, path);
6771 btrfs_free_path(path);
6779 leaf = path->nodes[0];
6780 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6781 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6786 chunk = btrfs_item_ptr(leaf, path->slots[0],
6787 struct btrfs_chunk);
6788 btrfs_add_block_group(fs_info, 0,
6789 btrfs_chunk_type(leaf, chunk),
6790 key.objectid, key.offset,
6791 btrfs_chunk_length(leaf, chunk));
6792 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6793 key.offset + btrfs_chunk_length(leaf, chunk),
6799 cache = btrfs_lookup_first_block_group(fs_info, start);
6803 start = cache->key.objectid + cache->key.offset;
6806 btrfs_free_path(path);
6810 static int reset_balance(struct btrfs_trans_handle *trans,
6811 struct btrfs_fs_info *fs_info)
6813 struct btrfs_root *root = fs_info->tree_root;
6814 struct btrfs_path *path;
6815 struct extent_buffer *leaf;
6816 struct btrfs_key key;
6817 int del_slot, del_nr = 0;
6821 path = btrfs_alloc_path();
6825 key.objectid = BTRFS_BALANCE_OBJECTID;
6826 key.type = BTRFS_BALANCE_ITEM_KEY;
6829 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6834 goto reinit_data_reloc;
6839 ret = btrfs_del_item(trans, root, path);
6842 btrfs_release_path(path);
6844 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6845 key.type = BTRFS_ROOT_ITEM_KEY;
6848 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6852 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6857 ret = btrfs_del_items(trans, root, path,
6864 btrfs_release_path(path);
6867 ret = btrfs_search_slot(trans, root, &key, path,
6874 leaf = path->nodes[0];
6875 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6876 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
6878 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6883 del_slot = path->slots[0];
6892 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
6896 btrfs_release_path(path);
6899 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6900 key.type = BTRFS_ROOT_ITEM_KEY;
6901 key.offset = (u64)-1;
6902 root = btrfs_read_fs_root(fs_info, &key);
6904 fprintf(stderr, "Error reading data reloc tree\n");
6905 return PTR_ERR(root);
6907 record_root_in_trans(trans, root);
6908 ret = btrfs_fsck_reinit_root(trans, root, 0);
6911 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
6913 btrfs_free_path(path);
6917 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
6918 struct btrfs_fs_info *fs_info)
6924 * The only reason we don't do this is because right now we're just
6925 * walking the trees we find and pinning down their bytes, we don't look
6926 * at any of the leaves. In order to do mixed groups we'd have to check
6927 * the leaves of any fs roots and pin down the bytes for any file
6928 * extents we find. Not hard but why do it if we don't have to?
6930 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
6931 fprintf(stderr, "We don't support re-initing the extent tree "
6932 "for mixed block groups yet, please notify a btrfs "
6933 "developer you want to do this so they can add this "
6934 "functionality.\n");
6939 * first we need to walk all of the trees except the extent tree and pin
6940 * down the bytes that are in use so we don't overwrite any existing
6943 ret = pin_metadata_blocks(fs_info);
6945 fprintf(stderr, "error pinning down used bytes\n");
6950 * Need to drop all the block groups since we're going to recreate all
6953 btrfs_free_block_groups(fs_info);
6954 ret = reset_block_groups(fs_info);
6956 fprintf(stderr, "error resetting the block groups\n");
6960 /* Ok we can allocate now, reinit the extent root */
6961 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
6963 fprintf(stderr, "extent root initialization failed\n");
6965 * When the transaction code is updated we should end the
6966 * transaction, but for now progs only knows about commit so
6967 * just return an error.
6973 * Now we have all the in-memory block groups setup so we can make
6974 * allocations properly, and the metadata we care about is safe since we
6975 * pinned all of it above.
6978 struct btrfs_block_group_cache *cache;
6980 cache = btrfs_lookup_first_block_group(fs_info, start);
6983 start = cache->key.objectid + cache->key.offset;
6984 ret = btrfs_insert_item(trans, fs_info->extent_root,
6985 &cache->key, &cache->item,
6986 sizeof(cache->item));
6988 fprintf(stderr, "Error adding block group\n");
6991 btrfs_extent_post_op(trans, fs_info->extent_root);
6994 ret = reset_balance(trans, fs_info);
6996 fprintf(stderr, "error reseting the pending balance\n");
7001 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
7003 struct btrfs_path *path;
7004 struct btrfs_trans_handle *trans;
7005 struct btrfs_key key;
7008 printf("Recowing metadata block %llu\n", eb->start);
7009 key.objectid = btrfs_header_owner(eb);
7010 key.type = BTRFS_ROOT_ITEM_KEY;
7011 key.offset = (u64)-1;
7013 root = btrfs_read_fs_root(root->fs_info, &key);
7015 fprintf(stderr, "Couldn't find owner root %llu\n",
7017 return PTR_ERR(root);
7020 path = btrfs_alloc_path();
7024 trans = btrfs_start_transaction(root, 1);
7025 if (IS_ERR(trans)) {
7026 btrfs_free_path(path);
7027 return PTR_ERR(trans);
7030 path->lowest_level = btrfs_header_level(eb);
7031 if (path->lowest_level)
7032 btrfs_node_key_to_cpu(eb, &key, 0);
7034 btrfs_item_key_to_cpu(eb, &key, 0);
7036 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7037 btrfs_commit_transaction(trans, root);
7038 btrfs_free_path(path);
7042 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
7044 struct btrfs_path *path;
7045 struct btrfs_trans_handle *trans;
7046 struct btrfs_key key;
7049 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
7050 bad->key.type, bad->key.offset);
7051 key.objectid = bad->root_id;
7052 key.type = BTRFS_ROOT_ITEM_KEY;
7053 key.offset = (u64)-1;
7055 root = btrfs_read_fs_root(root->fs_info, &key);
7057 fprintf(stderr, "Couldn't find owner root %llu\n",
7059 return PTR_ERR(root);
7062 path = btrfs_alloc_path();
7066 trans = btrfs_start_transaction(root, 1);
7067 if (IS_ERR(trans)) {
7068 btrfs_free_path(path);
7069 return PTR_ERR(trans);
7072 ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
7078 ret = btrfs_del_item(trans, root, path);
7080 btrfs_commit_transaction(trans, root);
7081 btrfs_free_path(path);
7085 static int zero_log_tree(struct btrfs_root *root)
7087 struct btrfs_trans_handle *trans;
7090 trans = btrfs_start_transaction(root, 1);
7091 if (IS_ERR(trans)) {
7092 ret = PTR_ERR(trans);
7095 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
7096 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
7097 ret = btrfs_commit_transaction(trans, root);
7101 static int populate_csum(struct btrfs_trans_handle *trans,
7102 struct btrfs_root *csum_root, char *buf, u64 start,
7109 while (offset < len) {
7110 sectorsize = csum_root->sectorsize;
7111 ret = read_extent_data(csum_root, buf, start + offset,
7115 ret = btrfs_csum_file_block(trans, csum_root, start + len,
7116 start + offset, buf, sectorsize);
7119 offset += sectorsize;
7124 static int fill_csum_tree(struct btrfs_trans_handle *trans,
7125 struct btrfs_root *csum_root)
7127 struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
7128 struct btrfs_path *path;
7129 struct btrfs_extent_item *ei;
7130 struct extent_buffer *leaf;
7132 struct btrfs_key key;
7135 path = btrfs_alloc_path();
7140 key.type = BTRFS_EXTENT_ITEM_KEY;
7143 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
7145 btrfs_free_path(path);
7149 buf = malloc(csum_root->sectorsize);
7151 btrfs_free_path(path);
7156 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7157 ret = btrfs_next_leaf(extent_root, path);
7165 leaf = path->nodes[0];
7167 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7168 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
7173 ei = btrfs_item_ptr(leaf, path->slots[0],
7174 struct btrfs_extent_item);
7175 if (!(btrfs_extent_flags(leaf, ei) &
7176 BTRFS_EXTENT_FLAG_DATA)) {
7181 ret = populate_csum(trans, csum_root, buf, key.objectid,
7188 btrfs_free_path(path);
7193 static struct option long_options[] = {
7194 { "super", 1, NULL, 's' },
7195 { "repair", 0, NULL, 0 },
7196 { "init-csum-tree", 0, NULL, 0 },
7197 { "init-extent-tree", 0, NULL, 0 },
7198 { "check-data-csum", 0, NULL, 0 },
7199 { "backup", 0, NULL, 0 },
7200 { "subvol-extents", no_argument, NULL, 'E' },
7201 { "qgroup-report", 0, NULL, 'Q' },
7205 const char * const cmd_check_usage[] = {
7206 "btrfs check [options] <device>",
7207 "Check an unmounted btrfs filesystem.",
7209 "-s|--super <superblock> use this superblock copy",
7210 "-b|--backup use the backup root copy",
7211 "--repair try to repair the filesystem",
7212 "--init-csum-tree create a new CRC tree",
7213 "--init-extent-tree create a new extent tree",
7214 "--check-data-csum verify checkums of data blocks",
7215 "--qgroup-report print a report on qgroup consistency",
7216 "--subvol-extents print subvolume extents and sharing state",
7220 int cmd_check(int argc, char **argv)
7222 struct cache_tree root_cache;
7223 struct btrfs_root *root;
7224 struct btrfs_fs_info *info;
7227 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
7230 int option_index = 0;
7231 int init_csum_tree = 0;
7232 int qgroup_report = 0;
7233 enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_EXCLUSIVE;
7237 c = getopt_long(argc, argv, "as:b", long_options,
7242 case 'a': /* ignored */ break;
7244 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
7247 num = arg_strtou64(optarg);
7248 if (num >= BTRFS_SUPER_MIRROR_MAX) {
7250 "ERROR: super mirror should be less than: %d\n",
7251 BTRFS_SUPER_MIRROR_MAX);
7254 bytenr = btrfs_sb_offset(((int)num));
7255 printf("using SB copy %llu, bytenr %llu\n", num,
7256 (unsigned long long)bytenr);
7262 subvolid = arg_strtou64(optarg);
7266 usage(cmd_check_usage);
7268 if (option_index == 1) {
7269 printf("enabling repair mode\n");
7271 ctree_flags |= OPEN_CTREE_WRITES;
7272 } else if (option_index == 2) {
7273 printf("Creating a new CRC tree\n");
7276 ctree_flags |= OPEN_CTREE_WRITES;
7277 } else if (option_index == 3) {
7278 init_extent_tree = 1;
7279 ctree_flags |= (OPEN_CTREE_WRITES |
7280 OPEN_CTREE_NO_BLOCK_GROUPS);
7282 } else if (option_index == 4) {
7283 check_data_csum = 1;
7286 argc = argc - optind;
7288 if (check_argc_exact(argc, 1))
7289 usage(cmd_check_usage);
7292 cache_tree_init(&root_cache);
7294 if((ret = check_mounted(argv[optind])) < 0) {
7295 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
7298 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
7303 /* only allow partial opening under repair mode */
7305 ctree_flags |= OPEN_CTREE_PARTIAL;
7307 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
7309 fprintf(stderr, "Couldn't open file system\n");
7314 root = info->fs_root;
7316 * repair mode will force us to commit transaction which
7317 * will make us fail to load log tree when mounting.
7319 if (repair && btrfs_super_log_root(info->super_copy)) {
7320 ret = ask_user("repair mode will force to clear out log tree, Are you sure?");
7325 ret = zero_log_tree(root);
7327 fprintf(stderr, "fail to zero log tree\n");
7332 uuid_unparse(info->super_copy->fsid, uuidbuf);
7333 if (qgroup_report) {
7334 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
7336 ret = qgroup_verify_all(info);
7338 print_qgroup_report(1);
7342 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
7343 subvolid, argv[optind], uuidbuf);
7344 ret = print_extent_state(info, subvolid);
7347 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
7349 if (!extent_buffer_uptodate(info->tree_root->node) ||
7350 !extent_buffer_uptodate(info->dev_root->node) ||
7351 !extent_buffer_uptodate(info->chunk_root->node)) {
7352 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7357 if (init_extent_tree || init_csum_tree) {
7358 struct btrfs_trans_handle *trans;
7360 trans = btrfs_start_transaction(info->extent_root, 0);
7361 if (IS_ERR(trans)) {
7362 fprintf(stderr, "Error starting transaction\n");
7363 ret = PTR_ERR(trans);
7367 if (init_extent_tree) {
7368 printf("Creating a new extent tree\n");
7369 ret = reinit_extent_tree(trans, info);
7374 if (init_csum_tree) {
7375 fprintf(stderr, "Reinit crc root\n");
7376 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
7378 fprintf(stderr, "crc root initialization failed\n");
7383 ret = fill_csum_tree(trans, info->csum_root);
7385 fprintf(stderr, "crc refilling failed\n");
7390 * Ok now we commit and run the normal fsck, which will add
7391 * extent entries for all of the items it finds.
7393 ret = btrfs_commit_transaction(trans, info->extent_root);
7397 if (!extent_buffer_uptodate(info->extent_root->node)) {
7398 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
7402 if (!extent_buffer_uptodate(info->csum_root->node)) {
7403 fprintf(stderr, "Checksum root corrupted, rerun with --init-csum-tree option\n");
7408 fprintf(stderr, "checking extents\n");
7409 ret = check_chunks_and_extents(root);
7411 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
7413 fprintf(stderr, "checking free space cache\n");
7414 ret = check_space_cache(root);
7419 * We used to have to have these hole extents in between our real
7420 * extents so if we don't have this flag set we need to make sure there
7421 * are no gaps in the file extents for inodes, otherwise we can just
7422 * ignore it when this happens.
7424 no_holes = btrfs_fs_incompat(root->fs_info,
7425 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
7426 fprintf(stderr, "checking fs roots\n");
7427 ret = check_fs_roots(root, &root_cache);
7431 fprintf(stderr, "checking csums\n");
7432 ret = check_csums(root);
7436 fprintf(stderr, "checking root refs\n");
7437 ret = check_root_refs(root, &root_cache);
7441 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
7442 struct extent_buffer *eb;
7444 eb = list_first_entry(&root->fs_info->recow_ebs,
7445 struct extent_buffer, recow);
7446 list_del_init(&eb->recow);
7447 ret = recow_extent_buffer(root, eb);
7452 while (!list_empty(&delete_items)) {
7453 struct bad_item *bad;
7455 bad = list_first_entry(&delete_items, struct bad_item, list);
7456 list_del_init(&bad->list);
7458 ret = delete_bad_item(root, bad);
7462 if (info->quota_enabled) {
7464 fprintf(stderr, "checking quota groups\n");
7465 err = qgroup_verify_all(info);
7470 if (!list_empty(&root->fs_info->recow_ebs)) {
7471 fprintf(stderr, "Transid errors in file system\n");
7475 print_qgroup_report(0);
7476 if (found_old_backref) { /*
7477 * there was a disk format change when mixed
7478 * backref was in testing tree. The old format
7479 * existed about one week.
7481 printf("\n * Found old mixed backref format. "
7482 "The old format is not supported! *"
7483 "\n * Please mount the FS in readonly mode, "
7484 "backup data and re-format the FS. *\n\n");
7487 printf("found %llu bytes used err is %d\n",
7488 (unsigned long long)bytes_used, ret);
7489 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
7490 printf("total tree bytes: %llu\n",
7491 (unsigned long long)total_btree_bytes);
7492 printf("total fs tree bytes: %llu\n",
7493 (unsigned long long)total_fs_tree_bytes);
7494 printf("total extent tree bytes: %llu\n",
7495 (unsigned long long)total_extent_tree_bytes);
7496 printf("btree space waste bytes: %llu\n",
7497 (unsigned long long)btree_space_waste);
7498 printf("file data blocks allocated: %llu\n referenced %llu\n",
7499 (unsigned long long)data_bytes_allocated,
7500 (unsigned long long)data_bytes_referenced);
7501 printf("%s\n", BTRFS_BUILD_VERSION);
7503 free_root_recs_tree(&root_cache);