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
27 #include "kerncompat.h"
31 #include "print-tree.h"
32 #include "transaction.h"
37 static u64 bytes_used = 0;
38 static u64 total_csum_bytes = 0;
39 static u64 total_btree_bytes = 0;
40 static u64 total_fs_tree_bytes = 0;
41 static u64 btree_space_waste = 0;
42 static u64 data_bytes_allocated = 0;
43 static u64 data_bytes_referenced = 0;
44 static int found_old_backref = 0;
46 struct extent_backref {
47 struct list_head list;
48 unsigned int is_data:1;
49 unsigned int found_extent_tree:1;
50 unsigned int full_backref:1;
51 unsigned int found_ref:1;
55 struct extent_backref node;
67 struct extent_backref node;
74 struct extent_record {
75 struct list_head backrefs;
76 struct cache_extent cache;
77 struct btrfs_disk_key parent_key;
86 unsigned int content_checked:1;
87 unsigned int owner_ref_checked:1;
88 unsigned int is_root:1;
91 struct inode_backref {
92 struct list_head list;
93 unsigned int found_dir_item:1;
94 unsigned int found_dir_index:1;
95 unsigned int found_inode_ref:1;
96 unsigned int filetype:8;
104 #define REF_ERR_NO_DIR_ITEM (1 << 0)
105 #define REF_ERR_NO_DIR_INDEX (1 << 1)
106 #define REF_ERR_NO_INODE_REF (1 << 2)
107 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
108 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
109 #define REF_ERR_DUP_INODE_REF (1 << 5)
110 #define REF_ERR_INDEX_UNMATCH (1 << 6)
111 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
112 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
113 #define REF_ERR_NO_ROOT_REF (1 << 9)
114 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
115 #define REF_ERR_DUP_ROOT_REF (1 << 11)
116 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
118 struct inode_record {
119 struct list_head backrefs;
120 unsigned int checked:1;
121 unsigned int merging:1;
122 unsigned int found_inode_item:1;
123 unsigned int found_dir_item:1;
124 unsigned int found_file_extent:1;
125 unsigned int found_csum_item:1;
126 unsigned int some_csum_missing:1;
127 unsigned int nodatasum:1;
140 u64 first_extent_gap;
145 #define I_ERR_NO_INODE_ITEM (1 << 0)
146 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
147 #define I_ERR_DUP_INODE_ITEM (1 << 2)
148 #define I_ERR_DUP_DIR_INDEX (1 << 3)
149 #define I_ERR_ODD_DIR_ITEM (1 << 4)
150 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
151 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
152 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
153 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
154 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
155 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
156 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
157 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
158 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
160 struct root_backref {
161 struct list_head list;
162 unsigned int found_dir_item:1;
163 unsigned int found_dir_index:1;
164 unsigned int found_back_ref:1;
165 unsigned int found_forward_ref:1;
166 unsigned int reachable:1;
176 struct list_head backrefs;
177 struct cache_extent cache;
178 unsigned int found_root_item:1;
184 struct cache_extent cache;
189 struct cache_extent cache;
190 struct cache_tree root_cache;
191 struct cache_tree inode_cache;
192 struct inode_record *current;
201 struct walk_control {
202 struct cache_tree shared;
203 struct shared_node *nodes[BTRFS_MAX_LEVEL];
208 static u8 imode_to_type(u32 imode)
211 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
212 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
213 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
214 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
215 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
216 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
217 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
218 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
221 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
225 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
227 struct inode_record *rec;
228 struct inode_backref *backref;
229 struct inode_backref *orig;
232 rec = malloc(sizeof(*rec));
233 memcpy(rec, orig_rec, sizeof(*rec));
235 INIT_LIST_HEAD(&rec->backrefs);
237 list_for_each_entry(orig, &orig_rec->backrefs, list) {
238 size = sizeof(*orig) + orig->namelen + 1;
239 backref = malloc(size);
240 memcpy(backref, orig, size);
241 list_add_tail(&backref->list, &rec->backrefs);
246 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
249 struct ptr_node *node;
250 struct cache_extent *cache;
251 struct inode_record *rec = NULL;
254 cache = find_cache_extent(inode_cache, ino, 1);
256 node = container_of(cache, struct ptr_node, cache);
258 if (mod && rec->refs > 1) {
259 node->data = clone_inode_rec(rec);
264 rec = calloc(1, sizeof(*rec));
266 rec->extent_start = (u64)-1;
267 rec->first_extent_gap = (u64)-1;
269 INIT_LIST_HEAD(&rec->backrefs);
271 node = malloc(sizeof(*node));
272 node->cache.start = ino;
273 node->cache.size = 1;
276 ret = insert_existing_cache_extent(inode_cache, &node->cache);
282 static void free_inode_rec(struct inode_record *rec)
284 struct inode_backref *backref;
289 while (!list_empty(&rec->backrefs)) {
290 backref = list_entry(rec->backrefs.next,
291 struct inode_backref, list);
292 list_del(&backref->list);
298 static int can_free_inode_rec(struct inode_record *rec)
300 if (!rec->errors && rec->checked && rec->found_inode_item &&
301 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
306 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
307 struct inode_record *rec)
309 struct cache_extent *cache;
310 struct inode_backref *tmp, *backref;
311 struct ptr_node *node;
312 unsigned char filetype;
314 if (!rec->found_inode_item)
317 filetype = imode_to_type(rec->imode);
318 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
319 if (backref->found_dir_item && backref->found_dir_index) {
320 if (backref->filetype != filetype)
321 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
322 if (!backref->errors && backref->found_inode_ref) {
323 list_del(&backref->list);
329 if (!rec->checked || rec->merging)
332 if (S_ISDIR(rec->imode)) {
333 if (rec->found_size != rec->isize)
334 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
335 if (rec->found_file_extent)
336 rec->errors |= I_ERR_ODD_FILE_EXTENT;
337 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
338 if (rec->found_dir_item)
339 rec->errors |= I_ERR_ODD_DIR_ITEM;
340 if (rec->found_size != rec->nbytes)
341 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
342 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
343 rec->first_extent_gap = 0;
344 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
345 rec->first_extent_gap < rec->isize))
346 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
349 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
350 if (rec->found_csum_item && rec->nodatasum)
351 rec->errors |= I_ERR_ODD_CSUM_ITEM;
352 if (rec->some_csum_missing && !rec->nodatasum)
353 rec->errors |= I_ERR_SOME_CSUM_MISSING;
356 BUG_ON(rec->refs != 1);
357 if (can_free_inode_rec(rec)) {
358 cache = find_cache_extent(inode_cache, rec->ino, 1);
359 node = container_of(cache, struct ptr_node, cache);
360 BUG_ON(node->data != rec);
361 remove_cache_extent(inode_cache, &node->cache);
367 static int check_orphan_item(struct btrfs_root *root, u64 ino)
369 struct btrfs_path path;
370 struct btrfs_key key;
373 key.objectid = BTRFS_ORPHAN_OBJECTID;
374 key.type = BTRFS_ORPHAN_ITEM_KEY;
377 btrfs_init_path(&path);
378 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
379 btrfs_release_path(root, &path);
385 static int process_inode_item(struct extent_buffer *eb,
386 int slot, struct btrfs_key *key,
387 struct shared_node *active_node)
389 struct inode_record *rec;
390 struct btrfs_inode_item *item;
392 rec = active_node->current;
393 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
394 if (rec->found_inode_item) {
395 rec->errors |= I_ERR_DUP_INODE_ITEM;
398 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
399 rec->nlink = btrfs_inode_nlink(eb, item);
400 rec->isize = btrfs_inode_size(eb, item);
401 rec->nbytes = btrfs_inode_nbytes(eb, item);
402 rec->imode = btrfs_inode_mode(eb, item);
403 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
405 rec->found_inode_item = 1;
407 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
408 maybe_free_inode_rec(&active_node->inode_cache, rec);
412 static struct inode_backref *get_inode_backref(struct inode_record *rec,
414 int namelen, u64 dir)
416 struct inode_backref *backref;
418 list_for_each_entry(backref, &rec->backrefs, list) {
419 if (backref->dir != dir || backref->namelen != namelen)
421 if (memcmp(name, backref->name, namelen))
426 backref = malloc(sizeof(*backref) + namelen + 1);
427 memset(backref, 0, sizeof(*backref));
429 backref->namelen = namelen;
430 memcpy(backref->name, name, namelen);
431 backref->name[namelen] = '\0';
432 list_add_tail(&backref->list, &rec->backrefs);
436 static int add_inode_backref(struct cache_tree *inode_cache,
437 u64 ino, u64 dir, u64 index,
438 const char *name, int namelen,
439 int filetype, int itemtype, int errors)
441 struct inode_record *rec;
442 struct inode_backref *backref;
444 rec = get_inode_rec(inode_cache, ino, 1);
445 backref = get_inode_backref(rec, name, namelen, dir);
447 backref->errors |= errors;
448 if (itemtype == BTRFS_DIR_INDEX_KEY) {
449 if (backref->found_dir_index)
450 backref->errors |= REF_ERR_DUP_DIR_INDEX;
451 if (backref->found_inode_ref && backref->index != index)
452 backref->errors |= REF_ERR_INDEX_UNMATCH;
453 if (backref->found_dir_item && backref->filetype != filetype)
454 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
456 backref->index = index;
457 backref->filetype = filetype;
458 backref->found_dir_index = 1;
459 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
461 if (backref->found_dir_item)
462 backref->errors |= REF_ERR_DUP_DIR_ITEM;
463 if (backref->found_dir_index && backref->filetype != filetype)
464 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
466 backref->filetype = filetype;
467 backref->found_dir_item = 1;
468 } else if (itemtype == BTRFS_INODE_REF_KEY) {
469 if (backref->found_inode_ref)
470 backref->errors |= REF_ERR_DUP_INODE_REF;
471 if (backref->found_dir_index && backref->index != index)
472 backref->errors |= REF_ERR_INDEX_UNMATCH;
474 backref->index = index;
475 backref->found_inode_ref = 1;
480 maybe_free_inode_rec(inode_cache, rec);
484 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
485 struct cache_tree *dst_cache)
487 struct inode_backref *backref;
491 list_for_each_entry(backref, &src->backrefs, list) {
492 if (backref->found_dir_index) {
493 add_inode_backref(dst_cache, dst->ino, backref->dir,
494 backref->index, backref->name,
495 backref->namelen, backref->filetype,
496 BTRFS_DIR_INDEX_KEY, backref->errors);
498 if (backref->found_dir_item) {
500 add_inode_backref(dst_cache, dst->ino,
501 backref->dir, 0, backref->name,
502 backref->namelen, backref->filetype,
503 BTRFS_DIR_ITEM_KEY, backref->errors);
505 if (backref->found_inode_ref) {
506 add_inode_backref(dst_cache, dst->ino,
507 backref->dir, backref->index,
508 backref->name, backref->namelen, 0,
509 BTRFS_INODE_REF_KEY, backref->errors);
513 if (src->found_dir_item)
514 dst->found_dir_item = 1;
515 if (src->found_file_extent)
516 dst->found_file_extent = 1;
517 if (src->found_csum_item)
518 dst->found_csum_item = 1;
519 if (src->some_csum_missing)
520 dst->some_csum_missing = 1;
521 if (dst->first_extent_gap > src->first_extent_gap)
522 dst->first_extent_gap = src->first_extent_gap;
524 BUG_ON(src->found_link < dir_count);
525 dst->found_link += src->found_link - dir_count;
526 dst->found_size += src->found_size;
527 if (src->extent_start != (u64)-1) {
528 if (dst->extent_start == (u64)-1) {
529 dst->extent_start = src->extent_start;
530 dst->extent_end = src->extent_end;
532 if (dst->extent_end > src->extent_start)
533 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
534 else if (dst->extent_end < src->extent_start &&
535 dst->extent_end < dst->first_extent_gap)
536 dst->first_extent_gap = dst->extent_end;
537 if (dst->extent_end < src->extent_end)
538 dst->extent_end = src->extent_end;
542 dst->errors |= src->errors;
543 if (src->found_inode_item) {
544 if (!dst->found_inode_item) {
545 dst->nlink = src->nlink;
546 dst->isize = src->isize;
547 dst->nbytes = src->nbytes;
548 dst->imode = src->imode;
549 dst->nodatasum = src->nodatasum;
550 dst->found_inode_item = 1;
552 dst->errors |= I_ERR_DUP_INODE_ITEM;
560 static int splice_shared_node(struct shared_node *src_node,
561 struct shared_node *dst_node)
563 struct cache_extent *cache;
564 struct ptr_node *node, *ins;
565 struct cache_tree *src, *dst;
566 struct inode_record *rec, *conflict;
571 if (--src_node->refs == 0)
573 if (src_node->current)
574 current_ino = src_node->current->ino;
576 src = &src_node->root_cache;
577 dst = &dst_node->root_cache;
579 cache = find_first_cache_extent(src, 0);
581 node = container_of(cache, struct ptr_node, cache);
583 cache = next_cache_extent(cache);
586 remove_cache_extent(src, &node->cache);
589 ins = malloc(sizeof(*ins));
590 ins->cache.start = node->cache.start;
591 ins->cache.size = node->cache.size;
595 ret = insert_existing_cache_extent(dst, &ins->cache);
596 if (ret == -EEXIST) {
597 conflict = get_inode_rec(dst, rec->ino, 1);
598 merge_inode_recs(rec, conflict, dst);
600 conflict->checked = 1;
601 if (dst_node->current == conflict)
602 dst_node->current = NULL;
604 maybe_free_inode_rec(dst, conflict);
612 if (src == &src_node->root_cache) {
613 src = &src_node->inode_cache;
614 dst = &dst_node->inode_cache;
618 if (current_ino > 0 && (!dst_node->current ||
619 current_ino > dst_node->current->ino)) {
620 if (dst_node->current) {
621 dst_node->current->checked = 1;
622 maybe_free_inode_rec(dst, dst_node->current);
624 dst_node->current = get_inode_rec(dst, current_ino, 1);
629 static void free_inode_recs(struct cache_tree *inode_cache)
631 struct cache_extent *cache;
632 struct ptr_node *node;
633 struct inode_record *rec;
636 cache = find_first_cache_extent(inode_cache, 0);
639 node = container_of(cache, struct ptr_node, cache);
641 remove_cache_extent(inode_cache, &node->cache);
647 static struct shared_node *find_shared_node(struct cache_tree *shared,
650 struct cache_extent *cache;
651 struct shared_node *node;
653 cache = find_cache_extent(shared, bytenr, 1);
655 node = container_of(cache, struct shared_node, cache);
661 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
664 struct shared_node *node;
666 node = calloc(1, sizeof(*node));
667 node->cache.start = bytenr;
668 node->cache.size = 1;
669 cache_tree_init(&node->root_cache);
670 cache_tree_init(&node->inode_cache);
673 ret = insert_existing_cache_extent(shared, &node->cache);
678 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
679 struct walk_control *wc, int level)
681 struct shared_node *node;
682 struct shared_node *dest;
684 if (level == wc->active_node)
687 BUG_ON(wc->active_node <= level);
688 node = find_shared_node(&wc->shared, bytenr);
690 add_shared_node(&wc->shared, bytenr, refs);
691 node = find_shared_node(&wc->shared, bytenr);
692 wc->nodes[level] = node;
693 wc->active_node = level;
697 if (wc->root_level == wc->active_node &&
698 btrfs_root_refs(&root->root_item) == 0) {
699 if (--node->refs == 0) {
700 free_inode_recs(&node->root_cache);
701 free_inode_recs(&node->inode_cache);
702 remove_cache_extent(&wc->shared, &node->cache);
708 dest = wc->nodes[wc->active_node];
709 splice_shared_node(node, dest);
710 if (node->refs == 0) {
711 remove_cache_extent(&wc->shared, &node->cache);
717 static int leave_shared_node(struct btrfs_root *root,
718 struct walk_control *wc, int level)
720 struct shared_node *node;
721 struct shared_node *dest;
724 if (level == wc->root_level)
727 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
731 BUG_ON(i >= BTRFS_MAX_LEVEL);
733 node = wc->nodes[wc->active_node];
734 wc->nodes[wc->active_node] = NULL;
737 dest = wc->nodes[wc->active_node];
738 if (wc->active_node < wc->root_level ||
739 btrfs_root_refs(&root->root_item) > 0) {
740 BUG_ON(node->refs <= 1);
741 splice_shared_node(node, dest);
743 BUG_ON(node->refs < 2);
749 static int process_dir_item(struct extent_buffer *eb,
750 int slot, struct btrfs_key *key,
751 struct shared_node *active_node)
761 struct btrfs_dir_item *di;
762 struct inode_record *rec;
763 struct cache_tree *root_cache;
764 struct cache_tree *inode_cache;
765 struct btrfs_key location;
766 char namebuf[BTRFS_NAME_LEN];
768 root_cache = &active_node->root_cache;
769 inode_cache = &active_node->inode_cache;
770 rec = active_node->current;
771 rec->found_dir_item = 1;
773 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
774 total = btrfs_item_size_nr(eb, slot);
775 while (cur < total) {
777 btrfs_dir_item_key_to_cpu(eb, di, &location);
778 name_len = btrfs_dir_name_len(eb, di);
779 data_len = btrfs_dir_data_len(eb, di);
780 filetype = btrfs_dir_type(eb, di);
782 rec->found_size += name_len;
783 if (name_len <= BTRFS_NAME_LEN) {
787 len = BTRFS_NAME_LEN;
788 error = REF_ERR_NAME_TOO_LONG;
790 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
792 if (location.type == BTRFS_INODE_ITEM_KEY) {
793 add_inode_backref(inode_cache, location.objectid,
794 key->objectid, key->offset, namebuf,
795 len, filetype, key->type, error);
796 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
797 add_inode_backref(root_cache, location.objectid,
798 key->objectid, key->offset, namebuf,
799 len, filetype, key->type, error);
801 fprintf(stderr, "warning line %d\n", __LINE__);
804 len = sizeof(*di) + name_len + data_len;
805 di = (struct btrfs_dir_item *)((char *)di + len);
808 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
809 rec->errors |= I_ERR_DUP_DIR_INDEX;
814 static int process_inode_ref(struct extent_buffer *eb,
815 int slot, struct btrfs_key *key,
816 struct shared_node *active_node)
824 struct cache_tree *inode_cache;
825 struct btrfs_inode_ref *ref;
826 char namebuf[BTRFS_NAME_LEN];
828 inode_cache = &active_node->inode_cache;
830 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
831 total = btrfs_item_size_nr(eb, slot);
832 while (cur < total) {
833 name_len = btrfs_inode_ref_name_len(eb, ref);
834 index = btrfs_inode_ref_index(eb, ref);
835 if (name_len <= BTRFS_NAME_LEN) {
839 len = BTRFS_NAME_LEN;
840 error = REF_ERR_NAME_TOO_LONG;
842 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
843 add_inode_backref(inode_cache, key->objectid, key->offset,
844 index, namebuf, len, 0, key->type, error);
846 len = sizeof(*ref) + name_len;
847 ref = (struct btrfs_inode_ref *)((char *)ref + len);
853 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
855 struct btrfs_key key;
856 struct btrfs_path path;
857 struct extent_buffer *leaf;
862 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
864 btrfs_init_path(&path);
866 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
868 key.type = BTRFS_EXTENT_CSUM_KEY;
870 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
873 if (ret > 0 && path.slots[0] > 0) {
874 leaf = path.nodes[0];
875 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
876 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
877 key.type == BTRFS_EXTENT_CSUM_KEY)
882 leaf = path.nodes[0];
883 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
884 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
888 leaf = path.nodes[0];
891 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
892 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
893 key.type != BTRFS_EXTENT_CSUM_KEY)
896 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
897 if (key.offset >= start + len)
900 if (key.offset > start)
903 size = btrfs_item_size_nr(leaf, path.slots[0]);
904 csum_end = key.offset + (size / csum_size) * root->sectorsize;
905 if (csum_end > start) {
906 size = min(csum_end - start, len);
914 btrfs_release_path(root->fs_info->csum_root, &path);
918 static int process_file_extent(struct btrfs_root *root,
919 struct extent_buffer *eb,
920 int slot, struct btrfs_key *key,
921 struct shared_node *active_node)
923 struct inode_record *rec;
924 struct btrfs_file_extent_item *fi;
927 u64 extent_offset = 0;
928 u64 mask = root->sectorsize - 1;
931 rec = active_node->current;
932 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
933 rec->found_file_extent = 1;
935 if (rec->extent_start == (u64)-1) {
936 rec->extent_start = key->offset;
937 rec->extent_end = key->offset;
940 if (rec->extent_end > key->offset)
941 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
942 else if (rec->extent_end < key->offset &&
943 rec->extent_end < rec->first_extent_gap)
944 rec->first_extent_gap = rec->extent_end;
946 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
947 extent_type = btrfs_file_extent_type(eb, fi);
949 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
950 num_bytes = btrfs_file_extent_inline_len(eb, fi);
952 rec->errors |= I_ERR_BAD_FILE_EXTENT;
953 rec->found_size += num_bytes;
954 num_bytes = (num_bytes + mask) & ~mask;
955 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
956 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
957 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
958 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
959 extent_offset = btrfs_file_extent_offset(eb, fi);
960 if (num_bytes == 0 || (num_bytes & mask))
961 rec->errors |= I_ERR_BAD_FILE_EXTENT;
962 if (num_bytes + extent_offset >
963 btrfs_file_extent_ram_bytes(eb, fi))
964 rec->errors |= I_ERR_BAD_FILE_EXTENT;
965 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
966 (btrfs_file_extent_compression(eb, fi) ||
967 btrfs_file_extent_encryption(eb, fi) ||
968 btrfs_file_extent_other_encoding(eb, fi)))
969 rec->errors |= I_ERR_BAD_FILE_EXTENT;
971 rec->found_size += num_bytes;
973 rec->errors |= I_ERR_BAD_FILE_EXTENT;
975 rec->extent_end = key->offset + num_bytes;
977 if (disk_bytenr > 0) {
979 if (btrfs_file_extent_compression(eb, fi))
980 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
982 disk_bytenr += extent_offset;
984 found = count_csum_range(root, disk_bytenr, num_bytes);
985 if (extent_type == BTRFS_FILE_EXTENT_REG) {
987 rec->found_csum_item = 1;
988 if (found < num_bytes)
989 rec->some_csum_missing = 1;
990 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
992 rec->errors |= I_ERR_ODD_CSUM_ITEM;
998 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
999 struct walk_control *wc)
1001 struct btrfs_key key;
1005 struct cache_tree *inode_cache;
1006 struct shared_node *active_node;
1008 if (wc->root_level == wc->active_node &&
1009 btrfs_root_refs(&root->root_item) == 0)
1012 active_node = wc->nodes[wc->active_node];
1013 inode_cache = &active_node->inode_cache;
1014 nritems = btrfs_header_nritems(eb);
1015 for (i = 0; i < nritems; i++) {
1016 btrfs_item_key_to_cpu(eb, &key, i);
1017 if (active_node->current == NULL ||
1018 active_node->current->ino < key.objectid) {
1019 if (active_node->current) {
1020 active_node->current->checked = 1;
1021 maybe_free_inode_rec(inode_cache,
1022 active_node->current);
1024 active_node->current = get_inode_rec(inode_cache,
1028 case BTRFS_DIR_ITEM_KEY:
1029 case BTRFS_DIR_INDEX_KEY:
1030 ret = process_dir_item(eb, i, &key, active_node);
1032 case BTRFS_INODE_REF_KEY:
1033 ret = process_inode_ref(eb, i, &key, active_node);
1035 case BTRFS_INODE_ITEM_KEY:
1036 ret = process_inode_item(eb, i, &key, active_node);
1038 case BTRFS_EXTENT_DATA_KEY:
1039 ret = process_file_extent(root, eb, i, &key,
1049 static void reada_walk_down(struct btrfs_root *root,
1050 struct extent_buffer *node, int slot)
1060 level = btrfs_header_level(node);
1064 nritems = btrfs_header_nritems(node);
1065 blocksize = btrfs_level_size(root, level - 1);
1066 for (i = slot; i < nritems; i++) {
1067 bytenr = btrfs_node_blockptr(node, i);
1068 ptr_gen = btrfs_node_ptr_generation(node, i);
1069 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1075 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1076 struct walk_control *wc, int *level)
1080 struct extent_buffer *next;
1081 struct extent_buffer *cur;
1086 WARN_ON(*level < 0);
1087 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1088 ret = btrfs_lookup_extent_info(NULL, root,
1089 path->nodes[*level]->start,
1090 path->nodes[*level]->len, &refs, NULL);
1095 ret = enter_shared_node(root, path->nodes[*level]->start,
1101 while (*level >= 0) {
1102 WARN_ON(*level < 0);
1103 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1104 cur = path->nodes[*level];
1106 if (btrfs_header_level(cur) != *level)
1109 if (path->slots[*level] >= btrfs_header_nritems(cur))
1112 ret = process_one_leaf(root, cur, wc);
1115 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1116 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1117 blocksize = btrfs_level_size(root, *level - 1);
1118 ret = btrfs_lookup_extent_info(NULL, root, bytenr, blocksize,
1124 ret = enter_shared_node(root, bytenr, refs,
1127 path->slots[*level]++;
1132 next = btrfs_find_tree_block(root, bytenr, blocksize);
1133 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1134 free_extent_buffer(next);
1135 reada_walk_down(root, cur, path->slots[*level]);
1136 next = read_tree_block(root, bytenr, blocksize,
1140 *level = *level - 1;
1141 free_extent_buffer(path->nodes[*level]);
1142 path->nodes[*level] = next;
1143 path->slots[*level] = 0;
1146 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1150 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1151 struct walk_control *wc, int *level)
1154 struct extent_buffer *leaf;
1156 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1157 leaf = path->nodes[i];
1158 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1163 free_extent_buffer(path->nodes[*level]);
1164 path->nodes[*level] = NULL;
1165 BUG_ON(*level > wc->active_node);
1166 if (*level == wc->active_node)
1167 leave_shared_node(root, wc, *level);
1174 static int check_root_dir(struct inode_record *rec)
1176 struct inode_backref *backref;
1179 if (!rec->found_inode_item || rec->errors)
1181 if (rec->nlink != 1 || rec->found_link != 0)
1183 if (list_empty(&rec->backrefs))
1185 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1186 if (!backref->found_inode_ref)
1188 if (backref->index != 0 || backref->namelen != 2 ||
1189 memcmp(backref->name, "..", 2))
1191 if (backref->found_dir_index || backref->found_dir_item)
1198 static int check_inode_recs(struct btrfs_root *root,
1199 struct cache_tree *inode_cache)
1201 struct cache_extent *cache;
1202 struct ptr_node *node;
1203 struct inode_record *rec;
1204 struct inode_backref *backref;
1207 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1209 if (btrfs_root_refs(&root->root_item) == 0) {
1210 if (!cache_tree_empty(inode_cache))
1211 fprintf(stderr, "warning line %d\n", __LINE__);
1215 rec = get_inode_rec(inode_cache, root_dirid, 0);
1217 ret = check_root_dir(rec);
1219 fprintf(stderr, "root %llu root dir %llu error\n",
1220 (unsigned long long)root->root_key.objectid,
1221 (unsigned long long)root_dirid);
1225 fprintf(stderr, "root %llu root dir %llu not found\n",
1226 (unsigned long long)root->root_key.objectid,
1227 (unsigned long long)root_dirid);
1231 cache = find_first_cache_extent(inode_cache, 0);
1234 node = container_of(cache, struct ptr_node, cache);
1236 remove_cache_extent(inode_cache, &node->cache);
1238 if (rec->ino == root_dirid ||
1239 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1240 free_inode_rec(rec);
1244 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1245 ret = check_orphan_item(root, rec->ino);
1247 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1248 if (can_free_inode_rec(rec)) {
1249 free_inode_rec(rec);
1255 if (!rec->found_inode_item)
1256 rec->errors |= I_ERR_NO_INODE_ITEM;
1257 if (rec->found_link != rec->nlink)
1258 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1259 fprintf(stderr, "root %llu inode %llu errors %x\n",
1260 (unsigned long long) root->root_key.objectid,
1261 (unsigned long long) rec->ino, rec->errors);
1262 list_for_each_entry(backref, &rec->backrefs, list) {
1263 if (!backref->found_dir_item)
1264 backref->errors |= REF_ERR_NO_DIR_ITEM;
1265 if (!backref->found_dir_index)
1266 backref->errors |= REF_ERR_NO_DIR_INDEX;
1267 if (!backref->found_inode_ref)
1268 backref->errors |= REF_ERR_NO_INODE_REF;
1269 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1270 " namelen %u name %s filetype %d error %x\n",
1271 (unsigned long long)backref->dir,
1272 (unsigned long long)backref->index,
1273 backref->namelen, backref->name,
1274 backref->filetype, backref->errors);
1276 free_inode_rec(rec);
1278 return (error > 0) ? -1 : 0;
1281 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1284 struct cache_extent *cache;
1285 struct root_record *rec = NULL;
1288 cache = find_cache_extent(root_cache, objectid, 1);
1290 rec = container_of(cache, struct root_record, cache);
1292 rec = calloc(1, sizeof(*rec));
1293 rec->objectid = objectid;
1294 INIT_LIST_HEAD(&rec->backrefs);
1295 rec->cache.start = objectid;
1296 rec->cache.size = 1;
1298 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1304 static struct root_backref *get_root_backref(struct root_record *rec,
1305 u64 ref_root, u64 dir, u64 index,
1306 const char *name, int namelen)
1308 struct root_backref *backref;
1310 list_for_each_entry(backref, &rec->backrefs, list) {
1311 if (backref->ref_root != ref_root || backref->dir != dir ||
1312 backref->namelen != namelen)
1314 if (memcmp(name, backref->name, namelen))
1319 backref = malloc(sizeof(*backref) + namelen + 1);
1320 memset(backref, 0, sizeof(*backref));
1321 backref->ref_root = ref_root;
1323 backref->index = index;
1324 backref->namelen = namelen;
1325 memcpy(backref->name, name, namelen);
1326 backref->name[namelen] = '\0';
1327 list_add_tail(&backref->list, &rec->backrefs);
1331 static void free_root_recs(struct cache_tree *root_cache)
1333 struct cache_extent *cache;
1334 struct root_record *rec;
1335 struct root_backref *backref;
1338 cache = find_first_cache_extent(root_cache, 0);
1341 rec = container_of(cache, struct root_record, cache);
1342 remove_cache_extent(root_cache, &rec->cache);
1344 while (!list_empty(&rec->backrefs)) {
1345 backref = list_entry(rec->backrefs.next,
1346 struct root_backref, list);
1347 list_del(&backref->list);
1354 static int add_root_backref(struct cache_tree *root_cache,
1355 u64 root_id, u64 ref_root, u64 dir, u64 index,
1356 const char *name, int namelen,
1357 int item_type, int errors)
1359 struct root_record *rec;
1360 struct root_backref *backref;
1362 rec = get_root_rec(root_cache, root_id);
1363 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1365 backref->errors |= errors;
1367 if (item_type != BTRFS_DIR_ITEM_KEY) {
1368 if (backref->found_dir_index || backref->found_back_ref ||
1369 backref->found_forward_ref) {
1370 if (backref->index != index)
1371 backref->errors |= REF_ERR_INDEX_UNMATCH;
1373 backref->index = index;
1377 if (item_type == BTRFS_DIR_ITEM_KEY) {
1378 backref->found_dir_item = 1;
1379 backref->reachable = 1;
1381 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1382 backref->found_dir_index = 1;
1383 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1384 if (backref->found_forward_ref)
1385 backref->errors |= REF_ERR_DUP_ROOT_REF;
1386 backref->found_forward_ref = 1;
1387 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1388 if (backref->found_back_ref)
1389 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1390 backref->found_back_ref = 1;
1398 static int merge_root_recs(struct btrfs_root *root,
1399 struct cache_tree *src_cache,
1400 struct cache_tree *dst_cache)
1402 struct cache_extent *cache;
1403 struct ptr_node *node;
1404 struct inode_record *rec;
1405 struct inode_backref *backref;
1407 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1408 free_inode_recs(src_cache);
1413 cache = find_first_cache_extent(src_cache, 0);
1416 node = container_of(cache, struct ptr_node, cache);
1418 remove_cache_extent(src_cache, &node->cache);
1421 list_for_each_entry(backref, &rec->backrefs, list) {
1422 BUG_ON(backref->found_inode_ref);
1423 if (backref->found_dir_item)
1424 add_root_backref(dst_cache, rec->ino,
1425 root->root_key.objectid, backref->dir,
1426 backref->index, backref->name,
1427 backref->namelen, BTRFS_DIR_ITEM_KEY,
1429 if (backref->found_dir_index)
1430 add_root_backref(dst_cache, rec->ino,
1431 root->root_key.objectid, backref->dir,
1432 backref->index, backref->name,
1433 backref->namelen, BTRFS_DIR_INDEX_KEY,
1436 free_inode_rec(rec);
1441 static int check_root_refs(struct btrfs_root *root,
1442 struct cache_tree *root_cache)
1444 struct root_record *rec;
1445 struct root_record *ref_root;
1446 struct root_backref *backref;
1447 struct cache_extent *cache;
1453 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1456 /* fixme: this can not detect circular references */
1459 cache = find_first_cache_extent(root_cache, 0);
1463 rec = container_of(cache, struct root_record, cache);
1464 cache = next_cache_extent(cache);
1466 if (rec->found_ref == 0)
1469 list_for_each_entry(backref, &rec->backrefs, list) {
1470 if (!backref->reachable)
1473 ref_root = get_root_rec(root_cache,
1475 if (ref_root->found_ref > 0)
1478 backref->reachable = 0;
1480 if (rec->found_ref == 0)
1486 cache = find_first_cache_extent(root_cache, 0);
1490 rec = container_of(cache, struct root_record, cache);
1491 cache = next_cache_extent(cache);
1493 if (rec->found_ref == 0 &&
1494 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1495 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1496 ret = check_orphan_item(root->fs_info->tree_root,
1501 fprintf(stderr, "fs tree %llu not referenced\n",
1502 (unsigned long long)rec->objectid);
1506 if (rec->found_ref > 0 && !rec->found_root_item)
1508 list_for_each_entry(backref, &rec->backrefs, list) {
1509 if (!backref->found_dir_item)
1510 backref->errors |= REF_ERR_NO_DIR_ITEM;
1511 if (!backref->found_dir_index)
1512 backref->errors |= REF_ERR_NO_DIR_INDEX;
1513 if (!backref->found_back_ref)
1514 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1515 if (!backref->found_forward_ref)
1516 backref->errors |= REF_ERR_NO_ROOT_REF;
1517 if (backref->reachable && backref->errors)
1524 fprintf(stderr, "fs tree %llu refs %u %s\n",
1525 (unsigned long long)rec->objectid, rec->found_ref,
1526 rec->found_root_item ? "" : "not found");
1528 list_for_each_entry(backref, &rec->backrefs, list) {
1529 if (!backref->reachable)
1531 if (!backref->errors && rec->found_root_item)
1533 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1534 " index %llu namelen %u name %s error %x\n",
1535 (unsigned long long)backref->ref_root,
1536 (unsigned long long)backref->dir,
1537 (unsigned long long)backref->index,
1538 backref->namelen, backref->name,
1542 return errors > 0 ? 1 : 0;
1545 static int process_root_ref(struct extent_buffer *eb, int slot,
1546 struct btrfs_key *key,
1547 struct cache_tree *root_cache)
1553 struct btrfs_root_ref *ref;
1554 char namebuf[BTRFS_NAME_LEN];
1557 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1559 dirid = btrfs_root_ref_dirid(eb, ref);
1560 index = btrfs_root_ref_sequence(eb, ref);
1561 name_len = btrfs_root_ref_name_len(eb, ref);
1563 if (name_len <= BTRFS_NAME_LEN) {
1567 len = BTRFS_NAME_LEN;
1568 error = REF_ERR_NAME_TOO_LONG;
1570 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1572 if (key->type == BTRFS_ROOT_REF_KEY) {
1573 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1574 index, namebuf, len, key->type, error);
1576 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1577 index, namebuf, len, key->type, error);
1582 static int check_fs_root(struct btrfs_root *root,
1583 struct cache_tree *root_cache,
1584 struct walk_control *wc)
1589 struct btrfs_path path;
1590 struct shared_node root_node;
1591 struct root_record *rec;
1592 struct btrfs_root_item *root_item = &root->root_item;
1594 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1595 rec = get_root_rec(root_cache, root->root_key.objectid);
1596 if (btrfs_root_refs(root_item) > 0)
1597 rec->found_root_item = 1;
1600 btrfs_init_path(&path);
1601 memset(&root_node, 0, sizeof(root_node));
1602 cache_tree_init(&root_node.root_cache);
1603 cache_tree_init(&root_node.inode_cache);
1605 level = btrfs_header_level(root->node);
1606 memset(wc->nodes, 0, sizeof(wc->nodes));
1607 wc->nodes[level] = &root_node;
1608 wc->active_node = level;
1609 wc->root_level = level;
1611 if (btrfs_root_refs(root_item) > 0 ||
1612 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1613 path.nodes[level] = root->node;
1614 extent_buffer_get(root->node);
1615 path.slots[level] = 0;
1617 struct btrfs_key key;
1618 struct btrfs_disk_key found_key;
1620 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1621 level = root_item->drop_level;
1622 path.lowest_level = level;
1623 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1625 btrfs_node_key(path.nodes[level], &found_key,
1627 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1628 sizeof(found_key)));
1632 wret = walk_down_tree(root, &path, wc, &level);
1638 wret = walk_up_tree(root, &path, wc, &level);
1644 btrfs_release_path(root, &path);
1646 merge_root_recs(root, &root_node.root_cache, root_cache);
1648 if (root_node.current) {
1649 root_node.current->checked = 1;
1650 maybe_free_inode_rec(&root_node.inode_cache,
1654 ret = check_inode_recs(root, &root_node.inode_cache);
1658 static int fs_root_objectid(u64 objectid)
1660 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1661 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1662 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1663 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1664 objectid <= BTRFS_LAST_FREE_OBJECTID))
1669 static int check_fs_roots(struct btrfs_root *root,
1670 struct cache_tree *root_cache)
1672 struct btrfs_path path;
1673 struct btrfs_key key;
1674 struct walk_control wc;
1675 struct extent_buffer *leaf;
1676 struct btrfs_root *tmp_root;
1677 struct btrfs_root *tree_root = root->fs_info->tree_root;
1681 memset(&wc, 0, sizeof(wc));
1682 cache_tree_init(&wc.shared);
1683 btrfs_init_path(&path);
1687 key.type = BTRFS_ROOT_ITEM_KEY;
1688 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1691 leaf = path.nodes[0];
1692 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1693 ret = btrfs_next_leaf(tree_root, &path);
1696 leaf = path.nodes[0];
1698 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1699 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1700 fs_root_objectid(key.objectid)) {
1701 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1703 ret = check_fs_root(tmp_root, root_cache, &wc);
1706 btrfs_free_fs_root(root->fs_info, tmp_root);
1707 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1708 key.type == BTRFS_ROOT_BACKREF_KEY) {
1709 process_root_ref(leaf, path.slots[0], &key,
1714 btrfs_release_path(tree_root, &path);
1716 if (!cache_tree_empty(&wc.shared))
1717 fprintf(stderr, "warning line %d\n", __LINE__);
1722 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1724 struct list_head *cur = rec->backrefs.next;
1725 struct extent_backref *back;
1726 struct tree_backref *tback;
1727 struct data_backref *dback;
1731 while(cur != &rec->backrefs) {
1732 back = list_entry(cur, struct extent_backref, list);
1734 if (!back->found_extent_tree) {
1738 if (back->is_data) {
1739 dback = (struct data_backref *)back;
1740 fprintf(stderr, "Backref %llu %s %llu"
1741 " owner %llu offset %llu num_refs %lu"
1742 " not found in extent tree\n",
1743 (unsigned long long)rec->start,
1744 back->full_backref ?
1746 back->full_backref ?
1747 (unsigned long long)dback->parent:
1748 (unsigned long long)dback->root,
1749 (unsigned long long)dback->owner,
1750 (unsigned long long)dback->offset,
1751 (unsigned long)dback->num_refs);
1753 tback = (struct tree_backref *)back;
1754 fprintf(stderr, "Backref %llu parent %llu"
1755 " root %llu not found in extent tree\n",
1756 (unsigned long long)rec->start,
1757 (unsigned long long)tback->parent,
1758 (unsigned long long)tback->root);
1761 if (!back->is_data && !back->found_ref) {
1765 tback = (struct tree_backref *)back;
1766 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1767 (unsigned long long)rec->start,
1768 back->full_backref ? "parent" : "root",
1769 back->full_backref ?
1770 (unsigned long long)tback->parent :
1771 (unsigned long long)tback->root, back);
1773 if (back->is_data) {
1774 dback = (struct data_backref *)back;
1775 if (dback->found_ref != dback->num_refs) {
1779 fprintf(stderr, "Incorrect local backref count"
1780 " on %llu %s %llu owner %llu"
1781 " offset %llu found %u wanted %u back %p\n",
1782 (unsigned long long)rec->start,
1783 back->full_backref ?
1785 back->full_backref ?
1786 (unsigned long long)dback->parent:
1787 (unsigned long long)dback->root,
1788 (unsigned long long)dback->owner,
1789 (unsigned long long)dback->offset,
1790 dback->found_ref, dback->num_refs, back);
1793 if (!back->is_data) {
1796 dback = (struct data_backref *)back;
1797 found += dback->found_ref;
1800 if (found != rec->refs) {
1804 fprintf(stderr, "Incorrect global backref count "
1805 "on %llu found %llu wanted %llu\n",
1806 (unsigned long long)rec->start,
1807 (unsigned long long)found,
1808 (unsigned long long)rec->refs);
1814 static int free_all_extent_backrefs(struct extent_record *rec)
1816 struct extent_backref *back;
1817 struct list_head *cur;
1818 while (!list_empty(&rec->backrefs)) {
1819 cur = rec->backrefs.next;
1820 back = list_entry(cur, struct extent_backref, list);
1827 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
1828 struct extent_record *rec)
1830 if (rec->content_checked && rec->owner_ref_checked &&
1831 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
1832 !all_backpointers_checked(rec, 0)) {
1833 remove_cache_extent(extent_cache, &rec->cache);
1834 free_all_extent_backrefs(rec);
1840 static int check_owner_ref(struct btrfs_root *root,
1841 struct extent_record *rec,
1842 struct extent_buffer *buf)
1844 struct extent_backref *node;
1845 struct tree_backref *back;
1846 struct btrfs_root *ref_root;
1847 struct btrfs_key key;
1848 struct btrfs_path path;
1852 list_for_each_entry(node, &rec->backrefs, list) {
1855 if (!node->found_ref)
1857 if (node->full_backref)
1859 back = (struct tree_backref *)node;
1860 if (btrfs_header_owner(buf) == back->root)
1863 BUG_ON(rec->is_root);
1865 /* try to find the block by search corresponding fs tree */
1866 key.objectid = btrfs_header_owner(buf);
1867 key.type = BTRFS_ROOT_ITEM_KEY;
1868 key.offset = (u64)-1;
1870 ref_root = btrfs_read_fs_root(root->fs_info, &key);
1871 BUG_ON(IS_ERR(ref_root));
1873 level = btrfs_header_level(buf);
1875 btrfs_item_key_to_cpu(buf, &key, 0);
1877 btrfs_node_key_to_cpu(buf, &key, 0);
1879 btrfs_init_path(&path);
1880 path.lowest_level = level + 1;
1881 btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
1883 if (buf->start == btrfs_node_blockptr(path.nodes[level + 1],
1884 path.slots[level + 1]))
1885 rec->owner_ref_checked = 1;
1887 btrfs_release_path(ref_root, &path);
1888 return found ? 0 : 1;
1891 static int is_extent_tree_record(struct extent_record *rec)
1893 struct list_head *cur = rec->backrefs.next;
1894 struct extent_backref *node;
1895 struct tree_backref *back;
1898 while(cur != &rec->backrefs) {
1899 node = list_entry(cur, struct extent_backref, list);
1903 back = (struct tree_backref *)node;
1904 if (node->full_backref)
1906 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
1913 static int record_bad_block_io(struct btrfs_fs_info *info,
1914 struct cache_tree *extent_cache,
1917 struct extent_record *rec;
1918 struct cache_extent *cache;
1919 struct btrfs_key key;
1921 cache = find_cache_extent(extent_cache, start, len);
1925 rec = container_of(cache, struct extent_record, cache);
1926 if (!is_extent_tree_record(rec))
1929 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
1930 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
1933 static int check_block(struct btrfs_root *root,
1934 struct cache_tree *extent_cache,
1935 struct extent_buffer *buf, u64 flags)
1937 struct extent_record *rec;
1938 struct cache_extent *cache;
1939 struct btrfs_key key;
1943 cache = find_cache_extent(extent_cache, buf->start, buf->len);
1946 rec = container_of(cache, struct extent_record, cache);
1947 rec->generation = btrfs_header_generation(buf);
1949 level = btrfs_header_level(buf);
1950 if (btrfs_header_nritems(buf) > 0) {
1953 btrfs_item_key_to_cpu(buf, &key, 0);
1955 btrfs_node_key_to_cpu(buf, &key, 0);
1957 rec->info_objectid = key.objectid;
1959 rec->info_level = level;
1961 if (btrfs_is_leaf(buf))
1962 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
1964 ret = btrfs_check_node(root, &rec->parent_key, buf);
1967 fprintf(stderr, "bad block %llu\n",
1968 (unsigned long long)buf->start);
1970 rec->content_checked = 1;
1971 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
1972 rec->owner_ref_checked = 1;
1974 ret = check_owner_ref(root, rec, buf);
1976 rec->owner_ref_checked = 1;
1980 maybe_free_extent_rec(extent_cache, rec);
1984 static struct tree_backref *find_tree_backref(struct extent_record *rec,
1985 u64 parent, u64 root)
1987 struct list_head *cur = rec->backrefs.next;
1988 struct extent_backref *node;
1989 struct tree_backref *back;
1991 while(cur != &rec->backrefs) {
1992 node = list_entry(cur, struct extent_backref, list);
1996 back = (struct tree_backref *)node;
1998 if (!node->full_backref)
2000 if (parent == back->parent)
2003 if (node->full_backref)
2005 if (back->root == root)
2012 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2013 u64 parent, u64 root)
2015 struct tree_backref *ref = malloc(sizeof(*ref));
2016 memset(&ref->node, 0, sizeof(ref->node));
2018 ref->parent = parent;
2019 ref->node.full_backref = 1;
2022 ref->node.full_backref = 0;
2024 list_add_tail(&ref->node.list, &rec->backrefs);
2029 static struct data_backref *find_data_backref(struct extent_record *rec,
2030 u64 parent, u64 root,
2031 u64 owner, u64 offset)
2033 struct list_head *cur = rec->backrefs.next;
2034 struct extent_backref *node;
2035 struct data_backref *back;
2037 while(cur != &rec->backrefs) {
2038 node = list_entry(cur, struct extent_backref, list);
2042 back = (struct data_backref *)node;
2044 if (!node->full_backref)
2046 if (parent == back->parent)
2049 if (node->full_backref)
2051 if (back->root == root && back->owner == owner &&
2052 back->offset == offset)
2059 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2060 u64 parent, u64 root,
2061 u64 owner, u64 offset,
2064 struct data_backref *ref = malloc(sizeof(*ref));
2065 memset(&ref->node, 0, sizeof(ref->node));
2066 ref->node.is_data = 1;
2069 ref->parent = parent;
2072 ref->node.full_backref = 1;
2076 ref->offset = offset;
2077 ref->node.full_backref = 0;
2081 list_add_tail(&ref->node.list, &rec->backrefs);
2082 if (max_size > rec->max_size)
2083 rec->max_size = max_size;
2087 static int add_extent_rec(struct cache_tree *extent_cache,
2088 struct btrfs_key *parent_key,
2089 u64 start, u64 nr, u64 extent_item_refs,
2090 int is_root, int inc_ref, int set_checked,
2093 struct extent_record *rec;
2094 struct cache_extent *cache;
2097 cache = find_cache_extent(extent_cache, start, nr);
2099 rec = container_of(cache, struct extent_record, cache);
2103 rec->nr = max(nr, max_size);
2105 if (start != rec->start) {
2106 fprintf(stderr, "warning, start mismatch %llu %llu\n",
2107 (unsigned long long)rec->start,
2108 (unsigned long long)start);
2111 if (extent_item_refs) {
2112 if (rec->extent_item_refs) {
2113 fprintf(stderr, "block %llu rec "
2114 "extent_item_refs %llu, passed %llu\n",
2115 (unsigned long long)start,
2116 (unsigned long long)
2117 rec->extent_item_refs,
2118 (unsigned long long)extent_item_refs);
2120 rec->extent_item_refs = extent_item_refs;
2125 rec->content_checked = 1;
2126 rec->owner_ref_checked = 1;
2130 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2132 if (rec->max_size < max_size)
2133 rec->max_size = max_size;
2135 maybe_free_extent_rec(extent_cache, rec);
2138 rec = malloc(sizeof(*rec));
2140 rec->max_size = max_size;
2141 rec->nr = max(nr, max_size);
2142 rec->content_checked = 0;
2143 rec->owner_ref_checked = 0;
2144 INIT_LIST_HEAD(&rec->backrefs);
2156 if (extent_item_refs)
2157 rec->extent_item_refs = extent_item_refs;
2159 rec->extent_item_refs = 0;
2162 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2164 memset(&rec->parent_key, 0, sizeof(*parent_key));
2166 rec->cache.start = start;
2167 rec->cache.size = nr;
2168 ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2172 rec->content_checked = 1;
2173 rec->owner_ref_checked = 1;
2178 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2179 u64 parent, u64 root, int found_ref)
2181 struct extent_record *rec;
2182 struct tree_backref *back;
2183 struct cache_extent *cache;
2185 cache = find_cache_extent(extent_cache, bytenr, 1);
2187 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0, 0);
2188 cache = find_cache_extent(extent_cache, bytenr, 1);
2193 rec = container_of(cache, struct extent_record, cache);
2194 if (rec->start != bytenr) {
2198 back = find_tree_backref(rec, parent, root);
2200 back = alloc_tree_backref(rec, parent, root);
2203 if (back->node.found_ref) {
2204 fprintf(stderr, "Extent back ref already exists "
2205 "for %llu parent %llu root %llu \n",
2206 (unsigned long long)bytenr,
2207 (unsigned long long)parent,
2208 (unsigned long long)root);
2210 back->node.found_ref = 1;
2212 if (back->node.found_extent_tree) {
2213 fprintf(stderr, "Extent back ref already exists "
2214 "for %llu parent %llu root %llu \n",
2215 (unsigned long long)bytenr,
2216 (unsigned long long)parent,
2217 (unsigned long long)root);
2219 back->node.found_extent_tree = 1;
2224 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2225 u64 parent, u64 root, u64 owner, u64 offset,
2226 u32 num_refs, int found_ref, u64 max_size)
2228 struct extent_record *rec;
2229 struct data_backref *back;
2230 struct cache_extent *cache;
2232 cache = find_cache_extent(extent_cache, bytenr, 1);
2234 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2236 cache = find_cache_extent(extent_cache, bytenr, 1);
2241 rec = container_of(cache, struct extent_record, cache);
2242 if (rec->start != bytenr) {
2245 if (rec->max_size < max_size)
2246 rec->max_size = max_size;
2248 back = find_data_backref(rec, parent, root, owner, offset);
2250 back = alloc_data_backref(rec, parent, root, owner, offset,
2254 BUG_ON(num_refs != 1);
2255 back->node.found_ref = 1;
2256 back->found_ref += 1;
2258 if (back->node.found_extent_tree) {
2259 fprintf(stderr, "Extent back ref already exists "
2260 "for %llu parent %llu root %llu"
2261 "owner %llu offset %llu num_refs %lu\n",
2262 (unsigned long long)bytenr,
2263 (unsigned long long)parent,
2264 (unsigned long long)root,
2265 (unsigned long long)owner,
2266 (unsigned long long)offset,
2267 (unsigned long)num_refs);
2269 back->num_refs = num_refs;
2270 back->node.found_extent_tree = 1;
2275 static int add_pending(struct cache_tree *pending,
2276 struct cache_tree *seen, u64 bytenr, u32 size)
2279 ret = insert_cache_extent(seen, bytenr, size);
2282 insert_cache_extent(pending, bytenr, size);
2286 static int pick_next_pending(struct cache_tree *pending,
2287 struct cache_tree *reada,
2288 struct cache_tree *nodes,
2289 u64 last, struct block_info *bits, int bits_nr,
2292 unsigned long node_start = last;
2293 struct cache_extent *cache;
2296 cache = find_first_cache_extent(reada, 0);
2298 bits[0].start = cache->start;
2299 bits[1].size = cache->size;
2304 if (node_start > 32768)
2305 node_start -= 32768;
2307 cache = find_first_cache_extent(nodes, node_start);
2309 cache = find_first_cache_extent(nodes, 0);
2312 cache = find_first_cache_extent(pending, 0);
2317 bits[ret].start = cache->start;
2318 bits[ret].size = cache->size;
2319 cache = next_cache_extent(cache);
2321 } while (cache && ret < bits_nr);
2327 bits[ret].start = cache->start;
2328 bits[ret].size = cache->size;
2329 cache = next_cache_extent(cache);
2331 } while (cache && ret < bits_nr);
2333 if (bits_nr - ret > 8) {
2334 u64 lookup = bits[0].start + bits[0].size;
2335 struct cache_extent *next;
2336 next = find_first_cache_extent(pending, lookup);
2338 if (next->start - lookup > 32768)
2340 bits[ret].start = next->start;
2341 bits[ret].size = next->size;
2342 lookup = next->start + next->size;
2346 next = next_cache_extent(next);
2354 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2355 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2356 struct extent_buffer *leaf, int slot)
2358 struct btrfs_extent_ref_v0 *ref0;
2359 struct btrfs_key key;
2361 btrfs_item_key_to_cpu(leaf, &key, slot);
2362 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2363 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2364 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2366 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2367 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2373 static int process_extent_item(struct cache_tree *extent_cache,
2374 struct extent_buffer *eb, int slot)
2376 struct btrfs_extent_item *ei;
2377 struct btrfs_extent_inline_ref *iref;
2378 struct btrfs_extent_data_ref *dref;
2379 struct btrfs_shared_data_ref *sref;
2380 struct btrfs_key key;
2384 u32 item_size = btrfs_item_size_nr(eb, slot);
2388 btrfs_item_key_to_cpu(eb, &key, slot);
2390 if (item_size < sizeof(*ei)) {
2391 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2392 struct btrfs_extent_item_v0 *ei0;
2393 BUG_ON(item_size != sizeof(*ei0));
2394 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2395 refs = btrfs_extent_refs_v0(eb, ei0);
2399 return add_extent_rec(extent_cache, NULL, key.objectid,
2400 key.offset, refs, 0, 0, 0, key.offset);
2403 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2404 refs = btrfs_extent_refs(eb, ei);
2406 add_extent_rec(extent_cache, NULL, key.objectid, key.offset,
2407 refs, 0, 0, 0, key.offset);
2409 ptr = (unsigned long)(ei + 1);
2410 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2411 ptr += sizeof(struct btrfs_tree_block_info);
2413 end = (unsigned long)ei + item_size;
2415 iref = (struct btrfs_extent_inline_ref *)ptr;
2416 type = btrfs_extent_inline_ref_type(eb, iref);
2417 offset = btrfs_extent_inline_ref_offset(eb, iref);
2419 case BTRFS_TREE_BLOCK_REF_KEY:
2420 add_tree_backref(extent_cache, key.objectid,
2423 case BTRFS_SHARED_BLOCK_REF_KEY:
2424 add_tree_backref(extent_cache, key.objectid,
2427 case BTRFS_EXTENT_DATA_REF_KEY:
2428 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2429 add_data_backref(extent_cache, key.objectid, 0,
2430 btrfs_extent_data_ref_root(eb, dref),
2431 btrfs_extent_data_ref_objectid(eb,
2433 btrfs_extent_data_ref_offset(eb, dref),
2434 btrfs_extent_data_ref_count(eb, dref),
2437 case BTRFS_SHARED_DATA_REF_KEY:
2438 sref = (struct btrfs_shared_data_ref *)(iref + 1);
2439 add_data_backref(extent_cache, key.objectid, offset,
2441 btrfs_shared_data_ref_count(eb, sref),
2445 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2446 key.objectid, key.type, key.offset);
2449 ptr += btrfs_extent_inline_ref_size(type);
2456 static int run_next_block(struct btrfs_root *root,
2457 struct block_info *bits,
2460 struct cache_tree *pending,
2461 struct cache_tree *seen,
2462 struct cache_tree *reada,
2463 struct cache_tree *nodes,
2464 struct cache_tree *extent_cache)
2466 struct extent_buffer *buf;
2475 struct btrfs_key key;
2476 struct cache_extent *cache;
2479 ret = pick_next_pending(pending, reada, nodes, *last, bits,
2480 bits_nr, &reada_bits);
2485 for(i = 0; i < ret; i++) {
2486 insert_cache_extent(reada, bits[i].start,
2489 /* fixme, get the parent transid */
2490 readahead_tree_block(root, bits[i].start,
2494 *last = bits[0].start;
2495 bytenr = bits[0].start;
2496 size = bits[0].size;
2498 cache = find_cache_extent(pending, bytenr, size);
2500 remove_cache_extent(pending, cache);
2503 cache = find_cache_extent(reada, bytenr, size);
2505 remove_cache_extent(reada, cache);
2508 cache = find_cache_extent(nodes, bytenr, size);
2510 remove_cache_extent(nodes, cache);
2514 /* fixme, get the real parent transid */
2515 buf = read_tree_block(root, bytenr, size, 0);
2516 if (!extent_buffer_uptodate(buf)) {
2517 record_bad_block_io(root->fs_info,
2518 extent_cache, bytenr, size);
2519 free_extent_buffer(buf);
2523 nritems = btrfs_header_nritems(buf);
2525 ret = btrfs_lookup_extent_info(NULL, root, bytenr, size, NULL, &flags);
2527 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
2529 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
2534 owner = btrfs_header_owner(buf);
2537 ret = check_block(root, extent_cache, buf, flags);
2541 if (btrfs_is_leaf(buf)) {
2542 btree_space_waste += btrfs_leaf_free_space(root, buf);
2543 for (i = 0; i < nritems; i++) {
2544 struct btrfs_file_extent_item *fi;
2545 btrfs_item_key_to_cpu(buf, &key, i);
2546 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
2547 process_extent_item(extent_cache, buf, i);
2550 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
2552 btrfs_item_size_nr(buf, i);
2555 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2558 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
2559 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2560 process_extent_ref_v0(extent_cache, buf, i);
2567 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
2568 add_tree_backref(extent_cache, key.objectid, 0,
2572 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
2573 add_tree_backref(extent_cache, key.objectid,
2577 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
2578 struct btrfs_extent_data_ref *ref;
2579 ref = btrfs_item_ptr(buf, i,
2580 struct btrfs_extent_data_ref);
2581 add_data_backref(extent_cache,
2583 btrfs_extent_data_ref_root(buf, ref),
2584 btrfs_extent_data_ref_objectid(buf,
2586 btrfs_extent_data_ref_offset(buf, ref),
2587 btrfs_extent_data_ref_count(buf, ref),
2588 0, root->sectorsize);
2591 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
2592 struct btrfs_shared_data_ref *ref;
2593 ref = btrfs_item_ptr(buf, i,
2594 struct btrfs_shared_data_ref);
2595 add_data_backref(extent_cache,
2596 key.objectid, key.offset, 0, 0, 0,
2597 btrfs_shared_data_ref_count(buf, ref),
2598 0, root->sectorsize);
2601 if (key.type != BTRFS_EXTENT_DATA_KEY)
2603 fi = btrfs_item_ptr(buf, i,
2604 struct btrfs_file_extent_item);
2605 if (btrfs_file_extent_type(buf, fi) ==
2606 BTRFS_FILE_EXTENT_INLINE)
2608 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
2611 data_bytes_allocated +=
2612 btrfs_file_extent_disk_num_bytes(buf, fi);
2613 if (data_bytes_allocated < root->sectorsize) {
2616 data_bytes_referenced +=
2617 btrfs_file_extent_num_bytes(buf, fi);
2618 ret = add_extent_rec(extent_cache, NULL,
2619 btrfs_file_extent_disk_bytenr(buf, fi),
2620 btrfs_file_extent_disk_num_bytes(buf, fi),
2622 btrfs_file_extent_disk_num_bytes(buf, fi));
2623 add_data_backref(extent_cache,
2624 btrfs_file_extent_disk_bytenr(buf, fi),
2625 parent, owner, key.objectid, key.offset -
2626 btrfs_file_extent_offset(buf, fi), 1, 1,
2627 btrfs_file_extent_disk_num_bytes(buf, fi));
2632 struct btrfs_key first_key;
2634 first_key.objectid = 0;
2637 btrfs_item_key_to_cpu(buf, &first_key, 0);
2638 level = btrfs_header_level(buf);
2639 for (i = 0; i < nritems; i++) {
2640 u64 ptr = btrfs_node_blockptr(buf, i);
2641 u32 size = btrfs_level_size(root, level - 1);
2642 btrfs_node_key_to_cpu(buf, &key, i);
2643 ret = add_extent_rec(extent_cache, &key,
2644 ptr, size, 0, 0, 1, 0, size);
2647 add_tree_backref(extent_cache, ptr, parent, owner, 1);
2650 add_pending(nodes, seen, ptr, size);
2652 add_pending(pending, seen, ptr, size);
2655 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
2656 nritems) * sizeof(struct btrfs_key_ptr);
2658 total_btree_bytes += buf->len;
2659 if (fs_root_objectid(btrfs_header_owner(buf)))
2660 total_fs_tree_bytes += buf->len;
2661 if (!found_old_backref &&
2662 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
2663 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
2664 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
2665 found_old_backref = 1;
2667 free_extent_buffer(buf);
2671 static int add_root_to_pending(struct extent_buffer *buf,
2672 struct block_info *bits,
2674 struct cache_tree *extent_cache,
2675 struct cache_tree *pending,
2676 struct cache_tree *seen,
2677 struct cache_tree *reada,
2678 struct cache_tree *nodes,
2679 struct btrfs_key *root_key)
2681 if (btrfs_header_level(buf) > 0)
2682 add_pending(nodes, seen, buf->start, buf->len);
2684 add_pending(pending, seen, buf->start, buf->len);
2685 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
2686 0, 1, 1, 0, buf->len);
2688 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
2689 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2690 add_tree_backref(extent_cache, buf->start, buf->start,
2693 add_tree_backref(extent_cache, buf->start, 0,
2694 root_key->objectid, 1);
2698 /* as we fix the tree, we might be deleting blocks that
2699 * we're tracking for repair. This hook makes sure we
2700 * remove any backrefs for blocks as we are fixing them.
2702 static int free_extent_hook(struct btrfs_trans_handle *trans,
2703 struct btrfs_root *root,
2704 u64 bytenr, u64 num_bytes, u64 parent,
2705 u64 root_objectid, u64 owner, u64 offset,
2708 struct extent_record *rec;
2709 struct cache_extent *cache;
2711 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
2713 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
2714 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
2718 rec = container_of(cache, struct extent_record, cache);
2720 struct data_backref *back;
2721 back = find_data_backref(rec, parent, root_objectid, owner,
2725 if (back->node.found_ref) {
2726 back->found_ref -= refs_to_drop;
2728 rec->refs -= refs_to_drop;
2730 if (back->node.found_extent_tree) {
2731 back->num_refs -= refs_to_drop;
2732 if (rec->extent_item_refs)
2733 rec->extent_item_refs -= refs_to_drop;
2735 if (back->found_ref == 0)
2736 back->node.found_ref = 0;
2737 if (back->num_refs == 0)
2738 back->node.found_extent_tree = 0;
2740 if (!back->node.found_extent_tree && back->node.found_ref) {
2741 list_del(&back->node.list);
2745 struct tree_backref *back;
2746 back = find_tree_backref(rec, parent, root_objectid);
2749 if (back->node.found_ref) {
2752 back->node.found_ref = 0;
2754 if (back->node.found_extent_tree) {
2755 if (rec->extent_item_refs)
2756 rec->extent_item_refs--;
2757 back->node.found_extent_tree = 0;
2759 if (!back->node.found_extent_tree && back->node.found_ref) {
2760 list_del(&back->node.list);
2764 maybe_free_extent_rec(extent_cache, rec);
2769 static int delete_extent_records(struct btrfs_trans_handle *trans,
2770 struct btrfs_root *root,
2771 struct btrfs_path *path,
2772 u64 bytenr, u64 new_len)
2774 struct btrfs_key key;
2775 struct btrfs_key found_key;
2776 struct extent_buffer *leaf;
2781 key.objectid = bytenr;
2783 key.offset = (u64)-1;
2786 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
2793 if (path->slots[0] == 0)
2799 leaf = path->nodes[0];
2800 slot = path->slots[0];
2802 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2803 if (found_key.objectid != bytenr)
2806 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
2807 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2808 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
2809 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
2810 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
2811 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
2812 btrfs_release_path(NULL, path);
2813 if (found_key.type == 0) {
2814 if (found_key.offset == 0)
2816 key.offset = found_key.offset - 1;
2817 key.type = found_key.type;
2819 key.type = found_key.type - 1;
2820 key.offset = (u64)-1;
2824 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
2825 found_key.objectid, found_key.type, found_key.offset);
2827 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
2830 btrfs_release_path(NULL, path);
2832 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2833 ret = btrfs_update_block_group(trans, root, bytenr,
2834 found_key.offset, 0, 0);
2840 btrfs_release_path(NULL, path);
2845 * for a single backref, this will allocate a new extent
2846 * and add the backref to it.
2848 static int record_extent(struct btrfs_trans_handle *trans,
2849 struct btrfs_fs_info *info,
2850 struct btrfs_path *path,
2851 struct extent_record *rec,
2852 struct extent_backref *back,
2853 int allocated, u64 flags)
2856 struct btrfs_root *extent_root = info->extent_root;
2857 struct extent_buffer *leaf;
2858 struct btrfs_key ins_key;
2859 struct btrfs_extent_item *ei;
2860 struct tree_backref *tback;
2861 struct data_backref *dback;
2862 struct btrfs_tree_block_info *bi;
2865 rec->max_size = max_t(u64, rec->max_size,
2866 info->extent_root->leafsize);
2869 u32 item_size = sizeof(*ei);
2872 item_size += sizeof(*bi);
2874 ins_key.objectid = rec->start;
2875 ins_key.offset = rec->max_size;
2876 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
2878 ret = btrfs_insert_empty_item(trans, extent_root, path,
2879 &ins_key, item_size);
2883 leaf = path->nodes[0];
2884 ei = btrfs_item_ptr(leaf, path->slots[0],
2885 struct btrfs_extent_item);
2887 btrfs_set_extent_refs(leaf, ei, 0);
2888 btrfs_set_extent_generation(leaf, ei, rec->generation);
2890 if (back->is_data) {
2891 btrfs_set_extent_flags(leaf, ei,
2892 BTRFS_EXTENT_FLAG_DATA);
2894 struct btrfs_disk_key copy_key;;
2896 tback = (struct tree_backref *)back;
2897 bi = (struct btrfs_tree_block_info *)(ei + 1);
2898 memset_extent_buffer(leaf, 0, (unsigned long)bi,
2900 memset(©_key, 0, sizeof(copy_key));
2902 copy_key.objectid = le64_to_cpu(rec->info_objectid);
2903 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
2904 btrfs_set_tree_block_key(leaf, bi, ©_key);
2906 btrfs_set_extent_flags(leaf, ei,
2907 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
2910 btrfs_mark_buffer_dirty(leaf);
2911 ret = btrfs_update_block_group(trans, extent_root, rec->start,
2912 rec->max_size, 1, 0);
2915 btrfs_release_path(NULL, path);
2918 if (back->is_data) {
2922 dback = (struct data_backref *)back;
2923 if (back->full_backref)
2924 parent = dback->parent;
2928 for (i = 0; i < dback->found_ref; i++) {
2929 /* if parent != 0, we're doing a full backref
2930 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
2931 * just makes the backref allocator create a data
2934 ret = btrfs_inc_extent_ref(trans, info->extent_root,
2935 rec->start, rec->max_size,
2939 BTRFS_FIRST_FREE_OBJECTID :
2945 fprintf(stderr, "adding new data backref"
2946 " on %llu %s %llu owner %llu"
2947 " offset %llu found %d\n",
2948 (unsigned long long)rec->start,
2949 back->full_backref ?
2951 back->full_backref ?
2952 (unsigned long long)parent :
2953 (unsigned long long)dback->root,
2954 (unsigned long long)dback->owner,
2955 (unsigned long long)dback->offset,
2960 tback = (struct tree_backref *)back;
2961 if (back->full_backref)
2962 parent = tback->parent;
2966 ret = btrfs_inc_extent_ref(trans, info->extent_root,
2967 rec->start, rec->max_size,
2968 parent, tback->root, 0, 0);
2969 fprintf(stderr, "adding new tree backref on "
2970 "start %llu len %llu parent %llu root %llu\n",
2971 rec->start, rec->max_size, tback->parent, tback->root);
2976 btrfs_release_path(NULL, path);
2981 * when an incorrect extent item is found, this will delete
2982 * all of the existing entries for it and recreate them
2983 * based on what the tree scan found.
2985 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
2986 struct btrfs_fs_info *info,
2987 struct extent_record *rec)
2990 struct btrfs_path *path;
2991 struct list_head *cur = rec->backrefs.next;
2992 struct cache_extent *cache;
2993 struct extent_backref *back;
2997 /* remember our flags for recreating the extent */
2998 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
2999 rec->max_size, NULL, &flags);
3001 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3003 path = btrfs_alloc_path();
3005 /* step one, delete all the existing records */
3006 ret = delete_extent_records(trans, info->extent_root, path,
3007 rec->start, rec->max_size);
3012 /* was this block corrupt? If so, don't add references to it */
3013 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3019 /* step two, recreate all the refs we did find */
3020 while(cur != &rec->backrefs) {
3021 back = list_entry(cur, struct extent_backref, list);
3025 * if we didn't find any references, don't create a
3028 if (!back->found_ref)
3031 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3038 btrfs_free_path(path);
3042 /* right now we only prune from the extent allocation tree */
3043 static int prune_one_block(struct btrfs_trans_handle *trans,
3044 struct btrfs_fs_info *info,
3045 struct btrfs_corrupt_block *corrupt)
3048 struct btrfs_path path;
3049 struct extent_buffer *eb;
3053 int level = corrupt->level + 1;
3055 btrfs_init_path(&path);
3057 /* we want to stop at the parent to our busted block */
3058 path.lowest_level = level;
3060 ret = btrfs_search_slot(trans, info->extent_root,
3061 &corrupt->key, &path, -1, 1);
3066 eb = path.nodes[level];
3073 * hopefully the search gave us the block we want to prune,
3074 * lets try that first
3076 slot = path.slots[level];
3077 found = btrfs_node_blockptr(eb, slot);
3078 if (found == corrupt->cache.start)
3081 nritems = btrfs_header_nritems(eb);
3083 /* the search failed, lets scan this node and hope we find it */
3084 for (slot = 0; slot < nritems; slot++) {
3085 found = btrfs_node_blockptr(eb, slot);
3086 if (found == corrupt->cache.start)
3090 * we couldn't find the bad block. TODO, search all the nodes for pointers
3093 if (eb == info->extent_root->node) {
3098 btrfs_release_path(NULL, &path);
3103 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3104 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3107 btrfs_release_path(NULL, &path);
3111 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3112 struct btrfs_fs_info *info)
3114 struct cache_extent *cache;
3115 struct btrfs_corrupt_block *corrupt;
3117 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3121 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3122 prune_one_block(trans, info, corrupt);
3123 cache = next_cache_extent(cache);
3128 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3130 struct cache_extent *cache;
3131 struct btrfs_corrupt_block *corrupt;
3134 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3137 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3138 remove_cache_extent(info->corrupt_blocks, cache);
3143 static int check_extent_refs(struct btrfs_trans_handle *trans,
3144 struct btrfs_root *root,
3145 struct cache_tree *extent_cache, int repair)
3147 struct extent_record *rec;
3148 struct cache_extent *cache;
3155 * if we're doing a repair, we have to make sure
3156 * we don't allocate from the problem extents.
3157 * In the worst case, this will be all the
3160 cache = find_first_cache_extent(extent_cache, 0);
3162 rec = container_of(cache, struct extent_record, cache);
3163 btrfs_pin_extent(root->fs_info,
3164 rec->start, rec->max_size);
3165 cache = next_cache_extent(cache);
3168 /* pin down all the corrupted blocks too */
3169 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3171 rec = container_of(cache, struct extent_record, cache);
3172 btrfs_pin_extent(root->fs_info,
3173 rec->start, rec->max_size);
3174 cache = next_cache_extent(cache);
3176 prune_corrupt_blocks(trans, root->fs_info);
3180 cache = find_first_cache_extent(extent_cache, 0);
3183 rec = container_of(cache, struct extent_record, cache);
3184 if (rec->refs != rec->extent_item_refs) {
3185 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3186 (unsigned long long)rec->start,
3187 (unsigned long long)rec->nr);
3188 fprintf(stderr, "extent item %llu, found %llu\n",
3189 (unsigned long long)rec->extent_item_refs,
3190 (unsigned long long)rec->refs);
3191 if (!fixed && repair) {
3192 ret = fixup_extent_refs(trans, root->fs_info, rec);
3200 if (all_backpointers_checked(rec, 1)) {
3201 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3202 (unsigned long long)rec->start,
3203 (unsigned long long)rec->nr);
3205 if (!fixed && repair) {
3206 ret = fixup_extent_refs(trans, root->fs_info, rec);
3214 if (!rec->owner_ref_checked) {
3215 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3216 (unsigned long long)rec->start,
3217 (unsigned long long)rec->nr);
3218 if (!fixed && repair) {
3219 ret = fixup_extent_refs(trans, root->fs_info, rec);
3227 remove_cache_extent(extent_cache, cache);
3228 free_all_extent_backrefs(rec);
3234 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3237 btrfs_fix_block_accounting(trans, root);
3240 fprintf(stderr, "repaired damaged extent references\n");
3246 static int check_extents(struct btrfs_trans_handle *trans,
3247 struct btrfs_root *root, int repair)
3249 struct cache_tree extent_cache;
3250 struct cache_tree seen;
3251 struct cache_tree pending;
3252 struct cache_tree reada;
3253 struct cache_tree nodes;
3254 struct cache_tree corrupt_blocks;
3255 struct btrfs_path path;
3256 struct btrfs_key key;
3257 struct btrfs_key found_key;
3260 struct block_info *bits;
3262 struct extent_buffer *leaf;
3264 struct btrfs_root_item ri;
3266 cache_tree_init(&extent_cache);
3267 cache_tree_init(&seen);
3268 cache_tree_init(&pending);
3269 cache_tree_init(&nodes);
3270 cache_tree_init(&reada);
3271 cache_tree_init(&corrupt_blocks);
3274 root->fs_info->fsck_extent_cache = &extent_cache;
3275 root->fs_info->free_extent_hook = free_extent_hook;
3276 root->fs_info->corrupt_blocks = &corrupt_blocks;
3280 bits = malloc(bits_nr * sizeof(struct block_info));
3286 add_root_to_pending(root->fs_info->tree_root->node, bits, bits_nr,
3287 &extent_cache, &pending, &seen, &reada, &nodes,
3288 &root->fs_info->tree_root->root_key);
3290 add_root_to_pending(root->fs_info->chunk_root->node, bits, bits_nr,
3291 &extent_cache, &pending, &seen, &reada, &nodes,
3292 &root->fs_info->chunk_root->root_key);
3294 btrfs_init_path(&path);
3297 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3298 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3302 leaf = path.nodes[0];
3303 slot = path.slots[0];
3304 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3305 ret = btrfs_next_leaf(root, &path);
3308 leaf = path.nodes[0];
3309 slot = path.slots[0];
3311 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3312 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3313 unsigned long offset;
3314 struct extent_buffer *buf;
3316 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3317 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3318 buf = read_tree_block(root->fs_info->tree_root,
3319 btrfs_root_bytenr(&ri),
3320 btrfs_level_size(root,
3321 btrfs_root_level(&ri)), 0);
3322 add_root_to_pending(buf, bits, bits_nr, &extent_cache,
3323 &pending, &seen, &reada, &nodes,
3325 free_extent_buffer(buf);
3329 btrfs_release_path(root, &path);
3331 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3332 &seen, &reada, &nodes, &extent_cache);
3336 ret = check_extent_refs(trans, root, &extent_cache, repair);
3339 free_corrupt_blocks(root->fs_info);
3340 root->fs_info->fsck_extent_cache = NULL;
3341 root->fs_info->free_extent_hook = NULL;
3342 root->fs_info->corrupt_blocks = NULL;
3348 static void print_usage(void)
3350 fprintf(stderr, "usage: btrfsck dev\n");
3351 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
3355 static struct option long_options[] = {
3356 { "super", 1, NULL, 's' },
3357 { "repair", 0, NULL, 0 },
3358 { "init-csum-tree", 0, NULL, 0 },
3362 int main(int ac, char **av)
3364 struct cache_tree root_cache;
3365 struct btrfs_root *root;
3366 struct btrfs_fs_info *info;
3367 struct btrfs_trans_handle *trans = NULL;
3372 int option_index = 0;
3373 int init_csum_tree = 0;
3378 c = getopt_long(ac, av, "", long_options,
3385 bytenr = btrfs_sb_offset(num);
3386 printf("using SB copy %d, bytenr %llu\n", num,
3387 (unsigned long long)bytenr);
3392 if (option_index == 1) {
3393 printf("enabling repair mode\n");
3396 } else if (option_index == 2) {
3397 printf("Creating a new CRC tree\n");
3409 cache_tree_init(&root_cache);
3411 if((ret = check_mounted(av[optind])) < 0) {
3412 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
3415 fprintf(stderr, "%s is currently mounted. Aborting.\n", av[optind]);
3419 info = open_ctree_fs_info(av[optind], bytenr, rw, 1);
3424 if (!extent_buffer_uptodate(info->tree_root->node) ||
3425 !extent_buffer_uptodate(info->dev_root->node) ||
3426 !extent_buffer_uptodate(info->extent_root->node) ||
3427 !extent_buffer_uptodate(info->chunk_root->node)) {
3428 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
3432 root = info->fs_root;
3434 fprintf(stderr, "checking extents\n");
3436 trans = btrfs_start_transaction(root, 1);
3438 if (init_csum_tree) {
3439 fprintf(stderr, "Reinit crc root\n");
3440 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
3442 fprintf(stderr, "crc root initialization failed\n");
3448 ret = check_extents(trans, root, repair);
3450 fprintf(stderr, "Errors found in extent allocation tree\n");
3452 fprintf(stderr, "checking fs roots\n");
3453 ret = check_fs_roots(root, &root_cache);
3457 fprintf(stderr, "checking root refs\n");
3458 ret = check_root_refs(root, &root_cache);
3460 free_root_recs(&root_cache);
3462 ret = btrfs_commit_transaction(trans, root);
3468 if (found_old_backref) { /*
3469 * there was a disk format change when mixed
3470 * backref was in testing tree. The old format
3471 * existed about one week.
3473 printf("\n * Found old mixed backref format. "
3474 "The old format is not supported! *"
3475 "\n * Please mount the FS in readonly mode, "
3476 "backup data and re-format the FS. *\n\n");
3479 printf("found %llu bytes used err is %d\n",
3480 (unsigned long long)bytes_used, ret);
3481 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
3482 printf("total tree bytes: %llu\n",
3483 (unsigned long long)total_btree_bytes);
3484 printf("total fs tree bytes: %llu\n",
3485 (unsigned long long)total_fs_tree_bytes);
3486 printf("btree space waste bytes: %llu\n",
3487 (unsigned long long)btree_space_waste);
3488 printf("file data blocks allocated: %llu\n referenced %llu\n",
3489 (unsigned long long)data_bytes_allocated,
3490 (unsigned long long)data_bytes_referenced);
3491 printf("%s\n", BTRFS_BUILD_VERSION);