u64 info_objectid;
u32 num_duplicates;
u8 info_level;
+ int flag_block_full_backref;
unsigned int found_rec:1;
unsigned int content_checked:1;
unsigned int owner_ref_checked:1;
unsigned int is_root:1;
unsigned int metadata:1;
- unsigned int flag_block_full_backref:1;
+ unsigned int bad_full_backref:1;
+ unsigned int crossing_stripes:1;
};
struct inode_backref {
struct list_head list;
u64 objectid;
u64 bytenr;
+ u64 last_snapshot;
u8 level;
u8 drop_level;
int level_size;
return hole->start;
}
-int compare_hole(struct rb_node *node1, struct rb_node *node2)
+static int compare_hole(struct rb_node *node1, struct rb_node *node2)
{
struct file_extent_hole *hole1;
struct file_extent_hole *hole2;
{
struct file_extent_hole *hole;
struct file_extent_hole tmp;
- struct file_extent_hole prev;
- struct file_extent_hole next;
+ u64 prev_start = 0;
+ u64 prev_len = 0;
+ u64 next_start = 0;
+ u64 next_len = 0;
struct rb_node *node;
int have_prev = 0;
int have_next = 0;
* split(s) if they exists.
*/
if (start > hole->start) {
- prev.start = hole->start;
- prev.len = start - hole->start;
+ prev_start = hole->start;
+ prev_len = start - hole->start;
have_prev = 1;
}
if (hole->start + hole->len > start + len) {
- next.start = start + len;
- next.len = hole->start + hole->len - start - len;
+ next_start = start + len;
+ next_len = hole->start + hole->len - start - len;
have_next = 1;
}
rb_erase(node, holes);
free(hole);
if (have_prev) {
- ret = add_file_extent_hole(holes, prev.start, prev.len);
+ ret = add_file_extent_hole(holes, prev_start, prev_len);
if (ret < 0)
return ret;
}
if (have_next) {
- ret = add_file_extent_hole(holes, next.start, next.len);
+ ret = add_file_extent_hole(holes, next_start, next_len);
if (ret < 0)
return ret;
}
struct orphan_data_extent *src_orphan;
struct orphan_data_extent *dst_orphan;
size_t size;
+ int ret;
rec = malloc(sizeof(*rec));
memcpy(rec, orig_rec, sizeof(*rec));
rec->refs = 1;
INIT_LIST_HEAD(&rec->backrefs);
INIT_LIST_HEAD(&rec->orphan_extents);
+ rec->holes = RB_ROOT;
list_for_each_entry(orig, &orig_rec->backrefs, list) {
size = sizeof(*orig) + orig->namelen + 1;
memcpy(dst_orphan, src_orphan, sizeof(*src_orphan));
list_add_tail(&dst_orphan->list, &rec->orphan_extents);
}
+ ret = copy_file_extent_holes(&rec->holes, &orig_rec->holes);
+ BUG_ON(ret < 0);
+
return rec;
}
if (errors & I_ERR_FILE_EXTENT_DISCOUNT) {
struct file_extent_hole *hole;
struct rb_node *node;
+ int found = 0;
node = rb_first(&rec->holes);
fprintf(stderr, "Found file extent holes:\n");
while (node) {
+ found = 1;
hole = rb_entry(node, struct file_extent_hole, node);
- fprintf(stderr, "\tstart: %llu, len:%llu\n",
+ fprintf(stderr, "\tstart: %llu, len: %llu\n",
hole->start, hole->len);
node = rb_next(node);
}
+ if (!found)
+ fprintf(stderr, "\tstart: 0, len: %llu\n",
+ round_up(rec->isize, root->sectorsize));
}
}
return ret;
}
+static int repair_inode_nbytes(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path,
+ struct inode_record *rec)
+{
+ struct btrfs_inode_item *ei;
+ struct btrfs_key key;
+ int ret = 0;
+
+ key.objectid = rec->ino;
+ key.type = BTRFS_INODE_ITEM_KEY;
+ key.offset = 0;
+
+ ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+ if (ret) {
+ if (ret > 0)
+ ret = -ENOENT;
+ goto out;
+ }
+
+ /* Since ret == 0, no need to check anything */
+ ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_inode_item);
+ btrfs_set_inode_nbytes(path->nodes[0], ei, rec->found_size);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+ rec->errors &= ~I_ERR_FILE_NBYTES_WRONG;
+ printf("reset nbytes for ino %llu root %llu\n",
+ rec->ino, root->root_key.objectid);
+out:
+ btrfs_release_path(path);
+ return ret;
+}
+
static int add_missing_dir_index(struct btrfs_root *root,
struct cache_tree *inode_cache,
struct inode_record *rec,
BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
mode);
if (ret < 0) {
- fprintf(stderr, "Failed to create '%s' dir: %s",
+ fprintf(stderr, "Failed to create '%s' dir: %s\n",
dir_name, strerror(-ret));
goto out;
}
ret = btrfs_add_link(trans, root, rec->ino, lost_found_ino,
namebuf, namelen, type, NULL, 1);
- if (ret == -EEXIST) {
+ /*
+ * Add ".INO" suffix several times to handle case where
+ * "FILENAME.INO" is already taken by another file.
+ */
+ while (ret == -EEXIST) {
/*
* Conflicting file name, add ".INO" as suffix * +1 for '.'
*/
}
if (ret < 0) {
fprintf(stderr,
- "Failed to link the inode %llu to %s dir: %s",
+ "Failed to link the inode %llu to %s dir: %s\n",
rec->ino, dir_name, strerror(-ret));
goto out;
}
printf("Moving file '%.*s' to '%s' dir since it has no valid backref\n",
namelen, namebuf, dir_name);
}
- rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
printf("Fixed the nlink of inode %llu\n", rec->ino);
out:
+ /*
+ * Clear the flag anyway, or we will loop forever for the same inode
+ * as it will not be removed from the bad inode list and the dead loop
+ * happens.
+ */
+ rec->errors &= ~I_ERR_LINK_COUNT_WRONG;
btrfs_release_path(path);
return ret;
}
{
struct rb_node *node;
struct file_extent_hole *hole;
+ int found = 0;
int ret = 0;
node = rb_first(&rec->holes);
while (node) {
+ found = 1;
hole = rb_entry(node, struct file_extent_hole, node);
ret = btrfs_punch_hole(trans, root, rec->ino,
hole->start, hole->len);
rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
node = rb_first(&rec->holes);
}
+ /* special case for a file losing all its file extent */
+ if (!found) {
+ ret = btrfs_punch_hole(trans, root, rec->ino, 0,
+ round_up(rec->isize, root->sectorsize));
+ if (ret < 0)
+ goto out;
+ }
printf("Fixed discount file extents for inode: %llu in root: %llu\n",
rec->ino, root->objectid);
out:
I_ERR_LINK_COUNT_WRONG |
I_ERR_NO_INODE_ITEM |
I_ERR_FILE_EXTENT_ORPHAN |
- I_ERR_FILE_EXTENT_DISCOUNT)))
+ I_ERR_FILE_EXTENT_DISCOUNT|
+ I_ERR_FILE_NBYTES_WRONG)))
return rec->errors;
path = btrfs_alloc_path();
ret = repair_inode_orphan_item(trans, root, path, rec);
if (!ret && rec->errors & I_ERR_LINK_COUNT_WRONG)
ret = repair_inode_nlinks(trans, root, path, rec);
+ if (!ret && rec->errors & I_ERR_FILE_NBYTES_WRONG)
+ ret = repair_inode_nbytes(trans, root, path, rec);
btrfs_commit_transaction(trans, root);
btrfs_free_path(path);
return ret;
}
backref = malloc(sizeof(*backref) + namelen + 1);
- memset(backref, 0, sizeof(*backref));
+ memset(backref, 0, sizeof(*backref) + namelen + 1);
backref->ref_root = ref_root;
backref->dir = dir;
backref->index = index;
{
if (rec->content_checked && rec->owner_ref_checked &&
rec->extent_item_refs == rec->refs && rec->refs > 0 &&
- rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0)) {
+ rec->num_duplicates == 0 && !all_backpointers_checked(rec, 0) &&
+ !rec->bad_full_backref && !rec->crossing_stripes) {
remove_cache_extent(extent_cache, &rec->cache);
free_all_extent_backrefs(rec);
list_del_init(&rec->list);
if (rec->max_size < max_size)
rec->max_size = max_size;
+ /*
+ * A metadata extent can't cross stripe_len boundary, otherwise
+ * kernel scrub won't be able to handle it.
+ * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
+ * it.
+ */
+ if (metadata && check_crossing_stripes(rec->start,
+ rec->max_size))
+ rec->crossing_stripes = 1;
maybe_free_extent_rec(extent_cache, rec);
return ret;
}
rec->owner_ref_checked = 0;
rec->num_duplicates = 0;
rec->metadata = metadata;
+ rec->flag_block_full_backref = -1;
+ rec->bad_full_backref = 0;
+ rec->crossing_stripes = 0;
INIT_LIST_HEAD(&rec->backrefs);
INIT_LIST_HEAD(&rec->dups);
INIT_LIST_HEAD(&rec->list);
rec->content_checked = 1;
rec->owner_ref_checked = 1;
}
+
+ if (metadata)
+ if (check_crossing_stripes(rec->start, rec->max_size))
+ rec->crossing_stripes = 1;
return ret;
}
return error ? -EINVAL : 0;
}
-static int read_extent_data(struct btrfs_root *root, char *data,
- u64 logical, u64 *len, int mirror)
-{
- u64 offset = 0;
- struct btrfs_multi_bio *multi = NULL;
- struct btrfs_fs_info *info = root->fs_info;
- struct btrfs_device *device;
- int ret = 0;
- u64 max_len = *len;
-
- ret = btrfs_map_block(&info->mapping_tree, READ, logical, len,
- &multi, mirror, NULL);
- if (ret) {
- fprintf(stderr, "Couldn't map the block %llu\n",
- logical + offset);
- goto err;
- }
- device = multi->stripes[0].dev;
-
- if (device->fd == 0)
- goto err;
- if (*len > max_len)
- *len = max_len;
-
- ret = pread64(device->fd, data, *len, multi->stripes[0].physical);
- if (ret != *len)
- ret = -EIO;
- else
- ret = 0;
-err:
- kfree(multi);
- return ret;
-}
-
static int check_extent_csums(struct btrfs_root *root, u64 bytenr,
u64 num_bytes, unsigned long leaf_offset,
struct extent_buffer *eb) {
return 0;
}
+/*
+ * Here are the rules for FULL_BACKREF.
+ *
+ * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
+ * 2) If btrfs_header_owner(buf) no longer points to buf then we have
+ * FULL_BACKREF set.
+ * 3) We cow'ed the block walking down a reloc tree. This is impossible to tell
+ * if it happened after the relocation occurred since we'll have dropped the
+ * reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
+ * have no real way to know for sure.
+ *
+ * We process the blocks one root at a time, and we start from the lowest root
+ * objectid and go to the highest. So we can just lookup the owner backref for
+ * the record and if we don't find it then we know it doesn't exist and we have
+ * a FULL BACKREF.
+ *
+ * FIXME: if we ever start reclaiming root objectid's then we need to fix this
+ * assumption and simply indicate that we _think_ that the FULL BACKREF needs to
+ * be set or not and then we can check later once we've gathered all the refs.
+ */
static int calc_extent_flag(struct btrfs_root *root,
struct cache_tree *extent_cache,
struct extent_buffer *buf,
struct root_item_record *ri,
u64 *flags)
{
- int i;
- int nritems = btrfs_header_nritems(buf);
- struct btrfs_key key;
struct extent_record *rec;
struct cache_extent *cache;
- struct data_backref *dback;
struct tree_backref *tback;
- struct extent_buffer *new_buf;
u64 owner = 0;
- u64 bytenr;
- u64 offset;
- u64 ptr;
- int size;
- int ret;
- u8 level;
+
+ cache = lookup_cache_extent(extent_cache, buf->start, 1);
+ /* we have added this extent before */
+ BUG_ON(!cache);
+ rec = container_of(cache, struct extent_record, cache);
/*
* Except file/reloc tree, we can not have
*/
if (buf->start == ri->bytenr)
goto normal;
- if (btrfs_is_leaf(buf)) {
- /*
- * we are searching from original root, world
- * peace is achieved, we use normal backref.
- */
- owner = btrfs_header_owner(buf);
- if (owner == ri->objectid)
- goto normal;
- /*
- * we check every eb here, and if any of
- * eb dosen't have original root refers
- * to this eb, we set full backref flag for
- * this extent, otherwise normal backref.
- */
- for (i = 0; i < nritems; i++) {
- struct btrfs_file_extent_item *fi;
- btrfs_item_key_to_cpu(buf, &key, i);
- if (key.type != BTRFS_EXTENT_DATA_KEY)
- continue;
- fi = btrfs_item_ptr(buf, i,
- struct btrfs_file_extent_item);
- if (btrfs_file_extent_type(buf, fi) ==
- BTRFS_FILE_EXTENT_INLINE)
- continue;
- if (btrfs_file_extent_disk_bytenr(buf, fi) == 0)
- continue;
- bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
- cache = lookup_cache_extent(extent_cache, bytenr, 1);
- if (!cache)
- goto full_backref;
- offset = btrfs_file_extent_offset(buf, fi);
- rec = container_of(cache, struct extent_record, cache);
- dback = find_data_backref(rec, 0, ri->objectid, owner,
- key.offset - offset, 1, bytenr, bytenr);
- if (!dback)
- goto full_backref;
- }
+ if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))
goto full_backref;
- } else {
- level = btrfs_header_level(buf);
- for (i = 0; i < nritems; i++) {
- ptr = btrfs_node_blockptr(buf, i);
- size = btrfs_level_size(root, level);
- if (i == 0) {
- new_buf = read_tree_block(root, ptr, size, 0);
- if (!extent_buffer_uptodate(new_buf)) {
- free_extent_buffer(new_buf);
- ret = -EIO;
- return ret;
- }
- /*
- * we are searching from origin root, world
- * peace is achieved, we use normal backref.
- */
- owner = btrfs_header_owner(new_buf);
- free_extent_buffer(new_buf);
- if (owner == ri->objectid)
- goto normal;
- }
- cache = lookup_cache_extent(extent_cache, ptr, size);
- if (!cache)
- goto full_backref;
- rec = container_of(cache, struct extent_record, cache);
- tback = find_tree_backref(rec, 0, owner);
- if (!tback)
- goto full_backref;
- }
- }
+ owner = btrfs_header_owner(buf);
+ if (owner == ri->objectid)
+ goto normal;
+
+ tback = find_tree_backref(rec, 0, owner);
+ if (!tback)
+ goto full_backref;
normal:
*flags = 0;
- cache = lookup_cache_extent(extent_cache, buf->start, 1);
- /* we have added this extent before */
- BUG_ON(!cache);
- rec = container_of(cache, struct extent_record, cache);
- rec->flag_block_full_backref = 0;
+ if (rec->flag_block_full_backref != -1 &&
+ rec->flag_block_full_backref != 0)
+ rec->bad_full_backref = 1;
return 0;
full_backref:
*flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
- cache = lookup_cache_extent(extent_cache, buf->start, 1);
- /* we have added this extent before */
- BUG_ON(!cache);
- rec = container_of(cache, struct extent_record, cache);
- rec->flag_block_full_backref = 1;
+ if (rec->flag_block_full_backref != -1 &&
+ rec->flag_block_full_backref != 1)
+ rec->bad_full_backref = 1;
return 0;
}
nritems = btrfs_header_nritems(buf);
- /*
- * FIXME, this only works only if we don't have any full
- * backref mode.
- */
flags = 0;
if (!init_extent_tree) {
ret = btrfs_lookup_extent_info(NULL, root, bytenr,
}
if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
- if (rec)
- rec->flag_block_full_backref = 1;
+ if (ri != NULL &&
+ ri->objectid != BTRFS_TREE_RELOC_OBJECTID &&
+ ri->objectid == btrfs_header_owner(buf)) {
+ /*
+ * Ok we got to this block from it's original owner and
+ * we have FULL_BACKREF set. Relocation can leave
+ * converted blocks over so this is altogether possible,
+ * however it's not possible if the generation > the
+ * last snapshot, so check for this case.
+ */
+ if (!btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC) &&
+ btrfs_header_generation(buf) > ri->last_snapshot) {
+ flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
+ rec->bad_full_backref = 1;
+ }
+ }
+ } else {
+ if (ri != NULL &&
+ (ri->objectid == BTRFS_TREE_RELOC_OBJECTID ||
+ btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) {
+ flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
+ rec->bad_full_backref = 1;
+ }
+ }
+
+ if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
+ rec->flag_block_full_backref = 1;
parent = bytenr;
owner = 0;
} else {
+ rec->flag_block_full_backref = 0;
parent = 0;
owner = btrfs_header_owner(buf);
}
parent, tback->root, 0, 0);
fprintf(stderr, "adding new tree backref on "
"start %llu len %llu parent %llu root %llu\n",
- rec->start, rec->max_size, tback->parent, tback->root);
+ rec->start, rec->max_size, parent, tback->root);
}
if (ret)
goto fail;
if (!back->found_ref)
continue;
+ rec->bad_full_backref = 0;
ret = record_extent(trans, info, path, rec, back, allocated, flags);
allocated = 1;
return ret;
}
+static int fixup_extent_flags(struct btrfs_fs_info *fs_info,
+ struct extent_record *rec)
+{
+ struct btrfs_trans_handle *trans;
+ struct btrfs_root *root = fs_info->extent_root;
+ struct btrfs_path *path;
+ struct btrfs_extent_item *ei;
+ struct btrfs_key key;
+ u64 flags;
+ int ret = 0;
+
+ key.objectid = rec->start;
+ if (rec->metadata) {
+ key.type = BTRFS_METADATA_ITEM_KEY;
+ key.offset = rec->info_level;
+ } else {
+ key.type = BTRFS_EXTENT_ITEM_KEY;
+ key.offset = rec->max_size;
+ }
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ trans = btrfs_start_transaction(root, 0);
+ if (IS_ERR(trans)) {
+ btrfs_free_path(path);
+ return PTR_ERR(trans);
+ }
+
+ ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+ if (ret < 0) {
+ btrfs_free_path(path);
+ btrfs_commit_transaction(trans, root);
+ return ret;
+ } else if (ret) {
+ fprintf(stderr, "Didn't find extent for %llu\n",
+ (unsigned long long)rec->start);
+ btrfs_free_path(path);
+ btrfs_commit_transaction(trans, root);
+ return -ENOENT;
+ }
+
+ ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_extent_item);
+ flags = btrfs_extent_flags(path->nodes[0], ei);
+ if (rec->flag_block_full_backref) {
+ fprintf(stderr, "setting full backref on %llu\n",
+ (unsigned long long)key.objectid);
+ flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
+ } else {
+ fprintf(stderr, "clearing full backref on %llu\n",
+ (unsigned long long)key.objectid);
+ flags &= ~BTRFS_BLOCK_FLAG_FULL_BACKREF;
+ }
+ btrfs_set_extent_flags(path->nodes[0], ei, flags);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+ btrfs_free_path(path);
+ return btrfs_commit_transaction(trans, root);
+}
+
/* right now we only prune from the extent allocation tree */
static int prune_one_block(struct btrfs_trans_handle *trans,
struct btrfs_fs_info *info,
err = 1;
cur_err = 1;
}
+ if (rec->bad_full_backref) {
+ fprintf(stderr, "bad full backref, on [%llu]\n",
+ (unsigned long long)rec->start);
+ if (repair) {
+ ret = fixup_extent_flags(root->fs_info, rec);
+ if (ret)
+ goto repair_abort;
+ fixed = 1;
+ }
+ err = 1;
+ cur_err = 1;
+ }
+ /*
+ * Although it's not a extent ref's problem, we reuse this
+ * routine for error reporting.
+ * No repair function yet.
+ */
+ if (rec->crossing_stripes) {
+ fprintf(stderr,
+ "bad metadata [%llu, %llu) crossing stripe boundary\n",
+ rec->start, rec->start + rec->max_size);
+ err = 1;
+ cur_err = 1;
+ }
remove_cache_extent(extent_cache, cache);
free_all_extent_backrefs(rec);
}
static int add_root_item_to_list(struct list_head *head,
- u64 objectid, u64 bytenr,
+ u64 objectid, u64 bytenr, u64 last_snapshot,
u8 level, u8 drop_level,
int level_size, struct btrfs_key *drop_key)
{
ri_rec->level = level;
ri_rec->level_size = level_size;
ri_rec->drop_level = drop_level;
+ ri_rec->last_snapshot = last_snapshot;
if (drop_key)
memcpy(&ri_rec->drop_key, drop_key, sizeof(*drop_key));
list_add_tail(&ri_rec->list, head);
root1 = root->fs_info->tree_root;
level = btrfs_header_level(root1->node);
ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
- root1->node->start, level, 0,
+ root1->node->start, 0, level, 0,
btrfs_level_size(root1, level), NULL);
if (ret < 0)
goto out;
root1 = root->fs_info->chunk_root;
level = btrfs_header_level(root1->node);
ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
- root1->node->start, level, 0,
+ root1->node->start, 0, level, 0,
btrfs_level_size(root1, level), NULL);
if (ret < 0)
goto out;
btrfs_item_key_to_cpu(leaf, &found_key, path.slots[0]);
if (btrfs_key_type(&found_key) == BTRFS_ROOT_ITEM_KEY) {
unsigned long offset;
+ u64 last_snapshot;
offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
read_extent_buffer(leaf, &ri, offset, sizeof(ri));
+ last_snapshot = btrfs_root_last_snapshot(&ri);
if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
level = btrfs_root_level(&ri);
level_size = btrfs_level_size(root, level);
ret = add_root_item_to_list(&normal_trees,
found_key.objectid,
- btrfs_root_bytenr(&ri), level,
+ btrfs_root_bytenr(&ri),
+ last_snapshot, level,
0, level_size, NULL);
if (ret < 0)
goto out;
ret = add_root_item_to_list(&dropping_trees,
objectid,
btrfs_root_bytenr(&ri),
- level, ri.drop_level,
+ last_snapshot, level,
+ ri.drop_level,
level_size, &found_key);
if (ret < 0)
goto out;
return ret;
}
-static int fill_csum_tree(struct btrfs_trans_handle *trans,
- struct btrfs_root *csum_root)
+static int fill_csum_tree_from_one_fs_root(struct btrfs_trans_handle *trans,
+ struct btrfs_root *csum_root,
+ struct btrfs_root *cur_root)
+{
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ struct extent_buffer *node;
+ struct btrfs_file_extent_item *fi;
+ char *buf = NULL;
+ u64 start = 0;
+ u64 len = 0;
+ int slot = 0;
+ int ret = 0;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+ buf = malloc(cur_root->fs_info->csum_root->sectorsize);
+ if (!buf) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ key.objectid = 0;
+ key.offset = 0;
+ key.type = 0;
+
+ ret = btrfs_search_slot(NULL, cur_root, &key, path, 0, 0);
+ if (ret < 0)
+ goto out;
+ /* Iterate all regular file extents and fill its csum */
+ while (1) {
+ btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+ if (key.type != BTRFS_EXTENT_DATA_KEY)
+ goto next;
+ node = path->nodes[0];
+ slot = path->slots[0];
+ fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
+ if (btrfs_file_extent_type(node, fi) != BTRFS_FILE_EXTENT_REG)
+ goto next;
+ start = btrfs_file_extent_disk_bytenr(node, fi);
+ len = btrfs_file_extent_disk_num_bytes(node, fi);
+
+ ret = populate_csum(trans, csum_root, buf, start, len);
+ if (ret == -EEXIST)
+ ret = 0;
+ if (ret < 0)
+ goto out;
+next:
+ /*
+ * TODO: if next leaf is corrupted, jump to nearest next valid
+ * leaf.
+ */
+ ret = btrfs_next_item(cur_root, path);
+ if (ret < 0)
+ goto out;
+ if (ret > 0) {
+ ret = 0;
+ goto out;
+ }
+ }
+
+out:
+ btrfs_free_path(path);
+ free(buf);
+ return ret;
+}
+
+static int fill_csum_tree_from_fs(struct btrfs_trans_handle *trans,
+ struct btrfs_root *csum_root)
+{
+ struct btrfs_fs_info *fs_info = csum_root->fs_info;
+ struct btrfs_path *path;
+ struct btrfs_root *tree_root = fs_info->tree_root;
+ struct btrfs_root *cur_root;
+ struct extent_buffer *node;
+ struct btrfs_key key;
+ int slot = 0;
+ int ret = 0;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ key.objectid = BTRFS_FS_TREE_OBJECTID;
+ key.offset = 0;
+ key.type = BTRFS_ROOT_ITEM_KEY;
+
+ ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
+ if (ret < 0)
+ goto out;
+ if (ret > 0) {
+ ret = -ENOENT;
+ goto out;
+ }
+
+ while (1) {
+ node = path->nodes[0];
+ slot = path->slots[0];
+ btrfs_item_key_to_cpu(node, &key, slot);
+ if (key.objectid > BTRFS_LAST_FREE_OBJECTID)
+ goto out;
+ if (key.type != BTRFS_ROOT_ITEM_KEY)
+ goto next;
+ if (!is_fstree(key.objectid))
+ goto next;
+ key.offset = (u64)-1;
+
+ cur_root = btrfs_read_fs_root(fs_info, &key);
+ if (IS_ERR(cur_root) || !cur_root) {
+ fprintf(stderr, "Fail to read fs/subvol tree: %lld\n",
+ key.objectid);
+ goto out;
+ }
+ ret = fill_csum_tree_from_one_fs_root(trans, csum_root,
+ cur_root);
+ if (ret < 0)
+ goto out;
+next:
+ ret = btrfs_next_item(tree_root, path);
+ if (ret > 0) {
+ ret = 0;
+ goto out;
+ }
+ if (ret < 0)
+ goto out;
+ }
+
+out:
+ btrfs_free_path(path);
+ return ret;
+}
+
+static int fill_csum_tree_from_extent(struct btrfs_trans_handle *trans,
+ struct btrfs_root *csum_root)
{
struct btrfs_root *extent_root = csum_root->fs_info->extent_root;
struct btrfs_path *path;
return ret;
}
+/*
+ * Recalculate the csum and put it into the csum tree.
+ *
+ * Extent tree init will wipe out all the extent info, so in that case, we
+ * can't depend on extent tree, but use fs tree. If search_fs_tree is set, we
+ * will use fs/subvol trees to init the csum tree.
+ */
+static int fill_csum_tree(struct btrfs_trans_handle *trans,
+ struct btrfs_root *csum_root,
+ int search_fs_tree)
+{
+ if (search_fs_tree)
+ return fill_csum_tree_from_fs(trans, csum_root);
+ else
+ return fill_csum_tree_from_extent(trans, csum_root);
+}
+
struct root_item_info {
/* level of the root */
u8 level;
ret = 0;
out:
free_roots_info_cache();
- if (path)
- btrfs_free_path(path);
+ btrfs_free_path(path);
if (trans)
btrfs_commit_transaction(trans, info->tree_root);
if (ret < 0)
while(1) {
int c;
- int option_index = 0;
enum { OPT_REPAIR = 257, OPT_INIT_CSUM, OPT_INIT_EXTENT,
OPT_CHECK_CSUM, OPT_READONLY };
static const struct option long_options[] = {
- { "super", 1, NULL, 's' },
- { "repair", 0, NULL, OPT_REPAIR },
- { "readonly", 0, NULL, OPT_READONLY },
- { "init-csum-tree", 0, NULL, OPT_INIT_CSUM },
- { "init-extent-tree", 0, NULL, OPT_INIT_EXTENT },
- { "check-data-csum", 0, NULL, OPT_CHECK_CSUM },
- { "backup", 0, NULL, 'b' },
- { "subvol-extents", 1, NULL, 'E' },
- { "qgroup-report", 0, NULL, 'Q' },
- { "tree-root", 1, NULL, 'r' },
+ { "super", required_argument, NULL, 's' },
+ { "repair", no_argument, NULL, OPT_REPAIR },
+ { "readonly", no_argument, NULL, OPT_READONLY },
+ { "init-csum-tree", no_argument, NULL, OPT_INIT_CSUM },
+ { "init-extent-tree", no_argument, NULL, OPT_INIT_EXTENT },
+ { "check-data-csum", no_argument, NULL, OPT_CHECK_CSUM },
+ { "backup", no_argument, NULL, 'b' },
+ { "subvol-extents", required_argument, NULL, 'E' },
+ { "qgroup-report", no_argument, NULL, 'Q' },
+ { "tree-root", required_argument, NULL, 'r' },
{ NULL, 0, NULL, 0}
};
- c = getopt_long(argc, argv, "as:br:", long_options,
- &option_index);
+ c = getopt_long(argc, argv, "as:br:", long_options, NULL);
if (c < 0)
break;
switch(c) {
goto close_out;
}
- ret = fill_csum_tree(trans, info->csum_root);
+ ret = fill_csum_tree(trans, info->csum_root,
+ init_extent_tree);
if (ret) {
fprintf(stderr, "crc refilling failed\n");
return -EIO;
free_root_recs_tree(&root_cache);
close_out:
close_ctree(root);
+ btrfs_close_all_devices();
err_out:
return ret;
}