bcachefs: bch2_stdio_redirect_readline_timeout()

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-05-29 20:37:39 -04:00
parent 0c97c437e3
commit d37dd9b604
2 changed files with 23 additions and 3 deletions

View File

@ -364,13 +364,21 @@ int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *ubuf, size_t le
return ret; return ret;
} }
int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, darray_char *line) int bch2_stdio_redirect_readline_timeout(struct stdio_redirect *stdio,
darray_char *line,
unsigned long timeout)
{ {
unsigned long until = jiffies + timeout, t;
struct stdio_buf *buf = &stdio->input; struct stdio_buf *buf = &stdio->input;
size_t seen = 0; size_t seen = 0;
again: again:
wait_event_timeout(buf->wait, stdio_redirect_has_more_input(stdio, seen), t = timeout != MAX_SCHEDULE_TIMEOUT
sysctl_hung_task_timeout_secs * HZ / 2); ? max_t(long, until - jiffies, 0)
: timeout;
t = min(t, sysctl_hung_task_timeout_secs * HZ / 2);
wait_event_timeout(buf->wait, stdio_redirect_has_more_input(stdio, seen), t);
if (stdio->done) if (stdio->done)
return -1; return -1;
@ -378,6 +386,12 @@ again:
spin_lock(&buf->lock); spin_lock(&buf->lock);
seen = buf->buf.nr; seen = buf->buf.nr;
char *n = memchr(buf->buf.data, '\n', seen); char *n = memchr(buf->buf.data, '\n', seen);
if (!n && timeout != MAX_SCHEDULE_TIMEOUT && jiffies >= until) {
spin_unlock(&buf->lock);
return -ETIME;
}
if (!n) { if (!n) {
buf->waiting_for_line = true; buf->waiting_for_line = true;
spin_unlock(&buf->lock); spin_unlock(&buf->lock);
@ -408,6 +422,11 @@ again:
return 0; return 0;
} }
int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, darray_char *line)
{
return bch2_stdio_redirect_readline_timeout(stdio, line, MAX_SCHEDULE_TIMEOUT);
}
__printf(3, 0) __printf(3, 0)
static ssize_t bch2_darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args) static ssize_t bch2_darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args)
{ {

View File

@ -72,6 +72,7 @@ int bch2_run_thread_with_stdout(struct thread_with_stdio *,
const struct thread_with_stdio_ops *); const struct thread_with_stdio_ops *);
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_timeout(struct stdio_redirect *, darray_char *, unsigned long);
int bch2_stdio_redirect_readline(struct stdio_redirect *, darray_char *); int bch2_stdio_redirect_readline(struct stdio_redirect *, darray_char *);
__printf(3, 0) ssize_t bch2_stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list); __printf(3, 0) ssize_t bch2_stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list);