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 void record_root_in_trans(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root)
249 if (root->last_trans != trans->transid) {
250 root->track_dirty = 1;
251 root->last_trans = trans->transid;
252 root->commit_root = root->node;
253 extent_buffer_get(root->node);
257 static u8 imode_to_type(u32 imode)
260 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
261 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
262 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
263 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
264 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
265 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
266 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
267 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
270 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
274 static int device_record_compare(struct rb_node *node1, struct rb_node *node2)
276 struct device_record *rec1;
277 struct device_record *rec2;
279 rec1 = rb_entry(node1, struct device_record, node);
280 rec2 = rb_entry(node2, struct device_record, node);
281 if (rec1->devid > rec2->devid)
283 else if (rec1->devid < rec2->devid)
289 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
291 struct inode_record *rec;
292 struct inode_backref *backref;
293 struct inode_backref *orig;
296 rec = malloc(sizeof(*rec));
297 memcpy(rec, orig_rec, sizeof(*rec));
299 INIT_LIST_HEAD(&rec->backrefs);
301 list_for_each_entry(orig, &orig_rec->backrefs, list) {
302 size = sizeof(*orig) + orig->namelen + 1;
303 backref = malloc(size);
304 memcpy(backref, orig, size);
305 list_add_tail(&backref->list, &rec->backrefs);
310 static void print_inode_error(int errors)
312 if (errors & I_ERR_NO_INODE_ITEM)
313 fprintf(stderr, ", no inode item");
314 if (errors & I_ERR_NO_ORPHAN_ITEM)
315 fprintf(stderr, ", no orphan item");
316 if (errors & I_ERR_DUP_INODE_ITEM)
317 fprintf(stderr, ", dup inode item");
318 if (errors & I_ERR_DUP_DIR_INDEX)
319 fprintf(stderr, ", dup dir index");
320 if (errors & I_ERR_ODD_DIR_ITEM)
321 fprintf(stderr, ", odd dir item");
322 if (errors & I_ERR_ODD_FILE_EXTENT)
323 fprintf(stderr, ", odd file extent");
324 if (errors & I_ERR_BAD_FILE_EXTENT)
325 fprintf(stderr, ", bad file extent");
326 if (errors & I_ERR_FILE_EXTENT_OVERLAP)
327 fprintf(stderr, ", file extent overlap");
328 if (errors & I_ERR_FILE_EXTENT_DISCOUNT)
329 fprintf(stderr, ", file extent discount");
330 if (errors & I_ERR_DIR_ISIZE_WRONG)
331 fprintf(stderr, ", dir isize wrong");
332 if (errors & I_ERR_FILE_NBYTES_WRONG)
333 fprintf(stderr, ", nbytes wrong");
334 if (errors & I_ERR_ODD_CSUM_ITEM)
335 fprintf(stderr, ", odd csum item");
336 if (errors & I_ERR_SOME_CSUM_MISSING)
337 fprintf(stderr, ", some csum missing");
338 if (errors & I_ERR_LINK_COUNT_WRONG)
339 fprintf(stderr, ", link count wrong");
340 fprintf(stderr, "\n");
343 static void print_ref_error(int errors)
345 if (errors & REF_ERR_NO_DIR_ITEM)
346 fprintf(stderr, ", no dir item");
347 if (errors & REF_ERR_NO_DIR_INDEX)
348 fprintf(stderr, ", no dir index");
349 if (errors & REF_ERR_NO_INODE_REF)
350 fprintf(stderr, ", no inode ref");
351 if (errors & REF_ERR_DUP_DIR_ITEM)
352 fprintf(stderr, ", dup dir item");
353 if (errors & REF_ERR_DUP_DIR_INDEX)
354 fprintf(stderr, ", dup dir index");
355 if (errors & REF_ERR_DUP_INODE_REF)
356 fprintf(stderr, ", dup inode ref");
357 if (errors & REF_ERR_INDEX_UNMATCH)
358 fprintf(stderr, ", index unmatch");
359 if (errors & REF_ERR_FILETYPE_UNMATCH)
360 fprintf(stderr, ", filetype unmatch");
361 if (errors & REF_ERR_NAME_TOO_LONG)
362 fprintf(stderr, ", name too long");
363 if (errors & REF_ERR_NO_ROOT_REF)
364 fprintf(stderr, ", no root ref");
365 if (errors & REF_ERR_NO_ROOT_BACKREF)
366 fprintf(stderr, ", no root backref");
367 if (errors & REF_ERR_DUP_ROOT_REF)
368 fprintf(stderr, ", dup root ref");
369 if (errors & REF_ERR_DUP_ROOT_BACKREF)
370 fprintf(stderr, ", dup root backref");
371 fprintf(stderr, "\n");
374 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
377 struct ptr_node *node;
378 struct cache_extent *cache;
379 struct inode_record *rec = NULL;
382 cache = lookup_cache_extent(inode_cache, ino, 1);
384 node = container_of(cache, struct ptr_node, cache);
386 if (mod && rec->refs > 1) {
387 node->data = clone_inode_rec(rec);
392 rec = calloc(1, sizeof(*rec));
394 rec->extent_start = (u64)-1;
395 rec->first_extent_gap = (u64)-1;
397 INIT_LIST_HEAD(&rec->backrefs);
399 node = malloc(sizeof(*node));
400 node->cache.start = ino;
401 node->cache.size = 1;
404 if (ino == BTRFS_FREE_INO_OBJECTID)
407 ret = insert_cache_extent(inode_cache, &node->cache);
413 static void free_inode_rec(struct inode_record *rec)
415 struct inode_backref *backref;
420 while (!list_empty(&rec->backrefs)) {
421 backref = list_entry(rec->backrefs.next,
422 struct inode_backref, list);
423 list_del(&backref->list);
429 static int can_free_inode_rec(struct inode_record *rec)
431 if (!rec->errors && rec->checked && rec->found_inode_item &&
432 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
437 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
438 struct inode_record *rec)
440 struct cache_extent *cache;
441 struct inode_backref *tmp, *backref;
442 struct ptr_node *node;
443 unsigned char filetype;
445 if (!rec->found_inode_item)
448 filetype = imode_to_type(rec->imode);
449 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
450 if (backref->found_dir_item && backref->found_dir_index) {
451 if (backref->filetype != filetype)
452 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
453 if (!backref->errors && backref->found_inode_ref) {
454 list_del(&backref->list);
460 if (!rec->checked || rec->merging)
463 if (S_ISDIR(rec->imode)) {
464 if (rec->found_size != rec->isize)
465 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
466 if (rec->found_file_extent)
467 rec->errors |= I_ERR_ODD_FILE_EXTENT;
468 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
469 if (rec->found_dir_item)
470 rec->errors |= I_ERR_ODD_DIR_ITEM;
471 if (rec->found_size != rec->nbytes)
472 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
473 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
474 rec->first_extent_gap = 0;
475 if (rec->nlink > 0 && !no_holes &&
476 (rec->extent_end < rec->isize ||
477 rec->first_extent_gap < rec->isize))
478 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
481 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
482 if (rec->found_csum_item && rec->nodatasum)
483 rec->errors |= I_ERR_ODD_CSUM_ITEM;
484 if (rec->some_csum_missing && !rec->nodatasum)
485 rec->errors |= I_ERR_SOME_CSUM_MISSING;
488 BUG_ON(rec->refs != 1);
489 if (can_free_inode_rec(rec)) {
490 cache = lookup_cache_extent(inode_cache, rec->ino, 1);
491 node = container_of(cache, struct ptr_node, cache);
492 BUG_ON(node->data != rec);
493 remove_cache_extent(inode_cache, &node->cache);
499 static int check_orphan_item(struct btrfs_root *root, u64 ino)
501 struct btrfs_path path;
502 struct btrfs_key key;
505 key.objectid = BTRFS_ORPHAN_OBJECTID;
506 key.type = BTRFS_ORPHAN_ITEM_KEY;
509 btrfs_init_path(&path);
510 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
511 btrfs_release_path(&path);
517 static int process_inode_item(struct extent_buffer *eb,
518 int slot, struct btrfs_key *key,
519 struct shared_node *active_node)
521 struct inode_record *rec;
522 struct btrfs_inode_item *item;
524 rec = active_node->current;
525 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
526 if (rec->found_inode_item) {
527 rec->errors |= I_ERR_DUP_INODE_ITEM;
530 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
531 rec->nlink = btrfs_inode_nlink(eb, item);
532 rec->isize = btrfs_inode_size(eb, item);
533 rec->nbytes = btrfs_inode_nbytes(eb, item);
534 rec->imode = btrfs_inode_mode(eb, item);
535 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
537 rec->found_inode_item = 1;
539 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
540 maybe_free_inode_rec(&active_node->inode_cache, rec);
544 static struct inode_backref *get_inode_backref(struct inode_record *rec,
546 int namelen, u64 dir)
548 struct inode_backref *backref;
550 list_for_each_entry(backref, &rec->backrefs, list) {
551 if (backref->dir != dir || backref->namelen != namelen)
553 if (memcmp(name, backref->name, namelen))
558 backref = malloc(sizeof(*backref) + namelen + 1);
559 memset(backref, 0, sizeof(*backref));
561 backref->namelen = namelen;
562 memcpy(backref->name, name, namelen);
563 backref->name[namelen] = '\0';
564 list_add_tail(&backref->list, &rec->backrefs);
568 static int add_inode_backref(struct cache_tree *inode_cache,
569 u64 ino, u64 dir, u64 index,
570 const char *name, int namelen,
571 int filetype, int itemtype, int errors)
573 struct inode_record *rec;
574 struct inode_backref *backref;
576 rec = get_inode_rec(inode_cache, ino, 1);
577 backref = get_inode_backref(rec, name, namelen, dir);
579 backref->errors |= errors;
580 if (itemtype == BTRFS_DIR_INDEX_KEY) {
581 if (backref->found_dir_index)
582 backref->errors |= REF_ERR_DUP_DIR_INDEX;
583 if (backref->found_inode_ref && backref->index != index)
584 backref->errors |= REF_ERR_INDEX_UNMATCH;
585 if (backref->found_dir_item && backref->filetype != filetype)
586 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
588 backref->index = index;
589 backref->filetype = filetype;
590 backref->found_dir_index = 1;
591 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
593 if (backref->found_dir_item)
594 backref->errors |= REF_ERR_DUP_DIR_ITEM;
595 if (backref->found_dir_index && backref->filetype != filetype)
596 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
598 backref->filetype = filetype;
599 backref->found_dir_item = 1;
600 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
601 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
602 if (backref->found_inode_ref)
603 backref->errors |= REF_ERR_DUP_INODE_REF;
604 if (backref->found_dir_index && backref->index != index)
605 backref->errors |= REF_ERR_INDEX_UNMATCH;
607 backref->ref_type = itemtype;
608 backref->index = index;
609 backref->found_inode_ref = 1;
614 maybe_free_inode_rec(inode_cache, rec);
618 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
619 struct cache_tree *dst_cache)
621 struct inode_backref *backref;
625 list_for_each_entry(backref, &src->backrefs, list) {
626 if (backref->found_dir_index) {
627 add_inode_backref(dst_cache, dst->ino, backref->dir,
628 backref->index, backref->name,
629 backref->namelen, backref->filetype,
630 BTRFS_DIR_INDEX_KEY, backref->errors);
632 if (backref->found_dir_item) {
634 add_inode_backref(dst_cache, dst->ino,
635 backref->dir, 0, backref->name,
636 backref->namelen, backref->filetype,
637 BTRFS_DIR_ITEM_KEY, backref->errors);
639 if (backref->found_inode_ref) {
640 add_inode_backref(dst_cache, dst->ino,
641 backref->dir, backref->index,
642 backref->name, backref->namelen, 0,
643 backref->ref_type, backref->errors);
647 if (src->found_dir_item)
648 dst->found_dir_item = 1;
649 if (src->found_file_extent)
650 dst->found_file_extent = 1;
651 if (src->found_csum_item)
652 dst->found_csum_item = 1;
653 if (src->some_csum_missing)
654 dst->some_csum_missing = 1;
655 if (dst->first_extent_gap > src->first_extent_gap)
656 dst->first_extent_gap = src->first_extent_gap;
658 BUG_ON(src->found_link < dir_count);
659 dst->found_link += src->found_link - dir_count;
660 dst->found_size += src->found_size;
661 if (src->extent_start != (u64)-1) {
662 if (dst->extent_start == (u64)-1) {
663 dst->extent_start = src->extent_start;
664 dst->extent_end = src->extent_end;
666 if (dst->extent_end > src->extent_start)
667 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
668 else if (dst->extent_end < src->extent_start &&
669 dst->extent_end < dst->first_extent_gap)
670 dst->first_extent_gap = dst->extent_end;
671 if (dst->extent_end < src->extent_end)
672 dst->extent_end = src->extent_end;
676 dst->errors |= src->errors;
677 if (src->found_inode_item) {
678 if (!dst->found_inode_item) {
679 dst->nlink = src->nlink;
680 dst->isize = src->isize;
681 dst->nbytes = src->nbytes;
682 dst->imode = src->imode;
683 dst->nodatasum = src->nodatasum;
684 dst->found_inode_item = 1;
686 dst->errors |= I_ERR_DUP_INODE_ITEM;
694 static int splice_shared_node(struct shared_node *src_node,
695 struct shared_node *dst_node)
697 struct cache_extent *cache;
698 struct ptr_node *node, *ins;
699 struct cache_tree *src, *dst;
700 struct inode_record *rec, *conflict;
705 if (--src_node->refs == 0)
707 if (src_node->current)
708 current_ino = src_node->current->ino;
710 src = &src_node->root_cache;
711 dst = &dst_node->root_cache;
713 cache = search_cache_extent(src, 0);
715 node = container_of(cache, struct ptr_node, cache);
717 cache = next_cache_extent(cache);
720 remove_cache_extent(src, &node->cache);
723 ins = malloc(sizeof(*ins));
724 ins->cache.start = node->cache.start;
725 ins->cache.size = node->cache.size;
729 ret = insert_cache_extent(dst, &ins->cache);
730 if (ret == -EEXIST) {
731 conflict = get_inode_rec(dst, rec->ino, 1);
732 merge_inode_recs(rec, conflict, dst);
734 conflict->checked = 1;
735 if (dst_node->current == conflict)
736 dst_node->current = NULL;
738 maybe_free_inode_rec(dst, conflict);
746 if (src == &src_node->root_cache) {
747 src = &src_node->inode_cache;
748 dst = &dst_node->inode_cache;
752 if (current_ino > 0 && (!dst_node->current ||
753 current_ino > dst_node->current->ino)) {
754 if (dst_node->current) {
755 dst_node->current->checked = 1;
756 maybe_free_inode_rec(dst, dst_node->current);
758 dst_node->current = get_inode_rec(dst, current_ino, 1);
763 static void free_inode_ptr(struct cache_extent *cache)
765 struct ptr_node *node;
766 struct inode_record *rec;
768 node = container_of(cache, struct ptr_node, cache);
774 FREE_EXTENT_CACHE_BASED_TREE(inode_recs, free_inode_ptr);
776 static struct shared_node *find_shared_node(struct cache_tree *shared,
779 struct cache_extent *cache;
780 struct shared_node *node;
782 cache = lookup_cache_extent(shared, bytenr, 1);
784 node = container_of(cache, struct shared_node, cache);
790 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
793 struct shared_node *node;
795 node = calloc(1, sizeof(*node));
796 node->cache.start = bytenr;
797 node->cache.size = 1;
798 cache_tree_init(&node->root_cache);
799 cache_tree_init(&node->inode_cache);
802 ret = insert_cache_extent(shared, &node->cache);
807 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
808 struct walk_control *wc, int level)
810 struct shared_node *node;
811 struct shared_node *dest;
813 if (level == wc->active_node)
816 BUG_ON(wc->active_node <= level);
817 node = find_shared_node(&wc->shared, bytenr);
819 add_shared_node(&wc->shared, bytenr, refs);
820 node = find_shared_node(&wc->shared, bytenr);
821 wc->nodes[level] = node;
822 wc->active_node = level;
826 if (wc->root_level == wc->active_node &&
827 btrfs_root_refs(&root->root_item) == 0) {
828 if (--node->refs == 0) {
829 free_inode_recs_tree(&node->root_cache);
830 free_inode_recs_tree(&node->inode_cache);
831 remove_cache_extent(&wc->shared, &node->cache);
837 dest = wc->nodes[wc->active_node];
838 splice_shared_node(node, dest);
839 if (node->refs == 0) {
840 remove_cache_extent(&wc->shared, &node->cache);
846 static int leave_shared_node(struct btrfs_root *root,
847 struct walk_control *wc, int level)
849 struct shared_node *node;
850 struct shared_node *dest;
853 if (level == wc->root_level)
856 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
860 BUG_ON(i >= BTRFS_MAX_LEVEL);
862 node = wc->nodes[wc->active_node];
863 wc->nodes[wc->active_node] = NULL;
866 dest = wc->nodes[wc->active_node];
867 if (wc->active_node < wc->root_level ||
868 btrfs_root_refs(&root->root_item) > 0) {
869 BUG_ON(node->refs <= 1);
870 splice_shared_node(node, dest);
872 BUG_ON(node->refs < 2);
878 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
881 struct btrfs_path path;
882 struct btrfs_key key;
883 struct extent_buffer *leaf;
887 btrfs_init_path(&path);
889 key.objectid = parent_root_id;
890 key.type = BTRFS_ROOT_REF_KEY;
891 key.offset = child_root_id;
892 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
896 btrfs_release_path(&path);
900 key.objectid = child_root_id;
901 key.type = BTRFS_ROOT_BACKREF_KEY;
903 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
909 leaf = path.nodes[0];
910 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
911 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
914 leaf = path.nodes[0];
917 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
918 if (key.objectid != child_root_id ||
919 key.type != BTRFS_ROOT_BACKREF_KEY)
924 if (key.offset == parent_root_id) {
925 btrfs_release_path(&path);
932 btrfs_release_path(&path);
935 return has_parent? 0 : -1;
938 static int process_dir_item(struct btrfs_root *root,
939 struct extent_buffer *eb,
940 int slot, struct btrfs_key *key,
941 struct shared_node *active_node)
951 struct btrfs_dir_item *di;
952 struct inode_record *rec;
953 struct cache_tree *root_cache;
954 struct cache_tree *inode_cache;
955 struct btrfs_key location;
956 char namebuf[BTRFS_NAME_LEN];
958 root_cache = &active_node->root_cache;
959 inode_cache = &active_node->inode_cache;
960 rec = active_node->current;
961 rec->found_dir_item = 1;
963 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
964 total = btrfs_item_size_nr(eb, slot);
965 while (cur < total) {
967 btrfs_dir_item_key_to_cpu(eb, di, &location);
968 name_len = btrfs_dir_name_len(eb, di);
969 data_len = btrfs_dir_data_len(eb, di);
970 filetype = btrfs_dir_type(eb, di);
972 rec->found_size += name_len;
973 if (name_len <= BTRFS_NAME_LEN) {
977 len = BTRFS_NAME_LEN;
978 error = REF_ERR_NAME_TOO_LONG;
980 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
982 if (location.type == BTRFS_INODE_ITEM_KEY) {
983 add_inode_backref(inode_cache, location.objectid,
984 key->objectid, key->offset, namebuf,
985 len, filetype, key->type, error);
986 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
987 add_inode_backref(root_cache, location.objectid,
988 key->objectid, key->offset,
989 namebuf, len, filetype,
992 fprintf(stderr, "warning line %d\n", __LINE__);
995 len = sizeof(*di) + name_len + data_len;
996 di = (struct btrfs_dir_item *)((char *)di + len);
999 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
1000 rec->errors |= I_ERR_DUP_DIR_INDEX;
1005 static int process_inode_ref(struct extent_buffer *eb,
1006 int slot, struct btrfs_key *key,
1007 struct shared_node *active_node)
1015 struct cache_tree *inode_cache;
1016 struct btrfs_inode_ref *ref;
1017 char namebuf[BTRFS_NAME_LEN];
1019 inode_cache = &active_node->inode_cache;
1021 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
1022 total = btrfs_item_size_nr(eb, slot);
1023 while (cur < total) {
1024 name_len = btrfs_inode_ref_name_len(eb, ref);
1025 index = btrfs_inode_ref_index(eb, ref);
1026 if (name_len <= BTRFS_NAME_LEN) {
1030 len = BTRFS_NAME_LEN;
1031 error = REF_ERR_NAME_TOO_LONG;
1033 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1034 add_inode_backref(inode_cache, key->objectid, key->offset,
1035 index, namebuf, len, 0, key->type, error);
1037 len = sizeof(*ref) + name_len;
1038 ref = (struct btrfs_inode_ref *)((char *)ref + len);
1044 static int process_inode_extref(struct extent_buffer *eb,
1045 int slot, struct btrfs_key *key,
1046 struct shared_node *active_node)
1055 struct cache_tree *inode_cache;
1056 struct btrfs_inode_extref *extref;
1057 char namebuf[BTRFS_NAME_LEN];
1059 inode_cache = &active_node->inode_cache;
1061 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
1062 total = btrfs_item_size_nr(eb, slot);
1063 while (cur < total) {
1064 name_len = btrfs_inode_extref_name_len(eb, extref);
1065 index = btrfs_inode_extref_index(eb, extref);
1066 parent = btrfs_inode_extref_parent(eb, extref);
1067 if (name_len <= BTRFS_NAME_LEN) {
1071 len = BTRFS_NAME_LEN;
1072 error = REF_ERR_NAME_TOO_LONG;
1074 read_extent_buffer(eb, namebuf,
1075 (unsigned long)(extref + 1), len);
1076 add_inode_backref(inode_cache, key->objectid, parent,
1077 index, namebuf, len, 0, key->type, error);
1079 len = sizeof(*extref) + name_len;
1080 extref = (struct btrfs_inode_extref *)((char *)extref + len);
1087 static int count_csum_range(struct btrfs_root *root, u64 start,
1088 u64 len, u64 *found)
1090 struct btrfs_key key;
1091 struct btrfs_path path;
1092 struct extent_buffer *leaf;
1097 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
1099 btrfs_init_path(&path);
1101 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
1103 key.type = BTRFS_EXTENT_CSUM_KEY;
1105 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
1109 if (ret > 0 && path.slots[0] > 0) {
1110 leaf = path.nodes[0];
1111 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1112 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1113 key.type == BTRFS_EXTENT_CSUM_KEY)
1118 leaf = path.nodes[0];
1119 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1120 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1125 leaf = path.nodes[0];
1128 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1129 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1130 key.type != BTRFS_EXTENT_CSUM_KEY)
1133 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1134 if (key.offset >= start + len)
1137 if (key.offset > start)
1140 size = btrfs_item_size_nr(leaf, path.slots[0]);
1141 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1142 if (csum_end > start) {
1143 size = min(csum_end - start, len);
1154 btrfs_release_path(&path);
1158 static int process_file_extent(struct btrfs_root *root,
1159 struct extent_buffer *eb,
1160 int slot, struct btrfs_key *key,
1161 struct shared_node *active_node)
1163 struct inode_record *rec;
1164 struct btrfs_file_extent_item *fi;
1166 u64 disk_bytenr = 0;
1167 u64 extent_offset = 0;
1168 u64 mask = root->sectorsize - 1;
1172 rec = active_node->current;
1173 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1174 rec->found_file_extent = 1;
1176 if (rec->extent_start == (u64)-1) {
1177 rec->extent_start = key->offset;
1178 rec->extent_end = key->offset;
1181 if (rec->extent_end > key->offset)
1182 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1183 else if (rec->extent_end < key->offset &&
1184 rec->extent_end < rec->first_extent_gap)
1185 rec->first_extent_gap = rec->extent_end;
1187 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1188 extent_type = btrfs_file_extent_type(eb, fi);
1190 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1191 num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1193 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1194 rec->found_size += num_bytes;
1195 num_bytes = (num_bytes + mask) & ~mask;
1196 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1197 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1198 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1199 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1200 extent_offset = btrfs_file_extent_offset(eb, fi);
1201 if (num_bytes == 0 || (num_bytes & mask))
1202 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1203 if (num_bytes + extent_offset >
1204 btrfs_file_extent_ram_bytes(eb, fi))
1205 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1206 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1207 (btrfs_file_extent_compression(eb, fi) ||
1208 btrfs_file_extent_encryption(eb, fi) ||
1209 btrfs_file_extent_other_encoding(eb, fi)))
1210 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1211 if (disk_bytenr > 0)
1212 rec->found_size += num_bytes;
1214 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1216 rec->extent_end = key->offset + num_bytes;
1218 if (disk_bytenr > 0) {
1220 if (btrfs_file_extent_compression(eb, fi))
1221 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1223 disk_bytenr += extent_offset;
1225 ret = count_csum_range(root, disk_bytenr, num_bytes, &found);
1228 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1230 rec->found_csum_item = 1;
1231 if (found < num_bytes)
1232 rec->some_csum_missing = 1;
1233 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1235 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1241 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1242 struct walk_control *wc)
1244 struct btrfs_key key;
1248 struct cache_tree *inode_cache;
1249 struct shared_node *active_node;
1251 if (wc->root_level == wc->active_node &&
1252 btrfs_root_refs(&root->root_item) == 0)
1255 active_node = wc->nodes[wc->active_node];
1256 inode_cache = &active_node->inode_cache;
1257 nritems = btrfs_header_nritems(eb);
1258 for (i = 0; i < nritems; i++) {
1259 btrfs_item_key_to_cpu(eb, &key, i);
1261 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1263 if (key.type == BTRFS_ORPHAN_ITEM_KEY)
1266 if (active_node->current == NULL ||
1267 active_node->current->ino < key.objectid) {
1268 if (active_node->current) {
1269 active_node->current->checked = 1;
1270 maybe_free_inode_rec(inode_cache,
1271 active_node->current);
1273 active_node->current = get_inode_rec(inode_cache,
1277 case BTRFS_DIR_ITEM_KEY:
1278 case BTRFS_DIR_INDEX_KEY:
1279 ret = process_dir_item(root, eb, i, &key, active_node);
1281 case BTRFS_INODE_REF_KEY:
1282 ret = process_inode_ref(eb, i, &key, active_node);
1284 case BTRFS_INODE_EXTREF_KEY:
1285 ret = process_inode_extref(eb, i, &key, active_node);
1287 case BTRFS_INODE_ITEM_KEY:
1288 ret = process_inode_item(eb, i, &key, active_node);
1290 case BTRFS_EXTENT_DATA_KEY:
1291 ret = process_file_extent(root, eb, i, &key,
1301 static void reada_walk_down(struct btrfs_root *root,
1302 struct extent_buffer *node, int slot)
1311 level = btrfs_header_level(node);
1315 nritems = btrfs_header_nritems(node);
1316 blocksize = btrfs_level_size(root, level - 1);
1317 for (i = slot; i < nritems; i++) {
1318 bytenr = btrfs_node_blockptr(node, i);
1319 ptr_gen = btrfs_node_ptr_generation(node, i);
1320 readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1324 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1325 struct walk_control *wc, int *level)
1329 struct extent_buffer *next;
1330 struct extent_buffer *cur;
1335 WARN_ON(*level < 0);
1336 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1337 ret = btrfs_lookup_extent_info(NULL, root,
1338 path->nodes[*level]->start,
1339 *level, 1, &refs, NULL);
1346 ret = enter_shared_node(root, path->nodes[*level]->start,
1354 while (*level >= 0) {
1355 WARN_ON(*level < 0);
1356 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1357 cur = path->nodes[*level];
1359 if (btrfs_header_level(cur) != *level)
1362 if (path->slots[*level] >= btrfs_header_nritems(cur))
1365 ret = process_one_leaf(root, cur, wc);
1370 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1371 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1372 blocksize = btrfs_level_size(root, *level - 1);
1373 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1379 ret = enter_shared_node(root, bytenr, refs,
1382 path->slots[*level]++;
1387 next = btrfs_find_tree_block(root, bytenr, blocksize);
1388 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1389 free_extent_buffer(next);
1390 reada_walk_down(root, cur, path->slots[*level]);
1391 next = read_tree_block(root, bytenr, blocksize,
1399 *level = *level - 1;
1400 free_extent_buffer(path->nodes[*level]);
1401 path->nodes[*level] = next;
1402 path->slots[*level] = 0;
1405 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1409 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1410 struct walk_control *wc, int *level)
1413 struct extent_buffer *leaf;
1415 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1416 leaf = path->nodes[i];
1417 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1422 free_extent_buffer(path->nodes[*level]);
1423 path->nodes[*level] = NULL;
1424 BUG_ON(*level > wc->active_node);
1425 if (*level == wc->active_node)
1426 leave_shared_node(root, wc, *level);
1433 static int check_root_dir(struct inode_record *rec)
1435 struct inode_backref *backref;
1438 if (!rec->found_inode_item || rec->errors)
1440 if (rec->nlink != 1 || rec->found_link != 0)
1442 if (list_empty(&rec->backrefs))
1444 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1445 if (!backref->found_inode_ref)
1447 if (backref->index != 0 || backref->namelen != 2 ||
1448 memcmp(backref->name, "..", 2))
1450 if (backref->found_dir_index || backref->found_dir_item)
1457 static int repair_inode_isize(struct btrfs_trans_handle *trans,
1458 struct btrfs_root *root, struct btrfs_path *path,
1459 struct inode_record *rec)
1461 struct btrfs_inode_item *ei;
1462 struct btrfs_key key;
1465 key.objectid = rec->ino;
1466 key.type = BTRFS_INODE_ITEM_KEY;
1467 key.offset = (u64)-1;
1469 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1473 if (!path->slots[0]) {
1480 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1481 if (key.objectid != rec->ino) {
1486 ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
1487 struct btrfs_inode_item);
1488 btrfs_set_inode_size(path->nodes[0], ei, rec->found_size);
1489 btrfs_mark_buffer_dirty(path->nodes[0]);
1490 rec->errors &= ~I_ERR_DIR_ISIZE_WRONG;
1491 printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1492 root->root_key.objectid);
1494 btrfs_release_path(path);
1498 static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
1499 struct btrfs_root *root,
1500 struct btrfs_path *path,
1501 struct inode_record *rec)
1503 struct btrfs_key key;
1506 key.objectid = BTRFS_ORPHAN_OBJECTID;
1507 key.type = BTRFS_ORPHAN_ITEM_KEY;
1508 key.offset = rec->ino;
1510 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1511 btrfs_release_path(path);
1513 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1517 static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1519 struct btrfs_trans_handle *trans;
1520 struct btrfs_path *path;
1523 /* So far we just fix dir isize wrong */
1524 if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1527 path = btrfs_alloc_path();
1531 trans = btrfs_start_transaction(root, 1);
1532 if (IS_ERR(trans)) {
1533 btrfs_free_path(path);
1534 return PTR_ERR(trans);
1537 if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
1538 ret = repair_inode_isize(trans, root, path, rec);
1539 if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
1540 ret = repair_inode_orphan_item(trans, root, path, rec);
1541 btrfs_commit_transaction(trans, root);
1542 btrfs_free_path(path);
1546 static int check_inode_recs(struct btrfs_root *root,
1547 struct cache_tree *inode_cache)
1549 struct cache_extent *cache;
1550 struct ptr_node *node;
1551 struct inode_record *rec;
1552 struct inode_backref *backref;
1555 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1557 if (btrfs_root_refs(&root->root_item) == 0) {
1558 if (!cache_tree_empty(inode_cache))
1559 fprintf(stderr, "warning line %d\n", __LINE__);
1563 rec = get_inode_rec(inode_cache, root_dirid, 0);
1565 ret = check_root_dir(rec);
1567 fprintf(stderr, "root %llu root dir %llu error\n",
1568 (unsigned long long)root->root_key.objectid,
1569 (unsigned long long)root_dirid);
1573 fprintf(stderr, "root %llu root dir %llu not found\n",
1574 (unsigned long long)root->root_key.objectid,
1575 (unsigned long long)root_dirid);
1579 cache = search_cache_extent(inode_cache, 0);
1582 node = container_of(cache, struct ptr_node, cache);
1584 remove_cache_extent(inode_cache, &node->cache);
1586 if (rec->ino == root_dirid ||
1587 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1588 free_inode_rec(rec);
1592 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1593 ret = check_orphan_item(root, rec->ino);
1595 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1596 if (can_free_inode_rec(rec)) {
1597 free_inode_rec(rec);
1603 ret = try_repair_inode(root, rec);
1604 if (ret == 0 && can_free_inode_rec(rec)) {
1605 free_inode_rec(rec);
1612 if (!rec->found_inode_item)
1613 rec->errors |= I_ERR_NO_INODE_ITEM;
1614 if (rec->found_link != rec->nlink)
1615 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1616 fprintf(stderr, "root %llu inode %llu errors %x",
1617 (unsigned long long) root->root_key.objectid,
1618 (unsigned long long) rec->ino, rec->errors);
1619 print_inode_error(rec->errors);
1620 list_for_each_entry(backref, &rec->backrefs, list) {
1621 if (!backref->found_dir_item)
1622 backref->errors |= REF_ERR_NO_DIR_ITEM;
1623 if (!backref->found_dir_index)
1624 backref->errors |= REF_ERR_NO_DIR_INDEX;
1625 if (!backref->found_inode_ref)
1626 backref->errors |= REF_ERR_NO_INODE_REF;
1627 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1628 " namelen %u name %s filetype %d errors %x",
1629 (unsigned long long)backref->dir,
1630 (unsigned long long)backref->index,
1631 backref->namelen, backref->name,
1632 backref->filetype, backref->errors);
1633 print_ref_error(backref->errors);
1635 free_inode_rec(rec);
1637 return (error > 0) ? -1 : 0;
1640 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1643 struct cache_extent *cache;
1644 struct root_record *rec = NULL;
1647 cache = lookup_cache_extent(root_cache, objectid, 1);
1649 rec = container_of(cache, struct root_record, cache);
1651 rec = calloc(1, sizeof(*rec));
1652 rec->objectid = objectid;
1653 INIT_LIST_HEAD(&rec->backrefs);
1654 rec->cache.start = objectid;
1655 rec->cache.size = 1;
1657 ret = insert_cache_extent(root_cache, &rec->cache);
1663 static struct root_backref *get_root_backref(struct root_record *rec,
1664 u64 ref_root, u64 dir, u64 index,
1665 const char *name, int namelen)
1667 struct root_backref *backref;
1669 list_for_each_entry(backref, &rec->backrefs, list) {
1670 if (backref->ref_root != ref_root || backref->dir != dir ||
1671 backref->namelen != namelen)
1673 if (memcmp(name, backref->name, namelen))
1678 backref = malloc(sizeof(*backref) + namelen + 1);
1679 memset(backref, 0, sizeof(*backref));
1680 backref->ref_root = ref_root;
1682 backref->index = index;
1683 backref->namelen = namelen;
1684 memcpy(backref->name, name, namelen);
1685 backref->name[namelen] = '\0';
1686 list_add_tail(&backref->list, &rec->backrefs);
1690 static void free_root_record(struct cache_extent *cache)
1692 struct root_record *rec;
1693 struct root_backref *backref;
1695 rec = container_of(cache, struct root_record, cache);
1696 while (!list_empty(&rec->backrefs)) {
1697 backref = list_entry(rec->backrefs.next,
1698 struct root_backref, list);
1699 list_del(&backref->list);
1706 FREE_EXTENT_CACHE_BASED_TREE(root_recs, free_root_record);
1708 static int add_root_backref(struct cache_tree *root_cache,
1709 u64 root_id, u64 ref_root, u64 dir, u64 index,
1710 const char *name, int namelen,
1711 int item_type, int errors)
1713 struct root_record *rec;
1714 struct root_backref *backref;
1716 rec = get_root_rec(root_cache, root_id);
1717 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1719 backref->errors |= errors;
1721 if (item_type != BTRFS_DIR_ITEM_KEY) {
1722 if (backref->found_dir_index || backref->found_back_ref ||
1723 backref->found_forward_ref) {
1724 if (backref->index != index)
1725 backref->errors |= REF_ERR_INDEX_UNMATCH;
1727 backref->index = index;
1731 if (item_type == BTRFS_DIR_ITEM_KEY) {
1732 if (backref->found_forward_ref)
1734 backref->found_dir_item = 1;
1735 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1736 backref->found_dir_index = 1;
1737 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1738 if (backref->found_forward_ref)
1739 backref->errors |= REF_ERR_DUP_ROOT_REF;
1740 else if (backref->found_dir_item)
1742 backref->found_forward_ref = 1;
1743 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1744 if (backref->found_back_ref)
1745 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1746 backref->found_back_ref = 1;
1751 if (backref->found_forward_ref && backref->found_dir_item)
1752 backref->reachable = 1;
1756 static int merge_root_recs(struct btrfs_root *root,
1757 struct cache_tree *src_cache,
1758 struct cache_tree *dst_cache)
1760 struct cache_extent *cache;
1761 struct ptr_node *node;
1762 struct inode_record *rec;
1763 struct inode_backref *backref;
1766 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1767 free_inode_recs_tree(src_cache);
1772 cache = search_cache_extent(src_cache, 0);
1775 node = container_of(cache, struct ptr_node, cache);
1777 remove_cache_extent(src_cache, &node->cache);
1780 ret = is_child_root(root, root->objectid, rec->ino);
1786 list_for_each_entry(backref, &rec->backrefs, list) {
1787 BUG_ON(backref->found_inode_ref);
1788 if (backref->found_dir_item)
1789 add_root_backref(dst_cache, rec->ino,
1790 root->root_key.objectid, backref->dir,
1791 backref->index, backref->name,
1792 backref->namelen, BTRFS_DIR_ITEM_KEY,
1794 if (backref->found_dir_index)
1795 add_root_backref(dst_cache, rec->ino,
1796 root->root_key.objectid, backref->dir,
1797 backref->index, backref->name,
1798 backref->namelen, BTRFS_DIR_INDEX_KEY,
1802 free_inode_rec(rec);
1809 static int check_root_refs(struct btrfs_root *root,
1810 struct cache_tree *root_cache)
1812 struct root_record *rec;
1813 struct root_record *ref_root;
1814 struct root_backref *backref;
1815 struct cache_extent *cache;
1821 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1824 /* fixme: this can not detect circular references */
1827 cache = search_cache_extent(root_cache, 0);
1831 rec = container_of(cache, struct root_record, cache);
1832 cache = next_cache_extent(cache);
1834 if (rec->found_ref == 0)
1837 list_for_each_entry(backref, &rec->backrefs, list) {
1838 if (!backref->reachable)
1841 ref_root = get_root_rec(root_cache,
1843 if (ref_root->found_ref > 0)
1846 backref->reachable = 0;
1848 if (rec->found_ref == 0)
1854 cache = search_cache_extent(root_cache, 0);
1858 rec = container_of(cache, struct root_record, cache);
1859 cache = next_cache_extent(cache);
1861 if (rec->found_ref == 0 &&
1862 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1863 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1864 ret = check_orphan_item(root->fs_info->tree_root,
1870 * If we don't have a root item then we likely just have
1871 * a dir item in a snapshot for this root but no actual
1872 * ref key or anything so it's meaningless.
1874 if (!rec->found_root_item)
1877 fprintf(stderr, "fs tree %llu not referenced\n",
1878 (unsigned long long)rec->objectid);
1882 if (rec->found_ref > 0 && !rec->found_root_item)
1884 list_for_each_entry(backref, &rec->backrefs, list) {
1885 if (!backref->found_dir_item)
1886 backref->errors |= REF_ERR_NO_DIR_ITEM;
1887 if (!backref->found_dir_index)
1888 backref->errors |= REF_ERR_NO_DIR_INDEX;
1889 if (!backref->found_back_ref)
1890 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1891 if (!backref->found_forward_ref)
1892 backref->errors |= REF_ERR_NO_ROOT_REF;
1893 if (backref->reachable && backref->errors)
1900 fprintf(stderr, "fs tree %llu refs %u %s\n",
1901 (unsigned long long)rec->objectid, rec->found_ref,
1902 rec->found_root_item ? "" : "not found");
1904 list_for_each_entry(backref, &rec->backrefs, list) {
1905 if (!backref->reachable)
1907 if (!backref->errors && rec->found_root_item)
1909 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1910 " index %llu namelen %u name %s errors %x\n",
1911 (unsigned long long)backref->ref_root,
1912 (unsigned long long)backref->dir,
1913 (unsigned long long)backref->index,
1914 backref->namelen, backref->name,
1916 print_ref_error(backref->errors);
1919 return errors > 0 ? 1 : 0;
1922 static int process_root_ref(struct extent_buffer *eb, int slot,
1923 struct btrfs_key *key,
1924 struct cache_tree *root_cache)
1930 struct btrfs_root_ref *ref;
1931 char namebuf[BTRFS_NAME_LEN];
1934 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1936 dirid = btrfs_root_ref_dirid(eb, ref);
1937 index = btrfs_root_ref_sequence(eb, ref);
1938 name_len = btrfs_root_ref_name_len(eb, ref);
1940 if (name_len <= BTRFS_NAME_LEN) {
1944 len = BTRFS_NAME_LEN;
1945 error = REF_ERR_NAME_TOO_LONG;
1947 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1949 if (key->type == BTRFS_ROOT_REF_KEY) {
1950 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1951 index, namebuf, len, key->type, error);
1953 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1954 index, namebuf, len, key->type, error);
1959 static int check_fs_root(struct btrfs_root *root,
1960 struct cache_tree *root_cache,
1961 struct walk_control *wc)
1967 struct btrfs_path path;
1968 struct shared_node root_node;
1969 struct root_record *rec;
1970 struct btrfs_root_item *root_item = &root->root_item;
1972 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1973 rec = get_root_rec(root_cache, root->root_key.objectid);
1974 if (btrfs_root_refs(root_item) > 0)
1975 rec->found_root_item = 1;
1978 btrfs_init_path(&path);
1979 memset(&root_node, 0, sizeof(root_node));
1980 cache_tree_init(&root_node.root_cache);
1981 cache_tree_init(&root_node.inode_cache);
1983 level = btrfs_header_level(root->node);
1984 memset(wc->nodes, 0, sizeof(wc->nodes));
1985 wc->nodes[level] = &root_node;
1986 wc->active_node = level;
1987 wc->root_level = level;
1989 if (btrfs_root_refs(root_item) > 0 ||
1990 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1991 path.nodes[level] = root->node;
1992 extent_buffer_get(root->node);
1993 path.slots[level] = 0;
1995 struct btrfs_key key;
1996 struct btrfs_disk_key found_key;
1998 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1999 level = root_item->drop_level;
2000 path.lowest_level = level;
2001 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
2004 btrfs_node_key(path.nodes[level], &found_key,
2006 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2007 sizeof(found_key)));
2011 wret = walk_down_tree(root, &path, wc, &level);
2017 wret = walk_up_tree(root, &path, wc, &level);
2024 btrfs_release_path(&path);
2026 err = merge_root_recs(root, &root_node.root_cache, root_cache);
2030 if (root_node.current) {
2031 root_node.current->checked = 1;
2032 maybe_free_inode_rec(&root_node.inode_cache,
2036 err = check_inode_recs(root, &root_node.inode_cache);
2042 static int fs_root_objectid(u64 objectid)
2044 if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
2045 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2047 return is_fstree(objectid);
2050 static int check_fs_roots(struct btrfs_root *root,
2051 struct cache_tree *root_cache)
2053 struct btrfs_path path;
2054 struct btrfs_key key;
2055 struct walk_control wc;
2056 struct extent_buffer *leaf;
2057 struct btrfs_root *tmp_root;
2058 struct btrfs_root *tree_root = root->fs_info->tree_root;
2063 * Just in case we made any changes to the extent tree that weren't
2064 * reflected into the free space cache yet.
2067 reset_cached_block_groups(root->fs_info);
2068 memset(&wc, 0, sizeof(wc));
2069 cache_tree_init(&wc.shared);
2070 btrfs_init_path(&path);
2074 key.type = BTRFS_ROOT_ITEM_KEY;
2075 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
2081 leaf = path.nodes[0];
2082 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2083 ret = btrfs_next_leaf(tree_root, &path);
2089 leaf = path.nodes[0];
2091 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2092 if (key.type == BTRFS_ROOT_ITEM_KEY &&
2093 fs_root_objectid(key.objectid)) {
2094 key.offset = (u64)-1;
2095 tmp_root = btrfs_read_fs_root(root->fs_info, &key);
2096 if (IS_ERR(tmp_root)) {
2100 ret = check_fs_root(tmp_root, root_cache, &wc);
2103 } else if (key.type == BTRFS_ROOT_REF_KEY ||
2104 key.type == BTRFS_ROOT_BACKREF_KEY) {
2105 process_root_ref(leaf, path.slots[0], &key,
2112 btrfs_release_path(&path);
2114 free_extent_cache_tree(&wc.shared);
2115 if (!cache_tree_empty(&wc.shared))
2116 fprintf(stderr, "warning line %d\n", __LINE__);
2121 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
2123 struct list_head *cur = rec->backrefs.next;
2124 struct extent_backref *back;
2125 struct tree_backref *tback;
2126 struct data_backref *dback;
2130 while(cur != &rec->backrefs) {
2131 back = list_entry(cur, struct extent_backref, list);
2133 if (!back->found_extent_tree) {
2137 if (back->is_data) {
2138 dback = (struct data_backref *)back;
2139 fprintf(stderr, "Backref %llu %s %llu"
2140 " owner %llu offset %llu num_refs %lu"
2141 " not found in extent tree\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 (unsigned long)dback->num_refs);
2152 tback = (struct tree_backref *)back;
2153 fprintf(stderr, "Backref %llu parent %llu"
2154 " root %llu not found in extent tree\n",
2155 (unsigned long long)rec->start,
2156 (unsigned long long)tback->parent,
2157 (unsigned long long)tback->root);
2160 if (!back->is_data && !back->found_ref) {
2164 tback = (struct tree_backref *)back;
2165 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
2166 (unsigned long long)rec->start,
2167 back->full_backref ? "parent" : "root",
2168 back->full_backref ?
2169 (unsigned long long)tback->parent :
2170 (unsigned long long)tback->root, back);
2172 if (back->is_data) {
2173 dback = (struct data_backref *)back;
2174 if (dback->found_ref != dback->num_refs) {
2178 fprintf(stderr, "Incorrect local backref count"
2179 " on %llu %s %llu owner %llu"
2180 " offset %llu found %u wanted %u back %p\n",
2181 (unsigned long long)rec->start,
2182 back->full_backref ?
2184 back->full_backref ?
2185 (unsigned long long)dback->parent:
2186 (unsigned long long)dback->root,
2187 (unsigned long long)dback->owner,
2188 (unsigned long long)dback->offset,
2189 dback->found_ref, dback->num_refs, back);
2191 if (dback->disk_bytenr != rec->start) {
2195 fprintf(stderr, "Backref disk bytenr does not"
2196 " match extent record, bytenr=%llu, "
2197 "ref bytenr=%llu\n",
2198 (unsigned long long)rec->start,
2199 (unsigned long long)dback->disk_bytenr);
2202 if (dback->bytes != rec->nr) {
2206 fprintf(stderr, "Backref bytes do not match "
2207 "extent backref, bytenr=%llu, ref "
2208 "bytes=%llu, backref bytes=%llu\n",
2209 (unsigned long long)rec->start,
2210 (unsigned long long)rec->nr,
2211 (unsigned long long)dback->bytes);
2214 if (!back->is_data) {
2217 dback = (struct data_backref *)back;
2218 found += dback->found_ref;
2221 if (found != rec->refs) {
2225 fprintf(stderr, "Incorrect global backref count "
2226 "on %llu found %llu wanted %llu\n",
2227 (unsigned long long)rec->start,
2228 (unsigned long long)found,
2229 (unsigned long long)rec->refs);
2235 static int free_all_extent_backrefs(struct extent_record *rec)
2237 struct extent_backref *back;
2238 struct list_head *cur;
2239 while (!list_empty(&rec->backrefs)) {
2240 cur = rec->backrefs.next;
2241 back = list_entry(cur, struct extent_backref, list);
2248 static void free_extent_record_cache(struct btrfs_fs_info *fs_info,
2249 struct cache_tree *extent_cache)
2251 struct cache_extent *cache;
2252 struct extent_record *rec;
2255 cache = first_cache_extent(extent_cache);
2258 rec = container_of(cache, struct extent_record, cache);
2259 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2260 remove_cache_extent(extent_cache, cache);
2261 free_all_extent_backrefs(rec);
2266 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2267 struct extent_record *rec)
2269 if (rec->content_checked && rec->owner_ref_checked &&
2270 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2271 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2272 remove_cache_extent(extent_cache, &rec->cache);
2273 free_all_extent_backrefs(rec);
2274 list_del_init(&rec->list);
2280 static int check_owner_ref(struct btrfs_root *root,
2281 struct extent_record *rec,
2282 struct extent_buffer *buf)
2284 struct extent_backref *node;
2285 struct tree_backref *back;
2286 struct btrfs_root *ref_root;
2287 struct btrfs_key key;
2288 struct btrfs_path path;
2289 struct extent_buffer *parent;
2294 list_for_each_entry(node, &rec->backrefs, list) {
2297 if (!node->found_ref)
2299 if (node->full_backref)
2301 back = (struct tree_backref *)node;
2302 if (btrfs_header_owner(buf) == back->root)
2305 BUG_ON(rec->is_root);
2307 /* try to find the block by search corresponding fs tree */
2308 key.objectid = btrfs_header_owner(buf);
2309 key.type = BTRFS_ROOT_ITEM_KEY;
2310 key.offset = (u64)-1;
2312 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2313 if (IS_ERR(ref_root))
2316 level = btrfs_header_level(buf);
2318 btrfs_item_key_to_cpu(buf, &key, 0);
2320 btrfs_node_key_to_cpu(buf, &key, 0);
2322 btrfs_init_path(&path);
2323 path.lowest_level = level + 1;
2324 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2328 parent = path.nodes[level + 1];
2329 if (parent && buf->start == btrfs_node_blockptr(parent,
2330 path.slots[level + 1]))
2333 btrfs_release_path(&path);
2334 return found ? 0 : 1;
2337 static int is_extent_tree_record(struct extent_record *rec)
2339 struct list_head *cur = rec->backrefs.next;
2340 struct extent_backref *node;
2341 struct tree_backref *back;
2344 while(cur != &rec->backrefs) {
2345 node = list_entry(cur, struct extent_backref, list);
2349 back = (struct tree_backref *)node;
2350 if (node->full_backref)
2352 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2359 static int record_bad_block_io(struct btrfs_fs_info *info,
2360 struct cache_tree *extent_cache,
2363 struct extent_record *rec;
2364 struct cache_extent *cache;
2365 struct btrfs_key key;
2367 cache = lookup_cache_extent(extent_cache, start, len);
2371 rec = container_of(cache, struct extent_record, cache);
2372 if (!is_extent_tree_record(rec))
2375 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2376 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2379 static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
2380 struct extent_buffer *buf, int slot)
2382 if (btrfs_header_level(buf)) {
2383 struct btrfs_key_ptr ptr1, ptr2;
2385 read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
2386 sizeof(struct btrfs_key_ptr));
2387 read_extent_buffer(buf, &ptr2,
2388 btrfs_node_key_ptr_offset(slot + 1),
2389 sizeof(struct btrfs_key_ptr));
2390 write_extent_buffer(buf, &ptr1,
2391 btrfs_node_key_ptr_offset(slot + 1),
2392 sizeof(struct btrfs_key_ptr));
2393 write_extent_buffer(buf, &ptr2,
2394 btrfs_node_key_ptr_offset(slot),
2395 sizeof(struct btrfs_key_ptr));
2397 struct btrfs_disk_key key;
2398 btrfs_node_key(buf, &key, 0);
2399 btrfs_fixup_low_keys(root, path, &key,
2400 btrfs_header_level(buf) + 1);
2403 struct btrfs_item *item1, *item2;
2404 struct btrfs_key k1, k2;
2405 char *item1_data, *item2_data;
2406 u32 item1_offset, item2_offset, item1_size, item2_size;
2408 item1 = btrfs_item_nr(slot);
2409 item2 = btrfs_item_nr(slot + 1);
2410 btrfs_item_key_to_cpu(buf, &k1, slot);
2411 btrfs_item_key_to_cpu(buf, &k2, slot + 1);
2412 item1_offset = btrfs_item_offset(buf, item1);
2413 item2_offset = btrfs_item_offset(buf, item2);
2414 item1_size = btrfs_item_size(buf, item1);
2415 item2_size = btrfs_item_size(buf, item2);
2417 item1_data = malloc(item1_size);
2420 item2_data = malloc(item2_size);
2426 read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2427 read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2429 write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2430 write_extent_buffer(buf, item2_data, item1_offset, item1_size);
2434 btrfs_set_item_offset(buf, item1, item2_offset);
2435 btrfs_set_item_offset(buf, item2, item1_offset);
2436 btrfs_set_item_size(buf, item1, item2_size);
2437 btrfs_set_item_size(buf, item2, item1_size);
2439 path->slots[0] = slot;
2440 btrfs_set_item_key_unsafe(root, path, &k2);
2441 path->slots[0] = slot + 1;
2442 btrfs_set_item_key_unsafe(root, path, &k1);
2448 * Attempt to fix basic block failures. Currently we only handle bad key
2449 * orders, we will cycle through the keys and swap them if necessary.
2451 static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
2452 struct btrfs_root *root,
2453 struct extent_buffer *buf,
2454 struct btrfs_disk_key *parent_key,
2455 enum btrfs_tree_block_status status)
2457 struct btrfs_path *path;
2458 struct btrfs_key k1, k2;
2463 if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
2466 k1.objectid = btrfs_header_owner(buf);
2467 k1.type = BTRFS_ROOT_ITEM_KEY;
2468 k1.offset = (u64)-1;
2470 root = btrfs_read_fs_root(root->fs_info, &k1);
2474 record_root_in_trans(trans, root);
2476 path = btrfs_alloc_path();
2480 level = btrfs_header_level(buf);
2481 path->lowest_level = level;
2482 path->skip_check_block = 1;
2484 btrfs_node_key_to_cpu(buf, &k1, 0);
2486 btrfs_item_key_to_cpu(buf, &k1, 0);
2488 ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
2490 btrfs_free_path(path);
2494 buf = path->nodes[level];
2495 for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
2497 btrfs_node_key_to_cpu(buf, &k1, i);
2498 btrfs_node_key_to_cpu(buf, &k2, i + 1);
2500 btrfs_item_key_to_cpu(buf, &k1, i);
2501 btrfs_item_key_to_cpu(buf, &k2, i + 1);
2503 if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2505 ret = swap_values(root, path, buf, i);
2508 btrfs_mark_buffer_dirty(buf);
2512 btrfs_free_path(path);
2516 static int check_block(struct btrfs_trans_handle *trans,
2517 struct btrfs_root *root,
2518 struct cache_tree *extent_cache,
2519 struct extent_buffer *buf, u64 flags)
2521 struct extent_record *rec;
2522 struct cache_extent *cache;
2523 struct btrfs_key key;
2524 enum btrfs_tree_block_status status;
2528 cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2531 rec = container_of(cache, struct extent_record, cache);
2532 rec->generation = btrfs_header_generation(buf);
2534 level = btrfs_header_level(buf);
2535 if (btrfs_header_nritems(buf) > 0) {
2538 btrfs_item_key_to_cpu(buf, &key, 0);
2540 btrfs_node_key_to_cpu(buf, &key, 0);
2542 rec->info_objectid = key.objectid;
2544 rec->info_level = level;
2546 if (btrfs_is_leaf(buf))
2547 status = btrfs_check_leaf(root, &rec->parent_key, buf);
2549 status = btrfs_check_node(root, &rec->parent_key, buf);
2551 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2553 status = try_to_fix_bad_block(trans, root, buf,
2556 if (status != BTRFS_TREE_BLOCK_CLEAN) {
2558 fprintf(stderr, "bad block %llu\n",
2559 (unsigned long long)buf->start);
2562 * Signal to callers we need to start the scan over
2563 * again since we'll have cow'ed blocks.
2568 rec->content_checked = 1;
2569 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2570 rec->owner_ref_checked = 1;
2572 ret = check_owner_ref(root, rec, buf);
2574 rec->owner_ref_checked = 1;
2578 maybe_free_extent_rec(extent_cache, rec);
2582 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2583 u64 parent, u64 root)
2585 struct list_head *cur = rec->backrefs.next;
2586 struct extent_backref *node;
2587 struct tree_backref *back;
2589 while(cur != &rec->backrefs) {
2590 node = list_entry(cur, struct extent_backref, list);
2594 back = (struct tree_backref *)node;
2596 if (!node->full_backref)
2598 if (parent == back->parent)
2601 if (node->full_backref)
2603 if (back->root == root)
2610 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2611 u64 parent, u64 root)
2613 struct tree_backref *ref = malloc(sizeof(*ref));
2614 memset(&ref->node, 0, sizeof(ref->node));
2616 ref->parent = parent;
2617 ref->node.full_backref = 1;
2620 ref->node.full_backref = 0;
2622 list_add_tail(&ref->node.list, &rec->backrefs);
2627 static struct data_backref *find_data_backref(struct extent_record *rec,
2628 u64 parent, u64 root,
2629 u64 owner, u64 offset,
2631 u64 disk_bytenr, u64 bytes)
2633 struct list_head *cur = rec->backrefs.next;
2634 struct extent_backref *node;
2635 struct data_backref *back;
2637 while(cur != &rec->backrefs) {
2638 node = list_entry(cur, struct extent_backref, list);
2642 back = (struct data_backref *)node;
2644 if (!node->full_backref)
2646 if (parent == back->parent)
2649 if (node->full_backref)
2651 if (back->root == root && back->owner == owner &&
2652 back->offset == offset) {
2653 if (found_ref && node->found_ref &&
2654 (back->bytes != bytes ||
2655 back->disk_bytenr != disk_bytenr))
2664 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2665 u64 parent, u64 root,
2666 u64 owner, u64 offset,
2669 struct data_backref *ref = malloc(sizeof(*ref));
2670 memset(&ref->node, 0, sizeof(ref->node));
2671 ref->node.is_data = 1;
2674 ref->parent = parent;
2677 ref->node.full_backref = 1;
2681 ref->offset = offset;
2682 ref->node.full_backref = 0;
2684 ref->bytes = max_size;
2687 list_add_tail(&ref->node.list, &rec->backrefs);
2688 if (max_size > rec->max_size)
2689 rec->max_size = max_size;
2693 static int add_extent_rec(struct cache_tree *extent_cache,
2694 struct btrfs_key *parent_key, u64 parent_gen,
2695 u64 start, u64 nr, u64 extent_item_refs,
2696 int is_root, int inc_ref, int set_checked,
2697 int metadata, int extent_rec, u64 max_size)
2699 struct extent_record *rec;
2700 struct cache_extent *cache;
2704 cache = lookup_cache_extent(extent_cache, start, nr);
2706 rec = container_of(cache, struct extent_record, cache);
2710 rec->nr = max(nr, max_size);
2713 * We need to make sure to reset nr to whatever the extent
2714 * record says was the real size, this way we can compare it to
2718 if (start != rec->start || rec->found_rec) {
2719 struct extent_record *tmp;
2722 if (list_empty(&rec->list))
2723 list_add_tail(&rec->list,
2724 &duplicate_extents);
2727 * We have to do this song and dance in case we
2728 * find an extent record that falls inside of
2729 * our current extent record but does not have
2730 * the same objectid.
2732 tmp = malloc(sizeof(*tmp));
2736 tmp->max_size = max_size;
2739 tmp->metadata = metadata;
2740 tmp->extent_item_refs = extent_item_refs;
2741 INIT_LIST_HEAD(&tmp->list);
2742 list_add_tail(&tmp->list, &rec->dups);
2743 rec->num_duplicates++;
2750 if (extent_item_refs && !dup) {
2751 if (rec->extent_item_refs) {
2752 fprintf(stderr, "block %llu rec "
2753 "extent_item_refs %llu, passed %llu\n",
2754 (unsigned long long)start,
2755 (unsigned long long)
2756 rec->extent_item_refs,
2757 (unsigned long long)extent_item_refs);
2759 rec->extent_item_refs = extent_item_refs;
2764 rec->content_checked = 1;
2765 rec->owner_ref_checked = 1;
2769 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2771 rec->parent_generation = parent_gen;
2773 if (rec->max_size < max_size)
2774 rec->max_size = max_size;
2776 maybe_free_extent_rec(extent_cache, rec);
2779 rec = malloc(sizeof(*rec));
2781 rec->max_size = max_size;
2782 rec->nr = max(nr, max_size);
2783 rec->found_rec = !!extent_rec;
2784 rec->content_checked = 0;
2785 rec->owner_ref_checked = 0;
2786 rec->num_duplicates = 0;
2787 rec->metadata = metadata;
2788 INIT_LIST_HEAD(&rec->backrefs);
2789 INIT_LIST_HEAD(&rec->dups);
2790 INIT_LIST_HEAD(&rec->list);
2802 if (extent_item_refs)
2803 rec->extent_item_refs = extent_item_refs;
2805 rec->extent_item_refs = 0;
2808 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2810 memset(&rec->parent_key, 0, sizeof(*parent_key));
2813 rec->parent_generation = parent_gen;
2815 rec->parent_generation = 0;
2817 rec->cache.start = start;
2818 rec->cache.size = nr;
2819 ret = insert_cache_extent(extent_cache, &rec->cache);
2823 rec->content_checked = 1;
2824 rec->owner_ref_checked = 1;
2829 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2830 u64 parent, u64 root, int found_ref)
2832 struct extent_record *rec;
2833 struct tree_backref *back;
2834 struct cache_extent *cache;
2836 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2838 add_extent_rec(extent_cache, NULL, 0, bytenr,
2839 1, 0, 0, 0, 0, 1, 0, 0);
2840 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2845 rec = container_of(cache, struct extent_record, cache);
2846 if (rec->start != bytenr) {
2850 back = find_tree_backref(rec, parent, root);
2852 back = alloc_tree_backref(rec, parent, root);
2855 if (back->node.found_ref) {
2856 fprintf(stderr, "Extent back ref already exists "
2857 "for %llu parent %llu root %llu \n",
2858 (unsigned long long)bytenr,
2859 (unsigned long long)parent,
2860 (unsigned long long)root);
2862 back->node.found_ref = 1;
2864 if (back->node.found_extent_tree) {
2865 fprintf(stderr, "Extent back ref already exists "
2866 "for %llu parent %llu root %llu \n",
2867 (unsigned long long)bytenr,
2868 (unsigned long long)parent,
2869 (unsigned long long)root);
2871 back->node.found_extent_tree = 1;
2873 maybe_free_extent_rec(extent_cache, rec);
2877 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2878 u64 parent, u64 root, u64 owner, u64 offset,
2879 u32 num_refs, int found_ref, u64 max_size)
2881 struct extent_record *rec;
2882 struct data_backref *back;
2883 struct cache_extent *cache;
2885 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2887 add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
2889 cache = lookup_cache_extent(extent_cache, bytenr, 1);
2894 rec = container_of(cache, struct extent_record, cache);
2895 if (rec->max_size < max_size)
2896 rec->max_size = max_size;
2899 * If found_ref is set then max_size is the real size and must match the
2900 * existing refs. So if we have already found a ref then we need to
2901 * make sure that this ref matches the existing one, otherwise we need
2902 * to add a new backref so we can notice that the backrefs don't match
2903 * and we need to figure out who is telling the truth. This is to
2904 * account for that awful fsync bug I introduced where we'd end up with
2905 * a btrfs_file_extent_item that would have its length include multiple
2906 * prealloc extents or point inside of a prealloc extent.
2908 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2911 back = alloc_data_backref(rec, parent, root, owner, offset,
2915 BUG_ON(num_refs != 1);
2916 if (back->node.found_ref)
2917 BUG_ON(back->bytes != max_size);
2918 back->node.found_ref = 1;
2919 back->found_ref += 1;
2920 back->bytes = max_size;
2921 back->disk_bytenr = bytenr;
2923 rec->content_checked = 1;
2924 rec->owner_ref_checked = 1;
2926 if (back->node.found_extent_tree) {
2927 fprintf(stderr, "Extent back ref already exists "
2928 "for %llu parent %llu root %llu "
2929 "owner %llu offset %llu num_refs %lu\n",
2930 (unsigned long long)bytenr,
2931 (unsigned long long)parent,
2932 (unsigned long long)root,
2933 (unsigned long long)owner,
2934 (unsigned long long)offset,
2935 (unsigned long)num_refs);
2937 back->num_refs = num_refs;
2938 back->node.found_extent_tree = 1;
2940 maybe_free_extent_rec(extent_cache, rec);
2944 static int add_pending(struct cache_tree *pending,
2945 struct cache_tree *seen, u64 bytenr, u32 size)
2948 ret = add_cache_extent(seen, bytenr, size);
2951 add_cache_extent(pending, bytenr, size);
2955 static int pick_next_pending(struct cache_tree *pending,
2956 struct cache_tree *reada,
2957 struct cache_tree *nodes,
2958 u64 last, struct block_info *bits, int bits_nr,
2961 unsigned long node_start = last;
2962 struct cache_extent *cache;
2965 cache = search_cache_extent(reada, 0);
2967 bits[0].start = cache->start;
2968 bits[0].size = cache->size;
2973 if (node_start > 32768)
2974 node_start -= 32768;
2976 cache = search_cache_extent(nodes, node_start);
2978 cache = search_cache_extent(nodes, 0);
2981 cache = search_cache_extent(pending, 0);
2986 bits[ret].start = cache->start;
2987 bits[ret].size = cache->size;
2988 cache = next_cache_extent(cache);
2990 } while (cache && ret < bits_nr);
2996 bits[ret].start = cache->start;
2997 bits[ret].size = cache->size;
2998 cache = next_cache_extent(cache);
3000 } while (cache && ret < bits_nr);
3002 if (bits_nr - ret > 8) {
3003 u64 lookup = bits[0].start + bits[0].size;
3004 struct cache_extent *next;
3005 next = search_cache_extent(pending, lookup);
3007 if (next->start - lookup > 32768)
3009 bits[ret].start = next->start;
3010 bits[ret].size = next->size;
3011 lookup = next->start + next->size;
3015 next = next_cache_extent(next);
3023 static void free_chunk_record(struct cache_extent *cache)
3025 struct chunk_record *rec;
3027 rec = container_of(cache, struct chunk_record, cache);
3031 void free_chunk_cache_tree(struct cache_tree *chunk_cache)
3033 cache_tree_free_extents(chunk_cache, free_chunk_record);
3036 static void free_device_record(struct rb_node *node)
3038 struct device_record *rec;
3040 rec = container_of(node, struct device_record, node);
3044 FREE_RB_BASED_TREE(device_cache, free_device_record);
3046 int insert_block_group_record(struct block_group_tree *tree,
3047 struct block_group_record *bg_rec)
3051 ret = insert_cache_extent(&tree->tree, &bg_rec->cache);
3055 list_add_tail(&bg_rec->list, &tree->block_groups);
3059 static void free_block_group_record(struct cache_extent *cache)
3061 struct block_group_record *rec;
3063 rec = container_of(cache, struct block_group_record, cache);
3067 void free_block_group_tree(struct block_group_tree *tree)
3069 cache_tree_free_extents(&tree->tree, free_block_group_record);
3072 int insert_device_extent_record(struct device_extent_tree *tree,
3073 struct device_extent_record *de_rec)
3078 * Device extent is a bit different from the other extents, because
3079 * the extents which belong to the different devices may have the
3080 * same start and size, so we need use the special extent cache
3081 * search/insert functions.
3083 ret = insert_cache_extent2(&tree->tree, &de_rec->cache);
3087 list_add_tail(&de_rec->chunk_list, &tree->no_chunk_orphans);
3088 list_add_tail(&de_rec->device_list, &tree->no_device_orphans);
3092 static void free_device_extent_record(struct cache_extent *cache)
3094 struct device_extent_record *rec;
3096 rec = container_of(cache, struct device_extent_record, cache);
3100 void free_device_extent_tree(struct device_extent_tree *tree)
3102 cache_tree_free_extents(&tree->tree, free_device_extent_record);
3105 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3106 static int process_extent_ref_v0(struct cache_tree *extent_cache,
3107 struct extent_buffer *leaf, int slot)
3109 struct btrfs_extent_ref_v0 *ref0;
3110 struct btrfs_key key;
3112 btrfs_item_key_to_cpu(leaf, &key, slot);
3113 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
3114 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
3115 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
3117 add_data_backref(extent_cache, key.objectid, key.offset, 0,
3118 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
3124 struct chunk_record *btrfs_new_chunk_record(struct extent_buffer *leaf,
3125 struct btrfs_key *key,
3128 struct btrfs_chunk *ptr;
3129 struct chunk_record *rec;
3132 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3133 num_stripes = btrfs_chunk_num_stripes(leaf, ptr);
3135 rec = malloc(btrfs_chunk_record_size(num_stripes));
3137 fprintf(stderr, "memory allocation failed\n");
3141 memset(rec, 0, btrfs_chunk_record_size(num_stripes));
3143 INIT_LIST_HEAD(&rec->list);
3144 INIT_LIST_HEAD(&rec->dextents);
3147 rec->cache.start = key->offset;
3148 rec->cache.size = btrfs_chunk_length(leaf, ptr);
3150 rec->generation = btrfs_header_generation(leaf);
3152 rec->objectid = key->objectid;
3153 rec->type = key->type;
3154 rec->offset = key->offset;
3156 rec->length = rec->cache.size;
3157 rec->owner = btrfs_chunk_owner(leaf, ptr);
3158 rec->stripe_len = btrfs_chunk_stripe_len(leaf, ptr);
3159 rec->type_flags = btrfs_chunk_type(leaf, ptr);
3160 rec->io_width = btrfs_chunk_io_width(leaf, ptr);
3161 rec->io_align = btrfs_chunk_io_align(leaf, ptr);
3162 rec->sector_size = btrfs_chunk_sector_size(leaf, ptr);
3163 rec->num_stripes = num_stripes;
3164 rec->sub_stripes = btrfs_chunk_sub_stripes(leaf, ptr);
3166 for (i = 0; i < rec->num_stripes; ++i) {
3167 rec->stripes[i].devid =
3168 btrfs_stripe_devid_nr(leaf, ptr, i);
3169 rec->stripes[i].offset =
3170 btrfs_stripe_offset_nr(leaf, ptr, i);
3171 read_extent_buffer(leaf, rec->stripes[i].dev_uuid,
3172 (unsigned long)btrfs_stripe_dev_uuid_nr(ptr, i),
3179 static int process_chunk_item(struct cache_tree *chunk_cache,
3180 struct btrfs_key *key, struct extent_buffer *eb,
3183 struct chunk_record *rec;
3186 rec = btrfs_new_chunk_record(eb, key, slot);
3187 ret = insert_cache_extent(chunk_cache, &rec->cache);
3189 fprintf(stderr, "Chunk[%llu, %llu] existed.\n",
3190 rec->offset, rec->length);
3197 static int process_device_item(struct rb_root *dev_cache,
3198 struct btrfs_key *key, struct extent_buffer *eb, int slot)
3200 struct btrfs_dev_item *ptr;
3201 struct device_record *rec;
3204 ptr = btrfs_item_ptr(eb,
3205 slot, struct btrfs_dev_item);
3207 rec = malloc(sizeof(*rec));
3209 fprintf(stderr, "memory allocation failed\n");
3213 rec->devid = key->offset;
3214 rec->generation = btrfs_header_generation(eb);
3216 rec->objectid = key->objectid;
3217 rec->type = key->type;
3218 rec->offset = key->offset;
3220 rec->devid = btrfs_device_id(eb, ptr);
3221 rec->total_byte = btrfs_device_total_bytes(eb, ptr);
3222 rec->byte_used = btrfs_device_bytes_used(eb, ptr);
3224 ret = rb_insert(dev_cache, &rec->node, device_record_compare);
3226 fprintf(stderr, "Device[%llu] existed.\n", rec->devid);
3233 struct block_group_record *
3234 btrfs_new_block_group_record(struct extent_buffer *leaf, struct btrfs_key *key,
3237 struct btrfs_block_group_item *ptr;
3238 struct block_group_record *rec;
3240 rec = malloc(sizeof(*rec));
3242 fprintf(stderr, "memory allocation failed\n");
3245 memset(rec, 0, sizeof(*rec));
3247 rec->cache.start = key->objectid;
3248 rec->cache.size = key->offset;
3250 rec->generation = btrfs_header_generation(leaf);
3252 rec->objectid = key->objectid;
3253 rec->type = key->type;
3254 rec->offset = key->offset;
3256 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_block_group_item);
3257 rec->flags = btrfs_disk_block_group_flags(leaf, ptr);
3259 INIT_LIST_HEAD(&rec->list);
3264 static int process_block_group_item(struct block_group_tree *block_group_cache,
3265 struct btrfs_key *key,
3266 struct extent_buffer *eb, int slot)
3268 struct block_group_record *rec;
3271 rec = btrfs_new_block_group_record(eb, key, slot);
3272 ret = insert_block_group_record(block_group_cache, rec);
3274 fprintf(stderr, "Block Group[%llu, %llu] existed.\n",
3275 rec->objectid, rec->offset);
3282 struct device_extent_record *
3283 btrfs_new_device_extent_record(struct extent_buffer *leaf,
3284 struct btrfs_key *key, int slot)
3286 struct device_extent_record *rec;
3287 struct btrfs_dev_extent *ptr;
3289 rec = malloc(sizeof(*rec));
3291 fprintf(stderr, "memory allocation failed\n");
3294 memset(rec, 0, sizeof(*rec));
3296 rec->cache.objectid = key->objectid;
3297 rec->cache.start = key->offset;
3299 rec->generation = btrfs_header_generation(leaf);
3301 rec->objectid = key->objectid;
3302 rec->type = key->type;
3303 rec->offset = key->offset;
3305 ptr = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
3306 rec->chunk_objecteid =
3307 btrfs_dev_extent_chunk_objectid(leaf, ptr);
3309 btrfs_dev_extent_chunk_offset(leaf, ptr);
3310 rec->length = btrfs_dev_extent_length(leaf, ptr);
3311 rec->cache.size = rec->length;
3313 INIT_LIST_HEAD(&rec->chunk_list);
3314 INIT_LIST_HEAD(&rec->device_list);
3320 process_device_extent_item(struct device_extent_tree *dev_extent_cache,
3321 struct btrfs_key *key, struct extent_buffer *eb,
3324 struct device_extent_record *rec;
3327 rec = btrfs_new_device_extent_record(eb, key, slot);
3328 ret = insert_device_extent_record(dev_extent_cache, rec);
3331 "Device extent[%llu, %llu, %llu] existed.\n",
3332 rec->objectid, rec->offset, rec->length);
3339 static int process_extent_item(struct btrfs_root *root,
3340 struct cache_tree *extent_cache,
3341 struct extent_buffer *eb, int slot)
3343 struct btrfs_extent_item *ei;
3344 struct btrfs_extent_inline_ref *iref;
3345 struct btrfs_extent_data_ref *dref;
3346 struct btrfs_shared_data_ref *sref;
3347 struct btrfs_key key;
3351 u32 item_size = btrfs_item_size_nr(eb, slot);
3357 btrfs_item_key_to_cpu(eb, &key, slot);
3359 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3361 num_bytes = root->leafsize;
3363 num_bytes = key.offset;
3366 if (item_size < sizeof(*ei)) {
3367 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3368 struct btrfs_extent_item_v0 *ei0;
3369 BUG_ON(item_size != sizeof(*ei0));
3370 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
3371 refs = btrfs_extent_refs_v0(eb, ei0);
3375 return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3376 num_bytes, refs, 0, 0, 0, metadata, 1,
3380 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3381 refs = btrfs_extent_refs(eb, ei);
3383 add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3384 refs, 0, 0, 0, metadata, 1, num_bytes);
3386 ptr = (unsigned long)(ei + 1);
3387 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
3388 key.type == BTRFS_EXTENT_ITEM_KEY)
3389 ptr += sizeof(struct btrfs_tree_block_info);
3391 end = (unsigned long)ei + item_size;
3393 iref = (struct btrfs_extent_inline_ref *)ptr;
3394 type = btrfs_extent_inline_ref_type(eb, iref);
3395 offset = btrfs_extent_inline_ref_offset(eb, iref);
3397 case BTRFS_TREE_BLOCK_REF_KEY:
3398 add_tree_backref(extent_cache, key.objectid,
3401 case BTRFS_SHARED_BLOCK_REF_KEY:
3402 add_tree_backref(extent_cache, key.objectid,
3405 case BTRFS_EXTENT_DATA_REF_KEY:
3406 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
3407 add_data_backref(extent_cache, key.objectid, 0,
3408 btrfs_extent_data_ref_root(eb, dref),
3409 btrfs_extent_data_ref_objectid(eb,
3411 btrfs_extent_data_ref_offset(eb, dref),
3412 btrfs_extent_data_ref_count(eb, dref),
3415 case BTRFS_SHARED_DATA_REF_KEY:
3416 sref = (struct btrfs_shared_data_ref *)(iref + 1);
3417 add_data_backref(extent_cache, key.objectid, offset,
3419 btrfs_shared_data_ref_count(eb, sref),
3423 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
3424 key.objectid, key.type, num_bytes);
3427 ptr += btrfs_extent_inline_ref_size(type);
3434 static int check_cache_range(struct btrfs_root *root,
3435 struct btrfs_block_group_cache *cache,
3436 u64 offset, u64 bytes)
3438 struct btrfs_free_space *entry;
3444 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3445 bytenr = btrfs_sb_offset(i);
3446 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
3447 cache->key.objectid, bytenr, 0,
3448 &logical, &nr, &stripe_len);
3453 if (logical[nr] + stripe_len <= offset)
3455 if (offset + bytes <= logical[nr])
3457 if (logical[nr] == offset) {
3458 if (stripe_len >= bytes) {
3462 bytes -= stripe_len;
3463 offset += stripe_len;
3464 } else if (logical[nr] < offset) {
3465 if (logical[nr] + stripe_len >=
3470 bytes = (offset + bytes) -
3471 (logical[nr] + stripe_len);
3472 offset = logical[nr] + stripe_len;
3475 * Could be tricky, the super may land in the
3476 * middle of the area we're checking. First
3477 * check the easiest case, it's at the end.
3479 if (logical[nr] + stripe_len >=
3481 bytes = logical[nr] - offset;
3485 /* Check the left side */
3486 ret = check_cache_range(root, cache,
3488 logical[nr] - offset);
3494 /* Now we continue with the right side */
3495 bytes = (offset + bytes) -
3496 (logical[nr] + stripe_len);
3497 offset = logical[nr] + stripe_len;
3504 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
3506 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
3507 offset, offset+bytes);
3511 if (entry->offset != offset) {
3512 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
3517 if (entry->bytes != bytes) {
3518 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
3519 bytes, entry->bytes, offset);
3523 unlink_free_space(cache->free_space_ctl, entry);
3528 static int verify_space_cache(struct btrfs_root *root,
3529 struct btrfs_block_group_cache *cache)
3531 struct btrfs_path *path;
3532 struct extent_buffer *leaf;
3533 struct btrfs_key key;
3537 path = btrfs_alloc_path();
3541 root = root->fs_info->extent_root;
3543 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
3545 key.objectid = last;
3547 key.type = BTRFS_EXTENT_ITEM_KEY;
3549 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3554 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3555 ret = btrfs_next_leaf(root, path);
3563 leaf = path->nodes[0];
3564 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3565 if (key.objectid >= cache->key.offset + cache->key.objectid)
3567 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
3568 key.type != BTRFS_METADATA_ITEM_KEY) {
3573 if (last == key.objectid) {
3574 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3575 last = key.objectid + key.offset;
3577 last = key.objectid + root->leafsize;
3582 ret = check_cache_range(root, cache, last,
3583 key.objectid - last);
3586 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3587 last = key.objectid + key.offset;
3589 last = key.objectid + root->leafsize;
3593 if (last < cache->key.objectid + cache->key.offset)
3594 ret = check_cache_range(root, cache, last,
3595 cache->key.objectid +
3596 cache->key.offset - last);
3599 btrfs_free_path(path);
3602 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
3603 fprintf(stderr, "There are still entries left in the space "
3611 static int check_space_cache(struct btrfs_root *root)
3613 struct btrfs_block_group_cache *cache;
3614 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
3618 if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
3619 btrfs_super_generation(root->fs_info->super_copy) !=
3620 btrfs_super_cache_generation(root->fs_info->super_copy)) {
3621 printf("cache and super generation don't match, space cache "
3622 "will be invalidated\n");
3627 cache = btrfs_lookup_first_block_group(root->fs_info, start);
3631 start = cache->key.objectid + cache->key.offset;
3632 if (!cache->free_space_ctl) {
3633 if (btrfs_init_free_space_ctl(cache,
3634 root->sectorsize)) {
3639 btrfs_remove_free_space_cache(cache);
3642 ret = load_free_space_cache(root->fs_info, cache);
3646 ret = verify_space_cache(root, cache);
3648 fprintf(stderr, "cache appears valid but isnt %Lu\n",
3649 cache->key.objectid);
3654 return error ? -EINVAL : 0;
3657 static int read_extent_data(struct btrfs_root *root, char *data,
3658 u64 logical, u64 *len, int mirror)
3661 struct btrfs_multi_bio *multi = NULL;
3662 struct btrfs_fs_info *info = root->fs_info;
3663 struct btrfs_device *device;
3667 ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
3668 &multi, mirror, NULL);
3670 fprintf(stderr, "Couldn't map the block %llu\n",
3674 device = multi->stripes[0].dev;
3676 if (device->fd == 0)
3681 ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
3691 static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
3692 u64 num_bytes, unsigned long leaf_offset,
3693 struct extent_buffer *eb) {
3696 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3698 unsigned long csum_offset;
3702 u64 data_checked = 0;
3708 if (num_bytes % root->sectorsize)
3711 data = malloc(num_bytes);
3715 while (offset < num_bytes) {
3718 read_len = num_bytes - offset;
3719 /* read as much space once a time */
3720 ret = read_extent_data(root, data + offset,
3721 bytenr + offset, &read_len, mirror);
3725 /* verify every 4k data's checksum */
3726 while (data_checked < read_len) {
3728 tmp = offset + data_checked;
3730 csum = btrfs_csum_data(NULL, (char *)data + tmp,
3731 csum, root->sectorsize);
3732 btrfs_csum_final(csum, (char *)&csum);
3734 csum_offset = leaf_offset +
3735 tmp / root->sectorsize * csum_size;
3736 read_extent_buffer(eb, (char *)&csum_expected,
3737 csum_offset, csum_size);
3738 /* try another mirror */
3739 if (csum != csum_expected) {
3740 fprintf(stderr, "mirror %d bytenr %llu csum %u expected csum %u\n",
3741 mirror, bytenr + tmp,
3742 csum, csum_expected);
3743 num_copies = btrfs_num_copies(
3744 &root->fs_info->mapping_tree,
3746 if (mirror < num_copies - 1) {
3751 data_checked += root->sectorsize;
3760 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
3763 struct btrfs_path *path;
3764 struct extent_buffer *leaf;
3765 struct btrfs_key key;
3768 path = btrfs_alloc_path();
3770 fprintf(stderr, "Error allocing path\n");
3774 key.objectid = bytenr;
3775 key.type = BTRFS_EXTENT_ITEM_KEY;
3780 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
3783 fprintf(stderr, "Error looking up extent record %d\n", ret);
3784 btrfs_free_path(path);
3790 btrfs_prev_leaf(root, path);
3793 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3796 * Block group items come before extent items if they have the same
3797 * bytenr, so walk back one more just in case. Dear future traveler,
3798 * first congrats on mastering time travel. Now if it's not too much
3799 * trouble could you go back to 2006 and tell Chris to make the
3800 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
3802 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3806 btrfs_prev_leaf(root, path);
3810 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3811 ret = btrfs_next_leaf(root, path);
3813 fprintf(stderr, "Error going to next leaf "
3815 btrfs_free_path(path);
3821 leaf = path->nodes[0];
3822 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3823 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3827 if (key.objectid + key.offset < bytenr) {
3831 if (key.objectid > bytenr + num_bytes)
3834 if (key.objectid == bytenr) {
3835 if (key.offset >= num_bytes) {
3839 num_bytes -= key.offset;
3840 bytenr += key.offset;
3841 } else if (key.objectid < bytenr) {
3842 if (key.objectid + key.offset >= bytenr + num_bytes) {
3846 num_bytes = (bytenr + num_bytes) -
3847 (key.objectid + key.offset);
3848 bytenr = key.objectid + key.offset;
3850 if (key.objectid + key.offset < bytenr + num_bytes) {
3851 u64 new_start = key.objectid + key.offset;
3852 u64 new_bytes = bytenr + num_bytes - new_start;
3855 * Weird case, the extent is in the middle of
3856 * our range, we'll have to search one side
3857 * and then the other. Not sure if this happens
3858 * in real life, but no harm in coding it up
3859 * anyway just in case.
3861 btrfs_release_path(path);
3862 ret = check_extent_exists(root, new_start,
3865 fprintf(stderr, "Right section didn't "
3869 num_bytes = key.objectid - bytenr;
3872 num_bytes = key.objectid - bytenr;
3879 fprintf(stderr, "There are no extents for csum range "
3880 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3884 btrfs_free_path(path);
3888 static int check_csums(struct btrfs_root *root)
3890 struct btrfs_path *path;
3891 struct extent_buffer *leaf;
3892 struct btrfs_key key;
3893 u64 offset = 0, num_bytes = 0;
3894 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3898 unsigned long leaf_offset;
3900 root = root->fs_info->csum_root;
3901 if (!extent_buffer_uptodate(root->node)) {
3902 fprintf(stderr, "No valid csum tree found\n");
3906 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3907 key.type = BTRFS_EXTENT_CSUM_KEY;
3910 path = btrfs_alloc_path();
3914 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3916 fprintf(stderr, "Error searching csum tree %d\n", ret);
3917 btrfs_free_path(path);
3921 if (ret > 0 && path->slots[0])
3926 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3927 ret = btrfs_next_leaf(root, path);
3929 fprintf(stderr, "Error going to next leaf "
3936 leaf = path->nodes[0];
3938 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3939 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3944 data_len = (btrfs_item_size_nr(leaf, path->slots[0]) /
3945 csum_size) * root->sectorsize;
3946 if (!check_data_csum)
3947 goto skip_csum_check;
3948 leaf_offset = btrfs_item_ptr_offset(leaf, path->slots[0]);
3949 ret = check_extent_csums(root, key.offset, data_len,
3955 offset = key.offset;
3956 } else if (key.offset != offset + num_bytes) {
3957 ret = check_extent_exists(root, offset, num_bytes);
3959 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3960 "there is no extent record\n",
3961 offset, offset+num_bytes);
3964 offset = key.offset;
3967 num_bytes += data_len;
3971 btrfs_free_path(path);
3975 static int is_dropped_key(struct btrfs_key *key,
3976 struct btrfs_key *drop_key) {
3977 if (key->objectid < drop_key->objectid)
3979 else if (key->objectid == drop_key->objectid) {
3980 if (key->type < drop_key->type)
3982 else if (key->type == drop_key->type) {
3983 if (key->offset < drop_key->offset)
3990 static int run_next_block(struct btrfs_trans_handle *trans,
3991 struct btrfs_root *root,
3992 struct block_info *bits,
3995 struct cache_tree *pending,
3996 struct cache_tree *seen,
3997 struct cache_tree *reada,
3998 struct cache_tree *nodes,
3999 struct cache_tree *extent_cache,
4000 struct cache_tree *chunk_cache,
4001 struct rb_root *dev_cache,
4002 struct block_group_tree *block_group_cache,
4003 struct device_extent_tree *dev_extent_cache,
4004 struct btrfs_root_item *ri)
4006 struct extent_buffer *buf;
4017 struct btrfs_key key;
4018 struct cache_extent *cache;
4021 nritems = pick_next_pending(pending, reada, nodes, *last, bits,
4022 bits_nr, &reada_bits);
4027 for(i = 0; i < nritems; i++) {
4028 ret = add_cache_extent(reada, bits[i].start,
4033 /* fixme, get the parent transid */
4034 readahead_tree_block(root, bits[i].start,
4038 *last = bits[0].start;
4039 bytenr = bits[0].start;
4040 size = bits[0].size;
4042 cache = lookup_cache_extent(pending, bytenr, size);
4044 remove_cache_extent(pending, cache);
4047 cache = lookup_cache_extent(reada, bytenr, size);
4049 remove_cache_extent(reada, cache);
4052 cache = lookup_cache_extent(nodes, bytenr, size);
4054 remove_cache_extent(nodes, cache);
4057 cache = lookup_cache_extent(extent_cache, bytenr, size);
4059 struct extent_record *rec;
4061 rec = container_of(cache, struct extent_record, cache);
4062 gen = rec->parent_generation;
4065 /* fixme, get the real parent transid */
4066 buf = read_tree_block(root, bytenr, size, gen);
4067 if (!extent_buffer_uptodate(buf)) {
4068 record_bad_block_io(root->fs_info,
4069 extent_cache, bytenr, size);
4073 nritems = btrfs_header_nritems(buf);
4076 * FIXME, this only works only if we don't have any full
4079 if (!init_extent_tree) {
4080 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
4081 btrfs_header_level(buf), 1, NULL,
4089 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
4094 owner = btrfs_header_owner(buf);
4097 ret = check_block(trans, root, extent_cache, buf, flags);
4101 if (btrfs_is_leaf(buf)) {
4102 btree_space_waste += btrfs_leaf_free_space(root, buf);
4103 for (i = 0; i < nritems; i++) {
4104 struct btrfs_file_extent_item *fi;
4105 btrfs_item_key_to_cpu(buf, &key, i);
4106 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
4107 process_extent_item(root, extent_cache, buf,
4111 if (key.type == BTRFS_METADATA_ITEM_KEY) {
4112 process_extent_item(root, extent_cache, buf,
4116 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
4118 btrfs_item_size_nr(buf, i);
4121 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
4122 process_chunk_item(chunk_cache, &key, buf, i);
4125 if (key.type == BTRFS_DEV_ITEM_KEY) {
4126 process_device_item(dev_cache, &key, buf, i);
4129 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
4130 process_block_group_item(block_group_cache,
4134 if (key.type == BTRFS_DEV_EXTENT_KEY) {
4135 process_device_extent_item(dev_extent_cache,
4140 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
4141 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4142 process_extent_ref_v0(extent_cache, buf, i);
4149 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
4150 add_tree_backref(extent_cache, key.objectid, 0,
4154 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
4155 add_tree_backref(extent_cache, key.objectid,
4159 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
4160 struct btrfs_extent_data_ref *ref;
4161 ref = btrfs_item_ptr(buf, i,
4162 struct btrfs_extent_data_ref);
4163 add_data_backref(extent_cache,
4165 btrfs_extent_data_ref_root(buf, ref),
4166 btrfs_extent_data_ref_objectid(buf,
4168 btrfs_extent_data_ref_offset(buf, ref),
4169 btrfs_extent_data_ref_count(buf, ref),
4170 0, root->sectorsize);
4173 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
4174 struct btrfs_shared_data_ref *ref;
4175 ref = btrfs_item_ptr(buf, i,
4176 struct btrfs_shared_data_ref);
4177 add_data_backref(extent_cache,
4178 key.objectid, key.offset, 0, 0, 0,
4179 btrfs_shared_data_ref_count(buf, ref),
4180 0, root->sectorsize);
4183 if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
4184 struct bad_item *bad;
4186 if (key.objectid == BTRFS_ORPHAN_OBJECTID)
4190 bad = malloc(sizeof(struct bad_item));
4193 INIT_LIST_HEAD(&bad->list);
4194 memcpy(&bad->key, &key,
4195 sizeof(struct btrfs_key));
4196 bad->root_id = owner;
4197 list_add_tail(&bad->list, &delete_items);
4200 if (key.type != BTRFS_EXTENT_DATA_KEY)
4202 fi = btrfs_item_ptr(buf, i,
4203 struct btrfs_file_extent_item);
4204 if (btrfs_file_extent_type(buf, fi) ==
4205 BTRFS_FILE_EXTENT_INLINE)
4207 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
4210 data_bytes_allocated +=
4211 btrfs_file_extent_disk_num_bytes(buf, fi);
4212 if (data_bytes_allocated < root->sectorsize) {
4215 data_bytes_referenced +=
4216 btrfs_file_extent_num_bytes(buf, fi);
4217 add_data_backref(extent_cache,
4218 btrfs_file_extent_disk_bytenr(buf, fi),
4219 parent, owner, key.objectid, key.offset -
4220 btrfs_file_extent_offset(buf, fi), 1, 1,
4221 btrfs_file_extent_disk_num_bytes(buf, fi));
4225 struct btrfs_key first_key;
4227 first_key.objectid = 0;
4230 btrfs_item_key_to_cpu(buf, &first_key, 0);
4231 level = btrfs_header_level(buf);
4232 for (i = 0; i < nritems; i++) {
4233 ptr = btrfs_node_blockptr(buf, i);
4234 size = btrfs_level_size(root, level - 1);
4235 btrfs_node_key_to_cpu(buf, &key, i);
4237 struct btrfs_key drop_key;
4238 btrfs_disk_key_to_cpu(&drop_key,
4239 &ri->drop_progress);
4240 if ((level == ri->drop_level)
4241 && is_dropped_key(&key, &drop_key)) {
4245 ret = add_extent_rec(extent_cache, &key,
4246 btrfs_node_ptr_generation(buf, i),
4247 ptr, size, 0, 0, 1, 0, 1, 0,
4251 add_tree_backref(extent_cache, ptr, parent, owner, 1);
4254 add_pending(nodes, seen, ptr, size);
4256 add_pending(pending, seen, ptr, size);
4259 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
4260 nritems) * sizeof(struct btrfs_key_ptr);
4262 total_btree_bytes += buf->len;
4263 if (fs_root_objectid(btrfs_header_owner(buf)))
4264 total_fs_tree_bytes += buf->len;
4265 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
4266 total_extent_tree_bytes += buf->len;
4267 if (!found_old_backref &&
4268 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
4269 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
4270 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
4271 found_old_backref = 1;
4273 free_extent_buffer(buf);
4277 static int add_root_to_pending(struct extent_buffer *buf,
4278 struct cache_tree *extent_cache,
4279 struct cache_tree *pending,
4280 struct cache_tree *seen,
4281 struct cache_tree *nodes,
4282 struct btrfs_key *root_key)
4284 if (btrfs_header_level(buf) > 0)
4285 add_pending(nodes, seen, buf->start, buf->len);
4287 add_pending(pending, seen, buf->start, buf->len);
4288 add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
4289 0, 1, 1, 0, 1, 0, buf->len);
4291 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4292 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
4293 add_tree_backref(extent_cache, buf->start, buf->start,
4296 add_tree_backref(extent_cache, buf->start, 0,
4297 root_key->objectid, 1);
4301 /* as we fix the tree, we might be deleting blocks that
4302 * we're tracking for repair. This hook makes sure we
4303 * remove any backrefs for blocks as we are fixing them.
4305 static int free_extent_hook(struct btrfs_trans_handle *trans,
4306 struct btrfs_root *root,
4307 u64 bytenr, u64 num_bytes, u64 parent,
4308 u64 root_objectid, u64 owner, u64 offset,
4311 struct extent_record *rec;
4312 struct cache_extent *cache;
4314 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
4316 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
4317 cache = lookup_cache_extent(extent_cache, bytenr, num_bytes);
4321 rec = container_of(cache, struct extent_record, cache);
4323 struct data_backref *back;
4324 back = find_data_backref(rec, parent, root_objectid, owner,
4325 offset, 1, bytenr, num_bytes);
4328 if (back->node.found_ref) {
4329 back->found_ref -= refs_to_drop;
4331 rec->refs -= refs_to_drop;
4333 if (back->node.found_extent_tree) {
4334 back->num_refs -= refs_to_drop;
4335 if (rec->extent_item_refs)
4336 rec->extent_item_refs -= refs_to_drop;
4338 if (back->found_ref == 0)
4339 back->node.found_ref = 0;
4340 if (back->num_refs == 0)
4341 back->node.found_extent_tree = 0;
4343 if (!back->node.found_extent_tree && back->node.found_ref) {
4344 list_del(&back->node.list);
4348 struct tree_backref *back;
4349 back = find_tree_backref(rec, parent, root_objectid);
4352 if (back->node.found_ref) {
4355 back->node.found_ref = 0;
4357 if (back->node.found_extent_tree) {
4358 if (rec->extent_item_refs)
4359 rec->extent_item_refs--;
4360 back->node.found_extent_tree = 0;
4362 if (!back->node.found_extent_tree && back->node.found_ref) {
4363 list_del(&back->node.list);
4367 maybe_free_extent_rec(extent_cache, rec);
4372 static int delete_extent_records(struct btrfs_trans_handle *trans,
4373 struct btrfs_root *root,
4374 struct btrfs_path *path,
4375 u64 bytenr, u64 new_len)
4377 struct btrfs_key key;
4378 struct btrfs_key found_key;
4379 struct extent_buffer *leaf;
4384 key.objectid = bytenr;
4386 key.offset = (u64)-1;
4389 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
4396 if (path->slots[0] == 0)
4402 leaf = path->nodes[0];
4403 slot = path->slots[0];
4405 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4406 if (found_key.objectid != bytenr)
4409 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
4410 found_key.type != BTRFS_METADATA_ITEM_KEY &&
4411 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
4412 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
4413 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
4414 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
4415 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
4416 btrfs_release_path(path);
4417 if (found_key.type == 0) {
4418 if (found_key.offset == 0)
4420 key.offset = found_key.offset - 1;
4421 key.type = found_key.type;
4423 key.type = found_key.type - 1;
4424 key.offset = (u64)-1;
4428 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
4429 found_key.objectid, found_key.type, found_key.offset);
4431 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
4434 btrfs_release_path(path);
4436 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4437 found_key.type == BTRFS_METADATA_ITEM_KEY) {
4438 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
4439 found_key.offset : root->leafsize;
4441 ret = btrfs_update_block_group(trans, root, bytenr,
4448 btrfs_release_path(path);
4453 * for a single backref, this will allocate a new extent
4454 * and add the backref to it.
4456 static int record_extent(struct btrfs_trans_handle *trans,
4457 struct btrfs_fs_info *info,
4458 struct btrfs_path *path,
4459 struct extent_record *rec,
4460 struct extent_backref *back,
4461 int allocated, u64 flags)
4464 struct btrfs_root *extent_root = info->extent_root;
4465 struct extent_buffer *leaf;
4466 struct btrfs_key ins_key;
4467 struct btrfs_extent_item *ei;
4468 struct tree_backref *tback;
4469 struct data_backref *dback;
4470 struct btrfs_tree_block_info *bi;
4473 rec->max_size = max_t(u64, rec->max_size,
4474 info->extent_root->leafsize);
4477 u32 item_size = sizeof(*ei);
4480 item_size += sizeof(*bi);
4482 ins_key.objectid = rec->start;
4483 ins_key.offset = rec->max_size;
4484 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
4486 ret = btrfs_insert_empty_item(trans, extent_root, path,
4487 &ins_key, item_size);
4491 leaf = path->nodes[0];
4492 ei = btrfs_item_ptr(leaf, path->slots[0],
4493 struct btrfs_extent_item);
4495 btrfs_set_extent_refs(leaf, ei, 0);
4496 btrfs_set_extent_generation(leaf, ei, rec->generation);
4498 if (back->is_data) {
4499 btrfs_set_extent_flags(leaf, ei,
4500 BTRFS_EXTENT_FLAG_DATA);
4502 struct btrfs_disk_key copy_key;;
4504 tback = (struct tree_backref *)back;
4505 bi = (struct btrfs_tree_block_info *)(ei + 1);
4506 memset_extent_buffer(leaf, 0, (unsigned long)bi,
4509 btrfs_set_disk_key_objectid(©_key,
4510 rec->info_objectid);
4511 btrfs_set_disk_key_type(©_key, 0);
4512 btrfs_set_disk_key_offset(©_key, 0);
4514 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
4515 btrfs_set_tree_block_key(leaf, bi, ©_key);
4517 btrfs_set_extent_flags(leaf, ei,
4518 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
4521 btrfs_mark_buffer_dirty(leaf);
4522 ret = btrfs_update_block_group(trans, extent_root, rec->start,
4523 rec->max_size, 1, 0);
4526 btrfs_release_path(path);
4529 if (back->is_data) {
4533 dback = (struct data_backref *)back;
4534 if (back->full_backref)
4535 parent = dback->parent;
4539 for (i = 0; i < dback->found_ref; i++) {
4540 /* if parent != 0, we're doing a full backref
4541 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
4542 * just makes the backref allocator create a data
4545 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4546 rec->start, rec->max_size,
4550 BTRFS_FIRST_FREE_OBJECTID :
4556 fprintf(stderr, "adding new data backref"
4557 " on %llu %s %llu owner %llu"
4558 " offset %llu found %d\n",
4559 (unsigned long long)rec->start,
4560 back->full_backref ?
4562 back->full_backref ?
4563 (unsigned long long)parent :
4564 (unsigned long long)dback->root,
4565 (unsigned long long)dback->owner,
4566 (unsigned long long)dback->offset,
4571 tback = (struct tree_backref *)back;
4572 if (back->full_backref)
4573 parent = tback->parent;
4577 ret = btrfs_inc_extent_ref(trans, info->extent_root,
4578 rec->start, rec->max_size,
4579 parent, tback->root, 0, 0);
4580 fprintf(stderr, "adding new tree backref on "
4581 "start %llu len %llu parent %llu root %llu\n",
4582 rec->start, rec->max_size, tback->parent, tback->root);
4587 btrfs_release_path(path);
4591 struct extent_entry {
4596 struct list_head list;
4599 static struct extent_entry *find_entry(struct list_head *entries,
4600 u64 bytenr, u64 bytes)
4602 struct extent_entry *entry = NULL;
4604 list_for_each_entry(entry, entries, list) {
4605 if (entry->bytenr == bytenr && entry->bytes == bytes)
4612 static struct extent_entry *find_most_right_entry(struct list_head *entries)
4614 struct extent_entry *entry, *best = NULL, *prev = NULL;
4616 list_for_each_entry(entry, entries, list) {
4623 * If there are as many broken entries as entries then we know
4624 * not to trust this particular entry.
4626 if (entry->broken == entry->count)
4630 * If our current entry == best then we can't be sure our best
4631 * is really the best, so we need to keep searching.
4633 if (best && best->count == entry->count) {
4639 /* Prev == entry, not good enough, have to keep searching */
4640 if (!prev->broken && prev->count == entry->count)
4644 best = (prev->count > entry->count) ? prev : entry;
4645 else if (best->count < entry->count)
4653 static int repair_ref(struct btrfs_trans_handle *trans,
4654 struct btrfs_fs_info *info, struct btrfs_path *path,
4655 struct data_backref *dback, struct extent_entry *entry)
4657 struct btrfs_root *root;
4658 struct btrfs_file_extent_item *fi;
4659 struct extent_buffer *leaf;
4660 struct btrfs_key key;
4664 key.objectid = dback->root;
4665 key.type = BTRFS_ROOT_ITEM_KEY;
4666 key.offset = (u64)-1;
4667 root = btrfs_read_fs_root(info, &key);
4669 fprintf(stderr, "Couldn't find root for our ref\n");
4674 * The backref points to the original offset of the extent if it was
4675 * split, so we need to search down to the offset we have and then walk
4676 * forward until we find the backref we're looking for.
4678 key.objectid = dback->owner;
4679 key.type = BTRFS_EXTENT_DATA_KEY;
4680 key.offset = dback->offset;
4681 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4683 fprintf(stderr, "Error looking up ref %d\n", ret);
4688 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4689 ret = btrfs_next_leaf(root, path);
4691 fprintf(stderr, "Couldn't find our ref, next\n");
4695 leaf = path->nodes[0];
4696 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4697 if (key.objectid != dback->owner ||
4698 key.type != BTRFS_EXTENT_DATA_KEY) {
4699 fprintf(stderr, "Couldn't find our ref, search\n");
4702 fi = btrfs_item_ptr(leaf, path->slots[0],
4703 struct btrfs_file_extent_item);
4704 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4705 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4707 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
4712 btrfs_release_path(path);
4715 * Have to make sure that this root gets updated when we commit the
4718 record_root_in_trans(trans, root);
4721 * Ok we have the key of the file extent we want to fix, now we can cow
4722 * down to the thing and fix it.
4724 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4726 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
4727 key.objectid, key.type, key.offset, ret);
4731 fprintf(stderr, "Well that's odd, we just found this key "
4732 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
4736 leaf = path->nodes[0];
4737 fi = btrfs_item_ptr(leaf, path->slots[0],
4738 struct btrfs_file_extent_item);
4740 if (btrfs_file_extent_compression(leaf, fi) &&
4741 dback->disk_bytenr != entry->bytenr) {
4742 fprintf(stderr, "Ref doesn't match the record start and is "
4743 "compressed, please take a btrfs-image of this file "
4744 "system and send it to a btrfs developer so they can "
4745 "complete this functionality for bytenr %Lu\n",
4746 dback->disk_bytenr);
4750 if (dback->node.broken && dback->disk_bytenr != entry->bytenr) {
4751 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4752 } else if (dback->disk_bytenr > entry->bytenr) {
4753 u64 off_diff, offset;
4755 off_diff = dback->disk_bytenr - entry->bytenr;
4756 offset = btrfs_file_extent_offset(leaf, fi);
4757 if (dback->disk_bytenr + offset +
4758 btrfs_file_extent_num_bytes(leaf, fi) >
4759 entry->bytenr + entry->bytes) {
4760 fprintf(stderr, "Ref is past the entry end, please "
4761 "take a btrfs-image of this file system and "
4762 "send it to a btrfs developer, ref %Lu\n",
4763 dback->disk_bytenr);
4767 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4768 btrfs_set_file_extent_offset(leaf, fi, offset);
4769 } else if (dback->disk_bytenr < entry->bytenr) {
4772 offset = btrfs_file_extent_offset(leaf, fi);
4773 if (dback->disk_bytenr + offset < entry->bytenr) {
4774 fprintf(stderr, "Ref is before the entry start, please"
4775 " take a btrfs-image of this file system and "
4776 "send it to a btrfs developer, ref %Lu\n",
4777 dback->disk_bytenr);
4781 offset += dback->disk_bytenr;
4782 offset -= entry->bytenr;
4783 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
4784 btrfs_set_file_extent_offset(leaf, fi, offset);
4787 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
4790 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
4791 * only do this if we aren't using compression, otherwise it's a
4794 if (!btrfs_file_extent_compression(leaf, fi))
4795 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
4797 printf("ram bytes may be wrong?\n");
4798 btrfs_mark_buffer_dirty(leaf);
4799 btrfs_release_path(path);
4803 static int verify_backrefs(struct btrfs_trans_handle *trans,
4804 struct btrfs_fs_info *info, struct btrfs_path *path,
4805 struct extent_record *rec)
4807 struct extent_backref *back;
4808 struct data_backref *dback;
4809 struct extent_entry *entry, *best = NULL;
4812 int broken_entries = 0;
4817 * Metadata is easy and the backrefs should always agree on bytenr and
4818 * size, if not we've got bigger issues.
4823 list_for_each_entry(back, &rec->backrefs, list) {
4824 dback = (struct data_backref *)back;
4826 * We only pay attention to backrefs that we found a real
4829 if (dback->found_ref == 0)
4831 if (back->full_backref)
4835 * For now we only catch when the bytes don't match, not the
4836 * bytenr. We can easily do this at the same time, but I want
4837 * to have a fs image to test on before we just add repair
4838 * functionality willy-nilly so we know we won't screw up the
4842 entry = find_entry(&entries, dback->disk_bytenr,
4845 entry = malloc(sizeof(struct extent_entry));
4850 memset(entry, 0, sizeof(*entry));
4851 entry->bytenr = dback->disk_bytenr;
4852 entry->bytes = dback->bytes;
4853 list_add_tail(&entry->list, &entries);
4858 * If we only have on entry we may think the entries agree when
4859 * in reality they don't so we have to do some extra checking.
4861 if (dback->disk_bytenr != rec->start ||
4862 dback->bytes != rec->nr || back->broken)
4873 /* Yay all the backrefs agree, carry on good sir */
4874 if (nr_entries <= 1 && !mismatch)
4877 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
4878 "%Lu\n", rec->start);
4881 * First we want to see if the backrefs can agree amongst themselves who
4882 * is right, so figure out which one of the entries has the highest
4885 best = find_most_right_entry(&entries);
4888 * Ok so we may have an even split between what the backrefs think, so
4889 * this is where we use the extent ref to see what it thinks.
4892 entry = find_entry(&entries, rec->start, rec->nr);
4893 if (!entry && (!broken_entries || !rec->found_rec)) {
4894 fprintf(stderr, "Backrefs don't agree with each other "
4895 "and extent record doesn't agree with anybody,"
4896 " so we can't fix bytenr %Lu bytes %Lu\n",
4897 rec->start, rec->nr);
4900 } else if (!entry) {
4902 * Ok our backrefs were broken, we'll assume this is the
4903 * correct value and add an entry for this range.
4905 entry = malloc(sizeof(struct extent_entry));
4910 memset(entry, 0, sizeof(*entry));
4911 entry->bytenr = rec->start;
4912 entry->bytes = rec->nr;
4913 list_add_tail(&entry->list, &entries);
4917 best = find_most_right_entry(&entries);
4919 fprintf(stderr, "Backrefs and extent record evenly "
4920 "split on who is right, this is going to "
4921 "require user input to fix bytenr %Lu bytes "
4922 "%Lu\n", rec->start, rec->nr);
4929 * I don't think this can happen currently as we'll abort() if we catch
4930 * this case higher up, but in case somebody removes that we still can't
4931 * deal with it properly here yet, so just bail out of that's the case.
4933 if (best->bytenr != rec->start) {
4934 fprintf(stderr, "Extent start and backref starts don't match, "
4935 "please use btrfs-image on this file system and send "
4936 "it to a btrfs developer so they can make fsck fix "
4937 "this particular case. bytenr is %Lu, bytes is %Lu\n",
4938 rec->start, rec->nr);
4944 * Ok great we all agreed on an extent record, let's go find the real
4945 * references and fix up the ones that don't match.
4947 list_for_each_entry(back, &rec->backrefs, list) {
4948 dback = (struct data_backref *)back;
4951 * Still ignoring backrefs that don't have a real ref attached
4954 if (dback->found_ref == 0)
4956 if (back->full_backref)
4959 if (dback->bytes == best->bytes &&
4960 dback->disk_bytenr == best->bytenr)
4963 ret = repair_ref(trans, info, path, dback, best);
4969 * Ok we messed with the actual refs, which means we need to drop our
4970 * entire cache and go back and rescan. I know this is a huge pain and
4971 * adds a lot of extra work, but it's the only way to be safe. Once all
4972 * the backrefs agree we may not need to do anything to the extent
4977 while (!list_empty(&entries)) {
4978 entry = list_entry(entries.next, struct extent_entry, list);
4979 list_del_init(&entry->list);
4985 static int process_duplicates(struct btrfs_root *root,
4986 struct cache_tree *extent_cache,
4987 struct extent_record *rec)
4989 struct extent_record *good, *tmp;
4990 struct cache_extent *cache;
4994 * If we found a extent record for this extent then return, or if we
4995 * have more than one duplicate we are likely going to need to delete
4998 if (rec->found_rec || rec->num_duplicates > 1)
5001 /* Shouldn't happen but just in case */
5002 BUG_ON(!rec->num_duplicates);
5005 * So this happens if we end up with a backref that doesn't match the
5006 * actual extent entry. So either the backref is bad or the extent
5007 * entry is bad. Either way we want to have the extent_record actually
5008 * reflect what we found in the extent_tree, so we need to take the
5009 * duplicate out and use that as the extent_record since the only way we
5010 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
5012 remove_cache_extent(extent_cache, &rec->cache);
5014 good = list_entry(rec->dups.next, struct extent_record, list);
5015 list_del_init(&good->list);
5016 INIT_LIST_HEAD(&good->backrefs);
5017 INIT_LIST_HEAD(&good->dups);
5018 good->cache.start = good->start;
5019 good->cache.size = good->nr;
5020 good->content_checked = 0;
5021 good->owner_ref_checked = 0;
5022 good->num_duplicates = 0;
5023 good->refs = rec->refs;
5024 list_splice_init(&rec->backrefs, &good->backrefs);
5026 cache = lookup_cache_extent(extent_cache, good->start,
5030 tmp = container_of(cache, struct extent_record, cache);
5033 * If we find another overlapping extent and it's found_rec is
5034 * set then it's a duplicate and we need to try and delete
5037 if (tmp->found_rec || tmp->num_duplicates > 0) {
5038 if (list_empty(&good->list))
5039 list_add_tail(&good->list,
5040 &duplicate_extents);
5041 good->num_duplicates += tmp->num_duplicates + 1;
5042 list_splice_init(&tmp->dups, &good->dups);
5043 list_del_init(&tmp->list);
5044 list_add_tail(&tmp->list, &good->dups);
5045 remove_cache_extent(extent_cache, &tmp->cache);
5050 * Ok we have another non extent item backed extent rec, so lets
5051 * just add it to this extent and carry on like we did above.
5053 good->refs += tmp->refs;
5054 list_splice_init(&tmp->backrefs, &good->backrefs);
5055 remove_cache_extent(extent_cache, &tmp->cache);
5058 ret = insert_cache_extent(extent_cache, &good->cache);
5061 return good->num_duplicates ? 0 : 1;
5064 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
5065 struct btrfs_root *root,
5066 struct extent_record *rec)
5068 LIST_HEAD(delete_list);
5069 struct btrfs_path *path;
5070 struct extent_record *tmp, *good, *n;
5073 struct btrfs_key key;
5075 path = btrfs_alloc_path();
5082 /* Find the record that covers all of the duplicates. */
5083 list_for_each_entry(tmp, &rec->dups, list) {
5084 if (good->start < tmp->start)
5086 if (good->nr > tmp->nr)
5089 if (tmp->start + tmp->nr < good->start + good->nr) {
5090 fprintf(stderr, "Ok we have overlapping extents that "
5091 "aren't completely covered by eachother, this "
5092 "is going to require more careful thought. "
5093 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
5094 tmp->start, tmp->nr, good->start, good->nr);
5101 list_add_tail(&rec->list, &delete_list);
5103 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
5106 list_move_tail(&tmp->list, &delete_list);
5109 root = root->fs_info->extent_root;
5110 list_for_each_entry(tmp, &delete_list, list) {
5111 if (tmp->found_rec == 0)
5113 key.objectid = tmp->start;
5114 key.type = BTRFS_EXTENT_ITEM_KEY;
5115 key.offset = tmp->nr;
5117 /* Shouldn't happen but just in case */
5118 if (tmp->metadata) {
5119 fprintf(stderr, "Well this shouldn't happen, extent "
5120 "record overlaps but is metadata? "
5121 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
5125 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5131 ret = btrfs_del_item(trans, root, path);
5134 btrfs_release_path(path);
5139 while (!list_empty(&delete_list)) {
5140 tmp = list_entry(delete_list.next, struct extent_record, list);
5141 list_del_init(&tmp->list);
5147 while (!list_empty(&rec->dups)) {
5148 tmp = list_entry(rec->dups.next, struct extent_record, list);
5149 list_del_init(&tmp->list);
5153 btrfs_free_path(path);
5155 if (!ret && !nr_del)
5156 rec->num_duplicates = 0;
5158 return ret ? ret : nr_del;
5161 static int find_possible_backrefs(struct btrfs_trans_handle *trans,
5162 struct btrfs_fs_info *info,
5163 struct btrfs_path *path,
5164 struct cache_tree *extent_cache,
5165 struct extent_record *rec)
5167 struct btrfs_root *root;
5168 struct extent_backref *back;
5169 struct data_backref *dback;
5170 struct cache_extent *cache;
5171 struct btrfs_file_extent_item *fi;
5172 struct btrfs_key key;
5176 list_for_each_entry(back, &rec->backrefs, list) {
5177 dback = (struct data_backref *)back;
5179 /* We found this one, we don't need to do a lookup */
5180 if (dback->found_ref)
5182 /* Don't care about full backrefs (poor unloved backrefs) */
5183 if (back->full_backref)
5185 key.objectid = dback->root;
5186 key.type = BTRFS_ROOT_ITEM_KEY;
5187 key.offset = (u64)-1;
5189 root = btrfs_read_fs_root(info, &key);
5191 /* No root, definitely a bad ref, skip */
5192 if (IS_ERR(root) && PTR_ERR(root) == -ENOENT)
5194 /* Other err, exit */
5196 return PTR_ERR(root);
5198 key.objectid = dback->owner;
5199 key.type = BTRFS_EXTENT_DATA_KEY;
5200 key.offset = dback->offset;
5201 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5203 btrfs_release_path(path);
5206 /* Didn't find it, we can carry on */
5211 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
5212 struct btrfs_file_extent_item);
5213 bytenr = btrfs_file_extent_disk_bytenr(path->nodes[0], fi);
5214 bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
5215 btrfs_release_path(path);
5216 cache = lookup_cache_extent(extent_cache, bytenr, 1);
5218 struct extent_record *tmp;
5219 tmp = container_of(cache, struct extent_record, cache);
5222 * If we found an extent record for the bytenr for this
5223 * particular backref then we can't add it to our
5224 * current extent record. We only want to add backrefs
5225 * that don't have a corresponding extent item in the
5226 * extent tree since they likely belong to this record
5227 * and we need to fix it if it doesn't match bytenrs.
5233 dback->found_ref += 1;
5234 dback->disk_bytenr = bytenr;
5235 dback->bytes = bytes;
5238 * Set this so the verify backref code knows not to trust the
5239 * values in this backref.
5248 * when an incorrect extent item is found, this will delete
5249 * all of the existing entries for it and recreate them
5250 * based on what the tree scan found.
5252 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
5253 struct btrfs_fs_info *info,
5254 struct cache_tree *extent_cache,
5255 struct extent_record *rec)
5258 struct btrfs_path *path;
5259 struct list_head *cur = rec->backrefs.next;
5260 struct cache_extent *cache;
5261 struct extent_backref *back;
5266 * remember our flags for recreating the extent.
5267 * FIXME, if we have cleared extent tree, we can not
5268 * lookup extent info in extent tree.
5270 if (!init_extent_tree) {
5271 ret = btrfs_lookup_extent_info(NULL, info->extent_root,
5272 rec->start, rec->max_size,
5273 rec->metadata, NULL, &flags);
5280 path = btrfs_alloc_path();
5284 if (rec->refs != rec->extent_item_refs && !rec->metadata) {
5286 * Sometimes the backrefs themselves are so broken they don't
5287 * get attached to any meaningful rec, so first go back and
5288 * check any of our backrefs that we couldn't find and throw
5289 * them into the list if we find the backref so that
5290 * verify_backrefs can figure out what to do.
5292 ret = find_possible_backrefs(trans, info, path, extent_cache,
5298 /* step one, make sure all of the backrefs agree */
5299 ret = verify_backrefs(trans, info, path, rec);
5303 /* step two, delete all the existing records */
5304 ret = delete_extent_records(trans, info->extent_root, path,
5305 rec->start, rec->max_size);
5310 /* was this block corrupt? If so, don't add references to it */
5311 cache = lookup_cache_extent(info->corrupt_blocks,
5312 rec->start, rec->max_size);
5318 /* step three, recreate all the refs we did find */
5319 while(cur != &rec->backrefs) {
5320 back = list_entry(cur, struct extent_backref, list);
5324 * if we didn't find any references, don't create a
5327 if (!back->found_ref)
5330 ret = record_extent(trans, info, path, rec, back, allocated, flags);
5337 btrfs_free_path(path);
5341 /* right now we only prune from the extent allocation tree */
5342 static int prune_one_block(struct btrfs_trans_handle *trans,
5343 struct btrfs_fs_info *info,
5344 struct btrfs_corrupt_block *corrupt)
5347 struct btrfs_path path;
5348 struct extent_buffer *eb;
5352 int level = corrupt->level + 1;
5354 btrfs_init_path(&path);
5356 /* we want to stop at the parent to our busted block */
5357 path.lowest_level = level;
5359 ret = btrfs_search_slot(trans, info->extent_root,
5360 &corrupt->key, &path, -1, 1);
5365 eb = path.nodes[level];
5372 * hopefully the search gave us the block we want to prune,
5373 * lets try that first
5375 slot = path.slots[level];
5376 found = btrfs_node_blockptr(eb, slot);
5377 if (found == corrupt->cache.start)
5380 nritems = btrfs_header_nritems(eb);
5382 /* the search failed, lets scan this node and hope we find it */
5383 for (slot = 0; slot < nritems; slot++) {
5384 found = btrfs_node_blockptr(eb, slot);
5385 if (found == corrupt->cache.start)
5389 * we couldn't find the bad block. TODO, search all the nodes for pointers
5392 if (eb == info->extent_root->node) {
5397 btrfs_release_path(&path);
5402 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
5403 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
5406 btrfs_release_path(&path);
5410 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
5411 struct btrfs_fs_info *info)
5413 struct cache_extent *cache;
5414 struct btrfs_corrupt_block *corrupt;
5416 cache = search_cache_extent(info->corrupt_blocks, 0);
5420 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5421 prune_one_block(trans, info, corrupt);
5422 cache = next_cache_extent(cache);
5427 static void free_corrupt_block(struct cache_extent *cache)
5429 struct btrfs_corrupt_block *corrupt;
5431 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
5435 FREE_EXTENT_CACHE_BASED_TREE(corrupt_blocks, free_corrupt_block);
5437 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
5439 struct btrfs_block_group_cache *cache;
5444 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
5445 &start, &end, EXTENT_DIRTY);
5448 clear_extent_dirty(&fs_info->free_space_cache, start, end,
5454 cache = btrfs_lookup_first_block_group(fs_info, start);
5459 start = cache->key.objectid + cache->key.offset;
5463 static int check_extent_refs(struct btrfs_trans_handle *trans,
5464 struct btrfs_root *root,
5465 struct cache_tree *extent_cache)
5467 struct extent_record *rec;
5468 struct cache_extent *cache;
5476 * if we're doing a repair, we have to make sure
5477 * we don't allocate from the problem extents.
5478 * In the worst case, this will be all the
5481 cache = search_cache_extent(extent_cache, 0);
5483 rec = container_of(cache, struct extent_record, cache);
5484 btrfs_pin_extent(root->fs_info,
5485 rec->start, rec->max_size);
5486 cache = next_cache_extent(cache);
5489 /* pin down all the corrupted blocks too */
5490 cache = search_cache_extent(root->fs_info->corrupt_blocks, 0);
5492 btrfs_pin_extent(root->fs_info,
5493 cache->start, cache->size);
5494 cache = next_cache_extent(cache);
5496 prune_corrupt_blocks(trans, root->fs_info);
5497 reset_cached_block_groups(root->fs_info);
5501 * We need to delete any duplicate entries we find first otherwise we
5502 * could mess up the extent tree when we have backrefs that actually
5503 * belong to a different extent item and not the weird duplicate one.
5505 while (repair && !list_empty(&duplicate_extents)) {
5506 rec = list_entry(duplicate_extents.next, struct extent_record,
5508 list_del_init(&rec->list);
5510 /* Sometimes we can find a backref before we find an actual
5511 * extent, so we need to process it a little bit to see if there
5512 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
5513 * if this is a backref screwup. If we need to delete stuff
5514 * process_duplicates() will return 0, otherwise it will return
5517 if (process_duplicates(root, extent_cache, rec))
5519 ret = delete_duplicate_records(trans, root, rec);
5523 * delete_duplicate_records will return the number of entries
5524 * deleted, so if it's greater than 0 then we know we actually
5525 * did something and we need to remove.
5536 cache = search_cache_extent(extent_cache, 0);
5539 rec = container_of(cache, struct extent_record, cache);
5540 if (rec->num_duplicates) {
5541 fprintf(stderr, "extent item %llu has multiple extent "
5542 "items\n", (unsigned long long)rec->start);
5546 if (rec->refs != rec->extent_item_refs) {
5547 fprintf(stderr, "ref mismatch on [%llu %llu] ",
5548 (unsigned long long)rec->start,
5549 (unsigned long long)rec->nr);
5550 fprintf(stderr, "extent item %llu, found %llu\n",
5551 (unsigned long long)rec->extent_item_refs,
5552 (unsigned long long)rec->refs);
5553 if (!fixed && repair) {
5554 ret = fixup_extent_refs(trans, root->fs_info,
5563 if (all_backpointers_checked(rec, 1)) {
5564 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
5565 (unsigned long long)rec->start,
5566 (unsigned long long)rec->nr);
5568 if (!fixed && repair) {
5569 ret = fixup_extent_refs(trans, root->fs_info,
5578 if (!rec->owner_ref_checked) {
5579 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
5580 (unsigned long long)rec->start,
5581 (unsigned long long)rec->nr);
5582 if (!fixed && repair) {
5583 ret = fixup_extent_refs(trans, root->fs_info,
5592 remove_cache_extent(extent_cache, cache);
5593 free_all_extent_backrefs(rec);
5598 if (ret && ret != -EAGAIN) {
5599 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
5602 btrfs_fix_block_accounting(trans, root);
5605 fprintf(stderr, "repaired damaged extent references\n");
5611 u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
5615 if (type & BTRFS_BLOCK_GROUP_RAID0) {
5616 stripe_size = length;
5617 stripe_size /= num_stripes;
5618 } else if (type & BTRFS_BLOCK_GROUP_RAID10) {
5619 stripe_size = length * 2;
5620 stripe_size /= num_stripes;
5621 } else if (type & BTRFS_BLOCK_GROUP_RAID5) {
5622 stripe_size = length;
5623 stripe_size /= (num_stripes - 1);
5624 } else if (type & BTRFS_BLOCK_GROUP_RAID6) {
5625 stripe_size = length;
5626 stripe_size /= (num_stripes - 2);
5628 stripe_size = length;
5633 static int check_chunk_refs(struct chunk_record *chunk_rec,
5634 struct block_group_tree *block_group_cache,
5635 struct device_extent_tree *dev_extent_cache,
5638 struct cache_extent *block_group_item;
5639 struct block_group_record *block_group_rec;
5640 struct cache_extent *dev_extent_item;
5641 struct device_extent_record *dev_extent_rec;
5648 block_group_item = lookup_cache_extent(&block_group_cache->tree,
5651 if (block_group_item) {
5652 block_group_rec = container_of(block_group_item,
5653 struct block_group_record,
5655 if (chunk_rec->length != block_group_rec->offset ||
5656 chunk_rec->offset != block_group_rec->objectid ||
5657 chunk_rec->type_flags != block_group_rec->flags) {
5660 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) mismatch with block group[%llu, %u, %llu]: offset(%llu), objectid(%llu), flags(%llu)\n",
5661 chunk_rec->objectid,
5666 chunk_rec->type_flags,
5667 block_group_rec->objectid,
5668 block_group_rec->type,
5669 block_group_rec->offset,
5670 block_group_rec->offset,
5671 block_group_rec->objectid,
5672 block_group_rec->flags);
5675 list_del_init(&block_group_rec->list);
5676 chunk_rec->bg_rec = block_group_rec;
5681 "Chunk[%llu, %u, %llu]: length(%llu), offset(%llu), type(%llu) is not found in block group\n",
5682 chunk_rec->objectid,
5687 chunk_rec->type_flags);
5691 length = calc_stripe_length(chunk_rec->type_flags, chunk_rec->length,
5692 chunk_rec->num_stripes);
5693 for (i = 0; i < chunk_rec->num_stripes; ++i) {
5694 devid = chunk_rec->stripes[i].devid;
5695 offset = chunk_rec->stripes[i].offset;
5696 dev_extent_item = lookup_cache_extent2(&dev_extent_cache->tree,
5697 devid, offset, length);
5698 if (dev_extent_item) {
5699 dev_extent_rec = container_of(dev_extent_item,
5700 struct device_extent_record,
5702 if (dev_extent_rec->objectid != devid ||
5703 dev_extent_rec->offset != offset ||
5704 dev_extent_rec->chunk_offset != chunk_rec->offset ||
5705 dev_extent_rec->length != length) {
5708 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] dismatch dev extent[%llu, %llu, %llu]\n",
5709 chunk_rec->objectid,
5712 chunk_rec->stripes[i].devid,
5713 chunk_rec->stripes[i].offset,
5714 dev_extent_rec->objectid,
5715 dev_extent_rec->offset,
5716 dev_extent_rec->length);
5719 list_move(&dev_extent_rec->chunk_list,
5720 &chunk_rec->dextents);
5725 "Chunk[%llu, %u, %llu] stripe[%llu, %llu] is not found in dev extent\n",
5726 chunk_rec->objectid,
5729 chunk_rec->stripes[i].devid,
5730 chunk_rec->stripes[i].offset);
5737 /* check btrfs_chunk -> btrfs_dev_extent / btrfs_block_group_item */
5738 int check_chunks(struct cache_tree *chunk_cache,
5739 struct block_group_tree *block_group_cache,
5740 struct device_extent_tree *dev_extent_cache,
5741 struct list_head *good, struct list_head *bad, int silent)
5743 struct cache_extent *chunk_item;
5744 struct chunk_record *chunk_rec;
5745 struct block_group_record *bg_rec;
5746 struct device_extent_record *dext_rec;
5750 chunk_item = first_cache_extent(chunk_cache);
5751 while (chunk_item) {
5752 chunk_rec = container_of(chunk_item, struct chunk_record,
5754 err = check_chunk_refs(chunk_rec, block_group_cache,
5755 dev_extent_cache, silent);
5759 list_add_tail(&chunk_rec->list, bad);
5762 list_add_tail(&chunk_rec->list, good);
5765 chunk_item = next_cache_extent(chunk_item);
5768 list_for_each_entry(bg_rec, &block_group_cache->block_groups, list) {
5771 "Block group[%llu, %llu] (flags = %llu) didn't find the relative chunk.\n",
5779 list_for_each_entry(dext_rec, &dev_extent_cache->no_chunk_orphans,
5783 "Device extent[%llu, %llu, %llu] didn't find the relative chunk.\n",
5794 static int check_device_used(struct device_record *dev_rec,
5795 struct device_extent_tree *dext_cache)
5797 struct cache_extent *cache;
5798 struct device_extent_record *dev_extent_rec;
5801 cache = search_cache_extent2(&dext_cache->tree, dev_rec->devid, 0);
5803 dev_extent_rec = container_of(cache,
5804 struct device_extent_record,
5806 if (dev_extent_rec->objectid != dev_rec->devid)
5809 list_del(&dev_extent_rec->device_list);
5810 total_byte += dev_extent_rec->length;
5811 cache = next_cache_extent(cache);
5814 if (total_byte != dev_rec->byte_used) {
5816 "Dev extent's total-byte(%llu) is not equal to byte-used(%llu) in dev[%llu, %u, %llu]\n",
5817 total_byte, dev_rec->byte_used, dev_rec->objectid,
5818 dev_rec->type, dev_rec->offset);
5825 /* check btrfs_dev_item -> btrfs_dev_extent */
5826 static int check_devices(struct rb_root *dev_cache,
5827 struct device_extent_tree *dev_extent_cache)
5829 struct rb_node *dev_node;
5830 struct device_record *dev_rec;
5831 struct device_extent_record *dext_rec;
5835 dev_node = rb_first(dev_cache);
5837 dev_rec = container_of(dev_node, struct device_record, node);
5838 err = check_device_used(dev_rec, dev_extent_cache);
5842 dev_node = rb_next(dev_node);
5844 list_for_each_entry(dext_rec, &dev_extent_cache->no_device_orphans,
5847 "Device extent[%llu, %llu, %llu] didn't find its device.\n",
5848 dext_rec->objectid, dext_rec->offset, dext_rec->length);
5855 static int check_chunks_and_extents(struct btrfs_root *root)
5857 struct rb_root dev_cache;
5858 struct cache_tree chunk_cache;
5859 struct block_group_tree block_group_cache;
5860 struct device_extent_tree dev_extent_cache;
5861 struct cache_tree extent_cache;
5862 struct cache_tree seen;
5863 struct cache_tree pending;
5864 struct cache_tree reada;
5865 struct cache_tree nodes;
5866 struct cache_tree corrupt_blocks;
5867 struct btrfs_path path;
5868 struct btrfs_key key;
5869 struct btrfs_key found_key;
5872 struct block_info *bits;
5874 struct extent_buffer *leaf;
5875 struct btrfs_trans_handle *trans = NULL;
5877 struct btrfs_root_item ri;
5878 struct list_head dropping_trees;
5880 dev_cache = RB_ROOT;
5881 cache_tree_init(&chunk_cache);
5882 block_group_tree_init(&block_group_cache);
5883 device_extent_tree_init(&dev_extent_cache);
5885 cache_tree_init(&extent_cache);
5886 cache_tree_init(&seen);
5887 cache_tree_init(&pending);
5888 cache_tree_init(&nodes);
5889 cache_tree_init(&reada);
5890 cache_tree_init(&corrupt_blocks);
5891 INIT_LIST_HEAD(&dropping_trees);
5894 trans = btrfs_start_transaction(root, 1);
5895 if (IS_ERR(trans)) {
5896 fprintf(stderr, "Error starting transaction\n");
5897 return PTR_ERR(trans);
5899 root->fs_info->fsck_extent_cache = &extent_cache;
5900 root->fs_info->free_extent_hook = free_extent_hook;
5901 root->fs_info->corrupt_blocks = &corrupt_blocks;
5905 bits = malloc(bits_nr * sizeof(struct block_info));
5912 add_root_to_pending(root->fs_info->tree_root->node,
5913 &extent_cache, &pending, &seen, &nodes,
5914 &root->fs_info->tree_root->root_key);
5916 add_root_to_pending(root->fs_info->chunk_root->node,
5917 &extent_cache, &pending, &seen, &nodes,
5918 &root->fs_info->chunk_root->root_key);
5920 btrfs_init_path(&path);
5923 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
5924 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
5929 leaf = path.nodes[0];
5930 slot = path.slots[0];
5931 if (slot >= btrfs_header_nritems(path.nodes[0])) {
5932 ret = btrfs_next_leaf(root, &path);
5935 leaf = path.nodes[0];
5936 slot = path.slots[0];
5938 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
5939 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
5940 unsigned long offset;
5941 struct extent_buffer *buf;
5943 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5944 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5945 if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
5946 buf = read_tree_block(root->fs_info->tree_root,
5947 btrfs_root_bytenr(&ri),
5948 btrfs_level_size(root,
5949 btrfs_root_level(&ri)),
5955 add_root_to_pending(buf, &extent_cache,
5956 &pending, &seen, &nodes,
5958 free_extent_buffer(buf);
5960 struct dropping_root_item_record *dri_rec;
5961 dri_rec = malloc(sizeof(*dri_rec));
5966 memcpy(&dri_rec->ri, &ri, sizeof(ri));
5967 memcpy(&dri_rec->found_key, &found_key,
5969 list_add_tail(&dri_rec->list, &dropping_trees);
5974 btrfs_release_path(&path);
5976 ret = run_next_block(trans, root, bits, bits_nr, &last,
5977 &pending, &seen, &reada, &nodes,
5978 &extent_cache, &chunk_cache, &dev_cache,
5979 &block_group_cache, &dev_extent_cache,
5985 while (!list_empty(&dropping_trees)) {
5986 struct dropping_root_item_record *rec;
5987 struct extent_buffer *buf;
5988 rec = list_entry(dropping_trees.next,
5989 struct dropping_root_item_record, list);
5995 buf = read_tree_block(root->fs_info->tree_root,
5996 btrfs_root_bytenr(&rec->ri),
5997 btrfs_level_size(root,
5998 btrfs_root_level(&rec->ri)), 0);
6003 add_root_to_pending(buf, &extent_cache, &pending,
6004 &seen, &nodes, &rec->found_key);
6006 ret = run_next_block(trans, root, bits, bits_nr, &last,
6007 &pending, &seen, &reada,
6008 &nodes, &extent_cache,
6009 &chunk_cache, &dev_cache,
6016 free_extent_buffer(buf);
6017 list_del(&rec->list);
6022 ret = check_extent_refs(trans, root, &extent_cache);
6023 if (ret == -EAGAIN) {
6024 ret = btrfs_commit_transaction(trans, root);
6028 trans = btrfs_start_transaction(root, 1);
6029 if (IS_ERR(trans)) {
6030 ret = PTR_ERR(trans);
6034 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6035 free_extent_cache_tree(&seen);
6036 free_extent_cache_tree(&pending);
6037 free_extent_cache_tree(&reada);
6038 free_extent_cache_tree(&nodes);
6039 free_extent_record_cache(root->fs_info, &extent_cache);
6043 err = check_chunks(&chunk_cache, &block_group_cache,
6044 &dev_extent_cache, NULL, NULL, 0);
6048 err = check_devices(&dev_cache, &dev_extent_cache);
6053 err = btrfs_commit_transaction(trans, root);
6059 free_corrupt_blocks_tree(root->fs_info->corrupt_blocks);
6060 root->fs_info->fsck_extent_cache = NULL;
6061 root->fs_info->free_extent_hook = NULL;
6062 root->fs_info->corrupt_blocks = NULL;
6065 free_chunk_cache_tree(&chunk_cache);
6066 free_device_cache_tree(&dev_cache);
6067 free_block_group_tree(&block_group_cache);
6068 free_device_extent_tree(&dev_extent_cache);
6069 free_extent_cache_tree(&seen);
6070 free_extent_cache_tree(&pending);
6071 free_extent_cache_tree(&reada);
6072 free_extent_cache_tree(&nodes);
6076 static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
6077 struct btrfs_root *root, int overwrite)
6079 struct extent_buffer *c;
6080 struct extent_buffer *old = root->node;
6083 struct btrfs_disk_key disk_key = {0,0,0};
6089 extent_buffer_get(c);
6092 c = btrfs_alloc_free_block(trans, root,
6093 btrfs_level_size(root, 0),
6094 root->root_key.objectid,
6095 &disk_key, level, 0, 0);
6098 extent_buffer_get(c);
6102 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
6103 btrfs_set_header_level(c, level);
6104 btrfs_set_header_bytenr(c, c->start);
6105 btrfs_set_header_generation(c, trans->transid);
6106 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
6107 btrfs_set_header_owner(c, root->root_key.objectid);
6109 write_extent_buffer(c, root->fs_info->fsid,
6110 btrfs_header_fsid(), BTRFS_FSID_SIZE);
6112 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
6113 btrfs_header_chunk_tree_uuid(c),
6116 btrfs_mark_buffer_dirty(c);
6118 * this case can happen in the following case:
6120 * 1.overwrite previous root.
6122 * 2.reinit reloc data root, this is because we skip pin
6123 * down reloc data tree before which means we can allocate
6124 * same block bytenr here.
6126 if (old->start == c->start) {
6127 btrfs_set_root_generation(&root->root_item,
6129 root->root_item.level = btrfs_header_level(root->node);
6130 ret = btrfs_update_root(trans, root->fs_info->tree_root,
6131 &root->root_key, &root->root_item);
6133 free_extent_buffer(c);
6137 free_extent_buffer(old);
6139 add_root_to_dirty_list(root);
6143 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
6144 struct extent_buffer *eb, int tree_root)
6146 struct extent_buffer *tmp;
6147 struct btrfs_root_item *ri;
6148 struct btrfs_key key;
6151 int level = btrfs_header_level(eb);
6157 * If we have pinned this block before, don't pin it again.
6158 * This can not only avoid forever loop with broken filesystem
6159 * but also give us some speedups.
6161 if (test_range_bit(&fs_info->pinned_extents, eb->start,
6162 eb->start + eb->len - 1, EXTENT_DIRTY, 0))
6165 btrfs_pin_extent(fs_info, eb->start, eb->len);
6167 leafsize = btrfs_super_leafsize(fs_info->super_copy);
6168 nritems = btrfs_header_nritems(eb);
6169 for (i = 0; i < nritems; i++) {
6171 btrfs_item_key_to_cpu(eb, &key, i);
6172 if (key.type != BTRFS_ROOT_ITEM_KEY)
6174 /* Skip the extent root and reloc roots */
6175 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6176 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
6177 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
6179 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
6180 bytenr = btrfs_disk_root_bytenr(eb, ri);
6183 * If at any point we start needing the real root we
6184 * will have to build a stump root for the root we are
6185 * in, but for now this doesn't actually use the root so
6186 * just pass in extent_root.
6188 tmp = read_tree_block(fs_info->extent_root, bytenr,
6191 fprintf(stderr, "Error reading root block\n");
6194 ret = pin_down_tree_blocks(fs_info, tmp, 0);
6195 free_extent_buffer(tmp);
6199 bytenr = btrfs_node_blockptr(eb, i);
6201 /* If we aren't the tree root don't read the block */
6202 if (level == 1 && !tree_root) {
6203 btrfs_pin_extent(fs_info, bytenr, leafsize);
6207 tmp = read_tree_block(fs_info->extent_root, bytenr,
6210 fprintf(stderr, "Error reading tree block\n");
6213 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
6214 free_extent_buffer(tmp);
6223 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
6227 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
6231 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
6234 static int reset_block_groups(struct btrfs_fs_info *fs_info)
6236 struct btrfs_block_group_cache *cache;
6237 struct btrfs_path *path;
6238 struct extent_buffer *leaf;
6239 struct btrfs_chunk *chunk;
6240 struct btrfs_key key;
6244 path = btrfs_alloc_path();
6249 key.type = BTRFS_CHUNK_ITEM_KEY;
6252 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
6254 btrfs_free_path(path);
6259 * We do this in case the block groups were screwed up and had alloc
6260 * bits that aren't actually set on the chunks. This happens with
6261 * restored images every time and could happen in real life I guess.
6263 fs_info->avail_data_alloc_bits = 0;
6264 fs_info->avail_metadata_alloc_bits = 0;
6265 fs_info->avail_system_alloc_bits = 0;
6267 /* First we need to create the in-memory block groups */
6269 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6270 ret = btrfs_next_leaf(fs_info->chunk_root, path);
6272 btrfs_free_path(path);
6280 leaf = path->nodes[0];
6281 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6282 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
6287 chunk = btrfs_item_ptr(leaf, path->slots[0],
6288 struct btrfs_chunk);
6289 btrfs_add_block_group(fs_info, 0,
6290 btrfs_chunk_type(leaf, chunk),
6291 key.objectid, key.offset,
6292 btrfs_chunk_length(leaf, chunk));
6293 set_extent_dirty(&fs_info->free_space_cache, key.offset,
6294 key.offset + btrfs_chunk_length(leaf, chunk),
6300 cache = btrfs_lookup_first_block_group(fs_info, start);
6304 start = cache->key.objectid + cache->key.offset;
6307 btrfs_free_path(path);
6311 static int reset_balance(struct btrfs_trans_handle *trans,
6312 struct btrfs_fs_info *fs_info)
6314 struct btrfs_root *root = fs_info->tree_root;
6315 struct btrfs_path *path;
6316 struct extent_buffer *leaf;
6317 struct btrfs_key key;
6318 int del_slot, del_nr = 0;
6322 path = btrfs_alloc_path();
6326 key.objectid = BTRFS_BALANCE_OBJECTID;
6327 key.type = BTRFS_BALANCE_ITEM_KEY;
6330 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6335 goto reinit_data_reloc;
6340 ret = btrfs_del_item(trans, root, path);
6343 btrfs_release_path(path);
6345 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
6346 key.type = BTRFS_ROOT_ITEM_KEY;
6349 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6353 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
6358 ret = btrfs_del_items(trans, root, path,
6365 btrfs_release_path(path);
6368 ret = btrfs_search_slot(trans, root, &key, path,
6375 leaf = path->nodes[0];
6376 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6377 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
6379 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6384 del_slot = path->slots[0];
6393 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
6397 btrfs_release_path(path);
6400 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6401 key.type = BTRFS_ROOT_ITEM_KEY;
6402 key.offset = (u64)-1;
6403 root = btrfs_read_fs_root(fs_info, &key);
6405 fprintf(stderr, "Error reading data reloc tree\n");
6406 return PTR_ERR(root);
6408 record_root_in_trans(trans, root);
6409 ret = btrfs_fsck_reinit_root(trans, root, 0);
6412 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
6414 btrfs_free_path(path);
6418 static int reinit_extent_tree(struct btrfs_trans_handle *trans,
6419 struct btrfs_fs_info *fs_info)
6425 * The only reason we don't do this is because right now we're just
6426 * walking the trees we find and pinning down their bytes, we don't look
6427 * at any of the leaves. In order to do mixed groups we'd have to check
6428 * the leaves of any fs roots and pin down the bytes for any file
6429 * extents we find. Not hard but why do it if we don't have to?
6431 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
6432 fprintf(stderr, "We don't support re-initing the extent tree "
6433 "for mixed block groups yet, please notify a btrfs "
6434 "developer you want to do this so they can add this "
6435 "functionality.\n");
6440 * first we need to walk all of the trees except the extent tree and pin
6441 * down the bytes that are in use so we don't overwrite any existing
6444 ret = pin_metadata_blocks(fs_info);
6446 fprintf(stderr, "error pinning down used bytes\n");
6451 * Need to drop all the block groups since we're going to recreate all
6454 btrfs_free_block_groups(fs_info);
6455 ret = reset_block_groups(fs_info);
6457 fprintf(stderr, "error resetting the block groups\n");
6461 /* Ok we can allocate now, reinit the extent root */
6462 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
6464 fprintf(stderr, "extent root initialization failed\n");
6466 * When the transaction code is updated we should end the
6467 * transaction, but for now progs only knows about commit so
6468 * just return an error.
6474 * Now we have all the in-memory block groups setup so we can make
6475 * allocations properly, and the metadata we care about is safe since we
6476 * pinned all of it above.
6479 struct btrfs_block_group_cache *cache;
6481 cache = btrfs_lookup_first_block_group(fs_info, start);
6484 start = cache->key.objectid + cache->key.offset;
6485 ret = btrfs_insert_item(trans, fs_info->extent_root,
6486 &cache->key, &cache->item,
6487 sizeof(cache->item));
6489 fprintf(stderr, "Error adding block group\n");
6492 btrfs_extent_post_op(trans, fs_info->extent_root);
6495 ret = reset_balance(trans, fs_info);
6497 fprintf(stderr, "error reseting the pending balance\n");
6502 static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
6504 struct btrfs_path *path;
6505 struct btrfs_trans_handle *trans;
6506 struct btrfs_key key;
6509 printf("Recowing metadata block %llu\n", eb->start);
6510 key.objectid = btrfs_header_owner(eb);
6511 key.type = BTRFS_ROOT_ITEM_KEY;
6512 key.offset = (u64)-1;
6514 root = btrfs_read_fs_root(root->fs_info, &key);
6516 fprintf(stderr, "Couldn't find owner root %llu\n",
6518 return PTR_ERR(root);
6521 path = btrfs_alloc_path();
6525 trans = btrfs_start_transaction(root, 1);
6526 if (IS_ERR(trans)) {
6527 btrfs_free_path(path);
6528 return PTR_ERR(trans);
6531 path->lowest_level = btrfs_header_level(eb);
6532 if (path->lowest_level)
6533 btrfs_node_key_to_cpu(eb, &key, 0);
6535 btrfs_item_key_to_cpu(eb, &key, 0);
6537 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6538 btrfs_commit_transaction(trans, root);
6539 btrfs_free_path(path);
6543 static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
6545 struct btrfs_path *path;
6546 struct btrfs_trans_handle *trans;
6547 struct btrfs_key key;
6550 printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
6551 bad->key.type, bad->key.offset);
6552 key.objectid = bad->root_id;
6553 key.type = BTRFS_ROOT_ITEM_KEY;
6554 key.offset = (u64)-1;
6556 root = btrfs_read_fs_root(root->fs_info, &key);
6558 fprintf(stderr, "Couldn't find owner root %llu\n",
6560 return PTR_ERR(root);
6563 path = btrfs_alloc_path();
6567 trans = btrfs_start_transaction(root, 1);
6568 if (IS_ERR(trans)) {
6569 btrfs_free_path(path);
6570 return PTR_ERR(trans);
6573 ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
6579 ret = btrfs_del_item(trans, root, path);
6581 btrfs_commit_transaction(trans, root);
6582 btrfs_free_path(path);
6586 static int zero_log_tree(struct btrfs_root *root)
6588 struct btrfs_trans_handle *trans;
6591 trans = btrfs_start_transaction(root, 1);
6592 if (IS_ERR(trans)) {
6593 ret = PTR_ERR(trans);
6596 btrfs_set_super_log_root(root->fs_info->super_copy, 0);
6597 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0);
6598 ret = btrfs_commit_transaction(trans, root);
6602 static struct option long_options[] = {
6603 { "super", 1, NULL, 's' },
6604 { "repair", 0, NULL, 0 },
6605 { "init-csum-tree", 0, NULL, 0 },
6606 { "init-extent-tree", 0, NULL, 0 },
6607 { "check-data-csum", 0, NULL, 0 },
6608 { "backup", 0, NULL, 0 },
6609 { "subvol-extents", no_argument, NULL, 'E' },
6610 { "qgroup-report", 0, NULL, 'Q' },
6614 const char * const cmd_check_usage[] = {
6615 "btrfs check [options] <device>",
6616 "Check an unmounted btrfs filesystem.",
6618 "-s|--super <superblock> use this superblock copy",
6619 "-b|--backup use the backup root copy",
6620 "--repair try to repair the filesystem",
6621 "--init-csum-tree create a new CRC tree",
6622 "--init-extent-tree create a new extent tree",
6623 "--check-data-csum verify checkums of data blocks",
6624 "--qgroup-report print a report on qgroup consistency",
6625 "--subvol-extents print subvolume extents and sharing state",
6629 int cmd_check(int argc, char **argv)
6631 struct cache_tree root_cache;
6632 struct btrfs_root *root;
6633 struct btrfs_fs_info *info;
6636 char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
6639 int option_index = 0;
6640 int init_csum_tree = 0;
6641 int qgroup_report = 0;
6642 enum btrfs_open_ctree_flags ctree_flags =
6643 OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
6647 c = getopt_long(argc, argv, "as:b", long_options,
6652 case 'a': /* ignored */ break;
6654 ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6657 num = arg_strtou64(optarg);
6658 if (num >= BTRFS_SUPER_MIRROR_MAX) {
6660 "ERROR: super mirror should be less than: %d\n",
6661 BTRFS_SUPER_MIRROR_MAX);
6664 bytenr = btrfs_sb_offset(((int)num));
6665 printf("using SB copy %llu, bytenr %llu\n", num,
6666 (unsigned long long)bytenr);
6672 subvolid = arg_strtou64(optarg);
6676 usage(cmd_check_usage);
6678 if (option_index == 1) {
6679 printf("enabling repair mode\n");
6681 ctree_flags |= OPEN_CTREE_WRITES;
6682 } else if (option_index == 2) {
6683 printf("Creating a new CRC tree\n");
6686 ctree_flags |= OPEN_CTREE_WRITES;
6687 } else if (option_index == 3) {
6688 init_extent_tree = 1;
6689 ctree_flags |= (OPEN_CTREE_WRITES |
6690 OPEN_CTREE_NO_BLOCK_GROUPS);
6692 } else if (option_index == 4) {
6693 check_data_csum = 1;
6696 argc = argc - optind;
6698 if (check_argc_exact(argc, 1))
6699 usage(cmd_check_usage);
6702 cache_tree_init(&root_cache);
6704 if((ret = check_mounted(argv[optind])) < 0) {
6705 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6708 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6713 info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6715 fprintf(stderr, "Couldn't open file system\n");
6720 root = info->fs_root;
6722 * repair mode will force us to commit transaction which
6723 * will make us fail to load log tree when mounting.
6725 if (repair && btrfs_super_log_root(info->super_copy)) {
6726 ret = ask_user("repair mode will force to clear out log tree, Are you sure?");
6731 ret = zero_log_tree(root);
6733 fprintf(stderr, "fail to zero log tree\n");
6738 uuid_unparse(info->super_copy->fsid, uuidbuf);
6739 if (qgroup_report) {
6740 printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
6742 ret = qgroup_verify_all(info);
6744 print_qgroup_report(1);
6748 printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
6749 subvolid, argv[optind], uuidbuf);
6750 ret = print_extent_state(info, subvolid);
6753 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6755 if (!extent_buffer_uptodate(info->tree_root->node) ||
6756 !extent_buffer_uptodate(info->dev_root->node) ||
6757 !extent_buffer_uptodate(info->chunk_root->node)) {
6758 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6763 if (init_extent_tree || init_csum_tree) {
6764 struct btrfs_trans_handle *trans;
6766 trans = btrfs_start_transaction(info->extent_root, 0);
6767 if (IS_ERR(trans)) {
6768 fprintf(stderr, "Error starting transaction\n");
6769 ret = PTR_ERR(trans);
6773 if (init_extent_tree) {
6774 printf("Creating a new extent tree\n");
6775 ret = reinit_extent_tree(trans, info);
6780 if (init_csum_tree) {
6781 fprintf(stderr, "Reinit crc root\n");
6782 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6784 fprintf(stderr, "crc root initialization failed\n");
6790 * Ok now we commit and run the normal fsck, which will add
6791 * extent entries for all of the items it finds.
6793 ret = btrfs_commit_transaction(trans, info->extent_root);
6797 if (!extent_buffer_uptodate(info->extent_root->node)) {
6798 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6803 fprintf(stderr, "checking extents\n");
6804 ret = check_chunks_and_extents(root);
6806 fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6808 fprintf(stderr, "checking free space cache\n");
6809 ret = check_space_cache(root);
6814 * We used to have to have these hole extents in between our real
6815 * extents so if we don't have this flag set we need to make sure there
6816 * are no gaps in the file extents for inodes, otherwise we can just
6817 * ignore it when this happens.
6819 no_holes = btrfs_fs_incompat(root->fs_info,
6820 BTRFS_FEATURE_INCOMPAT_NO_HOLES);
6821 fprintf(stderr, "checking fs roots\n");
6822 ret = check_fs_roots(root, &root_cache);
6826 fprintf(stderr, "checking csums\n");
6827 ret = check_csums(root);
6831 fprintf(stderr, "checking root refs\n");
6832 ret = check_root_refs(root, &root_cache);
6836 while (repair && !list_empty(&root->fs_info->recow_ebs)) {
6837 struct extent_buffer *eb;
6839 eb = list_first_entry(&root->fs_info->recow_ebs,
6840 struct extent_buffer, recow);
6841 ret = recow_extent_buffer(root, eb);
6846 while (!list_empty(&delete_items)) {
6847 struct bad_item *bad;
6849 bad = list_first_entry(&delete_items, struct bad_item, list);
6850 list_del_init(&bad->list);
6852 ret = delete_bad_item(root, bad);
6856 if (info->quota_enabled) {
6858 fprintf(stderr, "checking quota groups\n");
6859 err = qgroup_verify_all(info);
6864 if (!list_empty(&root->fs_info->recow_ebs)) {
6865 fprintf(stderr, "Transid errors in file system\n");
6869 print_qgroup_report(0);
6870 if (found_old_backref) { /*
6871 * there was a disk format change when mixed
6872 * backref was in testing tree. The old format
6873 * existed about one week.
6875 printf("\n * Found old mixed backref format. "
6876 "The old format is not supported! *"
6877 "\n * Please mount the FS in readonly mode, "
6878 "backup data and re-format the FS. *\n\n");
6881 printf("found %llu bytes used err is %d\n",
6882 (unsigned long long)bytes_used, ret);
6883 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
6884 printf("total tree bytes: %llu\n",
6885 (unsigned long long)total_btree_bytes);
6886 printf("total fs tree bytes: %llu\n",
6887 (unsigned long long)total_fs_tree_bytes);
6888 printf("total extent tree bytes: %llu\n",
6889 (unsigned long long)total_extent_tree_bytes);
6890 printf("btree space waste bytes: %llu\n",
6891 (unsigned long long)btree_space_waste);
6892 printf("file data blocks allocated: %llu\n referenced %llu\n",
6893 (unsigned long long)data_bytes_allocated,
6894 (unsigned long long)data_bytes_referenced);
6895 printf("%s\n", BTRFS_BUILD_VERSION);
6897 free_root_recs_tree(&root_cache);