1 // SPDX-License-Identifier: GPL-2.0
6 #include "thread_with_file.h"
8 #include <linux/anon_inodes.h>
9 #include <linux/file.h>
10 #include <linux/kthread.h>
11 #include <linux/pagemap.h>
12 #include <linux/poll.h>
14 void bch2_thread_with_file_exit(struct thread_with_file *thr)
17 kthread_stop(thr->task);
18 put_task_struct(thr->task);
22 int bch2_run_thread_with_file(struct thread_with_file *thr,
23 const struct file_operations *fops,
26 struct file *file = NULL;
28 unsigned fd_flags = O_CLOEXEC;
30 if (fops->read && fops->write)
37 char name[TASK_COMM_LEN];
38 get_task_comm(name, current);
41 thr->task = kthread_create(fn, thr, "%s", name);
42 ret = PTR_ERR_OR_ZERO(thr->task);
46 ret = get_unused_fd_flags(fd_flags);
51 file = anon_inode_getfile(name, fops, thr, fd_flags);
52 ret = PTR_ERR_OR_ZERO(file);
57 get_task_struct(thr->task);
58 wake_up_process(thr->task);
64 kthread_stop(thr->task);
68 static inline bool thread_with_stdio_has_output(struct thread_with_stdio *thr)
70 return thr->stdio.output_buf.pos ||
75 static ssize_t thread_with_stdio_read(struct file *file, char __user *buf,
76 size_t len, loff_t *ppos)
78 struct thread_with_stdio *thr =
79 container_of(file->private_data, struct thread_with_stdio, thr);
83 if ((file->f_flags & O_NONBLOCK) &&
84 !thread_with_stdio_has_output(thr))
87 ret = wait_event_interruptible(thr->stdio.output_wait,
88 thread_with_stdio_has_output(thr));
96 ret = darray_make_room(&thr->output2, thr->stdio.output_buf.pos);
100 spin_lock_irq(&thr->stdio.output_lock);
101 b = min_t(size_t, darray_room(thr->output2), thr->stdio.output_buf.pos);
103 memcpy(&darray_top(thr->output2), thr->stdio.output_buf.buf, b);
104 memmove(thr->stdio.output_buf.buf,
105 thr->stdio.output_buf.buf + b,
106 thr->stdio.output_buf.pos - b);
108 thr->output2.nr += b;
109 thr->stdio.output_buf.pos -= b;
110 spin_unlock_irq(&thr->stdio.output_lock);
112 b = min(len, thr->output2.nr);
116 b -= copy_to_user(buf, thr->output2.data, b);
126 memmove(thr->output2.data,
127 thr->output2.data + b,
128 thr->output2.nr - b);
129 thr->output2.nr -= b;
132 return copied ?: ret;
135 static int thread_with_stdio_release(struct inode *inode, struct file *file)
137 struct thread_with_stdio *thr =
138 container_of(file->private_data, struct thread_with_stdio, thr);
140 bch2_thread_with_file_exit(&thr->thr);
141 printbuf_exit(&thr->stdio.input_buf);
142 printbuf_exit(&thr->stdio.output_buf);
143 darray_exit(&thr->output2);
148 #define WRITE_BUFFER 4096
150 static inline bool thread_with_stdio_has_input_space(struct thread_with_stdio *thr)
152 return thr->stdio.input_buf.pos < WRITE_BUFFER || thr->thr.done;
155 static ssize_t thread_with_stdio_write(struct file *file, const char __user *ubuf,
156 size_t len, loff_t *ppos)
158 struct thread_with_stdio *thr =
159 container_of(file->private_data, struct thread_with_stdio, thr);
160 struct printbuf *buf = &thr->stdio.input_buf;
170 size_t b = len - fault_in_readable(ubuf, len);
176 spin_lock(&thr->stdio.input_lock);
177 if (buf->pos < WRITE_BUFFER)
178 bch2_printbuf_make_room(buf, min(b, WRITE_BUFFER - buf->pos));
179 b = min(len, printbuf_remaining_size(buf));
181 if (b && !copy_from_user_nofault(&buf->buf[buf->pos], ubuf, b)) {
187 spin_unlock(&thr->stdio.input_lock);
190 wake_up(&thr->stdio.input_wait);
192 if ((file->f_flags & O_NONBLOCK)) {
197 ret = wait_event_interruptible(thr->stdio.input_wait,
198 thread_with_stdio_has_input_space(thr));
204 return copied ?: ret;
207 static __poll_t thread_with_stdio_poll(struct file *file, struct poll_table_struct *wait)
209 struct thread_with_stdio *thr =
210 container_of(file->private_data, struct thread_with_stdio, thr);
212 poll_wait(file, &thr->stdio.output_wait, wait);
213 poll_wait(file, &thr->stdio.input_wait, wait);
217 if (thread_with_stdio_has_output(thr))
219 if (thread_with_stdio_has_input_space(thr))
222 mask |= EPOLLHUP|EPOLLERR;
226 static const struct file_operations thread_with_stdio_fops = {
227 .release = thread_with_stdio_release,
228 .read = thread_with_stdio_read,
229 .write = thread_with_stdio_write,
230 .poll = thread_with_stdio_poll,
234 int bch2_run_thread_with_stdio(struct thread_with_stdio *thr,
235 void (*exit)(struct thread_with_stdio *),
238 thr->stdio.input_buf = PRINTBUF;
239 thr->stdio.input_buf.atomic++;
240 spin_lock_init(&thr->stdio.input_lock);
241 init_waitqueue_head(&thr->stdio.input_wait);
243 thr->stdio.output_buf = PRINTBUF;
244 thr->stdio.output_buf.atomic++;
245 spin_lock_init(&thr->stdio.output_lock);
246 init_waitqueue_head(&thr->stdio.output_wait);
248 darray_init(&thr->output2);
251 return bch2_run_thread_with_file(&thr->thr, &thread_with_stdio_fops, fn);
254 int bch2_stdio_redirect_read(struct stdio_redirect *stdio, char *buf, size_t len)
256 wait_event(stdio->input_wait,
257 stdio->input_buf.pos || stdio->done);
262 spin_lock(&stdio->input_lock);
263 int ret = min(len, stdio->input_buf.pos);
264 stdio->input_buf.pos -= ret;
265 memcpy(buf, stdio->input_buf.buf, ret);
266 memmove(stdio->input_buf.buf,
267 stdio->input_buf.buf + ret,
268 stdio->input_buf.pos);
269 spin_unlock(&stdio->input_lock);
271 wake_up(&stdio->input_wait);
275 int bch2_stdio_redirect_readline(struct stdio_redirect *stdio, char *buf, size_t len)
277 wait_event(stdio->input_wait,
278 stdio->input_buf.pos || stdio->done);
283 spin_lock(&stdio->input_lock);
284 int ret = min(len, stdio->input_buf.pos);
285 char *n = memchr(stdio->input_buf.buf, '\n', ret);
287 ret = min(ret, n + 1 - stdio->input_buf.buf);
288 stdio->input_buf.pos -= ret;
289 memcpy(buf, stdio->input_buf.buf, ret);
290 memmove(stdio->input_buf.buf,
291 stdio->input_buf.buf + ret,
292 stdio->input_buf.pos);
293 spin_unlock(&stdio->input_lock);
295 wake_up(&stdio->input_wait);
299 #endif /* NO_BCACHEFS_FS */