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"
41 #include "free-space-cache.h"
43 static u64 bytes_used = 0;
44 static u64 total_csum_bytes = 0;
45 static u64 total_btree_bytes = 0;
46 static u64 total_fs_tree_bytes = 0;
47 static u64 total_extent_tree_bytes = 0;
48 static u64 btree_space_waste = 0;
49 static u64 data_bytes_allocated = 0;
50 static u64 data_bytes_referenced = 0;
51 static int found_old_backref = 0;
53 struct extent_backref {
54 struct list_head list;
55 unsigned int is_data:1;
56 unsigned int found_extent_tree:1;
57 unsigned int full_backref:1;
58 unsigned int found_ref:1;
62 struct extent_backref node;
75 struct extent_backref node;
82 struct extent_record {
83 struct list_head backrefs;
84 struct cache_extent cache;
85 struct btrfs_disk_key parent_key;
94 unsigned int content_checked:1;
95 unsigned int owner_ref_checked:1;
96 unsigned int is_root:1;
97 unsigned int metadata:1;
100 struct inode_backref {
101 struct list_head list;
102 unsigned int found_dir_item:1;
103 unsigned int found_dir_index:1;
104 unsigned int found_inode_ref:1;
105 unsigned int filetype:8;
107 unsigned int ref_type;
114 #define REF_ERR_NO_DIR_ITEM (1 << 0)
115 #define REF_ERR_NO_DIR_INDEX (1 << 1)
116 #define REF_ERR_NO_INODE_REF (1 << 2)
117 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
118 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
119 #define REF_ERR_DUP_INODE_REF (1 << 5)
120 #define REF_ERR_INDEX_UNMATCH (1 << 6)
121 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
122 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
123 #define REF_ERR_NO_ROOT_REF (1 << 9)
124 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
125 #define REF_ERR_DUP_ROOT_REF (1 << 11)
126 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
128 struct inode_record {
129 struct list_head backrefs;
130 unsigned int checked:1;
131 unsigned int merging:1;
132 unsigned int found_inode_item:1;
133 unsigned int found_dir_item:1;
134 unsigned int found_file_extent:1;
135 unsigned int found_csum_item:1;
136 unsigned int some_csum_missing:1;
137 unsigned int nodatasum:1;
150 u64 first_extent_gap;
155 #define I_ERR_NO_INODE_ITEM (1 << 0)
156 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
157 #define I_ERR_DUP_INODE_ITEM (1 << 2)
158 #define I_ERR_DUP_DIR_INDEX (1 << 3)
159 #define I_ERR_ODD_DIR_ITEM (1 << 4)
160 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
161 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
162 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
163 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
164 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
165 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
166 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
167 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
168 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
170 struct root_backref {
171 struct list_head list;
172 unsigned int found_dir_item:1;
173 unsigned int found_dir_index:1;
174 unsigned int found_back_ref:1;
175 unsigned int found_forward_ref:1;
176 unsigned int reachable:1;
186 struct list_head backrefs;
187 struct cache_extent cache;
188 unsigned int found_root_item:1;
194 struct cache_extent cache;
199 struct cache_extent cache;
200 struct cache_tree root_cache;
201 struct cache_tree inode_cache;
202 struct inode_record *current;
211 struct walk_control {
212 struct cache_tree shared;
213 struct shared_node *nodes[BTRFS_MAX_LEVEL];
218 static u8 imode_to_type(u32 imode)
221 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
222 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
223 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
224 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
225 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
226 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
227 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
228 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
231 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
235 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
237 struct inode_record *rec;
238 struct inode_backref *backref;
239 struct inode_backref *orig;
242 rec = malloc(sizeof(*rec));
243 memcpy(rec, orig_rec, sizeof(*rec));
245 INIT_LIST_HEAD(&rec->backrefs);
247 list_for_each_entry(orig, &orig_rec->backrefs, list) {
248 size = sizeof(*orig) + orig->namelen + 1;
249 backref = malloc(size);
250 memcpy(backref, orig, size);
251 list_add_tail(&backref->list, &rec->backrefs);
256 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
259 struct ptr_node *node;
260 struct cache_extent *cache;
261 struct inode_record *rec = NULL;
264 cache = find_cache_extent(inode_cache, ino, 1);
266 node = container_of(cache, struct ptr_node, cache);
268 if (mod && rec->refs > 1) {
269 node->data = clone_inode_rec(rec);
274 rec = calloc(1, sizeof(*rec));
276 rec->extent_start = (u64)-1;
277 rec->first_extent_gap = (u64)-1;
279 INIT_LIST_HEAD(&rec->backrefs);
281 node = malloc(sizeof(*node));
282 node->cache.start = ino;
283 node->cache.size = 1;
286 if (ino == BTRFS_FREE_INO_OBJECTID)
289 ret = insert_existing_cache_extent(inode_cache, &node->cache);
295 static void free_inode_rec(struct inode_record *rec)
297 struct inode_backref *backref;
302 while (!list_empty(&rec->backrefs)) {
303 backref = list_entry(rec->backrefs.next,
304 struct inode_backref, list);
305 list_del(&backref->list);
311 static int can_free_inode_rec(struct inode_record *rec)
313 if (!rec->errors && rec->checked && rec->found_inode_item &&
314 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
319 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
320 struct inode_record *rec)
322 struct cache_extent *cache;
323 struct inode_backref *tmp, *backref;
324 struct ptr_node *node;
325 unsigned char filetype;
327 if (!rec->found_inode_item)
330 filetype = imode_to_type(rec->imode);
331 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
332 if (backref->found_dir_item && backref->found_dir_index) {
333 if (backref->filetype != filetype)
334 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
335 if (!backref->errors && backref->found_inode_ref) {
336 list_del(&backref->list);
342 if (!rec->checked || rec->merging)
345 if (S_ISDIR(rec->imode)) {
346 if (rec->found_size != rec->isize)
347 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
348 if (rec->found_file_extent)
349 rec->errors |= I_ERR_ODD_FILE_EXTENT;
350 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
351 if (rec->found_dir_item)
352 rec->errors |= I_ERR_ODD_DIR_ITEM;
353 if (rec->found_size != rec->nbytes)
354 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
355 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
356 rec->first_extent_gap = 0;
357 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
358 rec->first_extent_gap < rec->isize))
359 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
362 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
363 if (rec->found_csum_item && rec->nodatasum)
364 rec->errors |= I_ERR_ODD_CSUM_ITEM;
365 if (rec->some_csum_missing && !rec->nodatasum)
366 rec->errors |= I_ERR_SOME_CSUM_MISSING;
369 BUG_ON(rec->refs != 1);
370 if (can_free_inode_rec(rec)) {
371 cache = find_cache_extent(inode_cache, rec->ino, 1);
372 node = container_of(cache, struct ptr_node, cache);
373 BUG_ON(node->data != rec);
374 remove_cache_extent(inode_cache, &node->cache);
380 static int check_orphan_item(struct btrfs_root *root, u64 ino)
382 struct btrfs_path path;
383 struct btrfs_key key;
386 key.objectid = BTRFS_ORPHAN_OBJECTID;
387 key.type = BTRFS_ORPHAN_ITEM_KEY;
390 btrfs_init_path(&path);
391 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
392 btrfs_release_path(root, &path);
398 static int process_inode_item(struct extent_buffer *eb,
399 int slot, struct btrfs_key *key,
400 struct shared_node *active_node)
402 struct inode_record *rec;
403 struct btrfs_inode_item *item;
405 rec = active_node->current;
406 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
407 if (rec->found_inode_item) {
408 rec->errors |= I_ERR_DUP_INODE_ITEM;
411 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
412 rec->nlink = btrfs_inode_nlink(eb, item);
413 rec->isize = btrfs_inode_size(eb, item);
414 rec->nbytes = btrfs_inode_nbytes(eb, item);
415 rec->imode = btrfs_inode_mode(eb, item);
416 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
418 rec->found_inode_item = 1;
420 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
421 maybe_free_inode_rec(&active_node->inode_cache, rec);
425 static struct inode_backref *get_inode_backref(struct inode_record *rec,
427 int namelen, u64 dir)
429 struct inode_backref *backref;
431 list_for_each_entry(backref, &rec->backrefs, list) {
432 if (backref->dir != dir || backref->namelen != namelen)
434 if (memcmp(name, backref->name, namelen))
439 backref = malloc(sizeof(*backref) + namelen + 1);
440 memset(backref, 0, sizeof(*backref));
442 backref->namelen = namelen;
443 memcpy(backref->name, name, namelen);
444 backref->name[namelen] = '\0';
445 list_add_tail(&backref->list, &rec->backrefs);
449 static int add_inode_backref(struct cache_tree *inode_cache,
450 u64 ino, u64 dir, u64 index,
451 const char *name, int namelen,
452 int filetype, int itemtype, int errors)
454 struct inode_record *rec;
455 struct inode_backref *backref;
457 rec = get_inode_rec(inode_cache, ino, 1);
458 backref = get_inode_backref(rec, name, namelen, dir);
460 backref->errors |= errors;
461 if (itemtype == BTRFS_DIR_INDEX_KEY) {
462 if (backref->found_dir_index)
463 backref->errors |= REF_ERR_DUP_DIR_INDEX;
464 if (backref->found_inode_ref && backref->index != index)
465 backref->errors |= REF_ERR_INDEX_UNMATCH;
466 if (backref->found_dir_item && backref->filetype != filetype)
467 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
469 backref->index = index;
470 backref->filetype = filetype;
471 backref->found_dir_index = 1;
472 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
474 if (backref->found_dir_item)
475 backref->errors |= REF_ERR_DUP_DIR_ITEM;
476 if (backref->found_dir_index && backref->filetype != filetype)
477 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
479 backref->filetype = filetype;
480 backref->found_dir_item = 1;
481 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
482 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
483 if (backref->found_inode_ref)
484 backref->errors |= REF_ERR_DUP_INODE_REF;
485 if (backref->found_dir_index && backref->index != index)
486 backref->errors |= REF_ERR_INDEX_UNMATCH;
488 backref->ref_type = itemtype;
489 backref->index = index;
490 backref->found_inode_ref = 1;
495 maybe_free_inode_rec(inode_cache, rec);
499 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
500 struct cache_tree *dst_cache)
502 struct inode_backref *backref;
506 list_for_each_entry(backref, &src->backrefs, list) {
507 if (backref->found_dir_index) {
508 add_inode_backref(dst_cache, dst->ino, backref->dir,
509 backref->index, backref->name,
510 backref->namelen, backref->filetype,
511 BTRFS_DIR_INDEX_KEY, backref->errors);
513 if (backref->found_dir_item) {
515 add_inode_backref(dst_cache, dst->ino,
516 backref->dir, 0, backref->name,
517 backref->namelen, backref->filetype,
518 BTRFS_DIR_ITEM_KEY, backref->errors);
520 if (backref->found_inode_ref) {
521 add_inode_backref(dst_cache, dst->ino,
522 backref->dir, backref->index,
523 backref->name, backref->namelen, 0,
524 backref->ref_type, backref->errors);
528 if (src->found_dir_item)
529 dst->found_dir_item = 1;
530 if (src->found_file_extent)
531 dst->found_file_extent = 1;
532 if (src->found_csum_item)
533 dst->found_csum_item = 1;
534 if (src->some_csum_missing)
535 dst->some_csum_missing = 1;
536 if (dst->first_extent_gap > src->first_extent_gap)
537 dst->first_extent_gap = src->first_extent_gap;
539 BUG_ON(src->found_link < dir_count);
540 dst->found_link += src->found_link - dir_count;
541 dst->found_size += src->found_size;
542 if (src->extent_start != (u64)-1) {
543 if (dst->extent_start == (u64)-1) {
544 dst->extent_start = src->extent_start;
545 dst->extent_end = src->extent_end;
547 if (dst->extent_end > src->extent_start)
548 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
549 else if (dst->extent_end < src->extent_start &&
550 dst->extent_end < dst->first_extent_gap)
551 dst->first_extent_gap = dst->extent_end;
552 if (dst->extent_end < src->extent_end)
553 dst->extent_end = src->extent_end;
557 dst->errors |= src->errors;
558 if (src->found_inode_item) {
559 if (!dst->found_inode_item) {
560 dst->nlink = src->nlink;
561 dst->isize = src->isize;
562 dst->nbytes = src->nbytes;
563 dst->imode = src->imode;
564 dst->nodatasum = src->nodatasum;
565 dst->found_inode_item = 1;
567 dst->errors |= I_ERR_DUP_INODE_ITEM;
575 static int splice_shared_node(struct shared_node *src_node,
576 struct shared_node *dst_node)
578 struct cache_extent *cache;
579 struct ptr_node *node, *ins;
580 struct cache_tree *src, *dst;
581 struct inode_record *rec, *conflict;
586 if (--src_node->refs == 0)
588 if (src_node->current)
589 current_ino = src_node->current->ino;
591 src = &src_node->root_cache;
592 dst = &dst_node->root_cache;
594 cache = find_first_cache_extent(src, 0);
596 node = container_of(cache, struct ptr_node, cache);
598 cache = next_cache_extent(cache);
601 remove_cache_extent(src, &node->cache);
604 ins = malloc(sizeof(*ins));
605 ins->cache.start = node->cache.start;
606 ins->cache.size = node->cache.size;
610 ret = insert_existing_cache_extent(dst, &ins->cache);
611 if (ret == -EEXIST) {
612 conflict = get_inode_rec(dst, rec->ino, 1);
613 merge_inode_recs(rec, conflict, dst);
615 conflict->checked = 1;
616 if (dst_node->current == conflict)
617 dst_node->current = NULL;
619 maybe_free_inode_rec(dst, conflict);
627 if (src == &src_node->root_cache) {
628 src = &src_node->inode_cache;
629 dst = &dst_node->inode_cache;
633 if (current_ino > 0 && (!dst_node->current ||
634 current_ino > dst_node->current->ino)) {
635 if (dst_node->current) {
636 dst_node->current->checked = 1;
637 maybe_free_inode_rec(dst, dst_node->current);
639 dst_node->current = get_inode_rec(dst, current_ino, 1);
644 static void free_inode_recs(struct cache_tree *inode_cache)
646 struct cache_extent *cache;
647 struct ptr_node *node;
648 struct inode_record *rec;
651 cache = find_first_cache_extent(inode_cache, 0);
654 node = container_of(cache, struct ptr_node, cache);
656 remove_cache_extent(inode_cache, &node->cache);
662 static struct shared_node *find_shared_node(struct cache_tree *shared,
665 struct cache_extent *cache;
666 struct shared_node *node;
668 cache = find_cache_extent(shared, bytenr, 1);
670 node = container_of(cache, struct shared_node, cache);
676 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
679 struct shared_node *node;
681 node = calloc(1, sizeof(*node));
682 node->cache.start = bytenr;
683 node->cache.size = 1;
684 cache_tree_init(&node->root_cache);
685 cache_tree_init(&node->inode_cache);
688 ret = insert_existing_cache_extent(shared, &node->cache);
693 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
694 struct walk_control *wc, int level)
696 struct shared_node *node;
697 struct shared_node *dest;
699 if (level == wc->active_node)
702 BUG_ON(wc->active_node <= level);
703 node = find_shared_node(&wc->shared, bytenr);
705 add_shared_node(&wc->shared, bytenr, refs);
706 node = find_shared_node(&wc->shared, bytenr);
707 wc->nodes[level] = node;
708 wc->active_node = level;
712 if (wc->root_level == wc->active_node &&
713 btrfs_root_refs(&root->root_item) == 0) {
714 if (--node->refs == 0) {
715 free_inode_recs(&node->root_cache);
716 free_inode_recs(&node->inode_cache);
717 remove_cache_extent(&wc->shared, &node->cache);
723 dest = wc->nodes[wc->active_node];
724 splice_shared_node(node, dest);
725 if (node->refs == 0) {
726 remove_cache_extent(&wc->shared, &node->cache);
732 static int leave_shared_node(struct btrfs_root *root,
733 struct walk_control *wc, int level)
735 struct shared_node *node;
736 struct shared_node *dest;
739 if (level == wc->root_level)
742 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
746 BUG_ON(i >= BTRFS_MAX_LEVEL);
748 node = wc->nodes[wc->active_node];
749 wc->nodes[wc->active_node] = NULL;
752 dest = wc->nodes[wc->active_node];
753 if (wc->active_node < wc->root_level ||
754 btrfs_root_refs(&root->root_item) > 0) {
755 BUG_ON(node->refs <= 1);
756 splice_shared_node(node, dest);
758 BUG_ON(node->refs < 2);
764 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
767 struct btrfs_path path;
768 struct btrfs_key key;
769 struct extent_buffer *leaf;
773 btrfs_init_path(&path);
775 key.objectid = parent_root_id;
776 key.type = BTRFS_ROOT_REF_KEY;
777 key.offset = child_root_id;
778 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
781 btrfs_release_path(root, &path);
785 key.objectid = child_root_id;
786 key.type = BTRFS_ROOT_BACKREF_KEY;
788 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
793 leaf = path.nodes[0];
794 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
795 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
800 leaf = path.nodes[0];
803 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
804 if (key.objectid != child_root_id ||
805 key.type != BTRFS_ROOT_BACKREF_KEY)
810 if (key.offset == parent_root_id) {
811 btrfs_release_path(root, &path);
818 btrfs_release_path(root, &path);
819 return has_parent? 0 : -1;
822 static int process_dir_item(struct btrfs_root *root,
823 struct extent_buffer *eb,
824 int slot, struct btrfs_key *key,
825 struct shared_node *active_node)
835 struct btrfs_dir_item *di;
836 struct inode_record *rec;
837 struct cache_tree *root_cache;
838 struct cache_tree *inode_cache;
839 struct btrfs_key location;
840 char namebuf[BTRFS_NAME_LEN];
842 root_cache = &active_node->root_cache;
843 inode_cache = &active_node->inode_cache;
844 rec = active_node->current;
845 rec->found_dir_item = 1;
847 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
848 total = btrfs_item_size_nr(eb, slot);
849 while (cur < total) {
851 btrfs_dir_item_key_to_cpu(eb, di, &location);
852 name_len = btrfs_dir_name_len(eb, di);
853 data_len = btrfs_dir_data_len(eb, di);
854 filetype = btrfs_dir_type(eb, di);
856 rec->found_size += name_len;
857 if (name_len <= BTRFS_NAME_LEN) {
861 len = BTRFS_NAME_LEN;
862 error = REF_ERR_NAME_TOO_LONG;
864 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
866 if (location.type == BTRFS_INODE_ITEM_KEY) {
867 add_inode_backref(inode_cache, location.objectid,
868 key->objectid, key->offset, namebuf,
869 len, filetype, key->type, error);
870 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
871 add_inode_backref(root_cache, location.objectid,
872 key->objectid, key->offset,
873 namebuf, len, filetype,
876 fprintf(stderr, "warning line %d\n", __LINE__);
879 len = sizeof(*di) + name_len + data_len;
880 di = (struct btrfs_dir_item *)((char *)di + len);
883 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
884 rec->errors |= I_ERR_DUP_DIR_INDEX;
889 static int process_inode_ref(struct extent_buffer *eb,
890 int slot, struct btrfs_key *key,
891 struct shared_node *active_node)
899 struct cache_tree *inode_cache;
900 struct btrfs_inode_ref *ref;
901 char namebuf[BTRFS_NAME_LEN];
903 inode_cache = &active_node->inode_cache;
905 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
906 total = btrfs_item_size_nr(eb, slot);
907 while (cur < total) {
908 name_len = btrfs_inode_ref_name_len(eb, ref);
909 index = btrfs_inode_ref_index(eb, ref);
910 if (name_len <= BTRFS_NAME_LEN) {
914 len = BTRFS_NAME_LEN;
915 error = REF_ERR_NAME_TOO_LONG;
917 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
918 add_inode_backref(inode_cache, key->objectid, key->offset,
919 index, namebuf, len, 0, key->type, error);
921 len = sizeof(*ref) + name_len;
922 ref = (struct btrfs_inode_ref *)((char *)ref + len);
928 static int process_inode_extref(struct extent_buffer *eb,
929 int slot, struct btrfs_key *key,
930 struct shared_node *active_node)
939 struct cache_tree *inode_cache;
940 struct btrfs_inode_extref *extref;
941 char namebuf[BTRFS_NAME_LEN];
943 inode_cache = &active_node->inode_cache;
945 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
946 total = btrfs_item_size_nr(eb, slot);
947 while (cur < total) {
948 name_len = btrfs_inode_extref_name_len(eb, extref);
949 index = btrfs_inode_extref_index(eb, extref);
950 parent = btrfs_inode_extref_parent(eb, extref);
951 if (name_len <= BTRFS_NAME_LEN) {
955 len = BTRFS_NAME_LEN;
956 error = REF_ERR_NAME_TOO_LONG;
958 read_extent_buffer(eb, namebuf,
959 (unsigned long)(extref + 1), len);
960 add_inode_backref(inode_cache, key->objectid, parent,
961 index, namebuf, len, 0, key->type, error);
963 len = sizeof(*extref) + name_len;
964 extref = (struct btrfs_inode_extref *)((char *)extref + len);
971 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
973 struct btrfs_key key;
974 struct btrfs_path path;
975 struct extent_buffer *leaf;
980 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
982 btrfs_init_path(&path);
984 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
986 key.type = BTRFS_EXTENT_CSUM_KEY;
988 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
991 if (ret > 0 && path.slots[0] > 0) {
992 leaf = path.nodes[0];
993 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
994 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
995 key.type == BTRFS_EXTENT_CSUM_KEY)
1000 leaf = path.nodes[0];
1001 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1002 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1006 leaf = path.nodes[0];
1009 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1010 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1011 key.type != BTRFS_EXTENT_CSUM_KEY)
1014 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1015 if (key.offset >= start + len)
1018 if (key.offset > start)
1021 size = btrfs_item_size_nr(leaf, path.slots[0]);
1022 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1023 if (csum_end > start) {
1024 size = min(csum_end - start, len);
1032 btrfs_release_path(root->fs_info->csum_root, &path);
1036 static int process_file_extent(struct btrfs_root *root,
1037 struct extent_buffer *eb,
1038 int slot, struct btrfs_key *key,
1039 struct shared_node *active_node)
1041 struct inode_record *rec;
1042 struct btrfs_file_extent_item *fi;
1044 u64 disk_bytenr = 0;
1045 u64 extent_offset = 0;
1046 u64 mask = root->sectorsize - 1;
1049 rec = active_node->current;
1050 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1051 rec->found_file_extent = 1;
1053 if (rec->extent_start == (u64)-1) {
1054 rec->extent_start = key->offset;
1055 rec->extent_end = key->offset;
1058 if (rec->extent_end > key->offset)
1059 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1060 else if (rec->extent_end < key->offset &&
1061 rec->extent_end < rec->first_extent_gap)
1062 rec->first_extent_gap = rec->extent_end;
1064 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1065 extent_type = btrfs_file_extent_type(eb, fi);
1067 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1068 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1070 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1071 rec->found_size += num_bytes;
1072 num_bytes = (num_bytes + mask) & ~mask;
1073 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1074 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1075 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1076 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1077 extent_offset = btrfs_file_extent_offset(eb, fi);
1078 if (num_bytes == 0 || (num_bytes & mask))
1079 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1080 if (num_bytes + extent_offset >
1081 btrfs_file_extent_ram_bytes(eb, fi))
1082 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1083 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1084 (btrfs_file_extent_compression(eb, fi) ||
1085 btrfs_file_extent_encryption(eb, fi) ||
1086 btrfs_file_extent_other_encoding(eb, fi)))
1087 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1088 if (disk_bytenr > 0)
1089 rec->found_size += num_bytes;
1091 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1093 rec->extent_end = key->offset + num_bytes;
1095 if (disk_bytenr > 0) {
1097 if (btrfs_file_extent_compression(eb, fi))
1098 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1100 disk_bytenr += extent_offset;
1102 found = count_csum_range(root, disk_bytenr, num_bytes);
1103 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1105 rec->found_csum_item = 1;
1106 if (found < num_bytes)
1107 rec->some_csum_missing = 1;
1108 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1110 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1116 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1117 struct walk_control *wc)
1119 struct btrfs_key key;
1123 struct cache_tree *inode_cache;
1124 struct shared_node *active_node;
1126 if (wc->root_level == wc->active_node &&
1127 btrfs_root_refs(&root->root_item) == 0)
1130 active_node = wc->nodes[wc->active_node];
1131 inode_cache = &active_node->inode_cache;
1132 nritems = btrfs_header_nritems(eb);
1133 for (i = 0; i < nritems; i++) {
1134 btrfs_item_key_to_cpu(eb, &key, i);
1136 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1139 if (active_node->current == NULL ||
1140 active_node->current->ino < key.objectid) {
1141 if (active_node->current) {
1142 active_node->current->checked = 1;
1143 maybe_free_inode_rec(inode_cache,
1144 active_node->current);
1146 active_node->current = get_inode_rec(inode_cache,
1150 case BTRFS_DIR_ITEM_KEY:
1151 case BTRFS_DIR_INDEX_KEY:
1152 ret = process_dir_item(root, eb, i, &key, active_node);
1154 case BTRFS_INODE_REF_KEY:
1155 ret = process_inode_ref(eb, i, &key, active_node);
1157 case BTRFS_INODE_EXTREF_KEY:
1158 ret = process_inode_extref(eb, i, &key, active_node);
1160 case BTRFS_INODE_ITEM_KEY:
1161 ret = process_inode_item(eb, i, &key, active_node);
1163 case BTRFS_EXTENT_DATA_KEY:
1164 ret = process_file_extent(root, eb, i, &key,
1174 static void reada_walk_down(struct btrfs_root *root,
1175 struct extent_buffer *node, int slot)
1185 level = btrfs_header_level(node);
1189 nritems = btrfs_header_nritems(node);
1190 blocksize = btrfs_level_size(root, level - 1);
1191 for (i = slot; i < nritems; i++) {
1192 bytenr = btrfs_node_blockptr(node, i);
1193 ptr_gen = btrfs_node_ptr_generation(node, i);
1194 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1200 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1201 struct walk_control *wc, int *level)
1205 struct extent_buffer *next;
1206 struct extent_buffer *cur;
1211 WARN_ON(*level < 0);
1212 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1213 ret = btrfs_lookup_extent_info(NULL, root,
1214 path->nodes[*level]->start,
1215 *level, 1, &refs, NULL);
1220 ret = enter_shared_node(root, path->nodes[*level]->start,
1226 while (*level >= 0) {
1227 WARN_ON(*level < 0);
1228 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1229 cur = path->nodes[*level];
1231 if (btrfs_header_level(cur) != *level)
1234 if (path->slots[*level] >= btrfs_header_nritems(cur))
1237 ret = process_one_leaf(root, cur, wc);
1240 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1241 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1242 blocksize = btrfs_level_size(root, *level - 1);
1243 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1249 ret = enter_shared_node(root, bytenr, refs,
1252 path->slots[*level]++;
1257 next = btrfs_find_tree_block(root, bytenr, blocksize);
1258 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1259 free_extent_buffer(next);
1260 reada_walk_down(root, cur, path->slots[*level]);
1261 next = read_tree_block(root, bytenr, blocksize,
1265 *level = *level - 1;
1266 free_extent_buffer(path->nodes[*level]);
1267 path->nodes[*level] = next;
1268 path->slots[*level] = 0;
1271 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1275 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1276 struct walk_control *wc, int *level)
1279 struct extent_buffer *leaf;
1281 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1282 leaf = path->nodes[i];
1283 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1288 free_extent_buffer(path->nodes[*level]);
1289 path->nodes[*level] = NULL;
1290 BUG_ON(*level > wc->active_node);
1291 if (*level == wc->active_node)
1292 leave_shared_node(root, wc, *level);
1299 static int check_root_dir(struct inode_record *rec)
1301 struct inode_backref *backref;
1304 if (!rec->found_inode_item || rec->errors)
1306 if (rec->nlink != 1 || rec->found_link != 0)
1308 if (list_empty(&rec->backrefs))
1310 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1311 if (!backref->found_inode_ref)
1313 if (backref->index != 0 || backref->namelen != 2 ||
1314 memcmp(backref->name, "..", 2))
1316 if (backref->found_dir_index || backref->found_dir_item)
1323 static int check_inode_recs(struct btrfs_root *root,
1324 struct cache_tree *inode_cache)
1326 struct cache_extent *cache;
1327 struct ptr_node *node;
1328 struct inode_record *rec;
1329 struct inode_backref *backref;
1332 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1334 if (btrfs_root_refs(&root->root_item) == 0) {
1335 if (!cache_tree_empty(inode_cache))
1336 fprintf(stderr, "warning line %d\n", __LINE__);
1340 rec = get_inode_rec(inode_cache, root_dirid, 0);
1342 ret = check_root_dir(rec);
1344 fprintf(stderr, "root %llu root dir %llu error\n",
1345 (unsigned long long)root->root_key.objectid,
1346 (unsigned long long)root_dirid);
1350 fprintf(stderr, "root %llu root dir %llu not found\n",
1351 (unsigned long long)root->root_key.objectid,
1352 (unsigned long long)root_dirid);
1356 cache = find_first_cache_extent(inode_cache, 0);
1359 node = container_of(cache, struct ptr_node, cache);
1361 remove_cache_extent(inode_cache, &node->cache);
1363 if (rec->ino == root_dirid ||
1364 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1365 free_inode_rec(rec);
1369 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1370 ret = check_orphan_item(root, rec->ino);
1372 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1373 if (can_free_inode_rec(rec)) {
1374 free_inode_rec(rec);
1380 if (!rec->found_inode_item)
1381 rec->errors |= I_ERR_NO_INODE_ITEM;
1382 if (rec->found_link != rec->nlink)
1383 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1384 fprintf(stderr, "root %llu inode %llu errors %x\n",
1385 (unsigned long long) root->root_key.objectid,
1386 (unsigned long long) rec->ino, rec->errors);
1387 list_for_each_entry(backref, &rec->backrefs, list) {
1388 if (!backref->found_dir_item)
1389 backref->errors |= REF_ERR_NO_DIR_ITEM;
1390 if (!backref->found_dir_index)
1391 backref->errors |= REF_ERR_NO_DIR_INDEX;
1392 if (!backref->found_inode_ref)
1393 backref->errors |= REF_ERR_NO_INODE_REF;
1394 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1395 " namelen %u name %s filetype %d error %x\n",
1396 (unsigned long long)backref->dir,
1397 (unsigned long long)backref->index,
1398 backref->namelen, backref->name,
1399 backref->filetype, backref->errors);
1401 free_inode_rec(rec);
1403 return (error > 0) ? -1 : 0;
1406 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1409 struct cache_extent *cache;
1410 struct root_record *rec = NULL;
1413 cache = find_cache_extent(root_cache, objectid, 1);
1415 rec = container_of(cache, struct root_record, cache);
1417 rec = calloc(1, sizeof(*rec));
1418 rec->objectid = objectid;
1419 INIT_LIST_HEAD(&rec->backrefs);
1420 rec->cache.start = objectid;
1421 rec->cache.size = 1;
1423 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1429 static struct root_backref *get_root_backref(struct root_record *rec,
1430 u64 ref_root, u64 dir, u64 index,
1431 const char *name, int namelen)
1433 struct root_backref *backref;
1435 list_for_each_entry(backref, &rec->backrefs, list) {
1436 if (backref->ref_root != ref_root || backref->dir != dir ||
1437 backref->namelen != namelen)
1439 if (memcmp(name, backref->name, namelen))
1444 backref = malloc(sizeof(*backref) + namelen + 1);
1445 memset(backref, 0, sizeof(*backref));
1446 backref->ref_root = ref_root;
1448 backref->index = index;
1449 backref->namelen = namelen;
1450 memcpy(backref->name, name, namelen);
1451 backref->name[namelen] = '\0';
1452 list_add_tail(&backref->list, &rec->backrefs);
1456 static void free_root_recs(struct cache_tree *root_cache)
1458 struct cache_extent *cache;
1459 struct root_record *rec;
1460 struct root_backref *backref;
1463 cache = find_first_cache_extent(root_cache, 0);
1466 rec = container_of(cache, struct root_record, cache);
1467 remove_cache_extent(root_cache, &rec->cache);
1469 while (!list_empty(&rec->backrefs)) {
1470 backref = list_entry(rec->backrefs.next,
1471 struct root_backref, list);
1472 list_del(&backref->list);
1479 static int add_root_backref(struct cache_tree *root_cache,
1480 u64 root_id, u64 ref_root, u64 dir, u64 index,
1481 const char *name, int namelen,
1482 int item_type, int errors)
1484 struct root_record *rec;
1485 struct root_backref *backref;
1487 rec = get_root_rec(root_cache, root_id);
1488 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1490 backref->errors |= errors;
1492 if (item_type != BTRFS_DIR_ITEM_KEY) {
1493 if (backref->found_dir_index || backref->found_back_ref ||
1494 backref->found_forward_ref) {
1495 if (backref->index != index)
1496 backref->errors |= REF_ERR_INDEX_UNMATCH;
1498 backref->index = index;
1502 if (item_type == BTRFS_DIR_ITEM_KEY) {
1503 backref->found_dir_item = 1;
1504 backref->reachable = 1;
1506 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1507 backref->found_dir_index = 1;
1508 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1509 if (backref->found_forward_ref)
1510 backref->errors |= REF_ERR_DUP_ROOT_REF;
1511 backref->found_forward_ref = 1;
1512 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1513 if (backref->found_back_ref)
1514 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1515 backref->found_back_ref = 1;
1523 static int merge_root_recs(struct btrfs_root *root,
1524 struct cache_tree *src_cache,
1525 struct cache_tree *dst_cache)
1527 struct cache_extent *cache;
1528 struct ptr_node *node;
1529 struct inode_record *rec;
1530 struct inode_backref *backref;
1532 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1533 free_inode_recs(src_cache);
1538 cache = find_first_cache_extent(src_cache, 0);
1541 node = container_of(cache, struct ptr_node, cache);
1543 remove_cache_extent(src_cache, &node->cache);
1546 if (!is_child_root(root, root->objectid, rec->ino))
1549 list_for_each_entry(backref, &rec->backrefs, list) {
1550 BUG_ON(backref->found_inode_ref);
1551 if (backref->found_dir_item)
1552 add_root_backref(dst_cache, rec->ino,
1553 root->root_key.objectid, backref->dir,
1554 backref->index, backref->name,
1555 backref->namelen, BTRFS_DIR_ITEM_KEY,
1557 if (backref->found_dir_index)
1558 add_root_backref(dst_cache, rec->ino,
1559 root->root_key.objectid, backref->dir,
1560 backref->index, backref->name,
1561 backref->namelen, BTRFS_DIR_INDEX_KEY,
1565 free_inode_rec(rec);
1570 static int check_root_refs(struct btrfs_root *root,
1571 struct cache_tree *root_cache)
1573 struct root_record *rec;
1574 struct root_record *ref_root;
1575 struct root_backref *backref;
1576 struct cache_extent *cache;
1582 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1585 /* fixme: this can not detect circular references */
1588 cache = find_first_cache_extent(root_cache, 0);
1592 rec = container_of(cache, struct root_record, cache);
1593 cache = next_cache_extent(cache);
1595 if (rec->found_ref == 0)
1598 list_for_each_entry(backref, &rec->backrefs, list) {
1599 if (!backref->reachable)
1602 ref_root = get_root_rec(root_cache,
1604 if (ref_root->found_ref > 0)
1607 backref->reachable = 0;
1609 if (rec->found_ref == 0)
1615 cache = find_first_cache_extent(root_cache, 0);
1619 rec = container_of(cache, struct root_record, cache);
1620 cache = next_cache_extent(cache);
1622 if (rec->found_ref == 0 &&
1623 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1624 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1625 ret = check_orphan_item(root->fs_info->tree_root,
1630 fprintf(stderr, "fs tree %llu not referenced\n",
1631 (unsigned long long)rec->objectid);
1635 if (rec->found_ref > 0 && !rec->found_root_item)
1637 list_for_each_entry(backref, &rec->backrefs, list) {
1638 if (!backref->found_dir_item)
1639 backref->errors |= REF_ERR_NO_DIR_ITEM;
1640 if (!backref->found_dir_index)
1641 backref->errors |= REF_ERR_NO_DIR_INDEX;
1642 if (!backref->found_back_ref)
1643 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1644 if (!backref->found_forward_ref)
1645 backref->errors |= REF_ERR_NO_ROOT_REF;
1646 if (backref->reachable && backref->errors)
1653 fprintf(stderr, "fs tree %llu refs %u %s\n",
1654 (unsigned long long)rec->objectid, rec->found_ref,
1655 rec->found_root_item ? "" : "not found");
1657 list_for_each_entry(backref, &rec->backrefs, list) {
1658 if (!backref->reachable)
1660 if (!backref->errors && rec->found_root_item)
1662 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1663 " index %llu namelen %u name %s error %x\n",
1664 (unsigned long long)backref->ref_root,
1665 (unsigned long long)backref->dir,
1666 (unsigned long long)backref->index,
1667 backref->namelen, backref->name,
1671 return errors > 0 ? 1 : 0;
1674 static int process_root_ref(struct extent_buffer *eb, int slot,
1675 struct btrfs_key *key,
1676 struct cache_tree *root_cache)
1682 struct btrfs_root_ref *ref;
1683 char namebuf[BTRFS_NAME_LEN];
1686 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1688 dirid = btrfs_root_ref_dirid(eb, ref);
1689 index = btrfs_root_ref_sequence(eb, ref);
1690 name_len = btrfs_root_ref_name_len(eb, ref);
1692 if (name_len <= BTRFS_NAME_LEN) {
1696 len = BTRFS_NAME_LEN;
1697 error = REF_ERR_NAME_TOO_LONG;
1699 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1701 if (key->type == BTRFS_ROOT_REF_KEY) {
1702 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1703 index, namebuf, len, key->type, error);
1705 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1706 index, namebuf, len, key->type, error);
1711 static int check_fs_root(struct btrfs_root *root,
1712 struct cache_tree *root_cache,
1713 struct walk_control *wc)
1718 struct btrfs_path path;
1719 struct shared_node root_node;
1720 struct root_record *rec;
1721 struct btrfs_root_item *root_item = &root->root_item;
1723 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1724 rec = get_root_rec(root_cache, root->root_key.objectid);
1725 if (btrfs_root_refs(root_item) > 0)
1726 rec->found_root_item = 1;
1729 btrfs_init_path(&path);
1730 memset(&root_node, 0, sizeof(root_node));
1731 cache_tree_init(&root_node.root_cache);
1732 cache_tree_init(&root_node.inode_cache);
1734 level = btrfs_header_level(root->node);
1735 memset(wc->nodes, 0, sizeof(wc->nodes));
1736 wc->nodes[level] = &root_node;
1737 wc->active_node = level;
1738 wc->root_level = level;
1740 if (btrfs_root_refs(root_item) > 0 ||
1741 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1742 path.nodes[level] = root->node;
1743 extent_buffer_get(root->node);
1744 path.slots[level] = 0;
1746 struct btrfs_key key;
1747 struct btrfs_disk_key found_key;
1749 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1750 level = root_item->drop_level;
1751 path.lowest_level = level;
1752 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1754 btrfs_node_key(path.nodes[level], &found_key,
1756 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1757 sizeof(found_key)));
1761 wret = walk_down_tree(root, &path, wc, &level);
1767 wret = walk_up_tree(root, &path, wc, &level);
1773 btrfs_release_path(root, &path);
1775 merge_root_recs(root, &root_node.root_cache, root_cache);
1777 if (root_node.current) {
1778 root_node.current->checked = 1;
1779 maybe_free_inode_rec(&root_node.inode_cache,
1783 ret = check_inode_recs(root, &root_node.inode_cache);
1787 static int fs_root_objectid(u64 objectid)
1789 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1790 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1791 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1792 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1793 objectid <= BTRFS_LAST_FREE_OBJECTID))
1798 static int check_fs_roots(struct btrfs_root *root,
1799 struct cache_tree *root_cache)
1801 struct btrfs_path path;
1802 struct btrfs_key key;
1803 struct walk_control wc;
1804 struct extent_buffer *leaf;
1805 struct btrfs_root *tmp_root;
1806 struct btrfs_root *tree_root = root->fs_info->tree_root;
1810 memset(&wc, 0, sizeof(wc));
1811 cache_tree_init(&wc.shared);
1812 btrfs_init_path(&path);
1816 key.type = BTRFS_ROOT_ITEM_KEY;
1817 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1820 leaf = path.nodes[0];
1821 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1822 ret = btrfs_next_leaf(tree_root, &path);
1825 leaf = path.nodes[0];
1827 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1828 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1829 fs_root_objectid(key.objectid)) {
1830 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1832 if (IS_ERR(tmp_root)) {
1836 ret = check_fs_root(tmp_root, root_cache, &wc);
1839 btrfs_free_fs_root(root->fs_info, tmp_root);
1840 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1841 key.type == BTRFS_ROOT_BACKREF_KEY) {
1842 process_root_ref(leaf, path.slots[0], &key,
1848 btrfs_release_path(tree_root, &path);
1850 if (!cache_tree_empty(&wc.shared))
1851 fprintf(stderr, "warning line %d\n", __LINE__);
1856 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1858 struct list_head *cur = rec->backrefs.next;
1859 struct extent_backref *back;
1860 struct tree_backref *tback;
1861 struct data_backref *dback;
1865 while(cur != &rec->backrefs) {
1866 back = list_entry(cur, struct extent_backref, list);
1868 if (!back->found_extent_tree) {
1872 if (back->is_data) {
1873 dback = (struct data_backref *)back;
1874 fprintf(stderr, "Backref %llu %s %llu"
1875 " owner %llu offset %llu num_refs %lu"
1876 " not found in extent tree\n",
1877 (unsigned long long)rec->start,
1878 back->full_backref ?
1880 back->full_backref ?
1881 (unsigned long long)dback->parent:
1882 (unsigned long long)dback->root,
1883 (unsigned long long)dback->owner,
1884 (unsigned long long)dback->offset,
1885 (unsigned long)dback->num_refs);
1887 tback = (struct tree_backref *)back;
1888 fprintf(stderr, "Backref %llu parent %llu"
1889 " root %llu not found in extent tree\n",
1890 (unsigned long long)rec->start,
1891 (unsigned long long)tback->parent,
1892 (unsigned long long)tback->root);
1895 if (!back->is_data && !back->found_ref) {
1899 tback = (struct tree_backref *)back;
1900 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1901 (unsigned long long)rec->start,
1902 back->full_backref ? "parent" : "root",
1903 back->full_backref ?
1904 (unsigned long long)tback->parent :
1905 (unsigned long long)tback->root, back);
1907 if (back->is_data) {
1908 dback = (struct data_backref *)back;
1909 if (dback->found_ref != dback->num_refs) {
1913 fprintf(stderr, "Incorrect local backref count"
1914 " on %llu %s %llu owner %llu"
1915 " offset %llu found %u wanted %u back %p\n",
1916 (unsigned long long)rec->start,
1917 back->full_backref ?
1919 back->full_backref ?
1920 (unsigned long long)dback->parent:
1921 (unsigned long long)dback->root,
1922 (unsigned long long)dback->owner,
1923 (unsigned long long)dback->offset,
1924 dback->found_ref, dback->num_refs, back);
1926 if (dback->bytes != rec->nr) {
1930 fprintf(stderr, "Backref bytes do not match "
1931 "extent backref, bytenr=%llu, ref "
1932 "bytes=%llu, backref bytes=%llu\n",
1933 (unsigned long long)rec->start,
1934 (unsigned long long)rec->nr,
1935 (unsigned long long)dback->bytes);
1938 if (!back->is_data) {
1941 dback = (struct data_backref *)back;
1942 found += dback->found_ref;
1945 if (found != rec->refs) {
1949 fprintf(stderr, "Incorrect global backref count "
1950 "on %llu found %llu wanted %llu\n",
1951 (unsigned long long)rec->start,
1952 (unsigned long long)found,
1953 (unsigned long long)rec->refs);
1959 static int free_all_extent_backrefs(struct extent_record *rec)
1961 struct extent_backref *back;
1962 struct list_head *cur;
1963 while (!list_empty(&rec->backrefs)) {
1964 cur = rec->backrefs.next;
1965 back = list_entry(cur, struct extent_backref, list);
1972 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
1973 struct extent_record *rec)
1975 if (rec->content_checked && rec->owner_ref_checked &&
1976 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
1977 !all_backpointers_checked(rec, 0)) {
1978 remove_cache_extent(extent_cache, &rec->cache);
1979 free_all_extent_backrefs(rec);
1985 static int check_owner_ref(struct btrfs_root *root,
1986 struct extent_record *rec,
1987 struct extent_buffer *buf)
1989 struct extent_backref *node;
1990 struct tree_backref *back;
1991 struct btrfs_root *ref_root;
1992 struct btrfs_key key;
1993 struct btrfs_path path;
1994 struct extent_buffer *parent;
1999 list_for_each_entry(node, &rec->backrefs, list) {
2002 if (!node->found_ref)
2004 if (node->full_backref)
2006 back = (struct tree_backref *)node;
2007 if (btrfs_header_owner(buf) == back->root)
2010 BUG_ON(rec->is_root);
2012 /* try to find the block by search corresponding fs tree */
2013 key.objectid = btrfs_header_owner(buf);
2014 key.type = BTRFS_ROOT_ITEM_KEY;
2015 key.offset = (u64)-1;
2017 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2018 if (IS_ERR(ref_root))
2021 level = btrfs_header_level(buf);
2023 btrfs_item_key_to_cpu(buf, &key, 0);
2025 btrfs_node_key_to_cpu(buf, &key, 0);
2027 btrfs_init_path(&path);
2028 path.lowest_level = level + 1;
2029 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2033 parent = path.nodes[level + 1];
2034 if (parent && buf->start == btrfs_node_blockptr(parent,
2035 path.slots[level + 1]))
2038 btrfs_release_path(ref_root, &path);
2039 return found ? 0 : 1;
2042 static int is_extent_tree_record(struct extent_record *rec)
2044 struct list_head *cur = rec->backrefs.next;
2045 struct extent_backref *node;
2046 struct tree_backref *back;
2049 while(cur != &rec->backrefs) {
2050 node = list_entry(cur, struct extent_backref, list);
2054 back = (struct tree_backref *)node;
2055 if (node->full_backref)
2057 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2064 static int record_bad_block_io(struct btrfs_fs_info *info,
2065 struct cache_tree *extent_cache,
2068 struct extent_record *rec;
2069 struct cache_extent *cache;
2070 struct btrfs_key key;
2072 cache = find_cache_extent(extent_cache, start, len);
2076 rec = container_of(cache, struct extent_record, cache);
2077 if (!is_extent_tree_record(rec))
2080 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2081 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2084 static int check_block(struct btrfs_root *root,
2085 struct cache_tree *extent_cache,
2086 struct extent_buffer *buf, u64 flags)
2088 struct extent_record *rec;
2089 struct cache_extent *cache;
2090 struct btrfs_key key;
2094 cache = find_cache_extent(extent_cache, buf->start, buf->len);
2097 rec = container_of(cache, struct extent_record, cache);
2098 rec->generation = btrfs_header_generation(buf);
2100 level = btrfs_header_level(buf);
2101 if (btrfs_header_nritems(buf) > 0) {
2104 btrfs_item_key_to_cpu(buf, &key, 0);
2106 btrfs_node_key_to_cpu(buf, &key, 0);
2108 rec->info_objectid = key.objectid;
2110 rec->info_level = level;
2112 if (btrfs_is_leaf(buf))
2113 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2115 ret = btrfs_check_node(root, &rec->parent_key, buf);
2118 fprintf(stderr, "bad block %llu\n",
2119 (unsigned long long)buf->start);
2121 rec->content_checked = 1;
2122 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2123 rec->owner_ref_checked = 1;
2125 ret = check_owner_ref(root, rec, buf);
2127 rec->owner_ref_checked = 1;
2131 maybe_free_extent_rec(extent_cache, rec);
2135 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2136 u64 parent, u64 root)
2138 struct list_head *cur = rec->backrefs.next;
2139 struct extent_backref *node;
2140 struct tree_backref *back;
2142 while(cur != &rec->backrefs) {
2143 node = list_entry(cur, struct extent_backref, list);
2147 back = (struct tree_backref *)node;
2149 if (!node->full_backref)
2151 if (parent == back->parent)
2154 if (node->full_backref)
2156 if (back->root == root)
2163 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2164 u64 parent, u64 root)
2166 struct tree_backref *ref = malloc(sizeof(*ref));
2167 memset(&ref->node, 0, sizeof(ref->node));
2169 ref->parent = parent;
2170 ref->node.full_backref = 1;
2173 ref->node.full_backref = 0;
2175 list_add_tail(&ref->node.list, &rec->backrefs);
2180 static struct data_backref *find_data_backref(struct extent_record *rec,
2181 u64 parent, u64 root,
2182 u64 owner, u64 offset,
2183 int found_ref, u64 bytes)
2185 struct list_head *cur = rec->backrefs.next;
2186 struct extent_backref *node;
2187 struct data_backref *back;
2189 while(cur != &rec->backrefs) {
2190 node = list_entry(cur, struct extent_backref, list);
2194 back = (struct data_backref *)node;
2196 if (!node->full_backref)
2198 if (parent == back->parent)
2201 if (node->full_backref)
2203 if (back->root == root && back->owner == owner &&
2204 back->offset == offset) {
2205 if (found_ref && node->found_ref &&
2206 back->bytes != bytes)
2215 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2216 u64 parent, u64 root,
2217 u64 owner, u64 offset,
2220 struct data_backref *ref = malloc(sizeof(*ref));
2221 memset(&ref->node, 0, sizeof(ref->node));
2222 ref->node.is_data = 1;
2225 ref->parent = parent;
2228 ref->node.full_backref = 1;
2232 ref->offset = offset;
2233 ref->node.full_backref = 0;
2235 ref->bytes = max_size;
2238 list_add_tail(&ref->node.list, &rec->backrefs);
2239 if (max_size > rec->max_size)
2240 rec->max_size = max_size;
2244 static int add_extent_rec(struct cache_tree *extent_cache,
2245 struct btrfs_key *parent_key,
2246 u64 start, u64 nr, u64 extent_item_refs,
2247 int is_root, int inc_ref, int set_checked,
2248 int metadata, int extent_rec, u64 max_size)
2250 struct extent_record *rec;
2251 struct cache_extent *cache;
2254 cache = find_cache_extent(extent_cache, start, nr);
2256 rec = container_of(cache, struct extent_record, cache);
2260 rec->nr = max(nr, max_size);
2263 * We need to make sure to reset nr to whatever the extent
2264 * record says was the real size, this way we can compare it to
2270 if (start != rec->start) {
2271 fprintf(stderr, "warning, start mismatch %llu %llu\n",
2272 (unsigned long long)rec->start,
2273 (unsigned long long)start);
2276 if (extent_item_refs) {
2277 if (rec->extent_item_refs) {
2278 fprintf(stderr, "block %llu rec "
2279 "extent_item_refs %llu, passed %llu\n",
2280 (unsigned long long)start,
2281 (unsigned long long)
2282 rec->extent_item_refs,
2283 (unsigned long long)extent_item_refs);
2285 rec->extent_item_refs = extent_item_refs;
2290 rec->content_checked = 1;
2291 rec->owner_ref_checked = 1;
2295 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2297 if (rec->max_size < max_size)
2298 rec->max_size = max_size;
2300 maybe_free_extent_rec(extent_cache, rec);
2303 rec = malloc(sizeof(*rec));
2305 rec->max_size = max_size;
2306 rec->nr = max(nr, max_size);
2307 rec->content_checked = 0;
2308 rec->owner_ref_checked = 0;
2309 rec->metadata = metadata;
2310 INIT_LIST_HEAD(&rec->backrefs);
2322 if (extent_item_refs)
2323 rec->extent_item_refs = extent_item_refs;
2325 rec->extent_item_refs = 0;
2328 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2330 memset(&rec->parent_key, 0, sizeof(*parent_key));
2332 rec->cache.start = start;
2333 rec->cache.size = nr;
2334 ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2338 rec->content_checked = 1;
2339 rec->owner_ref_checked = 1;
2344 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2345 u64 parent, u64 root, int found_ref)
2347 struct extent_record *rec;
2348 struct tree_backref *back;
2349 struct cache_extent *cache;
2351 cache = find_cache_extent(extent_cache, bytenr, 1);
2353 add_extent_rec(extent_cache, NULL, bytenr,
2354 1, 0, 0, 0, 0, 1, 0, 0);
2355 cache = find_cache_extent(extent_cache, bytenr, 1);
2360 rec = container_of(cache, struct extent_record, cache);
2361 if (rec->start != bytenr) {
2365 back = find_tree_backref(rec, parent, root);
2367 back = alloc_tree_backref(rec, parent, root);
2370 if (back->node.found_ref) {
2371 fprintf(stderr, "Extent back ref already exists "
2372 "for %llu parent %llu root %llu \n",
2373 (unsigned long long)bytenr,
2374 (unsigned long long)parent,
2375 (unsigned long long)root);
2377 back->node.found_ref = 1;
2379 if (back->node.found_extent_tree) {
2380 fprintf(stderr, "Extent back ref already exists "
2381 "for %llu parent %llu root %llu \n",
2382 (unsigned long long)bytenr,
2383 (unsigned long long)parent,
2384 (unsigned long long)root);
2386 back->node.found_extent_tree = 1;
2391 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2392 u64 parent, u64 root, u64 owner, u64 offset,
2393 u32 num_refs, int found_ref, u64 max_size)
2395 struct extent_record *rec;
2396 struct data_backref *back;
2397 struct cache_extent *cache;
2399 cache = find_cache_extent(extent_cache, bytenr, 1);
2401 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2403 cache = find_cache_extent(extent_cache, bytenr, 1);
2408 rec = container_of(cache, struct extent_record, cache);
2409 if (rec->start != bytenr) {
2412 if (rec->max_size < max_size)
2413 rec->max_size = max_size;
2416 * If found_ref is set then max_size is the real size and must match the
2417 * existing refs. So if we have already found a ref then we need to
2418 * make sure that this ref matches the existing one, otherwise we need
2419 * to add a new backref so we can notice that the backrefs don't match
2420 * and we need to figure out who is telling the truth. This is to
2421 * account for that awful fsync bug I introduced where we'd end up with
2422 * a btrfs_file_extent_item that would have its length include multiple
2423 * prealloc extents or point inside of a prealloc extent.
2425 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2428 back = alloc_data_backref(rec, parent, root, owner, offset,
2432 BUG_ON(num_refs != 1);
2433 if (back->node.found_ref)
2434 BUG_ON(back->bytes != max_size);
2435 back->node.found_ref = 1;
2436 back->found_ref += 1;
2437 back->bytes = max_size;
2439 if (back->node.found_extent_tree) {
2440 fprintf(stderr, "Extent back ref already exists "
2441 "for %llu parent %llu root %llu"
2442 "owner %llu offset %llu num_refs %lu\n",
2443 (unsigned long long)bytenr,
2444 (unsigned long long)parent,
2445 (unsigned long long)root,
2446 (unsigned long long)owner,
2447 (unsigned long long)offset,
2448 (unsigned long)num_refs);
2450 back->num_refs = num_refs;
2451 back->node.found_extent_tree = 1;
2456 static int add_pending(struct cache_tree *pending,
2457 struct cache_tree *seen, u64 bytenr, u32 size)
2460 ret = insert_cache_extent(seen, bytenr, size);
2463 insert_cache_extent(pending, bytenr, size);
2467 static int pick_next_pending(struct cache_tree *pending,
2468 struct cache_tree *reada,
2469 struct cache_tree *nodes,
2470 u64 last, struct block_info *bits, int bits_nr,
2473 unsigned long node_start = last;
2474 struct cache_extent *cache;
2477 cache = find_first_cache_extent(reada, 0);
2479 bits[0].start = cache->start;
2480 bits[1].size = cache->size;
2485 if (node_start > 32768)
2486 node_start -= 32768;
2488 cache = find_first_cache_extent(nodes, node_start);
2490 cache = find_first_cache_extent(nodes, 0);
2493 cache = find_first_cache_extent(pending, 0);
2498 bits[ret].start = cache->start;
2499 bits[ret].size = cache->size;
2500 cache = next_cache_extent(cache);
2502 } while (cache && ret < bits_nr);
2508 bits[ret].start = cache->start;
2509 bits[ret].size = cache->size;
2510 cache = next_cache_extent(cache);
2512 } while (cache && ret < bits_nr);
2514 if (bits_nr - ret > 8) {
2515 u64 lookup = bits[0].start + bits[0].size;
2516 struct cache_extent *next;
2517 next = find_first_cache_extent(pending, lookup);
2519 if (next->start - lookup > 32768)
2521 bits[ret].start = next->start;
2522 bits[ret].size = next->size;
2523 lookup = next->start + next->size;
2527 next = next_cache_extent(next);
2535 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2536 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2537 struct extent_buffer *leaf, int slot)
2539 struct btrfs_extent_ref_v0 *ref0;
2540 struct btrfs_key key;
2542 btrfs_item_key_to_cpu(leaf, &key, slot);
2543 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2544 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2545 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2547 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2548 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2554 static int process_extent_item(struct btrfs_root *root,
2555 struct cache_tree *extent_cache,
2556 struct extent_buffer *eb, int slot)
2558 struct btrfs_extent_item *ei;
2559 struct btrfs_extent_inline_ref *iref;
2560 struct btrfs_extent_data_ref *dref;
2561 struct btrfs_shared_data_ref *sref;
2562 struct btrfs_key key;
2566 u32 item_size = btrfs_item_size_nr(eb, slot);
2572 btrfs_item_key_to_cpu(eb, &key, slot);
2574 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2576 num_bytes = root->leafsize;
2578 num_bytes = key.offset;
2581 if (item_size < sizeof(*ei)) {
2582 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2583 struct btrfs_extent_item_v0 *ei0;
2584 BUG_ON(item_size != sizeof(*ei0));
2585 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2586 refs = btrfs_extent_refs_v0(eb, ei0);
2590 return add_extent_rec(extent_cache, NULL, key.objectid,
2591 num_bytes, refs, 0, 0, 0, metadata, 1,
2595 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2596 refs = btrfs_extent_refs(eb, ei);
2598 add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
2599 refs, 0, 0, 0, metadata, 1, num_bytes);
2601 ptr = (unsigned long)(ei + 1);
2602 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
2603 key.type == BTRFS_EXTENT_ITEM_KEY)
2604 ptr += sizeof(struct btrfs_tree_block_info);
2606 end = (unsigned long)ei + item_size;
2608 iref = (struct btrfs_extent_inline_ref *)ptr;
2609 type = btrfs_extent_inline_ref_type(eb, iref);
2610 offset = btrfs_extent_inline_ref_offset(eb, iref);
2612 case BTRFS_TREE_BLOCK_REF_KEY:
2613 add_tree_backref(extent_cache, key.objectid,
2616 case BTRFS_SHARED_BLOCK_REF_KEY:
2617 add_tree_backref(extent_cache, key.objectid,
2620 case BTRFS_EXTENT_DATA_REF_KEY:
2621 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2622 add_data_backref(extent_cache, key.objectid, 0,
2623 btrfs_extent_data_ref_root(eb, dref),
2624 btrfs_extent_data_ref_objectid(eb,
2626 btrfs_extent_data_ref_offset(eb, dref),
2627 btrfs_extent_data_ref_count(eb, dref),
2630 case BTRFS_SHARED_DATA_REF_KEY:
2631 sref = (struct btrfs_shared_data_ref *)(iref + 1);
2632 add_data_backref(extent_cache, key.objectid, offset,
2634 btrfs_shared_data_ref_count(eb, sref),
2638 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2639 key.objectid, key.type, num_bytes);
2642 ptr += btrfs_extent_inline_ref_size(type);
2649 static int check_cache_range(struct btrfs_root *root,
2650 struct btrfs_block_group_cache *cache,
2651 u64 offset, u64 bytes)
2653 struct btrfs_free_space *entry;
2659 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2660 bytenr = btrfs_sb_offset(i);
2661 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
2662 cache->key.objectid, bytenr, 0,
2663 &logical, &nr, &stripe_len);
2668 if (logical[nr] + stripe_len <= offset)
2670 if (offset + bytes <= logical[nr])
2672 if (logical[nr] == offset) {
2673 if (stripe_len >= bytes) {
2677 bytes -= stripe_len;
2678 offset += stripe_len;
2679 } else if (logical[nr] < offset) {
2680 if (logical[nr] + stripe_len >=
2685 bytes = (offset + bytes) -
2686 (logical[nr] + stripe_len);
2687 offset = logical[nr] + stripe_len;
2690 * Could be tricky, the super may land in the
2691 * middle of the area we're checking. First
2692 * check the easiest case, it's at the end.
2694 if (logical[nr] + stripe_len >=
2696 bytes = logical[nr] - offset;
2700 /* Check the left side */
2701 ret = check_cache_range(root, cache,
2703 logical[nr] - offset);
2709 /* Now we continue with the right side */
2710 bytes = (offset + bytes) -
2711 (logical[nr] + stripe_len);
2712 offset = logical[nr] + stripe_len;
2719 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
2721 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
2722 offset, offset+bytes);
2726 if (entry->offset != offset) {
2727 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
2732 if (entry->bytes != bytes) {
2733 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
2734 bytes, entry->bytes, offset);
2738 unlink_free_space(cache->free_space_ctl, entry);
2743 static int verify_space_cache(struct btrfs_root *root,
2744 struct btrfs_block_group_cache *cache)
2746 struct btrfs_path *path;
2747 struct extent_buffer *leaf;
2748 struct btrfs_key key;
2752 path = btrfs_alloc_path();
2756 root = root->fs_info->extent_root;
2758 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
2760 key.objectid = last;
2762 key.type = BTRFS_EXTENT_ITEM_KEY;
2764 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2769 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2770 ret = btrfs_next_leaf(root, path);
2778 leaf = path->nodes[0];
2779 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2780 if (key.objectid >= cache->key.offset + cache->key.objectid)
2782 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2783 key.type != BTRFS_METADATA_ITEM_KEY) {
2788 if (last == key.objectid) {
2789 last = key.objectid + key.offset;
2794 ret = check_cache_range(root, cache, last,
2795 key.objectid - last);
2798 if (key.type == BTRFS_EXTENT_ITEM_KEY)
2799 last = key.objectid + key.offset;
2801 last = key.objectid + root->leafsize;
2805 if (last < cache->key.objectid + cache->key.offset)
2806 ret = check_cache_range(root, cache, last,
2807 cache->key.objectid +
2808 cache->key.offset - last);
2809 btrfs_free_path(path);
2812 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
2813 fprintf(stderr, "There are still entries left in the space "
2821 static int check_space_cache(struct btrfs_root *root)
2823 struct btrfs_block_group_cache *cache;
2824 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
2828 if (btrfs_super_generation(root->fs_info->super_copy) !=
2829 btrfs_super_cache_generation(root->fs_info->super_copy)) {
2830 printf("cache and super generation don't match, space cache "
2831 "will be invalidated\n");
2836 cache = btrfs_lookup_first_block_group(root->fs_info, start);
2840 start = cache->key.objectid + cache->key.offset;
2841 if (!cache->free_space_ctl) {
2844 if (cache->flags & (BTRFS_BLOCK_GROUP_METADATA |
2845 BTRFS_BLOCK_GROUP_SYSTEM))
2846 sectorsize = root->leafsize;
2848 sectorsize = root->sectorsize;
2850 if (btrfs_init_free_space_ctl(cache, sectorsize)) {
2855 btrfs_remove_free_space_cache(cache);
2858 ret = load_free_space_cache(root->fs_info, cache);
2862 ret = verify_space_cache(root, cache);
2864 fprintf(stderr, "cache appears valid but isnt %Lu\n",
2865 cache->key.objectid);
2870 return error ? -EINVAL : 0;
2873 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
2876 struct btrfs_path *path;
2877 struct extent_buffer *leaf;
2878 struct btrfs_key key;
2881 path = btrfs_alloc_path();
2883 fprintf(stderr, "Error allocing path\n");
2887 key.objectid = bytenr;
2888 key.type = BTRFS_EXTENT_ITEM_KEY;
2893 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
2896 fprintf(stderr, "Error looking up extent record %d\n", ret);
2897 btrfs_free_path(path);
2903 btrfs_prev_leaf(root, path);
2906 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2909 * Block group items come before extent items if they have the same
2910 * bytenr, so walk back one more just in case. Dear future traveler,
2911 * first congrats on mastering time travel. Now if it's not too much
2912 * trouble could you go back to 2006 and tell Chris to make the
2913 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
2915 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2919 btrfs_prev_leaf(root, path);
2923 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2924 ret = btrfs_next_leaf(root, path);
2926 fprintf(stderr, "Error going to next leaf "
2928 btrfs_free_path(path);
2934 leaf = path->nodes[0];
2935 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2936 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
2940 if (key.objectid + key.offset < bytenr) {
2944 if (key.objectid > bytenr + num_bytes)
2947 if (key.objectid == bytenr) {
2948 if (key.offset >= num_bytes) {
2952 num_bytes -= key.offset;
2953 bytenr += key.offset;
2954 } else if (key.objectid < bytenr) {
2955 if (key.objectid + key.offset >= bytenr + num_bytes) {
2959 num_bytes = (bytenr + num_bytes) -
2960 (key.objectid + key.offset);
2961 bytenr = key.objectid + key.offset;
2963 if (key.objectid + key.offset < bytenr + num_bytes) {
2964 u64 new_start = key.objectid + key.offset;
2965 u64 new_bytes = bytenr + num_bytes - new_start;
2968 * Weird case, the extent is in the middle of
2969 * our range, we'll have to search one side
2970 * and then the other. Not sure if this happens
2971 * in real life, but no harm in coding it up
2972 * anyway just in case.
2974 btrfs_release_path(root, path);
2975 ret = check_extent_exists(root, new_start,
2978 fprintf(stderr, "Right section didn't "
2982 num_bytes = key.objectid - bytenr;
2985 num_bytes = key.objectid - bytenr;
2992 fprintf(stderr, "There are no extents for csum range "
2993 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
2997 btrfs_free_path(path);
3001 static int check_csums(struct btrfs_root *root)
3003 struct btrfs_path *path;
3004 struct extent_buffer *leaf;
3005 struct btrfs_key key;
3006 u64 offset = 0, num_bytes = 0;
3007 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3011 root = root->fs_info->csum_root;
3013 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3014 key.type = BTRFS_EXTENT_CSUM_KEY;
3017 path = btrfs_alloc_path();
3021 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3023 fprintf(stderr, "Error searching csum tree %d\n", ret);
3024 btrfs_free_path(path);
3028 if (ret > 0 && path->slots[0])
3033 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3034 ret = btrfs_next_leaf(root, path);
3036 fprintf(stderr, "Error going to next leaf "
3043 leaf = path->nodes[0];
3045 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3046 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3052 offset = key.offset;
3053 } else if (key.offset != offset + num_bytes) {
3054 ret = check_extent_exists(root, offset, num_bytes);
3056 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3057 "there is no extent record\n",
3058 offset, offset+num_bytes);
3061 offset = key.offset;
3065 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3066 csum_size) * root->sectorsize;
3070 btrfs_free_path(path);
3074 static int run_next_block(struct btrfs_root *root,
3075 struct block_info *bits,
3078 struct cache_tree *pending,
3079 struct cache_tree *seen,
3080 struct cache_tree *reada,
3081 struct cache_tree *nodes,
3082 struct cache_tree *extent_cache)
3084 struct extent_buffer *buf;
3093 struct btrfs_key key;
3094 struct cache_extent *cache;
3097 ret = pick_next_pending(pending, reada, nodes, *last, bits,
3098 bits_nr, &reada_bits);
3103 for(i = 0; i < ret; i++) {
3104 insert_cache_extent(reada, bits[i].start,
3107 /* fixme, get the parent transid */
3108 readahead_tree_block(root, bits[i].start,
3112 *last = bits[0].start;
3113 bytenr = bits[0].start;
3114 size = bits[0].size;
3116 cache = find_cache_extent(pending, bytenr, size);
3118 remove_cache_extent(pending, cache);
3121 cache = find_cache_extent(reada, bytenr, size);
3123 remove_cache_extent(reada, cache);
3126 cache = find_cache_extent(nodes, bytenr, size);
3128 remove_cache_extent(nodes, cache);
3132 /* fixme, get the real parent transid */
3133 buf = read_tree_block(root, bytenr, size, 0);
3134 if (!extent_buffer_uptodate(buf)) {
3135 record_bad_block_io(root->fs_info,
3136 extent_cache, bytenr, size);
3140 nritems = btrfs_header_nritems(buf);
3142 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3143 btrfs_header_level(buf), 1, NULL,
3146 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3148 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3153 owner = btrfs_header_owner(buf);
3156 ret = check_block(root, extent_cache, buf, flags);
3160 if (btrfs_is_leaf(buf)) {
3161 btree_space_waste += btrfs_leaf_free_space(root, buf);
3162 for (i = 0; i < nritems; i++) {
3163 struct btrfs_file_extent_item *fi;
3164 btrfs_item_key_to_cpu(buf, &key, i);
3165 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3166 process_extent_item(root, extent_cache, buf,
3170 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3171 process_extent_item(root, extent_cache, buf,
3175 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3177 btrfs_item_size_nr(buf, i);
3180 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3183 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3184 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3185 process_extent_ref_v0(extent_cache, buf, i);
3192 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3193 add_tree_backref(extent_cache, key.objectid, 0,
3197 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3198 add_tree_backref(extent_cache, key.objectid,
3202 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3203 struct btrfs_extent_data_ref *ref;
3204 ref = btrfs_item_ptr(buf, i,
3205 struct btrfs_extent_data_ref);
3206 add_data_backref(extent_cache,
3208 btrfs_extent_data_ref_root(buf, ref),
3209 btrfs_extent_data_ref_objectid(buf,
3211 btrfs_extent_data_ref_offset(buf, ref),
3212 btrfs_extent_data_ref_count(buf, ref),
3213 0, root->sectorsize);
3216 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3217 struct btrfs_shared_data_ref *ref;
3218 ref = btrfs_item_ptr(buf, i,
3219 struct btrfs_shared_data_ref);
3220 add_data_backref(extent_cache,
3221 key.objectid, key.offset, 0, 0, 0,
3222 btrfs_shared_data_ref_count(buf, ref),
3223 0, root->sectorsize);
3226 if (key.type != BTRFS_EXTENT_DATA_KEY)
3228 fi = btrfs_item_ptr(buf, i,
3229 struct btrfs_file_extent_item);
3230 if (btrfs_file_extent_type(buf, fi) ==
3231 BTRFS_FILE_EXTENT_INLINE)
3233 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3236 data_bytes_allocated +=
3237 btrfs_file_extent_disk_num_bytes(buf, fi);
3238 if (data_bytes_allocated < root->sectorsize) {
3241 data_bytes_referenced +=
3242 btrfs_file_extent_num_bytes(buf, fi);
3243 ret = add_extent_rec(extent_cache, NULL,
3244 btrfs_file_extent_disk_bytenr(buf, fi),
3245 btrfs_file_extent_disk_num_bytes(buf, fi),
3247 btrfs_file_extent_disk_num_bytes(buf, fi));
3248 add_data_backref(extent_cache,
3249 btrfs_file_extent_disk_bytenr(buf, fi),
3250 parent, owner, key.objectid, key.offset -
3251 btrfs_file_extent_offset(buf, fi), 1, 1,
3252 btrfs_file_extent_disk_num_bytes(buf, fi));
3257 struct btrfs_key first_key;
3259 first_key.objectid = 0;
3262 btrfs_item_key_to_cpu(buf, &first_key, 0);
3263 level = btrfs_header_level(buf);
3264 for (i = 0; i < nritems; i++) {
3265 u64 ptr = btrfs_node_blockptr(buf, i);
3266 u32 size = btrfs_level_size(root, level - 1);
3267 btrfs_node_key_to_cpu(buf, &key, i);
3268 ret = add_extent_rec(extent_cache, &key,
3269 ptr, size, 0, 0, 1, 0, 1, 0,
3273 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3276 add_pending(nodes, seen, ptr, size);
3278 add_pending(pending, seen, ptr, size);
3281 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3282 nritems) * sizeof(struct btrfs_key_ptr);
3284 total_btree_bytes += buf->len;
3285 if (fs_root_objectid(btrfs_header_owner(buf)))
3286 total_fs_tree_bytes += buf->len;
3287 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3288 total_extent_tree_bytes += buf->len;
3289 if (!found_old_backref &&
3290 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3291 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3292 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3293 found_old_backref = 1;
3295 free_extent_buffer(buf);
3299 static int add_root_to_pending(struct extent_buffer *buf,
3300 struct cache_tree *extent_cache,
3301 struct cache_tree *pending,
3302 struct cache_tree *seen,
3303 struct cache_tree *nodes,
3304 struct btrfs_key *root_key)
3306 if (btrfs_header_level(buf) > 0)
3307 add_pending(nodes, seen, buf->start, buf->len);
3309 add_pending(pending, seen, buf->start, buf->len);
3310 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3311 0, 1, 1, 0, 1, 0, buf->len);
3313 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3314 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3315 add_tree_backref(extent_cache, buf->start, buf->start,
3318 add_tree_backref(extent_cache, buf->start, 0,
3319 root_key->objectid, 1);
3323 /* as we fix the tree, we might be deleting blocks that
3324 * we're tracking for repair. This hook makes sure we
3325 * remove any backrefs for blocks as we are fixing them.
3327 static int free_extent_hook(struct btrfs_trans_handle *trans,
3328 struct btrfs_root *root,
3329 u64 bytenr, u64 num_bytes, u64 parent,
3330 u64 root_objectid, u64 owner, u64 offset,
3333 struct extent_record *rec;
3334 struct cache_extent *cache;
3336 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3338 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3339 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
3343 rec = container_of(cache, struct extent_record, cache);
3345 struct data_backref *back;
3346 back = find_data_backref(rec, parent, root_objectid, owner,
3347 offset, 1, num_bytes);
3350 if (back->node.found_ref) {
3351 back->found_ref -= refs_to_drop;
3353 rec->refs -= refs_to_drop;
3355 if (back->node.found_extent_tree) {
3356 back->num_refs -= refs_to_drop;
3357 if (rec->extent_item_refs)
3358 rec->extent_item_refs -= refs_to_drop;
3360 if (back->found_ref == 0)
3361 back->node.found_ref = 0;
3362 if (back->num_refs == 0)
3363 back->node.found_extent_tree = 0;
3365 if (!back->node.found_extent_tree && back->node.found_ref) {
3366 list_del(&back->node.list);
3370 struct tree_backref *back;
3371 back = find_tree_backref(rec, parent, root_objectid);
3374 if (back->node.found_ref) {
3377 back->node.found_ref = 0;
3379 if (back->node.found_extent_tree) {
3380 if (rec->extent_item_refs)
3381 rec->extent_item_refs--;
3382 back->node.found_extent_tree = 0;
3384 if (!back->node.found_extent_tree && back->node.found_ref) {
3385 list_del(&back->node.list);
3389 maybe_free_extent_rec(extent_cache, rec);
3394 static int delete_extent_records(struct btrfs_trans_handle *trans,
3395 struct btrfs_root *root,
3396 struct btrfs_path *path,
3397 u64 bytenr, u64 new_len)
3399 struct btrfs_key key;
3400 struct btrfs_key found_key;
3401 struct extent_buffer *leaf;
3406 key.objectid = bytenr;
3408 key.offset = (u64)-1;
3411 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3418 if (path->slots[0] == 0)
3424 leaf = path->nodes[0];
3425 slot = path->slots[0];
3427 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3428 if (found_key.objectid != bytenr)
3431 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3432 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3433 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3434 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3435 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3436 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3437 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3438 btrfs_release_path(NULL, path);
3439 if (found_key.type == 0) {
3440 if (found_key.offset == 0)
3442 key.offset = found_key.offset - 1;
3443 key.type = found_key.type;
3445 key.type = found_key.type - 1;
3446 key.offset = (u64)-1;
3450 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
3451 found_key.objectid, found_key.type, found_key.offset);
3453 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
3456 btrfs_release_path(NULL, path);
3458 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3459 found_key.type == BTRFS_METADATA_ITEM_KEY) {
3460 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
3461 found_key.offset : root->leafsize;
3463 ret = btrfs_update_block_group(trans, root, bytenr,
3470 btrfs_release_path(NULL, path);
3475 * for a single backref, this will allocate a new extent
3476 * and add the backref to it.
3478 static int record_extent(struct btrfs_trans_handle *trans,
3479 struct btrfs_fs_info *info,
3480 struct btrfs_path *path,
3481 struct extent_record *rec,
3482 struct extent_backref *back,
3483 int allocated, u64 flags)
3486 struct btrfs_root *extent_root = info->extent_root;
3487 struct extent_buffer *leaf;
3488 struct btrfs_key ins_key;
3489 struct btrfs_extent_item *ei;
3490 struct tree_backref *tback;
3491 struct data_backref *dback;
3492 struct btrfs_tree_block_info *bi;
3495 rec->max_size = max_t(u64, rec->max_size,
3496 info->extent_root->leafsize);
3499 u32 item_size = sizeof(*ei);
3502 item_size += sizeof(*bi);
3504 ins_key.objectid = rec->start;
3505 ins_key.offset = rec->max_size;
3506 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3508 ret = btrfs_insert_empty_item(trans, extent_root, path,
3509 &ins_key, item_size);
3513 leaf = path->nodes[0];
3514 ei = btrfs_item_ptr(leaf, path->slots[0],
3515 struct btrfs_extent_item);
3517 btrfs_set_extent_refs(leaf, ei, 0);
3518 btrfs_set_extent_generation(leaf, ei, rec->generation);
3520 if (back->is_data) {
3521 btrfs_set_extent_flags(leaf, ei,
3522 BTRFS_EXTENT_FLAG_DATA);
3524 struct btrfs_disk_key copy_key;;
3526 tback = (struct tree_backref *)back;
3527 bi = (struct btrfs_tree_block_info *)(ei + 1);
3528 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3530 memset(©_key, 0, sizeof(copy_key));
3532 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3533 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3534 btrfs_set_tree_block_key(leaf, bi, ©_key);
3536 btrfs_set_extent_flags(leaf, ei,
3537 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3540 btrfs_mark_buffer_dirty(leaf);
3541 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3542 rec->max_size, 1, 0);
3545 btrfs_release_path(NULL, path);
3548 if (back->is_data) {
3552 dback = (struct data_backref *)back;
3553 if (back->full_backref)
3554 parent = dback->parent;
3558 for (i = 0; i < dback->found_ref; i++) {
3559 /* if parent != 0, we're doing a full backref
3560 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3561 * just makes the backref allocator create a data
3564 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3565 rec->start, rec->max_size,
3569 BTRFS_FIRST_FREE_OBJECTID :
3575 fprintf(stderr, "adding new data backref"
3576 " on %llu %s %llu owner %llu"
3577 " offset %llu found %d\n",
3578 (unsigned long long)rec->start,
3579 back->full_backref ?
3581 back->full_backref ?
3582 (unsigned long long)parent :
3583 (unsigned long long)dback->root,
3584 (unsigned long long)dback->owner,
3585 (unsigned long long)dback->offset,
3590 tback = (struct tree_backref *)back;
3591 if (back->full_backref)
3592 parent = tback->parent;
3596 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3597 rec->start, rec->max_size,
3598 parent, tback->root, 0, 0);
3599 fprintf(stderr, "adding new tree backref on "
3600 "start %llu len %llu parent %llu root %llu\n",
3601 rec->start, rec->max_size, tback->parent, tback->root);
3606 btrfs_release_path(NULL, path);
3611 * when an incorrect extent item is found, this will delete
3612 * all of the existing entries for it and recreate them
3613 * based on what the tree scan found.
3615 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3616 struct btrfs_fs_info *info,
3617 struct extent_record *rec)
3620 struct btrfs_path *path;
3621 struct list_head *cur = rec->backrefs.next;
3622 struct cache_extent *cache;
3623 struct extent_backref *back;
3627 /* remember our flags for recreating the extent */
3628 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3629 rec->max_size, rec->metadata, NULL,
3632 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3634 path = btrfs_alloc_path();
3636 /* step one, delete all the existing records */
3637 ret = delete_extent_records(trans, info->extent_root, path,
3638 rec->start, rec->max_size);
3643 /* was this block corrupt? If so, don't add references to it */
3644 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3650 /* step two, recreate all the refs we did find */
3651 while(cur != &rec->backrefs) {
3652 back = list_entry(cur, struct extent_backref, list);
3656 * if we didn't find any references, don't create a
3659 if (!back->found_ref)
3662 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3669 btrfs_free_path(path);
3673 /* right now we only prune from the extent allocation tree */
3674 static int prune_one_block(struct btrfs_trans_handle *trans,
3675 struct btrfs_fs_info *info,
3676 struct btrfs_corrupt_block *corrupt)
3679 struct btrfs_path path;
3680 struct extent_buffer *eb;
3684 int level = corrupt->level + 1;
3686 btrfs_init_path(&path);
3688 /* we want to stop at the parent to our busted block */
3689 path.lowest_level = level;
3691 ret = btrfs_search_slot(trans, info->extent_root,
3692 &corrupt->key, &path, -1, 1);
3697 eb = path.nodes[level];
3704 * hopefully the search gave us the block we want to prune,
3705 * lets try that first
3707 slot = path.slots[level];
3708 found = btrfs_node_blockptr(eb, slot);
3709 if (found == corrupt->cache.start)
3712 nritems = btrfs_header_nritems(eb);
3714 /* the search failed, lets scan this node and hope we find it */
3715 for (slot = 0; slot < nritems; slot++) {
3716 found = btrfs_node_blockptr(eb, slot);
3717 if (found == corrupt->cache.start)
3721 * we couldn't find the bad block. TODO, search all the nodes for pointers
3724 if (eb == info->extent_root->node) {
3729 btrfs_release_path(NULL, &path);
3734 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3735 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3738 btrfs_release_path(NULL, &path);
3742 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3743 struct btrfs_fs_info *info)
3745 struct cache_extent *cache;
3746 struct btrfs_corrupt_block *corrupt;
3748 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3752 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3753 prune_one_block(trans, info, corrupt);
3754 cache = next_cache_extent(cache);
3759 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3761 struct cache_extent *cache;
3762 struct btrfs_corrupt_block *corrupt;
3765 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3768 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3769 remove_cache_extent(info->corrupt_blocks, cache);
3774 static int check_block_group(struct btrfs_trans_handle *trans,
3775 struct btrfs_fs_info *info,
3776 struct map_lookup *map,
3779 struct btrfs_key key;
3780 struct btrfs_path path;
3783 key.objectid = map->ce.start;
3784 key.offset = map->ce.size;
3785 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3787 btrfs_init_path(&path);
3788 ret = btrfs_search_slot(NULL, info->extent_root,
3790 btrfs_release_path(NULL, &path);
3794 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3795 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3796 key.objectid, key.offset);
3802 static int check_block_groups(struct btrfs_trans_handle *trans,
3803 struct btrfs_fs_info *info, int *reinit)
3805 struct cache_extent *ce;
3806 struct map_lookup *map;
3807 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3809 /* this isn't quite working */
3812 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3816 map = container_of(ce, struct map_lookup, ce);
3817 check_block_group(trans, info, map, reinit);
3818 ce = next_cache_extent(ce);
3823 static int check_extent_refs(struct btrfs_trans_handle *trans,
3824 struct btrfs_root *root,
3825 struct cache_tree *extent_cache, int repair)
3827 struct extent_record *rec;
3828 struct cache_extent *cache;
3836 * if we're doing a repair, we have to make sure
3837 * we don't allocate from the problem extents.
3838 * In the worst case, this will be all the
3841 cache = find_first_cache_extent(extent_cache, 0);
3843 rec = container_of(cache, struct extent_record, cache);
3844 btrfs_pin_extent(root->fs_info,
3845 rec->start, rec->max_size);
3846 cache = next_cache_extent(cache);
3849 /* pin down all the corrupted blocks too */
3850 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3852 rec = container_of(cache, struct extent_record, cache);
3853 btrfs_pin_extent(root->fs_info,
3854 rec->start, rec->max_size);
3855 cache = next_cache_extent(cache);
3857 prune_corrupt_blocks(trans, root->fs_info);
3858 check_block_groups(trans, root->fs_info, &reinit);
3860 btrfs_read_block_groups(root->fs_info->extent_root);
3864 cache = find_first_cache_extent(extent_cache, 0);
3867 rec = container_of(cache, struct extent_record, cache);
3868 if (rec->refs != rec->extent_item_refs) {
3869 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3870 (unsigned long long)rec->start,
3871 (unsigned long long)rec->nr);
3872 fprintf(stderr, "extent item %llu, found %llu\n",
3873 (unsigned long long)rec->extent_item_refs,
3874 (unsigned long long)rec->refs);
3875 if (!fixed && repair) {
3876 ret = fixup_extent_refs(trans, root->fs_info, rec);
3884 if (all_backpointers_checked(rec, 1)) {
3885 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3886 (unsigned long long)rec->start,
3887 (unsigned long long)rec->nr);
3889 if (!fixed && repair) {
3890 ret = fixup_extent_refs(trans, root->fs_info, rec);
3898 if (!rec->owner_ref_checked) {
3899 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3900 (unsigned long long)rec->start,
3901 (unsigned long long)rec->nr);
3902 if (!fixed && repair) {
3903 ret = fixup_extent_refs(trans, root->fs_info, rec);
3911 remove_cache_extent(extent_cache, cache);
3912 free_all_extent_backrefs(rec);
3918 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3921 btrfs_fix_block_accounting(trans, root);
3924 fprintf(stderr, "repaired damaged extent references\n");
3930 static int check_extents(struct btrfs_trans_handle *trans,
3931 struct btrfs_root *root, int repair)
3933 struct cache_tree extent_cache;
3934 struct cache_tree seen;
3935 struct cache_tree pending;
3936 struct cache_tree reada;
3937 struct cache_tree nodes;
3938 struct cache_tree corrupt_blocks;
3939 struct btrfs_path path;
3940 struct btrfs_key key;
3941 struct btrfs_key found_key;
3944 struct block_info *bits;
3946 struct extent_buffer *leaf;
3948 struct btrfs_root_item ri;
3950 cache_tree_init(&extent_cache);
3951 cache_tree_init(&seen);
3952 cache_tree_init(&pending);
3953 cache_tree_init(&nodes);
3954 cache_tree_init(&reada);
3955 cache_tree_init(&corrupt_blocks);
3958 root->fs_info->fsck_extent_cache = &extent_cache;
3959 root->fs_info->free_extent_hook = free_extent_hook;
3960 root->fs_info->corrupt_blocks = &corrupt_blocks;
3964 bits = malloc(bits_nr * sizeof(struct block_info));
3970 add_root_to_pending(root->fs_info->tree_root->node,
3971 &extent_cache, &pending, &seen, &nodes,
3972 &root->fs_info->tree_root->root_key);
3974 add_root_to_pending(root->fs_info->chunk_root->node,
3975 &extent_cache, &pending, &seen, &nodes,
3976 &root->fs_info->chunk_root->root_key);
3978 btrfs_init_path(&path);
3981 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3982 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3986 leaf = path.nodes[0];
3987 slot = path.slots[0];
3988 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3989 ret = btrfs_next_leaf(root, &path);
3992 leaf = path.nodes[0];
3993 slot = path.slots[0];
3995 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3996 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3997 unsigned long offset;
3998 struct extent_buffer *buf;
4000 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
4001 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
4002 buf = read_tree_block(root->fs_info->tree_root,
4003 btrfs_root_bytenr(&ri),
4004 btrfs_level_size(root,
4005 btrfs_root_level(&ri)), 0);
4006 add_root_to_pending(buf, &extent_cache, &pending,
4007 &seen, &nodes, &found_key);
4008 free_extent_buffer(buf);
4012 btrfs_release_path(root, &path);
4014 ret = run_next_block(root, bits, bits_nr, &last, &pending,
4015 &seen, &reada, &nodes, &extent_cache);
4019 ret = check_extent_refs(trans, root, &extent_cache, repair);
4022 free_corrupt_blocks(root->fs_info);
4023 root->fs_info->fsck_extent_cache = NULL;
4024 root->fs_info->free_extent_hook = NULL;
4025 root->fs_info->corrupt_blocks = NULL;
4032 static struct option long_options[] = {
4033 { "super", 1, NULL, 's' },
4034 { "repair", 0, NULL, 0 },
4035 { "init-csum-tree", 0, NULL, 0 },
4036 { "init-extent-tree", 0, NULL, 0 },
4040 const char * const cmd_check_usage[] = {
4041 "btrfs check [options] <device>",
4042 "Check an unmounted btrfs filesystem.",
4044 "-s|--super <superblock> use this superblock copy",
4045 "--repair try to repair the filesystem",
4046 "--init-csum-tree create a new CRC tree",
4047 "--init-extent-tree create a new extent tree",
4051 int cmd_check(int argc, char **argv)
4053 struct cache_tree root_cache;
4054 struct btrfs_root *root;
4055 struct btrfs_fs_info *info;
4056 struct btrfs_trans_handle *trans = NULL;
4062 int option_index = 0;
4063 int init_csum_tree = 0;
4068 c = getopt_long(argc, argv, "as:", long_options,
4073 case 'a': /* ignored */ break;
4076 bytenr = btrfs_sb_offset(num);
4077 printf("using SB copy %d, bytenr %llu\n", num,
4078 (unsigned long long)bytenr);
4082 usage(cmd_check_usage);
4084 if (option_index == 1) {
4085 printf("enabling repair mode\n");
4088 } else if (option_index == 2) {
4089 printf("Creating a new CRC tree\n");
4095 argc = argc - optind;
4098 usage(cmd_check_usage);
4101 cache_tree_init(&root_cache);
4103 if((ret = check_mounted(argv[optind])) < 0) {
4104 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
4107 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
4111 info = open_ctree_fs_info(argv[optind], bytenr, 0, rw, 1);
4113 fprintf(stderr, "Couldn't open file system\n");
4117 uuid_unparse(info->super_copy->fsid, uuidbuf);
4118 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
4120 if (!extent_buffer_uptodate(info->tree_root->node) ||
4121 !extent_buffer_uptodate(info->dev_root->node) ||
4122 !extent_buffer_uptodate(info->extent_root->node) ||
4123 !extent_buffer_uptodate(info->chunk_root->node)) {
4124 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
4128 root = info->fs_root;
4130 fprintf(stderr, "checking extents\n");
4132 trans = btrfs_start_transaction(root, 1);
4134 if (init_csum_tree) {
4135 fprintf(stderr, "Reinit crc root\n");
4136 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
4138 fprintf(stderr, "crc root initialization failed\n");
4143 ret = check_extents(trans, root, repair);
4145 fprintf(stderr, "Errors found in extent allocation tree\n");
4147 fprintf(stderr, "checking free space cache\n");
4148 ret = check_space_cache(root);
4152 fprintf(stderr, "checking fs roots\n");
4153 ret = check_fs_roots(root, &root_cache);
4157 fprintf(stderr, "checking csums\n");
4158 ret = check_csums(root);
4162 fprintf(stderr, "checking root refs\n");
4163 ret = check_root_refs(root, &root_cache);
4165 free_root_recs(&root_cache);
4167 ret = btrfs_commit_transaction(trans, root);
4173 if (found_old_backref) { /*
4174 * there was a disk format change when mixed
4175 * backref was in testing tree. The old format
4176 * existed about one week.
4178 printf("\n * Found old mixed backref format. "
4179 "The old format is not supported! *"
4180 "\n * Please mount the FS in readonly mode, "
4181 "backup data and re-format the FS. *\n\n");
4184 printf("found %llu bytes used err is %d\n",
4185 (unsigned long long)bytes_used, ret);
4186 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
4187 printf("total tree bytes: %llu\n",
4188 (unsigned long long)total_btree_bytes);
4189 printf("total fs tree bytes: %llu\n",
4190 (unsigned long long)total_fs_tree_bytes);
4191 printf("total extent tree bytes: %llu\n",
4192 (unsigned long long)total_extent_tree_bytes);
4193 printf("btree space waste bytes: %llu\n",
4194 (unsigned long long)btree_space_waste);
4195 printf("file data blocks allocated: %llu\n referenced %llu\n",
4196 (unsigned long long)data_bytes_allocated,
4197 (unsigned long long)data_bytes_referenced);
4198 printf("%s\n", BTRFS_BUILD_VERSION);