bcachefs: thread_with_file: create ops structure for thread_with_stdio

Create an ops structure so we can add more file-based functionality in
the next few patches.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Darrick J. Wong 2024-02-10 11:23:01 -08:00 committed by Kent Overstreet
parent 1cbae651e5
commit ab6752e24e
3 changed files with 28 additions and 22 deletions

View File

@ -165,6 +165,11 @@ static void bch2_fsck_offline_thread_fn(struct thread_with_stdio *stdio)
bch2_fs_stop(c); bch2_fs_stop(c);
} }
static const struct thread_with_stdio_ops bch2_offline_fsck_ops = {
.exit = bch2_fsck_thread_exit,
.fn = bch2_fsck_offline_thread_fn,
};
static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg) static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
{ {
struct bch_ioctl_fsck_offline arg; struct bch_ioctl_fsck_offline arg;
@ -217,9 +222,7 @@ static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_a
opt_set(thr->opts, stdio, (u64)(unsigned long)&thr->thr.stdio); opt_set(thr->opts, stdio, (u64)(unsigned long)&thr->thr.stdio);
ret = bch2_run_thread_with_stdio(&thr->thr, ret = bch2_run_thread_with_stdio(&thr->thr, &bch2_offline_fsck_ops);
bch2_fsck_thread_exit,
bch2_fsck_offline_thread_fn);
err: err:
if (ret < 0) { if (ret < 0) {
if (thr) if (thr)
@ -794,6 +797,11 @@ static void bch2_fsck_online_thread_fn(struct thread_with_stdio *stdio)
bch2_ro_ref_put(c); bch2_ro_ref_put(c);
} }
static const struct thread_with_stdio_ops bch2_online_fsck_ops = {
.exit = bch2_fsck_thread_exit,
.fn = bch2_fsck_online_thread_fn,
};
static long bch2_ioctl_fsck_online(struct bch_fs *c, static long bch2_ioctl_fsck_online(struct bch_fs *c,
struct bch_ioctl_fsck_online arg) struct bch_ioctl_fsck_online arg)
{ {
@ -834,9 +842,7 @@ static long bch2_ioctl_fsck_online(struct bch_fs *c,
goto err; goto err;
} }
ret = bch2_run_thread_with_stdio(&thr->thr, ret = bch2_run_thread_with_stdio(&thr->thr, &bch2_online_fsck_ops);
bch2_fsck_thread_exit,
bch2_fsck_online_thread_fn);
err: err:
if (ret < 0) { if (ret < 0) {
bch_err_fn(c, ret); bch_err_fn(c, ret);

View File

@ -155,7 +155,7 @@ static int thread_with_stdio_release(struct inode *inode, struct file *file)
bch2_thread_with_file_exit(&thr->thr); bch2_thread_with_file_exit(&thr->thr);
darray_exit(&thr->stdio.input.buf); darray_exit(&thr->stdio.input.buf);
darray_exit(&thr->stdio.output.buf); darray_exit(&thr->stdio.output.buf);
thr->exit(thr); thr->ops->exit(thr);
return 0; return 0;
} }
@ -266,32 +266,28 @@ static int thread_with_stdio_fn(void *arg)
{ {
struct thread_with_stdio *thr = arg; struct thread_with_stdio *thr = arg;
thr->fn(thr); thr->ops->fn(thr);
thread_with_stdio_done(thr); thread_with_stdio_done(thr);
return 0; return 0;
} }
int bch2_run_thread_with_stdio(struct thread_with_stdio *thr, int bch2_run_thread_with_stdio(struct thread_with_stdio *thr,
void (*exit)(struct thread_with_stdio *), const struct thread_with_stdio_ops *ops)
void (*fn)(struct thread_with_stdio *))
{ {
stdio_buf_init(&thr->stdio.input); stdio_buf_init(&thr->stdio.input);
stdio_buf_init(&thr->stdio.output); stdio_buf_init(&thr->stdio.output);
thr->exit = exit; thr->ops = ops;
thr->fn = fn;
return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, thread_with_stdio_fn); return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, thread_with_stdio_fn);
} }
int bch2_run_thread_with_stdout(struct thread_with_stdio *thr, int bch2_run_thread_with_stdout(struct thread_with_stdio *thr,
void (*exit)(struct thread_with_stdio *), const struct thread_with_stdio_ops *ops)
void (*fn)(struct thread_with_stdio *))
{ {
stdio_buf_init(&thr->stdio.input); stdio_buf_init(&thr->stdio.input);
stdio_buf_init(&thr->stdio.output); stdio_buf_init(&thr->stdio.output);
thr->exit = exit; thr->ops = ops;
thr->fn = fn;
return bch2_run_thread_with_file(&thr->thr, &thread_with_stdout_fops, thread_with_stdio_fn); return bch2_run_thread_with_file(&thr->thr, &thread_with_stdout_fops, thread_with_stdio_fn);
} }

View File

@ -49,19 +49,23 @@ int bch2_run_thread_with_file(struct thread_with_file *,
const struct file_operations *, const struct file_operations *,
int (*fn)(void *)); int (*fn)(void *));
struct thread_with_stdio;
struct thread_with_stdio_ops {
void (*exit)(struct thread_with_stdio *);
void (*fn)(struct thread_with_stdio *);
};
struct thread_with_stdio { struct thread_with_stdio {
struct thread_with_file thr; struct thread_with_file thr;
struct stdio_redirect stdio; struct stdio_redirect stdio;
void (*exit)(struct thread_with_stdio *); const struct thread_with_stdio_ops *ops;
void (*fn)(struct thread_with_stdio *);
}; };
int bch2_run_thread_with_stdio(struct thread_with_stdio *, int bch2_run_thread_with_stdio(struct thread_with_stdio *,
void (*exit)(struct thread_with_stdio *), const struct thread_with_stdio_ops *);
void (*fn)(struct thread_with_stdio *));
int bch2_run_thread_with_stdout(struct thread_with_stdio *, int bch2_run_thread_with_stdout(struct thread_with_stdio *,
void (*exit)(struct thread_with_stdio *), const struct thread_with_stdio_ops *);
void (*fn)(struct thread_with_stdio *));
int bch2_stdio_redirect_read(struct stdio_redirect *, char *, size_t); int bch2_stdio_redirect_read(struct stdio_redirect *, char *, size_t);
int bch2_stdio_redirect_readline(struct stdio_redirect *, char *, size_t); int bch2_stdio_redirect_readline(struct stdio_redirect *, char *, size_t);