diff --git a/check/mode-common.c b/check/mode-common.c index 466e7a8d..95dc32d8 100644 --- a/check/mode-common.c +++ b/check/mode-common.c @@ -21,6 +21,7 @@ #include "transaction.h" #include "utils.h" #include "disk-io.h" +#include "repair.h" #include "check/mode-common.h" /* @@ -889,3 +890,37 @@ abort: btrfs_abort_transaction(trans, ret); return ret; } + +/* + * For free space inodes, we can't call check_inode_item() as free space + * cache inode doesn't have INODE_REF. + * We just check its inode mode. + */ +int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, + struct btrfs_path *path) +{ + struct btrfs_inode_item *iitem; + struct btrfs_key key; + u32 mode; + int ret = 0; + + btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); + ASSERT(key.type == BTRFS_INODE_ITEM_KEY && is_fstree(key.objectid)); + iitem = btrfs_item_ptr(path->nodes[0], path->slots[0], + struct btrfs_inode_item); + mode = btrfs_inode_mode(path->nodes[0], iitem); + if (mode != FREE_SPACE_CACHE_INODE_MODE) { + error( + "free space cache inode %llu has invalid mode: has 0%o expect 0%o", + key.objectid, mode, FREE_SPACE_CACHE_INODE_MODE); + ret = -EUCLEAN; + if (repair) { + ret = repair_imode_common(fs_info->tree_root, + path); + if (ret < 0) + return ret; + return ret; + } + } + return ret; +} diff --git a/check/mode-common.h b/check/mode-common.h index 5aaf3aaa..4c169c6e 100644 --- a/check/mode-common.h +++ b/check/mode-common.h @@ -24,6 +24,7 @@ #include #include "ctree.h" +#define FREE_SPACE_CACHE_INODE_MODE (0100600) /* * Use for tree walk to walk through trees whose leaves/nodes can be shared * between different trees. (Namely subvolume/fs trees) @@ -128,7 +129,8 @@ int delete_corrupted_dir_item(struct btrfs_trans_handle *trans, int reset_imode(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u64 ino, u32 mode); int repair_imode_common(struct btrfs_root *root, struct btrfs_path *path); - +int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, + struct btrfs_path *path); /* * Check if the inode mode @imode is valid * diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c index 94856b4c..6d7ae2bc 100644 --- a/check/mode-lowmem.c +++ b/check/mode-lowmem.c @@ -5145,6 +5145,17 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info) btrfs_item_key_to_cpu(node, &key, slot); if (key.objectid > BTRFS_LAST_FREE_OBJECTID) goto out; + if (key.type == BTRFS_INODE_ITEM_KEY && + is_fstree(key.objectid)) { + ret = check_repair_free_space_inode(fs_info, &path); + /* Check if we still have a valid path to continue */ + if (ret < 0 && path.nodes[0]) { + err |= ret; + goto next; + } + if (ret < 0 && !path.nodes[0]) + goto out; + } if (key.type == BTRFS_ROOT_ITEM_KEY && fs_root_objectid(key.objectid)) { if (key.objectid == BTRFS_TREE_RELOC_OBJECTID) { diff --git a/check/mode-lowmem.h b/check/mode-lowmem.h index e0ab30b7..d2983fd1 100644 --- a/check/mode-lowmem.h +++ b/check/mode-lowmem.h @@ -67,5 +67,7 @@ int check_fs_roots_lowmem(struct btrfs_fs_info *fs_info); int check_chunks_and_extents_lowmem(struct btrfs_fs_info *fs_info); +int check_repair_free_space_inode(struct btrfs_fs_info *fs_info, + struct btrfs_path *path); #endif