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;
52 static LIST_HEAD(duplicate_extents);
54 struct extent_backref {
55 struct list_head list;
56 unsigned int is_data:1;
57 unsigned int found_extent_tree:1;
58 unsigned int full_backref:1;
59 unsigned int found_ref:1;
63 struct extent_backref node;
78 struct extent_backref node;
85 struct extent_record {
86 struct list_head backrefs;
87 struct list_head dups;
88 struct list_head list;
89 struct cache_extent cache;
90 struct btrfs_disk_key parent_key;
91 unsigned int found_rec;
101 unsigned int content_checked:1;
102 unsigned int owner_ref_checked:1;
103 unsigned int is_root:1;
104 unsigned int metadata:1;
107 struct inode_backref {
108 struct list_head list;
109 unsigned int found_dir_item:1;
110 unsigned int found_dir_index:1;
111 unsigned int found_inode_ref:1;
112 unsigned int filetype:8;
114 unsigned int ref_type;
121 #define REF_ERR_NO_DIR_ITEM (1 << 0)
122 #define REF_ERR_NO_DIR_INDEX (1 << 1)
123 #define REF_ERR_NO_INODE_REF (1 << 2)
124 #define REF_ERR_DUP_DIR_ITEM (1 << 3)
125 #define REF_ERR_DUP_DIR_INDEX (1 << 4)
126 #define REF_ERR_DUP_INODE_REF (1 << 5)
127 #define REF_ERR_INDEX_UNMATCH (1 << 6)
128 #define REF_ERR_FILETYPE_UNMATCH (1 << 7)
129 #define REF_ERR_NAME_TOO_LONG (1 << 8) // 100
130 #define REF_ERR_NO_ROOT_REF (1 << 9)
131 #define REF_ERR_NO_ROOT_BACKREF (1 << 10)
132 #define REF_ERR_DUP_ROOT_REF (1 << 11)
133 #define REF_ERR_DUP_ROOT_BACKREF (1 << 12)
135 struct inode_record {
136 struct list_head backrefs;
137 unsigned int checked:1;
138 unsigned int merging:1;
139 unsigned int found_inode_item:1;
140 unsigned int found_dir_item:1;
141 unsigned int found_file_extent:1;
142 unsigned int found_csum_item:1;
143 unsigned int some_csum_missing:1;
144 unsigned int nodatasum:1;
157 u64 first_extent_gap;
162 #define I_ERR_NO_INODE_ITEM (1 << 0)
163 #define I_ERR_NO_ORPHAN_ITEM (1 << 1)
164 #define I_ERR_DUP_INODE_ITEM (1 << 2)
165 #define I_ERR_DUP_DIR_INDEX (1 << 3)
166 #define I_ERR_ODD_DIR_ITEM (1 << 4)
167 #define I_ERR_ODD_FILE_EXTENT (1 << 5)
168 #define I_ERR_BAD_FILE_EXTENT (1 << 6)
169 #define I_ERR_FILE_EXTENT_OVERLAP (1 << 7)
170 #define I_ERR_FILE_EXTENT_DISCOUNT (1 << 8) // 100
171 #define I_ERR_DIR_ISIZE_WRONG (1 << 9)
172 #define I_ERR_FILE_NBYTES_WRONG (1 << 10) // 400
173 #define I_ERR_ODD_CSUM_ITEM (1 << 11)
174 #define I_ERR_SOME_CSUM_MISSING (1 << 12)
175 #define I_ERR_LINK_COUNT_WRONG (1 << 13)
177 struct root_backref {
178 struct list_head list;
179 unsigned int found_dir_item:1;
180 unsigned int found_dir_index:1;
181 unsigned int found_back_ref:1;
182 unsigned int found_forward_ref:1;
183 unsigned int reachable:1;
193 struct list_head backrefs;
194 struct cache_extent cache;
195 unsigned int found_root_item:1;
201 struct cache_extent cache;
206 struct cache_extent cache;
207 struct cache_tree root_cache;
208 struct cache_tree inode_cache;
209 struct inode_record *current;
218 struct walk_control {
219 struct cache_tree shared;
220 struct shared_node *nodes[BTRFS_MAX_LEVEL];
225 static u8 imode_to_type(u32 imode)
228 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
229 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
230 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
231 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
232 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
233 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
234 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
235 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
238 return btrfs_type_by_mode[(imode & S_IFMT) >> S_SHIFT];
242 static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
244 struct inode_record *rec;
245 struct inode_backref *backref;
246 struct inode_backref *orig;
249 rec = malloc(sizeof(*rec));
250 memcpy(rec, orig_rec, sizeof(*rec));
252 INIT_LIST_HEAD(&rec->backrefs);
254 list_for_each_entry(orig, &orig_rec->backrefs, list) {
255 size = sizeof(*orig) + orig->namelen + 1;
256 backref = malloc(size);
257 memcpy(backref, orig, size);
258 list_add_tail(&backref->list, &rec->backrefs);
263 static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
266 struct ptr_node *node;
267 struct cache_extent *cache;
268 struct inode_record *rec = NULL;
271 cache = find_cache_extent(inode_cache, ino, 1);
273 node = container_of(cache, struct ptr_node, cache);
275 if (mod && rec->refs > 1) {
276 node->data = clone_inode_rec(rec);
281 rec = calloc(1, sizeof(*rec));
283 rec->extent_start = (u64)-1;
284 rec->first_extent_gap = (u64)-1;
286 INIT_LIST_HEAD(&rec->backrefs);
288 node = malloc(sizeof(*node));
289 node->cache.start = ino;
290 node->cache.size = 1;
293 if (ino == BTRFS_FREE_INO_OBJECTID)
296 ret = insert_existing_cache_extent(inode_cache, &node->cache);
302 static void free_inode_rec(struct inode_record *rec)
304 struct inode_backref *backref;
309 while (!list_empty(&rec->backrefs)) {
310 backref = list_entry(rec->backrefs.next,
311 struct inode_backref, list);
312 list_del(&backref->list);
318 static int can_free_inode_rec(struct inode_record *rec)
320 if (!rec->errors && rec->checked && rec->found_inode_item &&
321 rec->nlink == rec->found_link && list_empty(&rec->backrefs))
326 static void maybe_free_inode_rec(struct cache_tree *inode_cache,
327 struct inode_record *rec)
329 struct cache_extent *cache;
330 struct inode_backref *tmp, *backref;
331 struct ptr_node *node;
332 unsigned char filetype;
334 if (!rec->found_inode_item)
337 filetype = imode_to_type(rec->imode);
338 list_for_each_entry_safe(backref, tmp, &rec->backrefs, list) {
339 if (backref->found_dir_item && backref->found_dir_index) {
340 if (backref->filetype != filetype)
341 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
342 if (!backref->errors && backref->found_inode_ref) {
343 list_del(&backref->list);
349 if (!rec->checked || rec->merging)
352 if (S_ISDIR(rec->imode)) {
353 if (rec->found_size != rec->isize)
354 rec->errors |= I_ERR_DIR_ISIZE_WRONG;
355 if (rec->found_file_extent)
356 rec->errors |= I_ERR_ODD_FILE_EXTENT;
357 } else if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
358 if (rec->found_dir_item)
359 rec->errors |= I_ERR_ODD_DIR_ITEM;
360 if (rec->found_size != rec->nbytes)
361 rec->errors |= I_ERR_FILE_NBYTES_WRONG;
362 if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
363 rec->first_extent_gap = 0;
364 if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
365 rec->first_extent_gap < rec->isize))
366 rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
369 if (S_ISREG(rec->imode) || S_ISLNK(rec->imode)) {
370 if (rec->found_csum_item && rec->nodatasum)
371 rec->errors |= I_ERR_ODD_CSUM_ITEM;
372 if (rec->some_csum_missing && !rec->nodatasum)
373 rec->errors |= I_ERR_SOME_CSUM_MISSING;
376 BUG_ON(rec->refs != 1);
377 if (can_free_inode_rec(rec)) {
378 cache = find_cache_extent(inode_cache, rec->ino, 1);
379 node = container_of(cache, struct ptr_node, cache);
380 BUG_ON(node->data != rec);
381 remove_cache_extent(inode_cache, &node->cache);
387 static int check_orphan_item(struct btrfs_root *root, u64 ino)
389 struct btrfs_path path;
390 struct btrfs_key key;
393 key.objectid = BTRFS_ORPHAN_OBJECTID;
394 key.type = BTRFS_ORPHAN_ITEM_KEY;
397 btrfs_init_path(&path);
398 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
399 btrfs_release_path(root, &path);
405 static int process_inode_item(struct extent_buffer *eb,
406 int slot, struct btrfs_key *key,
407 struct shared_node *active_node)
409 struct inode_record *rec;
410 struct btrfs_inode_item *item;
412 rec = active_node->current;
413 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
414 if (rec->found_inode_item) {
415 rec->errors |= I_ERR_DUP_INODE_ITEM;
418 item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item);
419 rec->nlink = btrfs_inode_nlink(eb, item);
420 rec->isize = btrfs_inode_size(eb, item);
421 rec->nbytes = btrfs_inode_nbytes(eb, item);
422 rec->imode = btrfs_inode_mode(eb, item);
423 if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM)
425 rec->found_inode_item = 1;
427 rec->errors |= I_ERR_NO_ORPHAN_ITEM;
428 maybe_free_inode_rec(&active_node->inode_cache, rec);
432 static struct inode_backref *get_inode_backref(struct inode_record *rec,
434 int namelen, u64 dir)
436 struct inode_backref *backref;
438 list_for_each_entry(backref, &rec->backrefs, list) {
439 if (backref->dir != dir || backref->namelen != namelen)
441 if (memcmp(name, backref->name, namelen))
446 backref = malloc(sizeof(*backref) + namelen + 1);
447 memset(backref, 0, sizeof(*backref));
449 backref->namelen = namelen;
450 memcpy(backref->name, name, namelen);
451 backref->name[namelen] = '\0';
452 list_add_tail(&backref->list, &rec->backrefs);
456 static int add_inode_backref(struct cache_tree *inode_cache,
457 u64 ino, u64 dir, u64 index,
458 const char *name, int namelen,
459 int filetype, int itemtype, int errors)
461 struct inode_record *rec;
462 struct inode_backref *backref;
464 rec = get_inode_rec(inode_cache, ino, 1);
465 backref = get_inode_backref(rec, name, namelen, dir);
467 backref->errors |= errors;
468 if (itemtype == BTRFS_DIR_INDEX_KEY) {
469 if (backref->found_dir_index)
470 backref->errors |= REF_ERR_DUP_DIR_INDEX;
471 if (backref->found_inode_ref && backref->index != index)
472 backref->errors |= REF_ERR_INDEX_UNMATCH;
473 if (backref->found_dir_item && backref->filetype != filetype)
474 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
476 backref->index = index;
477 backref->filetype = filetype;
478 backref->found_dir_index = 1;
479 } else if (itemtype == BTRFS_DIR_ITEM_KEY) {
481 if (backref->found_dir_item)
482 backref->errors |= REF_ERR_DUP_DIR_ITEM;
483 if (backref->found_dir_index && backref->filetype != filetype)
484 backref->errors |= REF_ERR_FILETYPE_UNMATCH;
486 backref->filetype = filetype;
487 backref->found_dir_item = 1;
488 } else if ((itemtype == BTRFS_INODE_REF_KEY) ||
489 (itemtype == BTRFS_INODE_EXTREF_KEY)) {
490 if (backref->found_inode_ref)
491 backref->errors |= REF_ERR_DUP_INODE_REF;
492 if (backref->found_dir_index && backref->index != index)
493 backref->errors |= REF_ERR_INDEX_UNMATCH;
495 backref->ref_type = itemtype;
496 backref->index = index;
497 backref->found_inode_ref = 1;
502 maybe_free_inode_rec(inode_cache, rec);
506 static int merge_inode_recs(struct inode_record *src, struct inode_record *dst,
507 struct cache_tree *dst_cache)
509 struct inode_backref *backref;
513 list_for_each_entry(backref, &src->backrefs, list) {
514 if (backref->found_dir_index) {
515 add_inode_backref(dst_cache, dst->ino, backref->dir,
516 backref->index, backref->name,
517 backref->namelen, backref->filetype,
518 BTRFS_DIR_INDEX_KEY, backref->errors);
520 if (backref->found_dir_item) {
522 add_inode_backref(dst_cache, dst->ino,
523 backref->dir, 0, backref->name,
524 backref->namelen, backref->filetype,
525 BTRFS_DIR_ITEM_KEY, backref->errors);
527 if (backref->found_inode_ref) {
528 add_inode_backref(dst_cache, dst->ino,
529 backref->dir, backref->index,
530 backref->name, backref->namelen, 0,
531 backref->ref_type, backref->errors);
535 if (src->found_dir_item)
536 dst->found_dir_item = 1;
537 if (src->found_file_extent)
538 dst->found_file_extent = 1;
539 if (src->found_csum_item)
540 dst->found_csum_item = 1;
541 if (src->some_csum_missing)
542 dst->some_csum_missing = 1;
543 if (dst->first_extent_gap > src->first_extent_gap)
544 dst->first_extent_gap = src->first_extent_gap;
546 BUG_ON(src->found_link < dir_count);
547 dst->found_link += src->found_link - dir_count;
548 dst->found_size += src->found_size;
549 if (src->extent_start != (u64)-1) {
550 if (dst->extent_start == (u64)-1) {
551 dst->extent_start = src->extent_start;
552 dst->extent_end = src->extent_end;
554 if (dst->extent_end > src->extent_start)
555 dst->errors |= I_ERR_FILE_EXTENT_OVERLAP;
556 else if (dst->extent_end < src->extent_start &&
557 dst->extent_end < dst->first_extent_gap)
558 dst->first_extent_gap = dst->extent_end;
559 if (dst->extent_end < src->extent_end)
560 dst->extent_end = src->extent_end;
564 dst->errors |= src->errors;
565 if (src->found_inode_item) {
566 if (!dst->found_inode_item) {
567 dst->nlink = src->nlink;
568 dst->isize = src->isize;
569 dst->nbytes = src->nbytes;
570 dst->imode = src->imode;
571 dst->nodatasum = src->nodatasum;
572 dst->found_inode_item = 1;
574 dst->errors |= I_ERR_DUP_INODE_ITEM;
582 static int splice_shared_node(struct shared_node *src_node,
583 struct shared_node *dst_node)
585 struct cache_extent *cache;
586 struct ptr_node *node, *ins;
587 struct cache_tree *src, *dst;
588 struct inode_record *rec, *conflict;
593 if (--src_node->refs == 0)
595 if (src_node->current)
596 current_ino = src_node->current->ino;
598 src = &src_node->root_cache;
599 dst = &dst_node->root_cache;
601 cache = find_first_cache_extent(src, 0);
603 node = container_of(cache, struct ptr_node, cache);
605 cache = next_cache_extent(cache);
608 remove_cache_extent(src, &node->cache);
611 ins = malloc(sizeof(*ins));
612 ins->cache.start = node->cache.start;
613 ins->cache.size = node->cache.size;
617 ret = insert_existing_cache_extent(dst, &ins->cache);
618 if (ret == -EEXIST) {
619 conflict = get_inode_rec(dst, rec->ino, 1);
620 merge_inode_recs(rec, conflict, dst);
622 conflict->checked = 1;
623 if (dst_node->current == conflict)
624 dst_node->current = NULL;
626 maybe_free_inode_rec(dst, conflict);
634 if (src == &src_node->root_cache) {
635 src = &src_node->inode_cache;
636 dst = &dst_node->inode_cache;
640 if (current_ino > 0 && (!dst_node->current ||
641 current_ino > dst_node->current->ino)) {
642 if (dst_node->current) {
643 dst_node->current->checked = 1;
644 maybe_free_inode_rec(dst, dst_node->current);
646 dst_node->current = get_inode_rec(dst, current_ino, 1);
651 static void free_inode_recs(struct cache_tree *inode_cache)
653 struct cache_extent *cache;
654 struct ptr_node *node;
655 struct inode_record *rec;
658 cache = find_first_cache_extent(inode_cache, 0);
661 node = container_of(cache, struct ptr_node, cache);
663 remove_cache_extent(inode_cache, &node->cache);
669 static struct shared_node *find_shared_node(struct cache_tree *shared,
672 struct cache_extent *cache;
673 struct shared_node *node;
675 cache = find_cache_extent(shared, bytenr, 1);
677 node = container_of(cache, struct shared_node, cache);
683 static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
686 struct shared_node *node;
688 node = calloc(1, sizeof(*node));
689 node->cache.start = bytenr;
690 node->cache.size = 1;
691 cache_tree_init(&node->root_cache);
692 cache_tree_init(&node->inode_cache);
695 ret = insert_existing_cache_extent(shared, &node->cache);
700 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
701 struct walk_control *wc, int level)
703 struct shared_node *node;
704 struct shared_node *dest;
706 if (level == wc->active_node)
709 BUG_ON(wc->active_node <= level);
710 node = find_shared_node(&wc->shared, bytenr);
712 add_shared_node(&wc->shared, bytenr, refs);
713 node = find_shared_node(&wc->shared, bytenr);
714 wc->nodes[level] = node;
715 wc->active_node = level;
719 if (wc->root_level == wc->active_node &&
720 btrfs_root_refs(&root->root_item) == 0) {
721 if (--node->refs == 0) {
722 free_inode_recs(&node->root_cache);
723 free_inode_recs(&node->inode_cache);
724 remove_cache_extent(&wc->shared, &node->cache);
730 dest = wc->nodes[wc->active_node];
731 splice_shared_node(node, dest);
732 if (node->refs == 0) {
733 remove_cache_extent(&wc->shared, &node->cache);
739 static int leave_shared_node(struct btrfs_root *root,
740 struct walk_control *wc, int level)
742 struct shared_node *node;
743 struct shared_node *dest;
746 if (level == wc->root_level)
749 for (i = level + 1; i < BTRFS_MAX_LEVEL; i++) {
753 BUG_ON(i >= BTRFS_MAX_LEVEL);
755 node = wc->nodes[wc->active_node];
756 wc->nodes[wc->active_node] = NULL;
759 dest = wc->nodes[wc->active_node];
760 if (wc->active_node < wc->root_level ||
761 btrfs_root_refs(&root->root_item) > 0) {
762 BUG_ON(node->refs <= 1);
763 splice_shared_node(node, dest);
765 BUG_ON(node->refs < 2);
771 static int is_child_root(struct btrfs_root *root, u64 parent_root_id,
774 struct btrfs_path path;
775 struct btrfs_key key;
776 struct extent_buffer *leaf;
780 btrfs_init_path(&path);
782 key.objectid = parent_root_id;
783 key.type = BTRFS_ROOT_REF_KEY;
784 key.offset = child_root_id;
785 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
788 btrfs_release_path(root, &path);
792 key.objectid = child_root_id;
793 key.type = BTRFS_ROOT_BACKREF_KEY;
795 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, &path,
800 leaf = path.nodes[0];
801 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
802 ret = btrfs_next_leaf(root->fs_info->tree_root, &path);
807 leaf = path.nodes[0];
810 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
811 if (key.objectid != child_root_id ||
812 key.type != BTRFS_ROOT_BACKREF_KEY)
817 if (key.offset == parent_root_id) {
818 btrfs_release_path(root, &path);
825 btrfs_release_path(root, &path);
826 return has_parent? 0 : -1;
829 static int process_dir_item(struct btrfs_root *root,
830 struct extent_buffer *eb,
831 int slot, struct btrfs_key *key,
832 struct shared_node *active_node)
842 struct btrfs_dir_item *di;
843 struct inode_record *rec;
844 struct cache_tree *root_cache;
845 struct cache_tree *inode_cache;
846 struct btrfs_key location;
847 char namebuf[BTRFS_NAME_LEN];
849 root_cache = &active_node->root_cache;
850 inode_cache = &active_node->inode_cache;
851 rec = active_node->current;
852 rec->found_dir_item = 1;
854 di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item);
855 total = btrfs_item_size_nr(eb, slot);
856 while (cur < total) {
858 btrfs_dir_item_key_to_cpu(eb, di, &location);
859 name_len = btrfs_dir_name_len(eb, di);
860 data_len = btrfs_dir_data_len(eb, di);
861 filetype = btrfs_dir_type(eb, di);
863 rec->found_size += name_len;
864 if (name_len <= BTRFS_NAME_LEN) {
868 len = BTRFS_NAME_LEN;
869 error = REF_ERR_NAME_TOO_LONG;
871 read_extent_buffer(eb, namebuf, (unsigned long)(di + 1), len);
873 if (location.type == BTRFS_INODE_ITEM_KEY) {
874 add_inode_backref(inode_cache, location.objectid,
875 key->objectid, key->offset, namebuf,
876 len, filetype, key->type, error);
877 } else if (location.type == BTRFS_ROOT_ITEM_KEY) {
878 add_inode_backref(root_cache, location.objectid,
879 key->objectid, key->offset,
880 namebuf, len, filetype,
883 fprintf(stderr, "warning line %d\n", __LINE__);
886 len = sizeof(*di) + name_len + data_len;
887 di = (struct btrfs_dir_item *)((char *)di + len);
890 if (key->type == BTRFS_DIR_INDEX_KEY && nritems > 1)
891 rec->errors |= I_ERR_DUP_DIR_INDEX;
896 static int process_inode_ref(struct extent_buffer *eb,
897 int slot, struct btrfs_key *key,
898 struct shared_node *active_node)
906 struct cache_tree *inode_cache;
907 struct btrfs_inode_ref *ref;
908 char namebuf[BTRFS_NAME_LEN];
910 inode_cache = &active_node->inode_cache;
912 ref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref);
913 total = btrfs_item_size_nr(eb, slot);
914 while (cur < total) {
915 name_len = btrfs_inode_ref_name_len(eb, ref);
916 index = btrfs_inode_ref_index(eb, ref);
917 if (name_len <= BTRFS_NAME_LEN) {
921 len = BTRFS_NAME_LEN;
922 error = REF_ERR_NAME_TOO_LONG;
924 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
925 add_inode_backref(inode_cache, key->objectid, key->offset,
926 index, namebuf, len, 0, key->type, error);
928 len = sizeof(*ref) + name_len;
929 ref = (struct btrfs_inode_ref *)((char *)ref + len);
935 static int process_inode_extref(struct extent_buffer *eb,
936 int slot, struct btrfs_key *key,
937 struct shared_node *active_node)
946 struct cache_tree *inode_cache;
947 struct btrfs_inode_extref *extref;
948 char namebuf[BTRFS_NAME_LEN];
950 inode_cache = &active_node->inode_cache;
952 extref = btrfs_item_ptr(eb, slot, struct btrfs_inode_extref);
953 total = btrfs_item_size_nr(eb, slot);
954 while (cur < total) {
955 name_len = btrfs_inode_extref_name_len(eb, extref);
956 index = btrfs_inode_extref_index(eb, extref);
957 parent = btrfs_inode_extref_parent(eb, extref);
958 if (name_len <= BTRFS_NAME_LEN) {
962 len = BTRFS_NAME_LEN;
963 error = REF_ERR_NAME_TOO_LONG;
965 read_extent_buffer(eb, namebuf,
966 (unsigned long)(extref + 1), len);
967 add_inode_backref(inode_cache, key->objectid, parent,
968 index, namebuf, len, 0, key->type, error);
970 len = sizeof(*extref) + name_len;
971 extref = (struct btrfs_inode_extref *)((char *)extref + len);
978 static u64 count_csum_range(struct btrfs_root *root, u64 start, u64 len)
980 struct btrfs_key key;
981 struct btrfs_path path;
982 struct extent_buffer *leaf;
987 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
989 btrfs_init_path(&path);
991 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
993 key.type = BTRFS_EXTENT_CSUM_KEY;
995 ret = btrfs_search_slot(NULL, root->fs_info->csum_root,
998 if (ret > 0 && path.slots[0] > 0) {
999 leaf = path.nodes[0];
1000 btrfs_item_key_to_cpu(leaf, &key, path.slots[0] - 1);
1001 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
1002 key.type == BTRFS_EXTENT_CSUM_KEY)
1007 leaf = path.nodes[0];
1008 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1009 ret = btrfs_next_leaf(root->fs_info->csum_root, &path);
1013 leaf = path.nodes[0];
1016 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1017 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
1018 key.type != BTRFS_EXTENT_CSUM_KEY)
1021 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1022 if (key.offset >= start + len)
1025 if (key.offset > start)
1028 size = btrfs_item_size_nr(leaf, path.slots[0]);
1029 csum_end = key.offset + (size / csum_size) * root->sectorsize;
1030 if (csum_end > start) {
1031 size = min(csum_end - start, len);
1039 btrfs_release_path(root->fs_info->csum_root, &path);
1043 static int process_file_extent(struct btrfs_root *root,
1044 struct extent_buffer *eb,
1045 int slot, struct btrfs_key *key,
1046 struct shared_node *active_node)
1048 struct inode_record *rec;
1049 struct btrfs_file_extent_item *fi;
1051 u64 disk_bytenr = 0;
1052 u64 extent_offset = 0;
1053 u64 mask = root->sectorsize - 1;
1056 rec = active_node->current;
1057 BUG_ON(rec->ino != key->objectid || rec->refs > 1);
1058 rec->found_file_extent = 1;
1060 if (rec->extent_start == (u64)-1) {
1061 rec->extent_start = key->offset;
1062 rec->extent_end = key->offset;
1065 if (rec->extent_end > key->offset)
1066 rec->errors |= I_ERR_FILE_EXTENT_OVERLAP;
1067 else if (rec->extent_end < key->offset &&
1068 rec->extent_end < rec->first_extent_gap)
1069 rec->first_extent_gap = rec->extent_end;
1071 fi = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item);
1072 extent_type = btrfs_file_extent_type(eb, fi);
1074 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1075 num_bytes = btrfs_file_extent_inline_len(eb, fi);
1077 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1078 rec->found_size += num_bytes;
1079 num_bytes = (num_bytes + mask) & ~mask;
1080 } else if (extent_type == BTRFS_FILE_EXTENT_REG ||
1081 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1082 num_bytes = btrfs_file_extent_num_bytes(eb, fi);
1083 disk_bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
1084 extent_offset = btrfs_file_extent_offset(eb, fi);
1085 if (num_bytes == 0 || (num_bytes & mask))
1086 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1087 if (num_bytes + extent_offset >
1088 btrfs_file_extent_ram_bytes(eb, fi))
1089 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1090 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC &&
1091 (btrfs_file_extent_compression(eb, fi) ||
1092 btrfs_file_extent_encryption(eb, fi) ||
1093 btrfs_file_extent_other_encoding(eb, fi)))
1094 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1095 if (disk_bytenr > 0)
1096 rec->found_size += num_bytes;
1098 rec->errors |= I_ERR_BAD_FILE_EXTENT;
1100 rec->extent_end = key->offset + num_bytes;
1102 if (disk_bytenr > 0) {
1104 if (btrfs_file_extent_compression(eb, fi))
1105 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1107 disk_bytenr += extent_offset;
1109 found = count_csum_range(root, disk_bytenr, num_bytes);
1110 if (extent_type == BTRFS_FILE_EXTENT_REG) {
1112 rec->found_csum_item = 1;
1113 if (found < num_bytes)
1114 rec->some_csum_missing = 1;
1115 } else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1117 rec->errors |= I_ERR_ODD_CSUM_ITEM;
1123 static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
1124 struct walk_control *wc)
1126 struct btrfs_key key;
1130 struct cache_tree *inode_cache;
1131 struct shared_node *active_node;
1133 if (wc->root_level == wc->active_node &&
1134 btrfs_root_refs(&root->root_item) == 0)
1137 active_node = wc->nodes[wc->active_node];
1138 inode_cache = &active_node->inode_cache;
1139 nritems = btrfs_header_nritems(eb);
1140 for (i = 0; i < nritems; i++) {
1141 btrfs_item_key_to_cpu(eb, &key, i);
1143 if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1146 if (active_node->current == NULL ||
1147 active_node->current->ino < key.objectid) {
1148 if (active_node->current) {
1149 active_node->current->checked = 1;
1150 maybe_free_inode_rec(inode_cache,
1151 active_node->current);
1153 active_node->current = get_inode_rec(inode_cache,
1157 case BTRFS_DIR_ITEM_KEY:
1158 case BTRFS_DIR_INDEX_KEY:
1159 ret = process_dir_item(root, eb, i, &key, active_node);
1161 case BTRFS_INODE_REF_KEY:
1162 ret = process_inode_ref(eb, i, &key, active_node);
1164 case BTRFS_INODE_EXTREF_KEY:
1165 ret = process_inode_extref(eb, i, &key, active_node);
1167 case BTRFS_INODE_ITEM_KEY:
1168 ret = process_inode_item(eb, i, &key, active_node);
1170 case BTRFS_EXTENT_DATA_KEY:
1171 ret = process_file_extent(root, eb, i, &key,
1181 static void reada_walk_down(struct btrfs_root *root,
1182 struct extent_buffer *node, int slot)
1192 level = btrfs_header_level(node);
1196 nritems = btrfs_header_nritems(node);
1197 blocksize = btrfs_level_size(root, level - 1);
1198 for (i = slot; i < nritems; i++) {
1199 bytenr = btrfs_node_blockptr(node, i);
1200 ptr_gen = btrfs_node_ptr_generation(node, i);
1201 ret = readahead_tree_block(root, bytenr, blocksize, ptr_gen);
1207 static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
1208 struct walk_control *wc, int *level)
1212 struct extent_buffer *next;
1213 struct extent_buffer *cur;
1218 WARN_ON(*level < 0);
1219 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1220 ret = btrfs_lookup_extent_info(NULL, root,
1221 path->nodes[*level]->start,
1222 *level, 1, &refs, NULL);
1227 ret = enter_shared_node(root, path->nodes[*level]->start,
1233 while (*level >= 0) {
1234 WARN_ON(*level < 0);
1235 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1236 cur = path->nodes[*level];
1238 if (btrfs_header_level(cur) != *level)
1241 if (path->slots[*level] >= btrfs_header_nritems(cur))
1244 ret = process_one_leaf(root, cur, wc);
1247 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1248 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
1249 blocksize = btrfs_level_size(root, *level - 1);
1250 ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
1256 ret = enter_shared_node(root, bytenr, refs,
1259 path->slots[*level]++;
1264 next = btrfs_find_tree_block(root, bytenr, blocksize);
1265 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
1266 free_extent_buffer(next);
1267 reada_walk_down(root, cur, path->slots[*level]);
1268 next = read_tree_block(root, bytenr, blocksize,
1272 *level = *level - 1;
1273 free_extent_buffer(path->nodes[*level]);
1274 path->nodes[*level] = next;
1275 path->slots[*level] = 0;
1278 path->slots[*level] = btrfs_header_nritems(path->nodes[*level]);
1282 static int walk_up_tree(struct btrfs_root *root, struct btrfs_path *path,
1283 struct walk_control *wc, int *level)
1286 struct extent_buffer *leaf;
1288 for (i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1289 leaf = path->nodes[i];
1290 if (path->slots[i] + 1 < btrfs_header_nritems(leaf)) {
1295 free_extent_buffer(path->nodes[*level]);
1296 path->nodes[*level] = NULL;
1297 BUG_ON(*level > wc->active_node);
1298 if (*level == wc->active_node)
1299 leave_shared_node(root, wc, *level);
1306 static int check_root_dir(struct inode_record *rec)
1308 struct inode_backref *backref;
1311 if (!rec->found_inode_item || rec->errors)
1313 if (rec->nlink != 1 || rec->found_link != 0)
1315 if (list_empty(&rec->backrefs))
1317 backref = list_entry(rec->backrefs.next, struct inode_backref, list);
1318 if (!backref->found_inode_ref)
1320 if (backref->index != 0 || backref->namelen != 2 ||
1321 memcmp(backref->name, "..", 2))
1323 if (backref->found_dir_index || backref->found_dir_item)
1330 static int check_inode_recs(struct btrfs_root *root,
1331 struct cache_tree *inode_cache)
1333 struct cache_extent *cache;
1334 struct ptr_node *node;
1335 struct inode_record *rec;
1336 struct inode_backref *backref;
1339 u64 root_dirid = btrfs_root_dirid(&root->root_item);
1341 if (btrfs_root_refs(&root->root_item) == 0) {
1342 if (!cache_tree_empty(inode_cache))
1343 fprintf(stderr, "warning line %d\n", __LINE__);
1347 rec = get_inode_rec(inode_cache, root_dirid, 0);
1349 ret = check_root_dir(rec);
1351 fprintf(stderr, "root %llu root dir %llu error\n",
1352 (unsigned long long)root->root_key.objectid,
1353 (unsigned long long)root_dirid);
1357 fprintf(stderr, "root %llu root dir %llu not found\n",
1358 (unsigned long long)root->root_key.objectid,
1359 (unsigned long long)root_dirid);
1363 cache = find_first_cache_extent(inode_cache, 0);
1366 node = container_of(cache, struct ptr_node, cache);
1368 remove_cache_extent(inode_cache, &node->cache);
1370 if (rec->ino == root_dirid ||
1371 rec->ino == BTRFS_ORPHAN_OBJECTID) {
1372 free_inode_rec(rec);
1376 if (rec->errors & I_ERR_NO_ORPHAN_ITEM) {
1377 ret = check_orphan_item(root, rec->ino);
1379 rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1380 if (can_free_inode_rec(rec)) {
1381 free_inode_rec(rec);
1387 if (!rec->found_inode_item)
1388 rec->errors |= I_ERR_NO_INODE_ITEM;
1389 if (rec->found_link != rec->nlink)
1390 rec->errors |= I_ERR_LINK_COUNT_WRONG;
1391 fprintf(stderr, "root %llu inode %llu errors %x\n",
1392 (unsigned long long) root->root_key.objectid,
1393 (unsigned long long) rec->ino, rec->errors);
1394 list_for_each_entry(backref, &rec->backrefs, list) {
1395 if (!backref->found_dir_item)
1396 backref->errors |= REF_ERR_NO_DIR_ITEM;
1397 if (!backref->found_dir_index)
1398 backref->errors |= REF_ERR_NO_DIR_INDEX;
1399 if (!backref->found_inode_ref)
1400 backref->errors |= REF_ERR_NO_INODE_REF;
1401 fprintf(stderr, "\tunresolved ref dir %llu index %llu"
1402 " namelen %u name %s filetype %d error %x\n",
1403 (unsigned long long)backref->dir,
1404 (unsigned long long)backref->index,
1405 backref->namelen, backref->name,
1406 backref->filetype, backref->errors);
1408 free_inode_rec(rec);
1410 return (error > 0) ? -1 : 0;
1413 static struct root_record *get_root_rec(struct cache_tree *root_cache,
1416 struct cache_extent *cache;
1417 struct root_record *rec = NULL;
1420 cache = find_cache_extent(root_cache, objectid, 1);
1422 rec = container_of(cache, struct root_record, cache);
1424 rec = calloc(1, sizeof(*rec));
1425 rec->objectid = objectid;
1426 INIT_LIST_HEAD(&rec->backrefs);
1427 rec->cache.start = objectid;
1428 rec->cache.size = 1;
1430 ret = insert_existing_cache_extent(root_cache, &rec->cache);
1436 static struct root_backref *get_root_backref(struct root_record *rec,
1437 u64 ref_root, u64 dir, u64 index,
1438 const char *name, int namelen)
1440 struct root_backref *backref;
1442 list_for_each_entry(backref, &rec->backrefs, list) {
1443 if (backref->ref_root != ref_root || backref->dir != dir ||
1444 backref->namelen != namelen)
1446 if (memcmp(name, backref->name, namelen))
1451 backref = malloc(sizeof(*backref) + namelen + 1);
1452 memset(backref, 0, sizeof(*backref));
1453 backref->ref_root = ref_root;
1455 backref->index = index;
1456 backref->namelen = namelen;
1457 memcpy(backref->name, name, namelen);
1458 backref->name[namelen] = '\0';
1459 list_add_tail(&backref->list, &rec->backrefs);
1463 static void free_root_recs(struct cache_tree *root_cache)
1465 struct cache_extent *cache;
1466 struct root_record *rec;
1467 struct root_backref *backref;
1470 cache = find_first_cache_extent(root_cache, 0);
1473 rec = container_of(cache, struct root_record, cache);
1474 remove_cache_extent(root_cache, &rec->cache);
1476 while (!list_empty(&rec->backrefs)) {
1477 backref = list_entry(rec->backrefs.next,
1478 struct root_backref, list);
1479 list_del(&backref->list);
1486 static int add_root_backref(struct cache_tree *root_cache,
1487 u64 root_id, u64 ref_root, u64 dir, u64 index,
1488 const char *name, int namelen,
1489 int item_type, int errors)
1491 struct root_record *rec;
1492 struct root_backref *backref;
1494 rec = get_root_rec(root_cache, root_id);
1495 backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
1497 backref->errors |= errors;
1499 if (item_type != BTRFS_DIR_ITEM_KEY) {
1500 if (backref->found_dir_index || backref->found_back_ref ||
1501 backref->found_forward_ref) {
1502 if (backref->index != index)
1503 backref->errors |= REF_ERR_INDEX_UNMATCH;
1505 backref->index = index;
1509 if (item_type == BTRFS_DIR_ITEM_KEY) {
1510 backref->found_dir_item = 1;
1511 backref->reachable = 1;
1513 } else if (item_type == BTRFS_DIR_INDEX_KEY) {
1514 backref->found_dir_index = 1;
1515 } else if (item_type == BTRFS_ROOT_REF_KEY) {
1516 if (backref->found_forward_ref)
1517 backref->errors |= REF_ERR_DUP_ROOT_REF;
1518 backref->found_forward_ref = 1;
1519 } else if (item_type == BTRFS_ROOT_BACKREF_KEY) {
1520 if (backref->found_back_ref)
1521 backref->errors |= REF_ERR_DUP_ROOT_BACKREF;
1522 backref->found_back_ref = 1;
1530 static int merge_root_recs(struct btrfs_root *root,
1531 struct cache_tree *src_cache,
1532 struct cache_tree *dst_cache)
1534 struct cache_extent *cache;
1535 struct ptr_node *node;
1536 struct inode_record *rec;
1537 struct inode_backref *backref;
1539 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1540 free_inode_recs(src_cache);
1545 cache = find_first_cache_extent(src_cache, 0);
1548 node = container_of(cache, struct ptr_node, cache);
1550 remove_cache_extent(src_cache, &node->cache);
1553 if (!is_child_root(root, root->objectid, rec->ino))
1556 list_for_each_entry(backref, &rec->backrefs, list) {
1557 BUG_ON(backref->found_inode_ref);
1558 if (backref->found_dir_item)
1559 add_root_backref(dst_cache, rec->ino,
1560 root->root_key.objectid, backref->dir,
1561 backref->index, backref->name,
1562 backref->namelen, BTRFS_DIR_ITEM_KEY,
1564 if (backref->found_dir_index)
1565 add_root_backref(dst_cache, rec->ino,
1566 root->root_key.objectid, backref->dir,
1567 backref->index, backref->name,
1568 backref->namelen, BTRFS_DIR_INDEX_KEY,
1572 free_inode_rec(rec);
1577 static int check_root_refs(struct btrfs_root *root,
1578 struct cache_tree *root_cache)
1580 struct root_record *rec;
1581 struct root_record *ref_root;
1582 struct root_backref *backref;
1583 struct cache_extent *cache;
1589 rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
1592 /* fixme: this can not detect circular references */
1595 cache = find_first_cache_extent(root_cache, 0);
1599 rec = container_of(cache, struct root_record, cache);
1600 cache = next_cache_extent(cache);
1602 if (rec->found_ref == 0)
1605 list_for_each_entry(backref, &rec->backrefs, list) {
1606 if (!backref->reachable)
1609 ref_root = get_root_rec(root_cache,
1611 if (ref_root->found_ref > 0)
1614 backref->reachable = 0;
1616 if (rec->found_ref == 0)
1622 cache = find_first_cache_extent(root_cache, 0);
1626 rec = container_of(cache, struct root_record, cache);
1627 cache = next_cache_extent(cache);
1629 if (rec->found_ref == 0 &&
1630 rec->objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1631 rec->objectid <= BTRFS_LAST_FREE_OBJECTID) {
1632 ret = check_orphan_item(root->fs_info->tree_root,
1637 fprintf(stderr, "fs tree %llu not referenced\n",
1638 (unsigned long long)rec->objectid);
1642 if (rec->found_ref > 0 && !rec->found_root_item)
1644 list_for_each_entry(backref, &rec->backrefs, list) {
1645 if (!backref->found_dir_item)
1646 backref->errors |= REF_ERR_NO_DIR_ITEM;
1647 if (!backref->found_dir_index)
1648 backref->errors |= REF_ERR_NO_DIR_INDEX;
1649 if (!backref->found_back_ref)
1650 backref->errors |= REF_ERR_NO_ROOT_BACKREF;
1651 if (!backref->found_forward_ref)
1652 backref->errors |= REF_ERR_NO_ROOT_REF;
1653 if (backref->reachable && backref->errors)
1660 fprintf(stderr, "fs tree %llu refs %u %s\n",
1661 (unsigned long long)rec->objectid, rec->found_ref,
1662 rec->found_root_item ? "" : "not found");
1664 list_for_each_entry(backref, &rec->backrefs, list) {
1665 if (!backref->reachable)
1667 if (!backref->errors && rec->found_root_item)
1669 fprintf(stderr, "\tunresolved ref root %llu dir %llu"
1670 " index %llu namelen %u name %s error %x\n",
1671 (unsigned long long)backref->ref_root,
1672 (unsigned long long)backref->dir,
1673 (unsigned long long)backref->index,
1674 backref->namelen, backref->name,
1678 return errors > 0 ? 1 : 0;
1681 static int process_root_ref(struct extent_buffer *eb, int slot,
1682 struct btrfs_key *key,
1683 struct cache_tree *root_cache)
1689 struct btrfs_root_ref *ref;
1690 char namebuf[BTRFS_NAME_LEN];
1693 ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref);
1695 dirid = btrfs_root_ref_dirid(eb, ref);
1696 index = btrfs_root_ref_sequence(eb, ref);
1697 name_len = btrfs_root_ref_name_len(eb, ref);
1699 if (name_len <= BTRFS_NAME_LEN) {
1703 len = BTRFS_NAME_LEN;
1704 error = REF_ERR_NAME_TOO_LONG;
1706 read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len);
1708 if (key->type == BTRFS_ROOT_REF_KEY) {
1709 add_root_backref(root_cache, key->offset, key->objectid, dirid,
1710 index, namebuf, len, key->type, error);
1712 add_root_backref(root_cache, key->objectid, key->offset, dirid,
1713 index, namebuf, len, key->type, error);
1718 static int check_fs_root(struct btrfs_root *root,
1719 struct cache_tree *root_cache,
1720 struct walk_control *wc)
1725 struct btrfs_path path;
1726 struct shared_node root_node;
1727 struct root_record *rec;
1728 struct btrfs_root_item *root_item = &root->root_item;
1730 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1731 rec = get_root_rec(root_cache, root->root_key.objectid);
1732 if (btrfs_root_refs(root_item) > 0)
1733 rec->found_root_item = 1;
1736 btrfs_init_path(&path);
1737 memset(&root_node, 0, sizeof(root_node));
1738 cache_tree_init(&root_node.root_cache);
1739 cache_tree_init(&root_node.inode_cache);
1741 level = btrfs_header_level(root->node);
1742 memset(wc->nodes, 0, sizeof(wc->nodes));
1743 wc->nodes[level] = &root_node;
1744 wc->active_node = level;
1745 wc->root_level = level;
1747 if (btrfs_root_refs(root_item) > 0 ||
1748 btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1749 path.nodes[level] = root->node;
1750 extent_buffer_get(root->node);
1751 path.slots[level] = 0;
1753 struct btrfs_key key;
1754 struct btrfs_disk_key found_key;
1756 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1757 level = root_item->drop_level;
1758 path.lowest_level = level;
1759 wret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
1761 btrfs_node_key(path.nodes[level], &found_key,
1763 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1764 sizeof(found_key)));
1768 wret = walk_down_tree(root, &path, wc, &level);
1774 wret = walk_up_tree(root, &path, wc, &level);
1780 btrfs_release_path(root, &path);
1782 merge_root_recs(root, &root_node.root_cache, root_cache);
1784 if (root_node.current) {
1785 root_node.current->checked = 1;
1786 maybe_free_inode_rec(&root_node.inode_cache,
1790 ret = check_inode_recs(root, &root_node.inode_cache);
1794 static int fs_root_objectid(u64 objectid)
1796 if (objectid == BTRFS_FS_TREE_OBJECTID ||
1797 objectid == BTRFS_TREE_RELOC_OBJECTID ||
1798 objectid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1799 (objectid >= BTRFS_FIRST_FREE_OBJECTID &&
1800 objectid <= BTRFS_LAST_FREE_OBJECTID))
1805 static int check_fs_roots(struct btrfs_root *root,
1806 struct cache_tree *root_cache)
1808 struct btrfs_path path;
1809 struct btrfs_key key;
1810 struct walk_control wc;
1811 struct extent_buffer *leaf;
1812 struct btrfs_root *tmp_root;
1813 struct btrfs_root *tree_root = root->fs_info->tree_root;
1817 memset(&wc, 0, sizeof(wc));
1818 cache_tree_init(&wc.shared);
1819 btrfs_init_path(&path);
1823 key.type = BTRFS_ROOT_ITEM_KEY;
1824 ret = btrfs_search_slot(NULL, tree_root, &key, &path, 0, 0);
1827 leaf = path.nodes[0];
1828 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1829 ret = btrfs_next_leaf(tree_root, &path);
1832 leaf = path.nodes[0];
1834 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1835 if (key.type == BTRFS_ROOT_ITEM_KEY &&
1836 fs_root_objectid(key.objectid)) {
1837 tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
1839 if (IS_ERR(tmp_root)) {
1843 ret = check_fs_root(tmp_root, root_cache, &wc);
1846 btrfs_free_fs_root(root->fs_info, tmp_root);
1847 } else if (key.type == BTRFS_ROOT_REF_KEY ||
1848 key.type == BTRFS_ROOT_BACKREF_KEY) {
1849 process_root_ref(leaf, path.slots[0], &key,
1855 btrfs_release_path(tree_root, &path);
1857 if (!cache_tree_empty(&wc.shared))
1858 fprintf(stderr, "warning line %d\n", __LINE__);
1863 static int all_backpointers_checked(struct extent_record *rec, int print_errs)
1865 struct list_head *cur = rec->backrefs.next;
1866 struct extent_backref *back;
1867 struct tree_backref *tback;
1868 struct data_backref *dback;
1872 while(cur != &rec->backrefs) {
1873 back = list_entry(cur, struct extent_backref, list);
1875 if (!back->found_extent_tree) {
1879 if (back->is_data) {
1880 dback = (struct data_backref *)back;
1881 fprintf(stderr, "Backref %llu %s %llu"
1882 " owner %llu offset %llu num_refs %lu"
1883 " not found in extent tree\n",
1884 (unsigned long long)rec->start,
1885 back->full_backref ?
1887 back->full_backref ?
1888 (unsigned long long)dback->parent:
1889 (unsigned long long)dback->root,
1890 (unsigned long long)dback->owner,
1891 (unsigned long long)dback->offset,
1892 (unsigned long)dback->num_refs);
1894 tback = (struct tree_backref *)back;
1895 fprintf(stderr, "Backref %llu parent %llu"
1896 " root %llu not found in extent tree\n",
1897 (unsigned long long)rec->start,
1898 (unsigned long long)tback->parent,
1899 (unsigned long long)tback->root);
1902 if (!back->is_data && !back->found_ref) {
1906 tback = (struct tree_backref *)back;
1907 fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n",
1908 (unsigned long long)rec->start,
1909 back->full_backref ? "parent" : "root",
1910 back->full_backref ?
1911 (unsigned long long)tback->parent :
1912 (unsigned long long)tback->root, back);
1914 if (back->is_data) {
1915 dback = (struct data_backref *)back;
1916 if (dback->found_ref != dback->num_refs) {
1920 fprintf(stderr, "Incorrect local backref count"
1921 " on %llu %s %llu owner %llu"
1922 " offset %llu found %u wanted %u back %p\n",
1923 (unsigned long long)rec->start,
1924 back->full_backref ?
1926 back->full_backref ?
1927 (unsigned long long)dback->parent:
1928 (unsigned long long)dback->root,
1929 (unsigned long long)dback->owner,
1930 (unsigned long long)dback->offset,
1931 dback->found_ref, dback->num_refs, back);
1933 if (dback->disk_bytenr != rec->start) {
1937 fprintf(stderr, "Backref disk bytenr does not"
1938 " match extent record, bytenr=%llu, "
1939 "ref bytenr=%llu\n",
1940 (unsigned long long)rec->start,
1941 (unsigned long long)dback->disk_bytenr);
1944 if (dback->bytes != rec->nr) {
1948 fprintf(stderr, "Backref bytes do not match "
1949 "extent backref, bytenr=%llu, ref "
1950 "bytes=%llu, backref bytes=%llu\n",
1951 (unsigned long long)rec->start,
1952 (unsigned long long)rec->nr,
1953 (unsigned long long)dback->bytes);
1956 if (!back->is_data) {
1959 dback = (struct data_backref *)back;
1960 found += dback->found_ref;
1963 if (found != rec->refs) {
1967 fprintf(stderr, "Incorrect global backref count "
1968 "on %llu found %llu wanted %llu\n",
1969 (unsigned long long)rec->start,
1970 (unsigned long long)found,
1971 (unsigned long long)rec->refs);
1977 static int free_all_extent_backrefs(struct extent_record *rec)
1979 struct extent_backref *back;
1980 struct list_head *cur;
1981 while (!list_empty(&rec->backrefs)) {
1982 cur = rec->backrefs.next;
1983 back = list_entry(cur, struct extent_backref, list);
1990 static void free_extent_cache(struct btrfs_fs_info *fs_info,
1991 struct cache_tree *extent_cache)
1993 struct cache_extent *cache;
1994 struct extent_record *rec;
1997 cache = find_first_cache_extent(extent_cache, 0);
2000 rec = container_of(cache, struct extent_record, cache);
2001 btrfs_unpin_extent(fs_info, rec->start, rec->max_size);
2002 remove_cache_extent(extent_cache, cache);
2003 free_all_extent_backrefs(rec);
2008 static int maybe_free_extent_rec(struct cache_tree *extent_cache,
2009 struct extent_record *rec)
2011 if (rec->content_checked && rec->owner_ref_checked &&
2012 rec->extent_item_refs == rec->refs && rec->refs > 0 &&
2013 rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
2014 remove_cache_extent(extent_cache, &rec->cache);
2015 free_all_extent_backrefs(rec);
2016 list_del_init(&rec->list);
2022 static int check_owner_ref(struct btrfs_root *root,
2023 struct extent_record *rec,
2024 struct extent_buffer *buf)
2026 struct extent_backref *node;
2027 struct tree_backref *back;
2028 struct btrfs_root *ref_root;
2029 struct btrfs_key key;
2030 struct btrfs_path path;
2031 struct extent_buffer *parent;
2036 list_for_each_entry(node, &rec->backrefs, list) {
2039 if (!node->found_ref)
2041 if (node->full_backref)
2043 back = (struct tree_backref *)node;
2044 if (btrfs_header_owner(buf) == back->root)
2047 BUG_ON(rec->is_root);
2049 /* try to find the block by search corresponding fs tree */
2050 key.objectid = btrfs_header_owner(buf);
2051 key.type = BTRFS_ROOT_ITEM_KEY;
2052 key.offset = (u64)-1;
2054 ref_root = btrfs_read_fs_root(root->fs_info, &key);
2055 if (IS_ERR(ref_root))
2058 level = btrfs_header_level(buf);
2060 btrfs_item_key_to_cpu(buf, &key, 0);
2062 btrfs_node_key_to_cpu(buf, &key, 0);
2064 btrfs_init_path(&path);
2065 path.lowest_level = level + 1;
2066 ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0);
2070 parent = path.nodes[level + 1];
2071 if (parent && buf->start == btrfs_node_blockptr(parent,
2072 path.slots[level + 1]))
2075 btrfs_release_path(ref_root, &path);
2076 return found ? 0 : 1;
2079 static int is_extent_tree_record(struct extent_record *rec)
2081 struct list_head *cur = rec->backrefs.next;
2082 struct extent_backref *node;
2083 struct tree_backref *back;
2086 while(cur != &rec->backrefs) {
2087 node = list_entry(cur, struct extent_backref, list);
2091 back = (struct tree_backref *)node;
2092 if (node->full_backref)
2094 if (back->root == BTRFS_EXTENT_TREE_OBJECTID)
2101 static int record_bad_block_io(struct btrfs_fs_info *info,
2102 struct cache_tree *extent_cache,
2105 struct extent_record *rec;
2106 struct cache_extent *cache;
2107 struct btrfs_key key;
2109 cache = find_cache_extent(extent_cache, start, len);
2113 rec = container_of(cache, struct extent_record, cache);
2114 if (!is_extent_tree_record(rec))
2117 btrfs_disk_key_to_cpu(&key, &rec->parent_key);
2118 return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2121 static int check_block(struct btrfs_root *root,
2122 struct cache_tree *extent_cache,
2123 struct extent_buffer *buf, u64 flags)
2125 struct extent_record *rec;
2126 struct cache_extent *cache;
2127 struct btrfs_key key;
2131 cache = find_cache_extent(extent_cache, buf->start, buf->len);
2134 rec = container_of(cache, struct extent_record, cache);
2135 rec->generation = btrfs_header_generation(buf);
2137 level = btrfs_header_level(buf);
2138 if (btrfs_header_nritems(buf) > 0) {
2141 btrfs_item_key_to_cpu(buf, &key, 0);
2143 btrfs_node_key_to_cpu(buf, &key, 0);
2145 rec->info_objectid = key.objectid;
2147 rec->info_level = level;
2149 if (btrfs_is_leaf(buf))
2150 ret = btrfs_check_leaf(root, &rec->parent_key, buf);
2152 ret = btrfs_check_node(root, &rec->parent_key, buf);
2155 fprintf(stderr, "bad block %llu\n",
2156 (unsigned long long)buf->start);
2158 rec->content_checked = 1;
2159 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2160 rec->owner_ref_checked = 1;
2162 ret = check_owner_ref(root, rec, buf);
2164 rec->owner_ref_checked = 1;
2168 maybe_free_extent_rec(extent_cache, rec);
2172 static struct tree_backref *find_tree_backref(struct extent_record *rec,
2173 u64 parent, u64 root)
2175 struct list_head *cur = rec->backrefs.next;
2176 struct extent_backref *node;
2177 struct tree_backref *back;
2179 while(cur != &rec->backrefs) {
2180 node = list_entry(cur, struct extent_backref, list);
2184 back = (struct tree_backref *)node;
2186 if (!node->full_backref)
2188 if (parent == back->parent)
2191 if (node->full_backref)
2193 if (back->root == root)
2200 static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
2201 u64 parent, u64 root)
2203 struct tree_backref *ref = malloc(sizeof(*ref));
2204 memset(&ref->node, 0, sizeof(ref->node));
2206 ref->parent = parent;
2207 ref->node.full_backref = 1;
2210 ref->node.full_backref = 0;
2212 list_add_tail(&ref->node.list, &rec->backrefs);
2217 static struct data_backref *find_data_backref(struct extent_record *rec,
2218 u64 parent, u64 root,
2219 u64 owner, u64 offset,
2221 u64 disk_bytenr, u64 bytes)
2223 struct list_head *cur = rec->backrefs.next;
2224 struct extent_backref *node;
2225 struct data_backref *back;
2227 while(cur != &rec->backrefs) {
2228 node = list_entry(cur, struct extent_backref, list);
2232 back = (struct data_backref *)node;
2234 if (!node->full_backref)
2236 if (parent == back->parent)
2239 if (node->full_backref)
2241 if (back->root == root && back->owner == owner &&
2242 back->offset == offset) {
2243 if (found_ref && node->found_ref &&
2244 (back->bytes != bytes ||
2245 back->disk_bytenr != disk_bytenr))
2254 static struct data_backref *alloc_data_backref(struct extent_record *rec,
2255 u64 parent, u64 root,
2256 u64 owner, u64 offset,
2259 struct data_backref *ref = malloc(sizeof(*ref));
2260 memset(&ref->node, 0, sizeof(ref->node));
2261 ref->node.is_data = 1;
2264 ref->parent = parent;
2267 ref->node.full_backref = 1;
2271 ref->offset = offset;
2272 ref->node.full_backref = 0;
2274 ref->bytes = max_size;
2277 list_add_tail(&ref->node.list, &rec->backrefs);
2278 if (max_size > rec->max_size)
2279 rec->max_size = max_size;
2283 static int add_extent_rec(struct cache_tree *extent_cache,
2284 struct btrfs_key *parent_key,
2285 u64 start, u64 nr, u64 extent_item_refs,
2286 int is_root, int inc_ref, int set_checked,
2287 int metadata, int extent_rec, u64 max_size)
2289 struct extent_record *rec;
2290 struct cache_extent *cache;
2294 cache = find_cache_extent(extent_cache, start, nr);
2296 rec = container_of(cache, struct extent_record, cache);
2300 rec->nr = max(nr, max_size);
2303 * We need to make sure to reset nr to whatever the extent
2304 * record says was the real size, this way we can compare it to
2308 if (start != rec->start || rec->found_rec) {
2309 struct extent_record *tmp;
2312 if (list_empty(&rec->list))
2313 list_add_tail(&rec->list,
2314 &duplicate_extents);
2317 * We have to do this song and dance in case we
2318 * find an extent record that falls inside of
2319 * our current extent record but does not have
2320 * the same objectid.
2322 tmp = malloc(sizeof(*tmp));
2326 tmp->max_size = max_size;
2329 tmp->metadata = metadata;
2330 tmp->extent_item_refs = extent_item_refs;
2331 INIT_LIST_HEAD(&tmp->list);
2332 list_add_tail(&tmp->list, &rec->dups);
2333 rec->num_duplicates++;
2340 if (extent_item_refs && !dup) {
2341 if (rec->extent_item_refs) {
2342 fprintf(stderr, "block %llu rec "
2343 "extent_item_refs %llu, passed %llu\n",
2344 (unsigned long long)start,
2345 (unsigned long long)
2346 rec->extent_item_refs,
2347 (unsigned long long)extent_item_refs);
2349 rec->extent_item_refs = extent_item_refs;
2354 rec->content_checked = 1;
2355 rec->owner_ref_checked = 1;
2359 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2361 if (rec->max_size < max_size)
2362 rec->max_size = max_size;
2364 maybe_free_extent_rec(extent_cache, rec);
2367 rec = malloc(sizeof(*rec));
2369 rec->max_size = max_size;
2370 rec->nr = max(nr, max_size);
2371 rec->found_rec = extent_rec;
2372 rec->content_checked = 0;
2373 rec->owner_ref_checked = 0;
2374 rec->num_duplicates = 0;
2375 rec->metadata = metadata;
2376 INIT_LIST_HEAD(&rec->backrefs);
2377 INIT_LIST_HEAD(&rec->dups);
2378 INIT_LIST_HEAD(&rec->list);
2390 if (extent_item_refs)
2391 rec->extent_item_refs = extent_item_refs;
2393 rec->extent_item_refs = 0;
2396 btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
2398 memset(&rec->parent_key, 0, sizeof(*parent_key));
2400 rec->cache.start = start;
2401 rec->cache.size = nr;
2402 ret = insert_existing_cache_extent(extent_cache, &rec->cache);
2406 rec->content_checked = 1;
2407 rec->owner_ref_checked = 1;
2412 static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
2413 u64 parent, u64 root, int found_ref)
2415 struct extent_record *rec;
2416 struct tree_backref *back;
2417 struct cache_extent *cache;
2419 cache = find_cache_extent(extent_cache, bytenr, 1);
2421 add_extent_rec(extent_cache, NULL, bytenr,
2422 1, 0, 0, 0, 0, 1, 0, 0);
2423 cache = find_cache_extent(extent_cache, bytenr, 1);
2428 rec = container_of(cache, struct extent_record, cache);
2429 if (rec->start != bytenr) {
2433 back = find_tree_backref(rec, parent, root);
2435 back = alloc_tree_backref(rec, parent, root);
2438 if (back->node.found_ref) {
2439 fprintf(stderr, "Extent back ref already exists "
2440 "for %llu parent %llu root %llu \n",
2441 (unsigned long long)bytenr,
2442 (unsigned long long)parent,
2443 (unsigned long long)root);
2445 back->node.found_ref = 1;
2447 if (back->node.found_extent_tree) {
2448 fprintf(stderr, "Extent back ref already exists "
2449 "for %llu parent %llu root %llu \n",
2450 (unsigned long long)bytenr,
2451 (unsigned long long)parent,
2452 (unsigned long long)root);
2454 back->node.found_extent_tree = 1;
2459 static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
2460 u64 parent, u64 root, u64 owner, u64 offset,
2461 u32 num_refs, int found_ref, u64 max_size)
2463 struct extent_record *rec;
2464 struct data_backref *back;
2465 struct cache_extent *cache;
2467 cache = find_cache_extent(extent_cache, bytenr, 1);
2469 add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
2471 cache = find_cache_extent(extent_cache, bytenr, 1);
2476 rec = container_of(cache, struct extent_record, cache);
2477 if (rec->max_size < max_size)
2478 rec->max_size = max_size;
2481 * If found_ref is set then max_size is the real size and must match the
2482 * existing refs. So if we have already found a ref then we need to
2483 * make sure that this ref matches the existing one, otherwise we need
2484 * to add a new backref so we can notice that the backrefs don't match
2485 * and we need to figure out who is telling the truth. This is to
2486 * account for that awful fsync bug I introduced where we'd end up with
2487 * a btrfs_file_extent_item that would have its length include multiple
2488 * prealloc extents or point inside of a prealloc extent.
2490 back = find_data_backref(rec, parent, root, owner, offset, found_ref,
2493 back = alloc_data_backref(rec, parent, root, owner, offset,
2497 BUG_ON(num_refs != 1);
2498 if (back->node.found_ref)
2499 BUG_ON(back->bytes != max_size);
2500 back->node.found_ref = 1;
2501 back->found_ref += 1;
2502 back->bytes = max_size;
2503 back->disk_bytenr = bytenr;
2505 rec->content_checked = 1;
2506 rec->owner_ref_checked = 1;
2508 if (back->node.found_extent_tree) {
2509 fprintf(stderr, "Extent back ref already exists "
2510 "for %llu parent %llu root %llu"
2511 "owner %llu offset %llu num_refs %lu\n",
2512 (unsigned long long)bytenr,
2513 (unsigned long long)parent,
2514 (unsigned long long)root,
2515 (unsigned long long)owner,
2516 (unsigned long long)offset,
2517 (unsigned long)num_refs);
2519 back->num_refs = num_refs;
2520 back->node.found_extent_tree = 1;
2525 static int add_pending(struct cache_tree *pending,
2526 struct cache_tree *seen, u64 bytenr, u32 size)
2529 ret = insert_cache_extent(seen, bytenr, size);
2532 insert_cache_extent(pending, bytenr, size);
2536 static int pick_next_pending(struct cache_tree *pending,
2537 struct cache_tree *reada,
2538 struct cache_tree *nodes,
2539 u64 last, struct block_info *bits, int bits_nr,
2542 unsigned long node_start = last;
2543 struct cache_extent *cache;
2546 cache = find_first_cache_extent(reada, 0);
2548 bits[0].start = cache->start;
2549 bits[1].size = cache->size;
2554 if (node_start > 32768)
2555 node_start -= 32768;
2557 cache = find_first_cache_extent(nodes, node_start);
2559 cache = find_first_cache_extent(nodes, 0);
2562 cache = find_first_cache_extent(pending, 0);
2567 bits[ret].start = cache->start;
2568 bits[ret].size = cache->size;
2569 cache = next_cache_extent(cache);
2571 } while (cache && ret < bits_nr);
2577 bits[ret].start = cache->start;
2578 bits[ret].size = cache->size;
2579 cache = next_cache_extent(cache);
2581 } while (cache && ret < bits_nr);
2583 if (bits_nr - ret > 8) {
2584 u64 lookup = bits[0].start + bits[0].size;
2585 struct cache_extent *next;
2586 next = find_first_cache_extent(pending, lookup);
2588 if (next->start - lookup > 32768)
2590 bits[ret].start = next->start;
2591 bits[ret].size = next->size;
2592 lookup = next->start + next->size;
2596 next = next_cache_extent(next);
2604 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2605 static int process_extent_ref_v0(struct cache_tree *extent_cache,
2606 struct extent_buffer *leaf, int slot)
2608 struct btrfs_extent_ref_v0 *ref0;
2609 struct btrfs_key key;
2611 btrfs_item_key_to_cpu(leaf, &key, slot);
2612 ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0);
2613 if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) {
2614 add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0);
2616 add_data_backref(extent_cache, key.objectid, key.offset, 0,
2617 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0);
2623 static int process_extent_item(struct btrfs_root *root,
2624 struct cache_tree *extent_cache,
2625 struct extent_buffer *eb, int slot)
2627 struct btrfs_extent_item *ei;
2628 struct btrfs_extent_inline_ref *iref;
2629 struct btrfs_extent_data_ref *dref;
2630 struct btrfs_shared_data_ref *sref;
2631 struct btrfs_key key;
2635 u32 item_size = btrfs_item_size_nr(eb, slot);
2641 btrfs_item_key_to_cpu(eb, &key, slot);
2643 if (key.type == BTRFS_METADATA_ITEM_KEY) {
2645 num_bytes = root->leafsize;
2647 num_bytes = key.offset;
2650 if (item_size < sizeof(*ei)) {
2651 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2652 struct btrfs_extent_item_v0 *ei0;
2653 BUG_ON(item_size != sizeof(*ei0));
2654 ei0 = btrfs_item_ptr(eb, slot, struct btrfs_extent_item_v0);
2655 refs = btrfs_extent_refs_v0(eb, ei0);
2659 return add_extent_rec(extent_cache, NULL, key.objectid,
2660 num_bytes, refs, 0, 0, 0, metadata, 1,
2664 ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
2665 refs = btrfs_extent_refs(eb, ei);
2667 add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
2668 refs, 0, 0, 0, metadata, 1, num_bytes);
2670 ptr = (unsigned long)(ei + 1);
2671 if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
2672 key.type == BTRFS_EXTENT_ITEM_KEY)
2673 ptr += sizeof(struct btrfs_tree_block_info);
2675 end = (unsigned long)ei + item_size;
2677 iref = (struct btrfs_extent_inline_ref *)ptr;
2678 type = btrfs_extent_inline_ref_type(eb, iref);
2679 offset = btrfs_extent_inline_ref_offset(eb, iref);
2681 case BTRFS_TREE_BLOCK_REF_KEY:
2682 add_tree_backref(extent_cache, key.objectid,
2685 case BTRFS_SHARED_BLOCK_REF_KEY:
2686 add_tree_backref(extent_cache, key.objectid,
2689 case BTRFS_EXTENT_DATA_REF_KEY:
2690 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2691 add_data_backref(extent_cache, key.objectid, 0,
2692 btrfs_extent_data_ref_root(eb, dref),
2693 btrfs_extent_data_ref_objectid(eb,
2695 btrfs_extent_data_ref_offset(eb, dref),
2696 btrfs_extent_data_ref_count(eb, dref),
2699 case BTRFS_SHARED_DATA_REF_KEY:
2700 sref = (struct btrfs_shared_data_ref *)(iref + 1);
2701 add_data_backref(extent_cache, key.objectid, offset,
2703 btrfs_shared_data_ref_count(eb, sref),
2707 fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n",
2708 key.objectid, key.type, num_bytes);
2711 ptr += btrfs_extent_inline_ref_size(type);
2718 static int check_cache_range(struct btrfs_root *root,
2719 struct btrfs_block_group_cache *cache,
2720 u64 offset, u64 bytes)
2722 struct btrfs_free_space *entry;
2728 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2729 bytenr = btrfs_sb_offset(i);
2730 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
2731 cache->key.objectid, bytenr, 0,
2732 &logical, &nr, &stripe_len);
2737 if (logical[nr] + stripe_len <= offset)
2739 if (offset + bytes <= logical[nr])
2741 if (logical[nr] == offset) {
2742 if (stripe_len >= bytes) {
2746 bytes -= stripe_len;
2747 offset += stripe_len;
2748 } else if (logical[nr] < offset) {
2749 if (logical[nr] + stripe_len >=
2754 bytes = (offset + bytes) -
2755 (logical[nr] + stripe_len);
2756 offset = logical[nr] + stripe_len;
2759 * Could be tricky, the super may land in the
2760 * middle of the area we're checking. First
2761 * check the easiest case, it's at the end.
2763 if (logical[nr] + stripe_len >=
2765 bytes = logical[nr] - offset;
2769 /* Check the left side */
2770 ret = check_cache_range(root, cache,
2772 logical[nr] - offset);
2778 /* Now we continue with the right side */
2779 bytes = (offset + bytes) -
2780 (logical[nr] + stripe_len);
2781 offset = logical[nr] + stripe_len;
2788 entry = btrfs_find_free_space(cache->free_space_ctl, offset, bytes);
2790 fprintf(stderr, "There is no free space entry for %Lu-%Lu\n",
2791 offset, offset+bytes);
2795 if (entry->offset != offset) {
2796 fprintf(stderr, "Wanted offset %Lu, found %Lu\n", offset,
2801 if (entry->bytes != bytes) {
2802 fprintf(stderr, "Wanted bytes %Lu, found %Lu for off %Lu\n",
2803 bytes, entry->bytes, offset);
2807 unlink_free_space(cache->free_space_ctl, entry);
2812 static int verify_space_cache(struct btrfs_root *root,
2813 struct btrfs_block_group_cache *cache)
2815 struct btrfs_path *path;
2816 struct extent_buffer *leaf;
2817 struct btrfs_key key;
2821 path = btrfs_alloc_path();
2825 root = root->fs_info->extent_root;
2827 last = max_t(u64, cache->key.objectid, BTRFS_SUPER_INFO_OFFSET);
2829 key.objectid = last;
2831 key.type = BTRFS_EXTENT_ITEM_KEY;
2833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2838 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2839 ret = btrfs_next_leaf(root, path);
2847 leaf = path->nodes[0];
2848 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2849 if (key.objectid >= cache->key.offset + cache->key.objectid)
2851 if (key.type != BTRFS_EXTENT_ITEM_KEY &&
2852 key.type != BTRFS_METADATA_ITEM_KEY) {
2857 if (last == key.objectid) {
2858 if (key.type == BTRFS_EXTENT_ITEM_KEY)
2859 last = key.objectid + key.offset;
2861 last = key.objectid + root->leafsize;
2866 ret = check_cache_range(root, cache, last,
2867 key.objectid - last);
2870 if (key.type == BTRFS_EXTENT_ITEM_KEY)
2871 last = key.objectid + key.offset;
2873 last = key.objectid + root->leafsize;
2877 if (last < cache->key.objectid + cache->key.offset)
2878 ret = check_cache_range(root, cache, last,
2879 cache->key.objectid +
2880 cache->key.offset - last);
2881 btrfs_free_path(path);
2884 !RB_EMPTY_ROOT(&cache->free_space_ctl->free_space_offset)) {
2885 fprintf(stderr, "There are still entries left in the space "
2893 static int check_space_cache(struct btrfs_root *root)
2895 struct btrfs_block_group_cache *cache;
2896 u64 start = BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE;
2900 if (btrfs_super_generation(root->fs_info->super_copy) !=
2901 btrfs_super_cache_generation(root->fs_info->super_copy)) {
2902 printf("cache and super generation don't match, space cache "
2903 "will be invalidated\n");
2908 cache = btrfs_lookup_first_block_group(root->fs_info, start);
2912 start = cache->key.objectid + cache->key.offset;
2913 if (!cache->free_space_ctl) {
2914 if (btrfs_init_free_space_ctl(cache,
2915 root->sectorsize)) {
2920 btrfs_remove_free_space_cache(cache);
2923 ret = load_free_space_cache(root->fs_info, cache);
2927 ret = verify_space_cache(root, cache);
2929 fprintf(stderr, "cache appears valid but isnt %Lu\n",
2930 cache->key.objectid);
2935 return error ? -EINVAL : 0;
2938 static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
2941 struct btrfs_path *path;
2942 struct extent_buffer *leaf;
2943 struct btrfs_key key;
2946 path = btrfs_alloc_path();
2948 fprintf(stderr, "Error allocing path\n");
2952 key.objectid = bytenr;
2953 key.type = BTRFS_EXTENT_ITEM_KEY;
2958 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
2961 fprintf(stderr, "Error looking up extent record %d\n", ret);
2962 btrfs_free_path(path);
2968 btrfs_prev_leaf(root, path);
2971 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
2974 * Block group items come before extent items if they have the same
2975 * bytenr, so walk back one more just in case. Dear future traveler,
2976 * first congrats on mastering time travel. Now if it's not too much
2977 * trouble could you go back to 2006 and tell Chris to make the
2978 * BLOCK_GROUP_ITEM_KEY lower than the EXTENT_ITEM_KEY please?
2980 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
2984 btrfs_prev_leaf(root, path);
2988 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
2989 ret = btrfs_next_leaf(root, path);
2991 fprintf(stderr, "Error going to next leaf "
2993 btrfs_free_path(path);
2999 leaf = path->nodes[0];
3000 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3001 if (key.type != BTRFS_EXTENT_ITEM_KEY) {
3005 if (key.objectid + key.offset < bytenr) {
3009 if (key.objectid > bytenr + num_bytes)
3012 if (key.objectid == bytenr) {
3013 if (key.offset >= num_bytes) {
3017 num_bytes -= key.offset;
3018 bytenr += key.offset;
3019 } else if (key.objectid < bytenr) {
3020 if (key.objectid + key.offset >= bytenr + num_bytes) {
3024 num_bytes = (bytenr + num_bytes) -
3025 (key.objectid + key.offset);
3026 bytenr = key.objectid + key.offset;
3028 if (key.objectid + key.offset < bytenr + num_bytes) {
3029 u64 new_start = key.objectid + key.offset;
3030 u64 new_bytes = bytenr + num_bytes - new_start;
3033 * Weird case, the extent is in the middle of
3034 * our range, we'll have to search one side
3035 * and then the other. Not sure if this happens
3036 * in real life, but no harm in coding it up
3037 * anyway just in case.
3039 btrfs_release_path(root, path);
3040 ret = check_extent_exists(root, new_start,
3043 fprintf(stderr, "Right section didn't "
3047 num_bytes = key.objectid - bytenr;
3050 num_bytes = key.objectid - bytenr;
3057 fprintf(stderr, "There are no extents for csum range "
3058 "%Lu-%Lu\n", bytenr, bytenr+num_bytes);
3062 btrfs_free_path(path);
3066 static int check_csums(struct btrfs_root *root)
3068 struct btrfs_path *path;
3069 struct extent_buffer *leaf;
3070 struct btrfs_key key;
3071 u64 offset = 0, num_bytes = 0;
3072 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
3076 root = root->fs_info->csum_root;
3078 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
3079 key.type = BTRFS_EXTENT_CSUM_KEY;
3082 path = btrfs_alloc_path();
3086 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3088 fprintf(stderr, "Error searching csum tree %d\n", ret);
3089 btrfs_free_path(path);
3093 if (ret > 0 && path->slots[0])
3098 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3099 ret = btrfs_next_leaf(root, path);
3101 fprintf(stderr, "Error going to next leaf "
3108 leaf = path->nodes[0];
3110 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3111 if (key.type != BTRFS_EXTENT_CSUM_KEY) {
3117 offset = key.offset;
3118 } else if (key.offset != offset + num_bytes) {
3119 ret = check_extent_exists(root, offset, num_bytes);
3121 fprintf(stderr, "Csum exists for %Lu-%Lu but "
3122 "there is no extent record\n",
3123 offset, offset+num_bytes);
3126 offset = key.offset;
3130 num_bytes += (btrfs_item_size_nr(leaf, path->slots[0]) /
3131 csum_size) * root->sectorsize;
3135 btrfs_free_path(path);
3139 static int run_next_block(struct btrfs_root *root,
3140 struct block_info *bits,
3143 struct cache_tree *pending,
3144 struct cache_tree *seen,
3145 struct cache_tree *reada,
3146 struct cache_tree *nodes,
3147 struct cache_tree *extent_cache)
3149 struct extent_buffer *buf;
3158 struct btrfs_key key;
3159 struct cache_extent *cache;
3162 ret = pick_next_pending(pending, reada, nodes, *last, bits,
3163 bits_nr, &reada_bits);
3168 for(i = 0; i < ret; i++) {
3169 insert_cache_extent(reada, bits[i].start,
3172 /* fixme, get the parent transid */
3173 readahead_tree_block(root, bits[i].start,
3177 *last = bits[0].start;
3178 bytenr = bits[0].start;
3179 size = bits[0].size;
3181 cache = find_cache_extent(pending, bytenr, size);
3183 remove_cache_extent(pending, cache);
3186 cache = find_cache_extent(reada, bytenr, size);
3188 remove_cache_extent(reada, cache);
3191 cache = find_cache_extent(nodes, bytenr, size);
3193 remove_cache_extent(nodes, cache);
3197 /* fixme, get the real parent transid */
3198 buf = read_tree_block(root, bytenr, size, 0);
3199 if (!extent_buffer_uptodate(buf)) {
3200 record_bad_block_io(root->fs_info,
3201 extent_cache, bytenr, size);
3205 nritems = btrfs_header_nritems(buf);
3207 ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3208 btrfs_header_level(buf), 1, NULL,
3211 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
3213 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3218 owner = btrfs_header_owner(buf);
3221 ret = check_block(root, extent_cache, buf, flags);
3225 if (btrfs_is_leaf(buf)) {
3226 btree_space_waste += btrfs_leaf_free_space(root, buf);
3227 for (i = 0; i < nritems; i++) {
3228 struct btrfs_file_extent_item *fi;
3229 btrfs_item_key_to_cpu(buf, &key, i);
3230 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3231 process_extent_item(root, extent_cache, buf,
3235 if (key.type == BTRFS_METADATA_ITEM_KEY) {
3236 process_extent_item(root, extent_cache, buf,
3240 if (key.type == BTRFS_EXTENT_CSUM_KEY) {
3242 btrfs_item_size_nr(buf, i);
3245 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3248 if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
3249 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
3250 process_extent_ref_v0(extent_cache, buf, i);
3257 if (key.type == BTRFS_TREE_BLOCK_REF_KEY) {
3258 add_tree_backref(extent_cache, key.objectid, 0,
3262 if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) {
3263 add_tree_backref(extent_cache, key.objectid,
3267 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
3268 struct btrfs_extent_data_ref *ref;
3269 ref = btrfs_item_ptr(buf, i,
3270 struct btrfs_extent_data_ref);
3271 add_data_backref(extent_cache,
3273 btrfs_extent_data_ref_root(buf, ref),
3274 btrfs_extent_data_ref_objectid(buf,
3276 btrfs_extent_data_ref_offset(buf, ref),
3277 btrfs_extent_data_ref_count(buf, ref),
3278 0, root->sectorsize);
3281 if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
3282 struct btrfs_shared_data_ref *ref;
3283 ref = btrfs_item_ptr(buf, i,
3284 struct btrfs_shared_data_ref);
3285 add_data_backref(extent_cache,
3286 key.objectid, key.offset, 0, 0, 0,
3287 btrfs_shared_data_ref_count(buf, ref),
3288 0, root->sectorsize);
3291 if (key.type != BTRFS_EXTENT_DATA_KEY)
3293 fi = btrfs_item_ptr(buf, i,
3294 struct btrfs_file_extent_item);
3295 if (btrfs_file_extent_type(buf, fi) ==
3296 BTRFS_FILE_EXTENT_INLINE)
3298 if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
3301 data_bytes_allocated +=
3302 btrfs_file_extent_disk_num_bytes(buf, fi);
3303 if (data_bytes_allocated < root->sectorsize) {
3306 data_bytes_referenced +=
3307 btrfs_file_extent_num_bytes(buf, fi);
3308 add_data_backref(extent_cache,
3309 btrfs_file_extent_disk_bytenr(buf, fi),
3310 parent, owner, key.objectid, key.offset -
3311 btrfs_file_extent_offset(buf, fi), 1, 1,
3312 btrfs_file_extent_disk_num_bytes(buf, fi));
3317 struct btrfs_key first_key;
3319 first_key.objectid = 0;
3322 btrfs_item_key_to_cpu(buf, &first_key, 0);
3323 level = btrfs_header_level(buf);
3324 for (i = 0; i < nritems; i++) {
3325 u64 ptr = btrfs_node_blockptr(buf, i);
3326 u32 size = btrfs_level_size(root, level - 1);
3327 btrfs_node_key_to_cpu(buf, &key, i);
3328 ret = add_extent_rec(extent_cache, &key,
3329 ptr, size, 0, 0, 1, 0, 1, 0,
3333 add_tree_backref(extent_cache, ptr, parent, owner, 1);
3336 add_pending(nodes, seen, ptr, size);
3338 add_pending(pending, seen, ptr, size);
3341 btree_space_waste += (BTRFS_NODEPTRS_PER_BLOCK(root) -
3342 nritems) * sizeof(struct btrfs_key_ptr);
3344 total_btree_bytes += buf->len;
3345 if (fs_root_objectid(btrfs_header_owner(buf)))
3346 total_fs_tree_bytes += buf->len;
3347 if (btrfs_header_owner(buf) == BTRFS_EXTENT_TREE_OBJECTID)
3348 total_extent_tree_bytes += buf->len;
3349 if (!found_old_backref &&
3350 btrfs_header_owner(buf) == BTRFS_TREE_RELOC_OBJECTID &&
3351 btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV &&
3352 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
3353 found_old_backref = 1;
3355 free_extent_buffer(buf);
3359 static int add_root_to_pending(struct extent_buffer *buf,
3360 struct cache_tree *extent_cache,
3361 struct cache_tree *pending,
3362 struct cache_tree *seen,
3363 struct cache_tree *nodes,
3364 struct btrfs_key *root_key)
3366 if (btrfs_header_level(buf) > 0)
3367 add_pending(nodes, seen, buf->start, buf->len);
3369 add_pending(pending, seen, buf->start, buf->len);
3370 add_extent_rec(extent_cache, NULL, buf->start, buf->len,
3371 0, 1, 1, 0, 1, 0, buf->len);
3373 if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
3374 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
3375 add_tree_backref(extent_cache, buf->start, buf->start,
3378 add_tree_backref(extent_cache, buf->start, 0,
3379 root_key->objectid, 1);
3383 /* as we fix the tree, we might be deleting blocks that
3384 * we're tracking for repair. This hook makes sure we
3385 * remove any backrefs for blocks as we are fixing them.
3387 static int free_extent_hook(struct btrfs_trans_handle *trans,
3388 struct btrfs_root *root,
3389 u64 bytenr, u64 num_bytes, u64 parent,
3390 u64 root_objectid, u64 owner, u64 offset,
3393 struct extent_record *rec;
3394 struct cache_extent *cache;
3396 struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache;
3398 is_data = owner >= BTRFS_FIRST_FREE_OBJECTID;
3399 cache = find_cache_extent(extent_cache, bytenr, num_bytes);
3403 rec = container_of(cache, struct extent_record, cache);
3405 struct data_backref *back;
3406 back = find_data_backref(rec, parent, root_objectid, owner,
3407 offset, 1, bytenr, num_bytes);
3410 if (back->node.found_ref) {
3411 back->found_ref -= refs_to_drop;
3413 rec->refs -= refs_to_drop;
3415 if (back->node.found_extent_tree) {
3416 back->num_refs -= refs_to_drop;
3417 if (rec->extent_item_refs)
3418 rec->extent_item_refs -= refs_to_drop;
3420 if (back->found_ref == 0)
3421 back->node.found_ref = 0;
3422 if (back->num_refs == 0)
3423 back->node.found_extent_tree = 0;
3425 if (!back->node.found_extent_tree && back->node.found_ref) {
3426 list_del(&back->node.list);
3430 struct tree_backref *back;
3431 back = find_tree_backref(rec, parent, root_objectid);
3434 if (back->node.found_ref) {
3437 back->node.found_ref = 0;
3439 if (back->node.found_extent_tree) {
3440 if (rec->extent_item_refs)
3441 rec->extent_item_refs--;
3442 back->node.found_extent_tree = 0;
3444 if (!back->node.found_extent_tree && back->node.found_ref) {
3445 list_del(&back->node.list);
3449 maybe_free_extent_rec(extent_cache, rec);
3454 static int delete_extent_records(struct btrfs_trans_handle *trans,
3455 struct btrfs_root *root,
3456 struct btrfs_path *path,
3457 u64 bytenr, u64 new_len)
3459 struct btrfs_key key;
3460 struct btrfs_key found_key;
3461 struct extent_buffer *leaf;
3466 key.objectid = bytenr;
3468 key.offset = (u64)-1;
3471 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3478 if (path->slots[0] == 0)
3484 leaf = path->nodes[0];
3485 slot = path->slots[0];
3487 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3488 if (found_key.objectid != bytenr)
3491 if (found_key.type != BTRFS_EXTENT_ITEM_KEY &&
3492 found_key.type != BTRFS_METADATA_ITEM_KEY &&
3493 found_key.type != BTRFS_TREE_BLOCK_REF_KEY &&
3494 found_key.type != BTRFS_EXTENT_DATA_REF_KEY &&
3495 found_key.type != BTRFS_EXTENT_REF_V0_KEY &&
3496 found_key.type != BTRFS_SHARED_BLOCK_REF_KEY &&
3497 found_key.type != BTRFS_SHARED_DATA_REF_KEY) {
3498 btrfs_release_path(NULL, path);
3499 if (found_key.type == 0) {
3500 if (found_key.offset == 0)
3502 key.offset = found_key.offset - 1;
3503 key.type = found_key.type;
3505 key.type = found_key.type - 1;
3506 key.offset = (u64)-1;
3510 fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n",
3511 found_key.objectid, found_key.type, found_key.offset);
3513 ret = btrfs_del_item(trans, root->fs_info->extent_root, path);
3516 btrfs_release_path(NULL, path);
3518 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
3519 found_key.type == BTRFS_METADATA_ITEM_KEY) {
3520 u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
3521 found_key.offset : root->leafsize;
3523 ret = btrfs_update_block_group(trans, root, bytenr,
3530 btrfs_release_path(NULL, path);
3535 * for a single backref, this will allocate a new extent
3536 * and add the backref to it.
3538 static int record_extent(struct btrfs_trans_handle *trans,
3539 struct btrfs_fs_info *info,
3540 struct btrfs_path *path,
3541 struct extent_record *rec,
3542 struct extent_backref *back,
3543 int allocated, u64 flags)
3546 struct btrfs_root *extent_root = info->extent_root;
3547 struct extent_buffer *leaf;
3548 struct btrfs_key ins_key;
3549 struct btrfs_extent_item *ei;
3550 struct tree_backref *tback;
3551 struct data_backref *dback;
3552 struct btrfs_tree_block_info *bi;
3555 rec->max_size = max_t(u64, rec->max_size,
3556 info->extent_root->leafsize);
3559 u32 item_size = sizeof(*ei);
3562 item_size += sizeof(*bi);
3564 ins_key.objectid = rec->start;
3565 ins_key.offset = rec->max_size;
3566 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
3568 ret = btrfs_insert_empty_item(trans, extent_root, path,
3569 &ins_key, item_size);
3573 leaf = path->nodes[0];
3574 ei = btrfs_item_ptr(leaf, path->slots[0],
3575 struct btrfs_extent_item);
3577 btrfs_set_extent_refs(leaf, ei, 0);
3578 btrfs_set_extent_generation(leaf, ei, rec->generation);
3580 if (back->is_data) {
3581 btrfs_set_extent_flags(leaf, ei,
3582 BTRFS_EXTENT_FLAG_DATA);
3584 struct btrfs_disk_key copy_key;;
3586 tback = (struct tree_backref *)back;
3587 bi = (struct btrfs_tree_block_info *)(ei + 1);
3588 memset_extent_buffer(leaf, 0, (unsigned long)bi,
3590 memset(©_key, 0, sizeof(copy_key));
3592 copy_key.objectid = le64_to_cpu(rec->info_objectid);
3593 btrfs_set_tree_block_level(leaf, bi, rec->info_level);
3594 btrfs_set_tree_block_key(leaf, bi, ©_key);
3596 btrfs_set_extent_flags(leaf, ei,
3597 BTRFS_EXTENT_FLAG_TREE_BLOCK | flags);
3600 btrfs_mark_buffer_dirty(leaf);
3601 ret = btrfs_update_block_group(trans, extent_root, rec->start,
3602 rec->max_size, 1, 0);
3605 btrfs_release_path(NULL, path);
3608 if (back->is_data) {
3612 dback = (struct data_backref *)back;
3613 if (back->full_backref)
3614 parent = dback->parent;
3618 for (i = 0; i < dback->found_ref; i++) {
3619 /* if parent != 0, we're doing a full backref
3620 * passing BTRFS_FIRST_FREE_OBJECTID as the owner
3621 * just makes the backref allocator create a data
3624 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3625 rec->start, rec->max_size,
3629 BTRFS_FIRST_FREE_OBJECTID :
3635 fprintf(stderr, "adding new data backref"
3636 " on %llu %s %llu owner %llu"
3637 " offset %llu found %d\n",
3638 (unsigned long long)rec->start,
3639 back->full_backref ?
3641 back->full_backref ?
3642 (unsigned long long)parent :
3643 (unsigned long long)dback->root,
3644 (unsigned long long)dback->owner,
3645 (unsigned long long)dback->offset,
3650 tback = (struct tree_backref *)back;
3651 if (back->full_backref)
3652 parent = tback->parent;
3656 ret = btrfs_inc_extent_ref(trans, info->extent_root,
3657 rec->start, rec->max_size,
3658 parent, tback->root, 0, 0);
3659 fprintf(stderr, "adding new tree backref on "
3660 "start %llu len %llu parent %llu root %llu\n",
3661 rec->start, rec->max_size, tback->parent, tback->root);
3666 btrfs_release_path(NULL, path);
3670 struct extent_entry {
3674 struct list_head list;
3677 static struct extent_entry *find_entry(struct list_head *entries,
3678 u64 bytenr, u64 bytes)
3680 struct extent_entry *entry = NULL;
3682 list_for_each_entry(entry, entries, list) {
3683 if (entry->bytenr == bytenr && entry->bytes == bytes)
3690 static struct extent_entry *find_most_right_entry(struct list_head *entries)
3692 struct extent_entry *entry, *best = NULL, *prev = NULL;
3694 list_for_each_entry(entry, entries, list) {
3701 * If our current entry == best then we can't be sure our best
3702 * is really the best, so we need to keep searching.
3704 if (best && best->count == entry->count) {
3710 /* Prev == entry, not good enough, have to keep searching */
3711 if (prev->count == entry->count)
3715 best = (prev->count > entry->count) ? prev : entry;
3716 else if (best->count < entry->count)
3724 static int repair_ref(struct btrfs_trans_handle *trans,
3725 struct btrfs_fs_info *info, struct btrfs_path *path,
3726 struct data_backref *dback, struct extent_entry *entry)
3728 struct btrfs_root *root;
3729 struct btrfs_file_extent_item *fi;
3730 struct extent_buffer *leaf;
3731 struct btrfs_key key;
3735 key.objectid = dback->root;
3736 key.type = BTRFS_ROOT_ITEM_KEY;
3737 key.offset = (u64)-1;
3738 root = btrfs_read_fs_root(info, &key);
3740 fprintf(stderr, "Couldn't find root for our ref\n");
3745 * The backref points to the original offset of the extent if it was
3746 * split, so we need to search down to the offset we have and then walk
3747 * forward until we find the backref we're looking for.
3749 key.objectid = dback->owner;
3750 key.type = BTRFS_EXTENT_DATA_KEY;
3751 key.offset = dback->offset;
3752 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3754 fprintf(stderr, "Error looking up ref %d\n", ret);
3759 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3760 ret = btrfs_next_leaf(root, path);
3762 fprintf(stderr, "Couldn't find our ref, next\n");
3766 leaf = path->nodes[0];
3767 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3768 if (key.objectid != dback->owner ||
3769 key.type != BTRFS_EXTENT_DATA_KEY) {
3770 fprintf(stderr, "Couldn't find our ref, search\n");
3773 fi = btrfs_item_ptr(leaf, path->slots[0],
3774 struct btrfs_file_extent_item);
3775 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3776 bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
3778 if (bytenr == dback->disk_bytenr && bytes == dback->bytes)
3783 btrfs_release_path(root, path);
3786 * Have to make sure that this root gets updated when we commit the
3789 root->track_dirty = 1;
3790 if (root->last_trans != trans->transid) {
3791 root->last_trans = trans->transid;
3792 root->commit_root = root->node;
3793 extent_buffer_get(root->node);
3797 * Ok we have the key of the file extent we want to fix, now we can cow
3798 * down to the thing and fix it.
3800 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3802 fprintf(stderr, "Error cowing down to ref [%Lu, %u, %Lu]: %d\n",
3803 key.objectid, key.type, key.offset, ret);
3807 fprintf(stderr, "Well that's odd, we just found this key "
3808 "[%Lu, %u, %Lu]\n", key.objectid, key.type,
3812 leaf = path->nodes[0];
3813 fi = btrfs_item_ptr(leaf, path->slots[0],
3814 struct btrfs_file_extent_item);
3816 if (btrfs_file_extent_compression(leaf, fi) &&
3817 dback->disk_bytenr != entry->bytenr) {
3818 fprintf(stderr, "Ref doesn't match the record start and is "
3819 "compressed, please take a btrfs-image of this file "
3820 "system and send it to a btrfs developer so they can "
3821 "complete this functionality for bytenr %Lu\n",
3822 dback->disk_bytenr);
3826 if (dback->disk_bytenr > entry->bytenr) {
3827 u64 off_diff, offset;
3829 off_diff = dback->disk_bytenr - entry->bytenr;
3830 offset = btrfs_file_extent_offset(leaf, fi);
3831 if (dback->disk_bytenr + offset +
3832 btrfs_file_extent_num_bytes(leaf, fi) >
3833 entry->bytenr + entry->bytes) {
3834 fprintf(stderr, "Ref is past the entry end, please "
3835 "take a btrfs-image of this file system and "
3836 "send it to a btrfs developer, ref %Lu\n",
3837 dback->disk_bytenr);
3841 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
3842 btrfs_set_file_extent_offset(leaf, fi, offset);
3843 } else if (dback->disk_bytenr < entry->bytenr) {
3846 offset = btrfs_file_extent_offset(leaf, fi);
3847 if (dback->disk_bytenr + offset < entry->bytenr) {
3848 fprintf(stderr, "Ref is before the entry start, please"
3849 " take a btrfs-image of this file system and "
3850 "send it to a btrfs developer, ref %Lu\n",
3851 dback->disk_bytenr);
3855 offset += dback->disk_bytenr;
3856 offset -= entry->bytenr;
3857 btrfs_set_file_extent_disk_bytenr(leaf, fi, entry->bytenr);
3858 btrfs_set_file_extent_offset(leaf, fi, offset);
3861 btrfs_set_file_extent_disk_num_bytes(leaf, fi, entry->bytes);
3864 * Chances are if disk_num_bytes were wrong then so is ram_bytes, but
3865 * only do this if we aren't using compression, otherwise it's a
3868 if (!btrfs_file_extent_compression(leaf, fi))
3869 btrfs_set_file_extent_ram_bytes(leaf, fi, entry->bytes);
3871 printf("ram bytes may be wrong?\n");
3872 btrfs_mark_buffer_dirty(leaf);
3873 btrfs_release_path(root, path);
3877 static int verify_backrefs(struct btrfs_trans_handle *trans,
3878 struct btrfs_fs_info *info, struct btrfs_path *path,
3879 struct extent_record *rec)
3881 struct extent_backref *back;
3882 struct data_backref *dback;
3883 struct extent_entry *entry, *best = NULL;
3889 * Metadata is easy and the backrefs should always agree on bytenr and
3890 * size, if not we've got bigger issues.
3895 list_for_each_entry(back, &rec->backrefs, list) {
3896 dback = (struct data_backref *)back;
3898 * We only pay attention to backrefs that we found a real
3901 if (dback->found_ref == 0)
3903 if (back->full_backref)
3907 * For now we only catch when the bytes don't match, not the
3908 * bytenr. We can easily do this at the same time, but I want
3909 * to have a fs image to test on before we just add repair
3910 * functionality willy-nilly so we know we won't screw up the
3914 entry = find_entry(&entries, dback->disk_bytenr,
3917 entry = malloc(sizeof(struct extent_entry));
3922 memset(entry, 0, sizeof(*entry));
3923 entry->bytenr = dback->disk_bytenr;
3924 entry->bytes = dback->bytes;
3925 list_add_tail(&entry->list, &entries);
3931 /* Yay all the backrefs agree, carry on good sir */
3932 if (nr_entries <= 1)
3935 fprintf(stderr, "attempting to repair backref discrepency for bytenr "
3936 "%Lu\n", rec->start);
3939 * First we want to see if the backrefs can agree amongst themselves who
3940 * is right, so figure out which one of the entries has the highest
3943 best = find_most_right_entry(&entries);
3946 * Ok so we may have an even split between what the backrefs think, so
3947 * this is where we use the extent ref to see what it thinks.
3950 entry = find_entry(&entries, rec->start, rec->nr);
3952 fprintf(stderr, "Backrefs don't agree with eachother "
3953 "and extent record doesn't agree with anybody,"
3954 " so we can't fix bytenr %Lu bytes %Lu\n",
3955 rec->start, rec->nr);
3960 best = find_most_right_entry(&entries);
3962 fprintf(stderr, "Backrefs and extent record evenly "
3963 "split on who is right, this is going to "
3964 "require user input to fix bytenr %Lu bytes "
3965 "%Lu\n", rec->start, rec->nr);
3972 * I don't think this can happen currently as we'll abort() if we catch
3973 * this case higher up, but in case somebody removes that we still can't
3974 * deal with it properly here yet, so just bail out of that's the case.
3976 if (best->bytenr != rec->start) {
3977 fprintf(stderr, "Extent start and backref starts don't match, "
3978 "please use btrfs-image on this file system and send "
3979 "it to a btrfs developer so they can make fsck fix "
3980 "this particular case. bytenr is %Lu, bytes is %Lu\n",
3981 rec->start, rec->nr);
3987 * Ok great we all agreed on an extent record, let's go find the real
3988 * references and fix up the ones that don't match.
3990 list_for_each_entry(back, &rec->backrefs, list) {
3991 dback = (struct data_backref *)back;
3994 * Still ignoring backrefs that don't have a real ref attached
3997 if (dback->found_ref == 0)
3999 if (back->full_backref)
4002 if (dback->bytes == best->bytes &&
4003 dback->disk_bytenr == best->bytenr)
4006 ret = repair_ref(trans, info, path, dback, best);
4012 * Ok we messed with the actual refs, which means we need to drop our
4013 * entire cache and go back and rescan. I know this is a huge pain and
4014 * adds a lot of extra work, but it's the only way to be safe. Once all
4015 * the backrefs agree we may not need to do anything to the extent
4020 while (!list_empty(&entries)) {
4021 entry = list_entry(entries.next, struct extent_entry, list);
4022 list_del_init(&entry->list);
4028 static int process_duplicates(struct btrfs_root *root,
4029 struct cache_tree *extent_cache,
4030 struct extent_record *rec)
4032 struct extent_record *good, *tmp;
4033 struct cache_extent *cache;
4037 * If we found a extent record for this extent then return, or if we
4038 * have more than one duplicate we are likely going to need to delete
4041 if (rec->found_rec || rec->num_duplicates > 1)
4044 /* Shouldn't happen but just in case */
4045 BUG_ON(!rec->num_duplicates);
4048 * So this happens if we end up with a backref that doesn't match the
4049 * actual extent entry. So either the backref is bad or the extent
4050 * entry is bad. Either way we want to have the extent_record actually
4051 * reflect what we found in the extent_tree, so we need to take the
4052 * duplicate out and use that as the extent_record since the only way we
4053 * get a duplicate is if we find a real life BTRFS_EXTENT_ITEM_KEY.
4055 remove_cache_extent(extent_cache, &rec->cache);
4057 good = list_entry(rec->dups.next, struct extent_record, list);
4058 list_del_init(&good->list);
4059 INIT_LIST_HEAD(&good->backrefs);
4060 INIT_LIST_HEAD(&good->dups);
4061 good->cache.start = good->start;
4062 good->cache.size = good->nr;
4063 good->content_checked = 0;
4064 good->owner_ref_checked = 0;
4065 good->num_duplicates = 0;
4066 good->refs = rec->refs;
4067 list_splice_init(&rec->backrefs, &good->backrefs);
4069 cache = find_cache_extent(extent_cache, good->start,
4073 tmp = container_of(cache, struct extent_record, cache);
4076 * If we find another overlapping extent and it's found_rec is
4077 * set then it's a duplicate and we need to try and delete
4080 if (tmp->found_rec || tmp->num_duplicates > 0) {
4081 if (list_empty(&good->list))
4082 list_add_tail(&good->list,
4083 &duplicate_extents);
4084 good->num_duplicates += tmp->num_duplicates + 1;
4085 list_splice_init(&tmp->dups, &good->dups);
4086 list_del_init(&tmp->list);
4087 list_add_tail(&tmp->list, &good->dups);
4088 remove_cache_extent(extent_cache, &tmp->cache);
4093 * Ok we have another non extent item backed extent rec, so lets
4094 * just add it to this extent and carry on like we did above.
4096 good->refs += tmp->refs;
4097 list_splice_init(&tmp->backrefs, &good->backrefs);
4098 remove_cache_extent(extent_cache, &tmp->cache);
4101 ret = insert_existing_cache_extent(extent_cache, &good->cache);
4104 return good->num_duplicates ? 0 : 1;
4107 static int delete_duplicate_records(struct btrfs_trans_handle *trans,
4108 struct btrfs_root *root,
4109 struct extent_record *rec)
4111 LIST_HEAD(delete_list);
4112 struct btrfs_path *path;
4113 struct extent_record *tmp, *good, *n;
4116 struct btrfs_key key;
4118 path = btrfs_alloc_path();
4125 /* Find the record that covers all of the duplicates. */
4126 list_for_each_entry(tmp, &rec->dups, list) {
4127 if (good->start < tmp->start)
4129 if (good->nr > tmp->nr)
4132 if (tmp->start + tmp->nr < good->start + good->nr) {
4133 fprintf(stderr, "Ok we have overlapping extents that "
4134 "aren't completely covered by eachother, this "
4135 "is going to require more careful thought. "
4136 "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
4137 tmp->start, tmp->nr, good->start, good->nr);
4144 list_add_tail(&rec->list, &delete_list);
4146 list_for_each_entry_safe(tmp, n, &rec->dups, list) {
4149 list_move_tail(&tmp->list, &delete_list);
4152 root = root->fs_info->extent_root;
4153 list_for_each_entry(tmp, &delete_list, list) {
4154 if (tmp->found_rec == 0)
4156 key.objectid = tmp->start;
4157 key.type = BTRFS_EXTENT_ITEM_KEY;
4158 key.offset = tmp->nr;
4160 /* Shouldn't happen but just in case */
4161 if (tmp->metadata) {
4162 fprintf(stderr, "Well this shouldn't happen, extent "
4163 "record overlaps but is metadata? "
4164 "[%Lu, %Lu]\n", tmp->start, tmp->nr);
4168 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4174 ret = btrfs_del_item(trans, root, path);
4177 btrfs_release_path(root, path);
4182 while (!list_empty(&delete_list)) {
4183 tmp = list_entry(delete_list.next, struct extent_record, list);
4184 list_del_init(&tmp->list);
4190 while (!list_empty(&rec->dups)) {
4191 tmp = list_entry(rec->dups.next, struct extent_record, list);
4192 list_del_init(&tmp->list);
4196 btrfs_free_path(path);
4198 if (!ret && !nr_del)
4199 rec->num_duplicates = 0;
4201 return ret ? ret : nr_del;
4205 * when an incorrect extent item is found, this will delete
4206 * all of the existing entries for it and recreate them
4207 * based on what the tree scan found.
4209 static int fixup_extent_refs(struct btrfs_trans_handle *trans,
4210 struct btrfs_fs_info *info,
4211 struct extent_record *rec)
4214 struct btrfs_path *path;
4215 struct list_head *cur = rec->backrefs.next;
4216 struct cache_extent *cache;
4217 struct extent_backref *back;
4221 /* remember our flags for recreating the extent */
4222 ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
4223 rec->max_size, rec->metadata, NULL,
4226 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4228 path = btrfs_alloc_path();
4230 /* step one, make sure all of the backrefs agree */
4231 ret = verify_backrefs(trans, info, path, rec);
4235 /* step two, delete all the existing records */
4236 ret = delete_extent_records(trans, info->extent_root, path,
4237 rec->start, rec->max_size);
4242 /* was this block corrupt? If so, don't add references to it */
4243 cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size);
4249 /* step three, recreate all the refs we did find */
4250 while(cur != &rec->backrefs) {
4251 back = list_entry(cur, struct extent_backref, list);
4255 * if we didn't find any references, don't create a
4258 if (!back->found_ref)
4261 ret = record_extent(trans, info, path, rec, back, allocated, flags);
4268 btrfs_free_path(path);
4272 /* right now we only prune from the extent allocation tree */
4273 static int prune_one_block(struct btrfs_trans_handle *trans,
4274 struct btrfs_fs_info *info,
4275 struct btrfs_corrupt_block *corrupt)
4278 struct btrfs_path path;
4279 struct extent_buffer *eb;
4283 int level = corrupt->level + 1;
4285 btrfs_init_path(&path);
4287 /* we want to stop at the parent to our busted block */
4288 path.lowest_level = level;
4290 ret = btrfs_search_slot(trans, info->extent_root,
4291 &corrupt->key, &path, -1, 1);
4296 eb = path.nodes[level];
4303 * hopefully the search gave us the block we want to prune,
4304 * lets try that first
4306 slot = path.slots[level];
4307 found = btrfs_node_blockptr(eb, slot);
4308 if (found == corrupt->cache.start)
4311 nritems = btrfs_header_nritems(eb);
4313 /* the search failed, lets scan this node and hope we find it */
4314 for (slot = 0; slot < nritems; slot++) {
4315 found = btrfs_node_blockptr(eb, slot);
4316 if (found == corrupt->cache.start)
4320 * we couldn't find the bad block. TODO, search all the nodes for pointers
4323 if (eb == info->extent_root->node) {
4328 btrfs_release_path(NULL, &path);
4333 printk("deleting pointer to block %Lu\n", corrupt->cache.start);
4334 ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot);
4337 btrfs_release_path(NULL, &path);
4341 static int prune_corrupt_blocks(struct btrfs_trans_handle *trans,
4342 struct btrfs_fs_info *info)
4344 struct cache_extent *cache;
4345 struct btrfs_corrupt_block *corrupt;
4347 cache = find_first_cache_extent(info->corrupt_blocks, 0);
4351 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
4352 prune_one_block(trans, info, corrupt);
4353 cache = next_cache_extent(cache);
4358 static void free_corrupt_blocks(struct btrfs_fs_info *info)
4360 struct cache_extent *cache;
4361 struct btrfs_corrupt_block *corrupt;
4364 cache = find_first_cache_extent(info->corrupt_blocks, 0);
4367 corrupt = container_of(cache, struct btrfs_corrupt_block, cache);
4368 remove_cache_extent(info->corrupt_blocks, cache);
4373 static int check_block_group(struct btrfs_trans_handle *trans,
4374 struct btrfs_fs_info *info,
4375 struct map_lookup *map,
4378 struct btrfs_key key;
4379 struct btrfs_path path;
4382 key.objectid = map->ce.start;
4383 key.offset = map->ce.size;
4384 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
4386 btrfs_init_path(&path);
4387 ret = btrfs_search_slot(NULL, info->extent_root,
4389 btrfs_release_path(NULL, &path);
4393 ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type,
4394 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4395 key.objectid, key.offset);
4401 static int check_block_groups(struct btrfs_trans_handle *trans,
4402 struct btrfs_fs_info *info, int *reinit)
4404 struct cache_extent *ce;
4405 struct map_lookup *map;
4406 struct btrfs_mapping_tree *map_tree = &info->mapping_tree;
4408 /* this isn't quite working */
4411 ce = find_first_cache_extent(&map_tree->cache_tree, 0);
4415 map = container_of(ce, struct map_lookup, ce);
4416 check_block_group(trans, info, map, reinit);
4417 ce = next_cache_extent(ce);
4422 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info)
4424 struct btrfs_block_group_cache *cache;
4429 ret = find_first_extent_bit(&fs_info->free_space_cache, 0,
4430 &start, &end, EXTENT_DIRTY);
4433 clear_extent_dirty(&fs_info->free_space_cache, start, end,
4439 cache = btrfs_lookup_first_block_group(fs_info, start);
4444 start = cache->key.objectid + cache->key.offset;
4448 static int check_extent_refs(struct btrfs_trans_handle *trans,
4449 struct btrfs_root *root,
4450 struct cache_tree *extent_cache, int repair)
4452 struct extent_record *rec;
4453 struct cache_extent *cache;
4462 * if we're doing a repair, we have to make sure
4463 * we don't allocate from the problem extents.
4464 * In the worst case, this will be all the
4467 cache = find_first_cache_extent(extent_cache, 0);
4469 rec = container_of(cache, struct extent_record, cache);
4470 btrfs_pin_extent(root->fs_info,
4471 rec->start, rec->max_size);
4472 cache = next_cache_extent(cache);
4475 /* pin down all the corrupted blocks too */
4476 cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0);
4478 rec = container_of(cache, struct extent_record, cache);
4479 btrfs_pin_extent(root->fs_info,
4480 rec->start, rec->max_size);
4481 cache = next_cache_extent(cache);
4483 prune_corrupt_blocks(trans, root->fs_info);
4484 check_block_groups(trans, root->fs_info, &reinit);
4486 btrfs_read_block_groups(root->fs_info->extent_root);
4487 reset_cached_block_groups(root->fs_info);
4491 * We need to delete any duplicate entries we find first otherwise we
4492 * could mess up the extent tree when we have backrefs that actually
4493 * belong to a different extent item and not the weird duplicate one.
4495 while (repair && !list_empty(&duplicate_extents)) {
4496 rec = list_entry(duplicate_extents.next, struct extent_record,
4498 list_del_init(&rec->list);
4500 /* Sometimes we can find a backref before we find an actual
4501 * extent, so we need to process it a little bit to see if there
4502 * truly are multiple EXTENT_ITEM_KEY's for the same range, or
4503 * if this is a backref screwup. If we need to delete stuff
4504 * process_duplicates() will return 0, otherwise it will return
4507 if (process_duplicates(root, extent_cache, rec))
4509 ret = delete_duplicate_records(trans, root, rec);
4513 * delete_duplicate_records will return the number of entries
4514 * deleted, so if it's greater than 0 then we know we actually
4515 * did something and we need to remove.
4526 cache = find_first_cache_extent(extent_cache, 0);
4529 rec = container_of(cache, struct extent_record, cache);
4530 if (rec->num_duplicates) {
4531 fprintf(stderr, "extent item %llu has multiple extent "
4532 "items\n", (unsigned long long)rec->start);
4536 if (rec->refs != rec->extent_item_refs) {
4537 fprintf(stderr, "ref mismatch on [%llu %llu] ",
4538 (unsigned long long)rec->start,
4539 (unsigned long long)rec->nr);
4540 fprintf(stderr, "extent item %llu, found %llu\n",
4541 (unsigned long long)rec->extent_item_refs,
4542 (unsigned long long)rec->refs);
4543 if (!fixed && repair) {
4544 ret = fixup_extent_refs(trans, root->fs_info, rec);
4552 if (all_backpointers_checked(rec, 1)) {
4553 fprintf(stderr, "backpointer mismatch on [%llu %llu]\n",
4554 (unsigned long long)rec->start,
4555 (unsigned long long)rec->nr);
4557 if (!fixed && repair) {
4558 ret = fixup_extent_refs(trans, root->fs_info, rec);
4566 if (!rec->owner_ref_checked) {
4567 fprintf(stderr, "owner ref check failed [%llu %llu]\n",
4568 (unsigned long long)rec->start,
4569 (unsigned long long)rec->nr);
4570 if (!fixed && repair) {
4571 ret = fixup_extent_refs(trans, root->fs_info, rec);
4579 remove_cache_extent(extent_cache, cache);
4580 free_all_extent_backrefs(rec);
4585 if (ret && ret != -EAGAIN) {
4586 fprintf(stderr, "failed to repair damaged filesystem, aborting\n");
4589 btrfs_fix_block_accounting(trans, root);
4592 fprintf(stderr, "repaired damaged extent references\n");
4598 static void free_cache_tree(struct cache_tree *tree)
4600 struct cache_extent *cache;
4603 cache = find_first_cache_extent(tree, 0);
4606 remove_cache_extent(tree, cache);
4611 static int check_extents(struct btrfs_root *root, int repair)
4613 struct cache_tree extent_cache;
4614 struct cache_tree seen;
4615 struct cache_tree pending;
4616 struct cache_tree reada;
4617 struct cache_tree nodes;
4618 struct cache_tree corrupt_blocks;
4619 struct btrfs_path path;
4620 struct btrfs_key key;
4621 struct btrfs_key found_key;
4624 struct block_info *bits;
4626 struct extent_buffer *leaf;
4627 struct btrfs_trans_handle *trans = NULL;
4629 struct btrfs_root_item ri;
4631 cache_tree_init(&extent_cache);
4632 cache_tree_init(&seen);
4633 cache_tree_init(&pending);
4634 cache_tree_init(&nodes);
4635 cache_tree_init(&reada);
4636 cache_tree_init(&corrupt_blocks);
4639 trans = btrfs_start_transaction(root, 1);
4640 if (IS_ERR(trans)) {
4641 fprintf(stderr, "Error starting transaction\n");
4642 return PTR_ERR(trans);
4644 root->fs_info->fsck_extent_cache = &extent_cache;
4645 root->fs_info->free_extent_hook = free_extent_hook;
4646 root->fs_info->corrupt_blocks = &corrupt_blocks;
4650 bits = malloc(bits_nr * sizeof(struct block_info));
4657 add_root_to_pending(root->fs_info->tree_root->node,
4658 &extent_cache, &pending, &seen, &nodes,
4659 &root->fs_info->tree_root->root_key);
4661 add_root_to_pending(root->fs_info->chunk_root->node,
4662 &extent_cache, &pending, &seen, &nodes,
4663 &root->fs_info->chunk_root->root_key);
4665 btrfs_init_path(&path);
4668 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
4669 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
4673 leaf = path.nodes[0];
4674 slot = path.slots[0];
4675 if (slot >= btrfs_header_nritems(path.nodes[0])) {
4676 ret = btrfs_next_leaf(root, &path);
4679 leaf = path.nodes[0];
4680 slot = path.slots[0];
4682 btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
4683 if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
4684 unsigned long offset;
4685 struct extent_buffer *buf;
4687 offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
4688 read_extent_buffer(leaf, &ri, offset, sizeof(ri));
4689 buf = read_tree_block(root->fs_info->tree_root,
4690 btrfs_root_bytenr(&ri),
4691 btrfs_level_size(root,
4692 btrfs_root_level(&ri)), 0);
4693 add_root_to_pending(buf, &extent_cache, &pending,
4694 &seen, &nodes, &found_key);
4695 free_extent_buffer(buf);
4699 btrfs_release_path(root, &path);
4701 ret = run_next_block(root, bits, bits_nr, &last, &pending,
4702 &seen, &reada, &nodes, &extent_cache);
4706 ret = check_extent_refs(trans, root, &extent_cache, repair);
4708 if (ret == -EAGAIN) {
4709 ret = btrfs_commit_transaction(trans, root);
4713 trans = btrfs_start_transaction(root, 1);
4714 if (IS_ERR(trans)) {
4715 ret = PTR_ERR(trans);
4719 free_corrupt_blocks(root->fs_info);
4720 free_cache_tree(&seen);
4721 free_cache_tree(&pending);
4722 free_cache_tree(&reada);
4723 free_cache_tree(&nodes);
4724 free_extent_cache(root->fs_info, &extent_cache);
4731 err = btrfs_commit_transaction(trans, root);
4737 free_corrupt_blocks(root->fs_info);
4738 root->fs_info->fsck_extent_cache = NULL;
4739 root->fs_info->free_extent_hook = NULL;
4740 root->fs_info->corrupt_blocks = NULL;
4746 static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
4747 struct extent_buffer *eb, int tree_root)
4749 struct extent_buffer *tmp;
4750 struct btrfs_root_item *ri;
4751 struct btrfs_key key;
4754 int level = btrfs_header_level(eb);
4759 btrfs_pin_extent(fs_info, eb->start, eb->len);
4761 leafsize = btrfs_super_leafsize(fs_info->super_copy);
4762 nritems = btrfs_header_nritems(eb);
4763 for (i = 0; i < nritems; i++) {
4765 btrfs_item_key_to_cpu(eb, &key, i);
4766 if (key.type != BTRFS_ROOT_ITEM_KEY)
4768 /* Skip the extent root and reloc roots */
4769 if (key.objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4770 key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
4771 key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
4773 ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
4774 bytenr = btrfs_disk_root_bytenr(eb, ri);
4777 * If at any point we start needing the real root we
4778 * will have to build a stump root for the root we are
4779 * in, but for now this doesn't actually use the root so
4780 * just pass in extent_root.
4782 tmp = read_tree_block(fs_info->extent_root, bytenr,
4785 fprintf(stderr, "Error reading root block\n");
4788 ret = pin_down_tree_blocks(fs_info, tmp, 0);
4789 free_extent_buffer(tmp);
4793 bytenr = btrfs_node_blockptr(eb, i);
4795 /* If we aren't the tree root don't read the block */
4796 if (level == 1 && !tree_root) {
4797 btrfs_pin_extent(fs_info, bytenr, leafsize);
4801 tmp = read_tree_block(fs_info->extent_root, bytenr,
4804 fprintf(stderr, "Error reading tree block\n");
4807 ret = pin_down_tree_blocks(fs_info, tmp, tree_root);
4808 free_extent_buffer(tmp);
4817 static int pin_metadata_blocks(struct btrfs_fs_info *fs_info)
4821 ret = pin_down_tree_blocks(fs_info, fs_info->chunk_root->node, 0);
4825 return pin_down_tree_blocks(fs_info, fs_info->tree_root->node, 1);
4828 static int reset_block_groups(struct btrfs_fs_info *fs_info)
4830 struct btrfs_path *path;
4831 struct extent_buffer *leaf;
4832 struct btrfs_chunk *chunk;
4833 struct btrfs_key key;
4836 path = btrfs_alloc_path();
4841 key.type = BTRFS_CHUNK_ITEM_KEY;
4844 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
4846 btrfs_free_path(path);
4851 * We do this in case the block groups were screwed up and had alloc
4852 * bits that aren't actually set on the chunks. This happens with
4853 * restored images every time and could happen in real life I guess.
4855 fs_info->avail_data_alloc_bits = 0;
4856 fs_info->avail_metadata_alloc_bits = 0;
4857 fs_info->avail_system_alloc_bits = 0;
4859 /* First we need to create the in-memory block groups */
4861 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4862 ret = btrfs_next_leaf(fs_info->chunk_root, path);
4864 btrfs_free_path(path);
4872 leaf = path->nodes[0];
4873 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4874 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
4879 chunk = btrfs_item_ptr(leaf, path->slots[0],
4880 struct btrfs_chunk);
4881 btrfs_add_block_group(fs_info, 0,
4882 btrfs_chunk_type(leaf, chunk),
4883 key.objectid, key.offset,
4884 btrfs_chunk_length(leaf, chunk));
4888 btrfs_free_path(path);
4892 static int reset_balance(struct btrfs_trans_handle *trans,
4893 struct btrfs_fs_info *fs_info)
4895 struct btrfs_root *root = fs_info->tree_root;
4896 struct btrfs_path *path;
4897 struct extent_buffer *leaf;
4898 struct btrfs_key key;
4899 int del_slot, del_nr = 0;
4903 path = btrfs_alloc_path();
4907 key.objectid = BTRFS_BALANCE_OBJECTID;
4908 key.type = BTRFS_BALANCE_ITEM_KEY;
4911 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4918 ret = btrfs_del_item(trans, root, path);
4921 btrfs_release_path(root, path);
4923 key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4924 key.type = BTRFS_ROOT_ITEM_KEY;
4927 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4931 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
4936 ret = btrfs_del_items(trans, root, path,
4943 btrfs_release_path(root, path);
4946 ret = btrfs_search_slot(trans, root, &key, path,
4953 leaf = path->nodes[0];
4954 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4955 if (key.objectid > BTRFS_TREE_RELOC_OBJECTID)
4957 if (key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
4962 del_slot = path->slots[0];
4971 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
4975 btrfs_release_path(root, path);
4977 key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4978 key.type = BTRFS_ROOT_ITEM_KEY;
4979 key.offset = (u64)-1;
4980 root = btrfs_read_fs_root(fs_info, &key);
4982 fprintf(stderr, "Error reading data reloc tree\n");
4983 return PTR_ERR(root);
4985 root->track_dirty = 1;
4986 if (root->last_trans != trans->transid) {
4987 root->last_trans = trans->transid;
4988 root->commit_root = root->node;
4989 extent_buffer_get(root->node);
4991 ret = btrfs_fsck_reinit_root(trans, root, 0);
4993 btrfs_free_path(path);
4997 static int reinit_extent_tree(struct btrfs_fs_info *fs_info)
4999 struct btrfs_trans_handle *trans;
5004 * The only reason we don't do this is because right now we're just
5005 * walking the trees we find and pinning down their bytes, we don't look
5006 * at any of the leaves. In order to do mixed groups we'd have to check
5007 * the leaves of any fs roots and pin down the bytes for any file
5008 * extents we find. Not hard but why do it if we don't have to?
5010 if (btrfs_fs_incompat(fs_info, BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)) {
5011 fprintf(stderr, "We don't support re-initing the extent tree "
5012 "for mixed block groups yet, please notify a btrfs "
5013 "developer you want to do this so they can add this "
5014 "functionality.\n");
5018 trans = btrfs_start_transaction(fs_info->extent_root, 1);
5019 if (IS_ERR(trans)) {
5020 fprintf(stderr, "Error starting transaction\n");
5021 return PTR_ERR(trans);
5025 * first we need to walk all of the trees except the extent tree and pin
5026 * down the bytes that are in use so we don't overwrite any existing
5029 ret = pin_metadata_blocks(fs_info);
5031 fprintf(stderr, "error pinning down used bytes\n");
5036 * Need to drop all the block groups since we're going to recreate all
5039 btrfs_free_block_groups(fs_info);
5040 ret = reset_block_groups(fs_info);
5042 fprintf(stderr, "error resetting the block groups\n");
5046 /* Ok we can allocate now, reinit the extent root */
5047 ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 1);
5049 fprintf(stderr, "extent root initialization failed\n");
5051 * When the transaction code is updated we should end the
5052 * transaction, but for now progs only knows about commit so
5053 * just return an error.
5058 ret = reset_balance(trans, fs_info);
5060 fprintf(stderr, "error reseting the pending balance\n");
5065 * Now we have all the in-memory block groups setup so we can make
5066 * allocations properly, and the metadata we care about is safe since we
5067 * pinned all of it above.
5070 struct btrfs_block_group_cache *cache;
5072 cache = btrfs_lookup_first_block_group(fs_info, start);
5075 start = cache->key.objectid + cache->key.offset;
5076 ret = btrfs_insert_item(trans, fs_info->extent_root,
5077 &cache->key, &cache->item,
5078 sizeof(cache->item));
5080 fprintf(stderr, "Error adding block group\n");
5083 btrfs_extent_post_op(trans, fs_info->extent_root);
5087 * Ok now we commit and run the normal fsck, which will add extent
5088 * entries for all of the items it finds.
5090 return btrfs_commit_transaction(trans, fs_info->extent_root);
5093 static struct option long_options[] = {
5094 { "super", 1, NULL, 's' },
5095 { "repair", 0, NULL, 0 },
5096 { "init-csum-tree", 0, NULL, 0 },
5097 { "init-extent-tree", 0, NULL, 0 },
5101 const char * const cmd_check_usage[] = {
5102 "btrfs check [options] <device>",
5103 "Check an unmounted btrfs filesystem.",
5105 "-s|--super <superblock> use this superblock copy",
5106 "--repair try to repair the filesystem",
5107 "--init-csum-tree create a new CRC tree",
5108 "--init-extent-tree create a new extent tree",
5112 int cmd_check(int argc, char **argv)
5114 struct cache_tree root_cache;
5115 struct btrfs_root *root;
5116 struct btrfs_fs_info *info;
5122 int option_index = 0;
5123 int init_csum_tree = 0;
5124 int init_extent_tree = 0;
5129 c = getopt_long(argc, argv, "as:", long_options,
5134 case 'a': /* ignored */ break;
5137 bytenr = btrfs_sb_offset(num);
5138 printf("using SB copy %d, bytenr %llu\n", num,
5139 (unsigned long long)bytenr);
5143 usage(cmd_check_usage);
5145 if (option_index == 1) {
5146 printf("enabling repair mode\n");
5149 } else if (option_index == 2) {
5150 printf("Creating a new CRC tree\n");
5153 } else if (option_index == 3) {
5154 init_extent_tree = 1;
5160 argc = argc - optind;
5163 usage(cmd_check_usage);
5166 cache_tree_init(&root_cache);
5168 if((ret = check_mounted(argv[optind])) < 0) {
5169 fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
5172 fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
5176 info = open_ctree_fs_info(argv[optind], bytenr, 0, rw, 1);
5178 fprintf(stderr, "Couldn't open file system\n");
5182 uuid_unparse(info->super_copy->fsid, uuidbuf);
5183 printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
5185 if (!extent_buffer_uptodate(info->tree_root->node) ||
5186 !extent_buffer_uptodate(info->dev_root->node) ||
5187 !extent_buffer_uptodate(info->extent_root->node) ||
5188 !extent_buffer_uptodate(info->chunk_root->node)) {
5189 fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
5193 root = info->fs_root;
5195 if (init_extent_tree) {
5196 printf("Creating a new extent tree\n");
5197 ret = reinit_extent_tree(info);
5201 fprintf(stderr, "checking extents\n");
5202 if (init_csum_tree) {
5203 struct btrfs_trans_handle *trans;
5205 fprintf(stderr, "Reinit crc root\n");
5206 trans = btrfs_start_transaction(info->csum_root, 1);
5207 if (IS_ERR(trans)) {
5208 fprintf(stderr, "Error starting transaction\n");
5209 return PTR_ERR(trans);
5212 ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
5214 fprintf(stderr, "crc root initialization failed\n");
5218 ret = btrfs_commit_transaction(trans, root);
5223 ret = check_extents(root, repair);
5225 fprintf(stderr, "Errors found in extent allocation tree\n");
5227 fprintf(stderr, "checking free space cache\n");
5228 ret = check_space_cache(root);
5232 fprintf(stderr, "checking fs roots\n");
5233 ret = check_fs_roots(root, &root_cache);
5237 fprintf(stderr, "checking csums\n");
5238 ret = check_csums(root);
5242 fprintf(stderr, "checking root refs\n");
5243 ret = check_root_refs(root, &root_cache);
5245 free_root_recs(&root_cache);
5248 if (found_old_backref) { /*
5249 * there was a disk format change when mixed
5250 * backref was in testing tree. The old format
5251 * existed about one week.
5253 printf("\n * Found old mixed backref format. "
5254 "The old format is not supported! *"
5255 "\n * Please mount the FS in readonly mode, "
5256 "backup data and re-format the FS. *\n\n");
5259 printf("found %llu bytes used err is %d\n",
5260 (unsigned long long)bytes_used, ret);
5261 printf("total csum bytes: %llu\n",(unsigned long long)total_csum_bytes);
5262 printf("total tree bytes: %llu\n",
5263 (unsigned long long)total_btree_bytes);
5264 printf("total fs tree bytes: %llu\n",
5265 (unsigned long long)total_fs_tree_bytes);
5266 printf("total extent tree bytes: %llu\n",
5267 (unsigned long long)total_extent_tree_bytes);
5268 printf("btree space waste bytes: %llu\n",
5269 (unsigned long long)btree_space_waste);
5270 printf("file data blocks allocated: %llu\n referenced %llu\n",
5271 (unsigned long long)data_bytes_allocated,
5272 (unsigned long long)data_bytes_referenced);
5273 printf("%s\n", BTRFS_BUILD_VERSION);