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;
74 struct extent_backref node;
81 struct extent_record {
82 struct list_head backrefs;
83 struct cache_extent cache;
84 struct btrfs_disk_key parent_key;
93 unsigned int content_checked:1;
94 unsigned int owner_ref_checked:1;
95 unsigned int is_root:1;
96 unsigned int metadata:1;
99 struct inode_backref {
100 struct list_head list;
101 unsigned int found_dir_item:1;
102 unsigned int found_dir_index:1;
103 unsigned int found_inode_ref:1;
104 unsigned int filetype:8;
106 unsigned int ref_type;
113 #define REF_ERR_NO_DIR_ITEM (1 << 0)
114 #define REF_ERR_NO_DIR_INDEX (1 << 1)
115 #define REF_ERR_NO_INODE_REF (1 << 2)
116 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
117 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
118 #define REF_ERR_DUP_INODE_REF (1 << 5)
119 #define REF_ERR_INDEX_UNMATCH (1 << 6)
120 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
121 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
122 #define REF_ERR_NO_ROOT_REF (1 << 9)
123 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
124 #define REF_ERR_DUP_ROOT_REF (1 << 11)
125 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
127 struct inode_record {
128 struct list_head backrefs;
129 unsigned int checked:1;
130 unsigned int merging:1;
131 unsigned int found_inode_item:1;
132 unsigned int found_dir_item:1;
133 unsigned int found_file_extent:1;
134 unsigned int found_csum_item:1;
135 unsigned int some_csum_missing:1;
136 unsigned int nodatasum:1;
149 u64 first_extent_gap;
154 #define I_ERR_NO_INODE_ITEM (1 << 0)
155 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
156 #define I_ERR_DUP_INODE_ITEM (1 << 2)
157 #define I_ERR_DUP_DIR_INDEX (1 << 3)
158 #define I_ERR_ODD_DIR_ITEM (1 << 4)
159 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
160 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
161 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
162 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
163 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
164 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
165 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
166 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
167 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
169 struct root_backref {
170 struct list_head list;
171 unsigned int found_dir_item:1;
172 unsigned int found_dir_index:1;
173 unsigned int found_back_ref:1;
174 unsigned int found_forward_ref:1;
175 unsigned int reachable:1;
185 struct list_head backrefs;
186 struct cache_extent cache;
187 unsigned int found_root_item:1;
193 struct cache_extent cache;
198 struct cache_extent cache;
199 struct cache_tree root_cache;
200 struct cache_tree inode_cache;
201 struct inode_record *current;
210 struct walk_control {
211 struct cache_tree shared;
212 struct shared_node *nodes[BTRFS_MAX_LEVEL];
217 static u8 imode_to_type(u32 imode)
220 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
221 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
222 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
223 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
224 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
225 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
226 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
227 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
230 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
234 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
236 struct inode_record *rec;
237 struct inode_backref *backref;
238 struct inode_backref *orig;
241 rec = malloc(sizeof(*rec));
242 memcpy(rec, orig_rec, sizeof(*rec));
244 INIT_LIST_HEAD(&rec->backrefs);
246 list_for_each_entry(orig, &orig_rec->backrefs, list) {
247 size = sizeof(*orig) + orig->namelen + 1;
248 backref = malloc(size);
249 memcpy(backref, orig, size);
250 list_add_tail(&backref->list, &rec->backrefs);
255 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
258 struct ptr_node *node;
259 struct cache_extent *cache;
260 struct inode_record *rec = NULL;
263 cache = find_cache_extent(inode_cache, ino, 1);
265 node = container_of(cache, struct ptr_node, cache);
267 if (mod && rec->refs > 1) {
268 node->data = clone_inode_rec(rec);
273 rec = calloc(1, sizeof(*rec));
275 rec->extent_start = (u64)-1;
276 rec->first_extent_gap = (u64)-1;
278 INIT_LIST_HEAD(&rec->backrefs);
280 node = malloc(sizeof(*node));
281 node->cache.start = ino;
282 node->cache.size = 1;
285 if (ino == BTRFS_FREE_INO_OBJECTID)
288 ret = insert_existing_cache_extent(inode_cache, &node->cache);
294 static void free_inode_rec(struct inode_record *rec)
296 struct inode_backref *backref;
301 while (!list_empty(&rec->backrefs)) {
302 backref = list_entry(rec->backrefs.next,
303 struct inode_backref, list);
304 list_del(&backref->list);
310 static int can_free_inode_rec(struct inode_record *rec)
312 if (!rec->errors && rec->checked && rec->found_inode_item &&
313 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
318 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
319 struct inode_record *rec)
321 struct cache_extent *cache;
322 struct inode_backref *tmp, *backref;
323 struct ptr_node *node;
324 unsigned char filetype;
326 if (!rec->found_inode_item)
329 filetype = imode_to_type(rec->imode);
330 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
331 if (backref->found_dir_item && backref->found_dir_index) {
332 if (backref->filetype != filetype)
333 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
334 if (!backref->errors && backref->found_inode_ref) {
335 list_del(&backref->list);
341 if (!rec->checked || rec->merging)
344 if (S_ISDIR(rec->imode)) {
345 if (rec->found_size != rec->isize)
346 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
347 if (rec->found_file_extent)
348 rec->errors |= I_ERR_ODD_FILE_EXTENT;
349 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
350 if (rec->found_dir_item)
351 rec->errors |= I_ERR_ODD_DIR_ITEM;
352 if (rec->found_size != rec->nbytes)
353 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
354 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
355 rec->first_extent_gap = 0;
356 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
357 rec->first_extent_gap < rec->isize))
358 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
361 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
362 if (rec->found_csum_item && rec->nodatasum)
363 rec->errors |= I_ERR_ODD_CSUM_ITEM;
364 if (rec->some_csum_missing && !rec->nodatasum)
365 rec->errors |= I_ERR_SOME_CSUM_MISSING;
368 BUG_ON(rec->refs != 1);
369 if (can_free_inode_rec(rec)) {
370 cache = find_cache_extent(inode_cache, rec->ino, 1);
371 node = container_of(cache, struct ptr_node, cache);
372 BUG_ON(node->data != rec);
373 remove_cache_extent(inode_cache, &node->cache);
379 static int check_orphan_item(struct btrfs_root *root, u64 ino)
381 struct btrfs_path path;
382 struct btrfs_key key;
385 key.objectid = BTRFS_ORPHAN_OBJECTID;
386 key.type = BTRFS_ORPHAN_ITEM_KEY;
389 btrfs_init_path(&path);
390 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
391 btrfs_release_path(root, &path);
397 static int process_inode_item(struct extent_buffer *eb,
398 int slot, struct btrfs_key *key,
399 struct shared_node *active_node)
401 struct inode_record *rec;
402 struct btrfs_inode_item *item;
404 rec = active_node->current;
405 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
406 if (rec->found_inode_item) {
407 rec->errors |= I_ERR_DUP_INODE_ITEM;
410 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
411 rec->nlink = btrfs_inode_nlink(eb, item);
412 rec->isize = btrfs_inode_size(eb, item);
413 rec->nbytes = btrfs_inode_nbytes(eb, item);
414 rec->imode = btrfs_inode_mode(eb, item);
415 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
417 rec->found_inode_item = 1;
419 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
420 maybe_free_inode_rec(&active_node->inode_cache, rec);
424 static struct inode_backref *get_inode_backref(struct inode_record *rec,
426 int namelen, u64 dir)
428 struct inode_backref *backref;
430 list_for_each_entry(backref, &rec->backrefs, list) {
431 if (backref->dir != dir || backref->namelen != namelen)
433 if (memcmp(name, backref->name, namelen))
438 backref = malloc(sizeof(*backref) + namelen + 1);
439 memset(backref, 0, sizeof(*backref));
441 backref->namelen = namelen;
442 memcpy(backref->name, name, namelen);
443 backref->name[namelen] = '\0';
444 list_add_tail(&backref->list, &rec->backrefs);
448 static int add_inode_backref(struct cache_tree *inode_cache,
449 u64 ino, u64 dir, u64 index,
450 const char *name, int namelen,
451 int filetype, int itemtype, int errors)
453 struct inode_record *rec;
454 struct inode_backref *backref;
456 rec = get_inode_rec(inode_cache, ino, 1);
457 backref = get_inode_backref(rec, name, namelen, dir);
459 backref->errors |= errors;
460 if (itemtype == BTRFS_DIR_INDEX_KEY) {
461 if (backref->found_dir_index)
462 backref->errors |= REF_ERR_DUP_DIR_INDEX;
463 if (backref->found_inode_ref && backref->index != index)
464 backref->errors |= REF_ERR_INDEX_UNMATCH;
465 if (backref->found_dir_item && backref->filetype != filetype)
466 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
468 backref->index = index;
469 backref->filetype = filetype;
470 backref->found_dir_index = 1;
471 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
473 if (backref->found_dir_item)
474 backref->errors |= REF_ERR_DUP_DIR_ITEM;
475 if (backref->found_dir_index && backref->filetype != filetype)
476 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
478 backref->filetype = filetype;
479 backref->found_dir_item = 1;
480 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
481 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
482 if (backref->found_inode_ref)
483 backref->errors |= REF_ERR_DUP_INODE_REF;
484 if (backref->found_dir_index && backref->index != index)
485 backref->errors |= REF_ERR_INDEX_UNMATCH;
487 backref->ref_type = itemtype;
488 backref->index = index;
489 backref->found_inode_ref = 1;
494 maybe_free_inode_rec(inode_cache, rec);
498 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
499 struct cache_tree *dst_cache)
501 struct inode_backref *backref;
505 list_for_each_entry(backref, &src->backrefs, list) {
506 if (backref->found_dir_index) {
507 add_inode_backref(dst_cache, dst->ino, backref->dir,
508 backref->index, backref->name,
509 backref->namelen, backref->filetype,
510 BTRFS_DIR_INDEX_KEY, backref->errors);
512 if (backref->found_dir_item) {
514 add_inode_backref(dst_cache, dst->ino,
515 backref->dir, 0, backref->name,
516 backref->namelen, backref->filetype,
517 BTRFS_DIR_ITEM_KEY, backref->errors);
519 if (backref->found_inode_ref) {
520 add_inode_backref(dst_cache, dst->ino,
521 backref->dir, backref->index,
522 backref->name, backref->namelen, 0,
523 backref->ref_type, backref->errors);
527 if (src->found_dir_item)
528 dst->found_dir_item = 1;
529 if (src->found_file_extent)
530 dst->found_file_extent = 1;
531 if (src->found_csum_item)
532 dst->found_csum_item = 1;
533 if (src->some_csum_missing)
534 dst->some_csum_missing = 1;
535 if (dst->first_extent_gap > src->first_extent_gap)
536 dst->first_extent_gap = src->first_extent_gap;
538 BUG_ON(src->found_link < dir_count);
539 dst->found_link += src->found_link - dir_count;
540 dst->found_size += src->found_size;
541 if (src->extent_start != (u64)-1) {
542 if (dst->extent_start == (u64)-1) {
543 dst->extent_start = src->extent_start;
544 dst->extent_end = src->extent_end;
546 if (dst->extent_end > src->extent_start)
547 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
548 else if (dst->extent_end < src->extent_start &&
549 dst->extent_end < dst->first_extent_gap)
550 dst->first_extent_gap = dst->extent_end;
551 if (dst->extent_end < src->extent_end)
552 dst->extent_end = src->extent_end;
556 dst->errors |= src->errors;
557 if (src->found_inode_item) {
558 if (!dst->found_inode_item) {
559 dst->nlink = src->nlink;
560 dst->isize = src->isize;
561 dst->nbytes = src->nbytes;
562 dst->imode = src->imode;
563 dst->nodatasum = src->nodatasum;
564 dst->found_inode_item = 1;
566 dst->errors |= I_ERR_DUP_INODE_ITEM;
574 static int splice_shared_node(struct shared_node *src_node,
575 struct shared_node *dst_node)
577 struct cache_extent *cache;
578 struct ptr_node *node, *ins;
579 struct cache_tree *src, *dst;
580 struct inode_record *rec, *conflict;
585 if (--src_node->refs == 0)
587 if (src_node->current)
588 current_ino = src_node->current->ino;
590 src = &src_node->root_cache;
591 dst = &dst_node->root_cache;
593 cache = find_first_cache_extent(src, 0);
595 node = container_of(cache, struct ptr_node, cache);
597 cache = next_cache_extent(cache);
600 remove_cache_extent(src, &node->cache);
603 ins = malloc(sizeof(*ins));
604 ins->cache.start = node->cache.start;
605 ins->cache.size = node->cache.size;
609 ret = insert_existing_cache_extent(dst, &ins->cache);
610 if (ret == -EEXIST) {
611 conflict = get_inode_rec(dst, rec->ino, 1);
612 merge_inode_recs(rec, conflict, dst);
614 conflict->checked = 1;
615 if (dst_node->current == conflict)
616 dst_node->current = NULL;
618 maybe_free_inode_rec(dst, conflict);
626 if (src == &src_node->root_cache) {
627 src = &src_node->inode_cache;
628 dst = &dst_node->inode_cache;
632 if (current_ino > 0 && (!dst_node->current ||
633 current_ino > dst_node->current->ino)) {
634 if (dst_node->current) {
635 dst_node->current->checked = 1;
636 maybe_free_inode_rec(dst, dst_node->current);
638 dst_node->current = get_inode_rec(dst, current_ino, 1);
643 static void free_inode_recs(struct cache_tree *inode_cache)
645 struct cache_extent *cache;
646 struct ptr_node *node;
647 struct inode_record *rec;
650 cache = find_first_cache_extent(inode_cache, 0);
653 node = container_of(cache, struct ptr_node, cache);
655 remove_cache_extent(inode_cache, &node->cache);
661 static struct shared_node *find_shared_node(struct cache_tree *shared,
664 struct cache_extent *cache;
665 struct shared_node *node;
667 cache = find_cache_extent(shared, bytenr, 1);
669 node = container_of(cache, struct shared_node, cache);
675 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
678 struct shared_node *node;
680 node = calloc(1, sizeof(*node));
681 node->cache.start = bytenr;
682 node->cache.size = 1;
683 cache_tree_init(&node->root_cache);
684 cache_tree_init(&node->inode_cache);
687 ret = insert_existing_cache_extent(shared, &node->cache);
692 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
693 struct walk_control *wc, int level)
695 struct shared_node *node;
696 struct shared_node *dest;
698 if (level == wc->active_node)
701 BUG_ON(wc->active_node <= level);
702 node = find_shared_node(&wc->shared, bytenr);
704 add_shared_node(&wc->shared, bytenr, refs);
705 node = find_shared_node(&wc->shared, bytenr);
706 wc->nodes[level] = node;
707 wc->active_node = level;
711 if (wc->root_level == wc->active_node &&
712 btrfs_root_refs(&root->root_item) == 0) {
713 if (--node->refs == 0) {
714 free_inode_recs(&node->root_cache);
715 free_inode_recs(&node->inode_cache);
716 remove_cache_extent(&wc->shared, &node->cache);
722 dest = wc->nodes[wc->active_node];
723 splice_shared_node(node, dest);
724 if (node->refs == 0) {
725 remove_cache_extent(&wc->shared, &node->cache);
731 static int leave_shared_node(struct btrfs_root *root,
732 struct walk_control *wc, int level)
734 struct shared_node *node;
735 struct shared_node *dest;
738 if (level == wc->root_level)
741 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
745 BUG_ON(i >= BTRFS_MAX_LEVEL);
747 node = wc->nodes[wc->active_node];
748 wc->nodes[wc->active_node] = NULL;
751 dest = wc->nodes[wc->active_node];
752 if (wc->active_node < wc->root_level ||
753 btrfs_root_refs(&root->root_item) > 0) {
754 BUG_ON(node->refs <= 1);
755 splice_shared_node(node, dest);
757 BUG_ON(node->refs < 2);
763 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
766 struct btrfs_path path;
767 struct btrfs_key key;
768 struct extent_buffer *leaf;
772 btrfs_init_path(&path);
774 key.objectid = parent_root_id;
775 key.type = BTRFS_ROOT_REF_KEY;
776 key.offset = child_root_id;
777 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
780 btrfs_release_path(root, &path);
784 key.objectid = child_root_id;
785 key.type = BTRFS_ROOT_BACKREF_KEY;
787 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
792 leaf = path.nodes[0];
793 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
794 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
799 leaf = path.nodes[0];
802 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
803 if (key.objectid != child_root_id ||
804 key.type != BTRFS_ROOT_BACKREF_KEY)
809 if (key.offset == parent_root_id) {
810 btrfs_release_path(root, &path);
817 btrfs_release_path(root, &path);
818 return has_parent? 0 : -1;
821 static int process_dir_item(struct btrfs_root *root,
822 struct extent_buffer *eb,
823 int slot, struct btrfs_key *key,
824 struct shared_node *active_node)
834 struct btrfs_dir_item *di;
835 struct inode_record *rec;
836 struct cache_tree *root_cache;
837 struct cache_tree *inode_cache;
838 struct btrfs_key location;
839 char namebuf[BTRFS_NAME_LEN];
841 root_cache = &active_node->root_cache;
842 inode_cache = &active_node->inode_cache;
843 rec = active_node->current;
844 rec->found_dir_item = 1;
846 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
847 total = btrfs_item_size_nr(eb, slot);
848 while (cur < total) {
850 btrfs_dir_item_key_to_cpu(eb, di, &location);
851 name_len = btrfs_dir_name_len(eb, di);
852 data_len = btrfs_dir_data_len(eb, di);
853 filetype = btrfs_dir_type(eb, di);
855 rec->found_size += name_len;
856 if (name_len <= BTRFS_NAME_LEN) {
860 len = BTRFS_NAME_LEN;
861 error = REF_ERR_NAME_TOO_LONG;
863 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
865 if (location.type == BTRFS_INODE_ITEM_KEY) {
866 add_inode_backref(inode_cache, location.objectid,
867 key->objectid, key->offset, namebuf,
868 len, filetype, key->type, error);
869 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
870 add_inode_backref(root_cache, location.objectid,
871 key->objectid, key->offset,
872 namebuf, len, filetype,
875 fprintf(stderr, "warning line %d\n", __LINE__);
878 len = sizeof(*di) + name_len + data_len;
879 di = (struct btrfs_dir_item *)((char *)di + len);
882 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
883 rec->errors |= I_ERR_DUP_DIR_INDEX;
888 static int process_inode_ref(struct extent_buffer *eb,
889 int slot, struct btrfs_key *key,
890 struct shared_node *active_node)
898 struct cache_tree *inode_cache;
899 struct btrfs_inode_ref *ref;
900 char namebuf[BTRFS_NAME_LEN];
902 inode_cache = &active_node->inode_cache;
904 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
905 total = btrfs_item_size_nr(eb, slot);
906 while (cur < total) {
907 name_len = btrfs_inode_ref_name_len(eb, ref);
908 index = btrfs_inode_ref_index(eb, ref);
909 if (name_len <= BTRFS_NAME_LEN) {
913 len = BTRFS_NAME_LEN;
914 error = REF_ERR_NAME_TOO_LONG;
916 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
917 add_inode_backref(inode_cache, key->objectid, key->offset,
918 index, namebuf, len, 0, key->type, error);
920 len = sizeof(*ref) + name_len;
921 ref = (struct btrfs_inode_ref *)((char *)ref + len);
927 static int process_inode_extref(struct extent_buffer *eb,
928 int slot, struct btrfs_key *key,
929 struct shared_node *active_node)
938 struct cache_tree *inode_cache;
939 struct btrfs_inode_extref *extref;
940 char namebuf[BTRFS_NAME_LEN];
942 inode_cache = &active_node->inode_cache;
944 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
945 total = btrfs_item_size_nr(eb, slot);
946 while (cur < total) {
947 name_len = btrfs_inode_extref_name_len(eb, extref);
948 index = btrfs_inode_extref_index(eb, extref);
949 parent = btrfs_inode_extref_parent(eb, extref);
950 if (name_len <= BTRFS_NAME_LEN) {
954 len = BTRFS_NAME_LEN;
955 error = REF_ERR_NAME_TOO_LONG;
957 read_extent_buffer(eb, namebuf,
958 (unsigned long)(extref + 1), len);
959 add_inode_backref(inode_cache, key->objectid, parent,
960 index, namebuf, len, 0, key->type, error);
962 len = sizeof(*extref) + name_len;
963 extref = (struct btrfs_inode_extref *)((char *)extref + len);
970 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
972 struct btrfs_key key;
973 struct btrfs_path path;
974 struct extent_buffer *leaf;
979 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
981 btrfs_init_path(&path);
983 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
985 key.type = BTRFS_EXTENT_CSUM_KEY;
987 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
990 if (ret > 0 && path.slots[0] > 0) {
991 leaf = path.nodes[0];
992 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
993 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
994 key.type == BTRFS_EXTENT_CSUM_KEY)
999 leaf = path.nodes[0];
1000 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1001 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1005 leaf = path.nodes[0];
1008 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1009 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1010 key.type != BTRFS_EXTENT_CSUM_KEY)
1013 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1014 if (key.offset >= start + len)
1017 if (key.offset > start)
1020 size = btrfs_item_size_nr(leaf, path.slots[0]);
1021 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1022 if (csum_end > start) {
1023 size = min(csum_end - start, len);
1031 btrfs_release_path(root->fs_info->csum_root, &path);
1035 static int process_file_extent(struct btrfs_root *root,
1036 struct extent_buffer *eb,
1037 int slot, struct btrfs_key *key,
1038 struct shared_node *active_node)
1040 struct inode_record *rec;
1041 struct btrfs_file_extent_item *fi;
1043 u64 disk_bytenr = 0;
1044 u64 extent_offset = 0;
1045 u64 mask = root->sectorsize - 1;
1048 rec = active_node->current;
1049 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1050 rec->found_file_extent = 1;
1052 if (rec->extent_start == (u64)-1) {
1053 rec->extent_start = key->offset;
1054 rec->extent_end = key->offset;
1057 if (rec->extent_end > key->offset)
1058 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1059 else if (rec->extent_end < key->offset &&
1060 rec->extent_end < rec->first_extent_gap)
1061 rec->first_extent_gap = rec->extent_end;
1063 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1064 extent_type = btrfs_file_extent_type(eb, fi);
1066 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1067 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1069 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1070 rec->found_size += num_bytes;
1071 num_bytes = (num_bytes + mask) & ~mask;
1072 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1073 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1074 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1075 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1076 extent_offset = btrfs_file_extent_offset(eb, fi);
1077 if (num_bytes == 0 || (num_bytes & mask))
1078 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1079 if (num_bytes + extent_offset >
1080 btrfs_file_extent_ram_bytes(eb, fi))
1081 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1082 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1083 (btrfs_file_extent_compression(eb, fi) ||
1084 btrfs_file_extent_encryption(eb, fi) ||
1085 btrfs_file_extent_other_encoding(eb, fi)))
1086 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1087 if (disk_bytenr > 0)
1088 rec->found_size += num_bytes;
1090 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1092 rec->extent_end = key->offset + num_bytes;
1094 if (disk_bytenr > 0) {
1096 if (btrfs_file_extent_compression(eb, fi))
1097 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1099 disk_bytenr += extent_offset;
1101 found = count_csum_range(root, disk_bytenr, num_bytes);
1102 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1104 rec->found_csum_item = 1;
1105 if (found < num_bytes)
1106 rec->some_csum_missing = 1;
1107 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1109 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1115 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1116 struct walk_control *wc)
1118 struct btrfs_key key;
1122 struct cache_tree *inode_cache;
1123 struct shared_node *active_node;
1125 if (wc->root_level == wc->active_node &&
1126 btrfs_root_refs(&root->root_item) == 0)
1129 active_node = wc->nodes[wc->active_node];
1130 inode_cache = &active_node->inode_cache;
1131 nritems = btrfs_header_nritems(eb);
1132 for (i = 0; i < nritems; i++) {
1133 btrfs_item_key_to_cpu(eb, &key, i);
1135 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1138 if (active_node->current == NULL ||
1139 active_node->current->ino < key.objectid) {
1140 if (active_node->current) {
1141 active_node->current->checked = 1;
1142 maybe_free_inode_rec(inode_cache,
1143 active_node->current);
1145 active_node->current = get_inode_rec(inode_cache,
1149 case BTRFS_DIR_ITEM_KEY:
1150 case BTRFS_DIR_INDEX_KEY:
1151 ret = process_dir_item(root, eb, i, &key, active_node);
1153 case BTRFS_INODE_REF_KEY:
1154 ret = process_inode_ref(eb, i, &key, active_node);
1156 case BTRFS_INODE_EXTREF_KEY:
1157 ret = process_inode_extref(eb, i, &key, active_node);
1159 case BTRFS_INODE_ITEM_KEY:
1160 ret = process_inode_item(eb, i, &key, active_node);
1162 case BTRFS_EXTENT_DATA_KEY:
1163 ret = process_file_extent(root, eb, i, &key,
1173 static void reada_walk_down(struct btrfs_root *root,
1174 struct extent_buffer *node, int slot)
1184 level = btrfs_header_level(node);
1188 nritems = btrfs_header_nritems(node);
1189 blocksize = btrfs_level_size(root, level - 1);
1190 for (i = slot; i < nritems; i++) {
1191 bytenr = btrfs_node_blockptr(node, i);
1192 ptr_gen = btrfs_node_ptr_generation(node, i);
1193 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1199 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1200 struct walk_control *wc, int *level)
1204 struct extent_buffer *next;
1205 struct extent_buffer *cur;
1210 WARN_ON(*level < 0);
1211 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1212 ret = btrfs_lookup_extent_info(NULL, root,
1213 path->nodes[*level]->start,
1214 *level, 1, &refs, NULL);
1219 ret = enter_shared_node(root, path->nodes[*level]->start,
1225 while (*level >= 0) {
1226 WARN_ON(*level < 0);
1227 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1228 cur = path->nodes[*level];
1230 if (btrfs_header_level(cur) != *level)
1233 if (path->slots[*level] >= btrfs_header_nritems(cur))
1236 ret = process_one_leaf(root, cur, wc);
1239 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1240 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1241 blocksize = btrfs_level_size(root, *level - 1);
1242 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1248 ret = enter_shared_node(root, bytenr, refs,
1251 path->slots[*level]++;
1256 next = btrfs_find_tree_block(root, bytenr, blocksize);
1257 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1258 free_extent_buffer(next);
1259 reada_walk_down(root, cur, path->slots[*level]);
1260 next = read_tree_block(root, bytenr, blocksize,
1264 *level = *level - 1;
1265 free_extent_buffer(path->nodes[*level]);
1266 path->nodes[*level] = next;
1267 path->slots[*level] = 0;
1270 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1274 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1275 struct walk_control *wc, int *level)
1278 struct extent_buffer *leaf;
1280 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1281 leaf = path->nodes[i];
1282 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1287 free_extent_buffer(path->nodes[*level]);
1288 path->nodes[*level] = NULL;
1289 BUG_ON(*level > wc->active_node);
1290 if (*level == wc->active_node)
1291 leave_shared_node(root, wc, *level);
1298 static int check_root_dir(struct inode_record *rec)
1300 struct inode_backref *backref;
1303 if (!rec->found_inode_item || rec->errors)
1305 if (rec->nlink != 1 || rec->found_link != 0)
1307 if (list_empty(&rec->backrefs))
1309 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1310 if (!backref->found_inode_ref)
1312 if (backref->index != 0 || backref->namelen != 2 ||
1313 memcmp(backref->name, "..", 2))
1315 if (backref->found_dir_index || backref->found_dir_item)
1322 static int check_inode_recs(struct btrfs_root *root,
1323 struct cache_tree *inode_cache)
1325 struct cache_extent *cache;
1326 struct ptr_node *node;
1327 struct inode_record *rec;
1328 struct inode_backref *backref;
1331 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1333 if (btrfs_root_refs(&root->root_item) == 0) {
1334 if (!cache_tree_empty(inode_cache))
1335 fprintf(stderr, "warning line %d\n", __LINE__);
1339 rec = get_inode_rec(inode_cache, root_dirid, 0);
1341 ret = check_root_dir(rec);
1343 fprintf(stderr, "root %llu root dir %llu error\n",
1344 (unsigned long long)root->root_key.objectid,
1345 (unsigned long long)root_dirid);
1349 fprintf(stderr, "root %llu root dir %llu not found\n",
1350 (unsigned long long)root->root_key.objectid,
1351 (unsigned long long)root_dirid);
1355 cache = find_first_cache_extent(inode_cache, 0);
1358 node = container_of(cache, struct ptr_node, cache);
1360 remove_cache_extent(inode_cache, &node->cache);
1362 if (rec->ino == root_dirid ||
1363 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1364 free_inode_rec(rec);
1368 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1369 ret = check_orphan_item(root, rec->ino);
1371 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1372 if (can_free_inode_rec(rec)) {
1373 free_inode_rec(rec);
1379 if (!rec->found_inode_item)
1380 rec->errors |= I_ERR_NO_INODE_ITEM;
1381 if (rec->found_link != rec->nlink)
1382 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1383 fprintf(stderr, "root %llu inode %llu errors %x\n",
1384 (unsigned long long) root->root_key.objectid,
1385 (unsigned long long) rec->ino, rec->errors);
1386 list_for_each_entry(backref, &rec->backrefs, list) {
1387 if (!backref->found_dir_item)
1388 backref->errors |= REF_ERR_NO_DIR_ITEM;
1389 if (!backref->found_dir_index)
1390 backref->errors |= REF_ERR_NO_DIR_INDEX;
1391 if (!backref->found_inode_ref)
1392 backref->errors |= REF_ERR_NO_INODE_REF;
1393 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1394 " namelen %u name %s filetype %d error %x\n",
1395 (unsigned long long)backref->dir,
1396 (unsigned long long)backref->index,
1397 backref->namelen, backref->name,
1398 backref->filetype, backref->errors);
1400 free_inode_rec(rec);
1402 return (error > 0) ? -1 : 0;
1405 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1408 struct cache_extent *cache;
1409 struct root_record *rec = NULL;
1412 cache = find_cache_extent(root_cache, objectid, 1);
1414 rec = container_of(cache, struct root_record, cache);
1416 rec = calloc(1, sizeof(*rec));
1417 rec->objectid = objectid;
1418 INIT_LIST_HEAD(&rec->backrefs);
1419 rec->cache.start = objectid;
1420 rec->cache.size = 1;
1422 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1428 static struct root_backref *get_root_backref(struct root_record *rec,
1429 u64 ref_root, u64 dir, u64 index,
1430 const char *name, int namelen)
1432 struct root_backref *backref;
1434 list_for_each_entry(backref, &rec->backrefs, list) {
1435 if (backref->ref_root != ref_root || backref->dir != dir ||
1436 backref->namelen != namelen)
1438 if (memcmp(name, backref->name, namelen))
1443 backref = malloc(sizeof(*backref) + namelen + 1);
1444 memset(backref, 0, sizeof(*backref));
1445 backref->ref_root = ref_root;
1447 backref->index = index;
1448 backref->namelen = namelen;
1449 memcpy(backref->name, name, namelen);
1450 backref->name[namelen] = '\0';
1451 list_add_tail(&backref->list, &rec->backrefs);
1455 static void free_root_recs(struct cache_tree *root_cache)
1457 struct cache_extent *cache;
1458 struct root_record *rec;
1459 struct root_backref *backref;
1462 cache = find_first_cache_extent(root_cache, 0);
1465 rec = container_of(cache, struct root_record, cache);
1466 remove_cache_extent(root_cache, &rec->cache);
1468 while (!list_empty(&rec->backrefs)) {
1469 backref = list_entry(rec->backrefs.next,
1470 struct root_backref, list);
1471 list_del(&backref->list);
1478 static int add_root_backref(struct cache_tree *root_cache,
1479 u64 root_id, u64 ref_root, u64 dir, u64 index,
1480 const char *name, int namelen,
1481 int item_type, int errors)
1483 struct root_record *rec;
1484 struct root_backref *backref;
1486 rec = get_root_rec(root_cache, root_id);
1487 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1489 backref->errors |= errors;
1491 if (item_type != BTRFS_DIR_ITEM_KEY) {
1492 if (backref->found_dir_index || backref->found_back_ref ||
1493 backref->found_forward_ref) {
1494 if (backref->index != index)
1495 backref->errors |= REF_ERR_INDEX_UNMATCH;
1497 backref->index = index;
1501 if (item_type == BTRFS_DIR_ITEM_KEY) {
1502 backref->found_dir_item = 1;
1503 backref->reachable = 1;
1505 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1506 backref->found_dir_index = 1;
1507 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1508 if (backref->found_forward_ref)
1509 backref->errors |= REF_ERR_DUP_ROOT_REF;
1510 backref->found_forward_ref = 1;
1511 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1512 if (backref->found_back_ref)
1513 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1514 backref->found_back_ref = 1;
1522 static int merge_root_recs(struct btrfs_root *root,
1523 struct cache_tree *src_cache,
1524 struct cache_tree *dst_cache)
1526 struct cache_extent *cache;
1527 struct ptr_node *node;
1528 struct inode_record *rec;
1529 struct inode_backref *backref;
1531 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1532 free_inode_recs(src_cache);
1537 cache = find_first_cache_extent(src_cache, 0);
1540 node = container_of(cache, struct ptr_node, cache);
1542 remove_cache_extent(src_cache, &node->cache);
1545 if (!is_child_root(root, root->objectid, rec->ino))
1548 list_for_each_entry(backref, &rec->backrefs, list) {
1549 BUG_ON(backref->found_inode_ref);
1550 if (backref->found_dir_item)
1551 add_root_backref(dst_cache, rec->ino,
1552 root->root_key.objectid, backref->dir,
1553 backref->index, backref->name,
1554 backref->namelen, BTRFS_DIR_ITEM_KEY,
1556 if (backref->found_dir_index)
1557 add_root_backref(dst_cache, rec->ino,
1558 root->root_key.objectid, backref->dir,
1559 backref->index, backref->name,
1560 backref->namelen, BTRFS_DIR_INDEX_KEY,
1564 free_inode_rec(rec);
1569 static int check_root_refs(struct btrfs_root *root,
1570 struct cache_tree *root_cache)
1572 struct root_record *rec;
1573 struct root_record *ref_root;
1574 struct root_backref *backref;
1575 struct cache_extent *cache;
1581 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1584 /* fixme: this can not detect circular references */
1587 cache = find_first_cache_extent(root_cache, 0);
1591 rec = container_of(cache, struct root_record, cache);
1592 cache = next_cache_extent(cache);
1594 if (rec->found_ref == 0)
1597 list_for_each_entry(backref, &rec->backrefs, list) {
1598 if (!backref->reachable)
1601 ref_root = get_root_rec(root_cache,
1603 if (ref_root->found_ref > 0)
1606 backref->reachable = 0;
1608 if (rec->found_ref == 0)
1614 cache = find_first_cache_extent(root_cache, 0);
1618 rec = container_of(cache, struct root_record, cache);
1619 cache = next_cache_extent(cache);
1621 if (rec->found_ref == 0 &&
1622 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1623 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1624 ret = check_orphan_item(root->fs_info->tree_root,
1629 fprintf(stderr, "fs tree %llu not referenced\n",
1630 (unsigned long long)rec->objectid);
1634 if (rec->found_ref > 0 && !rec->found_root_item)
1636 list_for_each_entry(backref, &rec->backrefs, list) {
1637 if (!backref->found_dir_item)
1638 backref->errors |= REF_ERR_NO_DIR_ITEM;
1639 if (!backref->found_dir_index)
1640 backref->errors |= REF_ERR_NO_DIR_INDEX;
1641 if (!backref->found_back_ref)
1642 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1643 if (!backref->found_forward_ref)
1644 backref->errors |= REF_ERR_NO_ROOT_REF;
1645 if (backref->reachable && backref->errors)
1652 fprintf(stderr, "fs tree %llu refs %u %s\n",
1653 (unsigned long long)rec->objectid, rec->found_ref,
1654 rec->found_root_item ? "" : "not found");
1656 list_for_each_entry(backref, &rec->backrefs, list) {
1657 if (!backref->reachable)
1659 if (!backref->errors && rec->found_root_item)
1661 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1662 " index %llu namelen %u name %s error %x\n",
1663 (unsigned long long)backref->ref_root,
1664 (unsigned long long)backref->dir,
1665 (unsigned long long)backref->index,
1666 backref->namelen, backref->name,
1670 return errors > 0 ? 1 : 0;
1673 static int process_root_ref(struct extent_buffer *eb, int slot,
1674 struct btrfs_key *key,
1675 struct cache_tree *root_cache)
1681 struct btrfs_root_ref *ref;
1682 char namebuf[BTRFS_NAME_LEN];
1685 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1687 dirid = btrfs_root_ref_dirid(eb, ref);
1688 index = btrfs_root_ref_sequence(eb, ref);
1689 name_len = btrfs_root_ref_name_len(eb, ref);
1691 if (name_len <= BTRFS_NAME_LEN) {
1695 len = BTRFS_NAME_LEN;
1696 error = REF_ERR_NAME_TOO_LONG;
1698 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1700 if (key->type == BTRFS_ROOT_REF_KEY) {
1701 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1702 index, namebuf, len, key->type, error);
1704 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1705 index, namebuf, len, key->type, error);
1710 static int check_fs_root(struct btrfs_root *root,
1711 struct cache_tree *root_cache,
1712 struct walk_control *wc)
1717 struct btrfs_path path;
1718 struct shared_node root_node;
1719 struct root_record *rec;
1720 struct btrfs_root_item *root_item = &root->root_item;
1722 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1723 rec = get_root_rec(root_cache, root->root_key.objectid);
1724 if (btrfs_root_refs(root_item) > 0)
1725 rec->found_root_item = 1;
1728 btrfs_init_path(&path);
1729 memset(&root_node, 0, sizeof(root_node));
1730 cache_tree_init(&root_node.root_cache);
1731 cache_tree_init(&root_node.inode_cache);
1733 level = btrfs_header_level(root->node);
1734 memset(wc->nodes, 0, sizeof(wc->nodes));
1735 wc->nodes[level] = &root_node;
1736 wc->active_node = level;
1737 wc->root_level = level;
1739 if (btrfs_root_refs(root_item) > 0 ||
1740 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1741 path.nodes[level] = root->node;
1742 extent_buffer_get(root->node);
1743 path.slots[level] = 0;
1745 struct btrfs_key key;
1746 struct btrfs_disk_key found_key;
1748 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1749 level = root_item->drop_level;
1750 path.lowest_level = level;
1751 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1753 btrfs_node_key(path.nodes[level], &found_key,
1755 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1756 sizeof(found_key)));
1760 wret = walk_down_tree(root, &path, wc, &level);
1766 wret = walk_up_tree(root, &path, wc, &level);
1772 btrfs_release_path(root, &path);
1774 merge_root_recs(root, &root_node.root_cache, root_cache);
1776 if (root_node.current) {
1777 root_node.current->checked = 1;
1778 maybe_free_inode_rec(&root_node.inode_cache,
1782 ret = check_inode_recs(root, &root_node.inode_cache);
1786 static int fs_root_objectid(u64 objectid)
1788 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1789 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1790 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1791 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1792 objectid <= BTRFS_LAST_FREE_OBJECTID))
1797 static int check_fs_roots(struct btrfs_root *root,
1798 struct cache_tree *root_cache)
1800 struct btrfs_path path;
1801 struct btrfs_key key;
1802 struct walk_control wc;
1803 struct extent_buffer *leaf;
1804 struct btrfs_root *tmp_root;
1805 struct btrfs_root *tree_root = root->fs_info->tree_root;
1809 memset(&wc, 0, sizeof(wc));
1810 cache_tree_init(&wc.shared);
1811 btrfs_init_path(&path);
1815 key.type = BTRFS_ROOT_ITEM_KEY;
1816 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1819 leaf = path.nodes[0];
1820 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1821 ret = btrfs_next_leaf(tree_root, &path);
1824 leaf = path.nodes[0];
1826 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1827 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1828 fs_root_objectid(key.objectid)) {
1829 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1831 if (IS_ERR(tmp_root)) {
1835 ret = check_fs_root(tmp_root, root_cache, &wc);
1838 btrfs_free_fs_root(root->fs_info, tmp_root);
1839 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1840 key.type == BTRFS_ROOT_BACKREF_KEY) {
1841 process_root_ref(leaf, path.slots[0], &key,
1847 btrfs_release_path(tree_root, &path);
1849 if (!cache_tree_empty(&wc.shared))
1850 fprintf(stderr, "warning line %d\n", __LINE__);
1855 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1857 struct list_head *cur = rec->backrefs.next;
1858 struct extent_backref *back;
1859 struct tree_backref *tback;
1860 struct data_backref *dback;
1864 while(cur != &rec->backrefs) {
1865 back = list_entry(cur, struct extent_backref, list);
1867 if (!back->found_extent_tree) {
1871 if (back->is_data) {
1872 dback = (struct data_backref *)back;
1873 fprintf(stderr, "Backref %llu %s %llu"
1874 " owner %llu offset %llu num_refs %lu"
1875 " not found in extent tree\n",
1876 (unsigned long long)rec->start,
1877 back->full_backref ?
1879 back->full_backref ?
1880 (unsigned long long)dback->parent:
1881 (unsigned long long)dback->root,
1882 (unsigned long long)dback->owner,
1883 (unsigned long long)dback->offset,
1884 (unsigned long)dback->num_refs);
1886 tback = (struct tree_backref *)back;
1887 fprintf(stderr, "Backref %llu parent %llu"
1888 " root %llu not found in extent tree\n",
1889 (unsigned long long)rec->start,
1890 (unsigned long long)tback->parent,
1891 (unsigned long long)tback->root);
1894 if (!back->is_data && !back->found_ref) {
1898 tback = (struct tree_backref *)back;
1899 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1900 (unsigned long long)rec->start,
1901 back->full_backref ? "parent" : "root",
1902 back->full_backref ?
1903 (unsigned long long)tback->parent :
1904 (unsigned long long)tback->root, back);
1906 if (back->is_data) {
1907 dback = (struct data_backref *)back;
1908 if (dback->found_ref != dback->num_refs) {
1912 fprintf(stderr, "Incorrect local backref count"
1913 " on %llu %s %llu owner %llu"
1914 " offset %llu found %u wanted %u back %p\n",
1915 (unsigned long long)rec->start,
1916 back->full_backref ?
1918 back->full_backref ?
1919 (unsigned long long)dback->parent:
1920 (unsigned long long)dback->root,
1921 (unsigned long long)dback->owner,
1922 (unsigned long long)dback->offset,
1923 dback->found_ref, dback->num_refs, back);
1926 if (!back->is_data) {
1929 dback = (struct data_backref *)back;
1930 found += dback->found_ref;
1933 if (found != rec->refs) {
1937 fprintf(stderr, "Incorrect global backref count "
1938 "on %llu found %llu wanted %llu\n",
1939 (unsigned long long)rec->start,
1940 (unsigned long long)found,
1941 (unsigned long long)rec->refs);
1947 static int free_all_extent_backrefs(struct extent_record *rec)
1949 struct extent_backref *back;
1950 struct list_head *cur;
1951 while (!list_empty(&rec->backrefs)) {
1952 cur = rec->backrefs.next;
1953 back = list_entry(cur, struct extent_backref, list);
1960 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
1961 struct extent_record *rec)
1963 if (rec->content_checked && rec->owner_ref_checked &&
1964 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
1965 !all_backpointers_checked(rec, 0)) {
1966 remove_cache_extent(extent_cache, &rec->cache);
1967 free_all_extent_backrefs(rec);
1973 static int check_owner_ref(struct btrfs_root *root,
1974 struct extent_record *rec,
1975 struct extent_buffer *buf)
1977 struct extent_backref *node;
1978 struct tree_backref *back;
1979 struct btrfs_root *ref_root;
1980 struct btrfs_key key;
1981 struct btrfs_path path;
1982 struct extent_buffer *parent;
1987 list_for_each_entry(node, &rec->backrefs, list) {
1990 if (!node->found_ref)
1992 if (node->full_backref)
1994 back = (struct tree_backref *)node;
1995 if (btrfs_header_owner(buf) == back->root)
1998 BUG_ON(rec->is_root);
2000 /* try to find the block by search corresponding fs tree */
2001 key.objectid = btrfs_header_owner(buf);
2002 key.type = BTRFS_ROOT_ITEM_KEY;
2003 key.offset = (u64)-1;
2005 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2006 if (IS_ERR(ref_root))
2009 level = btrfs_header_level(buf);
2011 btrfs_item_key_to_cpu(buf, &key, 0);
2013 btrfs_node_key_to_cpu(buf, &key, 0);
2015 btrfs_init_path(&path);
2016 path.lowest_level = level + 1;
2017 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2021 parent = path.nodes[level + 1];
2022 if (parent && buf->start == btrfs_node_blockptr(parent,
2023 path.slots[level + 1]))
2026 btrfs_release_path(ref_root, &path);
2027 return found ? 0 : 1;
2030 static int is_extent_tree_record(struct extent_record *rec)
2032 struct list_head *cur = rec->backrefs.next;
2033 struct extent_backref *node;
2034 struct tree_backref *back;
2037 while(cur != &rec->backrefs) {
2038 node = list_entry(cur, struct extent_backref, list);
2042 back = (struct tree_backref *)node;
2043 if (node->full_backref)
2045 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2052 static int record_bad_block_io(struct btrfs_fs_info *info,
2053 struct cache_tree *extent_cache,
2056 struct extent_record *rec;
2057 struct cache_extent *cache;
2058 struct btrfs_key key;
2060 cache = find_cache_extent(extent_cache, start, len);
2064 rec = container_of(cache, struct extent_record, cache);
2065 if (!is_extent_tree_record(rec))
2068 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2069 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2072 static int check_block(struct btrfs_root *root,
2073 struct cache_tree *extent_cache,
2074 struct extent_buffer *buf, u64 flags)
2076 struct extent_record *rec;
2077 struct cache_extent *cache;
2078 struct btrfs_key key;
2082 cache = find_cache_extent(extent_cache, buf->start, buf->len);
2085 rec = container_of(cache, struct extent_record, cache);
2086 rec->generation = btrfs_header_generation(buf);
2088 level = btrfs_header_level(buf);
2089 if (btrfs_header_nritems(buf) > 0) {
2092 btrfs_item_key_to_cpu(buf, &key, 0);
2094 btrfs_node_key_to_cpu(buf, &key, 0);
2096 rec->info_objectid = key.objectid;
2098 rec->info_level = level;
2100 if (btrfs_is_leaf(buf))
2101 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2103 ret = btrfs_check_node(root, &rec->parent_key, buf);
2106 fprintf(stderr, "bad block %llu\n",
2107 (unsigned long long)buf->start);
2109 rec->content_checked = 1;
2110 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2111 rec->owner_ref_checked = 1;
2113 ret = check_owner_ref(root, rec, buf);
2115 rec->owner_ref_checked = 1;
2119 maybe_free_extent_rec(extent_cache, rec);
2123 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2124 u64 parent, u64 root)
2126 struct list_head *cur = rec->backrefs.next;
2127 struct extent_backref *node;
2128 struct tree_backref *back;
2130 while(cur != &rec->backrefs) {
2131 node = list_entry(cur, struct extent_backref, list);
2135 back = (struct tree_backref *)node;
2137 if (!node->full_backref)
2139 if (parent == back->parent)
2142 if (node->full_backref)
2144 if (back->root == root)
2151 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2152 u64 parent, u64 root)
2154 struct tree_backref *ref = malloc(sizeof(*ref));
2155 memset(&ref->node, 0, sizeof(ref->node));
2157 ref->parent = parent;
2158 ref->node.full_backref = 1;
2161 ref->node.full_backref = 0;
2163 list_add_tail(&ref->node.list, &rec->backrefs);
2168 static struct data_backref *find_data_backref(struct extent_record *rec,
2169 u64 parent, u64 root,
2170 u64 owner, u64 offset)
2172 struct list_head *cur = rec->backrefs.next;
2173 struct extent_backref *node;
2174 struct data_backref *back;
2176 while(cur != &rec->backrefs) {
2177 node = list_entry(cur, struct extent_backref, list);
2181 back = (struct data_backref *)node;
2183 if (!node->full_backref)
2185 if (parent == back->parent)
2188 if (node->full_backref)
2190 if (back->root == root && back->owner == owner &&
2191 back->offset == offset)
2198 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2199 u64 parent, u64 root,
2200 u64 owner, u64 offset,
2203 struct data_backref *ref = malloc(sizeof(*ref));
2204 memset(&ref->node, 0, sizeof(ref->node));
2205 ref->node.is_data = 1;
2208 ref->parent = parent;
2211 ref->node.full_backref = 1;
2215 ref->offset = offset;
2216 ref->node.full_backref = 0;
2220 list_add_tail(&ref->node.list, &rec->backrefs);
2221 if (max_size > rec->max_size)
2222 rec->max_size = max_size;
2226 static int add_extent_rec(struct cache_tree *extent_cache,
2227 struct btrfs_key *parent_key,
2228 u64 start, u64 nr, u64 extent_item_refs,
2229 int is_root, int inc_ref, int set_checked,
2230 int metadata, u64 max_size)
2232 struct extent_record *rec;
2233 struct cache_extent *cache;
2236 cache = find_cache_extent(extent_cache, start, nr);
2238 rec = container_of(cache, struct extent_record, cache);
2242 rec->nr = max(nr, max_size);
2244 if (start != rec->start) {
2245 fprintf(stderr, "warning, start mismatch %llu %llu\n",
2246 (unsigned long long)rec->start,
2247 (unsigned long long)start);
2250 if (extent_item_refs) {
2251 if (rec->extent_item_refs) {
2252 fprintf(stderr, "block %llu rec "
2253 "extent_item_refs %llu, passed %llu\n",
2254 (unsigned long long)start,
2255 (unsigned long long)
2256 rec->extent_item_refs,
2257 (unsigned long long)extent_item_refs);
2259 rec->extent_item_refs = extent_item_refs;
2264 rec->content_checked = 1;
2265 rec->owner_ref_checked = 1;
2269 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2271 if (rec->max_size < max_size)
2272 rec->max_size = max_size;
2274 maybe_free_extent_rec(extent_cache, rec);
2277 rec = malloc(sizeof(*rec));
2279 rec->max_size = max_size;
2280 rec->nr = max(nr, max_size);
2281 rec->content_checked = 0;
2282 rec->owner_ref_checked = 0;
2283 rec->metadata = metadata;
2284 INIT_LIST_HEAD(&rec->backrefs);
2296 if (extent_item_refs)
2297 rec->extent_item_refs = extent_item_refs;
2299 rec->extent_item_refs = 0;
2302 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2304 memset(&rec->parent_key, 0, sizeof(*parent_key));
2306 rec->cache.start = start;
2307 rec->cache.size = nr;
2308 ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2312 rec->content_checked = 1;
2313 rec->owner_ref_checked = 1;
2318 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2319 u64 parent, u64 root, int found_ref)
2321 struct extent_record *rec;
2322 struct tree_backref *back;
2323 struct cache_extent *cache;
2325 cache = find_cache_extent(extent_cache, bytenr, 1);
2327 add_extent_rec(extent_cache, NULL, bytenr,
2328 1, 0, 0, 0, 0, 1, 0);
2329 cache = find_cache_extent(extent_cache, bytenr, 1);
2334 rec = container_of(cache, struct extent_record, cache);
2335 if (rec->start != bytenr) {
2339 back = find_tree_backref(rec, parent, root);
2341 back = alloc_tree_backref(rec, parent, root);
2344 if (back->node.found_ref) {
2345 fprintf(stderr, "Extent back ref already exists "
2346 "for %llu parent %llu root %llu \n",
2347 (unsigned long long)bytenr,
2348 (unsigned long long)parent,
2349 (unsigned long long)root);
2351 back->node.found_ref = 1;
2353 if (back->node.found_extent_tree) {
2354 fprintf(stderr, "Extent back ref already exists "
2355 "for %llu parent %llu root %llu \n",
2356 (unsigned long long)bytenr,
2357 (unsigned long long)parent,
2358 (unsigned long long)root);
2360 back->node.found_extent_tree = 1;
2365 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2366 u64 parent, u64 root, u64 owner, u64 offset,
2367 u32 num_refs, int found_ref, u64 max_size)
2369 struct extent_record *rec;
2370 struct data_backref *back;
2371 struct cache_extent *cache;
2373 cache = find_cache_extent(extent_cache, bytenr, 1);
2375 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2377 cache = find_cache_extent(extent_cache, bytenr, 1);
2382 rec = container_of(cache, struct extent_record, cache);
2383 if (rec->start != bytenr) {
2386 if (rec->max_size < max_size)
2387 rec->max_size = max_size;
2389 back = find_data_backref(rec, parent, root, owner, offset);
2391 back = alloc_data_backref(rec, parent, root, owner, offset,
2395 BUG_ON(num_refs != 1);
2396 back->node.found_ref = 1;
2397 back->found_ref += 1;
2399 if (back->node.found_extent_tree) {
2400 fprintf(stderr, "Extent back ref already exists "
2401 "for %llu parent %llu root %llu"
2402 "owner %llu offset %llu num_refs %lu\n",
2403 (unsigned long long)bytenr,
2404 (unsigned long long)parent,
2405 (unsigned long long)root,
2406 (unsigned long long)owner,
2407 (unsigned long long)offset,
2408 (unsigned long)num_refs);
2410 back->num_refs = num_refs;
2411 back->node.found_extent_tree = 1;
2416 static int add_pending(struct cache_tree *pending,
2417 struct cache_tree *seen, u64 bytenr, u32 size)
2420 ret = insert_cache_extent(seen, bytenr, size);
2423 insert_cache_extent(pending, bytenr, size);
2427 static int pick_next_pending(struct cache_tree *pending,
2428 struct cache_tree *reada,
2429 struct cache_tree *nodes,
2430 u64 last, struct block_info *bits, int bits_nr,
2433 unsigned long node_start = last;
2434 struct cache_extent *cache;
2437 cache = find_first_cache_extent(reada, 0);
2439 bits[0].start = cache->start;
2440 bits[1].size = cache->size;
2445 if (node_start > 32768)
2446 node_start -= 32768;
2448 cache = find_first_cache_extent(nodes, node_start);
2450 cache = find_first_cache_extent(nodes, 0);
2453 cache = find_first_cache_extent(pending, 0);
2458 bits[ret].start = cache->start;
2459 bits[ret].size = cache->size;
2460 cache = next_cache_extent(cache);
2462 } while (cache && ret < bits_nr);
2468 bits[ret].start = cache->start;
2469 bits[ret].size = cache->size;
2470 cache = next_cache_extent(cache);
2472 } while (cache && ret < bits_nr);
2474 if (bits_nr - ret > 8) {
2475 u64 lookup = bits[0].start + bits[0].size;
2476 struct cache_extent *next;
2477 next = find_first_cache_extent(pending, lookup);
2479 if (next->start - lookup > 32768)
2481 bits[ret].start = next->start;
2482 bits[ret].size = next->size;
2483 lookup = next->start + next->size;
2487 next = next_cache_extent(next);
2495 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2496 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2497 struct extent_buffer *leaf, int slot)
2499 struct btrfs_extent_ref_v0 *ref0;
2500 struct btrfs_key key;
2502 btrfs_item_key_to_cpu(leaf, &key, slot);
2503 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2504 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2505 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2507 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2508 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2514 static int process_extent_item(struct btrfs_root *root,
2515 struct cache_tree *extent_cache,
2516 struct extent_buffer *eb, int slot)
2518 struct btrfs_extent_item *ei;
2519 struct btrfs_extent_inline_ref *iref;
2520 struct btrfs_extent_data_ref *dref;
2521 struct btrfs_shared_data_ref *sref;
2522 struct btrfs_key key;
2526 u32 item_size = btrfs_item_size_nr(eb, slot);
2532 btrfs_item_key_to_cpu(eb, &key, slot);
2534 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2536 num_bytes = root->leafsize;
2538 num_bytes = key.offset;
2541 if (item_size < sizeof(*ei)) {
2542 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2543 struct btrfs_extent_item_v0 *ei0;
2544 BUG_ON(item_size != sizeof(*ei0));
2545 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2546 refs = btrfs_extent_refs_v0(eb, ei0);
2550 return add_extent_rec(extent_cache, NULL, key.objectid,
2551 num_bytes, refs, 0, 0, 0, metadata,
2555 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2556 refs = btrfs_extent_refs(eb, ei);
2558 add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
2559 refs, 0, 0, 0, metadata, num_bytes);
2561 ptr = (unsigned long)(ei + 1);
2562 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
2563 key.type == BTRFS_EXTENT_ITEM_KEY)
2564 ptr += sizeof(struct btrfs_tree_block_info);
2566 end = (unsigned long)ei + item_size;
2568 iref = (struct btrfs_extent_inline_ref *)ptr;
2569 type = btrfs_extent_inline_ref_type(eb, iref);
2570 offset = btrfs_extent_inline_ref_offset(eb, iref);
2572 case BTRFS_TREE_BLOCK_REF_KEY:
2573 add_tree_backref(extent_cache, key.objectid,
2576 case BTRFS_SHARED_BLOCK_REF_KEY:
2577 add_tree_backref(extent_cache, key.objectid,
2580 case BTRFS_EXTENT_DATA_REF_KEY:
2581 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2582 add_data_backref(extent_cache, key.objectid, 0,
2583 btrfs_extent_data_ref_root(eb, dref),
2584 btrfs_extent_data_ref_objectid(eb,
2586 btrfs_extent_data_ref_offset(eb, dref),
2587 btrfs_extent_data_ref_count(eb, dref),
2590 case BTRFS_SHARED_DATA_REF_KEY:
2591 sref = (struct btrfs_shared_data_ref *)(iref + 1);
2592 add_data_backref(extent_cache, key.objectid, offset,
2594 btrfs_shared_data_ref_count(eb, sref),
2598 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2599 key.objectid, key.type, num_bytes);
2602 ptr += btrfs_extent_inline_ref_size(type);
2609 static int check_cache_range(struct btrfs_root *root,
2610 struct btrfs_block_group_cache *cache,
2611 u64 offset, u64 bytes)
2613 struct btrfs_free_space *entry;
2619 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2620 bytenr = btrfs_sb_offset(i);
2621 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
2622 cache->key.objectid, bytenr, 0,
2623 &logical, &nr, &stripe_len);
2628 if (logical[nr] + stripe_len <= offset)
2630 if (offset + bytes <= logical[nr])
2632 if (logical[nr] == offset) {
2633 if (stripe_len >= bytes) {
2637 bytes -= stripe_len;
2638 offset += stripe_len;
2639 } else if (logical[nr] < offset) {
2640 if (logical[nr] + stripe_len >=
2645 bytes = (offset + bytes) -
2646 (logical[nr] + stripe_len);
2647 offset = logical[nr] + stripe_len;
2650 * Could be tricky, the super may land in the
2651 * middle of the area we're checking. First
2652 * check the easiest case, it's at the end.
2654 if (logical[nr] + stripe_len >=
2656 bytes = logical[nr] - offset;
2660 /* Check the left side */
2661 ret = check_cache_range(root, cache,
2663 logical[nr] - offset);
2669 /* Now we continue with the right side */
2670 bytes = (offset + bytes) -
2671 (logical[nr] + stripe_len);
2672 offset = logical[nr] + stripe_len;
2679 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
2681 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
2682 offset, offset+bytes);
2686 if (entry->offset != offset) {
2687 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
2692 if (entry->bytes != bytes) {
2693 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
2694 bytes, entry->bytes, offset);
2698 unlink_free_space(cache->free_space_ctl, entry);
2703 static int verify_space_cache(struct btrfs_root *root,
2704 struct btrfs_block_group_cache *cache)
2706 struct btrfs_path *path;
2707 struct extent_buffer *leaf;
2708 struct btrfs_key key;
2712 path = btrfs_alloc_path();
2716 root = root->fs_info->extent_root;
2718 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
2720 key.objectid = last;
2722 key.type = BTRFS_EXTENT_ITEM_KEY;
2724 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2729 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2730 ret = btrfs_next_leaf(root, path);
2738 leaf = path->nodes[0];
2739 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2740 if (key.objectid >= cache->key.offset + cache->key.objectid)
2742 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2743 key.type != BTRFS_METADATA_ITEM_KEY) {
2748 if (last == key.objectid) {
2749 last = key.objectid + key.offset;
2754 ret = check_cache_range(root, cache, last,
2755 key.objectid - last);
2758 if (key.type == BTRFS_EXTENT_ITEM_KEY)
2759 last = key.objectid + key.offset;
2761 last = key.objectid + root->leafsize;
2765 if (last < cache->key.objectid + cache->key.offset)
2766 ret = check_cache_range(root, cache, last,
2767 cache->key.objectid +
2768 cache->key.offset - last);
2769 btrfs_free_path(path);
2772 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
2773 fprintf(stderr, "There are still entries left in the space "
2781 static int check_space_cache(struct btrfs_root *root)
2783 struct btrfs_block_group_cache *cache;
2784 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
2788 if (btrfs_super_generation(root->fs_info->super_copy) !=
2789 btrfs_super_cache_generation(root->fs_info->super_copy)) {
2790 printf("cache and super generation don't match, space cache "
2791 "will be invalidated\n");
2796 cache = btrfs_lookup_first_block_group(root->fs_info, start);
2800 start = cache->key.objectid + cache->key.offset;
2801 if (!cache->free_space_ctl) {
2802 if (btrfs_init_free_space_ctl(cache,
2808 btrfs_remove_free_space_cache(cache);
2811 ret = load_free_space_cache(root->fs_info, cache);
2815 ret = verify_space_cache(root, cache);
2817 fprintf(stderr, "cache appears valid but isnt %Lu\n",
2818 cache->key.objectid);
2823 return error ? -EINVAL : 0;
2826 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
2829 struct btrfs_path *path;
2830 struct extent_buffer *leaf;
2831 struct btrfs_key key;
2834 path = btrfs_alloc_path();
2836 fprintf(stderr, "Error allocing path\n");
2840 key.objectid = bytenr;
2841 key.type = BTRFS_EXTENT_ITEM_KEY;
2845 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
2848 fprintf(stderr, "Error looking up extent record %d\n", ret);
2849 btrfs_free_path(path);
2851 } else if (ret && path->slots[0]) {
2856 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2857 ret = btrfs_next_leaf(root, path);
2859 fprintf(stderr, "Error going to next leaf "
2861 btrfs_free_path(path);
2867 leaf = path->nodes[0];
2868 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2869 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
2873 if (key.objectid + key.offset < bytenr) {
2877 if (key.objectid > bytenr + num_bytes)
2880 if (key.objectid == bytenr) {
2881 if (key.offset >= num_bytes) {
2885 num_bytes -= key.offset;
2886 bytenr += key.offset;
2887 } else if (key.objectid < bytenr) {
2888 if (key.objectid + key.offset >= bytenr + num_bytes) {
2892 num_bytes = (bytenr + num_bytes) -
2893 (key.objectid + key.offset);
2894 bytenr = key.objectid + key.offset;
2896 if (key.objectid + key.offset < bytenr + num_bytes) {
2897 u64 new_start = key.objectid + key.offset;
2898 u64 new_bytes = bytenr + num_bytes - new_start;
2901 * Weird case, the extent is in the middle of
2902 * our range, we'll have to search one side
2903 * and then the other. Not sure if this happens
2904 * in real life, but no harm in coding it up
2905 * anyway just in case.
2907 btrfs_release_path(root, path);
2908 ret = check_extent_exists(root, new_start,
2911 fprintf(stderr, "Right section didn't "
2915 num_bytes = key.objectid - bytenr;
2918 num_bytes = key.objectid - bytenr;
2925 fprintf(stderr, "There are no extents for csum range "
2926 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
2930 btrfs_free_path(path);
2934 static int check_csums(struct btrfs_root *root)
2936 struct btrfs_path *path;
2937 struct extent_buffer *leaf;
2938 struct btrfs_key key;
2939 u64 offset = 0, num_bytes = 0;
2940 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
2944 root = root->fs_info->csum_root;
2946 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2947 key.type = BTRFS_EXTENT_CSUM_KEY;
2950 path = btrfs_alloc_path();
2954 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2956 fprintf(stderr, "Error searching csum tree %d\n", ret);
2957 btrfs_free_path(path);
2961 if (ret > 0 && path->slots[0])
2966 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2967 ret = btrfs_next_leaf(root, path);
2969 fprintf(stderr, "Error going to next leaf "
2976 leaf = path->nodes[0];
2978 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2979 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
2985 offset = key.offset;
2986 } else if (key.offset != offset + num_bytes) {
2987 ret = check_extent_exists(root, offset, num_bytes);
2989 fprintf(stderr, "Csum exists for %Lu-%Lu but "
2990 "there is no extent record\n",
2991 offset, offset+num_bytes);
2994 offset = key.offset;
2998 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
2999 csum_size) * root->sectorsize;
3003 btrfs_free_path(path);
3007 static int run_next_block(struct btrfs_root *root,
3008 struct block_info *bits,
3011 struct cache_tree *pending,
3012 struct cache_tree *seen,
3013 struct cache_tree *reada,
3014 struct cache_tree *nodes,
3015 struct cache_tree *extent_cache)
3017 struct extent_buffer *buf;
3026 struct btrfs_key key;
3027 struct cache_extent *cache;
3030 ret = pick_next_pending(pending, reada, nodes, *last, bits,
3031 bits_nr, &reada_bits);
3036 for(i = 0; i < ret; i++) {
3037 insert_cache_extent(reada, bits[i].start,
3040 /* fixme, get the parent transid */
3041 readahead_tree_block(root, bits[i].start,
3045 *last = bits[0].start;
3046 bytenr = bits[0].start;
3047 size = bits[0].size;
3049 cache = find_cache_extent(pending, bytenr, size);
3051 remove_cache_extent(pending, cache);
3054 cache = find_cache_extent(reada, bytenr, size);
3056 remove_cache_extent(reada, cache);
3059 cache = find_cache_extent(nodes, bytenr, size);
3061 remove_cache_extent(nodes, cache);
3065 /* fixme, get the real parent transid */
3066 buf = read_tree_block(root, bytenr, size, 0);
3067 if (!extent_buffer_uptodate(buf)) {
3068 record_bad_block_io(root->fs_info,
3069 extent_cache, bytenr, size);
3073 nritems = btrfs_header_nritems(buf);
3075 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3076 btrfs_header_level(buf), 1, NULL,
3079 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3081 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3086 owner = btrfs_header_owner(buf);
3089 ret = check_block(root, extent_cache, buf, flags);
3093 if (btrfs_is_leaf(buf)) {
3094 btree_space_waste += btrfs_leaf_free_space(root, buf);
3095 for (i = 0; i < nritems; i++) {
3096 struct btrfs_file_extent_item *fi;
3097 btrfs_item_key_to_cpu(buf, &key, i);
3098 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3099 process_extent_item(root, extent_cache, buf,
3103 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3104 process_extent_item(root, extent_cache, buf,
3108 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3110 btrfs_item_size_nr(buf, i);
3113 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3116 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3117 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3118 process_extent_ref_v0(extent_cache, buf, i);
3125 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3126 add_tree_backref(extent_cache, key.objectid, 0,
3130 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3131 add_tree_backref(extent_cache, key.objectid,
3135 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3136 struct btrfs_extent_data_ref *ref;
3137 ref = btrfs_item_ptr(buf, i,
3138 struct btrfs_extent_data_ref);
3139 add_data_backref(extent_cache,
3141 btrfs_extent_data_ref_root(buf, ref),
3142 btrfs_extent_data_ref_objectid(buf,
3144 btrfs_extent_data_ref_offset(buf, ref),
3145 btrfs_extent_data_ref_count(buf, ref),
3146 0, root->sectorsize);
3149 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3150 struct btrfs_shared_data_ref *ref;
3151 ref = btrfs_item_ptr(buf, i,
3152 struct btrfs_shared_data_ref);
3153 add_data_backref(extent_cache,
3154 key.objectid, key.offset, 0, 0, 0,
3155 btrfs_shared_data_ref_count(buf, ref),
3156 0, root->sectorsize);
3159 if (key.type != BTRFS_EXTENT_DATA_KEY)
3161 fi = btrfs_item_ptr(buf, i,
3162 struct btrfs_file_extent_item);
3163 if (btrfs_file_extent_type(buf, fi) ==
3164 BTRFS_FILE_EXTENT_INLINE)
3166 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3169 data_bytes_allocated +=
3170 btrfs_file_extent_disk_num_bytes(buf, fi);
3171 if (data_bytes_allocated < root->sectorsize) {
3174 data_bytes_referenced +=
3175 btrfs_file_extent_num_bytes(buf, fi);
3176 ret = add_extent_rec(extent_cache, NULL,
3177 btrfs_file_extent_disk_bytenr(buf, fi),
3178 btrfs_file_extent_disk_num_bytes(buf, fi),
3180 btrfs_file_extent_disk_num_bytes(buf, fi));
3181 add_data_backref(extent_cache,
3182 btrfs_file_extent_disk_bytenr(buf, fi),
3183 parent, owner, key.objectid, key.offset -
3184 btrfs_file_extent_offset(buf, fi), 1, 1,
3185 btrfs_file_extent_disk_num_bytes(buf, fi));
3190 struct btrfs_key first_key;
3192 first_key.objectid = 0;
3195 btrfs_item_key_to_cpu(buf, &first_key, 0);
3196 level = btrfs_header_level(buf);
3197 for (i = 0; i < nritems; i++) {
3198 u64 ptr = btrfs_node_blockptr(buf, i);
3199 u32 size = btrfs_level_size(root, level - 1);
3200 btrfs_node_key_to_cpu(buf, &key, i);
3201 ret = add_extent_rec(extent_cache, &key,
3202 ptr, size, 0, 0, 1, 0, 1, size);
3205 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3208 add_pending(nodes, seen, ptr, size);
3210 add_pending(pending, seen, ptr, size);
3213 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3214 nritems) * sizeof(struct btrfs_key_ptr);
3216 total_btree_bytes += buf->len;
3217 if (fs_root_objectid(btrfs_header_owner(buf)))
3218 total_fs_tree_bytes += buf->len;
3219 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3220 total_extent_tree_bytes += buf->len;
3221 if (!found_old_backref &&
3222 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3223 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3224 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3225 found_old_backref = 1;
3227 free_extent_buffer(buf);
3231 static int add_root_to_pending(struct extent_buffer *buf,
3232 struct cache_tree *extent_cache,
3233 struct cache_tree *pending,
3234 struct cache_tree *seen,
3235 struct cache_tree *nodes,
3236 struct btrfs_key *root_key)
3238 if (btrfs_header_level(buf) > 0)
3239 add_pending(nodes, seen, buf->start, buf->len);
3241 add_pending(pending, seen, buf->start, buf->len);
3242 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3243 0, 1, 1, 0, 1, buf->len);
3245 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3246 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3247 add_tree_backref(extent_cache, buf->start, buf->start,
3250 add_tree_backref(extent_cache, buf->start, 0,
3251 root_key->objectid, 1);
3255 /* as we fix the tree, we might be deleting blocks that
3256 * we're tracking for repair. This hook makes sure we
3257 * remove any backrefs for blocks as we are fixing them.
3259 static int free_extent_hook(struct btrfs_trans_handle *trans,
3260 struct btrfs_root *root,
3261 u64 bytenr, u64 num_bytes, u64 parent,
3262 u64 root_objectid, u64 owner, u64 offset,
3265 struct extent_record *rec;
3266 struct cache_extent *cache;
3268 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3270 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3271 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
3275 rec = container_of(cache, struct extent_record, cache);
3277 struct data_backref *back;
3278 back = find_data_backref(rec, parent, root_objectid, owner,
3282 if (back->node.found_ref) {
3283 back->found_ref -= refs_to_drop;
3285 rec->refs -= refs_to_drop;
3287 if (back->node.found_extent_tree) {
3288 back->num_refs -= refs_to_drop;
3289 if (rec->extent_item_refs)
3290 rec->extent_item_refs -= refs_to_drop;
3292 if (back->found_ref == 0)
3293 back->node.found_ref = 0;
3294 if (back->num_refs == 0)
3295 back->node.found_extent_tree = 0;
3297 if (!back->node.found_extent_tree && back->node.found_ref) {
3298 list_del(&back->node.list);
3302 struct tree_backref *back;
3303 back = find_tree_backref(rec, parent, root_objectid);
3306 if (back->node.found_ref) {
3309 back->node.found_ref = 0;
3311 if (back->node.found_extent_tree) {
3312 if (rec->extent_item_refs)
3313 rec->extent_item_refs--;
3314 back->node.found_extent_tree = 0;
3316 if (!back->node.found_extent_tree && back->node.found_ref) {
3317 list_del(&back->node.list);
3321 maybe_free_extent_rec(extent_cache, rec);
3326 static int delete_extent_records(struct btrfs_trans_handle *trans,
3327 struct btrfs_root *root,
3328 struct btrfs_path *path,
3329 u64 bytenr, u64 new_len)
3331 struct btrfs_key key;
3332 struct btrfs_key found_key;
3333 struct extent_buffer *leaf;
3338 key.objectid = bytenr;
3340 key.offset = (u64)-1;
3343 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3350 if (path->slots[0] == 0)
3356 leaf = path->nodes[0];
3357 slot = path->slots[0];
3359 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3360 if (found_key.objectid != bytenr)
3363 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3364 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3365 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3366 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3367 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3368 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3369 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3370 btrfs_release_path(NULL, path);
3371 if (found_key.type == 0) {
3372 if (found_key.offset == 0)
3374 key.offset = found_key.offset - 1;
3375 key.type = found_key.type;
3377 key.type = found_key.type - 1;
3378 key.offset = (u64)-1;
3382 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
3383 found_key.objectid, found_key.type, found_key.offset);
3385 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
3388 btrfs_release_path(NULL, path);
3390 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3391 found_key.type == BTRFS_METADATA_ITEM_KEY) {
3392 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
3393 found_key.offset : root->leafsize;
3395 ret = btrfs_update_block_group(trans, root, bytenr,
3402 btrfs_release_path(NULL, path);
3407 * for a single backref, this will allocate a new extent
3408 * and add the backref to it.
3410 static int record_extent(struct btrfs_trans_handle *trans,
3411 struct btrfs_fs_info *info,
3412 struct btrfs_path *path,
3413 struct extent_record *rec,
3414 struct extent_backref *back,
3415 int allocated, u64 flags)
3418 struct btrfs_root *extent_root = info->extent_root;
3419 struct extent_buffer *leaf;
3420 struct btrfs_key ins_key;
3421 struct btrfs_extent_item *ei;
3422 struct tree_backref *tback;
3423 struct data_backref *dback;
3424 struct btrfs_tree_block_info *bi;
3427 rec->max_size = max_t(u64, rec->max_size,
3428 info->extent_root->leafsize);
3431 u32 item_size = sizeof(*ei);
3434 item_size += sizeof(*bi);
3436 ins_key.objectid = rec->start;
3437 ins_key.offset = rec->max_size;
3438 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3440 ret = btrfs_insert_empty_item(trans, extent_root, path,
3441 &ins_key, item_size);
3445 leaf = path->nodes[0];
3446 ei = btrfs_item_ptr(leaf, path->slots[0],
3447 struct btrfs_extent_item);
3449 btrfs_set_extent_refs(leaf, ei, 0);
3450 btrfs_set_extent_generation(leaf, ei, rec->generation);
3452 if (back->is_data) {
3453 btrfs_set_extent_flags(leaf, ei,
3454 BTRFS_EXTENT_FLAG_DATA);
3456 struct btrfs_disk_key copy_key;;
3458 tback = (struct tree_backref *)back;
3459 bi = (struct btrfs_tree_block_info *)(ei + 1);
3460 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3462 memset(©_key, 0, sizeof(copy_key));
3464 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3465 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3466 btrfs_set_tree_block_key(leaf, bi, ©_key);
3468 btrfs_set_extent_flags(leaf, ei,
3469 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3472 btrfs_mark_buffer_dirty(leaf);
3473 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3474 rec->max_size, 1, 0);
3477 btrfs_release_path(NULL, path);
3480 if (back->is_data) {
3484 dback = (struct data_backref *)back;
3485 if (back->full_backref)
3486 parent = dback->parent;
3490 for (i = 0; i < dback->found_ref; i++) {
3491 /* if parent != 0, we're doing a full backref
3492 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3493 * just makes the backref allocator create a data
3496 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3497 rec->start, rec->max_size,
3501 BTRFS_FIRST_FREE_OBJECTID :
3507 fprintf(stderr, "adding new data backref"
3508 " on %llu %s %llu owner %llu"
3509 " offset %llu found %d\n",
3510 (unsigned long long)rec->start,
3511 back->full_backref ?
3513 back->full_backref ?
3514 (unsigned long long)parent :
3515 (unsigned long long)dback->root,
3516 (unsigned long long)dback->owner,
3517 (unsigned long long)dback->offset,
3522 tback = (struct tree_backref *)back;
3523 if (back->full_backref)
3524 parent = tback->parent;
3528 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3529 rec->start, rec->max_size,
3530 parent, tback->root, 0, 0);
3531 fprintf(stderr, "adding new tree backref on "
3532 "start %llu len %llu parent %llu root %llu\n",
3533 rec->start, rec->max_size, tback->parent, tback->root);
3538 btrfs_release_path(NULL, path);
3543 * when an incorrect extent item is found, this will delete
3544 * all of the existing entries for it and recreate them
3545 * based on what the tree scan found.
3547 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3548 struct btrfs_fs_info *info,
3549 struct extent_record *rec)
3552 struct btrfs_path *path;
3553 struct list_head *cur = rec->backrefs.next;
3554 struct cache_extent *cache;
3555 struct extent_backref *back;
3559 /* remember our flags for recreating the extent */
3560 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3561 rec->max_size, rec->metadata, NULL,
3564 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3566 path = btrfs_alloc_path();
3568 /* step one, delete all the existing records */
3569 ret = delete_extent_records(trans, info->extent_root, path,
3570 rec->start, rec->max_size);
3575 /* was this block corrupt? If so, don't add references to it */
3576 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3582 /* step two, recreate all the refs we did find */
3583 while(cur != &rec->backrefs) {
3584 back = list_entry(cur, struct extent_backref, list);
3588 * if we didn't find any references, don't create a
3591 if (!back->found_ref)
3594 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3601 btrfs_free_path(path);
3605 /* right now we only prune from the extent allocation tree */
3606 static int prune_one_block(struct btrfs_trans_handle *trans,
3607 struct btrfs_fs_info *info,
3608 struct btrfs_corrupt_block *corrupt)
3611 struct btrfs_path path;
3612 struct extent_buffer *eb;
3616 int level = corrupt->level + 1;
3618 btrfs_init_path(&path);
3620 /* we want to stop at the parent to our busted block */
3621 path.lowest_level = level;
3623 ret = btrfs_search_slot(trans, info->extent_root,
3624 &corrupt->key, &path, -1, 1);
3629 eb = path.nodes[level];
3636 * hopefully the search gave us the block we want to prune,
3637 * lets try that first
3639 slot = path.slots[level];
3640 found = btrfs_node_blockptr(eb, slot);
3641 if (found == corrupt->cache.start)
3644 nritems = btrfs_header_nritems(eb);
3646 /* the search failed, lets scan this node and hope we find it */
3647 for (slot = 0; slot < nritems; slot++) {
3648 found = btrfs_node_blockptr(eb, slot);
3649 if (found == corrupt->cache.start)
3653 * we couldn't find the bad block. TODO, search all the nodes for pointers
3656 if (eb == info->extent_root->node) {
3661 btrfs_release_path(NULL, &path);
3666 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3667 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3670 btrfs_release_path(NULL, &path);
3674 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3675 struct btrfs_fs_info *info)
3677 struct cache_extent *cache;
3678 struct btrfs_corrupt_block *corrupt;
3680 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3684 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3685 prune_one_block(trans, info, corrupt);
3686 cache = next_cache_extent(cache);
3691 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3693 struct cache_extent *cache;
3694 struct btrfs_corrupt_block *corrupt;
3697 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3700 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3701 remove_cache_extent(info->corrupt_blocks, cache);
3706 static int check_block_group(struct btrfs_trans_handle *trans,
3707 struct btrfs_fs_info *info,
3708 struct map_lookup *map,
3711 struct btrfs_key key;
3712 struct btrfs_path path;
3715 key.objectid = map->ce.start;
3716 key.offset = map->ce.size;
3717 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3719 btrfs_init_path(&path);
3720 ret = btrfs_search_slot(NULL, info->extent_root,
3722 btrfs_release_path(NULL, &path);
3726 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3727 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3728 key.objectid, key.offset);
3734 static int check_block_groups(struct btrfs_trans_handle *trans,
3735 struct btrfs_fs_info *info, int *reinit)
3737 struct cache_extent *ce;
3738 struct map_lookup *map;
3739 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3741 /* this isn't quite working */
3744 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3748 map = container_of(ce, struct map_lookup, ce);
3749 check_block_group(trans, info, map, reinit);
3750 ce = next_cache_extent(ce);
3755 static int check_extent_refs(struct btrfs_trans_handle *trans,
3756 struct btrfs_root *root,
3757 struct cache_tree *extent_cache, int repair)
3759 struct extent_record *rec;
3760 struct cache_extent *cache;
3768 * if we're doing a repair, we have to make sure
3769 * we don't allocate from the problem extents.
3770 * In the worst case, this will be all the
3773 cache = find_first_cache_extent(extent_cache, 0);
3775 rec = container_of(cache, struct extent_record, cache);
3776 btrfs_pin_extent(root->fs_info,
3777 rec->start, rec->max_size);
3778 cache = next_cache_extent(cache);
3781 /* pin down all the corrupted blocks too */
3782 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3784 rec = container_of(cache, struct extent_record, cache);
3785 btrfs_pin_extent(root->fs_info,
3786 rec->start, rec->max_size);
3787 cache = next_cache_extent(cache);
3789 prune_corrupt_blocks(trans, root->fs_info);
3790 check_block_groups(trans, root->fs_info, &reinit);
3792 btrfs_read_block_groups(root->fs_info->extent_root);
3796 cache = find_first_cache_extent(extent_cache, 0);
3799 rec = container_of(cache, struct extent_record, cache);
3800 if (rec->refs != rec->extent_item_refs) {
3801 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3802 (unsigned long long)rec->start,
3803 (unsigned long long)rec->nr);
3804 fprintf(stderr, "extent item %llu, found %llu\n",
3805 (unsigned long long)rec->extent_item_refs,
3806 (unsigned long long)rec->refs);
3807 if (!fixed && repair) {
3808 ret = fixup_extent_refs(trans, root->fs_info, rec);
3816 if (all_backpointers_checked(rec, 1)) {
3817 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3818 (unsigned long long)rec->start,
3819 (unsigned long long)rec->nr);
3821 if (!fixed && repair) {
3822 ret = fixup_extent_refs(trans, root->fs_info, rec);
3830 if (!rec->owner_ref_checked) {
3831 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3832 (unsigned long long)rec->start,
3833 (unsigned long long)rec->nr);
3834 if (!fixed && repair) {
3835 ret = fixup_extent_refs(trans, root->fs_info, rec);
3843 remove_cache_extent(extent_cache, cache);
3844 free_all_extent_backrefs(rec);
3850 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3853 btrfs_fix_block_accounting(trans, root);
3856 fprintf(stderr, "repaired damaged extent references\n");
3862 static int check_extents(struct btrfs_trans_handle *trans,
3863 struct btrfs_root *root, int repair)
3865 struct cache_tree extent_cache;
3866 struct cache_tree seen;
3867 struct cache_tree pending;
3868 struct cache_tree reada;
3869 struct cache_tree nodes;
3870 struct cache_tree corrupt_blocks;
3871 struct btrfs_path path;
3872 struct btrfs_key key;
3873 struct btrfs_key found_key;
3876 struct block_info *bits;
3878 struct extent_buffer *leaf;
3880 struct btrfs_root_item ri;
3882 cache_tree_init(&extent_cache);
3883 cache_tree_init(&seen);
3884 cache_tree_init(&pending);
3885 cache_tree_init(&nodes);
3886 cache_tree_init(&reada);
3887 cache_tree_init(&corrupt_blocks);
3890 root->fs_info->fsck_extent_cache = &extent_cache;
3891 root->fs_info->free_extent_hook = free_extent_hook;
3892 root->fs_info->corrupt_blocks = &corrupt_blocks;
3896 bits = malloc(bits_nr * sizeof(struct block_info));
3902 add_root_to_pending(root->fs_info->tree_root->node,
3903 &extent_cache, &pending, &seen, &nodes,
3904 &root->fs_info->tree_root->root_key);
3906 add_root_to_pending(root->fs_info->chunk_root->node,
3907 &extent_cache, &pending, &seen, &nodes,
3908 &root->fs_info->chunk_root->root_key);
3910 btrfs_init_path(&path);
3913 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3914 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3918 leaf = path.nodes[0];
3919 slot = path.slots[0];
3920 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3921 ret = btrfs_next_leaf(root, &path);
3924 leaf = path.nodes[0];
3925 slot = path.slots[0];
3927 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3928 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3929 unsigned long offset;
3930 struct extent_buffer *buf;
3932 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3933 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3934 buf = read_tree_block(root->fs_info->tree_root,
3935 btrfs_root_bytenr(&ri),
3936 btrfs_level_size(root,
3937 btrfs_root_level(&ri)), 0);
3938 add_root_to_pending(buf, &extent_cache, &pending,
3939 &seen, &nodes, &found_key);
3940 free_extent_buffer(buf);
3944 btrfs_release_path(root, &path);
3946 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3947 &seen, &reada, &nodes, &extent_cache);
3951 ret = check_extent_refs(trans, root, &extent_cache, repair);
3954 free_corrupt_blocks(root->fs_info);
3955 root->fs_info->fsck_extent_cache = NULL;
3956 root->fs_info->free_extent_hook = NULL;
3957 root->fs_info->corrupt_blocks = NULL;
3964 static struct option long_options[] = {
3965 { "super", 1, NULL, 's' },
3966 { "repair", 0, NULL, 0 },
3967 { "init-csum-tree", 0, NULL, 0 },
3968 { "init-extent-tree", 0, NULL, 0 },
3972 const char * const cmd_check_usage[] = {
3973 "btrfs check [options] <device>",
3974 "Check an unmounted btrfs filesystem.",
3976 "-s|--super <superblock> use this superblock copy",
3977 "--repair try to repair the filesystem",
3978 "--init-csum-tree create a new CRC tree",
3979 "--init-extent-tree create a new extent tree",
3983 int cmd_check(int argc, char **argv)
3985 struct cache_tree root_cache;
3986 struct btrfs_root *root;
3987 struct btrfs_fs_info *info;
3988 struct btrfs_trans_handle *trans = NULL;
3994 int option_index = 0;
3995 int init_csum_tree = 0;
4000 c = getopt_long(argc, argv, "as:", long_options,
4005 case 'a': /* ignored */ break;
4008 bytenr = btrfs_sb_offset(num);
4009 printf("using SB copy %d, bytenr %llu\n", num,
4010 (unsigned long long)bytenr);
4014 usage(cmd_check_usage);
4016 if (option_index == 1) {
4017 printf("enabling repair mode\n");
4020 } else if (option_index == 2) {
4021 printf("Creating a new CRC tree\n");
4027 argc = argc - optind;
4030 usage(cmd_check_usage);
4033 cache_tree_init(&root_cache);
4035 if((ret = check_mounted(argv[optind])) < 0) {
4036 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
4039 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
4043 info = open_ctree_fs_info(argv[optind], bytenr, rw, 1);
4045 fprintf(stderr, "Couldn't open file system\n");
4049 uuid_unparse(info->super_copy->fsid, uuidbuf);
4050 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
4052 if (!extent_buffer_uptodate(info->tree_root->node) ||
4053 !extent_buffer_uptodate(info->dev_root->node) ||
4054 !extent_buffer_uptodate(info->extent_root->node) ||
4055 !extent_buffer_uptodate(info->chunk_root->node)) {
4056 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
4060 root = info->fs_root;
4062 fprintf(stderr, "checking extents\n");
4064 trans = btrfs_start_transaction(root, 1);
4066 if (init_csum_tree) {
4067 fprintf(stderr, "Reinit crc root\n");
4068 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
4070 fprintf(stderr, "crc root initialization failed\n");
4075 ret = check_extents(trans, root, repair);
4077 fprintf(stderr, "Errors found in extent allocation tree\n");
4079 fprintf(stderr, "checking free space cache\n");
4080 ret = check_space_cache(root);
4084 fprintf(stderr, "checking fs roots\n");
4085 ret = check_fs_roots(root, &root_cache);
4089 fprintf(stderr, "checking csums\n");
4090 ret = check_csums(root);
4094 fprintf(stderr, "checking root refs\n");
4095 ret = check_root_refs(root, &root_cache);
4097 free_root_recs(&root_cache);
4099 ret = btrfs_commit_transaction(trans, root);
4105 if (found_old_backref) { /*
4106 * there was a disk format change when mixed
4107 * backref was in testing tree. The old format
4108 * existed about one week.
4110 printf("\n * Found old mixed backref format. "
4111 "The old format is not supported! *"
4112 "\n * Please mount the FS in readonly mode, "
4113 "backup data and re-format the FS. *\n\n");
4116 printf("found %llu bytes used err is %d\n",
4117 (unsigned long long)bytes_used, ret);
4118 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
4119 printf("total tree bytes: %llu\n",
4120 (unsigned long long)total_btree_bytes);
4121 printf("total fs tree bytes: %llu\n",
4122 (unsigned long long)total_fs_tree_bytes);
4123 printf("total extent tree bytes: %llu\n",
4124 (unsigned long long)total_extent_tree_bytes);
4125 printf("btree space waste bytes: %llu\n",
4126 (unsigned long long)btree_space_waste);
4127 printf("file data blocks allocated: %llu\n referenced %llu\n",
4128 (unsigned long long)data_bytes_allocated,
4129 (unsigned long long)data_bytes_referenced);
4130 printf("%s\n", BTRFS_BUILD_VERSION);