]> www.infradead.org Git - nvme.git/commitdiff
bcachefs: bch2_stdio_redirect_readline_timeout()
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 30 May 2024 00:37:39 +0000 (20:37 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 14 Jul 2024 23:00:14 +0000 (19:00 -0400)
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/thread_with_file.c
fs/bcachefs/thread_with_file.h

index 080afa7eff25c306ed1f75d74b24f135a156d770..0807ce9b171acaad4030dbf2b37b41a3fa44e4e4 100644 (file)
@@ -364,13 +364,21 @@ int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *ubuf, size_t le
        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;
        size_t seen = 0;
 again:
-       wait_event_timeout(buf->wait, stdio_redirect_has_more_input(stdio, seen),
-                          sysctl_hung_task_timeout_secs * HZ / 2);
+       t = timeout != MAX_SCHEDULE_TIMEOUT
+               ? 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)
                return -1;
@@ -378,6 +386,12 @@ again:
        spin_lock(&buf->lock);
        seen = buf->buf.nr;
        char *n = memchr(buf->buf.data, '\n', seen);
+
+       if (!n && timeout != MAX_SCHEDULE_TIMEOUT && jiffies >= until) {
+               spin_unlock(&buf->lock);
+               return -ETIME;
+       }
+
        if (!n) {
                buf->waiting_for_line = true;
                spin_unlock(&buf->lock);
@@ -408,6 +422,11 @@ again:
        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)
 static ssize_t bch2_darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args)
 {
index e415dc2e2fb1fabed4a1dc0fe98838374bcdeac5..72497b9219113bb2dc9dc9e871d617029003605d 100644 (file)
@@ -72,6 +72,7 @@ int bch2_run_thread_with_stdout(struct thread_with_stdio *,
                                const struct thread_with_stdio_ops *);
 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 *);
 
 __printf(3, 0) ssize_t bch2_stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list);