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) {
2804 if (cache->flags & (BTRFS_BLOCK_GROUP_METADATA |
2805 BTRFS_BLOCK_GROUP_SYSTEM))
2806 sectorsize = root->leafsize;
2808 sectorsize = root->sectorsize;
2810 if (btrfs_init_free_space_ctl(cache, sectorsize)) {
2815 btrfs_remove_free_space_cache(cache);
2818 ret = load_free_space_cache(root->fs_info, cache);
2822 ret = verify_space_cache(root, cache);
2824 fprintf(stderr, "cache appears valid but isnt %Lu\n",
2825 cache->key.objectid);
2830 return error ? -EINVAL : 0;
2833 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
2836 struct btrfs_path *path;
2837 struct extent_buffer *leaf;
2838 struct btrfs_key key;
2841 path = btrfs_alloc_path();
2843 fprintf(stderr, "Error allocing path\n");
2847 key.objectid = bytenr;
2848 key.type = BTRFS_EXTENT_ITEM_KEY;
2853 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
2856 fprintf(stderr, "Error looking up extent record %d\n", ret);
2857 btrfs_free_path(path);
2863 btrfs_prev_leaf(root, path);
2866 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2869 * Block group items come before extent items if they have the same
2870 * bytenr, so walk back one more just in case. Dear future traveler,
2871 * first congrats on mastering time travel. Now if it's not too much
2872 * trouble could you go back to 2006 and tell Chris to make the
2873 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
2875 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2879 btrfs_prev_leaf(root, path);
2883 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2884 ret = btrfs_next_leaf(root, path);
2886 fprintf(stderr, "Error going to next leaf "
2888 btrfs_free_path(path);
2894 leaf = path->nodes[0];
2895 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2896 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
2900 if (key.objectid + key.offset < bytenr) {
2904 if (key.objectid > bytenr + num_bytes)
2907 if (key.objectid == bytenr) {
2908 if (key.offset >= num_bytes) {
2912 num_bytes -= key.offset;
2913 bytenr += key.offset;
2914 } else if (key.objectid < bytenr) {
2915 if (key.objectid + key.offset >= bytenr + num_bytes) {
2919 num_bytes = (bytenr + num_bytes) -
2920 (key.objectid + key.offset);
2921 bytenr = key.objectid + key.offset;
2923 if (key.objectid + key.offset < bytenr + num_bytes) {
2924 u64 new_start = key.objectid + key.offset;
2925 u64 new_bytes = bytenr + num_bytes - new_start;
2928 * Weird case, the extent is in the middle of
2929 * our range, we'll have to search one side
2930 * and then the other. Not sure if this happens
2931 * in real life, but no harm in coding it up
2932 * anyway just in case.
2934 btrfs_release_path(root, path);
2935 ret = check_extent_exists(root, new_start,
2938 fprintf(stderr, "Right section didn't "
2942 num_bytes = key.objectid - bytenr;
2945 num_bytes = key.objectid - bytenr;
2952 fprintf(stderr, "There are no extents for csum range "
2953 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
2957 btrfs_free_path(path);
2961 static int check_csums(struct btrfs_root *root)
2963 struct btrfs_path *path;
2964 struct extent_buffer *leaf;
2965 struct btrfs_key key;
2966 u64 offset = 0, num_bytes = 0;
2967 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
2971 root = root->fs_info->csum_root;
2973 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
2974 key.type = BTRFS_EXTENT_CSUM_KEY;
2977 path = btrfs_alloc_path();
2981 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2983 fprintf(stderr, "Error searching csum tree %d\n", ret);
2984 btrfs_free_path(path);
2988 if (ret > 0 && path->slots[0])
2993 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2994 ret = btrfs_next_leaf(root, path);
2996 fprintf(stderr, "Error going to next leaf "
3003 leaf = path->nodes[0];
3005 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3006 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3012 offset = key.offset;
3013 } else if (key.offset != offset + num_bytes) {
3014 ret = check_extent_exists(root, offset, num_bytes);
3016 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3017 "there is no extent record\n",
3018 offset, offset+num_bytes);
3021 offset = key.offset;
3025 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3026 csum_size) * root->sectorsize;
3030 btrfs_free_path(path);
3034 static int run_next_block(struct btrfs_root *root,
3035 struct block_info *bits,
3038 struct cache_tree *pending,
3039 struct cache_tree *seen,
3040 struct cache_tree *reada,
3041 struct cache_tree *nodes,
3042 struct cache_tree *extent_cache)
3044 struct extent_buffer *buf;
3053 struct btrfs_key key;
3054 struct cache_extent *cache;
3057 ret = pick_next_pending(pending, reada, nodes, *last, bits,
3058 bits_nr, &reada_bits);
3063 for(i = 0; i < ret; i++) {
3064 insert_cache_extent(reada, bits[i].start,
3067 /* fixme, get the parent transid */
3068 readahead_tree_block(root, bits[i].start,
3072 *last = bits[0].start;
3073 bytenr = bits[0].start;
3074 size = bits[0].size;
3076 cache = find_cache_extent(pending, bytenr, size);
3078 remove_cache_extent(pending, cache);
3081 cache = find_cache_extent(reada, bytenr, size);
3083 remove_cache_extent(reada, cache);
3086 cache = find_cache_extent(nodes, bytenr, size);
3088 remove_cache_extent(nodes, cache);
3092 /* fixme, get the real parent transid */
3093 buf = read_tree_block(root, bytenr, size, 0);
3094 if (!extent_buffer_uptodate(buf)) {
3095 record_bad_block_io(root->fs_info,
3096 extent_cache, bytenr, size);
3100 nritems = btrfs_header_nritems(buf);
3102 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3103 btrfs_header_level(buf), 1, NULL,
3106 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3108 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3113 owner = btrfs_header_owner(buf);
3116 ret = check_block(root, extent_cache, buf, flags);
3120 if (btrfs_is_leaf(buf)) {
3121 btree_space_waste += btrfs_leaf_free_space(root, buf);
3122 for (i = 0; i < nritems; i++) {
3123 struct btrfs_file_extent_item *fi;
3124 btrfs_item_key_to_cpu(buf, &key, i);
3125 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3126 process_extent_item(root, extent_cache, buf,
3130 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3131 process_extent_item(root, extent_cache, buf,
3135 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3137 btrfs_item_size_nr(buf, i);
3140 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3143 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3144 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3145 process_extent_ref_v0(extent_cache, buf, i);
3152 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3153 add_tree_backref(extent_cache, key.objectid, 0,
3157 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3158 add_tree_backref(extent_cache, key.objectid,
3162 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3163 struct btrfs_extent_data_ref *ref;
3164 ref = btrfs_item_ptr(buf, i,
3165 struct btrfs_extent_data_ref);
3166 add_data_backref(extent_cache,
3168 btrfs_extent_data_ref_root(buf, ref),
3169 btrfs_extent_data_ref_objectid(buf,
3171 btrfs_extent_data_ref_offset(buf, ref),
3172 btrfs_extent_data_ref_count(buf, ref),
3173 0, root->sectorsize);
3176 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3177 struct btrfs_shared_data_ref *ref;
3178 ref = btrfs_item_ptr(buf, i,
3179 struct btrfs_shared_data_ref);
3180 add_data_backref(extent_cache,
3181 key.objectid, key.offset, 0, 0, 0,
3182 btrfs_shared_data_ref_count(buf, ref),
3183 0, root->sectorsize);
3186 if (key.type != BTRFS_EXTENT_DATA_KEY)
3188 fi = btrfs_item_ptr(buf, i,
3189 struct btrfs_file_extent_item);
3190 if (btrfs_file_extent_type(buf, fi) ==
3191 BTRFS_FILE_EXTENT_INLINE)
3193 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3196 data_bytes_allocated +=
3197 btrfs_file_extent_disk_num_bytes(buf, fi);
3198 if (data_bytes_allocated < root->sectorsize) {
3201 data_bytes_referenced +=
3202 btrfs_file_extent_num_bytes(buf, fi);
3203 ret = add_extent_rec(extent_cache, NULL,
3204 btrfs_file_extent_disk_bytenr(buf, fi),
3205 btrfs_file_extent_disk_num_bytes(buf, fi),
3207 btrfs_file_extent_disk_num_bytes(buf, fi));
3208 add_data_backref(extent_cache,
3209 btrfs_file_extent_disk_bytenr(buf, fi),
3210 parent, owner, key.objectid, key.offset -
3211 btrfs_file_extent_offset(buf, fi), 1, 1,
3212 btrfs_file_extent_disk_num_bytes(buf, fi));
3217 struct btrfs_key first_key;
3219 first_key.objectid = 0;
3222 btrfs_item_key_to_cpu(buf, &first_key, 0);
3223 level = btrfs_header_level(buf);
3224 for (i = 0; i < nritems; i++) {
3225 u64 ptr = btrfs_node_blockptr(buf, i);
3226 u32 size = btrfs_level_size(root, level - 1);
3227 btrfs_node_key_to_cpu(buf, &key, i);
3228 ret = add_extent_rec(extent_cache, &key,
3229 ptr, size, 0, 0, 1, 0, 1, size);
3232 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3235 add_pending(nodes, seen, ptr, size);
3237 add_pending(pending, seen, ptr, size);
3240 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3241 nritems) * sizeof(struct btrfs_key_ptr);
3243 total_btree_bytes += buf->len;
3244 if (fs_root_objectid(btrfs_header_owner(buf)))
3245 total_fs_tree_bytes += buf->len;
3246 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3247 total_extent_tree_bytes += buf->len;
3248 if (!found_old_backref &&
3249 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3250 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3251 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3252 found_old_backref = 1;
3254 free_extent_buffer(buf);
3258 static int add_root_to_pending(struct extent_buffer *buf,
3259 struct cache_tree *extent_cache,
3260 struct cache_tree *pending,
3261 struct cache_tree *seen,
3262 struct cache_tree *nodes,
3263 struct btrfs_key *root_key)
3265 if (btrfs_header_level(buf) > 0)
3266 add_pending(nodes, seen, buf->start, buf->len);
3268 add_pending(pending, seen, buf->start, buf->len);
3269 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3270 0, 1, 1, 0, 1, buf->len);
3272 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3273 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3274 add_tree_backref(extent_cache, buf->start, buf->start,
3277 add_tree_backref(extent_cache, buf->start, 0,
3278 root_key->objectid, 1);
3282 /* as we fix the tree, we might be deleting blocks that
3283 * we're tracking for repair. This hook makes sure we
3284 * remove any backrefs for blocks as we are fixing them.
3286 static int free_extent_hook(struct btrfs_trans_handle *trans,
3287 struct btrfs_root *root,
3288 u64 bytenr, u64 num_bytes, u64 parent,
3289 u64 root_objectid, u64 owner, u64 offset,
3292 struct extent_record *rec;
3293 struct cache_extent *cache;
3295 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3297 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3298 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
3302 rec = container_of(cache, struct extent_record, cache);
3304 struct data_backref *back;
3305 back = find_data_backref(rec, parent, root_objectid, owner,
3309 if (back->node.found_ref) {
3310 back->found_ref -= refs_to_drop;
3312 rec->refs -= refs_to_drop;
3314 if (back->node.found_extent_tree) {
3315 back->num_refs -= refs_to_drop;
3316 if (rec->extent_item_refs)
3317 rec->extent_item_refs -= refs_to_drop;
3319 if (back->found_ref == 0)
3320 back->node.found_ref = 0;
3321 if (back->num_refs == 0)
3322 back->node.found_extent_tree = 0;
3324 if (!back->node.found_extent_tree && back->node.found_ref) {
3325 list_del(&back->node.list);
3329 struct tree_backref *back;
3330 back = find_tree_backref(rec, parent, root_objectid);
3333 if (back->node.found_ref) {
3336 back->node.found_ref = 0;
3338 if (back->node.found_extent_tree) {
3339 if (rec->extent_item_refs)
3340 rec->extent_item_refs--;
3341 back->node.found_extent_tree = 0;
3343 if (!back->node.found_extent_tree && back->node.found_ref) {
3344 list_del(&back->node.list);
3348 maybe_free_extent_rec(extent_cache, rec);
3353 static int delete_extent_records(struct btrfs_trans_handle *trans,
3354 struct btrfs_root *root,
3355 struct btrfs_path *path,
3356 u64 bytenr, u64 new_len)
3358 struct btrfs_key key;
3359 struct btrfs_key found_key;
3360 struct extent_buffer *leaf;
3365 key.objectid = bytenr;
3367 key.offset = (u64)-1;
3370 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3377 if (path->slots[0] == 0)
3383 leaf = path->nodes[0];
3384 slot = path->slots[0];
3386 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3387 if (found_key.objectid != bytenr)
3390 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3391 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3392 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3393 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3394 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3395 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3396 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3397 btrfs_release_path(NULL, path);
3398 if (found_key.type == 0) {
3399 if (found_key.offset == 0)
3401 key.offset = found_key.offset - 1;
3402 key.type = found_key.type;
3404 key.type = found_key.type - 1;
3405 key.offset = (u64)-1;
3409 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
3410 found_key.objectid, found_key.type, found_key.offset);
3412 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
3415 btrfs_release_path(NULL, path);
3417 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3418 found_key.type == BTRFS_METADATA_ITEM_KEY) {
3419 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
3420 found_key.offset : root->leafsize;
3422 ret = btrfs_update_block_group(trans, root, bytenr,
3429 btrfs_release_path(NULL, path);
3434 * for a single backref, this will allocate a new extent
3435 * and add the backref to it.
3437 static int record_extent(struct btrfs_trans_handle *trans,
3438 struct btrfs_fs_info *info,
3439 struct btrfs_path *path,
3440 struct extent_record *rec,
3441 struct extent_backref *back,
3442 int allocated, u64 flags)
3445 struct btrfs_root *extent_root = info->extent_root;
3446 struct extent_buffer *leaf;
3447 struct btrfs_key ins_key;
3448 struct btrfs_extent_item *ei;
3449 struct tree_backref *tback;
3450 struct data_backref *dback;
3451 struct btrfs_tree_block_info *bi;
3454 rec->max_size = max_t(u64, rec->max_size,
3455 info->extent_root->leafsize);
3458 u32 item_size = sizeof(*ei);
3461 item_size += sizeof(*bi);
3463 ins_key.objectid = rec->start;
3464 ins_key.offset = rec->max_size;
3465 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3467 ret = btrfs_insert_empty_item(trans, extent_root, path,
3468 &ins_key, item_size);
3472 leaf = path->nodes[0];
3473 ei = btrfs_item_ptr(leaf, path->slots[0],
3474 struct btrfs_extent_item);
3476 btrfs_set_extent_refs(leaf, ei, 0);
3477 btrfs_set_extent_generation(leaf, ei, rec->generation);
3479 if (back->is_data) {
3480 btrfs_set_extent_flags(leaf, ei,
3481 BTRFS_EXTENT_FLAG_DATA);
3483 struct btrfs_disk_key copy_key;;
3485 tback = (struct tree_backref *)back;
3486 bi = (struct btrfs_tree_block_info *)(ei + 1);
3487 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3489 memset(©_key, 0, sizeof(copy_key));
3491 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3492 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3493 btrfs_set_tree_block_key(leaf, bi, ©_key);
3495 btrfs_set_extent_flags(leaf, ei,
3496 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3499 btrfs_mark_buffer_dirty(leaf);
3500 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3501 rec->max_size, 1, 0);
3504 btrfs_release_path(NULL, path);
3507 if (back->is_data) {
3511 dback = (struct data_backref *)back;
3512 if (back->full_backref)
3513 parent = dback->parent;
3517 for (i = 0; i < dback->found_ref; i++) {
3518 /* if parent != 0, we're doing a full backref
3519 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3520 * just makes the backref allocator create a data
3523 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3524 rec->start, rec->max_size,
3528 BTRFS_FIRST_FREE_OBJECTID :
3534 fprintf(stderr, "adding new data backref"
3535 " on %llu %s %llu owner %llu"
3536 " offset %llu found %d\n",
3537 (unsigned long long)rec->start,
3538 back->full_backref ?
3540 back->full_backref ?
3541 (unsigned long long)parent :
3542 (unsigned long long)dback->root,
3543 (unsigned long long)dback->owner,
3544 (unsigned long long)dback->offset,
3549 tback = (struct tree_backref *)back;
3550 if (back->full_backref)
3551 parent = tback->parent;
3555 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3556 rec->start, rec->max_size,
3557 parent, tback->root, 0, 0);
3558 fprintf(stderr, "adding new tree backref on "
3559 "start %llu len %llu parent %llu root %llu\n",
3560 rec->start, rec->max_size, tback->parent, tback->root);
3565 btrfs_release_path(NULL, path);
3570 * when an incorrect extent item is found, this will delete
3571 * all of the existing entries for it and recreate them
3572 * based on what the tree scan found.
3574 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
3575 struct btrfs_fs_info *info,
3576 struct extent_record *rec)
3579 struct btrfs_path *path;
3580 struct list_head *cur = rec->backrefs.next;
3581 struct cache_extent *cache;
3582 struct extent_backref *back;
3586 /* remember our flags for recreating the extent */
3587 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
3588 rec->max_size, rec->metadata, NULL,
3591 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3593 path = btrfs_alloc_path();
3595 /* step one, delete all the existing records */
3596 ret = delete_extent_records(trans, info->extent_root, path,
3597 rec->start, rec->max_size);
3602 /* was this block corrupt? If so, don't add references to it */
3603 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
3609 /* step two, recreate all the refs we did find */
3610 while(cur != &rec->backrefs) {
3611 back = list_entry(cur, struct extent_backref, list);
3615 * if we didn't find any references, don't create a
3618 if (!back->found_ref)
3621 ret = record_extent(trans, info, path, rec, back, allocated, flags);
3628 btrfs_free_path(path);
3632 /* right now we only prune from the extent allocation tree */
3633 static int prune_one_block(struct btrfs_trans_handle *trans,
3634 struct btrfs_fs_info *info,
3635 struct btrfs_corrupt_block *corrupt)
3638 struct btrfs_path path;
3639 struct extent_buffer *eb;
3643 int level = corrupt->level + 1;
3645 btrfs_init_path(&path);
3647 /* we want to stop at the parent to our busted block */
3648 path.lowest_level = level;
3650 ret = btrfs_search_slot(trans, info->extent_root,
3651 &corrupt->key, &path, -1, 1);
3656 eb = path.nodes[level];
3663 * hopefully the search gave us the block we want to prune,
3664 * lets try that first
3666 slot = path.slots[level];
3667 found = btrfs_node_blockptr(eb, slot);
3668 if (found == corrupt->cache.start)
3671 nritems = btrfs_header_nritems(eb);
3673 /* the search failed, lets scan this node and hope we find it */
3674 for (slot = 0; slot < nritems; slot++) {
3675 found = btrfs_node_blockptr(eb, slot);
3676 if (found == corrupt->cache.start)
3680 * we couldn't find the bad block. TODO, search all the nodes for pointers
3683 if (eb == info->extent_root->node) {
3688 btrfs_release_path(NULL, &path);
3693 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
3694 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
3697 btrfs_release_path(NULL, &path);
3701 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
3702 struct btrfs_fs_info *info)
3704 struct cache_extent *cache;
3705 struct btrfs_corrupt_block *corrupt;
3707 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3711 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3712 prune_one_block(trans, info, corrupt);
3713 cache = next_cache_extent(cache);
3718 static void free_corrupt_blocks(struct btrfs_fs_info *info)
3720 struct cache_extent *cache;
3721 struct btrfs_corrupt_block *corrupt;
3724 cache = find_first_cache_extent(info->corrupt_blocks, 0);
3727 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
3728 remove_cache_extent(info->corrupt_blocks, cache);
3733 static int check_block_group(struct btrfs_trans_handle *trans,
3734 struct btrfs_fs_info *info,
3735 struct map_lookup *map,
3738 struct btrfs_key key;
3739 struct btrfs_path path;
3742 key.objectid = map->ce.start;
3743 key.offset = map->ce.size;
3744 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
3746 btrfs_init_path(&path);
3747 ret = btrfs_search_slot(NULL, info->extent_root,
3749 btrfs_release_path(NULL, &path);
3753 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
3754 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3755 key.objectid, key.offset);
3761 static int check_block_groups(struct btrfs_trans_handle *trans,
3762 struct btrfs_fs_info *info, int *reinit)
3764 struct cache_extent *ce;
3765 struct map_lookup *map;
3766 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
3768 /* this isn't quite working */
3771 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
3775 map = container_of(ce, struct map_lookup, ce);
3776 check_block_group(trans, info, map, reinit);
3777 ce = next_cache_extent(ce);
3782 static int check_extent_refs(struct btrfs_trans_handle *trans,
3783 struct btrfs_root *root,
3784 struct cache_tree *extent_cache, int repair)
3786 struct extent_record *rec;
3787 struct cache_extent *cache;
3795 * if we're doing a repair, we have to make sure
3796 * we don't allocate from the problem extents.
3797 * In the worst case, this will be all the
3800 cache = find_first_cache_extent(extent_cache, 0);
3802 rec = container_of(cache, struct extent_record, cache);
3803 btrfs_pin_extent(root->fs_info,
3804 rec->start, rec->max_size);
3805 cache = next_cache_extent(cache);
3808 /* pin down all the corrupted blocks too */
3809 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
3811 rec = container_of(cache, struct extent_record, cache);
3812 btrfs_pin_extent(root->fs_info,
3813 rec->start, rec->max_size);
3814 cache = next_cache_extent(cache);
3816 prune_corrupt_blocks(trans, root->fs_info);
3817 check_block_groups(trans, root->fs_info, &reinit);
3819 btrfs_read_block_groups(root->fs_info->extent_root);
3823 cache = find_first_cache_extent(extent_cache, 0);
3826 rec = container_of(cache, struct extent_record, cache);
3827 if (rec->refs != rec->extent_item_refs) {
3828 fprintf(stderr, "ref mismatch on [%llu %llu] ",
3829 (unsigned long long)rec->start,
3830 (unsigned long long)rec->nr);
3831 fprintf(stderr, "extent item %llu, found %llu\n",
3832 (unsigned long long)rec->extent_item_refs,
3833 (unsigned long long)rec->refs);
3834 if (!fixed && repair) {
3835 ret = fixup_extent_refs(trans, root->fs_info, rec);
3843 if (all_backpointers_checked(rec, 1)) {
3844 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
3845 (unsigned long long)rec->start,
3846 (unsigned long long)rec->nr);
3848 if (!fixed && repair) {
3849 ret = fixup_extent_refs(trans, root->fs_info, rec);
3857 if (!rec->owner_ref_checked) {
3858 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
3859 (unsigned long long)rec->start,
3860 (unsigned long long)rec->nr);
3861 if (!fixed && repair) {
3862 ret = fixup_extent_refs(trans, root->fs_info, rec);
3870 remove_cache_extent(extent_cache, cache);
3871 free_all_extent_backrefs(rec);
3877 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
3880 btrfs_fix_block_accounting(trans, root);
3883 fprintf(stderr, "repaired damaged extent references\n");
3889 static int check_extents(struct btrfs_trans_handle *trans,
3890 struct btrfs_root *root, int repair)
3892 struct cache_tree extent_cache;
3893 struct cache_tree seen;
3894 struct cache_tree pending;
3895 struct cache_tree reada;
3896 struct cache_tree nodes;
3897 struct cache_tree corrupt_blocks;
3898 struct btrfs_path path;
3899 struct btrfs_key key;
3900 struct btrfs_key found_key;
3903 struct block_info *bits;
3905 struct extent_buffer *leaf;
3907 struct btrfs_root_item ri;
3909 cache_tree_init(&extent_cache);
3910 cache_tree_init(&seen);
3911 cache_tree_init(&pending);
3912 cache_tree_init(&nodes);
3913 cache_tree_init(&reada);
3914 cache_tree_init(&corrupt_blocks);
3917 root->fs_info->fsck_extent_cache = &extent_cache;
3918 root->fs_info->free_extent_hook = free_extent_hook;
3919 root->fs_info->corrupt_blocks = &corrupt_blocks;
3923 bits = malloc(bits_nr * sizeof(struct block_info));
3929 add_root_to_pending(root->fs_info->tree_root->node,
3930 &extent_cache, &pending, &seen, &nodes,
3931 &root->fs_info->tree_root->root_key);
3933 add_root_to_pending(root->fs_info->chunk_root->node,
3934 &extent_cache, &pending, &seen, &nodes,
3935 &root->fs_info->chunk_root->root_key);
3937 btrfs_init_path(&path);
3940 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
3941 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
3945 leaf = path.nodes[0];
3946 slot = path.slots[0];
3947 if (slot >= btrfs_header_nritems(path.nodes[0])) {
3948 ret = btrfs_next_leaf(root, &path);
3951 leaf = path.nodes[0];
3952 slot = path.slots[0];
3954 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
3955 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
3956 unsigned long offset;
3957 struct extent_buffer *buf;
3959 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
3960 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
3961 buf = read_tree_block(root->fs_info->tree_root,
3962 btrfs_root_bytenr(&ri),
3963 btrfs_level_size(root,
3964 btrfs_root_level(&ri)), 0);
3965 add_root_to_pending(buf, &extent_cache, &pending,
3966 &seen, &nodes, &found_key);
3967 free_extent_buffer(buf);
3971 btrfs_release_path(root, &path);
3973 ret = run_next_block(root, bits, bits_nr, &last, &pending,
3974 &seen, &reada, &nodes, &extent_cache);
3978 ret = check_extent_refs(trans, root, &extent_cache, repair);
3981 free_corrupt_blocks(root->fs_info);
3982 root->fs_info->fsck_extent_cache = NULL;
3983 root->fs_info->free_extent_hook = NULL;
3984 root->fs_info->corrupt_blocks = NULL;
3991 static struct option long_options[] = {
3992 { "super", 1, NULL, 's' },
3993 { "repair", 0, NULL, 0 },
3994 { "init-csum-tree", 0, NULL, 0 },
3995 { "init-extent-tree", 0, NULL, 0 },
3999 const char * const cmd_check_usage[] = {
4000 "btrfs check [options] <device>",
4001 "Check an unmounted btrfs filesystem.",
4003 "-s|--super <superblock> use this superblock copy",
4004 "--repair try to repair the filesystem",
4005 "--init-csum-tree create a new CRC tree",
4006 "--init-extent-tree create a new extent tree",
4010 int cmd_check(int argc, char **argv)
4012 struct cache_tree root_cache;
4013 struct btrfs_root *root;
4014 struct btrfs_fs_info *info;
4015 struct btrfs_trans_handle *trans = NULL;
4021 int option_index = 0;
4022 int init_csum_tree = 0;
4027 c = getopt_long(argc, argv, "as:", long_options,
4032 case 'a': /* ignored */ break;
4035 bytenr = btrfs_sb_offset(num);
4036 printf("using SB copy %d, bytenr %llu\n", num,
4037 (unsigned long long)bytenr);
4041 usage(cmd_check_usage);
4043 if (option_index == 1) {
4044 printf("enabling repair mode\n");
4047 } else if (option_index == 2) {
4048 printf("Creating a new CRC tree\n");
4054 argc = argc - optind;
4057 usage(cmd_check_usage);
4060 cache_tree_init(&root_cache);
4062 if((ret = check_mounted(argv[optind])) < 0) {
4063 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
4066 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
4070 info = open_ctree_fs_info(argv[optind], bytenr, 0, rw, 1);
4072 fprintf(stderr, "Couldn't open file system\n");
4076 uuid_unparse(info->super_copy->fsid, uuidbuf);
4077 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
4079 if (!extent_buffer_uptodate(info->tree_root->node) ||
4080 !extent_buffer_uptodate(info->dev_root->node) ||
4081 !extent_buffer_uptodate(info->extent_root->node) ||
4082 !extent_buffer_uptodate(info->chunk_root->node)) {
4083 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
4087 root = info->fs_root;
4089 fprintf(stderr, "checking extents\n");
4091 trans = btrfs_start_transaction(root, 1);
4093 if (init_csum_tree) {
4094 fprintf(stderr, "Reinit crc root\n");
4095 ret = btrfs_fsck_reinit_root(trans, info->csum_root);
4097 fprintf(stderr, "crc root initialization failed\n");
4102 ret = check_extents(trans, root, repair);
4104 fprintf(stderr, "Errors found in extent allocation tree\n");
4106 fprintf(stderr, "checking free space cache\n");
4107 ret = check_space_cache(root);
4111 fprintf(stderr, "checking fs roots\n");
4112 ret = check_fs_roots(root, &root_cache);
4116 fprintf(stderr, "checking csums\n");
4117 ret = check_csums(root);
4121 fprintf(stderr, "checking root refs\n");
4122 ret = check_root_refs(root, &root_cache);
4124 free_root_recs(&root_cache);
4126 ret = btrfs_commit_transaction(trans, root);
4132 if (found_old_backref) { /*
4133 * there was a disk format change when mixed
4134 * backref was in testing tree. The old format
4135 * existed about one week.
4137 printf("\n * Found old mixed backref format. "
4138 "The old format is not supported! *"
4139 "\n * Please mount the FS in readonly mode, "
4140 "backup data and re-format the FS. *\n\n");
4143 printf("found %llu bytes used err is %d\n",
4144 (unsigned long long)bytes_used, ret);
4145 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
4146 printf("total tree bytes: %llu\n",
4147 (unsigned long long)total_btree_bytes);
4148 printf("total fs tree bytes: %llu\n",
4149 (unsigned long long)total_fs_tree_bytes);
4150 printf("total extent tree bytes: %llu\n",
4151 (unsigned long long)total_extent_tree_bytes);
4152 printf("btree space waste bytes: %llu\n",
4153 (unsigned long long)btree_space_waste);
4154 printf("file data blocks allocated: %llu\n referenced %llu\n",
4155 (unsigned long long)data_bytes_allocated,
4156 (unsigned long long)data_bytes_referenced);
4157 printf("%s\n", BTRFS_BUILD_VERSION);