btrfs-progs: check: move global variables to common headers

Add declarations for global fs_info and task context so they can be
accessed from any .c file once the main.c will be split. Add prefix "g_"
for the task.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2022-09-27 19:27:18 +02:00
parent 08bb354a1c
commit f7224bdd12
5 changed files with 52 additions and 45 deletions

View File

@ -25,6 +25,8 @@
#include "kernel-shared/ctree.h" #include "kernel-shared/ctree.h"
#include "common/extent-cache.h" #include "common/extent-cache.h"
extern struct btrfs_fs_info *gfs_info;
struct block_group_record { struct block_group_record {
struct cache_extent cache; struct cache_extent cache;
/* Used to identify the orphan block groups */ /* Used to identify the orphan block groups */

View File

@ -62,6 +62,9 @@
#include "check/qgroup-verify.h" #include "check/qgroup-verify.h"
#include "ioctl.h" #include "ioctl.h"
/* Global context variables */
struct btrfs_fs_info *gfs_info;
u64 bytes_used = 0; u64 bytes_used = 0;
u64 total_csum_bytes = 0; u64 total_csum_bytes = 0;
u64 total_btree_bytes = 0; u64 total_btree_bytes = 0;
@ -76,8 +79,6 @@ int no_holes = 0;
static int is_free_space_tree = 0; static int is_free_space_tree = 0;
int init_extent_tree = 0; int init_extent_tree = 0;
int check_data_csum = 0; int check_data_csum = 0;
struct btrfs_fs_info *gfs_info;
struct task_ctx ctx = { 0 };
struct cache_tree *roots_info_cache = NULL; struct cache_tree *roots_info_cache = NULL;
enum btrfs_check_mode { enum btrfs_check_mode {
@ -3383,7 +3384,7 @@ static int check_root_refs(struct btrfs_root *root,
loop = 0; loop = 0;
cache = search_cache_extent(root_cache, 0); cache = search_cache_extent(root_cache, 0);
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
if (!cache) if (!cache)
break; break;
rec = container_of(cache, struct root_record, cache); rec = container_of(cache, struct root_record, cache);
@ -3733,7 +3734,7 @@ static int check_fs_root(struct btrfs_root *root,
} }
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
wret = walk_down_tree(root, &path, wc, &level, &nrefs); wret = walk_down_tree(root, &path, wc, &level, &nrefs);
if (wret < 0) if (wret < 0)
ret = wret; ret = wret;
@ -5857,7 +5858,7 @@ static int check_space_cache(struct btrfs_root *root)
return ret; return ret;
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
cache = btrfs_lookup_first_block_group(gfs_info, start); cache = btrfs_lookup_first_block_group(gfs_info, start);
if (!cache) if (!cache)
break; break;
@ -6187,7 +6188,7 @@ static int check_csum_root(struct btrfs_root *root)
} }
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) { if (path.slots[0] >= btrfs_header_nritems(path.nodes[0])) {
ret = btrfs_next_leaf(root, &path); ret = btrfs_next_leaf(root, &path);
if (ret < 0) { if (ret < 0) {
@ -8863,7 +8864,7 @@ static int deal_root_from_list(struct list_head *list,
* can maximize readahead. * can maximize readahead.
*/ */
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
ret = run_next_block(root, bits, bits_nr, &last, ret = run_next_block(root, bits, bits_nr, &last,
pending, seen, reada, nodes, pending, seen, reada, nodes,
extent_cache, chunk_cache, extent_cache, chunk_cache,
@ -9838,7 +9839,7 @@ static int build_roots_info_cache(void)
struct cache_extent *entry; struct cache_extent *entry;
struct root_item_info *rii; struct root_item_info *rii;
ctx.item_count++; g_task_ctx.item_count++;
if (slot >= btrfs_header_nritems(leaf)) { if (slot >= btrfs_header_nritems(leaf)) {
ret = btrfs_next_leaf(extent_root, &path); ret = btrfs_next_leaf(extent_root, &path);
if (ret < 0) { if (ret < 0) {
@ -10591,7 +10592,7 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
chunk_root_bytenr = arg_strtou64(optarg); chunk_root_bytenr = arg_strtou64(optarg);
break; break;
case 'p': case 'p':
ctx.progress_enabled = true; g_task_ctx.progress_enabled = true;
break; break;
case '?': case '?':
case 'h': case 'h':
@ -10652,9 +10653,9 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
if (check_argc_exact(argc - optind, 1)) if (check_argc_exact(argc - optind, 1))
usage(cmd); usage(cmd);
if (ctx.progress_enabled) { if (g_task_ctx.progress_enabled) {
ctx.tp = TASK_NOTHING; g_task_ctx.tp = TASK_NOTHING;
ctx.info = task_init(print_status_check, print_status_return, &ctx); g_task_ctx.info = task_init(print_status_check, print_status_return, &g_task_ctx);
} }
/* This check is the only reason for --readonly to exist */ /* This check is the only reason for --readonly to exist */
@ -10690,7 +10691,7 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
printf("Opening filesystem to check...\n"); printf("Opening filesystem to check...\n");
cache_tree_init(&root_cache); cache_tree_init(&root_cache);
qgroup_set_item_count_ptr(&ctx.item_count); qgroup_set_item_count_ptr(&g_task_ctx.item_count);
ret = check_mounted(argv[optind]); ret = check_mounted(argv[optind]);
if (!force) { if (!force) {
@ -10860,14 +10861,15 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
} }
if (!init_extent_tree) { if (!init_extent_tree) {
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
fprintf(stderr, "[1/7] checking root items\n"); fprintf(stderr, "[1/7] checking root items\n");
} else { } else {
ctx.tp = TASK_ROOT_ITEMS; g_task_ctx.tp = TASK_ROOT_ITEMS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time,
&g_task_ctx.item_count);
} }
ret = repair_root_items(); ret = repair_root_items();
task_stop(ctx.info); task_stop(g_task_ctx.info);
if (ret < 0) { if (ret < 0) {
err = !!ret; err = !!ret;
errno = -ret; errno = -ret;
@ -10898,14 +10900,14 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
fprintf(stderr, "[1/7] checking root items... skipped\n"); fprintf(stderr, "[1/7] checking root items... skipped\n");
} }
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
fprintf(stderr, "[2/7] checking extents\n"); fprintf(stderr, "[2/7] checking extents\n");
} else { } else {
ctx.tp = TASK_EXTENTS; g_task_ctx.tp = TASK_EXTENTS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
ret = do_check_chunks_and_extents(); ret = do_check_chunks_and_extents();
task_stop(ctx.info); task_stop(g_task_ctx.info);
err |= !!ret; err |= !!ret;
if (ret) if (ret)
error( error(
@ -10916,18 +10918,18 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
is_free_space_tree = btrfs_fs_compat_ro(gfs_info, FREE_SPACE_TREE); is_free_space_tree = btrfs_fs_compat_ro(gfs_info, FREE_SPACE_TREE);
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
if (is_free_space_tree) if (is_free_space_tree)
fprintf(stderr, "[3/7] checking free space tree\n"); fprintf(stderr, "[3/7] checking free space tree\n");
else else
fprintf(stderr, "[3/7] checking free space cache\n"); fprintf(stderr, "[3/7] checking free space cache\n");
} else { } else {
ctx.tp = TASK_FREE_SPACE; g_task_ctx.tp = TASK_FREE_SPACE;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
ret = validate_free_space_cache(root); ret = validate_free_space_cache(root);
task_stop(ctx.info); task_stop(g_task_ctx.info);
err |= !!ret; err |= !!ret;
/* /*
@ -10937,34 +10939,34 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
* ignore it when this happens. * ignore it when this happens.
*/ */
no_holes = btrfs_fs_incompat(gfs_info, NO_HOLES); no_holes = btrfs_fs_incompat(gfs_info, NO_HOLES);
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
fprintf(stderr, "[4/7] checking fs roots\n"); fprintf(stderr, "[4/7] checking fs roots\n");
} else { } else {
ctx.tp = TASK_FS_ROOTS; g_task_ctx.tp = TASK_FS_ROOTS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
ret = do_check_fs_roots(&root_cache); ret = do_check_fs_roots(&root_cache);
task_stop(ctx.info); task_stop(g_task_ctx.info);
err |= !!ret; err |= !!ret;
if (ret) { if (ret) {
error("errors found in fs roots"); error("errors found in fs roots");
goto out; goto out;
} }
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
if (check_data_csum) if (check_data_csum)
fprintf(stderr, "[5/7] checking csums against data\n"); fprintf(stderr, "[5/7] checking csums against data\n");
else else
fprintf(stderr, fprintf(stderr,
"[5/7] checking only csums items (without verifying data)\n"); "[5/7] checking only csums items (without verifying data)\n");
} else { } else {
ctx.tp = TASK_CSUMS; g_task_ctx.tp = TASK_CSUMS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
ret = check_csums(); ret = check_csums();
task_stop(ctx.info); task_stop(g_task_ctx.info);
/* /*
* Data csum error is not fatal, and it may indicate more serious * Data csum error is not fatal, and it may indicate more serious
* corruption, continue checking. * corruption, continue checking.
@ -10975,15 +10977,15 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
/* For low memory mode, check_fs_roots_v2 handles root refs */ /* For low memory mode, check_fs_roots_v2 handles root refs */
if (check_mode != CHECK_MODE_LOWMEM) { if (check_mode != CHECK_MODE_LOWMEM) {
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
fprintf(stderr, "[6/7] checking root refs\n"); fprintf(stderr, "[6/7] checking root refs\n");
} else { } else {
ctx.tp = TASK_ROOT_REFS; g_task_ctx.tp = TASK_ROOT_REFS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
ret = check_root_refs(root, &root_cache); ret = check_root_refs(root, &root_cache);
task_stop(ctx.info); task_stop(g_task_ctx.info);
err |= !!ret; err |= !!ret;
if (ret) { if (ret) {
error("errors found in root refs"); error("errors found in root refs");
@ -11022,14 +11024,14 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
} }
if (gfs_info->quota_enabled) { if (gfs_info->quota_enabled) {
if (!ctx.progress_enabled) { if (!g_task_ctx.progress_enabled) {
fprintf(stderr, "[7/7] checking quota groups\n"); fprintf(stderr, "[7/7] checking quota groups\n");
} else { } else {
ctx.tp = TASK_QGROUPS; g_task_ctx.tp = TASK_QGROUPS;
task_start(ctx.info, &ctx.start_time, &ctx.item_count); task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
} }
qgroup_verify_ret = qgroup_verify_all(gfs_info); qgroup_verify_ret = qgroup_verify_all(gfs_info);
task_stop(ctx.info); task_stop(g_task_ctx.info);
if (qgroup_verify_ret < 0) { if (qgroup_verify_ret < 0) {
error("failed to check quota groups"); error("failed to check quota groups");
err |= !!qgroup_verify_ret; err |= !!qgroup_verify_ret;
@ -11079,8 +11081,8 @@ out:
close_out: close_out:
close_ctree(root); close_ctree(root);
err_out: err_out:
if (ctx.progress_enabled) if (g_task_ctx.progress_enabled)
task_deinit(ctx.info); task_deinit(g_task_ctx.info);
return err; return err;
} }

View File

@ -33,6 +33,8 @@
#include "common/repair.h" #include "common/repair.h"
#include "check/mode-common.h" #include "check/mode-common.h"
struct task_ctx g_task_ctx = { 0 };
/* /*
* Check if the inode referenced by the given data reference uses the extent * Check if the inode referenced by the given data reference uses the extent
* at disk_bytenr as a non-prealloc extent. * at disk_bytenr as a non-prealloc extent.

View File

@ -31,6 +31,8 @@
struct btrfs_trans_handle; struct btrfs_trans_handle;
struct extent_buffer; struct extent_buffer;
extern struct task_ctx g_task_ctx;
#define FREE_SPACE_CACHE_INODE_MODE (0100600) #define FREE_SPACE_CACHE_INODE_MODE (0100600)
/* /*
* Use for tree walk to walk through trees whose leaves/nodes can be shared * Use for tree walk to walk through trees whose leaves/nodes can be shared
@ -80,7 +82,6 @@ extern int no_holes;
extern int init_extent_tree; extern int init_extent_tree;
extern int check_data_csum; extern int check_data_csum;
extern struct btrfs_fs_info *gfs_info; extern struct btrfs_fs_info *gfs_info;
extern struct task_ctx ctx;
extern struct cache_tree *roots_info_cache; extern struct cache_tree *roots_info_cache;
static inline u8 imode_to_type(u32 imode) static inline u8 imode_to_type(u32 imode)

View File

@ -5305,7 +5305,7 @@ static int check_btrfs_root(struct btrfs_root *root, int check_all)
} }
while (1) { while (1) {
ctx.item_count++; g_task_ctx.item_count++;
ret = walk_down_tree(root, &path, &level, &nrefs, check_all); ret = walk_down_tree(root, &path, &level, &nrefs, check_all);
if (ret > 0) if (ret > 0)