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;
2846 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
2849 fprintf(stderr, "Error looking up extent record %d\n", ret);
2850 btrfs_free_path(path);
2856 btrfs_prev_leaf(root, path);
2859 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2862 * Block group items come before extent items if they have the same
2863 * bytenr, so walk back one more just in case. Dear future traveler,
2864 * first congrats on mastering time travel. Now if it's not too much
2865 * trouble could you go back to 2006 and tell Chris to make the
2866 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
2868 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2872 btrfs_prev_leaf(root, path);
2876 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2877 ret = btrfs_next_leaf(root, path);
2879 fprintf(stderr, "Error going to next leaf "
2881 btrfs_free_path(path);
2887 leaf = path->nodes[0];
2888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2889 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
2893 if (key.objectid + key.offset < bytenr) {
2897 if (key.objectid > bytenr + num_bytes)
2900 if (key.objectid == bytenr) {
2901 if (key.offset >= num_bytes) {
2905 num_bytes -= key.offset;
2906 bytenr += key.offset;
2907 } else if (key.objectid < bytenr) {
2908 if (key.objectid + key.offset >= bytenr + num_bytes) {
2912 num_bytes = (bytenr + num_bytes) -
2913 (key.objectid + key.offset);
2914 bytenr = key.objectid + key.offset;
2916 if (key.objectid + key.offset < bytenr + num_bytes) {
2917 u64 new_start = key.objectid + key.offset;
2918 u64 new_bytes = bytenr + num_bytes - new_start;
2921 * Weird case, the extent is in the middle of
2922 * our range, we'll have to search one side
2923 * and then the other. Not sure if this happens
2924 * in real life, but no harm in coding it up
2925 * anyway just in case.
2927 btrfs_release_path(root, path);
2928 ret = check_extent_exists(root, new_start,
2931 fprintf(stderr, "Right section didn't "
2935 num_bytes = key.objectid - bytenr;
2938 num_bytes = key.objectid - bytenr;
2945 fprintf(stderr, "There are no extents for csum range "
2946 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
2950 btrfs_free_path(path);
2954 static int check_csums(struct btrfs_root *root)
2956 struct btrfs_path *path;
2957 struct extent_buffer *leaf;
2958 struct btrfs_key key;
2959 u64 offset = 0, num_bytes = 0;
2960 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
2964 root = root->fs_info->csum_root;
2966 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2967 key.type = BTRFS_EXTENT_CSUM_KEY;
2970 path = btrfs_alloc_path();
2974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2976 fprintf(stderr, "Error searching csum tree %d\n", ret);
2977 btrfs_free_path(path);
2981 if (ret > 0 && path->slots[0])
2986 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2987 ret = btrfs_next_leaf(root, path);
2989 fprintf(stderr, "Error going to next leaf "
2996 leaf = path->nodes[0];
2998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2999 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3005 offset = key.offset;
3006 } else if (key.offset != offset + num_bytes) {
3007 ret = check_extent_exists(root, offset, num_bytes);
3009 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3010 "there is no extent record\n",
3011 offset, offset+num_bytes);
3014 offset = key.offset;
3018 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3019 csum_size) * root->sectorsize;
3023 btrfs_free_path(path);
3027 static int run_next_block(struct btrfs_root *root,
3028 struct block_info *bits,
3031 struct cache_tree *pending,
3032 struct cache_tree *seen,
3033 struct cache_tree *reada,
3034 struct cache_tree *nodes,
3035 struct cache_tree *extent_cache)
3037 struct extent_buffer *buf;
3046 struct btrfs_key key;
3047 struct cache_extent *cache;
3050 ret = pick_next_pending(pending, reada, nodes, *last, bits,
3051 bits_nr, &reada_bits);
3056 for(i = 0; i < ret; i++) {
3057 insert_cache_extent(reada, bits[i].start,
3060 /* fixme, get the parent transid */
3061 readahead_tree_block(root, bits[i].start,
3065 *last = bits[0].start;
3066 bytenr = bits[0].start;
3067 size = bits[0].size;
3069 cache = find_cache_extent(pending, bytenr, size);
3071 remove_cache_extent(pending, cache);
3074 cache = find_cache_extent(reada, bytenr, size);
3076 remove_cache_extent(reada, cache);
3079 cache = find_cache_extent(nodes, bytenr, size);
3081 remove_cache_extent(nodes, cache);
3085 /* fixme, get the real parent transid */
3086 buf = read_tree_block(root, bytenr, size, 0);
3087 if (!extent_buffer_uptodate(buf)) {
3088 record_bad_block_io(root->fs_info,
3089 extent_cache, bytenr, size);
3093 nritems = btrfs_header_nritems(buf);
3095 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3096 btrfs_header_level(buf), 1, NULL,
3099 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3101 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3106 owner = btrfs_header_owner(buf);
3109 ret = check_block(root, extent_cache, buf, flags);
3113 if (btrfs_is_leaf(buf)) {
3114 btree_space_waste += btrfs_leaf_free_space(root, buf);
3115 for (i = 0; i < nritems; i++) {
3116 struct btrfs_file_extent_item *fi;
3117 btrfs_item_key_to_cpu(buf, &key, i);
3118 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3119 process_extent_item(root, extent_cache, buf,
3123 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3124 process_extent_item(root, extent_cache, buf,
3128 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3130 btrfs_item_size_nr(buf, i);
3133 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3136 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3137 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3138 process_extent_ref_v0(extent_cache, buf, i);
3145 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3146 add_tree_backref(extent_cache, key.objectid, 0,
3150 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3151 add_tree_backref(extent_cache, key.objectid,
3155 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3156 struct btrfs_extent_data_ref *ref;
3157 ref = btrfs_item_ptr(buf, i,
3158 struct btrfs_extent_data_ref);
3159 add_data_backref(extent_cache,
3161 btrfs_extent_data_ref_root(buf, ref),
3162 btrfs_extent_data_ref_objectid(buf,
3164 btrfs_extent_data_ref_offset(buf, ref),
3165 btrfs_extent_data_ref_count(buf, ref),
3166 0, root->sectorsize);
3169 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3170 struct btrfs_shared_data_ref *ref;
3171 ref = btrfs_item_ptr(buf, i,
3172 struct btrfs_shared_data_ref);
3173 add_data_backref(extent_cache,
3174 key.objectid, key.offset, 0, 0, 0,
3175 btrfs_shared_data_ref_count(buf, ref),
3176 0, root->sectorsize);
3179 if (key.type != BTRFS_EXTENT_DATA_KEY)
3181 fi = btrfs_item_ptr(buf, i,
3182 struct btrfs_file_extent_item);
3183 if (btrfs_file_extent_type(buf, fi) ==
3184 BTRFS_FILE_EXTENT_INLINE)
3186 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3189 data_bytes_allocated +=
3190 btrfs_file_extent_disk_num_bytes(buf, fi);
3191 if (data_bytes_allocated < root->sectorsize) {
3194 data_bytes_referenced +=
3195 btrfs_file_extent_num_bytes(buf, fi);
3196 ret = add_extent_rec(extent_cache, NULL,
3197 btrfs_file_extent_disk_bytenr(buf, fi),
3198 btrfs_file_extent_disk_num_bytes(buf, fi),
3200 btrfs_file_extent_disk_num_bytes(buf, fi));
3201 add_data_backref(extent_cache,
3202 btrfs_file_extent_disk_bytenr(buf, fi),
3203 parent, owner, key.objectid, key.offset -
3204 btrfs_file_extent_offset(buf, fi), 1, 1,
3205 btrfs_file_extent_disk_num_bytes(buf, fi));
3210 struct btrfs_key first_key;
3212 first_key.objectid = 0;
3215 btrfs_item_key_to_cpu(buf, &first_key, 0);
3216 level = btrfs_header_level(buf);
3217 for (i = 0; i < nritems; i++) {
3218 u64 ptr = btrfs_node_blockptr(buf, i);
3219 u32 size = btrfs_level_size(root, level - 1);
3220 btrfs_node_key_to_cpu(buf, &key, i);
3221 ret = add_extent_rec(extent_cache, &key,
3222 ptr, size, 0, 0, 1, 0, 1, size);
3225 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3228 add_pending(nodes, seen, ptr, size);
3230 add_pending(pending, seen, ptr, size);
3233 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3234 nritems) * sizeof(struct btrfs_key_ptr);
3236 total_btree_bytes += buf->len;
3237 if (fs_root_objectid(btrfs_header_owner(buf)))
3238 total_fs_tree_bytes += buf->len;
3239 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3240 total_extent_tree_bytes += buf->len;
3241 if (!found_old_backref &&
3242 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3243 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3244 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3245 found_old_backref = 1;
3247 free_extent_buffer(buf);
3251 static int add_root_to_pending(struct extent_buffer *buf,
3252 struct cache_tree *extent_cache,
3253 struct cache_tree *pending,
3254 struct cache_tree *seen,
3255 struct cache_tree *nodes,
3256 struct btrfs_key *root_key)
3258 if (btrfs_header_level(buf) > 0)
3259 add_pending(nodes, seen, buf->start, buf->len);
3261 add_pending(pending, seen, buf->start, buf->len);
3262 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3263 0, 1, 1, 0, 1, buf->len);
3265 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3266 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3267 add_tree_backref(extent_cache, buf->start, buf->start,
3270 add_tree_backref(extent_cache, buf->start, 0,
3271 root_key->objectid, 1);
3275 /* as we fix the tree, we might be deleting blocks that
3276 * we're tracking for repair. This hook makes sure we
3277 * remove any backrefs for blocks as we are fixing them.
3279 static int free_extent_hook(struct btrfs_trans_handle *trans,
3280 struct btrfs_root *root,
3281 u64 bytenr, u64 num_bytes, u64 parent,
3282 u64 root_objectid, u64 owner, u64 offset,
3285 struct extent_record *rec;
3286 struct cache_extent *cache;
3288 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3290 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3291 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
3295 rec = container_of(cache, struct extent_record, cache);
3297 struct data_backref *back;
3298 back = find_data_backref(rec, parent, root_objectid, owner,
3302 if (back->node.found_ref) {
3303 back->found_ref -= refs_to_drop;
3305 rec->refs -= refs_to_drop;
3307 if (back->node.found_extent_tree) {
3308 back->num_refs -= refs_to_drop;
3309 if (rec->extent_item_refs)
3310 rec->extent_item_refs -= refs_to_drop;
3312 if (back->found_ref == 0)
3313 back->node.found_ref = 0;
3314 if (back->num_refs == 0)
3315 back->node.found_extent_tree = 0;
3317 if (!back->node.found_extent_tree && back->node.found_ref) {
3318 list_del(&back->node.list);
3322 struct tree_backref *back;
3323 back = find_tree_backref(rec, parent, root_objectid);
3326 if (back->node.found_ref) {
3329 back->node.found_ref = 0;
3331 if (back->node.found_extent_tree) {
3332 if (rec->extent_item_refs)
3333 rec->extent_item_refs--;
3334 back->node.found_extent_tree = 0;
3336 if (!back->node.found_extent_tree && back->node.found_ref) {
3337 list_del(&back->node.list);
3341 maybe_free_extent_rec(extent_cache, rec);
3346 static int delete_extent_records(struct btrfs_trans_handle *trans,
3347 struct btrfs_root *root,
3348 struct btrfs_path *path,
3349 u64 bytenr, u64 new_len)
3351 struct btrfs_key key;
3352 struct btrfs_key found_key;
3353 struct extent_buffer *leaf;
3358 key.objectid = bytenr;
3360 key.offset = (u64)-1;
3363 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3370 if (path->slots[0] == 0)
3376 leaf = path->nodes[0];
3377 slot = path->slots[0];
3379 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3380 if (found_key.objectid != bytenr)
3383 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3384 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3385 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3386 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3387 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3388 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3389 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3390 btrfs_release_path(NULL, path);
3391 if (found_key.type == 0) {
3392 if (found_key.offset == 0)
3394 key.offset = found_key.offset - 1;
3395 key.type = found_key.type;
3397 key.type = found_key.type - 1;
3398 key.offset = (u64)-1;
3402 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
3403 found_key.objectid, found_key.type, found_key.offset);
3405 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
3408 btrfs_release_path(NULL, path);
3410 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3411 found_key.type == BTRFS_METADATA_ITEM_KEY) {
3412 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
3413 found_key.offset : root->leafsize;
3415 ret = btrfs_update_block_group(trans, root, bytenr,
3422 btrfs_release_path(NULL, path);
3427 * for a single backref, this will allocate a new extent
3428 * and add the backref to it.
3430 static int record_extent(struct btrfs_trans_handle *trans,
3431 struct btrfs_fs_info *info,
3432 struct btrfs_path *path,
3433 struct extent_record *rec,
3434 struct extent_backref *back,
3435 int allocated, u64 flags)
3438 struct btrfs_root *extent_root = info->extent_root;
3439 struct extent_buffer *leaf;
3440 struct btrfs_key ins_key;
3441 struct btrfs_extent_item *ei;
3442 struct tree_backref *tback;
3443 struct data_backref *dback;
3444 struct btrfs_tree_block_info *bi;
3447 rec->max_size = max_t(u64, rec->max_size,
3448 info->extent_root->leafsize);
3451 u32 item_size = sizeof(*ei);
3454 item_size += sizeof(*bi);
3456 ins_key.objectid = rec->start;
3457 ins_key.offset = rec->max_size;
3458 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3460 ret = btrfs_insert_empty_item(trans, extent_root, path,
3461 &ins_key, item_size);
3465 leaf = path->nodes[0];
3466 ei = btrfs_item_ptr(leaf, path->slots[0],
3467 struct btrfs_extent_item);
3469 btrfs_set_extent_refs(leaf, ei, 0);
3470 btrfs_set_extent_generation(leaf, ei, rec->generation);
3472 if (back->is_data) {
3473 btrfs_set_extent_flags(leaf, ei,
3474 BTRFS_EXTENT_FLAG_DATA);
3476 struct btrfs_disk_key copy_key;;
3478 tback = (struct tree_backref *)back;
3479 bi = (struct btrfs_tree_block_info *)(ei + 1);
3480 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3482 memset(©_key, 0, sizeof(copy_key));
3484 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3485 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3486 btrfs_set_tree_block_key(leaf, bi, ©_key);
3488 btrfs_set_extent_flags(leaf, ei,
3489 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3492 btrfs_mark_buffer_dirty(leaf);
3493 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3494 rec->max_size, 1, 0);
3497 btrfs_release_path(NULL, path);
3500 if (back->is_data) {
3504 dback = (struct data_backref *)back;
3505 if (back->full_backref)
3506 parent = dback->parent;
3510 for (i = 0; i < dback->found_ref; i++) {
3511 /* if parent != 0, we're doing a full backref
3512 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3513 * just makes the backref allocator create a data
3516 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3517 rec->start, rec->max_size,
3521 BTRFS_FIRST_FREE_OBJECTID :
3527 fprintf(stderr, "adding new data backref"
3528 " on %llu %s %llu owner %llu"
3529 " offset %llu found %d\n",
3530 (unsigned long long)rec->start,
3531 back->full_backref ?
3533 back->full_backref ?
3534 (unsigned long long)parent :
3535 (unsigned long long)dback->root,
3536 (unsigned long long)dback->owner,
3537 (unsigned long long)dback->offset,
3542 tback = (struct tree_backref *)back;
3543 if (back->full_backref)
3544 parent = tback->parent;
3548 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3549 rec->start, rec->max_size,
3550 parent, tback->root, 0, 0);
3551 fprintf(stderr, "adding new tree backref on "
3552 "start %llu len %llu parent %llu root %llu\n",
3553 rec->start, rec->max_size, tback->parent, tback->root);
3558 btrfs_release_path(NULL, path);
3563 * when an incorrect extent item is found, this will delete
3564 * all of the existing entries for it and recreate them
3565 * based on what the tree scan found.
3567 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3568 struct btrfs_fs_info *info,
3569 struct extent_record *rec)
3572 struct btrfs_path *path;
3573 struct list_head *cur = rec->backrefs.next;
3574 struct cache_extent *cache;
3575 struct extent_backref *back;
3579 /* remember our flags for recreating the extent */
3580 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3581 rec->max_size, rec->metadata, NULL,
3584 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3586 path = btrfs_alloc_path();
3588 /* step one, delete all the existing records */
3589 ret = delete_extent_records(trans, info->extent_root, path,
3590 rec->start, rec->max_size);
3595 /* was this block corrupt? If so, don't add references to it */
3596 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3602 /* step two, recreate all the refs we did find */
3603 while(cur != &rec->backrefs) {
3604 back = list_entry(cur, struct extent_backref, list);
3608 * if we didn't find any references, don't create a
3611 if (!back->found_ref)
3614 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3621 btrfs_free_path(path);
3625 /* right now we only prune from the extent allocation tree */
3626 static int prune_one_block(struct btrfs_trans_handle *trans,
3627 struct btrfs_fs_info *info,
3628 struct btrfs_corrupt_block *corrupt)
3631 struct btrfs_path path;
3632 struct extent_buffer *eb;
3636 int level = corrupt->level + 1;
3638 btrfs_init_path(&path);
3640 /* we want to stop at the parent to our busted block */
3641 path.lowest_level = level;
3643 ret = btrfs_search_slot(trans, info->extent_root,
3644 &corrupt->key, &path, -1, 1);
3649 eb = path.nodes[level];
3656 * hopefully the search gave us the block we want to prune,
3657 * lets try that first
3659 slot = path.slots[level];
3660 found = btrfs_node_blockptr(eb, slot);
3661 if (found == corrupt->cache.start)
3664 nritems = btrfs_header_nritems(eb);
3666 /* the search failed, lets scan this node and hope we find it */
3667 for (slot = 0; slot < nritems; slot++) {
3668 found = btrfs_node_blockptr(eb, slot);
3669 if (found == corrupt->cache.start)
3673 * we couldn't find the bad block. TODO, search all the nodes for pointers
3676 if (eb == info->extent_root->node) {
3681 btrfs_release_path(NULL, &path);
3686 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3687 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3690 btrfs_release_path(NULL, &path);
3694 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3695 struct btrfs_fs_info *info)
3697 struct cache_extent *cache;
3698 struct btrfs_corrupt_block *corrupt;
3700 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3704 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3705 prune_one_block(trans, info, corrupt);
3706 cache = next_cache_extent(cache);
3711 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3713 struct cache_extent *cache;
3714 struct btrfs_corrupt_block *corrupt;
3717 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3720 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3721 remove_cache_extent(info->corrupt_blocks, cache);
3726 static int check_block_group(struct btrfs_trans_handle *trans,
3727 struct btrfs_fs_info *info,
3728 struct map_lookup *map,
3731 struct btrfs_key key;
3732 struct btrfs_path path;
3735 key.objectid = map->ce.start;
3736 key.offset = map->ce.size;
3737 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3739 btrfs_init_path(&path);
3740 ret = btrfs_search_slot(NULL, info->extent_root,
3742 btrfs_release_path(NULL, &path);
3746 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3747 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3748 key.objectid, key.offset);
3754 static int check_block_groups(struct btrfs_trans_handle *trans,
3755 struct btrfs_fs_info *info, int *reinit)
3757 struct cache_extent *ce;
3758 struct map_lookup *map;
3759 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3761 /* this isn't quite working */
3764 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3768 map = container_of(ce, struct map_lookup, ce);
3769 check_block_group(trans, info, map, reinit);
3770 ce = next_cache_extent(ce);
3775 static int check_extent_refs(struct btrfs_trans_handle *trans,
3776 struct btrfs_root *root,
3777 struct cache_tree *extent_cache, int repair)
3779 struct extent_record *rec;
3780 struct cache_extent *cache;
3788 * if we're doing a repair, we have to make sure
3789 * we don't allocate from the problem extents.
3790 * In the worst case, this will be all the
3793 cache = find_first_cache_extent(extent_cache, 0);
3795 rec = container_of(cache, struct extent_record, cache);
3796 btrfs_pin_extent(root->fs_info,
3797 rec->start, rec->max_size);
3798 cache = next_cache_extent(cache);
3801 /* pin down all the corrupted blocks too */
3802 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3804 rec = container_of(cache, struct extent_record, cache);
3805 btrfs_pin_extent(root->fs_info,
3806 rec->start, rec->max_size);
3807 cache = next_cache_extent(cache);
3809 prune_corrupt_blocks(trans, root->fs_info);
3810 check_block_groups(trans, root->fs_info, &reinit);
3812 btrfs_read_block_groups(root->fs_info->extent_root);
3816 cache = find_first_cache_extent(extent_cache, 0);
3819 rec = container_of(cache, struct extent_record, cache);
3820 if (rec->refs != rec->extent_item_refs) {
3821 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3822 (unsigned long long)rec->start,
3823 (unsigned long long)rec->nr);
3824 fprintf(stderr, "extent item %llu, found %llu\n",
3825 (unsigned long long)rec->extent_item_refs,
3826 (unsigned long long)rec->refs);
3827 if (!fixed && repair) {
3828 ret = fixup_extent_refs(trans, root->fs_info, rec);
3836 if (all_backpointers_checked(rec, 1)) {
3837 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3838 (unsigned long long)rec->start,
3839 (unsigned long long)rec->nr);
3841 if (!fixed && repair) {
3842 ret = fixup_extent_refs(trans, root->fs_info, rec);
3850 if (!rec->owner_ref_checked) {
3851 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3852 (unsigned long long)rec->start,
3853 (unsigned long long)rec->nr);
3854 if (!fixed && repair) {
3855 ret = fixup_extent_refs(trans, root->fs_info, rec);
3863 remove_cache_extent(extent_cache, cache);
3864 free_all_extent_backrefs(rec);
3870 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3873 btrfs_fix_block_accounting(trans, root);
3876 fprintf(stderr, "repaired damaged extent references\n");
3882 static int check_extents(struct btrfs_trans_handle *trans,
3883 struct btrfs_root *root, int repair)
3885 struct cache_tree extent_cache;
3886 struct cache_tree seen;
3887 struct cache_tree pending;
3888 struct cache_tree reada;
3889 struct cache_tree nodes;
3890 struct cache_tree corrupt_blocks;
3891 struct btrfs_path path;
3892 struct btrfs_key key;
3893 struct btrfs_key found_key;
3896 struct block_info *bits;
3898 struct extent_buffer *leaf;
3900 struct btrfs_root_item ri;
3902 cache_tree_init(&extent_cache);
3903 cache_tree_init(&seen);
3904 cache_tree_init(&pending);
3905 cache_tree_init(&nodes);
3906 cache_tree_init(&reada);
3907 cache_tree_init(&corrupt_blocks);
3910 root->fs_info->fsck_extent_cache = &extent_cache;
3911 root->fs_info->free_extent_hook = free_extent_hook;
3912 root->fs_info->corrupt_blocks = &corrupt_blocks;
3916 bits = malloc(bits_nr * sizeof(struct block_info));
3922 add_root_to_pending(root->fs_info->tree_root->node,
3923 &extent_cache, &pending, &seen, &nodes,
3924 &root->fs_info->tree_root->root_key);
3926 add_root_to_pending(root->fs_info->chunk_root->node,
3927 &extent_cache, &pending, &seen, &nodes,
3928 &root->fs_info->chunk_root->root_key);
3930 btrfs_init_path(&path);
3933 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3934 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3938 leaf = path.nodes[0];
3939 slot = path.slots[0];
3940 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3941 ret = btrfs_next_leaf(root, &path);
3944 leaf = path.nodes[0];
3945 slot = path.slots[0];
3947 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3948 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3949 unsigned long offset;
3950 struct extent_buffer *buf;
3952 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3953 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3954 buf = read_tree_block(root->fs_info->tree_root,
3955 btrfs_root_bytenr(&ri),
3956 btrfs_level_size(root,
3957 btrfs_root_level(&ri)), 0);
3958 add_root_to_pending(buf, &extent_cache, &pending,
3959 &seen, &nodes, &found_key);
3960 free_extent_buffer(buf);
3964 btrfs_release_path(root, &path);
3966 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3967 &seen, &reada, &nodes, &extent_cache);
3971 ret = check_extent_refs(trans, root, &extent_cache, repair);
3974 free_corrupt_blocks(root->fs_info);
3975 root->fs_info->fsck_extent_cache = NULL;
3976 root->fs_info->free_extent_hook = NULL;
3977 root->fs_info->corrupt_blocks = NULL;
3984 static struct option long_options[] = {
3985 { "super", 1, NULL, 's' },
3986 { "repair", 0, NULL, 0 },
3987 { "init-csum-tree", 0, NULL, 0 },
3988 { "init-extent-tree", 0, NULL, 0 },
3992 const char * const cmd_check_usage[] = {
3993 "btrfs check [options] <device>",
3994 "Check an unmounted btrfs filesystem.",
3996 "-s|--super <superblock> use this superblock copy",
3997 "--repair try to repair the filesystem",
3998 "--init-csum-tree create a new CRC tree",
3999 "--init-extent-tree create a new extent tree",
4003 int cmd_check(int argc, char **argv)
4005 struct cache_tree root_cache;
4006 struct btrfs_root *root;
4007 struct btrfs_fs_info *info;
4008 struct btrfs_trans_handle *trans = NULL;
4014 int option_index = 0;
4015 int init_csum_tree = 0;
4020 c = getopt_long(argc, argv, "as:", long_options,
4025 case 'a': /* ignored */ break;
4028 bytenr = btrfs_sb_offset(num);
4029 printf("using SB copy %d, bytenr %llu\n", num,
4030 (unsigned long long)bytenr);
4034 usage(cmd_check_usage);
4036 if (option_index == 1) {
4037 printf("enabling repair mode\n");
4040 } else if (option_index == 2) {
4041 printf("Creating a new CRC tree\n");
4047 argc = argc - optind;
4050 usage(cmd_check_usage);
4053 cache_tree_init(&root_cache);
4055 if((ret = check_mounted(argv[optind])) < 0) {
4056 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
4059 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
4063 info = open_ctree_fs_info(argv[optind], bytenr, 0, rw, 1);
4065 fprintf(stderr, "Couldn't open file system\n");
4069 uuid_unparse(info->super_copy->fsid, uuidbuf);
4070 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
4072 if (!extent_buffer_uptodate(info->tree_root->node) ||
4073 !extent_buffer_uptodate(info->dev_root->node) ||
4074 !extent_buffer_uptodate(info->extent_root->node) ||
4075 !extent_buffer_uptodate(info->chunk_root->node)) {
4076 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
4080 root = info->fs_root;
4082 fprintf(stderr, "checking extents\n");
4084 trans = btrfs_start_transaction(root, 1);
4086 if (init_csum_tree) {
4087 fprintf(stderr, "Reinit crc root\n");
4088 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
4090 fprintf(stderr, "crc root initialization failed\n");
4095 ret = check_extents(trans, root, repair);
4097 fprintf(stderr, "Errors found in extent allocation tree\n");
4099 fprintf(stderr, "checking free space cache\n");
4100 ret = check_space_cache(root);
4104 fprintf(stderr, "checking fs roots\n");
4105 ret = check_fs_roots(root, &root_cache);
4109 fprintf(stderr, "checking csums\n");
4110 ret = check_csums(root);
4114 fprintf(stderr, "checking root refs\n");
4115 ret = check_root_refs(root, &root_cache);
4117 free_root_recs(&root_cache);
4119 ret = btrfs_commit_transaction(trans, root);
4125 if (found_old_backref) { /*
4126 * there was a disk format change when mixed
4127 * backref was in testing tree. The old format
4128 * existed about one week.
4130 printf("\n * Found old mixed backref format. "
4131 "The old format is not supported! *"
4132 "\n * Please mount the FS in readonly mode, "
4133 "backup data and re-format the FS. *\n\n");
4136 printf("found %llu bytes used err is %d\n",
4137 (unsigned long long)bytes_used, ret);
4138 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
4139 printf("total tree bytes: %llu\n",
4140 (unsigned long long)total_btree_bytes);
4141 printf("total fs tree bytes: %llu\n",
4142 (unsigned long long)total_fs_tree_bytes);
4143 printf("total extent tree bytes: %llu\n",
4144 (unsigned long long)total_extent_tree_bytes);
4145 printf("btree space waste bytes: %llu\n",
4146 (unsigned long long)btree_space_waste);
4147 printf("file data blocks allocated: %llu\n referenced %llu\n",
4148 (unsigned long long)data_bytes_allocated,
4149 (unsigned long long)data_bytes_referenced);
4150 printf("%s\n", BTRFS_BUILD_VERSION);