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>
30 #include "kerncompat.h"
35 #include "print-tree.h"
36 #include "transaction.h"
42 static u64 bytes_used = 0;
43 static u64 total_csum_bytes = 0;
44 static u64 total_btree_bytes = 0;
45 static u64 total_fs_tree_bytes = 0;
46 static u64 btree_space_waste = 0;
47 static u64 data_bytes_allocated = 0;
48 static u64 data_bytes_referenced = 0;
49 static int found_old_backref = 0;
51 struct extent_backref {
52 struct list_head list;
53 unsigned int is_data:1;
54 unsigned int found_extent_tree:1;
55 unsigned int full_backref:1;
56 unsigned int found_ref:1;
60 struct extent_backref node;
72 struct extent_backref node;
79 struct extent_record {
80 struct list_head backrefs;
81 struct cache_extent cache;
82 struct btrfs_disk_key parent_key;
91 unsigned int content_checked:1;
92 unsigned int owner_ref_checked:1;
93 unsigned int is_root:1;
96 struct inode_backref {
97 struct list_head list;
98 unsigned int found_dir_item:1;
99 unsigned int found_dir_index:1;
100 unsigned int found_inode_ref:1;
101 unsigned int filetype:8;
103 unsigned int ref_type;
110 #define REF_ERR_NO_DIR_ITEM (1 << 0)
111 #define REF_ERR_NO_DIR_INDEX (1 << 1)
112 #define REF_ERR_NO_INODE_REF (1 << 2)
113 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
114 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
115 #define REF_ERR_DUP_INODE_REF (1 << 5)
116 #define REF_ERR_INDEX_UNMATCH (1 << 6)
117 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
118 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
119 #define REF_ERR_NO_ROOT_REF (1 << 9)
120 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
121 #define REF_ERR_DUP_ROOT_REF (1 << 11)
122 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
124 struct inode_record {
125 struct list_head backrefs;
126 unsigned int checked:1;
127 unsigned int merging:1;
128 unsigned int found_inode_item:1;
129 unsigned int found_dir_item:1;
130 unsigned int found_file_extent:1;
131 unsigned int found_csum_item:1;
132 unsigned int some_csum_missing:1;
133 unsigned int nodatasum:1;
146 u64 first_extent_gap;
151 #define I_ERR_NO_INODE_ITEM (1 << 0)
152 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
153 #define I_ERR_DUP_INODE_ITEM (1 << 2)
154 #define I_ERR_DUP_DIR_INDEX (1 << 3)
155 #define I_ERR_ODD_DIR_ITEM (1 << 4)
156 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
157 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
158 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
159 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
160 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
161 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
162 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
163 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
164 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
166 struct root_backref {
167 struct list_head list;
168 unsigned int found_dir_item:1;
169 unsigned int found_dir_index:1;
170 unsigned int found_back_ref:1;
171 unsigned int found_forward_ref:1;
172 unsigned int reachable:1;
182 struct list_head backrefs;
183 struct cache_extent cache;
184 unsigned int found_root_item:1;
190 struct cache_extent cache;
195 struct cache_extent cache;
196 struct cache_tree root_cache;
197 struct cache_tree inode_cache;
198 struct inode_record *current;
207 struct walk_control {
208 struct cache_tree shared;
209 struct shared_node *nodes[BTRFS_MAX_LEVEL];
214 static u8 imode_to_type(u32 imode)
217 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
218 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
219 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
220 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
221 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
222 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
223 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
224 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
227 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
231 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
233 struct inode_record *rec;
234 struct inode_backref *backref;
235 struct inode_backref *orig;
238 rec = malloc(sizeof(*rec));
239 memcpy(rec, orig_rec, sizeof(*rec));
241 INIT_LIST_HEAD(&rec->backrefs);
243 list_for_each_entry(orig, &orig_rec->backrefs, list) {
244 size = sizeof(*orig) + orig->namelen + 1;
245 backref = malloc(size);
246 memcpy(backref, orig, size);
247 list_add_tail(&backref->list, &rec->backrefs);
252 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
255 struct ptr_node *node;
256 struct cache_extent *cache;
257 struct inode_record *rec = NULL;
260 cache = find_cache_extent(inode_cache, ino, 1);
262 node = container_of(cache, struct ptr_node, cache);
264 if (mod && rec->refs > 1) {
265 node->data = clone_inode_rec(rec);
270 rec = calloc(1, sizeof(*rec));
272 rec->extent_start = (u64)-1;
273 rec->first_extent_gap = (u64)-1;
275 INIT_LIST_HEAD(&rec->backrefs);
277 node = malloc(sizeof(*node));
278 node->cache.start = ino;
279 node->cache.size = 1;
282 if (ino == BTRFS_FREE_INO_OBJECTID)
285 ret = insert_existing_cache_extent(inode_cache, &node->cache);
291 static void free_inode_rec(struct inode_record *rec)
293 struct inode_backref *backref;
298 while (!list_empty(&rec->backrefs)) {
299 backref = list_entry(rec->backrefs.next,
300 struct inode_backref, list);
301 list_del(&backref->list);
307 static int can_free_inode_rec(struct inode_record *rec)
309 if (!rec->errors && rec->checked && rec->found_inode_item &&
310 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
315 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
316 struct inode_record *rec)
318 struct cache_extent *cache;
319 struct inode_backref *tmp, *backref;
320 struct ptr_node *node;
321 unsigned char filetype;
323 if (!rec->found_inode_item)
326 filetype = imode_to_type(rec->imode);
327 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
328 if (backref->found_dir_item && backref->found_dir_index) {
329 if (backref->filetype != filetype)
330 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
331 if (!backref->errors && backref->found_inode_ref) {
332 list_del(&backref->list);
338 if (!rec->checked || rec->merging)
341 if (S_ISDIR(rec->imode)) {
342 if (rec->found_size != rec->isize)
343 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
344 if (rec->found_file_extent)
345 rec->errors |= I_ERR_ODD_FILE_EXTENT;
346 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
347 if (rec->found_dir_item)
348 rec->errors |= I_ERR_ODD_DIR_ITEM;
349 if (rec->found_size != rec->nbytes)
350 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
351 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
352 rec->first_extent_gap = 0;
353 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
354 rec->first_extent_gap < rec->isize))
355 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
358 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
359 if (rec->found_csum_item && rec->nodatasum)
360 rec->errors |= I_ERR_ODD_CSUM_ITEM;
361 if (rec->some_csum_missing && !rec->nodatasum)
362 rec->errors |= I_ERR_SOME_CSUM_MISSING;
365 BUG_ON(rec->refs != 1);
366 if (can_free_inode_rec(rec)) {
367 cache = find_cache_extent(inode_cache, rec->ino, 1);
368 node = container_of(cache, struct ptr_node, cache);
369 BUG_ON(node->data != rec);
370 remove_cache_extent(inode_cache, &node->cache);
376 static int check_orphan_item(struct btrfs_root *root, u64 ino)
378 struct btrfs_path path;
379 struct btrfs_key key;
382 key.objectid = BTRFS_ORPHAN_OBJECTID;
383 key.type = BTRFS_ORPHAN_ITEM_KEY;
386 btrfs_init_path(&path);
387 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
388 btrfs_release_path(root, &path);
394 static int process_inode_item(struct extent_buffer *eb,
395 int slot, struct btrfs_key *key,
396 struct shared_node *active_node)
398 struct inode_record *rec;
399 struct btrfs_inode_item *item;
401 rec = active_node->current;
402 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
403 if (rec->found_inode_item) {
404 rec->errors |= I_ERR_DUP_INODE_ITEM;
407 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
408 rec->nlink = btrfs_inode_nlink(eb, item);
409 rec->isize = btrfs_inode_size(eb, item);
410 rec->nbytes = btrfs_inode_nbytes(eb, item);
411 rec->imode = btrfs_inode_mode(eb, item);
412 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
414 rec->found_inode_item = 1;
416 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
417 maybe_free_inode_rec(&active_node->inode_cache, rec);
421 static struct inode_backref *get_inode_backref(struct inode_record *rec,
423 int namelen, u64 dir)
425 struct inode_backref *backref;
427 list_for_each_entry(backref, &rec->backrefs, list) {
428 if (backref->dir != dir || backref->namelen != namelen)
430 if (memcmp(name, backref->name, namelen))
435 backref = malloc(sizeof(*backref) + namelen + 1);
436 memset(backref, 0, sizeof(*backref));
438 backref->namelen = namelen;
439 memcpy(backref->name, name, namelen);
440 backref->name[namelen] = '\0';
441 list_add_tail(&backref->list, &rec->backrefs);
445 static int add_inode_backref(struct cache_tree *inode_cache,
446 u64 ino, u64 dir, u64 index,
447 const char *name, int namelen,
448 int filetype, int itemtype, int errors)
450 struct inode_record *rec;
451 struct inode_backref *backref;
453 rec = get_inode_rec(inode_cache, ino, 1);
454 backref = get_inode_backref(rec, name, namelen, dir);
456 backref->errors |= errors;
457 if (itemtype == BTRFS_DIR_INDEX_KEY) {
458 if (backref->found_dir_index)
459 backref->errors |= REF_ERR_DUP_DIR_INDEX;
460 if (backref->found_inode_ref && backref->index != index)
461 backref->errors |= REF_ERR_INDEX_UNMATCH;
462 if (backref->found_dir_item && backref->filetype != filetype)
463 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
465 backref->index = index;
466 backref->filetype = filetype;
467 backref->found_dir_index = 1;
468 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
470 if (backref->found_dir_item)
471 backref->errors |= REF_ERR_DUP_DIR_ITEM;
472 if (backref->found_dir_index && backref->filetype != filetype)
473 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
475 backref->filetype = filetype;
476 backref->found_dir_item = 1;
477 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
478 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
479 if (backref->found_inode_ref)
480 backref->errors |= REF_ERR_DUP_INODE_REF;
481 if (backref->found_dir_index && backref->index != index)
482 backref->errors |= REF_ERR_INDEX_UNMATCH;
484 backref->ref_type = itemtype;
485 backref->index = index;
486 backref->found_inode_ref = 1;
491 maybe_free_inode_rec(inode_cache, rec);
495 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
496 struct cache_tree *dst_cache)
498 struct inode_backref *backref;
502 list_for_each_entry(backref, &src->backrefs, list) {
503 if (backref->found_dir_index) {
504 add_inode_backref(dst_cache, dst->ino, backref->dir,
505 backref->index, backref->name,
506 backref->namelen, backref->filetype,
507 BTRFS_DIR_INDEX_KEY, backref->errors);
509 if (backref->found_dir_item) {
511 add_inode_backref(dst_cache, dst->ino,
512 backref->dir, 0, backref->name,
513 backref->namelen, backref->filetype,
514 BTRFS_DIR_ITEM_KEY, backref->errors);
516 if (backref->found_inode_ref) {
517 add_inode_backref(dst_cache, dst->ino,
518 backref->dir, backref->index,
519 backref->name, backref->namelen, 0,
520 backref->ref_type, backref->errors);
524 if (src->found_dir_item)
525 dst->found_dir_item = 1;
526 if (src->found_file_extent)
527 dst->found_file_extent = 1;
528 if (src->found_csum_item)
529 dst->found_csum_item = 1;
530 if (src->some_csum_missing)
531 dst->some_csum_missing = 1;
532 if (dst->first_extent_gap > src->first_extent_gap)
533 dst->first_extent_gap = src->first_extent_gap;
535 BUG_ON(src->found_link < dir_count);
536 dst->found_link += src->found_link - dir_count;
537 dst->found_size += src->found_size;
538 if (src->extent_start != (u64)-1) {
539 if (dst->extent_start == (u64)-1) {
540 dst->extent_start = src->extent_start;
541 dst->extent_end = src->extent_end;
543 if (dst->extent_end > src->extent_start)
544 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
545 else if (dst->extent_end < src->extent_start &&
546 dst->extent_end < dst->first_extent_gap)
547 dst->first_extent_gap = dst->extent_end;
548 if (dst->extent_end < src->extent_end)
549 dst->extent_end = src->extent_end;
553 dst->errors |= src->errors;
554 if (src->found_inode_item) {
555 if (!dst->found_inode_item) {
556 dst->nlink = src->nlink;
557 dst->isize = src->isize;
558 dst->nbytes = src->nbytes;
559 dst->imode = src->imode;
560 dst->nodatasum = src->nodatasum;
561 dst->found_inode_item = 1;
563 dst->errors |= I_ERR_DUP_INODE_ITEM;
571 static int splice_shared_node(struct shared_node *src_node,
572 struct shared_node *dst_node)
574 struct cache_extent *cache;
575 struct ptr_node *node, *ins;
576 struct cache_tree *src, *dst;
577 struct inode_record *rec, *conflict;
582 if (--src_node->refs == 0)
584 if (src_node->current)
585 current_ino = src_node->current->ino;
587 src = &src_node->root_cache;
588 dst = &dst_node->root_cache;
590 cache = find_first_cache_extent(src, 0);
592 node = container_of(cache, struct ptr_node, cache);
594 cache = next_cache_extent(cache);
597 remove_cache_extent(src, &node->cache);
600 ins = malloc(sizeof(*ins));
601 ins->cache.start = node->cache.start;
602 ins->cache.size = node->cache.size;
606 ret = insert_existing_cache_extent(dst, &ins->cache);
607 if (ret == -EEXIST) {
608 conflict = get_inode_rec(dst, rec->ino, 1);
609 merge_inode_recs(rec, conflict, dst);
611 conflict->checked = 1;
612 if (dst_node->current == conflict)
613 dst_node->current = NULL;
615 maybe_free_inode_rec(dst, conflict);
623 if (src == &src_node->root_cache) {
624 src = &src_node->inode_cache;
625 dst = &dst_node->inode_cache;
629 if (current_ino > 0 && (!dst_node->current ||
630 current_ino > dst_node->current->ino)) {
631 if (dst_node->current) {
632 dst_node->current->checked = 1;
633 maybe_free_inode_rec(dst, dst_node->current);
635 dst_node->current = get_inode_rec(dst, current_ino, 1);
640 static void free_inode_recs(struct cache_tree *inode_cache)
642 struct cache_extent *cache;
643 struct ptr_node *node;
644 struct inode_record *rec;
647 cache = find_first_cache_extent(inode_cache, 0);
650 node = container_of(cache, struct ptr_node, cache);
652 remove_cache_extent(inode_cache, &node->cache);
658 static struct shared_node *find_shared_node(struct cache_tree *shared,
661 struct cache_extent *cache;
662 struct shared_node *node;
664 cache = find_cache_extent(shared, bytenr, 1);
666 node = container_of(cache, struct shared_node, cache);
672 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
675 struct shared_node *node;
677 node = calloc(1, sizeof(*node));
678 node->cache.start = bytenr;
679 node->cache.size = 1;
680 cache_tree_init(&node->root_cache);
681 cache_tree_init(&node->inode_cache);
684 ret = insert_existing_cache_extent(shared, &node->cache);
689 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
690 struct walk_control *wc, int level)
692 struct shared_node *node;
693 struct shared_node *dest;
695 if (level == wc->active_node)
698 BUG_ON(wc->active_node <= level);
699 node = find_shared_node(&wc->shared, bytenr);
701 add_shared_node(&wc->shared, bytenr, refs);
702 node = find_shared_node(&wc->shared, bytenr);
703 wc->nodes[level] = node;
704 wc->active_node = level;
708 if (wc->root_level == wc->active_node &&
709 btrfs_root_refs(&root->root_item) == 0) {
710 if (--node->refs == 0) {
711 free_inode_recs(&node->root_cache);
712 free_inode_recs(&node->inode_cache);
713 remove_cache_extent(&wc->shared, &node->cache);
719 dest = wc->nodes[wc->active_node];
720 splice_shared_node(node, dest);
721 if (node->refs == 0) {
722 remove_cache_extent(&wc->shared, &node->cache);
728 static int leave_shared_node(struct btrfs_root *root,
729 struct walk_control *wc, int level)
731 struct shared_node *node;
732 struct shared_node *dest;
735 if (level == wc->root_level)
738 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
742 BUG_ON(i >= BTRFS_MAX_LEVEL);
744 node = wc->nodes[wc->active_node];
745 wc->nodes[wc->active_node] = NULL;
748 dest = wc->nodes[wc->active_node];
749 if (wc->active_node < wc->root_level ||
750 btrfs_root_refs(&root->root_item) > 0) {
751 BUG_ON(node->refs <= 1);
752 splice_shared_node(node, dest);
754 BUG_ON(node->refs < 2);
760 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
763 struct btrfs_path path;
764 struct btrfs_key key;
765 struct extent_buffer *leaf;
769 btrfs_init_path(&path);
771 key.objectid = parent_root_id;
772 key.type = BTRFS_ROOT_REF_KEY;
773 key.offset = child_root_id;
774 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
777 btrfs_release_path(root, &path);
781 key.objectid = child_root_id;
782 key.type = BTRFS_ROOT_BACKREF_KEY;
784 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
789 leaf = path.nodes[0];
790 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
791 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
796 leaf = path.nodes[0];
799 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
800 if (key.objectid != child_root_id ||
801 key.type != BTRFS_ROOT_BACKREF_KEY)
806 if (key.offset == parent_root_id) {
807 btrfs_release_path(root, &path);
814 btrfs_release_path(root, &path);
815 return has_parent? 0 : -1;
818 static int process_dir_item(struct btrfs_root *root,
819 struct extent_buffer *eb,
820 int slot, struct btrfs_key *key,
821 struct shared_node *active_node)
831 struct btrfs_dir_item *di;
832 struct inode_record *rec;
833 struct cache_tree *root_cache;
834 struct cache_tree *inode_cache;
835 struct btrfs_key location;
836 char namebuf[BTRFS_NAME_LEN];
838 root_cache = &active_node->root_cache;
839 inode_cache = &active_node->inode_cache;
840 rec = active_node->current;
841 rec->found_dir_item = 1;
843 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
844 total = btrfs_item_size_nr(eb, slot);
845 while (cur < total) {
847 btrfs_dir_item_key_to_cpu(eb, di, &location);
848 name_len = btrfs_dir_name_len(eb, di);
849 data_len = btrfs_dir_data_len(eb, di);
850 filetype = btrfs_dir_type(eb, di);
852 rec->found_size += name_len;
853 if (name_len <= BTRFS_NAME_LEN) {
857 len = BTRFS_NAME_LEN;
858 error = REF_ERR_NAME_TOO_LONG;
860 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
862 if (location.type == BTRFS_INODE_ITEM_KEY) {
863 add_inode_backref(inode_cache, location.objectid,
864 key->objectid, key->offset, namebuf,
865 len, filetype, key->type, error);
866 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
867 add_inode_backref(root_cache, location.objectid,
868 key->objectid, key->offset,
869 namebuf, len, filetype,
872 fprintf(stderr, "warning line %d\n", __LINE__);
875 len = sizeof(*di) + name_len + data_len;
876 di = (struct btrfs_dir_item *)((char *)di + len);
879 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
880 rec->errors |= I_ERR_DUP_DIR_INDEX;
885 static int process_inode_ref(struct extent_buffer *eb,
886 int slot, struct btrfs_key *key,
887 struct shared_node *active_node)
895 struct cache_tree *inode_cache;
896 struct btrfs_inode_ref *ref;
897 char namebuf[BTRFS_NAME_LEN];
899 inode_cache = &active_node->inode_cache;
901 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
902 total = btrfs_item_size_nr(eb, slot);
903 while (cur < total) {
904 name_len = btrfs_inode_ref_name_len(eb, ref);
905 index = btrfs_inode_ref_index(eb, ref);
906 if (name_len <= BTRFS_NAME_LEN) {
910 len = BTRFS_NAME_LEN;
911 error = REF_ERR_NAME_TOO_LONG;
913 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
914 add_inode_backref(inode_cache, key->objectid, key->offset,
915 index, namebuf, len, 0, key->type, error);
917 len = sizeof(*ref) + name_len;
918 ref = (struct btrfs_inode_ref *)((char *)ref + len);
924 static int process_inode_extref(struct extent_buffer *eb,
925 int slot, struct btrfs_key *key,
926 struct shared_node *active_node)
935 struct cache_tree *inode_cache;
936 struct btrfs_inode_extref *extref;
937 char namebuf[BTRFS_NAME_LEN];
939 inode_cache = &active_node->inode_cache;
941 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
942 total = btrfs_item_size_nr(eb, slot);
943 while (cur < total) {
944 name_len = btrfs_inode_extref_name_len(eb, extref);
945 index = btrfs_inode_extref_index(eb, extref);
946 parent = btrfs_inode_extref_parent(eb, extref);
947 if (name_len <= BTRFS_NAME_LEN) {
951 len = BTRFS_NAME_LEN;
952 error = REF_ERR_NAME_TOO_LONG;
954 read_extent_buffer(eb, namebuf,
955 (unsigned long)(extref + 1), len);
956 add_inode_backref(inode_cache, key->objectid, parent,
957 index, namebuf, len, 0, key->type, error);
959 len = sizeof(*extref) + name_len;
960 extref = (struct btrfs_inode_extref *)((char *)extref + len);
967 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
969 struct btrfs_key key;
970 struct btrfs_path path;
971 struct extent_buffer *leaf;
976 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
978 btrfs_init_path(&path);
980 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
982 key.type = BTRFS_EXTENT_CSUM_KEY;
984 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
987 if (ret > 0 && path.slots[0] > 0) {
988 leaf = path.nodes[0];
989 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
990 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
991 key.type == BTRFS_EXTENT_CSUM_KEY)
996 leaf = path.nodes[0];
997 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
998 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1002 leaf = path.nodes[0];
1005 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1006 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1007 key.type != BTRFS_EXTENT_CSUM_KEY)
1010 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1011 if (key.offset >= start + len)
1014 if (key.offset > start)
1017 size = btrfs_item_size_nr(leaf, path.slots[0]);
1018 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1019 if (csum_end > start) {
1020 size = min(csum_end - start, len);
1028 btrfs_release_path(root->fs_info->csum_root, &path);
1032 static int process_file_extent(struct btrfs_root *root,
1033 struct extent_buffer *eb,
1034 int slot, struct btrfs_key *key,
1035 struct shared_node *active_node)
1037 struct inode_record *rec;
1038 struct btrfs_file_extent_item *fi;
1040 u64 disk_bytenr = 0;
1041 u64 extent_offset = 0;
1042 u64 mask = root->sectorsize - 1;
1045 rec = active_node->current;
1046 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1047 rec->found_file_extent = 1;
1049 if (rec->extent_start == (u64)-1) {
1050 rec->extent_start = key->offset;
1051 rec->extent_end = key->offset;
1054 if (rec->extent_end > key->offset)
1055 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1056 else if (rec->extent_end < key->offset &&
1057 rec->extent_end < rec->first_extent_gap)
1058 rec->first_extent_gap = rec->extent_end;
1060 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1061 extent_type = btrfs_file_extent_type(eb, fi);
1063 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1064 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1066 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1067 rec->found_size += num_bytes;
1068 num_bytes = (num_bytes + mask) & ~mask;
1069 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1070 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1071 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1072 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1073 extent_offset = btrfs_file_extent_offset(eb, fi);
1074 if (num_bytes == 0 || (num_bytes & mask))
1075 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1076 if (num_bytes + extent_offset >
1077 btrfs_file_extent_ram_bytes(eb, fi))
1078 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1079 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1080 (btrfs_file_extent_compression(eb, fi) ||
1081 btrfs_file_extent_encryption(eb, fi) ||
1082 btrfs_file_extent_other_encoding(eb, fi)))
1083 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1084 if (disk_bytenr > 0)
1085 rec->found_size += num_bytes;
1087 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1089 rec->extent_end = key->offset + num_bytes;
1091 if (disk_bytenr > 0) {
1093 if (btrfs_file_extent_compression(eb, fi))
1094 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1096 disk_bytenr += extent_offset;
1098 found = count_csum_range(root, disk_bytenr, num_bytes);
1099 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1101 rec->found_csum_item = 1;
1102 if (found < num_bytes)
1103 rec->some_csum_missing = 1;
1104 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1106 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1112 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1113 struct walk_control *wc)
1115 struct btrfs_key key;
1119 struct cache_tree *inode_cache;
1120 struct shared_node *active_node;
1122 if (wc->root_level == wc->active_node &&
1123 btrfs_root_refs(&root->root_item) == 0)
1126 active_node = wc->nodes[wc->active_node];
1127 inode_cache = &active_node->inode_cache;
1128 nritems = btrfs_header_nritems(eb);
1129 for (i = 0; i < nritems; i++) {
1130 btrfs_item_key_to_cpu(eb, &key, i);
1132 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1135 if (active_node->current == NULL ||
1136 active_node->current->ino < key.objectid) {
1137 if (active_node->current) {
1138 active_node->current->checked = 1;
1139 maybe_free_inode_rec(inode_cache,
1140 active_node->current);
1142 active_node->current = get_inode_rec(inode_cache,
1146 case BTRFS_DIR_ITEM_KEY:
1147 case BTRFS_DIR_INDEX_KEY:
1148 ret = process_dir_item(root, eb, i, &key, active_node);
1150 case BTRFS_INODE_REF_KEY:
1151 ret = process_inode_ref(eb, i, &key, active_node);
1153 case BTRFS_INODE_EXTREF_KEY:
1154 ret = process_inode_extref(eb, i, &key, active_node);
1156 case BTRFS_INODE_ITEM_KEY:
1157 ret = process_inode_item(eb, i, &key, active_node);
1159 case BTRFS_EXTENT_DATA_KEY:
1160 ret = process_file_extent(root, eb, i, &key,
1170 static void reada_walk_down(struct btrfs_root *root,
1171 struct extent_buffer *node, int slot)
1181 level = btrfs_header_level(node);
1185 nritems = btrfs_header_nritems(node);
1186 blocksize = btrfs_level_size(root, level - 1);
1187 for (i = slot; i < nritems; i++) {
1188 bytenr = btrfs_node_blockptr(node, i);
1189 ptr_gen = btrfs_node_ptr_generation(node, i);
1190 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1196 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1197 struct walk_control *wc, int *level)
1201 struct extent_buffer *next;
1202 struct extent_buffer *cur;
1207 WARN_ON(*level < 0);
1208 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1209 ret = btrfs_lookup_extent_info(NULL, root,
1210 path->nodes[*level]->start,
1211 path->nodes[*level]->len, &refs, NULL);
1216 ret = enter_shared_node(root, path->nodes[*level]->start,
1222 while (*level >= 0) {
1223 WARN_ON(*level < 0);
1224 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1225 cur = path->nodes[*level];
1227 if (btrfs_header_level(cur) != *level)
1230 if (path->slots[*level] >= btrfs_header_nritems(cur))
1233 ret = process_one_leaf(root, cur, wc);
1236 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1237 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1238 blocksize = btrfs_level_size(root, *level - 1);
1239 ret = btrfs_lookup_extent_info(NULL, root, bytenr, blocksize,
1245 ret = enter_shared_node(root, bytenr, refs,
1248 path->slots[*level]++;
1253 next = btrfs_find_tree_block(root, bytenr, blocksize);
1254 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1255 free_extent_buffer(next);
1256 reada_walk_down(root, cur, path->slots[*level]);
1257 next = read_tree_block(root, bytenr, blocksize,
1261 *level = *level - 1;
1262 free_extent_buffer(path->nodes[*level]);
1263 path->nodes[*level] = next;
1264 path->slots[*level] = 0;
1267 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1271 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1272 struct walk_control *wc, int *level)
1275 struct extent_buffer *leaf;
1277 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1278 leaf = path->nodes[i];
1279 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1284 free_extent_buffer(path->nodes[*level]);
1285 path->nodes[*level] = NULL;
1286 BUG_ON(*level > wc->active_node);
1287 if (*level == wc->active_node)
1288 leave_shared_node(root, wc, *level);
1295 static int check_root_dir(struct inode_record *rec)
1297 struct inode_backref *backref;
1300 if (!rec->found_inode_item || rec->errors)
1302 if (rec->nlink != 1 || rec->found_link != 0)
1304 if (list_empty(&rec->backrefs))
1306 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1307 if (!backref->found_inode_ref)
1309 if (backref->index != 0 || backref->namelen != 2 ||
1310 memcmp(backref->name, "..", 2))
1312 if (backref->found_dir_index || backref->found_dir_item)
1319 static int check_inode_recs(struct btrfs_root *root,
1320 struct cache_tree *inode_cache)
1322 struct cache_extent *cache;
1323 struct ptr_node *node;
1324 struct inode_record *rec;
1325 struct inode_backref *backref;
1328 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1330 if (btrfs_root_refs(&root->root_item) == 0) {
1331 if (!cache_tree_empty(inode_cache))
1332 fprintf(stderr, "warning line %d\n", __LINE__);
1336 rec = get_inode_rec(inode_cache, root_dirid, 0);
1338 ret = check_root_dir(rec);
1340 fprintf(stderr, "root %llu root dir %llu error\n",
1341 (unsigned long long)root->root_key.objectid,
1342 (unsigned long long)root_dirid);
1346 fprintf(stderr, "root %llu root dir %llu not found\n",
1347 (unsigned long long)root->root_key.objectid,
1348 (unsigned long long)root_dirid);
1352 cache = find_first_cache_extent(inode_cache, 0);
1355 node = container_of(cache, struct ptr_node, cache);
1357 remove_cache_extent(inode_cache, &node->cache);
1359 if (rec->ino == root_dirid ||
1360 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1361 free_inode_rec(rec);
1365 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1366 ret = check_orphan_item(root, rec->ino);
1368 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1369 if (can_free_inode_rec(rec)) {
1370 free_inode_rec(rec);
1376 if (!rec->found_inode_item)
1377 rec->errors |= I_ERR_NO_INODE_ITEM;
1378 if (rec->found_link != rec->nlink)
1379 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1380 fprintf(stderr, "root %llu inode %llu errors %x\n",
1381 (unsigned long long) root->root_key.objectid,
1382 (unsigned long long) rec->ino, rec->errors);
1383 list_for_each_entry(backref, &rec->backrefs, list) {
1384 if (!backref->found_dir_item)
1385 backref->errors |= REF_ERR_NO_DIR_ITEM;
1386 if (!backref->found_dir_index)
1387 backref->errors |= REF_ERR_NO_DIR_INDEX;
1388 if (!backref->found_inode_ref)
1389 backref->errors |= REF_ERR_NO_INODE_REF;
1390 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1391 " namelen %u name %s filetype %d error %x\n",
1392 (unsigned long long)backref->dir,
1393 (unsigned long long)backref->index,
1394 backref->namelen, backref->name,
1395 backref->filetype, backref->errors);
1397 free_inode_rec(rec);
1399 return (error > 0) ? -1 : 0;
1402 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1405 struct cache_extent *cache;
1406 struct root_record *rec = NULL;
1409 cache = find_cache_extent(root_cache, objectid, 1);
1411 rec = container_of(cache, struct root_record, cache);
1413 rec = calloc(1, sizeof(*rec));
1414 rec->objectid = objectid;
1415 INIT_LIST_HEAD(&rec->backrefs);
1416 rec->cache.start = objectid;
1417 rec->cache.size = 1;
1419 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1425 static struct root_backref *get_root_backref(struct root_record *rec,
1426 u64 ref_root, u64 dir, u64 index,
1427 const char *name, int namelen)
1429 struct root_backref *backref;
1431 list_for_each_entry(backref, &rec->backrefs, list) {
1432 if (backref->ref_root != ref_root || backref->dir != dir ||
1433 backref->namelen != namelen)
1435 if (memcmp(name, backref->name, namelen))
1440 backref = malloc(sizeof(*backref) + namelen + 1);
1441 memset(backref, 0, sizeof(*backref));
1442 backref->ref_root = ref_root;
1444 backref->index = index;
1445 backref->namelen = namelen;
1446 memcpy(backref->name, name, namelen);
1447 backref->name[namelen] = '\0';
1448 list_add_tail(&backref->list, &rec->backrefs);
1452 static void free_root_recs(struct cache_tree *root_cache)
1454 struct cache_extent *cache;
1455 struct root_record *rec;
1456 struct root_backref *backref;
1459 cache = find_first_cache_extent(root_cache, 0);
1462 rec = container_of(cache, struct root_record, cache);
1463 remove_cache_extent(root_cache, &rec->cache);
1465 while (!list_empty(&rec->backrefs)) {
1466 backref = list_entry(rec->backrefs.next,
1467 struct root_backref, list);
1468 list_del(&backref->list);
1475 static int add_root_backref(struct cache_tree *root_cache,
1476 u64 root_id, u64 ref_root, u64 dir, u64 index,
1477 const char *name, int namelen,
1478 int item_type, int errors)
1480 struct root_record *rec;
1481 struct root_backref *backref;
1483 rec = get_root_rec(root_cache, root_id);
1484 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1486 backref->errors |= errors;
1488 if (item_type != BTRFS_DIR_ITEM_KEY) {
1489 if (backref->found_dir_index || backref->found_back_ref ||
1490 backref->found_forward_ref) {
1491 if (backref->index != index)
1492 backref->errors |= REF_ERR_INDEX_UNMATCH;
1494 backref->index = index;
1498 if (item_type == BTRFS_DIR_ITEM_KEY) {
1499 backref->found_dir_item = 1;
1500 backref->reachable = 1;
1502 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1503 backref->found_dir_index = 1;
1504 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1505 if (backref->found_forward_ref)
1506 backref->errors |= REF_ERR_DUP_ROOT_REF;
1507 backref->found_forward_ref = 1;
1508 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1509 if (backref->found_back_ref)
1510 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1511 backref->found_back_ref = 1;
1519 static int merge_root_recs(struct btrfs_root *root,
1520 struct cache_tree *src_cache,
1521 struct cache_tree *dst_cache)
1523 struct cache_extent *cache;
1524 struct ptr_node *node;
1525 struct inode_record *rec;
1526 struct inode_backref *backref;
1528 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1529 free_inode_recs(src_cache);
1534 cache = find_first_cache_extent(src_cache, 0);
1537 node = container_of(cache, struct ptr_node, cache);
1539 remove_cache_extent(src_cache, &node->cache);
1542 if (!is_child_root(root, root->objectid, rec->ino))
1545 list_for_each_entry(backref, &rec->backrefs, list) {
1546 BUG_ON(backref->found_inode_ref);
1547 if (backref->found_dir_item)
1548 add_root_backref(dst_cache, rec->ino,
1549 root->root_key.objectid, backref->dir,
1550 backref->index, backref->name,
1551 backref->namelen, BTRFS_DIR_ITEM_KEY,
1553 if (backref->found_dir_index)
1554 add_root_backref(dst_cache, rec->ino,
1555 root->root_key.objectid, backref->dir,
1556 backref->index, backref->name,
1557 backref->namelen, BTRFS_DIR_INDEX_KEY,
1561 free_inode_rec(rec);
1566 static int check_root_refs(struct btrfs_root *root,
1567 struct cache_tree *root_cache)
1569 struct root_record *rec;
1570 struct root_record *ref_root;
1571 struct root_backref *backref;
1572 struct cache_extent *cache;
1578 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1581 /* fixme: this can not detect circular references */
1584 cache = find_first_cache_extent(root_cache, 0);
1588 rec = container_of(cache, struct root_record, cache);
1589 cache = next_cache_extent(cache);
1591 if (rec->found_ref == 0)
1594 list_for_each_entry(backref, &rec->backrefs, list) {
1595 if (!backref->reachable)
1598 ref_root = get_root_rec(root_cache,
1600 if (ref_root->found_ref > 0)
1603 backref->reachable = 0;
1605 if (rec->found_ref == 0)
1611 cache = find_first_cache_extent(root_cache, 0);
1615 rec = container_of(cache, struct root_record, cache);
1616 cache = next_cache_extent(cache);
1618 if (rec->found_ref == 0 &&
1619 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1620 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1621 ret = check_orphan_item(root->fs_info->tree_root,
1626 fprintf(stderr, "fs tree %llu not referenced\n",
1627 (unsigned long long)rec->objectid);
1631 if (rec->found_ref > 0 && !rec->found_root_item)
1633 list_for_each_entry(backref, &rec->backrefs, list) {
1634 if (!backref->found_dir_item)
1635 backref->errors |= REF_ERR_NO_DIR_ITEM;
1636 if (!backref->found_dir_index)
1637 backref->errors |= REF_ERR_NO_DIR_INDEX;
1638 if (!backref->found_back_ref)
1639 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1640 if (!backref->found_forward_ref)
1641 backref->errors |= REF_ERR_NO_ROOT_REF;
1642 if (backref->reachable && backref->errors)
1649 fprintf(stderr, "fs tree %llu refs %u %s\n",
1650 (unsigned long long)rec->objectid, rec->found_ref,
1651 rec->found_root_item ? "" : "not found");
1653 list_for_each_entry(backref, &rec->backrefs, list) {
1654 if (!backref->reachable)
1656 if (!backref->errors && rec->found_root_item)
1658 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1659 " index %llu namelen %u name %s error %x\n",
1660 (unsigned long long)backref->ref_root,
1661 (unsigned long long)backref->dir,
1662 (unsigned long long)backref->index,
1663 backref->namelen, backref->name,
1667 return errors > 0 ? 1 : 0;
1670 static int process_root_ref(struct extent_buffer *eb, int slot,
1671 struct btrfs_key *key,
1672 struct cache_tree *root_cache)
1678 struct btrfs_root_ref *ref;
1679 char namebuf[BTRFS_NAME_LEN];
1682 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1684 dirid = btrfs_root_ref_dirid(eb, ref);
1685 index = btrfs_root_ref_sequence(eb, ref);
1686 name_len = btrfs_root_ref_name_len(eb, ref);
1688 if (name_len <= BTRFS_NAME_LEN) {
1692 len = BTRFS_NAME_LEN;
1693 error = REF_ERR_NAME_TOO_LONG;
1695 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1697 if (key->type == BTRFS_ROOT_REF_KEY) {
1698 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1699 index, namebuf, len, key->type, error);
1701 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1702 index, namebuf, len, key->type, error);
1707 static int check_fs_root(struct btrfs_root *root,
1708 struct cache_tree *root_cache,
1709 struct walk_control *wc)
1714 struct btrfs_path path;
1715 struct shared_node root_node;
1716 struct root_record *rec;
1717 struct btrfs_root_item *root_item = &root->root_item;
1719 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1720 rec = get_root_rec(root_cache, root->root_key.objectid);
1721 if (btrfs_root_refs(root_item) > 0)
1722 rec->found_root_item = 1;
1725 btrfs_init_path(&path);
1726 memset(&root_node, 0, sizeof(root_node));
1727 cache_tree_init(&root_node.root_cache);
1728 cache_tree_init(&root_node.inode_cache);
1730 level = btrfs_header_level(root->node);
1731 memset(wc->nodes, 0, sizeof(wc->nodes));
1732 wc->nodes[level] = &root_node;
1733 wc->active_node = level;
1734 wc->root_level = level;
1736 if (btrfs_root_refs(root_item) > 0 ||
1737 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1738 path.nodes[level] = root->node;
1739 extent_buffer_get(root->node);
1740 path.slots[level] = 0;
1742 struct btrfs_key key;
1743 struct btrfs_disk_key found_key;
1745 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1746 level = root_item->drop_level;
1747 path.lowest_level = level;
1748 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1750 btrfs_node_key(path.nodes[level], &found_key,
1752 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1753 sizeof(found_key)));
1757 wret = walk_down_tree(root, &path, wc, &level);
1763 wret = walk_up_tree(root, &path, wc, &level);
1769 btrfs_release_path(root, &path);
1771 merge_root_recs(root, &root_node.root_cache, root_cache);
1773 if (root_node.current) {
1774 root_node.current->checked = 1;
1775 maybe_free_inode_rec(&root_node.inode_cache,
1779 ret = check_inode_recs(root, &root_node.inode_cache);
1783 static int fs_root_objectid(u64 objectid)
1785 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1786 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1787 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1788 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1789 objectid <= BTRFS_LAST_FREE_OBJECTID))
1794 static int check_fs_roots(struct btrfs_root *root,
1795 struct cache_tree *root_cache)
1797 struct btrfs_path path;
1798 struct btrfs_key key;
1799 struct walk_control wc;
1800 struct extent_buffer *leaf;
1801 struct btrfs_root *tmp_root;
1802 struct btrfs_root *tree_root = root->fs_info->tree_root;
1806 memset(&wc, 0, sizeof(wc));
1807 cache_tree_init(&wc.shared);
1808 btrfs_init_path(&path);
1812 key.type = BTRFS_ROOT_ITEM_KEY;
1813 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1816 leaf = path.nodes[0];
1817 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1818 ret = btrfs_next_leaf(tree_root, &path);
1821 leaf = path.nodes[0];
1823 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1824 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1825 fs_root_objectid(key.objectid)) {
1826 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1828 if (IS_ERR(tmp_root)) {
1832 ret = check_fs_root(tmp_root, root_cache, &wc);
1835 btrfs_free_fs_root(root->fs_info, tmp_root);
1836 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1837 key.type == BTRFS_ROOT_BACKREF_KEY) {
1838 process_root_ref(leaf, path.slots[0], &key,
1844 btrfs_release_path(tree_root, &path);
1846 if (!cache_tree_empty(&wc.shared))
1847 fprintf(stderr, "warning line %d\n", __LINE__);
1852 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1854 struct list_head *cur = rec->backrefs.next;
1855 struct extent_backref *back;
1856 struct tree_backref *tback;
1857 struct data_backref *dback;
1861 while(cur != &rec->backrefs) {
1862 back = list_entry(cur, struct extent_backref, list);
1864 if (!back->found_extent_tree) {
1868 if (back->is_data) {
1869 dback = (struct data_backref *)back;
1870 fprintf(stderr, "Backref %llu %s %llu"
1871 " owner %llu offset %llu num_refs %lu"
1872 " not found in extent tree\n",
1873 (unsigned long long)rec->start,
1874 back->full_backref ?
1876 back->full_backref ?
1877 (unsigned long long)dback->parent:
1878 (unsigned long long)dback->root,
1879 (unsigned long long)dback->owner,
1880 (unsigned long long)dback->offset,
1881 (unsigned long)dback->num_refs);
1883 tback = (struct tree_backref *)back;
1884 fprintf(stderr, "Backref %llu parent %llu"
1885 " root %llu not found in extent tree\n",
1886 (unsigned long long)rec->start,
1887 (unsigned long long)tback->parent,
1888 (unsigned long long)tback->root);
1891 if (!back->is_data && !back->found_ref) {
1895 tback = (struct tree_backref *)back;
1896 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1897 (unsigned long long)rec->start,
1898 back->full_backref ? "parent" : "root",
1899 back->full_backref ?
1900 (unsigned long long)tback->parent :
1901 (unsigned long long)tback->root, back);
1903 if (back->is_data) {
1904 dback = (struct data_backref *)back;
1905 if (dback->found_ref != dback->num_refs) {
1909 fprintf(stderr, "Incorrect local backref count"
1910 " on %llu %s %llu owner %llu"
1911 " offset %llu found %u wanted %u back %p\n",
1912 (unsigned long long)rec->start,
1913 back->full_backref ?
1915 back->full_backref ?
1916 (unsigned long long)dback->parent:
1917 (unsigned long long)dback->root,
1918 (unsigned long long)dback->owner,
1919 (unsigned long long)dback->offset,
1920 dback->found_ref, dback->num_refs, back);
1923 if (!back->is_data) {
1926 dback = (struct data_backref *)back;
1927 found += dback->found_ref;
1930 if (found != rec->refs) {
1934 fprintf(stderr, "Incorrect global backref count "
1935 "on %llu found %llu wanted %llu\n",
1936 (unsigned long long)rec->start,
1937 (unsigned long long)found,
1938 (unsigned long long)rec->refs);
1944 static int free_all_extent_backrefs(struct extent_record *rec)
1946 struct extent_backref *back;
1947 struct list_head *cur;
1948 while (!list_empty(&rec->backrefs)) {
1949 cur = rec->backrefs.next;
1950 back = list_entry(cur, struct extent_backref, list);
1957 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
1958 struct extent_record *rec)
1960 if (rec->content_checked && rec->owner_ref_checked &&
1961 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
1962 !all_backpointers_checked(rec, 0)) {
1963 remove_cache_extent(extent_cache, &rec->cache);
1964 free_all_extent_backrefs(rec);
1970 static int check_owner_ref(struct btrfs_root *root,
1971 struct extent_record *rec,
1972 struct extent_buffer *buf)
1974 struct extent_backref *node;
1975 struct tree_backref *back;
1976 struct btrfs_root *ref_root;
1977 struct btrfs_key key;
1978 struct btrfs_path path;
1979 struct extent_buffer *parent;
1984 list_for_each_entry(node, &rec->backrefs, list) {
1987 if (!node->found_ref)
1989 if (node->full_backref)
1991 back = (struct tree_backref *)node;
1992 if (btrfs_header_owner(buf) == back->root)
1995 BUG_ON(rec->is_root);
1997 /* try to find the block by search corresponding fs tree */
1998 key.objectid = btrfs_header_owner(buf);
1999 key.type = BTRFS_ROOT_ITEM_KEY;
2000 key.offset = (u64)-1;
2002 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2003 if (IS_ERR(ref_root))
2006 level = btrfs_header_level(buf);
2008 btrfs_item_key_to_cpu(buf, &key, 0);
2010 btrfs_node_key_to_cpu(buf, &key, 0);
2012 btrfs_init_path(&path);
2013 path.lowest_level = level + 1;
2014 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2018 parent = path.nodes[level + 1];
2019 if (parent && buf->start == btrfs_node_blockptr(parent,
2020 path.slots[level + 1]))
2023 btrfs_release_path(ref_root, &path);
2024 return found ? 0 : 1;
2027 static int is_extent_tree_record(struct extent_record *rec)
2029 struct list_head *cur = rec->backrefs.next;
2030 struct extent_backref *node;
2031 struct tree_backref *back;
2034 while(cur != &rec->backrefs) {
2035 node = list_entry(cur, struct extent_backref, list);
2039 back = (struct tree_backref *)node;
2040 if (node->full_backref)
2042 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2049 static int record_bad_block_io(struct btrfs_fs_info *info,
2050 struct cache_tree *extent_cache,
2053 struct extent_record *rec;
2054 struct cache_extent *cache;
2055 struct btrfs_key key;
2057 cache = find_cache_extent(extent_cache, start, len);
2061 rec = container_of(cache, struct extent_record, cache);
2062 if (!is_extent_tree_record(rec))
2065 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2066 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2069 static int check_block(struct btrfs_root *root,
2070 struct cache_tree *extent_cache,
2071 struct extent_buffer *buf, u64 flags)
2073 struct extent_record *rec;
2074 struct cache_extent *cache;
2075 struct btrfs_key key;
2079 cache = find_cache_extent(extent_cache, buf->start, buf->len);
2082 rec = container_of(cache, struct extent_record, cache);
2083 rec->generation = btrfs_header_generation(buf);
2085 level = btrfs_header_level(buf);
2086 if (btrfs_header_nritems(buf) > 0) {
2089 btrfs_item_key_to_cpu(buf, &key, 0);
2091 btrfs_node_key_to_cpu(buf, &key, 0);
2093 rec->info_objectid = key.objectid;
2095 rec->info_level = level;
2097 if (btrfs_is_leaf(buf))
2098 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2100 ret = btrfs_check_node(root, &rec->parent_key, buf);
2103 fprintf(stderr, "bad block %llu\n",
2104 (unsigned long long)buf->start);
2106 rec->content_checked = 1;
2107 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2108 rec->owner_ref_checked = 1;
2110 ret = check_owner_ref(root, rec, buf);
2112 rec->owner_ref_checked = 1;
2116 maybe_free_extent_rec(extent_cache, rec);
2120 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2121 u64 parent, u64 root)
2123 struct list_head *cur = rec->backrefs.next;
2124 struct extent_backref *node;
2125 struct tree_backref *back;
2127 while(cur != &rec->backrefs) {
2128 node = list_entry(cur, struct extent_backref, list);
2132 back = (struct tree_backref *)node;
2134 if (!node->full_backref)
2136 if (parent == back->parent)
2139 if (node->full_backref)
2141 if (back->root == root)
2148 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2149 u64 parent, u64 root)
2151 struct tree_backref *ref = malloc(sizeof(*ref));
2152 memset(&ref->node, 0, sizeof(ref->node));
2154 ref->parent = parent;
2155 ref->node.full_backref = 1;
2158 ref->node.full_backref = 0;
2160 list_add_tail(&ref->node.list, &rec->backrefs);
2165 static struct data_backref *find_data_backref(struct extent_record *rec,
2166 u64 parent, u64 root,
2167 u64 owner, u64 offset)
2169 struct list_head *cur = rec->backrefs.next;
2170 struct extent_backref *node;
2171 struct data_backref *back;
2173 while(cur != &rec->backrefs) {
2174 node = list_entry(cur, struct extent_backref, list);
2178 back = (struct data_backref *)node;
2180 if (!node->full_backref)
2182 if (parent == back->parent)
2185 if (node->full_backref)
2187 if (back->root == root && back->owner == owner &&
2188 back->offset == offset)
2195 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2196 u64 parent, u64 root,
2197 u64 owner, u64 offset,
2200 struct data_backref *ref = malloc(sizeof(*ref));
2201 memset(&ref->node, 0, sizeof(ref->node));
2202 ref->node.is_data = 1;
2205 ref->parent = parent;
2208 ref->node.full_backref = 1;
2212 ref->offset = offset;
2213 ref->node.full_backref = 0;
2217 list_add_tail(&ref->node.list, &rec->backrefs);
2218 if (max_size > rec->max_size)
2219 rec->max_size = max_size;
2223 static int add_extent_rec(struct cache_tree *extent_cache,
2224 struct btrfs_key *parent_key,
2225 u64 start, u64 nr, u64 extent_item_refs,
2226 int is_root, int inc_ref, int set_checked,
2229 struct extent_record *rec;
2230 struct cache_extent *cache;
2233 cache = find_cache_extent(extent_cache, start, nr);
2235 rec = container_of(cache, struct extent_record, cache);
2239 rec->nr = max(nr, max_size);
2241 if (start != rec->start) {
2242 fprintf(stderr, "warning, start mismatch %llu %llu\n",
2243 (unsigned long long)rec->start,
2244 (unsigned long long)start);
2247 if (extent_item_refs) {
2248 if (rec->extent_item_refs) {
2249 fprintf(stderr, "block %llu rec "
2250 "extent_item_refs %llu, passed %llu\n",
2251 (unsigned long long)start,
2252 (unsigned long long)
2253 rec->extent_item_refs,
2254 (unsigned long long)extent_item_refs);
2256 rec->extent_item_refs = extent_item_refs;
2261 rec->content_checked = 1;
2262 rec->owner_ref_checked = 1;
2266 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2268 if (rec->max_size < max_size)
2269 rec->max_size = max_size;
2271 maybe_free_extent_rec(extent_cache, rec);
2274 rec = malloc(sizeof(*rec));
2276 rec->max_size = max_size;
2277 rec->nr = max(nr, max_size);
2278 rec->content_checked = 0;
2279 rec->owner_ref_checked = 0;
2280 INIT_LIST_HEAD(&rec->backrefs);
2292 if (extent_item_refs)
2293 rec->extent_item_refs = extent_item_refs;
2295 rec->extent_item_refs = 0;
2298 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2300 memset(&rec->parent_key, 0, sizeof(*parent_key));
2302 rec->cache.start = start;
2303 rec->cache.size = nr;
2304 ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2308 rec->content_checked = 1;
2309 rec->owner_ref_checked = 1;
2314 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2315 u64 parent, u64 root, int found_ref)
2317 struct extent_record *rec;
2318 struct tree_backref *back;
2319 struct cache_extent *cache;
2321 cache = find_cache_extent(extent_cache, bytenr, 1);
2323 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0, 0);
2324 cache = find_cache_extent(extent_cache, bytenr, 1);
2329 rec = container_of(cache, struct extent_record, cache);
2330 if (rec->start != bytenr) {
2334 back = find_tree_backref(rec, parent, root);
2336 back = alloc_tree_backref(rec, parent, root);
2339 if (back->node.found_ref) {
2340 fprintf(stderr, "Extent back ref already exists "
2341 "for %llu parent %llu root %llu \n",
2342 (unsigned long long)bytenr,
2343 (unsigned long long)parent,
2344 (unsigned long long)root);
2346 back->node.found_ref = 1;
2348 if (back->node.found_extent_tree) {
2349 fprintf(stderr, "Extent back ref already exists "
2350 "for %llu parent %llu root %llu \n",
2351 (unsigned long long)bytenr,
2352 (unsigned long long)parent,
2353 (unsigned long long)root);
2355 back->node.found_extent_tree = 1;
2360 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2361 u64 parent, u64 root, u64 owner, u64 offset,
2362 u32 num_refs, int found_ref, u64 max_size)
2364 struct extent_record *rec;
2365 struct data_backref *back;
2366 struct cache_extent *cache;
2368 cache = find_cache_extent(extent_cache, bytenr, 1);
2370 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2372 cache = find_cache_extent(extent_cache, bytenr, 1);
2377 rec = container_of(cache, struct extent_record, cache);
2378 if (rec->start != bytenr) {
2381 if (rec->max_size < max_size)
2382 rec->max_size = max_size;
2384 back = find_data_backref(rec, parent, root, owner, offset);
2386 back = alloc_data_backref(rec, parent, root, owner, offset,
2390 BUG_ON(num_refs != 1);
2391 back->node.found_ref = 1;
2392 back->found_ref += 1;
2394 if (back->node.found_extent_tree) {
2395 fprintf(stderr, "Extent back ref already exists "
2396 "for %llu parent %llu root %llu"
2397 "owner %llu offset %llu num_refs %lu\n",
2398 (unsigned long long)bytenr,
2399 (unsigned long long)parent,
2400 (unsigned long long)root,
2401 (unsigned long long)owner,
2402 (unsigned long long)offset,
2403 (unsigned long)num_refs);
2405 back->num_refs = num_refs;
2406 back->node.found_extent_tree = 1;
2411 static int add_pending(struct cache_tree *pending,
2412 struct cache_tree *seen, u64 bytenr, u32 size)
2415 ret = insert_cache_extent(seen, bytenr, size);
2418 insert_cache_extent(pending, bytenr, size);
2422 static int pick_next_pending(struct cache_tree *pending,
2423 struct cache_tree *reada,
2424 struct cache_tree *nodes,
2425 u64 last, struct block_info *bits, int bits_nr,
2428 unsigned long node_start = last;
2429 struct cache_extent *cache;
2432 cache = find_first_cache_extent(reada, 0);
2434 bits[0].start = cache->start;
2435 bits[1].size = cache->size;
2440 if (node_start > 32768)
2441 node_start -= 32768;
2443 cache = find_first_cache_extent(nodes, node_start);
2445 cache = find_first_cache_extent(nodes, 0);
2448 cache = find_first_cache_extent(pending, 0);
2453 bits[ret].start = cache->start;
2454 bits[ret].size = cache->size;
2455 cache = next_cache_extent(cache);
2457 } while (cache && ret < bits_nr);
2463 bits[ret].start = cache->start;
2464 bits[ret].size = cache->size;
2465 cache = next_cache_extent(cache);
2467 } while (cache && ret < bits_nr);
2469 if (bits_nr - ret > 8) {
2470 u64 lookup = bits[0].start + bits[0].size;
2471 struct cache_extent *next;
2472 next = find_first_cache_extent(pending, lookup);
2474 if (next->start - lookup > 32768)
2476 bits[ret].start = next->start;
2477 bits[ret].size = next->size;
2478 lookup = next->start + next->size;
2482 next = next_cache_extent(next);
2490 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2491 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2492 struct extent_buffer *leaf, int slot)
2494 struct btrfs_extent_ref_v0 *ref0;
2495 struct btrfs_key key;
2497 btrfs_item_key_to_cpu(leaf, &key, slot);
2498 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2499 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2500 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2502 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2503 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2509 static int process_extent_item(struct cache_tree *extent_cache,
2510 struct extent_buffer *eb, int slot)
2512 struct btrfs_extent_item *ei;
2513 struct btrfs_extent_inline_ref *iref;
2514 struct btrfs_extent_data_ref *dref;
2515 struct btrfs_shared_data_ref *sref;
2516 struct btrfs_key key;
2520 u32 item_size = btrfs_item_size_nr(eb, slot);
2524 btrfs_item_key_to_cpu(eb, &key, slot);
2526 if (item_size < sizeof(*ei)) {
2527 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2528 struct btrfs_extent_item_v0 *ei0;
2529 BUG_ON(item_size != sizeof(*ei0));
2530 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2531 refs = btrfs_extent_refs_v0(eb, ei0);
2535 return add_extent_rec(extent_cache, NULL, key.objectid,
2536 key.offset, refs, 0, 0, 0, key.offset);
2539 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2540 refs = btrfs_extent_refs(eb, ei);
2542 add_extent_rec(extent_cache, NULL, key.objectid, key.offset,
2543 refs, 0, 0, 0, key.offset);
2545 ptr = (unsigned long)(ei + 1);
2546 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
2547 ptr += sizeof(struct btrfs_tree_block_info);
2549 end = (unsigned long)ei + item_size;
2551 iref = (struct btrfs_extent_inline_ref *)ptr;
2552 type = btrfs_extent_inline_ref_type(eb, iref);
2553 offset = btrfs_extent_inline_ref_offset(eb, iref);
2555 case BTRFS_TREE_BLOCK_REF_KEY:
2556 add_tree_backref(extent_cache, key.objectid,
2559 case BTRFS_SHARED_BLOCK_REF_KEY:
2560 add_tree_backref(extent_cache, key.objectid,
2563 case BTRFS_EXTENT_DATA_REF_KEY:
2564 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2565 add_data_backref(extent_cache, key.objectid, 0,
2566 btrfs_extent_data_ref_root(eb, dref),
2567 btrfs_extent_data_ref_objectid(eb,
2569 btrfs_extent_data_ref_offset(eb, dref),
2570 btrfs_extent_data_ref_count(eb, dref),
2573 case BTRFS_SHARED_DATA_REF_KEY:
2574 sref = (struct btrfs_shared_data_ref *)(iref + 1);
2575 add_data_backref(extent_cache, key.objectid, offset,
2577 btrfs_shared_data_ref_count(eb, sref),
2581 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2582 key.objectid, key.type, key.offset);
2585 ptr += btrfs_extent_inline_ref_size(type);
2592 static int run_next_block(struct btrfs_root *root,
2593 struct block_info *bits,
2596 struct cache_tree *pending,
2597 struct cache_tree *seen,
2598 struct cache_tree *reada,
2599 struct cache_tree *nodes,
2600 struct cache_tree *extent_cache)
2602 struct extent_buffer *buf;
2611 struct btrfs_key key;
2612 struct cache_extent *cache;
2615 ret = pick_next_pending(pending, reada, nodes, *last, bits,
2616 bits_nr, &reada_bits);
2621 for(i = 0; i < ret; i++) {
2622 insert_cache_extent(reada, bits[i].start,
2625 /* fixme, get the parent transid */
2626 readahead_tree_block(root, bits[i].start,
2630 *last = bits[0].start;
2631 bytenr = bits[0].start;
2632 size = bits[0].size;
2634 cache = find_cache_extent(pending, bytenr, size);
2636 remove_cache_extent(pending, cache);
2639 cache = find_cache_extent(reada, bytenr, size);
2641 remove_cache_extent(reada, cache);
2644 cache = find_cache_extent(nodes, bytenr, size);
2646 remove_cache_extent(nodes, cache);
2650 /* fixme, get the real parent transid */
2651 buf = read_tree_block(root, bytenr, size, 0);
2652 if (!extent_buffer_uptodate(buf)) {
2653 record_bad_block_io(root->fs_info,
2654 extent_cache, bytenr, size);
2658 nritems = btrfs_header_nritems(buf);
2660 ret = btrfs_lookup_extent_info(NULL, root, bytenr, size, NULL, &flags);
2662 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
2664 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
2669 owner = btrfs_header_owner(buf);
2672 ret = check_block(root, extent_cache, buf, flags);
2676 if (btrfs_is_leaf(buf)) {
2677 btree_space_waste += btrfs_leaf_free_space(root, buf);
2678 for (i = 0; i < nritems; i++) {
2679 struct btrfs_file_extent_item *fi;
2680 btrfs_item_key_to_cpu(buf, &key, i);
2681 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
2682 process_extent_item(extent_cache, buf, i);
2685 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
2687 btrfs_item_size_nr(buf, i);
2690 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2693 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
2694 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2695 process_extent_ref_v0(extent_cache, buf, i);
2702 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
2703 add_tree_backref(extent_cache, key.objectid, 0,
2707 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
2708 add_tree_backref(extent_cache, key.objectid,
2712 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
2713 struct btrfs_extent_data_ref *ref;
2714 ref = btrfs_item_ptr(buf, i,
2715 struct btrfs_extent_data_ref);
2716 add_data_backref(extent_cache,
2718 btrfs_extent_data_ref_root(buf, ref),
2719 btrfs_extent_data_ref_objectid(buf,
2721 btrfs_extent_data_ref_offset(buf, ref),
2722 btrfs_extent_data_ref_count(buf, ref),
2723 0, root->sectorsize);
2726 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
2727 struct btrfs_shared_data_ref *ref;
2728 ref = btrfs_item_ptr(buf, i,
2729 struct btrfs_shared_data_ref);
2730 add_data_backref(extent_cache,
2731 key.objectid, key.offset, 0, 0, 0,
2732 btrfs_shared_data_ref_count(buf, ref),
2733 0, root->sectorsize);
2736 if (key.type != BTRFS_EXTENT_DATA_KEY)
2738 fi = btrfs_item_ptr(buf, i,
2739 struct btrfs_file_extent_item);
2740 if (btrfs_file_extent_type(buf, fi) ==
2741 BTRFS_FILE_EXTENT_INLINE)
2743 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
2746 data_bytes_allocated +=
2747 btrfs_file_extent_disk_num_bytes(buf, fi);
2748 if (data_bytes_allocated < root->sectorsize) {
2751 data_bytes_referenced +=
2752 btrfs_file_extent_num_bytes(buf, fi);
2753 ret = add_extent_rec(extent_cache, NULL,
2754 btrfs_file_extent_disk_bytenr(buf, fi),
2755 btrfs_file_extent_disk_num_bytes(buf, fi),
2757 btrfs_file_extent_disk_num_bytes(buf, fi));
2758 add_data_backref(extent_cache,
2759 btrfs_file_extent_disk_bytenr(buf, fi),
2760 parent, owner, key.objectid, key.offset -
2761 btrfs_file_extent_offset(buf, fi), 1, 1,
2762 btrfs_file_extent_disk_num_bytes(buf, fi));
2767 struct btrfs_key first_key;
2769 first_key.objectid = 0;
2772 btrfs_item_key_to_cpu(buf, &first_key, 0);
2773 level = btrfs_header_level(buf);
2774 for (i = 0; i < nritems; i++) {
2775 u64 ptr = btrfs_node_blockptr(buf, i);
2776 u32 size = btrfs_level_size(root, level - 1);
2777 btrfs_node_key_to_cpu(buf, &key, i);
2778 ret = add_extent_rec(extent_cache, &key,
2779 ptr, size, 0, 0, 1, 0, size);
2782 add_tree_backref(extent_cache, ptr, parent, owner, 1);
2785 add_pending(nodes, seen, ptr, size);
2787 add_pending(pending, seen, ptr, size);
2790 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
2791 nritems) * sizeof(struct btrfs_key_ptr);
2793 total_btree_bytes += buf->len;
2794 if (fs_root_objectid(btrfs_header_owner(buf)))
2795 total_fs_tree_bytes += buf->len;
2796 if (!found_old_backref &&
2797 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
2798 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
2799 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
2800 found_old_backref = 1;
2802 free_extent_buffer(buf);
2806 static int add_root_to_pending(struct extent_buffer *buf,
2807 struct cache_tree *extent_cache,
2808 struct cache_tree *pending,
2809 struct cache_tree *seen,
2810 struct cache_tree *nodes,
2811 struct btrfs_key *root_key)
2813 if (btrfs_header_level(buf) > 0)
2814 add_pending(nodes, seen, buf->start, buf->len);
2816 add_pending(pending, seen, buf->start, buf->len);
2817 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
2818 0, 1, 1, 0, buf->len);
2820 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
2821 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
2822 add_tree_backref(extent_cache, buf->start, buf->start,
2825 add_tree_backref(extent_cache, buf->start, 0,
2826 root_key->objectid, 1);
2830 /* as we fix the tree, we might be deleting blocks that
2831 * we're tracking for repair. This hook makes sure we
2832 * remove any backrefs for blocks as we are fixing them.
2834 static int free_extent_hook(struct btrfs_trans_handle *trans,
2835 struct btrfs_root *root,
2836 u64 bytenr, u64 num_bytes, u64 parent,
2837 u64 root_objectid, u64 owner, u64 offset,
2840 struct extent_record *rec;
2841 struct cache_extent *cache;
2843 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
2845 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
2846 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
2850 rec = container_of(cache, struct extent_record, cache);
2852 struct data_backref *back;
2853 back = find_data_backref(rec, parent, root_objectid, owner,
2857 if (back->node.found_ref) {
2858 back->found_ref -= refs_to_drop;
2860 rec->refs -= refs_to_drop;
2862 if (back->node.found_extent_tree) {
2863 back->num_refs -= refs_to_drop;
2864 if (rec->extent_item_refs)
2865 rec->extent_item_refs -= refs_to_drop;
2867 if (back->found_ref == 0)
2868 back->node.found_ref = 0;
2869 if (back->num_refs == 0)
2870 back->node.found_extent_tree = 0;
2872 if (!back->node.found_extent_tree && back->node.found_ref) {
2873 list_del(&back->node.list);
2877 struct tree_backref *back;
2878 back = find_tree_backref(rec, parent, root_objectid);
2881 if (back->node.found_ref) {
2884 back->node.found_ref = 0;
2886 if (back->node.found_extent_tree) {
2887 if (rec->extent_item_refs)
2888 rec->extent_item_refs--;
2889 back->node.found_extent_tree = 0;
2891 if (!back->node.found_extent_tree && back->node.found_ref) {
2892 list_del(&back->node.list);
2896 maybe_free_extent_rec(extent_cache, rec);
2901 static int delete_extent_records(struct btrfs_trans_handle *trans,
2902 struct btrfs_root *root,
2903 struct btrfs_path *path,
2904 u64 bytenr, u64 new_len)
2906 struct btrfs_key key;
2907 struct btrfs_key found_key;
2908 struct extent_buffer *leaf;
2913 key.objectid = bytenr;
2915 key.offset = (u64)-1;
2918 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
2925 if (path->slots[0] == 0)
2931 leaf = path->nodes[0];
2932 slot = path->slots[0];
2934 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2935 if (found_key.objectid != bytenr)
2938 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
2939 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
2940 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
2941 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
2942 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
2943 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
2944 btrfs_release_path(NULL, path);
2945 if (found_key.type == 0) {
2946 if (found_key.offset == 0)
2948 key.offset = found_key.offset - 1;
2949 key.type = found_key.type;
2951 key.type = found_key.type - 1;
2952 key.offset = (u64)-1;
2956 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
2957 found_key.objectid, found_key.type, found_key.offset);
2959 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
2962 btrfs_release_path(NULL, path);
2964 if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
2965 ret = btrfs_update_block_group(trans, root, bytenr,
2966 found_key.offset, 0, 0);
2972 btrfs_release_path(NULL, path);
2977 * for a single backref, this will allocate a new extent
2978 * and add the backref to it.
2980 static int record_extent(struct btrfs_trans_handle *trans,
2981 struct btrfs_fs_info *info,
2982 struct btrfs_path *path,
2983 struct extent_record *rec,
2984 struct extent_backref *back,
2985 int allocated, u64 flags)
2988 struct btrfs_root *extent_root = info->extent_root;
2989 struct extent_buffer *leaf;
2990 struct btrfs_key ins_key;
2991 struct btrfs_extent_item *ei;
2992 struct tree_backref *tback;
2993 struct data_backref *dback;
2994 struct btrfs_tree_block_info *bi;
2997 rec->max_size = max_t(u64, rec->max_size,
2998 info->extent_root->leafsize);
3001 u32 item_size = sizeof(*ei);
3004 item_size += sizeof(*bi);
3006 ins_key.objectid = rec->start;
3007 ins_key.offset = rec->max_size;
3008 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3010 ret = btrfs_insert_empty_item(trans, extent_root, path,
3011 &ins_key, item_size);
3015 leaf = path->nodes[0];
3016 ei = btrfs_item_ptr(leaf, path->slots[0],
3017 struct btrfs_extent_item);
3019 btrfs_set_extent_refs(leaf, ei, 0);
3020 btrfs_set_extent_generation(leaf, ei, rec->generation);
3022 if (back->is_data) {
3023 btrfs_set_extent_flags(leaf, ei,
3024 BTRFS_EXTENT_FLAG_DATA);
3026 struct btrfs_disk_key copy_key;;
3028 tback = (struct tree_backref *)back;
3029 bi = (struct btrfs_tree_block_info *)(ei + 1);
3030 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3032 memset(©_key, 0, sizeof(copy_key));
3034 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3035 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3036 btrfs_set_tree_block_key(leaf, bi, ©_key);
3038 btrfs_set_extent_flags(leaf, ei,
3039 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3042 btrfs_mark_buffer_dirty(leaf);
3043 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3044 rec->max_size, 1, 0);
3047 btrfs_release_path(NULL, path);
3050 if (back->is_data) {
3054 dback = (struct data_backref *)back;
3055 if (back->full_backref)
3056 parent = dback->parent;
3060 for (i = 0; i < dback->found_ref; i++) {
3061 /* if parent != 0, we're doing a full backref
3062 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3063 * just makes the backref allocator create a data
3066 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3067 rec->start, rec->max_size,
3071 BTRFS_FIRST_FREE_OBJECTID :
3077 fprintf(stderr, "adding new data backref"
3078 " on %llu %s %llu owner %llu"
3079 " offset %llu found %d\n",
3080 (unsigned long long)rec->start,
3081 back->full_backref ?
3083 back->full_backref ?
3084 (unsigned long long)parent :
3085 (unsigned long long)dback->root,
3086 (unsigned long long)dback->owner,
3087 (unsigned long long)dback->offset,
3092 tback = (struct tree_backref *)back;
3093 if (back->full_backref)
3094 parent = tback->parent;
3098 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3099 rec->start, rec->max_size,
3100 parent, tback->root, 0, 0);
3101 fprintf(stderr, "adding new tree backref on "
3102 "start %llu len %llu parent %llu root %llu\n",
3103 rec->start, rec->max_size, tback->parent, tback->root);
3108 btrfs_release_path(NULL, path);
3113 * when an incorrect extent item is found, this will delete
3114 * all of the existing entries for it and recreate them
3115 * based on what the tree scan found.
3117 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3118 struct btrfs_fs_info *info,
3119 struct extent_record *rec)
3122 struct btrfs_path *path;
3123 struct list_head *cur = rec->backrefs.next;
3124 struct cache_extent *cache;
3125 struct extent_backref *back;
3129 /* remember our flags for recreating the extent */
3130 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3131 rec->max_size, NULL, &flags);
3133 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3135 path = btrfs_alloc_path();
3137 /* step one, delete all the existing records */
3138 ret = delete_extent_records(trans, info->extent_root, path,
3139 rec->start, rec->max_size);
3144 /* was this block corrupt? If so, don't add references to it */
3145 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3151 /* step two, recreate all the refs we did find */
3152 while(cur != &rec->backrefs) {
3153 back = list_entry(cur, struct extent_backref, list);
3157 * if we didn't find any references, don't create a
3160 if (!back->found_ref)
3163 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3170 btrfs_free_path(path);
3174 /* right now we only prune from the extent allocation tree */
3175 static int prune_one_block(struct btrfs_trans_handle *trans,
3176 struct btrfs_fs_info *info,
3177 struct btrfs_corrupt_block *corrupt)
3180 struct btrfs_path path;
3181 struct extent_buffer *eb;
3185 int level = corrupt->level + 1;
3187 btrfs_init_path(&path);
3189 /* we want to stop at the parent to our busted block */
3190 path.lowest_level = level;
3192 ret = btrfs_search_slot(trans, info->extent_root,
3193 &corrupt->key, &path, -1, 1);
3198 eb = path.nodes[level];
3205 * hopefully the search gave us the block we want to prune,
3206 * lets try that first
3208 slot = path.slots[level];
3209 found = btrfs_node_blockptr(eb, slot);
3210 if (found == corrupt->cache.start)
3213 nritems = btrfs_header_nritems(eb);
3215 /* the search failed, lets scan this node and hope we find it */
3216 for (slot = 0; slot < nritems; slot++) {
3217 found = btrfs_node_blockptr(eb, slot);
3218 if (found == corrupt->cache.start)
3222 * we couldn't find the bad block. TODO, search all the nodes for pointers
3225 if (eb == info->extent_root->node) {
3230 btrfs_release_path(NULL, &path);
3235 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3236 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3239 btrfs_release_path(NULL, &path);
3243 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3244 struct btrfs_fs_info *info)
3246 struct cache_extent *cache;
3247 struct btrfs_corrupt_block *corrupt;
3249 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3253 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3254 prune_one_block(trans, info, corrupt);
3255 cache = next_cache_extent(cache);
3260 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3262 struct cache_extent *cache;
3263 struct btrfs_corrupt_block *corrupt;
3266 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3269 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3270 remove_cache_extent(info->corrupt_blocks, cache);
3275 static int check_block_group(struct btrfs_trans_handle *trans,
3276 struct btrfs_fs_info *info,
3277 struct map_lookup *map,
3280 struct btrfs_key key;
3281 struct btrfs_path path;
3284 key.objectid = map->ce.start;
3285 key.offset = map->ce.size;
3286 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3288 btrfs_init_path(&path);
3289 ret = btrfs_search_slot(NULL, info->extent_root,
3291 btrfs_release_path(NULL, &path);
3295 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3296 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3297 key.objectid, key.offset);
3303 static int check_block_groups(struct btrfs_trans_handle *trans,
3304 struct btrfs_fs_info *info, int *reinit)
3306 struct cache_extent *ce;
3307 struct map_lookup *map;
3308 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3310 /* this isn't quite working */
3313 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3317 map = container_of(ce, struct map_lookup, ce);
3318 check_block_group(trans, info, map, reinit);
3319 ce = next_cache_extent(ce);
3324 static int check_extent_refs(struct btrfs_trans_handle *trans,
3325 struct btrfs_root *root,
3326 struct cache_tree *extent_cache, int repair)
3328 struct extent_record *rec;
3329 struct cache_extent *cache;
3337 * if we're doing a repair, we have to make sure
3338 * we don't allocate from the problem extents.
3339 * In the worst case, this will be all the
3342 cache = find_first_cache_extent(extent_cache, 0);
3344 rec = container_of(cache, struct extent_record, cache);
3345 btrfs_pin_extent(root->fs_info,
3346 rec->start, rec->max_size);
3347 cache = next_cache_extent(cache);
3350 /* pin down all the corrupted blocks too */
3351 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3353 rec = container_of(cache, struct extent_record, cache);
3354 btrfs_pin_extent(root->fs_info,
3355 rec->start, rec->max_size);
3356 cache = next_cache_extent(cache);
3358 prune_corrupt_blocks(trans, root->fs_info);
3359 check_block_groups(trans, root->fs_info, &reinit);
3361 btrfs_read_block_groups(root->fs_info->extent_root);
3365 cache = find_first_cache_extent(extent_cache, 0);
3368 rec = container_of(cache, struct extent_record, cache);
3369 if (rec->refs != rec->extent_item_refs) {
3370 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3371 (unsigned long long)rec->start,
3372 (unsigned long long)rec->nr);
3373 fprintf(stderr, "extent item %llu, found %llu\n",
3374 (unsigned long long)rec->extent_item_refs,
3375 (unsigned long long)rec->refs);
3376 if (!fixed && repair) {
3377 ret = fixup_extent_refs(trans, root->fs_info, rec);
3385 if (all_backpointers_checked(rec, 1)) {
3386 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3387 (unsigned long long)rec->start,
3388 (unsigned long long)rec->nr);
3390 if (!fixed && repair) {
3391 ret = fixup_extent_refs(trans, root->fs_info, rec);
3399 if (!rec->owner_ref_checked) {
3400 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3401 (unsigned long long)rec->start,
3402 (unsigned long long)rec->nr);
3403 if (!fixed && repair) {
3404 ret = fixup_extent_refs(trans, root->fs_info, rec);
3412 remove_cache_extent(extent_cache, cache);
3413 free_all_extent_backrefs(rec);
3419 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3422 btrfs_fix_block_accounting(trans, root);
3425 fprintf(stderr, "repaired damaged extent references\n");
3431 static int check_extents(struct btrfs_trans_handle *trans,
3432 struct btrfs_root *root, int repair)
3434 struct cache_tree extent_cache;
3435 struct cache_tree seen;
3436 struct cache_tree pending;
3437 struct cache_tree reada;
3438 struct cache_tree nodes;
3439 struct cache_tree corrupt_blocks;
3440 struct btrfs_path path;
3441 struct btrfs_key key;
3442 struct btrfs_key found_key;
3445 struct block_info *bits;
3447 struct extent_buffer *leaf;
3449 struct btrfs_root_item ri;
3451 cache_tree_init(&extent_cache);
3452 cache_tree_init(&seen);
3453 cache_tree_init(&pending);
3454 cache_tree_init(&nodes);
3455 cache_tree_init(&reada);
3456 cache_tree_init(&corrupt_blocks);
3459 root->fs_info->fsck_extent_cache = &extent_cache;
3460 root->fs_info->free_extent_hook = free_extent_hook;
3461 root->fs_info->corrupt_blocks = &corrupt_blocks;
3465 bits = malloc(bits_nr * sizeof(struct block_info));
3471 add_root_to_pending(root->fs_info->tree_root->node,
3472 &extent_cache, &pending, &seen, &nodes,
3473 &root->fs_info->tree_root->root_key);
3475 add_root_to_pending(root->fs_info->chunk_root->node,
3476 &extent_cache, &pending, &seen, &nodes,
3477 &root->fs_info->chunk_root->root_key);
3479 btrfs_init_path(&path);
3482 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3483 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3487 leaf = path.nodes[0];
3488 slot = path.slots[0];
3489 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3490 ret = btrfs_next_leaf(root, &path);
3493 leaf = path.nodes[0];
3494 slot = path.slots[0];
3496 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3497 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3498 unsigned long offset;
3499 struct extent_buffer *buf;
3501 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3502 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3503 buf = read_tree_block(root->fs_info->tree_root,
3504 btrfs_root_bytenr(&ri),
3505 btrfs_level_size(root,
3506 btrfs_root_level(&ri)), 0);
3507 add_root_to_pending(buf, &extent_cache, &pending,
3508 &seen, &nodes, &found_key);
3509 free_extent_buffer(buf);
3513 btrfs_release_path(root, &path);
3515 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3516 &seen, &reada, &nodes, &extent_cache);
3520 ret = check_extent_refs(trans, root, &extent_cache, repair);
3523 free_corrupt_blocks(root->fs_info);
3524 root->fs_info->fsck_extent_cache = NULL;
3525 root->fs_info->free_extent_hook = NULL;
3526 root->fs_info->corrupt_blocks = NULL;
3533 static struct option long_options[] = {
3534 { "super", 1, NULL, 's' },
3535 { "repair", 0, NULL, 0 },
3536 { "init-csum-tree", 0, NULL, 0 },
3537 { "init-extent-tree", 0, NULL, 0 },
3541 const char * const cmd_check_usage[] = {
3542 "btrfs check [options] <device>",
3543 "Check an unmounted btrfs filesystem.",
3545 "-s|--super <superblock> use this superblock copy",
3546 "--repair try to repair the filesystem",
3547 "--init-csum-tree create a new CRC tree",
3548 "--init-extent-tree create a new extent tree",
3552 int cmd_check(int argc, char **argv)
3554 struct cache_tree root_cache;
3555 struct btrfs_root *root;
3556 struct btrfs_fs_info *info;
3557 struct btrfs_trans_handle *trans = NULL;
3563 int option_index = 0;
3564 int init_csum_tree = 0;
3569 c = getopt_long(argc, argv, "as:", long_options,
3574 case 'a': /* ignored */ break;
3577 bytenr = btrfs_sb_offset(num);
3578 printf("using SB copy %d, bytenr %llu\n", num,
3579 (unsigned long long)bytenr);
3583 usage(cmd_check_usage);
3585 if (option_index == 1) {
3586 printf("enabling repair mode\n");
3589 } else if (option_index == 2) {
3590 printf("Creating a new CRC tree\n");
3596 argc = argc - optind;
3599 usage(cmd_check_usage);
3602 cache_tree_init(&root_cache);
3604 if((ret = check_mounted(argv[optind])) < 0) {
3605 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
3608 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
3612 info = open_ctree_fs_info(argv[optind], bytenr, rw, 1);
3616 uuid_unparse(info->super_copy.fsid, uuidbuf);
3617 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
3619 if (!extent_buffer_uptodate(info->tree_root->node) ||
3620 !extent_buffer_uptodate(info->dev_root->node) ||
3621 !extent_buffer_uptodate(info->extent_root->node) ||
3622 !extent_buffer_uptodate(info->chunk_root->node)) {
3623 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
3627 root = info->fs_root;
3629 fprintf(stderr, "checking extents\n");
3631 trans = btrfs_start_transaction(root, 1);
3633 if (init_csum_tree) {
3634 fprintf(stderr, "Reinit crc root\n");
3635 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
3637 fprintf(stderr, "crc root initialization failed\n");
3642 ret = check_extents(trans, root, repair);
3644 fprintf(stderr, "Errors found in extent allocation tree\n");
3646 fprintf(stderr, "checking fs roots\n");
3647 ret = check_fs_roots(root, &root_cache);
3651 fprintf(stderr, "checking root refs\n");
3652 ret = check_root_refs(root, &root_cache);
3654 free_root_recs(&root_cache);
3656 ret = btrfs_commit_transaction(trans, root);
3662 if (found_old_backref) { /*
3663 * there was a disk format change when mixed
3664 * backref was in testing tree. The old format
3665 * existed about one week.
3667 printf("\n * Found old mixed backref format. "
3668 "The old format is not supported! *"
3669 "\n * Please mount the FS in readonly mode, "
3670 "backup data and re-format the FS. *\n\n");
3673 printf("found %llu bytes used err is %d\n",
3674 (unsigned long long)bytes_used, ret);
3675 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
3676 printf("total tree bytes: %llu\n",
3677 (unsigned long long)total_btree_bytes);
3678 printf("total fs tree bytes: %llu\n",
3679 (unsigned long long)total_fs_tree_bytes);
3680 printf("btree space waste bytes: %llu\n",
3681 (unsigned long long)btree_space_waste);
3682 printf("file data blocks allocated: %llu\n referenced %llu\n",
3683 (unsigned long long)data_bytes_allocated,
3684 (unsigned long long)data_bytes_referenced);
3685 printf("%s\n", BTRFS_BUILD_VERSION);