#define NBYTES_ERROR (1<<11) /* INODE_ITEM nbytes count error */
#define ISIZE_ERROR (1<<12) /* INODE_ITEM size count error */
#define ORPHAN_ITEM (1<<13) /* INODE_ITEM no reference */
+#define NO_INODE_ITEM (1<<14) /* no inode_item */
#define LAST_ITEM (1<<15) /* Complete this tree traversal */
+#define ROOT_REF_MISSING (1<<16) /* ROOT_REF not found */
+#define ROOT_REF_MISMATCH (1<<17) /* ROOT_REF found but not match */
/*
* Find DIR_ITEM/DIR_INDEX for the given key and check it with the specified
return err;
}
+/*
+ * Iterate all item on the tree and call check_inode_item() to check.
+ *
+ * @root: the root of the tree to be checked.
+ * @ext_ref: the EXTENDED_IREF feature
+ *
+ * Return 0 if no error found.
+ * Return <0 for error.
+ * All internal error bitmap will be converted to -EIO, to avoid
+ * mixing negative and postive return value.
+ */
+static int check_fs_root_v2(struct btrfs_root *root, unsigned int ext_ref)
+{
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ u64 inode_id;
+ int ret, err = 0;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ key.objectid = 0;
+ key.type = 0;
+ key.offset = 0;
+
+ ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+ if (ret < 0)
+ goto out;
+
+ while (1) {
+ btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+
+ /*
+ * All check must start with inode item, skip if not
+ */
+ if (key.type == BTRFS_INODE_ITEM_KEY) {
+ ret = check_inode_item(root, path, ext_ref);
+ err |= ret;
+ if (err & LAST_ITEM)
+ goto out;
+ continue;
+ }
+ error("root %llu ITEM[%llu %u %llu] isn't INODE_ITEM, skip to next inode",
+ root->objectid, key.objectid, key.type,
+ key.offset);
+
+ err |= NO_INODE_ITEM;
+ inode_id = key.objectid;
+
+ /*
+ * skip to next inode
+ * TODO: Maybe search_slot() will be faster?
+ */
+ do {
+ ret = btrfs_next_item(root, path);
+ if (ret > 0) {
+ goto out;
+ } else if (ret < 0) {
+ err = ret;
+ goto out;
+ }
+ btrfs_item_key_to_cpu(path->nodes[0], &key,
+ path->slots[0]);
+ } while (inode_id == key.objectid);
+ }
+
+out:
+ err &= ~LAST_ITEM;
+ if (err && !ret)
+ ret = -EIO;
+ btrfs_free_path(path);
+ return ret;
+}
+
+/*
+ * Find the relative ref for root_ref and root_backref.
+ *
+ * @root: the root of the root tree.
+ * @ref_key: the key of the root ref.
+ *
+ * Return 0 if no error occurred.
+ */
+static int check_root_ref(struct btrfs_root *root, struct btrfs_key *ref_key,
+ struct extent_buffer *node, int slot)
+{
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ struct btrfs_root_ref *ref;
+ struct btrfs_root_ref *backref;
+ char ref_name[BTRFS_NAME_LEN] = {0};
+ char backref_name[BTRFS_NAME_LEN] = {0};
+ u64 ref_dirid;
+ u64 ref_seq;
+ u32 ref_namelen;
+ u64 backref_dirid;
+ u64 backref_seq;
+ u32 backref_namelen;
+ u32 len;
+ int ret;
+ int err = 0;
+
+ ref = btrfs_item_ptr(node, slot, struct btrfs_root_ref);
+ ref_dirid = btrfs_root_ref_dirid(node, ref);
+ ref_seq = btrfs_root_ref_sequence(node, ref);
+ ref_namelen = btrfs_root_ref_name_len(node, ref);
+
+ if (ref_namelen <= BTRFS_NAME_LEN) {
+ len = ref_namelen;
+ } else {
+ len = BTRFS_NAME_LEN;
+ warning("%s[%llu %llu] ref_name too long",
+ ref_key->type == BTRFS_ROOT_REF_KEY ?
+ "ROOT_REF" : "ROOT_BACKREF", ref_key->objectid,
+ ref_key->offset);
+ }
+ read_extent_buffer(node, ref_name, (unsigned long)(ref + 1), len);
+
+ /* Find relative root_ref */
+ key.objectid = ref_key->offset;
+ key.type = BTRFS_ROOT_BACKREF_KEY + BTRFS_ROOT_REF_KEY - ref_key->type;
+ key.offset = ref_key->objectid;
+
+ path = btrfs_alloc_path();
+ ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+ if (ret) {
+ err |= ROOT_REF_MISSING;
+ error("%s[%llu %llu] couldn't find relative ref",
+ ref_key->type == BTRFS_ROOT_REF_KEY ?
+ "ROOT_REF" : "ROOT_BACKREF",
+ ref_key->objectid, ref_key->offset);
+ goto out;
+ }
+
+ backref = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_root_ref);
+ backref_dirid = btrfs_root_ref_dirid(path->nodes[0], backref);
+ backref_seq = btrfs_root_ref_sequence(path->nodes[0], backref);
+ backref_namelen = btrfs_root_ref_name_len(path->nodes[0], backref);
+
+ if (backref_namelen <= BTRFS_NAME_LEN) {
+ len = backref_namelen;
+ } else {
+ len = BTRFS_NAME_LEN;
+ warning("%s[%llu %llu] ref_name too long",
+ key.type == BTRFS_ROOT_REF_KEY ?
+ "ROOT_REF" : "ROOT_BACKREF",
+ key.objectid, key.offset);
+ }
+ read_extent_buffer(path->nodes[0], backref_name,
+ (unsigned long)(backref + 1), len);
+
+ if (ref_dirid != backref_dirid || ref_seq != backref_seq ||
+ ref_namelen != backref_namelen ||
+ strncmp(ref_name, backref_name, len)) {
+ err |= ROOT_REF_MISMATCH;
+ error("%s[%llu %llu] mismatch relative ref",
+ ref_key->type == BTRFS_ROOT_REF_KEY ?
+ "ROOT_REF" : "ROOT_BACKREF",
+ ref_key->objectid, ref_key->offset);
+ }
+out:
+ btrfs_free_path(path);
+ return err;
+}
+
+/*
+ * Check all fs/file tree in low_memory mode.
+ *
+ * 1. for fs tree root item, call check_fs_root_v2()
+ * 2. for fs tree root ref/backref, call check_root_ref()
+ *
+ * Return 0 if no error occurred.
+ */
+static int check_fs_roots_v2(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_root *tree_root = fs_info->tree_root;
+ struct btrfs_root *cur_root = NULL;
+ struct btrfs_path *path;
+ struct btrfs_key key;
+ struct extent_buffer *node;
+ unsigned int ext_ref;
+ int slot;
+ int ret;
+ int err = 0;
+
+ ext_ref = btrfs_fs_incompat(fs_info,
+ BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF);
+
+ 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) {
+ err = ret;
+ goto out;
+ } else if (ret > 0) {
+ err = -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 &&
+ fs_root_objectid(key.objectid)) {
+ if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
+ cur_root = btrfs_read_fs_root_no_cache(fs_info,
+ &key);
+ } else {
+ key.offset = (u64)-1;
+ cur_root = btrfs_read_fs_root(fs_info, &key);
+ }
+
+ if (IS_ERR(cur_root)) {
+ error("Fail to read fs/subvol tree: %lld",
+ key.objectid);
+ err = -EIO;
+ goto next;
+ }
+
+ ret = check_fs_root_v2(cur_root, ext_ref);
+ err |= ret;
+
+ if (key.objectid == BTRFS_TREE_RELOC_OBJECTID)
+ btrfs_free_fs_root(cur_root);
+ } else if (key.type == BTRFS_ROOT_REF_KEY ||
+ key.type == BTRFS_ROOT_BACKREF_KEY) {
+ ret = check_root_ref(tree_root, &key, node, slot);
+ err |= ret;
+ }
+next:
+ ret = btrfs_next_item(tree_root, path);
+ if (ret > 0)
+ goto out;
+ if (ret < 0) {
+ err = ret;
+ goto out;
+ }
+ }
+
+out:
+ btrfs_free_path(path);
+ return err;
+}
+
static int all_backpointers_checked(struct extent_record *rec, int print_errs)
{
struct list_head *cur = rec->backrefs.next;
u64 chunk_root_bytenr = 0;
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
int ret;
+ int err = 0;
u64 num;
int init_csum_tree = 0;
int readonly = 0;
if((ret = check_mounted(argv[optind])) < 0) {
error("could not check mount status: %s", strerror(-ret));
+ err |= !!ret;
goto err_out;
} else if(ret) {
error("%s is currently mounted, aborting", argv[optind]);
ret = -EBUSY;
+ err |= !!ret;
goto err_out;
}
if (!info) {
error("cannot open file system");
ret = -EIO;
+ err |= !!ret;
goto err_out;
}
ret = ask_user("repair mode will force to clear out log tree, are you sure?");
if (!ret) {
ret = 1;
+ err |= !!ret;
goto close_out;
}
ret = zero_log_tree(root);
+ err |= !!ret;
if (ret) {
error("failed to zero log tree: %d", ret);
goto close_out;
printf("Print quota groups for %s\nUUID: %s\n", argv[optind],
uuidbuf);
ret = qgroup_verify_all(info);
+ err |= !!ret;
if (ret == 0)
report_qgroups(1);
goto close_out;
printf("Print extent state for subvolume %llu on %s\nUUID: %s\n",
subvolid, argv[optind], uuidbuf);
ret = print_extent_state(info, subvolid);
+ err |= !!ret;
goto close_out;
}
printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
!extent_buffer_uptodate(info->dev_root->node) ||
!extent_buffer_uptodate(info->chunk_root->node)) {
error("critical roots corrupted, unable to check the filesystem");
+ err |= !!ret;
ret = -EIO;
goto close_out;
}
if (IS_ERR(trans)) {
error("error starting transaction");
ret = PTR_ERR(trans);
+ err |= !!ret;
goto close_out;
}
if (init_extent_tree) {
printf("Creating a new extent tree\n");
ret = reinit_extent_tree(trans, info);
+ err |= !!ret;
if (ret)
goto close_out;
}
error("checksum tree initialization failed: %d",
ret);
ret = -EIO;
+ err |= !!ret;
goto close_out;
}
ret = fill_csum_tree(trans, info->csum_root,
init_extent_tree);
+ err |= !!ret;
if (ret) {
error("checksum tree refilling failed: %d", ret);
return -EIO;
* extent entries for all of the items it finds.
*/
ret = btrfs_commit_transaction(trans, info->extent_root);
+ err |= !!ret;
if (ret)
goto close_out;
}
if (!extent_buffer_uptodate(info->extent_root->node)) {
error("critical: extent_root, unable to check the filesystem");
ret = -EIO;
+ err |= !!ret;
goto close_out;
}
if (!extent_buffer_uptodate(info->csum_root->node)) {
error("critical: csum_root, unable to check the filesystem");
ret = -EIO;
+ err |= !!ret;
goto close_out;
}
ret = check_chunks_and_extents_v2(root);
else
ret = check_chunks_and_extents(root);
+ err |= !!ret;
if (ret)
error(
"errors found in extent allocation tree or chunk allocation");
ret = repair_root_items(info);
+ err |= !!ret;
if (ret < 0)
goto close_out;
if (repair) {
fprintf(stderr,
"Please run a filesystem check with the option --repair to fix them.\n");
ret = 1;
+ err |= !!ret;
goto close_out;
}
fprintf(stderr, "checking free space cache\n");
}
ret = check_space_cache(root);
+ err |= !!ret;
if (ret)
goto out;
BTRFS_FEATURE_INCOMPAT_NO_HOLES);
if (!ctx.progress_enabled)
fprintf(stderr, "checking fs roots\n");
- ret = check_fs_roots(root, &root_cache);
+ if (check_mode == CHECK_MODE_LOWMEM)
+ ret = check_fs_roots_v2(root->fs_info);
+ else
+ ret = check_fs_roots(root, &root_cache);
+ err |= !!ret;
if (ret)
goto out;
fprintf(stderr, "checking csums\n");
ret = check_csums(root);
+ err |= !!ret;
if (ret)
goto out;
fprintf(stderr, "checking root refs\n");
- ret = check_root_refs(root, &root_cache);
- if (ret)
- goto out;
+ /* For low memory mode, check_fs_roots_v2 handles root refs */
+ if (check_mode != CHECK_MODE_LOWMEM) {
+ ret = check_root_refs(root, &root_cache);
+ err |= !!ret;
+ if (ret)
+ goto out;
+ }
while (repair && !list_empty(&root->fs_info->recow_ebs)) {
struct extent_buffer *eb;
struct extent_buffer, recow);
list_del_init(&eb->recow);
ret = recow_extent_buffer(root, eb);
+ err |= !!ret;
if (ret)
break;
}
bad = list_first_entry(&delete_items, struct bad_item, list);
list_del_init(&bad->list);
- if (repair)
+ if (repair) {
ret = delete_bad_item(root, bad);
+ err |= !!ret;
+ }
free(bad);
}
if (info->quota_enabled) {
- int err;
fprintf(stderr, "checking quota groups\n");
- err = qgroup_verify_all(info);
- if (err)
+ ret = qgroup_verify_all(info);
+ err |= !!ret;
+ if (ret)
goto out;
report_qgroups(0);
- err = repair_qgroups(info, &qgroups_repaired);
+ ret = repair_qgroups(info, &qgroups_repaired);
+ err |= !!ret;
if (err)
goto out;
+ ret = 0;
}
if (!list_empty(&root->fs_info->recow_ebs)) {
error("transid errors in file system");
ret = 1;
+ err |= !!ret;
}
out:
- /* Don't override original ret */
- if (!ret && qgroups_repaired)
- ret = qgroups_repaired;
-
if (found_old_backref) { /*
* there was a disk format change when mixed
* backref was in testing tree. The old format
"The old format is not supported! *"
"\n * Please mount the FS in readonly mode, "
"backup data and re-format the FS. *\n\n");
- ret = 1;
+ err |= 1;
}
printf("found %llu bytes used err is %d\n",
(unsigned long long)bytes_used, ret);
if (ctx.progress_enabled)
task_deinit(ctx.info);
- return ret;
+ return err;
}