2023-12-31 15:04:54 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2024-01-04 02:46:50 +00:00
|
|
|
#ifndef NO_BCACHEFS_FS
|
2023-12-31 15:04:54 +00:00
|
|
|
|
|
|
|
#include "bcachefs.h"
|
|
|
|
#include "thread_with_file.h"
|
|
|
|
|
|
|
|
#include <linux/anon_inodes.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/kthread.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/poll.h>
|
2024-02-09 01:41:34 +00:00
|
|
|
#include <linux/sched/sysctl.h>
|
2023-12-31 15:04:54 +00:00
|
|
|
|
|
|
|
void bch2_thread_with_file_exit(struct thread_with_file *thr)
|
|
|
|
{
|
|
|
|
if (thr->task) {
|
|
|
|
kthread_stop(thr->task);
|
|
|
|
put_task_struct(thr->task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int bch2_run_thread_with_file(struct thread_with_file *thr,
|
|
|
|
const struct file_operations *fops,
|
|
|
|
int (*fn)(void *))
|
|
|
|
{
|
|
|
|
struct file *file = NULL;
|
|
|
|
int ret, fd = -1;
|
|
|
|
unsigned fd_flags = O_CLOEXEC;
|
|
|
|
|
|
|
|
if (fops->read && fops->write)
|
|
|
|
fd_flags |= O_RDWR;
|
|
|
|
else if (fops->read)
|
|
|
|
fd_flags |= O_RDONLY;
|
|
|
|
else if (fops->write)
|
|
|
|
fd_flags |= O_WRONLY;
|
|
|
|
|
|
|
|
char name[TASK_COMM_LEN];
|
|
|
|
get_task_comm(name, current);
|
|
|
|
|
|
|
|
thr->ret = 0;
|
|
|
|
thr->task = kthread_create(fn, thr, "%s", name);
|
|
|
|
ret = PTR_ERR_OR_ZERO(thr->task);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = get_unused_fd_flags(fd_flags);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
fd = ret;
|
|
|
|
|
|
|
|
file = anon_inode_getfile(name, fops, thr, fd_flags);
|
|
|
|
ret = PTR_ERR_OR_ZERO(file);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
get_task_struct(thr->task);
|
|
|
|
wake_up_process(thr->task);
|
2024-02-04 07:51:52 +00:00
|
|
|
fd_install(fd, file);
|
2023-12-31 15:04:54 +00:00
|
|
|
return fd;
|
|
|
|
err:
|
|
|
|
if (fd >= 0)
|
|
|
|
put_unused_fd(fd);
|
|
|
|
if (thr->task)
|
|
|
|
kthread_stop(thr->task);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
/* stdio_redirect */
|
|
|
|
|
|
|
|
static bool stdio_redirect_has_input(struct stdio_redirect *stdio)
|
|
|
|
{
|
|
|
|
return stdio->input.buf.nr || stdio->done;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool stdio_redirect_has_output(struct stdio_redirect *stdio)
|
2023-12-31 15:04:54 +00:00
|
|
|
{
|
2024-02-05 03:20:40 +00:00
|
|
|
return stdio->output.buf.nr || stdio->done;
|
2023-12-31 15:04:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 20:43:16 +00:00
|
|
|
#define STDIO_REDIRECT_BUFSIZE 4096
|
2024-02-05 03:20:40 +00:00
|
|
|
|
|
|
|
static bool stdio_redirect_has_input_space(struct stdio_redirect *stdio)
|
|
|
|
{
|
2024-02-03 20:43:16 +00:00
|
|
|
return stdio->input.buf.nr < STDIO_REDIRECT_BUFSIZE || stdio->done;
|
2024-02-05 03:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool stdio_redirect_has_output_space(struct stdio_redirect *stdio)
|
|
|
|
{
|
2024-02-03 20:43:16 +00:00
|
|
|
return stdio->output.buf.nr < STDIO_REDIRECT_BUFSIZE || stdio->done;
|
2024-02-05 03:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void stdio_buf_init(struct stdio_buf *buf)
|
|
|
|
{
|
|
|
|
spin_lock_init(&buf->lock);
|
|
|
|
init_waitqueue_head(&buf->wait);
|
|
|
|
darray_init(&buf->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* thread_with_stdio */
|
|
|
|
|
2024-02-09 01:27:06 +00:00
|
|
|
static void thread_with_stdio_done(struct thread_with_stdio *thr)
|
|
|
|
{
|
|
|
|
thr->thr.done = true;
|
|
|
|
thr->stdio.done = true;
|
|
|
|
wake_up(&thr->stdio.input.wait);
|
|
|
|
wake_up(&thr->stdio.output.wait);
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:19:49 +00:00
|
|
|
static ssize_t thread_with_stdio_read(struct file *file, char __user *ubuf,
|
2023-12-31 15:04:54 +00:00
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
2024-02-05 03:20:40 +00:00
|
|
|
struct stdio_buf *buf = &thr->stdio.output;
|
2023-12-31 15:04:54 +00:00
|
|
|
size_t copied = 0, b;
|
|
|
|
int ret = 0;
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
if (!(file->f_flags & O_NONBLOCK)) {
|
|
|
|
ret = wait_event_interruptible(buf->wait, stdio_redirect_has_output(&thr->stdio));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else if (!stdio_redirect_has_output(&thr->stdio))
|
2023-12-31 15:04:54 +00:00
|
|
|
return -EAGAIN;
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
while (len && buf->buf.nr) {
|
2024-02-05 01:19:49 +00:00
|
|
|
if (fault_in_writeable(ubuf, len) == len) {
|
2023-12-31 15:04:54 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_lock_irq(&buf->lock);
|
|
|
|
b = min_t(size_t, len, buf->buf.nr);
|
2024-02-05 01:19:49 +00:00
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
if (b && !copy_to_user_nofault(ubuf, buf->buf.data, b)) {
|
2024-02-05 01:19:49 +00:00
|
|
|
ubuf += b;
|
|
|
|
len -= b;
|
|
|
|
copied += b;
|
2024-02-05 03:20:40 +00:00
|
|
|
buf->buf.nr -= b;
|
|
|
|
memmove(buf->buf.data,
|
|
|
|
buf->buf.data + b,
|
|
|
|
buf->buf.nr);
|
2024-02-05 01:19:49 +00:00
|
|
|
}
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_unlock_irq(&buf->lock);
|
2023-12-31 15:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return copied ?: ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_with_stdio_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
|
|
|
|
2024-02-09 01:27:06 +00:00
|
|
|
thread_with_stdio_done(thr);
|
2023-12-31 15:04:54 +00:00
|
|
|
bch2_thread_with_file_exit(&thr->thr);
|
2024-02-05 03:20:40 +00:00
|
|
|
darray_exit(&thr->stdio.input.buf);
|
|
|
|
darray_exit(&thr->stdio.output.buf);
|
2024-02-10 19:23:01 +00:00
|
|
|
thr->ops->exit(thr);
|
2023-12-31 15:04:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t thread_with_stdio_write(struct file *file, const char __user *ubuf,
|
|
|
|
size_t len, loff_t *ppos)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
2024-02-05 03:20:40 +00:00
|
|
|
struct stdio_buf *buf = &thr->stdio.input;
|
2023-12-31 15:04:54 +00:00
|
|
|
size_t copied = 0;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
if (thr->thr.done) {
|
|
|
|
ret = -EPIPE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t b = len - fault_in_readable(ubuf, len);
|
|
|
|
if (!b) {
|
|
|
|
ret = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_lock(&buf->lock);
|
2024-02-03 20:43:16 +00:00
|
|
|
if (buf->buf.nr < STDIO_REDIRECT_BUFSIZE)
|
|
|
|
darray_make_room_gfp(&buf->buf,
|
|
|
|
min(b, STDIO_REDIRECT_BUFSIZE - buf->buf.nr), GFP_NOWAIT);
|
2024-02-05 03:20:40 +00:00
|
|
|
b = min(len, darray_room(buf->buf));
|
2023-12-31 15:04:54 +00:00
|
|
|
|
2024-02-03 20:43:16 +00:00
|
|
|
if (b && !copy_from_user_nofault(&darray_top(buf->buf), ubuf, b)) {
|
2024-02-05 03:20:40 +00:00
|
|
|
buf->buf.nr += b;
|
|
|
|
ubuf += b;
|
|
|
|
len -= b;
|
|
|
|
copied += b;
|
2023-12-31 15:04:54 +00:00
|
|
|
}
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_unlock(&buf->lock);
|
2023-12-31 15:04:54 +00:00
|
|
|
|
|
|
|
if (b) {
|
2024-02-05 03:20:40 +00:00
|
|
|
wake_up(&buf->wait);
|
2023-12-31 15:04:54 +00:00
|
|
|
} else {
|
|
|
|
if ((file->f_flags & O_NONBLOCK)) {
|
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
ret = wait_event_interruptible(buf->wait,
|
|
|
|
stdio_redirect_has_input_space(&thr->stdio));
|
2023-12-31 15:04:54 +00:00
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return copied ?: ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __poll_t thread_with_stdio_poll(struct file *file, struct poll_table_struct *wait)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
poll_wait(file, &thr->stdio.output.wait, wait);
|
|
|
|
poll_wait(file, &thr->stdio.input.wait, wait);
|
2023-12-31 15:04:54 +00:00
|
|
|
|
|
|
|
__poll_t mask = 0;
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
if (stdio_redirect_has_output(&thr->stdio))
|
2023-12-31 15:04:54 +00:00
|
|
|
mask |= EPOLLIN;
|
2024-02-05 03:20:40 +00:00
|
|
|
if (stdio_redirect_has_input_space(&thr->stdio))
|
2023-12-31 15:04:54 +00:00
|
|
|
mask |= EPOLLOUT;
|
|
|
|
if (thr->thr.done)
|
|
|
|
mask |= EPOLLHUP|EPOLLERR;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2024-02-07 19:43:32 +00:00
|
|
|
static __poll_t thread_with_stdout_poll(struct file *file, struct poll_table_struct *wait)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
|
|
|
|
|
|
|
poll_wait(file, &thr->stdio.output.wait, wait);
|
|
|
|
|
|
|
|
__poll_t mask = 0;
|
|
|
|
|
|
|
|
if (stdio_redirect_has_output(&thr->stdio))
|
|
|
|
mask |= EPOLLIN;
|
|
|
|
if (thr->thr.done)
|
|
|
|
mask |= EPOLLHUP|EPOLLERR;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2024-02-18 01:49:11 +00:00
|
|
|
static int thread_with_stdio_flush(struct file *file, fl_owner_t id)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
|
|
|
|
|
|
|
return thr->thr.ret;
|
|
|
|
}
|
|
|
|
|
2024-02-10 19:32:20 +00:00
|
|
|
static long thread_with_stdio_ioctl(struct file *file, unsigned int cmd, unsigned long p)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr =
|
|
|
|
container_of(file->private_data, struct thread_with_stdio, thr);
|
|
|
|
|
|
|
|
if (thr->ops->unlocked_ioctl)
|
|
|
|
return thr->ops->unlocked_ioctl(thr, cmd, p);
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
2023-12-31 15:04:54 +00:00
|
|
|
static const struct file_operations thread_with_stdio_fops = {
|
2024-02-05 03:20:40 +00:00
|
|
|
.llseek = no_llseek,
|
2023-12-31 15:04:54 +00:00
|
|
|
.read = thread_with_stdio_read,
|
|
|
|
.write = thread_with_stdio_write,
|
|
|
|
.poll = thread_with_stdio_poll,
|
2024-02-18 01:49:11 +00:00
|
|
|
.flush = thread_with_stdio_flush,
|
2024-02-05 03:20:40 +00:00
|
|
|
.release = thread_with_stdio_release,
|
2024-02-10 19:32:20 +00:00
|
|
|
.unlocked_ioctl = thread_with_stdio_ioctl,
|
2023-12-31 15:04:54 +00:00
|
|
|
};
|
|
|
|
|
2024-02-07 19:43:32 +00:00
|
|
|
static const struct file_operations thread_with_stdout_fops = {
|
|
|
|
.llseek = no_llseek,
|
|
|
|
.read = thread_with_stdio_read,
|
|
|
|
.poll = thread_with_stdout_poll,
|
2024-02-18 01:49:11 +00:00
|
|
|
.flush = thread_with_stdio_flush,
|
2024-02-07 19:43:32 +00:00
|
|
|
.release = thread_with_stdio_release,
|
2024-02-10 19:32:20 +00:00
|
|
|
.unlocked_ioctl = thread_with_stdio_ioctl,
|
2024-02-07 19:43:32 +00:00
|
|
|
};
|
|
|
|
|
2024-02-05 03:49:34 +00:00
|
|
|
static int thread_with_stdio_fn(void *arg)
|
|
|
|
{
|
|
|
|
struct thread_with_stdio *thr = arg;
|
|
|
|
|
2024-02-18 01:49:11 +00:00
|
|
|
thr->thr.ret = thr->ops->fn(thr);
|
2024-02-05 03:49:34 +00:00
|
|
|
|
2024-02-09 01:27:06 +00:00
|
|
|
thread_with_stdio_done(thr);
|
2024-02-05 03:49:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-31 15:04:54 +00:00
|
|
|
int bch2_run_thread_with_stdio(struct thread_with_stdio *thr,
|
2024-02-10 19:23:01 +00:00
|
|
|
const struct thread_with_stdio_ops *ops)
|
2023-12-31 15:04:54 +00:00
|
|
|
{
|
2024-02-05 03:20:40 +00:00
|
|
|
stdio_buf_init(&thr->stdio.input);
|
|
|
|
stdio_buf_init(&thr->stdio.output);
|
2024-02-10 19:23:01 +00:00
|
|
|
thr->ops = ops;
|
2023-12-31 15:04:54 +00:00
|
|
|
|
2024-02-05 03:49:34 +00:00
|
|
|
return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, thread_with_stdio_fn);
|
2023-12-31 15:04:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 19:43:32 +00:00
|
|
|
int bch2_run_thread_with_stdout(struct thread_with_stdio *thr,
|
2024-02-10 19:23:01 +00:00
|
|
|
const struct thread_with_stdio_ops *ops)
|
2024-02-07 19:43:32 +00:00
|
|
|
{
|
|
|
|
stdio_buf_init(&thr->stdio.input);
|
|
|
|
stdio_buf_init(&thr->stdio.output);
|
2024-02-10 19:23:01 +00:00
|
|
|
thr->ops = ops;
|
2024-02-07 19:43:32 +00:00
|
|
|
|
|
|
|
return bch2_run_thread_with_file(&thr->thr, &thread_with_stdout_fops, thread_with_stdio_fn);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(bch2_run_thread_with_stdout);
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *ubuf, size_t len)
|
2023-12-31 15:04:54 +00:00
|
|
|
{
|
2024-02-05 03:20:40 +00:00
|
|
|
struct stdio_buf *buf = &stdio->input;
|
2023-12-31 15:04:54 +00:00
|
|
|
|
2024-02-09 01:41:34 +00:00
|
|
|
/*
|
|
|
|
* we're waiting on user input (or for the file descriptor to be
|
|
|
|
* closed), don't want a hung task warning:
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
wait_event_timeout(buf->wait, stdio_redirect_has_input(stdio),
|
|
|
|
sysctl_hung_task_timeout_secs * HZ / 2);
|
|
|
|
} while (!stdio_redirect_has_input(stdio));
|
|
|
|
|
2023-12-31 15:04:54 +00:00
|
|
|
if (stdio->done)
|
|
|
|
return -1;
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_lock(&buf->lock);
|
|
|
|
int ret = min(len, buf->buf.nr);
|
|
|
|
buf->buf.nr -= ret;
|
|
|
|
memcpy(ubuf, buf->buf.data, ret);
|
|
|
|
memmove(buf->buf.data,
|
|
|
|
buf->buf.data + ret,
|
|
|
|
buf->buf.nr);
|
|
|
|
spin_unlock(&buf->lock);
|
2023-12-31 15:04:54 +00:00
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
wake_up(&buf->wait);
|
2023-12-31 15:04:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, char *ubuf, size_t len)
|
2023-12-31 15:04:54 +00:00
|
|
|
{
|
2024-02-05 03:20:40 +00:00
|
|
|
struct stdio_buf *buf = &stdio->input;
|
2024-02-05 03:56:16 +00:00
|
|
|
size_t copied = 0;
|
|
|
|
ssize_t ret = 0;
|
|
|
|
again:
|
2024-02-09 01:41:34 +00:00
|
|
|
do {
|
|
|
|
wait_event_timeout(buf->wait, stdio_redirect_has_input(stdio),
|
|
|
|
sysctl_hung_task_timeout_secs * HZ / 2);
|
|
|
|
} while (!stdio_redirect_has_input(stdio));
|
|
|
|
|
2024-02-05 03:56:16 +00:00
|
|
|
if (stdio->done) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2023-12-31 15:04:54 +00:00
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_lock(&buf->lock);
|
2024-02-05 03:56:16 +00:00
|
|
|
size_t b = min(len, buf->buf.nr);
|
|
|
|
char *n = memchr(buf->buf.data, '\n', b);
|
|
|
|
if (n)
|
|
|
|
b = min_t(size_t, b, n + 1 - buf->buf.data);
|
|
|
|
buf->buf.nr -= b;
|
|
|
|
memcpy(ubuf, buf->buf.data, b);
|
2024-02-05 03:20:40 +00:00
|
|
|
memmove(buf->buf.data,
|
2024-02-05 03:56:16 +00:00
|
|
|
buf->buf.data + b,
|
2024-02-05 03:20:40 +00:00
|
|
|
buf->buf.nr);
|
2024-02-05 03:56:16 +00:00
|
|
|
ubuf += b;
|
|
|
|
len -= b;
|
|
|
|
copied += b;
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_unlock(&buf->lock);
|
|
|
|
|
|
|
|
wake_up(&buf->wait);
|
2024-02-05 03:56:16 +00:00
|
|
|
|
|
|
|
if (!n && len)
|
|
|
|
goto again;
|
|
|
|
out:
|
|
|
|
return copied ?: ret;
|
2023-12-31 15:04:54 +00:00
|
|
|
}
|
2024-01-04 02:46:50 +00:00
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
__printf(3, 0)
|
2024-02-07 19:39:03 +00:00
|
|
|
static ssize_t bch2_darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args)
|
2024-02-05 03:20:40 +00:00
|
|
|
{
|
2024-02-07 19:39:03 +00:00
|
|
|
ssize_t ret;
|
2024-02-05 03:20:40 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
va_list args2;
|
2024-02-07 19:39:03 +00:00
|
|
|
size_t len;
|
2024-02-05 03:20:40 +00:00
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
va_copy(args2, args);
|
2024-02-05 03:20:40 +00:00
|
|
|
len = vsnprintf(out->data + out->nr, darray_room(*out), fmt, args2);
|
2024-02-14 01:26:09 +00:00
|
|
|
va_end(args2);
|
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
if (len + 1 <= darray_room(*out)) {
|
|
|
|
out->nr += len;
|
|
|
|
return len;
|
|
|
|
}
|
2024-02-05 03:20:40 +00:00
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
ret = darray_make_room_gfp(out, len + 1, gfp);
|
|
|
|
} while (ret == 0);
|
|
|
|
|
|
|
|
return ret;
|
2024-02-05 03:20:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
ssize_t bch2_stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking,
|
|
|
|
const char *fmt, va_list args)
|
2024-02-05 03:20:40 +00:00
|
|
|
{
|
|
|
|
struct stdio_buf *buf = &stdio->output;
|
|
|
|
unsigned long flags;
|
2024-02-07 19:39:03 +00:00
|
|
|
ssize_t ret;
|
2024-02-05 03:20:40 +00:00
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
again:
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_lock_irqsave(&buf->lock, flags);
|
2024-02-07 19:39:03 +00:00
|
|
|
ret = bch2_darray_vprintf(&buf->buf, GFP_NOWAIT, fmt, args);
|
2024-02-05 03:20:40 +00:00
|
|
|
spin_unlock_irqrestore(&buf->lock, flags);
|
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
if (nonblocking)
|
|
|
|
return -EAGAIN;
|
|
|
|
|
|
|
|
ret = wait_event_interruptible(buf->wait,
|
|
|
|
stdio_redirect_has_output_space(stdio));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
wake_up(&buf->wait);
|
2024-02-07 19:39:03 +00:00
|
|
|
return ret;
|
2024-02-05 03:20:40 +00:00
|
|
|
}
|
|
|
|
|
2024-02-07 19:39:03 +00:00
|
|
|
ssize_t bch2_stdio_redirect_printf(struct stdio_redirect *stdio, bool nonblocking,
|
2024-02-05 03:20:40 +00:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
2024-02-07 19:39:03 +00:00
|
|
|
ssize_t ret;
|
|
|
|
|
2024-02-05 03:20:40 +00:00
|
|
|
va_start(args, fmt);
|
2024-02-07 19:39:03 +00:00
|
|
|
ret = bch2_stdio_redirect_vprintf(stdio, nonblocking, fmt, args);
|
2024-02-05 03:20:40 +00:00
|
|
|
va_end(args);
|
2024-02-07 19:39:03 +00:00
|
|
|
|
|
|
|
return ret;
|
2024-02-05 03:20:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-04 02:46:50 +00:00
|
|
|
#endif /* NO_BCACHEFS_FS */
|