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"
43 static u64 bytes_used = 0;
44 static u64 total_csum_bytes = 0;
45 static u64 total_btree_bytes = 0;
46 static u64 total_fs_tree_bytes = 0;
47 static u64 total_extent_tree_bytes = 0;
48 static u64 btree_space_waste = 0;
49 static u64 data_bytes_allocated = 0;
50 static u64 data_bytes_referenced = 0;
51 static int found_old_backref = 0;
52 static LIST_HEAD(duplicate_extents);
53 static LIST_HEAD(delete_items);
54 static int repair = 0;
55 static int no_holes = 0;
56 static int init_extent_tree = 0;
57 static int check_data_csum = 0;
59 struct extent_backref {
60 struct list_head list;
61 unsigned int is_data:1;
62 unsigned int found_extent_tree:1;
63 unsigned int full_backref:1;
64 unsigned int found_ref:1;
65 unsigned int broken:1;
69 struct extent_backref node;
84 struct extent_backref node;
91 struct extent_record {
92 struct list_head backrefs;
93 struct list_head dups;
94 struct list_head list;
95 struct cache_extent cache;
96 struct btrfs_disk_key parent_key;
101 u64 extent_item_refs;
103 u64 parent_generation;
107 unsigned int found_rec:1;
108 unsigned int content_checked:1;
109 unsigned int owner_ref_checked:1;
110 unsigned int is_root:1;
111 unsigned int metadata:1;
114 struct inode_backref {
115 struct list_head list;
116 unsigned int found_dir_item:1;
117 unsigned int found_dir_index:1;
118 unsigned int found_inode_ref:1;
119 unsigned int filetype:8;
121 unsigned int ref_type;
128 struct dropping_root_item_record {
129 struct list_head list;
130 struct btrfs_root_item ri;
131 struct btrfs_key found_key;
134 #define REF_ERR_NO_DIR_ITEM (1 << 0)
135 #define REF_ERR_NO_DIR_INDEX (1 << 1)
136 #define REF_ERR_NO_INODE_REF (1 << 2)
137 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
138 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
139 #define REF_ERR_DUP_INODE_REF (1 << 5)
140 #define REF_ERR_INDEX_UNMATCH (1 << 6)
141 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
142 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
143 #define REF_ERR_NO_ROOT_REF (1 << 9)
144 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
145 #define REF_ERR_DUP_ROOT_REF (1 << 11)
146 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
148 struct inode_record {
149 struct list_head backrefs;
150 unsigned int checked:1;
151 unsigned int merging:1;
152 unsigned int found_inode_item:1;
153 unsigned int found_dir_item:1;
154 unsigned int found_file_extent:1;
155 unsigned int found_csum_item:1;
156 unsigned int some_csum_missing:1;
157 unsigned int nodatasum:1;
170 u64 first_extent_gap;
175 #define I_ERR_NO_INODE_ITEM (1 << 0)
176 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
177 #define I_ERR_DUP_INODE_ITEM (1 << 2)
178 #define I_ERR_DUP_DIR_INDEX (1 << 3)
179 #define I_ERR_ODD_DIR_ITEM (1 << 4)
180 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
181 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
182 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
183 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
184 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
185 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
186 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
187 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
188 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
190 struct root_backref {
191 struct list_head list;
192 unsigned int found_dir_item:1;
193 unsigned int found_dir_index:1;
194 unsigned int found_back_ref:1;
195 unsigned int found_forward_ref:1;
196 unsigned int reachable:1;
206 struct list_head backrefs;
207 struct cache_extent cache;
208 unsigned int found_root_item:1;
214 struct cache_extent cache;
219 struct cache_extent cache;
220 struct cache_tree root_cache;
221 struct cache_tree inode_cache;
222 struct inode_record *current;
231 struct walk_control {
232 struct cache_tree shared;
233 struct shared_node *nodes[BTRFS_MAX_LEVEL];
239 struct btrfs_key key;
241 struct list_head list;
244 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
246 static u8 imode_to_type(u32 imode)
249 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
250 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
251 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
252 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
253 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
254 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
255 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
256 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
259 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
263 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
265 struct device_record *rec1;
266 struct device_record *rec2;
268 rec1 = rb_entry(node1, struct device_record, node);
269 rec2 = rb_entry(node2, struct device_record, node);
270 if (rec1->devid > rec2->devid)
272 else if (rec1->devid < rec2->devid)
278 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
280 struct inode_record *rec;
281 struct inode_backref *backref;
282 struct inode_backref *orig;
285 rec = malloc(sizeof(*rec));
286 memcpy(rec, orig_rec, sizeof(*rec));
288 INIT_LIST_HEAD(&rec->backrefs);
290 list_for_each_entry(orig, &orig_rec->backrefs, list) {
291 size = sizeof(*orig) + orig->namelen + 1;
292 backref = malloc(size);
293 memcpy(backref, orig, size);
294 list_add_tail(&backref->list, &rec->backrefs);
299 static void print_inode_error(int errors)
301 if (errors & I_ERR_NO_INODE_ITEM)
302 fprintf(stderr, ", no inode item");
303 if (errors & I_ERR_NO_ORPHAN_ITEM)
304 fprintf(stderr, ", no orphan item");
305 if (errors & I_ERR_DUP_INODE_ITEM)
306 fprintf(stderr, ", dup inode item");
307 if (errors & I_ERR_DUP_DIR_INDEX)
308 fprintf(stderr, ", dup dir index");
309 if (errors & I_ERR_ODD_DIR_ITEM)
310 fprintf(stderr, ", odd dir item");
311 if (errors & I_ERR_ODD_FILE_EXTENT)
312 fprintf(stderr, ", odd file extent");
313 if (errors & I_ERR_BAD_FILE_EXTENT)
314 fprintf(stderr, ", bad file extent");
315 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
316 fprintf(stderr, ", file extent overlap");
317 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
318 fprintf(stderr, ", file extent discount");
319 if (errors & I_ERR_DIR_ISIZE_WRONG)
320 fprintf(stderr, ", dir isize wrong");
321 if (errors & I_ERR_FILE_NBYTES_WRONG)
322 fprintf(stderr, ", nbytes wrong");
323 if (errors & I_ERR_ODD_CSUM_ITEM)
324 fprintf(stderr, ", odd csum item");
325 if (errors & I_ERR_SOME_CSUM_MISSING)
326 fprintf(stderr, ", some csum missing");
327 if (errors & I_ERR_LINK_COUNT_WRONG)
328 fprintf(stderr, ", link count wrong");
329 fprintf(stderr, "\n");
332 static void print_ref_error(int errors)
334 if (errors & REF_ERR_NO_DIR_ITEM)
335 fprintf(stderr, ", no dir item");
336 if (errors & REF_ERR_NO_DIR_INDEX)
337 fprintf(stderr, ", no dir index");
338 if (errors & REF_ERR_NO_INODE_REF)
339 fprintf(stderr, ", no inode ref");
340 if (errors & REF_ERR_DUP_DIR_ITEM)
341 fprintf(stderr, ", dup dir item");
342 if (errors & REF_ERR_DUP_DIR_INDEX)
343 fprintf(stderr, ", dup dir index");
344 if (errors & REF_ERR_DUP_INODE_REF)
345 fprintf(stderr, ", dup inode ref");
346 if (errors & REF_ERR_INDEX_UNMATCH)
347 fprintf(stderr, ", index unmatch");
348 if (errors & REF_ERR_FILETYPE_UNMATCH)
349 fprintf(stderr, ", filetype unmatch");
350 if (errors & REF_ERR_NAME_TOO_LONG)
351 fprintf(stderr, ", name too long");
352 if (errors & REF_ERR_NO_ROOT_REF)
353 fprintf(stderr, ", no root ref");
354 if (errors & REF_ERR_NO_ROOT_BACKREF)
355 fprintf(stderr, ", no root backref");
356 if (errors & REF_ERR_DUP_ROOT_REF)
357 fprintf(stderr, ", dup root ref");
358 if (errors & REF_ERR_DUP_ROOT_BACKREF)
359 fprintf(stderr, ", dup root backref");
360 fprintf(stderr, "\n");
363 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
366 struct ptr_node *node;
367 struct cache_extent *cache;
368 struct inode_record *rec = NULL;
371 cache = lookup_cache_extent(inode_cache, ino, 1);
373 node = container_of(cache, struct ptr_node, cache);
375 if (mod && rec->refs > 1) {
376 node->data = clone_inode_rec(rec);
381 rec = calloc(1, sizeof(*rec));
383 rec->extent_start = (u64)-1;
384 rec->first_extent_gap = (u64)-1;
386 INIT_LIST_HEAD(&rec->backrefs);
388 node = malloc(sizeof(*node));
389 node->cache.start = ino;
390 node->cache.size = 1;
393 if (ino == BTRFS_FREE_INO_OBJECTID)
396 ret = insert_cache_extent(inode_cache, &node->cache);
402 static void free_inode_rec(struct inode_record *rec)
404 struct inode_backref *backref;
409 while (!list_empty(&rec->backrefs)) {
410 backref = list_entry(rec->backrefs.next,
411 struct inode_backref, list);
412 list_del(&backref->list);
418 static int can_free_inode_rec(struct inode_record *rec)
420 if (!rec->errors && rec->checked && rec->found_inode_item &&
421 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
426 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
427 struct inode_record *rec)
429 struct cache_extent *cache;
430 struct inode_backref *tmp, *backref;
431 struct ptr_node *node;
432 unsigned char filetype;
434 if (!rec->found_inode_item)
437 filetype = imode_to_type(rec->imode);
438 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
439 if (backref->found_dir_item && backref->found_dir_index) {
440 if (backref->filetype != filetype)
441 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
442 if (!backref->errors && backref->found_inode_ref) {
443 list_del(&backref->list);
449 if (!rec->checked || rec->merging)
452 if (S_ISDIR(rec->imode)) {
453 if (rec->found_size != rec->isize)
454 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
455 if (rec->found_file_extent)
456 rec->errors |= I_ERR_ODD_FILE_EXTENT;
457 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
458 if (rec->found_dir_item)
459 rec->errors |= I_ERR_ODD_DIR_ITEM;
460 if (rec->found_size != rec->nbytes)
461 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
462 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
463 rec->first_extent_gap = 0;
464 if (rec->nlink > 0 && !no_holes &&
465 (rec->extent_end < rec->isize ||
466 rec->first_extent_gap < rec->isize))
467 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
470 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
471 if (rec->found_csum_item && rec->nodatasum)
472 rec->errors |= I_ERR_ODD_CSUM_ITEM;
473 if (rec->some_csum_missing && !rec->nodatasum)
474 rec->errors |= I_ERR_SOME_CSUM_MISSING;
477 BUG_ON(rec->refs != 1);
478 if (can_free_inode_rec(rec)) {
479 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
480 node = container_of(cache, struct ptr_node, cache);
481 BUG_ON(node->data != rec);
482 remove_cache_extent(inode_cache, &node->cache);
488 static int check_orphan_item(struct btrfs_root *root, u64 ino)
490 struct btrfs_path path;
491 struct btrfs_key key;
494 key.objectid = BTRFS_ORPHAN_OBJECTID;
495 key.type = BTRFS_ORPHAN_ITEM_KEY;
498 btrfs_init_path(&path);
499 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
500 btrfs_release_path(&path);
506 static int process_inode_item(struct extent_buffer *eb,
507 int slot, struct btrfs_key *key,
508 struct shared_node *active_node)
510 struct inode_record *rec;
511 struct btrfs_inode_item *item;
513 rec = active_node->current;
514 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
515 if (rec->found_inode_item) {
516 rec->errors |= I_ERR_DUP_INODE_ITEM;
519 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
520 rec->nlink = btrfs_inode_nlink(eb, item);
521 rec->isize = btrfs_inode_size(eb, item);
522 rec->nbytes = btrfs_inode_nbytes(eb, item);
523 rec->imode = btrfs_inode_mode(eb, item);
524 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
526 rec->found_inode_item = 1;
528 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
529 maybe_free_inode_rec(&active_node->inode_cache, rec);
533 static struct inode_backref *get_inode_backref(struct inode_record *rec,
535 int namelen, u64 dir)
537 struct inode_backref *backref;
539 list_for_each_entry(backref, &rec->backrefs, list) {
540 if (backref->dir != dir || backref->namelen != namelen)
542 if (memcmp(name, backref->name, namelen))
547 backref = malloc(sizeof(*backref) + namelen + 1);
548 memset(backref, 0, sizeof(*backref));
550 backref->namelen = namelen;
551 memcpy(backref->name, name, namelen);
552 backref->name[namelen] = '\0';
553 list_add_tail(&backref->list, &rec->backrefs);
557 static int add_inode_backref(struct cache_tree *inode_cache,
558 u64 ino, u64 dir, u64 index,
559 const char *name, int namelen,
560 int filetype, int itemtype, int errors)
562 struct inode_record *rec;
563 struct inode_backref *backref;
565 rec = get_inode_rec(inode_cache, ino, 1);
566 backref = get_inode_backref(rec, name, namelen, dir);
568 backref->errors |= errors;
569 if (itemtype == BTRFS_DIR_INDEX_KEY) {
570 if (backref->found_dir_index)
571 backref->errors |= REF_ERR_DUP_DIR_INDEX;
572 if (backref->found_inode_ref && backref->index != index)
573 backref->errors |= REF_ERR_INDEX_UNMATCH;
574 if (backref->found_dir_item && backref->filetype != filetype)
575 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
577 backref->index = index;
578 backref->filetype = filetype;
579 backref->found_dir_index = 1;
580 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
582 if (backref->found_dir_item)
583 backref->errors |= REF_ERR_DUP_DIR_ITEM;
584 if (backref->found_dir_index && backref->filetype != filetype)
585 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
587 backref->filetype = filetype;
588 backref->found_dir_item = 1;
589 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
590 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
591 if (backref->found_inode_ref)
592 backref->errors |= REF_ERR_DUP_INODE_REF;
593 if (backref->found_dir_index && backref->index != index)
594 backref->errors |= REF_ERR_INDEX_UNMATCH;
596 backref->ref_type = itemtype;
597 backref->index = index;
598 backref->found_inode_ref = 1;
603 maybe_free_inode_rec(inode_cache, rec);
607 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
608 struct cache_tree *dst_cache)
610 struct inode_backref *backref;
614 list_for_each_entry(backref, &src->backrefs, list) {
615 if (backref->found_dir_index) {
616 add_inode_backref(dst_cache, dst->ino, backref->dir,
617 backref->index, backref->name,
618 backref->namelen, backref->filetype,
619 BTRFS_DIR_INDEX_KEY, backref->errors);
621 if (backref->found_dir_item) {
623 add_inode_backref(dst_cache, dst->ino,
624 backref->dir, 0, backref->name,
625 backref->namelen, backref->filetype,
626 BTRFS_DIR_ITEM_KEY, backref->errors);
628 if (backref->found_inode_ref) {
629 add_inode_backref(dst_cache, dst->ino,
630 backref->dir, backref->index,
631 backref->name, backref->namelen, 0,
632 backref->ref_type, backref->errors);
636 if (src->found_dir_item)
637 dst->found_dir_item = 1;
638 if (src->found_file_extent)
639 dst->found_file_extent = 1;
640 if (src->found_csum_item)
641 dst->found_csum_item = 1;
642 if (src->some_csum_missing)
643 dst->some_csum_missing = 1;
644 if (dst->first_extent_gap > src->first_extent_gap)
645 dst->first_extent_gap = src->first_extent_gap;
647 BUG_ON(src->found_link < dir_count);
648 dst->found_link += src->found_link - dir_count;
649 dst->found_size += src->found_size;
650 if (src->extent_start != (u64)-1) {
651 if (dst->extent_start == (u64)-1) {
652 dst->extent_start = src->extent_start;
653 dst->extent_end = src->extent_end;
655 if (dst->extent_end > src->extent_start)
656 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
657 else if (dst->extent_end < src->extent_start &&
658 dst->extent_end < dst->first_extent_gap)
659 dst->first_extent_gap = dst->extent_end;
660 if (dst->extent_end < src->extent_end)
661 dst->extent_end = src->extent_end;
665 dst->errors |= src->errors;
666 if (src->found_inode_item) {
667 if (!dst->found_inode_item) {
668 dst->nlink = src->nlink;
669 dst->isize = src->isize;
670 dst->nbytes = src->nbytes;
671 dst->imode = src->imode;
672 dst->nodatasum = src->nodatasum;
673 dst->found_inode_item = 1;
675 dst->errors |= I_ERR_DUP_INODE_ITEM;
683 static int splice_shared_node(struct shared_node *src_node,
684 struct shared_node *dst_node)
686 struct cache_extent *cache;
687 struct ptr_node *node, *ins;
688 struct cache_tree *src, *dst;
689 struct inode_record *rec, *conflict;
694 if (--src_node->refs == 0)
696 if (src_node->current)
697 current_ino = src_node->current->ino;
699 src = &src_node->root_cache;
700 dst = &dst_node->root_cache;
702 cache = search_cache_extent(src, 0);
704 node = container_of(cache, struct ptr_node, cache);
706 cache = next_cache_extent(cache);
709 remove_cache_extent(src, &node->cache);
712 ins = malloc(sizeof(*ins));
713 ins->cache.start = node->cache.start;
714 ins->cache.size = node->cache.size;
718 ret = insert_cache_extent(dst, &ins->cache);
719 if (ret == -EEXIST) {
720 conflict = get_inode_rec(dst, rec->ino, 1);
721 merge_inode_recs(rec, conflict, dst);
723 conflict->checked = 1;
724 if (dst_node->current == conflict)
725 dst_node->current = NULL;
727 maybe_free_inode_rec(dst, conflict);
735 if (src == &src_node->root_cache) {
736 src = &src_node->inode_cache;
737 dst = &dst_node->inode_cache;
741 if (current_ino > 0 && (!dst_node->current ||
742 current_ino > dst_node->current->ino)) {
743 if (dst_node->current) {
744 dst_node->current->checked = 1;
745 maybe_free_inode_rec(dst, dst_node->current);
747 dst_node->current = get_inode_rec(dst, current_ino, 1);
752 static void free_inode_ptr(struct cache_extent *cache)
754 struct ptr_node *node;
755 struct inode_record *rec;
757 node = container_of(cache, struct ptr_node, cache);
763 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
765 static struct shared_node *find_shared_node(struct cache_tree *shared,
768 struct cache_extent *cache;
769 struct shared_node *node;
771 cache = lookup_cache_extent(shared, bytenr, 1);
773 node = container_of(cache, struct shared_node, cache);
779 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
782 struct shared_node *node;
784 node = calloc(1, sizeof(*node));
785 node->cache.start = bytenr;
786 node->cache.size = 1;
787 cache_tree_init(&node->root_cache);
788 cache_tree_init(&node->inode_cache);
791 ret = insert_cache_extent(shared, &node->cache);
796 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
797 struct walk_control *wc, int level)
799 struct shared_node *node;
800 struct shared_node *dest;
802 if (level == wc->active_node)
805 BUG_ON(wc->active_node <= level);
806 node = find_shared_node(&wc->shared, bytenr);
808 add_shared_node(&wc->shared, bytenr, refs);
809 node = find_shared_node(&wc->shared, bytenr);
810 wc->nodes[level] = node;
811 wc->active_node = level;
815 if (wc->root_level == wc->active_node &&
816 btrfs_root_refs(&root->root_item) == 0) {
817 if (--node->refs == 0) {
818 free_inode_recs_tree(&node->root_cache);
819 free_inode_recs_tree(&node->inode_cache);
820 remove_cache_extent(&wc->shared, &node->cache);
826 dest = wc->nodes[wc->active_node];
827 splice_shared_node(node, dest);
828 if (node->refs == 0) {
829 remove_cache_extent(&wc->shared, &node->cache);
835 static int leave_shared_node(struct btrfs_root *root,
836 struct walk_control *wc, int level)
838 struct shared_node *node;
839 struct shared_node *dest;
842 if (level == wc->root_level)
845 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
849 BUG_ON(i >= BTRFS_MAX_LEVEL);
851 node = wc->nodes[wc->active_node];
852 wc->nodes[wc->active_node] = NULL;
855 dest = wc->nodes[wc->active_node];
856 if (wc->active_node < wc->root_level ||
857 btrfs_root_refs(&root->root_item) > 0) {
858 BUG_ON(node->refs <= 1);
859 splice_shared_node(node, dest);
861 BUG_ON(node->refs < 2);
867 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
870 struct btrfs_path path;
871 struct btrfs_key key;
872 struct extent_buffer *leaf;
876 btrfs_init_path(&path);
878 key.objectid = parent_root_id;
879 key.type = BTRFS_ROOT_REF_KEY;
880 key.offset = child_root_id;
881 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
884 btrfs_release_path(&path);
888 key.objectid = child_root_id;
889 key.type = BTRFS_ROOT_BACKREF_KEY;
891 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
896 leaf = path.nodes[0];
897 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
898 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
903 leaf = path.nodes[0];
906 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
907 if (key.objectid != child_root_id ||
908 key.type != BTRFS_ROOT_BACKREF_KEY)
913 if (key.offset == parent_root_id) {
914 btrfs_release_path(&path);
921 btrfs_release_path(&path);
922 return has_parent? 0 : -1;
925 static int process_dir_item(struct btrfs_root *root,
926 struct extent_buffer *eb,
927 int slot, struct btrfs_key *key,
928 struct shared_node *active_node)
938 struct btrfs_dir_item *di;
939 struct inode_record *rec;
940 struct cache_tree *root_cache;
941 struct cache_tree *inode_cache;
942 struct btrfs_key location;
943 char namebuf[BTRFS_NAME_LEN];
945 root_cache = &active_node->root_cache;
946 inode_cache = &active_node->inode_cache;
947 rec = active_node->current;
948 rec->found_dir_item = 1;
950 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
951 total = btrfs_item_size_nr(eb, slot);
952 while (cur < total) {
954 btrfs_dir_item_key_to_cpu(eb, di, &location);
955 name_len = btrfs_dir_name_len(eb, di);
956 data_len = btrfs_dir_data_len(eb, di);
957 filetype = btrfs_dir_type(eb, di);
959 rec->found_size += name_len;
960 if (name_len <= BTRFS_NAME_LEN) {
964 len = BTRFS_NAME_LEN;
965 error = REF_ERR_NAME_TOO_LONG;
967 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
969 if (location.type == BTRFS_INODE_ITEM_KEY) {
970 add_inode_backref(inode_cache, location.objectid,
971 key->objectid, key->offset, namebuf,
972 len, filetype, key->type, error);
973 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
974 add_inode_backref(root_cache, location.objectid,
975 key->objectid, key->offset,
976 namebuf, len, filetype,
979 fprintf(stderr, "warning line %d\n", __LINE__);
982 len = sizeof(*di) + name_len + data_len;
983 di = (struct btrfs_dir_item *)((char *)di + len);
986 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
987 rec->errors |= I_ERR_DUP_DIR_INDEX;
992 static int process_inode_ref(struct extent_buffer *eb,
993 int slot, struct btrfs_key *key,
994 struct shared_node *active_node)
1002 struct cache_tree *inode_cache;
1003 struct btrfs_inode_ref *ref;
1004 char namebuf[BTRFS_NAME_LEN];
1006 inode_cache = &active_node->inode_cache;
1008 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1009 total = btrfs_item_size_nr(eb, slot);
1010 while (cur < total) {
1011 name_len = btrfs_inode_ref_name_len(eb, ref);
1012 index = btrfs_inode_ref_index(eb, ref);
1013 if (name_len <= BTRFS_NAME_LEN) {
1017 len = BTRFS_NAME_LEN;
1018 error = REF_ERR_NAME_TOO_LONG;
1020 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1021 add_inode_backref(inode_cache, key->objectid, key->offset,
1022 index, namebuf, len, 0, key->type, error);
1024 len = sizeof(*ref) + name_len;
1025 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1031 static int process_inode_extref(struct extent_buffer *eb,
1032 int slot, struct btrfs_key *key,
1033 struct shared_node *active_node)
1042 struct cache_tree *inode_cache;
1043 struct btrfs_inode_extref *extref;
1044 char namebuf[BTRFS_NAME_LEN];
1046 inode_cache = &active_node->inode_cache;
1048 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1049 total = btrfs_item_size_nr(eb, slot);
1050 while (cur < total) {
1051 name_len = btrfs_inode_extref_name_len(eb, extref);
1052 index = btrfs_inode_extref_index(eb, extref);
1053 parent = btrfs_inode_extref_parent(eb, extref);
1054 if (name_len <= BTRFS_NAME_LEN) {
1058 len = BTRFS_NAME_LEN;
1059 error = REF_ERR_NAME_TOO_LONG;
1061 read_extent_buffer(eb, namebuf,
1062 (unsigned long)(extref + 1), len);
1063 add_inode_backref(inode_cache, key->objectid, parent,
1064 index, namebuf, len, 0, key->type, error);
1066 len = sizeof(*extref) + name_len;
1067 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1074 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
1076 struct btrfs_key key;
1077 struct btrfs_path path;
1078 struct extent_buffer *leaf;
1083 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1085 btrfs_init_path(&path);
1087 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1089 key.type = BTRFS_EXTENT_CSUM_KEY;
1091 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1094 if (ret > 0 && path.slots[0] > 0) {
1095 leaf = path.nodes[0];
1096 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1097 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1098 key.type == BTRFS_EXTENT_CSUM_KEY)
1103 leaf = path.nodes[0];
1104 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1105 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1109 leaf = path.nodes[0];
1112 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1113 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1114 key.type != BTRFS_EXTENT_CSUM_KEY)
1117 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1118 if (key.offset >= start + len)
1121 if (key.offset > start)
1124 size = btrfs_item_size_nr(leaf, path.slots[0]);
1125 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1126 if (csum_end > start) {
1127 size = min(csum_end - start, len);
1135 btrfs_release_path(&path);
1139 static int process_file_extent(struct btrfs_root *root,
1140 struct extent_buffer *eb,
1141 int slot, struct btrfs_key *key,
1142 struct shared_node *active_node)
1144 struct inode_record *rec;
1145 struct btrfs_file_extent_item *fi;
1147 u64 disk_bytenr = 0;
1148 u64 extent_offset = 0;
1149 u64 mask = root->sectorsize - 1;
1152 rec = active_node->current;
1153 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1154 rec->found_file_extent = 1;
1156 if (rec->extent_start == (u64)-1) {
1157 rec->extent_start = key->offset;
1158 rec->extent_end = key->offset;
1161 if (rec->extent_end > key->offset)
1162 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1163 else if (rec->extent_end < key->offset &&
1164 rec->extent_end < rec->first_extent_gap)
1165 rec->first_extent_gap = rec->extent_end;
1167 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1168 extent_type = btrfs_file_extent_type(eb, fi);
1170 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1171 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1173 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1174 rec->found_size += num_bytes;
1175 num_bytes = (num_bytes + mask) & ~mask;
1176 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1177 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1178 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1179 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1180 extent_offset = btrfs_file_extent_offset(eb, fi);
1181 if (num_bytes == 0 || (num_bytes & mask))
1182 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1183 if (num_bytes + extent_offset >
1184 btrfs_file_extent_ram_bytes(eb, fi))
1185 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1186 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1187 (btrfs_file_extent_compression(eb, fi) ||
1188 btrfs_file_extent_encryption(eb, fi) ||
1189 btrfs_file_extent_other_encoding(eb, fi)))
1190 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1191 if (disk_bytenr > 0)
1192 rec->found_size += num_bytes;
1194 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1196 rec->extent_end = key->offset + num_bytes;
1198 if (disk_bytenr > 0) {
1200 if (btrfs_file_extent_compression(eb, fi))
1201 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1203 disk_bytenr += extent_offset;
1205 found = count_csum_range(root, disk_bytenr, num_bytes);
1206 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1208 rec->found_csum_item = 1;
1209 if (found < num_bytes)
1210 rec->some_csum_missing = 1;
1211 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1213 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1219 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1220 struct walk_control *wc)
1222 struct btrfs_key key;
1227 struct cache_tree *inode_cache;
1228 struct shared_node *active_node;
1230 if (wc->root_level == wc->active_node &&
1231 btrfs_root_refs(&root->root_item) == 0)
1234 active_node = wc->nodes[wc->active_node];
1235 inode_cache = &active_node->inode_cache;
1236 nritems = btrfs_header_nritems(eb);
1237 for (i = 0; i < nritems; i++) {
1238 btrfs_item_key_to_cpu(eb, &key, i);
1240 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1242 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1245 if (active_node->current == NULL ||
1246 active_node->current->ino < key.objectid) {
1247 if (active_node->current) {
1248 active_node->current->checked = 1;
1249 maybe_free_inode_rec(inode_cache,
1250 active_node->current);
1252 active_node->current = get_inode_rec(inode_cache,
1256 case BTRFS_DIR_ITEM_KEY:
1257 case BTRFS_DIR_INDEX_KEY:
1258 ret = process_dir_item(root, eb, i, &key, active_node);
1260 case BTRFS_INODE_REF_KEY:
1261 ret = process_inode_ref(eb, i, &key, active_node);
1263 case BTRFS_INODE_EXTREF_KEY:
1264 ret = process_inode_extref(eb, i, &key, active_node);
1266 case BTRFS_INODE_ITEM_KEY:
1267 ret = process_inode_item(eb, i, &key, active_node);
1269 case BTRFS_EXTENT_DATA_KEY:
1270 ret = process_file_extent(root, eb, i, &key,
1282 static void reada_walk_down(struct btrfs_root *root,
1283 struct extent_buffer *node, int slot)
1293 level = btrfs_header_level(node);
1297 nritems = btrfs_header_nritems(node);
1298 blocksize = btrfs_level_size(root, level - 1);
1299 for (i = slot; i < nritems; i++) {
1300 bytenr = btrfs_node_blockptr(node, i);
1301 ptr_gen = btrfs_node_ptr_generation(node, i);
1302 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1308 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1309 struct walk_control *wc, int *level)
1313 struct extent_buffer *next;
1314 struct extent_buffer *cur;
1319 WARN_ON(*level < 0);
1320 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1321 ret = btrfs_lookup_extent_info(NULL, root,
1322 path->nodes[*level]->start,
1323 *level, 1, &refs, NULL);
1330 ret = enter_shared_node(root, path->nodes[*level]->start,
1338 while (*level >= 0) {
1339 WARN_ON(*level < 0);
1340 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1341 cur = path->nodes[*level];
1343 if (btrfs_header_level(cur) != *level)
1346 if (path->slots[*level] >= btrfs_header_nritems(cur))
1349 ret = process_one_leaf(root, cur, wc);
1352 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1353 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1354 blocksize = btrfs_level_size(root, *level - 1);
1355 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1361 ret = enter_shared_node(root, bytenr, refs,
1364 path->slots[*level]++;
1369 next = btrfs_find_tree_block(root, bytenr, blocksize);
1370 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1371 free_extent_buffer(next);
1372 reada_walk_down(root, cur, path->slots[*level]);
1373 next = read_tree_block(root, bytenr, blocksize,
1381 *level = *level - 1;
1382 free_extent_buffer(path->nodes[*level]);
1383 path->nodes[*level] = next;
1384 path->slots[*level] = 0;
1387 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1391 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1392 struct walk_control *wc, int *level)
1395 struct extent_buffer *leaf;
1397 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1398 leaf = path->nodes[i];
1399 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1404 free_extent_buffer(path->nodes[*level]);
1405 path->nodes[*level] = NULL;
1406 BUG_ON(*level > wc->active_node);
1407 if (*level == wc->active_node)
1408 leave_shared_node(root, wc, *level);
1415 static int check_root_dir(struct inode_record *rec)
1417 struct inode_backref *backref;
1420 if (!rec->found_inode_item || rec->errors)
1422 if (rec->nlink != 1 || rec->found_link != 0)
1424 if (list_empty(&rec->backrefs))
1426 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1427 if (!backref->found_inode_ref)
1429 if (backref->index != 0 || backref->namelen != 2 ||
1430 memcmp(backref->name, "..", 2))
1432 if (backref->found_dir_index || backref->found_dir_item)
1439 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root, struct btrfs_path *path,
1441 struct inode_record *rec)
1443 struct btrfs_inode_item *ei;
1444 struct btrfs_key key;
1447 key.objectid = rec->ino;
1448 key.type = BTRFS_INODE_ITEM_KEY;
1449 key.offset = (u64)-1;
1451 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1455 if (!path->slots[0]) {
1462 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1463 if (key.objectid != rec->ino) {
1468 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1469 struct btrfs_inode_item);
1470 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1471 btrfs_mark_buffer_dirty(path->nodes[0]);
1472 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1473 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1474 root->root_key.objectid);
1476 btrfs_release_path(path);
1480 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1481 struct btrfs_root *root,
1482 struct btrfs_path *path,
1483 struct inode_record *rec)
1485 struct btrfs_key key;
1488 key.objectid = BTRFS_ORPHAN_OBJECTID;
1489 key.type = BTRFS_ORPHAN_ITEM_KEY;
1490 key.offset = rec->ino;
1492 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1493 btrfs_release_path(path);
1495 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1499 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1501 struct btrfs_trans_handle *trans;
1502 struct btrfs_path *path;
1505 /* So far we just fix dir isize wrong */
1506 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1509 path = btrfs_alloc_path();
1513 trans = btrfs_start_transaction(root, 1);
1514 if (IS_ERR(trans)) {
1515 btrfs_free_path(path);
1516 return PTR_ERR(trans);
1519 if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
1520 ret = repair_inode_isize(trans, root, path, rec);
1521 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
1522 ret = repair_inode_orphan_item(trans, root, path, rec);
1523 btrfs_commit_transaction(trans, root);
1524 btrfs_free_path(path);
1528 static int check_inode_recs(struct btrfs_root *root,
1529 struct cache_tree *inode_cache)
1531 struct cache_extent *cache;
1532 struct ptr_node *node;
1533 struct inode_record *rec;
1534 struct inode_backref *backref;
1537 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1539 if (btrfs_root_refs(&root->root_item) == 0) {
1540 if (!cache_tree_empty(inode_cache))
1541 fprintf(stderr, "warning line %d\n", __LINE__);
1545 rec = get_inode_rec(inode_cache, root_dirid, 0);
1547 ret = check_root_dir(rec);
1549 fprintf(stderr, "root %llu root dir %llu error\n",
1550 (unsigned long long)root->root_key.objectid,
1551 (unsigned long long)root_dirid);
1555 fprintf(stderr, "root %llu root dir %llu not found\n",
1556 (unsigned long long)root->root_key.objectid,
1557 (unsigned long long)root_dirid);
1561 cache = search_cache_extent(inode_cache, 0);
1564 node = container_of(cache, struct ptr_node, cache);
1566 remove_cache_extent(inode_cache, &node->cache);
1568 if (rec->ino == root_dirid ||
1569 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1570 free_inode_rec(rec);
1574 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1575 ret = check_orphan_item(root, rec->ino);
1577 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1578 if (can_free_inode_rec(rec)) {
1579 free_inode_rec(rec);
1585 ret = try_repair_inode(root, rec);
1586 if (ret == 0 && can_free_inode_rec(rec)) {
1587 free_inode_rec(rec);
1594 if (!rec->found_inode_item)
1595 rec->errors |= I_ERR_NO_INODE_ITEM;
1596 if (rec->found_link != rec->nlink)
1597 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1598 fprintf(stderr, "root %llu inode %llu errors %x",
1599 (unsigned long long) root->root_key.objectid,
1600 (unsigned long long) rec->ino, rec->errors);
1601 print_inode_error(rec->errors);
1602 list_for_each_entry(backref, &rec->backrefs, list) {
1603 if (!backref->found_dir_item)
1604 backref->errors |= REF_ERR_NO_DIR_ITEM;
1605 if (!backref->found_dir_index)
1606 backref->errors |= REF_ERR_NO_DIR_INDEX;
1607 if (!backref->found_inode_ref)
1608 backref->errors |= REF_ERR_NO_INODE_REF;
1609 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1610 " namelen %u name %s filetype %d errors %x",
1611 (unsigned long long)backref->dir,
1612 (unsigned long long)backref->index,
1613 backref->namelen, backref->name,
1614 backref->filetype, backref->errors);
1615 print_ref_error(backref->errors);
1617 free_inode_rec(rec);
1619 return (error > 0) ? -1 : 0;
1622 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1625 struct cache_extent *cache;
1626 struct root_record *rec = NULL;
1629 cache = lookup_cache_extent(root_cache, objectid, 1);
1631 rec = container_of(cache, struct root_record, cache);
1633 rec = calloc(1, sizeof(*rec));
1634 rec->objectid = objectid;
1635 INIT_LIST_HEAD(&rec->backrefs);
1636 rec->cache.start = objectid;
1637 rec->cache.size = 1;
1639 ret = insert_cache_extent(root_cache, &rec->cache);
1645 static struct root_backref *get_root_backref(struct root_record *rec,
1646 u64 ref_root, u64 dir, u64 index,
1647 const char *name, int namelen)
1649 struct root_backref *backref;
1651 list_for_each_entry(backref, &rec->backrefs, list) {
1652 if (backref->ref_root != ref_root || backref->dir != dir ||
1653 backref->namelen != namelen)
1655 if (memcmp(name, backref->name, namelen))
1660 backref = malloc(sizeof(*backref) + namelen + 1);
1661 memset(backref, 0, sizeof(*backref));
1662 backref->ref_root = ref_root;
1664 backref->index = index;
1665 backref->namelen = namelen;
1666 memcpy(backref->name, name, namelen);
1667 backref->name[namelen] = '\0';
1668 list_add_tail(&backref->list, &rec->backrefs);
1672 static void free_root_record(struct cache_extent *cache)
1674 struct root_record *rec;
1675 struct root_backref *backref;
1677 rec = container_of(cache, struct root_record, cache);
1678 while (!list_empty(&rec->backrefs)) {
1679 backref = list_entry(rec->backrefs.next,
1680 struct root_backref, list);
1681 list_del(&backref->list);
1688 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
1690 static int add_root_backref(struct cache_tree *root_cache,
1691 u64 root_id, u64 ref_root, u64 dir, u64 index,
1692 const char *name, int namelen,
1693 int item_type, int errors)
1695 struct root_record *rec;
1696 struct root_backref *backref;
1698 rec = get_root_rec(root_cache, root_id);
1699 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1701 backref->errors |= errors;
1703 if (item_type != BTRFS_DIR_ITEM_KEY) {
1704 if (backref->found_dir_index || backref->found_back_ref ||
1705 backref->found_forward_ref) {
1706 if (backref->index != index)
1707 backref->errors |= REF_ERR_INDEX_UNMATCH;
1709 backref->index = index;
1713 if (item_type == BTRFS_DIR_ITEM_KEY) {
1714 if (backref->found_forward_ref)
1716 backref->found_dir_item = 1;
1717 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1718 backref->found_dir_index = 1;
1719 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1720 if (backref->found_forward_ref)
1721 backref->errors |= REF_ERR_DUP_ROOT_REF;
1722 else if (backref->found_dir_item)
1724 backref->found_forward_ref = 1;
1725 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1726 if (backref->found_back_ref)
1727 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1728 backref->found_back_ref = 1;
1733 if (backref->found_forward_ref && backref->found_dir_item)
1734 backref->reachable = 1;
1738 static int merge_root_recs(struct btrfs_root *root,
1739 struct cache_tree *src_cache,
1740 struct cache_tree *dst_cache)
1742 struct cache_extent *cache;
1743 struct ptr_node *node;
1744 struct inode_record *rec;
1745 struct inode_backref *backref;
1747 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1748 free_inode_recs_tree(src_cache);
1753 cache = search_cache_extent(src_cache, 0);
1756 node = container_of(cache, struct ptr_node, cache);
1758 remove_cache_extent(src_cache, &node->cache);
1761 if (!is_child_root(root, root->objectid, rec->ino))
1764 list_for_each_entry(backref, &rec->backrefs, list) {
1765 BUG_ON(backref->found_inode_ref);
1766 if (backref->found_dir_item)
1767 add_root_backref(dst_cache, rec->ino,
1768 root->root_key.objectid, backref->dir,
1769 backref->index, backref->name,
1770 backref->namelen, BTRFS_DIR_ITEM_KEY,
1772 if (backref->found_dir_index)
1773 add_root_backref(dst_cache, rec->ino,
1774 root->root_key.objectid, backref->dir,
1775 backref->index, backref->name,
1776 backref->namelen, BTRFS_DIR_INDEX_KEY,
1780 free_inode_rec(rec);
1785 static int check_root_refs(struct btrfs_root *root,
1786 struct cache_tree *root_cache)
1788 struct root_record *rec;
1789 struct root_record *ref_root;
1790 struct root_backref *backref;
1791 struct cache_extent *cache;
1797 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1800 /* fixme: this can not detect circular references */
1803 cache = search_cache_extent(root_cache, 0);
1807 rec = container_of(cache, struct root_record, cache);
1808 cache = next_cache_extent(cache);
1810 if (rec->found_ref == 0)
1813 list_for_each_entry(backref, &rec->backrefs, list) {
1814 if (!backref->reachable)
1817 ref_root = get_root_rec(root_cache,
1819 if (ref_root->found_ref > 0)
1822 backref->reachable = 0;
1824 if (rec->found_ref == 0)
1830 cache = search_cache_extent(root_cache, 0);
1834 rec = container_of(cache, struct root_record, cache);
1835 cache = next_cache_extent(cache);
1837 if (rec->found_ref == 0 &&
1838 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1839 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1840 ret = check_orphan_item(root->fs_info->tree_root,
1846 * If we don't have a root item then we likely just have
1847 * a dir item in a snapshot for this root but no actual
1848 * ref key or anything so it's meaningless.
1850 if (!rec->found_root_item)
1853 fprintf(stderr, "fs tree %llu not referenced\n",
1854 (unsigned long long)rec->objectid);
1858 if (rec->found_ref > 0 && !rec->found_root_item)
1860 list_for_each_entry(backref, &rec->backrefs, list) {
1861 if (!backref->found_dir_item)
1862 backref->errors |= REF_ERR_NO_DIR_ITEM;
1863 if (!backref->found_dir_index)
1864 backref->errors |= REF_ERR_NO_DIR_INDEX;
1865 if (!backref->found_back_ref)
1866 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1867 if (!backref->found_forward_ref)
1868 backref->errors |= REF_ERR_NO_ROOT_REF;
1869 if (backref->reachable && backref->errors)
1876 fprintf(stderr, "fs tree %llu refs %u %s\n",
1877 (unsigned long long)rec->objectid, rec->found_ref,
1878 rec->found_root_item ? "" : "not found");
1880 list_for_each_entry(backref, &rec->backrefs, list) {
1881 if (!backref->reachable)
1883 if (!backref->errors && rec->found_root_item)
1885 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1886 " index %llu namelen %u name %s errors %x\n",
1887 (unsigned long long)backref->ref_root,
1888 (unsigned long long)backref->dir,
1889 (unsigned long long)backref->index,
1890 backref->namelen, backref->name,
1892 print_ref_error(backref->errors);
1895 return errors > 0 ? 1 : 0;
1898 static int process_root_ref(struct extent_buffer *eb, int slot,
1899 struct btrfs_key *key,
1900 struct cache_tree *root_cache)
1906 struct btrfs_root_ref *ref;
1907 char namebuf[BTRFS_NAME_LEN];
1910 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1912 dirid = btrfs_root_ref_dirid(eb, ref);
1913 index = btrfs_root_ref_sequence(eb, ref);
1914 name_len = btrfs_root_ref_name_len(eb, ref);
1916 if (name_len <= BTRFS_NAME_LEN) {
1920 len = BTRFS_NAME_LEN;
1921 error = REF_ERR_NAME_TOO_LONG;
1923 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1925 if (key->type == BTRFS_ROOT_REF_KEY) {
1926 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1927 index, namebuf, len, key->type, error);
1929 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1930 index, namebuf, len, key->type, error);
1935 static int check_fs_root(struct btrfs_root *root,
1936 struct cache_tree *root_cache,
1937 struct walk_control *wc)
1942 struct btrfs_path path;
1943 struct shared_node root_node;
1944 struct root_record *rec;
1945 struct btrfs_root_item *root_item = &root->root_item;
1947 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1948 rec = get_root_rec(root_cache, root->root_key.objectid);
1949 if (btrfs_root_refs(root_item) > 0)
1950 rec->found_root_item = 1;
1953 btrfs_init_path(&path);
1954 memset(&root_node, 0, sizeof(root_node));
1955 cache_tree_init(&root_node.root_cache);
1956 cache_tree_init(&root_node.inode_cache);
1958 level = btrfs_header_level(root->node);
1959 memset(wc->nodes, 0, sizeof(wc->nodes));
1960 wc->nodes[level] = &root_node;
1961 wc->active_node = level;
1962 wc->root_level = level;
1964 if (btrfs_root_refs(root_item) > 0 ||
1965 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1966 path.nodes[level] = root->node;
1967 extent_buffer_get(root->node);
1968 path.slots[level] = 0;
1970 struct btrfs_key key;
1971 struct btrfs_disk_key found_key;
1973 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1974 level = root_item->drop_level;
1975 path.lowest_level = level;
1976 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1978 btrfs_node_key(path.nodes[level], &found_key,
1980 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1981 sizeof(found_key)));
1985 wret = walk_down_tree(root, &path, wc, &level);
1991 wret = walk_up_tree(root, &path, wc, &level);
1997 btrfs_release_path(&path);
1999 merge_root_recs(root, &root_node.root_cache, root_cache);
2001 if (root_node.current) {
2002 root_node.current->checked = 1;
2003 maybe_free_inode_rec(&root_node.inode_cache,
2007 ret = check_inode_recs(root, &root_node.inode_cache);
2011 static int fs_root_objectid(u64 objectid)
2013 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
2014 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2016 return is_fstree(objectid);
2019 static int check_fs_roots(struct btrfs_root *root,
2020 struct cache_tree *root_cache)
2022 struct btrfs_path path;
2023 struct btrfs_key key;
2024 struct walk_control wc;
2025 struct extent_buffer *leaf;
2026 struct btrfs_root *tmp_root;
2027 struct btrfs_root *tree_root = root->fs_info->tree_root;
2032 * Just in case we made any changes to the extent tree that weren't
2033 * reflected into the free space cache yet.
2036 reset_cached_block_groups(root->fs_info);
2037 memset(&wc, 0, sizeof(wc));
2038 cache_tree_init(&wc.shared);
2039 btrfs_init_path(&path);
2043 key.type = BTRFS_ROOT_ITEM_KEY;
2044 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2047 leaf = path.nodes[0];
2048 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2049 ret = btrfs_next_leaf(tree_root, &path);
2052 leaf = path.nodes[0];
2054 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2055 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2056 fs_root_objectid(key.objectid)) {
2057 key.offset = (u64)-1;
2058 tmp_root = btrfs_read_fs_root(root->fs_info, &key);
2059 if (IS_ERR(tmp_root)) {
2063 ret = check_fs_root(tmp_root, root_cache, &wc);
2066 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2067 key.type == BTRFS_ROOT_BACKREF_KEY) {
2068 process_root_ref(leaf, path.slots[0], &key,
2074 btrfs_release_path(&path);
2076 if (!cache_tree_empty(&wc.shared))
2077 fprintf(stderr, "warning line %d\n", __LINE__);
2082 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2084 struct list_head *cur = rec->backrefs.next;
2085 struct extent_backref *back;
2086 struct tree_backref *tback;
2087 struct data_backref *dback;
2091 while(cur != &rec->backrefs) {
2092 back = list_entry(cur, struct extent_backref, list);
2094 if (!back->found_extent_tree) {
2098 if (back->is_data) {
2099 dback = (struct data_backref *)back;
2100 fprintf(stderr, "Backref %llu %s %llu"
2101 " owner %llu offset %llu num_refs %lu"
2102 " not found in extent tree\n",
2103 (unsigned long long)rec->start,
2104 back->full_backref ?
2106 back->full_backref ?
2107 (unsigned long long)dback->parent:
2108 (unsigned long long)dback->root,
2109 (unsigned long long)dback->owner,
2110 (unsigned long long)dback->offset,
2111 (unsigned long)dback->num_refs);
2113 tback = (struct tree_backref *)back;
2114 fprintf(stderr, "Backref %llu parent %llu"
2115 " root %llu not found in extent tree\n",
2116 (unsigned long long)rec->start,
2117 (unsigned long long)tback->parent,
2118 (unsigned long long)tback->root);
2121 if (!back->is_data && !back->found_ref) {
2125 tback = (struct tree_backref *)back;
2126 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2127 (unsigned long long)rec->start,
2128 back->full_backref ? "parent" : "root",
2129 back->full_backref ?
2130 (unsigned long long)tback->parent :
2131 (unsigned long long)tback->root, back);
2133 if (back->is_data) {
2134 dback = (struct data_backref *)back;
2135 if (dback->found_ref != dback->num_refs) {
2139 fprintf(stderr, "Incorrect local backref count"
2140 " on %llu %s %llu owner %llu"
2141 " offset %llu found %u wanted %u back %p\n",
2142 (unsigned long long)rec->start,
2143 back->full_backref ?
2145 back->full_backref ?
2146 (unsigned long long)dback->parent:
2147 (unsigned long long)dback->root,
2148 (unsigned long long)dback->owner,
2149 (unsigned long long)dback->offset,
2150 dback->found_ref, dback->num_refs, back);
2152 if (dback->disk_bytenr != rec->start) {
2156 fprintf(stderr, "Backref disk bytenr does not"
2157 " match extent record, bytenr=%llu, "
2158 "ref bytenr=%llu\n",
2159 (unsigned long long)rec->start,
2160 (unsigned long long)dback->disk_bytenr);
2163 if (dback->bytes != rec->nr) {
2167 fprintf(stderr, "Backref bytes do not match "
2168 "extent backref, bytenr=%llu, ref "
2169 "bytes=%llu, backref bytes=%llu\n",
2170 (unsigned long long)rec->start,
2171 (unsigned long long)rec->nr,
2172 (unsigned long long)dback->bytes);
2175 if (!back->is_data) {
2178 dback = (struct data_backref *)back;
2179 found += dback->found_ref;
2182 if (found != rec->refs) {
2186 fprintf(stderr, "Incorrect global backref count "
2187 "on %llu found %llu wanted %llu\n",
2188 (unsigned long long)rec->start,
2189 (unsigned long long)found,
2190 (unsigned long long)rec->refs);
2196 static int free_all_extent_backrefs(struct extent_record *rec)
2198 struct extent_backref *back;
2199 struct list_head *cur;
2200 while (!list_empty(&rec->backrefs)) {
2201 cur = rec->backrefs.next;
2202 back = list_entry(cur, struct extent_backref, list);
2209 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2210 struct cache_tree *extent_cache)
2212 struct cache_extent *cache;
2213 struct extent_record *rec;
2216 cache = first_cache_extent(extent_cache);
2219 rec = container_of(cache, struct extent_record, cache);
2220 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2221 remove_cache_extent(extent_cache, cache);
2222 free_all_extent_backrefs(rec);
2227 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2228 struct extent_record *rec)
2230 if (rec->content_checked && rec->owner_ref_checked &&
2231 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2232 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2233 remove_cache_extent(extent_cache, &rec->cache);
2234 free_all_extent_backrefs(rec);
2235 list_del_init(&rec->list);
2241 static int check_owner_ref(struct btrfs_root *root,
2242 struct extent_record *rec,
2243 struct extent_buffer *buf)
2245 struct extent_backref *node;
2246 struct tree_backref *back;
2247 struct btrfs_root *ref_root;
2248 struct btrfs_key key;
2249 struct btrfs_path path;
2250 struct extent_buffer *parent;
2255 list_for_each_entry(node, &rec->backrefs, list) {
2258 if (!node->found_ref)
2260 if (node->full_backref)
2262 back = (struct tree_backref *)node;
2263 if (btrfs_header_owner(buf) == back->root)
2266 BUG_ON(rec->is_root);
2268 /* try to find the block by search corresponding fs tree */
2269 key.objectid = btrfs_header_owner(buf);
2270 key.type = BTRFS_ROOT_ITEM_KEY;
2271 key.offset = (u64)-1;
2273 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2274 if (IS_ERR(ref_root))
2277 level = btrfs_header_level(buf);
2279 btrfs_item_key_to_cpu(buf, &key, 0);
2281 btrfs_node_key_to_cpu(buf, &key, 0);
2283 btrfs_init_path(&path);
2284 path.lowest_level = level + 1;
2285 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2289 parent = path.nodes[level + 1];
2290 if (parent && buf->start == btrfs_node_blockptr(parent,
2291 path.slots[level + 1]))
2294 btrfs_release_path(&path);
2295 return found ? 0 : 1;
2298 static int is_extent_tree_record(struct extent_record *rec)
2300 struct list_head *cur = rec->backrefs.next;
2301 struct extent_backref *node;
2302 struct tree_backref *back;
2305 while(cur != &rec->backrefs) {
2306 node = list_entry(cur, struct extent_backref, list);
2310 back = (struct tree_backref *)node;
2311 if (node->full_backref)
2313 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2320 static int record_bad_block_io(struct btrfs_fs_info *info,
2321 struct cache_tree *extent_cache,
2324 struct extent_record *rec;
2325 struct cache_extent *cache;
2326 struct btrfs_key key;
2328 cache = lookup_cache_extent(extent_cache, start, len);
2332 rec = container_of(cache, struct extent_record, cache);
2333 if (!is_extent_tree_record(rec))
2336 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2337 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2340 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2341 struct extent_buffer *buf, int slot)
2343 if (btrfs_header_level(buf)) {
2344 struct btrfs_key_ptr ptr1, ptr2;
2346 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2347 sizeof(struct btrfs_key_ptr));
2348 read_extent_buffer(buf, &ptr2,
2349 btrfs_node_key_ptr_offset(slot + 1),
2350 sizeof(struct btrfs_key_ptr));
2351 write_extent_buffer(buf, &ptr1,
2352 btrfs_node_key_ptr_offset(slot + 1),
2353 sizeof(struct btrfs_key_ptr));
2354 write_extent_buffer(buf, &ptr2,
2355 btrfs_node_key_ptr_offset(slot),
2356 sizeof(struct btrfs_key_ptr));
2358 struct btrfs_disk_key key;
2359 btrfs_node_key(buf, &key, 0);
2360 btrfs_fixup_low_keys(root, path, &key,
2361 btrfs_header_level(buf) + 1);
2364 struct btrfs_item *item1, *item2;
2365 struct btrfs_key k1, k2;
2366 char *item1_data, *item2_data;
2367 u32 item1_offset, item2_offset, item1_size, item2_size;
2369 item1 = btrfs_item_nr(slot);
2370 item2 = btrfs_item_nr(slot + 1);
2371 btrfs_item_key_to_cpu(buf, &k1, slot);
2372 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2373 item1_offset = btrfs_item_offset(buf, item1);
2374 item2_offset = btrfs_item_offset(buf, item2);
2375 item1_size = btrfs_item_size(buf, item1);
2376 item2_size = btrfs_item_size(buf, item2);
2378 item1_data = malloc(item1_size);
2381 item2_data = malloc(item2_size);
2387 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2388 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2390 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2391 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2395 btrfs_set_item_offset(buf, item1, item2_offset);
2396 btrfs_set_item_offset(buf, item2, item1_offset);
2397 btrfs_set_item_size(buf, item1, item2_size);
2398 btrfs_set_item_size(buf, item2, item1_size);
2400 path->slots[0] = slot;
2401 btrfs_set_item_key_unsafe(root, path, &k2);
2402 path->slots[0] = slot + 1;
2403 btrfs_set_item_key_unsafe(root, path, &k1);
2409 * Attempt to fix basic block failures. Currently we only handle bad key
2410 * orders, we will cycle through the keys and swap them if necessary.
2412 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
2413 struct btrfs_root *root,
2414 struct extent_buffer *buf,
2415 struct btrfs_disk_key *parent_key,
2416 enum btrfs_tree_block_status status)
2418 struct btrfs_path *path;
2419 struct btrfs_key k1, k2;
2424 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
2427 k1.objectid = btrfs_header_owner(buf);
2428 k1.type = BTRFS_ROOT_ITEM_KEY;
2429 k1.offset = (u64)-1;
2431 root = btrfs_read_fs_root(root->fs_info, &k1);
2435 path = btrfs_alloc_path();
2439 level = btrfs_header_level(buf);
2440 path->lowest_level = level;
2441 path->skip_check_block = 1;
2443 btrfs_node_key_to_cpu(buf, &k1, 0);
2445 btrfs_item_key_to_cpu(buf, &k1, 0);
2447 ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
2449 btrfs_free_path(path);
2453 buf = path->nodes[level];
2454 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2456 btrfs_node_key_to_cpu(buf, &k1, i);
2457 btrfs_node_key_to_cpu(buf, &k2, i + 1);
2459 btrfs_item_key_to_cpu(buf, &k1, i);
2460 btrfs_item_key_to_cpu(buf, &k2, i + 1);
2462 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2464 ret = swap_values(root, path, buf, i);
2467 btrfs_mark_buffer_dirty(buf);
2471 btrfs_free_path(path);
2475 static int check_block(struct btrfs_trans_handle *trans,
2476 struct btrfs_root *root,
2477 struct cache_tree *extent_cache,
2478 struct extent_buffer *buf, u64 flags)
2480 struct extent_record *rec;
2481 struct cache_extent *cache;
2482 struct btrfs_key key;
2483 enum btrfs_tree_block_status status;
2487 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2490 rec = container_of(cache, struct extent_record, cache);
2491 rec->generation = btrfs_header_generation(buf);
2493 level = btrfs_header_level(buf);
2494 if (btrfs_header_nritems(buf) > 0) {
2497 btrfs_item_key_to_cpu(buf, &key, 0);
2499 btrfs_node_key_to_cpu(buf, &key, 0);
2501 rec->info_objectid = key.objectid;
2503 rec->info_level = level;
2505 if (btrfs_is_leaf(buf))
2506 status = btrfs_check_leaf(root, &rec->parent_key, buf);
2508 status = btrfs_check_node(root, &rec->parent_key, buf);
2510 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2512 status = try_to_fix_bad_block(trans, root, buf,
2515 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2517 fprintf(stderr, "bad block %llu\n",
2518 (unsigned long long)buf->start);
2521 * Signal to callers we need to start the scan over
2522 * again since we'll have cow'ed blocks.
2527 rec->content_checked = 1;
2528 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2529 rec->owner_ref_checked = 1;
2531 ret = check_owner_ref(root, rec, buf);
2533 rec->owner_ref_checked = 1;
2537 maybe_free_extent_rec(extent_cache, rec);
2541 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2542 u64 parent, u64 root)
2544 struct list_head *cur = rec->backrefs.next;
2545 struct extent_backref *node;
2546 struct tree_backref *back;
2548 while(cur != &rec->backrefs) {
2549 node = list_entry(cur, struct extent_backref, list);
2553 back = (struct tree_backref *)node;
2555 if (!node->full_backref)
2557 if (parent == back->parent)
2560 if (node->full_backref)
2562 if (back->root == root)
2569 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2570 u64 parent, u64 root)
2572 struct tree_backref *ref = malloc(sizeof(*ref));
2573 memset(&ref->node, 0, sizeof(ref->node));
2575 ref->parent = parent;
2576 ref->node.full_backref = 1;
2579 ref->node.full_backref = 0;
2581 list_add_tail(&ref->node.list, &rec->backrefs);
2586 static struct data_backref *find_data_backref(struct extent_record *rec,
2587 u64 parent, u64 root,
2588 u64 owner, u64 offset,
2590 u64 disk_bytenr, u64 bytes)
2592 struct list_head *cur = rec->backrefs.next;
2593 struct extent_backref *node;
2594 struct data_backref *back;
2596 while(cur != &rec->backrefs) {
2597 node = list_entry(cur, struct extent_backref, list);
2601 back = (struct data_backref *)node;
2603 if (!node->full_backref)
2605 if (parent == back->parent)
2608 if (node->full_backref)
2610 if (back->root == root && back->owner == owner &&
2611 back->offset == offset) {
2612 if (found_ref && node->found_ref &&
2613 (back->bytes != bytes ||
2614 back->disk_bytenr != disk_bytenr))
2623 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2624 u64 parent, u64 root,
2625 u64 owner, u64 offset,
2628 struct data_backref *ref = malloc(sizeof(*ref));
2629 memset(&ref->node, 0, sizeof(ref->node));
2630 ref->node.is_data = 1;
2633 ref->parent = parent;
2636 ref->node.full_backref = 1;
2640 ref->offset = offset;
2641 ref->node.full_backref = 0;
2643 ref->bytes = max_size;
2646 list_add_tail(&ref->node.list, &rec->backrefs);
2647 if (max_size > rec->max_size)
2648 rec->max_size = max_size;
2652 static int add_extent_rec(struct cache_tree *extent_cache,
2653 struct btrfs_key *parent_key, u64 parent_gen,
2654 u64 start, u64 nr, u64 extent_item_refs,
2655 int is_root, int inc_ref, int set_checked,
2656 int metadata, int extent_rec, u64 max_size)
2658 struct extent_record *rec;
2659 struct cache_extent *cache;
2663 cache = lookup_cache_extent(extent_cache, start, nr);
2665 rec = container_of(cache, struct extent_record, cache);
2669 rec->nr = max(nr, max_size);
2672 * We need to make sure to reset nr to whatever the extent
2673 * record says was the real size, this way we can compare it to
2677 if (start != rec->start || rec->found_rec) {
2678 struct extent_record *tmp;
2681 if (list_empty(&rec->list))
2682 list_add_tail(&rec->list,
2683 &duplicate_extents);
2686 * We have to do this song and dance in case we
2687 * find an extent record that falls inside of
2688 * our current extent record but does not have
2689 * the same objectid.
2691 tmp = malloc(sizeof(*tmp));
2695 tmp->max_size = max_size;
2698 tmp->metadata = metadata;
2699 tmp->extent_item_refs = extent_item_refs;
2700 INIT_LIST_HEAD(&tmp->list);
2701 list_add_tail(&tmp->list, &rec->dups);
2702 rec->num_duplicates++;
2709 if (extent_item_refs && !dup) {
2710 if (rec->extent_item_refs) {
2711 fprintf(stderr, "block %llu rec "
2712 "extent_item_refs %llu, passed %llu\n",
2713 (unsigned long long)start,
2714 (unsigned long long)
2715 rec->extent_item_refs,
2716 (unsigned long long)extent_item_refs);
2718 rec->extent_item_refs = extent_item_refs;
2723 rec->content_checked = 1;
2724 rec->owner_ref_checked = 1;
2728 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2730 rec->parent_generation = parent_gen;
2732 if (rec->max_size < max_size)
2733 rec->max_size = max_size;
2735 maybe_free_extent_rec(extent_cache, rec);
2738 rec = malloc(sizeof(*rec));
2740 rec->max_size = max_size;
2741 rec->nr = max(nr, max_size);
2742 rec->found_rec = !!extent_rec;
2743 rec->content_checked = 0;
2744 rec->owner_ref_checked = 0;
2745 rec->num_duplicates = 0;
2746 rec->metadata = metadata;
2747 INIT_LIST_HEAD(&rec->backrefs);
2748 INIT_LIST_HEAD(&rec->dups);
2749 INIT_LIST_HEAD(&rec->list);
2761 if (extent_item_refs)
2762 rec->extent_item_refs = extent_item_refs;
2764 rec->extent_item_refs = 0;
2767 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2769 memset(&rec->parent_key, 0, sizeof(*parent_key));
2772 rec->parent_generation = parent_gen;
2774 rec->parent_generation = 0;
2776 rec->cache.start = start;
2777 rec->cache.size = nr;
2778 ret = insert_cache_extent(extent_cache, &rec->cache);
2782 rec->content_checked = 1;
2783 rec->owner_ref_checked = 1;
2788 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2789 u64 parent, u64 root, int found_ref)
2791 struct extent_record *rec;
2792 struct tree_backref *back;
2793 struct cache_extent *cache;
2795 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2797 add_extent_rec(extent_cache, NULL, 0, bytenr,
2798 1, 0, 0, 0, 0, 1, 0, 0);
2799 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2804 rec = container_of(cache, struct extent_record, cache);
2805 if (rec->start != bytenr) {
2809 back = find_tree_backref(rec, parent, root);
2811 back = alloc_tree_backref(rec, parent, root);
2814 if (back->node.found_ref) {
2815 fprintf(stderr, "Extent back ref already exists "
2816 "for %llu parent %llu root %llu \n",
2817 (unsigned long long)bytenr,
2818 (unsigned long long)parent,
2819 (unsigned long long)root);
2821 back->node.found_ref = 1;
2823 if (back->node.found_extent_tree) {
2824 fprintf(stderr, "Extent back ref already exists "
2825 "for %llu parent %llu root %llu \n",
2826 (unsigned long long)bytenr,
2827 (unsigned long long)parent,
2828 (unsigned long long)root);
2830 back->node.found_extent_tree = 1;
2835 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2836 u64 parent, u64 root, u64 owner, u64 offset,
2837 u32 num_refs, int found_ref, u64 max_size)
2839 struct extent_record *rec;
2840 struct data_backref *back;
2841 struct cache_extent *cache;
2843 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2845 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
2847 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2852 rec = container_of(cache, struct extent_record, cache);
2853 if (rec->max_size < max_size)
2854 rec->max_size = max_size;
2857 * If found_ref is set then max_size is the real size and must match the
2858 * existing refs. So if we have already found a ref then we need to
2859 * make sure that this ref matches the existing one, otherwise we need
2860 * to add a new backref so we can notice that the backrefs don't match
2861 * and we need to figure out who is telling the truth. This is to
2862 * account for that awful fsync bug I introduced where we'd end up with
2863 * a btrfs_file_extent_item that would have its length include multiple
2864 * prealloc extents or point inside of a prealloc extent.
2866 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2869 back = alloc_data_backref(rec, parent, root, owner, offset,
2873 BUG_ON(num_refs != 1);
2874 if (back->node.found_ref)
2875 BUG_ON(back->bytes != max_size);
2876 back->node.found_ref = 1;
2877 back->found_ref += 1;
2878 back->bytes = max_size;
2879 back->disk_bytenr = bytenr;
2881 rec->content_checked = 1;
2882 rec->owner_ref_checked = 1;
2884 if (back->node.found_extent_tree) {
2885 fprintf(stderr, "Extent back ref already exists "
2886 "for %llu parent %llu root %llu "
2887 "owner %llu offset %llu num_refs %lu\n",
2888 (unsigned long long)bytenr,
2889 (unsigned long long)parent,
2890 (unsigned long long)root,
2891 (unsigned long long)owner,
2892 (unsigned long long)offset,
2893 (unsigned long)num_refs);
2895 back->num_refs = num_refs;
2896 back->node.found_extent_tree = 1;
2901 static int add_pending(struct cache_tree *pending,
2902 struct cache_tree *seen, u64 bytenr, u32 size)
2905 ret = add_cache_extent(seen, bytenr, size);
2908 add_cache_extent(pending, bytenr, size);
2912 static int pick_next_pending(struct cache_tree *pending,
2913 struct cache_tree *reada,
2914 struct cache_tree *nodes,
2915 u64 last, struct block_info *bits, int bits_nr,
2918 unsigned long node_start = last;
2919 struct cache_extent *cache;
2922 cache = search_cache_extent(reada, 0);
2924 bits[0].start = cache->start;
2925 bits[0].size = cache->size;
2930 if (node_start > 32768)
2931 node_start -= 32768;
2933 cache = search_cache_extent(nodes, node_start);
2935 cache = search_cache_extent(nodes, 0);
2938 cache = search_cache_extent(pending, 0);
2943 bits[ret].start = cache->start;
2944 bits[ret].size = cache->size;
2945 cache = next_cache_extent(cache);
2947 } while (cache && ret < bits_nr);
2953 bits[ret].start = cache->start;
2954 bits[ret].size = cache->size;
2955 cache = next_cache_extent(cache);
2957 } while (cache && ret < bits_nr);
2959 if (bits_nr - ret > 8) {
2960 u64 lookup = bits[0].start + bits[0].size;
2961 struct cache_extent *next;
2962 next = search_cache_extent(pending, lookup);
2964 if (next->start - lookup > 32768)
2966 bits[ret].start = next->start;
2967 bits[ret].size = next->size;
2968 lookup = next->start + next->size;
2972 next = next_cache_extent(next);
2980 static void free_chunk_record(struct cache_extent *cache)
2982 struct chunk_record *rec;
2984 rec = container_of(cache, struct chunk_record, cache);
2988 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
2990 cache_tree_free_extents(chunk_cache, free_chunk_record);
2993 static void free_device_record(struct rb_node *node)
2995 struct device_record *rec;
2997 rec = container_of(node, struct device_record, node);
3001 FREE_RB_BASED_TREE(device_cache, free_device_record);
3003 int insert_block_group_record(struct block_group_tree *tree,
3004 struct block_group_record *bg_rec)
3008 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3012 list_add_tail(&bg_rec->list, &tree->block_groups);
3016 static void free_block_group_record(struct cache_extent *cache)
3018 struct block_group_record *rec;
3020 rec = container_of(cache, struct block_group_record, cache);
3024 void free_block_group_tree(struct block_group_tree *tree)
3026 cache_tree_free_extents(&tree->tree, free_block_group_record);
3029 int insert_device_extent_record(struct device_extent_tree *tree,
3030 struct device_extent_record *de_rec)
3035 * Device extent is a bit different from the other extents, because
3036 * the extents which belong to the different devices may have the
3037 * same start and size, so we need use the special extent cache
3038 * search/insert functions.
3040 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3044 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3045 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3049 static void free_device_extent_record(struct cache_extent *cache)
3051 struct device_extent_record *rec;
3053 rec = container_of(cache, struct device_extent_record, cache);
3057 void free_device_extent_tree(struct device_extent_tree *tree)
3059 cache_tree_free_extents(&tree->tree, free_device_extent_record);
3062 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3063 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3064 struct extent_buffer *leaf, int slot)
3066 struct btrfs_extent_ref_v0 *ref0;
3067 struct btrfs_key key;
3069 btrfs_item_key_to_cpu(leaf, &key, slot);
3070 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3071 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3072 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3074 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3075 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3081 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3082 struct btrfs_key *key,
3085 struct btrfs_chunk *ptr;
3086 struct chunk_record *rec;
3089 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3090 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3092 rec = malloc(btrfs_chunk_record_size(num_stripes));
3094 fprintf(stderr, "memory allocation failed\n");
3098 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3100 INIT_LIST_HEAD(&rec->list);
3101 INIT_LIST_HEAD(&rec->dextents);
3104 rec->cache.start = key->offset;
3105 rec->cache.size = btrfs_chunk_length(leaf, ptr);
3107 rec->generation = btrfs_header_generation(leaf);
3109 rec->objectid = key->objectid;
3110 rec->type = key->type;
3111 rec->offset = key->offset;
3113 rec->length = rec->cache.size;
3114 rec->owner = btrfs_chunk_owner(leaf, ptr);
3115 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3116 rec->type_flags = btrfs_chunk_type(leaf, ptr);
3117 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3118 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3119 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3120 rec->num_stripes = num_stripes;
3121 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3123 for (i = 0; i < rec->num_stripes; ++i) {
3124 rec->stripes[i].devid =
3125 btrfs_stripe_devid_nr(leaf, ptr, i);
3126 rec->stripes[i].offset =
3127 btrfs_stripe_offset_nr(leaf, ptr, i);
3128 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3129 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3136 static int process_chunk_item(struct cache_tree *chunk_cache,
3137 struct btrfs_key *key, struct extent_buffer *eb,
3140 struct chunk_record *rec;
3143 rec = btrfs_new_chunk_record(eb, key, slot);
3144 ret = insert_cache_extent(chunk_cache, &rec->cache);
3146 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3147 rec->offset, rec->length);
3154 static int process_device_item(struct rb_root *dev_cache,
3155 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3157 struct btrfs_dev_item *ptr;
3158 struct device_record *rec;
3161 ptr = btrfs_item_ptr(eb,
3162 slot, struct btrfs_dev_item);
3164 rec = malloc(sizeof(*rec));
3166 fprintf(stderr, "memory allocation failed\n");
3170 rec->devid = key->offset;
3171 rec->generation = btrfs_header_generation(eb);
3173 rec->objectid = key->objectid;
3174 rec->type = key->type;
3175 rec->offset = key->offset;
3177 rec->devid = btrfs_device_id(eb, ptr);
3178 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3179 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3181 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3183 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3190 struct block_group_record *
3191 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3194 struct btrfs_block_group_item *ptr;
3195 struct block_group_record *rec;
3197 rec = malloc(sizeof(*rec));
3199 fprintf(stderr, "memory allocation failed\n");
3202 memset(rec, 0, sizeof(*rec));
3204 rec->cache.start = key->objectid;
3205 rec->cache.size = key->offset;
3207 rec->generation = btrfs_header_generation(leaf);
3209 rec->objectid = key->objectid;
3210 rec->type = key->type;
3211 rec->offset = key->offset;
3213 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3214 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3216 INIT_LIST_HEAD(&rec->list);
3221 static int process_block_group_item(struct block_group_tree *block_group_cache,
3222 struct btrfs_key *key,
3223 struct extent_buffer *eb, int slot)
3225 struct block_group_record *rec;
3228 rec = btrfs_new_block_group_record(eb, key, slot);
3229 ret = insert_block_group_record(block_group_cache, rec);
3231 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3232 rec->objectid, rec->offset);
3239 struct device_extent_record *
3240 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3241 struct btrfs_key *key, int slot)
3243 struct device_extent_record *rec;
3244 struct btrfs_dev_extent *ptr;
3246 rec = malloc(sizeof(*rec));
3248 fprintf(stderr, "memory allocation failed\n");
3251 memset(rec, 0, sizeof(*rec));
3253 rec->cache.objectid = key->objectid;
3254 rec->cache.start = key->offset;
3256 rec->generation = btrfs_header_generation(leaf);
3258 rec->objectid = key->objectid;
3259 rec->type = key->type;
3260 rec->offset = key->offset;
3262 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3263 rec->chunk_objecteid =
3264 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3266 btrfs_dev_extent_chunk_offset(leaf, ptr);
3267 rec->length = btrfs_dev_extent_length(leaf, ptr);
3268 rec->cache.size = rec->length;
3270 INIT_LIST_HEAD(&rec->chunk_list);
3271 INIT_LIST_HEAD(&rec->device_list);
3277 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3278 struct btrfs_key *key, struct extent_buffer *eb,
3281 struct device_extent_record *rec;
3284 rec = btrfs_new_device_extent_record(eb, key, slot);
3285 ret = insert_device_extent_record(dev_extent_cache, rec);
3288 "Device extent[%llu, %llu, %llu] existed.\n",
3289 rec->objectid, rec->offset, rec->length);
3296 static int process_extent_item(struct btrfs_root *root,
3297 struct cache_tree *extent_cache,
3298 struct extent_buffer *eb, int slot)
3300 struct btrfs_extent_item *ei;
3301 struct btrfs_extent_inline_ref *iref;
3302 struct btrfs_extent_data_ref *dref;
3303 struct btrfs_shared_data_ref *sref;
3304 struct btrfs_key key;
3308 u32 item_size = btrfs_item_size_nr(eb, slot);
3314 btrfs_item_key_to_cpu(eb, &key, slot);
3316 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3318 num_bytes = root->leafsize;
3320 num_bytes = key.offset;
3323 if (item_size < sizeof(*ei)) {
3324 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3325 struct btrfs_extent_item_v0 *ei0;
3326 BUG_ON(item_size != sizeof(*ei0));
3327 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3328 refs = btrfs_extent_refs_v0(eb, ei0);
3332 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3333 num_bytes, refs, 0, 0, 0, metadata, 1,
3337 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3338 refs = btrfs_extent_refs(eb, ei);
3340 add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3341 refs, 0, 0, 0, metadata, 1, num_bytes);
3343 ptr = (unsigned long)(ei + 1);
3344 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3345 key.type == BTRFS_EXTENT_ITEM_KEY)
3346 ptr += sizeof(struct btrfs_tree_block_info);
3348 end = (unsigned long)ei + item_size;
3350 iref = (struct btrfs_extent_inline_ref *)ptr;
3351 type = btrfs_extent_inline_ref_type(eb, iref);
3352 offset = btrfs_extent_inline_ref_offset(eb, iref);
3354 case BTRFS_TREE_BLOCK_REF_KEY:
3355 add_tree_backref(extent_cache, key.objectid,
3358 case BTRFS_SHARED_BLOCK_REF_KEY:
3359 add_tree_backref(extent_cache, key.objectid,
3362 case BTRFS_EXTENT_DATA_REF_KEY:
3363 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3364 add_data_backref(extent_cache, key.objectid, 0,
3365 btrfs_extent_data_ref_root(eb, dref),
3366 btrfs_extent_data_ref_objectid(eb,
3368 btrfs_extent_data_ref_offset(eb, dref),
3369 btrfs_extent_data_ref_count(eb, dref),
3372 case BTRFS_SHARED_DATA_REF_KEY:
3373 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3374 add_data_backref(extent_cache, key.objectid, offset,
3376 btrfs_shared_data_ref_count(eb, sref),
3380 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3381 key.objectid, key.type, num_bytes);
3384 ptr += btrfs_extent_inline_ref_size(type);
3391 static int check_cache_range(struct btrfs_root *root,
3392 struct btrfs_block_group_cache *cache,
3393 u64 offset, u64 bytes)
3395 struct btrfs_free_space *entry;
3401 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3402 bytenr = btrfs_sb_offset(i);
3403 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3404 cache->key.objectid, bytenr, 0,
3405 &logical, &nr, &stripe_len);
3410 if (logical[nr] + stripe_len <= offset)
3412 if (offset + bytes <= logical[nr])
3414 if (logical[nr] == offset) {
3415 if (stripe_len >= bytes) {
3419 bytes -= stripe_len;
3420 offset += stripe_len;
3421 } else if (logical[nr] < offset) {
3422 if (logical[nr] + stripe_len >=
3427 bytes = (offset + bytes) -
3428 (logical[nr] + stripe_len);
3429 offset = logical[nr] + stripe_len;
3432 * Could be tricky, the super may land in the
3433 * middle of the area we're checking. First
3434 * check the easiest case, it's at the end.
3436 if (logical[nr] + stripe_len >=
3438 bytes = logical[nr] - offset;
3442 /* Check the left side */
3443 ret = check_cache_range(root, cache,
3445 logical[nr] - offset);
3451 /* Now we continue with the right side */
3452 bytes = (offset + bytes) -
3453 (logical[nr] + stripe_len);
3454 offset = logical[nr] + stripe_len;
3461 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3463 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3464 offset, offset+bytes);
3468 if (entry->offset != offset) {
3469 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3474 if (entry->bytes != bytes) {
3475 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3476 bytes, entry->bytes, offset);
3480 unlink_free_space(cache->free_space_ctl, entry);
3485 static int verify_space_cache(struct btrfs_root *root,
3486 struct btrfs_block_group_cache *cache)
3488 struct btrfs_path *path;
3489 struct extent_buffer *leaf;
3490 struct btrfs_key key;
3494 path = btrfs_alloc_path();
3498 root = root->fs_info->extent_root;
3500 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
3502 key.objectid = last;
3504 key.type = BTRFS_EXTENT_ITEM_KEY;
3506 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3511 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3512 ret = btrfs_next_leaf(root, path);
3520 leaf = path->nodes[0];
3521 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3522 if (key.objectid >= cache->key.offset + cache->key.objectid)
3524 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3525 key.type != BTRFS_METADATA_ITEM_KEY) {
3530 if (last == key.objectid) {
3531 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3532 last = key.objectid + key.offset;
3534 last = key.objectid + root->leafsize;
3539 ret = check_cache_range(root, cache, last,
3540 key.objectid - last);
3543 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3544 last = key.objectid + key.offset;
3546 last = key.objectid + root->leafsize;
3550 if (last < cache->key.objectid + cache->key.offset)
3551 ret = check_cache_range(root, cache, last,
3552 cache->key.objectid +
3553 cache->key.offset - last);
3556 btrfs_free_path(path);
3559 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
3560 fprintf(stderr, "There are still entries left in the space "
3568 static int check_space_cache(struct btrfs_root *root)
3570 struct btrfs_block_group_cache *cache;
3571 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
3575 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
3576 btrfs_super_generation(root->fs_info->super_copy) !=
3577 btrfs_super_cache_generation(root->fs_info->super_copy)) {
3578 printf("cache and super generation don't match, space cache "
3579 "will be invalidated\n");
3584 cache = btrfs_lookup_first_block_group(root->fs_info, start);
3588 start = cache->key.objectid + cache->key.offset;
3589 if (!cache->free_space_ctl) {
3590 if (btrfs_init_free_space_ctl(cache,
3591 root->sectorsize)) {
3596 btrfs_remove_free_space_cache(cache);
3599 ret = load_free_space_cache(root->fs_info, cache);
3603 ret = verify_space_cache(root, cache);
3605 fprintf(stderr, "cache appears valid but isnt %Lu\n",
3606 cache->key.objectid);
3611 return error ? -EINVAL : 0;
3614 static int read_extent_data(struct btrfs_root *root, char *data,
3615 u64 logical, u64 *len, int mirror)
3618 struct btrfs_multi_bio *multi = NULL;
3619 struct btrfs_fs_info *info = root->fs_info;
3620 struct btrfs_device *device;
3624 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
3625 &multi, mirror, NULL);
3627 fprintf(stderr, "Couldn't map the block %llu\n",
3631 device = multi->stripes[0].dev;
3633 if (device->fd == 0)
3638 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
3648 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
3649 u64 num_bytes, unsigned long leaf_offset,
3650 struct extent_buffer *eb) {
3653 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3655 unsigned long csum_offset;
3659 u64 data_checked = 0;
3665 if (num_bytes % root->sectorsize)
3668 data = malloc(num_bytes);
3672 while (offset < num_bytes) {
3675 read_len = num_bytes - offset;
3676 /* read as much space once a time */
3677 ret = read_extent_data(root, data + offset,
3678 bytenr + offset, &read_len, mirror);
3682 /* verify every 4k data's checksum */
3683 while (data_checked < read_len) {
3685 tmp = offset + data_checked;
3687 csum = btrfs_csum_data(NULL, (char *)data + tmp,
3688 csum, root->sectorsize);
3689 btrfs_csum_final(csum, (char *)&csum);
3691 csum_offset = leaf_offset +
3692 tmp / root->sectorsize * csum_size;
3693 read_extent_buffer(eb, (char *)&csum_expected,
3694 csum_offset, csum_size);
3695 /* try another mirror */
3696 if (csum != csum_expected) {
3697 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
3698 mirror, bytenr + tmp,
3699 csum, csum_expected);
3700 num_copies = btrfs_num_copies(
3701 &root->fs_info->mapping_tree,
3703 if (mirror < num_copies - 1) {
3708 data_checked += root->sectorsize;
3717 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
3720 struct btrfs_path *path;
3721 struct extent_buffer *leaf;
3722 struct btrfs_key key;
3725 path = btrfs_alloc_path();
3727 fprintf(stderr, "Error allocing path\n");
3731 key.objectid = bytenr;
3732 key.type = BTRFS_EXTENT_ITEM_KEY;
3737 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
3740 fprintf(stderr, "Error looking up extent record %d\n", ret);
3741 btrfs_free_path(path);
3747 btrfs_prev_leaf(root, path);
3750 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3753 * Block group items come before extent items if they have the same
3754 * bytenr, so walk back one more just in case. Dear future traveler,
3755 * first congrats on mastering time travel. Now if it's not too much
3756 * trouble could you go back to 2006 and tell Chris to make the
3757 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
3759 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3763 btrfs_prev_leaf(root, path);
3767 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3768 ret = btrfs_next_leaf(root, path);
3770 fprintf(stderr, "Error going to next leaf "
3772 btrfs_free_path(path);
3778 leaf = path->nodes[0];
3779 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3780 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3784 if (key.objectid + key.offset < bytenr) {
3788 if (key.objectid > bytenr + num_bytes)
3791 if (key.objectid == bytenr) {
3792 if (key.offset >= num_bytes) {
3796 num_bytes -= key.offset;
3797 bytenr += key.offset;
3798 } else if (key.objectid < bytenr) {
3799 if (key.objectid + key.offset >= bytenr + num_bytes) {
3803 num_bytes = (bytenr + num_bytes) -
3804 (key.objectid + key.offset);
3805 bytenr = key.objectid + key.offset;
3807 if (key.objectid + key.offset < bytenr + num_bytes) {
3808 u64 new_start = key.objectid + key.offset;
3809 u64 new_bytes = bytenr + num_bytes - new_start;
3812 * Weird case, the extent is in the middle of
3813 * our range, we'll have to search one side
3814 * and then the other. Not sure if this happens
3815 * in real life, but no harm in coding it up
3816 * anyway just in case.
3818 btrfs_release_path(path);
3819 ret = check_extent_exists(root, new_start,
3822 fprintf(stderr, "Right section didn't "
3826 num_bytes = key.objectid - bytenr;
3829 num_bytes = key.objectid - bytenr;
3836 fprintf(stderr, "There are no extents for csum range "
3837 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3841 btrfs_free_path(path);
3845 static int check_csums(struct btrfs_root *root)
3847 struct btrfs_path *path;
3848 struct extent_buffer *leaf;
3849 struct btrfs_key key;
3850 u64 offset = 0, num_bytes = 0;
3851 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3855 unsigned long leaf_offset;
3857 root = root->fs_info->csum_root;
3859 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3860 key.type = BTRFS_EXTENT_CSUM_KEY;
3863 path = btrfs_alloc_path();
3867 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3869 fprintf(stderr, "Error searching csum tree %d\n", ret);
3870 btrfs_free_path(path);
3874 if (ret > 0 && path->slots[0])
3879 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3880 ret = btrfs_next_leaf(root, path);
3882 fprintf(stderr, "Error going to next leaf "
3889 leaf = path->nodes[0];
3891 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3892 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3897 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
3898 csum_size) * root->sectorsize;
3899 if (!check_data_csum)
3900 goto skip_csum_check;
3901 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
3902 ret = check_extent_csums(root, key.offset, data_len,
3908 offset = key.offset;
3909 } else if (key.offset != offset + num_bytes) {
3910 ret = check_extent_exists(root, offset, num_bytes);
3912 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3913 "there is no extent record\n",
3914 offset, offset+num_bytes);
3917 offset = key.offset;
3920 num_bytes += data_len;
3924 btrfs_free_path(path);
3928 static int is_dropped_key(struct btrfs_key *key,
3929 struct btrfs_key *drop_key) {
3930 if (key->objectid < drop_key->objectid)
3932 else if (key->objectid == drop_key->objectid) {
3933 if (key->type < drop_key->type)
3935 else if (key->type == drop_key->type) {
3936 if (key->offset < drop_key->offset)
3943 static int run_next_block(struct btrfs_trans_handle *trans,
3944 struct btrfs_root *root,
3945 struct block_info *bits,
3948 struct cache_tree *pending,
3949 struct cache_tree *seen,
3950 struct cache_tree *reada,
3951 struct cache_tree *nodes,
3952 struct cache_tree *extent_cache,
3953 struct cache_tree *chunk_cache,
3954 struct rb_root *dev_cache,
3955 struct block_group_tree *block_group_cache,
3956 struct device_extent_tree *dev_extent_cache,
3957 struct btrfs_root_item *ri)
3959 struct extent_buffer *buf;
3970 struct btrfs_key key;
3971 struct cache_extent *cache;
3974 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
3975 bits_nr, &reada_bits);
3980 for(i = 0; i < nritems; i++) {
3981 ret = add_cache_extent(reada, bits[i].start,
3986 /* fixme, get the parent transid */
3987 readahead_tree_block(root, bits[i].start,
3991 *last = bits[0].start;
3992 bytenr = bits[0].start;
3993 size = bits[0].size;
3995 cache = lookup_cache_extent(pending, bytenr, size);
3997 remove_cache_extent(pending, cache);
4000 cache = lookup_cache_extent(reada, bytenr, size);
4002 remove_cache_extent(reada, cache);
4005 cache = lookup_cache_extent(nodes, bytenr, size);
4007 remove_cache_extent(nodes, cache);
4010 cache = lookup_cache_extent(extent_cache, bytenr, size);
4012 struct extent_record *rec;
4014 rec = container_of(cache, struct extent_record, cache);
4015 gen = rec->parent_generation;
4018 /* fixme, get the real parent transid */
4019 buf = read_tree_block(root, bytenr, size, gen);
4020 if (!extent_buffer_uptodate(buf)) {
4021 record_bad_block_io(root->fs_info,
4022 extent_cache, bytenr, size);
4026 nritems = btrfs_header_nritems(buf);
4029 * FIXME, this only works only if we don't have any full
4032 if (!init_extent_tree) {
4033 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
4034 btrfs_header_level(buf), 1, NULL,
4042 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4047 owner = btrfs_header_owner(buf);
4050 ret = check_block(trans, root, extent_cache, buf, flags);
4054 if (btrfs_is_leaf(buf)) {
4055 btree_space_waste += btrfs_leaf_free_space(root, buf);
4056 for (i = 0; i < nritems; i++) {
4057 struct btrfs_file_extent_item *fi;
4058 btrfs_item_key_to_cpu(buf, &key, i);
4059 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
4060 process_extent_item(root, extent_cache, buf,
4064 if (key.type == BTRFS_METADATA_ITEM_KEY) {
4065 process_extent_item(root, extent_cache, buf,
4069 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
4071 btrfs_item_size_nr(buf, i);
4074 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4075 process_chunk_item(chunk_cache, &key, buf, i);
4078 if (key.type == BTRFS_DEV_ITEM_KEY) {
4079 process_device_item(dev_cache, &key, buf, i);
4082 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4083 process_block_group_item(block_group_cache,
4087 if (key.type == BTRFS_DEV_EXTENT_KEY) {
4088 process_device_extent_item(dev_extent_cache,
4093 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
4094 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4095 process_extent_ref_v0(extent_cache, buf, i);
4102 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
4103 add_tree_backref(extent_cache, key.objectid, 0,
4107 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
4108 add_tree_backref(extent_cache, key.objectid,
4112 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
4113 struct btrfs_extent_data_ref *ref;
4114 ref = btrfs_item_ptr(buf, i,
4115 struct btrfs_extent_data_ref);
4116 add_data_backref(extent_cache,
4118 btrfs_extent_data_ref_root(buf, ref),
4119 btrfs_extent_data_ref_objectid(buf,
4121 btrfs_extent_data_ref_offset(buf, ref),
4122 btrfs_extent_data_ref_count(buf, ref),
4123 0, root->sectorsize);
4126 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4127 struct btrfs_shared_data_ref *ref;
4128 ref = btrfs_item_ptr(buf, i,
4129 struct btrfs_shared_data_ref);
4130 add_data_backref(extent_cache,
4131 key.objectid, key.offset, 0, 0, 0,
4132 btrfs_shared_data_ref_count(buf, ref),
4133 0, root->sectorsize);
4136 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4137 struct bad_item *bad;
4139 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4143 bad = malloc(sizeof(struct bad_item));
4146 INIT_LIST_HEAD(&bad->list);
4147 memcpy(&bad->key, &key,
4148 sizeof(struct btrfs_key));
4149 bad->root_id = owner;
4150 list_add_tail(&bad->list, &delete_items);
4153 if (key.type != BTRFS_EXTENT_DATA_KEY)
4155 fi = btrfs_item_ptr(buf, i,
4156 struct btrfs_file_extent_item);
4157 if (btrfs_file_extent_type(buf, fi) ==
4158 BTRFS_FILE_EXTENT_INLINE)
4160 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4163 data_bytes_allocated +=
4164 btrfs_file_extent_disk_num_bytes(buf, fi);
4165 if (data_bytes_allocated < root->sectorsize) {
4168 data_bytes_referenced +=
4169 btrfs_file_extent_num_bytes(buf, fi);
4170 add_data_backref(extent_cache,
4171 btrfs_file_extent_disk_bytenr(buf, fi),
4172 parent, owner, key.objectid, key.offset -
4173 btrfs_file_extent_offset(buf, fi), 1, 1,
4174 btrfs_file_extent_disk_num_bytes(buf, fi));
4178 struct btrfs_key first_key;
4180 first_key.objectid = 0;
4183 btrfs_item_key_to_cpu(buf, &first_key, 0);
4184 level = btrfs_header_level(buf);
4185 for (i = 0; i < nritems; i++) {
4186 ptr = btrfs_node_blockptr(buf, i);
4187 size = btrfs_level_size(root, level - 1);
4188 btrfs_node_key_to_cpu(buf, &key, i);
4190 struct btrfs_key drop_key;
4191 btrfs_disk_key_to_cpu(&drop_key,
4192 &ri->drop_progress);
4193 if ((level == ri->drop_level)
4194 && is_dropped_key(&key, &drop_key)) {
4198 ret = add_extent_rec(extent_cache, &key,
4199 btrfs_node_ptr_generation(buf, i),
4200 ptr, size, 0, 0, 1, 0, 1, 0,
4204 add_tree_backref(extent_cache, ptr, parent, owner, 1);
4207 add_pending(nodes, seen, ptr, size);
4209 add_pending(pending, seen, ptr, size);
4212 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4213 nritems) * sizeof(struct btrfs_key_ptr);
4215 total_btree_bytes += buf->len;
4216 if (fs_root_objectid(btrfs_header_owner(buf)))
4217 total_fs_tree_bytes += buf->len;
4218 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4219 total_extent_tree_bytes += buf->len;
4220 if (!found_old_backref &&
4221 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4222 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4223 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4224 found_old_backref = 1;
4226 free_extent_buffer(buf);
4230 static int add_root_to_pending(struct extent_buffer *buf,
4231 struct cache_tree *extent_cache,
4232 struct cache_tree *pending,
4233 struct cache_tree *seen,
4234 struct cache_tree *nodes,
4235 struct btrfs_key *root_key)
4237 if (btrfs_header_level(buf) > 0)
4238 add_pending(nodes, seen, buf->start, buf->len);
4240 add_pending(pending, seen, buf->start, buf->len);
4241 add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4242 0, 1, 1, 0, 1, 0, buf->len);
4244 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4245 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4246 add_tree_backref(extent_cache, buf->start, buf->start,
4249 add_tree_backref(extent_cache, buf->start, 0,
4250 root_key->objectid, 1);
4254 /* as we fix the tree, we might be deleting blocks that
4255 * we're tracking for repair. This hook makes sure we
4256 * remove any backrefs for blocks as we are fixing them.
4258 static int free_extent_hook(struct btrfs_trans_handle *trans,
4259 struct btrfs_root *root,
4260 u64 bytenr, u64 num_bytes, u64 parent,
4261 u64 root_objectid, u64 owner, u64 offset,
4264 struct extent_record *rec;
4265 struct cache_extent *cache;
4267 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4269 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4270 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4274 rec = container_of(cache, struct extent_record, cache);
4276 struct data_backref *back;
4277 back = find_data_backref(rec, parent, root_objectid, owner,
4278 offset, 1, bytenr, num_bytes);
4281 if (back->node.found_ref) {
4282 back->found_ref -= refs_to_drop;
4284 rec->refs -= refs_to_drop;
4286 if (back->node.found_extent_tree) {
4287 back->num_refs -= refs_to_drop;
4288 if (rec->extent_item_refs)
4289 rec->extent_item_refs -= refs_to_drop;
4291 if (back->found_ref == 0)
4292 back->node.found_ref = 0;
4293 if (back->num_refs == 0)
4294 back->node.found_extent_tree = 0;
4296 if (!back->node.found_extent_tree && back->node.found_ref) {
4297 list_del(&back->node.list);
4301 struct tree_backref *back;
4302 back = find_tree_backref(rec, parent, root_objectid);
4305 if (back->node.found_ref) {
4308 back->node.found_ref = 0;
4310 if (back->node.found_extent_tree) {
4311 if (rec->extent_item_refs)
4312 rec->extent_item_refs--;
4313 back->node.found_extent_tree = 0;
4315 if (!back->node.found_extent_tree && back->node.found_ref) {
4316 list_del(&back->node.list);
4320 maybe_free_extent_rec(extent_cache, rec);
4325 static int delete_extent_records(struct btrfs_trans_handle *trans,
4326 struct btrfs_root *root,
4327 struct btrfs_path *path,
4328 u64 bytenr, u64 new_len)
4330 struct btrfs_key key;
4331 struct btrfs_key found_key;
4332 struct extent_buffer *leaf;
4337 key.objectid = bytenr;
4339 key.offset = (u64)-1;
4342 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
4349 if (path->slots[0] == 0)
4355 leaf = path->nodes[0];
4356 slot = path->slots[0];
4358 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4359 if (found_key.objectid != bytenr)
4362 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
4363 found_key.type != BTRFS_METADATA_ITEM_KEY &&
4364 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
4365 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
4366 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
4367 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
4368 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
4369 btrfs_release_path(path);
4370 if (found_key.type == 0) {
4371 if (found_key.offset == 0)
4373 key.offset = found_key.offset - 1;
4374 key.type = found_key.type;
4376 key.type = found_key.type - 1;
4377 key.offset = (u64)-1;
4381 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4382 found_key.objectid, found_key.type, found_key.offset);
4384 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4387 btrfs_release_path(path);
4389 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4390 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4391 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4392 found_key.offset : root->leafsize;
4394 ret = btrfs_update_block_group(trans, root, bytenr,
4401 btrfs_release_path(path);
4406 * for a single backref, this will allocate a new extent
4407 * and add the backref to it.
4409 static int record_extent(struct btrfs_trans_handle *trans,
4410 struct btrfs_fs_info *info,
4411 struct btrfs_path *path,
4412 struct extent_record *rec,
4413 struct extent_backref *back,
4414 int allocated, u64 flags)
4417 struct btrfs_root *extent_root = info->extent_root;
4418 struct extent_buffer *leaf;
4419 struct btrfs_key ins_key;
4420 struct btrfs_extent_item *ei;
4421 struct tree_backref *tback;
4422 struct data_backref *dback;
4423 struct btrfs_tree_block_info *bi;
4426 rec->max_size = max_t(u64, rec->max_size,
4427 info->extent_root->leafsize);
4430 u32 item_size = sizeof(*ei);
4433 item_size += sizeof(*bi);
4435 ins_key.objectid = rec->start;
4436 ins_key.offset = rec->max_size;
4437 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4439 ret = btrfs_insert_empty_item(trans, extent_root, path,
4440 &ins_key, item_size);
4444 leaf = path->nodes[0];
4445 ei = btrfs_item_ptr(leaf, path->slots[0],
4446 struct btrfs_extent_item);
4448 btrfs_set_extent_refs(leaf, ei, 0);
4449 btrfs_set_extent_generation(leaf, ei, rec->generation);
4451 if (back->is_data) {
4452 btrfs_set_extent_flags(leaf, ei,
4453 BTRFS_EXTENT_FLAG_DATA);
4455 struct btrfs_disk_key copy_key;;
4457 tback = (struct tree_backref *)back;
4458 bi = (struct btrfs_tree_block_info *)(ei + 1);
4459 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4462 btrfs_set_disk_key_objectid(©_key,
4463 rec->info_objectid);
4464 btrfs_set_disk_key_type(©_key, 0);
4465 btrfs_set_disk_key_offset(©_key, 0);
4467 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
4468 btrfs_set_tree_block_key(leaf, bi, ©_key);
4470 btrfs_set_extent_flags(leaf, ei,
4471 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
4474 btrfs_mark_buffer_dirty(leaf);
4475 ret = btrfs_update_block_group(trans, extent_root, rec->start,
4476 rec->max_size, 1, 0);
4479 btrfs_release_path(path);
4482 if (back->is_data) {
4486 dback = (struct data_backref *)back;
4487 if (back->full_backref)
4488 parent = dback->parent;
4492 for (i = 0; i < dback->found_ref; i++) {
4493 /* if parent != 0, we're doing a full backref
4494 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
4495 * just makes the backref allocator create a data
4498 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4499 rec->start, rec->max_size,
4503 BTRFS_FIRST_FREE_OBJECTID :
4509 fprintf(stderr, "adding new data backref"
4510 " on %llu %s %llu owner %llu"
4511 " offset %llu found %d\n",
4512 (unsigned long long)rec->start,
4513 back->full_backref ?
4515 back->full_backref ?
4516 (unsigned long long)parent :
4517 (unsigned long long)dback->root,
4518 (unsigned long long)dback->owner,
4519 (unsigned long long)dback->offset,
4524 tback = (struct tree_backref *)back;
4525 if (back->full_backref)
4526 parent = tback->parent;
4530 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4531 rec->start, rec->max_size,
4532 parent, tback->root, 0, 0);
4533 fprintf(stderr, "adding new tree backref on "
4534 "start %llu len %llu parent %llu root %llu\n",
4535 rec->start, rec->max_size, tback->parent, tback->root);
4540 btrfs_release_path(path);
4544 struct extent_entry {
4549 struct list_head list;
4552 static struct extent_entry *find_entry(struct list_head *entries,
4553 u64 bytenr, u64 bytes)
4555 struct extent_entry *entry = NULL;
4557 list_for_each_entry(entry, entries, list) {
4558 if (entry->bytenr == bytenr && entry->bytes == bytes)
4565 static struct extent_entry *find_most_right_entry(struct list_head *entries)
4567 struct extent_entry *entry, *best = NULL, *prev = NULL;
4569 list_for_each_entry(entry, entries, list) {
4576 * If there are as many broken entries as entries then we know
4577 * not to trust this particular entry.
4579 if (entry->broken == entry->count)
4583 * If our current entry == best then we can't be sure our best
4584 * is really the best, so we need to keep searching.
4586 if (best && best->count == entry->count) {
4592 /* Prev == entry, not good enough, have to keep searching */
4593 if (!prev->broken && prev->count == entry->count)
4597 best = (prev->count > entry->count) ? prev : entry;
4598 else if (best->count < entry->count)
4606 static int repair_ref(struct btrfs_trans_handle *trans,
4607 struct btrfs_fs_info *info, struct btrfs_path *path,
4608 struct data_backref *dback, struct extent_entry *entry)
4610 struct btrfs_root *root;
4611 struct btrfs_file_extent_item *fi;
4612 struct extent_buffer *leaf;
4613 struct btrfs_key key;
4617 key.objectid = dback->root;
4618 key.type = BTRFS_ROOT_ITEM_KEY;
4619 key.offset = (u64)-1;
4620 root = btrfs_read_fs_root(info, &key);
4622 fprintf(stderr, "Couldn't find root for our ref\n");
4627 * The backref points to the original offset of the extent if it was
4628 * split, so we need to search down to the offset we have and then walk
4629 * forward until we find the backref we're looking for.
4631 key.objectid = dback->owner;
4632 key.type = BTRFS_EXTENT_DATA_KEY;
4633 key.offset = dback->offset;
4634 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4636 fprintf(stderr, "Error looking up ref %d\n", ret);
4641 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4642 ret = btrfs_next_leaf(root, path);
4644 fprintf(stderr, "Couldn't find our ref, next\n");
4648 leaf = path->nodes[0];
4649 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4650 if (key.objectid != dback->owner ||
4651 key.type != BTRFS_EXTENT_DATA_KEY) {
4652 fprintf(stderr, "Couldn't find our ref, search\n");
4655 fi = btrfs_item_ptr(leaf, path->slots[0],
4656 struct btrfs_file_extent_item);
4657 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4658 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4660 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
4665 btrfs_release_path(path);
4668 * Have to make sure that this root gets updated when we commit the
4671 root->track_dirty = 1;
4672 if (root->last_trans != trans->transid) {
4673 root->last_trans = trans->transid;
4674 root->commit_root = root->node;
4675 extent_buffer_get(root->node);
4679 * Ok we have the key of the file extent we want to fix, now we can cow
4680 * down to the thing and fix it.
4682 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4684 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
4685 key.objectid, key.type, key.offset, ret);
4689 fprintf(stderr, "Well that's odd, we just found this key "
4690 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
4694 leaf = path->nodes[0];
4695 fi = btrfs_item_ptr(leaf, path->slots[0],
4696 struct btrfs_file_extent_item);
4698 if (btrfs_file_extent_compression(leaf, fi) &&
4699 dback->disk_bytenr != entry->bytenr) {
4700 fprintf(stderr, "Ref doesn't match the record start and is "
4701 "compressed, please take a btrfs-image of this file "
4702 "system and send it to a btrfs developer so they can "
4703 "complete this functionality for bytenr %Lu\n",
4704 dback->disk_bytenr);
4708 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
4709 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4710 } else if (dback->disk_bytenr > entry->bytenr) {
4711 u64 off_diff, offset;
4713 off_diff = dback->disk_bytenr - entry->bytenr;
4714 offset = btrfs_file_extent_offset(leaf, fi);
4715 if (dback->disk_bytenr + offset +
4716 btrfs_file_extent_num_bytes(leaf, fi) >
4717 entry->bytenr + entry->bytes) {
4718 fprintf(stderr, "Ref is past the entry end, please "
4719 "take a btrfs-image of this file system and "
4720 "send it to a btrfs developer, ref %Lu\n",
4721 dback->disk_bytenr);
4725 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4726 btrfs_set_file_extent_offset(leaf, fi, offset);
4727 } else if (dback->disk_bytenr < entry->bytenr) {
4730 offset = btrfs_file_extent_offset(leaf, fi);
4731 if (dback->disk_bytenr + offset < entry->bytenr) {
4732 fprintf(stderr, "Ref is before the entry start, please"
4733 " take a btrfs-image of this file system and "
4734 "send it to a btrfs developer, ref %Lu\n",
4735 dback->disk_bytenr);
4739 offset += dback->disk_bytenr;
4740 offset -= entry->bytenr;
4741 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4742 btrfs_set_file_extent_offset(leaf, fi, offset);
4745 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
4748 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
4749 * only do this if we aren't using compression, otherwise it's a
4752 if (!btrfs_file_extent_compression(leaf, fi))
4753 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
4755 printf("ram bytes may be wrong?\n");
4756 btrfs_mark_buffer_dirty(leaf);
4757 btrfs_release_path(path);
4761 static int verify_backrefs(struct btrfs_trans_handle *trans,
4762 struct btrfs_fs_info *info, struct btrfs_path *path,
4763 struct extent_record *rec)
4765 struct extent_backref *back;
4766 struct data_backref *dback;
4767 struct extent_entry *entry, *best = NULL;
4770 int broken_entries = 0;
4775 * Metadata is easy and the backrefs should always agree on bytenr and
4776 * size, if not we've got bigger issues.
4781 list_for_each_entry(back, &rec->backrefs, list) {
4782 dback = (struct data_backref *)back;
4784 * We only pay attention to backrefs that we found a real
4787 if (dback->found_ref == 0)
4789 if (back->full_backref)
4793 * For now we only catch when the bytes don't match, not the
4794 * bytenr. We can easily do this at the same time, but I want
4795 * to have a fs image to test on before we just add repair
4796 * functionality willy-nilly so we know we won't screw up the
4800 entry = find_entry(&entries, dback->disk_bytenr,
4803 entry = malloc(sizeof(struct extent_entry));
4808 memset(entry, 0, sizeof(*entry));
4809 entry->bytenr = dback->disk_bytenr;
4810 entry->bytes = dback->bytes;
4811 list_add_tail(&entry->list, &entries);
4816 * If we only have on entry we may think the entries agree when
4817 * in reality they don't so we have to do some extra checking.
4819 if (dback->disk_bytenr != rec->start ||
4820 dback->bytes != rec->nr || back->broken)
4831 /* Yay all the backrefs agree, carry on good sir */
4832 if (nr_entries <= 1 && !mismatch)
4835 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
4836 "%Lu\n", rec->start);
4839 * First we want to see if the backrefs can agree amongst themselves who
4840 * is right, so figure out which one of the entries has the highest
4843 best = find_most_right_entry(&entries);
4846 * Ok so we may have an even split between what the backrefs think, so
4847 * this is where we use the extent ref to see what it thinks.
4850 entry = find_entry(&entries, rec->start, rec->nr);
4851 if (!entry && (!broken_entries || !rec->found_rec)) {
4852 fprintf(stderr, "Backrefs don't agree with each other "
4853 "and extent record doesn't agree with anybody,"
4854 " so we can't fix bytenr %Lu bytes %Lu\n",
4855 rec->start, rec->nr);
4858 } else if (!entry) {
4860 * Ok our backrefs were broken, we'll assume this is the
4861 * correct value and add an entry for this range.
4863 entry = malloc(sizeof(struct extent_entry));
4868 memset(entry, 0, sizeof(*entry));
4869 entry->bytenr = rec->start;
4870 entry->bytes = rec->nr;
4871 list_add_tail(&entry->list, &entries);
4875 best = find_most_right_entry(&entries);
4877 fprintf(stderr, "Backrefs and extent record evenly "
4878 "split on who is right, this is going to "
4879 "require user input to fix bytenr %Lu bytes "
4880 "%Lu\n", rec->start, rec->nr);
4887 * I don't think this can happen currently as we'll abort() if we catch
4888 * this case higher up, but in case somebody removes that we still can't
4889 * deal with it properly here yet, so just bail out of that's the case.
4891 if (best->bytenr != rec->start) {
4892 fprintf(stderr, "Extent start and backref starts don't match, "
4893 "please use btrfs-image on this file system and send "
4894 "it to a btrfs developer so they can make fsck fix "
4895 "this particular case. bytenr is %Lu, bytes is %Lu\n",
4896 rec->start, rec->nr);
4902 * Ok great we all agreed on an extent record, let's go find the real
4903 * references and fix up the ones that don't match.
4905 list_for_each_entry(back, &rec->backrefs, list) {
4906 dback = (struct data_backref *)back;
4909 * Still ignoring backrefs that don't have a real ref attached
4912 if (dback->found_ref == 0)
4914 if (back->full_backref)
4917 if (dback->bytes == best->bytes &&
4918 dback->disk_bytenr == best->bytenr)
4921 ret = repair_ref(trans, info, path, dback, best);
4927 * Ok we messed with the actual refs, which means we need to drop our
4928 * entire cache and go back and rescan. I know this is a huge pain and
4929 * adds a lot of extra work, but it's the only way to be safe. Once all
4930 * the backrefs agree we may not need to do anything to the extent
4935 while (!list_empty(&entries)) {
4936 entry = list_entry(entries.next, struct extent_entry, list);
4937 list_del_init(&entry->list);
4943 static int process_duplicates(struct btrfs_root *root,
4944 struct cache_tree *extent_cache,
4945 struct extent_record *rec)
4947 struct extent_record *good, *tmp;
4948 struct cache_extent *cache;
4952 * If we found a extent record for this extent then return, or if we
4953 * have more than one duplicate we are likely going to need to delete
4956 if (rec->found_rec || rec->num_duplicates > 1)
4959 /* Shouldn't happen but just in case */
4960 BUG_ON(!rec->num_duplicates);
4963 * So this happens if we end up with a backref that doesn't match the
4964 * actual extent entry. So either the backref is bad or the extent
4965 * entry is bad. Either way we want to have the extent_record actually
4966 * reflect what we found in the extent_tree, so we need to take the
4967 * duplicate out and use that as the extent_record since the only way we
4968 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
4970 remove_cache_extent(extent_cache, &rec->cache);
4972 good = list_entry(rec->dups.next, struct extent_record, list);
4973 list_del_init(&good->list);
4974 INIT_LIST_HEAD(&good->backrefs);
4975 INIT_LIST_HEAD(&good->dups);
4976 good->cache.start = good->start;
4977 good->cache.size = good->nr;
4978 good->content_checked = 0;
4979 good->owner_ref_checked = 0;
4980 good->num_duplicates = 0;
4981 good->refs = rec->refs;
4982 list_splice_init(&rec->backrefs, &good->backrefs);
4984 cache = lookup_cache_extent(extent_cache, good->start,
4988 tmp = container_of(cache, struct extent_record, cache);
4991 * If we find another overlapping extent and it's found_rec is
4992 * set then it's a duplicate and we need to try and delete
4995 if (tmp->found_rec || tmp->num_duplicates > 0) {
4996 if (list_empty(&good->list))
4997 list_add_tail(&good->list,
4998 &duplicate_extents);
4999 good->num_duplicates += tmp->num_duplicates + 1;
5000 list_splice_init(&tmp->dups, &good->dups);
5001 list_del_init(&tmp->list);
5002 list_add_tail(&tmp->list, &good->dups);
5003 remove_cache_extent(extent_cache, &tmp->cache);
5008 * Ok we have another non extent item backed extent rec, so lets
5009 * just add it to this extent and carry on like we did above.
5011 good->refs += tmp->refs;
5012 list_splice_init(&tmp->backrefs, &good->backrefs);
5013 remove_cache_extent(extent_cache, &tmp->cache);
5016 ret = insert_cache_extent(extent_cache, &good->cache);
5019 return good->num_duplicates ? 0 : 1;
5022 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
5023 struct btrfs_root *root,
5024 struct extent_record *rec)
5026 LIST_HEAD(delete_list);
5027 struct btrfs_path *path;
5028 struct extent_record *tmp, *good, *n;
5031 struct btrfs_key key;
5033 path = btrfs_alloc_path();
5040 /* Find the record that covers all of the duplicates. */
5041 list_for_each_entry(tmp, &rec->dups, list) {
5042 if (good->start < tmp->start)
5044 if (good->nr > tmp->nr)
5047 if (tmp->start + tmp->nr < good->start + good->nr) {
5048 fprintf(stderr, "Ok we have overlapping extents that "
5049 "aren't completely covered by eachother, this "
5050 "is going to require more careful thought. "
5051 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
5052 tmp->start, tmp->nr, good->start, good->nr);
5059 list_add_tail(&rec->list, &delete_list);
5061 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
5064 list_move_tail(&tmp->list, &delete_list);
5067 root = root->fs_info->extent_root;
5068 list_for_each_entry(tmp, &delete_list, list) {
5069 if (tmp->found_rec == 0)
5071 key.objectid = tmp->start;
5072 key.type = BTRFS_EXTENT_ITEM_KEY;
5073 key.offset = tmp->nr;
5075 /* Shouldn't happen but just in case */
5076 if (tmp->metadata) {
5077 fprintf(stderr, "Well this shouldn't happen, extent "
5078 "record overlaps but is metadata? "
5079 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
5083 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5089 ret = btrfs_del_item(trans, root, path);
5092 btrfs_release_path(path);
5097 while (!list_empty(&delete_list)) {
5098 tmp = list_entry(delete_list.next, struct extent_record, list);
5099 list_del_init(&tmp->list);
5105 while (!list_empty(&rec->dups)) {
5106 tmp = list_entry(rec->dups.next, struct extent_record, list);
5107 list_del_init(&tmp->list);
5111 btrfs_free_path(path);
5113 if (!ret && !nr_del)
5114 rec->num_duplicates = 0;
5116 return ret ? ret : nr_del;
5119 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5120 struct btrfs_fs_info *info,
5121 struct btrfs_path *path,
5122 struct cache_tree *extent_cache,
5123 struct extent_record *rec)
5125 struct btrfs_root *root;
5126 struct extent_backref *back;
5127 struct data_backref *dback;
5128 struct cache_extent *cache;
5129 struct btrfs_file_extent_item *fi;
5130 struct btrfs_key key;
5134 list_for_each_entry(back, &rec->backrefs, list) {
5135 dback = (struct data_backref *)back;
5137 /* We found this one, we don't need to do a lookup */
5138 if (dback->found_ref)
5140 /* Don't care about full backrefs (poor unloved backrefs) */
5141 if (back->full_backref)
5143 key.objectid = dback->root;
5144 key.type = BTRFS_ROOT_ITEM_KEY;
5145 key.offset = (u64)-1;
5147 root = btrfs_read_fs_root(info, &key);
5149 /* No root, definitely a bad ref, skip */
5150 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5152 /* Other err, exit */
5154 return PTR_ERR(root);
5156 key.objectid = dback->owner;
5157 key.type = BTRFS_EXTENT_DATA_KEY;
5158 key.offset = dback->offset;
5159 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5161 btrfs_release_path(path);
5164 /* Didn't find it, we can carry on */
5169 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5170 struct btrfs_file_extent_item);
5171 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5172 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5173 btrfs_release_path(path);
5174 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5176 struct extent_record *tmp;
5177 tmp = container_of(cache, struct extent_record, cache);
5180 * If we found an extent record for the bytenr for this
5181 * particular backref then we can't add it to our
5182 * current extent record. We only want to add backrefs
5183 * that don't have a corresponding extent item in the
5184 * extent tree since they likely belong to this record
5185 * and we need to fix it if it doesn't match bytenrs.
5191 dback->found_ref += 1;
5192 dback->disk_bytenr = bytenr;
5193 dback->bytes = bytes;
5196 * Set this so the verify backref code knows not to trust the
5197 * values in this backref.
5206 * when an incorrect extent item is found, this will delete
5207 * all of the existing entries for it and recreate them
5208 * based on what the tree scan found.
5210 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5211 struct btrfs_fs_info *info,
5212 struct cache_tree *extent_cache,
5213 struct extent_record *rec)
5216 struct btrfs_path *path;
5217 struct list_head *cur = rec->backrefs.next;
5218 struct cache_extent *cache;
5219 struct extent_backref *back;
5224 * remember our flags for recreating the extent.
5225 * FIXME, if we have cleared extent tree, we can not
5226 * lookup extent info in extent tree.
5228 if (!init_extent_tree) {
5229 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5230 rec->start, rec->max_size,
5231 rec->metadata, NULL, &flags);
5238 path = btrfs_alloc_path();
5242 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5244 * Sometimes the backrefs themselves are so broken they don't
5245 * get attached to any meaningful rec, so first go back and
5246 * check any of our backrefs that we couldn't find and throw
5247 * them into the list if we find the backref so that
5248 * verify_backrefs can figure out what to do.
5250 ret = find_possible_backrefs(trans, info, path, extent_cache,
5256 /* step one, make sure all of the backrefs agree */
5257 ret = verify_backrefs(trans, info, path, rec);
5261 /* step two, delete all the existing records */
5262 ret = delete_extent_records(trans, info->extent_root, path,
5263 rec->start, rec->max_size);
5268 /* was this block corrupt? If so, don't add references to it */
5269 cache = lookup_cache_extent(info->corrupt_blocks,
5270 rec->start, rec->max_size);
5276 /* step three, recreate all the refs we did find */
5277 while(cur != &rec->backrefs) {
5278 back = list_entry(cur, struct extent_backref, list);
5282 * if we didn't find any references, don't create a
5285 if (!back->found_ref)
5288 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5295 btrfs_free_path(path);
5299 /* right now we only prune from the extent allocation tree */
5300 static int prune_one_block(struct btrfs_trans_handle *trans,
5301 struct btrfs_fs_info *info,
5302 struct btrfs_corrupt_block *corrupt)
5305 struct btrfs_path path;
5306 struct extent_buffer *eb;
5310 int level = corrupt->level + 1;
5312 btrfs_init_path(&path);
5314 /* we want to stop at the parent to our busted block */
5315 path.lowest_level = level;
5317 ret = btrfs_search_slot(trans, info->extent_root,
5318 &corrupt->key, &path, -1, 1);
5323 eb = path.nodes[level];
5330 * hopefully the search gave us the block we want to prune,
5331 * lets try that first
5333 slot = path.slots[level];
5334 found = btrfs_node_blockptr(eb, slot);
5335 if (found == corrupt->cache.start)
5338 nritems = btrfs_header_nritems(eb);
5340 /* the search failed, lets scan this node and hope we find it */
5341 for (slot = 0; slot < nritems; slot++) {
5342 found = btrfs_node_blockptr(eb, slot);
5343 if (found == corrupt->cache.start)
5347 * we couldn't find the bad block. TODO, search all the nodes for pointers
5350 if (eb == info->extent_root->node) {
5355 btrfs_release_path(&path);
5360 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
5361 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
5364 btrfs_release_path(&path);
5368 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
5369 struct btrfs_fs_info *info)
5371 struct cache_extent *cache;
5372 struct btrfs_corrupt_block *corrupt;
5374 cache = search_cache_extent(info->corrupt_blocks, 0);
5378 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5379 prune_one_block(trans, info, corrupt);
5380 cache = next_cache_extent(cache);
5385 static void free_corrupt_block(struct cache_extent *cache)
5387 struct btrfs_corrupt_block *corrupt;
5389 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5393 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5395 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5397 struct btrfs_block_group_cache *cache;
5402 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5403 &start, &end, EXTENT_DIRTY);
5406 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5412 cache = btrfs_lookup_first_block_group(fs_info, start);
5417 start = cache->key.objectid + cache->key.offset;
5421 static int check_extent_refs(struct btrfs_trans_handle *trans,
5422 struct btrfs_root *root,
5423 struct cache_tree *extent_cache)
5425 struct extent_record *rec;
5426 struct cache_extent *cache;
5434 * if we're doing a repair, we have to make sure
5435 * we don't allocate from the problem extents.
5436 * In the worst case, this will be all the
5439 cache = search_cache_extent(extent_cache, 0);
5441 rec = container_of(cache, struct extent_record, cache);
5442 btrfs_pin_extent(root->fs_info,
5443 rec->start, rec->max_size);
5444 cache = next_cache_extent(cache);
5447 /* pin down all the corrupted blocks too */
5448 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5450 btrfs_pin_extent(root->fs_info,
5451 cache->start, cache->size);
5452 cache = next_cache_extent(cache);
5454 prune_corrupt_blocks(trans, root->fs_info);
5455 reset_cached_block_groups(root->fs_info);
5459 * We need to delete any duplicate entries we find first otherwise we
5460 * could mess up the extent tree when we have backrefs that actually
5461 * belong to a different extent item and not the weird duplicate one.
5463 while (repair && !list_empty(&duplicate_extents)) {
5464 rec = list_entry(duplicate_extents.next, struct extent_record,
5466 list_del_init(&rec->list);
5468 /* Sometimes we can find a backref before we find an actual
5469 * extent, so we need to process it a little bit to see if there
5470 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
5471 * if this is a backref screwup. If we need to delete stuff
5472 * process_duplicates() will return 0, otherwise it will return
5475 if (process_duplicates(root, extent_cache, rec))
5477 ret = delete_duplicate_records(trans, root, rec);
5481 * delete_duplicate_records will return the number of entries
5482 * deleted, so if it's greater than 0 then we know we actually
5483 * did something and we need to remove.
5494 cache = search_cache_extent(extent_cache, 0);
5497 rec = container_of(cache, struct extent_record, cache);
5498 if (rec->num_duplicates) {
5499 fprintf(stderr, "extent item %llu has multiple extent "
5500 "items\n", (unsigned long long)rec->start);
5504 if (rec->refs != rec->extent_item_refs) {
5505 fprintf(stderr, "ref mismatch on [%llu %llu] ",
5506 (unsigned long long)rec->start,
5507 (unsigned long long)rec->nr);
5508 fprintf(stderr, "extent item %llu, found %llu\n",
5509 (unsigned long long)rec->extent_item_refs,
5510 (unsigned long long)rec->refs);
5511 if (!fixed && repair) {
5512 ret = fixup_extent_refs(trans, root->fs_info,
5521 if (all_backpointers_checked(rec, 1)) {
5522 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
5523 (unsigned long long)rec->start,
5524 (unsigned long long)rec->nr);
5526 if (!fixed && repair) {
5527 ret = fixup_extent_refs(trans, root->fs_info,
5536 if (!rec->owner_ref_checked) {
5537 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
5538 (unsigned long long)rec->start,
5539 (unsigned long long)rec->nr);
5540 if (!fixed && repair) {
5541 ret = fixup_extent_refs(trans, root->fs_info,
5550 remove_cache_extent(extent_cache, cache);
5551 free_all_extent_backrefs(rec);
5556 if (ret && ret != -EAGAIN) {
5557 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
5560 btrfs_fix_block_accounting(trans, root);
5563 fprintf(stderr, "repaired damaged extent references\n");
5569 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
5573 if (type & BTRFS_BLOCK_GROUP_RAID0) {
5574 stripe_size = length;
5575 stripe_size /= num_stripes;
5576 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
5577 stripe_size = length * 2;
5578 stripe_size /= num_stripes;
5579 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
5580 stripe_size = length;
5581 stripe_size /= (num_stripes - 1);
5582 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
5583 stripe_size = length;
5584 stripe_size /= (num_stripes - 2);
5586 stripe_size = length;
5591 static int check_chunk_refs(struct chunk_record *chunk_rec,
5592 struct block_group_tree *block_group_cache,
5593 struct device_extent_tree *dev_extent_cache,
5596 struct cache_extent *block_group_item;
5597 struct block_group_record *block_group_rec;
5598 struct cache_extent *dev_extent_item;
5599 struct device_extent_record *dev_extent_rec;
5606 block_group_item = lookup_cache_extent(&block_group_cache->tree,
5609 if (block_group_item) {
5610 block_group_rec = container_of(block_group_item,
5611 struct block_group_record,
5613 if (chunk_rec->length != block_group_rec->offset ||
5614 chunk_rec->offset != block_group_rec->objectid ||
5615 chunk_rec->type_flags != block_group_rec->flags) {
5618 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
5619 chunk_rec->objectid,
5624 chunk_rec->type_flags,
5625 block_group_rec->objectid,
5626 block_group_rec->type,
5627 block_group_rec->offset,
5628 block_group_rec->offset,
5629 block_group_rec->objectid,
5630 block_group_rec->flags);
5633 list_del_init(&block_group_rec->list);
5634 chunk_rec->bg_rec = block_group_rec;
5639 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
5640 chunk_rec->objectid,
5645 chunk_rec->type_flags);
5649 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
5650 chunk_rec->num_stripes);
5651 for (i = 0; i < chunk_rec->num_stripes; ++i) {
5652 devid = chunk_rec->stripes[i].devid;
5653 offset = chunk_rec->stripes[i].offset;
5654 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
5655 devid, offset, length);
5656 if (dev_extent_item) {
5657 dev_extent_rec = container_of(dev_extent_item,
5658 struct device_extent_record,
5660 if (dev_extent_rec->objectid != devid ||
5661 dev_extent_rec->offset != offset ||
5662 dev_extent_rec->chunk_offset != chunk_rec->offset ||
5663 dev_extent_rec->length != length) {
5666 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
5667 chunk_rec->objectid,
5670 chunk_rec->stripes[i].devid,
5671 chunk_rec->stripes[i].offset,
5672 dev_extent_rec->objectid,
5673 dev_extent_rec->offset,
5674 dev_extent_rec->length);
5677 list_move(&dev_extent_rec->chunk_list,
5678 &chunk_rec->dextents);
5683 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
5684 chunk_rec->objectid,
5687 chunk_rec->stripes[i].devid,
5688 chunk_rec->stripes[i].offset);
5695 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
5696 int check_chunks(struct cache_tree *chunk_cache,
5697 struct block_group_tree *block_group_cache,
5698 struct device_extent_tree *dev_extent_cache,
5699 struct list_head *good, struct list_head *bad, int silent)
5701 struct cache_extent *chunk_item;
5702 struct chunk_record *chunk_rec;
5703 struct block_group_record *bg_rec;
5704 struct device_extent_record *dext_rec;
5708 chunk_item = first_cache_extent(chunk_cache);
5709 while (chunk_item) {
5710 chunk_rec = container_of(chunk_item, struct chunk_record,
5712 err = check_chunk_refs(chunk_rec, block_group_cache,
5713 dev_extent_cache, silent);
5717 list_add_tail(&chunk_rec->list, bad);
5720 list_add_tail(&chunk_rec->list, good);
5723 chunk_item = next_cache_extent(chunk_item);
5726 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
5729 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
5737 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
5741 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
5752 static int check_device_used(struct device_record *dev_rec,
5753 struct device_extent_tree *dext_cache)
5755 struct cache_extent *cache;
5756 struct device_extent_record *dev_extent_rec;
5759 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
5761 dev_extent_rec = container_of(cache,
5762 struct device_extent_record,
5764 if (dev_extent_rec->objectid != dev_rec->devid)
5767 list_del(&dev_extent_rec->device_list);
5768 total_byte += dev_extent_rec->length;
5769 cache = next_cache_extent(cache);
5772 if (total_byte != dev_rec->byte_used) {
5774 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
5775 total_byte, dev_rec->byte_used, dev_rec->objectid,
5776 dev_rec->type, dev_rec->offset);
5783 /* check btrfs_dev_item -> btrfs_dev_extent */
5784 static int check_devices(struct rb_root *dev_cache,
5785 struct device_extent_tree *dev_extent_cache)
5787 struct rb_node *dev_node;
5788 struct device_record *dev_rec;
5789 struct device_extent_record *dext_rec;
5793 dev_node = rb_first(dev_cache);
5795 dev_rec = container_of(dev_node, struct device_record, node);
5796 err = check_device_used(dev_rec, dev_extent_cache);
5800 dev_node = rb_next(dev_node);
5802 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
5805 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
5806 dext_rec->objectid, dext_rec->offset, dext_rec->length);
5813 static int check_chunks_and_extents(struct btrfs_root *root)
5815 struct rb_root dev_cache;
5816 struct cache_tree chunk_cache;
5817 struct block_group_tree block_group_cache;
5818 struct device_extent_tree dev_extent_cache;
5819 struct cache_tree extent_cache;
5820 struct cache_tree seen;
5821 struct cache_tree pending;
5822 struct cache_tree reada;
5823 struct cache_tree nodes;
5824 struct cache_tree corrupt_blocks;
5825 struct btrfs_path path;
5826 struct btrfs_key key;
5827 struct btrfs_key found_key;
5830 struct block_info *bits;
5832 struct extent_buffer *leaf;
5833 struct btrfs_trans_handle *trans = NULL;
5835 struct btrfs_root_item ri;
5836 struct list_head dropping_trees;
5838 dev_cache = RB_ROOT;
5839 cache_tree_init(&chunk_cache);
5840 block_group_tree_init(&block_group_cache);
5841 device_extent_tree_init(&dev_extent_cache);
5843 cache_tree_init(&extent_cache);
5844 cache_tree_init(&seen);
5845 cache_tree_init(&pending);
5846 cache_tree_init(&nodes);
5847 cache_tree_init(&reada);
5848 cache_tree_init(&corrupt_blocks);
5849 INIT_LIST_HEAD(&dropping_trees);
5852 trans = btrfs_start_transaction(root, 1);
5853 if (IS_ERR(trans)) {
5854 fprintf(stderr, "Error starting transaction\n");
5855 return PTR_ERR(trans);
5857 root->fs_info->fsck_extent_cache = &extent_cache;
5858 root->fs_info->free_extent_hook = free_extent_hook;
5859 root->fs_info->corrupt_blocks = &corrupt_blocks;
5863 bits = malloc(bits_nr * sizeof(struct block_info));
5870 add_root_to_pending(root->fs_info->tree_root->node,
5871 &extent_cache, &pending, &seen, &nodes,
5872 &root->fs_info->tree_root->root_key);
5874 add_root_to_pending(root->fs_info->chunk_root->node,
5875 &extent_cache, &pending, &seen, &nodes,
5876 &root->fs_info->chunk_root->root_key);
5878 btrfs_init_path(&path);
5881 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
5882 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
5886 leaf = path.nodes[0];
5887 slot = path.slots[0];
5888 if (slot >= btrfs_header_nritems(path.nodes[0])) {
5889 ret = btrfs_next_leaf(root, &path);
5892 leaf = path.nodes[0];
5893 slot = path.slots[0];
5895 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
5896 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
5897 unsigned long offset;
5898 struct extent_buffer *buf;
5900 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5901 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5902 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
5903 buf = read_tree_block(root->fs_info->tree_root,
5904 btrfs_root_bytenr(&ri),
5905 btrfs_level_size(root,
5906 btrfs_root_level(&ri)),
5912 add_root_to_pending(buf, &extent_cache,
5913 &pending, &seen, &nodes,
5915 free_extent_buffer(buf);
5917 struct dropping_root_item_record *dri_rec;
5918 dri_rec = malloc(sizeof(*dri_rec));
5923 memcpy(&dri_rec->ri, &ri, sizeof(ri));
5924 memcpy(&dri_rec->found_key, &found_key,
5926 list_add_tail(&dri_rec->list, &dropping_trees);
5931 btrfs_release_path(&path);
5933 ret = run_next_block(trans, root, bits, bits_nr, &last,
5934 &pending, &seen, &reada, &nodes,
5935 &extent_cache, &chunk_cache, &dev_cache,
5936 &block_group_cache, &dev_extent_cache,
5942 while (!list_empty(&dropping_trees)) {
5943 struct dropping_root_item_record *rec;
5944 struct extent_buffer *buf;
5945 rec = list_entry(dropping_trees.next,
5946 struct dropping_root_item_record, list);
5952 buf = read_tree_block(root->fs_info->tree_root,
5953 btrfs_root_bytenr(&rec->ri),
5954 btrfs_level_size(root,
5955 btrfs_root_level(&rec->ri)), 0);
5960 add_root_to_pending(buf, &extent_cache, &pending,
5961 &seen, &nodes, &rec->found_key);
5963 ret = run_next_block(trans, root, bits, bits_nr, &last,
5964 &pending, &seen, &reada,
5965 &nodes, &extent_cache,
5966 &chunk_cache, &dev_cache,
5973 free_extent_buffer(buf);
5974 list_del(&rec->list);
5979 ret = check_extent_refs(trans, root, &extent_cache);
5980 if (ret == -EAGAIN) {
5981 ret = btrfs_commit_transaction(trans, root);
5985 trans = btrfs_start_transaction(root, 1);
5986 if (IS_ERR(trans)) {
5987 ret = PTR_ERR(trans);
5991 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
5992 free_extent_cache_tree(&seen);
5993 free_extent_cache_tree(&pending);
5994 free_extent_cache_tree(&reada);
5995 free_extent_cache_tree(&nodes);
5996 free_extent_record_cache(root->fs_info, &extent_cache);
6000 err = check_chunks(&chunk_cache, &block_group_cache,
6001 &dev_extent_cache, NULL, NULL, 0);
6005 err = check_devices(&dev_cache, &dev_extent_cache);
6010 err = btrfs_commit_transaction(trans, root);
6016 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6017 root->fs_info->fsck_extent_cache = NULL;
6018 root->fs_info->free_extent_hook = NULL;
6019 root->fs_info->corrupt_blocks = NULL;
6022 free_chunk_cache_tree(&chunk_cache);
6023 free_device_cache_tree(&dev_cache);
6024 free_block_group_tree(&block_group_cache);
6025 free_device_extent_tree(&dev_extent_cache);
6026 free_extent_cache_tree(&seen);
6027 free_extent_cache_tree(&pending);
6028 free_extent_cache_tree(&reada);
6029 free_extent_cache_tree(&nodes);
6033 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
6034 struct btrfs_root *root, int overwrite)
6036 struct extent_buffer *c;
6037 struct extent_buffer *old = root->node;
6040 struct btrfs_disk_key disk_key = {0,0,0};
6046 extent_buffer_get(c);
6049 c = btrfs_alloc_free_block(trans, root,
6050 btrfs_level_size(root, 0),
6051 root->root_key.objectid,
6052 &disk_key, level, 0, 0);
6055 extent_buffer_get(c);
6059 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
6060 btrfs_set_header_level(c, level);
6061 btrfs_set_header_bytenr(c, c->start);
6062 btrfs_set_header_generation(c, trans->transid);
6063 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
6064 btrfs_set_header_owner(c, root->root_key.objectid);
6066 write_extent_buffer(c, root->fs_info->fsid,
6067 btrfs_header_fsid(), BTRFS_FSID_SIZE);
6069 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
6070 btrfs_header_chunk_tree_uuid(c),
6073 btrfs_mark_buffer_dirty(c);
6075 * this case can happen in the following case:
6077 * 1.overwrite previous root.
6079 * 2.reinit reloc data root, this is because we skip pin
6080 * down reloc data tree before which means we can allocate
6081 * same block bytenr here.
6083 if (old->start == c->start) {
6084 btrfs_set_root_generation(&root->root_item,
6086 root->root_item.level = btrfs_header_level(root->node);
6087 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6088 &root->root_key, &root->root_item);
6090 free_extent_buffer(c);
6094 free_extent_buffer(old);
6096 add_root_to_dirty_list(root);
6100 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
6101 struct extent_buffer *eb, int tree_root)
6103 struct extent_buffer *tmp;
6104 struct btrfs_root_item *ri;
6105 struct btrfs_key key;
6108 int level = btrfs_header_level(eb);
6113 btrfs_pin_extent(fs_info, eb->start, eb->len);
6115 leafsize = btrfs_super_leafsize(fs_info->super_copy);
6116 nritems = btrfs_header_nritems(eb);
6117 for (i = 0; i < nritems; i++) {
6119 btrfs_item_key_to_cpu(eb, &key, i);
6120 if (key.type != BTRFS_ROOT_ITEM_KEY)
6122 /* Skip the extent root and reloc roots */
6123 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6124 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6125 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6127 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6128 bytenr = btrfs_disk_root_bytenr(eb, ri);
6131 * If at any point we start needing the real root we
6132 * will have to build a stump root for the root we are
6133 * in, but for now this doesn't actually use the root so
6134 * just pass in extent_root.
6136 tmp = read_tree_block(fs_info->extent_root, bytenr,
6139 fprintf(stderr, "Error reading root block\n");
6142 ret = pin_down_tree_blocks(fs_info, tmp, 0);
6143 free_extent_buffer(tmp);
6147 bytenr = btrfs_node_blockptr(eb, i);
6149 /* If we aren't the tree root don't read the block */
6150 if (level == 1 && !tree_root) {
6151 btrfs_pin_extent(fs_info, bytenr, leafsize);
6155 tmp = read_tree_block(fs_info->extent_root, bytenr,
6158 fprintf(stderr, "Error reading tree block\n");
6161 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6162 free_extent_buffer(tmp);
6171 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6175 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6179 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6182 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6184 struct btrfs_block_group_cache *cache;
6185 struct btrfs_path *path;
6186 struct extent_buffer *leaf;
6187 struct btrfs_chunk *chunk;
6188 struct btrfs_key key;
6192 path = btrfs_alloc_path();
6197 key.type = BTRFS_CHUNK_ITEM_KEY;
6200 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6202 btrfs_free_path(path);
6207 * We do this in case the block groups were screwed up and had alloc
6208 * bits that aren't actually set on the chunks. This happens with
6209 * restored images every time and could happen in real life I guess.
6211 fs_info->avail_data_alloc_bits = 0;
6212 fs_info->avail_metadata_alloc_bits = 0;
6213 fs_info->avail_system_alloc_bits = 0;
6215 /* First we need to create the in-memory block groups */
6217 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6218 ret = btrfs_next_leaf(fs_info->chunk_root, path);
6220 btrfs_free_path(path);
6228 leaf = path->nodes[0];
6229 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6230 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6235 chunk = btrfs_item_ptr(leaf, path->slots[0],
6236 struct btrfs_chunk);
6237 btrfs_add_block_group(fs_info, 0,
6238 btrfs_chunk_type(leaf, chunk),
6239 key.objectid, key.offset,
6240 btrfs_chunk_length(leaf, chunk));
6241 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6242 key.offset + btrfs_chunk_length(leaf, chunk),
6248 cache = btrfs_lookup_first_block_group(fs_info, start);
6252 start = cache->key.objectid + cache->key.offset;
6255 btrfs_free_path(path);
6259 static int reset_balance(struct btrfs_trans_handle *trans,
6260 struct btrfs_fs_info *fs_info)
6262 struct btrfs_root *root = fs_info->tree_root;
6263 struct btrfs_path *path;
6264 struct extent_buffer *leaf;
6265 struct btrfs_key key;
6266 int del_slot, del_nr = 0;
6270 path = btrfs_alloc_path();
6274 key.objectid = BTRFS_BALANCE_OBJECTID;
6275 key.type = BTRFS_BALANCE_ITEM_KEY;
6278 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6283 goto reinit_data_reloc;
6288 ret = btrfs_del_item(trans, root, path);
6291 btrfs_release_path(path);
6293 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6294 key.type = BTRFS_ROOT_ITEM_KEY;
6297 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6301 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6306 ret = btrfs_del_items(trans, root, path,
6313 btrfs_release_path(path);
6316 ret = btrfs_search_slot(trans, root, &key, path,
6323 leaf = path->nodes[0];
6324 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6325 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
6327 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6332 del_slot = path->slots[0];
6341 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
6345 btrfs_release_path(path);
6348 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6349 key.type = BTRFS_ROOT_ITEM_KEY;
6350 key.offset = (u64)-1;
6351 root = btrfs_read_fs_root(fs_info, &key);
6353 fprintf(stderr, "Error reading data reloc tree\n");
6354 return PTR_ERR(root);
6356 root->track_dirty = 1;
6357 if (root->last_trans != trans->transid) {
6358 root->last_trans = trans->transid;
6359 root->commit_root = root->node;
6360 extent_buffer_get(root->node);
6362 ret = btrfs_fsck_reinit_root(trans, root, 0);
6365 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
6367 btrfs_free_path(path);
6371 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
6372 struct btrfs_fs_info *fs_info)
6378 * The only reason we don't do this is because right now we're just
6379 * walking the trees we find and pinning down their bytes, we don't look
6380 * at any of the leaves. In order to do mixed groups we'd have to check
6381 * the leaves of any fs roots and pin down the bytes for any file
6382 * extents we find. Not hard but why do it if we don't have to?
6384 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
6385 fprintf(stderr, "We don't support re-initing the extent tree "
6386 "for mixed block groups yet, please notify a btrfs "
6387 "developer you want to do this so they can add this "
6388 "functionality.\n");
6393 * first we need to walk all of the trees except the extent tree and pin
6394 * down the bytes that are in use so we don't overwrite any existing
6397 ret = pin_metadata_blocks(fs_info);
6399 fprintf(stderr, "error pinning down used bytes\n");
6404 * Need to drop all the block groups since we're going to recreate all
6407 btrfs_free_block_groups(fs_info);
6408 ret = reset_block_groups(fs_info);
6410 fprintf(stderr, "error resetting the block groups\n");
6414 /* Ok we can allocate now, reinit the extent root */
6415 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
6417 fprintf(stderr, "extent root initialization failed\n");
6419 * When the transaction code is updated we should end the
6420 * transaction, but for now progs only knows about commit so
6421 * just return an error.
6427 * Now we have all the in-memory block groups setup so we can make
6428 * allocations properly, and the metadata we care about is safe since we
6429 * pinned all of it above.
6432 struct btrfs_block_group_cache *cache;
6434 cache = btrfs_lookup_first_block_group(fs_info, start);
6437 start = cache->key.objectid + cache->key.offset;
6438 ret = btrfs_insert_item(trans, fs_info->extent_root,
6439 &cache->key, &cache->item,
6440 sizeof(cache->item));
6442 fprintf(stderr, "Error adding block group\n");
6445 btrfs_extent_post_op(trans, fs_info->extent_root);
6448 ret = reset_balance(trans, fs_info);
6450 fprintf(stderr, "error reseting the pending balance\n");
6455 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
6457 struct btrfs_path *path;
6458 struct btrfs_trans_handle *trans;
6459 struct btrfs_key key;
6462 printf("Recowing metadata block %llu\n", eb->start);
6463 key.objectid = btrfs_header_owner(eb);
6464 key.type = BTRFS_ROOT_ITEM_KEY;
6465 key.offset = (u64)-1;
6467 root = btrfs_read_fs_root(root->fs_info, &key);
6469 fprintf(stderr, "Couldn't find owner root %llu\n",
6471 return PTR_ERR(root);
6474 path = btrfs_alloc_path();
6478 trans = btrfs_start_transaction(root, 1);
6479 if (IS_ERR(trans)) {
6480 btrfs_free_path(path);
6481 return PTR_ERR(trans);
6484 path->lowest_level = btrfs_header_level(eb);
6485 if (path->lowest_level)
6486 btrfs_node_key_to_cpu(eb, &key, 0);
6488 btrfs_item_key_to_cpu(eb, &key, 0);
6490 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6491 btrfs_commit_transaction(trans, root);
6492 btrfs_free_path(path);
6496 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
6498 struct btrfs_path *path;
6499 struct btrfs_trans_handle *trans;
6500 struct btrfs_key key;
6503 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
6504 bad->key.type, bad->key.offset);
6505 key.objectid = bad->root_id;
6506 key.type = BTRFS_ROOT_ITEM_KEY;
6507 key.offset = (u64)-1;
6509 root = btrfs_read_fs_root(root->fs_info, &key);
6511 fprintf(stderr, "Couldn't find owner root %llu\n",
6513 return PTR_ERR(root);
6516 path = btrfs_alloc_path();
6520 trans = btrfs_start_transaction(root, 1);
6521 if (IS_ERR(trans)) {
6522 btrfs_free_path(path);
6523 return PTR_ERR(trans);
6526 ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
6532 ret = btrfs_del_item(trans, root, path);
6534 btrfs_commit_transaction(trans, root);
6535 btrfs_free_path(path);
6539 static struct option long_options[] = {
6540 { "super", 1, NULL, 's' },
6541 { "repair", 0, NULL, 0 },
6542 { "init-csum-tree", 0, NULL, 0 },
6543 { "init-extent-tree", 0, NULL, 0 },
6544 { "check-data-csum", 0, NULL, 0 },
6545 { "backup", 0, NULL, 0 },
6546 { "qgroup-report", 0, NULL, 'Q' },
6550 const char * const cmd_check_usage[] = {
6551 "btrfs check [options] <device>",
6552 "Check an unmounted btrfs filesystem.",
6554 "-s|--super <superblock> use this superblock copy",
6555 "-b|--backup use the backup root copy",
6556 "--repair try to repair the filesystem",
6557 "--init-csum-tree create a new CRC tree",
6558 "--init-extent-tree create a new extent tree",
6559 "--check-data-csum verify checkums of data blocks",
6560 "--qgroup-report print a report on qgroup consistency",
6564 int cmd_check(int argc, char **argv)
6566 struct cache_tree root_cache;
6567 struct btrfs_root *root;
6568 struct btrfs_fs_info *info;
6570 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
6573 int option_index = 0;
6574 int init_csum_tree = 0;
6575 int qgroup_report = 0;
6576 enum btrfs_open_ctree_flags ctree_flags =
6577 OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
6581 c = getopt_long(argc, argv, "as:b", long_options,
6586 case 'a': /* ignored */ break;
6588 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6591 num = arg_strtou64(optarg);
6592 if (num >= BTRFS_SUPER_MIRROR_MAX) {
6594 "ERROR: super mirror should be less than: %d\n",
6595 BTRFS_SUPER_MIRROR_MAX);
6598 bytenr = btrfs_sb_offset(((int)num));
6599 printf("using SB copy %llu, bytenr %llu\n", num,
6600 (unsigned long long)bytenr);
6607 usage(cmd_check_usage);
6609 if (option_index == 1) {
6610 printf("enabling repair mode\n");
6612 ctree_flags |= OPEN_CTREE_WRITES;
6613 } else if (option_index == 2) {
6614 printf("Creating a new CRC tree\n");
6617 ctree_flags |= OPEN_CTREE_WRITES;
6618 } else if (option_index == 3) {
6619 init_extent_tree = 1;
6620 ctree_flags |= (OPEN_CTREE_WRITES |
6621 OPEN_CTREE_NO_BLOCK_GROUPS);
6623 } else if (option_index == 4) {
6624 check_data_csum = 1;
6627 argc = argc - optind;
6630 usage(cmd_check_usage);
6633 cache_tree_init(&root_cache);
6635 if((ret = check_mounted(argv[optind])) < 0) {
6636 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6639 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6644 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6646 fprintf(stderr, "Couldn't open file system\n");
6651 root = info->fs_root;
6652 uuid_unparse(info->super_copy->fsid, uuidbuf);
6653 if (qgroup_report) {
6654 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
6656 ret = qgroup_verify_all(info);
6658 print_qgroup_report(1);
6661 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6663 if (!extent_buffer_uptodate(info->tree_root->node) ||
6664 !extent_buffer_uptodate(info->dev_root->node) ||
6665 !extent_buffer_uptodate(info->chunk_root->node)) {
6666 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6671 if (init_extent_tree || init_csum_tree) {
6672 struct btrfs_trans_handle *trans;
6674 trans = btrfs_start_transaction(info->extent_root, 0);
6675 if (IS_ERR(trans)) {
6676 fprintf(stderr, "Error starting transaction\n");
6677 ret = PTR_ERR(trans);
6681 if (init_extent_tree) {
6682 printf("Creating a new extent tree\n");
6683 ret = reinit_extent_tree(trans, info);
6688 if (init_csum_tree) {
6689 fprintf(stderr, "Reinit crc root\n");
6690 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6692 fprintf(stderr, "crc root initialization failed\n");
6698 * Ok now we commit and run the normal fsck, which will add
6699 * extent entries for all of the items it finds.
6701 ret = btrfs_commit_transaction(trans, info->extent_root);
6705 if (!extent_buffer_uptodate(info->extent_root->node)) {
6706 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6711 fprintf(stderr, "checking extents\n");
6712 ret = check_chunks_and_extents(root);
6714 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6716 fprintf(stderr, "checking free space cache\n");
6717 ret = check_space_cache(root);
6722 * We used to have to have these hole extents in between our real
6723 * extents so if we don't have this flag set we need to make sure there
6724 * are no gaps in the file extents for inodes, otherwise we can just
6725 * ignore it when this happens.
6727 no_holes = btrfs_fs_incompat(root->fs_info,
6728 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
6729 fprintf(stderr, "checking fs roots\n");
6730 ret = check_fs_roots(root, &root_cache);
6734 fprintf(stderr, "checking csums\n");
6735 ret = check_csums(root);
6739 fprintf(stderr, "checking root refs\n");
6740 ret = check_root_refs(root, &root_cache);
6744 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
6745 struct extent_buffer *eb;
6747 eb = list_first_entry(&root->fs_info->recow_ebs,
6748 struct extent_buffer, recow);
6749 ret = recow_extent_buffer(root, eb);
6754 while (!list_empty(&delete_items)) {
6755 struct bad_item *bad;
6757 bad = list_first_entry(&delete_items, struct bad_item, list);
6758 list_del_init(&bad->list);
6760 ret = delete_bad_item(root, bad);
6764 if (info->quota_enabled) {
6766 fprintf(stderr, "checking quota groups\n");
6767 err = qgroup_verify_all(info);
6772 if (!list_empty(&root->fs_info->recow_ebs)) {
6773 fprintf(stderr, "Transid errors in file system\n");
6777 print_qgroup_report(0);
6778 if (found_old_backref) { /*
6779 * there was a disk format change when mixed
6780 * backref was in testing tree. The old format
6781 * existed about one week.
6783 printf("\n * Found old mixed backref format. "
6784 "The old format is not supported! *"
6785 "\n * Please mount the FS in readonly mode, "
6786 "backup data and re-format the FS. *\n\n");
6789 printf("found %llu bytes used err is %d\n",
6790 (unsigned long long)bytes_used, ret);
6791 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
6792 printf("total tree bytes: %llu\n",
6793 (unsigned long long)total_btree_bytes);
6794 printf("total fs tree bytes: %llu\n",
6795 (unsigned long long)total_fs_tree_bytes);
6796 printf("total extent tree bytes: %llu\n",
6797 (unsigned long long)total_extent_tree_bytes);
6798 printf("btree space waste bytes: %llu\n",
6799 (unsigned long long)btree_space_waste);
6800 printf("file data blocks allocated: %llu\n referenced %llu\n",
6801 (unsigned long long)data_bytes_allocated,
6802 (unsigned long long)data_bytes_referenced);
6803 printf("%s\n", BTRFS_BUILD_VERSION);
6805 free_root_recs_tree(&root_cache);